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