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 // FIXME: The real way to do this is to figure out the longest instruction
200 // and align to that size before printing. I'll fix this when I get
201 // around to outputting relocations.
202 // 15 is the longest x86 instruction
203 // 3 is for the hex rep of a byte + a space.
204 // 1 is for the null terminator.
205 enum { OutputSize = (15 * 3) + 1 };
206 char output[OutputSize];
208 assert(bytes.size() <= 15
209 && "DumpBytes only supports instructions of up to 15 bytes");
210 memset(output, ' ', sizeof(output));
212 for (StringRef::iterator i = bytes.begin(),
213 e = bytes.end(); i != e; ++i) {
214 output[index] = hex_rep[(*i & 0xF0) >> 4];
215 output[index + 1] = hex_rep[*i & 0xF];
219 output[sizeof(output) - 1] = 0;
223 bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
224 uint64_t a_addr, b_addr;
225 if (error(a.getOffset(a_addr))) return false;
226 if (error(b.getOffset(b_addr))) return false;
227 return a_addr < b_addr;
230 static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
231 const Target *TheTarget = getTarget(Obj);
232 // getTarget() will have already issued a diagnostic if necessary, so
233 // just bail here if it failed.
237 // Package up features to be passed to target/subtarget
238 std::string FeaturesStr;
240 SubtargetFeatures Features;
241 for (unsigned i = 0; i != MAttrs.size(); ++i)
242 Features.AddFeature(MAttrs[i]);
243 FeaturesStr = Features.getString();
246 std::unique_ptr<const MCRegisterInfo> MRI(
247 TheTarget->createMCRegInfo(TripleName));
249 errs() << "error: no register info for target " << TripleName << "\n";
253 // Set up disassembler.
254 std::unique_ptr<const MCAsmInfo> AsmInfo(
255 TheTarget->createMCAsmInfo(*MRI, TripleName));
257 errs() << "error: no assembly info for target " << TripleName << "\n";
261 std::unique_ptr<const MCSubtargetInfo> STI(
262 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
264 errs() << "error: no subtarget info for target " << TripleName << "\n";
268 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
270 errs() << "error: no instruction info for target " << TripleName << "\n";
274 std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo);
275 MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get());
277 std::unique_ptr<MCDisassembler> DisAsm(
278 TheTarget->createMCDisassembler(*STI, Ctx));
281 errs() << "error: no disassembler for target " << TripleName << "\n";
285 std::unique_ptr<const MCInstrAnalysis> MIA(
286 TheTarget->createMCInstrAnalysis(MII.get()));
288 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
289 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
290 AsmPrinterVariant, *AsmInfo, *MII, *MRI, *STI));
292 errs() << "error: no instruction printer for target " << TripleName
297 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ": " :
298 "\t\t\t%08" PRIx64 ": ";
300 // Create a mapping, RelocSecs = SectionRelocMap[S], where sections
301 // in RelocSecs contain the relocations for section S.
303 std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
304 for (const SectionRef &Section : Obj->sections()) {
305 section_iterator Sec2 = Section.getRelocatedSection();
306 if (Sec2 != Obj->section_end())
307 SectionRelocMap[*Sec2].push_back(Section);
310 for (const SectionRef &Section : Obj->sections()) {
311 if (!Section.isText() || Section.isVirtual())
314 uint64_t SectionAddr = Section.getAddress();
315 uint64_t SectSize = Section.getSize();
319 // Make a list of all the symbols in this section.
320 std::vector<std::pair<uint64_t, StringRef>> Symbols;
321 for (const SymbolRef &Symbol : Obj->symbols()) {
322 if (Section.containsSymbol(Symbol)) {
324 if (error(Symbol.getAddress(Address)))
326 if (Address == UnknownAddressOrSize)
328 Address -= SectionAddr;
329 if (Address >= SectSize)
333 if (error(Symbol.getName(Name)))
335 Symbols.push_back(std::make_pair(Address, Name));
339 // Sort the symbols by address, just in case they didn't come in that way.
340 array_pod_sort(Symbols.begin(), Symbols.end());
342 // Make a list of all the relocations for this section.
343 std::vector<RelocationRef> Rels;
345 for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
346 for (const RelocationRef &Reloc : RelocSec.relocations()) {
347 Rels.push_back(Reloc);
352 // Sort relocations by address.
353 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
355 StringRef SegmentName = "";
356 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
357 DataRefImpl DR = Section.getRawDataRefImpl();
358 SegmentName = MachO->getSectionFinalSegmentName(DR);
361 if (error(Section.getName(name)))
363 outs() << "Disassembly of section ";
364 if (!SegmentName.empty())
365 outs() << SegmentName << ",";
366 outs() << name << ':';
368 // If the section has no symbols just insert a dummy one and disassemble
369 // the whole section.
371 Symbols.push_back(std::make_pair(0, name));
374 SmallString<40> Comments;
375 raw_svector_ostream CommentStream(Comments);
378 if (error(Section.getContents(BytesStr)))
380 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
386 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
387 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
388 // Disassemble symbol by symbol.
389 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
391 uint64_t Start = Symbols[si].first;
392 // The end is either the section end or the beginning of the next symbol.
393 uint64_t End = (si == se - 1) ? SectSize : Symbols[si + 1].first;
394 // If this symbol has the same address as the next symbol, then skip it.
398 outs() << '\n' << Symbols[si].second << ":\n";
401 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
403 raw_ostream &DebugOut = nulls();
406 for (Index = Start; Index < End; Index += Size) {
409 if (DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
410 SectionAddr + Index, DebugOut,
412 outs() << format("%8" PRIx64 ":", SectionAddr + Index);
413 if (!NoShowRawInsn) {
416 reinterpret_cast<const char *>(Bytes.data()) + Index, Size));
418 IP->printInst(&Inst, outs(), "");
419 outs() << CommentStream.str();
423 errs() << ToolName << ": warning: invalid instruction encoding\n";
425 Size = 1; // skip illegible bytes
428 // Print relocation for instruction.
429 while (rel_cur != rel_end) {
432 SmallString<16> name;
435 // If this relocation is hidden, skip it.
436 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
437 if (hidden) goto skip_print_rel;
439 if (error(rel_cur->getOffset(addr))) goto skip_print_rel;
440 // Stop when rel_cur's address is past the current instruction.
441 if (addr >= Index + Size) break;
442 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
443 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
445 outs() << format(Fmt.data(), SectionAddr + addr) << name
446 << "\t" << val << "\n";
456 void llvm::PrintRelocations(const ObjectFile *Obj) {
457 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
459 // Regular objdump doesn't print relocations in non-relocatable object
461 if (!Obj->isRelocatableObject())
464 for (const SectionRef &Section : Obj->sections()) {
465 if (Section.relocation_begin() == Section.relocation_end())
468 if (error(Section.getName(secname)))
470 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
471 for (const RelocationRef &Reloc : Section.relocations()) {
474 SmallString<32> relocname;
475 SmallString<32> valuestr;
476 if (error(Reloc.getHidden(hidden)))
480 if (error(Reloc.getTypeName(relocname)))
482 if (error(Reloc.getOffset(address)))
484 if (error(Reloc.getValueString(valuestr)))
486 outs() << format(Fmt.data(), address) << " " << relocname << " "
493 void llvm::PrintSectionHeaders(const ObjectFile *Obj) {
494 outs() << "Sections:\n"
495 "Idx Name Size Address Type\n";
497 for (const SectionRef &Section : Obj->sections()) {
499 if (error(Section.getName(Name)))
501 uint64_t Address = Section.getAddress();
502 uint64_t Size = Section.getSize();
503 bool Text = Section.isText();
504 bool Data = Section.isData();
505 bool BSS = Section.isBSS();
506 std::string Type = (std::string(Text ? "TEXT " : "") +
507 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
508 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
509 Name.str().c_str(), Size, Address, Type.c_str());
514 void llvm::PrintSectionContents(const ObjectFile *Obj) {
516 for (const SectionRef &Section : Obj->sections()) {
519 if (error(Section.getName(Name)))
521 uint64_t BaseAddr = Section.getAddress();
522 uint64_t Size = Section.getSize();
526 outs() << "Contents of section " << Name << ":\n";
527 if (Section.isBSS()) {
528 outs() << format("<skipping contents of bss section at [%04" PRIx64
529 ", %04" PRIx64 ")>\n",
530 BaseAddr, BaseAddr + Size);
534 if (error(Section.getContents(Contents)))
537 // Dump out the content as hex and printable ascii characters.
538 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
539 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
541 for (std::size_t i = 0; i < 16; ++i) {
542 if (i != 0 && i % 4 == 0)
545 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
546 << hexdigit(Contents[addr + i] & 0xF, true);
552 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
553 if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
554 outs() << Contents[addr + i];
563 static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
564 for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) {
565 ErrorOr<COFFSymbolRef> Symbol = coff->getSymbol(SI);
567 if (error(Symbol.getError()))
570 if (error(coff->getSymbolName(*Symbol, Name)))
573 outs() << "[" << format("%2d", SI) << "]"
574 << "(sec " << format("%2d", int(Symbol->getSectionNumber())) << ")"
575 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
576 << "(ty " << format("%3x", unsigned(Symbol->getType())) << ")"
577 << "(scl " << format("%3x", unsigned(Symbol->getStorageClass())) << ") "
578 << "(nx " << unsigned(Symbol->getNumberOfAuxSymbols()) << ") "
579 << "0x" << format("%08x", unsigned(Symbol->getValue())) << " "
582 for (unsigned AI = 0, AE = Symbol->getNumberOfAuxSymbols(); AI < AE; ++AI, ++SI) {
583 if (Symbol->isSectionDefinition()) {
584 const coff_aux_section_definition *asd;
585 if (error(coff->getAuxSymbol<coff_aux_section_definition>(SI + 1, asd)))
588 int32_t AuxNumber = asd->getNumber(Symbol->isBigObj());
591 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
592 , unsigned(asd->Length)
593 , unsigned(asd->NumberOfRelocations)
594 , unsigned(asd->NumberOfLinenumbers)
595 , unsigned(asd->CheckSum))
596 << format("assoc %d comdat %d\n"
597 , unsigned(AuxNumber)
598 , unsigned(asd->Selection));
599 } else if (Symbol->isFileRecord()) {
600 const char *FileName;
601 if (error(coff->getAuxSymbol<char>(SI + 1, FileName)))
604 StringRef Name(FileName, Symbol->getNumberOfAuxSymbols() *
605 coff->getSymbolTableEntrySize());
606 outs() << "AUX " << Name.rtrim(StringRef("\0", 1)) << '\n';
608 SI = SI + Symbol->getNumberOfAuxSymbols();
611 outs() << "AUX Unknown\n";
617 void llvm::PrintSymbolTable(const ObjectFile *o) {
618 outs() << "SYMBOL TABLE:\n";
620 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
621 PrintCOFFSymbolTable(coff);
624 for (const SymbolRef &Symbol : o->symbols()) {
627 SymbolRef::Type Type;
629 uint32_t Flags = Symbol.getFlags();
630 section_iterator Section = o->section_end();
631 if (error(Symbol.getName(Name)))
633 if (error(Symbol.getAddress(Address)))
635 if (error(Symbol.getType(Type)))
637 if (error(Symbol.getSize(Size)))
639 if (error(Symbol.getSection(Section)))
642 bool Global = Flags & SymbolRef::SF_Global;
643 bool Weak = Flags & SymbolRef::SF_Weak;
644 bool Absolute = Flags & SymbolRef::SF_Absolute;
645 bool Common = Flags & SymbolRef::SF_Common;
649 if (error(Symbol.getAlignment(Alignment)))
654 if (Address == UnknownAddressOrSize)
656 if (Size == UnknownAddressOrSize)
659 if (Type != SymbolRef::ST_Unknown)
660 GlobLoc = Global ? 'g' : 'l';
661 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
664 if (Type == SymbolRef::ST_File)
666 else if (Type == SymbolRef::ST_Function)
669 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
672 outs() << format(Fmt, Address) << " "
673 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
674 << (Weak ? 'w' : ' ') // Weak?
675 << ' ' // Constructor. Not supported yet.
676 << ' ' // Warning. Not supported yet.
677 << ' ' // Indirect reference to another symbol.
678 << Debug // Debugging (d) or dynamic (D) symbol.
679 << FileFunc // Name of function (F), file (f) or object (O).
685 } else if (Section == o->section_end()) {
688 if (const MachOObjectFile *MachO =
689 dyn_cast<const MachOObjectFile>(o)) {
690 DataRefImpl DR = Section->getRawDataRefImpl();
691 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
692 outs() << SegmentName << ",";
694 StringRef SectionName;
695 if (error(Section->getName(SectionName)))
697 outs() << SectionName;
700 << format("%08" PRIx64 " ", Size)
706 static void PrintUnwindInfo(const ObjectFile *o) {
707 outs() << "Unwind info:\n\n";
709 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
710 printCOFFUnwindInfo(coff);
711 } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
712 printMachOUnwindInfo(MachO);
714 // TODO: Extract DWARF dump tool to objdump.
715 errs() << "This operation is only currently supported "
716 "for COFF and MachO object files.\n";
721 void llvm::printExportsTrie(const ObjectFile *o) {
722 outs() << "Exports trie:\n";
723 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
724 printMachOExportsTrie(MachO);
726 errs() << "This operation is only currently supported "
727 "for Mach-O executable files.\n";
732 void llvm::printRebaseTable(const ObjectFile *o) {
733 outs() << "Rebase table:\n";
734 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
735 printMachORebaseTable(MachO);
737 errs() << "This operation is only currently supported "
738 "for Mach-O executable files.\n";
743 void llvm::printBindTable(const ObjectFile *o) {
744 outs() << "Bind table:\n";
745 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
746 printMachOBindTable(MachO);
748 errs() << "This operation is only currently supported "
749 "for Mach-O executable files.\n";
754 void llvm::printLazyBindTable(const ObjectFile *o) {
755 outs() << "Lazy bind table:\n";
756 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
757 printMachOLazyBindTable(MachO);
759 errs() << "This operation is only currently supported "
760 "for Mach-O executable files.\n";
765 void llvm::printWeakBindTable(const ObjectFile *o) {
766 outs() << "Weak bind table:\n";
767 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
768 printMachOWeakBindTable(MachO);
770 errs() << "This operation is only currently supported "
771 "for Mach-O executable files.\n";
776 static void printPrivateFileHeader(const ObjectFile *o) {
778 printELFFileHeader(o);
779 } else if (o->isCOFF()) {
780 printCOFFFileHeader(o);
781 } else if (o->isMachO()) {
782 printMachOFileHeader(o);
786 static void DumpObject(const ObjectFile *o) {
788 outs() << o->getFileName()
789 << ":\tfile format " << o->getFileFormatName() << "\n\n";
792 DisassembleObject(o, Relocations);
793 if (Relocations && !Disassemble)
796 PrintSectionHeaders(o);
798 PrintSectionContents(o);
804 printPrivateFileHeader(o);
812 printLazyBindTable(o);
814 printWeakBindTable(o);
817 /// @brief Dump each object file in \a a;
818 static void DumpArchive(const Archive *a) {
819 for (Archive::child_iterator i = a->child_begin(), e = a->child_end(); i != e;
821 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
822 if (std::error_code EC = ChildOrErr.getError()) {
823 // Ignore non-object files.
824 if (EC != object_error::invalid_file_type)
825 errs() << ToolName << ": '" << a->getFileName() << "': " << EC.message()
829 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
832 errs() << ToolName << ": '" << a->getFileName() << "': "
833 << "Unrecognized file type.\n";
837 /// @brief Open file and figure out how to dump it.
838 static void DumpInput(StringRef file) {
839 // If file isn't stdin, check that it exists.
840 if (file != "-" && !sys::fs::exists(file)) {
841 errs() << ToolName << ": '" << file << "': " << "No such file\n";
845 // If we are using the Mach-O specific object file parser, then let it parse
846 // the file and process the command line options. So the -arch flags can
847 // be used to select specific slices, etc.
849 ParseInputMachO(file);
853 // Attempt to open the binary.
854 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
855 if (std::error_code EC = BinaryOrErr.getError()) {
856 errs() << ToolName << ": '" << file << "': " << EC.message() << ".\n";
859 Binary &Binary = *BinaryOrErr.get().getBinary();
861 if (Archive *a = dyn_cast<Archive>(&Binary))
863 else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
866 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
869 int main(int argc, char **argv) {
870 // Print a stack trace if we signal out.
871 sys::PrintStackTraceOnErrorSignal();
872 PrettyStackTraceProgram X(argc, argv);
873 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
875 // Initialize targets and assembly printers/parsers.
876 llvm::InitializeAllTargetInfos();
877 llvm::InitializeAllTargetMCs();
878 llvm::InitializeAllAsmParsers();
879 llvm::InitializeAllDisassemblers();
881 // Register the target printer for --version.
882 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
884 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
885 TripleName = Triple::normalize(TripleName);
889 // Defaults to a.out if no filenames specified.
890 if (InputFilenames.size() == 0)
891 InputFilenames.push_back("a.out");
905 && !(UniversalHeaders && MachOOpt)
906 && !(ArchiveHeaders && MachOOpt)
907 && !(IndirectSymbols && MachOOpt)
908 && !(DataInCode && MachOOpt)
909 && !(LinkOptHints && MachOOpt)) {
910 cl::PrintHelpMessage();
914 std::for_each(InputFilenames.begin(), InputFilenames.end(),