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