blob: 476fb6c1de246ee96ed0161d5b80f476bba4737a [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
Sam Cleggb7787fd2017-06-20 04:04:59 +000052std::unique_ptr<WasmYAML::CustomSection> WasmDumper::dumpCustomSection(const WasmSection &WasmSec) {
53 std::unique_ptr<WasmYAML::CustomSection> CustomSec;
54 if (WasmSec.Name == "name") {
55 std::unique_ptr<WasmYAML::NameSection> NameSec = make_unique<WasmYAML::NameSection>();
56 for (const object::SymbolRef& Sym: Obj.symbols()) {
Sam Clegg31a2c802017-09-20 21:17:04 +000057 const object::WasmSymbol Symbol = Obj.getWasmSymbol(Sym);
58 if (Symbol.Type != object::WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME)
Sam Cleggb7787fd2017-06-20 04:04:59 +000059 continue;
60 WasmYAML::NameEntry NameEntry;
Sam Clegg31a2c802017-09-20 21:17:04 +000061 NameEntry.Name = Symbol.Name;
Sam Cleggb7787fd2017-06-20 04:04:59 +000062 NameEntry.Index = Sym.getValue();
63 NameSec->FunctionNames.push_back(NameEntry);
64 }
65 CustomSec = std::move(NameSec);
66 } else if (WasmSec.Name == "linking") {
67 std::unique_ptr<WasmYAML::LinkingSection> LinkingSec = make_unique<WasmYAML::LinkingSection>();
Sam Cleggd95ed952017-09-20 19:03:35 +000068 size_t Index = 0;
69 for (const object::WasmSegment &Segment : Obj.dataSegments()) {
70 if (!Segment.Data.Name.empty()) {
Sam Clegg63ebb812017-09-29 16:50:08 +000071 WasmYAML::SegmentInfo SegmentInfo;
72 SegmentInfo.Name = Segment.Data.Name;
73 SegmentInfo.Index = Index;
74 SegmentInfo.Alignment = Segment.Data.Alignment;
75 SegmentInfo.Flags = Segment.Data.Flags;
76 LinkingSec->SegmentInfos.push_back(SegmentInfo);
Sam Cleggd95ed952017-09-20 19:03:35 +000077 }
78 Index++;
79 }
Sam Cleggb7787fd2017-06-20 04:04:59 +000080 for (const object::SymbolRef& Sym: Obj.symbols()) {
81 const object::WasmSymbol Symbol = Obj.getWasmSymbol(Sym);
82 if (Symbol.Flags != 0) {
Sam Clegg42739982017-12-14 21:10:03 +000083 WasmYAML::SymbolInfo Info{Symbol.Name, Symbol.Flags};
84 LinkingSec->SymbolInfos.emplace_back(Info);
Sam Cleggb7787fd2017-06-20 04:04:59 +000085 }
86 }
Sam Clegg9e1ade92017-06-27 20:27:59 +000087 LinkingSec->DataSize = Obj.linkingData().DataSize;
Sam Clegg42739982017-12-14 21:10:03 +000088 for (const wasm::WasmInitFunc &Func : Obj.linkingData().InitFunctions) {
89 WasmYAML::InitFunction F{Func.Priority, Func.FunctionIndex};
90 LinkingSec->InitFunctions.emplace_back(F);
91 }
Sam Cleggb7787fd2017-06-20 04:04:59 +000092 CustomSec = std::move(LinkingSec);
93 } else {
94 CustomSec = make_unique<WasmYAML::CustomSection>(WasmSec.Name);
95 }
96 CustomSec->Payload = yaml::BinaryRef(WasmSec.Content);
97 return CustomSec;
98}
99
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000100ErrorOr<WasmYAML::Object *> WasmDumper::dump() {
101 auto Y = make_unique<WasmYAML::Object>();
102
103 // Dump header
104 Y->Header.Version = Obj.getHeader().Version;
105
106 // Dump sections
107 for (const auto &Sec : Obj.sections()) {
Sam Cleggb7787fd2017-06-20 04:04:59 +0000108 const WasmSection &WasmSec = Obj.getWasmSection(Sec);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000109 std::unique_ptr<WasmYAML::Section> S;
110 switch (WasmSec.Type) {
111 case wasm::WASM_SEC_CUSTOM: {
112 if (WasmSec.Name.startswith("reloc.")) {
113 // Relocations are attached the sections they apply to rather than
114 // being represented as a custom section in the YAML output.
115 continue;
116 }
Sam Cleggb7787fd2017-06-20 04:04:59 +0000117 S = dumpCustomSection(WasmSec);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000118 break;
119 }
120 case wasm::WASM_SEC_TYPE: {
121 auto TypeSec = make_unique<WasmYAML::TypeSection>();
122 uint32_t Index = 0;
123 for (const auto &FunctionSig : Obj.types()) {
124 WasmYAML::Signature Sig;
125 Sig.Index = Index++;
126 Sig.ReturnType = FunctionSig.ReturnType;
127 for (const auto &ParamType : FunctionSig.ParamTypes)
128 Sig.ParamTypes.push_back(ParamType);
129 TypeSec->Signatures.push_back(Sig);
130 }
131 S = std::move(TypeSec);
132 break;
133 }
134 case wasm::WASM_SEC_IMPORT: {
135 auto ImportSec = make_unique<WasmYAML::ImportSection>();
136 for (auto &Import : Obj.imports()) {
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000137 WasmYAML::Import Im;
138 Im.Module = Import.Module;
139 Im.Field = Import.Field;
140 Im.Kind = Import.Kind;
141 switch (Im.Kind) {
142 case wasm::WASM_EXTERNAL_FUNCTION:
143 Im.SigIndex = Import.SigIndex;
144 break;
145 case wasm::WASM_EXTERNAL_GLOBAL:
Sam Clegg41db5192017-05-10 00:14:04 +0000146 Im.GlobalImport.Type = Import.Global.Type;
147 Im.GlobalImport.Mutable = Import.Global.Mutable;
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000148 break;
149 case wasm::WASM_EXTERNAL_TABLE:
Sam Clegg41db5192017-05-10 00:14:04 +0000150 Im.TableImport = make_table(Import.Table);
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000151 break;
152 case wasm::WASM_EXTERNAL_MEMORY:
153 Im.Memory = make_limits(Import.Memory);
154 break;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000155 }
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000156 ImportSec->Imports.push_back(Im);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000157 }
158 S = std::move(ImportSec);
159 break;
160 }
161 case wasm::WASM_SEC_FUNCTION: {
162 auto FuncSec = make_unique<WasmYAML::FunctionSection>();
163 for (const auto &Func : Obj.functionTypes()) {
164 FuncSec->FunctionTypes.push_back(Func);
165 }
166 S = std::move(FuncSec);
167 break;
168 }
169 case wasm::WASM_SEC_TABLE: {
170 auto TableSec = make_unique<WasmYAML::TableSection>();
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000171 for (const wasm::WasmTable &Table : Obj.tables()) {
172 TableSec->Tables.push_back(make_table(Table));
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000173 }
174 S = std::move(TableSec);
175 break;
176 }
177 case wasm::WASM_SEC_MEMORY: {
178 auto MemorySec = make_unique<WasmYAML::MemorySection>();
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000179 for (const wasm::WasmLimits &Memory : Obj.memories()) {
180 MemorySec->Memories.push_back(make_limits(Memory));
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000181 }
182 S = std::move(MemorySec);
183 break;
184 }
185 case wasm::WASM_SEC_GLOBAL: {
186 auto GlobalSec = make_unique<WasmYAML::GlobalSection>();
187 for (auto &Global : Obj.globals()) {
188 WasmYAML::Global G;
Sam Clegge53af7f2018-01-09 21:38:53 +0000189 G.Index = Global.Index;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000190 G.Type = Global.Type;
191 G.Mutable = Global.Mutable;
192 G.InitExpr = Global.InitExpr;
193 GlobalSec->Globals.push_back(G);
194 }
195 S = std::move(GlobalSec);
196 break;
197 }
198 case wasm::WASM_SEC_START: {
199 auto StartSec = make_unique<WasmYAML::StartSection>();
200 StartSec->StartFunction = Obj.startFunction();
201 S = std::move(StartSec);
202 break;
203 }
204 case wasm::WASM_SEC_EXPORT: {
205 auto ExportSec = make_unique<WasmYAML::ExportSection>();
206 for (auto &Export : Obj.exports()) {
207 WasmYAML::Export Ex;
208 Ex.Name = Export.Name;
209 Ex.Kind = Export.Kind;
210 Ex.Index = Export.Index;
211 ExportSec->Exports.push_back(Ex);
212 }
213 S = std::move(ExportSec);
214 break;
215 }
216 case wasm::WASM_SEC_ELEM: {
217 auto ElemSec = make_unique<WasmYAML::ElemSection>();
218 for (auto &Segment : Obj.elements()) {
219 WasmYAML::ElemSegment Seg;
220 Seg.TableIndex = Segment.TableIndex;
221 Seg.Offset = Segment.Offset;
222 for (auto &Func : Segment.Functions) {
223 Seg.Functions.push_back(Func);
224 }
225 ElemSec->Segments.push_back(Seg);
226 }
227 S = std::move(ElemSec);
228 break;
229 }
230 case wasm::WASM_SEC_CODE: {
231 auto CodeSec = make_unique<WasmYAML::CodeSection>();
232 for (auto &Func : Obj.functions()) {
233 WasmYAML::Function Function;
Sam Clegge53af7f2018-01-09 21:38:53 +0000234 Function.Index = Func.Index;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000235 for (auto &Local : Func.Locals) {
236 WasmYAML::LocalDecl LocalDecl;
237 LocalDecl.Type = Local.Type;
238 LocalDecl.Count = Local.Count;
239 Function.Locals.push_back(LocalDecl);
240 }
241 Function.Body = yaml::BinaryRef(Func.Body);
242 CodeSec->Functions.push_back(Function);
243 }
244 S = std::move(CodeSec);
245 break;
246 }
247 case wasm::WASM_SEC_DATA: {
248 auto DataSec = make_unique<WasmYAML::DataSection>();
Sam Cleggd95ed952017-09-20 19:03:35 +0000249 for (const object::WasmSegment &Segment : Obj.dataSegments()) {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000250 WasmYAML::DataSegment Seg;
Sam Clegg9c07f942017-07-12 00:24:54 +0000251 Seg.SectionOffset = Segment.SectionOffset;
252 Seg.MemoryIndex = Segment.Data.MemoryIndex;
253 Seg.Offset = Segment.Data.Offset;
254 Seg.Content = yaml::BinaryRef(Segment.Data.Content);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000255 DataSec->Segments.push_back(Seg);
256 }
257 S = std::move(DataSec);
258 break;
259 }
260 default:
261 llvm_unreachable("Unknown section type");
262 break;
263 }
264 for (const wasm::WasmRelocation &Reloc: WasmSec.Relocations) {
265 WasmYAML::Relocation R;
266 R.Type = Reloc.Type;
267 R.Index = Reloc.Index;
268 R.Offset = Reloc.Offset;
269 R.Addend = Reloc.Addend;
270 S->Relocations.push_back(R);
271 }
272 Y->Sections.push_back(std::move(S));
273 }
274
275 return Y.release();
276}
277
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000278std::error_code wasm2yaml(raw_ostream &Out, const object::WasmObjectFile &Obj) {
279 WasmDumper Dumper(Obj);
280 ErrorOr<WasmYAML::Object *> YAMLOrErr = Dumper.dump();
281 if (std::error_code EC = YAMLOrErr.getError())
282 return EC;
283
284 std::unique_ptr<WasmYAML::Object> YAML(YAMLOrErr.get());
285 yaml::Output Yout(Out);
286 Yout << *YAML;
287
288 return std::error_code();
289}