blob: 0bd5642b7600fca7366d3351042ee307dd9616aa [file] [log] [blame]
Derek Schuffd3d84fd2017-03-30 19:44:09 +00001//===------ 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
16using namespace llvm;
Sam Cleggb7787fd2017-06-20 04:04:59 +000017using object::WasmSection;
Derek Schuffd3d84fd2017-03-30 19:44:09 +000018
19namespace {
20
21class WasmDumper {
22 const object::WasmObjectFile &Obj;
23
24public:
25 WasmDumper(const object::WasmObjectFile &O) : Obj(O) {}
Sam Cleggb7787fd2017-06-20 04:04:59 +000026
Derek Schuffd3d84fd2017-03-30 19:44:09 +000027 ErrorOr<WasmYAML::Object *> dump();
Sam Cleggb7787fd2017-06-20 04:04:59 +000028
29 std::unique_ptr<WasmYAML::CustomSection>
30 dumpCustomSection(const WasmSection &WasmSec);
Derek Schuffd3d84fd2017-03-30 19:44:09 +000031};
32
Sam Cleggb7787fd2017-06-20 04:04:59 +000033} // namespace
34
35static WasmYAML::Table make_table(const wasm::WasmTable &Table) {
Sam Clegg2ffff5a2017-05-09 23:48:41 +000036 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
Sam Cleggb7787fd2017-06-20 04:04:59 +000044static WasmYAML::Limits make_limits(const wasm::WasmLimits &Limits) {
Sam Clegg2ffff5a2017-05-09 23:48:41 +000045 WasmYAML::Limits L;
46 L.Flags = Limits.Flags;
47 L.Initial = Limits.Initial;
48 L.Maximum = Limits.Maximum;
49 return L;
50}
51
Heejin Ahnf208f632018-09-05 01:27:38 +000052std::unique_ptr<WasmYAML::CustomSection>
53WasmDumper::dumpCustomSection(const WasmSection &WasmSec) {
Sam Cleggb7787fd2017-06-20 04:04:59 +000054 std::unique_ptr<WasmYAML::CustomSection> CustomSec;
Sam Clegge4afbc62018-11-14 18:36:24 +000055 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;
Sam Clegg03801252018-12-12 23:40:58 +000063 DylinkSec->Needed = Info.Needed;
Sam Clegge4afbc62018-11-14 18:36:24 +000064 CustomSec = std::move(DylinkSec);
65 } else if (WasmSec.Name == "name") {
Heejin Ahnf208f632018-09-05 01:27:38 +000066 std::unique_ptr<WasmYAML::NameSection> NameSec =
67 make_unique<WasmYAML::NameSection>();
68 for (const llvm::wasm::WasmFunctionName &Func : Obj.debugNames()) {
Sam Cleggb7787fd2017-06-20 04:04:59 +000069 WasmYAML::NameEntry NameEntry;
Sam Clegg9f3fe422018-01-17 19:28:43 +000070 NameEntry.Name = Func.Name;
71 NameEntry.Index = Func.Index;
Sam Cleggb7787fd2017-06-20 04:04:59 +000072 NameSec->FunctionNames.push_back(NameEntry);
73 }
74 CustomSec = std::move(NameSec);
75 } else if (WasmSec.Name == "linking") {
Heejin Ahnf208f632018-09-05 01:27:38 +000076 std::unique_ptr<WasmYAML::LinkingSection> LinkingSec =
77 make_unique<WasmYAML::LinkingSection>();
Sam Clegg6bb5a412018-04-26 18:15:32 +000078 LinkingSec->Version = Obj.linkingData().Version;
79
Nicholas Wilson027b9352018-03-14 15:44:45 +000080 ArrayRef<StringRef> Comdats = Obj.linkingData().Comdats;
81 for (StringRef ComdatName : Comdats)
Sam Cleggea7cace2018-01-09 23:43:14 +000082 LinkingSec->Comdats.emplace_back(WasmYAML::Comdat{ComdatName, {}});
Sam Cleggea7cace2018-01-09 23:43:14 +000083 for (auto &Func : Obj.functions()) {
Nicholas Wilson027b9352018-03-14 15:44:45 +000084 if (Func.Comdat != UINT32_MAX) {
85 LinkingSec->Comdats[Func.Comdat].Entries.emplace_back(
Heejin Ahnf208f632018-09-05 01:27:38 +000086 WasmYAML::ComdatEntry{wasm::WASM_COMDAT_FUNCTION, Func.Index});
Sam Cleggea7cace2018-01-09 23:43:14 +000087 }
88 }
Sam Clegg6bb5a412018-04-26 18:15:32 +000089
Sam Cleggea7cace2018-01-09 23:43:14 +000090 uint32_t SegmentIndex = 0;
Sam Cleggd95ed952017-09-20 19:03:35 +000091 for (const object::WasmSegment &Segment : Obj.dataSegments()) {
92 if (!Segment.Data.Name.empty()) {
Sam Clegg63ebb812017-09-29 16:50:08 +000093 WasmYAML::SegmentInfo SegmentInfo;
94 SegmentInfo.Name = Segment.Data.Name;
Sam Cleggea7cace2018-01-09 23:43:14 +000095 SegmentInfo.Index = SegmentIndex;
Sam Clegg63ebb812017-09-29 16:50:08 +000096 SegmentInfo.Alignment = Segment.Data.Alignment;
97 SegmentInfo.Flags = Segment.Data.Flags;
98 LinkingSec->SegmentInfos.push_back(SegmentInfo);
Sam Cleggd95ed952017-09-20 19:03:35 +000099 }
Nicholas Wilson027b9352018-03-14 15:44:45 +0000100 if (Segment.Data.Comdat != UINT32_MAX) {
101 LinkingSec->Comdats[Segment.Data.Comdat].Entries.emplace_back(
Sam Cleggea7cace2018-01-09 23:43:14 +0000102 WasmYAML::ComdatEntry{wasm::WASM_COMDAT_DATA, SegmentIndex});
103 }
104 SegmentIndex++;
Sam Cleggd95ed952017-09-20 19:03:35 +0000105 }
Sam Clegg6bb5a412018-04-26 18:15:32 +0000106
Sam Clegg6c899ba2018-02-23 05:08:34 +0000107 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:
Heejin Ahnda419bd2018-11-14 02:46:21 +0000120 case wasm::WASM_SYMBOL_TYPE_EVENT:
Sam Clegg6c899ba2018-02-23 05:08:34 +0000121 Info.ElementIndex = Symbol.ElementIndex;
122 break;
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000123 case wasm::WASM_SYMBOL_TYPE_SECTION:
124 Info.ElementIndex = Symbol.ElementIndex;
125 break;
Sam Cleggb7787fd2017-06-20 04:04:59 +0000126 }
Sam Clegg6c899ba2018-02-23 05:08:34 +0000127 LinkingSec->SymbolTable.emplace_back(Info);
Sam Cleggb7787fd2017-06-20 04:04:59 +0000128 }
Sam Clegg6bb5a412018-04-26 18:15:32 +0000129
Sam Clegg42739982017-12-14 21:10:03 +0000130 for (const wasm::WasmInitFunc &Func : Obj.linkingData().InitFunctions) {
Sam Clegg6c899ba2018-02-23 05:08:34 +0000131 WasmYAML::InitFunction F{Func.Priority, Func.Symbol};
Sam Clegg42739982017-12-14 21:10:03 +0000132 LinkingSec->InitFunctions.emplace_back(F);
133 }
Sam Clegg6bb5a412018-04-26 18:15:32 +0000134
Sam Cleggb7787fd2017-06-20 04:04:59 +0000135 CustomSec = std::move(LinkingSec);
Thomas Livelycbda16e2019-01-17 02:29:55 +0000136 } else if (WasmSec.Name == "producers") {
137 std::unique_ptr<WasmYAML::ProducersSection> ProducersSec =
138 make_unique<WasmYAML::ProducersSection>();
139 const llvm::wasm::WasmProducerInfo &Info = Obj.getProducerInfo();
140 for (auto &E : Info.Languages) {
141 WasmYAML::ProducerEntry Producer;
142 Producer.Name = E.first;
143 Producer.Version = E.second;
144 ProducersSec->Languages.push_back(Producer);
145 }
146 for (auto &E : Info.Tools) {
147 WasmYAML::ProducerEntry Producer;
148 Producer.Name = E.first;
149 Producer.Version = E.second;
150 ProducersSec->Tools.push_back(Producer);
151 }
152 for (auto &E : Info.SDKs) {
153 WasmYAML::ProducerEntry Producer;
154 Producer.Name = E.first;
155 Producer.Version = E.second;
156 ProducersSec->SDKs.push_back(Producer);
157 }
158 CustomSec = std::move(ProducersSec);
Sam Cleggb7787fd2017-06-20 04:04:59 +0000159 } else {
160 CustomSec = make_unique<WasmYAML::CustomSection>(WasmSec.Name);
161 }
162 CustomSec->Payload = yaml::BinaryRef(WasmSec.Content);
163 return CustomSec;
164}
165
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000166ErrorOr<WasmYAML::Object *> WasmDumper::dump() {
167 auto Y = make_unique<WasmYAML::Object>();
168
169 // Dump header
170 Y->Header.Version = Obj.getHeader().Version;
171
172 // Dump sections
173 for (const auto &Sec : Obj.sections()) {
Sam Cleggb7787fd2017-06-20 04:04:59 +0000174 const WasmSection &WasmSec = Obj.getWasmSection(Sec);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000175 std::unique_ptr<WasmYAML::Section> S;
176 switch (WasmSec.Type) {
177 case wasm::WASM_SEC_CUSTOM: {
178 if (WasmSec.Name.startswith("reloc.")) {
179 // Relocations are attached the sections they apply to rather than
180 // being represented as a custom section in the YAML output.
181 continue;
182 }
Sam Cleggb7787fd2017-06-20 04:04:59 +0000183 S = dumpCustomSection(WasmSec);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000184 break;
185 }
186 case wasm::WASM_SEC_TYPE: {
187 auto TypeSec = make_unique<WasmYAML::TypeSection>();
188 uint32_t Index = 0;
189 for (const auto &FunctionSig : Obj.types()) {
190 WasmYAML::Signature Sig;
191 Sig.Index = Index++;
Derek Schuff77a7a382018-10-03 22:22:48 +0000192 Sig.ReturnType = wasm::WASM_TYPE_NORESULT;
193 assert(FunctionSig.Returns.size() <= 1 &&
194 "Functions with multiple returns are not supported");
195 if (FunctionSig.Returns.size())
196 Sig.ReturnType = static_cast<uint32_t>(FunctionSig.Returns[0]);
197 for (const auto &ParamType : FunctionSig.Params)
198 Sig.ParamTypes.push_back(static_cast<uint32_t>(ParamType));
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000199 TypeSec->Signatures.push_back(Sig);
200 }
201 S = std::move(TypeSec);
202 break;
203 }
204 case wasm::WASM_SEC_IMPORT: {
205 auto ImportSec = make_unique<WasmYAML::ImportSection>();
206 for (auto &Import : Obj.imports()) {
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000207 WasmYAML::Import Im;
208 Im.Module = Import.Module;
209 Im.Field = Import.Field;
210 Im.Kind = Import.Kind;
211 switch (Im.Kind) {
212 case wasm::WASM_EXTERNAL_FUNCTION:
213 Im.SigIndex = Import.SigIndex;
214 break;
215 case wasm::WASM_EXTERNAL_GLOBAL:
Sam Clegg41db5192017-05-10 00:14:04 +0000216 Im.GlobalImport.Type = Import.Global.Type;
217 Im.GlobalImport.Mutable = Import.Global.Mutable;
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000218 break;
Heejin Ahnda419bd2018-11-14 02:46:21 +0000219 case wasm::WASM_EXTERNAL_EVENT:
220 Im.EventImport.Attribute = Import.Event.Attribute;
221 Im.EventImport.SigIndex = Import.Event.SigIndex;
222 break;
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000223 case wasm::WASM_EXTERNAL_TABLE:
Sam Clegg41db5192017-05-10 00:14:04 +0000224 Im.TableImport = make_table(Import.Table);
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000225 break;
226 case wasm::WASM_EXTERNAL_MEMORY:
227 Im.Memory = make_limits(Import.Memory);
228 break;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000229 }
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000230 ImportSec->Imports.push_back(Im);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000231 }
232 S = std::move(ImportSec);
233 break;
234 }
235 case wasm::WASM_SEC_FUNCTION: {
236 auto FuncSec = make_unique<WasmYAML::FunctionSection>();
237 for (const auto &Func : Obj.functionTypes()) {
238 FuncSec->FunctionTypes.push_back(Func);
239 }
240 S = std::move(FuncSec);
241 break;
242 }
243 case wasm::WASM_SEC_TABLE: {
244 auto TableSec = make_unique<WasmYAML::TableSection>();
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000245 for (const wasm::WasmTable &Table : Obj.tables()) {
246 TableSec->Tables.push_back(make_table(Table));
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000247 }
248 S = std::move(TableSec);
249 break;
250 }
251 case wasm::WASM_SEC_MEMORY: {
252 auto MemorySec = make_unique<WasmYAML::MemorySection>();
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000253 for (const wasm::WasmLimits &Memory : Obj.memories()) {
254 MemorySec->Memories.push_back(make_limits(Memory));
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000255 }
256 S = std::move(MemorySec);
257 break;
258 }
259 case wasm::WASM_SEC_GLOBAL: {
260 auto GlobalSec = make_unique<WasmYAML::GlobalSection>();
261 for (auto &Global : Obj.globals()) {
262 WasmYAML::Global G;
Sam Clegge53af7f2018-01-09 21:38:53 +0000263 G.Index = Global.Index;
Sam Clegg6e7f1822018-01-31 19:50:14 +0000264 G.Type = Global.Type.Type;
265 G.Mutable = Global.Type.Mutable;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000266 G.InitExpr = Global.InitExpr;
267 GlobalSec->Globals.push_back(G);
268 }
269 S = std::move(GlobalSec);
270 break;
271 }
Heejin Ahnda419bd2018-11-14 02:46:21 +0000272 case wasm::WASM_SEC_EVENT: {
273 auto EventSec = 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 }
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000284 case wasm::WASM_SEC_START: {
285 auto StartSec = make_unique<WasmYAML::StartSection>();
286 StartSec->StartFunction = Obj.startFunction();
287 S = std::move(StartSec);
288 break;
289 }
290 case wasm::WASM_SEC_EXPORT: {
291 auto ExportSec = make_unique<WasmYAML::ExportSection>();
292 for (auto &Export : Obj.exports()) {
293 WasmYAML::Export Ex;
294 Ex.Name = Export.Name;
295 Ex.Kind = Export.Kind;
296 Ex.Index = Export.Index;
297 ExportSec->Exports.push_back(Ex);
298 }
299 S = std::move(ExportSec);
300 break;
301 }
302 case wasm::WASM_SEC_ELEM: {
303 auto ElemSec = make_unique<WasmYAML::ElemSection>();
304 for (auto &Segment : Obj.elements()) {
305 WasmYAML::ElemSegment Seg;
306 Seg.TableIndex = Segment.TableIndex;
307 Seg.Offset = Segment.Offset;
308 for (auto &Func : Segment.Functions) {
309 Seg.Functions.push_back(Func);
310 }
311 ElemSec->Segments.push_back(Seg);
312 }
313 S = std::move(ElemSec);
314 break;
315 }
316 case wasm::WASM_SEC_CODE: {
317 auto CodeSec = make_unique<WasmYAML::CodeSection>();
318 for (auto &Func : Obj.functions()) {
319 WasmYAML::Function Function;
Sam Clegge53af7f2018-01-09 21:38:53 +0000320 Function.Index = Func.Index;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000321 for (auto &Local : Func.Locals) {
322 WasmYAML::LocalDecl LocalDecl;
323 LocalDecl.Type = Local.Type;
324 LocalDecl.Count = Local.Count;
325 Function.Locals.push_back(LocalDecl);
326 }
327 Function.Body = yaml::BinaryRef(Func.Body);
328 CodeSec->Functions.push_back(Function);
329 }
330 S = std::move(CodeSec);
331 break;
332 }
333 case wasm::WASM_SEC_DATA: {
334 auto DataSec = make_unique<WasmYAML::DataSection>();
Sam Cleggd95ed952017-09-20 19:03:35 +0000335 for (const object::WasmSegment &Segment : Obj.dataSegments()) {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000336 WasmYAML::DataSegment Seg;
Sam Clegg9c07f942017-07-12 00:24:54 +0000337 Seg.SectionOffset = Segment.SectionOffset;
338 Seg.MemoryIndex = Segment.Data.MemoryIndex;
339 Seg.Offset = Segment.Data.Offset;
340 Seg.Content = yaml::BinaryRef(Segment.Data.Content);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000341 DataSec->Segments.push_back(Seg);
342 }
343 S = std::move(DataSec);
344 break;
345 }
346 default:
347 llvm_unreachable("Unknown section type");
348 break;
349 }
Heejin Ahnf208f632018-09-05 01:27:38 +0000350 for (const wasm::WasmRelocation &Reloc : WasmSec.Relocations) {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000351 WasmYAML::Relocation R;
352 R.Type = Reloc.Type;
353 R.Index = Reloc.Index;
354 R.Offset = Reloc.Offset;
355 R.Addend = Reloc.Addend;
356 S->Relocations.push_back(R);
357 }
358 Y->Sections.push_back(std::move(S));
359 }
360
361 return Y.release();
362}
363
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000364std::error_code wasm2yaml(raw_ostream &Out, const object::WasmObjectFile &Obj) {
365 WasmDumper Dumper(Obj);
366 ErrorOr<WasmYAML::Object *> YAMLOrErr = Dumper.dump();
367 if (std::error_code EC = YAMLOrErr.getError())
368 return EC;
369
370 std::unique_ptr<WasmYAML::Object> YAML(YAMLOrErr.get());
371 yaml::Output Yout(Out);
372 Yout << *YAML;
373
374 return std::error_code();
375}