blob: 8cdf06aa14ac33ae601f058fd9e6e822550dd995 [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
43class WasmDumper : public ObjDumper {
44public:
45 WasmDumper(const WasmObjectFile *Obj, ScopedPrinter &Writer)
46 : ObjDumper(Writer), Obj(Obj) {}
47
Sam Clegg135a4b82017-04-14 19:50:44 +000048 void printFileHeaders() override;
Jordan Rupprechtdbf552c2018-11-12 18:02:38 +000049 void printSectionHeaders() override;
Sam Clegg135a4b82017-04-14 19:50:44 +000050 void printRelocations() override;
51 void printSymbols() override;
Derek Schuff6d76b7b2017-01-30 23:30:52 +000052 void printDynamicSymbols() override { llvm_unreachable("unimplemented"); }
53 void printUnwindInfo() override { llvm_unreachable("unimplemented"); }
54 void printStackMap() const override { llvm_unreachable("unimplemented"); }
55
Sam Clegg135a4b82017-04-14 19:50:44 +000056protected:
57 void printSymbol(const SymbolRef &Sym);
58 void printRelocation(const SectionRef &Section, const RelocationRef &Reloc);
59
Derek Schuff6d76b7b2017-01-30 23:30:52 +000060private:
61 const WasmObjectFile *Obj;
62};
Sam Clegg135a4b82017-04-14 19:50:44 +000063
64void WasmDumper::printFileHeaders() {
65 W.printHex("Version", Obj->getHeader().Version);
66}
67
68void WasmDumper::printRelocation(const SectionRef &Section,
69 const RelocationRef &Reloc) {
70 SmallString<64> RelocTypeName;
71 uint64_t RelocType = Reloc.getType();
72 Reloc.getTypeName(RelocTypeName);
73 const wasm::WasmRelocation &WasmReloc = Obj->getWasmRelocation(Reloc);
74
Sam Clegg73812162018-05-01 16:35:16 +000075 StringRef SymName;
76 symbol_iterator SI = Reloc.getSymbol();
77 if (SI != Obj->symbol_end())
78 SymName = error(SI->getName());
79
Sam Clegg10545c92017-04-28 00:36:36 +000080 bool HasAddend = false;
81 switch (RelocType) {
Sam Clegg13a2e892017-09-01 17:32:01 +000082 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
83 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
84 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
Sam Clegg6a31a0d2018-04-26 19:27:28 +000085 case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
86 case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32:
Sam Clegg10545c92017-04-28 00:36:36 +000087 HasAddend = true;
88 break;
89 default:
90 break;
91 }
Sam Clegg135a4b82017-04-14 19:50:44 +000092 if (opts::ExpandRelocs) {
93 DictScope Group(W, "Relocation");
94 W.printNumber("Type", RelocTypeName, RelocType);
95 W.printHex("Offset", Reloc.getOffset());
Sam Clegg73812162018-05-01 16:35:16 +000096 if (!SymName.empty())
97 W.printString("Symbol", SymName);
98 else
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 {
Heejin Ahnf208f632018-09-05 01:27:38 +0000103 raw_ostream &OS = W.startLine();
Sam Clegg73812162018-05-01 16:35:16 +0000104 OS << W.hex(Reloc.getOffset()) << " " << RelocTypeName << " ";
105 if (!SymName.empty())
106 OS << SymName;
107 else
108 OS << WasmReloc.Index;
Sam Clegg10545c92017-04-28 00:36:36 +0000109 if (HasAddend)
110 OS << " " << WasmReloc.Addend;
111 OS << "\n";
Sam Clegg135a4b82017-04-14 19:50:44 +0000112 }
113}
114
115void WasmDumper::printRelocations() {
116 ListScope D(W, "Relocations");
117
118 int SectionNumber = 0;
119 for (const SectionRef &Section : Obj->sections()) {
120 bool PrintedGroup = false;
121 StringRef Name;
122 error(Section.getName(Name));
123 ++SectionNumber;
124
125 for (const RelocationRef &Reloc : Section.relocations()) {
126 if (!PrintedGroup) {
127 W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
128 W.indent();
129 PrintedGroup = true;
130 }
131
132 printRelocation(Section, Reloc);
133 }
134
135 if (PrintedGroup) {
136 W.unindent();
137 W.startLine() << "}\n";
138 }
139 }
140}
141
142void WasmDumper::printSymbols() {
143 ListScope Group(W, "Symbols");
144
145 for (const SymbolRef &Symbol : Obj->symbols())
146 printSymbol(Symbol);
147}
148
Jordan Rupprechtdbf552c2018-11-12 18:02:38 +0000149void WasmDumper::printSectionHeaders() {
Sam Clegg135a4b82017-04-14 19:50:44 +0000150 ListScope Group(W, "Sections");
151 for (const SectionRef &Section : Obj->sections()) {
152 const WasmSection &WasmSec = Obj->getWasmSection(Section);
153 DictScope SectionD(W, "Section");
154 W.printEnum("Type", WasmSec.Type, makeArrayRef(WasmSectionTypes));
Sam Cleggd95ed952017-09-20 19:03:35 +0000155 W.printNumber("Size", static_cast<uint64_t>(WasmSec.Content.size()));
Sam Clegg135a4b82017-04-14 19:50:44 +0000156 W.printNumber("Offset", WasmSec.Offset);
Sam Cleggff0730b2017-04-28 21:12:09 +0000157 switch (WasmSec.Type) {
158 case wasm::WASM_SEC_CUSTOM:
Sam Clegg135a4b82017-04-14 19:50:44 +0000159 W.printString("Name", WasmSec.Name);
Sam Clegg14612fb2017-07-10 20:47:12 +0000160 if (WasmSec.Name == "linking") {
161 const wasm::WasmLinkingData &LinkingData = Obj->linkingData();
Sam Cleggb23a2012017-12-19 00:04:41 +0000162 if (!LinkingData.InitFunctions.empty()) {
163 ListScope Group(W, "InitFunctions");
Heejin Ahnf208f632018-09-05 01:27:38 +0000164 for (const wasm::WasmInitFunc &F : LinkingData.InitFunctions)
Sam Clegg6c899ba2018-02-23 05:08:34 +0000165 W.startLine() << F.Symbol << " (priority=" << F.Priority << ")\n";
Sam Cleggb23a2012017-12-19 00:04:41 +0000166 }
Sam Clegg14612fb2017-07-10 20:47:12 +0000167 }
Sam Cleggff0730b2017-04-28 21:12:09 +0000168 break;
Sam Cleggd95ed952017-09-20 19:03:35 +0000169 case wasm::WASM_SEC_DATA: {
170 ListScope Group(W, "Segments");
171 for (const WasmSegment &Segment : Obj->dataSegments()) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000172 const wasm::WasmDataSegment &Seg = Segment.Data;
Sam Cleggd95ed952017-09-20 19:03:35 +0000173 DictScope Group(W, "Segment");
174 if (!Seg.Name.empty())
175 W.printString("Name", Seg.Name);
176 W.printNumber("Size", static_cast<uint64_t>(Seg.Content.size()));
177 if (Seg.Offset.Opcode == wasm::WASM_OPCODE_I32_CONST)
178 W.printNumber("Offset", Seg.Offset.Value.Int32);
179 }
180 break;
181 }
Sam Cleggff0730b2017-04-28 21:12:09 +0000182 case wasm::WASM_SEC_MEMORY:
183 ListScope Group(W, "Memories");
184 for (const wasm::WasmLimits &Memory : Obj->memories()) {
185 DictScope Group(W, "Memory");
186 W.printNumber("InitialPages", Memory.Initial);
187 if (Memory.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) {
188 W.printNumber("MaxPages", WasmSec.Offset);
189 }
190 }
191 break;
Sam Clegg135a4b82017-04-14 19:50:44 +0000192 }
193
194 if (opts::SectionRelocations) {
195 ListScope D(W, "Relocations");
196 for (const RelocationRef &Reloc : Section.relocations())
197 printRelocation(Section, Reloc);
198 }
199
200 if (opts::SectionData) {
201 W.printBinaryBlock("SectionData", WasmSec.Content);
202 }
203 }
204}
205
206void WasmDumper::printSymbol(const SymbolRef &Sym) {
207 DictScope D(W, "Symbol");
208 WasmSymbol Symbol = Obj->getWasmSymbol(Sym.getRawDataRefImpl());
Sam Clegg6c899ba2018-02-23 05:08:34 +0000209 W.printString("Name", Symbol.Info.Name);
210 W.printEnum("Type", Symbol.Info.Kind, makeArrayRef(WasmSymbolTypes));
211 W.printHex("Flags", Symbol.Info.Flags);
Sam Clegg135a4b82017-04-14 19:50:44 +0000212}
213
Heejin Ahnf208f632018-09-05 01:27:38 +0000214} // namespace
Derek Schuff6d76b7b2017-01-30 23:30:52 +0000215
216namespace llvm {
217
218std::error_code createWasmDumper(const object::ObjectFile *Obj,
219 ScopedPrinter &Writer,
220 std::unique_ptr<ObjDumper> &Result) {
221 const WasmObjectFile *WasmObj = dyn_cast<WasmObjectFile>(Obj);
222 assert(WasmObj && "createWasmDumper called with non-wasm object");
223
224 Result.reset(new WasmDumper(WasmObj, Writer));
225 return readobj_error::success;
226}
227
228} // namespace llvm