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