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, wasm::WASM_SYMBOL_TYPE_##X }
34 static const EnumEntry
<uint32_t> WasmSectionTypes
[] = {
35 #define ENUM_ENTRY(X) { #X, wasm::WASM_SEC_##X }
51 class WasmDumper
: public ObjDumper
{
53 WasmDumper(const WasmObjectFile
*Obj
, ScopedPrinter
&Writer
)
54 : ObjDumper(Writer
), Obj(Obj
) {}
56 void printFileHeaders() override
;
57 void printSections() override
;
58 void printRelocations() override
;
59 void printSymbols() override
;
60 void printDynamicSymbols() override
{ llvm_unreachable("unimplemented"); }
61 void printUnwindInfo() override
{ llvm_unreachable("unimplemented"); }
62 void printStackMap() const override
{ llvm_unreachable("unimplemented"); }
65 void printSymbol(const SymbolRef
&Sym
);
66 void printRelocation(const SectionRef
&Section
, const RelocationRef
&Reloc
);
69 const WasmObjectFile
*Obj
;
72 void WasmDumper::printFileHeaders() {
73 W
.printHex("Version", Obj
->getHeader().Version
);
76 void WasmDumper::printRelocation(const SectionRef
&Section
,
77 const RelocationRef
&Reloc
) {
78 SmallString
<64> RelocTypeName
;
79 uint64_t RelocType
= Reloc
.getType();
80 Reloc
.getTypeName(RelocTypeName
);
81 const wasm::WasmRelocation
&WasmReloc
= Obj
->getWasmRelocation(Reloc
);
84 symbol_iterator SI
= Reloc
.getSymbol();
85 if (SI
!= Obj
->symbol_end())
86 SymName
= error(SI
->getName());
88 bool HasAddend
= false;
90 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB
:
91 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB
:
92 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32
:
93 case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32
:
94 case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32
:
100 if (opts::ExpandRelocs
) {
101 DictScope
Group(W
, "Relocation");
102 W
.printNumber("Type", RelocTypeName
, RelocType
);
103 W
.printHex("Offset", Reloc
.getOffset());
104 if (!SymName
.empty())
105 W
.printString("Symbol", SymName
);
107 W
.printHex("Index", WasmReloc
.Index
);
109 W
.printNumber("Addend", WasmReloc
.Addend
);
111 raw_ostream
& OS
= W
.startLine();
112 OS
<< W
.hex(Reloc
.getOffset()) << " " << RelocTypeName
<< " ";
113 if (!SymName
.empty())
116 OS
<< WasmReloc
.Index
;
118 OS
<< " " << WasmReloc
.Addend
;
123 void WasmDumper::printRelocations() {
124 ListScope
D(W
, "Relocations");
126 int SectionNumber
= 0;
127 for (const SectionRef
&Section
: Obj
->sections()) {
128 bool PrintedGroup
= false;
130 error(Section
.getName(Name
));
133 for (const RelocationRef
&Reloc
: Section
.relocations()) {
135 W
.startLine() << "Section (" << SectionNumber
<< ") " << Name
<< " {\n";
140 printRelocation(Section
, Reloc
);
145 W
.startLine() << "}\n";
150 void WasmDumper::printSymbols() {
151 ListScope
Group(W
, "Symbols");
153 for (const SymbolRef
&Symbol
: Obj
->symbols())
157 void WasmDumper::printSections() {
158 ListScope
Group(W
, "Sections");
159 for (const SectionRef
&Section
: Obj
->sections()) {
160 const WasmSection
&WasmSec
= Obj
->getWasmSection(Section
);
161 DictScope
SectionD(W
, "Section");
162 W
.printEnum("Type", WasmSec
.Type
, makeArrayRef(WasmSectionTypes
));
163 W
.printNumber("Size", static_cast<uint64_t>(WasmSec
.Content
.size()));
164 W
.printNumber("Offset", WasmSec
.Offset
);
165 switch (WasmSec
.Type
) {
166 case wasm::WASM_SEC_CUSTOM
:
167 W
.printString("Name", WasmSec
.Name
);
168 if (WasmSec
.Name
== "linking") {
169 const wasm::WasmLinkingData
&LinkingData
= Obj
->linkingData();
170 if (!LinkingData
.InitFunctions
.empty()) {
171 ListScope
Group(W
, "InitFunctions");
172 for (const wasm::WasmInitFunc
&F
: LinkingData
.InitFunctions
)
173 W
.startLine() << F
.Symbol
<< " (priority=" << F
.Priority
<< ")\n";
177 case wasm::WASM_SEC_DATA
: {
178 ListScope
Group(W
, "Segments");
179 for (const WasmSegment
&Segment
: Obj
->dataSegments()) {
180 const wasm::WasmDataSegment
& Seg
= Segment
.Data
;
181 DictScope
Group(W
, "Segment");
182 if (!Seg
.Name
.empty())
183 W
.printString("Name", Seg
.Name
);
184 W
.printNumber("Size", static_cast<uint64_t>(Seg
.Content
.size()));
185 if (Seg
.Offset
.Opcode
== wasm::WASM_OPCODE_I32_CONST
)
186 W
.printNumber("Offset", Seg
.Offset
.Value
.Int32
);
190 case wasm::WASM_SEC_MEMORY
:
191 ListScope
Group(W
, "Memories");
192 for (const wasm::WasmLimits
&Memory
: Obj
->memories()) {
193 DictScope
Group(W
, "Memory");
194 W
.printNumber("InitialPages", Memory
.Initial
);
195 if (Memory
.Flags
& wasm::WASM_LIMITS_FLAG_HAS_MAX
) {
196 W
.printNumber("MaxPages", WasmSec
.Offset
);
202 if (opts::SectionRelocations
) {
203 ListScope
D(W
, "Relocations");
204 for (const RelocationRef
&Reloc
: Section
.relocations())
205 printRelocation(Section
, Reloc
);
208 if (opts::SectionData
) {
209 W
.printBinaryBlock("SectionData", WasmSec
.Content
);
214 void WasmDumper::printSymbol(const SymbolRef
&Sym
) {
215 DictScope
D(W
, "Symbol");
216 WasmSymbol Symbol
= Obj
->getWasmSymbol(Sym
.getRawDataRefImpl());
217 W
.printString("Name", Symbol
.Info
.Name
);
218 W
.printEnum("Type", Symbol
.Info
.Kind
, makeArrayRef(WasmSymbolTypes
));
219 W
.printHex("Flags", Symbol
.Info
.Flags
);
226 std::error_code
createWasmDumper(const object::ObjectFile
*Obj
,
227 ScopedPrinter
&Writer
,
228 std::unique_ptr
<ObjDumper
> &Result
) {
229 const WasmObjectFile
*WasmObj
= dyn_cast
<WasmObjectFile
>(Obj
);
230 assert(WasmObj
&& "createWasmDumper called with non-wasm object");
232 Result
.reset(new WasmDumper(WasmObj
, Writer
));
233 return readobj_error::success
;