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;
32 extern cl::opt<std::string> TripleName;
33 extern cl::opt<std::string> ArchName;
34 extern cl::opt<std::string> MCPU;
35 extern cl::list<std::string> MAttrs;
36 extern cl::list<std::string> FilterSections;
37 extern cl::opt<bool> AllHeaders;
38 extern cl::opt<bool> Demangle;
39 extern cl::opt<bool> Disassemble;
40 extern cl::opt<bool> DisassembleAll;
41 extern cl::opt<bool> NoShowRawInsn;
42 extern cl::opt<bool> NoLeadingAddr;
43 extern cl::opt<bool> PrivateHeaders;
44 extern cl::opt<bool> FileHeaders;
45 extern cl::opt<bool> FirstPrivateHeader;
46 extern cl::opt<bool> ExportsTrie;
47 extern cl::opt<bool> Rebase;
48 extern cl::opt<bool> Bind;
49 extern cl::opt<bool> LazyBind;
50 extern cl::opt<bool> WeakBind;
51 extern cl::opt<bool> RawClangAST;
52 extern cl::opt<bool> UniversalHeaders;
53 extern cl::opt<bool> ArchiveHeaders;
54 extern cl::opt<bool> IndirectSymbols;
55 extern cl::opt<bool> DataInCode;
56 extern cl::opt<bool> LinkOptHints;
57 extern cl::opt<bool> InfoPlist;
58 extern cl::opt<bool> DylibsUsed;
59 extern cl::opt<bool> DylibId;
60 extern cl::opt<bool> ObjcMetaData;
61 extern cl::opt<std::string> DisSymName;
62 extern cl::opt<bool> NonVerbose;
63 extern cl::opt<bool> Relocations;
64 extern cl::opt<bool> DynamicRelocations;
65 extern cl::opt<bool> SectionHeaders;
66 extern cl::opt<bool> SectionContents;
67 extern cl::opt<bool> SymbolTable;
68 extern cl::opt<bool> UnwindInfo;
69 extern cl::opt<bool> PrintImmHex;
70 extern cl::opt<DIDumpType> DwarfDumpType;
72 typedef std::function<bool(llvm::object::SectionRef const &)> FilterPredicate;
74 class SectionFilterIterator {
76 SectionFilterIterator(FilterPredicate P,
77 llvm::object::section_iterator const &I,
78 llvm::object::section_iterator const &E)
79 : Predicate(std::move(P)), Iterator(I), End(E) {
82 const llvm::object::SectionRef &operator*() const { return *Iterator; }
83 SectionFilterIterator &operator++() {
88 bool operator!=(SectionFilterIterator const &Other) const {
89 return Iterator != Other.Iterator;
93 void ScanPredicate() {
94 while (Iterator != End && !Predicate(*Iterator)) {
98 FilterPredicate Predicate;
99 llvm::object::section_iterator Iterator;
100 llvm::object::section_iterator End;
103 class SectionFilter {
105 SectionFilter(FilterPredicate P, llvm::object::ObjectFile const &O)
106 : Predicate(std::move(P)), Object(O) {}
107 SectionFilterIterator begin() {
108 return SectionFilterIterator(Predicate, Object.section_begin(),
109 Object.section_end());
111 SectionFilterIterator end() {
112 return SectionFilterIterator(Predicate, Object.section_end(),
113 Object.section_end());
117 FilterPredicate Predicate;
118 llvm::object::ObjectFile const &Object;
121 // Various helper functions.
122 SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O);
124 Error getELFRelocationValueString(const object::ELFObjectFileBase *Obj,
125 const object::RelocationRef &Rel,
126 llvm::SmallVectorImpl<char> &Result);
127 Error getCOFFRelocationValueString(const object::COFFObjectFile *Obj,
128 const object::RelocationRef &Rel,
129 llvm::SmallVectorImpl<char> &Result);
130 Error getWasmRelocationValueString(const object::WasmObjectFile *Obj,
131 const object::RelocationRef &RelRef,
132 llvm::SmallVectorImpl<char> &Result);
133 Error getMachORelocationValueString(const object::MachOObjectFile *Obj,
134 const object::RelocationRef &RelRef,
135 llvm::SmallVectorImpl<char> &Result);
137 uint64_t getELFSectionLMA(const object::ELFSectionRef& Sec);
139 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 printELFSymbolVersionInfo(const object::ObjectFile *Obj);
154 void printCOFFFileHeader(const object::ObjectFile *O);
155 void printCOFFSymbolTable(const object::COFFImportFile *I);
156 void printCOFFSymbolTable(const object::COFFObjectFile *O);
157 void printMachOFileHeader(const object::ObjectFile *O);
158 void printMachOLoadCommands(const object::ObjectFile *O);
159 void printWasmFileHeader(const object::ObjectFile *O);
160 void printExportsTrie(const object::ObjectFile *O);
161 void printRebaseTable(object::ObjectFile *O);
162 void printBindTable(object::ObjectFile *O);
163 void printLazyBindTable(object::ObjectFile *O);
164 void printWeakBindTable(object::ObjectFile *O);
165 void printRawClangAST(const object::ObjectFile *O);
166 void printRelocations(const object::ObjectFile *O);
167 void printDynamicRelocations(const object::ObjectFile *O);
168 void printSectionHeaders(const object::ObjectFile *O);
169 void printSectionContents(const object::ObjectFile *O);
170 void printSymbolTable(const object::ObjectFile *O, StringRef ArchiveName,
171 StringRef ArchitectureName = StringRef());
172 void warn(StringRef Message);
173 LLVM_ATTRIBUTE_NORETURN void error(Twine Message);
174 LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, Twine Message);
175 LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, std::error_code EC);
176 LLVM_ATTRIBUTE_NORETURN void report_error(Error E, StringRef File);
177 LLVM_ATTRIBUTE_NORETURN void
178 report_error(Error E, StringRef FileName, StringRef ArchiveName,
179 StringRef ArchitectureName = StringRef());
180 LLVM_ATTRIBUTE_NORETURN void
181 report_error(Error E, StringRef ArchiveName, const object::Archive::Child &C,
182 StringRef ArchitectureName = StringRef());
184 template <typename T, typename... Ts>
185 T unwrapOrError(Expected<T> EO, Ts &&... Args) {
187 return std::move(*EO);
188 report_error(EO.takeError(), std::forward<Ts>(Args)...);
191 } // end namespace llvm