blob: e3577d4ef4fd5cc6ff23039bd1a695c2e5a745b5 [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 Cleggea7cace2018-01-09 23:43:14 +000068 std::map<StringRef,size_t> ComdatIndexes;
69 for (StringRef ComdatName : Obj.comdats()) {
70 ComdatIndexes[ComdatName] = LinkingSec->Comdats.size();
71 LinkingSec->Comdats.emplace_back(WasmYAML::Comdat{ComdatName, {}});
72 }
73 for (auto &Func : Obj.functions()) {
74 if (!Func.Comdat.empty()) {
75 auto &Comdat = LinkingSec->Comdats[ComdatIndexes[Func.Comdat]];
76 Comdat.Entries.emplace_back(
77 WasmYAML::ComdatEntry{wasm::WASM_COMDAT_FUNCTION, Func.Index});
78 }
79 }
80 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 }
Sam Cleggea7cace2018-01-09 23:43:14 +000090 if (!Segment.Data.Comdat.empty()) {
91 auto &Comdat = LinkingSec->Comdats[ComdatIndexes[Segment.Data.Comdat]];
92 Comdat.Entries.emplace_back(
93 WasmYAML::ComdatEntry{wasm::WASM_COMDAT_DATA, SegmentIndex});
94 }
95 SegmentIndex++;
Sam Cleggd95ed952017-09-20 19:03:35 +000096 }
Sam Cleggb7787fd2017-06-20 04:04:59 +000097 for (const object::SymbolRef& Sym: Obj.symbols()) {
98 const object::WasmSymbol Symbol = Obj.getWasmSymbol(Sym);
99 if (Symbol.Flags != 0) {
Sam Clegg42739982017-12-14 21:10:03 +0000100 WasmYAML::SymbolInfo Info{Symbol.Name, Symbol.Flags};
101 LinkingSec->SymbolInfos.emplace_back(Info);
Sam Cleggb7787fd2017-06-20 04:04:59 +0000102 }
103 }
Sam Clegg9e1ade92017-06-27 20:27:59 +0000104 LinkingSec->DataSize = Obj.linkingData().DataSize;
Sam Clegg42739982017-12-14 21:10:03 +0000105 for (const wasm::WasmInitFunc &Func : Obj.linkingData().InitFunctions) {
106 WasmYAML::InitFunction F{Func.Priority, Func.FunctionIndex};
107 LinkingSec->InitFunctions.emplace_back(F);
108 }
Sam Cleggb7787fd2017-06-20 04:04:59 +0000109 CustomSec = std::move(LinkingSec);
110 } else {
111 CustomSec = make_unique<WasmYAML::CustomSection>(WasmSec.Name);
112 }
113 CustomSec->Payload = yaml::BinaryRef(WasmSec.Content);
114 return CustomSec;
115}
116
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000117ErrorOr<WasmYAML::Object *> WasmDumper::dump() {
118 auto Y = make_unique<WasmYAML::Object>();
119
120 // Dump header
121 Y->Header.Version = Obj.getHeader().Version;
122
123 // Dump sections
124 for (const auto &Sec : Obj.sections()) {
Sam Cleggb7787fd2017-06-20 04:04:59 +0000125 const WasmSection &WasmSec = Obj.getWasmSection(Sec);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000126 std::unique_ptr<WasmYAML::Section> S;
127 switch (WasmSec.Type) {
128 case wasm::WASM_SEC_CUSTOM: {
129 if (WasmSec.Name.startswith("reloc.")) {
130 // Relocations are attached the sections they apply to rather than
131 // being represented as a custom section in the YAML output.
132 continue;
133 }
Sam Cleggb7787fd2017-06-20 04:04:59 +0000134 S = dumpCustomSection(WasmSec);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000135 break;
136 }
137 case wasm::WASM_SEC_TYPE: {
138 auto TypeSec = make_unique<WasmYAML::TypeSection>();
139 uint32_t Index = 0;
140 for (const auto &FunctionSig : Obj.types()) {
141 WasmYAML::Signature Sig;
142 Sig.Index = Index++;
143 Sig.ReturnType = FunctionSig.ReturnType;
144 for (const auto &ParamType : FunctionSig.ParamTypes)
145 Sig.ParamTypes.push_back(ParamType);
146 TypeSec->Signatures.push_back(Sig);
147 }
148 S = std::move(TypeSec);
149 break;
150 }
151 case wasm::WASM_SEC_IMPORT: {
152 auto ImportSec = make_unique<WasmYAML::ImportSection>();
153 for (auto &Import : Obj.imports()) {
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000154 WasmYAML::Import Im;
155 Im.Module = Import.Module;
156 Im.Field = Import.Field;
157 Im.Kind = Import.Kind;
158 switch (Im.Kind) {
159 case wasm::WASM_EXTERNAL_FUNCTION:
160 Im.SigIndex = Import.SigIndex;
161 break;
162 case wasm::WASM_EXTERNAL_GLOBAL:
Sam Clegg41db5192017-05-10 00:14:04 +0000163 Im.GlobalImport.Type = Import.Global.Type;
164 Im.GlobalImport.Mutable = Import.Global.Mutable;
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000165 break;
166 case wasm::WASM_EXTERNAL_TABLE:
Sam Clegg41db5192017-05-10 00:14:04 +0000167 Im.TableImport = make_table(Import.Table);
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000168 break;
169 case wasm::WASM_EXTERNAL_MEMORY:
170 Im.Memory = make_limits(Import.Memory);
171 break;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000172 }
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000173 ImportSec->Imports.push_back(Im);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000174 }
175 S = std::move(ImportSec);
176 break;
177 }
178 case wasm::WASM_SEC_FUNCTION: {
179 auto FuncSec = make_unique<WasmYAML::FunctionSection>();
180 for (const auto &Func : Obj.functionTypes()) {
181 FuncSec->FunctionTypes.push_back(Func);
182 }
183 S = std::move(FuncSec);
184 break;
185 }
186 case wasm::WASM_SEC_TABLE: {
187 auto TableSec = make_unique<WasmYAML::TableSection>();
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000188 for (const wasm::WasmTable &Table : Obj.tables()) {
189 TableSec->Tables.push_back(make_table(Table));
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000190 }
191 S = std::move(TableSec);
192 break;
193 }
194 case wasm::WASM_SEC_MEMORY: {
195 auto MemorySec = make_unique<WasmYAML::MemorySection>();
Sam Clegg2ffff5a2017-05-09 23:48:41 +0000196 for (const wasm::WasmLimits &Memory : Obj.memories()) {
197 MemorySec->Memories.push_back(make_limits(Memory));
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000198 }
199 S = std::move(MemorySec);
200 break;
201 }
202 case wasm::WASM_SEC_GLOBAL: {
203 auto GlobalSec = make_unique<WasmYAML::GlobalSection>();
204 for (auto &Global : Obj.globals()) {
205 WasmYAML::Global G;
Sam Clegge53af7f2018-01-09 21:38:53 +0000206 G.Index = Global.Index;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000207 G.Type = Global.Type;
208 G.Mutable = Global.Mutable;
209 G.InitExpr = Global.InitExpr;
210 GlobalSec->Globals.push_back(G);
211 }
212 S = std::move(GlobalSec);
213 break;
214 }
215 case wasm::WASM_SEC_START: {
216 auto StartSec = make_unique<WasmYAML::StartSection>();
217 StartSec->StartFunction = Obj.startFunction();
218 S = std::move(StartSec);
219 break;
220 }
221 case wasm::WASM_SEC_EXPORT: {
222 auto ExportSec = make_unique<WasmYAML::ExportSection>();
223 for (auto &Export : Obj.exports()) {
224 WasmYAML::Export Ex;
225 Ex.Name = Export.Name;
226 Ex.Kind = Export.Kind;
227 Ex.Index = Export.Index;
228 ExportSec->Exports.push_back(Ex);
229 }
230 S = std::move(ExportSec);
231 break;
232 }
233 case wasm::WASM_SEC_ELEM: {
234 auto ElemSec = make_unique<WasmYAML::ElemSection>();
235 for (auto &Segment : Obj.elements()) {
236 WasmYAML::ElemSegment Seg;
237 Seg.TableIndex = Segment.TableIndex;
238 Seg.Offset = Segment.Offset;
239 for (auto &Func : Segment.Functions) {
240 Seg.Functions.push_back(Func);
241 }
242 ElemSec->Segments.push_back(Seg);
243 }
244 S = std::move(ElemSec);
245 break;
246 }
247 case wasm::WASM_SEC_CODE: {
248 auto CodeSec = make_unique<WasmYAML::CodeSection>();
249 for (auto &Func : Obj.functions()) {
250 WasmYAML::Function Function;
Sam Clegge53af7f2018-01-09 21:38:53 +0000251 Function.Index = Func.Index;
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000252 for (auto &Local : Func.Locals) {
253 WasmYAML::LocalDecl LocalDecl;
254 LocalDecl.Type = Local.Type;
255 LocalDecl.Count = Local.Count;
256 Function.Locals.push_back(LocalDecl);
257 }
258 Function.Body = yaml::BinaryRef(Func.Body);
259 CodeSec->Functions.push_back(Function);
260 }
261 S = std::move(CodeSec);
262 break;
263 }
264 case wasm::WASM_SEC_DATA: {
265 auto DataSec = make_unique<WasmYAML::DataSection>();
Sam Cleggd95ed952017-09-20 19:03:35 +0000266 for (const object::WasmSegment &Segment : Obj.dataSegments()) {
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000267 WasmYAML::DataSegment Seg;
Sam Clegg9c07f942017-07-12 00:24:54 +0000268 Seg.SectionOffset = Segment.SectionOffset;
269 Seg.MemoryIndex = Segment.Data.MemoryIndex;
270 Seg.Offset = Segment.Data.Offset;
271 Seg.Content = yaml::BinaryRef(Segment.Data.Content);
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000272 DataSec->Segments.push_back(Seg);
273 }
274 S = std::move(DataSec);
275 break;
276 }
277 default:
278 llvm_unreachable("Unknown section type");
279 break;
280 }
281 for (const wasm::WasmRelocation &Reloc: WasmSec.Relocations) {
282 WasmYAML::Relocation R;
283 R.Type = Reloc.Type;
284 R.Index = Reloc.Index;
285 R.Offset = Reloc.Offset;
286 R.Addend = Reloc.Addend;
287 S->Relocations.push_back(R);
288 }
289 Y->Sections.push_back(std::move(S));
290 }
291
292 return Y.release();
293}
294
Derek Schuffd3d84fd2017-03-30 19:44:09 +0000295std::error_code wasm2yaml(raw_ostream &Out, const object::WasmObjectFile &Obj) {
296 WasmDumper Dumper(Obj);
297 ErrorOr<WasmYAML::Object *> YAMLOrErr = Dumper.dump();
298 if (std::error_code EC = YAMLOrErr.getError())
299 return EC;
300
301 std::unique_ptr<WasmYAML::Object> YAML(YAMLOrErr.get());
302 yaml::Output Yout(Out);
303 Yout << *YAML;
304
305 return std::error_code();
306}