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