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