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;
17 using object::WasmSection;
18 
19 namespace {
20 
21 class WasmDumper {
22   const object::WasmObjectFile &Obj;
23 
24 public:
25   WasmDumper(const object::WasmObjectFile &O) : Obj(O) {}
26 
27   ErrorOr<WasmYAML::Object *> dump();
28 
29   std::unique_ptr<WasmYAML::CustomSection>
30   dumpCustomSection(const WasmSection &WasmSec);
31 };
32 
33 } // namespace
34 
35 static WasmYAML::Table make_table(const wasm::WasmTable &Table) {
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 
44 static WasmYAML::Limits make_limits(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         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         make_unique<WasmYAML::NameSection>();
68     for (const llvm::wasm::WasmFunctionName &Func : Obj.debugNames()) {
69       WasmYAML::NameEntry NameEntry;
70       NameEntry.Name = Func.Name;
71       NameEntry.Index = Func.Index;
72       NameSec->FunctionNames.push_back(NameEntry);
73     }
74     CustomSec = std::move(NameSec);
75   } else if (WasmSec.Name == "linking") {
76     std::unique_ptr<WasmYAML::LinkingSection> LinkingSec =
77         make_unique<WasmYAML::LinkingSection>();
78     LinkingSec->Version = Obj.linkingData().Version;
79 
80     ArrayRef<StringRef> Comdats = Obj.linkingData().Comdats;
81     for (StringRef ComdatName : Comdats)
82       LinkingSec->Comdats.emplace_back(WasmYAML::Comdat{ComdatName, {}});
83     for (auto &Func : Obj.functions()) {
84       if (Func.Comdat != UINT32_MAX) {
85         LinkingSec->Comdats[Func.Comdat].Entries.emplace_back(
86             WasmYAML::ComdatEntry{wasm::WASM_COMDAT_FUNCTION, Func.Index});
87       }
88     }
89 
90     uint32_t SegmentIndex = 0;
91     for (const object::WasmSegment &Segment : Obj.dataSegments()) {
92       if (!Segment.Data.Name.empty()) {
93         WasmYAML::SegmentInfo SegmentInfo;
94         SegmentInfo.Name = Segment.Data.Name;
95         SegmentInfo.Index = SegmentIndex;
96         SegmentInfo.Alignment = Segment.Data.Alignment;
97         SegmentInfo.Flags = Segment.Data.Flags;
98         LinkingSec->SegmentInfos.push_back(SegmentInfo);
99       }
100       if (Segment.Data.Comdat != UINT32_MAX) {
101         LinkingSec->Comdats[Segment.Data.Comdat].Entries.emplace_back(
102             WasmYAML::ComdatEntry{wasm::WASM_COMDAT_DATA, SegmentIndex});
103       }
104       SegmentIndex++;
105     }
106 
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:
120       case wasm::WASM_SYMBOL_TYPE_EVENT:
121         Info.ElementIndex = Symbol.ElementIndex;
122         break;
123       case wasm::WASM_SYMBOL_TYPE_SECTION:
124         Info.ElementIndex = Symbol.ElementIndex;
125         break;
126       }
127       LinkingSec->SymbolTable.emplace_back(Info);
128     }
129 
130     for (const wasm::WasmInitFunc &Func : Obj.linkingData().InitFunctions) {
131       WasmYAML::InitFunction F{Func.Priority, Func.Symbol};
132       LinkingSec->InitFunctions.emplace_back(F);
133     }
134 
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 
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()) {
151     const WasmSection &WasmSec = Obj.getWasmSection(Sec);
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       }
160       S = dumpCustomSection(WasmSec);
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++;
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));
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()) {
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:
193           Im.GlobalImport.Type = Import.Global.Type;
194           Im.GlobalImport.Mutable = Import.Global.Mutable;
195           break;
196         case wasm::WASM_EXTERNAL_EVENT:
197           Im.EventImport.Attribute = Import.Event.Attribute;
198           Im.EventImport.SigIndex = Import.Event.SigIndex;
199           break;
200         case wasm::WASM_EXTERNAL_TABLE:
201           Im.TableImport = make_table(Import.Table);
202           break;
203         case wasm::WASM_EXTERNAL_MEMORY:
204           Im.Memory = make_limits(Import.Memory);
205           break;
206         }
207         ImportSec->Imports.push_back(Im);
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>();
222       for (const wasm::WasmTable &Table : Obj.tables()) {
223         TableSec->Tables.push_back(make_table(Table));
224       }
225       S = std::move(TableSec);
226       break;
227     }
228     case wasm::WASM_SEC_MEMORY: {
229       auto MemorySec = make_unique<WasmYAML::MemorySection>();
230       for (const wasm::WasmLimits &Memory : Obj.memories()) {
231         MemorySec->Memories.push_back(make_limits(Memory));
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;
240         G.Index = Global.Index;
241         G.Type = Global.Type.Type;
242         G.Mutable = Global.Type.Mutable;
243         G.InitExpr = Global.InitExpr;
244         GlobalSec->Globals.push_back(G);
245       }
246       S = std::move(GlobalSec);
247       break;
248     }
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     }
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;
297         Function.Index = Func.Index;
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>();
312       for (const object::WasmSegment &Segment : Obj.dataSegments()) {
313         WasmYAML::DataSegment Seg;
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);
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     }
327     for (const wasm::WasmRelocation &Reloc : WasmSec.Relocations) {
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 
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 }
353