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