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