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