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
6 //===----------------------------------------------------------------------===//
8 #ifndef LLVM_TOOLS_LLVM_OBJDUMP_LLVM_OBJDUMP_H
9 #define LLVM_TOOLS_LLVM_OBJDUMP_LLVM_OBJDUMP_H
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"
23 class ELFObjectFileBase
;
25 class MachOObjectFile
;
26 class MachOUniversalBinary
;
30 extern cl::opt
<bool> Demangle
;
32 typedef std::function
<bool(llvm::object::SectionRef
const &)> FilterPredicate
;
34 class SectionFilterIterator
{
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
) {
42 const llvm::object::SectionRef
&operator*() const { return *Iterator
; }
43 SectionFilterIterator
&operator++() {
48 bool operator!=(SectionFilterIterator
const &Other
) const {
49 return Iterator
!= Other
.Iterator
;
53 void ScanPredicate() {
54 while (Iterator
!= End
&& !Predicate(*Iterator
)) {
58 FilterPredicate Predicate
;
59 llvm::object::section_iterator Iterator
;
60 llvm::object::section_iterator End
;
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());
71 SectionFilterIterator
end() {
72 return SectionFilterIterator(Predicate
, Object
.section_end(),
73 Object
.section_end());
77 FilterPredicate Predicate
;
78 llvm::object::ObjectFile
const &Object
;
81 // Various helper functions.
82 SectionFilter
ToolSectionFilter(llvm::object::ObjectFile
const &O
);
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
);
97 uint64_t getELFSectionLMA(const object::ELFSectionRef
& Sec
);
99 void error(std::error_code ec
);
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());
143 template <typename T
, typename
... Ts
>
144 T
unwrapOrError(Expected
<T
> EO
, Ts
&&... Args
) {
146 return std::move(*EO
);
147 report_error(EO
.takeError(), std::forward
<Ts
>(Args
)...);
150 } // end namespace llvm