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