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 // The flags and output of this program should be near identical to those of 15 // binutils objdump. 16 // 17 //===----------------------------------------------------------------------===// 18 19 #include "llvm-objdump.h" 20 #include "llvm/ADT/OwningPtr.h" 21 #include "llvm/ADT/STLExtras.h" 22 #include "llvm/ADT/StringExtras.h" 23 #include "llvm/ADT/Triple.h" 24 #include "llvm/MC/MCAsmInfo.h" 25 #include "llvm/MC/MCAtom.h" 26 #include "llvm/MC/MCContext.h" 27 #include "llvm/MC/MCDisassembler.h" 28 #include "llvm/MC/MCFunction.h" 29 #include "llvm/MC/MCInst.h" 30 #include "llvm/MC/MCInstPrinter.h" 31 #include "llvm/MC/MCInstrAnalysis.h" 32 #include "llvm/MC/MCInstrInfo.h" 33 #include "llvm/MC/MCModule.h" 34 #include "llvm/MC/MCModuleYAML.h" 35 #include "llvm/MC/MCObjectDisassembler.h" 36 #include "llvm/MC/MCObjectFileInfo.h" 37 #include "llvm/MC/MCObjectSymbolizer.h" 38 #include "llvm/MC/MCRegisterInfo.h" 39 #include "llvm/MC/MCRelocationInfo.h" 40 #include "llvm/MC/MCSubtargetInfo.h" 41 #include "llvm/Object/Archive.h" 42 #include "llvm/Object/COFF.h" 43 #include "llvm/Object/MachO.h" 44 #include "llvm/Object/ObjectFile.h" 45 #include "llvm/Support/Casting.h" 46 #include "llvm/Support/CommandLine.h" 47 #include "llvm/Support/Debug.h" 48 #include "llvm/Support/FileSystem.h" 49 #include "llvm/Support/Format.h" 50 #include "llvm/Support/GraphWriter.h" 51 #include "llvm/Support/Host.h" 52 #include "llvm/Support/ManagedStatic.h" 53 #include "llvm/Support/MemoryBuffer.h" 54 #include "llvm/Support/MemoryObject.h" 55 #include "llvm/Support/PrettyStackTrace.h" 56 #include "llvm/Support/Signals.h" 57 #include "llvm/Support/SourceMgr.h" 58 #include "llvm/Support/TargetRegistry.h" 59 #include "llvm/Support/TargetSelect.h" 60 #include "llvm/Support/raw_ostream.h" 61 #include "llvm/Support/system_error.h" 62 #include <algorithm> 63 #include <cctype> 64 #include <cstring> 65 66 using namespace llvm; 67 using namespace object; 68 69 static cl::list<std::string> 70 InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore); 71 72 static cl::opt<bool> 73 Disassemble("disassemble", 74 cl::desc("Display assembler mnemonics for the machine instructions")); 75 static cl::alias 76 Disassembled("d", cl::desc("Alias for --disassemble"), 77 cl::aliasopt(Disassemble)); 78 79 static cl::opt<bool> 80 Relocations("r", cl::desc("Display the relocation entries in the file")); 81 82 static cl::opt<bool> 83 SectionContents("s", cl::desc("Display the content of each section")); 84 85 static cl::opt<bool> 86 SymbolTable("t", cl::desc("Display the symbol table")); 87 88 static cl::opt<bool> 89 MachOOpt("macho", cl::desc("Use MachO specific object file parser")); 90 static cl::alias 91 MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachOOpt)); 92 93 cl::opt<std::string> 94 llvm::TripleName("triple", cl::desc("Target triple to disassemble for, " 95 "see -version for available targets")); 96 97 cl::opt<std::string> 98 llvm::ArchName("arch", cl::desc("Target arch to disassemble for, " 99 "see -version for available targets")); 100 101 static cl::opt<bool> 102 SectionHeaders("section-headers", cl::desc("Display summaries of the headers " 103 "for each section.")); 104 static cl::alias 105 SectionHeadersShort("headers", cl::desc("Alias for --section-headers"), 106 cl::aliasopt(SectionHeaders)); 107 static cl::alias 108 SectionHeadersShorter("h", cl::desc("Alias for --section-headers"), 109 cl::aliasopt(SectionHeaders)); 110 111 static cl::list<std::string> 112 MAttrs("mattr", 113 cl::CommaSeparated, 114 cl::desc("Target specific attributes"), 115 cl::value_desc("a1,+a2,-a3,...")); 116 117 static cl::opt<bool> 118 NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling instructions, " 119 "do not print the instruction bytes.")); 120 121 static cl::opt<bool> 122 UnwindInfo("unwind-info", cl::desc("Display unwind information")); 123 124 static cl::alias 125 UnwindInfoShort("u", cl::desc("Alias for --unwind-info"), 126 cl::aliasopt(UnwindInfo)); 127 128 static cl::opt<bool> 129 PrivateHeaders("private-headers", 130 cl::desc("Display format specific file headers")); 131 132 static cl::alias 133 PrivateHeadersShort("p", cl::desc("Alias for --private-headers"), 134 cl::aliasopt(PrivateHeaders)); 135 136 static cl::opt<bool> 137 Symbolize("symbolize", cl::desc("When disassembling instructions, " 138 "try to symbolize operands.")); 139 140 static cl::opt<bool> 141 CFG("cfg", cl::desc("Create a CFG for every function found in the object" 142 " and write it to a graphviz file")); 143 144 // FIXME: Does it make sense to have a dedicated tool for yaml cfg output? 145 static cl::opt<std::string> 146 YAMLCFG("yaml-cfg", 147 cl::desc("Create a CFG and write it as a YAML MCModule."), 148 cl::value_desc("yaml output file")); 149 150 static StringRef ToolName; 151 152 bool llvm::error(error_code EC) { 153 if (!EC) 154 return false; 155 156 outs() << ToolName << ": error reading file: " << EC.message() << ".\n"; 157 outs().flush(); 158 return true; 159 } 160 161 static const Target *getTarget(const ObjectFile *Obj = NULL) { 162 // Figure out the target triple. 163 llvm::Triple TheTriple("unknown-unknown-unknown"); 164 if (TripleName.empty()) { 165 if (Obj) { 166 TheTriple.setArch(Triple::ArchType(Obj->getArch())); 167 // TheTriple defaults to ELF, and COFF doesn't have an environment: 168 // the best we can do here is indicate that it is mach-o. 169 if (Obj->isMachO()) 170 TheTriple.setEnvironment(Triple::MachO); 171 } 172 } else 173 TheTriple.setTriple(Triple::normalize(TripleName)); 174 175 // Get the target specific parser. 176 std::string Error; 177 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple, 178 Error); 179 if (!TheTarget) { 180 errs() << ToolName << ": " << Error; 181 return 0; 182 } 183 184 // Update the triple name and return the found target. 185 TripleName = TheTriple.getTriple(); 186 return TheTarget; 187 } 188 189 // Write a graphviz file for the CFG inside an MCFunction. 190 // FIXME: Use GraphWriter 191 static void emitDOTFile(const char *FileName, const MCFunction &f, 192 MCInstPrinter *IP) { 193 // Start a new dot file. 194 std::string Error; 195 raw_fd_ostream Out(FileName, Error); 196 if (!Error.empty()) { 197 errs() << "llvm-objdump: warning: " << Error << '\n'; 198 return; 199 } 200 201 Out << "digraph \"" << f.getName() << "\" {\n"; 202 Out << "graph [ rankdir = \"LR\" ];\n"; 203 for (MCFunction::const_iterator i = f.begin(), e = f.end(); i != e; ++i) { 204 // Only print blocks that have predecessors. 205 bool hasPreds = (*i)->pred_begin() != (*i)->pred_end(); 206 207 if (!hasPreds && i != f.begin()) 208 continue; 209 210 Out << '"' << (*i)->getInsts()->getBeginAddr() << "\" [ label=\"<a>"; 211 // Print instructions. 212 for (unsigned ii = 0, ie = (*i)->getInsts()->size(); ii != ie; 213 ++ii) { 214 if (ii != 0) // Not the first line, start a new row. 215 Out << '|'; 216 if (ii + 1 == ie) // Last line, add an end id. 217 Out << "<o>"; 218 219 // Escape special chars and print the instruction in mnemonic form. 220 std::string Str; 221 raw_string_ostream OS(Str); 222 IP->printInst(&(*i)->getInsts()->at(ii).Inst, OS, ""); 223 Out << DOT::EscapeString(OS.str()); 224 } 225 Out << "\" shape=\"record\" ];\n"; 226 227 // Add edges. 228 for (MCBasicBlock::succ_const_iterator si = (*i)->succ_begin(), 229 se = (*i)->succ_end(); si != se; ++si) 230 Out << (*i)->getInsts()->getBeginAddr() << ":o -> " 231 << (*si)->getInsts()->getBeginAddr() << ":a\n"; 232 } 233 Out << "}\n"; 234 } 235 236 void llvm::DumpBytes(StringRef bytes) { 237 static const char hex_rep[] = "0123456789abcdef"; 238 // FIXME: The real way to do this is to figure out the longest instruction 239 // and align to that size before printing. I'll fix this when I get 240 // around to outputting relocations. 241 // 15 is the longest x86 instruction 242 // 3 is for the hex rep of a byte + a space. 243 // 1 is for the null terminator. 244 enum { OutputSize = (15 * 3) + 1 }; 245 char output[OutputSize]; 246 247 assert(bytes.size() <= 15 248 && "DumpBytes only supports instructions of up to 15 bytes"); 249 memset(output, ' ', sizeof(output)); 250 unsigned index = 0; 251 for (StringRef::iterator i = bytes.begin(), 252 e = bytes.end(); i != e; ++i) { 253 output[index] = hex_rep[(*i & 0xF0) >> 4]; 254 output[index + 1] = hex_rep[*i & 0xF]; 255 index += 3; 256 } 257 258 output[sizeof(output) - 1] = 0; 259 outs() << output; 260 } 261 262 bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) { 263 uint64_t a_addr, b_addr; 264 if (error(a.getOffset(a_addr))) return false; 265 if (error(b.getOffset(b_addr))) return false; 266 return a_addr < b_addr; 267 } 268 269 static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) { 270 const Target *TheTarget = getTarget(Obj); 271 // getTarget() will have already issued a diagnostic if necessary, so 272 // just bail here if it failed. 273 if (!TheTarget) 274 return; 275 276 // Package up features to be passed to target/subtarget 277 std::string FeaturesStr; 278 if (MAttrs.size()) { 279 SubtargetFeatures Features; 280 for (unsigned i = 0; i != MAttrs.size(); ++i) 281 Features.AddFeature(MAttrs[i]); 282 FeaturesStr = Features.getString(); 283 } 284 285 OwningPtr<const MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName)); 286 if (!MRI) { 287 errs() << "error: no register info for target " << TripleName << "\n"; 288 return; 289 } 290 291 // Set up disassembler. 292 OwningPtr<const MCAsmInfo> AsmInfo( 293 TheTarget->createMCAsmInfo(*MRI, TripleName)); 294 if (!AsmInfo) { 295 errs() << "error: no assembly info for target " << TripleName << "\n"; 296 return; 297 } 298 299 OwningPtr<const MCSubtargetInfo> STI( 300 TheTarget->createMCSubtargetInfo(TripleName, "", FeaturesStr)); 301 if (!STI) { 302 errs() << "error: no subtarget info for target " << TripleName << "\n"; 303 return; 304 } 305 306 OwningPtr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo()); 307 if (!MII) { 308 errs() << "error: no instruction info for target " << TripleName << "\n"; 309 return; 310 } 311 312 OwningPtr<MCDisassembler> DisAsm(TheTarget->createMCDisassembler(*STI)); 313 if (!DisAsm) { 314 errs() << "error: no disassembler for target " << TripleName << "\n"; 315 return; 316 } 317 318 OwningPtr<const MCObjectFileInfo> MOFI; 319 OwningPtr<MCContext> Ctx; 320 321 if (Symbolize) { 322 MOFI.reset(new MCObjectFileInfo); 323 Ctx.reset(new MCContext(AsmInfo.get(), MRI.get(), MOFI.get())); 324 OwningPtr<MCRelocationInfo> RelInfo( 325 TheTarget->createMCRelocationInfo(TripleName, *Ctx.get())); 326 if (RelInfo) { 327 OwningPtr<MCSymbolizer> Symzer( 328 MCObjectSymbolizer::createObjectSymbolizer(*Ctx.get(), RelInfo, Obj)); 329 if (Symzer) 330 DisAsm->setSymbolizer(Symzer); 331 } 332 } 333 334 OwningPtr<const MCInstrAnalysis> 335 MIA(TheTarget->createMCInstrAnalysis(MII.get())); 336 337 int AsmPrinterVariant = AsmInfo->getAssemblerDialect(); 338 OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter( 339 AsmPrinterVariant, *AsmInfo, *MII, *MRI, *STI)); 340 if (!IP) { 341 errs() << "error: no instruction printer for target " << TripleName 342 << '\n'; 343 return; 344 } 345 346 if (CFG || !YAMLCFG.empty()) { 347 OwningPtr<MCObjectDisassembler> OD( 348 new MCObjectDisassembler(*Obj, *DisAsm, *MIA)); 349 OwningPtr<MCModule> Mod(OD->buildModule(/* withCFG */ true)); 350 for (MCModule::const_atom_iterator AI = Mod->atom_begin(), 351 AE = Mod->atom_end(); 352 AI != AE; ++AI) { 353 outs() << "Atom " << (*AI)->getName() << ": \n"; 354 if (const MCTextAtom *TA = dyn_cast<MCTextAtom>(*AI)) { 355 for (MCTextAtom::const_iterator II = TA->begin(), IE = TA->end(); 356 II != IE; 357 ++II) { 358 IP->printInst(&II->Inst, outs(), ""); 359 outs() << "\n"; 360 } 361 } 362 } 363 if (CFG) { 364 for (MCModule::const_func_iterator FI = Mod->func_begin(), 365 FE = Mod->func_end(); 366 FI != FE; ++FI) { 367 static int filenum = 0; 368 emitDOTFile((Twine((*FI)->getName()) + "_" + 369 utostr(filenum) + ".dot").str().c_str(), 370 **FI, IP.get()); 371 ++filenum; 372 } 373 } 374 if (!YAMLCFG.empty()) { 375 std::string Error; 376 raw_fd_ostream YAMLOut(YAMLCFG.c_str(), Error); 377 if (!Error.empty()) { 378 errs() << ToolName << ": warning: " << Error << '\n'; 379 return; 380 } 381 mcmodule2yaml(YAMLOut, *Mod, *MII, *MRI); 382 } 383 } 384 385 error_code EC; 386 for (section_iterator I = Obj->begin_sections(), E = Obj->end_sections(); 387 I != E; I.increment(EC)) { 388 if (error(EC)) 389 break; 390 bool Text; 391 if (error(I->isText(Text))) 392 break; 393 if (!Text) 394 continue; 395 396 uint64_t SectionAddr; 397 if (error(I->getAddress(SectionAddr))) 398 break; 399 400 // Make a list of all the symbols in this section. 401 std::vector<std::pair<uint64_t, StringRef> > Symbols; 402 for (symbol_iterator SI = Obj->begin_symbols(), SE = Obj->end_symbols(); 403 SI != SE; SI.increment(EC)) { 404 bool contains; 405 if (!error(I->containsSymbol(*SI, contains)) && contains) { 406 uint64_t Address; 407 if (error(SI->getAddress(Address))) 408 break; 409 if (Address == UnknownAddressOrSize) 410 continue; 411 Address -= SectionAddr; 412 413 StringRef Name; 414 if (error(SI->getName(Name))) 415 break; 416 Symbols.push_back(std::make_pair(Address, Name)); 417 } 418 } 419 420 // Sort the symbols by address, just in case they didn't come in that way. 421 array_pod_sort(Symbols.begin(), Symbols.end()); 422 423 // Make a list of all the relocations for this section. 424 std::vector<RelocationRef> Rels; 425 if (InlineRelocs) { 426 for (relocation_iterator RI = I->begin_relocations(), 427 RE = I->end_relocations(); 428 RI != RE; RI.increment(EC)) { 429 if (error(EC)) 430 break; 431 Rels.push_back(*RI); 432 } 433 } 434 435 // Sort relocations by address. 436 std::sort(Rels.begin(), Rels.end(), RelocAddressLess); 437 438 StringRef SegmentName = ""; 439 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) { 440 DataRefImpl DR = I->getRawDataRefImpl(); 441 SegmentName = MachO->getSectionFinalSegmentName(DR); 442 } 443 StringRef name; 444 if (error(I->getName(name))) 445 break; 446 outs() << "Disassembly of section "; 447 if (!SegmentName.empty()) 448 outs() << SegmentName << ","; 449 outs() << name << ':'; 450 451 // If the section has no symbols just insert a dummy one and disassemble 452 // the whole section. 453 if (Symbols.empty()) 454 Symbols.push_back(std::make_pair(0, name)); 455 456 457 SmallString<40> Comments; 458 raw_svector_ostream CommentStream(Comments); 459 460 StringRef Bytes; 461 if (error(I->getContents(Bytes))) 462 break; 463 StringRefMemoryObject memoryObject(Bytes, SectionAddr); 464 uint64_t Size; 465 uint64_t Index; 466 uint64_t SectSize; 467 if (error(I->getSize(SectSize))) 468 break; 469 470 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin(); 471 std::vector<RelocationRef>::const_iterator rel_end = Rels.end(); 472 // Disassemble symbol by symbol. 473 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) { 474 uint64_t Start = Symbols[si].first; 475 uint64_t End; 476 // The end is either the size of the section or the beginning of the next 477 // symbol. 478 if (si == se - 1) 479 End = SectSize; 480 // Make sure this symbol takes up space. 481 else if (Symbols[si + 1].first != Start) 482 End = Symbols[si + 1].first - 1; 483 else 484 // This symbol has the same address as the next symbol. Skip it. 485 continue; 486 487 outs() << '\n' << Symbols[si].second << ":\n"; 488 489 #ifndef NDEBUG 490 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls(); 491 #else 492 raw_ostream &DebugOut = nulls(); 493 #endif 494 495 for (Index = Start; Index < End; Index += Size) { 496 MCInst Inst; 497 498 if (DisAsm->getInstruction(Inst, Size, memoryObject, 499 SectionAddr + Index, 500 DebugOut, CommentStream)) { 501 outs() << format("%8" PRIx64 ":", SectionAddr + Index); 502 if (!NoShowRawInsn) { 503 outs() << "\t"; 504 DumpBytes(StringRef(Bytes.data() + Index, Size)); 505 } 506 IP->printInst(&Inst, outs(), ""); 507 outs() << CommentStream.str(); 508 Comments.clear(); 509 outs() << "\n"; 510 } else { 511 errs() << ToolName << ": warning: invalid instruction encoding\n"; 512 if (Size == 0) 513 Size = 1; // skip illegible bytes 514 } 515 516 // Print relocation for instruction. 517 while (rel_cur != rel_end) { 518 bool hidden = false; 519 uint64_t addr; 520 SmallString<16> name; 521 SmallString<32> val; 522 523 // If this relocation is hidden, skip it. 524 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel; 525 if (hidden) goto skip_print_rel; 526 527 if (error(rel_cur->getOffset(addr))) goto skip_print_rel; 528 // Stop when rel_cur's address is past the current instruction. 529 if (addr >= Index + Size) break; 530 if (error(rel_cur->getTypeName(name))) goto skip_print_rel; 531 if (error(rel_cur->getValueString(val))) goto skip_print_rel; 532 533 outs() << format("\t\t\t%8" PRIx64 ": ", SectionAddr + addr) << name 534 << "\t" << val << "\n"; 535 536 skip_print_rel: 537 ++rel_cur; 538 } 539 } 540 } 541 } 542 } 543 544 static void PrintRelocations(const ObjectFile *o) { 545 error_code EC; 546 for (section_iterator si = o->begin_sections(), se = o->end_sections(); 547 si != se; si.increment(EC)) { 548 if (error(EC)) 549 return; 550 if (si->begin_relocations() == si->end_relocations()) 551 continue; 552 StringRef secname; 553 if (error(si->getName(secname))) continue; 554 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n"; 555 for (relocation_iterator ri = si->begin_relocations(), 556 re = si->end_relocations(); 557 ri != re; ri.increment(EC)) { 558 if (error(EC)) 559 return; 560 561 bool hidden; 562 uint64_t address; 563 SmallString<32> relocname; 564 SmallString<32> valuestr; 565 if (error(ri->getHidden(hidden))) continue; 566 if (hidden) continue; 567 if (error(ri->getTypeName(relocname))) continue; 568 if (error(ri->getOffset(address))) continue; 569 if (error(ri->getValueString(valuestr))) continue; 570 outs() << address << " " << relocname << " " << valuestr << "\n"; 571 } 572 outs() << "\n"; 573 } 574 } 575 576 static void PrintSectionHeaders(const ObjectFile *o) { 577 outs() << "Sections:\n" 578 "Idx Name Size Address Type\n"; 579 error_code EC; 580 unsigned i = 0; 581 for (section_iterator si = o->begin_sections(), se = o->end_sections(); 582 si != se; si.increment(EC)) { 583 if (error(EC)) 584 return; 585 StringRef Name; 586 if (error(si->getName(Name))) 587 return; 588 uint64_t Address; 589 if (error(si->getAddress(Address))) return; 590 uint64_t Size; 591 if (error(si->getSize(Size))) return; 592 bool Text, Data, BSS; 593 if (error(si->isText(Text))) return; 594 if (error(si->isData(Data))) return; 595 if (error(si->isBSS(BSS))) return; 596 std::string Type = (std::string(Text ? "TEXT " : "") + 597 (Data ? "DATA " : "") + (BSS ? "BSS" : "")); 598 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", 599 i, Name.str().c_str(), Size, Address, Type.c_str()); 600 ++i; 601 } 602 } 603 604 static void PrintSectionContents(const ObjectFile *o) { 605 error_code EC; 606 for (section_iterator si = o->begin_sections(), se = o->end_sections(); 607 si != se; si.increment(EC)) { 608 if (error(EC)) 609 return; 610 StringRef Name; 611 StringRef Contents; 612 uint64_t BaseAddr; 613 bool BSS; 614 if (error(si->getName(Name))) continue; 615 if (error(si->getContents(Contents))) continue; 616 if (error(si->getAddress(BaseAddr))) continue; 617 if (error(si->isBSS(BSS))) continue; 618 619 outs() << "Contents of section " << Name << ":\n"; 620 if (BSS) { 621 outs() << format("<skipping contents of bss section at [%04" PRIx64 622 ", %04" PRIx64 ")>\n", BaseAddr, 623 BaseAddr + Contents.size()); 624 continue; 625 } 626 627 // Dump out the content as hex and printable ascii characters. 628 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) { 629 outs() << format(" %04" PRIx64 " ", BaseAddr + addr); 630 // Dump line of hex. 631 for (std::size_t i = 0; i < 16; ++i) { 632 if (i != 0 && i % 4 == 0) 633 outs() << ' '; 634 if (addr + i < end) 635 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true) 636 << hexdigit(Contents[addr + i] & 0xF, true); 637 else 638 outs() << " "; 639 } 640 // Print ascii. 641 outs() << " "; 642 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) { 643 if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF)) 644 outs() << Contents[addr + i]; 645 else 646 outs() << "."; 647 } 648 outs() << "\n"; 649 } 650 } 651 } 652 653 static void PrintCOFFSymbolTable(const COFFObjectFile *coff) { 654 const coff_file_header *header; 655 if (error(coff->getHeader(header))) return; 656 int aux_count = 0; 657 const coff_symbol *symbol = 0; 658 for (int i = 0, e = header->NumberOfSymbols; i != e; ++i) { 659 if (aux_count--) { 660 // Figure out which type of aux this is. 661 if (symbol->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC 662 && symbol->Value == 0) { // Section definition. 663 const coff_aux_section_definition *asd; 664 if (error(coff->getAuxSymbol<coff_aux_section_definition>(i, asd))) 665 return; 666 outs() << "AUX " 667 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x " 668 , unsigned(asd->Length) 669 , unsigned(asd->NumberOfRelocations) 670 , unsigned(asd->NumberOfLinenumbers) 671 , unsigned(asd->CheckSum)) 672 << format("assoc %d comdat %d\n" 673 , unsigned(asd->Number) 674 , unsigned(asd->Selection)); 675 } else 676 outs() << "AUX Unknown\n"; 677 } else { 678 StringRef name; 679 if (error(coff->getSymbol(i, symbol))) return; 680 if (error(coff->getSymbolName(symbol, name))) return; 681 outs() << "[" << format("%2d", i) << "]" 682 << "(sec " << format("%2d", int(symbol->SectionNumber)) << ")" 683 << "(fl 0x00)" // Flag bits, which COFF doesn't have. 684 << "(ty " << format("%3x", unsigned(symbol->Type)) << ")" 685 << "(scl " << format("%3x", unsigned(symbol->StorageClass)) << ") " 686 << "(nx " << unsigned(symbol->NumberOfAuxSymbols) << ") " 687 << "0x" << format("%08x", unsigned(symbol->Value)) << " " 688 << name << "\n"; 689 aux_count = symbol->NumberOfAuxSymbols; 690 } 691 } 692 } 693 694 static void PrintSymbolTable(const ObjectFile *o) { 695 outs() << "SYMBOL TABLE:\n"; 696 697 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) 698 PrintCOFFSymbolTable(coff); 699 else { 700 error_code EC; 701 for (symbol_iterator si = o->begin_symbols(), se = o->end_symbols(); 702 si != se; si.increment(EC)) { 703 if (error(EC)) 704 return; 705 StringRef Name; 706 uint64_t Address; 707 SymbolRef::Type Type; 708 uint64_t Size; 709 uint32_t Flags; 710 section_iterator Section = o->end_sections(); 711 if (error(si->getName(Name))) continue; 712 if (error(si->getAddress(Address))) continue; 713 if (error(si->getFlags(Flags))) continue; 714 if (error(si->getType(Type))) continue; 715 if (error(si->getSize(Size))) continue; 716 if (error(si->getSection(Section))) continue; 717 718 bool Global = Flags & SymbolRef::SF_Global; 719 bool Weak = Flags & SymbolRef::SF_Weak; 720 bool Absolute = Flags & SymbolRef::SF_Absolute; 721 722 if (Address == UnknownAddressOrSize) 723 Address = 0; 724 if (Size == UnknownAddressOrSize) 725 Size = 0; 726 char GlobLoc = ' '; 727 if (Type != SymbolRef::ST_Unknown) 728 GlobLoc = Global ? 'g' : 'l'; 729 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File) 730 ? 'd' : ' '; 731 char FileFunc = ' '; 732 if (Type == SymbolRef::ST_File) 733 FileFunc = 'f'; 734 else if (Type == SymbolRef::ST_Function) 735 FileFunc = 'F'; 736 737 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 : 738 "%08" PRIx64; 739 740 outs() << format(Fmt, Address) << " " 741 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' ' 742 << (Weak ? 'w' : ' ') // Weak? 743 << ' ' // Constructor. Not supported yet. 744 << ' ' // Warning. Not supported yet. 745 << ' ' // Indirect reference to another symbol. 746 << Debug // Debugging (d) or dynamic (D) symbol. 747 << FileFunc // Name of function (F), file (f) or object (O). 748 << ' '; 749 if (Absolute) 750 outs() << "*ABS*"; 751 else if (Section == o->end_sections()) 752 outs() << "*UND*"; 753 else { 754 if (const MachOObjectFile *MachO = 755 dyn_cast<const MachOObjectFile>(o)) { 756 DataRefImpl DR = Section->getRawDataRefImpl(); 757 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR); 758 outs() << SegmentName << ","; 759 } 760 StringRef SectionName; 761 if (error(Section->getName(SectionName))) 762 SectionName = ""; 763 outs() << SectionName; 764 } 765 outs() << '\t' 766 << format("%08" PRIx64 " ", Size) 767 << Name 768 << '\n'; 769 } 770 } 771 } 772 773 static void PrintUnwindInfo(const ObjectFile *o) { 774 outs() << "Unwind info:\n\n"; 775 776 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) { 777 printCOFFUnwindInfo(coff); 778 } else { 779 // TODO: Extract DWARF dump tool to objdump. 780 errs() << "This operation is only currently supported " 781 "for COFF object files.\n"; 782 return; 783 } 784 } 785 786 static void printPrivateFileHeader(const ObjectFile *o) { 787 if (o->isELF()) { 788 printELFFileHeader(o); 789 } else if (o->isCOFF()) { 790 printCOFFFileHeader(o); 791 } 792 } 793 794 static void DumpObject(const ObjectFile *o) { 795 outs() << '\n'; 796 outs() << o->getFileName() 797 << ":\tfile format " << o->getFileFormatName() << "\n\n"; 798 799 if (Disassemble) 800 DisassembleObject(o, Relocations); 801 if (Relocations && !Disassemble) 802 PrintRelocations(o); 803 if (SectionHeaders) 804 PrintSectionHeaders(o); 805 if (SectionContents) 806 PrintSectionContents(o); 807 if (SymbolTable) 808 PrintSymbolTable(o); 809 if (UnwindInfo) 810 PrintUnwindInfo(o); 811 if (PrivateHeaders) 812 printPrivateFileHeader(o); 813 } 814 815 /// @brief Dump each object file in \a a; 816 static void DumpArchive(const Archive *a) { 817 for (Archive::child_iterator i = a->child_begin(), e = a->child_end(); i != e; 818 ++i) { 819 OwningPtr<Binary> child; 820 if (error_code EC = i->getAsBinary(child)) { 821 // Ignore non-object files. 822 if (EC != object_error::invalid_file_type) 823 errs() << ToolName << ": '" << a->getFileName() << "': " << EC.message() 824 << ".\n"; 825 continue; 826 } 827 if (ObjectFile *o = dyn_cast<ObjectFile>(child.get())) 828 DumpObject(o); 829 else 830 errs() << ToolName << ": '" << a->getFileName() << "': " 831 << "Unrecognized file type.\n"; 832 } 833 } 834 835 /// @brief Open file and figure out how to dump it. 836 static void DumpInput(StringRef file) { 837 // If file isn't stdin, check that it exists. 838 if (file != "-" && !sys::fs::exists(file)) { 839 errs() << ToolName << ": '" << file << "': " << "No such file\n"; 840 return; 841 } 842 843 if (MachOOpt && Disassemble) { 844 DisassembleInputMachO(file); 845 return; 846 } 847 848 // Attempt to open the binary. 849 ErrorOr<Binary *> BinaryOrErr = createBinary(file); 850 if (error_code EC = BinaryOrErr.getError()) { 851 errs() << ToolName << ": '" << file << "': " << EC.message() << ".\n"; 852 return; 853 } 854 OwningPtr<Binary> binary(BinaryOrErr.get()); 855 856 if (Archive *a = dyn_cast<Archive>(binary.get())) 857 DumpArchive(a); 858 else if (ObjectFile *o = dyn_cast<ObjectFile>(binary.get())) 859 DumpObject(o); 860 else 861 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n"; 862 } 863 864 int main(int argc, char **argv) { 865 // Print a stack trace if we signal out. 866 sys::PrintStackTraceOnErrorSignal(); 867 PrettyStackTraceProgram X(argc, argv); 868 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 869 870 // Initialize targets and assembly printers/parsers. 871 llvm::InitializeAllTargetInfos(); 872 llvm::InitializeAllTargetMCs(); 873 llvm::InitializeAllAsmParsers(); 874 llvm::InitializeAllDisassemblers(); 875 876 // Register the target printer for --version. 877 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); 878 879 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n"); 880 TripleName = Triple::normalize(TripleName); 881 882 ToolName = argv[0]; 883 884 // Defaults to a.out if no filenames specified. 885 if (InputFilenames.size() == 0) 886 InputFilenames.push_back("a.out"); 887 888 if (!Disassemble 889 && !Relocations 890 && !SectionHeaders 891 && !SectionContents 892 && !SymbolTable 893 && !UnwindInfo 894 && !PrivateHeaders) { 895 cl::PrintHelpMessage(); 896 return 2; 897 } 898 899 std::for_each(InputFilenames.begin(), InputFilenames.end(), 900 DumpInput); 901 902 return 0; 903 } 904