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/DebugInfo/DIContext.h"
12 #include "llvm/Support/CommandLine.h"
13 #include "llvm/Support/Compiler.h"
14 #include "llvm/Support/DataTypes.h"
15 #include "llvm/Object/Archive.h"
16 
17 namespace llvm {
18 class StringRef;
19 
20 namespace object {
21 class COFFObjectFile;
22 class COFFImportFile;
23 class ELFObjectFileBase;
24 class ELFSectionRef;
25 class MachOObjectFile;
26 class MachOUniversalBinary;
27 class RelocationRef;
28 }
29 
30 extern cl::opt<bool> Demangle;
31 
32 typedef std::function<bool(llvm::object::SectionRef const &)> FilterPredicate;
33 
34 /// A filtered iterator for SectionRefs that skips sections based on some given
35 /// predicate.
36 class SectionFilterIterator {
37 public:
38   SectionFilterIterator(FilterPredicate P,
39                         llvm::object::section_iterator const &I,
40                         llvm::object::section_iterator const &E)
41       : Predicate(std::move(P)), Iterator(I), End(E) {
42     ScanPredicate();
43   }
44   const llvm::object::SectionRef &operator*() const { return *Iterator; }
45   SectionFilterIterator &operator++() {
46     ++Iterator;
47     ScanPredicate();
48     return *this;
49   }
50   bool operator!=(SectionFilterIterator const &Other) const {
51     return Iterator != Other.Iterator;
52   }
53 
54 private:
55   void ScanPredicate() {
56     while (Iterator != End && !Predicate(*Iterator)) {
57       ++Iterator;
58     }
59   }
60   FilterPredicate Predicate;
61   llvm::object::section_iterator Iterator;
62   llvm::object::section_iterator End;
63 };
64 
65 /// Creates an iterator range of SectionFilterIterators for a given Object and
66 /// predicate.
67 class SectionFilter {
68 public:
69   SectionFilter(FilterPredicate P, llvm::object::ObjectFile const &O)
70       : Predicate(std::move(P)), Object(O) {}
71   SectionFilterIterator begin() {
72     return SectionFilterIterator(Predicate, Object.section_begin(),
73                                  Object.section_end());
74   }
75   SectionFilterIterator end() {
76     return SectionFilterIterator(Predicate, Object.section_end(),
77                                  Object.section_end());
78   }
79 
80 private:
81   FilterPredicate Predicate;
82   llvm::object::ObjectFile const &Object;
83 };
84 
85 // Various helper functions.
86 
87 /// Creates a SectionFilter with a standard predicate that conditionally skips
88 /// sections when the --section objdump flag is provided.
89 ///
90 /// Idx is an optional output parameter that keeps track of which section index
91 /// this is. This may be different than the actual section number, as some
92 /// sections may be filtered (e.g. symbol tables).
93 SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O,
94                                 uint64_t *Idx = nullptr);
95 
96 Error getELFRelocationValueString(const object::ELFObjectFileBase *Obj,
97                                   const object::RelocationRef &Rel,
98                                   llvm::SmallVectorImpl<char> &Result);
99 Error getCOFFRelocationValueString(const object::COFFObjectFile *Obj,
100                                    const object::RelocationRef &Rel,
101                                    llvm::SmallVectorImpl<char> &Result);
102 Error getWasmRelocationValueString(const object::WasmObjectFile *Obj,
103                                    const object::RelocationRef &RelRef,
104                                    llvm::SmallVectorImpl<char> &Result);
105 Error getMachORelocationValueString(const object::MachOObjectFile *Obj,
106                                     const object::RelocationRef &RelRef,
107                                     llvm::SmallVectorImpl<char> &Result);
108 
109 uint64_t getELFSectionLMA(const object::ELFSectionRef& Sec);
110 
111 bool isRelocAddressLess(object::RelocationRef A, object::RelocationRef B);
112 void parseInputMachO(StringRef Filename);
113 void parseInputMachO(object::MachOUniversalBinary *UB);
114 void printCOFFUnwindInfo(const object::COFFObjectFile *O);
115 void printMachOUnwindInfo(const object::MachOObjectFile *O);
116 void printMachOExportsTrie(const object::MachOObjectFile *O);
117 void printMachORebaseTable(object::MachOObjectFile *O);
118 void printMachOBindTable(object::MachOObjectFile *O);
119 void printMachOLazyBindTable(object::MachOObjectFile *O);
120 void printMachOWeakBindTable(object::MachOObjectFile *O);
121 void printELFFileHeader(const object::ObjectFile *O);
122 void printELFDynamicSection(const object::ObjectFile *Obj);
123 void printELFSymbolVersionInfo(const object::ObjectFile *Obj);
124 void printCOFFFileHeader(const object::ObjectFile *O);
125 void printCOFFSymbolTable(const object::COFFImportFile *I);
126 void printCOFFSymbolTable(const object::COFFObjectFile *O);
127 void printMachOFileHeader(const object::ObjectFile *O);
128 void printMachOLoadCommands(const object::ObjectFile *O);
129 void printWasmFileHeader(const object::ObjectFile *O);
130 void printExportsTrie(const object::ObjectFile *O);
131 void printRebaseTable(object::ObjectFile *O);
132 void printBindTable(object::ObjectFile *O);
133 void printLazyBindTable(object::ObjectFile *O);
134 void printWeakBindTable(object::ObjectFile *O);
135 void printRawClangAST(const object::ObjectFile *O);
136 void printRelocations(const object::ObjectFile *O);
137 void printDynamicRelocations(const object::ObjectFile *O);
138 void printSectionHeaders(const object::ObjectFile *O);
139 void printSectionContents(const object::ObjectFile *O);
140 void printSymbolTable(const object::ObjectFile *O, StringRef ArchiveName,
141                       StringRef ArchitectureName = StringRef());
142 LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Twine Message);
143 LLVM_ATTRIBUTE_NORETURN void reportError(Error E, StringRef FileName,
144                                          StringRef ArchiveName = "",
145                                          StringRef ArchitectureName = "");
146 void reportWarning(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 
158 } // end namespace llvm
159 
160 #endif
161