Derek Schuff | 349a48f | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 1 | //===------ utils/wasm2yaml.cpp - obj2yaml conversion tool ------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "obj2yaml.h" |
| 11 | #include "llvm/Object/COFF.h" |
| 12 | #include "llvm/ObjectYAML/WasmYAML.h" |
| 13 | #include "llvm/Support/ErrorHandling.h" |
| 14 | #include "llvm/Support/YAMLTraits.h" |
| 15 | |
| 16 | using namespace llvm; |
Sam Clegg | 1e975c1 | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 17 | using object::WasmSection; |
Derek Schuff | 349a48f | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 18 | |
| 19 | namespace { |
| 20 | |
| 21 | class WasmDumper { |
| 22 | const object::WasmObjectFile &Obj; |
| 23 | |
| 24 | public: |
| 25 | WasmDumper(const object::WasmObjectFile &O) : Obj(O) {} |
Sam Clegg | 1e975c1 | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 26 | |
Derek Schuff | 349a48f | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 27 | ErrorOr<WasmYAML::Object *> dump(); |
Sam Clegg | 1e975c1 | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 28 | |
| 29 | std::unique_ptr<WasmYAML::CustomSection> |
| 30 | dumpCustomSection(const WasmSection &WasmSec); |
Derek Schuff | 349a48f | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
Sam Clegg | 1e975c1 | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 33 | } // namespace |
| 34 | |
| 35 | static WasmYAML::Table make_table(const wasm::WasmTable &Table) { |
Sam Clegg | 65e284e | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 36 | WasmYAML::Table T; |
| 37 | T.ElemType = Table.ElemType; |
| 38 | T.TableLimits.Flags = Table.Limits.Flags; |
| 39 | T.TableLimits.Initial = Table.Limits.Initial; |
| 40 | T.TableLimits.Maximum = Table.Limits.Maximum; |
| 41 | return T; |
| 42 | } |
| 43 | |
Sam Clegg | 1e975c1 | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 44 | static WasmYAML::Limits make_limits(const wasm::WasmLimits &Limits) { |
Sam Clegg | 65e284e | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 45 | WasmYAML::Limits L; |
| 46 | L.Flags = Limits.Flags; |
| 47 | L.Initial = Limits.Initial; |
| 48 | L.Maximum = Limits.Maximum; |
| 49 | return L; |
| 50 | } |
| 51 | |
Heejin Ahn | 581d231 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 52 | std::unique_ptr<WasmYAML::CustomSection> |
| 53 | WasmDumper::dumpCustomSection(const WasmSection &WasmSec) { |
Sam Clegg | 1e975c1 | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 54 | std::unique_ptr<WasmYAML::CustomSection> CustomSec; |
Sam Clegg | 45c215a | 2018-11-14 18:36:24 +0000 | [diff] [blame] | 55 | if (WasmSec.Name == "dylink") { |
| 56 | std::unique_ptr<WasmYAML::DylinkSection> DylinkSec = |
| 57 | make_unique<WasmYAML::DylinkSection>(); |
| 58 | const wasm::WasmDylinkInfo& Info = Obj.dylinkInfo(); |
| 59 | DylinkSec->MemorySize = Info.MemorySize; |
| 60 | DylinkSec->MemoryAlignment = Info.MemoryAlignment; |
| 61 | DylinkSec->TableSize = Info.TableSize; |
| 62 | DylinkSec->TableAlignment = Info.TableAlignment; |
Sam Clegg | 559201d | 2018-12-12 23:40:58 +0000 | [diff] [blame] | 63 | DylinkSec->Needed = Info.Needed; |
Sam Clegg | 45c215a | 2018-11-14 18:36:24 +0000 | [diff] [blame] | 64 | CustomSec = std::move(DylinkSec); |
| 65 | } else if (WasmSec.Name == "name") { |
Heejin Ahn | 581d231 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 66 | std::unique_ptr<WasmYAML::NameSection> NameSec = |
| 67 | make_unique<WasmYAML::NameSection>(); |
| 68 | for (const llvm::wasm::WasmFunctionName &Func : Obj.debugNames()) { |
Sam Clegg | 1e975c1 | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 69 | WasmYAML::NameEntry NameEntry; |
Sam Clegg | eb7fd73 | 2018-01-17 19:28:43 +0000 | [diff] [blame] | 70 | NameEntry.Name = Func.Name; |
| 71 | NameEntry.Index = Func.Index; |
Sam Clegg | 1e975c1 | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 72 | NameSec->FunctionNames.push_back(NameEntry); |
| 73 | } |
| 74 | CustomSec = std::move(NameSec); |
| 75 | } else if (WasmSec.Name == "linking") { |
Heejin Ahn | 581d231 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 76 | std::unique_ptr<WasmYAML::LinkingSection> LinkingSec = |
| 77 | make_unique<WasmYAML::LinkingSection>(); |
Sam Clegg | 14598cb | 2018-04-26 18:15:32 +0000 | [diff] [blame] | 78 | LinkingSec->Version = Obj.linkingData().Version; |
| 79 | |
Nicholas Wilson | dc84b5c | 2018-03-14 15:44:45 +0000 | [diff] [blame] | 80 | ArrayRef<StringRef> Comdats = Obj.linkingData().Comdats; |
| 81 | for (StringRef ComdatName : Comdats) |
Sam Clegg | e92250a | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 82 | LinkingSec->Comdats.emplace_back(WasmYAML::Comdat{ComdatName, {}}); |
Sam Clegg | e92250a | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 83 | for (auto &Func : Obj.functions()) { |
Nicholas Wilson | dc84b5c | 2018-03-14 15:44:45 +0000 | [diff] [blame] | 84 | if (Func.Comdat != UINT32_MAX) { |
| 85 | LinkingSec->Comdats[Func.Comdat].Entries.emplace_back( |
Heejin Ahn | 581d231 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 86 | WasmYAML::ComdatEntry{wasm::WASM_COMDAT_FUNCTION, Func.Index}); |
Sam Clegg | e92250a | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
Sam Clegg | 14598cb | 2018-04-26 18:15:32 +0000 | [diff] [blame] | 89 | |
Sam Clegg | e92250a | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 90 | uint32_t SegmentIndex = 0; |
Sam Clegg | 81e3824 | 2017-09-20 19:03:35 +0000 | [diff] [blame] | 91 | for (const object::WasmSegment &Segment : Obj.dataSegments()) { |
| 92 | if (!Segment.Data.Name.empty()) { |
Sam Clegg | e286417 | 2017-09-29 16:50:08 +0000 | [diff] [blame] | 93 | WasmYAML::SegmentInfo SegmentInfo; |
| 94 | SegmentInfo.Name = Segment.Data.Name; |
Sam Clegg | e92250a | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 95 | SegmentInfo.Index = SegmentIndex; |
Sam Clegg | e286417 | 2017-09-29 16:50:08 +0000 | [diff] [blame] | 96 | SegmentInfo.Alignment = Segment.Data.Alignment; |
| 97 | SegmentInfo.Flags = Segment.Data.Flags; |
| 98 | LinkingSec->SegmentInfos.push_back(SegmentInfo); |
Sam Clegg | 81e3824 | 2017-09-20 19:03:35 +0000 | [diff] [blame] | 99 | } |
Nicholas Wilson | dc84b5c | 2018-03-14 15:44:45 +0000 | [diff] [blame] | 100 | if (Segment.Data.Comdat != UINT32_MAX) { |
| 101 | LinkingSec->Comdats[Segment.Data.Comdat].Entries.emplace_back( |
Sam Clegg | e92250a | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 102 | WasmYAML::ComdatEntry{wasm::WASM_COMDAT_DATA, SegmentIndex}); |
| 103 | } |
| 104 | SegmentIndex++; |
Sam Clegg | 81e3824 | 2017-09-20 19:03:35 +0000 | [diff] [blame] | 105 | } |
Sam Clegg | 14598cb | 2018-04-26 18:15:32 +0000 | [diff] [blame] | 106 | |
Sam Clegg | d578479 | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 107 | uint32_t SymbolIndex = 0; |
| 108 | for (const wasm::WasmSymbolInfo &Symbol : Obj.linkingData().SymbolTable) { |
| 109 | WasmYAML::SymbolInfo Info; |
| 110 | Info.Index = SymbolIndex++; |
| 111 | Info.Kind = static_cast<uint32_t>(Symbol.Kind); |
| 112 | Info.Name = Symbol.Name; |
| 113 | Info.Flags = Symbol.Flags; |
| 114 | switch (Symbol.Kind) { |
| 115 | case wasm::WASM_SYMBOL_TYPE_DATA: |
| 116 | Info.DataRef = Symbol.DataRef; |
| 117 | break; |
| 118 | case wasm::WASM_SYMBOL_TYPE_FUNCTION: |
| 119 | case wasm::WASM_SYMBOL_TYPE_GLOBAL: |
Heejin Ahn | 8bcdd04 | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 120 | case wasm::WASM_SYMBOL_TYPE_EVENT: |
Sam Clegg | d578479 | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 121 | Info.ElementIndex = Symbol.ElementIndex; |
| 122 | break; |
Sam Clegg | db15975 | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 123 | case wasm::WASM_SYMBOL_TYPE_SECTION: |
| 124 | Info.ElementIndex = Symbol.ElementIndex; |
| 125 | break; |
Sam Clegg | 1e975c1 | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 126 | } |
Sam Clegg | d578479 | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 127 | LinkingSec->SymbolTable.emplace_back(Info); |
Sam Clegg | 1e975c1 | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 128 | } |
Sam Clegg | 14598cb | 2018-04-26 18:15:32 +0000 | [diff] [blame] | 129 | |
Sam Clegg | e1acb56 | 2017-12-14 21:10:03 +0000 | [diff] [blame] | 130 | for (const wasm::WasmInitFunc &Func : Obj.linkingData().InitFunctions) { |
Sam Clegg | d578479 | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 131 | WasmYAML::InitFunction F{Func.Priority, Func.Symbol}; |
Sam Clegg | e1acb56 | 2017-12-14 21:10:03 +0000 | [diff] [blame] | 132 | LinkingSec->InitFunctions.emplace_back(F); |
| 133 | } |
Sam Clegg | 14598cb | 2018-04-26 18:15:32 +0000 | [diff] [blame] | 134 | |
Sam Clegg | 1e975c1 | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 135 | CustomSec = std::move(LinkingSec); |
| 136 | } else { |
| 137 | CustomSec = make_unique<WasmYAML::CustomSection>(WasmSec.Name); |
| 138 | } |
| 139 | CustomSec->Payload = yaml::BinaryRef(WasmSec.Content); |
| 140 | return CustomSec; |
| 141 | } |
| 142 | |
Derek Schuff | 349a48f | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 143 | ErrorOr<WasmYAML::Object *> WasmDumper::dump() { |
| 144 | auto Y = make_unique<WasmYAML::Object>(); |
| 145 | |
| 146 | // Dump header |
| 147 | Y->Header.Version = Obj.getHeader().Version; |
| 148 | |
| 149 | // Dump sections |
| 150 | for (const auto &Sec : Obj.sections()) { |
Sam Clegg | 1e975c1 | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 151 | const WasmSection &WasmSec = Obj.getWasmSection(Sec); |
Derek Schuff | 349a48f | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 152 | std::unique_ptr<WasmYAML::Section> S; |
| 153 | switch (WasmSec.Type) { |
| 154 | case wasm::WASM_SEC_CUSTOM: { |
| 155 | if (WasmSec.Name.startswith("reloc.")) { |
| 156 | // Relocations are attached the sections they apply to rather than |
| 157 | // being represented as a custom section in the YAML output. |
| 158 | continue; |
| 159 | } |
Sam Clegg | 1e975c1 | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 160 | S = dumpCustomSection(WasmSec); |
Derek Schuff | 349a48f | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 161 | break; |
| 162 | } |
| 163 | case wasm::WASM_SEC_TYPE: { |
| 164 | auto TypeSec = make_unique<WasmYAML::TypeSection>(); |
| 165 | uint32_t Index = 0; |
| 166 | for (const auto &FunctionSig : Obj.types()) { |
| 167 | WasmYAML::Signature Sig; |
| 168 | Sig.Index = Index++; |
Derek Schuff | ab9755b | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 169 | Sig.ReturnType = wasm::WASM_TYPE_NORESULT; |
| 170 | assert(FunctionSig.Returns.size() <= 1 && |
| 171 | "Functions with multiple returns are not supported"); |
| 172 | if (FunctionSig.Returns.size()) |
| 173 | Sig.ReturnType = static_cast<uint32_t>(FunctionSig.Returns[0]); |
| 174 | for (const auto &ParamType : FunctionSig.Params) |
| 175 | Sig.ParamTypes.push_back(static_cast<uint32_t>(ParamType)); |
Derek Schuff | 349a48f | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 176 | TypeSec->Signatures.push_back(Sig); |
| 177 | } |
| 178 | S = std::move(TypeSec); |
| 179 | break; |
| 180 | } |
| 181 | case wasm::WASM_SEC_IMPORT: { |
| 182 | auto ImportSec = make_unique<WasmYAML::ImportSection>(); |
| 183 | for (auto &Import : Obj.imports()) { |
Sam Clegg | 65e284e | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 184 | WasmYAML::Import Im; |
| 185 | Im.Module = Import.Module; |
| 186 | Im.Field = Import.Field; |
| 187 | Im.Kind = Import.Kind; |
| 188 | switch (Im.Kind) { |
| 189 | case wasm::WASM_EXTERNAL_FUNCTION: |
| 190 | Im.SigIndex = Import.SigIndex; |
| 191 | break; |
| 192 | case wasm::WASM_EXTERNAL_GLOBAL: |
Sam Clegg | bde17ff | 2017-05-10 00:14:04 +0000 | [diff] [blame] | 193 | Im.GlobalImport.Type = Import.Global.Type; |
| 194 | Im.GlobalImport.Mutable = Import.Global.Mutable; |
Sam Clegg | 65e284e | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 195 | break; |
Heejin Ahn | 8bcdd04 | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 196 | case wasm::WASM_EXTERNAL_EVENT: |
| 197 | Im.EventImport.Attribute = Import.Event.Attribute; |
| 198 | Im.EventImport.SigIndex = Import.Event.SigIndex; |
| 199 | break; |
Sam Clegg | 65e284e | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 200 | case wasm::WASM_EXTERNAL_TABLE: |
Sam Clegg | bde17ff | 2017-05-10 00:14:04 +0000 | [diff] [blame] | 201 | Im.TableImport = make_table(Import.Table); |
Sam Clegg | 65e284e | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 202 | break; |
| 203 | case wasm::WASM_EXTERNAL_MEMORY: |
| 204 | Im.Memory = make_limits(Import.Memory); |
| 205 | break; |
Derek Schuff | 349a48f | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 206 | } |
Sam Clegg | 65e284e | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 207 | ImportSec->Imports.push_back(Im); |
Derek Schuff | 349a48f | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 208 | } |
| 209 | S = std::move(ImportSec); |
| 210 | break; |
| 211 | } |
| 212 | case wasm::WASM_SEC_FUNCTION: { |
| 213 | auto FuncSec = make_unique<WasmYAML::FunctionSection>(); |
| 214 | for (const auto &Func : Obj.functionTypes()) { |
| 215 | FuncSec->FunctionTypes.push_back(Func); |
| 216 | } |
| 217 | S = std::move(FuncSec); |
| 218 | break; |
| 219 | } |
| 220 | case wasm::WASM_SEC_TABLE: { |
| 221 | auto TableSec = make_unique<WasmYAML::TableSection>(); |
Sam Clegg | 65e284e | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 222 | for (const wasm::WasmTable &Table : Obj.tables()) { |
| 223 | TableSec->Tables.push_back(make_table(Table)); |
Derek Schuff | 349a48f | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 224 | } |
| 225 | S = std::move(TableSec); |
| 226 | break; |
| 227 | } |
| 228 | case wasm::WASM_SEC_MEMORY: { |
| 229 | auto MemorySec = make_unique<WasmYAML::MemorySection>(); |
Sam Clegg | 65e284e | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 230 | for (const wasm::WasmLimits &Memory : Obj.memories()) { |
| 231 | MemorySec->Memories.push_back(make_limits(Memory)); |
Derek Schuff | 349a48f | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 232 | } |
| 233 | S = std::move(MemorySec); |
| 234 | break; |
| 235 | } |
| 236 | case wasm::WASM_SEC_GLOBAL: { |
| 237 | auto GlobalSec = make_unique<WasmYAML::GlobalSection>(); |
| 238 | for (auto &Global : Obj.globals()) { |
| 239 | WasmYAML::Global G; |
Sam Clegg | 6b54fc3 | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 240 | G.Index = Global.Index; |
Sam Clegg | bfa1dc6 | 2018-01-31 19:50:14 +0000 | [diff] [blame] | 241 | G.Type = Global.Type.Type; |
| 242 | G.Mutable = Global.Type.Mutable; |
Derek Schuff | 349a48f | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 243 | G.InitExpr = Global.InitExpr; |
| 244 | GlobalSec->Globals.push_back(G); |
| 245 | } |
| 246 | S = std::move(GlobalSec); |
| 247 | break; |
| 248 | } |
Heejin Ahn | 8bcdd04 | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 249 | case wasm::WASM_SEC_EVENT: { |
| 250 | auto EventSec = make_unique<WasmYAML::EventSection>(); |
| 251 | for (auto &Event : Obj.events()) { |
| 252 | WasmYAML::Event E; |
| 253 | E.Index = Event.Index; |
| 254 | E.Attribute = Event.Type.Attribute; |
| 255 | E.SigIndex = Event.Type.SigIndex; |
| 256 | EventSec->Events.push_back(E); |
| 257 | } |
| 258 | S = std::move(EventSec); |
| 259 | break; |
| 260 | } |
Derek Schuff | 349a48f | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 261 | case wasm::WASM_SEC_START: { |
| 262 | auto StartSec = make_unique<WasmYAML::StartSection>(); |
| 263 | StartSec->StartFunction = Obj.startFunction(); |
| 264 | S = std::move(StartSec); |
| 265 | break; |
| 266 | } |
| 267 | case wasm::WASM_SEC_EXPORT: { |
| 268 | auto ExportSec = make_unique<WasmYAML::ExportSection>(); |
| 269 | for (auto &Export : Obj.exports()) { |
| 270 | WasmYAML::Export Ex; |
| 271 | Ex.Name = Export.Name; |
| 272 | Ex.Kind = Export.Kind; |
| 273 | Ex.Index = Export.Index; |
| 274 | ExportSec->Exports.push_back(Ex); |
| 275 | } |
| 276 | S = std::move(ExportSec); |
| 277 | break; |
| 278 | } |
| 279 | case wasm::WASM_SEC_ELEM: { |
| 280 | auto ElemSec = make_unique<WasmYAML::ElemSection>(); |
| 281 | for (auto &Segment : Obj.elements()) { |
| 282 | WasmYAML::ElemSegment Seg; |
| 283 | Seg.TableIndex = Segment.TableIndex; |
| 284 | Seg.Offset = Segment.Offset; |
| 285 | for (auto &Func : Segment.Functions) { |
| 286 | Seg.Functions.push_back(Func); |
| 287 | } |
| 288 | ElemSec->Segments.push_back(Seg); |
| 289 | } |
| 290 | S = std::move(ElemSec); |
| 291 | break; |
| 292 | } |
| 293 | case wasm::WASM_SEC_CODE: { |
| 294 | auto CodeSec = make_unique<WasmYAML::CodeSection>(); |
| 295 | for (auto &Func : Obj.functions()) { |
| 296 | WasmYAML::Function Function; |
Sam Clegg | 6b54fc3 | 2018-01-09 21:38:53 +0000 | [diff] [blame] | 297 | Function.Index = Func.Index; |
Derek Schuff | 349a48f | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 298 | for (auto &Local : Func.Locals) { |
| 299 | WasmYAML::LocalDecl LocalDecl; |
| 300 | LocalDecl.Type = Local.Type; |
| 301 | LocalDecl.Count = Local.Count; |
| 302 | Function.Locals.push_back(LocalDecl); |
| 303 | } |
| 304 | Function.Body = yaml::BinaryRef(Func.Body); |
| 305 | CodeSec->Functions.push_back(Function); |
| 306 | } |
| 307 | S = std::move(CodeSec); |
| 308 | break; |
| 309 | } |
| 310 | case wasm::WASM_SEC_DATA: { |
| 311 | auto DataSec = make_unique<WasmYAML::DataSection>(); |
Sam Clegg | 81e3824 | 2017-09-20 19:03:35 +0000 | [diff] [blame] | 312 | for (const object::WasmSegment &Segment : Obj.dataSegments()) { |
Derek Schuff | 349a48f | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 313 | WasmYAML::DataSegment Seg; |
Sam Clegg | c9c28d9 | 2017-07-12 00:24:54 +0000 | [diff] [blame] | 314 | Seg.SectionOffset = Segment.SectionOffset; |
| 315 | Seg.MemoryIndex = Segment.Data.MemoryIndex; |
| 316 | Seg.Offset = Segment.Data.Offset; |
| 317 | Seg.Content = yaml::BinaryRef(Segment.Data.Content); |
Derek Schuff | 349a48f | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 318 | DataSec->Segments.push_back(Seg); |
| 319 | } |
| 320 | S = std::move(DataSec); |
| 321 | break; |
| 322 | } |
| 323 | default: |
| 324 | llvm_unreachable("Unknown section type"); |
| 325 | break; |
| 326 | } |
Heejin Ahn | 581d231 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 327 | for (const wasm::WasmRelocation &Reloc : WasmSec.Relocations) { |
Derek Schuff | 349a48f | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 328 | WasmYAML::Relocation R; |
| 329 | R.Type = Reloc.Type; |
| 330 | R.Index = Reloc.Index; |
| 331 | R.Offset = Reloc.Offset; |
| 332 | R.Addend = Reloc.Addend; |
| 333 | S->Relocations.push_back(R); |
| 334 | } |
| 335 | Y->Sections.push_back(std::move(S)); |
| 336 | } |
| 337 | |
| 338 | return Y.release(); |
| 339 | } |
| 340 | |
Derek Schuff | 349a48f | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 341 | std::error_code wasm2yaml(raw_ostream &Out, const object::WasmObjectFile &Obj) { |
| 342 | WasmDumper Dumper(Obj); |
| 343 | ErrorOr<WasmYAML::Object *> YAMLOrErr = Dumper.dump(); |
| 344 | if (std::error_code EC = YAMLOrErr.getError()) |
| 345 | return EC; |
| 346 | |
| 347 | std::unique_ptr<WasmYAML::Object> YAML(YAMLOrErr.get()); |
| 348 | yaml::Output Yout(Out); |
| 349 | Yout << *YAML; |
| 350 | |
| 351 | return std::error_code(); |
| 352 | } |