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