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
22 namespace object {
23 class ELFObjectFileBase;
24 class ELFSectionRef;
25 class MachOObjectFile;
26 class MachOUniversalBinary;
27 class RelocationRef;
28 } // namespace object
29
30 namespace objdump {
31
32 extern cl::opt<bool> ArchiveHeaders;
33 extern cl::opt<bool> CapRelocations;
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<bool> PrintImmHex;
44 extern cl::opt<bool> PrivateHeaders;
45 extern cl::opt<bool> Relocations;
46 extern cl::opt<bool> SectionHeaders;
47 extern cl::opt<bool> SectionContents;
48 extern cl::opt<bool> SymbolDescription;
49 extern cl::opt<bool> SymbolTable;
50 extern cl::opt<std::string> TripleName;
51 extern cl::opt<bool> UnwindInfo;
52
53 extern StringSet<> FoundSectionSet;
54
55 typedef std::function<bool(llvm::object::SectionRef const &)> FilterPredicate;
56
57 /// A filtered iterator for SectionRefs that skips sections based on some given
58 /// predicate.
59 class SectionFilterIterator {
60 public:
SectionFilterIterator(FilterPredicate P,llvm::object::section_iterator const & I,llvm::object::section_iterator const & E)61 SectionFilterIterator(FilterPredicate P,
62 llvm::object::section_iterator const &I,
63 llvm::object::section_iterator const &E)
64 : Predicate(std::move(P)), Iterator(I), End(E) {
65 ScanPredicate();
66 }
67 const llvm::object::SectionRef &operator*() const { return *Iterator; }
68 SectionFilterIterator &operator++() {
69 ++Iterator;
70 ScanPredicate();
71 return *this;
72 }
73 bool operator!=(SectionFilterIterator const &Other) const {
74 return Iterator != Other.Iterator;
75 }
76
77 private:
ScanPredicate()78 void ScanPredicate() {
79 while (Iterator != End && !Predicate(*Iterator)) {
80 ++Iterator;
81 }
82 }
83 FilterPredicate Predicate;
84 llvm::object::section_iterator Iterator;
85 llvm::object::section_iterator End;
86 };
87
88 /// Creates an iterator range of SectionFilterIterators for a given Object and
89 /// predicate.
90 class SectionFilter {
91 public:
SectionFilter(FilterPredicate P,llvm::object::ObjectFile const & O)92 SectionFilter(FilterPredicate P, llvm::object::ObjectFile const &O)
93 : Predicate(std::move(P)), Object(O) {}
begin()94 SectionFilterIterator begin() {
95 return SectionFilterIterator(Predicate, Object.section_begin(),
96 Object.section_end());
97 }
end()98 SectionFilterIterator end() {
99 return SectionFilterIterator(Predicate, Object.section_end(),
100 Object.section_end());
101 }
102
103 private:
104 FilterPredicate Predicate;
105 llvm::object::ObjectFile const &Object;
106 };
107
108 // Various helper functions.
109
110 /// Creates a SectionFilter with a standard predicate that conditionally skips
111 /// sections when the --section objdump flag is provided.
112 ///
113 /// Idx is an optional output parameter that keeps track of which section index
114 /// this is. This may be different than the actual section number, as some
115 /// sections may be filtered (e.g. symbol tables).
116 SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O,
117 uint64_t *Idx = nullptr);
118
119 bool isRelocAddressLess(object::RelocationRef A, object::RelocationRef B);
120 void printRelocations(const object::ObjectFile *O);
121 void printCapRelocations(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, Twine Message);
132 LLVM_ATTRIBUTE_NORETURN void reportError(Error E, StringRef FileName,
133 StringRef ArchiveName = "",
134 StringRef ArchitectureName = "");
135 void reportWarning(Twine Message, StringRef File);
136
137 template <typename T, typename... Ts>
unwrapOrError(Expected<T> EO,Ts &&...Args)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