1 //===-- WasmDumper.cpp - Wasm-specific object file dumper -----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Wasm-specific dumper for llvm-readobj.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "Error.h"
15 #include "ObjDumper.h"
16 #include "llvm-readobj.h"
17 #include "llvm/Object/Wasm.h"
18 #include "llvm/Support/ScopedPrinter.h"
19 
20 using namespace llvm;
21 using namespace object;
22 
23 namespace {
24 
25 static const EnumEntry<unsigned> WasmSymbolTypes[] = {
26 #define ENUM_ENTRY(X) { #X, wasm::WASM_SYMBOL_TYPE_##X }
27   ENUM_ENTRY(FUNCTION),
28   ENUM_ENTRY(DATA),
29   ENUM_ENTRY(GLOBAL),
30   ENUM_ENTRY(SECTION),
31 #undef ENUM_ENTRY
32 };
33 
34 static const EnumEntry<uint32_t> WasmSectionTypes[] = {
35 #define ENUM_ENTRY(X) { #X, wasm::WASM_SEC_##X }
36   ENUM_ENTRY(CUSTOM),
37   ENUM_ENTRY(TYPE),
38   ENUM_ENTRY(IMPORT),
39   ENUM_ENTRY(FUNCTION),
40   ENUM_ENTRY(TABLE),
41   ENUM_ENTRY(MEMORY),
42   ENUM_ENTRY(GLOBAL),
printDynamicRelocations()43   ENUM_ENTRY(EXPORT),
44   ENUM_ENTRY(START),
45   ENUM_ENTRY(ELEM),
46   ENUM_ENTRY(CODE),
47   ENUM_ENTRY(DATA),
48 #undef ENUM_ENTRY
49 };
printLoadName()50 
51 class WasmDumper : public ObjDumper {
52 public:
53   WasmDumper(const WasmObjectFile *Obj, ScopedPrinter &Writer)
54       : ObjDumper(Writer), Obj(Obj) {}
55 
56   void printFileHeaders() override;
57   void printSections() override;
58   void printRelocations() override;
59   void printSymbols() override;
60   void printDynamicSymbols() override { llvm_unreachable("unimplemented"); }
61   void printUnwindInfo() override { llvm_unreachable("unimplemented"); }
62   void printStackMap() const override { llvm_unreachable("unimplemented"); }
63 
64 protected:
65   void printSymbol(const SymbolRef &Sym);
66   void printRelocation(const SectionRef &Section, const RelocationRef &Reloc);
67 
68 private:
69   const WasmObjectFile *Obj;
70 };
printCOFFDirectives()71 
72 void WasmDumper::printFileHeaders() {
73   W.printHex("Version", Obj->getHeader().Version);
74 }
printCOFFLoadConfig()75 
76 void WasmDumper::printRelocation(const SectionRef &Section,
77                                  const RelocationRef &Reloc) {
78   SmallString<64> RelocTypeName;
79   uint64_t RelocType = Reloc.getType();
80   Reloc.getTypeName(RelocTypeName);
81   const wasm::WasmRelocation &WasmReloc = Obj->getWasmRelocation(Reloc);
82 
83   StringRef SymName;
84   symbol_iterator SI = Reloc.getSymbol();
85   if (SI != Obj->symbol_end())
86     SymName = error(SI->getName());
87 
88   bool HasAddend = false;
89   switch (RelocType) {
90   case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
91   case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
92   case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
93   case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
94   case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32:
95     HasAddend = true;
96     break;
97   default:
98     break;
99   }
100   if (opts::ExpandRelocs) {
101     DictScope Group(W, "Relocation");
102     W.printNumber("Type", RelocTypeName, RelocType);
103     W.printHex("Offset", Reloc.getOffset());
104     if (!SymName.empty())
105       W.printString("Symbol", SymName);
106     else
107       W.printHex("Index", WasmReloc.Index);
108     if (HasAddend)
109       W.printNumber("Addend", WasmReloc.Addend);
110   } else {
111     raw_ostream& OS = W.startLine();
112     OS << W.hex(Reloc.getOffset()) << " " << RelocTypeName << " ";
113     if (!SymName.empty())
114       OS << SymName;
115     else
116       OS << WasmReloc.Index;
117     if (HasAddend)
118       OS << " " << WasmReloc.Addend;
119     OS << "\n";
120   }
121 }
122 
123 void WasmDumper::printRelocations() {
124   ListScope D(W, "Relocations");
125 
126   int SectionNumber = 0;
127   for (const SectionRef &Section : Obj->sections()) {
128     bool PrintedGroup = false;
129     StringRef Name;
130     error(Section.getName(Name));
131     ++SectionNumber;
132 
133     for (const RelocationRef &Reloc : Section.relocations()) {
134       if (!PrintedGroup) {
135         W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
136         W.indent();
137         PrintedGroup = true;
138       }
139 
140       printRelocation(Section, Reloc);
141     }
142 
143     if (PrintedGroup) {
144       W.unindent();
145       W.startLine() << "}\n";
146     }
147   }
148 }
149 
150 void WasmDumper::printSymbols() {
151   ListScope Group(W, "Symbols");
152 
153   for (const SymbolRef &Symbol : Obj->symbols())
154     printSymbol(Symbol);
155 }
156 
157 void WasmDumper::printSections() {
158   ListScope Group(W, "Sections");
159   for (const SectionRef &Section : Obj->sections()) {
160     const WasmSection &WasmSec = Obj->getWasmSection(Section);
161     DictScope SectionD(W, "Section");
162     W.printEnum("Type", WasmSec.Type, makeArrayRef(WasmSectionTypes));
163     W.printNumber("Size", static_cast<uint64_t>(WasmSec.Content.size()));
164     W.printNumber("Offset", WasmSec.Offset);
165     switch (WasmSec.Type) {
166     case wasm::WASM_SEC_CUSTOM:
167       W.printString("Name", WasmSec.Name);
168       if (WasmSec.Name == "linking") {
169         const wasm::WasmLinkingData &LinkingData = Obj->linkingData();
170         if (!LinkingData.InitFunctions.empty()) {
171           ListScope Group(W, "InitFunctions");
172           for (const wasm::WasmInitFunc &F: LinkingData.InitFunctions)
173             W.startLine() << F.Symbol << " (priority=" << F.Priority << ")\n";
174         }
175       }
176       break;
177     case wasm::WASM_SEC_DATA: {
178       ListScope Group(W, "Segments");
179       for (const WasmSegment &Segment : Obj->dataSegments()) {
180         const wasm::WasmDataSegment& Seg = Segment.Data;
181         DictScope Group(W, "Segment");
182         if (!Seg.Name.empty())
183           W.printString("Name", Seg.Name);
184         W.printNumber("Size", static_cast<uint64_t>(Seg.Content.size()));
185         if (Seg.Offset.Opcode == wasm::WASM_OPCODE_I32_CONST)
186           W.printNumber("Offset", Seg.Offset.Value.Int32);
187       }
188       break;
189     }
190     case wasm::WASM_SEC_MEMORY:
191       ListScope Group(W, "Memories");
192       for (const wasm::WasmLimits &Memory : Obj->memories()) {
193         DictScope Group(W, "Memory");
194         W.printNumber("InitialPages", Memory.Initial);
195         if (Memory.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) {
196           W.printNumber("MaxPages", WasmSec.Offset);
197         }
198       }
199       break;
200     }
201 
202     if (opts::SectionRelocations) {
203       ListScope D(W, "Relocations");
204       for (const RelocationRef &Reloc : Section.relocations())
205         printRelocation(Section, Reloc);
206     }
207 
208     if (opts::SectionData) {
209       W.printBinaryBlock("SectionData", WasmSec.Content);
210     }
211   }
212 }
213 
214 void WasmDumper::printSymbol(const SymbolRef &Sym) {
215   DictScope D(W, "Symbol");
216   WasmSymbol Symbol = Obj->getWasmSymbol(Sym.getRawDataRefImpl());
217   W.printString("Name", Symbol.Info.Name);
218   W.printEnum("Type", Symbol.Info.Kind, makeArrayRef(WasmSymbolTypes));
219   W.printHex("Flags", Symbol.Info.Flags);
220 }
221 
222 }
223 
224 namespace llvm {
225 
226 std::error_code createWasmDumper(const object::ObjectFile *Obj,
227                                  ScopedPrinter &Writer,
228                                  std::unique_ptr<ObjDumper> &Result) {
229   const WasmObjectFile *WasmObj = dyn_cast<WasmObjectFile>(Obj);
230   assert(WasmObj && "createWasmDumper called with non-wasm object");
231 
232   Result.reset(new WasmDumper(WasmObj, Writer));
233   return readobj_error::success;
234 }
235 
236 } // namespace llvm
237