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