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/PrettyStackTrace.h"
48 #include "llvm/Support/Signals.h"
49 #include "llvm/Support/SourceMgr.h"
50 #include "llvm/Support/TargetRegistry.h"
51 #include "llvm/Support/TargetSelect.h"
52 #include "llvm/Support/raw_ostream.h"
56 #include <system_error>
59 using namespace object;
61 static cl::list<std::string>
62 InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
65 llvm::Disassemble("disassemble",
66 cl::desc("Display assembler mnemonics for the machine instructions"));
68 Disassembled("d", cl::desc("Alias for --disassemble"),
69 cl::aliasopt(Disassemble));
72 llvm::Relocations("r", cl::desc("Display the relocation entries in the file"));
75 llvm::SectionContents("s", cl::desc("Display the content of each section"));
78 llvm::SymbolTable("t", cl::desc("Display the symbol table"));
81 llvm::ExportsTrie("exports-trie", cl::desc("Display mach-o exported symbols"));
84 llvm::Rebase("rebase", cl::desc("Display mach-o rebasing info"));
87 llvm::Bind("bind", cl::desc("Display mach-o binding info"));
90 llvm::LazyBind("lazy-bind", cl::desc("Display mach-o lazy binding info"));
93 llvm::WeakBind("weak-bind", cl::desc("Display mach-o weak binding info"));
96 MachOOpt("macho", cl::desc("Use MachO specific object file parser"));
98 MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachOOpt));
101 llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
102 "see -version for available targets"));
106 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
107 cl::value_desc("cpu-name"),
111 llvm::ArchName("arch-name", cl::desc("Target arch to disassemble for, "
112 "see -version for available targets"));
115 llvm::SectionHeaders("section-headers", cl::desc("Display summaries of the "
116 "headers for each section."));
118 SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
119 cl::aliasopt(SectionHeaders));
121 SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
122 cl::aliasopt(SectionHeaders));
124 cl::list<std::string>
125 llvm::MAttrs("mattr",
127 cl::desc("Target specific attributes"),
128 cl::value_desc("a1,+a2,-a3,..."));
131 llvm::NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling "
132 "instructions, do not print "
133 "the instruction bytes."));
136 llvm::UnwindInfo("unwind-info", cl::desc("Display unwind information"));
139 UnwindInfoShort("u", cl::desc("Alias for --unwind-info"),
140 cl::aliasopt(UnwindInfo));
143 llvm::PrivateHeaders("private-headers",
144 cl::desc("Display format specific file headers"));
147 PrivateHeadersShort("p", cl::desc("Alias for --private-headers"),
148 cl::aliasopt(PrivateHeaders));
150 static StringRef ToolName;
151 static int ReturnValue = EXIT_SUCCESS;
153 bool llvm::error(std::error_code EC) {
157 outs() << ToolName << ": error reading file: " << EC.message() << ".\n";
159 ReturnValue = EXIT_FAILURE;
163 static const Target *getTarget(const ObjectFile *Obj = nullptr) {
164 // Figure out the target triple.
165 llvm::Triple TheTriple("unknown-unknown-unknown");
166 if (TripleName.empty()) {
168 TheTriple.setArch(Triple::ArchType(Obj->getArch()));
169 // TheTriple defaults to ELF, and COFF doesn't have an environment:
170 // the best we can do here is indicate that it is mach-o.
172 TheTriple.setObjectFormat(Triple::MachO);
175 const auto COFFObj = dyn_cast<COFFObjectFile>(Obj);
176 if (COFFObj->getArch() == Triple::thumb)
177 TheTriple.setTriple("thumbv7-windows");
181 TheTriple.setTriple(Triple::normalize(TripleName));
183 // Get the target specific parser.
185 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
188 errs() << ToolName << ": " << Error;
192 // Update the triple name and return the found target.
193 TripleName = TheTriple.getTriple();
197 void llvm::DumpBytes(ArrayRef<uint8_t> bytes) {
198 static const char hex_rep[] = "0123456789abcdef";
199 SmallString<64> output;
201 for (char i: bytes) {
202 output.push_back(hex_rep[(i & 0xF0) >> 4]);
203 output.push_back(hex_rep[i & 0xF]);
204 output.push_back(' ');
207 outs() << output.c_str();
210 bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
211 uint64_t a_addr, b_addr;
212 if (error(a.getOffset(a_addr))) return false;
213 if (error(b.getOffset(b_addr))) return false;
214 return a_addr < b_addr;
217 static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
218 const Target *TheTarget = getTarget(Obj);
219 // getTarget() will have already issued a diagnostic if necessary, so
220 // just bail here if it failed.
224 // Package up features to be passed to target/subtarget
225 std::string FeaturesStr;
227 SubtargetFeatures Features;
228 for (unsigned i = 0; i != MAttrs.size(); ++i)
229 Features.AddFeature(MAttrs[i]);
230 FeaturesStr = Features.getString();
233 std::unique_ptr<const MCRegisterInfo> MRI(
234 TheTarget->createMCRegInfo(TripleName));
236 errs() << "error: no register info for target " << TripleName << "\n";
240 // Set up disassembler.
241 std::unique_ptr<const MCAsmInfo> AsmInfo(
242 TheTarget->createMCAsmInfo(*MRI, TripleName));
244 errs() << "error: no assembly info for target " << TripleName << "\n";
248 std::unique_ptr<const MCSubtargetInfo> STI(
249 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
251 errs() << "error: no subtarget info for target " << TripleName << "\n";
255 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
257 errs() << "error: no instruction info for target " << TripleName << "\n";
261 std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo);
262 MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get());
264 std::unique_ptr<MCDisassembler> DisAsm(
265 TheTarget->createMCDisassembler(*STI, Ctx));
268 errs() << "error: no disassembler for target " << TripleName << "\n";
272 std::unique_ptr<const MCInstrAnalysis> MIA(
273 TheTarget->createMCInstrAnalysis(MII.get()));
275 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
276 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
277 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI));
279 errs() << "error: no instruction printer for target " << TripleName
284 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ": " :
285 "\t\t\t%08" PRIx64 ": ";
287 // Create a mapping, RelocSecs = SectionRelocMap[S], where sections
288 // in RelocSecs contain the relocations for section S.
290 std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
291 for (const SectionRef &Section : Obj->sections()) {
292 section_iterator Sec2 = Section.getRelocatedSection();
293 if (Sec2 != Obj->section_end())
294 SectionRelocMap[*Sec2].push_back(Section);
297 for (const SectionRef &Section : Obj->sections()) {
298 if (!Section.isText() || Section.isVirtual())
301 uint64_t SectionAddr = Section.getAddress();
302 uint64_t SectSize = Section.getSize();
306 // Make a list of all the symbols in this section.
307 std::vector<std::pair<uint64_t, StringRef>> Symbols;
308 for (const SymbolRef &Symbol : Obj->symbols()) {
309 if (Section.containsSymbol(Symbol)) {
311 if (error(Symbol.getAddress(Address)))
313 if (Address == UnknownAddressOrSize)
315 Address -= SectionAddr;
316 if (Address >= SectSize)
320 if (error(Symbol.getName(Name)))
322 Symbols.push_back(std::make_pair(Address, Name));
326 // Sort the symbols by address, just in case they didn't come in that way.
327 array_pod_sort(Symbols.begin(), Symbols.end());
329 // Make a list of all the relocations for this section.
330 std::vector<RelocationRef> Rels;
332 for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
333 for (const RelocationRef &Reloc : RelocSec.relocations()) {
334 Rels.push_back(Reloc);
339 // Sort relocations by address.
340 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
342 StringRef SegmentName = "";
343 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
344 DataRefImpl DR = Section.getRawDataRefImpl();
345 SegmentName = MachO->getSectionFinalSegmentName(DR);
348 if (error(Section.getName(name)))
350 outs() << "Disassembly of section ";
351 if (!SegmentName.empty())
352 outs() << SegmentName << ",";
353 outs() << name << ':';
355 // If the section has no symbols just insert a dummy one and disassemble
356 // the whole section.
358 Symbols.push_back(std::make_pair(0, name));
361 SmallString<40> Comments;
362 raw_svector_ostream CommentStream(Comments);
365 if (error(Section.getContents(BytesStr)))
367 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
373 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
374 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
375 // Disassemble symbol by symbol.
376 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
378 uint64_t Start = Symbols[si].first;
379 // The end is either the section end or the beginning of the next symbol.
380 uint64_t End = (si == se - 1) ? SectSize : Symbols[si + 1].first;
381 // If this symbol has the same address as the next symbol, then skip it.
385 outs() << '\n' << Symbols[si].second << ":\n";
388 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
390 raw_ostream &DebugOut = nulls();
393 for (Index = Start; Index < End; Index += Size) {
396 if (DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
397 SectionAddr + Index, DebugOut,
399 outs() << format("%8" PRIx64 ":", SectionAddr + Index);
400 if (!NoShowRawInsn) {
402 DumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, Size));
404 IP->printInst(&Inst, outs(), "", *STI);
405 outs() << CommentStream.str();
409 errs() << ToolName << ": warning: invalid instruction encoding\n";
411 Size = 1; // skip illegible bytes
414 // Print relocation for instruction.
415 while (rel_cur != rel_end) {
418 SmallString<16> name;
421 // If this relocation is hidden, skip it.
422 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
423 if (hidden) goto skip_print_rel;
425 if (error(rel_cur->getOffset(addr))) goto skip_print_rel;
426 // Stop when rel_cur's address is past the current instruction.
427 if (addr >= Index + Size) break;
428 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
429 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
431 outs() << format(Fmt.data(), SectionAddr + addr) << name
432 << "\t" << val << "\n";
442 void llvm::PrintRelocations(const ObjectFile *Obj) {
443 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
445 // Regular objdump doesn't print relocations in non-relocatable object
447 if (!Obj->isRelocatableObject())
450 for (const SectionRef &Section : Obj->sections()) {
451 if (Section.relocation_begin() == Section.relocation_end())
454 if (error(Section.getName(secname)))
456 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
457 for (const RelocationRef &Reloc : Section.relocations()) {
460 SmallString<32> relocname;
461 SmallString<32> valuestr;
462 if (error(Reloc.getHidden(hidden)))
466 if (error(Reloc.getTypeName(relocname)))
468 if (error(Reloc.getOffset(address)))
470 if (error(Reloc.getValueString(valuestr)))
472 outs() << format(Fmt.data(), address) << " " << relocname << " "
479 void llvm::PrintSectionHeaders(const ObjectFile *Obj) {
480 outs() << "Sections:\n"
481 "Idx Name Size Address Type\n";
483 for (const SectionRef &Section : Obj->sections()) {
485 if (error(Section.getName(Name)))
487 uint64_t Address = Section.getAddress();
488 uint64_t Size = Section.getSize();
489 bool Text = Section.isText();
490 bool Data = Section.isData();
491 bool BSS = Section.isBSS();
492 std::string Type = (std::string(Text ? "TEXT " : "") +
493 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
494 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
495 Name.str().c_str(), Size, Address, Type.c_str());
500 void llvm::PrintSectionContents(const ObjectFile *Obj) {
502 for (const SectionRef &Section : Obj->sections()) {
505 if (error(Section.getName(Name)))
507 uint64_t BaseAddr = Section.getAddress();
508 uint64_t Size = Section.getSize();
512 outs() << "Contents of section " << Name << ":\n";
513 if (Section.isBSS()) {
514 outs() << format("<skipping contents of bss section at [%04" PRIx64
515 ", %04" PRIx64 ")>\n",
516 BaseAddr, BaseAddr + Size);
520 if (error(Section.getContents(Contents)))
523 // Dump out the content as hex and printable ascii characters.
524 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
525 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
527 for (std::size_t i = 0; i < 16; ++i) {
528 if (i != 0 && i % 4 == 0)
531 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
532 << hexdigit(Contents[addr + i] & 0xF, true);
538 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
539 if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
540 outs() << Contents[addr + i];
549 static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
550 for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) {
551 ErrorOr<COFFSymbolRef> Symbol = coff->getSymbol(SI);
553 if (error(Symbol.getError()))
556 if (error(coff->getSymbolName(*Symbol, Name)))
559 outs() << "[" << format("%2d", SI) << "]"
560 << "(sec " << format("%2d", int(Symbol->getSectionNumber())) << ")"
561 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
562 << "(ty " << format("%3x", unsigned(Symbol->getType())) << ")"
563 << "(scl " << format("%3x", unsigned(Symbol->getStorageClass())) << ") "
564 << "(nx " << unsigned(Symbol->getNumberOfAuxSymbols()) << ") "
565 << "0x" << format("%08x", unsigned(Symbol->getValue())) << " "
568 for (unsigned AI = 0, AE = Symbol->getNumberOfAuxSymbols(); AI < AE; ++AI, ++SI) {
569 if (Symbol->isSectionDefinition()) {
570 const coff_aux_section_definition *asd;
571 if (error(coff->getAuxSymbol<coff_aux_section_definition>(SI + 1, asd)))
574 int32_t AuxNumber = asd->getNumber(Symbol->isBigObj());
577 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
578 , unsigned(asd->Length)
579 , unsigned(asd->NumberOfRelocations)
580 , unsigned(asd->NumberOfLinenumbers)
581 , unsigned(asd->CheckSum))
582 << format("assoc %d comdat %d\n"
583 , unsigned(AuxNumber)
584 , unsigned(asd->Selection));
585 } else if (Symbol->isFileRecord()) {
586 const char *FileName;
587 if (error(coff->getAuxSymbol<char>(SI + 1, FileName)))
590 StringRef Name(FileName, Symbol->getNumberOfAuxSymbols() *
591 coff->getSymbolTableEntrySize());
592 outs() << "AUX " << Name.rtrim(StringRef("\0", 1)) << '\n';
594 SI = SI + Symbol->getNumberOfAuxSymbols();
597 outs() << "AUX Unknown\n";
603 void llvm::PrintSymbolTable(const ObjectFile *o) {
604 outs() << "SYMBOL TABLE:\n";
606 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
607 PrintCOFFSymbolTable(coff);
610 for (const SymbolRef &Symbol : o->symbols()) {
613 SymbolRef::Type Type;
615 uint32_t Flags = Symbol.getFlags();
616 section_iterator Section = o->section_end();
617 if (error(Symbol.getName(Name)))
619 if (error(Symbol.getAddress(Address)))
621 if (error(Symbol.getType(Type)))
623 if (error(Symbol.getSize(Size)))
625 if (error(Symbol.getSection(Section)))
628 bool Global = Flags & SymbolRef::SF_Global;
629 bool Weak = Flags & SymbolRef::SF_Weak;
630 bool Absolute = Flags & SymbolRef::SF_Absolute;
631 bool Common = Flags & SymbolRef::SF_Common;
632 bool Hidden = Flags & SymbolRef::SF_Hidden;
636 if (error(Symbol.getAlignment(Alignment)))
641 if (Address == UnknownAddressOrSize)
643 if (Size == UnknownAddressOrSize)
646 if (Type != SymbolRef::ST_Unknown)
647 GlobLoc = Global ? 'g' : 'l';
648 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
651 if (Type == SymbolRef::ST_File)
653 else if (Type == SymbolRef::ST_Function)
656 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
659 outs() << format(Fmt, Address) << " "
660 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
661 << (Weak ? 'w' : ' ') // Weak?
662 << ' ' // Constructor. Not supported yet.
663 << ' ' // Warning. Not supported yet.
664 << ' ' // Indirect reference to another symbol.
665 << Debug // Debugging (d) or dynamic (D) symbol.
666 << FileFunc // Name of function (F), file (f) or object (O).
672 } else if (Section == o->section_end()) {
675 if (const MachOObjectFile *MachO =
676 dyn_cast<const MachOObjectFile>(o)) {
677 DataRefImpl DR = Section->getRawDataRefImpl();
678 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
679 outs() << SegmentName << ",";
681 StringRef SectionName;
682 if (error(Section->getName(SectionName)))
684 outs() << SectionName;
687 << format("%08" PRIx64 " ", Size);
689 outs() << ".hidden ";
696 static void PrintUnwindInfo(const ObjectFile *o) {
697 outs() << "Unwind info:\n\n";
699 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
700 printCOFFUnwindInfo(coff);
701 } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
702 printMachOUnwindInfo(MachO);
704 // TODO: Extract DWARF dump tool to objdump.
705 errs() << "This operation is only currently supported "
706 "for COFF and MachO object files.\n";
711 void llvm::printExportsTrie(const ObjectFile *o) {
712 outs() << "Exports trie:\n";
713 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
714 printMachOExportsTrie(MachO);
716 errs() << "This operation is only currently supported "
717 "for Mach-O executable files.\n";
722 void llvm::printRebaseTable(const ObjectFile *o) {
723 outs() << "Rebase table:\n";
724 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
725 printMachORebaseTable(MachO);
727 errs() << "This operation is only currently supported "
728 "for Mach-O executable files.\n";
733 void llvm::printBindTable(const ObjectFile *o) {
734 outs() << "Bind table:\n";
735 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
736 printMachOBindTable(MachO);
738 errs() << "This operation is only currently supported "
739 "for Mach-O executable files.\n";
744 void llvm::printLazyBindTable(const ObjectFile *o) {
745 outs() << "Lazy bind table:\n";
746 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
747 printMachOLazyBindTable(MachO);
749 errs() << "This operation is only currently supported "
750 "for Mach-O executable files.\n";
755 void llvm::printWeakBindTable(const ObjectFile *o) {
756 outs() << "Weak bind table:\n";
757 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
758 printMachOWeakBindTable(MachO);
760 errs() << "This operation is only currently supported "
761 "for Mach-O executable files.\n";
766 static void printPrivateFileHeader(const ObjectFile *o) {
768 printELFFileHeader(o);
769 } else if (o->isCOFF()) {
770 printCOFFFileHeader(o);
771 } else if (o->isMachO()) {
772 printMachOFileHeader(o);
776 static void DumpObject(const ObjectFile *o) {
778 outs() << o->getFileName()
779 << ":\tfile format " << o->getFileFormatName() << "\n\n";
782 DisassembleObject(o, Relocations);
783 if (Relocations && !Disassemble)
786 PrintSectionHeaders(o);
788 PrintSectionContents(o);
794 printPrivateFileHeader(o);
802 printLazyBindTable(o);
804 printWeakBindTable(o);
807 /// @brief Dump each object file in \a a;
808 static void DumpArchive(const Archive *a) {
809 for (Archive::child_iterator i = a->child_begin(), e = a->child_end(); i != e;
811 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
812 if (std::error_code EC = ChildOrErr.getError()) {
813 // Ignore non-object files.
814 if (EC != object_error::invalid_file_type)
815 errs() << ToolName << ": '" << a->getFileName() << "': " << EC.message()
819 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
822 errs() << ToolName << ": '" << a->getFileName() << "': "
823 << "Unrecognized file type.\n";
827 /// @brief Open file and figure out how to dump it.
828 static void DumpInput(StringRef file) {
829 // If file isn't stdin, check that it exists.
830 if (file != "-" && !sys::fs::exists(file)) {
831 errs() << ToolName << ": '" << file << "': " << "No such file\n";
835 // If we are using the Mach-O specific object file parser, then let it parse
836 // the file and process the command line options. So the -arch flags can
837 // be used to select specific slices, etc.
839 ParseInputMachO(file);
843 // Attempt to open the binary.
844 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
845 if (std::error_code EC = BinaryOrErr.getError()) {
846 errs() << ToolName << ": '" << file << "': " << EC.message() << ".\n";
849 Binary &Binary = *BinaryOrErr.get().getBinary();
851 if (Archive *a = dyn_cast<Archive>(&Binary))
853 else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
856 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
859 int main(int argc, char **argv) {
860 // Print a stack trace if we signal out.
861 sys::PrintStackTraceOnErrorSignal();
862 PrettyStackTraceProgram X(argc, argv);
863 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
865 // Initialize targets and assembly printers/parsers.
866 llvm::InitializeAllTargetInfos();
867 llvm::InitializeAllTargetMCs();
868 llvm::InitializeAllAsmParsers();
869 llvm::InitializeAllDisassemblers();
871 // Register the target printer for --version.
872 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
874 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
875 TripleName = Triple::normalize(TripleName);
879 // Defaults to a.out if no filenames specified.
880 if (InputFilenames.size() == 0)
881 InputFilenames.push_back("a.out");
895 && !(UniversalHeaders && MachOOpt)
896 && !(ArchiveHeaders && MachOOpt)
897 && !(IndirectSymbols && MachOOpt)
898 && !(DataInCode && MachOOpt)
899 && !(LinkOptHints && MachOOpt)
900 && !(InfoPlist && MachOOpt)
901 && !(DylibsUsed && MachOOpt)
902 && !(DylibId && MachOOpt)
903 && !(ObjcMetaData && MachOOpt)
904 && !(DumpSections.size() != 0 && MachOOpt)) {
905 cl::PrintHelpMessage();
909 std::for_each(InputFilenames.begin(), InputFilenames.end(),