blob: 03c72b74dbb1338cf0d08afac32215186237c562 [file] [log] [blame]
Benjamin Kramer43a772e2011-09-19 17:56:04 +00001//
Chandler Carruth2946cd72019-01-19 08:50:56 +00002// 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
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005//
6//===----------------------------------------------------------------------===//
7
Benjamin Kramera7c40ef2014-08-13 16:26:38 +00008#ifndef LLVM_TOOLS_LLVM_OBJDUMP_LLVM_OBJDUMP_H
9#define LLVM_TOOLS_LLVM_OBJDUMP_LLVM_OBJDUMP_H
Benjamin Kramer43a772e2011-09-19 17:56:04 +000010
Igor Laevsky03a670c2016-01-26 15:09:42 +000011#include "llvm/DebugInfo/DIContext.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000012#include "llvm/Support/CommandLine.h"
Davide Italianoed9d95b2015-12-29 13:41:02 +000013#include "llvm/Support/Compiler.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000014#include "llvm/Support/DataTypes.h"
Kevin Enderbyac9e1552016-05-17 17:10:12 +000015#include "llvm/Object/Archive.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000016
17namespace llvm {
Mehdi Aminib550cb12016-04-18 09:17:29 +000018class StringRef;
19
Michael J. Spencer0c6ec482012-12-05 20:12:35 +000020namespace object {
George Rimarc1964882019-01-18 11:33:26 +000021class COFFObjectFile;
22class COFFImportFile;
23class ELFObjectFileBase;
24class MachOObjectFile;
25class MachOUniversalBinary;
26class ObjectFile;
27class Archive;
28class RelocationRef;
Michael J. Spencer0c6ec482012-12-05 20:12:35 +000029}
Michael J. Spencer0c6ec482012-12-05 20:12:35 +000030
Benjamin Kramer43a772e2011-09-19 17:56:04 +000031extern cl::opt<std::string> TripleName;
32extern cl::opt<std::string> ArchName;
Kevin Enderbyc9595622014-08-06 23:24:41 +000033extern cl::opt<std::string> MCPU;
34extern cl::list<std::string> MAttrs;
Colin LeMahieufcc32762015-07-29 19:08:10 +000035extern cl::list<std::string> FilterSections;
Fangrui Song8513cd42018-06-27 20:45:11 +000036extern cl::opt<bool> AllHeaders;
Zachary Turner030ad372018-08-20 22:18:21 +000037extern cl::opt<bool> Demangle;
Colin LeMahieu77804be2015-07-29 15:45:39 +000038extern cl::opt<bool> Disassemble;
Colin LeMahieuf34933e2015-07-23 20:58:49 +000039extern cl::opt<bool> DisassembleAll;
Kevin Enderbybf246f52014-09-24 23:08:22 +000040extern cl::opt<bool> NoShowRawInsn;
Saleem Abdulrasooldea14b22017-02-08 18:11:31 +000041extern cl::opt<bool> NoLeadingAddr;
Kevin Enderbye2297dd2015-01-07 21:02:18 +000042extern cl::opt<bool> PrivateHeaders;
Paul Semeld2af4d62018-07-04 15:25:03 +000043extern cl::opt<bool> FileHeaders;
Kevin Enderby0ae163f2016-01-13 00:25:36 +000044extern cl::opt<bool> FirstPrivateHeader;
Kevin Enderbye2297dd2015-01-07 21:02:18 +000045extern cl::opt<bool> ExportsTrie;
46extern cl::opt<bool> Rebase;
47extern cl::opt<bool> Bind;
48extern cl::opt<bool> LazyBind;
49extern cl::opt<bool> WeakBind;
Adrian Prantl437105a2015-07-08 02:04:15 +000050extern cl::opt<bool> RawClangAST;
Kevin Enderby131d1772015-01-09 19:22:37 +000051extern cl::opt<bool> UniversalHeaders;
Kevin Enderby13023a12015-01-15 23:19:11 +000052extern cl::opt<bool> ArchiveHeaders;
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +000053extern cl::opt<bool> IndirectSymbols;
Kevin Enderby69fe98d2015-01-23 18:52:17 +000054extern cl::opt<bool> DataInCode;
Kevin Enderby9a509442015-01-27 21:28:24 +000055extern cl::opt<bool> LinkOptHints;
Kevin Enderbycd66be52015-03-11 22:06:32 +000056extern cl::opt<bool> InfoPlist;
Kevin Enderbybc847fa2015-03-16 20:08:09 +000057extern cl::opt<bool> DylibsUsed;
58extern cl::opt<bool> DylibId;
Kevin Enderby0fc11822015-04-01 20:57:01 +000059extern cl::opt<bool> ObjcMetaData;
Kevin Enderby6a221752015-03-17 17:10:57 +000060extern cl::opt<std::string> DisSymName;
Kevin Enderbyf0640752015-03-13 17:56:32 +000061extern cl::opt<bool> NonVerbose;
Kevin Enderby98da6132015-01-20 21:47:46 +000062extern cl::opt<bool> Relocations;
Paul Semelcb0f0432018-06-07 13:30:55 +000063extern cl::opt<bool> DynamicRelocations;
Kevin Enderby98da6132015-01-20 21:47:46 +000064extern cl::opt<bool> SectionHeaders;
65extern cl::opt<bool> SectionContents;
66extern cl::opt<bool> SymbolTable;
67extern cl::opt<bool> UnwindInfo;
Colin LeMahieu14ec76e2015-06-07 21:07:17 +000068extern cl::opt<bool> PrintImmHex;
Igor Laevsky03a670c2016-01-26 15:09:42 +000069extern cl::opt<DIDumpType> DwarfDumpType;
Benjamin Kramer43a772e2011-09-19 17:56:04 +000070
George Rimarc1964882019-01-18 11:33:26 +000071typedef std::function<bool(llvm::object::SectionRef const &)> FilterPredicate;
72
73class SectionFilterIterator {
74public:
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) {
79 ScanPredicate();
80 }
81 const llvm::object::SectionRef &operator*() const { return *Iterator; }
82 SectionFilterIterator &operator++() {
83 ++Iterator;
84 ScanPredicate();
85 return *this;
86 }
87 bool operator!=(SectionFilterIterator const &Other) const {
88 return Iterator != Other.Iterator;
89 }
90
91private:
92 void ScanPredicate() {
93 while (Iterator != End && !Predicate(*Iterator)) {
94 ++Iterator;
95 }
96 }
97 FilterPredicate Predicate;
98 llvm::object::section_iterator Iterator;
99 llvm::object::section_iterator End;
100};
101
102class SectionFilter {
103public:
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());
109 }
110 SectionFilterIterator end() {
111 return SectionFilterIterator(Predicate, Object.section_end(),
112 Object.section_end());
113 }
114
115private:
116 FilterPredicate Predicate;
117 llvm::object::ObjectFile const &Object;
118};
119
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000120// Various helper functions.
George Rimarc1964882019-01-18 11:33:26 +0000121SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O);
122
123std::error_code
124getELFRelocationValueString(const object::ELFObjectFileBase *Obj,
125 const object::RelocationRef &Rel,
126 llvm::SmallVectorImpl<char> &Result);
127std::error_code
128getCOFFRelocationValueString(const object::COFFObjectFile *Obj,
129 const object::RelocationRef &Rel,
130 llvm::SmallVectorImpl<char> &Result);
131std::error_code
132getWasmRelocationValueString(const object::WasmObjectFile *Obj,
133 const object::RelocationRef &RelRef,
134 llvm::SmallVectorImpl<char> &Result);
135std::error_code
136getMachORelocationValueString(const object::MachOObjectFile *Obj,
137 const object::RelocationRef &RelRef,
138 llvm::SmallVectorImpl<char> &Result);
139
Davide Italianoccd53fe2015-08-05 07:18:31 +0000140void error(std::error_code ec);
George Rimar73a27232019-01-15 09:19:18 +0000141bool isRelocAddressLess(object::RelocationRef A, object::RelocationRef B);
142void parseInputMachO(StringRef Filename);
143void parseInputMachO(object::MachOUniversalBinary *UB);
144void printCOFFUnwindInfo(const object::COFFObjectFile *O);
145void printMachOUnwindInfo(const object::MachOObjectFile *O);
146void printMachOExportsTrie(const object::MachOObjectFile *O);
147void printMachORebaseTable(object::MachOObjectFile *O);
148void printMachOBindTable(object::MachOObjectFile *O);
149void printMachOLazyBindTable(object::MachOObjectFile *O);
150void printMachOWeakBindTable(object::MachOObjectFile *O);
151void printELFFileHeader(const object::ObjectFile *O);
Paul Semel0913dcd2018-07-25 11:09:20 +0000152void printELFDynamicSection(const object::ObjectFile *Obj);
George Rimar73a27232019-01-15 09:19:18 +0000153void printCOFFFileHeader(const object::ObjectFile *O);
154void printCOFFSymbolTable(const object::COFFImportFile *I);
155void printCOFFSymbolTable(const object::COFFObjectFile *O);
156void printMachOFileHeader(const object::ObjectFile *O);
157void printMachOLoadCommands(const object::ObjectFile *O);
158void printWasmFileHeader(const object::ObjectFile *O);
159void printExportsTrie(const object::ObjectFile *O);
160void printRebaseTable(object::ObjectFile *O);
161void printBindTable(object::ObjectFile *O);
162void printLazyBindTable(object::ObjectFile *O);
163void printWeakBindTable(object::ObjectFile *O);
164void printRawClangAST(const object::ObjectFile *O);
165void printRelocations(const object::ObjectFile *O);
166void printDynamicRelocations(const object::ObjectFile *O);
167void printSectionHeaders(const object::ObjectFile *O);
168void printSectionContents(const object::ObjectFile *O);
169void printSymbolTable(const object::ObjectFile *O, StringRef ArchiveName,
Kevin Enderby9acb1092016-05-31 20:35:34 +0000170 StringRef ArchitectureName = StringRef());
Paul Semel007dedb2018-07-18 16:39:21 +0000171void warn(StringRef Message);
Kevin Enderby42398052016-06-28 23:16:13 +0000172LLVM_ATTRIBUTE_NORETURN void error(Twine Message);
Kevin Enderby7fa40c92016-11-16 22:17:38 +0000173LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, Twine Message);
Davide Italianoed9d95b2015-12-29 13:41:02 +0000174LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, std::error_code EC);
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000175LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, llvm::Error E);
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000176LLVM_ATTRIBUTE_NORETURN void report_error(StringRef FileName,
177 StringRef ArchiveName,
Kevin Enderby9acb1092016-05-31 20:35:34 +0000178 llvm::Error E,
179 StringRef ArchitectureName
180 = StringRef());
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000181LLVM_ATTRIBUTE_NORETURN void report_error(StringRef ArchiveName,
182 const object::Archive::Child &C,
Kevin Enderby9acb1092016-05-31 20:35:34 +0000183 llvm::Error E,
184 StringRef ArchitectureName
185 = StringRef());
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000186
Rui Ueyamac2bed422013-09-27 21:04:00 +0000187} // end namespace llvm
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000188
189#endif