Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / tools / llvm-readobj / WasmDumper.cpp
blobedcd0731553d10e20fccc5758d3bad4efeed1eb5
1 //===-- WasmDumper.cpp - Wasm-specific object file dumper -----------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the Wasm-specific dumper for llvm-readobj.
11 //===----------------------------------------------------------------------===//
13 #include "Error.h"
14 #include "ObjDumper.h"
15 #include "llvm-readobj.h"
16 #include "llvm/Object/Wasm.h"
17 #include "llvm/Support/ScopedPrinter.h"
19 using namespace llvm;
20 using namespace object;
22 namespace {
24 static const EnumEntry<unsigned> WasmSymbolTypes[] = {
25 #define ENUM_ENTRY(X) \
26 { #X, wasm::WASM_SYMBOL_TYPE_##X }
27 ENUM_ENTRY(FUNCTION), ENUM_ENTRY(DATA), ENUM_ENTRY(GLOBAL),
28 ENUM_ENTRY(SECTION), ENUM_ENTRY(EVENT),
29 #undef ENUM_ENTRY
32 static const EnumEntry<uint32_t> WasmSectionTypes[] = {
33 #define ENUM_ENTRY(X) \
34 { #X, wasm::WASM_SEC_##X }
35 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),
40 #undef ENUM_ENTRY
43 static 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 ENUM_ENTRY(EXPORTED),
53 #undef ENUM_ENTRY
56 class WasmDumper : public ObjDumper {
57 public:
58 WasmDumper(const WasmObjectFile *Obj, ScopedPrinter &Writer)
59 : ObjDumper(Writer), Obj(Obj) {}
61 void printFileHeaders() override;
62 void printSectionHeaders() override;
63 void printRelocations() override;
64 void printUnwindInfo() override { llvm_unreachable("unimplemented"); }
65 void printStackMap() const override { llvm_unreachable("unimplemented"); }
67 protected:
68 void printSymbol(const SymbolRef &Sym);
69 void printRelocation(const SectionRef &Section, const RelocationRef &Reloc);
71 private:
72 void printSymbols() override;
73 void printDynamicSymbols() override { llvm_unreachable("unimplemented"); }
75 const WasmObjectFile *Obj;
78 void WasmDumper::printFileHeaders() {
79 W.printHex("Version", Obj->getHeader().Version);
82 void 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);
89 StringRef SymName;
90 symbol_iterator SI = Reloc.getSymbol();
91 if (SI != Obj->symbol_end())
92 SymName = error(SI->getName());
94 bool HasAddend = false;
95 switch (RelocType) {
96 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:
101 HasAddend = true;
102 break;
103 default:
104 break;
106 if (opts::ExpandRelocs) {
107 DictScope Group(W, "Relocation");
108 W.printNumber("Type", RelocTypeName, RelocType);
109 W.printHex("Offset", Reloc.getOffset());
110 if (!SymName.empty())
111 W.printString("Symbol", SymName);
112 else
113 W.printHex("Index", WasmReloc.Index);
114 if (HasAddend)
115 W.printNumber("Addend", WasmReloc.Addend);
116 } else {
117 raw_ostream &OS = W.startLine();
118 OS << W.hex(Reloc.getOffset()) << " " << RelocTypeName << " ";
119 if (!SymName.empty())
120 OS << SymName;
121 else
122 OS << WasmReloc.Index;
123 if (HasAddend)
124 OS << " " << WasmReloc.Addend;
125 OS << "\n";
129 void WasmDumper::printRelocations() {
130 ListScope D(W, "Relocations");
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;
139 for (const RelocationRef &Reloc : Section.relocations()) {
140 if (!PrintedGroup) {
141 W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
142 W.indent();
143 PrintedGroup = true;
146 printRelocation(Section, Reloc);
149 if (PrintedGroup) {
150 W.unindent();
151 W.startLine() << "}\n";
156 void WasmDumper::printSymbols() {
157 ListScope Group(W, "Symbols");
159 for (const SymbolRef &Symbol : Obj->symbols())
160 printSymbol(Symbol);
163 void WasmDumper::printSectionHeaders() {
164 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));
169 W.printNumber("Size", static_cast<uint64_t>(WasmSec.Content.size()));
170 W.printNumber("Offset", WasmSec.Offset);
171 switch (WasmSec.Type) {
172 case wasm::WASM_SEC_CUSTOM:
173 W.printString("Name", WasmSec.Name);
174 if (WasmSec.Name == "linking") {
175 const wasm::WasmLinkingData &LinkingData = Obj->linkingData();
176 if (!LinkingData.InitFunctions.empty()) {
177 ListScope Group(W, "InitFunctions");
178 for (const wasm::WasmInitFunc &F : LinkingData.InitFunctions)
179 W.startLine() << F.Symbol << " (priority=" << F.Priority << ")\n";
182 break;
183 case wasm::WASM_SEC_DATA: {
184 ListScope Group(W, "Segments");
185 for (const WasmSegment &Segment : Obj->dataSegments()) {
186 const wasm::WasmDataSegment &Seg = Segment.Data;
187 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);
194 break;
196 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);
205 break;
208 if (opts::SectionRelocations) {
209 ListScope D(W, "Relocations");
210 for (const RelocationRef &Reloc : Section.relocations())
211 printRelocation(Section, Reloc);
214 if (opts::SectionData) {
215 W.printBinaryBlock("SectionData", WasmSec.Content);
220 void WasmDumper::printSymbol(const SymbolRef &Sym) {
221 DictScope D(W, "Symbol");
222 WasmSymbol Symbol = Obj->getWasmSymbol(Sym.getRawDataRefImpl());
223 W.printString("Name", Symbol.Info.Name);
224 W.printEnum("Type", Symbol.Info.Kind, makeArrayRef(WasmSymbolTypes));
225 W.printFlags("Flags", Symbol.Info.Flags, makeArrayRef(WasmSymbolFlags));
227 if (Symbol.Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) {
228 W.printString("ImportName", Symbol.Info.ImportName);
229 W.printString("ImportModule", Symbol.Info.ImportModule);
231 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);
240 } // namespace
242 namespace llvm {
244 std::error_code createWasmDumper(const object::ObjectFile *Obj,
245 ScopedPrinter &Writer,
246 std::unique_ptr<ObjDumper> &Result) {
247 const auto *WasmObj = dyn_cast<WasmObjectFile>(Obj);
248 assert(WasmObj && "createWasmDumper called with non-wasm object");
250 Result.reset(new WasmDumper(WasmObj, Writer));
251 return readobj_error::success;
254 } // namespace llvm