1 //===-- WasmDumper.cpp - Wasm-specific object file dumper -----------------===//
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 //===----------------------------------------------------------------------===//
10 // This file implements the Wasm-specific dumper for llvm-readobj.
12 //===----------------------------------------------------------------------===//
15 #include "ObjDumper.h"
16 #include "llvm-readobj.h"
17 #include "llvm/Object/Wasm.h"
18 #include "llvm/Support/ScopedPrinter.h"
21 using namespace object
;
25 static const EnumEntry
<unsigned> WasmSymbolTypes
[] = {
26 #define ENUM_ENTRY(X) { #X, static_cast<unsigned>(WasmSymbol::SymbolType::X) }
27 ENUM_ENTRY(FUNCTION_IMPORT
),
28 ENUM_ENTRY(FUNCTION_EXPORT
),
29 ENUM_ENTRY(GLOBAL_IMPORT
),
30 ENUM_ENTRY(GLOBAL_EXPORT
),
31 ENUM_ENTRY(DEBUG_FUNCTION_NAME
),
35 static const EnumEntry
<uint32_t> WasmSectionTypes
[] = {
36 #define ENUM_ENTRY(X) { #X, wasm::WASM_SEC_##X }
52 class WasmDumper
: public ObjDumper
{
54 WasmDumper(const WasmObjectFile
*Obj
, ScopedPrinter
&Writer
)
55 : ObjDumper(Writer
), Obj(Obj
) {}
57 void printFileHeaders() override
;
58 void printSections() override
;
59 void printRelocations() override
;
60 void printSymbols() override
;
61 void printDynamicSymbols() override
{ llvm_unreachable("unimplemented"); }
62 void printUnwindInfo() override
{ llvm_unreachable("unimplemented"); }
63 void printStackMap() const override
{ llvm_unreachable("unimplemented"); }
66 void printSymbol(const SymbolRef
&Sym
);
67 void printRelocation(const SectionRef
&Section
, const RelocationRef
&Reloc
);
70 const WasmObjectFile
*Obj
;
73 void WasmDumper::printFileHeaders() {
74 W
.printHex("Version", Obj
->getHeader().Version
);
77 void WasmDumper::printRelocation(const SectionRef
&Section
,
78 const RelocationRef
&Reloc
) {
79 SmallString
<64> RelocTypeName
;
80 uint64_t RelocType
= Reloc
.getType();
81 Reloc
.getTypeName(RelocTypeName
);
82 const wasm::WasmRelocation
&WasmReloc
= Obj
->getWasmRelocation(Reloc
);
84 bool HasAddend
= false;
86 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB
:
87 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB
:
88 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32
:
94 if (opts::ExpandRelocs
) {
95 DictScope
Group(W
, "Relocation");
96 W
.printNumber("Type", RelocTypeName
, RelocType
);
97 W
.printHex("Offset", Reloc
.getOffset());
98 W
.printHex("Index", WasmReloc
.Index
);
100 W
.printNumber("Addend", WasmReloc
.Addend
);
102 raw_ostream
& OS
= W
.startLine();
103 OS
<< W
.hex(Reloc
.getOffset())
104 << " " << RelocTypeName
<< "[" << WasmReloc
.Index
<< "]";
106 OS
<< " " << WasmReloc
.Addend
;
111 void WasmDumper::printRelocations() {
112 ListScope
D(W
, "Relocations");
114 int SectionNumber
= 0;
115 for (const SectionRef
&Section
: Obj
->sections()) {
116 bool PrintedGroup
= false;
118 error(Section
.getName(Name
));
121 for (const RelocationRef
&Reloc
: Section
.relocations()) {
123 W
.startLine() << "Section (" << SectionNumber
<< ") " << Name
<< " {\n";
128 printRelocation(Section
, Reloc
);
133 W
.startLine() << "}\n";
138 void WasmDumper::printSymbols() {
139 ListScope
Group(W
, "Symbols");
141 for (const SymbolRef
&Symbol
: Obj
->symbols())
145 void WasmDumper::printSections() {
146 ListScope
Group(W
, "Sections");
147 for (const SectionRef
&Section
: Obj
->sections()) {
148 const WasmSection
&WasmSec
= Obj
->getWasmSection(Section
);
149 DictScope
SectionD(W
, "Section");
150 W
.printEnum("Type", WasmSec
.Type
, makeArrayRef(WasmSectionTypes
));
151 W
.printNumber("Size", static_cast<uint64_t>(WasmSec
.Content
.size()));
152 W
.printNumber("Offset", WasmSec
.Offset
);
153 switch (WasmSec
.Type
) {
154 case wasm::WASM_SEC_CUSTOM
:
155 W
.printString("Name", WasmSec
.Name
);
156 if (WasmSec
.Name
== "linking") {
157 const wasm::WasmLinkingData
&LinkingData
= Obj
->linkingData();
158 W
.printNumber("DataSize", LinkingData
.DataSize
);
159 if (LinkingData
.DataAlignment
)
160 W
.printNumber("DataAlignment", LinkingData
.DataAlignment
);
163 case wasm::WASM_SEC_DATA
: {
164 ListScope
Group(W
, "Segments");
165 for (const WasmSegment
&Segment
: Obj
->dataSegments()) {
166 const wasm::WasmDataSegment
& Seg
= Segment
.Data
;
167 DictScope
Group(W
, "Segment");
168 if (!Seg
.Name
.empty())
169 W
.printString("Name", Seg
.Name
);
170 W
.printNumber("Size", static_cast<uint64_t>(Seg
.Content
.size()));
171 if (Seg
.Offset
.Opcode
== wasm::WASM_OPCODE_I32_CONST
)
172 W
.printNumber("Offset", Seg
.Offset
.Value
.Int32
);
176 case wasm::WASM_SEC_MEMORY
:
177 ListScope
Group(W
, "Memories");
178 for (const wasm::WasmLimits
&Memory
: Obj
->memories()) {
179 DictScope
Group(W
, "Memory");
180 W
.printNumber("InitialPages", Memory
.Initial
);
181 if (Memory
.Flags
& wasm::WASM_LIMITS_FLAG_HAS_MAX
) {
182 W
.printNumber("MaxPages", WasmSec
.Offset
);
188 if (opts::SectionRelocations
) {
189 ListScope
D(W
, "Relocations");
190 for (const RelocationRef
&Reloc
: Section
.relocations())
191 printRelocation(Section
, Reloc
);
194 if (opts::SectionData
) {
195 W
.printBinaryBlock("SectionData", WasmSec
.Content
);
200 void WasmDumper::printSymbol(const SymbolRef
&Sym
) {
201 DictScope
D(W
, "Symbol");
202 WasmSymbol Symbol
= Obj
->getWasmSymbol(Sym
.getRawDataRefImpl());
203 W
.printString("Name", Symbol
.Name
);
204 W
.printEnum("Type", static_cast<unsigned>(Symbol
.Type
), makeArrayRef(WasmSymbolTypes
));
205 W
.printHex("Flags", Symbol
.Flags
);
212 std::error_code
createWasmDumper(const object::ObjectFile
*Obj
,
213 ScopedPrinter
&Writer
,
214 std::unique_ptr
<ObjDumper
> &Result
) {
215 const WasmObjectFile
*WasmObj
= dyn_cast
<WasmObjectFile
>(Obj
);
216 assert(WasmObj
&& "createWasmDumper called with non-wasm object");
218 Result
.reset(new WasmDumper(WasmObj
, Writer
));
219 return readobj_error::success
;