1 //===------ utils/wasm2yaml.cpp - obj2yaml conversion tool ------*- C++ -*-===//
2 //
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
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;
16 using object::WasmSection;
17 
18 namespace {
19 
20 class WasmDumper {
21   const object::WasmObjectFile &Obj;
22 
23 public:
24   WasmDumper(const object::WasmObjectFile &O) : Obj(O) {}
25 
26   ErrorOr<WasmYAML::Object *> dump();
27 
28   std::unique_ptr<WasmYAML::CustomSection>
29   dumpCustomSection(const WasmSection &WasmSec);
30 };
31 
32 } // namespace
33 
34 static WasmYAML::Table makeTable(const wasm::WasmTable &Table) {
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 
43 static WasmYAML::Limits makeLimits(const wasm::WasmLimits &Limits) {
44   WasmYAML::Limits L;
45   L.Flags = Limits.Flags;
46   L.Initial = Limits.Initial;
47   L.Maximum = Limits.Maximum;
48   return L;
49 }
50 
51 std::unique_ptr<WasmYAML::CustomSection>
52 WasmDumper::dumpCustomSection(const WasmSection &WasmSec) {
53   std::unique_ptr<WasmYAML::CustomSection> CustomSec;
54   if (WasmSec.Name == "dylink") {
55     std::unique_ptr<WasmYAML::DylinkSection> DylinkSec =
56         std::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;
62     DylinkSec->Needed = Info.Needed;
63     CustomSec = std::move(DylinkSec);
64   } else if (WasmSec.Name == "name") {
65     std::unique_ptr<WasmYAML::NameSection> NameSec =
66         std::make_unique<WasmYAML::NameSection>();
67     for (const llvm::wasm::WasmFunctionName &Func : Obj.debugNames()) {
68       WasmYAML::NameEntry NameEntry;
69       NameEntry.Name = Func.Name;
70       NameEntry.Index = Func.Index;
71       NameSec->FunctionNames.push_back(NameEntry);
72     }
73     CustomSec = std::move(NameSec);
74   } else if (WasmSec.Name == "linking") {
75     std::unique_ptr<WasmYAML::LinkingSection> LinkingSec =
76         std::make_unique<WasmYAML::LinkingSection>();
77     LinkingSec->Version = Obj.linkingData().Version;
78 
79     ArrayRef<StringRef> Comdats = Obj.linkingData().Comdats;
80     for (StringRef ComdatName : Comdats)
81       LinkingSec->Comdats.emplace_back(WasmYAML::Comdat{ComdatName, {}});
82     for (auto &Func : Obj.functions()) {
83       if (Func.Comdat != UINT32_MAX) {
84         LinkingSec->Comdats[Func.Comdat].Entries.emplace_back(
85             WasmYAML::ComdatEntry{wasm::WASM_COMDAT_FUNCTION, Func.Index});
86       }
87     }
88 
89     uint32_t SegmentIndex = 0;
90     for (const object::WasmSegment &Segment : Obj.dataSegments()) {
91       if (!Segment.Data.Name.empty()) {
92         WasmYAML::SegmentInfo SegmentInfo;
93         SegmentInfo.Name = Segment.Data.Name;
94         SegmentInfo.Index = SegmentIndex;
95         SegmentInfo.Alignment = Segment.Data.Alignment;
96         SegmentInfo.Flags = Segment.Data.LinkerFlags;
97         LinkingSec->SegmentInfos.push_back(SegmentInfo);
98       }
99       if (Segment.Data.Comdat != UINT32_MAX) {
100         LinkingSec->Comdats[Segment.Data.Comdat].Entries.emplace_back(
101             WasmYAML::ComdatEntry{wasm::WASM_COMDAT_DATA, SegmentIndex});
102       }
103       SegmentIndex++;
104     }
105 
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:
119       case wasm::WASM_SYMBOL_TYPE_EVENT:
120         Info.ElementIndex = Symbol.ElementIndex;
121         break;
122       case wasm::WASM_SYMBOL_TYPE_SECTION:
123         Info.ElementIndex = Symbol.ElementIndex;
124         break;
125       }
126       LinkingSec->SymbolTable.emplace_back(Info);
127     }
128 
129     for (const wasm::WasmInitFunc &Func : Obj.linkingData().InitFunctions) {
130       WasmYAML::InitFunction F{Func.Priority, Func.Symbol};
131       LinkingSec->InitFunctions.emplace_back(F);
132     }
133 
134     CustomSec = std::move(LinkingSec);
135   } else if (WasmSec.Name == "producers") {
136     std::unique_ptr<WasmYAML::ProducersSection> ProducersSec =
137         std::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);
158   } else if (WasmSec.Name == "target_features") {
159     std::unique_ptr<WasmYAML::TargetFeaturesSection> TargetFeaturesSec =
160         std::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);
168   } else {
169     CustomSec = std::make_unique<WasmYAML::CustomSection>(WasmSec.Name);
170   }
171   CustomSec->Payload = yaml::BinaryRef(WasmSec.Content);
172   return CustomSec;
173 }
174 
175 ErrorOr<WasmYAML::Object *> WasmDumper::dump() {
176   auto Y = std::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()) {
183     const WasmSection &WasmSec = Obj.getWasmSection(Sec);
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       }
192       S = dumpCustomSection(WasmSec);
193       break;
194     }
195     case wasm::WASM_SEC_TYPE: {
196       auto TypeSec = std::make_unique<WasmYAML::TypeSection>();
197       uint32_t Index = 0;
198       for (const auto &FunctionSig : Obj.types()) {
199         WasmYAML::Signature Sig;
200         Sig.Index = Index++;
201         for (const auto &ParamType : FunctionSig.Params)
202           Sig.ParamTypes.emplace_back(static_cast<uint32_t>(ParamType));
203         for (const auto &ReturnType : FunctionSig.Returns)
204           Sig.ReturnTypes.emplace_back(static_cast<uint32_t>(ReturnType));
205         TypeSec->Signatures.push_back(Sig);
206       }
207       S = std::move(TypeSec);
208       break;
209     }
210     case wasm::WASM_SEC_IMPORT: {
211       auto ImportSec = std::make_unique<WasmYAML::ImportSection>();
212       for (auto &Import : Obj.imports()) {
213         WasmYAML::Import Im;
214         Im.Module = Import.Module;
215         Im.Field = Import.Field;
216         Im.Kind = Import.Kind;
217         switch (Im.Kind) {
218         case wasm::WASM_EXTERNAL_FUNCTION:
219           Im.SigIndex = Import.SigIndex;
220           break;
221         case wasm::WASM_EXTERNAL_GLOBAL:
222           Im.GlobalImport.Type = Import.Global.Type;
223           Im.GlobalImport.Mutable = Import.Global.Mutable;
224           break;
225         case wasm::WASM_EXTERNAL_EVENT:
226           Im.EventImport.Attribute = Import.Event.Attribute;
227           Im.EventImport.SigIndex = Import.Event.SigIndex;
228           break;
229         case wasm::WASM_EXTERNAL_TABLE:
230           Im.TableImport = makeTable(Import.Table);
231           break;
232         case wasm::WASM_EXTERNAL_MEMORY:
233           Im.Memory = makeLimits(Import.Memory);
234           break;
235         }
236         ImportSec->Imports.push_back(Im);
237       }
238       S = std::move(ImportSec);
239       break;
240     }
241     case wasm::WASM_SEC_FUNCTION: {
242       auto FuncSec = std::make_unique<WasmYAML::FunctionSection>();
243       for (const auto &Func : Obj.functionTypes()) {
244         FuncSec->FunctionTypes.push_back(Func);
245       }
246       S = std::move(FuncSec);
247       break;
248     }
249     case wasm::WASM_SEC_TABLE: {
250       auto TableSec = std::make_unique<WasmYAML::TableSection>();
251       for (const wasm::WasmTable &Table : Obj.tables()) {
252         TableSec->Tables.push_back(makeTable(Table));
253       }
254       S = std::move(TableSec);
255       break;
256     }
257     case wasm::WASM_SEC_MEMORY: {
258       auto MemorySec = std::make_unique<WasmYAML::MemorySection>();
259       for (const wasm::WasmLimits &Memory : Obj.memories()) {
260         MemorySec->Memories.push_back(makeLimits(Memory));
261       }
262       S = std::move(MemorySec);
263       break;
264     }
265     case wasm::WASM_SEC_EVENT: {
266       auto EventSec = std::make_unique<WasmYAML::EventSection>();
267       for (auto &Event : Obj.events()) {
268         WasmYAML::Event E;
269         E.Index = Event.Index;
270         E.Attribute = Event.Type.Attribute;
271         E.SigIndex = Event.Type.SigIndex;
272         EventSec->Events.push_back(E);
273       }
274       S = std::move(EventSec);
275       break;
276     }
277     case wasm::WASM_SEC_GLOBAL: {
278       auto GlobalSec = std::make_unique<WasmYAML::GlobalSection>();
279       for (auto &Global : Obj.globals()) {
280         WasmYAML::Global G;
281         G.Index = Global.Index;
282         G.Type = Global.Type.Type;
283         G.Mutable = Global.Type.Mutable;
284         G.InitExpr = Global.InitExpr;
285         GlobalSec->Globals.push_back(G);
286       }
287       S = std::move(GlobalSec);
288       break;
289     }
290     case wasm::WASM_SEC_START: {
291       auto StartSec = std::make_unique<WasmYAML::StartSection>();
292       StartSec->StartFunction = Obj.startFunction();
293       S = std::move(StartSec);
294       break;
295     }
296     case wasm::WASM_SEC_EXPORT: {
297       auto ExportSec = std::make_unique<WasmYAML::ExportSection>();
298       for (auto &Export : Obj.exports()) {
299         WasmYAML::Export Ex;
300         Ex.Name = Export.Name;
301         Ex.Kind = Export.Kind;
302         Ex.Index = Export.Index;
303         ExportSec->Exports.push_back(Ex);
304       }
305       S = std::move(ExportSec);
306       break;
307     }
308     case wasm::WASM_SEC_ELEM: {
309       auto ElemSec = std::make_unique<WasmYAML::ElemSection>();
310       for (auto &Segment : Obj.elements()) {
311         WasmYAML::ElemSegment Seg;
312         Seg.TableIndex = Segment.TableIndex;
313         Seg.Offset = Segment.Offset;
314         for (auto &Func : Segment.Functions) {
315           Seg.Functions.push_back(Func);
316         }
317         ElemSec->Segments.push_back(Seg);
318       }
319       S = std::move(ElemSec);
320       break;
321     }
322     case wasm::WASM_SEC_CODE: {
323       auto CodeSec = std::make_unique<WasmYAML::CodeSection>();
324       for (auto &Func : Obj.functions()) {
325         WasmYAML::Function Function;
326         Function.Index = Func.Index;
327         for (auto &Local : Func.Locals) {
328           WasmYAML::LocalDecl LocalDecl;
329           LocalDecl.Type = Local.Type;
330           LocalDecl.Count = Local.Count;
331           Function.Locals.push_back(LocalDecl);
332         }
333         Function.Body = yaml::BinaryRef(Func.Body);
334         CodeSec->Functions.push_back(Function);
335       }
336       S = std::move(CodeSec);
337       break;
338     }
339     case wasm::WASM_SEC_DATA: {
340       auto DataSec = std::make_unique<WasmYAML::DataSection>();
341       for (const object::WasmSegment &Segment : Obj.dataSegments()) {
342         WasmYAML::DataSegment Seg;
343         Seg.SectionOffset = Segment.SectionOffset;
344         Seg.InitFlags = Segment.Data.InitFlags;
345         Seg.MemoryIndex = Segment.Data.MemoryIndex;
346         Seg.Offset = Segment.Data.Offset;
347         Seg.Content = yaml::BinaryRef(Segment.Data.Content);
348         DataSec->Segments.push_back(Seg);
349       }
350       S = std::move(DataSec);
351       break;
352     }
353     case wasm::WASM_SEC_DATACOUNT: {
354       auto DataCountSec = std::make_unique<WasmYAML::DataCountSection>();
355       DataCountSec->Count = Obj.dataSegments().size();
356       S = std::move(DataCountSec);
357       break;
358     }
359     default:
360       llvm_unreachable("Unknown section type");
361       break;
362     }
363     for (const wasm::WasmRelocation &Reloc : WasmSec.Relocations) {
364       WasmYAML::Relocation R;
365       R.Type = Reloc.Type;
366       R.Index = Reloc.Index;
367       R.Offset = Reloc.Offset;
368       R.Addend = Reloc.Addend;
369       S->Relocations.push_back(R);
370     }
371     Y->Sections.push_back(std::move(S));
372   }
373 
374   return Y.release();
375 }
376 
377 std::error_code wasm2yaml(raw_ostream &Out, const object::WasmObjectFile &Obj) {
378   WasmDumper Dumper(Obj);
379   ErrorOr<WasmYAML::Object *> YAMLOrErr = Dumper.dump();
380   if (std::error_code EC = YAMLOrErr.getError())
381     return EC;
382 
383   std::unique_ptr<WasmYAML::Object> YAML(YAMLOrErr.get());
384   yaml::Output Yout(Out);
385   Yout << *YAML;
386 
387   return std::error_code();
388 }
389