blob: edcd0731553d10e20fccc5758d3bad4efeed1eb5 [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 }
Heejin Ahnda419bd2018-11-14 02:46:21 +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),
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),
Sam Clegge450bd72019-02-07 01:17:34 +000053#undef ENUM_ENTRY
54};
55
Derek Schuff6d76b7b2017-01-30 23:30:52 +000056class WasmDumper : public ObjDumper {
57public:
58 WasmDumper(const WasmObjectFile *Obj, ScopedPrinter &Writer)
59 : ObjDumper(Writer), Obj(Obj) {}
60
Sam Clegg135a4b82017-04-14 19:50:44 +000061 void printFileHeaders() override;
Jordan Rupprechtdbf552c2018-11-12 18:02:38 +000062 void printSectionHeaders() override;
Sam Clegg135a4b82017-04-14 19:50:44 +000063 void printRelocations() override;
Derek Schuff6d76b7b2017-01-30 23:30:52 +000064 void printUnwindInfo() override { llvm_unreachable("unimplemented"); }
65 void printStackMap() const override { llvm_unreachable("unimplemented"); }
66
Sam Clegg135a4b82017-04-14 19:50:44 +000067protected:
68 void printSymbol(const SymbolRef &Sym);
69 void printRelocation(const SectionRef &Section, const RelocationRef &Reloc);
70
Derek Schuff6d76b7b2017-01-30 23:30:52 +000071private:
James Henderson21ed8682019-01-23 16:15:39 +000072 void printSymbols() override;
73 void printDynamicSymbols() override { llvm_unreachable("unimplemented"); }
74
Derek Schuff6d76b7b2017-01-30 23:30:52 +000075 const WasmObjectFile *Obj;
76};
Sam Clegg135a4b82017-04-14 19:50:44 +000077
78void WasmDumper::printFileHeaders() {
79 W.printHex("Version", Obj->getHeader().Version);
80}
81
82void WasmDumper::printRelocation(const SectionRef &Section,
83 const RelocationRef &Reloc) {
84 SmallString<64> RelocTypeName;
85 uint64_t RelocType = Reloc.getType();
86 Reloc.getTypeName(RelocTypeName);
87 const wasm::WasmRelocation &WasmReloc = Obj->getWasmRelocation(Reloc);
88
Sam Clegg73812162018-05-01 16:35:16 +000089 StringRef SymName;
90 symbol_iterator SI = Reloc.getSymbol();
91 if (SI != Obj->symbol_end())
92 SymName = error(SI->getName());
93
Sam Clegg10545c92017-04-28 00:36:36 +000094 bool HasAddend = false;
95 switch (RelocType) {
Sam Cleggd1152a22019-02-04 17:28:46 +000096 case wasm::R_WASM_MEMORY_ADDR_LEB:
97 case wasm::R_WASM_MEMORY_ADDR_SLEB:
98 case wasm::R_WASM_MEMORY_ADDR_I32:
99 case wasm::R_WASM_FUNCTION_OFFSET_I32:
100 case wasm::R_WASM_SECTION_OFFSET_I32:
Sam Clegg10545c92017-04-28 00:36:36 +0000101 HasAddend = true;
102 break;
103 default:
104 break;
105 }
Sam Clegg135a4b82017-04-14 19:50:44 +0000106 if (opts::ExpandRelocs) {
107 DictScope Group(W, "Relocation");
108 W.printNumber("Type", RelocTypeName, RelocType);
109 W.printHex("Offset", Reloc.getOffset());
Sam Clegg73812162018-05-01 16:35:16 +0000110 if (!SymName.empty())
111 W.printString("Symbol", SymName);
112 else
113 W.printHex("Index", WasmReloc.Index);
Sam Clegg10545c92017-04-28 00:36:36 +0000114 if (HasAddend)
115 W.printNumber("Addend", WasmReloc.Addend);
Sam Clegg135a4b82017-04-14 19:50:44 +0000116 } else {
Heejin Ahnf208f632018-09-05 01:27:38 +0000117 raw_ostream &OS = W.startLine();
Sam Clegg73812162018-05-01 16:35:16 +0000118 OS << W.hex(Reloc.getOffset()) << " " << RelocTypeName << " ";
119 if (!SymName.empty())
120 OS << SymName;
121 else
122 OS << WasmReloc.Index;
Sam Clegg10545c92017-04-28 00:36:36 +0000123 if (HasAddend)
124 OS << " " << WasmReloc.Addend;
125 OS << "\n";
Sam Clegg135a4b82017-04-14 19:50:44 +0000126 }
127}
128
129void WasmDumper::printRelocations() {
130 ListScope D(W, "Relocations");
131
132 int SectionNumber = 0;
133 for (const SectionRef &Section : Obj->sections()) {
134 bool PrintedGroup = false;
135 StringRef Name;
136 error(Section.getName(Name));
137 ++SectionNumber;
138
139 for (const RelocationRef &Reloc : Section.relocations()) {
140 if (!PrintedGroup) {
141 W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
142 W.indent();
143 PrintedGroup = true;
144 }
145
146 printRelocation(Section, Reloc);
147 }
148
149 if (PrintedGroup) {
150 W.unindent();
151 W.startLine() << "}\n";
152 }
153 }
154}
155
156void WasmDumper::printSymbols() {
157 ListScope Group(W, "Symbols");
158
159 for (const SymbolRef &Symbol : Obj->symbols())
160 printSymbol(Symbol);
161}
162
Jordan Rupprechtdbf552c2018-11-12 18:02:38 +0000163void WasmDumper::printSectionHeaders() {
Sam Clegg135a4b82017-04-14 19:50:44 +0000164 ListScope Group(W, "Sections");
165 for (const SectionRef &Section : Obj->sections()) {
166 const WasmSection &WasmSec = Obj->getWasmSection(Section);
167 DictScope SectionD(W, "Section");
168 W.printEnum("Type", WasmSec.Type, makeArrayRef(WasmSectionTypes));
Sam Cleggd95ed952017-09-20 19:03:35 +0000169 W.printNumber("Size", static_cast<uint64_t>(WasmSec.Content.size()));
Sam Clegg135a4b82017-04-14 19:50:44 +0000170 W.printNumber("Offset", WasmSec.Offset);
Sam Cleggff0730b2017-04-28 21:12:09 +0000171 switch (WasmSec.Type) {
172 case wasm::WASM_SEC_CUSTOM:
Sam Clegg135a4b82017-04-14 19:50:44 +0000173 W.printString("Name", WasmSec.Name);
Sam Clegg14612fb2017-07-10 20:47:12 +0000174 if (WasmSec.Name == "linking") {
175 const wasm::WasmLinkingData &LinkingData = Obj->linkingData();
Sam Cleggb23a2012017-12-19 00:04:41 +0000176 if (!LinkingData.InitFunctions.empty()) {
177 ListScope Group(W, "InitFunctions");
Heejin Ahnf208f632018-09-05 01:27:38 +0000178 for (const wasm::WasmInitFunc &F : LinkingData.InitFunctions)
Sam Clegg6c899ba2018-02-23 05:08:34 +0000179 W.startLine() << F.Symbol << " (priority=" << F.Priority << ")\n";
Sam Cleggb23a2012017-12-19 00:04:41 +0000180 }
Sam Clegg14612fb2017-07-10 20:47:12 +0000181 }
Sam Cleggff0730b2017-04-28 21:12:09 +0000182 break;
Sam Cleggd95ed952017-09-20 19:03:35 +0000183 case wasm::WASM_SEC_DATA: {
184 ListScope Group(W, "Segments");
185 for (const WasmSegment &Segment : Obj->dataSegments()) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000186 const wasm::WasmDataSegment &Seg = Segment.Data;
Sam Cleggd95ed952017-09-20 19:03:35 +0000187 DictScope Group(W, "Segment");
188 if (!Seg.Name.empty())
189 W.printString("Name", Seg.Name);
190 W.printNumber("Size", static_cast<uint64_t>(Seg.Content.size()));
191 if (Seg.Offset.Opcode == wasm::WASM_OPCODE_I32_CONST)
192 W.printNumber("Offset", Seg.Offset.Value.Int32);
193 }
194 break;
195 }
Sam Cleggff0730b2017-04-28 21:12:09 +0000196 case wasm::WASM_SEC_MEMORY:
197 ListScope Group(W, "Memories");
198 for (const wasm::WasmLimits &Memory : Obj->memories()) {
199 DictScope Group(W, "Memory");
200 W.printNumber("InitialPages", Memory.Initial);
201 if (Memory.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) {
202 W.printNumber("MaxPages", WasmSec.Offset);
203 }
204 }
205 break;
Sam Clegg135a4b82017-04-14 19:50:44 +0000206 }
207
208 if (opts::SectionRelocations) {
209 ListScope D(W, "Relocations");
210 for (const RelocationRef &Reloc : Section.relocations())
211 printRelocation(Section, Reloc);
212 }
213
214 if (opts::SectionData) {
215 W.printBinaryBlock("SectionData", WasmSec.Content);
216 }
217 }
218}
219
220void WasmDumper::printSymbol(const SymbolRef &Sym) {
221 DictScope D(W, "Symbol");
222 WasmSymbol Symbol = Obj->getWasmSymbol(Sym.getRawDataRefImpl());
Sam Clegg6c899ba2018-02-23 05:08:34 +0000223 W.printString("Name", Symbol.Info.Name);
224 W.printEnum("Type", Symbol.Info.Kind, makeArrayRef(WasmSymbolTypes));
Sam Clegge450bd72019-02-07 01:17:34 +0000225 W.printFlags("Flags", Symbol.Info.Flags, makeArrayRef(WasmSymbolFlags));
226
Dan Gohman29874ce2019-02-07 22:03:32 +0000227 if (Symbol.Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) {
228 W.printString("ImportName", Symbol.Info.ImportName);
229 W.printString("ImportModule", Symbol.Info.ImportModule);
230 }
Sam Clegg3fd24622019-02-04 22:27:46 +0000231 if (Symbol.Info.Kind != wasm::WASM_SYMBOL_TYPE_DATA) {
232 W.printHex("ElementIndex", Symbol.Info.ElementIndex);
233 } else if (!(Symbol.Info.Flags & wasm::WASM_SYMBOL_UNDEFINED)) {
234 W.printHex("Offset", Symbol.Info.DataRef.Offset);
235 W.printHex("Segment", Symbol.Info.DataRef.Segment);
236 W.printHex("Size", Symbol.Info.DataRef.Size);
237 }
Sam Clegg135a4b82017-04-14 19:50:44 +0000238}
239
Heejin Ahnf208f632018-09-05 01:27:38 +0000240} // namespace
Derek Schuff6d76b7b2017-01-30 23:30:52 +0000241
242namespace llvm {
243
244std::error_code createWasmDumper(const object::ObjectFile *Obj,
245 ScopedPrinter &Writer,
246 std::unique_ptr<ObjDumper> &Result) {
Heejin Ahn18c56a02019-02-04 19:13:39 +0000247 const auto *WasmObj = dyn_cast<WasmObjectFile>(Obj);
Derek Schuff6d76b7b2017-01-30 23:30:52 +0000248 assert(WasmObj && "createWasmDumper called with non-wasm object");
249
250 Result.reset(new WasmDumper(WasmObj, Writer));
251 return readobj_error::success;
252}
253
254} // namespace llvm