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 } |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +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), |
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), |
Sam Clegg | e450bd7 | 2019-02-07 01:17:34 +0000 | [diff] [blame] | 53 | #undef ENUM_ENTRY |
| 54 | }; |
| 55 | |
Derek Schuff | 6d76b7b | 2017-01-30 23:30:52 +0000 | [diff] [blame] | 56 | class WasmDumper : public ObjDumper { |
| 57 | public: |
| 58 | WasmDumper(const WasmObjectFile *Obj, ScopedPrinter &Writer) |
| 59 | : ObjDumper(Writer), Obj(Obj) {} |
| 60 | |
Sam Clegg | 135a4b8 | 2017-04-14 19:50:44 +0000 | [diff] [blame] | 61 | void printFileHeaders() override; |
Jordan Rupprecht | dbf552c | 2018-11-12 18:02:38 +0000 | [diff] [blame] | 62 | void printSectionHeaders() override; |
Sam Clegg | 135a4b8 | 2017-04-14 19:50:44 +0000 | [diff] [blame] | 63 | void printRelocations() override; |
Derek Schuff | 6d76b7b | 2017-01-30 23:30:52 +0000 | [diff] [blame] | 64 | void printUnwindInfo() override { llvm_unreachable("unimplemented"); } |
| 65 | void printStackMap() const override { llvm_unreachable("unimplemented"); } |
| 66 | |
Sam Clegg | 135a4b8 | 2017-04-14 19:50:44 +0000 | [diff] [blame] | 67 | protected: |
| 68 | void printSymbol(const SymbolRef &Sym); |
| 69 | void printRelocation(const SectionRef &Section, const RelocationRef &Reloc); |
| 70 | |
Derek Schuff | 6d76b7b | 2017-01-30 23:30:52 +0000 | [diff] [blame] | 71 | private: |
James Henderson | 21ed868 | 2019-01-23 16:15:39 +0000 | [diff] [blame] | 72 | void printSymbols() override; |
| 73 | void printDynamicSymbols() override { llvm_unreachable("unimplemented"); } |
| 74 | |
Derek Schuff | 6d76b7b | 2017-01-30 23:30:52 +0000 | [diff] [blame] | 75 | const WasmObjectFile *Obj; |
| 76 | }; |
Sam Clegg | 135a4b8 | 2017-04-14 19:50:44 +0000 | [diff] [blame] | 77 | |
| 78 | void WasmDumper::printFileHeaders() { |
| 79 | W.printHex("Version", Obj->getHeader().Version); |
| 80 | } |
| 81 | |
| 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); |
| 88 | |
Sam Clegg | 7381216 | 2018-05-01 16:35:16 +0000 | [diff] [blame] | 89 | StringRef SymName; |
| 90 | symbol_iterator SI = Reloc.getSymbol(); |
| 91 | if (SI != Obj->symbol_end()) |
| 92 | SymName = error(SI->getName()); |
| 93 | |
Sam Clegg | 10545c9 | 2017-04-28 00:36:36 +0000 | [diff] [blame] | 94 | bool HasAddend = false; |
| 95 | switch (RelocType) { |
Sam Clegg | d1152a2 | 2019-02-04 17:28:46 +0000 | [diff] [blame] | 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: |
Sam Clegg | 10545c9 | 2017-04-28 00:36:36 +0000 | [diff] [blame] | 101 | HasAddend = true; |
| 102 | break; |
| 103 | default: |
| 104 | break; |
| 105 | } |
Sam Clegg | 135a4b8 | 2017-04-14 19:50:44 +0000 | [diff] [blame] | 106 | if (opts::ExpandRelocs) { |
| 107 | DictScope Group(W, "Relocation"); |
| 108 | W.printNumber("Type", RelocTypeName, RelocType); |
| 109 | W.printHex("Offset", Reloc.getOffset()); |
Sam Clegg | 7381216 | 2018-05-01 16:35:16 +0000 | [diff] [blame] | 110 | if (!SymName.empty()) |
| 111 | W.printString("Symbol", SymName); |
| 112 | else |
| 113 | W.printHex("Index", WasmReloc.Index); |
Sam Clegg | 10545c9 | 2017-04-28 00:36:36 +0000 | [diff] [blame] | 114 | if (HasAddend) |
| 115 | W.printNumber("Addend", WasmReloc.Addend); |
Sam Clegg | 135a4b8 | 2017-04-14 19:50:44 +0000 | [diff] [blame] | 116 | } else { |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 117 | raw_ostream &OS = W.startLine(); |
Sam Clegg | 7381216 | 2018-05-01 16:35:16 +0000 | [diff] [blame] | 118 | OS << W.hex(Reloc.getOffset()) << " " << RelocTypeName << " "; |
| 119 | if (!SymName.empty()) |
| 120 | OS << SymName; |
| 121 | else |
| 122 | OS << WasmReloc.Index; |
Sam Clegg | 10545c9 | 2017-04-28 00:36:36 +0000 | [diff] [blame] | 123 | if (HasAddend) |
| 124 | OS << " " << WasmReloc.Addend; |
| 125 | OS << "\n"; |
Sam Clegg | 135a4b8 | 2017-04-14 19:50:44 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | void WasmDumper::printRelocations() { |
| 130 | ListScope D(W, "Relocations"); |
| 131 | |
| 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; |
| 138 | |
| 139 | for (const RelocationRef &Reloc : Section.relocations()) { |
| 140 | if (!PrintedGroup) { |
| 141 | W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n"; |
| 142 | W.indent(); |
| 143 | PrintedGroup = true; |
| 144 | } |
| 145 | |
| 146 | printRelocation(Section, Reloc); |
| 147 | } |
| 148 | |
| 149 | if (PrintedGroup) { |
| 150 | W.unindent(); |
| 151 | W.startLine() << "}\n"; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void WasmDumper::printSymbols() { |
| 157 | ListScope Group(W, "Symbols"); |
| 158 | |
| 159 | for (const SymbolRef &Symbol : Obj->symbols()) |
| 160 | printSymbol(Symbol); |
| 161 | } |
| 162 | |
Jordan Rupprecht | dbf552c | 2018-11-12 18:02:38 +0000 | [diff] [blame] | 163 | void WasmDumper::printSectionHeaders() { |
Sam Clegg | 135a4b8 | 2017-04-14 19:50:44 +0000 | [diff] [blame] | 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)); |
Sam Clegg | d95ed95 | 2017-09-20 19:03:35 +0000 | [diff] [blame] | 169 | W.printNumber("Size", static_cast<uint64_t>(WasmSec.Content.size())); |
Sam Clegg | 135a4b8 | 2017-04-14 19:50:44 +0000 | [diff] [blame] | 170 | W.printNumber("Offset", WasmSec.Offset); |
Sam Clegg | ff0730b | 2017-04-28 21:12:09 +0000 | [diff] [blame] | 171 | switch (WasmSec.Type) { |
| 172 | case wasm::WASM_SEC_CUSTOM: |
Sam Clegg | 135a4b8 | 2017-04-14 19:50:44 +0000 | [diff] [blame] | 173 | W.printString("Name", WasmSec.Name); |
Sam Clegg | 14612fb | 2017-07-10 20:47:12 +0000 | [diff] [blame] | 174 | if (WasmSec.Name == "linking") { |
| 175 | const wasm::WasmLinkingData &LinkingData = Obj->linkingData(); |
Sam Clegg | b23a201 | 2017-12-19 00:04:41 +0000 | [diff] [blame] | 176 | if (!LinkingData.InitFunctions.empty()) { |
| 177 | ListScope Group(W, "InitFunctions"); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 178 | for (const wasm::WasmInitFunc &F : LinkingData.InitFunctions) |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 179 | W.startLine() << F.Symbol << " (priority=" << F.Priority << ")\n"; |
Sam Clegg | b23a201 | 2017-12-19 00:04:41 +0000 | [diff] [blame] | 180 | } |
Sam Clegg | 14612fb | 2017-07-10 20:47:12 +0000 | [diff] [blame] | 181 | } |
Sam Clegg | ff0730b | 2017-04-28 21:12:09 +0000 | [diff] [blame] | 182 | break; |
Sam Clegg | d95ed95 | 2017-09-20 19:03:35 +0000 | [diff] [blame] | 183 | case wasm::WASM_SEC_DATA: { |
| 184 | ListScope Group(W, "Segments"); |
| 185 | for (const WasmSegment &Segment : Obj->dataSegments()) { |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 186 | const wasm::WasmDataSegment &Seg = Segment.Data; |
Sam Clegg | d95ed95 | 2017-09-20 19:03:35 +0000 | [diff] [blame] | 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); |
| 193 | } |
| 194 | break; |
| 195 | } |
Sam Clegg | ff0730b | 2017-04-28 21:12:09 +0000 | [diff] [blame] | 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); |
| 203 | } |
| 204 | } |
| 205 | break; |
Sam Clegg | 135a4b8 | 2017-04-14 19:50:44 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | if (opts::SectionRelocations) { |
| 209 | ListScope D(W, "Relocations"); |
| 210 | for (const RelocationRef &Reloc : Section.relocations()) |
| 211 | printRelocation(Section, Reloc); |
| 212 | } |
| 213 | |
| 214 | if (opts::SectionData) { |
| 215 | W.printBinaryBlock("SectionData", WasmSec.Content); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | void WasmDumper::printSymbol(const SymbolRef &Sym) { |
| 221 | DictScope D(W, "Symbol"); |
| 222 | WasmSymbol Symbol = Obj->getWasmSymbol(Sym.getRawDataRefImpl()); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 223 | W.printString("Name", Symbol.Info.Name); |
| 224 | W.printEnum("Type", Symbol.Info.Kind, makeArrayRef(WasmSymbolTypes)); |
Sam Clegg | e450bd7 | 2019-02-07 01:17:34 +0000 | [diff] [blame] | 225 | W.printFlags("Flags", Symbol.Info.Flags, makeArrayRef(WasmSymbolFlags)); |
| 226 | |
Dan Gohman | 29874ce | 2019-02-07 22:03:32 +0000 | [diff] [blame^] | 227 | if (Symbol.Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) { |
| 228 | W.printString("ImportName", Symbol.Info.ImportName); |
| 229 | W.printString("ImportModule", Symbol.Info.ImportModule); |
| 230 | } |
Sam Clegg | 3fd2462 | 2019-02-04 22:27:46 +0000 | [diff] [blame] | 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); |
| 237 | } |
Sam Clegg | 135a4b8 | 2017-04-14 19:50:44 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 240 | } // namespace |
Derek Schuff | 6d76b7b | 2017-01-30 23:30:52 +0000 | [diff] [blame] | 241 | |
| 242 | namespace llvm { |
| 243 | |
| 244 | std::error_code createWasmDumper(const object::ObjectFile *Obj, |
| 245 | ScopedPrinter &Writer, |
| 246 | std::unique_ptr<ObjDumper> &Result) { |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 247 | const auto *WasmObj = dyn_cast<WasmObjectFile>(Obj); |
Derek Schuff | 6d76b7b | 2017-01-30 23:30:52 +0000 | [diff] [blame] | 248 | assert(WasmObj && "createWasmDumper called with non-wasm object"); |
| 249 | |
| 250 | Result.reset(new WasmDumper(WasmObj, Writer)); |
| 251 | return readobj_error::success; |
| 252 | } |
| 253 | |
| 254 | } // namespace llvm |