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