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;
24 class MachOObjectFile;
25 class MachOUniversalBinary;
31 extern cl::opt<std::string> TripleName;
32 extern cl::opt<std::string> ArchName;
33 extern cl::opt<std::string> MCPU;
34 extern cl::list<std::string> MAttrs;
35 extern cl::list<std::string> FilterSections;
36 extern cl::opt<bool> AllHeaders;
37 extern cl::opt<bool> Demangle;
38 extern cl::opt<bool> Disassemble;
39 extern cl::opt<bool> DisassembleAll;
40 extern cl::opt<bool> NoShowRawInsn;
41 extern cl::opt<bool> NoLeadingAddr;
42 extern cl::opt<bool> PrivateHeaders;
43 extern cl::opt<bool> FileHeaders;
44 extern cl::opt<bool> FirstPrivateHeader;
45 extern cl::opt<bool> ExportsTrie;
46 extern cl::opt<bool> Rebase;
47 extern cl::opt<bool> Bind;
48 extern cl::opt<bool> LazyBind;
49 extern cl::opt<bool> WeakBind;
50 extern cl::opt<bool> RawClangAST;
51 extern cl::opt<bool> UniversalHeaders;
52 extern cl::opt<bool> ArchiveHeaders;
53 extern cl::opt<bool> IndirectSymbols;
54 extern cl::opt<bool> DataInCode;
55 extern cl::opt<bool> LinkOptHints;
56 extern cl::opt<bool> InfoPlist;
57 extern cl::opt<bool> DylibsUsed;
58 extern cl::opt<bool> DylibId;
59 extern cl::opt<bool> ObjcMetaData;
60 extern cl::opt<std::string> DisSymName;
61 extern cl::opt<bool> NonVerbose;
62 extern cl::opt<bool> Relocations;
63 extern cl::opt<bool> DynamicRelocations;
64 extern cl::opt<bool> SectionHeaders;
65 extern cl::opt<bool> SectionContents;
66 extern cl::opt<bool> SymbolTable;
67 extern cl::opt<bool> UnwindInfo;
68 extern cl::opt<bool> PrintImmHex;
69 extern cl::opt<DIDumpType> DwarfDumpType;
71 typedef std::function<bool(llvm::object::SectionRef const &)> FilterPredicate;
73 class SectionFilterIterator {
75 SectionFilterIterator(FilterPredicate P,
76 llvm::object::section_iterator const &I,
77 llvm::object::section_iterator const &E)
78 : Predicate(std::move(P)), Iterator(I), End(E) {
81 const llvm::object::SectionRef &operator*() const { return *Iterator; }
82 SectionFilterIterator &operator++() {
87 bool operator!=(SectionFilterIterator const &Other) const {
88 return Iterator != Other.Iterator;
92 void ScanPredicate() {
93 while (Iterator != End && !Predicate(*Iterator)) {
97 FilterPredicate Predicate;
98 llvm::object::section_iterator Iterator;
99 llvm::object::section_iterator End;
102 class SectionFilter {
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());
110 SectionFilterIterator end() {
111 return SectionFilterIterator(Predicate, Object.section_end(),
112 Object.section_end());
116 FilterPredicate Predicate;
117 llvm::object::ObjectFile const &Object;
120 // Various helper functions.
121 SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O);
124 getELFRelocationValueString(const object::ELFObjectFileBase *Obj,
125 const object::RelocationRef &Rel,
126 llvm::SmallVectorImpl<char> &Result);
128 getCOFFRelocationValueString(const object::COFFObjectFile *Obj,
129 const object::RelocationRef &Rel,
130 llvm::SmallVectorImpl<char> &Result);
132 getWasmRelocationValueString(const object::WasmObjectFile *Obj,
133 const object::RelocationRef &RelRef,
134 llvm::SmallVectorImpl<char> &Result);
136 getMachORelocationValueString(const object::MachOObjectFile *Obj,
137 const object::RelocationRef &RelRef,
138 llvm::SmallVectorImpl<char> &Result);
140 void error(std::error_code ec);
141 bool isRelocAddressLess(object::RelocationRef A, object::RelocationRef B);
142 void parseInputMachO(StringRef Filename);
143 void parseInputMachO(object::MachOUniversalBinary *UB);
144 void printCOFFUnwindInfo(const object::COFFObjectFile *O);
145 void printMachOUnwindInfo(const object::MachOObjectFile *O);
146 void printMachOExportsTrie(const object::MachOObjectFile *O);
147 void printMachORebaseTable(object::MachOObjectFile *O);
148 void printMachOBindTable(object::MachOObjectFile *O);
149 void printMachOLazyBindTable(object::MachOObjectFile *O);
150 void printMachOWeakBindTable(object::MachOObjectFile *O);
151 void printELFFileHeader(const object::ObjectFile *O);
152 void printELFDynamicSection(const object::ObjectFile *Obj);
153 void printCOFFFileHeader(const object::ObjectFile *O);
154 void printCOFFSymbolTable(const object::COFFImportFile *I);
155 void printCOFFSymbolTable(const object::COFFObjectFile *O);
156 void printMachOFileHeader(const object::ObjectFile *O);
157 void printMachOLoadCommands(const object::ObjectFile *O);
158 void printWasmFileHeader(const object::ObjectFile *O);
159 void printExportsTrie(const object::ObjectFile *O);
160 void printRebaseTable(object::ObjectFile *O);
161 void printBindTable(object::ObjectFile *O);
162 void printLazyBindTable(object::ObjectFile *O);
163 void printWeakBindTable(object::ObjectFile *O);
164 void printRawClangAST(const object::ObjectFile *O);
165 void printRelocations(const object::ObjectFile *O);
166 void printDynamicRelocations(const object::ObjectFile *O);
167 void printSectionHeaders(const object::ObjectFile *O);
168 void printSectionContents(const object::ObjectFile *O);
169 void printSymbolTable(const object::ObjectFile *O, StringRef ArchiveName,
170 StringRef ArchitectureName = StringRef());
171 void warn(StringRef Message);
172 LLVM_ATTRIBUTE_NORETURN void error(Twine Message);
173 LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, Twine Message);
174 LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, std::error_code EC);
175 LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, llvm::Error E);
176 LLVM_ATTRIBUTE_NORETURN void report_error(StringRef FileName,
177 StringRef ArchiveName,
179 StringRef ArchitectureName
181 LLVM_ATTRIBUTE_NORETURN void report_error(StringRef ArchiveName,
182 const object::Archive::Child &C,
184 StringRef ArchitectureName
187 } // end namespace llvm