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 bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
198 uint64_t a_addr, b_addr;
199 if (error(a.getOffset(a_addr))) return false;
200 if (error(b.getOffset(b_addr))) return false;
201 return a_addr < b_addr;
205 class PrettyPrinter {
207 virtual void printInst(MCInstPrinter &IP, const MCInst *MI, bool ShowRawInsn,
208 ArrayRef<uint8_t> Bytes, uint64_t Address,
209 raw_ostream &OS, StringRef Annot,
210 MCSubtargetInfo const &STI) {
211 outs() << format("%8" PRIx64 ":", Address);
212 if (!NoShowRawInsn) {
214 dumpBytes(Bytes, outs());
216 IP.printInst(MI, outs(), "", STI);
219 PrettyPrinter PrettyPrinterInst;
220 PrettyPrinter &selectPrettyPrinter(Triple const &Triple, MCInstPrinter &IP) {
221 switch(Triple.getArch()) {
223 return PrettyPrinterInst;
228 static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
229 const Target *TheTarget = getTarget(Obj);
230 // getTarget() will have already issued a diagnostic if necessary, so
231 // just bail here if it failed.
235 // Package up features to be passed to target/subtarget
236 std::string FeaturesStr;
238 SubtargetFeatures Features;
239 for (unsigned i = 0; i != MAttrs.size(); ++i)
240 Features.AddFeature(MAttrs[i]);
241 FeaturesStr = Features.getString();
244 std::unique_ptr<const MCRegisterInfo> MRI(
245 TheTarget->createMCRegInfo(TripleName));
247 errs() << "error: no register info for target " << TripleName << "\n";
251 // Set up disassembler.
252 std::unique_ptr<const MCAsmInfo> AsmInfo(
253 TheTarget->createMCAsmInfo(*MRI, TripleName));
255 errs() << "error: no assembly info for target " << TripleName << "\n";
259 std::unique_ptr<const MCSubtargetInfo> STI(
260 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
262 errs() << "error: no subtarget info for target " << TripleName << "\n";
266 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
268 errs() << "error: no instruction info for target " << TripleName << "\n";
272 std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo);
273 MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get());
275 std::unique_ptr<MCDisassembler> DisAsm(
276 TheTarget->createMCDisassembler(*STI, Ctx));
279 errs() << "error: no disassembler for target " << TripleName << "\n";
283 std::unique_ptr<const MCInstrAnalysis> MIA(
284 TheTarget->createMCInstrAnalysis(MII.get()));
286 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
287 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
288 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI));
290 errs() << "error: no instruction printer for target " << TripleName
294 PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName), *IP);
296 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ": " :
297 "\t\t\t%08" PRIx64 ": ";
299 // Create a mapping, RelocSecs = SectionRelocMap[S], where sections
300 // in RelocSecs contain the relocations for section S.
302 std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
303 for (const SectionRef &Section : Obj->sections()) {
304 section_iterator Sec2 = Section.getRelocatedSection();
305 if (Sec2 != Obj->section_end())
306 SectionRelocMap[*Sec2].push_back(Section);
309 for (const SectionRef &Section : Obj->sections()) {
310 if (!Section.isText() || Section.isVirtual())
313 uint64_t SectionAddr = Section.getAddress();
314 uint64_t SectSize = Section.getSize();
318 // Make a list of all the symbols in this section.
319 std::vector<std::pair<uint64_t, StringRef>> Symbols;
320 for (const SymbolRef &Symbol : Obj->symbols()) {
321 if (Section.containsSymbol(Symbol)) {
323 if (error(Symbol.getAddress(Address)))
325 if (Address == UnknownAddressOrSize)
327 Address -= SectionAddr;
328 if (Address >= SectSize)
332 if (error(Symbol.getName(Name)))
334 Symbols.push_back(std::make_pair(Address, Name));
338 // Sort the symbols by address, just in case they didn't come in that way.
339 array_pod_sort(Symbols.begin(), Symbols.end());
341 // Make a list of all the relocations for this section.
342 std::vector<RelocationRef> Rels;
344 for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
345 for (const RelocationRef &Reloc : RelocSec.relocations()) {
346 Rels.push_back(Reloc);
351 // Sort relocations by address.
352 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
354 StringRef SegmentName = "";
355 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
356 DataRefImpl DR = Section.getRawDataRefImpl();
357 SegmentName = MachO->getSectionFinalSegmentName(DR);
360 if (error(Section.getName(name)))
362 outs() << "Disassembly of section ";
363 if (!SegmentName.empty())
364 outs() << SegmentName << ",";
365 outs() << name << ':';
367 // If the section has no symbols just insert a dummy one and disassemble
368 // the whole section.
370 Symbols.push_back(std::make_pair(0, name));
373 SmallString<40> Comments;
374 raw_svector_ostream CommentStream(Comments);
377 if (error(Section.getContents(BytesStr)))
379 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
385 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
386 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
387 // Disassemble symbol by symbol.
388 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
390 uint64_t Start = Symbols[si].first;
391 // The end is either the section end or the beginning of the next symbol.
392 uint64_t End = (si == se - 1) ? SectSize : Symbols[si + 1].first;
393 // If this symbol has the same address as the next symbol, then skip it.
397 outs() << '\n' << Symbols[si].second << ":\n";
400 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
402 raw_ostream &DebugOut = nulls();
405 for (Index = Start; Index < End; Index += Size) {
408 if (DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
409 SectionAddr + Index, DebugOut,
411 PIP.printInst(*IP, &Inst, !NoShowRawInsn,
412 Bytes.slice(Index, Size),
413 SectionAddr + Index, outs(), "", *STI);
414 outs() << CommentStream.str();
418 errs() << ToolName << ": warning: invalid instruction encoding\n";
420 Size = 1; // skip illegible bytes
423 // Print relocation for instruction.
424 while (rel_cur != rel_end) {
427 SmallString<16> name;
430 // If this relocation is hidden, skip it.
431 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
432 if (hidden) goto skip_print_rel;
434 if (error(rel_cur->getOffset(addr))) goto skip_print_rel;
435 // Stop when rel_cur's address is past the current instruction.
436 if (addr >= Index + Size) break;
437 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
438 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
440 outs() << format(Fmt.data(), SectionAddr + addr) << name
441 << "\t" << val << "\n";
451 void llvm::PrintRelocations(const ObjectFile *Obj) {
452 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
454 // Regular objdump doesn't print relocations in non-relocatable object
456 if (!Obj->isRelocatableObject())
459 for (const SectionRef &Section : Obj->sections()) {
460 if (Section.relocation_begin() == Section.relocation_end())
463 if (error(Section.getName(secname)))
465 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
466 for (const RelocationRef &Reloc : Section.relocations()) {
469 SmallString<32> relocname;
470 SmallString<32> valuestr;
471 if (error(Reloc.getHidden(hidden)))
475 if (error(Reloc.getTypeName(relocname)))
477 if (error(Reloc.getOffset(address)))
479 if (error(Reloc.getValueString(valuestr)))
481 outs() << format(Fmt.data(), address) << " " << relocname << " "
488 void llvm::PrintSectionHeaders(const ObjectFile *Obj) {
489 outs() << "Sections:\n"
490 "Idx Name Size Address Type\n";
492 for (const SectionRef &Section : Obj->sections()) {
494 if (error(Section.getName(Name)))
496 uint64_t Address = Section.getAddress();
497 uint64_t Size = Section.getSize();
498 bool Text = Section.isText();
499 bool Data = Section.isData();
500 bool BSS = Section.isBSS();
501 std::string Type = (std::string(Text ? "TEXT " : "") +
502 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
503 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
504 Name.str().c_str(), Size, Address, Type.c_str());
509 void llvm::PrintSectionContents(const ObjectFile *Obj) {
511 for (const SectionRef &Section : Obj->sections()) {
514 if (error(Section.getName(Name)))
516 uint64_t BaseAddr = Section.getAddress();
517 uint64_t Size = Section.getSize();
521 outs() << "Contents of section " << Name << ":\n";
522 if (Section.isBSS()) {
523 outs() << format("<skipping contents of bss section at [%04" PRIx64
524 ", %04" PRIx64 ")>\n",
525 BaseAddr, BaseAddr + Size);
529 if (error(Section.getContents(Contents)))
532 // Dump out the content as hex and printable ascii characters.
533 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
534 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
536 for (std::size_t i = 0; i < 16; ++i) {
537 if (i != 0 && i % 4 == 0)
540 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
541 << hexdigit(Contents[addr + i] & 0xF, true);
547 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
548 if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
549 outs() << Contents[addr + i];
558 static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
559 for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) {
560 ErrorOr<COFFSymbolRef> Symbol = coff->getSymbol(SI);
562 if (error(Symbol.getError()))
565 if (error(coff->getSymbolName(*Symbol, Name)))
568 outs() << "[" << format("%2d", SI) << "]"
569 << "(sec " << format("%2d", int(Symbol->getSectionNumber())) << ")"
570 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
571 << "(ty " << format("%3x", unsigned(Symbol->getType())) << ")"
572 << "(scl " << format("%3x", unsigned(Symbol->getStorageClass())) << ") "
573 << "(nx " << unsigned(Symbol->getNumberOfAuxSymbols()) << ") "
574 << "0x" << format("%08x", unsigned(Symbol->getValue())) << " "
577 for (unsigned AI = 0, AE = Symbol->getNumberOfAuxSymbols(); AI < AE; ++AI, ++SI) {
578 if (Symbol->isSectionDefinition()) {
579 const coff_aux_section_definition *asd;
580 if (error(coff->getAuxSymbol<coff_aux_section_definition>(SI + 1, asd)))
583 int32_t AuxNumber = asd->getNumber(Symbol->isBigObj());
586 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
587 , unsigned(asd->Length)
588 , unsigned(asd->NumberOfRelocations)
589 , unsigned(asd->NumberOfLinenumbers)
590 , unsigned(asd->CheckSum))
591 << format("assoc %d comdat %d\n"
592 , unsigned(AuxNumber)
593 , unsigned(asd->Selection));
594 } else if (Symbol->isFileRecord()) {
595 const char *FileName;
596 if (error(coff->getAuxSymbol<char>(SI + 1, FileName)))
599 StringRef Name(FileName, Symbol->getNumberOfAuxSymbols() *
600 coff->getSymbolTableEntrySize());
601 outs() << "AUX " << Name.rtrim(StringRef("\0", 1)) << '\n';
603 SI = SI + Symbol->getNumberOfAuxSymbols();
606 outs() << "AUX Unknown\n";
612 void llvm::PrintSymbolTable(const ObjectFile *o) {
613 outs() << "SYMBOL TABLE:\n";
615 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
616 PrintCOFFSymbolTable(coff);
619 for (const SymbolRef &Symbol : o->symbols()) {
622 SymbolRef::Type Type;
624 uint32_t Flags = Symbol.getFlags();
625 section_iterator Section = o->section_end();
626 if (error(Symbol.getName(Name)))
628 if (error(Symbol.getAddress(Address)))
630 if (error(Symbol.getType(Type)))
632 if (error(Symbol.getSize(Size)))
634 if (error(Symbol.getSection(Section)))
637 bool Global = Flags & SymbolRef::SF_Global;
638 bool Weak = Flags & SymbolRef::SF_Weak;
639 bool Absolute = Flags & SymbolRef::SF_Absolute;
640 bool Common = Flags & SymbolRef::SF_Common;
641 bool Hidden = Flags & SymbolRef::SF_Hidden;
645 if (error(Symbol.getAlignment(Alignment)))
650 if (Address == UnknownAddressOrSize)
652 if (Size == UnknownAddressOrSize)
655 if (Type != SymbolRef::ST_Unknown)
656 GlobLoc = Global ? 'g' : 'l';
657 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
660 if (Type == SymbolRef::ST_File)
662 else if (Type == SymbolRef::ST_Function)
665 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
668 outs() << format(Fmt, Address) << " "
669 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
670 << (Weak ? 'w' : ' ') // Weak?
671 << ' ' // Constructor. Not supported yet.
672 << ' ' // Warning. Not supported yet.
673 << ' ' // Indirect reference to another symbol.
674 << Debug // Debugging (d) or dynamic (D) symbol.
675 << FileFunc // Name of function (F), file (f) or object (O).
681 } else if (Section == o->section_end()) {
684 if (const MachOObjectFile *MachO =
685 dyn_cast<const MachOObjectFile>(o)) {
686 DataRefImpl DR = Section->getRawDataRefImpl();
687 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
688 outs() << SegmentName << ",";
690 StringRef SectionName;
691 if (error(Section->getName(SectionName)))
693 outs() << SectionName;
696 << format("%08" PRIx64 " ", Size);
698 outs() << ".hidden ";
705 static void PrintUnwindInfo(const ObjectFile *o) {
706 outs() << "Unwind info:\n\n";
708 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
709 printCOFFUnwindInfo(coff);
710 } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
711 printMachOUnwindInfo(MachO);
713 // TODO: Extract DWARF dump tool to objdump.
714 errs() << "This operation is only currently supported "
715 "for COFF and MachO object files.\n";
720 void llvm::printExportsTrie(const ObjectFile *o) {
721 outs() << "Exports trie:\n";
722 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
723 printMachOExportsTrie(MachO);
725 errs() << "This operation is only currently supported "
726 "for Mach-O executable files.\n";
731 void llvm::printRebaseTable(const ObjectFile *o) {
732 outs() << "Rebase table:\n";
733 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
734 printMachORebaseTable(MachO);
736 errs() << "This operation is only currently supported "
737 "for Mach-O executable files.\n";
742 void llvm::printBindTable(const ObjectFile *o) {
743 outs() << "Bind table:\n";
744 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
745 printMachOBindTable(MachO);
747 errs() << "This operation is only currently supported "
748 "for Mach-O executable files.\n";
753 void llvm::printLazyBindTable(const ObjectFile *o) {
754 outs() << "Lazy bind table:\n";
755 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
756 printMachOLazyBindTable(MachO);
758 errs() << "This operation is only currently supported "
759 "for Mach-O executable files.\n";
764 void llvm::printWeakBindTable(const ObjectFile *o) {
765 outs() << "Weak bind table:\n";
766 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
767 printMachOWeakBindTable(MachO);
769 errs() << "This operation is only currently supported "
770 "for Mach-O executable files.\n";
775 static void printPrivateFileHeader(const ObjectFile *o) {
777 printELFFileHeader(o);
778 } else if (o->isCOFF()) {
779 printCOFFFileHeader(o);
780 } else if (o->isMachO()) {
781 printMachOFileHeader(o);
785 static void DumpObject(const ObjectFile *o) {
787 outs() << o->getFileName()
788 << ":\tfile format " << o->getFileFormatName() << "\n\n";
791 DisassembleObject(o, Relocations);
792 if (Relocations && !Disassemble)
795 PrintSectionHeaders(o);
797 PrintSectionContents(o);
803 printPrivateFileHeader(o);
811 printLazyBindTable(o);
813 printWeakBindTable(o);
816 /// @brief Dump each object file in \a a;
817 static void DumpArchive(const Archive *a) {
818 for (Archive::child_iterator i = a->child_begin(), e = a->child_end(); i != e;
820 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
821 if (std::error_code EC = ChildOrErr.getError()) {
822 // Ignore non-object files.
823 if (EC != object_error::invalid_file_type)
824 errs() << ToolName << ": '" << a->getFileName() << "': " << EC.message()
828 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
831 errs() << ToolName << ": '" << a->getFileName() << "': "
832 << "Unrecognized file type.\n";
836 /// @brief Open file and figure out how to dump it.
837 static void DumpInput(StringRef file) {
838 // If file isn't stdin, check that it exists.
839 if (file != "-" && !sys::fs::exists(file)) {
840 errs() << ToolName << ": '" << file << "': " << "No such file\n";
844 // If we are using the Mach-O specific object file parser, then let it parse
845 // the file and process the command line options. So the -arch flags can
846 // be used to select specific slices, etc.
848 ParseInputMachO(file);
852 // Attempt to open the binary.
853 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
854 if (std::error_code EC = BinaryOrErr.getError()) {
855 errs() << ToolName << ": '" << file << "': " << EC.message() << ".\n";
858 Binary &Binary = *BinaryOrErr.get().getBinary();
860 if (Archive *a = dyn_cast<Archive>(&Binary))
862 else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
865 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
868 int main(int argc, char **argv) {
869 // Print a stack trace if we signal out.
870 sys::PrintStackTraceOnErrorSignal();
871 PrettyStackTraceProgram X(argc, argv);
872 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
874 // Initialize targets and assembly printers/parsers.
875 llvm::InitializeAllTargetInfos();
876 llvm::InitializeAllTargetMCs();
877 llvm::InitializeAllAsmParsers();
878 llvm::InitializeAllDisassemblers();
880 // Register the target printer for --version.
881 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
883 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
884 TripleName = Triple::normalize(TripleName);
888 // Defaults to a.out if no filenames specified.
889 if (InputFilenames.size() == 0)
890 InputFilenames.push_back("a.out");
904 && !(UniversalHeaders && MachOOpt)
905 && !(ArchiveHeaders && MachOOpt)
906 && !(IndirectSymbols && MachOOpt)
907 && !(DataInCode && MachOOpt)
908 && !(LinkOptHints && MachOOpt)
909 && !(InfoPlist && MachOOpt)
910 && !(DylibsUsed && MachOOpt)
911 && !(DylibId && MachOOpt)
912 && !(ObjcMetaData && MachOOpt)
913 && !(DumpSections.size() != 0 && MachOOpt)) {
914 cl::PrintHelpMessage();
918 std::for_each(InputFilenames.begin(), InputFilenames.end(),