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