blob: 1bf8149493c4f901e906c638d46e72c3ad952ac0 [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;
189 G.Type = Global.Type;
190 G.Mutable = Global.Mutable;
191 G.InitExpr = Global.InitExpr;
192 GlobalSec->Globals.push_back(G);
193 }
194 S = std::move(GlobalSec);
195 break;
196 }
197 case wasm::WASM_SEC_START: {
198 auto StartSec = make_unique<WasmYAML::StartSection>();
199 StartSec->StartFunction = Obj.startFunction();
200 S = std::move(StartSec);
201 break;
202 }
203 case wasm::WASM_SEC_EXPORT: {
204 auto ExportSec = make_unique<WasmYAML::ExportSection>();
205 for (auto &Export : Obj.exports()) {
206 WasmYAML::Export Ex;
207 Ex.Name = Export.Name;
208 Ex.Kind = Export.Kind;
209 Ex.Index = Export.Index;
210 ExportSec->Exports.push_back(Ex);
211 }
212 S = std::move(ExportSec);
213 break;
214 }
215 case wasm::WASM_SEC_ELEM: {
216 auto ElemSec = make_unique<WasmYAML::ElemSection>();
217 for (auto &Segment : Obj.elements()) {
218 WasmYAML::ElemSegment Seg;
219 Seg.TableIndex = Segment.TableIndex;
220 Seg.Offset = Segment.Offset;
221 for (auto &Func : Segment.Functions) {
222 Seg.Functions.push_back(Func);
223 }
224 ElemSec->Segments.push_back(Seg);
225 }
226 S = std::move(ElemSec);
227 break;
228 }
229 case wasm::WASM_SEC_CODE: {
230 auto CodeSec = make_unique<WasmYAML::CodeSection>();
231 for (auto &Func : Obj.functions()) {
232 WasmYAML::Function Function;
233 for (auto &Local : Func.Locals) {
234 WasmYAML::LocalDecl LocalDecl;
235 LocalDecl.Type = Local.Type;
236 LocalDecl.Count = Local.Count;
237 Function.Locals.push_back(LocalDecl);
238 }
239 Function.Body = yaml::BinaryRef(Func.Body);
240 CodeSec->Functions.push_back(Function);
241 }
242 S = std::move(CodeSec);
243 break;
244 }
245 case wasm::WASM_SEC_DATA: {
246 auto DataSec = make_unique<WasmYAML::DataSection>();
Sam Cleggd95ed952017-09-20 19:03:35 +0000247 for (const object::WasmSegment &Segment : Obj.dataSegments()) {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000248 WasmYAML::DataSegment Seg;
Sam Clegg9c07f942017-07-12 00:24:54 +0000249 Seg.SectionOffset = Segment.SectionOffset;
250 Seg.MemoryIndex = Segment.Data.MemoryIndex;
251 Seg.Offset = Segment.Data.Offset;
252 Seg.Content = yaml::BinaryRef(Segment.Data.Content);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000253 DataSec->Segments.push_back(Seg);
254 }
255 S = std::move(DataSec);
256 break;
257 }
258 default:
259 llvm_unreachable("Unknown section type");
260 break;
261 }
262 for (const wasm::WasmRelocation &Reloc: WasmSec.Relocations) {
263 WasmYAML::Relocation R;
264 R.Type = Reloc.Type;
265 R.Index = Reloc.Index;
266 R.Offset = Reloc.Offset;
267 R.Addend = Reloc.Addend;
268 S->Relocations.push_back(R);
269 }
270 Y->Sections.push_back(std::move(S));
271 }
272
273 return Y.release();
274}
275
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000276std::error_code wasm2yaml(raw_ostream &Out, const object::WasmObjectFile &Obj) {
277 WasmDumper Dumper(Obj);
278 ErrorOr<WasmYAML::Object *> YAMLOrErr = Dumper.dump();
279 if (std::error_code EC = YAMLOrErr.getError())
280 return EC;
281
282 std::unique_ptr<WasmYAML::Object> YAML(YAMLOrErr.get());
283 yaml::Output Yout(Out);
284 Yout << *YAML;
285
286 return std::error_code();
287}