blob: 6f13bcda3edcaeef261b4040148dc8529e8744f4 [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[] = {
Heejin Ahnf208f632018-09-05 01:27:38 +000026#define ENUM_ENTRY(X) \
27 { #X, wasm::WASM_SYMBOL_TYPE_##X }
28 ENUM_ENTRY(FUNCTION),
29 ENUM_ENTRY(DATA),
30 ENUM_ENTRY(GLOBAL),
31 ENUM_ENTRY(SECTION),
Sam Clegg135a4b82017-04-14 19:50:44 +000032#undef ENUM_ENTRY
33};
34
35static const EnumEntry<uint32_t> WasmSectionTypes[] = {
Heejin Ahnf208f632018-09-05 01:27:38 +000036#define ENUM_ENTRY(X) \
37 { #X, wasm::WASM_SEC_##X }
38 ENUM_ENTRY(CUSTOM), ENUM_ENTRY(TYPE), ENUM_ENTRY(IMPORT),
39 ENUM_ENTRY(FUNCTION), ENUM_ENTRY(TABLE), ENUM_ENTRY(MEMORY),
40 ENUM_ENTRY(GLOBAL), ENUM_ENTRY(EXPORT), ENUM_ENTRY(START),
41 ENUM_ENTRY(ELEM), ENUM_ENTRY(CODE), ENUM_ENTRY(DATA),
Sam Clegg135a4b82017-04-14 19:50:44 +000042#undef ENUM_ENTRY
43};
Derek Schuff6d76b7b2017-01-30 23:30:52 +000044
45class WasmDumper : public ObjDumper {
46public:
47 WasmDumper(const WasmObjectFile *Obj, ScopedPrinter &Writer)
48 : ObjDumper(Writer), Obj(Obj) {}
49
Sam Clegg135a4b82017-04-14 19:50:44 +000050 void printFileHeaders() override;
51 void printSections() override;
52 void printRelocations() override;
53 void printSymbols() override;
Derek Schuff6d76b7b2017-01-30 23:30:52 +000054 void printDynamicSymbols() override { llvm_unreachable("unimplemented"); }
55 void printUnwindInfo() override { llvm_unreachable("unimplemented"); }
56 void printStackMap() const override { llvm_unreachable("unimplemented"); }
57
Sam Clegg135a4b82017-04-14 19:50:44 +000058protected:
59 void printSymbol(const SymbolRef &Sym);
60 void printRelocation(const SectionRef &Section, const RelocationRef &Reloc);
61
Derek Schuff6d76b7b2017-01-30 23:30:52 +000062private:
63 const WasmObjectFile *Obj;
64};
Sam Clegg135a4b82017-04-14 19:50:44 +000065
66void WasmDumper::printFileHeaders() {
67 W.printHex("Version", Obj->getHeader().Version);
68}
69
70void WasmDumper::printRelocation(const SectionRef &Section,
71 const RelocationRef &Reloc) {
72 SmallString<64> RelocTypeName;
73 uint64_t RelocType = Reloc.getType();
74 Reloc.getTypeName(RelocTypeName);
75 const wasm::WasmRelocation &WasmReloc = Obj->getWasmRelocation(Reloc);
76
Sam Clegg73812162018-05-01 16:35:16 +000077 StringRef SymName;
78 symbol_iterator SI = Reloc.getSymbol();
79 if (SI != Obj->symbol_end())
80 SymName = error(SI->getName());
81
Sam Clegg10545c92017-04-28 00:36:36 +000082 bool HasAddend = false;
83 switch (RelocType) {
Sam Clegg13a2e892017-09-01 17:32:01 +000084 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
85 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
86 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
Sam Clegg6a31a0d2018-04-26 19:27:28 +000087 case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
88 case wasm::R_WEBASSEMBLY_SECTION_OFFSET_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());
Sam Clegg73812162018-05-01 16:35:16 +000098 if (!SymName.empty())
99 W.printString("Symbol", SymName);
100 else
101 W.printHex("Index", WasmReloc.Index);
Sam Clegg10545c92017-04-28 00:36:36 +0000102 if (HasAddend)
103 W.printNumber("Addend", WasmReloc.Addend);
Sam Clegg135a4b82017-04-14 19:50:44 +0000104 } else {
Heejin Ahnf208f632018-09-05 01:27:38 +0000105 raw_ostream &OS = W.startLine();
Sam Clegg73812162018-05-01 16:35:16 +0000106 OS << W.hex(Reloc.getOffset()) << " " << RelocTypeName << " ";
107 if (!SymName.empty())
108 OS << SymName;
109 else
110 OS << WasmReloc.Index;
Sam Clegg10545c92017-04-28 00:36:36 +0000111 if (HasAddend)
112 OS << " " << WasmReloc.Addend;
113 OS << "\n";
Sam Clegg135a4b82017-04-14 19:50:44 +0000114 }
115}
116
117void WasmDumper::printRelocations() {
118 ListScope D(W, "Relocations");
119
120 int SectionNumber = 0;
121 for (const SectionRef &Section : Obj->sections()) {
122 bool PrintedGroup = false;
123 StringRef Name;
124 error(Section.getName(Name));
125 ++SectionNumber;
126
127 for (const RelocationRef &Reloc : Section.relocations()) {
128 if (!PrintedGroup) {
129 W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
130 W.indent();
131 PrintedGroup = true;
132 }
133
134 printRelocation(Section, Reloc);
135 }
136
137 if (PrintedGroup) {
138 W.unindent();
139 W.startLine() << "}\n";
140 }
141 }
142}
143
144void WasmDumper::printSymbols() {
145 ListScope Group(W, "Symbols");
146
147 for (const SymbolRef &Symbol : Obj->symbols())
148 printSymbol(Symbol);
149}
150
151void WasmDumper::printSections() {
152 ListScope Group(W, "Sections");
153 for (const SectionRef &Section : Obj->sections()) {
154 const WasmSection &WasmSec = Obj->getWasmSection(Section);
155 DictScope SectionD(W, "Section");
156 W.printEnum("Type", WasmSec.Type, makeArrayRef(WasmSectionTypes));
Sam Cleggd95ed952017-09-20 19:03:35 +0000157 W.printNumber("Size", static_cast<uint64_t>(WasmSec.Content.size()));
Sam Clegg135a4b82017-04-14 19:50:44 +0000158 W.printNumber("Offset", WasmSec.Offset);
Sam Cleggff0730b2017-04-28 21:12:09 +0000159 switch (WasmSec.Type) {
160 case wasm::WASM_SEC_CUSTOM:
Sam Clegg135a4b82017-04-14 19:50:44 +0000161 W.printString("Name", WasmSec.Name);
Sam Clegg14612fb2017-07-10 20:47:12 +0000162 if (WasmSec.Name == "linking") {
163 const wasm::WasmLinkingData &LinkingData = Obj->linkingData();
Sam Cleggb23a2012017-12-19 00:04:41 +0000164 if (!LinkingData.InitFunctions.empty()) {
165 ListScope Group(W, "InitFunctions");
Heejin Ahnf208f632018-09-05 01:27:38 +0000166 for (const wasm::WasmInitFunc &F : LinkingData.InitFunctions)
Sam Clegg6c899ba2018-02-23 05:08:34 +0000167 W.startLine() << F.Symbol << " (priority=" << F.Priority << ")\n";
Sam Cleggb23a2012017-12-19 00:04:41 +0000168 }
Sam Clegg14612fb2017-07-10 20:47:12 +0000169 }
Sam Cleggff0730b2017-04-28 21:12:09 +0000170 break;
Sam Cleggd95ed952017-09-20 19:03:35 +0000171 case wasm::WASM_SEC_DATA: {
172 ListScope Group(W, "Segments");
173 for (const WasmSegment &Segment : Obj->dataSegments()) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000174 const wasm::WasmDataSegment &Seg = Segment.Data;
Sam Cleggd95ed952017-09-20 19:03:35 +0000175 DictScope Group(W, "Segment");
176 if (!Seg.Name.empty())
177 W.printString("Name", Seg.Name);
178 W.printNumber("Size", static_cast<uint64_t>(Seg.Content.size()));
179 if (Seg.Offset.Opcode == wasm::WASM_OPCODE_I32_CONST)
180 W.printNumber("Offset", Seg.Offset.Value.Int32);
181 }
182 break;
183 }
Sam Cleggff0730b2017-04-28 21:12:09 +0000184 case wasm::WASM_SEC_MEMORY:
185 ListScope Group(W, "Memories");
186 for (const wasm::WasmLimits &Memory : Obj->memories()) {
187 DictScope Group(W, "Memory");
188 W.printNumber("InitialPages", Memory.Initial);
189 if (Memory.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) {
190 W.printNumber("MaxPages", WasmSec.Offset);
191 }
192 }
193 break;
Sam Clegg135a4b82017-04-14 19:50:44 +0000194 }
195
196 if (opts::SectionRelocations) {
197 ListScope D(W, "Relocations");
198 for (const RelocationRef &Reloc : Section.relocations())
199 printRelocation(Section, Reloc);
200 }
201
202 if (opts::SectionData) {
203 W.printBinaryBlock("SectionData", WasmSec.Content);
204 }
205 }
206}
207
208void WasmDumper::printSymbol(const SymbolRef &Sym) {
209 DictScope D(W, "Symbol");
210 WasmSymbol Symbol = Obj->getWasmSymbol(Sym.getRawDataRefImpl());
Sam Clegg6c899ba2018-02-23 05:08:34 +0000211 W.printString("Name", Symbol.Info.Name);
212 W.printEnum("Type", Symbol.Info.Kind, makeArrayRef(WasmSymbolTypes));
213 W.printHex("Flags", Symbol.Info.Flags);
Sam Clegg135a4b82017-04-14 19:50:44 +0000214}
215
Heejin Ahnf208f632018-09-05 01:27:38 +0000216} // namespace
Derek Schuff6d76b7b2017-01-30 23:30:52 +0000217
218namespace llvm {
219
220std::error_code createWasmDumper(const object::ObjectFile *Obj,
221 ScopedPrinter &Writer,
222 std::unique_ptr<ObjDumper> &Result) {
223 const WasmObjectFile *WasmObj = dyn_cast<WasmObjectFile>(Obj);
224 assert(WasmObj && "createWasmDumper called with non-wasm object");
225
226 Result.reset(new WasmDumper(WasmObj, Writer));
227 return readobj_error::success;
228}
229
230} // namespace llvm