llvm-objdump.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //===--- llvm-objdump.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_OBJDUMP_LLVM_OBJDUMP_H
  9. #define LLVM_TOOLS_LLVM_OBJDUMP_LLVM_OBJDUMP_H
  10. #include "llvm/ADT/StringSet.h"
  11. #include "llvm/DebugInfo/DIContext.h"
  12. #include "llvm/MC/MCDisassembler/MCDisassembler.h"
  13. #include "llvm/Object/Archive.h"
  14. #include "llvm/Support/Compiler.h"
  15. #include "llvm/Support/DataTypes.h"
  16. namespace llvm {
  17. class StringRef;
  18. class Twine;
  19. namespace object {
  20. class RelocationRef;
  21. struct VersionEntry;
  22. } // namespace object
  23. namespace objdump {
  24. enum DebugVarsFormat { DVDisabled, DVUnicode, DVASCII, DVInvalid };
  25. extern bool ArchiveHeaders;
  26. extern int DbgIndent;
  27. extern DebugVarsFormat DbgVariables;
  28. extern bool Demangle;
  29. extern bool Disassemble;
  30. extern bool DisassembleAll;
  31. extern DIDumpType DwarfDumpType;
  32. extern std::vector<std::string> FilterSections;
  33. extern bool LeadingAddr;
  34. extern std::vector<std::string> MAttrs;
  35. extern std::string MCPU;
  36. extern std::string Prefix;
  37. extern uint32_t PrefixStrip;
  38. extern bool PrintImmHex;
  39. extern bool PrintLines;
  40. extern bool PrintSource;
  41. extern bool PrivateHeaders;
  42. extern bool Relocations;
  43. extern bool SectionHeaders;
  44. extern bool SectionContents;
  45. extern bool ShowRawInsn;
  46. extern bool SymbolDescription;
  47. extern bool SymbolTable;
  48. extern std::string TripleName;
  49. extern bool UnwindInfo;
  50. extern StringSet<> FoundSectionSet;
  51. typedef std::function<bool(llvm::object::SectionRef const &)> FilterPredicate;
  52. /// A filtered iterator for SectionRefs that skips sections based on some given
  53. /// predicate.
  54. class SectionFilterIterator {
  55. public:
  56. SectionFilterIterator(FilterPredicate P,
  57. llvm::object::section_iterator const &I,
  58. llvm::object::section_iterator const &E)
  59. : Predicate(std::move(P)), Iterator(I), End(E) {
  60. ScanPredicate();
  61. }
  62. const llvm::object::SectionRef &operator*() const { return *Iterator; }
  63. SectionFilterIterator &operator++() {
  64. ++Iterator;
  65. ScanPredicate();
  66. return *this;
  67. }
  68. bool operator!=(SectionFilterIterator const &Other) const {
  69. return Iterator != Other.Iterator;
  70. }
  71. private:
  72. void ScanPredicate() {
  73. while (Iterator != End && !Predicate(*Iterator)) {
  74. ++Iterator;
  75. }
  76. }
  77. FilterPredicate Predicate;
  78. llvm::object::section_iterator Iterator;
  79. llvm::object::section_iterator End;
  80. };
  81. /// Creates an iterator range of SectionFilterIterators for a given Object and
  82. /// predicate.
  83. class SectionFilter {
  84. public:
  85. SectionFilter(FilterPredicate P, llvm::object::ObjectFile const &O)
  86. : Predicate(std::move(P)), Object(O) {}
  87. SectionFilterIterator begin() {
  88. return SectionFilterIterator(Predicate, Object.section_begin(),
  89. Object.section_end());
  90. }
  91. SectionFilterIterator end() {
  92. return SectionFilterIterator(Predicate, Object.section_end(),
  93. Object.section_end());
  94. }
  95. private:
  96. FilterPredicate Predicate;
  97. llvm::object::ObjectFile const &Object;
  98. };
  99. // Various helper functions.
  100. /// Creates a SectionFilter with a standard predicate that conditionally skips
  101. /// sections when the --section objdump flag is provided.
  102. ///
  103. /// Idx is an optional output parameter that keeps track of which section index
  104. /// this is. This may be different than the actual section number, as some
  105. /// sections may be filtered (e.g. symbol tables).
  106. SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O,
  107. uint64_t *Idx = nullptr);
  108. bool isRelocAddressLess(object::RelocationRef A, object::RelocationRef B);
  109. void printRelocations(const object::ObjectFile *O);
  110. void printDynamicRelocations(const object::ObjectFile *O);
  111. void printSectionHeaders(const object::ObjectFile *O);
  112. void printSectionContents(const object::ObjectFile *O);
  113. void printSymbolTable(const object::ObjectFile *O, StringRef ArchiveName,
  114. StringRef ArchitectureName = StringRef(),
  115. bool DumpDynamic = false);
  116. void printSymbol(const object::ObjectFile *O, const object::SymbolRef &Symbol,
  117. ArrayRef<object::VersionEntry> SymbolVersions,
  118. StringRef FileName, StringRef ArchiveName,
  119. StringRef ArchitectureName, bool DumpDynamic);
  120. [[noreturn]] void reportError(StringRef File, const Twine &Message);
  121. [[noreturn]] void reportError(Error E, StringRef FileName,
  122. StringRef ArchiveName = "",
  123. StringRef ArchitectureName = "");
  124. void reportWarning(const Twine &Message, StringRef File);
  125. template <typename T, typename... Ts>
  126. T unwrapOrError(Expected<T> EO, Ts &&... Args) {
  127. if (EO)
  128. return std::move(*EO);
  129. reportError(EO.takeError(), std::forward<Ts>(Args)...);
  130. }
  131. std::string getFileNameForError(const object::Archive::Child &C,
  132. unsigned Index);
  133. SymbolInfoTy createSymbolInfo(const object::ObjectFile *Obj,
  134. const object::SymbolRef &Symbol);
  135. } // namespace objdump
  136. } // end namespace llvm
  137. #endif