blob: 8bbc9fb468c0098588e2f735f3873f53dfcb2ee4 [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;
55 if (WasmSec.Name == "name") {
Heejin Ahnf208f632018-09-05 01:27:38 +000056 std::unique_ptr<WasmYAML::NameSection> NameSec =
57 make_unique<WasmYAML::NameSection>();
58 for (const llvm::wasm::WasmFunctionName &Func : Obj.debugNames()) {
Sam Cleggb7787fd2017-06-20 04:04:59 +000059 WasmYAML::NameEntry NameEntry;
Sam Clegg9f3fe422018-01-17 19:28:43 +000060 NameEntry.Name = Func.Name;
61 NameEntry.Index = Func.Index;
Sam Cleggb7787fd2017-06-20 04:04:59 +000062 NameSec->FunctionNames.push_back(NameEntry);
63 }
64 CustomSec = std::move(NameSec);
65 } else if (WasmSec.Name == "linking") {
Heejin Ahnf208f632018-09-05 01:27:38 +000066 std::unique_ptr<WasmYAML::LinkingSection> LinkingSec =
67 make_unique<WasmYAML::LinkingSection>();
Sam Clegg6bb5a412018-04-26 18:15:32 +000068 LinkingSec->Version = Obj.linkingData().Version;
69
Nicholas Wilson027b9352018-03-14 15:44:45 +000070 ArrayRef<StringRef> Comdats = Obj.linkingData().Comdats;
71 for (StringRef ComdatName : Comdats)
Sam Cleggea7cace2018-01-09 23:43:14 +000072 LinkingSec->Comdats.emplace_back(WasmYAML::Comdat{ComdatName, {}});
Sam Cleggea7cace2018-01-09 23:43:14 +000073 for (auto &Func : Obj.functions()) {
Nicholas Wilson027b9352018-03-14 15:44:45 +000074 if (Func.Comdat != UINT32_MAX) {
75 LinkingSec->Comdats[Func.Comdat].Entries.emplace_back(
Heejin Ahnf208f632018-09-05 01:27:38 +000076 WasmYAML::ComdatEntry{wasm::WASM_COMDAT_FUNCTION, Func.Index});
Sam Cleggea7cace2018-01-09 23:43:14 +000077 }
78 }
Sam Clegg6bb5a412018-04-26 18:15:32 +000079
Sam Cleggea7cace2018-01-09 23:43:14 +000080 uint32_t SegmentIndex = 0;
Sam Cleggd95ed952017-09-20 19:03:35 +000081 for (const object::WasmSegment &Segment : Obj.dataSegments()) {
82 if (!Segment.Data.Name.empty()) {
Sam Clegg63ebb812017-09-29 16:50:08 +000083 WasmYAML::SegmentInfo SegmentInfo;
84 SegmentInfo.Name = Segment.Data.Name;
Sam Cleggea7cace2018-01-09 23:43:14 +000085 SegmentInfo.Index = SegmentIndex;
Sam Clegg63ebb812017-09-29 16:50:08 +000086 SegmentInfo.Alignment = Segment.Data.Alignment;
87 SegmentInfo.Flags = Segment.Data.Flags;
88 LinkingSec->SegmentInfos.push_back(SegmentInfo);
Sam Cleggd95ed952017-09-20 19:03:35 +000089 }
Nicholas Wilson027b9352018-03-14 15:44:45 +000090 if (Segment.Data.Comdat != UINT32_MAX) {
91 LinkingSec->Comdats[Segment.Data.Comdat].Entries.emplace_back(
Sam Cleggea7cace2018-01-09 23:43:14 +000092 WasmYAML::ComdatEntry{wasm::WASM_COMDAT_DATA, SegmentIndex});
93 }
94 SegmentIndex++;
Sam Cleggd95ed952017-09-20 19:03:35 +000095 }
Sam Clegg6bb5a412018-04-26 18:15:32 +000096
Sam Clegg6c899ba2018-02-23 05:08:34 +000097 uint32_t SymbolIndex = 0;
98 for (const wasm::WasmSymbolInfo &Symbol : Obj.linkingData().SymbolTable) {
99 WasmYAML::SymbolInfo Info;
100 Info.Index = SymbolIndex++;
101 Info.Kind = static_cast<uint32_t>(Symbol.Kind);
102 Info.Name = Symbol.Name;
103 Info.Flags = Symbol.Flags;
104 switch (Symbol.Kind) {
105 case wasm::WASM_SYMBOL_TYPE_DATA:
106 Info.DataRef = Symbol.DataRef;
107 break;
108 case wasm::WASM_SYMBOL_TYPE_FUNCTION:
109 case wasm::WASM_SYMBOL_TYPE_GLOBAL:
Heejin Ahnda419bd2018-11-14 02:46:21 +0000110 case wasm::WASM_SYMBOL_TYPE_EVENT:
Sam Clegg6c899ba2018-02-23 05:08:34 +0000111 Info.ElementIndex = Symbol.ElementIndex;
112 break;
Sam Clegg6a31a0d2018-04-26 19:27:28 +0000113 case wasm::WASM_SYMBOL_TYPE_SECTION:
114 Info.ElementIndex = Symbol.ElementIndex;
115 break;
Sam Cleggb7787fd2017-06-20 04:04:59 +0000116 }
Sam Clegg6c899ba2018-02-23 05:08:34 +0000117 LinkingSec->SymbolTable.emplace_back(Info);
Sam Cleggb7787fd2017-06-20 04:04:59 +0000118 }
Sam Clegg6bb5a412018-04-26 18:15:32 +0000119
Sam Clegg42739982017-12-14 21:10:03 +0000120 for (const wasm::WasmInitFunc &Func : Obj.linkingData().InitFunctions) {
Sam Clegg6c899ba2018-02-23 05:08:34 +0000121 WasmYAML::InitFunction F{Func.Priority, Func.Symbol};
Sam Clegg42739982017-12-14 21:10:03 +0000122 LinkingSec->InitFunctions.emplace_back(F);
123 }
Sam Clegg6bb5a412018-04-26 18:15:32 +0000124
Sam Cleggb7787fd2017-06-20 04:04:59 +0000125 CustomSec = std::move(LinkingSec);
126 } else {
127 CustomSec = make_unique<WasmYAML::CustomSection>(WasmSec.Name);
128 }
129 CustomSec->Payload = yaml::BinaryRef(WasmSec.Content);
130 return CustomSec;
131}
132
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000133ErrorOr<WasmYAML::Object *> WasmDumper::dump() {
134 auto Y = make_unique<WasmYAML::Object>();
135
136 // Dump header
137 Y->Header.Version = Obj.getHeader().Version;
138
139 // Dump sections
140 for (const auto &Sec : Obj.sections()) {
Sam Cleggb7787fd2017-06-20 04:04:59 +0000141 const WasmSection &WasmSec = Obj.getWasmSection(Sec);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000142 std::unique_ptr<WasmYAML::Section> S;
143 switch (WasmSec.Type) {
144 case wasm::WASM_SEC_CUSTOM: {
145 if (WasmSec.Name.startswith("reloc.")) {
146 // Relocations are attached the sections they apply to rather than
147 // being represented as a custom section in the YAML output.
148 continue;
149 }
Sam Cleggb7787fd2017-06-20 04:04:59 +0000150 S = dumpCustomSection(WasmSec);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000151 break;
152 }
153 case wasm::WASM_SEC_TYPE: {
154 auto TypeSec = make_unique<WasmYAML::TypeSection>();
155 uint32_t Index = 0;
156 for (const auto &FunctionSig : Obj.types()) {
157 WasmYAML::Signature Sig;
158 Sig.Index = Index++;
Derek Schuff77a7a382018-10-03 22:22:48 +0000159 Sig.ReturnType = wasm::WASM_TYPE_NORESULT;
160 assert(FunctionSig.Returns.size() <= 1 &&
161 "Functions with multiple returns are not supported");
162 if (FunctionSig.Returns.size())
163 Sig.ReturnType = static_cast<uint32_t>(FunctionSig.Returns[0]);
164 for (const auto &ParamType : FunctionSig.Params)
165 Sig.ParamTypes.push_back(static_cast<uint32_t>(ParamType));
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000166 TypeSec->Signatures.push_back(Sig);
167 }
168 S = std::move(TypeSec);
169 break;
170 }
171 case wasm::WASM_SEC_IMPORT: {
172 auto ImportSec = make_unique<WasmYAML::ImportSection>();
173 for (auto &Import : Obj.imports()) {
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000174 WasmYAML::Import Im;
175 Im.Module = Import.Module;
176 Im.Field = Import.Field;
177 Im.Kind = Import.Kind;
178 switch (Im.Kind) {
179 case wasm::WASM_EXTERNAL_FUNCTION:
180 Im.SigIndex = Import.SigIndex;
181 break;
182 case wasm::WASM_EXTERNAL_GLOBAL:
Sam Clegg41db5192017-05-10 00:14:04 +0000183 Im.GlobalImport.Type = Import.Global.Type;
184 Im.GlobalImport.Mutable = Import.Global.Mutable;
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000185 break;
Heejin Ahnda419bd2018-11-14 02:46:21 +0000186 case wasm::WASM_EXTERNAL_EVENT:
187 Im.EventImport.Attribute = Import.Event.Attribute;
188 Im.EventImport.SigIndex = Import.Event.SigIndex;
189 break;
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000190 case wasm::WASM_EXTERNAL_TABLE:
Sam Clegg41db5192017-05-10 00:14:04 +0000191 Im.TableImport = make_table(Import.Table);
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000192 break;
193 case wasm::WASM_EXTERNAL_MEMORY:
194 Im.Memory = make_limits(Import.Memory);
195 break;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000196 }
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000197 ImportSec->Imports.push_back(Im);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000198 }
199 S = std::move(ImportSec);
200 break;
201 }
202 case wasm::WASM_SEC_FUNCTION: {
203 auto FuncSec = make_unique<WasmYAML::FunctionSection>();
204 for (const auto &Func : Obj.functionTypes()) {
205 FuncSec->FunctionTypes.push_back(Func);
206 }
207 S = std::move(FuncSec);
208 break;
209 }
210 case wasm::WASM_SEC_TABLE: {
211 auto TableSec = make_unique<WasmYAML::TableSection>();
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000212 for (const wasm::WasmTable &Table : Obj.tables()) {
213 TableSec->Tables.push_back(make_table(Table));
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000214 }
215 S = std::move(TableSec);
216 break;
217 }
218 case wasm::WASM_SEC_MEMORY: {
219 auto MemorySec = make_unique<WasmYAML::MemorySection>();
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000220 for (const wasm::WasmLimits &Memory : Obj.memories()) {
221 MemorySec->Memories.push_back(make_limits(Memory));
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000222 }
223 S = std::move(MemorySec);
224 break;
225 }
226 case wasm::WASM_SEC_GLOBAL: {
227 auto GlobalSec = make_unique<WasmYAML::GlobalSection>();
228 for (auto &Global : Obj.globals()) {
229 WasmYAML::Global G;
Sam Clegge53af7f2018-01-09 21:38:53 +0000230 G.Index = Global.Index;
Sam Clegg6e7f1822018-01-31 19:50:14 +0000231 G.Type = Global.Type.Type;
232 G.Mutable = Global.Type.Mutable;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000233 G.InitExpr = Global.InitExpr;
234 GlobalSec->Globals.push_back(G);
235 }
236 S = std::move(GlobalSec);
237 break;
238 }
Heejin Ahnda419bd2018-11-14 02:46:21 +0000239 case wasm::WASM_SEC_EVENT: {
240 auto EventSec = make_unique<WasmYAML::EventSection>();
241 for (auto &Event : Obj.events()) {
242 WasmYAML::Event E;
243 E.Index = Event.Index;
244 E.Attribute = Event.Type.Attribute;
245 E.SigIndex = Event.Type.SigIndex;
246 EventSec->Events.push_back(E);
247 }
248 S = std::move(EventSec);
249 break;
250 }
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000251 case wasm::WASM_SEC_START: {
252 auto StartSec = make_unique<WasmYAML::StartSection>();
253 StartSec->StartFunction = Obj.startFunction();
254 S = std::move(StartSec);
255 break;
256 }
257 case wasm::WASM_SEC_EXPORT: {
258 auto ExportSec = make_unique<WasmYAML::ExportSection>();
259 for (auto &Export : Obj.exports()) {
260 WasmYAML::Export Ex;
261 Ex.Name = Export.Name;
262 Ex.Kind = Export.Kind;
263 Ex.Index = Export.Index;
264 ExportSec->Exports.push_back(Ex);
265 }
266 S = std::move(ExportSec);
267 break;
268 }
269 case wasm::WASM_SEC_ELEM: {
270 auto ElemSec = make_unique<WasmYAML::ElemSection>();
271 for (auto &Segment : Obj.elements()) {
272 WasmYAML::ElemSegment Seg;
273 Seg.TableIndex = Segment.TableIndex;
274 Seg.Offset = Segment.Offset;
275 for (auto &Func : Segment.Functions) {
276 Seg.Functions.push_back(Func);
277 }
278 ElemSec->Segments.push_back(Seg);
279 }
280 S = std::move(ElemSec);
281 break;
282 }
283 case wasm::WASM_SEC_CODE: {
284 auto CodeSec = make_unique<WasmYAML::CodeSection>();
285 for (auto &Func : Obj.functions()) {
286 WasmYAML::Function Function;
Sam Clegge53af7f2018-01-09 21:38:53 +0000287 Function.Index = Func.Index;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000288 for (auto &Local : Func.Locals) {
289 WasmYAML::LocalDecl LocalDecl;
290 LocalDecl.Type = Local.Type;
291 LocalDecl.Count = Local.Count;
292 Function.Locals.push_back(LocalDecl);
293 }
294 Function.Body = yaml::BinaryRef(Func.Body);
295 CodeSec->Functions.push_back(Function);
296 }
297 S = std::move(CodeSec);
298 break;
299 }
300 case wasm::WASM_SEC_DATA: {
301 auto DataSec = make_unique<WasmYAML::DataSection>();
Sam Cleggd95ed952017-09-20 19:03:35 +0000302 for (const object::WasmSegment &Segment : Obj.dataSegments()) {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000303 WasmYAML::DataSegment Seg;
Sam Clegg9c07f942017-07-12 00:24:54 +0000304 Seg.SectionOffset = Segment.SectionOffset;
305 Seg.MemoryIndex = Segment.Data.MemoryIndex;
306 Seg.Offset = Segment.Data.Offset;
307 Seg.Content = yaml::BinaryRef(Segment.Data.Content);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000308 DataSec->Segments.push_back(Seg);
309 }
310 S = std::move(DataSec);
311 break;
312 }
313 default:
314 llvm_unreachable("Unknown section type");
315 break;
316 }
Heejin Ahnf208f632018-09-05 01:27:38 +0000317 for (const wasm::WasmRelocation &Reloc : WasmSec.Relocations) {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000318 WasmYAML::Relocation R;
319 R.Type = Reloc.Type;
320 R.Index = Reloc.Index;
321 R.Offset = Reloc.Offset;
322 R.Addend = Reloc.Addend;
323 S->Relocations.push_back(R);
324 }
325 Y->Sections.push_back(std::move(S));
326 }
327
328 return Y.release();
329}
330
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000331std::error_code wasm2yaml(raw_ostream &Out, const object::WasmObjectFile &Obj) {
332 WasmDumper Dumper(Obj);
333 ErrorOr<WasmYAML::Object *> YAMLOrErr = Dumper.dump();
334 if (std::error_code EC = YAMLOrErr.getError())
335 return EC;
336
337 std::unique_ptr<WasmYAML::Object> YAML(YAMLOrErr.get());
338 yaml::Output Yout(Out);
339 Yout << *YAML;
340
341 return std::error_code();
342}