blob: 42851ab68320cc617659721aead8d327734f42cb [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),
52#undef ENUM_ENTRY
53};
54
Derek Schuff6d76b7b2017-01-30 23:30:52 +000055class WasmDumper : public ObjDumper {
56public:
57 WasmDumper(const WasmObjectFile *Obj, ScopedPrinter &Writer)
58 : ObjDumper(Writer), Obj(Obj) {}
59
Sam Clegg135a4b82017-04-14 19:50:44 +000060 void printFileHeaders() override;
Jordan Rupprechtdbf552c2018-11-12 18:02:38 +000061 void printSectionHeaders() override;
Sam Clegg135a4b82017-04-14 19:50:44 +000062 void printRelocations() override;
Derek Schuff6d76b7b2017-01-30 23:30:52 +000063 void printUnwindInfo() override { llvm_unreachable("unimplemented"); }
64 void printStackMap() const override { llvm_unreachable("unimplemented"); }
65
Sam Clegg135a4b82017-04-14 19:50:44 +000066protected:
67 void printSymbol(const SymbolRef &Sym);
68 void printRelocation(const SectionRef &Section, const RelocationRef &Reloc);
69
Derek Schuff6d76b7b2017-01-30 23:30:52 +000070private:
James Henderson21ed8682019-01-23 16:15:39 +000071 void printSymbols() override;
72 void printDynamicSymbols() override { llvm_unreachable("unimplemented"); }
73
Derek Schuff6d76b7b2017-01-30 23:30:52 +000074 const WasmObjectFile *Obj;
75};
Sam Clegg135a4b82017-04-14 19:50:44 +000076
77void WasmDumper::printFileHeaders() {
78 W.printHex("Version", Obj->getHeader().Version);
79}
80
81void WasmDumper::printRelocation(const SectionRef &Section,
82 const RelocationRef &Reloc) {
83 SmallString<64> RelocTypeName;
84 uint64_t RelocType = Reloc.getType();
85 Reloc.getTypeName(RelocTypeName);
86 const wasm::WasmRelocation &WasmReloc = Obj->getWasmRelocation(Reloc);
87
Sam Clegg73812162018-05-01 16:35:16 +000088 StringRef SymName;
89 symbol_iterator SI = Reloc.getSymbol();
90 if (SI != Obj->symbol_end())
91 SymName = error(SI->getName());
92
Sam Clegg10545c92017-04-28 00:36:36 +000093 bool HasAddend = false;
94 switch (RelocType) {
Sam Cleggd1152a22019-02-04 17:28:46 +000095 case wasm::R_WASM_MEMORY_ADDR_LEB:
96 case wasm::R_WASM_MEMORY_ADDR_SLEB:
97 case wasm::R_WASM_MEMORY_ADDR_I32:
98 case wasm::R_WASM_FUNCTION_OFFSET_I32:
99 case wasm::R_WASM_SECTION_OFFSET_I32:
Sam Clegg10545c92017-04-28 00:36:36 +0000100 HasAddend = true;
101 break;
102 default:
103 break;
104 }
Sam Clegg135a4b82017-04-14 19:50:44 +0000105 if (opts::ExpandRelocs) {
106 DictScope Group(W, "Relocation");
107 W.printNumber("Type", RelocTypeName, RelocType);
108 W.printHex("Offset", Reloc.getOffset());
Sam Clegg73812162018-05-01 16:35:16 +0000109 if (!SymName.empty())
110 W.printString("Symbol", SymName);
111 else
112 W.printHex("Index", WasmReloc.Index);
Sam Clegg10545c92017-04-28 00:36:36 +0000113 if (HasAddend)
114 W.printNumber("Addend", WasmReloc.Addend);
Sam Clegg135a4b82017-04-14 19:50:44 +0000115 } else {
Heejin Ahnf208f632018-09-05 01:27:38 +0000116 raw_ostream &OS = W.startLine();
Sam Clegg73812162018-05-01 16:35:16 +0000117 OS << W.hex(Reloc.getOffset()) << " " << RelocTypeName << " ";
118 if (!SymName.empty())
119 OS << SymName;
120 else
121 OS << WasmReloc.Index;
Sam Clegg10545c92017-04-28 00:36:36 +0000122 if (HasAddend)
123 OS << " " << WasmReloc.Addend;
124 OS << "\n";
Sam Clegg135a4b82017-04-14 19:50:44 +0000125 }
126}
127
128void WasmDumper::printRelocations() {
129 ListScope D(W, "Relocations");
130
131 int SectionNumber = 0;
132 for (const SectionRef &Section : Obj->sections()) {
133 bool PrintedGroup = false;
134 StringRef Name;
135 error(Section.getName(Name));
136 ++SectionNumber;
137
138 for (const RelocationRef &Reloc : Section.relocations()) {
139 if (!PrintedGroup) {
140 W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
141 W.indent();
142 PrintedGroup = true;
143 }
144
145 printRelocation(Section, Reloc);
146 }
147
148 if (PrintedGroup) {
149 W.unindent();
150 W.startLine() << "}\n";
151 }
152 }
153}
154
155void WasmDumper::printSymbols() {
156 ListScope Group(W, "Symbols");
157
158 for (const SymbolRef &Symbol : Obj->symbols())
159 printSymbol(Symbol);
160}
161
Jordan Rupprechtdbf552c2018-11-12 18:02:38 +0000162void WasmDumper::printSectionHeaders() {
Sam Clegg135a4b82017-04-14 19:50:44 +0000163 ListScope Group(W, "Sections");
164 for (const SectionRef &Section : Obj->sections()) {
165 const WasmSection &WasmSec = Obj->getWasmSection(Section);
166 DictScope SectionD(W, "Section");
167 W.printEnum("Type", WasmSec.Type, makeArrayRef(WasmSectionTypes));
Sam Cleggd95ed952017-09-20 19:03:35 +0000168 W.printNumber("Size", static_cast<uint64_t>(WasmSec.Content.size()));
Sam Clegg135a4b82017-04-14 19:50:44 +0000169 W.printNumber("Offset", WasmSec.Offset);
Sam Cleggff0730b2017-04-28 21:12:09 +0000170 switch (WasmSec.Type) {
171 case wasm::WASM_SEC_CUSTOM:
Sam Clegg135a4b82017-04-14 19:50:44 +0000172 W.printString("Name", WasmSec.Name);
Sam Clegg14612fb2017-07-10 20:47:12 +0000173 if (WasmSec.Name == "linking") {
174 const wasm::WasmLinkingData &LinkingData = Obj->linkingData();
Sam Cleggb23a2012017-12-19 00:04:41 +0000175 if (!LinkingData.InitFunctions.empty()) {
176 ListScope Group(W, "InitFunctions");
Heejin Ahnf208f632018-09-05 01:27:38 +0000177 for (const wasm::WasmInitFunc &F : LinkingData.InitFunctions)
Sam Clegg6c899ba2018-02-23 05:08:34 +0000178 W.startLine() << F.Symbol << " (priority=" << F.Priority << ")\n";
Sam Cleggb23a2012017-12-19 00:04:41 +0000179 }
Sam Clegg14612fb2017-07-10 20:47:12 +0000180 }
Sam Cleggff0730b2017-04-28 21:12:09 +0000181 break;
Sam Cleggd95ed952017-09-20 19:03:35 +0000182 case wasm::WASM_SEC_DATA: {
183 ListScope Group(W, "Segments");
184 for (const WasmSegment &Segment : Obj->dataSegments()) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000185 const wasm::WasmDataSegment &Seg = Segment.Data;
Sam Cleggd95ed952017-09-20 19:03:35 +0000186 DictScope Group(W, "Segment");
187 if (!Seg.Name.empty())
188 W.printString("Name", Seg.Name);
189 W.printNumber("Size", static_cast<uint64_t>(Seg.Content.size()));
190 if (Seg.Offset.Opcode == wasm::WASM_OPCODE_I32_CONST)
191 W.printNumber("Offset", Seg.Offset.Value.Int32);
192 }
193 break;
194 }
Sam Cleggff0730b2017-04-28 21:12:09 +0000195 case wasm::WASM_SEC_MEMORY:
196 ListScope Group(W, "Memories");
197 for (const wasm::WasmLimits &Memory : Obj->memories()) {
198 DictScope Group(W, "Memory");
199 W.printNumber("InitialPages", Memory.Initial);
200 if (Memory.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) {
201 W.printNumber("MaxPages", WasmSec.Offset);
202 }
203 }
204 break;
Sam Clegg135a4b82017-04-14 19:50:44 +0000205 }
206
207 if (opts::SectionRelocations) {
208 ListScope D(W, "Relocations");
209 for (const RelocationRef &Reloc : Section.relocations())
210 printRelocation(Section, Reloc);
211 }
212
213 if (opts::SectionData) {
214 W.printBinaryBlock("SectionData", WasmSec.Content);
215 }
216 }
217}
218
219void WasmDumper::printSymbol(const SymbolRef &Sym) {
220 DictScope D(W, "Symbol");
221 WasmSymbol Symbol = Obj->getWasmSymbol(Sym.getRawDataRefImpl());
Sam Clegg6c899ba2018-02-23 05:08:34 +0000222 W.printString("Name", Symbol.Info.Name);
223 W.printEnum("Type", Symbol.Info.Kind, makeArrayRef(WasmSymbolTypes));
Sam Clegge450bd72019-02-07 01:17:34 +0000224 W.printFlags("Flags", Symbol.Info.Flags, makeArrayRef(WasmSymbolFlags));
225
Sam Clegg3fd24622019-02-04 22:27:46 +0000226 if (Symbol.Info.Flags & wasm::WASM_SYMBOL_UNDEFINED)
227 W.printString("Module", Symbol.Info.Module);
228 if (Symbol.Info.Kind != wasm::WASM_SYMBOL_TYPE_DATA) {
229 W.printHex("ElementIndex", Symbol.Info.ElementIndex);
230 } else if (!(Symbol.Info.Flags & wasm::WASM_SYMBOL_UNDEFINED)) {
231 W.printHex("Offset", Symbol.Info.DataRef.Offset);
232 W.printHex("Segment", Symbol.Info.DataRef.Segment);
233 W.printHex("Size", Symbol.Info.DataRef.Size);
234 }
Sam Clegg135a4b82017-04-14 19:50:44 +0000235}
236
Heejin Ahnf208f632018-09-05 01:27:38 +0000237} // namespace
Derek Schuff6d76b7b2017-01-30 23:30:52 +0000238
239namespace llvm {
240
241std::error_code createWasmDumper(const object::ObjectFile *Obj,
242 ScopedPrinter &Writer,
243 std::unique_ptr<ObjDumper> &Result) {
Heejin Ahn18c56a02019-02-04 19:13:39 +0000244 const auto *WasmObj = dyn_cast<WasmObjectFile>(Obj);
Derek Schuff6d76b7b2017-01-30 23:30:52 +0000245 assert(WasmObj && "createWasmDumper called with non-wasm object");
246
247 Result.reset(new WasmDumper(WasmObj, Writer));
248 return readobj_error::success;
249}
250
251} // namespace llvm