ObjDumper.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //===-- ObjDumper.h ---------------------------------------------*- C++ -*-===//
  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. #ifndef LLVM_TOOLS_LLVM_READOBJ_OBJDUMPER_H
  9. #define LLVM_TOOLS_LLVM_READOBJ_OBJDUMPER_H
  10. #include <functional>
  11. #include <memory>
  12. #include <system_error>
  13. #include "llvm/ADT/STLFunctionalExtras.h"
  14. #include "llvm/ADT/SmallVector.h"
  15. #include "llvm/ADT/StringMap.h"
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/Object/ObjectFile.h"
  18. #include "llvm/Support/CommandLine.h"
  19. #include <unordered_set>
  20. namespace llvm {
  21. namespace object {
  22. class Archive;
  23. class COFFImportFile;
  24. class ObjectFile;
  25. class XCOFFObjectFile;
  26. class ELFObjectFileBase;
  27. } // namespace object
  28. namespace codeview {
  29. class GlobalTypeTableBuilder;
  30. class MergingTypeTableBuilder;
  31. } // namespace codeview
  32. class ScopedPrinter;
  33. // Comparator to compare symbols.
  34. // Usage: the caller registers predicates (i.e., how to compare the symbols) by
  35. // calling addPredicate(). The order in which predicates are registered is also
  36. // their priority.
  37. class SymbolComparator {
  38. public:
  39. using CompPredicate =
  40. std::function<bool(object::SymbolRef, object::SymbolRef)>;
  41. // Each Obj format has a slightly different way of retrieving a symbol's info
  42. // So we defer the predicate's impl to each format.
  43. void addPredicate(CompPredicate Pred) { Predicates.push_back(Pred); }
  44. bool operator()(object::SymbolRef LHS, object::SymbolRef RHS) {
  45. for (CompPredicate Pred : Predicates) {
  46. if (Pred(LHS, RHS))
  47. return true;
  48. if (Pred(RHS, LHS))
  49. return false;
  50. }
  51. return false;
  52. }
  53. private:
  54. SmallVector<CompPredicate, 2> Predicates;
  55. };
  56. class ObjDumper {
  57. public:
  58. ObjDumper(ScopedPrinter &Writer, StringRef ObjName);
  59. virtual ~ObjDumper();
  60. virtual bool canDumpContent() { return true; }
  61. virtual void printFileSummary(StringRef FileStr, object::ObjectFile &Obj,
  62. ArrayRef<std::string> InputFilenames,
  63. const object::Archive *A);
  64. virtual void printFileHeaders() = 0;
  65. virtual void printSectionHeaders() = 0;
  66. virtual void printRelocations() = 0;
  67. virtual void printSymbols(bool PrintSymbols, bool PrintDynamicSymbols) {
  68. if (PrintSymbols)
  69. printSymbols();
  70. if (PrintDynamicSymbols)
  71. printDynamicSymbols();
  72. }
  73. virtual void printSymbols(bool PrintSymbols, bool PrintDynamicSymbols,
  74. std::optional<SymbolComparator> SymComp) {
  75. if (SymComp) {
  76. if (PrintSymbols)
  77. printSymbols(SymComp);
  78. if (PrintDynamicSymbols)
  79. printDynamicSymbols(SymComp);
  80. } else {
  81. printSymbols(PrintSymbols, PrintDynamicSymbols);
  82. }
  83. }
  84. virtual void printProgramHeaders(bool PrintProgramHeaders,
  85. cl::boolOrDefault PrintSectionMapping) {
  86. if (PrintProgramHeaders)
  87. printProgramHeaders();
  88. if (PrintSectionMapping == cl::BOU_TRUE)
  89. printSectionMapping();
  90. }
  91. virtual void printUnwindInfo() = 0;
  92. // Symbol comparison functions.
  93. virtual bool canCompareSymbols() const { return false; }
  94. virtual bool compareSymbolsByName(object::SymbolRef LHS,
  95. object::SymbolRef RHS) const {
  96. return true;
  97. }
  98. virtual bool compareSymbolsByType(object::SymbolRef LHS,
  99. object::SymbolRef RHS) const {
  100. return true;
  101. }
  102. // Only implemented for ELF at this time.
  103. virtual void printDependentLibs() {}
  104. virtual void printDynamicRelocations() { }
  105. virtual void printDynamicTable() { }
  106. virtual void printNeededLibraries() { }
  107. virtual void printSectionAsHex(StringRef SectionName) {}
  108. virtual void printHashTable() { }
  109. virtual void printGnuHashTable() {}
  110. virtual void printHashSymbols() {}
  111. virtual void printLoadName() {}
  112. virtual void printVersionInfo() {}
  113. virtual void printGroupSections() {}
  114. virtual void printHashHistograms() {}
  115. virtual void printCGProfile() {}
  116. virtual void printBBAddrMaps() {}
  117. virtual void printAddrsig() {}
  118. virtual void printNotes() {}
  119. virtual void printELFLinkerOptions() {}
  120. virtual void printStackSizes() {}
  121. virtual void printSectionDetails() {}
  122. virtual void printArchSpecificInfo() {}
  123. // Only implemented for PE/COFF.
  124. virtual void printCOFFImports() { }
  125. virtual void printCOFFExports() { }
  126. virtual void printCOFFDirectives() { }
  127. virtual void printCOFFBaseReloc() { }
  128. virtual void printCOFFDebugDirectory() { }
  129. virtual void printCOFFTLSDirectory() {}
  130. virtual void printCOFFResources() {}
  131. virtual void printCOFFLoadConfig() { }
  132. virtual void printCodeViewDebugInfo() { }
  133. virtual void
  134. mergeCodeViewTypes(llvm::codeview::MergingTypeTableBuilder &CVIDs,
  135. llvm::codeview::MergingTypeTableBuilder &CVTypes,
  136. llvm::codeview::GlobalTypeTableBuilder &GlobalCVIDs,
  137. llvm::codeview::GlobalTypeTableBuilder &GlobalCVTypes,
  138. bool GHash) {}
  139. // Only implemented for XCOFF.
  140. virtual void printStringTable() {}
  141. virtual void printAuxiliaryHeader() {}
  142. virtual void printExceptionSection() {}
  143. virtual void printLoaderSection(bool PrintHeader, bool PrintSymbols,
  144. bool PrintRelocations) {}
  145. // Only implemented for MachO.
  146. virtual void printMachODataInCode() { }
  147. virtual void printMachOVersionMin() { }
  148. virtual void printMachODysymtab() { }
  149. virtual void printMachOSegment() { }
  150. virtual void printMachOIndirectSymbols() { }
  151. virtual void printMachOLinkerOptions() { }
  152. virtual void printStackMap() const = 0;
  153. void printAsStringList(StringRef StringContent, size_t StringDataOffset = 0);
  154. void printSectionsAsString(const object::ObjectFile &Obj,
  155. ArrayRef<std::string> Sections);
  156. void printSectionsAsHex(const object::ObjectFile &Obj,
  157. ArrayRef<std::string> Sections);
  158. std::function<Error(const Twine &Msg)> WarningHandler;
  159. void reportUniqueWarning(Error Err) const;
  160. void reportUniqueWarning(const Twine &Msg) const;
  161. protected:
  162. ScopedPrinter &W;
  163. private:
  164. virtual void printSymbols() {}
  165. virtual void printSymbols(std::optional<SymbolComparator> Comp) {}
  166. virtual void printDynamicSymbols() {}
  167. virtual void printDynamicSymbols(std::optional<SymbolComparator> Comp) {}
  168. virtual void printProgramHeaders() {}
  169. virtual void printSectionMapping() {}
  170. std::unordered_set<std::string> Warnings;
  171. };
  172. std::unique_ptr<ObjDumper> createCOFFDumper(const object::COFFObjectFile &Obj,
  173. ScopedPrinter &Writer);
  174. std::unique_ptr<ObjDumper> createELFDumper(const object::ELFObjectFileBase &Obj,
  175. ScopedPrinter &Writer);
  176. std::unique_ptr<ObjDumper> createMachODumper(const object::MachOObjectFile &Obj,
  177. ScopedPrinter &Writer);
  178. std::unique_ptr<ObjDumper> createWasmDumper(const object::WasmObjectFile &Obj,
  179. ScopedPrinter &Writer);
  180. std::unique_ptr<ObjDumper> createXCOFFDumper(const object::XCOFFObjectFile &Obj,
  181. ScopedPrinter &Writer);
  182. void dumpCOFFImportFile(const object::COFFImportFile *File,
  183. ScopedPrinter &Writer);
  184. void dumpCodeViewMergedTypes(ScopedPrinter &Writer,
  185. ArrayRef<ArrayRef<uint8_t>> IpiRecords,
  186. ArrayRef<ArrayRef<uint8_t>> TpiRecords);
  187. } // namespace llvm
  188. #endif