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