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/STLExtras.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/ADT/Triple.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/MC/MCContext.h"
25 #include "llvm/MC/MCDisassembler.h"
26 #include "llvm/MC/MCInst.h"
27 #include "llvm/MC/MCInstPrinter.h"
28 #include "llvm/MC/MCInstrAnalysis.h"
29 #include "llvm/MC/MCInstrInfo.h"
30 #include "llvm/MC/MCObjectFileInfo.h"
31 #include "llvm/MC/MCRegisterInfo.h"
32 #include "llvm/MC/MCRelocationInfo.h"
33 #include "llvm/MC/MCSubtargetInfo.h"
34 #include "llvm/Object/Archive.h"
35 #include "llvm/Object/COFF.h"
36 #include "llvm/Object/MachO.h"
37 #include "llvm/Object/ObjectFile.h"
38 #include "llvm/Support/Casting.h"
39 #include "llvm/Support/CommandLine.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/FileSystem.h"
42 #include "llvm/Support/Format.h"
43 #include "llvm/Support/GraphWriter.h"
44 #include "llvm/Support/Host.h"
45 #include "llvm/Support/ManagedStatic.h"
46 #include "llvm/Support/MemoryBuffer.h"
47 #include "llvm/Support/MemoryObject.h"
48 #include "llvm/Support/PrettyStackTrace.h"
49 #include "llvm/Support/Signals.h"
50 #include "llvm/Support/SourceMgr.h"
51 #include "llvm/Support/TargetRegistry.h"
52 #include "llvm/Support/TargetSelect.h"
53 #include "llvm/Support/raw_ostream.h"
57 #include <system_error>
60 using namespace object;
62 static cl::list<std::string>
63 InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
66 Disassemble("disassemble",
67 cl::desc("Display assembler mnemonics for the machine instructions"));
69 Disassembled("d", cl::desc("Alias for --disassemble"),
70 cl::aliasopt(Disassemble));
73 Relocations("r", cl::desc("Display the relocation entries in the file"));
76 SectionContents("s", cl::desc("Display the content of each section"));
79 SymbolTable("t", cl::desc("Display the symbol table"));
82 ExportsTrie("exports-trie", cl::desc("Display mach-o exported symbols"));
85 Rebase("rebase", cl::desc("Display mach-o rebasing info"));
88 Bind("bind", cl::desc("Display mach-o binding info"));
91 LazyBind("lazy-bind", cl::desc("Display mach-o lazy binding info"));
94 WeakBind("weak-bind", cl::desc("Display mach-o weak binding info"));
97 MachOOpt("macho", cl::desc("Use MachO specific object file parser"));
99 MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachOOpt));
102 llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
103 "see -version for available targets"));
107 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
108 cl::value_desc("cpu-name"),
112 llvm::ArchName("arch-name", cl::desc("Target arch to disassemble for, "
113 "see -version for available targets"));
116 SectionHeaders("section-headers", cl::desc("Display summaries of the headers "
117 "for each section."));
119 SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
120 cl::aliasopt(SectionHeaders));
122 SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
123 cl::aliasopt(SectionHeaders));
125 cl::list<std::string>
126 llvm::MAttrs("mattr",
128 cl::desc("Target specific attributes"),
129 cl::value_desc("a1,+a2,-a3,..."));
132 llvm::NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling "
133 "instructions, do not print "
134 "the instruction bytes."));
137 UnwindInfo("unwind-info", cl::desc("Display unwind information"));
140 UnwindInfoShort("u", cl::desc("Alias for --unwind-info"),
141 cl::aliasopt(UnwindInfo));
144 PrivateHeaders("private-headers",
145 cl::desc("Display format specific file headers"));
148 PrivateHeadersShort("p", cl::desc("Alias for --private-headers"),
149 cl::aliasopt(PrivateHeaders));
151 static StringRef ToolName;
152 static int ReturnValue = EXIT_SUCCESS;
154 bool llvm::error(std::error_code EC) {
158 outs() << ToolName << ": error reading file: " << EC.message() << ".\n";
160 ReturnValue = EXIT_FAILURE;
164 static const Target *getTarget(const ObjectFile *Obj = nullptr) {
165 // Figure out the target triple.
166 llvm::Triple TheTriple("unknown-unknown-unknown");
167 if (TripleName.empty()) {
169 TheTriple.setArch(Triple::ArchType(Obj->getArch()));
170 // TheTriple defaults to ELF, and COFF doesn't have an environment:
171 // the best we can do here is indicate that it is mach-o.
173 TheTriple.setObjectFormat(Triple::MachO);
176 const auto COFFObj = dyn_cast<COFFObjectFile>(Obj);
177 if (COFFObj->getArch() == Triple::thumb)
178 TheTriple.setTriple("thumbv7-windows");
182 TheTriple.setTriple(Triple::normalize(TripleName));
184 // Get the target specific parser.
186 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
189 errs() << ToolName << ": " << Error;
193 // Update the triple name and return the found target.
194 TripleName = TheTriple.getTriple();
198 void llvm::DumpBytes(StringRef bytes) {
199 static const char hex_rep[] = "0123456789abcdef";
200 // FIXME: The real way to do this is to figure out the longest instruction
201 // and align to that size before printing. I'll fix this when I get
202 // around to outputting relocations.
203 // 15 is the longest x86 instruction
204 // 3 is for the hex rep of a byte + a space.
205 // 1 is for the null terminator.
206 enum { OutputSize = (15 * 3) + 1 };
207 char output[OutputSize];
209 assert(bytes.size() <= 15
210 && "DumpBytes only supports instructions of up to 15 bytes");
211 memset(output, ' ', sizeof(output));
213 for (StringRef::iterator i = bytes.begin(),
214 e = bytes.end(); i != e; ++i) {
215 output[index] = hex_rep[(*i & 0xF0) >> 4];
216 output[index + 1] = hex_rep[*i & 0xF];
220 output[sizeof(output) - 1] = 0;
224 bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
225 uint64_t a_addr, b_addr;
226 if (error(a.getOffset(a_addr))) return false;
227 if (error(b.getOffset(b_addr))) return false;
228 return a_addr < b_addr;
231 static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
232 const Target *TheTarget = getTarget(Obj);
233 // getTarget() will have already issued a diagnostic if necessary, so
234 // just bail here if it failed.
238 // Package up features to be passed to target/subtarget
239 std::string FeaturesStr;
241 SubtargetFeatures Features;
242 for (unsigned i = 0; i != MAttrs.size(); ++i)
243 Features.AddFeature(MAttrs[i]);
244 FeaturesStr = Features.getString();
247 std::unique_ptr<const MCRegisterInfo> MRI(
248 TheTarget->createMCRegInfo(TripleName));
250 errs() << "error: no register info for target " << TripleName << "\n";
254 // Set up disassembler.
255 std::unique_ptr<const MCAsmInfo> AsmInfo(
256 TheTarget->createMCAsmInfo(*MRI, TripleName));
258 errs() << "error: no assembly info for target " << TripleName << "\n";
262 std::unique_ptr<const MCSubtargetInfo> STI(
263 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
265 errs() << "error: no subtarget info for target " << TripleName << "\n";
269 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
271 errs() << "error: no instruction info for target " << TripleName << "\n";
275 std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo);
276 MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get());
278 std::unique_ptr<MCDisassembler> DisAsm(
279 TheTarget->createMCDisassembler(*STI, Ctx));
282 errs() << "error: no disassembler for target " << TripleName << "\n";
286 std::unique_ptr<const MCInstrAnalysis> MIA(
287 TheTarget->createMCInstrAnalysis(MII.get()));
289 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
290 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
291 AsmPrinterVariant, *AsmInfo, *MII, *MRI, *STI));
293 errs() << "error: no instruction printer for target " << TripleName
298 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ": " :
299 "\t\t\t%08" PRIx64 ": ";
301 // Create a mapping, RelocSecs = SectionRelocMap[S], where sections
302 // in RelocSecs contain the relocations for section S.
304 std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
305 for (const SectionRef &Section : Obj->sections()) {
306 section_iterator Sec2 = Section.getRelocatedSection();
307 if (Sec2 != Obj->section_end())
308 SectionRelocMap[*Sec2].push_back(Section);
311 for (const SectionRef &Section : Obj->sections()) {
312 if (!Section.isText() || Section.isVirtual())
315 uint64_t SectionAddr = Section.getAddress();
316 uint64_t SectSize = Section.getSize();
320 // Make a list of all the symbols in this section.
321 std::vector<std::pair<uint64_t, StringRef>> Symbols;
322 for (const SymbolRef &Symbol : Obj->symbols()) {
323 if (Section.containsSymbol(Symbol)) {
325 if (error(Symbol.getAddress(Address)))
327 if (Address == UnknownAddressOrSize)
329 Address -= SectionAddr;
330 if (Address >= SectSize)
334 if (error(Symbol.getName(Name)))
336 Symbols.push_back(std::make_pair(Address, Name));
340 // Sort the symbols by address, just in case they didn't come in that way.
341 array_pod_sort(Symbols.begin(), Symbols.end());
343 // Make a list of all the relocations for this section.
344 std::vector<RelocationRef> Rels;
346 for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
347 for (const RelocationRef &Reloc : RelocSec.relocations()) {
348 Rels.push_back(Reloc);
353 // Sort relocations by address.
354 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
356 StringRef SegmentName = "";
357 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
358 DataRefImpl DR = Section.getRawDataRefImpl();
359 SegmentName = MachO->getSectionFinalSegmentName(DR);
362 if (error(Section.getName(name)))
364 outs() << "Disassembly of section ";
365 if (!SegmentName.empty())
366 outs() << SegmentName << ",";
367 outs() << name << ':';
369 // If the section has no symbols just insert a dummy one and disassemble
370 // the whole section.
372 Symbols.push_back(std::make_pair(0, name));
375 SmallString<40> Comments;
376 raw_svector_ostream CommentStream(Comments);
379 if (error(Section.getContents(BytesStr)))
381 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
387 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
388 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
389 // Disassemble symbol by symbol.
390 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
392 uint64_t Start = Symbols[si].first;
393 // The end is either the section end or the beginning of the next symbol.
394 uint64_t End = (si == se - 1) ? SectSize : Symbols[si + 1].first;
395 // If this symbol has the same address as the next symbol, then skip it.
399 outs() << '\n' << Symbols[si].second << ":\n";
402 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
404 raw_ostream &DebugOut = nulls();
407 for (Index = Start; Index < End; Index += Size) {
410 if (DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
411 SectionAddr + Index, DebugOut,
413 outs() << format("%8" PRIx64 ":", SectionAddr + Index);
414 if (!NoShowRawInsn) {
417 reinterpret_cast<const char *>(Bytes.data()) + Index, Size));
419 IP->printInst(&Inst, outs(), "");
420 outs() << CommentStream.str();
424 errs() << ToolName << ": warning: invalid instruction encoding\n";
426 Size = 1; // skip illegible bytes
429 // Print relocation for instruction.
430 while (rel_cur != rel_end) {
433 SmallString<16> name;
436 // If this relocation is hidden, skip it.
437 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
438 if (hidden) goto skip_print_rel;
440 if (error(rel_cur->getOffset(addr))) goto skip_print_rel;
441 // Stop when rel_cur's address is past the current instruction.
442 if (addr >= Index + Size) break;
443 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
444 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
446 outs() << format(Fmt.data(), SectionAddr + addr) << name
447 << "\t" << val << "\n";
457 static void PrintRelocations(const ObjectFile *Obj) {
458 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
460 // Regular objdump doesn't print relocations in non-relocatable object
462 if (!Obj->isRelocatableObject())
465 for (const SectionRef &Section : Obj->sections()) {
466 if (Section.relocation_begin() == Section.relocation_end())
469 if (error(Section.getName(secname)))
471 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
472 for (const RelocationRef &Reloc : Section.relocations()) {
475 SmallString<32> relocname;
476 SmallString<32> valuestr;
477 if (error(Reloc.getHidden(hidden)))
481 if (error(Reloc.getTypeName(relocname)))
483 if (error(Reloc.getOffset(address)))
485 if (error(Reloc.getValueString(valuestr)))
487 outs() << format(Fmt.data(), address) << " " << relocname << " "
494 static void PrintSectionHeaders(const ObjectFile *Obj) {
495 outs() << "Sections:\n"
496 "Idx Name Size Address Type\n";
498 for (const SectionRef &Section : Obj->sections()) {
500 if (error(Section.getName(Name)))
502 uint64_t Address = Section.getAddress();
503 uint64_t Size = Section.getSize();
504 bool Text = Section.isText();
505 bool Data = Section.isData();
506 bool BSS = Section.isBSS();
507 std::string Type = (std::string(Text ? "TEXT " : "") +
508 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
509 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
510 Name.str().c_str(), Size, Address, Type.c_str());
515 static void PrintSectionContents(const ObjectFile *Obj) {
517 for (const SectionRef &Section : Obj->sections()) {
520 if (error(Section.getName(Name)))
522 uint64_t BaseAddr = Section.getAddress();
523 uint64_t Size = Section.getSize();
527 outs() << "Contents of section " << Name << ":\n";
528 if (Section.isBSS()) {
529 outs() << format("<skipping contents of bss section at [%04" PRIx64
530 ", %04" PRIx64 ")>\n",
531 BaseAddr, BaseAddr + Size);
535 if (error(Section.getContents(Contents)))
538 // Dump out the content as hex and printable ascii characters.
539 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
540 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
542 for (std::size_t i = 0; i < 16; ++i) {
543 if (i != 0 && i % 4 == 0)
546 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
547 << hexdigit(Contents[addr + i] & 0xF, true);
553 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
554 if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
555 outs() << Contents[addr + i];
564 static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
565 for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) {
566 ErrorOr<COFFSymbolRef> Symbol = coff->getSymbol(SI);
568 if (error(Symbol.getError()))
571 if (error(coff->getSymbolName(*Symbol, Name)))
574 outs() << "[" << format("%2d", SI) << "]"
575 << "(sec " << format("%2d", int(Symbol->getSectionNumber())) << ")"
576 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
577 << "(ty " << format("%3x", unsigned(Symbol->getType())) << ")"
578 << "(scl " << format("%3x", unsigned(Symbol->getStorageClass())) << ") "
579 << "(nx " << unsigned(Symbol->getNumberOfAuxSymbols()) << ") "
580 << "0x" << format("%08x", unsigned(Symbol->getValue())) << " "
583 for (unsigned AI = 0, AE = Symbol->getNumberOfAuxSymbols(); AI < AE; ++AI, ++SI) {
584 if (Symbol->isSectionDefinition()) {
585 const coff_aux_section_definition *asd;
586 if (error(coff->getAuxSymbol<coff_aux_section_definition>(SI + 1, asd)))
589 int32_t AuxNumber = asd->getNumber(Symbol->isBigObj());
592 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
593 , unsigned(asd->Length)
594 , unsigned(asd->NumberOfRelocations)
595 , unsigned(asd->NumberOfLinenumbers)
596 , unsigned(asd->CheckSum))
597 << format("assoc %d comdat %d\n"
598 , unsigned(AuxNumber)
599 , unsigned(asd->Selection));
600 } else if (Symbol->isFileRecord()) {
601 const char *FileName;
602 if (error(coff->getAuxSymbol<char>(SI + 1, FileName)))
605 StringRef Name(FileName, Symbol->getNumberOfAuxSymbols() *
606 coff->getSymbolTableEntrySize());
607 outs() << "AUX " << Name.rtrim(StringRef("\0", 1)) << '\n';
609 SI = SI + Symbol->getNumberOfAuxSymbols();
612 outs() << "AUX Unknown\n";
618 static void PrintSymbolTable(const ObjectFile *o) {
619 outs() << "SYMBOL TABLE:\n";
621 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
622 PrintCOFFSymbolTable(coff);
625 for (const SymbolRef &Symbol : o->symbols()) {
628 SymbolRef::Type Type;
630 uint32_t Flags = Symbol.getFlags();
631 section_iterator Section = o->section_end();
632 if (error(Symbol.getName(Name)))
634 if (error(Symbol.getAddress(Address)))
636 if (error(Symbol.getType(Type)))
638 if (error(Symbol.getSize(Size)))
640 if (error(Symbol.getSection(Section)))
643 bool Global = Flags & SymbolRef::SF_Global;
644 bool Weak = Flags & SymbolRef::SF_Weak;
645 bool Absolute = Flags & SymbolRef::SF_Absolute;
647 if (Address == UnknownAddressOrSize)
649 if (Size == UnknownAddressOrSize)
652 if (Type != SymbolRef::ST_Unknown)
653 GlobLoc = Global ? 'g' : 'l';
654 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
657 if (Type == SymbolRef::ST_File)
659 else if (Type == SymbolRef::ST_Function)
662 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
665 outs() << format(Fmt, Address) << " "
666 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
667 << (Weak ? 'w' : ' ') // Weak?
668 << ' ' // Constructor. Not supported yet.
669 << ' ' // Warning. Not supported yet.
670 << ' ' // Indirect reference to another symbol.
671 << Debug // Debugging (d) or dynamic (D) symbol.
672 << FileFunc // Name of function (F), file (f) or object (O).
676 } else if (Section == o->section_end()) {
679 if (const MachOObjectFile *MachO =
680 dyn_cast<const MachOObjectFile>(o)) {
681 DataRefImpl DR = Section->getRawDataRefImpl();
682 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
683 outs() << SegmentName << ",";
685 StringRef SectionName;
686 if (error(Section->getName(SectionName)))
688 outs() << SectionName;
691 << format("%08" PRIx64 " ", Size)
697 static void PrintUnwindInfo(const ObjectFile *o) {
698 outs() << "Unwind info:\n\n";
700 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
701 printCOFFUnwindInfo(coff);
702 } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
703 printMachOUnwindInfo(MachO);
705 // TODO: Extract DWARF dump tool to objdump.
706 errs() << "This operation is only currently supported "
707 "for COFF and MachO object files.\n";
712 static void printExportsTrie(const ObjectFile *o) {
713 outs() << "Exports trie:\n";
714 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
715 printMachOExportsTrie(MachO);
717 errs() << "This operation is only currently supported "
718 "for Mach-O executable files.\n";
723 static void printRebaseTable(const ObjectFile *o) {
724 outs() << "Rebase table:\n";
725 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
726 printMachORebaseTable(MachO);
728 errs() << "This operation is only currently supported "
729 "for Mach-O executable files.\n";
734 static void printBindTable(const ObjectFile *o) {
735 outs() << "Bind table:\n";
736 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
737 printMachOBindTable(MachO);
739 errs() << "This operation is only currently supported "
740 "for Mach-O executable files.\n";
745 static void printLazyBindTable(const ObjectFile *o) {
746 outs() << "Lazy bind table:\n";
747 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
748 printMachOLazyBindTable(MachO);
750 errs() << "This operation is only currently supported "
751 "for Mach-O executable files.\n";
756 static void printWeakBindTable(const ObjectFile *o) {
757 outs() << "Weak bind table:\n";
758 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
759 printMachOWeakBindTable(MachO);
761 errs() << "This operation is only currently supported "
762 "for Mach-O executable files.\n";
767 static void printPrivateFileHeader(const ObjectFile *o) {
769 printELFFileHeader(o);
770 } else if (o->isCOFF()) {
771 printCOFFFileHeader(o);
772 } else if (o->isMachO()) {
773 printMachOFileHeader(o);
777 static void DumpObject(const ObjectFile *o) {
779 outs() << o->getFileName()
780 << ":\tfile format " << o->getFileFormatName() << "\n\n";
783 DisassembleObject(o, Relocations);
784 if (Relocations && !Disassemble)
787 PrintSectionHeaders(o);
789 PrintSectionContents(o);
795 printPrivateFileHeader(o);
803 printLazyBindTable(o);
805 printWeakBindTable(o);
808 /// @brief Dump each object file in \a a;
809 static void DumpArchive(const Archive *a) {
810 for (Archive::child_iterator i = a->child_begin(), e = a->child_end(); i != e;
812 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
813 if (std::error_code EC = ChildOrErr.getError()) {
814 // Ignore non-object files.
815 if (EC != object_error::invalid_file_type)
816 errs() << ToolName << ": '" << a->getFileName() << "': " << EC.message()
820 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
823 errs() << ToolName << ": '" << a->getFileName() << "': "
824 << "Unrecognized file type.\n";
828 /// @brief Open file and figure out how to dump it.
829 static void DumpInput(StringRef file) {
830 // If file isn't stdin, check that it exists.
831 if (file != "-" && !sys::fs::exists(file)) {
832 errs() << ToolName << ": '" << file << "': " << "No such file\n";
836 if (MachOOpt && Disassemble) {
837 DisassembleInputMachO(file);
841 // Attempt to open the binary.
842 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
843 if (std::error_code EC = BinaryOrErr.getError()) {
844 errs() << ToolName << ": '" << file << "': " << EC.message() << ".\n";
847 Binary &Binary = *BinaryOrErr.get().getBinary();
849 if (Archive *a = dyn_cast<Archive>(&Binary))
851 else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
854 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
857 int main(int argc, char **argv) {
858 // Print a stack trace if we signal out.
859 sys::PrintStackTraceOnErrorSignal();
860 PrettyStackTraceProgram X(argc, argv);
861 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
863 // Initialize targets and assembly printers/parsers.
864 llvm::InitializeAllTargetInfos();
865 llvm::InitializeAllTargetMCs();
866 llvm::InitializeAllAsmParsers();
867 llvm::InitializeAllDisassemblers();
869 // Register the target printer for --version.
870 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
872 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
873 TripleName = Triple::normalize(TripleName);
877 // Defaults to a.out if no filenames specified.
878 if (InputFilenames.size() == 0)
879 InputFilenames.push_back("a.out");
893 cl::PrintHelpMessage();
897 std::for_each(InputFilenames.begin(), InputFilenames.end(),