blob: 041a9a15bdb64410ec5128f70d90868ffc89ca16 [file] [log] [blame]
Derek Schuff6d76b7b2017-01-30 23:30:52 +00001//===-- WasmDumper.cpp - Wasm-specific object file dumper -----------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Derek Schuff6d76b7b2017-01-30 23:30:52 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Wasm-specific dumper for llvm-readobj.
10//
11//===----------------------------------------------------------------------===//
12
13#include "Error.h"
14#include "ObjDumper.h"
Sam Clegg135a4b82017-04-14 19:50:44 +000015#include "llvm-readobj.h"
Derek Schuff6d76b7b2017-01-30 23:30:52 +000016#include "llvm/Object/Wasm.h"
17#include "llvm/Support/ScopedPrinter.h"
18
19using namespace llvm;
20using namespace object;
21
22namespace {
23
Sam Clegg135a4b82017-04-14 19:50:44 +000024static const EnumEntry<unsigned> WasmSymbolTypes[] = {
Heejin Ahnf208f632018-09-05 01:27:38 +000025#define ENUM_ENTRY(X) \
26 { #X, wasm::WASM_SYMBOL_TYPE_##X }
Heejin Ahnda419bd2018-11-14 02:46:21 +000027 ENUM_ENTRY(FUNCTION), ENUM_ENTRY(DATA), ENUM_ENTRY(GLOBAL),
28 ENUM_ENTRY(SECTION), ENUM_ENTRY(EVENT),
Sam Clegg135a4b82017-04-14 19:50:44 +000029#undef ENUM_ENTRY
30};
31
32static const EnumEntry<uint32_t> WasmSectionTypes[] = {
Heejin Ahnf208f632018-09-05 01:27:38 +000033#define ENUM_ENTRY(X) \
34 { #X, wasm::WASM_SEC_##X }
Thomas Livelyfef8de62019-04-12 22:27:48 +000035 ENUM_ENTRY(CUSTOM), ENUM_ENTRY(TYPE), ENUM_ENTRY(IMPORT),
36 ENUM_ENTRY(FUNCTION), ENUM_ENTRY(TABLE), ENUM_ENTRY(MEMORY),
37 ENUM_ENTRY(GLOBAL), ENUM_ENTRY(EVENT), ENUM_ENTRY(EXPORT),
38 ENUM_ENTRY(START), ENUM_ENTRY(ELEM), ENUM_ENTRY(CODE),
39 ENUM_ENTRY(DATA), ENUM_ENTRY(DATACOUNT),
Sam Clegg135a4b82017-04-14 19:50:44 +000040#undef ENUM_ENTRY
41};
Derek Schuff6d76b7b2017-01-30 23:30:52 +000042
Sam Clegge450bd72019-02-07 01:17:34 +000043static const EnumEntry<unsigned> WasmSymbolFlags[] = {
44#define ENUM_ENTRY(X) \
45 { #X, wasm::WASM_SYMBOL_##X }
46 ENUM_ENTRY(BINDING_GLOBAL),
47 ENUM_ENTRY(BINDING_WEAK),
48 ENUM_ENTRY(BINDING_LOCAL),
49 ENUM_ENTRY(VISIBILITY_DEFAULT),
50 ENUM_ENTRY(VISIBILITY_HIDDEN),
51 ENUM_ENTRY(UNDEFINED),
Sam Cleggd6ef8da2019-02-07 01:24:44 +000052 ENUM_ENTRY(EXPORTED),
Dan Gohman3b5b9d02019-04-30 19:30:24 +000053 ENUM_ENTRY(EXPLICIT_NAME),
Sam Clegge450bd72019-02-07 01:17:34 +000054#undef ENUM_ENTRY
55};
56
Derek Schuff6d76b7b2017-01-30 23:30:52 +000057class WasmDumper : public ObjDumper {
58public:
59 WasmDumper(const WasmObjectFile *Obj, ScopedPrinter &Writer)
60 : ObjDumper(Writer), Obj(Obj) {}
61
Sam Clegg135a4b82017-04-14 19:50:44 +000062 void printFileHeaders() override;
Jordan Rupprechtdbf552c2018-11-12 18:02:38 +000063 void printSectionHeaders() override;
Sam Clegg135a4b82017-04-14 19:50:44 +000064 void printRelocations() override;
Derek Schuff6d76b7b2017-01-30 23:30:52 +000065 void printUnwindInfo() override { llvm_unreachable("unimplemented"); }
66 void printStackMap() const override { llvm_unreachable("unimplemented"); }
67
Sam Clegg135a4b82017-04-14 19:50:44 +000068protected:
69 void printSymbol(const SymbolRef &Sym);
70 void printRelocation(const SectionRef &Section, const RelocationRef &Reloc);
71
Derek Schuff6d76b7b2017-01-30 23:30:52 +000072private:
James Henderson21ed8682019-01-23 16:15:39 +000073 void printSymbols() override;
74 void printDynamicSymbols() override { llvm_unreachable("unimplemented"); }
75
Derek Schuff6d76b7b2017-01-30 23:30:52 +000076 const WasmObjectFile *Obj;
77};
Sam Clegg135a4b82017-04-14 19:50:44 +000078
79void WasmDumper::printFileHeaders() {
80 W.printHex("Version", Obj->getHeader().Version);
81}
82
83void WasmDumper::printRelocation(const SectionRef &Section,
84 const RelocationRef &Reloc) {
85 SmallString<64> RelocTypeName;
86 uint64_t RelocType = Reloc.getType();
87 Reloc.getTypeName(RelocTypeName);
88 const wasm::WasmRelocation &WasmReloc = Obj->getWasmRelocation(Reloc);
89
Sam Clegg73812162018-05-01 16:35:16 +000090 StringRef SymName;
91 symbol_iterator SI = Reloc.getSymbol();
92 if (SI != Obj->symbol_end())
93 SymName = error(SI->getName());
94
Sam Clegg10545c92017-04-28 00:36:36 +000095 bool HasAddend = false;
96 switch (RelocType) {
Sam Cleggd1152a22019-02-04 17:28:46 +000097 case wasm::R_WASM_MEMORY_ADDR_LEB:
98 case wasm::R_WASM_MEMORY_ADDR_SLEB:
99 case wasm::R_WASM_MEMORY_ADDR_I32:
100 case wasm::R_WASM_FUNCTION_OFFSET_I32:
101 case wasm::R_WASM_SECTION_OFFSET_I32:
Sam Clegg10545c92017-04-28 00:36:36 +0000102 HasAddend = true;
103 break;
104 default:
105 break;
106 }
Sam Clegg135a4b82017-04-14 19:50:44 +0000107 if (opts::ExpandRelocs) {
108 DictScope Group(W, "Relocation");
109 W.printNumber("Type", RelocTypeName, RelocType);
110 W.printHex("Offset", Reloc.getOffset());
Sam Clegg73812162018-05-01 16:35:16 +0000111 if (!SymName.empty())
112 W.printString("Symbol", SymName);
113 else
114 W.printHex("Index", WasmReloc.Index);
Sam Clegg10545c92017-04-28 00:36:36 +0000115 if (HasAddend)
116 W.printNumber("Addend", WasmReloc.Addend);
Sam Clegg135a4b82017-04-14 19:50:44 +0000117 } else {
Heejin Ahnf208f632018-09-05 01:27:38 +0000118 raw_ostream &OS = W.startLine();
Sam Clegg73812162018-05-01 16:35:16 +0000119 OS << W.hex(Reloc.getOffset()) << " " << RelocTypeName << " ";
120 if (!SymName.empty())
121 OS << SymName;
122 else
123 OS << WasmReloc.Index;
Sam Clegg10545c92017-04-28 00:36:36 +0000124 if (HasAddend)
125 OS << " " << WasmReloc.Addend;
126 OS << "\n";
Sam Clegg135a4b82017-04-14 19:50:44 +0000127 }
128}
129
130void WasmDumper::printRelocations() {
131 ListScope D(W, "Relocations");
132
133 int SectionNumber = 0;
134 for (const SectionRef &Section : Obj->sections()) {
135 bool PrintedGroup = false;
136 StringRef Name;
137 error(Section.getName(Name));
138 ++SectionNumber;
139
140 for (const RelocationRef &Reloc : Section.relocations()) {
141 if (!PrintedGroup) {
142 W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
143 W.indent();
144 PrintedGroup = true;
145 }
146
147 printRelocation(Section, Reloc);
148 }
149
150 if (PrintedGroup) {
151 W.unindent();
152 W.startLine() << "}\n";
153 }
154 }
155}
156
157void WasmDumper::printSymbols() {
158 ListScope Group(W, "Symbols");
159
160 for (const SymbolRef &Symbol : Obj->symbols())
161 printSymbol(Symbol);
162}
163
Jordan Rupprechtdbf552c2018-11-12 18:02:38 +0000164void WasmDumper::printSectionHeaders() {
Sam Clegg135a4b82017-04-14 19:50:44 +0000165 ListScope Group(W, "Sections");
166 for (const SectionRef &Section : Obj->sections()) {
167 const WasmSection &WasmSec = Obj->getWasmSection(Section);
168 DictScope SectionD(W, "Section");
169 W.printEnum("Type", WasmSec.Type, makeArrayRef(WasmSectionTypes));
Sam Cleggd95ed952017-09-20 19:03:35 +0000170 W.printNumber("Size", static_cast<uint64_t>(WasmSec.Content.size()));
Sam Clegg135a4b82017-04-14 19:50:44 +0000171 W.printNumber("Offset", WasmSec.Offset);
Sam Cleggff0730b2017-04-28 21:12:09 +0000172 switch (WasmSec.Type) {
173 case wasm::WASM_SEC_CUSTOM:
Sam Clegg135a4b82017-04-14 19:50:44 +0000174 W.printString("Name", WasmSec.Name);
Sam Clegg14612fb2017-07-10 20:47:12 +0000175 if (WasmSec.Name == "linking") {
176 const wasm::WasmLinkingData &LinkingData = Obj->linkingData();
Sam Cleggb23a2012017-12-19 00:04:41 +0000177 if (!LinkingData.InitFunctions.empty()) {
178 ListScope Group(W, "InitFunctions");
Heejin Ahnf208f632018-09-05 01:27:38 +0000179 for (const wasm::WasmInitFunc &F : LinkingData.InitFunctions)
Sam Clegg6c899ba2018-02-23 05:08:34 +0000180 W.startLine() << F.Symbol << " (priority=" << F.Priority << ")\n";
Sam Cleggb23a2012017-12-19 00:04:41 +0000181 }
Sam Clegg14612fb2017-07-10 20:47:12 +0000182 }
Sam Cleggff0730b2017-04-28 21:12:09 +0000183 break;
Sam Cleggd95ed952017-09-20 19:03:35 +0000184 case wasm::WASM_SEC_DATA: {
185 ListScope Group(W, "Segments");
186 for (const WasmSegment &Segment : Obj->dataSegments()) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000187 const wasm::WasmDataSegment &Seg = Segment.Data;
Sam Cleggd95ed952017-09-20 19:03:35 +0000188 DictScope Group(W, "Segment");
189 if (!Seg.Name.empty())
190 W.printString("Name", Seg.Name);
191 W.printNumber("Size", static_cast<uint64_t>(Seg.Content.size()));
192 if (Seg.Offset.Opcode == wasm::WASM_OPCODE_I32_CONST)
193 W.printNumber("Offset", Seg.Offset.Value.Int32);
194 }
195 break;
196 }
Sam Cleggff0730b2017-04-28 21:12:09 +0000197 case wasm::WASM_SEC_MEMORY:
198 ListScope Group(W, "Memories");
199 for (const wasm::WasmLimits &Memory : Obj->memories()) {
200 DictScope Group(W, "Memory");
201 W.printNumber("InitialPages", Memory.Initial);
202 if (Memory.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) {
203 W.printNumber("MaxPages", WasmSec.Offset);
204 }
205 }
206 break;
Sam Clegg135a4b82017-04-14 19:50:44 +0000207 }
208
209 if (opts::SectionRelocations) {
210 ListScope D(W, "Relocations");
211 for (const RelocationRef &Reloc : Section.relocations())
212 printRelocation(Section, Reloc);
213 }
214
215 if (opts::SectionData) {
216 W.printBinaryBlock("SectionData", WasmSec.Content);
217 }
218 }
219}
220
221void WasmDumper::printSymbol(const SymbolRef &Sym) {
222 DictScope D(W, "Symbol");
223 WasmSymbol Symbol = Obj->getWasmSymbol(Sym.getRawDataRefImpl());
Sam Clegg6c899ba2018-02-23 05:08:34 +0000224 W.printString("Name", Symbol.Info.Name);
225 W.printEnum("Type", Symbol.Info.Kind, makeArrayRef(WasmSymbolTypes));
Sam Clegge450bd72019-02-07 01:17:34 +0000226 W.printFlags("Flags", Symbol.Info.Flags, makeArrayRef(WasmSymbolFlags));
227
Dan Gohman29874ce2019-02-07 22:03:32 +0000228 if (Symbol.Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) {
229 W.printString("ImportName", Symbol.Info.ImportName);
230 W.printString("ImportModule", Symbol.Info.ImportModule);
231 }
Sam Clegg3fd24622019-02-04 22:27:46 +0000232 if (Symbol.Info.Kind != wasm::WASM_SYMBOL_TYPE_DATA) {
233 W.printHex("ElementIndex", Symbol.Info.ElementIndex);
234 } else if (!(Symbol.Info.Flags & wasm::WASM_SYMBOL_UNDEFINED)) {
235 W.printHex("Offset", Symbol.Info.DataRef.Offset);
236 W.printHex("Segment", Symbol.Info.DataRef.Segment);
237 W.printHex("Size", Symbol.Info.DataRef.Size);
238 }
Sam Clegg135a4b82017-04-14 19:50:44 +0000239}
240
Heejin Ahnf208f632018-09-05 01:27:38 +0000241} // namespace
Derek Schuff6d76b7b2017-01-30 23:30:52 +0000242
243namespace llvm {
244
245std::error_code createWasmDumper(const object::ObjectFile *Obj,
246 ScopedPrinter &Writer,
247 std::unique_ptr<ObjDumper> &Result) {
Heejin Ahn18c56a02019-02-04 19:13:39 +0000248 const auto *WasmObj = dyn_cast<WasmObjectFile>(Obj);
Derek Schuff6d76b7b2017-01-30 23:30:52 +0000249 assert(WasmObj && "createWasmDumper called with non-wasm object");
250
251 Result.reset(new WasmDumper(WasmObj, Writer));
252 return readobj_error::success;
253}
254
255} // namespace llvm