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 "MachODump.h" 14 15 #include "llvm-objdump.h" 16 #include "llvm-c/Disassembler.h" 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/ADT/StringExtras.h" 19 #include "llvm/ADT/Triple.h" 20 #include "llvm/BinaryFormat/MachO.h" 21 #include "llvm/Config/config.h" 22 #include "llvm/DebugInfo/DIContext.h" 23 #include "llvm/DebugInfo/DWARF/DWARFContext.h" 24 #include "llvm/Demangle/Demangle.h" 25 #include "llvm/MC/MCAsmInfo.h" 26 #include "llvm/MC/MCContext.h" 27 #include "llvm/MC/MCDisassembler/MCDisassembler.h" 28 #include "llvm/MC/MCInst.h" 29 #include "llvm/MC/MCInstPrinter.h" 30 #include "llvm/MC/MCInstrDesc.h" 31 #include "llvm/MC/MCInstrInfo.h" 32 #include "llvm/MC/MCRegisterInfo.h" 33 #include "llvm/MC/MCSubtargetInfo.h" 34 #include "llvm/MC/MCTargetOptions.h" 35 #include "llvm/Object/MachO.h" 36 #include "llvm/Object/MachOUniversal.h" 37 #include "llvm/Support/Casting.h" 38 #include "llvm/Support/CommandLine.h" 39 #include "llvm/Support/Debug.h" 40 #include "llvm/Support/Endian.h" 41 #include "llvm/Support/Format.h" 42 #include "llvm/Support/FormattedStream.h" 43 #include "llvm/Support/GraphWriter.h" 44 #include "llvm/Support/LEB128.h" 45 #include "llvm/Support/MemoryBuffer.h" 46 #include "llvm/Support/TargetRegistry.h" 47 #include "llvm/Support/TargetSelect.h" 48 #include "llvm/Support/ToolOutputFile.h" 49 #include "llvm/Support/WithColor.h" 50 #include "llvm/Support/raw_ostream.h" 51 #include <algorithm> 52 #include <cstring> 53 #include <system_error> 54 55 #ifdef HAVE_LIBXAR 56 extern "C" { 57 #include <xar/xar.h> 58 } 59 #endif 60 61 using namespace llvm; 62 using namespace llvm::object; 63 using namespace llvm::objdump; 64 65 cl::OptionCategory objdump::MachOCat("llvm-objdump MachO Specific Options"); 66 67 cl::opt<bool> objdump::FirstPrivateHeader( 68 "private-header", 69 cl::desc("Display only the first format specific file header"), 70 cl::cat(MachOCat)); 71 72 cl::opt<bool> objdump::ExportsTrie("exports-trie", 73 cl::desc("Display mach-o exported symbols"), 74 cl::cat(MachOCat)); 75 76 cl::opt<bool> objdump::Rebase("rebase", 77 cl::desc("Display mach-o rebasing info"), 78 cl::cat(MachOCat)); 79 80 cl::opt<bool> objdump::Bind("bind", cl::desc("Display mach-o binding info"), 81 cl::cat(MachOCat)); 82 83 cl::opt<bool> objdump::LazyBind("lazy-bind", 84 cl::desc("Display mach-o lazy binding info"), 85 cl::cat(MachOCat)); 86 87 cl::opt<bool> objdump::WeakBind("weak-bind", 88 cl::desc("Display mach-o weak binding info"), 89 cl::cat(MachOCat)); 90 91 static cl::opt<bool> 92 UseDbg("g", cl::Grouping, 93 cl::desc("Print line information from debug info if available"), 94 cl::cat(MachOCat)); 95 96 static cl::opt<std::string> DSYMFile("dsym", 97 cl::desc("Use .dSYM file for debug info"), 98 cl::cat(MachOCat)); 99 100 static cl::opt<bool> FullLeadingAddr("full-leading-addr", 101 cl::desc("Print full leading address"), 102 cl::cat(MachOCat)); 103 104 static cl::opt<bool> NoLeadingHeaders("no-leading-headers", 105 cl::desc("Print no leading headers"), 106 cl::cat(MachOCat)); 107 108 cl::opt<bool> objdump::UniversalHeaders( 109 "universal-headers", 110 cl::desc("Print Mach-O universal headers (requires --macho)"), 111 cl::cat(MachOCat)); 112 113 static cl::opt<bool> ArchiveMemberOffsets( 114 "archive-member-offsets", 115 cl::desc("Print the offset to each archive member for Mach-O archives " 116 "(requires --macho and --archive-headers)"), 117 cl::cat(MachOCat)); 118 119 cl::opt<bool> objdump::IndirectSymbols( 120 "indirect-symbols", 121 cl::desc( 122 "Print indirect symbol table for Mach-O objects (requires --macho)"), 123 cl::cat(MachOCat)); 124 125 cl::opt<bool> objdump::DataInCode( 126 "data-in-code", 127 cl::desc( 128 "Print the data in code table for Mach-O objects (requires --macho)"), 129 cl::cat(MachOCat)); 130 131 cl::opt<bool> 132 objdump::LinkOptHints("link-opt-hints", 133 cl::desc("Print the linker optimization hints for " 134 "Mach-O objects (requires --macho)"), 135 cl::cat(MachOCat)); 136 137 cl::opt<bool> 138 objdump::InfoPlist("info-plist", 139 cl::desc("Print the info plist section as strings for " 140 "Mach-O objects (requires --macho)"), 141 cl::cat(MachOCat)); 142 143 cl::opt<bool> 144 objdump::DylibsUsed("dylibs-used", 145 cl::desc("Print the shared libraries used for linked " 146 "Mach-O files (requires --macho)"), 147 cl::cat(MachOCat)); 148 149 cl::opt<bool> objdump::DylibId("dylib-id", 150 cl::desc("Print the shared library's id for the " 151 "dylib Mach-O file (requires --macho)"), 152 cl::cat(MachOCat)); 153 154 static cl::opt<bool> 155 NonVerbose("non-verbose", 156 cl::desc("Print the info for Mach-O objects in non-verbose or " 157 "numeric form (requires --macho)"), 158 cl::cat(MachOCat)); 159 160 cl::opt<bool> 161 objdump::ObjcMetaData("objc-meta-data", 162 cl::desc("Print the Objective-C runtime meta data " 163 "for Mach-O files (requires --macho)"), 164 cl::cat(MachOCat)); 165 166 static cl::opt<std::string> DisSymName( 167 "dis-symname", 168 cl::desc("disassemble just this symbol's instructions (requires --macho)"), 169 cl::cat(MachOCat)); 170 171 static cl::opt<bool> NoSymbolicOperands( 172 "no-symbolic-operands", 173 cl::desc("do not symbolic operands when disassembling (requires --macho)"), 174 cl::cat(MachOCat)); 175 176 static cl::list<std::string> 177 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"), 178 cl::ZeroOrMore, cl::cat(MachOCat)); 179 180 static bool ArchAll = false; 181 182 static std::string ThumbTripleName; 183 184 static const Target *GetTarget(const MachOObjectFile *MachOObj, 185 const char **McpuDefault, 186 const Target **ThumbTarget) { 187 // Figure out the target triple. 188 Triple TT(TripleName); 189 if (TripleName.empty()) { 190 TT = MachOObj->getArchTriple(McpuDefault); 191 TripleName = TT.str(); 192 } 193 194 if (TT.getArch() == Triple::arm) { 195 // We've inferred a 32-bit ARM target from the object file. All MachO CPUs 196 // that support ARM are also capable of Thumb mode. 197 Triple ThumbTriple = TT; 198 std::string ThumbName = (Twine("thumb") + TT.getArchName().substr(3)).str(); 199 ThumbTriple.setArchName(ThumbName); 200 ThumbTripleName = ThumbTriple.str(); 201 } 202 203 // Get the target specific parser. 204 std::string Error; 205 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); 206 if (TheTarget && ThumbTripleName.empty()) 207 return TheTarget; 208 209 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error); 210 if (*ThumbTarget) 211 return TheTarget; 212 213 WithColor::error(errs(), "llvm-objdump") << "unable to get target for '"; 214 if (!TheTarget) 215 errs() << TripleName; 216 else 217 errs() << ThumbTripleName; 218 errs() << "', see --version and --triple.\n"; 219 return nullptr; 220 } 221 222 namespace { 223 struct SymbolSorter { 224 bool operator()(const SymbolRef &A, const SymbolRef &B) { 225 Expected<SymbolRef::Type> ATypeOrErr = A.getType(); 226 if (!ATypeOrErr) 227 reportError(ATypeOrErr.takeError(), A.getObject()->getFileName()); 228 SymbolRef::Type AType = *ATypeOrErr; 229 Expected<SymbolRef::Type> BTypeOrErr = B.getType(); 230 if (!BTypeOrErr) 231 reportError(BTypeOrErr.takeError(), B.getObject()->getFileName()); 232 SymbolRef::Type BType = *BTypeOrErr; 233 uint64_t AAddr = 234 (AType != SymbolRef::ST_Function) ? 0 : cantFail(A.getValue()); 235 uint64_t BAddr = 236 (BType != SymbolRef::ST_Function) ? 0 : cantFail(B.getValue()); 237 return AAddr < BAddr; 238 } 239 }; 240 } // namespace 241 242 // Types for the storted data in code table that is built before disassembly 243 // and the predicate function to sort them. 244 typedef std::pair<uint64_t, DiceRef> DiceTableEntry; 245 typedef std::vector<DiceTableEntry> DiceTable; 246 typedef DiceTable::iterator dice_table_iterator; 247 248 #ifdef HAVE_LIBXAR 249 namespace { 250 struct ScopedXarFile { 251 xar_t xar; 252 ScopedXarFile(const char *filename, int32_t flags) 253 : xar(xar_open(filename, flags)) {} 254 ~ScopedXarFile() { 255 if (xar) 256 xar_close(xar); 257 } 258 ScopedXarFile(const ScopedXarFile &) = delete; 259 ScopedXarFile &operator=(const ScopedXarFile &) = delete; 260 operator xar_t() { return xar; } 261 }; 262 263 struct ScopedXarIter { 264 xar_iter_t iter; 265 ScopedXarIter() : iter(xar_iter_new()) {} 266 ~ScopedXarIter() { 267 if (iter) 268 xar_iter_free(iter); 269 } 270 ScopedXarIter(const ScopedXarIter &) = delete; 271 ScopedXarIter &operator=(const ScopedXarIter &) = delete; 272 operator xar_iter_t() { return iter; } 273 }; 274 } // namespace 275 #endif // defined(HAVE_LIBXAR) 276 277 // This is used to search for a data in code table entry for the PC being 278 // disassembled. The j parameter has the PC in j.first. A single data in code 279 // table entry can cover many bytes for each of its Kind's. So if the offset, 280 // aka the i.first value, of the data in code table entry plus its Length 281 // covers the PC being searched for this will return true. If not it will 282 // return false. 283 static bool compareDiceTableEntries(const DiceTableEntry &i, 284 const DiceTableEntry &j) { 285 uint16_t Length; 286 i.second.getLength(Length); 287 288 return j.first >= i.first && j.first < i.first + Length; 289 } 290 291 static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length, 292 unsigned short Kind) { 293 uint32_t Value, Size = 1; 294 295 switch (Kind) { 296 default: 297 case MachO::DICE_KIND_DATA: 298 if (Length >= 4) { 299 if (!NoShowRawInsn) 300 dumpBytes(makeArrayRef(bytes, 4), outs()); 301 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0]; 302 outs() << "\t.long " << Value; 303 Size = 4; 304 } else if (Length >= 2) { 305 if (!NoShowRawInsn) 306 dumpBytes(makeArrayRef(bytes, 2), outs()); 307 Value = bytes[1] << 8 | bytes[0]; 308 outs() << "\t.short " << Value; 309 Size = 2; 310 } else { 311 if (!NoShowRawInsn) 312 dumpBytes(makeArrayRef(bytes, 2), outs()); 313 Value = bytes[0]; 314 outs() << "\t.byte " << Value; 315 Size = 1; 316 } 317 if (Kind == MachO::DICE_KIND_DATA) 318 outs() << "\t@ KIND_DATA\n"; 319 else 320 outs() << "\t@ data in code kind = " << Kind << "\n"; 321 break; 322 case MachO::DICE_KIND_JUMP_TABLE8: 323 if (!NoShowRawInsn) 324 dumpBytes(makeArrayRef(bytes, 1), outs()); 325 Value = bytes[0]; 326 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n"; 327 Size = 1; 328 break; 329 case MachO::DICE_KIND_JUMP_TABLE16: 330 if (!NoShowRawInsn) 331 dumpBytes(makeArrayRef(bytes, 2), outs()); 332 Value = bytes[1] << 8 | bytes[0]; 333 outs() << "\t.short " << format("%5u", Value & 0xffff) 334 << "\t@ KIND_JUMP_TABLE16\n"; 335 Size = 2; 336 break; 337 case MachO::DICE_KIND_JUMP_TABLE32: 338 case MachO::DICE_KIND_ABS_JUMP_TABLE32: 339 if (!NoShowRawInsn) 340 dumpBytes(makeArrayRef(bytes, 4), outs()); 341 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0]; 342 outs() << "\t.long " << Value; 343 if (Kind == MachO::DICE_KIND_JUMP_TABLE32) 344 outs() << "\t@ KIND_JUMP_TABLE32\n"; 345 else 346 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n"; 347 Size = 4; 348 break; 349 } 350 return Size; 351 } 352 353 static void getSectionsAndSymbols(MachOObjectFile *MachOObj, 354 std::vector<SectionRef> &Sections, 355 std::vector<SymbolRef> &Symbols, 356 SmallVectorImpl<uint64_t> &FoundFns, 357 uint64_t &BaseSegmentAddress) { 358 const StringRef FileName = MachOObj->getFileName(); 359 for (const SymbolRef &Symbol : MachOObj->symbols()) { 360 StringRef SymName = unwrapOrError(Symbol.getName(), FileName); 361 if (!SymName.startswith("ltmp")) 362 Symbols.push_back(Symbol); 363 } 364 365 for (const SectionRef &Section : MachOObj->sections()) 366 Sections.push_back(Section); 367 368 bool BaseSegmentAddressSet = false; 369 for (const auto &Command : MachOObj->load_commands()) { 370 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) { 371 // We found a function starts segment, parse the addresses for later 372 // consumption. 373 MachO::linkedit_data_command LLC = 374 MachOObj->getLinkeditDataLoadCommand(Command); 375 376 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns); 377 } else if (Command.C.cmd == MachO::LC_SEGMENT) { 378 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command); 379 StringRef SegName = SLC.segname; 380 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") { 381 BaseSegmentAddressSet = true; 382 BaseSegmentAddress = SLC.vmaddr; 383 } 384 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) { 385 MachO::segment_command_64 SLC = MachOObj->getSegment64LoadCommand(Command); 386 StringRef SegName = SLC.segname; 387 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") { 388 BaseSegmentAddressSet = true; 389 BaseSegmentAddress = SLC.vmaddr; 390 } 391 } 392 } 393 } 394 395 static bool DumpAndSkipDataInCode(uint64_t PC, const uint8_t *bytes, 396 DiceTable &Dices, uint64_t &InstSize) { 397 // Check the data in code table here to see if this is data not an 398 // instruction to be disassembled. 399 DiceTable Dice; 400 Dice.push_back(std::make_pair(PC, DiceRef())); 401 dice_table_iterator DTI = 402 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(), 403 compareDiceTableEntries); 404 if (DTI != Dices.end()) { 405 uint16_t Length; 406 DTI->second.getLength(Length); 407 uint16_t Kind; 408 DTI->second.getKind(Kind); 409 InstSize = DumpDataInCode(bytes, Length, Kind); 410 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) && 411 (PC == (DTI->first + Length - 1)) && (Length & 1)) 412 InstSize++; 413 return true; 414 } 415 return false; 416 } 417 418 static void printRelocationTargetName(const MachOObjectFile *O, 419 const MachO::any_relocation_info &RE, 420 raw_string_ostream &Fmt) { 421 // Target of a scattered relocation is an address. In the interest of 422 // generating pretty output, scan through the symbol table looking for a 423 // symbol that aligns with that address. If we find one, print it. 424 // Otherwise, we just print the hex address of the target. 425 const StringRef FileName = O->getFileName(); 426 if (O->isRelocationScattered(RE)) { 427 uint32_t Val = O->getPlainRelocationSymbolNum(RE); 428 429 for (const SymbolRef &Symbol : O->symbols()) { 430 uint64_t Addr = unwrapOrError(Symbol.getAddress(), FileName); 431 if (Addr != Val) 432 continue; 433 Fmt << unwrapOrError(Symbol.getName(), FileName); 434 return; 435 } 436 437 // If we couldn't find a symbol that this relocation refers to, try 438 // to find a section beginning instead. 439 for (const SectionRef &Section : ToolSectionFilter(*O)) { 440 uint64_t Addr = Section.getAddress(); 441 if (Addr != Val) 442 continue; 443 StringRef NameOrErr = unwrapOrError(Section.getName(), O->getFileName()); 444 Fmt << NameOrErr; 445 return; 446 } 447 448 Fmt << format("0x%x", Val); 449 return; 450 } 451 452 StringRef S; 453 bool isExtern = O->getPlainRelocationExternal(RE); 454 uint64_t Val = O->getPlainRelocationSymbolNum(RE); 455 456 if (O->getAnyRelocationType(RE) == MachO::ARM64_RELOC_ADDEND && 457 (O->getArch() == Triple::aarch64 || O->getArch() == Triple::aarch64_be)) { 458 Fmt << format("0x%0" PRIx64, Val); 459 return; 460 } 461 462 if (isExtern) { 463 symbol_iterator SI = O->symbol_begin(); 464 advance(SI, Val); 465 S = unwrapOrError(SI->getName(), FileName); 466 } else { 467 section_iterator SI = O->section_begin(); 468 // Adjust for the fact that sections are 1-indexed. 469 if (Val == 0) { 470 Fmt << "0 (?,?)"; 471 return; 472 } 473 uint32_t I = Val - 1; 474 while (I != 0 && SI != O->section_end()) { 475 --I; 476 advance(SI, 1); 477 } 478 if (SI == O->section_end()) { 479 Fmt << Val << " (?,?)"; 480 } else { 481 if (Expected<StringRef> NameOrErr = SI->getName()) 482 S = *NameOrErr; 483 else 484 consumeError(NameOrErr.takeError()); 485 } 486 } 487 488 Fmt << S; 489 } 490 491 Error objdump::getMachORelocationValueString(const MachOObjectFile *Obj, 492 const RelocationRef &RelRef, 493 SmallVectorImpl<char> &Result) { 494 DataRefImpl Rel = RelRef.getRawDataRefImpl(); 495 MachO::any_relocation_info RE = Obj->getRelocation(Rel); 496 497 unsigned Arch = Obj->getArch(); 498 499 std::string FmtBuf; 500 raw_string_ostream Fmt(FmtBuf); 501 unsigned Type = Obj->getAnyRelocationType(RE); 502 bool IsPCRel = Obj->getAnyRelocationPCRel(RE); 503 504 // Determine any addends that should be displayed with the relocation. 505 // These require decoding the relocation type, which is triple-specific. 506 507 // X86_64 has entirely custom relocation types. 508 if (Arch == Triple::x86_64) { 509 switch (Type) { 510 case MachO::X86_64_RELOC_GOT_LOAD: 511 case MachO::X86_64_RELOC_GOT: { 512 printRelocationTargetName(Obj, RE, Fmt); 513 Fmt << "@GOT"; 514 if (IsPCRel) 515 Fmt << "PCREL"; 516 break; 517 } 518 case MachO::X86_64_RELOC_SUBTRACTOR: { 519 DataRefImpl RelNext = Rel; 520 Obj->moveRelocationNext(RelNext); 521 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext); 522 523 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type 524 // X86_64_RELOC_UNSIGNED. 525 // NOTE: Scattered relocations don't exist on x86_64. 526 unsigned RType = Obj->getAnyRelocationType(RENext); 527 if (RType != MachO::X86_64_RELOC_UNSIGNED) 528 reportError(Obj->getFileName(), "Expected X86_64_RELOC_UNSIGNED after " 529 "X86_64_RELOC_SUBTRACTOR."); 530 531 // The X86_64_RELOC_UNSIGNED contains the minuend symbol; 532 // X86_64_RELOC_SUBTRACTOR contains the subtrahend. 533 printRelocationTargetName(Obj, RENext, Fmt); 534 Fmt << "-"; 535 printRelocationTargetName(Obj, RE, Fmt); 536 break; 537 } 538 case MachO::X86_64_RELOC_TLV: 539 printRelocationTargetName(Obj, RE, Fmt); 540 Fmt << "@TLV"; 541 if (IsPCRel) 542 Fmt << "P"; 543 break; 544 case MachO::X86_64_RELOC_SIGNED_1: 545 printRelocationTargetName(Obj, RE, Fmt); 546 Fmt << "-1"; 547 break; 548 case MachO::X86_64_RELOC_SIGNED_2: 549 printRelocationTargetName(Obj, RE, Fmt); 550 Fmt << "-2"; 551 break; 552 case MachO::X86_64_RELOC_SIGNED_4: 553 printRelocationTargetName(Obj, RE, Fmt); 554 Fmt << "-4"; 555 break; 556 default: 557 printRelocationTargetName(Obj, RE, Fmt); 558 break; 559 } 560 // X86 and ARM share some relocation types in common. 561 } else if (Arch == Triple::x86 || Arch == Triple::arm || 562 Arch == Triple::ppc) { 563 // Generic relocation types... 564 switch (Type) { 565 case MachO::GENERIC_RELOC_PAIR: // prints no info 566 return Error::success(); 567 case MachO::GENERIC_RELOC_SECTDIFF: { 568 DataRefImpl RelNext = Rel; 569 Obj->moveRelocationNext(RelNext); 570 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext); 571 572 // X86 sect diff's must be followed by a relocation of type 573 // GENERIC_RELOC_PAIR. 574 unsigned RType = Obj->getAnyRelocationType(RENext); 575 576 if (RType != MachO::GENERIC_RELOC_PAIR) 577 reportError(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after " 578 "GENERIC_RELOC_SECTDIFF."); 579 580 printRelocationTargetName(Obj, RE, Fmt); 581 Fmt << "-"; 582 printRelocationTargetName(Obj, RENext, Fmt); 583 break; 584 } 585 } 586 587 if (Arch == Triple::x86 || Arch == Triple::ppc) { 588 switch (Type) { 589 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: { 590 DataRefImpl RelNext = Rel; 591 Obj->moveRelocationNext(RelNext); 592 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext); 593 594 // X86 sect diff's must be followed by a relocation of type 595 // GENERIC_RELOC_PAIR. 596 unsigned RType = Obj->getAnyRelocationType(RENext); 597 if (RType != MachO::GENERIC_RELOC_PAIR) 598 reportError(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after " 599 "GENERIC_RELOC_LOCAL_SECTDIFF."); 600 601 printRelocationTargetName(Obj, RE, Fmt); 602 Fmt << "-"; 603 printRelocationTargetName(Obj, RENext, Fmt); 604 break; 605 } 606 case MachO::GENERIC_RELOC_TLV: { 607 printRelocationTargetName(Obj, RE, Fmt); 608 Fmt << "@TLV"; 609 if (IsPCRel) 610 Fmt << "P"; 611 break; 612 } 613 default: 614 printRelocationTargetName(Obj, RE, Fmt); 615 } 616 } else { // ARM-specific relocations 617 switch (Type) { 618 case MachO::ARM_RELOC_HALF: 619 case MachO::ARM_RELOC_HALF_SECTDIFF: { 620 // Half relocations steal a bit from the length field to encode 621 // whether this is an upper16 or a lower16 relocation. 622 bool isUpper = (Obj->getAnyRelocationLength(RE) & 0x1) == 1; 623 624 if (isUpper) 625 Fmt << ":upper16:("; 626 else 627 Fmt << ":lower16:("; 628 printRelocationTargetName(Obj, RE, Fmt); 629 630 DataRefImpl RelNext = Rel; 631 Obj->moveRelocationNext(RelNext); 632 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext); 633 634 // ARM half relocs must be followed by a relocation of type 635 // ARM_RELOC_PAIR. 636 unsigned RType = Obj->getAnyRelocationType(RENext); 637 if (RType != MachO::ARM_RELOC_PAIR) 638 reportError(Obj->getFileName(), "Expected ARM_RELOC_PAIR after " 639 "ARM_RELOC_HALF"); 640 641 // NOTE: The half of the target virtual address is stashed in the 642 // address field of the secondary relocation, but we can't reverse 643 // engineer the constant offset from it without decoding the movw/movt 644 // instruction to find the other half in its immediate field. 645 646 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the 647 // symbol/section pointer of the follow-on relocation. 648 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) { 649 Fmt << "-"; 650 printRelocationTargetName(Obj, RENext, Fmt); 651 } 652 653 Fmt << ")"; 654 break; 655 } 656 default: { 657 printRelocationTargetName(Obj, RE, Fmt); 658 } 659 } 660 } 661 } else 662 printRelocationTargetName(Obj, RE, Fmt); 663 664 Fmt.flush(); 665 Result.append(FmtBuf.begin(), FmtBuf.end()); 666 return Error::success(); 667 } 668 669 static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose, 670 uint32_t n, uint32_t count, 671 uint32_t stride, uint64_t addr) { 672 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand(); 673 uint32_t nindirectsyms = Dysymtab.nindirectsyms; 674 if (n > nindirectsyms) 675 outs() << " (entries start past the end of the indirect symbol " 676 "table) (reserved1 field greater than the table size)"; 677 else if (n + count > nindirectsyms) 678 outs() << " (entries extends past the end of the indirect symbol " 679 "table)"; 680 outs() << "\n"; 681 uint32_t cputype = O->getHeader().cputype; 682 if (cputype & MachO::CPU_ARCH_ABI64) 683 outs() << "address index"; 684 else 685 outs() << "address index"; 686 if (verbose) 687 outs() << " name\n"; 688 else 689 outs() << "\n"; 690 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) { 691 if (cputype & MachO::CPU_ARCH_ABI64) 692 outs() << format("0x%016" PRIx64, addr + j * stride) << " "; 693 else 694 outs() << format("0x%08" PRIx32, (uint32_t)addr + j * stride) << " "; 695 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand(); 696 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j); 697 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) { 698 outs() << "LOCAL\n"; 699 continue; 700 } 701 if (indirect_symbol == 702 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) { 703 outs() << "LOCAL ABSOLUTE\n"; 704 continue; 705 } 706 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) { 707 outs() << "ABSOLUTE\n"; 708 continue; 709 } 710 outs() << format("%5u ", indirect_symbol); 711 if (verbose) { 712 MachO::symtab_command Symtab = O->getSymtabLoadCommand(); 713 if (indirect_symbol < Symtab.nsyms) { 714 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol); 715 SymbolRef Symbol = *Sym; 716 outs() << unwrapOrError(Symbol.getName(), O->getFileName()); 717 } else { 718 outs() << "?"; 719 } 720 } 721 outs() << "\n"; 722 } 723 } 724 725 static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) { 726 for (const auto &Load : O->load_commands()) { 727 if (Load.C.cmd == MachO::LC_SEGMENT_64) { 728 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load); 729 for (unsigned J = 0; J < Seg.nsects; ++J) { 730 MachO::section_64 Sec = O->getSection64(Load, J); 731 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE; 732 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS || 733 section_type == MachO::S_LAZY_SYMBOL_POINTERS || 734 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS || 735 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS || 736 section_type == MachO::S_SYMBOL_STUBS) { 737 uint32_t stride; 738 if (section_type == MachO::S_SYMBOL_STUBS) 739 stride = Sec.reserved2; 740 else 741 stride = 8; 742 if (stride == 0) { 743 outs() << "Can't print indirect symbols for (" << Sec.segname << "," 744 << Sec.sectname << ") " 745 << "(size of stubs in reserved2 field is zero)\n"; 746 continue; 747 } 748 uint32_t count = Sec.size / stride; 749 outs() << "Indirect symbols for (" << Sec.segname << "," 750 << Sec.sectname << ") " << count << " entries"; 751 uint32_t n = Sec.reserved1; 752 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr); 753 } 754 } 755 } else if (Load.C.cmd == MachO::LC_SEGMENT) { 756 MachO::segment_command Seg = O->getSegmentLoadCommand(Load); 757 for (unsigned J = 0; J < Seg.nsects; ++J) { 758 MachO::section Sec = O->getSection(Load, J); 759 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE; 760 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS || 761 section_type == MachO::S_LAZY_SYMBOL_POINTERS || 762 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS || 763 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS || 764 section_type == MachO::S_SYMBOL_STUBS) { 765 uint32_t stride; 766 if (section_type == MachO::S_SYMBOL_STUBS) 767 stride = Sec.reserved2; 768 else 769 stride = 4; 770 if (stride == 0) { 771 outs() << "Can't print indirect symbols for (" << Sec.segname << "," 772 << Sec.sectname << ") " 773 << "(size of stubs in reserved2 field is zero)\n"; 774 continue; 775 } 776 uint32_t count = Sec.size / stride; 777 outs() << "Indirect symbols for (" << Sec.segname << "," 778 << Sec.sectname << ") " << count << " entries"; 779 uint32_t n = Sec.reserved1; 780 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr); 781 } 782 } 783 } 784 } 785 } 786 787 static void PrintRType(const uint64_t cputype, const unsigned r_type) { 788 static char const *generic_r_types[] = { 789 "VANILLA ", "PAIR ", "SECTDIF ", "PBLAPTR ", "LOCSDIF ", "TLV ", 790 " 6 (?) ", " 7 (?) ", " 8 (?) ", " 9 (?) ", " 10 (?) ", " 11 (?) ", 791 " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) " 792 }; 793 static char const *x86_64_r_types[] = { 794 "UNSIGND ", "SIGNED ", "BRANCH ", "GOT_LD ", "GOT ", "SUB ", 795 "SIGNED1 ", "SIGNED2 ", "SIGNED4 ", "TLV ", " 10 (?) ", " 11 (?) ", 796 " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) " 797 }; 798 static char const *arm_r_types[] = { 799 "VANILLA ", "PAIR ", "SECTDIFF", "LOCSDIF ", "PBLAPTR ", 800 "BR24 ", "T_BR22 ", "T_BR32 ", "HALF ", "HALFDIF ", 801 " 10 (?) ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) " 802 }; 803 static char const *arm64_r_types[] = { 804 "UNSIGND ", "SUB ", "BR26 ", "PAGE21 ", "PAGOF12 ", 805 "GOTLDP ", "GOTLDPOF", "PTRTGOT ", "TLVLDP ", "TLVLDPOF", 806 "ADDEND ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) " 807 }; 808 809 if (r_type > 0xf){ 810 outs() << format("%-7u", r_type) << " "; 811 return; 812 } 813 switch (cputype) { 814 case MachO::CPU_TYPE_I386: 815 outs() << generic_r_types[r_type]; 816 break; 817 case MachO::CPU_TYPE_X86_64: 818 outs() << x86_64_r_types[r_type]; 819 break; 820 case MachO::CPU_TYPE_ARM: 821 outs() << arm_r_types[r_type]; 822 break; 823 case MachO::CPU_TYPE_ARM64: 824 case MachO::CPU_TYPE_ARM64_32: 825 outs() << arm64_r_types[r_type]; 826 break; 827 default: 828 outs() << format("%-7u ", r_type); 829 } 830 } 831 832 static void PrintRLength(const uint64_t cputype, const unsigned r_type, 833 const unsigned r_length, const bool previous_arm_half){ 834 if (cputype == MachO::CPU_TYPE_ARM && 835 (r_type == MachO::ARM_RELOC_HALF || 836 r_type == MachO::ARM_RELOC_HALF_SECTDIFF || previous_arm_half == true)) { 837 if ((r_length & 0x1) == 0) 838 outs() << "lo/"; 839 else 840 outs() << "hi/"; 841 if ((r_length & 0x1) == 0) 842 outs() << "arm "; 843 else 844 outs() << "thm "; 845 } else { 846 switch (r_length) { 847 case 0: 848 outs() << "byte "; 849 break; 850 case 1: 851 outs() << "word "; 852 break; 853 case 2: 854 outs() << "long "; 855 break; 856 case 3: 857 if (cputype == MachO::CPU_TYPE_X86_64) 858 outs() << "quad "; 859 else 860 outs() << format("?(%2d) ", r_length); 861 break; 862 default: 863 outs() << format("?(%2d) ", r_length); 864 } 865 } 866 } 867 868 static void PrintRelocationEntries(const MachOObjectFile *O, 869 const relocation_iterator Begin, 870 const relocation_iterator End, 871 const uint64_t cputype, 872 const bool verbose) { 873 const MachO::symtab_command Symtab = O->getSymtabLoadCommand(); 874 bool previous_arm_half = false; 875 bool previous_sectdiff = false; 876 uint32_t sectdiff_r_type = 0; 877 878 for (relocation_iterator Reloc = Begin; Reloc != End; ++Reloc) { 879 const DataRefImpl Rel = Reloc->getRawDataRefImpl(); 880 const MachO::any_relocation_info RE = O->getRelocation(Rel); 881 const unsigned r_type = O->getAnyRelocationType(RE); 882 const bool r_scattered = O->isRelocationScattered(RE); 883 const unsigned r_pcrel = O->getAnyRelocationPCRel(RE); 884 const unsigned r_length = O->getAnyRelocationLength(RE); 885 const unsigned r_address = O->getAnyRelocationAddress(RE); 886 const bool r_extern = (r_scattered ? false : 887 O->getPlainRelocationExternal(RE)); 888 const uint32_t r_value = (r_scattered ? 889 O->getScatteredRelocationValue(RE) : 0); 890 const unsigned r_symbolnum = (r_scattered ? 0 : 891 O->getPlainRelocationSymbolNum(RE)); 892 893 if (r_scattered && cputype != MachO::CPU_TYPE_X86_64) { 894 if (verbose) { 895 // scattered: address 896 if ((cputype == MachO::CPU_TYPE_I386 && 897 r_type == MachO::GENERIC_RELOC_PAIR) || 898 (cputype == MachO::CPU_TYPE_ARM && r_type == MachO::ARM_RELOC_PAIR)) 899 outs() << " "; 900 else 901 outs() << format("%08x ", (unsigned int)r_address); 902 903 // scattered: pcrel 904 if (r_pcrel) 905 outs() << "True "; 906 else 907 outs() << "False "; 908 909 // scattered: length 910 PrintRLength(cputype, r_type, r_length, previous_arm_half); 911 912 // scattered: extern & type 913 outs() << "n/a "; 914 PrintRType(cputype, r_type); 915 916 // scattered: scattered & value 917 outs() << format("True 0x%08x", (unsigned int)r_value); 918 if (previous_sectdiff == false) { 919 if ((cputype == MachO::CPU_TYPE_ARM && 920 r_type == MachO::ARM_RELOC_PAIR)) 921 outs() << format(" half = 0x%04x ", (unsigned int)r_address); 922 } else if (cputype == MachO::CPU_TYPE_ARM && 923 sectdiff_r_type == MachO::ARM_RELOC_HALF_SECTDIFF) 924 outs() << format(" other_half = 0x%04x ", (unsigned int)r_address); 925 if ((cputype == MachO::CPU_TYPE_I386 && 926 (r_type == MachO::GENERIC_RELOC_SECTDIFF || 927 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) || 928 (cputype == MachO::CPU_TYPE_ARM && 929 (sectdiff_r_type == MachO::ARM_RELOC_SECTDIFF || 930 sectdiff_r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF || 931 sectdiff_r_type == MachO::ARM_RELOC_HALF_SECTDIFF))) { 932 previous_sectdiff = true; 933 sectdiff_r_type = r_type; 934 } else { 935 previous_sectdiff = false; 936 sectdiff_r_type = 0; 937 } 938 if (cputype == MachO::CPU_TYPE_ARM && 939 (r_type == MachO::ARM_RELOC_HALF || 940 r_type == MachO::ARM_RELOC_HALF_SECTDIFF)) 941 previous_arm_half = true; 942 else 943 previous_arm_half = false; 944 outs() << "\n"; 945 } 946 else { 947 // scattered: address pcrel length extern type scattered value 948 outs() << format("%08x %1d %-2d n/a %-7d 1 0x%08x\n", 949 (unsigned int)r_address, r_pcrel, r_length, r_type, 950 (unsigned int)r_value); 951 } 952 } 953 else { 954 if (verbose) { 955 // plain: address 956 if (cputype == MachO::CPU_TYPE_ARM && r_type == MachO::ARM_RELOC_PAIR) 957 outs() << " "; 958 else 959 outs() << format("%08x ", (unsigned int)r_address); 960 961 // plain: pcrel 962 if (r_pcrel) 963 outs() << "True "; 964 else 965 outs() << "False "; 966 967 // plain: length 968 PrintRLength(cputype, r_type, r_length, previous_arm_half); 969 970 if (r_extern) { 971 // plain: extern & type & scattered 972 outs() << "True "; 973 PrintRType(cputype, r_type); 974 outs() << "False "; 975 976 // plain: symbolnum/value 977 if (r_symbolnum > Symtab.nsyms) 978 outs() << format("?(%d)\n", r_symbolnum); 979 else { 980 SymbolRef Symbol = *O->getSymbolByIndex(r_symbolnum); 981 Expected<StringRef> SymNameNext = Symbol.getName(); 982 const char *name = NULL; 983 if (SymNameNext) 984 name = SymNameNext->data(); 985 if (name == NULL) 986 outs() << format("?(%d)\n", r_symbolnum); 987 else 988 outs() << name << "\n"; 989 } 990 } 991 else { 992 // plain: extern & type & scattered 993 outs() << "False "; 994 PrintRType(cputype, r_type); 995 outs() << "False "; 996 997 // plain: symbolnum/value 998 if (cputype == MachO::CPU_TYPE_ARM && r_type == MachO::ARM_RELOC_PAIR) 999 outs() << format("other_half = 0x%04x\n", (unsigned int)r_address); 1000 else if ((cputype == MachO::CPU_TYPE_ARM64 || 1001 cputype == MachO::CPU_TYPE_ARM64_32) && 1002 r_type == MachO::ARM64_RELOC_ADDEND) 1003 outs() << format("addend = 0x%06x\n", (unsigned int)r_symbolnum); 1004 else { 1005 outs() << format("%d ", r_symbolnum); 1006 if (r_symbolnum == MachO::R_ABS) 1007 outs() << "R_ABS\n"; 1008 else { 1009 // in this case, r_symbolnum is actually a 1-based section number 1010 uint32_t nsects = O->section_end()->getRawDataRefImpl().d.a; 1011 if (r_symbolnum > 0 && r_symbolnum <= nsects) { 1012 object::DataRefImpl DRI; 1013 DRI.d.a = r_symbolnum-1; 1014 StringRef SegName = O->getSectionFinalSegmentName(DRI); 1015 if (Expected<StringRef> NameOrErr = O->getSectionName(DRI)) 1016 outs() << "(" << SegName << "," << *NameOrErr << ")\n"; 1017 else 1018 outs() << "(?,?)\n"; 1019 } 1020 else { 1021 outs() << "(?,?)\n"; 1022 } 1023 } 1024 } 1025 } 1026 if (cputype == MachO::CPU_TYPE_ARM && 1027 (r_type == MachO::ARM_RELOC_HALF || 1028 r_type == MachO::ARM_RELOC_HALF_SECTDIFF)) 1029 previous_arm_half = true; 1030 else 1031 previous_arm_half = false; 1032 } 1033 else { 1034 // plain: address pcrel length extern type scattered symbolnum/section 1035 outs() << format("%08x %1d %-2d %1d %-7d 0 %d\n", 1036 (unsigned int)r_address, r_pcrel, r_length, r_extern, 1037 r_type, r_symbolnum); 1038 } 1039 } 1040 } 1041 } 1042 1043 static void PrintRelocations(const MachOObjectFile *O, const bool verbose) { 1044 const uint64_t cputype = O->getHeader().cputype; 1045 const MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand(); 1046 if (Dysymtab.nextrel != 0) { 1047 outs() << "External relocation information " << Dysymtab.nextrel 1048 << " entries"; 1049 outs() << "\naddress pcrel length extern type scattered " 1050 "symbolnum/value\n"; 1051 PrintRelocationEntries(O, O->extrel_begin(), O->extrel_end(), cputype, 1052 verbose); 1053 } 1054 if (Dysymtab.nlocrel != 0) { 1055 outs() << format("Local relocation information %u entries", 1056 Dysymtab.nlocrel); 1057 outs() << "\naddress pcrel length extern type scattered " 1058 "symbolnum/value\n"; 1059 PrintRelocationEntries(O, O->locrel_begin(), O->locrel_end(), cputype, 1060 verbose); 1061 } 1062 for (const auto &Load : O->load_commands()) { 1063 if (Load.C.cmd == MachO::LC_SEGMENT_64) { 1064 const MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load); 1065 for (unsigned J = 0; J < Seg.nsects; ++J) { 1066 const MachO::section_64 Sec = O->getSection64(Load, J); 1067 if (Sec.nreloc != 0) { 1068 DataRefImpl DRI; 1069 DRI.d.a = J; 1070 const StringRef SegName = O->getSectionFinalSegmentName(DRI); 1071 if (Expected<StringRef> NameOrErr = O->getSectionName(DRI)) 1072 outs() << "Relocation information (" << SegName << "," << *NameOrErr 1073 << format(") %u entries", Sec.nreloc); 1074 else 1075 outs() << "Relocation information (" << SegName << ",?) " 1076 << format("%u entries", Sec.nreloc); 1077 outs() << "\naddress pcrel length extern type scattered " 1078 "symbolnum/value\n"; 1079 PrintRelocationEntries(O, O->section_rel_begin(DRI), 1080 O->section_rel_end(DRI), cputype, verbose); 1081 } 1082 } 1083 } else if (Load.C.cmd == MachO::LC_SEGMENT) { 1084 const MachO::segment_command Seg = O->getSegmentLoadCommand(Load); 1085 for (unsigned J = 0; J < Seg.nsects; ++J) { 1086 const MachO::section Sec = O->getSection(Load, J); 1087 if (Sec.nreloc != 0) { 1088 DataRefImpl DRI; 1089 DRI.d.a = J; 1090 const StringRef SegName = O->getSectionFinalSegmentName(DRI); 1091 if (Expected<StringRef> NameOrErr = O->getSectionName(DRI)) 1092 outs() << "Relocation information (" << SegName << "," << *NameOrErr 1093 << format(") %u entries", Sec.nreloc); 1094 else 1095 outs() << "Relocation information (" << SegName << ",?) " 1096 << format("%u entries", Sec.nreloc); 1097 outs() << "\naddress pcrel length extern type scattered " 1098 "symbolnum/value\n"; 1099 PrintRelocationEntries(O, O->section_rel_begin(DRI), 1100 O->section_rel_end(DRI), cputype, verbose); 1101 } 1102 } 1103 } 1104 } 1105 } 1106 1107 static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) { 1108 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand(); 1109 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry); 1110 outs() << "Data in code table (" << nentries << " entries)\n"; 1111 outs() << "offset length kind\n"; 1112 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE; 1113 ++DI) { 1114 uint32_t Offset; 1115 DI->getOffset(Offset); 1116 outs() << format("0x%08" PRIx32, Offset) << " "; 1117 uint16_t Length; 1118 DI->getLength(Length); 1119 outs() << format("%6u", Length) << " "; 1120 uint16_t Kind; 1121 DI->getKind(Kind); 1122 if (verbose) { 1123 switch (Kind) { 1124 case MachO::DICE_KIND_DATA: 1125 outs() << "DATA"; 1126 break; 1127 case MachO::DICE_KIND_JUMP_TABLE8: 1128 outs() << "JUMP_TABLE8"; 1129 break; 1130 case MachO::DICE_KIND_JUMP_TABLE16: 1131 outs() << "JUMP_TABLE16"; 1132 break; 1133 case MachO::DICE_KIND_JUMP_TABLE32: 1134 outs() << "JUMP_TABLE32"; 1135 break; 1136 case MachO::DICE_KIND_ABS_JUMP_TABLE32: 1137 outs() << "ABS_JUMP_TABLE32"; 1138 break; 1139 default: 1140 outs() << format("0x%04" PRIx32, Kind); 1141 break; 1142 } 1143 } else 1144 outs() << format("0x%04" PRIx32, Kind); 1145 outs() << "\n"; 1146 } 1147 } 1148 1149 static void PrintLinkOptHints(MachOObjectFile *O) { 1150 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand(); 1151 const char *loh = O->getData().substr(LohLC.dataoff, 1).data(); 1152 uint32_t nloh = LohLC.datasize; 1153 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n"; 1154 for (uint32_t i = 0; i < nloh;) { 1155 unsigned n; 1156 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n); 1157 i += n; 1158 outs() << " identifier " << identifier << " "; 1159 if (i >= nloh) 1160 return; 1161 switch (identifier) { 1162 case 1: 1163 outs() << "AdrpAdrp\n"; 1164 break; 1165 case 2: 1166 outs() << "AdrpLdr\n"; 1167 break; 1168 case 3: 1169 outs() << "AdrpAddLdr\n"; 1170 break; 1171 case 4: 1172 outs() << "AdrpLdrGotLdr\n"; 1173 break; 1174 case 5: 1175 outs() << "AdrpAddStr\n"; 1176 break; 1177 case 6: 1178 outs() << "AdrpLdrGotStr\n"; 1179 break; 1180 case 7: 1181 outs() << "AdrpAdd\n"; 1182 break; 1183 case 8: 1184 outs() << "AdrpLdrGot\n"; 1185 break; 1186 default: 1187 outs() << "Unknown identifier value\n"; 1188 break; 1189 } 1190 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n); 1191 i += n; 1192 outs() << " narguments " << narguments << "\n"; 1193 if (i >= nloh) 1194 return; 1195 1196 for (uint32_t j = 0; j < narguments; j++) { 1197 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n); 1198 i += n; 1199 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n"; 1200 if (i >= nloh) 1201 return; 1202 } 1203 } 1204 } 1205 1206 static void PrintDylibs(MachOObjectFile *O, bool JustId) { 1207 unsigned Index = 0; 1208 for (const auto &Load : O->load_commands()) { 1209 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) || 1210 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB || 1211 Load.C.cmd == MachO::LC_LOAD_DYLIB || 1212 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB || 1213 Load.C.cmd == MachO::LC_REEXPORT_DYLIB || 1214 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB || 1215 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) { 1216 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load); 1217 if (dl.dylib.name < dl.cmdsize) { 1218 const char *p = (const char *)(Load.Ptr) + dl.dylib.name; 1219 if (JustId) 1220 outs() << p << "\n"; 1221 else { 1222 outs() << "\t" << p; 1223 outs() << " (compatibility version " 1224 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "." 1225 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "." 1226 << (dl.dylib.compatibility_version & 0xff) << ","; 1227 outs() << " current version " 1228 << ((dl.dylib.current_version >> 16) & 0xffff) << "." 1229 << ((dl.dylib.current_version >> 8) & 0xff) << "." 1230 << (dl.dylib.current_version & 0xff); 1231 if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB) 1232 outs() << ", weak"; 1233 if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB) 1234 outs() << ", reexport"; 1235 if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) 1236 outs() << ", upward"; 1237 if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB) 1238 outs() << ", lazy"; 1239 outs() << ")\n"; 1240 } 1241 } else { 1242 outs() << "\tBad offset (" << dl.dylib.name << ") for name of "; 1243 if (Load.C.cmd == MachO::LC_ID_DYLIB) 1244 outs() << "LC_ID_DYLIB "; 1245 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB) 1246 outs() << "LC_LOAD_DYLIB "; 1247 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB) 1248 outs() << "LC_LOAD_WEAK_DYLIB "; 1249 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB) 1250 outs() << "LC_LAZY_LOAD_DYLIB "; 1251 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB) 1252 outs() << "LC_REEXPORT_DYLIB "; 1253 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) 1254 outs() << "LC_LOAD_UPWARD_DYLIB "; 1255 else 1256 outs() << "LC_??? "; 1257 outs() << "command " << Index++ << "\n"; 1258 } 1259 } 1260 } 1261 } 1262 1263 typedef DenseMap<uint64_t, StringRef> SymbolAddressMap; 1264 1265 static void CreateSymbolAddressMap(MachOObjectFile *O, 1266 SymbolAddressMap *AddrMap) { 1267 // Create a map of symbol addresses to symbol names. 1268 const StringRef FileName = O->getFileName(); 1269 for (const SymbolRef &Symbol : O->symbols()) { 1270 SymbolRef::Type ST = unwrapOrError(Symbol.getType(), FileName); 1271 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data || 1272 ST == SymbolRef::ST_Other) { 1273 uint64_t Address = cantFail(Symbol.getValue()); 1274 StringRef SymName = unwrapOrError(Symbol.getName(), FileName); 1275 if (!SymName.startswith(".objc")) 1276 (*AddrMap)[Address] = SymName; 1277 } 1278 } 1279 } 1280 1281 // GuessSymbolName is passed the address of what might be a symbol and a 1282 // pointer to the SymbolAddressMap. It returns the name of a symbol 1283 // with that address or nullptr if no symbol is found with that address. 1284 static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) { 1285 const char *SymbolName = nullptr; 1286 // A DenseMap can't lookup up some values. 1287 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) { 1288 StringRef name = AddrMap->lookup(value); 1289 if (!name.empty()) 1290 SymbolName = name.data(); 1291 } 1292 return SymbolName; 1293 } 1294 1295 static void DumpCstringChar(const char c) { 1296 char p[2]; 1297 p[0] = c; 1298 p[1] = '\0'; 1299 outs().write_escaped(p); 1300 } 1301 1302 static void DumpCstringSection(MachOObjectFile *O, const char *sect, 1303 uint32_t sect_size, uint64_t sect_addr, 1304 bool print_addresses) { 1305 for (uint32_t i = 0; i < sect_size; i++) { 1306 if (print_addresses) { 1307 if (O->is64Bit()) 1308 outs() << format("%016" PRIx64, sect_addr + i) << " "; 1309 else 1310 outs() << format("%08" PRIx64, sect_addr + i) << " "; 1311 } 1312 for (; i < sect_size && sect[i] != '\0'; i++) 1313 DumpCstringChar(sect[i]); 1314 if (i < sect_size && sect[i] == '\0') 1315 outs() << "\n"; 1316 } 1317 } 1318 1319 static void DumpLiteral4(uint32_t l, float f) { 1320 outs() << format("0x%08" PRIx32, l); 1321 if ((l & 0x7f800000) != 0x7f800000) 1322 outs() << format(" (%.16e)\n", f); 1323 else { 1324 if (l == 0x7f800000) 1325 outs() << " (+Infinity)\n"; 1326 else if (l == 0xff800000) 1327 outs() << " (-Infinity)\n"; 1328 else if ((l & 0x00400000) == 0x00400000) 1329 outs() << " (non-signaling Not-a-Number)\n"; 1330 else 1331 outs() << " (signaling Not-a-Number)\n"; 1332 } 1333 } 1334 1335 static void DumpLiteral4Section(MachOObjectFile *O, const char *sect, 1336 uint32_t sect_size, uint64_t sect_addr, 1337 bool print_addresses) { 1338 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) { 1339 if (print_addresses) { 1340 if (O->is64Bit()) 1341 outs() << format("%016" PRIx64, sect_addr + i) << " "; 1342 else 1343 outs() << format("%08" PRIx64, sect_addr + i) << " "; 1344 } 1345 float f; 1346 memcpy(&f, sect + i, sizeof(float)); 1347 if (O->isLittleEndian() != sys::IsLittleEndianHost) 1348 sys::swapByteOrder(f); 1349 uint32_t l; 1350 memcpy(&l, sect + i, sizeof(uint32_t)); 1351 if (O->isLittleEndian() != sys::IsLittleEndianHost) 1352 sys::swapByteOrder(l); 1353 DumpLiteral4(l, f); 1354 } 1355 } 1356 1357 static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1, 1358 double d) { 1359 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1); 1360 uint32_t Hi, Lo; 1361 Hi = (O->isLittleEndian()) ? l1 : l0; 1362 Lo = (O->isLittleEndian()) ? l0 : l1; 1363 1364 // Hi is the high word, so this is equivalent to if(isfinite(d)) 1365 if ((Hi & 0x7ff00000) != 0x7ff00000) 1366 outs() << format(" (%.16e)\n", d); 1367 else { 1368 if (Hi == 0x7ff00000 && Lo == 0) 1369 outs() << " (+Infinity)\n"; 1370 else if (Hi == 0xfff00000 && Lo == 0) 1371 outs() << " (-Infinity)\n"; 1372 else if ((Hi & 0x00080000) == 0x00080000) 1373 outs() << " (non-signaling Not-a-Number)\n"; 1374 else 1375 outs() << " (signaling Not-a-Number)\n"; 1376 } 1377 } 1378 1379 static void DumpLiteral8Section(MachOObjectFile *O, const char *sect, 1380 uint32_t sect_size, uint64_t sect_addr, 1381 bool print_addresses) { 1382 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) { 1383 if (print_addresses) { 1384 if (O->is64Bit()) 1385 outs() << format("%016" PRIx64, sect_addr + i) << " "; 1386 else 1387 outs() << format("%08" PRIx64, sect_addr + i) << " "; 1388 } 1389 double d; 1390 memcpy(&d, sect + i, sizeof(double)); 1391 if (O->isLittleEndian() != sys::IsLittleEndianHost) 1392 sys::swapByteOrder(d); 1393 uint32_t l0, l1; 1394 memcpy(&l0, sect + i, sizeof(uint32_t)); 1395 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t)); 1396 if (O->isLittleEndian() != sys::IsLittleEndianHost) { 1397 sys::swapByteOrder(l0); 1398 sys::swapByteOrder(l1); 1399 } 1400 DumpLiteral8(O, l0, l1, d); 1401 } 1402 } 1403 1404 static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) { 1405 outs() << format("0x%08" PRIx32, l0) << " "; 1406 outs() << format("0x%08" PRIx32, l1) << " "; 1407 outs() << format("0x%08" PRIx32, l2) << " "; 1408 outs() << format("0x%08" PRIx32, l3) << "\n"; 1409 } 1410 1411 static void DumpLiteral16Section(MachOObjectFile *O, const char *sect, 1412 uint32_t sect_size, uint64_t sect_addr, 1413 bool print_addresses) { 1414 for (uint32_t i = 0; i < sect_size; i += 16) { 1415 if (print_addresses) { 1416 if (O->is64Bit()) 1417 outs() << format("%016" PRIx64, sect_addr + i) << " "; 1418 else 1419 outs() << format("%08" PRIx64, sect_addr + i) << " "; 1420 } 1421 uint32_t l0, l1, l2, l3; 1422 memcpy(&l0, sect + i, sizeof(uint32_t)); 1423 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t)); 1424 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t)); 1425 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t)); 1426 if (O->isLittleEndian() != sys::IsLittleEndianHost) { 1427 sys::swapByteOrder(l0); 1428 sys::swapByteOrder(l1); 1429 sys::swapByteOrder(l2); 1430 sys::swapByteOrder(l3); 1431 } 1432 DumpLiteral16(l0, l1, l2, l3); 1433 } 1434 } 1435 1436 static void DumpLiteralPointerSection(MachOObjectFile *O, 1437 const SectionRef &Section, 1438 const char *sect, uint32_t sect_size, 1439 uint64_t sect_addr, 1440 bool print_addresses) { 1441 // Collect the literal sections in this Mach-O file. 1442 std::vector<SectionRef> LiteralSections; 1443 for (const SectionRef &Section : O->sections()) { 1444 DataRefImpl Ref = Section.getRawDataRefImpl(); 1445 uint32_t section_type; 1446 if (O->is64Bit()) { 1447 const MachO::section_64 Sec = O->getSection64(Ref); 1448 section_type = Sec.flags & MachO::SECTION_TYPE; 1449 } else { 1450 const MachO::section Sec = O->getSection(Ref); 1451 section_type = Sec.flags & MachO::SECTION_TYPE; 1452 } 1453 if (section_type == MachO::S_CSTRING_LITERALS || 1454 section_type == MachO::S_4BYTE_LITERALS || 1455 section_type == MachO::S_8BYTE_LITERALS || 1456 section_type == MachO::S_16BYTE_LITERALS) 1457 LiteralSections.push_back(Section); 1458 } 1459 1460 // Set the size of the literal pointer. 1461 uint32_t lp_size = O->is64Bit() ? 8 : 4; 1462 1463 // Collect the external relocation symbols for the literal pointers. 1464 std::vector<std::pair<uint64_t, SymbolRef>> Relocs; 1465 for (const RelocationRef &Reloc : Section.relocations()) { 1466 DataRefImpl Rel; 1467 MachO::any_relocation_info RE; 1468 bool isExtern = false; 1469 Rel = Reloc.getRawDataRefImpl(); 1470 RE = O->getRelocation(Rel); 1471 isExtern = O->getPlainRelocationExternal(RE); 1472 if (isExtern) { 1473 uint64_t RelocOffset = Reloc.getOffset(); 1474 symbol_iterator RelocSym = Reloc.getSymbol(); 1475 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym)); 1476 } 1477 } 1478 array_pod_sort(Relocs.begin(), Relocs.end()); 1479 1480 // Dump each literal pointer. 1481 for (uint32_t i = 0; i < sect_size; i += lp_size) { 1482 if (print_addresses) { 1483 if (O->is64Bit()) 1484 outs() << format("%016" PRIx64, sect_addr + i) << " "; 1485 else 1486 outs() << format("%08" PRIx64, sect_addr + i) << " "; 1487 } 1488 uint64_t lp; 1489 if (O->is64Bit()) { 1490 memcpy(&lp, sect + i, sizeof(uint64_t)); 1491 if (O->isLittleEndian() != sys::IsLittleEndianHost) 1492 sys::swapByteOrder(lp); 1493 } else { 1494 uint32_t li; 1495 memcpy(&li, sect + i, sizeof(uint32_t)); 1496 if (O->isLittleEndian() != sys::IsLittleEndianHost) 1497 sys::swapByteOrder(li); 1498 lp = li; 1499 } 1500 1501 // First look for an external relocation entry for this literal pointer. 1502 auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) { 1503 return P.first == i; 1504 }); 1505 if (Reloc != Relocs.end()) { 1506 symbol_iterator RelocSym = Reloc->second; 1507 StringRef SymName = unwrapOrError(RelocSym->getName(), O->getFileName()); 1508 outs() << "external relocation entry for symbol:" << SymName << "\n"; 1509 continue; 1510 } 1511 1512 // For local references see what the section the literal pointer points to. 1513 auto Sect = find_if(LiteralSections, [&](const SectionRef &R) { 1514 return lp >= R.getAddress() && lp < R.getAddress() + R.getSize(); 1515 }); 1516 if (Sect == LiteralSections.end()) { 1517 outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n"; 1518 continue; 1519 } 1520 1521 uint64_t SectAddress = Sect->getAddress(); 1522 uint64_t SectSize = Sect->getSize(); 1523 1524 StringRef SectName; 1525 Expected<StringRef> SectNameOrErr = Sect->getName(); 1526 if (SectNameOrErr) 1527 SectName = *SectNameOrErr; 1528 else 1529 consumeError(SectNameOrErr.takeError()); 1530 1531 DataRefImpl Ref = Sect->getRawDataRefImpl(); 1532 StringRef SegmentName = O->getSectionFinalSegmentName(Ref); 1533 outs() << SegmentName << ":" << SectName << ":"; 1534 1535 uint32_t section_type; 1536 if (O->is64Bit()) { 1537 const MachO::section_64 Sec = O->getSection64(Ref); 1538 section_type = Sec.flags & MachO::SECTION_TYPE; 1539 } else { 1540 const MachO::section Sec = O->getSection(Ref); 1541 section_type = Sec.flags & MachO::SECTION_TYPE; 1542 } 1543 1544 StringRef BytesStr = unwrapOrError(Sect->getContents(), O->getFileName()); 1545 1546 const char *Contents = reinterpret_cast<const char *>(BytesStr.data()); 1547 1548 switch (section_type) { 1549 case MachO::S_CSTRING_LITERALS: 1550 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0'; 1551 i++) { 1552 DumpCstringChar(Contents[i]); 1553 } 1554 outs() << "\n"; 1555 break; 1556 case MachO::S_4BYTE_LITERALS: 1557 float f; 1558 memcpy(&f, Contents + (lp - SectAddress), sizeof(float)); 1559 uint32_t l; 1560 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t)); 1561 if (O->isLittleEndian() != sys::IsLittleEndianHost) { 1562 sys::swapByteOrder(f); 1563 sys::swapByteOrder(l); 1564 } 1565 DumpLiteral4(l, f); 1566 break; 1567 case MachO::S_8BYTE_LITERALS: { 1568 double d; 1569 memcpy(&d, Contents + (lp - SectAddress), sizeof(double)); 1570 uint32_t l0, l1; 1571 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t)); 1572 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t), 1573 sizeof(uint32_t)); 1574 if (O->isLittleEndian() != sys::IsLittleEndianHost) { 1575 sys::swapByteOrder(f); 1576 sys::swapByteOrder(l0); 1577 sys::swapByteOrder(l1); 1578 } 1579 DumpLiteral8(O, l0, l1, d); 1580 break; 1581 } 1582 case MachO::S_16BYTE_LITERALS: { 1583 uint32_t l0, l1, l2, l3; 1584 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t)); 1585 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t), 1586 sizeof(uint32_t)); 1587 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t), 1588 sizeof(uint32_t)); 1589 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t), 1590 sizeof(uint32_t)); 1591 if (O->isLittleEndian() != sys::IsLittleEndianHost) { 1592 sys::swapByteOrder(l0); 1593 sys::swapByteOrder(l1); 1594 sys::swapByteOrder(l2); 1595 sys::swapByteOrder(l3); 1596 } 1597 DumpLiteral16(l0, l1, l2, l3); 1598 break; 1599 } 1600 } 1601 } 1602 } 1603 1604 static void DumpInitTermPointerSection(MachOObjectFile *O, 1605 const SectionRef &Section, 1606 const char *sect, 1607 uint32_t sect_size, uint64_t sect_addr, 1608 SymbolAddressMap *AddrMap, 1609 bool verbose) { 1610 uint32_t stride; 1611 stride = (O->is64Bit()) ? sizeof(uint64_t) : sizeof(uint32_t); 1612 1613 // Collect the external relocation symbols for the pointers. 1614 std::vector<std::pair<uint64_t, SymbolRef>> Relocs; 1615 for (const RelocationRef &Reloc : Section.relocations()) { 1616 DataRefImpl Rel; 1617 MachO::any_relocation_info RE; 1618 bool isExtern = false; 1619 Rel = Reloc.getRawDataRefImpl(); 1620 RE = O->getRelocation(Rel); 1621 isExtern = O->getPlainRelocationExternal(RE); 1622 if (isExtern) { 1623 uint64_t RelocOffset = Reloc.getOffset(); 1624 symbol_iterator RelocSym = Reloc.getSymbol(); 1625 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym)); 1626 } 1627 } 1628 array_pod_sort(Relocs.begin(), Relocs.end()); 1629 1630 for (uint32_t i = 0; i < sect_size; i += stride) { 1631 const char *SymbolName = nullptr; 1632 uint64_t p; 1633 if (O->is64Bit()) { 1634 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " "; 1635 uint64_t pointer_value; 1636 memcpy(&pointer_value, sect + i, stride); 1637 if (O->isLittleEndian() != sys::IsLittleEndianHost) 1638 sys::swapByteOrder(pointer_value); 1639 outs() << format("0x%016" PRIx64, pointer_value); 1640 p = pointer_value; 1641 } else { 1642 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " "; 1643 uint32_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%08" PRIx32, pointer_value); 1648 p = pointer_value; 1649 } 1650 if (verbose) { 1651 // First look for an external relocation entry for this pointer. 1652 auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) { 1653 return P.first == i; 1654 }); 1655 if (Reloc != Relocs.end()) { 1656 symbol_iterator RelocSym = Reloc->second; 1657 outs() << " " << unwrapOrError(RelocSym->getName(), O->getFileName()); 1658 } else { 1659 SymbolName = GuessSymbolName(p, AddrMap); 1660 if (SymbolName) 1661 outs() << " " << SymbolName; 1662 } 1663 } 1664 outs() << "\n"; 1665 } 1666 } 1667 1668 static void DumpRawSectionContents(MachOObjectFile *O, const char *sect, 1669 uint32_t size, uint64_t addr) { 1670 uint32_t cputype = O->getHeader().cputype; 1671 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) { 1672 uint32_t j; 1673 for (uint32_t i = 0; i < size; i += j, addr += j) { 1674 if (O->is64Bit()) 1675 outs() << format("%016" PRIx64, addr) << "\t"; 1676 else 1677 outs() << format("%08" PRIx64, addr) << "\t"; 1678 for (j = 0; j < 16 && i + j < size; j++) { 1679 uint8_t byte_word = *(sect + i + j); 1680 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " "; 1681 } 1682 outs() << "\n"; 1683 } 1684 } else { 1685 uint32_t j; 1686 for (uint32_t i = 0; i < size; i += j, addr += j) { 1687 if (O->is64Bit()) 1688 outs() << format("%016" PRIx64, addr) << "\t"; 1689 else 1690 outs() << format("%08" PRIx64, addr) << "\t"; 1691 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size; 1692 j += sizeof(int32_t)) { 1693 if (i + j + sizeof(int32_t) <= size) { 1694 uint32_t long_word; 1695 memcpy(&long_word, sect + i + j, sizeof(int32_t)); 1696 if (O->isLittleEndian() != sys::IsLittleEndianHost) 1697 sys::swapByteOrder(long_word); 1698 outs() << format("%08" PRIx32, long_word) << " "; 1699 } else { 1700 for (uint32_t k = 0; i + j + k < size; k++) { 1701 uint8_t byte_word = *(sect + i + j + k); 1702 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " "; 1703 } 1704 } 1705 } 1706 outs() << "\n"; 1707 } 1708 } 1709 } 1710 1711 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, 1712 StringRef DisSegName, StringRef DisSectName); 1713 static void DumpProtocolSection(MachOObjectFile *O, const char *sect, 1714 uint32_t size, uint32_t addr); 1715 #ifdef HAVE_LIBXAR 1716 static void DumpBitcodeSection(MachOObjectFile *O, const char *sect, 1717 uint32_t size, bool verbose, 1718 bool PrintXarHeader, bool PrintXarFileHeaders, 1719 std::string XarMemberName); 1720 #endif // defined(HAVE_LIBXAR) 1721 1722 static void DumpSectionContents(StringRef Filename, MachOObjectFile *O, 1723 bool verbose) { 1724 SymbolAddressMap AddrMap; 1725 if (verbose) 1726 CreateSymbolAddressMap(O, &AddrMap); 1727 1728 for (unsigned i = 0; i < FilterSections.size(); ++i) { 1729 StringRef DumpSection = FilterSections[i]; 1730 std::pair<StringRef, StringRef> DumpSegSectName; 1731 DumpSegSectName = DumpSection.split(','); 1732 StringRef DumpSegName, DumpSectName; 1733 if (!DumpSegSectName.second.empty()) { 1734 DumpSegName = DumpSegSectName.first; 1735 DumpSectName = DumpSegSectName.second; 1736 } else { 1737 DumpSegName = ""; 1738 DumpSectName = DumpSegSectName.first; 1739 } 1740 for (const SectionRef &Section : O->sections()) { 1741 StringRef SectName; 1742 Expected<StringRef> SecNameOrErr = Section.getName(); 1743 if (SecNameOrErr) 1744 SectName = *SecNameOrErr; 1745 else 1746 consumeError(SecNameOrErr.takeError()); 1747 1748 if (!DumpSection.empty()) 1749 FoundSectionSet.insert(DumpSection); 1750 1751 DataRefImpl Ref = Section.getRawDataRefImpl(); 1752 StringRef SegName = O->getSectionFinalSegmentName(Ref); 1753 if ((DumpSegName.empty() || SegName == DumpSegName) && 1754 (SectName == DumpSectName)) { 1755 1756 uint32_t section_flags; 1757 if (O->is64Bit()) { 1758 const MachO::section_64 Sec = O->getSection64(Ref); 1759 section_flags = Sec.flags; 1760 1761 } else { 1762 const MachO::section Sec = O->getSection(Ref); 1763 section_flags = Sec.flags; 1764 } 1765 uint32_t section_type = section_flags & MachO::SECTION_TYPE; 1766 1767 StringRef BytesStr = 1768 unwrapOrError(Section.getContents(), O->getFileName()); 1769 const char *sect = reinterpret_cast<const char *>(BytesStr.data()); 1770 uint32_t sect_size = BytesStr.size(); 1771 uint64_t sect_addr = Section.getAddress(); 1772 1773 if (!NoLeadingHeaders) 1774 outs() << "Contents of (" << SegName << "," << SectName 1775 << ") section\n"; 1776 1777 if (verbose) { 1778 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) || 1779 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) { 1780 DisassembleMachO(Filename, O, SegName, SectName); 1781 continue; 1782 } 1783 if (SegName == "__TEXT" && SectName == "__info_plist") { 1784 outs() << sect; 1785 continue; 1786 } 1787 if (SegName == "__OBJC" && SectName == "__protocol") { 1788 DumpProtocolSection(O, sect, sect_size, sect_addr); 1789 continue; 1790 } 1791 #ifdef HAVE_LIBXAR 1792 if (SegName == "__LLVM" && SectName == "__bundle") { 1793 DumpBitcodeSection(O, sect, sect_size, verbose, !NoSymbolicOperands, 1794 ArchiveHeaders, ""); 1795 continue; 1796 } 1797 #endif // defined(HAVE_LIBXAR) 1798 switch (section_type) { 1799 case MachO::S_REGULAR: 1800 DumpRawSectionContents(O, sect, sect_size, sect_addr); 1801 break; 1802 case MachO::S_ZEROFILL: 1803 outs() << "zerofill section and has no contents in the file\n"; 1804 break; 1805 case MachO::S_CSTRING_LITERALS: 1806 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr); 1807 break; 1808 case MachO::S_4BYTE_LITERALS: 1809 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr); 1810 break; 1811 case MachO::S_8BYTE_LITERALS: 1812 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr); 1813 break; 1814 case MachO::S_16BYTE_LITERALS: 1815 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr); 1816 break; 1817 case MachO::S_LITERAL_POINTERS: 1818 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr, 1819 !NoLeadingAddr); 1820 break; 1821 case MachO::S_MOD_INIT_FUNC_POINTERS: 1822 case MachO::S_MOD_TERM_FUNC_POINTERS: 1823 DumpInitTermPointerSection(O, Section, sect, sect_size, sect_addr, 1824 &AddrMap, verbose); 1825 break; 1826 default: 1827 outs() << "Unknown section type (" 1828 << format("0x%08" PRIx32, section_type) << ")\n"; 1829 DumpRawSectionContents(O, sect, sect_size, sect_addr); 1830 break; 1831 } 1832 } else { 1833 if (section_type == MachO::S_ZEROFILL) 1834 outs() << "zerofill section and has no contents in the file\n"; 1835 else 1836 DumpRawSectionContents(O, sect, sect_size, sect_addr); 1837 } 1838 } 1839 } 1840 } 1841 } 1842 1843 static void DumpInfoPlistSectionContents(StringRef Filename, 1844 MachOObjectFile *O) { 1845 for (const SectionRef &Section : O->sections()) { 1846 StringRef SectName; 1847 Expected<StringRef> SecNameOrErr = Section.getName(); 1848 if (SecNameOrErr) 1849 SectName = *SecNameOrErr; 1850 else 1851 consumeError(SecNameOrErr.takeError()); 1852 1853 DataRefImpl Ref = Section.getRawDataRefImpl(); 1854 StringRef SegName = O->getSectionFinalSegmentName(Ref); 1855 if (SegName == "__TEXT" && SectName == "__info_plist") { 1856 if (!NoLeadingHeaders) 1857 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 1858 StringRef BytesStr = 1859 unwrapOrError(Section.getContents(), O->getFileName()); 1860 const char *sect = reinterpret_cast<const char *>(BytesStr.data()); 1861 outs() << format("%.*s", BytesStr.size(), sect) << "\n"; 1862 return; 1863 } 1864 } 1865 } 1866 1867 // checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file 1868 // and if it is and there is a list of architecture flags is specified then 1869 // check to make sure this Mach-O file is one of those architectures or all 1870 // architectures were specified. If not then an error is generated and this 1871 // routine returns false. Else it returns true. 1872 static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) { 1873 auto *MachO = dyn_cast<MachOObjectFile>(O); 1874 1875 if (!MachO || ArchAll || ArchFlags.empty()) 1876 return true; 1877 1878 MachO::mach_header H; 1879 MachO::mach_header_64 H_64; 1880 Triple T; 1881 const char *McpuDefault, *ArchFlag; 1882 if (MachO->is64Bit()) { 1883 H_64 = MachO->MachOObjectFile::getHeader64(); 1884 T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype, 1885 &McpuDefault, &ArchFlag); 1886 } else { 1887 H = MachO->MachOObjectFile::getHeader(); 1888 T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype, 1889 &McpuDefault, &ArchFlag); 1890 } 1891 const std::string ArchFlagName(ArchFlag); 1892 if (!llvm::is_contained(ArchFlags, ArchFlagName)) { 1893 WithColor::error(errs(), "llvm-objdump") 1894 << Filename << ": no architecture specified.\n"; 1895 return false; 1896 } 1897 return true; 1898 } 1899 1900 static void printObjcMetaData(MachOObjectFile *O, bool verbose); 1901 1902 // ProcessMachO() is passed a single opened Mach-O file, which may be an 1903 // archive member and or in a slice of a universal file. It prints the 1904 // the file name and header info and then processes it according to the 1905 // command line options. 1906 static void ProcessMachO(StringRef Name, MachOObjectFile *MachOOF, 1907 StringRef ArchiveMemberName = StringRef(), 1908 StringRef ArchitectureName = StringRef()) { 1909 // If we are doing some processing here on the Mach-O file print the header 1910 // info. And don't print it otherwise like in the case of printing the 1911 // UniversalHeaders or ArchiveHeaders. 1912 if (Disassemble || Relocations || PrivateHeaders || ExportsTrie || Rebase || 1913 Bind || SymbolTable || LazyBind || WeakBind || IndirectSymbols || 1914 DataInCode || LinkOptHints || DylibsUsed || DylibId || ObjcMetaData || 1915 (!FilterSections.empty())) { 1916 if (!NoLeadingHeaders) { 1917 outs() << Name; 1918 if (!ArchiveMemberName.empty()) 1919 outs() << '(' << ArchiveMemberName << ')'; 1920 if (!ArchitectureName.empty()) 1921 outs() << " (architecture " << ArchitectureName << ")"; 1922 outs() << ":\n"; 1923 } 1924 } 1925 // To use the report_error() form with an ArchiveName and FileName set 1926 // these up based on what is passed for Name and ArchiveMemberName. 1927 StringRef ArchiveName; 1928 StringRef FileName; 1929 if (!ArchiveMemberName.empty()) { 1930 ArchiveName = Name; 1931 FileName = ArchiveMemberName; 1932 } else { 1933 ArchiveName = StringRef(); 1934 FileName = Name; 1935 } 1936 1937 // If we need the symbol table to do the operation then check it here to 1938 // produce a good error message as to where the Mach-O file comes from in 1939 // the error message. 1940 if (Disassemble || IndirectSymbols || !FilterSections.empty() || UnwindInfo) 1941 if (Error Err = MachOOF->checkSymbolTable()) 1942 reportError(std::move(Err), FileName, ArchiveName, ArchitectureName); 1943 1944 if (DisassembleAll) { 1945 for (const SectionRef &Section : MachOOF->sections()) { 1946 StringRef SectName; 1947 if (Expected<StringRef> NameOrErr = Section.getName()) 1948 SectName = *NameOrErr; 1949 else 1950 consumeError(NameOrErr.takeError()); 1951 1952 if (SectName.equals("__text")) { 1953 DataRefImpl Ref = Section.getRawDataRefImpl(); 1954 StringRef SegName = MachOOF->getSectionFinalSegmentName(Ref); 1955 DisassembleMachO(FileName, MachOOF, SegName, SectName); 1956 } 1957 } 1958 } 1959 else if (Disassemble) { 1960 if (MachOOF->getHeader().filetype == MachO::MH_KEXT_BUNDLE && 1961 MachOOF->getHeader().cputype == MachO::CPU_TYPE_ARM64) 1962 DisassembleMachO(FileName, MachOOF, "__TEXT_EXEC", "__text"); 1963 else 1964 DisassembleMachO(FileName, MachOOF, "__TEXT", "__text"); 1965 } 1966 if (IndirectSymbols) 1967 PrintIndirectSymbols(MachOOF, !NonVerbose); 1968 if (DataInCode) 1969 PrintDataInCodeTable(MachOOF, !NonVerbose); 1970 if (LinkOptHints) 1971 PrintLinkOptHints(MachOOF); 1972 if (Relocations) 1973 PrintRelocations(MachOOF, !NonVerbose); 1974 if (SectionHeaders) 1975 printSectionHeaders(MachOOF); 1976 if (SectionContents) 1977 printSectionContents(MachOOF); 1978 if (!FilterSections.empty()) 1979 DumpSectionContents(FileName, MachOOF, !NonVerbose); 1980 if (InfoPlist) 1981 DumpInfoPlistSectionContents(FileName, MachOOF); 1982 if (DylibsUsed) 1983 PrintDylibs(MachOOF, false); 1984 if (DylibId) 1985 PrintDylibs(MachOOF, true); 1986 if (SymbolTable) 1987 printSymbolTable(MachOOF, ArchiveName, ArchitectureName); 1988 if (UnwindInfo) 1989 printMachOUnwindInfo(MachOOF); 1990 if (PrivateHeaders) { 1991 printMachOFileHeader(MachOOF); 1992 printMachOLoadCommands(MachOOF); 1993 } 1994 if (FirstPrivateHeader) 1995 printMachOFileHeader(MachOOF); 1996 if (ObjcMetaData) 1997 printObjcMetaData(MachOOF, !NonVerbose); 1998 if (ExportsTrie) 1999 printExportsTrie(MachOOF); 2000 if (Rebase) 2001 printRebaseTable(MachOOF); 2002 if (Bind) 2003 printBindTable(MachOOF); 2004 if (LazyBind) 2005 printLazyBindTable(MachOOF); 2006 if (WeakBind) 2007 printWeakBindTable(MachOOF); 2008 2009 if (DwarfDumpType != DIDT_Null) { 2010 std::unique_ptr<DIContext> DICtx = DWARFContext::create(*MachOOF); 2011 // Dump the complete DWARF structure. 2012 DIDumpOptions DumpOpts; 2013 DumpOpts.DumpType = DwarfDumpType; 2014 DICtx->dump(outs(), DumpOpts); 2015 } 2016 } 2017 2018 // printUnknownCPUType() helps print_fat_headers for unknown CPU's. 2019 static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) { 2020 outs() << " cputype (" << cputype << ")\n"; 2021 outs() << " cpusubtype (" << cpusubtype << ")\n"; 2022 } 2023 2024 // printCPUType() helps print_fat_headers by printing the cputype and 2025 // pusubtype (symbolically for the one's it knows about). 2026 static void printCPUType(uint32_t cputype, uint32_t cpusubtype) { 2027 switch (cputype) { 2028 case MachO::CPU_TYPE_I386: 2029 switch (cpusubtype) { 2030 case MachO::CPU_SUBTYPE_I386_ALL: 2031 outs() << " cputype CPU_TYPE_I386\n"; 2032 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n"; 2033 break; 2034 default: 2035 printUnknownCPUType(cputype, cpusubtype); 2036 break; 2037 } 2038 break; 2039 case MachO::CPU_TYPE_X86_64: 2040 switch (cpusubtype) { 2041 case MachO::CPU_SUBTYPE_X86_64_ALL: 2042 outs() << " cputype CPU_TYPE_X86_64\n"; 2043 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n"; 2044 break; 2045 case MachO::CPU_SUBTYPE_X86_64_H: 2046 outs() << " cputype CPU_TYPE_X86_64\n"; 2047 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n"; 2048 break; 2049 default: 2050 printUnknownCPUType(cputype, cpusubtype); 2051 break; 2052 } 2053 break; 2054 case MachO::CPU_TYPE_ARM: 2055 switch (cpusubtype) { 2056 case MachO::CPU_SUBTYPE_ARM_ALL: 2057 outs() << " cputype CPU_TYPE_ARM\n"; 2058 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n"; 2059 break; 2060 case MachO::CPU_SUBTYPE_ARM_V4T: 2061 outs() << " cputype CPU_TYPE_ARM\n"; 2062 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n"; 2063 break; 2064 case MachO::CPU_SUBTYPE_ARM_V5TEJ: 2065 outs() << " cputype CPU_TYPE_ARM\n"; 2066 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n"; 2067 break; 2068 case MachO::CPU_SUBTYPE_ARM_XSCALE: 2069 outs() << " cputype CPU_TYPE_ARM\n"; 2070 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n"; 2071 break; 2072 case MachO::CPU_SUBTYPE_ARM_V6: 2073 outs() << " cputype CPU_TYPE_ARM\n"; 2074 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n"; 2075 break; 2076 case MachO::CPU_SUBTYPE_ARM_V6M: 2077 outs() << " cputype CPU_TYPE_ARM\n"; 2078 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n"; 2079 break; 2080 case MachO::CPU_SUBTYPE_ARM_V7: 2081 outs() << " cputype CPU_TYPE_ARM\n"; 2082 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n"; 2083 break; 2084 case MachO::CPU_SUBTYPE_ARM_V7EM: 2085 outs() << " cputype CPU_TYPE_ARM\n"; 2086 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n"; 2087 break; 2088 case MachO::CPU_SUBTYPE_ARM_V7K: 2089 outs() << " cputype CPU_TYPE_ARM\n"; 2090 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n"; 2091 break; 2092 case MachO::CPU_SUBTYPE_ARM_V7M: 2093 outs() << " cputype CPU_TYPE_ARM\n"; 2094 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n"; 2095 break; 2096 case MachO::CPU_SUBTYPE_ARM_V7S: 2097 outs() << " cputype CPU_TYPE_ARM\n"; 2098 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n"; 2099 break; 2100 default: 2101 printUnknownCPUType(cputype, cpusubtype); 2102 break; 2103 } 2104 break; 2105 case MachO::CPU_TYPE_ARM64: 2106 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 2107 case MachO::CPU_SUBTYPE_ARM64_ALL: 2108 outs() << " cputype CPU_TYPE_ARM64\n"; 2109 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n"; 2110 break; 2111 case MachO::CPU_SUBTYPE_ARM64_V8: 2112 outs() << " cputype CPU_TYPE_ARM64\n"; 2113 outs() << " cpusubtype CPU_SUBTYPE_ARM64_V8\n"; 2114 break; 2115 case MachO::CPU_SUBTYPE_ARM64E: 2116 outs() << " cputype CPU_TYPE_ARM64\n"; 2117 outs() << " cpusubtype CPU_SUBTYPE_ARM64E\n"; 2118 break; 2119 default: 2120 printUnknownCPUType(cputype, cpusubtype); 2121 break; 2122 } 2123 break; 2124 case MachO::CPU_TYPE_ARM64_32: 2125 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 2126 case MachO::CPU_SUBTYPE_ARM64_32_V8: 2127 outs() << " cputype CPU_TYPE_ARM64_32\n"; 2128 outs() << " cpusubtype CPU_SUBTYPE_ARM64_32_V8\n"; 2129 break; 2130 default: 2131 printUnknownCPUType(cputype, cpusubtype); 2132 break; 2133 } 2134 break; 2135 default: 2136 printUnknownCPUType(cputype, cpusubtype); 2137 break; 2138 } 2139 } 2140 2141 static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB, 2142 bool verbose) { 2143 outs() << "Fat headers\n"; 2144 if (verbose) { 2145 if (UB->getMagic() == MachO::FAT_MAGIC) 2146 outs() << "fat_magic FAT_MAGIC\n"; 2147 else // UB->getMagic() == MachO::FAT_MAGIC_64 2148 outs() << "fat_magic FAT_MAGIC_64\n"; 2149 } else 2150 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n"; 2151 2152 uint32_t nfat_arch = UB->getNumberOfObjects(); 2153 StringRef Buf = UB->getData(); 2154 uint64_t size = Buf.size(); 2155 uint64_t big_size = sizeof(struct MachO::fat_header) + 2156 nfat_arch * sizeof(struct MachO::fat_arch); 2157 outs() << "nfat_arch " << UB->getNumberOfObjects(); 2158 if (nfat_arch == 0) 2159 outs() << " (malformed, contains zero architecture types)\n"; 2160 else if (big_size > size) 2161 outs() << " (malformed, architectures past end of file)\n"; 2162 else 2163 outs() << "\n"; 2164 2165 for (uint32_t i = 0; i < nfat_arch; ++i) { 2166 MachOUniversalBinary::ObjectForArch OFA(UB, i); 2167 uint32_t cputype = OFA.getCPUType(); 2168 uint32_t cpusubtype = OFA.getCPUSubType(); 2169 outs() << "architecture "; 2170 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) { 2171 MachOUniversalBinary::ObjectForArch other_OFA(UB, j); 2172 uint32_t other_cputype = other_OFA.getCPUType(); 2173 uint32_t other_cpusubtype = other_OFA.getCPUSubType(); 2174 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype && 2175 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) == 2176 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) { 2177 outs() << "(illegal duplicate architecture) "; 2178 break; 2179 } 2180 } 2181 if (verbose) { 2182 outs() << OFA.getArchFlagName() << "\n"; 2183 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 2184 } else { 2185 outs() << i << "\n"; 2186 outs() << " cputype " << cputype << "\n"; 2187 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) 2188 << "\n"; 2189 } 2190 if (verbose && 2191 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) 2192 outs() << " capabilities CPU_SUBTYPE_LIB64\n"; 2193 else 2194 outs() << " capabilities " 2195 << format("0x%" PRIx32, 2196 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n"; 2197 outs() << " offset " << OFA.getOffset(); 2198 if (OFA.getOffset() > size) 2199 outs() << " (past end of file)"; 2200 if (OFA.getOffset() % (1ull << OFA.getAlign()) != 0) 2201 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")"; 2202 outs() << "\n"; 2203 outs() << " size " << OFA.getSize(); 2204 big_size = OFA.getOffset() + OFA.getSize(); 2205 if (big_size > size) 2206 outs() << " (past end of file)"; 2207 outs() << "\n"; 2208 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign()) 2209 << ")\n"; 2210 } 2211 } 2212 2213 static void printArchiveChild(StringRef Filename, const Archive::Child &C, 2214 size_t ChildIndex, bool verbose, 2215 bool print_offset, 2216 StringRef ArchitectureName = StringRef()) { 2217 if (print_offset) 2218 outs() << C.getChildOffset() << "\t"; 2219 sys::fs::perms Mode = 2220 unwrapOrError(C.getAccessMode(), getFileNameForError(C, ChildIndex), 2221 Filename, ArchitectureName); 2222 if (verbose) { 2223 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG. 2224 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG. 2225 outs() << "-"; 2226 outs() << ((Mode & sys::fs::owner_read) ? "r" : "-"); 2227 outs() << ((Mode & sys::fs::owner_write) ? "w" : "-"); 2228 outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-"); 2229 outs() << ((Mode & sys::fs::group_read) ? "r" : "-"); 2230 outs() << ((Mode & sys::fs::group_write) ? "w" : "-"); 2231 outs() << ((Mode & sys::fs::group_exe) ? "x" : "-"); 2232 outs() << ((Mode & sys::fs::others_read) ? "r" : "-"); 2233 outs() << ((Mode & sys::fs::others_write) ? "w" : "-"); 2234 outs() << ((Mode & sys::fs::others_exe) ? "x" : "-"); 2235 } else { 2236 outs() << format("0%o ", Mode); 2237 } 2238 2239 outs() << format("%3d/%-3d %5" PRId64 " ", 2240 unwrapOrError(C.getUID(), getFileNameForError(C, ChildIndex), 2241 Filename, ArchitectureName), 2242 unwrapOrError(C.getGID(), getFileNameForError(C, ChildIndex), 2243 Filename, ArchitectureName), 2244 unwrapOrError(C.getRawSize(), 2245 getFileNameForError(C, ChildIndex), Filename, 2246 ArchitectureName)); 2247 2248 StringRef RawLastModified = C.getRawLastModified(); 2249 if (verbose) { 2250 unsigned Seconds; 2251 if (RawLastModified.getAsInteger(10, Seconds)) 2252 outs() << "(date: \"" << RawLastModified 2253 << "\" contains non-decimal chars) "; 2254 else { 2255 // Since cime(3) returns a 26 character string of the form: 2256 // "Sun Sep 16 01:03:52 1973\n\0" 2257 // just print 24 characters. 2258 time_t t = Seconds; 2259 outs() << format("%.24s ", ctime(&t)); 2260 } 2261 } else { 2262 outs() << RawLastModified << " "; 2263 } 2264 2265 if (verbose) { 2266 Expected<StringRef> NameOrErr = C.getName(); 2267 if (!NameOrErr) { 2268 consumeError(NameOrErr.takeError()); 2269 outs() << unwrapOrError(C.getRawName(), 2270 getFileNameForError(C, ChildIndex), Filename, 2271 ArchitectureName) 2272 << "\n"; 2273 } else { 2274 StringRef Name = NameOrErr.get(); 2275 outs() << Name << "\n"; 2276 } 2277 } else { 2278 outs() << unwrapOrError(C.getRawName(), getFileNameForError(C, ChildIndex), 2279 Filename, ArchitectureName) 2280 << "\n"; 2281 } 2282 } 2283 2284 static void printArchiveHeaders(StringRef Filename, Archive *A, bool verbose, 2285 bool print_offset, 2286 StringRef ArchitectureName = StringRef()) { 2287 Error Err = Error::success(); 2288 size_t I = 0; 2289 for (const auto &C : A->children(Err, false)) 2290 printArchiveChild(Filename, C, I++, verbose, print_offset, 2291 ArchitectureName); 2292 2293 if (Err) 2294 reportError(std::move(Err), Filename, "", ArchitectureName); 2295 } 2296 2297 static bool ValidateArchFlags() { 2298 // Check for -arch all and verifiy the -arch flags are valid. 2299 for (unsigned i = 0; i < ArchFlags.size(); ++i) { 2300 if (ArchFlags[i] == "all") { 2301 ArchAll = true; 2302 } else { 2303 if (!MachOObjectFile::isValidArch(ArchFlags[i])) { 2304 WithColor::error(errs(), "llvm-objdump") 2305 << "unknown architecture named '" + ArchFlags[i] + 2306 "'for the -arch option\n"; 2307 return false; 2308 } 2309 } 2310 } 2311 return true; 2312 } 2313 2314 // ParseInputMachO() parses the named Mach-O file in Filename and handles the 2315 // -arch flags selecting just those slices as specified by them and also parses 2316 // archive files. Then for each individual Mach-O file ProcessMachO() is 2317 // called to process the file based on the command line options. 2318 void objdump::parseInputMachO(StringRef Filename) { 2319 if (!ValidateArchFlags()) 2320 return; 2321 2322 // Attempt to open the binary. 2323 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename); 2324 if (!BinaryOrErr) { 2325 if (Error E = isNotObjectErrorInvalidFileType(BinaryOrErr.takeError())) 2326 reportError(std::move(E), Filename); 2327 else 2328 outs() << Filename << ": is not an object file\n"; 2329 return; 2330 } 2331 Binary &Bin = *BinaryOrErr.get().getBinary(); 2332 2333 if (Archive *A = dyn_cast<Archive>(&Bin)) { 2334 outs() << "Archive : " << Filename << "\n"; 2335 if (ArchiveHeaders) 2336 printArchiveHeaders(Filename, A, !NonVerbose, ArchiveMemberOffsets); 2337 2338 Error Err = Error::success(); 2339 unsigned I = -1; 2340 for (auto &C : A->children(Err)) { 2341 ++I; 2342 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary(); 2343 if (!ChildOrErr) { 2344 if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) 2345 reportError(std::move(E), getFileNameForError(C, I), Filename); 2346 continue; 2347 } 2348 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) { 2349 if (!checkMachOAndArchFlags(O, Filename)) 2350 return; 2351 ProcessMachO(Filename, O, O->getFileName()); 2352 } 2353 } 2354 if (Err) 2355 reportError(std::move(Err), Filename); 2356 return; 2357 } 2358 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) { 2359 parseInputMachO(UB); 2360 return; 2361 } 2362 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) { 2363 if (!checkMachOAndArchFlags(O, Filename)) 2364 return; 2365 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) 2366 ProcessMachO(Filename, MachOOF); 2367 else 2368 WithColor::error(errs(), "llvm-objdump") 2369 << Filename << "': " 2370 << "object is not a Mach-O file type.\n"; 2371 return; 2372 } 2373 llvm_unreachable("Input object can't be invalid at this point"); 2374 } 2375 2376 void objdump::parseInputMachO(MachOUniversalBinary *UB) { 2377 if (!ValidateArchFlags()) 2378 return; 2379 2380 auto Filename = UB->getFileName(); 2381 2382 if (UniversalHeaders) 2383 printMachOUniversalHeaders(UB, !NonVerbose); 2384 2385 // If we have a list of architecture flags specified dump only those. 2386 if (!ArchAll && !ArchFlags.empty()) { 2387 // Look for a slice in the universal binary that matches each ArchFlag. 2388 bool ArchFound; 2389 for (unsigned i = 0; i < ArchFlags.size(); ++i) { 2390 ArchFound = false; 2391 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(), 2392 E = UB->end_objects(); 2393 I != E; ++I) { 2394 if (ArchFlags[i] == I->getArchFlagName()) { 2395 ArchFound = true; 2396 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = 2397 I->getAsObjectFile(); 2398 std::string ArchitectureName; 2399 if (ArchFlags.size() > 1) 2400 ArchitectureName = I->getArchFlagName(); 2401 if (ObjOrErr) { 2402 ObjectFile &O = *ObjOrErr.get(); 2403 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O)) 2404 ProcessMachO(Filename, MachOOF, "", ArchitectureName); 2405 } else if (Error E = isNotObjectErrorInvalidFileType( 2406 ObjOrErr.takeError())) { 2407 reportError(std::move(E), "", Filename, ArchitectureName); 2408 continue; 2409 } else if (Expected<std::unique_ptr<Archive>> AOrErr = 2410 I->getAsArchive()) { 2411 std::unique_ptr<Archive> &A = *AOrErr; 2412 outs() << "Archive : " << Filename; 2413 if (!ArchitectureName.empty()) 2414 outs() << " (architecture " << ArchitectureName << ")"; 2415 outs() << "\n"; 2416 if (ArchiveHeaders) 2417 printArchiveHeaders(Filename, A.get(), !NonVerbose, 2418 ArchiveMemberOffsets, ArchitectureName); 2419 Error Err = Error::success(); 2420 unsigned I = -1; 2421 for (auto &C : A->children(Err)) { 2422 ++I; 2423 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary(); 2424 if (!ChildOrErr) { 2425 if (Error E = 2426 isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) 2427 reportError(std::move(E), getFileNameForError(C, I), Filename, 2428 ArchitectureName); 2429 continue; 2430 } 2431 if (MachOObjectFile *O = 2432 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) 2433 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName); 2434 } 2435 if (Err) 2436 reportError(std::move(Err), Filename); 2437 } else { 2438 consumeError(AOrErr.takeError()); 2439 reportError(Filename, 2440 "Mach-O universal file for architecture " + 2441 StringRef(I->getArchFlagName()) + 2442 " is not a Mach-O file or an archive file"); 2443 } 2444 } 2445 } 2446 if (!ArchFound) { 2447 WithColor::error(errs(), "llvm-objdump") 2448 << "file: " + Filename + " does not contain " 2449 << "architecture: " + ArchFlags[i] + "\n"; 2450 return; 2451 } 2452 } 2453 return; 2454 } 2455 // No architecture flags were specified so if this contains a slice that 2456 // matches the host architecture dump only that. 2457 if (!ArchAll) { 2458 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(), 2459 E = UB->end_objects(); 2460 I != E; ++I) { 2461 if (MachOObjectFile::getHostArch().getArchName() == 2462 I->getArchFlagName()) { 2463 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile(); 2464 std::string ArchiveName; 2465 ArchiveName.clear(); 2466 if (ObjOrErr) { 2467 ObjectFile &O = *ObjOrErr.get(); 2468 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O)) 2469 ProcessMachO(Filename, MachOOF); 2470 } else if (Error E = 2471 isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) { 2472 reportError(std::move(E), Filename); 2473 } else if (Expected<std::unique_ptr<Archive>> AOrErr = 2474 I->getAsArchive()) { 2475 std::unique_ptr<Archive> &A = *AOrErr; 2476 outs() << "Archive : " << Filename << "\n"; 2477 if (ArchiveHeaders) 2478 printArchiveHeaders(Filename, A.get(), !NonVerbose, 2479 ArchiveMemberOffsets); 2480 Error Err = Error::success(); 2481 unsigned I = -1; 2482 for (auto &C : A->children(Err)) { 2483 ++I; 2484 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary(); 2485 if (!ChildOrErr) { 2486 if (Error E = 2487 isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) 2488 reportError(std::move(E), getFileNameForError(C, I), Filename); 2489 continue; 2490 } 2491 if (MachOObjectFile *O = 2492 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) 2493 ProcessMachO(Filename, O, O->getFileName()); 2494 } 2495 if (Err) 2496 reportError(std::move(Err), Filename); 2497 } else { 2498 consumeError(AOrErr.takeError()); 2499 reportError(Filename, "Mach-O universal file for architecture " + 2500 StringRef(I->getArchFlagName()) + 2501 " is not a Mach-O file or an archive file"); 2502 } 2503 return; 2504 } 2505 } 2506 } 2507 // Either all architectures have been specified or none have been specified 2508 // and this does not contain the host architecture so dump all the slices. 2509 bool moreThanOneArch = UB->getNumberOfObjects() > 1; 2510 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(), 2511 E = UB->end_objects(); 2512 I != E; ++I) { 2513 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile(); 2514 std::string ArchitectureName; 2515 if (moreThanOneArch) 2516 ArchitectureName = I->getArchFlagName(); 2517 if (ObjOrErr) { 2518 ObjectFile &Obj = *ObjOrErr.get(); 2519 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj)) 2520 ProcessMachO(Filename, MachOOF, "", ArchitectureName); 2521 } else if (Error E = 2522 isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) { 2523 reportError(std::move(E), Filename, "", ArchitectureName); 2524 } else if (Expected<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) { 2525 std::unique_ptr<Archive> &A = *AOrErr; 2526 outs() << "Archive : " << Filename; 2527 if (!ArchitectureName.empty()) 2528 outs() << " (architecture " << ArchitectureName << ")"; 2529 outs() << "\n"; 2530 if (ArchiveHeaders) 2531 printArchiveHeaders(Filename, A.get(), !NonVerbose, 2532 ArchiveMemberOffsets, ArchitectureName); 2533 Error Err = Error::success(); 2534 unsigned I = -1; 2535 for (auto &C : A->children(Err)) { 2536 ++I; 2537 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary(); 2538 if (!ChildOrErr) { 2539 if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) 2540 reportError(std::move(E), getFileNameForError(C, I), Filename, 2541 ArchitectureName); 2542 continue; 2543 } 2544 if (MachOObjectFile *O = 2545 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) { 2546 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O)) 2547 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(), 2548 ArchitectureName); 2549 } 2550 } 2551 if (Err) 2552 reportError(std::move(Err), Filename); 2553 } else { 2554 consumeError(AOrErr.takeError()); 2555 reportError(Filename, "Mach-O universal file for architecture " + 2556 StringRef(I->getArchFlagName()) + 2557 " is not a Mach-O file or an archive file"); 2558 } 2559 } 2560 } 2561 2562 namespace { 2563 // The block of info used by the Symbolizer call backs. 2564 struct DisassembleInfo { 2565 DisassembleInfo(MachOObjectFile *O, SymbolAddressMap *AddrMap, 2566 std::vector<SectionRef> *Sections, bool verbose) 2567 : verbose(verbose), O(O), AddrMap(AddrMap), Sections(Sections) {} 2568 bool verbose; 2569 MachOObjectFile *O; 2570 SectionRef S; 2571 SymbolAddressMap *AddrMap; 2572 std::vector<SectionRef> *Sections; 2573 const char *class_name = nullptr; 2574 const char *selector_name = nullptr; 2575 std::unique_ptr<char[]> method = nullptr; 2576 char *demangled_name = nullptr; 2577 uint64_t adrp_addr = 0; 2578 uint32_t adrp_inst = 0; 2579 std::unique_ptr<SymbolAddressMap> bindtable; 2580 uint32_t depth = 0; 2581 }; 2582 } // namespace 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 = cantFail(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 namespace { 3392 3393 // These are structs in the Objective-C meta data and read to produce the 3394 // comments for disassembly. While these are part of the ABI they are no 3395 // public defintions. So the are here not in include/llvm/BinaryFormat/MachO.h 3396 // . 3397 3398 // The cfstring object in a 64-bit Mach-O file. 3399 struct cfstring64_t { 3400 uint64_t isa; // class64_t * (64-bit pointer) 3401 uint64_t flags; // flag bits 3402 uint64_t characters; // char * (64-bit pointer) 3403 uint64_t length; // number of non-NULL characters in above 3404 }; 3405 3406 // The class object in a 64-bit Mach-O file. 3407 struct class64_t { 3408 uint64_t isa; // class64_t * (64-bit pointer) 3409 uint64_t superclass; // class64_t * (64-bit pointer) 3410 uint64_t cache; // Cache (64-bit pointer) 3411 uint64_t vtable; // IMP * (64-bit pointer) 3412 uint64_t data; // class_ro64_t * (64-bit pointer) 3413 }; 3414 3415 struct class32_t { 3416 uint32_t isa; /* class32_t * (32-bit pointer) */ 3417 uint32_t superclass; /* class32_t * (32-bit pointer) */ 3418 uint32_t cache; /* Cache (32-bit pointer) */ 3419 uint32_t vtable; /* IMP * (32-bit pointer) */ 3420 uint32_t data; /* class_ro32_t * (32-bit pointer) */ 3421 }; 3422 3423 struct class_ro64_t { 3424 uint32_t flags; 3425 uint32_t instanceStart; 3426 uint32_t instanceSize; 3427 uint32_t reserved; 3428 uint64_t ivarLayout; // const uint8_t * (64-bit pointer) 3429 uint64_t name; // const char * (64-bit pointer) 3430 uint64_t baseMethods; // const method_list_t * (64-bit pointer) 3431 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer) 3432 uint64_t ivars; // const ivar_list_t * (64-bit pointer) 3433 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer) 3434 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer) 3435 }; 3436 3437 struct class_ro32_t { 3438 uint32_t flags; 3439 uint32_t instanceStart; 3440 uint32_t instanceSize; 3441 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */ 3442 uint32_t name; /* const char * (32-bit pointer) */ 3443 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */ 3444 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */ 3445 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */ 3446 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */ 3447 uint32_t baseProperties; /* const struct objc_property_list * 3448 (32-bit pointer) */ 3449 }; 3450 3451 /* Values for class_ro{64,32}_t->flags */ 3452 #define RO_META (1 << 0) 3453 #define RO_ROOT (1 << 1) 3454 #define RO_HAS_CXX_STRUCTORS (1 << 2) 3455 3456 struct method_list64_t { 3457 uint32_t entsize; 3458 uint32_t count; 3459 /* struct method64_t first; These structures follow inline */ 3460 }; 3461 3462 struct method_list32_t { 3463 uint32_t entsize; 3464 uint32_t count; 3465 /* struct method32_t first; These structures follow inline */ 3466 }; 3467 3468 struct method64_t { 3469 uint64_t name; /* SEL (64-bit pointer) */ 3470 uint64_t types; /* const char * (64-bit pointer) */ 3471 uint64_t imp; /* IMP (64-bit pointer) */ 3472 }; 3473 3474 struct method32_t { 3475 uint32_t name; /* SEL (32-bit pointer) */ 3476 uint32_t types; /* const char * (32-bit pointer) */ 3477 uint32_t imp; /* IMP (32-bit pointer) */ 3478 }; 3479 3480 struct protocol_list64_t { 3481 uint64_t count; /* uintptr_t (a 64-bit value) */ 3482 /* struct protocol64_t * list[0]; These pointers follow inline */ 3483 }; 3484 3485 struct protocol_list32_t { 3486 uint32_t count; /* uintptr_t (a 32-bit value) */ 3487 /* struct protocol32_t * list[0]; These pointers follow inline */ 3488 }; 3489 3490 struct protocol64_t { 3491 uint64_t isa; /* id * (64-bit pointer) */ 3492 uint64_t name; /* const char * (64-bit pointer) */ 3493 uint64_t protocols; /* struct protocol_list64_t * 3494 (64-bit pointer) */ 3495 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */ 3496 uint64_t classMethods; /* method_list_t * (64-bit pointer) */ 3497 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */ 3498 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */ 3499 uint64_t instanceProperties; /* struct objc_property_list * 3500 (64-bit pointer) */ 3501 }; 3502 3503 struct protocol32_t { 3504 uint32_t isa; /* id * (32-bit pointer) */ 3505 uint32_t name; /* const char * (32-bit pointer) */ 3506 uint32_t protocols; /* struct protocol_list_t * 3507 (32-bit pointer) */ 3508 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */ 3509 uint32_t classMethods; /* method_list_t * (32-bit pointer) */ 3510 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */ 3511 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */ 3512 uint32_t instanceProperties; /* struct objc_property_list * 3513 (32-bit pointer) */ 3514 }; 3515 3516 struct ivar_list64_t { 3517 uint32_t entsize; 3518 uint32_t count; 3519 /* struct ivar64_t first; These structures follow inline */ 3520 }; 3521 3522 struct ivar_list32_t { 3523 uint32_t entsize; 3524 uint32_t count; 3525 /* struct ivar32_t first; These structures follow inline */ 3526 }; 3527 3528 struct ivar64_t { 3529 uint64_t offset; /* uintptr_t * (64-bit pointer) */ 3530 uint64_t name; /* const char * (64-bit pointer) */ 3531 uint64_t type; /* const char * (64-bit pointer) */ 3532 uint32_t alignment; 3533 uint32_t size; 3534 }; 3535 3536 struct ivar32_t { 3537 uint32_t offset; /* uintptr_t * (32-bit pointer) */ 3538 uint32_t name; /* const char * (32-bit pointer) */ 3539 uint32_t type; /* const char * (32-bit pointer) */ 3540 uint32_t alignment; 3541 uint32_t size; 3542 }; 3543 3544 struct objc_property_list64 { 3545 uint32_t entsize; 3546 uint32_t count; 3547 /* struct objc_property64 first; These structures follow inline */ 3548 }; 3549 3550 struct objc_property_list32 { 3551 uint32_t entsize; 3552 uint32_t count; 3553 /* struct objc_property32 first; These structures follow inline */ 3554 }; 3555 3556 struct objc_property64 { 3557 uint64_t name; /* const char * (64-bit pointer) */ 3558 uint64_t attributes; /* const char * (64-bit pointer) */ 3559 }; 3560 3561 struct objc_property32 { 3562 uint32_t name; /* const char * (32-bit pointer) */ 3563 uint32_t attributes; /* const char * (32-bit pointer) */ 3564 }; 3565 3566 struct category64_t { 3567 uint64_t name; /* const char * (64-bit pointer) */ 3568 uint64_t cls; /* struct class_t * (64-bit pointer) */ 3569 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */ 3570 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */ 3571 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */ 3572 uint64_t instanceProperties; /* struct objc_property_list * 3573 (64-bit pointer) */ 3574 }; 3575 3576 struct category32_t { 3577 uint32_t name; /* const char * (32-bit pointer) */ 3578 uint32_t cls; /* struct class_t * (32-bit pointer) */ 3579 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */ 3580 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */ 3581 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */ 3582 uint32_t instanceProperties; /* struct objc_property_list * 3583 (32-bit pointer) */ 3584 }; 3585 3586 struct objc_image_info64 { 3587 uint32_t version; 3588 uint32_t flags; 3589 }; 3590 struct objc_image_info32 { 3591 uint32_t version; 3592 uint32_t flags; 3593 }; 3594 struct imageInfo_t { 3595 uint32_t version; 3596 uint32_t flags; 3597 }; 3598 /* masks for objc_image_info.flags */ 3599 #define OBJC_IMAGE_IS_REPLACEMENT (1 << 0) 3600 #define OBJC_IMAGE_SUPPORTS_GC (1 << 1) 3601 #define OBJC_IMAGE_IS_SIMULATED (1 << 5) 3602 #define OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES (1 << 6) 3603 3604 struct message_ref64 { 3605 uint64_t imp; /* IMP (64-bit pointer) */ 3606 uint64_t sel; /* SEL (64-bit pointer) */ 3607 }; 3608 3609 struct message_ref32 { 3610 uint32_t imp; /* IMP (32-bit pointer) */ 3611 uint32_t sel; /* SEL (32-bit pointer) */ 3612 }; 3613 3614 // Objective-C 1 (32-bit only) meta data structs. 3615 3616 struct objc_module_t { 3617 uint32_t version; 3618 uint32_t size; 3619 uint32_t name; /* char * (32-bit pointer) */ 3620 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */ 3621 }; 3622 3623 struct objc_symtab_t { 3624 uint32_t sel_ref_cnt; 3625 uint32_t refs; /* SEL * (32-bit pointer) */ 3626 uint16_t cls_def_cnt; 3627 uint16_t cat_def_cnt; 3628 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */ 3629 }; 3630 3631 struct objc_class_t { 3632 uint32_t isa; /* struct objc_class * (32-bit pointer) */ 3633 uint32_t super_class; /* struct objc_class * (32-bit pointer) */ 3634 uint32_t name; /* const char * (32-bit pointer) */ 3635 int32_t version; 3636 int32_t info; 3637 int32_t instance_size; 3638 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */ 3639 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */ 3640 uint32_t cache; /* struct objc_cache * (32-bit pointer) */ 3641 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */ 3642 }; 3643 3644 #define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask)) 3645 // class is not a metaclass 3646 #define CLS_CLASS 0x1 3647 // class is a metaclass 3648 #define CLS_META 0x2 3649 3650 struct objc_category_t { 3651 uint32_t category_name; /* char * (32-bit pointer) */ 3652 uint32_t class_name; /* char * (32-bit pointer) */ 3653 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */ 3654 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */ 3655 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */ 3656 }; 3657 3658 struct objc_ivar_t { 3659 uint32_t ivar_name; /* char * (32-bit pointer) */ 3660 uint32_t ivar_type; /* char * (32-bit pointer) */ 3661 int32_t ivar_offset; 3662 }; 3663 3664 struct objc_ivar_list_t { 3665 int32_t ivar_count; 3666 // struct objc_ivar_t ivar_list[1]; /* variable length structure */ 3667 }; 3668 3669 struct objc_method_list_t { 3670 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */ 3671 int32_t method_count; 3672 // struct objc_method_t method_list[1]; /* variable length structure */ 3673 }; 3674 3675 struct objc_method_t { 3676 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */ 3677 uint32_t method_types; /* char * (32-bit pointer) */ 3678 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...) 3679 (32-bit pointer) */ 3680 }; 3681 3682 struct objc_protocol_list_t { 3683 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */ 3684 int32_t count; 3685 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t * 3686 // (32-bit pointer) */ 3687 }; 3688 3689 struct objc_protocol_t { 3690 uint32_t isa; /* struct objc_class * (32-bit pointer) */ 3691 uint32_t protocol_name; /* char * (32-bit pointer) */ 3692 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */ 3693 uint32_t instance_methods; /* struct objc_method_description_list * 3694 (32-bit pointer) */ 3695 uint32_t class_methods; /* struct objc_method_description_list * 3696 (32-bit pointer) */ 3697 }; 3698 3699 struct objc_method_description_list_t { 3700 int32_t count; 3701 // struct objc_method_description_t list[1]; 3702 }; 3703 3704 struct objc_method_description_t { 3705 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */ 3706 uint32_t types; /* char * (32-bit pointer) */ 3707 }; 3708 3709 inline void swapStruct(struct cfstring64_t &cfs) { 3710 sys::swapByteOrder(cfs.isa); 3711 sys::swapByteOrder(cfs.flags); 3712 sys::swapByteOrder(cfs.characters); 3713 sys::swapByteOrder(cfs.length); 3714 } 3715 3716 inline void swapStruct(struct class64_t &c) { 3717 sys::swapByteOrder(c.isa); 3718 sys::swapByteOrder(c.superclass); 3719 sys::swapByteOrder(c.cache); 3720 sys::swapByteOrder(c.vtable); 3721 sys::swapByteOrder(c.data); 3722 } 3723 3724 inline void swapStruct(struct class32_t &c) { 3725 sys::swapByteOrder(c.isa); 3726 sys::swapByteOrder(c.superclass); 3727 sys::swapByteOrder(c.cache); 3728 sys::swapByteOrder(c.vtable); 3729 sys::swapByteOrder(c.data); 3730 } 3731 3732 inline void swapStruct(struct class_ro64_t &cro) { 3733 sys::swapByteOrder(cro.flags); 3734 sys::swapByteOrder(cro.instanceStart); 3735 sys::swapByteOrder(cro.instanceSize); 3736 sys::swapByteOrder(cro.reserved); 3737 sys::swapByteOrder(cro.ivarLayout); 3738 sys::swapByteOrder(cro.name); 3739 sys::swapByteOrder(cro.baseMethods); 3740 sys::swapByteOrder(cro.baseProtocols); 3741 sys::swapByteOrder(cro.ivars); 3742 sys::swapByteOrder(cro.weakIvarLayout); 3743 sys::swapByteOrder(cro.baseProperties); 3744 } 3745 3746 inline void swapStruct(struct class_ro32_t &cro) { 3747 sys::swapByteOrder(cro.flags); 3748 sys::swapByteOrder(cro.instanceStart); 3749 sys::swapByteOrder(cro.instanceSize); 3750 sys::swapByteOrder(cro.ivarLayout); 3751 sys::swapByteOrder(cro.name); 3752 sys::swapByteOrder(cro.baseMethods); 3753 sys::swapByteOrder(cro.baseProtocols); 3754 sys::swapByteOrder(cro.ivars); 3755 sys::swapByteOrder(cro.weakIvarLayout); 3756 sys::swapByteOrder(cro.baseProperties); 3757 } 3758 3759 inline void swapStruct(struct method_list64_t &ml) { 3760 sys::swapByteOrder(ml.entsize); 3761 sys::swapByteOrder(ml.count); 3762 } 3763 3764 inline void swapStruct(struct method_list32_t &ml) { 3765 sys::swapByteOrder(ml.entsize); 3766 sys::swapByteOrder(ml.count); 3767 } 3768 3769 inline void swapStruct(struct method64_t &m) { 3770 sys::swapByteOrder(m.name); 3771 sys::swapByteOrder(m.types); 3772 sys::swapByteOrder(m.imp); 3773 } 3774 3775 inline void swapStruct(struct method32_t &m) { 3776 sys::swapByteOrder(m.name); 3777 sys::swapByteOrder(m.types); 3778 sys::swapByteOrder(m.imp); 3779 } 3780 3781 inline void swapStruct(struct protocol_list64_t &pl) { 3782 sys::swapByteOrder(pl.count); 3783 } 3784 3785 inline void swapStruct(struct protocol_list32_t &pl) { 3786 sys::swapByteOrder(pl.count); 3787 } 3788 3789 inline void swapStruct(struct protocol64_t &p) { 3790 sys::swapByteOrder(p.isa); 3791 sys::swapByteOrder(p.name); 3792 sys::swapByteOrder(p.protocols); 3793 sys::swapByteOrder(p.instanceMethods); 3794 sys::swapByteOrder(p.classMethods); 3795 sys::swapByteOrder(p.optionalInstanceMethods); 3796 sys::swapByteOrder(p.optionalClassMethods); 3797 sys::swapByteOrder(p.instanceProperties); 3798 } 3799 3800 inline void swapStruct(struct protocol32_t &p) { 3801 sys::swapByteOrder(p.isa); 3802 sys::swapByteOrder(p.name); 3803 sys::swapByteOrder(p.protocols); 3804 sys::swapByteOrder(p.instanceMethods); 3805 sys::swapByteOrder(p.classMethods); 3806 sys::swapByteOrder(p.optionalInstanceMethods); 3807 sys::swapByteOrder(p.optionalClassMethods); 3808 sys::swapByteOrder(p.instanceProperties); 3809 } 3810 3811 inline void swapStruct(struct ivar_list64_t &il) { 3812 sys::swapByteOrder(il.entsize); 3813 sys::swapByteOrder(il.count); 3814 } 3815 3816 inline void swapStruct(struct ivar_list32_t &il) { 3817 sys::swapByteOrder(il.entsize); 3818 sys::swapByteOrder(il.count); 3819 } 3820 3821 inline void swapStruct(struct ivar64_t &i) { 3822 sys::swapByteOrder(i.offset); 3823 sys::swapByteOrder(i.name); 3824 sys::swapByteOrder(i.type); 3825 sys::swapByteOrder(i.alignment); 3826 sys::swapByteOrder(i.size); 3827 } 3828 3829 inline void swapStruct(struct ivar32_t &i) { 3830 sys::swapByteOrder(i.offset); 3831 sys::swapByteOrder(i.name); 3832 sys::swapByteOrder(i.type); 3833 sys::swapByteOrder(i.alignment); 3834 sys::swapByteOrder(i.size); 3835 } 3836 3837 inline void swapStruct(struct objc_property_list64 &pl) { 3838 sys::swapByteOrder(pl.entsize); 3839 sys::swapByteOrder(pl.count); 3840 } 3841 3842 inline void swapStruct(struct objc_property_list32 &pl) { 3843 sys::swapByteOrder(pl.entsize); 3844 sys::swapByteOrder(pl.count); 3845 } 3846 3847 inline void swapStruct(struct objc_property64 &op) { 3848 sys::swapByteOrder(op.name); 3849 sys::swapByteOrder(op.attributes); 3850 } 3851 3852 inline void swapStruct(struct objc_property32 &op) { 3853 sys::swapByteOrder(op.name); 3854 sys::swapByteOrder(op.attributes); 3855 } 3856 3857 inline void swapStruct(struct category64_t &c) { 3858 sys::swapByteOrder(c.name); 3859 sys::swapByteOrder(c.cls); 3860 sys::swapByteOrder(c.instanceMethods); 3861 sys::swapByteOrder(c.classMethods); 3862 sys::swapByteOrder(c.protocols); 3863 sys::swapByteOrder(c.instanceProperties); 3864 } 3865 3866 inline void swapStruct(struct category32_t &c) { 3867 sys::swapByteOrder(c.name); 3868 sys::swapByteOrder(c.cls); 3869 sys::swapByteOrder(c.instanceMethods); 3870 sys::swapByteOrder(c.classMethods); 3871 sys::swapByteOrder(c.protocols); 3872 sys::swapByteOrder(c.instanceProperties); 3873 } 3874 3875 inline void swapStruct(struct objc_image_info64 &o) { 3876 sys::swapByteOrder(o.version); 3877 sys::swapByteOrder(o.flags); 3878 } 3879 3880 inline void swapStruct(struct objc_image_info32 &o) { 3881 sys::swapByteOrder(o.version); 3882 sys::swapByteOrder(o.flags); 3883 } 3884 3885 inline void swapStruct(struct imageInfo_t &o) { 3886 sys::swapByteOrder(o.version); 3887 sys::swapByteOrder(o.flags); 3888 } 3889 3890 inline void swapStruct(struct message_ref64 &mr) { 3891 sys::swapByteOrder(mr.imp); 3892 sys::swapByteOrder(mr.sel); 3893 } 3894 3895 inline void swapStruct(struct message_ref32 &mr) { 3896 sys::swapByteOrder(mr.imp); 3897 sys::swapByteOrder(mr.sel); 3898 } 3899 3900 inline void swapStruct(struct objc_module_t &module) { 3901 sys::swapByteOrder(module.version); 3902 sys::swapByteOrder(module.size); 3903 sys::swapByteOrder(module.name); 3904 sys::swapByteOrder(module.symtab); 3905 } 3906 3907 inline void swapStruct(struct objc_symtab_t &symtab) { 3908 sys::swapByteOrder(symtab.sel_ref_cnt); 3909 sys::swapByteOrder(symtab.refs); 3910 sys::swapByteOrder(symtab.cls_def_cnt); 3911 sys::swapByteOrder(symtab.cat_def_cnt); 3912 } 3913 3914 inline void swapStruct(struct objc_class_t &objc_class) { 3915 sys::swapByteOrder(objc_class.isa); 3916 sys::swapByteOrder(objc_class.super_class); 3917 sys::swapByteOrder(objc_class.name); 3918 sys::swapByteOrder(objc_class.version); 3919 sys::swapByteOrder(objc_class.info); 3920 sys::swapByteOrder(objc_class.instance_size); 3921 sys::swapByteOrder(objc_class.ivars); 3922 sys::swapByteOrder(objc_class.methodLists); 3923 sys::swapByteOrder(objc_class.cache); 3924 sys::swapByteOrder(objc_class.protocols); 3925 } 3926 3927 inline void swapStruct(struct objc_category_t &objc_category) { 3928 sys::swapByteOrder(objc_category.category_name); 3929 sys::swapByteOrder(objc_category.class_name); 3930 sys::swapByteOrder(objc_category.instance_methods); 3931 sys::swapByteOrder(objc_category.class_methods); 3932 sys::swapByteOrder(objc_category.protocols); 3933 } 3934 3935 inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) { 3936 sys::swapByteOrder(objc_ivar_list.ivar_count); 3937 } 3938 3939 inline void swapStruct(struct objc_ivar_t &objc_ivar) { 3940 sys::swapByteOrder(objc_ivar.ivar_name); 3941 sys::swapByteOrder(objc_ivar.ivar_type); 3942 sys::swapByteOrder(objc_ivar.ivar_offset); 3943 } 3944 3945 inline void swapStruct(struct objc_method_list_t &method_list) { 3946 sys::swapByteOrder(method_list.obsolete); 3947 sys::swapByteOrder(method_list.method_count); 3948 } 3949 3950 inline void swapStruct(struct objc_method_t &method) { 3951 sys::swapByteOrder(method.method_name); 3952 sys::swapByteOrder(method.method_types); 3953 sys::swapByteOrder(method.method_imp); 3954 } 3955 3956 inline void swapStruct(struct objc_protocol_list_t &protocol_list) { 3957 sys::swapByteOrder(protocol_list.next); 3958 sys::swapByteOrder(protocol_list.count); 3959 } 3960 3961 inline void swapStruct(struct objc_protocol_t &protocol) { 3962 sys::swapByteOrder(protocol.isa); 3963 sys::swapByteOrder(protocol.protocol_name); 3964 sys::swapByteOrder(protocol.protocol_list); 3965 sys::swapByteOrder(protocol.instance_methods); 3966 sys::swapByteOrder(protocol.class_methods); 3967 } 3968 3969 inline void swapStruct(struct objc_method_description_list_t &mdl) { 3970 sys::swapByteOrder(mdl.count); 3971 } 3972 3973 inline void swapStruct(struct objc_method_description_t &md) { 3974 sys::swapByteOrder(md.name); 3975 sys::swapByteOrder(md.types); 3976 } 3977 3978 } // namespace 3979 3980 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue, 3981 struct DisassembleInfo *info); 3982 3983 // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer 3984 // to an Objective-C class and returns the class name. It is also passed the 3985 // address of the pointer, so when the pointer is zero as it can be in an .o 3986 // file, that is used to look for an external relocation entry with a symbol 3987 // name. 3988 static const char *get_objc2_64bit_class_name(uint64_t pointer_value, 3989 uint64_t ReferenceValue, 3990 struct DisassembleInfo *info) { 3991 const char *r; 3992 uint32_t offset, left; 3993 SectionRef S; 3994 3995 // The pointer_value can be 0 in an object file and have a relocation 3996 // entry for the class symbol at the ReferenceValue (the address of the 3997 // pointer). 3998 if (pointer_value == 0) { 3999 r = get_pointer_64(ReferenceValue, offset, left, S, info); 4000 if (r == nullptr || left < sizeof(uint64_t)) 4001 return nullptr; 4002 uint64_t n_value; 4003 const char *symbol_name = get_symbol_64(offset, S, info, n_value); 4004 if (symbol_name == nullptr) 4005 return nullptr; 4006 const char *class_name = strrchr(symbol_name, '$'); 4007 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0') 4008 return class_name + 2; 4009 else 4010 return nullptr; 4011 } 4012 4013 // The case were the pointer_value is non-zero and points to a class defined 4014 // in this Mach-O file. 4015 r = get_pointer_64(pointer_value, offset, left, S, info); 4016 if (r == nullptr || left < sizeof(struct class64_t)) 4017 return nullptr; 4018 struct class64_t c; 4019 memcpy(&c, r, sizeof(struct class64_t)); 4020 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4021 swapStruct(c); 4022 if (c.data == 0) 4023 return nullptr; 4024 r = get_pointer_64(c.data, offset, left, S, info); 4025 if (r == nullptr || left < sizeof(struct class_ro64_t)) 4026 return nullptr; 4027 struct class_ro64_t cro; 4028 memcpy(&cro, r, sizeof(struct class_ro64_t)); 4029 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4030 swapStruct(cro); 4031 if (cro.name == 0) 4032 return nullptr; 4033 const char *name = get_pointer_64(cro.name, offset, left, S, info); 4034 return name; 4035 } 4036 4037 // get_objc2_64bit_cfstring_name is used for disassembly and is passed a 4038 // pointer to a cfstring and returns its name or nullptr. 4039 static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue, 4040 struct DisassembleInfo *info) { 4041 const char *r, *name; 4042 uint32_t offset, left; 4043 SectionRef S; 4044 struct cfstring64_t cfs; 4045 uint64_t cfs_characters; 4046 4047 r = get_pointer_64(ReferenceValue, offset, left, S, info); 4048 if (r == nullptr || left < sizeof(struct cfstring64_t)) 4049 return nullptr; 4050 memcpy(&cfs, r, sizeof(struct cfstring64_t)); 4051 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4052 swapStruct(cfs); 4053 if (cfs.characters == 0) { 4054 uint64_t n_value; 4055 const char *symbol_name = get_symbol_64( 4056 offset + offsetof(struct cfstring64_t, characters), S, info, n_value); 4057 if (symbol_name == nullptr) 4058 return nullptr; 4059 cfs_characters = n_value; 4060 } else 4061 cfs_characters = cfs.characters; 4062 name = get_pointer_64(cfs_characters, offset, left, S, info); 4063 4064 return name; 4065 } 4066 4067 // get_objc2_64bit_selref() is used for disassembly and is passed a the address 4068 // of a pointer to an Objective-C selector reference when the pointer value is 4069 // zero as in a .o file and is likely to have a external relocation entry with 4070 // who's symbol's n_value is the real pointer to the selector name. If that is 4071 // the case the real pointer to the selector name is returned else 0 is 4072 // returned 4073 static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue, 4074 struct DisassembleInfo *info) { 4075 uint32_t offset, left; 4076 SectionRef S; 4077 4078 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info); 4079 if (r == nullptr || left < sizeof(uint64_t)) 4080 return 0; 4081 uint64_t n_value; 4082 const char *symbol_name = get_symbol_64(offset, S, info, n_value); 4083 if (symbol_name == nullptr) 4084 return 0; 4085 return n_value; 4086 } 4087 4088 static const SectionRef get_section(MachOObjectFile *O, const char *segname, 4089 const char *sectname) { 4090 for (const SectionRef &Section : O->sections()) { 4091 StringRef SectName; 4092 Expected<StringRef> SecNameOrErr = Section.getName(); 4093 if (SecNameOrErr) 4094 SectName = *SecNameOrErr; 4095 else 4096 consumeError(SecNameOrErr.takeError()); 4097 4098 DataRefImpl Ref = Section.getRawDataRefImpl(); 4099 StringRef SegName = O->getSectionFinalSegmentName(Ref); 4100 if (SegName == segname && SectName == sectname) 4101 return Section; 4102 } 4103 return SectionRef(); 4104 } 4105 4106 static void 4107 walk_pointer_list_64(const char *listname, const SectionRef S, 4108 MachOObjectFile *O, struct DisassembleInfo *info, 4109 void (*func)(uint64_t, struct DisassembleInfo *info)) { 4110 if (S == SectionRef()) 4111 return; 4112 4113 StringRef SectName; 4114 Expected<StringRef> SecNameOrErr = S.getName(); 4115 if (SecNameOrErr) 4116 SectName = *SecNameOrErr; 4117 else 4118 consumeError(SecNameOrErr.takeError()); 4119 4120 DataRefImpl Ref = S.getRawDataRefImpl(); 4121 StringRef SegName = O->getSectionFinalSegmentName(Ref); 4122 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 4123 4124 StringRef BytesStr = unwrapOrError(S.getContents(), O->getFileName()); 4125 const char *Contents = reinterpret_cast<const char *>(BytesStr.data()); 4126 4127 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) { 4128 uint32_t left = S.getSize() - i; 4129 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t); 4130 uint64_t p = 0; 4131 memcpy(&p, Contents + i, size); 4132 if (i + sizeof(uint64_t) > S.getSize()) 4133 outs() << listname << " list pointer extends past end of (" << SegName 4134 << "," << SectName << ") section\n"; 4135 outs() << format("%016" PRIx64, S.getAddress() + i) << " "; 4136 4137 if (O->isLittleEndian() != sys::IsLittleEndianHost) 4138 sys::swapByteOrder(p); 4139 4140 uint64_t n_value = 0; 4141 const char *name = get_symbol_64(i, S, info, n_value, p); 4142 if (name == nullptr) 4143 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info); 4144 4145 if (n_value != 0) { 4146 outs() << format("0x%" PRIx64, n_value); 4147 if (p != 0) 4148 outs() << " + " << format("0x%" PRIx64, p); 4149 } else 4150 outs() << format("0x%" PRIx64, p); 4151 if (name != nullptr) 4152 outs() << " " << name; 4153 outs() << "\n"; 4154 4155 p += n_value; 4156 if (func) 4157 func(p, info); 4158 } 4159 } 4160 4161 static void 4162 walk_pointer_list_32(const char *listname, const SectionRef S, 4163 MachOObjectFile *O, struct DisassembleInfo *info, 4164 void (*func)(uint32_t, struct DisassembleInfo *info)) { 4165 if (S == SectionRef()) 4166 return; 4167 4168 StringRef SectName = unwrapOrError(S.getName(), O->getFileName()); 4169 DataRefImpl Ref = S.getRawDataRefImpl(); 4170 StringRef SegName = O->getSectionFinalSegmentName(Ref); 4171 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 4172 4173 StringRef BytesStr = unwrapOrError(S.getContents(), O->getFileName()); 4174 const char *Contents = reinterpret_cast<const char *>(BytesStr.data()); 4175 4176 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) { 4177 uint32_t left = S.getSize() - i; 4178 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t); 4179 uint32_t p = 0; 4180 memcpy(&p, Contents + i, size); 4181 if (i + sizeof(uint32_t) > S.getSize()) 4182 outs() << listname << " list pointer extends past end of (" << SegName 4183 << "," << SectName << ") section\n"; 4184 uint32_t Address = S.getAddress() + i; 4185 outs() << format("%08" PRIx32, Address) << " "; 4186 4187 if (O->isLittleEndian() != sys::IsLittleEndianHost) 4188 sys::swapByteOrder(p); 4189 outs() << format("0x%" PRIx32, p); 4190 4191 const char *name = get_symbol_32(i, S, info, p); 4192 if (name != nullptr) 4193 outs() << " " << name; 4194 outs() << "\n"; 4195 4196 if (func) 4197 func(p, info); 4198 } 4199 } 4200 4201 static void print_layout_map(const char *layout_map, uint32_t left) { 4202 if (layout_map == nullptr) 4203 return; 4204 outs() << " layout map: "; 4205 do { 4206 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " "; 4207 left--; 4208 layout_map++; 4209 } while (*layout_map != '\0' && left != 0); 4210 outs() << "\n"; 4211 } 4212 4213 static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) { 4214 uint32_t offset, left; 4215 SectionRef S; 4216 const char *layout_map; 4217 4218 if (p == 0) 4219 return; 4220 layout_map = get_pointer_64(p, offset, left, S, info); 4221 print_layout_map(layout_map, left); 4222 } 4223 4224 static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) { 4225 uint32_t offset, left; 4226 SectionRef S; 4227 const char *layout_map; 4228 4229 if (p == 0) 4230 return; 4231 layout_map = get_pointer_32(p, offset, left, S, info); 4232 print_layout_map(layout_map, left); 4233 } 4234 4235 static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info, 4236 const char *indent) { 4237 struct method_list64_t ml; 4238 struct method64_t m; 4239 const char *r; 4240 uint32_t offset, xoffset, left, i; 4241 SectionRef S, xS; 4242 const char *name, *sym_name; 4243 uint64_t n_value; 4244 4245 r = get_pointer_64(p, offset, left, S, info); 4246 if (r == nullptr) 4247 return; 4248 memset(&ml, '\0', sizeof(struct method_list64_t)); 4249 if (left < sizeof(struct method_list64_t)) { 4250 memcpy(&ml, r, left); 4251 outs() << " (method_list_t entends past the end of the section)\n"; 4252 } else 4253 memcpy(&ml, r, sizeof(struct method_list64_t)); 4254 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4255 swapStruct(ml); 4256 outs() << indent << "\t\t entsize " << ml.entsize << "\n"; 4257 outs() << indent << "\t\t count " << ml.count << "\n"; 4258 4259 p += sizeof(struct method_list64_t); 4260 offset += sizeof(struct method_list64_t); 4261 for (i = 0; i < ml.count; i++) { 4262 r = get_pointer_64(p, offset, left, S, info); 4263 if (r == nullptr) 4264 return; 4265 memset(&m, '\0', sizeof(struct method64_t)); 4266 if (left < sizeof(struct method64_t)) { 4267 memcpy(&m, r, left); 4268 outs() << indent << " (method_t extends past the end of the section)\n"; 4269 } else 4270 memcpy(&m, r, sizeof(struct method64_t)); 4271 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4272 swapStruct(m); 4273 4274 outs() << indent << "\t\t name "; 4275 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S, 4276 info, n_value, m.name); 4277 if (n_value != 0) { 4278 if (info->verbose && sym_name != nullptr) 4279 outs() << sym_name; 4280 else 4281 outs() << format("0x%" PRIx64, n_value); 4282 if (m.name != 0) 4283 outs() << " + " << format("0x%" PRIx64, m.name); 4284 } else 4285 outs() << format("0x%" PRIx64, m.name); 4286 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info); 4287 if (name != nullptr) 4288 outs() << format(" %.*s", left, name); 4289 outs() << "\n"; 4290 4291 outs() << indent << "\t\t types "; 4292 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S, 4293 info, n_value, m.types); 4294 if (n_value != 0) { 4295 if (info->verbose && sym_name != nullptr) 4296 outs() << sym_name; 4297 else 4298 outs() << format("0x%" PRIx64, n_value); 4299 if (m.types != 0) 4300 outs() << " + " << format("0x%" PRIx64, m.types); 4301 } else 4302 outs() << format("0x%" PRIx64, m.types); 4303 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info); 4304 if (name != nullptr) 4305 outs() << format(" %.*s", left, name); 4306 outs() << "\n"; 4307 4308 outs() << indent << "\t\t imp "; 4309 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info, 4310 n_value, m.imp); 4311 if (info->verbose && name == nullptr) { 4312 if (n_value != 0) { 4313 outs() << format("0x%" PRIx64, n_value) << " "; 4314 if (m.imp != 0) 4315 outs() << "+ " << format("0x%" PRIx64, m.imp) << " "; 4316 } else 4317 outs() << format("0x%" PRIx64, m.imp) << " "; 4318 } 4319 if (name != nullptr) 4320 outs() << name; 4321 outs() << "\n"; 4322 4323 p += sizeof(struct method64_t); 4324 offset += sizeof(struct method64_t); 4325 } 4326 } 4327 4328 static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info, 4329 const char *indent) { 4330 struct method_list32_t ml; 4331 struct method32_t m; 4332 const char *r, *name; 4333 uint32_t offset, xoffset, left, i; 4334 SectionRef S, xS; 4335 4336 r = get_pointer_32(p, offset, left, S, info); 4337 if (r == nullptr) 4338 return; 4339 memset(&ml, '\0', sizeof(struct method_list32_t)); 4340 if (left < sizeof(struct method_list32_t)) { 4341 memcpy(&ml, r, left); 4342 outs() << " (method_list_t entends past the end of the section)\n"; 4343 } else 4344 memcpy(&ml, r, sizeof(struct method_list32_t)); 4345 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4346 swapStruct(ml); 4347 outs() << indent << "\t\t entsize " << ml.entsize << "\n"; 4348 outs() << indent << "\t\t count " << ml.count << "\n"; 4349 4350 p += sizeof(struct method_list32_t); 4351 offset += sizeof(struct method_list32_t); 4352 for (i = 0; i < ml.count; i++) { 4353 r = get_pointer_32(p, offset, left, S, info); 4354 if (r == nullptr) 4355 return; 4356 memset(&m, '\0', sizeof(struct method32_t)); 4357 if (left < sizeof(struct method32_t)) { 4358 memcpy(&ml, r, left); 4359 outs() << indent << " (method_t entends past the end of the section)\n"; 4360 } else 4361 memcpy(&m, r, sizeof(struct method32_t)); 4362 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4363 swapStruct(m); 4364 4365 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name); 4366 name = get_pointer_32(m.name, xoffset, left, xS, info); 4367 if (name != nullptr) 4368 outs() << format(" %.*s", left, name); 4369 outs() << "\n"; 4370 4371 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types); 4372 name = get_pointer_32(m.types, xoffset, left, xS, info); 4373 if (name != nullptr) 4374 outs() << format(" %.*s", left, name); 4375 outs() << "\n"; 4376 4377 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp); 4378 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info, 4379 m.imp); 4380 if (name != nullptr) 4381 outs() << " " << name; 4382 outs() << "\n"; 4383 4384 p += sizeof(struct method32_t); 4385 offset += sizeof(struct method32_t); 4386 } 4387 } 4388 4389 static bool print_method_list(uint32_t p, struct DisassembleInfo *info) { 4390 uint32_t offset, left, xleft; 4391 SectionRef S; 4392 struct objc_method_list_t method_list; 4393 struct objc_method_t method; 4394 const char *r, *methods, *name, *SymbolName; 4395 int32_t i; 4396 4397 r = get_pointer_32(p, offset, left, S, info, true); 4398 if (r == nullptr) 4399 return true; 4400 4401 outs() << "\n"; 4402 if (left > sizeof(struct objc_method_list_t)) { 4403 memcpy(&method_list, r, sizeof(struct objc_method_list_t)); 4404 } else { 4405 outs() << "\t\t objc_method_list extends past end of the section\n"; 4406 memset(&method_list, '\0', sizeof(struct objc_method_list_t)); 4407 memcpy(&method_list, r, left); 4408 } 4409 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4410 swapStruct(method_list); 4411 4412 outs() << "\t\t obsolete " 4413 << format("0x%08" PRIx32, method_list.obsolete) << "\n"; 4414 outs() << "\t\t method_count " << method_list.method_count << "\n"; 4415 4416 methods = r + sizeof(struct objc_method_list_t); 4417 for (i = 0; i < method_list.method_count; i++) { 4418 if ((i + 1) * sizeof(struct objc_method_t) > left) { 4419 outs() << "\t\t remaining method's extend past the of the section\n"; 4420 break; 4421 } 4422 memcpy(&method, methods + i * sizeof(struct objc_method_t), 4423 sizeof(struct objc_method_t)); 4424 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4425 swapStruct(method); 4426 4427 outs() << "\t\t method_name " 4428 << format("0x%08" PRIx32, method.method_name); 4429 if (info->verbose) { 4430 name = get_pointer_32(method.method_name, offset, xleft, S, info, true); 4431 if (name != nullptr) 4432 outs() << format(" %.*s", xleft, name); 4433 else 4434 outs() << " (not in an __OBJC section)"; 4435 } 4436 outs() << "\n"; 4437 4438 outs() << "\t\t method_types " 4439 << format("0x%08" PRIx32, method.method_types); 4440 if (info->verbose) { 4441 name = get_pointer_32(method.method_types, offset, xleft, S, info, true); 4442 if (name != nullptr) 4443 outs() << format(" %.*s", xleft, name); 4444 else 4445 outs() << " (not in an __OBJC section)"; 4446 } 4447 outs() << "\n"; 4448 4449 outs() << "\t\t method_imp " 4450 << format("0x%08" PRIx32, method.method_imp) << " "; 4451 if (info->verbose) { 4452 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap); 4453 if (SymbolName != nullptr) 4454 outs() << SymbolName; 4455 } 4456 outs() << "\n"; 4457 } 4458 return false; 4459 } 4460 4461 static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) { 4462 struct protocol_list64_t pl; 4463 uint64_t q, n_value; 4464 struct protocol64_t pc; 4465 const char *r; 4466 uint32_t offset, xoffset, left, i; 4467 SectionRef S, xS; 4468 const char *name, *sym_name; 4469 4470 r = get_pointer_64(p, offset, left, S, info); 4471 if (r == nullptr) 4472 return; 4473 memset(&pl, '\0', sizeof(struct protocol_list64_t)); 4474 if (left < sizeof(struct protocol_list64_t)) { 4475 memcpy(&pl, r, left); 4476 outs() << " (protocol_list_t entends past the end of the section)\n"; 4477 } else 4478 memcpy(&pl, r, sizeof(struct protocol_list64_t)); 4479 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4480 swapStruct(pl); 4481 outs() << " count " << pl.count << "\n"; 4482 4483 p += sizeof(struct protocol_list64_t); 4484 offset += sizeof(struct protocol_list64_t); 4485 for (i = 0; i < pl.count; i++) { 4486 r = get_pointer_64(p, offset, left, S, info); 4487 if (r == nullptr) 4488 return; 4489 q = 0; 4490 if (left < sizeof(uint64_t)) { 4491 memcpy(&q, r, left); 4492 outs() << " (protocol_t * entends past the end of the section)\n"; 4493 } else 4494 memcpy(&q, r, sizeof(uint64_t)); 4495 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4496 sys::swapByteOrder(q); 4497 4498 outs() << "\t\t list[" << i << "] "; 4499 sym_name = get_symbol_64(offset, S, info, n_value, q); 4500 if (n_value != 0) { 4501 if (info->verbose && sym_name != nullptr) 4502 outs() << sym_name; 4503 else 4504 outs() << format("0x%" PRIx64, n_value); 4505 if (q != 0) 4506 outs() << " + " << format("0x%" PRIx64, q); 4507 } else 4508 outs() << format("0x%" PRIx64, q); 4509 outs() << " (struct protocol_t *)\n"; 4510 4511 r = get_pointer_64(q + n_value, offset, left, S, info); 4512 if (r == nullptr) 4513 return; 4514 memset(&pc, '\0', sizeof(struct protocol64_t)); 4515 if (left < sizeof(struct protocol64_t)) { 4516 memcpy(&pc, r, left); 4517 outs() << " (protocol_t entends past the end of the section)\n"; 4518 } else 4519 memcpy(&pc, r, sizeof(struct protocol64_t)); 4520 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4521 swapStruct(pc); 4522 4523 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n"; 4524 4525 outs() << "\t\t\t name "; 4526 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S, 4527 info, n_value, pc.name); 4528 if (n_value != 0) { 4529 if (info->verbose && sym_name != nullptr) 4530 outs() << sym_name; 4531 else 4532 outs() << format("0x%" PRIx64, n_value); 4533 if (pc.name != 0) 4534 outs() << " + " << format("0x%" PRIx64, pc.name); 4535 } else 4536 outs() << format("0x%" PRIx64, pc.name); 4537 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info); 4538 if (name != nullptr) 4539 outs() << format(" %.*s", left, name); 4540 outs() << "\n"; 4541 4542 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n"; 4543 4544 outs() << "\t\t instanceMethods "; 4545 sym_name = 4546 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods), 4547 S, info, n_value, pc.instanceMethods); 4548 if (n_value != 0) { 4549 if (info->verbose && sym_name != nullptr) 4550 outs() << sym_name; 4551 else 4552 outs() << format("0x%" PRIx64, n_value); 4553 if (pc.instanceMethods != 0) 4554 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods); 4555 } else 4556 outs() << format("0x%" PRIx64, pc.instanceMethods); 4557 outs() << " (struct method_list_t *)\n"; 4558 if (pc.instanceMethods + n_value != 0) 4559 print_method_list64_t(pc.instanceMethods + n_value, info, "\t"); 4560 4561 outs() << "\t\t classMethods "; 4562 sym_name = 4563 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S, 4564 info, n_value, pc.classMethods); 4565 if (n_value != 0) { 4566 if (info->verbose && sym_name != nullptr) 4567 outs() << sym_name; 4568 else 4569 outs() << format("0x%" PRIx64, n_value); 4570 if (pc.classMethods != 0) 4571 outs() << " + " << format("0x%" PRIx64, pc.classMethods); 4572 } else 4573 outs() << format("0x%" PRIx64, pc.classMethods); 4574 outs() << " (struct method_list_t *)\n"; 4575 if (pc.classMethods + n_value != 0) 4576 print_method_list64_t(pc.classMethods + n_value, info, "\t"); 4577 4578 outs() << "\t optionalInstanceMethods " 4579 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n"; 4580 outs() << "\t optionalClassMethods " 4581 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n"; 4582 outs() << "\t instanceProperties " 4583 << format("0x%" PRIx64, pc.instanceProperties) << "\n"; 4584 4585 p += sizeof(uint64_t); 4586 offset += sizeof(uint64_t); 4587 } 4588 } 4589 4590 static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) { 4591 struct protocol_list32_t pl; 4592 uint32_t q; 4593 struct protocol32_t pc; 4594 const char *r; 4595 uint32_t offset, xoffset, left, i; 4596 SectionRef S, xS; 4597 const char *name; 4598 4599 r = get_pointer_32(p, offset, left, S, info); 4600 if (r == nullptr) 4601 return; 4602 memset(&pl, '\0', sizeof(struct protocol_list32_t)); 4603 if (left < sizeof(struct protocol_list32_t)) { 4604 memcpy(&pl, r, left); 4605 outs() << " (protocol_list_t entends past the end of the section)\n"; 4606 } else 4607 memcpy(&pl, r, sizeof(struct protocol_list32_t)); 4608 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4609 swapStruct(pl); 4610 outs() << " count " << pl.count << "\n"; 4611 4612 p += sizeof(struct protocol_list32_t); 4613 offset += sizeof(struct protocol_list32_t); 4614 for (i = 0; i < pl.count; i++) { 4615 r = get_pointer_32(p, offset, left, S, info); 4616 if (r == nullptr) 4617 return; 4618 q = 0; 4619 if (left < sizeof(uint32_t)) { 4620 memcpy(&q, r, left); 4621 outs() << " (protocol_t * entends past the end of the section)\n"; 4622 } else 4623 memcpy(&q, r, sizeof(uint32_t)); 4624 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4625 sys::swapByteOrder(q); 4626 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q) 4627 << " (struct protocol_t *)\n"; 4628 r = get_pointer_32(q, offset, left, S, info); 4629 if (r == nullptr) 4630 return; 4631 memset(&pc, '\0', sizeof(struct protocol32_t)); 4632 if (left < sizeof(struct protocol32_t)) { 4633 memcpy(&pc, r, left); 4634 outs() << " (protocol_t entends past the end of the section)\n"; 4635 } else 4636 memcpy(&pc, r, sizeof(struct protocol32_t)); 4637 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4638 swapStruct(pc); 4639 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n"; 4640 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name); 4641 name = get_pointer_32(pc.name, xoffset, left, xS, info); 4642 if (name != nullptr) 4643 outs() << format(" %.*s", left, name); 4644 outs() << "\n"; 4645 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n"; 4646 outs() << "\t\t instanceMethods " 4647 << format("0x%" PRIx32, pc.instanceMethods) 4648 << " (struct method_list_t *)\n"; 4649 if (pc.instanceMethods != 0) 4650 print_method_list32_t(pc.instanceMethods, info, "\t"); 4651 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods) 4652 << " (struct method_list_t *)\n"; 4653 if (pc.classMethods != 0) 4654 print_method_list32_t(pc.classMethods, info, "\t"); 4655 outs() << "\t optionalInstanceMethods " 4656 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n"; 4657 outs() << "\t optionalClassMethods " 4658 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n"; 4659 outs() << "\t instanceProperties " 4660 << format("0x%" PRIx32, pc.instanceProperties) << "\n"; 4661 p += sizeof(uint32_t); 4662 offset += sizeof(uint32_t); 4663 } 4664 } 4665 4666 static void print_indent(uint32_t indent) { 4667 for (uint32_t i = 0; i < indent;) { 4668 if (indent - i >= 8) { 4669 outs() << "\t"; 4670 i += 8; 4671 } else { 4672 for (uint32_t j = i; j < indent; j++) 4673 outs() << " "; 4674 return; 4675 } 4676 } 4677 } 4678 4679 static bool print_method_description_list(uint32_t p, uint32_t indent, 4680 struct DisassembleInfo *info) { 4681 uint32_t offset, left, xleft; 4682 SectionRef S; 4683 struct objc_method_description_list_t mdl; 4684 struct objc_method_description_t md; 4685 const char *r, *list, *name; 4686 int32_t i; 4687 4688 r = get_pointer_32(p, offset, left, S, info, true); 4689 if (r == nullptr) 4690 return true; 4691 4692 outs() << "\n"; 4693 if (left > sizeof(struct objc_method_description_list_t)) { 4694 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t)); 4695 } else { 4696 print_indent(indent); 4697 outs() << " objc_method_description_list extends past end of the section\n"; 4698 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t)); 4699 memcpy(&mdl, r, left); 4700 } 4701 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4702 swapStruct(mdl); 4703 4704 print_indent(indent); 4705 outs() << " count " << mdl.count << "\n"; 4706 4707 list = r + sizeof(struct objc_method_description_list_t); 4708 for (i = 0; i < mdl.count; i++) { 4709 if ((i + 1) * sizeof(struct objc_method_description_t) > left) { 4710 print_indent(indent); 4711 outs() << " remaining list entries extend past the of the section\n"; 4712 break; 4713 } 4714 print_indent(indent); 4715 outs() << " list[" << i << "]\n"; 4716 memcpy(&md, list + i * sizeof(struct objc_method_description_t), 4717 sizeof(struct objc_method_description_t)); 4718 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4719 swapStruct(md); 4720 4721 print_indent(indent); 4722 outs() << " name " << format("0x%08" PRIx32, md.name); 4723 if (info->verbose) { 4724 name = get_pointer_32(md.name, offset, xleft, S, info, true); 4725 if (name != nullptr) 4726 outs() << format(" %.*s", xleft, name); 4727 else 4728 outs() << " (not in an __OBJC section)"; 4729 } 4730 outs() << "\n"; 4731 4732 print_indent(indent); 4733 outs() << " types " << format("0x%08" PRIx32, md.types); 4734 if (info->verbose) { 4735 name = get_pointer_32(md.types, offset, xleft, S, info, true); 4736 if (name != nullptr) 4737 outs() << format(" %.*s", xleft, name); 4738 else 4739 outs() << " (not in an __OBJC section)"; 4740 } 4741 outs() << "\n"; 4742 } 4743 return false; 4744 } 4745 4746 static bool print_protocol_list(uint32_t p, uint32_t indent, 4747 struct DisassembleInfo *info); 4748 4749 static bool print_protocol(uint32_t p, uint32_t indent, 4750 struct DisassembleInfo *info) { 4751 uint32_t offset, left; 4752 SectionRef S; 4753 struct objc_protocol_t protocol; 4754 const char *r, *name; 4755 4756 r = get_pointer_32(p, offset, left, S, info, true); 4757 if (r == nullptr) 4758 return true; 4759 4760 outs() << "\n"; 4761 if (left >= sizeof(struct objc_protocol_t)) { 4762 memcpy(&protocol, r, sizeof(struct objc_protocol_t)); 4763 } else { 4764 print_indent(indent); 4765 outs() << " Protocol extends past end of the section\n"; 4766 memset(&protocol, '\0', sizeof(struct objc_protocol_t)); 4767 memcpy(&protocol, r, left); 4768 } 4769 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4770 swapStruct(protocol); 4771 4772 print_indent(indent); 4773 outs() << " isa " << format("0x%08" PRIx32, protocol.isa) 4774 << "\n"; 4775 4776 print_indent(indent); 4777 outs() << " protocol_name " 4778 << format("0x%08" PRIx32, protocol.protocol_name); 4779 if (info->verbose) { 4780 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true); 4781 if (name != nullptr) 4782 outs() << format(" %.*s", left, name); 4783 else 4784 outs() << " (not in an __OBJC section)"; 4785 } 4786 outs() << "\n"; 4787 4788 print_indent(indent); 4789 outs() << " protocol_list " 4790 << format("0x%08" PRIx32, protocol.protocol_list); 4791 if (print_protocol_list(protocol.protocol_list, indent + 4, info)) 4792 outs() << " (not in an __OBJC section)\n"; 4793 4794 print_indent(indent); 4795 outs() << " instance_methods " 4796 << format("0x%08" PRIx32, protocol.instance_methods); 4797 if (print_method_description_list(protocol.instance_methods, indent, info)) 4798 outs() << " (not in an __OBJC section)\n"; 4799 4800 print_indent(indent); 4801 outs() << " class_methods " 4802 << format("0x%08" PRIx32, protocol.class_methods); 4803 if (print_method_description_list(protocol.class_methods, indent, info)) 4804 outs() << " (not in an __OBJC section)\n"; 4805 4806 return false; 4807 } 4808 4809 static bool print_protocol_list(uint32_t p, uint32_t indent, 4810 struct DisassembleInfo *info) { 4811 uint32_t offset, left, l; 4812 SectionRef S; 4813 struct objc_protocol_list_t protocol_list; 4814 const char *r, *list; 4815 int32_t i; 4816 4817 r = get_pointer_32(p, offset, left, S, info, true); 4818 if (r == nullptr) 4819 return true; 4820 4821 outs() << "\n"; 4822 if (left > sizeof(struct objc_protocol_list_t)) { 4823 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t)); 4824 } else { 4825 outs() << "\t\t objc_protocol_list_t extends past end of the section\n"; 4826 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t)); 4827 memcpy(&protocol_list, r, left); 4828 } 4829 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4830 swapStruct(protocol_list); 4831 4832 print_indent(indent); 4833 outs() << " next " << format("0x%08" PRIx32, protocol_list.next) 4834 << "\n"; 4835 print_indent(indent); 4836 outs() << " count " << protocol_list.count << "\n"; 4837 4838 list = r + sizeof(struct objc_protocol_list_t); 4839 for (i = 0; i < protocol_list.count; i++) { 4840 if ((i + 1) * sizeof(uint32_t) > left) { 4841 outs() << "\t\t remaining list entries extend past the of the section\n"; 4842 break; 4843 } 4844 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t)); 4845 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4846 sys::swapByteOrder(l); 4847 4848 print_indent(indent); 4849 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l); 4850 if (print_protocol(l, indent, info)) 4851 outs() << "(not in an __OBJC section)\n"; 4852 } 4853 return false; 4854 } 4855 4856 static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) { 4857 struct ivar_list64_t il; 4858 struct ivar64_t i; 4859 const char *r; 4860 uint32_t offset, xoffset, left, j; 4861 SectionRef S, xS; 4862 const char *name, *sym_name, *ivar_offset_p; 4863 uint64_t ivar_offset, n_value; 4864 4865 r = get_pointer_64(p, offset, left, S, info); 4866 if (r == nullptr) 4867 return; 4868 memset(&il, '\0', sizeof(struct ivar_list64_t)); 4869 if (left < sizeof(struct ivar_list64_t)) { 4870 memcpy(&il, r, left); 4871 outs() << " (ivar_list_t entends past the end of the section)\n"; 4872 } else 4873 memcpy(&il, r, sizeof(struct ivar_list64_t)); 4874 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4875 swapStruct(il); 4876 outs() << " entsize " << il.entsize << "\n"; 4877 outs() << " count " << il.count << "\n"; 4878 4879 p += sizeof(struct ivar_list64_t); 4880 offset += sizeof(struct ivar_list64_t); 4881 for (j = 0; j < il.count; j++) { 4882 r = get_pointer_64(p, offset, left, S, info); 4883 if (r == nullptr) 4884 return; 4885 memset(&i, '\0', sizeof(struct ivar64_t)); 4886 if (left < sizeof(struct ivar64_t)) { 4887 memcpy(&i, r, left); 4888 outs() << " (ivar_t entends past the end of the section)\n"; 4889 } else 4890 memcpy(&i, r, sizeof(struct ivar64_t)); 4891 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4892 swapStruct(i); 4893 4894 outs() << "\t\t\t offset "; 4895 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S, 4896 info, n_value, i.offset); 4897 if (n_value != 0) { 4898 if (info->verbose && sym_name != nullptr) 4899 outs() << sym_name; 4900 else 4901 outs() << format("0x%" PRIx64, n_value); 4902 if (i.offset != 0) 4903 outs() << " + " << format("0x%" PRIx64, i.offset); 4904 } else 4905 outs() << format("0x%" PRIx64, i.offset); 4906 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info); 4907 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) { 4908 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset)); 4909 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4910 sys::swapByteOrder(ivar_offset); 4911 outs() << " " << ivar_offset << "\n"; 4912 } else 4913 outs() << "\n"; 4914 4915 outs() << "\t\t\t name "; 4916 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info, 4917 n_value, i.name); 4918 if (n_value != 0) { 4919 if (info->verbose && sym_name != nullptr) 4920 outs() << sym_name; 4921 else 4922 outs() << format("0x%" PRIx64, n_value); 4923 if (i.name != 0) 4924 outs() << " + " << format("0x%" PRIx64, i.name); 4925 } else 4926 outs() << format("0x%" PRIx64, i.name); 4927 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info); 4928 if (name != nullptr) 4929 outs() << format(" %.*s", left, name); 4930 outs() << "\n"; 4931 4932 outs() << "\t\t\t type "; 4933 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info, 4934 n_value, i.name); 4935 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info); 4936 if (n_value != 0) { 4937 if (info->verbose && sym_name != nullptr) 4938 outs() << sym_name; 4939 else 4940 outs() << format("0x%" PRIx64, n_value); 4941 if (i.type != 0) 4942 outs() << " + " << format("0x%" PRIx64, i.type); 4943 } else 4944 outs() << format("0x%" PRIx64, i.type); 4945 if (name != nullptr) 4946 outs() << format(" %.*s", left, name); 4947 outs() << "\n"; 4948 4949 outs() << "\t\t\talignment " << i.alignment << "\n"; 4950 outs() << "\t\t\t size " << i.size << "\n"; 4951 4952 p += sizeof(struct ivar64_t); 4953 offset += sizeof(struct ivar64_t); 4954 } 4955 } 4956 4957 static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) { 4958 struct ivar_list32_t il; 4959 struct ivar32_t i; 4960 const char *r; 4961 uint32_t offset, xoffset, left, j; 4962 SectionRef S, xS; 4963 const char *name, *ivar_offset_p; 4964 uint32_t ivar_offset; 4965 4966 r = get_pointer_32(p, offset, left, S, info); 4967 if (r == nullptr) 4968 return; 4969 memset(&il, '\0', sizeof(struct ivar_list32_t)); 4970 if (left < sizeof(struct ivar_list32_t)) { 4971 memcpy(&il, r, left); 4972 outs() << " (ivar_list_t entends past the end of the section)\n"; 4973 } else 4974 memcpy(&il, r, sizeof(struct ivar_list32_t)); 4975 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4976 swapStruct(il); 4977 outs() << " entsize " << il.entsize << "\n"; 4978 outs() << " count " << il.count << "\n"; 4979 4980 p += sizeof(struct ivar_list32_t); 4981 offset += sizeof(struct ivar_list32_t); 4982 for (j = 0; j < il.count; j++) { 4983 r = get_pointer_32(p, offset, left, S, info); 4984 if (r == nullptr) 4985 return; 4986 memset(&i, '\0', sizeof(struct ivar32_t)); 4987 if (left < sizeof(struct ivar32_t)) { 4988 memcpy(&i, r, left); 4989 outs() << " (ivar_t entends past the end of the section)\n"; 4990 } else 4991 memcpy(&i, r, sizeof(struct ivar32_t)); 4992 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4993 swapStruct(i); 4994 4995 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset); 4996 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info); 4997 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) { 4998 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset)); 4999 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5000 sys::swapByteOrder(ivar_offset); 5001 outs() << " " << ivar_offset << "\n"; 5002 } else 5003 outs() << "\n"; 5004 5005 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name); 5006 name = get_pointer_32(i.name, xoffset, left, xS, info); 5007 if (name != nullptr) 5008 outs() << format(" %.*s", left, name); 5009 outs() << "\n"; 5010 5011 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type); 5012 name = get_pointer_32(i.type, xoffset, left, xS, info); 5013 if (name != nullptr) 5014 outs() << format(" %.*s", left, name); 5015 outs() << "\n"; 5016 5017 outs() << "\t\t\talignment " << i.alignment << "\n"; 5018 outs() << "\t\t\t size " << i.size << "\n"; 5019 5020 p += sizeof(struct ivar32_t); 5021 offset += sizeof(struct ivar32_t); 5022 } 5023 } 5024 5025 static void print_objc_property_list64(uint64_t p, 5026 struct DisassembleInfo *info) { 5027 struct objc_property_list64 opl; 5028 struct objc_property64 op; 5029 const char *r; 5030 uint32_t offset, xoffset, left, j; 5031 SectionRef S, xS; 5032 const char *name, *sym_name; 5033 uint64_t n_value; 5034 5035 r = get_pointer_64(p, offset, left, S, info); 5036 if (r == nullptr) 5037 return; 5038 memset(&opl, '\0', sizeof(struct objc_property_list64)); 5039 if (left < sizeof(struct objc_property_list64)) { 5040 memcpy(&opl, r, left); 5041 outs() << " (objc_property_list entends past the end of the section)\n"; 5042 } else 5043 memcpy(&opl, r, sizeof(struct objc_property_list64)); 5044 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5045 swapStruct(opl); 5046 outs() << " entsize " << opl.entsize << "\n"; 5047 outs() << " count " << opl.count << "\n"; 5048 5049 p += sizeof(struct objc_property_list64); 5050 offset += sizeof(struct objc_property_list64); 5051 for (j = 0; j < opl.count; j++) { 5052 r = get_pointer_64(p, offset, left, S, info); 5053 if (r == nullptr) 5054 return; 5055 memset(&op, '\0', sizeof(struct objc_property64)); 5056 if (left < sizeof(struct objc_property64)) { 5057 memcpy(&op, r, left); 5058 outs() << " (objc_property entends past the end of the section)\n"; 5059 } else 5060 memcpy(&op, r, sizeof(struct objc_property64)); 5061 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5062 swapStruct(op); 5063 5064 outs() << "\t\t\t name "; 5065 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S, 5066 info, n_value, op.name); 5067 if (n_value != 0) { 5068 if (info->verbose && sym_name != nullptr) 5069 outs() << sym_name; 5070 else 5071 outs() << format("0x%" PRIx64, n_value); 5072 if (op.name != 0) 5073 outs() << " + " << format("0x%" PRIx64, op.name); 5074 } else 5075 outs() << format("0x%" PRIx64, op.name); 5076 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info); 5077 if (name != nullptr) 5078 outs() << format(" %.*s", left, name); 5079 outs() << "\n"; 5080 5081 outs() << "\t\t\tattributes "; 5082 sym_name = 5083 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S, 5084 info, n_value, op.attributes); 5085 if (n_value != 0) { 5086 if (info->verbose && sym_name != nullptr) 5087 outs() << sym_name; 5088 else 5089 outs() << format("0x%" PRIx64, n_value); 5090 if (op.attributes != 0) 5091 outs() << " + " << format("0x%" PRIx64, op.attributes); 5092 } else 5093 outs() << format("0x%" PRIx64, op.attributes); 5094 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info); 5095 if (name != nullptr) 5096 outs() << format(" %.*s", left, name); 5097 outs() << "\n"; 5098 5099 p += sizeof(struct objc_property64); 5100 offset += sizeof(struct objc_property64); 5101 } 5102 } 5103 5104 static void print_objc_property_list32(uint32_t p, 5105 struct DisassembleInfo *info) { 5106 struct objc_property_list32 opl; 5107 struct objc_property32 op; 5108 const char *r; 5109 uint32_t offset, xoffset, left, j; 5110 SectionRef S, xS; 5111 const char *name; 5112 5113 r = get_pointer_32(p, offset, left, S, info); 5114 if (r == nullptr) 5115 return; 5116 memset(&opl, '\0', sizeof(struct objc_property_list32)); 5117 if (left < sizeof(struct objc_property_list32)) { 5118 memcpy(&opl, r, left); 5119 outs() << " (objc_property_list entends past the end of the section)\n"; 5120 } else 5121 memcpy(&opl, r, sizeof(struct objc_property_list32)); 5122 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5123 swapStruct(opl); 5124 outs() << " entsize " << opl.entsize << "\n"; 5125 outs() << " count " << opl.count << "\n"; 5126 5127 p += sizeof(struct objc_property_list32); 5128 offset += sizeof(struct objc_property_list32); 5129 for (j = 0; j < opl.count; j++) { 5130 r = get_pointer_32(p, offset, left, S, info); 5131 if (r == nullptr) 5132 return; 5133 memset(&op, '\0', sizeof(struct objc_property32)); 5134 if (left < sizeof(struct objc_property32)) { 5135 memcpy(&op, r, left); 5136 outs() << " (objc_property entends past the end of the section)\n"; 5137 } else 5138 memcpy(&op, r, sizeof(struct objc_property32)); 5139 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5140 swapStruct(op); 5141 5142 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name); 5143 name = get_pointer_32(op.name, xoffset, left, xS, info); 5144 if (name != nullptr) 5145 outs() << format(" %.*s", left, name); 5146 outs() << "\n"; 5147 5148 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes); 5149 name = get_pointer_32(op.attributes, xoffset, left, xS, info); 5150 if (name != nullptr) 5151 outs() << format(" %.*s", left, name); 5152 outs() << "\n"; 5153 5154 p += sizeof(struct objc_property32); 5155 offset += sizeof(struct objc_property32); 5156 } 5157 } 5158 5159 static bool print_class_ro64_t(uint64_t p, struct DisassembleInfo *info, 5160 bool &is_meta_class) { 5161 struct class_ro64_t cro; 5162 const char *r; 5163 uint32_t offset, xoffset, left; 5164 SectionRef S, xS; 5165 const char *name, *sym_name; 5166 uint64_t n_value; 5167 5168 r = get_pointer_64(p, offset, left, S, info); 5169 if (r == nullptr || left < sizeof(struct class_ro64_t)) 5170 return false; 5171 memcpy(&cro, r, sizeof(struct class_ro64_t)); 5172 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5173 swapStruct(cro); 5174 outs() << " flags " << format("0x%" PRIx32, cro.flags); 5175 if (cro.flags & RO_META) 5176 outs() << " RO_META"; 5177 if (cro.flags & RO_ROOT) 5178 outs() << " RO_ROOT"; 5179 if (cro.flags & RO_HAS_CXX_STRUCTORS) 5180 outs() << " RO_HAS_CXX_STRUCTORS"; 5181 outs() << "\n"; 5182 outs() << " instanceStart " << cro.instanceStart << "\n"; 5183 outs() << " instanceSize " << cro.instanceSize << "\n"; 5184 outs() << " reserved " << format("0x%" PRIx32, cro.reserved) 5185 << "\n"; 5186 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout) 5187 << "\n"; 5188 print_layout_map64(cro.ivarLayout, info); 5189 5190 outs() << " name "; 5191 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S, 5192 info, n_value, cro.name); 5193 if (n_value != 0) { 5194 if (info->verbose && sym_name != nullptr) 5195 outs() << sym_name; 5196 else 5197 outs() << format("0x%" PRIx64, n_value); 5198 if (cro.name != 0) 5199 outs() << " + " << format("0x%" PRIx64, cro.name); 5200 } else 5201 outs() << format("0x%" PRIx64, cro.name); 5202 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info); 5203 if (name != nullptr) 5204 outs() << format(" %.*s", left, name); 5205 outs() << "\n"; 5206 5207 outs() << " baseMethods "; 5208 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods), 5209 S, info, n_value, cro.baseMethods); 5210 if (n_value != 0) { 5211 if (info->verbose && sym_name != nullptr) 5212 outs() << sym_name; 5213 else 5214 outs() << format("0x%" PRIx64, n_value); 5215 if (cro.baseMethods != 0) 5216 outs() << " + " << format("0x%" PRIx64, cro.baseMethods); 5217 } else 5218 outs() << format("0x%" PRIx64, cro.baseMethods); 5219 outs() << " (struct method_list_t *)\n"; 5220 if (cro.baseMethods + n_value != 0) 5221 print_method_list64_t(cro.baseMethods + n_value, info, ""); 5222 5223 outs() << " baseProtocols "; 5224 sym_name = 5225 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S, 5226 info, n_value, cro.baseProtocols); 5227 if (n_value != 0) { 5228 if (info->verbose && sym_name != nullptr) 5229 outs() << sym_name; 5230 else 5231 outs() << format("0x%" PRIx64, n_value); 5232 if (cro.baseProtocols != 0) 5233 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols); 5234 } else 5235 outs() << format("0x%" PRIx64, cro.baseProtocols); 5236 outs() << "\n"; 5237 if (cro.baseProtocols + n_value != 0) 5238 print_protocol_list64_t(cro.baseProtocols + n_value, info); 5239 5240 outs() << " ivars "; 5241 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S, 5242 info, n_value, cro.ivars); 5243 if (n_value != 0) { 5244 if (info->verbose && sym_name != nullptr) 5245 outs() << sym_name; 5246 else 5247 outs() << format("0x%" PRIx64, n_value); 5248 if (cro.ivars != 0) 5249 outs() << " + " << format("0x%" PRIx64, cro.ivars); 5250 } else 5251 outs() << format("0x%" PRIx64, cro.ivars); 5252 outs() << "\n"; 5253 if (cro.ivars + n_value != 0) 5254 print_ivar_list64_t(cro.ivars + n_value, info); 5255 5256 outs() << " weakIvarLayout "; 5257 sym_name = 5258 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S, 5259 info, n_value, cro.weakIvarLayout); 5260 if (n_value != 0) { 5261 if (info->verbose && sym_name != nullptr) 5262 outs() << sym_name; 5263 else 5264 outs() << format("0x%" PRIx64, n_value); 5265 if (cro.weakIvarLayout != 0) 5266 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout); 5267 } else 5268 outs() << format("0x%" PRIx64, cro.weakIvarLayout); 5269 outs() << "\n"; 5270 print_layout_map64(cro.weakIvarLayout + n_value, info); 5271 5272 outs() << " baseProperties "; 5273 sym_name = 5274 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S, 5275 info, n_value, cro.baseProperties); 5276 if (n_value != 0) { 5277 if (info->verbose && sym_name != nullptr) 5278 outs() << sym_name; 5279 else 5280 outs() << format("0x%" PRIx64, n_value); 5281 if (cro.baseProperties != 0) 5282 outs() << " + " << format("0x%" PRIx64, cro.baseProperties); 5283 } else 5284 outs() << format("0x%" PRIx64, cro.baseProperties); 5285 outs() << "\n"; 5286 if (cro.baseProperties + n_value != 0) 5287 print_objc_property_list64(cro.baseProperties + n_value, info); 5288 5289 is_meta_class = (cro.flags & RO_META) != 0; 5290 return true; 5291 } 5292 5293 static bool print_class_ro32_t(uint32_t p, struct DisassembleInfo *info, 5294 bool &is_meta_class) { 5295 struct class_ro32_t cro; 5296 const char *r; 5297 uint32_t offset, xoffset, left; 5298 SectionRef S, xS; 5299 const char *name; 5300 5301 r = get_pointer_32(p, offset, left, S, info); 5302 if (r == nullptr) 5303 return false; 5304 memset(&cro, '\0', sizeof(struct class_ro32_t)); 5305 if (left < sizeof(struct class_ro32_t)) { 5306 memcpy(&cro, r, left); 5307 outs() << " (class_ro_t entends past the end of the section)\n"; 5308 } else 5309 memcpy(&cro, r, sizeof(struct class_ro32_t)); 5310 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5311 swapStruct(cro); 5312 outs() << " flags " << format("0x%" PRIx32, cro.flags); 5313 if (cro.flags & RO_META) 5314 outs() << " RO_META"; 5315 if (cro.flags & RO_ROOT) 5316 outs() << " RO_ROOT"; 5317 if (cro.flags & RO_HAS_CXX_STRUCTORS) 5318 outs() << " RO_HAS_CXX_STRUCTORS"; 5319 outs() << "\n"; 5320 outs() << " instanceStart " << cro.instanceStart << "\n"; 5321 outs() << " instanceSize " << cro.instanceSize << "\n"; 5322 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout) 5323 << "\n"; 5324 print_layout_map32(cro.ivarLayout, info); 5325 5326 outs() << " name " << format("0x%" PRIx32, cro.name); 5327 name = get_pointer_32(cro.name, xoffset, left, xS, info); 5328 if (name != nullptr) 5329 outs() << format(" %.*s", left, name); 5330 outs() << "\n"; 5331 5332 outs() << " baseMethods " 5333 << format("0x%" PRIx32, cro.baseMethods) 5334 << " (struct method_list_t *)\n"; 5335 if (cro.baseMethods != 0) 5336 print_method_list32_t(cro.baseMethods, info, ""); 5337 5338 outs() << " baseProtocols " 5339 << format("0x%" PRIx32, cro.baseProtocols) << "\n"; 5340 if (cro.baseProtocols != 0) 5341 print_protocol_list32_t(cro.baseProtocols, info); 5342 outs() << " ivars " << format("0x%" PRIx32, cro.ivars) 5343 << "\n"; 5344 if (cro.ivars != 0) 5345 print_ivar_list32_t(cro.ivars, info); 5346 outs() << " weakIvarLayout " 5347 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n"; 5348 print_layout_map32(cro.weakIvarLayout, info); 5349 outs() << " baseProperties " 5350 << format("0x%" PRIx32, cro.baseProperties) << "\n"; 5351 if (cro.baseProperties != 0) 5352 print_objc_property_list32(cro.baseProperties, info); 5353 is_meta_class = (cro.flags & RO_META) != 0; 5354 return true; 5355 } 5356 5357 static void print_class64_t(uint64_t p, struct DisassembleInfo *info) { 5358 struct class64_t c; 5359 const char *r; 5360 uint32_t offset, left; 5361 SectionRef S; 5362 const char *name; 5363 uint64_t isa_n_value, n_value; 5364 5365 r = get_pointer_64(p, offset, left, S, info); 5366 if (r == nullptr || left < sizeof(struct class64_t)) 5367 return; 5368 memcpy(&c, r, sizeof(struct class64_t)); 5369 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5370 swapStruct(c); 5371 5372 outs() << " isa " << format("0x%" PRIx64, c.isa); 5373 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info, 5374 isa_n_value, c.isa); 5375 if (name != nullptr) 5376 outs() << " " << name; 5377 outs() << "\n"; 5378 5379 outs() << " superclass " << format("0x%" PRIx64, c.superclass); 5380 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info, 5381 n_value, c.superclass); 5382 if (name != nullptr) 5383 outs() << " " << name; 5384 else { 5385 name = get_dyld_bind_info_symbolname(S.getAddress() + 5386 offset + offsetof(struct class64_t, superclass), info); 5387 if (name != nullptr) 5388 outs() << " " << name; 5389 } 5390 outs() << "\n"; 5391 5392 outs() << " cache " << format("0x%" PRIx64, c.cache); 5393 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info, 5394 n_value, c.cache); 5395 if (name != nullptr) 5396 outs() << " " << name; 5397 outs() << "\n"; 5398 5399 outs() << " vtable " << format("0x%" PRIx64, c.vtable); 5400 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info, 5401 n_value, c.vtable); 5402 if (name != nullptr) 5403 outs() << " " << name; 5404 outs() << "\n"; 5405 5406 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info, 5407 n_value, c.data); 5408 outs() << " data "; 5409 if (n_value != 0) { 5410 if (info->verbose && name != nullptr) 5411 outs() << name; 5412 else 5413 outs() << format("0x%" PRIx64, n_value); 5414 if (c.data != 0) 5415 outs() << " + " << format("0x%" PRIx64, c.data); 5416 } else 5417 outs() << format("0x%" PRIx64, c.data); 5418 outs() << " (struct class_ro_t *)"; 5419 5420 // This is a Swift class if some of the low bits of the pointer are set. 5421 if ((c.data + n_value) & 0x7) 5422 outs() << " Swift class"; 5423 outs() << "\n"; 5424 bool is_meta_class; 5425 if (!print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class)) 5426 return; 5427 5428 if (!is_meta_class && 5429 c.isa + isa_n_value != p && 5430 c.isa + isa_n_value != 0 && 5431 info->depth < 100) { 5432 info->depth++; 5433 outs() << "Meta Class\n"; 5434 print_class64_t(c.isa + isa_n_value, info); 5435 } 5436 } 5437 5438 static void print_class32_t(uint32_t p, struct DisassembleInfo *info) { 5439 struct class32_t c; 5440 const char *r; 5441 uint32_t offset, left; 5442 SectionRef S; 5443 const char *name; 5444 5445 r = get_pointer_32(p, offset, left, S, info); 5446 if (r == nullptr) 5447 return; 5448 memset(&c, '\0', sizeof(struct class32_t)); 5449 if (left < sizeof(struct class32_t)) { 5450 memcpy(&c, r, left); 5451 outs() << " (class_t entends past the end of the section)\n"; 5452 } else 5453 memcpy(&c, r, sizeof(struct class32_t)); 5454 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5455 swapStruct(c); 5456 5457 outs() << " isa " << format("0x%" PRIx32, c.isa); 5458 name = 5459 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa); 5460 if (name != nullptr) 5461 outs() << " " << name; 5462 outs() << "\n"; 5463 5464 outs() << " superclass " << format("0x%" PRIx32, c.superclass); 5465 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info, 5466 c.superclass); 5467 if (name != nullptr) 5468 outs() << " " << name; 5469 outs() << "\n"; 5470 5471 outs() << " cache " << format("0x%" PRIx32, c.cache); 5472 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info, 5473 c.cache); 5474 if (name != nullptr) 5475 outs() << " " << name; 5476 outs() << "\n"; 5477 5478 outs() << " vtable " << format("0x%" PRIx32, c.vtable); 5479 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info, 5480 c.vtable); 5481 if (name != nullptr) 5482 outs() << " " << name; 5483 outs() << "\n"; 5484 5485 name = 5486 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data); 5487 outs() << " data " << format("0x%" PRIx32, c.data) 5488 << " (struct class_ro_t *)"; 5489 5490 // This is a Swift class if some of the low bits of the pointer are set. 5491 if (c.data & 0x3) 5492 outs() << " Swift class"; 5493 outs() << "\n"; 5494 bool is_meta_class; 5495 if (!print_class_ro32_t(c.data & ~0x3, info, is_meta_class)) 5496 return; 5497 5498 if (!is_meta_class) { 5499 outs() << "Meta Class\n"; 5500 print_class32_t(c.isa, info); 5501 } 5502 } 5503 5504 static void print_objc_class_t(struct objc_class_t *objc_class, 5505 struct DisassembleInfo *info) { 5506 uint32_t offset, left, xleft; 5507 const char *name, *p, *ivar_list; 5508 SectionRef S; 5509 int32_t i; 5510 struct objc_ivar_list_t objc_ivar_list; 5511 struct objc_ivar_t ivar; 5512 5513 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa); 5514 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) { 5515 name = get_pointer_32(objc_class->isa, offset, left, S, info, true); 5516 if (name != nullptr) 5517 outs() << format(" %.*s", left, name); 5518 else 5519 outs() << " (not in an __OBJC section)"; 5520 } 5521 outs() << "\n"; 5522 5523 outs() << "\t super_class " 5524 << format("0x%08" PRIx32, objc_class->super_class); 5525 if (info->verbose) { 5526 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true); 5527 if (name != nullptr) 5528 outs() << format(" %.*s", left, name); 5529 else 5530 outs() << " (not in an __OBJC section)"; 5531 } 5532 outs() << "\n"; 5533 5534 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name); 5535 if (info->verbose) { 5536 name = get_pointer_32(objc_class->name, offset, left, S, info, true); 5537 if (name != nullptr) 5538 outs() << format(" %.*s", left, name); 5539 else 5540 outs() << " (not in an __OBJC section)"; 5541 } 5542 outs() << "\n"; 5543 5544 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version) 5545 << "\n"; 5546 5547 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info); 5548 if (info->verbose) { 5549 if (CLS_GETINFO(objc_class, CLS_CLASS)) 5550 outs() << " CLS_CLASS"; 5551 else if (CLS_GETINFO(objc_class, CLS_META)) 5552 outs() << " CLS_META"; 5553 } 5554 outs() << "\n"; 5555 5556 outs() << "\t instance_size " 5557 << format("0x%08" PRIx32, objc_class->instance_size) << "\n"; 5558 5559 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true); 5560 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars); 5561 if (p != nullptr) { 5562 if (left > sizeof(struct objc_ivar_list_t)) { 5563 outs() << "\n"; 5564 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t)); 5565 } else { 5566 outs() << " (entends past the end of the section)\n"; 5567 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t)); 5568 memcpy(&objc_ivar_list, p, left); 5569 } 5570 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5571 swapStruct(objc_ivar_list); 5572 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n"; 5573 ivar_list = p + sizeof(struct objc_ivar_list_t); 5574 for (i = 0; i < objc_ivar_list.ivar_count; i++) { 5575 if ((i + 1) * sizeof(struct objc_ivar_t) > left) { 5576 outs() << "\t\t remaining ivar's extend past the of the section\n"; 5577 break; 5578 } 5579 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t), 5580 sizeof(struct objc_ivar_t)); 5581 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5582 swapStruct(ivar); 5583 5584 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name); 5585 if (info->verbose) { 5586 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true); 5587 if (name != nullptr) 5588 outs() << format(" %.*s", xleft, name); 5589 else 5590 outs() << " (not in an __OBJC section)"; 5591 } 5592 outs() << "\n"; 5593 5594 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type); 5595 if (info->verbose) { 5596 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true); 5597 if (name != nullptr) 5598 outs() << format(" %.*s", xleft, name); 5599 else 5600 outs() << " (not in an __OBJC section)"; 5601 } 5602 outs() << "\n"; 5603 5604 outs() << "\t\t ivar_offset " 5605 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n"; 5606 } 5607 } else { 5608 outs() << " (not in an __OBJC section)\n"; 5609 } 5610 5611 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists); 5612 if (print_method_list(objc_class->methodLists, info)) 5613 outs() << " (not in an __OBJC section)\n"; 5614 5615 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache) 5616 << "\n"; 5617 5618 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols); 5619 if (print_protocol_list(objc_class->protocols, 16, info)) 5620 outs() << " (not in an __OBJC section)\n"; 5621 } 5622 5623 static void print_objc_objc_category_t(struct objc_category_t *objc_category, 5624 struct DisassembleInfo *info) { 5625 uint32_t offset, left; 5626 const char *name; 5627 SectionRef S; 5628 5629 outs() << "\t category name " 5630 << format("0x%08" PRIx32, objc_category->category_name); 5631 if (info->verbose) { 5632 name = get_pointer_32(objc_category->category_name, offset, left, S, info, 5633 true); 5634 if (name != nullptr) 5635 outs() << format(" %.*s", left, name); 5636 else 5637 outs() << " (not in an __OBJC section)"; 5638 } 5639 outs() << "\n"; 5640 5641 outs() << "\t\t class name " 5642 << format("0x%08" PRIx32, objc_category->class_name); 5643 if (info->verbose) { 5644 name = 5645 get_pointer_32(objc_category->class_name, offset, left, S, info, true); 5646 if (name != nullptr) 5647 outs() << format(" %.*s", left, name); 5648 else 5649 outs() << " (not in an __OBJC section)"; 5650 } 5651 outs() << "\n"; 5652 5653 outs() << "\t instance methods " 5654 << format("0x%08" PRIx32, objc_category->instance_methods); 5655 if (print_method_list(objc_category->instance_methods, info)) 5656 outs() << " (not in an __OBJC section)\n"; 5657 5658 outs() << "\t class methods " 5659 << format("0x%08" PRIx32, objc_category->class_methods); 5660 if (print_method_list(objc_category->class_methods, info)) 5661 outs() << " (not in an __OBJC section)\n"; 5662 } 5663 5664 static void print_category64_t(uint64_t p, struct DisassembleInfo *info) { 5665 struct category64_t c; 5666 const char *r; 5667 uint32_t offset, xoffset, left; 5668 SectionRef S, xS; 5669 const char *name, *sym_name; 5670 uint64_t n_value; 5671 5672 r = get_pointer_64(p, offset, left, S, info); 5673 if (r == nullptr) 5674 return; 5675 memset(&c, '\0', sizeof(struct category64_t)); 5676 if (left < sizeof(struct category64_t)) { 5677 memcpy(&c, r, left); 5678 outs() << " (category_t entends past the end of the section)\n"; 5679 } else 5680 memcpy(&c, r, sizeof(struct category64_t)); 5681 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5682 swapStruct(c); 5683 5684 outs() << " name "; 5685 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S, 5686 info, n_value, c.name); 5687 if (n_value != 0) { 5688 if (info->verbose && sym_name != nullptr) 5689 outs() << sym_name; 5690 else 5691 outs() << format("0x%" PRIx64, n_value); 5692 if (c.name != 0) 5693 outs() << " + " << format("0x%" PRIx64, c.name); 5694 } else 5695 outs() << format("0x%" PRIx64, c.name); 5696 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info); 5697 if (name != nullptr) 5698 outs() << format(" %.*s", left, name); 5699 outs() << "\n"; 5700 5701 outs() << " cls "; 5702 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info, 5703 n_value, c.cls); 5704 if (n_value != 0) { 5705 if (info->verbose && sym_name != nullptr) 5706 outs() << sym_name; 5707 else 5708 outs() << format("0x%" PRIx64, n_value); 5709 if (c.cls != 0) 5710 outs() << " + " << format("0x%" PRIx64, c.cls); 5711 } else 5712 outs() << format("0x%" PRIx64, c.cls); 5713 outs() << "\n"; 5714 if (c.cls + n_value != 0) 5715 print_class64_t(c.cls + n_value, info); 5716 5717 outs() << " instanceMethods "; 5718 sym_name = 5719 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S, 5720 info, n_value, c.instanceMethods); 5721 if (n_value != 0) { 5722 if (info->verbose && sym_name != nullptr) 5723 outs() << sym_name; 5724 else 5725 outs() << format("0x%" PRIx64, n_value); 5726 if (c.instanceMethods != 0) 5727 outs() << " + " << format("0x%" PRIx64, c.instanceMethods); 5728 } else 5729 outs() << format("0x%" PRIx64, c.instanceMethods); 5730 outs() << "\n"; 5731 if (c.instanceMethods + n_value != 0) 5732 print_method_list64_t(c.instanceMethods + n_value, info, ""); 5733 5734 outs() << " classMethods "; 5735 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods), 5736 S, info, n_value, c.classMethods); 5737 if (n_value != 0) { 5738 if (info->verbose && sym_name != nullptr) 5739 outs() << sym_name; 5740 else 5741 outs() << format("0x%" PRIx64, n_value); 5742 if (c.classMethods != 0) 5743 outs() << " + " << format("0x%" PRIx64, c.classMethods); 5744 } else 5745 outs() << format("0x%" PRIx64, c.classMethods); 5746 outs() << "\n"; 5747 if (c.classMethods + n_value != 0) 5748 print_method_list64_t(c.classMethods + n_value, info, ""); 5749 5750 outs() << " protocols "; 5751 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S, 5752 info, n_value, c.protocols); 5753 if (n_value != 0) { 5754 if (info->verbose && sym_name != nullptr) 5755 outs() << sym_name; 5756 else 5757 outs() << format("0x%" PRIx64, n_value); 5758 if (c.protocols != 0) 5759 outs() << " + " << format("0x%" PRIx64, c.protocols); 5760 } else 5761 outs() << format("0x%" PRIx64, c.protocols); 5762 outs() << "\n"; 5763 if (c.protocols + n_value != 0) 5764 print_protocol_list64_t(c.protocols + n_value, info); 5765 5766 outs() << "instanceProperties "; 5767 sym_name = 5768 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties), 5769 S, info, n_value, c.instanceProperties); 5770 if (n_value != 0) { 5771 if (info->verbose && sym_name != nullptr) 5772 outs() << sym_name; 5773 else 5774 outs() << format("0x%" PRIx64, n_value); 5775 if (c.instanceProperties != 0) 5776 outs() << " + " << format("0x%" PRIx64, c.instanceProperties); 5777 } else 5778 outs() << format("0x%" PRIx64, c.instanceProperties); 5779 outs() << "\n"; 5780 if (c.instanceProperties + n_value != 0) 5781 print_objc_property_list64(c.instanceProperties + n_value, info); 5782 } 5783 5784 static void print_category32_t(uint32_t p, struct DisassembleInfo *info) { 5785 struct category32_t c; 5786 const char *r; 5787 uint32_t offset, left; 5788 SectionRef S, xS; 5789 const char *name; 5790 5791 r = get_pointer_32(p, offset, left, S, info); 5792 if (r == nullptr) 5793 return; 5794 memset(&c, '\0', sizeof(struct category32_t)); 5795 if (left < sizeof(struct category32_t)) { 5796 memcpy(&c, r, left); 5797 outs() << " (category_t entends past the end of the section)\n"; 5798 } else 5799 memcpy(&c, r, sizeof(struct category32_t)); 5800 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5801 swapStruct(c); 5802 5803 outs() << " name " << format("0x%" PRIx32, c.name); 5804 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info, 5805 c.name); 5806 if (name) 5807 outs() << " " << name; 5808 outs() << "\n"; 5809 5810 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n"; 5811 if (c.cls != 0) 5812 print_class32_t(c.cls, info); 5813 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods) 5814 << "\n"; 5815 if (c.instanceMethods != 0) 5816 print_method_list32_t(c.instanceMethods, info, ""); 5817 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods) 5818 << "\n"; 5819 if (c.classMethods != 0) 5820 print_method_list32_t(c.classMethods, info, ""); 5821 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n"; 5822 if (c.protocols != 0) 5823 print_protocol_list32_t(c.protocols, info); 5824 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties) 5825 << "\n"; 5826 if (c.instanceProperties != 0) 5827 print_objc_property_list32(c.instanceProperties, info); 5828 } 5829 5830 static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) { 5831 uint32_t i, left, offset, xoffset; 5832 uint64_t p, n_value; 5833 struct message_ref64 mr; 5834 const char *name, *sym_name; 5835 const char *r; 5836 SectionRef xS; 5837 5838 if (S == SectionRef()) 5839 return; 5840 5841 StringRef SectName; 5842 Expected<StringRef> SecNameOrErr = S.getName(); 5843 if (SecNameOrErr) 5844 SectName = *SecNameOrErr; 5845 else 5846 consumeError(SecNameOrErr.takeError()); 5847 5848 DataRefImpl Ref = S.getRawDataRefImpl(); 5849 StringRef SegName = info->O->getSectionFinalSegmentName(Ref); 5850 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 5851 offset = 0; 5852 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) { 5853 p = S.getAddress() + i; 5854 r = get_pointer_64(p, offset, left, S, info); 5855 if (r == nullptr) 5856 return; 5857 memset(&mr, '\0', sizeof(struct message_ref64)); 5858 if (left < sizeof(struct message_ref64)) { 5859 memcpy(&mr, r, left); 5860 outs() << " (message_ref entends past the end of the section)\n"; 5861 } else 5862 memcpy(&mr, r, sizeof(struct message_ref64)); 5863 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5864 swapStruct(mr); 5865 5866 outs() << " imp "; 5867 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info, 5868 n_value, mr.imp); 5869 if (n_value != 0) { 5870 outs() << format("0x%" PRIx64, n_value) << " "; 5871 if (mr.imp != 0) 5872 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " "; 5873 } else 5874 outs() << format("0x%" PRIx64, mr.imp) << " "; 5875 if (name != nullptr) 5876 outs() << " " << name; 5877 outs() << "\n"; 5878 5879 outs() << " sel "; 5880 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S, 5881 info, n_value, mr.sel); 5882 if (n_value != 0) { 5883 if (info->verbose && sym_name != nullptr) 5884 outs() << sym_name; 5885 else 5886 outs() << format("0x%" PRIx64, n_value); 5887 if (mr.sel != 0) 5888 outs() << " + " << format("0x%" PRIx64, mr.sel); 5889 } else 5890 outs() << format("0x%" PRIx64, mr.sel); 5891 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info); 5892 if (name != nullptr) 5893 outs() << format(" %.*s", left, name); 5894 outs() << "\n"; 5895 5896 offset += sizeof(struct message_ref64); 5897 } 5898 } 5899 5900 static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) { 5901 uint32_t i, left, offset, xoffset, p; 5902 struct message_ref32 mr; 5903 const char *name, *r; 5904 SectionRef xS; 5905 5906 if (S == SectionRef()) 5907 return; 5908 5909 StringRef SectName; 5910 Expected<StringRef> SecNameOrErr = S.getName(); 5911 if (SecNameOrErr) 5912 SectName = *SecNameOrErr; 5913 else 5914 consumeError(SecNameOrErr.takeError()); 5915 5916 DataRefImpl Ref = S.getRawDataRefImpl(); 5917 StringRef SegName = info->O->getSectionFinalSegmentName(Ref); 5918 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 5919 offset = 0; 5920 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) { 5921 p = S.getAddress() + i; 5922 r = get_pointer_32(p, offset, left, S, info); 5923 if (r == nullptr) 5924 return; 5925 memset(&mr, '\0', sizeof(struct message_ref32)); 5926 if (left < sizeof(struct message_ref32)) { 5927 memcpy(&mr, r, left); 5928 outs() << " (message_ref entends past the end of the section)\n"; 5929 } else 5930 memcpy(&mr, r, sizeof(struct message_ref32)); 5931 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5932 swapStruct(mr); 5933 5934 outs() << " imp " << format("0x%" PRIx32, mr.imp); 5935 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info, 5936 mr.imp); 5937 if (name != nullptr) 5938 outs() << " " << name; 5939 outs() << "\n"; 5940 5941 outs() << " sel " << format("0x%" PRIx32, mr.sel); 5942 name = get_pointer_32(mr.sel, xoffset, left, xS, info); 5943 if (name != nullptr) 5944 outs() << " " << name; 5945 outs() << "\n"; 5946 5947 offset += sizeof(struct message_ref32); 5948 } 5949 } 5950 5951 static void print_image_info64(SectionRef S, struct DisassembleInfo *info) { 5952 uint32_t left, offset, swift_version; 5953 uint64_t p; 5954 struct objc_image_info64 o; 5955 const char *r; 5956 5957 if (S == SectionRef()) 5958 return; 5959 5960 StringRef SectName; 5961 Expected<StringRef> SecNameOrErr = S.getName(); 5962 if (SecNameOrErr) 5963 SectName = *SecNameOrErr; 5964 else 5965 consumeError(SecNameOrErr.takeError()); 5966 5967 DataRefImpl Ref = S.getRawDataRefImpl(); 5968 StringRef SegName = info->O->getSectionFinalSegmentName(Ref); 5969 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 5970 p = S.getAddress(); 5971 r = get_pointer_64(p, offset, left, S, info); 5972 if (r == nullptr) 5973 return; 5974 memset(&o, '\0', sizeof(struct objc_image_info64)); 5975 if (left < sizeof(struct objc_image_info64)) { 5976 memcpy(&o, r, left); 5977 outs() << " (objc_image_info entends past the end of the section)\n"; 5978 } else 5979 memcpy(&o, r, sizeof(struct objc_image_info64)); 5980 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5981 swapStruct(o); 5982 outs() << " version " << o.version << "\n"; 5983 outs() << " flags " << format("0x%" PRIx32, o.flags); 5984 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT) 5985 outs() << " OBJC_IMAGE_IS_REPLACEMENT"; 5986 if (o.flags & OBJC_IMAGE_SUPPORTS_GC) 5987 outs() << " OBJC_IMAGE_SUPPORTS_GC"; 5988 if (o.flags & OBJC_IMAGE_IS_SIMULATED) 5989 outs() << " OBJC_IMAGE_IS_SIMULATED"; 5990 if (o.flags & OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES) 5991 outs() << " OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES"; 5992 swift_version = (o.flags >> 8) & 0xff; 5993 if (swift_version != 0) { 5994 if (swift_version == 1) 5995 outs() << " Swift 1.0"; 5996 else if (swift_version == 2) 5997 outs() << " Swift 1.1"; 5998 else if(swift_version == 3) 5999 outs() << " Swift 2.0"; 6000 else if(swift_version == 4) 6001 outs() << " Swift 3.0"; 6002 else if(swift_version == 5) 6003 outs() << " Swift 4.0"; 6004 else if(swift_version == 6) 6005 outs() << " Swift 4.1/Swift 4.2"; 6006 else if(swift_version == 7) 6007 outs() << " Swift 5 or later"; 6008 else 6009 outs() << " unknown future Swift version (" << swift_version << ")"; 6010 } 6011 outs() << "\n"; 6012 } 6013 6014 static void print_image_info32(SectionRef S, struct DisassembleInfo *info) { 6015 uint32_t left, offset, swift_version, p; 6016 struct objc_image_info32 o; 6017 const char *r; 6018 6019 if (S == SectionRef()) 6020 return; 6021 6022 StringRef SectName; 6023 Expected<StringRef> SecNameOrErr = S.getName(); 6024 if (SecNameOrErr) 6025 SectName = *SecNameOrErr; 6026 else 6027 consumeError(SecNameOrErr.takeError()); 6028 6029 DataRefImpl Ref = S.getRawDataRefImpl(); 6030 StringRef SegName = info->O->getSectionFinalSegmentName(Ref); 6031 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 6032 p = S.getAddress(); 6033 r = get_pointer_32(p, offset, left, S, info); 6034 if (r == nullptr) 6035 return; 6036 memset(&o, '\0', sizeof(struct objc_image_info32)); 6037 if (left < sizeof(struct objc_image_info32)) { 6038 memcpy(&o, r, left); 6039 outs() << " (objc_image_info entends past the end of the section)\n"; 6040 } else 6041 memcpy(&o, r, sizeof(struct objc_image_info32)); 6042 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 6043 swapStruct(o); 6044 outs() << " version " << o.version << "\n"; 6045 outs() << " flags " << format("0x%" PRIx32, o.flags); 6046 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT) 6047 outs() << " OBJC_IMAGE_IS_REPLACEMENT"; 6048 if (o.flags & OBJC_IMAGE_SUPPORTS_GC) 6049 outs() << " OBJC_IMAGE_SUPPORTS_GC"; 6050 swift_version = (o.flags >> 8) & 0xff; 6051 if (swift_version != 0) { 6052 if (swift_version == 1) 6053 outs() << " Swift 1.0"; 6054 else if (swift_version == 2) 6055 outs() << " Swift 1.1"; 6056 else if(swift_version == 3) 6057 outs() << " Swift 2.0"; 6058 else if(swift_version == 4) 6059 outs() << " Swift 3.0"; 6060 else if(swift_version == 5) 6061 outs() << " Swift 4.0"; 6062 else if(swift_version == 6) 6063 outs() << " Swift 4.1/Swift 4.2"; 6064 else if(swift_version == 7) 6065 outs() << " Swift 5 or later"; 6066 else 6067 outs() << " unknown future Swift version (" << swift_version << ")"; 6068 } 6069 outs() << "\n"; 6070 } 6071 6072 static void print_image_info(SectionRef S, struct DisassembleInfo *info) { 6073 uint32_t left, offset, p; 6074 struct imageInfo_t o; 6075 const char *r; 6076 6077 StringRef SectName; 6078 Expected<StringRef> SecNameOrErr = S.getName(); 6079 if (SecNameOrErr) 6080 SectName = *SecNameOrErr; 6081 else 6082 consumeError(SecNameOrErr.takeError()); 6083 6084 DataRefImpl Ref = S.getRawDataRefImpl(); 6085 StringRef SegName = info->O->getSectionFinalSegmentName(Ref); 6086 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 6087 p = S.getAddress(); 6088 r = get_pointer_32(p, offset, left, S, info); 6089 if (r == nullptr) 6090 return; 6091 memset(&o, '\0', sizeof(struct imageInfo_t)); 6092 if (left < sizeof(struct imageInfo_t)) { 6093 memcpy(&o, r, left); 6094 outs() << " (imageInfo entends past the end of the section)\n"; 6095 } else 6096 memcpy(&o, r, sizeof(struct imageInfo_t)); 6097 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 6098 swapStruct(o); 6099 outs() << " version " << o.version << "\n"; 6100 outs() << " flags " << format("0x%" PRIx32, o.flags); 6101 if (o.flags & 0x1) 6102 outs() << " F&C"; 6103 if (o.flags & 0x2) 6104 outs() << " GC"; 6105 if (o.flags & 0x4) 6106 outs() << " GC-only"; 6107 else 6108 outs() << " RR"; 6109 outs() << "\n"; 6110 } 6111 6112 static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) { 6113 SymbolAddressMap AddrMap; 6114 if (verbose) 6115 CreateSymbolAddressMap(O, &AddrMap); 6116 6117 std::vector<SectionRef> Sections; 6118 for (const SectionRef &Section : O->sections()) 6119 Sections.push_back(Section); 6120 6121 struct DisassembleInfo info(O, &AddrMap, &Sections, verbose); 6122 6123 SectionRef CL = get_section(O, "__OBJC2", "__class_list"); 6124 if (CL == SectionRef()) 6125 CL = get_section(O, "__DATA", "__objc_classlist"); 6126 if (CL == SectionRef()) 6127 CL = get_section(O, "__DATA_CONST", "__objc_classlist"); 6128 if (CL == SectionRef()) 6129 CL = get_section(O, "__DATA_DIRTY", "__objc_classlist"); 6130 info.S = CL; 6131 walk_pointer_list_64("class", CL, O, &info, print_class64_t); 6132 6133 SectionRef CR = get_section(O, "__OBJC2", "__class_refs"); 6134 if (CR == SectionRef()) 6135 CR = get_section(O, "__DATA", "__objc_classrefs"); 6136 if (CR == SectionRef()) 6137 CR = get_section(O, "__DATA_CONST", "__objc_classrefs"); 6138 if (CR == SectionRef()) 6139 CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs"); 6140 info.S = CR; 6141 walk_pointer_list_64("class refs", CR, O, &info, nullptr); 6142 6143 SectionRef SR = get_section(O, "__OBJC2", "__super_refs"); 6144 if (SR == SectionRef()) 6145 SR = get_section(O, "__DATA", "__objc_superrefs"); 6146 if (SR == SectionRef()) 6147 SR = get_section(O, "__DATA_CONST", "__objc_superrefs"); 6148 if (SR == SectionRef()) 6149 SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs"); 6150 info.S = SR; 6151 walk_pointer_list_64("super refs", SR, O, &info, nullptr); 6152 6153 SectionRef CA = get_section(O, "__OBJC2", "__category_list"); 6154 if (CA == SectionRef()) 6155 CA = get_section(O, "__DATA", "__objc_catlist"); 6156 if (CA == SectionRef()) 6157 CA = get_section(O, "__DATA_CONST", "__objc_catlist"); 6158 if (CA == SectionRef()) 6159 CA = get_section(O, "__DATA_DIRTY", "__objc_catlist"); 6160 info.S = CA; 6161 walk_pointer_list_64("category", CA, O, &info, print_category64_t); 6162 6163 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list"); 6164 if (PL == SectionRef()) 6165 PL = get_section(O, "__DATA", "__objc_protolist"); 6166 if (PL == SectionRef()) 6167 PL = get_section(O, "__DATA_CONST", "__objc_protolist"); 6168 if (PL == SectionRef()) 6169 PL = get_section(O, "__DATA_DIRTY", "__objc_protolist"); 6170 info.S = PL; 6171 walk_pointer_list_64("protocol", PL, O, &info, nullptr); 6172 6173 SectionRef MR = get_section(O, "__OBJC2", "__message_refs"); 6174 if (MR == SectionRef()) 6175 MR = get_section(O, "__DATA", "__objc_msgrefs"); 6176 if (MR == SectionRef()) 6177 MR = get_section(O, "__DATA_CONST", "__objc_msgrefs"); 6178 if (MR == SectionRef()) 6179 MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs"); 6180 info.S = MR; 6181 print_message_refs64(MR, &info); 6182 6183 SectionRef II = get_section(O, "__OBJC2", "__image_info"); 6184 if (II == SectionRef()) 6185 II = get_section(O, "__DATA", "__objc_imageinfo"); 6186 if (II == SectionRef()) 6187 II = get_section(O, "__DATA_CONST", "__objc_imageinfo"); 6188 if (II == SectionRef()) 6189 II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo"); 6190 info.S = II; 6191 print_image_info64(II, &info); 6192 } 6193 6194 static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) { 6195 SymbolAddressMap AddrMap; 6196 if (verbose) 6197 CreateSymbolAddressMap(O, &AddrMap); 6198 6199 std::vector<SectionRef> Sections; 6200 for (const SectionRef &Section : O->sections()) 6201 Sections.push_back(Section); 6202 6203 struct DisassembleInfo info(O, &AddrMap, &Sections, verbose); 6204 6205 SectionRef CL = get_section(O, "__OBJC2", "__class_list"); 6206 if (CL == SectionRef()) 6207 CL = get_section(O, "__DATA", "__objc_classlist"); 6208 if (CL == SectionRef()) 6209 CL = get_section(O, "__DATA_CONST", "__objc_classlist"); 6210 if (CL == SectionRef()) 6211 CL = get_section(O, "__DATA_DIRTY", "__objc_classlist"); 6212 info.S = CL; 6213 walk_pointer_list_32("class", CL, O, &info, print_class32_t); 6214 6215 SectionRef CR = get_section(O, "__OBJC2", "__class_refs"); 6216 if (CR == SectionRef()) 6217 CR = get_section(O, "__DATA", "__objc_classrefs"); 6218 if (CR == SectionRef()) 6219 CR = get_section(O, "__DATA_CONST", "__objc_classrefs"); 6220 if (CR == SectionRef()) 6221 CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs"); 6222 info.S = CR; 6223 walk_pointer_list_32("class refs", CR, O, &info, nullptr); 6224 6225 SectionRef SR = get_section(O, "__OBJC2", "__super_refs"); 6226 if (SR == SectionRef()) 6227 SR = get_section(O, "__DATA", "__objc_superrefs"); 6228 if (SR == SectionRef()) 6229 SR = get_section(O, "__DATA_CONST", "__objc_superrefs"); 6230 if (SR == SectionRef()) 6231 SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs"); 6232 info.S = SR; 6233 walk_pointer_list_32("super refs", SR, O, &info, nullptr); 6234 6235 SectionRef CA = get_section(O, "__OBJC2", "__category_list"); 6236 if (CA == SectionRef()) 6237 CA = get_section(O, "__DATA", "__objc_catlist"); 6238 if (CA == SectionRef()) 6239 CA = get_section(O, "__DATA_CONST", "__objc_catlist"); 6240 if (CA == SectionRef()) 6241 CA = get_section(O, "__DATA_DIRTY", "__objc_catlist"); 6242 info.S = CA; 6243 walk_pointer_list_32("category", CA, O, &info, print_category32_t); 6244 6245 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list"); 6246 if (PL == SectionRef()) 6247 PL = get_section(O, "__DATA", "__objc_protolist"); 6248 if (PL == SectionRef()) 6249 PL = get_section(O, "__DATA_CONST", "__objc_protolist"); 6250 if (PL == SectionRef()) 6251 PL = get_section(O, "__DATA_DIRTY", "__objc_protolist"); 6252 info.S = PL; 6253 walk_pointer_list_32("protocol", PL, O, &info, nullptr); 6254 6255 SectionRef MR = get_section(O, "__OBJC2", "__message_refs"); 6256 if (MR == SectionRef()) 6257 MR = get_section(O, "__DATA", "__objc_msgrefs"); 6258 if (MR == SectionRef()) 6259 MR = get_section(O, "__DATA_CONST", "__objc_msgrefs"); 6260 if (MR == SectionRef()) 6261 MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs"); 6262 info.S = MR; 6263 print_message_refs32(MR, &info); 6264 6265 SectionRef II = get_section(O, "__OBJC2", "__image_info"); 6266 if (II == SectionRef()) 6267 II = get_section(O, "__DATA", "__objc_imageinfo"); 6268 if (II == SectionRef()) 6269 II = get_section(O, "__DATA_CONST", "__objc_imageinfo"); 6270 if (II == SectionRef()) 6271 II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo"); 6272 info.S = II; 6273 print_image_info32(II, &info); 6274 } 6275 6276 static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) { 6277 uint32_t i, j, p, offset, xoffset, left, defs_left, def; 6278 const char *r, *name, *defs; 6279 struct objc_module_t module; 6280 SectionRef S, xS; 6281 struct objc_symtab_t symtab; 6282 struct objc_class_t objc_class; 6283 struct objc_category_t objc_category; 6284 6285 outs() << "Objective-C segment\n"; 6286 S = get_section(O, "__OBJC", "__module_info"); 6287 if (S == SectionRef()) 6288 return false; 6289 6290 SymbolAddressMap AddrMap; 6291 if (verbose) 6292 CreateSymbolAddressMap(O, &AddrMap); 6293 6294 std::vector<SectionRef> Sections; 6295 for (const SectionRef &Section : O->sections()) 6296 Sections.push_back(Section); 6297 6298 struct DisassembleInfo info(O, &AddrMap, &Sections, verbose); 6299 6300 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) { 6301 p = S.getAddress() + i; 6302 r = get_pointer_32(p, offset, left, S, &info, true); 6303 if (r == nullptr) 6304 return true; 6305 memset(&module, '\0', sizeof(struct objc_module_t)); 6306 if (left < sizeof(struct objc_module_t)) { 6307 memcpy(&module, r, left); 6308 outs() << " (module extends past end of __module_info section)\n"; 6309 } else 6310 memcpy(&module, r, sizeof(struct objc_module_t)); 6311 if (O->isLittleEndian() != sys::IsLittleEndianHost) 6312 swapStruct(module); 6313 6314 outs() << "Module " << format("0x%" PRIx32, p) << "\n"; 6315 outs() << " version " << module.version << "\n"; 6316 outs() << " size " << module.size << "\n"; 6317 outs() << " name "; 6318 name = get_pointer_32(module.name, xoffset, left, xS, &info, true); 6319 if (name != nullptr) 6320 outs() << format("%.*s", left, name); 6321 else 6322 outs() << format("0x%08" PRIx32, module.name) 6323 << "(not in an __OBJC section)"; 6324 outs() << "\n"; 6325 6326 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true); 6327 if (module.symtab == 0 || r == nullptr) { 6328 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) 6329 << " (not in an __OBJC section)\n"; 6330 continue; 6331 } 6332 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n"; 6333 memset(&symtab, '\0', sizeof(struct objc_symtab_t)); 6334 defs_left = 0; 6335 defs = nullptr; 6336 if (left < sizeof(struct objc_symtab_t)) { 6337 memcpy(&symtab, r, left); 6338 outs() << "\tsymtab extends past end of an __OBJC section)\n"; 6339 } else { 6340 memcpy(&symtab, r, sizeof(struct objc_symtab_t)); 6341 if (left > sizeof(struct objc_symtab_t)) { 6342 defs_left = left - sizeof(struct objc_symtab_t); 6343 defs = r + sizeof(struct objc_symtab_t); 6344 } 6345 } 6346 if (O->isLittleEndian() != sys::IsLittleEndianHost) 6347 swapStruct(symtab); 6348 6349 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n"; 6350 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true); 6351 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs); 6352 if (r == nullptr) 6353 outs() << " (not in an __OBJC section)"; 6354 outs() << "\n"; 6355 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n"; 6356 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n"; 6357 if (symtab.cls_def_cnt > 0) 6358 outs() << "\tClass Definitions\n"; 6359 for (j = 0; j < symtab.cls_def_cnt; j++) { 6360 if ((j + 1) * sizeof(uint32_t) > defs_left) { 6361 outs() << "\t(remaining class defs entries entends past the end of the " 6362 << "section)\n"; 6363 break; 6364 } 6365 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t)); 6366 if (O->isLittleEndian() != sys::IsLittleEndianHost) 6367 sys::swapByteOrder(def); 6368 6369 r = get_pointer_32(def, xoffset, left, xS, &info, true); 6370 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def); 6371 if (r != nullptr) { 6372 if (left > sizeof(struct objc_class_t)) { 6373 outs() << "\n"; 6374 memcpy(&objc_class, r, sizeof(struct objc_class_t)); 6375 } else { 6376 outs() << " (entends past the end of the section)\n"; 6377 memset(&objc_class, '\0', sizeof(struct objc_class_t)); 6378 memcpy(&objc_class, r, left); 6379 } 6380 if (O->isLittleEndian() != sys::IsLittleEndianHost) 6381 swapStruct(objc_class); 6382 print_objc_class_t(&objc_class, &info); 6383 } else { 6384 outs() << "(not in an __OBJC section)\n"; 6385 } 6386 6387 if (CLS_GETINFO(&objc_class, CLS_CLASS)) { 6388 outs() << "\tMeta Class"; 6389 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true); 6390 if (r != nullptr) { 6391 if (left > sizeof(struct objc_class_t)) { 6392 outs() << "\n"; 6393 memcpy(&objc_class, r, sizeof(struct objc_class_t)); 6394 } else { 6395 outs() << " (entends past the end of the section)\n"; 6396 memset(&objc_class, '\0', sizeof(struct objc_class_t)); 6397 memcpy(&objc_class, r, left); 6398 } 6399 if (O->isLittleEndian() != sys::IsLittleEndianHost) 6400 swapStruct(objc_class); 6401 print_objc_class_t(&objc_class, &info); 6402 } else { 6403 outs() << "(not in an __OBJC section)\n"; 6404 } 6405 } 6406 } 6407 if (symtab.cat_def_cnt > 0) 6408 outs() << "\tCategory Definitions\n"; 6409 for (j = 0; j < symtab.cat_def_cnt; j++) { 6410 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) { 6411 outs() << "\t(remaining category defs entries entends past the end of " 6412 << "the section)\n"; 6413 break; 6414 } 6415 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t), 6416 sizeof(uint32_t)); 6417 if (O->isLittleEndian() != sys::IsLittleEndianHost) 6418 sys::swapByteOrder(def); 6419 6420 r = get_pointer_32(def, xoffset, left, xS, &info, true); 6421 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] " 6422 << format("0x%08" PRIx32, def); 6423 if (r != nullptr) { 6424 if (left > sizeof(struct objc_category_t)) { 6425 outs() << "\n"; 6426 memcpy(&objc_category, r, sizeof(struct objc_category_t)); 6427 } else { 6428 outs() << " (entends past the end of the section)\n"; 6429 memset(&objc_category, '\0', sizeof(struct objc_category_t)); 6430 memcpy(&objc_category, r, left); 6431 } 6432 if (O->isLittleEndian() != sys::IsLittleEndianHost) 6433 swapStruct(objc_category); 6434 print_objc_objc_category_t(&objc_category, &info); 6435 } else { 6436 outs() << "(not in an __OBJC section)\n"; 6437 } 6438 } 6439 } 6440 const SectionRef II = get_section(O, "__OBJC", "__image_info"); 6441 if (II != SectionRef()) 6442 print_image_info(II, &info); 6443 6444 return true; 6445 } 6446 6447 static void DumpProtocolSection(MachOObjectFile *O, const char *sect, 6448 uint32_t size, uint32_t addr) { 6449 SymbolAddressMap AddrMap; 6450 CreateSymbolAddressMap(O, &AddrMap); 6451 6452 std::vector<SectionRef> Sections; 6453 for (const SectionRef &Section : O->sections()) 6454 Sections.push_back(Section); 6455 6456 struct DisassembleInfo info(O, &AddrMap, &Sections, true); 6457 6458 const char *p; 6459 struct objc_protocol_t protocol; 6460 uint32_t left, paddr; 6461 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) { 6462 memset(&protocol, '\0', sizeof(struct objc_protocol_t)); 6463 left = size - (p - sect); 6464 if (left < sizeof(struct objc_protocol_t)) { 6465 outs() << "Protocol extends past end of __protocol section\n"; 6466 memcpy(&protocol, p, left); 6467 } else 6468 memcpy(&protocol, p, sizeof(struct objc_protocol_t)); 6469 if (O->isLittleEndian() != sys::IsLittleEndianHost) 6470 swapStruct(protocol); 6471 paddr = addr + (p - sect); 6472 outs() << "Protocol " << format("0x%" PRIx32, paddr); 6473 if (print_protocol(paddr, 0, &info)) 6474 outs() << "(not in an __OBJC section)\n"; 6475 } 6476 } 6477 6478 #ifdef HAVE_LIBXAR 6479 static inline void swapStruct(struct xar_header &xar) { 6480 sys::swapByteOrder(xar.magic); 6481 sys::swapByteOrder(xar.size); 6482 sys::swapByteOrder(xar.version); 6483 sys::swapByteOrder(xar.toc_length_compressed); 6484 sys::swapByteOrder(xar.toc_length_uncompressed); 6485 sys::swapByteOrder(xar.cksum_alg); 6486 } 6487 6488 static void PrintModeVerbose(uint32_t mode) { 6489 switch(mode & S_IFMT){ 6490 case S_IFDIR: 6491 outs() << "d"; 6492 break; 6493 case S_IFCHR: 6494 outs() << "c"; 6495 break; 6496 case S_IFBLK: 6497 outs() << "b"; 6498 break; 6499 case S_IFREG: 6500 outs() << "-"; 6501 break; 6502 case S_IFLNK: 6503 outs() << "l"; 6504 break; 6505 case S_IFSOCK: 6506 outs() << "s"; 6507 break; 6508 default: 6509 outs() << "?"; 6510 break; 6511 } 6512 6513 /* owner permissions */ 6514 if(mode & S_IREAD) 6515 outs() << "r"; 6516 else 6517 outs() << "-"; 6518 if(mode & S_IWRITE) 6519 outs() << "w"; 6520 else 6521 outs() << "-"; 6522 if(mode & S_ISUID) 6523 outs() << "s"; 6524 else if(mode & S_IEXEC) 6525 outs() << "x"; 6526 else 6527 outs() << "-"; 6528 6529 /* group permissions */ 6530 if(mode & (S_IREAD >> 3)) 6531 outs() << "r"; 6532 else 6533 outs() << "-"; 6534 if(mode & (S_IWRITE >> 3)) 6535 outs() << "w"; 6536 else 6537 outs() << "-"; 6538 if(mode & S_ISGID) 6539 outs() << "s"; 6540 else if(mode & (S_IEXEC >> 3)) 6541 outs() << "x"; 6542 else 6543 outs() << "-"; 6544 6545 /* other permissions */ 6546 if(mode & (S_IREAD >> 6)) 6547 outs() << "r"; 6548 else 6549 outs() << "-"; 6550 if(mode & (S_IWRITE >> 6)) 6551 outs() << "w"; 6552 else 6553 outs() << "-"; 6554 if(mode & S_ISVTX) 6555 outs() << "t"; 6556 else if(mode & (S_IEXEC >> 6)) 6557 outs() << "x"; 6558 else 6559 outs() << "-"; 6560 } 6561 6562 static void PrintXarFilesSummary(const char *XarFilename, xar_t xar) { 6563 xar_file_t xf; 6564 const char *key, *type, *mode, *user, *group, *size, *mtime, *name, *m; 6565 char *endp; 6566 uint32_t mode_value; 6567 6568 ScopedXarIter xi; 6569 if (!xi) { 6570 WithColor::error(errs(), "llvm-objdump") 6571 << "can't obtain an xar iterator for xar archive " << XarFilename 6572 << "\n"; 6573 return; 6574 } 6575 6576 // Go through the xar's files. 6577 for (xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)) { 6578 ScopedXarIter xp; 6579 if(!xp){ 6580 WithColor::error(errs(), "llvm-objdump") 6581 << "can't obtain an xar iterator for xar archive " << XarFilename 6582 << "\n"; 6583 return; 6584 } 6585 type = nullptr; 6586 mode = nullptr; 6587 user = nullptr; 6588 group = nullptr; 6589 size = nullptr; 6590 mtime = nullptr; 6591 name = nullptr; 6592 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){ 6593 const char *val = nullptr; 6594 xar_prop_get(xf, key, &val); 6595 #if 0 // Useful for debugging. 6596 outs() << "key: " << key << " value: " << val << "\n"; 6597 #endif 6598 if(strcmp(key, "type") == 0) 6599 type = val; 6600 if(strcmp(key, "mode") == 0) 6601 mode = val; 6602 if(strcmp(key, "user") == 0) 6603 user = val; 6604 if(strcmp(key, "group") == 0) 6605 group = val; 6606 if(strcmp(key, "data/size") == 0) 6607 size = val; 6608 if(strcmp(key, "mtime") == 0) 6609 mtime = val; 6610 if(strcmp(key, "name") == 0) 6611 name = val; 6612 } 6613 if(mode != nullptr){ 6614 mode_value = strtoul(mode, &endp, 8); 6615 if(*endp != '\0') 6616 outs() << "(mode: \"" << mode << "\" contains non-octal chars) "; 6617 if(strcmp(type, "file") == 0) 6618 mode_value |= S_IFREG; 6619 PrintModeVerbose(mode_value); 6620 outs() << " "; 6621 } 6622 if(user != nullptr) 6623 outs() << format("%10s/", user); 6624 if(group != nullptr) 6625 outs() << format("%-10s ", group); 6626 if(size != nullptr) 6627 outs() << format("%7s ", size); 6628 if(mtime != nullptr){ 6629 for(m = mtime; *m != 'T' && *m != '\0'; m++) 6630 outs() << *m; 6631 if(*m == 'T') 6632 m++; 6633 outs() << " "; 6634 for( ; *m != 'Z' && *m != '\0'; m++) 6635 outs() << *m; 6636 outs() << " "; 6637 } 6638 if(name != nullptr) 6639 outs() << name; 6640 outs() << "\n"; 6641 } 6642 } 6643 6644 static void DumpBitcodeSection(MachOObjectFile *O, const char *sect, 6645 uint32_t size, bool verbose, 6646 bool PrintXarHeader, bool PrintXarFileHeaders, 6647 std::string XarMemberName) { 6648 if(size < sizeof(struct xar_header)) { 6649 outs() << "size of (__LLVM,__bundle) section too small (smaller than size " 6650 "of struct xar_header)\n"; 6651 return; 6652 } 6653 struct xar_header XarHeader; 6654 memcpy(&XarHeader, sect, sizeof(struct xar_header)); 6655 if (sys::IsLittleEndianHost) 6656 swapStruct(XarHeader); 6657 if (PrintXarHeader) { 6658 if (!XarMemberName.empty()) 6659 outs() << "In xar member " << XarMemberName << ": "; 6660 else 6661 outs() << "For (__LLVM,__bundle) section: "; 6662 outs() << "xar header\n"; 6663 if (XarHeader.magic == XAR_HEADER_MAGIC) 6664 outs() << " magic XAR_HEADER_MAGIC\n"; 6665 else 6666 outs() << " magic " 6667 << format_hex(XarHeader.magic, 10, true) 6668 << " (not XAR_HEADER_MAGIC)\n"; 6669 outs() << " size " << XarHeader.size << "\n"; 6670 outs() << " version " << XarHeader.version << "\n"; 6671 outs() << " toc_length_compressed " << XarHeader.toc_length_compressed 6672 << "\n"; 6673 outs() << "toc_length_uncompressed " << XarHeader.toc_length_uncompressed 6674 << "\n"; 6675 outs() << " cksum_alg "; 6676 switch (XarHeader.cksum_alg) { 6677 case XAR_CKSUM_NONE: 6678 outs() << "XAR_CKSUM_NONE\n"; 6679 break; 6680 case XAR_CKSUM_SHA1: 6681 outs() << "XAR_CKSUM_SHA1\n"; 6682 break; 6683 case XAR_CKSUM_MD5: 6684 outs() << "XAR_CKSUM_MD5\n"; 6685 break; 6686 #ifdef XAR_CKSUM_SHA256 6687 case XAR_CKSUM_SHA256: 6688 outs() << "XAR_CKSUM_SHA256\n"; 6689 break; 6690 #endif 6691 #ifdef XAR_CKSUM_SHA512 6692 case XAR_CKSUM_SHA512: 6693 outs() << "XAR_CKSUM_SHA512\n"; 6694 break; 6695 #endif 6696 default: 6697 outs() << XarHeader.cksum_alg << "\n"; 6698 } 6699 } 6700 6701 SmallString<128> XarFilename; 6702 int FD; 6703 std::error_code XarEC = 6704 sys::fs::createTemporaryFile("llvm-objdump", "xar", FD, XarFilename); 6705 if (XarEC) { 6706 WithColor::error(errs(), "llvm-objdump") << XarEC.message() << "\n"; 6707 return; 6708 } 6709 ToolOutputFile XarFile(XarFilename, FD); 6710 raw_fd_ostream &XarOut = XarFile.os(); 6711 StringRef XarContents(sect, size); 6712 XarOut << XarContents; 6713 XarOut.close(); 6714 if (XarOut.has_error()) 6715 return; 6716 6717 ScopedXarFile xar(XarFilename.c_str(), READ); 6718 if (!xar) { 6719 WithColor::error(errs(), "llvm-objdump") 6720 << "can't create temporary xar archive " << XarFilename << "\n"; 6721 return; 6722 } 6723 6724 SmallString<128> TocFilename; 6725 std::error_code TocEC = 6726 sys::fs::createTemporaryFile("llvm-objdump", "toc", TocFilename); 6727 if (TocEC) { 6728 WithColor::error(errs(), "llvm-objdump") << TocEC.message() << "\n"; 6729 return; 6730 } 6731 xar_serialize(xar, TocFilename.c_str()); 6732 6733 if (PrintXarFileHeaders) { 6734 if (!XarMemberName.empty()) 6735 outs() << "In xar member " << XarMemberName << ": "; 6736 else 6737 outs() << "For (__LLVM,__bundle) section: "; 6738 outs() << "xar archive files:\n"; 6739 PrintXarFilesSummary(XarFilename.c_str(), xar); 6740 } 6741 6742 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = 6743 MemoryBuffer::getFileOrSTDIN(TocFilename.c_str()); 6744 if (std::error_code EC = FileOrErr.getError()) { 6745 WithColor::error(errs(), "llvm-objdump") << EC.message() << "\n"; 6746 return; 6747 } 6748 std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get(); 6749 6750 if (!XarMemberName.empty()) 6751 outs() << "In xar member " << XarMemberName << ": "; 6752 else 6753 outs() << "For (__LLVM,__bundle) section: "; 6754 outs() << "xar table of contents:\n"; 6755 outs() << Buffer->getBuffer() << "\n"; 6756 6757 // TODO: Go through the xar's files. 6758 ScopedXarIter xi; 6759 if(!xi){ 6760 WithColor::error(errs(), "llvm-objdump") 6761 << "can't obtain an xar iterator for xar archive " 6762 << XarFilename.c_str() << "\n"; 6763 return; 6764 } 6765 for(xar_file_t xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)){ 6766 const char *key; 6767 const char *member_name, *member_type, *member_size_string; 6768 size_t member_size; 6769 6770 ScopedXarIter xp; 6771 if(!xp){ 6772 WithColor::error(errs(), "llvm-objdump") 6773 << "can't obtain an xar iterator for xar archive " 6774 << XarFilename.c_str() << "\n"; 6775 return; 6776 } 6777 member_name = NULL; 6778 member_type = NULL; 6779 member_size_string = NULL; 6780 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){ 6781 const char *val = nullptr; 6782 xar_prop_get(xf, key, &val); 6783 #if 0 // Useful for debugging. 6784 outs() << "key: " << key << " value: " << val << "\n"; 6785 #endif 6786 if (strcmp(key, "name") == 0) 6787 member_name = val; 6788 if (strcmp(key, "type") == 0) 6789 member_type = val; 6790 if (strcmp(key, "data/size") == 0) 6791 member_size_string = val; 6792 } 6793 /* 6794 * If we find a file with a name, date/size and type properties 6795 * and with the type being "file" see if that is a xar file. 6796 */ 6797 if (member_name != NULL && member_type != NULL && 6798 strcmp(member_type, "file") == 0 && 6799 member_size_string != NULL){ 6800 // Extract the file into a buffer. 6801 char *endptr; 6802 member_size = strtoul(member_size_string, &endptr, 10); 6803 if (*endptr == '\0' && member_size != 0) { 6804 char *buffer; 6805 if (xar_extract_tobuffersz(xar, xf, &buffer, &member_size) == 0) { 6806 #if 0 // Useful for debugging. 6807 outs() << "xar member: " << member_name << " extracted\n"; 6808 #endif 6809 // Set the XarMemberName we want to see printed in the header. 6810 std::string OldXarMemberName; 6811 // If XarMemberName is already set this is nested. So 6812 // save the old name and create the nested name. 6813 if (!XarMemberName.empty()) { 6814 OldXarMemberName = XarMemberName; 6815 XarMemberName = 6816 (Twine("[") + XarMemberName + "]" + member_name).str(); 6817 } else { 6818 OldXarMemberName = ""; 6819 XarMemberName = member_name; 6820 } 6821 // See if this is could be a xar file (nested). 6822 if (member_size >= sizeof(struct xar_header)) { 6823 #if 0 // Useful for debugging. 6824 outs() << "could be a xar file: " << member_name << "\n"; 6825 #endif 6826 memcpy((char *)&XarHeader, buffer, sizeof(struct xar_header)); 6827 if (sys::IsLittleEndianHost) 6828 swapStruct(XarHeader); 6829 if (XarHeader.magic == XAR_HEADER_MAGIC) 6830 DumpBitcodeSection(O, buffer, member_size, verbose, 6831 PrintXarHeader, PrintXarFileHeaders, 6832 XarMemberName); 6833 } 6834 XarMemberName = OldXarMemberName; 6835 delete buffer; 6836 } 6837 } 6838 } 6839 } 6840 } 6841 #endif // defined(HAVE_LIBXAR) 6842 6843 static void printObjcMetaData(MachOObjectFile *O, bool verbose) { 6844 if (O->is64Bit()) 6845 printObjc2_64bit_MetaData(O, verbose); 6846 else { 6847 MachO::mach_header H; 6848 H = O->getHeader(); 6849 if (H.cputype == MachO::CPU_TYPE_ARM) 6850 printObjc2_32bit_MetaData(O, verbose); 6851 else { 6852 // This is the 32-bit non-arm cputype case. Which is normally 6853 // the first Objective-C ABI. But it may be the case of a 6854 // binary for the iOS simulator which is the second Objective-C 6855 // ABI. In that case printObjc1_32bit_MetaData() will determine that 6856 // and return false. 6857 if (!printObjc1_32bit_MetaData(O, verbose)) 6858 printObjc2_32bit_MetaData(O, verbose); 6859 } 6860 } 6861 } 6862 6863 // GuessLiteralPointer returns a string which for the item in the Mach-O file 6864 // for the address passed in as ReferenceValue for printing as a comment with 6865 // the instruction and also returns the corresponding type of that item 6866 // indirectly through ReferenceType. 6867 // 6868 // If ReferenceValue is an address of literal cstring then a pointer to the 6869 // cstring is returned and ReferenceType is set to 6870 // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr . 6871 // 6872 // If ReferenceValue is an address of an Objective-C CFString, Selector ref or 6873 // Class ref that name is returned and the ReferenceType is set accordingly. 6874 // 6875 // Lastly, literals which are Symbol address in a literal pool are looked for 6876 // and if found the symbol name is returned and ReferenceType is set to 6877 // LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr . 6878 // 6879 // If there is no item in the Mach-O file for the address passed in as 6880 // ReferenceValue nullptr is returned and ReferenceType is unchanged. 6881 static const char *GuessLiteralPointer(uint64_t ReferenceValue, 6882 uint64_t ReferencePC, 6883 uint64_t *ReferenceType, 6884 struct DisassembleInfo *info) { 6885 // First see if there is an external relocation entry at the ReferencePC. 6886 if (info->O->getHeader().filetype == MachO::MH_OBJECT) { 6887 uint64_t sect_addr = info->S.getAddress(); 6888 uint64_t sect_offset = ReferencePC - sect_addr; 6889 bool reloc_found = false; 6890 DataRefImpl Rel; 6891 MachO::any_relocation_info RE; 6892 bool isExtern = false; 6893 SymbolRef Symbol; 6894 for (const RelocationRef &Reloc : info->S.relocations()) { 6895 uint64_t RelocOffset = Reloc.getOffset(); 6896 if (RelocOffset == sect_offset) { 6897 Rel = Reloc.getRawDataRefImpl(); 6898 RE = info->O->getRelocation(Rel); 6899 if (info->O->isRelocationScattered(RE)) 6900 continue; 6901 isExtern = info->O->getPlainRelocationExternal(RE); 6902 if (isExtern) { 6903 symbol_iterator RelocSym = Reloc.getSymbol(); 6904 Symbol = *RelocSym; 6905 } 6906 reloc_found = true; 6907 break; 6908 } 6909 } 6910 // If there is an external relocation entry for a symbol in a section 6911 // then used that symbol's value for the value of the reference. 6912 if (reloc_found && isExtern) { 6913 if (info->O->getAnyRelocationPCRel(RE)) { 6914 unsigned Type = info->O->getAnyRelocationType(RE); 6915 if (Type == MachO::X86_64_RELOC_SIGNED) { 6916 ReferenceValue = cantFail(Symbol.getValue()); 6917 } 6918 } 6919 } 6920 } 6921 6922 // Look for literals such as Objective-C CFStrings refs, Selector refs, 6923 // Message refs and Class refs. 6924 bool classref, selref, msgref, cfstring; 6925 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref, 6926 selref, msgref, cfstring); 6927 if (classref && pointer_value == 0) { 6928 // Note the ReferenceValue is a pointer into the __objc_classrefs section. 6929 // And the pointer_value in that section is typically zero as it will be 6930 // set by dyld as part of the "bind information". 6931 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info); 6932 if (name != nullptr) { 6933 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref; 6934 const char *class_name = strrchr(name, '$'); 6935 if (class_name != nullptr && class_name[1] == '_' && 6936 class_name[2] != '\0') { 6937 info->class_name = class_name + 2; 6938 return name; 6939 } 6940 } 6941 } 6942 6943 if (classref) { 6944 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref; 6945 const char *name = 6946 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info); 6947 if (name != nullptr) 6948 info->class_name = name; 6949 else 6950 name = "bad class ref"; 6951 return name; 6952 } 6953 6954 if (cfstring) { 6955 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref; 6956 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info); 6957 return name; 6958 } 6959 6960 if (selref && pointer_value == 0) 6961 pointer_value = get_objc2_64bit_selref(ReferenceValue, info); 6962 6963 if (pointer_value != 0) 6964 ReferenceValue = pointer_value; 6965 6966 const char *name = GuessCstringPointer(ReferenceValue, info); 6967 if (name) { 6968 if (pointer_value != 0 && selref) { 6969 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref; 6970 info->selector_name = name; 6971 } else if (pointer_value != 0 && msgref) { 6972 info->class_name = nullptr; 6973 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref; 6974 info->selector_name = name; 6975 } else 6976 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr; 6977 return name; 6978 } 6979 6980 // Lastly look for an indirect symbol with this ReferenceValue which is in 6981 // a literal pool. If found return that symbol name. 6982 name = GuessIndirectSymbol(ReferenceValue, info); 6983 if (name) { 6984 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr; 6985 return name; 6986 } 6987 6988 return nullptr; 6989 } 6990 6991 // SymbolizerSymbolLookUp is the symbol lookup function passed when creating 6992 // the Symbolizer. It looks up the ReferenceValue using the info passed via the 6993 // pointer to the struct DisassembleInfo that was passed when MCSymbolizer 6994 // is created and returns the symbol name that matches the ReferenceValue or 6995 // nullptr if none. The ReferenceType is passed in for the IN type of 6996 // reference the instruction is making from the values in defined in the header 6997 // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific 6998 // Out type and the ReferenceName will also be set which is added as a comment 6999 // to the disassembled instruction. 7000 // 7001 // If the symbol name is a C++ mangled name then the demangled name is 7002 // returned through ReferenceName and ReferenceType is set to 7003 // LLVMDisassembler_ReferenceType_DeMangled_Name . 7004 // 7005 // When this is called to get a symbol name for a branch target then the 7006 // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then 7007 // SymbolValue will be looked for in the indirect symbol table to determine if 7008 // it is an address for a symbol stub. If so then the symbol name for that 7009 // stub is returned indirectly through ReferenceName and then ReferenceType is 7010 // set to LLVMDisassembler_ReferenceType_Out_SymbolStub. 7011 // 7012 // When this is called with an value loaded via a PC relative load then 7013 // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the 7014 // SymbolValue is checked to be an address of literal pointer, symbol pointer, 7015 // or an Objective-C meta data reference. If so the output ReferenceType is 7016 // set to correspond to that as well as setting the ReferenceName. 7017 static const char *SymbolizerSymbolLookUp(void *DisInfo, 7018 uint64_t ReferenceValue, 7019 uint64_t *ReferenceType, 7020 uint64_t ReferencePC, 7021 const char **ReferenceName) { 7022 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo; 7023 // If no verbose symbolic information is wanted then just return nullptr. 7024 if (!info->verbose) { 7025 *ReferenceName = nullptr; 7026 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7027 return nullptr; 7028 } 7029 7030 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap); 7031 7032 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) { 7033 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info); 7034 if (*ReferenceName != nullptr) { 7035 method_reference(info, ReferenceType, ReferenceName); 7036 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message) 7037 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub; 7038 } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) { 7039 if (info->demangled_name != nullptr) 7040 free(info->demangled_name); 7041 int status; 7042 info->demangled_name = 7043 itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status); 7044 if (info->demangled_name != nullptr) { 7045 *ReferenceName = info->demangled_name; 7046 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name; 7047 } else 7048 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7049 } else 7050 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7051 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) { 7052 *ReferenceName = 7053 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info); 7054 if (*ReferenceName) 7055 method_reference(info, ReferenceType, ReferenceName); 7056 else 7057 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7058 // If this is arm64 and the reference is an adrp instruction save the 7059 // instruction, passed in ReferenceValue and the address of the instruction 7060 // for use later if we see and add immediate instruction. 7061 } else if (info->O->getArch() == Triple::aarch64 && 7062 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) { 7063 info->adrp_inst = ReferenceValue; 7064 info->adrp_addr = ReferencePC; 7065 SymbolName = nullptr; 7066 *ReferenceName = nullptr; 7067 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7068 // If this is arm64 and reference is an add immediate instruction and we 7069 // have 7070 // seen an adrp instruction just before it and the adrp's Xd register 7071 // matches 7072 // this add's Xn register reconstruct the value being referenced and look to 7073 // see if it is a literal pointer. Note the add immediate instruction is 7074 // passed in ReferenceValue. 7075 } else if (info->O->getArch() == Triple::aarch64 && 7076 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri && 7077 ReferencePC - 4 == info->adrp_addr && 7078 (info->adrp_inst & 0x9f000000) == 0x90000000 && 7079 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) { 7080 uint32_t addxri_inst; 7081 uint64_t adrp_imm, addxri_imm; 7082 7083 adrp_imm = 7084 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3); 7085 if (info->adrp_inst & 0x0200000) 7086 adrp_imm |= 0xfffffffffc000000LL; 7087 7088 addxri_inst = ReferenceValue; 7089 addxri_imm = (addxri_inst >> 10) & 0xfff; 7090 if (((addxri_inst >> 22) & 0x3) == 1) 7091 addxri_imm <<= 12; 7092 7093 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) + 7094 (adrp_imm << 12) + addxri_imm; 7095 7096 *ReferenceName = 7097 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info); 7098 if (*ReferenceName == nullptr) 7099 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7100 // If this is arm64 and the reference is a load register instruction and we 7101 // have seen an adrp instruction just before it and the adrp's Xd register 7102 // matches this add's Xn register reconstruct the value being referenced and 7103 // look to see if it is a literal pointer. Note the load register 7104 // instruction is passed in ReferenceValue. 7105 } else if (info->O->getArch() == Triple::aarch64 && 7106 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui && 7107 ReferencePC - 4 == info->adrp_addr && 7108 (info->adrp_inst & 0x9f000000) == 0x90000000 && 7109 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) { 7110 uint32_t ldrxui_inst; 7111 uint64_t adrp_imm, ldrxui_imm; 7112 7113 adrp_imm = 7114 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3); 7115 if (info->adrp_inst & 0x0200000) 7116 adrp_imm |= 0xfffffffffc000000LL; 7117 7118 ldrxui_inst = ReferenceValue; 7119 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff; 7120 7121 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) + 7122 (adrp_imm << 12) + (ldrxui_imm << 3); 7123 7124 *ReferenceName = 7125 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info); 7126 if (*ReferenceName == nullptr) 7127 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7128 } 7129 // If this arm64 and is an load register (PC-relative) instruction the 7130 // ReferenceValue is the PC plus the immediate value. 7131 else if (info->O->getArch() == Triple::aarch64 && 7132 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl || 7133 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) { 7134 *ReferenceName = 7135 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info); 7136 if (*ReferenceName == nullptr) 7137 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7138 } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) { 7139 if (info->demangled_name != nullptr) 7140 free(info->demangled_name); 7141 int status; 7142 info->demangled_name = 7143 itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status); 7144 if (info->demangled_name != nullptr) { 7145 *ReferenceName = info->demangled_name; 7146 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name; 7147 } 7148 } 7149 else { 7150 *ReferenceName = nullptr; 7151 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 7152 } 7153 7154 return SymbolName; 7155 } 7156 7157 /// Emits the comments that are stored in the CommentStream. 7158 /// Each comment in the CommentStream must end with a newline. 7159 static void emitComments(raw_svector_ostream &CommentStream, 7160 SmallString<128> &CommentsToEmit, 7161 formatted_raw_ostream &FormattedOS, 7162 const MCAsmInfo &MAI) { 7163 // Flush the stream before taking its content. 7164 StringRef Comments = CommentsToEmit.str(); 7165 // Get the default information for printing a comment. 7166 StringRef CommentBegin = MAI.getCommentString(); 7167 unsigned CommentColumn = MAI.getCommentColumn(); 7168 bool IsFirst = true; 7169 while (!Comments.empty()) { 7170 if (!IsFirst) 7171 FormattedOS << '\n'; 7172 // Emit a line of comments. 7173 FormattedOS.PadToColumn(CommentColumn); 7174 size_t Position = Comments.find('\n'); 7175 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position); 7176 // Move after the newline character. 7177 Comments = Comments.substr(Position + 1); 7178 IsFirst = false; 7179 } 7180 FormattedOS.flush(); 7181 7182 // Tell the comment stream that the vector changed underneath it. 7183 CommentsToEmit.clear(); 7184 } 7185 7186 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, 7187 StringRef DisSegName, StringRef DisSectName) { 7188 const char *McpuDefault = nullptr; 7189 const Target *ThumbTarget = nullptr; 7190 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget); 7191 if (!TheTarget) { 7192 // GetTarget prints out stuff. 7193 return; 7194 } 7195 std::string MachOMCPU; 7196 if (MCPU.empty() && McpuDefault) 7197 MachOMCPU = McpuDefault; 7198 else 7199 MachOMCPU = MCPU; 7200 7201 #define CHECK_TARGET_INFO_CREATION(NAME) \ 7202 do { \ 7203 if (!NAME) { \ 7204 WithColor::error(errs(), "llvm-objdump") \ 7205 << "couldn't initialize disassembler for target " << TripleName \ 7206 << '\n'; \ 7207 return; \ 7208 } \ 7209 } while (false) 7210 #define CHECK_THUMB_TARGET_INFO_CREATION(NAME) \ 7211 do { \ 7212 if (!NAME) { \ 7213 WithColor::error(errs(), "llvm-objdump") \ 7214 << "couldn't initialize disassembler for target " << ThumbTripleName \ 7215 << '\n'; \ 7216 return; \ 7217 } \ 7218 } while (false) 7219 7220 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo()); 7221 CHECK_TARGET_INFO_CREATION(InstrInfo); 7222 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo; 7223 if (ThumbTarget) { 7224 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo()); 7225 CHECK_THUMB_TARGET_INFO_CREATION(ThumbInstrInfo); 7226 } 7227 7228 // Package up features to be passed to target/subtarget 7229 std::string FeaturesStr; 7230 if (!MAttrs.empty()) { 7231 SubtargetFeatures Features; 7232 for (unsigned i = 0; i != MAttrs.size(); ++i) 7233 Features.AddFeature(MAttrs[i]); 7234 FeaturesStr = Features.getString(); 7235 } 7236 7237 MCTargetOptions MCOptions; 7238 // Set up disassembler. 7239 std::unique_ptr<const MCRegisterInfo> MRI( 7240 TheTarget->createMCRegInfo(TripleName)); 7241 CHECK_TARGET_INFO_CREATION(MRI); 7242 std::unique_ptr<const MCAsmInfo> AsmInfo( 7243 TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions)); 7244 CHECK_TARGET_INFO_CREATION(AsmInfo); 7245 std::unique_ptr<const MCSubtargetInfo> STI( 7246 TheTarget->createMCSubtargetInfo(TripleName, MachOMCPU, FeaturesStr)); 7247 CHECK_TARGET_INFO_CREATION(STI); 7248 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr); 7249 std::unique_ptr<MCDisassembler> DisAsm( 7250 TheTarget->createMCDisassembler(*STI, Ctx)); 7251 CHECK_TARGET_INFO_CREATION(DisAsm); 7252 std::unique_ptr<MCSymbolizer> Symbolizer; 7253 struct DisassembleInfo SymbolizerInfo(nullptr, nullptr, nullptr, false); 7254 std::unique_ptr<MCRelocationInfo> RelInfo( 7255 TheTarget->createMCRelocationInfo(TripleName, Ctx)); 7256 if (RelInfo) { 7257 Symbolizer.reset(TheTarget->createMCSymbolizer( 7258 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp, 7259 &SymbolizerInfo, &Ctx, std::move(RelInfo))); 7260 DisAsm->setSymbolizer(std::move(Symbolizer)); 7261 } 7262 int AsmPrinterVariant = AsmInfo->getAssemblerDialect(); 7263 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter( 7264 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI)); 7265 CHECK_TARGET_INFO_CREATION(IP); 7266 // Set the display preference for hex vs. decimal immediates. 7267 IP->setPrintImmHex(PrintImmHex); 7268 // Comment stream and backing vector. 7269 SmallString<128> CommentsToEmit; 7270 raw_svector_ostream CommentStream(CommentsToEmit); 7271 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that 7272 // if it is done then arm64 comments for string literals don't get printed 7273 // and some constant get printed instead and not setting it causes intel 7274 // (32-bit and 64-bit) comments printed with different spacing before the 7275 // comment causing different diffs with the 'C' disassembler library API. 7276 // IP->setCommentStream(CommentStream); 7277 7278 // Set up separate thumb disassembler if needed. 7279 std::unique_ptr<const MCRegisterInfo> ThumbMRI; 7280 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo; 7281 std::unique_ptr<const MCSubtargetInfo> ThumbSTI; 7282 std::unique_ptr<MCDisassembler> ThumbDisAsm; 7283 std::unique_ptr<MCInstPrinter> ThumbIP; 7284 std::unique_ptr<MCContext> ThumbCtx; 7285 std::unique_ptr<MCSymbolizer> ThumbSymbolizer; 7286 struct DisassembleInfo ThumbSymbolizerInfo(nullptr, nullptr, nullptr, false); 7287 std::unique_ptr<MCRelocationInfo> ThumbRelInfo; 7288 if (ThumbTarget) { 7289 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName)); 7290 CHECK_THUMB_TARGET_INFO_CREATION(ThumbMRI); 7291 ThumbAsmInfo.reset( 7292 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName, MCOptions)); 7293 CHECK_THUMB_TARGET_INFO_CREATION(ThumbAsmInfo); 7294 ThumbSTI.reset( 7295 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MachOMCPU, 7296 FeaturesStr)); 7297 CHECK_THUMB_TARGET_INFO_CREATION(ThumbSTI); 7298 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr)); 7299 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx)); 7300 CHECK_THUMB_TARGET_INFO_CREATION(ThumbDisAsm); 7301 MCContext *PtrThumbCtx = ThumbCtx.get(); 7302 ThumbRelInfo.reset( 7303 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx)); 7304 if (ThumbRelInfo) { 7305 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer( 7306 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp, 7307 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo))); 7308 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer)); 7309 } 7310 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect(); 7311 ThumbIP.reset(ThumbTarget->createMCInstPrinter( 7312 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo, 7313 *ThumbInstrInfo, *ThumbMRI)); 7314 CHECK_THUMB_TARGET_INFO_CREATION(ThumbIP); 7315 // Set the display preference for hex vs. decimal immediates. 7316 ThumbIP->setPrintImmHex(PrintImmHex); 7317 } 7318 7319 #undef CHECK_TARGET_INFO_CREATION 7320 #undef CHECK_THUMB_TARGET_INFO_CREATION 7321 7322 MachO::mach_header Header = MachOOF->getHeader(); 7323 7324 // FIXME: Using the -cfg command line option, this code used to be able to 7325 // annotate relocations with the referenced symbol's name, and if this was 7326 // inside a __[cf]string section, the data it points to. This is now replaced 7327 // by the upcoming MCSymbolizer, which needs the appropriate setup done above. 7328 std::vector<SectionRef> Sections; 7329 std::vector<SymbolRef> Symbols; 7330 SmallVector<uint64_t, 8> FoundFns; 7331 uint64_t BaseSegmentAddress = 0; 7332 7333 getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns, 7334 BaseSegmentAddress); 7335 7336 // Sort the symbols by address, just in case they didn't come in that way. 7337 llvm::sort(Symbols, SymbolSorter()); 7338 7339 // Build a data in code table that is sorted on by the address of each entry. 7340 uint64_t BaseAddress = 0; 7341 if (Header.filetype == MachO::MH_OBJECT) 7342 BaseAddress = Sections[0].getAddress(); 7343 else 7344 BaseAddress = BaseSegmentAddress; 7345 DiceTable Dices; 7346 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices(); 7347 DI != DE; ++DI) { 7348 uint32_t Offset; 7349 DI->getOffset(Offset); 7350 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI)); 7351 } 7352 array_pod_sort(Dices.begin(), Dices.end()); 7353 7354 // Try to find debug info and set up the DIContext for it. 7355 std::unique_ptr<DIContext> diContext; 7356 std::unique_ptr<Binary> DSYMBinary; 7357 std::unique_ptr<MemoryBuffer> DSYMBuf; 7358 if (UseDbg) { 7359 ObjectFile *DbgObj = MachOOF; 7360 7361 // A separate DSym file path was specified, parse it as a macho file, 7362 // get the sections and supply it to the section name parsing machinery. 7363 if (!DSYMFile.empty()) { 7364 std::string DSYMPath(DSYMFile); 7365 7366 // If DSYMPath is a .dSYM directory, append the Mach-O file. 7367 if (llvm::sys::fs::is_directory(DSYMPath) && 7368 llvm::sys::path::extension(DSYMPath) == ".dSYM") { 7369 SmallString<128> ShortName(llvm::sys::path::filename(DSYMPath)); 7370 llvm::sys::path::replace_extension(ShortName, ""); 7371 SmallString<1024> FullPath(DSYMPath); 7372 llvm::sys::path::append(FullPath, "Contents", "Resources", "DWARF", 7373 ShortName); 7374 DSYMPath = std::string(FullPath.str()); 7375 } 7376 7377 // Load the file. 7378 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr = 7379 MemoryBuffer::getFileOrSTDIN(DSYMPath); 7380 if (std::error_code EC = BufOrErr.getError()) { 7381 reportError(errorCodeToError(EC), DSYMPath); 7382 return; 7383 } 7384 7385 // We need to keep the file alive, because we're replacing DbgObj with it. 7386 DSYMBuf = std::move(BufOrErr.get()); 7387 7388 Expected<std::unique_ptr<Binary>> BinaryOrErr = 7389 createBinary(DSYMBuf.get()->getMemBufferRef()); 7390 if (!BinaryOrErr) { 7391 reportError(BinaryOrErr.takeError(), DSYMPath); 7392 return; 7393 } 7394 7395 // We need to keep the Binary alive with the buffer 7396 DSYMBinary = std::move(BinaryOrErr.get()); 7397 if (ObjectFile *O = dyn_cast<ObjectFile>(DSYMBinary.get())) { 7398 // this is a Mach-O object file, use it 7399 if (MachOObjectFile *MachDSYM = dyn_cast<MachOObjectFile>(&*O)) { 7400 DbgObj = MachDSYM; 7401 } 7402 else { 7403 WithColor::error(errs(), "llvm-objdump") 7404 << DSYMPath << " is not a Mach-O file type.\n"; 7405 return; 7406 } 7407 } 7408 else if (auto UB = dyn_cast<MachOUniversalBinary>(DSYMBinary.get())){ 7409 // this is a Universal Binary, find a Mach-O for this architecture 7410 uint32_t CPUType, CPUSubType; 7411 const char *ArchFlag; 7412 if (MachOOF->is64Bit()) { 7413 const MachO::mach_header_64 H_64 = MachOOF->getHeader64(); 7414 CPUType = H_64.cputype; 7415 CPUSubType = H_64.cpusubtype; 7416 } else { 7417 const MachO::mach_header H = MachOOF->getHeader(); 7418 CPUType = H.cputype; 7419 CPUSubType = H.cpusubtype; 7420 } 7421 Triple T = MachOObjectFile::getArchTriple(CPUType, CPUSubType, nullptr, 7422 &ArchFlag); 7423 Expected<std::unique_ptr<MachOObjectFile>> MachDSYM = 7424 UB->getMachOObjectForArch(ArchFlag); 7425 if (!MachDSYM) { 7426 reportError(MachDSYM.takeError(), DSYMPath); 7427 return; 7428 } 7429 7430 // We need to keep the Binary alive with the buffer 7431 DbgObj = &*MachDSYM.get(); 7432 DSYMBinary = std::move(*MachDSYM); 7433 } 7434 else { 7435 WithColor::error(errs(), "llvm-objdump") 7436 << DSYMPath << " is not a Mach-O or Universal file type.\n"; 7437 return; 7438 } 7439 } 7440 7441 // Setup the DIContext 7442 diContext = DWARFContext::create(*DbgObj); 7443 } 7444 7445 if (FilterSections.empty()) 7446 outs() << "(" << DisSegName << "," << DisSectName << ") section\n"; 7447 7448 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) { 7449 Expected<StringRef> SecNameOrErr = Sections[SectIdx].getName(); 7450 if (!SecNameOrErr) { 7451 consumeError(SecNameOrErr.takeError()); 7452 continue; 7453 } 7454 if (*SecNameOrErr != DisSectName) 7455 continue; 7456 7457 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl(); 7458 7459 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR); 7460 if (SegmentName != DisSegName) 7461 continue; 7462 7463 StringRef BytesStr = 7464 unwrapOrError(Sections[SectIdx].getContents(), Filename); 7465 ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(BytesStr); 7466 uint64_t SectAddress = Sections[SectIdx].getAddress(); 7467 7468 bool symbolTableWorked = false; 7469 7470 // Create a map of symbol addresses to symbol names for use by 7471 // the SymbolizerSymbolLookUp() routine. 7472 SymbolAddressMap AddrMap; 7473 bool DisSymNameFound = false; 7474 for (const SymbolRef &Symbol : MachOOF->symbols()) { 7475 SymbolRef::Type ST = 7476 unwrapOrError(Symbol.getType(), MachOOF->getFileName()); 7477 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data || 7478 ST == SymbolRef::ST_Other) { 7479 uint64_t Address = cantFail(Symbol.getValue()); 7480 StringRef SymName = 7481 unwrapOrError(Symbol.getName(), MachOOF->getFileName()); 7482 AddrMap[Address] = SymName; 7483 if (!DisSymName.empty() && DisSymName == SymName) 7484 DisSymNameFound = true; 7485 } 7486 } 7487 if (!DisSymName.empty() && !DisSymNameFound) { 7488 outs() << "Can't find -dis-symname: " << DisSymName << "\n"; 7489 return; 7490 } 7491 // Set up the block of info used by the Symbolizer call backs. 7492 SymbolizerInfo.verbose = !NoSymbolicOperands; 7493 SymbolizerInfo.O = MachOOF; 7494 SymbolizerInfo.S = Sections[SectIdx]; 7495 SymbolizerInfo.AddrMap = &AddrMap; 7496 SymbolizerInfo.Sections = &Sections; 7497 // Same for the ThumbSymbolizer 7498 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands; 7499 ThumbSymbolizerInfo.O = MachOOF; 7500 ThumbSymbolizerInfo.S = Sections[SectIdx]; 7501 ThumbSymbolizerInfo.AddrMap = &AddrMap; 7502 ThumbSymbolizerInfo.Sections = &Sections; 7503 7504 unsigned int Arch = MachOOF->getArch(); 7505 7506 // Skip all symbols if this is a stubs file. 7507 if (Bytes.empty()) 7508 return; 7509 7510 // If the section has symbols but no symbol at the start of the section 7511 // these are used to make sure the bytes before the first symbol are 7512 // disassembled. 7513 bool FirstSymbol = true; 7514 bool FirstSymbolAtSectionStart = true; 7515 7516 // Disassemble symbol by symbol. 7517 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) { 7518 StringRef SymName = 7519 unwrapOrError(Symbols[SymIdx].getName(), MachOOF->getFileName()); 7520 SymbolRef::Type ST = 7521 unwrapOrError(Symbols[SymIdx].getType(), MachOOF->getFileName()); 7522 if (ST != SymbolRef::ST_Function && ST != SymbolRef::ST_Data) 7523 continue; 7524 7525 // Make sure the symbol is defined in this section. 7526 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]); 7527 if (!containsSym) { 7528 if (!DisSymName.empty() && DisSymName == SymName) { 7529 outs() << "-dis-symname: " << DisSymName << " not in the section\n"; 7530 return; 7531 } 7532 continue; 7533 } 7534 // The __mh_execute_header is special and we need to deal with that fact 7535 // this symbol is before the start of the (__TEXT,__text) section and at the 7536 // address of the start of the __TEXT segment. This is because this symbol 7537 // is an N_SECT symbol in the (__TEXT,__text) but its address is before the 7538 // start of the section in a standard MH_EXECUTE filetype. 7539 if (!DisSymName.empty() && DisSymName == "__mh_execute_header") { 7540 outs() << "-dis-symname: __mh_execute_header not in any section\n"; 7541 return; 7542 } 7543 // When this code is trying to disassemble a symbol at a time and in the 7544 // case there is only the __mh_execute_header symbol left as in a stripped 7545 // executable, we need to deal with this by ignoring this symbol so the 7546 // whole section is disassembled and this symbol is then not displayed. 7547 if (SymName == "__mh_execute_header" || SymName == "__mh_dylib_header" || 7548 SymName == "__mh_bundle_header" || SymName == "__mh_object_header" || 7549 SymName == "__mh_preload_header" || SymName == "__mh_dylinker_header") 7550 continue; 7551 7552 // If we are only disassembling one symbol see if this is that symbol. 7553 if (!DisSymName.empty() && DisSymName != SymName) 7554 continue; 7555 7556 // Start at the address of the symbol relative to the section's address. 7557 uint64_t SectSize = Sections[SectIdx].getSize(); 7558 uint64_t Start = cantFail(Symbols[SymIdx].getValue()); 7559 uint64_t SectionAddress = Sections[SectIdx].getAddress(); 7560 Start -= SectionAddress; 7561 7562 if (Start > SectSize) { 7563 outs() << "section data ends, " << SymName 7564 << " lies outside valid range\n"; 7565 return; 7566 } 7567 7568 // Stop disassembling either at the beginning of the next symbol or at 7569 // the end of the section. 7570 bool containsNextSym = false; 7571 uint64_t NextSym = 0; 7572 uint64_t NextSymIdx = SymIdx + 1; 7573 while (Symbols.size() > NextSymIdx) { 7574 SymbolRef::Type NextSymType = unwrapOrError( 7575 Symbols[NextSymIdx].getType(), MachOOF->getFileName()); 7576 if (NextSymType == SymbolRef::ST_Function) { 7577 containsNextSym = 7578 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]); 7579 NextSym = cantFail(Symbols[NextSymIdx].getValue()); 7580 NextSym -= SectionAddress; 7581 break; 7582 } 7583 ++NextSymIdx; 7584 } 7585 7586 uint64_t End = containsNextSym ? std::min(NextSym, SectSize) : SectSize; 7587 uint64_t Size; 7588 7589 symbolTableWorked = true; 7590 7591 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl(); 7592 uint32_t SymbolFlags = cantFail(MachOOF->getSymbolFlags(Symb)); 7593 bool IsThumb = SymbolFlags & SymbolRef::SF_Thumb; 7594 7595 // We only need the dedicated Thumb target if there's a real choice 7596 // (i.e. we're not targeting M-class) and the function is Thumb. 7597 bool UseThumbTarget = IsThumb && ThumbTarget; 7598 7599 // If we are not specifying a symbol to start disassembly with and this 7600 // is the first symbol in the section but not at the start of the section 7601 // then move the disassembly index to the start of the section and 7602 // don't print the symbol name just yet. This is so the bytes before the 7603 // first symbol are disassembled. 7604 uint64_t SymbolStart = Start; 7605 if (DisSymName.empty() && FirstSymbol && Start != 0) { 7606 FirstSymbolAtSectionStart = false; 7607 Start = 0; 7608 } 7609 else 7610 outs() << SymName << ":\n"; 7611 7612 DILineInfo lastLine; 7613 for (uint64_t Index = Start; Index < End; Index += Size) { 7614 MCInst Inst; 7615 7616 // If this is the first symbol in the section and it was not at the 7617 // start of the section, see if we are at its Index now and if so print 7618 // the symbol name. 7619 if (FirstSymbol && !FirstSymbolAtSectionStart && Index == SymbolStart) 7620 outs() << SymName << ":\n"; 7621 7622 uint64_t PC = SectAddress + Index; 7623 if (!NoLeadingAddr) { 7624 if (FullLeadingAddr) { 7625 if (MachOOF->is64Bit()) 7626 outs() << format("%016" PRIx64, PC); 7627 else 7628 outs() << format("%08" PRIx64, PC); 7629 } else { 7630 outs() << format("%8" PRIx64 ":", PC); 7631 } 7632 } 7633 if (!NoShowRawInsn || Arch == Triple::arm) 7634 outs() << "\t"; 7635 7636 if (DumpAndSkipDataInCode(PC, Bytes.data() + Index, Dices, Size)) 7637 continue; 7638 7639 SmallVector<char, 64> AnnotationsBytes; 7640 raw_svector_ostream Annotations(AnnotationsBytes); 7641 7642 bool gotInst; 7643 if (UseThumbTarget) 7644 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index), 7645 PC, Annotations); 7646 else 7647 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC, 7648 Annotations); 7649 if (gotInst) { 7650 if (!NoShowRawInsn || Arch == Triple::arm) { 7651 dumpBytes(makeArrayRef(Bytes.data() + Index, Size), outs()); 7652 } 7653 formatted_raw_ostream FormattedOS(outs()); 7654 StringRef AnnotationsStr = Annotations.str(); 7655 if (UseThumbTarget) 7656 ThumbIP->printInst(&Inst, PC, AnnotationsStr, *ThumbSTI, 7657 FormattedOS); 7658 else 7659 IP->printInst(&Inst, PC, AnnotationsStr, *STI, FormattedOS); 7660 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo); 7661 7662 // Print debug info. 7663 if (diContext) { 7664 DILineInfo dli = diContext->getLineInfoForAddress({PC, SectIdx}); 7665 // Print valid line info if it changed. 7666 if (dli != lastLine && dli.Line != 0) 7667 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':' 7668 << dli.Column; 7669 lastLine = dli; 7670 } 7671 outs() << "\n"; 7672 } else { 7673 if (MachOOF->getArchTriple().isX86()) { 7674 outs() << format("\t.byte 0x%02x #bad opcode\n", 7675 *(Bytes.data() + Index) & 0xff); 7676 Size = 1; // skip exactly one illegible byte and move on. 7677 } else if (Arch == Triple::aarch64 || 7678 (Arch == Triple::arm && !IsThumb)) { 7679 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) | 7680 (*(Bytes.data() + Index + 1) & 0xff) << 8 | 7681 (*(Bytes.data() + Index + 2) & 0xff) << 16 | 7682 (*(Bytes.data() + Index + 3) & 0xff) << 24; 7683 outs() << format("\t.long\t0x%08x\n", opcode); 7684 Size = 4; 7685 } else if (Arch == Triple::arm) { 7686 assert(IsThumb && "ARM mode should have been dealt with above"); 7687 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) | 7688 (*(Bytes.data() + Index + 1) & 0xff) << 8; 7689 outs() << format("\t.short\t0x%04x\n", opcode); 7690 Size = 2; 7691 } else{ 7692 WithColor::warning(errs(), "llvm-objdump") 7693 << "invalid instruction encoding\n"; 7694 if (Size == 0) 7695 Size = 1; // skip illegible bytes 7696 } 7697 } 7698 } 7699 // Now that we are done disassembled the first symbol set the bool that 7700 // were doing this to false. 7701 FirstSymbol = false; 7702 } 7703 if (!symbolTableWorked) { 7704 // Reading the symbol table didn't work, disassemble the whole section. 7705 uint64_t SectAddress = Sections[SectIdx].getAddress(); 7706 uint64_t SectSize = Sections[SectIdx].getSize(); 7707 uint64_t InstSize; 7708 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) { 7709 MCInst Inst; 7710 7711 uint64_t PC = SectAddress + Index; 7712 7713 if (DumpAndSkipDataInCode(PC, Bytes.data() + Index, Dices, InstSize)) 7714 continue; 7715 7716 SmallVector<char, 64> AnnotationsBytes; 7717 raw_svector_ostream Annotations(AnnotationsBytes); 7718 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC, 7719 Annotations)) { 7720 if (!NoLeadingAddr) { 7721 if (FullLeadingAddr) { 7722 if (MachOOF->is64Bit()) 7723 outs() << format("%016" PRIx64, PC); 7724 else 7725 outs() << format("%08" PRIx64, PC); 7726 } else { 7727 outs() << format("%8" PRIx64 ":", PC); 7728 } 7729 } 7730 if (!NoShowRawInsn || Arch == Triple::arm) { 7731 outs() << "\t"; 7732 dumpBytes(makeArrayRef(Bytes.data() + Index, InstSize), outs()); 7733 } 7734 StringRef AnnotationsStr = Annotations.str(); 7735 IP->printInst(&Inst, PC, AnnotationsStr, *STI, outs()); 7736 outs() << "\n"; 7737 } else { 7738 if (MachOOF->getArchTriple().isX86()) { 7739 outs() << format("\t.byte 0x%02x #bad opcode\n", 7740 *(Bytes.data() + Index) & 0xff); 7741 InstSize = 1; // skip exactly one illegible byte and move on. 7742 } else { 7743 WithColor::warning(errs(), "llvm-objdump") 7744 << "invalid instruction encoding\n"; 7745 if (InstSize == 0) 7746 InstSize = 1; // skip illegible bytes 7747 } 7748 } 7749 } 7750 } 7751 // The TripleName's need to be reset if we are called again for a different 7752 // architecture. 7753 TripleName = ""; 7754 ThumbTripleName = ""; 7755 7756 if (SymbolizerInfo.demangled_name != nullptr) 7757 free(SymbolizerInfo.demangled_name); 7758 if (ThumbSymbolizerInfo.demangled_name != nullptr) 7759 free(ThumbSymbolizerInfo.demangled_name); 7760 } 7761 } 7762 7763 //===----------------------------------------------------------------------===// 7764 // __compact_unwind section dumping 7765 //===----------------------------------------------------------------------===// 7766 7767 namespace { 7768 7769 template <typename T> 7770 static uint64_t read(StringRef Contents, ptrdiff_t Offset) { 7771 using llvm::support::little; 7772 using llvm::support::unaligned; 7773 7774 if (Offset + sizeof(T) > Contents.size()) { 7775 outs() << "warning: attempt to read past end of buffer\n"; 7776 return T(); 7777 } 7778 7779 uint64_t Val = 7780 support::endian::read<T, little, unaligned>(Contents.data() + Offset); 7781 return Val; 7782 } 7783 7784 template <typename T> 7785 static uint64_t readNext(StringRef Contents, ptrdiff_t &Offset) { 7786 T Val = read<T>(Contents, Offset); 7787 Offset += sizeof(T); 7788 return Val; 7789 } 7790 7791 struct CompactUnwindEntry { 7792 uint32_t OffsetInSection; 7793 7794 uint64_t FunctionAddr; 7795 uint32_t Length; 7796 uint32_t CompactEncoding; 7797 uint64_t PersonalityAddr; 7798 uint64_t LSDAAddr; 7799 7800 RelocationRef FunctionReloc; 7801 RelocationRef PersonalityReloc; 7802 RelocationRef LSDAReloc; 7803 7804 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64) 7805 : OffsetInSection(Offset) { 7806 if (Is64) 7807 read<uint64_t>(Contents, Offset); 7808 else 7809 read<uint32_t>(Contents, Offset); 7810 } 7811 7812 private: 7813 template <typename UIntPtr> void read(StringRef Contents, ptrdiff_t Offset) { 7814 FunctionAddr = readNext<UIntPtr>(Contents, Offset); 7815 Length = readNext<uint32_t>(Contents, Offset); 7816 CompactEncoding = readNext<uint32_t>(Contents, Offset); 7817 PersonalityAddr = readNext<UIntPtr>(Contents, Offset); 7818 LSDAAddr = readNext<UIntPtr>(Contents, Offset); 7819 } 7820 }; 7821 } 7822 7823 /// Given a relocation from __compact_unwind, consisting of the RelocationRef 7824 /// and data being relocated, determine the best base Name and Addend to use for 7825 /// display purposes. 7826 /// 7827 /// 1. An Extern relocation will directly reference a symbol (and the data is 7828 /// then already an addend), so use that. 7829 /// 2. Otherwise the data is an offset in the object file's layout; try to find 7830 // a symbol before it in the same section, and use the offset from there. 7831 /// 3. Finally, if all that fails, fall back to an offset from the start of the 7832 /// referenced section. 7833 static void findUnwindRelocNameAddend(const MachOObjectFile *Obj, 7834 std::map<uint64_t, SymbolRef> &Symbols, 7835 const RelocationRef &Reloc, uint64_t Addr, 7836 StringRef &Name, uint64_t &Addend) { 7837 if (Reloc.getSymbol() != Obj->symbol_end()) { 7838 Name = unwrapOrError(Reloc.getSymbol()->getName(), Obj->getFileName()); 7839 Addend = Addr; 7840 return; 7841 } 7842 7843 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl()); 7844 SectionRef RelocSection = Obj->getAnyRelocationSection(RE); 7845 7846 uint64_t SectionAddr = RelocSection.getAddress(); 7847 7848 auto Sym = Symbols.upper_bound(Addr); 7849 if (Sym == Symbols.begin()) { 7850 // The first symbol in the object is after this reference, the best we can 7851 // do is section-relative notation. 7852 if (Expected<StringRef> NameOrErr = RelocSection.getName()) 7853 Name = *NameOrErr; 7854 else 7855 consumeError(NameOrErr.takeError()); 7856 7857 Addend = Addr - SectionAddr; 7858 return; 7859 } 7860 7861 // Go back one so that SymbolAddress <= Addr. 7862 --Sym; 7863 7864 section_iterator SymSection = 7865 unwrapOrError(Sym->second.getSection(), Obj->getFileName()); 7866 if (RelocSection == *SymSection) { 7867 // There's a valid symbol in the same section before this reference. 7868 Name = unwrapOrError(Sym->second.getName(), Obj->getFileName()); 7869 Addend = Addr - Sym->first; 7870 return; 7871 } 7872 7873 // There is a symbol before this reference, but it's in a different 7874 // section. Probably not helpful to mention it, so use the section name. 7875 if (Expected<StringRef> NameOrErr = RelocSection.getName()) 7876 Name = *NameOrErr; 7877 else 7878 consumeError(NameOrErr.takeError()); 7879 7880 Addend = Addr - SectionAddr; 7881 } 7882 7883 static void printUnwindRelocDest(const MachOObjectFile *Obj, 7884 std::map<uint64_t, SymbolRef> &Symbols, 7885 const RelocationRef &Reloc, uint64_t Addr) { 7886 StringRef Name; 7887 uint64_t Addend; 7888 7889 if (!Reloc.getObject()) 7890 return; 7891 7892 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend); 7893 7894 outs() << Name; 7895 if (Addend) 7896 outs() << " + " << format("0x%" PRIx64, Addend); 7897 } 7898 7899 static void 7900 printMachOCompactUnwindSection(const MachOObjectFile *Obj, 7901 std::map<uint64_t, SymbolRef> &Symbols, 7902 const SectionRef &CompactUnwind) { 7903 7904 if (!Obj->isLittleEndian()) { 7905 outs() << "Skipping big-endian __compact_unwind section\n"; 7906 return; 7907 } 7908 7909 bool Is64 = Obj->is64Bit(); 7910 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t); 7911 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t); 7912 7913 StringRef Contents = 7914 unwrapOrError(CompactUnwind.getContents(), Obj->getFileName()); 7915 SmallVector<CompactUnwindEntry, 4> CompactUnwinds; 7916 7917 // First populate the initial raw offsets, encodings and so on from the entry. 7918 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) { 7919 CompactUnwindEntry Entry(Contents, Offset, Is64); 7920 CompactUnwinds.push_back(Entry); 7921 } 7922 7923 // Next we need to look at the relocations to find out what objects are 7924 // actually being referred to. 7925 for (const RelocationRef &Reloc : CompactUnwind.relocations()) { 7926 uint64_t RelocAddress = Reloc.getOffset(); 7927 7928 uint32_t EntryIdx = RelocAddress / EntrySize; 7929 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize; 7930 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx]; 7931 7932 if (OffsetInEntry == 0) 7933 Entry.FunctionReloc = Reloc; 7934 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t)) 7935 Entry.PersonalityReloc = Reloc; 7936 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t)) 7937 Entry.LSDAReloc = Reloc; 7938 else { 7939 outs() << "Invalid relocation in __compact_unwind section\n"; 7940 return; 7941 } 7942 } 7943 7944 // Finally, we're ready to print the data we've gathered. 7945 outs() << "Contents of __compact_unwind section:\n"; 7946 for (auto &Entry : CompactUnwinds) { 7947 outs() << " Entry at offset " 7948 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n"; 7949 7950 // 1. Start of the region this entry applies to. 7951 outs() << " start: " << format("0x%" PRIx64, 7952 Entry.FunctionAddr) << ' '; 7953 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr); 7954 outs() << '\n'; 7955 7956 // 2. Length of the region this entry applies to. 7957 outs() << " length: " << format("0x%" PRIx32, Entry.Length) 7958 << '\n'; 7959 // 3. The 32-bit compact encoding. 7960 outs() << " compact encoding: " 7961 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n'; 7962 7963 // 4. The personality function, if present. 7964 if (Entry.PersonalityReloc.getObject()) { 7965 outs() << " personality function: " 7966 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' '; 7967 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc, 7968 Entry.PersonalityAddr); 7969 outs() << '\n'; 7970 } 7971 7972 // 5. This entry's language-specific data area. 7973 if (Entry.LSDAReloc.getObject()) { 7974 outs() << " LSDA: " << format("0x%" PRIx64, 7975 Entry.LSDAAddr) << ' '; 7976 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr); 7977 outs() << '\n'; 7978 } 7979 } 7980 } 7981 7982 //===----------------------------------------------------------------------===// 7983 // __unwind_info section dumping 7984 //===----------------------------------------------------------------------===// 7985 7986 static void printRegularSecondLevelUnwindPage(StringRef PageData) { 7987 ptrdiff_t Pos = 0; 7988 uint32_t Kind = readNext<uint32_t>(PageData, Pos); 7989 (void)Kind; 7990 assert(Kind == 2 && "kind for a regular 2nd level index should be 2"); 7991 7992 uint16_t EntriesStart = readNext<uint16_t>(PageData, Pos); 7993 uint16_t NumEntries = readNext<uint16_t>(PageData, Pos); 7994 7995 Pos = EntriesStart; 7996 for (unsigned i = 0; i < NumEntries; ++i) { 7997 uint32_t FunctionOffset = readNext<uint32_t>(PageData, Pos); 7998 uint32_t Encoding = readNext<uint32_t>(PageData, Pos); 7999 8000 outs() << " [" << i << "]: " 8001 << "function offset=" << format("0x%08" PRIx32, FunctionOffset) 8002 << ", " 8003 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n'; 8004 } 8005 } 8006 8007 static void printCompressedSecondLevelUnwindPage( 8008 StringRef PageData, uint32_t FunctionBase, 8009 const SmallVectorImpl<uint32_t> &CommonEncodings) { 8010 ptrdiff_t Pos = 0; 8011 uint32_t Kind = readNext<uint32_t>(PageData, Pos); 8012 (void)Kind; 8013 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3"); 8014 8015 uint16_t EntriesStart = readNext<uint16_t>(PageData, Pos); 8016 uint16_t NumEntries = readNext<uint16_t>(PageData, Pos); 8017 8018 uint16_t EncodingsStart = readNext<uint16_t>(PageData, Pos); 8019 readNext<uint16_t>(PageData, Pos); 8020 StringRef PageEncodings = PageData.substr(EncodingsStart, StringRef::npos); 8021 8022 Pos = EntriesStart; 8023 for (unsigned i = 0; i < NumEntries; ++i) { 8024 uint32_t Entry = readNext<uint32_t>(PageData, Pos); 8025 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff); 8026 uint32_t EncodingIdx = Entry >> 24; 8027 8028 uint32_t Encoding; 8029 if (EncodingIdx < CommonEncodings.size()) 8030 Encoding = CommonEncodings[EncodingIdx]; 8031 else 8032 Encoding = read<uint32_t>(PageEncodings, 8033 sizeof(uint32_t) * 8034 (EncodingIdx - CommonEncodings.size())); 8035 8036 outs() << " [" << i << "]: " 8037 << "function offset=" << format("0x%08" PRIx32, FunctionOffset) 8038 << ", " 8039 << "encoding[" << EncodingIdx 8040 << "]=" << format("0x%08" PRIx32, Encoding) << '\n'; 8041 } 8042 } 8043 8044 static void printMachOUnwindInfoSection(const MachOObjectFile *Obj, 8045 std::map<uint64_t, SymbolRef> &Symbols, 8046 const SectionRef &UnwindInfo) { 8047 8048 if (!Obj->isLittleEndian()) { 8049 outs() << "Skipping big-endian __unwind_info section\n"; 8050 return; 8051 } 8052 8053 outs() << "Contents of __unwind_info section:\n"; 8054 8055 StringRef Contents = 8056 unwrapOrError(UnwindInfo.getContents(), Obj->getFileName()); 8057 ptrdiff_t Pos = 0; 8058 8059 //===---------------------------------- 8060 // Section header 8061 //===---------------------------------- 8062 8063 uint32_t Version = readNext<uint32_t>(Contents, Pos); 8064 outs() << " Version: " 8065 << format("0x%" PRIx32, Version) << '\n'; 8066 if (Version != 1) { 8067 outs() << " Skipping section with unknown version\n"; 8068 return; 8069 } 8070 8071 uint32_t CommonEncodingsStart = readNext<uint32_t>(Contents, Pos); 8072 outs() << " Common encodings array section offset: " 8073 << format("0x%" PRIx32, CommonEncodingsStart) << '\n'; 8074 uint32_t NumCommonEncodings = readNext<uint32_t>(Contents, Pos); 8075 outs() << " Number of common encodings in array: " 8076 << format("0x%" PRIx32, NumCommonEncodings) << '\n'; 8077 8078 uint32_t PersonalitiesStart = readNext<uint32_t>(Contents, Pos); 8079 outs() << " Personality function array section offset: " 8080 << format("0x%" PRIx32, PersonalitiesStart) << '\n'; 8081 uint32_t NumPersonalities = readNext<uint32_t>(Contents, Pos); 8082 outs() << " Number of personality functions in array: " 8083 << format("0x%" PRIx32, NumPersonalities) << '\n'; 8084 8085 uint32_t IndicesStart = readNext<uint32_t>(Contents, Pos); 8086 outs() << " Index array section offset: " 8087 << format("0x%" PRIx32, IndicesStart) << '\n'; 8088 uint32_t NumIndices = readNext<uint32_t>(Contents, Pos); 8089 outs() << " Number of indices in array: " 8090 << format("0x%" PRIx32, NumIndices) << '\n'; 8091 8092 //===---------------------------------- 8093 // A shared list of common encodings 8094 //===---------------------------------- 8095 8096 // These occupy indices in the range [0, N] whenever an encoding is referenced 8097 // from a compressed 2nd level index table. In practice the linker only 8098 // creates ~128 of these, so that indices are available to embed encodings in 8099 // the 2nd level index. 8100 8101 SmallVector<uint32_t, 64> CommonEncodings; 8102 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n"; 8103 Pos = CommonEncodingsStart; 8104 for (unsigned i = 0; i < NumCommonEncodings; ++i) { 8105 uint32_t Encoding = readNext<uint32_t>(Contents, Pos); 8106 CommonEncodings.push_back(Encoding); 8107 8108 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding) 8109 << '\n'; 8110 } 8111 8112 //===---------------------------------- 8113 // Personality functions used in this executable 8114 //===---------------------------------- 8115 8116 // There should be only a handful of these (one per source language, 8117 // roughly). Particularly since they only get 2 bits in the compact encoding. 8118 8119 outs() << " Personality functions: (count = " << NumPersonalities << ")\n"; 8120 Pos = PersonalitiesStart; 8121 for (unsigned i = 0; i < NumPersonalities; ++i) { 8122 uint32_t PersonalityFn = readNext<uint32_t>(Contents, Pos); 8123 outs() << " personality[" << i + 1 8124 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n'; 8125 } 8126 8127 //===---------------------------------- 8128 // The level 1 index entries 8129 //===---------------------------------- 8130 8131 // These specify an approximate place to start searching for the more detailed 8132 // information, sorted by PC. 8133 8134 struct IndexEntry { 8135 uint32_t FunctionOffset; 8136 uint32_t SecondLevelPageStart; 8137 uint32_t LSDAStart; 8138 }; 8139 8140 SmallVector<IndexEntry, 4> IndexEntries; 8141 8142 outs() << " Top level indices: (count = " << NumIndices << ")\n"; 8143 Pos = IndicesStart; 8144 for (unsigned i = 0; i < NumIndices; ++i) { 8145 IndexEntry Entry; 8146 8147 Entry.FunctionOffset = readNext<uint32_t>(Contents, Pos); 8148 Entry.SecondLevelPageStart = readNext<uint32_t>(Contents, Pos); 8149 Entry.LSDAStart = readNext<uint32_t>(Contents, Pos); 8150 IndexEntries.push_back(Entry); 8151 8152 outs() << " [" << i << "]: " 8153 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset) 8154 << ", " 8155 << "2nd level page offset=" 8156 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", " 8157 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n'; 8158 } 8159 8160 //===---------------------------------- 8161 // Next come the LSDA tables 8162 //===---------------------------------- 8163 8164 // The LSDA layout is rather implicit: it's a contiguous array of entries from 8165 // the first top-level index's LSDAOffset to the last (sentinel). 8166 8167 outs() << " LSDA descriptors:\n"; 8168 Pos = IndexEntries[0].LSDAStart; 8169 const uint32_t LSDASize = 2 * sizeof(uint32_t); 8170 int NumLSDAs = 8171 (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) / LSDASize; 8172 8173 for (int i = 0; i < NumLSDAs; ++i) { 8174 uint32_t FunctionOffset = readNext<uint32_t>(Contents, Pos); 8175 uint32_t LSDAOffset = readNext<uint32_t>(Contents, Pos); 8176 outs() << " [" << i << "]: " 8177 << "function offset=" << format("0x%08" PRIx32, FunctionOffset) 8178 << ", " 8179 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n'; 8180 } 8181 8182 //===---------------------------------- 8183 // Finally, the 2nd level indices 8184 //===---------------------------------- 8185 8186 // Generally these are 4K in size, and have 2 possible forms: 8187 // + Regular stores up to 511 entries with disparate encodings 8188 // + Compressed stores up to 1021 entries if few enough compact encoding 8189 // values are used. 8190 outs() << " Second level indices:\n"; 8191 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) { 8192 // The final sentinel top-level index has no associated 2nd level page 8193 if (IndexEntries[i].SecondLevelPageStart == 0) 8194 break; 8195 8196 outs() << " Second level index[" << i << "]: " 8197 << "offset in section=" 8198 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart) 8199 << ", " 8200 << "base function offset=" 8201 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n'; 8202 8203 Pos = IndexEntries[i].SecondLevelPageStart; 8204 if (Pos + sizeof(uint32_t) > Contents.size()) { 8205 outs() << "warning: invalid offset for second level page: " << Pos << '\n'; 8206 continue; 8207 } 8208 8209 uint32_t Kind = 8210 *reinterpret_cast<const support::ulittle32_t *>(Contents.data() + Pos); 8211 if (Kind == 2) 8212 printRegularSecondLevelUnwindPage(Contents.substr(Pos, 4096)); 8213 else if (Kind == 3) 8214 printCompressedSecondLevelUnwindPage(Contents.substr(Pos, 4096), 8215 IndexEntries[i].FunctionOffset, 8216 CommonEncodings); 8217 else 8218 outs() << " Skipping 2nd level page with unknown kind " << Kind 8219 << '\n'; 8220 } 8221 } 8222 8223 void objdump::printMachOUnwindInfo(const MachOObjectFile *Obj) { 8224 std::map<uint64_t, SymbolRef> Symbols; 8225 for (const SymbolRef &SymRef : Obj->symbols()) { 8226 // Discard any undefined or absolute symbols. They're not going to take part 8227 // in the convenience lookup for unwind info and just take up resources. 8228 auto SectOrErr = SymRef.getSection(); 8229 if (!SectOrErr) { 8230 // TODO: Actually report errors helpfully. 8231 consumeError(SectOrErr.takeError()); 8232 continue; 8233 } 8234 section_iterator Section = *SectOrErr; 8235 if (Section == Obj->section_end()) 8236 continue; 8237 8238 uint64_t Addr = cantFail(SymRef.getValue()); 8239 Symbols.insert(std::make_pair(Addr, SymRef)); 8240 } 8241 8242 for (const SectionRef &Section : Obj->sections()) { 8243 StringRef SectName; 8244 if (Expected<StringRef> NameOrErr = Section.getName()) 8245 SectName = *NameOrErr; 8246 else 8247 consumeError(NameOrErr.takeError()); 8248 8249 if (SectName == "__compact_unwind") 8250 printMachOCompactUnwindSection(Obj, Symbols, Section); 8251 else if (SectName == "__unwind_info") 8252 printMachOUnwindInfoSection(Obj, Symbols, Section); 8253 } 8254 } 8255 8256 static void PrintMachHeader(uint32_t magic, uint32_t cputype, 8257 uint32_t cpusubtype, uint32_t filetype, 8258 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags, 8259 bool verbose) { 8260 outs() << "Mach header\n"; 8261 outs() << " magic cputype cpusubtype caps filetype ncmds " 8262 "sizeofcmds flags\n"; 8263 if (verbose) { 8264 if (magic == MachO::MH_MAGIC) 8265 outs() << " MH_MAGIC"; 8266 else if (magic == MachO::MH_MAGIC_64) 8267 outs() << "MH_MAGIC_64"; 8268 else 8269 outs() << format(" 0x%08" PRIx32, magic); 8270 switch (cputype) { 8271 case MachO::CPU_TYPE_I386: 8272 outs() << " I386"; 8273 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 8274 case MachO::CPU_SUBTYPE_I386_ALL: 8275 outs() << " ALL"; 8276 break; 8277 default: 8278 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8279 break; 8280 } 8281 break; 8282 case MachO::CPU_TYPE_X86_64: 8283 outs() << " X86_64"; 8284 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 8285 case MachO::CPU_SUBTYPE_X86_64_ALL: 8286 outs() << " ALL"; 8287 break; 8288 case MachO::CPU_SUBTYPE_X86_64_H: 8289 outs() << " Haswell"; 8290 break; 8291 default: 8292 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8293 break; 8294 } 8295 break; 8296 case MachO::CPU_TYPE_ARM: 8297 outs() << " ARM"; 8298 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 8299 case MachO::CPU_SUBTYPE_ARM_ALL: 8300 outs() << " ALL"; 8301 break; 8302 case MachO::CPU_SUBTYPE_ARM_V4T: 8303 outs() << " V4T"; 8304 break; 8305 case MachO::CPU_SUBTYPE_ARM_V5TEJ: 8306 outs() << " V5TEJ"; 8307 break; 8308 case MachO::CPU_SUBTYPE_ARM_XSCALE: 8309 outs() << " XSCALE"; 8310 break; 8311 case MachO::CPU_SUBTYPE_ARM_V6: 8312 outs() << " V6"; 8313 break; 8314 case MachO::CPU_SUBTYPE_ARM_V6M: 8315 outs() << " V6M"; 8316 break; 8317 case MachO::CPU_SUBTYPE_ARM_V7: 8318 outs() << " V7"; 8319 break; 8320 case MachO::CPU_SUBTYPE_ARM_V7EM: 8321 outs() << " V7EM"; 8322 break; 8323 case MachO::CPU_SUBTYPE_ARM_V7K: 8324 outs() << " V7K"; 8325 break; 8326 case MachO::CPU_SUBTYPE_ARM_V7M: 8327 outs() << " V7M"; 8328 break; 8329 case MachO::CPU_SUBTYPE_ARM_V7S: 8330 outs() << " V7S"; 8331 break; 8332 default: 8333 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8334 break; 8335 } 8336 break; 8337 case MachO::CPU_TYPE_ARM64: 8338 outs() << " ARM64"; 8339 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 8340 case MachO::CPU_SUBTYPE_ARM64_ALL: 8341 outs() << " ALL"; 8342 break; 8343 case MachO::CPU_SUBTYPE_ARM64_V8: 8344 outs() << " V8"; 8345 break; 8346 case MachO::CPU_SUBTYPE_ARM64E: 8347 outs() << " E"; 8348 break; 8349 default: 8350 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8351 break; 8352 } 8353 break; 8354 case MachO::CPU_TYPE_ARM64_32: 8355 outs() << " ARM64_32"; 8356 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 8357 case MachO::CPU_SUBTYPE_ARM64_32_V8: 8358 outs() << " V8"; 8359 break; 8360 default: 8361 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8362 break; 8363 } 8364 break; 8365 case MachO::CPU_TYPE_POWERPC: 8366 outs() << " PPC"; 8367 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 8368 case MachO::CPU_SUBTYPE_POWERPC_ALL: 8369 outs() << " ALL"; 8370 break; 8371 default: 8372 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8373 break; 8374 } 8375 break; 8376 case MachO::CPU_TYPE_POWERPC64: 8377 outs() << " PPC64"; 8378 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 8379 case MachO::CPU_SUBTYPE_POWERPC_ALL: 8380 outs() << " ALL"; 8381 break; 8382 default: 8383 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8384 break; 8385 } 8386 break; 8387 default: 8388 outs() << format(" %7d", cputype); 8389 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8390 break; 8391 } 8392 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) { 8393 outs() << " LIB64"; 8394 } else { 8395 outs() << format(" 0x%02" PRIx32, 8396 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24); 8397 } 8398 switch (filetype) { 8399 case MachO::MH_OBJECT: 8400 outs() << " OBJECT"; 8401 break; 8402 case MachO::MH_EXECUTE: 8403 outs() << " EXECUTE"; 8404 break; 8405 case MachO::MH_FVMLIB: 8406 outs() << " FVMLIB"; 8407 break; 8408 case MachO::MH_CORE: 8409 outs() << " CORE"; 8410 break; 8411 case MachO::MH_PRELOAD: 8412 outs() << " PRELOAD"; 8413 break; 8414 case MachO::MH_DYLIB: 8415 outs() << " DYLIB"; 8416 break; 8417 case MachO::MH_DYLIB_STUB: 8418 outs() << " DYLIB_STUB"; 8419 break; 8420 case MachO::MH_DYLINKER: 8421 outs() << " DYLINKER"; 8422 break; 8423 case MachO::MH_BUNDLE: 8424 outs() << " BUNDLE"; 8425 break; 8426 case MachO::MH_DSYM: 8427 outs() << " DSYM"; 8428 break; 8429 case MachO::MH_KEXT_BUNDLE: 8430 outs() << " KEXTBUNDLE"; 8431 break; 8432 default: 8433 outs() << format(" %10u", filetype); 8434 break; 8435 } 8436 outs() << format(" %5u", ncmds); 8437 outs() << format(" %10u", sizeofcmds); 8438 uint32_t f = flags; 8439 if (f & MachO::MH_NOUNDEFS) { 8440 outs() << " NOUNDEFS"; 8441 f &= ~MachO::MH_NOUNDEFS; 8442 } 8443 if (f & MachO::MH_INCRLINK) { 8444 outs() << " INCRLINK"; 8445 f &= ~MachO::MH_INCRLINK; 8446 } 8447 if (f & MachO::MH_DYLDLINK) { 8448 outs() << " DYLDLINK"; 8449 f &= ~MachO::MH_DYLDLINK; 8450 } 8451 if (f & MachO::MH_BINDATLOAD) { 8452 outs() << " BINDATLOAD"; 8453 f &= ~MachO::MH_BINDATLOAD; 8454 } 8455 if (f & MachO::MH_PREBOUND) { 8456 outs() << " PREBOUND"; 8457 f &= ~MachO::MH_PREBOUND; 8458 } 8459 if (f & MachO::MH_SPLIT_SEGS) { 8460 outs() << " SPLIT_SEGS"; 8461 f &= ~MachO::MH_SPLIT_SEGS; 8462 } 8463 if (f & MachO::MH_LAZY_INIT) { 8464 outs() << " LAZY_INIT"; 8465 f &= ~MachO::MH_LAZY_INIT; 8466 } 8467 if (f & MachO::MH_TWOLEVEL) { 8468 outs() << " TWOLEVEL"; 8469 f &= ~MachO::MH_TWOLEVEL; 8470 } 8471 if (f & MachO::MH_FORCE_FLAT) { 8472 outs() << " FORCE_FLAT"; 8473 f &= ~MachO::MH_FORCE_FLAT; 8474 } 8475 if (f & MachO::MH_NOMULTIDEFS) { 8476 outs() << " NOMULTIDEFS"; 8477 f &= ~MachO::MH_NOMULTIDEFS; 8478 } 8479 if (f & MachO::MH_NOFIXPREBINDING) { 8480 outs() << " NOFIXPREBINDING"; 8481 f &= ~MachO::MH_NOFIXPREBINDING; 8482 } 8483 if (f & MachO::MH_PREBINDABLE) { 8484 outs() << " PREBINDABLE"; 8485 f &= ~MachO::MH_PREBINDABLE; 8486 } 8487 if (f & MachO::MH_ALLMODSBOUND) { 8488 outs() << " ALLMODSBOUND"; 8489 f &= ~MachO::MH_ALLMODSBOUND; 8490 } 8491 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) { 8492 outs() << " SUBSECTIONS_VIA_SYMBOLS"; 8493 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS; 8494 } 8495 if (f & MachO::MH_CANONICAL) { 8496 outs() << " CANONICAL"; 8497 f &= ~MachO::MH_CANONICAL; 8498 } 8499 if (f & MachO::MH_WEAK_DEFINES) { 8500 outs() << " WEAK_DEFINES"; 8501 f &= ~MachO::MH_WEAK_DEFINES; 8502 } 8503 if (f & MachO::MH_BINDS_TO_WEAK) { 8504 outs() << " BINDS_TO_WEAK"; 8505 f &= ~MachO::MH_BINDS_TO_WEAK; 8506 } 8507 if (f & MachO::MH_ALLOW_STACK_EXECUTION) { 8508 outs() << " ALLOW_STACK_EXECUTION"; 8509 f &= ~MachO::MH_ALLOW_STACK_EXECUTION; 8510 } 8511 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) { 8512 outs() << " DEAD_STRIPPABLE_DYLIB"; 8513 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB; 8514 } 8515 if (f & MachO::MH_PIE) { 8516 outs() << " PIE"; 8517 f &= ~MachO::MH_PIE; 8518 } 8519 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) { 8520 outs() << " NO_REEXPORTED_DYLIBS"; 8521 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS; 8522 } 8523 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) { 8524 outs() << " MH_HAS_TLV_DESCRIPTORS"; 8525 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS; 8526 } 8527 if (f & MachO::MH_NO_HEAP_EXECUTION) { 8528 outs() << " MH_NO_HEAP_EXECUTION"; 8529 f &= ~MachO::MH_NO_HEAP_EXECUTION; 8530 } 8531 if (f & MachO::MH_APP_EXTENSION_SAFE) { 8532 outs() << " APP_EXTENSION_SAFE"; 8533 f &= ~MachO::MH_APP_EXTENSION_SAFE; 8534 } 8535 if (f & MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO) { 8536 outs() << " NLIST_OUTOFSYNC_WITH_DYLDINFO"; 8537 f &= ~MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO; 8538 } 8539 if (f != 0 || flags == 0) 8540 outs() << format(" 0x%08" PRIx32, f); 8541 } else { 8542 outs() << format(" 0x%08" PRIx32, magic); 8543 outs() << format(" %7d", cputype); 8544 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 8545 outs() << format(" 0x%02" PRIx32, 8546 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24); 8547 outs() << format(" %10u", filetype); 8548 outs() << format(" %5u", ncmds); 8549 outs() << format(" %10u", sizeofcmds); 8550 outs() << format(" 0x%08" PRIx32, flags); 8551 } 8552 outs() << "\n"; 8553 } 8554 8555 static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize, 8556 StringRef SegName, uint64_t vmaddr, 8557 uint64_t vmsize, uint64_t fileoff, 8558 uint64_t filesize, uint32_t maxprot, 8559 uint32_t initprot, uint32_t nsects, 8560 uint32_t flags, uint32_t object_size, 8561 bool verbose) { 8562 uint64_t expected_cmdsize; 8563 if (cmd == MachO::LC_SEGMENT) { 8564 outs() << " cmd LC_SEGMENT\n"; 8565 expected_cmdsize = nsects; 8566 expected_cmdsize *= sizeof(struct MachO::section); 8567 expected_cmdsize += sizeof(struct MachO::segment_command); 8568 } else { 8569 outs() << " cmd LC_SEGMENT_64\n"; 8570 expected_cmdsize = nsects; 8571 expected_cmdsize *= sizeof(struct MachO::section_64); 8572 expected_cmdsize += sizeof(struct MachO::segment_command_64); 8573 } 8574 outs() << " cmdsize " << cmdsize; 8575 if (cmdsize != expected_cmdsize) 8576 outs() << " Inconsistent size\n"; 8577 else 8578 outs() << "\n"; 8579 outs() << " segname " << SegName << "\n"; 8580 if (cmd == MachO::LC_SEGMENT_64) { 8581 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n"; 8582 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n"; 8583 } else { 8584 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n"; 8585 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n"; 8586 } 8587 outs() << " fileoff " << fileoff; 8588 if (fileoff > object_size) 8589 outs() << " (past end of file)\n"; 8590 else 8591 outs() << "\n"; 8592 outs() << " filesize " << filesize; 8593 if (fileoff + filesize > object_size) 8594 outs() << " (past end of file)\n"; 8595 else 8596 outs() << "\n"; 8597 if (verbose) { 8598 if ((maxprot & 8599 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | 8600 MachO::VM_PROT_EXECUTE)) != 0) 8601 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n"; 8602 else { 8603 outs() << " maxprot "; 8604 outs() << ((maxprot & MachO::VM_PROT_READ) ? "r" : "-"); 8605 outs() << ((maxprot & MachO::VM_PROT_WRITE) ? "w" : "-"); 8606 outs() << ((maxprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n"); 8607 } 8608 if ((initprot & 8609 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | 8610 MachO::VM_PROT_EXECUTE)) != 0) 8611 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n"; 8612 else { 8613 outs() << " initprot "; 8614 outs() << ((initprot & MachO::VM_PROT_READ) ? "r" : "-"); 8615 outs() << ((initprot & MachO::VM_PROT_WRITE) ? "w" : "-"); 8616 outs() << ((initprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n"); 8617 } 8618 } else { 8619 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n"; 8620 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n"; 8621 } 8622 outs() << " nsects " << nsects << "\n"; 8623 if (verbose) { 8624 outs() << " flags"; 8625 if (flags == 0) 8626 outs() << " (none)\n"; 8627 else { 8628 if (flags & MachO::SG_HIGHVM) { 8629 outs() << " HIGHVM"; 8630 flags &= ~MachO::SG_HIGHVM; 8631 } 8632 if (flags & MachO::SG_FVMLIB) { 8633 outs() << " FVMLIB"; 8634 flags &= ~MachO::SG_FVMLIB; 8635 } 8636 if (flags & MachO::SG_NORELOC) { 8637 outs() << " NORELOC"; 8638 flags &= ~MachO::SG_NORELOC; 8639 } 8640 if (flags & MachO::SG_PROTECTED_VERSION_1) { 8641 outs() << " PROTECTED_VERSION_1"; 8642 flags &= ~MachO::SG_PROTECTED_VERSION_1; 8643 } 8644 if (flags) 8645 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n"; 8646 else 8647 outs() << "\n"; 8648 } 8649 } else { 8650 outs() << " flags " << format("0x%" PRIx32, flags) << "\n"; 8651 } 8652 } 8653 8654 static void PrintSection(const char *sectname, const char *segname, 8655 uint64_t addr, uint64_t size, uint32_t offset, 8656 uint32_t align, uint32_t reloff, uint32_t nreloc, 8657 uint32_t flags, uint32_t reserved1, uint32_t reserved2, 8658 uint32_t cmd, const char *sg_segname, 8659 uint32_t filetype, uint32_t object_size, 8660 bool verbose) { 8661 outs() << "Section\n"; 8662 outs() << " sectname " << format("%.16s\n", sectname); 8663 outs() << " segname " << format("%.16s", segname); 8664 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0) 8665 outs() << " (does not match segment)\n"; 8666 else 8667 outs() << "\n"; 8668 if (cmd == MachO::LC_SEGMENT_64) { 8669 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n"; 8670 outs() << " size " << format("0x%016" PRIx64, size); 8671 } else { 8672 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n"; 8673 outs() << " size " << format("0x%08" PRIx64, size); 8674 } 8675 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size) 8676 outs() << " (past end of file)\n"; 8677 else 8678 outs() << "\n"; 8679 outs() << " offset " << offset; 8680 if (offset > object_size) 8681 outs() << " (past end of file)\n"; 8682 else 8683 outs() << "\n"; 8684 uint32_t align_shifted = 1 << align; 8685 outs() << " align 2^" << align << " (" << align_shifted << ")\n"; 8686 outs() << " reloff " << reloff; 8687 if (reloff > object_size) 8688 outs() << " (past end of file)\n"; 8689 else 8690 outs() << "\n"; 8691 outs() << " nreloc " << nreloc; 8692 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size) 8693 outs() << " (past end of file)\n"; 8694 else 8695 outs() << "\n"; 8696 uint32_t section_type = flags & MachO::SECTION_TYPE; 8697 if (verbose) { 8698 outs() << " type"; 8699 if (section_type == MachO::S_REGULAR) 8700 outs() << " S_REGULAR\n"; 8701 else if (section_type == MachO::S_ZEROFILL) 8702 outs() << " S_ZEROFILL\n"; 8703 else if (section_type == MachO::S_CSTRING_LITERALS) 8704 outs() << " S_CSTRING_LITERALS\n"; 8705 else if (section_type == MachO::S_4BYTE_LITERALS) 8706 outs() << " S_4BYTE_LITERALS\n"; 8707 else if (section_type == MachO::S_8BYTE_LITERALS) 8708 outs() << " S_8BYTE_LITERALS\n"; 8709 else if (section_type == MachO::S_16BYTE_LITERALS) 8710 outs() << " S_16BYTE_LITERALS\n"; 8711 else if (section_type == MachO::S_LITERAL_POINTERS) 8712 outs() << " S_LITERAL_POINTERS\n"; 8713 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS) 8714 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n"; 8715 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS) 8716 outs() << " S_LAZY_SYMBOL_POINTERS\n"; 8717 else if (section_type == MachO::S_SYMBOL_STUBS) 8718 outs() << " S_SYMBOL_STUBS\n"; 8719 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS) 8720 outs() << " S_MOD_INIT_FUNC_POINTERS\n"; 8721 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS) 8722 outs() << " S_MOD_TERM_FUNC_POINTERS\n"; 8723 else if (section_type == MachO::S_COALESCED) 8724 outs() << " S_COALESCED\n"; 8725 else if (section_type == MachO::S_INTERPOSING) 8726 outs() << " S_INTERPOSING\n"; 8727 else if (section_type == MachO::S_DTRACE_DOF) 8728 outs() << " S_DTRACE_DOF\n"; 8729 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS) 8730 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n"; 8731 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR) 8732 outs() << " S_THREAD_LOCAL_REGULAR\n"; 8733 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL) 8734 outs() << " S_THREAD_LOCAL_ZEROFILL\n"; 8735 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES) 8736 outs() << " S_THREAD_LOCAL_VARIABLES\n"; 8737 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS) 8738 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n"; 8739 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS) 8740 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n"; 8741 else 8742 outs() << format("0x%08" PRIx32, section_type) << "\n"; 8743 outs() << "attributes"; 8744 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES; 8745 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS) 8746 outs() << " PURE_INSTRUCTIONS"; 8747 if (section_attributes & MachO::S_ATTR_NO_TOC) 8748 outs() << " NO_TOC"; 8749 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS) 8750 outs() << " STRIP_STATIC_SYMS"; 8751 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP) 8752 outs() << " NO_DEAD_STRIP"; 8753 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT) 8754 outs() << " LIVE_SUPPORT"; 8755 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE) 8756 outs() << " SELF_MODIFYING_CODE"; 8757 if (section_attributes & MachO::S_ATTR_DEBUG) 8758 outs() << " DEBUG"; 8759 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS) 8760 outs() << " SOME_INSTRUCTIONS"; 8761 if (section_attributes & MachO::S_ATTR_EXT_RELOC) 8762 outs() << " EXT_RELOC"; 8763 if (section_attributes & MachO::S_ATTR_LOC_RELOC) 8764 outs() << " LOC_RELOC"; 8765 if (section_attributes == 0) 8766 outs() << " (none)"; 8767 outs() << "\n"; 8768 } else 8769 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n"; 8770 outs() << " reserved1 " << reserved1; 8771 if (section_type == MachO::S_SYMBOL_STUBS || 8772 section_type == MachO::S_LAZY_SYMBOL_POINTERS || 8773 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS || 8774 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS || 8775 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS) 8776 outs() << " (index into indirect symbol table)\n"; 8777 else 8778 outs() << "\n"; 8779 outs() << " reserved2 " << reserved2; 8780 if (section_type == MachO::S_SYMBOL_STUBS) 8781 outs() << " (size of stubs)\n"; 8782 else 8783 outs() << "\n"; 8784 } 8785 8786 static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit, 8787 uint32_t object_size) { 8788 outs() << " cmd LC_SYMTAB\n"; 8789 outs() << " cmdsize " << st.cmdsize; 8790 if (st.cmdsize != sizeof(struct MachO::symtab_command)) 8791 outs() << " Incorrect size\n"; 8792 else 8793 outs() << "\n"; 8794 outs() << " symoff " << st.symoff; 8795 if (st.symoff > object_size) 8796 outs() << " (past end of file)\n"; 8797 else 8798 outs() << "\n"; 8799 outs() << " nsyms " << st.nsyms; 8800 uint64_t big_size; 8801 if (Is64Bit) { 8802 big_size = st.nsyms; 8803 big_size *= sizeof(struct MachO::nlist_64); 8804 big_size += st.symoff; 8805 if (big_size > object_size) 8806 outs() << " (past end of file)\n"; 8807 else 8808 outs() << "\n"; 8809 } else { 8810 big_size = st.nsyms; 8811 big_size *= sizeof(struct MachO::nlist); 8812 big_size += st.symoff; 8813 if (big_size > object_size) 8814 outs() << " (past end of file)\n"; 8815 else 8816 outs() << "\n"; 8817 } 8818 outs() << " stroff " << st.stroff; 8819 if (st.stroff > object_size) 8820 outs() << " (past end of file)\n"; 8821 else 8822 outs() << "\n"; 8823 outs() << " strsize " << st.strsize; 8824 big_size = st.stroff; 8825 big_size += st.strsize; 8826 if (big_size > object_size) 8827 outs() << " (past end of file)\n"; 8828 else 8829 outs() << "\n"; 8830 } 8831 8832 static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst, 8833 uint32_t nsyms, uint32_t object_size, 8834 bool Is64Bit) { 8835 outs() << " cmd LC_DYSYMTAB\n"; 8836 outs() << " cmdsize " << dyst.cmdsize; 8837 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command)) 8838 outs() << " Incorrect size\n"; 8839 else 8840 outs() << "\n"; 8841 outs() << " ilocalsym " << dyst.ilocalsym; 8842 if (dyst.ilocalsym > nsyms) 8843 outs() << " (greater than the number of symbols)\n"; 8844 else 8845 outs() << "\n"; 8846 outs() << " nlocalsym " << dyst.nlocalsym; 8847 uint64_t big_size; 8848 big_size = dyst.ilocalsym; 8849 big_size += dyst.nlocalsym; 8850 if (big_size > nsyms) 8851 outs() << " (past the end of the symbol table)\n"; 8852 else 8853 outs() << "\n"; 8854 outs() << " iextdefsym " << dyst.iextdefsym; 8855 if (dyst.iextdefsym > nsyms) 8856 outs() << " (greater than the number of symbols)\n"; 8857 else 8858 outs() << "\n"; 8859 outs() << " nextdefsym " << dyst.nextdefsym; 8860 big_size = dyst.iextdefsym; 8861 big_size += dyst.nextdefsym; 8862 if (big_size > nsyms) 8863 outs() << " (past the end of the symbol table)\n"; 8864 else 8865 outs() << "\n"; 8866 outs() << " iundefsym " << dyst.iundefsym; 8867 if (dyst.iundefsym > nsyms) 8868 outs() << " (greater than the number of symbols)\n"; 8869 else 8870 outs() << "\n"; 8871 outs() << " nundefsym " << dyst.nundefsym; 8872 big_size = dyst.iundefsym; 8873 big_size += dyst.nundefsym; 8874 if (big_size > nsyms) 8875 outs() << " (past the end of the symbol table)\n"; 8876 else 8877 outs() << "\n"; 8878 outs() << " tocoff " << dyst.tocoff; 8879 if (dyst.tocoff > object_size) 8880 outs() << " (past end of file)\n"; 8881 else 8882 outs() << "\n"; 8883 outs() << " ntoc " << dyst.ntoc; 8884 big_size = dyst.ntoc; 8885 big_size *= sizeof(struct MachO::dylib_table_of_contents); 8886 big_size += dyst.tocoff; 8887 if (big_size > object_size) 8888 outs() << " (past end of file)\n"; 8889 else 8890 outs() << "\n"; 8891 outs() << " modtaboff " << dyst.modtaboff; 8892 if (dyst.modtaboff > object_size) 8893 outs() << " (past end of file)\n"; 8894 else 8895 outs() << "\n"; 8896 outs() << " nmodtab " << dyst.nmodtab; 8897 uint64_t modtabend; 8898 if (Is64Bit) { 8899 modtabend = dyst.nmodtab; 8900 modtabend *= sizeof(struct MachO::dylib_module_64); 8901 modtabend += dyst.modtaboff; 8902 } else { 8903 modtabend = dyst.nmodtab; 8904 modtabend *= sizeof(struct MachO::dylib_module); 8905 modtabend += dyst.modtaboff; 8906 } 8907 if (modtabend > object_size) 8908 outs() << " (past end of file)\n"; 8909 else 8910 outs() << "\n"; 8911 outs() << " extrefsymoff " << dyst.extrefsymoff; 8912 if (dyst.extrefsymoff > object_size) 8913 outs() << " (past end of file)\n"; 8914 else 8915 outs() << "\n"; 8916 outs() << " nextrefsyms " << dyst.nextrefsyms; 8917 big_size = dyst.nextrefsyms; 8918 big_size *= sizeof(struct MachO::dylib_reference); 8919 big_size += dyst.extrefsymoff; 8920 if (big_size > object_size) 8921 outs() << " (past end of file)\n"; 8922 else 8923 outs() << "\n"; 8924 outs() << " indirectsymoff " << dyst.indirectsymoff; 8925 if (dyst.indirectsymoff > object_size) 8926 outs() << " (past end of file)\n"; 8927 else 8928 outs() << "\n"; 8929 outs() << " nindirectsyms " << dyst.nindirectsyms; 8930 big_size = dyst.nindirectsyms; 8931 big_size *= sizeof(uint32_t); 8932 big_size += dyst.indirectsymoff; 8933 if (big_size > object_size) 8934 outs() << " (past end of file)\n"; 8935 else 8936 outs() << "\n"; 8937 outs() << " extreloff " << dyst.extreloff; 8938 if (dyst.extreloff > object_size) 8939 outs() << " (past end of file)\n"; 8940 else 8941 outs() << "\n"; 8942 outs() << " nextrel " << dyst.nextrel; 8943 big_size = dyst.nextrel; 8944 big_size *= sizeof(struct MachO::relocation_info); 8945 big_size += dyst.extreloff; 8946 if (big_size > object_size) 8947 outs() << " (past end of file)\n"; 8948 else 8949 outs() << "\n"; 8950 outs() << " locreloff " << dyst.locreloff; 8951 if (dyst.locreloff > object_size) 8952 outs() << " (past end of file)\n"; 8953 else 8954 outs() << "\n"; 8955 outs() << " nlocrel " << dyst.nlocrel; 8956 big_size = dyst.nlocrel; 8957 big_size *= sizeof(struct MachO::relocation_info); 8958 big_size += dyst.locreloff; 8959 if (big_size > object_size) 8960 outs() << " (past end of file)\n"; 8961 else 8962 outs() << "\n"; 8963 } 8964 8965 static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc, 8966 uint32_t object_size) { 8967 if (dc.cmd == MachO::LC_DYLD_INFO) 8968 outs() << " cmd LC_DYLD_INFO\n"; 8969 else 8970 outs() << " cmd LC_DYLD_INFO_ONLY\n"; 8971 outs() << " cmdsize " << dc.cmdsize; 8972 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command)) 8973 outs() << " Incorrect size\n"; 8974 else 8975 outs() << "\n"; 8976 outs() << " rebase_off " << dc.rebase_off; 8977 if (dc.rebase_off > object_size) 8978 outs() << " (past end of file)\n"; 8979 else 8980 outs() << "\n"; 8981 outs() << " rebase_size " << dc.rebase_size; 8982 uint64_t big_size; 8983 big_size = dc.rebase_off; 8984 big_size += dc.rebase_size; 8985 if (big_size > object_size) 8986 outs() << " (past end of file)\n"; 8987 else 8988 outs() << "\n"; 8989 outs() << " bind_off " << dc.bind_off; 8990 if (dc.bind_off > object_size) 8991 outs() << " (past end of file)\n"; 8992 else 8993 outs() << "\n"; 8994 outs() << " bind_size " << dc.bind_size; 8995 big_size = dc.bind_off; 8996 big_size += dc.bind_size; 8997 if (big_size > object_size) 8998 outs() << " (past end of file)\n"; 8999 else 9000 outs() << "\n"; 9001 outs() << " weak_bind_off " << dc.weak_bind_off; 9002 if (dc.weak_bind_off > object_size) 9003 outs() << " (past end of file)\n"; 9004 else 9005 outs() << "\n"; 9006 outs() << " weak_bind_size " << dc.weak_bind_size; 9007 big_size = dc.weak_bind_off; 9008 big_size += dc.weak_bind_size; 9009 if (big_size > object_size) 9010 outs() << " (past end of file)\n"; 9011 else 9012 outs() << "\n"; 9013 outs() << " lazy_bind_off " << dc.lazy_bind_off; 9014 if (dc.lazy_bind_off > object_size) 9015 outs() << " (past end of file)\n"; 9016 else 9017 outs() << "\n"; 9018 outs() << " lazy_bind_size " << dc.lazy_bind_size; 9019 big_size = dc.lazy_bind_off; 9020 big_size += dc.lazy_bind_size; 9021 if (big_size > object_size) 9022 outs() << " (past end of file)\n"; 9023 else 9024 outs() << "\n"; 9025 outs() << " export_off " << dc.export_off; 9026 if (dc.export_off > object_size) 9027 outs() << " (past end of file)\n"; 9028 else 9029 outs() << "\n"; 9030 outs() << " export_size " << dc.export_size; 9031 big_size = dc.export_off; 9032 big_size += dc.export_size; 9033 if (big_size > object_size) 9034 outs() << " (past end of file)\n"; 9035 else 9036 outs() << "\n"; 9037 } 9038 9039 static void PrintDyldLoadCommand(MachO::dylinker_command dyld, 9040 const char *Ptr) { 9041 if (dyld.cmd == MachO::LC_ID_DYLINKER) 9042 outs() << " cmd LC_ID_DYLINKER\n"; 9043 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER) 9044 outs() << " cmd LC_LOAD_DYLINKER\n"; 9045 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT) 9046 outs() << " cmd LC_DYLD_ENVIRONMENT\n"; 9047 else 9048 outs() << " cmd ?(" << dyld.cmd << ")\n"; 9049 outs() << " cmdsize " << dyld.cmdsize; 9050 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command)) 9051 outs() << " Incorrect size\n"; 9052 else 9053 outs() << "\n"; 9054 if (dyld.name >= dyld.cmdsize) 9055 outs() << " name ?(bad offset " << dyld.name << ")\n"; 9056 else { 9057 const char *P = (const char *)(Ptr) + dyld.name; 9058 outs() << " name " << P << " (offset " << dyld.name << ")\n"; 9059 } 9060 } 9061 9062 static void PrintUuidLoadCommand(MachO::uuid_command uuid) { 9063 outs() << " cmd LC_UUID\n"; 9064 outs() << " cmdsize " << uuid.cmdsize; 9065 if (uuid.cmdsize != sizeof(struct MachO::uuid_command)) 9066 outs() << " Incorrect size\n"; 9067 else 9068 outs() << "\n"; 9069 outs() << " uuid "; 9070 for (int i = 0; i < 16; ++i) { 9071 outs() << format("%02" PRIX32, uuid.uuid[i]); 9072 if (i == 3 || i == 5 || i == 7 || i == 9) 9073 outs() << "-"; 9074 } 9075 outs() << "\n"; 9076 } 9077 9078 static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) { 9079 outs() << " cmd LC_RPATH\n"; 9080 outs() << " cmdsize " << rpath.cmdsize; 9081 if (rpath.cmdsize < sizeof(struct MachO::rpath_command)) 9082 outs() << " Incorrect size\n"; 9083 else 9084 outs() << "\n"; 9085 if (rpath.path >= rpath.cmdsize) 9086 outs() << " path ?(bad offset " << rpath.path << ")\n"; 9087 else { 9088 const char *P = (const char *)(Ptr) + rpath.path; 9089 outs() << " path " << P << " (offset " << rpath.path << ")\n"; 9090 } 9091 } 9092 9093 static void PrintVersionMinLoadCommand(MachO::version_min_command vd) { 9094 StringRef LoadCmdName; 9095 switch (vd.cmd) { 9096 case MachO::LC_VERSION_MIN_MACOSX: 9097 LoadCmdName = "LC_VERSION_MIN_MACOSX"; 9098 break; 9099 case MachO::LC_VERSION_MIN_IPHONEOS: 9100 LoadCmdName = "LC_VERSION_MIN_IPHONEOS"; 9101 break; 9102 case MachO::LC_VERSION_MIN_TVOS: 9103 LoadCmdName = "LC_VERSION_MIN_TVOS"; 9104 break; 9105 case MachO::LC_VERSION_MIN_WATCHOS: 9106 LoadCmdName = "LC_VERSION_MIN_WATCHOS"; 9107 break; 9108 default: 9109 llvm_unreachable("Unknown version min load command"); 9110 } 9111 9112 outs() << " cmd " << LoadCmdName << '\n'; 9113 outs() << " cmdsize " << vd.cmdsize; 9114 if (vd.cmdsize != sizeof(struct MachO::version_min_command)) 9115 outs() << " Incorrect size\n"; 9116 else 9117 outs() << "\n"; 9118 outs() << " version " 9119 << MachOObjectFile::getVersionMinMajor(vd, false) << "." 9120 << MachOObjectFile::getVersionMinMinor(vd, false); 9121 uint32_t Update = MachOObjectFile::getVersionMinUpdate(vd, false); 9122 if (Update != 0) 9123 outs() << "." << Update; 9124 outs() << "\n"; 9125 if (vd.sdk == 0) 9126 outs() << " sdk n/a"; 9127 else { 9128 outs() << " sdk " 9129 << MachOObjectFile::getVersionMinMajor(vd, true) << "." 9130 << MachOObjectFile::getVersionMinMinor(vd, true); 9131 } 9132 Update = MachOObjectFile::getVersionMinUpdate(vd, true); 9133 if (Update != 0) 9134 outs() << "." << Update; 9135 outs() << "\n"; 9136 } 9137 9138 static void PrintNoteLoadCommand(MachO::note_command Nt) { 9139 outs() << " cmd LC_NOTE\n"; 9140 outs() << " cmdsize " << Nt.cmdsize; 9141 if (Nt.cmdsize != sizeof(struct MachO::note_command)) 9142 outs() << " Incorrect size\n"; 9143 else 9144 outs() << "\n"; 9145 const char *d = Nt.data_owner; 9146 outs() << "data_owner " << format("%.16s\n", d); 9147 outs() << " offset " << Nt.offset << "\n"; 9148 outs() << " size " << Nt.size << "\n"; 9149 } 9150 9151 static void PrintBuildToolVersion(MachO::build_tool_version bv) { 9152 outs() << " tool " << MachOObjectFile::getBuildTool(bv.tool) << "\n"; 9153 outs() << " version " << MachOObjectFile::getVersionString(bv.version) 9154 << "\n"; 9155 } 9156 9157 static void PrintBuildVersionLoadCommand(const MachOObjectFile *obj, 9158 MachO::build_version_command bd) { 9159 outs() << " cmd LC_BUILD_VERSION\n"; 9160 outs() << " cmdsize " << bd.cmdsize; 9161 if (bd.cmdsize != 9162 sizeof(struct MachO::build_version_command) + 9163 bd.ntools * sizeof(struct MachO::build_tool_version)) 9164 outs() << " Incorrect size\n"; 9165 else 9166 outs() << "\n"; 9167 outs() << " platform " << MachOObjectFile::getBuildPlatform(bd.platform) 9168 << "\n"; 9169 if (bd.sdk) 9170 outs() << " sdk " << MachOObjectFile::getVersionString(bd.sdk) 9171 << "\n"; 9172 else 9173 outs() << " sdk n/a\n"; 9174 outs() << " minos " << MachOObjectFile::getVersionString(bd.minos) 9175 << "\n"; 9176 outs() << " ntools " << bd.ntools << "\n"; 9177 for (unsigned i = 0; i < bd.ntools; ++i) { 9178 MachO::build_tool_version bv = obj->getBuildToolVersion(i); 9179 PrintBuildToolVersion(bv); 9180 } 9181 } 9182 9183 static void PrintSourceVersionCommand(MachO::source_version_command sd) { 9184 outs() << " cmd LC_SOURCE_VERSION\n"; 9185 outs() << " cmdsize " << sd.cmdsize; 9186 if (sd.cmdsize != sizeof(struct MachO::source_version_command)) 9187 outs() << " Incorrect size\n"; 9188 else 9189 outs() << "\n"; 9190 uint64_t a = (sd.version >> 40) & 0xffffff; 9191 uint64_t b = (sd.version >> 30) & 0x3ff; 9192 uint64_t c = (sd.version >> 20) & 0x3ff; 9193 uint64_t d = (sd.version >> 10) & 0x3ff; 9194 uint64_t e = sd.version & 0x3ff; 9195 outs() << " version " << a << "." << b; 9196 if (e != 0) 9197 outs() << "." << c << "." << d << "." << e; 9198 else if (d != 0) 9199 outs() << "." << c << "." << d; 9200 else if (c != 0) 9201 outs() << "." << c; 9202 outs() << "\n"; 9203 } 9204 9205 static void PrintEntryPointCommand(MachO::entry_point_command ep) { 9206 outs() << " cmd LC_MAIN\n"; 9207 outs() << " cmdsize " << ep.cmdsize; 9208 if (ep.cmdsize != sizeof(struct MachO::entry_point_command)) 9209 outs() << " Incorrect size\n"; 9210 else 9211 outs() << "\n"; 9212 outs() << " entryoff " << ep.entryoff << "\n"; 9213 outs() << " stacksize " << ep.stacksize << "\n"; 9214 } 9215 9216 static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec, 9217 uint32_t object_size) { 9218 outs() << " cmd LC_ENCRYPTION_INFO\n"; 9219 outs() << " cmdsize " << ec.cmdsize; 9220 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command)) 9221 outs() << " Incorrect size\n"; 9222 else 9223 outs() << "\n"; 9224 outs() << " cryptoff " << ec.cryptoff; 9225 if (ec.cryptoff > object_size) 9226 outs() << " (past end of file)\n"; 9227 else 9228 outs() << "\n"; 9229 outs() << " cryptsize " << ec.cryptsize; 9230 if (ec.cryptsize > object_size) 9231 outs() << " (past end of file)\n"; 9232 else 9233 outs() << "\n"; 9234 outs() << " cryptid " << ec.cryptid << "\n"; 9235 } 9236 9237 static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec, 9238 uint32_t object_size) { 9239 outs() << " cmd LC_ENCRYPTION_INFO_64\n"; 9240 outs() << " cmdsize " << ec.cmdsize; 9241 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64)) 9242 outs() << " Incorrect size\n"; 9243 else 9244 outs() << "\n"; 9245 outs() << " cryptoff " << ec.cryptoff; 9246 if (ec.cryptoff > object_size) 9247 outs() << " (past end of file)\n"; 9248 else 9249 outs() << "\n"; 9250 outs() << " cryptsize " << ec.cryptsize; 9251 if (ec.cryptsize > object_size) 9252 outs() << " (past end of file)\n"; 9253 else 9254 outs() << "\n"; 9255 outs() << " cryptid " << ec.cryptid << "\n"; 9256 outs() << " pad " << ec.pad << "\n"; 9257 } 9258 9259 static void PrintLinkerOptionCommand(MachO::linker_option_command lo, 9260 const char *Ptr) { 9261 outs() << " cmd LC_LINKER_OPTION\n"; 9262 outs() << " cmdsize " << lo.cmdsize; 9263 if (lo.cmdsize < sizeof(struct MachO::linker_option_command)) 9264 outs() << " Incorrect size\n"; 9265 else 9266 outs() << "\n"; 9267 outs() << " count " << lo.count << "\n"; 9268 const char *string = Ptr + sizeof(struct MachO::linker_option_command); 9269 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command); 9270 uint32_t i = 0; 9271 while (left > 0) { 9272 while (*string == '\0' && left > 0) { 9273 string++; 9274 left--; 9275 } 9276 if (left > 0) { 9277 i++; 9278 outs() << " string #" << i << " " << format("%.*s\n", left, string); 9279 uint32_t NullPos = StringRef(string, left).find('\0'); 9280 uint32_t len = std::min(NullPos, left) + 1; 9281 string += len; 9282 left -= len; 9283 } 9284 } 9285 if (lo.count != i) 9286 outs() << " count " << lo.count << " does not match number of strings " 9287 << i << "\n"; 9288 } 9289 9290 static void PrintSubFrameworkCommand(MachO::sub_framework_command sub, 9291 const char *Ptr) { 9292 outs() << " cmd LC_SUB_FRAMEWORK\n"; 9293 outs() << " cmdsize " << sub.cmdsize; 9294 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command)) 9295 outs() << " Incorrect size\n"; 9296 else 9297 outs() << "\n"; 9298 if (sub.umbrella < sub.cmdsize) { 9299 const char *P = Ptr + sub.umbrella; 9300 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n"; 9301 } else { 9302 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n"; 9303 } 9304 } 9305 9306 static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub, 9307 const char *Ptr) { 9308 outs() << " cmd LC_SUB_UMBRELLA\n"; 9309 outs() << " cmdsize " << sub.cmdsize; 9310 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command)) 9311 outs() << " Incorrect size\n"; 9312 else 9313 outs() << "\n"; 9314 if (sub.sub_umbrella < sub.cmdsize) { 9315 const char *P = Ptr + sub.sub_umbrella; 9316 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n"; 9317 } else { 9318 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n"; 9319 } 9320 } 9321 9322 static void PrintSubLibraryCommand(MachO::sub_library_command sub, 9323 const char *Ptr) { 9324 outs() << " cmd LC_SUB_LIBRARY\n"; 9325 outs() << " cmdsize " << sub.cmdsize; 9326 if (sub.cmdsize < sizeof(struct MachO::sub_library_command)) 9327 outs() << " Incorrect size\n"; 9328 else 9329 outs() << "\n"; 9330 if (sub.sub_library < sub.cmdsize) { 9331 const char *P = Ptr + sub.sub_library; 9332 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n"; 9333 } else { 9334 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n"; 9335 } 9336 } 9337 9338 static void PrintSubClientCommand(MachO::sub_client_command sub, 9339 const char *Ptr) { 9340 outs() << " cmd LC_SUB_CLIENT\n"; 9341 outs() << " cmdsize " << sub.cmdsize; 9342 if (sub.cmdsize < sizeof(struct MachO::sub_client_command)) 9343 outs() << " Incorrect size\n"; 9344 else 9345 outs() << "\n"; 9346 if (sub.client < sub.cmdsize) { 9347 const char *P = Ptr + sub.client; 9348 outs() << " client " << P << " (offset " << sub.client << ")\n"; 9349 } else { 9350 outs() << " client ?(bad offset " << sub.client << ")\n"; 9351 } 9352 } 9353 9354 static void PrintRoutinesCommand(MachO::routines_command r) { 9355 outs() << " cmd LC_ROUTINES\n"; 9356 outs() << " cmdsize " << r.cmdsize; 9357 if (r.cmdsize != sizeof(struct MachO::routines_command)) 9358 outs() << " Incorrect size\n"; 9359 else 9360 outs() << "\n"; 9361 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n"; 9362 outs() << " init_module " << r.init_module << "\n"; 9363 outs() << " reserved1 " << r.reserved1 << "\n"; 9364 outs() << " reserved2 " << r.reserved2 << "\n"; 9365 outs() << " reserved3 " << r.reserved3 << "\n"; 9366 outs() << " reserved4 " << r.reserved4 << "\n"; 9367 outs() << " reserved5 " << r.reserved5 << "\n"; 9368 outs() << " reserved6 " << r.reserved6 << "\n"; 9369 } 9370 9371 static void PrintRoutinesCommand64(MachO::routines_command_64 r) { 9372 outs() << " cmd LC_ROUTINES_64\n"; 9373 outs() << " cmdsize " << r.cmdsize; 9374 if (r.cmdsize != sizeof(struct MachO::routines_command_64)) 9375 outs() << " Incorrect size\n"; 9376 else 9377 outs() << "\n"; 9378 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n"; 9379 outs() << " init_module " << r.init_module << "\n"; 9380 outs() << " reserved1 " << r.reserved1 << "\n"; 9381 outs() << " reserved2 " << r.reserved2 << "\n"; 9382 outs() << " reserved3 " << r.reserved3 << "\n"; 9383 outs() << " reserved4 " << r.reserved4 << "\n"; 9384 outs() << " reserved5 " << r.reserved5 << "\n"; 9385 outs() << " reserved6 " << r.reserved6 << "\n"; 9386 } 9387 9388 static void Print_x86_thread_state32_t(MachO::x86_thread_state32_t &cpu32) { 9389 outs() << "\t eax " << format("0x%08" PRIx32, cpu32.eax); 9390 outs() << " ebx " << format("0x%08" PRIx32, cpu32.ebx); 9391 outs() << " ecx " << format("0x%08" PRIx32, cpu32.ecx); 9392 outs() << " edx " << format("0x%08" PRIx32, cpu32.edx) << "\n"; 9393 outs() << "\t edi " << format("0x%08" PRIx32, cpu32.edi); 9394 outs() << " esi " << format("0x%08" PRIx32, cpu32.esi); 9395 outs() << " ebp " << format("0x%08" PRIx32, cpu32.ebp); 9396 outs() << " esp " << format("0x%08" PRIx32, cpu32.esp) << "\n"; 9397 outs() << "\t ss " << format("0x%08" PRIx32, cpu32.ss); 9398 outs() << " eflags " << format("0x%08" PRIx32, cpu32.eflags); 9399 outs() << " eip " << format("0x%08" PRIx32, cpu32.eip); 9400 outs() << " cs " << format("0x%08" PRIx32, cpu32.cs) << "\n"; 9401 outs() << "\t ds " << format("0x%08" PRIx32, cpu32.ds); 9402 outs() << " es " << format("0x%08" PRIx32, cpu32.es); 9403 outs() << " fs " << format("0x%08" PRIx32, cpu32.fs); 9404 outs() << " gs " << format("0x%08" PRIx32, cpu32.gs) << "\n"; 9405 } 9406 9407 static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) { 9408 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax); 9409 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx); 9410 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n"; 9411 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx); 9412 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi); 9413 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n"; 9414 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp); 9415 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp); 9416 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n"; 9417 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9); 9418 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10); 9419 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n"; 9420 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12); 9421 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13); 9422 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n"; 9423 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15); 9424 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n"; 9425 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags); 9426 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs); 9427 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n"; 9428 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n"; 9429 } 9430 9431 static void Print_mmst_reg(MachO::mmst_reg_t &r) { 9432 uint32_t f; 9433 outs() << "\t mmst_reg "; 9434 for (f = 0; f < 10; f++) 9435 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " "; 9436 outs() << "\n"; 9437 outs() << "\t mmst_rsrv "; 9438 for (f = 0; f < 6; f++) 9439 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " "; 9440 outs() << "\n"; 9441 } 9442 9443 static void Print_xmm_reg(MachO::xmm_reg_t &r) { 9444 uint32_t f; 9445 outs() << "\t xmm_reg "; 9446 for (f = 0; f < 16; f++) 9447 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " "; 9448 outs() << "\n"; 9449 } 9450 9451 static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) { 9452 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0]; 9453 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n"; 9454 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid; 9455 outs() << " denorm " << fpu.fpu_fcw.denorm; 9456 outs() << " zdiv " << fpu.fpu_fcw.zdiv; 9457 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl; 9458 outs() << " undfl " << fpu.fpu_fcw.undfl; 9459 outs() << " precis " << fpu.fpu_fcw.precis << "\n"; 9460 outs() << "\t\t pc "; 9461 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B) 9462 outs() << "FP_PREC_24B "; 9463 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B) 9464 outs() << "FP_PREC_53B "; 9465 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B) 9466 outs() << "FP_PREC_64B "; 9467 else 9468 outs() << fpu.fpu_fcw.pc << " "; 9469 outs() << "rc "; 9470 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR) 9471 outs() << "FP_RND_NEAR "; 9472 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN) 9473 outs() << "FP_RND_DOWN "; 9474 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP) 9475 outs() << "FP_RND_UP "; 9476 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP) 9477 outs() << "FP_CHOP "; 9478 outs() << "\n"; 9479 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid; 9480 outs() << " denorm " << fpu.fpu_fsw.denorm; 9481 outs() << " zdiv " << fpu.fpu_fsw.zdiv; 9482 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl; 9483 outs() << " undfl " << fpu.fpu_fsw.undfl; 9484 outs() << " precis " << fpu.fpu_fsw.precis; 9485 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n"; 9486 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm; 9487 outs() << " c0 " << fpu.fpu_fsw.c0; 9488 outs() << " c1 " << fpu.fpu_fsw.c1; 9489 outs() << " c2 " << fpu.fpu_fsw.c2; 9490 outs() << " tos " << fpu.fpu_fsw.tos; 9491 outs() << " c3 " << fpu.fpu_fsw.c3; 9492 outs() << " busy " << fpu.fpu_fsw.busy << "\n"; 9493 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw); 9494 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1); 9495 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop); 9496 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n"; 9497 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs); 9498 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2); 9499 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp); 9500 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n"; 9501 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3); 9502 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr); 9503 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask); 9504 outs() << "\n"; 9505 outs() << "\t fpu_stmm0:\n"; 9506 Print_mmst_reg(fpu.fpu_stmm0); 9507 outs() << "\t fpu_stmm1:\n"; 9508 Print_mmst_reg(fpu.fpu_stmm1); 9509 outs() << "\t fpu_stmm2:\n"; 9510 Print_mmst_reg(fpu.fpu_stmm2); 9511 outs() << "\t fpu_stmm3:\n"; 9512 Print_mmst_reg(fpu.fpu_stmm3); 9513 outs() << "\t fpu_stmm4:\n"; 9514 Print_mmst_reg(fpu.fpu_stmm4); 9515 outs() << "\t fpu_stmm5:\n"; 9516 Print_mmst_reg(fpu.fpu_stmm5); 9517 outs() << "\t fpu_stmm6:\n"; 9518 Print_mmst_reg(fpu.fpu_stmm6); 9519 outs() << "\t fpu_stmm7:\n"; 9520 Print_mmst_reg(fpu.fpu_stmm7); 9521 outs() << "\t fpu_xmm0:\n"; 9522 Print_xmm_reg(fpu.fpu_xmm0); 9523 outs() << "\t fpu_xmm1:\n"; 9524 Print_xmm_reg(fpu.fpu_xmm1); 9525 outs() << "\t fpu_xmm2:\n"; 9526 Print_xmm_reg(fpu.fpu_xmm2); 9527 outs() << "\t fpu_xmm3:\n"; 9528 Print_xmm_reg(fpu.fpu_xmm3); 9529 outs() << "\t fpu_xmm4:\n"; 9530 Print_xmm_reg(fpu.fpu_xmm4); 9531 outs() << "\t fpu_xmm5:\n"; 9532 Print_xmm_reg(fpu.fpu_xmm5); 9533 outs() << "\t fpu_xmm6:\n"; 9534 Print_xmm_reg(fpu.fpu_xmm6); 9535 outs() << "\t fpu_xmm7:\n"; 9536 Print_xmm_reg(fpu.fpu_xmm7); 9537 outs() << "\t fpu_xmm8:\n"; 9538 Print_xmm_reg(fpu.fpu_xmm8); 9539 outs() << "\t fpu_xmm9:\n"; 9540 Print_xmm_reg(fpu.fpu_xmm9); 9541 outs() << "\t fpu_xmm10:\n"; 9542 Print_xmm_reg(fpu.fpu_xmm10); 9543 outs() << "\t fpu_xmm11:\n"; 9544 Print_xmm_reg(fpu.fpu_xmm11); 9545 outs() << "\t fpu_xmm12:\n"; 9546 Print_xmm_reg(fpu.fpu_xmm12); 9547 outs() << "\t fpu_xmm13:\n"; 9548 Print_xmm_reg(fpu.fpu_xmm13); 9549 outs() << "\t fpu_xmm14:\n"; 9550 Print_xmm_reg(fpu.fpu_xmm14); 9551 outs() << "\t fpu_xmm15:\n"; 9552 Print_xmm_reg(fpu.fpu_xmm15); 9553 outs() << "\t fpu_rsrv4:\n"; 9554 for (uint32_t f = 0; f < 6; f++) { 9555 outs() << "\t "; 9556 for (uint32_t g = 0; g < 16; g++) 9557 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " "; 9558 outs() << "\n"; 9559 } 9560 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1); 9561 outs() << "\n"; 9562 } 9563 9564 static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) { 9565 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno); 9566 outs() << " err " << format("0x%08" PRIx32, exc64.err); 9567 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n"; 9568 } 9569 9570 static void Print_arm_thread_state32_t(MachO::arm_thread_state32_t &cpu32) { 9571 outs() << "\t r0 " << format("0x%08" PRIx32, cpu32.r[0]); 9572 outs() << " r1 " << format("0x%08" PRIx32, cpu32.r[1]); 9573 outs() << " r2 " << format("0x%08" PRIx32, cpu32.r[2]); 9574 outs() << " r3 " << format("0x%08" PRIx32, cpu32.r[3]) << "\n"; 9575 outs() << "\t r4 " << format("0x%08" PRIx32, cpu32.r[4]); 9576 outs() << " r5 " << format("0x%08" PRIx32, cpu32.r[5]); 9577 outs() << " r6 " << format("0x%08" PRIx32, cpu32.r[6]); 9578 outs() << " r7 " << format("0x%08" PRIx32, cpu32.r[7]) << "\n"; 9579 outs() << "\t r8 " << format("0x%08" PRIx32, cpu32.r[8]); 9580 outs() << " r9 " << format("0x%08" PRIx32, cpu32.r[9]); 9581 outs() << " r10 " << format("0x%08" PRIx32, cpu32.r[10]); 9582 outs() << " r11 " << format("0x%08" PRIx32, cpu32.r[11]) << "\n"; 9583 outs() << "\t r12 " << format("0x%08" PRIx32, cpu32.r[12]); 9584 outs() << " sp " << format("0x%08" PRIx32, cpu32.sp); 9585 outs() << " lr " << format("0x%08" PRIx32, cpu32.lr); 9586 outs() << " pc " << format("0x%08" PRIx32, cpu32.pc) << "\n"; 9587 outs() << "\t cpsr " << format("0x%08" PRIx32, cpu32.cpsr) << "\n"; 9588 } 9589 9590 static void Print_arm_thread_state64_t(MachO::arm_thread_state64_t &cpu64) { 9591 outs() << "\t x0 " << format("0x%016" PRIx64, cpu64.x[0]); 9592 outs() << " x1 " << format("0x%016" PRIx64, cpu64.x[1]); 9593 outs() << " x2 " << format("0x%016" PRIx64, cpu64.x[2]) << "\n"; 9594 outs() << "\t x3 " << format("0x%016" PRIx64, cpu64.x[3]); 9595 outs() << " x4 " << format("0x%016" PRIx64, cpu64.x[4]); 9596 outs() << " x5 " << format("0x%016" PRIx64, cpu64.x[5]) << "\n"; 9597 outs() << "\t x6 " << format("0x%016" PRIx64, cpu64.x[6]); 9598 outs() << " x7 " << format("0x%016" PRIx64, cpu64.x[7]); 9599 outs() << " x8 " << format("0x%016" PRIx64, cpu64.x[8]) << "\n"; 9600 outs() << "\t x9 " << format("0x%016" PRIx64, cpu64.x[9]); 9601 outs() << " x10 " << format("0x%016" PRIx64, cpu64.x[10]); 9602 outs() << " x11 " << format("0x%016" PRIx64, cpu64.x[11]) << "\n"; 9603 outs() << "\t x12 " << format("0x%016" PRIx64, cpu64.x[12]); 9604 outs() << " x13 " << format("0x%016" PRIx64, cpu64.x[13]); 9605 outs() << " x14 " << format("0x%016" PRIx64, cpu64.x[14]) << "\n"; 9606 outs() << "\t x15 " << format("0x%016" PRIx64, cpu64.x[15]); 9607 outs() << " x16 " << format("0x%016" PRIx64, cpu64.x[16]); 9608 outs() << " x17 " << format("0x%016" PRIx64, cpu64.x[17]) << "\n"; 9609 outs() << "\t x18 " << format("0x%016" PRIx64, cpu64.x[18]); 9610 outs() << " x19 " << format("0x%016" PRIx64, cpu64.x[19]); 9611 outs() << " x20 " << format("0x%016" PRIx64, cpu64.x[20]) << "\n"; 9612 outs() << "\t x21 " << format("0x%016" PRIx64, cpu64.x[21]); 9613 outs() << " x22 " << format("0x%016" PRIx64, cpu64.x[22]); 9614 outs() << " x23 " << format("0x%016" PRIx64, cpu64.x[23]) << "\n"; 9615 outs() << "\t x24 " << format("0x%016" PRIx64, cpu64.x[24]); 9616 outs() << " x25 " << format("0x%016" PRIx64, cpu64.x[25]); 9617 outs() << " x26 " << format("0x%016" PRIx64, cpu64.x[26]) << "\n"; 9618 outs() << "\t x27 " << format("0x%016" PRIx64, cpu64.x[27]); 9619 outs() << " x28 " << format("0x%016" PRIx64, cpu64.x[28]); 9620 outs() << " fp " << format("0x%016" PRIx64, cpu64.fp) << "\n"; 9621 outs() << "\t lr " << format("0x%016" PRIx64, cpu64.lr); 9622 outs() << " sp " << format("0x%016" PRIx64, cpu64.sp); 9623 outs() << " pc " << format("0x%016" PRIx64, cpu64.pc) << "\n"; 9624 outs() << "\t cpsr " << format("0x%08" PRIx32, cpu64.cpsr) << "\n"; 9625 } 9626 9627 static void PrintThreadCommand(MachO::thread_command t, const char *Ptr, 9628 bool isLittleEndian, uint32_t cputype) { 9629 if (t.cmd == MachO::LC_THREAD) 9630 outs() << " cmd LC_THREAD\n"; 9631 else if (t.cmd == MachO::LC_UNIXTHREAD) 9632 outs() << " cmd LC_UNIXTHREAD\n"; 9633 else 9634 outs() << " cmd " << t.cmd << " (unknown)\n"; 9635 outs() << " cmdsize " << t.cmdsize; 9636 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t)) 9637 outs() << " Incorrect size\n"; 9638 else 9639 outs() << "\n"; 9640 9641 const char *begin = Ptr + sizeof(struct MachO::thread_command); 9642 const char *end = Ptr + t.cmdsize; 9643 uint32_t flavor, count, left; 9644 if (cputype == MachO::CPU_TYPE_I386) { 9645 while (begin < end) { 9646 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9647 memcpy((char *)&flavor, begin, sizeof(uint32_t)); 9648 begin += sizeof(uint32_t); 9649 } else { 9650 flavor = 0; 9651 begin = end; 9652 } 9653 if (isLittleEndian != sys::IsLittleEndianHost) 9654 sys::swapByteOrder(flavor); 9655 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9656 memcpy((char *)&count, begin, sizeof(uint32_t)); 9657 begin += sizeof(uint32_t); 9658 } else { 9659 count = 0; 9660 begin = end; 9661 } 9662 if (isLittleEndian != sys::IsLittleEndianHost) 9663 sys::swapByteOrder(count); 9664 if (flavor == MachO::x86_THREAD_STATE32) { 9665 outs() << " flavor i386_THREAD_STATE\n"; 9666 if (count == MachO::x86_THREAD_STATE32_COUNT) 9667 outs() << " count i386_THREAD_STATE_COUNT\n"; 9668 else 9669 outs() << " count " << count 9670 << " (not x86_THREAD_STATE32_COUNT)\n"; 9671 MachO::x86_thread_state32_t cpu32; 9672 left = end - begin; 9673 if (left >= sizeof(MachO::x86_thread_state32_t)) { 9674 memcpy(&cpu32, begin, sizeof(MachO::x86_thread_state32_t)); 9675 begin += sizeof(MachO::x86_thread_state32_t); 9676 } else { 9677 memset(&cpu32, '\0', sizeof(MachO::x86_thread_state32_t)); 9678 memcpy(&cpu32, begin, left); 9679 begin += left; 9680 } 9681 if (isLittleEndian != sys::IsLittleEndianHost) 9682 swapStruct(cpu32); 9683 Print_x86_thread_state32_t(cpu32); 9684 } else if (flavor == MachO::x86_THREAD_STATE) { 9685 outs() << " flavor x86_THREAD_STATE\n"; 9686 if (count == MachO::x86_THREAD_STATE_COUNT) 9687 outs() << " count x86_THREAD_STATE_COUNT\n"; 9688 else 9689 outs() << " count " << count 9690 << " (not x86_THREAD_STATE_COUNT)\n"; 9691 struct MachO::x86_thread_state_t ts; 9692 left = end - begin; 9693 if (left >= sizeof(MachO::x86_thread_state_t)) { 9694 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t)); 9695 begin += sizeof(MachO::x86_thread_state_t); 9696 } else { 9697 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t)); 9698 memcpy(&ts, begin, left); 9699 begin += left; 9700 } 9701 if (isLittleEndian != sys::IsLittleEndianHost) 9702 swapStruct(ts); 9703 if (ts.tsh.flavor == MachO::x86_THREAD_STATE32) { 9704 outs() << "\t tsh.flavor x86_THREAD_STATE32 "; 9705 if (ts.tsh.count == MachO::x86_THREAD_STATE32_COUNT) 9706 outs() << "tsh.count x86_THREAD_STATE32_COUNT\n"; 9707 else 9708 outs() << "tsh.count " << ts.tsh.count 9709 << " (not x86_THREAD_STATE32_COUNT\n"; 9710 Print_x86_thread_state32_t(ts.uts.ts32); 9711 } else { 9712 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count " 9713 << ts.tsh.count << "\n"; 9714 } 9715 } else { 9716 outs() << " flavor " << flavor << " (unknown)\n"; 9717 outs() << " count " << count << "\n"; 9718 outs() << " state (unknown)\n"; 9719 begin += count * sizeof(uint32_t); 9720 } 9721 } 9722 } else if (cputype == MachO::CPU_TYPE_X86_64) { 9723 while (begin < end) { 9724 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9725 memcpy((char *)&flavor, begin, sizeof(uint32_t)); 9726 begin += sizeof(uint32_t); 9727 } else { 9728 flavor = 0; 9729 begin = end; 9730 } 9731 if (isLittleEndian != sys::IsLittleEndianHost) 9732 sys::swapByteOrder(flavor); 9733 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9734 memcpy((char *)&count, begin, sizeof(uint32_t)); 9735 begin += sizeof(uint32_t); 9736 } else { 9737 count = 0; 9738 begin = end; 9739 } 9740 if (isLittleEndian != sys::IsLittleEndianHost) 9741 sys::swapByteOrder(count); 9742 if (flavor == MachO::x86_THREAD_STATE64) { 9743 outs() << " flavor x86_THREAD_STATE64\n"; 9744 if (count == MachO::x86_THREAD_STATE64_COUNT) 9745 outs() << " count x86_THREAD_STATE64_COUNT\n"; 9746 else 9747 outs() << " count " << count 9748 << " (not x86_THREAD_STATE64_COUNT)\n"; 9749 MachO::x86_thread_state64_t cpu64; 9750 left = end - begin; 9751 if (left >= sizeof(MachO::x86_thread_state64_t)) { 9752 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t)); 9753 begin += sizeof(MachO::x86_thread_state64_t); 9754 } else { 9755 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t)); 9756 memcpy(&cpu64, begin, left); 9757 begin += left; 9758 } 9759 if (isLittleEndian != sys::IsLittleEndianHost) 9760 swapStruct(cpu64); 9761 Print_x86_thread_state64_t(cpu64); 9762 } else if (flavor == MachO::x86_THREAD_STATE) { 9763 outs() << " flavor x86_THREAD_STATE\n"; 9764 if (count == MachO::x86_THREAD_STATE_COUNT) 9765 outs() << " count x86_THREAD_STATE_COUNT\n"; 9766 else 9767 outs() << " count " << count 9768 << " (not x86_THREAD_STATE_COUNT)\n"; 9769 struct MachO::x86_thread_state_t ts; 9770 left = end - begin; 9771 if (left >= sizeof(MachO::x86_thread_state_t)) { 9772 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t)); 9773 begin += sizeof(MachO::x86_thread_state_t); 9774 } else { 9775 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t)); 9776 memcpy(&ts, begin, left); 9777 begin += left; 9778 } 9779 if (isLittleEndian != sys::IsLittleEndianHost) 9780 swapStruct(ts); 9781 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) { 9782 outs() << "\t tsh.flavor x86_THREAD_STATE64 "; 9783 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT) 9784 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n"; 9785 else 9786 outs() << "tsh.count " << ts.tsh.count 9787 << " (not x86_THREAD_STATE64_COUNT\n"; 9788 Print_x86_thread_state64_t(ts.uts.ts64); 9789 } else { 9790 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count " 9791 << ts.tsh.count << "\n"; 9792 } 9793 } else if (flavor == MachO::x86_FLOAT_STATE) { 9794 outs() << " flavor x86_FLOAT_STATE\n"; 9795 if (count == MachO::x86_FLOAT_STATE_COUNT) 9796 outs() << " count x86_FLOAT_STATE_COUNT\n"; 9797 else 9798 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n"; 9799 struct MachO::x86_float_state_t fs; 9800 left = end - begin; 9801 if (left >= sizeof(MachO::x86_float_state_t)) { 9802 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t)); 9803 begin += sizeof(MachO::x86_float_state_t); 9804 } else { 9805 memset(&fs, '\0', sizeof(MachO::x86_float_state_t)); 9806 memcpy(&fs, begin, left); 9807 begin += left; 9808 } 9809 if (isLittleEndian != sys::IsLittleEndianHost) 9810 swapStruct(fs); 9811 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) { 9812 outs() << "\t fsh.flavor x86_FLOAT_STATE64 "; 9813 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT) 9814 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n"; 9815 else 9816 outs() << "fsh.count " << fs.fsh.count 9817 << " (not x86_FLOAT_STATE64_COUNT\n"; 9818 Print_x86_float_state_t(fs.ufs.fs64); 9819 } else { 9820 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count " 9821 << fs.fsh.count << "\n"; 9822 } 9823 } else if (flavor == MachO::x86_EXCEPTION_STATE) { 9824 outs() << " flavor x86_EXCEPTION_STATE\n"; 9825 if (count == MachO::x86_EXCEPTION_STATE_COUNT) 9826 outs() << " count x86_EXCEPTION_STATE_COUNT\n"; 9827 else 9828 outs() << " count " << count 9829 << " (not x86_EXCEPTION_STATE_COUNT)\n"; 9830 struct MachO::x86_exception_state_t es; 9831 left = end - begin; 9832 if (left >= sizeof(MachO::x86_exception_state_t)) { 9833 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t)); 9834 begin += sizeof(MachO::x86_exception_state_t); 9835 } else { 9836 memset(&es, '\0', sizeof(MachO::x86_exception_state_t)); 9837 memcpy(&es, begin, left); 9838 begin += left; 9839 } 9840 if (isLittleEndian != sys::IsLittleEndianHost) 9841 swapStruct(es); 9842 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) { 9843 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n"; 9844 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT) 9845 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n"; 9846 else 9847 outs() << "\t esh.count " << es.esh.count 9848 << " (not x86_EXCEPTION_STATE64_COUNT\n"; 9849 Print_x86_exception_state_t(es.ues.es64); 9850 } else { 9851 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count " 9852 << es.esh.count << "\n"; 9853 } 9854 } else if (flavor == MachO::x86_EXCEPTION_STATE64) { 9855 outs() << " flavor x86_EXCEPTION_STATE64\n"; 9856 if (count == MachO::x86_EXCEPTION_STATE64_COUNT) 9857 outs() << " count x86_EXCEPTION_STATE64_COUNT\n"; 9858 else 9859 outs() << " count " << count 9860 << " (not x86_EXCEPTION_STATE64_COUNT)\n"; 9861 struct MachO::x86_exception_state64_t es64; 9862 left = end - begin; 9863 if (left >= sizeof(MachO::x86_exception_state64_t)) { 9864 memcpy(&es64, begin, sizeof(MachO::x86_exception_state64_t)); 9865 begin += sizeof(MachO::x86_exception_state64_t); 9866 } else { 9867 memset(&es64, '\0', sizeof(MachO::x86_exception_state64_t)); 9868 memcpy(&es64, begin, left); 9869 begin += left; 9870 } 9871 if (isLittleEndian != sys::IsLittleEndianHost) 9872 swapStruct(es64); 9873 Print_x86_exception_state_t(es64); 9874 } else { 9875 outs() << " flavor " << flavor << " (unknown)\n"; 9876 outs() << " count " << count << "\n"; 9877 outs() << " state (unknown)\n"; 9878 begin += count * sizeof(uint32_t); 9879 } 9880 } 9881 } else if (cputype == MachO::CPU_TYPE_ARM) { 9882 while (begin < end) { 9883 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9884 memcpy((char *)&flavor, begin, sizeof(uint32_t)); 9885 begin += sizeof(uint32_t); 9886 } else { 9887 flavor = 0; 9888 begin = end; 9889 } 9890 if (isLittleEndian != sys::IsLittleEndianHost) 9891 sys::swapByteOrder(flavor); 9892 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9893 memcpy((char *)&count, begin, sizeof(uint32_t)); 9894 begin += sizeof(uint32_t); 9895 } else { 9896 count = 0; 9897 begin = end; 9898 } 9899 if (isLittleEndian != sys::IsLittleEndianHost) 9900 sys::swapByteOrder(count); 9901 if (flavor == MachO::ARM_THREAD_STATE) { 9902 outs() << " flavor ARM_THREAD_STATE\n"; 9903 if (count == MachO::ARM_THREAD_STATE_COUNT) 9904 outs() << " count ARM_THREAD_STATE_COUNT\n"; 9905 else 9906 outs() << " count " << count 9907 << " (not ARM_THREAD_STATE_COUNT)\n"; 9908 MachO::arm_thread_state32_t cpu32; 9909 left = end - begin; 9910 if (left >= sizeof(MachO::arm_thread_state32_t)) { 9911 memcpy(&cpu32, begin, sizeof(MachO::arm_thread_state32_t)); 9912 begin += sizeof(MachO::arm_thread_state32_t); 9913 } else { 9914 memset(&cpu32, '\0', sizeof(MachO::arm_thread_state32_t)); 9915 memcpy(&cpu32, begin, left); 9916 begin += left; 9917 } 9918 if (isLittleEndian != sys::IsLittleEndianHost) 9919 swapStruct(cpu32); 9920 Print_arm_thread_state32_t(cpu32); 9921 } else { 9922 outs() << " flavor " << flavor << " (unknown)\n"; 9923 outs() << " count " << count << "\n"; 9924 outs() << " state (unknown)\n"; 9925 begin += count * sizeof(uint32_t); 9926 } 9927 } 9928 } else if (cputype == MachO::CPU_TYPE_ARM64 || 9929 cputype == MachO::CPU_TYPE_ARM64_32) { 9930 while (begin < end) { 9931 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9932 memcpy((char *)&flavor, begin, sizeof(uint32_t)); 9933 begin += sizeof(uint32_t); 9934 } else { 9935 flavor = 0; 9936 begin = end; 9937 } 9938 if (isLittleEndian != sys::IsLittleEndianHost) 9939 sys::swapByteOrder(flavor); 9940 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9941 memcpy((char *)&count, begin, sizeof(uint32_t)); 9942 begin += sizeof(uint32_t); 9943 } else { 9944 count = 0; 9945 begin = end; 9946 } 9947 if (isLittleEndian != sys::IsLittleEndianHost) 9948 sys::swapByteOrder(count); 9949 if (flavor == MachO::ARM_THREAD_STATE64) { 9950 outs() << " flavor ARM_THREAD_STATE64\n"; 9951 if (count == MachO::ARM_THREAD_STATE64_COUNT) 9952 outs() << " count ARM_THREAD_STATE64_COUNT\n"; 9953 else 9954 outs() << " count " << count 9955 << " (not ARM_THREAD_STATE64_COUNT)\n"; 9956 MachO::arm_thread_state64_t cpu64; 9957 left = end - begin; 9958 if (left >= sizeof(MachO::arm_thread_state64_t)) { 9959 memcpy(&cpu64, begin, sizeof(MachO::arm_thread_state64_t)); 9960 begin += sizeof(MachO::arm_thread_state64_t); 9961 } else { 9962 memset(&cpu64, '\0', sizeof(MachO::arm_thread_state64_t)); 9963 memcpy(&cpu64, begin, left); 9964 begin += left; 9965 } 9966 if (isLittleEndian != sys::IsLittleEndianHost) 9967 swapStruct(cpu64); 9968 Print_arm_thread_state64_t(cpu64); 9969 } else { 9970 outs() << " flavor " << flavor << " (unknown)\n"; 9971 outs() << " count " << count << "\n"; 9972 outs() << " state (unknown)\n"; 9973 begin += count * sizeof(uint32_t); 9974 } 9975 } 9976 } else { 9977 while (begin < end) { 9978 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9979 memcpy((char *)&flavor, begin, sizeof(uint32_t)); 9980 begin += sizeof(uint32_t); 9981 } else { 9982 flavor = 0; 9983 begin = end; 9984 } 9985 if (isLittleEndian != sys::IsLittleEndianHost) 9986 sys::swapByteOrder(flavor); 9987 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 9988 memcpy((char *)&count, begin, sizeof(uint32_t)); 9989 begin += sizeof(uint32_t); 9990 } else { 9991 count = 0; 9992 begin = end; 9993 } 9994 if (isLittleEndian != sys::IsLittleEndianHost) 9995 sys::swapByteOrder(count); 9996 outs() << " flavor " << flavor << "\n"; 9997 outs() << " count " << count << "\n"; 9998 outs() << " state (Unknown cputype/cpusubtype)\n"; 9999 begin += count * sizeof(uint32_t); 10000 } 10001 } 10002 } 10003 10004 static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) { 10005 if (dl.cmd == MachO::LC_ID_DYLIB) 10006 outs() << " cmd LC_ID_DYLIB\n"; 10007 else if (dl.cmd == MachO::LC_LOAD_DYLIB) 10008 outs() << " cmd LC_LOAD_DYLIB\n"; 10009 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB) 10010 outs() << " cmd LC_LOAD_WEAK_DYLIB\n"; 10011 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB) 10012 outs() << " cmd LC_REEXPORT_DYLIB\n"; 10013 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB) 10014 outs() << " cmd LC_LAZY_LOAD_DYLIB\n"; 10015 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB) 10016 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n"; 10017 else 10018 outs() << " cmd " << dl.cmd << " (unknown)\n"; 10019 outs() << " cmdsize " << dl.cmdsize; 10020 if (dl.cmdsize < sizeof(struct MachO::dylib_command)) 10021 outs() << " Incorrect size\n"; 10022 else 10023 outs() << "\n"; 10024 if (dl.dylib.name < dl.cmdsize) { 10025 const char *P = (const char *)(Ptr) + dl.dylib.name; 10026 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n"; 10027 } else { 10028 outs() << " name ?(bad offset " << dl.dylib.name << ")\n"; 10029 } 10030 outs() << " time stamp " << dl.dylib.timestamp << " "; 10031 time_t t = dl.dylib.timestamp; 10032 outs() << ctime(&t); 10033 outs() << " current version "; 10034 if (dl.dylib.current_version == 0xffffffff) 10035 outs() << "n/a\n"; 10036 else 10037 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "." 10038 << ((dl.dylib.current_version >> 8) & 0xff) << "." 10039 << (dl.dylib.current_version & 0xff) << "\n"; 10040 outs() << "compatibility version "; 10041 if (dl.dylib.compatibility_version == 0xffffffff) 10042 outs() << "n/a\n"; 10043 else 10044 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "." 10045 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "." 10046 << (dl.dylib.compatibility_version & 0xff) << "\n"; 10047 } 10048 10049 static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld, 10050 uint32_t object_size) { 10051 if (ld.cmd == MachO::LC_CODE_SIGNATURE) 10052 outs() << " cmd LC_CODE_SIGNATURE\n"; 10053 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO) 10054 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n"; 10055 else if (ld.cmd == MachO::LC_FUNCTION_STARTS) 10056 outs() << " cmd LC_FUNCTION_STARTS\n"; 10057 else if (ld.cmd == MachO::LC_DATA_IN_CODE) 10058 outs() << " cmd LC_DATA_IN_CODE\n"; 10059 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS) 10060 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n"; 10061 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) 10062 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n"; 10063 else 10064 outs() << " cmd " << ld.cmd << " (?)\n"; 10065 outs() << " cmdsize " << ld.cmdsize; 10066 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command)) 10067 outs() << " Incorrect size\n"; 10068 else 10069 outs() << "\n"; 10070 outs() << " dataoff " << ld.dataoff; 10071 if (ld.dataoff > object_size) 10072 outs() << " (past end of file)\n"; 10073 else 10074 outs() << "\n"; 10075 outs() << " datasize " << ld.datasize; 10076 uint64_t big_size = ld.dataoff; 10077 big_size += ld.datasize; 10078 if (big_size > object_size) 10079 outs() << " (past end of file)\n"; 10080 else 10081 outs() << "\n"; 10082 } 10083 10084 static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype, 10085 uint32_t cputype, bool verbose) { 10086 StringRef Buf = Obj->getData(); 10087 unsigned Index = 0; 10088 for (const auto &Command : Obj->load_commands()) { 10089 outs() << "Load command " << Index++ << "\n"; 10090 if (Command.C.cmd == MachO::LC_SEGMENT) { 10091 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command); 10092 const char *sg_segname = SLC.segname; 10093 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr, 10094 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot, 10095 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(), 10096 verbose); 10097 for (unsigned j = 0; j < SLC.nsects; j++) { 10098 MachO::section S = Obj->getSection(Command, j); 10099 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align, 10100 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2, 10101 SLC.cmd, sg_segname, filetype, Buf.size(), verbose); 10102 } 10103 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) { 10104 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command); 10105 const char *sg_segname = SLC_64.segname; 10106 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname, 10107 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff, 10108 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot, 10109 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose); 10110 for (unsigned j = 0; j < SLC_64.nsects; j++) { 10111 MachO::section_64 S_64 = Obj->getSection64(Command, j); 10112 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size, 10113 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc, 10114 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd, 10115 sg_segname, filetype, Buf.size(), verbose); 10116 } 10117 } else if (Command.C.cmd == MachO::LC_SYMTAB) { 10118 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand(); 10119 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size()); 10120 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) { 10121 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand(); 10122 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand(); 10123 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(), 10124 Obj->is64Bit()); 10125 } else if (Command.C.cmd == MachO::LC_DYLD_INFO || 10126 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) { 10127 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command); 10128 PrintDyldInfoLoadCommand(DyldInfo, Buf.size()); 10129 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER || 10130 Command.C.cmd == MachO::LC_ID_DYLINKER || 10131 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) { 10132 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command); 10133 PrintDyldLoadCommand(Dyld, Command.Ptr); 10134 } else if (Command.C.cmd == MachO::LC_UUID) { 10135 MachO::uuid_command Uuid = Obj->getUuidCommand(Command); 10136 PrintUuidLoadCommand(Uuid); 10137 } else if (Command.C.cmd == MachO::LC_RPATH) { 10138 MachO::rpath_command Rpath = Obj->getRpathCommand(Command); 10139 PrintRpathLoadCommand(Rpath, Command.Ptr); 10140 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX || 10141 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS || 10142 Command.C.cmd == MachO::LC_VERSION_MIN_TVOS || 10143 Command.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) { 10144 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command); 10145 PrintVersionMinLoadCommand(Vd); 10146 } else if (Command.C.cmd == MachO::LC_NOTE) { 10147 MachO::note_command Nt = Obj->getNoteLoadCommand(Command); 10148 PrintNoteLoadCommand(Nt); 10149 } else if (Command.C.cmd == MachO::LC_BUILD_VERSION) { 10150 MachO::build_version_command Bv = 10151 Obj->getBuildVersionLoadCommand(Command); 10152 PrintBuildVersionLoadCommand(Obj, Bv); 10153 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) { 10154 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command); 10155 PrintSourceVersionCommand(Sd); 10156 } else if (Command.C.cmd == MachO::LC_MAIN) { 10157 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command); 10158 PrintEntryPointCommand(Ep); 10159 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) { 10160 MachO::encryption_info_command Ei = 10161 Obj->getEncryptionInfoCommand(Command); 10162 PrintEncryptionInfoCommand(Ei, Buf.size()); 10163 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) { 10164 MachO::encryption_info_command_64 Ei = 10165 Obj->getEncryptionInfoCommand64(Command); 10166 PrintEncryptionInfoCommand64(Ei, Buf.size()); 10167 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) { 10168 MachO::linker_option_command Lo = 10169 Obj->getLinkerOptionLoadCommand(Command); 10170 PrintLinkerOptionCommand(Lo, Command.Ptr); 10171 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) { 10172 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command); 10173 PrintSubFrameworkCommand(Sf, Command.Ptr); 10174 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) { 10175 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command); 10176 PrintSubUmbrellaCommand(Sf, Command.Ptr); 10177 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) { 10178 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command); 10179 PrintSubLibraryCommand(Sl, Command.Ptr); 10180 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) { 10181 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command); 10182 PrintSubClientCommand(Sc, Command.Ptr); 10183 } else if (Command.C.cmd == MachO::LC_ROUTINES) { 10184 MachO::routines_command Rc = Obj->getRoutinesCommand(Command); 10185 PrintRoutinesCommand(Rc); 10186 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) { 10187 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command); 10188 PrintRoutinesCommand64(Rc); 10189 } else if (Command.C.cmd == MachO::LC_THREAD || 10190 Command.C.cmd == MachO::LC_UNIXTHREAD) { 10191 MachO::thread_command Tc = Obj->getThreadCommand(Command); 10192 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype); 10193 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB || 10194 Command.C.cmd == MachO::LC_ID_DYLIB || 10195 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB || 10196 Command.C.cmd == MachO::LC_REEXPORT_DYLIB || 10197 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB || 10198 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) { 10199 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command); 10200 PrintDylibCommand(Dl, Command.Ptr); 10201 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE || 10202 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO || 10203 Command.C.cmd == MachO::LC_FUNCTION_STARTS || 10204 Command.C.cmd == MachO::LC_DATA_IN_CODE || 10205 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS || 10206 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) { 10207 MachO::linkedit_data_command Ld = 10208 Obj->getLinkeditDataLoadCommand(Command); 10209 PrintLinkEditDataCommand(Ld, Buf.size()); 10210 } else { 10211 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd) 10212 << ")\n"; 10213 outs() << " cmdsize " << Command.C.cmdsize << "\n"; 10214 // TODO: get and print the raw bytes of the load command. 10215 } 10216 // TODO: print all the other kinds of load commands. 10217 } 10218 } 10219 10220 static void PrintMachHeader(const MachOObjectFile *Obj, bool verbose) { 10221 if (Obj->is64Bit()) { 10222 MachO::mach_header_64 H_64; 10223 H_64 = Obj->getHeader64(); 10224 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype, 10225 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose); 10226 } else { 10227 MachO::mach_header H; 10228 H = Obj->getHeader(); 10229 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds, 10230 H.sizeofcmds, H.flags, verbose); 10231 } 10232 } 10233 10234 void objdump::printMachOFileHeader(const object::ObjectFile *Obj) { 10235 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj); 10236 PrintMachHeader(file, !NonVerbose); 10237 } 10238 10239 void objdump::printMachOLoadCommands(const object::ObjectFile *Obj) { 10240 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj); 10241 uint32_t filetype = 0; 10242 uint32_t cputype = 0; 10243 if (file->is64Bit()) { 10244 MachO::mach_header_64 H_64; 10245 H_64 = file->getHeader64(); 10246 filetype = H_64.filetype; 10247 cputype = H_64.cputype; 10248 } else { 10249 MachO::mach_header H; 10250 H = file->getHeader(); 10251 filetype = H.filetype; 10252 cputype = H.cputype; 10253 } 10254 PrintLoadCommands(file, filetype, cputype, !NonVerbose); 10255 } 10256 10257 //===----------------------------------------------------------------------===// 10258 // export trie dumping 10259 //===----------------------------------------------------------------------===// 10260 10261 static void printMachOExportsTrie(const object::MachOObjectFile *Obj) { 10262 uint64_t BaseSegmentAddress = 0; 10263 for (const auto &Command : Obj->load_commands()) { 10264 if (Command.C.cmd == MachO::LC_SEGMENT) { 10265 MachO::segment_command Seg = Obj->getSegmentLoadCommand(Command); 10266 if (Seg.fileoff == 0 && Seg.filesize != 0) { 10267 BaseSegmentAddress = Seg.vmaddr; 10268 break; 10269 } 10270 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) { 10271 MachO::segment_command_64 Seg = Obj->getSegment64LoadCommand(Command); 10272 if (Seg.fileoff == 0 && Seg.filesize != 0) { 10273 BaseSegmentAddress = Seg.vmaddr; 10274 break; 10275 } 10276 } 10277 } 10278 Error Err = Error::success(); 10279 for (const object::ExportEntry &Entry : Obj->exports(Err)) { 10280 uint64_t Flags = Entry.flags(); 10281 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT); 10282 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION); 10283 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) == 10284 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL); 10285 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) == 10286 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE); 10287 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER); 10288 if (ReExport) 10289 outs() << "[re-export] "; 10290 else 10291 outs() << format("0x%08llX ", 10292 Entry.address() + BaseSegmentAddress); 10293 outs() << Entry.name(); 10294 if (WeakDef || ThreadLocal || Resolver || Abs) { 10295 bool NeedsComma = false; 10296 outs() << " ["; 10297 if (WeakDef) { 10298 outs() << "weak_def"; 10299 NeedsComma = true; 10300 } 10301 if (ThreadLocal) { 10302 if (NeedsComma) 10303 outs() << ", "; 10304 outs() << "per-thread"; 10305 NeedsComma = true; 10306 } 10307 if (Abs) { 10308 if (NeedsComma) 10309 outs() << ", "; 10310 outs() << "absolute"; 10311 NeedsComma = true; 10312 } 10313 if (Resolver) { 10314 if (NeedsComma) 10315 outs() << ", "; 10316 outs() << format("resolver=0x%08llX", Entry.other()); 10317 NeedsComma = true; 10318 } 10319 outs() << "]"; 10320 } 10321 if (ReExport) { 10322 StringRef DylibName = "unknown"; 10323 int Ordinal = Entry.other() - 1; 10324 Obj->getLibraryShortNameByIndex(Ordinal, DylibName); 10325 if (Entry.otherName().empty()) 10326 outs() << " (from " << DylibName << ")"; 10327 else 10328 outs() << " (" << Entry.otherName() << " from " << DylibName << ")"; 10329 } 10330 outs() << "\n"; 10331 } 10332 if (Err) 10333 reportError(std::move(Err), Obj->getFileName()); 10334 } 10335 10336 //===----------------------------------------------------------------------===// 10337 // rebase table dumping 10338 //===----------------------------------------------------------------------===// 10339 10340 static void printMachORebaseTable(object::MachOObjectFile *Obj) { 10341 outs() << "segment section address type\n"; 10342 Error Err = Error::success(); 10343 for (const object::MachORebaseEntry &Entry : Obj->rebaseTable(Err)) { 10344 StringRef SegmentName = Entry.segmentName(); 10345 StringRef SectionName = Entry.sectionName(); 10346 uint64_t Address = Entry.address(); 10347 10348 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer 10349 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n", 10350 SegmentName.str().c_str(), SectionName.str().c_str(), 10351 Address, Entry.typeName().str().c_str()); 10352 } 10353 if (Err) 10354 reportError(std::move(Err), Obj->getFileName()); 10355 } 10356 10357 static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) { 10358 StringRef DylibName; 10359 switch (Ordinal) { 10360 case MachO::BIND_SPECIAL_DYLIB_SELF: 10361 return "this-image"; 10362 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE: 10363 return "main-executable"; 10364 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP: 10365 return "flat-namespace"; 10366 default: 10367 if (Ordinal > 0) { 10368 std::error_code EC = 10369 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName); 10370 if (EC) 10371 return "<<bad library ordinal>>"; 10372 return DylibName; 10373 } 10374 } 10375 return "<<unknown special ordinal>>"; 10376 } 10377 10378 //===----------------------------------------------------------------------===// 10379 // bind table dumping 10380 //===----------------------------------------------------------------------===// 10381 10382 static void printMachOBindTable(object::MachOObjectFile *Obj) { 10383 // Build table of sections so names can used in final output. 10384 outs() << "segment section address type " 10385 "addend dylib symbol\n"; 10386 Error Err = Error::success(); 10387 for (const object::MachOBindEntry &Entry : Obj->bindTable(Err)) { 10388 StringRef SegmentName = Entry.segmentName(); 10389 StringRef SectionName = Entry.sectionName(); 10390 uint64_t Address = Entry.address(); 10391 10392 // Table lines look like: 10393 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard 10394 StringRef Attr; 10395 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT) 10396 Attr = " (weak_import)"; 10397 outs() << left_justify(SegmentName, 8) << " " 10398 << left_justify(SectionName, 18) << " " 10399 << format_hex(Address, 10, true) << " " 10400 << left_justify(Entry.typeName(), 8) << " " 10401 << format_decimal(Entry.addend(), 8) << " " 10402 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " " 10403 << Entry.symbolName() << Attr << "\n"; 10404 } 10405 if (Err) 10406 reportError(std::move(Err), Obj->getFileName()); 10407 } 10408 10409 //===----------------------------------------------------------------------===// 10410 // lazy bind table dumping 10411 //===----------------------------------------------------------------------===// 10412 10413 static void printMachOLazyBindTable(object::MachOObjectFile *Obj) { 10414 outs() << "segment section address " 10415 "dylib symbol\n"; 10416 Error Err = Error::success(); 10417 for (const object::MachOBindEntry &Entry : Obj->lazyBindTable(Err)) { 10418 StringRef SegmentName = Entry.segmentName(); 10419 StringRef SectionName = Entry.sectionName(); 10420 uint64_t Address = Entry.address(); 10421 10422 // Table lines look like: 10423 // __DATA __got 0x00012010 libSystem ___stack_chk_guard 10424 outs() << left_justify(SegmentName, 8) << " " 10425 << left_justify(SectionName, 18) << " " 10426 << format_hex(Address, 10, true) << " " 10427 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " " 10428 << Entry.symbolName() << "\n"; 10429 } 10430 if (Err) 10431 reportError(std::move(Err), Obj->getFileName()); 10432 } 10433 10434 //===----------------------------------------------------------------------===// 10435 // weak bind table dumping 10436 //===----------------------------------------------------------------------===// 10437 10438 static void printMachOWeakBindTable(object::MachOObjectFile *Obj) { 10439 outs() << "segment section address " 10440 "type addend symbol\n"; 10441 Error Err = Error::success(); 10442 for (const object::MachOBindEntry &Entry : Obj->weakBindTable(Err)) { 10443 // Strong symbols don't have a location to update. 10444 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) { 10445 outs() << " strong " 10446 << Entry.symbolName() << "\n"; 10447 continue; 10448 } 10449 StringRef SegmentName = Entry.segmentName(); 10450 StringRef SectionName = Entry.sectionName(); 10451 uint64_t Address = Entry.address(); 10452 10453 // Table lines look like: 10454 // __DATA __data 0x00001000 pointer 0 _foo 10455 outs() << left_justify(SegmentName, 8) << " " 10456 << left_justify(SectionName, 18) << " " 10457 << format_hex(Address, 10, true) << " " 10458 << left_justify(Entry.typeName(), 8) << " " 10459 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName() 10460 << "\n"; 10461 } 10462 if (Err) 10463 reportError(std::move(Err), Obj->getFileName()); 10464 } 10465 10466 // get_dyld_bind_info_symbolname() is used for disassembly and passed an 10467 // address, ReferenceValue, in the Mach-O file and looks in the dyld bind 10468 // information for that address. If the address is found its binding symbol 10469 // name is returned. If not nullptr is returned. 10470 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue, 10471 struct DisassembleInfo *info) { 10472 if (info->bindtable == nullptr) { 10473 info->bindtable = std::make_unique<SymbolAddressMap>(); 10474 Error Err = Error::success(); 10475 for (const object::MachOBindEntry &Entry : info->O->bindTable(Err)) { 10476 uint64_t Address = Entry.address(); 10477 StringRef name = Entry.symbolName(); 10478 if (!name.empty()) 10479 (*info->bindtable)[Address] = name; 10480 } 10481 if (Err) 10482 reportError(std::move(Err), info->O->getFileName()); 10483 } 10484 auto name = info->bindtable->lookup(ReferenceValue); 10485 return !name.empty() ? name.data() : nullptr; 10486 } 10487 10488 void objdump::printLazyBindTable(ObjectFile *o) { 10489 outs() << "Lazy bind table:\n"; 10490 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 10491 printMachOLazyBindTable(MachO); 10492 else 10493 WithColor::error() 10494 << "This operation is only currently supported " 10495 "for Mach-O executable files.\n"; 10496 } 10497 10498 void objdump::printWeakBindTable(ObjectFile *o) { 10499 outs() << "Weak bind table:\n"; 10500 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 10501 printMachOWeakBindTable(MachO); 10502 else 10503 WithColor::error() 10504 << "This operation is only currently supported " 10505 "for Mach-O executable files.\n"; 10506 } 10507 10508 void objdump::printExportsTrie(const ObjectFile *o) { 10509 outs() << "Exports trie:\n"; 10510 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 10511 printMachOExportsTrie(MachO); 10512 else 10513 WithColor::error() 10514 << "This operation is only currently supported " 10515 "for Mach-O executable files.\n"; 10516 } 10517 10518 void objdump::printRebaseTable(ObjectFile *o) { 10519 outs() << "Rebase table:\n"; 10520 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 10521 printMachORebaseTable(MachO); 10522 else 10523 WithColor::error() 10524 << "This operation is only currently supported " 10525 "for Mach-O executable files.\n"; 10526 } 10527 10528 void objdump::printBindTable(ObjectFile *o) { 10529 outs() << "Bind table:\n"; 10530 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 10531 printMachOBindTable(MachO); 10532 else 10533 WithColor::error() 10534 << "This operation is only currently supported " 10535 "for Mach-O executable files.\n"; 10536 } 10537