blob: 210ae680e1b41d34de6bb730ba0177d95a18f3c5 [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 Cleggb7787fd2017-06-20 04:04:59 +000094 for (const object::SymbolRef& Sym: Obj.symbols()) {
95 const object::WasmSymbol Symbol = Obj.getWasmSymbol(Sym);
96 if (Symbol.Flags != 0) {
Sam Clegg42739982017-12-14 21:10:03 +000097 WasmYAML::SymbolInfo Info{Symbol.Name, Symbol.Flags};
98 LinkingSec->SymbolInfos.emplace_back(Info);
Sam Cleggb7787fd2017-06-20 04:04:59 +000099 }
100 }
Sam Clegg9e1ade92017-06-27 20:27:59 +0000101 LinkingSec->DataSize = Obj.linkingData().DataSize;
Sam Clegg42739982017-12-14 21:10:03 +0000102 for (const wasm::WasmInitFunc &Func : Obj.linkingData().InitFunctions) {
103 WasmYAML::InitFunction F{Func.Priority, Func.FunctionIndex};
104 LinkingSec->InitFunctions.emplace_back(F);
105 }
Sam Cleggb7787fd2017-06-20 04:04:59 +0000106 CustomSec = std::move(LinkingSec);
107 } else {
108 CustomSec = make_unique<WasmYAML::CustomSection>(WasmSec.Name);
109 }
110 CustomSec->Payload = yaml::BinaryRef(WasmSec.Content);
111 return CustomSec;
112}
113
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000114ErrorOr<WasmYAML::Object *> WasmDumper::dump() {
115 auto Y = make_unique<WasmYAML::Object>();
116
117 // Dump header
118 Y->Header.Version = Obj.getHeader().Version;
119
120 // Dump sections
121 for (const auto &Sec : Obj.sections()) {
Sam Cleggb7787fd2017-06-20 04:04:59 +0000122 const WasmSection &WasmSec = Obj.getWasmSection(Sec);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000123 std::unique_ptr<WasmYAML::Section> S;
124 switch (WasmSec.Type) {
125 case wasm::WASM_SEC_CUSTOM: {
126 if (WasmSec.Name.startswith("reloc.")) {
127 // Relocations are attached the sections they apply to rather than
128 // being represented as a custom section in the YAML output.
129 continue;
130 }
Sam Cleggb7787fd2017-06-20 04:04:59 +0000131 S = dumpCustomSection(WasmSec);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000132 break;
133 }
134 case wasm::WASM_SEC_TYPE: {
135 auto TypeSec = make_unique<WasmYAML::TypeSection>();
136 uint32_t Index = 0;
137 for (const auto &FunctionSig : Obj.types()) {
138 WasmYAML::Signature Sig;
139 Sig.Index = Index++;
140 Sig.ReturnType = FunctionSig.ReturnType;
141 for (const auto &ParamType : FunctionSig.ParamTypes)
142 Sig.ParamTypes.push_back(ParamType);
143 TypeSec->Signatures.push_back(Sig);
144 }
145 S = std::move(TypeSec);
146 break;
147 }
148 case wasm::WASM_SEC_IMPORT: {
149 auto ImportSec = make_unique<WasmYAML::ImportSection>();
150 for (auto &Import : Obj.imports()) {
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000151 WasmYAML::Import Im;
152 Im.Module = Import.Module;
153 Im.Field = Import.Field;
154 Im.Kind = Import.Kind;
155 switch (Im.Kind) {
156 case wasm::WASM_EXTERNAL_FUNCTION:
157 Im.SigIndex = Import.SigIndex;
158 break;
159 case wasm::WASM_EXTERNAL_GLOBAL:
Sam Clegg41db5192017-05-10 00:14:04 +0000160 Im.GlobalImport.Type = Import.Global.Type;
161 Im.GlobalImport.Mutable = Import.Global.Mutable;
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000162 break;
163 case wasm::WASM_EXTERNAL_TABLE:
Sam Clegg41db5192017-05-10 00:14:04 +0000164 Im.TableImport = make_table(Import.Table);
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000165 break;
166 case wasm::WASM_EXTERNAL_MEMORY:
167 Im.Memory = make_limits(Import.Memory);
168 break;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000169 }
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000170 ImportSec->Imports.push_back(Im);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000171 }
172 S = std::move(ImportSec);
173 break;
174 }
175 case wasm::WASM_SEC_FUNCTION: {
176 auto FuncSec = make_unique<WasmYAML::FunctionSection>();
177 for (const auto &Func : Obj.functionTypes()) {
178 FuncSec->FunctionTypes.push_back(Func);
179 }
180 S = std::move(FuncSec);
181 break;
182 }
183 case wasm::WASM_SEC_TABLE: {
184 auto TableSec = make_unique<WasmYAML::TableSection>();
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000185 for (const wasm::WasmTable &Table : Obj.tables()) {
186 TableSec->Tables.push_back(make_table(Table));
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000187 }
188 S = std::move(TableSec);
189 break;
190 }
191 case wasm::WASM_SEC_MEMORY: {
192 auto MemorySec = make_unique<WasmYAML::MemorySection>();
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000193 for (const wasm::WasmLimits &Memory : Obj.memories()) {
194 MemorySec->Memories.push_back(make_limits(Memory));
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000195 }
196 S = std::move(MemorySec);
197 break;
198 }
199 case wasm::WASM_SEC_GLOBAL: {
200 auto GlobalSec = make_unique<WasmYAML::GlobalSection>();
201 for (auto &Global : Obj.globals()) {
202 WasmYAML::Global G;
Sam Clegge53af7f2018-01-09 21:38:53 +0000203 G.Index = Global.Index;
Sam Clegg6e7f1822018-01-31 19:50:14 +0000204 G.Type = Global.Type.Type;
205 G.Mutable = Global.Type.Mutable;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000206 G.InitExpr = Global.InitExpr;
207 GlobalSec->Globals.push_back(G);
208 }
209 S = std::move(GlobalSec);
210 break;
211 }
212 case wasm::WASM_SEC_START: {
213 auto StartSec = make_unique<WasmYAML::StartSection>();
214 StartSec->StartFunction = Obj.startFunction();
215 S = std::move(StartSec);
216 break;
217 }
218 case wasm::WASM_SEC_EXPORT: {
219 auto ExportSec = make_unique<WasmYAML::ExportSection>();
220 for (auto &Export : Obj.exports()) {
221 WasmYAML::Export Ex;
222 Ex.Name = Export.Name;
223 Ex.Kind = Export.Kind;
224 Ex.Index = Export.Index;
225 ExportSec->Exports.push_back(Ex);
226 }
227 S = std::move(ExportSec);
228 break;
229 }
230 case wasm::WASM_SEC_ELEM: {
231 auto ElemSec = make_unique<WasmYAML::ElemSection>();
232 for (auto &Segment : Obj.elements()) {
233 WasmYAML::ElemSegment Seg;
234 Seg.TableIndex = Segment.TableIndex;
235 Seg.Offset = Segment.Offset;
236 for (auto &Func : Segment.Functions) {
237 Seg.Functions.push_back(Func);
238 }
239 ElemSec->Segments.push_back(Seg);
240 }
241 S = std::move(ElemSec);
242 break;
243 }
244 case wasm::WASM_SEC_CODE: {
245 auto CodeSec = make_unique<WasmYAML::CodeSection>();
246 for (auto &Func : Obj.functions()) {
247 WasmYAML::Function Function;
Sam Clegge53af7f2018-01-09 21:38:53 +0000248 Function.Index = Func.Index;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000249 for (auto &Local : Func.Locals) {
250 WasmYAML::LocalDecl LocalDecl;
251 LocalDecl.Type = Local.Type;
252 LocalDecl.Count = Local.Count;
253 Function.Locals.push_back(LocalDecl);
254 }
255 Function.Body = yaml::BinaryRef(Func.Body);
256 CodeSec->Functions.push_back(Function);
257 }
258 S = std::move(CodeSec);
259 break;
260 }
261 case wasm::WASM_SEC_DATA: {
262 auto DataSec = make_unique<WasmYAML::DataSection>();
Sam Cleggd95ed952017-09-20 19:03:35 +0000263 for (const object::WasmSegment &Segment : Obj.dataSegments()) {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000264 WasmYAML::DataSegment Seg;
Sam Clegg9c07f942017-07-12 00:24:54 +0000265 Seg.SectionOffset = Segment.SectionOffset;
266 Seg.MemoryIndex = Segment.Data.MemoryIndex;
267 Seg.Offset = Segment.Data.Offset;
268 Seg.Content = yaml::BinaryRef(Segment.Data.Content);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000269 DataSec->Segments.push_back(Seg);
270 }
271 S = std::move(DataSec);
272 break;
273 }
274 default:
275 llvm_unreachable("Unknown section type");
276 break;
277 }
278 for (const wasm::WasmRelocation &Reloc: WasmSec.Relocations) {
279 WasmYAML::Relocation R;
280 R.Type = Reloc.Type;
281 R.Index = Reloc.Index;
282 R.Offset = Reloc.Offset;
283 R.Addend = Reloc.Addend;
284 S->Relocations.push_back(R);
285 }
286 Y->Sections.push_back(std::move(S));
287 }
288
289 return Y.release();
290}
291
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000292std::error_code wasm2yaml(raw_ostream &Out, const object::WasmObjectFile &Obj) {
293 WasmDumper Dumper(Obj);
294 ErrorOr<WasmYAML::Object *> YAMLOrErr = Dumper.dump();
295 if (std::error_code EC = YAMLOrErr.getError())
296 return EC;
297
298 std::unique_ptr<WasmYAML::Object> YAML(YAMLOrErr.get());
299 yaml::Output Yout(Out);
300 Yout << *YAML;
301
302 return std::error_code();
303}