llvm-objdump.h 5.3 KB

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