blob: 79d3db4e2d291659bc7c92529118062c5492ed3c [file] [log] [blame]
Derek Schuffc20099f2017-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 Clegga70c36a2017-04-14 19:50:44 +000016#include "llvm-readobj.h"
Derek Schuffc20099f2017-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 Clegga70c36a2017-04-14 19:50:44 +000025static const EnumEntry<unsigned> WasmSymbolTypes[] = {
Heejin Ahn581d2312018-09-05 01:27:38 +000026#define ENUM_ENTRY(X) \
27 { #X, wasm::WASM_SYMBOL_TYPE_##X }
Heejin Ahn8bcdd042018-11-14 02:46:21 +000028 ENUM_ENTRY(FUNCTION), ENUM_ENTRY(DATA), ENUM_ENTRY(GLOBAL),
29 ENUM_ENTRY(SECTION), ENUM_ENTRY(EVENT),
Sam Clegga70c36a2017-04-14 19:50:44 +000030#undef ENUM_ENTRY
31};
32
33static const EnumEntry<uint32_t> WasmSectionTypes[] = {
Heejin Ahn581d2312018-09-05 01:27:38 +000034#define ENUM_ENTRY(X) \
35 { #X, wasm::WASM_SEC_##X }
Heejin Ahn8bcdd042018-11-14 02:46:21 +000036 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),
40 ENUM_ENTRY(DATA),
Sam Clegga70c36a2017-04-14 19:50:44 +000041#undef ENUM_ENTRY
42};
Derek Schuffc20099f2017-01-30 23:30:52 +000043
44class WasmDumper : public ObjDumper {
45public:
46 WasmDumper(const WasmObjectFile *Obj, ScopedPrinter &Writer)
47 : ObjDumper(Writer), Obj(Obj) {}
48
Sam Clegga70c36a2017-04-14 19:50:44 +000049 void printFileHeaders() override;
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +000050 void printSectionHeaders() override;
Sam Clegga70c36a2017-04-14 19:50:44 +000051 void printRelocations() override;
52 void printSymbols() override;
Derek Schuffc20099f2017-01-30 23:30:52 +000053 void printDynamicSymbols() override { llvm_unreachable("unimplemented"); }
54 void printUnwindInfo() override { llvm_unreachable("unimplemented"); }
55 void printStackMap() const override { llvm_unreachable("unimplemented"); }
56
Sam Clegga70c36a2017-04-14 19:50:44 +000057protected:
58 void printSymbol(const SymbolRef &Sym);
59 void printRelocation(const SectionRef &Section, const RelocationRef &Reloc);
60
Derek Schuffc20099f2017-01-30 23:30:52 +000061private:
62 const WasmObjectFile *Obj;
63};
Sam Clegga70c36a2017-04-14 19:50:44 +000064
65void WasmDumper::printFileHeaders() {
66 W.printHex("Version", Obj->getHeader().Version);
67}
68
69void 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);
75
Sam Cleggedd76942018-05-01 16:35:16 +000076 StringRef SymName;
77 symbol_iterator SI = Reloc.getSymbol();
78 if (SI != Obj->symbol_end())
79 SymName = error(SI->getName());
80
Sam Clegg4275ee92017-04-28 00:36:36 +000081 bool HasAddend = false;
82 switch (RelocType) {
Sam Clegg8b020d72017-09-01 17:32:01 +000083 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
84 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
85 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
Sam Cleggdb159752018-04-26 19:27:28 +000086 case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
87 case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32:
Sam Clegg4275ee92017-04-28 00:36:36 +000088 HasAddend = true;
89 break;
90 default:
91 break;
92 }
Sam Clegga70c36a2017-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());
Sam Cleggedd76942018-05-01 16:35:16 +000097 if (!SymName.empty())
98 W.printString("Symbol", SymName);
99 else
100 W.printHex("Index", WasmReloc.Index);
Sam Clegg4275ee92017-04-28 00:36:36 +0000101 if (HasAddend)
102 W.printNumber("Addend", WasmReloc.Addend);
Sam Clegga70c36a2017-04-14 19:50:44 +0000103 } else {
Heejin Ahn581d2312018-09-05 01:27:38 +0000104 raw_ostream &OS = W.startLine();
Sam Cleggedd76942018-05-01 16:35:16 +0000105 OS << W.hex(Reloc.getOffset()) << " " << RelocTypeName << " ";
106 if (!SymName.empty())
107 OS << SymName;
108 else
109 OS << WasmReloc.Index;
Sam Clegg4275ee92017-04-28 00:36:36 +0000110 if (HasAddend)
111 OS << " " << WasmReloc.Addend;
112 OS << "\n";
Sam Clegga70c36a2017-04-14 19:50:44 +0000113 }
114}
115
116void WasmDumper::printRelocations() {
117 ListScope D(W, "Relocations");
118
119 int SectionNumber = 0;
120 for (const SectionRef &Section : Obj->sections()) {
121 bool PrintedGroup = false;
122 StringRef Name;
123 error(Section.getName(Name));
124 ++SectionNumber;
125
126 for (const RelocationRef &Reloc : Section.relocations()) {
127 if (!PrintedGroup) {
128 W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
129 W.indent();
130 PrintedGroup = true;
131 }
132
133 printRelocation(Section, Reloc);
134 }
135
136 if (PrintedGroup) {
137 W.unindent();
138 W.startLine() << "}\n";
139 }
140 }
141}
142
143void WasmDumper::printSymbols() {
144 ListScope Group(W, "Symbols");
145
146 for (const SymbolRef &Symbol : Obj->symbols())
147 printSymbol(Symbol);
148}
149
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000150void WasmDumper::printSectionHeaders() {
Sam Clegga70c36a2017-04-14 19:50:44 +0000151 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));
Sam Clegg81e38242017-09-20 19:03:35 +0000156 W.printNumber("Size", static_cast<uint64_t>(WasmSec.Content.size()));
Sam Clegga70c36a2017-04-14 19:50:44 +0000157 W.printNumber("Offset", WasmSec.Offset);
Sam Clegg649003c2017-04-28 21:12:09 +0000158 switch (WasmSec.Type) {
159 case wasm::WASM_SEC_CUSTOM:
Sam Clegga70c36a2017-04-14 19:50:44 +0000160 W.printString("Name", WasmSec.Name);
Sam Cleggf3f79372017-07-10 20:47:12 +0000161 if (WasmSec.Name == "linking") {
162 const wasm::WasmLinkingData &LinkingData = Obj->linkingData();
Sam Clegg44b97042017-12-19 00:04:41 +0000163 if (!LinkingData.InitFunctions.empty()) {
164 ListScope Group(W, "InitFunctions");
Heejin Ahn581d2312018-09-05 01:27:38 +0000165 for (const wasm::WasmInitFunc &F : LinkingData.InitFunctions)
Sam Cleggd5784792018-02-23 05:08:34 +0000166 W.startLine() << F.Symbol << " (priority=" << F.Priority << ")\n";
Sam Clegg44b97042017-12-19 00:04:41 +0000167 }
Sam Cleggf3f79372017-07-10 20:47:12 +0000168 }
Sam Clegg649003c2017-04-28 21:12:09 +0000169 break;
Sam Clegg81e38242017-09-20 19:03:35 +0000170 case wasm::WASM_SEC_DATA: {
171 ListScope Group(W, "Segments");
172 for (const WasmSegment &Segment : Obj->dataSegments()) {
Heejin Ahn581d2312018-09-05 01:27:38 +0000173 const wasm::WasmDataSegment &Seg = Segment.Data;
Sam Clegg81e38242017-09-20 19:03:35 +0000174 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);
180 }
181 break;
182 }
Sam Clegg649003c2017-04-28 21:12:09 +0000183 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);
190 }
191 }
192 break;
Sam Clegga70c36a2017-04-14 19:50:44 +0000193 }
194
195 if (opts::SectionRelocations) {
196 ListScope D(W, "Relocations");
197 for (const RelocationRef &Reloc : Section.relocations())
198 printRelocation(Section, Reloc);
199 }
200
201 if (opts::SectionData) {
202 W.printBinaryBlock("SectionData", WasmSec.Content);
203 }
204 }
205}
206
207void WasmDumper::printSymbol(const SymbolRef &Sym) {
208 DictScope D(W, "Symbol");
209 WasmSymbol Symbol = Obj->getWasmSymbol(Sym.getRawDataRefImpl());
Sam Cleggd5784792018-02-23 05:08:34 +0000210 W.printString("Name", Symbol.Info.Name);
211 W.printEnum("Type", Symbol.Info.Kind, makeArrayRef(WasmSymbolTypes));
212 W.printHex("Flags", Symbol.Info.Flags);
Sam Clegga70c36a2017-04-14 19:50:44 +0000213}
214
Heejin Ahn581d2312018-09-05 01:27:38 +0000215} // namespace
Derek Schuffc20099f2017-01-30 23:30:52 +0000216
217namespace llvm {
218
219std::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");
224
225 Result.reset(new WasmDumper(WasmObj, Writer));
226 return readobj_error::success;
227}
228
229} // namespace llvm