Fix for PR34888.
[llvm-core.git] / tools / obj2yaml / wasm2yaml.cpp
blob27398e5b00b9e42a80ab6cc47d35cb5d9d768ea1
1 //===------ 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 //===----------------------------------------------------------------------===//
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"
16 using namespace llvm;
17 using object::WasmSection;
19 namespace {
21 class WasmDumper {
22 const object::WasmObjectFile &Obj;
24 public:
25 WasmDumper(const object::WasmObjectFile &O) : Obj(O) {}
27 ErrorOr<WasmYAML::Object *> dump();
29 std::unique_ptr<WasmYAML::CustomSection>
30 dumpCustomSection(const WasmSection &WasmSec);
33 } // namespace
35 static WasmYAML::Table make_table(const wasm::WasmTable &Table) {
36 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;
44 static WasmYAML::Limits make_limits(const wasm::WasmLimits &Limits) {
45 WasmYAML::Limits L;
46 L.Flags = Limits.Flags;
47 L.Initial = Limits.Initial;
48 L.Maximum = Limits.Maximum;
49 return L;
52 std::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()) {
57 const object::WasmSymbol Symbol = Obj.getWasmSymbol(Sym);
58 if (Symbol.Type != object::WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME)
59 continue;
60 WasmYAML::NameEntry NameEntry;
61 NameEntry.Name = Symbol.Name;
62 NameEntry.Index = Sym.getValue();
63 NameSec->FunctionNames.push_back(NameEntry);
65 CustomSec = std::move(NameSec);
66 } else if (WasmSec.Name == "linking") {
67 std::unique_ptr<WasmYAML::LinkingSection> LinkingSec = make_unique<WasmYAML::LinkingSection>();
68 size_t Index = 0;
69 for (const object::WasmSegment &Segment : Obj.dataSegments()) {
70 if (!Segment.Data.Name.empty()) {
71 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);
78 Index++;
80 for (const object::SymbolRef& Sym: Obj.symbols()) {
81 const object::WasmSymbol Symbol = Obj.getWasmSymbol(Sym);
82 if (Symbol.Flags != 0) {
83 WasmYAML::SymbolInfo Info = { Symbol.Name, Symbol.Flags };
84 LinkingSec->SymbolInfos.push_back(Info);
87 LinkingSec->DataSize = Obj.linkingData().DataSize;
88 CustomSec = std::move(LinkingSec);
89 } else {
90 CustomSec = make_unique<WasmYAML::CustomSection>(WasmSec.Name);
92 CustomSec->Payload = yaml::BinaryRef(WasmSec.Content);
93 return CustomSec;
96 ErrorOr<WasmYAML::Object *> WasmDumper::dump() {
97 auto Y = make_unique<WasmYAML::Object>();
99 // Dump header
100 Y->Header.Version = Obj.getHeader().Version;
102 // Dump sections
103 for (const auto &Sec : Obj.sections()) {
104 const WasmSection &WasmSec = Obj.getWasmSection(Sec);
105 std::unique_ptr<WasmYAML::Section> S;
106 switch (WasmSec.Type) {
107 case wasm::WASM_SEC_CUSTOM: {
108 if (WasmSec.Name.startswith("reloc.")) {
109 // Relocations are attached the sections they apply to rather than
110 // being represented as a custom section in the YAML output.
111 continue;
113 S = dumpCustomSection(WasmSec);
114 break;
116 case wasm::WASM_SEC_TYPE: {
117 auto TypeSec = make_unique<WasmYAML::TypeSection>();
118 uint32_t Index = 0;
119 for (const auto &FunctionSig : Obj.types()) {
120 WasmYAML::Signature Sig;
121 Sig.Index = Index++;
122 Sig.ReturnType = FunctionSig.ReturnType;
123 for (const auto &ParamType : FunctionSig.ParamTypes)
124 Sig.ParamTypes.push_back(ParamType);
125 TypeSec->Signatures.push_back(Sig);
127 S = std::move(TypeSec);
128 break;
130 case wasm::WASM_SEC_IMPORT: {
131 auto ImportSec = make_unique<WasmYAML::ImportSection>();
132 for (auto &Import : Obj.imports()) {
133 WasmYAML::Import Im;
134 Im.Module = Import.Module;
135 Im.Field = Import.Field;
136 Im.Kind = Import.Kind;
137 switch (Im.Kind) {
138 case wasm::WASM_EXTERNAL_FUNCTION:
139 Im.SigIndex = Import.SigIndex;
140 break;
141 case wasm::WASM_EXTERNAL_GLOBAL:
142 Im.GlobalImport.Type = Import.Global.Type;
143 Im.GlobalImport.Mutable = Import.Global.Mutable;
144 break;
145 case wasm::WASM_EXTERNAL_TABLE:
146 Im.TableImport = make_table(Import.Table);
147 break;
148 case wasm::WASM_EXTERNAL_MEMORY:
149 Im.Memory = make_limits(Import.Memory);
150 break;
152 ImportSec->Imports.push_back(Im);
154 S = std::move(ImportSec);
155 break;
157 case wasm::WASM_SEC_FUNCTION: {
158 auto FuncSec = make_unique<WasmYAML::FunctionSection>();
159 for (const auto &Func : Obj.functionTypes()) {
160 FuncSec->FunctionTypes.push_back(Func);
162 S = std::move(FuncSec);
163 break;
165 case wasm::WASM_SEC_TABLE: {
166 auto TableSec = make_unique<WasmYAML::TableSection>();
167 for (const wasm::WasmTable &Table : Obj.tables()) {
168 TableSec->Tables.push_back(make_table(Table));
170 S = std::move(TableSec);
171 break;
173 case wasm::WASM_SEC_MEMORY: {
174 auto MemorySec = make_unique<WasmYAML::MemorySection>();
175 for (const wasm::WasmLimits &Memory : Obj.memories()) {
176 MemorySec->Memories.push_back(make_limits(Memory));
178 S = std::move(MemorySec);
179 break;
181 case wasm::WASM_SEC_GLOBAL: {
182 auto GlobalSec = make_unique<WasmYAML::GlobalSection>();
183 for (auto &Global : Obj.globals()) {
184 WasmYAML::Global G;
185 G.Type = Global.Type;
186 G.Mutable = Global.Mutable;
187 G.InitExpr = Global.InitExpr;
188 GlobalSec->Globals.push_back(G);
190 S = std::move(GlobalSec);
191 break;
193 case wasm::WASM_SEC_START: {
194 auto StartSec = make_unique<WasmYAML::StartSection>();
195 StartSec->StartFunction = Obj.startFunction();
196 S = std::move(StartSec);
197 break;
199 case wasm::WASM_SEC_EXPORT: {
200 auto ExportSec = make_unique<WasmYAML::ExportSection>();
201 for (auto &Export : Obj.exports()) {
202 WasmYAML::Export Ex;
203 Ex.Name = Export.Name;
204 Ex.Kind = Export.Kind;
205 Ex.Index = Export.Index;
206 ExportSec->Exports.push_back(Ex);
208 S = std::move(ExportSec);
209 break;
211 case wasm::WASM_SEC_ELEM: {
212 auto ElemSec = make_unique<WasmYAML::ElemSection>();
213 for (auto &Segment : Obj.elements()) {
214 WasmYAML::ElemSegment Seg;
215 Seg.TableIndex = Segment.TableIndex;
216 Seg.Offset = Segment.Offset;
217 for (auto &Func : Segment.Functions) {
218 Seg.Functions.push_back(Func);
220 ElemSec->Segments.push_back(Seg);
222 S = std::move(ElemSec);
223 break;
225 case wasm::WASM_SEC_CODE: {
226 auto CodeSec = make_unique<WasmYAML::CodeSection>();
227 for (auto &Func : Obj.functions()) {
228 WasmYAML::Function Function;
229 for (auto &Local : Func.Locals) {
230 WasmYAML::LocalDecl LocalDecl;
231 LocalDecl.Type = Local.Type;
232 LocalDecl.Count = Local.Count;
233 Function.Locals.push_back(LocalDecl);
235 Function.Body = yaml::BinaryRef(Func.Body);
236 CodeSec->Functions.push_back(Function);
238 S = std::move(CodeSec);
239 break;
241 case wasm::WASM_SEC_DATA: {
242 auto DataSec = make_unique<WasmYAML::DataSection>();
243 for (const object::WasmSegment &Segment : Obj.dataSegments()) {
244 WasmYAML::DataSegment Seg;
245 Seg.SectionOffset = Segment.SectionOffset;
246 Seg.MemoryIndex = Segment.Data.MemoryIndex;
247 Seg.Offset = Segment.Data.Offset;
248 Seg.Content = yaml::BinaryRef(Segment.Data.Content);
249 DataSec->Segments.push_back(Seg);
251 S = std::move(DataSec);
252 break;
254 default:
255 llvm_unreachable("Unknown section type");
256 break;
258 for (const wasm::WasmRelocation &Reloc: WasmSec.Relocations) {
259 WasmYAML::Relocation R;
260 R.Type = Reloc.Type;
261 R.Index = Reloc.Index;
262 R.Offset = Reloc.Offset;
263 R.Addend = Reloc.Addend;
264 S->Relocations.push_back(R);
266 Y->Sections.push_back(std::move(S));
269 return Y.release();
272 std::error_code wasm2yaml(raw_ostream &Out, const object::WasmObjectFile &Obj) {
273 WasmDumper Dumper(Obj);
274 ErrorOr<WasmYAML::Object *> YAMLOrErr = Dumper.dump();
275 if (std::error_code EC = YAMLOrErr.getError())
276 return EC;
278 std::unique_ptr<WasmYAML::Object> YAML(YAMLOrErr.get());
279 yaml::Output Yout(Out);
280 Yout << *YAML;
282 return std::error_code();