blob: ce224836225ecfbd5818ad3cc3ce843ef623a724 [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[] = {
Sam Clegg6c899ba2018-02-23 05:08:34 +000026#define ENUM_ENTRY(X) { #X, wasm::WASM_SYMBOL_TYPE_##X }
27 ENUM_ENTRY(FUNCTION),
28 ENUM_ENTRY(DATA),
29 ENUM_ENTRY(GLOBAL),
Sam Cleggd5504a02018-04-27 00:17:21 +000030 ENUM_ENTRY(SECTION),
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 Clegg73812162018-05-01 16:35:16 +000083 StringRef SymName;
84 symbol_iterator SI = Reloc.getSymbol();
85 if (SI != Obj->symbol_end())
86 SymName = error(SI->getName());
87
Sam Clegg10545c92017-04-28 00:36:36 +000088 bool HasAddend = false;
89 switch (RelocType) {
Sam Clegg13a2e892017-09-01 17:32:01 +000090 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
91 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
92 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
Sam Clegg6a31a0d2018-04-26 19:27:28 +000093 case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
94 case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32:
Sam Clegg10545c92017-04-28 00:36:36 +000095 HasAddend = true;
96 break;
97 default:
98 break;
99 }
Sam Clegg135a4b82017-04-14 19:50:44 +0000100 if (opts::ExpandRelocs) {
101 DictScope Group(W, "Relocation");
102 W.printNumber("Type", RelocTypeName, RelocType);
103 W.printHex("Offset", Reloc.getOffset());
Sam Clegg73812162018-05-01 16:35:16 +0000104 if (!SymName.empty())
105 W.printString("Symbol", SymName);
106 else
107 W.printHex("Index", WasmReloc.Index);
Sam Clegg10545c92017-04-28 00:36:36 +0000108 if (HasAddend)
109 W.printNumber("Addend", WasmReloc.Addend);
Sam Clegg135a4b82017-04-14 19:50:44 +0000110 } else {
111 raw_ostream& OS = W.startLine();
Sam Clegg73812162018-05-01 16:35:16 +0000112 OS << W.hex(Reloc.getOffset()) << " " << RelocTypeName << " ";
113 if (!SymName.empty())
114 OS << SymName;
115 else
116 OS << WasmReloc.Index;
Sam Clegg10545c92017-04-28 00:36:36 +0000117 if (HasAddend)
118 OS << " " << WasmReloc.Addend;
119 OS << "\n";
Sam Clegg135a4b82017-04-14 19:50:44 +0000120 }
121}
122
123void WasmDumper::printRelocations() {
124 ListScope D(W, "Relocations");
125
126 int SectionNumber = 0;
127 for (const SectionRef &Section : Obj->sections()) {
128 bool PrintedGroup = false;
129 StringRef Name;
130 error(Section.getName(Name));
131 ++SectionNumber;
132
133 for (const RelocationRef &Reloc : Section.relocations()) {
134 if (!PrintedGroup) {
135 W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
136 W.indent();
137 PrintedGroup = true;
138 }
139
140 printRelocation(Section, Reloc);
141 }
142
143 if (PrintedGroup) {
144 W.unindent();
145 W.startLine() << "}\n";
146 }
147 }
148}
149
150void WasmDumper::printSymbols() {
151 ListScope Group(W, "Symbols");
152
153 for (const SymbolRef &Symbol : Obj->symbols())
154 printSymbol(Symbol);
155}
156
157void 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));
Sam Cleggd95ed952017-09-20 19:03:35 +0000163 W.printNumber("Size", static_cast<uint64_t>(WasmSec.Content.size()));
Sam Clegg135a4b82017-04-14 19:50:44 +0000164 W.printNumber("Offset", WasmSec.Offset);
Sam Cleggff0730b2017-04-28 21:12:09 +0000165 switch (WasmSec.Type) {
166 case wasm::WASM_SEC_CUSTOM:
Sam Clegg135a4b82017-04-14 19:50:44 +0000167 W.printString("Name", WasmSec.Name);
Sam Clegg14612fb2017-07-10 20:47:12 +0000168 if (WasmSec.Name == "linking") {
169 const wasm::WasmLinkingData &LinkingData = Obj->linkingData();
Sam Cleggb23a2012017-12-19 00:04:41 +0000170 if (!LinkingData.InitFunctions.empty()) {
171 ListScope Group(W, "InitFunctions");
172 for (const wasm::WasmInitFunc &F: LinkingData.InitFunctions)
Sam Clegg6c899ba2018-02-23 05:08:34 +0000173 W.startLine() << F.Symbol << " (priority=" << F.Priority << ")\n";
Sam Cleggb23a2012017-12-19 00:04:41 +0000174 }
Sam Clegg14612fb2017-07-10 20:47:12 +0000175 }
Sam Cleggff0730b2017-04-28 21:12:09 +0000176 break;
Sam Cleggd95ed952017-09-20 19:03:35 +0000177 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);
187 }
188 break;
189 }
Sam Cleggff0730b2017-04-28 21:12:09 +0000190 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);
197 }
198 }
199 break;
Sam Clegg135a4b82017-04-14 19:50:44 +0000200 }
201
202 if (opts::SectionRelocations) {
203 ListScope D(W, "Relocations");
204 for (const RelocationRef &Reloc : Section.relocations())
205 printRelocation(Section, Reloc);
206 }
207
208 if (opts::SectionData) {
209 W.printBinaryBlock("SectionData", WasmSec.Content);
210 }
211 }
212}
213
214void WasmDumper::printSymbol(const SymbolRef &Sym) {
215 DictScope D(W, "Symbol");
216 WasmSymbol Symbol = Obj->getWasmSymbol(Sym.getRawDataRefImpl());
Sam Clegg6c899ba2018-02-23 05:08:34 +0000217 W.printString("Name", Symbol.Info.Name);
218 W.printEnum("Type", Symbol.Info.Kind, makeArrayRef(WasmSymbolTypes));
219 W.printHex("Flags", Symbol.Info.Flags);
Sam Clegg135a4b82017-04-14 19:50:44 +0000220}
221
Derek Schuff6d76b7b2017-01-30 23:30:52 +0000222}
223
224namespace llvm {
225
226std::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");
231
232 Result.reset(new WasmDumper(WasmObj, Writer));
233 return readobj_error::success;
234}
235
236} // namespace llvm