blob: e559b72aeb7ac23477bf5eac51afa5569614d438 [file] [log] [blame]
Benjamin Kramer0b8b7712011-09-19 17:56:04 +00001//
Chandler Carruth6b547682019-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 Kramer0b8b7712011-09-19 17:56:04 +00005//
6//===----------------------------------------------------------------------===//
7
Benjamin Kramer00e08fc2014-08-13 16:26:38 +00008#ifndef LLVM_TOOLS_LLVM_OBJDUMP_LLVM_OBJDUMP_H
9#define LLVM_TOOLS_LLVM_OBJDUMP_LLVM_OBJDUMP_H
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000010
Igor Laevsky25574802016-01-26 15:09:42 +000011#include "llvm/DebugInfo/DIContext.h"
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000012#include "llvm/Support/CommandLine.h"
Davide Italianoec0fb282015-12-29 13:41:02 +000013#include "llvm/Support/Compiler.h"
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000014#include "llvm/Support/DataTypes.h"
Kevin Enderby77be0942016-05-17 17:10:12 +000015#include "llvm/Object/Archive.h"
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000016
17namespace llvm {
Mehdi Aminif6071e12016-04-18 09:17:29 +000018class StringRef;
19
Michael J. Spencereef7b622012-12-05 20:12:35 +000020namespace object {
George Rimar605942e2019-01-18 11:33:26 +000021class COFFObjectFile;
22class COFFImportFile;
23class ELFObjectFileBase;
George Rimar47718732019-01-28 14:11:35 +000024class ELFSectionRef;
George Rimar605942e2019-01-18 11:33:26 +000025class MachOObjectFile;
26class MachOUniversalBinary;
27class ObjectFile;
28class Archive;
29class RelocationRef;
Michael J. Spencereef7b622012-12-05 20:12:35 +000030}
Michael J. Spencereef7b622012-12-05 20:12:35 +000031
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000032extern cl::opt<std::string> TripleName;
33extern cl::opt<std::string> ArchName;
Kevin Enderby75d423f2014-08-06 23:24:41 +000034extern cl::opt<std::string> MCPU;
35extern cl::list<std::string> MAttrs;
Colin LeMahieu154b3f02015-07-29 19:08:10 +000036extern cl::list<std::string> FilterSections;
Fangrui Song84e5dbf2018-06-27 20:45:11 +000037extern cl::opt<bool> AllHeaders;
Zachary Turnerc04ae042018-08-20 22:18:21 +000038extern cl::opt<bool> Demangle;
Colin LeMahieuedbf9d72015-07-29 15:45:39 +000039extern cl::opt<bool> Disassemble;
Colin LeMahieuea805022015-07-23 20:58:49 +000040extern cl::opt<bool> DisassembleAll;
Kevin Enderbye7938622014-09-24 23:08:22 +000041extern cl::opt<bool> NoShowRawInsn;
Saleem Abdulrasool5bbd1892017-02-08 18:11:31 +000042extern cl::opt<bool> NoLeadingAddr;
Kevin Enderby60e9ca42015-01-07 21:02:18 +000043extern cl::opt<bool> PrivateHeaders;
Paul Semeld069a212018-07-04 15:25:03 +000044extern cl::opt<bool> FileHeaders;
Kevin Enderbyf3e60262016-01-13 00:25:36 +000045extern cl::opt<bool> FirstPrivateHeader;
Kevin Enderby60e9ca42015-01-07 21:02:18 +000046extern cl::opt<bool> ExportsTrie;
47extern cl::opt<bool> Rebase;
48extern cl::opt<bool> Bind;
49extern cl::opt<bool> LazyBind;
50extern cl::opt<bool> WeakBind;
Adrian Prantl54a27682015-07-08 02:04:15 +000051extern cl::opt<bool> RawClangAST;
Kevin Enderby6248d1b2015-01-09 19:22:37 +000052extern cl::opt<bool> UniversalHeaders;
Kevin Enderbycdfe54f2015-01-15 23:19:11 +000053extern cl::opt<bool> ArchiveHeaders;
Kevin Enderbybcbb8692015-01-22 18:55:27 +000054extern cl::opt<bool> IndirectSymbols;
Kevin Enderby66e2ddc2015-01-23 18:52:17 +000055extern cl::opt<bool> DataInCode;
Kevin Enderbya167fe12015-01-27 21:28:24 +000056extern cl::opt<bool> LinkOptHints;
Kevin Enderby8284f0f2015-03-11 22:06:32 +000057extern cl::opt<bool> InfoPlist;
Kevin Enderby62507242015-03-16 20:08:09 +000058extern cl::opt<bool> DylibsUsed;
59extern cl::opt<bool> DylibId;
Kevin Enderby95d81552015-04-01 20:57:01 +000060extern cl::opt<bool> ObjcMetaData;
Kevin Enderby73cd697d2015-03-17 17:10:57 +000061extern cl::opt<std::string> DisSymName;
Kevin Enderby300c79d2015-03-13 17:56:32 +000062extern cl::opt<bool> NonVerbose;
Kevin Enderbyc97fb732015-01-20 21:47:46 +000063extern cl::opt<bool> Relocations;
Paul Semel3fc706582018-06-07 13:30:55 +000064extern cl::opt<bool> DynamicRelocations;
Kevin Enderbyc97fb732015-01-20 21:47:46 +000065extern cl::opt<bool> SectionHeaders;
66extern cl::opt<bool> SectionContents;
67extern cl::opt<bool> SymbolTable;
68extern cl::opt<bool> UnwindInfo;
Colin LeMahieu8cc8dbc2015-06-07 21:07:17 +000069extern cl::opt<bool> PrintImmHex;
Igor Laevsky25574802016-01-26 15:09:42 +000070extern cl::opt<DIDumpType> DwarfDumpType;
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000071
George Rimar605942e2019-01-18 11:33:26 +000072typedef std::function<bool(llvm::object::SectionRef const &)> FilterPredicate;
73
74class SectionFilterIterator {
75public:
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) {
80 ScanPredicate();
81 }
82 const llvm::object::SectionRef &operator*() const { return *Iterator; }
83 SectionFilterIterator &operator++() {
84 ++Iterator;
85 ScanPredicate();
86 return *this;
87 }
88 bool operator!=(SectionFilterIterator const &Other) const {
89 return Iterator != Other.Iterator;
90 }
91
92private:
93 void ScanPredicate() {
94 while (Iterator != End && !Predicate(*Iterator)) {
95 ++Iterator;
96 }
97 }
98 FilterPredicate Predicate;
99 llvm::object::section_iterator Iterator;
100 llvm::object::section_iterator End;
101};
102
103class SectionFilter {
104public:
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());
110 }
111 SectionFilterIterator end() {
112 return SectionFilterIterator(Predicate, Object.section_end(),
113 Object.section_end());
114 }
115
116private:
117 FilterPredicate Predicate;
118 llvm::object::ObjectFile const &Object;
119};
120
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000121// Various helper functions.
George Rimar605942e2019-01-18 11:33:26 +0000122SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O);
123
124std::error_code
125getELFRelocationValueString(const object::ELFObjectFileBase *Obj,
126 const object::RelocationRef &Rel,
127 llvm::SmallVectorImpl<char> &Result);
128std::error_code
129getCOFFRelocationValueString(const object::COFFObjectFile *Obj,
130 const object::RelocationRef &Rel,
131 llvm::SmallVectorImpl<char> &Result);
132std::error_code
133getWasmRelocationValueString(const object::WasmObjectFile *Obj,
134 const object::RelocationRef &RelRef,
135 llvm::SmallVectorImpl<char> &Result);
136std::error_code
137getMachORelocationValueString(const object::MachOObjectFile *Obj,
138 const object::RelocationRef &RelRef,
139 llvm::SmallVectorImpl<char> &Result);
140
George Rimar47718732019-01-28 14:11:35 +0000141uint64_t getELFSectionLMA(const object::ELFSectionRef& Sec);
142
Davide Italiano281c1b02015-08-05 07:18:31 +0000143void error(std::error_code ec);
George Rimar45d116d2019-01-15 09:19:18 +0000144bool isRelocAddressLess(object::RelocationRef A, object::RelocationRef B);
145void parseInputMachO(StringRef Filename);
146void parseInputMachO(object::MachOUniversalBinary *UB);
147void printCOFFUnwindInfo(const object::COFFObjectFile *O);
148void printMachOUnwindInfo(const object::MachOObjectFile *O);
149void printMachOExportsTrie(const object::MachOObjectFile *O);
150void printMachORebaseTable(object::MachOObjectFile *O);
151void printMachOBindTable(object::MachOObjectFile *O);
152void printMachOLazyBindTable(object::MachOObjectFile *O);
153void printMachOWeakBindTable(object::MachOObjectFile *O);
154void printELFFileHeader(const object::ObjectFile *O);
Paul Semel05d358b2018-07-25 11:09:20 +0000155void printELFDynamicSection(const object::ObjectFile *Obj);
George Rimar45d116d2019-01-15 09:19:18 +0000156void printCOFFFileHeader(const object::ObjectFile *O);
157void printCOFFSymbolTable(const object::COFFImportFile *I);
158void printCOFFSymbolTable(const object::COFFObjectFile *O);
159void printMachOFileHeader(const object::ObjectFile *O);
160void printMachOLoadCommands(const object::ObjectFile *O);
161void printWasmFileHeader(const object::ObjectFile *O);
162void printExportsTrie(const object::ObjectFile *O);
163void printRebaseTable(object::ObjectFile *O);
164void printBindTable(object::ObjectFile *O);
165void printLazyBindTable(object::ObjectFile *O);
166void printWeakBindTable(object::ObjectFile *O);
167void printRawClangAST(const object::ObjectFile *O);
168void printRelocations(const object::ObjectFile *O);
169void printDynamicRelocations(const object::ObjectFile *O);
170void printSectionHeaders(const object::ObjectFile *O);
171void printSectionContents(const object::ObjectFile *O);
172void printSymbolTable(const object::ObjectFile *O, StringRef ArchiveName,
Kevin Enderbye9ddf3a2016-05-31 20:35:34 +0000173 StringRef ArchitectureName = StringRef());
Paul Semelbaea3f02018-07-18 16:39:21 +0000174void warn(StringRef Message);
Kevin Enderbyc827f2c2016-06-28 23:16:13 +0000175LLVM_ATTRIBUTE_NORETURN void error(Twine Message);
Kevin Enderbyfbd189d2016-11-16 22:17:38 +0000176LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, Twine Message);
Davide Italianoec0fb282015-12-29 13:41:02 +0000177LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, std::error_code EC);
Kevin Enderbyc6bf9be2016-04-06 22:14:09 +0000178LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, llvm::Error E);
Kevin Enderby77be0942016-05-17 17:10:12 +0000179LLVM_ATTRIBUTE_NORETURN void report_error(StringRef FileName,
180 StringRef ArchiveName,
Kevin Enderbye9ddf3a2016-05-31 20:35:34 +0000181 llvm::Error E,
182 StringRef ArchitectureName
183 = StringRef());
Kevin Enderby77be0942016-05-17 17:10:12 +0000184LLVM_ATTRIBUTE_NORETURN void report_error(StringRef ArchiveName,
185 const object::Archive::Child &C,
Kevin Enderbye9ddf3a2016-05-31 20:35:34 +0000186 llvm::Error E,
187 StringRef ArchitectureName
188 = StringRef());
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000189
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000190} // end namespace llvm
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000191
192#endif