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