1 //===-- WasmDumper.cpp - Wasm-specific object file dumper -----------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the Wasm-specific dumper for llvm-readobj.
11 //===----------------------------------------------------------------------===//
14 #include "ObjDumper.h"
15 #include "llvm-readobj.h"
16 #include "llvm/Object/Wasm.h"
17 #include "llvm/Support/ScopedPrinter.h"
20 using namespace object
;
24 static const EnumEntry
<unsigned> WasmSymbolTypes
[] = {
25 #define ENUM_ENTRY(X) \
26 { #X, wasm::WASM_SYMBOL_TYPE_##X }
27 ENUM_ENTRY(FUNCTION
), ENUM_ENTRY(DATA
), ENUM_ENTRY(GLOBAL
),
28 ENUM_ENTRY(SECTION
), ENUM_ENTRY(EVENT
),
32 static const EnumEntry
<uint32_t> WasmSectionTypes
[] = {
33 #define ENUM_ENTRY(X) \
34 { #X, wasm::WASM_SEC_##X }
35 ENUM_ENTRY(CUSTOM
), ENUM_ENTRY(TYPE
), ENUM_ENTRY(IMPORT
),
36 ENUM_ENTRY(FUNCTION
), ENUM_ENTRY(TABLE
), ENUM_ENTRY(MEMORY
),
37 ENUM_ENTRY(GLOBAL
), ENUM_ENTRY(EVENT
), ENUM_ENTRY(EXPORT
),
38 ENUM_ENTRY(START
), ENUM_ENTRY(ELEM
), ENUM_ENTRY(CODE
),
39 ENUM_ENTRY(DATA
), ENUM_ENTRY(DATACOUNT
),
43 static const EnumEntry
<unsigned> WasmSymbolFlags
[] = {
44 #define ENUM_ENTRY(X) \
45 { #X, wasm::WASM_SYMBOL_##X }
46 ENUM_ENTRY(BINDING_GLOBAL
),
47 ENUM_ENTRY(BINDING_WEAK
),
48 ENUM_ENTRY(BINDING_LOCAL
),
49 ENUM_ENTRY(VISIBILITY_DEFAULT
),
50 ENUM_ENTRY(VISIBILITY_HIDDEN
),
51 ENUM_ENTRY(UNDEFINED
),
53 ENUM_ENTRY(EXPLICIT_NAME
),
57 class WasmDumper
: public ObjDumper
{
59 WasmDumper(const WasmObjectFile
*Obj
, ScopedPrinter
&Writer
)
60 : ObjDumper(Writer
), Obj(Obj
) {}
62 void printFileHeaders() override
;
63 void printSectionHeaders() override
;
64 void printRelocations() override
;
65 void printUnwindInfo() override
{ llvm_unreachable("unimplemented"); }
66 void printStackMap() const override
{ llvm_unreachable("unimplemented"); }
69 void printSymbol(const SymbolRef
&Sym
);
70 void printRelocation(const SectionRef
&Section
, const RelocationRef
&Reloc
);
73 void printSymbols() override
;
74 void printDynamicSymbols() override
{ llvm_unreachable("unimplemented"); }
76 const WasmObjectFile
*Obj
;
79 void WasmDumper::printFileHeaders() {
80 W
.printHex("Version", Obj
->getHeader().Version
);
83 void WasmDumper::printRelocation(const SectionRef
&Section
,
84 const RelocationRef
&Reloc
) {
85 SmallString
<64> RelocTypeName
;
86 uint64_t RelocType
= Reloc
.getType();
87 Reloc
.getTypeName(RelocTypeName
);
88 const wasm::WasmRelocation
&WasmReloc
= Obj
->getWasmRelocation(Reloc
);
91 symbol_iterator SI
= Reloc
.getSymbol();
92 if (SI
!= Obj
->symbol_end())
93 SymName
= unwrapOrError(Obj
->getFileName(), SI
->getName());
95 bool HasAddend
= false;
97 case wasm::R_WASM_MEMORY_ADDR_LEB
:
98 case wasm::R_WASM_MEMORY_ADDR_SLEB
:
99 case wasm::R_WASM_MEMORY_ADDR_I32
:
100 case wasm::R_WASM_FUNCTION_OFFSET_I32
:
101 case wasm::R_WASM_SECTION_OFFSET_I32
:
107 if (opts::ExpandRelocs
) {
108 DictScope
Group(W
, "Relocation");
109 W
.printNumber("Type", RelocTypeName
, RelocType
);
110 W
.printHex("Offset", Reloc
.getOffset());
111 if (!SymName
.empty())
112 W
.printString("Symbol", SymName
);
114 W
.printHex("Index", WasmReloc
.Index
);
116 W
.printNumber("Addend", WasmReloc
.Addend
);
118 raw_ostream
&OS
= W
.startLine();
119 OS
<< W
.hex(Reloc
.getOffset()) << " " << RelocTypeName
<< " ";
120 if (!SymName
.empty())
123 OS
<< WasmReloc
.Index
;
125 OS
<< " " << WasmReloc
.Addend
;
130 void WasmDumper::printRelocations() {
131 ListScope
D(W
, "Relocations");
133 int SectionNumber
= 0;
134 for (const SectionRef
&Section
: Obj
->sections()) {
135 bool PrintedGroup
= false;
137 error(Section
.getName(Name
));
140 for (const RelocationRef
&Reloc
: Section
.relocations()) {
142 W
.startLine() << "Section (" << SectionNumber
<< ") " << Name
<< " {\n";
147 printRelocation(Section
, Reloc
);
152 W
.startLine() << "}\n";
157 void WasmDumper::printSymbols() {
158 ListScope
Group(W
, "Symbols");
160 for (const SymbolRef
&Symbol
: Obj
->symbols())
164 void WasmDumper::printSectionHeaders() {
165 ListScope
Group(W
, "Sections");
166 for (const SectionRef
&Section
: Obj
->sections()) {
167 const WasmSection
&WasmSec
= Obj
->getWasmSection(Section
);
168 DictScope
SectionD(W
, "Section");
169 W
.printEnum("Type", WasmSec
.Type
, makeArrayRef(WasmSectionTypes
));
170 W
.printNumber("Size", static_cast<uint64_t>(WasmSec
.Content
.size()));
171 W
.printNumber("Offset", WasmSec
.Offset
);
172 switch (WasmSec
.Type
) {
173 case wasm::WASM_SEC_CUSTOM
:
174 W
.printString("Name", WasmSec
.Name
);
175 if (WasmSec
.Name
== "linking") {
176 const wasm::WasmLinkingData
&LinkingData
= Obj
->linkingData();
177 if (!LinkingData
.InitFunctions
.empty()) {
178 ListScope
Group(W
, "InitFunctions");
179 for (const wasm::WasmInitFunc
&F
: LinkingData
.InitFunctions
)
180 W
.startLine() << F
.Symbol
<< " (priority=" << F
.Priority
<< ")\n";
184 case wasm::WASM_SEC_DATA
: {
185 ListScope
Group(W
, "Segments");
186 for (const WasmSegment
&Segment
: Obj
->dataSegments()) {
187 const wasm::WasmDataSegment
&Seg
= Segment
.Data
;
188 DictScope
Group(W
, "Segment");
189 if (!Seg
.Name
.empty())
190 W
.printString("Name", Seg
.Name
);
191 W
.printNumber("Size", static_cast<uint64_t>(Seg
.Content
.size()));
192 if (Seg
.Offset
.Opcode
== wasm::WASM_OPCODE_I32_CONST
)
193 W
.printNumber("Offset", Seg
.Offset
.Value
.Int32
);
197 case wasm::WASM_SEC_MEMORY
:
198 ListScope
Group(W
, "Memories");
199 for (const wasm::WasmLimits
&Memory
: Obj
->memories()) {
200 DictScope
Group(W
, "Memory");
201 W
.printNumber("InitialPages", Memory
.Initial
);
202 if (Memory
.Flags
& wasm::WASM_LIMITS_FLAG_HAS_MAX
) {
203 W
.printNumber("MaxPages", WasmSec
.Offset
);
209 if (opts::SectionRelocations
) {
210 ListScope
D(W
, "Relocations");
211 for (const RelocationRef
&Reloc
: Section
.relocations())
212 printRelocation(Section
, Reloc
);
215 if (opts::SectionData
) {
216 W
.printBinaryBlock("SectionData", WasmSec
.Content
);
221 void WasmDumper::printSymbol(const SymbolRef
&Sym
) {
222 DictScope
D(W
, "Symbol");
223 WasmSymbol Symbol
= Obj
->getWasmSymbol(Sym
.getRawDataRefImpl());
224 W
.printString("Name", Symbol
.Info
.Name
);
225 W
.printEnum("Type", Symbol
.Info
.Kind
, makeArrayRef(WasmSymbolTypes
));
226 W
.printFlags("Flags", Symbol
.Info
.Flags
, makeArrayRef(WasmSymbolFlags
));
228 if (Symbol
.Info
.Flags
& wasm::WASM_SYMBOL_UNDEFINED
) {
229 W
.printString("ImportName", Symbol
.Info
.ImportName
);
230 W
.printString("ImportModule", Symbol
.Info
.ImportModule
);
232 if (Symbol
.Info
.Kind
!= wasm::WASM_SYMBOL_TYPE_DATA
) {
233 W
.printHex("ElementIndex", Symbol
.Info
.ElementIndex
);
234 } else if (!(Symbol
.Info
.Flags
& wasm::WASM_SYMBOL_UNDEFINED
)) {
235 W
.printHex("Offset", Symbol
.Info
.DataRef
.Offset
);
236 W
.printHex("Segment", Symbol
.Info
.DataRef
.Segment
);
237 W
.printHex("Size", Symbol
.Info
.DataRef
.Size
);
245 std::error_code
createWasmDumper(const object::ObjectFile
*Obj
,
246 ScopedPrinter
&Writer
,
247 std::unique_ptr
<ObjDumper
> &Result
) {
248 const auto *WasmObj
= dyn_cast
<WasmObjectFile
>(Obj
);
249 assert(WasmObj
&& "createWasmDumper called with non-wasm object");
251 Result
.reset(new WasmDumper(WasmObj
, Writer
));
252 return readobj_error::success
;