ObjDumper.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 <memory>
  11. #include <system_error>
  12. #include "llvm/ADT/StringRef.h"
  13. #include "llvm/Object/ObjectFile.h"
  14. #include "llvm/Support/CommandLine.h"
  15. #include <unordered_set>
  16. namespace llvm {
  17. namespace object {
  18. class Archive;
  19. class COFFImportFile;
  20. class ObjectFile;
  21. class XCOFFObjectFile;
  22. class ELFObjectFileBase;
  23. }
  24. namespace codeview {
  25. class GlobalTypeTableBuilder;
  26. class MergingTypeTableBuilder;
  27. } // namespace codeview
  28. class ScopedPrinter;
  29. class ObjDumper {
  30. public:
  31. ObjDumper(ScopedPrinter &Writer, StringRef ObjName);
  32. virtual ~ObjDumper();
  33. virtual bool canDumpContent() { return true; }
  34. virtual void printFileSummary(StringRef FileStr, object::ObjectFile &Obj,
  35. ArrayRef<std::string> InputFilenames,
  36. const object::Archive *A);
  37. virtual void printFileHeaders() = 0;
  38. virtual void printSectionHeaders() = 0;
  39. virtual void printRelocations() = 0;
  40. virtual void printSymbols(bool PrintSymbols, bool PrintDynamicSymbols) {
  41. if (PrintSymbols)
  42. printSymbols();
  43. if (PrintDynamicSymbols)
  44. printDynamicSymbols();
  45. }
  46. virtual void printProgramHeaders(bool PrintProgramHeaders,
  47. cl::boolOrDefault PrintSectionMapping) {
  48. if (PrintProgramHeaders)
  49. printProgramHeaders();
  50. if (PrintSectionMapping == cl::BOU_TRUE)
  51. printSectionMapping();
  52. }
  53. virtual void printUnwindInfo() = 0;
  54. // Only implemented for ELF at this time.
  55. virtual void printDependentLibs() {}
  56. virtual void printDynamicRelocations() { }
  57. virtual void printDynamicTable() { }
  58. virtual void printNeededLibraries() { }
  59. virtual void printSectionAsHex(StringRef SectionName) {}
  60. virtual void printHashTable() { }
  61. virtual void printGnuHashTable() {}
  62. virtual void printHashSymbols() {}
  63. virtual void printLoadName() {}
  64. virtual void printVersionInfo() {}
  65. virtual void printGroupSections() {}
  66. virtual void printHashHistograms() {}
  67. virtual void printCGProfile() {}
  68. virtual void printBBAddrMaps() {}
  69. virtual void printAddrsig() {}
  70. virtual void printNotes() {}
  71. virtual void printELFLinkerOptions() {}
  72. virtual void printStackSizes() {}
  73. virtual void printSectionDetails() {}
  74. virtual void printArchSpecificInfo() {}
  75. // Only implemented for PE/COFF.
  76. virtual void printCOFFImports() { }
  77. virtual void printCOFFExports() { }
  78. virtual void printCOFFDirectives() { }
  79. virtual void printCOFFBaseReloc() { }
  80. virtual void printCOFFDebugDirectory() { }
  81. virtual void printCOFFTLSDirectory() {}
  82. virtual void printCOFFResources() {}
  83. virtual void printCOFFLoadConfig() { }
  84. virtual void printCodeViewDebugInfo() { }
  85. virtual void
  86. mergeCodeViewTypes(llvm::codeview::MergingTypeTableBuilder &CVIDs,
  87. llvm::codeview::MergingTypeTableBuilder &CVTypes,
  88. llvm::codeview::GlobalTypeTableBuilder &GlobalCVIDs,
  89. llvm::codeview::GlobalTypeTableBuilder &GlobalCVTypes,
  90. bool GHash) {}
  91. // Only implement for XCOFF
  92. virtual void printAuxiliaryHeader() {}
  93. // Only implemented for MachO.
  94. virtual void printMachODataInCode() { }
  95. virtual void printMachOVersionMin() { }
  96. virtual void printMachODysymtab() { }
  97. virtual void printMachOSegment() { }
  98. virtual void printMachOIndirectSymbols() { }
  99. virtual void printMachOLinkerOptions() { }
  100. // Currently only implemented for XCOFF.
  101. virtual void printStringTable() { }
  102. virtual void printStackMap() const = 0;
  103. void printAsStringList(StringRef StringContent, size_t StringDataOffset = 0);
  104. void printSectionsAsString(const object::ObjectFile &Obj,
  105. ArrayRef<std::string> Sections);
  106. void printSectionsAsHex(const object::ObjectFile &Obj,
  107. ArrayRef<std::string> Sections);
  108. std::function<Error(const Twine &Msg)> WarningHandler;
  109. void reportUniqueWarning(Error Err) const;
  110. void reportUniqueWarning(const Twine &Msg) const;
  111. protected:
  112. ScopedPrinter &W;
  113. private:
  114. virtual void printSymbols() {}
  115. virtual void printDynamicSymbols() {}
  116. virtual void printProgramHeaders() {}
  117. virtual void printSectionMapping() {}
  118. std::unordered_set<std::string> Warnings;
  119. };
  120. std::unique_ptr<ObjDumper> createCOFFDumper(const object::COFFObjectFile &Obj,
  121. ScopedPrinter &Writer);
  122. std::unique_ptr<ObjDumper> createELFDumper(const object::ELFObjectFileBase &Obj,
  123. ScopedPrinter &Writer);
  124. std::unique_ptr<ObjDumper> createMachODumper(const object::MachOObjectFile &Obj,
  125. ScopedPrinter &Writer);
  126. std::unique_ptr<ObjDumper> createWasmDumper(const object::WasmObjectFile &Obj,
  127. ScopedPrinter &Writer);
  128. std::unique_ptr<ObjDumper> createXCOFFDumper(const object::XCOFFObjectFile &Obj,
  129. ScopedPrinter &Writer);
  130. void dumpCOFFImportFile(const object::COFFImportFile *File,
  131. ScopedPrinter &Writer);
  132. void dumpCodeViewMergedTypes(ScopedPrinter &Writer,
  133. ArrayRef<ArrayRef<uint8_t>> IpiRecords,
  134. ArrayRef<ArrayRef<uint8_t>> TpiRecords);
  135. } // namespace llvm
  136. #endif