1 //===-- llvm-objdump.cpp - Object file dumping utility for llvm -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
14 // The flags and output of this program should be near identical to those of
17 //===----------------------------------------------------------------------===//
19 #include "llvm-objdump.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/ADT/Triple.h"
24 #include "llvm/CodeGen/FaultMaps.h"
25 #include "llvm/MC/MCAsmInfo.h"
26 #include "llvm/MC/MCContext.h"
27 #include "llvm/MC/MCDisassembler.h"
28 #include "llvm/MC/MCInst.h"
29 #include "llvm/MC/MCInstPrinter.h"
30 #include "llvm/MC/MCInstrAnalysis.h"
31 #include "llvm/MC/MCInstrInfo.h"
32 #include "llvm/MC/MCObjectFileInfo.h"
33 #include "llvm/MC/MCRegisterInfo.h"
34 #include "llvm/MC/MCRelocationInfo.h"
35 #include "llvm/MC/MCSubtargetInfo.h"
36 #include "llvm/Object/Archive.h"
37 #include "llvm/Object/ELFObjectFile.h"
38 #include "llvm/Object/COFF.h"
39 #include "llvm/Object/MachO.h"
40 #include "llvm/Object/ObjectFile.h"
41 #include "llvm/Support/Casting.h"
42 #include "llvm/Support/CommandLine.h"
43 #include "llvm/Support/Debug.h"
44 #include "llvm/Support/Errc.h"
45 #include "llvm/Support/FileSystem.h"
46 #include "llvm/Support/Format.h"
47 #include "llvm/Support/GraphWriter.h"
48 #include "llvm/Support/Host.h"
49 #include "llvm/Support/ManagedStatic.h"
50 #include "llvm/Support/MemoryBuffer.h"
51 #include "llvm/Support/PrettyStackTrace.h"
52 #include "llvm/Support/Signals.h"
53 #include "llvm/Support/SourceMgr.h"
54 #include "llvm/Support/TargetRegistry.h"
55 #include "llvm/Support/TargetSelect.h"
56 #include "llvm/Support/raw_ostream.h"
60 #include <system_error>
63 using namespace object;
65 static cl::list<std::string>
66 InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
69 llvm::Disassemble("disassemble",
70 cl::desc("Display assembler mnemonics for the machine instructions"));
72 Disassembled("d", cl::desc("Alias for --disassemble"),
73 cl::aliasopt(Disassemble));
76 llvm::DisassembleAll("disassemble-all",
77 cl::desc("Display assembler mnemonics for the machine instructions"));
79 DisassembleAlld("D", cl::desc("Alias for --disassemble-all"),
80 cl::aliasopt(DisassembleAll));
83 llvm::Relocations("r", cl::desc("Display the relocation entries in the file"));
86 llvm::SectionContents("s", cl::desc("Display the content of each section"));
89 llvm::SymbolTable("t", cl::desc("Display the symbol table"));
92 llvm::ExportsTrie("exports-trie", cl::desc("Display mach-o exported symbols"));
95 llvm::Rebase("rebase", cl::desc("Display mach-o rebasing info"));
98 llvm::Bind("bind", cl::desc("Display mach-o binding info"));
101 llvm::LazyBind("lazy-bind", cl::desc("Display mach-o lazy binding info"));
104 llvm::WeakBind("weak-bind", cl::desc("Display mach-o weak binding info"));
107 llvm::RawClangAST("raw-clang-ast",
108 cl::desc("Dump the raw binary contents of the clang AST section"));
111 MachOOpt("macho", cl::desc("Use MachO specific object file parser"));
113 MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachOOpt));
116 llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
117 "see -version for available targets"));
121 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
122 cl::value_desc("cpu-name"),
126 llvm::ArchName("arch-name", cl::desc("Target arch to disassemble for, "
127 "see -version for available targets"));
130 llvm::SectionHeaders("section-headers", cl::desc("Display summaries of the "
131 "headers for each section."));
133 SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
134 cl::aliasopt(SectionHeaders));
136 SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
137 cl::aliasopt(SectionHeaders));
139 cl::list<std::string>
140 llvm::FilterSections("section", cl::desc("Operate on the specified sections only. "
141 "With -macho dump segment,section"));
143 static FilterSectionsj("j", cl::desc("Alias for --section"),
144 cl::aliasopt(llvm::FilterSections));
146 cl::list<std::string>
147 llvm::MAttrs("mattr",
149 cl::desc("Target specific attributes"),
150 cl::value_desc("a1,+a2,-a3,..."));
153 llvm::NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling "
154 "instructions, do not print "
155 "the instruction bytes."));
158 llvm::UnwindInfo("unwind-info", cl::desc("Display unwind information"));
161 UnwindInfoShort("u", cl::desc("Alias for --unwind-info"),
162 cl::aliasopt(UnwindInfo));
165 llvm::PrivateHeaders("private-headers",
166 cl::desc("Display format specific file headers"));
169 PrivateHeadersShort("p", cl::desc("Alias for --private-headers"),
170 cl::aliasopt(PrivateHeaders));
173 llvm::PrintImmHex("print-imm-hex",
174 cl::desc("Use hex format for immediate values"));
176 cl::opt<bool> PrintFaultMaps("fault-map-section",
177 cl::desc("Display contents of faultmap section"));
179 static StringRef ToolName;
182 typedef std::function<bool(llvm::object::SectionRef const &)> FilterPredicate;
184 class SectionFilterIterator {
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) {
192 llvm::object::SectionRef operator*() const { return *Iterator; }
193 SectionFilterIterator &operator++() {
198 bool operator!=(SectionFilterIterator const &Other) const {
199 return Iterator != Other.Iterator;
203 void ScanPredicate() {
204 while (Iterator != End && !Predicate(*Iterator)) {
208 FilterPredicate Predicate;
209 llvm::object::section_iterator Iterator;
210 llvm::object::section_iterator End;
213 class SectionFilter {
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());
221 SectionFilterIterator end() {
222 return SectionFilterIterator(Predicate, Object.section_end(),
223 Object.section_end());
227 FilterPredicate Predicate;
228 llvm::object::ObjectFile const &Object;
230 SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O) {
231 return SectionFilter([](llvm::object::SectionRef const &S) {
232 if(FilterSections.empty())
234 llvm::StringRef String;
235 std::error_code error = S.getName(String);
238 return std::find(FilterSections.begin(),
239 FilterSections.end(),
240 String) != FilterSections.end();
246 void llvm::error(std::error_code EC) {
250 outs() << ToolName << ": error reading file: " << EC.message() << ".\n";
254 static void report_error(StringRef File, std::error_code EC) {
256 errs() << ToolName << ": '" << File << "': " << EC.message() << ".\n";
260 static const Target *getTarget(const ObjectFile *Obj = nullptr) {
261 // Figure out the target triple.
262 llvm::Triple TheTriple("unknown-unknown-unknown");
263 if (TripleName.empty()) {
265 TheTriple.setArch(Triple::ArchType(Obj->getArch()));
266 // TheTriple defaults to ELF, and COFF doesn't have an environment:
267 // the best we can do here is indicate that it is mach-o.
269 TheTriple.setObjectFormat(Triple::MachO);
272 const auto COFFObj = dyn_cast<COFFObjectFile>(Obj);
273 if (COFFObj->getArch() == Triple::thumb)
274 TheTriple.setTriple("thumbv7-windows");
278 TheTriple.setTriple(Triple::normalize(TripleName));
280 // Get the target specific parser.
282 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
285 errs() << ToolName << ": " << Error;
289 // Update the triple name and return the found target.
290 TripleName = TheTriple.getTriple();
294 bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
295 return a.getOffset() < b.getOffset();
299 class PrettyPrinter {
301 virtual ~PrettyPrinter(){}
302 virtual void printInst(MCInstPrinter &IP, const MCInst *MI,
303 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) {
309 dumpBytes(Bytes, outs());
311 IP.printInst(MI, outs(), "", STI);
314 PrettyPrinter PrettyPrinterInst;
315 class HexagonPrettyPrinter : public PrettyPrinter {
317 void printLead(ArrayRef<uint8_t> Bytes, uint64_t Address,
320 (Bytes[3] << 24) | (Bytes[2] << 16) | (Bytes[1] << 8) | Bytes[0];
321 OS << format("%8" PRIx64 ":", Address);
322 if (!NoShowRawInsn) {
324 dumpBytes(Bytes.slice(0, 4), OS);
325 OS << format("%08" PRIx32, opcode);
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 {
334 raw_string_ostream TempStream(Buffer);
335 IP.printInst(MI, TempStream, "", STI);
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 = " { ";
344 while(!HeadTail.first.empty()) {
347 printLead(Bytes, Address, OS);
351 auto Duplex = HeadTail.first.split('\v');
352 if(!Duplex.second.empty()){
355 Inst = Duplex.second;
358 Inst = HeadTail.first;
360 Bytes = Bytes.slice(4);
362 HeadTail = HeadTail.second.split('\n');
364 OS << " } " << PacketBundle.second;
367 HexagonPrettyPrinter HexagonPrettyPrinterInst;
368 PrettyPrinter &selectPrettyPrinter(Triple const &Triple) {
369 switch(Triple.getArch()) {
371 return PrettyPrinterInst;
372 case Triple::hexagon:
373 return HexagonPrettyPrinterInst;
378 template <class ELFT>
379 static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
381 SmallVectorImpl<char> &Result) {
382 typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
383 typedef typename ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr;
384 typedef typename ELFObjectFile<ELFT>::Elf_Rel Elf_Rel;
385 typedef typename ELFObjectFile<ELFT>::Elf_Rela Elf_Rela;
387 const ELFFile<ELFT> &EF = *Obj->getELFFile();
389 ErrorOr<const Elf_Shdr *> SecOrErr = EF.getSection(Rel.d.a);
390 if (std::error_code EC = SecOrErr.getError())
392 const Elf_Shdr *Sec = *SecOrErr;
393 ErrorOr<const Elf_Shdr *> SymTabOrErr = EF.getSection(Sec->sh_link);
394 if (std::error_code EC = SymTabOrErr.getError())
396 const Elf_Shdr *SymTab = *SymTabOrErr;
397 assert(SymTab->sh_type == ELF::SHT_SYMTAB ||
398 SymTab->sh_type == ELF::SHT_DYNSYM);
399 ErrorOr<const Elf_Shdr *> StrTabSec = EF.getSection(SymTab->sh_link);
400 if (std::error_code EC = StrTabSec.getError())
402 ErrorOr<StringRef> StrTabOrErr = EF.getStringTable(*StrTabSec);
403 if (std::error_code EC = StrTabOrErr.getError())
405 StringRef StrTab = *StrTabOrErr;
409 uint16_t symbol_index = 0;
410 switch (Sec->sh_type) {
412 return object_error::parse_failed;
414 const Elf_Rel *ERel = Obj->getRel(Rel);
415 type = ERel->getType(EF.isMips64EL());
416 symbol_index = ERel->getSymbol(EF.isMips64EL());
417 // TODO: Read implicit addend from section data.
420 case ELF::SHT_RELA: {
421 const Elf_Rela *ERela = Obj->getRela(Rel);
422 type = ERela->getType(EF.isMips64EL());
423 symbol_index = ERela->getSymbol(EF.isMips64EL());
424 addend = ERela->r_addend;
428 const Elf_Sym *symb =
429 EF.template getEntry<Elf_Sym>(Sec->sh_link, symbol_index);
431 ErrorOr<const Elf_Shdr *> SymSec = EF.getSection(symb);
432 if (std::error_code EC = SymSec.getError())
434 if (symb->getType() == ELF::STT_SECTION) {
435 ErrorOr<StringRef> SecName = EF.getSectionName(*SymSec);
436 if (std::error_code EC = SecName.getError())
440 ErrorOr<StringRef> SymName = symb->getName(StrTab);
442 return SymName.getError();
445 switch (EF.getHeader()->e_machine) {
448 case ELF::R_X86_64_PC8:
449 case ELF::R_X86_64_PC16:
450 case ELF::R_X86_64_PC32: {
452 raw_string_ostream fmt(fmtbuf);
453 fmt << Target << (addend < 0 ? "" : "+") << addend << "-P";
455 Result.append(fmtbuf.begin(), fmtbuf.end());
457 case ELF::R_X86_64_8:
458 case ELF::R_X86_64_16:
459 case ELF::R_X86_64_32:
460 case ELF::R_X86_64_32S:
461 case ELF::R_X86_64_64: {
463 raw_string_ostream fmt(fmtbuf);
464 fmt << Target << (addend < 0 ? "" : "+") << addend;
466 Result.append(fmtbuf.begin(), fmtbuf.end());
472 case ELF::EM_AARCH64: {
474 raw_string_ostream fmt(fmtbuf);
477 fmt << (addend < 0 ? "" : "+") << addend;
479 Result.append(fmtbuf.begin(), fmtbuf.end());
484 case ELF::EM_HEXAGON:
492 Result.append(res.begin(), res.end());
493 return std::error_code();
496 static std::error_code getRelocationValueString(const ELFObjectFileBase *Obj,
497 const RelocationRef &RelRef,
498 SmallVectorImpl<char> &Result) {
499 DataRefImpl Rel = RelRef.getRawDataRefImpl();
500 if (auto *ELF32LE = dyn_cast<ELF32LEObjectFile>(Obj))
501 return getRelocationValueString(ELF32LE, Rel, Result);
502 if (auto *ELF64LE = dyn_cast<ELF64LEObjectFile>(Obj))
503 return getRelocationValueString(ELF64LE, Rel, Result);
504 if (auto *ELF32BE = dyn_cast<ELF32BEObjectFile>(Obj))
505 return getRelocationValueString(ELF32BE, Rel, Result);
506 auto *ELF64BE = cast<ELF64BEObjectFile>(Obj);
507 return getRelocationValueString(ELF64BE, Rel, Result);
510 static std::error_code getRelocationValueString(const COFFObjectFile *Obj,
511 const RelocationRef &Rel,
512 SmallVectorImpl<char> &Result) {
513 symbol_iterator SymI = Rel.getSymbol();
514 ErrorOr<StringRef> SymNameOrErr = SymI->getName();
515 if (std::error_code EC = SymNameOrErr.getError())
517 StringRef SymName = *SymNameOrErr;
518 Result.append(SymName.begin(), SymName.end());
519 return std::error_code();
522 static void printRelocationTargetName(const MachOObjectFile *O,
523 const MachO::any_relocation_info &RE,
524 raw_string_ostream &fmt) {
525 bool IsScattered = O->isRelocationScattered(RE);
527 // Target of a scattered relocation is an address. In the interest of
528 // generating pretty output, scan through the symbol table looking for a
529 // symbol that aligns with that address. If we find one, print it.
530 // Otherwise, we just print the hex address of the target.
532 uint32_t Val = O->getPlainRelocationSymbolNum(RE);
534 for (const SymbolRef &Symbol : O->symbols()) {
536 ErrorOr<uint64_t> Addr = Symbol.getAddress();
537 if ((ec = Addr.getError()))
538 report_fatal_error(ec.message());
541 ErrorOr<StringRef> Name = Symbol.getName();
542 if (std::error_code EC = Name.getError())
543 report_fatal_error(EC.message());
548 // If we couldn't find a symbol that this relocation refers to, try
549 // to find a section beginning instead.
550 for (const SectionRef &Section : ToolSectionFilter(*O)) {
554 uint64_t Addr = Section.getAddress();
557 if ((ec = Section.getName(Name)))
558 report_fatal_error(ec.message());
563 fmt << format("0x%x", Val);
568 bool isExtern = O->getPlainRelocationExternal(RE);
569 uint64_t Val = O->getPlainRelocationSymbolNum(RE);
572 symbol_iterator SI = O->symbol_begin();
574 ErrorOr<StringRef> SOrErr = SI->getName();
575 error(SOrErr.getError());
578 section_iterator SI = O->section_begin();
579 // Adjust for the fact that sections are 1-indexed.
580 advance(SI, Val - 1);
587 static std::error_code getRelocationValueString(const MachOObjectFile *Obj,
588 const RelocationRef &RelRef,
589 SmallVectorImpl<char> &Result) {
590 DataRefImpl Rel = RelRef.getRawDataRefImpl();
591 MachO::any_relocation_info RE = Obj->getRelocation(Rel);
593 unsigned Arch = Obj->getArch();
596 raw_string_ostream fmt(fmtbuf);
597 unsigned Type = Obj->getAnyRelocationType(RE);
598 bool IsPCRel = Obj->getAnyRelocationPCRel(RE);
600 // Determine any addends that should be displayed with the relocation.
601 // These require decoding the relocation type, which is triple-specific.
603 // X86_64 has entirely custom relocation types.
604 if (Arch == Triple::x86_64) {
605 bool isPCRel = Obj->getAnyRelocationPCRel(RE);
608 case MachO::X86_64_RELOC_GOT_LOAD:
609 case MachO::X86_64_RELOC_GOT: {
610 printRelocationTargetName(Obj, RE, fmt);
616 case MachO::X86_64_RELOC_SUBTRACTOR: {
617 DataRefImpl RelNext = Rel;
618 Obj->moveRelocationNext(RelNext);
619 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
621 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
622 // X86_64_RELOC_UNSIGNED.
623 // NOTE: Scattered relocations don't exist on x86_64.
624 unsigned RType = Obj->getAnyRelocationType(RENext);
625 if (RType != MachO::X86_64_RELOC_UNSIGNED)
626 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
627 "X86_64_RELOC_SUBTRACTOR.");
629 // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
630 // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
631 printRelocationTargetName(Obj, RENext, fmt);
633 printRelocationTargetName(Obj, RE, fmt);
636 case MachO::X86_64_RELOC_TLV:
637 printRelocationTargetName(Obj, RE, fmt);
642 case MachO::X86_64_RELOC_SIGNED_1:
643 printRelocationTargetName(Obj, RE, fmt);
646 case MachO::X86_64_RELOC_SIGNED_2:
647 printRelocationTargetName(Obj, RE, fmt);
650 case MachO::X86_64_RELOC_SIGNED_4:
651 printRelocationTargetName(Obj, RE, fmt);
655 printRelocationTargetName(Obj, RE, fmt);
658 // X86 and ARM share some relocation types in common.
659 } else if (Arch == Triple::x86 || Arch == Triple::arm ||
660 Arch == Triple::ppc) {
661 // Generic relocation types...
663 case MachO::GENERIC_RELOC_PAIR: // prints no info
664 return std::error_code();
665 case MachO::GENERIC_RELOC_SECTDIFF: {
666 DataRefImpl RelNext = Rel;
667 Obj->moveRelocationNext(RelNext);
668 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
670 // X86 sect diff's must be followed by a relocation of type
671 // GENERIC_RELOC_PAIR.
672 unsigned RType = Obj->getAnyRelocationType(RENext);
674 if (RType != MachO::GENERIC_RELOC_PAIR)
675 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
676 "GENERIC_RELOC_SECTDIFF.");
678 printRelocationTargetName(Obj, RE, fmt);
680 printRelocationTargetName(Obj, RENext, fmt);
685 if (Arch == Triple::x86 || Arch == Triple::ppc) {
687 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
688 DataRefImpl RelNext = Rel;
689 Obj->moveRelocationNext(RelNext);
690 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
692 // X86 sect diff's must be followed by a relocation of type
693 // GENERIC_RELOC_PAIR.
694 unsigned RType = Obj->getAnyRelocationType(RENext);
695 if (RType != MachO::GENERIC_RELOC_PAIR)
696 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
697 "GENERIC_RELOC_LOCAL_SECTDIFF.");
699 printRelocationTargetName(Obj, RE, fmt);
701 printRelocationTargetName(Obj, RENext, fmt);
704 case MachO::GENERIC_RELOC_TLV: {
705 printRelocationTargetName(Obj, RE, fmt);
712 printRelocationTargetName(Obj, RE, fmt);
714 } else { // ARM-specific relocations
716 case MachO::ARM_RELOC_HALF:
717 case MachO::ARM_RELOC_HALF_SECTDIFF: {
718 // Half relocations steal a bit from the length field to encode
719 // whether this is an upper16 or a lower16 relocation.
720 bool isUpper = Obj->getAnyRelocationLength(RE) >> 1;
726 printRelocationTargetName(Obj, RE, fmt);
728 DataRefImpl RelNext = Rel;
729 Obj->moveRelocationNext(RelNext);
730 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
732 // ARM half relocs must be followed by a relocation of type
734 unsigned RType = Obj->getAnyRelocationType(RENext);
735 if (RType != MachO::ARM_RELOC_PAIR)
736 report_fatal_error("Expected ARM_RELOC_PAIR after "
739 // NOTE: The half of the target virtual address is stashed in the
740 // address field of the secondary relocation, but we can't reverse
741 // engineer the constant offset from it without decoding the movw/movt
742 // instruction to find the other half in its immediate field.
744 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
745 // symbol/section pointer of the follow-on relocation.
746 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
748 printRelocationTargetName(Obj, RENext, fmt);
754 default: { printRelocationTargetName(Obj, RE, fmt); }
758 printRelocationTargetName(Obj, RE, fmt);
761 Result.append(fmtbuf.begin(), fmtbuf.end());
762 return std::error_code();
765 static std::error_code getRelocationValueString(const RelocationRef &Rel,
766 SmallVectorImpl<char> &Result) {
767 const ObjectFile *Obj = Rel.getObject();
768 if (auto *ELF = dyn_cast<ELFObjectFileBase>(Obj))
769 return getRelocationValueString(ELF, Rel, Result);
770 if (auto *COFF = dyn_cast<COFFObjectFile>(Obj))
771 return getRelocationValueString(COFF, Rel, Result);
772 auto *MachO = cast<MachOObjectFile>(Obj);
773 return getRelocationValueString(MachO, Rel, Result);
776 /// @brief Indicates whether this relocation should hidden when listing
777 /// relocations, usually because it is the trailing part of a multipart
778 /// relocation that will be printed as part of the leading relocation.
779 static bool getHidden(RelocationRef RelRef) {
780 const ObjectFile *Obj = RelRef.getObject();
781 auto *MachO = dyn_cast<MachOObjectFile>(Obj);
785 unsigned Arch = MachO->getArch();
786 DataRefImpl Rel = RelRef.getRawDataRefImpl();
787 uint64_t Type = MachO->getRelocationType(Rel);
789 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
791 if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
792 if (Type == MachO::GENERIC_RELOC_PAIR)
794 } else if (Arch == Triple::x86_64) {
795 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
796 // an X86_64_RELOC_SUBTRACTOR.
797 if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
798 DataRefImpl RelPrev = Rel;
800 uint64_t PrevType = MachO->getRelocationType(RelPrev);
801 if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
809 static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
810 const Target *TheTarget = getTarget(Obj);
811 // getTarget() will have already issued a diagnostic if necessary, so
812 // just bail here if it failed.
816 // Package up features to be passed to target/subtarget
817 std::string FeaturesStr;
819 SubtargetFeatures Features;
820 for (unsigned i = 0; i != MAttrs.size(); ++i)
821 Features.AddFeature(MAttrs[i]);
822 FeaturesStr = Features.getString();
825 std::unique_ptr<const MCRegisterInfo> MRI(
826 TheTarget->createMCRegInfo(TripleName));
828 errs() << "error: no register info for target " << TripleName << "\n";
832 // Set up disassembler.
833 std::unique_ptr<const MCAsmInfo> AsmInfo(
834 TheTarget->createMCAsmInfo(*MRI, TripleName));
836 errs() << "error: no assembly info for target " << TripleName << "\n";
840 std::unique_ptr<const MCSubtargetInfo> STI(
841 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
843 errs() << "error: no subtarget info for target " << TripleName << "\n";
847 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
849 errs() << "error: no instruction info for target " << TripleName << "\n";
853 std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo);
854 MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get());
856 std::unique_ptr<MCDisassembler> DisAsm(
857 TheTarget->createMCDisassembler(*STI, Ctx));
860 errs() << "error: no disassembler for target " << TripleName << "\n";
864 std::unique_ptr<const MCInstrAnalysis> MIA(
865 TheTarget->createMCInstrAnalysis(MII.get()));
867 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
868 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
869 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI));
871 errs() << "error: no instruction printer for target " << TripleName
875 IP->setPrintImmHex(PrintImmHex);
876 PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName));
878 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ": " :
879 "\t\t\t%08" PRIx64 ": ";
881 // Create a mapping, RelocSecs = SectionRelocMap[S], where sections
882 // in RelocSecs contain the relocations for section S.
884 std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
885 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
886 section_iterator Sec2 = Section.getRelocatedSection();
887 if (Sec2 != Obj->section_end())
888 SectionRelocMap[*Sec2].push_back(Section);
891 // Create a mapping from virtual address to symbol name. This is used to
892 // pretty print the target of a call.
893 std::vector<std::pair<uint64_t, StringRef>> AllSymbols;
895 for (const SymbolRef &Symbol : Obj->symbols()) {
896 if (Symbol.getType() != SymbolRef::ST_Function)
899 ErrorOr<uint64_t> AddressOrErr = Symbol.getAddress();
900 error(AddressOrErr.getError());
901 uint64_t Address = *AddressOrErr;
903 ErrorOr<StringRef> Name = Symbol.getName();
904 error(Name.getError());
907 AllSymbols.push_back(std::make_pair(Address, *Name));
910 array_pod_sort(AllSymbols.begin(), AllSymbols.end());
913 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
914 if (!DisassembleAll && (!Section.isText() || Section.isVirtual()))
917 uint64_t SectionAddr = Section.getAddress();
918 uint64_t SectSize = Section.getSize();
922 // Make a list of all the symbols in this section.
923 std::vector<std::pair<uint64_t, StringRef>> Symbols;
924 for (const SymbolRef &Symbol : Obj->symbols()) {
925 if (Section.containsSymbol(Symbol)) {
926 ErrorOr<uint64_t> AddressOrErr = Symbol.getAddress();
927 error(AddressOrErr.getError());
928 uint64_t Address = *AddressOrErr;
929 Address -= SectionAddr;
930 if (Address >= SectSize)
933 ErrorOr<StringRef> Name = Symbol.getName();
934 error(Name.getError());
935 Symbols.push_back(std::make_pair(Address, *Name));
939 // Sort the symbols by address, just in case they didn't come in that way.
940 array_pod_sort(Symbols.begin(), Symbols.end());
942 // Make a list of all the relocations for this section.
943 std::vector<RelocationRef> Rels;
945 for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
946 for (const RelocationRef &Reloc : RelocSec.relocations()) {
947 Rels.push_back(Reloc);
952 // Sort relocations by address.
953 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
955 StringRef SegmentName = "";
956 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
957 DataRefImpl DR = Section.getRawDataRefImpl();
958 SegmentName = MachO->getSectionFinalSegmentName(DR);
961 error(Section.getName(name));
962 outs() << "Disassembly of section ";
963 if (!SegmentName.empty())
964 outs() << SegmentName << ",";
965 outs() << name << ':';
967 // If the section has no symbol at the start, just insert a dummy one.
968 if (Symbols.empty() || Symbols[0].first != 0)
969 Symbols.insert(Symbols.begin(), std::make_pair(0, name));
971 SmallString<40> Comments;
972 raw_svector_ostream CommentStream(Comments);
975 error(Section.getContents(BytesStr));
976 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
982 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
983 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
984 // Disassemble symbol by symbol.
985 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
987 uint64_t Start = Symbols[si].first;
988 // The end is either the section end or the beginning of the next symbol.
989 uint64_t End = (si == se - 1) ? SectSize : Symbols[si + 1].first;
990 // If this symbol has the same address as the next symbol, then skip it.
994 outs() << '\n' << Symbols[si].second << ":\n";
997 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
999 raw_ostream &DebugOut = nulls();
1002 for (Index = Start; Index < End; Index += Size) {
1005 if (DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
1006 SectionAddr + Index, DebugOut,
1008 PIP.printInst(*IP, &Inst,
1009 Bytes.slice(Index, Size),
1010 SectionAddr + Index, outs(), "", *STI);
1011 outs() << CommentStream.str();
1013 if (MIA && (MIA->isCall(Inst) || MIA->isUnconditionalBranch(Inst) ||
1014 MIA->isConditionalBranch(Inst))) {
1016 if (MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target)) {
1017 auto TargetSym = std::upper_bound(
1018 AllSymbols.begin(), AllSymbols.end(), Target,
1019 [](uint64_t LHS, const std::pair<uint64_t, StringRef> &RHS) {
1020 return LHS < RHS.first;
1022 if (TargetSym != AllSymbols.begin())
1025 TargetSym = AllSymbols.end();
1027 if (TargetSym != AllSymbols.end()) {
1028 outs() << " <" << TargetSym->second;
1029 uint64_t Disp = Target - TargetSym->first;
1031 outs() << '+' << utohexstr(Disp);
1038 errs() << ToolName << ": warning: invalid instruction encoding\n";
1040 Size = 1; // skip illegible bytes
1043 // Print relocation for instruction.
1044 while (rel_cur != rel_end) {
1045 bool hidden = getHidden(*rel_cur);
1046 uint64_t addr = rel_cur->getOffset();
1047 SmallString<16> name;
1048 SmallString<32> val;
1050 // If this relocation is hidden, skip it.
1051 if (hidden) goto skip_print_rel;
1053 // Stop when rel_cur's address is past the current instruction.
1054 if (addr >= Index + Size) break;
1055 rel_cur->getTypeName(name);
1056 error(getRelocationValueString(*rel_cur, val));
1057 outs() << format(Fmt.data(), SectionAddr + addr) << name
1058 << "\t" << val << "\n";
1068 void llvm::PrintRelocations(const ObjectFile *Obj) {
1069 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
1071 // Regular objdump doesn't print relocations in non-relocatable object
1073 if (!Obj->isRelocatableObject())
1076 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
1077 if (Section.relocation_begin() == Section.relocation_end())
1080 error(Section.getName(secname));
1081 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
1082 for (const RelocationRef &Reloc : Section.relocations()) {
1083 bool hidden = getHidden(Reloc);
1084 uint64_t address = Reloc.getOffset();
1085 SmallString<32> relocname;
1086 SmallString<32> valuestr;
1089 Reloc.getTypeName(relocname);
1090 error(getRelocationValueString(Reloc, valuestr));
1091 outs() << format(Fmt.data(), address) << " " << relocname << " "
1092 << valuestr << "\n";
1098 void llvm::PrintSectionHeaders(const ObjectFile *Obj) {
1099 outs() << "Sections:\n"
1100 "Idx Name Size Address Type\n";
1102 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
1104 error(Section.getName(Name));
1105 uint64_t Address = Section.getAddress();
1106 uint64_t Size = Section.getSize();
1107 bool Text = Section.isText();
1108 bool Data = Section.isData();
1109 bool BSS = Section.isBSS();
1110 std::string Type = (std::string(Text ? "TEXT " : "") +
1111 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
1112 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
1113 Name.str().c_str(), Size, Address, Type.c_str());
1118 void llvm::PrintSectionContents(const ObjectFile *Obj) {
1120 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
1123 error(Section.getName(Name));
1124 uint64_t BaseAddr = Section.getAddress();
1125 uint64_t Size = Section.getSize();
1129 outs() << "Contents of section " << Name << ":\n";
1130 if (Section.isBSS()) {
1131 outs() << format("<skipping contents of bss section at [%04" PRIx64
1132 ", %04" PRIx64 ")>\n",
1133 BaseAddr, BaseAddr + Size);
1137 error(Section.getContents(Contents));
1139 // Dump out the content as hex and printable ascii characters.
1140 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
1141 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
1142 // Dump line of hex.
1143 for (std::size_t i = 0; i < 16; ++i) {
1144 if (i != 0 && i % 4 == 0)
1147 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
1148 << hexdigit(Contents[addr + i] & 0xF, true);
1154 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
1155 if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
1156 outs() << Contents[addr + i];
1165 static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
1166 for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) {
1167 ErrorOr<COFFSymbolRef> Symbol = coff->getSymbol(SI);
1169 error(Symbol.getError());
1170 error(coff->getSymbolName(*Symbol, Name));
1172 outs() << "[" << format("%2d", SI) << "]"
1173 << "(sec " << format("%2d", int(Symbol->getSectionNumber())) << ")"
1174 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
1175 << "(ty " << format("%3x", unsigned(Symbol->getType())) << ")"
1176 << "(scl " << format("%3x", unsigned(Symbol->getStorageClass())) << ") "
1177 << "(nx " << unsigned(Symbol->getNumberOfAuxSymbols()) << ") "
1178 << "0x" << format("%08x", unsigned(Symbol->getValue())) << " "
1181 for (unsigned AI = 0, AE = Symbol->getNumberOfAuxSymbols(); AI < AE; ++AI, ++SI) {
1182 if (Symbol->isSectionDefinition()) {
1183 const coff_aux_section_definition *asd;
1184 error(coff->getAuxSymbol<coff_aux_section_definition>(SI + 1, asd));
1186 int32_t AuxNumber = asd->getNumber(Symbol->isBigObj());
1189 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
1190 , unsigned(asd->Length)
1191 , unsigned(asd->NumberOfRelocations)
1192 , unsigned(asd->NumberOfLinenumbers)
1193 , unsigned(asd->CheckSum))
1194 << format("assoc %d comdat %d\n"
1195 , unsigned(AuxNumber)
1196 , unsigned(asd->Selection));
1197 } else if (Symbol->isFileRecord()) {
1198 const char *FileName;
1199 error(coff->getAuxSymbol<char>(SI + 1, FileName));
1201 StringRef Name(FileName, Symbol->getNumberOfAuxSymbols() *
1202 coff->getSymbolTableEntrySize());
1203 outs() << "AUX " << Name.rtrim(StringRef("\0", 1)) << '\n';
1205 SI = SI + Symbol->getNumberOfAuxSymbols();
1208 outs() << "AUX Unknown\n";
1214 void llvm::PrintSymbolTable(const ObjectFile *o) {
1215 outs() << "SYMBOL TABLE:\n";
1217 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
1218 PrintCOFFSymbolTable(coff);
1221 for (const SymbolRef &Symbol : o->symbols()) {
1222 ErrorOr<uint64_t> AddressOrError = Symbol.getAddress();
1223 error(AddressOrError.getError());
1224 uint64_t Address = *AddressOrError;
1225 SymbolRef::Type Type = Symbol.getType();
1226 uint32_t Flags = Symbol.getFlags();
1227 section_iterator Section = o->section_end();
1228 error(Symbol.getSection(Section));
1230 if (Type == SymbolRef::ST_Debug && Section != o->section_end()) {
1231 Section->getName(Name);
1233 ErrorOr<StringRef> NameOrErr = Symbol.getName();
1234 error(NameOrErr.getError());
1238 bool Global = Flags & SymbolRef::SF_Global;
1239 bool Weak = Flags & SymbolRef::SF_Weak;
1240 bool Absolute = Flags & SymbolRef::SF_Absolute;
1241 bool Common = Flags & SymbolRef::SF_Common;
1242 bool Hidden = Flags & SymbolRef::SF_Hidden;
1245 if (Type != SymbolRef::ST_Unknown)
1246 GlobLoc = Global ? 'g' : 'l';
1247 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
1249 char FileFunc = ' ';
1250 if (Type == SymbolRef::ST_File)
1252 else if (Type == SymbolRef::ST_Function)
1255 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
1258 outs() << format(Fmt, Address) << " "
1259 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
1260 << (Weak ? 'w' : ' ') // Weak?
1261 << ' ' // Constructor. Not supported yet.
1262 << ' ' // Warning. Not supported yet.
1263 << ' ' // Indirect reference to another symbol.
1264 << Debug // Debugging (d) or dynamic (D) symbol.
1265 << FileFunc // Name of function (F), file (f) or object (O).
1269 } else if (Common) {
1271 } else if (Section == o->section_end()) {
1274 if (const MachOObjectFile *MachO =
1275 dyn_cast<const MachOObjectFile>(o)) {
1276 DataRefImpl DR = Section->getRawDataRefImpl();
1277 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
1278 outs() << SegmentName << ",";
1280 StringRef SectionName;
1281 error(Section->getName(SectionName));
1282 outs() << SectionName;
1286 if (Common || isa<ELFObjectFileBase>(o)) {
1288 Common ? Symbol.getAlignment() : ELFSymbolRef(Symbol).getSize();
1289 outs() << format("\t %08" PRIx64 " ", Val);
1293 outs() << ".hidden ";
1300 static void PrintUnwindInfo(const ObjectFile *o) {
1301 outs() << "Unwind info:\n\n";
1303 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
1304 printCOFFUnwindInfo(coff);
1305 } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1306 printMachOUnwindInfo(MachO);
1308 // TODO: Extract DWARF dump tool to objdump.
1309 errs() << "This operation is only currently supported "
1310 "for COFF and MachO object files.\n";
1315 void llvm::printExportsTrie(const ObjectFile *o) {
1316 outs() << "Exports trie:\n";
1317 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1318 printMachOExportsTrie(MachO);
1320 errs() << "This operation is only currently supported "
1321 "for Mach-O executable files.\n";
1326 void llvm::printRebaseTable(const ObjectFile *o) {
1327 outs() << "Rebase table:\n";
1328 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1329 printMachORebaseTable(MachO);
1331 errs() << "This operation is only currently supported "
1332 "for Mach-O executable files.\n";
1337 void llvm::printBindTable(const ObjectFile *o) {
1338 outs() << "Bind table:\n";
1339 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1340 printMachOBindTable(MachO);
1342 errs() << "This operation is only currently supported "
1343 "for Mach-O executable files.\n";
1348 void llvm::printLazyBindTable(const ObjectFile *o) {
1349 outs() << "Lazy bind table:\n";
1350 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1351 printMachOLazyBindTable(MachO);
1353 errs() << "This operation is only currently supported "
1354 "for Mach-O executable files.\n";
1359 void llvm::printWeakBindTable(const ObjectFile *o) {
1360 outs() << "Weak bind table:\n";
1361 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1362 printMachOWeakBindTable(MachO);
1364 errs() << "This operation is only currently supported "
1365 "for Mach-O executable files.\n";
1370 /// Dump the raw contents of the __clangast section so the output can be piped
1371 /// into llvm-bcanalyzer.
1372 void llvm::printRawClangAST(const ObjectFile *Obj) {
1373 if (outs().is_displayed()) {
1374 errs() << "The -raw-clang-ast option will dump the raw binary contents of "
1375 "the clang ast section.\n"
1376 "Please redirect the output to a file or another program such as "
1377 "llvm-bcanalyzer.\n";
1381 StringRef ClangASTSectionName("__clangast");
1382 if (isa<COFFObjectFile>(Obj)) {
1383 ClangASTSectionName = "clangast";
1386 Optional<object::SectionRef> ClangASTSection;
1387 for (auto Sec : ToolSectionFilter(*Obj)) {
1390 if (Name == ClangASTSectionName) {
1391 ClangASTSection = Sec;
1395 if (!ClangASTSection)
1398 StringRef ClangASTContents;
1399 error(ClangASTSection.getValue().getContents(ClangASTContents));
1400 outs().write(ClangASTContents.data(), ClangASTContents.size());
1403 static void printFaultMaps(const ObjectFile *Obj) {
1404 const char *FaultMapSectionName = nullptr;
1406 if (isa<ELFObjectFileBase>(Obj)) {
1407 FaultMapSectionName = ".llvm_faultmaps";
1408 } else if (isa<MachOObjectFile>(Obj)) {
1409 FaultMapSectionName = "__llvm_faultmaps";
1411 errs() << "This operation is only currently supported "
1412 "for ELF and Mach-O executable files.\n";
1416 Optional<object::SectionRef> FaultMapSection;
1418 for (auto Sec : ToolSectionFilter(*Obj)) {
1421 if (Name == FaultMapSectionName) {
1422 FaultMapSection = Sec;
1427 outs() << "FaultMap table:\n";
1429 if (!FaultMapSection.hasValue()) {
1430 outs() << "<not found>\n";
1434 StringRef FaultMapContents;
1435 error(FaultMapSection.getValue().getContents(FaultMapContents));
1437 FaultMapParser FMP(FaultMapContents.bytes_begin(),
1438 FaultMapContents.bytes_end());
1443 static void printPrivateFileHeader(const ObjectFile *o) {
1445 printELFFileHeader(o);
1446 } else if (o->isCOFF()) {
1447 printCOFFFileHeader(o);
1448 } else if (o->isMachO()) {
1449 printMachOFileHeader(o);
1453 static void DumpObject(const ObjectFile *o) {
1454 // Avoid other output when using a raw option.
1457 outs() << o->getFileName()
1458 << ":\tfile format " << o->getFileFormatName() << "\n\n";
1462 DisassembleObject(o, Relocations);
1463 if (Relocations && !Disassemble)
1464 PrintRelocations(o);
1466 PrintSectionHeaders(o);
1467 if (SectionContents)
1468 PrintSectionContents(o);
1470 PrintSymbolTable(o);
1474 printPrivateFileHeader(o);
1476 printExportsTrie(o);
1478 printRebaseTable(o);
1482 printLazyBindTable(o);
1484 printWeakBindTable(o);
1486 printRawClangAST(o);
1491 /// @brief Dump each object file in \a a;
1492 static void DumpArchive(const Archive *a) {
1493 for (const Archive::Child &C : a->children()) {
1494 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1495 if (std::error_code EC = ChildOrErr.getError())
1496 if (EC != object_error::invalid_file_type)
1497 report_error(a->getFileName(), EC);
1498 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
1501 report_error(a->getFileName(), object_error::invalid_file_type);
1505 /// @brief Open file and figure out how to dump it.
1506 static void DumpInput(StringRef file) {
1507 // If file isn't stdin, check that it exists.
1508 if (file != "-" && !sys::fs::exists(file))
1509 report_error(file, errc::no_such_file_or_directory);
1511 // If we are using the Mach-O specific object file parser, then let it parse
1512 // the file and process the command line options. So the -arch flags can
1513 // be used to select specific slices, etc.
1515 ParseInputMachO(file);
1519 // Attempt to open the binary.
1520 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
1521 if (std::error_code EC = BinaryOrErr.getError())
1522 report_error(file, EC);
1523 Binary &Binary = *BinaryOrErr.get().getBinary();
1525 if (Archive *a = dyn_cast<Archive>(&Binary))
1527 else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
1530 report_error(file, object_error::invalid_file_type);
1533 int main(int argc, char **argv) {
1534 // Print a stack trace if we signal out.
1535 sys::PrintStackTraceOnErrorSignal();
1536 PrettyStackTraceProgram X(argc, argv);
1537 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
1539 // Initialize targets and assembly printers/parsers.
1540 llvm::InitializeAllTargetInfos();
1541 llvm::InitializeAllTargetMCs();
1542 llvm::InitializeAllAsmParsers();
1543 llvm::InitializeAllDisassemblers();
1545 // Register the target printer for --version.
1546 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
1548 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
1549 TripleName = Triple::normalize(TripleName);
1553 // Defaults to a.out if no filenames specified.
1554 if (InputFilenames.size() == 0)
1555 InputFilenames.push_back("a.out");
1572 && !(UniversalHeaders && MachOOpt)
1573 && !(ArchiveHeaders && MachOOpt)
1574 && !(IndirectSymbols && MachOOpt)
1575 && !(DataInCode && MachOOpt)
1576 && !(LinkOptHints && MachOOpt)
1577 && !(InfoPlist && MachOOpt)
1578 && !(DylibsUsed && MachOOpt)
1579 && !(DylibId && MachOOpt)
1580 && !(ObjcMetaData && MachOOpt)
1581 && !(FilterSections.size() != 0 && MachOOpt)
1582 && !PrintFaultMaps) {
1583 cl::PrintHelpMessage();
1587 std::for_each(InputFilenames.begin(), InputFilenames.end(),
1590 return EXIT_SUCCESS;