1 //===-- MachODump.cpp - Object file dumping utility for llvm --------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the MachO-specific dumper for llvm-objdump. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm-objdump.h" 14 #include "llvm-c/Disassembler.h" 15 #include "llvm/ADT/STLExtras.h" 16 #include "llvm/ADT/StringExtras.h" 17 #include "llvm/ADT/Triple.h" 18 #include "llvm/BinaryFormat/MachO.h" 19 #include "llvm/Config/config.h" 20 #include "llvm/DebugInfo/DIContext.h" 21 #include "llvm/DebugInfo/DWARF/DWARFContext.h" 22 #include "llvm/Demangle/Demangle.h" 23 #include "llvm/MC/MCAsmInfo.h" 24 #include "llvm/MC/MCContext.h" 25 #include "llvm/MC/MCDisassembler/MCDisassembler.h" 26 #include "llvm/MC/MCInst.h" 27 #include "llvm/MC/MCInstPrinter.h" 28 #include "llvm/MC/MCInstrDesc.h" 29 #include "llvm/MC/MCInstrInfo.h" 30 #include "llvm/MC/MCRegisterInfo.h" 31 #include "llvm/MC/MCSubtargetInfo.h" 32 #include "llvm/Object/MachO.h" 33 #include "llvm/Object/MachOUniversal.h" 34 #include "llvm/Support/Casting.h" 35 #include "llvm/Support/CommandLine.h" 36 #include "llvm/Support/Debug.h" 37 #include "llvm/Support/Endian.h" 38 #include "llvm/Support/Format.h" 39 #include "llvm/Support/FormattedStream.h" 40 #include "llvm/Support/GraphWriter.h" 41 #include "llvm/Support/LEB128.h" 42 #include "llvm/Support/MemoryBuffer.h" 43 #include "llvm/Support/TargetRegistry.h" 44 #include "llvm/Support/TargetSelect.h" 45 #include "llvm/Support/ToolOutputFile.h" 46 #include "llvm/Support/WithColor.h" 47 #include "llvm/Support/raw_ostream.h" 48 #include <algorithm> 49 #include <cstring> 50 #include <system_error> 51 52 #ifdef HAVE_LIBXAR 53 extern "C" { 54 #include <xar/xar.h> 55 } 56 #endif 57 58 using namespace llvm::object; 59 60 namespace llvm { 61 62 cl::OptionCategory MachOCat("llvm-objdump MachO Specific Options"); 63 64 extern cl::opt<bool> ArchiveHeaders; 65 extern cl::opt<bool> Disassemble; 66 extern cl::opt<bool> DisassembleAll; 67 extern cl::opt<DIDumpType> DwarfDumpType; 68 extern cl::list<std::string> FilterSections; 69 extern cl::list<std::string> MAttrs; 70 extern cl::opt<std::string> MCPU; 71 extern cl::opt<bool> NoShowRawInsn; 72 extern cl::opt<bool> NoLeadingAddr; 73 extern cl::opt<bool> PrintImmHex; 74 extern cl::opt<bool> PrivateHeaders; 75 extern cl::opt<bool> Relocations; 76 extern cl::opt<bool> SectionHeaders; 77 extern cl::opt<bool> SectionContents; 78 extern cl::opt<bool> SymbolTable; 79 extern cl::opt<std::string> TripleName; 80 extern cl::opt<bool> UnwindInfo; 81 82 cl::opt<bool> 83 FirstPrivateHeader("private-header", 84 cl::desc("Display only the first format specific file " 85 "header"), 86 cl::cat(MachOCat)); 87 88 cl::opt<bool> ExportsTrie("exports-trie", 89 cl::desc("Display mach-o exported symbols"), 90 cl::cat(MachOCat)); 91 92 cl::opt<bool> Rebase("rebase", cl::desc("Display mach-o rebasing info"), 93 cl::cat(MachOCat)); 94 95 cl::opt<bool> Bind("bind", cl::desc("Display mach-o binding info"), 96 cl::cat(MachOCat)); 97 98 cl::opt<bool> LazyBind("lazy-bind", 99 cl::desc("Display mach-o lazy binding info"), 100 cl::cat(MachOCat)); 101 102 cl::opt<bool> WeakBind("weak-bind", 103 cl::desc("Display mach-o weak binding info"), 104 cl::cat(MachOCat)); 105 106 static cl::opt<bool> 107 UseDbg("g", cl::Grouping, 108 cl::desc("Print line information from debug info if available"), 109 cl::cat(MachOCat)); 110 111 static cl::opt<std::string> DSYMFile("dsym", 112 cl::desc("Use .dSYM file for debug info"), 113 cl::cat(MachOCat)); 114 115 static cl::opt<bool> FullLeadingAddr("full-leading-addr", 116 cl::desc("Print full leading address"), 117 cl::cat(MachOCat)); 118 119 static cl::opt<bool> NoLeadingHeaders("no-leading-headers", 120 cl::desc("Print no leading headers"), 121 cl::cat(MachOCat)); 122 123 cl::opt<bool> UniversalHeaders("universal-headers", 124 cl::desc("Print Mach-O universal headers " 125 "(requires -macho)"), 126 cl::cat(MachOCat)); 127 128 cl::opt<bool> 129 ArchiveMemberOffsets("archive-member-offsets", 130 cl::desc("Print the offset to each archive member for " 131 "Mach-O archives (requires -macho and " 132 "-archive-headers)"), 133 cl::cat(MachOCat)); 134 135 cl::opt<bool> IndirectSymbols("indirect-symbols", 136 cl::desc("Print indirect symbol table for Mach-O " 137 "objects (requires -macho)"), 138 cl::cat(MachOCat)); 139 140 cl::opt<bool> 141 DataInCode("data-in-code", 142 cl::desc("Print the data in code table for Mach-O objects " 143 "(requires -macho)"), 144 cl::cat(MachOCat)); 145 146 cl::opt<bool> LinkOptHints("link-opt-hints", 147 cl::desc("Print the linker optimization hints for " 148 "Mach-O objects (requires -macho)"), 149 cl::cat(MachOCat)); 150 151 cl::opt<bool> InfoPlist("info-plist", 152 cl::desc("Print the info plist section as strings for " 153 "Mach-O objects (requires -macho)"), 154 cl::cat(MachOCat)); 155 156 cl::opt<bool> DylibsUsed("dylibs-used", 157 cl::desc("Print the shared libraries used for linked " 158 "Mach-O files (requires -macho)"), 159 cl::cat(MachOCat)); 160 161 cl::opt<bool> 162 DylibId("dylib-id", 163 cl::desc("Print the shared library's id for the dylib Mach-O " 164 "file (requires -macho)"), 165 cl::cat(MachOCat)); 166 167 cl::opt<bool> 168 NonVerbose("non-verbose", 169 cl::desc("Print the info for Mach-O objects in " 170 "non-verbose or numeric form (requires -macho)"), 171 cl::cat(MachOCat)); 172 173 cl::opt<bool> 174 ObjcMetaData("objc-meta-data", 175 cl::desc("Print the Objective-C runtime meta data for " 176 "Mach-O files (requires -macho)"), 177 cl::cat(MachOCat)); 178 179 cl::opt<std::string> DisSymName( 180 "dis-symname", 181 cl::desc("disassemble just this symbol's instructions (requires -macho)"), 182 cl::cat(MachOCat)); 183 184 static cl::opt<bool> NoSymbolicOperands( 185 "no-symbolic-operands", 186 cl::desc("do not symbolic operands when disassembling (requires -macho)"), 187 cl::cat(MachOCat)); 188 189 static cl::list<std::string> 190 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"), 191 cl::ZeroOrMore, cl::cat(MachOCat)); 192 193 bool ArchAll = false; 194 195 static std::string ThumbTripleName; 196 197 static const Target *GetTarget(const MachOObjectFile *MachOObj, 198 const char **McpuDefault, 199 const Target **ThumbTarget) { 200 // Figure out the target triple. 201 Triple TT(TripleName); 202 if (TripleName.empty()) { 203 TT = MachOObj->getArchTriple(McpuDefault); 204 TripleName = TT.str(); 205 } 206 207 if (TT.getArch() == Triple::arm) { 208 // We've inferred a 32-bit ARM target from the object file. All MachO CPUs 209 // that support ARM are also capable of Thumb mode. 210 Triple ThumbTriple = TT; 211 std::string ThumbName = (Twine("thumb") + TT.getArchName().substr(3)).str(); 212 ThumbTriple.setArchName(ThumbName); 213 ThumbTripleName = ThumbTriple.str(); 214 } 215 216 // Get the target specific parser. 217 std::string Error; 218 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); 219 if (TheTarget && ThumbTripleName.empty()) 220 return TheTarget; 221 222 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error); 223 if (*ThumbTarget) 224 return TheTarget; 225 226 WithColor::error(errs(), "llvm-objdump") << "unable to get target for '"; 227 if (!TheTarget) 228 errs() << TripleName; 229 else 230 errs() << ThumbTripleName; 231 errs() << "', see --version and --triple.\n"; 232 return nullptr; 233 } 234 235 struct SymbolSorter { 236 bool operator()(const SymbolRef &A, const SymbolRef &B) { 237 Expected<SymbolRef::Type> ATypeOrErr = A.getType(); 238 if (!ATypeOrErr) 239 report_error(ATypeOrErr.takeError(), A.getObject()->getFileName()); 240 SymbolRef::Type AType = *ATypeOrErr; 241 Expected<SymbolRef::Type> BTypeOrErr = B.getType(); 242 if (!BTypeOrErr) 243 report_error(BTypeOrErr.takeError(), B.getObject()->getFileName()); 244 SymbolRef::Type BType = *BTypeOrErr; 245 uint64_t AAddr = (AType != SymbolRef::ST_Function) ? 0 : A.getValue(); 246 uint64_t BAddr = (BType != SymbolRef::ST_Function) ? 0 : B.getValue(); 247 return AAddr < BAddr; 248 } 249 }; 250 251 // Types for the storted data in code table that is built before disassembly 252 // and the predicate function to sort them. 253 typedef std::pair<uint64_t, DiceRef> DiceTableEntry; 254 typedef std::vector<DiceTableEntry> DiceTable; 255 typedef DiceTable::iterator dice_table_iterator; 256 257 #ifdef HAVE_LIBXAR 258 namespace { 259 struct ScopedXarFile { 260 xar_t xar; 261 ScopedXarFile(const char *filename, int32_t flags) 262 : xar(xar_open(filename, flags)) {} 263 ~ScopedXarFile() { 264 if (xar) 265 xar_close(xar); 266 } 267 ScopedXarFile(const ScopedXarFile &) = delete; 268 ScopedXarFile &operator=(const ScopedXarFile &) = delete; 269 operator xar_t() { return xar; } 270 }; 271 272 struct ScopedXarIter { 273 xar_iter_t iter; 274 ScopedXarIter() : iter(xar_iter_new()) {} 275 ~ScopedXarIter() { 276 if (iter) 277 xar_iter_free(iter); 278 } 279 ScopedXarIter(const ScopedXarIter &) = delete; 280 ScopedXarIter &operator=(const ScopedXarIter &) = delete; 281 operator xar_iter_t() { return iter; } 282 }; 283 } // namespace 284 #endif // defined(HAVE_LIBXAR) 285 286 // This is used to search for a data in code table entry for the PC being 287 // disassembled. The j parameter has the PC in j.first. A single data in code 288 // table entry can cover many bytes for each of its Kind's. So if the offset, 289 // aka the i.first value, of the data in code table entry plus its Length 290 // covers the PC being searched for this will return true. If not it will 291 // return false. 292 static bool compareDiceTableEntries(const DiceTableEntry &i, 293 const DiceTableEntry &j) { 294 uint16_t Length; 295 i.second.getLength(Length); 296 297 return j.first >= i.first && j.first < i.first + Length; 298 } 299 300 static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length, 301 unsigned short Kind) { 302 uint32_t Value, Size = 1; 303 304 switch (Kind) { 305 default: 306 case MachO::DICE_KIND_DATA: 307 if (Length >= 4) { 308 if (!NoShowRawInsn) 309 dumpBytes(makeArrayRef(bytes, 4), outs()); 310 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0]; 311 outs() << "\t.long " << Value; 312 Size = 4; 313 } else if (Length >= 2) { 314 if (!NoShowRawInsn) 315 dumpBytes(makeArrayRef(bytes, 2), outs()); 316 Value = bytes[1] << 8 | bytes[0]; 317 outs() << "\t.short " << Value; 318 Size = 2; 319 } else { 320 if (!NoShowRawInsn) 321 dumpBytes(makeArrayRef(bytes, 2), outs()); 322 Value = bytes[0]; 323 outs() << "\t.byte " << Value; 324 Size = 1; 325 } 326 if (Kind == MachO::DICE_KIND_DATA) 327 outs() << "\t@ KIND_DATA\n"; 328 else 329 outs() << "\t@ data in code kind = " << Kind << "\n"; 330 break; 331 case MachO::DICE_KIND_JUMP_TABLE8: 332 if (!NoShowRawInsn) 333 dumpBytes(makeArrayRef(bytes, 1), outs()); 334 Value = bytes[0]; 335 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n"; 336 Size = 1; 337 break; 338 case MachO::DICE_KIND_JUMP_TABLE16: 339 if (!NoShowRawInsn) 340 dumpBytes(makeArrayRef(bytes, 2), outs()); 341 Value = bytes[1] << 8 | bytes[0]; 342 outs() << "\t.short " << format("%5u", Value & 0xffff) 343 << "\t@ KIND_JUMP_TABLE16\n"; 344 Size = 2; 345 break; 346 case MachO::DICE_KIND_JUMP_TABLE32: 347 case MachO::DICE_KIND_ABS_JUMP_TABLE32: 348 if (!NoShowRawInsn) 349 dumpBytes(makeArrayRef(bytes, 4), outs()); 350 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0]; 351 outs() << "\t.long " << Value; 352 if (Kind == MachO::DICE_KIND_JUMP_TABLE32) 353 outs() << "\t@ KIND_JUMP_TABLE32\n"; 354 else 355 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n"; 356 Size = 4; 357 break; 358 } 359 return Size; 360 } 361 362 static void getSectionsAndSymbols(MachOObjectFile *MachOObj, 363 std::vector<SectionRef> &Sections, 364 std::vector<SymbolRef> &Symbols, 365 SmallVectorImpl<uint64_t> &FoundFns, 366 uint64_t &BaseSegmentAddress) { 367 const StringRef FileName = MachOObj->getFileName(); 368 for (const SymbolRef &Symbol : MachOObj->symbols()) { 369 StringRef SymName = unwrapOrError(Symbol.getName(), FileName); 370 if (!SymName.startswith("ltmp")) 371 Symbols.push_back(Symbol); 372 } 373 374 for (const SectionRef &Section : MachOObj->sections()) 375 Sections.push_back(Section); 376 377 bool BaseSegmentAddressSet = false; 378 for (const auto &Command : MachOObj->load_commands()) { 379 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) { 380 // We found a function starts segment, parse the addresses for later 381 // consumption. 382 MachO::linkedit_data_command LLC = 383 MachOObj->getLinkeditDataLoadCommand(Command); 384 385 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns); 386 } else if (Command.C.cmd == MachO::LC_SEGMENT) { 387 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command); 388 StringRef SegName = SLC.segname; 389 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") { 390 BaseSegmentAddressSet = true; 391 BaseSegmentAddress = SLC.vmaddr; 392 } 393 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) { 394 MachO::segment_command_64 SLC = MachOObj->getSegment64LoadCommand(Command); 395 StringRef SegName = SLC.segname; 396 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") { 397 BaseSegmentAddressSet = true; 398 BaseSegmentAddress = SLC.vmaddr; 399 } 400 } 401 } 402 } 403 404 static bool DumpAndSkipDataInCode(uint64_t PC, const uint8_t *bytes, 405 DiceTable &Dices, uint64_t &InstSize) { 406 // Check the data in code table here to see if this is data not an 407 // instruction to be disassembled. 408 DiceTable Dice; 409 Dice.push_back(std::make_pair(PC, DiceRef())); 410 dice_table_iterator DTI = 411 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(), 412 compareDiceTableEntries); 413 if (DTI != Dices.end()) { 414 uint16_t Length; 415 DTI->second.getLength(Length); 416 uint16_t Kind; 417 DTI->second.getKind(Kind); 418 InstSize = DumpDataInCode(bytes, Length, Kind); 419 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) && 420 (PC == (DTI->first + Length - 1)) && (Length & 1)) 421 InstSize++; 422 return true; 423 } 424 return false; 425 } 426 427 static void printRelocationTargetName(const MachOObjectFile *O, 428 const MachO::any_relocation_info &RE, 429 raw_string_ostream &Fmt) { 430 // Target of a scattered relocation is an address. In the interest of 431 // generating pretty output, scan through the symbol table looking for a 432 // symbol that aligns with that address. If we find one, print it. 433 // Otherwise, we just print the hex address of the target. 434 const StringRef FileName = O->getFileName(); 435 if (O->isRelocationScattered(RE)) { 436 uint32_t Val = O->getPlainRelocationSymbolNum(RE); 437 438 for (const SymbolRef &Symbol : O->symbols()) { 439 uint64_t Addr = unwrapOrError(Symbol.getAddress(), FileName); 440 if (Addr != Val) 441 continue; 442 Fmt << unwrapOrError(Symbol.getName(), FileName); 443 return; 444 } 445 446 // If we couldn't find a symbol that this relocation refers to, try 447 // to find a section beginning instead. 448 for (const SectionRef &Section : ToolSectionFilter(*O)) { 449 uint64_t Addr = Section.getAddress(); 450 if (Addr != Val) 451 continue; 452 StringRef NameOrErr = unwrapOrError(Section.getName(), O->getFileName()); 453 Fmt << NameOrErr; 454 return; 455 } 456 457 Fmt << format("0x%x", Val); 458 return; 459 } 460 461 StringRef S; 462 bool isExtern = O->getPlainRelocationExternal(RE); 463 uint64_t Val = O->getPlainRelocationSymbolNum(RE); 464 465 if (O->getAnyRelocationType(RE) == MachO::ARM64_RELOC_ADDEND) { 466 Fmt << format("0x%0" PRIx64, Val); 467 return; 468 } 469 470 if (isExtern) { 471 symbol_iterator SI = O->symbol_begin(); 472 advance(SI, Val); 473 S = unwrapOrError(SI->getName(), FileName); 474 } else { 475 section_iterator SI = O->section_begin(); 476 // Adjust for the fact that sections are 1-indexed. 477 if (Val == 0) { 478 Fmt << "0 (?,?)"; 479 return; 480 } 481 uint32_t I = Val - 1; 482 while (I != 0 && SI != O->section_end()) { 483 --I; 484 advance(SI, 1); 485 } 486 if (SI == O->section_end()) { 487 Fmt << Val << " (?,?)"; 488 } else { 489 if (Expected<StringRef> NameOrErr = SI->getName()) 490 S = *NameOrErr; 491 else 492 consumeError(NameOrErr.takeError()); 493 } 494 } 495 496 Fmt << S; 497 } 498 499 Error getMachORelocationValueString(const MachOObjectFile *Obj, 500 const RelocationRef &RelRef, 501 SmallVectorImpl<char> &Result) { 502 DataRefImpl Rel = RelRef.getRawDataRefImpl(); 503 MachO::any_relocation_info RE = Obj->getRelocation(Rel); 504 505 unsigned Arch = Obj->getArch(); 506 507 std::string FmtBuf; 508 raw_string_ostream Fmt(FmtBuf); 509 unsigned Type = Obj->getAnyRelocationType(RE); 510 bool IsPCRel = Obj->getAnyRelocationPCRel(RE); 511 512 // Determine any addends that should be displayed with the relocation. 513 // These require decoding the relocation type, which is triple-specific. 514 515 // X86_64 has entirely custom relocation types. 516 if (Arch == Triple::x86_64) { 517 switch (Type) { 518 case MachO::X86_64_RELOC_GOT_LOAD: 519 case MachO::X86_64_RELOC_GOT: { 520 printRelocationTargetName(Obj, RE, Fmt); 521 Fmt << "@GOT"; 522 if (IsPCRel) 523 Fmt << "PCREL"; 524 break; 525 } 526 case MachO::X86_64_RELOC_SUBTRACTOR: { 527 DataRefImpl RelNext = Rel; 528 Obj->moveRelocationNext(RelNext); 529 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext); 530 531 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type 532 // X86_64_RELOC_UNSIGNED. 533 // NOTE: Scattered relocations don't exist on x86_64. 534 unsigned RType = Obj->getAnyRelocationType(RENext); 535 if (RType != MachO::X86_64_RELOC_UNSIGNED) 536 report_error(Obj->getFileName(), "Expected X86_64_RELOC_UNSIGNED after " 537 "X86_64_RELOC_SUBTRACTOR."); 538 539 // The X86_64_RELOC_UNSIGNED contains the minuend symbol; 540 // X86_64_RELOC_SUBTRACTOR contains the subtrahend. 541 printRelocationTargetName(Obj, RENext, Fmt); 542 Fmt << "-"; 543 printRelocationTargetName(Obj, RE, Fmt); 544 break; 545 } 546 case MachO::X86_64_RELOC_TLV: 547 printRelocationTargetName(Obj, RE, Fmt); 548 Fmt << "@TLV"; 549 if (IsPCRel) 550 Fmt << "P"; 551 break; 552 case MachO::X86_64_RELOC_SIGNED_1: 553 printRelocationTargetName(Obj, RE, Fmt); 554 Fmt << "-1"; 555 break; 556 case MachO::X86_64_RELOC_SIGNED_2: 557 printRelocationTargetName(Obj, RE, Fmt); 558 Fmt << "-2"; 559 break; 560 case MachO::X86_64_RELOC_SIGNED_4: 561 printRelocationTargetName(Obj, RE, Fmt); 562 Fmt << "-4"; 563 break; 564 default: 565 printRelocationTargetName(Obj, RE, Fmt); 566 break; 567 } 568 // X86 and ARM share some relocation types in common. 569 } else if (Arch == Triple::x86 || Arch == Triple::arm || 570 Arch == Triple::ppc) { 571 // Generic relocation types... 572 switch (Type) { 573 case MachO::GENERIC_RELOC_PAIR: // prints no info 574 return Error::success(); 575 case MachO::GENERIC_RELOC_SECTDIFF: { 576 DataRefImpl RelNext = Rel; 577 Obj->moveRelocationNext(RelNext); 578 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext); 579 580 // X86 sect diff's must be followed by a relocation of type 581 // GENERIC_RELOC_PAIR. 582 unsigned RType = Obj->getAnyRelocationType(RENext); 583 584 if (RType != MachO::GENERIC_RELOC_PAIR) 585 report_error(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after " 586 "GENERIC_RELOC_SECTDIFF."); 587 588 printRelocationTargetName(Obj, RE, Fmt); 589 Fmt << "-"; 590 printRelocationTargetName(Obj, RENext, Fmt); 591 break; 592 } 593 } 594 595 if (Arch == Triple::x86 || Arch == Triple::ppc) { 596 switch (Type) { 597 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: { 598 DataRefImpl RelNext = Rel; 599 Obj->moveRelocationNext(RelNext); 600 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext); 601 602 // X86 sect diff's must be followed by a relocation of type 603 // GENERIC_RELOC_PAIR. 604 unsigned RType = Obj->getAnyRelocationType(RENext); 605 if (RType != MachO::GENERIC_RELOC_PAIR) 606 report_error(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after " 607 "GENERIC_RELOC_LOCAL_SECTDIFF."); 608 609 printRelocationTargetName(Obj, RE, Fmt); 610 Fmt << "-"; 611 printRelocationTargetName(Obj, RENext, Fmt); 612 break; 613 } 614 case MachO::GENERIC_RELOC_TLV: { 615 printRelocationTargetName(Obj, RE, Fmt); 616 Fmt << "@TLV"; 617 if (IsPCRel) 618 Fmt << "P"; 619 break; 620 } 621 default: 622 printRelocationTargetName(Obj, RE, Fmt); 623 } 624 } else { // ARM-specific relocations 625 switch (Type) { 626 case MachO::ARM_RELOC_HALF: 627 case MachO::ARM_RELOC_HALF_SECTDIFF: { 628 // Half relocations steal a bit from the length field to encode 629 // whether this is an upper16 or a lower16 relocation. 630 bool isUpper = (Obj->getAnyRelocationLength(RE) & 0x1) == 1; 631 632 if (isUpper) 633 Fmt << ":upper16:("; 634 else 635 Fmt << ":lower16:("; 636 printRelocationTargetName(Obj, RE, Fmt); 637 638 DataRefImpl RelNext = Rel; 639 Obj->moveRelocationNext(RelNext); 640 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext); 641 642 // ARM half relocs must be followed by a relocation of type 643 // ARM_RELOC_PAIR. 644 unsigned RType = Obj->getAnyRelocationType(RENext); 645 if (RType != MachO::ARM_RELOC_PAIR) 646 report_error(Obj->getFileName(), "Expected ARM_RELOC_PAIR after " 647 "ARM_RELOC_HALF"); 648 649 // NOTE: The half of the target virtual address is stashed in the 650 // address field of the secondary relocation, but we can't reverse 651 // engineer the constant offset from it without decoding the movw/movt 652 // instruction to find the other half in its immediate field. 653 654 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the 655 // symbol/section pointer of the follow-on relocation. 656 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) { 657 Fmt << "-"; 658 printRelocationTargetName(Obj, RENext, Fmt); 659 } 660 661 Fmt << ")"; 662 break; 663 } 664 default: { 665 printRelocationTargetName(Obj, RE, Fmt); 666 } 667 } 668 } 669 } else 670 printRelocationTargetName(Obj, RE, Fmt); 671 672 Fmt.flush(); 673 Result.append(FmtBuf.begin(), FmtBuf.end()); 674 return Error::success(); 675 } 676 677 static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose, 678 uint32_t n, uint32_t count, 679 uint32_t stride, uint64_t addr) { 680 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand(); 681 uint32_t nindirectsyms = Dysymtab.nindirectsyms; 682 if (n > nindirectsyms) 683 outs() << " (entries start past the end of the indirect symbol " 684 "table) (reserved1 field greater than the table size)"; 685 else if (n + count > nindirectsyms) 686 outs() << " (entries extends past the end of the indirect symbol " 687 "table)"; 688 outs() << "\n"; 689 uint32_t cputype = O->getHeader().cputype; 690 if (cputype & MachO::CPU_ARCH_ABI64) 691 outs() << "address index"; 692 else 693 outs() << "address index"; 694 if (verbose) 695 outs() << " name\n"; 696 else 697 outs() << "\n"; 698 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) { 699 if (cputype & MachO::CPU_ARCH_ABI64) 700 outs() << format("0x%016" PRIx64, addr + j * stride) << " "; 701 else 702 outs() << format("0x%08" PRIx32, (uint32_t)addr + j * stride) << " "; 703 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand(); 704 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j); 705 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) { 706 outs() << "LOCAL\n"; 707 continue; 708 } 709 if (indirect_symbol == 710 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) { 711 outs() << "LOCAL ABSOLUTE\n"; 712 continue; 713 } 714 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) { 715 outs() << "ABSOLUTE\n"; 716 continue; 717 } 718 outs() << format("%5u ", indirect_symbol); 719 if (verbose) { 720 MachO::symtab_command Symtab = O->getSymtabLoadCommand(); 721 if (indirect_symbol < Symtab.nsyms) { 722 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol); 723 SymbolRef Symbol = *Sym; 724 outs() << unwrapOrError(Symbol.getName(), O->getFileName()); 725 } else { 726 outs() << "?"; 727 } 728 } 729 outs() << "\n"; 730 } 731 } 732 733 static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) { 734 for (const auto &Load : O->load_commands()) { 735 if (Load.C.cmd == MachO::LC_SEGMENT_64) { 736 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load); 737 for (unsigned J = 0; J < Seg.nsects; ++J) { 738 MachO::section_64 Sec = O->getSection64(Load, J); 739 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE; 740 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS || 741 section_type == MachO::S_LAZY_SYMBOL_POINTERS || 742 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS || 743 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS || 744 section_type == MachO::S_SYMBOL_STUBS) { 745 uint32_t stride; 746 if (section_type == MachO::S_SYMBOL_STUBS) 747 stride = Sec.reserved2; 748 else 749 stride = 8; 750 if (stride == 0) { 751 outs() << "Can't print indirect symbols for (" << Sec.segname << "," 752 << Sec.sectname << ") " 753 << "(size of stubs in reserved2 field is zero)\n"; 754 continue; 755 } 756 uint32_t count = Sec.size / stride; 757 outs() << "Indirect symbols for (" << Sec.segname << "," 758 << Sec.sectname << ") " << count << " entries"; 759 uint32_t n = Sec.reserved1; 760 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr); 761 } 762 } 763 } else if (Load.C.cmd == MachO::LC_SEGMENT) { 764 MachO::segment_command Seg = O->getSegmentLoadCommand(Load); 765 for (unsigned J = 0; J < Seg.nsects; ++J) { 766 MachO::section Sec = O->getSection(Load, J); 767 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE; 768 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS || 769 section_type == MachO::S_LAZY_SYMBOL_POINTERS || 770 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS || 771 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS || 772 section_type == MachO::S_SYMBOL_STUBS) { 773 uint32_t stride; 774 if (section_type == MachO::S_SYMBOL_STUBS) 775 stride = Sec.reserved2; 776 else 777 stride = 4; 778 if (stride == 0) { 779 outs() << "Can't print indirect symbols for (" << Sec.segname << "," 780 << Sec.sectname << ") " 781 << "(size of stubs in reserved2 field is zero)\n"; 782 continue; 783 } 784 uint32_t count = Sec.size / stride; 785 outs() << "Indirect symbols for (" << Sec.segname << "," 786 << Sec.sectname << ") " << count << " entries"; 787 uint32_t n = Sec.reserved1; 788 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr); 789 } 790 } 791 } 792 } 793 } 794 795 static void PrintRType(const uint64_t cputype, const unsigned r_type) { 796 static char const *generic_r_types[] = { 797 "VANILLA ", "PAIR ", "SECTDIF ", "PBLAPTR ", "LOCSDIF ", "TLV ", 798 " 6 (?) ", " 7 (?) ", " 8 (?) ", " 9 (?) ", " 10 (?) ", " 11 (?) ", 799 " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) " 800 }; 801 static char const *x86_64_r_types[] = { 802 "UNSIGND ", "SIGNED ", "BRANCH ", "GOT_LD ", "GOT ", "SUB ", 803 "SIGNED1 ", "SIGNED2 ", "SIGNED4 ", "TLV ", " 10 (?) ", " 11 (?) ", 804 " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) " 805 }; 806 static char const *arm_r_types[] = { 807 "VANILLA ", "PAIR ", "SECTDIFF", "LOCSDIF ", "PBLAPTR ", 808 "BR24 ", "T_BR22 ", "T_BR32 ", "HALF ", "HALFDIF ", 809 " 10 (?) ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) " 810 }; 811 static char const *arm64_r_types[] = { 812 "UNSIGND ", "SUB ", "BR26 ", "PAGE21 ", "PAGOF12 ", 813 "GOTLDP ", "GOTLDPOF", "PTRTGOT ", "TLVLDP ", "TLVLDPOF", 814 "ADDEND ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) " 815 }; 816 817 if (r_type > 0xf){ 818 outs() << format("%-7u", r_type) << " "; 819 return; 820 } 821 switch (cputype) { 822 case MachO::CPU_TYPE_I386: 823 outs() << generic_r_types[r_type]; 824 break; 825 case MachO::CPU_TYPE_X86_64: 826 outs() << x86_64_r_types[r_type]; 827 break; 828 case MachO::CPU_TYPE_ARM: 829 outs() << arm_r_types[r_type]; 830 break; 831 case MachO::CPU_TYPE_ARM64: 832 case MachO::CPU_TYPE_ARM64_32: 833 outs() << arm64_r_types[r_type]; 834 break; 835 default: 836 outs() << format("%-7u ", r_type); 837 } 838 } 839 840 static void PrintRLength(const uint64_t cputype, const unsigned r_type, 841 const unsigned r_length, const bool previous_arm_half){ 842 if (cputype == MachO::CPU_TYPE_ARM && 843 (r_type == MachO::ARM_RELOC_HALF || 844 r_type == MachO::ARM_RELOC_HALF_SECTDIFF || previous_arm_half == true)) { 845 if ((r_length & 0x1) == 0) 846 outs() << "lo/"; 847 else 848 outs() << "hi/"; 849 if ((r_length & 0x1) == 0) 850 outs() << "arm "; 851 else 852 outs() << "thm "; 853 } else { 854 switch (r_length) { 855 case 0: 856 outs() << "byte "; 857 break; 858 case 1: 859 outs() << "word "; 860 break; 861 case 2: 862 outs() << "long "; 863 break; 864 case 3: 865 if (cputype == MachO::CPU_TYPE_X86_64) 866 outs() << "quad "; 867 else 868 outs() << format("?(%2d) ", r_length); 869 break; 870 default: 871 outs() << format("?(%2d) ", r_length); 872 } 873 } 874 } 875 876 static void PrintRelocationEntries(const MachOObjectFile *O, 877 const relocation_iterator Begin, 878 const relocation_iterator End, 879 const uint64_t cputype, 880 const bool verbose) { 881 const MachO::symtab_command Symtab = O->getSymtabLoadCommand(); 882 bool previous_arm_half = false; 883 bool previous_sectdiff = false; 884 uint32_t sectdiff_r_type = 0; 885 886 for (relocation_iterator Reloc = Begin; Reloc != End; ++Reloc) { 887 const DataRefImpl Rel = Reloc->getRawDataRefImpl(); 888 const MachO::any_relocation_info RE = O->getRelocation(Rel); 889 const unsigned r_type = O->getAnyRelocationType(RE); 890 const bool r_scattered = O->isRelocationScattered(RE); 891 const unsigned r_pcrel = O->getAnyRelocationPCRel(RE); 892 const unsigned r_length = O->getAnyRelocationLength(RE); 893 const unsigned r_address = O->getAnyRelocationAddress(RE); 894 const bool r_extern = (r_scattered ? false : 895 O->getPlainRelocationExternal(RE)); 896 const uint32_t r_value = (r_scattered ? 897 O->getScatteredRelocationValue(RE) : 0); 898 const unsigned r_symbolnum = (r_scattered ? 0 : 899 O->getPlainRelocationSymbolNum(RE)); 900 901 if (r_scattered && cputype != MachO::CPU_TYPE_X86_64) { 902 if (verbose) { 903 // scattered: address 904 if ((cputype == MachO::CPU_TYPE_I386 && 905 r_type == MachO::GENERIC_RELOC_PAIR) || 906 (cputype == MachO::CPU_TYPE_ARM && r_type == MachO::ARM_RELOC_PAIR)) 907 outs() << " "; 908 else 909 outs() << format("%08x ", (unsigned int)r_address); 910 911 // scattered: pcrel 912 if (r_pcrel) 913 outs() << "True "; 914 else 915 outs() << "False "; 916 917 // scattered: length 918 PrintRLength(cputype, r_type, r_length, previous_arm_half); 919 920 // scattered: extern & type 921 outs() << "n/a "; 922 PrintRType(cputype, r_type); 923 924 // scattered: scattered & value 925 outs() << format("True 0x%08x", (unsigned int)r_value); 926 if (previous_sectdiff == false) { 927 if ((cputype == MachO::CPU_TYPE_ARM && 928 r_type == MachO::ARM_RELOC_PAIR)) 929 outs() << format(" half = 0x%04x ", (unsigned int)r_address); 930 } else if (cputype == MachO::CPU_TYPE_ARM && 931 sectdiff_r_type == MachO::ARM_RELOC_HALF_SECTDIFF) 932 outs() << format(" other_half = 0x%04x ", (unsigned int)r_address); 933 if ((cputype == MachO::CPU_TYPE_I386 && 934 (r_type == MachO::GENERIC_RELOC_SECTDIFF || 935 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) || 936 (cputype == MachO::CPU_TYPE_ARM && 937 (sectdiff_r_type == MachO::ARM_RELOC_SECTDIFF || 938 sectdiff_r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF || 939 sectdiff_r_type == MachO::ARM_RELOC_HALF_SECTDIFF))) { 940 previous_sectdiff = true; 941 sectdiff_r_type = r_type; 942 } else { 943 previous_sectdiff = false; 944 sectdiff_r_type = 0; 945 } 946 if (cputype == MachO::CPU_TYPE_ARM && 947 (r_type == MachO::ARM_RELOC_HALF || 948 r_type == MachO::ARM_RELOC_HALF_SECTDIFF)) 949 previous_arm_half = true; 950 else 951 previous_arm_half = false; 952 outs() << "\n"; 953 } 954 else { 955 // scattered: address pcrel length extern type scattered value 956 outs() << format("%08x %1d %-2d n/a %-7d 1 0x%08x\n", 957 (unsigned int)r_address, r_pcrel, r_length, r_type, 958 (unsigned int)r_value); 959 } 960 } 961 else { 962 if (verbose) { 963 // plain: address 964 if (cputype == MachO::CPU_TYPE_ARM && r_type == MachO::ARM_RELOC_PAIR) 965 outs() << " "; 966 else 967 outs() << format("%08x ", (unsigned int)r_address); 968 969 // plain: pcrel 970 if (r_pcrel) 971 outs() << "True "; 972 else 973 outs() << "False "; 974 975 // plain: length 976 PrintRLength(cputype, r_type, r_length, previous_arm_half); 977 978 if (r_extern) { 979 // plain: extern & type & scattered 980 outs() << "True "; 981 PrintRType(cputype, r_type); 982 outs() << "False "; 983 984 // plain: symbolnum/value 985 if (r_symbolnum > Symtab.nsyms) 986 outs() << format("?(%d)\n", r_symbolnum); 987 else { 988 SymbolRef Symbol = *O->getSymbolByIndex(r_symbolnum); 989 Expected<StringRef> SymNameNext = Symbol.getName(); 990 const char *name = NULL; 991 if (SymNameNext) 992 name = SymNameNext->data(); 993 if (name == NULL) 994 outs() << format("?(%d)\n", r_symbolnum); 995 else 996 outs() << name << "\n"; 997 } 998 } 999 else { 1000 // plain: extern & type & scattered 1001 outs() << "False "; 1002 PrintRType(cputype, r_type); 1003 outs() << "False "; 1004 1005 // plain: symbolnum/value 1006 if (cputype == MachO::CPU_TYPE_ARM && r_type == MachO::ARM_RELOC_PAIR) 1007 outs() << format("other_half = 0x%04x\n", (unsigned int)r_address); 1008 else if ((cputype == MachO::CPU_TYPE_ARM64 || 1009 cputype == MachO::CPU_TYPE_ARM64_32) && 1010 r_type == MachO::ARM64_RELOC_ADDEND) 1011 outs() << format("addend = 0x%06x\n", (unsigned int)r_symbolnum); 1012 else { 1013 outs() << format("%d ", r_symbolnum); 1014 if (r_symbolnum == MachO::R_ABS) 1015 outs() << "R_ABS\n"; 1016 else { 1017 // in this case, r_symbolnum is actually a 1-based section number 1018 uint32_t nsects = O->section_end()->getRawDataRefImpl().d.a; 1019 if (r_symbolnum > 0 && r_symbolnum <= nsects) { 1020 object::DataRefImpl DRI; 1021 DRI.d.a = r_symbolnum-1; 1022 StringRef SegName = O->getSectionFinalSegmentName(DRI); 1023 if (Expected<StringRef> NameOrErr = O->getSectionName(DRI)) 1024 outs() << "(" << SegName << "," << *NameOrErr << ")\n"; 1025 else 1026 outs() << "(?,?)\n"; 1027 } 1028 else { 1029 outs() << "(?,?)\n"; 1030 } 1031 } 1032 } 1033 } 1034 if (cputype == MachO::CPU_TYPE_ARM && 1035 (r_type == MachO::ARM_RELOC_HALF || 1036 r_type == MachO::ARM_RELOC_HALF_SECTDIFF)) 1037 previous_arm_half = true; 1038 else 1039 previous_arm_half = false; 1040 } 1041 else { 1042 // plain: address pcrel length extern type scattered symbolnum/section 1043 outs() << format("%08x %1d %-2d %1d %-7d 0 %d\n", 1044 (unsigned int)r_address, r_pcrel, r_length, r_extern, 1045 r_type, r_symbolnum); 1046 } 1047 } 1048 } 1049 } 1050 1051 static void PrintRelocations(const MachOObjectFile *O, const bool verbose) { 1052 const uint64_t cputype = O->getHeader().cputype; 1053 const MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand(); 1054 if (Dysymtab.nextrel != 0) { 1055 outs() << "External relocation information " << Dysymtab.nextrel 1056 << " entries"; 1057 outs() << "\naddress pcrel length extern type scattered " 1058 "symbolnum/value\n"; 1059 PrintRelocationEntries(O, O->extrel_begin(), O->extrel_end(), cputype, 1060 verbose); 1061 } 1062 if (Dysymtab.nlocrel != 0) { 1063 outs() << format("Local relocation information %u entries", 1064 Dysymtab.nlocrel); 1065 outs() << "\naddress pcrel length extern type scattered " 1066 "symbolnum/value\n"; 1067 PrintRelocationEntries(O, O->locrel_begin(), O->locrel_end(), cputype, 1068 verbose); 1069 } 1070 for (const auto &Load : O->load_commands()) { 1071 if (Load.C.cmd == MachO::LC_SEGMENT_64) { 1072 const MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load); 1073 for (unsigned J = 0; J < Seg.nsects; ++J) { 1074 const MachO::section_64 Sec = O->getSection64(Load, J); 1075 if (Sec.nreloc != 0) { 1076 DataRefImpl DRI; 1077 DRI.d.a = J; 1078 const StringRef SegName = O->getSectionFinalSegmentName(DRI); 1079 if (Expected<StringRef> NameOrErr = O->getSectionName(DRI)) 1080 outs() << "Relocation information (" << SegName << "," << *NameOrErr 1081 << format(") %u entries", Sec.nreloc); 1082 else 1083 outs() << "Relocation information (" << SegName << ",?) " 1084 << format("%u entries", Sec.nreloc); 1085 outs() << "\naddress pcrel length extern type scattered " 1086 "symbolnum/value\n"; 1087 PrintRelocationEntries(O, O->section_rel_begin(DRI), 1088 O->section_rel_end(DRI), cputype, verbose); 1089 } 1090 } 1091 } else if (Load.C.cmd == MachO::LC_SEGMENT) { 1092 const MachO::segment_command Seg = O->getSegmentLoadCommand(Load); 1093 for (unsigned J = 0; J < Seg.nsects; ++J) { 1094 const MachO::section Sec = O->getSection(Load, J); 1095 if (Sec.nreloc != 0) { 1096 DataRefImpl DRI; 1097 DRI.d.a = J; 1098 const StringRef SegName = O->getSectionFinalSegmentName(DRI); 1099 if (Expected<StringRef> NameOrErr = O->getSectionName(DRI)) 1100 outs() << "Relocation information (" << SegName << "," << *NameOrErr 1101 << format(") %u entries", Sec.nreloc); 1102 else 1103 outs() << "Relocation information (" << SegName << ",?) " 1104 << format("%u entries", Sec.nreloc); 1105 outs() << "\naddress pcrel length extern type scattered " 1106 "symbolnum/value\n"; 1107 PrintRelocationEntries(O, O->section_rel_begin(DRI), 1108 O->section_rel_end(DRI), cputype, verbose); 1109 } 1110 } 1111 } 1112 } 1113 } 1114 1115 static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) { 1116 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand(); 1117 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry); 1118 outs() << "Data in code table (" << nentries << " entries)\n"; 1119 outs() << "offset length kind\n"; 1120 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE; 1121 ++DI) { 1122 uint32_t Offset; 1123 DI->getOffset(Offset); 1124 outs() << format("0x%08" PRIx32, Offset) << " "; 1125 uint16_t Length; 1126 DI->getLength(Length); 1127 outs() << format("%6u", Length) << " "; 1128 uint16_t Kind; 1129 DI->getKind(Kind); 1130 if (verbose) { 1131 switch (Kind) { 1132 case MachO::DICE_KIND_DATA: 1133 outs() << "DATA"; 1134 break; 1135 case MachO::DICE_KIND_JUMP_TABLE8: 1136 outs() << "JUMP_TABLE8"; 1137 break; 1138 case MachO::DICE_KIND_JUMP_TABLE16: 1139 outs() << "JUMP_TABLE16"; 1140 break; 1141 case MachO::DICE_KIND_JUMP_TABLE32: 1142 outs() << "JUMP_TABLE32"; 1143 break; 1144 case MachO::DICE_KIND_ABS_JUMP_TABLE32: 1145 outs() << "ABS_JUMP_TABLE32"; 1146 break; 1147 default: 1148 outs() << format("0x%04" PRIx32, Kind); 1149 break; 1150 } 1151 } else 1152 outs() << format("0x%04" PRIx32, Kind); 1153 outs() << "\n"; 1154 } 1155 } 1156 1157 static void PrintLinkOptHints(MachOObjectFile *O) { 1158 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand(); 1159 const char *loh = O->getData().substr(LohLC.dataoff, 1).data(); 1160 uint32_t nloh = LohLC.datasize; 1161 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n"; 1162 for (uint32_t i = 0; i < nloh;) { 1163 unsigned n; 1164 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n); 1165 i += n; 1166 outs() << " identifier " << identifier << " "; 1167 if (i >= nloh) 1168 return; 1169 switch (identifier) { 1170 case 1: 1171 outs() << "AdrpAdrp\n"; 1172 break; 1173 case 2: 1174 outs() << "AdrpLdr\n"; 1175 break; 1176 case 3: 1177 outs() << "AdrpAddLdr\n"; 1178 break; 1179 case 4: 1180 outs() << "AdrpLdrGotLdr\n"; 1181 break; 1182 case 5: 1183 outs() << "AdrpAddStr\n"; 1184 break; 1185 case 6: 1186 outs() << "AdrpLdrGotStr\n"; 1187 break; 1188 case 7: 1189 outs() << "AdrpAdd\n"; 1190 break; 1191 case 8: 1192 outs() << "AdrpLdrGot\n"; 1193 break; 1194 default: 1195 outs() << "Unknown identifier value\n"; 1196 break; 1197 } 1198 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n); 1199 i += n; 1200 outs() << " narguments " << narguments << "\n"; 1201 if (i >= nloh) 1202 return; 1203 1204 for (uint32_t j = 0; j < narguments; j++) { 1205 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n); 1206 i += n; 1207 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n"; 1208 if (i >= nloh) 1209 return; 1210 } 1211 } 1212 } 1213 1214 static void PrintDylibs(MachOObjectFile *O, bool JustId) { 1215 unsigned Index = 0; 1216 for (const auto &Load : O->load_commands()) { 1217 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) || 1218 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB || 1219 Load.C.cmd == MachO::LC_LOAD_DYLIB || 1220 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB || 1221 Load.C.cmd == MachO::LC_REEXPORT_DYLIB || 1222 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB || 1223 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) { 1224 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load); 1225 if (dl.dylib.name < dl.cmdsize) { 1226 const char *p = (const char *)(Load.Ptr) + dl.dylib.name; 1227 if (JustId) 1228 outs() << p << "\n"; 1229 else { 1230 outs() << "\t" << p; 1231 outs() << " (compatibility version " 1232 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "." 1233 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "." 1234 << (dl.dylib.compatibility_version & 0xff) << ","; 1235 outs() << " current version " 1236 << ((dl.dylib.current_version >> 16) & 0xffff) << "." 1237 << ((dl.dylib.current_version >> 8) & 0xff) << "." 1238 << (dl.dylib.current_version & 0xff); 1239 if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB) 1240 outs() << ", weak"; 1241 if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB) 1242 outs() << ", reexport"; 1243 if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) 1244 outs() << ", upward"; 1245 if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB) 1246 outs() << ", lazy"; 1247 outs() << ")\n"; 1248 } 1249 } else { 1250 outs() << "\tBad offset (" << dl.dylib.name << ") for name of "; 1251 if (Load.C.cmd == MachO::LC_ID_DYLIB) 1252 outs() << "LC_ID_DYLIB "; 1253 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB) 1254 outs() << "LC_LOAD_DYLIB "; 1255 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB) 1256 outs() << "LC_LOAD_WEAK_DYLIB "; 1257 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB) 1258 outs() << "LC_LAZY_LOAD_DYLIB "; 1259 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB) 1260 outs() << "LC_REEXPORT_DYLIB "; 1261 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) 1262 outs() << "LC_LOAD_UPWARD_DYLIB "; 1263 else 1264 outs() << "LC_??? "; 1265 outs() << "command " << Index++ << "\n"; 1266 } 1267 } 1268 } 1269 } 1270 1271 typedef DenseMap<uint64_t, StringRef> SymbolAddressMap; 1272 1273 static void CreateSymbolAddressMap(MachOObjectFile *O, 1274 SymbolAddressMap *AddrMap) { 1275 // Create a map of symbol addresses to symbol names. 1276 const StringRef FileName = O->getFileName(); 1277 for (const SymbolRef &Symbol : O->symbols()) { 1278 SymbolRef::Type ST = unwrapOrError(Symbol.getType(), FileName); 1279 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data || 1280 ST == SymbolRef::ST_Other) { 1281 uint64_t Address = Symbol.getValue(); 1282 StringRef SymName = unwrapOrError(Symbol.getName(), FileName); 1283 if (!SymName.startswith(".objc")) 1284 (*AddrMap)[Address] = SymName; 1285 } 1286 } 1287 } 1288 1289 // GuessSymbolName is passed the address of what might be a symbol and a 1290 // pointer to the SymbolAddressMap. It returns the name of a symbol 1291 // with that address or nullptr if no symbol is found with that address. 1292 static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) { 1293 const char *SymbolName = nullptr; 1294 // A DenseMap can't lookup up some values. 1295 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) { 1296 StringRef name = AddrMap->lookup(value); 1297 if (!name.empty()) 1298 SymbolName = name.data(); 1299 } 1300 return SymbolName; 1301 } 1302 1303 static void DumpCstringChar(const char c) { 1304 char p[2]; 1305 p[0] = c; 1306 p[1] = '\0'; 1307 outs().write_escaped(p); 1308 } 1309 1310 static void DumpCstringSection(MachOObjectFile *O, const char *sect, 1311 uint32_t sect_size, uint64_t sect_addr, 1312 bool print_addresses) { 1313 for (uint32_t i = 0; i < sect_size; i++) { 1314 if (print_addresses) { 1315 if (O->is64Bit()) 1316 outs() << format("%016" PRIx64, sect_addr + i) << " "; 1317 else 1318 outs() << format("%08" PRIx64, sect_addr + i) << " "; 1319 } 1320 for (; i < sect_size && sect[i] != '\0'; i++) 1321 DumpCstringChar(sect[i]); 1322 if (i < sect_size && sect[i] == '\0') 1323 outs() << "\n"; 1324 } 1325 } 1326 1327 static void DumpLiteral4(uint32_t l, float f) { 1328 outs() << format("0x%08" PRIx32, l); 1329 if ((l & 0x7f800000) != 0x7f800000) 1330 outs() << format(" (%.16e)\n", f); 1331 else { 1332 if (l == 0x7f800000) 1333 outs() << " (+Infinity)\n"; 1334 else if (l == 0xff800000) 1335 outs() << " (-Infinity)\n"; 1336 else if ((l & 0x00400000) == 0x00400000) 1337 outs() << " (non-signaling Not-a-Number)\n"; 1338 else 1339 outs() << " (signaling Not-a-Number)\n"; 1340 } 1341 } 1342 1343 static void DumpLiteral4Section(MachOObjectFile *O, const char *sect, 1344 uint32_t sect_size, uint64_t sect_addr, 1345 bool print_addresses) { 1346 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) { 1347 if (print_addresses) { 1348 if (O->is64Bit()) 1349 outs() << format("%016" PRIx64, sect_addr + i) << " "; 1350 else 1351 outs() << format("%08" PRIx64, sect_addr + i) << " "; 1352 } 1353 float f; 1354 memcpy(&f, sect + i, sizeof(float)); 1355 if (O->isLittleEndian() != sys::IsLittleEndianHost) 1356 sys::swapByteOrder(f); 1357 uint32_t l; 1358 memcpy(&l, sect + i, sizeof(uint32_t)); 1359 if (O->isLittleEndian() != sys::IsLittleEndianHost) 1360 sys::swapByteOrder(l); 1361 DumpLiteral4(l, f); 1362 } 1363 } 1364 1365 static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1, 1366 double d) { 1367 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1); 1368 uint32_t Hi, Lo; 1369 Hi = (O->isLittleEndian()) ? l1 : l0; 1370 Lo = (O->isLittleEndian()) ? l0 : l1; 1371 1372 // Hi is the high word, so this is equivalent to if(isfinite(d)) 1373 if ((Hi & 0x7ff00000) != 0x7ff00000) 1374 outs() << format(" (%.16e)\n", d); 1375 else { 1376 if (Hi == 0x7ff00000 && Lo == 0) 1377 outs() << " (+Infinity)\n"; 1378 else if (Hi == 0xfff00000 && Lo == 0) 1379 outs() << " (-Infinity)\n"; 1380 else if ((Hi & 0x00080000) == 0x00080000) 1381 outs() << " (non-signaling Not-a-Number)\n"; 1382 else 1383 outs() << " (signaling Not-a-Number)\n"; 1384 } 1385 } 1386 1387 static void DumpLiteral8Section(MachOObjectFile *O, const char *sect, 1388 uint32_t sect_size, uint64_t sect_addr, 1389 bool print_addresses) { 1390 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) { 1391 if (print_addresses) { 1392 if (O->is64Bit()) 1393 outs() << format("%016" PRIx64, sect_addr + i) << " "; 1394 else 1395 outs() << format("%08" PRIx64, sect_addr + i) << " "; 1396 } 1397 double d; 1398 memcpy(&d, sect + i, sizeof(double)); 1399 if (O->isLittleEndian() != sys::IsLittleEndianHost) 1400 sys::swapByteOrder(d); 1401 uint32_t l0, l1; 1402 memcpy(&l0, sect + i, sizeof(uint32_t)); 1403 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t)); 1404 if (O->isLittleEndian() != sys::IsLittleEndianHost) { 1405 sys::swapByteOrder(l0); 1406 sys::swapByteOrder(l1); 1407 } 1408 DumpLiteral8(O, l0, l1, d); 1409 } 1410 } 1411 1412 static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) { 1413 outs() << format("0x%08" PRIx32, l0) << " "; 1414 outs() << format("0x%08" PRIx32, l1) << " "; 1415 outs() << format("0x%08" PRIx32, l2) << " "; 1416 outs() << format("0x%08" PRIx32, l3) << "\n"; 1417 } 1418 1419 static void DumpLiteral16Section(MachOObjectFile *O, const char *sect, 1420 uint32_t sect_size, uint64_t sect_addr, 1421 bool print_addresses) { 1422 for (uint32_t i = 0; i < sect_size; i += 16) { 1423 if (print_addresses) { 1424 if (O->is64Bit()) 1425 outs() << format("%016" PRIx64, sect_addr + i) << " "; 1426 else 1427 outs() << format("%08" PRIx64, sect_addr + i) << " "; 1428 } 1429 uint32_t l0, l1, l2, l3; 1430 memcpy(&l0, sect + i, sizeof(uint32_t)); 1431 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t)); 1432 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t)); 1433 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t)); 1434 if (O->isLittleEndian() != sys::IsLittleEndianHost) { 1435 sys::swapByteOrder(l0); 1436 sys::swapByteOrder(l1); 1437 sys::swapByteOrder(l2); 1438 sys::swapByteOrder(l3); 1439 } 1440 DumpLiteral16(l0, l1, l2, l3); 1441 } 1442 } 1443 1444 static void DumpLiteralPointerSection(MachOObjectFile *O, 1445 const SectionRef &Section, 1446 const char *sect, uint32_t sect_size, 1447 uint64_t sect_addr, 1448 bool print_addresses) { 1449 // Collect the literal sections in this Mach-O file. 1450 std::vector<SectionRef> LiteralSections; 1451 for (const SectionRef &Section : O->sections()) { 1452 DataRefImpl Ref = Section.getRawDataRefImpl(); 1453 uint32_t section_type; 1454 if (O->is64Bit()) { 1455 const MachO::section_64 Sec = O->getSection64(Ref); 1456 section_type = Sec.flags & MachO::SECTION_TYPE; 1457 } else { 1458 const MachO::section Sec = O->getSection(Ref); 1459 section_type = Sec.flags & MachO::SECTION_TYPE; 1460 } 1461 if (section_type == MachO::S_CSTRING_LITERALS || 1462 section_type == MachO::S_4BYTE_LITERALS || 1463 section_type == MachO::S_8BYTE_LITERALS || 1464 section_type == MachO::S_16BYTE_LITERALS) 1465 LiteralSections.push_back(Section); 1466 } 1467 1468 // Set the size of the literal pointer. 1469 uint32_t lp_size = O->is64Bit() ? 8 : 4; 1470 1471 // Collect the external relocation symbols for the literal pointers. 1472 std::vector<std::pair<uint64_t, SymbolRef>> Relocs; 1473 for (const RelocationRef &Reloc : Section.relocations()) { 1474 DataRefImpl Rel; 1475 MachO::any_relocation_info RE; 1476 bool isExtern = false; 1477 Rel = Reloc.getRawDataRefImpl(); 1478 RE = O->getRelocation(Rel); 1479 isExtern = O->getPlainRelocationExternal(RE); 1480 if (isExtern) { 1481 uint64_t RelocOffset = Reloc.getOffset(); 1482 symbol_iterator RelocSym = Reloc.getSymbol(); 1483 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym)); 1484 } 1485 } 1486 array_pod_sort(Relocs.begin(), Relocs.end()); 1487 1488 // Dump each literal pointer. 1489 for (uint32_t i = 0; i < sect_size; i += lp_size) { 1490 if (print_addresses) { 1491 if (O->is64Bit()) 1492 outs() << format("%016" PRIx64, sect_addr + i) << " "; 1493 else 1494 outs() << format("%08" PRIx64, sect_addr + i) << " "; 1495 } 1496 uint64_t lp; 1497 if (O->is64Bit()) { 1498 memcpy(&lp, sect + i, sizeof(uint64_t)); 1499 if (O->isLittleEndian() != sys::IsLittleEndianHost) 1500 sys::swapByteOrder(lp); 1501 } else { 1502 uint32_t li; 1503 memcpy(&li, sect + i, sizeof(uint32_t)); 1504 if (O->isLittleEndian() != sys::IsLittleEndianHost) 1505 sys::swapByteOrder(li); 1506 lp = li; 1507 } 1508 1509 // First look for an external relocation entry for this literal pointer. 1510 auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) { 1511 return P.first == i; 1512 }); 1513 if (Reloc != Relocs.end()) { 1514 symbol_iterator RelocSym = Reloc->second; 1515 StringRef SymName = unwrapOrError(RelocSym->getName(), O->getFileName()); 1516 outs() << "external relocation entry for symbol:" << SymName << "\n"; 1517 continue; 1518 } 1519 1520 // For local references see what the section the literal pointer points to. 1521 auto Sect = find_if(LiteralSections, [&](const SectionRef &R) { 1522 return lp >= R.getAddress() && lp < R.getAddress() + R.getSize(); 1523 }); 1524 if (Sect == LiteralSections.end()) { 1525 outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n"; 1526 continue; 1527 } 1528 1529 uint64_t SectAddress = Sect->getAddress(); 1530 uint64_t SectSize = Sect->getSize(); 1531 1532 StringRef SectName; 1533 Expected<StringRef> SectNameOrErr = Sect->getName(); 1534 if (SectNameOrErr) 1535 SectName = *SectNameOrErr; 1536 else 1537 consumeError(SectNameOrErr.takeError()); 1538 1539 DataRefImpl Ref = Sect->getRawDataRefImpl(); 1540 StringRef SegmentName = O->getSectionFinalSegmentName(Ref); 1541 outs() << SegmentName << ":" << SectName << ":"; 1542 1543 uint32_t section_type; 1544 if (O->is64Bit()) { 1545 const MachO::section_64 Sec = O->getSection64(Ref); 1546 section_type = Sec.flags & MachO::SECTION_TYPE; 1547 } else { 1548 const MachO::section Sec = O->getSection(Ref); 1549 section_type = Sec.flags & MachO::SECTION_TYPE; 1550 } 1551 1552 StringRef BytesStr = unwrapOrError(Sect->getContents(), O->getFileName()); 1553 1554 const char *Contents = reinterpret_cast<const char *>(BytesStr.data()); 1555 1556 switch (section_type) { 1557 case MachO::S_CSTRING_LITERALS: 1558 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0'; 1559 i++) { 1560 DumpCstringChar(Contents[i]); 1561 } 1562 outs() << "\n"; 1563 break; 1564 case MachO::S_4BYTE_LITERALS: 1565 float f; 1566 memcpy(&f, Contents + (lp - SectAddress), sizeof(float)); 1567 uint32_t l; 1568 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t)); 1569 if (O->isLittleEndian() != sys::IsLittleEndianHost) { 1570 sys::swapByteOrder(f); 1571 sys::swapByteOrder(l); 1572 } 1573 DumpLiteral4(l, f); 1574 break; 1575 case MachO::S_8BYTE_LITERALS: { 1576 double d; 1577 memcpy(&d, Contents + (lp - SectAddress), sizeof(double)); 1578 uint32_t l0, l1; 1579 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t)); 1580 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t), 1581 sizeof(uint32_t)); 1582 if (O->isLittleEndian() != sys::IsLittleEndianHost) { 1583 sys::swapByteOrder(f); 1584 sys::swapByteOrder(l0); 1585 sys::swapByteOrder(l1); 1586 } 1587 DumpLiteral8(O, l0, l1, d); 1588 break; 1589 } 1590 case MachO::S_16BYTE_LITERALS: { 1591 uint32_t l0, l1, l2, l3; 1592 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t)); 1593 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t), 1594 sizeof(uint32_t)); 1595 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t), 1596 sizeof(uint32_t)); 1597 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t), 1598 sizeof(uint32_t)); 1599 if (O->isLittleEndian() != sys::IsLittleEndianHost) { 1600 sys::swapByteOrder(l0); 1601 sys::swapByteOrder(l1); 1602 sys::swapByteOrder(l2); 1603 sys::swapByteOrder(l3); 1604 } 1605 DumpLiteral16(l0, l1, l2, l3); 1606 break; 1607 } 1608 } 1609 } 1610 } 1611 1612 static void DumpInitTermPointerSection(MachOObjectFile *O, 1613 const SectionRef &Section, 1614 const char *sect, 1615 uint32_t sect_size, uint64_t sect_addr, 1616 SymbolAddressMap *AddrMap, 1617 bool verbose) { 1618 uint32_t stride; 1619 stride = (O->is64Bit()) ? sizeof(uint64_t) : sizeof(uint32_t); 1620 1621 // Collect the external relocation symbols for the pointers. 1622 std::vector<std::pair<uint64_t, SymbolRef>> Relocs; 1623 for (const RelocationRef &Reloc : Section.relocations()) { 1624 DataRefImpl Rel; 1625 MachO::any_relocation_info RE; 1626 bool isExtern = false; 1627 Rel = Reloc.getRawDataRefImpl(); 1628 RE = O->getRelocation(Rel); 1629 isExtern = O->getPlainRelocationExternal(RE); 1630 if (isExtern) { 1631 uint64_t RelocOffset = Reloc.getOffset(); 1632 symbol_iterator RelocSym = Reloc.getSymbol(); 1633 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym)); 1634 } 1635 } 1636 array_pod_sort(Relocs.begin(), Relocs.end()); 1637 1638 for (uint32_t i = 0; i < sect_size; i += stride) { 1639 const char *SymbolName = nullptr; 1640 uint64_t p; 1641 if (O->is64Bit()) { 1642 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " "; 1643 uint64_t pointer_value; 1644 memcpy(&pointer_value, sect + i, stride); 1645 if (O->isLittleEndian() != sys::IsLittleEndianHost) 1646 sys::swapByteOrder(pointer_value); 1647 outs() << format("0x%016" PRIx64, pointer_value); 1648 p = pointer_value; 1649 } else { 1650 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " "; 1651 uint32_t pointer_value; 1652 memcpy(&pointer_value, sect + i, stride); 1653 if (O->isLittleEndian() != sys::IsLittleEndianHost) 1654 sys::swapByteOrder(pointer_value); 1655 outs() << format("0x%08" PRIx32, pointer_value); 1656 p = pointer_value; 1657 } 1658 if (verbose) { 1659 // First look for an external relocation entry for this pointer. 1660 auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) { 1661 return P.first == i; 1662 }); 1663 if (Reloc != Relocs.end()) { 1664 symbol_iterator RelocSym = Reloc->second; 1665 outs() << " " << unwrapOrError(RelocSym->getName(), O->getFileName()); 1666 } else { 1667 SymbolName = GuessSymbolName(p, AddrMap); 1668 if (SymbolName) 1669 outs() << " " << SymbolName; 1670 } 1671 } 1672 outs() << "\n"; 1673 } 1674 } 1675 1676 static void DumpRawSectionContents(MachOObjectFile *O, const char *sect, 1677 uint32_t size, uint64_t addr) { 1678 uint32_t cputype = O->getHeader().cputype; 1679 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) { 1680 uint32_t j; 1681 for (uint32_t i = 0; i < size; i += j, addr += j) { 1682 if (O->is64Bit()) 1683 outs() << format("%016" PRIx64, addr) << "\t"; 1684 else 1685 outs() << format("%08" PRIx64, addr) << "\t"; 1686 for (j = 0; j < 16 && i + j < size; j++) { 1687 uint8_t byte_word = *(sect + i + j); 1688 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " "; 1689 } 1690 outs() << "\n"; 1691 } 1692 } else { 1693 uint32_t j; 1694 for (uint32_t i = 0; i < size; i += j, addr += j) { 1695 if (O->is64Bit()) 1696 outs() << format("%016" PRIx64, addr) << "\t"; 1697 else 1698 outs() << format("%08" PRIx64, addr) << "\t"; 1699 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size; 1700 j += sizeof(int32_t)) { 1701 if (i + j + sizeof(int32_t) <= size) { 1702 uint32_t long_word; 1703 memcpy(&long_word, sect + i + j, sizeof(int32_t)); 1704 if (O->isLittleEndian() != sys::IsLittleEndianHost) 1705 sys::swapByteOrder(long_word); 1706 outs() << format("%08" PRIx32, long_word) << " "; 1707 } else { 1708 for (uint32_t k = 0; i + j + k < size; k++) { 1709 uint8_t byte_word = *(sect + i + j + k); 1710 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " "; 1711 } 1712 } 1713 } 1714 outs() << "\n"; 1715 } 1716 } 1717 } 1718 1719 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, 1720 StringRef DisSegName, StringRef DisSectName); 1721 static void DumpProtocolSection(MachOObjectFile *O, const char *sect, 1722 uint32_t size, uint32_t addr); 1723 #ifdef HAVE_LIBXAR 1724 static void DumpBitcodeSection(MachOObjectFile *O, const char *sect, 1725 uint32_t size, bool verbose, 1726 bool PrintXarHeader, bool PrintXarFileHeaders, 1727 std::string XarMemberName); 1728 #endif // defined(HAVE_LIBXAR) 1729 1730 static void DumpSectionContents(StringRef Filename, MachOObjectFile *O, 1731 bool verbose) { 1732 SymbolAddressMap AddrMap; 1733 if (verbose) 1734 CreateSymbolAddressMap(O, &AddrMap); 1735 1736 for (unsigned i = 0; i < FilterSections.size(); ++i) { 1737 StringRef DumpSection = FilterSections[i]; 1738 std::pair<StringRef, StringRef> DumpSegSectName; 1739 DumpSegSectName = DumpSection.split(','); 1740 StringRef DumpSegName, DumpSectName; 1741 if (!DumpSegSectName.second.empty()) { 1742 DumpSegName = DumpSegSectName.first; 1743 DumpSectName = DumpSegSectName.second; 1744 } else { 1745 DumpSegName = ""; 1746 DumpSectName = DumpSegSectName.first; 1747 } 1748 for (const SectionRef &Section : O->sections()) { 1749 StringRef SectName; 1750 Expected<StringRef> SecNameOrErr = Section.getName(); 1751 if (SecNameOrErr) 1752 SectName = *SecNameOrErr; 1753 else 1754 consumeError(SecNameOrErr.takeError()); 1755 1756 DataRefImpl Ref = Section.getRawDataRefImpl(); 1757 StringRef SegName = O->getSectionFinalSegmentName(Ref); 1758 if ((DumpSegName.empty() || SegName == DumpSegName) && 1759 (SectName == DumpSectName)) { 1760 1761 uint32_t section_flags; 1762 if (O->is64Bit()) { 1763 const MachO::section_64 Sec = O->getSection64(Ref); 1764 section_flags = Sec.flags; 1765 1766 } else { 1767 const MachO::section Sec = O->getSection(Ref); 1768 section_flags = Sec.flags; 1769 } 1770 uint32_t section_type = section_flags & MachO::SECTION_TYPE; 1771 1772 StringRef BytesStr = 1773 unwrapOrError(Section.getContents(), O->getFileName()); 1774 const char *sect = reinterpret_cast<const char *>(BytesStr.data()); 1775 uint32_t sect_size = BytesStr.size(); 1776 uint64_t sect_addr = Section.getAddress(); 1777 1778 outs() << "Contents of (" << SegName << "," << SectName 1779 << ") section\n"; 1780 1781 if (verbose) { 1782 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) || 1783 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) { 1784 DisassembleMachO(Filename, O, SegName, SectName); 1785 continue; 1786 } 1787 if (SegName == "__TEXT" && SectName == "__info_plist") { 1788 outs() << sect; 1789 continue; 1790 } 1791 if (SegName == "__OBJC" && SectName == "__protocol") { 1792 DumpProtocolSection(O, sect, sect_size, sect_addr); 1793 continue; 1794 } 1795 #ifdef HAVE_LIBXAR 1796 if (SegName == "__LLVM" && SectName == "__bundle") { 1797 DumpBitcodeSection(O, sect, sect_size, verbose, !NoSymbolicOperands, 1798 ArchiveHeaders, ""); 1799 continue; 1800 } 1801 #endif // defined(HAVE_LIBXAR) 1802 switch (section_type) { 1803 case MachO::S_REGULAR: 1804 DumpRawSectionContents(O, sect, sect_size, sect_addr); 1805 break; 1806 case MachO::S_ZEROFILL: 1807 outs() << "zerofill section and has no contents in the file\n"; 1808 break; 1809 case MachO::S_CSTRING_LITERALS: 1810 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr); 1811 break; 1812 case MachO::S_4BYTE_LITERALS: 1813 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr); 1814 break; 1815 case MachO::S_8BYTE_LITERALS: 1816 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr); 1817 break; 1818 case MachO::S_16BYTE_LITERALS: 1819 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr); 1820 break; 1821 case MachO::S_LITERAL_POINTERS: 1822 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr, 1823 !NoLeadingAddr); 1824 break; 1825 case MachO::S_MOD_INIT_FUNC_POINTERS: 1826 case MachO::S_MOD_TERM_FUNC_POINTERS: 1827 DumpInitTermPointerSection(O, Section, sect, sect_size, sect_addr, 1828 &AddrMap, verbose); 1829 break; 1830 default: 1831 outs() << "Unknown section type (" 1832 << format("0x%08" PRIx32, section_type) << ")\n"; 1833 DumpRawSectionContents(O, sect, sect_size, sect_addr); 1834 break; 1835 } 1836 } else { 1837 if (section_type == MachO::S_ZEROFILL) 1838 outs() << "zerofill section and has no contents in the file\n"; 1839 else 1840 DumpRawSectionContents(O, sect, sect_size, sect_addr); 1841 } 1842 } 1843 } 1844 } 1845 } 1846 1847 static void DumpInfoPlistSectionContents(StringRef Filename, 1848 MachOObjectFile *O) { 1849 for (const SectionRef &Section : O->sections()) { 1850 StringRef SectName; 1851 Expected<StringRef> SecNameOrErr = Section.getName(); 1852 if (SecNameOrErr) 1853 SectName = *SecNameOrErr; 1854 else 1855 consumeError(SecNameOrErr.takeError()); 1856 1857 DataRefImpl Ref = Section.getRawDataRefImpl(); 1858 StringRef SegName = O->getSectionFinalSegmentName(Ref); 1859 if (SegName == "__TEXT" && SectName == "__info_plist") { 1860 if (!NoLeadingHeaders) 1861 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 1862 StringRef BytesStr = 1863 unwrapOrError(Section.getContents(), O->getFileName()); 1864 const char *sect = reinterpret_cast<const char *>(BytesStr.data()); 1865 outs() << format("%.*s", BytesStr.size(), sect) << "\n"; 1866 return; 1867 } 1868 } 1869 } 1870 1871 // checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file 1872 // and if it is and there is a list of architecture flags is specified then 1873 // check to make sure this Mach-O file is one of those architectures or all 1874 // architectures were specified. If not then an error is generated and this 1875 // routine returns false. Else it returns true. 1876 static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) { 1877 auto *MachO = dyn_cast<MachOObjectFile>(O); 1878 1879 if (!MachO || ArchAll || ArchFlags.empty()) 1880 return true; 1881 1882 MachO::mach_header H; 1883 MachO::mach_header_64 H_64; 1884 Triple T; 1885 const char *McpuDefault, *ArchFlag; 1886 if (MachO->is64Bit()) { 1887 H_64 = MachO->MachOObjectFile::getHeader64(); 1888 T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype, 1889 &McpuDefault, &ArchFlag); 1890 } else { 1891 H = MachO->MachOObjectFile::getHeader(); 1892 T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype, 1893 &McpuDefault, &ArchFlag); 1894 } 1895 const std::string ArchFlagName(ArchFlag); 1896 if (none_of(ArchFlags, [&](const std::string &Name) { 1897 return Name == ArchFlagName; 1898 })) { 1899 WithColor::error(errs(), "llvm-objdump") 1900 << Filename << ": no architecture specified.\n"; 1901 return false; 1902 } 1903 return true; 1904 } 1905 1906 static void printObjcMetaData(MachOObjectFile *O, bool verbose); 1907 1908 // ProcessMachO() is passed a single opened Mach-O file, which may be an 1909 // archive member and or in a slice of a universal file. It prints the 1910 // the file name and header info and then processes it according to the 1911 // command line options. 1912 static void ProcessMachO(StringRef Name, MachOObjectFile *MachOOF, 1913 StringRef ArchiveMemberName = StringRef(), 1914 StringRef ArchitectureName = StringRef()) { 1915 // If we are doing some processing here on the Mach-O file print the header 1916 // info. And don't print it otherwise like in the case of printing the 1917 // UniversalHeaders or ArchiveHeaders. 1918 if (Disassemble || Relocations || PrivateHeaders || ExportsTrie || Rebase || 1919 Bind || SymbolTable || LazyBind || WeakBind || IndirectSymbols || 1920 DataInCode || LinkOptHints || DylibsUsed || DylibId || ObjcMetaData || 1921 (!FilterSections.empty())) { 1922 if (!NoLeadingHeaders) { 1923 outs() << Name; 1924 if (!ArchiveMemberName.empty()) 1925 outs() << '(' << ArchiveMemberName << ')'; 1926 if (!ArchitectureName.empty()) 1927 outs() << " (architecture " << ArchitectureName << ")"; 1928 outs() << ":\n"; 1929 } 1930 } 1931 // To use the report_error() form with an ArchiveName and FileName set 1932 // these up based on what is passed for Name and ArchiveMemberName. 1933 StringRef ArchiveName; 1934 StringRef FileName; 1935 if (!ArchiveMemberName.empty()) { 1936 ArchiveName = Name; 1937 FileName = ArchiveMemberName; 1938 } else { 1939 ArchiveName = StringRef(); 1940 FileName = Name; 1941 } 1942 1943 // If we need the symbol table to do the operation then check it here to 1944 // produce a good error message as to where the Mach-O file comes from in 1945 // the error message. 1946 if (Disassemble || IndirectSymbols || !FilterSections.empty() || UnwindInfo) 1947 if (Error Err = MachOOF->checkSymbolTable()) 1948 report_error(std::move(Err), ArchiveName, FileName, ArchitectureName); 1949 1950 if (DisassembleAll) { 1951 for (const SectionRef &Section : MachOOF->sections()) { 1952 StringRef SectName; 1953 if (Expected<StringRef> NameOrErr = Section.getName()) 1954 SectName = *NameOrErr; 1955 else 1956 consumeError(NameOrErr.takeError()); 1957 1958 if (SectName.equals("__text")) { 1959 DataRefImpl Ref = Section.getRawDataRefImpl(); 1960 StringRef SegName = MachOOF->getSectionFinalSegmentName(Ref); 1961 DisassembleMachO(FileName, MachOOF, SegName, SectName); 1962 } 1963 } 1964 } 1965 else if (Disassemble) { 1966 if (MachOOF->getHeader().filetype == MachO::MH_KEXT_BUNDLE && 1967 MachOOF->getHeader().cputype == MachO::CPU_TYPE_ARM64) 1968 DisassembleMachO(FileName, MachOOF, "__TEXT_EXEC", "__text"); 1969 else 1970 DisassembleMachO(FileName, MachOOF, "__TEXT", "__text"); 1971 } 1972 if (IndirectSymbols) 1973 PrintIndirectSymbols(MachOOF, !NonVerbose); 1974 if (DataInCode) 1975 PrintDataInCodeTable(MachOOF, !NonVerbose); 1976 if (LinkOptHints) 1977 PrintLinkOptHints(MachOOF); 1978 if (Relocations) 1979 PrintRelocations(MachOOF, !NonVerbose); 1980 if (SectionHeaders) 1981 printSectionHeaders(MachOOF); 1982 if (SectionContents) 1983 printSectionContents(MachOOF); 1984 if (!FilterSections.empty()) 1985 DumpSectionContents(FileName, MachOOF, !NonVerbose); 1986 if (InfoPlist) 1987 DumpInfoPlistSectionContents(FileName, MachOOF); 1988 if (DylibsUsed) 1989 PrintDylibs(MachOOF, false); 1990 if (DylibId) 1991 PrintDylibs(MachOOF, true); 1992 if (SymbolTable) 1993 printSymbolTable(MachOOF, ArchiveName, ArchitectureName); 1994 if (UnwindInfo) 1995 printMachOUnwindInfo(MachOOF); 1996 if (PrivateHeaders) { 1997 printMachOFileHeader(MachOOF); 1998 printMachOLoadCommands(MachOOF); 1999 } 2000 if (FirstPrivateHeader) 2001 printMachOFileHeader(MachOOF); 2002 if (ObjcMetaData) 2003 printObjcMetaData(MachOOF, !NonVerbose); 2004 if (ExportsTrie) 2005 printExportsTrie(MachOOF); 2006 if (Rebase) 2007 printRebaseTable(MachOOF); 2008 if (Bind) 2009 printBindTable(MachOOF); 2010 if (LazyBind) 2011 printLazyBindTable(MachOOF); 2012 if (WeakBind) 2013 printWeakBindTable(MachOOF); 2014 2015 if (DwarfDumpType != DIDT_Null) { 2016 std::unique_ptr<DIContext> DICtx = DWARFContext::create(*MachOOF); 2017 // Dump the complete DWARF structure. 2018 DIDumpOptions DumpOpts; 2019 DumpOpts.DumpType = DwarfDumpType; 2020 DICtx->dump(outs(), DumpOpts); 2021 } 2022 } 2023 2024 // printUnknownCPUType() helps print_fat_headers for unknown CPU's. 2025 static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) { 2026 outs() << " cputype (" << cputype << ")\n"; 2027 outs() << " cpusubtype (" << cpusubtype << ")\n"; 2028 } 2029 2030 // printCPUType() helps print_fat_headers by printing the cputype and 2031 // pusubtype (symbolically for the one's it knows about). 2032 static void printCPUType(uint32_t cputype, uint32_t cpusubtype) { 2033 switch (cputype) { 2034 case MachO::CPU_TYPE_I386: 2035 switch (cpusubtype) { 2036 case MachO::CPU_SUBTYPE_I386_ALL: 2037 outs() << " cputype CPU_TYPE_I386\n"; 2038 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n"; 2039 break; 2040 default: 2041 printUnknownCPUType(cputype, cpusubtype); 2042 break; 2043 } 2044 break; 2045 case MachO::CPU_TYPE_X86_64: 2046 switch (cpusubtype) { 2047 case MachO::CPU_SUBTYPE_X86_64_ALL: 2048 outs() << " cputype CPU_TYPE_X86_64\n"; 2049 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n"; 2050 break; 2051 case MachO::CPU_SUBTYPE_X86_64_H: 2052 outs() << " cputype CPU_TYPE_X86_64\n"; 2053 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n"; 2054 break; 2055 default: 2056 printUnknownCPUType(cputype, cpusubtype); 2057 break; 2058 } 2059 break; 2060 case MachO::CPU_TYPE_ARM: 2061 switch (cpusubtype) { 2062 case MachO::CPU_SUBTYPE_ARM_ALL: 2063 outs() << " cputype CPU_TYPE_ARM\n"; 2064 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n"; 2065 break; 2066 case MachO::CPU_SUBTYPE_ARM_V4T: 2067 outs() << " cputype CPU_TYPE_ARM\n"; 2068 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n"; 2069 break; 2070 case MachO::CPU_SUBTYPE_ARM_V5TEJ: 2071 outs() << " cputype CPU_TYPE_ARM\n"; 2072 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n"; 2073 break; 2074 case MachO::CPU_SUBTYPE_ARM_XSCALE: 2075 outs() << " cputype CPU_TYPE_ARM\n"; 2076 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n"; 2077 break; 2078 case MachO::CPU_SUBTYPE_ARM_V6: 2079 outs() << " cputype CPU_TYPE_ARM\n"; 2080 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n"; 2081 break; 2082 case MachO::CPU_SUBTYPE_ARM_V6M: 2083 outs() << " cputype CPU_TYPE_ARM\n"; 2084 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n"; 2085 break; 2086 case MachO::CPU_SUBTYPE_ARM_V7: 2087 outs() << " cputype CPU_TYPE_ARM\n"; 2088 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n"; 2089 break; 2090 case MachO::CPU_SUBTYPE_ARM_V7EM: 2091 outs() << " cputype CPU_TYPE_ARM\n"; 2092 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n"; 2093 break; 2094 case MachO::CPU_SUBTYPE_ARM_V7K: 2095 outs() << " cputype CPU_TYPE_ARM\n"; 2096 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n"; 2097 break; 2098 case MachO::CPU_SUBTYPE_ARM_V7M: 2099 outs() << " cputype CPU_TYPE_ARM\n"; 2100 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n"; 2101 break; 2102 case MachO::CPU_SUBTYPE_ARM_V7S: 2103 outs() << " cputype CPU_TYPE_ARM\n"; 2104 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n"; 2105 break; 2106 default: 2107 printUnknownCPUType(cputype, cpusubtype); 2108 break; 2109 } 2110 break; 2111 case MachO::CPU_TYPE_ARM64: 2112 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 2113 case MachO::CPU_SUBTYPE_ARM64_ALL: 2114 outs() << " cputype CPU_TYPE_ARM64\n"; 2115 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n"; 2116 break; 2117 case MachO::CPU_SUBTYPE_ARM64E: 2118 outs() << " cputype CPU_TYPE_ARM64\n"; 2119 outs() << " cpusubtype CPU_SUBTYPE_ARM64E\n"; 2120 break; 2121 default: 2122 printUnknownCPUType(cputype, cpusubtype); 2123 break; 2124 } 2125 break; 2126 case MachO::CPU_TYPE_ARM64_32: 2127 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 2128 case MachO::CPU_SUBTYPE_ARM64_32_V8: 2129 outs() << " cputype CPU_TYPE_ARM64_32\n"; 2130 outs() << " cpusubtype CPU_SUBTYPE_ARM64_32_V8\n"; 2131 break; 2132 default: 2133 printUnknownCPUType(cputype, cpusubtype); 2134 break; 2135 } 2136 break; 2137 default: 2138 printUnknownCPUType(cputype, cpusubtype); 2139 break; 2140 } 2141 } 2142 2143 static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB, 2144 bool verbose) { 2145 outs() << "Fat headers\n"; 2146 if (verbose) { 2147 if (UB->getMagic() == MachO::FAT_MAGIC) 2148 outs() << "fat_magic FAT_MAGIC\n"; 2149 else // UB->getMagic() == MachO::FAT_MAGIC_64 2150 outs() << "fat_magic FAT_MAGIC_64\n"; 2151 } else 2152 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n"; 2153 2154 uint32_t nfat_arch = UB->getNumberOfObjects(); 2155 StringRef Buf = UB->getData(); 2156 uint64_t size = Buf.size(); 2157 uint64_t big_size = sizeof(struct MachO::fat_header) + 2158 nfat_arch * sizeof(struct MachO::fat_arch); 2159 outs() << "nfat_arch " << UB->getNumberOfObjects(); 2160 if (nfat_arch == 0) 2161 outs() << " (malformed, contains zero architecture types)\n"; 2162 else if (big_size > size) 2163 outs() << " (malformed, architectures past end of file)\n"; 2164 else 2165 outs() << "\n"; 2166 2167 for (uint32_t i = 0; i < nfat_arch; ++i) { 2168 MachOUniversalBinary::ObjectForArch OFA(UB, i); 2169 uint32_t cputype = OFA.getCPUType(); 2170 uint32_t cpusubtype = OFA.getCPUSubType(); 2171 outs() << "architecture "; 2172 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) { 2173 MachOUniversalBinary::ObjectForArch other_OFA(UB, j); 2174 uint32_t other_cputype = other_OFA.getCPUType(); 2175 uint32_t other_cpusubtype = other_OFA.getCPUSubType(); 2176 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype && 2177 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) == 2178 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) { 2179 outs() << "(illegal duplicate architecture) "; 2180 break; 2181 } 2182 } 2183 if (verbose) { 2184 outs() << OFA.getArchFlagName() << "\n"; 2185 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 2186 } else { 2187 outs() << i << "\n"; 2188 outs() << " cputype " << cputype << "\n"; 2189 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) 2190 << "\n"; 2191 } 2192 if (verbose && 2193 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) 2194 outs() << " capabilities CPU_SUBTYPE_LIB64\n"; 2195 else 2196 outs() << " capabilities " 2197 << format("0x%" PRIx32, 2198 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n"; 2199 outs() << " offset " << OFA.getOffset(); 2200 if (OFA.getOffset() > size) 2201 outs() << " (past end of file)"; 2202 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0) 2203 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")"; 2204 outs() << "\n"; 2205 outs() << " size " << OFA.getSize(); 2206 big_size = OFA.getOffset() + OFA.getSize(); 2207 if (big_size > size) 2208 outs() << " (past end of file)"; 2209 outs() << "\n"; 2210 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign()) 2211 << ")\n"; 2212 } 2213 } 2214 2215 static void printArchiveChild(StringRef Filename, const Archive::Child &C, 2216 bool verbose, bool print_offset, 2217 StringRef ArchitectureName = StringRef()) { 2218 if (print_offset) 2219 outs() << C.getChildOffset() << "\t"; 2220 sys::fs::perms Mode = 2221 unwrapOrError(C.getAccessMode(), Filename, C, ArchitectureName); 2222 if (verbose) { 2223 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG. 2224 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG. 2225 outs() << "-"; 2226 outs() << ((Mode & sys::fs::owner_read) ? "r" : "-"); 2227 outs() << ((Mode & sys::fs::owner_write) ? "w" : "-"); 2228 outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-"); 2229 outs() << ((Mode & sys::fs::group_read) ? "r" : "-"); 2230 outs() << ((Mode & sys::fs::group_write) ? "w" : "-"); 2231 outs() << ((Mode & sys::fs::group_exe) ? "x" : "-"); 2232 outs() << ((Mode & sys::fs::others_read) ? "r" : "-"); 2233 outs() << ((Mode & sys::fs::others_write) ? "w" : "-"); 2234 outs() << ((Mode & sys::fs::others_exe) ? "x" : "-"); 2235 } else { 2236 outs() << format("0%o ", Mode); 2237 } 2238 2239 outs() << format( 2240 "%3d/%-3d %5" PRId64 " ", 2241 unwrapOrError(C.getUID(), Filename, C, ArchitectureName), 2242 unwrapOrError(C.getGID(), Filename, C, ArchitectureName), 2243 unwrapOrError(C.getRawSize(), Filename, C, ArchitectureName)); 2244 2245 StringRef RawLastModified = C.getRawLastModified(); 2246 if (verbose) { 2247 unsigned Seconds; 2248 if (RawLastModified.getAsInteger(10, Seconds)) 2249 outs() << "(date: \"" << RawLastModified 2250 << "\" contains non-decimal chars) "; 2251 else { 2252 // Since cime(3) returns a 26 character string of the form: 2253 // "Sun Sep 16 01:03:52 1973\n\0" 2254 // just print 24 characters. 2255 time_t t = Seconds; 2256 outs() << format("%.24s ", ctime(&t)); 2257 } 2258 } else { 2259 outs() << RawLastModified << " "; 2260 } 2261 2262 if (verbose) { 2263 Expected<StringRef> NameOrErr = C.getName(); 2264 if (!NameOrErr) { 2265 consumeError(NameOrErr.takeError()); 2266 outs() << unwrapOrError(C.getRawName(), Filename, C, ArchitectureName) 2267 << "\n"; 2268 } else { 2269 StringRef Name = NameOrErr.get(); 2270 outs() << Name << "\n"; 2271 } 2272 } else { 2273 outs() << unwrapOrError(C.getRawName(), Filename, C, ArchitectureName) 2274 << "\n"; 2275 } 2276 } 2277 2278 static void printArchiveHeaders(StringRef Filename, Archive *A, bool verbose, 2279 bool print_offset, 2280 StringRef ArchitectureName = StringRef()) { 2281 Error Err = Error::success(); 2282 for (const auto &C : A->children(Err, false)) 2283 printArchiveChild(Filename, C, verbose, print_offset, ArchitectureName); 2284 2285 if (Err) 2286 report_error(std::move(Err), StringRef(), Filename, ArchitectureName); 2287 } 2288 2289 static bool ValidateArchFlags() { 2290 // Check for -arch all and verifiy the -arch flags are valid. 2291 for (unsigned i = 0; i < ArchFlags.size(); ++i) { 2292 if (ArchFlags[i] == "all") { 2293 ArchAll = true; 2294 } else { 2295 if (!MachOObjectFile::isValidArch(ArchFlags[i])) { 2296 WithColor::error(errs(), "llvm-objdump") 2297 << "unknown architecture named '" + ArchFlags[i] + 2298 "'for the -arch option\n"; 2299 return false; 2300 } 2301 } 2302 } 2303 return true; 2304 } 2305 2306 // ParseInputMachO() parses the named Mach-O file in Filename and handles the 2307 // -arch flags selecting just those slices as specified by them and also parses 2308 // archive files. Then for each individual Mach-O file ProcessMachO() is 2309 // called to process the file based on the command line options. 2310 void parseInputMachO(StringRef Filename) { 2311 if (!ValidateArchFlags()) 2312 return; 2313 2314 // Attempt to open the binary. 2315 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename); 2316 if (!BinaryOrErr) { 2317 if (Error E = isNotObjectErrorInvalidFileType(BinaryOrErr.takeError())) 2318 report_error(std::move(E), Filename); 2319 else 2320 outs() << Filename << ": is not an object file\n"; 2321 return; 2322 } 2323 Binary &Bin = *BinaryOrErr.get().getBinary(); 2324 2325 if (Archive *A = dyn_cast<Archive>(&Bin)) { 2326 outs() << "Archive : " << Filename << "\n"; 2327 if (ArchiveHeaders) 2328 printArchiveHeaders(Filename, A, !NonVerbose, ArchiveMemberOffsets); 2329 2330 Error Err = Error::success(); 2331 for (auto &C : A->children(Err)) { 2332 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary(); 2333 if (!ChildOrErr) { 2334 if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) 2335 report_error(std::move(E), Filename, C); 2336 continue; 2337 } 2338 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) { 2339 if (!checkMachOAndArchFlags(O, Filename)) 2340 return; 2341 ProcessMachO(Filename, O, O->getFileName()); 2342 } 2343 } 2344 if (Err) 2345 report_error(std::move(Err), Filename); 2346 return; 2347 } 2348 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) { 2349 parseInputMachO(UB); 2350 return; 2351 } 2352 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) { 2353 if (!checkMachOAndArchFlags(O, Filename)) 2354 return; 2355 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) 2356 ProcessMachO(Filename, MachOOF); 2357 else 2358 WithColor::error(errs(), "llvm-objdump") 2359 << Filename << "': " 2360 << "object is not a Mach-O file type.\n"; 2361 return; 2362 } 2363 llvm_unreachable("Input object can't be invalid at this point"); 2364 } 2365 2366 void parseInputMachO(MachOUniversalBinary *UB) { 2367 if (!ValidateArchFlags()) 2368 return; 2369 2370 auto Filename = UB->getFileName(); 2371 2372 if (UniversalHeaders) 2373 printMachOUniversalHeaders(UB, !NonVerbose); 2374 2375 // If we have a list of architecture flags specified dump only those. 2376 if (!ArchAll && !ArchFlags.empty()) { 2377 // Look for a slice in the universal binary that matches each ArchFlag. 2378 bool ArchFound; 2379 for (unsigned i = 0; i < ArchFlags.size(); ++i) { 2380 ArchFound = false; 2381 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(), 2382 E = UB->end_objects(); 2383 I != E; ++I) { 2384 if (ArchFlags[i] == I->getArchFlagName()) { 2385 ArchFound = true; 2386 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = 2387 I->getAsObjectFile(); 2388 std::string ArchitectureName = ""; 2389 if (ArchFlags.size() > 1) 2390 ArchitectureName = I->getArchFlagName(); 2391 if (ObjOrErr) { 2392 ObjectFile &O = *ObjOrErr.get(); 2393 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O)) 2394 ProcessMachO(Filename, MachOOF, "", ArchitectureName); 2395 } else if (Error E = isNotObjectErrorInvalidFileType( 2396 ObjOrErr.takeError())) { 2397 report_error(std::move(E), Filename, StringRef(), ArchitectureName); 2398 continue; 2399 } else if (Expected<std::unique_ptr<Archive>> AOrErr = 2400 I->getAsArchive()) { 2401 std::unique_ptr<Archive> &A = *AOrErr; 2402 outs() << "Archive : " << Filename; 2403 if (!ArchitectureName.empty()) 2404 outs() << " (architecture " << ArchitectureName << ")"; 2405 outs() << "\n"; 2406 if (ArchiveHeaders) 2407 printArchiveHeaders(Filename, A.get(), !NonVerbose, 2408 ArchiveMemberOffsets, ArchitectureName); 2409 Error Err = Error::success(); 2410 for (auto &C : A->children(Err)) { 2411 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary(); 2412 if (!ChildOrErr) { 2413 if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) 2414 report_error(std::move(E), Filename, C, ArchitectureName); 2415 continue; 2416 } 2417 if (MachOObjectFile *O = 2418 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) 2419 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName); 2420 } 2421 if (Err) 2422 report_error(std::move(Err), Filename); 2423 } else { 2424 consumeError(AOrErr.takeError()); 2425 error("Mach-O universal file: " + Filename + " for " + 2426 "architecture " + StringRef(I->getArchFlagName()) + 2427 " is not a Mach-O file or an archive file"); 2428 } 2429 } 2430 } 2431 if (!ArchFound) { 2432 WithColor::error(errs(), "llvm-objdump") 2433 << "file: " + Filename + " does not contain " 2434 << "architecture: " + ArchFlags[i] + "\n"; 2435 return; 2436 } 2437 } 2438 return; 2439 } 2440 // No architecture flags were specified so if this contains a slice that 2441 // matches the host architecture dump only that. 2442 if (!ArchAll) { 2443 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(), 2444 E = UB->end_objects(); 2445 I != E; ++I) { 2446 if (MachOObjectFile::getHostArch().getArchName() == 2447 I->getArchFlagName()) { 2448 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile(); 2449 std::string ArchiveName; 2450 ArchiveName.clear(); 2451 if (ObjOrErr) { 2452 ObjectFile &O = *ObjOrErr.get(); 2453 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O)) 2454 ProcessMachO(Filename, MachOOF); 2455 } else if (Error E = 2456 isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) { 2457 report_error(std::move(E), Filename); 2458 } else if (Expected<std::unique_ptr<Archive>> AOrErr = 2459 I->getAsArchive()) { 2460 std::unique_ptr<Archive> &A = *AOrErr; 2461 outs() << "Archive : " << Filename << "\n"; 2462 if (ArchiveHeaders) 2463 printArchiveHeaders(Filename, A.get(), !NonVerbose, 2464 ArchiveMemberOffsets); 2465 Error Err = Error::success(); 2466 for (auto &C : A->children(Err)) { 2467 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary(); 2468 if (!ChildOrErr) { 2469 if (Error E = 2470 isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) 2471 report_error(std::move(E), Filename, C); 2472 continue; 2473 } 2474 if (MachOObjectFile *O = 2475 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) 2476 ProcessMachO(Filename, O, O->getFileName()); 2477 } 2478 if (Err) 2479 report_error(std::move(Err), Filename); 2480 } else { 2481 consumeError(AOrErr.takeError()); 2482 error("Mach-O universal file: " + Filename + " for architecture " + 2483 StringRef(I->getArchFlagName()) + 2484 " is not a Mach-O file or an archive file"); 2485 } 2486 return; 2487 } 2488 } 2489 } 2490 // Either all architectures have been specified or none have been specified 2491 // and this does not contain the host architecture so dump all the slices. 2492 bool moreThanOneArch = UB->getNumberOfObjects() > 1; 2493 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(), 2494 E = UB->end_objects(); 2495 I != E; ++I) { 2496 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile(); 2497 std::string ArchitectureName = ""; 2498 if (moreThanOneArch) 2499 ArchitectureName = I->getArchFlagName(); 2500 if (ObjOrErr) { 2501 ObjectFile &Obj = *ObjOrErr.get(); 2502 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj)) 2503 ProcessMachO(Filename, MachOOF, "", ArchitectureName); 2504 } else if (Error E = 2505 isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) { 2506 report_error(std::move(E), StringRef(), Filename, ArchitectureName); 2507 } else if (Expected<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) { 2508 std::unique_ptr<Archive> &A = *AOrErr; 2509 outs() << "Archive : " << Filename; 2510 if (!ArchitectureName.empty()) 2511 outs() << " (architecture " << ArchitectureName << ")"; 2512 outs() << "\n"; 2513 if (ArchiveHeaders) 2514 printArchiveHeaders(Filename, A.get(), !NonVerbose, 2515 ArchiveMemberOffsets, ArchitectureName); 2516 Error Err = Error::success(); 2517 for (auto &C : A->children(Err)) { 2518 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary(); 2519 if (!ChildOrErr) { 2520 if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) 2521 report_error(std::move(E), Filename, C, ArchitectureName); 2522 continue; 2523 } 2524 if (MachOObjectFile *O = 2525 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) { 2526 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O)) 2527 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(), 2528 ArchitectureName); 2529 } 2530 } 2531 if (Err) 2532 report_error(std::move(Err), Filename); 2533 } else { 2534 consumeError(AOrErr.takeError()); 2535 error("Mach-O universal file: " + Filename + " for architecture " + 2536 StringRef(I->getArchFlagName()) + 2537 " is not a Mach-O file or an archive file"); 2538 } 2539 } 2540 } 2541 2542 // The block of info used by the Symbolizer call backs. 2543 struct DisassembleInfo { 2544 DisassembleInfo(MachOObjectFile *O, SymbolAddressMap *AddrMap, 2545 std::vector<SectionRef> *Sections, bool verbose) 2546 : verbose(verbose), O(O), AddrMap(AddrMap), Sections(Sections) {} 2547 bool verbose; 2548 MachOObjectFile *O; 2549 SectionRef S; 2550 SymbolAddressMap *AddrMap; 2551 std::vector<SectionRef> *Sections; 2552 const char *class_name = nullptr; 2553 const char *selector_name = nullptr; 2554 std::unique_ptr<char[]> method = nullptr; 2555 char *demangled_name = nullptr; 2556 uint64_t adrp_addr = 0; 2557 uint32_t adrp_inst = 0; 2558 std::unique_ptr<SymbolAddressMap> bindtable; 2559 uint32_t depth = 0; 2560 }; 2561 2562 // SymbolizerGetOpInfo() is the operand information call back function. 2563 // This is called to get the symbolic information for operand(s) of an 2564 // instruction when it is being done. This routine does this from 2565 // the relocation information, symbol table, etc. That block of information 2566 // is a pointer to the struct DisassembleInfo that was passed when the 2567 // disassembler context was created and passed to back to here when 2568 // called back by the disassembler for instruction operands that could have 2569 // relocation information. The address of the instruction containing operand is 2570 // at the Pc parameter. The immediate value the operand has is passed in 2571 // op_info->Value and is at Offset past the start of the instruction and has a 2572 // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the 2573 // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol 2574 // names and addends of the symbolic expression to add for the operand. The 2575 // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic 2576 // information is returned then this function returns 1 else it returns 0. 2577 static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset, 2578 uint64_t Size, int TagType, void *TagBuf) { 2579 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo; 2580 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf; 2581 uint64_t value = op_info->Value; 2582 2583 // Make sure all fields returned are zero if we don't set them. 2584 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1)); 2585 op_info->Value = value; 2586 2587 // If the TagType is not the value 1 which it code knows about or if no 2588 // verbose symbolic information is wanted then just return 0, indicating no 2589 // information is being returned. 2590 if (TagType != 1 || !info->verbose) 2591 return 0; 2592 2593 unsigned int Arch = info->O->getArch(); 2594 if (Arch == Triple::x86) { 2595 if (Size != 1 && Size != 2 && Size != 4 && Size != 0) 2596 return 0; 2597 if (info->O->getHeader().filetype != MachO::MH_OBJECT) { 2598 // TODO: 2599 // Search the external relocation entries of a fully linked image 2600 // (if any) for an entry that matches this segment offset. 2601 // uint32_t seg_offset = (Pc + Offset); 2602 return 0; 2603 } 2604 // In MH_OBJECT filetypes search the section's relocation entries (if any) 2605 // for an entry for this section offset. 2606 uint32_t sect_addr = info->S.getAddress(); 2607 uint32_t sect_offset = (Pc + Offset) - sect_addr; 2608 bool reloc_found = false; 2609 DataRefImpl Rel; 2610 MachO::any_relocation_info RE; 2611 bool isExtern = false; 2612 SymbolRef Symbol; 2613 bool r_scattered = false; 2614 uint32_t r_value, pair_r_value, r_type; 2615 for (const RelocationRef &Reloc : info->S.relocations()) { 2616 uint64_t RelocOffset = Reloc.getOffset(); 2617 if (RelocOffset == sect_offset) { 2618 Rel = Reloc.getRawDataRefImpl(); 2619 RE = info->O->getRelocation(Rel); 2620 r_type = info->O->getAnyRelocationType(RE); 2621 r_scattered = info->O->isRelocationScattered(RE); 2622 if (r_scattered) { 2623 r_value = info->O->getScatteredRelocationValue(RE); 2624 if (r_type == MachO::GENERIC_RELOC_SECTDIFF || 2625 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) { 2626 DataRefImpl RelNext = Rel; 2627 info->O->moveRelocationNext(RelNext); 2628 MachO::any_relocation_info RENext; 2629 RENext = info->O->getRelocation(RelNext); 2630 if (info->O->isRelocationScattered(RENext)) 2631 pair_r_value = info->O->getScatteredRelocationValue(RENext); 2632 else 2633 return 0; 2634 } 2635 } else { 2636 isExtern = info->O->getPlainRelocationExternal(RE); 2637 if (isExtern) { 2638 symbol_iterator RelocSym = Reloc.getSymbol(); 2639 Symbol = *RelocSym; 2640 } 2641 } 2642 reloc_found = true; 2643 break; 2644 } 2645 } 2646 if (reloc_found && isExtern) { 2647 op_info->AddSymbol.Present = 1; 2648 op_info->AddSymbol.Name = 2649 unwrapOrError(Symbol.getName(), info->O->getFileName()).data(); 2650 // For i386 extern relocation entries the value in the instruction is 2651 // the offset from the symbol, and value is already set in op_info->Value. 2652 return 1; 2653 } 2654 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF || 2655 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) { 2656 const char *add = GuessSymbolName(r_value, info->AddrMap); 2657 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap); 2658 uint32_t offset = value - (r_value - pair_r_value); 2659 op_info->AddSymbol.Present = 1; 2660 if (add != nullptr) 2661 op_info->AddSymbol.Name = add; 2662 else 2663 op_info->AddSymbol.Value = r_value; 2664 op_info->SubtractSymbol.Present = 1; 2665 if (sub != nullptr) 2666 op_info->SubtractSymbol.Name = sub; 2667 else 2668 op_info->SubtractSymbol.Value = pair_r_value; 2669 op_info->Value = offset; 2670 return 1; 2671 } 2672 return 0; 2673 } 2674 if (Arch == Triple::x86_64) { 2675 if (Size != 1 && Size != 2 && Size != 4 && Size != 0) 2676 return 0; 2677 // For non MH_OBJECT types, like MH_KEXT_BUNDLE, Search the external 2678 // relocation entries of a linked image (if any) for an entry that matches 2679 // this segment offset. 2680 if (info->O->getHeader().filetype != MachO::MH_OBJECT) { 2681 uint64_t seg_offset = Pc + Offset; 2682 bool reloc_found = false; 2683 DataRefImpl Rel; 2684 MachO::any_relocation_info RE; 2685 bool isExtern = false; 2686 SymbolRef Symbol; 2687 for (const RelocationRef &Reloc : info->O->external_relocations()) { 2688 uint64_t RelocOffset = Reloc.getOffset(); 2689 if (RelocOffset == seg_offset) { 2690 Rel = Reloc.getRawDataRefImpl(); 2691 RE = info->O->getRelocation(Rel); 2692 // external relocation entries should always be external. 2693 isExtern = info->O->getPlainRelocationExternal(RE); 2694 if (isExtern) { 2695 symbol_iterator RelocSym = Reloc.getSymbol(); 2696 Symbol = *RelocSym; 2697 } 2698 reloc_found = true; 2699 break; 2700 } 2701 } 2702 if (reloc_found && isExtern) { 2703 // The Value passed in will be adjusted by the Pc if the instruction 2704 // adds the Pc. But for x86_64 external relocation entries the Value 2705 // is the offset from the external symbol. 2706 if (info->O->getAnyRelocationPCRel(RE)) 2707 op_info->Value -= Pc + Offset + Size; 2708 const char *name = 2709 unwrapOrError(Symbol.getName(), info->O->getFileName()).data(); 2710 op_info->AddSymbol.Present = 1; 2711 op_info->AddSymbol.Name = name; 2712 return 1; 2713 } 2714 return 0; 2715 } 2716 // In MH_OBJECT filetypes search the section's relocation entries (if any) 2717 // for an entry for this section offset. 2718 uint64_t sect_addr = info->S.getAddress(); 2719 uint64_t sect_offset = (Pc + Offset) - sect_addr; 2720 bool reloc_found = false; 2721 DataRefImpl Rel; 2722 MachO::any_relocation_info RE; 2723 bool isExtern = false; 2724 SymbolRef Symbol; 2725 for (const RelocationRef &Reloc : info->S.relocations()) { 2726 uint64_t RelocOffset = Reloc.getOffset(); 2727 if (RelocOffset == sect_offset) { 2728 Rel = Reloc.getRawDataRefImpl(); 2729 RE = info->O->getRelocation(Rel); 2730 // NOTE: Scattered relocations don't exist on x86_64. 2731 isExtern = info->O->getPlainRelocationExternal(RE); 2732 if (isExtern) { 2733 symbol_iterator RelocSym = Reloc.getSymbol(); 2734 Symbol = *RelocSym; 2735 } 2736 reloc_found = true; 2737 break; 2738 } 2739 } 2740 if (reloc_found && isExtern) { 2741 // The Value passed in will be adjusted by the Pc if the instruction 2742 // adds the Pc. But for x86_64 external relocation entries the Value 2743 // is the offset from the external symbol. 2744 if (info->O->getAnyRelocationPCRel(RE)) 2745 op_info->Value -= Pc + Offset + Size; 2746 const char *name = 2747 unwrapOrError(Symbol.getName(), info->O->getFileName()).data(); 2748 unsigned Type = info->O->getAnyRelocationType(RE); 2749 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) { 2750 DataRefImpl RelNext = Rel; 2751 info->O->moveRelocationNext(RelNext); 2752 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext); 2753 unsigned TypeNext = info->O->getAnyRelocationType(RENext); 2754 bool isExternNext = info->O->getPlainRelocationExternal(RENext); 2755 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext); 2756 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) { 2757 op_info->SubtractSymbol.Present = 1; 2758 op_info->SubtractSymbol.Name = name; 2759 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum); 2760 Symbol = *RelocSymNext; 2761 name = unwrapOrError(Symbol.getName(), info->O->getFileName()).data(); 2762 } 2763 } 2764 // TODO: add the VariantKinds to op_info->VariantKind for relocation types 2765 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT. 2766 op_info->AddSymbol.Present = 1; 2767 op_info->AddSymbol.Name = name; 2768 return 1; 2769 } 2770 return 0; 2771 } 2772 if (Arch == Triple::arm) { 2773 if (Offset != 0 || (Size != 4 && Size != 2)) 2774 return 0; 2775 if (info->O->getHeader().filetype != MachO::MH_OBJECT) { 2776 // TODO: 2777 // Search the external relocation entries of a fully linked image 2778 // (if any) for an entry that matches this segment offset. 2779 // uint32_t seg_offset = (Pc + Offset); 2780 return 0; 2781 } 2782 // In MH_OBJECT filetypes search the section's relocation entries (if any) 2783 // for an entry for this section offset. 2784 uint32_t sect_addr = info->S.getAddress(); 2785 uint32_t sect_offset = (Pc + Offset) - sect_addr; 2786 DataRefImpl Rel; 2787 MachO::any_relocation_info RE; 2788 bool isExtern = false; 2789 SymbolRef Symbol; 2790 bool r_scattered = false; 2791 uint32_t r_value, pair_r_value, r_type, r_length, other_half; 2792 auto Reloc = 2793 find_if(info->S.relocations(), [&](const RelocationRef &Reloc) { 2794 uint64_t RelocOffset = Reloc.getOffset(); 2795 return RelocOffset == sect_offset; 2796 }); 2797 2798 if (Reloc == info->S.relocations().end()) 2799 return 0; 2800 2801 Rel = Reloc->getRawDataRefImpl(); 2802 RE = info->O->getRelocation(Rel); 2803 r_length = info->O->getAnyRelocationLength(RE); 2804 r_scattered = info->O->isRelocationScattered(RE); 2805 if (r_scattered) { 2806 r_value = info->O->getScatteredRelocationValue(RE); 2807 r_type = info->O->getScatteredRelocationType(RE); 2808 } else { 2809 r_type = info->O->getAnyRelocationType(RE); 2810 isExtern = info->O->getPlainRelocationExternal(RE); 2811 if (isExtern) { 2812 symbol_iterator RelocSym = Reloc->getSymbol(); 2813 Symbol = *RelocSym; 2814 } 2815 } 2816 if (r_type == MachO::ARM_RELOC_HALF || 2817 r_type == MachO::ARM_RELOC_SECTDIFF || 2818 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF || 2819 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) { 2820 DataRefImpl RelNext = Rel; 2821 info->O->moveRelocationNext(RelNext); 2822 MachO::any_relocation_info RENext; 2823 RENext = info->O->getRelocation(RelNext); 2824 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff; 2825 if (info->O->isRelocationScattered(RENext)) 2826 pair_r_value = info->O->getScatteredRelocationValue(RENext); 2827 } 2828 2829 if (isExtern) { 2830 const char *name = 2831 unwrapOrError(Symbol.getName(), info->O->getFileName()).data(); 2832 op_info->AddSymbol.Present = 1; 2833 op_info->AddSymbol.Name = name; 2834 switch (r_type) { 2835 case MachO::ARM_RELOC_HALF: 2836 if ((r_length & 0x1) == 1) { 2837 op_info->Value = value << 16 | other_half; 2838 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16; 2839 } else { 2840 op_info->Value = other_half << 16 | value; 2841 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16; 2842 } 2843 break; 2844 default: 2845 break; 2846 } 2847 return 1; 2848 } 2849 // If we have a branch that is not an external relocation entry then 2850 // return 0 so the code in tryAddingSymbolicOperand() can use the 2851 // SymbolLookUp call back with the branch target address to look up the 2852 // symbol and possibility add an annotation for a symbol stub. 2853 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 || 2854 r_type == MachO::ARM_THUMB_RELOC_BR22)) 2855 return 0; 2856 2857 uint32_t offset = 0; 2858 if (r_type == MachO::ARM_RELOC_HALF || 2859 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) { 2860 if ((r_length & 0x1) == 1) 2861 value = value << 16 | other_half; 2862 else 2863 value = other_half << 16 | value; 2864 } 2865 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF && 2866 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) { 2867 offset = value - r_value; 2868 value = r_value; 2869 } 2870 2871 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) { 2872 if ((r_length & 0x1) == 1) 2873 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16; 2874 else 2875 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16; 2876 const char *add = GuessSymbolName(r_value, info->AddrMap); 2877 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap); 2878 int32_t offset = value - (r_value - pair_r_value); 2879 op_info->AddSymbol.Present = 1; 2880 if (add != nullptr) 2881 op_info->AddSymbol.Name = add; 2882 else 2883 op_info->AddSymbol.Value = r_value; 2884 op_info->SubtractSymbol.Present = 1; 2885 if (sub != nullptr) 2886 op_info->SubtractSymbol.Name = sub; 2887 else 2888 op_info->SubtractSymbol.Value = pair_r_value; 2889 op_info->Value = offset; 2890 return 1; 2891 } 2892 2893 op_info->AddSymbol.Present = 1; 2894 op_info->Value = offset; 2895 if (r_type == MachO::ARM_RELOC_HALF) { 2896 if ((r_length & 0x1) == 1) 2897 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16; 2898 else 2899 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16; 2900 } 2901 const char *add = GuessSymbolName(value, info->AddrMap); 2902 if (add != nullptr) { 2903 op_info->AddSymbol.Name = add; 2904 return 1; 2905 } 2906 op_info->AddSymbol.Value = value; 2907 return 1; 2908 } 2909 if (Arch == Triple::aarch64) { 2910 if (Offset != 0 || Size != 4) 2911 return 0; 2912 if (info->O->getHeader().filetype != MachO::MH_OBJECT) { 2913 // TODO: 2914 // Search the external relocation entries of a fully linked image 2915 // (if any) for an entry that matches this segment offset. 2916 // uint64_t seg_offset = (Pc + Offset); 2917 return 0; 2918 } 2919 // In MH_OBJECT filetypes search the section's relocation entries (if any) 2920 // for an entry for this section offset. 2921 uint64_t sect_addr = info->S.getAddress(); 2922 uint64_t sect_offset = (Pc + Offset) - sect_addr; 2923 auto Reloc = 2924 find_if(info->S.relocations(), [&](const RelocationRef &Reloc) { 2925 uint64_t RelocOffset = Reloc.getOffset(); 2926 return RelocOffset == sect_offset; 2927 }); 2928 2929 if (Reloc == info->S.relocations().end()) 2930 return 0; 2931 2932 DataRefImpl Rel = Reloc->getRawDataRefImpl(); 2933 MachO::any_relocation_info RE = info->O->getRelocation(Rel); 2934 uint32_t r_type = info->O->getAnyRelocationType(RE); 2935 if (r_type == MachO::ARM64_RELOC_ADDEND) { 2936 DataRefImpl RelNext = Rel; 2937 info->O->moveRelocationNext(RelNext); 2938 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext); 2939 if (value == 0) { 2940 value = info->O->getPlainRelocationSymbolNum(RENext); 2941 op_info->Value = value; 2942 } 2943 } 2944 // NOTE: Scattered relocations don't exist on arm64. 2945 if (!info->O->getPlainRelocationExternal(RE)) 2946 return 0; 2947 const char *name = 2948 unwrapOrError(Reloc->getSymbol()->getName(), info->O->getFileName()) 2949 .data(); 2950 op_info->AddSymbol.Present = 1; 2951 op_info->AddSymbol.Name = name; 2952 2953 switch (r_type) { 2954 case MachO::ARM64_RELOC_PAGE21: 2955 /* @page */ 2956 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE; 2957 break; 2958 case MachO::ARM64_RELOC_PAGEOFF12: 2959 /* @pageoff */ 2960 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF; 2961 break; 2962 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21: 2963 /* @gotpage */ 2964 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE; 2965 break; 2966 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12: 2967 /* @gotpageoff */ 2968 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF; 2969 break; 2970 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21: 2971 /* @tvlppage is not implemented in llvm-mc */ 2972 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP; 2973 break; 2974 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12: 2975 /* @tvlppageoff is not implemented in llvm-mc */ 2976 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF; 2977 break; 2978 default: 2979 case MachO::ARM64_RELOC_BRANCH26: 2980 op_info->VariantKind = LLVMDisassembler_VariantKind_None; 2981 break; 2982 } 2983 return 1; 2984 } 2985 return 0; 2986 } 2987 2988 // GuessCstringPointer is passed the address of what might be a pointer to a 2989 // literal string in a cstring section. If that address is in a cstring section 2990 // it returns a pointer to that string. Else it returns nullptr. 2991 static const char *GuessCstringPointer(uint64_t ReferenceValue, 2992 struct DisassembleInfo *info) { 2993 for (const auto &Load : info->O->load_commands()) { 2994 if (Load.C.cmd == MachO::LC_SEGMENT_64) { 2995 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load); 2996 for (unsigned J = 0; J < Seg.nsects; ++J) { 2997 MachO::section_64 Sec = info->O->getSection64(Load, J); 2998 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE; 2999 if (section_type == MachO::S_CSTRING_LITERALS && 3000 ReferenceValue >= Sec.addr && 3001 ReferenceValue < Sec.addr + Sec.size) { 3002 uint64_t sect_offset = ReferenceValue - Sec.addr; 3003 uint64_t object_offset = Sec.offset + sect_offset; 3004 StringRef MachOContents = info->O->getData(); 3005 uint64_t object_size = MachOContents.size(); 3006 const char *object_addr = (const char *)MachOContents.data(); 3007 if (object_offset < object_size) { 3008 const char *name = object_addr + object_offset; 3009 return name; 3010 } else { 3011 return nullptr; 3012 } 3013 } 3014 } 3015 } else if (Load.C.cmd == MachO::LC_SEGMENT) { 3016 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load); 3017 for (unsigned J = 0; J < Seg.nsects; ++J) { 3018 MachO::section Sec = info->O->getSection(Load, J); 3019 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE; 3020 if (section_type == MachO::S_CSTRING_LITERALS && 3021 ReferenceValue >= Sec.addr && 3022 ReferenceValue < Sec.addr + Sec.size) { 3023 uint64_t sect_offset = ReferenceValue - Sec.addr; 3024 uint64_t object_offset = Sec.offset + sect_offset; 3025 StringRef MachOContents = info->O->getData(); 3026 uint64_t object_size = MachOContents.size(); 3027 const char *object_addr = (const char *)MachOContents.data(); 3028 if (object_offset < object_size) { 3029 const char *name = object_addr + object_offset; 3030 return name; 3031 } else { 3032 return nullptr; 3033 } 3034 } 3035 } 3036 } 3037 } 3038 return nullptr; 3039 } 3040 3041 // GuessIndirectSymbol returns the name of the indirect symbol for the 3042 // ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe 3043 // an address of a symbol stub or a lazy or non-lazy pointer to associate the 3044 // symbol name being referenced by the stub or pointer. 3045 static const char *GuessIndirectSymbol(uint64_t ReferenceValue, 3046 struct DisassembleInfo *info) { 3047 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand(); 3048 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand(); 3049 for (const auto &Load : info->O->load_commands()) { 3050 if (Load.C.cmd == MachO::LC_SEGMENT_64) { 3051 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load); 3052 for (unsigned J = 0; J < Seg.nsects; ++J) { 3053 MachO::section_64 Sec = info->O->getSection64(Load, J); 3054 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE; 3055 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS || 3056 section_type == MachO::S_LAZY_SYMBOL_POINTERS || 3057 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS || 3058 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS || 3059 section_type == MachO::S_SYMBOL_STUBS) && 3060 ReferenceValue >= Sec.addr && 3061 ReferenceValue < Sec.addr + Sec.size) { 3062 uint32_t stride; 3063 if (section_type == MachO::S_SYMBOL_STUBS) 3064 stride = Sec.reserved2; 3065 else 3066 stride = 8; 3067 if (stride == 0) 3068 return nullptr; 3069 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride; 3070 if (index < Dysymtab.nindirectsyms) { 3071 uint32_t indirect_symbol = 3072 info->O->getIndirectSymbolTableEntry(Dysymtab, index); 3073 if (indirect_symbol < Symtab.nsyms) { 3074 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol); 3075 return unwrapOrError(Sym->getName(), info->O->getFileName()) 3076 .data(); 3077 } 3078 } 3079 } 3080 } 3081 } else if (Load.C.cmd == MachO::LC_SEGMENT) { 3082 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load); 3083 for (unsigned J = 0; J < Seg.nsects; ++J) { 3084 MachO::section Sec = info->O->getSection(Load, J); 3085 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE; 3086 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS || 3087 section_type == MachO::S_LAZY_SYMBOL_POINTERS || 3088 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS || 3089 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS || 3090 section_type == MachO::S_SYMBOL_STUBS) && 3091 ReferenceValue >= Sec.addr && 3092 ReferenceValue < Sec.addr + Sec.size) { 3093 uint32_t stride; 3094 if (section_type == MachO::S_SYMBOL_STUBS) 3095 stride = Sec.reserved2; 3096 else 3097 stride = 4; 3098 if (stride == 0) 3099 return nullptr; 3100 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride; 3101 if (index < Dysymtab.nindirectsyms) { 3102 uint32_t indirect_symbol = 3103 info->O->getIndirectSymbolTableEntry(Dysymtab, index); 3104 if (indirect_symbol < Symtab.nsyms) { 3105 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol); 3106 return unwrapOrError(Sym->getName(), info->O->getFileName()) 3107 .data(); 3108 } 3109 } 3110 } 3111 } 3112 } 3113 } 3114 return nullptr; 3115 } 3116 3117 // method_reference() is called passing it the ReferenceName that might be 3118 // a reference it to an Objective-C method call. If so then it allocates and 3119 // assembles a method call string with the values last seen and saved in 3120 // the DisassembleInfo's class_name and selector_name fields. This is saved 3121 // into the method field of the info and any previous string is free'ed. 3122 // Then the class_name field in the info is set to nullptr. The method call 3123 // string is set into ReferenceName and ReferenceType is set to 3124 // LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call 3125 // then both ReferenceType and ReferenceName are left unchanged. 3126 static void method_reference(struct DisassembleInfo *info, 3127 uint64_t *ReferenceType, 3128 const char **ReferenceName) { 3129 unsigned int Arch = info->O->getArch(); 3130 if (*ReferenceName != nullptr) { 3131 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) { 3132 if (info->selector_name != nullptr) { 3133 if (info->class_name != nullptr) { 3134 info->method = std::make_unique<char[]>( 3135 5 + strlen(info->class_name) + strlen(info->selector_name)); 3136 char *method = info->method.get(); 3137 if (method != nullptr) { 3138 strcpy(method, "+["); 3139 strcat(method, info->class_name); 3140 strcat(method, " "); 3141 strcat(method, info->selector_name); 3142 strcat(method, "]"); 3143 *ReferenceName = method; 3144 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message; 3145 } 3146 } else { 3147 info->method = 3148 std::make_unique<char[]>(9 + strlen(info->selector_name)); 3149 char *method = info->method.get(); 3150 if (method != nullptr) { 3151 if (Arch == Triple::x86_64) 3152 strcpy(method, "-[%rdi "); 3153 else if (Arch == Triple::aarch64) 3154 strcpy(method, "-[x0 "); 3155 else 3156 strcpy(method, "-[r? "); 3157 strcat(method, info->selector_name); 3158 strcat(method, "]"); 3159 *ReferenceName = method; 3160 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message; 3161 } 3162 } 3163 info->class_name = nullptr; 3164 } 3165 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) { 3166 if (info->selector_name != nullptr) { 3167 info->method = 3168 std::make_unique<char[]>(17 + strlen(info->selector_name)); 3169 char *method = info->method.get(); 3170 if (method != nullptr) { 3171 if (Arch == Triple::x86_64) 3172 strcpy(method, "-[[%rdi super] "); 3173 else if (Arch == Triple::aarch64) 3174 strcpy(method, "-[[x0 super] "); 3175 else 3176 strcpy(method, "-[[r? super] "); 3177 strcat(method, info->selector_name); 3178 strcat(method, "]"); 3179 *ReferenceName = method; 3180 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message; 3181 } 3182 info->class_name = nullptr; 3183 } 3184 } 3185 } 3186 } 3187 3188 // GuessPointerPointer() is passed the address of what might be a pointer to 3189 // a reference to an Objective-C class, selector, message ref or cfstring. 3190 // If so the value of the pointer is returned and one of the booleans are set 3191 // to true. If not zero is returned and all the booleans are set to false. 3192 static uint64_t GuessPointerPointer(uint64_t ReferenceValue, 3193 struct DisassembleInfo *info, 3194 bool &classref, bool &selref, bool &msgref, 3195 bool &cfstring) { 3196 classref = false; 3197 selref = false; 3198 msgref = false; 3199 cfstring = false; 3200 for (const auto &Load : info->O->load_commands()) { 3201 if (Load.C.cmd == MachO::LC_SEGMENT_64) { 3202 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load); 3203 for (unsigned J = 0; J < Seg.nsects; ++J) { 3204 MachO::section_64 Sec = info->O->getSection64(Load, J); 3205 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 || 3206 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 || 3207 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 || 3208 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 || 3209 strncmp(Sec.sectname, "__cfstring", 16) == 0) && 3210 ReferenceValue >= Sec.addr && 3211 ReferenceValue < Sec.addr + Sec.size) { 3212 uint64_t sect_offset = ReferenceValue - Sec.addr; 3213 uint64_t object_offset = Sec.offset + sect_offset; 3214 StringRef MachOContents = info->O->getData(); 3215 uint64_t object_size = MachOContents.size(); 3216 const char *object_addr = (const char *)MachOContents.data(); 3217 if (object_offset < object_size) { 3218 uint64_t pointer_value; 3219 memcpy(&pointer_value, object_addr + object_offset, 3220 sizeof(uint64_t)); 3221 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 3222 sys::swapByteOrder(pointer_value); 3223 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0) 3224 selref = true; 3225 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 || 3226 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0) 3227 classref = true; 3228 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 && 3229 ReferenceValue + 8 < Sec.addr + Sec.size) { 3230 msgref = true; 3231 memcpy(&pointer_value, object_addr + object_offset + 8, 3232 sizeof(uint64_t)); 3233 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 3234 sys::swapByteOrder(pointer_value); 3235 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0) 3236 cfstring = true; 3237 return pointer_value; 3238 } else { 3239 return 0; 3240 } 3241 } 3242 } 3243 } 3244 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files. 3245 } 3246 return 0; 3247 } 3248 3249 // get_pointer_64 returns a pointer to the bytes in the object file at the 3250 // Address from a section in the Mach-O file. And indirectly returns the 3251 // offset into the section, number of bytes left in the section past the offset 3252 // and which section is was being referenced. If the Address is not in a 3253 // section nullptr is returned. 3254 static const char *get_pointer_64(uint64_t Address, uint32_t &offset, 3255 uint32_t &left, SectionRef &S, 3256 DisassembleInfo *info, 3257 bool objc_only = false) { 3258 offset = 0; 3259 left = 0; 3260 S = SectionRef(); 3261 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) { 3262 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress(); 3263 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize(); 3264 if (SectSize == 0) 3265 continue; 3266 if (objc_only) { 3267 StringRef SectName; 3268 Expected<StringRef> SecNameOrErr = 3269 ((*(info->Sections))[SectIdx]).getName(); 3270 if (SecNameOrErr) 3271 SectName = *SecNameOrErr; 3272 else 3273 consumeError(SecNameOrErr.takeError()); 3274 3275 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl(); 3276 StringRef SegName = info->O->getSectionFinalSegmentName(Ref); 3277 if (SegName != "__OBJC" && SectName != "__cstring") 3278 continue; 3279 } 3280 if (Address >= SectAddress && Address < SectAddress + SectSize) { 3281 S = (*(info->Sections))[SectIdx]; 3282 offset = Address - SectAddress; 3283 left = SectSize - offset; 3284 StringRef SectContents = unwrapOrError( 3285 ((*(info->Sections))[SectIdx]).getContents(), info->O->getFileName()); 3286 return SectContents.data() + offset; 3287 } 3288 } 3289 return nullptr; 3290 } 3291 3292 static const char *get_pointer_32(uint32_t Address, uint32_t &offset, 3293 uint32_t &left, SectionRef &S, 3294 DisassembleInfo *info, 3295 bool objc_only = false) { 3296 return get_pointer_64(Address, offset, left, S, info, objc_only); 3297 } 3298 3299 // get_symbol_64() returns the name of a symbol (or nullptr) and the address of 3300 // the symbol indirectly through n_value. Based on the relocation information 3301 // for the specified section offset in the specified section reference. 3302 // If no relocation information is found and a non-zero ReferenceValue for the 3303 // symbol is passed, look up that address in the info's AddrMap. 3304 static const char *get_symbol_64(uint32_t sect_offset, SectionRef S, 3305 DisassembleInfo *info, uint64_t &n_value, 3306 uint64_t ReferenceValue = 0) { 3307 n_value = 0; 3308 if (!info->verbose) 3309 return nullptr; 3310 3311 // See if there is an external relocation entry at the sect_offset. 3312 bool reloc_found = false; 3313 DataRefImpl Rel; 3314 MachO::any_relocation_info RE; 3315 bool isExtern = false; 3316 SymbolRef Symbol; 3317 for (const RelocationRef &Reloc : S.relocations()) { 3318 uint64_t RelocOffset = Reloc.getOffset(); 3319 if (RelocOffset == sect_offset) { 3320 Rel = Reloc.getRawDataRefImpl(); 3321 RE = info->O->getRelocation(Rel); 3322 if (info->O->isRelocationScattered(RE)) 3323 continue; 3324 isExtern = info->O->getPlainRelocationExternal(RE); 3325 if (isExtern) { 3326 symbol_iterator RelocSym = Reloc.getSymbol(); 3327 Symbol = *RelocSym; 3328 } 3329 reloc_found = true; 3330 break; 3331 } 3332 } 3333 // If there is an external relocation entry for a symbol in this section 3334 // at this section_offset then use that symbol's value for the n_value 3335 // and return its name. 3336 const char *SymbolName = nullptr; 3337 if (reloc_found && isExtern) { 3338 n_value = Symbol.getValue(); 3339 StringRef Name = unwrapOrError(Symbol.getName(), info->O->getFileName()); 3340 if (!Name.empty()) { 3341 SymbolName = Name.data(); 3342 return SymbolName; 3343 } 3344 } 3345 3346 // TODO: For fully linked images, look through the external relocation 3347 // entries off the dynamic symtab command. For these the r_offset is from the 3348 // start of the first writeable segment in the Mach-O file. So the offset 3349 // to this section from that segment is passed to this routine by the caller, 3350 // as the database_offset. Which is the difference of the section's starting 3351 // address and the first writable segment. 3352 // 3353 // NOTE: need add passing the database_offset to this routine. 3354 3355 // We did not find an external relocation entry so look up the ReferenceValue 3356 // as an address of a symbol and if found return that symbol's name. 3357 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap); 3358 3359 return SymbolName; 3360 } 3361 3362 static const char *get_symbol_32(uint32_t sect_offset, SectionRef S, 3363 DisassembleInfo *info, 3364 uint32_t ReferenceValue) { 3365 uint64_t n_value64; 3366 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue); 3367 } 3368 3369 // These are structs in the Objective-C meta data and read to produce the 3370 // comments for disassembly. While these are part of the ABI they are no 3371 // public defintions. So the are here not in include/llvm/BinaryFormat/MachO.h 3372 // . 3373 3374 // The cfstring object in a 64-bit Mach-O file. 3375 struct cfstring64_t { 3376 uint64_t isa; // class64_t * (64-bit pointer) 3377 uint64_t flags; // flag bits 3378 uint64_t characters; // char * (64-bit pointer) 3379 uint64_t length; // number of non-NULL characters in above 3380 }; 3381 3382 // The class object in a 64-bit Mach-O file. 3383 struct class64_t { 3384 uint64_t isa; // class64_t * (64-bit pointer) 3385 uint64_t superclass; // class64_t * (64-bit pointer) 3386 uint64_t cache; // Cache (64-bit pointer) 3387 uint64_t vtable; // IMP * (64-bit pointer) 3388 uint64_t data; // class_ro64_t * (64-bit pointer) 3389 }; 3390 3391 struct class32_t { 3392 uint32_t isa; /* class32_t * (32-bit pointer) */ 3393 uint32_t superclass; /* class32_t * (32-bit pointer) */ 3394 uint32_t cache; /* Cache (32-bit pointer) */ 3395 uint32_t vtable; /* IMP * (32-bit pointer) */ 3396 uint32_t data; /* class_ro32_t * (32-bit pointer) */ 3397 }; 3398 3399 struct class_ro64_t { 3400 uint32_t flags; 3401 uint32_t instanceStart; 3402 uint32_t instanceSize; 3403 uint32_t reserved; 3404 uint64_t ivarLayout; // const uint8_t * (64-bit pointer) 3405 uint64_t name; // const char * (64-bit pointer) 3406 uint64_t baseMethods; // const method_list_t * (64-bit pointer) 3407 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer) 3408 uint64_t ivars; // const ivar_list_t * (64-bit pointer) 3409 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer) 3410 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer) 3411 }; 3412 3413 struct class_ro32_t { 3414 uint32_t flags; 3415 uint32_t instanceStart; 3416 uint32_t instanceSize; 3417 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */ 3418 uint32_t name; /* const char * (32-bit pointer) */ 3419 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */ 3420 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */ 3421 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */ 3422 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */ 3423 uint32_t baseProperties; /* const struct objc_property_list * 3424 (32-bit pointer) */ 3425 }; 3426 3427 /* Values for class_ro{64,32}_t->flags */ 3428 #define RO_META (1 << 0) 3429 #define RO_ROOT (1 << 1) 3430 #define RO_HAS_CXX_STRUCTORS (1 << 2) 3431 3432 struct method_list64_t { 3433 uint32_t entsize; 3434 uint32_t count; 3435 /* struct method64_t first; These structures follow inline */ 3436 }; 3437 3438 struct method_list32_t { 3439 uint32_t entsize; 3440 uint32_t count; 3441 /* struct method32_t first; These structures follow inline */ 3442 }; 3443 3444 struct method64_t { 3445 uint64_t name; /* SEL (64-bit pointer) */ 3446 uint64_t types; /* const char * (64-bit pointer) */ 3447 uint64_t imp; /* IMP (64-bit pointer) */ 3448 }; 3449 3450 struct method32_t { 3451 uint32_t name; /* SEL (32-bit pointer) */ 3452 uint32_t types; /* const char * (32-bit pointer) */ 3453 uint32_t imp; /* IMP (32-bit pointer) */ 3454 }; 3455 3456 struct protocol_list64_t { 3457 uint64_t count; /* uintptr_t (a 64-bit value) */ 3458 /* struct protocol64_t * list[0]; These pointers follow inline */ 3459 }; 3460 3461 struct protocol_list32_t { 3462 uint32_t count; /* uintptr_t (a 32-bit value) */ 3463 /* struct protocol32_t * list[0]; These pointers follow inline */ 3464 }; 3465 3466 struct protocol64_t { 3467 uint64_t isa; /* id * (64-bit pointer) */ 3468 uint64_t name; /* const char * (64-bit pointer) */ 3469 uint64_t protocols; /* struct protocol_list64_t * 3470 (64-bit pointer) */ 3471 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */ 3472 uint64_t classMethods; /* method_list_t * (64-bit pointer) */ 3473 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */ 3474 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */ 3475 uint64_t instanceProperties; /* struct objc_property_list * 3476 (64-bit pointer) */ 3477 }; 3478 3479 struct protocol32_t { 3480 uint32_t isa; /* id * (32-bit pointer) */ 3481 uint32_t name; /* const char * (32-bit pointer) */ 3482 uint32_t protocols; /* struct protocol_list_t * 3483 (32-bit pointer) */ 3484 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */ 3485 uint32_t classMethods; /* method_list_t * (32-bit pointer) */ 3486 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */ 3487 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */ 3488 uint32_t instanceProperties; /* struct objc_property_list * 3489 (32-bit pointer) */ 3490 }; 3491 3492 struct ivar_list64_t { 3493 uint32_t entsize; 3494 uint32_t count; 3495 /* struct ivar64_t first; These structures follow inline */ 3496 }; 3497 3498 struct ivar_list32_t { 3499 uint32_t entsize; 3500 uint32_t count; 3501 /* struct ivar32_t first; These structures follow inline */ 3502 }; 3503 3504 struct ivar64_t { 3505 uint64_t offset; /* uintptr_t * (64-bit pointer) */ 3506 uint64_t name; /* const char * (64-bit pointer) */ 3507 uint64_t type; /* const char * (64-bit pointer) */ 3508 uint32_t alignment; 3509 uint32_t size; 3510 }; 3511 3512 struct ivar32_t { 3513 uint32_t offset; /* uintptr_t * (32-bit pointer) */ 3514 uint32_t name; /* const char * (32-bit pointer) */ 3515 uint32_t type; /* const char * (32-bit pointer) */ 3516 uint32_t alignment; 3517 uint32_t size; 3518 }; 3519 3520 struct objc_property_list64 { 3521 uint32_t entsize; 3522 uint32_t count; 3523 /* struct objc_property64 first; These structures follow inline */ 3524 }; 3525 3526 struct objc_property_list32 { 3527 uint32_t entsize; 3528 uint32_t count; 3529 /* struct objc_property32 first; These structures follow inline */ 3530 }; 3531 3532 struct objc_property64 { 3533 uint64_t name; /* const char * (64-bit pointer) */ 3534 uint64_t attributes; /* const char * (64-bit pointer) */ 3535 }; 3536 3537 struct objc_property32 { 3538 uint32_t name; /* const char * (32-bit pointer) */ 3539 uint32_t attributes; /* const char * (32-bit pointer) */ 3540 }; 3541 3542 struct category64_t { 3543 uint64_t name; /* const char * (64-bit pointer) */ 3544 uint64_t cls; /* struct class_t * (64-bit pointer) */ 3545 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */ 3546 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */ 3547 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */ 3548 uint64_t instanceProperties; /* struct objc_property_list * 3549 (64-bit pointer) */ 3550 }; 3551 3552 struct category32_t { 3553 uint32_t name; /* const char * (32-bit pointer) */ 3554 uint32_t cls; /* struct class_t * (32-bit pointer) */ 3555 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */ 3556 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */ 3557 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */ 3558 uint32_t instanceProperties; /* struct objc_property_list * 3559 (32-bit pointer) */ 3560 }; 3561 3562 struct objc_image_info64 { 3563 uint32_t version; 3564 uint32_t flags; 3565 }; 3566 struct objc_image_info32 { 3567 uint32_t version; 3568 uint32_t flags; 3569 }; 3570 struct imageInfo_t { 3571 uint32_t version; 3572 uint32_t flags; 3573 }; 3574 /* masks for objc_image_info.flags */ 3575 #define OBJC_IMAGE_IS_REPLACEMENT (1 << 0) 3576 #define OBJC_IMAGE_SUPPORTS_GC (1 << 1) 3577 #define OBJC_IMAGE_IS_SIMULATED (1 << 5) 3578 #define OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES (1 << 6) 3579 3580 struct message_ref64 { 3581 uint64_t imp; /* IMP (64-bit pointer) */ 3582 uint64_t sel; /* SEL (64-bit pointer) */ 3583 }; 3584 3585 struct message_ref32 { 3586 uint32_t imp; /* IMP (32-bit pointer) */ 3587 uint32_t sel; /* SEL (32-bit pointer) */ 3588 }; 3589 3590 // Objective-C 1 (32-bit only) meta data structs. 3591 3592 struct objc_module_t { 3593 uint32_t version; 3594 uint32_t size; 3595 uint32_t name; /* char * (32-bit pointer) */ 3596 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */ 3597 }; 3598 3599 struct objc_symtab_t { 3600 uint32_t sel_ref_cnt; 3601 uint32_t refs; /* SEL * (32-bit pointer) */ 3602 uint16_t cls_def_cnt; 3603 uint16_t cat_def_cnt; 3604 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */ 3605 }; 3606 3607 struct objc_class_t { 3608 uint32_t isa; /* struct objc_class * (32-bit pointer) */ 3609 uint32_t super_class; /* struct objc_class * (32-bit pointer) */ 3610 uint32_t name; /* const char * (32-bit pointer) */ 3611 int32_t version; 3612 int32_t info; 3613 int32_t instance_size; 3614 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */ 3615 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */ 3616 uint32_t cache; /* struct objc_cache * (32-bit pointer) */ 3617 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */ 3618 }; 3619 3620 #define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask)) 3621 // class is not a metaclass 3622 #define CLS_CLASS 0x1 3623 // class is a metaclass 3624 #define CLS_META 0x2 3625 3626 struct objc_category_t { 3627 uint32_t category_name; /* char * (32-bit pointer) */ 3628 uint32_t class_name; /* char * (32-bit pointer) */ 3629 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */ 3630 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */ 3631 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */ 3632 }; 3633 3634 struct objc_ivar_t { 3635 uint32_t ivar_name; /* char * (32-bit pointer) */ 3636 uint32_t ivar_type; /* char * (32-bit pointer) */ 3637 int32_t ivar_offset; 3638 }; 3639 3640 struct objc_ivar_list_t { 3641 int32_t ivar_count; 3642 // struct objc_ivar_t ivar_list[1]; /* variable length structure */ 3643 }; 3644 3645 struct objc_method_list_t { 3646 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */ 3647 int32_t method_count; 3648 // struct objc_method_t method_list[1]; /* variable length structure */ 3649 }; 3650 3651 struct objc_method_t { 3652 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */ 3653 uint32_t method_types; /* char * (32-bit pointer) */ 3654 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...) 3655 (32-bit pointer) */ 3656 }; 3657 3658 struct objc_protocol_list_t { 3659 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */ 3660 int32_t count; 3661 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t * 3662 // (32-bit pointer) */ 3663 }; 3664 3665 struct objc_protocol_t { 3666 uint32_t isa; /* struct objc_class * (32-bit pointer) */ 3667 uint32_t protocol_name; /* char * (32-bit pointer) */ 3668 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */ 3669 uint32_t instance_methods; /* struct objc_method_description_list * 3670 (32-bit pointer) */ 3671 uint32_t class_methods; /* struct objc_method_description_list * 3672 (32-bit pointer) */ 3673 }; 3674 3675 struct objc_method_description_list_t { 3676 int32_t count; 3677 // struct objc_method_description_t list[1]; 3678 }; 3679 3680 struct objc_method_description_t { 3681 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */ 3682 uint32_t types; /* char * (32-bit pointer) */ 3683 }; 3684 3685 inline void swapStruct(struct cfstring64_t &cfs) { 3686 sys::swapByteOrder(cfs.isa); 3687 sys::swapByteOrder(cfs.flags); 3688 sys::swapByteOrder(cfs.characters); 3689 sys::swapByteOrder(cfs.length); 3690 } 3691 3692 inline void swapStruct(struct class64_t &c) { 3693 sys::swapByteOrder(c.isa); 3694 sys::swapByteOrder(c.superclass); 3695 sys::swapByteOrder(c.cache); 3696 sys::swapByteOrder(c.vtable); 3697 sys::swapByteOrder(c.data); 3698 } 3699 3700 inline void swapStruct(struct class32_t &c) { 3701 sys::swapByteOrder(c.isa); 3702 sys::swapByteOrder(c.superclass); 3703 sys::swapByteOrder(c.cache); 3704 sys::swapByteOrder(c.vtable); 3705 sys::swapByteOrder(c.data); 3706 } 3707 3708 inline void swapStruct(struct class_ro64_t &cro) { 3709 sys::swapByteOrder(cro.flags); 3710 sys::swapByteOrder(cro.instanceStart); 3711 sys::swapByteOrder(cro.instanceSize); 3712 sys::swapByteOrder(cro.reserved); 3713 sys::swapByteOrder(cro.ivarLayout); 3714 sys::swapByteOrder(cro.name); 3715 sys::swapByteOrder(cro.baseMethods); 3716 sys::swapByteOrder(cro.baseProtocols); 3717 sys::swapByteOrder(cro.ivars); 3718 sys::swapByteOrder(cro.weakIvarLayout); 3719 sys::swapByteOrder(cro.baseProperties); 3720 } 3721 3722 inline void swapStruct(struct class_ro32_t &cro) { 3723 sys::swapByteOrder(cro.flags); 3724 sys::swapByteOrder(cro.instanceStart); 3725 sys::swapByteOrder(cro.instanceSize); 3726 sys::swapByteOrder(cro.ivarLayout); 3727 sys::swapByteOrder(cro.name); 3728 sys::swapByteOrder(cro.baseMethods); 3729 sys::swapByteOrder(cro.baseProtocols); 3730 sys::swapByteOrder(cro.ivars); 3731 sys::swapByteOrder(cro.weakIvarLayout); 3732 sys::swapByteOrder(cro.baseProperties); 3733 } 3734 3735 inline void swapStruct(struct method_list64_t &ml) { 3736 sys::swapByteOrder(ml.entsize); 3737 sys::swapByteOrder(ml.count); 3738 } 3739 3740 inline void swapStruct(struct method_list32_t &ml) { 3741 sys::swapByteOrder(ml.entsize); 3742 sys::swapByteOrder(ml.count); 3743 } 3744 3745 inline void swapStruct(struct method64_t &m) { 3746 sys::swapByteOrder(m.name); 3747 sys::swapByteOrder(m.types); 3748 sys::swapByteOrder(m.imp); 3749 } 3750 3751 inline void swapStruct(struct method32_t &m) { 3752 sys::swapByteOrder(m.name); 3753 sys::swapByteOrder(m.types); 3754 sys::swapByteOrder(m.imp); 3755 } 3756 3757 inline void swapStruct(struct protocol_list64_t &pl) { 3758 sys::swapByteOrder(pl.count); 3759 } 3760 3761 inline void swapStruct(struct protocol_list32_t &pl) { 3762 sys::swapByteOrder(pl.count); 3763 } 3764 3765 inline void swapStruct(struct protocol64_t &p) { 3766 sys::swapByteOrder(p.isa); 3767 sys::swapByteOrder(p.name); 3768 sys::swapByteOrder(p.protocols); 3769 sys::swapByteOrder(p.instanceMethods); 3770 sys::swapByteOrder(p.classMethods); 3771 sys::swapByteOrder(p.optionalInstanceMethods); 3772 sys::swapByteOrder(p.optionalClassMethods); 3773 sys::swapByteOrder(p.instanceProperties); 3774 } 3775 3776 inline void swapStruct(struct protocol32_t &p) { 3777 sys::swapByteOrder(p.isa); 3778 sys::swapByteOrder(p.name); 3779 sys::swapByteOrder(p.protocols); 3780 sys::swapByteOrder(p.instanceMethods); 3781 sys::swapByteOrder(p.classMethods); 3782 sys::swapByteOrder(p.optionalInstanceMethods); 3783 sys::swapByteOrder(p.optionalClassMethods); 3784 sys::swapByteOrder(p.instanceProperties); 3785 } 3786 3787 inline void swapStruct(struct ivar_list64_t &il) { 3788 sys::swapByteOrder(il.entsize); 3789 sys::swapByteOrder(il.count); 3790 } 3791 3792 inline void swapStruct(struct ivar_list32_t &il) { 3793 sys::swapByteOrder(il.entsize); 3794 sys::swapByteOrder(il.count); 3795 } 3796 3797 inline void swapStruct(struct ivar64_t &i) { 3798 sys::swapByteOrder(i.offset); 3799 sys::swapByteOrder(i.name); 3800 sys::swapByteOrder(i.type); 3801 sys::swapByteOrder(i.alignment); 3802 sys::swapByteOrder(i.size); 3803 } 3804 3805 inline void swapStruct(struct ivar32_t &i) { 3806 sys::swapByteOrder(i.offset); 3807 sys::swapByteOrder(i.name); 3808 sys::swapByteOrder(i.type); 3809 sys::swapByteOrder(i.alignment); 3810 sys::swapByteOrder(i.size); 3811 } 3812 3813 inline void swapStruct(struct objc_property_list64 &pl) { 3814 sys::swapByteOrder(pl.entsize); 3815 sys::swapByteOrder(pl.count); 3816 } 3817 3818 inline void swapStruct(struct objc_property_list32 &pl) { 3819 sys::swapByteOrder(pl.entsize); 3820 sys::swapByteOrder(pl.count); 3821 } 3822 3823 inline void swapStruct(struct objc_property64 &op) { 3824 sys::swapByteOrder(op.name); 3825 sys::swapByteOrder(op.attributes); 3826 } 3827 3828 inline void swapStruct(struct objc_property32 &op) { 3829 sys::swapByteOrder(op.name); 3830 sys::swapByteOrder(op.attributes); 3831 } 3832 3833 inline void swapStruct(struct category64_t &c) { 3834 sys::swapByteOrder(c.name); 3835 sys::swapByteOrder(c.cls); 3836 sys::swapByteOrder(c.instanceMethods); 3837 sys::swapByteOrder(c.classMethods); 3838 sys::swapByteOrder(c.protocols); 3839 sys::swapByteOrder(c.instanceProperties); 3840 } 3841 3842 inline void swapStruct(struct category32_t &c) { 3843 sys::swapByteOrder(c.name); 3844 sys::swapByteOrder(c.cls); 3845 sys::swapByteOrder(c.instanceMethods); 3846 sys::swapByteOrder(c.classMethods); 3847 sys::swapByteOrder(c.protocols); 3848 sys::swapByteOrder(c.instanceProperties); 3849 } 3850 3851 inline void swapStruct(struct objc_image_info64 &o) { 3852 sys::swapByteOrder(o.version); 3853 sys::swapByteOrder(o.flags); 3854 } 3855 3856 inline void swapStruct(struct objc_image_info32 &o) { 3857 sys::swapByteOrder(o.version); 3858 sys::swapByteOrder(o.flags); 3859 } 3860 3861 inline void swapStruct(struct imageInfo_t &o) { 3862 sys::swapByteOrder(o.version); 3863 sys::swapByteOrder(o.flags); 3864 } 3865 3866 inline void swapStruct(struct message_ref64 &mr) { 3867 sys::swapByteOrder(mr.imp); 3868 sys::swapByteOrder(mr.sel); 3869 } 3870 3871 inline void swapStruct(struct message_ref32 &mr) { 3872 sys::swapByteOrder(mr.imp); 3873 sys::swapByteOrder(mr.sel); 3874 } 3875 3876 inline void swapStruct(struct objc_module_t &module) { 3877 sys::swapByteOrder(module.version); 3878 sys::swapByteOrder(module.size); 3879 sys::swapByteOrder(module.name); 3880 sys::swapByteOrder(module.symtab); 3881 } 3882 3883 inline void swapStruct(struct objc_symtab_t &symtab) { 3884 sys::swapByteOrder(symtab.sel_ref_cnt); 3885 sys::swapByteOrder(symtab.refs); 3886 sys::swapByteOrder(symtab.cls_def_cnt); 3887 sys::swapByteOrder(symtab.cat_def_cnt); 3888 } 3889 3890 inline void swapStruct(struct objc_class_t &objc_class) { 3891 sys::swapByteOrder(objc_class.isa); 3892 sys::swapByteOrder(objc_class.super_class); 3893 sys::swapByteOrder(objc_class.name); 3894 sys::swapByteOrder(objc_class.version); 3895 sys::swapByteOrder(objc_class.info); 3896 sys::swapByteOrder(objc_class.instance_size); 3897 sys::swapByteOrder(objc_class.ivars); 3898 sys::swapByteOrder(objc_class.methodLists); 3899 sys::swapByteOrder(objc_class.cache); 3900 sys::swapByteOrder(objc_class.protocols); 3901 } 3902 3903 inline void swapStruct(struct objc_category_t &objc_category) { 3904 sys::swapByteOrder(objc_category.category_name); 3905 sys::swapByteOrder(objc_category.class_name); 3906 sys::swapByteOrder(objc_category.instance_methods); 3907 sys::swapByteOrder(objc_category.class_methods); 3908 sys::swapByteOrder(objc_category.protocols); 3909 } 3910 3911 inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) { 3912 sys::swapByteOrder(objc_ivar_list.ivar_count); 3913 } 3914 3915 inline void swapStruct(struct objc_ivar_t &objc_ivar) { 3916 sys::swapByteOrder(objc_ivar.ivar_name); 3917 sys::swapByteOrder(objc_ivar.ivar_type); 3918 sys::swapByteOrder(objc_ivar.ivar_offset); 3919 } 3920 3921 inline void swapStruct(struct objc_method_list_t &method_list) { 3922 sys::swapByteOrder(method_list.obsolete); 3923 sys::swapByteOrder(method_list.method_count); 3924 } 3925 3926 inline void swapStruct(struct objc_method_t &method) { 3927 sys::swapByteOrder(method.method_name); 3928 sys::swapByteOrder(method.method_types); 3929 sys::swapByteOrder(method.method_imp); 3930 } 3931 3932 inline void swapStruct(struct objc_protocol_list_t &protocol_list) { 3933 sys::swapByteOrder(protocol_list.next); 3934 sys::swapByteOrder(protocol_list.count); 3935 } 3936 3937 inline void swapStruct(struct objc_protocol_t &protocol) { 3938 sys::swapByteOrder(protocol.isa); 3939 sys::swapByteOrder(protocol.protocol_name); 3940 sys::swapByteOrder(protocol.protocol_list); 3941 sys::swapByteOrder(protocol.instance_methods); 3942 sys::swapByteOrder(protocol.class_methods); 3943 } 3944 3945 inline void swapStruct(struct objc_method_description_list_t &mdl) { 3946 sys::swapByteOrder(mdl.count); 3947 } 3948 3949 inline void swapStruct(struct objc_method_description_t &md) { 3950 sys::swapByteOrder(md.name); 3951 sys::swapByteOrder(md.types); 3952 } 3953 3954 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue, 3955 struct DisassembleInfo *info); 3956 3957 // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer 3958 // to an Objective-C class and returns the class name. It is also passed the 3959 // address of the pointer, so when the pointer is zero as it can be in an .o 3960 // file, that is used to look for an external relocation entry with a symbol 3961 // name. 3962 static const char *get_objc2_64bit_class_name(uint64_t pointer_value, 3963 uint64_t ReferenceValue, 3964 struct DisassembleInfo *info) { 3965 const char *r; 3966 uint32_t offset, left; 3967 SectionRef S; 3968 3969 // The pointer_value can be 0 in an object file and have a relocation 3970 // entry for the class symbol at the ReferenceValue (the address of the 3971 // pointer). 3972 if (pointer_value == 0) { 3973 r = get_pointer_64(ReferenceValue, offset, left, S, info); 3974 if (r == nullptr || left < sizeof(uint64_t)) 3975 return nullptr; 3976 uint64_t n_value; 3977 const char *symbol_name = get_symbol_64(offset, S, info, n_value); 3978 if (symbol_name == nullptr) 3979 return nullptr; 3980 const char *class_name = strrchr(symbol_name, '$'); 3981 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0') 3982 return class_name + 2; 3983 else 3984 return nullptr; 3985 } 3986 3987 // The case were the pointer_value is non-zero and points to a class defined 3988 // in this Mach-O file. 3989 r = get_pointer_64(pointer_value, offset, left, S, info); 3990 if (r == nullptr || left < sizeof(struct class64_t)) 3991 return nullptr; 3992 struct class64_t c; 3993 memcpy(&c, r, sizeof(struct class64_t)); 3994 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 3995 swapStruct(c); 3996 if (c.data == 0) 3997 return nullptr; 3998 r = get_pointer_64(c.data, offset, left, S, info); 3999 if (r == nullptr || left < sizeof(struct class_ro64_t)) 4000 return nullptr; 4001 struct class_ro64_t cro; 4002 memcpy(&cro, r, sizeof(struct class_ro64_t)); 4003 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4004 swapStruct(cro); 4005 if (cro.name == 0) 4006 return nullptr; 4007 const char *name = get_pointer_64(cro.name, offset, left, S, info); 4008 return name; 4009 } 4010 4011 // get_objc2_64bit_cfstring_name is used for disassembly and is passed a 4012 // pointer to a cfstring and returns its name or nullptr. 4013 static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue, 4014 struct DisassembleInfo *info) { 4015 const char *r, *name; 4016 uint32_t offset, left; 4017 SectionRef S; 4018 struct cfstring64_t cfs; 4019 uint64_t cfs_characters; 4020 4021 r = get_pointer_64(ReferenceValue, offset, left, S, info); 4022 if (r == nullptr || left < sizeof(struct cfstring64_t)) 4023 return nullptr; 4024 memcpy(&cfs, r, sizeof(struct cfstring64_t)); 4025 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4026 swapStruct(cfs); 4027 if (cfs.characters == 0) { 4028 uint64_t n_value; 4029 const char *symbol_name = get_symbol_64( 4030 offset + offsetof(struct cfstring64_t, characters), S, info, n_value); 4031 if (symbol_name == nullptr) 4032 return nullptr; 4033 cfs_characters = n_value; 4034 } else 4035 cfs_characters = cfs.characters; 4036 name = get_pointer_64(cfs_characters, offset, left, S, info); 4037 4038 return name; 4039 } 4040 4041 // get_objc2_64bit_selref() is used for disassembly and is passed a the address 4042 // of a pointer to an Objective-C selector reference when the pointer value is 4043 // zero as in a .o file and is likely to have a external relocation entry with 4044 // who's symbol's n_value is the real pointer to the selector name. If that is 4045 // the case the real pointer to the selector name is returned else 0 is 4046 // returned 4047 static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue, 4048 struct DisassembleInfo *info) { 4049 uint32_t offset, left; 4050 SectionRef S; 4051 4052 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info); 4053 if (r == nullptr || left < sizeof(uint64_t)) 4054 return 0; 4055 uint64_t n_value; 4056 const char *symbol_name = get_symbol_64(offset, S, info, n_value); 4057 if (symbol_name == nullptr) 4058 return 0; 4059 return n_value; 4060 } 4061 4062 static const SectionRef get_section(MachOObjectFile *O, const char *segname, 4063 const char *sectname) { 4064 for (const SectionRef &Section : O->sections()) { 4065 StringRef SectName; 4066 Expected<StringRef> SecNameOrErr = Section.getName(); 4067 if (SecNameOrErr) 4068 SectName = *SecNameOrErr; 4069 else 4070 consumeError(SecNameOrErr.takeError()); 4071 4072 DataRefImpl Ref = Section.getRawDataRefImpl(); 4073 StringRef SegName = O->getSectionFinalSegmentName(Ref); 4074 if (SegName == segname && SectName == sectname) 4075 return Section; 4076 } 4077 return SectionRef(); 4078 } 4079 4080 static void 4081 walk_pointer_list_64(const char *listname, const SectionRef S, 4082 MachOObjectFile *O, struct DisassembleInfo *info, 4083 void (*func)(uint64_t, struct DisassembleInfo *info)) { 4084 if (S == SectionRef()) 4085 return; 4086 4087 StringRef SectName; 4088 Expected<StringRef> SecNameOrErr = S.getName(); 4089 if (SecNameOrErr) 4090 SectName = *SecNameOrErr; 4091 else 4092 consumeError(SecNameOrErr.takeError()); 4093 4094 DataRefImpl Ref = S.getRawDataRefImpl(); 4095 StringRef SegName = O->getSectionFinalSegmentName(Ref); 4096 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 4097 4098 StringRef BytesStr = unwrapOrError(S.getContents(), O->getFileName()); 4099 const char *Contents = reinterpret_cast<const char *>(BytesStr.data()); 4100 4101 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) { 4102 uint32_t left = S.getSize() - i; 4103 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t); 4104 uint64_t p = 0; 4105 memcpy(&p, Contents + i, size); 4106 if (i + sizeof(uint64_t) > S.getSize()) 4107 outs() << listname << " list pointer extends past end of (" << SegName 4108 << "," << SectName << ") section\n"; 4109 outs() << format("%016" PRIx64, S.getAddress() + i) << " "; 4110 4111 if (O->isLittleEndian() != sys::IsLittleEndianHost) 4112 sys::swapByteOrder(p); 4113 4114 uint64_t n_value = 0; 4115 const char *name = get_symbol_64(i, S, info, n_value, p); 4116 if (name == nullptr) 4117 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info); 4118 4119 if (n_value != 0) { 4120 outs() << format("0x%" PRIx64, n_value); 4121 if (p != 0) 4122 outs() << " + " << format("0x%" PRIx64, p); 4123 } else 4124 outs() << format("0x%" PRIx64, p); 4125 if (name != nullptr) 4126 outs() << " " << name; 4127 outs() << "\n"; 4128 4129 p += n_value; 4130 if (func) 4131 func(p, info); 4132 } 4133 } 4134 4135 static void 4136 walk_pointer_list_32(const char *listname, const SectionRef S, 4137 MachOObjectFile *O, struct DisassembleInfo *info, 4138 void (*func)(uint32_t, struct DisassembleInfo *info)) { 4139 if (S == SectionRef()) 4140 return; 4141 4142 StringRef SectName = unwrapOrError(S.getName(), O->getFileName()); 4143 DataRefImpl Ref = S.getRawDataRefImpl(); 4144 StringRef SegName = O->getSectionFinalSegmentName(Ref); 4145 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 4146 4147 StringRef BytesStr = unwrapOrError(S.getContents(), O->getFileName()); 4148 const char *Contents = reinterpret_cast<const char *>(BytesStr.data()); 4149 4150 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) { 4151 uint32_t left = S.getSize() - i; 4152 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t); 4153 uint32_t p = 0; 4154 memcpy(&p, Contents + i, size); 4155 if (i + sizeof(uint32_t) > S.getSize()) 4156 outs() << listname << " list pointer extends past end of (" << SegName 4157 << "," << SectName << ") section\n"; 4158 uint32_t Address = S.getAddress() + i; 4159 outs() << format("%08" PRIx32, Address) << " "; 4160 4161 if (O->isLittleEndian() != sys::IsLittleEndianHost) 4162 sys::swapByteOrder(p); 4163 outs() << format("0x%" PRIx32, p); 4164 4165 const char *name = get_symbol_32(i, S, info, p); 4166 if (name != nullptr) 4167 outs() << " " << name; 4168 outs() << "\n"; 4169 4170 if (func) 4171 func(p, info); 4172 } 4173 } 4174 4175 static void print_layout_map(const char *layout_map, uint32_t left) { 4176 if (layout_map == nullptr) 4177 return; 4178 outs() << " layout map: "; 4179 do { 4180 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " "; 4181 left--; 4182 layout_map++; 4183 } while (*layout_map != '\0' && left != 0); 4184 outs() << "\n"; 4185 } 4186 4187 static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) { 4188 uint32_t offset, left; 4189 SectionRef S; 4190 const char *layout_map; 4191 4192 if (p == 0) 4193 return; 4194 layout_map = get_pointer_64(p, offset, left, S, info); 4195 print_layout_map(layout_map, left); 4196 } 4197 4198 static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) { 4199 uint32_t offset, left; 4200 SectionRef S; 4201 const char *layout_map; 4202 4203 if (p == 0) 4204 return; 4205 layout_map = get_pointer_32(p, offset, left, S, info); 4206 print_layout_map(layout_map, left); 4207 } 4208 4209 static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info, 4210 const char *indent) { 4211 struct method_list64_t ml; 4212 struct method64_t m; 4213 const char *r; 4214 uint32_t offset, xoffset, left, i; 4215 SectionRef S, xS; 4216 const char *name, *sym_name; 4217 uint64_t n_value; 4218 4219 r = get_pointer_64(p, offset, left, S, info); 4220 if (r == nullptr) 4221 return; 4222 memset(&ml, '\0', sizeof(struct method_list64_t)); 4223 if (left < sizeof(struct method_list64_t)) { 4224 memcpy(&ml, r, left); 4225 outs() << " (method_list_t entends past the end of the section)\n"; 4226 } else 4227 memcpy(&ml, r, sizeof(struct method_list64_t)); 4228 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4229 swapStruct(ml); 4230 outs() << indent << "\t\t entsize " << ml.entsize << "\n"; 4231 outs() << indent << "\t\t count " << ml.count << "\n"; 4232 4233 p += sizeof(struct method_list64_t); 4234 offset += sizeof(struct method_list64_t); 4235 for (i = 0; i < ml.count; i++) { 4236 r = get_pointer_64(p, offset, left, S, info); 4237 if (r == nullptr) 4238 return; 4239 memset(&m, '\0', sizeof(struct method64_t)); 4240 if (left < sizeof(struct method64_t)) { 4241 memcpy(&m, r, left); 4242 outs() << indent << " (method_t extends past the end of the section)\n"; 4243 } else 4244 memcpy(&m, r, sizeof(struct method64_t)); 4245 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4246 swapStruct(m); 4247 4248 outs() << indent << "\t\t name "; 4249 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S, 4250 info, n_value, m.name); 4251 if (n_value != 0) { 4252 if (info->verbose && sym_name != nullptr) 4253 outs() << sym_name; 4254 else 4255 outs() << format("0x%" PRIx64, n_value); 4256 if (m.name != 0) 4257 outs() << " + " << format("0x%" PRIx64, m.name); 4258 } else 4259 outs() << format("0x%" PRIx64, m.name); 4260 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info); 4261 if (name != nullptr) 4262 outs() << format(" %.*s", left, name); 4263 outs() << "\n"; 4264 4265 outs() << indent << "\t\t types "; 4266 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S, 4267 info, n_value, m.types); 4268 if (n_value != 0) { 4269 if (info->verbose && sym_name != nullptr) 4270 outs() << sym_name; 4271 else 4272 outs() << format("0x%" PRIx64, n_value); 4273 if (m.types != 0) 4274 outs() << " + " << format("0x%" PRIx64, m.types); 4275 } else 4276 outs() << format("0x%" PRIx64, m.types); 4277 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info); 4278 if (name != nullptr) 4279 outs() << format(" %.*s", left, name); 4280 outs() << "\n"; 4281 4282 outs() << indent << "\t\t imp "; 4283 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info, 4284 n_value, m.imp); 4285 if (info->verbose && name == nullptr) { 4286 if (n_value != 0) { 4287 outs() << format("0x%" PRIx64, n_value) << " "; 4288 if (m.imp != 0) 4289 outs() << "+ " << format("0x%" PRIx64, m.imp) << " "; 4290 } else 4291 outs() << format("0x%" PRIx64, m.imp) << " "; 4292 } 4293 if (name != nullptr) 4294 outs() << name; 4295 outs() << "\n"; 4296 4297 p += sizeof(struct method64_t); 4298 offset += sizeof(struct method64_t); 4299 } 4300 } 4301 4302 static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info, 4303 const char *indent) { 4304 struct method_list32_t ml; 4305 struct method32_t m; 4306 const char *r, *name; 4307 uint32_t offset, xoffset, left, i; 4308 SectionRef S, xS; 4309 4310 r = get_pointer_32(p, offset, left, S, info); 4311 if (r == nullptr) 4312 return; 4313 memset(&ml, '\0', sizeof(struct method_list32_t)); 4314 if (left < sizeof(struct method_list32_t)) { 4315 memcpy(&ml, r, left); 4316 outs() << " (method_list_t entends past the end of the section)\n"; 4317 } else 4318 memcpy(&ml, r, sizeof(struct method_list32_t)); 4319 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4320 swapStruct(ml); 4321 outs() << indent << "\t\t entsize " << ml.entsize << "\n"; 4322 outs() << indent << "\t\t count " << ml.count << "\n"; 4323 4324 p += sizeof(struct method_list32_t); 4325 offset += sizeof(struct method_list32_t); 4326 for (i = 0; i < ml.count; i++) { 4327 r = get_pointer_32(p, offset, left, S, info); 4328 if (r == nullptr) 4329 return; 4330 memset(&m, '\0', sizeof(struct method32_t)); 4331 if (left < sizeof(struct method32_t)) { 4332 memcpy(&ml, r, left); 4333 outs() << indent << " (method_t entends past the end of the section)\n"; 4334 } else 4335 memcpy(&m, r, sizeof(struct method32_t)); 4336 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4337 swapStruct(m); 4338 4339 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name); 4340 name = get_pointer_32(m.name, xoffset, left, xS, info); 4341 if (name != nullptr) 4342 outs() << format(" %.*s", left, name); 4343 outs() << "\n"; 4344 4345 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types); 4346 name = get_pointer_32(m.types, xoffset, left, xS, info); 4347 if (name != nullptr) 4348 outs() << format(" %.*s", left, name); 4349 outs() << "\n"; 4350 4351 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp); 4352 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info, 4353 m.imp); 4354 if (name != nullptr) 4355 outs() << " " << name; 4356 outs() << "\n"; 4357 4358 p += sizeof(struct method32_t); 4359 offset += sizeof(struct method32_t); 4360 } 4361 } 4362 4363 static bool print_method_list(uint32_t p, struct DisassembleInfo *info) { 4364 uint32_t offset, left, xleft; 4365 SectionRef S; 4366 struct objc_method_list_t method_list; 4367 struct objc_method_t method; 4368 const char *r, *methods, *name, *SymbolName; 4369 int32_t i; 4370 4371 r = get_pointer_32(p, offset, left, S, info, true); 4372 if (r == nullptr) 4373 return true; 4374 4375 outs() << "\n"; 4376 if (left > sizeof(struct objc_method_list_t)) { 4377 memcpy(&method_list, r, sizeof(struct objc_method_list_t)); 4378 } else { 4379 outs() << "\t\t objc_method_list extends past end of the section\n"; 4380 memset(&method_list, '\0', sizeof(struct objc_method_list_t)); 4381 memcpy(&method_list, r, left); 4382 } 4383 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4384 swapStruct(method_list); 4385 4386 outs() << "\t\t obsolete " 4387 << format("0x%08" PRIx32, method_list.obsolete) << "\n"; 4388 outs() << "\t\t method_count " << method_list.method_count << "\n"; 4389 4390 methods = r + sizeof(struct objc_method_list_t); 4391 for (i = 0; i < method_list.method_count; i++) { 4392 if ((i + 1) * sizeof(struct objc_method_t) > left) { 4393 outs() << "\t\t remaining method's extend past the of the section\n"; 4394 break; 4395 } 4396 memcpy(&method, methods + i * sizeof(struct objc_method_t), 4397 sizeof(struct objc_method_t)); 4398 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4399 swapStruct(method); 4400 4401 outs() << "\t\t method_name " 4402 << format("0x%08" PRIx32, method.method_name); 4403 if (info->verbose) { 4404 name = get_pointer_32(method.method_name, offset, xleft, S, info, true); 4405 if (name != nullptr) 4406 outs() << format(" %.*s", xleft, name); 4407 else 4408 outs() << " (not in an __OBJC section)"; 4409 } 4410 outs() << "\n"; 4411 4412 outs() << "\t\t method_types " 4413 << format("0x%08" PRIx32, method.method_types); 4414 if (info->verbose) { 4415 name = get_pointer_32(method.method_types, offset, xleft, S, info, true); 4416 if (name != nullptr) 4417 outs() << format(" %.*s", xleft, name); 4418 else 4419 outs() << " (not in an __OBJC section)"; 4420 } 4421 outs() << "\n"; 4422 4423 outs() << "\t\t method_imp " 4424 << format("0x%08" PRIx32, method.method_imp) << " "; 4425 if (info->verbose) { 4426 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap); 4427 if (SymbolName != nullptr) 4428 outs() << SymbolName; 4429 } 4430 outs() << "\n"; 4431 } 4432 return false; 4433 } 4434 4435 static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) { 4436 struct protocol_list64_t pl; 4437 uint64_t q, n_value; 4438 struct protocol64_t pc; 4439 const char *r; 4440 uint32_t offset, xoffset, left, i; 4441 SectionRef S, xS; 4442 const char *name, *sym_name; 4443 4444 r = get_pointer_64(p, offset, left, S, info); 4445 if (r == nullptr) 4446 return; 4447 memset(&pl, '\0', sizeof(struct protocol_list64_t)); 4448 if (left < sizeof(struct protocol_list64_t)) { 4449 memcpy(&pl, r, left); 4450 outs() << " (protocol_list_t entends past the end of the section)\n"; 4451 } else 4452 memcpy(&pl, r, sizeof(struct protocol_list64_t)); 4453 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4454 swapStruct(pl); 4455 outs() << " count " << pl.count << "\n"; 4456 4457 p += sizeof(struct protocol_list64_t); 4458 offset += sizeof(struct protocol_list64_t); 4459 for (i = 0; i < pl.count; i++) { 4460 r = get_pointer_64(p, offset, left, S, info); 4461 if (r == nullptr) 4462 return; 4463 q = 0; 4464 if (left < sizeof(uint64_t)) { 4465 memcpy(&q, r, left); 4466 outs() << " (protocol_t * entends past the end of the section)\n"; 4467 } else 4468 memcpy(&q, r, sizeof(uint64_t)); 4469 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4470 sys::swapByteOrder(q); 4471 4472 outs() << "\t\t list[" << i << "] "; 4473 sym_name = get_symbol_64(offset, S, info, n_value, q); 4474 if (n_value != 0) { 4475 if (info->verbose && sym_name != nullptr) 4476 outs() << sym_name; 4477 else 4478 outs() << format("0x%" PRIx64, n_value); 4479 if (q != 0) 4480 outs() << " + " << format("0x%" PRIx64, q); 4481 } else 4482 outs() << format("0x%" PRIx64, q); 4483 outs() << " (struct protocol_t *)\n"; 4484 4485 r = get_pointer_64(q + n_value, offset, left, S, info); 4486 if (r == nullptr) 4487 return; 4488 memset(&pc, '\0', sizeof(struct protocol64_t)); 4489 if (left < sizeof(struct protocol64_t)) { 4490 memcpy(&pc, r, left); 4491 outs() << " (protocol_t entends past the end of the section)\n"; 4492 } else 4493 memcpy(&pc, r, sizeof(struct protocol64_t)); 4494 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4495 swapStruct(pc); 4496 4497 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n"; 4498 4499 outs() << "\t\t\t name "; 4500 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S, 4501 info, n_value, pc.name); 4502 if (n_value != 0) { 4503 if (info->verbose && sym_name != nullptr) 4504 outs() << sym_name; 4505 else 4506 outs() << format("0x%" PRIx64, n_value); 4507 if (pc.name != 0) 4508 outs() << " + " << format("0x%" PRIx64, pc.name); 4509 } else 4510 outs() << format("0x%" PRIx64, pc.name); 4511 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info); 4512 if (name != nullptr) 4513 outs() << format(" %.*s", left, name); 4514 outs() << "\n"; 4515 4516 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n"; 4517 4518 outs() << "\t\t instanceMethods "; 4519 sym_name = 4520 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods), 4521 S, info, n_value, pc.instanceMethods); 4522 if (n_value != 0) { 4523 if (info->verbose && sym_name != nullptr) 4524 outs() << sym_name; 4525 else 4526 outs() << format("0x%" PRIx64, n_value); 4527 if (pc.instanceMethods != 0) 4528 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods); 4529 } else 4530 outs() << format("0x%" PRIx64, pc.instanceMethods); 4531 outs() << " (struct method_list_t *)\n"; 4532 if (pc.instanceMethods + n_value != 0) 4533 print_method_list64_t(pc.instanceMethods + n_value, info, "\t"); 4534 4535 outs() << "\t\t classMethods "; 4536 sym_name = 4537 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S, 4538 info, n_value, pc.classMethods); 4539 if (n_value != 0) { 4540 if (info->verbose && sym_name != nullptr) 4541 outs() << sym_name; 4542 else 4543 outs() << format("0x%" PRIx64, n_value); 4544 if (pc.classMethods != 0) 4545 outs() << " + " << format("0x%" PRIx64, pc.classMethods); 4546 } else 4547 outs() << format("0x%" PRIx64, pc.classMethods); 4548 outs() << " (struct method_list_t *)\n"; 4549 if (pc.classMethods + n_value != 0) 4550 print_method_list64_t(pc.classMethods + n_value, info, "\t"); 4551 4552 outs() << "\t optionalInstanceMethods " 4553 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n"; 4554 outs() << "\t optionalClassMethods " 4555 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n"; 4556 outs() << "\t instanceProperties " 4557 << format("0x%" PRIx64, pc.instanceProperties) << "\n"; 4558 4559 p += sizeof(uint64_t); 4560 offset += sizeof(uint64_t); 4561 } 4562 } 4563 4564 static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) { 4565 struct protocol_list32_t pl; 4566 uint32_t q; 4567 struct protocol32_t pc; 4568 const char *r; 4569 uint32_t offset, xoffset, left, i; 4570 SectionRef S, xS; 4571 const char *name; 4572 4573 r = get_pointer_32(p, offset, left, S, info); 4574 if (r == nullptr) 4575 return; 4576 memset(&pl, '\0', sizeof(struct protocol_list32_t)); 4577 if (left < sizeof(struct protocol_list32_t)) { 4578 memcpy(&pl, r, left); 4579 outs() << " (protocol_list_t entends past the end of the section)\n"; 4580 } else 4581 memcpy(&pl, r, sizeof(struct protocol_list32_t)); 4582 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4583 swapStruct(pl); 4584 outs() << " count " << pl.count << "\n"; 4585 4586 p += sizeof(struct protocol_list32_t); 4587 offset += sizeof(struct protocol_list32_t); 4588 for (i = 0; i < pl.count; i++) { 4589 r = get_pointer_32(p, offset, left, S, info); 4590 if (r == nullptr) 4591 return; 4592 q = 0; 4593 if (left < sizeof(uint32_t)) { 4594 memcpy(&q, r, left); 4595 outs() << " (protocol_t * entends past the end of the section)\n"; 4596 } else 4597 memcpy(&q, r, sizeof(uint32_t)); 4598 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4599 sys::swapByteOrder(q); 4600 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q) 4601 << " (struct protocol_t *)\n"; 4602 r = get_pointer_32(q, offset, left, S, info); 4603 if (r == nullptr) 4604 return; 4605 memset(&pc, '\0', sizeof(struct protocol32_t)); 4606 if (left < sizeof(struct protocol32_t)) { 4607 memcpy(&pc, r, left); 4608 outs() << " (protocol_t entends past the end of the section)\n"; 4609 } else 4610 memcpy(&pc, r, sizeof(struct protocol32_t)); 4611 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4612 swapStruct(pc); 4613 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n"; 4614 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name); 4615 name = get_pointer_32(pc.name, xoffset, left, xS, info); 4616 if (name != nullptr) 4617 outs() << format(" %.*s", left, name); 4618 outs() << "\n"; 4619 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n"; 4620 outs() << "\t\t instanceMethods " 4621 << format("0x%" PRIx32, pc.instanceMethods) 4622 << " (struct method_list_t *)\n"; 4623 if (pc.instanceMethods != 0) 4624 print_method_list32_t(pc.instanceMethods, info, "\t"); 4625 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods) 4626 << " (struct method_list_t *)\n"; 4627 if (pc.classMethods != 0) 4628 print_method_list32_t(pc.classMethods, info, "\t"); 4629 outs() << "\t optionalInstanceMethods " 4630 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n"; 4631 outs() << "\t optionalClassMethods " 4632 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n"; 4633 outs() << "\t instanceProperties " 4634 << format("0x%" PRIx32, pc.instanceProperties) << "\n"; 4635 p += sizeof(uint32_t); 4636 offset += sizeof(uint32_t); 4637 } 4638 } 4639 4640 static void print_indent(uint32_t indent) { 4641 for (uint32_t i = 0; i < indent;) { 4642 if (indent - i >= 8) { 4643 outs() << "\t"; 4644 i += 8; 4645 } else { 4646 for (uint32_t j = i; j < indent; j++) 4647 outs() << " "; 4648 return; 4649 } 4650 } 4651 } 4652 4653 static bool print_method_description_list(uint32_t p, uint32_t indent, 4654 struct DisassembleInfo *info) { 4655 uint32_t offset, left, xleft; 4656 SectionRef S; 4657 struct objc_method_description_list_t mdl; 4658 struct objc_method_description_t md; 4659 const char *r, *list, *name; 4660 int32_t i; 4661 4662 r = get_pointer_32(p, offset, left, S, info, true); 4663 if (r == nullptr) 4664 return true; 4665 4666 outs() << "\n"; 4667 if (left > sizeof(struct objc_method_description_list_t)) { 4668 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t)); 4669 } else { 4670 print_indent(indent); 4671 outs() << " objc_method_description_list extends past end of the section\n"; 4672 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t)); 4673 memcpy(&mdl, r, left); 4674 } 4675 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4676 swapStruct(mdl); 4677 4678 print_indent(indent); 4679 outs() << " count " << mdl.count << "\n"; 4680 4681 list = r + sizeof(struct objc_method_description_list_t); 4682 for (i = 0; i < mdl.count; i++) { 4683 if ((i + 1) * sizeof(struct objc_method_description_t) > left) { 4684 print_indent(indent); 4685 outs() << " remaining list entries extend past the of the section\n"; 4686 break; 4687 } 4688 print_indent(indent); 4689 outs() << " list[" << i << "]\n"; 4690 memcpy(&md, list + i * sizeof(struct objc_method_description_t), 4691 sizeof(struct objc_method_description_t)); 4692 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4693 swapStruct(md); 4694 4695 print_indent(indent); 4696 outs() << " name " << format("0x%08" PRIx32, md.name); 4697 if (info->verbose) { 4698 name = get_pointer_32(md.name, offset, xleft, S, info, true); 4699 if (name != nullptr) 4700 outs() << format(" %.*s", xleft, name); 4701 else 4702 outs() << " (not in an __OBJC section)"; 4703 } 4704 outs() << "\n"; 4705 4706 print_indent(indent); 4707 outs() << " types " << format("0x%08" PRIx32, md.types); 4708 if (info->verbose) { 4709 name = get_pointer_32(md.types, offset, xleft, S, info, true); 4710 if (name != nullptr) 4711 outs() << format(" %.*s", xleft, name); 4712 else 4713 outs() << " (not in an __OBJC section)"; 4714 } 4715 outs() << "\n"; 4716 } 4717 return false; 4718 } 4719 4720 static bool print_protocol_list(uint32_t p, uint32_t indent, 4721 struct DisassembleInfo *info); 4722 4723 static bool print_protocol(uint32_t p, uint32_t indent, 4724 struct DisassembleInfo *info) { 4725 uint32_t offset, left; 4726 SectionRef S; 4727 struct objc_protocol_t protocol; 4728 const char *r, *name; 4729 4730 r = get_pointer_32(p, offset, left, S, info, true); 4731 if (r == nullptr) 4732 return true; 4733 4734 outs() << "\n"; 4735 if (left >= sizeof(struct objc_protocol_t)) { 4736 memcpy(&protocol, r, sizeof(struct objc_protocol_t)); 4737 } else { 4738 print_indent(indent); 4739 outs() << " Protocol extends past end of the section\n"; 4740 memset(&protocol, '\0', sizeof(struct objc_protocol_t)); 4741 memcpy(&protocol, r, left); 4742 } 4743 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4744 swapStruct(protocol); 4745 4746 print_indent(indent); 4747 outs() << " isa " << format("0x%08" PRIx32, protocol.isa) 4748 << "\n"; 4749 4750 print_indent(indent); 4751 outs() << " protocol_name " 4752 << format("0x%08" PRIx32, protocol.protocol_name); 4753 if (info->verbose) { 4754 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true); 4755 if (name != nullptr) 4756 outs() << format(" %.*s", left, name); 4757 else 4758 outs() << " (not in an __OBJC section)"; 4759 } 4760 outs() << "\n"; 4761 4762 print_indent(indent); 4763 outs() << " protocol_list " 4764 << format("0x%08" PRIx32, protocol.protocol_list); 4765 if (print_protocol_list(protocol.protocol_list, indent + 4, info)) 4766 outs() << " (not in an __OBJC section)\n"; 4767 4768 print_indent(indent); 4769 outs() << " instance_methods " 4770 << format("0x%08" PRIx32, protocol.instance_methods); 4771 if (print_method_description_list(protocol.instance_methods, indent, info)) 4772 outs() << " (not in an __OBJC section)\n"; 4773 4774 print_indent(indent); 4775 outs() << " class_methods " 4776 << format("0x%08" PRIx32, protocol.class_methods); 4777 if (print_method_description_list(protocol.class_methods, indent, info)) 4778 outs() << " (not in an __OBJC section)\n"; 4779 4780 return false; 4781 } 4782 4783 static bool print_protocol_list(uint32_t p, uint32_t indent, 4784 struct DisassembleInfo *info) { 4785 uint32_t offset, left, l; 4786 SectionRef S; 4787 struct objc_protocol_list_t protocol_list; 4788 const char *r, *list; 4789 int32_t i; 4790 4791 r = get_pointer_32(p, offset, left, S, info, true); 4792 if (r == nullptr) 4793 return true; 4794 4795 outs() << "\n"; 4796 if (left > sizeof(struct objc_protocol_list_t)) { 4797 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t)); 4798 } else { 4799 outs() << "\t\t objc_protocol_list_t extends past end of the section\n"; 4800 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t)); 4801 memcpy(&protocol_list, r, left); 4802 } 4803 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4804 swapStruct(protocol_list); 4805 4806 print_indent(indent); 4807 outs() << " next " << format("0x%08" PRIx32, protocol_list.next) 4808 << "\n"; 4809 print_indent(indent); 4810 outs() << " count " << protocol_list.count << "\n"; 4811 4812 list = r + sizeof(struct objc_protocol_list_t); 4813 for (i = 0; i < protocol_list.count; i++) { 4814 if ((i + 1) * sizeof(uint32_t) > left) { 4815 outs() << "\t\t remaining list entries extend past the of the section\n"; 4816 break; 4817 } 4818 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t)); 4819 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4820 sys::swapByteOrder(l); 4821 4822 print_indent(indent); 4823 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l); 4824 if (print_protocol(l, indent, info)) 4825 outs() << "(not in an __OBJC section)\n"; 4826 } 4827 return false; 4828 } 4829 4830 static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) { 4831 struct ivar_list64_t il; 4832 struct ivar64_t i; 4833 const char *r; 4834 uint32_t offset, xoffset, left, j; 4835 SectionRef S, xS; 4836 const char *name, *sym_name, *ivar_offset_p; 4837 uint64_t ivar_offset, n_value; 4838 4839 r = get_pointer_64(p, offset, left, S, info); 4840 if (r == nullptr) 4841 return; 4842 memset(&il, '\0', sizeof(struct ivar_list64_t)); 4843 if (left < sizeof(struct ivar_list64_t)) { 4844 memcpy(&il, r, left); 4845 outs() << " (ivar_list_t entends past the end of the section)\n"; 4846 } else 4847 memcpy(&il, r, sizeof(struct ivar_list64_t)); 4848 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4849 swapStruct(il); 4850 outs() << " entsize " << il.entsize << "\n"; 4851 outs() << " count " << il.count << "\n"; 4852 4853 p += sizeof(struct ivar_list64_t); 4854 offset += sizeof(struct ivar_list64_t); 4855 for (j = 0; j < il.count; j++) { 4856 r = get_pointer_64(p, offset, left, S, info); 4857 if (r == nullptr) 4858 return; 4859 memset(&i, '\0', sizeof(struct ivar64_t)); 4860 if (left < sizeof(struct ivar64_t)) { 4861 memcpy(&i, r, left); 4862 outs() << " (ivar_t entends past the end of the section)\n"; 4863 } else 4864 memcpy(&i, r, sizeof(struct ivar64_t)); 4865 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4866 swapStruct(i); 4867 4868 outs() << "\t\t\t offset "; 4869 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S, 4870 info, n_value, i.offset); 4871 if (n_value != 0) { 4872 if (info->verbose && sym_name != nullptr) 4873 outs() << sym_name; 4874 else 4875 outs() << format("0x%" PRIx64, n_value); 4876 if (i.offset != 0) 4877 outs() << " + " << format("0x%" PRIx64, i.offset); 4878 } else 4879 outs() << format("0x%" PRIx64, i.offset); 4880 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info); 4881 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) { 4882 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset)); 4883 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4884 sys::swapByteOrder(ivar_offset); 4885 outs() << " " << ivar_offset << "\n"; 4886 } else 4887 outs() << "\n"; 4888 4889 outs() << "\t\t\t name "; 4890 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info, 4891 n_value, i.name); 4892 if (n_value != 0) { 4893 if (info->verbose && sym_name != nullptr) 4894 outs() << sym_name; 4895 else 4896 outs() << format("0x%" PRIx64, n_value); 4897 if (i.name != 0) 4898 outs() << " + " << format("0x%" PRIx64, i.name); 4899 } else 4900 outs() << format("0x%" PRIx64, i.name); 4901 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info); 4902 if (name != nullptr) 4903 outs() << format(" %.*s", left, name); 4904 outs() << "\n"; 4905 4906 outs() << "\t\t\t type "; 4907 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info, 4908 n_value, i.name); 4909 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info); 4910 if (n_value != 0) { 4911 if (info->verbose && sym_name != nullptr) 4912 outs() << sym_name; 4913 else 4914 outs() << format("0x%" PRIx64, n_value); 4915 if (i.type != 0) 4916 outs() << " + " << format("0x%" PRIx64, i.type); 4917 } else 4918 outs() << format("0x%" PRIx64, i.type); 4919 if (name != nullptr) 4920 outs() << format(" %.*s", left, name); 4921 outs() << "\n"; 4922 4923 outs() << "\t\t\talignment " << i.alignment << "\n"; 4924 outs() << "\t\t\t size " << i.size << "\n"; 4925 4926 p += sizeof(struct ivar64_t); 4927 offset += sizeof(struct ivar64_t); 4928 } 4929 } 4930 4931 static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) { 4932 struct ivar_list32_t il; 4933 struct ivar32_t i; 4934 const char *r; 4935 uint32_t offset, xoffset, left, j; 4936 SectionRef S, xS; 4937 const char *name, *ivar_offset_p; 4938 uint32_t ivar_offset; 4939 4940 r = get_pointer_32(p, offset, left, S, info); 4941 if (r == nullptr) 4942 return; 4943 memset(&il, '\0', sizeof(struct ivar_list32_t)); 4944 if (left < sizeof(struct ivar_list32_t)) { 4945 memcpy(&il, r, left); 4946 outs() << " (ivar_list_t entends past the end of the section)\n"; 4947 } else 4948 memcpy(&il, r, sizeof(struct ivar_list32_t)); 4949 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4950 swapStruct(il); 4951 outs() << " entsize " << il.entsize << "\n"; 4952 outs() << " count " << il.count << "\n"; 4953 4954 p += sizeof(struct ivar_list32_t); 4955 offset += sizeof(struct ivar_list32_t); 4956 for (j = 0; j < il.count; j++) { 4957 r = get_pointer_32(p, offset, left, S, info); 4958 if (r == nullptr) 4959 return; 4960 memset(&i, '\0', sizeof(struct ivar32_t)); 4961 if (left < sizeof(struct ivar32_t)) { 4962 memcpy(&i, r, left); 4963 outs() << " (ivar_t entends past the end of the section)\n"; 4964 } else 4965 memcpy(&i, r, sizeof(struct ivar32_t)); 4966 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4967 swapStruct(i); 4968 4969 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset); 4970 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info); 4971 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) { 4972 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset)); 4973 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4974 sys::swapByteOrder(ivar_offset); 4975 outs() << " " << ivar_offset << "\n"; 4976 } else 4977 outs() << "\n"; 4978 4979 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name); 4980 name = get_pointer_32(i.name, xoffset, left, xS, info); 4981 if (name != nullptr) 4982 outs() << format(" %.*s", left, name); 4983 outs() << "\n"; 4984 4985 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type); 4986 name = get_pointer_32(i.type, xoffset, left, xS, info); 4987 if (name != nullptr) 4988 outs() << format(" %.*s", left, name); 4989 outs() << "\n"; 4990 4991 outs() << "\t\t\talignment " << i.alignment << "\n"; 4992 outs() << "\t\t\t size " << i.size << "\n"; 4993 4994 p += sizeof(struct ivar32_t); 4995 offset += sizeof(struct ivar32_t); 4996 } 4997 } 4998 4999 static void print_objc_property_list64(uint64_t p, 5000 struct DisassembleInfo *info) { 5001 struct objc_property_list64 opl; 5002 struct objc_property64 op; 5003 const char *r; 5004 uint32_t offset, xoffset, left, j; 5005 SectionRef S, xS; 5006 const char *name, *sym_name; 5007 uint64_t n_value; 5008 5009 r = get_pointer_64(p, offset, left, S, info); 5010 if (r == nullptr) 5011 return; 5012 memset(&opl, '\0', sizeof(struct objc_property_list64)); 5013 if (left < sizeof(struct objc_property_list64)) { 5014 memcpy(&opl, r, left); 5015 outs() << " (objc_property_list entends past the end of the section)\n"; 5016 } else 5017 memcpy(&opl, r, sizeof(struct objc_property_list64)); 5018 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5019 swapStruct(opl); 5020 outs() << " entsize " << opl.entsize << "\n"; 5021 outs() << " count " << opl.count << "\n"; 5022 5023 p += sizeof(struct objc_property_list64); 5024 offset += sizeof(struct objc_property_list64); 5025 for (j = 0; j < opl.count; j++) { 5026 r = get_pointer_64(p, offset, left, S, info); 5027 if (r == nullptr) 5028 return; 5029 memset(&op, '\0', sizeof(struct objc_property64)); 5030 if (left < sizeof(struct objc_property64)) { 5031 memcpy(&op, r, left); 5032 outs() << " (objc_property entends past the end of the section)\n"; 5033 } else 5034 memcpy(&op, r, sizeof(struct objc_property64)); 5035 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5036 swapStruct(op); 5037 5038 outs() << "\t\t\t name "; 5039 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S, 5040 info, n_value, op.name); 5041 if (n_value != 0) { 5042 if (info->verbose && sym_name != nullptr) 5043 outs() << sym_name; 5044 else 5045 outs() << format("0x%" PRIx64, n_value); 5046 if (op.name != 0) 5047 outs() << " + " << format("0x%" PRIx64, op.name); 5048 } else 5049 outs() << format("0x%" PRIx64, op.name); 5050 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info); 5051 if (name != nullptr) 5052 outs() << format(" %.*s", left, name); 5053 outs() << "\n"; 5054 5055 outs() << "\t\t\tattributes "; 5056 sym_name = 5057 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S, 5058 info, n_value, op.attributes); 5059 if (n_value != 0) { 5060 if (info->verbose && sym_name != nullptr) 5061 outs() << sym_name; 5062 else 5063 outs() << format("0x%" PRIx64, n_value); 5064 if (op.attributes != 0) 5065 outs() << " + " << format("0x%" PRIx64, op.attributes); 5066 } else 5067 outs() << format("0x%" PRIx64, op.attributes); 5068 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info); 5069 if (name != nullptr) 5070 outs() << format(" %.*s", left, name); 5071 outs() << "\n"; 5072 5073 p += sizeof(struct objc_property64); 5074 offset += sizeof(struct objc_property64); 5075 } 5076 } 5077 5078 static void print_objc_property_list32(uint32_t p, 5079 struct DisassembleInfo *info) { 5080 struct objc_property_list32 opl; 5081 struct objc_property32 op; 5082 const char *r; 5083 uint32_t offset, xoffset, left, j; 5084 SectionRef S, xS; 5085 const char *name; 5086 5087 r = get_pointer_32(p, offset, left, S, info); 5088 if (r == nullptr) 5089 return; 5090 memset(&opl, '\0', sizeof(struct objc_property_list32)); 5091 if (left < sizeof(struct objc_property_list32)) { 5092 memcpy(&opl, r, left); 5093 outs() << " (objc_property_list entends past the end of the section)\n"; 5094 } else 5095 memcpy(&opl, r, sizeof(struct objc_property_list32)); 5096 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5097 swapStruct(opl); 5098 outs() << " entsize " << opl.entsize << "\n"; 5099 outs() << " count " << opl.count << "\n"; 5100 5101 p += sizeof(struct objc_property_list32); 5102 offset += sizeof(struct objc_property_list32); 5103 for (j = 0; j < opl.count; j++) { 5104 r = get_pointer_32(p, offset, left, S, info); 5105 if (r == nullptr) 5106 return; 5107 memset(&op, '\0', sizeof(struct objc_property32)); 5108 if (left < sizeof(struct objc_property32)) { 5109 memcpy(&op, r, left); 5110 outs() << " (objc_property entends past the end of the section)\n"; 5111 } else 5112 memcpy(&op, r, sizeof(struct objc_property32)); 5113 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5114 swapStruct(op); 5115 5116 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name); 5117 name = get_pointer_32(op.name, xoffset, left, xS, info); 5118 if (name != nullptr) 5119 outs() << format(" %.*s", left, name); 5120 outs() << "\n"; 5121 5122 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes); 5123 name = get_pointer_32(op.attributes, xoffset, left, xS, info); 5124 if (name != nullptr) 5125 outs() << format(" %.*s", left, name); 5126 outs() << "\n"; 5127 5128 p += sizeof(struct objc_property32); 5129 offset += sizeof(struct objc_property32); 5130 } 5131 } 5132 5133 static bool print_class_ro64_t(uint64_t p, struct DisassembleInfo *info, 5134 bool &is_meta_class) { 5135 struct class_ro64_t cro; 5136 const char *r; 5137 uint32_t offset, xoffset, left; 5138 SectionRef S, xS; 5139 const char *name, *sym_name; 5140 uint64_t n_value; 5141 5142 r = get_pointer_64(p, offset, left, S, info); 5143 if (r == nullptr || left < sizeof(struct class_ro64_t)) 5144 return false; 5145 memcpy(&cro, r, sizeof(struct class_ro64_t)); 5146 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5147 swapStruct(cro); 5148 outs() << " flags " << format("0x%" PRIx32, cro.flags); 5149 if (cro.flags & RO_META) 5150 outs() << " RO_META"; 5151 if (cro.flags & RO_ROOT) 5152 outs() << " RO_ROOT"; 5153 if (cro.flags & RO_HAS_CXX_STRUCTORS) 5154 outs() << " RO_HAS_CXX_STRUCTORS"; 5155 outs() << "\n"; 5156 outs() << " instanceStart " << cro.instanceStart << "\n"; 5157 outs() << " instanceSize " << cro.instanceSize << "\n"; 5158 outs() << " reserved " << format("0x%" PRIx32, cro.reserved) 5159 << "\n"; 5160 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout) 5161 << "\n"; 5162 print_layout_map64(cro.ivarLayout, info); 5163 5164 outs() << " name "; 5165 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S, 5166 info, n_value, cro.name); 5167 if (n_value != 0) { 5168 if (info->verbose && sym_name != nullptr) 5169 outs() << sym_name; 5170 else 5171 outs() << format("0x%" PRIx64, n_value); 5172 if (cro.name != 0) 5173 outs() << " + " << format("0x%" PRIx64, cro.name); 5174 } else 5175 outs() << format("0x%" PRIx64, cro.name); 5176 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info); 5177 if (name != nullptr) 5178 outs() << format(" %.*s", left, name); 5179 outs() << "\n"; 5180 5181 outs() << " baseMethods "; 5182 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods), 5183 S, info, n_value, cro.baseMethods); 5184 if (n_value != 0) { 5185 if (info->verbose && sym_name != nullptr) 5186 outs() << sym_name; 5187 else 5188 outs() << format("0x%" PRIx64, n_value); 5189 if (cro.baseMethods != 0) 5190 outs() << " + " << format("0x%" PRIx64, cro.baseMethods); 5191 } else 5192 outs() << format("0x%" PRIx64, cro.baseMethods); 5193 outs() << " (struct method_list_t *)\n"; 5194 if (cro.baseMethods + n_value != 0) 5195 print_method_list64_t(cro.baseMethods + n_value, info, ""); 5196 5197 outs() << " baseProtocols "; 5198 sym_name = 5199 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S, 5200 info, n_value, cro.baseProtocols); 5201 if (n_value != 0) { 5202 if (info->verbose && sym_name != nullptr) 5203 outs() << sym_name; 5204 else 5205 outs() << format("0x%" PRIx64, n_value); 5206 if (cro.baseProtocols != 0) 5207 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols); 5208 } else 5209 outs() << format("0x%" PRIx64, cro.baseProtocols); 5210 outs() << "\n"; 5211 if (cro.baseProtocols + n_value != 0) 5212 print_protocol_list64_t(cro.baseProtocols + n_value, info); 5213 5214 outs() << " ivars "; 5215 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S, 5216 info, n_value, cro.ivars); 5217 if (n_value != 0) { 5218 if (info->verbose && sym_name != nullptr) 5219 outs() << sym_name; 5220 else 5221 outs() << format("0x%" PRIx64, n_value); 5222 if (cro.ivars != 0) 5223 outs() << " + " << format("0x%" PRIx64, cro.ivars); 5224 } else 5225 outs() << format("0x%" PRIx64, cro.ivars); 5226 outs() << "\n"; 5227 if (cro.ivars + n_value != 0) 5228 print_ivar_list64_t(cro.ivars + n_value, info); 5229 5230 outs() << " weakIvarLayout "; 5231 sym_name = 5232 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S, 5233 info, n_value, cro.weakIvarLayout); 5234 if (n_value != 0) { 5235 if (info->verbose && sym_name != nullptr) 5236 outs() << sym_name; 5237 else 5238 outs() << format("0x%" PRIx64, n_value); 5239 if (cro.weakIvarLayout != 0) 5240 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout); 5241 } else 5242 outs() << format("0x%" PRIx64, cro.weakIvarLayout); 5243 outs() << "\n"; 5244 print_layout_map64(cro.weakIvarLayout + n_value, info); 5245 5246 outs() << " baseProperties "; 5247 sym_name = 5248 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S, 5249 info, n_value, cro.baseProperties); 5250 if (n_value != 0) { 5251 if (info->verbose && sym_name != nullptr) 5252 outs() << sym_name; 5253 else 5254 outs() << format("0x%" PRIx64, n_value); 5255 if (cro.baseProperties != 0) 5256 outs() << " + " << format("0x%" PRIx64, cro.baseProperties); 5257 } else 5258 outs() << format("0x%" PRIx64, cro.baseProperties); 5259 outs() << "\n"; 5260 if (cro.baseProperties + n_value != 0) 5261 print_objc_property_list64(cro.baseProperties + n_value, info); 5262 5263 is_meta_class = (cro.flags & RO_META) != 0; 5264 return true; 5265 } 5266 5267 static bool print_class_ro32_t(uint32_t p, struct DisassembleInfo *info, 5268 bool &is_meta_class) { 5269 struct class_ro32_t cro; 5270 const char *r; 5271 uint32_t offset, xoffset, left; 5272 SectionRef S, xS; 5273 const char *name; 5274 5275 r = get_pointer_32(p, offset, left, S, info); 5276 if (r == nullptr) 5277 return false; 5278 memset(&cro, '\0', sizeof(struct class_ro32_t)); 5279 if (left < sizeof(struct class_ro32_t)) { 5280 memcpy(&cro, r, left); 5281 outs() << " (class_ro_t entends past the end of the section)\n"; 5282 } else 5283 memcpy(&cro, r, sizeof(struct class_ro32_t)); 5284 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5285 swapStruct(cro); 5286 outs() << " flags " << format("0x%" PRIx32, cro.flags); 5287 if (cro.flags & RO_META) 5288 outs() << " RO_META"; 5289 if (cro.flags & RO_ROOT) 5290 outs() << " RO_ROOT"; 5291 if (cro.flags & RO_HAS_CXX_STRUCTORS) 5292 outs() << " RO_HAS_CXX_STRUCTORS"; 5293 outs() << "\n"; 5294 outs() << " instanceStart " << cro.instanceStart << "\n"; 5295 outs() << " instanceSize " << cro.instanceSize << "\n"; 5296 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout) 5297 << "\n"; 5298 print_layout_map32(cro.ivarLayout, info); 5299 5300 outs() << " name " << format("0x%" PRIx32, cro.name); 5301 name = get_pointer_32(cro.name, xoffset, left, xS, info); 5302 if (name != nullptr) 5303 outs() << format(" %.*s", left, name); 5304 outs() << "\n"; 5305 5306 outs() << " baseMethods " 5307 << format("0x%" PRIx32, cro.baseMethods) 5308 << " (struct method_list_t *)\n"; 5309 if (cro.baseMethods != 0) 5310 print_method_list32_t(cro.baseMethods, info, ""); 5311 5312 outs() << " baseProtocols " 5313 << format("0x%" PRIx32, cro.baseProtocols) << "\n"; 5314 if (cro.baseProtocols != 0) 5315 print_protocol_list32_t(cro.baseProtocols, info); 5316 outs() << " ivars " << format("0x%" PRIx32, cro.ivars) 5317 << "\n"; 5318 if (cro.ivars != 0) 5319 print_ivar_list32_t(cro.ivars, info); 5320 outs() << " weakIvarLayout " 5321 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n"; 5322 print_layout_map32(cro.weakIvarLayout, info); 5323 outs() << " baseProperties " 5324 << format("0x%" PRIx32, cro.baseProperties) << "\n"; 5325 if (cro.baseProperties != 0) 5326 print_objc_property_list32(cro.baseProperties, info); 5327 is_meta_class = (cro.flags & RO_META) != 0; 5328 return true; 5329 } 5330 5331 static void print_class64_t(uint64_t p, struct DisassembleInfo *info) { 5332 struct class64_t c; 5333 const char *r; 5334 uint32_t offset, left; 5335 SectionRef S; 5336 const char *name; 5337 uint64_t isa_n_value, n_value; 5338 5339 r = get_pointer_64(p, offset, left, S, info); 5340 if (r == nullptr || left < sizeof(struct class64_t)) 5341 return; 5342 memcpy(&c, r, sizeof(struct class64_t)); 5343 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5344 swapStruct(c); 5345 5346 outs() << " isa " << format("0x%" PRIx64, c.isa); 5347 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info, 5348 isa_n_value, c.isa); 5349 if (name != nullptr) 5350 outs() << " " << name; 5351 outs() << "\n"; 5352 5353 outs() << " superclass " << format("0x%" PRIx64, c.superclass); 5354 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info, 5355 n_value, c.superclass); 5356 if (name != nullptr) 5357 outs() << " " << name; 5358 else { 5359 name = get_dyld_bind_info_symbolname(S.getAddress() + 5360 offset + offsetof(struct class64_t, superclass), info); 5361 if (name != nullptr) 5362 outs() << " " << name; 5363 } 5364 outs() << "\n"; 5365 5366 outs() << " cache " << format("0x%" PRIx64, c.cache); 5367 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info, 5368 n_value, c.cache); 5369 if (name != nullptr) 5370 outs() << " " << name; 5371 outs() << "\n"; 5372 5373 outs() << " vtable " << format("0x%" PRIx64, c.vtable); 5374 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info, 5375 n_value, c.vtable); 5376 if (name != nullptr) 5377 outs() << " " << name; 5378 outs() << "\n"; 5379 5380 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info, 5381 n_value, c.data); 5382 outs() << " data "; 5383 if (n_value != 0) { 5384 if (info->verbose && name != nullptr) 5385 outs() << name; 5386 else 5387 outs() << format("0x%" PRIx64, n_value); 5388 if (c.data != 0) 5389 outs() << " + " << format("0x%" PRIx64, c.data); 5390 } else 5391 outs() << format("0x%" PRIx64, c.data); 5392 outs() << " (struct class_ro_t *)"; 5393 5394 // This is a Swift class if some of the low bits of the pointer are set. 5395 if ((c.data + n_value) & 0x7) 5396 outs() << " Swift class"; 5397 outs() << "\n"; 5398 bool is_meta_class; 5399 if (!print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class)) 5400 return; 5401 5402 if (!is_meta_class && 5403 c.isa + isa_n_value != p && 5404 c.isa + isa_n_value != 0 && 5405 info->depth < 100) { 5406 info->depth++; 5407 outs() << "Meta Class\n"; 5408 print_class64_t(c.isa + isa_n_value, info); 5409 } 5410 } 5411 5412 static void print_class32_t(uint32_t p, struct DisassembleInfo *info) { 5413 struct class32_t c; 5414 const char *r; 5415 uint32_t offset, left; 5416 SectionRef S; 5417 const char *name; 5418 5419 r = get_pointer_32(p, offset, left, S, info); 5420 if (r == nullptr) 5421 return; 5422 memset(&c, '\0', sizeof(struct class32_t)); 5423 if (left < sizeof(struct class32_t)) { 5424 memcpy(&c, r, left); 5425 outs() << " (class_t entends past the end of the section)\n"; 5426 } else 5427 memcpy(&c, r, sizeof(struct class32_t)); 5428 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5429 swapStruct(c); 5430 5431 outs() << " isa " << format("0x%" PRIx32, c.isa); 5432 name = 5433 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa); 5434 if (name != nullptr) 5435 outs() << " " << name; 5436 outs() << "\n"; 5437 5438 outs() << " superclass " << format("0x%" PRIx32, c.superclass); 5439 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info, 5440 c.superclass); 5441 if (name != nullptr) 5442 outs() << " " << name; 5443 outs() << "\n"; 5444 5445 outs() << " cache " << format("0x%" PRIx32, c.cache); 5446 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info, 5447 c.cache); 5448 if (name != nullptr) 5449 outs() << " " << name; 5450 outs() << "\n"; 5451 5452 outs() << " vtable " << format("0x%" PRIx32, c.vtable); 5453 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info, 5454 c.vtable); 5455 if (name != nullptr) 5456 outs() << " " << name; 5457 outs() << "\n"; 5458 5459 name = 5460 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data); 5461 outs() << " data " << format("0x%" PRIx32, c.data) 5462 << " (struct class_ro_t *)"; 5463 5464 // This is a Swift class if some of the low bits of the pointer are set. 5465 if (c.data & 0x3) 5466 outs() << " Swift class"; 5467 outs() << "\n"; 5468 bool is_meta_class; 5469 if (!print_class_ro32_t(c.data & ~0x3, info, is_meta_class)) 5470 return; 5471 5472 if (!is_meta_class) { 5473 outs() << "Meta Class\n"; 5474 print_class32_t(c.isa, info); 5475 } 5476 } 5477 5478 static void print_objc_class_t(struct objc_class_t *objc_class, 5479 struct DisassembleInfo *info) { 5480 uint32_t offset, left, xleft; 5481 const char *name, *p, *ivar_list; 5482 SectionRef S; 5483 int32_t i; 5484 struct objc_ivar_list_t objc_ivar_list; 5485 struct objc_ivar_t ivar; 5486 5487 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa); 5488 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) { 5489 name = get_pointer_32(objc_class->isa, offset, left, S, info, true); 5490 if (name != nullptr) 5491 outs() << format(" %.*s", left, name); 5492 else 5493 outs() << " (not in an __OBJC section)"; 5494 } 5495 outs() << "\n"; 5496 5497 outs() << "\t super_class " 5498 << format("0x%08" PRIx32, objc_class->super_class); 5499 if (info->verbose) { 5500 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true); 5501 if (name != nullptr) 5502 outs() << format(" %.*s", left, name); 5503 else 5504 outs() << " (not in an __OBJC section)"; 5505 } 5506 outs() << "\n"; 5507 5508 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name); 5509 if (info->verbose) { 5510 name = get_pointer_32(objc_class->name, offset, left, S, info, true); 5511 if (name != nullptr) 5512 outs() << format(" %.*s", left, name); 5513 else 5514 outs() << " (not in an __OBJC section)"; 5515 } 5516 outs() << "\n"; 5517 5518 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version) 5519 << "\n"; 5520 5521 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info); 5522 if (info->verbose) { 5523 if (CLS_GETINFO(objc_class, CLS_CLASS)) 5524 outs() << " CLS_CLASS"; 5525 else if (CLS_GETINFO(objc_class, CLS_META)) 5526 outs() << " CLS_META"; 5527 } 5528 outs() << "\n"; 5529 5530 outs() << "\t instance_size " 5531 << format("0x%08" PRIx32, objc_class->instance_size) << "\n"; 5532 5533 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true); 5534 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars); 5535 if (p != nullptr) { 5536 if (left > sizeof(struct objc_ivar_list_t)) { 5537 outs() << "\n"; 5538 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t)); 5539 } else { 5540 outs() << " (entends past the end of the section)\n"; 5541 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t)); 5542 memcpy(&objc_ivar_list, p, left); 5543 } 5544 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5545 swapStruct(objc_ivar_list); 5546 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n"; 5547 ivar_list = p + sizeof(struct objc_ivar_list_t); 5548 for (i = 0; i < objc_ivar_list.ivar_count; i++) { 5549 if ((i + 1) * sizeof(struct objc_ivar_t) > left) { 5550 outs() << "\t\t remaining ivar's extend past the of the section\n"; 5551 break; 5552 } 5553 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t), 5554 sizeof(struct objc_ivar_t)); 5555 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5556 swapStruct(ivar); 5557 5558 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name); 5559 if (info->verbose) { 5560 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true); 5561 if (name != nullptr) 5562 outs() << format(" %.*s", xleft, name); 5563 else 5564 outs() << " (not in an __OBJC section)"; 5565 } 5566 outs() << "\n"; 5567 5568 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type); 5569 if (info->verbose) { 5570 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true); 5571 if (name != nullptr) 5572 outs() << format(" %.*s", xleft, name); 5573 else 5574 outs() << " (not in an __OBJC section)"; 5575 } 5576 outs() << "\n"; 5577 5578 outs() << "\t\t ivar_offset " 5579 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n"; 5580 } 5581 } else { 5582 outs() << " (not in an __OBJC section)\n"; 5583 } 5584 5585 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists); 5586 if (print_method_list(objc_class->methodLists, info)) 5587 outs() << " (not in an __OBJC section)\n"; 5588 5589 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache) 5590 << "\n"; 5591 5592 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols); 5593 if (print_protocol_list(objc_class->protocols, 16, info)) 5594 outs() << " (not in an __OBJC section)\n"; 5595 } 5596 5597 static void print_objc_objc_category_t(struct objc_category_t *objc_category, 5598 struct DisassembleInfo *info) { 5599 uint32_t offset, left; 5600 const char *name; 5601 SectionRef S; 5602 5603 outs() << "\t category name " 5604 << format("0x%08" PRIx32, objc_category->category_name); 5605 if (info->verbose) { 5606 name = get_pointer_32(objc_category->category_name, offset, left, S, info, 5607 true); 5608 if (name != nullptr) 5609 outs() << format(" %.*s", left, name); 5610 else 5611 outs() << " (not in an __OBJC section)"; 5612 } 5613 outs() << "\n"; 5614 5615 outs() << "\t\t class name " 5616 << format("0x%08" PRIx32, objc_category->class_name); 5617 if (info->verbose) { 5618 name = 5619 get_pointer_32(objc_category->class_name, offset, left, S, info, true); 5620 if (name != nullptr) 5621 outs() << format(" %.*s", left, name); 5622 else 5623 outs() << " (not in an __OBJC section)"; 5624 } 5625 outs() << "\n"; 5626 5627 outs() << "\t instance methods " 5628 << format("0x%08" PRIx32, objc_category->instance_methods); 5629 if (print_method_list(objc_category->instance_methods, info)) 5630 outs() << " (not in an __OBJC section)\n"; 5631 5632 outs() << "\t class methods " 5633 << format("0x%08" PRIx32, objc_category->class_methods); 5634 if (print_method_list(objc_category->class_methods, info)) 5635 outs() << " (not in an __OBJC section)\n"; 5636 } 5637 5638 static void print_category64_t(uint64_t p, struct DisassembleInfo *info) { 5639 struct category64_t c; 5640 const char *r; 5641 uint32_t offset, xoffset, left; 5642 SectionRef S, xS; 5643 const char *name, *sym_name; 5644 uint64_t n_value; 5645 5646 r = get_pointer_64(p, offset, left, S, info); 5647 if (r == nullptr) 5648 return; 5649 memset(&c, '\0', sizeof(struct category64_t)); 5650 if (left < sizeof(struct category64_t)) { 5651 memcpy(&c, r, left); 5652 outs() << " (category_t entends past the end of the section)\n"; 5653 } else 5654 memcpy(&c, r, sizeof(struct category64_t)); 5655 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5656 swapStruct(c); 5657 5658 outs() << " name "; 5659 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S, 5660 info, n_value, c.name); 5661 if (n_value != 0) { 5662 if (info->verbose && sym_name != nullptr) 5663 outs() << sym_name; 5664 else 5665 outs() << format("0x%" PRIx64, n_value); 5666 if (c.name != 0) 5667 outs() << " + " << format("0x%" PRIx64, c.name); 5668 } else 5669 outs() << format("0x%" PRIx64, c.name); 5670 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info); 5671 if (name != nullptr) 5672 outs() << format(" %.*s", left, name); 5673 outs() << "\n"; 5674 5675 outs() << " cls "; 5676 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info, 5677 n_value, c.cls); 5678 if (n_value != 0) { 5679 if (info->verbose && sym_name != nullptr) 5680 outs() << sym_name; 5681 else 5682 outs() << format("0x%" PRIx64, n_value); 5683 if (c.cls != 0) 5684 outs() << " + " << format("0x%" PRIx64, c.cls); 5685 } else 5686 outs() << format("0x%" PRIx64, c.cls); 5687 outs() << "\n"; 5688 if (c.cls + n_value != 0) 5689 print_class64_t(c.cls + n_value, info); 5690 5691 outs() << " instanceMethods "; 5692 sym_name = 5693 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S, 5694 info, n_value, c.instanceMethods); 5695 if (n_value != 0) { 5696 if (info->verbose && sym_name != nullptr) 5697 outs() << sym_name; 5698 else 5699 outs() << format("0x%" PRIx64, n_value); 5700 if (c.instanceMethods != 0) 5701 outs() << " + " << format("0x%" PRIx64, c.instanceMethods); 5702 } else 5703 outs() << format("0x%" PRIx64, c.instanceMethods); 5704 outs() << "\n"; 5705 if (c.instanceMethods + n_value != 0) 5706 print_method_list64_t(c.instanceMethods + n_value, info, ""); 5707 5708 outs() << " classMethods "; 5709 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods), 5710 S, info, n_value, c.classMethods); 5711 if (n_value != 0) { 5712 if (info->verbose && sym_name != nullptr) 5713 outs() << sym_name; 5714 else 5715 outs() << format("0x%" PRIx64, n_value); 5716 if (c.classMethods != 0) 5717 outs() << " + " << format("0x%" PRIx64, c.classMethods); 5718 } else 5719 outs() << format("0x%" PRIx64, c.classMethods); 5720 outs() << "\n"; 5721 if (c.classMethods + n_value != 0) 5722 print_method_list64_t(c.classMethods + n_value, info, ""); 5723 5724 outs() << " protocols "; 5725 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S, 5726 info, n_value, c.protocols); 5727 if (n_value != 0) { 5728 if (info->verbose && sym_name != nullptr) 5729 outs() << sym_name; 5730 else 5731 outs() << format("0x%" PRIx64, n_value); 5732 if (c.protocols != 0) 5733 outs() << " + " << format("0x%" PRIx64, c.protocols); 5734 } else 5735 outs() << format("0x%" PRIx64, c.protocols); 5736 outs() << "\n"; 5737 if (c.protocols + n_value != 0) 5738 print_protocol_list64_t(c.protocols + n_value, info); 5739 5740 outs() << "instanceProperties "; 5741 sym_name = 5742 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties), 5743 S, info, n_value, c.instanceProperties); 5744 if (n_value != 0) { 5745 if (info->verbose && sym_name != nullptr) 5746 outs() << sym_name; 5747 else 5748 outs() << format("0x%" PRIx64, n_value); 5749 if (c.instanceProperties != 0) 5750 outs() << " + " << format("0x%" PRIx64, c.instanceProperties); 5751 } else 5752 outs() << format("0x%" PRIx64, c.instanceProperties); 5753 outs() << "\n"; 5754 if (c.instanceProperties + n_value != 0) 5755 print_objc_property_list64(c.instanceProperties + n_value, info); 5756 } 5757 5758 static void print_category32_t(uint32_t p, struct DisassembleInfo *info) { 5759 struct category32_t c; 5760 const char *r; 5761 uint32_t offset, left; 5762 SectionRef S, xS; 5763 const char *name; 5764 5765 r = get_pointer_32(p, offset, left, S, info); 5766 if (r == nullptr) 5767 return; 5768 memset(&c, '\0', sizeof(struct category32_t)); 5769 if (left < sizeof(struct category32_t)) { 5770 memcpy(&c, r, left); 5771 outs() << " (category_t entends past the end of the section)\n"; 5772 } else 5773 memcpy(&c, r, sizeof(struct category32_t)); 5774 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5775 swapStruct(c); 5776 5777 outs() << " name " << format("0x%" PRIx32, c.name); 5778 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info, 5779 c.name); 5780 if (name) 5781 outs() << " " << name; 5782 outs() << "\n"; 5783 5784 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n"; 5785 if (c.cls != 0) 5786 print_class32_t(c.cls, info); 5787 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods) 5788 << "\n"; 5789 if (c.instanceMethods != 0) 5790 print_method_list32_t(c.instanceMethods, info, ""); 5791 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods) 5792 << "\n"; 5793 if (c.classMethods != 0) 5794 print_method_list32_t(c.classMethods, info, ""); 5795 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n"; 5796 if (c.protocols != 0) 5797 print_protocol_list32_t(c.protocols, info); 5798 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties) 5799 << "\n"; 5800 if (c.instanceProperties != 0) 5801 print_objc_property_list32(c.instanceProperties, info); 5802 } 5803 5804 static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) { 5805 uint32_t i, left, offset, xoffset; 5806 uint64_t p, n_value; 5807 struct message_ref64 mr; 5808 const char *name, *sym_name; 5809 const char *r; 5810 SectionRef xS; 5811 5812 if (S == SectionRef()) 5813 return; 5814 5815 StringRef SectName; 5816 Expected<StringRef> SecNameOrErr = S.getName(); 5817 if (SecNameOrErr) 5818 SectName = *SecNameOrErr; 5819 else 5820 consumeError(SecNameOrErr.takeError()); 5821 5822 DataRefImpl Ref = S.getRawDataRefImpl(); 5823 StringRef SegName = info->O->getSectionFinalSegmentName(Ref); 5824 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 5825 offset = 0; 5826 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) { 5827 p = S.getAddress() + i; 5828 r = get_pointer_64(p, offset, left, S, info); 5829 if (r == nullptr) 5830 return; 5831 memset(&mr, '\0', sizeof(struct message_ref64)); 5832 if (left < sizeof(struct message_ref64)) { 5833 memcpy(&mr, r, left); 5834 outs() << " (message_ref entends past the end of the section)\n"; 5835 } else 5836 memcpy(&mr, r, sizeof(struct message_ref64)); 5837 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5838 swapStruct(mr); 5839 5840 outs() << " imp "; 5841 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info, 5842 n_value, mr.imp); 5843 if (n_value != 0) { 5844 outs() << format("0x%" PRIx64, n_value) << " "; 5845 if (mr.imp != 0) 5846 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " "; 5847 } else 5848 outs() << format("0x%" PRIx64, mr.imp) << " "; 5849 if (name != nullptr) 5850 outs() << " " << name; 5851 outs() << "\n"; 5852 5853 outs() << " sel "; 5854 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S, 5855 info, n_value, mr.sel); 5856 if (n_value != 0) { 5857 if (info->verbose && sym_name != nullptr) 5858 outs() << sym_name; 5859 else 5860 outs() << format("0x%" PRIx64, n_value); 5861 if (mr.sel != 0) 5862 outs() << " + " << format("0x%" PRIx64, mr.sel); 5863 } else 5864 outs() << format("0x%" PRIx64, mr.sel); 5865 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info); 5866 if (name != nullptr) 5867 outs() << format(" %.*s", left, name); 5868 outs() << "\n"; 5869 5870 offset += sizeof(struct message_ref64); 5871 } 5872 } 5873 5874 static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) { 5875 uint32_t i, left, offset, xoffset, p; 5876 struct message_ref32 mr; 5877 const char *name, *r; 5878 SectionRef xS; 5879 5880 if (S == SectionRef()) 5881 return; 5882 5883 StringRef SectName; 5884 Expected<StringRef> SecNameOrErr = S.getName(); 5885 if (SecNameOrErr) 5886 SectName = *SecNameOrErr; 5887 else 5888 consumeError(SecNameOrErr.takeError()); 5889 5890 DataRefImpl Ref = S.getRawDataRefImpl(); 5891 StringRef SegName = info->O->getSectionFinalSegmentName(Ref); 5892 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 5893 offset = 0; 5894 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) { 5895 p = S.getAddress() + i; 5896 r = get_pointer_32(p, offset, left, S, info); 5897 if (r == nullptr) 5898 return; 5899 memset(&mr, '\0', sizeof(struct message_ref32)); 5900 if (left < sizeof(struct message_ref32)) { 5901 memcpy(&mr, r, left); 5902 outs() << " (message_ref entends past the end of the section)\n"; 5903 } else 5904 memcpy(&mr, r, sizeof(struct message_ref32)); 5905 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5906 swapStruct(mr); 5907 5908 outs() << " imp " << format("0x%" PRIx32, mr.imp); 5909 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info, 5910 mr.imp); 5911 if (name != nullptr) 5912 outs() << " " << name; 5913 outs() << "\n"; 5914 5915 outs() << " sel " << format("0x%" PRIx32, mr.sel); 5916 name = get_pointer_32(mr.sel, xoffset, left, xS, info); 5917 if (name != nullptr) 5918 outs() << " " << name; 5919 outs() << "\n"; 5920 5921 offset += sizeof(struct message_ref32); 5922 } 5923 } 5924 5925 static void print_image_info64(SectionRef S, struct DisassembleInfo *info) { 5926 uint32_t left, offset, swift_version; 5927 uint64_t p; 5928 struct objc_image_info64 o; 5929 const char *r; 5930 5931 if (S == SectionRef()) 5932 return; 5933 5934 StringRef SectName; 5935 Expected<StringRef> SecNameOrErr = S.getName(); 5936 if (SecNameOrErr) 5937 SectName = *SecNameOrErr; 5938 else 5939 consumeError(SecNameOrErr.takeError()); 5940 5941 DataRefImpl Ref = S.getRawDataRefImpl(); 5942 StringRef SegName = info->O->getSectionFinalSegmentName(Ref); 5943 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 5944 p = S.getAddress(); 5945 r = get_pointer_64(p, offset, left, S, info); 5946 if (r == nullptr) 5947 return; 5948 memset(&o, '\0', sizeof(struct objc_image_info64)); 5949 if (left < sizeof(struct objc_image_info64)) { 5950 memcpy(&o, r, left); 5951 outs() << " (objc_image_info entends past the end of the section)\n"; 5952 } else 5953 memcpy(&o, r, sizeof(struct objc_image_info64)); 5954 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5955 swapStruct(o); 5956 outs() << " version " << o.version << "\n"; 5957 outs() << " flags " << format("0x%" PRIx32, o.flags); 5958 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT) 5959 outs() << " OBJC_IMAGE_IS_REPLACEMENT"; 5960 if (o.flags & OBJC_IMAGE_SUPPORTS_GC) 5961 outs() << " OBJC_IMAGE_SUPPORTS_GC"; 5962 if (o.flags & OBJC_IMAGE_IS_SIMULATED) 5963 outs() << " OBJC_IMAGE_IS_SIMULATED"; 5964 if (o.flags & OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES) 5965 outs() << " OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES"; 5966 swift_version = (o.flags >> 8) & 0xff; 5967 if (swift_version != 0) { 5968 if (swift_version == 1) 5969 outs() << " Swift 1.0"; 5970 else if (swift_version == 2) 5971 outs() << " Swift 1.1"; 5972 else if(swift_version == 3) 5973 outs() << " Swift 2.0"; 5974 else if(swift_version == 4) 5975 outs() << " Swift 3.0"; 5976 else if(swift_version == 5) 5977 outs() << " Swift 4.0"; 5978 else if(swift_version == 6) 5979 outs() << " Swift 4.1/Swift 4.2"; 5980 else if(swift_version == 7) 5981 outs() << " Swift 5 or later"; 5982 else 5983 outs() << " unknown future Swift version (" << swift_version << ")"; 5984 } 5985 outs() << "\n"; 5986 } 5987 5988 static void print_image_info32(SectionRef S, struct DisassembleInfo *info) { 5989 uint32_t left, offset, swift_version, p; 5990 struct objc_image_info32 o; 5991 const char *r; 5992 5993 if (S == SectionRef()) 5994 return; 5995 5996 StringRef SectName; 5997 Expected<StringRef> SecNameOrErr = S.getName(); 5998 if (SecNameOrErr) 5999 SectName = *SecNameOrErr; 6000 else 6001 consumeError(SecNameOrErr.takeError()); 6002 6003 DataRefImpl Ref = S.getRawDataRefImpl(); 6004 StringRef SegName = info->O->getSectionFinalSegmentName(Ref); 6005 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 6006 p = S.getAddress(); 6007 r = get_pointer_32(p, offset, left, S, info); 6008 if (r == nullptr) 6009 return; 6010 memset(&o, '\0', sizeof(struct objc_image_info32)); 6011 if (left < sizeof(struct objc_image_info32)) { 6012 memcpy(&o, r, left); 6013 outs() << " (objc_image_info entends past the end of the section)\n"; 6014 } else 6015 memcpy(&o, r, sizeof(struct objc_image_info32)); 6016 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 6017 swapStruct(o); 6018 outs() << " version " << o.version << "\n"; 6019 outs() << " flags " << format("0x%" PRIx32, o.flags); 6020 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT) 6021 outs() << " OBJC_IMAGE_IS_REPLACEMENT"; 6022 if (o.flags & OBJC_IMAGE_SUPPORTS_GC) 6023 outs() << " OBJC_IMAGE_SUPPORTS_GC"; 6024 swift_version = (o.flags >> 8) & 0xff; 6025 if (swift_version != 0) { 6026 if (swift_version == 1) 6027 outs() << " Swift 1.0"; 6028 else if (swift_version == 2) 6029 outs() << " Swift 1.1"; 6030 else if(swift_version == 3) 6031 outs() << " Swift 2.0"; 6032 else if(swift_version == 4) 6033 outs() << " Swift 3.0"; 6034 else if(swift_version == 5) 6035 outs() << " Swift 4.0"; 6036 else if(swift_version == 6) 6037 outs() << " Swift 4.1/Swift 4.2"; 6038 else if(swift_version == 7) 6039 outs() << " Swift 5 or later"; 6040 else 6041 outs() << " unknown future Swift version (" << swift_version << ")"; 6042 } 6043 outs() << "\n"; 6044 } 6045 6046 static void print_image_info(SectionRef S, struct DisassembleInfo *info) { 6047 uint32_t left, offset, p; 6048 struct imageInfo_t o; 6049 const char *r; 6050 6051 StringRef SectName; 6052 Expected<StringRef> SecNameOrErr = S.getName(); 6053 if (SecNameOrErr) 6054 SectName = *SecNameOrErr; 6055 else 6056 consumeError(SecNameOrErr.takeError()); 6057 6058 DataRefImpl Ref = S.getRawDataRefImpl(); 6059 StringRef SegName = info->O->getSectionFinalSegmentName(Ref); 6060 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 6061 p = S.getAddress(); 6062 r = get_pointer_32(p, offset, left, S, info); 6063 if (r == nullptr) 6064 return; 6065 memset(&o, '\0', sizeof(struct imageInfo_t)); 6066 if (left < sizeof(struct imageInfo_t)) { 6067 memcpy(&o, r, left); 6068 outs() << " (imageInfo entends past the end of the section)\n"; 6069 } else 6070 memcpy(&o, r, sizeof(struct imageInfo_t)); 6071 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 6072 swapStruct(o); 6073 outs() << " version " << o.version << "\n"; 6074 outs() << " flags " << format("0x%" PRIx32, o.flags); 6075 if (o.flags & 0x1) 6076 outs() << " F&C"; 6077 if (o.flags & 0x2) 6078 outs() << " GC"; 6079 if (o.flags & 0x4) 6080 outs() << " GC-only"; 6081 else 6082 outs() << " RR"; 6083 outs() << "\n"; 6084 } 6085 6086 static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) { 6087 SymbolAddressMap AddrMap; 6088 if (verbose) 6089 CreateSymbolAddressMap(O, &AddrMap); 6090 6091 std::vector<SectionRef> Sections; 6092 for (const SectionRef &Section : O->sections()) 6093 Sections.push_back(Section); 6094 6095 struct DisassembleInfo info(O, &AddrMap, &Sections, verbose); 6096 6097 SectionRef CL = get_section(O, "__OBJC2", "__class_list"); 6098 if (CL == SectionRef()) 6099 CL = get_section(O, "__DATA", "__objc_classlist"); 6100 if (CL == SectionRef()) 6101 CL = get_section(O, "__DATA_CONST", "__objc_classlist"); 6102 if (CL == SectionRef()) 6103 CL = get_section(O, "__DATA_DIRTY", "__objc_classlist"); 6104 info.S = CL; 6105 walk_pointer_list_64("class", CL, O, &info, print_class64_t); 6106 6107 SectionRef CR = get_section(O, "__OBJC2", "__class_refs"); 6108 if (CR == SectionRef()) 6109 CR = get_section(O, "__DATA", "__objc_classrefs"); 6110 if (CR == SectionRef()) 6111 CR = get_section(O, "__DATA_CONST", "__objc_classrefs"); 6112 if (CR == SectionRef()) 6113 CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs"); 6114 info.S = CR; 6115 walk_pointer_list_64("class refs", CR, O, &info, nullptr); 6116 6117 SectionRef SR = get_section(O, "__OBJC2", "__super_refs"); 6118 if (SR == SectionRef()) 6119 SR = get_section(O, "__DATA", "__objc_superrefs"); 6120 if (SR == SectionRef()) 6121 SR = get_section(O, "__DATA_CONST", "__objc_superrefs"); 6122 if (SR == SectionRef()) 6123 SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs"); 6124 info.S = SR; 6125 walk_pointer_list_64("super refs", SR, O, &info, nullptr); 6126 6127 SectionRef CA = get_section(O, "__OBJC2", "__category_list"); 6128 if (CA == SectionRef()) 6129 CA = get_section(O, "__DATA", "__objc_catlist"); 6130 if (CA == SectionRef()) 6131 CA = get_section(O, "__DATA_CONST", "__objc_catlist"); 6132 if (CA == SectionRef()) 6133 CA = get_section(O, "__DATA_DIRTY", "__objc_catlist"); 6134 info.S = CA; 6135 walk_pointer_list_64("category", CA, O, &info, print_category64_t); 6136 6137 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list"); 6138 if (PL == SectionRef()) 6139 PL = get_section(O, "__DATA", "__objc_protolist"); 6140 if (PL == SectionRef()) 6141 PL = get_section(O, "__DATA_CONST", "__objc_protolist"); 6142 if (PL == SectionRef()) 6143 PL = get_section(O, "__DATA_DIRTY", "__objc_protolist"); 6144 info.S = PL; 6145 walk_pointer_list_64("protocol", PL, O, &info, nullptr); 6146 6147 SectionRef MR = get_section(O, "__OBJC2", "__message_refs"); 6148 if (MR == SectionRef()) 6149 MR = get_section(O, "__DATA", "__objc_msgrefs"); 6150 if (MR == SectionRef()) 6151 MR = get_section(O, "__DATA_CONST", "__objc_msgrefs"); 6152 if (MR == SectionRef()) 6153 MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs"); 6154 info.S = MR; 6155 print_message_refs64(MR, &info); 6156 6157 SectionRef II = get_section(O, "__OBJC2", "__image_info"); 6158 if (II == SectionRef()) 6159 II = get_section(O, "__DATA", "__objc_imageinfo"); 6160 if (II == SectionRef()) 6161 II = get_section(O, "__DATA_CONST", "__objc_imageinfo"); 6162 if (II == SectionRef()) 6163 II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo"); 6164 info.S = II; 6165 print_image_info64(II, &info); 6166 } 6167 6168 static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) { 6169 SymbolAddressMap AddrMap; 6170 if (verbose) 6171 CreateSymbolAddressMap(O, &AddrMap); 6172 6173 std::vector<SectionRef> Sections; 6174 for (const SectionRef &Section : O->sections()) 6175 Sections.push_back(Section); 6176 6177 struct DisassembleInfo info(O, &AddrMap, &Sections, verbose); 6178 6179 SectionRef CL = get_section(O, "__OBJC2", "__class_list"); 6180 if (CL == SectionRef()) 6181 CL = get_section(O, "__DATA", "__objc_classlist"); 6182 if (CL == SectionRef()) 6183 CL = get_section(O, "__DATA_CONST", "__objc_classlist"); 6184 if (CL == SectionRef()) 6185 CL = get_section(O, "__DATA_DIRTY", "__objc_classlist"); 6186 info.S = CL; 6187 walk_pointer_list_32("class", CL, O, &info, print_class32_t); 6188 6189 SectionRef CR = get_section(O, "__OBJC2", "__class_refs"); 6190 if (CR == SectionRef()) 6191 CR = get_section(O, "__DATA", "__objc_classrefs"); 6192 if (CR == SectionRef()) 6193 CR = get_section(O, "__DATA_CONST", "__objc_classrefs"); 6194 if (CR == SectionRef()) 6195 CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs"); 6196 info.S = CR; 6197 walk_pointer_list_32("class refs", CR, O, &info, nullptr); 6198 6199 SectionRef SR = get_section(O, "__OBJC2", "__super_refs"); 6200 if (SR == SectionRef()) 6201 SR = get_section(O, "__DATA", "__objc_superrefs"); 6202 if (SR == SectionRef()) 6203 SR = get_section(O, "__DATA_CONST", "__objc_superrefs"); 6204 if (SR == SectionRef()) 6205 SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs"); 6206 info.S = SR; 6207 walk_pointer_list_32("super refs", SR, O, &info, nullptr); 6208 6209 SectionRef CA = get_section(O, "__OBJC2", "__category_list"); 6210 if (CA == SectionRef()) 6211 CA = get_section(O, "__DATA", "__objc_catlist"); 6212 if (CA == SectionRef()) 6213 CA = get_section(O, "__DATA_CONST", "__objc_catlist"); 6214 if (CA == SectionRef()) 6215 CA = get_section(O, "__DATA_DIRTY", "__objc_catlist"); 6216 info.S = CA; 6217 walk_pointer_list_32("category", CA, O, &info, print_category32_t); 6218 6219 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list"); 6220 if (PL == SectionRef()) 6221 PL = get_section(O, "__DATA", "__objc_protolist"); 6222 if (PL == SectionRef()) 6223 PL = get_section(O, "__DATA_CONST", "__objc_protolist"); 6224 if (PL == SectionRef()) 6225 PL = get_section(O, "__DATA_DIRTY", "__objc_protolist"); 6226 info.S = PL; 6227 walk_pointer_list_32("protocol", PL, O, &info, nullptr); 6228 6229 SectionRef MR = get_section(O, "__OBJC2", "__message_refs"); 6230 if (MR == SectionRef()) 6231 MR = get_section(O, "__DATA", "__objc_msgrefs"); 6232 if (MR == SectionRef()) 6233 MR = get_section(O, "__DATA_CONST", "__objc_msgrefs"); 6234 if (MR == SectionRef()) 6235 MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs"); 6236 info.S = MR; 6237 print_message_refs32(MR, &info); 6238 6239 SectionRef II = get_section(O, "__OBJC2", "__image_info"); 6240 if (II == SectionRef()) 6241 II = get_section(O, "__DATA", "__objc_imageinfo"); 6242 if (II == SectionRef()) 6243 II = get_section(O, "__DATA_CONST", "__objc_imageinfo"); 6244 if (II == SectionRef()) 6245 II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo"); 6246 info.S = II; 6247 print_image_info32(II, &info); 6248 } 6249 6250 static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) { 6251 uint32_t i, j, p, offset, xoffset, left, defs_left, def; 6252 const char *r, *name, *defs; 6253 struct objc_module_t module; 6254 SectionRef S, xS; 6255 struct objc_symtab_t symtab; 6256 struct objc_class_t objc_class; 6257 struct objc_category_t objc_category; 6258 6259 outs() << "Objective-C segment\n"; 6260 S = get_section(O, "__OBJC", "__module_info"); 6261 if (S == SectionRef()) 6262 return false; 6263 6264 SymbolAddressMap AddrMap; 6265 if (verbose) 6266 CreateSymbolAddressMap(O, &AddrMap); 6267 6268 std::vector<SectionRef> Sections; 6269 for (const SectionRef &Section : O->sections()) 6270 Sections.push_back(Section); 6271 6272 struct DisassembleInfo info(O, &AddrMap, &Sections, verbose); 6273 6274 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) { 6275 p = S.getAddress() + i; 6276 r = get_pointer_32(p, offset, left, S, &info, true); 6277 if (r == nullptr) 6278 return true; 6279 memset(&module, '\0', sizeof(struct objc_module_t)); 6280 if (left < sizeof(struct objc_module_t)) { 6281 memcpy(&module, r, left); 6282 outs() << " (module extends past end of __module_info section)\n"; 6283 } else 6284 memcpy(&module, r, sizeof(struct objc_module_t)); 6285 if (O->isLittleEndian() != sys::IsLittleEndianHost) 6286 swapStruct(module); 6287 6288 outs() << "Module " << format("0x%" PRIx32, p) << "\n"; 6289 outs() << " version " << module.version << "\n"; 6290 outs() << " size " << module.size << "\n"; 6291 outs() << " name "; 6292 name = get_pointer_32(module.name, xoffset, left, xS, &info, true); 6293 if (name != nullptr) 6294 outs() << format("%.*s", left, name); 6295 else 6296 outs() << format("0x%08" PRIx32, module.name) 6297 << "(not in an __OBJC section)"; 6298 outs() << "\n"; 6299 6300 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true); 6301 if (module.symtab == 0 || r == nullptr) { 6302 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) 6303 << " (not in an __OBJC section)\n"; 6304 continue; 6305 } 6306 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n"; 6307 memset(&symtab, '\0', sizeof(struct objc_symtab_t)); 6308 defs_left = 0; 6309 defs = nullptr; 6310 if (left < sizeof(struct objc_symtab_t)) { 6311 memcpy(&symtab, r, left); 6312 outs() << "\tsymtab extends past end of an __OBJC section)\n"; 6313 } else { 6314 memcpy(&symtab, r, sizeof(struct objc_symtab_t)); 6315 if (left > sizeof(struct objc_symtab_t)) { 6316 defs_left = left - sizeof(struct objc_symtab_t); 6317 defs = r + sizeof(struct objc_symtab_t); 6318 } 6319 } 6320 if (O->isLittleEndian() != sys::IsLittleEndianHost) 6321 swapStruct(symtab); 6322 6323 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n"; 6324 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true); 6325 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs); 6326 if (r == nullptr) 6327 outs() << " (not in an __OBJC section)"; 6328 outs() << "\n"; 6329 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n"; 6330 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n"; 6331 if (symtab.cls_def_cnt > 0) 6332 outs() << "\tClass Definitions\n"; 6333 for (j = 0; j < symtab.cls_def_cnt; j++) { 6334 if ((j + 1) * sizeof(uint32_t) > defs_left) { 6335 outs() << "\t(remaining class defs entries entends past the end of the " 6336 << "section)\n"; 6337 break; 6338 } 6339 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t)); 6340 if (O->isLittleEndian() != sys::IsLittleEndianHost) 6341 sys::swapByteOrder(def); 6342 6343 r = get_pointer_32(def, xoffset, left, xS, &info, true); 6344 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def); 6345 if (r != nullptr) { 6346 if (left > sizeof(struct objc_class_t)) { 6347 outs() << "\n"; 6348 memcpy(&objc_class, r, sizeof(struct objc_class_t)); 6349 } else { 6350 outs() << " (entends past the end of the section)\n"; 6351 memset(&objc_class, '\0', sizeof(struct objc_class_t)); 6352 memcpy(&objc_class, r, left); 6353 } 6354 if (O->isLittleEndian() != sys::IsLittleEndianHost) 6355 swapStruct(objc_class); 6356 print_objc_class_t(&objc_class, &info); 6357 } else { 6358 outs() << "(not in an __OBJC section)\n"; 6359 } 6360 6361 if (CLS_GETINFO(&objc_class, CLS_CLASS)) { 6362 outs() << "\tMeta Class"; 6363 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true); 6364 if (r != nullptr) { 6365 if (left > sizeof(struct objc_class_t)) { 6366 outs() << "\n"; 6367 memcpy(&objc_class, r, sizeof(struct objc_class_t)); 6368 } else { 6369 outs() << " (entends past the end of the section)\n"; 6370 memset(&objc_class, '\0', sizeof(struct objc_class_t)); 6371 memcpy(&objc_class, r, left); 6372 } 6373 if (O->isLittleEndian() != sys::IsLittleEndianHost) 6374 swapStruct(objc_class); 6375 print_objc_class_t(&objc_class, &info); 6376 } else { 6377 outs() << "(not in an __OBJC section)\n"; 6378 } 6379 } 6380 } 6381 if (symtab.cat_def_cnt > 0) 6382 outs() << "\tCategory Definitions\n"; 6383 for (j = 0; j < symtab.cat_def_cnt; j++) { 6384 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) { 6385 outs() << "\t(remaining category defs entries entends past the end of " 6386 << "the section)\n"; 6387 break; 6388 } 6389 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t), 6390 sizeof(uint32_t)); 6391 if (O->isLittleEndian() != sys::IsLittleEndianHost) 6392 sys::swapByteOrder(def); 6393 6394 r = get_pointer_32(def, xoffset, left, xS, &info, true); 6395 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] " 6396 << format("0x%08" PRIx32, def); 6397 if (r != nullptr) { 6398 if (left > sizeof(struct objc_category_t)) { 6399 outs() << "\n"; 6400 memcpy(&objc_category, r, sizeof(struct objc_category_t)); 6401 } else { 6402 outs() << " (entends past the end of the section)\n"; 6403 memset(&objc_category, '\0', sizeof(struct objc_category_t)); 6404 memcpy(&objc_category, r, left); 6405 } 6406 if (O->isLittleEndian() != sys::IsLittleEndianHost) 6407 swapStruct(objc_category); 6408 print_objc_objc_category_t(&objc_category, &info); 6409 } else { 6410 outs() << "(not in an __OBJC section)\n"; 6411 } 6412 } 6413 } 6414 const SectionRef II = get_section(O, "__OBJC", "__image_info"); 6415 if (II != SectionRef()) 6416 print_image_info(II, &info); 6417 6418 return true; 6419 } 6420 6421 static void DumpProtocolSection(MachOObjectFile *O, const char *sect, 6422 uint32_t size, uint32_t addr) { 6423 SymbolAddressMap AddrMap; 6424 CreateSymbolAddressMap(O, &AddrMap); 6425 6426 std::vector<SectionRef> Sections; 6427 for (const SectionRef &Section : O->sections()) 6428 Sections.push_back(Section); 6429 6430 struct DisassembleInfo info(O, &AddrMap, &Sections, true); 6431 6432 const char *p; 6433 struct objc_protocol_t protocol; 6434 uint32_t left, paddr; 6435 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) { 6436 memset(&protocol, '\0', sizeof(struct objc_protocol_t)); 6437 left = size - (p - sect); 6438 if (left < sizeof(struct objc_protocol_t)) { 6439 outs() << "Protocol extends past end of __protocol section\n"; 6440 memcpy(&protocol, p, left); 6441 } else 6442 memcpy(&protocol, p, sizeof(struct objc_protocol_t)); 6443 if (O->isLittleEndian() != sys::IsLittleEndianHost) 6444 swapStruct(protocol); 6445 paddr = addr + (p - sect); 6446 outs() << "Protocol " << format("0x%" PRIx32, paddr); 6447 if (print_protocol(paddr, 0, &info)) 6448 outs() << "(not in an __OBJC section)\n"; 6449 } 6450 } 6451 6452 #ifdef HAVE_LIBXAR 6453 inline void swapStruct(struct xar_header &xar) { 6454 sys::swapByteOrder(xar.magic); 6455 sys::swapByteOrder(xar.size); 6456 sys::swapByteOrder(xar.version); 6457 sys::swapByteOrder(xar.toc_length_compressed); 6458 sys::swapByteOrder(xar.toc_length_uncompressed); 6459 sys::swapByteOrder(xar.cksum_alg); 6460 } 6461 6462 static void PrintModeVerbose(uint32_t mode) { 6463 switch(mode & S_IFMT){ 6464 case S_IFDIR: 6465 outs() << "d"; 6466 break; 6467 case S_IFCHR: 6468 outs() << "c"; 6469 break; 6470 case S_IFBLK: 6471 outs() << "b"; 6472 break; 6473 case S_IFREG: 6474 outs() << "-"; 6475 break; 6476 case S_IFLNK: 6477 outs() << "l"; 6478 break; 6479 case S_IFSOCK: 6480 outs() << "s"; 6481 break; 6482 default: 6483 outs() << "?"; 6484 break; 6485 } 6486 6487 /* owner permissions */ 6488 if(mode & S_IREAD) 6489 outs() << "r"; 6490 else 6491 outs() << "-"; 6492 if(mode & S_IWRITE) 6493 outs() << "w"; 6494 else 6495 outs() << "-"; 6496 if(mode & S_ISUID) 6497 outs() << "s"; 6498 else if(mode & S_IEXEC) 6499 outs() << "x"; 6500 else 6501 outs() << "-"; 6502 6503 /* group permissions */ 6504 if(mode & (S_IREAD >> 3)) 6505 outs() << "r"; 6506 else 6507 outs() << "-"; 6508 if(mode & (S_IWRITE >> 3)) 6509 outs() << "w"; 6510 else 6511 outs() << "-"; 6512 if(mode & S_ISGID) 6513 outs() << "s"; 6514 else if(mode & (S_IEXEC >> 3)) 6515 outs() << "x"; 6516 else 6517 outs() << "-"; 6518 6519 /* other permissions */ 6520 if(mode & (S_IREAD >> 6)) 6521 outs() << "r"; 6522 else 6523 outs() << "-"; 6524 if(mode & (S_IWRITE >> 6)) 6525 outs() << "w"; 6526 else 6527 outs() << "-"; 6528 if(mode & S_ISVTX) 6529 outs() << "t"; 6530 else if(mode & (S_IEXEC >> 6)) 6531 outs() << "x"; 6532 else 6533 outs() << "-"; 6534 } 6535 6536 static void PrintXarFilesSummary(const char *XarFilename, xar_t xar) { 6537 xar_file_t xf; 6538 const char *key, *type, *mode, *user, *group, *size, *mtime, *name, *m; 6539 char *endp; 6540 uint32_t mode_value; 6541 6542 ScopedXarIter xi; 6543 if (!xi) { 6544 WithColor::error(errs(), "llvm-objdump") 6545 << "can't obtain an xar iterator for xar archive " << XarFilename 6546 << "\n"; 6547 return; 6548 } 6549 6550 // Go through the xar's files. 6551 for (xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)) { 6552 ScopedXarIter xp; 6553 if(!xp){ 6554 WithColor::error(errs(), "llvm-objdump") 6555 << "can't obtain an xar iterator for xar archive " << XarFilename 6556 << "\n"; 6557 return; 6558 } 6559 type = nullptr; 6560 mode = nullptr; 6561 user = nullptr; 6562 group = nullptr; 6563 size = nullptr; 6564 mtime = nullptr; 6565 name = nullptr; 6566 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){ 6567 const char *val = nullptr; 6568 xar_prop_get(xf, key, &val); 6569 #if 0 // Useful for debugging. 6570 outs() << "key: " << key << " value: " << val << "\n"; 6571 #endif 6572 if(strcmp(key, "type") == 0) 6573 type = val; 6574 if(strcmp(key, "mode") == 0) 6575 mode = val; 6576 if(strcmp(key, "user") == 0) 6577 user = val; 6578 if(strcmp(key, "group") == 0) 6579 group = val; 6580 if(strcmp(key, "data/size") == 0) 6581 size = val; 6582 if(strcmp(key, "mtime") == 0) 6583 mtime = val; 6584 if(strcmp(key, "name") == 0) 6585 name = val; 6586 } 6587 if(mode != nullptr){ 6588 mode_value = strtoul(mode, &endp, 8); 6589 if(*endp != '\0') 6590 outs() << "(mode: \"" << mode << "\" contains non-octal chars) "; 6591 if(strcmp(type, "file") == 0) 6592 mode_value |= S_IFREG; 6593 PrintModeVerbose(mode_value); 6594 outs() << " "; 6595 } 6596 if(user != nullptr) 6597 outs() << format("%10s/", user); 6598 if(group != nullptr) 6599 outs() << format("%-10s ", group); 6600 if(size != nullptr) 6601 outs() << format("%7s ", size); 6602 if(mtime != nullptr){ 6603 for(m = mtime; *m != 'T' && *m != '\0'; m++) 6604 outs() << *m; 6605 if(*m == 'T') 6606 m++; 6607 outs() << " "; 6608 for( ; *m != 'Z' && *m != '\0'; m++) 6609 outs() << *m; 6610 outs() << " "; 6611 } 6612 if(name != nullptr) 6613 outs() << name; 6614 outs() << "\n"; 6615 } 6616 } 6617 6618 static void DumpBitcodeSection(MachOObjectFile *O, const char *sect, 6619 uint32_t size, bool verbose, 6620 bool PrintXarHeader, bool PrintXarFileHeaders, 6621 std::string XarMemberName) { 6622 if(size < sizeof(struct xar_header)) { 6623 outs() << "size of (__LLVM,__bundle) section too small (smaller than size " 6624 "of struct xar_header)\n"; 6625 return; 6626 } 6627 struct xar_header XarHeader; 6628 memcpy(&XarHeader, sect, sizeof(struct xar_header)); 6629 if (sys::IsLittleEndianHost) 6630 swapStruct(XarHeader); 6631 if (PrintXarHeader) { 6632 if (!XarMemberName.empty()) 6633 outs() << "In xar member " << XarMemberName << ": "; 6634 else 6635 outs() << "For (__LLVM,__bundle) section: "; 6636 outs() << "xar header\n"; 6637 if (XarHeader.magic == XAR_HEADER_MAGIC) 6638 outs() << " magic XAR_HEADER_MAGIC\n"; 6639 else 6640 outs() << " magic " 6641 << format_hex(XarHeader.magic, 10, true) 6642 << " (not XAR_HEADER_MAGIC)\n"; 6643 outs() << " size " << XarHeader.size << "\n"; 6644 outs() << " version " << XarHeader.version << "\n"; 6645 outs() << " toc_length_compressed " << XarHeader.toc_length_compressed 6646 << "\n"; 6647 outs() << "toc_length_uncompressed " << XarHeader.toc_length_uncompressed 6648 << "\n"; 6649 outs() << " cksum_alg "; 6650 switch (XarHeader.cksum_alg) { 6651 case XAR_CKSUM_NONE: 6652 outs() << "XAR_CKSUM_NONE\n"; 6653 break; 6654 case XAR_CKSUM_SHA1: 6655 outs() << "XAR_CKSUM_SHA1\n"; 6656 break; 6657 case XAR_CKSUM_MD5: 6658 outs() << "XAR_CKSUM_MD5\n"; 6659 break; 6660 #ifdef XAR_CKSUM_SHA256 6661 case XAR_CKSUM_SHA256: 6662 outs() << "XAR_CKSUM_SHA256\n"; 6663 break; 6664 #endif 6665 #ifdef XAR_CKSUM_SHA512 6666 case XAR_CKSUM_SHA512: 6667 outs() << "XAR_CKSUM_SHA512\n"; 6668 break; 6669 #endif 6670 default: 6671 outs() << XarHeader.cksum_alg << "\n"; 6672 } 6673 } 6674 6675 SmallString<128> XarFilename; 6676 int FD; 6677 std::error_code XarEC = 6678 sys::fs::createTemporaryFile("llvm-objdump", "xar", FD, XarFilename); 6679 if (XarEC) { 6680 WithColor::error(errs(), "llvm-objdump") << XarEC.message() << "\n"; 6681 return; 6682 } 6683 ToolOutputFile XarFile(XarFilename, FD); 6684 raw_fd_ostream &XarOut = XarFile.os(); 6685 StringRef XarContents(sect, size); 6686 XarOut << XarContents; 6687 XarOut.close(); 6688 if (XarOut.has_error()) 6689 return; 6690 6691 ScopedXarFile xar(XarFilename.c_str(), READ); 6692 if (!xar) { 6693 WithColor::error(errs(), "llvm-objdump") 6694 << "can't create temporary xar archive " << XarFilename << "\n"; 6695 return; 6696 } 6697 6698 SmallString<128> TocFilename; 6699 std::error_code TocEC = 6700 sys::fs::createTemporaryFile("llvm-objdump", "toc", TocFilename); 6701 if (TocEC) { 6702 WithColor::error(errs(), "llvm-objdump") << TocEC.message() << "\n"; 6703 return; 6704 } 6705 xar_serialize(xar, TocFilename.c_str()); 6706 6707 if (PrintXarFileHeaders) { 6708 if (!XarMemberName.empty()) 6709 outs() << "In xar member " << XarMemberName << ": "; 6710 else 6711 outs() << "For (__LLVM,__bundle) section: "; 6712 outs() << "xar archive files:\n"; 6713 PrintXarFilesSummary(XarFilename.c_str(), xar); 6714 } 6715 6716 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = 6717 MemoryBuffer::getFileOrSTDIN(TocFilename.c_str()); 6718 if (std::error_code EC = FileOrErr.getError()) { 6719 WithColor::error(errs(), "llvm-objdump") << EC.message() << "\n"; 6720 return; 6721 } 6722 std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get(); 6723 6724 if (!XarMemberName.empty()) 6725 outs() << "In xar member " << XarMemberName << ": "; 6726 else 6727 outs() << "For (__LLVM,__bundle) section: "; 6728 outs() << "xar table of contents:\n"; 6729 outs() << Buffer->getBuffer() << "\n"; 6730 6731 // TODO: Go through the xar's files. 6732 ScopedXarIter xi; 6733 if(!xi){ 6734 WithColor::error(errs(), "llvm-objdump") 6735 << "can't obtain an xar iterator for xar archive " 6736 << XarFilename.c_str() << "\n"; 6737 return; 6738 } 6739 for(xar_file_t xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)){ 6740 const char *key; 6741 const char *member_name, *member_type, *member_size_string; 6742 size_t member_size; 6743 6744 ScopedXarIter xp; 6745 if(!xp){ 6746 WithColor::error(errs(), "llvm-objdump") 6747 << "can't obtain an xar iterator for xar archive " 6748 << XarFilename.c_str() << "\n"; 6749 return; 6750 } 6751 member_name = NULL; 6752 member_type = NULL; 6753 member_size_string = NULL; 6754 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){ 6755 const char *val = nullptr; 6756 xar_prop_get(xf, key, &val); 6757 #if 0 // Useful for debugging. 6758 outs() << "key: " << key << " value: " << val << "\n"; 6759 #endif 6760 if (strcmp(key, "name") == 0) 6761 member_name = val; 6762 if (strcmp(key, "type") == 0) 6763 member_type = val; 6764 if (strcmp(key, "data/size") == 0) 6765 member_size_string = val; 6766 } 6767 /* 6768 * If we find a file with a name, date/size and type properties 6769 * and with the type being "file" see if that is a xar file. 6770 */ 6771 if (member_name != NULL && member_type != NULL && 6772 strcmp(member_type, "file") == 0 && 6773 member_size_string != NULL){ 6774 // Extract the file into a buffer. 6775 char *endptr; 6776 member_size = strtoul(member_size_string, &endptr, 10); 6777 if (*endptr == '\0' && member_size != 0) { 6778 char *buffer; 6779 if (xar_extract_tobuffersz(xar, xf, &buffer, &member_size) == 0) { 6780 #if 0 // Useful for debugging. 6781 outs() << "xar member: " << member_name << " extracted\n"; 6782 #endif 6783 // Set the XarMemberName we want to see printed in the header. 6784 std::string OldXarMemberName; 6785 // If XarMemberName is already set this is nested. So 6786 // save the old name and create the nested name. 6787 if (!XarMemberName.empty()) { 6788 OldXarMemberName = XarMemberName; 6789 XarMemberName = 6790 (Twine("[") + XarMemberName + "]" + member_name).str(); 6791 } else { 6792 OldXarMemberName = ""; 6793 XarMemberName = member_name; 6794 } 6795 // See if this is could be a xar file (nested). 6796 if (member_size >= sizeof(struct xar_header)) { 6797 #if 0 // Useful for debugging. 6798 outs() << "could be a xar file: " << member_name << "\n"; 6799 #endif 6800 memcpy((char *)&XarHeader, buffer, sizeof(struct xar_header)); 6801 if (sys::IsLittleEndianHost) 6802 swapStruct(XarHeader); 6803 if (XarHeader.magic == XAR_HEADER_MAGIC) 6804 DumpBitcodeSection(O, buffer, member_size, verbose, 6805 PrintXarHeader, PrintXarFileHeaders, 6806 XarMemberName); 6807 } 6808 XarMemberName = OldXarMemberName; 6809 delete buffer; 6810 } 6811 } 6812 } 6813 } 6814 } 6815 #endif // defined(HAVE_LIBXAR) 6816 6817 static void printObjcMetaData(MachOObjectFile *O, bool verbose) { 6818 if (O->is64Bit()) 6819 printObjc2_64bit_MetaData(O, verbose); 6820 else { 6821 MachO::mach_header H; 6822 H = O->getHeader(); 6823 if (H.cputype == MachO::CPU_TYPE_ARM) 6824 printObjc2_32bit_MetaData(O, verbose); 6825 else { 6826 // This is the 32-bit non-arm cputype case. Which is normally 6827 // the first Objective-C ABI. But it may be the case of a 6828 // binary for the iOS simulator which is the second Objective-C 6829 // ABI. In that case printObjc1_32bit_MetaData() will determine that 6830 // and return false. 6831 if (!printObjc1_32bit_MetaData(O, verbose)) 6832 printObjc2_32bit_MetaData(O, verbose); 6833 } 6834 } 6835 } 6836 6837 // GuessLiteralPointer returns a string which for the item in the Mach-O file 6838 // for the address passed in as ReferenceValue for printing as a comment with 6839 // the instruction and also returns the corresponding type of that item 6840 // indirectly through ReferenceType. 6841 // 6842 // If ReferenceValue is an address of literal cstring then a pointer to the 6843 // cstring is returned and ReferenceType is set to 6844 // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr . 6845 // 6846 // If ReferenceValue is an address of an Objective-C CFString, Selector ref or 6847 // Class ref that name is returned and the ReferenceType is set accordingly. 6848 // 6849 // Lastly, literals which are Symbol address in a literal pool are looked for 6850 // and if found the symbol name is returned and ReferenceType is set to 6851 // LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr . 6852 // 6853 // If there is no item in the Mach-O file for the address passed in as 6854 // ReferenceValue nullptr is returned and ReferenceType is unchanged. 6855 static const char *GuessLiteralPointer(uint64_t ReferenceValue, 6856 uint64_t ReferencePC, 6857 uint64_t *ReferenceType, 6858 struct DisassembleInfo *info) { 6859 // First see if there is an external relocation entry at the ReferencePC. 6860 if (info->O->getHeader().filetype == MachO::MH_OBJECT) { 6861 uint64_t sect_addr = info->S.getAddress(); 6862 uint64_t sect_offset = ReferencePC - sect_addr; 6863 bool reloc_found = false; 6864 DataRefImpl Rel; 6865 MachO::any_relocation_info RE; 6866 bool isExtern = false; 6867 SymbolRef Symbol; 6868 for (const RelocationRef &Reloc : info->S.relocations()) { 6869 uint64_t RelocOffset = Reloc.getOffset(); 6870 if (RelocOffset == sect_offset) { 6871 Rel = Reloc.getRawDataRefImpl(); 6872 RE = info->O->getRelocation(Rel); 6873 if (info->O->isRelocationScattered(RE)) 6874 continue; 6875 isExtern = info->O->getPlainRelocationExternal(RE); 6876 if (isExtern) { 6877 symbol_iterator RelocSym = Reloc.getSymbol(); 6878 Symbol = *RelocSym; 6879 } 6880 reloc_found = true; 6881 break; 6882 } 6883 } 6884 // If there is an external relocation entry for a symbol in a section 6885 // then used that symbol's value for the value of the reference. 6886 if (reloc_found && isExtern) { 6887 if (info->O->getAnyRelocationPCRel(RE)) { 6888 unsigned Type = info->O->getAnyRelocationType(RE); 6889 if (Type == MachO::X86_64_RELOC_SIGNED) { 6890 ReferenceValue = Symbol.getValue(); 6891 } 6892 } 6893 } 6894 } 6895 6896 // Look for literals such as Objective-C CFStrings refs, Selector refs, 6897 // Message refs and Class refs. 6898 bool classref, selref, msgref, cfstring; 6899 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref, 6900 selref, msgref, cfstring); 6901 if (classref && pointer_value == 0) { 6902 // Note the ReferenceValue is a pointer into the __objc_classrefs section. 6903 // And the pointer_value in that section is typically zero as it will be 6904 // set by dyld as part of the "bind information". 6905 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info); 6906 if (name != nullptr) { 6907 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref; 6908 const char *class_name = strrchr(name, '$'); 6909 if (class_name != nullptr && class_name[1] == '_' && 6910 class_name[2] != '\0') { 6911 info->class_name = class_name + 2; 6912 return name; 6913 } 6914 } 6915 } 6916 6917 if (classref) { 6918 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref; 6919 const char *name = 6920 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info); 6921 if (name != nullptr) 6922 info->class_name = name; 6923 else 6924 name = "bad class ref"; 6925 return name; 6926 } 6927 6928 if (cfstring) { 6929 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref; 6930 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info); 6931 return name; 6932 } 6933 6934 if (selref && pointer_value == 0) 6935 pointer_value = get_objc2_64bit_selref(ReferenceValue, info); 6936 6937 if (pointer_value != 0) 6938 ReferenceValue = pointer_value; 6939 6940 const char *name = GuessCstringPointer(ReferenceValue, info); 6941 if (name) { 6942 if (pointer_value != 0 && selref) { 6943 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref; 6944 info->selector_name = name; 6945 } else if (pointer_value != 0 && msgref) { 6946 info->class_name = nullptr; 6947 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref; 6948 info->selector_name = name; 6949 } else 6950 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr; 6951 return name; 6952 } 6953 6954 // Lastly look for an indirect symbol with this ReferenceValue which is in 6955 // a literal pool. If found return that symbol name. 6956 name = GuessIndirectSymbol(ReferenceValue, info); 6957 if (name) { 6958 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr; 6959 return name; 6960 } 6961 6962 return nullptr; 6963 } 6964 6965 // SymbolizerSymbolLookUp is the symbol lookup function passed when creating 6966 // the Symbolizer. It looks up the ReferenceValue using the info passed via the 6967 // pointer to the struct DisassembleInfo that was passed when MCSymbolizer 6968 // is created and returns the symbol name that matches the ReferenceValue or 6969 // nullptr if none. The ReferenceType is passed in for the IN type of 6970 // reference the instruction is making from the values in defined in the header 6971 // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific 6972 // Out type and the ReferenceName will also be set which is added as a comment 6973 // to the disassembled instruction. 6974 // 6975 // If the symbol name is a C++ mangled name then the demangled name is 6976 // returned through ReferenceName and ReferenceType is set to 6977 // LLVMDisassembler_ReferenceType_DeMangled_Name . 6978 // 6979 // When this is called to get a symbol name for a branch target then the 6980 // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then 6981 // SymbolValue will be looked for in the indirect symbol table to determine if 6982 // it is an address for a symbol stub. If so then the symbol name for that 6983 // stub is returned indirectly through ReferenceName and then ReferenceType is 6984 // set to LLVMDisassembler_ReferenceType_Out_SymbolStub. 6985 // 6986 // When this is called with an value loaded via a PC relative load then 6987 // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the 6988 // SymbolValue is checked to be an address of literal pointer, symbol pointer, 6989 // or an Objective-C meta data reference. If so the output ReferenceType is 6990 // set to correspond to that as well as setting the ReferenceName. 6991 static const char *SymbolizerSymbolLookUp(void *DisInfo, 6992 uint64_t ReferenceValue, 6993 uint64_t *ReferenceType, 6994 uint64_t ReferencePC, 6995 const char **ReferenceName) { 6996 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo; 6997 // If no verbose symbolic information is wanted then just return nullptr. 6998 if (!info->verbose) { 6999 *ReferenceName = nullptr; 7000 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7001 return nullptr; 7002 } 7003 7004 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap); 7005 7006 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) { 7007 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info); 7008 if (*ReferenceName != nullptr) { 7009 method_reference(info, ReferenceType, ReferenceName); 7010 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message) 7011 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub; 7012 } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) { 7013 if (info->demangled_name != nullptr) 7014 free(info->demangled_name); 7015 int status; 7016 info->demangled_name = 7017 itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status); 7018 if (info->demangled_name != nullptr) { 7019 *ReferenceName = info->demangled_name; 7020 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name; 7021 } else 7022 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7023 } else 7024 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7025 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) { 7026 *ReferenceName = 7027 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info); 7028 if (*ReferenceName) 7029 method_reference(info, ReferenceType, ReferenceName); 7030 else 7031 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7032 // If this is arm64 and the reference is an adrp instruction save the 7033 // instruction, passed in ReferenceValue and the address of the instruction 7034 // for use later if we see and add immediate instruction. 7035 } else if (info->O->getArch() == Triple::aarch64 && 7036 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) { 7037 info->adrp_inst = ReferenceValue; 7038 info->adrp_addr = ReferencePC; 7039 SymbolName = nullptr; 7040 *ReferenceName = nullptr; 7041 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7042 // If this is arm64 and reference is an add immediate instruction and we 7043 // have 7044 // seen an adrp instruction just before it and the adrp's Xd register 7045 // matches 7046 // this add's Xn register reconstruct the value being referenced and look to 7047 // see if it is a literal pointer. Note the add immediate instruction is 7048 // passed in ReferenceValue. 7049 } else if (info->O->getArch() == Triple::aarch64 && 7050 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri && 7051 ReferencePC - 4 == info->adrp_addr && 7052 (info->adrp_inst & 0x9f000000) == 0x90000000 && 7053 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) { 7054 uint32_t addxri_inst; 7055 uint64_t adrp_imm, addxri_imm; 7056 7057 adrp_imm = 7058 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3); 7059 if (info->adrp_inst & 0x0200000) 7060 adrp_imm |= 0xfffffffffc000000LL; 7061 7062 addxri_inst = ReferenceValue; 7063 addxri_imm = (addxri_inst >> 10) & 0xfff; 7064 if (((addxri_inst >> 22) & 0x3) == 1) 7065 addxri_imm <<= 12; 7066 7067 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) + 7068 (adrp_imm << 12) + addxri_imm; 7069 7070 *ReferenceName = 7071 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info); 7072 if (*ReferenceName == nullptr) 7073 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7074 // If this is arm64 and the reference is a load register instruction and we 7075 // have seen an adrp instruction just before it and the adrp's Xd register 7076 // matches this add's Xn register reconstruct the value being referenced and 7077 // look to see if it is a literal pointer. Note the load register 7078 // instruction is passed in ReferenceValue. 7079 } else if (info->O->getArch() == Triple::aarch64 && 7080 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui && 7081 ReferencePC - 4 == info->adrp_addr && 7082 (info->adrp_inst & 0x9f000000) == 0x90000000 && 7083 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) { 7084 uint32_t ldrxui_inst; 7085 uint64_t adrp_imm, ldrxui_imm; 7086 7087 adrp_imm = 7088 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3); 7089 if (info->adrp_inst & 0x0200000) 7090 adrp_imm |= 0xfffffffffc000000LL; 7091 7092 ldrxui_inst = ReferenceValue; 7093 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff; 7094 7095 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) + 7096 (adrp_imm << 12) + (ldrxui_imm << 3); 7097 7098 *ReferenceName = 7099 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info); 7100 if (*ReferenceName == nullptr) 7101 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7102 } 7103 // If this arm64 and is an load register (PC-relative) instruction the 7104 // ReferenceValue is the PC plus the immediate value. 7105 else if (info->O->getArch() == Triple::aarch64 && 7106 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl || 7107 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) { 7108 *ReferenceName = 7109 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info); 7110 if (*ReferenceName == nullptr) 7111 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7112 } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) { 7113 if (info->demangled_name != nullptr) 7114 free(info->demangled_name); 7115 int status; 7116 info->demangled_name = 7117 itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status); 7118 if (info->demangled_name != nullptr) { 7119 *ReferenceName = info->demangled_name; 7120 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name; 7121 } 7122 } 7123 else { 7124 *ReferenceName = nullptr; 7125 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7126 } 7127 7128 return SymbolName; 7129 } 7130 7131 /// Emits the comments that are stored in the CommentStream. 7132 /// Each comment in the CommentStream must end with a newline. 7133 static void emitComments(raw_svector_ostream &CommentStream, 7134 SmallString<128> &CommentsToEmit, 7135 formatted_raw_ostream &FormattedOS, 7136 const MCAsmInfo &MAI) { 7137 // Flush the stream before taking its content. 7138 StringRef Comments = CommentsToEmit.str(); 7139 // Get the default information for printing a comment. 7140 StringRef CommentBegin = MAI.getCommentString(); 7141 unsigned CommentColumn = MAI.getCommentColumn(); 7142 bool IsFirst = true; 7143 while (!Comments.empty()) { 7144 if (!IsFirst) 7145 FormattedOS << '\n'; 7146 // Emit a line of comments. 7147 FormattedOS.PadToColumn(CommentColumn); 7148 size_t Position = Comments.find('\n'); 7149 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position); 7150 // Move after the newline character. 7151 Comments = Comments.substr(Position + 1); 7152 IsFirst = false; 7153 } 7154 FormattedOS.flush(); 7155 7156 // Tell the comment stream that the vector changed underneath it. 7157 CommentsToEmit.clear(); 7158 } 7159 7160 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, 7161 StringRef DisSegName, StringRef DisSectName) { 7162 const char *McpuDefault = nullptr; 7163 const Target *ThumbTarget = nullptr; 7164 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget); 7165 if (!TheTarget) { 7166 // GetTarget prints out stuff. 7167 return; 7168 } 7169 std::string MachOMCPU; 7170 if (MCPU.empty() && McpuDefault) 7171 MachOMCPU = McpuDefault; 7172 else 7173 MachOMCPU = MCPU; 7174 7175 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo()); 7176 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo; 7177 if (ThumbTarget) 7178 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo()); 7179 7180 // Package up features to be passed to target/subtarget 7181 std::string FeaturesStr; 7182 if (!MAttrs.empty()) { 7183 SubtargetFeatures Features; 7184 for (unsigned i = 0; i != MAttrs.size(); ++i) 7185 Features.AddFeature(MAttrs[i]); 7186 FeaturesStr = Features.getString(); 7187 } 7188 7189 // Set up disassembler. 7190 std::unique_ptr<const MCRegisterInfo> MRI( 7191 TheTarget->createMCRegInfo(TripleName)); 7192 std::unique_ptr<const MCAsmInfo> AsmInfo( 7193 TheTarget->createMCAsmInfo(*MRI, TripleName)); 7194 std::unique_ptr<const MCSubtargetInfo> STI( 7195 TheTarget->createMCSubtargetInfo(TripleName, MachOMCPU, FeaturesStr)); 7196 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr); 7197 std::unique_ptr<MCDisassembler> DisAsm( 7198 TheTarget->createMCDisassembler(*STI, Ctx)); 7199 std::unique_ptr<MCSymbolizer> Symbolizer; 7200 struct DisassembleInfo SymbolizerInfo(nullptr, nullptr, nullptr, false); 7201 std::unique_ptr<MCRelocationInfo> RelInfo( 7202 TheTarget->createMCRelocationInfo(TripleName, Ctx)); 7203 if (RelInfo) { 7204 Symbolizer.reset(TheTarget->createMCSymbolizer( 7205 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp, 7206 &SymbolizerInfo, &Ctx, std::move(RelInfo))); 7207 DisAsm->setSymbolizer(std::move(Symbolizer)); 7208 } 7209 int AsmPrinterVariant = AsmInfo->getAssemblerDialect(); 7210 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter( 7211 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI)); 7212 // Set the display preference for hex vs. decimal immediates. 7213 IP->setPrintImmHex(PrintImmHex); 7214 // Comment stream and backing vector. 7215 SmallString<128> CommentsToEmit; 7216 raw_svector_ostream CommentStream(CommentsToEmit); 7217 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that 7218 // if it is done then arm64 comments for string literals don't get printed 7219 // and some constant get printed instead and not setting it causes intel 7220 // (32-bit and 64-bit) comments printed with different spacing before the 7221 // comment causing different diffs with the 'C' disassembler library API. 7222 // IP->setCommentStream(CommentStream); 7223 7224 if (!AsmInfo || !STI || !DisAsm || !IP) { 7225 WithColor::error(errs(), "llvm-objdump") 7226 << "couldn't initialize disassembler for target " << TripleName << '\n'; 7227 return; 7228 } 7229 7230 // Set up separate thumb disassembler if needed. 7231 std::unique_ptr<const MCRegisterInfo> ThumbMRI; 7232 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo; 7233 std::unique_ptr<const MCSubtargetInfo> ThumbSTI; 7234 std::unique_ptr<MCDisassembler> ThumbDisAsm; 7235 std::unique_ptr<MCInstPrinter> ThumbIP; 7236 std::unique_ptr<MCContext> ThumbCtx; 7237 std::unique_ptr<MCSymbolizer> ThumbSymbolizer; 7238 struct DisassembleInfo ThumbSymbolizerInfo(nullptr, nullptr, nullptr, false); 7239 std::unique_ptr<MCRelocationInfo> ThumbRelInfo; 7240 if (ThumbTarget) { 7241 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName)); 7242 ThumbAsmInfo.reset( 7243 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName)); 7244 ThumbSTI.reset( 7245 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MachOMCPU, 7246 FeaturesStr)); 7247 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr)); 7248 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx)); 7249 MCContext *PtrThumbCtx = ThumbCtx.get(); 7250 ThumbRelInfo.reset( 7251 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx)); 7252 if (ThumbRelInfo) { 7253 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer( 7254 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp, 7255 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo))); 7256 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer)); 7257 } 7258 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect(); 7259 ThumbIP.reset(ThumbTarget->createMCInstPrinter( 7260 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo, 7261 *ThumbInstrInfo, *ThumbMRI)); 7262 // Set the display preference for hex vs. decimal immediates. 7263 ThumbIP->setPrintImmHex(PrintImmHex); 7264 } 7265 7266 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) { 7267 WithColor::error(errs(), "llvm-objdump") 7268 << "couldn't initialize disassembler for target " << ThumbTripleName 7269 << '\n'; 7270 return; 7271 } 7272 7273 MachO::mach_header Header = MachOOF->getHeader(); 7274 7275 // FIXME: Using the -cfg command line option, this code used to be able to 7276 // annotate relocations with the referenced symbol's name, and if this was 7277 // inside a __[cf]string section, the data it points to. This is now replaced 7278 // by the upcoming MCSymbolizer, which needs the appropriate setup done above. 7279 std::vector<SectionRef> Sections; 7280 std::vector<SymbolRef> Symbols; 7281 SmallVector<uint64_t, 8> FoundFns; 7282 uint64_t BaseSegmentAddress = 0; 7283 7284 getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns, 7285 BaseSegmentAddress); 7286 7287 // Sort the symbols by address, just in case they didn't come in that way. 7288 llvm::sort(Symbols, SymbolSorter()); 7289 7290 // Build a data in code table that is sorted on by the address of each entry. 7291 uint64_t BaseAddress = 0; 7292 if (Header.filetype == MachO::MH_OBJECT) 7293 BaseAddress = Sections[0].getAddress(); 7294 else 7295 BaseAddress = BaseSegmentAddress; 7296 DiceTable Dices; 7297 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices(); 7298 DI != DE; ++DI) { 7299 uint32_t Offset; 7300 DI->getOffset(Offset); 7301 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI)); 7302 } 7303 array_pod_sort(Dices.begin(), Dices.end()); 7304 7305 #ifndef NDEBUG 7306 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls(); 7307 #else 7308 raw_ostream &DebugOut = nulls(); 7309 #endif 7310 7311 // Try to find debug info and set up the DIContext for it. 7312 std::unique_ptr<DIContext> diContext; 7313 std::unique_ptr<Binary> DSYMBinary; 7314 std::unique_ptr<MemoryBuffer> DSYMBuf; 7315 if (UseDbg) { 7316 ObjectFile *DbgObj = MachOOF; 7317 7318 // A separate DSym file path was specified, parse it as a macho file, 7319 // get the sections and supply it to the section name parsing machinery. 7320 if (!DSYMFile.empty()) { 7321 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr = 7322 MemoryBuffer::getFileOrSTDIN(DSYMFile); 7323 if (std::error_code EC = BufOrErr.getError()) { 7324 report_error(errorCodeToError(EC), DSYMFile); 7325 return; 7326 } 7327 7328 // We need to keep the file alive, because we're replacing DbgObj with it. 7329 DSYMBuf = std::move(BufOrErr.get()); 7330 7331 Expected<std::unique_ptr<Binary>> BinaryOrErr = 7332 createBinary(DSYMBuf.get()->getMemBufferRef()); 7333 if (!BinaryOrErr) { 7334 report_error(BinaryOrErr.takeError(), DSYMFile); 7335 return; 7336 } 7337 7338 // We need to keep the Binary elive with the buffer 7339 DSYMBinary = std::move(BinaryOrErr.get()); 7340 7341 if (ObjectFile *O = dyn_cast<ObjectFile>(DSYMBinary.get())) { 7342 // this is a Mach-O object file, use it 7343 if (MachOObjectFile *MachDSYM = dyn_cast<MachOObjectFile>(&*O)) { 7344 DbgObj = MachDSYM; 7345 } 7346 else { 7347 WithColor::error(errs(), "llvm-objdump") 7348 << DSYMFile << " is not a Mach-O file type.\n"; 7349 return; 7350 } 7351 } 7352 else if (auto UB = dyn_cast<MachOUniversalBinary>(DSYMBinary.get())){ 7353 // this is a Universal Binary, find a Mach-O for this architecture 7354 uint32_t CPUType, CPUSubType; 7355 const char *ArchFlag; 7356 if (MachOOF->is64Bit()) { 7357 const MachO::mach_header_64 H_64 = MachOOF->getHeader64(); 7358 CPUType = H_64.cputype; 7359 CPUSubType = H_64.cpusubtype; 7360 } else { 7361 const MachO::mach_header H = MachOOF->getHeader(); 7362 CPUType = H.cputype; 7363 CPUSubType = H.cpusubtype; 7364 } 7365 Triple T = MachOObjectFile::getArchTriple(CPUType, CPUSubType, nullptr, 7366 &ArchFlag); 7367 Expected<std::unique_ptr<MachOObjectFile>> MachDSYM = 7368 UB->getObjectForArch(ArchFlag); 7369 if (!MachDSYM) { 7370 report_error(MachDSYM.takeError(), DSYMFile); 7371 return; 7372 } 7373 7374 // We need to keep the Binary elive with the buffer 7375 DbgObj = &*MachDSYM.get(); 7376 DSYMBinary = std::move(*MachDSYM); 7377 } 7378 else { 7379 WithColor::error(errs(), "llvm-objdump") 7380 << DSYMFile << " is not a Mach-O or Universal file type.\n"; 7381 return; 7382 } 7383 } 7384 7385 // Setup the DIContext 7386 diContext = DWARFContext::create(*DbgObj); 7387 } 7388 7389 if (FilterSections.empty()) 7390 outs() << "(" << DisSegName << "," << DisSectName << ") section\n"; 7391 7392 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) { 7393 Expected<StringRef> SecNameOrErr = Sections[SectIdx].getName(); 7394 if (!SecNameOrErr) { 7395 consumeError(SecNameOrErr.takeError()); 7396 continue; 7397 } 7398 if (*SecNameOrErr != DisSectName) 7399 continue; 7400 7401 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl(); 7402 7403 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR); 7404 if (SegmentName != DisSegName) 7405 continue; 7406 7407 StringRef BytesStr = 7408 unwrapOrError(Sections[SectIdx].getContents(), Filename); 7409 ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(BytesStr); 7410 uint64_t SectAddress = Sections[SectIdx].getAddress(); 7411 7412 bool symbolTableWorked = false; 7413 7414 // Create a map of symbol addresses to symbol names for use by 7415 // the SymbolizerSymbolLookUp() routine. 7416 SymbolAddressMap AddrMap; 7417 bool DisSymNameFound = false; 7418 for (const SymbolRef &Symbol : MachOOF->symbols()) { 7419 SymbolRef::Type ST = 7420 unwrapOrError(Symbol.getType(), MachOOF->getFileName()); 7421 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data || 7422 ST == SymbolRef::ST_Other) { 7423 uint64_t Address = Symbol.getValue(); 7424 StringRef SymName = 7425 unwrapOrError(Symbol.getName(), MachOOF->getFileName()); 7426 AddrMap[Address] = SymName; 7427 if (!DisSymName.empty() && DisSymName == SymName) 7428 DisSymNameFound = true; 7429 } 7430 } 7431 if (!DisSymName.empty() && !DisSymNameFound) { 7432 outs() << "Can't find -dis-symname: " << DisSymName << "\n"; 7433 return; 7434 } 7435 // Set up the block of info used by the Symbolizer call backs. 7436 SymbolizerInfo.verbose = !NoSymbolicOperands; 7437 SymbolizerInfo.O = MachOOF; 7438 SymbolizerInfo.S = Sections[SectIdx]; 7439 SymbolizerInfo.AddrMap = &AddrMap; 7440 SymbolizerInfo.Sections = &Sections; 7441 // Same for the ThumbSymbolizer 7442 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands; 7443 ThumbSymbolizerInfo.O = MachOOF; 7444 ThumbSymbolizerInfo.S = Sections[SectIdx]; 7445 ThumbSymbolizerInfo.AddrMap = &AddrMap; 7446 ThumbSymbolizerInfo.Sections = &Sections; 7447 7448 unsigned int Arch = MachOOF->getArch(); 7449 7450 // Skip all symbols if this is a stubs file. 7451 if (Bytes.empty()) 7452 return; 7453 7454 // If the section has symbols but no symbol at the start of the section 7455 // these are used to make sure the bytes before the first symbol are 7456 // disassembled. 7457 bool FirstSymbol = true; 7458 bool FirstSymbolAtSectionStart = true; 7459 7460 // Disassemble symbol by symbol. 7461 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) { 7462 StringRef SymName = 7463 unwrapOrError(Symbols[SymIdx].getName(), MachOOF->getFileName()); 7464 SymbolRef::Type ST = 7465 unwrapOrError(Symbols[SymIdx].getType(), MachOOF->getFileName()); 7466 if (ST != SymbolRef::ST_Function && ST != SymbolRef::ST_Data) 7467 continue; 7468 7469 // Make sure the symbol is defined in this section. 7470 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]); 7471 if (!containsSym) { 7472 if (!DisSymName.empty() && DisSymName == SymName) { 7473 outs() << "-dis-symname: " << DisSymName << " not in the section\n"; 7474 return; 7475 } 7476 continue; 7477 } 7478 // The __mh_execute_header is special and we need to deal with that fact 7479 // this symbol is before the start of the (__TEXT,__text) section and at the 7480 // address of the start of the __TEXT segment. This is because this symbol 7481 // is an N_SECT symbol in the (__TEXT,__text) but its address is before the 7482 // start of the section in a standard MH_EXECUTE filetype. 7483 if (!DisSymName.empty() && DisSymName == "__mh_execute_header") { 7484 outs() << "-dis-symname: __mh_execute_header not in any section\n"; 7485 return; 7486 } 7487 // When this code is trying to disassemble a symbol at a time and in the 7488 // case there is only the __mh_execute_header symbol left as in a stripped 7489 // executable, we need to deal with this by ignoring this symbol so the 7490 // whole section is disassembled and this symbol is then not displayed. 7491 if (SymName == "__mh_execute_header" || SymName == "__mh_dylib_header" || 7492 SymName == "__mh_bundle_header" || SymName == "__mh_object_header" || 7493 SymName == "__mh_preload_header" || SymName == "__mh_dylinker_header") 7494 continue; 7495 7496 // If we are only disassembling one symbol see if this is that symbol. 7497 if (!DisSymName.empty() && DisSymName != SymName) 7498 continue; 7499 7500 // Start at the address of the symbol relative to the section's address. 7501 uint64_t SectSize = Sections[SectIdx].getSize(); 7502 uint64_t Start = Symbols[SymIdx].getValue(); 7503 uint64_t SectionAddress = Sections[SectIdx].getAddress(); 7504 Start -= SectionAddress; 7505 7506 if (Start > SectSize) { 7507 outs() << "section data ends, " << SymName 7508 << " lies outside valid range\n"; 7509 return; 7510 } 7511 7512 // Stop disassembling either at the beginning of the next symbol or at 7513 // the end of the section. 7514 bool containsNextSym = false; 7515 uint64_t NextSym = 0; 7516 uint64_t NextSymIdx = SymIdx + 1; 7517 while (Symbols.size() > NextSymIdx) { 7518 SymbolRef::Type NextSymType = unwrapOrError( 7519 Symbols[NextSymIdx].getType(), MachOOF->getFileName()); 7520 if (NextSymType == SymbolRef::ST_Function) { 7521 containsNextSym = 7522 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]); 7523 NextSym = Symbols[NextSymIdx].getValue(); 7524 NextSym -= SectionAddress; 7525 break; 7526 } 7527 ++NextSymIdx; 7528 } 7529 7530 uint64_t End = containsNextSym ? std::min(NextSym, SectSize) : SectSize; 7531 uint64_t Size; 7532 7533 symbolTableWorked = true; 7534 7535 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl(); 7536 bool IsThumb = MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb; 7537 7538 // We only need the dedicated Thumb target if there's a real choice 7539 // (i.e. we're not targeting M-class) and the function is Thumb. 7540 bool UseThumbTarget = IsThumb && ThumbTarget; 7541 7542 // If we are not specifying a symbol to start disassembly with and this 7543 // is the first symbol in the section but not at the start of the section 7544 // then move the disassembly index to the start of the section and 7545 // don't print the symbol name just yet. This is so the bytes before the 7546 // first symbol are disassembled. 7547 uint64_t SymbolStart = Start; 7548 if (DisSymName.empty() && FirstSymbol && Start != 0) { 7549 FirstSymbolAtSectionStart = false; 7550 Start = 0; 7551 } 7552 else 7553 outs() << SymName << ":\n"; 7554 7555 DILineInfo lastLine; 7556 for (uint64_t Index = Start; Index < End; Index += Size) { 7557 MCInst Inst; 7558 7559 // If this is the first symbol in the section and it was not at the 7560 // start of the section, see if we are at its Index now and if so print 7561 // the symbol name. 7562 if (FirstSymbol && !FirstSymbolAtSectionStart && Index == SymbolStart) 7563 outs() << SymName << ":\n"; 7564 7565 uint64_t PC = SectAddress + Index; 7566 if (!NoLeadingAddr) { 7567 if (FullLeadingAddr) { 7568 if (MachOOF->is64Bit()) 7569 outs() << format("%016" PRIx64, PC); 7570 else 7571 outs() << format("%08" PRIx64, PC); 7572 } else { 7573 outs() << format("%8" PRIx64 ":", PC); 7574 } 7575 } 7576 if (!NoShowRawInsn || Arch == Triple::arm) 7577 outs() << "\t"; 7578 7579 if (DumpAndSkipDataInCode(PC, Bytes.data() + Index, Dices, Size)) 7580 continue; 7581 7582 SmallVector<char, 64> AnnotationsBytes; 7583 raw_svector_ostream Annotations(AnnotationsBytes); 7584 7585 bool gotInst; 7586 if (UseThumbTarget) 7587 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index), 7588 PC, DebugOut, Annotations); 7589 else 7590 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC, 7591 DebugOut, Annotations); 7592 if (gotInst) { 7593 if (!NoShowRawInsn || Arch == Triple::arm) { 7594 dumpBytes(makeArrayRef(Bytes.data() + Index, Size), outs()); 7595 } 7596 formatted_raw_ostream FormattedOS(outs()); 7597 StringRef AnnotationsStr = Annotations.str(); 7598 if (UseThumbTarget) 7599 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI); 7600 else 7601 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI); 7602 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo); 7603 7604 // Print debug info. 7605 if (diContext) { 7606 DILineInfo dli = diContext->getLineInfoForAddress({PC, SectIdx}); 7607 // Print valid line info if it changed. 7608 if (dli != lastLine && dli.Line != 0) 7609 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':' 7610 << dli.Column; 7611 lastLine = dli; 7612 } 7613 outs() << "\n"; 7614 } else { 7615 unsigned int Arch = MachOOF->getArch(); 7616 if (Arch == Triple::x86_64 || Arch == Triple::x86) { 7617 outs() << format("\t.byte 0x%02x #bad opcode\n", 7618 *(Bytes.data() + Index) & 0xff); 7619 Size = 1; // skip exactly one illegible byte and move on. 7620 } else if (Arch == Triple::aarch64 || 7621 (Arch == Triple::arm && !IsThumb)) { 7622 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) | 7623 (*(Bytes.data() + Index + 1) & 0xff) << 8 | 7624 (*(Bytes.data() + Index + 2) & 0xff) << 16 | 7625 (*(Bytes.data() + Index + 3) & 0xff) << 24; 7626 outs() << format("\t.long\t0x%08x\n", opcode); 7627 Size = 4; 7628 } else if (Arch == Triple::arm) { 7629 assert(IsThumb && "ARM mode should have been dealt with above"); 7630 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) | 7631 (*(Bytes.data() + Index + 1) & 0xff) << 8; 7632 outs() << format("\t.short\t0x%04x\n", opcode); 7633 Size = 2; 7634 } else{ 7635 WithColor::warning(errs(), "llvm-objdump") 7636 << "invalid instruction encoding\n"; 7637 if (Size == 0) 7638 Size = 1; // skip illegible bytes 7639 } 7640 } 7641 } 7642 // Now that we are done disassembled the first symbol set the bool that 7643 // were doing this to false. 7644 FirstSymbol = false; 7645 } 7646 if (!symbolTableWorked) { 7647 // Reading the symbol table didn't work, disassemble the whole section. 7648 uint64_t SectAddress = Sections[SectIdx].getAddress(); 7649 uint64_t SectSize = Sections[SectIdx].getSize(); 7650 uint64_t InstSize; 7651 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) { 7652 MCInst Inst; 7653 7654 uint64_t PC = SectAddress + Index; 7655 7656 if (DumpAndSkipDataInCode(PC, Bytes.data() + Index, Dices, InstSize)) 7657 continue; 7658 7659 SmallVector<char, 64> AnnotationsBytes; 7660 raw_svector_ostream Annotations(AnnotationsBytes); 7661 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC, 7662 DebugOut, Annotations)) { 7663 if (!NoLeadingAddr) { 7664 if (FullLeadingAddr) { 7665 if (MachOOF->is64Bit()) 7666 outs() << format("%016" PRIx64, PC); 7667 else 7668 outs() << format("%08" PRIx64, PC); 7669 } else { 7670 outs() << format("%8" PRIx64 ":", PC); 7671 } 7672 } 7673 if (!NoShowRawInsn || Arch == Triple::arm) { 7674 outs() << "\t"; 7675 dumpBytes(makeArrayRef(Bytes.data() + Index, InstSize), outs()); 7676 } 7677 StringRef AnnotationsStr = Annotations.str(); 7678 IP->printInst(&Inst, outs(), AnnotationsStr, *STI); 7679 outs() << "\n"; 7680 } else { 7681 unsigned int Arch = MachOOF->getArch(); 7682 if (Arch == Triple::x86_64 || Arch == Triple::x86) { 7683 outs() << format("\t.byte 0x%02x #bad opcode\n", 7684 *(Bytes.data() + Index) & 0xff); 7685 InstSize = 1; // skip exactly one illegible byte and move on. 7686 } else { 7687 WithColor::warning(errs(), "llvm-objdump") 7688 << "invalid instruction encoding\n"; 7689 if (InstSize == 0) 7690 InstSize = 1; // skip illegible bytes 7691 } 7692 } 7693 } 7694 } 7695 // The TripleName's need to be reset if we are called again for a different 7696 // archtecture. 7697 TripleName = ""; 7698 ThumbTripleName = ""; 7699 7700 if (SymbolizerInfo.demangled_name != nullptr) 7701 free(SymbolizerInfo.demangled_name); 7702 if (ThumbSymbolizerInfo.demangled_name != nullptr) 7703 free(ThumbSymbolizerInfo.demangled_name); 7704 } 7705 } 7706 7707 //===----------------------------------------------------------------------===// 7708 // __compact_unwind section dumping 7709 //===----------------------------------------------------------------------===// 7710 7711 namespace { 7712 7713 template <typename T> 7714 static uint64_t read(StringRef Contents, ptrdiff_t Offset) { 7715 using llvm::support::little; 7716 using llvm::support::unaligned; 7717 7718 if (Offset + sizeof(T) > Contents.size()) { 7719 outs() << "warning: attempt to read past end of buffer\n"; 7720 return T(); 7721 } 7722 7723 uint64_t Val = 7724 support::endian::read<T, little, unaligned>(Contents.data() + Offset); 7725 return Val; 7726 } 7727 7728 template <typename T> 7729 static uint64_t readNext(StringRef Contents, ptrdiff_t &Offset) { 7730 T Val = read<T>(Contents, Offset); 7731 Offset += sizeof(T); 7732 return Val; 7733 } 7734 7735 struct CompactUnwindEntry { 7736 uint32_t OffsetInSection; 7737 7738 uint64_t FunctionAddr; 7739 uint32_t Length; 7740 uint32_t CompactEncoding; 7741 uint64_t PersonalityAddr; 7742 uint64_t LSDAAddr; 7743 7744 RelocationRef FunctionReloc; 7745 RelocationRef PersonalityReloc; 7746 RelocationRef LSDAReloc; 7747 7748 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64) 7749 : OffsetInSection(Offset) { 7750 if (Is64) 7751 read<uint64_t>(Contents, Offset); 7752 else 7753 read<uint32_t>(Contents, Offset); 7754 } 7755 7756 private: 7757 template <typename UIntPtr> void read(StringRef Contents, ptrdiff_t Offset) { 7758 FunctionAddr = readNext<UIntPtr>(Contents, Offset); 7759 Length = readNext<uint32_t>(Contents, Offset); 7760 CompactEncoding = readNext<uint32_t>(Contents, Offset); 7761 PersonalityAddr = readNext<UIntPtr>(Contents, Offset); 7762 LSDAAddr = readNext<UIntPtr>(Contents, Offset); 7763 } 7764 }; 7765 } 7766 7767 /// Given a relocation from __compact_unwind, consisting of the RelocationRef 7768 /// and data being relocated, determine the best base Name and Addend to use for 7769 /// display purposes. 7770 /// 7771 /// 1. An Extern relocation will directly reference a symbol (and the data is 7772 /// then already an addend), so use that. 7773 /// 2. Otherwise the data is an offset in the object file's layout; try to find 7774 // a symbol before it in the same section, and use the offset from there. 7775 /// 3. Finally, if all that fails, fall back to an offset from the start of the 7776 /// referenced section. 7777 static void findUnwindRelocNameAddend(const MachOObjectFile *Obj, 7778 std::map<uint64_t, SymbolRef> &Symbols, 7779 const RelocationRef &Reloc, uint64_t Addr, 7780 StringRef &Name, uint64_t &Addend) { 7781 if (Reloc.getSymbol() != Obj->symbol_end()) { 7782 Name = unwrapOrError(Reloc.getSymbol()->getName(), Obj->getFileName()); 7783 Addend = Addr; 7784 return; 7785 } 7786 7787 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl()); 7788 SectionRef RelocSection = Obj->getAnyRelocationSection(RE); 7789 7790 uint64_t SectionAddr = RelocSection.getAddress(); 7791 7792 auto Sym = Symbols.upper_bound(Addr); 7793 if (Sym == Symbols.begin()) { 7794 // The first symbol in the object is after this reference, the best we can 7795 // do is section-relative notation. 7796 if (Expected<StringRef> NameOrErr = RelocSection.getName()) 7797 Name = *NameOrErr; 7798 else 7799 consumeError(NameOrErr.takeError()); 7800 7801 Addend = Addr - SectionAddr; 7802 return; 7803 } 7804 7805 // Go back one so that SymbolAddress <= Addr. 7806 --Sym; 7807 7808 section_iterator SymSection = 7809 unwrapOrError(Sym->second.getSection(), Obj->getFileName()); 7810 if (RelocSection == *SymSection) { 7811 // There's a valid symbol in the same section before this reference. 7812 Name = unwrapOrError(Sym->second.getName(), Obj->getFileName()); 7813 Addend = Addr - Sym->first; 7814 return; 7815 } 7816 7817 // There is a symbol before this reference, but it's in a different 7818 // section. Probably not helpful to mention it, so use the section name. 7819 if (Expected<StringRef> NameOrErr = RelocSection.getName()) 7820 Name = *NameOrErr; 7821 else 7822 consumeError(NameOrErr.takeError()); 7823 7824 Addend = Addr - SectionAddr; 7825 } 7826 7827 static void printUnwindRelocDest(const MachOObjectFile *Obj, 7828 std::map<uint64_t, SymbolRef> &Symbols, 7829 const RelocationRef &Reloc, uint64_t Addr) { 7830 StringRef Name; 7831 uint64_t Addend; 7832 7833 if (!Reloc.getObject()) 7834 return; 7835 7836 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend); 7837 7838 outs() << Name; 7839 if (Addend) 7840 outs() << " + " << format("0x%" PRIx64, Addend); 7841 } 7842 7843 static void 7844 printMachOCompactUnwindSection(const MachOObjectFile *Obj, 7845 std::map<uint64_t, SymbolRef> &Symbols, 7846 const SectionRef &CompactUnwind) { 7847 7848 if (!Obj->isLittleEndian()) { 7849 outs() << "Skipping big-endian __compact_unwind section\n"; 7850 return; 7851 } 7852 7853 bool Is64 = Obj->is64Bit(); 7854 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t); 7855 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t); 7856 7857 StringRef Contents = 7858 unwrapOrError(CompactUnwind.getContents(), Obj->getFileName()); 7859 SmallVector<CompactUnwindEntry, 4> CompactUnwinds; 7860 7861 // First populate the initial raw offsets, encodings and so on from the entry. 7862 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) { 7863 CompactUnwindEntry Entry(Contents, Offset, Is64); 7864 CompactUnwinds.push_back(Entry); 7865 } 7866 7867 // Next we need to look at the relocations to find out what objects are 7868 // actually being referred to. 7869 for (const RelocationRef &Reloc : CompactUnwind.relocations()) { 7870 uint64_t RelocAddress = Reloc.getOffset(); 7871 7872 uint32_t EntryIdx = RelocAddress / EntrySize; 7873 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize; 7874 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx]; 7875 7876 if (OffsetInEntry == 0) 7877 Entry.FunctionReloc = Reloc; 7878 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t)) 7879 Entry.PersonalityReloc = Reloc; 7880 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t)) 7881 Entry.LSDAReloc = Reloc; 7882 else { 7883 outs() << "Invalid relocation in __compact_unwind section\n"; 7884 return; 7885 } 7886 } 7887 7888 // Finally, we're ready to print the data we've gathered. 7889 outs() << "Contents of __compact_unwind section:\n"; 7890 for (auto &Entry : CompactUnwinds) { 7891 outs() << " Entry at offset " 7892 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n"; 7893 7894 // 1. Start of the region this entry applies to. 7895 outs() << " start: " << format("0x%" PRIx64, 7896 Entry.FunctionAddr) << ' '; 7897 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr); 7898 outs() << '\n'; 7899 7900 // 2. Length of the region this entry applies to. 7901 outs() << " length: " << format("0x%" PRIx32, Entry.Length) 7902 << '\n'; 7903 // 3. The 32-bit compact encoding. 7904 outs() << " compact encoding: " 7905 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n'; 7906 7907 // 4. The personality function, if present. 7908 if (Entry.PersonalityReloc.getObject()) { 7909 outs() << " personality function: " 7910 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' '; 7911 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc, 7912 Entry.PersonalityAddr); 7913 outs() << '\n'; 7914 } 7915 7916 // 5. This entry's language-specific data area. 7917 if (Entry.LSDAReloc.getObject()) { 7918 outs() << " LSDA: " << format("0x%" PRIx64, 7919 Entry.LSDAAddr) << ' '; 7920 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr); 7921 outs() << '\n'; 7922 } 7923 } 7924 } 7925 7926 //===----------------------------------------------------------------------===// 7927 // __unwind_info section dumping 7928 //===----------------------------------------------------------------------===// 7929 7930 static void printRegularSecondLevelUnwindPage(StringRef PageData) { 7931 ptrdiff_t Pos = 0; 7932 uint32_t Kind = readNext<uint32_t>(PageData, Pos); 7933 (void)Kind; 7934 assert(Kind == 2 && "kind for a regular 2nd level index should be 2"); 7935 7936 uint16_t EntriesStart = readNext<uint16_t>(PageData, Pos); 7937 uint16_t NumEntries = readNext<uint16_t>(PageData, Pos); 7938 7939 Pos = EntriesStart; 7940 for (unsigned i = 0; i < NumEntries; ++i) { 7941 uint32_t FunctionOffset = readNext<uint32_t>(PageData, Pos); 7942 uint32_t Encoding = readNext<uint32_t>(PageData, Pos); 7943 7944 outs() << " [" << i << "]: " 7945 << "function offset=" << format("0x%08" PRIx32, FunctionOffset) 7946 << ", " 7947 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n'; 7948 } 7949 } 7950 7951 static void printCompressedSecondLevelUnwindPage( 7952 StringRef PageData, uint32_t FunctionBase, 7953 const SmallVectorImpl<uint32_t> &CommonEncodings) { 7954 ptrdiff_t Pos = 0; 7955 uint32_t Kind = readNext<uint32_t>(PageData, Pos); 7956 (void)Kind; 7957 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3"); 7958 7959 uint16_t EntriesStart = readNext<uint16_t>(PageData, Pos); 7960 uint16_t NumEntries = readNext<uint16_t>(PageData, Pos); 7961 7962 uint16_t EncodingsStart = readNext<uint16_t>(PageData, Pos); 7963 readNext<uint16_t>(PageData, Pos); 7964 StringRef PageEncodings = PageData.substr(EncodingsStart, StringRef::npos); 7965 7966 Pos = EntriesStart; 7967 for (unsigned i = 0; i < NumEntries; ++i) { 7968 uint32_t Entry = readNext<uint32_t>(PageData, Pos); 7969 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff); 7970 uint32_t EncodingIdx = Entry >> 24; 7971 7972 uint32_t Encoding; 7973 if (EncodingIdx < CommonEncodings.size()) 7974 Encoding = CommonEncodings[EncodingIdx]; 7975 else 7976 Encoding = read<uint32_t>(PageEncodings, 7977 sizeof(uint32_t) * 7978 (EncodingIdx - CommonEncodings.size())); 7979 7980 outs() << " [" << i << "]: " 7981 << "function offset=" << format("0x%08" PRIx32, FunctionOffset) 7982 << ", " 7983 << "encoding[" << EncodingIdx 7984 << "]=" << format("0x%08" PRIx32, Encoding) << '\n'; 7985 } 7986 } 7987 7988 static void printMachOUnwindInfoSection(const MachOObjectFile *Obj, 7989 std::map<uint64_t, SymbolRef> &Symbols, 7990 const SectionRef &UnwindInfo) { 7991 7992 if (!Obj->isLittleEndian()) { 7993 outs() << "Skipping big-endian __unwind_info section\n"; 7994 return; 7995 } 7996 7997 outs() << "Contents of __unwind_info section:\n"; 7998 7999 StringRef Contents = 8000 unwrapOrError(UnwindInfo.getContents(), Obj->getFileName()); 8001 ptrdiff_t Pos = 0; 8002 8003 //===---------------------------------- 8004 // Section header 8005 //===---------------------------------- 8006 8007 uint32_t Version = readNext<uint32_t>(Contents, Pos); 8008 outs() << " Version: " 8009 << format("0x%" PRIx32, Version) << '\n'; 8010 if (Version != 1) { 8011 outs() << " Skipping section with unknown version\n"; 8012 return; 8013 } 8014 8015 uint32_t CommonEncodingsStart = readNext<uint32_t>(Contents, Pos); 8016 outs() << " Common encodings array section offset: " 8017 << format("0x%" PRIx32, CommonEncodingsStart) << '\n'; 8018 uint32_t NumCommonEncodings = readNext<uint32_t>(Contents, Pos); 8019 outs() << " Number of common encodings in array: " 8020 << format("0x%" PRIx32, NumCommonEncodings) << '\n'; 8021 8022 uint32_t PersonalitiesStart = readNext<uint32_t>(Contents, Pos); 8023 outs() << " Personality function array section offset: " 8024 << format("0x%" PRIx32, PersonalitiesStart) << '\n'; 8025 uint32_t NumPersonalities = readNext<uint32_t>(Contents, Pos); 8026 outs() << " Number of personality functions in array: " 8027 << format("0x%" PRIx32, NumPersonalities) << '\n'; 8028 8029 uint32_t IndicesStart = readNext<uint32_t>(Contents, Pos); 8030 outs() << " Index array section offset: " 8031 << format("0x%" PRIx32, IndicesStart) << '\n'; 8032 uint32_t NumIndices = readNext<uint32_t>(Contents, Pos); 8033 outs() << " Number of indices in array: " 8034 << format("0x%" PRIx32, NumIndices) << '\n'; 8035 8036 //===---------------------------------- 8037 // A shared list of common encodings 8038 //===---------------------------------- 8039 8040 // These occupy indices in the range [0, N] whenever an encoding is referenced 8041 // from a compressed 2nd level index table. In practice the linker only 8042 // creates ~128 of these, so that indices are available to embed encodings in 8043 // the 2nd level index. 8044 8045 SmallVector<uint32_t, 64> CommonEncodings; 8046 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n"; 8047 Pos = CommonEncodingsStart; 8048 for (unsigned i = 0; i < NumCommonEncodings; ++i) { 8049 uint32_t Encoding = readNext<uint32_t>(Contents, Pos); 8050 CommonEncodings.push_back(Encoding); 8051 8052 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding) 8053 << '\n'; 8054 } 8055 8056 //===---------------------------------- 8057 // Personality functions used in this executable 8058 //===---------------------------------- 8059 8060 // There should be only a handful of these (one per source language, 8061 // roughly). Particularly since they only get 2 bits in the compact encoding. 8062 8063 outs() << " Personality functions: (count = " << NumPersonalities << ")\n"; 8064 Pos = PersonalitiesStart; 8065 for (unsigned i = 0; i < NumPersonalities; ++i) { 8066 uint32_t PersonalityFn = readNext<uint32_t>(Contents, Pos); 8067 outs() << " personality[" << i + 1 8068 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n'; 8069 } 8070 8071 //===---------------------------------- 8072 // The level 1 index entries 8073 //===---------------------------------- 8074 8075 // These specify an approximate place to start searching for the more detailed 8076 // information, sorted by PC. 8077 8078 struct IndexEntry { 8079 uint32_t FunctionOffset; 8080 uint32_t SecondLevelPageStart; 8081 uint32_t LSDAStart; 8082 }; 8083 8084 SmallVector<IndexEntry, 4> IndexEntries; 8085 8086 outs() << " Top level indices: (count = " << NumIndices << ")\n"; 8087 Pos = IndicesStart; 8088 for (unsigned i = 0; i < NumIndices; ++i) { 8089 IndexEntry Entry; 8090 8091 Entry.FunctionOffset = readNext<uint32_t>(Contents, Pos); 8092 Entry.SecondLevelPageStart = readNext<uint32_t>(Contents, Pos); 8093 Entry.LSDAStart = readNext<uint32_t>(Contents, Pos); 8094 IndexEntries.push_back(Entry); 8095 8096 outs() << " [" << i << "]: " 8097 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset) 8098 << ", " 8099 << "2nd level page offset=" 8100 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", " 8101 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n'; 8102 } 8103 8104 //===---------------------------------- 8105 // Next come the LSDA tables 8106 //===---------------------------------- 8107 8108 // The LSDA layout is rather implicit: it's a contiguous array of entries from 8109 // the first top-level index's LSDAOffset to the last (sentinel). 8110 8111 outs() << " LSDA descriptors:\n"; 8112 Pos = IndexEntries[0].LSDAStart; 8113 const uint32_t LSDASize = 2 * sizeof(uint32_t); 8114 int NumLSDAs = 8115 (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) / LSDASize; 8116 8117 for (int i = 0; i < NumLSDAs; ++i) { 8118 uint32_t FunctionOffset = readNext<uint32_t>(Contents, Pos); 8119 uint32_t LSDAOffset = readNext<uint32_t>(Contents, Pos); 8120 outs() << " [" << i << "]: " 8121 << "function offset=" << format("0x%08" PRIx32, FunctionOffset) 8122 << ", " 8123 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n'; 8124 } 8125 8126 //===---------------------------------- 8127 // Finally, the 2nd level indices 8128 //===---------------------------------- 8129 8130 // Generally these are 4K in size, and have 2 possible forms: 8131 // + Regular stores up to 511 entries with disparate encodings 8132 // + Compressed stores up to 1021 entries if few enough compact encoding 8133 // values are used. 8134 outs() << " Second level indices:\n"; 8135 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) { 8136 // The final sentinel top-level index has no associated 2nd level page 8137 if (IndexEntries[i].SecondLevelPageStart == 0) 8138 break; 8139 8140 outs() << " Second level index[" << i << "]: " 8141 << "offset in section=" 8142 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart) 8143 << ", " 8144 << "base function offset=" 8145 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n'; 8146 8147 Pos = IndexEntries[i].SecondLevelPageStart; 8148 if (Pos + sizeof(uint32_t) > Contents.size()) { 8149 outs() << "warning: invalid offset for second level page: " << Pos << '\n'; 8150 continue; 8151 } 8152 8153 uint32_t Kind = 8154 *reinterpret_cast<const support::ulittle32_t *>(Contents.data() + Pos); 8155 if (Kind == 2) 8156 printRegularSecondLevelUnwindPage(Contents.substr(Pos, 4096)); 8157 else if (Kind == 3) 8158 printCompressedSecondLevelUnwindPage(Contents.substr(Pos, 4096), 8159 IndexEntries[i].FunctionOffset, 8160 CommonEncodings); 8161 else 8162 outs() << " Skipping 2nd level page with unknown kind " << Kind 8163 << '\n'; 8164 } 8165 } 8166 8167 void printMachOUnwindInfo(const MachOObjectFile *Obj) { 8168 std::map<uint64_t, SymbolRef> Symbols; 8169 for (const SymbolRef &SymRef : Obj->symbols()) { 8170 // Discard any undefined or absolute symbols. They're not going to take part 8171 // in the convenience lookup for unwind info and just take up resources. 8172 auto SectOrErr = SymRef.getSection(); 8173 if (!SectOrErr) { 8174 // TODO: Actually report errors helpfully. 8175 consumeError(SectOrErr.takeError()); 8176 continue; 8177 } 8178 section_iterator Section = *SectOrErr; 8179 if (Section == Obj->section_end()) 8180 continue; 8181 8182 uint64_t Addr = SymRef.getValue(); 8183 Symbols.insert(std::make_pair(Addr, SymRef)); 8184 } 8185 8186 for (const SectionRef &Section : Obj->sections()) { 8187 StringRef SectName; 8188 if (Expected<StringRef> NameOrErr = Section.getName()) 8189 SectName = *NameOrErr; 8190 else 8191 consumeError(NameOrErr.takeError()); 8192 8193 if (SectName == "__compact_unwind") 8194 printMachOCompactUnwindSection(Obj, Symbols, Section); 8195 else if (SectName == "__unwind_info") 8196 printMachOUnwindInfoSection(Obj, Symbols, Section); 8197 } 8198 } 8199 8200 static void PrintMachHeader(uint32_t magic, uint32_t cputype, 8201 uint32_t cpusubtype, uint32_t filetype, 8202 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags, 8203 bool verbose) { 8204 outs() << "Mach header\n"; 8205 outs() << " magic cputype cpusubtype caps filetype ncmds " 8206 "sizeofcmds flags\n"; 8207 if (verbose) { 8208 if (magic == MachO::MH_MAGIC) 8209 outs() << " MH_MAGIC"; 8210 else if (magic == MachO::MH_MAGIC_64) 8211 outs() << "MH_MAGIC_64"; 8212 else 8213 outs() << format(" 0x%08" PRIx32, magic); 8214 switch (cputype) { 8215 case MachO::CPU_TYPE_I386: 8216 outs() << " I386"; 8217 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 8218 case MachO::CPU_SUBTYPE_I386_ALL: 8219 outs() << " ALL"; 8220 break; 8221 default: 8222 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8223 break; 8224 } 8225 break; 8226 case MachO::CPU_TYPE_X86_64: 8227 outs() << " X86_64"; 8228 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 8229 case MachO::CPU_SUBTYPE_X86_64_ALL: 8230 outs() << " ALL"; 8231 break; 8232 case MachO::CPU_SUBTYPE_X86_64_H: 8233 outs() << " Haswell"; 8234 break; 8235 default: 8236 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8237 break; 8238 } 8239 break; 8240 case MachO::CPU_TYPE_ARM: 8241 outs() << " ARM"; 8242 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 8243 case MachO::CPU_SUBTYPE_ARM_ALL: 8244 outs() << " ALL"; 8245 break; 8246 case MachO::CPU_SUBTYPE_ARM_V4T: 8247 outs() << " V4T"; 8248 break; 8249 case MachO::CPU_SUBTYPE_ARM_V5TEJ: 8250 outs() << " V5TEJ"; 8251 break; 8252 case MachO::CPU_SUBTYPE_ARM_XSCALE: 8253 outs() << " XSCALE"; 8254 break; 8255 case MachO::CPU_SUBTYPE_ARM_V6: 8256 outs() << " V6"; 8257 break; 8258 case MachO::CPU_SUBTYPE_ARM_V6M: 8259 outs() << " V6M"; 8260 break; 8261 case MachO::CPU_SUBTYPE_ARM_V7: 8262 outs() << " V7"; 8263 break; 8264 case MachO::CPU_SUBTYPE_ARM_V7EM: 8265 outs() << " V7EM"; 8266 break; 8267 case MachO::CPU_SUBTYPE_ARM_V7K: 8268 outs() << " V7K"; 8269 break; 8270 case MachO::CPU_SUBTYPE_ARM_V7M: 8271 outs() << " V7M"; 8272 break; 8273 case MachO::CPU_SUBTYPE_ARM_V7S: 8274 outs() << " V7S"; 8275 break; 8276 default: 8277 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8278 break; 8279 } 8280 break; 8281 case MachO::CPU_TYPE_ARM64: 8282 outs() << " ARM64"; 8283 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 8284 case MachO::CPU_SUBTYPE_ARM64_ALL: 8285 outs() << " ALL"; 8286 break; 8287 case MachO::CPU_SUBTYPE_ARM64E: 8288 outs() << " E"; 8289 break; 8290 default: 8291 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8292 break; 8293 } 8294 break; 8295 case MachO::CPU_TYPE_ARM64_32: 8296 outs() << " ARM64_32"; 8297 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 8298 case MachO::CPU_SUBTYPE_ARM64_32_V8: 8299 outs() << " V8"; 8300 break; 8301 default: 8302 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8303 break; 8304 } 8305 break; 8306 case MachO::CPU_TYPE_POWERPC: 8307 outs() << " PPC"; 8308 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 8309 case MachO::CPU_SUBTYPE_POWERPC_ALL: 8310 outs() << " ALL"; 8311 break; 8312 default: 8313 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8314 break; 8315 } 8316 break; 8317 case MachO::CPU_TYPE_POWERPC64: 8318 outs() << " PPC64"; 8319 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 8320 case MachO::CPU_SUBTYPE_POWERPC_ALL: 8321 outs() << " ALL"; 8322 break; 8323 default: 8324 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8325 break; 8326 } 8327 break; 8328 default: 8329 outs() << format(" %7d", cputype); 8330 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8331 break; 8332 } 8333 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) { 8334 outs() << " LIB64"; 8335 } else { 8336 outs() << format(" 0x%02" PRIx32, 8337 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24); 8338 } 8339 switch (filetype) { 8340 case MachO::MH_OBJECT: 8341 outs() << " OBJECT"; 8342 break; 8343 case MachO::MH_EXECUTE: 8344 outs() << " EXECUTE"; 8345 break; 8346 case MachO::MH_FVMLIB: 8347 outs() << " FVMLIB"; 8348 break; 8349 case MachO::MH_CORE: 8350 outs() << " CORE"; 8351 break; 8352 case MachO::MH_PRELOAD: 8353 outs() << " PRELOAD"; 8354 break; 8355 case MachO::MH_DYLIB: 8356 outs() << " DYLIB"; 8357 break; 8358 case MachO::MH_DYLIB_STUB: 8359 outs() << " DYLIB_STUB"; 8360 break; 8361 case MachO::MH_DYLINKER: 8362 outs() << " DYLINKER"; 8363 break; 8364 case MachO::MH_BUNDLE: 8365 outs() << " BUNDLE"; 8366 break; 8367 case MachO::MH_DSYM: 8368 outs() << " DSYM"; 8369 break; 8370 case MachO::MH_KEXT_BUNDLE: 8371 outs() << " KEXTBUNDLE"; 8372 break; 8373 default: 8374 outs() << format(" %10u", filetype); 8375 break; 8376 } 8377 outs() << format(" %5u", ncmds); 8378 outs() << format(" %10u", sizeofcmds); 8379 uint32_t f = flags; 8380 if (f & MachO::MH_NOUNDEFS) { 8381 outs() << " NOUNDEFS"; 8382 f &= ~MachO::MH_NOUNDEFS; 8383 } 8384 if (f & MachO::MH_INCRLINK) { 8385 outs() << " INCRLINK"; 8386 f &= ~MachO::MH_INCRLINK; 8387 } 8388 if (f & MachO::MH_DYLDLINK) { 8389 outs() << " DYLDLINK"; 8390 f &= ~MachO::MH_DYLDLINK; 8391 } 8392 if (f & MachO::MH_BINDATLOAD) { 8393 outs() << " BINDATLOAD"; 8394 f &= ~MachO::MH_BINDATLOAD; 8395 } 8396 if (f & MachO::MH_PREBOUND) { 8397 outs() << " PREBOUND"; 8398 f &= ~MachO::MH_PREBOUND; 8399 } 8400 if (f & MachO::MH_SPLIT_SEGS) { 8401 outs() << " SPLIT_SEGS"; 8402 f &= ~MachO::MH_SPLIT_SEGS; 8403 } 8404 if (f & MachO::MH_LAZY_INIT) { 8405 outs() << " LAZY_INIT"; 8406 f &= ~MachO::MH_LAZY_INIT; 8407 } 8408 if (f & MachO::MH_TWOLEVEL) { 8409 outs() << " TWOLEVEL"; 8410 f &= ~MachO::MH_TWOLEVEL; 8411 } 8412 if (f & MachO::MH_FORCE_FLAT) { 8413 outs() << " FORCE_FLAT"; 8414 f &= ~MachO::MH_FORCE_FLAT; 8415 } 8416 if (f & MachO::MH_NOMULTIDEFS) { 8417 outs() << " NOMULTIDEFS"; 8418 f &= ~MachO::MH_NOMULTIDEFS; 8419 } 8420 if (f & MachO::MH_NOFIXPREBINDING) { 8421 outs() << " NOFIXPREBINDING"; 8422 f &= ~MachO::MH_NOFIXPREBINDING; 8423 } 8424 if (f & MachO::MH_PREBINDABLE) { 8425 outs() << " PREBINDABLE"; 8426 f &= ~MachO::MH_PREBINDABLE; 8427 } 8428 if (f & MachO::MH_ALLMODSBOUND) { 8429 outs() << " ALLMODSBOUND"; 8430 f &= ~MachO::MH_ALLMODSBOUND; 8431 } 8432 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) { 8433 outs() << " SUBSECTIONS_VIA_SYMBOLS"; 8434 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS; 8435 } 8436 if (f & MachO::MH_CANONICAL) { 8437 outs() << " CANONICAL"; 8438 f &= ~MachO::MH_CANONICAL; 8439 } 8440 if (f & MachO::MH_WEAK_DEFINES) { 8441 outs() << " WEAK_DEFINES"; 8442 f &= ~MachO::MH_WEAK_DEFINES; 8443 } 8444 if (f & MachO::MH_BINDS_TO_WEAK) { 8445 outs() << " BINDS_TO_WEAK"; 8446 f &= ~MachO::MH_BINDS_TO_WEAK; 8447 } 8448 if (f & MachO::MH_ALLOW_STACK_EXECUTION) { 8449 outs() << " ALLOW_STACK_EXECUTION"; 8450 f &= ~MachO::MH_ALLOW_STACK_EXECUTION; 8451 } 8452 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) { 8453 outs() << " DEAD_STRIPPABLE_DYLIB"; 8454 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB; 8455 } 8456 if (f & MachO::MH_PIE) { 8457 outs() << " PIE"; 8458 f &= ~MachO::MH_PIE; 8459 } 8460 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) { 8461 outs() << " NO_REEXPORTED_DYLIBS"; 8462 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS; 8463 } 8464 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) { 8465 outs() << " MH_HAS_TLV_DESCRIPTORS"; 8466 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS; 8467 } 8468 if (f & MachO::MH_NO_HEAP_EXECUTION) { 8469 outs() << " MH_NO_HEAP_EXECUTION"; 8470 f &= ~MachO::MH_NO_HEAP_EXECUTION; 8471 } 8472 if (f & MachO::MH_APP_EXTENSION_SAFE) { 8473 outs() << " APP_EXTENSION_SAFE"; 8474 f &= ~MachO::MH_APP_EXTENSION_SAFE; 8475 } 8476 if (f & MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO) { 8477 outs() << " NLIST_OUTOFSYNC_WITH_DYLDINFO"; 8478 f &= ~MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO; 8479 } 8480 if (f != 0 || flags == 0) 8481 outs() << format(" 0x%08" PRIx32, f); 8482 } else { 8483 outs() << format(" 0x%08" PRIx32, magic); 8484 outs() << format(" %7d", cputype); 8485 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8486 outs() << format(" 0x%02" PRIx32, 8487 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24); 8488 outs() << format(" %10u", filetype); 8489 outs() << format(" %5u", ncmds); 8490 outs() << format(" %10u", sizeofcmds); 8491 outs() << format(" 0x%08" PRIx32, flags); 8492 } 8493 outs() << "\n"; 8494 } 8495 8496 static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize, 8497 StringRef SegName, uint64_t vmaddr, 8498 uint64_t vmsize, uint64_t fileoff, 8499 uint64_t filesize, uint32_t maxprot, 8500 uint32_t initprot, uint32_t nsects, 8501 uint32_t flags, uint32_t object_size, 8502 bool verbose) { 8503 uint64_t expected_cmdsize; 8504 if (cmd == MachO::LC_SEGMENT) { 8505 outs() << " cmd LC_SEGMENT\n"; 8506 expected_cmdsize = nsects; 8507 expected_cmdsize *= sizeof(struct MachO::section); 8508 expected_cmdsize += sizeof(struct MachO::segment_command); 8509 } else { 8510 outs() << " cmd LC_SEGMENT_64\n"; 8511 expected_cmdsize = nsects; 8512 expected_cmdsize *= sizeof(struct MachO::section_64); 8513 expected_cmdsize += sizeof(struct MachO::segment_command_64); 8514 } 8515 outs() << " cmdsize " << cmdsize; 8516 if (cmdsize != expected_cmdsize) 8517 outs() << " Inconsistent size\n"; 8518 else 8519 outs() << "\n"; 8520 outs() << " segname " << SegName << "\n"; 8521 if (cmd == MachO::LC_SEGMENT_64) { 8522 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n"; 8523 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n"; 8524 } else { 8525 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n"; 8526 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n"; 8527 } 8528 outs() << " fileoff " << fileoff; 8529 if (fileoff > object_size) 8530 outs() << " (past end of file)\n"; 8531 else 8532 outs() << "\n"; 8533 outs() << " filesize " << filesize; 8534 if (fileoff + filesize > object_size) 8535 outs() << " (past end of file)\n"; 8536 else 8537 outs() << "\n"; 8538 if (verbose) { 8539 if ((maxprot & 8540 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | 8541 MachO::VM_PROT_EXECUTE)) != 0) 8542 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n"; 8543 else { 8544 outs() << " maxprot "; 8545 outs() << ((maxprot & MachO::VM_PROT_READ) ? "r" : "-"); 8546 outs() << ((maxprot & MachO::VM_PROT_WRITE) ? "w" : "-"); 8547 outs() << ((maxprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n"); 8548 } 8549 if ((initprot & 8550 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | 8551 MachO::VM_PROT_EXECUTE)) != 0) 8552 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n"; 8553 else { 8554 outs() << " initprot "; 8555 outs() << ((initprot & MachO::VM_PROT_READ) ? "r" : "-"); 8556 outs() << ((initprot & MachO::VM_PROT_WRITE) ? "w" : "-"); 8557 outs() << ((initprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n"); 8558 } 8559 } else { 8560 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n"; 8561 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n"; 8562 } 8563 outs() << " nsects " << nsects << "\n"; 8564 if (verbose) { 8565 outs() << " flags"; 8566 if (flags == 0) 8567 outs() << " (none)\n"; 8568 else { 8569 if (flags & MachO::SG_HIGHVM) { 8570 outs() << " HIGHVM"; 8571 flags &= ~MachO::SG_HIGHVM; 8572 } 8573 if (flags & MachO::SG_FVMLIB) { 8574 outs() << " FVMLIB"; 8575 flags &= ~MachO::SG_FVMLIB; 8576 } 8577 if (flags & MachO::SG_NORELOC) { 8578 outs() << " NORELOC"; 8579 flags &= ~MachO::SG_NORELOC; 8580 } 8581 if (flags & MachO::SG_PROTECTED_VERSION_1) { 8582 outs() << " PROTECTED_VERSION_1"; 8583 flags &= ~MachO::SG_PROTECTED_VERSION_1; 8584 } 8585 if (flags) 8586 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n"; 8587 else 8588 outs() << "\n"; 8589 } 8590 } else { 8591 outs() << " flags " << format("0x%" PRIx32, flags) << "\n"; 8592 } 8593 } 8594 8595 static void PrintSection(const char *sectname, const char *segname, 8596 uint64_t addr, uint64_t size, uint32_t offset, 8597 uint32_t align, uint32_t reloff, uint32_t nreloc, 8598 uint32_t flags, uint32_t reserved1, uint32_t reserved2, 8599 uint32_t cmd, const char *sg_segname, 8600 uint32_t filetype, uint32_t object_size, 8601 bool verbose) { 8602 outs() << "Section\n"; 8603 outs() << " sectname " << format("%.16s\n", sectname); 8604 outs() << " segname " << format("%.16s", segname); 8605 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0) 8606 outs() << " (does not match segment)\n"; 8607 else 8608 outs() << "\n"; 8609 if (cmd == MachO::LC_SEGMENT_64) { 8610 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n"; 8611 outs() << " size " << format("0x%016" PRIx64, size); 8612 } else { 8613 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n"; 8614 outs() << " size " << format("0x%08" PRIx64, size); 8615 } 8616 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size) 8617 outs() << " (past end of file)\n"; 8618 else 8619 outs() << "\n"; 8620 outs() << " offset " << offset; 8621 if (offset > object_size) 8622 outs() << " (past end of file)\n"; 8623 else 8624 outs() << "\n"; 8625 uint32_t align_shifted = 1 << align; 8626 outs() << " align 2^" << align << " (" << align_shifted << ")\n"; 8627 outs() << " reloff " << reloff; 8628 if (reloff > object_size) 8629 outs() << " (past end of file)\n"; 8630 else 8631 outs() << "\n"; 8632 outs() << " nreloc " << nreloc; 8633 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size) 8634 outs() << " (past end of file)\n"; 8635 else 8636 outs() << "\n"; 8637 uint32_t section_type = flags & MachO::SECTION_TYPE; 8638 if (verbose) { 8639 outs() << " type"; 8640 if (section_type == MachO::S_REGULAR) 8641 outs() << " S_REGULAR\n"; 8642 else if (section_type == MachO::S_ZEROFILL) 8643 outs() << " S_ZEROFILL\n"; 8644 else if (section_type == MachO::S_CSTRING_LITERALS) 8645 outs() << " S_CSTRING_LITERALS\n"; 8646 else if (section_type == MachO::S_4BYTE_LITERALS) 8647 outs() << " S_4BYTE_LITERALS\n"; 8648 else if (section_type == MachO::S_8BYTE_LITERALS) 8649 outs() << " S_8BYTE_LITERALS\n"; 8650 else if (section_type == MachO::S_16BYTE_LITERALS) 8651 outs() << " S_16BYTE_LITERALS\n"; 8652 else if (section_type == MachO::S_LITERAL_POINTERS) 8653 outs() << " S_LITERAL_POINTERS\n"; 8654 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS) 8655 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n"; 8656 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS) 8657 outs() << " S_LAZY_SYMBOL_POINTERS\n"; 8658 else if (section_type == MachO::S_SYMBOL_STUBS) 8659 outs() << " S_SYMBOL_STUBS\n"; 8660 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS) 8661 outs() << " S_MOD_INIT_FUNC_POINTERS\n"; 8662 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS) 8663 outs() << " S_MOD_TERM_FUNC_POINTERS\n"; 8664 else if (section_type == MachO::S_COALESCED) 8665 outs() << " S_COALESCED\n"; 8666 else if (section_type == MachO::S_INTERPOSING) 8667 outs() << " S_INTERPOSING\n"; 8668 else if (section_type == MachO::S_DTRACE_DOF) 8669 outs() << " S_DTRACE_DOF\n"; 8670 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS) 8671 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n"; 8672 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR) 8673 outs() << " S_THREAD_LOCAL_REGULAR\n"; 8674 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL) 8675 outs() << " S_THREAD_LOCAL_ZEROFILL\n"; 8676 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES) 8677 outs() << " S_THREAD_LOCAL_VARIABLES\n"; 8678 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS) 8679 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n"; 8680 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS) 8681 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n"; 8682 else 8683 outs() << format("0x%08" PRIx32, section_type) << "\n"; 8684 outs() << "attributes"; 8685 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES; 8686 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS) 8687 outs() << " PURE_INSTRUCTIONS"; 8688 if (section_attributes & MachO::S_ATTR_NO_TOC) 8689 outs() << " NO_TOC"; 8690 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS) 8691 outs() << " STRIP_STATIC_SYMS"; 8692 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP) 8693 outs() << " NO_DEAD_STRIP"; 8694 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT) 8695 outs() << " LIVE_SUPPORT"; 8696 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE) 8697 outs() << " SELF_MODIFYING_CODE"; 8698 if (section_attributes & MachO::S_ATTR_DEBUG) 8699 outs() << " DEBUG"; 8700 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS) 8701 outs() << " SOME_INSTRUCTIONS"; 8702 if (section_attributes & MachO::S_ATTR_EXT_RELOC) 8703 outs() << " EXT_RELOC"; 8704 if (section_attributes & MachO::S_ATTR_LOC_RELOC) 8705 outs() << " LOC_RELOC"; 8706 if (section_attributes == 0) 8707 outs() << " (none)"; 8708 outs() << "\n"; 8709 } else 8710 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n"; 8711 outs() << " reserved1 " << reserved1; 8712 if (section_type == MachO::S_SYMBOL_STUBS || 8713 section_type == MachO::S_LAZY_SYMBOL_POINTERS || 8714 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS || 8715 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS || 8716 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS) 8717 outs() << " (index into indirect symbol table)\n"; 8718 else 8719 outs() << "\n"; 8720 outs() << " reserved2 " << reserved2; 8721 if (section_type == MachO::S_SYMBOL_STUBS) 8722 outs() << " (size of stubs)\n"; 8723 else 8724 outs() << "\n"; 8725 } 8726 8727 static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit, 8728 uint32_t object_size) { 8729 outs() << " cmd LC_SYMTAB\n"; 8730 outs() << " cmdsize " << st.cmdsize; 8731 if (st.cmdsize != sizeof(struct MachO::symtab_command)) 8732 outs() << " Incorrect size\n"; 8733 else 8734 outs() << "\n"; 8735 outs() << " symoff " << st.symoff; 8736 if (st.symoff > object_size) 8737 outs() << " (past end of file)\n"; 8738 else 8739 outs() << "\n"; 8740 outs() << " nsyms " << st.nsyms; 8741 uint64_t big_size; 8742 if (Is64Bit) { 8743 big_size = st.nsyms; 8744 big_size *= sizeof(struct MachO::nlist_64); 8745 big_size += st.symoff; 8746 if (big_size > object_size) 8747 outs() << " (past end of file)\n"; 8748 else 8749 outs() << "\n"; 8750 } else { 8751 big_size = st.nsyms; 8752 big_size *= sizeof(struct MachO::nlist); 8753 big_size += st.symoff; 8754 if (big_size > object_size) 8755 outs() << " (past end of file)\n"; 8756 else 8757 outs() << "\n"; 8758 } 8759 outs() << " stroff " << st.stroff; 8760 if (st.stroff > object_size) 8761 outs() << " (past end of file)\n"; 8762 else 8763 outs() << "\n"; 8764 outs() << " strsize " << st.strsize; 8765 big_size = st.stroff; 8766 big_size += st.strsize; 8767 if (big_size > object_size) 8768 outs() << " (past end of file)\n"; 8769 else 8770 outs() << "\n"; 8771 } 8772 8773 static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst, 8774 uint32_t nsyms, uint32_t object_size, 8775 bool Is64Bit) { 8776 outs() << " cmd LC_DYSYMTAB\n"; 8777 outs() << " cmdsize " << dyst.cmdsize; 8778 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command)) 8779 outs() << " Incorrect size\n"; 8780 else 8781 outs() << "\n"; 8782 outs() << " ilocalsym " << dyst.ilocalsym; 8783 if (dyst.ilocalsym > nsyms) 8784 outs() << " (greater than the number of symbols)\n"; 8785 else 8786 outs() << "\n"; 8787 outs() << " nlocalsym " << dyst.nlocalsym; 8788 uint64_t big_size; 8789 big_size = dyst.ilocalsym; 8790 big_size += dyst.nlocalsym; 8791 if (big_size > nsyms) 8792 outs() << " (past the end of the symbol table)\n"; 8793 else 8794 outs() << "\n"; 8795 outs() << " iextdefsym " << dyst.iextdefsym; 8796 if (dyst.iextdefsym > nsyms) 8797 outs() << " (greater than the number of symbols)\n"; 8798 else 8799 outs() << "\n"; 8800 outs() << " nextdefsym " << dyst.nextdefsym; 8801 big_size = dyst.iextdefsym; 8802 big_size += dyst.nextdefsym; 8803 if (big_size > nsyms) 8804 outs() << " (past the end of the symbol table)\n"; 8805 else 8806 outs() << "\n"; 8807 outs() << " iundefsym " << dyst.iundefsym; 8808 if (dyst.iundefsym > nsyms) 8809 outs() << " (greater than the number of symbols)\n"; 8810 else 8811 outs() << "\n"; 8812 outs() << " nundefsym " << dyst.nundefsym; 8813 big_size = dyst.iundefsym; 8814 big_size += dyst.nundefsym; 8815 if (big_size > nsyms) 8816 outs() << " (past the end of the symbol table)\n"; 8817 else 8818 outs() << "\n"; 8819 outs() << " tocoff " << dyst.tocoff; 8820 if (dyst.tocoff > object_size) 8821 outs() << " (past end of file)\n"; 8822 else 8823 outs() << "\n"; 8824 outs() << " ntoc " << dyst.ntoc; 8825 big_size = dyst.ntoc; 8826 big_size *= sizeof(struct MachO::dylib_table_of_contents); 8827 big_size += dyst.tocoff; 8828 if (big_size > object_size) 8829 outs() << " (past end of file)\n"; 8830 else 8831 outs() << "\n"; 8832 outs() << " modtaboff " << dyst.modtaboff; 8833 if (dyst.modtaboff > object_size) 8834 outs() << " (past end of file)\n"; 8835 else 8836 outs() << "\n"; 8837 outs() << " nmodtab " << dyst.nmodtab; 8838 uint64_t modtabend; 8839 if (Is64Bit) { 8840 modtabend = dyst.nmodtab; 8841 modtabend *= sizeof(struct MachO::dylib_module_64); 8842 modtabend += dyst.modtaboff; 8843 } else { 8844 modtabend = dyst.nmodtab; 8845 modtabend *= sizeof(struct MachO::dylib_module); 8846 modtabend += dyst.modtaboff; 8847 } 8848 if (modtabend > object_size) 8849 outs() << " (past end of file)\n"; 8850 else 8851 outs() << "\n"; 8852 outs() << " extrefsymoff " << dyst.extrefsymoff; 8853 if (dyst.extrefsymoff > object_size) 8854 outs() << " (past end of file)\n"; 8855 else 8856 outs() << "\n"; 8857 outs() << " nextrefsyms " << dyst.nextrefsyms; 8858 big_size = dyst.nextrefsyms; 8859 big_size *= sizeof(struct MachO::dylib_reference); 8860 big_size += dyst.extrefsymoff; 8861 if (big_size > object_size) 8862 outs() << " (past end of file)\n"; 8863 else 8864 outs() << "\n"; 8865 outs() << " indirectsymoff " << dyst.indirectsymoff; 8866 if (dyst.indirectsymoff > object_size) 8867 outs() << " (past end of file)\n"; 8868 else 8869 outs() << "\n"; 8870 outs() << " nindirectsyms " << dyst.nindirectsyms; 8871 big_size = dyst.nindirectsyms; 8872 big_size *= sizeof(uint32_t); 8873 big_size += dyst.indirectsymoff; 8874 if (big_size > object_size) 8875 outs() << " (past end of file)\n"; 8876 else 8877 outs() << "\n"; 8878 outs() << " extreloff " << dyst.extreloff; 8879 if (dyst.extreloff > object_size) 8880 outs() << " (past end of file)\n"; 8881 else 8882 outs() << "\n"; 8883 outs() << " nextrel " << dyst.nextrel; 8884 big_size = dyst.nextrel; 8885 big_size *= sizeof(struct MachO::relocation_info); 8886 big_size += dyst.extreloff; 8887 if (big_size > object_size) 8888 outs() << " (past end of file)\n"; 8889 else 8890 outs() << "\n"; 8891 outs() << " locreloff " << dyst.locreloff; 8892 if (dyst.locreloff > object_size) 8893 outs() << " (past end of file)\n"; 8894 else 8895 outs() << "\n"; 8896 outs() << " nlocrel " << dyst.nlocrel; 8897 big_size = dyst.nlocrel; 8898 big_size *= sizeof(struct MachO::relocation_info); 8899 big_size += dyst.locreloff; 8900 if (big_size > object_size) 8901 outs() << " (past end of file)\n"; 8902 else 8903 outs() << "\n"; 8904 } 8905 8906 static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc, 8907 uint32_t object_size) { 8908 if (dc.cmd == MachO::LC_DYLD_INFO) 8909 outs() << " cmd LC_DYLD_INFO\n"; 8910 else 8911 outs() << " cmd LC_DYLD_INFO_ONLY\n"; 8912 outs() << " cmdsize " << dc.cmdsize; 8913 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command)) 8914 outs() << " Incorrect size\n"; 8915 else 8916 outs() << "\n"; 8917 outs() << " rebase_off " << dc.rebase_off; 8918 if (dc.rebase_off > object_size) 8919 outs() << " (past end of file)\n"; 8920 else 8921 outs() << "\n"; 8922 outs() << " rebase_size " << dc.rebase_size; 8923 uint64_t big_size; 8924 big_size = dc.rebase_off; 8925 big_size += dc.rebase_size; 8926 if (big_size > object_size) 8927 outs() << " (past end of file)\n"; 8928 else 8929 outs() << "\n"; 8930 outs() << " bind_off " << dc.bind_off; 8931 if (dc.bind_off > object_size) 8932 outs() << " (past end of file)\n"; 8933 else 8934 outs() << "\n"; 8935 outs() << " bind_size " << dc.bind_size; 8936 big_size = dc.bind_off; 8937 big_size += dc.bind_size; 8938 if (big_size > object_size) 8939 outs() << " (past end of file)\n"; 8940 else 8941 outs() << "\n"; 8942 outs() << " weak_bind_off " << dc.weak_bind_off; 8943 if (dc.weak_bind_off > object_size) 8944 outs() << " (past end of file)\n"; 8945 else 8946 outs() << "\n"; 8947 outs() << " weak_bind_size " << dc.weak_bind_size; 8948 big_size = dc.weak_bind_off; 8949 big_size += dc.weak_bind_size; 8950 if (big_size > object_size) 8951 outs() << " (past end of file)\n"; 8952 else 8953 outs() << "\n"; 8954 outs() << " lazy_bind_off " << dc.lazy_bind_off; 8955 if (dc.lazy_bind_off > object_size) 8956 outs() << " (past end of file)\n"; 8957 else 8958 outs() << "\n"; 8959 outs() << " lazy_bind_size " << dc.lazy_bind_size; 8960 big_size = dc.lazy_bind_off; 8961 big_size += dc.lazy_bind_size; 8962 if (big_size > object_size) 8963 outs() << " (past end of file)\n"; 8964 else 8965 outs() << "\n"; 8966 outs() << " export_off " << dc.export_off; 8967 if (dc.export_off > object_size) 8968 outs() << " (past end of file)\n"; 8969 else 8970 outs() << "\n"; 8971 outs() << " export_size " << dc.export_size; 8972 big_size = dc.export_off; 8973 big_size += dc.export_size; 8974 if (big_size > object_size) 8975 outs() << " (past end of file)\n"; 8976 else 8977 outs() << "\n"; 8978 } 8979 8980 static void PrintDyldLoadCommand(MachO::dylinker_command dyld, 8981 const char *Ptr) { 8982 if (dyld.cmd == MachO::LC_ID_DYLINKER) 8983 outs() << " cmd LC_ID_DYLINKER\n"; 8984 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER) 8985 outs() << " cmd LC_LOAD_DYLINKER\n"; 8986 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT) 8987 outs() << " cmd LC_DYLD_ENVIRONMENT\n"; 8988 else 8989 outs() << " cmd ?(" << dyld.cmd << ")\n"; 8990 outs() << " cmdsize " << dyld.cmdsize; 8991 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command)) 8992 outs() << " Incorrect size\n"; 8993 else 8994 outs() << "\n"; 8995 if (dyld.name >= dyld.cmdsize) 8996 outs() << " name ?(bad offset " << dyld.name << ")\n"; 8997 else { 8998 const char *P = (const char *)(Ptr) + dyld.name; 8999 outs() << " name " << P << " (offset " << dyld.name << ")\n"; 9000 } 9001 } 9002 9003 static void PrintUuidLoadCommand(MachO::uuid_command uuid) { 9004 outs() << " cmd LC_UUID\n"; 9005 outs() << " cmdsize " << uuid.cmdsize; 9006 if (uuid.cmdsize != sizeof(struct MachO::uuid_command)) 9007 outs() << " Incorrect size\n"; 9008 else 9009 outs() << "\n"; 9010 outs() << " uuid "; 9011 for (int i = 0; i < 16; ++i) { 9012 outs() << format("%02" PRIX32, uuid.uuid[i]); 9013 if (i == 3 || i == 5 || i == 7 || i == 9) 9014 outs() << "-"; 9015 } 9016 outs() << "\n"; 9017 } 9018 9019 static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) { 9020 outs() << " cmd LC_RPATH\n"; 9021 outs() << " cmdsize " << rpath.cmdsize; 9022 if (rpath.cmdsize < sizeof(struct MachO::rpath_command)) 9023 outs() << " Incorrect size\n"; 9024 else 9025 outs() << "\n"; 9026 if (rpath.path >= rpath.cmdsize) 9027 outs() << " path ?(bad offset " << rpath.path << ")\n"; 9028 else { 9029 const char *P = (const char *)(Ptr) + rpath.path; 9030 outs() << " path " << P << " (offset " << rpath.path << ")\n"; 9031 } 9032 } 9033 9034 static void PrintVersionMinLoadCommand(MachO::version_min_command vd) { 9035 StringRef LoadCmdName; 9036 switch (vd.cmd) { 9037 case MachO::LC_VERSION_MIN_MACOSX: 9038 LoadCmdName = "LC_VERSION_MIN_MACOSX"; 9039 break; 9040 case MachO::LC_VERSION_MIN_IPHONEOS: 9041 LoadCmdName = "LC_VERSION_MIN_IPHONEOS"; 9042 break; 9043 case MachO::LC_VERSION_MIN_TVOS: 9044 LoadCmdName = "LC_VERSION_MIN_TVOS"; 9045 break; 9046 case MachO::LC_VERSION_MIN_WATCHOS: 9047 LoadCmdName = "LC_VERSION_MIN_WATCHOS"; 9048 break; 9049 default: 9050 llvm_unreachable("Unknown version min load command"); 9051 } 9052 9053 outs() << " cmd " << LoadCmdName << '\n'; 9054 outs() << " cmdsize " << vd.cmdsize; 9055 if (vd.cmdsize != sizeof(struct MachO::version_min_command)) 9056 outs() << " Incorrect size\n"; 9057 else 9058 outs() << "\n"; 9059 outs() << " version " 9060 << MachOObjectFile::getVersionMinMajor(vd, false) << "." 9061 << MachOObjectFile::getVersionMinMinor(vd, false); 9062 uint32_t Update = MachOObjectFile::getVersionMinUpdate(vd, false); 9063 if (Update != 0) 9064 outs() << "." << Update; 9065 outs() << "\n"; 9066 if (vd.sdk == 0) 9067 outs() << " sdk n/a"; 9068 else { 9069 outs() << " sdk " 9070 << MachOObjectFile::getVersionMinMajor(vd, true) << "." 9071 << MachOObjectFile::getVersionMinMinor(vd, true); 9072 } 9073 Update = MachOObjectFile::getVersionMinUpdate(vd, true); 9074 if (Update != 0) 9075 outs() << "." << Update; 9076 outs() << "\n"; 9077 } 9078 9079 static void PrintNoteLoadCommand(MachO::note_command Nt) { 9080 outs() << " cmd LC_NOTE\n"; 9081 outs() << " cmdsize " << Nt.cmdsize; 9082 if (Nt.cmdsize != sizeof(struct MachO::note_command)) 9083 outs() << " Incorrect size\n"; 9084 else 9085 outs() << "\n"; 9086 const char *d = Nt.data_owner; 9087 outs() << "data_owner " << format("%.16s\n", d); 9088 outs() << " offset " << Nt.offset << "\n"; 9089 outs() << " size " << Nt.size << "\n"; 9090 } 9091 9092 static void PrintBuildToolVersion(MachO::build_tool_version bv) { 9093 outs() << " tool " << MachOObjectFile::getBuildTool(bv.tool) << "\n"; 9094 outs() << " version " << MachOObjectFile::getVersionString(bv.version) 9095 << "\n"; 9096 } 9097 9098 static void PrintBuildVersionLoadCommand(const MachOObjectFile *obj, 9099 MachO::build_version_command bd) { 9100 outs() << " cmd LC_BUILD_VERSION\n"; 9101 outs() << " cmdsize " << bd.cmdsize; 9102 if (bd.cmdsize != 9103 sizeof(struct MachO::build_version_command) + 9104 bd.ntools * sizeof(struct MachO::build_tool_version)) 9105 outs() << " Incorrect size\n"; 9106 else 9107 outs() << "\n"; 9108 outs() << " platform " << MachOObjectFile::getBuildPlatform(bd.platform) 9109 << "\n"; 9110 if (bd.sdk) 9111 outs() << " sdk " << MachOObjectFile::getVersionString(bd.sdk) 9112 << "\n"; 9113 else 9114 outs() << " sdk n/a\n"; 9115 outs() << " minos " << MachOObjectFile::getVersionString(bd.minos) 9116 << "\n"; 9117 outs() << " ntools " << bd.ntools << "\n"; 9118 for (unsigned i = 0; i < bd.ntools; ++i) { 9119 MachO::build_tool_version bv = obj->getBuildToolVersion(i); 9120 PrintBuildToolVersion(bv); 9121 } 9122 } 9123 9124 static void PrintSourceVersionCommand(MachO::source_version_command sd) { 9125 outs() << " cmd LC_SOURCE_VERSION\n"; 9126 outs() << " cmdsize " << sd.cmdsize; 9127 if (sd.cmdsize != sizeof(struct MachO::source_version_command)) 9128 outs() << " Incorrect size\n"; 9129 else 9130 outs() << "\n"; 9131 uint64_t a = (sd.version >> 40) & 0xffffff; 9132 uint64_t b = (sd.version >> 30) & 0x3ff; 9133 uint64_t c = (sd.version >> 20) & 0x3ff; 9134 uint64_t d = (sd.version >> 10) & 0x3ff; 9135 uint64_t e = sd.version & 0x3ff; 9136 outs() << " version " << a << "." << b; 9137 if (e != 0) 9138 outs() << "." << c << "." << d << "." << e; 9139 else if (d != 0) 9140 outs() << "." << c << "." << d; 9141 else if (c != 0) 9142 outs() << "." << c; 9143 outs() << "\n"; 9144 } 9145 9146 static void PrintEntryPointCommand(MachO::entry_point_command ep) { 9147 outs() << " cmd LC_MAIN\n"; 9148 outs() << " cmdsize " << ep.cmdsize; 9149 if (ep.cmdsize != sizeof(struct MachO::entry_point_command)) 9150 outs() << " Incorrect size\n"; 9151 else 9152 outs() << "\n"; 9153 outs() << " entryoff " << ep.entryoff << "\n"; 9154 outs() << " stacksize " << ep.stacksize << "\n"; 9155 } 9156 9157 static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec, 9158 uint32_t object_size) { 9159 outs() << " cmd LC_ENCRYPTION_INFO\n"; 9160 outs() << " cmdsize " << ec.cmdsize; 9161 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command)) 9162 outs() << " Incorrect size\n"; 9163 else 9164 outs() << "\n"; 9165 outs() << " cryptoff " << ec.cryptoff; 9166 if (ec.cryptoff > object_size) 9167 outs() << " (past end of file)\n"; 9168 else 9169 outs() << "\n"; 9170 outs() << " cryptsize " << ec.cryptsize; 9171 if (ec.cryptsize > object_size) 9172 outs() << " (past end of file)\n"; 9173 else 9174 outs() << "\n"; 9175 outs() << " cryptid " << ec.cryptid << "\n"; 9176 } 9177 9178 static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec, 9179 uint32_t object_size) { 9180 outs() << " cmd LC_ENCRYPTION_INFO_64\n"; 9181 outs() << " cmdsize " << ec.cmdsize; 9182 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64)) 9183 outs() << " Incorrect size\n"; 9184 else 9185 outs() << "\n"; 9186 outs() << " cryptoff " << ec.cryptoff; 9187 if (ec.cryptoff > object_size) 9188 outs() << " (past end of file)\n"; 9189 else 9190 outs() << "\n"; 9191 outs() << " cryptsize " << ec.cryptsize; 9192 if (ec.cryptsize > object_size) 9193 outs() << " (past end of file)\n"; 9194 else 9195 outs() << "\n"; 9196 outs() << " cryptid " << ec.cryptid << "\n"; 9197 outs() << " pad " << ec.pad << "\n"; 9198 } 9199 9200 static void PrintLinkerOptionCommand(MachO::linker_option_command lo, 9201 const char *Ptr) { 9202 outs() << " cmd LC_LINKER_OPTION\n"; 9203 outs() << " cmdsize " << lo.cmdsize; 9204 if (lo.cmdsize < sizeof(struct MachO::linker_option_command)) 9205 outs() << " Incorrect size\n"; 9206 else 9207 outs() << "\n"; 9208 outs() << " count " << lo.count << "\n"; 9209 const char *string = Ptr + sizeof(struct MachO::linker_option_command); 9210 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command); 9211 uint32_t i = 0; 9212 while (left > 0) { 9213 while (*string == '\0' && left > 0) { 9214 string++; 9215 left--; 9216 } 9217 if (left > 0) { 9218 i++; 9219 outs() << " string #" << i << " " << format("%.*s\n", left, string); 9220 uint32_t NullPos = StringRef(string, left).find('\0'); 9221 uint32_t len = std::min(NullPos, left) + 1; 9222 string += len; 9223 left -= len; 9224 } 9225 } 9226 if (lo.count != i) 9227 outs() << " count " << lo.count << " does not match number of strings " 9228 << i << "\n"; 9229 } 9230 9231 static void PrintSubFrameworkCommand(MachO::sub_framework_command sub, 9232 const char *Ptr) { 9233 outs() << " cmd LC_SUB_FRAMEWORK\n"; 9234 outs() << " cmdsize " << sub.cmdsize; 9235 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command)) 9236 outs() << " Incorrect size\n"; 9237 else 9238 outs() << "\n"; 9239 if (sub.umbrella < sub.cmdsize) { 9240 const char *P = Ptr + sub.umbrella; 9241 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n"; 9242 } else { 9243 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n"; 9244 } 9245 } 9246 9247 static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub, 9248 const char *Ptr) { 9249 outs() << " cmd LC_SUB_UMBRELLA\n"; 9250 outs() << " cmdsize " << sub.cmdsize; 9251 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command)) 9252 outs() << " Incorrect size\n"; 9253 else 9254 outs() << "\n"; 9255 if (sub.sub_umbrella < sub.cmdsize) { 9256 const char *P = Ptr + sub.sub_umbrella; 9257 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n"; 9258 } else { 9259 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n"; 9260 } 9261 } 9262 9263 static void PrintSubLibraryCommand(MachO::sub_library_command sub, 9264 const char *Ptr) { 9265 outs() << " cmd LC_SUB_LIBRARY\n"; 9266 outs() << " cmdsize " << sub.cmdsize; 9267 if (sub.cmdsize < sizeof(struct MachO::sub_library_command)) 9268 outs() << " Incorrect size\n"; 9269 else 9270 outs() << "\n"; 9271 if (sub.sub_library < sub.cmdsize) { 9272 const char *P = Ptr + sub.sub_library; 9273 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n"; 9274 } else { 9275 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n"; 9276 } 9277 } 9278 9279 static void PrintSubClientCommand(MachO::sub_client_command sub, 9280 const char *Ptr) { 9281 outs() << " cmd LC_SUB_CLIENT\n"; 9282 outs() << " cmdsize " << sub.cmdsize; 9283 if (sub.cmdsize < sizeof(struct MachO::sub_client_command)) 9284 outs() << " Incorrect size\n"; 9285 else 9286 outs() << "\n"; 9287 if (sub.client < sub.cmdsize) { 9288 const char *P = Ptr + sub.client; 9289 outs() << " client " << P << " (offset " << sub.client << ")\n"; 9290 } else { 9291 outs() << " client ?(bad offset " << sub.client << ")\n"; 9292 } 9293 } 9294 9295 static void PrintRoutinesCommand(MachO::routines_command r) { 9296 outs() << " cmd LC_ROUTINES\n"; 9297 outs() << " cmdsize " << r.cmdsize; 9298 if (r.cmdsize != sizeof(struct MachO::routines_command)) 9299 outs() << " Incorrect size\n"; 9300 else 9301 outs() << "\n"; 9302 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n"; 9303 outs() << " init_module " << r.init_module << "\n"; 9304 outs() << " reserved1 " << r.reserved1 << "\n"; 9305 outs() << " reserved2 " << r.reserved2 << "\n"; 9306 outs() << " reserved3 " << r.reserved3 << "\n"; 9307 outs() << " reserved4 " << r.reserved4 << "\n"; 9308 outs() << " reserved5 " << r.reserved5 << "\n"; 9309 outs() << " reserved6 " << r.reserved6 << "\n"; 9310 } 9311 9312 static void PrintRoutinesCommand64(MachO::routines_command_64 r) { 9313 outs() << " cmd LC_ROUTINES_64\n"; 9314 outs() << " cmdsize " << r.cmdsize; 9315 if (r.cmdsize != sizeof(struct MachO::routines_command_64)) 9316 outs() << " Incorrect size\n"; 9317 else 9318 outs() << "\n"; 9319 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n"; 9320 outs() << " init_module " << r.init_module << "\n"; 9321 outs() << " reserved1 " << r.reserved1 << "\n"; 9322 outs() << " reserved2 " << r.reserved2 << "\n"; 9323 outs() << " reserved3 " << r.reserved3 << "\n"; 9324 outs() << " reserved4 " << r.reserved4 << "\n"; 9325 outs() << " reserved5 " << r.reserved5 << "\n"; 9326 outs() << " reserved6 " << r.reserved6 << "\n"; 9327 } 9328 9329 static void Print_x86_thread_state32_t(MachO::x86_thread_state32_t &cpu32) { 9330 outs() << "\t eax " << format("0x%08" PRIx32, cpu32.eax); 9331 outs() << " ebx " << format("0x%08" PRIx32, cpu32.ebx); 9332 outs() << " ecx " << format("0x%08" PRIx32, cpu32.ecx); 9333 outs() << " edx " << format("0x%08" PRIx32, cpu32.edx) << "\n"; 9334 outs() << "\t edi " << format("0x%08" PRIx32, cpu32.edi); 9335 outs() << " esi " << format("0x%08" PRIx32, cpu32.esi); 9336 outs() << " ebp " << format("0x%08" PRIx32, cpu32.ebp); 9337 outs() << " esp " << format("0x%08" PRIx32, cpu32.esp) << "\n"; 9338 outs() << "\t ss " << format("0x%08" PRIx32, cpu32.ss); 9339 outs() << " eflags " << format("0x%08" PRIx32, cpu32.eflags); 9340 outs() << " eip " << format("0x%08" PRIx32, cpu32.eip); 9341 outs() << " cs " << format("0x%08" PRIx32, cpu32.cs) << "\n"; 9342 outs() << "\t ds " << format("0x%08" PRIx32, cpu32.ds); 9343 outs() << " es " << format("0x%08" PRIx32, cpu32.es); 9344 outs() << " fs " << format("0x%08" PRIx32, cpu32.fs); 9345 outs() << " gs " << format("0x%08" PRIx32, cpu32.gs) << "\n"; 9346 } 9347 9348 static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) { 9349 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax); 9350 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx); 9351 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n"; 9352 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx); 9353 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi); 9354 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n"; 9355 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp); 9356 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp); 9357 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n"; 9358 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9); 9359 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10); 9360 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n"; 9361 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12); 9362 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13); 9363 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n"; 9364 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15); 9365 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n"; 9366 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags); 9367 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs); 9368 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n"; 9369 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n"; 9370 } 9371 9372 static void Print_mmst_reg(MachO::mmst_reg_t &r) { 9373 uint32_t f; 9374 outs() << "\t mmst_reg "; 9375 for (f = 0; f < 10; f++) 9376 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " "; 9377 outs() << "\n"; 9378 outs() << "\t mmst_rsrv "; 9379 for (f = 0; f < 6; f++) 9380 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " "; 9381 outs() << "\n"; 9382 } 9383 9384 static void Print_xmm_reg(MachO::xmm_reg_t &r) { 9385 uint32_t f; 9386 outs() << "\t xmm_reg "; 9387 for (f = 0; f < 16; f++) 9388 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " "; 9389 outs() << "\n"; 9390 } 9391 9392 static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) { 9393 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0]; 9394 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n"; 9395 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid; 9396 outs() << " denorm " << fpu.fpu_fcw.denorm; 9397 outs() << " zdiv " << fpu.fpu_fcw.zdiv; 9398 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl; 9399 outs() << " undfl " << fpu.fpu_fcw.undfl; 9400 outs() << " precis " << fpu.fpu_fcw.precis << "\n"; 9401 outs() << "\t\t pc "; 9402 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B) 9403 outs() << "FP_PREC_24B "; 9404 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B) 9405 outs() << "FP_PREC_53B "; 9406 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B) 9407 outs() << "FP_PREC_64B "; 9408 else 9409 outs() << fpu.fpu_fcw.pc << " "; 9410 outs() << "rc "; 9411 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR) 9412 outs() << "FP_RND_NEAR "; 9413 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN) 9414 outs() << "FP_RND_DOWN "; 9415 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP) 9416 outs() << "FP_RND_UP "; 9417 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP) 9418 outs() << "FP_CHOP "; 9419 outs() << "\n"; 9420 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid; 9421 outs() << " denorm " << fpu.fpu_fsw.denorm; 9422 outs() << " zdiv " << fpu.fpu_fsw.zdiv; 9423 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl; 9424 outs() << " undfl " << fpu.fpu_fsw.undfl; 9425 outs() << " precis " << fpu.fpu_fsw.precis; 9426 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n"; 9427 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm; 9428 outs() << " c0 " << fpu.fpu_fsw.c0; 9429 outs() << " c1 " << fpu.fpu_fsw.c1; 9430 outs() << " c2 " << fpu.fpu_fsw.c2; 9431 outs() << " tos " << fpu.fpu_fsw.tos; 9432 outs() << " c3 " << fpu.fpu_fsw.c3; 9433 outs() << " busy " << fpu.fpu_fsw.busy << "\n"; 9434 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw); 9435 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1); 9436 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop); 9437 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n"; 9438 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs); 9439 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2); 9440 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp); 9441 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n"; 9442 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3); 9443 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr); 9444 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask); 9445 outs() << "\n"; 9446 outs() << "\t fpu_stmm0:\n"; 9447 Print_mmst_reg(fpu.fpu_stmm0); 9448 outs() << "\t fpu_stmm1:\n"; 9449 Print_mmst_reg(fpu.fpu_stmm1); 9450 outs() << "\t fpu_stmm2:\n"; 9451 Print_mmst_reg(fpu.fpu_stmm2); 9452 outs() << "\t fpu_stmm3:\n"; 9453 Print_mmst_reg(fpu.fpu_stmm3); 9454 outs() << "\t fpu_stmm4:\n"; 9455 Print_mmst_reg(fpu.fpu_stmm4); 9456 outs() << "\t fpu_stmm5:\n"; 9457 Print_mmst_reg(fpu.fpu_stmm5); 9458 outs() << "\t fpu_stmm6:\n"; 9459 Print_mmst_reg(fpu.fpu_stmm6); 9460 outs() << "\t fpu_stmm7:\n"; 9461 Print_mmst_reg(fpu.fpu_stmm7); 9462 outs() << "\t fpu_xmm0:\n"; 9463 Print_xmm_reg(fpu.fpu_xmm0); 9464 outs() << "\t fpu_xmm1:\n"; 9465 Print_xmm_reg(fpu.fpu_xmm1); 9466 outs() << "\t fpu_xmm2:\n"; 9467 Print_xmm_reg(fpu.fpu_xmm2); 9468 outs() << "\t fpu_xmm3:\n"; 9469 Print_xmm_reg(fpu.fpu_xmm3); 9470 outs() << "\t fpu_xmm4:\n"; 9471 Print_xmm_reg(fpu.fpu_xmm4); 9472 outs() << "\t fpu_xmm5:\n"; 9473 Print_xmm_reg(fpu.fpu_xmm5); 9474 outs() << "\t fpu_xmm6:\n"; 9475 Print_xmm_reg(fpu.fpu_xmm6); 9476 outs() << "\t fpu_xmm7:\n"; 9477 Print_xmm_reg(fpu.fpu_xmm7); 9478 outs() << "\t fpu_xmm8:\n"; 9479 Print_xmm_reg(fpu.fpu_xmm8); 9480 outs() << "\t fpu_xmm9:\n"; 9481 Print_xmm_reg(fpu.fpu_xmm9); 9482 outs() << "\t fpu_xmm10:\n"; 9483 Print_xmm_reg(fpu.fpu_xmm10); 9484 outs() << "\t fpu_xmm11:\n"; 9485 Print_xmm_reg(fpu.fpu_xmm11); 9486 outs() << "\t fpu_xmm12:\n"; 9487 Print_xmm_reg(fpu.fpu_xmm12); 9488 outs() << "\t fpu_xmm13:\n"; 9489 Print_xmm_reg(fpu.fpu_xmm13); 9490 outs() << "\t fpu_xmm14:\n"; 9491 Print_xmm_reg(fpu.fpu_xmm14); 9492 outs() << "\t fpu_xmm15:\n"; 9493 Print_xmm_reg(fpu.fpu_xmm15); 9494 outs() << "\t fpu_rsrv4:\n"; 9495 for (uint32_t f = 0; f < 6; f++) { 9496 outs() << "\t "; 9497 for (uint32_t g = 0; g < 16; g++) 9498 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " "; 9499 outs() << "\n"; 9500 } 9501 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1); 9502 outs() << "\n"; 9503 } 9504 9505 static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) { 9506 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno); 9507 outs() << " err " << format("0x%08" PRIx32, exc64.err); 9508 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n"; 9509 } 9510 9511 static void Print_arm_thread_state32_t(MachO::arm_thread_state32_t &cpu32) { 9512 outs() << "\t r0 " << format("0x%08" PRIx32, cpu32.r[0]); 9513 outs() << " r1 " << format("0x%08" PRIx32, cpu32.r[1]); 9514 outs() << " r2 " << format("0x%08" PRIx32, cpu32.r[2]); 9515 outs() << " r3 " << format("0x%08" PRIx32, cpu32.r[3]) << "\n"; 9516 outs() << "\t r4 " << format("0x%08" PRIx32, cpu32.r[4]); 9517 outs() << " r5 " << format("0x%08" PRIx32, cpu32.r[5]); 9518 outs() << " r6 " << format("0x%08" PRIx32, cpu32.r[6]); 9519 outs() << " r7 " << format("0x%08" PRIx32, cpu32.r[7]) << "\n"; 9520 outs() << "\t r8 " << format("0x%08" PRIx32, cpu32.r[8]); 9521 outs() << " r9 " << format("0x%08" PRIx32, cpu32.r[9]); 9522 outs() << " r10 " << format("0x%08" PRIx32, cpu32.r[10]); 9523 outs() << " r11 " << format("0x%08" PRIx32, cpu32.r[11]) << "\n"; 9524 outs() << "\t r12 " << format("0x%08" PRIx32, cpu32.r[12]); 9525 outs() << " sp " << format("0x%08" PRIx32, cpu32.sp); 9526 outs() << " lr " << format("0x%08" PRIx32, cpu32.lr); 9527 outs() << " pc " << format("0x%08" PRIx32, cpu32.pc) << "\n"; 9528 outs() << "\t cpsr " << format("0x%08" PRIx32, cpu32.cpsr) << "\n"; 9529 } 9530 9531 static void Print_arm_thread_state64_t(MachO::arm_thread_state64_t &cpu64) { 9532 outs() << "\t x0 " << format("0x%016" PRIx64, cpu64.x[0]); 9533 outs() << " x1 " << format("0x%016" PRIx64, cpu64.x[1]); 9534 outs() << " x2 " << format("0x%016" PRIx64, cpu64.x[2]) << "\n"; 9535 outs() << "\t x3 " << format("0x%016" PRIx64, cpu64.x[3]); 9536 outs() << " x4 " << format("0x%016" PRIx64, cpu64.x[4]); 9537 outs() << " x5 " << format("0x%016" PRIx64, cpu64.x[5]) << "\n"; 9538 outs() << "\t x6 " << format("0x%016" PRIx64, cpu64.x[6]); 9539 outs() << " x7 " << format("0x%016" PRIx64, cpu64.x[7]); 9540 outs() << " x8 " << format("0x%016" PRIx64, cpu64.x[8]) << "\n"; 9541 outs() << "\t x9 " << format("0x%016" PRIx64, cpu64.x[9]); 9542 outs() << " x10 " << format("0x%016" PRIx64, cpu64.x[10]); 9543 outs() << " x11 " << format("0x%016" PRIx64, cpu64.x[11]) << "\n"; 9544 outs() << "\t x12 " << format("0x%016" PRIx64, cpu64.x[12]); 9545 outs() << " x13 " << format("0x%016" PRIx64, cpu64.x[13]); 9546 outs() << " x14 " << format("0x%016" PRIx64, cpu64.x[14]) << "\n"; 9547 outs() << "\t x15 " << format("0x%016" PRIx64, cpu64.x[15]); 9548 outs() << " x16 " << format("0x%016" PRIx64, cpu64.x[16]); 9549 outs() << " x17 " << format("0x%016" PRIx64, cpu64.x[17]) << "\n"; 9550 outs() << "\t x18 " << format("0x%016" PRIx64, cpu64.x[18]); 9551 outs() << " x19 " << format("0x%016" PRIx64, cpu64.x[19]); 9552 outs() << " x20 " << format("0x%016" PRIx64, cpu64.x[20]) << "\n"; 9553 outs() << "\t x21 " << format("0x%016" PRIx64, cpu64.x[21]); 9554 outs() << " x22 " << format("0x%016" PRIx64, cpu64.x[22]); 9555 outs() << " x23 " << format("0x%016" PRIx64, cpu64.x[23]) << "\n"; 9556 outs() << "\t x24 " << format("0x%016" PRIx64, cpu64.x[24]); 9557 outs() << " x25 " << format("0x%016" PRIx64, cpu64.x[25]); 9558 outs() << " x26 " << format("0x%016" PRIx64, cpu64.x[26]) << "\n"; 9559 outs() << "\t x27 " << format("0x%016" PRIx64, cpu64.x[27]); 9560 outs() << " x28 " << format("0x%016" PRIx64, cpu64.x[28]); 9561 outs() << " fp " << format("0x%016" PRIx64, cpu64.fp) << "\n"; 9562 outs() << "\t lr " << format("0x%016" PRIx64, cpu64.lr); 9563 outs() << " sp " << format("0x%016" PRIx64, cpu64.sp); 9564 outs() << " pc " << format("0x%016" PRIx64, cpu64.pc) << "\n"; 9565 outs() << "\t cpsr " << format("0x%08" PRIx32, cpu64.cpsr) << "\n"; 9566 } 9567 9568 static void PrintThreadCommand(MachO::thread_command t, const char *Ptr, 9569 bool isLittleEndian, uint32_t cputype) { 9570 if (t.cmd == MachO::LC_THREAD) 9571 outs() << " cmd LC_THREAD\n"; 9572 else if (t.cmd == MachO::LC_UNIXTHREAD) 9573 outs() << " cmd LC_UNIXTHREAD\n"; 9574 else 9575 outs() << " cmd " << t.cmd << " (unknown)\n"; 9576 outs() << " cmdsize " << t.cmdsize; 9577 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t)) 9578 outs() << " Incorrect size\n"; 9579 else 9580 outs() << "\n"; 9581 9582 const char *begin = Ptr + sizeof(struct MachO::thread_command); 9583 const char *end = Ptr + t.cmdsize; 9584 uint32_t flavor, count, left; 9585 if (cputype == MachO::CPU_TYPE_I386) { 9586 while (begin < end) { 9587 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9588 memcpy((char *)&flavor, begin, sizeof(uint32_t)); 9589 begin += sizeof(uint32_t); 9590 } else { 9591 flavor = 0; 9592 begin = end; 9593 } 9594 if (isLittleEndian != sys::IsLittleEndianHost) 9595 sys::swapByteOrder(flavor); 9596 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9597 memcpy((char *)&count, begin, sizeof(uint32_t)); 9598 begin += sizeof(uint32_t); 9599 } else { 9600 count = 0; 9601 begin = end; 9602 } 9603 if (isLittleEndian != sys::IsLittleEndianHost) 9604 sys::swapByteOrder(count); 9605 if (flavor == MachO::x86_THREAD_STATE32) { 9606 outs() << " flavor i386_THREAD_STATE\n"; 9607 if (count == MachO::x86_THREAD_STATE32_COUNT) 9608 outs() << " count i386_THREAD_STATE_COUNT\n"; 9609 else 9610 outs() << " count " << count 9611 << " (not x86_THREAD_STATE32_COUNT)\n"; 9612 MachO::x86_thread_state32_t cpu32; 9613 left = end - begin; 9614 if (left >= sizeof(MachO::x86_thread_state32_t)) { 9615 memcpy(&cpu32, begin, sizeof(MachO::x86_thread_state32_t)); 9616 begin += sizeof(MachO::x86_thread_state32_t); 9617 } else { 9618 memset(&cpu32, '\0', sizeof(MachO::x86_thread_state32_t)); 9619 memcpy(&cpu32, begin, left); 9620 begin += left; 9621 } 9622 if (isLittleEndian != sys::IsLittleEndianHost) 9623 swapStruct(cpu32); 9624 Print_x86_thread_state32_t(cpu32); 9625 } else if (flavor == MachO::x86_THREAD_STATE) { 9626 outs() << " flavor x86_THREAD_STATE\n"; 9627 if (count == MachO::x86_THREAD_STATE_COUNT) 9628 outs() << " count x86_THREAD_STATE_COUNT\n"; 9629 else 9630 outs() << " count " << count 9631 << " (not x86_THREAD_STATE_COUNT)\n"; 9632 struct MachO::x86_thread_state_t ts; 9633 left = end - begin; 9634 if (left >= sizeof(MachO::x86_thread_state_t)) { 9635 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t)); 9636 begin += sizeof(MachO::x86_thread_state_t); 9637 } else { 9638 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t)); 9639 memcpy(&ts, begin, left); 9640 begin += left; 9641 } 9642 if (isLittleEndian != sys::IsLittleEndianHost) 9643 swapStruct(ts); 9644 if (ts.tsh.flavor == MachO::x86_THREAD_STATE32) { 9645 outs() << "\t tsh.flavor x86_THREAD_STATE32 "; 9646 if (ts.tsh.count == MachO::x86_THREAD_STATE32_COUNT) 9647 outs() << "tsh.count x86_THREAD_STATE32_COUNT\n"; 9648 else 9649 outs() << "tsh.count " << ts.tsh.count 9650 << " (not x86_THREAD_STATE32_COUNT\n"; 9651 Print_x86_thread_state32_t(ts.uts.ts32); 9652 } else { 9653 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count " 9654 << ts.tsh.count << "\n"; 9655 } 9656 } else { 9657 outs() << " flavor " << flavor << " (unknown)\n"; 9658 outs() << " count " << count << "\n"; 9659 outs() << " state (unknown)\n"; 9660 begin += count * sizeof(uint32_t); 9661 } 9662 } 9663 } else if (cputype == MachO::CPU_TYPE_X86_64) { 9664 while (begin < end) { 9665 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9666 memcpy((char *)&flavor, begin, sizeof(uint32_t)); 9667 begin += sizeof(uint32_t); 9668 } else { 9669 flavor = 0; 9670 begin = end; 9671 } 9672 if (isLittleEndian != sys::IsLittleEndianHost) 9673 sys::swapByteOrder(flavor); 9674 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9675 memcpy((char *)&count, begin, sizeof(uint32_t)); 9676 begin += sizeof(uint32_t); 9677 } else { 9678 count = 0; 9679 begin = end; 9680 } 9681 if (isLittleEndian != sys::IsLittleEndianHost) 9682 sys::swapByteOrder(count); 9683 if (flavor == MachO::x86_THREAD_STATE64) { 9684 outs() << " flavor x86_THREAD_STATE64\n"; 9685 if (count == MachO::x86_THREAD_STATE64_COUNT) 9686 outs() << " count x86_THREAD_STATE64_COUNT\n"; 9687 else 9688 outs() << " count " << count 9689 << " (not x86_THREAD_STATE64_COUNT)\n"; 9690 MachO::x86_thread_state64_t cpu64; 9691 left = end - begin; 9692 if (left >= sizeof(MachO::x86_thread_state64_t)) { 9693 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t)); 9694 begin += sizeof(MachO::x86_thread_state64_t); 9695 } else { 9696 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t)); 9697 memcpy(&cpu64, begin, left); 9698 begin += left; 9699 } 9700 if (isLittleEndian != sys::IsLittleEndianHost) 9701 swapStruct(cpu64); 9702 Print_x86_thread_state64_t(cpu64); 9703 } else if (flavor == MachO::x86_THREAD_STATE) { 9704 outs() << " flavor x86_THREAD_STATE\n"; 9705 if (count == MachO::x86_THREAD_STATE_COUNT) 9706 outs() << " count x86_THREAD_STATE_COUNT\n"; 9707 else 9708 outs() << " count " << count 9709 << " (not x86_THREAD_STATE_COUNT)\n"; 9710 struct MachO::x86_thread_state_t ts; 9711 left = end - begin; 9712 if (left >= sizeof(MachO::x86_thread_state_t)) { 9713 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t)); 9714 begin += sizeof(MachO::x86_thread_state_t); 9715 } else { 9716 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t)); 9717 memcpy(&ts, begin, left); 9718 begin += left; 9719 } 9720 if (isLittleEndian != sys::IsLittleEndianHost) 9721 swapStruct(ts); 9722 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) { 9723 outs() << "\t tsh.flavor x86_THREAD_STATE64 "; 9724 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT) 9725 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n"; 9726 else 9727 outs() << "tsh.count " << ts.tsh.count 9728 << " (not x86_THREAD_STATE64_COUNT\n"; 9729 Print_x86_thread_state64_t(ts.uts.ts64); 9730 } else { 9731 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count " 9732 << ts.tsh.count << "\n"; 9733 } 9734 } else if (flavor == MachO::x86_FLOAT_STATE) { 9735 outs() << " flavor x86_FLOAT_STATE\n"; 9736 if (count == MachO::x86_FLOAT_STATE_COUNT) 9737 outs() << " count x86_FLOAT_STATE_COUNT\n"; 9738 else 9739 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n"; 9740 struct MachO::x86_float_state_t fs; 9741 left = end - begin; 9742 if (left >= sizeof(MachO::x86_float_state_t)) { 9743 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t)); 9744 begin += sizeof(MachO::x86_float_state_t); 9745 } else { 9746 memset(&fs, '\0', sizeof(MachO::x86_float_state_t)); 9747 memcpy(&fs, begin, left); 9748 begin += left; 9749 } 9750 if (isLittleEndian != sys::IsLittleEndianHost) 9751 swapStruct(fs); 9752 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) { 9753 outs() << "\t fsh.flavor x86_FLOAT_STATE64 "; 9754 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT) 9755 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n"; 9756 else 9757 outs() << "fsh.count " << fs.fsh.count 9758 << " (not x86_FLOAT_STATE64_COUNT\n"; 9759 Print_x86_float_state_t(fs.ufs.fs64); 9760 } else { 9761 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count " 9762 << fs.fsh.count << "\n"; 9763 } 9764 } else if (flavor == MachO::x86_EXCEPTION_STATE) { 9765 outs() << " flavor x86_EXCEPTION_STATE\n"; 9766 if (count == MachO::x86_EXCEPTION_STATE_COUNT) 9767 outs() << " count x86_EXCEPTION_STATE_COUNT\n"; 9768 else 9769 outs() << " count " << count 9770 << " (not x86_EXCEPTION_STATE_COUNT)\n"; 9771 struct MachO::x86_exception_state_t es; 9772 left = end - begin; 9773 if (left >= sizeof(MachO::x86_exception_state_t)) { 9774 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t)); 9775 begin += sizeof(MachO::x86_exception_state_t); 9776 } else { 9777 memset(&es, '\0', sizeof(MachO::x86_exception_state_t)); 9778 memcpy(&es, begin, left); 9779 begin += left; 9780 } 9781 if (isLittleEndian != sys::IsLittleEndianHost) 9782 swapStruct(es); 9783 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) { 9784 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n"; 9785 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT) 9786 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n"; 9787 else 9788 outs() << "\t esh.count " << es.esh.count 9789 << " (not x86_EXCEPTION_STATE64_COUNT\n"; 9790 Print_x86_exception_state_t(es.ues.es64); 9791 } else { 9792 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count " 9793 << es.esh.count << "\n"; 9794 } 9795 } else if (flavor == MachO::x86_EXCEPTION_STATE64) { 9796 outs() << " flavor x86_EXCEPTION_STATE64\n"; 9797 if (count == MachO::x86_EXCEPTION_STATE64_COUNT) 9798 outs() << " count x86_EXCEPTION_STATE64_COUNT\n"; 9799 else 9800 outs() << " count " << count 9801 << " (not x86_EXCEPTION_STATE64_COUNT)\n"; 9802 struct MachO::x86_exception_state64_t es64; 9803 left = end - begin; 9804 if (left >= sizeof(MachO::x86_exception_state64_t)) { 9805 memcpy(&es64, begin, sizeof(MachO::x86_exception_state64_t)); 9806 begin += sizeof(MachO::x86_exception_state64_t); 9807 } else { 9808 memset(&es64, '\0', sizeof(MachO::x86_exception_state64_t)); 9809 memcpy(&es64, begin, left); 9810 begin += left; 9811 } 9812 if (isLittleEndian != sys::IsLittleEndianHost) 9813 swapStruct(es64); 9814 Print_x86_exception_state_t(es64); 9815 } else { 9816 outs() << " flavor " << flavor << " (unknown)\n"; 9817 outs() << " count " << count << "\n"; 9818 outs() << " state (unknown)\n"; 9819 begin += count * sizeof(uint32_t); 9820 } 9821 } 9822 } else if (cputype == MachO::CPU_TYPE_ARM) { 9823 while (begin < end) { 9824 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9825 memcpy((char *)&flavor, begin, sizeof(uint32_t)); 9826 begin += sizeof(uint32_t); 9827 } else { 9828 flavor = 0; 9829 begin = end; 9830 } 9831 if (isLittleEndian != sys::IsLittleEndianHost) 9832 sys::swapByteOrder(flavor); 9833 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9834 memcpy((char *)&count, begin, sizeof(uint32_t)); 9835 begin += sizeof(uint32_t); 9836 } else { 9837 count = 0; 9838 begin = end; 9839 } 9840 if (isLittleEndian != sys::IsLittleEndianHost) 9841 sys::swapByteOrder(count); 9842 if (flavor == MachO::ARM_THREAD_STATE) { 9843 outs() << " flavor ARM_THREAD_STATE\n"; 9844 if (count == MachO::ARM_THREAD_STATE_COUNT) 9845 outs() << " count ARM_THREAD_STATE_COUNT\n"; 9846 else 9847 outs() << " count " << count 9848 << " (not ARM_THREAD_STATE_COUNT)\n"; 9849 MachO::arm_thread_state32_t cpu32; 9850 left = end - begin; 9851 if (left >= sizeof(MachO::arm_thread_state32_t)) { 9852 memcpy(&cpu32, begin, sizeof(MachO::arm_thread_state32_t)); 9853 begin += sizeof(MachO::arm_thread_state32_t); 9854 } else { 9855 memset(&cpu32, '\0', sizeof(MachO::arm_thread_state32_t)); 9856 memcpy(&cpu32, begin, left); 9857 begin += left; 9858 } 9859 if (isLittleEndian != sys::IsLittleEndianHost) 9860 swapStruct(cpu32); 9861 Print_arm_thread_state32_t(cpu32); 9862 } else { 9863 outs() << " flavor " << flavor << " (unknown)\n"; 9864 outs() << " count " << count << "\n"; 9865 outs() << " state (unknown)\n"; 9866 begin += count * sizeof(uint32_t); 9867 } 9868 } 9869 } else if (cputype == MachO::CPU_TYPE_ARM64 || 9870 cputype == MachO::CPU_TYPE_ARM64_32) { 9871 while (begin < end) { 9872 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9873 memcpy((char *)&flavor, begin, sizeof(uint32_t)); 9874 begin += sizeof(uint32_t); 9875 } else { 9876 flavor = 0; 9877 begin = end; 9878 } 9879 if (isLittleEndian != sys::IsLittleEndianHost) 9880 sys::swapByteOrder(flavor); 9881 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9882 memcpy((char *)&count, begin, sizeof(uint32_t)); 9883 begin += sizeof(uint32_t); 9884 } else { 9885 count = 0; 9886 begin = end; 9887 } 9888 if (isLittleEndian != sys::IsLittleEndianHost) 9889 sys::swapByteOrder(count); 9890 if (flavor == MachO::ARM_THREAD_STATE64) { 9891 outs() << " flavor ARM_THREAD_STATE64\n"; 9892 if (count == MachO::ARM_THREAD_STATE64_COUNT) 9893 outs() << " count ARM_THREAD_STATE64_COUNT\n"; 9894 else 9895 outs() << " count " << count 9896 << " (not ARM_THREAD_STATE64_COUNT)\n"; 9897 MachO::arm_thread_state64_t cpu64; 9898 left = end - begin; 9899 if (left >= sizeof(MachO::arm_thread_state64_t)) { 9900 memcpy(&cpu64, begin, sizeof(MachO::arm_thread_state64_t)); 9901 begin += sizeof(MachO::arm_thread_state64_t); 9902 } else { 9903 memset(&cpu64, '\0', sizeof(MachO::arm_thread_state64_t)); 9904 memcpy(&cpu64, begin, left); 9905 begin += left; 9906 } 9907 if (isLittleEndian != sys::IsLittleEndianHost) 9908 swapStruct(cpu64); 9909 Print_arm_thread_state64_t(cpu64); 9910 } else { 9911 outs() << " flavor " << flavor << " (unknown)\n"; 9912 outs() << " count " << count << "\n"; 9913 outs() << " state (unknown)\n"; 9914 begin += count * sizeof(uint32_t); 9915 } 9916 } 9917 } else { 9918 while (begin < end) { 9919 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9920 memcpy((char *)&flavor, begin, sizeof(uint32_t)); 9921 begin += sizeof(uint32_t); 9922 } else { 9923 flavor = 0; 9924 begin = end; 9925 } 9926 if (isLittleEndian != sys::IsLittleEndianHost) 9927 sys::swapByteOrder(flavor); 9928 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9929 memcpy((char *)&count, begin, sizeof(uint32_t)); 9930 begin += sizeof(uint32_t); 9931 } else { 9932 count = 0; 9933 begin = end; 9934 } 9935 if (isLittleEndian != sys::IsLittleEndianHost) 9936 sys::swapByteOrder(count); 9937 outs() << " flavor " << flavor << "\n"; 9938 outs() << " count " << count << "\n"; 9939 outs() << " state (Unknown cputype/cpusubtype)\n"; 9940 begin += count * sizeof(uint32_t); 9941 } 9942 } 9943 } 9944 9945 static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) { 9946 if (dl.cmd == MachO::LC_ID_DYLIB) 9947 outs() << " cmd LC_ID_DYLIB\n"; 9948 else if (dl.cmd == MachO::LC_LOAD_DYLIB) 9949 outs() << " cmd LC_LOAD_DYLIB\n"; 9950 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB) 9951 outs() << " cmd LC_LOAD_WEAK_DYLIB\n"; 9952 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB) 9953 outs() << " cmd LC_REEXPORT_DYLIB\n"; 9954 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB) 9955 outs() << " cmd LC_LAZY_LOAD_DYLIB\n"; 9956 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB) 9957 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n"; 9958 else 9959 outs() << " cmd " << dl.cmd << " (unknown)\n"; 9960 outs() << " cmdsize " << dl.cmdsize; 9961 if (dl.cmdsize < sizeof(struct MachO::dylib_command)) 9962 outs() << " Incorrect size\n"; 9963 else 9964 outs() << "\n"; 9965 if (dl.dylib.name < dl.cmdsize) { 9966 const char *P = (const char *)(Ptr) + dl.dylib.name; 9967 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n"; 9968 } else { 9969 outs() << " name ?(bad offset " << dl.dylib.name << ")\n"; 9970 } 9971 outs() << " time stamp " << dl.dylib.timestamp << " "; 9972 time_t t = dl.dylib.timestamp; 9973 outs() << ctime(&t); 9974 outs() << " current version "; 9975 if (dl.dylib.current_version == 0xffffffff) 9976 outs() << "n/a\n"; 9977 else 9978 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "." 9979 << ((dl.dylib.current_version >> 8) & 0xff) << "." 9980 << (dl.dylib.current_version & 0xff) << "\n"; 9981 outs() << "compatibility version "; 9982 if (dl.dylib.compatibility_version == 0xffffffff) 9983 outs() << "n/a\n"; 9984 else 9985 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "." 9986 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "." 9987 << (dl.dylib.compatibility_version & 0xff) << "\n"; 9988 } 9989 9990 static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld, 9991 uint32_t object_size) { 9992 if (ld.cmd == MachO::LC_CODE_SIGNATURE) 9993 outs() << " cmd LC_CODE_SIGNATURE\n"; 9994 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO) 9995 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n"; 9996 else if (ld.cmd == MachO::LC_FUNCTION_STARTS) 9997 outs() << " cmd LC_FUNCTION_STARTS\n"; 9998 else if (ld.cmd == MachO::LC_DATA_IN_CODE) 9999 outs() << " cmd LC_DATA_IN_CODE\n"; 10000 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS) 10001 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n"; 10002 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) 10003 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n"; 10004 else 10005 outs() << " cmd " << ld.cmd << " (?)\n"; 10006 outs() << " cmdsize " << ld.cmdsize; 10007 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command)) 10008 outs() << " Incorrect size\n"; 10009 else 10010 outs() << "\n"; 10011 outs() << " dataoff " << ld.dataoff; 10012 if (ld.dataoff > object_size) 10013 outs() << " (past end of file)\n"; 10014 else 10015 outs() << "\n"; 10016 outs() << " datasize " << ld.datasize; 10017 uint64_t big_size = ld.dataoff; 10018 big_size += ld.datasize; 10019 if (big_size > object_size) 10020 outs() << " (past end of file)\n"; 10021 else 10022 outs() << "\n"; 10023 } 10024 10025 static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype, 10026 uint32_t cputype, bool verbose) { 10027 StringRef Buf = Obj->getData(); 10028 unsigned Index = 0; 10029 for (const auto &Command : Obj->load_commands()) { 10030 outs() << "Load command " << Index++ << "\n"; 10031 if (Command.C.cmd == MachO::LC_SEGMENT) { 10032 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command); 10033 const char *sg_segname = SLC.segname; 10034 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr, 10035 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot, 10036 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(), 10037 verbose); 10038 for (unsigned j = 0; j < SLC.nsects; j++) { 10039 MachO::section S = Obj->getSection(Command, j); 10040 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align, 10041 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2, 10042 SLC.cmd, sg_segname, filetype, Buf.size(), verbose); 10043 } 10044 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) { 10045 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command); 10046 const char *sg_segname = SLC_64.segname; 10047 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname, 10048 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff, 10049 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot, 10050 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose); 10051 for (unsigned j = 0; j < SLC_64.nsects; j++) { 10052 MachO::section_64 S_64 = Obj->getSection64(Command, j); 10053 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size, 10054 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc, 10055 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd, 10056 sg_segname, filetype, Buf.size(), verbose); 10057 } 10058 } else if (Command.C.cmd == MachO::LC_SYMTAB) { 10059 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand(); 10060 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size()); 10061 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) { 10062 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand(); 10063 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand(); 10064 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(), 10065 Obj->is64Bit()); 10066 } else if (Command.C.cmd == MachO::LC_DYLD_INFO || 10067 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) { 10068 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command); 10069 PrintDyldInfoLoadCommand(DyldInfo, Buf.size()); 10070 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER || 10071 Command.C.cmd == MachO::LC_ID_DYLINKER || 10072 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) { 10073 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command); 10074 PrintDyldLoadCommand(Dyld, Command.Ptr); 10075 } else if (Command.C.cmd == MachO::LC_UUID) { 10076 MachO::uuid_command Uuid = Obj->getUuidCommand(Command); 10077 PrintUuidLoadCommand(Uuid); 10078 } else if (Command.C.cmd == MachO::LC_RPATH) { 10079 MachO::rpath_command Rpath = Obj->getRpathCommand(Command); 10080 PrintRpathLoadCommand(Rpath, Command.Ptr); 10081 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX || 10082 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS || 10083 Command.C.cmd == MachO::LC_VERSION_MIN_TVOS || 10084 Command.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) { 10085 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command); 10086 PrintVersionMinLoadCommand(Vd); 10087 } else if (Command.C.cmd == MachO::LC_NOTE) { 10088 MachO::note_command Nt = Obj->getNoteLoadCommand(Command); 10089 PrintNoteLoadCommand(Nt); 10090 } else if (Command.C.cmd == MachO::LC_BUILD_VERSION) { 10091 MachO::build_version_command Bv = 10092 Obj->getBuildVersionLoadCommand(Command); 10093 PrintBuildVersionLoadCommand(Obj, Bv); 10094 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) { 10095 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command); 10096 PrintSourceVersionCommand(Sd); 10097 } else if (Command.C.cmd == MachO::LC_MAIN) { 10098 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command); 10099 PrintEntryPointCommand(Ep); 10100 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) { 10101 MachO::encryption_info_command Ei = 10102 Obj->getEncryptionInfoCommand(Command); 10103 PrintEncryptionInfoCommand(Ei, Buf.size()); 10104 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) { 10105 MachO::encryption_info_command_64 Ei = 10106 Obj->getEncryptionInfoCommand64(Command); 10107 PrintEncryptionInfoCommand64(Ei, Buf.size()); 10108 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) { 10109 MachO::linker_option_command Lo = 10110 Obj->getLinkerOptionLoadCommand(Command); 10111 PrintLinkerOptionCommand(Lo, Command.Ptr); 10112 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) { 10113 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command); 10114 PrintSubFrameworkCommand(Sf, Command.Ptr); 10115 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) { 10116 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command); 10117 PrintSubUmbrellaCommand(Sf, Command.Ptr); 10118 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) { 10119 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command); 10120 PrintSubLibraryCommand(Sl, Command.Ptr); 10121 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) { 10122 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command); 10123 PrintSubClientCommand(Sc, Command.Ptr); 10124 } else if (Command.C.cmd == MachO::LC_ROUTINES) { 10125 MachO::routines_command Rc = Obj->getRoutinesCommand(Command); 10126 PrintRoutinesCommand(Rc); 10127 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) { 10128 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command); 10129 PrintRoutinesCommand64(Rc); 10130 } else if (Command.C.cmd == MachO::LC_THREAD || 10131 Command.C.cmd == MachO::LC_UNIXTHREAD) { 10132 MachO::thread_command Tc = Obj->getThreadCommand(Command); 10133 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype); 10134 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB || 10135 Command.C.cmd == MachO::LC_ID_DYLIB || 10136 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB || 10137 Command.C.cmd == MachO::LC_REEXPORT_DYLIB || 10138 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB || 10139 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) { 10140 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command); 10141 PrintDylibCommand(Dl, Command.Ptr); 10142 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE || 10143 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO || 10144 Command.C.cmd == MachO::LC_FUNCTION_STARTS || 10145 Command.C.cmd == MachO::LC_DATA_IN_CODE || 10146 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS || 10147 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) { 10148 MachO::linkedit_data_command Ld = 10149 Obj->getLinkeditDataLoadCommand(Command); 10150 PrintLinkEditDataCommand(Ld, Buf.size()); 10151 } else { 10152 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd) 10153 << ")\n"; 10154 outs() << " cmdsize " << Command.C.cmdsize << "\n"; 10155 // TODO: get and print the raw bytes of the load command. 10156 } 10157 // TODO: print all the other kinds of load commands. 10158 } 10159 } 10160 10161 static void PrintMachHeader(const MachOObjectFile *Obj, bool verbose) { 10162 if (Obj->is64Bit()) { 10163 MachO::mach_header_64 H_64; 10164 H_64 = Obj->getHeader64(); 10165 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype, 10166 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose); 10167 } else { 10168 MachO::mach_header H; 10169 H = Obj->getHeader(); 10170 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds, 10171 H.sizeofcmds, H.flags, verbose); 10172 } 10173 } 10174 10175 void printMachOFileHeader(const object::ObjectFile *Obj) { 10176 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj); 10177 PrintMachHeader(file, !NonVerbose); 10178 } 10179 10180 void printMachOLoadCommands(const object::ObjectFile *Obj) { 10181 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj); 10182 uint32_t filetype = 0; 10183 uint32_t cputype = 0; 10184 if (file->is64Bit()) { 10185 MachO::mach_header_64 H_64; 10186 H_64 = file->getHeader64(); 10187 filetype = H_64.filetype; 10188 cputype = H_64.cputype; 10189 } else { 10190 MachO::mach_header H; 10191 H = file->getHeader(); 10192 filetype = H.filetype; 10193 cputype = H.cputype; 10194 } 10195 PrintLoadCommands(file, filetype, cputype, !NonVerbose); 10196 } 10197 10198 //===----------------------------------------------------------------------===// 10199 // export trie dumping 10200 //===----------------------------------------------------------------------===// 10201 10202 void printMachOExportsTrie(const object::MachOObjectFile *Obj) { 10203 uint64_t BaseSegmentAddress = 0; 10204 for (const auto &Command : Obj->load_commands()) { 10205 if (Command.C.cmd == MachO::LC_SEGMENT) { 10206 MachO::segment_command Seg = Obj->getSegmentLoadCommand(Command); 10207 if (Seg.fileoff == 0 && Seg.filesize != 0) { 10208 BaseSegmentAddress = Seg.vmaddr; 10209 break; 10210 } 10211 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) { 10212 MachO::segment_command_64 Seg = Obj->getSegment64LoadCommand(Command); 10213 if (Seg.fileoff == 0 && Seg.filesize != 0) { 10214 BaseSegmentAddress = Seg.vmaddr; 10215 break; 10216 } 10217 } 10218 } 10219 Error Err = Error::success(); 10220 for (const object::ExportEntry &Entry : Obj->exports(Err)) { 10221 uint64_t Flags = Entry.flags(); 10222 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT); 10223 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION); 10224 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) == 10225 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL); 10226 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) == 10227 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE); 10228 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER); 10229 if (ReExport) 10230 outs() << "[re-export] "; 10231 else 10232 outs() << format("0x%08llX ", 10233 Entry.address() + BaseSegmentAddress); 10234 outs() << Entry.name(); 10235 if (WeakDef || ThreadLocal || Resolver || Abs) { 10236 bool NeedsComma = false; 10237 outs() << " ["; 10238 if (WeakDef) { 10239 outs() << "weak_def"; 10240 NeedsComma = true; 10241 } 10242 if (ThreadLocal) { 10243 if (NeedsComma) 10244 outs() << ", "; 10245 outs() << "per-thread"; 10246 NeedsComma = true; 10247 } 10248 if (Abs) { 10249 if (NeedsComma) 10250 outs() << ", "; 10251 outs() << "absolute"; 10252 NeedsComma = true; 10253 } 10254 if (Resolver) { 10255 if (NeedsComma) 10256 outs() << ", "; 10257 outs() << format("resolver=0x%08llX", Entry.other()); 10258 NeedsComma = true; 10259 } 10260 outs() << "]"; 10261 } 10262 if (ReExport) { 10263 StringRef DylibName = "unknown"; 10264 int Ordinal = Entry.other() - 1; 10265 Obj->getLibraryShortNameByIndex(Ordinal, DylibName); 10266 if (Entry.otherName().empty()) 10267 outs() << " (from " << DylibName << ")"; 10268 else 10269 outs() << " (" << Entry.otherName() << " from " << DylibName << ")"; 10270 } 10271 outs() << "\n"; 10272 } 10273 if (Err) 10274 report_error(std::move(Err), Obj->getFileName()); 10275 } 10276 10277 //===----------------------------------------------------------------------===// 10278 // rebase table dumping 10279 //===----------------------------------------------------------------------===// 10280 10281 void printMachORebaseTable(object::MachOObjectFile *Obj) { 10282 outs() << "segment section address type\n"; 10283 Error Err = Error::success(); 10284 for (const object::MachORebaseEntry &Entry : Obj->rebaseTable(Err)) { 10285 StringRef SegmentName = Entry.segmentName(); 10286 StringRef SectionName = Entry.sectionName(); 10287 uint64_t Address = Entry.address(); 10288 10289 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer 10290 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n", 10291 SegmentName.str().c_str(), SectionName.str().c_str(), 10292 Address, Entry.typeName().str().c_str()); 10293 } 10294 if (Err) 10295 report_error(std::move(Err), Obj->getFileName()); 10296 } 10297 10298 static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) { 10299 StringRef DylibName; 10300 switch (Ordinal) { 10301 case MachO::BIND_SPECIAL_DYLIB_SELF: 10302 return "this-image"; 10303 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE: 10304 return "main-executable"; 10305 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP: 10306 return "flat-namespace"; 10307 default: 10308 if (Ordinal > 0) { 10309 std::error_code EC = 10310 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName); 10311 if (EC) 10312 return "<<bad library ordinal>>"; 10313 return DylibName; 10314 } 10315 } 10316 return "<<unknown special ordinal>>"; 10317 } 10318 10319 //===----------------------------------------------------------------------===// 10320 // bind table dumping 10321 //===----------------------------------------------------------------------===// 10322 10323 void printMachOBindTable(object::MachOObjectFile *Obj) { 10324 // Build table of sections so names can used in final output. 10325 outs() << "segment section address type " 10326 "addend dylib symbol\n"; 10327 Error Err = Error::success(); 10328 for (const object::MachOBindEntry &Entry : Obj->bindTable(Err)) { 10329 StringRef SegmentName = Entry.segmentName(); 10330 StringRef SectionName = Entry.sectionName(); 10331 uint64_t Address = Entry.address(); 10332 10333 // Table lines look like: 10334 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard 10335 StringRef Attr; 10336 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT) 10337 Attr = " (weak_import)"; 10338 outs() << left_justify(SegmentName, 8) << " " 10339 << left_justify(SectionName, 18) << " " 10340 << format_hex(Address, 10, true) << " " 10341 << left_justify(Entry.typeName(), 8) << " " 10342 << format_decimal(Entry.addend(), 8) << " " 10343 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " " 10344 << Entry.symbolName() << Attr << "\n"; 10345 } 10346 if (Err) 10347 report_error(std::move(Err), Obj->getFileName()); 10348 } 10349 10350 //===----------------------------------------------------------------------===// 10351 // lazy bind table dumping 10352 //===----------------------------------------------------------------------===// 10353 10354 void printMachOLazyBindTable(object::MachOObjectFile *Obj) { 10355 outs() << "segment section address " 10356 "dylib symbol\n"; 10357 Error Err = Error::success(); 10358 for (const object::MachOBindEntry &Entry : Obj->lazyBindTable(Err)) { 10359 StringRef SegmentName = Entry.segmentName(); 10360 StringRef SectionName = Entry.sectionName(); 10361 uint64_t Address = Entry.address(); 10362 10363 // Table lines look like: 10364 // __DATA __got 0x00012010 libSystem ___stack_chk_guard 10365 outs() << left_justify(SegmentName, 8) << " " 10366 << left_justify(SectionName, 18) << " " 10367 << format_hex(Address, 10, true) << " " 10368 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " " 10369 << Entry.symbolName() << "\n"; 10370 } 10371 if (Err) 10372 report_error(std::move(Err), Obj->getFileName()); 10373 } 10374 10375 //===----------------------------------------------------------------------===// 10376 // weak bind table dumping 10377 //===----------------------------------------------------------------------===// 10378 10379 void printMachOWeakBindTable(object::MachOObjectFile *Obj) { 10380 outs() << "segment section address " 10381 "type addend symbol\n"; 10382 Error Err = Error::success(); 10383 for (const object::MachOBindEntry &Entry : Obj->weakBindTable(Err)) { 10384 // Strong symbols don't have a location to update. 10385 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) { 10386 outs() << " strong " 10387 << Entry.symbolName() << "\n"; 10388 continue; 10389 } 10390 StringRef SegmentName = Entry.segmentName(); 10391 StringRef SectionName = Entry.sectionName(); 10392 uint64_t Address = Entry.address(); 10393 10394 // Table lines look like: 10395 // __DATA __data 0x00001000 pointer 0 _foo 10396 outs() << left_justify(SegmentName, 8) << " " 10397 << left_justify(SectionName, 18) << " " 10398 << format_hex(Address, 10, true) << " " 10399 << left_justify(Entry.typeName(), 8) << " " 10400 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName() 10401 << "\n"; 10402 } 10403 if (Err) 10404 report_error(std::move(Err), Obj->getFileName()); 10405 } 10406 10407 // get_dyld_bind_info_symbolname() is used for disassembly and passed an 10408 // address, ReferenceValue, in the Mach-O file and looks in the dyld bind 10409 // information for that address. If the address is found its binding symbol 10410 // name is returned. If not nullptr is returned. 10411 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue, 10412 struct DisassembleInfo *info) { 10413 if (info->bindtable == nullptr) { 10414 info->bindtable = std::make_unique<SymbolAddressMap>(); 10415 Error Err = Error::success(); 10416 for (const object::MachOBindEntry &Entry : info->O->bindTable(Err)) { 10417 uint64_t Address = Entry.address(); 10418 StringRef name = Entry.symbolName(); 10419 if (!name.empty()) 10420 (*info->bindtable)[Address] = name; 10421 } 10422 if (Err) 10423 report_error(std::move(Err), info->O->getFileName()); 10424 } 10425 auto name = info->bindtable->lookup(ReferenceValue); 10426 return !name.empty() ? name.data() : nullptr; 10427 } 10428 10429 void printLazyBindTable(ObjectFile *o) { 10430 outs() << "Lazy bind table:\n"; 10431 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 10432 printMachOLazyBindTable(MachO); 10433 else 10434 WithColor::error() 10435 << "This operation is only currently supported " 10436 "for Mach-O executable files.\n"; 10437 } 10438 10439 void printWeakBindTable(ObjectFile *o) { 10440 outs() << "Weak bind table:\n"; 10441 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 10442 printMachOWeakBindTable(MachO); 10443 else 10444 WithColor::error() 10445 << "This operation is only currently supported " 10446 "for Mach-O executable files.\n"; 10447 } 10448 10449 void printExportsTrie(const ObjectFile *o) { 10450 outs() << "Exports trie:\n"; 10451 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 10452 printMachOExportsTrie(MachO); 10453 else 10454 WithColor::error() 10455 << "This operation is only currently supported " 10456 "for Mach-O executable files.\n"; 10457 } 10458 10459 void printRebaseTable(ObjectFile *o) { 10460 outs() << "Rebase table:\n"; 10461 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 10462 printMachORebaseTable(MachO); 10463 else 10464 WithColor::error() 10465 << "This operation is only currently supported " 10466 "for Mach-O executable files.\n"; 10467 } 10468 10469 void printBindTable(ObjectFile *o) { 10470 outs() << "Bind table:\n"; 10471 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 10472 printMachOBindTable(MachO); 10473 else 10474 WithColor::error() 10475 << "This operation is only currently supported " 10476 "for Mach-O executable files.\n"; 10477 } 10478 } // namespace llvm 10479