blob: 738b5b5e5cc2d9447a45fbaf6eb5ef50c54db4b3 [file] [log] [blame]
Derek Schuff6d76b7b2017-01-30 23:30:52 +00001//===-- WasmDumper.cpp - Wasm-specific object file dumper -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Wasm-specific dumper for llvm-readobj.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Error.h"
15#include "ObjDumper.h"
Sam Clegg135a4b82017-04-14 19:50:44 +000016#include "llvm-readobj.h"
Derek Schuff6d76b7b2017-01-30 23:30:52 +000017#include "llvm/Object/Wasm.h"
18#include "llvm/Support/ScopedPrinter.h"
19
20using namespace llvm;
21using namespace object;
22
23namespace {
24
Sam Clegg135a4b82017-04-14 19:50:44 +000025static 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),
Sam Clegg135a4b82017-04-14 19:50:44 +000031#undef ENUM_ENTRY
32};
33
34static const EnumEntry<uint32_t> WasmSectionTypes[] = {
35#define ENUM_ENTRY(X) { #X, wasm::WASM_SEC_##X }
36 ENUM_ENTRY(CUSTOM),
37 ENUM_ENTRY(TYPE),
38 ENUM_ENTRY(IMPORT),
39 ENUM_ENTRY(FUNCTION),
40 ENUM_ENTRY(TABLE),
41 ENUM_ENTRY(MEMORY),
42 ENUM_ENTRY(GLOBAL),
43 ENUM_ENTRY(EXPORT),
44 ENUM_ENTRY(START),
45 ENUM_ENTRY(ELEM),
46 ENUM_ENTRY(CODE),
47 ENUM_ENTRY(DATA),
48#undef ENUM_ENTRY
49};
Derek Schuff6d76b7b2017-01-30 23:30:52 +000050
51class WasmDumper : public ObjDumper {
52public:
53 WasmDumper(const WasmObjectFile *Obj, ScopedPrinter &Writer)
54 : ObjDumper(Writer), Obj(Obj) {}
55
Sam Clegg135a4b82017-04-14 19:50:44 +000056 void printFileHeaders() override;
57 void printSections() override;
58 void printRelocations() override;
59 void printSymbols() override;
Derek Schuff6d76b7b2017-01-30 23:30:52 +000060 void printDynamicSymbols() override { llvm_unreachable("unimplemented"); }
61 void printUnwindInfo() override { llvm_unreachable("unimplemented"); }
62 void printStackMap() const override { llvm_unreachable("unimplemented"); }
63
Sam Clegg135a4b82017-04-14 19:50:44 +000064protected:
65 void printSymbol(const SymbolRef &Sym);
66 void printRelocation(const SectionRef &Section, const RelocationRef &Reloc);
67
Derek Schuff6d76b7b2017-01-30 23:30:52 +000068private:
69 const WasmObjectFile *Obj;
70};
Sam Clegg135a4b82017-04-14 19:50:44 +000071
72void WasmDumper::printFileHeaders() {
73 W.printHex("Version", Obj->getHeader().Version);
74}
75
76void 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);
82
Sam Clegg10545c92017-04-28 00:36:36 +000083 bool HasAddend = false;
84 switch (RelocType) {
Sam Clegg13a2e892017-09-01 17:32:01 +000085 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
86 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
87 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
Sam Clegg10545c92017-04-28 00:36:36 +000088 HasAddend = true;
89 break;
90 default:
91 break;
92 }
Sam Clegg135a4b82017-04-14 19:50:44 +000093 if (opts::ExpandRelocs) {
94 DictScope Group(W, "Relocation");
95 W.printNumber("Type", RelocTypeName, RelocType);
96 W.printHex("Offset", Reloc.getOffset());
97 W.printHex("Index", WasmReloc.Index);
Sam Clegg10545c92017-04-28 00:36:36 +000098 if (HasAddend)
99 W.printNumber("Addend", WasmReloc.Addend);
Sam Clegg135a4b82017-04-14 19:50:44 +0000100 } else {
101 raw_ostream& OS = W.startLine();
Sam Cleggb23a2012017-12-19 00:04:41 +0000102 OS << W.hex(Reloc.getOffset()) << " " << RelocTypeName << "["
103 << WasmReloc.Index << "]";
Sam Clegg10545c92017-04-28 00:36:36 +0000104 if (HasAddend)
105 OS << " " << WasmReloc.Addend;
106 OS << "\n";
Sam Clegg135a4b82017-04-14 19:50:44 +0000107 }
108}
109
110void WasmDumper::printRelocations() {
111 ListScope D(W, "Relocations");
112
113 int SectionNumber = 0;
114 for (const SectionRef &Section : Obj->sections()) {
115 bool PrintedGroup = false;
116 StringRef Name;
117 error(Section.getName(Name));
118 ++SectionNumber;
119
120 for (const RelocationRef &Reloc : Section.relocations()) {
121 if (!PrintedGroup) {
122 W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
123 W.indent();
124 PrintedGroup = true;
125 }
126
127 printRelocation(Section, Reloc);
128 }
129
130 if (PrintedGroup) {
131 W.unindent();
132 W.startLine() << "}\n";
133 }
134 }
135}
136
137void WasmDumper::printSymbols() {
138 ListScope Group(W, "Symbols");
139
140 for (const SymbolRef &Symbol : Obj->symbols())
141 printSymbol(Symbol);
142}
143
144void WasmDumper::printSections() {
145 ListScope Group(W, "Sections");
146 for (const SectionRef &Section : Obj->sections()) {
147 const WasmSection &WasmSec = Obj->getWasmSection(Section);
148 DictScope SectionD(W, "Section");
149 W.printEnum("Type", WasmSec.Type, makeArrayRef(WasmSectionTypes));
Sam Cleggd95ed952017-09-20 19:03:35 +0000150 W.printNumber("Size", static_cast<uint64_t>(WasmSec.Content.size()));
Sam Clegg135a4b82017-04-14 19:50:44 +0000151 W.printNumber("Offset", WasmSec.Offset);
Sam Cleggff0730b2017-04-28 21:12:09 +0000152 switch (WasmSec.Type) {
153 case wasm::WASM_SEC_CUSTOM:
Sam Clegg135a4b82017-04-14 19:50:44 +0000154 W.printString("Name", WasmSec.Name);
Sam Clegg14612fb2017-07-10 20:47:12 +0000155 if (WasmSec.Name == "linking") {
156 const wasm::WasmLinkingData &LinkingData = Obj->linkingData();
157 W.printNumber("DataSize", LinkingData.DataSize);
Sam Cleggb23a2012017-12-19 00:04:41 +0000158 if (!LinkingData.InitFunctions.empty()) {
159 ListScope Group(W, "InitFunctions");
160 for (const wasm::WasmInitFunc &F: LinkingData.InitFunctions)
161 W.startLine() << F.FunctionIndex << " (priority=" << F.Priority
162 << ")\n";
163 }
Sam Clegg14612fb2017-07-10 20:47:12 +0000164 }
Sam Cleggff0730b2017-04-28 21:12:09 +0000165 break;
Sam Cleggd95ed952017-09-20 19:03:35 +0000166 case wasm::WASM_SEC_DATA: {
167 ListScope Group(W, "Segments");
168 for (const WasmSegment &Segment : Obj->dataSegments()) {
169 const wasm::WasmDataSegment& Seg = Segment.Data;
170 DictScope Group(W, "Segment");
171 if (!Seg.Name.empty())
172 W.printString("Name", Seg.Name);
173 W.printNumber("Size", static_cast<uint64_t>(Seg.Content.size()));
174 if (Seg.Offset.Opcode == wasm::WASM_OPCODE_I32_CONST)
175 W.printNumber("Offset", Seg.Offset.Value.Int32);
176 }
177 break;
178 }
Sam Cleggff0730b2017-04-28 21:12:09 +0000179 case wasm::WASM_SEC_MEMORY:
180 ListScope Group(W, "Memories");
181 for (const wasm::WasmLimits &Memory : Obj->memories()) {
182 DictScope Group(W, "Memory");
183 W.printNumber("InitialPages", Memory.Initial);
184 if (Memory.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) {
185 W.printNumber("MaxPages", WasmSec.Offset);
186 }
187 }
188 break;
Sam Clegg135a4b82017-04-14 19:50:44 +0000189 }
190
191 if (opts::SectionRelocations) {
192 ListScope D(W, "Relocations");
193 for (const RelocationRef &Reloc : Section.relocations())
194 printRelocation(Section, Reloc);
195 }
196
197 if (opts::SectionData) {
198 W.printBinaryBlock("SectionData", WasmSec.Content);
199 }
200 }
201}
202
203void WasmDumper::printSymbol(const SymbolRef &Sym) {
204 DictScope D(W, "Symbol");
205 WasmSymbol Symbol = Obj->getWasmSymbol(Sym.getRawDataRefImpl());
206 W.printString("Name", Symbol.Name);
207 W.printEnum("Type", static_cast<unsigned>(Symbol.Type), makeArrayRef(WasmSymbolTypes));
Sam Clegg933df262017-06-26 21:01:39 +0000208 W.printHex("Flags", Symbol.Flags);
Sam Clegg135a4b82017-04-14 19:50:44 +0000209}
210
Derek Schuff6d76b7b2017-01-30 23:30:52 +0000211}
212
213namespace llvm {
214
215std::error_code createWasmDumper(const object::ObjectFile *Obj,
216 ScopedPrinter &Writer,
217 std::unique_ptr<ObjDumper> &Result) {
218 const WasmObjectFile *WasmObj = dyn_cast<WasmObjectFile>(Obj);
219 assert(WasmObj && "createWasmDumper called with non-wasm object");
220
221 Result.reset(new WasmDumper(WasmObj, Writer));
222 return readobj_error::success;
223}
224
225} // namespace llvm