xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-objdump/llvm-objdump.h (revision 6966ac055c3b7a39266fb982493330df7a097997)
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 class SectionFilterIterator {
35 public:
36   SectionFilterIterator(FilterPredicate P,
37                         llvm::object::section_iterator const &I,
38                         llvm::object::section_iterator const &E)
39       : Predicate(std::move(P)), Iterator(I), End(E) {
40     ScanPredicate();
41   }
42   const llvm::object::SectionRef &operator*() const { return *Iterator; }
43   SectionFilterIterator &operator++() {
44     ++Iterator;
45     ScanPredicate();
46     return *this;
47   }
48   bool operator!=(SectionFilterIterator const &Other) const {
49     return Iterator != Other.Iterator;
50   }
51 
52 private:
53   void ScanPredicate() {
54     while (Iterator != End && !Predicate(*Iterator)) {
55       ++Iterator;
56     }
57   }
58   FilterPredicate Predicate;
59   llvm::object::section_iterator Iterator;
60   llvm::object::section_iterator End;
61 };
62 
63 class SectionFilter {
64 public:
65   SectionFilter(FilterPredicate P, llvm::object::ObjectFile const &O)
66       : Predicate(std::move(P)), Object(O) {}
67   SectionFilterIterator begin() {
68     return SectionFilterIterator(Predicate, Object.section_begin(),
69                                  Object.section_end());
70   }
71   SectionFilterIterator end() {
72     return SectionFilterIterator(Predicate, Object.section_end(),
73                                  Object.section_end());
74   }
75 
76 private:
77   FilterPredicate Predicate;
78   llvm::object::ObjectFile const &Object;
79 };
80 
81 // Various helper functions.
82 SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O);
83 
84 Error getELFRelocationValueString(const object::ELFObjectFileBase *Obj,
85                                   const object::RelocationRef &Rel,
86                                   llvm::SmallVectorImpl<char> &Result);
87 Error getCOFFRelocationValueString(const object::COFFObjectFile *Obj,
88                                    const object::RelocationRef &Rel,
89                                    llvm::SmallVectorImpl<char> &Result);
90 Error getWasmRelocationValueString(const object::WasmObjectFile *Obj,
91                                    const object::RelocationRef &RelRef,
92                                    llvm::SmallVectorImpl<char> &Result);
93 Error getMachORelocationValueString(const object::MachOObjectFile *Obj,
94                                     const object::RelocationRef &RelRef,
95                                     llvm::SmallVectorImpl<char> &Result);
96 
97 uint64_t getELFSectionLMA(const object::ELFSectionRef& Sec);
98 
99 void error(std::error_code ec);
100 void error(Error E);
101 bool isRelocAddressLess(object::RelocationRef A, object::RelocationRef B);
102 void parseInputMachO(StringRef Filename);
103 void parseInputMachO(object::MachOUniversalBinary *UB);
104 void printCOFFUnwindInfo(const object::COFFObjectFile *O);
105 void printMachOUnwindInfo(const object::MachOObjectFile *O);
106 void printMachOExportsTrie(const object::MachOObjectFile *O);
107 void printMachORebaseTable(object::MachOObjectFile *O);
108 void printMachOBindTable(object::MachOObjectFile *O);
109 void printMachOLazyBindTable(object::MachOObjectFile *O);
110 void printMachOWeakBindTable(object::MachOObjectFile *O);
111 void printELFFileHeader(const object::ObjectFile *O);
112 void printELFDynamicSection(const object::ObjectFile *Obj);
113 void printELFSymbolVersionInfo(const object::ObjectFile *Obj);
114 void printCOFFFileHeader(const object::ObjectFile *O);
115 void printCOFFSymbolTable(const object::COFFImportFile *I);
116 void printCOFFSymbolTable(const object::COFFObjectFile *O);
117 void printMachOFileHeader(const object::ObjectFile *O);
118 void printMachOLoadCommands(const object::ObjectFile *O);
119 void printWasmFileHeader(const object::ObjectFile *O);
120 void printExportsTrie(const object::ObjectFile *O);
121 void printRebaseTable(object::ObjectFile *O);
122 void printBindTable(object::ObjectFile *O);
123 void printLazyBindTable(object::ObjectFile *O);
124 void printWeakBindTable(object::ObjectFile *O);
125 void printRawClangAST(const object::ObjectFile *O);
126 void printRelocations(const object::ObjectFile *O);
127 void printDynamicRelocations(const object::ObjectFile *O);
128 void printSectionHeaders(const object::ObjectFile *O);
129 void printSectionContents(const object::ObjectFile *O);
130 void printSymbolTable(const object::ObjectFile *O, StringRef ArchiveName,
131                       StringRef ArchitectureName = StringRef());
132 void warn(StringRef Message);
133 LLVM_ATTRIBUTE_NORETURN void error(Twine Message);
134 LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, Twine Message);
135 LLVM_ATTRIBUTE_NORETURN void report_error(Error E, StringRef File);
136 LLVM_ATTRIBUTE_NORETURN void
137 report_error(Error E, StringRef FileName, StringRef ArchiveName,
138              StringRef ArchitectureName = StringRef());
139 LLVM_ATTRIBUTE_NORETURN void
140 report_error(Error E, StringRef ArchiveName, const object::Archive::Child &C,
141              StringRef ArchitectureName = StringRef());
142 
143 template <typename T, typename... Ts>
144 T unwrapOrError(Expected<T> EO, Ts &&... Args) {
145   if (EO)
146     return std::move(*EO);
147   report_error(EO.takeError(), std::forward<Ts>(Args)...);
148 }
149 
150 } // end namespace llvm
151 
152 #endif
153