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