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) \
27 { #X, wasm::WASM_SYMBOL_TYPE_##X }
28 ENUM_ENTRY(FUNCTION), ENUM_ENTRY(DATA), ENUM_ENTRY(GLOBAL),
29 ENUM_ENTRY(SECTION), ENUM_ENTRY(EVENT),
33 static const EnumEntry<uint32_t> WasmSectionTypes[] = {
34 #define ENUM_ENTRY(X) \
35 { #X, wasm::WASM_SEC_##X }
36 ENUM_ENTRY(CUSTOM), ENUM_ENTRY(TYPE), ENUM_ENTRY(IMPORT),
37 ENUM_ENTRY(FUNCTION), ENUM_ENTRY(TABLE), ENUM_ENTRY(MEMORY),
38 ENUM_ENTRY(GLOBAL), ENUM_ENTRY(EVENT), ENUM_ENTRY(EXPORT),
39 ENUM_ENTRY(START), ENUM_ENTRY(ELEM), ENUM_ENTRY(CODE),
44 class WasmDumper : public ObjDumper {
46 WasmDumper(const WasmObjectFile *Obj, ScopedPrinter &Writer)
47 : ObjDumper(Writer), Obj(Obj) {}
49 void printFileHeaders() override;
50 void printSectionHeaders() override;
51 void printRelocations() override;
52 void printSymbols() override;
53 void printDynamicSymbols() override { llvm_unreachable("unimplemented"); }
54 void printUnwindInfo() override { llvm_unreachable("unimplemented"); }
55 void printStackMap() const override { llvm_unreachable("unimplemented"); }
58 void printSymbol(const SymbolRef &Sym);
59 void printRelocation(const SectionRef &Section, const RelocationRef &Reloc);
62 const WasmObjectFile *Obj;
65 void WasmDumper::printFileHeaders() {
66 W.printHex("Version", Obj->getHeader().Version);
69 void WasmDumper::printRelocation(const SectionRef &Section,
70 const RelocationRef &Reloc) {
71 SmallString<64> RelocTypeName;
72 uint64_t RelocType = Reloc.getType();
73 Reloc.getTypeName(RelocTypeName);
74 const wasm::WasmRelocation &WasmReloc = Obj->getWasmRelocation(Reloc);
77 symbol_iterator SI = Reloc.getSymbol();
78 if (SI != Obj->symbol_end())
79 SymName = error(SI->getName());
81 bool HasAddend = false;
83 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
84 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
85 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
86 case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
87 case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32:
93 if (opts::ExpandRelocs) {
94 DictScope Group(W, "Relocation");
95 W.printNumber("Type", RelocTypeName, RelocType);
96 W.printHex("Offset", Reloc.getOffset());
98 W.printString("Symbol", SymName);
100 W.printHex("Index", WasmReloc.Index);
102 W.printNumber("Addend", WasmReloc.Addend);
104 raw_ostream &OS = W.startLine();
105 OS << W.hex(Reloc.getOffset()) << " " << RelocTypeName << " ";
106 if (!SymName.empty())
109 OS << WasmReloc.Index;
111 OS << " " << WasmReloc.Addend;
116 void WasmDumper::printRelocations() {
117 ListScope D(W, "Relocations");
119 int SectionNumber = 0;
120 for (const SectionRef &Section : Obj->sections()) {
121 bool PrintedGroup = false;
123 error(Section.getName(Name));
126 for (const RelocationRef &Reloc : Section.relocations()) {
128 W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
133 printRelocation(Section, Reloc);
138 W.startLine() << "}\n";
143 void WasmDumper::printSymbols() {
144 ListScope Group(W, "Symbols");
146 for (const SymbolRef &Symbol : Obj->symbols())
150 void WasmDumper::printSectionHeaders() {
151 ListScope Group(W, "Sections");
152 for (const SectionRef &Section : Obj->sections()) {
153 const WasmSection &WasmSec = Obj->getWasmSection(Section);
154 DictScope SectionD(W, "Section");
155 W.printEnum("Type", WasmSec.Type, makeArrayRef(WasmSectionTypes));
156 W.printNumber("Size", static_cast<uint64_t>(WasmSec.Content.size()));
157 W.printNumber("Offset", WasmSec.Offset);
158 switch (WasmSec.Type) {
159 case wasm::WASM_SEC_CUSTOM:
160 W.printString("Name", WasmSec.Name);
161 if (WasmSec.Name == "linking") {
162 const wasm::WasmLinkingData &LinkingData = Obj->linkingData();
163 if (!LinkingData.InitFunctions.empty()) {
164 ListScope Group(W, "InitFunctions");
165 for (const wasm::WasmInitFunc &F : LinkingData.InitFunctions)
166 W.startLine() << F.Symbol << " (priority=" << F.Priority << ")\n";
170 case wasm::WASM_SEC_DATA: {
171 ListScope Group(W, "Segments");
172 for (const WasmSegment &Segment : Obj->dataSegments()) {
173 const wasm::WasmDataSegment &Seg = Segment.Data;
174 DictScope Group(W, "Segment");
175 if (!Seg.Name.empty())
176 W.printString("Name", Seg.Name);
177 W.printNumber("Size", static_cast<uint64_t>(Seg.Content.size()));
178 if (Seg.Offset.Opcode == wasm::WASM_OPCODE_I32_CONST)
179 W.printNumber("Offset", Seg.Offset.Value.Int32);
183 case wasm::WASM_SEC_MEMORY:
184 ListScope Group(W, "Memories");
185 for (const wasm::WasmLimits &Memory : Obj->memories()) {
186 DictScope Group(W, "Memory");
187 W.printNumber("InitialPages", Memory.Initial);
188 if (Memory.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) {
189 W.printNumber("MaxPages", WasmSec.Offset);
195 if (opts::SectionRelocations) {
196 ListScope D(W, "Relocations");
197 for (const RelocationRef &Reloc : Section.relocations())
198 printRelocation(Section, Reloc);
201 if (opts::SectionData) {
202 W.printBinaryBlock("SectionData", WasmSec.Content);
207 void WasmDumper::printSymbol(const SymbolRef &Sym) {
208 DictScope D(W, "Symbol");
209 WasmSymbol Symbol = Obj->getWasmSymbol(Sym.getRawDataRefImpl());
210 W.printString("Name", Symbol.Info.Name);
211 W.printEnum("Type", Symbol.Info.Kind, makeArrayRef(WasmSymbolTypes));
212 W.printHex("Flags", Symbol.Info.Flags);
219 std::error_code createWasmDumper(const object::ObjectFile *Obj,
220 ScopedPrinter &Writer,
221 std::unique_ptr<ObjDumper> &Result) {
222 const WasmObjectFile *WasmObj = dyn_cast<WasmObjectFile>(Obj);
223 assert(WasmObj && "createWasmDumper called with non-wasm object");
225 Result.reset(new WasmDumper(WasmObj, Writer));
226 return readobj_error::success;