1 //===-- MachODump.cpp - Object file dumping utility for llvm --------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the MachO-specific dumper for llvm-objdump. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm-objdump.h" 15 #include "llvm-c/Disassembler.h" 16 #include "llvm/ADT/STLExtras.h" 17 #include "llvm/ADT/StringExtras.h" 18 #include "llvm/ADT/Triple.h" 19 #include "llvm/Config/config.h" 20 #include "llvm/DebugInfo/DIContext.h" 21 #include "llvm/DebugInfo/DWARF/DWARFContext.h" 22 #include "llvm/MC/MCAsmInfo.h" 23 #include "llvm/MC/MCContext.h" 24 #include "llvm/MC/MCDisassembler.h" 25 #include "llvm/MC/MCInst.h" 26 #include "llvm/MC/MCInstPrinter.h" 27 #include "llvm/MC/MCInstrDesc.h" 28 #include "llvm/MC/MCInstrInfo.h" 29 #include "llvm/MC/MCRegisterInfo.h" 30 #include "llvm/MC/MCSubtargetInfo.h" 31 #include "llvm/Object/MachO.h" 32 #include "llvm/Object/MachOUniversal.h" 33 #include "llvm/Support/Casting.h" 34 #include "llvm/Support/CommandLine.h" 35 #include "llvm/Support/Debug.h" 36 #include "llvm/Support/Endian.h" 37 #include "llvm/Support/Format.h" 38 #include "llvm/Support/FormattedStream.h" 39 #include "llvm/Support/GraphWriter.h" 40 #include "llvm/Support/LEB128.h" 41 #include "llvm/Support/MachO.h" 42 #include "llvm/Support/MemoryBuffer.h" 43 #include "llvm/Support/TargetRegistry.h" 44 #include "llvm/Support/TargetSelect.h" 45 #include "llvm/Support/raw_ostream.h" 46 #include <algorithm> 47 #include <cstring> 48 #include <system_error> 49 50 #if HAVE_CXXABI_H 51 #include <cxxabi.h> 52 #endif 53 54 using namespace llvm; 55 using namespace object; 56 57 static cl::opt<bool> 58 UseDbg("g", 59 cl::desc("Print line information from debug info if available")); 60 61 static cl::opt<std::string> DSYMFile("dsym", 62 cl::desc("Use .dSYM file for debug info")); 63 64 static cl::opt<bool> FullLeadingAddr("full-leading-addr", 65 cl::desc("Print full leading address")); 66 67 static cl::opt<bool> NoLeadingAddr("no-leading-addr", 68 cl::desc("Print no leading address")); 69 70 static cl::opt<bool> 71 PrintImmHex("print-imm-hex", 72 cl::desc("Use hex format for immediate values")); 73 74 cl::opt<bool> llvm::UniversalHeaders("universal-headers", 75 cl::desc("Print Mach-O universal headers " 76 "(requires -macho)")); 77 78 cl::opt<bool> 79 llvm::ArchiveHeaders("archive-headers", 80 cl::desc("Print archive headers for Mach-O archives " 81 "(requires -macho)")); 82 83 cl::opt<bool> 84 ArchiveMemberOffsets("archive-member-offsets", 85 cl::desc("Print the offset to each archive member for " 86 "Mach-O archives (requires -macho and " 87 "-archive-headers)")); 88 89 cl::opt<bool> 90 llvm::IndirectSymbols("indirect-symbols", 91 cl::desc("Print indirect symbol table for Mach-O " 92 "objects (requires -macho)")); 93 94 cl::opt<bool> 95 llvm::DataInCode("data-in-code", 96 cl::desc("Print the data in code table for Mach-O objects " 97 "(requires -macho)")); 98 99 cl::opt<bool> 100 llvm::LinkOptHints("link-opt-hints", 101 cl::desc("Print the linker optimization hints for " 102 "Mach-O objects (requires -macho)")); 103 104 cl::list<std::string> 105 llvm::DumpSections("section", 106 cl::desc("Prints the specified segment,section for " 107 "Mach-O objects (requires -macho)")); 108 109 cl::opt<bool> llvm::Raw("raw", 110 cl::desc("Have -section dump the raw binary contents")); 111 112 cl::opt<bool> 113 llvm::InfoPlist("info-plist", 114 cl::desc("Print the info plist section as strings for " 115 "Mach-O objects (requires -macho)")); 116 117 cl::opt<bool> 118 llvm::DylibsUsed("dylibs-used", 119 cl::desc("Print the shared libraries used for linked " 120 "Mach-O files (requires -macho)")); 121 122 cl::opt<bool> 123 llvm::DylibId("dylib-id", 124 cl::desc("Print the shared library's id for the dylib Mach-O " 125 "file (requires -macho)")); 126 127 cl::opt<bool> 128 llvm::NonVerbose("non-verbose", 129 cl::desc("Print the info for Mach-O objects in " 130 "non-verbose or numeric form (requires -macho)")); 131 132 cl::opt<bool> 133 llvm::ObjcMetaData("objc-meta-data", 134 cl::desc("Print the Objective-C runtime meta data for " 135 "Mach-O files (requires -macho)")); 136 137 cl::opt<std::string> llvm::DisSymName( 138 "dis-symname", 139 cl::desc("disassemble just this symbol's instructions (requires -macho")); 140 141 static cl::opt<bool> NoSymbolicOperands( 142 "no-symbolic-operands", 143 cl::desc("do not symbolic operands when disassembling (requires -macho)")); 144 145 static cl::list<std::string> 146 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"), 147 cl::ZeroOrMore); 148 bool ArchAll = false; 149 150 static std::string ThumbTripleName; 151 152 static const Target *GetTarget(const MachOObjectFile *MachOObj, 153 const char **McpuDefault, 154 const Target **ThumbTarget) { 155 // Figure out the target triple. 156 if (TripleName.empty()) { 157 llvm::Triple TT("unknown-unknown-unknown"); 158 llvm::Triple ThumbTriple = Triple(); 159 TT = MachOObj->getArch(McpuDefault, &ThumbTriple); 160 TripleName = TT.str(); 161 ThumbTripleName = ThumbTriple.str(); 162 } 163 164 // Get the target specific parser. 165 std::string Error; 166 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); 167 if (TheTarget && ThumbTripleName.empty()) 168 return TheTarget; 169 170 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error); 171 if (*ThumbTarget) 172 return TheTarget; 173 174 errs() << "llvm-objdump: error: unable to get target for '"; 175 if (!TheTarget) 176 errs() << TripleName; 177 else 178 errs() << ThumbTripleName; 179 errs() << "', see --version and --triple.\n"; 180 return nullptr; 181 } 182 183 struct SymbolSorter { 184 bool operator()(const SymbolRef &A, const SymbolRef &B) { 185 SymbolRef::Type AType, BType; 186 A.getType(AType); 187 B.getType(BType); 188 189 uint64_t AAddr, BAddr; 190 if (AType != SymbolRef::ST_Function) 191 AAddr = 0; 192 else 193 A.getAddress(AAddr); 194 if (BType != SymbolRef::ST_Function) 195 BAddr = 0; 196 else 197 B.getAddress(BAddr); 198 return AAddr < BAddr; 199 } 200 }; 201 202 // Types for the storted data in code table that is built before disassembly 203 // and the predicate function to sort them. 204 typedef std::pair<uint64_t, DiceRef> DiceTableEntry; 205 typedef std::vector<DiceTableEntry> DiceTable; 206 typedef DiceTable::iterator dice_table_iterator; 207 208 // This is used to search for a data in code table entry for the PC being 209 // disassembled. The j parameter has the PC in j.first. A single data in code 210 // table entry can cover many bytes for each of its Kind's. So if the offset, 211 // aka the i.first value, of the data in code table entry plus its Length 212 // covers the PC being searched for this will return true. If not it will 213 // return false. 214 static bool compareDiceTableEntries(const DiceTableEntry &i, 215 const DiceTableEntry &j) { 216 uint16_t Length; 217 i.second.getLength(Length); 218 219 return j.first >= i.first && j.first < i.first + Length; 220 } 221 222 static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length, 223 unsigned short Kind) { 224 uint32_t Value, Size = 1; 225 226 switch (Kind) { 227 default: 228 case MachO::DICE_KIND_DATA: 229 if (Length >= 4) { 230 if (!NoShowRawInsn) 231 DumpBytes(ArrayRef<uint8_t>(bytes, 4)); 232 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0]; 233 outs() << "\t.long " << Value; 234 Size = 4; 235 } else if (Length >= 2) { 236 if (!NoShowRawInsn) 237 DumpBytes(ArrayRef<uint8_t>(bytes, 2)); 238 Value = bytes[1] << 8 | bytes[0]; 239 outs() << "\t.short " << Value; 240 Size = 2; 241 } else { 242 if (!NoShowRawInsn) 243 DumpBytes(ArrayRef<uint8_t>(bytes, 2)); 244 Value = bytes[0]; 245 outs() << "\t.byte " << Value; 246 Size = 1; 247 } 248 if (Kind == MachO::DICE_KIND_DATA) 249 outs() << "\t@ KIND_DATA\n"; 250 else 251 outs() << "\t@ data in code kind = " << Kind << "\n"; 252 break; 253 case MachO::DICE_KIND_JUMP_TABLE8: 254 if (!NoShowRawInsn) 255 DumpBytes(ArrayRef<uint8_t>(bytes, 1)); 256 Value = bytes[0]; 257 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n"; 258 Size = 1; 259 break; 260 case MachO::DICE_KIND_JUMP_TABLE16: 261 if (!NoShowRawInsn) 262 DumpBytes(ArrayRef<uint8_t>(bytes, 2)); 263 Value = bytes[1] << 8 | bytes[0]; 264 outs() << "\t.short " << format("%5u", Value & 0xffff) 265 << "\t@ KIND_JUMP_TABLE16\n"; 266 Size = 2; 267 break; 268 case MachO::DICE_KIND_JUMP_TABLE32: 269 case MachO::DICE_KIND_ABS_JUMP_TABLE32: 270 if (!NoShowRawInsn) 271 DumpBytes(ArrayRef<uint8_t>(bytes, 4)); 272 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0]; 273 outs() << "\t.long " << Value; 274 if (Kind == MachO::DICE_KIND_JUMP_TABLE32) 275 outs() << "\t@ KIND_JUMP_TABLE32\n"; 276 else 277 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n"; 278 Size = 4; 279 break; 280 } 281 return Size; 282 } 283 284 static void getSectionsAndSymbols(const MachO::mach_header Header, 285 MachOObjectFile *MachOObj, 286 std::vector<SectionRef> &Sections, 287 std::vector<SymbolRef> &Symbols, 288 SmallVectorImpl<uint64_t> &FoundFns, 289 uint64_t &BaseSegmentAddress) { 290 for (const SymbolRef &Symbol : MachOObj->symbols()) { 291 StringRef SymName; 292 Symbol.getName(SymName); 293 if (!SymName.startswith("ltmp")) 294 Symbols.push_back(Symbol); 295 } 296 297 for (const SectionRef &Section : MachOObj->sections()) { 298 StringRef SectName; 299 Section.getName(SectName); 300 Sections.push_back(Section); 301 } 302 303 MachOObjectFile::LoadCommandInfo Command = 304 MachOObj->getFirstLoadCommandInfo(); 305 bool BaseSegmentAddressSet = false; 306 for (unsigned i = 0;; ++i) { 307 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) { 308 // We found a function starts segment, parse the addresses for later 309 // consumption. 310 MachO::linkedit_data_command LLC = 311 MachOObj->getLinkeditDataLoadCommand(Command); 312 313 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns); 314 } else if (Command.C.cmd == MachO::LC_SEGMENT) { 315 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command); 316 StringRef SegName = SLC.segname; 317 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") { 318 BaseSegmentAddressSet = true; 319 BaseSegmentAddress = SLC.vmaddr; 320 } 321 } 322 323 if (i == Header.ncmds - 1) 324 break; 325 else 326 Command = MachOObj->getNextLoadCommandInfo(Command); 327 } 328 } 329 330 static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose, 331 uint32_t n, uint32_t count, 332 uint32_t stride, uint64_t addr) { 333 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand(); 334 uint32_t nindirectsyms = Dysymtab.nindirectsyms; 335 if (n > nindirectsyms) 336 outs() << " (entries start past the end of the indirect symbol " 337 "table) (reserved1 field greater than the table size)"; 338 else if (n + count > nindirectsyms) 339 outs() << " (entries extends past the end of the indirect symbol " 340 "table)"; 341 outs() << "\n"; 342 uint32_t cputype = O->getHeader().cputype; 343 if (cputype & MachO::CPU_ARCH_ABI64) 344 outs() << "address index"; 345 else 346 outs() << "address index"; 347 if (verbose) 348 outs() << " name\n"; 349 else 350 outs() << "\n"; 351 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) { 352 if (cputype & MachO::CPU_ARCH_ABI64) 353 outs() << format("0x%016" PRIx64, addr + j * stride) << " "; 354 else 355 outs() << format("0x%08" PRIx32, addr + j * stride) << " "; 356 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand(); 357 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j); 358 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) { 359 outs() << "LOCAL\n"; 360 continue; 361 } 362 if (indirect_symbol == 363 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) { 364 outs() << "LOCAL ABSOLUTE\n"; 365 continue; 366 } 367 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) { 368 outs() << "ABSOLUTE\n"; 369 continue; 370 } 371 outs() << format("%5u ", indirect_symbol); 372 if (verbose) { 373 MachO::symtab_command Symtab = O->getSymtabLoadCommand(); 374 if (indirect_symbol < Symtab.nsyms) { 375 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol); 376 SymbolRef Symbol = *Sym; 377 StringRef SymName; 378 Symbol.getName(SymName); 379 outs() << SymName; 380 } else { 381 outs() << "?"; 382 } 383 } 384 outs() << "\n"; 385 } 386 } 387 388 static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) { 389 uint32_t LoadCommandCount = O->getHeader().ncmds; 390 MachOObjectFile::LoadCommandInfo Load = O->getFirstLoadCommandInfo(); 391 for (unsigned I = 0;; ++I) { 392 if (Load.C.cmd == MachO::LC_SEGMENT_64) { 393 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load); 394 for (unsigned J = 0; J < Seg.nsects; ++J) { 395 MachO::section_64 Sec = O->getSection64(Load, J); 396 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE; 397 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS || 398 section_type == MachO::S_LAZY_SYMBOL_POINTERS || 399 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS || 400 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS || 401 section_type == MachO::S_SYMBOL_STUBS) { 402 uint32_t stride; 403 if (section_type == MachO::S_SYMBOL_STUBS) 404 stride = Sec.reserved2; 405 else 406 stride = 8; 407 if (stride == 0) { 408 outs() << "Can't print indirect symbols for (" << Sec.segname << "," 409 << Sec.sectname << ") " 410 << "(size of stubs in reserved2 field is zero)\n"; 411 continue; 412 } 413 uint32_t count = Sec.size / stride; 414 outs() << "Indirect symbols for (" << Sec.segname << "," 415 << Sec.sectname << ") " << count << " entries"; 416 uint32_t n = Sec.reserved1; 417 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr); 418 } 419 } 420 } else if (Load.C.cmd == MachO::LC_SEGMENT) { 421 MachO::segment_command Seg = O->getSegmentLoadCommand(Load); 422 for (unsigned J = 0; J < Seg.nsects; ++J) { 423 MachO::section Sec = O->getSection(Load, J); 424 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE; 425 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS || 426 section_type == MachO::S_LAZY_SYMBOL_POINTERS || 427 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS || 428 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS || 429 section_type == MachO::S_SYMBOL_STUBS) { 430 uint32_t stride; 431 if (section_type == MachO::S_SYMBOL_STUBS) 432 stride = Sec.reserved2; 433 else 434 stride = 4; 435 if (stride == 0) { 436 outs() << "Can't print indirect symbols for (" << Sec.segname << "," 437 << Sec.sectname << ") " 438 << "(size of stubs in reserved2 field is zero)\n"; 439 continue; 440 } 441 uint32_t count = Sec.size / stride; 442 outs() << "Indirect symbols for (" << Sec.segname << "," 443 << Sec.sectname << ") " << count << " entries"; 444 uint32_t n = Sec.reserved1; 445 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr); 446 } 447 } 448 } 449 if (I == LoadCommandCount - 1) 450 break; 451 else 452 Load = O->getNextLoadCommandInfo(Load); 453 } 454 } 455 456 static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) { 457 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand(); 458 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry); 459 outs() << "Data in code table (" << nentries << " entries)\n"; 460 outs() << "offset length kind\n"; 461 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE; 462 ++DI) { 463 uint32_t Offset; 464 DI->getOffset(Offset); 465 outs() << format("0x%08" PRIx32, Offset) << " "; 466 uint16_t Length; 467 DI->getLength(Length); 468 outs() << format("%6u", Length) << " "; 469 uint16_t Kind; 470 DI->getKind(Kind); 471 if (verbose) { 472 switch (Kind) { 473 case MachO::DICE_KIND_DATA: 474 outs() << "DATA"; 475 break; 476 case MachO::DICE_KIND_JUMP_TABLE8: 477 outs() << "JUMP_TABLE8"; 478 break; 479 case MachO::DICE_KIND_JUMP_TABLE16: 480 outs() << "JUMP_TABLE16"; 481 break; 482 case MachO::DICE_KIND_JUMP_TABLE32: 483 outs() << "JUMP_TABLE32"; 484 break; 485 case MachO::DICE_KIND_ABS_JUMP_TABLE32: 486 outs() << "ABS_JUMP_TABLE32"; 487 break; 488 default: 489 outs() << format("0x%04" PRIx32, Kind); 490 break; 491 } 492 } else 493 outs() << format("0x%04" PRIx32, Kind); 494 outs() << "\n"; 495 } 496 } 497 498 static void PrintLinkOptHints(MachOObjectFile *O) { 499 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand(); 500 const char *loh = O->getData().substr(LohLC.dataoff, 1).data(); 501 uint32_t nloh = LohLC.datasize; 502 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n"; 503 for (uint32_t i = 0; i < nloh;) { 504 unsigned n; 505 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n); 506 i += n; 507 outs() << " identifier " << identifier << " "; 508 if (i >= nloh) 509 return; 510 switch (identifier) { 511 case 1: 512 outs() << "AdrpAdrp\n"; 513 break; 514 case 2: 515 outs() << "AdrpLdr\n"; 516 break; 517 case 3: 518 outs() << "AdrpAddLdr\n"; 519 break; 520 case 4: 521 outs() << "AdrpLdrGotLdr\n"; 522 break; 523 case 5: 524 outs() << "AdrpAddStr\n"; 525 break; 526 case 6: 527 outs() << "AdrpLdrGotStr\n"; 528 break; 529 case 7: 530 outs() << "AdrpAdd\n"; 531 break; 532 case 8: 533 outs() << "AdrpLdrGot\n"; 534 break; 535 default: 536 outs() << "Unknown identifier value\n"; 537 break; 538 } 539 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n); 540 i += n; 541 outs() << " narguments " << narguments << "\n"; 542 if (i >= nloh) 543 return; 544 545 for (uint32_t j = 0; j < narguments; j++) { 546 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n); 547 i += n; 548 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n"; 549 if (i >= nloh) 550 return; 551 } 552 } 553 } 554 555 static void PrintDylibs(MachOObjectFile *O, bool JustId) { 556 uint32_t LoadCommandCount = O->getHeader().ncmds; 557 MachOObjectFile::LoadCommandInfo Load = O->getFirstLoadCommandInfo(); 558 for (unsigned I = 0;; ++I) { 559 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) || 560 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB || 561 Load.C.cmd == MachO::LC_LOAD_DYLIB || 562 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB || 563 Load.C.cmd == MachO::LC_REEXPORT_DYLIB || 564 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB || 565 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) { 566 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load); 567 if (dl.dylib.name < dl.cmdsize) { 568 const char *p = (const char *)(Load.Ptr) + dl.dylib.name; 569 if (JustId) 570 outs() << p << "\n"; 571 else { 572 outs() << "\t" << p; 573 outs() << " (compatibility version " 574 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "." 575 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "." 576 << (dl.dylib.compatibility_version & 0xff) << ","; 577 outs() << " current version " 578 << ((dl.dylib.current_version >> 16) & 0xffff) << "." 579 << ((dl.dylib.current_version >> 8) & 0xff) << "." 580 << (dl.dylib.current_version & 0xff) << ")\n"; 581 } 582 } else { 583 outs() << "\tBad offset (" << dl.dylib.name << ") for name of "; 584 if (Load.C.cmd == MachO::LC_ID_DYLIB) 585 outs() << "LC_ID_DYLIB "; 586 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB) 587 outs() << "LC_LOAD_DYLIB "; 588 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB) 589 outs() << "LC_LOAD_WEAK_DYLIB "; 590 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB) 591 outs() << "LC_LAZY_LOAD_DYLIB "; 592 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB) 593 outs() << "LC_REEXPORT_DYLIB "; 594 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) 595 outs() << "LC_LOAD_UPWARD_DYLIB "; 596 else 597 outs() << "LC_??? "; 598 outs() << "command " << I << "\n"; 599 } 600 } 601 if (I == LoadCommandCount - 1) 602 break; 603 else 604 Load = O->getNextLoadCommandInfo(Load); 605 } 606 } 607 608 typedef DenseMap<uint64_t, StringRef> SymbolAddressMap; 609 610 static void CreateSymbolAddressMap(MachOObjectFile *O, 611 SymbolAddressMap *AddrMap) { 612 // Create a map of symbol addresses to symbol names. 613 for (const SymbolRef &Symbol : O->symbols()) { 614 SymbolRef::Type ST; 615 Symbol.getType(ST); 616 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data || 617 ST == SymbolRef::ST_Other) { 618 uint64_t Address; 619 Symbol.getAddress(Address); 620 StringRef SymName; 621 Symbol.getName(SymName); 622 if (!SymName.startswith(".objc")) 623 (*AddrMap)[Address] = SymName; 624 } 625 } 626 } 627 628 // GuessSymbolName is passed the address of what might be a symbol and a 629 // pointer to the SymbolAddressMap. It returns the name of a symbol 630 // with that address or nullptr if no symbol is found with that address. 631 static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) { 632 const char *SymbolName = nullptr; 633 // A DenseMap can't lookup up some values. 634 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) { 635 StringRef name = AddrMap->lookup(value); 636 if (!name.empty()) 637 SymbolName = name.data(); 638 } 639 return SymbolName; 640 } 641 642 static void DumpCstringChar(const char c) { 643 char p[2]; 644 p[0] = c; 645 p[1] = '\0'; 646 outs().write_escaped(p); 647 } 648 649 static void DumpCstringSection(MachOObjectFile *O, const char *sect, 650 uint32_t sect_size, uint64_t sect_addr, 651 bool print_addresses) { 652 for (uint32_t i = 0; i < sect_size; i++) { 653 if (print_addresses) { 654 if (O->is64Bit()) 655 outs() << format("%016" PRIx64, sect_addr + i) << " "; 656 else 657 outs() << format("%08" PRIx64, sect_addr + i) << " "; 658 } 659 for (; i < sect_size && sect[i] != '\0'; i++) 660 DumpCstringChar(sect[i]); 661 if (i < sect_size && sect[i] == '\0') 662 outs() << "\n"; 663 } 664 } 665 666 static void DumpLiteral4(uint32_t l, float f) { 667 outs() << format("0x%08" PRIx32, l); 668 if ((l & 0x7f800000) != 0x7f800000) 669 outs() << format(" (%.16e)\n", f); 670 else { 671 if (l == 0x7f800000) 672 outs() << " (+Infinity)\n"; 673 else if (l == 0xff800000) 674 outs() << " (-Infinity)\n"; 675 else if ((l & 0x00400000) == 0x00400000) 676 outs() << " (non-signaling Not-a-Number)\n"; 677 else 678 outs() << " (signaling Not-a-Number)\n"; 679 } 680 } 681 682 static void DumpLiteral4Section(MachOObjectFile *O, const char *sect, 683 uint32_t sect_size, uint64_t sect_addr, 684 bool print_addresses) { 685 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) { 686 if (print_addresses) { 687 if (O->is64Bit()) 688 outs() << format("%016" PRIx64, sect_addr + i) << " "; 689 else 690 outs() << format("%08" PRIx64, sect_addr + i) << " "; 691 } 692 float f; 693 memcpy(&f, sect + i, sizeof(float)); 694 if (O->isLittleEndian() != sys::IsLittleEndianHost) 695 sys::swapByteOrder(f); 696 uint32_t l; 697 memcpy(&l, sect + i, sizeof(uint32_t)); 698 if (O->isLittleEndian() != sys::IsLittleEndianHost) 699 sys::swapByteOrder(l); 700 DumpLiteral4(l, f); 701 } 702 } 703 704 static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1, 705 double d) { 706 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1); 707 uint32_t Hi, Lo; 708 if (O->isLittleEndian()) { 709 Hi = l1; 710 Lo = l0; 711 } else { 712 Hi = l0; 713 Lo = l1; 714 } 715 // Hi is the high word, so this is equivalent to if(isfinite(d)) 716 if ((Hi & 0x7ff00000) != 0x7ff00000) 717 outs() << format(" (%.16e)\n", d); 718 else { 719 if (Hi == 0x7ff00000 && Lo == 0) 720 outs() << " (+Infinity)\n"; 721 else if (Hi == 0xfff00000 && Lo == 0) 722 outs() << " (-Infinity)\n"; 723 else if ((Hi & 0x00080000) == 0x00080000) 724 outs() << " (non-signaling Not-a-Number)\n"; 725 else 726 outs() << " (signaling Not-a-Number)\n"; 727 } 728 } 729 730 static void DumpLiteral8Section(MachOObjectFile *O, const char *sect, 731 uint32_t sect_size, uint64_t sect_addr, 732 bool print_addresses) { 733 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) { 734 if (print_addresses) { 735 if (O->is64Bit()) 736 outs() << format("%016" PRIx64, sect_addr + i) << " "; 737 else 738 outs() << format("%08" PRIx64, sect_addr + i) << " "; 739 } 740 double d; 741 memcpy(&d, sect + i, sizeof(double)); 742 if (O->isLittleEndian() != sys::IsLittleEndianHost) 743 sys::swapByteOrder(d); 744 uint32_t l0, l1; 745 memcpy(&l0, sect + i, sizeof(uint32_t)); 746 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t)); 747 if (O->isLittleEndian() != sys::IsLittleEndianHost) { 748 sys::swapByteOrder(l0); 749 sys::swapByteOrder(l1); 750 } 751 DumpLiteral8(O, l0, l1, d); 752 } 753 } 754 755 static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) { 756 outs() << format("0x%08" PRIx32, l0) << " "; 757 outs() << format("0x%08" PRIx32, l1) << " "; 758 outs() << format("0x%08" PRIx32, l2) << " "; 759 outs() << format("0x%08" PRIx32, l3) << "\n"; 760 } 761 762 static void DumpLiteral16Section(MachOObjectFile *O, const char *sect, 763 uint32_t sect_size, uint64_t sect_addr, 764 bool print_addresses) { 765 for (uint32_t i = 0; i < sect_size; i += 16) { 766 if (print_addresses) { 767 if (O->is64Bit()) 768 outs() << format("%016" PRIx64, sect_addr + i) << " "; 769 else 770 outs() << format("%08" PRIx64, sect_addr + i) << " "; 771 } 772 uint32_t l0, l1, l2, l3; 773 memcpy(&l0, sect + i, sizeof(uint32_t)); 774 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t)); 775 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t)); 776 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t)); 777 if (O->isLittleEndian() != sys::IsLittleEndianHost) { 778 sys::swapByteOrder(l0); 779 sys::swapByteOrder(l1); 780 sys::swapByteOrder(l2); 781 sys::swapByteOrder(l3); 782 } 783 DumpLiteral16(l0, l1, l2, l3); 784 } 785 } 786 787 static void DumpLiteralPointerSection(MachOObjectFile *O, 788 const SectionRef &Section, 789 const char *sect, uint32_t sect_size, 790 uint64_t sect_addr, 791 bool print_addresses) { 792 // Collect the literal sections in this Mach-O file. 793 std::vector<SectionRef> LiteralSections; 794 for (const SectionRef &Section : O->sections()) { 795 DataRefImpl Ref = Section.getRawDataRefImpl(); 796 uint32_t section_type; 797 if (O->is64Bit()) { 798 const MachO::section_64 Sec = O->getSection64(Ref); 799 section_type = Sec.flags & MachO::SECTION_TYPE; 800 } else { 801 const MachO::section Sec = O->getSection(Ref); 802 section_type = Sec.flags & MachO::SECTION_TYPE; 803 } 804 if (section_type == MachO::S_CSTRING_LITERALS || 805 section_type == MachO::S_4BYTE_LITERALS || 806 section_type == MachO::S_8BYTE_LITERALS || 807 section_type == MachO::S_16BYTE_LITERALS) 808 LiteralSections.push_back(Section); 809 } 810 811 // Set the size of the literal pointer. 812 uint32_t lp_size = O->is64Bit() ? 8 : 4; 813 814 // Collect the external relocation symbols for the the literal pointers. 815 std::vector<std::pair<uint64_t, SymbolRef>> Relocs; 816 for (const RelocationRef &Reloc : Section.relocations()) { 817 DataRefImpl Rel; 818 MachO::any_relocation_info RE; 819 bool isExtern = false; 820 Rel = Reloc.getRawDataRefImpl(); 821 RE = O->getRelocation(Rel); 822 isExtern = O->getPlainRelocationExternal(RE); 823 if (isExtern) { 824 uint64_t RelocOffset; 825 Reloc.getOffset(RelocOffset); 826 symbol_iterator RelocSym = Reloc.getSymbol(); 827 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym)); 828 } 829 } 830 array_pod_sort(Relocs.begin(), Relocs.end()); 831 832 // Dump each literal pointer. 833 for (uint32_t i = 0; i < sect_size; i += lp_size) { 834 if (print_addresses) { 835 if (O->is64Bit()) 836 outs() << format("%016" PRIx64, sect_addr + i) << " "; 837 else 838 outs() << format("%08" PRIx64, sect_addr + i) << " "; 839 } 840 uint64_t lp; 841 if (O->is64Bit()) { 842 memcpy(&lp, sect + i, sizeof(uint64_t)); 843 if (O->isLittleEndian() != sys::IsLittleEndianHost) 844 sys::swapByteOrder(lp); 845 } else { 846 uint32_t li; 847 memcpy(&li, sect + i, sizeof(uint32_t)); 848 if (O->isLittleEndian() != sys::IsLittleEndianHost) 849 sys::swapByteOrder(li); 850 lp = li; 851 } 852 853 // First look for an external relocation entry for this literal pointer. 854 auto Reloc = std::find_if( 855 Relocs.begin(), Relocs.end(), 856 [&](const std::pair<uint64_t, SymbolRef> &P) { return P.first == i; }); 857 if (Reloc != Relocs.end()) { 858 symbol_iterator RelocSym = Reloc->second; 859 StringRef SymName; 860 RelocSym->getName(SymName); 861 outs() << "external relocation entry for symbol:" << SymName << "\n"; 862 continue; 863 } 864 865 // For local references see what the section the literal pointer points to. 866 auto Sect = std::find_if(LiteralSections.begin(), LiteralSections.end(), 867 [&](const SectionRef &R) { 868 return lp >= R.getAddress() && 869 lp < R.getAddress() + R.getSize(); 870 }); 871 if (Sect == LiteralSections.end()) { 872 outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n"; 873 continue; 874 } 875 876 uint64_t SectAddress = Sect->getAddress(); 877 uint64_t SectSize = Sect->getSize(); 878 879 StringRef SectName; 880 Sect->getName(SectName); 881 DataRefImpl Ref = Sect->getRawDataRefImpl(); 882 StringRef SegmentName = O->getSectionFinalSegmentName(Ref); 883 outs() << SegmentName << ":" << SectName << ":"; 884 885 uint32_t section_type; 886 if (O->is64Bit()) { 887 const MachO::section_64 Sec = O->getSection64(Ref); 888 section_type = Sec.flags & MachO::SECTION_TYPE; 889 } else { 890 const MachO::section Sec = O->getSection(Ref); 891 section_type = Sec.flags & MachO::SECTION_TYPE; 892 } 893 894 StringRef BytesStr; 895 Sect->getContents(BytesStr); 896 const char *Contents = reinterpret_cast<const char *>(BytesStr.data()); 897 898 switch (section_type) { 899 case MachO::S_CSTRING_LITERALS: 900 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0'; 901 i++) { 902 DumpCstringChar(Contents[i]); 903 } 904 outs() << "\n"; 905 break; 906 case MachO::S_4BYTE_LITERALS: 907 float f; 908 memcpy(&f, Contents + (lp - SectAddress), sizeof(float)); 909 uint32_t l; 910 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t)); 911 if (O->isLittleEndian() != sys::IsLittleEndianHost) { 912 sys::swapByteOrder(f); 913 sys::swapByteOrder(l); 914 } 915 DumpLiteral4(l, f); 916 break; 917 case MachO::S_8BYTE_LITERALS: { 918 double d; 919 memcpy(&d, Contents + (lp - SectAddress), sizeof(double)); 920 uint32_t l0, l1; 921 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t)); 922 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t), 923 sizeof(uint32_t)); 924 if (O->isLittleEndian() != sys::IsLittleEndianHost) { 925 sys::swapByteOrder(f); 926 sys::swapByteOrder(l0); 927 sys::swapByteOrder(l1); 928 } 929 DumpLiteral8(O, l0, l1, d); 930 break; 931 } 932 case MachO::S_16BYTE_LITERALS: { 933 uint32_t l0, l1, l2, l3; 934 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t)); 935 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t), 936 sizeof(uint32_t)); 937 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t), 938 sizeof(uint32_t)); 939 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t), 940 sizeof(uint32_t)); 941 if (O->isLittleEndian() != sys::IsLittleEndianHost) { 942 sys::swapByteOrder(l0); 943 sys::swapByteOrder(l1); 944 sys::swapByteOrder(l2); 945 sys::swapByteOrder(l3); 946 } 947 DumpLiteral16(l0, l1, l2, l3); 948 break; 949 } 950 } 951 } 952 } 953 954 static void DumpInitTermPointerSection(MachOObjectFile *O, const char *sect, 955 uint32_t sect_size, uint64_t sect_addr, 956 SymbolAddressMap *AddrMap, 957 bool verbose) { 958 uint32_t stride; 959 if (O->is64Bit()) 960 stride = sizeof(uint64_t); 961 else 962 stride = sizeof(uint32_t); 963 for (uint32_t i = 0; i < sect_size; i += stride) { 964 const char *SymbolName = nullptr; 965 if (O->is64Bit()) { 966 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " "; 967 uint64_t pointer_value; 968 memcpy(&pointer_value, sect + i, stride); 969 if (O->isLittleEndian() != sys::IsLittleEndianHost) 970 sys::swapByteOrder(pointer_value); 971 outs() << format("0x%016" PRIx64, pointer_value); 972 if (verbose) 973 SymbolName = GuessSymbolName(pointer_value, AddrMap); 974 } else { 975 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " "; 976 uint32_t pointer_value; 977 memcpy(&pointer_value, sect + i, stride); 978 if (O->isLittleEndian() != sys::IsLittleEndianHost) 979 sys::swapByteOrder(pointer_value); 980 outs() << format("0x%08" PRIx32, pointer_value); 981 if (verbose) 982 SymbolName = GuessSymbolName(pointer_value, AddrMap); 983 } 984 if (SymbolName) 985 outs() << " " << SymbolName; 986 outs() << "\n"; 987 } 988 } 989 990 static void DumpRawSectionContents(MachOObjectFile *O, const char *sect, 991 uint32_t size, uint64_t addr) { 992 uint32_t cputype = O->getHeader().cputype; 993 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) { 994 uint32_t j; 995 for (uint32_t i = 0; i < size; i += j, addr += j) { 996 if (O->is64Bit()) 997 outs() << format("%016" PRIx64, addr) << "\t"; 998 else 999 outs() << format("%08" PRIx64, addr) << "\t"; 1000 for (j = 0; j < 16 && i + j < size; j++) { 1001 uint8_t byte_word = *(sect + i + j); 1002 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " "; 1003 } 1004 outs() << "\n"; 1005 } 1006 } else { 1007 uint32_t j; 1008 for (uint32_t i = 0; i < size; i += j, addr += j) { 1009 if (O->is64Bit()) 1010 outs() << format("%016" PRIx64, addr) << "\t"; 1011 else 1012 outs() << format("%08" PRIx64, sect) << "\t"; 1013 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size; 1014 j += sizeof(int32_t)) { 1015 if (i + j + sizeof(int32_t) < size) { 1016 uint32_t long_word; 1017 memcpy(&long_word, sect + i + j, sizeof(int32_t)); 1018 if (O->isLittleEndian() != sys::IsLittleEndianHost) 1019 sys::swapByteOrder(long_word); 1020 outs() << format("%08" PRIx32, long_word) << " "; 1021 } else { 1022 for (uint32_t k = 0; i + j + k < size; k++) { 1023 uint8_t byte_word = *(sect + i + j); 1024 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " "; 1025 } 1026 } 1027 } 1028 outs() << "\n"; 1029 } 1030 } 1031 } 1032 1033 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, 1034 StringRef DisSegName, StringRef DisSectName); 1035 static void DumpProtocolSection(MachOObjectFile *O, const char *sect, 1036 uint32_t size, uint32_t addr); 1037 1038 static void DumpSectionContents(StringRef Filename, MachOObjectFile *O, 1039 bool verbose) { 1040 SymbolAddressMap AddrMap; 1041 if (verbose) 1042 CreateSymbolAddressMap(O, &AddrMap); 1043 1044 for (unsigned i = 0; i < DumpSections.size(); ++i) { 1045 StringRef DumpSection = DumpSections[i]; 1046 std::pair<StringRef, StringRef> DumpSegSectName; 1047 DumpSegSectName = DumpSection.split(','); 1048 StringRef DumpSegName, DumpSectName; 1049 if (DumpSegSectName.second.size()) { 1050 DumpSegName = DumpSegSectName.first; 1051 DumpSectName = DumpSegSectName.second; 1052 } else { 1053 DumpSegName = ""; 1054 DumpSectName = DumpSegSectName.first; 1055 } 1056 for (const SectionRef &Section : O->sections()) { 1057 StringRef SectName; 1058 Section.getName(SectName); 1059 DataRefImpl Ref = Section.getRawDataRefImpl(); 1060 StringRef SegName = O->getSectionFinalSegmentName(Ref); 1061 if ((DumpSegName.empty() || SegName == DumpSegName) && 1062 (SectName == DumpSectName)) { 1063 1064 uint32_t section_flags; 1065 if (O->is64Bit()) { 1066 const MachO::section_64 Sec = O->getSection64(Ref); 1067 section_flags = Sec.flags; 1068 1069 } else { 1070 const MachO::section Sec = O->getSection(Ref); 1071 section_flags = Sec.flags; 1072 } 1073 uint32_t section_type = section_flags & MachO::SECTION_TYPE; 1074 1075 StringRef BytesStr; 1076 Section.getContents(BytesStr); 1077 const char *sect = reinterpret_cast<const char *>(BytesStr.data()); 1078 uint32_t sect_size = BytesStr.size(); 1079 uint64_t sect_addr = Section.getAddress(); 1080 1081 if (Raw) { 1082 outs().write(BytesStr.data(), BytesStr.size()); 1083 continue; 1084 } 1085 1086 outs() << "Contents of (" << SegName << "," << SectName 1087 << ") section\n"; 1088 1089 if (verbose) { 1090 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) || 1091 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) { 1092 DisassembleMachO(Filename, O, SegName, SectName); 1093 continue; 1094 } 1095 if (SegName == "__TEXT" && SectName == "__info_plist") { 1096 outs() << sect; 1097 continue; 1098 } 1099 if (SegName == "__OBJC" && SectName == "__protocol") { 1100 DumpProtocolSection(O, sect, sect_size, sect_addr); 1101 continue; 1102 } 1103 switch (section_type) { 1104 case MachO::S_REGULAR: 1105 DumpRawSectionContents(O, sect, sect_size, sect_addr); 1106 break; 1107 case MachO::S_ZEROFILL: 1108 outs() << "zerofill section and has no contents in the file\n"; 1109 break; 1110 case MachO::S_CSTRING_LITERALS: 1111 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr); 1112 break; 1113 case MachO::S_4BYTE_LITERALS: 1114 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr); 1115 break; 1116 case MachO::S_8BYTE_LITERALS: 1117 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr); 1118 break; 1119 case MachO::S_16BYTE_LITERALS: 1120 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr); 1121 break; 1122 case MachO::S_LITERAL_POINTERS: 1123 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr, 1124 !NoLeadingAddr); 1125 break; 1126 case MachO::S_MOD_INIT_FUNC_POINTERS: 1127 case MachO::S_MOD_TERM_FUNC_POINTERS: 1128 DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap, 1129 verbose); 1130 break; 1131 default: 1132 outs() << "Unknown section type (" 1133 << format("0x%08" PRIx32, section_type) << ")\n"; 1134 DumpRawSectionContents(O, sect, sect_size, sect_addr); 1135 break; 1136 } 1137 } else { 1138 if (section_type == MachO::S_ZEROFILL) 1139 outs() << "zerofill section and has no contents in the file\n"; 1140 else 1141 DumpRawSectionContents(O, sect, sect_size, sect_addr); 1142 } 1143 } 1144 } 1145 } 1146 } 1147 1148 static void DumpInfoPlistSectionContents(StringRef Filename, 1149 MachOObjectFile *O) { 1150 for (const SectionRef &Section : O->sections()) { 1151 StringRef SectName; 1152 Section.getName(SectName); 1153 DataRefImpl Ref = Section.getRawDataRefImpl(); 1154 StringRef SegName = O->getSectionFinalSegmentName(Ref); 1155 if (SegName == "__TEXT" && SectName == "__info_plist") { 1156 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 1157 StringRef BytesStr; 1158 Section.getContents(BytesStr); 1159 const char *sect = reinterpret_cast<const char *>(BytesStr.data()); 1160 outs() << sect; 1161 return; 1162 } 1163 } 1164 } 1165 1166 // checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file 1167 // and if it is and there is a list of architecture flags is specified then 1168 // check to make sure this Mach-O file is one of those architectures or all 1169 // architectures were specified. If not then an error is generated and this 1170 // routine returns false. Else it returns true. 1171 static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) { 1172 if (isa<MachOObjectFile>(O) && !ArchAll && ArchFlags.size() != 0) { 1173 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O); 1174 bool ArchFound = false; 1175 MachO::mach_header H; 1176 MachO::mach_header_64 H_64; 1177 Triple T; 1178 if (MachO->is64Bit()) { 1179 H_64 = MachO->MachOObjectFile::getHeader64(); 1180 T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype); 1181 } else { 1182 H = MachO->MachOObjectFile::getHeader(); 1183 T = MachOObjectFile::getArch(H.cputype, H.cpusubtype); 1184 } 1185 unsigned i; 1186 for (i = 0; i < ArchFlags.size(); ++i) { 1187 if (ArchFlags[i] == T.getArchName()) 1188 ArchFound = true; 1189 break; 1190 } 1191 if (!ArchFound) { 1192 errs() << "llvm-objdump: file: " + Filename + " does not contain " 1193 << "architecture: " + ArchFlags[i] + "\n"; 1194 return false; 1195 } 1196 } 1197 return true; 1198 } 1199 1200 static void printObjcMetaData(MachOObjectFile *O, bool verbose); 1201 1202 // ProcessMachO() is passed a single opened Mach-O file, which may be an 1203 // archive member and or in a slice of a universal file. It prints the 1204 // the file name and header info and then processes it according to the 1205 // command line options. 1206 static void ProcessMachO(StringRef Filename, MachOObjectFile *MachOOF, 1207 StringRef ArchiveMemberName = StringRef(), 1208 StringRef ArchitectureName = StringRef()) { 1209 // If we are doing some processing here on the Mach-O file print the header 1210 // info. And don't print it otherwise like in the case of printing the 1211 // UniversalHeaders or ArchiveHeaders. 1212 if (Disassemble || PrivateHeaders || ExportsTrie || Rebase || Bind || 1213 LazyBind || WeakBind || IndirectSymbols || DataInCode || LinkOptHints || 1214 DylibsUsed || DylibId || ObjcMetaData || 1215 (DumpSections.size() != 0 && !Raw)) { 1216 outs() << Filename; 1217 if (!ArchiveMemberName.empty()) 1218 outs() << '(' << ArchiveMemberName << ')'; 1219 if (!ArchitectureName.empty()) 1220 outs() << " (architecture " << ArchitectureName << ")"; 1221 outs() << ":\n"; 1222 } 1223 1224 if (Disassemble) 1225 DisassembleMachO(Filename, MachOOF, "__TEXT", "__text"); 1226 if (IndirectSymbols) 1227 PrintIndirectSymbols(MachOOF, !NonVerbose); 1228 if (DataInCode) 1229 PrintDataInCodeTable(MachOOF, !NonVerbose); 1230 if (LinkOptHints) 1231 PrintLinkOptHints(MachOOF); 1232 if (Relocations) 1233 PrintRelocations(MachOOF); 1234 if (SectionHeaders) 1235 PrintSectionHeaders(MachOOF); 1236 if (SectionContents) 1237 PrintSectionContents(MachOOF); 1238 if (DumpSections.size() != 0) 1239 DumpSectionContents(Filename, MachOOF, !NonVerbose); 1240 if (InfoPlist) 1241 DumpInfoPlistSectionContents(Filename, MachOOF); 1242 if (DylibsUsed) 1243 PrintDylibs(MachOOF, false); 1244 if (DylibId) 1245 PrintDylibs(MachOOF, true); 1246 if (SymbolTable) 1247 PrintSymbolTable(MachOOF); 1248 if (UnwindInfo) 1249 printMachOUnwindInfo(MachOOF); 1250 if (PrivateHeaders) 1251 printMachOFileHeader(MachOOF); 1252 if (ObjcMetaData) 1253 printObjcMetaData(MachOOF, !NonVerbose); 1254 if (ExportsTrie) 1255 printExportsTrie(MachOOF); 1256 if (Rebase) 1257 printRebaseTable(MachOOF); 1258 if (Bind) 1259 printBindTable(MachOOF); 1260 if (LazyBind) 1261 printLazyBindTable(MachOOF); 1262 if (WeakBind) 1263 printWeakBindTable(MachOOF); 1264 } 1265 1266 // printUnknownCPUType() helps print_fat_headers for unknown CPU's. 1267 static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) { 1268 outs() << " cputype (" << cputype << ")\n"; 1269 outs() << " cpusubtype (" << cpusubtype << ")\n"; 1270 } 1271 1272 // printCPUType() helps print_fat_headers by printing the cputype and 1273 // pusubtype (symbolically for the one's it knows about). 1274 static void printCPUType(uint32_t cputype, uint32_t cpusubtype) { 1275 switch (cputype) { 1276 case MachO::CPU_TYPE_I386: 1277 switch (cpusubtype) { 1278 case MachO::CPU_SUBTYPE_I386_ALL: 1279 outs() << " cputype CPU_TYPE_I386\n"; 1280 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n"; 1281 break; 1282 default: 1283 printUnknownCPUType(cputype, cpusubtype); 1284 break; 1285 } 1286 break; 1287 case MachO::CPU_TYPE_X86_64: 1288 switch (cpusubtype) { 1289 case MachO::CPU_SUBTYPE_X86_64_ALL: 1290 outs() << " cputype CPU_TYPE_X86_64\n"; 1291 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n"; 1292 break; 1293 case MachO::CPU_SUBTYPE_X86_64_H: 1294 outs() << " cputype CPU_TYPE_X86_64\n"; 1295 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n"; 1296 break; 1297 default: 1298 printUnknownCPUType(cputype, cpusubtype); 1299 break; 1300 } 1301 break; 1302 case MachO::CPU_TYPE_ARM: 1303 switch (cpusubtype) { 1304 case MachO::CPU_SUBTYPE_ARM_ALL: 1305 outs() << " cputype CPU_TYPE_ARM\n"; 1306 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n"; 1307 break; 1308 case MachO::CPU_SUBTYPE_ARM_V4T: 1309 outs() << " cputype CPU_TYPE_ARM\n"; 1310 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n"; 1311 break; 1312 case MachO::CPU_SUBTYPE_ARM_V5TEJ: 1313 outs() << " cputype CPU_TYPE_ARM\n"; 1314 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n"; 1315 break; 1316 case MachO::CPU_SUBTYPE_ARM_XSCALE: 1317 outs() << " cputype CPU_TYPE_ARM\n"; 1318 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n"; 1319 break; 1320 case MachO::CPU_SUBTYPE_ARM_V6: 1321 outs() << " cputype CPU_TYPE_ARM\n"; 1322 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n"; 1323 break; 1324 case MachO::CPU_SUBTYPE_ARM_V6M: 1325 outs() << " cputype CPU_TYPE_ARM\n"; 1326 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n"; 1327 break; 1328 case MachO::CPU_SUBTYPE_ARM_V7: 1329 outs() << " cputype CPU_TYPE_ARM\n"; 1330 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n"; 1331 break; 1332 case MachO::CPU_SUBTYPE_ARM_V7EM: 1333 outs() << " cputype CPU_TYPE_ARM\n"; 1334 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n"; 1335 break; 1336 case MachO::CPU_SUBTYPE_ARM_V7K: 1337 outs() << " cputype CPU_TYPE_ARM\n"; 1338 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n"; 1339 break; 1340 case MachO::CPU_SUBTYPE_ARM_V7M: 1341 outs() << " cputype CPU_TYPE_ARM\n"; 1342 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n"; 1343 break; 1344 case MachO::CPU_SUBTYPE_ARM_V7S: 1345 outs() << " cputype CPU_TYPE_ARM\n"; 1346 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n"; 1347 break; 1348 default: 1349 printUnknownCPUType(cputype, cpusubtype); 1350 break; 1351 } 1352 break; 1353 case MachO::CPU_TYPE_ARM64: 1354 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 1355 case MachO::CPU_SUBTYPE_ARM64_ALL: 1356 outs() << " cputype CPU_TYPE_ARM64\n"; 1357 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n"; 1358 break; 1359 default: 1360 printUnknownCPUType(cputype, cpusubtype); 1361 break; 1362 } 1363 break; 1364 default: 1365 printUnknownCPUType(cputype, cpusubtype); 1366 break; 1367 } 1368 } 1369 1370 static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB, 1371 bool verbose) { 1372 outs() << "Fat headers\n"; 1373 if (verbose) 1374 outs() << "fat_magic FAT_MAGIC\n"; 1375 else 1376 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n"; 1377 1378 uint32_t nfat_arch = UB->getNumberOfObjects(); 1379 StringRef Buf = UB->getData(); 1380 uint64_t size = Buf.size(); 1381 uint64_t big_size = sizeof(struct MachO::fat_header) + 1382 nfat_arch * sizeof(struct MachO::fat_arch); 1383 outs() << "nfat_arch " << UB->getNumberOfObjects(); 1384 if (nfat_arch == 0) 1385 outs() << " (malformed, contains zero architecture types)\n"; 1386 else if (big_size > size) 1387 outs() << " (malformed, architectures past end of file)\n"; 1388 else 1389 outs() << "\n"; 1390 1391 for (uint32_t i = 0; i < nfat_arch; ++i) { 1392 MachOUniversalBinary::ObjectForArch OFA(UB, i); 1393 uint32_t cputype = OFA.getCPUType(); 1394 uint32_t cpusubtype = OFA.getCPUSubType(); 1395 outs() << "architecture "; 1396 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) { 1397 MachOUniversalBinary::ObjectForArch other_OFA(UB, j); 1398 uint32_t other_cputype = other_OFA.getCPUType(); 1399 uint32_t other_cpusubtype = other_OFA.getCPUSubType(); 1400 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype && 1401 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) == 1402 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) { 1403 outs() << "(illegal duplicate architecture) "; 1404 break; 1405 } 1406 } 1407 if (verbose) { 1408 outs() << OFA.getArchTypeName() << "\n"; 1409 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 1410 } else { 1411 outs() << i << "\n"; 1412 outs() << " cputype " << cputype << "\n"; 1413 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) 1414 << "\n"; 1415 } 1416 if (verbose && 1417 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) 1418 outs() << " capabilities CPU_SUBTYPE_LIB64\n"; 1419 else 1420 outs() << " capabilities " 1421 << format("0x%" PRIx32, 1422 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n"; 1423 outs() << " offset " << OFA.getOffset(); 1424 if (OFA.getOffset() > size) 1425 outs() << " (past end of file)"; 1426 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0) 1427 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")"; 1428 outs() << "\n"; 1429 outs() << " size " << OFA.getSize(); 1430 big_size = OFA.getOffset() + OFA.getSize(); 1431 if (big_size > size) 1432 outs() << " (past end of file)"; 1433 outs() << "\n"; 1434 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign()) 1435 << ")\n"; 1436 } 1437 } 1438 1439 static void printArchiveChild(Archive::Child &C, bool verbose, 1440 bool print_offset) { 1441 if (print_offset) 1442 outs() << C.getChildOffset() << "\t"; 1443 sys::fs::perms Mode = C.getAccessMode(); 1444 if (verbose) { 1445 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG. 1446 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG. 1447 outs() << "-"; 1448 if (Mode & sys::fs::owner_read) 1449 outs() << "r"; 1450 else 1451 outs() << "-"; 1452 if (Mode & sys::fs::owner_write) 1453 outs() << "w"; 1454 else 1455 outs() << "-"; 1456 if (Mode & sys::fs::owner_exe) 1457 outs() << "x"; 1458 else 1459 outs() << "-"; 1460 if (Mode & sys::fs::group_read) 1461 outs() << "r"; 1462 else 1463 outs() << "-"; 1464 if (Mode & sys::fs::group_write) 1465 outs() << "w"; 1466 else 1467 outs() << "-"; 1468 if (Mode & sys::fs::group_exe) 1469 outs() << "x"; 1470 else 1471 outs() << "-"; 1472 if (Mode & sys::fs::others_read) 1473 outs() << "r"; 1474 else 1475 outs() << "-"; 1476 if (Mode & sys::fs::others_write) 1477 outs() << "w"; 1478 else 1479 outs() << "-"; 1480 if (Mode & sys::fs::others_exe) 1481 outs() << "x"; 1482 else 1483 outs() << "-"; 1484 } else { 1485 outs() << format("0%o ", Mode); 1486 } 1487 1488 unsigned UID = C.getUID(); 1489 outs() << format("%3d/", UID); 1490 unsigned GID = C.getGID(); 1491 outs() << format("%-3d ", GID); 1492 uint64_t Size = C.getRawSize(); 1493 outs() << format("%5" PRId64, Size) << " "; 1494 1495 StringRef RawLastModified = C.getRawLastModified(); 1496 if (verbose) { 1497 unsigned Seconds; 1498 if (RawLastModified.getAsInteger(10, Seconds)) 1499 outs() << "(date: \"%s\" contains non-decimal chars) " << RawLastModified; 1500 else { 1501 // Since cime(3) returns a 26 character string of the form: 1502 // "Sun Sep 16 01:03:52 1973\n\0" 1503 // just print 24 characters. 1504 time_t t = Seconds; 1505 outs() << format("%.24s ", ctime(&t)); 1506 } 1507 } else { 1508 outs() << RawLastModified << " "; 1509 } 1510 1511 if (verbose) { 1512 ErrorOr<StringRef> NameOrErr = C.getName(); 1513 if (NameOrErr.getError()) { 1514 StringRef RawName = C.getRawName(); 1515 outs() << RawName << "\n"; 1516 } else { 1517 StringRef Name = NameOrErr.get(); 1518 outs() << Name << "\n"; 1519 } 1520 } else { 1521 StringRef RawName = C.getRawName(); 1522 outs() << RawName << "\n"; 1523 } 1524 } 1525 1526 static void printArchiveHeaders(Archive *A, bool verbose, bool print_offset) { 1527 if (A->hasSymbolTable()) { 1528 Archive::child_iterator S = A->getSymbolTableChild(); 1529 Archive::Child C = *S; 1530 printArchiveChild(C, verbose, print_offset); 1531 } 1532 for (Archive::child_iterator I = A->child_begin(), E = A->child_end(); I != E; 1533 ++I) { 1534 Archive::Child C = *I; 1535 printArchiveChild(C, verbose, print_offset); 1536 } 1537 } 1538 1539 // ParseInputMachO() parses the named Mach-O file in Filename and handles the 1540 // -arch flags selecting just those slices as specified by them and also parses 1541 // archive files. Then for each individual Mach-O file ProcessMachO() is 1542 // called to process the file based on the command line options. 1543 void llvm::ParseInputMachO(StringRef Filename) { 1544 // Check for -arch all and verifiy the -arch flags are valid. 1545 for (unsigned i = 0; i < ArchFlags.size(); ++i) { 1546 if (ArchFlags[i] == "all") { 1547 ArchAll = true; 1548 } else { 1549 if (!MachOObjectFile::isValidArch(ArchFlags[i])) { 1550 errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] + 1551 "'for the -arch option\n"; 1552 return; 1553 } 1554 } 1555 } 1556 1557 // Attempt to open the binary. 1558 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename); 1559 if (std::error_code EC = BinaryOrErr.getError()) { 1560 errs() << "llvm-objdump: '" << Filename << "': " << EC.message() << ".\n"; 1561 return; 1562 } 1563 Binary &Bin = *BinaryOrErr.get().getBinary(); 1564 1565 if (Archive *A = dyn_cast<Archive>(&Bin)) { 1566 outs() << "Archive : " << Filename << "\n"; 1567 if (ArchiveHeaders) 1568 printArchiveHeaders(A, !NonVerbose, ArchiveMemberOffsets); 1569 for (Archive::child_iterator I = A->child_begin(), E = A->child_end(); 1570 I != E; ++I) { 1571 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = I->getAsBinary(); 1572 if (ChildOrErr.getError()) 1573 continue; 1574 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) { 1575 if (!checkMachOAndArchFlags(O, Filename)) 1576 return; 1577 ProcessMachO(Filename, O, O->getFileName()); 1578 } 1579 } 1580 return; 1581 } 1582 if (UniversalHeaders) { 1583 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) 1584 printMachOUniversalHeaders(UB, !NonVerbose); 1585 } 1586 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) { 1587 // If we have a list of architecture flags specified dump only those. 1588 if (!ArchAll && ArchFlags.size() != 0) { 1589 // Look for a slice in the universal binary that matches each ArchFlag. 1590 bool ArchFound; 1591 for (unsigned i = 0; i < ArchFlags.size(); ++i) { 1592 ArchFound = false; 1593 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(), 1594 E = UB->end_objects(); 1595 I != E; ++I) { 1596 if (ArchFlags[i] == I->getArchTypeName()) { 1597 ArchFound = true; 1598 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = 1599 I->getAsObjectFile(); 1600 std::string ArchitectureName = ""; 1601 if (ArchFlags.size() > 1) 1602 ArchitectureName = I->getArchTypeName(); 1603 if (ObjOrErr) { 1604 ObjectFile &O = *ObjOrErr.get(); 1605 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O)) 1606 ProcessMachO(Filename, MachOOF, "", ArchitectureName); 1607 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = 1608 I->getAsArchive()) { 1609 std::unique_ptr<Archive> &A = *AOrErr; 1610 outs() << "Archive : " << Filename; 1611 if (!ArchitectureName.empty()) 1612 outs() << " (architecture " << ArchitectureName << ")"; 1613 outs() << "\n"; 1614 if (ArchiveHeaders) 1615 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets); 1616 for (Archive::child_iterator AI = A->child_begin(), 1617 AE = A->child_end(); 1618 AI != AE; ++AI) { 1619 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary(); 1620 if (ChildOrErr.getError()) 1621 continue; 1622 if (MachOObjectFile *O = 1623 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) 1624 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName); 1625 } 1626 } 1627 } 1628 } 1629 if (!ArchFound) { 1630 errs() << "llvm-objdump: file: " + Filename + " does not contain " 1631 << "architecture: " + ArchFlags[i] + "\n"; 1632 return; 1633 } 1634 } 1635 return; 1636 } 1637 // No architecture flags were specified so if this contains a slice that 1638 // matches the host architecture dump only that. 1639 if (!ArchAll) { 1640 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(), 1641 E = UB->end_objects(); 1642 I != E; ++I) { 1643 if (MachOObjectFile::getHostArch().getArchName() == 1644 I->getArchTypeName()) { 1645 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile(); 1646 std::string ArchiveName; 1647 ArchiveName.clear(); 1648 if (ObjOrErr) { 1649 ObjectFile &O = *ObjOrErr.get(); 1650 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O)) 1651 ProcessMachO(Filename, MachOOF); 1652 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = 1653 I->getAsArchive()) { 1654 std::unique_ptr<Archive> &A = *AOrErr; 1655 outs() << "Archive : " << Filename << "\n"; 1656 if (ArchiveHeaders) 1657 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets); 1658 for (Archive::child_iterator AI = A->child_begin(), 1659 AE = A->child_end(); 1660 AI != AE; ++AI) { 1661 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary(); 1662 if (ChildOrErr.getError()) 1663 continue; 1664 if (MachOObjectFile *O = 1665 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) 1666 ProcessMachO(Filename, O, O->getFileName()); 1667 } 1668 } 1669 return; 1670 } 1671 } 1672 } 1673 // Either all architectures have been specified or none have been specified 1674 // and this does not contain the host architecture so dump all the slices. 1675 bool moreThanOneArch = UB->getNumberOfObjects() > 1; 1676 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(), 1677 E = UB->end_objects(); 1678 I != E; ++I) { 1679 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile(); 1680 std::string ArchitectureName = ""; 1681 if (moreThanOneArch) 1682 ArchitectureName = I->getArchTypeName(); 1683 if (ObjOrErr) { 1684 ObjectFile &Obj = *ObjOrErr.get(); 1685 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj)) 1686 ProcessMachO(Filename, MachOOF, "", ArchitectureName); 1687 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) { 1688 std::unique_ptr<Archive> &A = *AOrErr; 1689 outs() << "Archive : " << Filename; 1690 if (!ArchitectureName.empty()) 1691 outs() << " (architecture " << ArchitectureName << ")"; 1692 outs() << "\n"; 1693 if (ArchiveHeaders) 1694 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets); 1695 for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end(); 1696 AI != AE; ++AI) { 1697 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary(); 1698 if (ChildOrErr.getError()) 1699 continue; 1700 if (MachOObjectFile *O = 1701 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) { 1702 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O)) 1703 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(), 1704 ArchitectureName); 1705 } 1706 } 1707 } 1708 } 1709 return; 1710 } 1711 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) { 1712 if (!checkMachOAndArchFlags(O, Filename)) 1713 return; 1714 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) { 1715 ProcessMachO(Filename, MachOOF); 1716 } else 1717 errs() << "llvm-objdump: '" << Filename << "': " 1718 << "Object is not a Mach-O file type.\n"; 1719 } else 1720 errs() << "llvm-objdump: '" << Filename << "': " 1721 << "Unrecognized file type.\n"; 1722 } 1723 1724 typedef std::pair<uint64_t, const char *> BindInfoEntry; 1725 typedef std::vector<BindInfoEntry> BindTable; 1726 typedef BindTable::iterator bind_table_iterator; 1727 1728 // The block of info used by the Symbolizer call backs. 1729 struct DisassembleInfo { 1730 bool verbose; 1731 MachOObjectFile *O; 1732 SectionRef S; 1733 SymbolAddressMap *AddrMap; 1734 std::vector<SectionRef> *Sections; 1735 const char *class_name; 1736 const char *selector_name; 1737 char *method; 1738 char *demangled_name; 1739 uint64_t adrp_addr; 1740 uint32_t adrp_inst; 1741 BindTable *bindtable; 1742 }; 1743 1744 // SymbolizerGetOpInfo() is the operand information call back function. 1745 // This is called to get the symbolic information for operand(s) of an 1746 // instruction when it is being done. This routine does this from 1747 // the relocation information, symbol table, etc. That block of information 1748 // is a pointer to the struct DisassembleInfo that was passed when the 1749 // disassembler context was created and passed to back to here when 1750 // called back by the disassembler for instruction operands that could have 1751 // relocation information. The address of the instruction containing operand is 1752 // at the Pc parameter. The immediate value the operand has is passed in 1753 // op_info->Value and is at Offset past the start of the instruction and has a 1754 // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the 1755 // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol 1756 // names and addends of the symbolic expression to add for the operand. The 1757 // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic 1758 // information is returned then this function returns 1 else it returns 0. 1759 static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset, 1760 uint64_t Size, int TagType, void *TagBuf) { 1761 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo; 1762 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf; 1763 uint64_t value = op_info->Value; 1764 1765 // Make sure all fields returned are zero if we don't set them. 1766 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1)); 1767 op_info->Value = value; 1768 1769 // If the TagType is not the value 1 which it code knows about or if no 1770 // verbose symbolic information is wanted then just return 0, indicating no 1771 // information is being returned. 1772 if (TagType != 1 || !info->verbose) 1773 return 0; 1774 1775 unsigned int Arch = info->O->getArch(); 1776 if (Arch == Triple::x86) { 1777 if (Size != 1 && Size != 2 && Size != 4 && Size != 0) 1778 return 0; 1779 // First search the section's relocation entries (if any) for an entry 1780 // for this section offset. 1781 uint32_t sect_addr = info->S.getAddress(); 1782 uint32_t sect_offset = (Pc + Offset) - sect_addr; 1783 bool reloc_found = false; 1784 DataRefImpl Rel; 1785 MachO::any_relocation_info RE; 1786 bool isExtern = false; 1787 SymbolRef Symbol; 1788 bool r_scattered = false; 1789 uint32_t r_value, pair_r_value, r_type; 1790 for (const RelocationRef &Reloc : info->S.relocations()) { 1791 uint64_t RelocOffset; 1792 Reloc.getOffset(RelocOffset); 1793 if (RelocOffset == sect_offset) { 1794 Rel = Reloc.getRawDataRefImpl(); 1795 RE = info->O->getRelocation(Rel); 1796 r_type = info->O->getAnyRelocationType(RE); 1797 r_scattered = info->O->isRelocationScattered(RE); 1798 if (r_scattered) { 1799 r_value = info->O->getScatteredRelocationValue(RE); 1800 if (r_type == MachO::GENERIC_RELOC_SECTDIFF || 1801 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) { 1802 DataRefImpl RelNext = Rel; 1803 info->O->moveRelocationNext(RelNext); 1804 MachO::any_relocation_info RENext; 1805 RENext = info->O->getRelocation(RelNext); 1806 if (info->O->isRelocationScattered(RENext)) 1807 pair_r_value = info->O->getScatteredRelocationValue(RENext); 1808 else 1809 return 0; 1810 } 1811 } else { 1812 isExtern = info->O->getPlainRelocationExternal(RE); 1813 if (isExtern) { 1814 symbol_iterator RelocSym = Reloc.getSymbol(); 1815 Symbol = *RelocSym; 1816 } 1817 } 1818 reloc_found = true; 1819 break; 1820 } 1821 } 1822 if (reloc_found && isExtern) { 1823 StringRef SymName; 1824 Symbol.getName(SymName); 1825 const char *name = SymName.data(); 1826 op_info->AddSymbol.Present = 1; 1827 op_info->AddSymbol.Name = name; 1828 // For i386 extern relocation entries the value in the instruction is 1829 // the offset from the symbol, and value is already set in op_info->Value. 1830 return 1; 1831 } 1832 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF || 1833 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) { 1834 const char *add = GuessSymbolName(r_value, info->AddrMap); 1835 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap); 1836 uint32_t offset = value - (r_value - pair_r_value); 1837 op_info->AddSymbol.Present = 1; 1838 if (add != nullptr) 1839 op_info->AddSymbol.Name = add; 1840 else 1841 op_info->AddSymbol.Value = r_value; 1842 op_info->SubtractSymbol.Present = 1; 1843 if (sub != nullptr) 1844 op_info->SubtractSymbol.Name = sub; 1845 else 1846 op_info->SubtractSymbol.Value = pair_r_value; 1847 op_info->Value = offset; 1848 return 1; 1849 } 1850 // TODO: 1851 // Second search the external relocation entries of a fully linked image 1852 // (if any) for an entry that matches this segment offset. 1853 // uint32_t seg_offset = (Pc + Offset); 1854 return 0; 1855 } 1856 if (Arch == Triple::x86_64) { 1857 if (Size != 1 && Size != 2 && Size != 4 && Size != 0) 1858 return 0; 1859 // First search the section's relocation entries (if any) for an entry 1860 // for this section offset. 1861 uint64_t sect_addr = info->S.getAddress(); 1862 uint64_t sect_offset = (Pc + Offset) - sect_addr; 1863 bool reloc_found = false; 1864 DataRefImpl Rel; 1865 MachO::any_relocation_info RE; 1866 bool isExtern = false; 1867 SymbolRef Symbol; 1868 for (const RelocationRef &Reloc : info->S.relocations()) { 1869 uint64_t RelocOffset; 1870 Reloc.getOffset(RelocOffset); 1871 if (RelocOffset == sect_offset) { 1872 Rel = Reloc.getRawDataRefImpl(); 1873 RE = info->O->getRelocation(Rel); 1874 // NOTE: Scattered relocations don't exist on x86_64. 1875 isExtern = info->O->getPlainRelocationExternal(RE); 1876 if (isExtern) { 1877 symbol_iterator RelocSym = Reloc.getSymbol(); 1878 Symbol = *RelocSym; 1879 } 1880 reloc_found = true; 1881 break; 1882 } 1883 } 1884 if (reloc_found && isExtern) { 1885 // The Value passed in will be adjusted by the Pc if the instruction 1886 // adds the Pc. But for x86_64 external relocation entries the Value 1887 // is the offset from the external symbol. 1888 if (info->O->getAnyRelocationPCRel(RE)) 1889 op_info->Value -= Pc + Offset + Size; 1890 StringRef SymName; 1891 Symbol.getName(SymName); 1892 const char *name = SymName.data(); 1893 unsigned Type = info->O->getAnyRelocationType(RE); 1894 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) { 1895 DataRefImpl RelNext = Rel; 1896 info->O->moveRelocationNext(RelNext); 1897 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext); 1898 unsigned TypeNext = info->O->getAnyRelocationType(RENext); 1899 bool isExternNext = info->O->getPlainRelocationExternal(RENext); 1900 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext); 1901 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) { 1902 op_info->SubtractSymbol.Present = 1; 1903 op_info->SubtractSymbol.Name = name; 1904 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum); 1905 Symbol = *RelocSymNext; 1906 StringRef SymNameNext; 1907 Symbol.getName(SymNameNext); 1908 name = SymNameNext.data(); 1909 } 1910 } 1911 // TODO: add the VariantKinds to op_info->VariantKind for relocation types 1912 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT. 1913 op_info->AddSymbol.Present = 1; 1914 op_info->AddSymbol.Name = name; 1915 return 1; 1916 } 1917 // TODO: 1918 // Second search the external relocation entries of a fully linked image 1919 // (if any) for an entry that matches this segment offset. 1920 // uint64_t seg_offset = (Pc + Offset); 1921 return 0; 1922 } 1923 if (Arch == Triple::arm) { 1924 if (Offset != 0 || (Size != 4 && Size != 2)) 1925 return 0; 1926 // First search the section's relocation entries (if any) for an entry 1927 // for this section offset. 1928 uint32_t sect_addr = info->S.getAddress(); 1929 uint32_t sect_offset = (Pc + Offset) - sect_addr; 1930 DataRefImpl Rel; 1931 MachO::any_relocation_info RE; 1932 bool isExtern = false; 1933 SymbolRef Symbol; 1934 bool r_scattered = false; 1935 uint32_t r_value, pair_r_value, r_type, r_length, other_half; 1936 auto Reloc = 1937 std::find_if(info->S.relocations().begin(), info->S.relocations().end(), 1938 [&](const RelocationRef &Reloc) { 1939 uint64_t RelocOffset; 1940 Reloc.getOffset(RelocOffset); 1941 return RelocOffset == sect_offset; 1942 }); 1943 1944 if (Reloc == info->S.relocations().end()) 1945 return 0; 1946 1947 Rel = Reloc->getRawDataRefImpl(); 1948 RE = info->O->getRelocation(Rel); 1949 r_length = info->O->getAnyRelocationLength(RE); 1950 r_scattered = info->O->isRelocationScattered(RE); 1951 if (r_scattered) { 1952 r_value = info->O->getScatteredRelocationValue(RE); 1953 r_type = info->O->getScatteredRelocationType(RE); 1954 } else { 1955 r_type = info->O->getAnyRelocationType(RE); 1956 isExtern = info->O->getPlainRelocationExternal(RE); 1957 if (isExtern) { 1958 symbol_iterator RelocSym = Reloc->getSymbol(); 1959 Symbol = *RelocSym; 1960 } 1961 } 1962 if (r_type == MachO::ARM_RELOC_HALF || 1963 r_type == MachO::ARM_RELOC_SECTDIFF || 1964 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF || 1965 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) { 1966 DataRefImpl RelNext = Rel; 1967 info->O->moveRelocationNext(RelNext); 1968 MachO::any_relocation_info RENext; 1969 RENext = info->O->getRelocation(RelNext); 1970 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff; 1971 if (info->O->isRelocationScattered(RENext)) 1972 pair_r_value = info->O->getScatteredRelocationValue(RENext); 1973 } 1974 1975 if (isExtern) { 1976 StringRef SymName; 1977 Symbol.getName(SymName); 1978 const char *name = SymName.data(); 1979 op_info->AddSymbol.Present = 1; 1980 op_info->AddSymbol.Name = name; 1981 switch (r_type) { 1982 case MachO::ARM_RELOC_HALF: 1983 if ((r_length & 0x1) == 1) { 1984 op_info->Value = value << 16 | other_half; 1985 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16; 1986 } else { 1987 op_info->Value = other_half << 16 | value; 1988 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16; 1989 } 1990 break; 1991 default: 1992 break; 1993 } 1994 return 1; 1995 } 1996 // If we have a branch that is not an external relocation entry then 1997 // return 0 so the code in tryAddingSymbolicOperand() can use the 1998 // SymbolLookUp call back with the branch target address to look up the 1999 // symbol and possiblity add an annotation for a symbol stub. 2000 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 || 2001 r_type == MachO::ARM_THUMB_RELOC_BR22)) 2002 return 0; 2003 2004 uint32_t offset = 0; 2005 if (r_type == MachO::ARM_RELOC_HALF || 2006 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) { 2007 if ((r_length & 0x1) == 1) 2008 value = value << 16 | other_half; 2009 else 2010 value = other_half << 16 | value; 2011 } 2012 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF && 2013 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) { 2014 offset = value - r_value; 2015 value = r_value; 2016 } 2017 2018 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) { 2019 if ((r_length & 0x1) == 1) 2020 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16; 2021 else 2022 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16; 2023 const char *add = GuessSymbolName(r_value, info->AddrMap); 2024 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap); 2025 int32_t offset = value - (r_value - pair_r_value); 2026 op_info->AddSymbol.Present = 1; 2027 if (add != nullptr) 2028 op_info->AddSymbol.Name = add; 2029 else 2030 op_info->AddSymbol.Value = r_value; 2031 op_info->SubtractSymbol.Present = 1; 2032 if (sub != nullptr) 2033 op_info->SubtractSymbol.Name = sub; 2034 else 2035 op_info->SubtractSymbol.Value = pair_r_value; 2036 op_info->Value = offset; 2037 return 1; 2038 } 2039 2040 op_info->AddSymbol.Present = 1; 2041 op_info->Value = offset; 2042 if (r_type == MachO::ARM_RELOC_HALF) { 2043 if ((r_length & 0x1) == 1) 2044 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16; 2045 else 2046 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16; 2047 } 2048 const char *add = GuessSymbolName(value, info->AddrMap); 2049 if (add != nullptr) { 2050 op_info->AddSymbol.Name = add; 2051 return 1; 2052 } 2053 op_info->AddSymbol.Value = value; 2054 return 1; 2055 } 2056 if (Arch == Triple::aarch64) { 2057 if (Offset != 0 || Size != 4) 2058 return 0; 2059 // First search the section's relocation entries (if any) for an entry 2060 // for this section offset. 2061 uint64_t sect_addr = info->S.getAddress(); 2062 uint64_t sect_offset = (Pc + Offset) - sect_addr; 2063 auto Reloc = 2064 std::find_if(info->S.relocations().begin(), info->S.relocations().end(), 2065 [&](const RelocationRef &Reloc) { 2066 uint64_t RelocOffset; 2067 Reloc.getOffset(RelocOffset); 2068 return RelocOffset == sect_offset; 2069 }); 2070 2071 if (Reloc == info->S.relocations().end()) 2072 return 0; 2073 2074 DataRefImpl Rel = Reloc->getRawDataRefImpl(); 2075 MachO::any_relocation_info RE = info->O->getRelocation(Rel); 2076 uint32_t r_type = info->O->getAnyRelocationType(RE); 2077 if (r_type == MachO::ARM64_RELOC_ADDEND) { 2078 DataRefImpl RelNext = Rel; 2079 info->O->moveRelocationNext(RelNext); 2080 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext); 2081 if (value == 0) { 2082 value = info->O->getPlainRelocationSymbolNum(RENext); 2083 op_info->Value = value; 2084 } 2085 } 2086 // NOTE: Scattered relocations don't exist on arm64. 2087 if (!info->O->getPlainRelocationExternal(RE)) 2088 return 0; 2089 StringRef SymName; 2090 Reloc->getSymbol()->getName(SymName); 2091 const char *name = SymName.data(); 2092 op_info->AddSymbol.Present = 1; 2093 op_info->AddSymbol.Name = name; 2094 2095 switch (r_type) { 2096 case MachO::ARM64_RELOC_PAGE21: 2097 /* @page */ 2098 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE; 2099 break; 2100 case MachO::ARM64_RELOC_PAGEOFF12: 2101 /* @pageoff */ 2102 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF; 2103 break; 2104 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21: 2105 /* @gotpage */ 2106 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE; 2107 break; 2108 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12: 2109 /* @gotpageoff */ 2110 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF; 2111 break; 2112 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21: 2113 /* @tvlppage is not implemented in llvm-mc */ 2114 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP; 2115 break; 2116 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12: 2117 /* @tvlppageoff is not implemented in llvm-mc */ 2118 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF; 2119 break; 2120 default: 2121 case MachO::ARM64_RELOC_BRANCH26: 2122 op_info->VariantKind = LLVMDisassembler_VariantKind_None; 2123 break; 2124 } 2125 return 1; 2126 } 2127 return 0; 2128 } 2129 2130 // GuessCstringPointer is passed the address of what might be a pointer to a 2131 // literal string in a cstring section. If that address is in a cstring section 2132 // it returns a pointer to that string. Else it returns nullptr. 2133 static const char *GuessCstringPointer(uint64_t ReferenceValue, 2134 struct DisassembleInfo *info) { 2135 uint32_t LoadCommandCount = info->O->getHeader().ncmds; 2136 MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo(); 2137 for (unsigned I = 0;; ++I) { 2138 if (Load.C.cmd == MachO::LC_SEGMENT_64) { 2139 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load); 2140 for (unsigned J = 0; J < Seg.nsects; ++J) { 2141 MachO::section_64 Sec = info->O->getSection64(Load, J); 2142 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE; 2143 if (section_type == MachO::S_CSTRING_LITERALS && 2144 ReferenceValue >= Sec.addr && 2145 ReferenceValue < Sec.addr + Sec.size) { 2146 uint64_t sect_offset = ReferenceValue - Sec.addr; 2147 uint64_t object_offset = Sec.offset + sect_offset; 2148 StringRef MachOContents = info->O->getData(); 2149 uint64_t object_size = MachOContents.size(); 2150 const char *object_addr = (const char *)MachOContents.data(); 2151 if (object_offset < object_size) { 2152 const char *name = object_addr + object_offset; 2153 return name; 2154 } else { 2155 return nullptr; 2156 } 2157 } 2158 } 2159 } else if (Load.C.cmd == MachO::LC_SEGMENT) { 2160 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load); 2161 for (unsigned J = 0; J < Seg.nsects; ++J) { 2162 MachO::section Sec = info->O->getSection(Load, J); 2163 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE; 2164 if (section_type == MachO::S_CSTRING_LITERALS && 2165 ReferenceValue >= Sec.addr && 2166 ReferenceValue < Sec.addr + Sec.size) { 2167 uint64_t sect_offset = ReferenceValue - Sec.addr; 2168 uint64_t object_offset = Sec.offset + sect_offset; 2169 StringRef MachOContents = info->O->getData(); 2170 uint64_t object_size = MachOContents.size(); 2171 const char *object_addr = (const char *)MachOContents.data(); 2172 if (object_offset < object_size) { 2173 const char *name = object_addr + object_offset; 2174 return name; 2175 } else { 2176 return nullptr; 2177 } 2178 } 2179 } 2180 } 2181 if (I == LoadCommandCount - 1) 2182 break; 2183 else 2184 Load = info->O->getNextLoadCommandInfo(Load); 2185 } 2186 return nullptr; 2187 } 2188 2189 // GuessIndirectSymbol returns the name of the indirect symbol for the 2190 // ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe 2191 // an address of a symbol stub or a lazy or non-lazy pointer to associate the 2192 // symbol name being referenced by the stub or pointer. 2193 static const char *GuessIndirectSymbol(uint64_t ReferenceValue, 2194 struct DisassembleInfo *info) { 2195 uint32_t LoadCommandCount = info->O->getHeader().ncmds; 2196 MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo(); 2197 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand(); 2198 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand(); 2199 for (unsigned I = 0;; ++I) { 2200 if (Load.C.cmd == MachO::LC_SEGMENT_64) { 2201 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load); 2202 for (unsigned J = 0; J < Seg.nsects; ++J) { 2203 MachO::section_64 Sec = info->O->getSection64(Load, J); 2204 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE; 2205 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS || 2206 section_type == MachO::S_LAZY_SYMBOL_POINTERS || 2207 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS || 2208 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS || 2209 section_type == MachO::S_SYMBOL_STUBS) && 2210 ReferenceValue >= Sec.addr && 2211 ReferenceValue < Sec.addr + Sec.size) { 2212 uint32_t stride; 2213 if (section_type == MachO::S_SYMBOL_STUBS) 2214 stride = Sec.reserved2; 2215 else 2216 stride = 8; 2217 if (stride == 0) 2218 return nullptr; 2219 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride; 2220 if (index < Dysymtab.nindirectsyms) { 2221 uint32_t indirect_symbol = 2222 info->O->getIndirectSymbolTableEntry(Dysymtab, index); 2223 if (indirect_symbol < Symtab.nsyms) { 2224 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol); 2225 SymbolRef Symbol = *Sym; 2226 StringRef SymName; 2227 Symbol.getName(SymName); 2228 const char *name = SymName.data(); 2229 return name; 2230 } 2231 } 2232 } 2233 } 2234 } else if (Load.C.cmd == MachO::LC_SEGMENT) { 2235 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load); 2236 for (unsigned J = 0; J < Seg.nsects; ++J) { 2237 MachO::section Sec = info->O->getSection(Load, J); 2238 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE; 2239 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS || 2240 section_type == MachO::S_LAZY_SYMBOL_POINTERS || 2241 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS || 2242 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS || 2243 section_type == MachO::S_SYMBOL_STUBS) && 2244 ReferenceValue >= Sec.addr && 2245 ReferenceValue < Sec.addr + Sec.size) { 2246 uint32_t stride; 2247 if (section_type == MachO::S_SYMBOL_STUBS) 2248 stride = Sec.reserved2; 2249 else 2250 stride = 4; 2251 if (stride == 0) 2252 return nullptr; 2253 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride; 2254 if (index < Dysymtab.nindirectsyms) { 2255 uint32_t indirect_symbol = 2256 info->O->getIndirectSymbolTableEntry(Dysymtab, index); 2257 if (indirect_symbol < Symtab.nsyms) { 2258 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol); 2259 SymbolRef Symbol = *Sym; 2260 StringRef SymName; 2261 Symbol.getName(SymName); 2262 const char *name = SymName.data(); 2263 return name; 2264 } 2265 } 2266 } 2267 } 2268 } 2269 if (I == LoadCommandCount - 1) 2270 break; 2271 else 2272 Load = info->O->getNextLoadCommandInfo(Load); 2273 } 2274 return nullptr; 2275 } 2276 2277 // method_reference() is called passing it the ReferenceName that might be 2278 // a reference it to an Objective-C method call. If so then it allocates and 2279 // assembles a method call string with the values last seen and saved in 2280 // the DisassembleInfo's class_name and selector_name fields. This is saved 2281 // into the method field of the info and any previous string is free'ed. 2282 // Then the class_name field in the info is set to nullptr. The method call 2283 // string is set into ReferenceName and ReferenceType is set to 2284 // LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call 2285 // then both ReferenceType and ReferenceName are left unchanged. 2286 static void method_reference(struct DisassembleInfo *info, 2287 uint64_t *ReferenceType, 2288 const char **ReferenceName) { 2289 unsigned int Arch = info->O->getArch(); 2290 if (*ReferenceName != nullptr) { 2291 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) { 2292 if (info->selector_name != nullptr) { 2293 if (info->method != nullptr) 2294 free(info->method); 2295 if (info->class_name != nullptr) { 2296 info->method = (char *)malloc(5 + strlen(info->class_name) + 2297 strlen(info->selector_name)); 2298 if (info->method != nullptr) { 2299 strcpy(info->method, "+["); 2300 strcat(info->method, info->class_name); 2301 strcat(info->method, " "); 2302 strcat(info->method, info->selector_name); 2303 strcat(info->method, "]"); 2304 *ReferenceName = info->method; 2305 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message; 2306 } 2307 } else { 2308 info->method = (char *)malloc(9 + strlen(info->selector_name)); 2309 if (info->method != nullptr) { 2310 if (Arch == Triple::x86_64) 2311 strcpy(info->method, "-[%rdi "); 2312 else if (Arch == Triple::aarch64) 2313 strcpy(info->method, "-[x0 "); 2314 else 2315 strcpy(info->method, "-[r? "); 2316 strcat(info->method, info->selector_name); 2317 strcat(info->method, "]"); 2318 *ReferenceName = info->method; 2319 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message; 2320 } 2321 } 2322 info->class_name = nullptr; 2323 } 2324 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) { 2325 if (info->selector_name != nullptr) { 2326 if (info->method != nullptr) 2327 free(info->method); 2328 info->method = (char *)malloc(17 + strlen(info->selector_name)); 2329 if (info->method != nullptr) { 2330 if (Arch == Triple::x86_64) 2331 strcpy(info->method, "-[[%rdi super] "); 2332 else if (Arch == Triple::aarch64) 2333 strcpy(info->method, "-[[x0 super] "); 2334 else 2335 strcpy(info->method, "-[[r? super] "); 2336 strcat(info->method, info->selector_name); 2337 strcat(info->method, "]"); 2338 *ReferenceName = info->method; 2339 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message; 2340 } 2341 info->class_name = nullptr; 2342 } 2343 } 2344 } 2345 } 2346 2347 // GuessPointerPointer() is passed the address of what might be a pointer to 2348 // a reference to an Objective-C class, selector, message ref or cfstring. 2349 // If so the value of the pointer is returned and one of the booleans are set 2350 // to true. If not zero is returned and all the booleans are set to false. 2351 static uint64_t GuessPointerPointer(uint64_t ReferenceValue, 2352 struct DisassembleInfo *info, 2353 bool &classref, bool &selref, bool &msgref, 2354 bool &cfstring) { 2355 classref = false; 2356 selref = false; 2357 msgref = false; 2358 cfstring = false; 2359 uint32_t LoadCommandCount = info->O->getHeader().ncmds; 2360 MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo(); 2361 for (unsigned I = 0;; ++I) { 2362 if (Load.C.cmd == MachO::LC_SEGMENT_64) { 2363 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load); 2364 for (unsigned J = 0; J < Seg.nsects; ++J) { 2365 MachO::section_64 Sec = info->O->getSection64(Load, J); 2366 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 || 2367 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 || 2368 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 || 2369 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 || 2370 strncmp(Sec.sectname, "__cfstring", 16) == 0) && 2371 ReferenceValue >= Sec.addr && 2372 ReferenceValue < Sec.addr + Sec.size) { 2373 uint64_t sect_offset = ReferenceValue - Sec.addr; 2374 uint64_t object_offset = Sec.offset + sect_offset; 2375 StringRef MachOContents = info->O->getData(); 2376 uint64_t object_size = MachOContents.size(); 2377 const char *object_addr = (const char *)MachOContents.data(); 2378 if (object_offset < object_size) { 2379 uint64_t pointer_value; 2380 memcpy(&pointer_value, object_addr + object_offset, 2381 sizeof(uint64_t)); 2382 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 2383 sys::swapByteOrder(pointer_value); 2384 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0) 2385 selref = true; 2386 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 || 2387 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0) 2388 classref = true; 2389 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 && 2390 ReferenceValue + 8 < Sec.addr + Sec.size) { 2391 msgref = true; 2392 memcpy(&pointer_value, object_addr + object_offset + 8, 2393 sizeof(uint64_t)); 2394 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 2395 sys::swapByteOrder(pointer_value); 2396 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0) 2397 cfstring = true; 2398 return pointer_value; 2399 } else { 2400 return 0; 2401 } 2402 } 2403 } 2404 } 2405 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files. 2406 if (I == LoadCommandCount - 1) 2407 break; 2408 else 2409 Load = info->O->getNextLoadCommandInfo(Load); 2410 } 2411 return 0; 2412 } 2413 2414 // get_pointer_64 returns a pointer to the bytes in the object file at the 2415 // Address from a section in the Mach-O file. And indirectly returns the 2416 // offset into the section, number of bytes left in the section past the offset 2417 // and which section is was being referenced. If the Address is not in a 2418 // section nullptr is returned. 2419 static const char *get_pointer_64(uint64_t Address, uint32_t &offset, 2420 uint32_t &left, SectionRef &S, 2421 DisassembleInfo *info, 2422 bool objc_only = false) { 2423 offset = 0; 2424 left = 0; 2425 S = SectionRef(); 2426 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) { 2427 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress(); 2428 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize(); 2429 if (objc_only) { 2430 StringRef SectName; 2431 ((*(info->Sections))[SectIdx]).getName(SectName); 2432 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl(); 2433 StringRef SegName = info->O->getSectionFinalSegmentName(Ref); 2434 if (SegName != "__OBJC" && SectName != "__cstring") 2435 continue; 2436 } 2437 if (Address >= SectAddress && Address < SectAddress + SectSize) { 2438 S = (*(info->Sections))[SectIdx]; 2439 offset = Address - SectAddress; 2440 left = SectSize - offset; 2441 StringRef SectContents; 2442 ((*(info->Sections))[SectIdx]).getContents(SectContents); 2443 return SectContents.data() + offset; 2444 } 2445 } 2446 return nullptr; 2447 } 2448 2449 static const char *get_pointer_32(uint32_t Address, uint32_t &offset, 2450 uint32_t &left, SectionRef &S, 2451 DisassembleInfo *info, 2452 bool objc_only = false) { 2453 return get_pointer_64(Address, offset, left, S, info, objc_only); 2454 } 2455 2456 // get_symbol_64() returns the name of a symbol (or nullptr) and the address of 2457 // the symbol indirectly through n_value. Based on the relocation information 2458 // for the specified section offset in the specified section reference. 2459 // If no relocation information is found and a non-zero ReferenceValue for the 2460 // symbol is passed, look up that address in the info's AddrMap. 2461 static const char * 2462 get_symbol_64(uint32_t sect_offset, SectionRef S, DisassembleInfo *info, 2463 uint64_t &n_value, 2464 uint64_t ReferenceValue = UnknownAddressOrSize) { 2465 n_value = 0; 2466 if (!info->verbose) 2467 return nullptr; 2468 2469 // See if there is an external relocation entry at the sect_offset. 2470 bool reloc_found = false; 2471 DataRefImpl Rel; 2472 MachO::any_relocation_info RE; 2473 bool isExtern = false; 2474 SymbolRef Symbol; 2475 for (const RelocationRef &Reloc : S.relocations()) { 2476 uint64_t RelocOffset; 2477 Reloc.getOffset(RelocOffset); 2478 if (RelocOffset == sect_offset) { 2479 Rel = Reloc.getRawDataRefImpl(); 2480 RE = info->O->getRelocation(Rel); 2481 if (info->O->isRelocationScattered(RE)) 2482 continue; 2483 isExtern = info->O->getPlainRelocationExternal(RE); 2484 if (isExtern) { 2485 symbol_iterator RelocSym = Reloc.getSymbol(); 2486 Symbol = *RelocSym; 2487 } 2488 reloc_found = true; 2489 break; 2490 } 2491 } 2492 // If there is an external relocation entry for a symbol in this section 2493 // at this section_offset then use that symbol's value for the n_value 2494 // and return its name. 2495 const char *SymbolName = nullptr; 2496 if (reloc_found && isExtern) { 2497 Symbol.getAddress(n_value); 2498 if (n_value == UnknownAddressOrSize) 2499 n_value = 0; 2500 StringRef name; 2501 Symbol.getName(name); 2502 if (!name.empty()) { 2503 SymbolName = name.data(); 2504 return SymbolName; 2505 } 2506 } 2507 2508 // TODO: For fully linked images, look through the external relocation 2509 // entries off the dynamic symtab command. For these the r_offset is from the 2510 // start of the first writeable segment in the Mach-O file. So the offset 2511 // to this section from that segment is passed to this routine by the caller, 2512 // as the database_offset. Which is the difference of the section's starting 2513 // address and the first writable segment. 2514 // 2515 // NOTE: need add passing the database_offset to this routine. 2516 2517 // We did not find an external relocation entry so look up the ReferenceValue 2518 // as an address of a symbol and if found return that symbol's name. 2519 if (ReferenceValue != UnknownAddressOrSize) 2520 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap); 2521 2522 return SymbolName; 2523 } 2524 2525 static const char *get_symbol_32(uint32_t sect_offset, SectionRef S, 2526 DisassembleInfo *info, 2527 uint32_t ReferenceValue) { 2528 uint64_t n_value64; 2529 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue); 2530 } 2531 2532 // These are structs in the Objective-C meta data and read to produce the 2533 // comments for disassembly. While these are part of the ABI they are no 2534 // public defintions. So the are here not in include/llvm/Support/MachO.h . 2535 2536 // The cfstring object in a 64-bit Mach-O file. 2537 struct cfstring64_t { 2538 uint64_t isa; // class64_t * (64-bit pointer) 2539 uint64_t flags; // flag bits 2540 uint64_t characters; // char * (64-bit pointer) 2541 uint64_t length; // number of non-NULL characters in above 2542 }; 2543 2544 // The class object in a 64-bit Mach-O file. 2545 struct class64_t { 2546 uint64_t isa; // class64_t * (64-bit pointer) 2547 uint64_t superclass; // class64_t * (64-bit pointer) 2548 uint64_t cache; // Cache (64-bit pointer) 2549 uint64_t vtable; // IMP * (64-bit pointer) 2550 uint64_t data; // class_ro64_t * (64-bit pointer) 2551 }; 2552 2553 struct class32_t { 2554 uint32_t isa; /* class32_t * (32-bit pointer) */ 2555 uint32_t superclass; /* class32_t * (32-bit pointer) */ 2556 uint32_t cache; /* Cache (32-bit pointer) */ 2557 uint32_t vtable; /* IMP * (32-bit pointer) */ 2558 uint32_t data; /* class_ro32_t * (32-bit pointer) */ 2559 }; 2560 2561 struct class_ro64_t { 2562 uint32_t flags; 2563 uint32_t instanceStart; 2564 uint32_t instanceSize; 2565 uint32_t reserved; 2566 uint64_t ivarLayout; // const uint8_t * (64-bit pointer) 2567 uint64_t name; // const char * (64-bit pointer) 2568 uint64_t baseMethods; // const method_list_t * (64-bit pointer) 2569 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer) 2570 uint64_t ivars; // const ivar_list_t * (64-bit pointer) 2571 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer) 2572 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer) 2573 }; 2574 2575 struct class_ro32_t { 2576 uint32_t flags; 2577 uint32_t instanceStart; 2578 uint32_t instanceSize; 2579 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */ 2580 uint32_t name; /* const char * (32-bit pointer) */ 2581 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */ 2582 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */ 2583 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */ 2584 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */ 2585 uint32_t baseProperties; /* const struct objc_property_list * 2586 (32-bit pointer) */ 2587 }; 2588 2589 /* Values for class_ro{64,32}_t->flags */ 2590 #define RO_META (1 << 0) 2591 #define RO_ROOT (1 << 1) 2592 #define RO_HAS_CXX_STRUCTORS (1 << 2) 2593 2594 struct method_list64_t { 2595 uint32_t entsize; 2596 uint32_t count; 2597 /* struct method64_t first; These structures follow inline */ 2598 }; 2599 2600 struct method_list32_t { 2601 uint32_t entsize; 2602 uint32_t count; 2603 /* struct method32_t first; These structures follow inline */ 2604 }; 2605 2606 struct method64_t { 2607 uint64_t name; /* SEL (64-bit pointer) */ 2608 uint64_t types; /* const char * (64-bit pointer) */ 2609 uint64_t imp; /* IMP (64-bit pointer) */ 2610 }; 2611 2612 struct method32_t { 2613 uint32_t name; /* SEL (32-bit pointer) */ 2614 uint32_t types; /* const char * (32-bit pointer) */ 2615 uint32_t imp; /* IMP (32-bit pointer) */ 2616 }; 2617 2618 struct protocol_list64_t { 2619 uint64_t count; /* uintptr_t (a 64-bit value) */ 2620 /* struct protocol64_t * list[0]; These pointers follow inline */ 2621 }; 2622 2623 struct protocol_list32_t { 2624 uint32_t count; /* uintptr_t (a 32-bit value) */ 2625 /* struct protocol32_t * list[0]; These pointers follow inline */ 2626 }; 2627 2628 struct protocol64_t { 2629 uint64_t isa; /* id * (64-bit pointer) */ 2630 uint64_t name; /* const char * (64-bit pointer) */ 2631 uint64_t protocols; /* struct protocol_list64_t * 2632 (64-bit pointer) */ 2633 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */ 2634 uint64_t classMethods; /* method_list_t * (64-bit pointer) */ 2635 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */ 2636 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */ 2637 uint64_t instanceProperties; /* struct objc_property_list * 2638 (64-bit pointer) */ 2639 }; 2640 2641 struct protocol32_t { 2642 uint32_t isa; /* id * (32-bit pointer) */ 2643 uint32_t name; /* const char * (32-bit pointer) */ 2644 uint32_t protocols; /* struct protocol_list_t * 2645 (32-bit pointer) */ 2646 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */ 2647 uint32_t classMethods; /* method_list_t * (32-bit pointer) */ 2648 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */ 2649 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */ 2650 uint32_t instanceProperties; /* struct objc_property_list * 2651 (32-bit pointer) */ 2652 }; 2653 2654 struct ivar_list64_t { 2655 uint32_t entsize; 2656 uint32_t count; 2657 /* struct ivar64_t first; These structures follow inline */ 2658 }; 2659 2660 struct ivar_list32_t { 2661 uint32_t entsize; 2662 uint32_t count; 2663 /* struct ivar32_t first; These structures follow inline */ 2664 }; 2665 2666 struct ivar64_t { 2667 uint64_t offset; /* uintptr_t * (64-bit pointer) */ 2668 uint64_t name; /* const char * (64-bit pointer) */ 2669 uint64_t type; /* const char * (64-bit pointer) */ 2670 uint32_t alignment; 2671 uint32_t size; 2672 }; 2673 2674 struct ivar32_t { 2675 uint32_t offset; /* uintptr_t * (32-bit pointer) */ 2676 uint32_t name; /* const char * (32-bit pointer) */ 2677 uint32_t type; /* const char * (32-bit pointer) */ 2678 uint32_t alignment; 2679 uint32_t size; 2680 }; 2681 2682 struct objc_property_list64 { 2683 uint32_t entsize; 2684 uint32_t count; 2685 /* struct objc_property64 first; These structures follow inline */ 2686 }; 2687 2688 struct objc_property_list32 { 2689 uint32_t entsize; 2690 uint32_t count; 2691 /* struct objc_property32 first; These structures follow inline */ 2692 }; 2693 2694 struct objc_property64 { 2695 uint64_t name; /* const char * (64-bit pointer) */ 2696 uint64_t attributes; /* const char * (64-bit pointer) */ 2697 }; 2698 2699 struct objc_property32 { 2700 uint32_t name; /* const char * (32-bit pointer) */ 2701 uint32_t attributes; /* const char * (32-bit pointer) */ 2702 }; 2703 2704 struct category64_t { 2705 uint64_t name; /* const char * (64-bit pointer) */ 2706 uint64_t cls; /* struct class_t * (64-bit pointer) */ 2707 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */ 2708 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */ 2709 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */ 2710 uint64_t instanceProperties; /* struct objc_property_list * 2711 (64-bit pointer) */ 2712 }; 2713 2714 struct category32_t { 2715 uint32_t name; /* const char * (32-bit pointer) */ 2716 uint32_t cls; /* struct class_t * (32-bit pointer) */ 2717 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */ 2718 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */ 2719 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */ 2720 uint32_t instanceProperties; /* struct objc_property_list * 2721 (32-bit pointer) */ 2722 }; 2723 2724 struct objc_image_info64 { 2725 uint32_t version; 2726 uint32_t flags; 2727 }; 2728 struct objc_image_info32 { 2729 uint32_t version; 2730 uint32_t flags; 2731 }; 2732 struct imageInfo_t { 2733 uint32_t version; 2734 uint32_t flags; 2735 }; 2736 /* masks for objc_image_info.flags */ 2737 #define OBJC_IMAGE_IS_REPLACEMENT (1 << 0) 2738 #define OBJC_IMAGE_SUPPORTS_GC (1 << 1) 2739 2740 struct message_ref64 { 2741 uint64_t imp; /* IMP (64-bit pointer) */ 2742 uint64_t sel; /* SEL (64-bit pointer) */ 2743 }; 2744 2745 struct message_ref32 { 2746 uint32_t imp; /* IMP (32-bit pointer) */ 2747 uint32_t sel; /* SEL (32-bit pointer) */ 2748 }; 2749 2750 // Objective-C 1 (32-bit only) meta data structs. 2751 2752 struct objc_module_t { 2753 uint32_t version; 2754 uint32_t size; 2755 uint32_t name; /* char * (32-bit pointer) */ 2756 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */ 2757 }; 2758 2759 struct objc_symtab_t { 2760 uint32_t sel_ref_cnt; 2761 uint32_t refs; /* SEL * (32-bit pointer) */ 2762 uint16_t cls_def_cnt; 2763 uint16_t cat_def_cnt; 2764 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */ 2765 }; 2766 2767 struct objc_class_t { 2768 uint32_t isa; /* struct objc_class * (32-bit pointer) */ 2769 uint32_t super_class; /* struct objc_class * (32-bit pointer) */ 2770 uint32_t name; /* const char * (32-bit pointer) */ 2771 int32_t version; 2772 int32_t info; 2773 int32_t instance_size; 2774 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */ 2775 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */ 2776 uint32_t cache; /* struct objc_cache * (32-bit pointer) */ 2777 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */ 2778 }; 2779 2780 #define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask)) 2781 // class is not a metaclass 2782 #define CLS_CLASS 0x1 2783 // class is a metaclass 2784 #define CLS_META 0x2 2785 2786 struct objc_category_t { 2787 uint32_t category_name; /* char * (32-bit pointer) */ 2788 uint32_t class_name; /* char * (32-bit pointer) */ 2789 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */ 2790 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */ 2791 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */ 2792 }; 2793 2794 struct objc_ivar_t { 2795 uint32_t ivar_name; /* char * (32-bit pointer) */ 2796 uint32_t ivar_type; /* char * (32-bit pointer) */ 2797 int32_t ivar_offset; 2798 }; 2799 2800 struct objc_ivar_list_t { 2801 int32_t ivar_count; 2802 // struct objc_ivar_t ivar_list[1]; /* variable length structure */ 2803 }; 2804 2805 struct objc_method_list_t { 2806 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */ 2807 int32_t method_count; 2808 // struct objc_method_t method_list[1]; /* variable length structure */ 2809 }; 2810 2811 struct objc_method_t { 2812 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */ 2813 uint32_t method_types; /* char * (32-bit pointer) */ 2814 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...) 2815 (32-bit pointer) */ 2816 }; 2817 2818 struct objc_protocol_list_t { 2819 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */ 2820 int32_t count; 2821 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t * 2822 // (32-bit pointer) */ 2823 }; 2824 2825 struct objc_protocol_t { 2826 uint32_t isa; /* struct objc_class * (32-bit pointer) */ 2827 uint32_t protocol_name; /* char * (32-bit pointer) */ 2828 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */ 2829 uint32_t instance_methods; /* struct objc_method_description_list * 2830 (32-bit pointer) */ 2831 uint32_t class_methods; /* struct objc_method_description_list * 2832 (32-bit pointer) */ 2833 }; 2834 2835 struct objc_method_description_list_t { 2836 int32_t count; 2837 // struct objc_method_description_t list[1]; 2838 }; 2839 2840 struct objc_method_description_t { 2841 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */ 2842 uint32_t types; /* char * (32-bit pointer) */ 2843 }; 2844 2845 inline void swapStruct(struct cfstring64_t &cfs) { 2846 sys::swapByteOrder(cfs.isa); 2847 sys::swapByteOrder(cfs.flags); 2848 sys::swapByteOrder(cfs.characters); 2849 sys::swapByteOrder(cfs.length); 2850 } 2851 2852 inline void swapStruct(struct class64_t &c) { 2853 sys::swapByteOrder(c.isa); 2854 sys::swapByteOrder(c.superclass); 2855 sys::swapByteOrder(c.cache); 2856 sys::swapByteOrder(c.vtable); 2857 sys::swapByteOrder(c.data); 2858 } 2859 2860 inline void swapStruct(struct class32_t &c) { 2861 sys::swapByteOrder(c.isa); 2862 sys::swapByteOrder(c.superclass); 2863 sys::swapByteOrder(c.cache); 2864 sys::swapByteOrder(c.vtable); 2865 sys::swapByteOrder(c.data); 2866 } 2867 2868 inline void swapStruct(struct class_ro64_t &cro) { 2869 sys::swapByteOrder(cro.flags); 2870 sys::swapByteOrder(cro.instanceStart); 2871 sys::swapByteOrder(cro.instanceSize); 2872 sys::swapByteOrder(cro.reserved); 2873 sys::swapByteOrder(cro.ivarLayout); 2874 sys::swapByteOrder(cro.name); 2875 sys::swapByteOrder(cro.baseMethods); 2876 sys::swapByteOrder(cro.baseProtocols); 2877 sys::swapByteOrder(cro.ivars); 2878 sys::swapByteOrder(cro.weakIvarLayout); 2879 sys::swapByteOrder(cro.baseProperties); 2880 } 2881 2882 inline void swapStruct(struct class_ro32_t &cro) { 2883 sys::swapByteOrder(cro.flags); 2884 sys::swapByteOrder(cro.instanceStart); 2885 sys::swapByteOrder(cro.instanceSize); 2886 sys::swapByteOrder(cro.ivarLayout); 2887 sys::swapByteOrder(cro.name); 2888 sys::swapByteOrder(cro.baseMethods); 2889 sys::swapByteOrder(cro.baseProtocols); 2890 sys::swapByteOrder(cro.ivars); 2891 sys::swapByteOrder(cro.weakIvarLayout); 2892 sys::swapByteOrder(cro.baseProperties); 2893 } 2894 2895 inline void swapStruct(struct method_list64_t &ml) { 2896 sys::swapByteOrder(ml.entsize); 2897 sys::swapByteOrder(ml.count); 2898 } 2899 2900 inline void swapStruct(struct method_list32_t &ml) { 2901 sys::swapByteOrder(ml.entsize); 2902 sys::swapByteOrder(ml.count); 2903 } 2904 2905 inline void swapStruct(struct method64_t &m) { 2906 sys::swapByteOrder(m.name); 2907 sys::swapByteOrder(m.types); 2908 sys::swapByteOrder(m.imp); 2909 } 2910 2911 inline void swapStruct(struct method32_t &m) { 2912 sys::swapByteOrder(m.name); 2913 sys::swapByteOrder(m.types); 2914 sys::swapByteOrder(m.imp); 2915 } 2916 2917 inline void swapStruct(struct protocol_list64_t &pl) { 2918 sys::swapByteOrder(pl.count); 2919 } 2920 2921 inline void swapStruct(struct protocol_list32_t &pl) { 2922 sys::swapByteOrder(pl.count); 2923 } 2924 2925 inline void swapStruct(struct protocol64_t &p) { 2926 sys::swapByteOrder(p.isa); 2927 sys::swapByteOrder(p.name); 2928 sys::swapByteOrder(p.protocols); 2929 sys::swapByteOrder(p.instanceMethods); 2930 sys::swapByteOrder(p.classMethods); 2931 sys::swapByteOrder(p.optionalInstanceMethods); 2932 sys::swapByteOrder(p.optionalClassMethods); 2933 sys::swapByteOrder(p.instanceProperties); 2934 } 2935 2936 inline void swapStruct(struct protocol32_t &p) { 2937 sys::swapByteOrder(p.isa); 2938 sys::swapByteOrder(p.name); 2939 sys::swapByteOrder(p.protocols); 2940 sys::swapByteOrder(p.instanceMethods); 2941 sys::swapByteOrder(p.classMethods); 2942 sys::swapByteOrder(p.optionalInstanceMethods); 2943 sys::swapByteOrder(p.optionalClassMethods); 2944 sys::swapByteOrder(p.instanceProperties); 2945 } 2946 2947 inline void swapStruct(struct ivar_list64_t &il) { 2948 sys::swapByteOrder(il.entsize); 2949 sys::swapByteOrder(il.count); 2950 } 2951 2952 inline void swapStruct(struct ivar_list32_t &il) { 2953 sys::swapByteOrder(il.entsize); 2954 sys::swapByteOrder(il.count); 2955 } 2956 2957 inline void swapStruct(struct ivar64_t &i) { 2958 sys::swapByteOrder(i.offset); 2959 sys::swapByteOrder(i.name); 2960 sys::swapByteOrder(i.type); 2961 sys::swapByteOrder(i.alignment); 2962 sys::swapByteOrder(i.size); 2963 } 2964 2965 inline void swapStruct(struct ivar32_t &i) { 2966 sys::swapByteOrder(i.offset); 2967 sys::swapByteOrder(i.name); 2968 sys::swapByteOrder(i.type); 2969 sys::swapByteOrder(i.alignment); 2970 sys::swapByteOrder(i.size); 2971 } 2972 2973 inline void swapStruct(struct objc_property_list64 &pl) { 2974 sys::swapByteOrder(pl.entsize); 2975 sys::swapByteOrder(pl.count); 2976 } 2977 2978 inline void swapStruct(struct objc_property_list32 &pl) { 2979 sys::swapByteOrder(pl.entsize); 2980 sys::swapByteOrder(pl.count); 2981 } 2982 2983 inline void swapStruct(struct objc_property64 &op) { 2984 sys::swapByteOrder(op.name); 2985 sys::swapByteOrder(op.attributes); 2986 } 2987 2988 inline void swapStruct(struct objc_property32 &op) { 2989 sys::swapByteOrder(op.name); 2990 sys::swapByteOrder(op.attributes); 2991 } 2992 2993 inline void swapStruct(struct category64_t &c) { 2994 sys::swapByteOrder(c.name); 2995 sys::swapByteOrder(c.cls); 2996 sys::swapByteOrder(c.instanceMethods); 2997 sys::swapByteOrder(c.classMethods); 2998 sys::swapByteOrder(c.protocols); 2999 sys::swapByteOrder(c.instanceProperties); 3000 } 3001 3002 inline void swapStruct(struct category32_t &c) { 3003 sys::swapByteOrder(c.name); 3004 sys::swapByteOrder(c.cls); 3005 sys::swapByteOrder(c.instanceMethods); 3006 sys::swapByteOrder(c.classMethods); 3007 sys::swapByteOrder(c.protocols); 3008 sys::swapByteOrder(c.instanceProperties); 3009 } 3010 3011 inline void swapStruct(struct objc_image_info64 &o) { 3012 sys::swapByteOrder(o.version); 3013 sys::swapByteOrder(o.flags); 3014 } 3015 3016 inline void swapStruct(struct objc_image_info32 &o) { 3017 sys::swapByteOrder(o.version); 3018 sys::swapByteOrder(o.flags); 3019 } 3020 3021 inline void swapStruct(struct imageInfo_t &o) { 3022 sys::swapByteOrder(o.version); 3023 sys::swapByteOrder(o.flags); 3024 } 3025 3026 inline void swapStruct(struct message_ref64 &mr) { 3027 sys::swapByteOrder(mr.imp); 3028 sys::swapByteOrder(mr.sel); 3029 } 3030 3031 inline void swapStruct(struct message_ref32 &mr) { 3032 sys::swapByteOrder(mr.imp); 3033 sys::swapByteOrder(mr.sel); 3034 } 3035 3036 inline void swapStruct(struct objc_module_t &module) { 3037 sys::swapByteOrder(module.version); 3038 sys::swapByteOrder(module.size); 3039 sys::swapByteOrder(module.name); 3040 sys::swapByteOrder(module.symtab); 3041 } 3042 3043 inline void swapStruct(struct objc_symtab_t &symtab) { 3044 sys::swapByteOrder(symtab.sel_ref_cnt); 3045 sys::swapByteOrder(symtab.refs); 3046 sys::swapByteOrder(symtab.cls_def_cnt); 3047 sys::swapByteOrder(symtab.cat_def_cnt); 3048 } 3049 3050 inline void swapStruct(struct objc_class_t &objc_class) { 3051 sys::swapByteOrder(objc_class.isa); 3052 sys::swapByteOrder(objc_class.super_class); 3053 sys::swapByteOrder(objc_class.name); 3054 sys::swapByteOrder(objc_class.version); 3055 sys::swapByteOrder(objc_class.info); 3056 sys::swapByteOrder(objc_class.instance_size); 3057 sys::swapByteOrder(objc_class.ivars); 3058 sys::swapByteOrder(objc_class.methodLists); 3059 sys::swapByteOrder(objc_class.cache); 3060 sys::swapByteOrder(objc_class.protocols); 3061 } 3062 3063 inline void swapStruct(struct objc_category_t &objc_category) { 3064 sys::swapByteOrder(objc_category.category_name); 3065 sys::swapByteOrder(objc_category.class_name); 3066 sys::swapByteOrder(objc_category.instance_methods); 3067 sys::swapByteOrder(objc_category.class_methods); 3068 sys::swapByteOrder(objc_category.protocols); 3069 } 3070 3071 inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) { 3072 sys::swapByteOrder(objc_ivar_list.ivar_count); 3073 } 3074 3075 inline void swapStruct(struct objc_ivar_t &objc_ivar) { 3076 sys::swapByteOrder(objc_ivar.ivar_name); 3077 sys::swapByteOrder(objc_ivar.ivar_type); 3078 sys::swapByteOrder(objc_ivar.ivar_offset); 3079 } 3080 3081 inline void swapStruct(struct objc_method_list_t &method_list) { 3082 sys::swapByteOrder(method_list.obsolete); 3083 sys::swapByteOrder(method_list.method_count); 3084 } 3085 3086 inline void swapStruct(struct objc_method_t &method) { 3087 sys::swapByteOrder(method.method_name); 3088 sys::swapByteOrder(method.method_types); 3089 sys::swapByteOrder(method.method_imp); 3090 } 3091 3092 inline void swapStruct(struct objc_protocol_list_t &protocol_list) { 3093 sys::swapByteOrder(protocol_list.next); 3094 sys::swapByteOrder(protocol_list.count); 3095 } 3096 3097 inline void swapStruct(struct objc_protocol_t &protocol) { 3098 sys::swapByteOrder(protocol.isa); 3099 sys::swapByteOrder(protocol.protocol_name); 3100 sys::swapByteOrder(protocol.protocol_list); 3101 sys::swapByteOrder(protocol.instance_methods); 3102 sys::swapByteOrder(protocol.class_methods); 3103 } 3104 3105 inline void swapStruct(struct objc_method_description_list_t &mdl) { 3106 sys::swapByteOrder(mdl.count); 3107 } 3108 3109 inline void swapStruct(struct objc_method_description_t &md) { 3110 sys::swapByteOrder(md.name); 3111 sys::swapByteOrder(md.types); 3112 } 3113 3114 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue, 3115 struct DisassembleInfo *info); 3116 3117 // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer 3118 // to an Objective-C class and returns the class name. It is also passed the 3119 // address of the pointer, so when the pointer is zero as it can be in an .o 3120 // file, that is used to look for an external relocation entry with a symbol 3121 // name. 3122 static const char *get_objc2_64bit_class_name(uint64_t pointer_value, 3123 uint64_t ReferenceValue, 3124 struct DisassembleInfo *info) { 3125 const char *r; 3126 uint32_t offset, left; 3127 SectionRef S; 3128 3129 // The pointer_value can be 0 in an object file and have a relocation 3130 // entry for the class symbol at the ReferenceValue (the address of the 3131 // pointer). 3132 if (pointer_value == 0) { 3133 r = get_pointer_64(ReferenceValue, offset, left, S, info); 3134 if (r == nullptr || left < sizeof(uint64_t)) 3135 return nullptr; 3136 uint64_t n_value; 3137 const char *symbol_name = get_symbol_64(offset, S, info, n_value); 3138 if (symbol_name == nullptr) 3139 return nullptr; 3140 const char *class_name = strrchr(symbol_name, '$'); 3141 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0') 3142 return class_name + 2; 3143 else 3144 return nullptr; 3145 } 3146 3147 // The case were the pointer_value is non-zero and points to a class defined 3148 // in this Mach-O file. 3149 r = get_pointer_64(pointer_value, offset, left, S, info); 3150 if (r == nullptr || left < sizeof(struct class64_t)) 3151 return nullptr; 3152 struct class64_t c; 3153 memcpy(&c, r, sizeof(struct class64_t)); 3154 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 3155 swapStruct(c); 3156 if (c.data == 0) 3157 return nullptr; 3158 r = get_pointer_64(c.data, offset, left, S, info); 3159 if (r == nullptr || left < sizeof(struct class_ro64_t)) 3160 return nullptr; 3161 struct class_ro64_t cro; 3162 memcpy(&cro, r, sizeof(struct class_ro64_t)); 3163 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 3164 swapStruct(cro); 3165 if (cro.name == 0) 3166 return nullptr; 3167 const char *name = get_pointer_64(cro.name, offset, left, S, info); 3168 return name; 3169 } 3170 3171 // get_objc2_64bit_cfstring_name is used for disassembly and is passed a 3172 // pointer to a cfstring and returns its name or nullptr. 3173 static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue, 3174 struct DisassembleInfo *info) { 3175 const char *r, *name; 3176 uint32_t offset, left; 3177 SectionRef S; 3178 struct cfstring64_t cfs; 3179 uint64_t cfs_characters; 3180 3181 r = get_pointer_64(ReferenceValue, offset, left, S, info); 3182 if (r == nullptr || left < sizeof(struct cfstring64_t)) 3183 return nullptr; 3184 memcpy(&cfs, r, sizeof(struct cfstring64_t)); 3185 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 3186 swapStruct(cfs); 3187 if (cfs.characters == 0) { 3188 uint64_t n_value; 3189 const char *symbol_name = get_symbol_64( 3190 offset + offsetof(struct cfstring64_t, characters), S, info, n_value); 3191 if (symbol_name == nullptr) 3192 return nullptr; 3193 cfs_characters = n_value; 3194 } else 3195 cfs_characters = cfs.characters; 3196 name = get_pointer_64(cfs_characters, offset, left, S, info); 3197 3198 return name; 3199 } 3200 3201 // get_objc2_64bit_selref() is used for disassembly and is passed a the address 3202 // of a pointer to an Objective-C selector reference when the pointer value is 3203 // zero as in a .o file and is likely to have a external relocation entry with 3204 // who's symbol's n_value is the real pointer to the selector name. If that is 3205 // the case the real pointer to the selector name is returned else 0 is 3206 // returned 3207 static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue, 3208 struct DisassembleInfo *info) { 3209 uint32_t offset, left; 3210 SectionRef S; 3211 3212 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info); 3213 if (r == nullptr || left < sizeof(uint64_t)) 3214 return 0; 3215 uint64_t n_value; 3216 const char *symbol_name = get_symbol_64(offset, S, info, n_value); 3217 if (symbol_name == nullptr) 3218 return 0; 3219 return n_value; 3220 } 3221 3222 static const SectionRef get_section(MachOObjectFile *O, const char *segname, 3223 const char *sectname) { 3224 for (const SectionRef &Section : O->sections()) { 3225 StringRef SectName; 3226 Section.getName(SectName); 3227 DataRefImpl Ref = Section.getRawDataRefImpl(); 3228 StringRef SegName = O->getSectionFinalSegmentName(Ref); 3229 if (SegName == segname && SectName == sectname) 3230 return Section; 3231 } 3232 return SectionRef(); 3233 } 3234 3235 static void 3236 walk_pointer_list_64(const char *listname, const SectionRef S, 3237 MachOObjectFile *O, struct DisassembleInfo *info, 3238 void (*func)(uint64_t, struct DisassembleInfo *info)) { 3239 if (S == SectionRef()) 3240 return; 3241 3242 StringRef SectName; 3243 S.getName(SectName); 3244 DataRefImpl Ref = S.getRawDataRefImpl(); 3245 StringRef SegName = O->getSectionFinalSegmentName(Ref); 3246 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 3247 3248 StringRef BytesStr; 3249 S.getContents(BytesStr); 3250 const char *Contents = reinterpret_cast<const char *>(BytesStr.data()); 3251 3252 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) { 3253 uint32_t left = S.getSize() - i; 3254 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t); 3255 uint64_t p = 0; 3256 memcpy(&p, Contents + i, size); 3257 if (i + sizeof(uint64_t) > S.getSize()) 3258 outs() << listname << " list pointer extends past end of (" << SegName 3259 << "," << SectName << ") section\n"; 3260 outs() << format("%016" PRIx64, S.getAddress() + i) << " "; 3261 3262 if (O->isLittleEndian() != sys::IsLittleEndianHost) 3263 sys::swapByteOrder(p); 3264 3265 uint64_t n_value = 0; 3266 const char *name = get_symbol_64(i, S, info, n_value, p); 3267 if (name == nullptr) 3268 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info); 3269 3270 if (n_value != 0) { 3271 outs() << format("0x%" PRIx64, n_value); 3272 if (p != 0) 3273 outs() << " + " << format("0x%" PRIx64, p); 3274 } else 3275 outs() << format("0x%" PRIx64, p); 3276 if (name != nullptr) 3277 outs() << " " << name; 3278 outs() << "\n"; 3279 3280 p += n_value; 3281 if (func) 3282 func(p, info); 3283 } 3284 } 3285 3286 static void 3287 walk_pointer_list_32(const char *listname, const SectionRef S, 3288 MachOObjectFile *O, struct DisassembleInfo *info, 3289 void (*func)(uint32_t, struct DisassembleInfo *info)) { 3290 if (S == SectionRef()) 3291 return; 3292 3293 StringRef SectName; 3294 S.getName(SectName); 3295 DataRefImpl Ref = S.getRawDataRefImpl(); 3296 StringRef SegName = O->getSectionFinalSegmentName(Ref); 3297 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 3298 3299 StringRef BytesStr; 3300 S.getContents(BytesStr); 3301 const char *Contents = reinterpret_cast<const char *>(BytesStr.data()); 3302 3303 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) { 3304 uint32_t left = S.getSize() - i; 3305 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t); 3306 uint32_t p = 0; 3307 memcpy(&p, Contents + i, size); 3308 if (i + sizeof(uint32_t) > S.getSize()) 3309 outs() << listname << " list pointer extends past end of (" << SegName 3310 << "," << SectName << ") section\n"; 3311 uint32_t Address = S.getAddress() + i; 3312 outs() << format("%08" PRIx32, Address) << " "; 3313 3314 if (O->isLittleEndian() != sys::IsLittleEndianHost) 3315 sys::swapByteOrder(p); 3316 outs() << format("0x%" PRIx32, p); 3317 3318 const char *name = get_symbol_32(i, S, info, p); 3319 if (name != nullptr) 3320 outs() << " " << name; 3321 outs() << "\n"; 3322 3323 if (func) 3324 func(p, info); 3325 } 3326 } 3327 3328 static void print_layout_map(const char *layout_map, uint32_t left) { 3329 outs() << " layout map: "; 3330 do { 3331 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " "; 3332 left--; 3333 layout_map++; 3334 } while (*layout_map != '\0' && left != 0); 3335 outs() << "\n"; 3336 } 3337 3338 static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) { 3339 uint32_t offset, left; 3340 SectionRef S; 3341 const char *layout_map; 3342 3343 if (p == 0) 3344 return; 3345 layout_map = get_pointer_64(p, offset, left, S, info); 3346 print_layout_map(layout_map, left); 3347 } 3348 3349 static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) { 3350 uint32_t offset, left; 3351 SectionRef S; 3352 const char *layout_map; 3353 3354 if (p == 0) 3355 return; 3356 layout_map = get_pointer_32(p, offset, left, S, info); 3357 print_layout_map(layout_map, left); 3358 } 3359 3360 static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info, 3361 const char *indent) { 3362 struct method_list64_t ml; 3363 struct method64_t m; 3364 const char *r; 3365 uint32_t offset, xoffset, left, i; 3366 SectionRef S, xS; 3367 const char *name, *sym_name; 3368 uint64_t n_value; 3369 3370 r = get_pointer_64(p, offset, left, S, info); 3371 if (r == nullptr) 3372 return; 3373 memset(&ml, '\0', sizeof(struct method_list64_t)); 3374 if (left < sizeof(struct method_list64_t)) { 3375 memcpy(&ml, r, left); 3376 outs() << " (method_list_t entends past the end of the section)\n"; 3377 } else 3378 memcpy(&ml, r, sizeof(struct method_list64_t)); 3379 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 3380 swapStruct(ml); 3381 outs() << indent << "\t\t entsize " << ml.entsize << "\n"; 3382 outs() << indent << "\t\t count " << ml.count << "\n"; 3383 3384 p += sizeof(struct method_list64_t); 3385 offset += sizeof(struct method_list64_t); 3386 for (i = 0; i < ml.count; i++) { 3387 r = get_pointer_64(p, offset, left, S, info); 3388 if (r == nullptr) 3389 return; 3390 memset(&m, '\0', sizeof(struct method64_t)); 3391 if (left < sizeof(struct method64_t)) { 3392 memcpy(&ml, r, left); 3393 outs() << indent << " (method_t entends past the end of the section)\n"; 3394 } else 3395 memcpy(&m, r, sizeof(struct method64_t)); 3396 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 3397 swapStruct(m); 3398 3399 outs() << indent << "\t\t name "; 3400 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S, 3401 info, n_value, m.name); 3402 if (n_value != 0) { 3403 if (info->verbose && sym_name != nullptr) 3404 outs() << sym_name; 3405 else 3406 outs() << format("0x%" PRIx64, n_value); 3407 if (m.name != 0) 3408 outs() << " + " << format("0x%" PRIx64, m.name); 3409 } else 3410 outs() << format("0x%" PRIx64, m.name); 3411 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info); 3412 if (name != nullptr) 3413 outs() << format(" %.*s", left, name); 3414 outs() << "\n"; 3415 3416 outs() << indent << "\t\t types "; 3417 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S, 3418 info, n_value, m.types); 3419 if (n_value != 0) { 3420 if (info->verbose && sym_name != nullptr) 3421 outs() << sym_name; 3422 else 3423 outs() << format("0x%" PRIx64, n_value); 3424 if (m.types != 0) 3425 outs() << " + " << format("0x%" PRIx64, m.types); 3426 } else 3427 outs() << format("0x%" PRIx64, m.types); 3428 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info); 3429 if (name != nullptr) 3430 outs() << format(" %.*s", left, name); 3431 outs() << "\n"; 3432 3433 outs() << indent << "\t\t imp "; 3434 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info, 3435 n_value, m.imp); 3436 if (info->verbose && name == nullptr) { 3437 if (n_value != 0) { 3438 outs() << format("0x%" PRIx64, n_value) << " "; 3439 if (m.imp != 0) 3440 outs() << "+ " << format("0x%" PRIx64, m.imp) << " "; 3441 } else 3442 outs() << format("0x%" PRIx64, m.imp) << " "; 3443 } 3444 if (name != nullptr) 3445 outs() << name; 3446 outs() << "\n"; 3447 3448 p += sizeof(struct method64_t); 3449 offset += sizeof(struct method64_t); 3450 } 3451 } 3452 3453 static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info, 3454 const char *indent) { 3455 struct method_list32_t ml; 3456 struct method32_t m; 3457 const char *r, *name; 3458 uint32_t offset, xoffset, left, i; 3459 SectionRef S, xS; 3460 3461 r = get_pointer_32(p, offset, left, S, info); 3462 if (r == nullptr) 3463 return; 3464 memset(&ml, '\0', sizeof(struct method_list32_t)); 3465 if (left < sizeof(struct method_list32_t)) { 3466 memcpy(&ml, r, left); 3467 outs() << " (method_list_t entends past the end of the section)\n"; 3468 } else 3469 memcpy(&ml, r, sizeof(struct method_list32_t)); 3470 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 3471 swapStruct(ml); 3472 outs() << indent << "\t\t entsize " << ml.entsize << "\n"; 3473 outs() << indent << "\t\t count " << ml.count << "\n"; 3474 3475 p += sizeof(struct method_list32_t); 3476 offset += sizeof(struct method_list32_t); 3477 for (i = 0; i < ml.count; i++) { 3478 r = get_pointer_32(p, offset, left, S, info); 3479 if (r == nullptr) 3480 return; 3481 memset(&m, '\0', sizeof(struct method32_t)); 3482 if (left < sizeof(struct method32_t)) { 3483 memcpy(&ml, r, left); 3484 outs() << indent << " (method_t entends past the end of the section)\n"; 3485 } else 3486 memcpy(&m, r, sizeof(struct method32_t)); 3487 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 3488 swapStruct(m); 3489 3490 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name); 3491 name = get_pointer_32(m.name, xoffset, left, xS, info); 3492 if (name != nullptr) 3493 outs() << format(" %.*s", left, name); 3494 outs() << "\n"; 3495 3496 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types); 3497 name = get_pointer_32(m.types, xoffset, left, xS, info); 3498 if (name != nullptr) 3499 outs() << format(" %.*s", left, name); 3500 outs() << "\n"; 3501 3502 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp); 3503 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info, 3504 m.imp); 3505 if (name != nullptr) 3506 outs() << " " << name; 3507 outs() << "\n"; 3508 3509 p += sizeof(struct method32_t); 3510 offset += sizeof(struct method32_t); 3511 } 3512 } 3513 3514 static bool print_method_list(uint32_t p, struct DisassembleInfo *info) { 3515 uint32_t offset, left, xleft; 3516 SectionRef S; 3517 struct objc_method_list_t method_list; 3518 struct objc_method_t method; 3519 const char *r, *methods, *name, *SymbolName; 3520 int32_t i; 3521 3522 r = get_pointer_32(p, offset, left, S, info, true); 3523 if (r == nullptr) 3524 return true; 3525 3526 outs() << "\n"; 3527 if (left > sizeof(struct objc_method_list_t)) { 3528 memcpy(&method_list, r, sizeof(struct objc_method_list_t)); 3529 } else { 3530 outs() << "\t\t objc_method_list extends past end of the section\n"; 3531 memset(&method_list, '\0', sizeof(struct objc_method_list_t)); 3532 memcpy(&method_list, r, left); 3533 } 3534 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 3535 swapStruct(method_list); 3536 3537 outs() << "\t\t obsolete " 3538 << format("0x%08" PRIx32, method_list.obsolete) << "\n"; 3539 outs() << "\t\t method_count " << method_list.method_count << "\n"; 3540 3541 methods = r + sizeof(struct objc_method_list_t); 3542 for (i = 0; i < method_list.method_count; i++) { 3543 if ((i + 1) * sizeof(struct objc_method_t) > left) { 3544 outs() << "\t\t remaining method's extend past the of the section\n"; 3545 break; 3546 } 3547 memcpy(&method, methods + i * sizeof(struct objc_method_t), 3548 sizeof(struct objc_method_t)); 3549 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 3550 swapStruct(method); 3551 3552 outs() << "\t\t method_name " 3553 << format("0x%08" PRIx32, method.method_name); 3554 if (info->verbose) { 3555 name = get_pointer_32(method.method_name, offset, xleft, S, info, true); 3556 if (name != nullptr) 3557 outs() << format(" %.*s", xleft, name); 3558 else 3559 outs() << " (not in an __OBJC section)"; 3560 } 3561 outs() << "\n"; 3562 3563 outs() << "\t\t method_types " 3564 << format("0x%08" PRIx32, method.method_types); 3565 if (info->verbose) { 3566 name = get_pointer_32(method.method_types, offset, xleft, S, info, true); 3567 if (name != nullptr) 3568 outs() << format(" %.*s", xleft, name); 3569 else 3570 outs() << " (not in an __OBJC section)"; 3571 } 3572 outs() << "\n"; 3573 3574 outs() << "\t\t method_imp " 3575 << format("0x%08" PRIx32, method.method_imp) << " "; 3576 if (info->verbose) { 3577 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap); 3578 if (SymbolName != nullptr) 3579 outs() << SymbolName; 3580 } 3581 outs() << "\n"; 3582 } 3583 return false; 3584 } 3585 3586 static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) { 3587 struct protocol_list64_t pl; 3588 uint64_t q, n_value; 3589 struct protocol64_t pc; 3590 const char *r; 3591 uint32_t offset, xoffset, left, i; 3592 SectionRef S, xS; 3593 const char *name, *sym_name; 3594 3595 r = get_pointer_64(p, offset, left, S, info); 3596 if (r == nullptr) 3597 return; 3598 memset(&pl, '\0', sizeof(struct protocol_list64_t)); 3599 if (left < sizeof(struct protocol_list64_t)) { 3600 memcpy(&pl, r, left); 3601 outs() << " (protocol_list_t entends past the end of the section)\n"; 3602 } else 3603 memcpy(&pl, r, sizeof(struct protocol_list64_t)); 3604 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 3605 swapStruct(pl); 3606 outs() << " count " << pl.count << "\n"; 3607 3608 p += sizeof(struct protocol_list64_t); 3609 offset += sizeof(struct protocol_list64_t); 3610 for (i = 0; i < pl.count; i++) { 3611 r = get_pointer_64(p, offset, left, S, info); 3612 if (r == nullptr) 3613 return; 3614 q = 0; 3615 if (left < sizeof(uint64_t)) { 3616 memcpy(&q, r, left); 3617 outs() << " (protocol_t * entends past the end of the section)\n"; 3618 } else 3619 memcpy(&q, r, sizeof(uint64_t)); 3620 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 3621 sys::swapByteOrder(q); 3622 3623 outs() << "\t\t list[" << i << "] "; 3624 sym_name = get_symbol_64(offset, S, info, n_value, q); 3625 if (n_value != 0) { 3626 if (info->verbose && sym_name != nullptr) 3627 outs() << sym_name; 3628 else 3629 outs() << format("0x%" PRIx64, n_value); 3630 if (q != 0) 3631 outs() << " + " << format("0x%" PRIx64, q); 3632 } else 3633 outs() << format("0x%" PRIx64, q); 3634 outs() << " (struct protocol_t *)\n"; 3635 3636 r = get_pointer_64(q + n_value, offset, left, S, info); 3637 if (r == nullptr) 3638 return; 3639 memset(&pc, '\0', sizeof(struct protocol64_t)); 3640 if (left < sizeof(struct protocol64_t)) { 3641 memcpy(&pc, r, left); 3642 outs() << " (protocol_t entends past the end of the section)\n"; 3643 } else 3644 memcpy(&pc, r, sizeof(struct protocol64_t)); 3645 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 3646 swapStruct(pc); 3647 3648 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n"; 3649 3650 outs() << "\t\t\t name "; 3651 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S, 3652 info, n_value, pc.name); 3653 if (n_value != 0) { 3654 if (info->verbose && sym_name != nullptr) 3655 outs() << sym_name; 3656 else 3657 outs() << format("0x%" PRIx64, n_value); 3658 if (pc.name != 0) 3659 outs() << " + " << format("0x%" PRIx64, pc.name); 3660 } else 3661 outs() << format("0x%" PRIx64, pc.name); 3662 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info); 3663 if (name != nullptr) 3664 outs() << format(" %.*s", left, name); 3665 outs() << "\n"; 3666 3667 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n"; 3668 3669 outs() << "\t\t instanceMethods "; 3670 sym_name = 3671 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods), 3672 S, info, n_value, pc.instanceMethods); 3673 if (n_value != 0) { 3674 if (info->verbose && sym_name != nullptr) 3675 outs() << sym_name; 3676 else 3677 outs() << format("0x%" PRIx64, n_value); 3678 if (pc.instanceMethods != 0) 3679 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods); 3680 } else 3681 outs() << format("0x%" PRIx64, pc.instanceMethods); 3682 outs() << " (struct method_list_t *)\n"; 3683 if (pc.instanceMethods + n_value != 0) 3684 print_method_list64_t(pc.instanceMethods + n_value, info, "\t"); 3685 3686 outs() << "\t\t classMethods "; 3687 sym_name = 3688 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S, 3689 info, n_value, pc.classMethods); 3690 if (n_value != 0) { 3691 if (info->verbose && sym_name != nullptr) 3692 outs() << sym_name; 3693 else 3694 outs() << format("0x%" PRIx64, n_value); 3695 if (pc.classMethods != 0) 3696 outs() << " + " << format("0x%" PRIx64, pc.classMethods); 3697 } else 3698 outs() << format("0x%" PRIx64, pc.classMethods); 3699 outs() << " (struct method_list_t *)\n"; 3700 if (pc.classMethods + n_value != 0) 3701 print_method_list64_t(pc.classMethods + n_value, info, "\t"); 3702 3703 outs() << "\t optionalInstanceMethods " 3704 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n"; 3705 outs() << "\t optionalClassMethods " 3706 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n"; 3707 outs() << "\t instanceProperties " 3708 << format("0x%" PRIx64, pc.instanceProperties) << "\n"; 3709 3710 p += sizeof(uint64_t); 3711 offset += sizeof(uint64_t); 3712 } 3713 } 3714 3715 static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) { 3716 struct protocol_list32_t pl; 3717 uint32_t q; 3718 struct protocol32_t pc; 3719 const char *r; 3720 uint32_t offset, xoffset, left, i; 3721 SectionRef S, xS; 3722 const char *name; 3723 3724 r = get_pointer_32(p, offset, left, S, info); 3725 if (r == nullptr) 3726 return; 3727 memset(&pl, '\0', sizeof(struct protocol_list32_t)); 3728 if (left < sizeof(struct protocol_list32_t)) { 3729 memcpy(&pl, r, left); 3730 outs() << " (protocol_list_t entends past the end of the section)\n"; 3731 } else 3732 memcpy(&pl, r, sizeof(struct protocol_list32_t)); 3733 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 3734 swapStruct(pl); 3735 outs() << " count " << pl.count << "\n"; 3736 3737 p += sizeof(struct protocol_list32_t); 3738 offset += sizeof(struct protocol_list32_t); 3739 for (i = 0; i < pl.count; i++) { 3740 r = get_pointer_32(p, offset, left, S, info); 3741 if (r == nullptr) 3742 return; 3743 q = 0; 3744 if (left < sizeof(uint32_t)) { 3745 memcpy(&q, r, left); 3746 outs() << " (protocol_t * entends past the end of the section)\n"; 3747 } else 3748 memcpy(&q, r, sizeof(uint32_t)); 3749 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 3750 sys::swapByteOrder(q); 3751 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q) 3752 << " (struct protocol_t *)\n"; 3753 r = get_pointer_32(q, offset, left, S, info); 3754 if (r == nullptr) 3755 return; 3756 memset(&pc, '\0', sizeof(struct protocol32_t)); 3757 if (left < sizeof(struct protocol32_t)) { 3758 memcpy(&pc, r, left); 3759 outs() << " (protocol_t entends past the end of the section)\n"; 3760 } else 3761 memcpy(&pc, r, sizeof(struct protocol32_t)); 3762 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 3763 swapStruct(pc); 3764 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n"; 3765 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name); 3766 name = get_pointer_32(pc.name, xoffset, left, xS, info); 3767 if (name != nullptr) 3768 outs() << format(" %.*s", left, name); 3769 outs() << "\n"; 3770 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n"; 3771 outs() << "\t\t instanceMethods " 3772 << format("0x%" PRIx32, pc.instanceMethods) 3773 << " (struct method_list_t *)\n"; 3774 if (pc.instanceMethods != 0) 3775 print_method_list32_t(pc.instanceMethods, info, "\t"); 3776 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods) 3777 << " (struct method_list_t *)\n"; 3778 if (pc.classMethods != 0) 3779 print_method_list32_t(pc.classMethods, info, "\t"); 3780 outs() << "\t optionalInstanceMethods " 3781 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n"; 3782 outs() << "\t optionalClassMethods " 3783 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n"; 3784 outs() << "\t instanceProperties " 3785 << format("0x%" PRIx32, pc.instanceProperties) << "\n"; 3786 p += sizeof(uint32_t); 3787 offset += sizeof(uint32_t); 3788 } 3789 } 3790 3791 static void print_indent(uint32_t indent) { 3792 for (uint32_t i = 0; i < indent;) { 3793 if (indent - i >= 8) { 3794 outs() << "\t"; 3795 i += 8; 3796 } else { 3797 for (uint32_t j = i; j < indent; j++) 3798 outs() << " "; 3799 return; 3800 } 3801 } 3802 } 3803 3804 static bool print_method_description_list(uint32_t p, uint32_t indent, 3805 struct DisassembleInfo *info) { 3806 uint32_t offset, left, xleft; 3807 SectionRef S; 3808 struct objc_method_description_list_t mdl; 3809 struct objc_method_description_t md; 3810 const char *r, *list, *name; 3811 int32_t i; 3812 3813 r = get_pointer_32(p, offset, left, S, info, true); 3814 if (r == nullptr) 3815 return true; 3816 3817 outs() << "\n"; 3818 if (left > sizeof(struct objc_method_description_list_t)) { 3819 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t)); 3820 } else { 3821 print_indent(indent); 3822 outs() << " objc_method_description_list extends past end of the section\n"; 3823 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t)); 3824 memcpy(&mdl, r, left); 3825 } 3826 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 3827 swapStruct(mdl); 3828 3829 print_indent(indent); 3830 outs() << " count " << mdl.count << "\n"; 3831 3832 list = r + sizeof(struct objc_method_description_list_t); 3833 for (i = 0; i < mdl.count; i++) { 3834 if ((i + 1) * sizeof(struct objc_method_description_t) > left) { 3835 print_indent(indent); 3836 outs() << " remaining list entries extend past the of the section\n"; 3837 break; 3838 } 3839 print_indent(indent); 3840 outs() << " list[" << i << "]\n"; 3841 memcpy(&md, list + i * sizeof(struct objc_method_description_t), 3842 sizeof(struct objc_method_description_t)); 3843 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 3844 swapStruct(md); 3845 3846 print_indent(indent); 3847 outs() << " name " << format("0x%08" PRIx32, md.name); 3848 if (info->verbose) { 3849 name = get_pointer_32(md.name, offset, xleft, S, info, true); 3850 if (name != nullptr) 3851 outs() << format(" %.*s", xleft, name); 3852 else 3853 outs() << " (not in an __OBJC section)"; 3854 } 3855 outs() << "\n"; 3856 3857 print_indent(indent); 3858 outs() << " types " << format("0x%08" PRIx32, md.types); 3859 if (info->verbose) { 3860 name = get_pointer_32(md.types, offset, xleft, S, info, true); 3861 if (name != nullptr) 3862 outs() << format(" %.*s", xleft, name); 3863 else 3864 outs() << " (not in an __OBJC section)"; 3865 } 3866 outs() << "\n"; 3867 } 3868 return false; 3869 } 3870 3871 static bool print_protocol_list(uint32_t p, uint32_t indent, 3872 struct DisassembleInfo *info); 3873 3874 static bool print_protocol(uint32_t p, uint32_t indent, 3875 struct DisassembleInfo *info) { 3876 uint32_t offset, left; 3877 SectionRef S; 3878 struct objc_protocol_t protocol; 3879 const char *r, *name; 3880 3881 r = get_pointer_32(p, offset, left, S, info, true); 3882 if (r == nullptr) 3883 return true; 3884 3885 outs() << "\n"; 3886 if (left >= sizeof(struct objc_protocol_t)) { 3887 memcpy(&protocol, r, sizeof(struct objc_protocol_t)); 3888 } else { 3889 print_indent(indent); 3890 outs() << " Protocol extends past end of the section\n"; 3891 memset(&protocol, '\0', sizeof(struct objc_protocol_t)); 3892 memcpy(&protocol, r, left); 3893 } 3894 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 3895 swapStruct(protocol); 3896 3897 print_indent(indent); 3898 outs() << " isa " << format("0x%08" PRIx32, protocol.isa) 3899 << "\n"; 3900 3901 print_indent(indent); 3902 outs() << " protocol_name " 3903 << format("0x%08" PRIx32, protocol.protocol_name); 3904 if (info->verbose) { 3905 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true); 3906 if (name != nullptr) 3907 outs() << format(" %.*s", left, name); 3908 else 3909 outs() << " (not in an __OBJC section)"; 3910 } 3911 outs() << "\n"; 3912 3913 print_indent(indent); 3914 outs() << " protocol_list " 3915 << format("0x%08" PRIx32, protocol.protocol_list); 3916 if (print_protocol_list(protocol.protocol_list, indent + 4, info)) 3917 outs() << " (not in an __OBJC section)\n"; 3918 3919 print_indent(indent); 3920 outs() << " instance_methods " 3921 << format("0x%08" PRIx32, protocol.instance_methods); 3922 if (print_method_description_list(protocol.instance_methods, indent, info)) 3923 outs() << " (not in an __OBJC section)\n"; 3924 3925 print_indent(indent); 3926 outs() << " class_methods " 3927 << format("0x%08" PRIx32, protocol.class_methods); 3928 if (print_method_description_list(protocol.class_methods, indent, info)) 3929 outs() << " (not in an __OBJC section)\n"; 3930 3931 return false; 3932 } 3933 3934 static bool print_protocol_list(uint32_t p, uint32_t indent, 3935 struct DisassembleInfo *info) { 3936 uint32_t offset, left, l; 3937 SectionRef S; 3938 struct objc_protocol_list_t protocol_list; 3939 const char *r, *list; 3940 int32_t i; 3941 3942 r = get_pointer_32(p, offset, left, S, info, true); 3943 if (r == nullptr) 3944 return true; 3945 3946 outs() << "\n"; 3947 if (left > sizeof(struct objc_protocol_list_t)) { 3948 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t)); 3949 } else { 3950 outs() << "\t\t objc_protocol_list_t extends past end of the section\n"; 3951 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t)); 3952 memcpy(&protocol_list, r, left); 3953 } 3954 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 3955 swapStruct(protocol_list); 3956 3957 print_indent(indent); 3958 outs() << " next " << format("0x%08" PRIx32, protocol_list.next) 3959 << "\n"; 3960 print_indent(indent); 3961 outs() << " count " << protocol_list.count << "\n"; 3962 3963 list = r + sizeof(struct objc_protocol_list_t); 3964 for (i = 0; i < protocol_list.count; i++) { 3965 if ((i + 1) * sizeof(uint32_t) > left) { 3966 outs() << "\t\t remaining list entries extend past the of the section\n"; 3967 break; 3968 } 3969 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t)); 3970 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 3971 sys::swapByteOrder(l); 3972 3973 print_indent(indent); 3974 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l); 3975 if (print_protocol(l, indent, info)) 3976 outs() << "(not in an __OBJC section)\n"; 3977 } 3978 return false; 3979 } 3980 3981 static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) { 3982 struct ivar_list64_t il; 3983 struct ivar64_t i; 3984 const char *r; 3985 uint32_t offset, xoffset, left, j; 3986 SectionRef S, xS; 3987 const char *name, *sym_name, *ivar_offset_p; 3988 uint64_t ivar_offset, n_value; 3989 3990 r = get_pointer_64(p, offset, left, S, info); 3991 if (r == nullptr) 3992 return; 3993 memset(&il, '\0', sizeof(struct ivar_list64_t)); 3994 if (left < sizeof(struct ivar_list64_t)) { 3995 memcpy(&il, r, left); 3996 outs() << " (ivar_list_t entends past the end of the section)\n"; 3997 } else 3998 memcpy(&il, r, sizeof(struct ivar_list64_t)); 3999 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4000 swapStruct(il); 4001 outs() << " entsize " << il.entsize << "\n"; 4002 outs() << " count " << il.count << "\n"; 4003 4004 p += sizeof(struct ivar_list64_t); 4005 offset += sizeof(struct ivar_list64_t); 4006 for (j = 0; j < il.count; j++) { 4007 r = get_pointer_64(p, offset, left, S, info); 4008 if (r == nullptr) 4009 return; 4010 memset(&i, '\0', sizeof(struct ivar64_t)); 4011 if (left < sizeof(struct ivar64_t)) { 4012 memcpy(&i, r, left); 4013 outs() << " (ivar_t entends past the end of the section)\n"; 4014 } else 4015 memcpy(&i, r, sizeof(struct ivar64_t)); 4016 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4017 swapStruct(i); 4018 4019 outs() << "\t\t\t offset "; 4020 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S, 4021 info, n_value, i.offset); 4022 if (n_value != 0) { 4023 if (info->verbose && sym_name != nullptr) 4024 outs() << sym_name; 4025 else 4026 outs() << format("0x%" PRIx64, n_value); 4027 if (i.offset != 0) 4028 outs() << " + " << format("0x%" PRIx64, i.offset); 4029 } else 4030 outs() << format("0x%" PRIx64, i.offset); 4031 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info); 4032 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) { 4033 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset)); 4034 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4035 sys::swapByteOrder(ivar_offset); 4036 outs() << " " << ivar_offset << "\n"; 4037 } else 4038 outs() << "\n"; 4039 4040 outs() << "\t\t\t name "; 4041 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info, 4042 n_value, i.name); 4043 if (n_value != 0) { 4044 if (info->verbose && sym_name != nullptr) 4045 outs() << sym_name; 4046 else 4047 outs() << format("0x%" PRIx64, n_value); 4048 if (i.name != 0) 4049 outs() << " + " << format("0x%" PRIx64, i.name); 4050 } else 4051 outs() << format("0x%" PRIx64, i.name); 4052 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info); 4053 if (name != nullptr) 4054 outs() << format(" %.*s", left, name); 4055 outs() << "\n"; 4056 4057 outs() << "\t\t\t type "; 4058 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info, 4059 n_value, i.name); 4060 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info); 4061 if (n_value != 0) { 4062 if (info->verbose && sym_name != nullptr) 4063 outs() << sym_name; 4064 else 4065 outs() << format("0x%" PRIx64, n_value); 4066 if (i.type != 0) 4067 outs() << " + " << format("0x%" PRIx64, i.type); 4068 } else 4069 outs() << format("0x%" PRIx64, i.type); 4070 if (name != nullptr) 4071 outs() << format(" %.*s", left, name); 4072 outs() << "\n"; 4073 4074 outs() << "\t\t\talignment " << i.alignment << "\n"; 4075 outs() << "\t\t\t size " << i.size << "\n"; 4076 4077 p += sizeof(struct ivar64_t); 4078 offset += sizeof(struct ivar64_t); 4079 } 4080 } 4081 4082 static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) { 4083 struct ivar_list32_t il; 4084 struct ivar32_t i; 4085 const char *r; 4086 uint32_t offset, xoffset, left, j; 4087 SectionRef S, xS; 4088 const char *name, *ivar_offset_p; 4089 uint32_t ivar_offset; 4090 4091 r = get_pointer_32(p, offset, left, S, info); 4092 if (r == nullptr) 4093 return; 4094 memset(&il, '\0', sizeof(struct ivar_list32_t)); 4095 if (left < sizeof(struct ivar_list32_t)) { 4096 memcpy(&il, r, left); 4097 outs() << " (ivar_list_t entends past the end of the section)\n"; 4098 } else 4099 memcpy(&il, r, sizeof(struct ivar_list32_t)); 4100 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4101 swapStruct(il); 4102 outs() << " entsize " << il.entsize << "\n"; 4103 outs() << " count " << il.count << "\n"; 4104 4105 p += sizeof(struct ivar_list32_t); 4106 offset += sizeof(struct ivar_list32_t); 4107 for (j = 0; j < il.count; j++) { 4108 r = get_pointer_32(p, offset, left, S, info); 4109 if (r == nullptr) 4110 return; 4111 memset(&i, '\0', sizeof(struct ivar32_t)); 4112 if (left < sizeof(struct ivar32_t)) { 4113 memcpy(&i, r, left); 4114 outs() << " (ivar_t entends past the end of the section)\n"; 4115 } else 4116 memcpy(&i, r, sizeof(struct ivar32_t)); 4117 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4118 swapStruct(i); 4119 4120 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset); 4121 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info); 4122 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) { 4123 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset)); 4124 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4125 sys::swapByteOrder(ivar_offset); 4126 outs() << " " << ivar_offset << "\n"; 4127 } else 4128 outs() << "\n"; 4129 4130 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name); 4131 name = get_pointer_32(i.name, xoffset, left, xS, info); 4132 if (name != nullptr) 4133 outs() << format(" %.*s", left, name); 4134 outs() << "\n"; 4135 4136 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type); 4137 name = get_pointer_32(i.type, xoffset, left, xS, info); 4138 if (name != nullptr) 4139 outs() << format(" %.*s", left, name); 4140 outs() << "\n"; 4141 4142 outs() << "\t\t\talignment " << i.alignment << "\n"; 4143 outs() << "\t\t\t size " << i.size << "\n"; 4144 4145 p += sizeof(struct ivar32_t); 4146 offset += sizeof(struct ivar32_t); 4147 } 4148 } 4149 4150 static void print_objc_property_list64(uint64_t p, 4151 struct DisassembleInfo *info) { 4152 struct objc_property_list64 opl; 4153 struct objc_property64 op; 4154 const char *r; 4155 uint32_t offset, xoffset, left, j; 4156 SectionRef S, xS; 4157 const char *name, *sym_name; 4158 uint64_t n_value; 4159 4160 r = get_pointer_64(p, offset, left, S, info); 4161 if (r == nullptr) 4162 return; 4163 memset(&opl, '\0', sizeof(struct objc_property_list64)); 4164 if (left < sizeof(struct objc_property_list64)) { 4165 memcpy(&opl, r, left); 4166 outs() << " (objc_property_list entends past the end of the section)\n"; 4167 } else 4168 memcpy(&opl, r, sizeof(struct objc_property_list64)); 4169 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4170 swapStruct(opl); 4171 outs() << " entsize " << opl.entsize << "\n"; 4172 outs() << " count " << opl.count << "\n"; 4173 4174 p += sizeof(struct objc_property_list64); 4175 offset += sizeof(struct objc_property_list64); 4176 for (j = 0; j < opl.count; j++) { 4177 r = get_pointer_64(p, offset, left, S, info); 4178 if (r == nullptr) 4179 return; 4180 memset(&op, '\0', sizeof(struct objc_property64)); 4181 if (left < sizeof(struct objc_property64)) { 4182 memcpy(&op, r, left); 4183 outs() << " (objc_property entends past the end of the section)\n"; 4184 } else 4185 memcpy(&op, r, sizeof(struct objc_property64)); 4186 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4187 swapStruct(op); 4188 4189 outs() << "\t\t\t name "; 4190 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S, 4191 info, n_value, op.name); 4192 if (n_value != 0) { 4193 if (info->verbose && sym_name != nullptr) 4194 outs() << sym_name; 4195 else 4196 outs() << format("0x%" PRIx64, n_value); 4197 if (op.name != 0) 4198 outs() << " + " << format("0x%" PRIx64, op.name); 4199 } else 4200 outs() << format("0x%" PRIx64, op.name); 4201 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info); 4202 if (name != nullptr) 4203 outs() << format(" %.*s", left, name); 4204 outs() << "\n"; 4205 4206 outs() << "\t\t\tattributes "; 4207 sym_name = 4208 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S, 4209 info, n_value, op.attributes); 4210 if (n_value != 0) { 4211 if (info->verbose && sym_name != nullptr) 4212 outs() << sym_name; 4213 else 4214 outs() << format("0x%" PRIx64, n_value); 4215 if (op.attributes != 0) 4216 outs() << " + " << format("0x%" PRIx64, op.attributes); 4217 } else 4218 outs() << format("0x%" PRIx64, op.attributes); 4219 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info); 4220 if (name != nullptr) 4221 outs() << format(" %.*s", left, name); 4222 outs() << "\n"; 4223 4224 p += sizeof(struct objc_property64); 4225 offset += sizeof(struct objc_property64); 4226 } 4227 } 4228 4229 static void print_objc_property_list32(uint32_t p, 4230 struct DisassembleInfo *info) { 4231 struct objc_property_list32 opl; 4232 struct objc_property32 op; 4233 const char *r; 4234 uint32_t offset, xoffset, left, j; 4235 SectionRef S, xS; 4236 const char *name; 4237 4238 r = get_pointer_32(p, offset, left, S, info); 4239 if (r == nullptr) 4240 return; 4241 memset(&opl, '\0', sizeof(struct objc_property_list32)); 4242 if (left < sizeof(struct objc_property_list32)) { 4243 memcpy(&opl, r, left); 4244 outs() << " (objc_property_list entends past the end of the section)\n"; 4245 } else 4246 memcpy(&opl, r, sizeof(struct objc_property_list32)); 4247 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4248 swapStruct(opl); 4249 outs() << " entsize " << opl.entsize << "\n"; 4250 outs() << " count " << opl.count << "\n"; 4251 4252 p += sizeof(struct objc_property_list32); 4253 offset += sizeof(struct objc_property_list32); 4254 for (j = 0; j < opl.count; j++) { 4255 r = get_pointer_32(p, offset, left, S, info); 4256 if (r == nullptr) 4257 return; 4258 memset(&op, '\0', sizeof(struct objc_property32)); 4259 if (left < sizeof(struct objc_property32)) { 4260 memcpy(&op, r, left); 4261 outs() << " (objc_property entends past the end of the section)\n"; 4262 } else 4263 memcpy(&op, r, sizeof(struct objc_property32)); 4264 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4265 swapStruct(op); 4266 4267 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name); 4268 name = get_pointer_32(op.name, xoffset, left, xS, info); 4269 if (name != nullptr) 4270 outs() << format(" %.*s", left, name); 4271 outs() << "\n"; 4272 4273 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes); 4274 name = get_pointer_32(op.attributes, xoffset, left, xS, info); 4275 if (name != nullptr) 4276 outs() << format(" %.*s", left, name); 4277 outs() << "\n"; 4278 4279 p += sizeof(struct objc_property32); 4280 offset += sizeof(struct objc_property32); 4281 } 4282 } 4283 4284 static void print_class_ro64_t(uint64_t p, struct DisassembleInfo *info, 4285 bool &is_meta_class) { 4286 struct class_ro64_t cro; 4287 const char *r; 4288 uint32_t offset, xoffset, left; 4289 SectionRef S, xS; 4290 const char *name, *sym_name; 4291 uint64_t n_value; 4292 4293 r = get_pointer_64(p, offset, left, S, info); 4294 if (r == nullptr || left < sizeof(struct class_ro64_t)) 4295 return; 4296 memset(&cro, '\0', sizeof(struct class_ro64_t)); 4297 if (left < sizeof(struct class_ro64_t)) { 4298 memcpy(&cro, r, left); 4299 outs() << " (class_ro_t entends past the end of the section)\n"; 4300 } else 4301 memcpy(&cro, r, sizeof(struct class_ro64_t)); 4302 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4303 swapStruct(cro); 4304 outs() << " flags " << format("0x%" PRIx32, cro.flags); 4305 if (cro.flags & RO_META) 4306 outs() << " RO_META"; 4307 if (cro.flags & RO_ROOT) 4308 outs() << " RO_ROOT"; 4309 if (cro.flags & RO_HAS_CXX_STRUCTORS) 4310 outs() << " RO_HAS_CXX_STRUCTORS"; 4311 outs() << "\n"; 4312 outs() << " instanceStart " << cro.instanceStart << "\n"; 4313 outs() << " instanceSize " << cro.instanceSize << "\n"; 4314 outs() << " reserved " << format("0x%" PRIx32, cro.reserved) 4315 << "\n"; 4316 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout) 4317 << "\n"; 4318 print_layout_map64(cro.ivarLayout, info); 4319 4320 outs() << " name "; 4321 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S, 4322 info, n_value, cro.name); 4323 if (n_value != 0) { 4324 if (info->verbose && sym_name != nullptr) 4325 outs() << sym_name; 4326 else 4327 outs() << format("0x%" PRIx64, n_value); 4328 if (cro.name != 0) 4329 outs() << " + " << format("0x%" PRIx64, cro.name); 4330 } else 4331 outs() << format("0x%" PRIx64, cro.name); 4332 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info); 4333 if (name != nullptr) 4334 outs() << format(" %.*s", left, name); 4335 outs() << "\n"; 4336 4337 outs() << " baseMethods "; 4338 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods), 4339 S, info, n_value, cro.baseMethods); 4340 if (n_value != 0) { 4341 if (info->verbose && sym_name != nullptr) 4342 outs() << sym_name; 4343 else 4344 outs() << format("0x%" PRIx64, n_value); 4345 if (cro.baseMethods != 0) 4346 outs() << " + " << format("0x%" PRIx64, cro.baseMethods); 4347 } else 4348 outs() << format("0x%" PRIx64, cro.baseMethods); 4349 outs() << " (struct method_list_t *)\n"; 4350 if (cro.baseMethods + n_value != 0) 4351 print_method_list64_t(cro.baseMethods + n_value, info, ""); 4352 4353 outs() << " baseProtocols "; 4354 sym_name = 4355 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S, 4356 info, n_value, cro.baseProtocols); 4357 if (n_value != 0) { 4358 if (info->verbose && sym_name != nullptr) 4359 outs() << sym_name; 4360 else 4361 outs() << format("0x%" PRIx64, n_value); 4362 if (cro.baseProtocols != 0) 4363 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols); 4364 } else 4365 outs() << format("0x%" PRIx64, cro.baseProtocols); 4366 outs() << "\n"; 4367 if (cro.baseProtocols + n_value != 0) 4368 print_protocol_list64_t(cro.baseProtocols + n_value, info); 4369 4370 outs() << " ivars "; 4371 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S, 4372 info, n_value, cro.ivars); 4373 if (n_value != 0) { 4374 if (info->verbose && sym_name != nullptr) 4375 outs() << sym_name; 4376 else 4377 outs() << format("0x%" PRIx64, n_value); 4378 if (cro.ivars != 0) 4379 outs() << " + " << format("0x%" PRIx64, cro.ivars); 4380 } else 4381 outs() << format("0x%" PRIx64, cro.ivars); 4382 outs() << "\n"; 4383 if (cro.ivars + n_value != 0) 4384 print_ivar_list64_t(cro.ivars + n_value, info); 4385 4386 outs() << " weakIvarLayout "; 4387 sym_name = 4388 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S, 4389 info, n_value, cro.weakIvarLayout); 4390 if (n_value != 0) { 4391 if (info->verbose && sym_name != nullptr) 4392 outs() << sym_name; 4393 else 4394 outs() << format("0x%" PRIx64, n_value); 4395 if (cro.weakIvarLayout != 0) 4396 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout); 4397 } else 4398 outs() << format("0x%" PRIx64, cro.weakIvarLayout); 4399 outs() << "\n"; 4400 print_layout_map64(cro.weakIvarLayout + n_value, info); 4401 4402 outs() << " baseProperties "; 4403 sym_name = 4404 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S, 4405 info, n_value, cro.baseProperties); 4406 if (n_value != 0) { 4407 if (info->verbose && sym_name != nullptr) 4408 outs() << sym_name; 4409 else 4410 outs() << format("0x%" PRIx64, n_value); 4411 if (cro.baseProperties != 0) 4412 outs() << " + " << format("0x%" PRIx64, cro.baseProperties); 4413 } else 4414 outs() << format("0x%" PRIx64, cro.baseProperties); 4415 outs() << "\n"; 4416 if (cro.baseProperties + n_value != 0) 4417 print_objc_property_list64(cro.baseProperties + n_value, info); 4418 4419 is_meta_class = (cro.flags & RO_META) ? true : false; 4420 } 4421 4422 static void print_class_ro32_t(uint32_t p, struct DisassembleInfo *info, 4423 bool &is_meta_class) { 4424 struct class_ro32_t cro; 4425 const char *r; 4426 uint32_t offset, xoffset, left; 4427 SectionRef S, xS; 4428 const char *name; 4429 4430 r = get_pointer_32(p, offset, left, S, info); 4431 if (r == nullptr) 4432 return; 4433 memset(&cro, '\0', sizeof(struct class_ro32_t)); 4434 if (left < sizeof(struct class_ro32_t)) { 4435 memcpy(&cro, r, left); 4436 outs() << " (class_ro_t entends past the end of the section)\n"; 4437 } else 4438 memcpy(&cro, r, sizeof(struct class_ro32_t)); 4439 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4440 swapStruct(cro); 4441 outs() << " flags " << format("0x%" PRIx32, cro.flags); 4442 if (cro.flags & RO_META) 4443 outs() << " RO_META"; 4444 if (cro.flags & RO_ROOT) 4445 outs() << " RO_ROOT"; 4446 if (cro.flags & RO_HAS_CXX_STRUCTORS) 4447 outs() << " RO_HAS_CXX_STRUCTORS"; 4448 outs() << "\n"; 4449 outs() << " instanceStart " << cro.instanceStart << "\n"; 4450 outs() << " instanceSize " << cro.instanceSize << "\n"; 4451 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout) 4452 << "\n"; 4453 print_layout_map32(cro.ivarLayout, info); 4454 4455 outs() << " name " << format("0x%" PRIx32, cro.name); 4456 name = get_pointer_32(cro.name, xoffset, left, xS, info); 4457 if (name != nullptr) 4458 outs() << format(" %.*s", left, name); 4459 outs() << "\n"; 4460 4461 outs() << " baseMethods " 4462 << format("0x%" PRIx32, cro.baseMethods) 4463 << " (struct method_list_t *)\n"; 4464 if (cro.baseMethods != 0) 4465 print_method_list32_t(cro.baseMethods, info, ""); 4466 4467 outs() << " baseProtocols " 4468 << format("0x%" PRIx32, cro.baseProtocols) << "\n"; 4469 if (cro.baseProtocols != 0) 4470 print_protocol_list32_t(cro.baseProtocols, info); 4471 outs() << " ivars " << format("0x%" PRIx32, cro.ivars) 4472 << "\n"; 4473 if (cro.ivars != 0) 4474 print_ivar_list32_t(cro.ivars, info); 4475 outs() << " weakIvarLayout " 4476 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n"; 4477 print_layout_map32(cro.weakIvarLayout, info); 4478 outs() << " baseProperties " 4479 << format("0x%" PRIx32, cro.baseProperties) << "\n"; 4480 if (cro.baseProperties != 0) 4481 print_objc_property_list32(cro.baseProperties, info); 4482 is_meta_class = (cro.flags & RO_META) ? true : false; 4483 } 4484 4485 static void print_class64_t(uint64_t p, struct DisassembleInfo *info) { 4486 struct class64_t c; 4487 const char *r; 4488 uint32_t offset, left; 4489 SectionRef S; 4490 const char *name; 4491 uint64_t isa_n_value, n_value; 4492 4493 r = get_pointer_64(p, offset, left, S, info); 4494 if (r == nullptr || left < sizeof(struct class64_t)) 4495 return; 4496 memset(&c, '\0', sizeof(struct class64_t)); 4497 if (left < sizeof(struct class64_t)) { 4498 memcpy(&c, r, left); 4499 outs() << " (class_t entends past the end of the section)\n"; 4500 } else 4501 memcpy(&c, r, sizeof(struct class64_t)); 4502 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4503 swapStruct(c); 4504 4505 outs() << " isa " << format("0x%" PRIx64, c.isa); 4506 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info, 4507 isa_n_value, c.isa); 4508 if (name != nullptr) 4509 outs() << " " << name; 4510 outs() << "\n"; 4511 4512 outs() << " superclass " << format("0x%" PRIx64, c.superclass); 4513 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info, 4514 n_value, c.superclass); 4515 if (name != nullptr) 4516 outs() << " " << name; 4517 outs() << "\n"; 4518 4519 outs() << " cache " << format("0x%" PRIx64, c.cache); 4520 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info, 4521 n_value, c.cache); 4522 if (name != nullptr) 4523 outs() << " " << name; 4524 outs() << "\n"; 4525 4526 outs() << " vtable " << format("0x%" PRIx64, c.vtable); 4527 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info, 4528 n_value, c.vtable); 4529 if (name != nullptr) 4530 outs() << " " << name; 4531 outs() << "\n"; 4532 4533 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info, 4534 n_value, c.data); 4535 outs() << " data "; 4536 if (n_value != 0) { 4537 if (info->verbose && name != nullptr) 4538 outs() << name; 4539 else 4540 outs() << format("0x%" PRIx64, n_value); 4541 if (c.data != 0) 4542 outs() << " + " << format("0x%" PRIx64, c.data); 4543 } else 4544 outs() << format("0x%" PRIx64, c.data); 4545 outs() << " (struct class_ro_t *)"; 4546 4547 // This is a Swift class if some of the low bits of the pointer are set. 4548 if ((c.data + n_value) & 0x7) 4549 outs() << " Swift class"; 4550 outs() << "\n"; 4551 bool is_meta_class; 4552 print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class); 4553 4554 if (is_meta_class == false) { 4555 outs() << "Meta Class\n"; 4556 print_class64_t(c.isa + isa_n_value, info); 4557 } 4558 } 4559 4560 static void print_class32_t(uint32_t p, struct DisassembleInfo *info) { 4561 struct class32_t c; 4562 const char *r; 4563 uint32_t offset, left; 4564 SectionRef S; 4565 const char *name; 4566 4567 r = get_pointer_32(p, offset, left, S, info); 4568 if (r == nullptr) 4569 return; 4570 memset(&c, '\0', sizeof(struct class32_t)); 4571 if (left < sizeof(struct class32_t)) { 4572 memcpy(&c, r, left); 4573 outs() << " (class_t entends past the end of the section)\n"; 4574 } else 4575 memcpy(&c, r, sizeof(struct class32_t)); 4576 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4577 swapStruct(c); 4578 4579 outs() << " isa " << format("0x%" PRIx32, c.isa); 4580 name = 4581 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa); 4582 if (name != nullptr) 4583 outs() << " " << name; 4584 outs() << "\n"; 4585 4586 outs() << " superclass " << format("0x%" PRIx32, c.superclass); 4587 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info, 4588 c.superclass); 4589 if (name != nullptr) 4590 outs() << " " << name; 4591 outs() << "\n"; 4592 4593 outs() << " cache " << format("0x%" PRIx32, c.cache); 4594 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info, 4595 c.cache); 4596 if (name != nullptr) 4597 outs() << " " << name; 4598 outs() << "\n"; 4599 4600 outs() << " vtable " << format("0x%" PRIx32, c.vtable); 4601 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info, 4602 c.vtable); 4603 if (name != nullptr) 4604 outs() << " " << name; 4605 outs() << "\n"; 4606 4607 name = 4608 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data); 4609 outs() << " data " << format("0x%" PRIx32, c.data) 4610 << " (struct class_ro_t *)"; 4611 4612 // This is a Swift class if some of the low bits of the pointer are set. 4613 if (c.data & 0x3) 4614 outs() << " Swift class"; 4615 outs() << "\n"; 4616 bool is_meta_class; 4617 print_class_ro32_t(c.data & ~0x3, info, is_meta_class); 4618 4619 if (is_meta_class == false) { 4620 outs() << "Meta Class\n"; 4621 print_class32_t(c.isa, info); 4622 } 4623 } 4624 4625 static void print_objc_class_t(struct objc_class_t *objc_class, 4626 struct DisassembleInfo *info) { 4627 uint32_t offset, left, xleft; 4628 const char *name, *p, *ivar_list; 4629 SectionRef S; 4630 int32_t i; 4631 struct objc_ivar_list_t objc_ivar_list; 4632 struct objc_ivar_t ivar; 4633 4634 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa); 4635 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) { 4636 name = get_pointer_32(objc_class->isa, offset, left, S, info, true); 4637 if (name != nullptr) 4638 outs() << format(" %.*s", left, name); 4639 else 4640 outs() << " (not in an __OBJC section)"; 4641 } 4642 outs() << "\n"; 4643 4644 outs() << "\t super_class " 4645 << format("0x%08" PRIx32, objc_class->super_class); 4646 if (info->verbose) { 4647 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true); 4648 if (name != nullptr) 4649 outs() << format(" %.*s", left, name); 4650 else 4651 outs() << " (not in an __OBJC section)"; 4652 } 4653 outs() << "\n"; 4654 4655 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name); 4656 if (info->verbose) { 4657 name = get_pointer_32(objc_class->name, offset, left, S, info, true); 4658 if (name != nullptr) 4659 outs() << format(" %.*s", left, name); 4660 else 4661 outs() << " (not in an __OBJC section)"; 4662 } 4663 outs() << "\n"; 4664 4665 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version) 4666 << "\n"; 4667 4668 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info); 4669 if (info->verbose) { 4670 if (CLS_GETINFO(objc_class, CLS_CLASS)) 4671 outs() << " CLS_CLASS"; 4672 else if (CLS_GETINFO(objc_class, CLS_META)) 4673 outs() << " CLS_META"; 4674 } 4675 outs() << "\n"; 4676 4677 outs() << "\t instance_size " 4678 << format("0x%08" PRIx32, objc_class->instance_size) << "\n"; 4679 4680 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true); 4681 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars); 4682 if (p != nullptr) { 4683 if (left > sizeof(struct objc_ivar_list_t)) { 4684 outs() << "\n"; 4685 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t)); 4686 } else { 4687 outs() << " (entends past the end of the section)\n"; 4688 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t)); 4689 memcpy(&objc_ivar_list, p, left); 4690 } 4691 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4692 swapStruct(objc_ivar_list); 4693 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n"; 4694 ivar_list = p + sizeof(struct objc_ivar_list_t); 4695 for (i = 0; i < objc_ivar_list.ivar_count; i++) { 4696 if ((i + 1) * sizeof(struct objc_ivar_t) > left) { 4697 outs() << "\t\t remaining ivar's extend past the of the section\n"; 4698 break; 4699 } 4700 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t), 4701 sizeof(struct objc_ivar_t)); 4702 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4703 swapStruct(ivar); 4704 4705 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name); 4706 if (info->verbose) { 4707 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true); 4708 if (name != nullptr) 4709 outs() << format(" %.*s", xleft, name); 4710 else 4711 outs() << " (not in an __OBJC section)"; 4712 } 4713 outs() << "\n"; 4714 4715 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type); 4716 if (info->verbose) { 4717 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true); 4718 if (name != nullptr) 4719 outs() << format(" %.*s", xleft, name); 4720 else 4721 outs() << " (not in an __OBJC section)"; 4722 } 4723 outs() << "\n"; 4724 4725 outs() << "\t\t ivar_offset " 4726 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n"; 4727 } 4728 } else { 4729 outs() << " (not in an __OBJC section)\n"; 4730 } 4731 4732 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists); 4733 if (print_method_list(objc_class->methodLists, info)) 4734 outs() << " (not in an __OBJC section)\n"; 4735 4736 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache) 4737 << "\n"; 4738 4739 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols); 4740 if (print_protocol_list(objc_class->protocols, 16, info)) 4741 outs() << " (not in an __OBJC section)\n"; 4742 } 4743 4744 static void print_objc_objc_category_t(struct objc_category_t *objc_category, 4745 struct DisassembleInfo *info) { 4746 uint32_t offset, left; 4747 const char *name; 4748 SectionRef S; 4749 4750 outs() << "\t category name " 4751 << format("0x%08" PRIx32, objc_category->category_name); 4752 if (info->verbose) { 4753 name = get_pointer_32(objc_category->category_name, offset, left, S, info, 4754 true); 4755 if (name != nullptr) 4756 outs() << format(" %.*s", left, name); 4757 else 4758 outs() << " (not in an __OBJC section)"; 4759 } 4760 outs() << "\n"; 4761 4762 outs() << "\t\t class name " 4763 << format("0x%08" PRIx32, objc_category->class_name); 4764 if (info->verbose) { 4765 name = 4766 get_pointer_32(objc_category->class_name, offset, left, S, info, true); 4767 if (name != nullptr) 4768 outs() << format(" %.*s", left, name); 4769 else 4770 outs() << " (not in an __OBJC section)"; 4771 } 4772 outs() << "\n"; 4773 4774 outs() << "\t instance methods " 4775 << format("0x%08" PRIx32, objc_category->instance_methods); 4776 if (print_method_list(objc_category->instance_methods, info)) 4777 outs() << " (not in an __OBJC section)\n"; 4778 4779 outs() << "\t class methods " 4780 << format("0x%08" PRIx32, objc_category->class_methods); 4781 if (print_method_list(objc_category->class_methods, info)) 4782 outs() << " (not in an __OBJC section)\n"; 4783 } 4784 4785 static void print_category64_t(uint64_t p, struct DisassembleInfo *info) { 4786 struct category64_t c; 4787 const char *r; 4788 uint32_t offset, xoffset, left; 4789 SectionRef S, xS; 4790 const char *name, *sym_name; 4791 uint64_t n_value; 4792 4793 r = get_pointer_64(p, offset, left, S, info); 4794 if (r == nullptr) 4795 return; 4796 memset(&c, '\0', sizeof(struct category64_t)); 4797 if (left < sizeof(struct category64_t)) { 4798 memcpy(&c, r, left); 4799 outs() << " (category_t entends past the end of the section)\n"; 4800 } else 4801 memcpy(&c, r, sizeof(struct category64_t)); 4802 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4803 swapStruct(c); 4804 4805 outs() << " name "; 4806 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S, 4807 info, n_value, c.name); 4808 if (n_value != 0) { 4809 if (info->verbose && sym_name != nullptr) 4810 outs() << sym_name; 4811 else 4812 outs() << format("0x%" PRIx64, n_value); 4813 if (c.name != 0) 4814 outs() << " + " << format("0x%" PRIx64, c.name); 4815 } else 4816 outs() << format("0x%" PRIx64, c.name); 4817 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info); 4818 if (name != nullptr) 4819 outs() << format(" %.*s", left, name); 4820 outs() << "\n"; 4821 4822 outs() << " cls "; 4823 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info, 4824 n_value, c.cls); 4825 if (n_value != 0) { 4826 if (info->verbose && sym_name != nullptr) 4827 outs() << sym_name; 4828 else 4829 outs() << format("0x%" PRIx64, n_value); 4830 if (c.cls != 0) 4831 outs() << " + " << format("0x%" PRIx64, c.cls); 4832 } else 4833 outs() << format("0x%" PRIx64, c.cls); 4834 outs() << "\n"; 4835 if (c.cls + n_value != 0) 4836 print_class64_t(c.cls + n_value, info); 4837 4838 outs() << " instanceMethods "; 4839 sym_name = 4840 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S, 4841 info, n_value, c.instanceMethods); 4842 if (n_value != 0) { 4843 if (info->verbose && sym_name != nullptr) 4844 outs() << sym_name; 4845 else 4846 outs() << format("0x%" PRIx64, n_value); 4847 if (c.instanceMethods != 0) 4848 outs() << " + " << format("0x%" PRIx64, c.instanceMethods); 4849 } else 4850 outs() << format("0x%" PRIx64, c.instanceMethods); 4851 outs() << "\n"; 4852 if (c.instanceMethods + n_value != 0) 4853 print_method_list64_t(c.instanceMethods + n_value, info, ""); 4854 4855 outs() << " classMethods "; 4856 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods), 4857 S, info, n_value, c.classMethods); 4858 if (n_value != 0) { 4859 if (info->verbose && sym_name != nullptr) 4860 outs() << sym_name; 4861 else 4862 outs() << format("0x%" PRIx64, n_value); 4863 if (c.classMethods != 0) 4864 outs() << " + " << format("0x%" PRIx64, c.classMethods); 4865 } else 4866 outs() << format("0x%" PRIx64, c.classMethods); 4867 outs() << "\n"; 4868 if (c.classMethods + n_value != 0) 4869 print_method_list64_t(c.classMethods + n_value, info, ""); 4870 4871 outs() << " protocols "; 4872 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S, 4873 info, n_value, c.protocols); 4874 if (n_value != 0) { 4875 if (info->verbose && sym_name != nullptr) 4876 outs() << sym_name; 4877 else 4878 outs() << format("0x%" PRIx64, n_value); 4879 if (c.protocols != 0) 4880 outs() << " + " << format("0x%" PRIx64, c.protocols); 4881 } else 4882 outs() << format("0x%" PRIx64, c.protocols); 4883 outs() << "\n"; 4884 if (c.protocols + n_value != 0) 4885 print_protocol_list64_t(c.protocols + n_value, info); 4886 4887 outs() << "instanceProperties "; 4888 sym_name = 4889 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties), 4890 S, info, n_value, c.instanceProperties); 4891 if (n_value != 0) { 4892 if (info->verbose && sym_name != nullptr) 4893 outs() << sym_name; 4894 else 4895 outs() << format("0x%" PRIx64, n_value); 4896 if (c.instanceProperties != 0) 4897 outs() << " + " << format("0x%" PRIx64, c.instanceProperties); 4898 } else 4899 outs() << format("0x%" PRIx64, c.instanceProperties); 4900 outs() << "\n"; 4901 if (c.instanceProperties + n_value != 0) 4902 print_objc_property_list64(c.instanceProperties + n_value, info); 4903 } 4904 4905 static void print_category32_t(uint32_t p, struct DisassembleInfo *info) { 4906 struct category32_t c; 4907 const char *r; 4908 uint32_t offset, left; 4909 SectionRef S, xS; 4910 const char *name; 4911 4912 r = get_pointer_32(p, offset, left, S, info); 4913 if (r == nullptr) 4914 return; 4915 memset(&c, '\0', sizeof(struct category32_t)); 4916 if (left < sizeof(struct category32_t)) { 4917 memcpy(&c, r, left); 4918 outs() << " (category_t entends past the end of the section)\n"; 4919 } else 4920 memcpy(&c, r, sizeof(struct category32_t)); 4921 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4922 swapStruct(c); 4923 4924 outs() << " name " << format("0x%" PRIx32, c.name); 4925 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info, 4926 c.name); 4927 if (name != NULL) 4928 outs() << " " << name; 4929 outs() << "\n"; 4930 4931 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n"; 4932 if (c.cls != 0) 4933 print_class32_t(c.cls, info); 4934 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods) 4935 << "\n"; 4936 if (c.instanceMethods != 0) 4937 print_method_list32_t(c.instanceMethods, info, ""); 4938 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods) 4939 << "\n"; 4940 if (c.classMethods != 0) 4941 print_method_list32_t(c.classMethods, info, ""); 4942 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n"; 4943 if (c.protocols != 0) 4944 print_protocol_list32_t(c.protocols, info); 4945 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties) 4946 << "\n"; 4947 if (c.instanceProperties != 0) 4948 print_objc_property_list32(c.instanceProperties, info); 4949 } 4950 4951 static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) { 4952 uint32_t i, left, offset, xoffset; 4953 uint64_t p, n_value; 4954 struct message_ref64 mr; 4955 const char *name, *sym_name; 4956 const char *r; 4957 SectionRef xS; 4958 4959 if (S == SectionRef()) 4960 return; 4961 4962 StringRef SectName; 4963 S.getName(SectName); 4964 DataRefImpl Ref = S.getRawDataRefImpl(); 4965 StringRef SegName = info->O->getSectionFinalSegmentName(Ref); 4966 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 4967 offset = 0; 4968 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) { 4969 p = S.getAddress() + i; 4970 r = get_pointer_64(p, offset, left, S, info); 4971 if (r == nullptr) 4972 return; 4973 memset(&mr, '\0', sizeof(struct message_ref64)); 4974 if (left < sizeof(struct message_ref64)) { 4975 memcpy(&mr, r, left); 4976 outs() << " (message_ref entends past the end of the section)\n"; 4977 } else 4978 memcpy(&mr, r, sizeof(struct message_ref64)); 4979 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 4980 swapStruct(mr); 4981 4982 outs() << " imp "; 4983 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info, 4984 n_value, mr.imp); 4985 if (n_value != 0) { 4986 outs() << format("0x%" PRIx64, n_value) << " "; 4987 if (mr.imp != 0) 4988 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " "; 4989 } else 4990 outs() << format("0x%" PRIx64, mr.imp) << " "; 4991 if (name != nullptr) 4992 outs() << " " << name; 4993 outs() << "\n"; 4994 4995 outs() << " sel "; 4996 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S, 4997 info, n_value, mr.sel); 4998 if (n_value != 0) { 4999 if (info->verbose && sym_name != nullptr) 5000 outs() << sym_name; 5001 else 5002 outs() << format("0x%" PRIx64, n_value); 5003 if (mr.sel != 0) 5004 outs() << " + " << format("0x%" PRIx64, mr.sel); 5005 } else 5006 outs() << format("0x%" PRIx64, mr.sel); 5007 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info); 5008 if (name != nullptr) 5009 outs() << format(" %.*s", left, name); 5010 outs() << "\n"; 5011 5012 offset += sizeof(struct message_ref64); 5013 } 5014 } 5015 5016 static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) { 5017 uint32_t i, left, offset, xoffset, p; 5018 struct message_ref32 mr; 5019 const char *name, *r; 5020 SectionRef xS; 5021 5022 if (S == SectionRef()) 5023 return; 5024 5025 StringRef SectName; 5026 S.getName(SectName); 5027 DataRefImpl Ref = S.getRawDataRefImpl(); 5028 StringRef SegName = info->O->getSectionFinalSegmentName(Ref); 5029 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 5030 offset = 0; 5031 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) { 5032 p = S.getAddress() + i; 5033 r = get_pointer_32(p, offset, left, S, info); 5034 if (r == nullptr) 5035 return; 5036 memset(&mr, '\0', sizeof(struct message_ref32)); 5037 if (left < sizeof(struct message_ref32)) { 5038 memcpy(&mr, r, left); 5039 outs() << " (message_ref entends past the end of the section)\n"; 5040 } else 5041 memcpy(&mr, r, sizeof(struct message_ref32)); 5042 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5043 swapStruct(mr); 5044 5045 outs() << " imp " << format("0x%" PRIx32, mr.imp); 5046 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info, 5047 mr.imp); 5048 if (name != nullptr) 5049 outs() << " " << name; 5050 outs() << "\n"; 5051 5052 outs() << " sel " << format("0x%" PRIx32, mr.sel); 5053 name = get_pointer_32(mr.sel, xoffset, left, xS, info); 5054 if (name != nullptr) 5055 outs() << " " << name; 5056 outs() << "\n"; 5057 5058 offset += sizeof(struct message_ref32); 5059 } 5060 } 5061 5062 static void print_image_info64(SectionRef S, struct DisassembleInfo *info) { 5063 uint32_t left, offset, swift_version; 5064 uint64_t p; 5065 struct objc_image_info64 o; 5066 const char *r; 5067 5068 StringRef SectName; 5069 S.getName(SectName); 5070 DataRefImpl Ref = S.getRawDataRefImpl(); 5071 StringRef SegName = info->O->getSectionFinalSegmentName(Ref); 5072 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 5073 p = S.getAddress(); 5074 r = get_pointer_64(p, offset, left, S, info); 5075 if (r == nullptr) 5076 return; 5077 memset(&o, '\0', sizeof(struct objc_image_info64)); 5078 if (left < sizeof(struct objc_image_info64)) { 5079 memcpy(&o, r, left); 5080 outs() << " (objc_image_info entends past the end of the section)\n"; 5081 } else 5082 memcpy(&o, r, sizeof(struct objc_image_info64)); 5083 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5084 swapStruct(o); 5085 outs() << " version " << o.version << "\n"; 5086 outs() << " flags " << format("0x%" PRIx32, o.flags); 5087 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT) 5088 outs() << " OBJC_IMAGE_IS_REPLACEMENT"; 5089 if (o.flags & OBJC_IMAGE_SUPPORTS_GC) 5090 outs() << " OBJC_IMAGE_SUPPORTS_GC"; 5091 swift_version = (o.flags >> 8) & 0xff; 5092 if (swift_version != 0) { 5093 if (swift_version == 1) 5094 outs() << " Swift 1.0"; 5095 else if (swift_version == 2) 5096 outs() << " Swift 1.1"; 5097 else 5098 outs() << " unknown future Swift version (" << swift_version << ")"; 5099 } 5100 outs() << "\n"; 5101 } 5102 5103 static void print_image_info32(SectionRef S, struct DisassembleInfo *info) { 5104 uint32_t left, offset, swift_version, p; 5105 struct objc_image_info32 o; 5106 const char *r; 5107 5108 StringRef SectName; 5109 S.getName(SectName); 5110 DataRefImpl Ref = S.getRawDataRefImpl(); 5111 StringRef SegName = info->O->getSectionFinalSegmentName(Ref); 5112 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 5113 p = S.getAddress(); 5114 r = get_pointer_32(p, offset, left, S, info); 5115 if (r == nullptr) 5116 return; 5117 memset(&o, '\0', sizeof(struct objc_image_info32)); 5118 if (left < sizeof(struct objc_image_info32)) { 5119 memcpy(&o, r, left); 5120 outs() << " (objc_image_info entends past the end of the section)\n"; 5121 } else 5122 memcpy(&o, r, sizeof(struct objc_image_info32)); 5123 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5124 swapStruct(o); 5125 outs() << " version " << o.version << "\n"; 5126 outs() << " flags " << format("0x%" PRIx32, o.flags); 5127 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT) 5128 outs() << " OBJC_IMAGE_IS_REPLACEMENT"; 5129 if (o.flags & OBJC_IMAGE_SUPPORTS_GC) 5130 outs() << " OBJC_IMAGE_SUPPORTS_GC"; 5131 swift_version = (o.flags >> 8) & 0xff; 5132 if (swift_version != 0) { 5133 if (swift_version == 1) 5134 outs() << " Swift 1.0"; 5135 else if (swift_version == 2) 5136 outs() << " Swift 1.1"; 5137 else 5138 outs() << " unknown future Swift version (" << swift_version << ")"; 5139 } 5140 outs() << "\n"; 5141 } 5142 5143 static void print_image_info(SectionRef S, struct DisassembleInfo *info) { 5144 uint32_t left, offset, p; 5145 struct imageInfo_t o; 5146 const char *r; 5147 5148 StringRef SectName; 5149 S.getName(SectName); 5150 DataRefImpl Ref = S.getRawDataRefImpl(); 5151 StringRef SegName = info->O->getSectionFinalSegmentName(Ref); 5152 outs() << "Contents of (" << SegName << "," << SectName << ") section\n"; 5153 p = S.getAddress(); 5154 r = get_pointer_32(p, offset, left, S, info); 5155 if (r == nullptr) 5156 return; 5157 memset(&o, '\0', sizeof(struct imageInfo_t)); 5158 if (left < sizeof(struct imageInfo_t)) { 5159 memcpy(&o, r, left); 5160 outs() << " (imageInfo entends past the end of the section)\n"; 5161 } else 5162 memcpy(&o, r, sizeof(struct imageInfo_t)); 5163 if (info->O->isLittleEndian() != sys::IsLittleEndianHost) 5164 swapStruct(o); 5165 outs() << " version " << o.version << "\n"; 5166 outs() << " flags " << format("0x%" PRIx32, o.flags); 5167 if (o.flags & 0x1) 5168 outs() << " F&C"; 5169 if (o.flags & 0x2) 5170 outs() << " GC"; 5171 if (o.flags & 0x4) 5172 outs() << " GC-only"; 5173 else 5174 outs() << " RR"; 5175 outs() << "\n"; 5176 } 5177 5178 static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) { 5179 SymbolAddressMap AddrMap; 5180 if (verbose) 5181 CreateSymbolAddressMap(O, &AddrMap); 5182 5183 std::vector<SectionRef> Sections; 5184 for (const SectionRef &Section : O->sections()) { 5185 StringRef SectName; 5186 Section.getName(SectName); 5187 Sections.push_back(Section); 5188 } 5189 5190 struct DisassembleInfo info; 5191 // Set up the block of info used by the Symbolizer call backs. 5192 info.verbose = verbose; 5193 info.O = O; 5194 info.AddrMap = &AddrMap; 5195 info.Sections = &Sections; 5196 info.class_name = nullptr; 5197 info.selector_name = nullptr; 5198 info.method = nullptr; 5199 info.demangled_name = nullptr; 5200 info.bindtable = nullptr; 5201 info.adrp_addr = 0; 5202 info.adrp_inst = 0; 5203 5204 const SectionRef CL = get_section(O, "__OBJC2", "__class_list"); 5205 if (CL != SectionRef()) { 5206 info.S = CL; 5207 walk_pointer_list_64("class", CL, O, &info, print_class64_t); 5208 } else { 5209 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist"); 5210 info.S = CL; 5211 walk_pointer_list_64("class", CL, O, &info, print_class64_t); 5212 } 5213 5214 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs"); 5215 if (CR != SectionRef()) { 5216 info.S = CR; 5217 walk_pointer_list_64("class refs", CR, O, &info, nullptr); 5218 } else { 5219 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs"); 5220 info.S = CR; 5221 walk_pointer_list_64("class refs", CR, O, &info, nullptr); 5222 } 5223 5224 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs"); 5225 if (SR != SectionRef()) { 5226 info.S = SR; 5227 walk_pointer_list_64("super refs", SR, O, &info, nullptr); 5228 } else { 5229 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs"); 5230 info.S = SR; 5231 walk_pointer_list_64("super refs", SR, O, &info, nullptr); 5232 } 5233 5234 const SectionRef CA = get_section(O, "__OBJC2", "__category_list"); 5235 if (CA != SectionRef()) { 5236 info.S = CA; 5237 walk_pointer_list_64("category", CA, O, &info, print_category64_t); 5238 } else { 5239 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist"); 5240 info.S = CA; 5241 walk_pointer_list_64("category", CA, O, &info, print_category64_t); 5242 } 5243 5244 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list"); 5245 if (PL != SectionRef()) { 5246 info.S = PL; 5247 walk_pointer_list_64("protocol", PL, O, &info, nullptr); 5248 } else { 5249 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist"); 5250 info.S = PL; 5251 walk_pointer_list_64("protocol", PL, O, &info, nullptr); 5252 } 5253 5254 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs"); 5255 if (MR != SectionRef()) { 5256 info.S = MR; 5257 print_message_refs64(MR, &info); 5258 } else { 5259 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs"); 5260 info.S = MR; 5261 print_message_refs64(MR, &info); 5262 } 5263 5264 const SectionRef II = get_section(O, "__OBJC2", "__image_info"); 5265 if (II != SectionRef()) { 5266 info.S = II; 5267 print_image_info64(II, &info); 5268 } else { 5269 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo"); 5270 info.S = II; 5271 print_image_info64(II, &info); 5272 } 5273 5274 if (info.bindtable != nullptr) 5275 delete info.bindtable; 5276 } 5277 5278 static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) { 5279 SymbolAddressMap AddrMap; 5280 if (verbose) 5281 CreateSymbolAddressMap(O, &AddrMap); 5282 5283 std::vector<SectionRef> Sections; 5284 for (const SectionRef &Section : O->sections()) { 5285 StringRef SectName; 5286 Section.getName(SectName); 5287 Sections.push_back(Section); 5288 } 5289 5290 struct DisassembleInfo info; 5291 // Set up the block of info used by the Symbolizer call backs. 5292 info.verbose = verbose; 5293 info.O = O; 5294 info.AddrMap = &AddrMap; 5295 info.Sections = &Sections; 5296 info.class_name = nullptr; 5297 info.selector_name = nullptr; 5298 info.method = nullptr; 5299 info.demangled_name = nullptr; 5300 info.bindtable = nullptr; 5301 info.adrp_addr = 0; 5302 info.adrp_inst = 0; 5303 5304 const SectionRef CL = get_section(O, "__OBJC2", "__class_list"); 5305 if (CL != SectionRef()) { 5306 info.S = CL; 5307 walk_pointer_list_32("class", CL, O, &info, print_class32_t); 5308 } else { 5309 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist"); 5310 info.S = CL; 5311 walk_pointer_list_32("class", CL, O, &info, print_class32_t); 5312 } 5313 5314 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs"); 5315 if (CR != SectionRef()) { 5316 info.S = CR; 5317 walk_pointer_list_32("class refs", CR, O, &info, nullptr); 5318 } else { 5319 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs"); 5320 info.S = CR; 5321 walk_pointer_list_32("class refs", CR, O, &info, nullptr); 5322 } 5323 5324 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs"); 5325 if (SR != SectionRef()) { 5326 info.S = SR; 5327 walk_pointer_list_32("super refs", SR, O, &info, nullptr); 5328 } else { 5329 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs"); 5330 info.S = SR; 5331 walk_pointer_list_32("super refs", SR, O, &info, nullptr); 5332 } 5333 5334 const SectionRef CA = get_section(O, "__OBJC2", "__category_list"); 5335 if (CA != SectionRef()) { 5336 info.S = CA; 5337 walk_pointer_list_32("category", CA, O, &info, print_category32_t); 5338 } else { 5339 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist"); 5340 info.S = CA; 5341 walk_pointer_list_32("category", CA, O, &info, print_category32_t); 5342 } 5343 5344 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list"); 5345 if (PL != SectionRef()) { 5346 info.S = PL; 5347 walk_pointer_list_32("protocol", PL, O, &info, nullptr); 5348 } else { 5349 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist"); 5350 info.S = PL; 5351 walk_pointer_list_32("protocol", PL, O, &info, nullptr); 5352 } 5353 5354 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs"); 5355 if (MR != SectionRef()) { 5356 info.S = MR; 5357 print_message_refs32(MR, &info); 5358 } else { 5359 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs"); 5360 info.S = MR; 5361 print_message_refs32(MR, &info); 5362 } 5363 5364 const SectionRef II = get_section(O, "__OBJC2", "__image_info"); 5365 if (II != SectionRef()) { 5366 info.S = II; 5367 print_image_info32(II, &info); 5368 } else { 5369 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo"); 5370 info.S = II; 5371 print_image_info32(II, &info); 5372 } 5373 } 5374 5375 static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) { 5376 uint32_t i, j, p, offset, xoffset, left, defs_left, def; 5377 const char *r, *name, *defs; 5378 struct objc_module_t module; 5379 SectionRef S, xS; 5380 struct objc_symtab_t symtab; 5381 struct objc_class_t objc_class; 5382 struct objc_category_t objc_category; 5383 5384 outs() << "Objective-C segment\n"; 5385 S = get_section(O, "__OBJC", "__module_info"); 5386 if (S == SectionRef()) 5387 return false; 5388 5389 SymbolAddressMap AddrMap; 5390 if (verbose) 5391 CreateSymbolAddressMap(O, &AddrMap); 5392 5393 std::vector<SectionRef> Sections; 5394 for (const SectionRef &Section : O->sections()) { 5395 StringRef SectName; 5396 Section.getName(SectName); 5397 Sections.push_back(Section); 5398 } 5399 5400 struct DisassembleInfo info; 5401 // Set up the block of info used by the Symbolizer call backs. 5402 info.verbose = verbose; 5403 info.O = O; 5404 info.AddrMap = &AddrMap; 5405 info.Sections = &Sections; 5406 info.class_name = nullptr; 5407 info.selector_name = nullptr; 5408 info.method = nullptr; 5409 info.demangled_name = nullptr; 5410 info.bindtable = nullptr; 5411 info.adrp_addr = 0; 5412 info.adrp_inst = 0; 5413 5414 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) { 5415 p = S.getAddress() + i; 5416 r = get_pointer_32(p, offset, left, S, &info, true); 5417 if (r == nullptr) 5418 return true; 5419 memset(&module, '\0', sizeof(struct objc_module_t)); 5420 if (left < sizeof(struct objc_module_t)) { 5421 memcpy(&module, r, left); 5422 outs() << " (module extends past end of __module_info section)\n"; 5423 } else 5424 memcpy(&module, r, sizeof(struct objc_module_t)); 5425 if (O->isLittleEndian() != sys::IsLittleEndianHost) 5426 swapStruct(module); 5427 5428 outs() << "Module " << format("0x%" PRIx32, p) << "\n"; 5429 outs() << " version " << module.version << "\n"; 5430 outs() << " size " << module.size << "\n"; 5431 outs() << " name "; 5432 name = get_pointer_32(module.name, xoffset, left, xS, &info, true); 5433 if (name != nullptr) 5434 outs() << format("%.*s", left, name); 5435 else 5436 outs() << format("0x%08" PRIx32, module.name) 5437 << "(not in an __OBJC section)"; 5438 outs() << "\n"; 5439 5440 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true); 5441 if (module.symtab == 0 || r == nullptr) { 5442 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) 5443 << " (not in an __OBJC section)\n"; 5444 continue; 5445 } 5446 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n"; 5447 memset(&symtab, '\0', sizeof(struct objc_symtab_t)); 5448 defs_left = 0; 5449 defs = nullptr; 5450 if (left < sizeof(struct objc_symtab_t)) { 5451 memcpy(&symtab, r, left); 5452 outs() << "\tsymtab extends past end of an __OBJC section)\n"; 5453 } else { 5454 memcpy(&symtab, r, sizeof(struct objc_symtab_t)); 5455 if (left > sizeof(struct objc_symtab_t)) { 5456 defs_left = left - sizeof(struct objc_symtab_t); 5457 defs = r + sizeof(struct objc_symtab_t); 5458 } 5459 } 5460 if (O->isLittleEndian() != sys::IsLittleEndianHost) 5461 swapStruct(symtab); 5462 5463 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n"; 5464 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true); 5465 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs); 5466 if (r == nullptr) 5467 outs() << " (not in an __OBJC section)"; 5468 outs() << "\n"; 5469 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n"; 5470 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n"; 5471 if (symtab.cls_def_cnt > 0) 5472 outs() << "\tClass Definitions\n"; 5473 for (j = 0; j < symtab.cls_def_cnt; j++) { 5474 if ((j + 1) * sizeof(uint32_t) > defs_left) { 5475 outs() << "\t(remaining class defs entries entends past the end of the " 5476 << "section)\n"; 5477 break; 5478 } 5479 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t)); 5480 if (O->isLittleEndian() != sys::IsLittleEndianHost) 5481 sys::swapByteOrder(def); 5482 5483 r = get_pointer_32(def, xoffset, left, xS, &info, true); 5484 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def); 5485 if (r != nullptr) { 5486 if (left > sizeof(struct objc_class_t)) { 5487 outs() << "\n"; 5488 memcpy(&objc_class, r, sizeof(struct objc_class_t)); 5489 } else { 5490 outs() << " (entends past the end of the section)\n"; 5491 memset(&objc_class, '\0', sizeof(struct objc_class_t)); 5492 memcpy(&objc_class, r, left); 5493 } 5494 if (O->isLittleEndian() != sys::IsLittleEndianHost) 5495 swapStruct(objc_class); 5496 print_objc_class_t(&objc_class, &info); 5497 } else { 5498 outs() << "(not in an __OBJC section)\n"; 5499 } 5500 5501 if (CLS_GETINFO(&objc_class, CLS_CLASS)) { 5502 outs() << "\tMeta Class"; 5503 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true); 5504 if (r != nullptr) { 5505 if (left > sizeof(struct objc_class_t)) { 5506 outs() << "\n"; 5507 memcpy(&objc_class, r, sizeof(struct objc_class_t)); 5508 } else { 5509 outs() << " (entends past the end of the section)\n"; 5510 memset(&objc_class, '\0', sizeof(struct objc_class_t)); 5511 memcpy(&objc_class, r, left); 5512 } 5513 if (O->isLittleEndian() != sys::IsLittleEndianHost) 5514 swapStruct(objc_class); 5515 print_objc_class_t(&objc_class, &info); 5516 } else { 5517 outs() << "(not in an __OBJC section)\n"; 5518 } 5519 } 5520 } 5521 if (symtab.cat_def_cnt > 0) 5522 outs() << "\tCategory Definitions\n"; 5523 for (j = 0; j < symtab.cat_def_cnt; j++) { 5524 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) { 5525 outs() << "\t(remaining category defs entries entends past the end of " 5526 << "the section)\n"; 5527 break; 5528 } 5529 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t), 5530 sizeof(uint32_t)); 5531 if (O->isLittleEndian() != sys::IsLittleEndianHost) 5532 sys::swapByteOrder(def); 5533 5534 r = get_pointer_32(def, xoffset, left, xS, &info, true); 5535 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] " 5536 << format("0x%08" PRIx32, def); 5537 if (r != nullptr) { 5538 if (left > sizeof(struct objc_category_t)) { 5539 outs() << "\n"; 5540 memcpy(&objc_category, r, sizeof(struct objc_category_t)); 5541 } else { 5542 outs() << " (entends past the end of the section)\n"; 5543 memset(&objc_category, '\0', sizeof(struct objc_category_t)); 5544 memcpy(&objc_category, r, left); 5545 } 5546 if (O->isLittleEndian() != sys::IsLittleEndianHost) 5547 swapStruct(objc_category); 5548 print_objc_objc_category_t(&objc_category, &info); 5549 } else { 5550 outs() << "(not in an __OBJC section)\n"; 5551 } 5552 } 5553 } 5554 const SectionRef II = get_section(O, "__OBJC", "__image_info"); 5555 if (II != SectionRef()) 5556 print_image_info(II, &info); 5557 5558 return true; 5559 } 5560 5561 static void DumpProtocolSection(MachOObjectFile *O, const char *sect, 5562 uint32_t size, uint32_t addr) { 5563 SymbolAddressMap AddrMap; 5564 CreateSymbolAddressMap(O, &AddrMap); 5565 5566 std::vector<SectionRef> Sections; 5567 for (const SectionRef &Section : O->sections()) { 5568 StringRef SectName; 5569 Section.getName(SectName); 5570 Sections.push_back(Section); 5571 } 5572 5573 struct DisassembleInfo info; 5574 // Set up the block of info used by the Symbolizer call backs. 5575 info.verbose = true; 5576 info.O = O; 5577 info.AddrMap = &AddrMap; 5578 info.Sections = &Sections; 5579 info.class_name = nullptr; 5580 info.selector_name = nullptr; 5581 info.method = nullptr; 5582 info.demangled_name = nullptr; 5583 info.bindtable = nullptr; 5584 info.adrp_addr = 0; 5585 info.adrp_inst = 0; 5586 5587 const char *p; 5588 struct objc_protocol_t protocol; 5589 uint32_t left, paddr; 5590 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) { 5591 memset(&protocol, '\0', sizeof(struct objc_protocol_t)); 5592 left = size - (p - sect); 5593 if (left < sizeof(struct objc_protocol_t)) { 5594 outs() << "Protocol extends past end of __protocol section\n"; 5595 memcpy(&protocol, p, left); 5596 } else 5597 memcpy(&protocol, p, sizeof(struct objc_protocol_t)); 5598 if (O->isLittleEndian() != sys::IsLittleEndianHost) 5599 swapStruct(protocol); 5600 paddr = addr + (p - sect); 5601 outs() << "Protocol " << format("0x%" PRIx32, paddr); 5602 if (print_protocol(paddr, 0, &info)) 5603 outs() << "(not in an __OBJC section)\n"; 5604 } 5605 } 5606 5607 static void printObjcMetaData(MachOObjectFile *O, bool verbose) { 5608 if (O->is64Bit()) 5609 printObjc2_64bit_MetaData(O, verbose); 5610 else { 5611 MachO::mach_header H; 5612 H = O->getHeader(); 5613 if (H.cputype == MachO::CPU_TYPE_ARM) 5614 printObjc2_32bit_MetaData(O, verbose); 5615 else { 5616 // This is the 32-bit non-arm cputype case. Which is normally 5617 // the first Objective-C ABI. But it may be the case of a 5618 // binary for the iOS simulator which is the second Objective-C 5619 // ABI. In that case printObjc1_32bit_MetaData() will determine that 5620 // and return false. 5621 if (printObjc1_32bit_MetaData(O, verbose) == false) 5622 printObjc2_32bit_MetaData(O, verbose); 5623 } 5624 } 5625 } 5626 5627 // GuessLiteralPointer returns a string which for the item in the Mach-O file 5628 // for the address passed in as ReferenceValue for printing as a comment with 5629 // the instruction and also returns the corresponding type of that item 5630 // indirectly through ReferenceType. 5631 // 5632 // If ReferenceValue is an address of literal cstring then a pointer to the 5633 // cstring is returned and ReferenceType is set to 5634 // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr . 5635 // 5636 // If ReferenceValue is an address of an Objective-C CFString, Selector ref or 5637 // Class ref that name is returned and the ReferenceType is set accordingly. 5638 // 5639 // Lastly, literals which are Symbol address in a literal pool are looked for 5640 // and if found the symbol name is returned and ReferenceType is set to 5641 // LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr . 5642 // 5643 // If there is no item in the Mach-O file for the address passed in as 5644 // ReferenceValue nullptr is returned and ReferenceType is unchanged. 5645 static const char *GuessLiteralPointer(uint64_t ReferenceValue, 5646 uint64_t ReferencePC, 5647 uint64_t *ReferenceType, 5648 struct DisassembleInfo *info) { 5649 // First see if there is an external relocation entry at the ReferencePC. 5650 uint64_t sect_addr = info->S.getAddress(); 5651 uint64_t sect_offset = ReferencePC - sect_addr; 5652 bool reloc_found = false; 5653 DataRefImpl Rel; 5654 MachO::any_relocation_info RE; 5655 bool isExtern = false; 5656 SymbolRef Symbol; 5657 for (const RelocationRef &Reloc : info->S.relocations()) { 5658 uint64_t RelocOffset; 5659 Reloc.getOffset(RelocOffset); 5660 if (RelocOffset == sect_offset) { 5661 Rel = Reloc.getRawDataRefImpl(); 5662 RE = info->O->getRelocation(Rel); 5663 if (info->O->isRelocationScattered(RE)) 5664 continue; 5665 isExtern = info->O->getPlainRelocationExternal(RE); 5666 if (isExtern) { 5667 symbol_iterator RelocSym = Reloc.getSymbol(); 5668 Symbol = *RelocSym; 5669 } 5670 reloc_found = true; 5671 break; 5672 } 5673 } 5674 // If there is an external relocation entry for a symbol in a section 5675 // then used that symbol's value for the value of the reference. 5676 if (reloc_found && isExtern) { 5677 if (info->O->getAnyRelocationPCRel(RE)) { 5678 unsigned Type = info->O->getAnyRelocationType(RE); 5679 if (Type == MachO::X86_64_RELOC_SIGNED) { 5680 Symbol.getAddress(ReferenceValue); 5681 } 5682 } 5683 } 5684 5685 // Look for literals such as Objective-C CFStrings refs, Selector refs, 5686 // Message refs and Class refs. 5687 bool classref, selref, msgref, cfstring; 5688 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref, 5689 selref, msgref, cfstring); 5690 if (classref && pointer_value == 0) { 5691 // Note the ReferenceValue is a pointer into the __objc_classrefs section. 5692 // And the pointer_value in that section is typically zero as it will be 5693 // set by dyld as part of the "bind information". 5694 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info); 5695 if (name != nullptr) { 5696 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref; 5697 const char *class_name = strrchr(name, '$'); 5698 if (class_name != nullptr && class_name[1] == '_' && 5699 class_name[2] != '\0') { 5700 info->class_name = class_name + 2; 5701 return name; 5702 } 5703 } 5704 } 5705 5706 if (classref) { 5707 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref; 5708 const char *name = 5709 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info); 5710 if (name != nullptr) 5711 info->class_name = name; 5712 else 5713 name = "bad class ref"; 5714 return name; 5715 } 5716 5717 if (cfstring) { 5718 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref; 5719 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info); 5720 return name; 5721 } 5722 5723 if (selref && pointer_value == 0) 5724 pointer_value = get_objc2_64bit_selref(ReferenceValue, info); 5725 5726 if (pointer_value != 0) 5727 ReferenceValue = pointer_value; 5728 5729 const char *name = GuessCstringPointer(ReferenceValue, info); 5730 if (name) { 5731 if (pointer_value != 0 && selref) { 5732 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref; 5733 info->selector_name = name; 5734 } else if (pointer_value != 0 && msgref) { 5735 info->class_name = nullptr; 5736 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref; 5737 info->selector_name = name; 5738 } else 5739 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr; 5740 return name; 5741 } 5742 5743 // Lastly look for an indirect symbol with this ReferenceValue which is in 5744 // a literal pool. If found return that symbol name. 5745 name = GuessIndirectSymbol(ReferenceValue, info); 5746 if (name) { 5747 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr; 5748 return name; 5749 } 5750 5751 return nullptr; 5752 } 5753 5754 // SymbolizerSymbolLookUp is the symbol lookup function passed when creating 5755 // the Symbolizer. It looks up the ReferenceValue using the info passed via the 5756 // pointer to the struct DisassembleInfo that was passed when MCSymbolizer 5757 // is created and returns the symbol name that matches the ReferenceValue or 5758 // nullptr if none. The ReferenceType is passed in for the IN type of 5759 // reference the instruction is making from the values in defined in the header 5760 // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific 5761 // Out type and the ReferenceName will also be set which is added as a comment 5762 // to the disassembled instruction. 5763 // 5764 #if HAVE_CXXABI_H 5765 // If the symbol name is a C++ mangled name then the demangled name is 5766 // returned through ReferenceName and ReferenceType is set to 5767 // LLVMDisassembler_ReferenceType_DeMangled_Name . 5768 #endif 5769 // 5770 // When this is called to get a symbol name for a branch target then the 5771 // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then 5772 // SymbolValue will be looked for in the indirect symbol table to determine if 5773 // it is an address for a symbol stub. If so then the symbol name for that 5774 // stub is returned indirectly through ReferenceName and then ReferenceType is 5775 // set to LLVMDisassembler_ReferenceType_Out_SymbolStub. 5776 // 5777 // When this is called with an value loaded via a PC relative load then 5778 // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the 5779 // SymbolValue is checked to be an address of literal pointer, symbol pointer, 5780 // or an Objective-C meta data reference. If so the output ReferenceType is 5781 // set to correspond to that as well as setting the ReferenceName. 5782 static const char *SymbolizerSymbolLookUp(void *DisInfo, 5783 uint64_t ReferenceValue, 5784 uint64_t *ReferenceType, 5785 uint64_t ReferencePC, 5786 const char **ReferenceName) { 5787 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo; 5788 // If no verbose symbolic information is wanted then just return nullptr. 5789 if (!info->verbose) { 5790 *ReferenceName = nullptr; 5791 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 5792 return nullptr; 5793 } 5794 5795 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap); 5796 5797 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) { 5798 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info); 5799 if (*ReferenceName != nullptr) { 5800 method_reference(info, ReferenceType, ReferenceName); 5801 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message) 5802 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub; 5803 } else 5804 #if HAVE_CXXABI_H 5805 if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) { 5806 if (info->demangled_name != nullptr) 5807 free(info->demangled_name); 5808 int status; 5809 info->demangled_name = 5810 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status); 5811 if (info->demangled_name != nullptr) { 5812 *ReferenceName = info->demangled_name; 5813 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name; 5814 } else 5815 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 5816 } else 5817 #endif 5818 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 5819 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) { 5820 *ReferenceName = 5821 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info); 5822 if (*ReferenceName) 5823 method_reference(info, ReferenceType, ReferenceName); 5824 else 5825 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 5826 // If this is arm64 and the reference is an adrp instruction save the 5827 // instruction, passed in ReferenceValue and the address of the instruction 5828 // for use later if we see and add immediate instruction. 5829 } else if (info->O->getArch() == Triple::aarch64 && 5830 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) { 5831 info->adrp_inst = ReferenceValue; 5832 info->adrp_addr = ReferencePC; 5833 SymbolName = nullptr; 5834 *ReferenceName = nullptr; 5835 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 5836 // If this is arm64 and reference is an add immediate instruction and we 5837 // have 5838 // seen an adrp instruction just before it and the adrp's Xd register 5839 // matches 5840 // this add's Xn register reconstruct the value being referenced and look to 5841 // see if it is a literal pointer. Note the add immediate instruction is 5842 // passed in ReferenceValue. 5843 } else if (info->O->getArch() == Triple::aarch64 && 5844 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri && 5845 ReferencePC - 4 == info->adrp_addr && 5846 (info->adrp_inst & 0x9f000000) == 0x90000000 && 5847 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) { 5848 uint32_t addxri_inst; 5849 uint64_t adrp_imm, addxri_imm; 5850 5851 adrp_imm = 5852 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3); 5853 if (info->adrp_inst & 0x0200000) 5854 adrp_imm |= 0xfffffffffc000000LL; 5855 5856 addxri_inst = ReferenceValue; 5857 addxri_imm = (addxri_inst >> 10) & 0xfff; 5858 if (((addxri_inst >> 22) & 0x3) == 1) 5859 addxri_imm <<= 12; 5860 5861 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) + 5862 (adrp_imm << 12) + addxri_imm; 5863 5864 *ReferenceName = 5865 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info); 5866 if (*ReferenceName == nullptr) 5867 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 5868 // If this is arm64 and the reference is a load register instruction and we 5869 // have seen an adrp instruction just before it and the adrp's Xd register 5870 // matches this add's Xn register reconstruct the value being referenced and 5871 // look to see if it is a literal pointer. Note the load register 5872 // instruction is passed in ReferenceValue. 5873 } else if (info->O->getArch() == Triple::aarch64 && 5874 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui && 5875 ReferencePC - 4 == info->adrp_addr && 5876 (info->adrp_inst & 0x9f000000) == 0x90000000 && 5877 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) { 5878 uint32_t ldrxui_inst; 5879 uint64_t adrp_imm, ldrxui_imm; 5880 5881 adrp_imm = 5882 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3); 5883 if (info->adrp_inst & 0x0200000) 5884 adrp_imm |= 0xfffffffffc000000LL; 5885 5886 ldrxui_inst = ReferenceValue; 5887 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff; 5888 5889 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) + 5890 (adrp_imm << 12) + (ldrxui_imm << 3); 5891 5892 *ReferenceName = 5893 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info); 5894 if (*ReferenceName == nullptr) 5895 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 5896 } 5897 // If this arm64 and is an load register (PC-relative) instruction the 5898 // ReferenceValue is the PC plus the immediate value. 5899 else if (info->O->getArch() == Triple::aarch64 && 5900 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl || 5901 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) { 5902 *ReferenceName = 5903 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info); 5904 if (*ReferenceName == nullptr) 5905 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 5906 } 5907 #if HAVE_CXXABI_H 5908 else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) { 5909 if (info->demangled_name != nullptr) 5910 free(info->demangled_name); 5911 int status; 5912 info->demangled_name = 5913 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status); 5914 if (info->demangled_name != nullptr) { 5915 *ReferenceName = info->demangled_name; 5916 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name; 5917 } 5918 } 5919 #endif 5920 else { 5921 *ReferenceName = nullptr; 5922 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; 5923 } 5924 5925 return SymbolName; 5926 } 5927 5928 /// \brief Emits the comments that are stored in the CommentStream. 5929 /// Each comment in the CommentStream must end with a newline. 5930 static void emitComments(raw_svector_ostream &CommentStream, 5931 SmallString<128> &CommentsToEmit, 5932 formatted_raw_ostream &FormattedOS, 5933 const MCAsmInfo &MAI) { 5934 // Flush the stream before taking its content. 5935 CommentStream.flush(); 5936 StringRef Comments = CommentsToEmit.str(); 5937 // Get the default information for printing a comment. 5938 const char *CommentBegin = MAI.getCommentString(); 5939 unsigned CommentColumn = MAI.getCommentColumn(); 5940 bool IsFirst = true; 5941 while (!Comments.empty()) { 5942 if (!IsFirst) 5943 FormattedOS << '\n'; 5944 // Emit a line of comments. 5945 FormattedOS.PadToColumn(CommentColumn); 5946 size_t Position = Comments.find('\n'); 5947 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position); 5948 // Move after the newline character. 5949 Comments = Comments.substr(Position + 1); 5950 IsFirst = false; 5951 } 5952 FormattedOS.flush(); 5953 5954 // Tell the comment stream that the vector changed underneath it. 5955 CommentsToEmit.clear(); 5956 CommentStream.resync(); 5957 } 5958 5959 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, 5960 StringRef DisSegName, StringRef DisSectName) { 5961 const char *McpuDefault = nullptr; 5962 const Target *ThumbTarget = nullptr; 5963 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget); 5964 if (!TheTarget) { 5965 // GetTarget prints out stuff. 5966 return; 5967 } 5968 if (MCPU.empty() && McpuDefault) 5969 MCPU = McpuDefault; 5970 5971 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo()); 5972 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo; 5973 if (ThumbTarget) 5974 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo()); 5975 5976 // Package up features to be passed to target/subtarget 5977 std::string FeaturesStr; 5978 if (MAttrs.size()) { 5979 SubtargetFeatures Features; 5980 for (unsigned i = 0; i != MAttrs.size(); ++i) 5981 Features.AddFeature(MAttrs[i]); 5982 FeaturesStr = Features.getString(); 5983 } 5984 5985 // Set up disassembler. 5986 std::unique_ptr<const MCRegisterInfo> MRI( 5987 TheTarget->createMCRegInfo(TripleName)); 5988 std::unique_ptr<const MCAsmInfo> AsmInfo( 5989 TheTarget->createMCAsmInfo(*MRI, TripleName)); 5990 std::unique_ptr<const MCSubtargetInfo> STI( 5991 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr)); 5992 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr); 5993 std::unique_ptr<MCDisassembler> DisAsm( 5994 TheTarget->createMCDisassembler(*STI, Ctx)); 5995 std::unique_ptr<MCSymbolizer> Symbolizer; 5996 struct DisassembleInfo SymbolizerInfo; 5997 std::unique_ptr<MCRelocationInfo> RelInfo( 5998 TheTarget->createMCRelocationInfo(TripleName, Ctx)); 5999 if (RelInfo) { 6000 Symbolizer.reset(TheTarget->createMCSymbolizer( 6001 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp, 6002 &SymbolizerInfo, &Ctx, std::move(RelInfo))); 6003 DisAsm->setSymbolizer(std::move(Symbolizer)); 6004 } 6005 int AsmPrinterVariant = AsmInfo->getAssemblerDialect(); 6006 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter( 6007 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI)); 6008 // Set the display preference for hex vs. decimal immediates. 6009 IP->setPrintImmHex(PrintImmHex); 6010 // Comment stream and backing vector. 6011 SmallString<128> CommentsToEmit; 6012 raw_svector_ostream CommentStream(CommentsToEmit); 6013 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that 6014 // if it is done then arm64 comments for string literals don't get printed 6015 // and some constant get printed instead and not setting it causes intel 6016 // (32-bit and 64-bit) comments printed with different spacing before the 6017 // comment causing different diffs with the 'C' disassembler library API. 6018 // IP->setCommentStream(CommentStream); 6019 6020 if (!AsmInfo || !STI || !DisAsm || !IP) { 6021 errs() << "error: couldn't initialize disassembler for target " 6022 << TripleName << '\n'; 6023 return; 6024 } 6025 6026 // Set up thumb disassembler. 6027 std::unique_ptr<const MCRegisterInfo> ThumbMRI; 6028 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo; 6029 std::unique_ptr<const MCSubtargetInfo> ThumbSTI; 6030 std::unique_ptr<MCDisassembler> ThumbDisAsm; 6031 std::unique_ptr<MCInstPrinter> ThumbIP; 6032 std::unique_ptr<MCContext> ThumbCtx; 6033 std::unique_ptr<MCSymbolizer> ThumbSymbolizer; 6034 struct DisassembleInfo ThumbSymbolizerInfo; 6035 std::unique_ptr<MCRelocationInfo> ThumbRelInfo; 6036 if (ThumbTarget) { 6037 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName)); 6038 ThumbAsmInfo.reset( 6039 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName)); 6040 ThumbSTI.reset( 6041 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr)); 6042 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr)); 6043 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx)); 6044 MCContext *PtrThumbCtx = ThumbCtx.get(); 6045 ThumbRelInfo.reset( 6046 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx)); 6047 if (ThumbRelInfo) { 6048 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer( 6049 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp, 6050 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo))); 6051 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer)); 6052 } 6053 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect(); 6054 ThumbIP.reset(ThumbTarget->createMCInstPrinter( 6055 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo, 6056 *ThumbInstrInfo, *ThumbMRI)); 6057 // Set the display preference for hex vs. decimal immediates. 6058 ThumbIP->setPrintImmHex(PrintImmHex); 6059 } 6060 6061 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) { 6062 errs() << "error: couldn't initialize disassembler for target " 6063 << ThumbTripleName << '\n'; 6064 return; 6065 } 6066 6067 MachO::mach_header Header = MachOOF->getHeader(); 6068 6069 // FIXME: Using the -cfg command line option, this code used to be able to 6070 // annotate relocations with the referenced symbol's name, and if this was 6071 // inside a __[cf]string section, the data it points to. This is now replaced 6072 // by the upcoming MCSymbolizer, which needs the appropriate setup done above. 6073 std::vector<SectionRef> Sections; 6074 std::vector<SymbolRef> Symbols; 6075 SmallVector<uint64_t, 8> FoundFns; 6076 uint64_t BaseSegmentAddress; 6077 6078 getSectionsAndSymbols(Header, MachOOF, Sections, Symbols, FoundFns, 6079 BaseSegmentAddress); 6080 6081 // Sort the symbols by address, just in case they didn't come in that way. 6082 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter()); 6083 6084 // Build a data in code table that is sorted on by the address of each entry. 6085 uint64_t BaseAddress = 0; 6086 if (Header.filetype == MachO::MH_OBJECT) 6087 BaseAddress = Sections[0].getAddress(); 6088 else 6089 BaseAddress = BaseSegmentAddress; 6090 DiceTable Dices; 6091 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices(); 6092 DI != DE; ++DI) { 6093 uint32_t Offset; 6094 DI->getOffset(Offset); 6095 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI)); 6096 } 6097 array_pod_sort(Dices.begin(), Dices.end()); 6098 6099 #ifndef NDEBUG 6100 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls(); 6101 #else 6102 raw_ostream &DebugOut = nulls(); 6103 #endif 6104 6105 std::unique_ptr<DIContext> diContext; 6106 ObjectFile *DbgObj = MachOOF; 6107 // Try to find debug info and set up the DIContext for it. 6108 if (UseDbg) { 6109 // A separate DSym file path was specified, parse it as a macho file, 6110 // get the sections and supply it to the section name parsing machinery. 6111 if (!DSYMFile.empty()) { 6112 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr = 6113 MemoryBuffer::getFileOrSTDIN(DSYMFile); 6114 if (std::error_code EC = BufOrErr.getError()) { 6115 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n'; 6116 return; 6117 } 6118 DbgObj = 6119 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef()) 6120 .get() 6121 .release(); 6122 } 6123 6124 // Setup the DIContext 6125 diContext.reset(new DWARFContextInMemory(*DbgObj)); 6126 } 6127 6128 if (DumpSections.size() == 0) 6129 outs() << "(" << DisSegName << "," << DisSectName << ") section\n"; 6130 6131 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) { 6132 StringRef SectName; 6133 if (Sections[SectIdx].getName(SectName) || SectName != DisSectName) 6134 continue; 6135 6136 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl(); 6137 6138 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR); 6139 if (SegmentName != DisSegName) 6140 continue; 6141 6142 StringRef BytesStr; 6143 Sections[SectIdx].getContents(BytesStr); 6144 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()), 6145 BytesStr.size()); 6146 uint64_t SectAddress = Sections[SectIdx].getAddress(); 6147 6148 bool symbolTableWorked = false; 6149 6150 // Parse relocations. 6151 std::vector<std::pair<uint64_t, SymbolRef>> Relocs; 6152 for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) { 6153 uint64_t RelocOffset; 6154 Reloc.getOffset(RelocOffset); 6155 uint64_t SectionAddress = Sections[SectIdx].getAddress(); 6156 RelocOffset -= SectionAddress; 6157 6158 symbol_iterator RelocSym = Reloc.getSymbol(); 6159 6160 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym)); 6161 } 6162 array_pod_sort(Relocs.begin(), Relocs.end()); 6163 6164 // Create a map of symbol addresses to symbol names for use by 6165 // the SymbolizerSymbolLookUp() routine. 6166 SymbolAddressMap AddrMap; 6167 bool DisSymNameFound = false; 6168 for (const SymbolRef &Symbol : MachOOF->symbols()) { 6169 SymbolRef::Type ST; 6170 Symbol.getType(ST); 6171 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data || 6172 ST == SymbolRef::ST_Other) { 6173 uint64_t Address; 6174 Symbol.getAddress(Address); 6175 StringRef SymName; 6176 Symbol.getName(SymName); 6177 AddrMap[Address] = SymName; 6178 if (!DisSymName.empty() && DisSymName == SymName) 6179 DisSymNameFound = true; 6180 } 6181 } 6182 if (!DisSymName.empty() && !DisSymNameFound) { 6183 outs() << "Can't find -dis-symname: " << DisSymName << "\n"; 6184 return; 6185 } 6186 // Set up the block of info used by the Symbolizer call backs. 6187 SymbolizerInfo.verbose = !NoSymbolicOperands; 6188 SymbolizerInfo.O = MachOOF; 6189 SymbolizerInfo.S = Sections[SectIdx]; 6190 SymbolizerInfo.AddrMap = &AddrMap; 6191 SymbolizerInfo.Sections = &Sections; 6192 SymbolizerInfo.class_name = nullptr; 6193 SymbolizerInfo.selector_name = nullptr; 6194 SymbolizerInfo.method = nullptr; 6195 SymbolizerInfo.demangled_name = nullptr; 6196 SymbolizerInfo.bindtable = nullptr; 6197 SymbolizerInfo.adrp_addr = 0; 6198 SymbolizerInfo.adrp_inst = 0; 6199 // Same for the ThumbSymbolizer 6200 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands; 6201 ThumbSymbolizerInfo.O = MachOOF; 6202 ThumbSymbolizerInfo.S = Sections[SectIdx]; 6203 ThumbSymbolizerInfo.AddrMap = &AddrMap; 6204 ThumbSymbolizerInfo.Sections = &Sections; 6205 ThumbSymbolizerInfo.class_name = nullptr; 6206 ThumbSymbolizerInfo.selector_name = nullptr; 6207 ThumbSymbolizerInfo.method = nullptr; 6208 ThumbSymbolizerInfo.demangled_name = nullptr; 6209 ThumbSymbolizerInfo.bindtable = nullptr; 6210 ThumbSymbolizerInfo.adrp_addr = 0; 6211 ThumbSymbolizerInfo.adrp_inst = 0; 6212 6213 // Disassemble symbol by symbol. 6214 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) { 6215 StringRef SymName; 6216 Symbols[SymIdx].getName(SymName); 6217 6218 SymbolRef::Type ST; 6219 Symbols[SymIdx].getType(ST); 6220 if (ST != SymbolRef::ST_Function) 6221 continue; 6222 6223 // Make sure the symbol is defined in this section. 6224 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]); 6225 if (!containsSym) 6226 continue; 6227 6228 // If we are only disassembling one symbol see if this is that symbol. 6229 if (!DisSymName.empty() && DisSymName != SymName) 6230 continue; 6231 6232 // Start at the address of the symbol relative to the section's address. 6233 uint64_t Start = 0; 6234 uint64_t SectionAddress = Sections[SectIdx].getAddress(); 6235 Symbols[SymIdx].getAddress(Start); 6236 Start -= SectionAddress; 6237 6238 // Stop disassembling either at the beginning of the next symbol or at 6239 // the end of the section. 6240 bool containsNextSym = false; 6241 uint64_t NextSym = 0; 6242 uint64_t NextSymIdx = SymIdx + 1; 6243 while (Symbols.size() > NextSymIdx) { 6244 SymbolRef::Type NextSymType; 6245 Symbols[NextSymIdx].getType(NextSymType); 6246 if (NextSymType == SymbolRef::ST_Function) { 6247 containsNextSym = 6248 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]); 6249 Symbols[NextSymIdx].getAddress(NextSym); 6250 NextSym -= SectionAddress; 6251 break; 6252 } 6253 ++NextSymIdx; 6254 } 6255 6256 uint64_t SectSize = Sections[SectIdx].getSize(); 6257 uint64_t End = containsNextSym ? NextSym : SectSize; 6258 uint64_t Size; 6259 6260 symbolTableWorked = true; 6261 6262 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl(); 6263 bool isThumb = 6264 (MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget; 6265 6266 outs() << SymName << ":\n"; 6267 DILineInfo lastLine; 6268 for (uint64_t Index = Start; Index < End; Index += Size) { 6269 MCInst Inst; 6270 6271 uint64_t PC = SectAddress + Index; 6272 if (!NoLeadingAddr) { 6273 if (FullLeadingAddr) { 6274 if (MachOOF->is64Bit()) 6275 outs() << format("%016" PRIx64, PC); 6276 else 6277 outs() << format("%08" PRIx64, PC); 6278 } else { 6279 outs() << format("%8" PRIx64 ":", PC); 6280 } 6281 } 6282 if (!NoShowRawInsn) 6283 outs() << "\t"; 6284 6285 // Check the data in code table here to see if this is data not an 6286 // instruction to be disassembled. 6287 DiceTable Dice; 6288 Dice.push_back(std::make_pair(PC, DiceRef())); 6289 dice_table_iterator DTI = 6290 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(), 6291 compareDiceTableEntries); 6292 if (DTI != Dices.end()) { 6293 uint16_t Length; 6294 DTI->second.getLength(Length); 6295 uint16_t Kind; 6296 DTI->second.getKind(Kind); 6297 Size = DumpDataInCode(Bytes.data() + Index, Length, Kind); 6298 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) && 6299 (PC == (DTI->first + Length - 1)) && (Length & 1)) 6300 Size++; 6301 continue; 6302 } 6303 6304 SmallVector<char, 64> AnnotationsBytes; 6305 raw_svector_ostream Annotations(AnnotationsBytes); 6306 6307 bool gotInst; 6308 if (isThumb) 6309 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index), 6310 PC, DebugOut, Annotations); 6311 else 6312 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC, 6313 DebugOut, Annotations); 6314 if (gotInst) { 6315 if (!NoShowRawInsn) { 6316 DumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, Size)); 6317 } 6318 formatted_raw_ostream FormattedOS(outs()); 6319 Annotations.flush(); 6320 StringRef AnnotationsStr = Annotations.str(); 6321 if (isThumb) 6322 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI); 6323 else 6324 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI); 6325 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo); 6326 6327 // Print debug info. 6328 if (diContext) { 6329 DILineInfo dli = diContext->getLineInfoForAddress(PC); 6330 // Print valid line info if it changed. 6331 if (dli != lastLine && dli.Line != 0) 6332 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':' 6333 << dli.Column; 6334 lastLine = dli; 6335 } 6336 outs() << "\n"; 6337 } else { 6338 unsigned int Arch = MachOOF->getArch(); 6339 if (Arch == Triple::x86_64 || Arch == Triple::x86) { 6340 outs() << format("\t.byte 0x%02x #bad opcode\n", 6341 *(Bytes.data() + Index) & 0xff); 6342 Size = 1; // skip exactly one illegible byte and move on. 6343 } else if (Arch == Triple::aarch64) { 6344 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) | 6345 (*(Bytes.data() + Index + 1) & 0xff) << 8 | 6346 (*(Bytes.data() + Index + 2) & 0xff) << 16 | 6347 (*(Bytes.data() + Index + 3) & 0xff) << 24; 6348 outs() << format("\t.long\t0x%08x\n", opcode); 6349 Size = 4; 6350 } else { 6351 errs() << "llvm-objdump: warning: invalid instruction encoding\n"; 6352 if (Size == 0) 6353 Size = 1; // skip illegible bytes 6354 } 6355 } 6356 } 6357 } 6358 if (!symbolTableWorked) { 6359 // Reading the symbol table didn't work, disassemble the whole section. 6360 uint64_t SectAddress = Sections[SectIdx].getAddress(); 6361 uint64_t SectSize = Sections[SectIdx].getSize(); 6362 uint64_t InstSize; 6363 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) { 6364 MCInst Inst; 6365 6366 uint64_t PC = SectAddress + Index; 6367 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC, 6368 DebugOut, nulls())) { 6369 if (!NoLeadingAddr) { 6370 if (FullLeadingAddr) { 6371 if (MachOOF->is64Bit()) 6372 outs() << format("%016" PRIx64, PC); 6373 else 6374 outs() << format("%08" PRIx64, PC); 6375 } else { 6376 outs() << format("%8" PRIx64 ":", PC); 6377 } 6378 } 6379 if (!NoShowRawInsn) { 6380 outs() << "\t"; 6381 DumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, InstSize)); 6382 } 6383 IP->printInst(&Inst, outs(), "", *STI); 6384 outs() << "\n"; 6385 } else { 6386 unsigned int Arch = MachOOF->getArch(); 6387 if (Arch == Triple::x86_64 || Arch == Triple::x86) { 6388 outs() << format("\t.byte 0x%02x #bad opcode\n", 6389 *(Bytes.data() + Index) & 0xff); 6390 InstSize = 1; // skip exactly one illegible byte and move on. 6391 } else { 6392 errs() << "llvm-objdump: warning: invalid instruction encoding\n"; 6393 if (InstSize == 0) 6394 InstSize = 1; // skip illegible bytes 6395 } 6396 } 6397 } 6398 } 6399 // The TripleName's need to be reset if we are called again for a different 6400 // archtecture. 6401 TripleName = ""; 6402 ThumbTripleName = ""; 6403 6404 if (SymbolizerInfo.method != nullptr) 6405 free(SymbolizerInfo.method); 6406 if (SymbolizerInfo.demangled_name != nullptr) 6407 free(SymbolizerInfo.demangled_name); 6408 if (SymbolizerInfo.bindtable != nullptr) 6409 delete SymbolizerInfo.bindtable; 6410 if (ThumbSymbolizerInfo.method != nullptr) 6411 free(ThumbSymbolizerInfo.method); 6412 if (ThumbSymbolizerInfo.demangled_name != nullptr) 6413 free(ThumbSymbolizerInfo.demangled_name); 6414 if (ThumbSymbolizerInfo.bindtable != nullptr) 6415 delete ThumbSymbolizerInfo.bindtable; 6416 } 6417 } 6418 6419 //===----------------------------------------------------------------------===// 6420 // __compact_unwind section dumping 6421 //===----------------------------------------------------------------------===// 6422 6423 namespace { 6424 6425 template <typename T> static uint64_t readNext(const char *&Buf) { 6426 using llvm::support::little; 6427 using llvm::support::unaligned; 6428 6429 uint64_t Val = support::endian::read<T, little, unaligned>(Buf); 6430 Buf += sizeof(T); 6431 return Val; 6432 } 6433 6434 struct CompactUnwindEntry { 6435 uint32_t OffsetInSection; 6436 6437 uint64_t FunctionAddr; 6438 uint32_t Length; 6439 uint32_t CompactEncoding; 6440 uint64_t PersonalityAddr; 6441 uint64_t LSDAAddr; 6442 6443 RelocationRef FunctionReloc; 6444 RelocationRef PersonalityReloc; 6445 RelocationRef LSDAReloc; 6446 6447 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64) 6448 : OffsetInSection(Offset) { 6449 if (Is64) 6450 read<uint64_t>(Contents.data() + Offset); 6451 else 6452 read<uint32_t>(Contents.data() + Offset); 6453 } 6454 6455 private: 6456 template <typename UIntPtr> void read(const char *Buf) { 6457 FunctionAddr = readNext<UIntPtr>(Buf); 6458 Length = readNext<uint32_t>(Buf); 6459 CompactEncoding = readNext<uint32_t>(Buf); 6460 PersonalityAddr = readNext<UIntPtr>(Buf); 6461 LSDAAddr = readNext<UIntPtr>(Buf); 6462 } 6463 }; 6464 } 6465 6466 /// Given a relocation from __compact_unwind, consisting of the RelocationRef 6467 /// and data being relocated, determine the best base Name and Addend to use for 6468 /// display purposes. 6469 /// 6470 /// 1. An Extern relocation will directly reference a symbol (and the data is 6471 /// then already an addend), so use that. 6472 /// 2. Otherwise the data is an offset in the object file's layout; try to find 6473 // a symbol before it in the same section, and use the offset from there. 6474 /// 3. Finally, if all that fails, fall back to an offset from the start of the 6475 /// referenced section. 6476 static void findUnwindRelocNameAddend(const MachOObjectFile *Obj, 6477 std::map<uint64_t, SymbolRef> &Symbols, 6478 const RelocationRef &Reloc, uint64_t Addr, 6479 StringRef &Name, uint64_t &Addend) { 6480 if (Reloc.getSymbol() != Obj->symbol_end()) { 6481 Reloc.getSymbol()->getName(Name); 6482 Addend = Addr; 6483 return; 6484 } 6485 6486 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl()); 6487 SectionRef RelocSection = Obj->getRelocationSection(RE); 6488 6489 uint64_t SectionAddr = RelocSection.getAddress(); 6490 6491 auto Sym = Symbols.upper_bound(Addr); 6492 if (Sym == Symbols.begin()) { 6493 // The first symbol in the object is after this reference, the best we can 6494 // do is section-relative notation. 6495 RelocSection.getName(Name); 6496 Addend = Addr - SectionAddr; 6497 return; 6498 } 6499 6500 // Go back one so that SymbolAddress <= Addr. 6501 --Sym; 6502 6503 section_iterator SymSection = Obj->section_end(); 6504 Sym->second.getSection(SymSection); 6505 if (RelocSection == *SymSection) { 6506 // There's a valid symbol in the same section before this reference. 6507 Sym->second.getName(Name); 6508 Addend = Addr - Sym->first; 6509 return; 6510 } 6511 6512 // There is a symbol before this reference, but it's in a different 6513 // section. Probably not helpful to mention it, so use the section name. 6514 RelocSection.getName(Name); 6515 Addend = Addr - SectionAddr; 6516 } 6517 6518 static void printUnwindRelocDest(const MachOObjectFile *Obj, 6519 std::map<uint64_t, SymbolRef> &Symbols, 6520 const RelocationRef &Reloc, uint64_t Addr) { 6521 StringRef Name; 6522 uint64_t Addend; 6523 6524 if (!Reloc.getObjectFile()) 6525 return; 6526 6527 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend); 6528 6529 outs() << Name; 6530 if (Addend) 6531 outs() << " + " << format("0x%" PRIx64, Addend); 6532 } 6533 6534 static void 6535 printMachOCompactUnwindSection(const MachOObjectFile *Obj, 6536 std::map<uint64_t, SymbolRef> &Symbols, 6537 const SectionRef &CompactUnwind) { 6538 6539 assert(Obj->isLittleEndian() && 6540 "There should not be a big-endian .o with __compact_unwind"); 6541 6542 bool Is64 = Obj->is64Bit(); 6543 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t); 6544 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t); 6545 6546 StringRef Contents; 6547 CompactUnwind.getContents(Contents); 6548 6549 SmallVector<CompactUnwindEntry, 4> CompactUnwinds; 6550 6551 // First populate the initial raw offsets, encodings and so on from the entry. 6552 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) { 6553 CompactUnwindEntry Entry(Contents.data(), Offset, Is64); 6554 CompactUnwinds.push_back(Entry); 6555 } 6556 6557 // Next we need to look at the relocations to find out what objects are 6558 // actually being referred to. 6559 for (const RelocationRef &Reloc : CompactUnwind.relocations()) { 6560 uint64_t RelocAddress; 6561 Reloc.getOffset(RelocAddress); 6562 6563 uint32_t EntryIdx = RelocAddress / EntrySize; 6564 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize; 6565 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx]; 6566 6567 if (OffsetInEntry == 0) 6568 Entry.FunctionReloc = Reloc; 6569 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t)) 6570 Entry.PersonalityReloc = Reloc; 6571 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t)) 6572 Entry.LSDAReloc = Reloc; 6573 else 6574 llvm_unreachable("Unexpected relocation in __compact_unwind section"); 6575 } 6576 6577 // Finally, we're ready to print the data we've gathered. 6578 outs() << "Contents of __compact_unwind section:\n"; 6579 for (auto &Entry : CompactUnwinds) { 6580 outs() << " Entry at offset " 6581 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n"; 6582 6583 // 1. Start of the region this entry applies to. 6584 outs() << " start: " << format("0x%" PRIx64, 6585 Entry.FunctionAddr) << ' '; 6586 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr); 6587 outs() << '\n'; 6588 6589 // 2. Length of the region this entry applies to. 6590 outs() << " length: " << format("0x%" PRIx32, Entry.Length) 6591 << '\n'; 6592 // 3. The 32-bit compact encoding. 6593 outs() << " compact encoding: " 6594 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n'; 6595 6596 // 4. The personality function, if present. 6597 if (Entry.PersonalityReloc.getObjectFile()) { 6598 outs() << " personality function: " 6599 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' '; 6600 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc, 6601 Entry.PersonalityAddr); 6602 outs() << '\n'; 6603 } 6604 6605 // 5. This entry's language-specific data area. 6606 if (Entry.LSDAReloc.getObjectFile()) { 6607 outs() << " LSDA: " << format("0x%" PRIx64, 6608 Entry.LSDAAddr) << ' '; 6609 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr); 6610 outs() << '\n'; 6611 } 6612 } 6613 } 6614 6615 //===----------------------------------------------------------------------===// 6616 // __unwind_info section dumping 6617 //===----------------------------------------------------------------------===// 6618 6619 static void printRegularSecondLevelUnwindPage(const char *PageStart) { 6620 const char *Pos = PageStart; 6621 uint32_t Kind = readNext<uint32_t>(Pos); 6622 (void)Kind; 6623 assert(Kind == 2 && "kind for a regular 2nd level index should be 2"); 6624 6625 uint16_t EntriesStart = readNext<uint16_t>(Pos); 6626 uint16_t NumEntries = readNext<uint16_t>(Pos); 6627 6628 Pos = PageStart + EntriesStart; 6629 for (unsigned i = 0; i < NumEntries; ++i) { 6630 uint32_t FunctionOffset = readNext<uint32_t>(Pos); 6631 uint32_t Encoding = readNext<uint32_t>(Pos); 6632 6633 outs() << " [" << i << "]: " 6634 << "function offset=" << format("0x%08" PRIx32, FunctionOffset) 6635 << ", " 6636 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n'; 6637 } 6638 } 6639 6640 static void printCompressedSecondLevelUnwindPage( 6641 const char *PageStart, uint32_t FunctionBase, 6642 const SmallVectorImpl<uint32_t> &CommonEncodings) { 6643 const char *Pos = PageStart; 6644 uint32_t Kind = readNext<uint32_t>(Pos); 6645 (void)Kind; 6646 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3"); 6647 6648 uint16_t EntriesStart = readNext<uint16_t>(Pos); 6649 uint16_t NumEntries = readNext<uint16_t>(Pos); 6650 6651 uint16_t EncodingsStart = readNext<uint16_t>(Pos); 6652 readNext<uint16_t>(Pos); 6653 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>( 6654 PageStart + EncodingsStart); 6655 6656 Pos = PageStart + EntriesStart; 6657 for (unsigned i = 0; i < NumEntries; ++i) { 6658 uint32_t Entry = readNext<uint32_t>(Pos); 6659 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff); 6660 uint32_t EncodingIdx = Entry >> 24; 6661 6662 uint32_t Encoding; 6663 if (EncodingIdx < CommonEncodings.size()) 6664 Encoding = CommonEncodings[EncodingIdx]; 6665 else 6666 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()]; 6667 6668 outs() << " [" << i << "]: " 6669 << "function offset=" << format("0x%08" PRIx32, FunctionOffset) 6670 << ", " 6671 << "encoding[" << EncodingIdx 6672 << "]=" << format("0x%08" PRIx32, Encoding) << '\n'; 6673 } 6674 } 6675 6676 static void printMachOUnwindInfoSection(const MachOObjectFile *Obj, 6677 std::map<uint64_t, SymbolRef> &Symbols, 6678 const SectionRef &UnwindInfo) { 6679 6680 assert(Obj->isLittleEndian() && 6681 "There should not be a big-endian .o with __unwind_info"); 6682 6683 outs() << "Contents of __unwind_info section:\n"; 6684 6685 StringRef Contents; 6686 UnwindInfo.getContents(Contents); 6687 const char *Pos = Contents.data(); 6688 6689 //===---------------------------------- 6690 // Section header 6691 //===---------------------------------- 6692 6693 uint32_t Version = readNext<uint32_t>(Pos); 6694 outs() << " Version: " 6695 << format("0x%" PRIx32, Version) << '\n'; 6696 assert(Version == 1 && "only understand version 1"); 6697 6698 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos); 6699 outs() << " Common encodings array section offset: " 6700 << format("0x%" PRIx32, CommonEncodingsStart) << '\n'; 6701 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos); 6702 outs() << " Number of common encodings in array: " 6703 << format("0x%" PRIx32, NumCommonEncodings) << '\n'; 6704 6705 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos); 6706 outs() << " Personality function array section offset: " 6707 << format("0x%" PRIx32, PersonalitiesStart) << '\n'; 6708 uint32_t NumPersonalities = readNext<uint32_t>(Pos); 6709 outs() << " Number of personality functions in array: " 6710 << format("0x%" PRIx32, NumPersonalities) << '\n'; 6711 6712 uint32_t IndicesStart = readNext<uint32_t>(Pos); 6713 outs() << " Index array section offset: " 6714 << format("0x%" PRIx32, IndicesStart) << '\n'; 6715 uint32_t NumIndices = readNext<uint32_t>(Pos); 6716 outs() << " Number of indices in array: " 6717 << format("0x%" PRIx32, NumIndices) << '\n'; 6718 6719 //===---------------------------------- 6720 // A shared list of common encodings 6721 //===---------------------------------- 6722 6723 // These occupy indices in the range [0, N] whenever an encoding is referenced 6724 // from a compressed 2nd level index table. In practice the linker only 6725 // creates ~128 of these, so that indices are available to embed encodings in 6726 // the 2nd level index. 6727 6728 SmallVector<uint32_t, 64> CommonEncodings; 6729 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n"; 6730 Pos = Contents.data() + CommonEncodingsStart; 6731 for (unsigned i = 0; i < NumCommonEncodings; ++i) { 6732 uint32_t Encoding = readNext<uint32_t>(Pos); 6733 CommonEncodings.push_back(Encoding); 6734 6735 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding) 6736 << '\n'; 6737 } 6738 6739 //===---------------------------------- 6740 // Personality functions used in this executable 6741 //===---------------------------------- 6742 6743 // There should be only a handful of these (one per source language, 6744 // roughly). Particularly since they only get 2 bits in the compact encoding. 6745 6746 outs() << " Personality functions: (count = " << NumPersonalities << ")\n"; 6747 Pos = Contents.data() + PersonalitiesStart; 6748 for (unsigned i = 0; i < NumPersonalities; ++i) { 6749 uint32_t PersonalityFn = readNext<uint32_t>(Pos); 6750 outs() << " personality[" << i + 1 6751 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n'; 6752 } 6753 6754 //===---------------------------------- 6755 // The level 1 index entries 6756 //===---------------------------------- 6757 6758 // These specify an approximate place to start searching for the more detailed 6759 // information, sorted by PC. 6760 6761 struct IndexEntry { 6762 uint32_t FunctionOffset; 6763 uint32_t SecondLevelPageStart; 6764 uint32_t LSDAStart; 6765 }; 6766 6767 SmallVector<IndexEntry, 4> IndexEntries; 6768 6769 outs() << " Top level indices: (count = " << NumIndices << ")\n"; 6770 Pos = Contents.data() + IndicesStart; 6771 for (unsigned i = 0; i < NumIndices; ++i) { 6772 IndexEntry Entry; 6773 6774 Entry.FunctionOffset = readNext<uint32_t>(Pos); 6775 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos); 6776 Entry.LSDAStart = readNext<uint32_t>(Pos); 6777 IndexEntries.push_back(Entry); 6778 6779 outs() << " [" << i << "]: " 6780 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset) 6781 << ", " 6782 << "2nd level page offset=" 6783 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", " 6784 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n'; 6785 } 6786 6787 //===---------------------------------- 6788 // Next come the LSDA tables 6789 //===---------------------------------- 6790 6791 // The LSDA layout is rather implicit: it's a contiguous array of entries from 6792 // the first top-level index's LSDAOffset to the last (sentinel). 6793 6794 outs() << " LSDA descriptors:\n"; 6795 Pos = Contents.data() + IndexEntries[0].LSDAStart; 6796 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) / 6797 (2 * sizeof(uint32_t)); 6798 for (int i = 0; i < NumLSDAs; ++i) { 6799 uint32_t FunctionOffset = readNext<uint32_t>(Pos); 6800 uint32_t LSDAOffset = readNext<uint32_t>(Pos); 6801 outs() << " [" << i << "]: " 6802 << "function offset=" << format("0x%08" PRIx32, FunctionOffset) 6803 << ", " 6804 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n'; 6805 } 6806 6807 //===---------------------------------- 6808 // Finally, the 2nd level indices 6809 //===---------------------------------- 6810 6811 // Generally these are 4K in size, and have 2 possible forms: 6812 // + Regular stores up to 511 entries with disparate encodings 6813 // + Compressed stores up to 1021 entries if few enough compact encoding 6814 // values are used. 6815 outs() << " Second level indices:\n"; 6816 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) { 6817 // The final sentinel top-level index has no associated 2nd level page 6818 if (IndexEntries[i].SecondLevelPageStart == 0) 6819 break; 6820 6821 outs() << " Second level index[" << i << "]: " 6822 << "offset in section=" 6823 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart) 6824 << ", " 6825 << "base function offset=" 6826 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n'; 6827 6828 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart; 6829 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos); 6830 if (Kind == 2) 6831 printRegularSecondLevelUnwindPage(Pos); 6832 else if (Kind == 3) 6833 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset, 6834 CommonEncodings); 6835 else 6836 llvm_unreachable("Do not know how to print this kind of 2nd level page"); 6837 } 6838 } 6839 6840 void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) { 6841 std::map<uint64_t, SymbolRef> Symbols; 6842 for (const SymbolRef &SymRef : Obj->symbols()) { 6843 // Discard any undefined or absolute symbols. They're not going to take part 6844 // in the convenience lookup for unwind info and just take up resources. 6845 section_iterator Section = Obj->section_end(); 6846 SymRef.getSection(Section); 6847 if (Section == Obj->section_end()) 6848 continue; 6849 6850 uint64_t Addr; 6851 SymRef.getAddress(Addr); 6852 Symbols.insert(std::make_pair(Addr, SymRef)); 6853 } 6854 6855 for (const SectionRef &Section : Obj->sections()) { 6856 StringRef SectName; 6857 Section.getName(SectName); 6858 if (SectName == "__compact_unwind") 6859 printMachOCompactUnwindSection(Obj, Symbols, Section); 6860 else if (SectName == "__unwind_info") 6861 printMachOUnwindInfoSection(Obj, Symbols, Section); 6862 else if (SectName == "__eh_frame") 6863 outs() << "llvm-objdump: warning: unhandled __eh_frame section\n"; 6864 } 6865 } 6866 6867 static void PrintMachHeader(uint32_t magic, uint32_t cputype, 6868 uint32_t cpusubtype, uint32_t filetype, 6869 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags, 6870 bool verbose) { 6871 outs() << "Mach header\n"; 6872 outs() << " magic cputype cpusubtype caps filetype ncmds " 6873 "sizeofcmds flags\n"; 6874 if (verbose) { 6875 if (magic == MachO::MH_MAGIC) 6876 outs() << " MH_MAGIC"; 6877 else if (magic == MachO::MH_MAGIC_64) 6878 outs() << "MH_MAGIC_64"; 6879 else 6880 outs() << format(" 0x%08" PRIx32, magic); 6881 switch (cputype) { 6882 case MachO::CPU_TYPE_I386: 6883 outs() << " I386"; 6884 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 6885 case MachO::CPU_SUBTYPE_I386_ALL: 6886 outs() << " ALL"; 6887 break; 6888 default: 6889 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 6890 break; 6891 } 6892 break; 6893 case MachO::CPU_TYPE_X86_64: 6894 outs() << " X86_64"; 6895 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 6896 case MachO::CPU_SUBTYPE_X86_64_ALL: 6897 outs() << " ALL"; 6898 break; 6899 case MachO::CPU_SUBTYPE_X86_64_H: 6900 outs() << " Haswell"; 6901 break; 6902 default: 6903 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 6904 break; 6905 } 6906 break; 6907 case MachO::CPU_TYPE_ARM: 6908 outs() << " ARM"; 6909 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 6910 case MachO::CPU_SUBTYPE_ARM_ALL: 6911 outs() << " ALL"; 6912 break; 6913 case MachO::CPU_SUBTYPE_ARM_V4T: 6914 outs() << " V4T"; 6915 break; 6916 case MachO::CPU_SUBTYPE_ARM_V5TEJ: 6917 outs() << " V5TEJ"; 6918 break; 6919 case MachO::CPU_SUBTYPE_ARM_XSCALE: 6920 outs() << " XSCALE"; 6921 break; 6922 case MachO::CPU_SUBTYPE_ARM_V6: 6923 outs() << " V6"; 6924 break; 6925 case MachO::CPU_SUBTYPE_ARM_V6M: 6926 outs() << " V6M"; 6927 break; 6928 case MachO::CPU_SUBTYPE_ARM_V7: 6929 outs() << " V7"; 6930 break; 6931 case MachO::CPU_SUBTYPE_ARM_V7EM: 6932 outs() << " V7EM"; 6933 break; 6934 case MachO::CPU_SUBTYPE_ARM_V7K: 6935 outs() << " V7K"; 6936 break; 6937 case MachO::CPU_SUBTYPE_ARM_V7M: 6938 outs() << " V7M"; 6939 break; 6940 case MachO::CPU_SUBTYPE_ARM_V7S: 6941 outs() << " V7S"; 6942 break; 6943 default: 6944 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 6945 break; 6946 } 6947 break; 6948 case MachO::CPU_TYPE_ARM64: 6949 outs() << " ARM64"; 6950 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 6951 case MachO::CPU_SUBTYPE_ARM64_ALL: 6952 outs() << " ALL"; 6953 break; 6954 default: 6955 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 6956 break; 6957 } 6958 break; 6959 case MachO::CPU_TYPE_POWERPC: 6960 outs() << " PPC"; 6961 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 6962 case MachO::CPU_SUBTYPE_POWERPC_ALL: 6963 outs() << " ALL"; 6964 break; 6965 default: 6966 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 6967 break; 6968 } 6969 break; 6970 case MachO::CPU_TYPE_POWERPC64: 6971 outs() << " PPC64"; 6972 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { 6973 case MachO::CPU_SUBTYPE_POWERPC_ALL: 6974 outs() << " ALL"; 6975 break; 6976 default: 6977 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 6978 break; 6979 } 6980 break; 6981 } 6982 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) { 6983 outs() << " LIB64"; 6984 } else { 6985 outs() << format(" 0x%02" PRIx32, 6986 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24); 6987 } 6988 switch (filetype) { 6989 case MachO::MH_OBJECT: 6990 outs() << " OBJECT"; 6991 break; 6992 case MachO::MH_EXECUTE: 6993 outs() << " EXECUTE"; 6994 break; 6995 case MachO::MH_FVMLIB: 6996 outs() << " FVMLIB"; 6997 break; 6998 case MachO::MH_CORE: 6999 outs() << " CORE"; 7000 break; 7001 case MachO::MH_PRELOAD: 7002 outs() << " PRELOAD"; 7003 break; 7004 case MachO::MH_DYLIB: 7005 outs() << " DYLIB"; 7006 break; 7007 case MachO::MH_DYLIB_STUB: 7008 outs() << " DYLIB_STUB"; 7009 break; 7010 case MachO::MH_DYLINKER: 7011 outs() << " DYLINKER"; 7012 break; 7013 case MachO::MH_BUNDLE: 7014 outs() << " BUNDLE"; 7015 break; 7016 case MachO::MH_DSYM: 7017 outs() << " DSYM"; 7018 break; 7019 case MachO::MH_KEXT_BUNDLE: 7020 outs() << " KEXTBUNDLE"; 7021 break; 7022 default: 7023 outs() << format(" %10u", filetype); 7024 break; 7025 } 7026 outs() << format(" %5u", ncmds); 7027 outs() << format(" %10u", sizeofcmds); 7028 uint32_t f = flags; 7029 if (f & MachO::MH_NOUNDEFS) { 7030 outs() << " NOUNDEFS"; 7031 f &= ~MachO::MH_NOUNDEFS; 7032 } 7033 if (f & MachO::MH_INCRLINK) { 7034 outs() << " INCRLINK"; 7035 f &= ~MachO::MH_INCRLINK; 7036 } 7037 if (f & MachO::MH_DYLDLINK) { 7038 outs() << " DYLDLINK"; 7039 f &= ~MachO::MH_DYLDLINK; 7040 } 7041 if (f & MachO::MH_BINDATLOAD) { 7042 outs() << " BINDATLOAD"; 7043 f &= ~MachO::MH_BINDATLOAD; 7044 } 7045 if (f & MachO::MH_PREBOUND) { 7046 outs() << " PREBOUND"; 7047 f &= ~MachO::MH_PREBOUND; 7048 } 7049 if (f & MachO::MH_SPLIT_SEGS) { 7050 outs() << " SPLIT_SEGS"; 7051 f &= ~MachO::MH_SPLIT_SEGS; 7052 } 7053 if (f & MachO::MH_LAZY_INIT) { 7054 outs() << " LAZY_INIT"; 7055 f &= ~MachO::MH_LAZY_INIT; 7056 } 7057 if (f & MachO::MH_TWOLEVEL) { 7058 outs() << " TWOLEVEL"; 7059 f &= ~MachO::MH_TWOLEVEL; 7060 } 7061 if (f & MachO::MH_FORCE_FLAT) { 7062 outs() << " FORCE_FLAT"; 7063 f &= ~MachO::MH_FORCE_FLAT; 7064 } 7065 if (f & MachO::MH_NOMULTIDEFS) { 7066 outs() << " NOMULTIDEFS"; 7067 f &= ~MachO::MH_NOMULTIDEFS; 7068 } 7069 if (f & MachO::MH_NOFIXPREBINDING) { 7070 outs() << " NOFIXPREBINDING"; 7071 f &= ~MachO::MH_NOFIXPREBINDING; 7072 } 7073 if (f & MachO::MH_PREBINDABLE) { 7074 outs() << " PREBINDABLE"; 7075 f &= ~MachO::MH_PREBINDABLE; 7076 } 7077 if (f & MachO::MH_ALLMODSBOUND) { 7078 outs() << " ALLMODSBOUND"; 7079 f &= ~MachO::MH_ALLMODSBOUND; 7080 } 7081 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) { 7082 outs() << " SUBSECTIONS_VIA_SYMBOLS"; 7083 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS; 7084 } 7085 if (f & MachO::MH_CANONICAL) { 7086 outs() << " CANONICAL"; 7087 f &= ~MachO::MH_CANONICAL; 7088 } 7089 if (f & MachO::MH_WEAK_DEFINES) { 7090 outs() << " WEAK_DEFINES"; 7091 f &= ~MachO::MH_WEAK_DEFINES; 7092 } 7093 if (f & MachO::MH_BINDS_TO_WEAK) { 7094 outs() << " BINDS_TO_WEAK"; 7095 f &= ~MachO::MH_BINDS_TO_WEAK; 7096 } 7097 if (f & MachO::MH_ALLOW_STACK_EXECUTION) { 7098 outs() << " ALLOW_STACK_EXECUTION"; 7099 f &= ~MachO::MH_ALLOW_STACK_EXECUTION; 7100 } 7101 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) { 7102 outs() << " DEAD_STRIPPABLE_DYLIB"; 7103 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB; 7104 } 7105 if (f & MachO::MH_PIE) { 7106 outs() << " PIE"; 7107 f &= ~MachO::MH_PIE; 7108 } 7109 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) { 7110 outs() << " NO_REEXPORTED_DYLIBS"; 7111 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS; 7112 } 7113 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) { 7114 outs() << " MH_HAS_TLV_DESCRIPTORS"; 7115 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS; 7116 } 7117 if (f & MachO::MH_NO_HEAP_EXECUTION) { 7118 outs() << " MH_NO_HEAP_EXECUTION"; 7119 f &= ~MachO::MH_NO_HEAP_EXECUTION; 7120 } 7121 if (f & MachO::MH_APP_EXTENSION_SAFE) { 7122 outs() << " APP_EXTENSION_SAFE"; 7123 f &= ~MachO::MH_APP_EXTENSION_SAFE; 7124 } 7125 if (f != 0 || flags == 0) 7126 outs() << format(" 0x%08" PRIx32, f); 7127 } else { 7128 outs() << format(" 0x%08" PRIx32, magic); 7129 outs() << format(" %7d", cputype); 7130 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); 7131 outs() << format(" 0x%02" PRIx32, 7132 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24); 7133 outs() << format(" %10u", filetype); 7134 outs() << format(" %5u", ncmds); 7135 outs() << format(" %10u", sizeofcmds); 7136 outs() << format(" 0x%08" PRIx32, flags); 7137 } 7138 outs() << "\n"; 7139 } 7140 7141 static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize, 7142 StringRef SegName, uint64_t vmaddr, 7143 uint64_t vmsize, uint64_t fileoff, 7144 uint64_t filesize, uint32_t maxprot, 7145 uint32_t initprot, uint32_t nsects, 7146 uint32_t flags, uint32_t object_size, 7147 bool verbose) { 7148 uint64_t expected_cmdsize; 7149 if (cmd == MachO::LC_SEGMENT) { 7150 outs() << " cmd LC_SEGMENT\n"; 7151 expected_cmdsize = nsects; 7152 expected_cmdsize *= sizeof(struct MachO::section); 7153 expected_cmdsize += sizeof(struct MachO::segment_command); 7154 } else { 7155 outs() << " cmd LC_SEGMENT_64\n"; 7156 expected_cmdsize = nsects; 7157 expected_cmdsize *= sizeof(struct MachO::section_64); 7158 expected_cmdsize += sizeof(struct MachO::segment_command_64); 7159 } 7160 outs() << " cmdsize " << cmdsize; 7161 if (cmdsize != expected_cmdsize) 7162 outs() << " Inconsistent size\n"; 7163 else 7164 outs() << "\n"; 7165 outs() << " segname " << SegName << "\n"; 7166 if (cmd == MachO::LC_SEGMENT_64) { 7167 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n"; 7168 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n"; 7169 } else { 7170 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n"; 7171 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n"; 7172 } 7173 outs() << " fileoff " << fileoff; 7174 if (fileoff > object_size) 7175 outs() << " (past end of file)\n"; 7176 else 7177 outs() << "\n"; 7178 outs() << " filesize " << filesize; 7179 if (fileoff + filesize > object_size) 7180 outs() << " (past end of file)\n"; 7181 else 7182 outs() << "\n"; 7183 if (verbose) { 7184 if ((maxprot & 7185 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | 7186 MachO::VM_PROT_EXECUTE)) != 0) 7187 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n"; 7188 else { 7189 if (maxprot & MachO::VM_PROT_READ) 7190 outs() << " maxprot r"; 7191 else 7192 outs() << " maxprot -"; 7193 if (maxprot & MachO::VM_PROT_WRITE) 7194 outs() << "w"; 7195 else 7196 outs() << "-"; 7197 if (maxprot & MachO::VM_PROT_EXECUTE) 7198 outs() << "x\n"; 7199 else 7200 outs() << "-\n"; 7201 } 7202 if ((initprot & 7203 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | 7204 MachO::VM_PROT_EXECUTE)) != 0) 7205 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n"; 7206 else { 7207 if (initprot & MachO::VM_PROT_READ) 7208 outs() << " initprot r"; 7209 else 7210 outs() << " initprot -"; 7211 if (initprot & MachO::VM_PROT_WRITE) 7212 outs() << "w"; 7213 else 7214 outs() << "-"; 7215 if (initprot & MachO::VM_PROT_EXECUTE) 7216 outs() << "x\n"; 7217 else 7218 outs() << "-\n"; 7219 } 7220 } else { 7221 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n"; 7222 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n"; 7223 } 7224 outs() << " nsects " << nsects << "\n"; 7225 if (verbose) { 7226 outs() << " flags"; 7227 if (flags == 0) 7228 outs() << " (none)\n"; 7229 else { 7230 if (flags & MachO::SG_HIGHVM) { 7231 outs() << " HIGHVM"; 7232 flags &= ~MachO::SG_HIGHVM; 7233 } 7234 if (flags & MachO::SG_FVMLIB) { 7235 outs() << " FVMLIB"; 7236 flags &= ~MachO::SG_FVMLIB; 7237 } 7238 if (flags & MachO::SG_NORELOC) { 7239 outs() << " NORELOC"; 7240 flags &= ~MachO::SG_NORELOC; 7241 } 7242 if (flags & MachO::SG_PROTECTED_VERSION_1) { 7243 outs() << " PROTECTED_VERSION_1"; 7244 flags &= ~MachO::SG_PROTECTED_VERSION_1; 7245 } 7246 if (flags) 7247 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n"; 7248 else 7249 outs() << "\n"; 7250 } 7251 } else { 7252 outs() << " flags " << format("0x%" PRIx32, flags) << "\n"; 7253 } 7254 } 7255 7256 static void PrintSection(const char *sectname, const char *segname, 7257 uint64_t addr, uint64_t size, uint32_t offset, 7258 uint32_t align, uint32_t reloff, uint32_t nreloc, 7259 uint32_t flags, uint32_t reserved1, uint32_t reserved2, 7260 uint32_t cmd, const char *sg_segname, 7261 uint32_t filetype, uint32_t object_size, 7262 bool verbose) { 7263 outs() << "Section\n"; 7264 outs() << " sectname " << format("%.16s\n", sectname); 7265 outs() << " segname " << format("%.16s", segname); 7266 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0) 7267 outs() << " (does not match segment)\n"; 7268 else 7269 outs() << "\n"; 7270 if (cmd == MachO::LC_SEGMENT_64) { 7271 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n"; 7272 outs() << " size " << format("0x%016" PRIx64, size); 7273 } else { 7274 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n"; 7275 outs() << " size " << format("0x%08" PRIx64, size); 7276 } 7277 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size) 7278 outs() << " (past end of file)\n"; 7279 else 7280 outs() << "\n"; 7281 outs() << " offset " << offset; 7282 if (offset > object_size) 7283 outs() << " (past end of file)\n"; 7284 else 7285 outs() << "\n"; 7286 uint32_t align_shifted = 1 << align; 7287 outs() << " align 2^" << align << " (" << align_shifted << ")\n"; 7288 outs() << " reloff " << reloff; 7289 if (reloff > object_size) 7290 outs() << " (past end of file)\n"; 7291 else 7292 outs() << "\n"; 7293 outs() << " nreloc " << nreloc; 7294 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size) 7295 outs() << " (past end of file)\n"; 7296 else 7297 outs() << "\n"; 7298 uint32_t section_type = flags & MachO::SECTION_TYPE; 7299 if (verbose) { 7300 outs() << " type"; 7301 if (section_type == MachO::S_REGULAR) 7302 outs() << " S_REGULAR\n"; 7303 else if (section_type == MachO::S_ZEROFILL) 7304 outs() << " S_ZEROFILL\n"; 7305 else if (section_type == MachO::S_CSTRING_LITERALS) 7306 outs() << " S_CSTRING_LITERALS\n"; 7307 else if (section_type == MachO::S_4BYTE_LITERALS) 7308 outs() << " S_4BYTE_LITERALS\n"; 7309 else if (section_type == MachO::S_8BYTE_LITERALS) 7310 outs() << " S_8BYTE_LITERALS\n"; 7311 else if (section_type == MachO::S_16BYTE_LITERALS) 7312 outs() << " S_16BYTE_LITERALS\n"; 7313 else if (section_type == MachO::S_LITERAL_POINTERS) 7314 outs() << " S_LITERAL_POINTERS\n"; 7315 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS) 7316 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n"; 7317 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS) 7318 outs() << " S_LAZY_SYMBOL_POINTERS\n"; 7319 else if (section_type == MachO::S_SYMBOL_STUBS) 7320 outs() << " S_SYMBOL_STUBS\n"; 7321 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS) 7322 outs() << " S_MOD_INIT_FUNC_POINTERS\n"; 7323 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS) 7324 outs() << " S_MOD_TERM_FUNC_POINTERS\n"; 7325 else if (section_type == MachO::S_COALESCED) 7326 outs() << " S_COALESCED\n"; 7327 else if (section_type == MachO::S_INTERPOSING) 7328 outs() << " S_INTERPOSING\n"; 7329 else if (section_type == MachO::S_DTRACE_DOF) 7330 outs() << " S_DTRACE_DOF\n"; 7331 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS) 7332 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n"; 7333 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR) 7334 outs() << " S_THREAD_LOCAL_REGULAR\n"; 7335 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL) 7336 outs() << " S_THREAD_LOCAL_ZEROFILL\n"; 7337 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES) 7338 outs() << " S_THREAD_LOCAL_VARIABLES\n"; 7339 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS) 7340 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n"; 7341 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS) 7342 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n"; 7343 else 7344 outs() << format("0x%08" PRIx32, section_type) << "\n"; 7345 outs() << "attributes"; 7346 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES; 7347 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS) 7348 outs() << " PURE_INSTRUCTIONS"; 7349 if (section_attributes & MachO::S_ATTR_NO_TOC) 7350 outs() << " NO_TOC"; 7351 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS) 7352 outs() << " STRIP_STATIC_SYMS"; 7353 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP) 7354 outs() << " NO_DEAD_STRIP"; 7355 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT) 7356 outs() << " LIVE_SUPPORT"; 7357 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE) 7358 outs() << " SELF_MODIFYING_CODE"; 7359 if (section_attributes & MachO::S_ATTR_DEBUG) 7360 outs() << " DEBUG"; 7361 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS) 7362 outs() << " SOME_INSTRUCTIONS"; 7363 if (section_attributes & MachO::S_ATTR_EXT_RELOC) 7364 outs() << " EXT_RELOC"; 7365 if (section_attributes & MachO::S_ATTR_LOC_RELOC) 7366 outs() << " LOC_RELOC"; 7367 if (section_attributes == 0) 7368 outs() << " (none)"; 7369 outs() << "\n"; 7370 } else 7371 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n"; 7372 outs() << " reserved1 " << reserved1; 7373 if (section_type == MachO::S_SYMBOL_STUBS || 7374 section_type == MachO::S_LAZY_SYMBOL_POINTERS || 7375 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS || 7376 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS || 7377 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS) 7378 outs() << " (index into indirect symbol table)\n"; 7379 else 7380 outs() << "\n"; 7381 outs() << " reserved2 " << reserved2; 7382 if (section_type == MachO::S_SYMBOL_STUBS) 7383 outs() << " (size of stubs)\n"; 7384 else 7385 outs() << "\n"; 7386 } 7387 7388 static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit, 7389 uint32_t object_size) { 7390 outs() << " cmd LC_SYMTAB\n"; 7391 outs() << " cmdsize " << st.cmdsize; 7392 if (st.cmdsize != sizeof(struct MachO::symtab_command)) 7393 outs() << " Incorrect size\n"; 7394 else 7395 outs() << "\n"; 7396 outs() << " symoff " << st.symoff; 7397 if (st.symoff > object_size) 7398 outs() << " (past end of file)\n"; 7399 else 7400 outs() << "\n"; 7401 outs() << " nsyms " << st.nsyms; 7402 uint64_t big_size; 7403 if (Is64Bit) { 7404 big_size = st.nsyms; 7405 big_size *= sizeof(struct MachO::nlist_64); 7406 big_size += st.symoff; 7407 if (big_size > object_size) 7408 outs() << " (past end of file)\n"; 7409 else 7410 outs() << "\n"; 7411 } else { 7412 big_size = st.nsyms; 7413 big_size *= sizeof(struct MachO::nlist); 7414 big_size += st.symoff; 7415 if (big_size > object_size) 7416 outs() << " (past end of file)\n"; 7417 else 7418 outs() << "\n"; 7419 } 7420 outs() << " stroff " << st.stroff; 7421 if (st.stroff > object_size) 7422 outs() << " (past end of file)\n"; 7423 else 7424 outs() << "\n"; 7425 outs() << " strsize " << st.strsize; 7426 big_size = st.stroff; 7427 big_size += st.strsize; 7428 if (big_size > object_size) 7429 outs() << " (past end of file)\n"; 7430 else 7431 outs() << "\n"; 7432 } 7433 7434 static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst, 7435 uint32_t nsyms, uint32_t object_size, 7436 bool Is64Bit) { 7437 outs() << " cmd LC_DYSYMTAB\n"; 7438 outs() << " cmdsize " << dyst.cmdsize; 7439 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command)) 7440 outs() << " Incorrect size\n"; 7441 else 7442 outs() << "\n"; 7443 outs() << " ilocalsym " << dyst.ilocalsym; 7444 if (dyst.ilocalsym > nsyms) 7445 outs() << " (greater than the number of symbols)\n"; 7446 else 7447 outs() << "\n"; 7448 outs() << " nlocalsym " << dyst.nlocalsym; 7449 uint64_t big_size; 7450 big_size = dyst.ilocalsym; 7451 big_size += dyst.nlocalsym; 7452 if (big_size > nsyms) 7453 outs() << " (past the end of the symbol table)\n"; 7454 else 7455 outs() << "\n"; 7456 outs() << " iextdefsym " << dyst.iextdefsym; 7457 if (dyst.iextdefsym > nsyms) 7458 outs() << " (greater than the number of symbols)\n"; 7459 else 7460 outs() << "\n"; 7461 outs() << " nextdefsym " << dyst.nextdefsym; 7462 big_size = dyst.iextdefsym; 7463 big_size += dyst.nextdefsym; 7464 if (big_size > nsyms) 7465 outs() << " (past the end of the symbol table)\n"; 7466 else 7467 outs() << "\n"; 7468 outs() << " iundefsym " << dyst.iundefsym; 7469 if (dyst.iundefsym > nsyms) 7470 outs() << " (greater than the number of symbols)\n"; 7471 else 7472 outs() << "\n"; 7473 outs() << " nundefsym " << dyst.nundefsym; 7474 big_size = dyst.iundefsym; 7475 big_size += dyst.nundefsym; 7476 if (big_size > nsyms) 7477 outs() << " (past the end of the symbol table)\n"; 7478 else 7479 outs() << "\n"; 7480 outs() << " tocoff " << dyst.tocoff; 7481 if (dyst.tocoff > object_size) 7482 outs() << " (past end of file)\n"; 7483 else 7484 outs() << "\n"; 7485 outs() << " ntoc " << dyst.ntoc; 7486 big_size = dyst.ntoc; 7487 big_size *= sizeof(struct MachO::dylib_table_of_contents); 7488 big_size += dyst.tocoff; 7489 if (big_size > object_size) 7490 outs() << " (past end of file)\n"; 7491 else 7492 outs() << "\n"; 7493 outs() << " modtaboff " << dyst.modtaboff; 7494 if (dyst.modtaboff > object_size) 7495 outs() << " (past end of file)\n"; 7496 else 7497 outs() << "\n"; 7498 outs() << " nmodtab " << dyst.nmodtab; 7499 uint64_t modtabend; 7500 if (Is64Bit) { 7501 modtabend = dyst.nmodtab; 7502 modtabend *= sizeof(struct MachO::dylib_module_64); 7503 modtabend += dyst.modtaboff; 7504 } else { 7505 modtabend = dyst.nmodtab; 7506 modtabend *= sizeof(struct MachO::dylib_module); 7507 modtabend += dyst.modtaboff; 7508 } 7509 if (modtabend > object_size) 7510 outs() << " (past end of file)\n"; 7511 else 7512 outs() << "\n"; 7513 outs() << " extrefsymoff " << dyst.extrefsymoff; 7514 if (dyst.extrefsymoff > object_size) 7515 outs() << " (past end of file)\n"; 7516 else 7517 outs() << "\n"; 7518 outs() << " nextrefsyms " << dyst.nextrefsyms; 7519 big_size = dyst.nextrefsyms; 7520 big_size *= sizeof(struct MachO::dylib_reference); 7521 big_size += dyst.extrefsymoff; 7522 if (big_size > object_size) 7523 outs() << " (past end of file)\n"; 7524 else 7525 outs() << "\n"; 7526 outs() << " indirectsymoff " << dyst.indirectsymoff; 7527 if (dyst.indirectsymoff > object_size) 7528 outs() << " (past end of file)\n"; 7529 else 7530 outs() << "\n"; 7531 outs() << " nindirectsyms " << dyst.nindirectsyms; 7532 big_size = dyst.nindirectsyms; 7533 big_size *= sizeof(uint32_t); 7534 big_size += dyst.indirectsymoff; 7535 if (big_size > object_size) 7536 outs() << " (past end of file)\n"; 7537 else 7538 outs() << "\n"; 7539 outs() << " extreloff " << dyst.extreloff; 7540 if (dyst.extreloff > object_size) 7541 outs() << " (past end of file)\n"; 7542 else 7543 outs() << "\n"; 7544 outs() << " nextrel " << dyst.nextrel; 7545 big_size = dyst.nextrel; 7546 big_size *= sizeof(struct MachO::relocation_info); 7547 big_size += dyst.extreloff; 7548 if (big_size > object_size) 7549 outs() << " (past end of file)\n"; 7550 else 7551 outs() << "\n"; 7552 outs() << " locreloff " << dyst.locreloff; 7553 if (dyst.locreloff > object_size) 7554 outs() << " (past end of file)\n"; 7555 else 7556 outs() << "\n"; 7557 outs() << " nlocrel " << dyst.nlocrel; 7558 big_size = dyst.nlocrel; 7559 big_size *= sizeof(struct MachO::relocation_info); 7560 big_size += dyst.locreloff; 7561 if (big_size > object_size) 7562 outs() << " (past end of file)\n"; 7563 else 7564 outs() << "\n"; 7565 } 7566 7567 static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc, 7568 uint32_t object_size) { 7569 if (dc.cmd == MachO::LC_DYLD_INFO) 7570 outs() << " cmd LC_DYLD_INFO\n"; 7571 else 7572 outs() << " cmd LC_DYLD_INFO_ONLY\n"; 7573 outs() << " cmdsize " << dc.cmdsize; 7574 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command)) 7575 outs() << " Incorrect size\n"; 7576 else 7577 outs() << "\n"; 7578 outs() << " rebase_off " << dc.rebase_off; 7579 if (dc.rebase_off > object_size) 7580 outs() << " (past end of file)\n"; 7581 else 7582 outs() << "\n"; 7583 outs() << " rebase_size " << dc.rebase_size; 7584 uint64_t big_size; 7585 big_size = dc.rebase_off; 7586 big_size += dc.rebase_size; 7587 if (big_size > object_size) 7588 outs() << " (past end of file)\n"; 7589 else 7590 outs() << "\n"; 7591 outs() << " bind_off " << dc.bind_off; 7592 if (dc.bind_off > object_size) 7593 outs() << " (past end of file)\n"; 7594 else 7595 outs() << "\n"; 7596 outs() << " bind_size " << dc.bind_size; 7597 big_size = dc.bind_off; 7598 big_size += dc.bind_size; 7599 if (big_size > object_size) 7600 outs() << " (past end of file)\n"; 7601 else 7602 outs() << "\n"; 7603 outs() << " weak_bind_off " << dc.weak_bind_off; 7604 if (dc.weak_bind_off > object_size) 7605 outs() << " (past end of file)\n"; 7606 else 7607 outs() << "\n"; 7608 outs() << " weak_bind_size " << dc.weak_bind_size; 7609 big_size = dc.weak_bind_off; 7610 big_size += dc.weak_bind_size; 7611 if (big_size > object_size) 7612 outs() << " (past end of file)\n"; 7613 else 7614 outs() << "\n"; 7615 outs() << " lazy_bind_off " << dc.lazy_bind_off; 7616 if (dc.lazy_bind_off > object_size) 7617 outs() << " (past end of file)\n"; 7618 else 7619 outs() << "\n"; 7620 outs() << " lazy_bind_size " << dc.lazy_bind_size; 7621 big_size = dc.lazy_bind_off; 7622 big_size += dc.lazy_bind_size; 7623 if (big_size > object_size) 7624 outs() << " (past end of file)\n"; 7625 else 7626 outs() << "\n"; 7627 outs() << " export_off " << dc.export_off; 7628 if (dc.export_off > object_size) 7629 outs() << " (past end of file)\n"; 7630 else 7631 outs() << "\n"; 7632 outs() << " export_size " << dc.export_size; 7633 big_size = dc.export_off; 7634 big_size += dc.export_size; 7635 if (big_size > object_size) 7636 outs() << " (past end of file)\n"; 7637 else 7638 outs() << "\n"; 7639 } 7640 7641 static void PrintDyldLoadCommand(MachO::dylinker_command dyld, 7642 const char *Ptr) { 7643 if (dyld.cmd == MachO::LC_ID_DYLINKER) 7644 outs() << " cmd LC_ID_DYLINKER\n"; 7645 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER) 7646 outs() << " cmd LC_LOAD_DYLINKER\n"; 7647 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT) 7648 outs() << " cmd LC_DYLD_ENVIRONMENT\n"; 7649 else 7650 outs() << " cmd ?(" << dyld.cmd << ")\n"; 7651 outs() << " cmdsize " << dyld.cmdsize; 7652 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command)) 7653 outs() << " Incorrect size\n"; 7654 else 7655 outs() << "\n"; 7656 if (dyld.name >= dyld.cmdsize) 7657 outs() << " name ?(bad offset " << dyld.name << ")\n"; 7658 else { 7659 const char *P = (const char *)(Ptr) + dyld.name; 7660 outs() << " name " << P << " (offset " << dyld.name << ")\n"; 7661 } 7662 } 7663 7664 static void PrintUuidLoadCommand(MachO::uuid_command uuid) { 7665 outs() << " cmd LC_UUID\n"; 7666 outs() << " cmdsize " << uuid.cmdsize; 7667 if (uuid.cmdsize != sizeof(struct MachO::uuid_command)) 7668 outs() << " Incorrect size\n"; 7669 else 7670 outs() << "\n"; 7671 outs() << " uuid "; 7672 outs() << format("%02" PRIX32, uuid.uuid[0]); 7673 outs() << format("%02" PRIX32, uuid.uuid[1]); 7674 outs() << format("%02" PRIX32, uuid.uuid[2]); 7675 outs() << format("%02" PRIX32, uuid.uuid[3]); 7676 outs() << "-"; 7677 outs() << format("%02" PRIX32, uuid.uuid[4]); 7678 outs() << format("%02" PRIX32, uuid.uuid[5]); 7679 outs() << "-"; 7680 outs() << format("%02" PRIX32, uuid.uuid[6]); 7681 outs() << format("%02" PRIX32, uuid.uuid[7]); 7682 outs() << "-"; 7683 outs() << format("%02" PRIX32, uuid.uuid[8]); 7684 outs() << format("%02" PRIX32, uuid.uuid[9]); 7685 outs() << "-"; 7686 outs() << format("%02" PRIX32, uuid.uuid[10]); 7687 outs() << format("%02" PRIX32, uuid.uuid[11]); 7688 outs() << format("%02" PRIX32, uuid.uuid[12]); 7689 outs() << format("%02" PRIX32, uuid.uuid[13]); 7690 outs() << format("%02" PRIX32, uuid.uuid[14]); 7691 outs() << format("%02" PRIX32, uuid.uuid[15]); 7692 outs() << "\n"; 7693 } 7694 7695 static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) { 7696 outs() << " cmd LC_RPATH\n"; 7697 outs() << " cmdsize " << rpath.cmdsize; 7698 if (rpath.cmdsize < sizeof(struct MachO::rpath_command)) 7699 outs() << " Incorrect size\n"; 7700 else 7701 outs() << "\n"; 7702 if (rpath.path >= rpath.cmdsize) 7703 outs() << " path ?(bad offset " << rpath.path << ")\n"; 7704 else { 7705 const char *P = (const char *)(Ptr) + rpath.path; 7706 outs() << " path " << P << " (offset " << rpath.path << ")\n"; 7707 } 7708 } 7709 7710 static void PrintVersionMinLoadCommand(MachO::version_min_command vd) { 7711 if (vd.cmd == MachO::LC_VERSION_MIN_MACOSX) 7712 outs() << " cmd LC_VERSION_MIN_MACOSX\n"; 7713 else if (vd.cmd == MachO::LC_VERSION_MIN_IPHONEOS) 7714 outs() << " cmd LC_VERSION_MIN_IPHONEOS\n"; 7715 else 7716 outs() << " cmd " << vd.cmd << " (?)\n"; 7717 outs() << " cmdsize " << vd.cmdsize; 7718 if (vd.cmdsize != sizeof(struct MachO::version_min_command)) 7719 outs() << " Incorrect size\n"; 7720 else 7721 outs() << "\n"; 7722 outs() << " version " << ((vd.version >> 16) & 0xffff) << "." 7723 << ((vd.version >> 8) & 0xff); 7724 if ((vd.version & 0xff) != 0) 7725 outs() << "." << (vd.version & 0xff); 7726 outs() << "\n"; 7727 if (vd.sdk == 0) 7728 outs() << " sdk n/a"; 7729 else { 7730 outs() << " sdk " << ((vd.sdk >> 16) & 0xffff) << "." 7731 << ((vd.sdk >> 8) & 0xff); 7732 } 7733 if ((vd.sdk & 0xff) != 0) 7734 outs() << "." << (vd.sdk & 0xff); 7735 outs() << "\n"; 7736 } 7737 7738 static void PrintSourceVersionCommand(MachO::source_version_command sd) { 7739 outs() << " cmd LC_SOURCE_VERSION\n"; 7740 outs() << " cmdsize " << sd.cmdsize; 7741 if (sd.cmdsize != sizeof(struct MachO::source_version_command)) 7742 outs() << " Incorrect size\n"; 7743 else 7744 outs() << "\n"; 7745 uint64_t a = (sd.version >> 40) & 0xffffff; 7746 uint64_t b = (sd.version >> 30) & 0x3ff; 7747 uint64_t c = (sd.version >> 20) & 0x3ff; 7748 uint64_t d = (sd.version >> 10) & 0x3ff; 7749 uint64_t e = sd.version & 0x3ff; 7750 outs() << " version " << a << "." << b; 7751 if (e != 0) 7752 outs() << "." << c << "." << d << "." << e; 7753 else if (d != 0) 7754 outs() << "." << c << "." << d; 7755 else if (c != 0) 7756 outs() << "." << c; 7757 outs() << "\n"; 7758 } 7759 7760 static void PrintEntryPointCommand(MachO::entry_point_command ep) { 7761 outs() << " cmd LC_MAIN\n"; 7762 outs() << " cmdsize " << ep.cmdsize; 7763 if (ep.cmdsize != sizeof(struct MachO::entry_point_command)) 7764 outs() << " Incorrect size\n"; 7765 else 7766 outs() << "\n"; 7767 outs() << " entryoff " << ep.entryoff << "\n"; 7768 outs() << " stacksize " << ep.stacksize << "\n"; 7769 } 7770 7771 static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec, 7772 uint32_t object_size) { 7773 outs() << " cmd LC_ENCRYPTION_INFO\n"; 7774 outs() << " cmdsize " << ec.cmdsize; 7775 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command)) 7776 outs() << " Incorrect size\n"; 7777 else 7778 outs() << "\n"; 7779 outs() << " cryptoff " << ec.cryptoff; 7780 if (ec.cryptoff > object_size) 7781 outs() << " (past end of file)\n"; 7782 else 7783 outs() << "\n"; 7784 outs() << " cryptsize " << ec.cryptsize; 7785 if (ec.cryptsize > object_size) 7786 outs() << " (past end of file)\n"; 7787 else 7788 outs() << "\n"; 7789 outs() << " cryptid " << ec.cryptid << "\n"; 7790 } 7791 7792 static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec, 7793 uint32_t object_size) { 7794 outs() << " cmd LC_ENCRYPTION_INFO_64\n"; 7795 outs() << " cmdsize " << ec.cmdsize; 7796 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64)) 7797 outs() << " Incorrect size\n"; 7798 else 7799 outs() << "\n"; 7800 outs() << " cryptoff " << ec.cryptoff; 7801 if (ec.cryptoff > object_size) 7802 outs() << " (past end of file)\n"; 7803 else 7804 outs() << "\n"; 7805 outs() << " cryptsize " << ec.cryptsize; 7806 if (ec.cryptsize > object_size) 7807 outs() << " (past end of file)\n"; 7808 else 7809 outs() << "\n"; 7810 outs() << " cryptid " << ec.cryptid << "\n"; 7811 outs() << " pad " << ec.pad << "\n"; 7812 } 7813 7814 static void PrintLinkerOptionCommand(MachO::linker_option_command lo, 7815 const char *Ptr) { 7816 outs() << " cmd LC_LINKER_OPTION\n"; 7817 outs() << " cmdsize " << lo.cmdsize; 7818 if (lo.cmdsize < sizeof(struct MachO::linker_option_command)) 7819 outs() << " Incorrect size\n"; 7820 else 7821 outs() << "\n"; 7822 outs() << " count " << lo.count << "\n"; 7823 const char *string = Ptr + sizeof(struct MachO::linker_option_command); 7824 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command); 7825 uint32_t i = 0; 7826 while (left > 0) { 7827 while (*string == '\0' && left > 0) { 7828 string++; 7829 left--; 7830 } 7831 if (left > 0) { 7832 i++; 7833 outs() << " string #" << i << " " << format("%.*s\n", left, string); 7834 uint32_t NullPos = StringRef(string, left).find('\0'); 7835 uint32_t len = std::min(NullPos, left) + 1; 7836 string += len; 7837 left -= len; 7838 } 7839 } 7840 if (lo.count != i) 7841 outs() << " count " << lo.count << " does not match number of strings " 7842 << i << "\n"; 7843 } 7844 7845 static void PrintSubFrameworkCommand(MachO::sub_framework_command sub, 7846 const char *Ptr) { 7847 outs() << " cmd LC_SUB_FRAMEWORK\n"; 7848 outs() << " cmdsize " << sub.cmdsize; 7849 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command)) 7850 outs() << " Incorrect size\n"; 7851 else 7852 outs() << "\n"; 7853 if (sub.umbrella < sub.cmdsize) { 7854 const char *P = Ptr + sub.umbrella; 7855 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n"; 7856 } else { 7857 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n"; 7858 } 7859 } 7860 7861 static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub, 7862 const char *Ptr) { 7863 outs() << " cmd LC_SUB_UMBRELLA\n"; 7864 outs() << " cmdsize " << sub.cmdsize; 7865 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command)) 7866 outs() << " Incorrect size\n"; 7867 else 7868 outs() << "\n"; 7869 if (sub.sub_umbrella < sub.cmdsize) { 7870 const char *P = Ptr + sub.sub_umbrella; 7871 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n"; 7872 } else { 7873 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n"; 7874 } 7875 } 7876 7877 static void PrintSubLibraryCommand(MachO::sub_library_command sub, 7878 const char *Ptr) { 7879 outs() << " cmd LC_SUB_LIBRARY\n"; 7880 outs() << " cmdsize " << sub.cmdsize; 7881 if (sub.cmdsize < sizeof(struct MachO::sub_library_command)) 7882 outs() << " Incorrect size\n"; 7883 else 7884 outs() << "\n"; 7885 if (sub.sub_library < sub.cmdsize) { 7886 const char *P = Ptr + sub.sub_library; 7887 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n"; 7888 } else { 7889 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n"; 7890 } 7891 } 7892 7893 static void PrintSubClientCommand(MachO::sub_client_command sub, 7894 const char *Ptr) { 7895 outs() << " cmd LC_SUB_CLIENT\n"; 7896 outs() << " cmdsize " << sub.cmdsize; 7897 if (sub.cmdsize < sizeof(struct MachO::sub_client_command)) 7898 outs() << " Incorrect size\n"; 7899 else 7900 outs() << "\n"; 7901 if (sub.client < sub.cmdsize) { 7902 const char *P = Ptr + sub.client; 7903 outs() << " client " << P << " (offset " << sub.client << ")\n"; 7904 } else { 7905 outs() << " client ?(bad offset " << sub.client << ")\n"; 7906 } 7907 } 7908 7909 static void PrintRoutinesCommand(MachO::routines_command r) { 7910 outs() << " cmd LC_ROUTINES\n"; 7911 outs() << " cmdsize " << r.cmdsize; 7912 if (r.cmdsize != sizeof(struct MachO::routines_command)) 7913 outs() << " Incorrect size\n"; 7914 else 7915 outs() << "\n"; 7916 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n"; 7917 outs() << " init_module " << r.init_module << "\n"; 7918 outs() << " reserved1 " << r.reserved1 << "\n"; 7919 outs() << " reserved2 " << r.reserved2 << "\n"; 7920 outs() << " reserved3 " << r.reserved3 << "\n"; 7921 outs() << " reserved4 " << r.reserved4 << "\n"; 7922 outs() << " reserved5 " << r.reserved5 << "\n"; 7923 outs() << " reserved6 " << r.reserved6 << "\n"; 7924 } 7925 7926 static void PrintRoutinesCommand64(MachO::routines_command_64 r) { 7927 outs() << " cmd LC_ROUTINES_64\n"; 7928 outs() << " cmdsize " << r.cmdsize; 7929 if (r.cmdsize != sizeof(struct MachO::routines_command_64)) 7930 outs() << " Incorrect size\n"; 7931 else 7932 outs() << "\n"; 7933 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n"; 7934 outs() << " init_module " << r.init_module << "\n"; 7935 outs() << " reserved1 " << r.reserved1 << "\n"; 7936 outs() << " reserved2 " << r.reserved2 << "\n"; 7937 outs() << " reserved3 " << r.reserved3 << "\n"; 7938 outs() << " reserved4 " << r.reserved4 << "\n"; 7939 outs() << " reserved5 " << r.reserved5 << "\n"; 7940 outs() << " reserved6 " << r.reserved6 << "\n"; 7941 } 7942 7943 static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) { 7944 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax); 7945 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx); 7946 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n"; 7947 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx); 7948 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi); 7949 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n"; 7950 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp); 7951 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp); 7952 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n"; 7953 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9); 7954 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10); 7955 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n"; 7956 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12); 7957 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13); 7958 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n"; 7959 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15); 7960 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n"; 7961 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags); 7962 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs); 7963 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n"; 7964 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n"; 7965 } 7966 7967 static void Print_mmst_reg(MachO::mmst_reg_t &r) { 7968 uint32_t f; 7969 outs() << "\t mmst_reg "; 7970 for (f = 0; f < 10; f++) 7971 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " "; 7972 outs() << "\n"; 7973 outs() << "\t mmst_rsrv "; 7974 for (f = 0; f < 6; f++) 7975 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " "; 7976 outs() << "\n"; 7977 } 7978 7979 static void Print_xmm_reg(MachO::xmm_reg_t &r) { 7980 uint32_t f; 7981 outs() << "\t xmm_reg "; 7982 for (f = 0; f < 16; f++) 7983 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " "; 7984 outs() << "\n"; 7985 } 7986 7987 static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) { 7988 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0]; 7989 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n"; 7990 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid; 7991 outs() << " denorm " << fpu.fpu_fcw.denorm; 7992 outs() << " zdiv " << fpu.fpu_fcw.zdiv; 7993 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl; 7994 outs() << " undfl " << fpu.fpu_fcw.undfl; 7995 outs() << " precis " << fpu.fpu_fcw.precis << "\n"; 7996 outs() << "\t\t pc "; 7997 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B) 7998 outs() << "FP_PREC_24B "; 7999 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B) 8000 outs() << "FP_PREC_53B "; 8001 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B) 8002 outs() << "FP_PREC_64B "; 8003 else 8004 outs() << fpu.fpu_fcw.pc << " "; 8005 outs() << "rc "; 8006 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR) 8007 outs() << "FP_RND_NEAR "; 8008 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN) 8009 outs() << "FP_RND_DOWN "; 8010 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP) 8011 outs() << "FP_RND_UP "; 8012 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP) 8013 outs() << "FP_CHOP "; 8014 outs() << "\n"; 8015 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid; 8016 outs() << " denorm " << fpu.fpu_fsw.denorm; 8017 outs() << " zdiv " << fpu.fpu_fsw.zdiv; 8018 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl; 8019 outs() << " undfl " << fpu.fpu_fsw.undfl; 8020 outs() << " precis " << fpu.fpu_fsw.precis; 8021 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n"; 8022 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm; 8023 outs() << " c0 " << fpu.fpu_fsw.c0; 8024 outs() << " c1 " << fpu.fpu_fsw.c1; 8025 outs() << " c2 " << fpu.fpu_fsw.c2; 8026 outs() << " tos " << fpu.fpu_fsw.tos; 8027 outs() << " c3 " << fpu.fpu_fsw.c3; 8028 outs() << " busy " << fpu.fpu_fsw.busy << "\n"; 8029 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw); 8030 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1); 8031 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop); 8032 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n"; 8033 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs); 8034 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2); 8035 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp); 8036 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n"; 8037 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3); 8038 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr); 8039 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask); 8040 outs() << "\n"; 8041 outs() << "\t fpu_stmm0:\n"; 8042 Print_mmst_reg(fpu.fpu_stmm0); 8043 outs() << "\t fpu_stmm1:\n"; 8044 Print_mmst_reg(fpu.fpu_stmm1); 8045 outs() << "\t fpu_stmm2:\n"; 8046 Print_mmst_reg(fpu.fpu_stmm2); 8047 outs() << "\t fpu_stmm3:\n"; 8048 Print_mmst_reg(fpu.fpu_stmm3); 8049 outs() << "\t fpu_stmm4:\n"; 8050 Print_mmst_reg(fpu.fpu_stmm4); 8051 outs() << "\t fpu_stmm5:\n"; 8052 Print_mmst_reg(fpu.fpu_stmm5); 8053 outs() << "\t fpu_stmm6:\n"; 8054 Print_mmst_reg(fpu.fpu_stmm6); 8055 outs() << "\t fpu_stmm7:\n"; 8056 Print_mmst_reg(fpu.fpu_stmm7); 8057 outs() << "\t fpu_xmm0:\n"; 8058 Print_xmm_reg(fpu.fpu_xmm0); 8059 outs() << "\t fpu_xmm1:\n"; 8060 Print_xmm_reg(fpu.fpu_xmm1); 8061 outs() << "\t fpu_xmm2:\n"; 8062 Print_xmm_reg(fpu.fpu_xmm2); 8063 outs() << "\t fpu_xmm3:\n"; 8064 Print_xmm_reg(fpu.fpu_xmm3); 8065 outs() << "\t fpu_xmm4:\n"; 8066 Print_xmm_reg(fpu.fpu_xmm4); 8067 outs() << "\t fpu_xmm5:\n"; 8068 Print_xmm_reg(fpu.fpu_xmm5); 8069 outs() << "\t fpu_xmm6:\n"; 8070 Print_xmm_reg(fpu.fpu_xmm6); 8071 outs() << "\t fpu_xmm7:\n"; 8072 Print_xmm_reg(fpu.fpu_xmm7); 8073 outs() << "\t fpu_xmm8:\n"; 8074 Print_xmm_reg(fpu.fpu_xmm8); 8075 outs() << "\t fpu_xmm9:\n"; 8076 Print_xmm_reg(fpu.fpu_xmm9); 8077 outs() << "\t fpu_xmm10:\n"; 8078 Print_xmm_reg(fpu.fpu_xmm10); 8079 outs() << "\t fpu_xmm11:\n"; 8080 Print_xmm_reg(fpu.fpu_xmm11); 8081 outs() << "\t fpu_xmm12:\n"; 8082 Print_xmm_reg(fpu.fpu_xmm12); 8083 outs() << "\t fpu_xmm13:\n"; 8084 Print_xmm_reg(fpu.fpu_xmm13); 8085 outs() << "\t fpu_xmm14:\n"; 8086 Print_xmm_reg(fpu.fpu_xmm14); 8087 outs() << "\t fpu_xmm15:\n"; 8088 Print_xmm_reg(fpu.fpu_xmm15); 8089 outs() << "\t fpu_rsrv4:\n"; 8090 for (uint32_t f = 0; f < 6; f++) { 8091 outs() << "\t "; 8092 for (uint32_t g = 0; g < 16; g++) 8093 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " "; 8094 outs() << "\n"; 8095 } 8096 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1); 8097 outs() << "\n"; 8098 } 8099 8100 static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) { 8101 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno); 8102 outs() << " err " << format("0x%08" PRIx32, exc64.err); 8103 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n"; 8104 } 8105 8106 static void PrintThreadCommand(MachO::thread_command t, const char *Ptr, 8107 bool isLittleEndian, uint32_t cputype) { 8108 if (t.cmd == MachO::LC_THREAD) 8109 outs() << " cmd LC_THREAD\n"; 8110 else if (t.cmd == MachO::LC_UNIXTHREAD) 8111 outs() << " cmd LC_UNIXTHREAD\n"; 8112 else 8113 outs() << " cmd " << t.cmd << " (unknown)\n"; 8114 outs() << " cmdsize " << t.cmdsize; 8115 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t)) 8116 outs() << " Incorrect size\n"; 8117 else 8118 outs() << "\n"; 8119 8120 const char *begin = Ptr + sizeof(struct MachO::thread_command); 8121 const char *end = Ptr + t.cmdsize; 8122 uint32_t flavor, count, left; 8123 if (cputype == MachO::CPU_TYPE_X86_64) { 8124 while (begin < end) { 8125 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 8126 memcpy((char *)&flavor, begin, sizeof(uint32_t)); 8127 begin += sizeof(uint32_t); 8128 } else { 8129 flavor = 0; 8130 begin = end; 8131 } 8132 if (isLittleEndian != sys::IsLittleEndianHost) 8133 sys::swapByteOrder(flavor); 8134 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 8135 memcpy((char *)&count, begin, sizeof(uint32_t)); 8136 begin += sizeof(uint32_t); 8137 } else { 8138 count = 0; 8139 begin = end; 8140 } 8141 if (isLittleEndian != sys::IsLittleEndianHost) 8142 sys::swapByteOrder(count); 8143 if (flavor == MachO::x86_THREAD_STATE64) { 8144 outs() << " flavor x86_THREAD_STATE64\n"; 8145 if (count == MachO::x86_THREAD_STATE64_COUNT) 8146 outs() << " count x86_THREAD_STATE64_COUNT\n"; 8147 else 8148 outs() << " count " << count 8149 << " (not x86_THREAD_STATE64_COUNT)\n"; 8150 MachO::x86_thread_state64_t cpu64; 8151 left = end - begin; 8152 if (left >= sizeof(MachO::x86_thread_state64_t)) { 8153 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t)); 8154 begin += sizeof(MachO::x86_thread_state64_t); 8155 } else { 8156 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t)); 8157 memcpy(&cpu64, begin, left); 8158 begin += left; 8159 } 8160 if (isLittleEndian != sys::IsLittleEndianHost) 8161 swapStruct(cpu64); 8162 Print_x86_thread_state64_t(cpu64); 8163 } else if (flavor == MachO::x86_THREAD_STATE) { 8164 outs() << " flavor x86_THREAD_STATE\n"; 8165 if (count == MachO::x86_THREAD_STATE_COUNT) 8166 outs() << " count x86_THREAD_STATE_COUNT\n"; 8167 else 8168 outs() << " count " << count 8169 << " (not x86_THREAD_STATE_COUNT)\n"; 8170 struct MachO::x86_thread_state_t ts; 8171 left = end - begin; 8172 if (left >= sizeof(MachO::x86_thread_state_t)) { 8173 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t)); 8174 begin += sizeof(MachO::x86_thread_state_t); 8175 } else { 8176 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t)); 8177 memcpy(&ts, begin, left); 8178 begin += left; 8179 } 8180 if (isLittleEndian != sys::IsLittleEndianHost) 8181 swapStruct(ts); 8182 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) { 8183 outs() << "\t tsh.flavor x86_THREAD_STATE64 "; 8184 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT) 8185 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n"; 8186 else 8187 outs() << "tsh.count " << ts.tsh.count 8188 << " (not x86_THREAD_STATE64_COUNT\n"; 8189 Print_x86_thread_state64_t(ts.uts.ts64); 8190 } else { 8191 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count " 8192 << ts.tsh.count << "\n"; 8193 } 8194 } else if (flavor == MachO::x86_FLOAT_STATE) { 8195 outs() << " flavor x86_FLOAT_STATE\n"; 8196 if (count == MachO::x86_FLOAT_STATE_COUNT) 8197 outs() << " count x86_FLOAT_STATE_COUNT\n"; 8198 else 8199 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n"; 8200 struct MachO::x86_float_state_t fs; 8201 left = end - begin; 8202 if (left >= sizeof(MachO::x86_float_state_t)) { 8203 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t)); 8204 begin += sizeof(MachO::x86_float_state_t); 8205 } else { 8206 memset(&fs, '\0', sizeof(MachO::x86_float_state_t)); 8207 memcpy(&fs, begin, left); 8208 begin += left; 8209 } 8210 if (isLittleEndian != sys::IsLittleEndianHost) 8211 swapStruct(fs); 8212 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) { 8213 outs() << "\t fsh.flavor x86_FLOAT_STATE64 "; 8214 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT) 8215 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n"; 8216 else 8217 outs() << "fsh.count " << fs.fsh.count 8218 << " (not x86_FLOAT_STATE64_COUNT\n"; 8219 Print_x86_float_state_t(fs.ufs.fs64); 8220 } else { 8221 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count " 8222 << fs.fsh.count << "\n"; 8223 } 8224 } else if (flavor == MachO::x86_EXCEPTION_STATE) { 8225 outs() << " flavor x86_EXCEPTION_STATE\n"; 8226 if (count == MachO::x86_EXCEPTION_STATE_COUNT) 8227 outs() << " count x86_EXCEPTION_STATE_COUNT\n"; 8228 else 8229 outs() << " count " << count 8230 << " (not x86_EXCEPTION_STATE_COUNT)\n"; 8231 struct MachO::x86_exception_state_t es; 8232 left = end - begin; 8233 if (left >= sizeof(MachO::x86_exception_state_t)) { 8234 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t)); 8235 begin += sizeof(MachO::x86_exception_state_t); 8236 } else { 8237 memset(&es, '\0', sizeof(MachO::x86_exception_state_t)); 8238 memcpy(&es, begin, left); 8239 begin += left; 8240 } 8241 if (isLittleEndian != sys::IsLittleEndianHost) 8242 swapStruct(es); 8243 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) { 8244 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n"; 8245 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT) 8246 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n"; 8247 else 8248 outs() << "\t esh.count " << es.esh.count 8249 << " (not x86_EXCEPTION_STATE64_COUNT\n"; 8250 Print_x86_exception_state_t(es.ues.es64); 8251 } else { 8252 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count " 8253 << es.esh.count << "\n"; 8254 } 8255 } else { 8256 outs() << " flavor " << flavor << " (unknown)\n"; 8257 outs() << " count " << count << "\n"; 8258 outs() << " state (unknown)\n"; 8259 begin += count * sizeof(uint32_t); 8260 } 8261 } 8262 } else { 8263 while (begin < end) { 8264 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 8265 memcpy((char *)&flavor, begin, sizeof(uint32_t)); 8266 begin += sizeof(uint32_t); 8267 } else { 8268 flavor = 0; 8269 begin = end; 8270 } 8271 if (isLittleEndian != sys::IsLittleEndianHost) 8272 sys::swapByteOrder(flavor); 8273 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { 8274 memcpy((char *)&count, begin, sizeof(uint32_t)); 8275 begin += sizeof(uint32_t); 8276 } else { 8277 count = 0; 8278 begin = end; 8279 } 8280 if (isLittleEndian != sys::IsLittleEndianHost) 8281 sys::swapByteOrder(count); 8282 outs() << " flavor " << flavor << "\n"; 8283 outs() << " count " << count << "\n"; 8284 outs() << " state (Unknown cputype/cpusubtype)\n"; 8285 begin += count * sizeof(uint32_t); 8286 } 8287 } 8288 } 8289 8290 static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) { 8291 if (dl.cmd == MachO::LC_ID_DYLIB) 8292 outs() << " cmd LC_ID_DYLIB\n"; 8293 else if (dl.cmd == MachO::LC_LOAD_DYLIB) 8294 outs() << " cmd LC_LOAD_DYLIB\n"; 8295 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB) 8296 outs() << " cmd LC_LOAD_WEAK_DYLIB\n"; 8297 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB) 8298 outs() << " cmd LC_REEXPORT_DYLIB\n"; 8299 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB) 8300 outs() << " cmd LC_LAZY_LOAD_DYLIB\n"; 8301 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB) 8302 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n"; 8303 else 8304 outs() << " cmd " << dl.cmd << " (unknown)\n"; 8305 outs() << " cmdsize " << dl.cmdsize; 8306 if (dl.cmdsize < sizeof(struct MachO::dylib_command)) 8307 outs() << " Incorrect size\n"; 8308 else 8309 outs() << "\n"; 8310 if (dl.dylib.name < dl.cmdsize) { 8311 const char *P = (const char *)(Ptr) + dl.dylib.name; 8312 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n"; 8313 } else { 8314 outs() << " name ?(bad offset " << dl.dylib.name << ")\n"; 8315 } 8316 outs() << " time stamp " << dl.dylib.timestamp << " "; 8317 time_t t = dl.dylib.timestamp; 8318 outs() << ctime(&t); 8319 outs() << " current version "; 8320 if (dl.dylib.current_version == 0xffffffff) 8321 outs() << "n/a\n"; 8322 else 8323 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "." 8324 << ((dl.dylib.current_version >> 8) & 0xff) << "." 8325 << (dl.dylib.current_version & 0xff) << "\n"; 8326 outs() << "compatibility version "; 8327 if (dl.dylib.compatibility_version == 0xffffffff) 8328 outs() << "n/a\n"; 8329 else 8330 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "." 8331 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "." 8332 << (dl.dylib.compatibility_version & 0xff) << "\n"; 8333 } 8334 8335 static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld, 8336 uint32_t object_size) { 8337 if (ld.cmd == MachO::LC_CODE_SIGNATURE) 8338 outs() << " cmd LC_FUNCTION_STARTS\n"; 8339 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO) 8340 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n"; 8341 else if (ld.cmd == MachO::LC_FUNCTION_STARTS) 8342 outs() << " cmd LC_FUNCTION_STARTS\n"; 8343 else if (ld.cmd == MachO::LC_DATA_IN_CODE) 8344 outs() << " cmd LC_DATA_IN_CODE\n"; 8345 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS) 8346 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n"; 8347 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) 8348 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n"; 8349 else 8350 outs() << " cmd " << ld.cmd << " (?)\n"; 8351 outs() << " cmdsize " << ld.cmdsize; 8352 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command)) 8353 outs() << " Incorrect size\n"; 8354 else 8355 outs() << "\n"; 8356 outs() << " dataoff " << ld.dataoff; 8357 if (ld.dataoff > object_size) 8358 outs() << " (past end of file)\n"; 8359 else 8360 outs() << "\n"; 8361 outs() << " datasize " << ld.datasize; 8362 uint64_t big_size = ld.dataoff; 8363 big_size += ld.datasize; 8364 if (big_size > object_size) 8365 outs() << " (past end of file)\n"; 8366 else 8367 outs() << "\n"; 8368 } 8369 8370 static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t ncmds, 8371 uint32_t filetype, uint32_t cputype, 8372 bool verbose) { 8373 if (ncmds == 0) 8374 return; 8375 StringRef Buf = Obj->getData(); 8376 MachOObjectFile::LoadCommandInfo Command = Obj->getFirstLoadCommandInfo(); 8377 for (unsigned i = 0;; ++i) { 8378 outs() << "Load command " << i << "\n"; 8379 if (Command.C.cmd == MachO::LC_SEGMENT) { 8380 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command); 8381 const char *sg_segname = SLC.segname; 8382 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr, 8383 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot, 8384 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(), 8385 verbose); 8386 for (unsigned j = 0; j < SLC.nsects; j++) { 8387 MachO::section S = Obj->getSection(Command, j); 8388 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align, 8389 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2, 8390 SLC.cmd, sg_segname, filetype, Buf.size(), verbose); 8391 } 8392 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) { 8393 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command); 8394 const char *sg_segname = SLC_64.segname; 8395 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname, 8396 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff, 8397 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot, 8398 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose); 8399 for (unsigned j = 0; j < SLC_64.nsects; j++) { 8400 MachO::section_64 S_64 = Obj->getSection64(Command, j); 8401 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size, 8402 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc, 8403 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd, 8404 sg_segname, filetype, Buf.size(), verbose); 8405 } 8406 } else if (Command.C.cmd == MachO::LC_SYMTAB) { 8407 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand(); 8408 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size()); 8409 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) { 8410 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand(); 8411 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand(); 8412 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(), 8413 Obj->is64Bit()); 8414 } else if (Command.C.cmd == MachO::LC_DYLD_INFO || 8415 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) { 8416 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command); 8417 PrintDyldInfoLoadCommand(DyldInfo, Buf.size()); 8418 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER || 8419 Command.C.cmd == MachO::LC_ID_DYLINKER || 8420 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) { 8421 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command); 8422 PrintDyldLoadCommand(Dyld, Command.Ptr); 8423 } else if (Command.C.cmd == MachO::LC_UUID) { 8424 MachO::uuid_command Uuid = Obj->getUuidCommand(Command); 8425 PrintUuidLoadCommand(Uuid); 8426 } else if (Command.C.cmd == MachO::LC_RPATH) { 8427 MachO::rpath_command Rpath = Obj->getRpathCommand(Command); 8428 PrintRpathLoadCommand(Rpath, Command.Ptr); 8429 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX || 8430 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS) { 8431 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command); 8432 PrintVersionMinLoadCommand(Vd); 8433 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) { 8434 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command); 8435 PrintSourceVersionCommand(Sd); 8436 } else if (Command.C.cmd == MachO::LC_MAIN) { 8437 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command); 8438 PrintEntryPointCommand(Ep); 8439 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) { 8440 MachO::encryption_info_command Ei = 8441 Obj->getEncryptionInfoCommand(Command); 8442 PrintEncryptionInfoCommand(Ei, Buf.size()); 8443 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) { 8444 MachO::encryption_info_command_64 Ei = 8445 Obj->getEncryptionInfoCommand64(Command); 8446 PrintEncryptionInfoCommand64(Ei, Buf.size()); 8447 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) { 8448 MachO::linker_option_command Lo = 8449 Obj->getLinkerOptionLoadCommand(Command); 8450 PrintLinkerOptionCommand(Lo, Command.Ptr); 8451 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) { 8452 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command); 8453 PrintSubFrameworkCommand(Sf, Command.Ptr); 8454 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) { 8455 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command); 8456 PrintSubUmbrellaCommand(Sf, Command.Ptr); 8457 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) { 8458 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command); 8459 PrintSubLibraryCommand(Sl, Command.Ptr); 8460 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) { 8461 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command); 8462 PrintSubClientCommand(Sc, Command.Ptr); 8463 } else if (Command.C.cmd == MachO::LC_ROUTINES) { 8464 MachO::routines_command Rc = Obj->getRoutinesCommand(Command); 8465 PrintRoutinesCommand(Rc); 8466 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) { 8467 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command); 8468 PrintRoutinesCommand64(Rc); 8469 } else if (Command.C.cmd == MachO::LC_THREAD || 8470 Command.C.cmd == MachO::LC_UNIXTHREAD) { 8471 MachO::thread_command Tc = Obj->getThreadCommand(Command); 8472 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype); 8473 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB || 8474 Command.C.cmd == MachO::LC_ID_DYLIB || 8475 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB || 8476 Command.C.cmd == MachO::LC_REEXPORT_DYLIB || 8477 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB || 8478 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) { 8479 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command); 8480 PrintDylibCommand(Dl, Command.Ptr); 8481 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE || 8482 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO || 8483 Command.C.cmd == MachO::LC_FUNCTION_STARTS || 8484 Command.C.cmd == MachO::LC_DATA_IN_CODE || 8485 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS || 8486 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) { 8487 MachO::linkedit_data_command Ld = 8488 Obj->getLinkeditDataLoadCommand(Command); 8489 PrintLinkEditDataCommand(Ld, Buf.size()); 8490 } else { 8491 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd) 8492 << ")\n"; 8493 outs() << " cmdsize " << Command.C.cmdsize << "\n"; 8494 // TODO: get and print the raw bytes of the load command. 8495 } 8496 // TODO: print all the other kinds of load commands. 8497 if (i == ncmds - 1) 8498 break; 8499 else 8500 Command = Obj->getNextLoadCommandInfo(Command); 8501 } 8502 } 8503 8504 static void getAndPrintMachHeader(const MachOObjectFile *Obj, uint32_t &ncmds, 8505 uint32_t &filetype, uint32_t &cputype, 8506 bool verbose) { 8507 if (Obj->is64Bit()) { 8508 MachO::mach_header_64 H_64; 8509 H_64 = Obj->getHeader64(); 8510 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype, 8511 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose); 8512 ncmds = H_64.ncmds; 8513 filetype = H_64.filetype; 8514 cputype = H_64.cputype; 8515 } else { 8516 MachO::mach_header H; 8517 H = Obj->getHeader(); 8518 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds, 8519 H.sizeofcmds, H.flags, verbose); 8520 ncmds = H.ncmds; 8521 filetype = H.filetype; 8522 cputype = H.cputype; 8523 } 8524 } 8525 8526 void llvm::printMachOFileHeader(const object::ObjectFile *Obj) { 8527 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj); 8528 uint32_t ncmds = 0; 8529 uint32_t filetype = 0; 8530 uint32_t cputype = 0; 8531 getAndPrintMachHeader(file, ncmds, filetype, cputype, !NonVerbose); 8532 PrintLoadCommands(file, ncmds, filetype, cputype, !NonVerbose); 8533 } 8534 8535 //===----------------------------------------------------------------------===// 8536 // export trie dumping 8537 //===----------------------------------------------------------------------===// 8538 8539 void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) { 8540 for (const llvm::object::ExportEntry &Entry : Obj->exports()) { 8541 uint64_t Flags = Entry.flags(); 8542 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT); 8543 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION); 8544 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) == 8545 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL); 8546 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) == 8547 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE); 8548 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER); 8549 if (ReExport) 8550 outs() << "[re-export] "; 8551 else 8552 outs() << format("0x%08llX ", 8553 Entry.address()); // FIXME:add in base address 8554 outs() << Entry.name(); 8555 if (WeakDef || ThreadLocal || Resolver || Abs) { 8556 bool NeedsComma = false; 8557 outs() << " ["; 8558 if (WeakDef) { 8559 outs() << "weak_def"; 8560 NeedsComma = true; 8561 } 8562 if (ThreadLocal) { 8563 if (NeedsComma) 8564 outs() << ", "; 8565 outs() << "per-thread"; 8566 NeedsComma = true; 8567 } 8568 if (Abs) { 8569 if (NeedsComma) 8570 outs() << ", "; 8571 outs() << "absolute"; 8572 NeedsComma = true; 8573 } 8574 if (Resolver) { 8575 if (NeedsComma) 8576 outs() << ", "; 8577 outs() << format("resolver=0x%08llX", Entry.other()); 8578 NeedsComma = true; 8579 } 8580 outs() << "]"; 8581 } 8582 if (ReExport) { 8583 StringRef DylibName = "unknown"; 8584 int Ordinal = Entry.other() - 1; 8585 Obj->getLibraryShortNameByIndex(Ordinal, DylibName); 8586 if (Entry.otherName().empty()) 8587 outs() << " (from " << DylibName << ")"; 8588 else 8589 outs() << " (" << Entry.otherName() << " from " << DylibName << ")"; 8590 } 8591 outs() << "\n"; 8592 } 8593 } 8594 8595 //===----------------------------------------------------------------------===// 8596 // rebase table dumping 8597 //===----------------------------------------------------------------------===// 8598 8599 namespace { 8600 class SegInfo { 8601 public: 8602 SegInfo(const object::MachOObjectFile *Obj); 8603 8604 StringRef segmentName(uint32_t SegIndex); 8605 StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset); 8606 uint64_t address(uint32_t SegIndex, uint64_t SegOffset); 8607 8608 private: 8609 struct SectionInfo { 8610 uint64_t Address; 8611 uint64_t Size; 8612 StringRef SectionName; 8613 StringRef SegmentName; 8614 uint64_t OffsetInSegment; 8615 uint64_t SegmentStartAddress; 8616 uint32_t SegmentIndex; 8617 }; 8618 const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset); 8619 SmallVector<SectionInfo, 32> Sections; 8620 }; 8621 } 8622 8623 SegInfo::SegInfo(const object::MachOObjectFile *Obj) { 8624 // Build table of sections so segIndex/offset pairs can be translated. 8625 uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0; 8626 StringRef CurSegName; 8627 uint64_t CurSegAddress; 8628 for (const SectionRef &Section : Obj->sections()) { 8629 SectionInfo Info; 8630 if (error(Section.getName(Info.SectionName))) 8631 return; 8632 Info.Address = Section.getAddress(); 8633 Info.Size = Section.getSize(); 8634 Info.SegmentName = 8635 Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl()); 8636 if (!Info.SegmentName.equals(CurSegName)) { 8637 ++CurSegIndex; 8638 CurSegName = Info.SegmentName; 8639 CurSegAddress = Info.Address; 8640 } 8641 Info.SegmentIndex = CurSegIndex - 1; 8642 Info.OffsetInSegment = Info.Address - CurSegAddress; 8643 Info.SegmentStartAddress = CurSegAddress; 8644 Sections.push_back(Info); 8645 } 8646 } 8647 8648 StringRef SegInfo::segmentName(uint32_t SegIndex) { 8649 for (const SectionInfo &SI : Sections) { 8650 if (SI.SegmentIndex == SegIndex) 8651 return SI.SegmentName; 8652 } 8653 llvm_unreachable("invalid segIndex"); 8654 } 8655 8656 const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex, 8657 uint64_t OffsetInSeg) { 8658 for (const SectionInfo &SI : Sections) { 8659 if (SI.SegmentIndex != SegIndex) 8660 continue; 8661 if (SI.OffsetInSegment > OffsetInSeg) 8662 continue; 8663 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size)) 8664 continue; 8665 return SI; 8666 } 8667 llvm_unreachable("segIndex and offset not in any section"); 8668 } 8669 8670 StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) { 8671 return findSection(SegIndex, OffsetInSeg).SectionName; 8672 } 8673 8674 uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) { 8675 const SectionInfo &SI = findSection(SegIndex, OffsetInSeg); 8676 return SI.SegmentStartAddress + OffsetInSeg; 8677 } 8678 8679 void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) { 8680 // Build table of sections so names can used in final output. 8681 SegInfo sectionTable(Obj); 8682 8683 outs() << "segment section address type\n"; 8684 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) { 8685 uint32_t SegIndex = Entry.segmentIndex(); 8686 uint64_t OffsetInSeg = Entry.segmentOffset(); 8687 StringRef SegmentName = sectionTable.segmentName(SegIndex); 8688 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg); 8689 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg); 8690 8691 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer 8692 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n", 8693 SegmentName.str().c_str(), SectionName.str().c_str(), 8694 Address, Entry.typeName().str().c_str()); 8695 } 8696 } 8697 8698 static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) { 8699 StringRef DylibName; 8700 switch (Ordinal) { 8701 case MachO::BIND_SPECIAL_DYLIB_SELF: 8702 return "this-image"; 8703 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE: 8704 return "main-executable"; 8705 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP: 8706 return "flat-namespace"; 8707 default: 8708 if (Ordinal > 0) { 8709 std::error_code EC = 8710 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName); 8711 if (EC) 8712 return "<<bad library ordinal>>"; 8713 return DylibName; 8714 } 8715 } 8716 return "<<unknown special ordinal>>"; 8717 } 8718 8719 //===----------------------------------------------------------------------===// 8720 // bind table dumping 8721 //===----------------------------------------------------------------------===// 8722 8723 void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) { 8724 // Build table of sections so names can used in final output. 8725 SegInfo sectionTable(Obj); 8726 8727 outs() << "segment section address type " 8728 "addend dylib symbol\n"; 8729 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) { 8730 uint32_t SegIndex = Entry.segmentIndex(); 8731 uint64_t OffsetInSeg = Entry.segmentOffset(); 8732 StringRef SegmentName = sectionTable.segmentName(SegIndex); 8733 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg); 8734 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg); 8735 8736 // Table lines look like: 8737 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard 8738 StringRef Attr; 8739 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT) 8740 Attr = " (weak_import)"; 8741 outs() << left_justify(SegmentName, 8) << " " 8742 << left_justify(SectionName, 18) << " " 8743 << format_hex(Address, 10, true) << " " 8744 << left_justify(Entry.typeName(), 8) << " " 8745 << format_decimal(Entry.addend(), 8) << " " 8746 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " " 8747 << Entry.symbolName() << Attr << "\n"; 8748 } 8749 } 8750 8751 //===----------------------------------------------------------------------===// 8752 // lazy bind table dumping 8753 //===----------------------------------------------------------------------===// 8754 8755 void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) { 8756 // Build table of sections so names can used in final output. 8757 SegInfo sectionTable(Obj); 8758 8759 outs() << "segment section address " 8760 "dylib symbol\n"; 8761 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) { 8762 uint32_t SegIndex = Entry.segmentIndex(); 8763 uint64_t OffsetInSeg = Entry.segmentOffset(); 8764 StringRef SegmentName = sectionTable.segmentName(SegIndex); 8765 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg); 8766 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg); 8767 8768 // Table lines look like: 8769 // __DATA __got 0x00012010 libSystem ___stack_chk_guard 8770 outs() << left_justify(SegmentName, 8) << " " 8771 << left_justify(SectionName, 18) << " " 8772 << format_hex(Address, 10, true) << " " 8773 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " " 8774 << Entry.symbolName() << "\n"; 8775 } 8776 } 8777 8778 //===----------------------------------------------------------------------===// 8779 // weak bind table dumping 8780 //===----------------------------------------------------------------------===// 8781 8782 void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) { 8783 // Build table of sections so names can used in final output. 8784 SegInfo sectionTable(Obj); 8785 8786 outs() << "segment section address " 8787 "type addend symbol\n"; 8788 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) { 8789 // Strong symbols don't have a location to update. 8790 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) { 8791 outs() << " strong " 8792 << Entry.symbolName() << "\n"; 8793 continue; 8794 } 8795 uint32_t SegIndex = Entry.segmentIndex(); 8796 uint64_t OffsetInSeg = Entry.segmentOffset(); 8797 StringRef SegmentName = sectionTable.segmentName(SegIndex); 8798 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg); 8799 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg); 8800 8801 // Table lines look like: 8802 // __DATA __data 0x00001000 pointer 0 _foo 8803 outs() << left_justify(SegmentName, 8) << " " 8804 << left_justify(SectionName, 18) << " " 8805 << format_hex(Address, 10, true) << " " 8806 << left_justify(Entry.typeName(), 8) << " " 8807 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName() 8808 << "\n"; 8809 } 8810 } 8811 8812 // get_dyld_bind_info_symbolname() is used for disassembly and passed an 8813 // address, ReferenceValue, in the Mach-O file and looks in the dyld bind 8814 // information for that address. If the address is found its binding symbol 8815 // name is returned. If not nullptr is returned. 8816 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue, 8817 struct DisassembleInfo *info) { 8818 if (info->bindtable == nullptr) { 8819 info->bindtable = new (BindTable); 8820 SegInfo sectionTable(info->O); 8821 for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable()) { 8822 uint32_t SegIndex = Entry.segmentIndex(); 8823 uint64_t OffsetInSeg = Entry.segmentOffset(); 8824 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg); 8825 const char *SymbolName = nullptr; 8826 StringRef name = Entry.symbolName(); 8827 if (!name.empty()) 8828 SymbolName = name.data(); 8829 info->bindtable->push_back(std::make_pair(Address, SymbolName)); 8830 } 8831 } 8832 for (bind_table_iterator BI = info->bindtable->begin(), 8833 BE = info->bindtable->end(); 8834 BI != BE; ++BI) { 8835 uint64_t Address = BI->first; 8836 if (ReferenceValue == Address) { 8837 const char *SymbolName = BI->second; 8838 return SymbolName; 8839 } 8840 } 8841 return nullptr; 8842 } 8843