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