blob: 94022693cc21326e4624ad8d53c7735acb42f5df [file] [log] [blame]
Michael J. Spencer2670c252011-01-20 06:39:06 +00001//===-- llvm-objdump.cpp - Object file dumping utility for llvm -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This program is a utility that works like binutils "objdump", that is, it
11// dumps out a plethora of information about an object file depending on the
12// flags.
13//
Michael J. Spencerd7e70032013-02-05 20:27:22 +000014// The flags and output of this program should be near identical to those of
15// binutils objdump.
16//
Michael J. Spencer2670c252011-01-20 06:39:06 +000017//===----------------------------------------------------------------------===//
18
Benjamin Kramer43a772e2011-09-19 17:56:04 +000019#include "llvm-objdump.h"
Sanjoy Das6f567a42015-06-22 18:03:02 +000020#include "llvm/ADT/Optional.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000021#include "llvm/ADT/STLExtras.h"
Michael J. Spencer4e25c022011-10-17 17:13:22 +000022#include "llvm/ADT/StringExtras.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000023#include "llvm/ADT/Triple.h"
Sanjoy Das3f1bc3b2015-06-23 20:09:03 +000024#include "llvm/CodeGen/FaultMaps.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000025#include "llvm/MC/MCAsmInfo.h"
Ahmed Bougachaad1084d2013-05-24 00:39:57 +000026#include "llvm/MC/MCContext.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000027#include "llvm/MC/MCDisassembler.h"
28#include "llvm/MC/MCInst.h"
29#include "llvm/MC/MCInstPrinter.h"
Ahmed Bougachaaa790682013-05-24 01:07:04 +000030#include "llvm/MC/MCInstrAnalysis.h"
Craig Topper54bfde72012-04-02 06:09:36 +000031#include "llvm/MC/MCInstrInfo.h"
Ahmed Bougachaad1084d2013-05-24 00:39:57 +000032#include "llvm/MC/MCObjectFileInfo.h"
Jim Grosbachfd93a592012-03-05 19:33:20 +000033#include "llvm/MC/MCRegisterInfo.h"
Ahmed Bougachaad1084d2013-05-24 00:39:57 +000034#include "llvm/MC/MCRelocationInfo.h"
Ahmed Bougachaaa790682013-05-24 01:07:04 +000035#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000036#include "llvm/Object/Archive.h"
Rafael Espindola37070a52015-06-03 04:48:06 +000037#include "llvm/Object/ELFObjectFile.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000038#include "llvm/Object/COFF.h"
Rafael Espindolaa9f810b2012-12-21 03:47:03 +000039#include "llvm/Object/MachO.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000040#include "llvm/Object/ObjectFile.h"
Michael J. Spencerba4a3622011-10-08 00:18:30 +000041#include "llvm/Support/Casting.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000042#include "llvm/Support/CommandLine.h"
43#include "llvm/Support/Debug.h"
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +000044#include "llvm/Support/Errc.h"
Michael J. Spencerba4a3622011-10-08 00:18:30 +000045#include "llvm/Support/FileSystem.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000046#include "llvm/Support/Format.h"
Benjamin Kramerbf115312011-07-25 23:04:36 +000047#include "llvm/Support/GraphWriter.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000048#include "llvm/Support/Host.h"
49#include "llvm/Support/ManagedStatic.h"
50#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000051#include "llvm/Support/PrettyStackTrace.h"
52#include "llvm/Support/Signals.h"
53#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000054#include "llvm/Support/TargetRegistry.h"
55#include "llvm/Support/TargetSelect.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000056#include "llvm/Support/raw_ostream.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000057#include <algorithm>
Benjamin Kramera5177e62012-03-23 11:49:32 +000058#include <cctype>
Michael J. Spencer2670c252011-01-20 06:39:06 +000059#include <cstring>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000060#include <system_error>
Ahmed Bougacha17926472013-08-21 07:29:02 +000061
Michael J. Spencer2670c252011-01-20 06:39:06 +000062using namespace llvm;
63using namespace object;
64
Benjamin Kramer43a772e2011-09-19 17:56:04 +000065static cl::list<std::string>
66InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
Michael J. Spencer2670c252011-01-20 06:39:06 +000067
Kevin Enderbye2297dd2015-01-07 21:02:18 +000068cl::opt<bool>
69llvm::Disassemble("disassemble",
Benjamin Kramer43a772e2011-09-19 17:56:04 +000070 cl::desc("Display assembler mnemonics for the machine instructions"));
71static cl::alias
72Disassembled("d", cl::desc("Alias for --disassemble"),
Colin LeMahieu77804be2015-07-29 15:45:39 +000073 cl::aliasopt(Disassemble));
74
75cl::opt<bool>
76llvm::DisassembleAll("disassemble-all",
77 cl::desc("Display assembler mnemonics for the machine instructions"));
78static cl::alias
79DisassembleAlld("D", cl::desc("Alias for --disassemble-all"),
Colin LeMahieuf34933e2015-07-23 20:58:49 +000080 cl::aliasopt(DisassembleAll));
Michael J. Spencer2670c252011-01-20 06:39:06 +000081
Kevin Enderby98da6132015-01-20 21:47:46 +000082cl::opt<bool>
83llvm::Relocations("r", cl::desc("Display the relocation entries in the file"));
Michael J. Spencerba4a3622011-10-08 00:18:30 +000084
Kevin Enderby98da6132015-01-20 21:47:46 +000085cl::opt<bool>
86llvm::SectionContents("s", cl::desc("Display the content of each section"));
Michael J. Spencer4e25c022011-10-17 17:13:22 +000087
Kevin Enderby98da6132015-01-20 21:47:46 +000088cl::opt<bool>
89llvm::SymbolTable("t", cl::desc("Display the symbol table"));
Michael J. Spencerbfa06782011-10-18 19:32:17 +000090
Kevin Enderbye2297dd2015-01-07 21:02:18 +000091cl::opt<bool>
92llvm::ExportsTrie("exports-trie", cl::desc("Display mach-o exported symbols"));
Nick Kledzikd04bc352014-08-30 00:20:14 +000093
Kevin Enderbye2297dd2015-01-07 21:02:18 +000094cl::opt<bool>
95llvm::Rebase("rebase", cl::desc("Display mach-o rebasing info"));
Nick Kledzikac431442014-09-12 21:34:15 +000096
Kevin Enderbye2297dd2015-01-07 21:02:18 +000097cl::opt<bool>
98llvm::Bind("bind", cl::desc("Display mach-o binding info"));
Nick Kledzik56ebef42014-09-16 01:41:51 +000099
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000100cl::opt<bool>
101llvm::LazyBind("lazy-bind", cl::desc("Display mach-o lazy binding info"));
Nick Kledzik56ebef42014-09-16 01:41:51 +0000102
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000103cl::opt<bool>
104llvm::WeakBind("weak-bind", cl::desc("Display mach-o weak binding info"));
Nick Kledzik56ebef42014-09-16 01:41:51 +0000105
Adrian Prantl437105a2015-07-08 02:04:15 +0000106cl::opt<bool>
107llvm::RawClangAST("raw-clang-ast",
108 cl::desc("Dump the raw binary contents of the clang AST section"));
109
Nick Kledzik56ebef42014-09-16 01:41:51 +0000110static cl::opt<bool>
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000111MachOOpt("macho", cl::desc("Use MachO specific object file parser"));
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000112static cl::alias
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000113MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachOOpt));
Benjamin Kramer87ee76c2011-07-20 19:37:35 +0000114
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000115cl::opt<std::string>
116llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
117 "see -version for available targets"));
118
119cl::opt<std::string>
Kevin Enderbyc9595622014-08-06 23:24:41 +0000120llvm::MCPU("mcpu",
121 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
122 cl::value_desc("cpu-name"),
123 cl::init(""));
124
125cl::opt<std::string>
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000126llvm::ArchName("arch-name", cl::desc("Target arch to disassemble for, "
Michael J. Spencer2670c252011-01-20 06:39:06 +0000127 "see -version for available targets"));
128
Kevin Enderby98da6132015-01-20 21:47:46 +0000129cl::opt<bool>
130llvm::SectionHeaders("section-headers", cl::desc("Display summaries of the "
131 "headers for each section."));
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000132static cl::alias
133SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
134 cl::aliasopt(SectionHeaders));
135static cl::alias
136SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
137 cl::aliasopt(SectionHeaders));
Colin LeMahieufcc32762015-07-29 19:08:10 +0000138
Colin LeMahieu77804be2015-07-29 15:45:39 +0000139cl::list<std::string>
Colin LeMahieufcc32762015-07-29 19:08:10 +0000140llvm::FilterSections("section", cl::desc("Operate on the specified sections only. "
141 "With -macho dump segment,section"));
142cl::alias
143static FilterSectionsj("j", cl::desc("Alias for --section"),
144 cl::aliasopt(llvm::FilterSections));
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000145
Kevin Enderbyc9595622014-08-06 23:24:41 +0000146cl::list<std::string>
147llvm::MAttrs("mattr",
Jack Carter551efd72012-08-28 19:24:49 +0000148 cl::CommaSeparated,
149 cl::desc("Target specific attributes"),
150 cl::value_desc("a1,+a2,-a3,..."));
151
Kevin Enderbybf246f52014-09-24 23:08:22 +0000152cl::opt<bool>
153llvm::NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling "
154 "instructions, do not print "
155 "the instruction bytes."));
Eli Bendersky3a6808c2012-11-20 22:57:02 +0000156
Kevin Enderby98da6132015-01-20 21:47:46 +0000157cl::opt<bool>
158llvm::UnwindInfo("unwind-info", cl::desc("Display unwind information"));
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000159
160static cl::alias
161UnwindInfoShort("u", cl::desc("Alias for --unwind-info"),
162 cl::aliasopt(UnwindInfo));
163
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000164cl::opt<bool>
165llvm::PrivateHeaders("private-headers",
166 cl::desc("Display format specific file headers"));
Michael J. Spencer209565db2013-01-06 03:56:49 +0000167
168static cl::alias
169PrivateHeadersShort("p", cl::desc("Alias for --private-headers"),
170 cl::aliasopt(PrivateHeaders));
171
Colin LeMahieu14ec76e2015-06-07 21:07:17 +0000172cl::opt<bool>
173 llvm::PrintImmHex("print-imm-hex",
174 cl::desc("Use hex format for immediate values"));
175
Sanjoy Das6f567a42015-06-22 18:03:02 +0000176cl::opt<bool> PrintFaultMaps("fault-map-section",
177 cl::desc("Display contents of faultmap section"));
178
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000179static StringRef ToolName;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000180
Colin LeMahieu77804be2015-07-29 15:45:39 +0000181namespace {
Colin LeMahieufcc32762015-07-29 19:08:10 +0000182typedef std::function<bool(llvm::object::SectionRef const &)> FilterPredicate;
Colin LeMahieu77804be2015-07-29 15:45:39 +0000183
184class SectionFilterIterator {
185public:
186 SectionFilterIterator(FilterPredicate P,
187 llvm::object::section_iterator const &I,
188 llvm::object::section_iterator const &E)
189 : Predicate(P), Iterator(I), End(E) {
190 ScanPredicate();
191 }
Benjamin Kramerac9257b2015-09-24 14:52:52 +0000192 const llvm::object::SectionRef &operator*() const { return *Iterator; }
Colin LeMahieu77804be2015-07-29 15:45:39 +0000193 SectionFilterIterator &operator++() {
194 ++Iterator;
195 ScanPredicate();
196 return *this;
197 }
198 bool operator!=(SectionFilterIterator const &Other) const {
199 return Iterator != Other.Iterator;
200 }
201
202private:
203 void ScanPredicate() {
Colin LeMahieuda1723f2015-07-29 19:21:13 +0000204 while (Iterator != End && !Predicate(*Iterator)) {
Colin LeMahieu77804be2015-07-29 15:45:39 +0000205 ++Iterator;
206 }
207 }
208 FilterPredicate Predicate;
209 llvm::object::section_iterator Iterator;
210 llvm::object::section_iterator End;
211};
212
213class SectionFilter {
214public:
215 SectionFilter(FilterPredicate P, llvm::object::ObjectFile const &O)
216 : Predicate(P), Object(O) {}
217 SectionFilterIterator begin() {
218 return SectionFilterIterator(Predicate, Object.section_begin(),
219 Object.section_end());
220 }
221 SectionFilterIterator end() {
222 return SectionFilterIterator(Predicate, Object.section_end(),
223 Object.section_end());
224 }
225
226private:
227 FilterPredicate Predicate;
228 llvm::object::ObjectFile const &Object;
229};
230SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O) {
Colin LeMahieu77804be2015-07-29 15:45:39 +0000231 return SectionFilter([](llvm::object::SectionRef const &S) {
Colin LeMahieufcc32762015-07-29 19:08:10 +0000232 if(FilterSections.empty())
Colin LeMahieuda1723f2015-07-29 19:21:13 +0000233 return true;
Colin LeMahieu77804be2015-07-29 15:45:39 +0000234 llvm::StringRef String;
235 std::error_code error = S.getName(String);
Colin LeMahieufcc32762015-07-29 19:08:10 +0000236 if (error)
Colin LeMahieuda1723f2015-07-29 19:21:13 +0000237 return false;
Colin LeMahieufcc32762015-07-29 19:08:10 +0000238 return std::find(FilterSections.begin(),
239 FilterSections.end(),
Colin LeMahieuda1723f2015-07-29 19:21:13 +0000240 String) != FilterSections.end();
Colin LeMahieu77804be2015-07-29 15:45:39 +0000241 },
242 O);
243}
244}
245
Davide Italianoccd53fe2015-08-05 07:18:31 +0000246void llvm::error(std::error_code EC) {
Mark Seaborneb03ac52014-01-25 00:32:01 +0000247 if (!EC)
Davide Italianoccd53fe2015-08-05 07:18:31 +0000248 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000249
Davide Italiano140af642015-12-25 18:16:45 +0000250 errs() << ToolName << ": error reading file: " << EC.message() << ".\n";
251 errs().flush();
Davide Italiano7f6c3012015-08-06 00:18:52 +0000252 exit(1);
Michael J. Spencer2670c252011-01-20 06:39:06 +0000253}
254
Davide Italianoed9d95b2015-12-29 13:41:02 +0000255LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File,
256 std::error_code EC) {
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +0000257 assert(EC);
258 errs() << ToolName << ": '" << File << "': " << EC.message() << ".\n";
Davide Italianoccd53fe2015-08-05 07:18:31 +0000259 exit(1);
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +0000260}
261
Craig Toppere6cb63e2014-04-25 04:24:47 +0000262static const Target *getTarget(const ObjectFile *Obj = nullptr) {
Michael J. Spencer2670c252011-01-20 06:39:06 +0000263 // Figure out the target triple.
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000264 llvm::Triple TheTriple("unknown-unknown-unknown");
Michael J. Spencer05350e6d2011-01-20 07:22:04 +0000265 if (TripleName.empty()) {
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000266 if (Obj) {
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000267 TheTriple.setArch(Triple::ArchType(Obj->getArch()));
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000268 // TheTriple defaults to ELF, and COFF doesn't have an environment:
269 // the best we can do here is indicate that it is mach-o.
270 if (Obj->isMachO())
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000271 TheTriple.setObjectFormat(Triple::MachO);
Saleem Abdulrasool98938f12014-04-17 06:17:23 +0000272
273 if (Obj->isCOFF()) {
274 const auto COFFObj = dyn_cast<COFFObjectFile>(Obj);
275 if (COFFObj->getArch() == Triple::thumb)
276 TheTriple.setTriple("thumbv7-windows");
277 }
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000278 }
Michael J. Spencer05350e6d2011-01-20 07:22:04 +0000279 } else
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000280 TheTriple.setTriple(Triple::normalize(TripleName));
Michael J. Spencer2670c252011-01-20 06:39:06 +0000281
282 // Get the target specific parser.
283 std::string Error;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000284 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
285 Error);
Davide Italianobb599e32015-12-03 22:13:40 +0000286 if (!TheTarget)
287 report_fatal_error("can't find target: " + Error);
Michael J. Spencer2670c252011-01-20 06:39:06 +0000288
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000289 // Update the triple name and return the found target.
290 TripleName = TheTriple.getTriple();
291 return TheTarget;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000292}
293
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000294bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
Rafael Espindola704cd842015-07-06 15:47:43 +0000295 return a.getOffset() < b.getOffset();
Michael J. Spencer51862b32011-10-13 22:17:18 +0000296}
297
Colin LeMahieufb76b002015-05-28 19:07:14 +0000298namespace {
299class PrettyPrinter {
300public:
Colin LeMahieu0b5890d2015-05-28 20:59:08 +0000301 virtual ~PrettyPrinter(){}
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000302 virtual void printInst(MCInstPrinter &IP, const MCInst *MI,
Colin LeMahieufb76b002015-05-28 19:07:14 +0000303 ArrayRef<uint8_t> Bytes, uint64_t Address,
304 raw_ostream &OS, StringRef Annot,
305 MCSubtargetInfo const &STI) {
306 outs() << format("%8" PRIx64 ":", Address);
307 if (!NoShowRawInsn) {
308 outs() << "\t";
309 dumpBytes(Bytes, outs());
310 }
311 IP.printInst(MI, outs(), "", STI);
312 }
313};
314PrettyPrinter PrettyPrinterInst;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000315class HexagonPrettyPrinter : public PrettyPrinter {
316public:
317 void printLead(ArrayRef<uint8_t> Bytes, uint64_t Address,
318 raw_ostream &OS) {
319 uint32_t opcode =
320 (Bytes[3] << 24) | (Bytes[2] << 16) | (Bytes[1] << 8) | Bytes[0];
321 OS << format("%8" PRIx64 ":", Address);
322 if (!NoShowRawInsn) {
323 OS << "\t";
324 dumpBytes(Bytes.slice(0, 4), OS);
325 OS << format("%08" PRIx32, opcode);
326 }
327 }
328 void printInst(MCInstPrinter &IP, const MCInst *MI,
329 ArrayRef<uint8_t> Bytes, uint64_t Address,
330 raw_ostream &OS, StringRef Annot,
331 MCSubtargetInfo const &STI) override {
332 std::string Buffer;
333 {
334 raw_string_ostream TempStream(Buffer);
335 IP.printInst(MI, TempStream, "", STI);
336 }
337 StringRef Contents(Buffer);
338 // Split off bundle attributes
339 auto PacketBundle = Contents.rsplit('\n');
340 // Split off first instruction from the rest
341 auto HeadTail = PacketBundle.first.split('\n');
342 auto Preamble = " { ";
343 auto Separator = "";
344 while(!HeadTail.first.empty()) {
345 OS << Separator;
346 Separator = "\n";
347 printLead(Bytes, Address, OS);
348 OS << Preamble;
349 Preamble = " ";
350 StringRef Inst;
351 auto Duplex = HeadTail.first.split('\v');
352 if(!Duplex.second.empty()){
353 OS << Duplex.first;
354 OS << "; ";
355 Inst = Duplex.second;
356 }
357 else
358 Inst = HeadTail.first;
359 OS << Inst;
360 Bytes = Bytes.slice(4);
361 Address += 4;
362 HeadTail = HeadTail.second.split('\n');
363 }
364 OS << " } " << PacketBundle.second;
365 }
366};
367HexagonPrettyPrinter HexagonPrettyPrinterInst;
Colin LeMahieu35436a22015-05-29 14:48:25 +0000368PrettyPrinter &selectPrettyPrinter(Triple const &Triple) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000369 switch(Triple.getArch()) {
370 default:
371 return PrettyPrinterInst;
372 case Triple::hexagon:
373 return HexagonPrettyPrinterInst;
374 }
Colin LeMahieufb76b002015-05-28 19:07:14 +0000375}
376}
377
Rafael Espindola37070a52015-06-03 04:48:06 +0000378template <class ELFT>
Rafael Espindola37070a52015-06-03 04:48:06 +0000379static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
Rafael Espindolaa01ff222015-08-10 20:50:40 +0000380 const RelocationRef &RelRef,
Rafael Espindola37070a52015-06-03 04:48:06 +0000381 SmallVectorImpl<char> &Result) {
Rafael Espindolaa01ff222015-08-10 20:50:40 +0000382 DataRefImpl Rel = RelRef.getRawDataRefImpl();
383
Rafael Espindola37070a52015-06-03 04:48:06 +0000384 typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
385 typedef typename ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr;
Rafael Espindola7f162ec2015-07-02 14:21:38 +0000386 typedef typename ELFObjectFile<ELFT>::Elf_Rela Elf_Rela;
387
Rafael Espindola37070a52015-06-03 04:48:06 +0000388 const ELFFile<ELFT> &EF = *Obj->getELFFile();
389
Rafael Espindola6def3042015-07-01 12:56:27 +0000390 ErrorOr<const Elf_Shdr *> SecOrErr = EF.getSection(Rel.d.a);
391 if (std::error_code EC = SecOrErr.getError())
392 return EC;
393 const Elf_Shdr *Sec = *SecOrErr;
394 ErrorOr<const Elf_Shdr *> SymTabOrErr = EF.getSection(Sec->sh_link);
395 if (std::error_code EC = SymTabOrErr.getError())
396 return EC;
397 const Elf_Shdr *SymTab = *SymTabOrErr;
Rafael Espindola719dc7c2015-06-29 12:38:31 +0000398 assert(SymTab->sh_type == ELF::SHT_SYMTAB ||
399 SymTab->sh_type == ELF::SHT_DYNSYM);
Rafael Espindola6def3042015-07-01 12:56:27 +0000400 ErrorOr<const Elf_Shdr *> StrTabSec = EF.getSection(SymTab->sh_link);
401 if (std::error_code EC = StrTabSec.getError())
402 return EC;
403 ErrorOr<StringRef> StrTabOrErr = EF.getStringTable(*StrTabSec);
Rafael Espindola6a1bfb22015-06-29 14:39:25 +0000404 if (std::error_code EC = StrTabOrErr.getError())
405 return EC;
406 StringRef StrTab = *StrTabOrErr;
Rafael Espindolaa01ff222015-08-10 20:50:40 +0000407 uint8_t type = RelRef.getType();
Rafael Espindola37070a52015-06-03 04:48:06 +0000408 StringRef res;
409 int64_t addend = 0;
Rafael Espindola6def3042015-07-01 12:56:27 +0000410 switch (Sec->sh_type) {
Rafael Espindola37070a52015-06-03 04:48:06 +0000411 default:
412 return object_error::parse_failed;
413 case ELF::SHT_REL: {
Rafael Espindola37070a52015-06-03 04:48:06 +0000414 // TODO: Read implicit addend from section data.
415 break;
416 }
417 case ELF::SHT_RELA: {
Rafael Espindola7f162ec2015-07-02 14:21:38 +0000418 const Elf_Rela *ERela = Obj->getRela(Rel);
Rafael Espindola7f162ec2015-07-02 14:21:38 +0000419 addend = ERela->r_addend;
Rafael Espindola37070a52015-06-03 04:48:06 +0000420 break;
421 }
422 }
Rafael Espindolaa01ff222015-08-10 20:50:40 +0000423 symbol_iterator SI = RelRef.getSymbol();
424 const Elf_Sym *symb = Obj->getSymbol(SI->getRawDataRefImpl());
Rafael Espindola75d5b542015-06-03 05:14:22 +0000425 StringRef Target;
Rafael Espindola75d5b542015-06-03 05:14:22 +0000426 if (symb->getType() == ELF::STT_SECTION) {
Rafael Espindolaa01ff222015-08-10 20:50:40 +0000427 ErrorOr<section_iterator> SymSI = SI->getSection();
428 if (std::error_code EC = SymSI.getError())
429 return EC;
430 const Elf_Shdr *SymSec = Obj->getSection((*SymSI)->getRawDataRefImpl());
431 ErrorOr<StringRef> SecName = EF.getSectionName(SymSec);
Rafael Espindola75d5b542015-06-03 05:14:22 +0000432 if (std::error_code EC = SecName.getError())
433 return EC;
434 Target = *SecName;
435 } else {
Rafael Espindola44c28712015-06-29 21:24:55 +0000436 ErrorOr<StringRef> SymName = symb->getName(StrTab);
Rafael Espindola75d5b542015-06-03 05:14:22 +0000437 if (!SymName)
438 return SymName.getError();
439 Target = *SymName;
440 }
Rafael Espindola37070a52015-06-03 04:48:06 +0000441 switch (EF.getHeader()->e_machine) {
442 case ELF::EM_X86_64:
443 switch (type) {
444 case ELF::R_X86_64_PC8:
445 case ELF::R_X86_64_PC16:
446 case ELF::R_X86_64_PC32: {
447 std::string fmtbuf;
448 raw_string_ostream fmt(fmtbuf);
Rafael Espindola75d5b542015-06-03 05:14:22 +0000449 fmt << Target << (addend < 0 ? "" : "+") << addend << "-P";
Rafael Espindola37070a52015-06-03 04:48:06 +0000450 fmt.flush();
451 Result.append(fmtbuf.begin(), fmtbuf.end());
452 } break;
453 case ELF::R_X86_64_8:
454 case ELF::R_X86_64_16:
455 case ELF::R_X86_64_32:
456 case ELF::R_X86_64_32S:
457 case ELF::R_X86_64_64: {
458 std::string fmtbuf;
459 raw_string_ostream fmt(fmtbuf);
Rafael Espindola75d5b542015-06-03 05:14:22 +0000460 fmt << Target << (addend < 0 ? "" : "+") << addend;
Rafael Espindola37070a52015-06-03 04:48:06 +0000461 fmt.flush();
462 Result.append(fmtbuf.begin(), fmtbuf.end());
463 } break;
464 default:
465 res = "Unknown";
466 }
467 break;
468 case ELF::EM_AARCH64: {
469 std::string fmtbuf;
470 raw_string_ostream fmt(fmtbuf);
Rafael Espindola75d5b542015-06-03 05:14:22 +0000471 fmt << Target;
Rafael Espindola37070a52015-06-03 04:48:06 +0000472 if (addend != 0)
473 fmt << (addend < 0 ? "" : "+") << addend;
474 fmt.flush();
475 Result.append(fmtbuf.begin(), fmtbuf.end());
476 break;
477 }
478 case ELF::EM_386:
Michael Kupersteina3b79dd2015-11-04 11:21:50 +0000479 case ELF::EM_IAMCU:
Rafael Espindola37070a52015-06-03 04:48:06 +0000480 case ELF::EM_ARM:
481 case ELF::EM_HEXAGON:
482 case ELF::EM_MIPS:
Rafael Espindola75d5b542015-06-03 05:14:22 +0000483 res = Target;
Rafael Espindola37070a52015-06-03 04:48:06 +0000484 break;
Dan Gohman46350172016-01-12 20:56:01 +0000485 case ELF::EM_WEBASSEMBLY:
486 switch (type) {
487 case ELF::R_WEBASSEMBLY_DATA: {
488 std::string fmtbuf;
489 raw_string_ostream fmt(fmtbuf);
490 fmt << Target << (addend < 0 ? "" : "+") << addend;
491 fmt.flush();
492 Result.append(fmtbuf.begin(), fmtbuf.end());
493 break;
494 }
495 case ELF::R_WEBASSEMBLY_FUNCTION:
496 res = Target;
497 break;
498 default:
499 res = "Unknown";
500 }
501 break;
Rafael Espindola37070a52015-06-03 04:48:06 +0000502 default:
503 res = "Unknown";
504 }
505 if (Result.empty())
506 Result.append(res.begin(), res.end());
Rui Ueyama7d099192015-06-09 15:20:42 +0000507 return std::error_code();
Rafael Espindola37070a52015-06-03 04:48:06 +0000508}
509
510static std::error_code getRelocationValueString(const ELFObjectFileBase *Obj,
Rafael Espindolaa01ff222015-08-10 20:50:40 +0000511 const RelocationRef &Rel,
Rafael Espindola37070a52015-06-03 04:48:06 +0000512 SmallVectorImpl<char> &Result) {
Rafael Espindola37070a52015-06-03 04:48:06 +0000513 if (auto *ELF32LE = dyn_cast<ELF32LEObjectFile>(Obj))
514 return getRelocationValueString(ELF32LE, Rel, Result);
515 if (auto *ELF64LE = dyn_cast<ELF64LEObjectFile>(Obj))
516 return getRelocationValueString(ELF64LE, Rel, Result);
517 if (auto *ELF32BE = dyn_cast<ELF32BEObjectFile>(Obj))
518 return getRelocationValueString(ELF32BE, Rel, Result);
519 auto *ELF64BE = cast<ELF64BEObjectFile>(Obj);
520 return getRelocationValueString(ELF64BE, Rel, Result);
521}
522
523static std::error_code getRelocationValueString(const COFFObjectFile *Obj,
524 const RelocationRef &Rel,
525 SmallVectorImpl<char> &Result) {
526 symbol_iterator SymI = Rel.getSymbol();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000527 ErrorOr<StringRef> SymNameOrErr = SymI->getName();
528 if (std::error_code EC = SymNameOrErr.getError())
Rafael Espindola37070a52015-06-03 04:48:06 +0000529 return EC;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000530 StringRef SymName = *SymNameOrErr;
Rafael Espindola37070a52015-06-03 04:48:06 +0000531 Result.append(SymName.begin(), SymName.end());
Rui Ueyama7d099192015-06-09 15:20:42 +0000532 return std::error_code();
Rafael Espindola37070a52015-06-03 04:48:06 +0000533}
534
535static void printRelocationTargetName(const MachOObjectFile *O,
536 const MachO::any_relocation_info &RE,
537 raw_string_ostream &fmt) {
538 bool IsScattered = O->isRelocationScattered(RE);
539
540 // Target of a scattered relocation is an address. In the interest of
541 // generating pretty output, scan through the symbol table looking for a
542 // symbol that aligns with that address. If we find one, print it.
543 // Otherwise, we just print the hex address of the target.
544 if (IsScattered) {
545 uint32_t Val = O->getPlainRelocationSymbolNum(RE);
546
547 for (const SymbolRef &Symbol : O->symbols()) {
548 std::error_code ec;
Rafael Espindolaed067c42015-07-03 18:19:00 +0000549 ErrorOr<uint64_t> Addr = Symbol.getAddress();
550 if ((ec = Addr.getError()))
Rafael Espindola37070a52015-06-03 04:48:06 +0000551 report_fatal_error(ec.message());
Rafael Espindolaed067c42015-07-03 18:19:00 +0000552 if (*Addr != Val)
Rafael Espindola37070a52015-06-03 04:48:06 +0000553 continue;
Rafael Espindolaed067c42015-07-03 18:19:00 +0000554 ErrorOr<StringRef> Name = Symbol.getName();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000555 if (std::error_code EC = Name.getError())
556 report_fatal_error(EC.message());
557 fmt << *Name;
Rafael Espindola37070a52015-06-03 04:48:06 +0000558 return;
559 }
560
561 // If we couldn't find a symbol that this relocation refers to, try
562 // to find a section beginning instead.
Colin LeMahieu77804be2015-07-29 15:45:39 +0000563 for (const SectionRef &Section : ToolSectionFilter(*O)) {
Rafael Espindola37070a52015-06-03 04:48:06 +0000564 std::error_code ec;
565
566 StringRef Name;
567 uint64_t Addr = Section.getAddress();
568 if (Addr != Val)
569 continue;
570 if ((ec = Section.getName(Name)))
571 report_fatal_error(ec.message());
572 fmt << Name;
573 return;
574 }
575
576 fmt << format("0x%x", Val);
577 return;
578 }
579
580 StringRef S;
581 bool isExtern = O->getPlainRelocationExternal(RE);
582 uint64_t Val = O->getPlainRelocationSymbolNum(RE);
583
584 if (isExtern) {
585 symbol_iterator SI = O->symbol_begin();
586 advance(SI, Val);
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000587 ErrorOr<StringRef> SOrErr = SI->getName();
Davide Italianoccd53fe2015-08-05 07:18:31 +0000588 error(SOrErr.getError());
589 S = *SOrErr;
Rafael Espindola37070a52015-06-03 04:48:06 +0000590 } else {
591 section_iterator SI = O->section_begin();
592 // Adjust for the fact that sections are 1-indexed.
593 advance(SI, Val - 1);
594 SI->getName(S);
595 }
596
597 fmt << S;
598}
599
600static std::error_code getRelocationValueString(const MachOObjectFile *Obj,
601 const RelocationRef &RelRef,
602 SmallVectorImpl<char> &Result) {
603 DataRefImpl Rel = RelRef.getRawDataRefImpl();
604 MachO::any_relocation_info RE = Obj->getRelocation(Rel);
605
606 unsigned Arch = Obj->getArch();
607
608 std::string fmtbuf;
609 raw_string_ostream fmt(fmtbuf);
610 unsigned Type = Obj->getAnyRelocationType(RE);
611 bool IsPCRel = Obj->getAnyRelocationPCRel(RE);
612
613 // Determine any addends that should be displayed with the relocation.
614 // These require decoding the relocation type, which is triple-specific.
615
616 // X86_64 has entirely custom relocation types.
617 if (Arch == Triple::x86_64) {
618 bool isPCRel = Obj->getAnyRelocationPCRel(RE);
619
620 switch (Type) {
621 case MachO::X86_64_RELOC_GOT_LOAD:
622 case MachO::X86_64_RELOC_GOT: {
623 printRelocationTargetName(Obj, RE, fmt);
624 fmt << "@GOT";
625 if (isPCRel)
626 fmt << "PCREL";
627 break;
628 }
629 case MachO::X86_64_RELOC_SUBTRACTOR: {
630 DataRefImpl RelNext = Rel;
631 Obj->moveRelocationNext(RelNext);
632 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
633
634 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
635 // X86_64_RELOC_UNSIGNED.
636 // NOTE: Scattered relocations don't exist on x86_64.
637 unsigned RType = Obj->getAnyRelocationType(RENext);
638 if (RType != MachO::X86_64_RELOC_UNSIGNED)
639 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
640 "X86_64_RELOC_SUBTRACTOR.");
641
642 // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
643 // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
644 printRelocationTargetName(Obj, RENext, fmt);
645 fmt << "-";
646 printRelocationTargetName(Obj, RE, fmt);
647 break;
648 }
649 case MachO::X86_64_RELOC_TLV:
650 printRelocationTargetName(Obj, RE, fmt);
651 fmt << "@TLV";
652 if (isPCRel)
653 fmt << "P";
654 break;
655 case MachO::X86_64_RELOC_SIGNED_1:
656 printRelocationTargetName(Obj, RE, fmt);
657 fmt << "-1";
658 break;
659 case MachO::X86_64_RELOC_SIGNED_2:
660 printRelocationTargetName(Obj, RE, fmt);
661 fmt << "-2";
662 break;
663 case MachO::X86_64_RELOC_SIGNED_4:
664 printRelocationTargetName(Obj, RE, fmt);
665 fmt << "-4";
666 break;
667 default:
668 printRelocationTargetName(Obj, RE, fmt);
669 break;
670 }
671 // X86 and ARM share some relocation types in common.
672 } else if (Arch == Triple::x86 || Arch == Triple::arm ||
673 Arch == Triple::ppc) {
674 // Generic relocation types...
675 switch (Type) {
676 case MachO::GENERIC_RELOC_PAIR: // prints no info
Rui Ueyama7d099192015-06-09 15:20:42 +0000677 return std::error_code();
Rafael Espindola37070a52015-06-03 04:48:06 +0000678 case MachO::GENERIC_RELOC_SECTDIFF: {
679 DataRefImpl RelNext = Rel;
680 Obj->moveRelocationNext(RelNext);
681 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
682
683 // X86 sect diff's must be followed by a relocation of type
684 // GENERIC_RELOC_PAIR.
685 unsigned RType = Obj->getAnyRelocationType(RENext);
686
687 if (RType != MachO::GENERIC_RELOC_PAIR)
688 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
689 "GENERIC_RELOC_SECTDIFF.");
690
691 printRelocationTargetName(Obj, RE, fmt);
692 fmt << "-";
693 printRelocationTargetName(Obj, RENext, fmt);
694 break;
695 }
696 }
697
698 if (Arch == Triple::x86 || Arch == Triple::ppc) {
699 switch (Type) {
700 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
701 DataRefImpl RelNext = Rel;
702 Obj->moveRelocationNext(RelNext);
703 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
704
705 // X86 sect diff's must be followed by a relocation of type
706 // GENERIC_RELOC_PAIR.
707 unsigned RType = Obj->getAnyRelocationType(RENext);
708 if (RType != MachO::GENERIC_RELOC_PAIR)
709 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
710 "GENERIC_RELOC_LOCAL_SECTDIFF.");
711
712 printRelocationTargetName(Obj, RE, fmt);
713 fmt << "-";
714 printRelocationTargetName(Obj, RENext, fmt);
715 break;
716 }
717 case MachO::GENERIC_RELOC_TLV: {
718 printRelocationTargetName(Obj, RE, fmt);
719 fmt << "@TLV";
720 if (IsPCRel)
721 fmt << "P";
722 break;
723 }
724 default:
725 printRelocationTargetName(Obj, RE, fmt);
726 }
727 } else { // ARM-specific relocations
728 switch (Type) {
729 case MachO::ARM_RELOC_HALF:
730 case MachO::ARM_RELOC_HALF_SECTDIFF: {
731 // Half relocations steal a bit from the length field to encode
732 // whether this is an upper16 or a lower16 relocation.
733 bool isUpper = Obj->getAnyRelocationLength(RE) >> 1;
734
735 if (isUpper)
736 fmt << ":upper16:(";
737 else
738 fmt << ":lower16:(";
739 printRelocationTargetName(Obj, RE, fmt);
740
741 DataRefImpl RelNext = Rel;
742 Obj->moveRelocationNext(RelNext);
743 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
744
745 // ARM half relocs must be followed by a relocation of type
746 // ARM_RELOC_PAIR.
747 unsigned RType = Obj->getAnyRelocationType(RENext);
748 if (RType != MachO::ARM_RELOC_PAIR)
749 report_fatal_error("Expected ARM_RELOC_PAIR after "
750 "ARM_RELOC_HALF");
751
752 // NOTE: The half of the target virtual address is stashed in the
753 // address field of the secondary relocation, but we can't reverse
754 // engineer the constant offset from it without decoding the movw/movt
755 // instruction to find the other half in its immediate field.
756
757 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
758 // symbol/section pointer of the follow-on relocation.
759 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
760 fmt << "-";
761 printRelocationTargetName(Obj, RENext, fmt);
762 }
763
764 fmt << ")";
765 break;
766 }
767 default: { printRelocationTargetName(Obj, RE, fmt); }
768 }
769 }
770 } else
771 printRelocationTargetName(Obj, RE, fmt);
772
773 fmt.flush();
774 Result.append(fmtbuf.begin(), fmtbuf.end());
Rui Ueyama7d099192015-06-09 15:20:42 +0000775 return std::error_code();
Rafael Espindola37070a52015-06-03 04:48:06 +0000776}
777
778static std::error_code getRelocationValueString(const RelocationRef &Rel,
779 SmallVectorImpl<char> &Result) {
Rafael Espindola854038e2015-06-26 14:51:16 +0000780 const ObjectFile *Obj = Rel.getObject();
Rafael Espindola37070a52015-06-03 04:48:06 +0000781 if (auto *ELF = dyn_cast<ELFObjectFileBase>(Obj))
782 return getRelocationValueString(ELF, Rel, Result);
783 if (auto *COFF = dyn_cast<COFFObjectFile>(Obj))
784 return getRelocationValueString(COFF, Rel, Result);
785 auto *MachO = cast<MachOObjectFile>(Obj);
786 return getRelocationValueString(MachO, Rel, Result);
787}
788
Rafael Espindola0ad71d92015-06-30 03:41:26 +0000789/// @brief Indicates whether this relocation should hidden when listing
790/// relocations, usually because it is the trailing part of a multipart
791/// relocation that will be printed as part of the leading relocation.
792static bool getHidden(RelocationRef RelRef) {
793 const ObjectFile *Obj = RelRef.getObject();
794 auto *MachO = dyn_cast<MachOObjectFile>(Obj);
795 if (!MachO)
796 return false;
797
798 unsigned Arch = MachO->getArch();
799 DataRefImpl Rel = RelRef.getRawDataRefImpl();
800 uint64_t Type = MachO->getRelocationType(Rel);
801
802 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
803 // is always hidden.
804 if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
805 if (Type == MachO::GENERIC_RELOC_PAIR)
806 return true;
807 } else if (Arch == Triple::x86_64) {
808 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
809 // an X86_64_RELOC_SUBTRACTOR.
810 if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
811 DataRefImpl RelPrev = Rel;
812 RelPrev.d.a--;
813 uint64_t PrevType = MachO->getRelocationType(RelPrev);
814 if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
815 return true;
816 }
817 }
818
819 return false;
820}
821
Michael J. Spencer51862b32011-10-13 22:17:18 +0000822static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
Jim Grosbachaf9aec02012-08-07 17:53:14 +0000823 const Target *TheTarget = getTarget(Obj);
Michael J. Spencer2670c252011-01-20 06:39:06 +0000824
Jack Carter551efd72012-08-28 19:24:49 +0000825 // Package up features to be passed to target/subtarget
826 std::string FeaturesStr;
827 if (MAttrs.size()) {
828 SubtargetFeatures Features;
829 for (unsigned i = 0; i != MAttrs.size(); ++i)
830 Features.AddFeature(MAttrs[i]);
831 FeaturesStr = Features.getString();
832 }
833
Ahmed Charles56440fd2014-03-06 05:51:42 +0000834 std::unique_ptr<const MCRegisterInfo> MRI(
835 TheTarget->createMCRegInfo(TripleName));
Davide Italiano711e4952015-12-17 01:59:50 +0000836 if (!MRI)
837 report_fatal_error("error: no register info for target " + TripleName);
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000838
839 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000840 std::unique_ptr<const MCAsmInfo> AsmInfo(
841 TheTarget->createMCAsmInfo(*MRI, TripleName));
Davide Italiano711e4952015-12-17 01:59:50 +0000842 if (!AsmInfo)
843 report_fatal_error("error: no assembly info for target " + TripleName);
Ahmed Charles56440fd2014-03-06 05:51:42 +0000844 std::unique_ptr<const MCSubtargetInfo> STI(
Kevin Enderbyc9595622014-08-06 23:24:41 +0000845 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Davide Italiano711e4952015-12-17 01:59:50 +0000846 if (!STI)
847 report_fatal_error("error: no subtarget info for target " + TripleName);
Ahmed Charles56440fd2014-03-06 05:51:42 +0000848 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
Davide Italiano711e4952015-12-17 01:59:50 +0000849 if (!MII)
850 report_fatal_error("error: no instruction info for target " + TripleName);
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000851 std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo);
852 MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get());
853
854 std::unique_ptr<MCDisassembler> DisAsm(
855 TheTarget->createMCDisassembler(*STI, Ctx));
Davide Italiano711e4952015-12-17 01:59:50 +0000856 if (!DisAsm)
857 report_fatal_error("error: no disassembler for target " + TripleName);
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000858
Ahmed Charles56440fd2014-03-06 05:51:42 +0000859 std::unique_ptr<const MCInstrAnalysis> MIA(
860 TheTarget->createMCInstrAnalysis(MII.get()));
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000861
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000862 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Daniel Sanders50f17232015-09-15 16:17:27 +0000863 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
864 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI));
Davide Italiano711e4952015-12-17 01:59:50 +0000865 if (!IP)
866 report_fatal_error("error: no instruction printer for target " +
867 TripleName);
Colin LeMahieu14ec76e2015-06-07 21:07:17 +0000868 IP->setPrintImmHex(PrintImmHex);
Colin LeMahieu35436a22015-05-29 14:48:25 +0000869 PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000870
Greg Fitzgerald18432272014-03-20 22:55:15 +0000871 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ": " :
872 "\t\t\t%08" PRIx64 ": ";
873
Mark Seaborn0929d3d2014-01-25 17:38:19 +0000874 // Create a mapping, RelocSecs = SectionRelocMap[S], where sections
875 // in RelocSecs contain the relocations for section S.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000876 std::error_code EC;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000877 std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
Colin LeMahieu77804be2015-07-29 15:45:39 +0000878 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
Alexey Samsonov48803e52014-03-13 14:37:36 +0000879 section_iterator Sec2 = Section.getRelocatedSection();
Rafael Espindolab5155a52014-02-10 20:24:04 +0000880 if (Sec2 != Obj->section_end())
Alexey Samsonov48803e52014-03-13 14:37:36 +0000881 SectionRelocMap[*Sec2].push_back(Section);
Mark Seaborn0929d3d2014-01-25 17:38:19 +0000882 }
883
David Majnemer81afca62015-07-07 22:06:59 +0000884 // Create a mapping from virtual address to symbol name. This is used to
David Majnemerfbb1c3a2015-11-18 02:49:19 +0000885 // pretty print the symbols while disassembling.
886 typedef std::vector<std::pair<uint64_t, StringRef>> SectionSymbolsTy;
887 std::map<SectionRef, SectionSymbolsTy> AllSymbols;
888 for (const SymbolRef &Symbol : Obj->symbols()) {
889 ErrorOr<uint64_t> AddressOrErr = Symbol.getAddress();
890 error(AddressOrErr.getError());
891 uint64_t Address = *AddressOrErr;
David Majnemer2603a8fa2015-07-09 18:11:40 +0000892
David Majnemerfbb1c3a2015-11-18 02:49:19 +0000893 ErrorOr<StringRef> Name = Symbol.getName();
894 error(Name.getError());
895 if (Name->empty())
896 continue;
David Majnemer81afca62015-07-07 22:06:59 +0000897
David Majnemerfbb1c3a2015-11-18 02:49:19 +0000898 ErrorOr<section_iterator> SectionOrErr = Symbol.getSection();
899 error(SectionOrErr.getError());
900 section_iterator SecI = *SectionOrErr;
901 if (SecI == Obj->section_end())
902 continue;
David Majnemer81afca62015-07-07 22:06:59 +0000903
David Majnemerfbb1c3a2015-11-18 02:49:19 +0000904 AllSymbols[*SecI].emplace_back(Address, *Name);
David Majnemer81afca62015-07-07 22:06:59 +0000905 }
906
David Majnemerfbb1c3a2015-11-18 02:49:19 +0000907 // Create a mapping from virtual address to section.
908 std::vector<std::pair<uint64_t, SectionRef>> SectionAddresses;
909 for (SectionRef Sec : Obj->sections())
910 SectionAddresses.emplace_back(Sec.getAddress(), Sec);
911 array_pod_sort(SectionAddresses.begin(), SectionAddresses.end());
912
913 // Linked executables (.exe and .dll files) typically don't include a real
914 // symbol table but they might contain an export table.
915 if (const auto *COFFObj = dyn_cast<COFFObjectFile>(Obj)) {
916 for (const auto &ExportEntry : COFFObj->export_directories()) {
917 StringRef Name;
918 error(ExportEntry.getSymbolName(Name));
919 if (Name.empty())
920 continue;
921 uint32_t RVA;
922 error(ExportEntry.getExportRVA(RVA));
923
924 uint64_t VA = COFFObj->getImageBase() + RVA;
925 auto Sec = std::upper_bound(
926 SectionAddresses.begin(), SectionAddresses.end(), VA,
927 [](uint64_t LHS, const std::pair<uint64_t, SectionRef> &RHS) {
928 return LHS < RHS.first;
929 });
930 if (Sec != SectionAddresses.begin())
931 --Sec;
932 else
933 Sec = SectionAddresses.end();
934
935 if (Sec != SectionAddresses.end())
936 AllSymbols[Sec->second].emplace_back(VA, Name);
937 }
938 }
939
940 // Sort all the symbols, this allows us to use a simple binary search to find
941 // a symbol near an address.
942 for (std::pair<const SectionRef, SectionSymbolsTy> &SecSyms : AllSymbols)
943 array_pod_sort(SecSyms.second.begin(), SecSyms.second.end());
944
Colin LeMahieu77804be2015-07-29 15:45:39 +0000945 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
Colin LeMahieuf34933e2015-07-23 20:58:49 +0000946 if (!DisassembleAll && (!Section.isText() || Section.isVirtual()))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000947 continue;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000948
Rafael Espindola80291272014-10-08 15:28:58 +0000949 uint64_t SectionAddr = Section.getAddress();
950 uint64_t SectSize = Section.getSize();
David Majnemer185b5b12014-11-11 09:58:25 +0000951 if (!SectSize)
952 continue;
Simon Atanasyan2b614e12014-02-24 22:12:11 +0000953
David Majnemerfbb1c3a2015-11-18 02:49:19 +0000954 // Get the list of all the symbols in this section.
955 SectionSymbolsTy &Symbols = AllSymbols[Section];
Davide Italianof0706882015-10-01 21:57:09 +0000956 std::vector<uint64_t> DataMappingSymsAddr;
957 std::vector<uint64_t> TextMappingSymsAddr;
David Majnemerfbb1c3a2015-11-18 02:49:19 +0000958 if (Obj->isELF() && Obj->getArch() == Triple::aarch64) {
959 for (const auto &Symb : Symbols) {
960 uint64_t Address = Symb.first;
961 StringRef Name = Symb.second;
962 if (Name.startswith("$d"))
David Majnemer153722d2015-11-18 04:35:32 +0000963 DataMappingSymsAddr.push_back(Address - SectionAddr);
David Majnemerfbb1c3a2015-11-18 02:49:19 +0000964 if (Name.startswith("$x"))
David Majnemer153722d2015-11-18 04:35:32 +0000965 TextMappingSymsAddr.push_back(Address - SectionAddr);
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000966 }
967 }
968
Davide Italianof0706882015-10-01 21:57:09 +0000969 std::sort(DataMappingSymsAddr.begin(), DataMappingSymsAddr.end());
970 std::sort(TextMappingSymsAddr.begin(), TextMappingSymsAddr.end());
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000971
Michael J. Spencer51862b32011-10-13 22:17:18 +0000972 // Make a list of all the relocations for this section.
973 std::vector<RelocationRef> Rels;
974 if (InlineRelocs) {
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000975 for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
976 for (const RelocationRef &Reloc : RelocSec.relocations()) {
977 Rels.push_back(Reloc);
978 }
Michael J. Spencer51862b32011-10-13 22:17:18 +0000979 }
980 }
981
982 // Sort relocations by address.
983 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
984
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000985 StringRef SegmentName = "";
Mark Seaborneb03ac52014-01-25 00:32:01 +0000986 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
Alexey Samsonov48803e52014-03-13 14:37:36 +0000987 DataRefImpl DR = Section.getRawDataRefImpl();
Rafael Espindola56f976f2013-04-18 18:08:55 +0000988 SegmentName = MachO->getSectionFinalSegmentName(DR);
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000989 }
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000990 StringRef name;
Davide Italianoccd53fe2015-08-05 07:18:31 +0000991 error(Section.getName(name));
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000992 outs() << "Disassembly of section ";
993 if (!SegmentName.empty())
994 outs() << SegmentName << ",";
995 outs() << name << ':';
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000996
Rafael Espindola7884c952015-06-04 15:01:05 +0000997 // If the section has no symbol at the start, just insert a dummy one.
998 if (Symbols.empty() || Symbols[0].first != 0)
David Majnemer153722d2015-11-18 04:35:32 +0000999 Symbols.insert(Symbols.begin(), std::make_pair(SectionAddr, name));
Alp Tokere69170a2014-06-26 22:52:05 +00001000
1001 SmallString<40> Comments;
1002 raw_svector_ostream CommentStream(Comments);
Ahmed Bougachaad1084d2013-05-24 00:39:57 +00001003
Rafael Espindola7fc5b872014-11-12 02:04:27 +00001004 StringRef BytesStr;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001005 error(Section.getContents(BytesStr));
Aaron Ballman106fd7b2014-11-12 14:01:17 +00001006 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
1007 BytesStr.size());
Rafael Espindola7fc5b872014-11-12 02:04:27 +00001008
Michael J. Spencer2670c252011-01-20 06:39:06 +00001009 uint64_t Size;
1010 uint64_t Index;
1011
Michael J. Spencer51862b32011-10-13 22:17:18 +00001012 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
1013 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001014 // Disassemble symbol by symbol.
1015 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
Rafael Espindolae45c7402014-08-17 16:31:39 +00001016
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001017 uint64_t Start = Symbols[si].first - SectionAddr;
1018 // The end is either the section end or the beginning of the next
1019 // symbol.
1020 uint64_t End =
1021 (si == se - 1) ? SectSize : Symbols[si + 1].first - SectionAddr;
1022 // Don't try to disassemble beyond the end of section contents.
1023 if (End > SectSize)
1024 End = SectSize;
Rafael Espindolae45c7402014-08-17 16:31:39 +00001025 // If this symbol has the same address as the next symbol, then skip it.
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001026 if (Start >= End)
Michael J. Spenceree84f642011-10-13 20:37:08 +00001027 continue;
1028
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001029 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer2670c252011-01-20 06:39:06 +00001030
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001031#ifndef NDEBUG
Mark Seaborneb03ac52014-01-25 00:32:01 +00001032 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001033#else
Mark Seaborneb03ac52014-01-25 00:32:01 +00001034 raw_ostream &DebugOut = nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001035#endif
1036
Benjamin Kramer43a772e2011-09-19 17:56:04 +00001037 for (Index = Start; Index < End; Index += Size) {
1038 MCInst Inst;
Owen Andersona0c3b972011-09-15 23:38:46 +00001039
Davide Italianof0706882015-10-01 21:57:09 +00001040 // AArch64 ELF binaries can interleave data and text in the
1041 // same section. We rely on the markers introduced to
1042 // understand what we need to dump.
1043 if (Obj->isELF() && Obj->getArch() == Triple::aarch64) {
1044 uint64_t Stride = 0;
1045
1046 auto DAI = std::lower_bound(DataMappingSymsAddr.begin(),
1047 DataMappingSymsAddr.end(), Index);
1048 if (DAI != DataMappingSymsAddr.end() && *DAI == Index) {
1049 // Switch to data.
1050 while (Index < End) {
1051 outs() << format("%8" PRIx64 ":", SectionAddr + Index);
1052 outs() << "\t";
1053 if (Index + 4 <= End) {
1054 Stride = 4;
1055 dumpBytes(Bytes.slice(Index, 4), outs());
1056 outs() << "\t.word";
1057 } else if (Index + 2 <= End) {
1058 Stride = 2;
1059 dumpBytes(Bytes.slice(Index, 2), outs());
1060 outs() << "\t.short";
1061 } else {
1062 Stride = 1;
1063 dumpBytes(Bytes.slice(Index, 1), outs());
1064 outs() << "\t.byte";
1065 }
1066 Index += Stride;
1067 outs() << "\n";
1068 auto TAI = std::lower_bound(TextMappingSymsAddr.begin(),
1069 TextMappingSymsAddr.end(), Index);
1070 if (TAI != TextMappingSymsAddr.end() && *TAI == Index)
1071 break;
1072 }
1073 }
1074 }
1075
1076 if (Index >= End)
1077 break;
1078
Rafael Espindola7fc5b872014-11-12 02:04:27 +00001079 if (DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
1080 SectionAddr + Index, DebugOut,
1081 CommentStream)) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +00001082 PIP.printInst(*IP, &Inst,
Colin LeMahieufb76b002015-05-28 19:07:14 +00001083 Bytes.slice(Index, Size),
1084 SectionAddr + Index, outs(), "", *STI);
Alp Tokere69170a2014-06-26 22:52:05 +00001085 outs() << CommentStream.str();
Ahmed Bougachaad1084d2013-05-24 00:39:57 +00001086 Comments.clear();
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001087
1088 // Try to resolve the target of a call, tail call, etc. to a specific
1089 // symbol.
David Majnemer2603a8fa2015-07-09 18:11:40 +00001090 if (MIA && (MIA->isCall(Inst) || MIA->isUnconditionalBranch(Inst) ||
1091 MIA->isConditionalBranch(Inst))) {
David Majnemer81afca62015-07-07 22:06:59 +00001092 uint64_t Target;
1093 if (MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target)) {
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001094 // In a relocatable object, the target's section must reside in
1095 // the same section as the call instruction or it is accessed
1096 // through a relocation.
1097 //
1098 // In a non-relocatable object, the target may be in any section.
1099 //
1100 // N.B. We don't walk the relocations in the relocatable case yet.
1101 auto *TargetSectionSymbols = &Symbols;
1102 if (!Obj->isRelocatableObject()) {
1103 auto SectionAddress = std::upper_bound(
1104 SectionAddresses.begin(), SectionAddresses.end(), Target,
1105 [](uint64_t LHS,
1106 const std::pair<uint64_t, SectionRef> &RHS) {
1107 return LHS < RHS.first;
1108 });
1109 if (SectionAddress != SectionAddresses.begin()) {
1110 --SectionAddress;
1111 TargetSectionSymbols = &AllSymbols[SectionAddress->second];
1112 } else {
1113 TargetSectionSymbols = nullptr;
1114 }
1115 }
David Majnemer2603a8fa2015-07-09 18:11:40 +00001116
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001117 // Find the first symbol in the section whose offset is less than
1118 // or equal to the target.
1119 if (TargetSectionSymbols) {
1120 auto TargetSym = std::upper_bound(
1121 TargetSectionSymbols->begin(), TargetSectionSymbols->end(),
1122 Target, [](uint64_t LHS,
1123 const std::pair<uint64_t, StringRef> &RHS) {
1124 return LHS < RHS.first;
1125 });
David Majnemer153722d2015-11-18 04:35:32 +00001126 if (TargetSym != TargetSectionSymbols->begin()) {
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001127 --TargetSym;
1128 uint64_t TargetAddress = std::get<0>(*TargetSym);
1129 StringRef TargetName = std::get<1>(*TargetSym);
1130 outs() << " <" << TargetName;
1131 uint64_t Disp = Target - TargetAddress;
1132 if (Disp)
1133 outs() << '+' << utohexstr(Disp);
1134 outs() << '>';
1135 }
David Majnemer81afca62015-07-07 22:06:59 +00001136 }
1137 }
1138 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00001139 outs() << "\n";
1140 } else {
1141 errs() << ToolName << ": warning: invalid instruction encoding\n";
1142 if (Size == 0)
1143 Size = 1; // skip illegible bytes
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001144 }
Michael J. Spencer51862b32011-10-13 22:17:18 +00001145
1146 // Print relocation for instruction.
1147 while (rel_cur != rel_end) {
Rafael Espindola0ad71d92015-06-30 03:41:26 +00001148 bool hidden = getHidden(*rel_cur);
Rafael Espindola96d071c2015-06-29 23:29:12 +00001149 uint64_t addr = rel_cur->getOffset();
Michael J. Spencer51862b32011-10-13 22:17:18 +00001150 SmallString<16> name;
1151 SmallString<32> val;
Owen Andersonfa3e5202011-10-25 20:35:53 +00001152
1153 // If this relocation is hidden, skip it.
Owen Andersonfa3e5202011-10-25 20:35:53 +00001154 if (hidden) goto skip_print_rel;
1155
Michael J. Spencer51862b32011-10-13 22:17:18 +00001156 // Stop when rel_cur's address is past the current instruction.
Owen Andersonf20e3e52011-10-25 20:15:39 +00001157 if (addr >= Index + Size) break;
Rafael Espindola41bb4322015-06-30 04:08:37 +00001158 rel_cur->getTypeName(name);
Davide Italianoccd53fe2015-08-05 07:18:31 +00001159 error(getRelocationValueString(*rel_cur, val));
Greg Fitzgerald18432272014-03-20 22:55:15 +00001160 outs() << format(Fmt.data(), SectionAddr + addr) << name
Benjamin Kramer82803112012-03-10 02:04:38 +00001161 << "\t" << val << "\n";
Michael J. Spencer51862b32011-10-13 22:17:18 +00001162
1163 skip_print_rel:
1164 ++rel_cur;
1165 }
Benjamin Kramer87ee76c2011-07-20 19:37:35 +00001166 }
Michael J. Spencer2670c252011-01-20 06:39:06 +00001167 }
1168 }
1169}
1170
Kevin Enderby98da6132015-01-20 21:47:46 +00001171void llvm::PrintRelocations(const ObjectFile *Obj) {
Greg Fitzgerald18432272014-03-20 22:55:15 +00001172 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
1173 "%08" PRIx64;
Rafael Espindolac66d7612014-08-17 19:09:37 +00001174 // Regular objdump doesn't print relocations in non-relocatable object
1175 // files.
1176 if (!Obj->isRelocatableObject())
1177 return;
1178
Colin LeMahieu77804be2015-07-29 15:45:39 +00001179 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
Alexey Samsonov48803e52014-03-13 14:37:36 +00001180 if (Section.relocation_begin() == Section.relocation_end())
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001181 continue;
1182 StringRef secname;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001183 error(Section.getName(secname));
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001184 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00001185 for (const RelocationRef &Reloc : Section.relocations()) {
Rafael Espindola0ad71d92015-06-30 03:41:26 +00001186 bool hidden = getHidden(Reloc);
Rafael Espindola96d071c2015-06-29 23:29:12 +00001187 uint64_t address = Reloc.getOffset();
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001188 SmallString<32> relocname;
1189 SmallString<32> valuestr;
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00001190 if (hidden)
1191 continue;
Rafael Espindola41bb4322015-06-30 04:08:37 +00001192 Reloc.getTypeName(relocname);
Davide Italianoccd53fe2015-08-05 07:18:31 +00001193 error(getRelocationValueString(Reloc, valuestr));
Greg Fitzgerald18432272014-03-20 22:55:15 +00001194 outs() << format(Fmt.data(), address) << " " << relocname << " "
1195 << valuestr << "\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001196 }
1197 outs() << "\n";
1198 }
1199}
1200
Kevin Enderby98da6132015-01-20 21:47:46 +00001201void llvm::PrintSectionHeaders(const ObjectFile *Obj) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001202 outs() << "Sections:\n"
1203 "Idx Name Size Address Type\n";
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001204 unsigned i = 0;
Colin LeMahieu77804be2015-07-29 15:45:39 +00001205 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001206 StringRef Name;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001207 error(Section.getName(Name));
Rafael Espindola80291272014-10-08 15:28:58 +00001208 uint64_t Address = Section.getAddress();
1209 uint64_t Size = Section.getSize();
1210 bool Text = Section.isText();
1211 bool Data = Section.isData();
1212 bool BSS = Section.isBSS();
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001213 std::string Type = (std::string(Text ? "TEXT " : "") +
Michael J. Spencer8f67d472011-10-13 20:37:20 +00001214 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
Alexey Samsonov48803e52014-03-13 14:37:36 +00001215 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
1216 Name.str().c_str(), Size, Address, Type.c_str());
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001217 ++i;
1218 }
1219}
1220
Kevin Enderby98da6132015-01-20 21:47:46 +00001221void llvm::PrintSectionContents(const ObjectFile *Obj) {
Rafael Espindola4453e42942014-06-13 03:07:50 +00001222 std::error_code EC;
Colin LeMahieu77804be2015-07-29 15:45:39 +00001223 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001224 StringRef Name;
1225 StringRef Contents;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001226 error(Section.getName(Name));
Rafael Espindola80291272014-10-08 15:28:58 +00001227 uint64_t BaseAddr = Section.getAddress();
David Majnemer185b5b12014-11-11 09:58:25 +00001228 uint64_t Size = Section.getSize();
1229 if (!Size)
1230 continue;
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001231
1232 outs() << "Contents of section " << Name << ":\n";
David Majnemer185b5b12014-11-11 09:58:25 +00001233 if (Section.isBSS()) {
Alexey Samsonov209095c2013-04-16 10:53:11 +00001234 outs() << format("<skipping contents of bss section at [%04" PRIx64
David Majnemer8f6b04c2014-07-14 16:20:14 +00001235 ", %04" PRIx64 ")>\n",
1236 BaseAddr, BaseAddr + Size);
Alexey Samsonov209095c2013-04-16 10:53:11 +00001237 continue;
1238 }
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001239
Davide Italianoccd53fe2015-08-05 07:18:31 +00001240 error(Section.getContents(Contents));
David Majnemer8f6b04c2014-07-14 16:20:14 +00001241
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001242 // Dump out the content as hex and printable ascii characters.
1243 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
Benjamin Kramer82803112012-03-10 02:04:38 +00001244 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001245 // Dump line of hex.
1246 for (std::size_t i = 0; i < 16; ++i) {
1247 if (i != 0 && i % 4 == 0)
1248 outs() << ' ';
1249 if (addr + i < end)
1250 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
1251 << hexdigit(Contents[addr + i] & 0xF, true);
1252 else
1253 outs() << " ";
1254 }
1255 // Print ascii.
1256 outs() << " ";
1257 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001258 if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001259 outs() << Contents[addr + i];
1260 else
1261 outs() << ".";
1262 }
1263 outs() << "\n";
1264 }
1265 }
1266}
1267
Kevin Enderby98da6132015-01-20 21:47:46 +00001268void llvm::PrintSymbolTable(const ObjectFile *o) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001269 outs() << "SYMBOL TABLE:\n";
1270
Rui Ueyama4e39f712014-03-18 18:58:51 +00001271 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
Davide Italianoe85abf72015-12-20 09:54:34 +00001272 printCOFFSymbolTable(coff);
Rui Ueyama4e39f712014-03-18 18:58:51 +00001273 return;
1274 }
1275 for (const SymbolRef &Symbol : o->symbols()) {
Rafael Espindolaed067c42015-07-03 18:19:00 +00001276 ErrorOr<uint64_t> AddressOrError = Symbol.getAddress();
Davide Italianoccd53fe2015-08-05 07:18:31 +00001277 error(AddressOrError.getError());
Rafael Espindolaed067c42015-07-03 18:19:00 +00001278 uint64_t Address = *AddressOrError;
Rafael Espindola2fa80cc2015-06-26 12:18:49 +00001279 SymbolRef::Type Type = Symbol.getType();
Rui Ueyama4e39f712014-03-18 18:58:51 +00001280 uint32_t Flags = Symbol.getFlags();
Rafael Espindola8bab8892015-08-07 23:27:14 +00001281 ErrorOr<section_iterator> SectionOrErr = Symbol.getSection();
1282 error(SectionOrErr.getError());
1283 section_iterator Section = *SectionOrErr;
Rafael Espindola75d5b542015-06-03 05:14:22 +00001284 StringRef Name;
1285 if (Type == SymbolRef::ST_Debug && Section != o->section_end()) {
1286 Section->getName(Name);
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001287 } else {
1288 ErrorOr<StringRef> NameOrErr = Symbol.getName();
Davide Italianoccd53fe2015-08-05 07:18:31 +00001289 error(NameOrErr.getError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001290 Name = *NameOrErr;
Rafael Espindola75d5b542015-06-03 05:14:22 +00001291 }
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001292
Rui Ueyama4e39f712014-03-18 18:58:51 +00001293 bool Global = Flags & SymbolRef::SF_Global;
1294 bool Weak = Flags & SymbolRef::SF_Weak;
1295 bool Absolute = Flags & SymbolRef::SF_Absolute;
Colin LeMahieubc2f47a2015-01-23 20:06:24 +00001296 bool Common = Flags & SymbolRef::SF_Common;
Davide Italianocd2514d2015-04-30 23:08:53 +00001297 bool Hidden = Flags & SymbolRef::SF_Hidden;
David Meyer1df4b842012-02-28 23:47:53 +00001298
Rui Ueyama4e39f712014-03-18 18:58:51 +00001299 char GlobLoc = ' ';
1300 if (Type != SymbolRef::ST_Unknown)
1301 GlobLoc = Global ? 'g' : 'l';
1302 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
1303 ? 'd' : ' ';
1304 char FileFunc = ' ';
1305 if (Type == SymbolRef::ST_File)
1306 FileFunc = 'f';
1307 else if (Type == SymbolRef::ST_Function)
1308 FileFunc = 'F';
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001309
Rui Ueyama4e39f712014-03-18 18:58:51 +00001310 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
1311 "%08" PRIx64;
Michael J. Spencerd857c1c2013-01-10 22:40:50 +00001312
Rui Ueyama4e39f712014-03-18 18:58:51 +00001313 outs() << format(Fmt, Address) << " "
1314 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
1315 << (Weak ? 'w' : ' ') // Weak?
1316 << ' ' // Constructor. Not supported yet.
1317 << ' ' // Warning. Not supported yet.
1318 << ' ' // Indirect reference to another symbol.
1319 << Debug // Debugging (d) or dynamic (D) symbol.
1320 << FileFunc // Name of function (F), file (f) or object (O).
1321 << ' ';
1322 if (Absolute) {
1323 outs() << "*ABS*";
Colin LeMahieubc2f47a2015-01-23 20:06:24 +00001324 } else if (Common) {
1325 outs() << "*COM*";
Rui Ueyama4e39f712014-03-18 18:58:51 +00001326 } else if (Section == o->section_end()) {
1327 outs() << "*UND*";
1328 } else {
1329 if (const MachOObjectFile *MachO =
1330 dyn_cast<const MachOObjectFile>(o)) {
1331 DataRefImpl DR = Section->getRawDataRefImpl();
1332 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
1333 outs() << SegmentName << ",";
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001334 }
Rui Ueyama4e39f712014-03-18 18:58:51 +00001335 StringRef SectionName;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001336 error(Section->getName(SectionName));
Rui Ueyama4e39f712014-03-18 18:58:51 +00001337 outs() << SectionName;
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001338 }
Rafael Espindola5f7ade22015-06-23 15:45:38 +00001339
1340 outs() << '\t';
Rafael Espindolaae3ac082015-06-23 18:34:25 +00001341 if (Common || isa<ELFObjectFileBase>(o)) {
Rafael Espindoladbb6bd32015-06-25 22:10:04 +00001342 uint64_t Val =
1343 Common ? Symbol.getAlignment() : ELFSymbolRef(Symbol).getSize();
Rafael Espindolaae3ac082015-06-23 18:34:25 +00001344 outs() << format("\t %08" PRIx64 " ", Val);
1345 }
Rafael Espindola5f7ade22015-06-23 15:45:38 +00001346
Davide Italianocd2514d2015-04-30 23:08:53 +00001347 if (Hidden) {
1348 outs() << ".hidden ";
1349 }
1350 outs() << Name
Rui Ueyama4e39f712014-03-18 18:58:51 +00001351 << '\n';
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001352 }
1353}
1354
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00001355static void PrintUnwindInfo(const ObjectFile *o) {
1356 outs() << "Unwind info:\n\n";
1357
1358 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
1359 printCOFFUnwindInfo(coff);
Tim Northover4bd286a2014-08-01 13:07:19 +00001360 } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1361 printMachOUnwindInfo(MachO);
1362 else {
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00001363 // TODO: Extract DWARF dump tool to objdump.
1364 errs() << "This operation is only currently supported "
Tim Northover4bd286a2014-08-01 13:07:19 +00001365 "for COFF and MachO object files.\n";
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00001366 return;
1367 }
1368}
1369
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001370void llvm::printExportsTrie(const ObjectFile *o) {
Nick Kledzikd04bc352014-08-30 00:20:14 +00001371 outs() << "Exports trie:\n";
1372 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1373 printMachOExportsTrie(MachO);
1374 else {
1375 errs() << "This operation is only currently supported "
1376 "for Mach-O executable files.\n";
1377 return;
1378 }
1379}
1380
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001381void llvm::printRebaseTable(const ObjectFile *o) {
Nick Kledzikac431442014-09-12 21:34:15 +00001382 outs() << "Rebase table:\n";
1383 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1384 printMachORebaseTable(MachO);
1385 else {
1386 errs() << "This operation is only currently supported "
1387 "for Mach-O executable files.\n";
1388 return;
1389 }
1390}
1391
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001392void llvm::printBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +00001393 outs() << "Bind table:\n";
1394 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1395 printMachOBindTable(MachO);
1396 else {
1397 errs() << "This operation is only currently supported "
1398 "for Mach-O executable files.\n";
1399 return;
1400 }
1401}
1402
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001403void llvm::printLazyBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +00001404 outs() << "Lazy bind table:\n";
1405 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1406 printMachOLazyBindTable(MachO);
1407 else {
1408 errs() << "This operation is only currently supported "
1409 "for Mach-O executable files.\n";
1410 return;
1411 }
1412}
1413
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001414void llvm::printWeakBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +00001415 outs() << "Weak bind table:\n";
1416 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1417 printMachOWeakBindTable(MachO);
1418 else {
1419 errs() << "This operation is only currently supported "
1420 "for Mach-O executable files.\n";
1421 return;
1422 }
1423}
Nick Kledzikac431442014-09-12 21:34:15 +00001424
Adrian Prantl437105a2015-07-08 02:04:15 +00001425/// Dump the raw contents of the __clangast section so the output can be piped
1426/// into llvm-bcanalyzer.
1427void llvm::printRawClangAST(const ObjectFile *Obj) {
1428 if (outs().is_displayed()) {
1429 errs() << "The -raw-clang-ast option will dump the raw binary contents of "
1430 "the clang ast section.\n"
1431 "Please redirect the output to a file or another program such as "
1432 "llvm-bcanalyzer.\n";
1433 return;
1434 }
1435
1436 StringRef ClangASTSectionName("__clangast");
1437 if (isa<COFFObjectFile>(Obj)) {
1438 ClangASTSectionName = "clangast";
1439 }
1440
1441 Optional<object::SectionRef> ClangASTSection;
Colin LeMahieu77804be2015-07-29 15:45:39 +00001442 for (auto Sec : ToolSectionFilter(*Obj)) {
Adrian Prantl437105a2015-07-08 02:04:15 +00001443 StringRef Name;
1444 Sec.getName(Name);
1445 if (Name == ClangASTSectionName) {
1446 ClangASTSection = Sec;
1447 break;
1448 }
1449 }
1450 if (!ClangASTSection)
1451 return;
1452
1453 StringRef ClangASTContents;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001454 error(ClangASTSection.getValue().getContents(ClangASTContents));
Adrian Prantl437105a2015-07-08 02:04:15 +00001455 outs().write(ClangASTContents.data(), ClangASTContents.size());
1456}
1457
Sanjoy Das6f567a42015-06-22 18:03:02 +00001458static void printFaultMaps(const ObjectFile *Obj) {
1459 const char *FaultMapSectionName = nullptr;
1460
1461 if (isa<ELFObjectFileBase>(Obj)) {
1462 FaultMapSectionName = ".llvm_faultmaps";
1463 } else if (isa<MachOObjectFile>(Obj)) {
1464 FaultMapSectionName = "__llvm_faultmaps";
1465 } else {
1466 errs() << "This operation is only currently supported "
1467 "for ELF and Mach-O executable files.\n";
1468 return;
1469 }
1470
1471 Optional<object::SectionRef> FaultMapSection;
1472
Colin LeMahieu77804be2015-07-29 15:45:39 +00001473 for (auto Sec : ToolSectionFilter(*Obj)) {
Sanjoy Das6f567a42015-06-22 18:03:02 +00001474 StringRef Name;
1475 Sec.getName(Name);
1476 if (Name == FaultMapSectionName) {
1477 FaultMapSection = Sec;
1478 break;
1479 }
1480 }
1481
1482 outs() << "FaultMap table:\n";
1483
1484 if (!FaultMapSection.hasValue()) {
1485 outs() << "<not found>\n";
1486 return;
1487 }
1488
1489 StringRef FaultMapContents;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001490 error(FaultMapSection.getValue().getContents(FaultMapContents));
Sanjoy Das6f567a42015-06-22 18:03:02 +00001491
1492 FaultMapParser FMP(FaultMapContents.bytes_begin(),
1493 FaultMapContents.bytes_end());
1494
1495 outs() << FMP;
1496}
1497
Rui Ueyamac2bed422013-09-27 21:04:00 +00001498static void printPrivateFileHeader(const ObjectFile *o) {
Davide Italiano540e9212015-12-19 22:09:40 +00001499 if (o->isELF())
Rui Ueyamac2bed422013-09-27 21:04:00 +00001500 printELFFileHeader(o);
Davide Italiano540e9212015-12-19 22:09:40 +00001501 else if (o->isCOFF())
Rui Ueyamac2bed422013-09-27 21:04:00 +00001502 printCOFFFileHeader(o);
Davide Italiano540e9212015-12-19 22:09:40 +00001503 else if (o->isMachO())
Kevin Enderbyb76d3862014-08-22 20:35:18 +00001504 printMachOFileHeader(o);
Davide Italiano540e9212015-12-19 22:09:40 +00001505 else
1506 report_fatal_error("Invalid/Unsupported object file format");
Rui Ueyamac2bed422013-09-27 21:04:00 +00001507}
1508
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001509static void DumpObject(const ObjectFile *o) {
Adrian Prantl437105a2015-07-08 02:04:15 +00001510 // Avoid other output when using a raw option.
1511 if (!RawClangAST) {
1512 outs() << '\n';
1513 outs() << o->getFileName()
1514 << ":\tfile format " << o->getFileFormatName() << "\n\n";
1515 }
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001516
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001517 if (Disassemble)
Michael J. Spencer51862b32011-10-13 22:17:18 +00001518 DisassembleObject(o, Relocations);
1519 if (Relocations && !Disassemble)
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001520 PrintRelocations(o);
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001521 if (SectionHeaders)
1522 PrintSectionHeaders(o);
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001523 if (SectionContents)
1524 PrintSectionContents(o);
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001525 if (SymbolTable)
1526 PrintSymbolTable(o);
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00001527 if (UnwindInfo)
1528 PrintUnwindInfo(o);
Rui Ueyamac2bed422013-09-27 21:04:00 +00001529 if (PrivateHeaders)
1530 printPrivateFileHeader(o);
Nick Kledzikd04bc352014-08-30 00:20:14 +00001531 if (ExportsTrie)
1532 printExportsTrie(o);
Nick Kledzikac431442014-09-12 21:34:15 +00001533 if (Rebase)
1534 printRebaseTable(o);
Nick Kledzik56ebef42014-09-16 01:41:51 +00001535 if (Bind)
1536 printBindTable(o);
1537 if (LazyBind)
1538 printLazyBindTable(o);
1539 if (WeakBind)
1540 printWeakBindTable(o);
Adrian Prantl437105a2015-07-08 02:04:15 +00001541 if (RawClangAST)
1542 printRawClangAST(o);
Sanjoy Das6f567a42015-06-22 18:03:02 +00001543 if (PrintFaultMaps)
1544 printFaultMaps(o);
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001545}
1546
1547/// @brief Dump each object file in \a a;
1548static void DumpArchive(const Archive *a) {
Kevin Enderby7a969422015-11-05 19:24:56 +00001549 for (auto &ErrorOrChild : a->children()) {
1550 if (std::error_code EC = ErrorOrChild.getError())
1551 report_error(a->getFileName(), EC);
1552 const Archive::Child &C = *ErrorOrChild;
Davide Italiano95dd3cb2015-08-03 21:46:32 +00001553 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
Davide Italianoccd53fe2015-08-05 07:18:31 +00001554 if (std::error_code EC = ChildOrErr.getError())
Mark Seaborneb03ac52014-01-25 00:32:01 +00001555 if (EC != object_error::invalid_file_type)
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +00001556 report_error(a->getFileName(), EC);
Rafael Espindolaae460022014-06-16 16:08:36 +00001557 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001558 DumpObject(o);
1559 else
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +00001560 report_error(a->getFileName(), object_error::invalid_file_type);
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001561 }
1562}
1563
1564/// @brief Open file and figure out how to dump it.
1565static void DumpInput(StringRef file) {
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001566
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001567 // If we are using the Mach-O specific object file parser, then let it parse
1568 // the file and process the command line options. So the -arch flags can
1569 // be used to select specific slices, etc.
1570 if (MachOOpt) {
1571 ParseInputMachO(file);
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001572 return;
1573 }
1574
1575 // Attempt to open the binary.
Rafael Espindola48af1c22014-08-19 18:44:46 +00001576 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
Davide Italianoccd53fe2015-08-05 07:18:31 +00001577 if (std::error_code EC = BinaryOrErr.getError())
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +00001578 report_error(file, EC);
Rafael Espindola48af1c22014-08-19 18:44:46 +00001579 Binary &Binary = *BinaryOrErr.get().getBinary();
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001580
Rafael Espindola3f6481d2014-08-01 14:31:55 +00001581 if (Archive *a = dyn_cast<Archive>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001582 DumpArchive(a);
Rafael Espindola3f6481d2014-08-01 14:31:55 +00001583 else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001584 DumpObject(o);
Jim Grosbachaf9aec02012-08-07 17:53:14 +00001585 else
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +00001586 report_error(file, object_error::invalid_file_type);
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001587}
1588
Michael J. Spencer2670c252011-01-20 06:39:06 +00001589int main(int argc, char **argv) {
1590 // Print a stack trace if we signal out.
1591 sys::PrintStackTraceOnErrorSignal();
1592 PrettyStackTraceProgram X(argc, argv);
1593 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
1594
1595 // Initialize targets and assembly printers/parsers.
1596 llvm::InitializeAllTargetInfos();
Evan Cheng8c886a42011-07-22 21:58:54 +00001597 llvm::InitializeAllTargetMCs();
Michael J. Spencer2670c252011-01-20 06:39:06 +00001598 llvm::InitializeAllDisassemblers();
1599
Pete Cooper28fb4fc2012-05-03 23:20:10 +00001600 // Register the target printer for --version.
1601 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
1602
Michael J. Spencer2670c252011-01-20 06:39:06 +00001603 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
1604 TripleName = Triple::normalize(TripleName);
1605
1606 ToolName = argv[0];
1607
1608 // Defaults to a.out if no filenames specified.
1609 if (InputFilenames.size() == 0)
1610 InputFilenames.push_back("a.out");
1611
Colin LeMahieuf34933e2015-07-23 20:58:49 +00001612 if (DisassembleAll)
1613 Disassemble = true;
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001614 if (!Disassemble
1615 && !Relocations
1616 && !SectionHeaders
1617 && !SectionContents
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00001618 && !SymbolTable
Michael J. Spencer209565db2013-01-06 03:56:49 +00001619 && !UnwindInfo
Nick Kledzikd04bc352014-08-30 00:20:14 +00001620 && !PrivateHeaders
Nick Kledzikac431442014-09-12 21:34:15 +00001621 && !ExportsTrie
Nick Kledzik56ebef42014-09-16 01:41:51 +00001622 && !Rebase
1623 && !Bind
1624 && !LazyBind
Kevin Enderby131d1772015-01-09 19:22:37 +00001625 && !WeakBind
Adrian Prantl437105a2015-07-08 02:04:15 +00001626 && !RawClangAST
Kevin Enderby13023a12015-01-15 23:19:11 +00001627 && !(UniversalHeaders && MachOOpt)
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001628 && !(ArchiveHeaders && MachOOpt)
Kevin Enderby69fe98d2015-01-23 18:52:17 +00001629 && !(IndirectSymbols && MachOOpt)
Kevin Enderby9a509442015-01-27 21:28:24 +00001630 && !(DataInCode && MachOOpt)
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001631 && !(LinkOptHints && MachOOpt)
Kevin Enderbycd66be52015-03-11 22:06:32 +00001632 && !(InfoPlist && MachOOpt)
Kevin Enderbybc847fa2015-03-16 20:08:09 +00001633 && !(DylibsUsed && MachOOpt)
1634 && !(DylibId && MachOOpt)
Kevin Enderby0fc11822015-04-01 20:57:01 +00001635 && !(ObjcMetaData && MachOOpt)
Colin LeMahieufcc32762015-07-29 19:08:10 +00001636 && !(FilterSections.size() != 0 && MachOOpt)
Pete Cooper98052532015-12-18 19:45:38 +00001637 && !PrintFaultMaps) {
Michael J. Spencer2670c252011-01-20 06:39:06 +00001638 cl::PrintHelpMessage();
1639 return 2;
1640 }
1641
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001642 std::for_each(InputFilenames.begin(), InputFilenames.end(),
1643 DumpInput);
Michael J. Spencer2670c252011-01-20 06:39:06 +00001644
Davide Italianoccd53fe2015-08-05 07:18:31 +00001645 return EXIT_SUCCESS;
Michael J. Spencer2670c252011-01-20 06:39:06 +00001646}