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