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/Optional.h" 21#include "llvm/ADT/STLExtras.h" 22#include "llvm/ADT/StringExtras.h" 23#include "llvm/ADT/Triple.h" 24#include "llvm/CodeGen/FaultMaps.h" 25#include "llvm/MC/MCAsmInfo.h" 26#include "llvm/MC/MCContext.h" 27#include "llvm/MC/MCDisassembler.h" 28#include "llvm/MC/MCInst.h" 29#include "llvm/MC/MCInstPrinter.h" 30#include "llvm/MC/MCInstrAnalysis.h" 31#include "llvm/MC/MCInstrInfo.h" 32#include "llvm/MC/MCObjectFileInfo.h" 33#include "llvm/MC/MCRegisterInfo.h" 34#include "llvm/MC/MCRelocationInfo.h" 35#include "llvm/MC/MCSubtargetInfo.h" 36#include "llvm/Object/Archive.h" 37#include "llvm/Object/ELFObjectFile.h" 38#include "llvm/Object/COFF.h" 39#include "llvm/Object/MachO.h" 40#include "llvm/Object/ObjectFile.h" 41#include "llvm/Support/Casting.h" 42#include "llvm/Support/CommandLine.h" 43#include "llvm/Support/Debug.h" 44#include "llvm/Support/Errc.h" 45#include "llvm/Support/FileSystem.h" 46#include "llvm/Support/Format.h" 47#include "llvm/Support/GraphWriter.h" 48#include "llvm/Support/Host.h" 49#include "llvm/Support/ManagedStatic.h" 50#include "llvm/Support/MemoryBuffer.h" 51#include "llvm/Support/PrettyStackTrace.h" 52#include "llvm/Support/Signals.h" 53#include "llvm/Support/SourceMgr.h" 54#include "llvm/Support/TargetRegistry.h" 55#include "llvm/Support/TargetSelect.h" 56#include "llvm/Support/raw_ostream.h" 57#include <algorithm> 58#include <cctype> 59#include <cstring> 60#include <system_error> 61 62using namespace llvm; 63using namespace object; 64 65static cl::list<std::string> 66InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore); 67 68cl::opt<bool> 69llvm::Disassemble("disassemble", 70 cl::desc("Display assembler mnemonics for the machine instructions")); 71static cl::alias 72Disassembled("d", cl::desc("Alias for --disassemble"), 73 cl::aliasopt(Disassemble)); 74 75cl::opt<bool> 76llvm::DisassembleAll("disassemble-all", 77 cl::desc("Display assembler mnemonics for the machine instructions")); 78static cl::alias 79DisassembleAlld("D", cl::desc("Alias for --disassemble-all"), 80 cl::aliasopt(DisassembleAll)); 81 82cl::opt<bool> 83llvm::Relocations("r", cl::desc("Display the relocation entries in the file")); 84 85cl::opt<bool> 86llvm::SectionContents("s", cl::desc("Display the content of each section")); 87 88cl::opt<bool> 89llvm::SymbolTable("t", cl::desc("Display the symbol table")); 90 91cl::opt<bool> 92llvm::ExportsTrie("exports-trie", cl::desc("Display mach-o exported symbols")); 93 94cl::opt<bool> 95llvm::Rebase("rebase", cl::desc("Display mach-o rebasing info")); 96 97cl::opt<bool> 98llvm::Bind("bind", cl::desc("Display mach-o binding info")); 99 100cl::opt<bool> 101llvm::LazyBind("lazy-bind", cl::desc("Display mach-o lazy binding info")); 102 103cl::opt<bool> 104llvm::WeakBind("weak-bind", cl::desc("Display mach-o weak binding info")); 105 106cl::opt<bool> 107llvm::RawClangAST("raw-clang-ast", 108 cl::desc("Dump the raw binary contents of the clang AST section")); 109 110static cl::opt<bool> 111MachOOpt("macho", cl::desc("Use MachO specific object file parser")); 112static cl::alias 113MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachOOpt)); 114 115cl::opt<std::string> 116llvm::TripleName("triple", cl::desc("Target triple to disassemble for, " 117 "see -version for available targets")); 118 119cl::opt<std::string> 120llvm::MCPU("mcpu", 121 cl::desc("Target a specific cpu type (-mcpu=help for details)"), 122 cl::value_desc("cpu-name"), 123 cl::init("")); 124 125cl::opt<std::string> 126llvm::ArchName("arch-name", cl::desc("Target arch to disassemble for, " 127 "see -version for available targets")); 128 129cl::opt<bool> 130llvm::SectionHeaders("section-headers", cl::desc("Display summaries of the " 131 "headers for each section.")); 132static cl::alias 133SectionHeadersShort("headers", cl::desc("Alias for --section-headers"), 134 cl::aliasopt(SectionHeaders)); 135static cl::alias 136SectionHeadersShorter("h", cl::desc("Alias for --section-headers"), 137 cl::aliasopt(SectionHeaders)); 138 139cl::list<std::string> 140llvm::FilterSections("section", cl::desc("Operate on the specified sections only. " 141 "With -macho dump segment,section")); 142cl::alias 143static FilterSectionsj("j", cl::desc("Alias for --section"), 144 cl::aliasopt(llvm::FilterSections)); 145 146cl::list<std::string> 147llvm::MAttrs("mattr", 148 cl::CommaSeparated, 149 cl::desc("Target specific attributes"), 150 cl::value_desc("a1,+a2,-a3,...")); 151 152cl::opt<bool> 153llvm::NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling " 154 "instructions, do not print " 155 "the instruction bytes.")); 156 157cl::opt<bool> 158llvm::UnwindInfo("unwind-info", cl::desc("Display unwind information")); 159 160static cl::alias 161UnwindInfoShort("u", cl::desc("Alias for --unwind-info"), 162 cl::aliasopt(UnwindInfo)); 163 164cl::opt<bool> 165llvm::PrivateHeaders("private-headers", 166 cl::desc("Display format specific file headers")); 167 168static cl::alias 169PrivateHeadersShort("p", cl::desc("Alias for --private-headers"), 170 cl::aliasopt(PrivateHeaders)); 171 172cl::opt<bool> 173 llvm::PrintImmHex("print-imm-hex", 174 cl::desc("Use hex format for immediate values")); 175 176cl::opt<bool> PrintFaultMaps("fault-map-section", 177 cl::desc("Display contents of faultmap section")); 178 179static StringRef ToolName; 180 181namespace { 182typedef std::function<bool(llvm::object::SectionRef const &)> FilterPredicate; 183 184class SectionFilterIterator { 185public: 186 SectionFilterIterator(FilterPredicate P, 187 llvm::object::section_iterator const &I, 188 llvm::object::section_iterator const &E) 189 : Predicate(P), Iterator(I), End(E) { 190 ScanPredicate(); 191 } 192 const llvm::object::SectionRef &operator*() const { return *Iterator; } 193 SectionFilterIterator &operator++() { 194 ++Iterator; 195 ScanPredicate(); 196 return *this; 197 } 198 bool operator!=(SectionFilterIterator const &Other) const { 199 return Iterator != Other.Iterator; 200 } 201 202private: 203 void ScanPredicate() { 204 while (Iterator != End && !Predicate(*Iterator)) { 205 ++Iterator; 206 } 207 } 208 FilterPredicate Predicate; 209 llvm::object::section_iterator Iterator; 210 llvm::object::section_iterator End; 211}; 212 213class SectionFilter { 214public: 215 SectionFilter(FilterPredicate P, llvm::object::ObjectFile const &O) 216 : Predicate(P), Object(O) {} 217 SectionFilterIterator begin() { 218 return SectionFilterIterator(Predicate, Object.section_begin(), 219 Object.section_end()); 220 } 221 SectionFilterIterator end() { 222 return SectionFilterIterator(Predicate, Object.section_end(), 223 Object.section_end()); 224 } 225 226private: 227 FilterPredicate Predicate; 228 llvm::object::ObjectFile const &Object; 229}; 230SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O) { 231 return SectionFilter([](llvm::object::SectionRef const &S) { 232 if(FilterSections.empty()) 233 return true; 234 llvm::StringRef String; 235 std::error_code error = S.getName(String); 236 if (error) 237 return false; 238 return std::find(FilterSections.begin(), 239 FilterSections.end(), 240 String) != FilterSections.end(); 241 }, 242 O); 243} 244} 245 246void llvm::error(std::error_code EC) { 247 if (!EC) 248 return; 249 250 outs() << ToolName << ": error reading file: " << EC.message() << ".\n"; 251 outs().flush(); 252 exit(1); 253} 254 255void llvm::report_error(StringRef File, std::error_code EC) { 256 assert(EC); 257 errs() << ToolName << ": '" << File << "': " << EC.message() << ".\n"; 258 exit(1); 259} 260 261static const Target *getTarget(const ObjectFile *Obj = nullptr) { 262 // Figure out the target triple. 263 llvm::Triple TheTriple("unknown-unknown-unknown"); 264 if (TripleName.empty()) { 265 if (Obj) { 266 TheTriple.setArch(Triple::ArchType(Obj->getArch())); 267 // TheTriple defaults to ELF, and COFF doesn't have an environment: 268 // the best we can do here is indicate that it is mach-o. 269 if (Obj->isMachO()) 270 TheTriple.setObjectFormat(Triple::MachO); 271 272 if (Obj->isCOFF()) { 273 const auto COFFObj = dyn_cast<COFFObjectFile>(Obj); 274 if (COFFObj->getArch() == Triple::thumb) 275 TheTriple.setTriple("thumbv7-windows"); 276 } 277 } 278 } else 279 TheTriple.setTriple(Triple::normalize(TripleName)); 280 281 // Get the target specific parser. 282 std::string Error; 283 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple, 284 Error); 285 if (!TheTarget) 286 report_fatal_error("can't find target: " + Error); 287 288 // Update the triple name and return the found target. 289 TripleName = TheTriple.getTriple(); 290 return TheTarget; 291} 292 293bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) { 294 return a.getOffset() < b.getOffset(); 295} 296 297namespace { 298class PrettyPrinter { 299public: 300 virtual ~PrettyPrinter(){} 301 virtual void printInst(MCInstPrinter &IP, const MCInst *MI, 302 ArrayRef<uint8_t> Bytes, uint64_t Address, 303 raw_ostream &OS, StringRef Annot, 304 MCSubtargetInfo const &STI) { 305 outs() << format("%8" PRIx64 ":", Address); 306 if (!NoShowRawInsn) { 307 outs() << "\t"; 308 dumpBytes(Bytes, outs()); 309 } 310 IP.printInst(MI, outs(), "", STI); 311 } 312}; 313PrettyPrinter PrettyPrinterInst; 314class HexagonPrettyPrinter : public PrettyPrinter { 315public: 316 void printLead(ArrayRef<uint8_t> Bytes, uint64_t Address, 317 raw_ostream &OS) { 318 uint32_t opcode = 319 (Bytes[3] << 24) | (Bytes[2] << 16) | (Bytes[1] << 8) | Bytes[0]; 320 OS << format("%8" PRIx64 ":", Address); 321 if (!NoShowRawInsn) { 322 OS << "\t"; 323 dumpBytes(Bytes.slice(0, 4), OS); 324 OS << format("%08" PRIx32, opcode); 325 } 326 } 327 void printInst(MCInstPrinter &IP, const MCInst *MI, 328 ArrayRef<uint8_t> Bytes, uint64_t Address, 329 raw_ostream &OS, StringRef Annot, 330 MCSubtargetInfo const &STI) override { 331 std::string Buffer; 332 { 333 raw_string_ostream TempStream(Buffer); 334 IP.printInst(MI, TempStream, "", STI); 335 } 336 StringRef Contents(Buffer); 337 // Split off bundle attributes 338 auto PacketBundle = Contents.rsplit('\n'); 339 // Split off first instruction from the rest 340 auto HeadTail = PacketBundle.first.split('\n'); 341 auto Preamble = " { "; 342 auto Separator = ""; 343 while(!HeadTail.first.empty()) { 344 OS << Separator; 345 Separator = "\n"; 346 printLead(Bytes, Address, OS); 347 OS << Preamble; 348 Preamble = " "; 349 StringRef Inst; 350 auto Duplex = HeadTail.first.split('\v'); 351 if(!Duplex.second.empty()){ 352 OS << Duplex.first; 353 OS << "; "; 354 Inst = Duplex.second; 355 } 356 else 357 Inst = HeadTail.first; 358 OS << Inst; 359 Bytes = Bytes.slice(4); 360 Address += 4; 361 HeadTail = HeadTail.second.split('\n'); 362 } 363 OS << " } " << PacketBundle.second; 364 } 365}; 366HexagonPrettyPrinter HexagonPrettyPrinterInst; 367PrettyPrinter &selectPrettyPrinter(Triple const &Triple) { 368 switch(Triple.getArch()) { 369 default: 370 return PrettyPrinterInst; 371 case Triple::hexagon: 372 return HexagonPrettyPrinterInst; 373 } 374} 375} 376 377template <class ELFT> 378static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj, 379 const RelocationRef &RelRef, 380 SmallVectorImpl<char> &Result) { 381 DataRefImpl Rel = RelRef.getRawDataRefImpl(); 382 383 typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym; 384 typedef typename ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr; 385 typedef typename ELFObjectFile<ELFT>::Elf_Rela Elf_Rela; 386 387 const ELFFile<ELFT> &EF = *Obj->getELFFile(); 388 389 ErrorOr<const Elf_Shdr *> SecOrErr = EF.getSection(Rel.d.a); 390 if (std::error_code EC = SecOrErr.getError()) 391 return EC; 392 const Elf_Shdr *Sec = *SecOrErr; 393 ErrorOr<const Elf_Shdr *> SymTabOrErr = EF.getSection(Sec->sh_link); 394 if (std::error_code EC = SymTabOrErr.getError()) 395 return EC; 396 const Elf_Shdr *SymTab = *SymTabOrErr; 397 assert(SymTab->sh_type == ELF::SHT_SYMTAB || 398 SymTab->sh_type == ELF::SHT_DYNSYM); 399 ErrorOr<const Elf_Shdr *> StrTabSec = EF.getSection(SymTab->sh_link); 400 if (std::error_code EC = StrTabSec.getError()) 401 return EC; 402 ErrorOr<StringRef> StrTabOrErr = EF.getStringTable(*StrTabSec); 403 if (std::error_code EC = StrTabOrErr.getError()) 404 return EC; 405 StringRef StrTab = *StrTabOrErr; 406 uint8_t type = RelRef.getType(); 407 StringRef res; 408 int64_t addend = 0; 409 switch (Sec->sh_type) { 410 default: 411 return object_error::parse_failed; 412 case ELF::SHT_REL: { 413 // TODO: Read implicit addend from section data. 414 break; 415 } 416 case ELF::SHT_RELA: { 417 const Elf_Rela *ERela = Obj->getRela(Rel); 418 addend = ERela->r_addend; 419 break; 420 } 421 } 422 symbol_iterator SI = RelRef.getSymbol(); 423 const Elf_Sym *symb = Obj->getSymbol(SI->getRawDataRefImpl()); 424 StringRef Target; 425 if (symb->getType() == ELF::STT_SECTION) { 426 ErrorOr<section_iterator> SymSI = SI->getSection(); 427 if (std::error_code EC = SymSI.getError()) 428 return EC; 429 const Elf_Shdr *SymSec = Obj->getSection((*SymSI)->getRawDataRefImpl()); 430 ErrorOr<StringRef> SecName = EF.getSectionName(SymSec); 431 if (std::error_code EC = SecName.getError()) 432 return EC; 433 Target = *SecName; 434 } else { 435 ErrorOr<StringRef> SymName = symb->getName(StrTab); 436 if (!SymName) 437 return SymName.getError(); 438 Target = *SymName; 439 } 440 switch (EF.getHeader()->e_machine) { 441 case ELF::EM_X86_64: 442 switch (type) { 443 case ELF::R_X86_64_PC8: 444 case ELF::R_X86_64_PC16: 445 case ELF::R_X86_64_PC32: { 446 std::string fmtbuf; 447 raw_string_ostream fmt(fmtbuf); 448 fmt << Target << (addend < 0 ? "" : "+") << addend << "-P"; 449 fmt.flush(); 450 Result.append(fmtbuf.begin(), fmtbuf.end()); 451 } break; 452 case ELF::R_X86_64_8: 453 case ELF::R_X86_64_16: 454 case ELF::R_X86_64_32: 455 case ELF::R_X86_64_32S: 456 case ELF::R_X86_64_64: { 457 std::string fmtbuf; 458 raw_string_ostream fmt(fmtbuf); 459 fmt << Target << (addend < 0 ? "" : "+") << addend; 460 fmt.flush(); 461 Result.append(fmtbuf.begin(), fmtbuf.end()); 462 } break; 463 default: 464 res = "Unknown"; 465 } 466 break; 467 case ELF::EM_AARCH64: { 468 std::string fmtbuf; 469 raw_string_ostream fmt(fmtbuf); 470 fmt << Target; 471 if (addend != 0) 472 fmt << (addend < 0 ? "" : "+") << addend; 473 fmt.flush(); 474 Result.append(fmtbuf.begin(), fmtbuf.end()); 475 break; 476 } 477 case ELF::EM_386: 478 case ELF::EM_IAMCU: 479 case ELF::EM_ARM: 480 case ELF::EM_HEXAGON: 481 case ELF::EM_MIPS: 482 res = Target; 483 break; 484 default: 485 res = "Unknown"; 486 } 487 if (Result.empty()) 488 Result.append(res.begin(), res.end()); 489 return std::error_code(); 490} 491 492static std::error_code getRelocationValueString(const ELFObjectFileBase *Obj, 493 const RelocationRef &Rel, 494 SmallVectorImpl<char> &Result) { 495 if (auto *ELF32LE = dyn_cast<ELF32LEObjectFile>(Obj)) 496 return getRelocationValueString(ELF32LE, Rel, Result); 497 if (auto *ELF64LE = dyn_cast<ELF64LEObjectFile>(Obj)) 498 return getRelocationValueString(ELF64LE, Rel, Result); 499 if (auto *ELF32BE = dyn_cast<ELF32BEObjectFile>(Obj)) 500 return getRelocationValueString(ELF32BE, Rel, Result); 501 auto *ELF64BE = cast<ELF64BEObjectFile>(Obj); 502 return getRelocationValueString(ELF64BE, Rel, Result); 503} 504 505static std::error_code getRelocationValueString(const COFFObjectFile *Obj, 506 const RelocationRef &Rel, 507 SmallVectorImpl<char> &Result) { 508 symbol_iterator SymI = Rel.getSymbol(); 509 ErrorOr<StringRef> SymNameOrErr = SymI->getName(); 510 if (std::error_code EC = SymNameOrErr.getError()) 511 return EC; 512 StringRef SymName = *SymNameOrErr; 513 Result.append(SymName.begin(), SymName.end()); 514 return std::error_code(); 515} 516 517static void printRelocationTargetName(const MachOObjectFile *O, 518 const MachO::any_relocation_info &RE, 519 raw_string_ostream &fmt) { 520 bool IsScattered = O->isRelocationScattered(RE); 521 522 // Target of a scattered relocation is an address. In the interest of 523 // generating pretty output, scan through the symbol table looking for a 524 // symbol that aligns with that address. If we find one, print it. 525 // Otherwise, we just print the hex address of the target. 526 if (IsScattered) { 527 uint32_t Val = O->getPlainRelocationSymbolNum(RE); 528 529 for (const SymbolRef &Symbol : O->symbols()) { 530 std::error_code ec; 531 ErrorOr<uint64_t> Addr = Symbol.getAddress(); 532 if ((ec = Addr.getError())) 533 report_fatal_error(ec.message()); 534 if (*Addr != Val) 535 continue; 536 ErrorOr<StringRef> Name = Symbol.getName(); 537 if (std::error_code EC = Name.getError()) 538 report_fatal_error(EC.message()); 539 fmt << *Name; 540 return; 541 } 542 543 // If we couldn't find a symbol that this relocation refers to, try 544 // to find a section beginning instead. 545 for (const SectionRef &Section : ToolSectionFilter(*O)) { 546 std::error_code ec; 547 548 StringRef Name; 549 uint64_t Addr = Section.getAddress(); 550 if (Addr != Val) 551 continue; 552 if ((ec = Section.getName(Name))) 553 report_fatal_error(ec.message()); 554 fmt << Name; 555 return; 556 } 557 558 fmt << format("0x%x", Val); 559 return; 560 } 561 562 StringRef S; 563 bool isExtern = O->getPlainRelocationExternal(RE); 564 uint64_t Val = O->getPlainRelocationSymbolNum(RE); 565 566 if (isExtern) { 567 symbol_iterator SI = O->symbol_begin(); 568 advance(SI, Val); 569 ErrorOr<StringRef> SOrErr = SI->getName(); 570 error(SOrErr.getError()); 571 S = *SOrErr; 572 } else { 573 section_iterator SI = O->section_begin(); 574 // Adjust for the fact that sections are 1-indexed. 575 advance(SI, Val - 1); 576 SI->getName(S); 577 } 578 579 fmt << S; 580} 581 582static std::error_code getRelocationValueString(const MachOObjectFile *Obj, 583 const RelocationRef &RelRef, 584 SmallVectorImpl<char> &Result) { 585 DataRefImpl Rel = RelRef.getRawDataRefImpl(); 586 MachO::any_relocation_info RE = Obj->getRelocation(Rel); 587 588 unsigned Arch = Obj->getArch(); 589 590 std::string fmtbuf; 591 raw_string_ostream fmt(fmtbuf); 592 unsigned Type = Obj->getAnyRelocationType(RE); 593 bool IsPCRel = Obj->getAnyRelocationPCRel(RE); 594 595 // Determine any addends that should be displayed with the relocation. 596 // These require decoding the relocation type, which is triple-specific. 597 598 // X86_64 has entirely custom relocation types. 599 if (Arch == Triple::x86_64) { 600 bool isPCRel = Obj->getAnyRelocationPCRel(RE); 601 602 switch (Type) { 603 case MachO::X86_64_RELOC_GOT_LOAD: 604 case MachO::X86_64_RELOC_GOT: { 605 printRelocationTargetName(Obj, RE, fmt); 606 fmt << "@GOT"; 607 if (isPCRel) 608 fmt << "PCREL"; 609 break; 610 } 611 case MachO::X86_64_RELOC_SUBTRACTOR: { 612 DataRefImpl RelNext = Rel; 613 Obj->moveRelocationNext(RelNext); 614 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext); 615 616 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type 617 // X86_64_RELOC_UNSIGNED. 618 // NOTE: Scattered relocations don't exist on x86_64. 619 unsigned RType = Obj->getAnyRelocationType(RENext); 620 if (RType != MachO::X86_64_RELOC_UNSIGNED) 621 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after " 622 "X86_64_RELOC_SUBTRACTOR."); 623 624 // The X86_64_RELOC_UNSIGNED contains the minuend symbol; 625 // X86_64_RELOC_SUBTRACTOR contains the subtrahend. 626 printRelocationTargetName(Obj, RENext, fmt); 627 fmt << "-"; 628 printRelocationTargetName(Obj, RE, fmt); 629 break; 630 } 631 case MachO::X86_64_RELOC_TLV: 632 printRelocationTargetName(Obj, RE, fmt); 633 fmt << "@TLV"; 634 if (isPCRel) 635 fmt << "P"; 636 break; 637 case MachO::X86_64_RELOC_SIGNED_1: 638 printRelocationTargetName(Obj, RE, fmt); 639 fmt << "-1"; 640 break; 641 case MachO::X86_64_RELOC_SIGNED_2: 642 printRelocationTargetName(Obj, RE, fmt); 643 fmt << "-2"; 644 break; 645 case MachO::X86_64_RELOC_SIGNED_4: 646 printRelocationTargetName(Obj, RE, fmt); 647 fmt << "-4"; 648 break; 649 default: 650 printRelocationTargetName(Obj, RE, fmt); 651 break; 652 } 653 // X86 and ARM share some relocation types in common. 654 } else if (Arch == Triple::x86 || Arch == Triple::arm || 655 Arch == Triple::ppc) { 656 // Generic relocation types... 657 switch (Type) { 658 case MachO::GENERIC_RELOC_PAIR: // prints no info 659 return std::error_code(); 660 case MachO::GENERIC_RELOC_SECTDIFF: { 661 DataRefImpl RelNext = Rel; 662 Obj->moveRelocationNext(RelNext); 663 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext); 664 665 // X86 sect diff's must be followed by a relocation of type 666 // GENERIC_RELOC_PAIR. 667 unsigned RType = Obj->getAnyRelocationType(RENext); 668 669 if (RType != MachO::GENERIC_RELOC_PAIR) 670 report_fatal_error("Expected GENERIC_RELOC_PAIR after " 671 "GENERIC_RELOC_SECTDIFF."); 672 673 printRelocationTargetName(Obj, RE, fmt); 674 fmt << "-"; 675 printRelocationTargetName(Obj, RENext, fmt); 676 break; 677 } 678 } 679 680 if (Arch == Triple::x86 || Arch == Triple::ppc) { 681 switch (Type) { 682 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: { 683 DataRefImpl RelNext = Rel; 684 Obj->moveRelocationNext(RelNext); 685 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext); 686 687 // X86 sect diff's must be followed by a relocation of type 688 // GENERIC_RELOC_PAIR. 689 unsigned RType = Obj->getAnyRelocationType(RENext); 690 if (RType != MachO::GENERIC_RELOC_PAIR) 691 report_fatal_error("Expected GENERIC_RELOC_PAIR after " 692 "GENERIC_RELOC_LOCAL_SECTDIFF."); 693 694 printRelocationTargetName(Obj, RE, fmt); 695 fmt << "-"; 696 printRelocationTargetName(Obj, RENext, fmt); 697 break; 698 } 699 case MachO::GENERIC_RELOC_TLV: { 700 printRelocationTargetName(Obj, RE, fmt); 701 fmt << "@TLV"; 702 if (IsPCRel) 703 fmt << "P"; 704 break; 705 } 706 default: 707 printRelocationTargetName(Obj, RE, fmt); 708 } 709 } else { // ARM-specific relocations 710 switch (Type) { 711 case MachO::ARM_RELOC_HALF: 712 case MachO::ARM_RELOC_HALF_SECTDIFF: { 713 // Half relocations steal a bit from the length field to encode 714 // whether this is an upper16 or a lower16 relocation. 715 bool isUpper = Obj->getAnyRelocationLength(RE) >> 1; 716 717 if (isUpper) 718 fmt << ":upper16:("; 719 else 720 fmt << ":lower16:("; 721 printRelocationTargetName(Obj, RE, fmt); 722 723 DataRefImpl RelNext = Rel; 724 Obj->moveRelocationNext(RelNext); 725 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext); 726 727 // ARM half relocs must be followed by a relocation of type 728 // ARM_RELOC_PAIR. 729 unsigned RType = Obj->getAnyRelocationType(RENext); 730 if (RType != MachO::ARM_RELOC_PAIR) 731 report_fatal_error("Expected ARM_RELOC_PAIR after " 732 "ARM_RELOC_HALF"); 733 734 // NOTE: The half of the target virtual address is stashed in the 735 // address field of the secondary relocation, but we can't reverse 736 // engineer the constant offset from it without decoding the movw/movt 737 // instruction to find the other half in its immediate field. 738 739 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the 740 // symbol/section pointer of the follow-on relocation. 741 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) { 742 fmt << "-"; 743 printRelocationTargetName(Obj, RENext, fmt); 744 } 745 746 fmt << ")"; 747 break; 748 } 749 default: { printRelocationTargetName(Obj, RE, fmt); } 750 } 751 } 752 } else 753 printRelocationTargetName(Obj, RE, fmt); 754 755 fmt.flush(); 756 Result.append(fmtbuf.begin(), fmtbuf.end()); 757 return std::error_code(); 758} 759 760static std::error_code getRelocationValueString(const RelocationRef &Rel, 761 SmallVectorImpl<char> &Result) { 762 const ObjectFile *Obj = Rel.getObject(); 763 if (auto *ELF = dyn_cast<ELFObjectFileBase>(Obj)) 764 return getRelocationValueString(ELF, Rel, Result); 765 if (auto *COFF = dyn_cast<COFFObjectFile>(Obj)) 766 return getRelocationValueString(COFF, Rel, Result); 767 auto *MachO = cast<MachOObjectFile>(Obj); 768 return getRelocationValueString(MachO, Rel, Result); 769} 770 771/// @brief Indicates whether this relocation should hidden when listing 772/// relocations, usually because it is the trailing part of a multipart 773/// relocation that will be printed as part of the leading relocation. 774static bool getHidden(RelocationRef RelRef) { 775 const ObjectFile *Obj = RelRef.getObject(); 776 auto *MachO = dyn_cast<MachOObjectFile>(Obj); 777 if (!MachO) 778 return false; 779 780 unsigned Arch = MachO->getArch(); 781 DataRefImpl Rel = RelRef.getRawDataRefImpl(); 782 uint64_t Type = MachO->getRelocationType(Rel); 783 784 // On arches that use the generic relocations, GENERIC_RELOC_PAIR 785 // is always hidden. 786 if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) { 787 if (Type == MachO::GENERIC_RELOC_PAIR) 788 return true; 789 } else if (Arch == Triple::x86_64) { 790 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows 791 // an X86_64_RELOC_SUBTRACTOR. 792 if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) { 793 DataRefImpl RelPrev = Rel; 794 RelPrev.d.a--; 795 uint64_t PrevType = MachO->getRelocationType(RelPrev); 796 if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR) 797 return true; 798 } 799 } 800 801 return false; 802} 803 804static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) { 805 const Target *TheTarget = getTarget(Obj); 806 807 // Package up features to be passed to target/subtarget 808 std::string FeaturesStr; 809 if (MAttrs.size()) { 810 SubtargetFeatures Features; 811 for (unsigned i = 0; i != MAttrs.size(); ++i) 812 Features.AddFeature(MAttrs[i]); 813 FeaturesStr = Features.getString(); 814 } 815 816 std::unique_ptr<const MCRegisterInfo> MRI( 817 TheTarget->createMCRegInfo(TripleName)); 818 if (!MRI) 819 report_fatal_error("error: no register info for target " + TripleName); 820 821 // Set up disassembler. 822 std::unique_ptr<const MCAsmInfo> AsmInfo( 823 TheTarget->createMCAsmInfo(*MRI, TripleName)); 824 if (!AsmInfo) 825 report_fatal_error("error: no assembly info for target " + TripleName); 826 std::unique_ptr<const MCSubtargetInfo> STI( 827 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr)); 828 if (!STI) 829 report_fatal_error("error: no subtarget info for target " + TripleName); 830 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo()); 831 if (!MII) 832 report_fatal_error("error: no instruction info for target " + TripleName); 833 std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo); 834 MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get()); 835 836 std::unique_ptr<MCDisassembler> DisAsm( 837 TheTarget->createMCDisassembler(*STI, Ctx)); 838 if (!DisAsm) 839 report_fatal_error("error: no disassembler for target " + TripleName); 840 841 std::unique_ptr<const MCInstrAnalysis> MIA( 842 TheTarget->createMCInstrAnalysis(MII.get())); 843 844 int AsmPrinterVariant = AsmInfo->getAssemblerDialect(); 845 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter( 846 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI)); 847 if (!IP) 848 report_fatal_error("error: no instruction printer for target " + 849 TripleName); 850 IP->setPrintImmHex(PrintImmHex); 851 PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName)); 852 853 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ": " : 854 "\t\t\t%08" PRIx64 ": "; 855 856 // Create a mapping, RelocSecs = SectionRelocMap[S], where sections 857 // in RelocSecs contain the relocations for section S. 858 std::error_code EC; 859 std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap; 860 for (const SectionRef &Section : ToolSectionFilter(*Obj)) { 861 section_iterator Sec2 = Section.getRelocatedSection(); 862 if (Sec2 != Obj->section_end()) 863 SectionRelocMap[*Sec2].push_back(Section); 864 } 865 866 // Create a mapping from virtual address to symbol name. This is used to 867 // pretty print the symbols while disassembling. 868 typedef std::vector<std::pair<uint64_t, StringRef>> SectionSymbolsTy; 869 std::map<SectionRef, SectionSymbolsTy> AllSymbols; 870 for (const SymbolRef &Symbol : Obj->symbols()) { 871 ErrorOr<uint64_t> AddressOrErr = Symbol.getAddress(); 872 error(AddressOrErr.getError()); 873 uint64_t Address = *AddressOrErr; 874 875 ErrorOr<StringRef> Name = Symbol.getName(); 876 error(Name.getError()); 877 if (Name->empty()) 878 continue; 879 880 ErrorOr<section_iterator> SectionOrErr = Symbol.getSection(); 881 error(SectionOrErr.getError()); 882 section_iterator SecI = *SectionOrErr; 883 if (SecI == Obj->section_end()) 884 continue; 885 886 AllSymbols[*SecI].emplace_back(Address, *Name); 887 } 888 889 // Create a mapping from virtual address to section. 890 std::vector<std::pair<uint64_t, SectionRef>> SectionAddresses; 891 for (SectionRef Sec : Obj->sections()) 892 SectionAddresses.emplace_back(Sec.getAddress(), Sec); 893 array_pod_sort(SectionAddresses.begin(), SectionAddresses.end()); 894 895 // Linked executables (.exe and .dll files) typically don't include a real 896 // symbol table but they might contain an export table. 897 if (const auto *COFFObj = dyn_cast<COFFObjectFile>(Obj)) { 898 for (const auto &ExportEntry : COFFObj->export_directories()) { 899 StringRef Name; 900 error(ExportEntry.getSymbolName(Name)); 901 if (Name.empty()) 902 continue; 903 uint32_t RVA; 904 error(ExportEntry.getExportRVA(RVA)); 905 906 uint64_t VA = COFFObj->getImageBase() + RVA; 907 auto Sec = std::upper_bound( 908 SectionAddresses.begin(), SectionAddresses.end(), VA, 909 [](uint64_t LHS, const std::pair<uint64_t, SectionRef> &RHS) { 910 return LHS < RHS.first; 911 }); 912 if (Sec != SectionAddresses.begin()) 913 --Sec; 914 else 915 Sec = SectionAddresses.end(); 916 917 if (Sec != SectionAddresses.end()) 918 AllSymbols[Sec->second].emplace_back(VA, Name); 919 } 920 } 921 922 // Sort all the symbols, this allows us to use a simple binary search to find 923 // a symbol near an address. 924 for (std::pair<const SectionRef, SectionSymbolsTy> &SecSyms : AllSymbols) 925 array_pod_sort(SecSyms.second.begin(), SecSyms.second.end()); 926 927 for (const SectionRef &Section : ToolSectionFilter(*Obj)) { 928 if (!DisassembleAll && (!Section.isText() || Section.isVirtual())) 929 continue; 930 931 uint64_t SectionAddr = Section.getAddress(); 932 uint64_t SectSize = Section.getSize(); 933 if (!SectSize) 934 continue; 935 936 // Get the list of all the symbols in this section. 937 SectionSymbolsTy &Symbols = AllSymbols[Section]; 938 std::vector<uint64_t> DataMappingSymsAddr; 939 std::vector<uint64_t> TextMappingSymsAddr; 940 if (Obj->isELF() && Obj->getArch() == Triple::aarch64) { 941 for (const auto &Symb : Symbols) { 942 uint64_t Address = Symb.first; 943 StringRef Name = Symb.second; 944 if (Name.startswith("$d")) 945 DataMappingSymsAddr.push_back(Address - SectionAddr); 946 if (Name.startswith("$x")) 947 TextMappingSymsAddr.push_back(Address - SectionAddr); 948 } 949 } 950 951 std::sort(DataMappingSymsAddr.begin(), DataMappingSymsAddr.end()); 952 std::sort(TextMappingSymsAddr.begin(), TextMappingSymsAddr.end()); 953 954 // Make a list of all the relocations for this section. 955 std::vector<RelocationRef> Rels; 956 if (InlineRelocs) { 957 for (const SectionRef &RelocSec : SectionRelocMap[Section]) { 958 for (const RelocationRef &Reloc : RelocSec.relocations()) { 959 Rels.push_back(Reloc); 960 } 961 } 962 } 963 964 // Sort relocations by address. 965 std::sort(Rels.begin(), Rels.end(), RelocAddressLess); 966 967 StringRef SegmentName = ""; 968 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) { 969 DataRefImpl DR = Section.getRawDataRefImpl(); 970 SegmentName = MachO->getSectionFinalSegmentName(DR); 971 } 972 StringRef name; 973 error(Section.getName(name)); 974 outs() << "Disassembly of section "; 975 if (!SegmentName.empty()) 976 outs() << SegmentName << ","; 977 outs() << name << ':'; 978 979 // If the section has no symbol at the start, just insert a dummy one. 980 if (Symbols.empty() || Symbols[0].first != 0) 981 Symbols.insert(Symbols.begin(), std::make_pair(SectionAddr, name)); 982 983 SmallString<40> Comments; 984 raw_svector_ostream CommentStream(Comments); 985 986 StringRef BytesStr; 987 error(Section.getContents(BytesStr)); 988 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()), 989 BytesStr.size()); 990 991 uint64_t Size; 992 uint64_t Index; 993 994 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin(); 995 std::vector<RelocationRef>::const_iterator rel_end = Rels.end(); 996 // Disassemble symbol by symbol. 997 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) { 998 999 uint64_t Start = Symbols[si].first - SectionAddr; 1000 // The end is either the section end or the beginning of the next 1001 // symbol. 1002 uint64_t End = 1003 (si == se - 1) ? SectSize : Symbols[si + 1].first - SectionAddr; 1004 // Don't try to disassemble beyond the end of section contents. 1005 if (End > SectSize) 1006 End = SectSize; 1007 // If this symbol has the same address as the next symbol, then skip it. 1008 if (Start >= End) 1009 continue; 1010 1011 outs() << '\n' << Symbols[si].second << ":\n"; 1012 1013#ifndef NDEBUG 1014 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls(); 1015#else 1016 raw_ostream &DebugOut = nulls(); 1017#endif 1018 1019 for (Index = Start; Index < End; Index += Size) { 1020 MCInst Inst; 1021 1022 // AArch64 ELF binaries can interleave data and text in the 1023 // same section. We rely on the markers introduced to 1024 // understand what we need to dump. 1025 if (Obj->isELF() && Obj->getArch() == Triple::aarch64) { 1026 uint64_t Stride = 0; 1027 1028 auto DAI = std::lower_bound(DataMappingSymsAddr.begin(), 1029 DataMappingSymsAddr.end(), Index); 1030 if (DAI != DataMappingSymsAddr.end() && *DAI == Index) { 1031 // Switch to data. 1032 while (Index < End) { 1033 outs() << format("%8" PRIx64 ":", SectionAddr + Index); 1034 outs() << "\t"; 1035 if (Index + 4 <= End) { 1036 Stride = 4; 1037 dumpBytes(Bytes.slice(Index, 4), outs()); 1038 outs() << "\t.word"; 1039 } else if (Index + 2 <= End) { 1040 Stride = 2; 1041 dumpBytes(Bytes.slice(Index, 2), outs()); 1042 outs() << "\t.short"; 1043 } else { 1044 Stride = 1; 1045 dumpBytes(Bytes.slice(Index, 1), outs()); 1046 outs() << "\t.byte"; 1047 } 1048 Index += Stride; 1049 outs() << "\n"; 1050 auto TAI = std::lower_bound(TextMappingSymsAddr.begin(), 1051 TextMappingSymsAddr.end(), Index); 1052 if (TAI != TextMappingSymsAddr.end() && *TAI == Index) 1053 break; 1054 } 1055 } 1056 } 1057 1058 if (Index >= End) 1059 break; 1060 1061 if (DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), 1062 SectionAddr + Index, DebugOut, 1063 CommentStream)) { 1064 PIP.printInst(*IP, &Inst, 1065 Bytes.slice(Index, Size), 1066 SectionAddr + Index, outs(), "", *STI); 1067 outs() << CommentStream.str(); 1068 Comments.clear(); 1069 1070 // Try to resolve the target of a call, tail call, etc. to a specific 1071 // symbol. 1072 if (MIA && (MIA->isCall(Inst) || MIA->isUnconditionalBranch(Inst) || 1073 MIA->isConditionalBranch(Inst))) { 1074 uint64_t Target; 1075 if (MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target)) { 1076 // In a relocatable object, the target's section must reside in 1077 // the same section as the call instruction or it is accessed 1078 // through a relocation. 1079 // 1080 // In a non-relocatable object, the target may be in any section. 1081 // 1082 // N.B. We don't walk the relocations in the relocatable case yet. 1083 auto *TargetSectionSymbols = &Symbols; 1084 if (!Obj->isRelocatableObject()) { 1085 auto SectionAddress = std::upper_bound( 1086 SectionAddresses.begin(), SectionAddresses.end(), Target, 1087 [](uint64_t LHS, 1088 const std::pair<uint64_t, SectionRef> &RHS) { 1089 return LHS < RHS.first; 1090 }); 1091 if (SectionAddress != SectionAddresses.begin()) { 1092 --SectionAddress; 1093 TargetSectionSymbols = &AllSymbols[SectionAddress->second]; 1094 } else { 1095 TargetSectionSymbols = nullptr; 1096 } 1097 } 1098 1099 // Find the first symbol in the section whose offset is less than 1100 // or equal to the target. 1101 if (TargetSectionSymbols) { 1102 auto TargetSym = std::upper_bound( 1103 TargetSectionSymbols->begin(), TargetSectionSymbols->end(), 1104 Target, [](uint64_t LHS, 1105 const std::pair<uint64_t, StringRef> &RHS) { 1106 return LHS < RHS.first; 1107 }); 1108 if (TargetSym != TargetSectionSymbols->begin()) { 1109 --TargetSym; 1110 uint64_t TargetAddress = std::get<0>(*TargetSym); 1111 StringRef TargetName = std::get<1>(*TargetSym); 1112 outs() << " <" << TargetName; 1113 uint64_t Disp = Target - TargetAddress; 1114 if (Disp) 1115 outs() << '+' << utohexstr(Disp); 1116 outs() << '>'; 1117 } 1118 } 1119 } 1120 } 1121 outs() << "\n"; 1122 } else { 1123 errs() << ToolName << ": warning: invalid instruction encoding\n"; 1124 if (Size == 0) 1125 Size = 1; // skip illegible bytes 1126 } 1127 1128 // Print relocation for instruction. 1129 while (rel_cur != rel_end) { 1130 bool hidden = getHidden(*rel_cur); 1131 uint64_t addr = rel_cur->getOffset(); 1132 SmallString<16> name; 1133 SmallString<32> val; 1134 1135 // If this relocation is hidden, skip it. 1136 if (hidden) goto skip_print_rel; 1137 1138 // Stop when rel_cur's address is past the current instruction. 1139 if (addr >= Index + Size) break; 1140 rel_cur->getTypeName(name); 1141 error(getRelocationValueString(*rel_cur, val)); 1142 outs() << format(Fmt.data(), SectionAddr + addr) << name 1143 << "\t" << val << "\n"; 1144 1145 skip_print_rel: 1146 ++rel_cur; 1147 } 1148 } 1149 } 1150 } 1151} 1152 1153void llvm::PrintRelocations(const ObjectFile *Obj) { 1154 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 : 1155 "%08" PRIx64; 1156 // Regular objdump doesn't print relocations in non-relocatable object 1157 // files. 1158 if (!Obj->isRelocatableObject()) 1159 return; 1160 1161 for (const SectionRef &Section : ToolSectionFilter(*Obj)) { 1162 if (Section.relocation_begin() == Section.relocation_end()) 1163 continue; 1164 StringRef secname; 1165 error(Section.getName(secname)); 1166 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n"; 1167 for (const RelocationRef &Reloc : Section.relocations()) { 1168 bool hidden = getHidden(Reloc); 1169 uint64_t address = Reloc.getOffset(); 1170 SmallString<32> relocname; 1171 SmallString<32> valuestr; 1172 if (hidden) 1173 continue; 1174 Reloc.getTypeName(relocname); 1175 error(getRelocationValueString(Reloc, valuestr)); 1176 outs() << format(Fmt.data(), address) << " " << relocname << " " 1177 << valuestr << "\n"; 1178 } 1179 outs() << "\n"; 1180 } 1181} 1182 1183void llvm::PrintSectionHeaders(const ObjectFile *Obj) { 1184 outs() << "Sections:\n" 1185 "Idx Name Size Address Type\n"; 1186 unsigned i = 0; 1187 for (const SectionRef &Section : ToolSectionFilter(*Obj)) { 1188 StringRef Name; 1189 error(Section.getName(Name)); 1190 uint64_t Address = Section.getAddress(); 1191 uint64_t Size = Section.getSize(); 1192 bool Text = Section.isText(); 1193 bool Data = Section.isData(); 1194 bool BSS = Section.isBSS(); 1195 std::string Type = (std::string(Text ? "TEXT " : "") + 1196 (Data ? "DATA " : "") + (BSS ? "BSS" : "")); 1197 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i, 1198 Name.str().c_str(), Size, Address, Type.c_str()); 1199 ++i; 1200 } 1201} 1202 1203void llvm::PrintSectionContents(const ObjectFile *Obj) { 1204 std::error_code EC; 1205 for (const SectionRef &Section : ToolSectionFilter(*Obj)) { 1206 StringRef Name; 1207 StringRef Contents; 1208 error(Section.getName(Name)); 1209 uint64_t BaseAddr = Section.getAddress(); 1210 uint64_t Size = Section.getSize(); 1211 if (!Size) 1212 continue; 1213 1214 outs() << "Contents of section " << Name << ":\n"; 1215 if (Section.isBSS()) { 1216 outs() << format("<skipping contents of bss section at [%04" PRIx64 1217 ", %04" PRIx64 ")>\n", 1218 BaseAddr, BaseAddr + Size); 1219 continue; 1220 } 1221 1222 error(Section.getContents(Contents)); 1223 1224 // Dump out the content as hex and printable ascii characters. 1225 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) { 1226 outs() << format(" %04" PRIx64 " ", BaseAddr + addr); 1227 // Dump line of hex. 1228 for (std::size_t i = 0; i < 16; ++i) { 1229 if (i != 0 && i % 4 == 0) 1230 outs() << ' '; 1231 if (addr + i < end) 1232 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true) 1233 << hexdigit(Contents[addr + i] & 0xF, true); 1234 else 1235 outs() << " "; 1236 } 1237 // Print ascii. 1238 outs() << " "; 1239 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) { 1240 if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF)) 1241 outs() << Contents[addr + i]; 1242 else 1243 outs() << "."; 1244 } 1245 outs() << "\n"; 1246 } 1247 } 1248} 1249 1250void llvm::PrintSymbolTable(const ObjectFile *o) { 1251 outs() << "SYMBOL TABLE:\n"; 1252 1253 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) { 1254 printCOFFSymbolTable(coff); 1255 return; 1256 } 1257 for (const SymbolRef &Symbol : o->symbols()) { 1258 ErrorOr<uint64_t> AddressOrError = Symbol.getAddress(); 1259 error(AddressOrError.getError()); 1260 uint64_t Address = *AddressOrError; 1261 SymbolRef::Type Type = Symbol.getType(); 1262 uint32_t Flags = Symbol.getFlags(); 1263 ErrorOr<section_iterator> SectionOrErr = Symbol.getSection(); 1264 error(SectionOrErr.getError()); 1265 section_iterator Section = *SectionOrErr; 1266 StringRef Name; 1267 if (Type == SymbolRef::ST_Debug && Section != o->section_end()) { 1268 Section->getName(Name); 1269 } else { 1270 ErrorOr<StringRef> NameOrErr = Symbol.getName(); 1271 error(NameOrErr.getError()); 1272 Name = *NameOrErr; 1273 } 1274 1275 bool Global = Flags & SymbolRef::SF_Global; 1276 bool Weak = Flags & SymbolRef::SF_Weak; 1277 bool Absolute = Flags & SymbolRef::SF_Absolute; 1278 bool Common = Flags & SymbolRef::SF_Common; 1279 bool Hidden = Flags & SymbolRef::SF_Hidden; 1280 1281 char GlobLoc = ' '; 1282 if (Type != SymbolRef::ST_Unknown) 1283 GlobLoc = Global ? 'g' : 'l'; 1284 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File) 1285 ? 'd' : ' '; 1286 char FileFunc = ' '; 1287 if (Type == SymbolRef::ST_File) 1288 FileFunc = 'f'; 1289 else if (Type == SymbolRef::ST_Function) 1290 FileFunc = 'F'; 1291 1292 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 : 1293 "%08" PRIx64; 1294 1295 outs() << format(Fmt, Address) << " " 1296 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' ' 1297 << (Weak ? 'w' : ' ') // Weak? 1298 << ' ' // Constructor. Not supported yet. 1299 << ' ' // Warning. Not supported yet. 1300 << ' ' // Indirect reference to another symbol. 1301 << Debug // Debugging (d) or dynamic (D) symbol. 1302 << FileFunc // Name of function (F), file (f) or object (O). 1303 << ' '; 1304 if (Absolute) { 1305 outs() << "*ABS*"; 1306 } else if (Common) { 1307 outs() << "*COM*"; 1308 } else if (Section == o->section_end()) { 1309 outs() << "*UND*"; 1310 } else { 1311 if (const MachOObjectFile *MachO = 1312 dyn_cast<const MachOObjectFile>(o)) { 1313 DataRefImpl DR = Section->getRawDataRefImpl(); 1314 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR); 1315 outs() << SegmentName << ","; 1316 } 1317 StringRef SectionName; 1318 error(Section->getName(SectionName)); 1319 outs() << SectionName; 1320 } 1321 1322 outs() << '\t'; 1323 if (Common || isa<ELFObjectFileBase>(o)) { 1324 uint64_t Val = 1325 Common ? Symbol.getAlignment() : ELFSymbolRef(Symbol).getSize(); 1326 outs() << format("\t %08" PRIx64 " ", Val); 1327 } 1328 1329 if (Hidden) { 1330 outs() << ".hidden "; 1331 } 1332 outs() << Name 1333 << '\n'; 1334 } 1335} 1336 1337static void PrintUnwindInfo(const ObjectFile *o) { 1338 outs() << "Unwind info:\n\n"; 1339 1340 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) { 1341 printCOFFUnwindInfo(coff); 1342 } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 1343 printMachOUnwindInfo(MachO); 1344 else { 1345 // TODO: Extract DWARF dump tool to objdump. 1346 errs() << "This operation is only currently supported " 1347 "for COFF and MachO object files.\n"; 1348 return; 1349 } 1350} 1351 1352void llvm::printExportsTrie(const ObjectFile *o) { 1353 outs() << "Exports trie:\n"; 1354 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 1355 printMachOExportsTrie(MachO); 1356 else { 1357 errs() << "This operation is only currently supported " 1358 "for Mach-O executable files.\n"; 1359 return; 1360 } 1361} 1362 1363void llvm::printRebaseTable(const ObjectFile *o) { 1364 outs() << "Rebase table:\n"; 1365 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 1366 printMachORebaseTable(MachO); 1367 else { 1368 errs() << "This operation is only currently supported " 1369 "for Mach-O executable files.\n"; 1370 return; 1371 } 1372} 1373 1374void llvm::printBindTable(const ObjectFile *o) { 1375 outs() << "Bind table:\n"; 1376 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 1377 printMachOBindTable(MachO); 1378 else { 1379 errs() << "This operation is only currently supported " 1380 "for Mach-O executable files.\n"; 1381 return; 1382 } 1383} 1384 1385void llvm::printLazyBindTable(const ObjectFile *o) { 1386 outs() << "Lazy bind table:\n"; 1387 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 1388 printMachOLazyBindTable(MachO); 1389 else { 1390 errs() << "This operation is only currently supported " 1391 "for Mach-O executable files.\n"; 1392 return; 1393 } 1394} 1395 1396void llvm::printWeakBindTable(const ObjectFile *o) { 1397 outs() << "Weak bind table:\n"; 1398 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 1399 printMachOWeakBindTable(MachO); 1400 else { 1401 errs() << "This operation is only currently supported " 1402 "for Mach-O executable files.\n"; 1403 return; 1404 } 1405} 1406 1407/// Dump the raw contents of the __clangast section so the output can be piped 1408/// into llvm-bcanalyzer. 1409void llvm::printRawClangAST(const ObjectFile *Obj) { 1410 if (outs().is_displayed()) { 1411 errs() << "The -raw-clang-ast option will dump the raw binary contents of " 1412 "the clang ast section.\n" 1413 "Please redirect the output to a file or another program such as " 1414 "llvm-bcanalyzer.\n"; 1415 return; 1416 } 1417 1418 StringRef ClangASTSectionName("__clangast"); 1419 if (isa<COFFObjectFile>(Obj)) { 1420 ClangASTSectionName = "clangast"; 1421 } 1422 1423 Optional<object::SectionRef> ClangASTSection; 1424 for (auto Sec : ToolSectionFilter(*Obj)) { 1425 StringRef Name; 1426 Sec.getName(Name); 1427 if (Name == ClangASTSectionName) { 1428 ClangASTSection = Sec; 1429 break; 1430 } 1431 } 1432 if (!ClangASTSection) 1433 return; 1434 1435 StringRef ClangASTContents; 1436 error(ClangASTSection.getValue().getContents(ClangASTContents)); 1437 outs().write(ClangASTContents.data(), ClangASTContents.size()); 1438} 1439 1440static void printFaultMaps(const ObjectFile *Obj) { 1441 const char *FaultMapSectionName = nullptr; 1442 1443 if (isa<ELFObjectFileBase>(Obj)) { 1444 FaultMapSectionName = ".llvm_faultmaps"; 1445 } else if (isa<MachOObjectFile>(Obj)) { 1446 FaultMapSectionName = "__llvm_faultmaps"; 1447 } else { 1448 errs() << "This operation is only currently supported " 1449 "for ELF and Mach-O executable files.\n"; 1450 return; 1451 } 1452 1453 Optional<object::SectionRef> FaultMapSection; 1454 1455 for (auto Sec : ToolSectionFilter(*Obj)) { 1456 StringRef Name; 1457 Sec.getName(Name); 1458 if (Name == FaultMapSectionName) { 1459 FaultMapSection = Sec; 1460 break; 1461 } 1462 } 1463 1464 outs() << "FaultMap table:\n"; 1465 1466 if (!FaultMapSection.hasValue()) { 1467 outs() << "<not found>\n"; 1468 return; 1469 } 1470 1471 StringRef FaultMapContents; 1472 error(FaultMapSection.getValue().getContents(FaultMapContents)); 1473 1474 FaultMapParser FMP(FaultMapContents.bytes_begin(), 1475 FaultMapContents.bytes_end()); 1476 1477 outs() << FMP; 1478} 1479 1480static void printPrivateFileHeader(const ObjectFile *o) { 1481 if (o->isELF()) 1482 printELFFileHeader(o); 1483 else if (o->isCOFF()) 1484 printCOFFFileHeader(o); 1485 else if (o->isMachO()) 1486 printMachOFileHeader(o); 1487 else 1488 report_fatal_error("Invalid/Unsupported object file format"); 1489} 1490 1491static void DumpObject(const ObjectFile *o) { 1492 // Avoid other output when using a raw option. 1493 if (!RawClangAST) { 1494 outs() << '\n'; 1495 outs() << o->getFileName() 1496 << ":\tfile format " << o->getFileFormatName() << "\n\n"; 1497 } 1498 1499 if (Disassemble) 1500 DisassembleObject(o, Relocations); 1501 if (Relocations && !Disassemble) 1502 PrintRelocations(o); 1503 if (SectionHeaders) 1504 PrintSectionHeaders(o); 1505 if (SectionContents) 1506 PrintSectionContents(o); 1507 if (SymbolTable) 1508 PrintSymbolTable(o); 1509 if (UnwindInfo) 1510 PrintUnwindInfo(o); 1511 if (PrivateHeaders) 1512 printPrivateFileHeader(o); 1513 if (ExportsTrie) 1514 printExportsTrie(o); 1515 if (Rebase) 1516 printRebaseTable(o); 1517 if (Bind) 1518 printBindTable(o); 1519 if (LazyBind) 1520 printLazyBindTable(o); 1521 if (WeakBind) 1522 printWeakBindTable(o); 1523 if (RawClangAST) 1524 printRawClangAST(o); 1525 if (PrintFaultMaps) 1526 printFaultMaps(o); 1527} 1528 1529/// @brief Dump each object file in \a a; 1530static void DumpArchive(const Archive *a) { 1531 for (auto &ErrorOrChild : a->children()) { 1532 if (std::error_code EC = ErrorOrChild.getError()) 1533 report_error(a->getFileName(), EC); 1534 const Archive::Child &C = *ErrorOrChild; 1535 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary(); 1536 if (std::error_code EC = ChildOrErr.getError()) 1537 if (EC != object_error::invalid_file_type) 1538 report_error(a->getFileName(), EC); 1539 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) 1540 DumpObject(o); 1541 else 1542 report_error(a->getFileName(), object_error::invalid_file_type); 1543 } 1544} 1545 1546/// @brief Open file and figure out how to dump it. 1547static void DumpInput(StringRef file) { 1548 1549 // If we are using the Mach-O specific object file parser, then let it parse 1550 // the file and process the command line options. So the -arch flags can 1551 // be used to select specific slices, etc. 1552 if (MachOOpt) { 1553 ParseInputMachO(file); 1554 return; 1555 } 1556 1557 // Attempt to open the binary. 1558 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(file); 1559 if (std::error_code EC = BinaryOrErr.getError()) 1560 report_error(file, EC); 1561 Binary &Binary = *BinaryOrErr.get().getBinary(); 1562 1563 if (Archive *a = dyn_cast<Archive>(&Binary)) 1564 DumpArchive(a); 1565 else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary)) 1566 DumpObject(o); 1567 else 1568 report_error(file, object_error::invalid_file_type); 1569} 1570 1571int main(int argc, char **argv) { 1572 // Print a stack trace if we signal out. 1573 sys::PrintStackTraceOnErrorSignal(); 1574 PrettyStackTraceProgram X(argc, argv); 1575 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 1576 1577 // Initialize targets and assembly printers/parsers. 1578 llvm::InitializeAllTargetInfos(); 1579 llvm::InitializeAllTargetMCs(); 1580 llvm::InitializeAllDisassemblers(); 1581 1582 // Register the target printer for --version. 1583 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); 1584 1585 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n"); 1586 TripleName = Triple::normalize(TripleName); 1587 1588 ToolName = argv[0]; 1589 1590 // Defaults to a.out if no filenames specified. 1591 if (InputFilenames.size() == 0) 1592 InputFilenames.push_back("a.out"); 1593 1594 if (DisassembleAll) 1595 Disassemble = true; 1596 if (!Disassemble 1597 && !Relocations 1598 && !SectionHeaders 1599 && !SectionContents 1600 && !SymbolTable 1601 && !UnwindInfo 1602 && !PrivateHeaders 1603 && !ExportsTrie 1604 && !Rebase 1605 && !Bind 1606 && !LazyBind 1607 && !WeakBind 1608 && !RawClangAST 1609 && !(UniversalHeaders && MachOOpt) 1610 && !(ArchiveHeaders && MachOOpt) 1611 && !(IndirectSymbols && MachOOpt) 1612 && !(DataInCode && MachOOpt) 1613 && !(LinkOptHints && MachOOpt) 1614 && !(InfoPlist && MachOOpt) 1615 && !(DylibsUsed && MachOOpt) 1616 && !(DylibId && MachOOpt) 1617 && !(ObjcMetaData && MachOOpt) 1618 && !(FilterSections.size() != 0 && MachOOpt) 1619 && !PrintFaultMaps) { 1620 cl::PrintHelpMessage(); 1621 return 2; 1622 } 1623 1624 std::for_each(InputFilenames.begin(), InputFilenames.end(), 1625 DumpInput); 1626 1627 return EXIT_SUCCESS; 1628} 1629