1 //===-- llvm-objdump.cpp - Object file dumping utility for llvm -----------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 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 12 // flags. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "llvm-objdump.h" 17 #include "MCFunction.h" 18 #include "llvm/ADT/OwningPtr.h" 19 #include "llvm/ADT/STLExtras.h" 20 #include "llvm/ADT/StringExtras.h" 21 #include "llvm/ADT/Triple.h" 22 #include "llvm/MC/MCAsmInfo.h" 23 #include "llvm/MC/MCDisassembler.h" 24 #include "llvm/MC/MCInst.h" 25 #include "llvm/MC/MCInstPrinter.h" 26 #include "llvm/MC/MCInstrInfo.h" 27 #include "llvm/MC/MCRegisterInfo.h" 28 #include "llvm/MC/MCSubtargetInfo.h" 29 #include "llvm/Object/Archive.h" 30 #include "llvm/Object/COFF.h" 31 #include "llvm/Object/MachO.h" 32 #include "llvm/Object/ObjectFile.h" 33 #include "llvm/Support/Casting.h" 34 #include "llvm/Support/CommandLine.h" 35 #include "llvm/Support/Debug.h" 36 #include "llvm/Support/FileSystem.h" 37 #include "llvm/Support/Format.h" 38 #include "llvm/Support/GraphWriter.h" 39 #include "llvm/Support/Host.h" 40 #include "llvm/Support/ManagedStatic.h" 41 #include "llvm/Support/MemoryBuffer.h" 42 #include "llvm/Support/MemoryObject.h" 43 #include "llvm/Support/PrettyStackTrace.h" 44 #include "llvm/Support/Signals.h" 45 #include "llvm/Support/SourceMgr.h" 46 #include "llvm/Support/TargetRegistry.h" 47 #include "llvm/Support/TargetSelect.h" 48 #include "llvm/Support/raw_ostream.h" 49 #include "llvm/Support/system_error.h" 50 #include <algorithm> 51 #include <cctype> 52 #include <cstring> 53 using namespace llvm; 54 using namespace object; 55 56 static cl::list<std::string> 57 InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore); 58 59 static cl::opt<bool> 60 Disassemble("disassemble", 61 cl::desc("Display assembler mnemonics for the machine instructions")); 62 static cl::alias 63 Disassembled("d", cl::desc("Alias for --disassemble"), 64 cl::aliasopt(Disassemble)); 65 66 static cl::opt<bool> 67 Relocations("r", cl::desc("Display the relocation entries in the file")); 68 69 static cl::opt<bool> 70 SectionContents("s", cl::desc("Display the content of each section")); 71 72 static cl::opt<bool> 73 SymbolTable("t", cl::desc("Display the symbol table")); 74 75 static cl::opt<bool> 76 MachOOpt("macho", cl::desc("Use MachO specific object file parser")); 77 static cl::alias 78 MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachOOpt)); 79 80 cl::opt<std::string> 81 llvm::TripleName("triple", cl::desc("Target triple to disassemble for, " 82 "see -version for available targets")); 83 84 cl::opt<std::string> 85 llvm::ArchName("arch", cl::desc("Target arch to disassemble for, " 86 "see -version for available targets")); 87 88 static cl::opt<bool> 89 SectionHeaders("section-headers", cl::desc("Display summaries of the headers " 90 "for each section.")); 91 static cl::alias 92 SectionHeadersShort("headers", cl::desc("Alias for --section-headers"), 93 cl::aliasopt(SectionHeaders)); 94 static cl::alias 95 SectionHeadersShorter("h", cl::desc("Alias for --section-headers"), 96 cl::aliasopt(SectionHeaders)); 97 98 static cl::list<std::string> 99 MAttrs("mattr", 100 cl::CommaSeparated, 101 cl::desc("Target specific attributes"), 102 cl::value_desc("a1,+a2,-a3,...")); 103 104 static cl::opt<bool> 105 NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling instructions, " 106 "do not print the instruction bytes.")); 107 108 static cl::opt<bool> 109 UnwindInfo("unwind-info", cl::desc("Display unwind information")); 110 111 static cl::alias 112 UnwindInfoShort("u", cl::desc("Alias for --unwind-info"), 113 cl::aliasopt(UnwindInfo)); 114 115 static cl::opt<bool> 116 PrivateHeaders("private-headers", 117 cl::desc("Display format specific file headers")); 118 119 static cl::alias 120 PrivateHeadersShort("p", cl::desc("Alias for --private-headers"), 121 cl::aliasopt(PrivateHeaders)); 122 123 static StringRef ToolName; 124 125 bool llvm::error(error_code ec) { 126 if (!ec) return false; 127 128 outs() << ToolName << ": error reading file: " << ec.message() << ".\n"; 129 outs().flush(); 130 return true; 131 } 132 133 static const Target *getTarget(const ObjectFile *Obj = NULL) { 134 // Figure out the target triple. 135 llvm::Triple TheTriple("unknown-unknown-unknown"); 136 if (TripleName.empty()) { 137 if (Obj) 138 TheTriple.setArch(Triple::ArchType(Obj->getArch())); 139 } else 140 TheTriple.setTriple(Triple::normalize(TripleName)); 141 142 // Get the target specific parser. 143 std::string Error; 144 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple, 145 Error); 146 if (!TheTarget) { 147 errs() << ToolName << ": " << Error; 148 return 0; 149 } 150 151 // Update the triple name and return the found target. 152 TripleName = TheTriple.getTriple(); 153 return TheTarget; 154 } 155 156 void llvm::StringRefMemoryObject::anchor() { } 157 158 void llvm::DumpBytes(StringRef bytes) { 159 static const char hex_rep[] = "0123456789abcdef"; 160 // FIXME: The real way to do this is to figure out the longest instruction 161 // and align to that size before printing. I'll fix this when I get 162 // around to outputting relocations. 163 // 15 is the longest x86 instruction 164 // 3 is for the hex rep of a byte + a space. 165 // 1 is for the null terminator. 166 enum { OutputSize = (15 * 3) + 1 }; 167 char output[OutputSize]; 168 169 assert(bytes.size() <= 15 170 && "DumpBytes only supports instructions of up to 15 bytes"); 171 memset(output, ' ', sizeof(output)); 172 unsigned index = 0; 173 for (StringRef::iterator i = bytes.begin(), 174 e = bytes.end(); i != e; ++i) { 175 output[index] = hex_rep[(*i & 0xF0) >> 4]; 176 output[index + 1] = hex_rep[*i & 0xF]; 177 index += 3; 178 } 179 180 output[sizeof(output) - 1] = 0; 181 outs() << output; 182 } 183 184 bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) { 185 uint64_t a_addr, b_addr; 186 if (error(a.getAddress(a_addr))) return false; 187 if (error(b.getAddress(b_addr))) return false; 188 return a_addr < b_addr; 189 } 190 191 static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) { 192 const Target *TheTarget = getTarget(Obj); 193 // getTarget() will have already issued a diagnostic if necessary, so 194 // just bail here if it failed. 195 if (!TheTarget) 196 return; 197 198 // Package up features to be passed to target/subtarget 199 std::string FeaturesStr; 200 if (MAttrs.size()) { 201 SubtargetFeatures Features; 202 for (unsigned i = 0; i != MAttrs.size(); ++i) 203 Features.AddFeature(MAttrs[i]); 204 FeaturesStr = Features.getString(); 205 } 206 207 error_code ec; 208 for (section_iterator i = Obj->begin_sections(), 209 e = Obj->end_sections(); 210 i != e; i.increment(ec)) { 211 if (error(ec)) break; 212 bool text; 213 if (error(i->isText(text))) break; 214 if (!text) continue; 215 216 uint64_t SectionAddr; 217 if (error(i->getAddress(SectionAddr))) break; 218 219 // Make a list of all the symbols in this section. 220 std::vector<std::pair<uint64_t, StringRef> > Symbols; 221 for (symbol_iterator si = Obj->begin_symbols(), 222 se = Obj->end_symbols(); 223 si != se; si.increment(ec)) { 224 bool contains; 225 if (!error(i->containsSymbol(*si, contains)) && contains) { 226 uint64_t Address; 227 if (error(si->getAddress(Address))) break; 228 Address -= SectionAddr; 229 230 StringRef Name; 231 if (error(si->getName(Name))) break; 232 Symbols.push_back(std::make_pair(Address, Name)); 233 } 234 } 235 236 // Sort the symbols by address, just in case they didn't come in that way. 237 array_pod_sort(Symbols.begin(), Symbols.end()); 238 239 // Make a list of all the relocations for this section. 240 std::vector<RelocationRef> Rels; 241 if (InlineRelocs) { 242 for (relocation_iterator ri = i->begin_relocations(), 243 re = i->end_relocations(); 244 ri != re; ri.increment(ec)) { 245 if (error(ec)) break; 246 Rels.push_back(*ri); 247 } 248 } 249 250 // Sort relocations by address. 251 std::sort(Rels.begin(), Rels.end(), RelocAddressLess); 252 253 StringRef SegmentName = ""; 254 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) { 255 DataRefImpl DR = i->getRawDataRefImpl(); 256 if (error(MachO->getSectionFinalSegmentName(DR, SegmentName))) 257 break; 258 } 259 StringRef name; 260 if (error(i->getName(name))) break; 261 outs() << "Disassembly of section "; 262 if (!SegmentName.empty()) 263 outs() << SegmentName << ","; 264 outs() << name << ':'; 265 266 // If the section has no symbols just insert a dummy one and disassemble 267 // the whole section. 268 if (Symbols.empty()) 269 Symbols.push_back(std::make_pair(0, name)); 270 271 // Set up disassembler. 272 OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createMCAsmInfo(TripleName)); 273 274 if (!AsmInfo) { 275 errs() << "error: no assembly info for target " << TripleName << "\n"; 276 return; 277 } 278 279 OwningPtr<const MCSubtargetInfo> STI( 280 TheTarget->createMCSubtargetInfo(TripleName, "", FeaturesStr)); 281 282 if (!STI) { 283 errs() << "error: no subtarget info for target " << TripleName << "\n"; 284 return; 285 } 286 287 OwningPtr<const MCDisassembler> DisAsm( 288 TheTarget->createMCDisassembler(*STI)); 289 if (!DisAsm) { 290 errs() << "error: no disassembler for target " << TripleName << "\n"; 291 return; 292 } 293 294 OwningPtr<const MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName)); 295 if (!MRI) { 296 errs() << "error: no register info for target " << TripleName << "\n"; 297 return; 298 } 299 300 OwningPtr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo()); 301 if (!MII) { 302 errs() << "error: no instruction info for target " << TripleName << "\n"; 303 return; 304 } 305 306 int AsmPrinterVariant = AsmInfo->getAssemblerDialect(); 307 OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter( 308 AsmPrinterVariant, *AsmInfo, *MII, *MRI, *STI)); 309 if (!IP) { 310 errs() << "error: no instruction printer for target " << TripleName 311 << '\n'; 312 return; 313 } 314 315 StringRef Bytes; 316 if (error(i->getContents(Bytes))) break; 317 StringRefMemoryObject memoryObject(Bytes); 318 uint64_t Size; 319 uint64_t Index; 320 uint64_t SectSize; 321 if (error(i->getSize(SectSize))) break; 322 323 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin(); 324 std::vector<RelocationRef>::const_iterator rel_end = Rels.end(); 325 // Disassemble symbol by symbol. 326 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) { 327 uint64_t Start = Symbols[si].first; 328 uint64_t End; 329 // The end is either the size of the section or the beginning of the next 330 // symbol. 331 if (si == se - 1) 332 End = SectSize; 333 // Make sure this symbol takes up space. 334 else if (Symbols[si + 1].first != Start) 335 End = Symbols[si + 1].first - 1; 336 else 337 // This symbol has the same address as the next symbol. Skip it. 338 continue; 339 340 outs() << '\n' << Symbols[si].second << ":\n"; 341 342 #ifndef NDEBUG 343 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls(); 344 #else 345 raw_ostream &DebugOut = nulls(); 346 #endif 347 348 for (Index = Start; Index < End; Index += Size) { 349 MCInst Inst; 350 351 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index, 352 DebugOut, nulls())) { 353 outs() << format("%8" PRIx64 ":", SectionAddr + Index); 354 if (!NoShowRawInsn) { 355 outs() << "\t"; 356 DumpBytes(StringRef(Bytes.data() + Index, Size)); 357 } 358 IP->printInst(&Inst, outs(), ""); 359 outs() << "\n"; 360 } else { 361 errs() << ToolName << ": warning: invalid instruction encoding\n"; 362 if (Size == 0) 363 Size = 1; // skip illegible bytes 364 } 365 366 // Print relocation for instruction. 367 while (rel_cur != rel_end) { 368 bool hidden = false; 369 uint64_t addr; 370 SmallString<16> name; 371 SmallString<32> val; 372 373 // If this relocation is hidden, skip it. 374 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel; 375 if (hidden) goto skip_print_rel; 376 377 if (error(rel_cur->getAddress(addr))) goto skip_print_rel; 378 // Stop when rel_cur's address is past the current instruction. 379 if (addr >= Index + Size) break; 380 if (error(rel_cur->getTypeName(name))) goto skip_print_rel; 381 if (error(rel_cur->getValueString(val))) goto skip_print_rel; 382 383 outs() << format("\t\t\t%8" PRIx64 ": ", SectionAddr + addr) << name 384 << "\t" << val << "\n"; 385 386 skip_print_rel: 387 ++rel_cur; 388 } 389 } 390 } 391 } 392 } 393 394 static void PrintRelocations(const ObjectFile *o) { 395 error_code ec; 396 for (section_iterator si = o->begin_sections(), se = o->end_sections(); 397 si != se; si.increment(ec)){ 398 if (error(ec)) return; 399 if (si->begin_relocations() == si->end_relocations()) 400 continue; 401 StringRef secname; 402 if (error(si->getName(secname))) continue; 403 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n"; 404 for (relocation_iterator ri = si->begin_relocations(), 405 re = si->end_relocations(); 406 ri != re; ri.increment(ec)) { 407 if (error(ec)) return; 408 409 bool hidden; 410 uint64_t address; 411 SmallString<32> relocname; 412 SmallString<32> valuestr; 413 if (error(ri->getHidden(hidden))) continue; 414 if (hidden) continue; 415 if (error(ri->getTypeName(relocname))) continue; 416 if (error(ri->getAddress(address))) continue; 417 if (error(ri->getValueString(valuestr))) continue; 418 outs() << address << " " << relocname << " " << valuestr << "\n"; 419 } 420 outs() << "\n"; 421 } 422 } 423 424 static void PrintSectionHeaders(const ObjectFile *o) { 425 outs() << "Sections:\n" 426 "Idx Name Size Address Type\n"; 427 error_code ec; 428 unsigned i = 0; 429 for (section_iterator si = o->begin_sections(), se = o->end_sections(); 430 si != se; si.increment(ec)) { 431 if (error(ec)) return; 432 StringRef Name; 433 if (error(si->getName(Name))) return; 434 uint64_t Address; 435 if (error(si->getAddress(Address))) return; 436 uint64_t Size; 437 if (error(si->getSize(Size))) return; 438 bool Text, Data, BSS; 439 if (error(si->isText(Text))) return; 440 if (error(si->isData(Data))) return; 441 if (error(si->isBSS(BSS))) return; 442 std::string Type = (std::string(Text ? "TEXT " : "") + 443 (Data ? "DATA " : "") + (BSS ? "BSS" : "")); 444 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", 445 i, Name.str().c_str(), Size, Address, Type.c_str()); 446 ++i; 447 } 448 } 449 450 static void PrintSectionContents(const ObjectFile *o) { 451 error_code ec; 452 for (section_iterator si = o->begin_sections(), 453 se = o->end_sections(); 454 si != se; si.increment(ec)) { 455 if (error(ec)) return; 456 StringRef Name; 457 StringRef Contents; 458 uint64_t BaseAddr; 459 if (error(si->getName(Name))) continue; 460 if (error(si->getContents(Contents))) continue; 461 if (error(si->getAddress(BaseAddr))) continue; 462 463 outs() << "Contents of section " << Name << ":\n"; 464 465 // Dump out the content as hex and printable ascii characters. 466 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) { 467 outs() << format(" %04" PRIx64 " ", BaseAddr + addr); 468 // Dump line of hex. 469 for (std::size_t i = 0; i < 16; ++i) { 470 if (i != 0 && i % 4 == 0) 471 outs() << ' '; 472 if (addr + i < end) 473 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true) 474 << hexdigit(Contents[addr + i] & 0xF, true); 475 else 476 outs() << " "; 477 } 478 // Print ascii. 479 outs() << " "; 480 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) { 481 if (std::isprint(Contents[addr + i] & 0xFF)) 482 outs() << Contents[addr + i]; 483 else 484 outs() << "."; 485 } 486 outs() << "\n"; 487 } 488 } 489 } 490 491 static void PrintCOFFSymbolTable(const COFFObjectFile *coff) { 492 const coff_file_header *header; 493 if (error(coff->getHeader(header))) return; 494 int aux_count = 0; 495 const coff_symbol *symbol = 0; 496 for (int i = 0, e = header->NumberOfSymbols; i != e; ++i) { 497 if (aux_count--) { 498 // Figure out which type of aux this is. 499 if (symbol->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC 500 && symbol->Value == 0) { // Section definition. 501 const coff_aux_section_definition *asd; 502 if (error(coff->getAuxSymbol<coff_aux_section_definition>(i, asd))) 503 return; 504 outs() << "AUX " 505 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x " 506 , unsigned(asd->Length) 507 , unsigned(asd->NumberOfRelocations) 508 , unsigned(asd->NumberOfLinenumbers) 509 , unsigned(asd->CheckSum)) 510 << format("assoc %d comdat %d\n" 511 , unsigned(asd->Number) 512 , unsigned(asd->Selection)); 513 } else 514 outs() << "AUX Unknown\n"; 515 } else { 516 StringRef name; 517 if (error(coff->getSymbol(i, symbol))) return; 518 if (error(coff->getSymbolName(symbol, name))) return; 519 outs() << "[" << format("%2d", i) << "]" 520 << "(sec " << format("%2d", int(symbol->SectionNumber)) << ")" 521 << "(fl 0x00)" // Flag bits, which COFF doesn't have. 522 << "(ty " << format("%3x", unsigned(symbol->Type)) << ")" 523 << "(scl " << format("%3x", unsigned(symbol->StorageClass)) << ") " 524 << "(nx " << unsigned(symbol->NumberOfAuxSymbols) << ") " 525 << "0x" << format("%08x", unsigned(symbol->Value)) << " " 526 << name << "\n"; 527 aux_count = symbol->NumberOfAuxSymbols; 528 } 529 } 530 } 531 532 static void PrintSymbolTable(const ObjectFile *o) { 533 outs() << "SYMBOL TABLE:\n"; 534 535 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) 536 PrintCOFFSymbolTable(coff); 537 else { 538 error_code ec; 539 for (symbol_iterator si = o->begin_symbols(), 540 se = o->end_symbols(); si != se; si.increment(ec)) { 541 if (error(ec)) return; 542 StringRef Name; 543 uint64_t Address; 544 SymbolRef::Type Type; 545 uint64_t Size; 546 uint32_t Flags; 547 section_iterator Section = o->end_sections(); 548 if (error(si->getName(Name))) continue; 549 if (error(si->getAddress(Address))) continue; 550 if (error(si->getFlags(Flags))) continue; 551 if (error(si->getType(Type))) continue; 552 if (error(si->getSize(Size))) continue; 553 if (error(si->getSection(Section))) continue; 554 555 bool Global = Flags & SymbolRef::SF_Global; 556 bool Weak = Flags & SymbolRef::SF_Weak; 557 bool Absolute = Flags & SymbolRef::SF_Absolute; 558 559 if (Address == UnknownAddressOrSize) 560 Address = 0; 561 if (Size == UnknownAddressOrSize) 562 Size = 0; 563 char GlobLoc = ' '; 564 if (Type != SymbolRef::ST_Unknown) 565 GlobLoc = Global ? 'g' : 'l'; 566 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File) 567 ? 'd' : ' '; 568 char FileFunc = ' '; 569 if (Type == SymbolRef::ST_File) 570 FileFunc = 'f'; 571 else if (Type == SymbolRef::ST_Function) 572 FileFunc = 'F'; 573 574 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 : 575 "%08" PRIx64; 576 577 outs() << format(Fmt, Address) << " " 578 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' ' 579 << (Weak ? 'w' : ' ') // Weak? 580 << ' ' // Constructor. Not supported yet. 581 << ' ' // Warning. Not supported yet. 582 << ' ' // Indirect reference to another symbol. 583 << Debug // Debugging (d) or dynamic (D) symbol. 584 << FileFunc // Name of function (F), file (f) or object (O). 585 << ' '; 586 if (Absolute) 587 outs() << "*ABS*"; 588 else if (Section == o->end_sections()) 589 outs() << "*UND*"; 590 else { 591 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(o)) { 592 StringRef SegmentName; 593 DataRefImpl DR = Section->getRawDataRefImpl(); 594 if (error(MachO->getSectionFinalSegmentName(DR, SegmentName))) 595 SegmentName = ""; 596 outs() << SegmentName << ","; 597 } 598 StringRef SectionName; 599 if (error(Section->getName(SectionName))) 600 SectionName = ""; 601 outs() << SectionName; 602 } 603 outs() << '\t' 604 << format("%08" PRIx64 " ", Size) 605 << Name 606 << '\n'; 607 } 608 } 609 } 610 611 static void PrintUnwindInfo(const ObjectFile *o) { 612 outs() << "Unwind info:\n\n"; 613 614 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) { 615 printCOFFUnwindInfo(coff); 616 } else { 617 // TODO: Extract DWARF dump tool to objdump. 618 errs() << "This operation is only currently supported " 619 "for COFF object files.\n"; 620 return; 621 } 622 } 623 624 static void DumpObject(const ObjectFile *o) { 625 outs() << '\n'; 626 outs() << o->getFileName() 627 << ":\tfile format " << o->getFileFormatName() << "\n\n"; 628 629 if (Disassemble) 630 DisassembleObject(o, Relocations); 631 if (Relocations && !Disassemble) 632 PrintRelocations(o); 633 if (SectionHeaders) 634 PrintSectionHeaders(o); 635 if (SectionContents) 636 PrintSectionContents(o); 637 if (SymbolTable) 638 PrintSymbolTable(o); 639 if (UnwindInfo) 640 PrintUnwindInfo(o); 641 if (PrivateHeaders && o->isELF()) 642 printELFFileHeader(o); 643 } 644 645 /// @brief Dump each object file in \a a; 646 static void DumpArchive(const Archive *a) { 647 for (Archive::child_iterator i = a->begin_children(), 648 e = a->end_children(); i != e; ++i) { 649 OwningPtr<Binary> child; 650 if (error_code ec = i->getAsBinary(child)) { 651 // Ignore non-object files. 652 if (ec != object_error::invalid_file_type) 653 errs() << ToolName << ": '" << a->getFileName() << "': " << ec.message() 654 << ".\n"; 655 continue; 656 } 657 if (ObjectFile *o = dyn_cast<ObjectFile>(child.get())) 658 DumpObject(o); 659 else 660 errs() << ToolName << ": '" << a->getFileName() << "': " 661 << "Unrecognized file type.\n"; 662 } 663 } 664 665 /// @brief Open file and figure out how to dump it. 666 static void DumpInput(StringRef file) { 667 // If file isn't stdin, check that it exists. 668 if (file != "-" && !sys::fs::exists(file)) { 669 errs() << ToolName << ": '" << file << "': " << "No such file\n"; 670 return; 671 } 672 673 if (MachOOpt && Disassemble) { 674 DisassembleInputMachO(file); 675 return; 676 } 677 678 // Attempt to open the binary. 679 OwningPtr<Binary> binary; 680 if (error_code ec = createBinary(file, binary)) { 681 errs() << ToolName << ": '" << file << "': " << ec.message() << ".\n"; 682 return; 683 } 684 685 if (Archive *a = dyn_cast<Archive>(binary.get())) 686 DumpArchive(a); 687 else if (ObjectFile *o = dyn_cast<ObjectFile>(binary.get())) 688 DumpObject(o); 689 else 690 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n"; 691 } 692 693 int main(int argc, char **argv) { 694 // Print a stack trace if we signal out. 695 sys::PrintStackTraceOnErrorSignal(); 696 PrettyStackTraceProgram X(argc, argv); 697 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 698 699 // Initialize targets and assembly printers/parsers. 700 llvm::InitializeAllTargetInfos(); 701 llvm::InitializeAllTargetMCs(); 702 llvm::InitializeAllAsmParsers(); 703 llvm::InitializeAllDisassemblers(); 704 705 // Register the target printer for --version. 706 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); 707 708 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n"); 709 TripleName = Triple::normalize(TripleName); 710 711 ToolName = argv[0]; 712 713 // Defaults to a.out if no filenames specified. 714 if (InputFilenames.size() == 0) 715 InputFilenames.push_back("a.out"); 716 717 if (!Disassemble 718 && !Relocations 719 && !SectionHeaders 720 && !SectionContents 721 && !SymbolTable 722 && !UnwindInfo 723 && !PrivateHeaders) { 724 cl::PrintHelpMessage(); 725 return 2; 726 } 727 728 std::for_each(InputFilenames.begin(), InputFilenames.end(), 729 DumpInput); 730 731 return 0; 732 } 733