llvm-objdump.h 5.2 KB

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