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