blob: 562c76f85be50299f630cef036611b2fd60dfb0e [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 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 Clegg6a31a0d2018-04-26 19:27:28 +000088 case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
89 case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32:
Sam Clegg10545c92017-04-28 00:36:36 +000090 HasAddend = true;
91 break;
92 default:
93 break;
94 }
Sam Clegg135a4b82017-04-14 19:50:44 +000095 if (opts::ExpandRelocs) {
96 DictScope Group(W, "Relocation");
97 W.printNumber("Type", RelocTypeName, RelocType);
98 W.printHex("Offset", Reloc.getOffset());
99 W.printHex("Index", WasmReloc.Index);
Sam Clegg10545c92017-04-28 00:36:36 +0000100 if (HasAddend)
101 W.printNumber("Addend", WasmReloc.Addend);
Sam Clegg135a4b82017-04-14 19:50:44 +0000102 } else {
103 raw_ostream& OS = W.startLine();
Sam Cleggb23a2012017-12-19 00:04:41 +0000104 OS << W.hex(Reloc.getOffset()) << " " << RelocTypeName << "["
105 << WasmReloc.Index << "]";
Sam Clegg10545c92017-04-28 00:36:36 +0000106 if (HasAddend)
107 OS << " " << WasmReloc.Addend;
108 OS << "\n";
Sam Clegg135a4b82017-04-14 19:50:44 +0000109 }
110}
111
112void WasmDumper::printRelocations() {
113 ListScope D(W, "Relocations");
114
115 int SectionNumber = 0;
116 for (const SectionRef &Section : Obj->sections()) {
117 bool PrintedGroup = false;
118 StringRef Name;
119 error(Section.getName(Name));
120 ++SectionNumber;
121
122 for (const RelocationRef &Reloc : Section.relocations()) {
123 if (!PrintedGroup) {
124 W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
125 W.indent();
126 PrintedGroup = true;
127 }
128
129 printRelocation(Section, Reloc);
130 }
131
132 if (PrintedGroup) {
133 W.unindent();
134 W.startLine() << "}\n";
135 }
136 }
137}
138
139void WasmDumper::printSymbols() {
140 ListScope Group(W, "Symbols");
141
142 for (const SymbolRef &Symbol : Obj->symbols())
143 printSymbol(Symbol);
144}
145
146void WasmDumper::printSections() {
147 ListScope Group(W, "Sections");
148 for (const SectionRef &Section : Obj->sections()) {
149 const WasmSection &WasmSec = Obj->getWasmSection(Section);
150 DictScope SectionD(W, "Section");
151 W.printEnum("Type", WasmSec.Type, makeArrayRef(WasmSectionTypes));
Sam Cleggd95ed952017-09-20 19:03:35 +0000152 W.printNumber("Size", static_cast<uint64_t>(WasmSec.Content.size()));
Sam Clegg135a4b82017-04-14 19:50:44 +0000153 W.printNumber("Offset", WasmSec.Offset);
Sam Cleggff0730b2017-04-28 21:12:09 +0000154 switch (WasmSec.Type) {
155 case wasm::WASM_SEC_CUSTOM:
Sam Clegg135a4b82017-04-14 19:50:44 +0000156 W.printString("Name", WasmSec.Name);
Sam Clegg14612fb2017-07-10 20:47:12 +0000157 if (WasmSec.Name == "linking") {
158 const wasm::WasmLinkingData &LinkingData = Obj->linkingData();
Sam Cleggb23a2012017-12-19 00:04:41 +0000159 if (!LinkingData.InitFunctions.empty()) {
160 ListScope Group(W, "InitFunctions");
161 for (const wasm::WasmInitFunc &F: LinkingData.InitFunctions)
Sam Clegg6c899ba2018-02-23 05:08:34 +0000162 W.startLine() << F.Symbol << " (priority=" << F.Priority << ")\n";
Sam Cleggb23a2012017-12-19 00:04:41 +0000163 }
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());
Sam Clegg6c899ba2018-02-23 05:08:34 +0000206 W.printString("Name", Symbol.Info.Name);
207 W.printEnum("Type", Symbol.Info.Kind, makeArrayRef(WasmSymbolTypes));
208 W.printHex("Flags", Symbol.Info.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