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