blob: 77711e749aa09e4dff273ba71686a90f971a3d8e [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),
31 ENUM_ENTRY(DEBUG_FUNCTION_NAME),
32#undef ENUM_ENTRY
33};
34
35static const EnumEntry<uint32_t> WasmSectionTypes[] = {
36#define ENUM_ENTRY(X) { #X, wasm::WASM_SEC_##X }
37 ENUM_ENTRY(CUSTOM),
38 ENUM_ENTRY(TYPE),
39 ENUM_ENTRY(IMPORT),
40 ENUM_ENTRY(FUNCTION),
41 ENUM_ENTRY(TABLE),
42 ENUM_ENTRY(MEMORY),
43 ENUM_ENTRY(GLOBAL),
44 ENUM_ENTRY(EXPORT),
45 ENUM_ENTRY(START),
46 ENUM_ENTRY(ELEM),
47 ENUM_ENTRY(CODE),
48 ENUM_ENTRY(DATA),
49#undef ENUM_ENTRY
50};
Derek Schuff6d76b7b2017-01-30 23:30:52 +000051
52class WasmDumper : public ObjDumper {
53public:
54 WasmDumper(const WasmObjectFile *Obj, ScopedPrinter &Writer)
55 : ObjDumper(Writer), Obj(Obj) {}
56
Sam Clegg135a4b82017-04-14 19:50:44 +000057 void printFileHeaders() override;
58 void printSections() override;
59 void printRelocations() override;
60 void printSymbols() override;
Derek Schuff6d76b7b2017-01-30 23:30:52 +000061 void printDynamicSymbols() override { llvm_unreachable("unimplemented"); }
62 void printUnwindInfo() override { llvm_unreachable("unimplemented"); }
63 void printStackMap() const override { llvm_unreachable("unimplemented"); }
64
Sam Clegg135a4b82017-04-14 19:50:44 +000065protected:
66 void printSymbol(const SymbolRef &Sym);
67 void printRelocation(const SectionRef &Section, const RelocationRef &Reloc);
68
Derek Schuff6d76b7b2017-01-30 23:30:52 +000069private:
70 const WasmObjectFile *Obj;
71};
Sam Clegg135a4b82017-04-14 19:50:44 +000072
73void WasmDumper::printFileHeaders() {
74 W.printHex("Version", Obj->getHeader().Version);
75}
76
77void WasmDumper::printRelocation(const SectionRef &Section,
78 const RelocationRef &Reloc) {
79 SmallString<64> RelocTypeName;
80 uint64_t RelocType = Reloc.getType();
81 Reloc.getTypeName(RelocTypeName);
82 const wasm::WasmRelocation &WasmReloc = Obj->getWasmRelocation(Reloc);
83
Sam Clegg10545c92017-04-28 00:36:36 +000084 bool HasAddend = false;
85 switch (RelocType) {
Sam Clegg13a2e892017-09-01 17:32:01 +000086 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
87 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
88 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
Sam Clegg10545c92017-04-28 00:36:36 +000089 HasAddend = true;
90 break;
91 default:
92 break;
93 }
Sam Clegg135a4b82017-04-14 19:50:44 +000094 if (opts::ExpandRelocs) {
95 DictScope Group(W, "Relocation");
96 W.printNumber("Type", RelocTypeName, RelocType);
97 W.printHex("Offset", Reloc.getOffset());
98 W.printHex("Index", WasmReloc.Index);
Sam Clegg10545c92017-04-28 00:36:36 +000099 if (HasAddend)
100 W.printNumber("Addend", WasmReloc.Addend);
Sam Clegg135a4b82017-04-14 19:50:44 +0000101 } else {
102 raw_ostream& OS = W.startLine();
103 OS << W.hex(Reloc.getOffset())
Sam Clegg10545c92017-04-28 00:36:36 +0000104 << " " << RelocTypeName << "[" << WasmReloc.Index << "]";
105 if (HasAddend)
106 OS << " " << WasmReloc.Addend;
107 OS << "\n";
Sam Clegg135a4b82017-04-14 19:50:44 +0000108 }
109}
110
111void WasmDumper::printRelocations() {
112 ListScope D(W, "Relocations");
113
114 int SectionNumber = 0;
115 for (const SectionRef &Section : Obj->sections()) {
116 bool PrintedGroup = false;
117 StringRef Name;
118 error(Section.getName(Name));
119 ++SectionNumber;
120
121 for (const RelocationRef &Reloc : Section.relocations()) {
122 if (!PrintedGroup) {
123 W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
124 W.indent();
125 PrintedGroup = true;
126 }
127
128 printRelocation(Section, Reloc);
129 }
130
131 if (PrintedGroup) {
132 W.unindent();
133 W.startLine() << "}\n";
134 }
135 }
136}
137
138void WasmDumper::printSymbols() {
139 ListScope Group(W, "Symbols");
140
141 for (const SymbolRef &Symbol : Obj->symbols())
142 printSymbol(Symbol);
143}
144
145void WasmDumper::printSections() {
146 ListScope Group(W, "Sections");
147 for (const SectionRef &Section : Obj->sections()) {
148 const WasmSection &WasmSec = Obj->getWasmSection(Section);
149 DictScope SectionD(W, "Section");
150 W.printEnum("Type", WasmSec.Type, makeArrayRef(WasmSectionTypes));
Sam Cleggd95ed952017-09-20 19:03:35 +0000151 W.printNumber("Size", static_cast<uint64_t>(WasmSec.Content.size()));
Sam Clegg135a4b82017-04-14 19:50:44 +0000152 W.printNumber("Offset", WasmSec.Offset);
Sam Cleggff0730b2017-04-28 21:12:09 +0000153 switch (WasmSec.Type) {
154 case wasm::WASM_SEC_CUSTOM:
Sam Clegg135a4b82017-04-14 19:50:44 +0000155 W.printString("Name", WasmSec.Name);
Sam Clegg14612fb2017-07-10 20:47:12 +0000156 if (WasmSec.Name == "linking") {
157 const wasm::WasmLinkingData &LinkingData = Obj->linkingData();
158 W.printNumber("DataSize", LinkingData.DataSize);
Sam Clegg14612fb2017-07-10 20:47:12 +0000159 }
Sam Cleggff0730b2017-04-28 21:12:09 +0000160 break;
Sam Cleggd95ed952017-09-20 19:03:35 +0000161 case wasm::WASM_SEC_DATA: {
162 ListScope Group(W, "Segments");
163 for (const WasmSegment &Segment : Obj->dataSegments()) {
164 const wasm::WasmDataSegment& Seg = Segment.Data;
165 DictScope Group(W, "Segment");
166 if (!Seg.Name.empty())
167 W.printString("Name", Seg.Name);
168 W.printNumber("Size", static_cast<uint64_t>(Seg.Content.size()));
169 if (Seg.Offset.Opcode == wasm::WASM_OPCODE_I32_CONST)
170 W.printNumber("Offset", Seg.Offset.Value.Int32);
171 }
172 break;
173 }
Sam Cleggff0730b2017-04-28 21:12:09 +0000174 case wasm::WASM_SEC_MEMORY:
175 ListScope Group(W, "Memories");
176 for (const wasm::WasmLimits &Memory : Obj->memories()) {
177 DictScope Group(W, "Memory");
178 W.printNumber("InitialPages", Memory.Initial);
179 if (Memory.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) {
180 W.printNumber("MaxPages", WasmSec.Offset);
181 }
182 }
183 break;
Sam Clegg135a4b82017-04-14 19:50:44 +0000184 }
185
186 if (opts::SectionRelocations) {
187 ListScope D(W, "Relocations");
188 for (const RelocationRef &Reloc : Section.relocations())
189 printRelocation(Section, Reloc);
190 }
191
192 if (opts::SectionData) {
193 W.printBinaryBlock("SectionData", WasmSec.Content);
194 }
195 }
196}
197
198void WasmDumper::printSymbol(const SymbolRef &Sym) {
199 DictScope D(W, "Symbol");
200 WasmSymbol Symbol = Obj->getWasmSymbol(Sym.getRawDataRefImpl());
201 W.printString("Name", Symbol.Name);
202 W.printEnum("Type", static_cast<unsigned>(Symbol.Type), makeArrayRef(WasmSymbolTypes));
Sam Clegg933df262017-06-26 21:01:39 +0000203 W.printHex("Flags", Symbol.Flags);
Sam Clegg135a4b82017-04-14 19:50:44 +0000204}
205
Derek Schuff6d76b7b2017-01-30 23:30:52 +0000206}
207
208namespace llvm {
209
210std::error_code createWasmDumper(const object::ObjectFile *Obj,
211 ScopedPrinter &Writer,
212 std::unique_ptr<ObjDumper> &Result) {
213 const WasmObjectFile *WasmObj = dyn_cast<WasmObjectFile>(Obj);
214 assert(WasmObj && "createWasmDumper called with non-wasm object");
215
216 Result.reset(new WasmDumper(WasmObj, Writer));
217 return readobj_error::success;
218}
219
220} // namespace llvm