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   T.Index = Table.Index;
41   return T;
42 }
43 
44 static WasmYAML::Limits makeLimits(const wasm::WasmLimits &Limits) {
45   WasmYAML::Limits L;
46   L.Flags = Limits.Flags;
47   L.Initial = Limits.Initial;
48   L.Maximum = Limits.Maximum;
49   return L;
50 }
51 
52 std::unique_ptr<WasmYAML::CustomSection>
53 WasmDumper::dumpCustomSection(const WasmSection &WasmSec) {
54   std::unique_ptr<WasmYAML::CustomSection> CustomSec;
55   if (WasmSec.Name == "dylink") {
56     std::unique_ptr<WasmYAML::DylinkSection> DylinkSec =
57         std::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;
63     DylinkSec->Needed = Info.Needed;
64     CustomSec = std::move(DylinkSec);
65   } else if (WasmSec.Name == "name") {
66     std::unique_ptr<WasmYAML::NameSection> NameSec =
67         std::make_unique<WasmYAML::NameSection>();
68     for (const llvm::wasm::WasmDebugName &Name : Obj.debugNames()) {
69       WasmYAML::NameEntry NameEntry;
70       NameEntry.Name = Name.Name;
71       NameEntry.Index = Name.Index;
72       if (Name.Type == llvm::wasm::NameType::FUNCTION) {
73         NameSec->FunctionNames.push_back(NameEntry);
74       } else {
75         assert(Name.Type == llvm::wasm::NameType::GLOBAL);
76         NameSec->GlobalNames.push_back(NameEntry);
77       }
78     }
79     CustomSec = std::move(NameSec);
80   } else if (WasmSec.Name == "linking") {
81     std::unique_ptr<WasmYAML::LinkingSection> LinkingSec =
82         std::make_unique<WasmYAML::LinkingSection>();
83     LinkingSec->Version = Obj.linkingData().Version;
84 
85     ArrayRef<StringRef> Comdats = Obj.linkingData().Comdats;
86     for (StringRef ComdatName : Comdats)
87       LinkingSec->Comdats.emplace_back(WasmYAML::Comdat{ComdatName, {}});
88     for (auto &Func : Obj.functions()) {
89       if (Func.Comdat != UINT32_MAX) {
90         LinkingSec->Comdats[Func.Comdat].Entries.emplace_back(
91             WasmYAML::ComdatEntry{wasm::WASM_COMDAT_FUNCTION, Func.Index});
92       }
93     }
94 
95     uint32_t SegmentIndex = 0;
96     for (const object::WasmSegment &Segment : Obj.dataSegments()) {
97       if (!Segment.Data.Name.empty()) {
98         WasmYAML::SegmentInfo SegmentInfo;
99         SegmentInfo.Name = Segment.Data.Name;
100         SegmentInfo.Index = SegmentIndex;
101         SegmentInfo.Alignment = Segment.Data.Alignment;
102         SegmentInfo.Flags = Segment.Data.LinkerFlags;
103         LinkingSec->SegmentInfos.push_back(SegmentInfo);
104       }
105       if (Segment.Data.Comdat != UINT32_MAX) {
106         LinkingSec->Comdats[Segment.Data.Comdat].Entries.emplace_back(
107             WasmYAML::ComdatEntry{wasm::WASM_COMDAT_DATA, SegmentIndex});
108       }
109       SegmentIndex++;
110     }
111 
112     uint32_t SymbolIndex = 0;
113     for (const wasm::WasmSymbolInfo &Symbol : Obj.linkingData().SymbolTable) {
114       WasmYAML::SymbolInfo Info;
115       Info.Index = SymbolIndex++;
116       Info.Kind = static_cast<uint32_t>(Symbol.Kind);
117       Info.Name = Symbol.Name;
118       Info.Flags = Symbol.Flags;
119       switch (Symbol.Kind) {
120       case wasm::WASM_SYMBOL_TYPE_DATA:
121         Info.DataRef = Symbol.DataRef;
122         break;
123       case wasm::WASM_SYMBOL_TYPE_FUNCTION:
124       case wasm::WASM_SYMBOL_TYPE_GLOBAL:
125       case wasm::WASM_SYMBOL_TYPE_TABLE:
126       case wasm::WASM_SYMBOL_TYPE_EVENT:
127         Info.ElementIndex = Symbol.ElementIndex;
128         break;
129       case wasm::WASM_SYMBOL_TYPE_SECTION:
130         Info.ElementIndex = Symbol.ElementIndex;
131         break;
132       }
133       LinkingSec->SymbolTable.emplace_back(Info);
134     }
135 
136     for (const wasm::WasmInitFunc &Func : Obj.linkingData().InitFunctions) {
137       WasmYAML::InitFunction F{Func.Priority, Func.Symbol};
138       LinkingSec->InitFunctions.emplace_back(F);
139     }
140 
141     CustomSec = std::move(LinkingSec);
142   } else if (WasmSec.Name == "producers") {
143     std::unique_ptr<WasmYAML::ProducersSection> ProducersSec =
144         std::make_unique<WasmYAML::ProducersSection>();
145     const llvm::wasm::WasmProducerInfo &Info = Obj.getProducerInfo();
146     for (auto &E : Info.Languages) {
147       WasmYAML::ProducerEntry Producer;
148       Producer.Name = E.first;
149       Producer.Version = E.second;
150       ProducersSec->Languages.push_back(Producer);
151     }
152     for (auto &E : Info.Tools) {
153       WasmYAML::ProducerEntry Producer;
154       Producer.Name = E.first;
155       Producer.Version = E.second;
156       ProducersSec->Tools.push_back(Producer);
157     }
158     for (auto &E : Info.SDKs) {
159       WasmYAML::ProducerEntry Producer;
160       Producer.Name = E.first;
161       Producer.Version = E.second;
162       ProducersSec->SDKs.push_back(Producer);
163     }
164     CustomSec = std::move(ProducersSec);
165   } else if (WasmSec.Name == "target_features") {
166     std::unique_ptr<WasmYAML::TargetFeaturesSection> TargetFeaturesSec =
167         std::make_unique<WasmYAML::TargetFeaturesSection>();
168     for (auto &E : Obj.getTargetFeatures()) {
169       WasmYAML::FeatureEntry Feature;
170       Feature.Prefix = E.Prefix;
171       Feature.Name = E.Name;
172       TargetFeaturesSec->Features.push_back(Feature);
173     }
174     CustomSec = std::move(TargetFeaturesSec);
175   } else {
176     CustomSec = std::make_unique<WasmYAML::CustomSection>(WasmSec.Name);
177   }
178   CustomSec->Payload = yaml::BinaryRef(WasmSec.Content);
179   return CustomSec;
180 }
181 
182 ErrorOr<WasmYAML::Object *> WasmDumper::dump() {
183   auto Y = std::make_unique<WasmYAML::Object>();
184 
185   // Dump header
186   Y->Header.Version = Obj.getHeader().Version;
187 
188   // Dump sections
189   for (const auto &Sec : Obj.sections()) {
190     const WasmSection &WasmSec = Obj.getWasmSection(Sec);
191     std::unique_ptr<WasmYAML::Section> S;
192     switch (WasmSec.Type) {
193     case wasm::WASM_SEC_CUSTOM: {
194       if (WasmSec.Name.startswith("reloc.")) {
195         // Relocations are attached the sections they apply to rather than
196         // being represented as a custom section in the YAML output.
197         continue;
198       }
199       S = dumpCustomSection(WasmSec);
200       break;
201     }
202     case wasm::WASM_SEC_TYPE: {
203       auto TypeSec = std::make_unique<WasmYAML::TypeSection>();
204       uint32_t Index = 0;
205       for (const auto &FunctionSig : Obj.types()) {
206         WasmYAML::Signature Sig;
207         Sig.Index = Index++;
208         for (const auto &ParamType : FunctionSig.Params)
209           Sig.ParamTypes.emplace_back(static_cast<uint32_t>(ParamType));
210         for (const auto &ReturnType : FunctionSig.Returns)
211           Sig.ReturnTypes.emplace_back(static_cast<uint32_t>(ReturnType));
212         TypeSec->Signatures.push_back(Sig);
213       }
214       S = std::move(TypeSec);
215       break;
216     }
217     case wasm::WASM_SEC_IMPORT: {
218       auto ImportSec = std::make_unique<WasmYAML::ImportSection>();
219       for (auto &Import : Obj.imports()) {
220         WasmYAML::Import Im;
221         Im.Module = Import.Module;
222         Im.Field = Import.Field;
223         Im.Kind = Import.Kind;
224         switch (Im.Kind) {
225         case wasm::WASM_EXTERNAL_FUNCTION:
226           Im.SigIndex = Import.SigIndex;
227           break;
228         case wasm::WASM_EXTERNAL_GLOBAL:
229           Im.GlobalImport.Type = Import.Global.Type;
230           Im.GlobalImport.Mutable = Import.Global.Mutable;
231           break;
232         case wasm::WASM_EXTERNAL_EVENT:
233           Im.EventImport.Attribute = Import.Event.Attribute;
234           Im.EventImport.SigIndex = Import.Event.SigIndex;
235           break;
236         case wasm::WASM_EXTERNAL_TABLE:
237           Im.TableImport = makeTable(Import.Table);
238           break;
239         case wasm::WASM_EXTERNAL_MEMORY:
240           Im.Memory = makeLimits(Import.Memory);
241           break;
242         }
243         ImportSec->Imports.push_back(Im);
244       }
245       S = std::move(ImportSec);
246       break;
247     }
248     case wasm::WASM_SEC_FUNCTION: {
249       auto FuncSec = std::make_unique<WasmYAML::FunctionSection>();
250       for (const auto &Func : Obj.functionTypes()) {
251         FuncSec->FunctionTypes.push_back(Func);
252       }
253       S = std::move(FuncSec);
254       break;
255     }
256     case wasm::WASM_SEC_TABLE: {
257       auto TableSec = std::make_unique<WasmYAML::TableSection>();
258       for (const wasm::WasmTable &Table : Obj.tables()) {
259         TableSec->Tables.push_back(makeTable(Table));
260       }
261       S = std::move(TableSec);
262       break;
263     }
264     case wasm::WASM_SEC_MEMORY: {
265       auto MemorySec = std::make_unique<WasmYAML::MemorySection>();
266       for (const wasm::WasmLimits &Memory : Obj.memories()) {
267         MemorySec->Memories.push_back(makeLimits(Memory));
268       }
269       S = std::move(MemorySec);
270       break;
271     }
272     case wasm::WASM_SEC_EVENT: {
273       auto EventSec = std::make_unique<WasmYAML::EventSection>();
274       for (auto &Event : Obj.events()) {
275         WasmYAML::Event E;
276         E.Index = Event.Index;
277         E.Attribute = Event.Type.Attribute;
278         E.SigIndex = Event.Type.SigIndex;
279         EventSec->Events.push_back(E);
280       }
281       S = std::move(EventSec);
282       break;
283     }
284     case wasm::WASM_SEC_GLOBAL: {
285       auto GlobalSec = std::make_unique<WasmYAML::GlobalSection>();
286       for (auto &Global : Obj.globals()) {
287         WasmYAML::Global G;
288         G.Index = Global.Index;
289         G.Type = Global.Type.Type;
290         G.Mutable = Global.Type.Mutable;
291         G.InitExpr = Global.InitExpr;
292         GlobalSec->Globals.push_back(G);
293       }
294       S = std::move(GlobalSec);
295       break;
296     }
297     case wasm::WASM_SEC_START: {
298       auto StartSec = std::make_unique<WasmYAML::StartSection>();
299       StartSec->StartFunction = Obj.startFunction();
300       S = std::move(StartSec);
301       break;
302     }
303     case wasm::WASM_SEC_EXPORT: {
304       auto ExportSec = std::make_unique<WasmYAML::ExportSection>();
305       for (auto &Export : Obj.exports()) {
306         WasmYAML::Export Ex;
307         Ex.Name = Export.Name;
308         Ex.Kind = Export.Kind;
309         Ex.Index = Export.Index;
310         ExportSec->Exports.push_back(Ex);
311       }
312       S = std::move(ExportSec);
313       break;
314     }
315     case wasm::WASM_SEC_ELEM: {
316       auto ElemSec = std::make_unique<WasmYAML::ElemSection>();
317       for (auto &Segment : Obj.elements()) {
318         WasmYAML::ElemSegment Seg;
319         Seg.TableIndex = Segment.TableIndex;
320         Seg.Offset = Segment.Offset;
321         for (auto &Func : Segment.Functions) {
322           Seg.Functions.push_back(Func);
323         }
324         ElemSec->Segments.push_back(Seg);
325       }
326       S = std::move(ElemSec);
327       break;
328     }
329     case wasm::WASM_SEC_CODE: {
330       auto CodeSec = std::make_unique<WasmYAML::CodeSection>();
331       for (auto &Func : Obj.functions()) {
332         WasmYAML::Function Function;
333         Function.Index = Func.Index;
334         for (auto &Local : Func.Locals) {
335           WasmYAML::LocalDecl LocalDecl;
336           LocalDecl.Type = Local.Type;
337           LocalDecl.Count = Local.Count;
338           Function.Locals.push_back(LocalDecl);
339         }
340         Function.Body = yaml::BinaryRef(Func.Body);
341         CodeSec->Functions.push_back(Function);
342       }
343       S = std::move(CodeSec);
344       break;
345     }
346     case wasm::WASM_SEC_DATA: {
347       auto DataSec = std::make_unique<WasmYAML::DataSection>();
348       for (const object::WasmSegment &Segment : Obj.dataSegments()) {
349         WasmYAML::DataSegment Seg;
350         Seg.SectionOffset = Segment.SectionOffset;
351         Seg.InitFlags = Segment.Data.InitFlags;
352         Seg.MemoryIndex = Segment.Data.MemoryIndex;
353         Seg.Offset = Segment.Data.Offset;
354         Seg.Content = yaml::BinaryRef(Segment.Data.Content);
355         DataSec->Segments.push_back(Seg);
356       }
357       S = std::move(DataSec);
358       break;
359     }
360     case wasm::WASM_SEC_DATACOUNT: {
361       auto DataCountSec = std::make_unique<WasmYAML::DataCountSection>();
362       DataCountSec->Count = Obj.dataSegments().size();
363       S = std::move(DataCountSec);
364       break;
365     }
366     default:
367       llvm_unreachable("Unknown section type");
368       break;
369     }
370     for (const wasm::WasmRelocation &Reloc : WasmSec.Relocations) {
371       WasmYAML::Relocation R;
372       R.Type = Reloc.Type;
373       R.Index = Reloc.Index;
374       R.Offset = Reloc.Offset;
375       R.Addend = Reloc.Addend;
376       S->Relocations.push_back(R);
377     }
378     Y->Sections.push_back(std::move(S));
379   }
380 
381   return Y.release();
382 }
383 
384 std::error_code wasm2yaml(raw_ostream &Out, const object::WasmObjectFile &Obj) {
385   WasmDumper Dumper(Obj);
386   ErrorOr<WasmYAML::Object *> YAMLOrErr = Dumper.dump();
387   if (std::error_code EC = YAMLOrErr.getError())
388     return EC;
389 
390   std::unique_ptr<WasmYAML::Object> YAML(YAMLOrErr.get());
391   yaml::Output Yout(Out);
392   Yout << *YAML;
393 
394   return std::error_code();
395 }
396