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