blob: 97243cb8016372a965c182195fbd8fbba2aa825b [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;
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000180static int ReturnValue = EXIT_SUCCESS;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000181
Colin LeMahieu77804be2015-07-29 15:45:39 +0000182namespace {
Colin LeMahieufcc32762015-07-29 19:08:10 +0000183typedef std::function<bool(llvm::object::SectionRef const &)> FilterPredicate;
Colin LeMahieu77804be2015-07-29 15:45:39 +0000184
185class SectionFilterIterator {
186public:
187 SectionFilterIterator(FilterPredicate P,
188 llvm::object::section_iterator const &I,
189 llvm::object::section_iterator const &E)
190 : Predicate(P), Iterator(I), End(E) {
191 ScanPredicate();
192 }
193 llvm::object::SectionRef operator*() const { return *Iterator; }
194 SectionFilterIterator &operator++() {
195 ++Iterator;
196 ScanPredicate();
197 return *this;
198 }
199 bool operator!=(SectionFilterIterator const &Other) const {
200 return Iterator != Other.Iterator;
201 }
202
203private:
204 void ScanPredicate() {
205 while (Iterator != End && Predicate(*Iterator)) {
206 ++Iterator;
207 }
208 }
209 FilterPredicate Predicate;
210 llvm::object::section_iterator Iterator;
211 llvm::object::section_iterator End;
212};
213
214class SectionFilter {
215public:
216 SectionFilter(FilterPredicate P, llvm::object::ObjectFile const &O)
217 : Predicate(P), Object(O) {}
218 SectionFilterIterator begin() {
219 return SectionFilterIterator(Predicate, Object.section_begin(),
220 Object.section_end());
221 }
222 SectionFilterIterator end() {
223 return SectionFilterIterator(Predicate, Object.section_end(),
224 Object.section_end());
225 }
226
227private:
228 FilterPredicate Predicate;
229 llvm::object::ObjectFile const &Object;
230};
231SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O) {
Colin LeMahieu77804be2015-07-29 15:45:39 +0000232 return SectionFilter([](llvm::object::SectionRef const &S) {
Colin LeMahieufcc32762015-07-29 19:08:10 +0000233 if(FilterSections.empty())
234 return false;
Colin LeMahieu77804be2015-07-29 15:45:39 +0000235 llvm::StringRef String;
236 std::error_code error = S.getName(String);
Colin LeMahieufcc32762015-07-29 19:08:10 +0000237 if (error)
238 return true;
239 return std::find(FilterSections.begin(),
240 FilterSections.end(),
241 String) == FilterSections.end();
Colin LeMahieu77804be2015-07-29 15:45:39 +0000242 },
243 O);
244}
245}
246
Rafael Espindola4453e42942014-06-13 03:07:50 +0000247bool llvm::error(std::error_code EC) {
Mark Seaborneb03ac52014-01-25 00:32:01 +0000248 if (!EC)
249 return false;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000250
Mark Seaborneb03ac52014-01-25 00:32:01 +0000251 outs() << ToolName << ": error reading file: " << EC.message() << ".\n";
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000252 outs().flush();
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000253 ReturnValue = EXIT_FAILURE;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000254 return true;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000255}
256
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +0000257static void report_error(StringRef File, std::error_code EC) {
258 assert(EC);
259 errs() << ToolName << ": '" << File << "': " << EC.message() << ".\n";
260 ReturnValue = EXIT_FAILURE;
261}
262
Craig Toppere6cb63e2014-04-25 04:24:47 +0000263static const Target *getTarget(const ObjectFile *Obj = nullptr) {
Michael J. Spencer2670c252011-01-20 06:39:06 +0000264 // Figure out the target triple.
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000265 llvm::Triple TheTriple("unknown-unknown-unknown");
Michael J. Spencer05350e6d2011-01-20 07:22:04 +0000266 if (TripleName.empty()) {
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000267 if (Obj) {
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000268 TheTriple.setArch(Triple::ArchType(Obj->getArch()));
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000269 // TheTriple defaults to ELF, and COFF doesn't have an environment:
270 // the best we can do here is indicate that it is mach-o.
271 if (Obj->isMachO())
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000272 TheTriple.setObjectFormat(Triple::MachO);
Saleem Abdulrasool98938f12014-04-17 06:17:23 +0000273
274 if (Obj->isCOFF()) {
275 const auto COFFObj = dyn_cast<COFFObjectFile>(Obj);
276 if (COFFObj->getArch() == Triple::thumb)
277 TheTriple.setTriple("thumbv7-windows");
278 }
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000279 }
Michael J. Spencer05350e6d2011-01-20 07:22:04 +0000280 } else
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000281 TheTriple.setTriple(Triple::normalize(TripleName));
Michael J. Spencer2670c252011-01-20 06:39:06 +0000282
283 // Get the target specific parser.
284 std::string Error;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000285 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
286 Error);
287 if (!TheTarget) {
288 errs() << ToolName << ": " << Error;
Craig Toppere6cb63e2014-04-25 04:24:47 +0000289 return nullptr;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000290 }
Michael J. Spencer2670c252011-01-20 06:39:06 +0000291
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000292 // Update the triple name and return the found target.
293 TripleName = TheTriple.getTriple();
294 return TheTarget;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000295}
296
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000297bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
Rafael Espindola704cd842015-07-06 15:47:43 +0000298 return a.getOffset() < b.getOffset();
Michael J. Spencer51862b32011-10-13 22:17:18 +0000299}
300
Colin LeMahieufb76b002015-05-28 19:07:14 +0000301namespace {
302class PrettyPrinter {
303public:
Colin LeMahieu0b5890d2015-05-28 20:59:08 +0000304 virtual ~PrettyPrinter(){}
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000305 virtual void printInst(MCInstPrinter &IP, const MCInst *MI,
Colin LeMahieufb76b002015-05-28 19:07:14 +0000306 ArrayRef<uint8_t> Bytes, uint64_t Address,
307 raw_ostream &OS, StringRef Annot,
308 MCSubtargetInfo const &STI) {
309 outs() << format("%8" PRIx64 ":", Address);
310 if (!NoShowRawInsn) {
311 outs() << "\t";
312 dumpBytes(Bytes, outs());
313 }
314 IP.printInst(MI, outs(), "", STI);
315 }
316};
317PrettyPrinter PrettyPrinterInst;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000318class HexagonPrettyPrinter : public PrettyPrinter {
319public:
320 void printLead(ArrayRef<uint8_t> Bytes, uint64_t Address,
321 raw_ostream &OS) {
322 uint32_t opcode =
323 (Bytes[3] << 24) | (Bytes[2] << 16) | (Bytes[1] << 8) | Bytes[0];
324 OS << format("%8" PRIx64 ":", Address);
325 if (!NoShowRawInsn) {
326 OS << "\t";
327 dumpBytes(Bytes.slice(0, 4), OS);
328 OS << format("%08" PRIx32, opcode);
329 }
330 }
331 void printInst(MCInstPrinter &IP, const MCInst *MI,
332 ArrayRef<uint8_t> Bytes, uint64_t Address,
333 raw_ostream &OS, StringRef Annot,
334 MCSubtargetInfo const &STI) override {
335 std::string Buffer;
336 {
337 raw_string_ostream TempStream(Buffer);
338 IP.printInst(MI, TempStream, "", STI);
339 }
340 StringRef Contents(Buffer);
341 // Split off bundle attributes
342 auto PacketBundle = Contents.rsplit('\n');
343 // Split off first instruction from the rest
344 auto HeadTail = PacketBundle.first.split('\n');
345 auto Preamble = " { ";
346 auto Separator = "";
347 while(!HeadTail.first.empty()) {
348 OS << Separator;
349 Separator = "\n";
350 printLead(Bytes, Address, OS);
351 OS << Preamble;
352 Preamble = " ";
353 StringRef Inst;
354 auto Duplex = HeadTail.first.split('\v');
355 if(!Duplex.second.empty()){
356 OS << Duplex.first;
357 OS << "; ";
358 Inst = Duplex.second;
359 }
360 else
361 Inst = HeadTail.first;
362 OS << Inst;
363 Bytes = Bytes.slice(4);
364 Address += 4;
365 HeadTail = HeadTail.second.split('\n');
366 }
367 OS << " } " << PacketBundle.second;
368 }
369};
370HexagonPrettyPrinter HexagonPrettyPrinterInst;
Colin LeMahieu35436a22015-05-29 14:48:25 +0000371PrettyPrinter &selectPrettyPrinter(Triple const &Triple) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000372 switch(Triple.getArch()) {
373 default:
374 return PrettyPrinterInst;
375 case Triple::hexagon:
376 return HexagonPrettyPrinterInst;
377 }
Colin LeMahieufb76b002015-05-28 19:07:14 +0000378}
379}
380
Rafael Espindola37070a52015-06-03 04:48:06 +0000381template <class ELFT>
Rafael Espindola37070a52015-06-03 04:48:06 +0000382static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
383 DataRefImpl Rel,
384 SmallVectorImpl<char> &Result) {
385 typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
386 typedef typename ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr;
Rafael Espindola7f162ec2015-07-02 14:21:38 +0000387 typedef typename ELFObjectFile<ELFT>::Elf_Rel Elf_Rel;
388 typedef typename ELFObjectFile<ELFT>::Elf_Rela Elf_Rela;
389
Rafael Espindola37070a52015-06-03 04:48:06 +0000390 const ELFFile<ELFT> &EF = *Obj->getELFFile();
391
Rafael Espindola6def3042015-07-01 12:56:27 +0000392 ErrorOr<const Elf_Shdr *> SecOrErr = EF.getSection(Rel.d.a);
393 if (std::error_code EC = SecOrErr.getError())
394 return EC;
395 const Elf_Shdr *Sec = *SecOrErr;
396 ErrorOr<const Elf_Shdr *> SymTabOrErr = EF.getSection(Sec->sh_link);
397 if (std::error_code EC = SymTabOrErr.getError())
398 return EC;
399 const Elf_Shdr *SymTab = *SymTabOrErr;
Rafael Espindola719dc7c2015-06-29 12:38:31 +0000400 assert(SymTab->sh_type == ELF::SHT_SYMTAB ||
401 SymTab->sh_type == ELF::SHT_DYNSYM);
Rafael Espindola6def3042015-07-01 12:56:27 +0000402 ErrorOr<const Elf_Shdr *> StrTabSec = EF.getSection(SymTab->sh_link);
403 if (std::error_code EC = StrTabSec.getError())
404 return EC;
405 ErrorOr<StringRef> StrTabOrErr = EF.getStringTable(*StrTabSec);
Rafael Espindola6a1bfb22015-06-29 14:39:25 +0000406 if (std::error_code EC = StrTabOrErr.getError())
407 return EC;
408 StringRef StrTab = *StrTabOrErr;
Rafael Espindola37070a52015-06-03 04:48:06 +0000409 uint8_t type;
410 StringRef res;
411 int64_t addend = 0;
412 uint16_t symbol_index = 0;
Rafael Espindola6def3042015-07-01 12:56:27 +0000413 switch (Sec->sh_type) {
Rafael Espindola37070a52015-06-03 04:48:06 +0000414 default:
415 return object_error::parse_failed;
416 case ELF::SHT_REL: {
Rafael Espindola7f162ec2015-07-02 14:21:38 +0000417 const Elf_Rel *ERel = Obj->getRel(Rel);
418 type = ERel->getType(EF.isMips64EL());
419 symbol_index = ERel->getSymbol(EF.isMips64EL());
Rafael Espindola37070a52015-06-03 04:48:06 +0000420 // TODO: Read implicit addend from section data.
421 break;
422 }
423 case ELF::SHT_RELA: {
Rafael Espindola7f162ec2015-07-02 14:21:38 +0000424 const Elf_Rela *ERela = Obj->getRela(Rel);
425 type = ERela->getType(EF.isMips64EL());
426 symbol_index = ERela->getSymbol(EF.isMips64EL());
427 addend = ERela->r_addend;
Rafael Espindola37070a52015-06-03 04:48:06 +0000428 break;
429 }
430 }
431 const Elf_Sym *symb =
Rafael Espindola6def3042015-07-01 12:56:27 +0000432 EF.template getEntry<Elf_Sym>(Sec->sh_link, symbol_index);
Rafael Espindola75d5b542015-06-03 05:14:22 +0000433 StringRef Target;
Rafael Espindola6def3042015-07-01 12:56:27 +0000434 ErrorOr<const Elf_Shdr *> SymSec = EF.getSection(symb);
435 if (std::error_code EC = SymSec.getError())
436 return EC;
Rafael Espindola75d5b542015-06-03 05:14:22 +0000437 if (symb->getType() == ELF::STT_SECTION) {
Rafael Espindola6def3042015-07-01 12:56:27 +0000438 ErrorOr<StringRef> SecName = EF.getSectionName(*SymSec);
Rafael Espindola75d5b542015-06-03 05:14:22 +0000439 if (std::error_code EC = SecName.getError())
440 return EC;
441 Target = *SecName;
442 } else {
Rafael Espindola44c28712015-06-29 21:24:55 +0000443 ErrorOr<StringRef> SymName = symb->getName(StrTab);
Rafael Espindola75d5b542015-06-03 05:14:22 +0000444 if (!SymName)
445 return SymName.getError();
446 Target = *SymName;
447 }
Rafael Espindola37070a52015-06-03 04:48:06 +0000448 switch (EF.getHeader()->e_machine) {
449 case ELF::EM_X86_64:
450 switch (type) {
451 case ELF::R_X86_64_PC8:
452 case ELF::R_X86_64_PC16:
453 case ELF::R_X86_64_PC32: {
454 std::string fmtbuf;
455 raw_string_ostream fmt(fmtbuf);
Rafael Espindola75d5b542015-06-03 05:14:22 +0000456 fmt << Target << (addend < 0 ? "" : "+") << addend << "-P";
Rafael Espindola37070a52015-06-03 04:48:06 +0000457 fmt.flush();
458 Result.append(fmtbuf.begin(), fmtbuf.end());
459 } break;
460 case ELF::R_X86_64_8:
461 case ELF::R_X86_64_16:
462 case ELF::R_X86_64_32:
463 case ELF::R_X86_64_32S:
464 case ELF::R_X86_64_64: {
465 std::string fmtbuf;
466 raw_string_ostream fmt(fmtbuf);
Rafael Espindola75d5b542015-06-03 05:14:22 +0000467 fmt << Target << (addend < 0 ? "" : "+") << addend;
Rafael Espindola37070a52015-06-03 04:48:06 +0000468 fmt.flush();
469 Result.append(fmtbuf.begin(), fmtbuf.end());
470 } break;
471 default:
472 res = "Unknown";
473 }
474 break;
475 case ELF::EM_AARCH64: {
476 std::string fmtbuf;
477 raw_string_ostream fmt(fmtbuf);
Rafael Espindola75d5b542015-06-03 05:14:22 +0000478 fmt << Target;
Rafael Espindola37070a52015-06-03 04:48:06 +0000479 if (addend != 0)
480 fmt << (addend < 0 ? "" : "+") << addend;
481 fmt.flush();
482 Result.append(fmtbuf.begin(), fmtbuf.end());
483 break;
484 }
485 case ELF::EM_386:
486 case ELF::EM_ARM:
487 case ELF::EM_HEXAGON:
488 case ELF::EM_MIPS:
Rafael Espindola75d5b542015-06-03 05:14:22 +0000489 res = Target;
Rafael Espindola37070a52015-06-03 04:48:06 +0000490 break;
491 default:
492 res = "Unknown";
493 }
494 if (Result.empty())
495 Result.append(res.begin(), res.end());
Rui Ueyama7d099192015-06-09 15:20:42 +0000496 return std::error_code();
Rafael Espindola37070a52015-06-03 04:48:06 +0000497}
498
499static std::error_code getRelocationValueString(const ELFObjectFileBase *Obj,
500 const RelocationRef &RelRef,
501 SmallVectorImpl<char> &Result) {
502 DataRefImpl Rel = RelRef.getRawDataRefImpl();
503 if (auto *ELF32LE = dyn_cast<ELF32LEObjectFile>(Obj))
504 return getRelocationValueString(ELF32LE, Rel, Result);
505 if (auto *ELF64LE = dyn_cast<ELF64LEObjectFile>(Obj))
506 return getRelocationValueString(ELF64LE, Rel, Result);
507 if (auto *ELF32BE = dyn_cast<ELF32BEObjectFile>(Obj))
508 return getRelocationValueString(ELF32BE, Rel, Result);
509 auto *ELF64BE = cast<ELF64BEObjectFile>(Obj);
510 return getRelocationValueString(ELF64BE, Rel, Result);
511}
512
513static std::error_code getRelocationValueString(const COFFObjectFile *Obj,
514 const RelocationRef &Rel,
515 SmallVectorImpl<char> &Result) {
516 symbol_iterator SymI = Rel.getSymbol();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000517 ErrorOr<StringRef> SymNameOrErr = SymI->getName();
518 if (std::error_code EC = SymNameOrErr.getError())
Rafael Espindola37070a52015-06-03 04:48:06 +0000519 return EC;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000520 StringRef SymName = *SymNameOrErr;
Rafael Espindola37070a52015-06-03 04:48:06 +0000521 Result.append(SymName.begin(), SymName.end());
Rui Ueyama7d099192015-06-09 15:20:42 +0000522 return std::error_code();
Rafael Espindola37070a52015-06-03 04:48:06 +0000523}
524
525static void printRelocationTargetName(const MachOObjectFile *O,
526 const MachO::any_relocation_info &RE,
527 raw_string_ostream &fmt) {
528 bool IsScattered = O->isRelocationScattered(RE);
529
530 // Target of a scattered relocation is an address. In the interest of
531 // generating pretty output, scan through the symbol table looking for a
532 // symbol that aligns with that address. If we find one, print it.
533 // Otherwise, we just print the hex address of the target.
534 if (IsScattered) {
535 uint32_t Val = O->getPlainRelocationSymbolNum(RE);
536
537 for (const SymbolRef &Symbol : O->symbols()) {
538 std::error_code ec;
Rafael Espindolaed067c42015-07-03 18:19:00 +0000539 ErrorOr<uint64_t> Addr = Symbol.getAddress();
540 if ((ec = Addr.getError()))
Rafael Espindola37070a52015-06-03 04:48:06 +0000541 report_fatal_error(ec.message());
Rafael Espindolaed067c42015-07-03 18:19:00 +0000542 if (*Addr != Val)
Rafael Espindola37070a52015-06-03 04:48:06 +0000543 continue;
Rafael Espindolaed067c42015-07-03 18:19:00 +0000544 ErrorOr<StringRef> Name = Symbol.getName();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000545 if (std::error_code EC = Name.getError())
546 report_fatal_error(EC.message());
547 fmt << *Name;
Rafael Espindola37070a52015-06-03 04:48:06 +0000548 return;
549 }
550
551 // If we couldn't find a symbol that this relocation refers to, try
552 // to find a section beginning instead.
Colin LeMahieu77804be2015-07-29 15:45:39 +0000553 for (const SectionRef &Section : ToolSectionFilter(*O)) {
Rafael Espindola37070a52015-06-03 04:48:06 +0000554 std::error_code ec;
555
556 StringRef Name;
557 uint64_t Addr = Section.getAddress();
558 if (Addr != Val)
559 continue;
560 if ((ec = Section.getName(Name)))
561 report_fatal_error(ec.message());
562 fmt << Name;
563 return;
564 }
565
566 fmt << format("0x%x", Val);
567 return;
568 }
569
570 StringRef S;
571 bool isExtern = O->getPlainRelocationExternal(RE);
572 uint64_t Val = O->getPlainRelocationSymbolNum(RE);
573
574 if (isExtern) {
575 symbol_iterator SI = O->symbol_begin();
576 advance(SI, Val);
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000577 ErrorOr<StringRef> SOrErr = SI->getName();
578 if (!error(SOrErr.getError()))
579 S = *SOrErr;
Rafael Espindola37070a52015-06-03 04:48:06 +0000580 } else {
581 section_iterator SI = O->section_begin();
582 // Adjust for the fact that sections are 1-indexed.
583 advance(SI, Val - 1);
584 SI->getName(S);
585 }
586
587 fmt << S;
588}
589
590static std::error_code getRelocationValueString(const MachOObjectFile *Obj,
591 const RelocationRef &RelRef,
592 SmallVectorImpl<char> &Result) {
593 DataRefImpl Rel = RelRef.getRawDataRefImpl();
594 MachO::any_relocation_info RE = Obj->getRelocation(Rel);
595
596 unsigned Arch = Obj->getArch();
597
598 std::string fmtbuf;
599 raw_string_ostream fmt(fmtbuf);
600 unsigned Type = Obj->getAnyRelocationType(RE);
601 bool IsPCRel = Obj->getAnyRelocationPCRel(RE);
602
603 // Determine any addends that should be displayed with the relocation.
604 // These require decoding the relocation type, which is triple-specific.
605
606 // X86_64 has entirely custom relocation types.
607 if (Arch == Triple::x86_64) {
608 bool isPCRel = Obj->getAnyRelocationPCRel(RE);
609
610 switch (Type) {
611 case MachO::X86_64_RELOC_GOT_LOAD:
612 case MachO::X86_64_RELOC_GOT: {
613 printRelocationTargetName(Obj, RE, fmt);
614 fmt << "@GOT";
615 if (isPCRel)
616 fmt << "PCREL";
617 break;
618 }
619 case MachO::X86_64_RELOC_SUBTRACTOR: {
620 DataRefImpl RelNext = Rel;
621 Obj->moveRelocationNext(RelNext);
622 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
623
624 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
625 // X86_64_RELOC_UNSIGNED.
626 // NOTE: Scattered relocations don't exist on x86_64.
627 unsigned RType = Obj->getAnyRelocationType(RENext);
628 if (RType != MachO::X86_64_RELOC_UNSIGNED)
629 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
630 "X86_64_RELOC_SUBTRACTOR.");
631
632 // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
633 // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
634 printRelocationTargetName(Obj, RENext, fmt);
635 fmt << "-";
636 printRelocationTargetName(Obj, RE, fmt);
637 break;
638 }
639 case MachO::X86_64_RELOC_TLV:
640 printRelocationTargetName(Obj, RE, fmt);
641 fmt << "@TLV";
642 if (isPCRel)
643 fmt << "P";
644 break;
645 case MachO::X86_64_RELOC_SIGNED_1:
646 printRelocationTargetName(Obj, RE, fmt);
647 fmt << "-1";
648 break;
649 case MachO::X86_64_RELOC_SIGNED_2:
650 printRelocationTargetName(Obj, RE, fmt);
651 fmt << "-2";
652 break;
653 case MachO::X86_64_RELOC_SIGNED_4:
654 printRelocationTargetName(Obj, RE, fmt);
655 fmt << "-4";
656 break;
657 default:
658 printRelocationTargetName(Obj, RE, fmt);
659 break;
660 }
661 // X86 and ARM share some relocation types in common.
662 } else if (Arch == Triple::x86 || Arch == Triple::arm ||
663 Arch == Triple::ppc) {
664 // Generic relocation types...
665 switch (Type) {
666 case MachO::GENERIC_RELOC_PAIR: // prints no info
Rui Ueyama7d099192015-06-09 15:20:42 +0000667 return std::error_code();
Rafael Espindola37070a52015-06-03 04:48:06 +0000668 case MachO::GENERIC_RELOC_SECTDIFF: {
669 DataRefImpl RelNext = Rel;
670 Obj->moveRelocationNext(RelNext);
671 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
672
673 // X86 sect diff's must be followed by a relocation of type
674 // GENERIC_RELOC_PAIR.
675 unsigned RType = Obj->getAnyRelocationType(RENext);
676
677 if (RType != MachO::GENERIC_RELOC_PAIR)
678 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
679 "GENERIC_RELOC_SECTDIFF.");
680
681 printRelocationTargetName(Obj, RE, fmt);
682 fmt << "-";
683 printRelocationTargetName(Obj, RENext, fmt);
684 break;
685 }
686 }
687
688 if (Arch == Triple::x86 || Arch == Triple::ppc) {
689 switch (Type) {
690 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
691 DataRefImpl RelNext = Rel;
692 Obj->moveRelocationNext(RelNext);
693 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
694
695 // X86 sect diff's must be followed by a relocation of type
696 // GENERIC_RELOC_PAIR.
697 unsigned RType = Obj->getAnyRelocationType(RENext);
698 if (RType != MachO::GENERIC_RELOC_PAIR)
699 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
700 "GENERIC_RELOC_LOCAL_SECTDIFF.");
701
702 printRelocationTargetName(Obj, RE, fmt);
703 fmt << "-";
704 printRelocationTargetName(Obj, RENext, fmt);
705 break;
706 }
707 case MachO::GENERIC_RELOC_TLV: {
708 printRelocationTargetName(Obj, RE, fmt);
709 fmt << "@TLV";
710 if (IsPCRel)
711 fmt << "P";
712 break;
713 }
714 default:
715 printRelocationTargetName(Obj, RE, fmt);
716 }
717 } else { // ARM-specific relocations
718 switch (Type) {
719 case MachO::ARM_RELOC_HALF:
720 case MachO::ARM_RELOC_HALF_SECTDIFF: {
721 // Half relocations steal a bit from the length field to encode
722 // whether this is an upper16 or a lower16 relocation.
723 bool isUpper = Obj->getAnyRelocationLength(RE) >> 1;
724
725 if (isUpper)
726 fmt << ":upper16:(";
727 else
728 fmt << ":lower16:(";
729 printRelocationTargetName(Obj, RE, fmt);
730
731 DataRefImpl RelNext = Rel;
732 Obj->moveRelocationNext(RelNext);
733 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
734
735 // ARM half relocs must be followed by a relocation of type
736 // ARM_RELOC_PAIR.
737 unsigned RType = Obj->getAnyRelocationType(RENext);
738 if (RType != MachO::ARM_RELOC_PAIR)
739 report_fatal_error("Expected ARM_RELOC_PAIR after "
740 "ARM_RELOC_HALF");
741
742 // NOTE: The half of the target virtual address is stashed in the
743 // address field of the secondary relocation, but we can't reverse
744 // engineer the constant offset from it without decoding the movw/movt
745 // instruction to find the other half in its immediate field.
746
747 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
748 // symbol/section pointer of the follow-on relocation.
749 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
750 fmt << "-";
751 printRelocationTargetName(Obj, RENext, fmt);
752 }
753
754 fmt << ")";
755 break;
756 }
757 default: { printRelocationTargetName(Obj, RE, fmt); }
758 }
759 }
760 } else
761 printRelocationTargetName(Obj, RE, fmt);
762
763 fmt.flush();
764 Result.append(fmtbuf.begin(), fmtbuf.end());
Rui Ueyama7d099192015-06-09 15:20:42 +0000765 return std::error_code();
Rafael Espindola37070a52015-06-03 04:48:06 +0000766}
767
768static std::error_code getRelocationValueString(const RelocationRef &Rel,
769 SmallVectorImpl<char> &Result) {
Rafael Espindola854038e2015-06-26 14:51:16 +0000770 const ObjectFile *Obj = Rel.getObject();
Rafael Espindola37070a52015-06-03 04:48:06 +0000771 if (auto *ELF = dyn_cast<ELFObjectFileBase>(Obj))
772 return getRelocationValueString(ELF, Rel, Result);
773 if (auto *COFF = dyn_cast<COFFObjectFile>(Obj))
774 return getRelocationValueString(COFF, Rel, Result);
775 auto *MachO = cast<MachOObjectFile>(Obj);
776 return getRelocationValueString(MachO, Rel, Result);
777}
778
Rafael Espindola0ad71d92015-06-30 03:41:26 +0000779/// @brief Indicates whether this relocation should hidden when listing
780/// relocations, usually because it is the trailing part of a multipart
781/// relocation that will be printed as part of the leading relocation.
782static bool getHidden(RelocationRef RelRef) {
783 const ObjectFile *Obj = RelRef.getObject();
784 auto *MachO = dyn_cast<MachOObjectFile>(Obj);
785 if (!MachO)
786 return false;
787
788 unsigned Arch = MachO->getArch();
789 DataRefImpl Rel = RelRef.getRawDataRefImpl();
790 uint64_t Type = MachO->getRelocationType(Rel);
791
792 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
793 // is always hidden.
794 if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
795 if (Type == MachO::GENERIC_RELOC_PAIR)
796 return true;
797 } else if (Arch == Triple::x86_64) {
798 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
799 // an X86_64_RELOC_SUBTRACTOR.
800 if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
801 DataRefImpl RelPrev = Rel;
802 RelPrev.d.a--;
803 uint64_t PrevType = MachO->getRelocationType(RelPrev);
804 if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
805 return true;
806 }
807 }
808
809 return false;
810}
811
Michael J. Spencer51862b32011-10-13 22:17:18 +0000812static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
Jim Grosbachaf9aec02012-08-07 17:53:14 +0000813 const Target *TheTarget = getTarget(Obj);
814 // getTarget() will have already issued a diagnostic if necessary, so
815 // just bail here if it failed.
816 if (!TheTarget)
Michael J. Spencer2670c252011-01-20 06:39:06 +0000817 return;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000818
Jack Carter551efd72012-08-28 19:24:49 +0000819 // Package up features to be passed to target/subtarget
820 std::string FeaturesStr;
821 if (MAttrs.size()) {
822 SubtargetFeatures Features;
823 for (unsigned i = 0; i != MAttrs.size(); ++i)
824 Features.AddFeature(MAttrs[i]);
825 FeaturesStr = Features.getString();
826 }
827
Ahmed Charles56440fd2014-03-06 05:51:42 +0000828 std::unique_ptr<const MCRegisterInfo> MRI(
829 TheTarget->createMCRegInfo(TripleName));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000830 if (!MRI) {
831 errs() << "error: no register info for target " << TripleName << "\n";
832 return;
833 }
834
835 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000836 std::unique_ptr<const MCAsmInfo> AsmInfo(
837 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000838 if (!AsmInfo) {
839 errs() << "error: no assembly info for target " << TripleName << "\n";
840 return;
841 }
842
Ahmed Charles56440fd2014-03-06 05:51:42 +0000843 std::unique_ptr<const MCSubtargetInfo> STI(
Kevin Enderbyc9595622014-08-06 23:24:41 +0000844 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000845 if (!STI) {
846 errs() << "error: no subtarget info for target " << TripleName << "\n";
847 return;
848 }
849
Ahmed Charles56440fd2014-03-06 05:51:42 +0000850 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000851 if (!MII) {
852 errs() << "error: no instruction info for target " << TripleName << "\n";
853 return;
854 }
855
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000856 std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo);
857 MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get());
858
859 std::unique_ptr<MCDisassembler> DisAsm(
860 TheTarget->createMCDisassembler(*STI, Ctx));
861
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000862 if (!DisAsm) {
863 errs() << "error: no disassembler for target " << TripleName << "\n";
864 return;
865 }
866
Ahmed Charles56440fd2014-03-06 05:51:42 +0000867 std::unique_ptr<const MCInstrAnalysis> MIA(
868 TheTarget->createMCInstrAnalysis(MII.get()));
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000869
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000870 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Ahmed Charles56440fd2014-03-06 05:51:42 +0000871 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Eric Christopherf8019402015-03-31 00:10:04 +0000872 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000873 if (!IP) {
874 errs() << "error: no instruction printer for target " << TripleName
875 << '\n';
876 return;
877 }
Colin LeMahieu14ec76e2015-06-07 21:07:17 +0000878 IP->setPrintImmHex(PrintImmHex);
Colin LeMahieu35436a22015-05-29 14:48:25 +0000879 PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000880
Greg Fitzgerald18432272014-03-20 22:55:15 +0000881 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ": " :
882 "\t\t\t%08" PRIx64 ": ";
883
Mark Seaborn0929d3d2014-01-25 17:38:19 +0000884 // Create a mapping, RelocSecs = SectionRelocMap[S], where sections
885 // in RelocSecs contain the relocations for section S.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000886 std::error_code EC;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000887 std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
Colin LeMahieu77804be2015-07-29 15:45:39 +0000888 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
Alexey Samsonov48803e52014-03-13 14:37:36 +0000889 section_iterator Sec2 = Section.getRelocatedSection();
Rafael Espindolab5155a52014-02-10 20:24:04 +0000890 if (Sec2 != Obj->section_end())
Alexey Samsonov48803e52014-03-13 14:37:36 +0000891 SectionRelocMap[*Sec2].push_back(Section);
Mark Seaborn0929d3d2014-01-25 17:38:19 +0000892 }
893
David Majnemer81afca62015-07-07 22:06:59 +0000894 // Create a mapping from virtual address to symbol name. This is used to
895 // pretty print the target of a call.
896 std::vector<std::pair<uint64_t, StringRef>> AllSymbols;
897 if (MIA) {
898 for (const SymbolRef &Symbol : Obj->symbols()) {
David Majnemer2603a8fa2015-07-09 18:11:40 +0000899 if (Symbol.getType() != SymbolRef::ST_Function)
900 continue;
901
David Majnemer81afca62015-07-07 22:06:59 +0000902 ErrorOr<uint64_t> AddressOrErr = Symbol.getAddress();
903 if (error(AddressOrErr.getError()))
904 break;
905 uint64_t Address = *AddressOrErr;
906
907 ErrorOr<StringRef> Name = Symbol.getName();
908 if (error(Name.getError()))
909 break;
910 if (Name->empty())
911 continue;
912 AllSymbols.push_back(std::make_pair(Address, *Name));
913 }
914
915 array_pod_sort(AllSymbols.begin(), AllSymbols.end());
916 }
917
Colin LeMahieu77804be2015-07-29 15:45:39 +0000918 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
Colin LeMahieuf34933e2015-07-23 20:58:49 +0000919 if (!DisassembleAll && (!Section.isText() || Section.isVirtual()))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000920 continue;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000921
Rafael Espindola80291272014-10-08 15:28:58 +0000922 uint64_t SectionAddr = Section.getAddress();
923 uint64_t SectSize = Section.getSize();
David Majnemer185b5b12014-11-11 09:58:25 +0000924 if (!SectSize)
925 continue;
Simon Atanasyan2b614e12014-02-24 22:12:11 +0000926
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000927 // Make a list of all the symbols in this section.
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000928 std::vector<std::pair<uint64_t, StringRef>> Symbols;
929 for (const SymbolRef &Symbol : Obj->symbols()) {
Rafael Espindola80291272014-10-08 15:28:58 +0000930 if (Section.containsSymbol(Symbol)) {
Rafael Espindolaed067c42015-07-03 18:19:00 +0000931 ErrorOr<uint64_t> AddressOrErr = Symbol.getAddress();
932 if (error(AddressOrErr.getError()))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000933 break;
Rafael Espindolaed067c42015-07-03 18:19:00 +0000934 uint64_t Address = *AddressOrErr;
Cameron Zwarich07f0f772012-02-03 04:13:37 +0000935 Address -= SectionAddr;
Simon Atanasyan2b614e12014-02-24 22:12:11 +0000936 if (Address >= SectSize)
937 continue;
Cameron Zwarich07f0f772012-02-03 04:13:37 +0000938
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000939 ErrorOr<StringRef> Name = Symbol.getName();
940 if (error(Name.getError()))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000941 break;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000942 Symbols.push_back(std::make_pair(Address, *Name));
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000943 }
944 }
945
946 // Sort the symbols by address, just in case they didn't come in that way.
947 array_pod_sort(Symbols.begin(), Symbols.end());
948
Michael J. Spencer51862b32011-10-13 22:17:18 +0000949 // Make a list of all the relocations for this section.
950 std::vector<RelocationRef> Rels;
951 if (InlineRelocs) {
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000952 for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
953 for (const RelocationRef &Reloc : RelocSec.relocations()) {
954 Rels.push_back(Reloc);
955 }
Michael J. Spencer51862b32011-10-13 22:17:18 +0000956 }
957 }
958
959 // Sort relocations by address.
960 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
961
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000962 StringRef SegmentName = "";
Mark Seaborneb03ac52014-01-25 00:32:01 +0000963 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
Alexey Samsonov48803e52014-03-13 14:37:36 +0000964 DataRefImpl DR = Section.getRawDataRefImpl();
Rafael Espindola56f976f2013-04-18 18:08:55 +0000965 SegmentName = MachO->getSectionFinalSegmentName(DR);
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000966 }
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000967 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000968 if (error(Section.getName(name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000969 break;
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000970 outs() << "Disassembly of section ";
971 if (!SegmentName.empty())
972 outs() << SegmentName << ",";
973 outs() << name << ':';
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000974
Rafael Espindola7884c952015-06-04 15:01:05 +0000975 // If the section has no symbol at the start, just insert a dummy one.
976 if (Symbols.empty() || Symbols[0].first != 0)
977 Symbols.insert(Symbols.begin(), std::make_pair(0, name));
Alp Tokere69170a2014-06-26 22:52:05 +0000978
979 SmallString<40> Comments;
980 raw_svector_ostream CommentStream(Comments);
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000981
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000982 StringRef BytesStr;
983 if (error(Section.getContents(BytesStr)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000984 break;
Aaron Ballman106fd7b2014-11-12 14:01:17 +0000985 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
986 BytesStr.size());
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000987
Michael J. Spencer2670c252011-01-20 06:39:06 +0000988 uint64_t Size;
989 uint64_t Index;
990
Michael J. Spencer51862b32011-10-13 22:17:18 +0000991 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
992 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000993 // Disassemble symbol by symbol.
994 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
Rafael Espindolae45c7402014-08-17 16:31:39 +0000995
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000996 uint64_t Start = Symbols[si].first;
Rafael Espindolae45c7402014-08-17 16:31:39 +0000997 // The end is either the section end or the beginning of the next symbol.
998 uint64_t End = (si == se - 1) ? SectSize : Symbols[si + 1].first;
999 // If this symbol has the same address as the next symbol, then skip it.
1000 if (Start == End)
Michael J. Spenceree84f642011-10-13 20:37:08 +00001001 continue;
1002
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001003 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer2670c252011-01-20 06:39:06 +00001004
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001005#ifndef NDEBUG
Mark Seaborneb03ac52014-01-25 00:32:01 +00001006 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001007#else
Mark Seaborneb03ac52014-01-25 00:32:01 +00001008 raw_ostream &DebugOut = nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001009#endif
1010
Benjamin Kramer43a772e2011-09-19 17:56:04 +00001011 for (Index = Start; Index < End; Index += Size) {
1012 MCInst Inst;
Owen Andersona0c3b972011-09-15 23:38:46 +00001013
Rafael Espindola7fc5b872014-11-12 02:04:27 +00001014 if (DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
1015 SectionAddr + Index, DebugOut,
1016 CommentStream)) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +00001017 PIP.printInst(*IP, &Inst,
Colin LeMahieufb76b002015-05-28 19:07:14 +00001018 Bytes.slice(Index, Size),
1019 SectionAddr + Index, outs(), "", *STI);
Alp Tokere69170a2014-06-26 22:52:05 +00001020 outs() << CommentStream.str();
Ahmed Bougachaad1084d2013-05-24 00:39:57 +00001021 Comments.clear();
David Majnemer2603a8fa2015-07-09 18:11:40 +00001022 if (MIA && (MIA->isCall(Inst) || MIA->isUnconditionalBranch(Inst) ||
1023 MIA->isConditionalBranch(Inst))) {
David Majnemer81afca62015-07-07 22:06:59 +00001024 uint64_t Target;
1025 if (MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target)) {
David Majnemer2603a8fa2015-07-09 18:11:40 +00001026 auto TargetSym = std::upper_bound(
1027 AllSymbols.begin(), AllSymbols.end(), Target,
Daniel Jasper0f70ee92015-07-10 07:09:20 +00001028 [](uint64_t LHS, const std::pair<uint64_t, StringRef> &RHS) {
David Majnemer2603a8fa2015-07-09 18:11:40 +00001029 return LHS < RHS.first;
1030 });
1031 if (TargetSym != AllSymbols.begin())
1032 --TargetSym;
1033 else
1034 TargetSym = AllSymbols.end();
1035
David Majnemer81afca62015-07-07 22:06:59 +00001036 if (TargetSym != AllSymbols.end()) {
1037 outs() << " <" << TargetSym->second;
David Majnemer2603a8fa2015-07-09 18:11:40 +00001038 uint64_t Disp = Target - TargetSym->first;
David Majnemer81afca62015-07-07 22:06:59 +00001039 if (Disp)
David Majnemer2603a8fa2015-07-09 18:11:40 +00001040 outs() << '+' << utohexstr(Disp);
David Majnemer81afca62015-07-07 22:06:59 +00001041 outs() << '>';
1042 }
1043 }
1044 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00001045 outs() << "\n";
1046 } else {
1047 errs() << ToolName << ": warning: invalid instruction encoding\n";
1048 if (Size == 0)
1049 Size = 1; // skip illegible bytes
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001050 }
Michael J. Spencer51862b32011-10-13 22:17:18 +00001051
1052 // Print relocation for instruction.
1053 while (rel_cur != rel_end) {
Rafael Espindola0ad71d92015-06-30 03:41:26 +00001054 bool hidden = getHidden(*rel_cur);
Rafael Espindola96d071c2015-06-29 23:29:12 +00001055 uint64_t addr = rel_cur->getOffset();
Michael J. Spencer51862b32011-10-13 22:17:18 +00001056 SmallString<16> name;
1057 SmallString<32> val;
Owen Andersonfa3e5202011-10-25 20:35:53 +00001058
1059 // If this relocation is hidden, skip it.
Owen Andersonfa3e5202011-10-25 20:35:53 +00001060 if (hidden) goto skip_print_rel;
1061
Michael J. Spencer51862b32011-10-13 22:17:18 +00001062 // Stop when rel_cur's address is past the current instruction.
Owen Andersonf20e3e52011-10-25 20:15:39 +00001063 if (addr >= Index + Size) break;
Rafael Espindola41bb4322015-06-30 04:08:37 +00001064 rel_cur->getTypeName(name);
Rafael Espindola37070a52015-06-03 04:48:06 +00001065 if (error(getRelocationValueString(*rel_cur, val)))
1066 goto skip_print_rel;
Greg Fitzgerald18432272014-03-20 22:55:15 +00001067 outs() << format(Fmt.data(), SectionAddr + addr) << name
Benjamin Kramer82803112012-03-10 02:04:38 +00001068 << "\t" << val << "\n";
Michael J. Spencer51862b32011-10-13 22:17:18 +00001069
1070 skip_print_rel:
1071 ++rel_cur;
1072 }
Benjamin Kramer87ee76c2011-07-20 19:37:35 +00001073 }
Michael J. Spencer2670c252011-01-20 06:39:06 +00001074 }
1075 }
1076}
1077
Kevin Enderby98da6132015-01-20 21:47:46 +00001078void llvm::PrintRelocations(const ObjectFile *Obj) {
Greg Fitzgerald18432272014-03-20 22:55:15 +00001079 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
1080 "%08" PRIx64;
Rafael Espindolac66d7612014-08-17 19:09:37 +00001081 // Regular objdump doesn't print relocations in non-relocatable object
1082 // files.
1083 if (!Obj->isRelocatableObject())
1084 return;
1085
Colin LeMahieu77804be2015-07-29 15:45:39 +00001086 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
Alexey Samsonov48803e52014-03-13 14:37:36 +00001087 if (Section.relocation_begin() == Section.relocation_end())
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001088 continue;
1089 StringRef secname;
Alexey Samsonov48803e52014-03-13 14:37:36 +00001090 if (error(Section.getName(secname)))
1091 continue;
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001092 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00001093 for (const RelocationRef &Reloc : Section.relocations()) {
Rafael Espindola0ad71d92015-06-30 03:41:26 +00001094 bool hidden = getHidden(Reloc);
Rafael Espindola96d071c2015-06-29 23:29:12 +00001095 uint64_t address = Reloc.getOffset();
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001096 SmallString<32> relocname;
1097 SmallString<32> valuestr;
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00001098 if (hidden)
1099 continue;
Rafael Espindola41bb4322015-06-30 04:08:37 +00001100 Reloc.getTypeName(relocname);
Rafael Espindola37070a52015-06-03 04:48:06 +00001101 if (error(getRelocationValueString(Reloc, valuestr)))
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00001102 continue;
Greg Fitzgerald18432272014-03-20 22:55:15 +00001103 outs() << format(Fmt.data(), address) << " " << relocname << " "
1104 << valuestr << "\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001105 }
1106 outs() << "\n";
1107 }
1108}
1109
Kevin Enderby98da6132015-01-20 21:47:46 +00001110void llvm::PrintSectionHeaders(const ObjectFile *Obj) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001111 outs() << "Sections:\n"
1112 "Idx Name Size Address Type\n";
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001113 unsigned i = 0;
Colin LeMahieu77804be2015-07-29 15:45:39 +00001114 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001115 StringRef Name;
Alexey Samsonov48803e52014-03-13 14:37:36 +00001116 if (error(Section.getName(Name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +00001117 return;
Rafael Espindola80291272014-10-08 15:28:58 +00001118 uint64_t Address = Section.getAddress();
1119 uint64_t Size = Section.getSize();
1120 bool Text = Section.isText();
1121 bool Data = Section.isData();
1122 bool BSS = Section.isBSS();
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001123 std::string Type = (std::string(Text ? "TEXT " : "") +
Michael J. Spencer8f67d472011-10-13 20:37:20 +00001124 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
Alexey Samsonov48803e52014-03-13 14:37:36 +00001125 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
1126 Name.str().c_str(), Size, Address, Type.c_str());
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001127 ++i;
1128 }
1129}
1130
Kevin Enderby98da6132015-01-20 21:47:46 +00001131void llvm::PrintSectionContents(const ObjectFile *Obj) {
Rafael Espindola4453e42942014-06-13 03:07:50 +00001132 std::error_code EC;
Colin LeMahieu77804be2015-07-29 15:45:39 +00001133 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001134 StringRef Name;
1135 StringRef Contents;
Alexey Samsonov48803e52014-03-13 14:37:36 +00001136 if (error(Section.getName(Name)))
1137 continue;
Rafael Espindola80291272014-10-08 15:28:58 +00001138 uint64_t BaseAddr = Section.getAddress();
David Majnemer185b5b12014-11-11 09:58:25 +00001139 uint64_t Size = Section.getSize();
1140 if (!Size)
1141 continue;
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001142
1143 outs() << "Contents of section " << Name << ":\n";
David Majnemer185b5b12014-11-11 09:58:25 +00001144 if (Section.isBSS()) {
Alexey Samsonov209095c2013-04-16 10:53:11 +00001145 outs() << format("<skipping contents of bss section at [%04" PRIx64
David Majnemer8f6b04c2014-07-14 16:20:14 +00001146 ", %04" PRIx64 ")>\n",
1147 BaseAddr, BaseAddr + Size);
Alexey Samsonov209095c2013-04-16 10:53:11 +00001148 continue;
1149 }
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001150
David Majnemer8f6b04c2014-07-14 16:20:14 +00001151 if (error(Section.getContents(Contents)))
1152 continue;
1153
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001154 // Dump out the content as hex and printable ascii characters.
1155 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
Benjamin Kramer82803112012-03-10 02:04:38 +00001156 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001157 // Dump line of hex.
1158 for (std::size_t i = 0; i < 16; ++i) {
1159 if (i != 0 && i % 4 == 0)
1160 outs() << ' ';
1161 if (addr + i < end)
1162 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
1163 << hexdigit(Contents[addr + i] & 0xF, true);
1164 else
1165 outs() << " ";
1166 }
1167 // Print ascii.
1168 outs() << " ";
1169 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001170 if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001171 outs() << Contents[addr + i];
1172 else
1173 outs() << ".";
1174 }
1175 outs() << "\n";
1176 }
1177 }
1178}
1179
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001180static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
David Majnemer44f51e52014-09-10 12:51:52 +00001181 for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) {
1182 ErrorOr<COFFSymbolRef> Symbol = coff->getSymbol(SI);
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +00001183 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +00001184 if (error(Symbol.getError()))
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +00001185 return;
1186
David Majnemer44f51e52014-09-10 12:51:52 +00001187 if (error(coff->getSymbolName(*Symbol, Name)))
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +00001188 return;
1189
1190 outs() << "[" << format("%2d", SI) << "]"
David Majnemer44f51e52014-09-10 12:51:52 +00001191 << "(sec " << format("%2d", int(Symbol->getSectionNumber())) << ")"
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +00001192 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
David Majnemer44f51e52014-09-10 12:51:52 +00001193 << "(ty " << format("%3x", unsigned(Symbol->getType())) << ")"
1194 << "(scl " << format("%3x", unsigned(Symbol->getStorageClass())) << ") "
1195 << "(nx " << unsigned(Symbol->getNumberOfAuxSymbols()) << ") "
1196 << "0x" << format("%08x", unsigned(Symbol->getValue())) << " "
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +00001197 << Name << "\n";
1198
David Majnemer44f51e52014-09-10 12:51:52 +00001199 for (unsigned AI = 0, AE = Symbol->getNumberOfAuxSymbols(); AI < AE; ++AI, ++SI) {
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +00001200 if (Symbol->isSectionDefinition()) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001201 const coff_aux_section_definition *asd;
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +00001202 if (error(coff->getAuxSymbol<coff_aux_section_definition>(SI + 1, asd)))
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001203 return;
Saleem Abdulrasool9ede5c72014-04-13 03:11:08 +00001204
David Majnemer4d571592014-09-15 19:42:42 +00001205 int32_t AuxNumber = asd->getNumber(Symbol->isBigObj());
1206
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001207 outs() << "AUX "
1208 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
1209 , unsigned(asd->Length)
1210 , unsigned(asd->NumberOfRelocations)
1211 , unsigned(asd->NumberOfLinenumbers)
1212 , unsigned(asd->CheckSum))
1213 << format("assoc %d comdat %d\n"
David Majnemer4d571592014-09-15 19:42:42 +00001214 , unsigned(AuxNumber)
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001215 , unsigned(asd->Selection));
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +00001216 } else if (Symbol->isFileRecord()) {
David Majnemer44f51e52014-09-10 12:51:52 +00001217 const char *FileName;
1218 if (error(coff->getAuxSymbol<char>(SI + 1, FileName)))
Saleem Abdulrasool63a0dd62014-04-13 22:54:11 +00001219 return;
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +00001220
David Majnemer44f51e52014-09-10 12:51:52 +00001221 StringRef Name(FileName, Symbol->getNumberOfAuxSymbols() *
1222 coff->getSymbolTableEntrySize());
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +00001223 outs() << "AUX " << Name.rtrim(StringRef("\0", 1)) << '\n';
Saleem Abdulrasool13a3f692014-04-14 16:38:25 +00001224
David Majnemer44f51e52014-09-10 12:51:52 +00001225 SI = SI + Symbol->getNumberOfAuxSymbols();
Saleem Abdulrasool13a3f692014-04-14 16:38:25 +00001226 break;
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +00001227 } else {
1228 outs() << "AUX Unknown\n";
Saleem Abdulrasool9ede5c72014-04-13 03:11:08 +00001229 }
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001230 }
1231 }
1232}
1233
Kevin Enderby98da6132015-01-20 21:47:46 +00001234void llvm::PrintSymbolTable(const ObjectFile *o) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001235 outs() << "SYMBOL TABLE:\n";
1236
Rui Ueyama4e39f712014-03-18 18:58:51 +00001237 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001238 PrintCOFFSymbolTable(coff);
Rui Ueyama4e39f712014-03-18 18:58:51 +00001239 return;
1240 }
1241 for (const SymbolRef &Symbol : o->symbols()) {
Rafael Espindolaed067c42015-07-03 18:19:00 +00001242 ErrorOr<uint64_t> AddressOrError = Symbol.getAddress();
1243 if (error(AddressOrError.getError()))
1244 continue;
1245 uint64_t Address = *AddressOrError;
Rafael Espindola2fa80cc2015-06-26 12:18:49 +00001246 SymbolRef::Type Type = Symbol.getType();
Rui Ueyama4e39f712014-03-18 18:58:51 +00001247 uint32_t Flags = Symbol.getFlags();
1248 section_iterator Section = o->section_end();
Rui Ueyama4e39f712014-03-18 18:58:51 +00001249 if (error(Symbol.getSection(Section)))
1250 continue;
Rafael Espindola75d5b542015-06-03 05:14:22 +00001251 StringRef Name;
1252 if (Type == SymbolRef::ST_Debug && Section != o->section_end()) {
1253 Section->getName(Name);
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001254 } else {
1255 ErrorOr<StringRef> NameOrErr = Symbol.getName();
1256 if (error(NameOrErr.getError()))
1257 continue;
1258 Name = *NameOrErr;
Rafael Espindola75d5b542015-06-03 05:14:22 +00001259 }
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001260
Rui Ueyama4e39f712014-03-18 18:58:51 +00001261 bool Global = Flags & SymbolRef::SF_Global;
1262 bool Weak = Flags & SymbolRef::SF_Weak;
1263 bool Absolute = Flags & SymbolRef::SF_Absolute;
Colin LeMahieubc2f47a2015-01-23 20:06:24 +00001264 bool Common = Flags & SymbolRef::SF_Common;
Davide Italianocd2514d2015-04-30 23:08:53 +00001265 bool Hidden = Flags & SymbolRef::SF_Hidden;
David Meyer1df4b842012-02-28 23:47:53 +00001266
Rui Ueyama4e39f712014-03-18 18:58:51 +00001267 char GlobLoc = ' ';
1268 if (Type != SymbolRef::ST_Unknown)
1269 GlobLoc = Global ? 'g' : 'l';
1270 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
1271 ? 'd' : ' ';
1272 char FileFunc = ' ';
1273 if (Type == SymbolRef::ST_File)
1274 FileFunc = 'f';
1275 else if (Type == SymbolRef::ST_Function)
1276 FileFunc = 'F';
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001277
Rui Ueyama4e39f712014-03-18 18:58:51 +00001278 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
1279 "%08" PRIx64;
Michael J. Spencerd857c1c2013-01-10 22:40:50 +00001280
Rui Ueyama4e39f712014-03-18 18:58:51 +00001281 outs() << format(Fmt, Address) << " "
1282 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
1283 << (Weak ? 'w' : ' ') // Weak?
1284 << ' ' // Constructor. Not supported yet.
1285 << ' ' // Warning. Not supported yet.
1286 << ' ' // Indirect reference to another symbol.
1287 << Debug // Debugging (d) or dynamic (D) symbol.
1288 << FileFunc // Name of function (F), file (f) or object (O).
1289 << ' ';
1290 if (Absolute) {
1291 outs() << "*ABS*";
Colin LeMahieubc2f47a2015-01-23 20:06:24 +00001292 } else if (Common) {
1293 outs() << "*COM*";
Rui Ueyama4e39f712014-03-18 18:58:51 +00001294 } else if (Section == o->section_end()) {
1295 outs() << "*UND*";
1296 } else {
1297 if (const MachOObjectFile *MachO =
1298 dyn_cast<const MachOObjectFile>(o)) {
1299 DataRefImpl DR = Section->getRawDataRefImpl();
1300 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
1301 outs() << SegmentName << ",";
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001302 }
Rui Ueyama4e39f712014-03-18 18:58:51 +00001303 StringRef SectionName;
1304 if (error(Section->getName(SectionName)))
1305 SectionName = "";
1306 outs() << SectionName;
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001307 }
Rafael Espindola5f7ade22015-06-23 15:45:38 +00001308
1309 outs() << '\t';
Rafael Espindolaae3ac082015-06-23 18:34:25 +00001310 if (Common || isa<ELFObjectFileBase>(o)) {
Rafael Espindoladbb6bd32015-06-25 22:10:04 +00001311 uint64_t Val =
1312 Common ? Symbol.getAlignment() : ELFSymbolRef(Symbol).getSize();
Rafael Espindolaae3ac082015-06-23 18:34:25 +00001313 outs() << format("\t %08" PRIx64 " ", Val);
1314 }
Rafael Espindola5f7ade22015-06-23 15:45:38 +00001315
Davide Italianocd2514d2015-04-30 23:08:53 +00001316 if (Hidden) {
1317 outs() << ".hidden ";
1318 }
1319 outs() << Name
Rui Ueyama4e39f712014-03-18 18:58:51 +00001320 << '\n';
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001321 }
1322}
1323
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00001324static void PrintUnwindInfo(const ObjectFile *o) {
1325 outs() << "Unwind info:\n\n";
1326
1327 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
1328 printCOFFUnwindInfo(coff);
Tim Northover4bd286a2014-08-01 13:07:19 +00001329 } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1330 printMachOUnwindInfo(MachO);
1331 else {
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00001332 // TODO: Extract DWARF dump tool to objdump.
1333 errs() << "This operation is only currently supported "
Tim Northover4bd286a2014-08-01 13:07:19 +00001334 "for COFF and MachO object files.\n";
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00001335 return;
1336 }
1337}
1338
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001339void llvm::printExportsTrie(const ObjectFile *o) {
Nick Kledzikd04bc352014-08-30 00:20:14 +00001340 outs() << "Exports trie:\n";
1341 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1342 printMachOExportsTrie(MachO);
1343 else {
1344 errs() << "This operation is only currently supported "
1345 "for Mach-O executable files.\n";
1346 return;
1347 }
1348}
1349
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001350void llvm::printRebaseTable(const ObjectFile *o) {
Nick Kledzikac431442014-09-12 21:34:15 +00001351 outs() << "Rebase table:\n";
1352 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1353 printMachORebaseTable(MachO);
1354 else {
1355 errs() << "This operation is only currently supported "
1356 "for Mach-O executable files.\n";
1357 return;
1358 }
1359}
1360
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001361void llvm::printBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +00001362 outs() << "Bind table:\n";
1363 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1364 printMachOBindTable(MachO);
1365 else {
1366 errs() << "This operation is only currently supported "
1367 "for Mach-O executable files.\n";
1368 return;
1369 }
1370}
1371
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001372void llvm::printLazyBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +00001373 outs() << "Lazy bind table:\n";
1374 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1375 printMachOLazyBindTable(MachO);
1376 else {
1377 errs() << "This operation is only currently supported "
1378 "for Mach-O executable files.\n";
1379 return;
1380 }
1381}
1382
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001383void llvm::printWeakBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +00001384 outs() << "Weak bind table:\n";
1385 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1386 printMachOWeakBindTable(MachO);
1387 else {
1388 errs() << "This operation is only currently supported "
1389 "for Mach-O executable files.\n";
1390 return;
1391 }
1392}
Nick Kledzikac431442014-09-12 21:34:15 +00001393
Adrian Prantl437105a2015-07-08 02:04:15 +00001394/// Dump the raw contents of the __clangast section so the output can be piped
1395/// into llvm-bcanalyzer.
1396void llvm::printRawClangAST(const ObjectFile *Obj) {
1397 if (outs().is_displayed()) {
1398 errs() << "The -raw-clang-ast option will dump the raw binary contents of "
1399 "the clang ast section.\n"
1400 "Please redirect the output to a file or another program such as "
1401 "llvm-bcanalyzer.\n";
1402 return;
1403 }
1404
1405 StringRef ClangASTSectionName("__clangast");
1406 if (isa<COFFObjectFile>(Obj)) {
1407 ClangASTSectionName = "clangast";
1408 }
1409
1410 Optional<object::SectionRef> ClangASTSection;
Colin LeMahieu77804be2015-07-29 15:45:39 +00001411 for (auto Sec : ToolSectionFilter(*Obj)) {
Adrian Prantl437105a2015-07-08 02:04:15 +00001412 StringRef Name;
1413 Sec.getName(Name);
1414 if (Name == ClangASTSectionName) {
1415 ClangASTSection = Sec;
1416 break;
1417 }
1418 }
1419 if (!ClangASTSection)
1420 return;
1421
1422 StringRef ClangASTContents;
1423 if (error(ClangASTSection.getValue().getContents(ClangASTContents))) {
1424 errs() << "Could not read the " << ClangASTSectionName << " section!\n";
1425 return;
1426 }
1427
1428 outs().write(ClangASTContents.data(), ClangASTContents.size());
1429}
1430
Sanjoy Das6f567a42015-06-22 18:03:02 +00001431static void printFaultMaps(const ObjectFile *Obj) {
1432 const char *FaultMapSectionName = nullptr;
1433
1434 if (isa<ELFObjectFileBase>(Obj)) {
1435 FaultMapSectionName = ".llvm_faultmaps";
1436 } else if (isa<MachOObjectFile>(Obj)) {
1437 FaultMapSectionName = "__llvm_faultmaps";
1438 } else {
1439 errs() << "This operation is only currently supported "
1440 "for ELF and Mach-O executable files.\n";
1441 return;
1442 }
1443
1444 Optional<object::SectionRef> FaultMapSection;
1445
Colin LeMahieu77804be2015-07-29 15:45:39 +00001446 for (auto Sec : ToolSectionFilter(*Obj)) {
Sanjoy Das6f567a42015-06-22 18:03:02 +00001447 StringRef Name;
1448 Sec.getName(Name);
1449 if (Name == FaultMapSectionName) {
1450 FaultMapSection = Sec;
1451 break;
1452 }
1453 }
1454
1455 outs() << "FaultMap table:\n";
1456
1457 if (!FaultMapSection.hasValue()) {
1458 outs() << "<not found>\n";
1459 return;
1460 }
1461
1462 StringRef FaultMapContents;
1463 if (error(FaultMapSection.getValue().getContents(FaultMapContents))) {
1464 errs() << "Could not read the " << FaultMapContents << " section!\n";
1465 return;
1466 }
1467
1468 FaultMapParser FMP(FaultMapContents.bytes_begin(),
1469 FaultMapContents.bytes_end());
1470
1471 outs() << FMP;
1472}
1473
Rui Ueyamac2bed422013-09-27 21:04:00 +00001474static void printPrivateFileHeader(const ObjectFile *o) {
1475 if (o->isELF()) {
1476 printELFFileHeader(o);
1477 } else if (o->isCOFF()) {
1478 printCOFFFileHeader(o);
Kevin Enderbyb76d3862014-08-22 20:35:18 +00001479 } else if (o->isMachO()) {
1480 printMachOFileHeader(o);
Rui Ueyamac2bed422013-09-27 21:04:00 +00001481 }
1482}
1483
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001484static void DumpObject(const ObjectFile *o) {
Adrian Prantl437105a2015-07-08 02:04:15 +00001485 // Avoid other output when using a raw option.
1486 if (!RawClangAST) {
1487 outs() << '\n';
1488 outs() << o->getFileName()
1489 << ":\tfile format " << o->getFileFormatName() << "\n\n";
1490 }
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001491
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001492 if (Disassemble)
Michael J. Spencer51862b32011-10-13 22:17:18 +00001493 DisassembleObject(o, Relocations);
1494 if (Relocations && !Disassemble)
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001495 PrintRelocations(o);
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001496 if (SectionHeaders)
1497 PrintSectionHeaders(o);
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001498 if (SectionContents)
1499 PrintSectionContents(o);
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001500 if (SymbolTable)
1501 PrintSymbolTable(o);
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00001502 if (UnwindInfo)
1503 PrintUnwindInfo(o);
Rui Ueyamac2bed422013-09-27 21:04:00 +00001504 if (PrivateHeaders)
1505 printPrivateFileHeader(o);
Nick Kledzikd04bc352014-08-30 00:20:14 +00001506 if (ExportsTrie)
1507 printExportsTrie(o);
Nick Kledzikac431442014-09-12 21:34:15 +00001508 if (Rebase)
1509 printRebaseTable(o);
Nick Kledzik56ebef42014-09-16 01:41:51 +00001510 if (Bind)
1511 printBindTable(o);
1512 if (LazyBind)
1513 printLazyBindTable(o);
1514 if (WeakBind)
1515 printWeakBindTable(o);
Adrian Prantl437105a2015-07-08 02:04:15 +00001516 if (RawClangAST)
1517 printRawClangAST(o);
Sanjoy Das6f567a42015-06-22 18:03:02 +00001518 if (PrintFaultMaps)
1519 printFaultMaps(o);
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001520}
1521
1522/// @brief Dump each object file in \a a;
1523static void DumpArchive(const Archive *a) {
Mark Seaborneb03ac52014-01-25 00:32:01 +00001524 for (Archive::child_iterator i = a->child_begin(), e = a->child_end(); i != e;
1525 ++i) {
Rafael Espindolaae460022014-06-16 16:08:36 +00001526 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
1527 if (std::error_code EC = ChildOrErr.getError()) {
Michael J. Spencer53723de2011-11-16 01:24:41 +00001528 // Ignore non-object files.
Mark Seaborneb03ac52014-01-25 00:32:01 +00001529 if (EC != object_error::invalid_file_type)
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +00001530 report_error(a->getFileName(), EC);
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001531 continue;
1532 }
Rafael Espindolaae460022014-06-16 16:08:36 +00001533 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001534 DumpObject(o);
1535 else
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +00001536 report_error(a->getFileName(), object_error::invalid_file_type);
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001537 }
1538}
1539
1540/// @brief Open file and figure out how to dump it.
1541static void DumpInput(StringRef file) {
1542 // If file isn't stdin, check that it exists.
1543 if (file != "-" && !sys::fs::exists(file)) {
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +00001544 report_error(file, errc::no_such_file_or_directory);
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001545 return;
1546 }
1547
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001548 // If we are using the Mach-O specific object file parser, then let it parse
1549 // the file and process the command line options. So the -arch flags can
1550 // be used to select specific slices, etc.
1551 if (MachOOpt) {
1552 ParseInputMachO(file);
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001553 return;
1554 }
1555
1556 // Attempt to open the binary.
Rafael Espindola48af1c22014-08-19 18:44:46 +00001557 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
Rafael Espindola4453e42942014-06-13 03:07:50 +00001558 if (std::error_code EC = BinaryOrErr.getError()) {
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +00001559 report_error(file, EC);
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001560 return;
1561 }
Rafael Espindola48af1c22014-08-19 18:44:46 +00001562 Binary &Binary = *BinaryOrErr.get().getBinary();
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001563
Rafael Espindola3f6481d2014-08-01 14:31:55 +00001564 if (Archive *a = dyn_cast<Archive>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001565 DumpArchive(a);
Rafael Espindola3f6481d2014-08-01 14:31:55 +00001566 else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001567 DumpObject(o);
Jim Grosbachaf9aec02012-08-07 17:53:14 +00001568 else
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +00001569 report_error(file, object_error::invalid_file_type);
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001570}
1571
Michael J. Spencer2670c252011-01-20 06:39:06 +00001572int main(int argc, char **argv) {
1573 // Print a stack trace if we signal out.
1574 sys::PrintStackTraceOnErrorSignal();
1575 PrettyStackTraceProgram X(argc, argv);
1576 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
1577
1578 // Initialize targets and assembly printers/parsers.
1579 llvm::InitializeAllTargetInfos();
Evan Cheng8c886a42011-07-22 21:58:54 +00001580 llvm::InitializeAllTargetMCs();
Michael J. Spencer2670c252011-01-20 06:39:06 +00001581 llvm::InitializeAllAsmParsers();
1582 llvm::InitializeAllDisassemblers();
1583
Pete Cooper28fb4fc2012-05-03 23:20:10 +00001584 // Register the target printer for --version.
1585 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
1586
Michael J. Spencer2670c252011-01-20 06:39:06 +00001587 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
1588 TripleName = Triple::normalize(TripleName);
1589
1590 ToolName = argv[0];
1591
1592 // Defaults to a.out if no filenames specified.
1593 if (InputFilenames.size() == 0)
1594 InputFilenames.push_back("a.out");
1595
Colin LeMahieuf34933e2015-07-23 20:58:49 +00001596 if (DisassembleAll)
1597 Disassemble = true;
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001598 if (!Disassemble
1599 && !Relocations
1600 && !SectionHeaders
1601 && !SectionContents
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00001602 && !SymbolTable
Michael J. Spencer209565db2013-01-06 03:56:49 +00001603 && !UnwindInfo
Nick Kledzikd04bc352014-08-30 00:20:14 +00001604 && !PrivateHeaders
Nick Kledzikac431442014-09-12 21:34:15 +00001605 && !ExportsTrie
Nick Kledzik56ebef42014-09-16 01:41:51 +00001606 && !Rebase
1607 && !Bind
1608 && !LazyBind
Kevin Enderby131d1772015-01-09 19:22:37 +00001609 && !WeakBind
Adrian Prantl437105a2015-07-08 02:04:15 +00001610 && !RawClangAST
Kevin Enderby13023a12015-01-15 23:19:11 +00001611 && !(UniversalHeaders && MachOOpt)
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001612 && !(ArchiveHeaders && MachOOpt)
Kevin Enderby69fe98d2015-01-23 18:52:17 +00001613 && !(IndirectSymbols && MachOOpt)
Kevin Enderby9a509442015-01-27 21:28:24 +00001614 && !(DataInCode && MachOOpt)
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001615 && !(LinkOptHints && MachOOpt)
Kevin Enderbycd66be52015-03-11 22:06:32 +00001616 && !(InfoPlist && MachOOpt)
Kevin Enderbybc847fa2015-03-16 20:08:09 +00001617 && !(DylibsUsed && MachOOpt)
1618 && !(DylibId && MachOOpt)
Kevin Enderby0fc11822015-04-01 20:57:01 +00001619 && !(ObjcMetaData && MachOOpt)
Colin LeMahieufcc32762015-07-29 19:08:10 +00001620 && !(FilterSections.size() != 0 && MachOOpt)
Sanjoy Das6f567a42015-06-22 18:03:02 +00001621 && !PrintFaultMaps) {
Michael J. Spencer2670c252011-01-20 06:39:06 +00001622 cl::PrintHelpMessage();
1623 return 2;
1624 }
1625
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001626 std::for_each(InputFilenames.begin(), InputFilenames.end(),
1627 DumpInput);
Michael J. Spencer2670c252011-01-20 06:39:06 +00001628
Rui Ueyama98fe58a2014-11-26 22:17:25 +00001629 return ReturnValue;
Michael J. Spencer2670c252011-01-20 06:39:06 +00001630}