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