blob: ebd8b57f2ebdad3d449ef6fca79e72ffa987a144 [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>();
Sam Clegg9f3fe422018-01-17 19:28:43 +000056 for (const llvm::wasm::WasmFunctionName &Func: Obj.debugNames()) {
Sam Cleggb7787fd2017-06-20 04:04:59 +000057 WasmYAML::NameEntry NameEntry;
Sam Clegg9f3fe422018-01-17 19:28:43 +000058 NameEntry.Name = Func.Name;
59 NameEntry.Index = Func.Index;
Sam Cleggb7787fd2017-06-20 04:04:59 +000060 NameSec->FunctionNames.push_back(NameEntry);
61 }
62 CustomSec = std::move(NameSec);
63 } else if (WasmSec.Name == "linking") {
64 std::unique_ptr<WasmYAML::LinkingSection> LinkingSec = make_unique<WasmYAML::LinkingSection>();
Sam Cleggea7cace2018-01-09 23:43:14 +000065 std::map<StringRef,size_t> ComdatIndexes;
66 for (StringRef ComdatName : Obj.comdats()) {
67 ComdatIndexes[ComdatName] = LinkingSec->Comdats.size();
68 LinkingSec->Comdats.emplace_back(WasmYAML::Comdat{ComdatName, {}});
69 }
70 for (auto &Func : Obj.functions()) {
71 if (!Func.Comdat.empty()) {
72 auto &Comdat = LinkingSec->Comdats[ComdatIndexes[Func.Comdat]];
73 Comdat.Entries.emplace_back(
74 WasmYAML::ComdatEntry{wasm::WASM_COMDAT_FUNCTION, Func.Index});
75 }
76 }
77 uint32_t SegmentIndex = 0;
Sam Cleggd95ed952017-09-20 19:03:35 +000078 for (const object::WasmSegment &Segment : Obj.dataSegments()) {
79 if (!Segment.Data.Name.empty()) {
Sam Clegg63ebb812017-09-29 16:50:08 +000080 WasmYAML::SegmentInfo SegmentInfo;
81 SegmentInfo.Name = Segment.Data.Name;
Sam Cleggea7cace2018-01-09 23:43:14 +000082 SegmentInfo.Index = SegmentIndex;
Sam Clegg63ebb812017-09-29 16:50:08 +000083 SegmentInfo.Alignment = Segment.Data.Alignment;
84 SegmentInfo.Flags = Segment.Data.Flags;
85 LinkingSec->SegmentInfos.push_back(SegmentInfo);
Sam Cleggd95ed952017-09-20 19:03:35 +000086 }
Sam Cleggea7cace2018-01-09 23:43:14 +000087 if (!Segment.Data.Comdat.empty()) {
88 auto &Comdat = LinkingSec->Comdats[ComdatIndexes[Segment.Data.Comdat]];
89 Comdat.Entries.emplace_back(
90 WasmYAML::ComdatEntry{wasm::WASM_COMDAT_DATA, SegmentIndex});
91 }
92 SegmentIndex++;
Sam Cleggd95ed952017-09-20 19:03:35 +000093 }
Sam Clegg6c899ba2018-02-23 05:08:34 +000094 uint32_t SymbolIndex = 0;
95 for (const wasm::WasmSymbolInfo &Symbol : Obj.linkingData().SymbolTable) {
96 WasmYAML::SymbolInfo Info;
97 Info.Index = SymbolIndex++;
98 Info.Kind = static_cast<uint32_t>(Symbol.Kind);
99 Info.Name = Symbol.Name;
100 Info.Flags = Symbol.Flags;
101 switch (Symbol.Kind) {
102 case wasm::WASM_SYMBOL_TYPE_DATA:
103 Info.DataRef = Symbol.DataRef;
104 break;
105 case wasm::WASM_SYMBOL_TYPE_FUNCTION:
106 case wasm::WASM_SYMBOL_TYPE_GLOBAL:
107 Info.ElementIndex = Symbol.ElementIndex;
108 break;
Sam Cleggb7787fd2017-06-20 04:04:59 +0000109 }
Sam Clegg6c899ba2018-02-23 05:08:34 +0000110 LinkingSec->SymbolTable.emplace_back(Info);
Sam Cleggb7787fd2017-06-20 04:04:59 +0000111 }
Sam Clegg9e1ade92017-06-27 20:27:59 +0000112 LinkingSec->DataSize = Obj.linkingData().DataSize;
Sam Clegg42739982017-12-14 21:10:03 +0000113 for (const wasm::WasmInitFunc &Func : Obj.linkingData().InitFunctions) {
Sam Clegg6c899ba2018-02-23 05:08:34 +0000114 WasmYAML::InitFunction F{Func.Priority, Func.Symbol};
Sam Clegg42739982017-12-14 21:10:03 +0000115 LinkingSec->InitFunctions.emplace_back(F);
116 }
Sam Cleggb7787fd2017-06-20 04:04:59 +0000117 CustomSec = std::move(LinkingSec);
118 } else {
119 CustomSec = make_unique<WasmYAML::CustomSection>(WasmSec.Name);
120 }
121 CustomSec->Payload = yaml::BinaryRef(WasmSec.Content);
122 return CustomSec;
123}
124
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000125ErrorOr<WasmYAML::Object *> WasmDumper::dump() {
126 auto Y = make_unique<WasmYAML::Object>();
127
128 // Dump header
129 Y->Header.Version = Obj.getHeader().Version;
130
131 // Dump sections
132 for (const auto &Sec : Obj.sections()) {
Sam Cleggb7787fd2017-06-20 04:04:59 +0000133 const WasmSection &WasmSec = Obj.getWasmSection(Sec);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000134 std::unique_ptr<WasmYAML::Section> S;
135 switch (WasmSec.Type) {
136 case wasm::WASM_SEC_CUSTOM: {
137 if (WasmSec.Name.startswith("reloc.")) {
138 // Relocations are attached the sections they apply to rather than
139 // being represented as a custom section in the YAML output.
140 continue;
141 }
Sam Cleggb7787fd2017-06-20 04:04:59 +0000142 S = dumpCustomSection(WasmSec);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000143 break;
144 }
145 case wasm::WASM_SEC_TYPE: {
146 auto TypeSec = make_unique<WasmYAML::TypeSection>();
147 uint32_t Index = 0;
148 for (const auto &FunctionSig : Obj.types()) {
149 WasmYAML::Signature Sig;
150 Sig.Index = Index++;
151 Sig.ReturnType = FunctionSig.ReturnType;
152 for (const auto &ParamType : FunctionSig.ParamTypes)
153 Sig.ParamTypes.push_back(ParamType);
154 TypeSec->Signatures.push_back(Sig);
155 }
156 S = std::move(TypeSec);
157 break;
158 }
159 case wasm::WASM_SEC_IMPORT: {
160 auto ImportSec = make_unique<WasmYAML::ImportSection>();
161 for (auto &Import : Obj.imports()) {
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000162 WasmYAML::Import Im;
163 Im.Module = Import.Module;
164 Im.Field = Import.Field;
165 Im.Kind = Import.Kind;
166 switch (Im.Kind) {
167 case wasm::WASM_EXTERNAL_FUNCTION:
168 Im.SigIndex = Import.SigIndex;
169 break;
170 case wasm::WASM_EXTERNAL_GLOBAL:
Sam Clegg41db5192017-05-10 00:14:04 +0000171 Im.GlobalImport.Type = Import.Global.Type;
172 Im.GlobalImport.Mutable = Import.Global.Mutable;
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000173 break;
174 case wasm::WASM_EXTERNAL_TABLE:
Sam Clegg41db5192017-05-10 00:14:04 +0000175 Im.TableImport = make_table(Import.Table);
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000176 break;
177 case wasm::WASM_EXTERNAL_MEMORY:
178 Im.Memory = make_limits(Import.Memory);
179 break;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000180 }
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000181 ImportSec->Imports.push_back(Im);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000182 }
183 S = std::move(ImportSec);
184 break;
185 }
186 case wasm::WASM_SEC_FUNCTION: {
187 auto FuncSec = make_unique<WasmYAML::FunctionSection>();
188 for (const auto &Func : Obj.functionTypes()) {
189 FuncSec->FunctionTypes.push_back(Func);
190 }
191 S = std::move(FuncSec);
192 break;
193 }
194 case wasm::WASM_SEC_TABLE: {
195 auto TableSec = make_unique<WasmYAML::TableSection>();
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000196 for (const wasm::WasmTable &Table : Obj.tables()) {
197 TableSec->Tables.push_back(make_table(Table));
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000198 }
199 S = std::move(TableSec);
200 break;
201 }
202 case wasm::WASM_SEC_MEMORY: {
203 auto MemorySec = make_unique<WasmYAML::MemorySection>();
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000204 for (const wasm::WasmLimits &Memory : Obj.memories()) {
205 MemorySec->Memories.push_back(make_limits(Memory));
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000206 }
207 S = std::move(MemorySec);
208 break;
209 }
210 case wasm::WASM_SEC_GLOBAL: {
211 auto GlobalSec = make_unique<WasmYAML::GlobalSection>();
212 for (auto &Global : Obj.globals()) {
213 WasmYAML::Global G;
Sam Clegge53af7f2018-01-09 21:38:53 +0000214 G.Index = Global.Index;
Sam Clegg6e7f1822018-01-31 19:50:14 +0000215 G.Type = Global.Type.Type;
216 G.Mutable = Global.Type.Mutable;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000217 G.InitExpr = Global.InitExpr;
218 GlobalSec->Globals.push_back(G);
219 }
220 S = std::move(GlobalSec);
221 break;
222 }
223 case wasm::WASM_SEC_START: {
224 auto StartSec = make_unique<WasmYAML::StartSection>();
225 StartSec->StartFunction = Obj.startFunction();
226 S = std::move(StartSec);
227 break;
228 }
229 case wasm::WASM_SEC_EXPORT: {
230 auto ExportSec = make_unique<WasmYAML::ExportSection>();
231 for (auto &Export : Obj.exports()) {
232 WasmYAML::Export Ex;
233 Ex.Name = Export.Name;
234 Ex.Kind = Export.Kind;
235 Ex.Index = Export.Index;
236 ExportSec->Exports.push_back(Ex);
237 }
238 S = std::move(ExportSec);
239 break;
240 }
241 case wasm::WASM_SEC_ELEM: {
242 auto ElemSec = make_unique<WasmYAML::ElemSection>();
243 for (auto &Segment : Obj.elements()) {
244 WasmYAML::ElemSegment Seg;
245 Seg.TableIndex = Segment.TableIndex;
246 Seg.Offset = Segment.Offset;
247 for (auto &Func : Segment.Functions) {
248 Seg.Functions.push_back(Func);
249 }
250 ElemSec->Segments.push_back(Seg);
251 }
252 S = std::move(ElemSec);
253 break;
254 }
255 case wasm::WASM_SEC_CODE: {
256 auto CodeSec = make_unique<WasmYAML::CodeSection>();
257 for (auto &Func : Obj.functions()) {
258 WasmYAML::Function Function;
Sam Clegge53af7f2018-01-09 21:38:53 +0000259 Function.Index = Func.Index;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000260 for (auto &Local : Func.Locals) {
261 WasmYAML::LocalDecl LocalDecl;
262 LocalDecl.Type = Local.Type;
263 LocalDecl.Count = Local.Count;
264 Function.Locals.push_back(LocalDecl);
265 }
266 Function.Body = yaml::BinaryRef(Func.Body);
267 CodeSec->Functions.push_back(Function);
268 }
269 S = std::move(CodeSec);
270 break;
271 }
272 case wasm::WASM_SEC_DATA: {
273 auto DataSec = make_unique<WasmYAML::DataSection>();
Sam Cleggd95ed952017-09-20 19:03:35 +0000274 for (const object::WasmSegment &Segment : Obj.dataSegments()) {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000275 WasmYAML::DataSegment Seg;
Sam Clegg9c07f942017-07-12 00:24:54 +0000276 Seg.SectionOffset = Segment.SectionOffset;
277 Seg.MemoryIndex = Segment.Data.MemoryIndex;
278 Seg.Offset = Segment.Data.Offset;
279 Seg.Content = yaml::BinaryRef(Segment.Data.Content);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000280 DataSec->Segments.push_back(Seg);
281 }
282 S = std::move(DataSec);
283 break;
284 }
285 default:
286 llvm_unreachable("Unknown section type");
287 break;
288 }
289 for (const wasm::WasmRelocation &Reloc: WasmSec.Relocations) {
290 WasmYAML::Relocation R;
291 R.Type = Reloc.Type;
292 R.Index = Reloc.Index;
293 R.Offset = Reloc.Offset;
294 R.Addend = Reloc.Addend;
295 S->Relocations.push_back(R);
296 }
297 Y->Sections.push_back(std::move(S));
298 }
299
300 return Y.release();
301}
302
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000303std::error_code wasm2yaml(raw_ostream &Out, const object::WasmObjectFile &Obj) {
304 WasmDumper Dumper(Obj);
305 ErrorOr<WasmYAML::Object *> YAMLOrErr = Dumper.dump();
306 if (std::error_code EC = YAMLOrErr.getError())
307 return EC;
308
309 std::unique_ptr<WasmYAML::Object> YAML(YAMLOrErr.get());
310 yaml::Output Yout(Out);
311 Yout << *YAML;
312
313 return std::error_code();
314}