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