blob: cc04b995f667a257bd3fda1a0ad3229b7911f4f7 [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;
17
18namespace {
19
20class WasmDumper {
21 const object::WasmObjectFile &Obj;
22
23public:
24 WasmDumper(const object::WasmObjectFile &O) : Obj(O) {}
25 ErrorOr<WasmYAML::Object *> dump();
26};
27
28ErrorOr<WasmYAML::Object *> WasmDumper::dump() {
29 auto Y = make_unique<WasmYAML::Object>();
30
31 // Dump header
32 Y->Header.Version = Obj.getHeader().Version;
33
34 // Dump sections
35 for (const auto &Sec : Obj.sections()) {
36 const object::WasmSection &WasmSec = Obj.getWasmSection(Sec);
37 std::unique_ptr<WasmYAML::Section> S;
38 switch (WasmSec.Type) {
39 case wasm::WASM_SEC_CUSTOM: {
40 if (WasmSec.Name.startswith("reloc.")) {
41 // Relocations are attached the sections they apply to rather than
42 // being represented as a custom section in the YAML output.
43 continue;
44 }
45 auto CustomSec = make_unique<WasmYAML::CustomSection>();
46 CustomSec->Name = WasmSec.Name;
Sam Clegg03cdd122017-05-05 18:12:34 +000047 if (CustomSec->Name == "name") {
48 for (const object::SymbolRef& Sym: Obj.symbols()) {
49 uint32_t Flags = Sym.getFlags();
50 // Skip over symbols that come from imports or exports
51 if (Flags &
52 (object::SymbolRef::SF_Global | object::SymbolRef::SF_Undefined))
53 continue;
54 Expected<StringRef> NameOrError = Sym.getName();
55 if (!NameOrError)
56 continue;
57 WasmYAML::NameEntry NameEntry;
58 NameEntry.Name = *NameOrError;
59 NameEntry.Index = Sym.getValue();
60 CustomSec->FunctionNames.push_back(NameEntry);
61 }
62 } else {
63 CustomSec->Payload = yaml::BinaryRef(WasmSec.Content);
64 }
Derek Schuffd3d84fd2017-03-30 19:44:09 +000065 S = std::move(CustomSec);
66 break;
67 }
68 case wasm::WASM_SEC_TYPE: {
69 auto TypeSec = make_unique<WasmYAML::TypeSection>();
70 uint32_t Index = 0;
71 for (const auto &FunctionSig : Obj.types()) {
72 WasmYAML::Signature Sig;
73 Sig.Index = Index++;
74 Sig.ReturnType = FunctionSig.ReturnType;
75 for (const auto &ParamType : FunctionSig.ParamTypes)
76 Sig.ParamTypes.push_back(ParamType);
77 TypeSec->Signatures.push_back(Sig);
78 }
79 S = std::move(TypeSec);
80 break;
81 }
82 case wasm::WASM_SEC_IMPORT: {
83 auto ImportSec = make_unique<WasmYAML::ImportSection>();
84 for (auto &Import : Obj.imports()) {
85 WasmYAML::Import Ex;
86 Ex.Module = Import.Module;
87 Ex.Field = Import.Field;
88 Ex.Kind = Import.Kind;
89 if (Ex.Kind == wasm::WASM_EXTERNAL_FUNCTION) {
90 Ex.SigIndex = Import.SigIndex;
91 } else if (Ex.Kind == wasm::WASM_EXTERNAL_GLOBAL) {
92 Ex.GlobalType = Import.GlobalType;
93 Ex.GlobalMutable = Import.GlobalMutable;
94 }
95 ImportSec->Imports.push_back(Ex);
96 }
97 S = std::move(ImportSec);
98 break;
99 }
100 case wasm::WASM_SEC_FUNCTION: {
101 auto FuncSec = make_unique<WasmYAML::FunctionSection>();
102 for (const auto &Func : Obj.functionTypes()) {
103 FuncSec->FunctionTypes.push_back(Func);
104 }
105 S = std::move(FuncSec);
106 break;
107 }
108 case wasm::WASM_SEC_TABLE: {
109 auto TableSec = make_unique<WasmYAML::TableSection>();
110 for (auto &Table : Obj.tables()) {
111 WasmYAML::Table T;
112 T.ElemType = Table.ElemType;
113 T.TableLimits.Flags = Table.Limits.Flags;
114 T.TableLimits.Initial = Table.Limits.Initial;
115 T.TableLimits.Maximum = Table.Limits.Maximum;
116 TableSec->Tables.push_back(T);
117 }
118 S = std::move(TableSec);
119 break;
120 }
121 case wasm::WASM_SEC_MEMORY: {
122 auto MemorySec = make_unique<WasmYAML::MemorySection>();
123 for (auto &Memory : Obj.memories()) {
124 WasmYAML::Limits L;
125 L.Flags = Memory.Flags;
126 L.Initial = Memory.Initial;
127 L.Maximum = Memory.Maximum;
128 MemorySec->Memories.push_back(L);
129 }
130 S = std::move(MemorySec);
131 break;
132 }
133 case wasm::WASM_SEC_GLOBAL: {
134 auto GlobalSec = make_unique<WasmYAML::GlobalSection>();
135 for (auto &Global : Obj.globals()) {
136 WasmYAML::Global G;
137 G.Type = Global.Type;
138 G.Mutable = Global.Mutable;
139 G.InitExpr = Global.InitExpr;
140 GlobalSec->Globals.push_back(G);
141 }
142 S = std::move(GlobalSec);
143 break;
144 }
145 case wasm::WASM_SEC_START: {
146 auto StartSec = make_unique<WasmYAML::StartSection>();
147 StartSec->StartFunction = Obj.startFunction();
148 S = std::move(StartSec);
149 break;
150 }
151 case wasm::WASM_SEC_EXPORT: {
152 auto ExportSec = make_unique<WasmYAML::ExportSection>();
153 for (auto &Export : Obj.exports()) {
154 WasmYAML::Export Ex;
155 Ex.Name = Export.Name;
156 Ex.Kind = Export.Kind;
157 Ex.Index = Export.Index;
158 ExportSec->Exports.push_back(Ex);
159 }
160 S = std::move(ExportSec);
161 break;
162 }
163 case wasm::WASM_SEC_ELEM: {
164 auto ElemSec = make_unique<WasmYAML::ElemSection>();
165 for (auto &Segment : Obj.elements()) {
166 WasmYAML::ElemSegment Seg;
167 Seg.TableIndex = Segment.TableIndex;
168 Seg.Offset = Segment.Offset;
169 for (auto &Func : Segment.Functions) {
170 Seg.Functions.push_back(Func);
171 }
172 ElemSec->Segments.push_back(Seg);
173 }
174 S = std::move(ElemSec);
175 break;
176 }
177 case wasm::WASM_SEC_CODE: {
178 auto CodeSec = make_unique<WasmYAML::CodeSection>();
179 for (auto &Func : Obj.functions()) {
180 WasmYAML::Function Function;
181 for (auto &Local : Func.Locals) {
182 WasmYAML::LocalDecl LocalDecl;
183 LocalDecl.Type = Local.Type;
184 LocalDecl.Count = Local.Count;
185 Function.Locals.push_back(LocalDecl);
186 }
187 Function.Body = yaml::BinaryRef(Func.Body);
188 CodeSec->Functions.push_back(Function);
189 }
190 S = std::move(CodeSec);
191 break;
192 }
193 case wasm::WASM_SEC_DATA: {
194 auto DataSec = make_unique<WasmYAML::DataSection>();
195 for (auto &Segment : Obj.dataSegments()) {
196 WasmYAML::DataSegment Seg;
197 Seg.Index = Segment.Index;
198 Seg.Offset = Segment.Offset;
199 Seg.Content = yaml::BinaryRef(Segment.Content);
200 DataSec->Segments.push_back(Seg);
201 }
202 S = std::move(DataSec);
203 break;
204 }
205 default:
206 llvm_unreachable("Unknown section type");
207 break;
208 }
209 for (const wasm::WasmRelocation &Reloc: WasmSec.Relocations) {
210 WasmYAML::Relocation R;
211 R.Type = Reloc.Type;
212 R.Index = Reloc.Index;
213 R.Offset = Reloc.Offset;
214 R.Addend = Reloc.Addend;
215 S->Relocations.push_back(R);
216 }
217 Y->Sections.push_back(std::move(S));
218 }
219
220 return Y.release();
221}
222
223} // namespace
224
225std::error_code wasm2yaml(raw_ostream &Out, const object::WasmObjectFile &Obj) {
226 WasmDumper Dumper(Obj);
227 ErrorOr<WasmYAML::Object *> YAMLOrErr = Dumper.dump();
228 if (std::error_code EC = YAMLOrErr.getError())
229 return EC;
230
231 std::unique_ptr<WasmYAML::Object> YAML(YAMLOrErr.get());
232 yaml::Output Yout(Out);
233 Yout << *YAML;
234
235 return std::error_code();
236}