blob: 63ec0b732eec8a1f77658dde600396676b211b17 [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 Clegg135a4b82017-04-14 19:50:44 +000030#undef ENUM_ENTRY
31};
32
33static const EnumEntry<uint32_t> WasmSectionTypes[] = {
34#define ENUM_ENTRY(X) { #X, wasm::WASM_SEC_##X }
35 ENUM_ENTRY(CUSTOM),
36 ENUM_ENTRY(TYPE),
37 ENUM_ENTRY(IMPORT),
38 ENUM_ENTRY(FUNCTION),
39 ENUM_ENTRY(TABLE),
40 ENUM_ENTRY(MEMORY),
41 ENUM_ENTRY(GLOBAL),
42 ENUM_ENTRY(EXPORT),
43 ENUM_ENTRY(START),
44 ENUM_ENTRY(ELEM),
45 ENUM_ENTRY(CODE),
46 ENUM_ENTRY(DATA),
47#undef ENUM_ENTRY
48};
Derek Schuff6d76b7b2017-01-30 23:30:52 +000049
50class WasmDumper : public ObjDumper {
51public:
52 WasmDumper(const WasmObjectFile *Obj, ScopedPrinter &Writer)
53 : ObjDumper(Writer), Obj(Obj) {}
54
Sam Clegg135a4b82017-04-14 19:50:44 +000055 void printFileHeaders() override;
56 void printSections() override;
57 void printRelocations() override;
58 void printSymbols() override;
Derek Schuff6d76b7b2017-01-30 23:30:52 +000059 void printDynamicSymbols() override { llvm_unreachable("unimplemented"); }
60 void printUnwindInfo() override { llvm_unreachable("unimplemented"); }
61 void printStackMap() const override { llvm_unreachable("unimplemented"); }
62
Sam Clegg135a4b82017-04-14 19:50:44 +000063protected:
64 void printSymbol(const SymbolRef &Sym);
65 void printRelocation(const SectionRef &Section, const RelocationRef &Reloc);
66
Derek Schuff6d76b7b2017-01-30 23:30:52 +000067private:
68 const WasmObjectFile *Obj;
69};
Sam Clegg135a4b82017-04-14 19:50:44 +000070
71void WasmDumper::printFileHeaders() {
72 W.printHex("Version", Obj->getHeader().Version);
73}
74
75void WasmDumper::printRelocation(const SectionRef &Section,
76 const RelocationRef &Reloc) {
77 SmallString<64> RelocTypeName;
78 uint64_t RelocType = Reloc.getType();
79 Reloc.getTypeName(RelocTypeName);
80 const wasm::WasmRelocation &WasmReloc = Obj->getWasmRelocation(Reloc);
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());
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();
Sam Cleggb23a2012017-12-19 00:04:41 +0000103 OS << W.hex(Reloc.getOffset()) << " " << RelocTypeName << "["
104 << WasmReloc.Index << "]";
Sam Clegg10545c92017-04-28 00:36:36 +0000105 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();
Sam Cleggb23a2012017-12-19 00:04:41 +0000158 if (!LinkingData.InitFunctions.empty()) {
159 ListScope Group(W, "InitFunctions");
160 for (const wasm::WasmInitFunc &F: LinkingData.InitFunctions)
Sam Clegg6c899ba2018-02-23 05:08:34 +0000161 W.startLine() << F.Symbol << " (priority=" << F.Priority << ")\n";
Sam Cleggb23a2012017-12-19 00:04:41 +0000162 }
Sam Clegg14612fb2017-07-10 20:47:12 +0000163 }
Sam Cleggff0730b2017-04-28 21:12:09 +0000164 break;
Sam Cleggd95ed952017-09-20 19:03:35 +0000165 case wasm::WASM_SEC_DATA: {
166 ListScope Group(W, "Segments");
167 for (const WasmSegment &Segment : Obj->dataSegments()) {
168 const wasm::WasmDataSegment& Seg = Segment.Data;
169 DictScope Group(W, "Segment");
170 if (!Seg.Name.empty())
171 W.printString("Name", Seg.Name);
172 W.printNumber("Size", static_cast<uint64_t>(Seg.Content.size()));
173 if (Seg.Offset.Opcode == wasm::WASM_OPCODE_I32_CONST)
174 W.printNumber("Offset", Seg.Offset.Value.Int32);
175 }
176 break;
177 }
Sam Cleggff0730b2017-04-28 21:12:09 +0000178 case wasm::WASM_SEC_MEMORY:
179 ListScope Group(W, "Memories");
180 for (const wasm::WasmLimits &Memory : Obj->memories()) {
181 DictScope Group(W, "Memory");
182 W.printNumber("InitialPages", Memory.Initial);
183 if (Memory.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) {
184 W.printNumber("MaxPages", WasmSec.Offset);
185 }
186 }
187 break;
Sam Clegg135a4b82017-04-14 19:50:44 +0000188 }
189
190 if (opts::SectionRelocations) {
191 ListScope D(W, "Relocations");
192 for (const RelocationRef &Reloc : Section.relocations())
193 printRelocation(Section, Reloc);
194 }
195
196 if (opts::SectionData) {
197 W.printBinaryBlock("SectionData", WasmSec.Content);
198 }
199 }
200}
201
202void WasmDumper::printSymbol(const SymbolRef &Sym) {
203 DictScope D(W, "Symbol");
204 WasmSymbol Symbol = Obj->getWasmSymbol(Sym.getRawDataRefImpl());
Sam Clegg6c899ba2018-02-23 05:08:34 +0000205 W.printString("Name", Symbol.Info.Name);
206 W.printEnum("Type", Symbol.Info.Kind, makeArrayRef(WasmSymbolTypes));
207 W.printHex("Flags", Symbol.Info.Flags);
Sam Clegg135a4b82017-04-14 19:50:44 +0000208}
209
Derek Schuff6d76b7b2017-01-30 23:30:52 +0000210}
211
212namespace llvm {
213
214std::error_code createWasmDumper(const object::ObjectFile *Obj,
215 ScopedPrinter &Writer,
216 std::unique_ptr<ObjDumper> &Result) {
217 const WasmObjectFile *WasmObj = dyn_cast<WasmObjectFile>(Obj);
218 assert(WasmObj && "createWasmDumper called with non-wasm object");
219
220 Result.reset(new WasmDumper(WasmObj, Writer));
221 return readobj_error::success;
222}
223
224} // namespace llvm