1 //===------ utils/wasm2yaml.cpp - obj2yaml conversion tool ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "llvm/Object/COFF.h"
12 #include "llvm/ObjectYAML/WasmYAML.h"
13 #include "llvm/Support/ErrorHandling.h"
14 #include "llvm/Support/YAMLTraits.h"
17 using object::WasmSection
;
22 const object::WasmObjectFile
&Obj
;
25 WasmDumper(const object::WasmObjectFile
&O
) : Obj(O
) {}
27 ErrorOr
<WasmYAML::Object
*> dump();
29 std::unique_ptr
<WasmYAML::CustomSection
>
30 dumpCustomSection(const WasmSection
&WasmSec
);
35 static WasmYAML::Table
make_table(const wasm::WasmTable
&Table
) {
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
;
44 static WasmYAML::Limits
make_limits(const wasm::WasmLimits
&Limits
) {
46 L
.Flags
= Limits
.Flags
;
47 L
.Initial
= Limits
.Initial
;
48 L
.Maximum
= Limits
.Maximum
;
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
)
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
>();
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
);
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
);
90 CustomSec
= make_unique
<WasmYAML::CustomSection
>(WasmSec
.Name
);
92 CustomSec
->Payload
= yaml::BinaryRef(WasmSec
.Content
);
96 ErrorOr
<WasmYAML::Object
*> WasmDumper::dump() {
97 auto Y
= make_unique
<WasmYAML::Object
>();
100 Y
->Header
.Version
= Obj
.getHeader().Version
;
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.
113 S
= dumpCustomSection(WasmSec
);
116 case wasm::WASM_SEC_TYPE
: {
117 auto TypeSec
= make_unique
<WasmYAML::TypeSection
>();
119 for (const auto &FunctionSig
: Obj
.types()) {
120 WasmYAML::Signature Sig
;
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
);
130 case wasm::WASM_SEC_IMPORT
: {
131 auto ImportSec
= make_unique
<WasmYAML::ImportSection
>();
132 for (auto &Import
: Obj
.imports()) {
134 Im
.Module
= Import
.Module
;
135 Im
.Field
= Import
.Field
;
136 Im
.Kind
= Import
.Kind
;
138 case wasm::WASM_EXTERNAL_FUNCTION
:
139 Im
.SigIndex
= Import
.SigIndex
;
141 case wasm::WASM_EXTERNAL_GLOBAL
:
142 Im
.GlobalImport
.Type
= Import
.Global
.Type
;
143 Im
.GlobalImport
.Mutable
= Import
.Global
.Mutable
;
145 case wasm::WASM_EXTERNAL_TABLE
:
146 Im
.TableImport
= make_table(Import
.Table
);
148 case wasm::WASM_EXTERNAL_MEMORY
:
149 Im
.Memory
= make_limits(Import
.Memory
);
152 ImportSec
->Imports
.push_back(Im
);
154 S
= std::move(ImportSec
);
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
);
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
);
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
);
181 case wasm::WASM_SEC_GLOBAL
: {
182 auto GlobalSec
= make_unique
<WasmYAML::GlobalSection
>();
183 for (auto &Global
: Obj
.globals()) {
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
);
193 case wasm::WASM_SEC_START
: {
194 auto StartSec
= make_unique
<WasmYAML::StartSection
>();
195 StartSec
->StartFunction
= Obj
.startFunction();
196 S
= std::move(StartSec
);
199 case wasm::WASM_SEC_EXPORT
: {
200 auto ExportSec
= make_unique
<WasmYAML::ExportSection
>();
201 for (auto &Export
: Obj
.exports()) {
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
);
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
);
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
);
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
);
255 llvm_unreachable("Unknown section type");
258 for (const wasm::WasmRelocation
&Reloc
: WasmSec
.Relocations
) {
259 WasmYAML::Relocation R
;
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
));
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())
278 std::unique_ptr
<WasmYAML::Object
> YAML(YAMLOrErr
.get());
279 yaml::Output
Yout(Out
);
282 return std::error_code();