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(StringRef 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 AsmPrinterVariant, *AsmInfo, *MII, *MRI, *STI));
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) {
403 reinterpret_cast<const char *>(Bytes.data()) + Index, Size));
405 IP->printInst(&Inst, outs(), "");
406 outs() << CommentStream.str();
410 errs() << ToolName << ": warning: invalid instruction encoding\n";
412 Size = 1; // skip illegible bytes
415 // Print relocation for instruction.
416 while (rel_cur != rel_end) {
419 SmallString<16> name;
422 // If this relocation is hidden, skip it.
423 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
424 if (hidden) goto skip_print_rel;
426 if (error(rel_cur->getOffset(addr))) goto skip_print_rel;
427 // Stop when rel_cur's address is past the current instruction.
428 if (addr >= Index + Size) break;
429 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
430 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
432 outs() << format(Fmt.data(), SectionAddr + addr) << name
433 << "\t" << val << "\n";
443 void llvm::PrintRelocations(const ObjectFile *Obj) {
444 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
446 // Regular objdump doesn't print relocations in non-relocatable object
448 if (!Obj->isRelocatableObject())
451 for (const SectionRef &Section : Obj->sections()) {
452 if (Section.relocation_begin() == Section.relocation_end())
455 if (error(Section.getName(secname)))
457 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
458 for (const RelocationRef &Reloc : Section.relocations()) {
461 SmallString<32> relocname;
462 SmallString<32> valuestr;
463 if (error(Reloc.getHidden(hidden)))
467 if (error(Reloc.getTypeName(relocname)))
469 if (error(Reloc.getOffset(address)))
471 if (error(Reloc.getValueString(valuestr)))
473 outs() << format(Fmt.data(), address) << " " << relocname << " "
480 void llvm::PrintSectionHeaders(const ObjectFile *Obj) {
481 outs() << "Sections:\n"
482 "Idx Name Size Address Type\n";
484 for (const SectionRef &Section : Obj->sections()) {
486 if (error(Section.getName(Name)))
488 uint64_t Address = Section.getAddress();
489 uint64_t Size = Section.getSize();
490 bool Text = Section.isText();
491 bool Data = Section.isData();
492 bool BSS = Section.isBSS();
493 std::string Type = (std::string(Text ? "TEXT " : "") +
494 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
495 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
496 Name.str().c_str(), Size, Address, Type.c_str());
501 void llvm::PrintSectionContents(const ObjectFile *Obj) {
503 for (const SectionRef &Section : Obj->sections()) {
506 if (error(Section.getName(Name)))
508 uint64_t BaseAddr = Section.getAddress();
509 uint64_t Size = Section.getSize();
513 outs() << "Contents of section " << Name << ":\n";
514 if (Section.isBSS()) {
515 outs() << format("<skipping contents of bss section at [%04" PRIx64
516 ", %04" PRIx64 ")>\n",
517 BaseAddr, BaseAddr + Size);
521 if (error(Section.getContents(Contents)))
524 // Dump out the content as hex and printable ascii characters.
525 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
526 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
528 for (std::size_t i = 0; i < 16; ++i) {
529 if (i != 0 && i % 4 == 0)
532 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
533 << hexdigit(Contents[addr + i] & 0xF, true);
539 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
540 if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
541 outs() << Contents[addr + i];
550 static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
551 for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) {
552 ErrorOr<COFFSymbolRef> Symbol = coff->getSymbol(SI);
554 if (error(Symbol.getError()))
557 if (error(coff->getSymbolName(*Symbol, Name)))
560 outs() << "[" << format("%2d", SI) << "]"
561 << "(sec " << format("%2d", int(Symbol->getSectionNumber())) << ")"
562 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
563 << "(ty " << format("%3x", unsigned(Symbol->getType())) << ")"
564 << "(scl " << format("%3x", unsigned(Symbol->getStorageClass())) << ") "
565 << "(nx " << unsigned(Symbol->getNumberOfAuxSymbols()) << ") "
566 << "0x" << format("%08x", unsigned(Symbol->getValue())) << " "
569 for (unsigned AI = 0, AE = Symbol->getNumberOfAuxSymbols(); AI < AE; ++AI, ++SI) {
570 if (Symbol->isSectionDefinition()) {
571 const coff_aux_section_definition *asd;
572 if (error(coff->getAuxSymbol<coff_aux_section_definition>(SI + 1, asd)))
575 int32_t AuxNumber = asd->getNumber(Symbol->isBigObj());
578 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
579 , unsigned(asd->Length)
580 , unsigned(asd->NumberOfRelocations)
581 , unsigned(asd->NumberOfLinenumbers)
582 , unsigned(asd->CheckSum))
583 << format("assoc %d comdat %d\n"
584 , unsigned(AuxNumber)
585 , unsigned(asd->Selection));
586 } else if (Symbol->isFileRecord()) {
587 const char *FileName;
588 if (error(coff->getAuxSymbol<char>(SI + 1, FileName)))
591 StringRef Name(FileName, Symbol->getNumberOfAuxSymbols() *
592 coff->getSymbolTableEntrySize());
593 outs() << "AUX " << Name.rtrim(StringRef("\0", 1)) << '\n';
595 SI = SI + Symbol->getNumberOfAuxSymbols();
598 outs() << "AUX Unknown\n";
604 void llvm::PrintSymbolTable(const ObjectFile *o) {
605 outs() << "SYMBOL TABLE:\n";
607 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
608 PrintCOFFSymbolTable(coff);
611 for (const SymbolRef &Symbol : o->symbols()) {
614 SymbolRef::Type Type;
616 uint32_t Flags = Symbol.getFlags();
617 section_iterator Section = o->section_end();
618 if (error(Symbol.getName(Name)))
620 if (error(Symbol.getAddress(Address)))
622 if (error(Symbol.getType(Type)))
624 if (error(Symbol.getSize(Size)))
626 if (error(Symbol.getSection(Section)))
629 bool Global = Flags & SymbolRef::SF_Global;
630 bool Weak = Flags & SymbolRef::SF_Weak;
631 bool Absolute = Flags & SymbolRef::SF_Absolute;
632 bool Common = Flags & SymbolRef::SF_Common;
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)
693 static void PrintUnwindInfo(const ObjectFile *o) {
694 outs() << "Unwind info:\n\n";
696 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
697 printCOFFUnwindInfo(coff);
698 } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
699 printMachOUnwindInfo(MachO);
701 // TODO: Extract DWARF dump tool to objdump.
702 errs() << "This operation is only currently supported "
703 "for COFF and MachO object files.\n";
708 void llvm::printExportsTrie(const ObjectFile *o) {
709 outs() << "Exports trie:\n";
710 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
711 printMachOExportsTrie(MachO);
713 errs() << "This operation is only currently supported "
714 "for Mach-O executable files.\n";
719 void llvm::printRebaseTable(const ObjectFile *o) {
720 outs() << "Rebase table:\n";
721 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
722 printMachORebaseTable(MachO);
724 errs() << "This operation is only currently supported "
725 "for Mach-O executable files.\n";
730 void llvm::printBindTable(const ObjectFile *o) {
731 outs() << "Bind table:\n";
732 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
733 printMachOBindTable(MachO);
735 errs() << "This operation is only currently supported "
736 "for Mach-O executable files.\n";
741 void llvm::printLazyBindTable(const ObjectFile *o) {
742 outs() << "Lazy bind table:\n";
743 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
744 printMachOLazyBindTable(MachO);
746 errs() << "This operation is only currently supported "
747 "for Mach-O executable files.\n";
752 void llvm::printWeakBindTable(const ObjectFile *o) {
753 outs() << "Weak bind table:\n";
754 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
755 printMachOWeakBindTable(MachO);
757 errs() << "This operation is only currently supported "
758 "for Mach-O executable files.\n";
763 static void printPrivateFileHeader(const ObjectFile *o) {
765 printELFFileHeader(o);
766 } else if (o->isCOFF()) {
767 printCOFFFileHeader(o);
768 } else if (o->isMachO()) {
769 printMachOFileHeader(o);
773 static void DumpObject(const ObjectFile *o) {
775 outs() << o->getFileName()
776 << ":\tfile format " << o->getFileFormatName() << "\n\n";
779 DisassembleObject(o, Relocations);
780 if (Relocations && !Disassemble)
783 PrintSectionHeaders(o);
785 PrintSectionContents(o);
791 printPrivateFileHeader(o);
799 printLazyBindTable(o);
801 printWeakBindTable(o);
804 /// @brief Dump each object file in \a a;
805 static void DumpArchive(const Archive *a) {
806 for (Archive::child_iterator i = a->child_begin(), e = a->child_end(); i != e;
808 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
809 if (std::error_code EC = ChildOrErr.getError()) {
810 // Ignore non-object files.
811 if (EC != object_error::invalid_file_type)
812 errs() << ToolName << ": '" << a->getFileName() << "': " << EC.message()
816 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
819 errs() << ToolName << ": '" << a->getFileName() << "': "
820 << "Unrecognized file type.\n";
824 /// @brief Open file and figure out how to dump it.
825 static void DumpInput(StringRef file) {
826 // If file isn't stdin, check that it exists.
827 if (file != "-" && !sys::fs::exists(file)) {
828 errs() << ToolName << ": '" << file << "': " << "No such file\n";
832 // If we are using the Mach-O specific object file parser, then let it parse
833 // the file and process the command line options. So the -arch flags can
834 // be used to select specific slices, etc.
836 ParseInputMachO(file);
840 // Attempt to open the binary.
841 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
842 if (std::error_code EC = BinaryOrErr.getError()) {
843 errs() << ToolName << ": '" << file << "': " << EC.message() << ".\n";
846 Binary &Binary = *BinaryOrErr.get().getBinary();
848 if (Archive *a = dyn_cast<Archive>(&Binary))
850 else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
853 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
856 int main(int argc, char **argv) {
857 // Print a stack trace if we signal out.
858 sys::PrintStackTraceOnErrorSignal();
859 PrettyStackTraceProgram X(argc, argv);
860 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
862 // Initialize targets and assembly printers/parsers.
863 llvm::InitializeAllTargetInfos();
864 llvm::InitializeAllTargetMCs();
865 llvm::InitializeAllAsmParsers();
866 llvm::InitializeAllDisassemblers();
868 // Register the target printer for --version.
869 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
871 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
872 TripleName = Triple::normalize(TripleName);
876 // Defaults to a.out if no filenames specified.
877 if (InputFilenames.size() == 0)
878 InputFilenames.push_back("a.out");
892 && !(UniversalHeaders && MachOOpt)
893 && !(ArchiveHeaders && MachOOpt)
894 && !(IndirectSymbols && MachOOpt)
895 && !(DataInCode && MachOOpt)
896 && !(LinkOptHints && MachOOpt)
897 && !(InfoPlist && MachOOpt)
898 && !(DylibsUsed && MachOOpt)
899 && !(DylibId && MachOOpt)
900 && !(DumpSections.size() != 0 && MachOOpt)) {
901 cl::PrintHelpMessage();
905 std::for_each(InputFilenames.begin(), InputFilenames.end(),