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 reportError(ATypeOrErr.takeError(), A.getObject()->getFileName()); 240 SymbolRef::Type AType = *ATypeOrErr; 241 Expected<SymbolRef::Type> BTypeOrErr = B.getType(); 242 if (!BTypeOrErr) 243 reportError(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 reportError(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 reportError(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 reportError(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 reportError(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 reportError(std::move(Err), FileName, ArchiveName, 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 size_t ChildIndex, bool verbose, 2217 bool print_offset, 2218 StringRef ArchitectureName = StringRef()) { 2219 if (print_offset) 2220 outs() << C.getChildOffset() << "\t"; 2221 sys::fs::perms Mode = 2222 unwrapOrError(C.getAccessMode(), getFileNameForError(C, ChildIndex), 2223 Filename, ArchitectureName); 2224 if (verbose) { 2225 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG. 2226 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG. 2227 outs() << "-"; 2228 outs() << ((Mode & sys::fs::owner_read) ? "r" : "-"); 2229 outs() << ((Mode & sys::fs::owner_write) ? "w" : "-"); 2230 outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-"); 2231 outs() << ((Mode & sys::fs::group_read) ? "r" : "-"); 2232 outs() << ((Mode & sys::fs::group_write) ? "w" : "-"); 2233 outs() << ((Mode & sys::fs::group_exe) ? "x" : "-"); 2234 outs() << ((Mode & sys::fs::others_read) ? "r" : "-"); 2235 outs() << ((Mode & sys::fs::others_write) ? "w" : "-"); 2236 outs() << ((Mode & sys::fs::others_exe) ? "x" : "-"); 2237 } else { 2238 outs() << format("0%o ", Mode); 2239 } 2240 2241 outs() << format("%3d/%-3d %5" PRId64 " ", 2242 unwrapOrError(C.getUID(), getFileNameForError(C, ChildIndex), 2243 Filename, ArchitectureName), 2244 unwrapOrError(C.getGID(), getFileNameForError(C, ChildIndex), 2245 Filename, ArchitectureName), 2246 unwrapOrError(C.getRawSize(), 2247 getFileNameForError(C, ChildIndex), Filename, 2248 ArchitectureName)); 2249 2250 StringRef RawLastModified = C.getRawLastModified(); 2251 if (verbose) { 2252 unsigned Seconds; 2253 if (RawLastModified.getAsInteger(10, Seconds)) 2254 outs() << "(date: \"" << RawLastModified 2255 << "\" contains non-decimal chars) "; 2256 else { 2257 // Since cime(3) returns a 26 character string of the form: 2258 // "Sun Sep 16 01:03:52 1973\n\0" 2259 // just print 24 characters. 2260 time_t t = Seconds; 2261 outs() << format("%.24s ", ctime(&t)); 2262 } 2263 } else { 2264 outs() << RawLastModified << " "; 2265 } 2266 2267 if (verbose) { 2268 Expected<StringRef> NameOrErr = C.getName(); 2269 if (!NameOrErr) { 2270 consumeError(NameOrErr.takeError()); 2271 outs() << unwrapOrError(C.getRawName(), 2272 getFileNameForError(C, ChildIndex), Filename, 2273 ArchitectureName) 2274 << "\n"; 2275 } else { 2276 StringRef Name = NameOrErr.get(); 2277 outs() << Name << "\n"; 2278 } 2279 } else { 2280 outs() << unwrapOrError(C.getRawName(), getFileNameForError(C, ChildIndex), 2281 Filename, ArchitectureName) 2282 << "\n"; 2283 } 2284 } 2285 2286 static void printArchiveHeaders(StringRef Filename, Archive *A, bool verbose, 2287 bool print_offset, 2288 StringRef ArchitectureName = StringRef()) { 2289 Error Err = Error::success(); 2290 size_t I = 0; 2291 for (const auto &C : A->children(Err, false)) 2292 printArchiveChild(Filename, C, I++, verbose, print_offset, 2293 ArchitectureName); 2294 2295 if (Err) 2296 reportError(std::move(Err), Filename, "", ArchitectureName); 2297 } 2298 2299 static bool ValidateArchFlags() { 2300 // Check for -arch all and verifiy the -arch flags are valid. 2301 for (unsigned i = 0; i < ArchFlags.size(); ++i) { 2302 if (ArchFlags[i] == "all") { 2303 ArchAll = true; 2304 } else { 2305 if (!MachOObjectFile::isValidArch(ArchFlags[i])) { 2306 WithColor::error(errs(), "llvm-objdump") 2307 << "unknown architecture named '" + ArchFlags[i] + 2308 "'for the -arch option\n"; 2309 return false; 2310 } 2311 } 2312 } 2313 return true; 2314 } 2315 2316 // ParseInputMachO() parses the named Mach-O file in Filename and handles the 2317 // -arch flags selecting just those slices as specified by them and also parses 2318 // archive files. Then for each individual Mach-O file ProcessMachO() is 2319 // called to process the file based on the command line options. 2320 void parseInputMachO(StringRef Filename) { 2321 if (!ValidateArchFlags()) 2322 return; 2323 2324 // Attempt to open the binary. 2325 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename); 2326 if (!BinaryOrErr) { 2327 if (Error E = isNotObjectErrorInvalidFileType(BinaryOrErr.takeError())) 2328 reportError(std::move(E), Filename); 2329 else 2330 outs() << Filename << ": is not an object file\n"; 2331 return; 2332 } 2333 Binary &Bin = *BinaryOrErr.get().getBinary(); 2334 2335 if (Archive *A = dyn_cast<Archive>(&Bin)) { 2336 outs() << "Archive : " << Filename << "\n"; 2337 if (ArchiveHeaders) 2338 printArchiveHeaders(Filename, A, !NonVerbose, ArchiveMemberOffsets); 2339 2340 Error Err = Error::success(); 2341 unsigned I = -1; 2342 for (auto &C : A->children(Err)) { 2343 ++I; 2344 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary(); 2345 if (!ChildOrErr) { 2346 if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) 2347 reportError(std::move(E), getFileNameForError(C, I), Filename); 2348 continue; 2349 } 2350 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) { 2351 if (!checkMachOAndArchFlags(O, Filename)) 2352 return; 2353 ProcessMachO(Filename, O, O->getFileName()); 2354 } 2355 } 2356 if (Err) 2357 reportError(std::move(Err), Filename); 2358 return; 2359 } 2360 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) { 2361 parseInputMachO(UB); 2362 return; 2363 } 2364 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) { 2365 if (!checkMachOAndArchFlags(O, Filename)) 2366 return; 2367 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) 2368 ProcessMachO(Filename, MachOOF); 2369 else 2370 WithColor::error(errs(), "llvm-objdump") 2371 << Filename << "': " 2372 << "object is not a Mach-O file type.\n"; 2373 return; 2374 } 2375 llvm_unreachable("Input object can't be invalid at this point"); 2376 } 2377 2378 void parseInputMachO(MachOUniversalBinary *UB) { 2379 if (!ValidateArchFlags()) 2380 return; 2381 2382 auto Filename = UB->getFileName(); 2383 2384 if (UniversalHeaders) 2385 printMachOUniversalHeaders(UB, !NonVerbose); 2386 2387 // If we have a list of architecture flags specified dump only those. 2388 if (!ArchAll && !ArchFlags.empty()) { 2389 // Look for a slice in the universal binary that matches each ArchFlag. 2390 bool ArchFound; 2391 for (unsigned i = 0; i < ArchFlags.size(); ++i) { 2392 ArchFound = false; 2393 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(), 2394 E = UB->end_objects(); 2395 I != E; ++I) { 2396 if (ArchFlags[i] == I->getArchFlagName()) { 2397 ArchFound = true; 2398 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = 2399 I->getAsObjectFile(); 2400 std::string ArchitectureName = ""; 2401 if (ArchFlags.size() > 1) 2402 ArchitectureName = I->getArchFlagName(); 2403 if (ObjOrErr) { 2404 ObjectFile &O = *ObjOrErr.get(); 2405 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O)) 2406 ProcessMachO(Filename, MachOOF, "", ArchitectureName); 2407 } else if (Error E = isNotObjectErrorInvalidFileType( 2408 ObjOrErr.takeError())) { 2409 reportError(std::move(E), "", Filename, ArchitectureName); 2410 continue; 2411 } else if (Expected<std::unique_ptr<Archive>> AOrErr = 2412 I->getAsArchive()) { 2413 std::unique_ptr<Archive> &A = *AOrErr; 2414 outs() << "Archive : " << Filename; 2415 if (!ArchitectureName.empty()) 2416 outs() << " (architecture " << ArchitectureName << ")"; 2417 outs() << "\n"; 2418 if (ArchiveHeaders) 2419 printArchiveHeaders(Filename, A.get(), !NonVerbose, 2420 ArchiveMemberOffsets, ArchitectureName); 2421 Error Err = Error::success(); 2422 unsigned I = -1; 2423 for (auto &C : A->children(Err)) { 2424 ++I; 2425 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary(); 2426 if (!ChildOrErr) { 2427 if (Error E = 2428 isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) 2429 reportError(std::move(E), getFileNameForError(C, I), Filename, 2430 ArchitectureName); 2431 continue; 2432 } 2433 if (MachOObjectFile *O = 2434 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) 2435 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName); 2436 } 2437 if (Err) 2438 reportError(std::move(Err), Filename); 2439 } else { 2440 consumeError(AOrErr.takeError()); 2441 reportError(Filename, 2442 "Mach-O universal file for architecture " + 2443 StringRef(I->getArchFlagName()) + 2444 " is not a Mach-O file or an archive file"); 2445 } 2446 } 2447 } 2448 if (!ArchFound) { 2449 WithColor::error(errs(), "llvm-objdump") 2450 << "file: " + Filename + " does not contain " 2451 << "architecture: " + ArchFlags[i] + "\n"; 2452 return; 2453 } 2454 } 2455 return; 2456 } 2457 // No architecture flags were specified so if this contains a slice that 2458 // matches the host architecture dump only that. 2459 if (!ArchAll) { 2460 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(), 2461 E = UB->end_objects(); 2462 I != E; ++I) { 2463 if (MachOObjectFile::getHostArch().getArchName() == 2464 I->getArchFlagName()) { 2465 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile(); 2466 std::string ArchiveName; 2467 ArchiveName.clear(); 2468 if (ObjOrErr) { 2469 ObjectFile &O = *ObjOrErr.get(); 2470 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O)) 2471 ProcessMachO(Filename, MachOOF); 2472 } else if (Error E = 2473 isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) { 2474 reportError(std::move(E), Filename); 2475 } else if (Expected<std::unique_ptr<Archive>> AOrErr = 2476 I->getAsArchive()) { 2477 std::unique_ptr<Archive> &A = *AOrErr; 2478 outs() << "Archive : " << Filename << "\n"; 2479 if (ArchiveHeaders) 2480 printArchiveHeaders(Filename, A.get(), !NonVerbose, 2481 ArchiveMemberOffsets); 2482 Error Err = Error::success(); 2483 unsigned I = -1; 2484 for (auto &C : A->children(Err)) { 2485 ++I; 2486 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary(); 2487 if (!ChildOrErr) { 2488 if (Error E = 2489 isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) 2490 reportError(std::move(E), getFileNameForError(C, I), Filename); 2491 continue; 2492 } 2493 if (MachOObjectFile *O = 2494 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) 2495 ProcessMachO(Filename, O, O->getFileName()); 2496 } 2497 if (Err) 2498 reportError(std::move(Err), Filename); 2499 } else { 2500 consumeError(AOrErr.takeError()); 2501 reportError(Filename, "Mach-O universal file for architecture " + 2502 StringRef(I->getArchFlagName()) + 2503 " is not a Mach-O file or an archive file"); 2504 } 2505 return; 2506 } 2507 } 2508 } 2509 // Either all architectures have been specified or none have been specified 2510 // and this does not contain the host architecture so dump all the slices. 2511 bool moreThanOneArch = UB->getNumberOfObjects() > 1; 2512 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(), 2513 E = UB->end_objects(); 2514 I != E; ++I) { 2515 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile(); 2516 std::string ArchitectureName = ""; 2517 if (moreThanOneArch) 2518 ArchitectureName = I->getArchFlagName(); 2519 if (ObjOrErr) { 2520 ObjectFile &Obj = *ObjOrErr.get(); 2521 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj)) 2522 ProcessMachO(Filename, MachOOF, "", ArchitectureName); 2523 } else if (Error E = 2524 isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) { 2525 reportError(std::move(E), Filename, "", ArchitectureName); 2526 } else if (Expected<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) { 2527 std::unique_ptr<Archive> &A = *AOrErr; 2528 outs() << "Archive : " << Filename; 2529 if (!ArchitectureName.empty()) 2530 outs() << " (architecture " << ArchitectureName << ")"; 2531 outs() << "\n"; 2532 if (ArchiveHeaders) 2533 printArchiveHeaders(Filename, A.get(), !NonVerbose, 2534 ArchiveMemberOffsets, ArchitectureName); 2535 Error Err = Error::success(); 2536 unsigned I = -1; 2537 for (auto &C : A->children(Err)) { 2538 ++I; 2539 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary(); 2540 if (!ChildOrErr) { 2541 if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) 2542 reportError(std::move(E), getFileNameForError(C, I), Filename, 2543 ArchitectureName); 2544 continue; 2545 } 2546 if (MachOObjectFile *O = 2547 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) { 2548 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O)) 2549 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(), 2550 ArchitectureName); 2551 } 2552 } 2553 if (Err) 2554 reportError(std::move(Err), Filename); 2555 } else { 2556 consumeError(AOrErr.takeError()); 2557 reportError(Filename, "Mach-O universal file for architecture " + 2558 StringRef(I->getArchFlagName()) + 2559 " is not a Mach-O file or an archive file"); 2560 } 2561 } 2562 } 2563 2564 // The block of info used by the Symbolizer call backs. 2565 struct DisassembleInfo { 2566 DisassembleInfo(MachOObjectFile *O, SymbolAddressMap *AddrMap, 2567 std::vector<SectionRef> *Sections, bool verbose) 2568 : verbose(verbose), O(O), AddrMap(AddrMap), Sections(Sections) {} 2569 bool verbose; 2570 MachOObjectFile *O; 2571 SectionRef S; 2572 SymbolAddressMap *AddrMap; 2573 std::vector<SectionRef> *Sections; 2574 const char *class_name = nullptr; 2575 const char *selector_name = nullptr; 2576 std::unique_ptr<char[]> method = nullptr; 2577 char *demangled_name = nullptr; 2578 uint64_t adrp_addr = 0; 2579 uint32_t adrp_inst = 0; 2580 std::unique_ptr<SymbolAddressMap> bindtable; 2581 uint32_t depth = 0; 2582 }; 2583 2584 // SymbolizerGetOpInfo() is the operand information call back function. 2585 // This is called to get the symbolic information for operand(s) of an 2586 // instruction when it is being done. This routine does this from 2587 // the relocation information, symbol table, etc. That block of information 2588 // is a pointer to the struct DisassembleInfo that was passed when the 2589 // disassembler context was created and passed to back to here when 2590 // called back by the disassembler for instruction operands that could have 2591 // relocation information. The address of the instruction containing operand is 2592 // at the Pc parameter. The immediate value the operand has is passed in 2593 // op_info->Value and is at Offset past the start of the instruction and has a 2594 // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the 2595 // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol 2596 // names and addends of the symbolic expression to add for the operand. The 2597 // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic 2598 // information is returned then this function returns 1 else it returns 0. 2599 static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset, 2600 uint64_t Size, int TagType, void *TagBuf) { 2601 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo; 2602 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf; 2603 uint64_t value = op_info->Value; 2604 2605 // Make sure all fields returned are zero if we don't set them. 2606 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1)); 2607 op_info->Value = value; 2608 2609 // If the TagType is not the value 1 which it code knows about or if no 2610 // verbose symbolic information is wanted then just return 0, indicating no 2611 // information is being returned. 2612 if (TagType != 1 || !info->verbose) 2613 return 0; 2614 2615 unsigned int Arch = info->O->getArch(); 2616 if (Arch == Triple::x86) { 2617 if (Size != 1 && Size != 2 && Size != 4 && Size != 0) 2618 return 0; 2619 if (info->O->getHeader().filetype != MachO::MH_OBJECT) { 2620 // TODO: 2621 // Search the external relocation entries of a fully linked image 2622 // (if any) for an entry that matches this segment offset. 2623 // uint32_t seg_offset = (Pc + Offset); 2624 return 0; 2625 } 2626 // In MH_OBJECT filetypes search the section's relocation entries (if any) 2627 // for an entry for this section offset. 2628 uint32_t sect_addr = info->S.getAddress(); 2629 uint32_t sect_offset = (Pc + Offset) - sect_addr; 2630 bool reloc_found = false; 2631 DataRefImpl Rel; 2632 MachO::any_relocation_info RE; 2633 bool isExtern = false; 2634 SymbolRef Symbol; 2635 bool r_scattered = false; 2636 uint32_t r_value, pair_r_value, r_type; 2637 for (const RelocationRef &Reloc : info->S.relocations()) { 2638 uint64_t RelocOffset = Reloc.getOffset(); 2639 if (RelocOffset == sect_offset) { 2640 Rel = Reloc.getRawDataRefImpl(); 2641 RE = info->O->getRelocation(Rel); 2642 r_type = info->O->getAnyRelocationType(RE); 2643 r_scattered = info->O->isRelocationScattered(RE); 2644 if (r_scattered) { 2645 r_value = info->O->getScatteredRelocationValue(RE); 2646 if (r_type == MachO::GENERIC_RELOC_SECTDIFF || 2647 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) { 2648 DataRefImpl RelNext = Rel; 2649 info->O->moveRelocationNext(RelNext); 2650 MachO::any_relocation_info RENext; 2651 RENext = info->O->getRelocation(RelNext); 2652 if (info->O->isRelocationScattered(RENext)) 2653 pair_r_value = info->O->getScatteredRelocationValue(RENext); 2654 else 2655 return 0; 2656 } 2657 } else { 2658 isExtern = info->O->getPlainRelocationExternal(RE); 2659 if (isExtern) { 2660 symbol_iterator RelocSym = Reloc.getSymbol(); 2661 Symbol = *RelocSym; 2662 } 2663 } 2664 reloc_found = true; 2665 break; 2666 } 2667 } 2668 if (reloc_found && isExtern) { 2669 op_info->AddSymbol.Present = 1; 2670 op_info->AddSymbol.Name = 2671 unwrapOrError(Symbol.getName(), info->O->getFileName()).data(); 2672 // For i386 extern relocation entries the value in the instruction is 2673 // the offset from the symbol, and value is already set in op_info->Value. 2674 return 1; 2675 } 2676 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF || 2677 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) { 2678 const char *add = GuessSymbolName(r_value, info->AddrMap); 2679 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap); 2680 uint32_t offset = value - (r_value - pair_r_value); 2681 op_info->AddSymbol.Present = 1; 2682 if (add != nullptr) 2683 op_info->AddSymbol.Name = add; 2684 else 2685 op_info->AddSymbol.Value = r_value; 2686 op_info->SubtractSymbol.Present = 1; 2687 if (sub != nullptr) 2688 op_info->SubtractSymbol.Name = sub; 2689 else 2690 op_info->SubtractSymbol.Value = pair_r_value; 2691 op_info->Value = offset; 2692 return 1; 2693 } 2694 return 0; 2695 } 2696 if (Arch == Triple::x86_64) { 2697 if (Size != 1 && Size != 2 && Size != 4 && Size != 0) 2698 return 0; 2699 // For non MH_OBJECT types, like MH_KEXT_BUNDLE, Search the external 2700 // relocation entries of a linked image (if any) for an entry that matches 2701 // this segment offset. 2702 if (info->O->getHeader().filetype != MachO::MH_OBJECT) { 2703 uint64_t seg_offset = Pc + Offset; 2704 bool reloc_found = false; 2705 DataRefImpl Rel; 2706 MachO::any_relocation_info RE; 2707 bool isExtern = false; 2708 SymbolRef Symbol; 2709 for (const RelocationRef &Reloc : info->O->external_relocations()) { 2710 uint64_t RelocOffset = Reloc.getOffset(); 2711 if (RelocOffset == seg_offset) { 2712 Rel = Reloc.getRawDataRefImpl(); 2713 RE = info->O->getRelocation(Rel); 2714 // external relocation entries should always be external. 2715 isExtern = info->O->getPlainRelocationExternal(RE); 2716 if (isExtern) { 2717 symbol_iterator RelocSym = Reloc.getSymbol(); 2718 Symbol = *RelocSym; 2719 } 2720 reloc_found = true; 2721 break; 2722 } 2723 } 2724 if (reloc_found && isExtern) { 2725 // The Value passed in will be adjusted by the Pc if the instruction 2726 // adds the Pc. But for x86_64 external relocation entries the Value 2727 // is the offset from the external symbol. 2728 if (info->O->getAnyRelocationPCRel(RE)) 2729 op_info->Value -= Pc + Offset + Size; 2730 const char *name = 2731 unwrapOrError(Symbol.getName(), info->O->getFileName()).data(); 2732 op_info->AddSymbol.Present = 1; 2733 op_info->AddSymbol.Name = name; 2734 return 1; 2735 } 2736 return 0; 2737 } 2738 // In MH_OBJECT filetypes search the section's relocation entries (if any) 2739 // for an entry for this section offset. 2740 uint64_t sect_addr = info->S.getAddress(); 2741 uint64_t sect_offset = (Pc + Offset) - sect_addr; 2742 bool reloc_found = false; 2743 DataRefImpl Rel; 2744 MachO::any_relocation_info RE; 2745 bool isExtern = false; 2746 SymbolRef Symbol; 2747 for (const RelocationRef &Reloc : info->S.relocations()) { 2748 uint64_t RelocOffset = Reloc.getOffset(); 2749 if (RelocOffset == sect_offset) { 2750 Rel = Reloc.getRawDataRefImpl(); 2751 RE = info->O->getRelocation(Rel); 2752 // NOTE: Scattered relocations don't exist on x86_64. 2753 isExtern = info->O->getPlainRelocationExternal(RE); 2754 if (isExtern) { 2755 symbol_iterator RelocSym = Reloc.getSymbol(); 2756 Symbol = *RelocSym; 2757 } 2758 reloc_found = true; 2759 break; 2760 } 2761 } 2762 if (reloc_found && isExtern) { 2763 // The Value passed in will be adjusted by the Pc if the instruction 2764 // adds the Pc. But for x86_64 external relocation entries the Value 2765 // is the offset from the external symbol. 2766 if (info->O->getAnyRelocationPCRel(RE)) 2767 op_info->Value -= Pc + Offset + Size; 2768 const char *name = 2769 unwrapOrError(Symbol.getName(), info->O->getFileName()).data(); 2770 unsigned Type = info->O->getAnyRelocationType(RE); 2771 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) { 2772 DataRefImpl RelNext = Rel; 2773 info->O->moveRelocationNext(RelNext); 2774 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext); 2775 unsigned TypeNext = info->O->getAnyRelocationType(RENext); 2776 bool isExternNext = info->O->getPlainRelocationExternal(RENext); 2777 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext); 2778 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) { 2779 op_info->SubtractSymbol.Present = 1; 2780 op_info->SubtractSymbol.Name = name; 2781 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum); 2782 Symbol = *RelocSymNext; 2783 name = unwrapOrError(Symbol.getName(), info->O->getFileName()).data(); 2784 } 2785 } 2786 // TODO: add the VariantKinds to op_info->VariantKind for relocation types 2787 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT. 2788 op_info->AddSymbol.Present = 1; 2789 op_info->AddSymbol.Name = name; 2790 return 1; 2791 } 2792 return 0; 2793 } 2794 if (Arch == Triple::arm) { 2795 if (Offset != 0 || (Size != 4 && Size != 2)) 2796 return 0; 2797 if (info->O->getHeader().filetype != MachO::MH_OBJECT) { 2798 // TODO: 2799 // Search the external relocation entries of a fully linked image 2800 // (if any) for an entry that matches this segment offset. 2801 // uint32_t seg_offset = (Pc + Offset); 2802 return 0; 2803 } 2804 // In MH_OBJECT filetypes search the section's relocation entries (if any) 2805 // for an entry for this section offset. 2806 uint32_t sect_addr = info->S.getAddress(); 2807 uint32_t sect_offset = (Pc + Offset) - sect_addr; 2808 DataRefImpl Rel; 2809 MachO::any_relocation_info RE; 2810 bool isExtern = false; 2811 SymbolRef Symbol; 2812 bool r_scattered = false; 2813 uint32_t r_value, pair_r_value, r_type, r_length, other_half; 2814 auto Reloc = 2815 find_if(info->S.relocations(), [&](const RelocationRef &Reloc) { 2816 uint64_t RelocOffset = Reloc.getOffset(); 2817 return RelocOffset == sect_offset; 2818 }); 2819 2820 if (Reloc == info->S.relocations().end()) 2821 return 0; 2822 2823 Rel = Reloc->getRawDataRefImpl(); 2824 RE = info->O->getRelocation(Rel); 2825 r_length = info->O->getAnyRelocationLength(RE); 2826 r_scattered = info->O->isRelocationScattered(RE); 2827 if (r_scattered) { 2828 r_value = info->O->getScatteredRelocationValue(RE); 2829 r_type = info->O->getScatteredRelocationType(RE); 2830 } else { 2831 r_type = info->O->getAnyRelocationType(RE); 2832 isExtern = info->O->getPlainRelocationExternal(RE); 2833 if (isExtern) { 2834 symbol_iterator RelocSym = Reloc->getSymbol(); 2835 Symbol = *RelocSym; 2836 } 2837 } 2838 if (r_type == MachO::ARM_RELOC_HALF || 2839 r_type == MachO::ARM_RELOC_SECTDIFF || 2840 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF || 2841 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) { 2842 DataRefImpl RelNext = Rel; 2843 info->O->moveRelocationNext(RelNext); 2844 MachO::any_relocation_info RENext; 2845 RENext = info->O->getRelocation(RelNext); 2846 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff; 2847 if (info->O->isRelocationScattered(RENext)) 2848 pair_r_value = info->O->getScatteredRelocationValue(RENext); 2849 } 2850 2851 if (isExtern) { 2852 const char *name = 2853 unwrapOrError(Symbol.getName(), info->O->getFileName()).data(); 2854 op_info->AddSymbol.Present = 1; 2855 op_info->AddSymbol.Name = name; 2856 switch (r_type) { 2857 case MachO::ARM_RELOC_HALF: 2858 if ((r_length & 0x1) == 1) { 2859 op_info->Value = value << 16 | other_half; 2860 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16; 2861 } else { 2862 op_info->Value = other_half << 16 | value; 2863 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16; 2864 } 2865 break; 2866 default: 2867 break; 2868 } 2869 return 1; 2870 } 2871 // If we have a branch that is not an external relocation entry then 2872 // return 0 so the code in tryAddingSymbolicOperand() can use the 2873 // SymbolLookUp call back with the branch target address to look up the 2874 // symbol and possibility add an annotation for a symbol stub. 2875 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 || 2876 r_type == MachO::ARM_THUMB_RELOC_BR22)) 2877 return 0; 2878 2879 uint32_t offset = 0; 2880 if (r_type == MachO::ARM_RELOC_HALF || 2881 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) { 2882 if ((r_length & 0x1) == 1) 2883 value = value << 16 | other_half; 2884 else 2885 value = other_half << 16 | value; 2886 } 2887 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF && 2888 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) { 2889 offset = value - r_value; 2890 value = r_value; 2891 } 2892 2893 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) { 2894 if ((r_length & 0x1) == 1) 2895 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16; 2896 else 2897 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16; 2898 const char *add = GuessSymbolName(r_value, info->AddrMap); 2899 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap); 2900 int32_t offset = value - (r_value - pair_r_value); 2901 op_info->AddSymbol.Present = 1; 2902 if (add != nullptr) 2903 op_info->AddSymbol.Name = add; 2904 else 2905 op_info->AddSymbol.Value = r_value; 2906 op_info->SubtractSymbol.Present = 1; 2907 if (sub != nullptr) 2908 op_info->SubtractSymbol.Name = sub; 2909 else 2910 op_info->SubtractSymbol.Value = pair_r_value; 2911 op_info->Value = offset; 2912 return 1; 2913 } 2914 2915 op_info->AddSymbol.Present = 1; 2916 op_info->Value = offset; 2917 if (r_type == MachO::ARM_RELOC_HALF) { 2918 if ((r_length & 0x1) == 1) 2919 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16; 2920 else 2921 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16; 2922 } 2923 const char *add = GuessSymbolName(value, info->AddrMap); 2924 if (add != nullptr) { 2925 op_info->AddSymbol.Name = add; 2926 return 1; 2927 } 2928 op_info->AddSymbol.Value = value; 2929 return 1; 2930 } 2931 if (Arch == Triple::aarch64) { 2932 if (Offset != 0 || Size != 4) 2933 return 0; 2934 if (info->O->getHeader().filetype != MachO::MH_OBJECT) { 2935 // TODO: 2936 // Search the external relocation entries of a fully linked image 2937 // (if any) for an entry that matches this segment offset. 2938 // uint64_t seg_offset = (Pc + Offset); 2939 return 0; 2940 } 2941 // In MH_OBJECT filetypes search the section's relocation entries (if any) 2942 // for an entry for this section offset. 2943 uint64_t sect_addr = info->S.getAddress(); 2944 uint64_t sect_offset = (Pc + Offset) - sect_addr; 2945 auto Reloc = 2946 find_if(info->S.relocations(), [&](const RelocationRef &Reloc) { 2947 uint64_t RelocOffset = Reloc.getOffset(); 2948 return RelocOffset == sect_offset; 2949 }); 2950 2951 if (Reloc == info->S.relocations().end()) 2952 return 0; 2953 2954 DataRefImpl Rel = Reloc->getRawDataRefImpl(); 2955 MachO::any_relocation_info RE = info->O->getRelocation(Rel); 2956 uint32_t r_type = info->O->getAnyRelocationType(RE); 2957 if (r_type == MachO::ARM64_RELOC_ADDEND) { 2958 DataRefImpl RelNext = Rel; 2959 info->O->moveRelocationNext(RelNext); 2960 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext); 2961 if (value == 0) { 2962 value = info->O->getPlainRelocationSymbolNum(RENext); 2963 op_info->Value = value; 2964 } 2965 } 2966 // NOTE: Scattered relocations don't exist on arm64. 2967 if (!info->O->getPlainRelocationExternal(RE)) 2968 return 0; 2969 const char *name = 2970 unwrapOrError(Reloc->getSymbol()->getName(), info->O->getFileName()) 2971 .data(); 2972 op_info->AddSymbol.Present = 1; 2973 op_info->AddSymbol.Name = name; 2974 2975 switch (r_type) { 2976 case MachO::ARM64_RELOC_PAGE21: 2977 /* @page */ 2978 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE; 2979 break; 2980 case MachO::ARM64_RELOC_PAGEOFF12: 2981 /* @pageoff */ 2982 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF; 2983 break; 2984 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21: 2985 /* @gotpage */ 2986 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE; 2987 break; 2988 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12: 2989 /* @gotpageoff */ 2990 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF; 2991 break; 2992 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21: 2993 /* @tvlppage is not implemented in llvm-mc */ 2994 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP; 2995 break; 2996 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12: 2997 /* @tvlppageoff is not implemented in llvm-mc */ 2998 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF; 2999 break; 3000 default: 3001 case MachO::ARM64_RELOC_BRANCH26: 3002 op_info->VariantKind = LLVMDisassembler_VariantKind_None; 3003 break; 3004 } 3005 return 1; 3006 } 3007 return 0; 3008 } 3009 3010 // GuessCstringPointer is passed the address of what might be a pointer to a 3011 // literal string in a cstring section. If that address is in a cstring section 3012 // it returns a pointer to that string. Else it returns nullptr. 3013 static const char *GuessCstringPointer(uint64_t ReferenceValue, 3014 struct DisassembleInfo *info) { 3015 for (const auto &Load : info->O->load_commands()) { 3016 if (Load.C.cmd == MachO::LC_SEGMENT_64) { 3017 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load); 3018 for (unsigned J = 0; J < Seg.nsects; ++J) { 3019 MachO::section_64 Sec = info->O->getSection64(Load, J); 3020 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE; 3021 if (section_type == MachO::S_CSTRING_LITERALS && 3022 ReferenceValue >= Sec.addr && 3023 ReferenceValue < Sec.addr + Sec.size) { 3024 uint64_t sect_offset = ReferenceValue - Sec.addr; 3025 uint64_t object_offset = Sec.offset + sect_offset; 3026 StringRef MachOContents = info->O->getData(); 3027 uint64_t object_size = MachOContents.size(); 3028 const char *object_addr = (const char *)MachOContents.data(); 3029 if (object_offset < object_size) { 3030 const char *name = object_addr + object_offset; 3031 return name; 3032 } else { 3033 return nullptr; 3034 } 3035 } 3036 } 3037 } else if (Load.C.cmd == MachO::LC_SEGMENT) { 3038 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load); 3039 for (unsigned J = 0; J < Seg.nsects; ++J) { 3040 MachO::section Sec = info->O->getSection(Load, J); 3041 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE; 3042 if (section_type == MachO::S_CSTRING_LITERALS && 3043 ReferenceValue >= Sec.addr && 3044 ReferenceValue < Sec.addr + Sec.size) { 3045 uint64_t sect_offset = ReferenceValue - Sec.addr; 3046 uint64_t object_offset = Sec.offset + sect_offset; 3047 StringRef MachOContents = info->O->getData(); 3048 uint64_t object_size = MachOContents.size(); 3049 const char *object_addr = (const char *)MachOContents.data(); 3050 if (object_offset < object_size) { 3051 const char *name = object_addr + object_offset; 3052 return name; 3053 } else { 3054 return nullptr; 3055 } 3056 } 3057 } 3058 } 3059 } 3060 return nullptr; 3061 } 3062 3063 // GuessIndirectSymbol returns the name of the indirect symbol for the 3064 // ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe 3065 // an address of a symbol stub or a lazy or non-lazy pointer to associate the 3066 // symbol name being referenced by the stub or pointer. 3067 static const char *GuessIndirectSymbol(uint64_t ReferenceValue, 3068 struct DisassembleInfo *info) { 3069 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand(); 3070 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand(); 3071 for (const auto &Load : info->O->load_commands()) { 3072 if (Load.C.cmd == MachO::LC_SEGMENT_64) { 3073 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load); 3074 for (unsigned J = 0; J < Seg.nsects; ++J) { 3075 MachO::section_64 Sec = info->O->getSection64(Load, J); 3076 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE; 3077 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS || 3078 section_type == MachO::S_LAZY_SYMBOL_POINTERS || 3079 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS || 3080 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS || 3081 section_type == MachO::S_SYMBOL_STUBS) && 3082 ReferenceValue >= Sec.addr && 3083 ReferenceValue < Sec.addr + Sec.size) { 3084 uint32_t stride; 3085 if (section_type == MachO::S_SYMBOL_STUBS) 3086 stride = Sec.reserved2; 3087 else 3088 stride = 8; 3089 if (stride == 0) 3090 return nullptr; 3091 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride; 3092 if (index < Dysymtab.nindirectsyms) { 3093 uint32_t indirect_symbol = 3094 info->O->getIndirectSymbolTableEntry(Dysymtab, index); 3095 if (indirect_symbol < Symtab.nsyms) { 3096 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol); 3097 return unwrapOrError(Sym->getName(), info->O->getFileName()) 3098 .data(); 3099 } 3100 } 3101 } 3102 } 3103 } else if (Load.C.cmd == MachO::LC_SEGMENT) { 3104 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load); 3105 for (unsigned J = 0; J < Seg.nsects; ++J) { 3106 MachO::section Sec = info->O->getSection(Load, J); 3107 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE; 3108 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS || 3109 section_type == MachO::S_LAZY_SYMBOL_POINTERS || 3110 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS || 3111 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS || 3112 section_type == MachO::S_SYMBOL_STUBS) && 3113 ReferenceValue >= Sec.addr && 3114 ReferenceValue < Sec.addr + Sec.size) { 3115 uint32_t stride; 3116 if (section_type == MachO::S_SYMBOL_STUBS) 3117 stride = Sec.reserved2; 3118 else 3119 stride = 4; 3120 if (stride == 0) 3121 return nullptr; 3122 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride; 3123 if (index < Dysymtab.nindirectsyms) { 3124 uint32_t indirect_symbol = 3125 info->O->getIndirectSymbolTableEntry(Dysymtab, index); 3126 if (indirect_symbol < Symtab.nsyms) { 3127 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol); 3128 return unwrapOrError(Sym->getName(), info->O->getFileName()) 3129 .data(); 3130 } 3131 } 3132 } 3133 } 3134 } 3135 } 3136 return nullptr; 3137 } 3138 3139 // method_reference() is called passing it the ReferenceName that might be 3140 // a reference it to an Objective-C method call. If so then it allocates and 3141 // assembles a method call string with the values last seen and saved in 3142 // the DisassembleInfo's class_name and selector_name fields. This is saved 3143 // into the method field of the info and any previous string is free'ed. 3144 // Then the class_name field in the info is set to nullptr. The method call 3145 // string is set into ReferenceName and ReferenceType is set to 3146 // LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call 3147 // then both ReferenceType and ReferenceName are left unchanged. 3148 static void method_reference(struct DisassembleInfo *info, 3149 uint64_t *ReferenceType, 3150 const char **ReferenceName) { 3151 unsigned int Arch = info->O->getArch(); 3152 if (*ReferenceName != nullptr) { 3153 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) { 3154 if (info->selector_name != nullptr) { 3155 if (info->class_name != nullptr) { 3156 info->method = std::make_unique<char[]>( 3157 5 + strlen(info->class_name) + strlen(info->selector_name)); 3158 char *method = info->method.get(); 3159 if (method != nullptr) { 3160 strcpy(method, "+["); 3161 strcat(method, info->class_name); 3162 strcat(method, " "); 3163 strcat(method, info->selector_name); 3164 strcat(method, "]"); 3165 *ReferenceName = method; 3166 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message; 3167 } 3168 } else { 3169 info->method = 3170 std::make_unique<char[]>(9 + strlen(info->selector_name)); 3171 char *method = info->method.get(); 3172 if (method != nullptr) { 3173 if (Arch == Triple::x86_64) 3174 strcpy(method, "-[%rdi "); 3175 else if (Arch == Triple::aarch64) 3176 strcpy(method, "-[x0 "); 3177 else 3178 strcpy(method, "-[r? "); 3179 strcat(method, info->selector_name); 3180 strcat(method, "]"); 3181 *ReferenceName = method; 3182 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message; 3183 } 3184 } 3185 info->class_name = nullptr; 3186 } 3187 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) { 3188 if (info->selector_name != nullptr) { 3189 info->method = 3190 std::make_unique<char[]>(17 + strlen(info->selector_name)); 3191 char *method = info->method.get(); 3192 if (method != nullptr) { 3193 if (Arch == Triple::x86_64) 3194 strcpy(method, "-[[%rdi super] "); 3195 else if (Arch == Triple::aarch64) 3196 strcpy(method, "-[[x0 super] "); 3197 else 3198 strcpy(method, "-[[r? super] "); 3199 strcat(method, info->selector_name); 3200 strcat(method, "]"); 3201 *ReferenceName = method; 3202 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message; 3203 } 3204 info->class_name = nullptr; 3205 } 3206 } 3207 } 3208 } 3209 3210 // GuessPointerPointer() is passed the address of what might be a pointer to 3211 // a reference to an Objective-C class, selector, message ref or cfstring. 3212 // If so the value of the pointer is returned and one of the booleans are set 3213 // to true. If not zero is returned and all the booleans are set to false. 3214 static uint64_t GuessPointerPointer(uint64_t ReferenceValue, 3215 struct DisassembleInfo *info, 3216 bool &classref, bool &selref, bool &msgref, 3217 bool &cfstring) { 3218 classref = false; 3219 selref = false; 3220 msgref = false; 3221 cfstring = false; 3222 for (const auto &Load : info->O->load_commands()) { 3223 if (Load.C.cmd == MachO::LC_SEGMENT_64) { 3224 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load); 3225 for (unsigned J = 0; J < Seg.nsects; ++J) { 3226 MachO::section_64 Sec = info->O->getSection64(Load, J); 3227 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 || 3228 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 || 3229 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 || 3230 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 || 3231 strncmp(Sec.sectname, "__cfstring", 16) == 0) && 3232 ReferenceValue >= Sec.addr && 3233 ReferenceValue < Sec.addr + Sec.size) { 3234 uint64_t sect_offset = ReferenceValue - Sec.addr; 3235 uint64_t object_offset = Sec.offset + sect_offset; 3236 StringRef MachOContents = info->O->getData(); 3237 uint64_t object_size = MachOContents.size(); 3238 const char *object_addr = (const char *)MachOContents.data(); 3239 if (object_offset < object_size) { 3240 uint64_t pointer_value; 3241 memcpy(&pointer_value, object_addr + object_offset, 3242 sizeof(uint64_t)); 3243 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 3244 sys::swapByteOrder(pointer_value); 3245 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0) 3246 selref = true; 3247 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 || 3248 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0) 3249 classref = true; 3250 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 && 3251 ReferenceValue + 8 < Sec.addr + Sec.size) { 3252 msgref = true; 3253 memcpy(&pointer_value, object_addr + object_offset + 8, 3254 sizeof(uint64_t)); 3255 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 3256 sys::swapByteOrder(pointer_value); 3257 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0) 3258 cfstring = true; 3259 return pointer_value; 3260 } else { 3261 return 0; 3262 } 3263 } 3264 } 3265 } 3266 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files. 3267 } 3268 return 0; 3269 } 3270 3271 // get_pointer_64 returns a pointer to the bytes in the object file at the 3272 // Address from a section in the Mach-O file. And indirectly returns the 3273 // offset into the section, number of bytes left in the section past the offset 3274 // and which section is was being referenced. If the Address is not in a 3275 // section nullptr is returned. 3276 static const char *get_pointer_64(uint64_t Address, uint32_t &offset, 3277 uint32_t &left, SectionRef &S, 3278 DisassembleInfo *info, 3279 bool objc_only = false) { 3280 offset = 0; 3281 left = 0; 3282 S = SectionRef(); 3283 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) { 3284 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress(); 3285 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize(); 3286 if (SectSize == 0) 3287 continue; 3288 if (objc_only) { 3289 StringRef SectName; 3290 Expected<StringRef> SecNameOrErr = 3291 ((*(info->Sections))[SectIdx]).getName(); 3292 if (SecNameOrErr) 3293 SectName = *SecNameOrErr; 3294 else 3295 consumeError(SecNameOrErr.takeError()); 3296 3297 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl(); 3298 StringRef SegName = info->O->getSectionFinalSegmentName(Ref); 3299 if (SegName != "__OBJC" && SectName != "__cstring") 3300 continue; 3301 } 3302 if (Address >= SectAddress && Address < SectAddress + SectSize) { 3303 S = (*(info->Sections))[SectIdx]; 3304 offset = Address - SectAddress; 3305 left = SectSize - offset; 3306 StringRef SectContents = unwrapOrError( 3307 ((*(info->Sections))[SectIdx]).getContents(), info->O->getFileName()); 3308 return SectContents.data() + offset; 3309 } 3310 } 3311 return nullptr; 3312 } 3313 3314 static const char *get_pointer_32(uint32_t Address, uint32_t &offset, 3315 uint32_t &left, SectionRef &S, 3316 DisassembleInfo *info, 3317 bool objc_only = false) { 3318 return get_pointer_64(Address, offset, left, S, info, objc_only); 3319 } 3320 3321 // get_symbol_64() returns the name of a symbol (or nullptr) and the address of 3322 // the symbol indirectly through n_value. Based on the relocation information 3323 // for the specified section offset in the specified section reference. 3324 // If no relocation information is found and a non-zero ReferenceValue for the 3325 // symbol is passed, look up that address in the info's AddrMap. 3326 static const char *get_symbol_64(uint32_t sect_offset, SectionRef S, 3327 DisassembleInfo *info, uint64_t &n_value, 3328 uint64_t ReferenceValue = 0) { 3329 n_value = 0; 3330 if (!info->verbose) 3331 return nullptr; 3332 3333 // See if there is an external relocation entry at the sect_offset. 3334 bool reloc_found = false; 3335 DataRefImpl Rel; 3336 MachO::any_relocation_info RE; 3337 bool isExtern = false; 3338 SymbolRef Symbol; 3339 for (const RelocationRef &Reloc : S.relocations()) { 3340 uint64_t RelocOffset = Reloc.getOffset(); 3341 if (RelocOffset == sect_offset) { 3342 Rel = Reloc.getRawDataRefImpl(); 3343 RE = info->O->getRelocation(Rel); 3344 if (info->O->isRelocationScattered(RE)) 3345 continue; 3346 isExtern = info->O->getPlainRelocationExternal(RE); 3347 if (isExtern) { 3348 symbol_iterator RelocSym = Reloc.getSymbol(); 3349 Symbol = *RelocSym; 3350 } 3351 reloc_found = true; 3352 break; 3353 } 3354 } 3355 // If there is an external relocation entry for a symbol in this section 3356 // at this section_offset then use that symbol's value for the n_value 3357 // and return its name. 3358 const char *SymbolName = nullptr; 3359 if (reloc_found && isExtern) { 3360 n_value = Symbol.getValue(); 3361 StringRef Name = unwrapOrError(Symbol.getName(), info->O->getFileName()); 3362 if (!Name.empty()) { 3363 SymbolName = Name.data(); 3364 return SymbolName; 3365 } 3366 } 3367 3368 // TODO: For fully linked images, look through the external relocation 3369 // entries off the dynamic symtab command. For these the r_offset is from the 3370 // start of the first writeable segment in the Mach-O file. So the offset 3371 // to this section from that segment is passed to this routine by the caller, 3372 // as the database_offset. Which is the difference of the section's starting 3373 // address and the first writable segment. 3374 // 3375 // NOTE: need add passing the database_offset to this routine. 3376 3377 // We did not find an external relocation entry so look up the ReferenceValue 3378 // as an address of a symbol and if found return that symbol's name. 3379 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap); 3380 3381 return SymbolName; 3382 } 3383 3384 static const char *get_symbol_32(uint32_t sect_offset, SectionRef S, 3385 DisassembleInfo *info, 3386 uint32_t ReferenceValue) { 3387 uint64_t n_value64; 3388 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue); 3389 } 3390 3391 // These are structs in the Objective-C meta data and read to produce the 3392 // comments for disassembly. While these are part of the ABI they are no 3393 // public defintions. So the are here not in include/llvm/BinaryFormat/MachO.h 3394 // . 3395 3396 // The cfstring object in a 64-bit Mach-O file. 3397 struct cfstring64_t { 3398 uint64_t isa; // class64_t * (64-bit pointer) 3399 uint64_t flags; // flag bits 3400 uint64_t characters; // char * (64-bit pointer) 3401 uint64_t length; // number of non-NULL characters in above 3402 }; 3403 3404 // The class object in a 64-bit Mach-O file. 3405 struct class64_t { 3406 uint64_t isa; // class64_t * (64-bit pointer) 3407 uint64_t superclass; // class64_t * (64-bit pointer) 3408 uint64_t cache; // Cache (64-bit pointer) 3409 uint64_t vtable; // IMP * (64-bit pointer) 3410 uint64_t data; // class_ro64_t * (64-bit pointer) 3411 }; 3412 3413 struct class32_t { 3414 uint32_t isa; /* class32_t * (32-bit pointer) */ 3415 uint32_t superclass; /* class32_t * (32-bit pointer) */ 3416 uint32_t cache; /* Cache (32-bit pointer) */ 3417 uint32_t vtable; /* IMP * (32-bit pointer) */ 3418 uint32_t data; /* class_ro32_t * (32-bit pointer) */ 3419 }; 3420 3421 struct class_ro64_t { 3422 uint32_t flags; 3423 uint32_t instanceStart; 3424 uint32_t instanceSize; 3425 uint32_t reserved; 3426 uint64_t ivarLayout; // const uint8_t * (64-bit pointer) 3427 uint64_t name; // const char * (64-bit pointer) 3428 uint64_t baseMethods; // const method_list_t * (64-bit pointer) 3429 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer) 3430 uint64_t ivars; // const ivar_list_t * (64-bit pointer) 3431 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer) 3432 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer) 3433 }; 3434 3435 struct class_ro32_t { 3436 uint32_t flags; 3437 uint32_t instanceStart; 3438 uint32_t instanceSize; 3439 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */ 3440 uint32_t name; /* const char * (32-bit pointer) */ 3441 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */ 3442 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */ 3443 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */ 3444 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */ 3445 uint32_t baseProperties; /* const struct objc_property_list * 3446 (32-bit pointer) */ 3447 }; 3448 3449 /* Values for class_ro{64,32}_t->flags */ 3450 #define RO_META (1 << 0) 3451 #define RO_ROOT (1 << 1) 3452 #define RO_HAS_CXX_STRUCTORS (1 << 2) 3453 3454 struct method_list64_t { 3455 uint32_t entsize; 3456 uint32_t count; 3457 /* struct method64_t first; These structures follow inline */ 3458 }; 3459 3460 struct method_list32_t { 3461 uint32_t entsize; 3462 uint32_t count; 3463 /* struct method32_t first; These structures follow inline */ 3464 }; 3465 3466 struct method64_t { 3467 uint64_t name; /* SEL (64-bit pointer) */ 3468 uint64_t types; /* const char * (64-bit pointer) */ 3469 uint64_t imp; /* IMP (64-bit pointer) */ 3470 }; 3471 3472 struct method32_t { 3473 uint32_t name; /* SEL (32-bit pointer) */ 3474 uint32_t types; /* const char * (32-bit pointer) */ 3475 uint32_t imp; /* IMP (32-bit pointer) */ 3476 }; 3477 3478 struct protocol_list64_t { 3479 uint64_t count; /* uintptr_t (a 64-bit value) */ 3480 /* struct protocol64_t * list[0]; These pointers follow inline */ 3481 }; 3482 3483 struct protocol_list32_t { 3484 uint32_t count; /* uintptr_t (a 32-bit value) */ 3485 /* struct protocol32_t * list[0]; These pointers follow inline */ 3486 }; 3487 3488 struct protocol64_t { 3489 uint64_t isa; /* id * (64-bit pointer) */ 3490 uint64_t name; /* const char * (64-bit pointer) */ 3491 uint64_t protocols; /* struct protocol_list64_t * 3492 (64-bit pointer) */ 3493 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */ 3494 uint64_t classMethods; /* method_list_t * (64-bit pointer) */ 3495 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */ 3496 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */ 3497 uint64_t instanceProperties; /* struct objc_property_list * 3498 (64-bit pointer) */ 3499 }; 3500 3501 struct protocol32_t { 3502 uint32_t isa; /* id * (32-bit pointer) */ 3503 uint32_t name; /* const char * (32-bit pointer) */ 3504 uint32_t protocols; /* struct protocol_list_t * 3505 (32-bit pointer) */ 3506 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */ 3507 uint32_t classMethods; /* method_list_t * (32-bit pointer) */ 3508 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */ 3509 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */ 3510 uint32_t instanceProperties; /* struct objc_property_list * 3511 (32-bit pointer) */ 3512 }; 3513 3514 struct ivar_list64_t { 3515 uint32_t entsize; 3516 uint32_t count; 3517 /* struct ivar64_t first; These structures follow inline */ 3518 }; 3519 3520 struct ivar_list32_t { 3521 uint32_t entsize; 3522 uint32_t count; 3523 /* struct ivar32_t first; These structures follow inline */ 3524 }; 3525 3526 struct ivar64_t { 3527 uint64_t offset; /* uintptr_t * (64-bit pointer) */ 3528 uint64_t name; /* const char * (64-bit pointer) */ 3529 uint64_t type; /* const char * (64-bit pointer) */ 3530 uint32_t alignment; 3531 uint32_t size; 3532 }; 3533 3534 struct ivar32_t { 3535 uint32_t offset; /* uintptr_t * (32-bit pointer) */ 3536 uint32_t name; /* const char * (32-bit pointer) */ 3537 uint32_t type; /* const char * (32-bit pointer) */ 3538 uint32_t alignment; 3539 uint32_t size; 3540 }; 3541 3542 struct objc_property_list64 { 3543 uint32_t entsize; 3544 uint32_t count; 3545 /* struct objc_property64 first; These structures follow inline */ 3546 }; 3547 3548 struct objc_property_list32 { 3549 uint32_t entsize; 3550 uint32_t count; 3551 /* struct objc_property32 first; These structures follow inline */ 3552 }; 3553 3554 struct objc_property64 { 3555 uint64_t name; /* const char * (64-bit pointer) */ 3556 uint64_t attributes; /* const char * (64-bit pointer) */ 3557 }; 3558 3559 struct objc_property32 { 3560 uint32_t name; /* const char * (32-bit pointer) */ 3561 uint32_t attributes; /* const char * (32-bit pointer) */ 3562 }; 3563 3564 struct category64_t { 3565 uint64_t name; /* const char * (64-bit pointer) */ 3566 uint64_t cls; /* struct class_t * (64-bit pointer) */ 3567 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */ 3568 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */ 3569 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */ 3570 uint64_t instanceProperties; /* struct objc_property_list * 3571 (64-bit pointer) */ 3572 }; 3573 3574 struct category32_t { 3575 uint32_t name; /* const char * (32-bit pointer) */ 3576 uint32_t cls; /* struct class_t * (32-bit pointer) */ 3577 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */ 3578 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */ 3579 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */ 3580 uint32_t instanceProperties; /* struct objc_property_list * 3581 (32-bit pointer) */ 3582 }; 3583 3584 struct objc_image_info64 { 3585 uint32_t version; 3586 uint32_t flags; 3587 }; 3588 struct objc_image_info32 { 3589 uint32_t version; 3590 uint32_t flags; 3591 }; 3592 struct imageInfo_t { 3593 uint32_t version; 3594 uint32_t flags; 3595 }; 3596 /* masks for objc_image_info.flags */ 3597 #define OBJC_IMAGE_IS_REPLACEMENT (1 << 0) 3598 #define OBJC_IMAGE_SUPPORTS_GC (1 << 1) 3599 #define OBJC_IMAGE_IS_SIMULATED (1 << 5) 3600 #define OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES (1 << 6) 3601 3602 struct message_ref64 { 3603 uint64_t imp; /* IMP (64-bit pointer) */ 3604 uint64_t sel; /* SEL (64-bit pointer) */ 3605 }; 3606 3607 struct message_ref32 { 3608 uint32_t imp; /* IMP (32-bit pointer) */ 3609 uint32_t sel; /* SEL (32-bit pointer) */ 3610 }; 3611 3612 // Objective-C 1 (32-bit only) meta data structs. 3613 3614 struct objc_module_t { 3615 uint32_t version; 3616 uint32_t size; 3617 uint32_t name; /* char * (32-bit pointer) */ 3618 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */ 3619 }; 3620 3621 struct objc_symtab_t { 3622 uint32_t sel_ref_cnt; 3623 uint32_t refs; /* SEL * (32-bit pointer) */ 3624 uint16_t cls_def_cnt; 3625 uint16_t cat_def_cnt; 3626 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */ 3627 }; 3628 3629 struct objc_class_t { 3630 uint32_t isa; /* struct objc_class * (32-bit pointer) */ 3631 uint32_t super_class; /* struct objc_class * (32-bit pointer) */ 3632 uint32_t name; /* const char * (32-bit pointer) */ 3633 int32_t version; 3634 int32_t info; 3635 int32_t instance_size; 3636 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */ 3637 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */ 3638 uint32_t cache; /* struct objc_cache * (32-bit pointer) */ 3639 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */ 3640 }; 3641 3642 #define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask)) 3643 // class is not a metaclass 3644 #define CLS_CLASS 0x1 3645 // class is a metaclass 3646 #define CLS_META 0x2 3647 3648 struct objc_category_t { 3649 uint32_t category_name; /* char * (32-bit pointer) */ 3650 uint32_t class_name; /* char * (32-bit pointer) */ 3651 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */ 3652 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */ 3653 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */ 3654 }; 3655 3656 struct objc_ivar_t { 3657 uint32_t ivar_name; /* char * (32-bit pointer) */ 3658 uint32_t ivar_type; /* char * (32-bit pointer) */ 3659 int32_t ivar_offset; 3660 }; 3661 3662 struct objc_ivar_list_t { 3663 int32_t ivar_count; 3664 // struct objc_ivar_t ivar_list[1]; /* variable length structure */ 3665 }; 3666 3667 struct objc_method_list_t { 3668 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */ 3669 int32_t method_count; 3670 // struct objc_method_t method_list[1]; /* variable length structure */ 3671 }; 3672 3673 struct objc_method_t { 3674 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */ 3675 uint32_t method_types; /* char * (32-bit pointer) */ 3676 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...) 3677 (32-bit pointer) */ 3678 }; 3679 3680 struct objc_protocol_list_t { 3681 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */ 3682 int32_t count; 3683 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t * 3684 // (32-bit pointer) */ 3685 }; 3686 3687 struct objc_protocol_t { 3688 uint32_t isa; /* struct objc_class * (32-bit pointer) */ 3689 uint32_t protocol_name; /* char * (32-bit pointer) */ 3690 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */ 3691 uint32_t instance_methods; /* struct objc_method_description_list * 3692 (32-bit pointer) */ 3693 uint32_t class_methods; /* struct objc_method_description_list * 3694 (32-bit pointer) */ 3695 }; 3696 3697 struct objc_method_description_list_t { 3698 int32_t count; 3699 // struct objc_method_description_t list[1]; 3700 }; 3701 3702 struct objc_method_description_t { 3703 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */ 3704 uint32_t types; /* char * (32-bit pointer) */ 3705 }; 3706 3707 inline void swapStruct(struct cfstring64_t &cfs) { 3708 sys::swapByteOrder(cfs.isa); 3709 sys::swapByteOrder(cfs.flags); 3710 sys::swapByteOrder(cfs.characters); 3711 sys::swapByteOrder(cfs.length); 3712 } 3713 3714 inline void swapStruct(struct class64_t &c) { 3715 sys::swapByteOrder(c.isa); 3716 sys::swapByteOrder(c.superclass); 3717 sys::swapByteOrder(c.cache); 3718 sys::swapByteOrder(c.vtable); 3719 sys::swapByteOrder(c.data); 3720 } 3721 3722 inline void swapStruct(struct class32_t &c) { 3723 sys::swapByteOrder(c.isa); 3724 sys::swapByteOrder(c.superclass); 3725 sys::swapByteOrder(c.cache); 3726 sys::swapByteOrder(c.vtable); 3727 sys::swapByteOrder(c.data); 3728 } 3729 3730 inline void swapStruct(struct class_ro64_t &cro) { 3731 sys::swapByteOrder(cro.flags); 3732 sys::swapByteOrder(cro.instanceStart); 3733 sys::swapByteOrder(cro.instanceSize); 3734 sys::swapByteOrder(cro.reserved); 3735 sys::swapByteOrder(cro.ivarLayout); 3736 sys::swapByteOrder(cro.name); 3737 sys::swapByteOrder(cro.baseMethods); 3738 sys::swapByteOrder(cro.baseProtocols); 3739 sys::swapByteOrder(cro.ivars); 3740 sys::swapByteOrder(cro.weakIvarLayout); 3741 sys::swapByteOrder(cro.baseProperties); 3742 } 3743 3744 inline void swapStruct(struct class_ro32_t &cro) { 3745 sys::swapByteOrder(cro.flags); 3746 sys::swapByteOrder(cro.instanceStart); 3747 sys::swapByteOrder(cro.instanceSize); 3748 sys::swapByteOrder(cro.ivarLayout); 3749 sys::swapByteOrder(cro.name); 3750 sys::swapByteOrder(cro.baseMethods); 3751 sys::swapByteOrder(cro.baseProtocols); 3752 sys::swapByteOrder(cro.ivars); 3753 sys::swapByteOrder(cro.weakIvarLayout); 3754 sys::swapByteOrder(cro.baseProperties); 3755 } 3756 3757 inline void swapStruct(struct method_list64_t &ml) { 3758 sys::swapByteOrder(ml.entsize); 3759 sys::swapByteOrder(ml.count); 3760 } 3761 3762 inline void swapStruct(struct method_list32_t &ml) { 3763 sys::swapByteOrder(ml.entsize); 3764 sys::swapByteOrder(ml.count); 3765 } 3766 3767 inline void swapStruct(struct method64_t &m) { 3768 sys::swapByteOrder(m.name); 3769 sys::swapByteOrder(m.types); 3770 sys::swapByteOrder(m.imp); 3771 } 3772 3773 inline void swapStruct(struct method32_t &m) { 3774 sys::swapByteOrder(m.name); 3775 sys::swapByteOrder(m.types); 3776 sys::swapByteOrder(m.imp); 3777 } 3778 3779 inline void swapStruct(struct protocol_list64_t &pl) { 3780 sys::swapByteOrder(pl.count); 3781 } 3782 3783 inline void swapStruct(struct protocol_list32_t &pl) { 3784 sys::swapByteOrder(pl.count); 3785 } 3786 3787 inline void swapStruct(struct protocol64_t &p) { 3788 sys::swapByteOrder(p.isa); 3789 sys::swapByteOrder(p.name); 3790 sys::swapByteOrder(p.protocols); 3791 sys::swapByteOrder(p.instanceMethods); 3792 sys::swapByteOrder(p.classMethods); 3793 sys::swapByteOrder(p.optionalInstanceMethods); 3794 sys::swapByteOrder(p.optionalClassMethods); 3795 sys::swapByteOrder(p.instanceProperties); 3796 } 3797 3798 inline void swapStruct(struct protocol32_t &p) { 3799 sys::swapByteOrder(p.isa); 3800 sys::swapByteOrder(p.name); 3801 sys::swapByteOrder(p.protocols); 3802 sys::swapByteOrder(p.instanceMethods); 3803 sys::swapByteOrder(p.classMethods); 3804 sys::swapByteOrder(p.optionalInstanceMethods); 3805 sys::swapByteOrder(p.optionalClassMethods); 3806 sys::swapByteOrder(p.instanceProperties); 3807 } 3808 3809 inline void swapStruct(struct ivar_list64_t &il) { 3810 sys::swapByteOrder(il.entsize); 3811 sys::swapByteOrder(il.count); 3812 } 3813 3814 inline void swapStruct(struct ivar_list32_t &il) { 3815 sys::swapByteOrder(il.entsize); 3816 sys::swapByteOrder(il.count); 3817 } 3818 3819 inline void swapStruct(struct ivar64_t &i) { 3820 sys::swapByteOrder(i.offset); 3821 sys::swapByteOrder(i.name); 3822 sys::swapByteOrder(i.type); 3823 sys::swapByteOrder(i.alignment); 3824 sys::swapByteOrder(i.size); 3825 } 3826 3827 inline void swapStruct(struct ivar32_t &i) { 3828 sys::swapByteOrder(i.offset); 3829 sys::swapByteOrder(i.name); 3830 sys::swapByteOrder(i.type); 3831 sys::swapByteOrder(i.alignment); 3832 sys::swapByteOrder(i.size); 3833 } 3834 3835 inline void swapStruct(struct objc_property_list64 &pl) { 3836 sys::swapByteOrder(pl.entsize); 3837 sys::swapByteOrder(pl.count); 3838 } 3839 3840 inline void swapStruct(struct objc_property_list32 &pl) { 3841 sys::swapByteOrder(pl.entsize); 3842 sys::swapByteOrder(pl.count); 3843 } 3844 3845 inline void swapStruct(struct objc_property64 &op) { 3846 sys::swapByteOrder(op.name); 3847 sys::swapByteOrder(op.attributes); 3848 } 3849 3850 inline void swapStruct(struct objc_property32 &op) { 3851 sys::swapByteOrder(op.name); 3852 sys::swapByteOrder(op.attributes); 3853 } 3854 3855 inline void swapStruct(struct category64_t &c) { 3856 sys::swapByteOrder(c.name); 3857 sys::swapByteOrder(c.cls); 3858 sys::swapByteOrder(c.instanceMethods); 3859 sys::swapByteOrder(c.classMethods); 3860 sys::swapByteOrder(c.protocols); 3861 sys::swapByteOrder(c.instanceProperties); 3862 } 3863 3864 inline void swapStruct(struct category32_t &c) { 3865 sys::swapByteOrder(c.name); 3866 sys::swapByteOrder(c.cls); 3867 sys::swapByteOrder(c.instanceMethods); 3868 sys::swapByteOrder(c.classMethods); 3869 sys::swapByteOrder(c.protocols); 3870 sys::swapByteOrder(c.instanceProperties); 3871 } 3872 3873 inline void swapStruct(struct objc_image_info64 &o) { 3874 sys::swapByteOrder(o.version); 3875 sys::swapByteOrder(o.flags); 3876 } 3877 3878 inline void swapStruct(struct objc_image_info32 &o) { 3879 sys::swapByteOrder(o.version); 3880 sys::swapByteOrder(o.flags); 3881 } 3882 3883 inline void swapStruct(struct imageInfo_t &o) { 3884 sys::swapByteOrder(o.version); 3885 sys::swapByteOrder(o.flags); 3886 } 3887 3888 inline void swapStruct(struct message_ref64 &mr) { 3889 sys::swapByteOrder(mr.imp); 3890 sys::swapByteOrder(mr.sel); 3891 } 3892 3893 inline void swapStruct(struct message_ref32 &mr) { 3894 sys::swapByteOrder(mr.imp); 3895 sys::swapByteOrder(mr.sel); 3896 } 3897 3898 inline void swapStruct(struct objc_module_t &module) { 3899 sys::swapByteOrder(module.version); 3900 sys::swapByteOrder(module.size); 3901 sys::swapByteOrder(module.name); 3902 sys::swapByteOrder(module.symtab); 3903 } 3904 3905 inline void swapStruct(struct objc_symtab_t &symtab) { 3906 sys::swapByteOrder(symtab.sel_ref_cnt); 3907 sys::swapByteOrder(symtab.refs); 3908 sys::swapByteOrder(symtab.cls_def_cnt); 3909 sys::swapByteOrder(symtab.cat_def_cnt); 3910 } 3911 3912 inline void swapStruct(struct objc_class_t &objc_class) { 3913 sys::swapByteOrder(objc_class.isa); 3914 sys::swapByteOrder(objc_class.super_class); 3915 sys::swapByteOrder(objc_class.name); 3916 sys::swapByteOrder(objc_class.version); 3917 sys::swapByteOrder(objc_class.info); 3918 sys::swapByteOrder(objc_class.instance_size); 3919 sys::swapByteOrder(objc_class.ivars); 3920 sys::swapByteOrder(objc_class.methodLists); 3921 sys::swapByteOrder(objc_class.cache); 3922 sys::swapByteOrder(objc_class.protocols); 3923 } 3924 3925 inline void swapStruct(struct objc_category_t &objc_category) { 3926 sys::swapByteOrder(objc_category.category_name); 3927 sys::swapByteOrder(objc_category.class_name); 3928 sys::swapByteOrder(objc_category.instance_methods); 3929 sys::swapByteOrder(objc_category.class_methods); 3930 sys::swapByteOrder(objc_category.protocols); 3931 } 3932 3933 inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) { 3934 sys::swapByteOrder(objc_ivar_list.ivar_count); 3935 } 3936 3937 inline void swapStruct(struct objc_ivar_t &objc_ivar) { 3938 sys::swapByteOrder(objc_ivar.ivar_name); 3939 sys::swapByteOrder(objc_ivar.ivar_type); 3940 sys::swapByteOrder(objc_ivar.ivar_offset); 3941 } 3942 3943 inline void swapStruct(struct objc_method_list_t &method_list) { 3944 sys::swapByteOrder(method_list.obsolete); 3945 sys::swapByteOrder(method_list.method_count); 3946 } 3947 3948 inline void swapStruct(struct objc_method_t &method) { 3949 sys::swapByteOrder(method.method_name); 3950 sys::swapByteOrder(method.method_types); 3951 sys::swapByteOrder(method.method_imp); 3952 } 3953 3954 inline void swapStruct(struct objc_protocol_list_t &protocol_list) { 3955 sys::swapByteOrder(protocol_list.next); 3956 sys::swapByteOrder(protocol_list.count); 3957 } 3958 3959 inline void swapStruct(struct objc_protocol_t &protocol) { 3960 sys::swapByteOrder(protocol.isa); 3961 sys::swapByteOrder(protocol.protocol_name); 3962 sys::swapByteOrder(protocol.protocol_list); 3963 sys::swapByteOrder(protocol.instance_methods); 3964 sys::swapByteOrder(protocol.class_methods); 3965 } 3966 3967 inline void swapStruct(struct objc_method_description_list_t &mdl) { 3968 sys::swapByteOrder(mdl.count); 3969 } 3970 3971 inline void swapStruct(struct objc_method_description_t &md) { 3972 sys::swapByteOrder(md.name); 3973 sys::swapByteOrder(md.types); 3974 } 3975 3976 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue, 3977 struct DisassembleInfo *info); 3978 3979 // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer 3980 // to an Objective-C class and returns the class name. It is also passed the 3981 // address of the pointer, so when the pointer is zero as it can be in an .o 3982 // file, that is used to look for an external relocation entry with a symbol 3983 // name. 3984 static const char *get_objc2_64bit_class_name(uint64_t pointer_value, 3985 uint64_t ReferenceValue, 3986 struct DisassembleInfo *info) { 3987 const char *r; 3988 uint32_t offset, left; 3989 SectionRef S; 3990 3991 // The pointer_value can be 0 in an object file and have a relocation 3992 // entry for the class symbol at the ReferenceValue (the address of the 3993 // pointer). 3994 if (pointer_value == 0) { 3995 r = get_pointer_64(ReferenceValue, offset, left, S, info); 3996 if (r == nullptr || left < sizeof(uint64_t)) 3997 return nullptr; 3998 uint64_t n_value; 3999 const char *symbol_name = get_symbol_64(offset, S, info, n_value); 4000 if (symbol_name == nullptr) 4001 return nullptr; 4002 const char *class_name = strrchr(symbol_name, '$'); 4003 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0') 4004 return class_name + 2; 4005 else 4006 return nullptr; 4007 } 4008 4009 // The case were the pointer_value is non-zero and points to a class defined 4010 // in this Mach-O file. 4011 r = get_pointer_64(pointer_value, offset, left, S, info); 4012 if (r == nullptr || left < sizeof(struct class64_t)) 4013 return nullptr; 4014 struct class64_t c; 4015 memcpy(&c, r, sizeof(struct class64_t)); 4016 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4017 swapStruct(c); 4018 if (c.data == 0) 4019 return nullptr; 4020 r = get_pointer_64(c.data, offset, left, S, info); 4021 if (r == nullptr || left < sizeof(struct class_ro64_t)) 4022 return nullptr; 4023 struct class_ro64_t cro; 4024 memcpy(&cro, r, sizeof(struct class_ro64_t)); 4025 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4026 swapStruct(cro); 4027 if (cro.name == 0) 4028 return nullptr; 4029 const char *name = get_pointer_64(cro.name, offset, left, S, info); 4030 return name; 4031 } 4032 4033 // get_objc2_64bit_cfstring_name is used for disassembly and is passed a 4034 // pointer to a cfstring and returns its name or nullptr. 4035 static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue, 4036 struct DisassembleInfo *info) { 4037 const char *r, *name; 4038 uint32_t offset, left; 4039 SectionRef S; 4040 struct cfstring64_t cfs; 4041 uint64_t cfs_characters; 4042 4043 r = get_pointer_64(ReferenceValue, offset, left, S, info); 4044 if (r == nullptr || left < sizeof(struct cfstring64_t)) 4045 return nullptr; 4046 memcpy(&cfs, r, sizeof(struct cfstring64_t)); 4047 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4048 swapStruct(cfs); 4049 if (cfs.characters == 0) { 4050 uint64_t n_value; 4051 const char *symbol_name = get_symbol_64( 4052 offset + offsetof(struct cfstring64_t, characters), S, info, n_value); 4053 if (symbol_name == nullptr) 4054 return nullptr; 4055 cfs_characters = n_value; 4056 } else 4057 cfs_characters = cfs.characters; 4058 name = get_pointer_64(cfs_characters, offset, left, S, info); 4059 4060 return name; 4061 } 4062 4063 // get_objc2_64bit_selref() is used for disassembly and is passed a the address 4064 // of a pointer to an Objective-C selector reference when the pointer value is 4065 // zero as in a .o file and is likely to have a external relocation entry with 4066 // who's symbol's n_value is the real pointer to the selector name. If that is 4067 // the case the real pointer to the selector name is returned else 0 is 4068 // returned 4069 static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue, 4070 struct DisassembleInfo *info) { 4071 uint32_t offset, left; 4072 SectionRef S; 4073 4074 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info); 4075 if (r == nullptr || left < sizeof(uint64_t)) 4076 return 0; 4077 uint64_t n_value; 4078 const char *symbol_name = get_symbol_64(offset, S, info, n_value); 4079 if (symbol_name == nullptr) 4080 return 0; 4081 return n_value; 4082 } 4083 4084 static const SectionRef get_section(MachOObjectFile *O, const char *segname, 4085 const char *sectname) { 4086 for (const SectionRef &Section : O->sections()) { 4087 StringRef SectName; 4088 Expected<StringRef> SecNameOrErr = Section.getName(); 4089 if (SecNameOrErr) 4090 SectName = *SecNameOrErr; 4091 else 4092 consumeError(SecNameOrErr.takeError()); 4093 4094 DataRefImpl Ref = Section.getRawDataRefImpl(); 4095 StringRef SegName = O->getSectionFinalSegmentName(Ref); 4096 if (SegName == segname && SectName == sectname) 4097 return Section; 4098 } 4099 return SectionRef(); 4100 } 4101 4102 static void 4103 walk_pointer_list_64(const char *listname, const SectionRef S, 4104 MachOObjectFile *O, struct DisassembleInfo *info, 4105 void (*func)(uint64_t, struct DisassembleInfo *info)) { 4106 if (S == SectionRef()) 4107 return; 4108 4109 StringRef SectName; 4110 Expected<StringRef> SecNameOrErr = S.getName(); 4111 if (SecNameOrErr) 4112 SectName = *SecNameOrErr; 4113 else 4114 consumeError(SecNameOrErr.takeError()); 4115 4116 DataRefImpl Ref = S.getRawDataRefImpl(); 4117 StringRef SegName = O->getSectionFinalSegmentName(Ref); 4118 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 4119 4120 StringRef BytesStr = unwrapOrError(S.getContents(), O->getFileName()); 4121 const char *Contents = reinterpret_cast<const char *>(BytesStr.data()); 4122 4123 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) { 4124 uint32_t left = S.getSize() - i; 4125 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t); 4126 uint64_t p = 0; 4127 memcpy(&p, Contents + i, size); 4128 if (i + sizeof(uint64_t) > S.getSize()) 4129 outs() << listname << " list pointer extends past end of (" << SegName 4130 << "," << SectName << ") section\n"; 4131 outs() << format("%016" PRIx64, S.getAddress() + i) << " "; 4132 4133 if (O->isLittleEndian() != sys::IsLittleEndianHost) 4134 sys::swapByteOrder(p); 4135 4136 uint64_t n_value = 0; 4137 const char *name = get_symbol_64(i, S, info, n_value, p); 4138 if (name == nullptr) 4139 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info); 4140 4141 if (n_value != 0) { 4142 outs() << format("0x%" PRIx64, n_value); 4143 if (p != 0) 4144 outs() << " + " << format("0x%" PRIx64, p); 4145 } else 4146 outs() << format("0x%" PRIx64, p); 4147 if (name != nullptr) 4148 outs() << " " << name; 4149 outs() << "\n"; 4150 4151 p += n_value; 4152 if (func) 4153 func(p, info); 4154 } 4155 } 4156 4157 static void 4158 walk_pointer_list_32(const char *listname, const SectionRef S, 4159 MachOObjectFile *O, struct DisassembleInfo *info, 4160 void (*func)(uint32_t, struct DisassembleInfo *info)) { 4161 if (S == SectionRef()) 4162 return; 4163 4164 StringRef SectName = unwrapOrError(S.getName(), O->getFileName()); 4165 DataRefImpl Ref = S.getRawDataRefImpl(); 4166 StringRef SegName = O->getSectionFinalSegmentName(Ref); 4167 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 4168 4169 StringRef BytesStr = unwrapOrError(S.getContents(), O->getFileName()); 4170 const char *Contents = reinterpret_cast<const char *>(BytesStr.data()); 4171 4172 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) { 4173 uint32_t left = S.getSize() - i; 4174 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t); 4175 uint32_t p = 0; 4176 memcpy(&p, Contents + i, size); 4177 if (i + sizeof(uint32_t) > S.getSize()) 4178 outs() << listname << " list pointer extends past end of (" << SegName 4179 << "," << SectName << ") section\n"; 4180 uint32_t Address = S.getAddress() + i; 4181 outs() << format("%08" PRIx32, Address) << " "; 4182 4183 if (O->isLittleEndian() != sys::IsLittleEndianHost) 4184 sys::swapByteOrder(p); 4185 outs() << format("0x%" PRIx32, p); 4186 4187 const char *name = get_symbol_32(i, S, info, p); 4188 if (name != nullptr) 4189 outs() << " " << name; 4190 outs() << "\n"; 4191 4192 if (func) 4193 func(p, info); 4194 } 4195 } 4196 4197 static void print_layout_map(const char *layout_map, uint32_t left) { 4198 if (layout_map == nullptr) 4199 return; 4200 outs() << " layout map: "; 4201 do { 4202 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " "; 4203 left--; 4204 layout_map++; 4205 } while (*layout_map != '\0' && left != 0); 4206 outs() << "\n"; 4207 } 4208 4209 static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) { 4210 uint32_t offset, left; 4211 SectionRef S; 4212 const char *layout_map; 4213 4214 if (p == 0) 4215 return; 4216 layout_map = get_pointer_64(p, offset, left, S, info); 4217 print_layout_map(layout_map, left); 4218 } 4219 4220 static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) { 4221 uint32_t offset, left; 4222 SectionRef S; 4223 const char *layout_map; 4224 4225 if (p == 0) 4226 return; 4227 layout_map = get_pointer_32(p, offset, left, S, info); 4228 print_layout_map(layout_map, left); 4229 } 4230 4231 static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info, 4232 const char *indent) { 4233 struct method_list64_t ml; 4234 struct method64_t m; 4235 const char *r; 4236 uint32_t offset, xoffset, left, i; 4237 SectionRef S, xS; 4238 const char *name, *sym_name; 4239 uint64_t n_value; 4240 4241 r = get_pointer_64(p, offset, left, S, info); 4242 if (r == nullptr) 4243 return; 4244 memset(&ml, '\0', sizeof(struct method_list64_t)); 4245 if (left < sizeof(struct method_list64_t)) { 4246 memcpy(&ml, r, left); 4247 outs() << " (method_list_t entends past the end of the section)\n"; 4248 } else 4249 memcpy(&ml, r, sizeof(struct method_list64_t)); 4250 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4251 swapStruct(ml); 4252 outs() << indent << "\t\t entsize " << ml.entsize << "\n"; 4253 outs() << indent << "\t\t count " << ml.count << "\n"; 4254 4255 p += sizeof(struct method_list64_t); 4256 offset += sizeof(struct method_list64_t); 4257 for (i = 0; i < ml.count; i++) { 4258 r = get_pointer_64(p, offset, left, S, info); 4259 if (r == nullptr) 4260 return; 4261 memset(&m, '\0', sizeof(struct method64_t)); 4262 if (left < sizeof(struct method64_t)) { 4263 memcpy(&m, r, left); 4264 outs() << indent << " (method_t extends past the end of the section)\n"; 4265 } else 4266 memcpy(&m, r, sizeof(struct method64_t)); 4267 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4268 swapStruct(m); 4269 4270 outs() << indent << "\t\t name "; 4271 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S, 4272 info, n_value, m.name); 4273 if (n_value != 0) { 4274 if (info->verbose && sym_name != nullptr) 4275 outs() << sym_name; 4276 else 4277 outs() << format("0x%" PRIx64, n_value); 4278 if (m.name != 0) 4279 outs() << " + " << format("0x%" PRIx64, m.name); 4280 } else 4281 outs() << format("0x%" PRIx64, m.name); 4282 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info); 4283 if (name != nullptr) 4284 outs() << format(" %.*s", left, name); 4285 outs() << "\n"; 4286 4287 outs() << indent << "\t\t types "; 4288 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S, 4289 info, n_value, m.types); 4290 if (n_value != 0) { 4291 if (info->verbose && sym_name != nullptr) 4292 outs() << sym_name; 4293 else 4294 outs() << format("0x%" PRIx64, n_value); 4295 if (m.types != 0) 4296 outs() << " + " << format("0x%" PRIx64, m.types); 4297 } else 4298 outs() << format("0x%" PRIx64, m.types); 4299 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info); 4300 if (name != nullptr) 4301 outs() << format(" %.*s", left, name); 4302 outs() << "\n"; 4303 4304 outs() << indent << "\t\t imp "; 4305 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info, 4306 n_value, m.imp); 4307 if (info->verbose && name == nullptr) { 4308 if (n_value != 0) { 4309 outs() << format("0x%" PRIx64, n_value) << " "; 4310 if (m.imp != 0) 4311 outs() << "+ " << format("0x%" PRIx64, m.imp) << " "; 4312 } else 4313 outs() << format("0x%" PRIx64, m.imp) << " "; 4314 } 4315 if (name != nullptr) 4316 outs() << name; 4317 outs() << "\n"; 4318 4319 p += sizeof(struct method64_t); 4320 offset += sizeof(struct method64_t); 4321 } 4322 } 4323 4324 static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info, 4325 const char *indent) { 4326 struct method_list32_t ml; 4327 struct method32_t m; 4328 const char *r, *name; 4329 uint32_t offset, xoffset, left, i; 4330 SectionRef S, xS; 4331 4332 r = get_pointer_32(p, offset, left, S, info); 4333 if (r == nullptr) 4334 return; 4335 memset(&ml, '\0', sizeof(struct method_list32_t)); 4336 if (left < sizeof(struct method_list32_t)) { 4337 memcpy(&ml, r, left); 4338 outs() << " (method_list_t entends past the end of the section)\n"; 4339 } else 4340 memcpy(&ml, r, sizeof(struct method_list32_t)); 4341 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4342 swapStruct(ml); 4343 outs() << indent << "\t\t entsize " << ml.entsize << "\n"; 4344 outs() << indent << "\t\t count " << ml.count << "\n"; 4345 4346 p += sizeof(struct method_list32_t); 4347 offset += sizeof(struct method_list32_t); 4348 for (i = 0; i < ml.count; i++) { 4349 r = get_pointer_32(p, offset, left, S, info); 4350 if (r == nullptr) 4351 return; 4352 memset(&m, '\0', sizeof(struct method32_t)); 4353 if (left < sizeof(struct method32_t)) { 4354 memcpy(&ml, r, left); 4355 outs() << indent << " (method_t entends past the end of the section)\n"; 4356 } else 4357 memcpy(&m, r, sizeof(struct method32_t)); 4358 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4359 swapStruct(m); 4360 4361 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name); 4362 name = get_pointer_32(m.name, xoffset, left, xS, info); 4363 if (name != nullptr) 4364 outs() << format(" %.*s", left, name); 4365 outs() << "\n"; 4366 4367 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types); 4368 name = get_pointer_32(m.types, xoffset, left, xS, info); 4369 if (name != nullptr) 4370 outs() << format(" %.*s", left, name); 4371 outs() << "\n"; 4372 4373 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp); 4374 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info, 4375 m.imp); 4376 if (name != nullptr) 4377 outs() << " " << name; 4378 outs() << "\n"; 4379 4380 p += sizeof(struct method32_t); 4381 offset += sizeof(struct method32_t); 4382 } 4383 } 4384 4385 static bool print_method_list(uint32_t p, struct DisassembleInfo *info) { 4386 uint32_t offset, left, xleft; 4387 SectionRef S; 4388 struct objc_method_list_t method_list; 4389 struct objc_method_t method; 4390 const char *r, *methods, *name, *SymbolName; 4391 int32_t i; 4392 4393 r = get_pointer_32(p, offset, left, S, info, true); 4394 if (r == nullptr) 4395 return true; 4396 4397 outs() << "\n"; 4398 if (left > sizeof(struct objc_method_list_t)) { 4399 memcpy(&method_list, r, sizeof(struct objc_method_list_t)); 4400 } else { 4401 outs() << "\t\t objc_method_list extends past end of the section\n"; 4402 memset(&method_list, '\0', sizeof(struct objc_method_list_t)); 4403 memcpy(&method_list, r, left); 4404 } 4405 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4406 swapStruct(method_list); 4407 4408 outs() << "\t\t obsolete " 4409 << format("0x%08" PRIx32, method_list.obsolete) << "\n"; 4410 outs() << "\t\t method_count " << method_list.method_count << "\n"; 4411 4412 methods = r + sizeof(struct objc_method_list_t); 4413 for (i = 0; i < method_list.method_count; i++) { 4414 if ((i + 1) * sizeof(struct objc_method_t) > left) { 4415 outs() << "\t\t remaining method's extend past the of the section\n"; 4416 break; 4417 } 4418 memcpy(&method, methods + i * sizeof(struct objc_method_t), 4419 sizeof(struct objc_method_t)); 4420 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4421 swapStruct(method); 4422 4423 outs() << "\t\t method_name " 4424 << format("0x%08" PRIx32, method.method_name); 4425 if (info->verbose) { 4426 name = get_pointer_32(method.method_name, offset, xleft, S, info, true); 4427 if (name != nullptr) 4428 outs() << format(" %.*s", xleft, name); 4429 else 4430 outs() << " (not in an __OBJC section)"; 4431 } 4432 outs() << "\n"; 4433 4434 outs() << "\t\t method_types " 4435 << format("0x%08" PRIx32, method.method_types); 4436 if (info->verbose) { 4437 name = get_pointer_32(method.method_types, offset, xleft, S, info, true); 4438 if (name != nullptr) 4439 outs() << format(" %.*s", xleft, name); 4440 else 4441 outs() << " (not in an __OBJC section)"; 4442 } 4443 outs() << "\n"; 4444 4445 outs() << "\t\t method_imp " 4446 << format("0x%08" PRIx32, method.method_imp) << " "; 4447 if (info->verbose) { 4448 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap); 4449 if (SymbolName != nullptr) 4450 outs() << SymbolName; 4451 } 4452 outs() << "\n"; 4453 } 4454 return false; 4455 } 4456 4457 static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) { 4458 struct protocol_list64_t pl; 4459 uint64_t q, n_value; 4460 struct protocol64_t pc; 4461 const char *r; 4462 uint32_t offset, xoffset, left, i; 4463 SectionRef S, xS; 4464 const char *name, *sym_name; 4465 4466 r = get_pointer_64(p, offset, left, S, info); 4467 if (r == nullptr) 4468 return; 4469 memset(&pl, '\0', sizeof(struct protocol_list64_t)); 4470 if (left < sizeof(struct protocol_list64_t)) { 4471 memcpy(&pl, r, left); 4472 outs() << " (protocol_list_t entends past the end of the section)\n"; 4473 } else 4474 memcpy(&pl, r, sizeof(struct protocol_list64_t)); 4475 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4476 swapStruct(pl); 4477 outs() << " count " << pl.count << "\n"; 4478 4479 p += sizeof(struct protocol_list64_t); 4480 offset += sizeof(struct protocol_list64_t); 4481 for (i = 0; i < pl.count; i++) { 4482 r = get_pointer_64(p, offset, left, S, info); 4483 if (r == nullptr) 4484 return; 4485 q = 0; 4486 if (left < sizeof(uint64_t)) { 4487 memcpy(&q, r, left); 4488 outs() << " (protocol_t * entends past the end of the section)\n"; 4489 } else 4490 memcpy(&q, r, sizeof(uint64_t)); 4491 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4492 sys::swapByteOrder(q); 4493 4494 outs() << "\t\t list[" << i << "] "; 4495 sym_name = get_symbol_64(offset, S, info, n_value, q); 4496 if (n_value != 0) { 4497 if (info->verbose && sym_name != nullptr) 4498 outs() << sym_name; 4499 else 4500 outs() << format("0x%" PRIx64, n_value); 4501 if (q != 0) 4502 outs() << " + " << format("0x%" PRIx64, q); 4503 } else 4504 outs() << format("0x%" PRIx64, q); 4505 outs() << " (struct protocol_t *)\n"; 4506 4507 r = get_pointer_64(q + n_value, offset, left, S, info); 4508 if (r == nullptr) 4509 return; 4510 memset(&pc, '\0', sizeof(struct protocol64_t)); 4511 if (left < sizeof(struct protocol64_t)) { 4512 memcpy(&pc, r, left); 4513 outs() << " (protocol_t entends past the end of the section)\n"; 4514 } else 4515 memcpy(&pc, r, sizeof(struct protocol64_t)); 4516 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4517 swapStruct(pc); 4518 4519 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n"; 4520 4521 outs() << "\t\t\t name "; 4522 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S, 4523 info, n_value, pc.name); 4524 if (n_value != 0) { 4525 if (info->verbose && sym_name != nullptr) 4526 outs() << sym_name; 4527 else 4528 outs() << format("0x%" PRIx64, n_value); 4529 if (pc.name != 0) 4530 outs() << " + " << format("0x%" PRIx64, pc.name); 4531 } else 4532 outs() << format("0x%" PRIx64, pc.name); 4533 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info); 4534 if (name != nullptr) 4535 outs() << format(" %.*s", left, name); 4536 outs() << "\n"; 4537 4538 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n"; 4539 4540 outs() << "\t\t instanceMethods "; 4541 sym_name = 4542 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods), 4543 S, info, n_value, pc.instanceMethods); 4544 if (n_value != 0) { 4545 if (info->verbose && sym_name != nullptr) 4546 outs() << sym_name; 4547 else 4548 outs() << format("0x%" PRIx64, n_value); 4549 if (pc.instanceMethods != 0) 4550 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods); 4551 } else 4552 outs() << format("0x%" PRIx64, pc.instanceMethods); 4553 outs() << " (struct method_list_t *)\n"; 4554 if (pc.instanceMethods + n_value != 0) 4555 print_method_list64_t(pc.instanceMethods + n_value, info, "\t"); 4556 4557 outs() << "\t\t classMethods "; 4558 sym_name = 4559 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S, 4560 info, n_value, pc.classMethods); 4561 if (n_value != 0) { 4562 if (info->verbose && sym_name != nullptr) 4563 outs() << sym_name; 4564 else 4565 outs() << format("0x%" PRIx64, n_value); 4566 if (pc.classMethods != 0) 4567 outs() << " + " << format("0x%" PRIx64, pc.classMethods); 4568 } else 4569 outs() << format("0x%" PRIx64, pc.classMethods); 4570 outs() << " (struct method_list_t *)\n"; 4571 if (pc.classMethods + n_value != 0) 4572 print_method_list64_t(pc.classMethods + n_value, info, "\t"); 4573 4574 outs() << "\t optionalInstanceMethods " 4575 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n"; 4576 outs() << "\t optionalClassMethods " 4577 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n"; 4578 outs() << "\t instanceProperties " 4579 << format("0x%" PRIx64, pc.instanceProperties) << "\n"; 4580 4581 p += sizeof(uint64_t); 4582 offset += sizeof(uint64_t); 4583 } 4584 } 4585 4586 static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) { 4587 struct protocol_list32_t pl; 4588 uint32_t q; 4589 struct protocol32_t pc; 4590 const char *r; 4591 uint32_t offset, xoffset, left, i; 4592 SectionRef S, xS; 4593 const char *name; 4594 4595 r = get_pointer_32(p, offset, left, S, info); 4596 if (r == nullptr) 4597 return; 4598 memset(&pl, '\0', sizeof(struct protocol_list32_t)); 4599 if (left < sizeof(struct protocol_list32_t)) { 4600 memcpy(&pl, r, left); 4601 outs() << " (protocol_list_t entends past the end of the section)\n"; 4602 } else 4603 memcpy(&pl, r, sizeof(struct protocol_list32_t)); 4604 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4605 swapStruct(pl); 4606 outs() << " count " << pl.count << "\n"; 4607 4608 p += sizeof(struct protocol_list32_t); 4609 offset += sizeof(struct protocol_list32_t); 4610 for (i = 0; i < pl.count; i++) { 4611 r = get_pointer_32(p, offset, left, S, info); 4612 if (r == nullptr) 4613 return; 4614 q = 0; 4615 if (left < sizeof(uint32_t)) { 4616 memcpy(&q, r, left); 4617 outs() << " (protocol_t * entends past the end of the section)\n"; 4618 } else 4619 memcpy(&q, r, sizeof(uint32_t)); 4620 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4621 sys::swapByteOrder(q); 4622 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q) 4623 << " (struct protocol_t *)\n"; 4624 r = get_pointer_32(q, offset, left, S, info); 4625 if (r == nullptr) 4626 return; 4627 memset(&pc, '\0', sizeof(struct protocol32_t)); 4628 if (left < sizeof(struct protocol32_t)) { 4629 memcpy(&pc, r, left); 4630 outs() << " (protocol_t entends past the end of the section)\n"; 4631 } else 4632 memcpy(&pc, r, sizeof(struct protocol32_t)); 4633 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4634 swapStruct(pc); 4635 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n"; 4636 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name); 4637 name = get_pointer_32(pc.name, xoffset, left, xS, info); 4638 if (name != nullptr) 4639 outs() << format(" %.*s", left, name); 4640 outs() << "\n"; 4641 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n"; 4642 outs() << "\t\t instanceMethods " 4643 << format("0x%" PRIx32, pc.instanceMethods) 4644 << " (struct method_list_t *)\n"; 4645 if (pc.instanceMethods != 0) 4646 print_method_list32_t(pc.instanceMethods, info, "\t"); 4647 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods) 4648 << " (struct method_list_t *)\n"; 4649 if (pc.classMethods != 0) 4650 print_method_list32_t(pc.classMethods, info, "\t"); 4651 outs() << "\t optionalInstanceMethods " 4652 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n"; 4653 outs() << "\t optionalClassMethods " 4654 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n"; 4655 outs() << "\t instanceProperties " 4656 << format("0x%" PRIx32, pc.instanceProperties) << "\n"; 4657 p += sizeof(uint32_t); 4658 offset += sizeof(uint32_t); 4659 } 4660 } 4661 4662 static void print_indent(uint32_t indent) { 4663 for (uint32_t i = 0; i < indent;) { 4664 if (indent - i >= 8) { 4665 outs() << "\t"; 4666 i += 8; 4667 } else { 4668 for (uint32_t j = i; j < indent; j++) 4669 outs() << " "; 4670 return; 4671 } 4672 } 4673 } 4674 4675 static bool print_method_description_list(uint32_t p, uint32_t indent, 4676 struct DisassembleInfo *info) { 4677 uint32_t offset, left, xleft; 4678 SectionRef S; 4679 struct objc_method_description_list_t mdl; 4680 struct objc_method_description_t md; 4681 const char *r, *list, *name; 4682 int32_t i; 4683 4684 r = get_pointer_32(p, offset, left, S, info, true); 4685 if (r == nullptr) 4686 return true; 4687 4688 outs() << "\n"; 4689 if (left > sizeof(struct objc_method_description_list_t)) { 4690 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t)); 4691 } else { 4692 print_indent(indent); 4693 outs() << " objc_method_description_list extends past end of the section\n"; 4694 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t)); 4695 memcpy(&mdl, r, left); 4696 } 4697 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4698 swapStruct(mdl); 4699 4700 print_indent(indent); 4701 outs() << " count " << mdl.count << "\n"; 4702 4703 list = r + sizeof(struct objc_method_description_list_t); 4704 for (i = 0; i < mdl.count; i++) { 4705 if ((i + 1) * sizeof(struct objc_method_description_t) > left) { 4706 print_indent(indent); 4707 outs() << " remaining list entries extend past the of the section\n"; 4708 break; 4709 } 4710 print_indent(indent); 4711 outs() << " list[" << i << "]\n"; 4712 memcpy(&md, list + i * sizeof(struct objc_method_description_t), 4713 sizeof(struct objc_method_description_t)); 4714 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4715 swapStruct(md); 4716 4717 print_indent(indent); 4718 outs() << " name " << format("0x%08" PRIx32, md.name); 4719 if (info->verbose) { 4720 name = get_pointer_32(md.name, offset, xleft, S, info, true); 4721 if (name != nullptr) 4722 outs() << format(" %.*s", xleft, name); 4723 else 4724 outs() << " (not in an __OBJC section)"; 4725 } 4726 outs() << "\n"; 4727 4728 print_indent(indent); 4729 outs() << " types " << format("0x%08" PRIx32, md.types); 4730 if (info->verbose) { 4731 name = get_pointer_32(md.types, offset, xleft, S, info, true); 4732 if (name != nullptr) 4733 outs() << format(" %.*s", xleft, name); 4734 else 4735 outs() << " (not in an __OBJC section)"; 4736 } 4737 outs() << "\n"; 4738 } 4739 return false; 4740 } 4741 4742 static bool print_protocol_list(uint32_t p, uint32_t indent, 4743 struct DisassembleInfo *info); 4744 4745 static bool print_protocol(uint32_t p, uint32_t indent, 4746 struct DisassembleInfo *info) { 4747 uint32_t offset, left; 4748 SectionRef S; 4749 struct objc_protocol_t protocol; 4750 const char *r, *name; 4751 4752 r = get_pointer_32(p, offset, left, S, info, true); 4753 if (r == nullptr) 4754 return true; 4755 4756 outs() << "\n"; 4757 if (left >= sizeof(struct objc_protocol_t)) { 4758 memcpy(&protocol, r, sizeof(struct objc_protocol_t)); 4759 } else { 4760 print_indent(indent); 4761 outs() << " Protocol extends past end of the section\n"; 4762 memset(&protocol, '\0', sizeof(struct objc_protocol_t)); 4763 memcpy(&protocol, r, left); 4764 } 4765 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4766 swapStruct(protocol); 4767 4768 print_indent(indent); 4769 outs() << " isa " << format("0x%08" PRIx32, protocol.isa) 4770 << "\n"; 4771 4772 print_indent(indent); 4773 outs() << " protocol_name " 4774 << format("0x%08" PRIx32, protocol.protocol_name); 4775 if (info->verbose) { 4776 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true); 4777 if (name != nullptr) 4778 outs() << format(" %.*s", left, name); 4779 else 4780 outs() << " (not in an __OBJC section)"; 4781 } 4782 outs() << "\n"; 4783 4784 print_indent(indent); 4785 outs() << " protocol_list " 4786 << format("0x%08" PRIx32, protocol.protocol_list); 4787 if (print_protocol_list(protocol.protocol_list, indent + 4, info)) 4788 outs() << " (not in an __OBJC section)\n"; 4789 4790 print_indent(indent); 4791 outs() << " instance_methods " 4792 << format("0x%08" PRIx32, protocol.instance_methods); 4793 if (print_method_description_list(protocol.instance_methods, indent, info)) 4794 outs() << " (not in an __OBJC section)\n"; 4795 4796 print_indent(indent); 4797 outs() << " class_methods " 4798 << format("0x%08" PRIx32, protocol.class_methods); 4799 if (print_method_description_list(protocol.class_methods, indent, info)) 4800 outs() << " (not in an __OBJC section)\n"; 4801 4802 return false; 4803 } 4804 4805 static bool print_protocol_list(uint32_t p, uint32_t indent, 4806 struct DisassembleInfo *info) { 4807 uint32_t offset, left, l; 4808 SectionRef S; 4809 struct objc_protocol_list_t protocol_list; 4810 const char *r, *list; 4811 int32_t i; 4812 4813 r = get_pointer_32(p, offset, left, S, info, true); 4814 if (r == nullptr) 4815 return true; 4816 4817 outs() << "\n"; 4818 if (left > sizeof(struct objc_protocol_list_t)) { 4819 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t)); 4820 } else { 4821 outs() << "\t\t objc_protocol_list_t extends past end of the section\n"; 4822 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t)); 4823 memcpy(&protocol_list, r, left); 4824 } 4825 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4826 swapStruct(protocol_list); 4827 4828 print_indent(indent); 4829 outs() << " next " << format("0x%08" PRIx32, protocol_list.next) 4830 << "\n"; 4831 print_indent(indent); 4832 outs() << " count " << protocol_list.count << "\n"; 4833 4834 list = r + sizeof(struct objc_protocol_list_t); 4835 for (i = 0; i < protocol_list.count; i++) { 4836 if ((i + 1) * sizeof(uint32_t) > left) { 4837 outs() << "\t\t remaining list entries extend past the of the section\n"; 4838 break; 4839 } 4840 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t)); 4841 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4842 sys::swapByteOrder(l); 4843 4844 print_indent(indent); 4845 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l); 4846 if (print_protocol(l, indent, info)) 4847 outs() << "(not in an __OBJC section)\n"; 4848 } 4849 return false; 4850 } 4851 4852 static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) { 4853 struct ivar_list64_t il; 4854 struct ivar64_t i; 4855 const char *r; 4856 uint32_t offset, xoffset, left, j; 4857 SectionRef S, xS; 4858 const char *name, *sym_name, *ivar_offset_p; 4859 uint64_t ivar_offset, n_value; 4860 4861 r = get_pointer_64(p, offset, left, S, info); 4862 if (r == nullptr) 4863 return; 4864 memset(&il, '\0', sizeof(struct ivar_list64_t)); 4865 if (left < sizeof(struct ivar_list64_t)) { 4866 memcpy(&il, r, left); 4867 outs() << " (ivar_list_t entends past the end of the section)\n"; 4868 } else 4869 memcpy(&il, r, sizeof(struct ivar_list64_t)); 4870 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4871 swapStruct(il); 4872 outs() << " entsize " << il.entsize << "\n"; 4873 outs() << " count " << il.count << "\n"; 4874 4875 p += sizeof(struct ivar_list64_t); 4876 offset += sizeof(struct ivar_list64_t); 4877 for (j = 0; j < il.count; j++) { 4878 r = get_pointer_64(p, offset, left, S, info); 4879 if (r == nullptr) 4880 return; 4881 memset(&i, '\0', sizeof(struct ivar64_t)); 4882 if (left < sizeof(struct ivar64_t)) { 4883 memcpy(&i, r, left); 4884 outs() << " (ivar_t entends past the end of the section)\n"; 4885 } else 4886 memcpy(&i, r, sizeof(struct ivar64_t)); 4887 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4888 swapStruct(i); 4889 4890 outs() << "\t\t\t offset "; 4891 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S, 4892 info, n_value, i.offset); 4893 if (n_value != 0) { 4894 if (info->verbose && sym_name != nullptr) 4895 outs() << sym_name; 4896 else 4897 outs() << format("0x%" PRIx64, n_value); 4898 if (i.offset != 0) 4899 outs() << " + " << format("0x%" PRIx64, i.offset); 4900 } else 4901 outs() << format("0x%" PRIx64, i.offset); 4902 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info); 4903 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) { 4904 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset)); 4905 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4906 sys::swapByteOrder(ivar_offset); 4907 outs() << " " << ivar_offset << "\n"; 4908 } else 4909 outs() << "\n"; 4910 4911 outs() << "\t\t\t name "; 4912 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info, 4913 n_value, i.name); 4914 if (n_value != 0) { 4915 if (info->verbose && sym_name != nullptr) 4916 outs() << sym_name; 4917 else 4918 outs() << format("0x%" PRIx64, n_value); 4919 if (i.name != 0) 4920 outs() << " + " << format("0x%" PRIx64, i.name); 4921 } else 4922 outs() << format("0x%" PRIx64, i.name); 4923 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info); 4924 if (name != nullptr) 4925 outs() << format(" %.*s", left, name); 4926 outs() << "\n"; 4927 4928 outs() << "\t\t\t type "; 4929 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info, 4930 n_value, i.name); 4931 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info); 4932 if (n_value != 0) { 4933 if (info->verbose && sym_name != nullptr) 4934 outs() << sym_name; 4935 else 4936 outs() << format("0x%" PRIx64, n_value); 4937 if (i.type != 0) 4938 outs() << " + " << format("0x%" PRIx64, i.type); 4939 } else 4940 outs() << format("0x%" PRIx64, i.type); 4941 if (name != nullptr) 4942 outs() << format(" %.*s", left, name); 4943 outs() << "\n"; 4944 4945 outs() << "\t\t\talignment " << i.alignment << "\n"; 4946 outs() << "\t\t\t size " << i.size << "\n"; 4947 4948 p += sizeof(struct ivar64_t); 4949 offset += sizeof(struct ivar64_t); 4950 } 4951 } 4952 4953 static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) { 4954 struct ivar_list32_t il; 4955 struct ivar32_t i; 4956 const char *r; 4957 uint32_t offset, xoffset, left, j; 4958 SectionRef S, xS; 4959 const char *name, *ivar_offset_p; 4960 uint32_t ivar_offset; 4961 4962 r = get_pointer_32(p, offset, left, S, info); 4963 if (r == nullptr) 4964 return; 4965 memset(&il, '\0', sizeof(struct ivar_list32_t)); 4966 if (left < sizeof(struct ivar_list32_t)) { 4967 memcpy(&il, r, left); 4968 outs() << " (ivar_list_t entends past the end of the section)\n"; 4969 } else 4970 memcpy(&il, r, sizeof(struct ivar_list32_t)); 4971 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4972 swapStruct(il); 4973 outs() << " entsize " << il.entsize << "\n"; 4974 outs() << " count " << il.count << "\n"; 4975 4976 p += sizeof(struct ivar_list32_t); 4977 offset += sizeof(struct ivar_list32_t); 4978 for (j = 0; j < il.count; j++) { 4979 r = get_pointer_32(p, offset, left, S, info); 4980 if (r == nullptr) 4981 return; 4982 memset(&i, '\0', sizeof(struct ivar32_t)); 4983 if (left < sizeof(struct ivar32_t)) { 4984 memcpy(&i, r, left); 4985 outs() << " (ivar_t entends past the end of the section)\n"; 4986 } else 4987 memcpy(&i, r, sizeof(struct ivar32_t)); 4988 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4989 swapStruct(i); 4990 4991 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset); 4992 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info); 4993 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) { 4994 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset)); 4995 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4996 sys::swapByteOrder(ivar_offset); 4997 outs() << " " << ivar_offset << "\n"; 4998 } else 4999 outs() << "\n"; 5000 5001 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name); 5002 name = get_pointer_32(i.name, xoffset, left, xS, info); 5003 if (name != nullptr) 5004 outs() << format(" %.*s", left, name); 5005 outs() << "\n"; 5006 5007 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type); 5008 name = get_pointer_32(i.type, xoffset, left, xS, info); 5009 if (name != nullptr) 5010 outs() << format(" %.*s", left, name); 5011 outs() << "\n"; 5012 5013 outs() << "\t\t\talignment " << i.alignment << "\n"; 5014 outs() << "\t\t\t size " << i.size << "\n"; 5015 5016 p += sizeof(struct ivar32_t); 5017 offset += sizeof(struct ivar32_t); 5018 } 5019 } 5020 5021 static void print_objc_property_list64(uint64_t p, 5022 struct DisassembleInfo *info) { 5023 struct objc_property_list64 opl; 5024 struct objc_property64 op; 5025 const char *r; 5026 uint32_t offset, xoffset, left, j; 5027 SectionRef S, xS; 5028 const char *name, *sym_name; 5029 uint64_t n_value; 5030 5031 r = get_pointer_64(p, offset, left, S, info); 5032 if (r == nullptr) 5033 return; 5034 memset(&opl, '\0', sizeof(struct objc_property_list64)); 5035 if (left < sizeof(struct objc_property_list64)) { 5036 memcpy(&opl, r, left); 5037 outs() << " (objc_property_list entends past the end of the section)\n"; 5038 } else 5039 memcpy(&opl, r, sizeof(struct objc_property_list64)); 5040 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5041 swapStruct(opl); 5042 outs() << " entsize " << opl.entsize << "\n"; 5043 outs() << " count " << opl.count << "\n"; 5044 5045 p += sizeof(struct objc_property_list64); 5046 offset += sizeof(struct objc_property_list64); 5047 for (j = 0; j < opl.count; j++) { 5048 r = get_pointer_64(p, offset, left, S, info); 5049 if (r == nullptr) 5050 return; 5051 memset(&op, '\0', sizeof(struct objc_property64)); 5052 if (left < sizeof(struct objc_property64)) { 5053 memcpy(&op, r, left); 5054 outs() << " (objc_property entends past the end of the section)\n"; 5055 } else 5056 memcpy(&op, r, sizeof(struct objc_property64)); 5057 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5058 swapStruct(op); 5059 5060 outs() << "\t\t\t name "; 5061 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S, 5062 info, n_value, op.name); 5063 if (n_value != 0) { 5064 if (info->verbose && sym_name != nullptr) 5065 outs() << sym_name; 5066 else 5067 outs() << format("0x%" PRIx64, n_value); 5068 if (op.name != 0) 5069 outs() << " + " << format("0x%" PRIx64, op.name); 5070 } else 5071 outs() << format("0x%" PRIx64, op.name); 5072 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info); 5073 if (name != nullptr) 5074 outs() << format(" %.*s", left, name); 5075 outs() << "\n"; 5076 5077 outs() << "\t\t\tattributes "; 5078 sym_name = 5079 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S, 5080 info, n_value, op.attributes); 5081 if (n_value != 0) { 5082 if (info->verbose && sym_name != nullptr) 5083 outs() << sym_name; 5084 else 5085 outs() << format("0x%" PRIx64, n_value); 5086 if (op.attributes != 0) 5087 outs() << " + " << format("0x%" PRIx64, op.attributes); 5088 } else 5089 outs() << format("0x%" PRIx64, op.attributes); 5090 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info); 5091 if (name != nullptr) 5092 outs() << format(" %.*s", left, name); 5093 outs() << "\n"; 5094 5095 p += sizeof(struct objc_property64); 5096 offset += sizeof(struct objc_property64); 5097 } 5098 } 5099 5100 static void print_objc_property_list32(uint32_t p, 5101 struct DisassembleInfo *info) { 5102 struct objc_property_list32 opl; 5103 struct objc_property32 op; 5104 const char *r; 5105 uint32_t offset, xoffset, left, j; 5106 SectionRef S, xS; 5107 const char *name; 5108 5109 r = get_pointer_32(p, offset, left, S, info); 5110 if (r == nullptr) 5111 return; 5112 memset(&opl, '\0', sizeof(struct objc_property_list32)); 5113 if (left < sizeof(struct objc_property_list32)) { 5114 memcpy(&opl, r, left); 5115 outs() << " (objc_property_list entends past the end of the section)\n"; 5116 } else 5117 memcpy(&opl, r, sizeof(struct objc_property_list32)); 5118 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5119 swapStruct(opl); 5120 outs() << " entsize " << opl.entsize << "\n"; 5121 outs() << " count " << opl.count << "\n"; 5122 5123 p += sizeof(struct objc_property_list32); 5124 offset += sizeof(struct objc_property_list32); 5125 for (j = 0; j < opl.count; j++) { 5126 r = get_pointer_32(p, offset, left, S, info); 5127 if (r == nullptr) 5128 return; 5129 memset(&op, '\0', sizeof(struct objc_property32)); 5130 if (left < sizeof(struct objc_property32)) { 5131 memcpy(&op, r, left); 5132 outs() << " (objc_property entends past the end of the section)\n"; 5133 } else 5134 memcpy(&op, r, sizeof(struct objc_property32)); 5135 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5136 swapStruct(op); 5137 5138 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name); 5139 name = get_pointer_32(op.name, xoffset, left, xS, info); 5140 if (name != nullptr) 5141 outs() << format(" %.*s", left, name); 5142 outs() << "\n"; 5143 5144 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes); 5145 name = get_pointer_32(op.attributes, xoffset, left, xS, info); 5146 if (name != nullptr) 5147 outs() << format(" %.*s", left, name); 5148 outs() << "\n"; 5149 5150 p += sizeof(struct objc_property32); 5151 offset += sizeof(struct objc_property32); 5152 } 5153 } 5154 5155 static bool print_class_ro64_t(uint64_t p, struct DisassembleInfo *info, 5156 bool &is_meta_class) { 5157 struct class_ro64_t cro; 5158 const char *r; 5159 uint32_t offset, xoffset, left; 5160 SectionRef S, xS; 5161 const char *name, *sym_name; 5162 uint64_t n_value; 5163 5164 r = get_pointer_64(p, offset, left, S, info); 5165 if (r == nullptr || left < sizeof(struct class_ro64_t)) 5166 return false; 5167 memcpy(&cro, r, sizeof(struct class_ro64_t)); 5168 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5169 swapStruct(cro); 5170 outs() << " flags " << format("0x%" PRIx32, cro.flags); 5171 if (cro.flags & RO_META) 5172 outs() << " RO_META"; 5173 if (cro.flags & RO_ROOT) 5174 outs() << " RO_ROOT"; 5175 if (cro.flags & RO_HAS_CXX_STRUCTORS) 5176 outs() << " RO_HAS_CXX_STRUCTORS"; 5177 outs() << "\n"; 5178 outs() << " instanceStart " << cro.instanceStart << "\n"; 5179 outs() << " instanceSize " << cro.instanceSize << "\n"; 5180 outs() << " reserved " << format("0x%" PRIx32, cro.reserved) 5181 << "\n"; 5182 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout) 5183 << "\n"; 5184 print_layout_map64(cro.ivarLayout, info); 5185 5186 outs() << " name "; 5187 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S, 5188 info, n_value, cro.name); 5189 if (n_value != 0) { 5190 if (info->verbose && sym_name != nullptr) 5191 outs() << sym_name; 5192 else 5193 outs() << format("0x%" PRIx64, n_value); 5194 if (cro.name != 0) 5195 outs() << " + " << format("0x%" PRIx64, cro.name); 5196 } else 5197 outs() << format("0x%" PRIx64, cro.name); 5198 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info); 5199 if (name != nullptr) 5200 outs() << format(" %.*s", left, name); 5201 outs() << "\n"; 5202 5203 outs() << " baseMethods "; 5204 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods), 5205 S, info, n_value, cro.baseMethods); 5206 if (n_value != 0) { 5207 if (info->verbose && sym_name != nullptr) 5208 outs() << sym_name; 5209 else 5210 outs() << format("0x%" PRIx64, n_value); 5211 if (cro.baseMethods != 0) 5212 outs() << " + " << format("0x%" PRIx64, cro.baseMethods); 5213 } else 5214 outs() << format("0x%" PRIx64, cro.baseMethods); 5215 outs() << " (struct method_list_t *)\n"; 5216 if (cro.baseMethods + n_value != 0) 5217 print_method_list64_t(cro.baseMethods + n_value, info, ""); 5218 5219 outs() << " baseProtocols "; 5220 sym_name = 5221 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S, 5222 info, n_value, cro.baseProtocols); 5223 if (n_value != 0) { 5224 if (info->verbose && sym_name != nullptr) 5225 outs() << sym_name; 5226 else 5227 outs() << format("0x%" PRIx64, n_value); 5228 if (cro.baseProtocols != 0) 5229 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols); 5230 } else 5231 outs() << format("0x%" PRIx64, cro.baseProtocols); 5232 outs() << "\n"; 5233 if (cro.baseProtocols + n_value != 0) 5234 print_protocol_list64_t(cro.baseProtocols + n_value, info); 5235 5236 outs() << " ivars "; 5237 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S, 5238 info, n_value, cro.ivars); 5239 if (n_value != 0) { 5240 if (info->verbose && sym_name != nullptr) 5241 outs() << sym_name; 5242 else 5243 outs() << format("0x%" PRIx64, n_value); 5244 if (cro.ivars != 0) 5245 outs() << " + " << format("0x%" PRIx64, cro.ivars); 5246 } else 5247 outs() << format("0x%" PRIx64, cro.ivars); 5248 outs() << "\n"; 5249 if (cro.ivars + n_value != 0) 5250 print_ivar_list64_t(cro.ivars + n_value, info); 5251 5252 outs() << " weakIvarLayout "; 5253 sym_name = 5254 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S, 5255 info, n_value, cro.weakIvarLayout); 5256 if (n_value != 0) { 5257 if (info->verbose && sym_name != nullptr) 5258 outs() << sym_name; 5259 else 5260 outs() << format("0x%" PRIx64, n_value); 5261 if (cro.weakIvarLayout != 0) 5262 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout); 5263 } else 5264 outs() << format("0x%" PRIx64, cro.weakIvarLayout); 5265 outs() << "\n"; 5266 print_layout_map64(cro.weakIvarLayout + n_value, info); 5267 5268 outs() << " baseProperties "; 5269 sym_name = 5270 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S, 5271 info, n_value, cro.baseProperties); 5272 if (n_value != 0) { 5273 if (info->verbose && sym_name != nullptr) 5274 outs() << sym_name; 5275 else 5276 outs() << format("0x%" PRIx64, n_value); 5277 if (cro.baseProperties != 0) 5278 outs() << " + " << format("0x%" PRIx64, cro.baseProperties); 5279 } else 5280 outs() << format("0x%" PRIx64, cro.baseProperties); 5281 outs() << "\n"; 5282 if (cro.baseProperties + n_value != 0) 5283 print_objc_property_list64(cro.baseProperties + n_value, info); 5284 5285 is_meta_class = (cro.flags & RO_META) != 0; 5286 return true; 5287 } 5288 5289 static bool print_class_ro32_t(uint32_t p, struct DisassembleInfo *info, 5290 bool &is_meta_class) { 5291 struct class_ro32_t cro; 5292 const char *r; 5293 uint32_t offset, xoffset, left; 5294 SectionRef S, xS; 5295 const char *name; 5296 5297 r = get_pointer_32(p, offset, left, S, info); 5298 if (r == nullptr) 5299 return false; 5300 memset(&cro, '\0', sizeof(struct class_ro32_t)); 5301 if (left < sizeof(struct class_ro32_t)) { 5302 memcpy(&cro, r, left); 5303 outs() << " (class_ro_t entends past the end of the section)\n"; 5304 } else 5305 memcpy(&cro, r, sizeof(struct class_ro32_t)); 5306 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5307 swapStruct(cro); 5308 outs() << " flags " << format("0x%" PRIx32, cro.flags); 5309 if (cro.flags & RO_META) 5310 outs() << " RO_META"; 5311 if (cro.flags & RO_ROOT) 5312 outs() << " RO_ROOT"; 5313 if (cro.flags & RO_HAS_CXX_STRUCTORS) 5314 outs() << " RO_HAS_CXX_STRUCTORS"; 5315 outs() << "\n"; 5316 outs() << " instanceStart " << cro.instanceStart << "\n"; 5317 outs() << " instanceSize " << cro.instanceSize << "\n"; 5318 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout) 5319 << "\n"; 5320 print_layout_map32(cro.ivarLayout, info); 5321 5322 outs() << " name " << format("0x%" PRIx32, cro.name); 5323 name = get_pointer_32(cro.name, xoffset, left, xS, info); 5324 if (name != nullptr) 5325 outs() << format(" %.*s", left, name); 5326 outs() << "\n"; 5327 5328 outs() << " baseMethods " 5329 << format("0x%" PRIx32, cro.baseMethods) 5330 << " (struct method_list_t *)\n"; 5331 if (cro.baseMethods != 0) 5332 print_method_list32_t(cro.baseMethods, info, ""); 5333 5334 outs() << " baseProtocols " 5335 << format("0x%" PRIx32, cro.baseProtocols) << "\n"; 5336 if (cro.baseProtocols != 0) 5337 print_protocol_list32_t(cro.baseProtocols, info); 5338 outs() << " ivars " << format("0x%" PRIx32, cro.ivars) 5339 << "\n"; 5340 if (cro.ivars != 0) 5341 print_ivar_list32_t(cro.ivars, info); 5342 outs() << " weakIvarLayout " 5343 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n"; 5344 print_layout_map32(cro.weakIvarLayout, info); 5345 outs() << " baseProperties " 5346 << format("0x%" PRIx32, cro.baseProperties) << "\n"; 5347 if (cro.baseProperties != 0) 5348 print_objc_property_list32(cro.baseProperties, info); 5349 is_meta_class = (cro.flags & RO_META) != 0; 5350 return true; 5351 } 5352 5353 static void print_class64_t(uint64_t p, struct DisassembleInfo *info) { 5354 struct class64_t c; 5355 const char *r; 5356 uint32_t offset, left; 5357 SectionRef S; 5358 const char *name; 5359 uint64_t isa_n_value, n_value; 5360 5361 r = get_pointer_64(p, offset, left, S, info); 5362 if (r == nullptr || left < sizeof(struct class64_t)) 5363 return; 5364 memcpy(&c, r, sizeof(struct class64_t)); 5365 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5366 swapStruct(c); 5367 5368 outs() << " isa " << format("0x%" PRIx64, c.isa); 5369 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info, 5370 isa_n_value, c.isa); 5371 if (name != nullptr) 5372 outs() << " " << name; 5373 outs() << "\n"; 5374 5375 outs() << " superclass " << format("0x%" PRIx64, c.superclass); 5376 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info, 5377 n_value, c.superclass); 5378 if (name != nullptr) 5379 outs() << " " << name; 5380 else { 5381 name = get_dyld_bind_info_symbolname(S.getAddress() + 5382 offset + offsetof(struct class64_t, superclass), info); 5383 if (name != nullptr) 5384 outs() << " " << name; 5385 } 5386 outs() << "\n"; 5387 5388 outs() << " cache " << format("0x%" PRIx64, c.cache); 5389 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info, 5390 n_value, c.cache); 5391 if (name != nullptr) 5392 outs() << " " << name; 5393 outs() << "\n"; 5394 5395 outs() << " vtable " << format("0x%" PRIx64, c.vtable); 5396 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info, 5397 n_value, c.vtable); 5398 if (name != nullptr) 5399 outs() << " " << name; 5400 outs() << "\n"; 5401 5402 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info, 5403 n_value, c.data); 5404 outs() << " data "; 5405 if (n_value != 0) { 5406 if (info->verbose && name != nullptr) 5407 outs() << name; 5408 else 5409 outs() << format("0x%" PRIx64, n_value); 5410 if (c.data != 0) 5411 outs() << " + " << format("0x%" PRIx64, c.data); 5412 } else 5413 outs() << format("0x%" PRIx64, c.data); 5414 outs() << " (struct class_ro_t *)"; 5415 5416 // This is a Swift class if some of the low bits of the pointer are set. 5417 if ((c.data + n_value) & 0x7) 5418 outs() << " Swift class"; 5419 outs() << "\n"; 5420 bool is_meta_class; 5421 if (!print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class)) 5422 return; 5423 5424 if (!is_meta_class && 5425 c.isa + isa_n_value != p && 5426 c.isa + isa_n_value != 0 && 5427 info->depth < 100) { 5428 info->depth++; 5429 outs() << "Meta Class\n"; 5430 print_class64_t(c.isa + isa_n_value, info); 5431 } 5432 } 5433 5434 static void print_class32_t(uint32_t p, struct DisassembleInfo *info) { 5435 struct class32_t c; 5436 const char *r; 5437 uint32_t offset, left; 5438 SectionRef S; 5439 const char *name; 5440 5441 r = get_pointer_32(p, offset, left, S, info); 5442 if (r == nullptr) 5443 return; 5444 memset(&c, '\0', sizeof(struct class32_t)); 5445 if (left < sizeof(struct class32_t)) { 5446 memcpy(&c, r, left); 5447 outs() << " (class_t entends past the end of the section)\n"; 5448 } else 5449 memcpy(&c, r, sizeof(struct class32_t)); 5450 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5451 swapStruct(c); 5452 5453 outs() << " isa " << format("0x%" PRIx32, c.isa); 5454 name = 5455 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa); 5456 if (name != nullptr) 5457 outs() << " " << name; 5458 outs() << "\n"; 5459 5460 outs() << " superclass " << format("0x%" PRIx32, c.superclass); 5461 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info, 5462 c.superclass); 5463 if (name != nullptr) 5464 outs() << " " << name; 5465 outs() << "\n"; 5466 5467 outs() << " cache " << format("0x%" PRIx32, c.cache); 5468 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info, 5469 c.cache); 5470 if (name != nullptr) 5471 outs() << " " << name; 5472 outs() << "\n"; 5473 5474 outs() << " vtable " << format("0x%" PRIx32, c.vtable); 5475 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info, 5476 c.vtable); 5477 if (name != nullptr) 5478 outs() << " " << name; 5479 outs() << "\n"; 5480 5481 name = 5482 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data); 5483 outs() << " data " << format("0x%" PRIx32, c.data) 5484 << " (struct class_ro_t *)"; 5485 5486 // This is a Swift class if some of the low bits of the pointer are set. 5487 if (c.data & 0x3) 5488 outs() << " Swift class"; 5489 outs() << "\n"; 5490 bool is_meta_class; 5491 if (!print_class_ro32_t(c.data & ~0x3, info, is_meta_class)) 5492 return; 5493 5494 if (!is_meta_class) { 5495 outs() << "Meta Class\n"; 5496 print_class32_t(c.isa, info); 5497 } 5498 } 5499 5500 static void print_objc_class_t(struct objc_class_t *objc_class, 5501 struct DisassembleInfo *info) { 5502 uint32_t offset, left, xleft; 5503 const char *name, *p, *ivar_list; 5504 SectionRef S; 5505 int32_t i; 5506 struct objc_ivar_list_t objc_ivar_list; 5507 struct objc_ivar_t ivar; 5508 5509 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa); 5510 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) { 5511 name = get_pointer_32(objc_class->isa, offset, left, S, info, true); 5512 if (name != nullptr) 5513 outs() << format(" %.*s", left, name); 5514 else 5515 outs() << " (not in an __OBJC section)"; 5516 } 5517 outs() << "\n"; 5518 5519 outs() << "\t super_class " 5520 << format("0x%08" PRIx32, objc_class->super_class); 5521 if (info->verbose) { 5522 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true); 5523 if (name != nullptr) 5524 outs() << format(" %.*s", left, name); 5525 else 5526 outs() << " (not in an __OBJC section)"; 5527 } 5528 outs() << "\n"; 5529 5530 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name); 5531 if (info->verbose) { 5532 name = get_pointer_32(objc_class->name, offset, left, S, info, true); 5533 if (name != nullptr) 5534 outs() << format(" %.*s", left, name); 5535 else 5536 outs() << " (not in an __OBJC section)"; 5537 } 5538 outs() << "\n"; 5539 5540 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version) 5541 << "\n"; 5542 5543 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info); 5544 if (info->verbose) { 5545 if (CLS_GETINFO(objc_class, CLS_CLASS)) 5546 outs() << " CLS_CLASS"; 5547 else if (CLS_GETINFO(objc_class, CLS_META)) 5548 outs() << " CLS_META"; 5549 } 5550 outs() << "\n"; 5551 5552 outs() << "\t instance_size " 5553 << format("0x%08" PRIx32, objc_class->instance_size) << "\n"; 5554 5555 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true); 5556 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars); 5557 if (p != nullptr) { 5558 if (left > sizeof(struct objc_ivar_list_t)) { 5559 outs() << "\n"; 5560 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t)); 5561 } else { 5562 outs() << " (entends past the end of the section)\n"; 5563 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t)); 5564 memcpy(&objc_ivar_list, p, left); 5565 } 5566 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5567 swapStruct(objc_ivar_list); 5568 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n"; 5569 ivar_list = p + sizeof(struct objc_ivar_list_t); 5570 for (i = 0; i < objc_ivar_list.ivar_count; i++) { 5571 if ((i + 1) * sizeof(struct objc_ivar_t) > left) { 5572 outs() << "\t\t remaining ivar's extend past the of the section\n"; 5573 break; 5574 } 5575 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t), 5576 sizeof(struct objc_ivar_t)); 5577 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5578 swapStruct(ivar); 5579 5580 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name); 5581 if (info->verbose) { 5582 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true); 5583 if (name != nullptr) 5584 outs() << format(" %.*s", xleft, name); 5585 else 5586 outs() << " (not in an __OBJC section)"; 5587 } 5588 outs() << "\n"; 5589 5590 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type); 5591 if (info->verbose) { 5592 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true); 5593 if (name != nullptr) 5594 outs() << format(" %.*s", xleft, name); 5595 else 5596 outs() << " (not in an __OBJC section)"; 5597 } 5598 outs() << "\n"; 5599 5600 outs() << "\t\t ivar_offset " 5601 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n"; 5602 } 5603 } else { 5604 outs() << " (not in an __OBJC section)\n"; 5605 } 5606 5607 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists); 5608 if (print_method_list(objc_class->methodLists, info)) 5609 outs() << " (not in an __OBJC section)\n"; 5610 5611 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache) 5612 << "\n"; 5613 5614 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols); 5615 if (print_protocol_list(objc_class->protocols, 16, info)) 5616 outs() << " (not in an __OBJC section)\n"; 5617 } 5618 5619 static void print_objc_objc_category_t(struct objc_category_t *objc_category, 5620 struct DisassembleInfo *info) { 5621 uint32_t offset, left; 5622 const char *name; 5623 SectionRef S; 5624 5625 outs() << "\t category name " 5626 << format("0x%08" PRIx32, objc_category->category_name); 5627 if (info->verbose) { 5628 name = get_pointer_32(objc_category->category_name, offset, left, S, info, 5629 true); 5630 if (name != nullptr) 5631 outs() << format(" %.*s", left, name); 5632 else 5633 outs() << " (not in an __OBJC section)"; 5634 } 5635 outs() << "\n"; 5636 5637 outs() << "\t\t class name " 5638 << format("0x%08" PRIx32, objc_category->class_name); 5639 if (info->verbose) { 5640 name = 5641 get_pointer_32(objc_category->class_name, offset, left, S, info, true); 5642 if (name != nullptr) 5643 outs() << format(" %.*s", left, name); 5644 else 5645 outs() << " (not in an __OBJC section)"; 5646 } 5647 outs() << "\n"; 5648 5649 outs() << "\t instance methods " 5650 << format("0x%08" PRIx32, objc_category->instance_methods); 5651 if (print_method_list(objc_category->instance_methods, info)) 5652 outs() << " (not in an __OBJC section)\n"; 5653 5654 outs() << "\t class methods " 5655 << format("0x%08" PRIx32, objc_category->class_methods); 5656 if (print_method_list(objc_category->class_methods, info)) 5657 outs() << " (not in an __OBJC section)\n"; 5658 } 5659 5660 static void print_category64_t(uint64_t p, struct DisassembleInfo *info) { 5661 struct category64_t c; 5662 const char *r; 5663 uint32_t offset, xoffset, left; 5664 SectionRef S, xS; 5665 const char *name, *sym_name; 5666 uint64_t n_value; 5667 5668 r = get_pointer_64(p, offset, left, S, info); 5669 if (r == nullptr) 5670 return; 5671 memset(&c, '\0', sizeof(struct category64_t)); 5672 if (left < sizeof(struct category64_t)) { 5673 memcpy(&c, r, left); 5674 outs() << " (category_t entends past the end of the section)\n"; 5675 } else 5676 memcpy(&c, r, sizeof(struct category64_t)); 5677 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5678 swapStruct(c); 5679 5680 outs() << " name "; 5681 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S, 5682 info, n_value, c.name); 5683 if (n_value != 0) { 5684 if (info->verbose && sym_name != nullptr) 5685 outs() << sym_name; 5686 else 5687 outs() << format("0x%" PRIx64, n_value); 5688 if (c.name != 0) 5689 outs() << " + " << format("0x%" PRIx64, c.name); 5690 } else 5691 outs() << format("0x%" PRIx64, c.name); 5692 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info); 5693 if (name != nullptr) 5694 outs() << format(" %.*s", left, name); 5695 outs() << "\n"; 5696 5697 outs() << " cls "; 5698 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info, 5699 n_value, c.cls); 5700 if (n_value != 0) { 5701 if (info->verbose && sym_name != nullptr) 5702 outs() << sym_name; 5703 else 5704 outs() << format("0x%" PRIx64, n_value); 5705 if (c.cls != 0) 5706 outs() << " + " << format("0x%" PRIx64, c.cls); 5707 } else 5708 outs() << format("0x%" PRIx64, c.cls); 5709 outs() << "\n"; 5710 if (c.cls + n_value != 0) 5711 print_class64_t(c.cls + n_value, info); 5712 5713 outs() << " instanceMethods "; 5714 sym_name = 5715 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S, 5716 info, n_value, c.instanceMethods); 5717 if (n_value != 0) { 5718 if (info->verbose && sym_name != nullptr) 5719 outs() << sym_name; 5720 else 5721 outs() << format("0x%" PRIx64, n_value); 5722 if (c.instanceMethods != 0) 5723 outs() << " + " << format("0x%" PRIx64, c.instanceMethods); 5724 } else 5725 outs() << format("0x%" PRIx64, c.instanceMethods); 5726 outs() << "\n"; 5727 if (c.instanceMethods + n_value != 0) 5728 print_method_list64_t(c.instanceMethods + n_value, info, ""); 5729 5730 outs() << " classMethods "; 5731 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods), 5732 S, info, n_value, c.classMethods); 5733 if (n_value != 0) { 5734 if (info->verbose && sym_name != nullptr) 5735 outs() << sym_name; 5736 else 5737 outs() << format("0x%" PRIx64, n_value); 5738 if (c.classMethods != 0) 5739 outs() << " + " << format("0x%" PRIx64, c.classMethods); 5740 } else 5741 outs() << format("0x%" PRIx64, c.classMethods); 5742 outs() << "\n"; 5743 if (c.classMethods + n_value != 0) 5744 print_method_list64_t(c.classMethods + n_value, info, ""); 5745 5746 outs() << " protocols "; 5747 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S, 5748 info, n_value, c.protocols); 5749 if (n_value != 0) { 5750 if (info->verbose && sym_name != nullptr) 5751 outs() << sym_name; 5752 else 5753 outs() << format("0x%" PRIx64, n_value); 5754 if (c.protocols != 0) 5755 outs() << " + " << format("0x%" PRIx64, c.protocols); 5756 } else 5757 outs() << format("0x%" PRIx64, c.protocols); 5758 outs() << "\n"; 5759 if (c.protocols + n_value != 0) 5760 print_protocol_list64_t(c.protocols + n_value, info); 5761 5762 outs() << "instanceProperties "; 5763 sym_name = 5764 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties), 5765 S, info, n_value, c.instanceProperties); 5766 if (n_value != 0) { 5767 if (info->verbose && sym_name != nullptr) 5768 outs() << sym_name; 5769 else 5770 outs() << format("0x%" PRIx64, n_value); 5771 if (c.instanceProperties != 0) 5772 outs() << " + " << format("0x%" PRIx64, c.instanceProperties); 5773 } else 5774 outs() << format("0x%" PRIx64, c.instanceProperties); 5775 outs() << "\n"; 5776 if (c.instanceProperties + n_value != 0) 5777 print_objc_property_list64(c.instanceProperties + n_value, info); 5778 } 5779 5780 static void print_category32_t(uint32_t p, struct DisassembleInfo *info) { 5781 struct category32_t c; 5782 const char *r; 5783 uint32_t offset, left; 5784 SectionRef S, xS; 5785 const char *name; 5786 5787 r = get_pointer_32(p, offset, left, S, info); 5788 if (r == nullptr) 5789 return; 5790 memset(&c, '\0', sizeof(struct category32_t)); 5791 if (left < sizeof(struct category32_t)) { 5792 memcpy(&c, r, left); 5793 outs() << " (category_t entends past the end of the section)\n"; 5794 } else 5795 memcpy(&c, r, sizeof(struct category32_t)); 5796 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5797 swapStruct(c); 5798 5799 outs() << " name " << format("0x%" PRIx32, c.name); 5800 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info, 5801 c.name); 5802 if (name) 5803 outs() << " " << name; 5804 outs() << "\n"; 5805 5806 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n"; 5807 if (c.cls != 0) 5808 print_class32_t(c.cls, info); 5809 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods) 5810 << "\n"; 5811 if (c.instanceMethods != 0) 5812 print_method_list32_t(c.instanceMethods, info, ""); 5813 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods) 5814 << "\n"; 5815 if (c.classMethods != 0) 5816 print_method_list32_t(c.classMethods, info, ""); 5817 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n"; 5818 if (c.protocols != 0) 5819 print_protocol_list32_t(c.protocols, info); 5820 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties) 5821 << "\n"; 5822 if (c.instanceProperties != 0) 5823 print_objc_property_list32(c.instanceProperties, info); 5824 } 5825 5826 static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) { 5827 uint32_t i, left, offset, xoffset; 5828 uint64_t p, n_value; 5829 struct message_ref64 mr; 5830 const char *name, *sym_name; 5831 const char *r; 5832 SectionRef xS; 5833 5834 if (S == SectionRef()) 5835 return; 5836 5837 StringRef SectName; 5838 Expected<StringRef> SecNameOrErr = S.getName(); 5839 if (SecNameOrErr) 5840 SectName = *SecNameOrErr; 5841 else 5842 consumeError(SecNameOrErr.takeError()); 5843 5844 DataRefImpl Ref = S.getRawDataRefImpl(); 5845 StringRef SegName = info->O->getSectionFinalSegmentName(Ref); 5846 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 5847 offset = 0; 5848 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) { 5849 p = S.getAddress() + i; 5850 r = get_pointer_64(p, offset, left, S, info); 5851 if (r == nullptr) 5852 return; 5853 memset(&mr, '\0', sizeof(struct message_ref64)); 5854 if (left < sizeof(struct message_ref64)) { 5855 memcpy(&mr, r, left); 5856 outs() << " (message_ref entends past the end of the section)\n"; 5857 } else 5858 memcpy(&mr, r, sizeof(struct message_ref64)); 5859 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5860 swapStruct(mr); 5861 5862 outs() << " imp "; 5863 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info, 5864 n_value, mr.imp); 5865 if (n_value != 0) { 5866 outs() << format("0x%" PRIx64, n_value) << " "; 5867 if (mr.imp != 0) 5868 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " "; 5869 } else 5870 outs() << format("0x%" PRIx64, mr.imp) << " "; 5871 if (name != nullptr) 5872 outs() << " " << name; 5873 outs() << "\n"; 5874 5875 outs() << " sel "; 5876 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S, 5877 info, n_value, mr.sel); 5878 if (n_value != 0) { 5879 if (info->verbose && sym_name != nullptr) 5880 outs() << sym_name; 5881 else 5882 outs() << format("0x%" PRIx64, n_value); 5883 if (mr.sel != 0) 5884 outs() << " + " << format("0x%" PRIx64, mr.sel); 5885 } else 5886 outs() << format("0x%" PRIx64, mr.sel); 5887 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info); 5888 if (name != nullptr) 5889 outs() << format(" %.*s", left, name); 5890 outs() << "\n"; 5891 5892 offset += sizeof(struct message_ref64); 5893 } 5894 } 5895 5896 static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) { 5897 uint32_t i, left, offset, xoffset, p; 5898 struct message_ref32 mr; 5899 const char *name, *r; 5900 SectionRef xS; 5901 5902 if (S == SectionRef()) 5903 return; 5904 5905 StringRef SectName; 5906 Expected<StringRef> SecNameOrErr = S.getName(); 5907 if (SecNameOrErr) 5908 SectName = *SecNameOrErr; 5909 else 5910 consumeError(SecNameOrErr.takeError()); 5911 5912 DataRefImpl Ref = S.getRawDataRefImpl(); 5913 StringRef SegName = info->O->getSectionFinalSegmentName(Ref); 5914 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 5915 offset = 0; 5916 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) { 5917 p = S.getAddress() + i; 5918 r = get_pointer_32(p, offset, left, S, info); 5919 if (r == nullptr) 5920 return; 5921 memset(&mr, '\0', sizeof(struct message_ref32)); 5922 if (left < sizeof(struct message_ref32)) { 5923 memcpy(&mr, r, left); 5924 outs() << " (message_ref entends past the end of the section)\n"; 5925 } else 5926 memcpy(&mr, r, sizeof(struct message_ref32)); 5927 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5928 swapStruct(mr); 5929 5930 outs() << " imp " << format("0x%" PRIx32, mr.imp); 5931 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info, 5932 mr.imp); 5933 if (name != nullptr) 5934 outs() << " " << name; 5935 outs() << "\n"; 5936 5937 outs() << " sel " << format("0x%" PRIx32, mr.sel); 5938 name = get_pointer_32(mr.sel, xoffset, left, xS, info); 5939 if (name != nullptr) 5940 outs() << " " << name; 5941 outs() << "\n"; 5942 5943 offset += sizeof(struct message_ref32); 5944 } 5945 } 5946 5947 static void print_image_info64(SectionRef S, struct DisassembleInfo *info) { 5948 uint32_t left, offset, swift_version; 5949 uint64_t p; 5950 struct objc_image_info64 o; 5951 const char *r; 5952 5953 if (S == SectionRef()) 5954 return; 5955 5956 StringRef SectName; 5957 Expected<StringRef> SecNameOrErr = S.getName(); 5958 if (SecNameOrErr) 5959 SectName = *SecNameOrErr; 5960 else 5961 consumeError(SecNameOrErr.takeError()); 5962 5963 DataRefImpl Ref = S.getRawDataRefImpl(); 5964 StringRef SegName = info->O->getSectionFinalSegmentName(Ref); 5965 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 5966 p = S.getAddress(); 5967 r = get_pointer_64(p, offset, left, S, info); 5968 if (r == nullptr) 5969 return; 5970 memset(&o, '\0', sizeof(struct objc_image_info64)); 5971 if (left < sizeof(struct objc_image_info64)) { 5972 memcpy(&o, r, left); 5973 outs() << " (objc_image_info entends past the end of the section)\n"; 5974 } else 5975 memcpy(&o, r, sizeof(struct objc_image_info64)); 5976 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5977 swapStruct(o); 5978 outs() << " version " << o.version << "\n"; 5979 outs() << " flags " << format("0x%" PRIx32, o.flags); 5980 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT) 5981 outs() << " OBJC_IMAGE_IS_REPLACEMENT"; 5982 if (o.flags & OBJC_IMAGE_SUPPORTS_GC) 5983 outs() << " OBJC_IMAGE_SUPPORTS_GC"; 5984 if (o.flags & OBJC_IMAGE_IS_SIMULATED) 5985 outs() << " OBJC_IMAGE_IS_SIMULATED"; 5986 if (o.flags & OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES) 5987 outs() << " OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES"; 5988 swift_version = (o.flags >> 8) & 0xff; 5989 if (swift_version != 0) { 5990 if (swift_version == 1) 5991 outs() << " Swift 1.0"; 5992 else if (swift_version == 2) 5993 outs() << " Swift 1.1"; 5994 else if(swift_version == 3) 5995 outs() << " Swift 2.0"; 5996 else if(swift_version == 4) 5997 outs() << " Swift 3.0"; 5998 else if(swift_version == 5) 5999 outs() << " Swift 4.0"; 6000 else if(swift_version == 6) 6001 outs() << " Swift 4.1/Swift 4.2"; 6002 else if(swift_version == 7) 6003 outs() << " Swift 5 or later"; 6004 else 6005 outs() << " unknown future Swift version (" << swift_version << ")"; 6006 } 6007 outs() << "\n"; 6008 } 6009 6010 static void print_image_info32(SectionRef S, struct DisassembleInfo *info) { 6011 uint32_t left, offset, swift_version, p; 6012 struct objc_image_info32 o; 6013 const char *r; 6014 6015 if (S == SectionRef()) 6016 return; 6017 6018 StringRef SectName; 6019 Expected<StringRef> SecNameOrErr = S.getName(); 6020 if (SecNameOrErr) 6021 SectName = *SecNameOrErr; 6022 else 6023 consumeError(SecNameOrErr.takeError()); 6024 6025 DataRefImpl Ref = S.getRawDataRefImpl(); 6026 StringRef SegName = info->O->getSectionFinalSegmentName(Ref); 6027 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 6028 p = S.getAddress(); 6029 r = get_pointer_32(p, offset, left, S, info); 6030 if (r == nullptr) 6031 return; 6032 memset(&o, '\0', sizeof(struct objc_image_info32)); 6033 if (left < sizeof(struct objc_image_info32)) { 6034 memcpy(&o, r, left); 6035 outs() << " (objc_image_info entends past the end of the section)\n"; 6036 } else 6037 memcpy(&o, r, sizeof(struct objc_image_info32)); 6038 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 6039 swapStruct(o); 6040 outs() << " version " << o.version << "\n"; 6041 outs() << " flags " << format("0x%" PRIx32, o.flags); 6042 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT) 6043 outs() << " OBJC_IMAGE_IS_REPLACEMENT"; 6044 if (o.flags & OBJC_IMAGE_SUPPORTS_GC) 6045 outs() << " OBJC_IMAGE_SUPPORTS_GC"; 6046 swift_version = (o.flags >> 8) & 0xff; 6047 if (swift_version != 0) { 6048 if (swift_version == 1) 6049 outs() << " Swift 1.0"; 6050 else if (swift_version == 2) 6051 outs() << " Swift 1.1"; 6052 else if(swift_version == 3) 6053 outs() << " Swift 2.0"; 6054 else if(swift_version == 4) 6055 outs() << " Swift 3.0"; 6056 else if(swift_version == 5) 6057 outs() << " Swift 4.0"; 6058 else if(swift_version == 6) 6059 outs() << " Swift 4.1/Swift 4.2"; 6060 else if(swift_version == 7) 6061 outs() << " Swift 5 or later"; 6062 else 6063 outs() << " unknown future Swift version (" << swift_version << ")"; 6064 } 6065 outs() << "\n"; 6066 } 6067 6068 static void print_image_info(SectionRef S, struct DisassembleInfo *info) { 6069 uint32_t left, offset, p; 6070 struct imageInfo_t o; 6071 const char *r; 6072 6073 StringRef SectName; 6074 Expected<StringRef> SecNameOrErr = S.getName(); 6075 if (SecNameOrErr) 6076 SectName = *SecNameOrErr; 6077 else 6078 consumeError(SecNameOrErr.takeError()); 6079 6080 DataRefImpl Ref = S.getRawDataRefImpl(); 6081 StringRef SegName = info->O->getSectionFinalSegmentName(Ref); 6082 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 6083 p = S.getAddress(); 6084 r = get_pointer_32(p, offset, left, S, info); 6085 if (r == nullptr) 6086 return; 6087 memset(&o, '\0', sizeof(struct imageInfo_t)); 6088 if (left < sizeof(struct imageInfo_t)) { 6089 memcpy(&o, r, left); 6090 outs() << " (imageInfo entends past the end of the section)\n"; 6091 } else 6092 memcpy(&o, r, sizeof(struct imageInfo_t)); 6093 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 6094 swapStruct(o); 6095 outs() << " version " << o.version << "\n"; 6096 outs() << " flags " << format("0x%" PRIx32, o.flags); 6097 if (o.flags & 0x1) 6098 outs() << " F&C"; 6099 if (o.flags & 0x2) 6100 outs() << " GC"; 6101 if (o.flags & 0x4) 6102 outs() << " GC-only"; 6103 else 6104 outs() << " RR"; 6105 outs() << "\n"; 6106 } 6107 6108 static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) { 6109 SymbolAddressMap AddrMap; 6110 if (verbose) 6111 CreateSymbolAddressMap(O, &AddrMap); 6112 6113 std::vector<SectionRef> Sections; 6114 for (const SectionRef &Section : O->sections()) 6115 Sections.push_back(Section); 6116 6117 struct DisassembleInfo info(O, &AddrMap, &Sections, verbose); 6118 6119 SectionRef CL = get_section(O, "__OBJC2", "__class_list"); 6120 if (CL == SectionRef()) 6121 CL = get_section(O, "__DATA", "__objc_classlist"); 6122 if (CL == SectionRef()) 6123 CL = get_section(O, "__DATA_CONST", "__objc_classlist"); 6124 if (CL == SectionRef()) 6125 CL = get_section(O, "__DATA_DIRTY", "__objc_classlist"); 6126 info.S = CL; 6127 walk_pointer_list_64("class", CL, O, &info, print_class64_t); 6128 6129 SectionRef CR = get_section(O, "__OBJC2", "__class_refs"); 6130 if (CR == SectionRef()) 6131 CR = get_section(O, "__DATA", "__objc_classrefs"); 6132 if (CR == SectionRef()) 6133 CR = get_section(O, "__DATA_CONST", "__objc_classrefs"); 6134 if (CR == SectionRef()) 6135 CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs"); 6136 info.S = CR; 6137 walk_pointer_list_64("class refs", CR, O, &info, nullptr); 6138 6139 SectionRef SR = get_section(O, "__OBJC2", "__super_refs"); 6140 if (SR == SectionRef()) 6141 SR = get_section(O, "__DATA", "__objc_superrefs"); 6142 if (SR == SectionRef()) 6143 SR = get_section(O, "__DATA_CONST", "__objc_superrefs"); 6144 if (SR == SectionRef()) 6145 SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs"); 6146 info.S = SR; 6147 walk_pointer_list_64("super refs", SR, O, &info, nullptr); 6148 6149 SectionRef CA = get_section(O, "__OBJC2", "__category_list"); 6150 if (CA == SectionRef()) 6151 CA = get_section(O, "__DATA", "__objc_catlist"); 6152 if (CA == SectionRef()) 6153 CA = get_section(O, "__DATA_CONST", "__objc_catlist"); 6154 if (CA == SectionRef()) 6155 CA = get_section(O, "__DATA_DIRTY", "__objc_catlist"); 6156 info.S = CA; 6157 walk_pointer_list_64("category", CA, O, &info, print_category64_t); 6158 6159 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list"); 6160 if (PL == SectionRef()) 6161 PL = get_section(O, "__DATA", "__objc_protolist"); 6162 if (PL == SectionRef()) 6163 PL = get_section(O, "__DATA_CONST", "__objc_protolist"); 6164 if (PL == SectionRef()) 6165 PL = get_section(O, "__DATA_DIRTY", "__objc_protolist"); 6166 info.S = PL; 6167 walk_pointer_list_64("protocol", PL, O, &info, nullptr); 6168 6169 SectionRef MR = get_section(O, "__OBJC2", "__message_refs"); 6170 if (MR == SectionRef()) 6171 MR = get_section(O, "__DATA", "__objc_msgrefs"); 6172 if (MR == SectionRef()) 6173 MR = get_section(O, "__DATA_CONST", "__objc_msgrefs"); 6174 if (MR == SectionRef()) 6175 MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs"); 6176 info.S = MR; 6177 print_message_refs64(MR, &info); 6178 6179 SectionRef II = get_section(O, "__OBJC2", "__image_info"); 6180 if (II == SectionRef()) 6181 II = get_section(O, "__DATA", "__objc_imageinfo"); 6182 if (II == SectionRef()) 6183 II = get_section(O, "__DATA_CONST", "__objc_imageinfo"); 6184 if (II == SectionRef()) 6185 II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo"); 6186 info.S = II; 6187 print_image_info64(II, &info); 6188 } 6189 6190 static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) { 6191 SymbolAddressMap AddrMap; 6192 if (verbose) 6193 CreateSymbolAddressMap(O, &AddrMap); 6194 6195 std::vector<SectionRef> Sections; 6196 for (const SectionRef &Section : O->sections()) 6197 Sections.push_back(Section); 6198 6199 struct DisassembleInfo info(O, &AddrMap, &Sections, verbose); 6200 6201 SectionRef CL = get_section(O, "__OBJC2", "__class_list"); 6202 if (CL == SectionRef()) 6203 CL = get_section(O, "__DATA", "__objc_classlist"); 6204 if (CL == SectionRef()) 6205 CL = get_section(O, "__DATA_CONST", "__objc_classlist"); 6206 if (CL == SectionRef()) 6207 CL = get_section(O, "__DATA_DIRTY", "__objc_classlist"); 6208 info.S = CL; 6209 walk_pointer_list_32("class", CL, O, &info, print_class32_t); 6210 6211 SectionRef CR = get_section(O, "__OBJC2", "__class_refs"); 6212 if (CR == SectionRef()) 6213 CR = get_section(O, "__DATA", "__objc_classrefs"); 6214 if (CR == SectionRef()) 6215 CR = get_section(O, "__DATA_CONST", "__objc_classrefs"); 6216 if (CR == SectionRef()) 6217 CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs"); 6218 info.S = CR; 6219 walk_pointer_list_32("class refs", CR, O, &info, nullptr); 6220 6221 SectionRef SR = get_section(O, "__OBJC2", "__super_refs"); 6222 if (SR == SectionRef()) 6223 SR = get_section(O, "__DATA", "__objc_superrefs"); 6224 if (SR == SectionRef()) 6225 SR = get_section(O, "__DATA_CONST", "__objc_superrefs"); 6226 if (SR == SectionRef()) 6227 SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs"); 6228 info.S = SR; 6229 walk_pointer_list_32("super refs", SR, O, &info, nullptr); 6230 6231 SectionRef CA = get_section(O, "__OBJC2", "__category_list"); 6232 if (CA == SectionRef()) 6233 CA = get_section(O, "__DATA", "__objc_catlist"); 6234 if (CA == SectionRef()) 6235 CA = get_section(O, "__DATA_CONST", "__objc_catlist"); 6236 if (CA == SectionRef()) 6237 CA = get_section(O, "__DATA_DIRTY", "__objc_catlist"); 6238 info.S = CA; 6239 walk_pointer_list_32("category", CA, O, &info, print_category32_t); 6240 6241 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list"); 6242 if (PL == SectionRef()) 6243 PL = get_section(O, "__DATA", "__objc_protolist"); 6244 if (PL == SectionRef()) 6245 PL = get_section(O, "__DATA_CONST", "__objc_protolist"); 6246 if (PL == SectionRef()) 6247 PL = get_section(O, "__DATA_DIRTY", "__objc_protolist"); 6248 info.S = PL; 6249 walk_pointer_list_32("protocol", PL, O, &info, nullptr); 6250 6251 SectionRef MR = get_section(O, "__OBJC2", "__message_refs"); 6252 if (MR == SectionRef()) 6253 MR = get_section(O, "__DATA", "__objc_msgrefs"); 6254 if (MR == SectionRef()) 6255 MR = get_section(O, "__DATA_CONST", "__objc_msgrefs"); 6256 if (MR == SectionRef()) 6257 MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs"); 6258 info.S = MR; 6259 print_message_refs32(MR, &info); 6260 6261 SectionRef II = get_section(O, "__OBJC2", "__image_info"); 6262 if (II == SectionRef()) 6263 II = get_section(O, "__DATA", "__objc_imageinfo"); 6264 if (II == SectionRef()) 6265 II = get_section(O, "__DATA_CONST", "__objc_imageinfo"); 6266 if (II == SectionRef()) 6267 II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo"); 6268 info.S = II; 6269 print_image_info32(II, &info); 6270 } 6271 6272 static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) { 6273 uint32_t i, j, p, offset, xoffset, left, defs_left, def; 6274 const char *r, *name, *defs; 6275 struct objc_module_t module; 6276 SectionRef S, xS; 6277 struct objc_symtab_t symtab; 6278 struct objc_class_t objc_class; 6279 struct objc_category_t objc_category; 6280 6281 outs() << "Objective-C segment\n"; 6282 S = get_section(O, "__OBJC", "__module_info"); 6283 if (S == SectionRef()) 6284 return false; 6285 6286 SymbolAddressMap AddrMap; 6287 if (verbose) 6288 CreateSymbolAddressMap(O, &AddrMap); 6289 6290 std::vector<SectionRef> Sections; 6291 for (const SectionRef &Section : O->sections()) 6292 Sections.push_back(Section); 6293 6294 struct DisassembleInfo info(O, &AddrMap, &Sections, verbose); 6295 6296 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) { 6297 p = S.getAddress() + i; 6298 r = get_pointer_32(p, offset, left, S, &info, true); 6299 if (r == nullptr) 6300 return true; 6301 memset(&module, '\0', sizeof(struct objc_module_t)); 6302 if (left < sizeof(struct objc_module_t)) { 6303 memcpy(&module, r, left); 6304 outs() << " (module extends past end of __module_info section)\n"; 6305 } else 6306 memcpy(&module, r, sizeof(struct objc_module_t)); 6307 if (O->isLittleEndian() != sys::IsLittleEndianHost) 6308 swapStruct(module); 6309 6310 outs() << "Module " << format("0x%" PRIx32, p) << "\n"; 6311 outs() << " version " << module.version << "\n"; 6312 outs() << " size " << module.size << "\n"; 6313 outs() << " name "; 6314 name = get_pointer_32(module.name, xoffset, left, xS, &info, true); 6315 if (name != nullptr) 6316 outs() << format("%.*s", left, name); 6317 else 6318 outs() << format("0x%08" PRIx32, module.name) 6319 << "(not in an __OBJC section)"; 6320 outs() << "\n"; 6321 6322 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true); 6323 if (module.symtab == 0 || r == nullptr) { 6324 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) 6325 << " (not in an __OBJC section)\n"; 6326 continue; 6327 } 6328 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n"; 6329 memset(&symtab, '\0', sizeof(struct objc_symtab_t)); 6330 defs_left = 0; 6331 defs = nullptr; 6332 if (left < sizeof(struct objc_symtab_t)) { 6333 memcpy(&symtab, r, left); 6334 outs() << "\tsymtab extends past end of an __OBJC section)\n"; 6335 } else { 6336 memcpy(&symtab, r, sizeof(struct objc_symtab_t)); 6337 if (left > sizeof(struct objc_symtab_t)) { 6338 defs_left = left - sizeof(struct objc_symtab_t); 6339 defs = r + sizeof(struct objc_symtab_t); 6340 } 6341 } 6342 if (O->isLittleEndian() != sys::IsLittleEndianHost) 6343 swapStruct(symtab); 6344 6345 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n"; 6346 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true); 6347 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs); 6348 if (r == nullptr) 6349 outs() << " (not in an __OBJC section)"; 6350 outs() << "\n"; 6351 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n"; 6352 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n"; 6353 if (symtab.cls_def_cnt > 0) 6354 outs() << "\tClass Definitions\n"; 6355 for (j = 0; j < symtab.cls_def_cnt; j++) { 6356 if ((j + 1) * sizeof(uint32_t) > defs_left) { 6357 outs() << "\t(remaining class defs entries entends past the end of the " 6358 << "section)\n"; 6359 break; 6360 } 6361 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t)); 6362 if (O->isLittleEndian() != sys::IsLittleEndianHost) 6363 sys::swapByteOrder(def); 6364 6365 r = get_pointer_32(def, xoffset, left, xS, &info, true); 6366 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def); 6367 if (r != nullptr) { 6368 if (left > sizeof(struct objc_class_t)) { 6369 outs() << "\n"; 6370 memcpy(&objc_class, r, sizeof(struct objc_class_t)); 6371 } else { 6372 outs() << " (entends past the end of the section)\n"; 6373 memset(&objc_class, '\0', sizeof(struct objc_class_t)); 6374 memcpy(&objc_class, r, left); 6375 } 6376 if (O->isLittleEndian() != sys::IsLittleEndianHost) 6377 swapStruct(objc_class); 6378 print_objc_class_t(&objc_class, &info); 6379 } else { 6380 outs() << "(not in an __OBJC section)\n"; 6381 } 6382 6383 if (CLS_GETINFO(&objc_class, CLS_CLASS)) { 6384 outs() << "\tMeta Class"; 6385 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true); 6386 if (r != nullptr) { 6387 if (left > sizeof(struct objc_class_t)) { 6388 outs() << "\n"; 6389 memcpy(&objc_class, r, sizeof(struct objc_class_t)); 6390 } else { 6391 outs() << " (entends past the end of the section)\n"; 6392 memset(&objc_class, '\0', sizeof(struct objc_class_t)); 6393 memcpy(&objc_class, r, left); 6394 } 6395 if (O->isLittleEndian() != sys::IsLittleEndianHost) 6396 swapStruct(objc_class); 6397 print_objc_class_t(&objc_class, &info); 6398 } else { 6399 outs() << "(not in an __OBJC section)\n"; 6400 } 6401 } 6402 } 6403 if (symtab.cat_def_cnt > 0) 6404 outs() << "\tCategory Definitions\n"; 6405 for (j = 0; j < symtab.cat_def_cnt; j++) { 6406 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) { 6407 outs() << "\t(remaining category defs entries entends past the end of " 6408 << "the section)\n"; 6409 break; 6410 } 6411 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t), 6412 sizeof(uint32_t)); 6413 if (O->isLittleEndian() != sys::IsLittleEndianHost) 6414 sys::swapByteOrder(def); 6415 6416 r = get_pointer_32(def, xoffset, left, xS, &info, true); 6417 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] " 6418 << format("0x%08" PRIx32, def); 6419 if (r != nullptr) { 6420 if (left > sizeof(struct objc_category_t)) { 6421 outs() << "\n"; 6422 memcpy(&objc_category, r, sizeof(struct objc_category_t)); 6423 } else { 6424 outs() << " (entends past the end of the section)\n"; 6425 memset(&objc_category, '\0', sizeof(struct objc_category_t)); 6426 memcpy(&objc_category, r, left); 6427 } 6428 if (O->isLittleEndian() != sys::IsLittleEndianHost) 6429 swapStruct(objc_category); 6430 print_objc_objc_category_t(&objc_category, &info); 6431 } else { 6432 outs() << "(not in an __OBJC section)\n"; 6433 } 6434 } 6435 } 6436 const SectionRef II = get_section(O, "__OBJC", "__image_info"); 6437 if (II != SectionRef()) 6438 print_image_info(II, &info); 6439 6440 return true; 6441 } 6442 6443 static void DumpProtocolSection(MachOObjectFile *O, const char *sect, 6444 uint32_t size, uint32_t addr) { 6445 SymbolAddressMap AddrMap; 6446 CreateSymbolAddressMap(O, &AddrMap); 6447 6448 std::vector<SectionRef> Sections; 6449 for (const SectionRef &Section : O->sections()) 6450 Sections.push_back(Section); 6451 6452 struct DisassembleInfo info(O, &AddrMap, &Sections, true); 6453 6454 const char *p; 6455 struct objc_protocol_t protocol; 6456 uint32_t left, paddr; 6457 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) { 6458 memset(&protocol, '\0', sizeof(struct objc_protocol_t)); 6459 left = size - (p - sect); 6460 if (left < sizeof(struct objc_protocol_t)) { 6461 outs() << "Protocol extends past end of __protocol section\n"; 6462 memcpy(&protocol, p, left); 6463 } else 6464 memcpy(&protocol, p, sizeof(struct objc_protocol_t)); 6465 if (O->isLittleEndian() != sys::IsLittleEndianHost) 6466 swapStruct(protocol); 6467 paddr = addr + (p - sect); 6468 outs() << "Protocol " << format("0x%" PRIx32, paddr); 6469 if (print_protocol(paddr, 0, &info)) 6470 outs() << "(not in an __OBJC section)\n"; 6471 } 6472 } 6473 6474 #ifdef HAVE_LIBXAR 6475 inline void swapStruct(struct xar_header &xar) { 6476 sys::swapByteOrder(xar.magic); 6477 sys::swapByteOrder(xar.size); 6478 sys::swapByteOrder(xar.version); 6479 sys::swapByteOrder(xar.toc_length_compressed); 6480 sys::swapByteOrder(xar.toc_length_uncompressed); 6481 sys::swapByteOrder(xar.cksum_alg); 6482 } 6483 6484 static void PrintModeVerbose(uint32_t mode) { 6485 switch(mode & S_IFMT){ 6486 case S_IFDIR: 6487 outs() << "d"; 6488 break; 6489 case S_IFCHR: 6490 outs() << "c"; 6491 break; 6492 case S_IFBLK: 6493 outs() << "b"; 6494 break; 6495 case S_IFREG: 6496 outs() << "-"; 6497 break; 6498 case S_IFLNK: 6499 outs() << "l"; 6500 break; 6501 case S_IFSOCK: 6502 outs() << "s"; 6503 break; 6504 default: 6505 outs() << "?"; 6506 break; 6507 } 6508 6509 /* owner permissions */ 6510 if(mode & S_IREAD) 6511 outs() << "r"; 6512 else 6513 outs() << "-"; 6514 if(mode & S_IWRITE) 6515 outs() << "w"; 6516 else 6517 outs() << "-"; 6518 if(mode & S_ISUID) 6519 outs() << "s"; 6520 else if(mode & S_IEXEC) 6521 outs() << "x"; 6522 else 6523 outs() << "-"; 6524 6525 /* group permissions */ 6526 if(mode & (S_IREAD >> 3)) 6527 outs() << "r"; 6528 else 6529 outs() << "-"; 6530 if(mode & (S_IWRITE >> 3)) 6531 outs() << "w"; 6532 else 6533 outs() << "-"; 6534 if(mode & S_ISGID) 6535 outs() << "s"; 6536 else if(mode & (S_IEXEC >> 3)) 6537 outs() << "x"; 6538 else 6539 outs() << "-"; 6540 6541 /* other permissions */ 6542 if(mode & (S_IREAD >> 6)) 6543 outs() << "r"; 6544 else 6545 outs() << "-"; 6546 if(mode & (S_IWRITE >> 6)) 6547 outs() << "w"; 6548 else 6549 outs() << "-"; 6550 if(mode & S_ISVTX) 6551 outs() << "t"; 6552 else if(mode & (S_IEXEC >> 6)) 6553 outs() << "x"; 6554 else 6555 outs() << "-"; 6556 } 6557 6558 static void PrintXarFilesSummary(const char *XarFilename, xar_t xar) { 6559 xar_file_t xf; 6560 const char *key, *type, *mode, *user, *group, *size, *mtime, *name, *m; 6561 char *endp; 6562 uint32_t mode_value; 6563 6564 ScopedXarIter xi; 6565 if (!xi) { 6566 WithColor::error(errs(), "llvm-objdump") 6567 << "can't obtain an xar iterator for xar archive " << XarFilename 6568 << "\n"; 6569 return; 6570 } 6571 6572 // Go through the xar's files. 6573 for (xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)) { 6574 ScopedXarIter xp; 6575 if(!xp){ 6576 WithColor::error(errs(), "llvm-objdump") 6577 << "can't obtain an xar iterator for xar archive " << XarFilename 6578 << "\n"; 6579 return; 6580 } 6581 type = nullptr; 6582 mode = nullptr; 6583 user = nullptr; 6584 group = nullptr; 6585 size = nullptr; 6586 mtime = nullptr; 6587 name = nullptr; 6588 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){ 6589 const char *val = nullptr; 6590 xar_prop_get(xf, key, &val); 6591 #if 0 // Useful for debugging. 6592 outs() << "key: " << key << " value: " << val << "\n"; 6593 #endif 6594 if(strcmp(key, "type") == 0) 6595 type = val; 6596 if(strcmp(key, "mode") == 0) 6597 mode = val; 6598 if(strcmp(key, "user") == 0) 6599 user = val; 6600 if(strcmp(key, "group") == 0) 6601 group = val; 6602 if(strcmp(key, "data/size") == 0) 6603 size = val; 6604 if(strcmp(key, "mtime") == 0) 6605 mtime = val; 6606 if(strcmp(key, "name") == 0) 6607 name = val; 6608 } 6609 if(mode != nullptr){ 6610 mode_value = strtoul(mode, &endp, 8); 6611 if(*endp != '\0') 6612 outs() << "(mode: \"" << mode << "\" contains non-octal chars) "; 6613 if(strcmp(type, "file") == 0) 6614 mode_value |= S_IFREG; 6615 PrintModeVerbose(mode_value); 6616 outs() << " "; 6617 } 6618 if(user != nullptr) 6619 outs() << format("%10s/", user); 6620 if(group != nullptr) 6621 outs() << format("%-10s ", group); 6622 if(size != nullptr) 6623 outs() << format("%7s ", size); 6624 if(mtime != nullptr){ 6625 for(m = mtime; *m != 'T' && *m != '\0'; m++) 6626 outs() << *m; 6627 if(*m == 'T') 6628 m++; 6629 outs() << " "; 6630 for( ; *m != 'Z' && *m != '\0'; m++) 6631 outs() << *m; 6632 outs() << " "; 6633 } 6634 if(name != nullptr) 6635 outs() << name; 6636 outs() << "\n"; 6637 } 6638 } 6639 6640 static void DumpBitcodeSection(MachOObjectFile *O, const char *sect, 6641 uint32_t size, bool verbose, 6642 bool PrintXarHeader, bool PrintXarFileHeaders, 6643 std::string XarMemberName) { 6644 if(size < sizeof(struct xar_header)) { 6645 outs() << "size of (__LLVM,__bundle) section too small (smaller than size " 6646 "of struct xar_header)\n"; 6647 return; 6648 } 6649 struct xar_header XarHeader; 6650 memcpy(&XarHeader, sect, sizeof(struct xar_header)); 6651 if (sys::IsLittleEndianHost) 6652 swapStruct(XarHeader); 6653 if (PrintXarHeader) { 6654 if (!XarMemberName.empty()) 6655 outs() << "In xar member " << XarMemberName << ": "; 6656 else 6657 outs() << "For (__LLVM,__bundle) section: "; 6658 outs() << "xar header\n"; 6659 if (XarHeader.magic == XAR_HEADER_MAGIC) 6660 outs() << " magic XAR_HEADER_MAGIC\n"; 6661 else 6662 outs() << " magic " 6663 << format_hex(XarHeader.magic, 10, true) 6664 << " (not XAR_HEADER_MAGIC)\n"; 6665 outs() << " size " << XarHeader.size << "\n"; 6666 outs() << " version " << XarHeader.version << "\n"; 6667 outs() << " toc_length_compressed " << XarHeader.toc_length_compressed 6668 << "\n"; 6669 outs() << "toc_length_uncompressed " << XarHeader.toc_length_uncompressed 6670 << "\n"; 6671 outs() << " cksum_alg "; 6672 switch (XarHeader.cksum_alg) { 6673 case XAR_CKSUM_NONE: 6674 outs() << "XAR_CKSUM_NONE\n"; 6675 break; 6676 case XAR_CKSUM_SHA1: 6677 outs() << "XAR_CKSUM_SHA1\n"; 6678 break; 6679 case XAR_CKSUM_MD5: 6680 outs() << "XAR_CKSUM_MD5\n"; 6681 break; 6682 #ifdef XAR_CKSUM_SHA256 6683 case XAR_CKSUM_SHA256: 6684 outs() << "XAR_CKSUM_SHA256\n"; 6685 break; 6686 #endif 6687 #ifdef XAR_CKSUM_SHA512 6688 case XAR_CKSUM_SHA512: 6689 outs() << "XAR_CKSUM_SHA512\n"; 6690 break; 6691 #endif 6692 default: 6693 outs() << XarHeader.cksum_alg << "\n"; 6694 } 6695 } 6696 6697 SmallString<128> XarFilename; 6698 int FD; 6699 std::error_code XarEC = 6700 sys::fs::createTemporaryFile("llvm-objdump", "xar", FD, XarFilename); 6701 if (XarEC) { 6702 WithColor::error(errs(), "llvm-objdump") << XarEC.message() << "\n"; 6703 return; 6704 } 6705 ToolOutputFile XarFile(XarFilename, FD); 6706 raw_fd_ostream &XarOut = XarFile.os(); 6707 StringRef XarContents(sect, size); 6708 XarOut << XarContents; 6709 XarOut.close(); 6710 if (XarOut.has_error()) 6711 return; 6712 6713 ScopedXarFile xar(XarFilename.c_str(), READ); 6714 if (!xar) { 6715 WithColor::error(errs(), "llvm-objdump") 6716 << "can't create temporary xar archive " << XarFilename << "\n"; 6717 return; 6718 } 6719 6720 SmallString<128> TocFilename; 6721 std::error_code TocEC = 6722 sys::fs::createTemporaryFile("llvm-objdump", "toc", TocFilename); 6723 if (TocEC) { 6724 WithColor::error(errs(), "llvm-objdump") << TocEC.message() << "\n"; 6725 return; 6726 } 6727 xar_serialize(xar, TocFilename.c_str()); 6728 6729 if (PrintXarFileHeaders) { 6730 if (!XarMemberName.empty()) 6731 outs() << "In xar member " << XarMemberName << ": "; 6732 else 6733 outs() << "For (__LLVM,__bundle) section: "; 6734 outs() << "xar archive files:\n"; 6735 PrintXarFilesSummary(XarFilename.c_str(), xar); 6736 } 6737 6738 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = 6739 MemoryBuffer::getFileOrSTDIN(TocFilename.c_str()); 6740 if (std::error_code EC = FileOrErr.getError()) { 6741 WithColor::error(errs(), "llvm-objdump") << EC.message() << "\n"; 6742 return; 6743 } 6744 std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get(); 6745 6746 if (!XarMemberName.empty()) 6747 outs() << "In xar member " << XarMemberName << ": "; 6748 else 6749 outs() << "For (__LLVM,__bundle) section: "; 6750 outs() << "xar table of contents:\n"; 6751 outs() << Buffer->getBuffer() << "\n"; 6752 6753 // TODO: Go through the xar's files. 6754 ScopedXarIter xi; 6755 if(!xi){ 6756 WithColor::error(errs(), "llvm-objdump") 6757 << "can't obtain an xar iterator for xar archive " 6758 << XarFilename.c_str() << "\n"; 6759 return; 6760 } 6761 for(xar_file_t xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)){ 6762 const char *key; 6763 const char *member_name, *member_type, *member_size_string; 6764 size_t member_size; 6765 6766 ScopedXarIter xp; 6767 if(!xp){ 6768 WithColor::error(errs(), "llvm-objdump") 6769 << "can't obtain an xar iterator for xar archive " 6770 << XarFilename.c_str() << "\n"; 6771 return; 6772 } 6773 member_name = NULL; 6774 member_type = NULL; 6775 member_size_string = NULL; 6776 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){ 6777 const char *val = nullptr; 6778 xar_prop_get(xf, key, &val); 6779 #if 0 // Useful for debugging. 6780 outs() << "key: " << key << " value: " << val << "\n"; 6781 #endif 6782 if (strcmp(key, "name") == 0) 6783 member_name = val; 6784 if (strcmp(key, "type") == 0) 6785 member_type = val; 6786 if (strcmp(key, "data/size") == 0) 6787 member_size_string = val; 6788 } 6789 /* 6790 * If we find a file with a name, date/size and type properties 6791 * and with the type being "file" see if that is a xar file. 6792 */ 6793 if (member_name != NULL && member_type != NULL && 6794 strcmp(member_type, "file") == 0 && 6795 member_size_string != NULL){ 6796 // Extract the file into a buffer. 6797 char *endptr; 6798 member_size = strtoul(member_size_string, &endptr, 10); 6799 if (*endptr == '\0' && member_size != 0) { 6800 char *buffer; 6801 if (xar_extract_tobuffersz(xar, xf, &buffer, &member_size) == 0) { 6802 #if 0 // Useful for debugging. 6803 outs() << "xar member: " << member_name << " extracted\n"; 6804 #endif 6805 // Set the XarMemberName we want to see printed in the header. 6806 std::string OldXarMemberName; 6807 // If XarMemberName is already set this is nested. So 6808 // save the old name and create the nested name. 6809 if (!XarMemberName.empty()) { 6810 OldXarMemberName = XarMemberName; 6811 XarMemberName = 6812 (Twine("[") + XarMemberName + "]" + member_name).str(); 6813 } else { 6814 OldXarMemberName = ""; 6815 XarMemberName = member_name; 6816 } 6817 // See if this is could be a xar file (nested). 6818 if (member_size >= sizeof(struct xar_header)) { 6819 #if 0 // Useful for debugging. 6820 outs() << "could be a xar file: " << member_name << "\n"; 6821 #endif 6822 memcpy((char *)&XarHeader, buffer, sizeof(struct xar_header)); 6823 if (sys::IsLittleEndianHost) 6824 swapStruct(XarHeader); 6825 if (XarHeader.magic == XAR_HEADER_MAGIC) 6826 DumpBitcodeSection(O, buffer, member_size, verbose, 6827 PrintXarHeader, PrintXarFileHeaders, 6828 XarMemberName); 6829 } 6830 XarMemberName = OldXarMemberName; 6831 delete buffer; 6832 } 6833 } 6834 } 6835 } 6836 } 6837 #endif // defined(HAVE_LIBXAR) 6838 6839 static void printObjcMetaData(MachOObjectFile *O, bool verbose) { 6840 if (O->is64Bit()) 6841 printObjc2_64bit_MetaData(O, verbose); 6842 else { 6843 MachO::mach_header H; 6844 H = O->getHeader(); 6845 if (H.cputype == MachO::CPU_TYPE_ARM) 6846 printObjc2_32bit_MetaData(O, verbose); 6847 else { 6848 // This is the 32-bit non-arm cputype case. Which is normally 6849 // the first Objective-C ABI. But it may be the case of a 6850 // binary for the iOS simulator which is the second Objective-C 6851 // ABI. In that case printObjc1_32bit_MetaData() will determine that 6852 // and return false. 6853 if (!printObjc1_32bit_MetaData(O, verbose)) 6854 printObjc2_32bit_MetaData(O, verbose); 6855 } 6856 } 6857 } 6858 6859 // GuessLiteralPointer returns a string which for the item in the Mach-O file 6860 // for the address passed in as ReferenceValue for printing as a comment with 6861 // the instruction and also returns the corresponding type of that item 6862 // indirectly through ReferenceType. 6863 // 6864 // If ReferenceValue is an address of literal cstring then a pointer to the 6865 // cstring is returned and ReferenceType is set to 6866 // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr . 6867 // 6868 // If ReferenceValue is an address of an Objective-C CFString, Selector ref or 6869 // Class ref that name is returned and the ReferenceType is set accordingly. 6870 // 6871 // Lastly, literals which are Symbol address in a literal pool are looked for 6872 // and if found the symbol name is returned and ReferenceType is set to 6873 // LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr . 6874 // 6875 // If there is no item in the Mach-O file for the address passed in as 6876 // ReferenceValue nullptr is returned and ReferenceType is unchanged. 6877 static const char *GuessLiteralPointer(uint64_t ReferenceValue, 6878 uint64_t ReferencePC, 6879 uint64_t *ReferenceType, 6880 struct DisassembleInfo *info) { 6881 // First see if there is an external relocation entry at the ReferencePC. 6882 if (info->O->getHeader().filetype == MachO::MH_OBJECT) { 6883 uint64_t sect_addr = info->S.getAddress(); 6884 uint64_t sect_offset = ReferencePC - sect_addr; 6885 bool reloc_found = false; 6886 DataRefImpl Rel; 6887 MachO::any_relocation_info RE; 6888 bool isExtern = false; 6889 SymbolRef Symbol; 6890 for (const RelocationRef &Reloc : info->S.relocations()) { 6891 uint64_t RelocOffset = Reloc.getOffset(); 6892 if (RelocOffset == sect_offset) { 6893 Rel = Reloc.getRawDataRefImpl(); 6894 RE = info->O->getRelocation(Rel); 6895 if (info->O->isRelocationScattered(RE)) 6896 continue; 6897 isExtern = info->O->getPlainRelocationExternal(RE); 6898 if (isExtern) { 6899 symbol_iterator RelocSym = Reloc.getSymbol(); 6900 Symbol = *RelocSym; 6901 } 6902 reloc_found = true; 6903 break; 6904 } 6905 } 6906 // If there is an external relocation entry for a symbol in a section 6907 // then used that symbol's value for the value of the reference. 6908 if (reloc_found && isExtern) { 6909 if (info->O->getAnyRelocationPCRel(RE)) { 6910 unsigned Type = info->O->getAnyRelocationType(RE); 6911 if (Type == MachO::X86_64_RELOC_SIGNED) { 6912 ReferenceValue = Symbol.getValue(); 6913 } 6914 } 6915 } 6916 } 6917 6918 // Look for literals such as Objective-C CFStrings refs, Selector refs, 6919 // Message refs and Class refs. 6920 bool classref, selref, msgref, cfstring; 6921 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref, 6922 selref, msgref, cfstring); 6923 if (classref && pointer_value == 0) { 6924 // Note the ReferenceValue is a pointer into the __objc_classrefs section. 6925 // And the pointer_value in that section is typically zero as it will be 6926 // set by dyld as part of the "bind information". 6927 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info); 6928 if (name != nullptr) { 6929 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref; 6930 const char *class_name = strrchr(name, '$'); 6931 if (class_name != nullptr && class_name[1] == '_' && 6932 class_name[2] != '\0') { 6933 info->class_name = class_name + 2; 6934 return name; 6935 } 6936 } 6937 } 6938 6939 if (classref) { 6940 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref; 6941 const char *name = 6942 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info); 6943 if (name != nullptr) 6944 info->class_name = name; 6945 else 6946 name = "bad class ref"; 6947 return name; 6948 } 6949 6950 if (cfstring) { 6951 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref; 6952 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info); 6953 return name; 6954 } 6955 6956 if (selref && pointer_value == 0) 6957 pointer_value = get_objc2_64bit_selref(ReferenceValue, info); 6958 6959 if (pointer_value != 0) 6960 ReferenceValue = pointer_value; 6961 6962 const char *name = GuessCstringPointer(ReferenceValue, info); 6963 if (name) { 6964 if (pointer_value != 0 && selref) { 6965 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref; 6966 info->selector_name = name; 6967 } else if (pointer_value != 0 && msgref) { 6968 info->class_name = nullptr; 6969 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref; 6970 info->selector_name = name; 6971 } else 6972 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr; 6973 return name; 6974 } 6975 6976 // Lastly look for an indirect symbol with this ReferenceValue which is in 6977 // a literal pool. If found return that symbol name. 6978 name = GuessIndirectSymbol(ReferenceValue, info); 6979 if (name) { 6980 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr; 6981 return name; 6982 } 6983 6984 return nullptr; 6985 } 6986 6987 // SymbolizerSymbolLookUp is the symbol lookup function passed when creating 6988 // the Symbolizer. It looks up the ReferenceValue using the info passed via the 6989 // pointer to the struct DisassembleInfo that was passed when MCSymbolizer 6990 // is created and returns the symbol name that matches the ReferenceValue or 6991 // nullptr if none. The ReferenceType is passed in for the IN type of 6992 // reference the instruction is making from the values in defined in the header 6993 // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific 6994 // Out type and the ReferenceName will also be set which is added as a comment 6995 // to the disassembled instruction. 6996 // 6997 // If the symbol name is a C++ mangled name then the demangled name is 6998 // returned through ReferenceName and ReferenceType is set to 6999 // LLVMDisassembler_ReferenceType_DeMangled_Name . 7000 // 7001 // When this is called to get a symbol name for a branch target then the 7002 // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then 7003 // SymbolValue will be looked for in the indirect symbol table to determine if 7004 // it is an address for a symbol stub. If so then the symbol name for that 7005 // stub is returned indirectly through ReferenceName and then ReferenceType is 7006 // set to LLVMDisassembler_ReferenceType_Out_SymbolStub. 7007 // 7008 // When this is called with an value loaded via a PC relative load then 7009 // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the 7010 // SymbolValue is checked to be an address of literal pointer, symbol pointer, 7011 // or an Objective-C meta data reference. If so the output ReferenceType is 7012 // set to correspond to that as well as setting the ReferenceName. 7013 static const char *SymbolizerSymbolLookUp(void *DisInfo, 7014 uint64_t ReferenceValue, 7015 uint64_t *ReferenceType, 7016 uint64_t ReferencePC, 7017 const char **ReferenceName) { 7018 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo; 7019 // If no verbose symbolic information is wanted then just return nullptr. 7020 if (!info->verbose) { 7021 *ReferenceName = nullptr; 7022 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7023 return nullptr; 7024 } 7025 7026 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap); 7027 7028 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) { 7029 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info); 7030 if (*ReferenceName != nullptr) { 7031 method_reference(info, ReferenceType, ReferenceName); 7032 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message) 7033 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub; 7034 } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) { 7035 if (info->demangled_name != nullptr) 7036 free(info->demangled_name); 7037 int status; 7038 info->demangled_name = 7039 itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status); 7040 if (info->demangled_name != nullptr) { 7041 *ReferenceName = info->demangled_name; 7042 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name; 7043 } else 7044 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7045 } else 7046 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7047 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) { 7048 *ReferenceName = 7049 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info); 7050 if (*ReferenceName) 7051 method_reference(info, ReferenceType, ReferenceName); 7052 else 7053 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7054 // If this is arm64 and the reference is an adrp instruction save the 7055 // instruction, passed in ReferenceValue and the address of the instruction 7056 // for use later if we see and add immediate instruction. 7057 } else if (info->O->getArch() == Triple::aarch64 && 7058 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) { 7059 info->adrp_inst = ReferenceValue; 7060 info->adrp_addr = ReferencePC; 7061 SymbolName = nullptr; 7062 *ReferenceName = nullptr; 7063 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7064 // If this is arm64 and reference is an add immediate instruction and we 7065 // have 7066 // seen an adrp instruction just before it and the adrp's Xd register 7067 // matches 7068 // this add's Xn register reconstruct the value being referenced and look to 7069 // see if it is a literal pointer. Note the add immediate instruction is 7070 // passed in ReferenceValue. 7071 } else if (info->O->getArch() == Triple::aarch64 && 7072 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri && 7073 ReferencePC - 4 == info->adrp_addr && 7074 (info->adrp_inst & 0x9f000000) == 0x90000000 && 7075 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) { 7076 uint32_t addxri_inst; 7077 uint64_t adrp_imm, addxri_imm; 7078 7079 adrp_imm = 7080 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3); 7081 if (info->adrp_inst & 0x0200000) 7082 adrp_imm |= 0xfffffffffc000000LL; 7083 7084 addxri_inst = ReferenceValue; 7085 addxri_imm = (addxri_inst >> 10) & 0xfff; 7086 if (((addxri_inst >> 22) & 0x3) == 1) 7087 addxri_imm <<= 12; 7088 7089 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) + 7090 (adrp_imm << 12) + addxri_imm; 7091 7092 *ReferenceName = 7093 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info); 7094 if (*ReferenceName == nullptr) 7095 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7096 // If this is arm64 and the reference is a load register instruction and we 7097 // have seen an adrp instruction just before it and the adrp's Xd register 7098 // matches this add's Xn register reconstruct the value being referenced and 7099 // look to see if it is a literal pointer. Note the load register 7100 // instruction is passed in ReferenceValue. 7101 } else if (info->O->getArch() == Triple::aarch64 && 7102 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui && 7103 ReferencePC - 4 == info->adrp_addr && 7104 (info->adrp_inst & 0x9f000000) == 0x90000000 && 7105 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) { 7106 uint32_t ldrxui_inst; 7107 uint64_t adrp_imm, ldrxui_imm; 7108 7109 adrp_imm = 7110 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3); 7111 if (info->adrp_inst & 0x0200000) 7112 adrp_imm |= 0xfffffffffc000000LL; 7113 7114 ldrxui_inst = ReferenceValue; 7115 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff; 7116 7117 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) + 7118 (adrp_imm << 12) + (ldrxui_imm << 3); 7119 7120 *ReferenceName = 7121 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info); 7122 if (*ReferenceName == nullptr) 7123 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7124 } 7125 // If this arm64 and is an load register (PC-relative) instruction the 7126 // ReferenceValue is the PC plus the immediate value. 7127 else if (info->O->getArch() == Triple::aarch64 && 7128 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl || 7129 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) { 7130 *ReferenceName = 7131 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info); 7132 if (*ReferenceName == nullptr) 7133 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7134 } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) { 7135 if (info->demangled_name != nullptr) 7136 free(info->demangled_name); 7137 int status; 7138 info->demangled_name = 7139 itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status); 7140 if (info->demangled_name != nullptr) { 7141 *ReferenceName = info->demangled_name; 7142 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name; 7143 } 7144 } 7145 else { 7146 *ReferenceName = nullptr; 7147 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7148 } 7149 7150 return SymbolName; 7151 } 7152 7153 /// Emits the comments that are stored in the CommentStream. 7154 /// Each comment in the CommentStream must end with a newline. 7155 static void emitComments(raw_svector_ostream &CommentStream, 7156 SmallString<128> &CommentsToEmit, 7157 formatted_raw_ostream &FormattedOS, 7158 const MCAsmInfo &MAI) { 7159 // Flush the stream before taking its content. 7160 StringRef Comments = CommentsToEmit.str(); 7161 // Get the default information for printing a comment. 7162 StringRef CommentBegin = MAI.getCommentString(); 7163 unsigned CommentColumn = MAI.getCommentColumn(); 7164 bool IsFirst = true; 7165 while (!Comments.empty()) { 7166 if (!IsFirst) 7167 FormattedOS << '\n'; 7168 // Emit a line of comments. 7169 FormattedOS.PadToColumn(CommentColumn); 7170 size_t Position = Comments.find('\n'); 7171 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position); 7172 // Move after the newline character. 7173 Comments = Comments.substr(Position + 1); 7174 IsFirst = false; 7175 } 7176 FormattedOS.flush(); 7177 7178 // Tell the comment stream that the vector changed underneath it. 7179 CommentsToEmit.clear(); 7180 } 7181 7182 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, 7183 StringRef DisSegName, StringRef DisSectName) { 7184 const char *McpuDefault = nullptr; 7185 const Target *ThumbTarget = nullptr; 7186 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget); 7187 if (!TheTarget) { 7188 // GetTarget prints out stuff. 7189 return; 7190 } 7191 std::string MachOMCPU; 7192 if (MCPU.empty() && McpuDefault) 7193 MachOMCPU = McpuDefault; 7194 else 7195 MachOMCPU = MCPU; 7196 7197 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo()); 7198 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo; 7199 if (ThumbTarget) 7200 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo()); 7201 7202 // Package up features to be passed to target/subtarget 7203 std::string FeaturesStr; 7204 if (!MAttrs.empty()) { 7205 SubtargetFeatures Features; 7206 for (unsigned i = 0; i != MAttrs.size(); ++i) 7207 Features.AddFeature(MAttrs[i]); 7208 FeaturesStr = Features.getString(); 7209 } 7210 7211 // Set up disassembler. 7212 std::unique_ptr<const MCRegisterInfo> MRI( 7213 TheTarget->createMCRegInfo(TripleName)); 7214 std::unique_ptr<const MCAsmInfo> AsmInfo( 7215 TheTarget->createMCAsmInfo(*MRI, TripleName)); 7216 std::unique_ptr<const MCSubtargetInfo> STI( 7217 TheTarget->createMCSubtargetInfo(TripleName, MachOMCPU, FeaturesStr)); 7218 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr); 7219 std::unique_ptr<MCDisassembler> DisAsm( 7220 TheTarget->createMCDisassembler(*STI, Ctx)); 7221 std::unique_ptr<MCSymbolizer> Symbolizer; 7222 struct DisassembleInfo SymbolizerInfo(nullptr, nullptr, nullptr, false); 7223 std::unique_ptr<MCRelocationInfo> RelInfo( 7224 TheTarget->createMCRelocationInfo(TripleName, Ctx)); 7225 if (RelInfo) { 7226 Symbolizer.reset(TheTarget->createMCSymbolizer( 7227 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp, 7228 &SymbolizerInfo, &Ctx, std::move(RelInfo))); 7229 DisAsm->setSymbolizer(std::move(Symbolizer)); 7230 } 7231 int AsmPrinterVariant = AsmInfo->getAssemblerDialect(); 7232 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter( 7233 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI)); 7234 // Set the display preference for hex vs. decimal immediates. 7235 IP->setPrintImmHex(PrintImmHex); 7236 // Comment stream and backing vector. 7237 SmallString<128> CommentsToEmit; 7238 raw_svector_ostream CommentStream(CommentsToEmit); 7239 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that 7240 // if it is done then arm64 comments for string literals don't get printed 7241 // and some constant get printed instead and not setting it causes intel 7242 // (32-bit and 64-bit) comments printed with different spacing before the 7243 // comment causing different diffs with the 'C' disassembler library API. 7244 // IP->setCommentStream(CommentStream); 7245 7246 if (!AsmInfo || !STI || !DisAsm || !IP) { 7247 WithColor::error(errs(), "llvm-objdump") 7248 << "couldn't initialize disassembler for target " << TripleName << '\n'; 7249 return; 7250 } 7251 7252 // Set up separate thumb disassembler if needed. 7253 std::unique_ptr<const MCRegisterInfo> ThumbMRI; 7254 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo; 7255 std::unique_ptr<const MCSubtargetInfo> ThumbSTI; 7256 std::unique_ptr<MCDisassembler> ThumbDisAsm; 7257 std::unique_ptr<MCInstPrinter> ThumbIP; 7258 std::unique_ptr<MCContext> ThumbCtx; 7259 std::unique_ptr<MCSymbolizer> ThumbSymbolizer; 7260 struct DisassembleInfo ThumbSymbolizerInfo(nullptr, nullptr, nullptr, false); 7261 std::unique_ptr<MCRelocationInfo> ThumbRelInfo; 7262 if (ThumbTarget) { 7263 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName)); 7264 ThumbAsmInfo.reset( 7265 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName)); 7266 ThumbSTI.reset( 7267 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MachOMCPU, 7268 FeaturesStr)); 7269 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr)); 7270 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx)); 7271 MCContext *PtrThumbCtx = ThumbCtx.get(); 7272 ThumbRelInfo.reset( 7273 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx)); 7274 if (ThumbRelInfo) { 7275 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer( 7276 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp, 7277 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo))); 7278 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer)); 7279 } 7280 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect(); 7281 ThumbIP.reset(ThumbTarget->createMCInstPrinter( 7282 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo, 7283 *ThumbInstrInfo, *ThumbMRI)); 7284 // Set the display preference for hex vs. decimal immediates. 7285 ThumbIP->setPrintImmHex(PrintImmHex); 7286 } 7287 7288 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) { 7289 WithColor::error(errs(), "llvm-objdump") 7290 << "couldn't initialize disassembler for target " << ThumbTripleName 7291 << '\n'; 7292 return; 7293 } 7294 7295 MachO::mach_header Header = MachOOF->getHeader(); 7296 7297 // FIXME: Using the -cfg command line option, this code used to be able to 7298 // annotate relocations with the referenced symbol's name, and if this was 7299 // inside a __[cf]string section, the data it points to. This is now replaced 7300 // by the upcoming MCSymbolizer, which needs the appropriate setup done above. 7301 std::vector<SectionRef> Sections; 7302 std::vector<SymbolRef> Symbols; 7303 SmallVector<uint64_t, 8> FoundFns; 7304 uint64_t BaseSegmentAddress = 0; 7305 7306 getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns, 7307 BaseSegmentAddress); 7308 7309 // Sort the symbols by address, just in case they didn't come in that way. 7310 llvm::sort(Symbols, SymbolSorter()); 7311 7312 // Build a data in code table that is sorted on by the address of each entry. 7313 uint64_t BaseAddress = 0; 7314 if (Header.filetype == MachO::MH_OBJECT) 7315 BaseAddress = Sections[0].getAddress(); 7316 else 7317 BaseAddress = BaseSegmentAddress; 7318 DiceTable Dices; 7319 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices(); 7320 DI != DE; ++DI) { 7321 uint32_t Offset; 7322 DI->getOffset(Offset); 7323 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI)); 7324 } 7325 array_pod_sort(Dices.begin(), Dices.end()); 7326 7327 #ifndef NDEBUG 7328 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls(); 7329 #else 7330 raw_ostream &DebugOut = nulls(); 7331 #endif 7332 7333 // Try to find debug info and set up the DIContext for it. 7334 std::unique_ptr<DIContext> diContext; 7335 std::unique_ptr<Binary> DSYMBinary; 7336 std::unique_ptr<MemoryBuffer> DSYMBuf; 7337 if (UseDbg) { 7338 ObjectFile *DbgObj = MachOOF; 7339 7340 // A separate DSym file path was specified, parse it as a macho file, 7341 // get the sections and supply it to the section name parsing machinery. 7342 if (!DSYMFile.empty()) { 7343 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr = 7344 MemoryBuffer::getFileOrSTDIN(DSYMFile); 7345 if (std::error_code EC = BufOrErr.getError()) { 7346 reportError(errorCodeToError(EC), DSYMFile); 7347 return; 7348 } 7349 7350 // We need to keep the file alive, because we're replacing DbgObj with it. 7351 DSYMBuf = std::move(BufOrErr.get()); 7352 7353 Expected<std::unique_ptr<Binary>> BinaryOrErr = 7354 createBinary(DSYMBuf.get()->getMemBufferRef()); 7355 if (!BinaryOrErr) { 7356 reportError(BinaryOrErr.takeError(), DSYMFile); 7357 return; 7358 } 7359 7360 // We need to keep the Binary elive with the buffer 7361 DSYMBinary = std::move(BinaryOrErr.get()); 7362 7363 if (ObjectFile *O = dyn_cast<ObjectFile>(DSYMBinary.get())) { 7364 // this is a Mach-O object file, use it 7365 if (MachOObjectFile *MachDSYM = dyn_cast<MachOObjectFile>(&*O)) { 7366 DbgObj = MachDSYM; 7367 } 7368 else { 7369 WithColor::error(errs(), "llvm-objdump") 7370 << DSYMFile << " is not a Mach-O file type.\n"; 7371 return; 7372 } 7373 } 7374 else if (auto UB = dyn_cast<MachOUniversalBinary>(DSYMBinary.get())){ 7375 // this is a Universal Binary, find a Mach-O for this architecture 7376 uint32_t CPUType, CPUSubType; 7377 const char *ArchFlag; 7378 if (MachOOF->is64Bit()) { 7379 const MachO::mach_header_64 H_64 = MachOOF->getHeader64(); 7380 CPUType = H_64.cputype; 7381 CPUSubType = H_64.cpusubtype; 7382 } else { 7383 const MachO::mach_header H = MachOOF->getHeader(); 7384 CPUType = H.cputype; 7385 CPUSubType = H.cpusubtype; 7386 } 7387 Triple T = MachOObjectFile::getArchTriple(CPUType, CPUSubType, nullptr, 7388 &ArchFlag); 7389 Expected<std::unique_ptr<MachOObjectFile>> MachDSYM = 7390 UB->getObjectForArch(ArchFlag); 7391 if (!MachDSYM) { 7392 reportError(MachDSYM.takeError(), DSYMFile); 7393 return; 7394 } 7395 7396 // We need to keep the Binary elive with the buffer 7397 DbgObj = &*MachDSYM.get(); 7398 DSYMBinary = std::move(*MachDSYM); 7399 } 7400 else { 7401 WithColor::error(errs(), "llvm-objdump") 7402 << DSYMFile << " is not a Mach-O or Universal file type.\n"; 7403 return; 7404 } 7405 } 7406 7407 // Setup the DIContext 7408 diContext = DWARFContext::create(*DbgObj); 7409 } 7410 7411 if (FilterSections.empty()) 7412 outs() << "(" << DisSegName << "," << DisSectName << ") section\n"; 7413 7414 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) { 7415 Expected<StringRef> SecNameOrErr = Sections[SectIdx].getName(); 7416 if (!SecNameOrErr) { 7417 consumeError(SecNameOrErr.takeError()); 7418 continue; 7419 } 7420 if (*SecNameOrErr != DisSectName) 7421 continue; 7422 7423 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl(); 7424 7425 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR); 7426 if (SegmentName != DisSegName) 7427 continue; 7428 7429 StringRef BytesStr = 7430 unwrapOrError(Sections[SectIdx].getContents(), Filename); 7431 ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(BytesStr); 7432 uint64_t SectAddress = Sections[SectIdx].getAddress(); 7433 7434 bool symbolTableWorked = false; 7435 7436 // Create a map of symbol addresses to symbol names for use by 7437 // the SymbolizerSymbolLookUp() routine. 7438 SymbolAddressMap AddrMap; 7439 bool DisSymNameFound = false; 7440 for (const SymbolRef &Symbol : MachOOF->symbols()) { 7441 SymbolRef::Type ST = 7442 unwrapOrError(Symbol.getType(), MachOOF->getFileName()); 7443 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data || 7444 ST == SymbolRef::ST_Other) { 7445 uint64_t Address = Symbol.getValue(); 7446 StringRef SymName = 7447 unwrapOrError(Symbol.getName(), MachOOF->getFileName()); 7448 AddrMap[Address] = SymName; 7449 if (!DisSymName.empty() && DisSymName == SymName) 7450 DisSymNameFound = true; 7451 } 7452 } 7453 if (!DisSymName.empty() && !DisSymNameFound) { 7454 outs() << "Can't find -dis-symname: " << DisSymName << "\n"; 7455 return; 7456 } 7457 // Set up the block of info used by the Symbolizer call backs. 7458 SymbolizerInfo.verbose = !NoSymbolicOperands; 7459 SymbolizerInfo.O = MachOOF; 7460 SymbolizerInfo.S = Sections[SectIdx]; 7461 SymbolizerInfo.AddrMap = &AddrMap; 7462 SymbolizerInfo.Sections = &Sections; 7463 // Same for the ThumbSymbolizer 7464 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands; 7465 ThumbSymbolizerInfo.O = MachOOF; 7466 ThumbSymbolizerInfo.S = Sections[SectIdx]; 7467 ThumbSymbolizerInfo.AddrMap = &AddrMap; 7468 ThumbSymbolizerInfo.Sections = &Sections; 7469 7470 unsigned int Arch = MachOOF->getArch(); 7471 7472 // Skip all symbols if this is a stubs file. 7473 if (Bytes.empty()) 7474 return; 7475 7476 // If the section has symbols but no symbol at the start of the section 7477 // these are used to make sure the bytes before the first symbol are 7478 // disassembled. 7479 bool FirstSymbol = true; 7480 bool FirstSymbolAtSectionStart = true; 7481 7482 // Disassemble symbol by symbol. 7483 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) { 7484 StringRef SymName = 7485 unwrapOrError(Symbols[SymIdx].getName(), MachOOF->getFileName()); 7486 SymbolRef::Type ST = 7487 unwrapOrError(Symbols[SymIdx].getType(), MachOOF->getFileName()); 7488 if (ST != SymbolRef::ST_Function && ST != SymbolRef::ST_Data) 7489 continue; 7490 7491 // Make sure the symbol is defined in this section. 7492 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]); 7493 if (!containsSym) { 7494 if (!DisSymName.empty() && DisSymName == SymName) { 7495 outs() << "-dis-symname: " << DisSymName << " not in the section\n"; 7496 return; 7497 } 7498 continue; 7499 } 7500 // The __mh_execute_header is special and we need to deal with that fact 7501 // this symbol is before the start of the (__TEXT,__text) section and at the 7502 // address of the start of the __TEXT segment. This is because this symbol 7503 // is an N_SECT symbol in the (__TEXT,__text) but its address is before the 7504 // start of the section in a standard MH_EXECUTE filetype. 7505 if (!DisSymName.empty() && DisSymName == "__mh_execute_header") { 7506 outs() << "-dis-symname: __mh_execute_header not in any section\n"; 7507 return; 7508 } 7509 // When this code is trying to disassemble a symbol at a time and in the 7510 // case there is only the __mh_execute_header symbol left as in a stripped 7511 // executable, we need to deal with this by ignoring this symbol so the 7512 // whole section is disassembled and this symbol is then not displayed. 7513 if (SymName == "__mh_execute_header" || SymName == "__mh_dylib_header" || 7514 SymName == "__mh_bundle_header" || SymName == "__mh_object_header" || 7515 SymName == "__mh_preload_header" || SymName == "__mh_dylinker_header") 7516 continue; 7517 7518 // If we are only disassembling one symbol see if this is that symbol. 7519 if (!DisSymName.empty() && DisSymName != SymName) 7520 continue; 7521 7522 // Start at the address of the symbol relative to the section's address. 7523 uint64_t SectSize = Sections[SectIdx].getSize(); 7524 uint64_t Start = Symbols[SymIdx].getValue(); 7525 uint64_t SectionAddress = Sections[SectIdx].getAddress(); 7526 Start -= SectionAddress; 7527 7528 if (Start > SectSize) { 7529 outs() << "section data ends, " << SymName 7530 << " lies outside valid range\n"; 7531 return; 7532 } 7533 7534 // Stop disassembling either at the beginning of the next symbol or at 7535 // the end of the section. 7536 bool containsNextSym = false; 7537 uint64_t NextSym = 0; 7538 uint64_t NextSymIdx = SymIdx + 1; 7539 while (Symbols.size() > NextSymIdx) { 7540 SymbolRef::Type NextSymType = unwrapOrError( 7541 Symbols[NextSymIdx].getType(), MachOOF->getFileName()); 7542 if (NextSymType == SymbolRef::ST_Function) { 7543 containsNextSym = 7544 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]); 7545 NextSym = Symbols[NextSymIdx].getValue(); 7546 NextSym -= SectionAddress; 7547 break; 7548 } 7549 ++NextSymIdx; 7550 } 7551 7552 uint64_t End = containsNextSym ? std::min(NextSym, SectSize) : SectSize; 7553 uint64_t Size; 7554 7555 symbolTableWorked = true; 7556 7557 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl(); 7558 bool IsThumb = MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb; 7559 7560 // We only need the dedicated Thumb target if there's a real choice 7561 // (i.e. we're not targeting M-class) and the function is Thumb. 7562 bool UseThumbTarget = IsThumb && ThumbTarget; 7563 7564 // If we are not specifying a symbol to start disassembly with and this 7565 // is the first symbol in the section but not at the start of the section 7566 // then move the disassembly index to the start of the section and 7567 // don't print the symbol name just yet. This is so the bytes before the 7568 // first symbol are disassembled. 7569 uint64_t SymbolStart = Start; 7570 if (DisSymName.empty() && FirstSymbol && Start != 0) { 7571 FirstSymbolAtSectionStart = false; 7572 Start = 0; 7573 } 7574 else 7575 outs() << SymName << ":\n"; 7576 7577 DILineInfo lastLine; 7578 for (uint64_t Index = Start; Index < End; Index += Size) { 7579 MCInst Inst; 7580 7581 // If this is the first symbol in the section and it was not at the 7582 // start of the section, see if we are at its Index now and if so print 7583 // the symbol name. 7584 if (FirstSymbol && !FirstSymbolAtSectionStart && Index == SymbolStart) 7585 outs() << SymName << ":\n"; 7586 7587 uint64_t PC = SectAddress + Index; 7588 if (!NoLeadingAddr) { 7589 if (FullLeadingAddr) { 7590 if (MachOOF->is64Bit()) 7591 outs() << format("%016" PRIx64, PC); 7592 else 7593 outs() << format("%08" PRIx64, PC); 7594 } else { 7595 outs() << format("%8" PRIx64 ":", PC); 7596 } 7597 } 7598 if (!NoShowRawInsn || Arch == Triple::arm) 7599 outs() << "\t"; 7600 7601 if (DumpAndSkipDataInCode(PC, Bytes.data() + Index, Dices, Size)) 7602 continue; 7603 7604 SmallVector<char, 64> AnnotationsBytes; 7605 raw_svector_ostream Annotations(AnnotationsBytes); 7606 7607 bool gotInst; 7608 if (UseThumbTarget) 7609 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index), 7610 PC, DebugOut, Annotations); 7611 else 7612 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC, 7613 DebugOut, Annotations); 7614 if (gotInst) { 7615 if (!NoShowRawInsn || Arch == Triple::arm) { 7616 dumpBytes(makeArrayRef(Bytes.data() + Index, Size), outs()); 7617 } 7618 formatted_raw_ostream FormattedOS(outs()); 7619 StringRef AnnotationsStr = Annotations.str(); 7620 if (UseThumbTarget) 7621 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI); 7622 else 7623 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI); 7624 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo); 7625 7626 // Print debug info. 7627 if (diContext) { 7628 DILineInfo dli = diContext->getLineInfoForAddress({PC, SectIdx}); 7629 // Print valid line info if it changed. 7630 if (dli != lastLine && dli.Line != 0) 7631 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':' 7632 << dli.Column; 7633 lastLine = dli; 7634 } 7635 outs() << "\n"; 7636 } else { 7637 unsigned int Arch = MachOOF->getArch(); 7638 if (Arch == Triple::x86_64 || Arch == Triple::x86) { 7639 outs() << format("\t.byte 0x%02x #bad opcode\n", 7640 *(Bytes.data() + Index) & 0xff); 7641 Size = 1; // skip exactly one illegible byte and move on. 7642 } else if (Arch == Triple::aarch64 || 7643 (Arch == Triple::arm && !IsThumb)) { 7644 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) | 7645 (*(Bytes.data() + Index + 1) & 0xff) << 8 | 7646 (*(Bytes.data() + Index + 2) & 0xff) << 16 | 7647 (*(Bytes.data() + Index + 3) & 0xff) << 24; 7648 outs() << format("\t.long\t0x%08x\n", opcode); 7649 Size = 4; 7650 } else if (Arch == Triple::arm) { 7651 assert(IsThumb && "ARM mode should have been dealt with above"); 7652 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) | 7653 (*(Bytes.data() + Index + 1) & 0xff) << 8; 7654 outs() << format("\t.short\t0x%04x\n", opcode); 7655 Size = 2; 7656 } else{ 7657 WithColor::warning(errs(), "llvm-objdump") 7658 << "invalid instruction encoding\n"; 7659 if (Size == 0) 7660 Size = 1; // skip illegible bytes 7661 } 7662 } 7663 } 7664 // Now that we are done disassembled the first symbol set the bool that 7665 // were doing this to false. 7666 FirstSymbol = false; 7667 } 7668 if (!symbolTableWorked) { 7669 // Reading the symbol table didn't work, disassemble the whole section. 7670 uint64_t SectAddress = Sections[SectIdx].getAddress(); 7671 uint64_t SectSize = Sections[SectIdx].getSize(); 7672 uint64_t InstSize; 7673 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) { 7674 MCInst Inst; 7675 7676 uint64_t PC = SectAddress + Index; 7677 7678 if (DumpAndSkipDataInCode(PC, Bytes.data() + Index, Dices, InstSize)) 7679 continue; 7680 7681 SmallVector<char, 64> AnnotationsBytes; 7682 raw_svector_ostream Annotations(AnnotationsBytes); 7683 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC, 7684 DebugOut, Annotations)) { 7685 if (!NoLeadingAddr) { 7686 if (FullLeadingAddr) { 7687 if (MachOOF->is64Bit()) 7688 outs() << format("%016" PRIx64, PC); 7689 else 7690 outs() << format("%08" PRIx64, PC); 7691 } else { 7692 outs() << format("%8" PRIx64 ":", PC); 7693 } 7694 } 7695 if (!NoShowRawInsn || Arch == Triple::arm) { 7696 outs() << "\t"; 7697 dumpBytes(makeArrayRef(Bytes.data() + Index, InstSize), outs()); 7698 } 7699 StringRef AnnotationsStr = Annotations.str(); 7700 IP->printInst(&Inst, outs(), AnnotationsStr, *STI); 7701 outs() << "\n"; 7702 } else { 7703 unsigned int Arch = MachOOF->getArch(); 7704 if (Arch == Triple::x86_64 || Arch == Triple::x86) { 7705 outs() << format("\t.byte 0x%02x #bad opcode\n", 7706 *(Bytes.data() + Index) & 0xff); 7707 InstSize = 1; // skip exactly one illegible byte and move on. 7708 } else { 7709 WithColor::warning(errs(), "llvm-objdump") 7710 << "invalid instruction encoding\n"; 7711 if (InstSize == 0) 7712 InstSize = 1; // skip illegible bytes 7713 } 7714 } 7715 } 7716 } 7717 // The TripleName's need to be reset if we are called again for a different 7718 // archtecture. 7719 TripleName = ""; 7720 ThumbTripleName = ""; 7721 7722 if (SymbolizerInfo.demangled_name != nullptr) 7723 free(SymbolizerInfo.demangled_name); 7724 if (ThumbSymbolizerInfo.demangled_name != nullptr) 7725 free(ThumbSymbolizerInfo.demangled_name); 7726 } 7727 } 7728 7729 //===----------------------------------------------------------------------===// 7730 // __compact_unwind section dumping 7731 //===----------------------------------------------------------------------===// 7732 7733 namespace { 7734 7735 template <typename T> 7736 static uint64_t read(StringRef Contents, ptrdiff_t Offset) { 7737 using llvm::support::little; 7738 using llvm::support::unaligned; 7739 7740 if (Offset + sizeof(T) > Contents.size()) { 7741 outs() << "warning: attempt to read past end of buffer\n"; 7742 return T(); 7743 } 7744 7745 uint64_t Val = 7746 support::endian::read<T, little, unaligned>(Contents.data() + Offset); 7747 return Val; 7748 } 7749 7750 template <typename T> 7751 static uint64_t readNext(StringRef Contents, ptrdiff_t &Offset) { 7752 T Val = read<T>(Contents, Offset); 7753 Offset += sizeof(T); 7754 return Val; 7755 } 7756 7757 struct CompactUnwindEntry { 7758 uint32_t OffsetInSection; 7759 7760 uint64_t FunctionAddr; 7761 uint32_t Length; 7762 uint32_t CompactEncoding; 7763 uint64_t PersonalityAddr; 7764 uint64_t LSDAAddr; 7765 7766 RelocationRef FunctionReloc; 7767 RelocationRef PersonalityReloc; 7768 RelocationRef LSDAReloc; 7769 7770 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64) 7771 : OffsetInSection(Offset) { 7772 if (Is64) 7773 read<uint64_t>(Contents, Offset); 7774 else 7775 read<uint32_t>(Contents, Offset); 7776 } 7777 7778 private: 7779 template <typename UIntPtr> void read(StringRef Contents, ptrdiff_t Offset) { 7780 FunctionAddr = readNext<UIntPtr>(Contents, Offset); 7781 Length = readNext<uint32_t>(Contents, Offset); 7782 CompactEncoding = readNext<uint32_t>(Contents, Offset); 7783 PersonalityAddr = readNext<UIntPtr>(Contents, Offset); 7784 LSDAAddr = readNext<UIntPtr>(Contents, Offset); 7785 } 7786 }; 7787 } 7788 7789 /// Given a relocation from __compact_unwind, consisting of the RelocationRef 7790 /// and data being relocated, determine the best base Name and Addend to use for 7791 /// display purposes. 7792 /// 7793 /// 1. An Extern relocation will directly reference a symbol (and the data is 7794 /// then already an addend), so use that. 7795 /// 2. Otherwise the data is an offset in the object file's layout; try to find 7796 // a symbol before it in the same section, and use the offset from there. 7797 /// 3. Finally, if all that fails, fall back to an offset from the start of the 7798 /// referenced section. 7799 static void findUnwindRelocNameAddend(const MachOObjectFile *Obj, 7800 std::map<uint64_t, SymbolRef> &Symbols, 7801 const RelocationRef &Reloc, uint64_t Addr, 7802 StringRef &Name, uint64_t &Addend) { 7803 if (Reloc.getSymbol() != Obj->symbol_end()) { 7804 Name = unwrapOrError(Reloc.getSymbol()->getName(), Obj->getFileName()); 7805 Addend = Addr; 7806 return; 7807 } 7808 7809 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl()); 7810 SectionRef RelocSection = Obj->getAnyRelocationSection(RE); 7811 7812 uint64_t SectionAddr = RelocSection.getAddress(); 7813 7814 auto Sym = Symbols.upper_bound(Addr); 7815 if (Sym == Symbols.begin()) { 7816 // The first symbol in the object is after this reference, the best we can 7817 // do is section-relative notation. 7818 if (Expected<StringRef> NameOrErr = RelocSection.getName()) 7819 Name = *NameOrErr; 7820 else 7821 consumeError(NameOrErr.takeError()); 7822 7823 Addend = Addr - SectionAddr; 7824 return; 7825 } 7826 7827 // Go back one so that SymbolAddress <= Addr. 7828 --Sym; 7829 7830 section_iterator SymSection = 7831 unwrapOrError(Sym->second.getSection(), Obj->getFileName()); 7832 if (RelocSection == *SymSection) { 7833 // There's a valid symbol in the same section before this reference. 7834 Name = unwrapOrError(Sym->second.getName(), Obj->getFileName()); 7835 Addend = Addr - Sym->first; 7836 return; 7837 } 7838 7839 // There is a symbol before this reference, but it's in a different 7840 // section. Probably not helpful to mention it, so use the section name. 7841 if (Expected<StringRef> NameOrErr = RelocSection.getName()) 7842 Name = *NameOrErr; 7843 else 7844 consumeError(NameOrErr.takeError()); 7845 7846 Addend = Addr - SectionAddr; 7847 } 7848 7849 static void printUnwindRelocDest(const MachOObjectFile *Obj, 7850 std::map<uint64_t, SymbolRef> &Symbols, 7851 const RelocationRef &Reloc, uint64_t Addr) { 7852 StringRef Name; 7853 uint64_t Addend; 7854 7855 if (!Reloc.getObject()) 7856 return; 7857 7858 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend); 7859 7860 outs() << Name; 7861 if (Addend) 7862 outs() << " + " << format("0x%" PRIx64, Addend); 7863 } 7864 7865 static void 7866 printMachOCompactUnwindSection(const MachOObjectFile *Obj, 7867 std::map<uint64_t, SymbolRef> &Symbols, 7868 const SectionRef &CompactUnwind) { 7869 7870 if (!Obj->isLittleEndian()) { 7871 outs() << "Skipping big-endian __compact_unwind section\n"; 7872 return; 7873 } 7874 7875 bool Is64 = Obj->is64Bit(); 7876 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t); 7877 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t); 7878 7879 StringRef Contents = 7880 unwrapOrError(CompactUnwind.getContents(), Obj->getFileName()); 7881 SmallVector<CompactUnwindEntry, 4> CompactUnwinds; 7882 7883 // First populate the initial raw offsets, encodings and so on from the entry. 7884 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) { 7885 CompactUnwindEntry Entry(Contents, Offset, Is64); 7886 CompactUnwinds.push_back(Entry); 7887 } 7888 7889 // Next we need to look at the relocations to find out what objects are 7890 // actually being referred to. 7891 for (const RelocationRef &Reloc : CompactUnwind.relocations()) { 7892 uint64_t RelocAddress = Reloc.getOffset(); 7893 7894 uint32_t EntryIdx = RelocAddress / EntrySize; 7895 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize; 7896 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx]; 7897 7898 if (OffsetInEntry == 0) 7899 Entry.FunctionReloc = Reloc; 7900 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t)) 7901 Entry.PersonalityReloc = Reloc; 7902 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t)) 7903 Entry.LSDAReloc = Reloc; 7904 else { 7905 outs() << "Invalid relocation in __compact_unwind section\n"; 7906 return; 7907 } 7908 } 7909 7910 // Finally, we're ready to print the data we've gathered. 7911 outs() << "Contents of __compact_unwind section:\n"; 7912 for (auto &Entry : CompactUnwinds) { 7913 outs() << " Entry at offset " 7914 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n"; 7915 7916 // 1. Start of the region this entry applies to. 7917 outs() << " start: " << format("0x%" PRIx64, 7918 Entry.FunctionAddr) << ' '; 7919 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr); 7920 outs() << '\n'; 7921 7922 // 2. Length of the region this entry applies to. 7923 outs() << " length: " << format("0x%" PRIx32, Entry.Length) 7924 << '\n'; 7925 // 3. The 32-bit compact encoding. 7926 outs() << " compact encoding: " 7927 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n'; 7928 7929 // 4. The personality function, if present. 7930 if (Entry.PersonalityReloc.getObject()) { 7931 outs() << " personality function: " 7932 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' '; 7933 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc, 7934 Entry.PersonalityAddr); 7935 outs() << '\n'; 7936 } 7937 7938 // 5. This entry's language-specific data area. 7939 if (Entry.LSDAReloc.getObject()) { 7940 outs() << " LSDA: " << format("0x%" PRIx64, 7941 Entry.LSDAAddr) << ' '; 7942 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr); 7943 outs() << '\n'; 7944 } 7945 } 7946 } 7947 7948 //===----------------------------------------------------------------------===// 7949 // __unwind_info section dumping 7950 //===----------------------------------------------------------------------===// 7951 7952 static void printRegularSecondLevelUnwindPage(StringRef PageData) { 7953 ptrdiff_t Pos = 0; 7954 uint32_t Kind = readNext<uint32_t>(PageData, Pos); 7955 (void)Kind; 7956 assert(Kind == 2 && "kind for a regular 2nd level index should be 2"); 7957 7958 uint16_t EntriesStart = readNext<uint16_t>(PageData, Pos); 7959 uint16_t NumEntries = readNext<uint16_t>(PageData, Pos); 7960 7961 Pos = EntriesStart; 7962 for (unsigned i = 0; i < NumEntries; ++i) { 7963 uint32_t FunctionOffset = readNext<uint32_t>(PageData, Pos); 7964 uint32_t Encoding = readNext<uint32_t>(PageData, Pos); 7965 7966 outs() << " [" << i << "]: " 7967 << "function offset=" << format("0x%08" PRIx32, FunctionOffset) 7968 << ", " 7969 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n'; 7970 } 7971 } 7972 7973 static void printCompressedSecondLevelUnwindPage( 7974 StringRef PageData, uint32_t FunctionBase, 7975 const SmallVectorImpl<uint32_t> &CommonEncodings) { 7976 ptrdiff_t Pos = 0; 7977 uint32_t Kind = readNext<uint32_t>(PageData, Pos); 7978 (void)Kind; 7979 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3"); 7980 7981 uint16_t EntriesStart = readNext<uint16_t>(PageData, Pos); 7982 uint16_t NumEntries = readNext<uint16_t>(PageData, Pos); 7983 7984 uint16_t EncodingsStart = readNext<uint16_t>(PageData, Pos); 7985 readNext<uint16_t>(PageData, Pos); 7986 StringRef PageEncodings = PageData.substr(EncodingsStart, StringRef::npos); 7987 7988 Pos = EntriesStart; 7989 for (unsigned i = 0; i < NumEntries; ++i) { 7990 uint32_t Entry = readNext<uint32_t>(PageData, Pos); 7991 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff); 7992 uint32_t EncodingIdx = Entry >> 24; 7993 7994 uint32_t Encoding; 7995 if (EncodingIdx < CommonEncodings.size()) 7996 Encoding = CommonEncodings[EncodingIdx]; 7997 else 7998 Encoding = read<uint32_t>(PageEncodings, 7999 sizeof(uint32_t) * 8000 (EncodingIdx - CommonEncodings.size())); 8001 8002 outs() << " [" << i << "]: " 8003 << "function offset=" << format("0x%08" PRIx32, FunctionOffset) 8004 << ", " 8005 << "encoding[" << EncodingIdx 8006 << "]=" << format("0x%08" PRIx32, Encoding) << '\n'; 8007 } 8008 } 8009 8010 static void printMachOUnwindInfoSection(const MachOObjectFile *Obj, 8011 std::map<uint64_t, SymbolRef> &Symbols, 8012 const SectionRef &UnwindInfo) { 8013 8014 if (!Obj->isLittleEndian()) { 8015 outs() << "Skipping big-endian __unwind_info section\n"; 8016 return; 8017 } 8018 8019 outs() << "Contents of __unwind_info section:\n"; 8020 8021 StringRef Contents = 8022 unwrapOrError(UnwindInfo.getContents(), Obj->getFileName()); 8023 ptrdiff_t Pos = 0; 8024 8025 //===---------------------------------- 8026 // Section header 8027 //===---------------------------------- 8028 8029 uint32_t Version = readNext<uint32_t>(Contents, Pos); 8030 outs() << " Version: " 8031 << format("0x%" PRIx32, Version) << '\n'; 8032 if (Version != 1) { 8033 outs() << " Skipping section with unknown version\n"; 8034 return; 8035 } 8036 8037 uint32_t CommonEncodingsStart = readNext<uint32_t>(Contents, Pos); 8038 outs() << " Common encodings array section offset: " 8039 << format("0x%" PRIx32, CommonEncodingsStart) << '\n'; 8040 uint32_t NumCommonEncodings = readNext<uint32_t>(Contents, Pos); 8041 outs() << " Number of common encodings in array: " 8042 << format("0x%" PRIx32, NumCommonEncodings) << '\n'; 8043 8044 uint32_t PersonalitiesStart = readNext<uint32_t>(Contents, Pos); 8045 outs() << " Personality function array section offset: " 8046 << format("0x%" PRIx32, PersonalitiesStart) << '\n'; 8047 uint32_t NumPersonalities = readNext<uint32_t>(Contents, Pos); 8048 outs() << " Number of personality functions in array: " 8049 << format("0x%" PRIx32, NumPersonalities) << '\n'; 8050 8051 uint32_t IndicesStart = readNext<uint32_t>(Contents, Pos); 8052 outs() << " Index array section offset: " 8053 << format("0x%" PRIx32, IndicesStart) << '\n'; 8054 uint32_t NumIndices = readNext<uint32_t>(Contents, Pos); 8055 outs() << " Number of indices in array: " 8056 << format("0x%" PRIx32, NumIndices) << '\n'; 8057 8058 //===---------------------------------- 8059 // A shared list of common encodings 8060 //===---------------------------------- 8061 8062 // These occupy indices in the range [0, N] whenever an encoding is referenced 8063 // from a compressed 2nd level index table. In practice the linker only 8064 // creates ~128 of these, so that indices are available to embed encodings in 8065 // the 2nd level index. 8066 8067 SmallVector<uint32_t, 64> CommonEncodings; 8068 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n"; 8069 Pos = CommonEncodingsStart; 8070 for (unsigned i = 0; i < NumCommonEncodings; ++i) { 8071 uint32_t Encoding = readNext<uint32_t>(Contents, Pos); 8072 CommonEncodings.push_back(Encoding); 8073 8074 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding) 8075 << '\n'; 8076 } 8077 8078 //===---------------------------------- 8079 // Personality functions used in this executable 8080 //===---------------------------------- 8081 8082 // There should be only a handful of these (one per source language, 8083 // roughly). Particularly since they only get 2 bits in the compact encoding. 8084 8085 outs() << " Personality functions: (count = " << NumPersonalities << ")\n"; 8086 Pos = PersonalitiesStart; 8087 for (unsigned i = 0; i < NumPersonalities; ++i) { 8088 uint32_t PersonalityFn = readNext<uint32_t>(Contents, Pos); 8089 outs() << " personality[" << i + 1 8090 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n'; 8091 } 8092 8093 //===---------------------------------- 8094 // The level 1 index entries 8095 //===---------------------------------- 8096 8097 // These specify an approximate place to start searching for the more detailed 8098 // information, sorted by PC. 8099 8100 struct IndexEntry { 8101 uint32_t FunctionOffset; 8102 uint32_t SecondLevelPageStart; 8103 uint32_t LSDAStart; 8104 }; 8105 8106 SmallVector<IndexEntry, 4> IndexEntries; 8107 8108 outs() << " Top level indices: (count = " << NumIndices << ")\n"; 8109 Pos = IndicesStart; 8110 for (unsigned i = 0; i < NumIndices; ++i) { 8111 IndexEntry Entry; 8112 8113 Entry.FunctionOffset = readNext<uint32_t>(Contents, Pos); 8114 Entry.SecondLevelPageStart = readNext<uint32_t>(Contents, Pos); 8115 Entry.LSDAStart = readNext<uint32_t>(Contents, Pos); 8116 IndexEntries.push_back(Entry); 8117 8118 outs() << " [" << i << "]: " 8119 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset) 8120 << ", " 8121 << "2nd level page offset=" 8122 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", " 8123 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n'; 8124 } 8125 8126 //===---------------------------------- 8127 // Next come the LSDA tables 8128 //===---------------------------------- 8129 8130 // The LSDA layout is rather implicit: it's a contiguous array of entries from 8131 // the first top-level index's LSDAOffset to the last (sentinel). 8132 8133 outs() << " LSDA descriptors:\n"; 8134 Pos = IndexEntries[0].LSDAStart; 8135 const uint32_t LSDASize = 2 * sizeof(uint32_t); 8136 int NumLSDAs = 8137 (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) / LSDASize; 8138 8139 for (int i = 0; i < NumLSDAs; ++i) { 8140 uint32_t FunctionOffset = readNext<uint32_t>(Contents, Pos); 8141 uint32_t LSDAOffset = readNext<uint32_t>(Contents, Pos); 8142 outs() << " [" << i << "]: " 8143 << "function offset=" << format("0x%08" PRIx32, FunctionOffset) 8144 << ", " 8145 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n'; 8146 } 8147 8148 //===---------------------------------- 8149 // Finally, the 2nd level indices 8150 //===---------------------------------- 8151 8152 // Generally these are 4K in size, and have 2 possible forms: 8153 // + Regular stores up to 511 entries with disparate encodings 8154 // + Compressed stores up to 1021 entries if few enough compact encoding 8155 // values are used. 8156 outs() << " Second level indices:\n"; 8157 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) { 8158 // The final sentinel top-level index has no associated 2nd level page 8159 if (IndexEntries[i].SecondLevelPageStart == 0) 8160 break; 8161 8162 outs() << " Second level index[" << i << "]: " 8163 << "offset in section=" 8164 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart) 8165 << ", " 8166 << "base function offset=" 8167 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n'; 8168 8169 Pos = IndexEntries[i].SecondLevelPageStart; 8170 if (Pos + sizeof(uint32_t) > Contents.size()) { 8171 outs() << "warning: invalid offset for second level page: " << Pos << '\n'; 8172 continue; 8173 } 8174 8175 uint32_t Kind = 8176 *reinterpret_cast<const support::ulittle32_t *>(Contents.data() + Pos); 8177 if (Kind == 2) 8178 printRegularSecondLevelUnwindPage(Contents.substr(Pos, 4096)); 8179 else if (Kind == 3) 8180 printCompressedSecondLevelUnwindPage(Contents.substr(Pos, 4096), 8181 IndexEntries[i].FunctionOffset, 8182 CommonEncodings); 8183 else 8184 outs() << " Skipping 2nd level page with unknown kind " << Kind 8185 << '\n'; 8186 } 8187 } 8188 8189 void printMachOUnwindInfo(const MachOObjectFile *Obj) { 8190 std::map<uint64_t, SymbolRef> Symbols; 8191 for (const SymbolRef &SymRef : Obj->symbols()) { 8192 // Discard any undefined or absolute symbols. They're not going to take part 8193 // in the convenience lookup for unwind info and just take up resources. 8194 auto SectOrErr = SymRef.getSection(); 8195 if (!SectOrErr) { 8196 // TODO: Actually report errors helpfully. 8197 consumeError(SectOrErr.takeError()); 8198 continue; 8199 } 8200 section_iterator Section = *SectOrErr; 8201 if (Section == Obj->section_end()) 8202 continue; 8203 8204 uint64_t Addr = SymRef.getValue(); 8205 Symbols.insert(std::make_pair(Addr, SymRef)); 8206 } 8207 8208 for (const SectionRef &Section : Obj->sections()) { 8209 StringRef SectName; 8210 if (Expected<StringRef> NameOrErr = Section.getName()) 8211 SectName = *NameOrErr; 8212 else 8213 consumeError(NameOrErr.takeError()); 8214 8215 if (SectName == "__compact_unwind") 8216 printMachOCompactUnwindSection(Obj, Symbols, Section); 8217 else if (SectName == "__unwind_info") 8218 printMachOUnwindInfoSection(Obj, Symbols, Section); 8219 } 8220 } 8221 8222 static void PrintMachHeader(uint32_t magic, uint32_t cputype, 8223 uint32_t cpusubtype, uint32_t filetype, 8224 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags, 8225 bool verbose) { 8226 outs() << "Mach header\n"; 8227 outs() << " magic cputype cpusubtype caps filetype ncmds " 8228 "sizeofcmds flags\n"; 8229 if (verbose) { 8230 if (magic == MachO::MH_MAGIC) 8231 outs() << " MH_MAGIC"; 8232 else if (magic == MachO::MH_MAGIC_64) 8233 outs() << "MH_MAGIC_64"; 8234 else 8235 outs() << format(" 0x%08" PRIx32, magic); 8236 switch (cputype) { 8237 case MachO::CPU_TYPE_I386: 8238 outs() << " I386"; 8239 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 8240 case MachO::CPU_SUBTYPE_I386_ALL: 8241 outs() << " ALL"; 8242 break; 8243 default: 8244 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8245 break; 8246 } 8247 break; 8248 case MachO::CPU_TYPE_X86_64: 8249 outs() << " X86_64"; 8250 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 8251 case MachO::CPU_SUBTYPE_X86_64_ALL: 8252 outs() << " ALL"; 8253 break; 8254 case MachO::CPU_SUBTYPE_X86_64_H: 8255 outs() << " Haswell"; 8256 break; 8257 default: 8258 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8259 break; 8260 } 8261 break; 8262 case MachO::CPU_TYPE_ARM: 8263 outs() << " ARM"; 8264 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 8265 case MachO::CPU_SUBTYPE_ARM_ALL: 8266 outs() << " ALL"; 8267 break; 8268 case MachO::CPU_SUBTYPE_ARM_V4T: 8269 outs() << " V4T"; 8270 break; 8271 case MachO::CPU_SUBTYPE_ARM_V5TEJ: 8272 outs() << " V5TEJ"; 8273 break; 8274 case MachO::CPU_SUBTYPE_ARM_XSCALE: 8275 outs() << " XSCALE"; 8276 break; 8277 case MachO::CPU_SUBTYPE_ARM_V6: 8278 outs() << " V6"; 8279 break; 8280 case MachO::CPU_SUBTYPE_ARM_V6M: 8281 outs() << " V6M"; 8282 break; 8283 case MachO::CPU_SUBTYPE_ARM_V7: 8284 outs() << " V7"; 8285 break; 8286 case MachO::CPU_SUBTYPE_ARM_V7EM: 8287 outs() << " V7EM"; 8288 break; 8289 case MachO::CPU_SUBTYPE_ARM_V7K: 8290 outs() << " V7K"; 8291 break; 8292 case MachO::CPU_SUBTYPE_ARM_V7M: 8293 outs() << " V7M"; 8294 break; 8295 case MachO::CPU_SUBTYPE_ARM_V7S: 8296 outs() << " V7S"; 8297 break; 8298 default: 8299 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8300 break; 8301 } 8302 break; 8303 case MachO::CPU_TYPE_ARM64: 8304 outs() << " ARM64"; 8305 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 8306 case MachO::CPU_SUBTYPE_ARM64_ALL: 8307 outs() << " ALL"; 8308 break; 8309 case MachO::CPU_SUBTYPE_ARM64E: 8310 outs() << " E"; 8311 break; 8312 default: 8313 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8314 break; 8315 } 8316 break; 8317 case MachO::CPU_TYPE_ARM64_32: 8318 outs() << " ARM64_32"; 8319 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 8320 case MachO::CPU_SUBTYPE_ARM64_32_V8: 8321 outs() << " V8"; 8322 break; 8323 default: 8324 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8325 break; 8326 } 8327 break; 8328 case MachO::CPU_TYPE_POWERPC: 8329 outs() << " PPC"; 8330 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 8331 case MachO::CPU_SUBTYPE_POWERPC_ALL: 8332 outs() << " ALL"; 8333 break; 8334 default: 8335 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8336 break; 8337 } 8338 break; 8339 case MachO::CPU_TYPE_POWERPC64: 8340 outs() << " PPC64"; 8341 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 8342 case MachO::CPU_SUBTYPE_POWERPC_ALL: 8343 outs() << " ALL"; 8344 break; 8345 default: 8346 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8347 break; 8348 } 8349 break; 8350 default: 8351 outs() << format(" %7d", cputype); 8352 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8353 break; 8354 } 8355 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) { 8356 outs() << " LIB64"; 8357 } else { 8358 outs() << format(" 0x%02" PRIx32, 8359 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24); 8360 } 8361 switch (filetype) { 8362 case MachO::MH_OBJECT: 8363 outs() << " OBJECT"; 8364 break; 8365 case MachO::MH_EXECUTE: 8366 outs() << " EXECUTE"; 8367 break; 8368 case MachO::MH_FVMLIB: 8369 outs() << " FVMLIB"; 8370 break; 8371 case MachO::MH_CORE: 8372 outs() << " CORE"; 8373 break; 8374 case MachO::MH_PRELOAD: 8375 outs() << " PRELOAD"; 8376 break; 8377 case MachO::MH_DYLIB: 8378 outs() << " DYLIB"; 8379 break; 8380 case MachO::MH_DYLIB_STUB: 8381 outs() << " DYLIB_STUB"; 8382 break; 8383 case MachO::MH_DYLINKER: 8384 outs() << " DYLINKER"; 8385 break; 8386 case MachO::MH_BUNDLE: 8387 outs() << " BUNDLE"; 8388 break; 8389 case MachO::MH_DSYM: 8390 outs() << " DSYM"; 8391 break; 8392 case MachO::MH_KEXT_BUNDLE: 8393 outs() << " KEXTBUNDLE"; 8394 break; 8395 default: 8396 outs() << format(" %10u", filetype); 8397 break; 8398 } 8399 outs() << format(" %5u", ncmds); 8400 outs() << format(" %10u", sizeofcmds); 8401 uint32_t f = flags; 8402 if (f & MachO::MH_NOUNDEFS) { 8403 outs() << " NOUNDEFS"; 8404 f &= ~MachO::MH_NOUNDEFS; 8405 } 8406 if (f & MachO::MH_INCRLINK) { 8407 outs() << " INCRLINK"; 8408 f &= ~MachO::MH_INCRLINK; 8409 } 8410 if (f & MachO::MH_DYLDLINK) { 8411 outs() << " DYLDLINK"; 8412 f &= ~MachO::MH_DYLDLINK; 8413 } 8414 if (f & MachO::MH_BINDATLOAD) { 8415 outs() << " BINDATLOAD"; 8416 f &= ~MachO::MH_BINDATLOAD; 8417 } 8418 if (f & MachO::MH_PREBOUND) { 8419 outs() << " PREBOUND"; 8420 f &= ~MachO::MH_PREBOUND; 8421 } 8422 if (f & MachO::MH_SPLIT_SEGS) { 8423 outs() << " SPLIT_SEGS"; 8424 f &= ~MachO::MH_SPLIT_SEGS; 8425 } 8426 if (f & MachO::MH_LAZY_INIT) { 8427 outs() << " LAZY_INIT"; 8428 f &= ~MachO::MH_LAZY_INIT; 8429 } 8430 if (f & MachO::MH_TWOLEVEL) { 8431 outs() << " TWOLEVEL"; 8432 f &= ~MachO::MH_TWOLEVEL; 8433 } 8434 if (f & MachO::MH_FORCE_FLAT) { 8435 outs() << " FORCE_FLAT"; 8436 f &= ~MachO::MH_FORCE_FLAT; 8437 } 8438 if (f & MachO::MH_NOMULTIDEFS) { 8439 outs() << " NOMULTIDEFS"; 8440 f &= ~MachO::MH_NOMULTIDEFS; 8441 } 8442 if (f & MachO::MH_NOFIXPREBINDING) { 8443 outs() << " NOFIXPREBINDING"; 8444 f &= ~MachO::MH_NOFIXPREBINDING; 8445 } 8446 if (f & MachO::MH_PREBINDABLE) { 8447 outs() << " PREBINDABLE"; 8448 f &= ~MachO::MH_PREBINDABLE; 8449 } 8450 if (f & MachO::MH_ALLMODSBOUND) { 8451 outs() << " ALLMODSBOUND"; 8452 f &= ~MachO::MH_ALLMODSBOUND; 8453 } 8454 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) { 8455 outs() << " SUBSECTIONS_VIA_SYMBOLS"; 8456 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS; 8457 } 8458 if (f & MachO::MH_CANONICAL) { 8459 outs() << " CANONICAL"; 8460 f &= ~MachO::MH_CANONICAL; 8461 } 8462 if (f & MachO::MH_WEAK_DEFINES) { 8463 outs() << " WEAK_DEFINES"; 8464 f &= ~MachO::MH_WEAK_DEFINES; 8465 } 8466 if (f & MachO::MH_BINDS_TO_WEAK) { 8467 outs() << " BINDS_TO_WEAK"; 8468 f &= ~MachO::MH_BINDS_TO_WEAK; 8469 } 8470 if (f & MachO::MH_ALLOW_STACK_EXECUTION) { 8471 outs() << " ALLOW_STACK_EXECUTION"; 8472 f &= ~MachO::MH_ALLOW_STACK_EXECUTION; 8473 } 8474 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) { 8475 outs() << " DEAD_STRIPPABLE_DYLIB"; 8476 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB; 8477 } 8478 if (f & MachO::MH_PIE) { 8479 outs() << " PIE"; 8480 f &= ~MachO::MH_PIE; 8481 } 8482 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) { 8483 outs() << " NO_REEXPORTED_DYLIBS"; 8484 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS; 8485 } 8486 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) { 8487 outs() << " MH_HAS_TLV_DESCRIPTORS"; 8488 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS; 8489 } 8490 if (f & MachO::MH_NO_HEAP_EXECUTION) { 8491 outs() << " MH_NO_HEAP_EXECUTION"; 8492 f &= ~MachO::MH_NO_HEAP_EXECUTION; 8493 } 8494 if (f & MachO::MH_APP_EXTENSION_SAFE) { 8495 outs() << " APP_EXTENSION_SAFE"; 8496 f &= ~MachO::MH_APP_EXTENSION_SAFE; 8497 } 8498 if (f & MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO) { 8499 outs() << " NLIST_OUTOFSYNC_WITH_DYLDINFO"; 8500 f &= ~MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO; 8501 } 8502 if (f != 0 || flags == 0) 8503 outs() << format(" 0x%08" PRIx32, f); 8504 } else { 8505 outs() << format(" 0x%08" PRIx32, magic); 8506 outs() << format(" %7d", cputype); 8507 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8508 outs() << format(" 0x%02" PRIx32, 8509 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24); 8510 outs() << format(" %10u", filetype); 8511 outs() << format(" %5u", ncmds); 8512 outs() << format(" %10u", sizeofcmds); 8513 outs() << format(" 0x%08" PRIx32, flags); 8514 } 8515 outs() << "\n"; 8516 } 8517 8518 static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize, 8519 StringRef SegName, uint64_t vmaddr, 8520 uint64_t vmsize, uint64_t fileoff, 8521 uint64_t filesize, uint32_t maxprot, 8522 uint32_t initprot, uint32_t nsects, 8523 uint32_t flags, uint32_t object_size, 8524 bool verbose) { 8525 uint64_t expected_cmdsize; 8526 if (cmd == MachO::LC_SEGMENT) { 8527 outs() << " cmd LC_SEGMENT\n"; 8528 expected_cmdsize = nsects; 8529 expected_cmdsize *= sizeof(struct MachO::section); 8530 expected_cmdsize += sizeof(struct MachO::segment_command); 8531 } else { 8532 outs() << " cmd LC_SEGMENT_64\n"; 8533 expected_cmdsize = nsects; 8534 expected_cmdsize *= sizeof(struct MachO::section_64); 8535 expected_cmdsize += sizeof(struct MachO::segment_command_64); 8536 } 8537 outs() << " cmdsize " << cmdsize; 8538 if (cmdsize != expected_cmdsize) 8539 outs() << " Inconsistent size\n"; 8540 else 8541 outs() << "\n"; 8542 outs() << " segname " << SegName << "\n"; 8543 if (cmd == MachO::LC_SEGMENT_64) { 8544 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n"; 8545 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n"; 8546 } else { 8547 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n"; 8548 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n"; 8549 } 8550 outs() << " fileoff " << fileoff; 8551 if (fileoff > object_size) 8552 outs() << " (past end of file)\n"; 8553 else 8554 outs() << "\n"; 8555 outs() << " filesize " << filesize; 8556 if (fileoff + filesize > object_size) 8557 outs() << " (past end of file)\n"; 8558 else 8559 outs() << "\n"; 8560 if (verbose) { 8561 if ((maxprot & 8562 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | 8563 MachO::VM_PROT_EXECUTE)) != 0) 8564 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n"; 8565 else { 8566 outs() << " maxprot "; 8567 outs() << ((maxprot & MachO::VM_PROT_READ) ? "r" : "-"); 8568 outs() << ((maxprot & MachO::VM_PROT_WRITE) ? "w" : "-"); 8569 outs() << ((maxprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n"); 8570 } 8571 if ((initprot & 8572 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | 8573 MachO::VM_PROT_EXECUTE)) != 0) 8574 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n"; 8575 else { 8576 outs() << " initprot "; 8577 outs() << ((initprot & MachO::VM_PROT_READ) ? "r" : "-"); 8578 outs() << ((initprot & MachO::VM_PROT_WRITE) ? "w" : "-"); 8579 outs() << ((initprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n"); 8580 } 8581 } else { 8582 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n"; 8583 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n"; 8584 } 8585 outs() << " nsects " << nsects << "\n"; 8586 if (verbose) { 8587 outs() << " flags"; 8588 if (flags == 0) 8589 outs() << " (none)\n"; 8590 else { 8591 if (flags & MachO::SG_HIGHVM) { 8592 outs() << " HIGHVM"; 8593 flags &= ~MachO::SG_HIGHVM; 8594 } 8595 if (flags & MachO::SG_FVMLIB) { 8596 outs() << " FVMLIB"; 8597 flags &= ~MachO::SG_FVMLIB; 8598 } 8599 if (flags & MachO::SG_NORELOC) { 8600 outs() << " NORELOC"; 8601 flags &= ~MachO::SG_NORELOC; 8602 } 8603 if (flags & MachO::SG_PROTECTED_VERSION_1) { 8604 outs() << " PROTECTED_VERSION_1"; 8605 flags &= ~MachO::SG_PROTECTED_VERSION_1; 8606 } 8607 if (flags) 8608 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n"; 8609 else 8610 outs() << "\n"; 8611 } 8612 } else { 8613 outs() << " flags " << format("0x%" PRIx32, flags) << "\n"; 8614 } 8615 } 8616 8617 static void PrintSection(const char *sectname, const char *segname, 8618 uint64_t addr, uint64_t size, uint32_t offset, 8619 uint32_t align, uint32_t reloff, uint32_t nreloc, 8620 uint32_t flags, uint32_t reserved1, uint32_t reserved2, 8621 uint32_t cmd, const char *sg_segname, 8622 uint32_t filetype, uint32_t object_size, 8623 bool verbose) { 8624 outs() << "Section\n"; 8625 outs() << " sectname " << format("%.16s\n", sectname); 8626 outs() << " segname " << format("%.16s", segname); 8627 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0) 8628 outs() << " (does not match segment)\n"; 8629 else 8630 outs() << "\n"; 8631 if (cmd == MachO::LC_SEGMENT_64) { 8632 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n"; 8633 outs() << " size " << format("0x%016" PRIx64, size); 8634 } else { 8635 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n"; 8636 outs() << " size " << format("0x%08" PRIx64, size); 8637 } 8638 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size) 8639 outs() << " (past end of file)\n"; 8640 else 8641 outs() << "\n"; 8642 outs() << " offset " << offset; 8643 if (offset > object_size) 8644 outs() << " (past end of file)\n"; 8645 else 8646 outs() << "\n"; 8647 uint32_t align_shifted = 1 << align; 8648 outs() << " align 2^" << align << " (" << align_shifted << ")\n"; 8649 outs() << " reloff " << reloff; 8650 if (reloff > object_size) 8651 outs() << " (past end of file)\n"; 8652 else 8653 outs() << "\n"; 8654 outs() << " nreloc " << nreloc; 8655 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size) 8656 outs() << " (past end of file)\n"; 8657 else 8658 outs() << "\n"; 8659 uint32_t section_type = flags & MachO::SECTION_TYPE; 8660 if (verbose) { 8661 outs() << " type"; 8662 if (section_type == MachO::S_REGULAR) 8663 outs() << " S_REGULAR\n"; 8664 else if (section_type == MachO::S_ZEROFILL) 8665 outs() << " S_ZEROFILL\n"; 8666 else if (section_type == MachO::S_CSTRING_LITERALS) 8667 outs() << " S_CSTRING_LITERALS\n"; 8668 else if (section_type == MachO::S_4BYTE_LITERALS) 8669 outs() << " S_4BYTE_LITERALS\n"; 8670 else if (section_type == MachO::S_8BYTE_LITERALS) 8671 outs() << " S_8BYTE_LITERALS\n"; 8672 else if (section_type == MachO::S_16BYTE_LITERALS) 8673 outs() << " S_16BYTE_LITERALS\n"; 8674 else if (section_type == MachO::S_LITERAL_POINTERS) 8675 outs() << " S_LITERAL_POINTERS\n"; 8676 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS) 8677 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n"; 8678 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS) 8679 outs() << " S_LAZY_SYMBOL_POINTERS\n"; 8680 else if (section_type == MachO::S_SYMBOL_STUBS) 8681 outs() << " S_SYMBOL_STUBS\n"; 8682 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS) 8683 outs() << " S_MOD_INIT_FUNC_POINTERS\n"; 8684 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS) 8685 outs() << " S_MOD_TERM_FUNC_POINTERS\n"; 8686 else if (section_type == MachO::S_COALESCED) 8687 outs() << " S_COALESCED\n"; 8688 else if (section_type == MachO::S_INTERPOSING) 8689 outs() << " S_INTERPOSING\n"; 8690 else if (section_type == MachO::S_DTRACE_DOF) 8691 outs() << " S_DTRACE_DOF\n"; 8692 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS) 8693 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n"; 8694 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR) 8695 outs() << " S_THREAD_LOCAL_REGULAR\n"; 8696 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL) 8697 outs() << " S_THREAD_LOCAL_ZEROFILL\n"; 8698 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES) 8699 outs() << " S_THREAD_LOCAL_VARIABLES\n"; 8700 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS) 8701 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n"; 8702 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS) 8703 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n"; 8704 else 8705 outs() << format("0x%08" PRIx32, section_type) << "\n"; 8706 outs() << "attributes"; 8707 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES; 8708 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS) 8709 outs() << " PURE_INSTRUCTIONS"; 8710 if (section_attributes & MachO::S_ATTR_NO_TOC) 8711 outs() << " NO_TOC"; 8712 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS) 8713 outs() << " STRIP_STATIC_SYMS"; 8714 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP) 8715 outs() << " NO_DEAD_STRIP"; 8716 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT) 8717 outs() << " LIVE_SUPPORT"; 8718 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE) 8719 outs() << " SELF_MODIFYING_CODE"; 8720 if (section_attributes & MachO::S_ATTR_DEBUG) 8721 outs() << " DEBUG"; 8722 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS) 8723 outs() << " SOME_INSTRUCTIONS"; 8724 if (section_attributes & MachO::S_ATTR_EXT_RELOC) 8725 outs() << " EXT_RELOC"; 8726 if (section_attributes & MachO::S_ATTR_LOC_RELOC) 8727 outs() << " LOC_RELOC"; 8728 if (section_attributes == 0) 8729 outs() << " (none)"; 8730 outs() << "\n"; 8731 } else 8732 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n"; 8733 outs() << " reserved1 " << reserved1; 8734 if (section_type == MachO::S_SYMBOL_STUBS || 8735 section_type == MachO::S_LAZY_SYMBOL_POINTERS || 8736 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS || 8737 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS || 8738 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS) 8739 outs() << " (index into indirect symbol table)\n"; 8740 else 8741 outs() << "\n"; 8742 outs() << " reserved2 " << reserved2; 8743 if (section_type == MachO::S_SYMBOL_STUBS) 8744 outs() << " (size of stubs)\n"; 8745 else 8746 outs() << "\n"; 8747 } 8748 8749 static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit, 8750 uint32_t object_size) { 8751 outs() << " cmd LC_SYMTAB\n"; 8752 outs() << " cmdsize " << st.cmdsize; 8753 if (st.cmdsize != sizeof(struct MachO::symtab_command)) 8754 outs() << " Incorrect size\n"; 8755 else 8756 outs() << "\n"; 8757 outs() << " symoff " << st.symoff; 8758 if (st.symoff > object_size) 8759 outs() << " (past end of file)\n"; 8760 else 8761 outs() << "\n"; 8762 outs() << " nsyms " << st.nsyms; 8763 uint64_t big_size; 8764 if (Is64Bit) { 8765 big_size = st.nsyms; 8766 big_size *= sizeof(struct MachO::nlist_64); 8767 big_size += st.symoff; 8768 if (big_size > object_size) 8769 outs() << " (past end of file)\n"; 8770 else 8771 outs() << "\n"; 8772 } else { 8773 big_size = st.nsyms; 8774 big_size *= sizeof(struct MachO::nlist); 8775 big_size += st.symoff; 8776 if (big_size > object_size) 8777 outs() << " (past end of file)\n"; 8778 else 8779 outs() << "\n"; 8780 } 8781 outs() << " stroff " << st.stroff; 8782 if (st.stroff > object_size) 8783 outs() << " (past end of file)\n"; 8784 else 8785 outs() << "\n"; 8786 outs() << " strsize " << st.strsize; 8787 big_size = st.stroff; 8788 big_size += st.strsize; 8789 if (big_size > object_size) 8790 outs() << " (past end of file)\n"; 8791 else 8792 outs() << "\n"; 8793 } 8794 8795 static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst, 8796 uint32_t nsyms, uint32_t object_size, 8797 bool Is64Bit) { 8798 outs() << " cmd LC_DYSYMTAB\n"; 8799 outs() << " cmdsize " << dyst.cmdsize; 8800 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command)) 8801 outs() << " Incorrect size\n"; 8802 else 8803 outs() << "\n"; 8804 outs() << " ilocalsym " << dyst.ilocalsym; 8805 if (dyst.ilocalsym > nsyms) 8806 outs() << " (greater than the number of symbols)\n"; 8807 else 8808 outs() << "\n"; 8809 outs() << " nlocalsym " << dyst.nlocalsym; 8810 uint64_t big_size; 8811 big_size = dyst.ilocalsym; 8812 big_size += dyst.nlocalsym; 8813 if (big_size > nsyms) 8814 outs() << " (past the end of the symbol table)\n"; 8815 else 8816 outs() << "\n"; 8817 outs() << " iextdefsym " << dyst.iextdefsym; 8818 if (dyst.iextdefsym > nsyms) 8819 outs() << " (greater than the number of symbols)\n"; 8820 else 8821 outs() << "\n"; 8822 outs() << " nextdefsym " << dyst.nextdefsym; 8823 big_size = dyst.iextdefsym; 8824 big_size += dyst.nextdefsym; 8825 if (big_size > nsyms) 8826 outs() << " (past the end of the symbol table)\n"; 8827 else 8828 outs() << "\n"; 8829 outs() << " iundefsym " << dyst.iundefsym; 8830 if (dyst.iundefsym > nsyms) 8831 outs() << " (greater than the number of symbols)\n"; 8832 else 8833 outs() << "\n"; 8834 outs() << " nundefsym " << dyst.nundefsym; 8835 big_size = dyst.iundefsym; 8836 big_size += dyst.nundefsym; 8837 if (big_size > nsyms) 8838 outs() << " (past the end of the symbol table)\n"; 8839 else 8840 outs() << "\n"; 8841 outs() << " tocoff " << dyst.tocoff; 8842 if (dyst.tocoff > object_size) 8843 outs() << " (past end of file)\n"; 8844 else 8845 outs() << "\n"; 8846 outs() << " ntoc " << dyst.ntoc; 8847 big_size = dyst.ntoc; 8848 big_size *= sizeof(struct MachO::dylib_table_of_contents); 8849 big_size += dyst.tocoff; 8850 if (big_size > object_size) 8851 outs() << " (past end of file)\n"; 8852 else 8853 outs() << "\n"; 8854 outs() << " modtaboff " << dyst.modtaboff; 8855 if (dyst.modtaboff > object_size) 8856 outs() << " (past end of file)\n"; 8857 else 8858 outs() << "\n"; 8859 outs() << " nmodtab " << dyst.nmodtab; 8860 uint64_t modtabend; 8861 if (Is64Bit) { 8862 modtabend = dyst.nmodtab; 8863 modtabend *= sizeof(struct MachO::dylib_module_64); 8864 modtabend += dyst.modtaboff; 8865 } else { 8866 modtabend = dyst.nmodtab; 8867 modtabend *= sizeof(struct MachO::dylib_module); 8868 modtabend += dyst.modtaboff; 8869 } 8870 if (modtabend > object_size) 8871 outs() << " (past end of file)\n"; 8872 else 8873 outs() << "\n"; 8874 outs() << " extrefsymoff " << dyst.extrefsymoff; 8875 if (dyst.extrefsymoff > object_size) 8876 outs() << " (past end of file)\n"; 8877 else 8878 outs() << "\n"; 8879 outs() << " nextrefsyms " << dyst.nextrefsyms; 8880 big_size = dyst.nextrefsyms; 8881 big_size *= sizeof(struct MachO::dylib_reference); 8882 big_size += dyst.extrefsymoff; 8883 if (big_size > object_size) 8884 outs() << " (past end of file)\n"; 8885 else 8886 outs() << "\n"; 8887 outs() << " indirectsymoff " << dyst.indirectsymoff; 8888 if (dyst.indirectsymoff > object_size) 8889 outs() << " (past end of file)\n"; 8890 else 8891 outs() << "\n"; 8892 outs() << " nindirectsyms " << dyst.nindirectsyms; 8893 big_size = dyst.nindirectsyms; 8894 big_size *= sizeof(uint32_t); 8895 big_size += dyst.indirectsymoff; 8896 if (big_size > object_size) 8897 outs() << " (past end of file)\n"; 8898 else 8899 outs() << "\n"; 8900 outs() << " extreloff " << dyst.extreloff; 8901 if (dyst.extreloff > object_size) 8902 outs() << " (past end of file)\n"; 8903 else 8904 outs() << "\n"; 8905 outs() << " nextrel " << dyst.nextrel; 8906 big_size = dyst.nextrel; 8907 big_size *= sizeof(struct MachO::relocation_info); 8908 big_size += dyst.extreloff; 8909 if (big_size > object_size) 8910 outs() << " (past end of file)\n"; 8911 else 8912 outs() << "\n"; 8913 outs() << " locreloff " << dyst.locreloff; 8914 if (dyst.locreloff > object_size) 8915 outs() << " (past end of file)\n"; 8916 else 8917 outs() << "\n"; 8918 outs() << " nlocrel " << dyst.nlocrel; 8919 big_size = dyst.nlocrel; 8920 big_size *= sizeof(struct MachO::relocation_info); 8921 big_size += dyst.locreloff; 8922 if (big_size > object_size) 8923 outs() << " (past end of file)\n"; 8924 else 8925 outs() << "\n"; 8926 } 8927 8928 static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc, 8929 uint32_t object_size) { 8930 if (dc.cmd == MachO::LC_DYLD_INFO) 8931 outs() << " cmd LC_DYLD_INFO\n"; 8932 else 8933 outs() << " cmd LC_DYLD_INFO_ONLY\n"; 8934 outs() << " cmdsize " << dc.cmdsize; 8935 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command)) 8936 outs() << " Incorrect size\n"; 8937 else 8938 outs() << "\n"; 8939 outs() << " rebase_off " << dc.rebase_off; 8940 if (dc.rebase_off > object_size) 8941 outs() << " (past end of file)\n"; 8942 else 8943 outs() << "\n"; 8944 outs() << " rebase_size " << dc.rebase_size; 8945 uint64_t big_size; 8946 big_size = dc.rebase_off; 8947 big_size += dc.rebase_size; 8948 if (big_size > object_size) 8949 outs() << " (past end of file)\n"; 8950 else 8951 outs() << "\n"; 8952 outs() << " bind_off " << dc.bind_off; 8953 if (dc.bind_off > object_size) 8954 outs() << " (past end of file)\n"; 8955 else 8956 outs() << "\n"; 8957 outs() << " bind_size " << dc.bind_size; 8958 big_size = dc.bind_off; 8959 big_size += dc.bind_size; 8960 if (big_size > object_size) 8961 outs() << " (past end of file)\n"; 8962 else 8963 outs() << "\n"; 8964 outs() << " weak_bind_off " << dc.weak_bind_off; 8965 if (dc.weak_bind_off > object_size) 8966 outs() << " (past end of file)\n"; 8967 else 8968 outs() << "\n"; 8969 outs() << " weak_bind_size " << dc.weak_bind_size; 8970 big_size = dc.weak_bind_off; 8971 big_size += dc.weak_bind_size; 8972 if (big_size > object_size) 8973 outs() << " (past end of file)\n"; 8974 else 8975 outs() << "\n"; 8976 outs() << " lazy_bind_off " << dc.lazy_bind_off; 8977 if (dc.lazy_bind_off > object_size) 8978 outs() << " (past end of file)\n"; 8979 else 8980 outs() << "\n"; 8981 outs() << " lazy_bind_size " << dc.lazy_bind_size; 8982 big_size = dc.lazy_bind_off; 8983 big_size += dc.lazy_bind_size; 8984 if (big_size > object_size) 8985 outs() << " (past end of file)\n"; 8986 else 8987 outs() << "\n"; 8988 outs() << " export_off " << dc.export_off; 8989 if (dc.export_off > object_size) 8990 outs() << " (past end of file)\n"; 8991 else 8992 outs() << "\n"; 8993 outs() << " export_size " << dc.export_size; 8994 big_size = dc.export_off; 8995 big_size += dc.export_size; 8996 if (big_size > object_size) 8997 outs() << " (past end of file)\n"; 8998 else 8999 outs() << "\n"; 9000 } 9001 9002 static void PrintDyldLoadCommand(MachO::dylinker_command dyld, 9003 const char *Ptr) { 9004 if (dyld.cmd == MachO::LC_ID_DYLINKER) 9005 outs() << " cmd LC_ID_DYLINKER\n"; 9006 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER) 9007 outs() << " cmd LC_LOAD_DYLINKER\n"; 9008 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT) 9009 outs() << " cmd LC_DYLD_ENVIRONMENT\n"; 9010 else 9011 outs() << " cmd ?(" << dyld.cmd << ")\n"; 9012 outs() << " cmdsize " << dyld.cmdsize; 9013 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command)) 9014 outs() << " Incorrect size\n"; 9015 else 9016 outs() << "\n"; 9017 if (dyld.name >= dyld.cmdsize) 9018 outs() << " name ?(bad offset " << dyld.name << ")\n"; 9019 else { 9020 const char *P = (const char *)(Ptr) + dyld.name; 9021 outs() << " name " << P << " (offset " << dyld.name << ")\n"; 9022 } 9023 } 9024 9025 static void PrintUuidLoadCommand(MachO::uuid_command uuid) { 9026 outs() << " cmd LC_UUID\n"; 9027 outs() << " cmdsize " << uuid.cmdsize; 9028 if (uuid.cmdsize != sizeof(struct MachO::uuid_command)) 9029 outs() << " Incorrect size\n"; 9030 else 9031 outs() << "\n"; 9032 outs() << " uuid "; 9033 for (int i = 0; i < 16; ++i) { 9034 outs() << format("%02" PRIX32, uuid.uuid[i]); 9035 if (i == 3 || i == 5 || i == 7 || i == 9) 9036 outs() << "-"; 9037 } 9038 outs() << "\n"; 9039 } 9040 9041 static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) { 9042 outs() << " cmd LC_RPATH\n"; 9043 outs() << " cmdsize " << rpath.cmdsize; 9044 if (rpath.cmdsize < sizeof(struct MachO::rpath_command)) 9045 outs() << " Incorrect size\n"; 9046 else 9047 outs() << "\n"; 9048 if (rpath.path >= rpath.cmdsize) 9049 outs() << " path ?(bad offset " << rpath.path << ")\n"; 9050 else { 9051 const char *P = (const char *)(Ptr) + rpath.path; 9052 outs() << " path " << P << " (offset " << rpath.path << ")\n"; 9053 } 9054 } 9055 9056 static void PrintVersionMinLoadCommand(MachO::version_min_command vd) { 9057 StringRef LoadCmdName; 9058 switch (vd.cmd) { 9059 case MachO::LC_VERSION_MIN_MACOSX: 9060 LoadCmdName = "LC_VERSION_MIN_MACOSX"; 9061 break; 9062 case MachO::LC_VERSION_MIN_IPHONEOS: 9063 LoadCmdName = "LC_VERSION_MIN_IPHONEOS"; 9064 break; 9065 case MachO::LC_VERSION_MIN_TVOS: 9066 LoadCmdName = "LC_VERSION_MIN_TVOS"; 9067 break; 9068 case MachO::LC_VERSION_MIN_WATCHOS: 9069 LoadCmdName = "LC_VERSION_MIN_WATCHOS"; 9070 break; 9071 default: 9072 llvm_unreachable("Unknown version min load command"); 9073 } 9074 9075 outs() << " cmd " << LoadCmdName << '\n'; 9076 outs() << " cmdsize " << vd.cmdsize; 9077 if (vd.cmdsize != sizeof(struct MachO::version_min_command)) 9078 outs() << " Incorrect size\n"; 9079 else 9080 outs() << "\n"; 9081 outs() << " version " 9082 << MachOObjectFile::getVersionMinMajor(vd, false) << "." 9083 << MachOObjectFile::getVersionMinMinor(vd, false); 9084 uint32_t Update = MachOObjectFile::getVersionMinUpdate(vd, false); 9085 if (Update != 0) 9086 outs() << "." << Update; 9087 outs() << "\n"; 9088 if (vd.sdk == 0) 9089 outs() << " sdk n/a"; 9090 else { 9091 outs() << " sdk " 9092 << MachOObjectFile::getVersionMinMajor(vd, true) << "." 9093 << MachOObjectFile::getVersionMinMinor(vd, true); 9094 } 9095 Update = MachOObjectFile::getVersionMinUpdate(vd, true); 9096 if (Update != 0) 9097 outs() << "." << Update; 9098 outs() << "\n"; 9099 } 9100 9101 static void PrintNoteLoadCommand(MachO::note_command Nt) { 9102 outs() << " cmd LC_NOTE\n"; 9103 outs() << " cmdsize " << Nt.cmdsize; 9104 if (Nt.cmdsize != sizeof(struct MachO::note_command)) 9105 outs() << " Incorrect size\n"; 9106 else 9107 outs() << "\n"; 9108 const char *d = Nt.data_owner; 9109 outs() << "data_owner " << format("%.16s\n", d); 9110 outs() << " offset " << Nt.offset << "\n"; 9111 outs() << " size " << Nt.size << "\n"; 9112 } 9113 9114 static void PrintBuildToolVersion(MachO::build_tool_version bv) { 9115 outs() << " tool " << MachOObjectFile::getBuildTool(bv.tool) << "\n"; 9116 outs() << " version " << MachOObjectFile::getVersionString(bv.version) 9117 << "\n"; 9118 } 9119 9120 static void PrintBuildVersionLoadCommand(const MachOObjectFile *obj, 9121 MachO::build_version_command bd) { 9122 outs() << " cmd LC_BUILD_VERSION\n"; 9123 outs() << " cmdsize " << bd.cmdsize; 9124 if (bd.cmdsize != 9125 sizeof(struct MachO::build_version_command) + 9126 bd.ntools * sizeof(struct MachO::build_tool_version)) 9127 outs() << " Incorrect size\n"; 9128 else 9129 outs() << "\n"; 9130 outs() << " platform " << MachOObjectFile::getBuildPlatform(bd.platform) 9131 << "\n"; 9132 if (bd.sdk) 9133 outs() << " sdk " << MachOObjectFile::getVersionString(bd.sdk) 9134 << "\n"; 9135 else 9136 outs() << " sdk n/a\n"; 9137 outs() << " minos " << MachOObjectFile::getVersionString(bd.minos) 9138 << "\n"; 9139 outs() << " ntools " << bd.ntools << "\n"; 9140 for (unsigned i = 0; i < bd.ntools; ++i) { 9141 MachO::build_tool_version bv = obj->getBuildToolVersion(i); 9142 PrintBuildToolVersion(bv); 9143 } 9144 } 9145 9146 static void PrintSourceVersionCommand(MachO::source_version_command sd) { 9147 outs() << " cmd LC_SOURCE_VERSION\n"; 9148 outs() << " cmdsize " << sd.cmdsize; 9149 if (sd.cmdsize != sizeof(struct MachO::source_version_command)) 9150 outs() << " Incorrect size\n"; 9151 else 9152 outs() << "\n"; 9153 uint64_t a = (sd.version >> 40) & 0xffffff; 9154 uint64_t b = (sd.version >> 30) & 0x3ff; 9155 uint64_t c = (sd.version >> 20) & 0x3ff; 9156 uint64_t d = (sd.version >> 10) & 0x3ff; 9157 uint64_t e = sd.version & 0x3ff; 9158 outs() << " version " << a << "." << b; 9159 if (e != 0) 9160 outs() << "." << c << "." << d << "." << e; 9161 else if (d != 0) 9162 outs() << "." << c << "." << d; 9163 else if (c != 0) 9164 outs() << "." << c; 9165 outs() << "\n"; 9166 } 9167 9168 static void PrintEntryPointCommand(MachO::entry_point_command ep) { 9169 outs() << " cmd LC_MAIN\n"; 9170 outs() << " cmdsize " << ep.cmdsize; 9171 if (ep.cmdsize != sizeof(struct MachO::entry_point_command)) 9172 outs() << " Incorrect size\n"; 9173 else 9174 outs() << "\n"; 9175 outs() << " entryoff " << ep.entryoff << "\n"; 9176 outs() << " stacksize " << ep.stacksize << "\n"; 9177 } 9178 9179 static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec, 9180 uint32_t object_size) { 9181 outs() << " cmd LC_ENCRYPTION_INFO\n"; 9182 outs() << " cmdsize " << ec.cmdsize; 9183 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command)) 9184 outs() << " Incorrect size\n"; 9185 else 9186 outs() << "\n"; 9187 outs() << " cryptoff " << ec.cryptoff; 9188 if (ec.cryptoff > object_size) 9189 outs() << " (past end of file)\n"; 9190 else 9191 outs() << "\n"; 9192 outs() << " cryptsize " << ec.cryptsize; 9193 if (ec.cryptsize > object_size) 9194 outs() << " (past end of file)\n"; 9195 else 9196 outs() << "\n"; 9197 outs() << " cryptid " << ec.cryptid << "\n"; 9198 } 9199 9200 static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec, 9201 uint32_t object_size) { 9202 outs() << " cmd LC_ENCRYPTION_INFO_64\n"; 9203 outs() << " cmdsize " << ec.cmdsize; 9204 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64)) 9205 outs() << " Incorrect size\n"; 9206 else 9207 outs() << "\n"; 9208 outs() << " cryptoff " << ec.cryptoff; 9209 if (ec.cryptoff > object_size) 9210 outs() << " (past end of file)\n"; 9211 else 9212 outs() << "\n"; 9213 outs() << " cryptsize " << ec.cryptsize; 9214 if (ec.cryptsize > object_size) 9215 outs() << " (past end of file)\n"; 9216 else 9217 outs() << "\n"; 9218 outs() << " cryptid " << ec.cryptid << "\n"; 9219 outs() << " pad " << ec.pad << "\n"; 9220 } 9221 9222 static void PrintLinkerOptionCommand(MachO::linker_option_command lo, 9223 const char *Ptr) { 9224 outs() << " cmd LC_LINKER_OPTION\n"; 9225 outs() << " cmdsize " << lo.cmdsize; 9226 if (lo.cmdsize < sizeof(struct MachO::linker_option_command)) 9227 outs() << " Incorrect size\n"; 9228 else 9229 outs() << "\n"; 9230 outs() << " count " << lo.count << "\n"; 9231 const char *string = Ptr + sizeof(struct MachO::linker_option_command); 9232 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command); 9233 uint32_t i = 0; 9234 while (left > 0) { 9235 while (*string == '\0' && left > 0) { 9236 string++; 9237 left--; 9238 } 9239 if (left > 0) { 9240 i++; 9241 outs() << " string #" << i << " " << format("%.*s\n", left, string); 9242 uint32_t NullPos = StringRef(string, left).find('\0'); 9243 uint32_t len = std::min(NullPos, left) + 1; 9244 string += len; 9245 left -= len; 9246 } 9247 } 9248 if (lo.count != i) 9249 outs() << " count " << lo.count << " does not match number of strings " 9250 << i << "\n"; 9251 } 9252 9253 static void PrintSubFrameworkCommand(MachO::sub_framework_command sub, 9254 const char *Ptr) { 9255 outs() << " cmd LC_SUB_FRAMEWORK\n"; 9256 outs() << " cmdsize " << sub.cmdsize; 9257 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command)) 9258 outs() << " Incorrect size\n"; 9259 else 9260 outs() << "\n"; 9261 if (sub.umbrella < sub.cmdsize) { 9262 const char *P = Ptr + sub.umbrella; 9263 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n"; 9264 } else { 9265 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n"; 9266 } 9267 } 9268 9269 static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub, 9270 const char *Ptr) { 9271 outs() << " cmd LC_SUB_UMBRELLA\n"; 9272 outs() << " cmdsize " << sub.cmdsize; 9273 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command)) 9274 outs() << " Incorrect size\n"; 9275 else 9276 outs() << "\n"; 9277 if (sub.sub_umbrella < sub.cmdsize) { 9278 const char *P = Ptr + sub.sub_umbrella; 9279 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n"; 9280 } else { 9281 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n"; 9282 } 9283 } 9284 9285 static void PrintSubLibraryCommand(MachO::sub_library_command sub, 9286 const char *Ptr) { 9287 outs() << " cmd LC_SUB_LIBRARY\n"; 9288 outs() << " cmdsize " << sub.cmdsize; 9289 if (sub.cmdsize < sizeof(struct MachO::sub_library_command)) 9290 outs() << " Incorrect size\n"; 9291 else 9292 outs() << "\n"; 9293 if (sub.sub_library < sub.cmdsize) { 9294 const char *P = Ptr + sub.sub_library; 9295 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n"; 9296 } else { 9297 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n"; 9298 } 9299 } 9300 9301 static void PrintSubClientCommand(MachO::sub_client_command sub, 9302 const char *Ptr) { 9303 outs() << " cmd LC_SUB_CLIENT\n"; 9304 outs() << " cmdsize " << sub.cmdsize; 9305 if (sub.cmdsize < sizeof(struct MachO::sub_client_command)) 9306 outs() << " Incorrect size\n"; 9307 else 9308 outs() << "\n"; 9309 if (sub.client < sub.cmdsize) { 9310 const char *P = Ptr + sub.client; 9311 outs() << " client " << P << " (offset " << sub.client << ")\n"; 9312 } else { 9313 outs() << " client ?(bad offset " << sub.client << ")\n"; 9314 } 9315 } 9316 9317 static void PrintRoutinesCommand(MachO::routines_command r) { 9318 outs() << " cmd LC_ROUTINES\n"; 9319 outs() << " cmdsize " << r.cmdsize; 9320 if (r.cmdsize != sizeof(struct MachO::routines_command)) 9321 outs() << " Incorrect size\n"; 9322 else 9323 outs() << "\n"; 9324 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n"; 9325 outs() << " init_module " << r.init_module << "\n"; 9326 outs() << " reserved1 " << r.reserved1 << "\n"; 9327 outs() << " reserved2 " << r.reserved2 << "\n"; 9328 outs() << " reserved3 " << r.reserved3 << "\n"; 9329 outs() << " reserved4 " << r.reserved4 << "\n"; 9330 outs() << " reserved5 " << r.reserved5 << "\n"; 9331 outs() << " reserved6 " << r.reserved6 << "\n"; 9332 } 9333 9334 static void PrintRoutinesCommand64(MachO::routines_command_64 r) { 9335 outs() << " cmd LC_ROUTINES_64\n"; 9336 outs() << " cmdsize " << r.cmdsize; 9337 if (r.cmdsize != sizeof(struct MachO::routines_command_64)) 9338 outs() << " Incorrect size\n"; 9339 else 9340 outs() << "\n"; 9341 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n"; 9342 outs() << " init_module " << r.init_module << "\n"; 9343 outs() << " reserved1 " << r.reserved1 << "\n"; 9344 outs() << " reserved2 " << r.reserved2 << "\n"; 9345 outs() << " reserved3 " << r.reserved3 << "\n"; 9346 outs() << " reserved4 " << r.reserved4 << "\n"; 9347 outs() << " reserved5 " << r.reserved5 << "\n"; 9348 outs() << " reserved6 " << r.reserved6 << "\n"; 9349 } 9350 9351 static void Print_x86_thread_state32_t(MachO::x86_thread_state32_t &cpu32) { 9352 outs() << "\t eax " << format("0x%08" PRIx32, cpu32.eax); 9353 outs() << " ebx " << format("0x%08" PRIx32, cpu32.ebx); 9354 outs() << " ecx " << format("0x%08" PRIx32, cpu32.ecx); 9355 outs() << " edx " << format("0x%08" PRIx32, cpu32.edx) << "\n"; 9356 outs() << "\t edi " << format("0x%08" PRIx32, cpu32.edi); 9357 outs() << " esi " << format("0x%08" PRIx32, cpu32.esi); 9358 outs() << " ebp " << format("0x%08" PRIx32, cpu32.ebp); 9359 outs() << " esp " << format("0x%08" PRIx32, cpu32.esp) << "\n"; 9360 outs() << "\t ss " << format("0x%08" PRIx32, cpu32.ss); 9361 outs() << " eflags " << format("0x%08" PRIx32, cpu32.eflags); 9362 outs() << " eip " << format("0x%08" PRIx32, cpu32.eip); 9363 outs() << " cs " << format("0x%08" PRIx32, cpu32.cs) << "\n"; 9364 outs() << "\t ds " << format("0x%08" PRIx32, cpu32.ds); 9365 outs() << " es " << format("0x%08" PRIx32, cpu32.es); 9366 outs() << " fs " << format("0x%08" PRIx32, cpu32.fs); 9367 outs() << " gs " << format("0x%08" PRIx32, cpu32.gs) << "\n"; 9368 } 9369 9370 static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) { 9371 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax); 9372 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx); 9373 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n"; 9374 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx); 9375 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi); 9376 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n"; 9377 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp); 9378 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp); 9379 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n"; 9380 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9); 9381 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10); 9382 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n"; 9383 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12); 9384 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13); 9385 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n"; 9386 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15); 9387 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n"; 9388 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags); 9389 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs); 9390 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n"; 9391 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n"; 9392 } 9393 9394 static void Print_mmst_reg(MachO::mmst_reg_t &r) { 9395 uint32_t f; 9396 outs() << "\t mmst_reg "; 9397 for (f = 0; f < 10; f++) 9398 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " "; 9399 outs() << "\n"; 9400 outs() << "\t mmst_rsrv "; 9401 for (f = 0; f < 6; f++) 9402 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " "; 9403 outs() << "\n"; 9404 } 9405 9406 static void Print_xmm_reg(MachO::xmm_reg_t &r) { 9407 uint32_t f; 9408 outs() << "\t xmm_reg "; 9409 for (f = 0; f < 16; f++) 9410 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " "; 9411 outs() << "\n"; 9412 } 9413 9414 static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) { 9415 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0]; 9416 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n"; 9417 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid; 9418 outs() << " denorm " << fpu.fpu_fcw.denorm; 9419 outs() << " zdiv " << fpu.fpu_fcw.zdiv; 9420 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl; 9421 outs() << " undfl " << fpu.fpu_fcw.undfl; 9422 outs() << " precis " << fpu.fpu_fcw.precis << "\n"; 9423 outs() << "\t\t pc "; 9424 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B) 9425 outs() << "FP_PREC_24B "; 9426 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B) 9427 outs() << "FP_PREC_53B "; 9428 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B) 9429 outs() << "FP_PREC_64B "; 9430 else 9431 outs() << fpu.fpu_fcw.pc << " "; 9432 outs() << "rc "; 9433 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR) 9434 outs() << "FP_RND_NEAR "; 9435 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN) 9436 outs() << "FP_RND_DOWN "; 9437 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP) 9438 outs() << "FP_RND_UP "; 9439 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP) 9440 outs() << "FP_CHOP "; 9441 outs() << "\n"; 9442 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid; 9443 outs() << " denorm " << fpu.fpu_fsw.denorm; 9444 outs() << " zdiv " << fpu.fpu_fsw.zdiv; 9445 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl; 9446 outs() << " undfl " << fpu.fpu_fsw.undfl; 9447 outs() << " precis " << fpu.fpu_fsw.precis; 9448 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n"; 9449 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm; 9450 outs() << " c0 " << fpu.fpu_fsw.c0; 9451 outs() << " c1 " << fpu.fpu_fsw.c1; 9452 outs() << " c2 " << fpu.fpu_fsw.c2; 9453 outs() << " tos " << fpu.fpu_fsw.tos; 9454 outs() << " c3 " << fpu.fpu_fsw.c3; 9455 outs() << " busy " << fpu.fpu_fsw.busy << "\n"; 9456 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw); 9457 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1); 9458 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop); 9459 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n"; 9460 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs); 9461 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2); 9462 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp); 9463 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n"; 9464 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3); 9465 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr); 9466 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask); 9467 outs() << "\n"; 9468 outs() << "\t fpu_stmm0:\n"; 9469 Print_mmst_reg(fpu.fpu_stmm0); 9470 outs() << "\t fpu_stmm1:\n"; 9471 Print_mmst_reg(fpu.fpu_stmm1); 9472 outs() << "\t fpu_stmm2:\n"; 9473 Print_mmst_reg(fpu.fpu_stmm2); 9474 outs() << "\t fpu_stmm3:\n"; 9475 Print_mmst_reg(fpu.fpu_stmm3); 9476 outs() << "\t fpu_stmm4:\n"; 9477 Print_mmst_reg(fpu.fpu_stmm4); 9478 outs() << "\t fpu_stmm5:\n"; 9479 Print_mmst_reg(fpu.fpu_stmm5); 9480 outs() << "\t fpu_stmm6:\n"; 9481 Print_mmst_reg(fpu.fpu_stmm6); 9482 outs() << "\t fpu_stmm7:\n"; 9483 Print_mmst_reg(fpu.fpu_stmm7); 9484 outs() << "\t fpu_xmm0:\n"; 9485 Print_xmm_reg(fpu.fpu_xmm0); 9486 outs() << "\t fpu_xmm1:\n"; 9487 Print_xmm_reg(fpu.fpu_xmm1); 9488 outs() << "\t fpu_xmm2:\n"; 9489 Print_xmm_reg(fpu.fpu_xmm2); 9490 outs() << "\t fpu_xmm3:\n"; 9491 Print_xmm_reg(fpu.fpu_xmm3); 9492 outs() << "\t fpu_xmm4:\n"; 9493 Print_xmm_reg(fpu.fpu_xmm4); 9494 outs() << "\t fpu_xmm5:\n"; 9495 Print_xmm_reg(fpu.fpu_xmm5); 9496 outs() << "\t fpu_xmm6:\n"; 9497 Print_xmm_reg(fpu.fpu_xmm6); 9498 outs() << "\t fpu_xmm7:\n"; 9499 Print_xmm_reg(fpu.fpu_xmm7); 9500 outs() << "\t fpu_xmm8:\n"; 9501 Print_xmm_reg(fpu.fpu_xmm8); 9502 outs() << "\t fpu_xmm9:\n"; 9503 Print_xmm_reg(fpu.fpu_xmm9); 9504 outs() << "\t fpu_xmm10:\n"; 9505 Print_xmm_reg(fpu.fpu_xmm10); 9506 outs() << "\t fpu_xmm11:\n"; 9507 Print_xmm_reg(fpu.fpu_xmm11); 9508 outs() << "\t fpu_xmm12:\n"; 9509 Print_xmm_reg(fpu.fpu_xmm12); 9510 outs() << "\t fpu_xmm13:\n"; 9511 Print_xmm_reg(fpu.fpu_xmm13); 9512 outs() << "\t fpu_xmm14:\n"; 9513 Print_xmm_reg(fpu.fpu_xmm14); 9514 outs() << "\t fpu_xmm15:\n"; 9515 Print_xmm_reg(fpu.fpu_xmm15); 9516 outs() << "\t fpu_rsrv4:\n"; 9517 for (uint32_t f = 0; f < 6; f++) { 9518 outs() << "\t "; 9519 for (uint32_t g = 0; g < 16; g++) 9520 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " "; 9521 outs() << "\n"; 9522 } 9523 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1); 9524 outs() << "\n"; 9525 } 9526 9527 static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) { 9528 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno); 9529 outs() << " err " << format("0x%08" PRIx32, exc64.err); 9530 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n"; 9531 } 9532 9533 static void Print_arm_thread_state32_t(MachO::arm_thread_state32_t &cpu32) { 9534 outs() << "\t r0 " << format("0x%08" PRIx32, cpu32.r[0]); 9535 outs() << " r1 " << format("0x%08" PRIx32, cpu32.r[1]); 9536 outs() << " r2 " << format("0x%08" PRIx32, cpu32.r[2]); 9537 outs() << " r3 " << format("0x%08" PRIx32, cpu32.r[3]) << "\n"; 9538 outs() << "\t r4 " << format("0x%08" PRIx32, cpu32.r[4]); 9539 outs() << " r5 " << format("0x%08" PRIx32, cpu32.r[5]); 9540 outs() << " r6 " << format("0x%08" PRIx32, cpu32.r[6]); 9541 outs() << " r7 " << format("0x%08" PRIx32, cpu32.r[7]) << "\n"; 9542 outs() << "\t r8 " << format("0x%08" PRIx32, cpu32.r[8]); 9543 outs() << " r9 " << format("0x%08" PRIx32, cpu32.r[9]); 9544 outs() << " r10 " << format("0x%08" PRIx32, cpu32.r[10]); 9545 outs() << " r11 " << format("0x%08" PRIx32, cpu32.r[11]) << "\n"; 9546 outs() << "\t r12 " << format("0x%08" PRIx32, cpu32.r[12]); 9547 outs() << " sp " << format("0x%08" PRIx32, cpu32.sp); 9548 outs() << " lr " << format("0x%08" PRIx32, cpu32.lr); 9549 outs() << " pc " << format("0x%08" PRIx32, cpu32.pc) << "\n"; 9550 outs() << "\t cpsr " << format("0x%08" PRIx32, cpu32.cpsr) << "\n"; 9551 } 9552 9553 static void Print_arm_thread_state64_t(MachO::arm_thread_state64_t &cpu64) { 9554 outs() << "\t x0 " << format("0x%016" PRIx64, cpu64.x[0]); 9555 outs() << " x1 " << format("0x%016" PRIx64, cpu64.x[1]); 9556 outs() << " x2 " << format("0x%016" PRIx64, cpu64.x[2]) << "\n"; 9557 outs() << "\t x3 " << format("0x%016" PRIx64, cpu64.x[3]); 9558 outs() << " x4 " << format("0x%016" PRIx64, cpu64.x[4]); 9559 outs() << " x5 " << format("0x%016" PRIx64, cpu64.x[5]) << "\n"; 9560 outs() << "\t x6 " << format("0x%016" PRIx64, cpu64.x[6]); 9561 outs() << " x7 " << format("0x%016" PRIx64, cpu64.x[7]); 9562 outs() << " x8 " << format("0x%016" PRIx64, cpu64.x[8]) << "\n"; 9563 outs() << "\t x9 " << format("0x%016" PRIx64, cpu64.x[9]); 9564 outs() << " x10 " << format("0x%016" PRIx64, cpu64.x[10]); 9565 outs() << " x11 " << format("0x%016" PRIx64, cpu64.x[11]) << "\n"; 9566 outs() << "\t x12 " << format("0x%016" PRIx64, cpu64.x[12]); 9567 outs() << " x13 " << format("0x%016" PRIx64, cpu64.x[13]); 9568 outs() << " x14 " << format("0x%016" PRIx64, cpu64.x[14]) << "\n"; 9569 outs() << "\t x15 " << format("0x%016" PRIx64, cpu64.x[15]); 9570 outs() << " x16 " << format("0x%016" PRIx64, cpu64.x[16]); 9571 outs() << " x17 " << format("0x%016" PRIx64, cpu64.x[17]) << "\n"; 9572 outs() << "\t x18 " << format("0x%016" PRIx64, cpu64.x[18]); 9573 outs() << " x19 " << format("0x%016" PRIx64, cpu64.x[19]); 9574 outs() << " x20 " << format("0x%016" PRIx64, cpu64.x[20]) << "\n"; 9575 outs() << "\t x21 " << format("0x%016" PRIx64, cpu64.x[21]); 9576 outs() << " x22 " << format("0x%016" PRIx64, cpu64.x[22]); 9577 outs() << " x23 " << format("0x%016" PRIx64, cpu64.x[23]) << "\n"; 9578 outs() << "\t x24 " << format("0x%016" PRIx64, cpu64.x[24]); 9579 outs() << " x25 " << format("0x%016" PRIx64, cpu64.x[25]); 9580 outs() << " x26 " << format("0x%016" PRIx64, cpu64.x[26]) << "\n"; 9581 outs() << "\t x27 " << format("0x%016" PRIx64, cpu64.x[27]); 9582 outs() << " x28 " << format("0x%016" PRIx64, cpu64.x[28]); 9583 outs() << " fp " << format("0x%016" PRIx64, cpu64.fp) << "\n"; 9584 outs() << "\t lr " << format("0x%016" PRIx64, cpu64.lr); 9585 outs() << " sp " << format("0x%016" PRIx64, cpu64.sp); 9586 outs() << " pc " << format("0x%016" PRIx64, cpu64.pc) << "\n"; 9587 outs() << "\t cpsr " << format("0x%08" PRIx32, cpu64.cpsr) << "\n"; 9588 } 9589 9590 static void PrintThreadCommand(MachO::thread_command t, const char *Ptr, 9591 bool isLittleEndian, uint32_t cputype) { 9592 if (t.cmd == MachO::LC_THREAD) 9593 outs() << " cmd LC_THREAD\n"; 9594 else if (t.cmd == MachO::LC_UNIXTHREAD) 9595 outs() << " cmd LC_UNIXTHREAD\n"; 9596 else 9597 outs() << " cmd " << t.cmd << " (unknown)\n"; 9598 outs() << " cmdsize " << t.cmdsize; 9599 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t)) 9600 outs() << " Incorrect size\n"; 9601 else 9602 outs() << "\n"; 9603 9604 const char *begin = Ptr + sizeof(struct MachO::thread_command); 9605 const char *end = Ptr + t.cmdsize; 9606 uint32_t flavor, count, left; 9607 if (cputype == MachO::CPU_TYPE_I386) { 9608 while (begin < end) { 9609 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9610 memcpy((char *)&flavor, begin, sizeof(uint32_t)); 9611 begin += sizeof(uint32_t); 9612 } else { 9613 flavor = 0; 9614 begin = end; 9615 } 9616 if (isLittleEndian != sys::IsLittleEndianHost) 9617 sys::swapByteOrder(flavor); 9618 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9619 memcpy((char *)&count, begin, sizeof(uint32_t)); 9620 begin += sizeof(uint32_t); 9621 } else { 9622 count = 0; 9623 begin = end; 9624 } 9625 if (isLittleEndian != sys::IsLittleEndianHost) 9626 sys::swapByteOrder(count); 9627 if (flavor == MachO::x86_THREAD_STATE32) { 9628 outs() << " flavor i386_THREAD_STATE\n"; 9629 if (count == MachO::x86_THREAD_STATE32_COUNT) 9630 outs() << " count i386_THREAD_STATE_COUNT\n"; 9631 else 9632 outs() << " count " << count 9633 << " (not x86_THREAD_STATE32_COUNT)\n"; 9634 MachO::x86_thread_state32_t cpu32; 9635 left = end - begin; 9636 if (left >= sizeof(MachO::x86_thread_state32_t)) { 9637 memcpy(&cpu32, begin, sizeof(MachO::x86_thread_state32_t)); 9638 begin += sizeof(MachO::x86_thread_state32_t); 9639 } else { 9640 memset(&cpu32, '\0', sizeof(MachO::x86_thread_state32_t)); 9641 memcpy(&cpu32, begin, left); 9642 begin += left; 9643 } 9644 if (isLittleEndian != sys::IsLittleEndianHost) 9645 swapStruct(cpu32); 9646 Print_x86_thread_state32_t(cpu32); 9647 } else if (flavor == MachO::x86_THREAD_STATE) { 9648 outs() << " flavor x86_THREAD_STATE\n"; 9649 if (count == MachO::x86_THREAD_STATE_COUNT) 9650 outs() << " count x86_THREAD_STATE_COUNT\n"; 9651 else 9652 outs() << " count " << count 9653 << " (not x86_THREAD_STATE_COUNT)\n"; 9654 struct MachO::x86_thread_state_t ts; 9655 left = end - begin; 9656 if (left >= sizeof(MachO::x86_thread_state_t)) { 9657 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t)); 9658 begin += sizeof(MachO::x86_thread_state_t); 9659 } else { 9660 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t)); 9661 memcpy(&ts, begin, left); 9662 begin += left; 9663 } 9664 if (isLittleEndian != sys::IsLittleEndianHost) 9665 swapStruct(ts); 9666 if (ts.tsh.flavor == MachO::x86_THREAD_STATE32) { 9667 outs() << "\t tsh.flavor x86_THREAD_STATE32 "; 9668 if (ts.tsh.count == MachO::x86_THREAD_STATE32_COUNT) 9669 outs() << "tsh.count x86_THREAD_STATE32_COUNT\n"; 9670 else 9671 outs() << "tsh.count " << ts.tsh.count 9672 << " (not x86_THREAD_STATE32_COUNT\n"; 9673 Print_x86_thread_state32_t(ts.uts.ts32); 9674 } else { 9675 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count " 9676 << ts.tsh.count << "\n"; 9677 } 9678 } else { 9679 outs() << " flavor " << flavor << " (unknown)\n"; 9680 outs() << " count " << count << "\n"; 9681 outs() << " state (unknown)\n"; 9682 begin += count * sizeof(uint32_t); 9683 } 9684 } 9685 } else if (cputype == MachO::CPU_TYPE_X86_64) { 9686 while (begin < end) { 9687 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9688 memcpy((char *)&flavor, begin, sizeof(uint32_t)); 9689 begin += sizeof(uint32_t); 9690 } else { 9691 flavor = 0; 9692 begin = end; 9693 } 9694 if (isLittleEndian != sys::IsLittleEndianHost) 9695 sys::swapByteOrder(flavor); 9696 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9697 memcpy((char *)&count, begin, sizeof(uint32_t)); 9698 begin += sizeof(uint32_t); 9699 } else { 9700 count = 0; 9701 begin = end; 9702 } 9703 if (isLittleEndian != sys::IsLittleEndianHost) 9704 sys::swapByteOrder(count); 9705 if (flavor == MachO::x86_THREAD_STATE64) { 9706 outs() << " flavor x86_THREAD_STATE64\n"; 9707 if (count == MachO::x86_THREAD_STATE64_COUNT) 9708 outs() << " count x86_THREAD_STATE64_COUNT\n"; 9709 else 9710 outs() << " count " << count 9711 << " (not x86_THREAD_STATE64_COUNT)\n"; 9712 MachO::x86_thread_state64_t cpu64; 9713 left = end - begin; 9714 if (left >= sizeof(MachO::x86_thread_state64_t)) { 9715 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t)); 9716 begin += sizeof(MachO::x86_thread_state64_t); 9717 } else { 9718 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t)); 9719 memcpy(&cpu64, begin, left); 9720 begin += left; 9721 } 9722 if (isLittleEndian != sys::IsLittleEndianHost) 9723 swapStruct(cpu64); 9724 Print_x86_thread_state64_t(cpu64); 9725 } else if (flavor == MachO::x86_THREAD_STATE) { 9726 outs() << " flavor x86_THREAD_STATE\n"; 9727 if (count == MachO::x86_THREAD_STATE_COUNT) 9728 outs() << " count x86_THREAD_STATE_COUNT\n"; 9729 else 9730 outs() << " count " << count 9731 << " (not x86_THREAD_STATE_COUNT)\n"; 9732 struct MachO::x86_thread_state_t ts; 9733 left = end - begin; 9734 if (left >= sizeof(MachO::x86_thread_state_t)) { 9735 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t)); 9736 begin += sizeof(MachO::x86_thread_state_t); 9737 } else { 9738 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t)); 9739 memcpy(&ts, begin, left); 9740 begin += left; 9741 } 9742 if (isLittleEndian != sys::IsLittleEndianHost) 9743 swapStruct(ts); 9744 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) { 9745 outs() << "\t tsh.flavor x86_THREAD_STATE64 "; 9746 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT) 9747 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n"; 9748 else 9749 outs() << "tsh.count " << ts.tsh.count 9750 << " (not x86_THREAD_STATE64_COUNT\n"; 9751 Print_x86_thread_state64_t(ts.uts.ts64); 9752 } else { 9753 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count " 9754 << ts.tsh.count << "\n"; 9755 } 9756 } else if (flavor == MachO::x86_FLOAT_STATE) { 9757 outs() << " flavor x86_FLOAT_STATE\n"; 9758 if (count == MachO::x86_FLOAT_STATE_COUNT) 9759 outs() << " count x86_FLOAT_STATE_COUNT\n"; 9760 else 9761 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n"; 9762 struct MachO::x86_float_state_t fs; 9763 left = end - begin; 9764 if (left >= sizeof(MachO::x86_float_state_t)) { 9765 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t)); 9766 begin += sizeof(MachO::x86_float_state_t); 9767 } else { 9768 memset(&fs, '\0', sizeof(MachO::x86_float_state_t)); 9769 memcpy(&fs, begin, left); 9770 begin += left; 9771 } 9772 if (isLittleEndian != sys::IsLittleEndianHost) 9773 swapStruct(fs); 9774 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) { 9775 outs() << "\t fsh.flavor x86_FLOAT_STATE64 "; 9776 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT) 9777 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n"; 9778 else 9779 outs() << "fsh.count " << fs.fsh.count 9780 << " (not x86_FLOAT_STATE64_COUNT\n"; 9781 Print_x86_float_state_t(fs.ufs.fs64); 9782 } else { 9783 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count " 9784 << fs.fsh.count << "\n"; 9785 } 9786 } else if (flavor == MachO::x86_EXCEPTION_STATE) { 9787 outs() << " flavor x86_EXCEPTION_STATE\n"; 9788 if (count == MachO::x86_EXCEPTION_STATE_COUNT) 9789 outs() << " count x86_EXCEPTION_STATE_COUNT\n"; 9790 else 9791 outs() << " count " << count 9792 << " (not x86_EXCEPTION_STATE_COUNT)\n"; 9793 struct MachO::x86_exception_state_t es; 9794 left = end - begin; 9795 if (left >= sizeof(MachO::x86_exception_state_t)) { 9796 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t)); 9797 begin += sizeof(MachO::x86_exception_state_t); 9798 } else { 9799 memset(&es, '\0', sizeof(MachO::x86_exception_state_t)); 9800 memcpy(&es, begin, left); 9801 begin += left; 9802 } 9803 if (isLittleEndian != sys::IsLittleEndianHost) 9804 swapStruct(es); 9805 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) { 9806 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n"; 9807 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT) 9808 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n"; 9809 else 9810 outs() << "\t esh.count " << es.esh.count 9811 << " (not x86_EXCEPTION_STATE64_COUNT\n"; 9812 Print_x86_exception_state_t(es.ues.es64); 9813 } else { 9814 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count " 9815 << es.esh.count << "\n"; 9816 } 9817 } else if (flavor == MachO::x86_EXCEPTION_STATE64) { 9818 outs() << " flavor x86_EXCEPTION_STATE64\n"; 9819 if (count == MachO::x86_EXCEPTION_STATE64_COUNT) 9820 outs() << " count x86_EXCEPTION_STATE64_COUNT\n"; 9821 else 9822 outs() << " count " << count 9823 << " (not x86_EXCEPTION_STATE64_COUNT)\n"; 9824 struct MachO::x86_exception_state64_t es64; 9825 left = end - begin; 9826 if (left >= sizeof(MachO::x86_exception_state64_t)) { 9827 memcpy(&es64, begin, sizeof(MachO::x86_exception_state64_t)); 9828 begin += sizeof(MachO::x86_exception_state64_t); 9829 } else { 9830 memset(&es64, '\0', sizeof(MachO::x86_exception_state64_t)); 9831 memcpy(&es64, begin, left); 9832 begin += left; 9833 } 9834 if (isLittleEndian != sys::IsLittleEndianHost) 9835 swapStruct(es64); 9836 Print_x86_exception_state_t(es64); 9837 } else { 9838 outs() << " flavor " << flavor << " (unknown)\n"; 9839 outs() << " count " << count << "\n"; 9840 outs() << " state (unknown)\n"; 9841 begin += count * sizeof(uint32_t); 9842 } 9843 } 9844 } else if (cputype == MachO::CPU_TYPE_ARM) { 9845 while (begin < end) { 9846 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9847 memcpy((char *)&flavor, begin, sizeof(uint32_t)); 9848 begin += sizeof(uint32_t); 9849 } else { 9850 flavor = 0; 9851 begin = end; 9852 } 9853 if (isLittleEndian != sys::IsLittleEndianHost) 9854 sys::swapByteOrder(flavor); 9855 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9856 memcpy((char *)&count, begin, sizeof(uint32_t)); 9857 begin += sizeof(uint32_t); 9858 } else { 9859 count = 0; 9860 begin = end; 9861 } 9862 if (isLittleEndian != sys::IsLittleEndianHost) 9863 sys::swapByteOrder(count); 9864 if (flavor == MachO::ARM_THREAD_STATE) { 9865 outs() << " flavor ARM_THREAD_STATE\n"; 9866 if (count == MachO::ARM_THREAD_STATE_COUNT) 9867 outs() << " count ARM_THREAD_STATE_COUNT\n"; 9868 else 9869 outs() << " count " << count 9870 << " (not ARM_THREAD_STATE_COUNT)\n"; 9871 MachO::arm_thread_state32_t cpu32; 9872 left = end - begin; 9873 if (left >= sizeof(MachO::arm_thread_state32_t)) { 9874 memcpy(&cpu32, begin, sizeof(MachO::arm_thread_state32_t)); 9875 begin += sizeof(MachO::arm_thread_state32_t); 9876 } else { 9877 memset(&cpu32, '\0', sizeof(MachO::arm_thread_state32_t)); 9878 memcpy(&cpu32, begin, left); 9879 begin += left; 9880 } 9881 if (isLittleEndian != sys::IsLittleEndianHost) 9882 swapStruct(cpu32); 9883 Print_arm_thread_state32_t(cpu32); 9884 } else { 9885 outs() << " flavor " << flavor << " (unknown)\n"; 9886 outs() << " count " << count << "\n"; 9887 outs() << " state (unknown)\n"; 9888 begin += count * sizeof(uint32_t); 9889 } 9890 } 9891 } else if (cputype == MachO::CPU_TYPE_ARM64 || 9892 cputype == MachO::CPU_TYPE_ARM64_32) { 9893 while (begin < end) { 9894 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9895 memcpy((char *)&flavor, begin, sizeof(uint32_t)); 9896 begin += sizeof(uint32_t); 9897 } else { 9898 flavor = 0; 9899 begin = end; 9900 } 9901 if (isLittleEndian != sys::IsLittleEndianHost) 9902 sys::swapByteOrder(flavor); 9903 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9904 memcpy((char *)&count, begin, sizeof(uint32_t)); 9905 begin += sizeof(uint32_t); 9906 } else { 9907 count = 0; 9908 begin = end; 9909 } 9910 if (isLittleEndian != sys::IsLittleEndianHost) 9911 sys::swapByteOrder(count); 9912 if (flavor == MachO::ARM_THREAD_STATE64) { 9913 outs() << " flavor ARM_THREAD_STATE64\n"; 9914 if (count == MachO::ARM_THREAD_STATE64_COUNT) 9915 outs() << " count ARM_THREAD_STATE64_COUNT\n"; 9916 else 9917 outs() << " count " << count 9918 << " (not ARM_THREAD_STATE64_COUNT)\n"; 9919 MachO::arm_thread_state64_t cpu64; 9920 left = end - begin; 9921 if (left >= sizeof(MachO::arm_thread_state64_t)) { 9922 memcpy(&cpu64, begin, sizeof(MachO::arm_thread_state64_t)); 9923 begin += sizeof(MachO::arm_thread_state64_t); 9924 } else { 9925 memset(&cpu64, '\0', sizeof(MachO::arm_thread_state64_t)); 9926 memcpy(&cpu64, begin, left); 9927 begin += left; 9928 } 9929 if (isLittleEndian != sys::IsLittleEndianHost) 9930 swapStruct(cpu64); 9931 Print_arm_thread_state64_t(cpu64); 9932 } else { 9933 outs() << " flavor " << flavor << " (unknown)\n"; 9934 outs() << " count " << count << "\n"; 9935 outs() << " state (unknown)\n"; 9936 begin += count * sizeof(uint32_t); 9937 } 9938 } 9939 } else { 9940 while (begin < end) { 9941 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9942 memcpy((char *)&flavor, begin, sizeof(uint32_t)); 9943 begin += sizeof(uint32_t); 9944 } else { 9945 flavor = 0; 9946 begin = end; 9947 } 9948 if (isLittleEndian != sys::IsLittleEndianHost) 9949 sys::swapByteOrder(flavor); 9950 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9951 memcpy((char *)&count, begin, sizeof(uint32_t)); 9952 begin += sizeof(uint32_t); 9953 } else { 9954 count = 0; 9955 begin = end; 9956 } 9957 if (isLittleEndian != sys::IsLittleEndianHost) 9958 sys::swapByteOrder(count); 9959 outs() << " flavor " << flavor << "\n"; 9960 outs() << " count " << count << "\n"; 9961 outs() << " state (Unknown cputype/cpusubtype)\n"; 9962 begin += count * sizeof(uint32_t); 9963 } 9964 } 9965 } 9966 9967 static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) { 9968 if (dl.cmd == MachO::LC_ID_DYLIB) 9969 outs() << " cmd LC_ID_DYLIB\n"; 9970 else if (dl.cmd == MachO::LC_LOAD_DYLIB) 9971 outs() << " cmd LC_LOAD_DYLIB\n"; 9972 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB) 9973 outs() << " cmd LC_LOAD_WEAK_DYLIB\n"; 9974 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB) 9975 outs() << " cmd LC_REEXPORT_DYLIB\n"; 9976 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB) 9977 outs() << " cmd LC_LAZY_LOAD_DYLIB\n"; 9978 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB) 9979 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n"; 9980 else 9981 outs() << " cmd " << dl.cmd << " (unknown)\n"; 9982 outs() << " cmdsize " << dl.cmdsize; 9983 if (dl.cmdsize < sizeof(struct MachO::dylib_command)) 9984 outs() << " Incorrect size\n"; 9985 else 9986 outs() << "\n"; 9987 if (dl.dylib.name < dl.cmdsize) { 9988 const char *P = (const char *)(Ptr) + dl.dylib.name; 9989 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n"; 9990 } else { 9991 outs() << " name ?(bad offset " << dl.dylib.name << ")\n"; 9992 } 9993 outs() << " time stamp " << dl.dylib.timestamp << " "; 9994 time_t t = dl.dylib.timestamp; 9995 outs() << ctime(&t); 9996 outs() << " current version "; 9997 if (dl.dylib.current_version == 0xffffffff) 9998 outs() << "n/a\n"; 9999 else 10000 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "." 10001 << ((dl.dylib.current_version >> 8) & 0xff) << "." 10002 << (dl.dylib.current_version & 0xff) << "\n"; 10003 outs() << "compatibility version "; 10004 if (dl.dylib.compatibility_version == 0xffffffff) 10005 outs() << "n/a\n"; 10006 else 10007 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "." 10008 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "." 10009 << (dl.dylib.compatibility_version & 0xff) << "\n"; 10010 } 10011 10012 static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld, 10013 uint32_t object_size) { 10014 if (ld.cmd == MachO::LC_CODE_SIGNATURE) 10015 outs() << " cmd LC_CODE_SIGNATURE\n"; 10016 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO) 10017 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n"; 10018 else if (ld.cmd == MachO::LC_FUNCTION_STARTS) 10019 outs() << " cmd LC_FUNCTION_STARTS\n"; 10020 else if (ld.cmd == MachO::LC_DATA_IN_CODE) 10021 outs() << " cmd LC_DATA_IN_CODE\n"; 10022 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS) 10023 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n"; 10024 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) 10025 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n"; 10026 else 10027 outs() << " cmd " << ld.cmd << " (?)\n"; 10028 outs() << " cmdsize " << ld.cmdsize; 10029 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command)) 10030 outs() << " Incorrect size\n"; 10031 else 10032 outs() << "\n"; 10033 outs() << " dataoff " << ld.dataoff; 10034 if (ld.dataoff > object_size) 10035 outs() << " (past end of file)\n"; 10036 else 10037 outs() << "\n"; 10038 outs() << " datasize " << ld.datasize; 10039 uint64_t big_size = ld.dataoff; 10040 big_size += ld.datasize; 10041 if (big_size > object_size) 10042 outs() << " (past end of file)\n"; 10043 else 10044 outs() << "\n"; 10045 } 10046 10047 static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype, 10048 uint32_t cputype, bool verbose) { 10049 StringRef Buf = Obj->getData(); 10050 unsigned Index = 0; 10051 for (const auto &Command : Obj->load_commands()) { 10052 outs() << "Load command " << Index++ << "\n"; 10053 if (Command.C.cmd == MachO::LC_SEGMENT) { 10054 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command); 10055 const char *sg_segname = SLC.segname; 10056 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr, 10057 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot, 10058 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(), 10059 verbose); 10060 for (unsigned j = 0; j < SLC.nsects; j++) { 10061 MachO::section S = Obj->getSection(Command, j); 10062 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align, 10063 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2, 10064 SLC.cmd, sg_segname, filetype, Buf.size(), verbose); 10065 } 10066 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) { 10067 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command); 10068 const char *sg_segname = SLC_64.segname; 10069 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname, 10070 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff, 10071 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot, 10072 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose); 10073 for (unsigned j = 0; j < SLC_64.nsects; j++) { 10074 MachO::section_64 S_64 = Obj->getSection64(Command, j); 10075 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size, 10076 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc, 10077 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd, 10078 sg_segname, filetype, Buf.size(), verbose); 10079 } 10080 } else if (Command.C.cmd == MachO::LC_SYMTAB) { 10081 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand(); 10082 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size()); 10083 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) { 10084 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand(); 10085 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand(); 10086 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(), 10087 Obj->is64Bit()); 10088 } else if (Command.C.cmd == MachO::LC_DYLD_INFO || 10089 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) { 10090 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command); 10091 PrintDyldInfoLoadCommand(DyldInfo, Buf.size()); 10092 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER || 10093 Command.C.cmd == MachO::LC_ID_DYLINKER || 10094 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) { 10095 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command); 10096 PrintDyldLoadCommand(Dyld, Command.Ptr); 10097 } else if (Command.C.cmd == MachO::LC_UUID) { 10098 MachO::uuid_command Uuid = Obj->getUuidCommand(Command); 10099 PrintUuidLoadCommand(Uuid); 10100 } else if (Command.C.cmd == MachO::LC_RPATH) { 10101 MachO::rpath_command Rpath = Obj->getRpathCommand(Command); 10102 PrintRpathLoadCommand(Rpath, Command.Ptr); 10103 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX || 10104 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS || 10105 Command.C.cmd == MachO::LC_VERSION_MIN_TVOS || 10106 Command.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) { 10107 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command); 10108 PrintVersionMinLoadCommand(Vd); 10109 } else if (Command.C.cmd == MachO::LC_NOTE) { 10110 MachO::note_command Nt = Obj->getNoteLoadCommand(Command); 10111 PrintNoteLoadCommand(Nt); 10112 } else if (Command.C.cmd == MachO::LC_BUILD_VERSION) { 10113 MachO::build_version_command Bv = 10114 Obj->getBuildVersionLoadCommand(Command); 10115 PrintBuildVersionLoadCommand(Obj, Bv); 10116 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) { 10117 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command); 10118 PrintSourceVersionCommand(Sd); 10119 } else if (Command.C.cmd == MachO::LC_MAIN) { 10120 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command); 10121 PrintEntryPointCommand(Ep); 10122 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) { 10123 MachO::encryption_info_command Ei = 10124 Obj->getEncryptionInfoCommand(Command); 10125 PrintEncryptionInfoCommand(Ei, Buf.size()); 10126 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) { 10127 MachO::encryption_info_command_64 Ei = 10128 Obj->getEncryptionInfoCommand64(Command); 10129 PrintEncryptionInfoCommand64(Ei, Buf.size()); 10130 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) { 10131 MachO::linker_option_command Lo = 10132 Obj->getLinkerOptionLoadCommand(Command); 10133 PrintLinkerOptionCommand(Lo, Command.Ptr); 10134 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) { 10135 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command); 10136 PrintSubFrameworkCommand(Sf, Command.Ptr); 10137 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) { 10138 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command); 10139 PrintSubUmbrellaCommand(Sf, Command.Ptr); 10140 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) { 10141 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command); 10142 PrintSubLibraryCommand(Sl, Command.Ptr); 10143 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) { 10144 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command); 10145 PrintSubClientCommand(Sc, Command.Ptr); 10146 } else if (Command.C.cmd == MachO::LC_ROUTINES) { 10147 MachO::routines_command Rc = Obj->getRoutinesCommand(Command); 10148 PrintRoutinesCommand(Rc); 10149 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) { 10150 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command); 10151 PrintRoutinesCommand64(Rc); 10152 } else if (Command.C.cmd == MachO::LC_THREAD || 10153 Command.C.cmd == MachO::LC_UNIXTHREAD) { 10154 MachO::thread_command Tc = Obj->getThreadCommand(Command); 10155 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype); 10156 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB || 10157 Command.C.cmd == MachO::LC_ID_DYLIB || 10158 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB || 10159 Command.C.cmd == MachO::LC_REEXPORT_DYLIB || 10160 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB || 10161 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) { 10162 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command); 10163 PrintDylibCommand(Dl, Command.Ptr); 10164 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE || 10165 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO || 10166 Command.C.cmd == MachO::LC_FUNCTION_STARTS || 10167 Command.C.cmd == MachO::LC_DATA_IN_CODE || 10168 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS || 10169 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) { 10170 MachO::linkedit_data_command Ld = 10171 Obj->getLinkeditDataLoadCommand(Command); 10172 PrintLinkEditDataCommand(Ld, Buf.size()); 10173 } else { 10174 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd) 10175 << ")\n"; 10176 outs() << " cmdsize " << Command.C.cmdsize << "\n"; 10177 // TODO: get and print the raw bytes of the load command. 10178 } 10179 // TODO: print all the other kinds of load commands. 10180 } 10181 } 10182 10183 static void PrintMachHeader(const MachOObjectFile *Obj, bool verbose) { 10184 if (Obj->is64Bit()) { 10185 MachO::mach_header_64 H_64; 10186 H_64 = Obj->getHeader64(); 10187 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype, 10188 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose); 10189 } else { 10190 MachO::mach_header H; 10191 H = Obj->getHeader(); 10192 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds, 10193 H.sizeofcmds, H.flags, verbose); 10194 } 10195 } 10196 10197 void printMachOFileHeader(const object::ObjectFile *Obj) { 10198 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj); 10199 PrintMachHeader(file, !NonVerbose); 10200 } 10201 10202 void printMachOLoadCommands(const object::ObjectFile *Obj) { 10203 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj); 10204 uint32_t filetype = 0; 10205 uint32_t cputype = 0; 10206 if (file->is64Bit()) { 10207 MachO::mach_header_64 H_64; 10208 H_64 = file->getHeader64(); 10209 filetype = H_64.filetype; 10210 cputype = H_64.cputype; 10211 } else { 10212 MachO::mach_header H; 10213 H = file->getHeader(); 10214 filetype = H.filetype; 10215 cputype = H.cputype; 10216 } 10217 PrintLoadCommands(file, filetype, cputype, !NonVerbose); 10218 } 10219 10220 //===----------------------------------------------------------------------===// 10221 // export trie dumping 10222 //===----------------------------------------------------------------------===// 10223 10224 void printMachOExportsTrie(const object::MachOObjectFile *Obj) { 10225 uint64_t BaseSegmentAddress = 0; 10226 for (const auto &Command : Obj->load_commands()) { 10227 if (Command.C.cmd == MachO::LC_SEGMENT) { 10228 MachO::segment_command Seg = Obj->getSegmentLoadCommand(Command); 10229 if (Seg.fileoff == 0 && Seg.filesize != 0) { 10230 BaseSegmentAddress = Seg.vmaddr; 10231 break; 10232 } 10233 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) { 10234 MachO::segment_command_64 Seg = Obj->getSegment64LoadCommand(Command); 10235 if (Seg.fileoff == 0 && Seg.filesize != 0) { 10236 BaseSegmentAddress = Seg.vmaddr; 10237 break; 10238 } 10239 } 10240 } 10241 Error Err = Error::success(); 10242 for (const object::ExportEntry &Entry : Obj->exports(Err)) { 10243 uint64_t Flags = Entry.flags(); 10244 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT); 10245 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION); 10246 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) == 10247 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL); 10248 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) == 10249 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE); 10250 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER); 10251 if (ReExport) 10252 outs() << "[re-export] "; 10253 else 10254 outs() << format("0x%08llX ", 10255 Entry.address() + BaseSegmentAddress); 10256 outs() << Entry.name(); 10257 if (WeakDef || ThreadLocal || Resolver || Abs) { 10258 bool NeedsComma = false; 10259 outs() << " ["; 10260 if (WeakDef) { 10261 outs() << "weak_def"; 10262 NeedsComma = true; 10263 } 10264 if (ThreadLocal) { 10265 if (NeedsComma) 10266 outs() << ", "; 10267 outs() << "per-thread"; 10268 NeedsComma = true; 10269 } 10270 if (Abs) { 10271 if (NeedsComma) 10272 outs() << ", "; 10273 outs() << "absolute"; 10274 NeedsComma = true; 10275 } 10276 if (Resolver) { 10277 if (NeedsComma) 10278 outs() << ", "; 10279 outs() << format("resolver=0x%08llX", Entry.other()); 10280 NeedsComma = true; 10281 } 10282 outs() << "]"; 10283 } 10284 if (ReExport) { 10285 StringRef DylibName = "unknown"; 10286 int Ordinal = Entry.other() - 1; 10287 Obj->getLibraryShortNameByIndex(Ordinal, DylibName); 10288 if (Entry.otherName().empty()) 10289 outs() << " (from " << DylibName << ")"; 10290 else 10291 outs() << " (" << Entry.otherName() << " from " << DylibName << ")"; 10292 } 10293 outs() << "\n"; 10294 } 10295 if (Err) 10296 reportError(std::move(Err), Obj->getFileName()); 10297 } 10298 10299 //===----------------------------------------------------------------------===// 10300 // rebase table dumping 10301 //===----------------------------------------------------------------------===// 10302 10303 void printMachORebaseTable(object::MachOObjectFile *Obj) { 10304 outs() << "segment section address type\n"; 10305 Error Err = Error::success(); 10306 for (const object::MachORebaseEntry &Entry : Obj->rebaseTable(Err)) { 10307 StringRef SegmentName = Entry.segmentName(); 10308 StringRef SectionName = Entry.sectionName(); 10309 uint64_t Address = Entry.address(); 10310 10311 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer 10312 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n", 10313 SegmentName.str().c_str(), SectionName.str().c_str(), 10314 Address, Entry.typeName().str().c_str()); 10315 } 10316 if (Err) 10317 reportError(std::move(Err), Obj->getFileName()); 10318 } 10319 10320 static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) { 10321 StringRef DylibName; 10322 switch (Ordinal) { 10323 case MachO::BIND_SPECIAL_DYLIB_SELF: 10324 return "this-image"; 10325 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE: 10326 return "main-executable"; 10327 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP: 10328 return "flat-namespace"; 10329 default: 10330 if (Ordinal > 0) { 10331 std::error_code EC = 10332 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName); 10333 if (EC) 10334 return "<<bad library ordinal>>"; 10335 return DylibName; 10336 } 10337 } 10338 return "<<unknown special ordinal>>"; 10339 } 10340 10341 //===----------------------------------------------------------------------===// 10342 // bind table dumping 10343 //===----------------------------------------------------------------------===// 10344 10345 void printMachOBindTable(object::MachOObjectFile *Obj) { 10346 // Build table of sections so names can used in final output. 10347 outs() << "segment section address type " 10348 "addend dylib symbol\n"; 10349 Error Err = Error::success(); 10350 for (const object::MachOBindEntry &Entry : Obj->bindTable(Err)) { 10351 StringRef SegmentName = Entry.segmentName(); 10352 StringRef SectionName = Entry.sectionName(); 10353 uint64_t Address = Entry.address(); 10354 10355 // Table lines look like: 10356 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard 10357 StringRef Attr; 10358 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT) 10359 Attr = " (weak_import)"; 10360 outs() << left_justify(SegmentName, 8) << " " 10361 << left_justify(SectionName, 18) << " " 10362 << format_hex(Address, 10, true) << " " 10363 << left_justify(Entry.typeName(), 8) << " " 10364 << format_decimal(Entry.addend(), 8) << " " 10365 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " " 10366 << Entry.symbolName() << Attr << "\n"; 10367 } 10368 if (Err) 10369 reportError(std::move(Err), Obj->getFileName()); 10370 } 10371 10372 //===----------------------------------------------------------------------===// 10373 // lazy bind table dumping 10374 //===----------------------------------------------------------------------===// 10375 10376 void printMachOLazyBindTable(object::MachOObjectFile *Obj) { 10377 outs() << "segment section address " 10378 "dylib symbol\n"; 10379 Error Err = Error::success(); 10380 for (const object::MachOBindEntry &Entry : Obj->lazyBindTable(Err)) { 10381 StringRef SegmentName = Entry.segmentName(); 10382 StringRef SectionName = Entry.sectionName(); 10383 uint64_t Address = Entry.address(); 10384 10385 // Table lines look like: 10386 // __DATA __got 0x00012010 libSystem ___stack_chk_guard 10387 outs() << left_justify(SegmentName, 8) << " " 10388 << left_justify(SectionName, 18) << " " 10389 << format_hex(Address, 10, true) << " " 10390 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " " 10391 << Entry.symbolName() << "\n"; 10392 } 10393 if (Err) 10394 reportError(std::move(Err), Obj->getFileName()); 10395 } 10396 10397 //===----------------------------------------------------------------------===// 10398 // weak bind table dumping 10399 //===----------------------------------------------------------------------===// 10400 10401 void printMachOWeakBindTable(object::MachOObjectFile *Obj) { 10402 outs() << "segment section address " 10403 "type addend symbol\n"; 10404 Error Err = Error::success(); 10405 for (const object::MachOBindEntry &Entry : Obj->weakBindTable(Err)) { 10406 // Strong symbols don't have a location to update. 10407 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) { 10408 outs() << " strong " 10409 << Entry.symbolName() << "\n"; 10410 continue; 10411 } 10412 StringRef SegmentName = Entry.segmentName(); 10413 StringRef SectionName = Entry.sectionName(); 10414 uint64_t Address = Entry.address(); 10415 10416 // Table lines look like: 10417 // __DATA __data 0x00001000 pointer 0 _foo 10418 outs() << left_justify(SegmentName, 8) << " " 10419 << left_justify(SectionName, 18) << " " 10420 << format_hex(Address, 10, true) << " " 10421 << left_justify(Entry.typeName(), 8) << " " 10422 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName() 10423 << "\n"; 10424 } 10425 if (Err) 10426 reportError(std::move(Err), Obj->getFileName()); 10427 } 10428 10429 // get_dyld_bind_info_symbolname() is used for disassembly and passed an 10430 // address, ReferenceValue, in the Mach-O file and looks in the dyld bind 10431 // information for that address. If the address is found its binding symbol 10432 // name is returned. If not nullptr is returned. 10433 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue, 10434 struct DisassembleInfo *info) { 10435 if (info->bindtable == nullptr) { 10436 info->bindtable = std::make_unique<SymbolAddressMap>(); 10437 Error Err = Error::success(); 10438 for (const object::MachOBindEntry &Entry : info->O->bindTable(Err)) { 10439 uint64_t Address = Entry.address(); 10440 StringRef name = Entry.symbolName(); 10441 if (!name.empty()) 10442 (*info->bindtable)[Address] = name; 10443 } 10444 if (Err) 10445 reportError(std::move(Err), info->O->getFileName()); 10446 } 10447 auto name = info->bindtable->lookup(ReferenceValue); 10448 return !name.empty() ? name.data() : nullptr; 10449 } 10450 10451 void printLazyBindTable(ObjectFile *o) { 10452 outs() << "Lazy bind table:\n"; 10453 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 10454 printMachOLazyBindTable(MachO); 10455 else 10456 WithColor::error() 10457 << "This operation is only currently supported " 10458 "for Mach-O executable files.\n"; 10459 } 10460 10461 void printWeakBindTable(ObjectFile *o) { 10462 outs() << "Weak bind table:\n"; 10463 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 10464 printMachOWeakBindTable(MachO); 10465 else 10466 WithColor::error() 10467 << "This operation is only currently supported " 10468 "for Mach-O executable files.\n"; 10469 } 10470 10471 void printExportsTrie(const ObjectFile *o) { 10472 outs() << "Exports trie:\n"; 10473 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 10474 printMachOExportsTrie(MachO); 10475 else 10476 WithColor::error() 10477 << "This operation is only currently supported " 10478 "for Mach-O executable files.\n"; 10479 } 10480 10481 void printRebaseTable(ObjectFile *o) { 10482 outs() << "Rebase table:\n"; 10483 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 10484 printMachORebaseTable(MachO); 10485 else 10486 WithColor::error() 10487 << "This operation is only currently supported " 10488 "for Mach-O executable files.\n"; 10489 } 10490 10491 void printBindTable(ObjectFile *o) { 10492 outs() << "Bind table:\n"; 10493 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 10494 printMachOBindTable(MachO); 10495 else 10496 WithColor::error() 10497 << "This operation is only currently supported " 10498 "for Mach-O executable files.\n"; 10499 } 10500 } // namespace llvm 10501