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