WasmDumper.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "ObjDumper.h"
  13. #include "llvm-readobj.h"
  14. #include "llvm/Object/Wasm.h"
  15. #include "llvm/Support/ScopedPrinter.h"
  16. using namespace llvm;
  17. using namespace object;
  18. namespace {
  19. const EnumEntry<unsigned> WasmSymbolTypes[] = {
  20. #define ENUM_ENTRY(X) \
  21. { #X, wasm::WASM_SYMBOL_TYPE_##X }
  22. ENUM_ENTRY(FUNCTION), ENUM_ENTRY(DATA), ENUM_ENTRY(GLOBAL),
  23. ENUM_ENTRY(SECTION), ENUM_ENTRY(TAG), ENUM_ENTRY(TABLE),
  24. #undef ENUM_ENTRY
  25. };
  26. const EnumEntry<uint32_t> WasmSectionTypes[] = {
  27. #define ENUM_ENTRY(X) \
  28. { #X, wasm::WASM_SEC_##X }
  29. ENUM_ENTRY(CUSTOM), ENUM_ENTRY(TYPE), ENUM_ENTRY(IMPORT),
  30. ENUM_ENTRY(FUNCTION), ENUM_ENTRY(TABLE), ENUM_ENTRY(MEMORY),
  31. ENUM_ENTRY(GLOBAL), ENUM_ENTRY(TAG), ENUM_ENTRY(EXPORT),
  32. ENUM_ENTRY(START), ENUM_ENTRY(ELEM), ENUM_ENTRY(CODE),
  33. ENUM_ENTRY(DATA), ENUM_ENTRY(DATACOUNT),
  34. #undef ENUM_ENTRY
  35. };
  36. const EnumEntry<unsigned> WasmSymbolFlags[] = {
  37. #define ENUM_ENTRY(X) \
  38. { #X, wasm::WASM_SYMBOL_##X }
  39. ENUM_ENTRY(BINDING_GLOBAL),
  40. ENUM_ENTRY(BINDING_WEAK),
  41. ENUM_ENTRY(BINDING_LOCAL),
  42. ENUM_ENTRY(VISIBILITY_DEFAULT),
  43. ENUM_ENTRY(VISIBILITY_HIDDEN),
  44. ENUM_ENTRY(UNDEFINED),
  45. ENUM_ENTRY(EXPORTED),
  46. ENUM_ENTRY(EXPLICIT_NAME),
  47. ENUM_ENTRY(NO_STRIP),
  48. #undef ENUM_ENTRY
  49. };
  50. class WasmDumper : public ObjDumper {
  51. public:
  52. WasmDumper(const WasmObjectFile *Obj, ScopedPrinter &Writer)
  53. : ObjDumper(Writer, Obj->getFileName()), Obj(Obj) {}
  54. void printFileHeaders() override;
  55. void printSectionHeaders() override;
  56. void printRelocations() override;
  57. void printUnwindInfo() override { llvm_unreachable("unimplemented"); }
  58. void printStackMap() const override { llvm_unreachable("unimplemented"); }
  59. protected:
  60. void printSymbol(const SymbolRef &Sym);
  61. void printRelocation(const SectionRef &Section, const RelocationRef &Reloc);
  62. private:
  63. void printSymbols() override;
  64. void printDynamicSymbols() override { llvm_unreachable("unimplemented"); }
  65. const WasmObjectFile *Obj;
  66. };
  67. void WasmDumper::printFileHeaders() {
  68. W.printHex("Version", Obj->getHeader().Version);
  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. StringRef SymName;
  77. symbol_iterator SI = Reloc.getSymbol();
  78. if (SI != Obj->symbol_end())
  79. SymName = unwrapOrError(Obj->getFileName(), SI->getName());
  80. bool HasAddend = wasm::relocTypeHasAddend(static_cast<uint32_t>(RelocType));
  81. if (opts::ExpandRelocs) {
  82. DictScope Group(W, "Relocation");
  83. W.printNumber("Type", RelocTypeName, RelocType);
  84. W.printHex("Offset", Reloc.getOffset());
  85. if (!SymName.empty())
  86. W.printString("Symbol", SymName);
  87. else
  88. W.printHex("Index", WasmReloc.Index);
  89. if (HasAddend)
  90. W.printNumber("Addend", WasmReloc.Addend);
  91. } else {
  92. raw_ostream &OS = W.startLine();
  93. OS << W.hex(Reloc.getOffset()) << " " << RelocTypeName << " ";
  94. if (!SymName.empty())
  95. OS << SymName;
  96. else
  97. OS << WasmReloc.Index;
  98. if (HasAddend)
  99. OS << " " << WasmReloc.Addend;
  100. OS << "\n";
  101. }
  102. }
  103. void WasmDumper::printRelocations() {
  104. ListScope D(W, "Relocations");
  105. int SectionNumber = 0;
  106. for (const SectionRef &Section : Obj->sections()) {
  107. bool PrintedGroup = false;
  108. StringRef Name = unwrapOrError(Obj->getFileName(), Section.getName());
  109. ++SectionNumber;
  110. for (const RelocationRef &Reloc : Section.relocations()) {
  111. if (!PrintedGroup) {
  112. W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
  113. W.indent();
  114. PrintedGroup = true;
  115. }
  116. printRelocation(Section, Reloc);
  117. }
  118. if (PrintedGroup) {
  119. W.unindent();
  120. W.startLine() << "}\n";
  121. }
  122. }
  123. }
  124. void WasmDumper::printSymbols() {
  125. ListScope Group(W, "Symbols");
  126. for (const SymbolRef &Symbol : Obj->symbols())
  127. printSymbol(Symbol);
  128. }
  129. void WasmDumper::printSectionHeaders() {
  130. ListScope Group(W, "Sections");
  131. for (const SectionRef &Section : Obj->sections()) {
  132. const WasmSection &WasmSec = Obj->getWasmSection(Section);
  133. DictScope SectionD(W, "Section");
  134. W.printEnum("Type", WasmSec.Type, ArrayRef(WasmSectionTypes));
  135. W.printNumber("Size", static_cast<uint64_t>(WasmSec.Content.size()));
  136. W.printNumber("Offset", WasmSec.Offset);
  137. switch (WasmSec.Type) {
  138. case wasm::WASM_SEC_CUSTOM:
  139. W.printString("Name", WasmSec.Name);
  140. if (WasmSec.Name == "linking") {
  141. const wasm::WasmLinkingData &LinkingData = Obj->linkingData();
  142. if (!LinkingData.InitFunctions.empty()) {
  143. ListScope Group(W, "InitFunctions");
  144. for (const wasm::WasmInitFunc &F : LinkingData.InitFunctions)
  145. W.startLine() << F.Symbol << " (priority=" << F.Priority << ")\n";
  146. }
  147. }
  148. break;
  149. case wasm::WASM_SEC_DATA: {
  150. ListScope Group(W, "Segments");
  151. for (const WasmSegment &Segment : Obj->dataSegments()) {
  152. const wasm::WasmDataSegment &Seg = Segment.Data;
  153. DictScope Group(W, "Segment");
  154. if (!Seg.Name.empty())
  155. W.printString("Name", Seg.Name);
  156. W.printNumber("Size", static_cast<uint64_t>(Seg.Content.size()));
  157. if (Seg.Offset.Extended)
  158. llvm_unreachable("extended const exprs not supported");
  159. else if (Seg.Offset.Inst.Opcode == wasm::WASM_OPCODE_I32_CONST)
  160. W.printNumber("Offset", Seg.Offset.Inst.Value.Int32);
  161. else if (Seg.Offset.Inst.Opcode == wasm::WASM_OPCODE_I64_CONST)
  162. W.printNumber("Offset", Seg.Offset.Inst.Value.Int64);
  163. else if (Seg.Offset.Inst.Opcode == wasm::WASM_OPCODE_GLOBAL_GET) {
  164. ListScope Group(W, "Offset");
  165. W.printNumber("Global", Seg.Offset.Inst.Value.Global);
  166. } else
  167. llvm_unreachable("unknown init expr opcode");
  168. }
  169. break;
  170. }
  171. case wasm::WASM_SEC_MEMORY:
  172. ListScope Group(W, "Memories");
  173. for (const wasm::WasmLimits &Memory : Obj->memories()) {
  174. DictScope Group(W, "Memory");
  175. W.printNumber("MinPages", Memory.Minimum);
  176. if (Memory.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) {
  177. W.printNumber("MaxPages", WasmSec.Offset);
  178. }
  179. }
  180. break;
  181. }
  182. if (opts::SectionRelocations) {
  183. ListScope D(W, "Relocations");
  184. for (const RelocationRef &Reloc : Section.relocations())
  185. printRelocation(Section, Reloc);
  186. }
  187. if (opts::SectionData) {
  188. W.printBinaryBlock("SectionData", WasmSec.Content);
  189. }
  190. }
  191. }
  192. void WasmDumper::printSymbol(const SymbolRef &Sym) {
  193. DictScope D(W, "Symbol");
  194. WasmSymbol Symbol = Obj->getWasmSymbol(Sym.getRawDataRefImpl());
  195. W.printString("Name", Symbol.Info.Name);
  196. W.printEnum("Type", Symbol.Info.Kind, ArrayRef(WasmSymbolTypes));
  197. W.printFlags("Flags", Symbol.Info.Flags, ArrayRef(WasmSymbolFlags));
  198. if (Symbol.Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) {
  199. if (Symbol.Info.ImportName) {
  200. W.printString("ImportName", *Symbol.Info.ImportName);
  201. }
  202. if (Symbol.Info.ImportModule) {
  203. W.printString("ImportModule", *Symbol.Info.ImportModule);
  204. }
  205. }
  206. if (Symbol.Info.Kind != wasm::WASM_SYMBOL_TYPE_DATA) {
  207. W.printHex("ElementIndex", Symbol.Info.ElementIndex);
  208. } else if (!(Symbol.Info.Flags & wasm::WASM_SYMBOL_UNDEFINED)) {
  209. W.printHex("Offset", Symbol.Info.DataRef.Offset);
  210. W.printHex("Segment", Symbol.Info.DataRef.Segment);
  211. W.printHex("Size", Symbol.Info.DataRef.Size);
  212. }
  213. }
  214. } // namespace
  215. namespace llvm {
  216. std::unique_ptr<ObjDumper> createWasmDumper(const object::WasmObjectFile &Obj,
  217. ScopedPrinter &Writer) {
  218. return std::make_unique<WasmDumper>(&Obj, Writer);
  219. }
  220. } // namespace llvm