blob: 7581bbef50ad602b31a5c60d1eadc38dac1b541f [file] [log] [blame]
Derek Schuff349a48f2017-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 Clegg1e975c12017-06-20 04:04:59 +000017using object::WasmSection;
Derek Schuff349a48f2017-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 Clegg1e975c12017-06-20 04:04:59 +000026
Derek Schuff349a48f2017-03-30 19:44:09 +000027 ErrorOr<WasmYAML::Object *> dump();
Sam Clegg1e975c12017-06-20 04:04:59 +000028
29 std::unique_ptr<WasmYAML::CustomSection>
30 dumpCustomSection(const WasmSection &WasmSec);
Derek Schuff349a48f2017-03-30 19:44:09 +000031};
32
Sam Clegg1e975c12017-06-20 04:04:59 +000033} // namespace
34
35static WasmYAML::Table make_table(const wasm::WasmTable &Table) {
Sam Clegg65e284e2017-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 Clegg1e975c12017-06-20 04:04:59 +000044static WasmYAML::Limits make_limits(const wasm::WasmLimits &Limits) {
Sam Clegg65e284e2017-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 Ahn581d2312018-09-05 01:27:38 +000052std::unique_ptr<WasmYAML::CustomSection>
53WasmDumper::dumpCustomSection(const WasmSection &WasmSec) {
Sam Clegg1e975c12017-06-20 04:04:59 +000054 std::unique_ptr<WasmYAML::CustomSection> CustomSec;
Sam Clegg45c215a2018-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 Clegg559201d2018-12-12 23:40:58 +000063 DylinkSec->Needed = Info.Needed;
Sam Clegg45c215a2018-11-14 18:36:24 +000064 CustomSec = std::move(DylinkSec);
65 } else if (WasmSec.Name == "name") {
Heejin Ahn581d2312018-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 Clegg1e975c12017-06-20 04:04:59 +000069 WasmYAML::NameEntry NameEntry;
Sam Cleggeb7fd732018-01-17 19:28:43 +000070 NameEntry.Name = Func.Name;
71 NameEntry.Index = Func.Index;
Sam Clegg1e975c12017-06-20 04:04:59 +000072 NameSec->FunctionNames.push_back(NameEntry);
73 }
74 CustomSec = std::move(NameSec);
75 } else if (WasmSec.Name == "linking") {
Heejin Ahn581d2312018-09-05 01:27:38 +000076 std::unique_ptr<WasmYAML::LinkingSection> LinkingSec =
77 make_unique<WasmYAML::LinkingSection>();
Sam Clegg14598cb2018-04-26 18:15:32 +000078 LinkingSec->Version = Obj.linkingData().Version;
79
Nicholas Wilsondc84b5c2018-03-14 15:44:45 +000080 ArrayRef<StringRef> Comdats = Obj.linkingData().Comdats;
81 for (StringRef ComdatName : Comdats)
Sam Clegge92250a2018-01-09 23:43:14 +000082 LinkingSec->Comdats.emplace_back(WasmYAML::Comdat{ComdatName, {}});
Sam Clegge92250a2018-01-09 23:43:14 +000083 for (auto &Func : Obj.functions()) {
Nicholas Wilsondc84b5c2018-03-14 15:44:45 +000084 if (Func.Comdat != UINT32_MAX) {
85 LinkingSec->Comdats[Func.Comdat].Entries.emplace_back(
Heejin Ahn581d2312018-09-05 01:27:38 +000086 WasmYAML::ComdatEntry{wasm::WASM_COMDAT_FUNCTION, Func.Index});
Sam Clegge92250a2018-01-09 23:43:14 +000087 }
88 }
Sam Clegg14598cb2018-04-26 18:15:32 +000089
Sam Clegge92250a2018-01-09 23:43:14 +000090 uint32_t SegmentIndex = 0;
Sam Clegg81e38242017-09-20 19:03:35 +000091 for (const object::WasmSegment &Segment : Obj.dataSegments()) {
92 if (!Segment.Data.Name.empty()) {
Sam Clegge2864172017-09-29 16:50:08 +000093 WasmYAML::SegmentInfo SegmentInfo;
94 SegmentInfo.Name = Segment.Data.Name;
Sam Clegge92250a2018-01-09 23:43:14 +000095 SegmentInfo.Index = SegmentIndex;
Sam Clegge2864172017-09-29 16:50:08 +000096 SegmentInfo.Alignment = Segment.Data.Alignment;
97 SegmentInfo.Flags = Segment.Data.Flags;
98 LinkingSec->SegmentInfos.push_back(SegmentInfo);
Sam Clegg81e38242017-09-20 19:03:35 +000099 }
Nicholas Wilsondc84b5c2018-03-14 15:44:45 +0000100 if (Segment.Data.Comdat != UINT32_MAX) {
101 LinkingSec->Comdats[Segment.Data.Comdat].Entries.emplace_back(
Sam Clegge92250a2018-01-09 23:43:14 +0000102 WasmYAML::ComdatEntry{wasm::WASM_COMDAT_DATA, SegmentIndex});
103 }
104 SegmentIndex++;
Sam Clegg81e38242017-09-20 19:03:35 +0000105 }
Sam Clegg14598cb2018-04-26 18:15:32 +0000106
Sam Cleggd5784792018-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 Ahn8bcdd042018-11-14 02:46:21 +0000120 case wasm::WASM_SYMBOL_TYPE_EVENT:
Sam Cleggd5784792018-02-23 05:08:34 +0000121 Info.ElementIndex = Symbol.ElementIndex;
122 break;
Sam Cleggdb159752018-04-26 19:27:28 +0000123 case wasm::WASM_SYMBOL_TYPE_SECTION:
124 Info.ElementIndex = Symbol.ElementIndex;
125 break;
Sam Clegg1e975c12017-06-20 04:04:59 +0000126 }
Sam Cleggd5784792018-02-23 05:08:34 +0000127 LinkingSec->SymbolTable.emplace_back(Info);
Sam Clegg1e975c12017-06-20 04:04:59 +0000128 }
Sam Clegg14598cb2018-04-26 18:15:32 +0000129
Sam Clegge1acb562017-12-14 21:10:03 +0000130 for (const wasm::WasmInitFunc &Func : Obj.linkingData().InitFunctions) {
Sam Cleggd5784792018-02-23 05:08:34 +0000131 WasmYAML::InitFunction F{Func.Priority, Func.Symbol};
Sam Clegge1acb562017-12-14 21:10:03 +0000132 LinkingSec->InitFunctions.emplace_back(F);
133 }
Sam Clegg14598cb2018-04-26 18:15:32 +0000134
Sam Clegg1e975c12017-06-20 04:04:59 +0000135 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
Derek Schuff349a48f2017-03-30 19:44:09 +0000143ErrorOr<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()) {
Sam Clegg1e975c12017-06-20 04:04:59 +0000151 const WasmSection &WasmSec = Obj.getWasmSection(Sec);
Derek Schuff349a48f2017-03-30 19:44:09 +0000152 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 }
Sam Clegg1e975c12017-06-20 04:04:59 +0000160 S = dumpCustomSection(WasmSec);
Derek Schuff349a48f2017-03-30 19:44:09 +0000161 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++;
Derek Schuffab9755b2018-10-03 22:22:48 +0000169 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));
Derek Schuff349a48f2017-03-30 19:44:09 +0000176 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()) {
Sam Clegg65e284e2017-05-09 23:48:41 +0000184 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:
Sam Cleggbde17ff2017-05-10 00:14:04 +0000193 Im.GlobalImport.Type = Import.Global.Type;
194 Im.GlobalImport.Mutable = Import.Global.Mutable;
Sam Clegg65e284e2017-05-09 23:48:41 +0000195 break;
Heejin Ahn8bcdd042018-11-14 02:46:21 +0000196 case wasm::WASM_EXTERNAL_EVENT:
197 Im.EventImport.Attribute = Import.Event.Attribute;
198 Im.EventImport.SigIndex = Import.Event.SigIndex;
199 break;
Sam Clegg65e284e2017-05-09 23:48:41 +0000200 case wasm::WASM_EXTERNAL_TABLE:
Sam Cleggbde17ff2017-05-10 00:14:04 +0000201 Im.TableImport = make_table(Import.Table);
Sam Clegg65e284e2017-05-09 23:48:41 +0000202 break;
203 case wasm::WASM_EXTERNAL_MEMORY:
204 Im.Memory = make_limits(Import.Memory);
205 break;
Derek Schuff349a48f2017-03-30 19:44:09 +0000206 }
Sam Clegg65e284e2017-05-09 23:48:41 +0000207 ImportSec->Imports.push_back(Im);
Derek Schuff349a48f2017-03-30 19:44:09 +0000208 }
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>();
Sam Clegg65e284e2017-05-09 23:48:41 +0000222 for (const wasm::WasmTable &Table : Obj.tables()) {
223 TableSec->Tables.push_back(make_table(Table));
Derek Schuff349a48f2017-03-30 19:44:09 +0000224 }
225 S = std::move(TableSec);
226 break;
227 }
228 case wasm::WASM_SEC_MEMORY: {
229 auto MemorySec = make_unique<WasmYAML::MemorySection>();
Sam Clegg65e284e2017-05-09 23:48:41 +0000230 for (const wasm::WasmLimits &Memory : Obj.memories()) {
231 MemorySec->Memories.push_back(make_limits(Memory));
Derek Schuff349a48f2017-03-30 19:44:09 +0000232 }
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;
Sam Clegg6b54fc32018-01-09 21:38:53 +0000240 G.Index = Global.Index;
Sam Cleggbfa1dc62018-01-31 19:50:14 +0000241 G.Type = Global.Type.Type;
242 G.Mutable = Global.Type.Mutable;
Derek Schuff349a48f2017-03-30 19:44:09 +0000243 G.InitExpr = Global.InitExpr;
244 GlobalSec->Globals.push_back(G);
245 }
246 S = std::move(GlobalSec);
247 break;
248 }
Heejin Ahn8bcdd042018-11-14 02:46:21 +0000249 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 }
Derek Schuff349a48f2017-03-30 19:44:09 +0000261 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;
Sam Clegg6b54fc32018-01-09 21:38:53 +0000297 Function.Index = Func.Index;
Derek Schuff349a48f2017-03-30 19:44:09 +0000298 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>();
Sam Clegg81e38242017-09-20 19:03:35 +0000312 for (const object::WasmSegment &Segment : Obj.dataSegments()) {
Derek Schuff349a48f2017-03-30 19:44:09 +0000313 WasmYAML::DataSegment Seg;
Sam Cleggc9c28d92017-07-12 00:24:54 +0000314 Seg.SectionOffset = Segment.SectionOffset;
315 Seg.MemoryIndex = Segment.Data.MemoryIndex;
316 Seg.Offset = Segment.Data.Offset;
317 Seg.Content = yaml::BinaryRef(Segment.Data.Content);
Derek Schuff349a48f2017-03-30 19:44:09 +0000318 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 }
Heejin Ahn581d2312018-09-05 01:27:38 +0000327 for (const wasm::WasmRelocation &Reloc : WasmSec.Relocations) {
Derek Schuff349a48f2017-03-30 19:44:09 +0000328 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
Derek Schuff349a48f2017-03-30 19:44:09 +0000341std::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}