1 //===-- MachODump.cpp - Object file dumping utility for llvm --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the MachO-specific dumper for llvm-objdump.
12 //===----------------------------------------------------------------------===//
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/DWARF/DIContext.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCDisassembler.h"
24 #include "llvm/MC/MCInst.h"
25 #include "llvm/MC/MCInstPrinter.h"
26 #include "llvm/MC/MCInstrDesc.h"
27 #include "llvm/MC/MCInstrInfo.h"
28 #include "llvm/MC/MCRegisterInfo.h"
29 #include "llvm/MC/MCSubtargetInfo.h"
30 #include "llvm/Object/MachO.h"
31 #include "llvm/Object/MachOUniversal.h"
32 #include "llvm/Support/Casting.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/Endian.h"
36 #include "llvm/Support/Format.h"
37 #include "llvm/Support/FormattedStream.h"
38 #include "llvm/Support/GraphWriter.h"
39 #include "llvm/Support/LEB128.h"
40 #include "llvm/Support/MachO.h"
41 #include "llvm/Support/MemoryBuffer.h"
42 #include "llvm/Support/TargetRegistry.h"
43 #include "llvm/Support/TargetSelect.h"
44 #include "llvm/Support/raw_ostream.h"
47 #include <system_error>
54 using namespace object;
58 cl::desc("Print line information from debug info if available"));
60 static cl::opt<std::string> DSYMFile("dsym",
61 cl::desc("Use .dSYM file for debug info"));
63 static cl::opt<bool> FullLeadingAddr("full-leading-addr",
64 cl::desc("Print full leading address"));
66 static cl::opt<bool> NoLeadingAddr("no-leading-addr",
67 cl::desc("Print no leading address"));
70 PrintImmHex("print-imm-hex",
71 cl::desc("Use hex format for immediate values"));
73 cl::opt<bool> llvm::UniversalHeaders("universal-headers",
74 cl::desc("Print Mach-O universal headers "
75 "(requires -macho)"));
78 llvm::ArchiveHeaders("archive-headers",
79 cl::desc("Print archive headers for Mach-O archives "
80 "(requires -macho)"));
83 llvm::IndirectSymbols("indirect-symbols",
84 cl::desc("Print indirect symbol table for Mach-O "
85 "objects (requires -macho)"));
88 llvm::DataInCode("data-in-code",
89 cl::desc("Print the data in code table for Mach-O objects "
90 "(requires -macho)"));
93 llvm::LinkOptHints("link-opt-hints",
94 cl::desc("Print the linker optimization hints for "
95 "Mach-O objects (requires -macho)"));
98 llvm::DumpSections("section",
99 cl::desc("Prints the specified segment,section for "
100 "Mach-O objects (requires -macho)"));
102 cl::opt<bool> llvm::Raw("raw",
103 cl::desc("Have -section dump the raw binary contents"));
106 llvm::InfoPlist("info-plist",
107 cl::desc("Print the info plist section as strings for "
108 "Mach-O objects (requires -macho)"));
111 llvm::DylibsUsed("dylibs-used",
112 cl::desc("Print the shared libraries used for linked "
113 "Mach-O files (requires -macho)"));
116 llvm::DylibId("dylib-id",
117 cl::desc("Print the shared library's id for the dylib Mach-O "
118 "file (requires -macho)"));
121 llvm::NonVerbose("non-verbose",
122 cl::desc("Print the info for Mach-O objects in "
123 "non-verbose or numeric form (requires -macho)"));
126 llvm::ObjcMetaData("objc-meta-data",
127 cl::desc("Print the Objective-C runtime meta data for "
128 "Mach-O files (requires -macho)"));
130 cl::opt<std::string> llvm::DisSymName(
132 cl::desc("disassemble just this symbol's instructions (requires -macho"));
134 static cl::opt<bool> NoSymbolicOperands(
135 "no-symbolic-operands",
136 cl::desc("do not symbolic operands when disassembling (requires -macho)"));
138 static cl::list<std::string>
139 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
141 bool ArchAll = false;
143 static std::string ThumbTripleName;
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();
157 // Get the target specific parser.
159 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
160 if (TheTarget && ThumbTripleName.empty())
163 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
167 errs() << "llvm-objdump: error: unable to get target for '";
169 errs() << TripleName;
171 errs() << ThumbTripleName;
172 errs() << "', see --version and --triple.\n";
176 struct SymbolSorter {
177 bool operator()(const SymbolRef &A, const SymbolRef &B) {
178 SymbolRef::Type AType, BType;
182 uint64_t AAddr, BAddr;
183 if (AType != SymbolRef::ST_Function)
187 if (BType != SymbolRef::ST_Function)
191 return AAddr < BAddr;
195 // Types for the storted data in code table that is built before disassembly
196 // and the predicate function to sort them.
197 typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
198 typedef std::vector<DiceTableEntry> DiceTable;
199 typedef DiceTable::iterator dice_table_iterator;
201 // This is used to search for a data in code table entry for the PC being
202 // disassembled. The j parameter has the PC in j.first. A single data in code
203 // table entry can cover many bytes for each of its Kind's. So if the offset,
204 // aka the i.first value, of the data in code table entry plus its Length
205 // covers the PC being searched for this will return true. If not it will
207 static bool compareDiceTableEntries(const DiceTableEntry &i,
208 const DiceTableEntry &j) {
210 i.second.getLength(Length);
212 return j.first >= i.first && j.first < i.first + Length;
215 static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
216 unsigned short Kind) {
217 uint32_t Value, Size = 1;
221 case MachO::DICE_KIND_DATA:
224 DumpBytes(ArrayRef<uint8_t>(bytes, 4));
225 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
226 outs() << "\t.long " << Value;
228 } else if (Length >= 2) {
230 DumpBytes(ArrayRef<uint8_t>(bytes, 2));
231 Value = bytes[1] << 8 | bytes[0];
232 outs() << "\t.short " << Value;
236 DumpBytes(ArrayRef<uint8_t>(bytes, 2));
238 outs() << "\t.byte " << Value;
241 if (Kind == MachO::DICE_KIND_DATA)
242 outs() << "\t@ KIND_DATA\n";
244 outs() << "\t@ data in code kind = " << Kind << "\n";
246 case MachO::DICE_KIND_JUMP_TABLE8:
248 DumpBytes(ArrayRef<uint8_t>(bytes, 1));
250 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
253 case MachO::DICE_KIND_JUMP_TABLE16:
255 DumpBytes(ArrayRef<uint8_t>(bytes, 2));
256 Value = bytes[1] << 8 | bytes[0];
257 outs() << "\t.short " << format("%5u", Value & 0xffff)
258 << "\t@ KIND_JUMP_TABLE16\n";
261 case MachO::DICE_KIND_JUMP_TABLE32:
262 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
264 DumpBytes(ArrayRef<uint8_t>(bytes, 4));
265 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
266 outs() << "\t.long " << Value;
267 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
268 outs() << "\t@ KIND_JUMP_TABLE32\n";
270 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
277 static void getSectionsAndSymbols(const MachO::mach_header Header,
278 MachOObjectFile *MachOObj,
279 std::vector<SectionRef> &Sections,
280 std::vector<SymbolRef> &Symbols,
281 SmallVectorImpl<uint64_t> &FoundFns,
282 uint64_t &BaseSegmentAddress) {
283 for (const SymbolRef &Symbol : MachOObj->symbols()) {
285 Symbol.getName(SymName);
286 if (!SymName.startswith("ltmp"))
287 Symbols.push_back(Symbol);
290 for (const SectionRef &Section : MachOObj->sections()) {
292 Section.getName(SectName);
293 Sections.push_back(Section);
296 MachOObjectFile::LoadCommandInfo Command =
297 MachOObj->getFirstLoadCommandInfo();
298 bool BaseSegmentAddressSet = false;
299 for (unsigned i = 0;; ++i) {
300 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
301 // We found a function starts segment, parse the addresses for later
303 MachO::linkedit_data_command LLC =
304 MachOObj->getLinkeditDataLoadCommand(Command);
306 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
307 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
308 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
309 StringRef SegName = SLC.segname;
310 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
311 BaseSegmentAddressSet = true;
312 BaseSegmentAddress = SLC.vmaddr;
316 if (i == Header.ncmds - 1)
319 Command = MachOObj->getNextLoadCommandInfo(Command);
323 static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
324 uint32_t n, uint32_t count,
325 uint32_t stride, uint64_t addr) {
326 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
327 uint32_t nindirectsyms = Dysymtab.nindirectsyms;
328 if (n > nindirectsyms)
329 outs() << " (entries start past the end of the indirect symbol "
330 "table) (reserved1 field greater than the table size)";
331 else if (n + count > nindirectsyms)
332 outs() << " (entries extends past the end of the indirect symbol "
335 uint32_t cputype = O->getHeader().cputype;
336 if (cputype & MachO::CPU_ARCH_ABI64)
337 outs() << "address index";
339 outs() << "address index";
344 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
345 if (cputype & MachO::CPU_ARCH_ABI64)
346 outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
348 outs() << format("0x%08" PRIx32, addr + j * stride) << " ";
349 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
350 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
351 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
355 if (indirect_symbol ==
356 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
357 outs() << "LOCAL ABSOLUTE\n";
360 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
361 outs() << "ABSOLUTE\n";
364 outs() << format("%5u ", indirect_symbol);
366 MachO::symtab_command Symtab = O->getSymtabLoadCommand();
367 if (indirect_symbol < Symtab.nsyms) {
368 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
369 SymbolRef Symbol = *Sym;
371 Symbol.getName(SymName);
381 static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
382 uint32_t LoadCommandCount = O->getHeader().ncmds;
383 MachOObjectFile::LoadCommandInfo Load = O->getFirstLoadCommandInfo();
384 for (unsigned I = 0;; ++I) {
385 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
386 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
387 for (unsigned J = 0; J < Seg.nsects; ++J) {
388 MachO::section_64 Sec = O->getSection64(Load, J);
389 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
390 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
391 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
392 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
393 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
394 section_type == MachO::S_SYMBOL_STUBS) {
396 if (section_type == MachO::S_SYMBOL_STUBS)
397 stride = Sec.reserved2;
401 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
402 << Sec.sectname << ") "
403 << "(size of stubs in reserved2 field is zero)\n";
406 uint32_t count = Sec.size / stride;
407 outs() << "Indirect symbols for (" << Sec.segname << ","
408 << Sec.sectname << ") " << count << " entries";
409 uint32_t n = Sec.reserved1;
410 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
413 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
414 MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
415 for (unsigned J = 0; J < Seg.nsects; ++J) {
416 MachO::section Sec = O->getSection(Load, J);
417 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
418 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
419 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
420 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
421 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
422 section_type == MachO::S_SYMBOL_STUBS) {
424 if (section_type == MachO::S_SYMBOL_STUBS)
425 stride = Sec.reserved2;
429 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
430 << Sec.sectname << ") "
431 << "(size of stubs in reserved2 field is zero)\n";
434 uint32_t count = Sec.size / stride;
435 outs() << "Indirect symbols for (" << Sec.segname << ","
436 << Sec.sectname << ") " << count << " entries";
437 uint32_t n = Sec.reserved1;
438 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
442 if (I == LoadCommandCount - 1)
445 Load = O->getNextLoadCommandInfo(Load);
449 static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
450 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
451 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
452 outs() << "Data in code table (" << nentries << " entries)\n";
453 outs() << "offset length kind\n";
454 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
457 DI->getOffset(Offset);
458 outs() << format("0x%08" PRIx32, Offset) << " ";
460 DI->getLength(Length);
461 outs() << format("%6u", Length) << " ";
466 case MachO::DICE_KIND_DATA:
469 case MachO::DICE_KIND_JUMP_TABLE8:
470 outs() << "JUMP_TABLE8";
472 case MachO::DICE_KIND_JUMP_TABLE16:
473 outs() << "JUMP_TABLE16";
475 case MachO::DICE_KIND_JUMP_TABLE32:
476 outs() << "JUMP_TABLE32";
478 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
479 outs() << "ABS_JUMP_TABLE32";
482 outs() << format("0x%04" PRIx32, Kind);
486 outs() << format("0x%04" PRIx32, Kind);
491 static void PrintLinkOptHints(MachOObjectFile *O) {
492 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
493 const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
494 uint32_t nloh = LohLC.datasize;
495 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
496 for (uint32_t i = 0; i < nloh;) {
498 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
500 outs() << " identifier " << identifier << " ";
503 switch (identifier) {
505 outs() << "AdrpAdrp\n";
508 outs() << "AdrpLdr\n";
511 outs() << "AdrpAddLdr\n";
514 outs() << "AdrpLdrGotLdr\n";
517 outs() << "AdrpAddStr\n";
520 outs() << "AdrpLdrGotStr\n";
523 outs() << "AdrpAdd\n";
526 outs() << "AdrpLdrGot\n";
529 outs() << "Unknown identifier value\n";
532 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
534 outs() << " narguments " << narguments << "\n";
538 for (uint32_t j = 0; j < narguments; j++) {
539 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
541 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
548 static void PrintDylibs(MachOObjectFile *O, bool JustId) {
549 uint32_t LoadCommandCount = O->getHeader().ncmds;
550 MachOObjectFile::LoadCommandInfo Load = O->getFirstLoadCommandInfo();
551 for (unsigned I = 0;; ++I) {
552 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
553 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
554 Load.C.cmd == MachO::LC_LOAD_DYLIB ||
555 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
556 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
557 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
558 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
559 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
560 if (dl.dylib.name < dl.cmdsize) {
561 const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
566 outs() << " (compatibility version "
567 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
568 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
569 << (dl.dylib.compatibility_version & 0xff) << ",";
570 outs() << " current version "
571 << ((dl.dylib.current_version >> 16) & 0xffff) << "."
572 << ((dl.dylib.current_version >> 8) & 0xff) << "."
573 << (dl.dylib.current_version & 0xff) << ")\n";
576 outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
577 if (Load.C.cmd == MachO::LC_ID_DYLIB)
578 outs() << "LC_ID_DYLIB ";
579 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
580 outs() << "LC_LOAD_DYLIB ";
581 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
582 outs() << "LC_LOAD_WEAK_DYLIB ";
583 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
584 outs() << "LC_LAZY_LOAD_DYLIB ";
585 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
586 outs() << "LC_REEXPORT_DYLIB ";
587 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
588 outs() << "LC_LOAD_UPWARD_DYLIB ";
591 outs() << "command " << I << "\n";
594 if (I == LoadCommandCount - 1)
597 Load = O->getNextLoadCommandInfo(Load);
601 typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
603 static void CreateSymbolAddressMap(MachOObjectFile *O,
604 SymbolAddressMap *AddrMap) {
605 // Create a map of symbol addresses to symbol names.
606 for (const SymbolRef &Symbol : O->symbols()) {
609 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
610 ST == SymbolRef::ST_Other) {
612 Symbol.getAddress(Address);
614 Symbol.getName(SymName);
615 if (!SymName.startswith(".objc"))
616 (*AddrMap)[Address] = SymName;
621 // GuessSymbolName is passed the address of what might be a symbol and a
622 // pointer to the SymbolAddressMap. It returns the name of a symbol
623 // with that address or nullptr if no symbol is found with that address.
624 static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
625 const char *SymbolName = nullptr;
626 // A DenseMap can't lookup up some values.
627 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
628 StringRef name = AddrMap->lookup(value);
630 SymbolName = name.data();
635 static void DumpCstringChar(const char c) {
639 outs().write_escaped(p);
642 static void DumpCstringSection(MachOObjectFile *O, const char *sect,
643 uint32_t sect_size, uint64_t sect_addr,
644 bool print_addresses) {
645 for (uint32_t i = 0; i < sect_size; i++) {
646 if (print_addresses) {
648 outs() << format("%016" PRIx64, sect_addr + i) << " ";
650 outs() << format("%08" PRIx64, sect_addr + i) << " ";
652 for (; i < sect_size && sect[i] != '\0'; i++)
653 DumpCstringChar(sect[i]);
654 if (i < sect_size && sect[i] == '\0')
659 static void DumpLiteral4(uint32_t l, float f) {
660 outs() << format("0x%08" PRIx32, l);
661 if ((l & 0x7f800000) != 0x7f800000)
662 outs() << format(" (%.16e)\n", f);
665 outs() << " (+Infinity)\n";
666 else if (l == 0xff800000)
667 outs() << " (-Infinity)\n";
668 else if ((l & 0x00400000) == 0x00400000)
669 outs() << " (non-signaling Not-a-Number)\n";
671 outs() << " (signaling Not-a-Number)\n";
675 static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
676 uint32_t sect_size, uint64_t sect_addr,
677 bool print_addresses) {
678 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
679 if (print_addresses) {
681 outs() << format("%016" PRIx64, sect_addr + i) << " ";
683 outs() << format("%08" PRIx64, sect_addr + i) << " ";
686 memcpy(&f, sect + i, sizeof(float));
687 if (O->isLittleEndian() != sys::IsLittleEndianHost)
688 sys::swapByteOrder(f);
690 memcpy(&l, sect + i, sizeof(uint32_t));
691 if (O->isLittleEndian() != sys::IsLittleEndianHost)
692 sys::swapByteOrder(l);
697 static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
699 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
701 if (O->isLittleEndian()) {
708 // Hi is the high word, so this is equivalent to if(isfinite(d))
709 if ((Hi & 0x7ff00000) != 0x7ff00000)
710 outs() << format(" (%.16e)\n", d);
712 if (Hi == 0x7ff00000 && Lo == 0)
713 outs() << " (+Infinity)\n";
714 else if (Hi == 0xfff00000 && Lo == 0)
715 outs() << " (-Infinity)\n";
716 else if ((Hi & 0x00080000) == 0x00080000)
717 outs() << " (non-signaling Not-a-Number)\n";
719 outs() << " (signaling Not-a-Number)\n";
723 static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
724 uint32_t sect_size, uint64_t sect_addr,
725 bool print_addresses) {
726 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
727 if (print_addresses) {
729 outs() << format("%016" PRIx64, sect_addr + i) << " ";
731 outs() << format("%08" PRIx64, sect_addr + i) << " ";
734 memcpy(&d, sect + i, sizeof(double));
735 if (O->isLittleEndian() != sys::IsLittleEndianHost)
736 sys::swapByteOrder(d);
738 memcpy(&l0, sect + i, sizeof(uint32_t));
739 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
740 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
741 sys::swapByteOrder(l0);
742 sys::swapByteOrder(l1);
744 DumpLiteral8(O, l0, l1, d);
748 static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
749 outs() << format("0x%08" PRIx32, l0) << " ";
750 outs() << format("0x%08" PRIx32, l1) << " ";
751 outs() << format("0x%08" PRIx32, l2) << " ";
752 outs() << format("0x%08" PRIx32, l3) << "\n";
755 static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
756 uint32_t sect_size, uint64_t sect_addr,
757 bool print_addresses) {
758 for (uint32_t i = 0; i < sect_size; i += 16) {
759 if (print_addresses) {
761 outs() << format("%016" PRIx64, sect_addr + i) << " ";
763 outs() << format("%08" PRIx64, sect_addr + i) << " ";
765 uint32_t l0, l1, l2, l3;
766 memcpy(&l0, sect + i, sizeof(uint32_t));
767 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
768 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
769 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
770 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
771 sys::swapByteOrder(l0);
772 sys::swapByteOrder(l1);
773 sys::swapByteOrder(l2);
774 sys::swapByteOrder(l3);
776 DumpLiteral16(l0, l1, l2, l3);
780 static void DumpLiteralPointerSection(MachOObjectFile *O,
781 const SectionRef &Section,
782 const char *sect, uint32_t sect_size,
784 bool print_addresses) {
785 // Collect the literal sections in this Mach-O file.
786 std::vector<SectionRef> LiteralSections;
787 for (const SectionRef &Section : O->sections()) {
788 DataRefImpl Ref = Section.getRawDataRefImpl();
789 uint32_t section_type;
791 const MachO::section_64 Sec = O->getSection64(Ref);
792 section_type = Sec.flags & MachO::SECTION_TYPE;
794 const MachO::section Sec = O->getSection(Ref);
795 section_type = Sec.flags & MachO::SECTION_TYPE;
797 if (section_type == MachO::S_CSTRING_LITERALS ||
798 section_type == MachO::S_4BYTE_LITERALS ||
799 section_type == MachO::S_8BYTE_LITERALS ||
800 section_type == MachO::S_16BYTE_LITERALS)
801 LiteralSections.push_back(Section);
804 // Set the size of the literal pointer.
805 uint32_t lp_size = O->is64Bit() ? 8 : 4;
807 // Collect the external relocation symbols for the the literal pointers.
808 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
809 for (const RelocationRef &Reloc : Section.relocations()) {
811 MachO::any_relocation_info RE;
812 bool isExtern = false;
813 Rel = Reloc.getRawDataRefImpl();
814 RE = O->getRelocation(Rel);
815 isExtern = O->getPlainRelocationExternal(RE);
817 uint64_t RelocOffset;
818 Reloc.getOffset(RelocOffset);
819 symbol_iterator RelocSym = Reloc.getSymbol();
820 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
823 array_pod_sort(Relocs.begin(), Relocs.end());
825 // Dump each literal pointer.
826 for (uint32_t i = 0; i < sect_size; i += lp_size) {
827 if (print_addresses) {
829 outs() << format("%016" PRIx64, sect_addr + i) << " ";
831 outs() << format("%08" PRIx64, sect_addr + i) << " ";
835 memcpy(&lp, sect + i, sizeof(uint64_t));
836 if (O->isLittleEndian() != sys::IsLittleEndianHost)
837 sys::swapByteOrder(lp);
840 memcpy(&li, sect + i, sizeof(uint32_t));
841 if (O->isLittleEndian() != sys::IsLittleEndianHost)
842 sys::swapByteOrder(li);
846 // First look for an external relocation entry for this literal pointer.
847 auto Reloc = std::find_if(
848 Relocs.begin(), Relocs.end(),
849 [&](const std::pair<uint64_t, SymbolRef> &P) { return P.first == i; });
850 if (Reloc != Relocs.end()) {
851 symbol_iterator RelocSym = Reloc->second;
853 RelocSym->getName(SymName);
854 outs() << "external relocation entry for symbol:" << SymName << "\n";
858 // For local references see what the section the literal pointer points to.
859 auto Sect = std::find_if(LiteralSections.begin(), LiteralSections.end(),
860 [&](const SectionRef &R) {
861 return lp >= R.getAddress() &&
862 lp < R.getAddress() + R.getSize();
864 if (Sect == LiteralSections.end()) {
865 outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
869 uint64_t SectAddress = Sect->getAddress();
870 uint64_t SectSize = Sect->getSize();
873 Sect->getName(SectName);
874 DataRefImpl Ref = Sect->getRawDataRefImpl();
875 StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
876 outs() << SegmentName << ":" << SectName << ":";
878 uint32_t section_type;
880 const MachO::section_64 Sec = O->getSection64(Ref);
881 section_type = Sec.flags & MachO::SECTION_TYPE;
883 const MachO::section Sec = O->getSection(Ref);
884 section_type = Sec.flags & MachO::SECTION_TYPE;
888 Sect->getContents(BytesStr);
889 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
891 switch (section_type) {
892 case MachO::S_CSTRING_LITERALS:
893 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
895 DumpCstringChar(Contents[i]);
899 case MachO::S_4BYTE_LITERALS:
901 memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
903 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
904 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
905 sys::swapByteOrder(f);
906 sys::swapByteOrder(l);
910 case MachO::S_8BYTE_LITERALS: {
912 memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
914 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
915 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
917 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
918 sys::swapByteOrder(f);
919 sys::swapByteOrder(l0);
920 sys::swapByteOrder(l1);
922 DumpLiteral8(O, l0, l1, d);
925 case MachO::S_16BYTE_LITERALS: {
926 uint32_t l0, l1, l2, l3;
927 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
928 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
930 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
932 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
934 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
935 sys::swapByteOrder(l0);
936 sys::swapByteOrder(l1);
937 sys::swapByteOrder(l2);
938 sys::swapByteOrder(l3);
940 DumpLiteral16(l0, l1, l2, l3);
947 static void DumpInitTermPointerSection(MachOObjectFile *O, const char *sect,
948 uint32_t sect_size, uint64_t sect_addr,
949 SymbolAddressMap *AddrMap,
953 stride = sizeof(uint64_t);
955 stride = sizeof(uint32_t);
956 for (uint32_t i = 0; i < sect_size; i += stride) {
957 const char *SymbolName = nullptr;
959 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
960 uint64_t pointer_value;
961 memcpy(&pointer_value, sect + i, stride);
962 if (O->isLittleEndian() != sys::IsLittleEndianHost)
963 sys::swapByteOrder(pointer_value);
964 outs() << format("0x%016" PRIx64, pointer_value);
966 SymbolName = GuessSymbolName(pointer_value, AddrMap);
968 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
969 uint32_t pointer_value;
970 memcpy(&pointer_value, sect + i, stride);
971 if (O->isLittleEndian() != sys::IsLittleEndianHost)
972 sys::swapByteOrder(pointer_value);
973 outs() << format("0x%08" PRIx32, pointer_value);
975 SymbolName = GuessSymbolName(pointer_value, AddrMap);
978 outs() << " " << SymbolName;
983 static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
984 uint32_t size, uint64_t addr) {
985 uint32_t cputype = O->getHeader().cputype;
986 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
988 for (uint32_t i = 0; i < size; i += j, addr += j) {
990 outs() << format("%016" PRIx64, addr) << "\t";
992 outs() << format("%08" PRIx64, addr) << "\t";
993 for (j = 0; j < 16 && i + j < size; j++) {
994 uint8_t byte_word = *(sect + i + j);
995 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
1001 for (uint32_t i = 0; i < size; i += j, addr += j) {
1003 outs() << format("%016" PRIx64, addr) << "\t";
1005 outs() << format("%08" PRIx64, sect) << "\t";
1006 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
1007 j += sizeof(int32_t)) {
1008 if (i + j + sizeof(int32_t) < size) {
1010 memcpy(&long_word, sect + i + j, sizeof(int32_t));
1011 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1012 sys::swapByteOrder(long_word);
1013 outs() << format("%08" PRIx32, long_word) << " ";
1015 for (uint32_t k = 0; i + j + k < size; k++) {
1016 uint8_t byte_word = *(sect + i + j);
1017 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
1026 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
1027 StringRef DisSegName, StringRef DisSectName);
1029 static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
1031 SymbolAddressMap AddrMap;
1033 CreateSymbolAddressMap(O, &AddrMap);
1035 for (unsigned i = 0; i < DumpSections.size(); ++i) {
1036 StringRef DumpSection = DumpSections[i];
1037 std::pair<StringRef, StringRef> DumpSegSectName;
1038 DumpSegSectName = DumpSection.split(',');
1039 StringRef DumpSegName, DumpSectName;
1040 if (DumpSegSectName.second.size()) {
1041 DumpSegName = DumpSegSectName.first;
1042 DumpSectName = DumpSegSectName.second;
1045 DumpSectName = DumpSegSectName.first;
1047 for (const SectionRef &Section : O->sections()) {
1049 Section.getName(SectName);
1050 DataRefImpl Ref = Section.getRawDataRefImpl();
1051 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1052 if ((DumpSegName.empty() || SegName == DumpSegName) &&
1053 (SectName == DumpSectName)) {
1055 uint32_t section_flags;
1057 const MachO::section_64 Sec = O->getSection64(Ref);
1058 section_flags = Sec.flags;
1061 const MachO::section Sec = O->getSection(Ref);
1062 section_flags = Sec.flags;
1064 uint32_t section_type = section_flags & MachO::SECTION_TYPE;
1067 Section.getContents(BytesStr);
1068 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1069 uint32_t sect_size = BytesStr.size();
1070 uint64_t sect_addr = Section.getAddress();
1073 outs().write(BytesStr.data(), BytesStr.size());
1077 outs() << "Contents of (" << SegName << "," << SectName
1081 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1082 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1083 DisassembleMachO(Filename, O, SegName, SectName);
1086 if (SegName == "__TEXT" && SectName == "__info_plist") {
1090 switch (section_type) {
1091 case MachO::S_REGULAR:
1092 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1094 case MachO::S_ZEROFILL:
1095 outs() << "zerofill section and has no contents in the file\n";
1097 case MachO::S_CSTRING_LITERALS:
1098 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1100 case MachO::S_4BYTE_LITERALS:
1101 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1103 case MachO::S_8BYTE_LITERALS:
1104 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1106 case MachO::S_16BYTE_LITERALS:
1107 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1109 case MachO::S_LITERAL_POINTERS:
1110 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
1113 case MachO::S_MOD_INIT_FUNC_POINTERS:
1114 case MachO::S_MOD_TERM_FUNC_POINTERS:
1115 DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap,
1119 outs() << "Unknown section type ("
1120 << format("0x%08" PRIx32, section_type) << ")\n";
1121 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1125 if (section_type == MachO::S_ZEROFILL)
1126 outs() << "zerofill section and has no contents in the file\n";
1128 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1135 static void DumpInfoPlistSectionContents(StringRef Filename,
1136 MachOObjectFile *O) {
1137 for (const SectionRef &Section : O->sections()) {
1139 Section.getName(SectName);
1140 DataRefImpl Ref = Section.getRawDataRefImpl();
1141 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1142 if (SegName == "__TEXT" && SectName == "__info_plist") {
1143 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
1145 Section.getContents(BytesStr);
1146 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1153 // checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1154 // and if it is and there is a list of architecture flags is specified then
1155 // check to make sure this Mach-O file is one of those architectures or all
1156 // architectures were specified. If not then an error is generated and this
1157 // routine returns false. Else it returns true.
1158 static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
1159 if (isa<MachOObjectFile>(O) && !ArchAll && ArchFlags.size() != 0) {
1160 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O);
1161 bool ArchFound = false;
1162 MachO::mach_header H;
1163 MachO::mach_header_64 H_64;
1165 if (MachO->is64Bit()) {
1166 H_64 = MachO->MachOObjectFile::getHeader64();
1167 T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype);
1169 H = MachO->MachOObjectFile::getHeader();
1170 T = MachOObjectFile::getArch(H.cputype, H.cpusubtype);
1173 for (i = 0; i < ArchFlags.size(); ++i) {
1174 if (ArchFlags[i] == T.getArchName())
1179 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1180 << "architecture: " + ArchFlags[i] + "\n";
1187 static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1189 // ProcessMachO() is passed a single opened Mach-O file, which may be an
1190 // archive member and or in a slice of a universal file. It prints the
1191 // the file name and header info and then processes it according to the
1192 // command line options.
1193 static void ProcessMachO(StringRef Filename, MachOObjectFile *MachOOF,
1194 StringRef ArchiveMemberName = StringRef(),
1195 StringRef ArchitectureName = StringRef()) {
1196 // If we are doing some processing here on the Mach-O file print the header
1197 // info. And don't print it otherwise like in the case of printing the
1198 // UniversalHeaders or ArchiveHeaders.
1199 if (Disassemble || PrivateHeaders || ExportsTrie || Rebase || Bind ||
1200 LazyBind || WeakBind || IndirectSymbols || DataInCode || LinkOptHints ||
1201 DylibsUsed || DylibId || ObjcMetaData ||
1202 (DumpSections.size() != 0 && !Raw)) {
1204 if (!ArchiveMemberName.empty())
1205 outs() << '(' << ArchiveMemberName << ')';
1206 if (!ArchitectureName.empty())
1207 outs() << " (architecture " << ArchitectureName << ")";
1212 DisassembleMachO(Filename, MachOOF, "__TEXT", "__text");
1213 if (IndirectSymbols)
1214 PrintIndirectSymbols(MachOOF, !NonVerbose);
1216 PrintDataInCodeTable(MachOOF, !NonVerbose);
1218 PrintLinkOptHints(MachOOF);
1220 PrintRelocations(MachOOF);
1222 PrintSectionHeaders(MachOOF);
1223 if (SectionContents)
1224 PrintSectionContents(MachOOF);
1225 if (DumpSections.size() != 0)
1226 DumpSectionContents(Filename, MachOOF, !NonVerbose);
1228 DumpInfoPlistSectionContents(Filename, MachOOF);
1230 PrintDylibs(MachOOF, false);
1232 PrintDylibs(MachOOF, true);
1234 PrintSymbolTable(MachOOF);
1236 printMachOUnwindInfo(MachOOF);
1238 printMachOFileHeader(MachOOF);
1240 printObjcMetaData(MachOOF, !NonVerbose);
1242 printExportsTrie(MachOOF);
1244 printRebaseTable(MachOOF);
1246 printBindTable(MachOOF);
1248 printLazyBindTable(MachOOF);
1250 printWeakBindTable(MachOOF);
1253 // printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1254 static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
1255 outs() << " cputype (" << cputype << ")\n";
1256 outs() << " cpusubtype (" << cpusubtype << ")\n";
1259 // printCPUType() helps print_fat_headers by printing the cputype and
1260 // pusubtype (symbolically for the one's it knows about).
1261 static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
1263 case MachO::CPU_TYPE_I386:
1264 switch (cpusubtype) {
1265 case MachO::CPU_SUBTYPE_I386_ALL:
1266 outs() << " cputype CPU_TYPE_I386\n";
1267 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
1270 printUnknownCPUType(cputype, cpusubtype);
1274 case MachO::CPU_TYPE_X86_64:
1275 switch (cpusubtype) {
1276 case MachO::CPU_SUBTYPE_X86_64_ALL:
1277 outs() << " cputype CPU_TYPE_X86_64\n";
1278 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1280 case MachO::CPU_SUBTYPE_X86_64_H:
1281 outs() << " cputype CPU_TYPE_X86_64\n";
1282 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
1285 printUnknownCPUType(cputype, cpusubtype);
1289 case MachO::CPU_TYPE_ARM:
1290 switch (cpusubtype) {
1291 case MachO::CPU_SUBTYPE_ARM_ALL:
1292 outs() << " cputype CPU_TYPE_ARM\n";
1293 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
1295 case MachO::CPU_SUBTYPE_ARM_V4T:
1296 outs() << " cputype CPU_TYPE_ARM\n";
1297 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
1299 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1300 outs() << " cputype CPU_TYPE_ARM\n";
1301 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
1303 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1304 outs() << " cputype CPU_TYPE_ARM\n";
1305 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
1307 case MachO::CPU_SUBTYPE_ARM_V6:
1308 outs() << " cputype CPU_TYPE_ARM\n";
1309 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
1311 case MachO::CPU_SUBTYPE_ARM_V6M:
1312 outs() << " cputype CPU_TYPE_ARM\n";
1313 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
1315 case MachO::CPU_SUBTYPE_ARM_V7:
1316 outs() << " cputype CPU_TYPE_ARM\n";
1317 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
1319 case MachO::CPU_SUBTYPE_ARM_V7EM:
1320 outs() << " cputype CPU_TYPE_ARM\n";
1321 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
1323 case MachO::CPU_SUBTYPE_ARM_V7K:
1324 outs() << " cputype CPU_TYPE_ARM\n";
1325 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
1327 case MachO::CPU_SUBTYPE_ARM_V7M:
1328 outs() << " cputype CPU_TYPE_ARM\n";
1329 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
1331 case MachO::CPU_SUBTYPE_ARM_V7S:
1332 outs() << " cputype CPU_TYPE_ARM\n";
1333 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
1336 printUnknownCPUType(cputype, cpusubtype);
1340 case MachO::CPU_TYPE_ARM64:
1341 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1342 case MachO::CPU_SUBTYPE_ARM64_ALL:
1343 outs() << " cputype CPU_TYPE_ARM64\n";
1344 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
1347 printUnknownCPUType(cputype, cpusubtype);
1352 printUnknownCPUType(cputype, cpusubtype);
1357 static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
1359 outs() << "Fat headers\n";
1361 outs() << "fat_magic FAT_MAGIC\n";
1363 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
1365 uint32_t nfat_arch = UB->getNumberOfObjects();
1366 StringRef Buf = UB->getData();
1367 uint64_t size = Buf.size();
1368 uint64_t big_size = sizeof(struct MachO::fat_header) +
1369 nfat_arch * sizeof(struct MachO::fat_arch);
1370 outs() << "nfat_arch " << UB->getNumberOfObjects();
1372 outs() << " (malformed, contains zero architecture types)\n";
1373 else if (big_size > size)
1374 outs() << " (malformed, architectures past end of file)\n";
1378 for (uint32_t i = 0; i < nfat_arch; ++i) {
1379 MachOUniversalBinary::ObjectForArch OFA(UB, i);
1380 uint32_t cputype = OFA.getCPUType();
1381 uint32_t cpusubtype = OFA.getCPUSubType();
1382 outs() << "architecture ";
1383 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
1384 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
1385 uint32_t other_cputype = other_OFA.getCPUType();
1386 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
1387 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
1388 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
1389 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
1390 outs() << "(illegal duplicate architecture) ";
1395 outs() << OFA.getArchTypeName() << "\n";
1396 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1398 outs() << i << "\n";
1399 outs() << " cputype " << cputype << "\n";
1400 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
1404 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
1405 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
1407 outs() << " capabilities "
1408 << format("0x%" PRIx32,
1409 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
1410 outs() << " offset " << OFA.getOffset();
1411 if (OFA.getOffset() > size)
1412 outs() << " (past end of file)";
1413 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
1414 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
1416 outs() << " size " << OFA.getSize();
1417 big_size = OFA.getOffset() + OFA.getSize();
1418 if (big_size > size)
1419 outs() << " (past end of file)";
1421 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
1426 static void printArchiveChild(Archive::Child &C, bool verbose,
1427 bool print_offset) {
1429 outs() << C.getChildOffset() << "\t";
1430 sys::fs::perms Mode = C.getAccessMode();
1432 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
1433 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
1435 if (Mode & sys::fs::owner_read)
1439 if (Mode & sys::fs::owner_write)
1443 if (Mode & sys::fs::owner_exe)
1447 if (Mode & sys::fs::group_read)
1451 if (Mode & sys::fs::group_write)
1455 if (Mode & sys::fs::group_exe)
1459 if (Mode & sys::fs::others_read)
1463 if (Mode & sys::fs::others_write)
1467 if (Mode & sys::fs::others_exe)
1472 outs() << format("0%o ", Mode);
1475 unsigned UID = C.getUID();
1476 outs() << format("%3d/", UID);
1477 unsigned GID = C.getGID();
1478 outs() << format("%-3d ", GID);
1479 uint64_t Size = C.getRawSize();
1480 outs() << format("%5" PRId64, Size) << " ";
1482 StringRef RawLastModified = C.getRawLastModified();
1485 if (RawLastModified.getAsInteger(10, Seconds))
1486 outs() << "(date: \"%s\" contains non-decimal chars) " << RawLastModified;
1488 // Since cime(3) returns a 26 character string of the form:
1489 // "Sun Sep 16 01:03:52 1973\n\0"
1490 // just print 24 characters.
1492 outs() << format("%.24s ", ctime(&t));
1495 outs() << RawLastModified << " ";
1499 ErrorOr<StringRef> NameOrErr = C.getName();
1500 if (NameOrErr.getError()) {
1501 StringRef RawName = C.getRawName();
1502 outs() << RawName << "\n";
1504 StringRef Name = NameOrErr.get();
1505 outs() << Name << "\n";
1508 StringRef RawName = C.getRawName();
1509 outs() << RawName << "\n";
1513 static void printArchiveHeaders(Archive *A, bool verbose, bool print_offset) {
1514 if (A->hasSymbolTable()) {
1515 Archive::child_iterator S = A->getSymbolTableChild();
1516 Archive::Child C = *S;
1517 printArchiveChild(C, verbose, print_offset);
1519 for (Archive::child_iterator I = A->child_begin(), E = A->child_end(); I != E;
1521 Archive::Child C = *I;
1522 printArchiveChild(C, verbose, print_offset);
1526 // ParseInputMachO() parses the named Mach-O file in Filename and handles the
1527 // -arch flags selecting just those slices as specified by them and also parses
1528 // archive files. Then for each individual Mach-O file ProcessMachO() is
1529 // called to process the file based on the command line options.
1530 void llvm::ParseInputMachO(StringRef Filename) {
1531 // Check for -arch all and verifiy the -arch flags are valid.
1532 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1533 if (ArchFlags[i] == "all") {
1536 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
1537 errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
1538 "'for the -arch option\n";
1544 // Attempt to open the binary.
1545 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
1546 if (std::error_code EC = BinaryOrErr.getError()) {
1547 errs() << "llvm-objdump: '" << Filename << "': " << EC.message() << ".\n";
1550 Binary &Bin = *BinaryOrErr.get().getBinary();
1552 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1553 outs() << "Archive : " << Filename << "\n";
1555 printArchiveHeaders(A, true, false);
1556 for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
1558 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = I->getAsBinary();
1559 if (ChildOrErr.getError())
1561 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1562 if (!checkMachOAndArchFlags(O, Filename))
1564 ProcessMachO(Filename, O, O->getFileName());
1569 if (UniversalHeaders) {
1570 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
1571 printMachOUniversalHeaders(UB, !NonVerbose);
1573 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1574 // If we have a list of architecture flags specified dump only those.
1575 if (!ArchAll && ArchFlags.size() != 0) {
1576 // Look for a slice in the universal binary that matches each ArchFlag.
1578 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1580 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1581 E = UB->end_objects();
1583 if (ArchFlags[i] == I->getArchTypeName()) {
1585 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
1586 I->getAsObjectFile();
1587 std::string ArchitectureName = "";
1588 if (ArchFlags.size() > 1)
1589 ArchitectureName = I->getArchTypeName();
1591 ObjectFile &O = *ObjOrErr.get();
1592 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
1593 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
1594 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1595 I->getAsArchive()) {
1596 std::unique_ptr<Archive> &A = *AOrErr;
1597 outs() << "Archive : " << Filename;
1598 if (!ArchitectureName.empty())
1599 outs() << " (architecture " << ArchitectureName << ")";
1602 printArchiveHeaders(A.get(), true, false);
1603 for (Archive::child_iterator AI = A->child_begin(),
1604 AE = A->child_end();
1606 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1607 if (ChildOrErr.getError())
1609 if (MachOObjectFile *O =
1610 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
1611 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
1617 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1618 << "architecture: " + ArchFlags[i] + "\n";
1624 // No architecture flags were specified so if this contains a slice that
1625 // matches the host architecture dump only that.
1627 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1628 E = UB->end_objects();
1630 if (MachOObjectFile::getHostArch().getArchName() ==
1631 I->getArchTypeName()) {
1632 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1633 std::string ArchiveName;
1634 ArchiveName.clear();
1636 ObjectFile &O = *ObjOrErr.get();
1637 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
1638 ProcessMachO(Filename, MachOOF);
1639 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1640 I->getAsArchive()) {
1641 std::unique_ptr<Archive> &A = *AOrErr;
1642 outs() << "Archive : " << Filename << "\n";
1644 printArchiveHeaders(A.get(), true, false);
1645 for (Archive::child_iterator AI = A->child_begin(),
1646 AE = A->child_end();
1648 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1649 if (ChildOrErr.getError())
1651 if (MachOObjectFile *O =
1652 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
1653 ProcessMachO(Filename, O, O->getFileName());
1660 // Either all architectures have been specified or none have been specified
1661 // and this does not contain the host architecture so dump all the slices.
1662 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1663 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1664 E = UB->end_objects();
1666 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1667 std::string ArchitectureName = "";
1668 if (moreThanOneArch)
1669 ArchitectureName = I->getArchTypeName();
1671 ObjectFile &Obj = *ObjOrErr.get();
1672 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
1673 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
1674 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
1675 std::unique_ptr<Archive> &A = *AOrErr;
1676 outs() << "Archive : " << Filename;
1677 if (!ArchitectureName.empty())
1678 outs() << " (architecture " << ArchitectureName << ")";
1681 printArchiveHeaders(A.get(), true, false);
1682 for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
1684 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1685 if (ChildOrErr.getError())
1687 if (MachOObjectFile *O =
1688 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1689 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
1690 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
1698 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
1699 if (!checkMachOAndArchFlags(O, Filename))
1701 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
1702 ProcessMachO(Filename, MachOOF);
1704 errs() << "llvm-objdump: '" << Filename << "': "
1705 << "Object is not a Mach-O file type.\n";
1707 errs() << "llvm-objdump: '" << Filename << "': "
1708 << "Unrecognized file type.\n";
1711 typedef std::pair<uint64_t, const char *> BindInfoEntry;
1712 typedef std::vector<BindInfoEntry> BindTable;
1713 typedef BindTable::iterator bind_table_iterator;
1715 // The block of info used by the Symbolizer call backs.
1716 struct DisassembleInfo {
1720 SymbolAddressMap *AddrMap;
1721 std::vector<SectionRef> *Sections;
1722 const char *class_name;
1723 const char *selector_name;
1725 char *demangled_name;
1728 BindTable *bindtable;
1731 // SymbolizerGetOpInfo() is the operand information call back function.
1732 // This is called to get the symbolic information for operand(s) of an
1733 // instruction when it is being done. This routine does this from
1734 // the relocation information, symbol table, etc. That block of information
1735 // is a pointer to the struct DisassembleInfo that was passed when the
1736 // disassembler context was created and passed to back to here when
1737 // called back by the disassembler for instruction operands that could have
1738 // relocation information. The address of the instruction containing operand is
1739 // at the Pc parameter. The immediate value the operand has is passed in
1740 // op_info->Value and is at Offset past the start of the instruction and has a
1741 // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
1742 // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
1743 // names and addends of the symbolic expression to add for the operand. The
1744 // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
1745 // information is returned then this function returns 1 else it returns 0.
1746 static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
1747 uint64_t Size, int TagType, void *TagBuf) {
1748 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
1749 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
1750 uint64_t value = op_info->Value;
1752 // Make sure all fields returned are zero if we don't set them.
1753 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
1754 op_info->Value = value;
1756 // If the TagType is not the value 1 which it code knows about or if no
1757 // verbose symbolic information is wanted then just return 0, indicating no
1758 // information is being returned.
1759 if (TagType != 1 || !info->verbose)
1762 unsigned int Arch = info->O->getArch();
1763 if (Arch == Triple::x86) {
1764 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1766 // First search the section's relocation entries (if any) for an entry
1767 // for this section offset.
1768 uint32_t sect_addr = info->S.getAddress();
1769 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1770 bool reloc_found = false;
1772 MachO::any_relocation_info RE;
1773 bool isExtern = false;
1775 bool r_scattered = false;
1776 uint32_t r_value, pair_r_value, r_type;
1777 for (const RelocationRef &Reloc : info->S.relocations()) {
1778 uint64_t RelocOffset;
1779 Reloc.getOffset(RelocOffset);
1780 if (RelocOffset == sect_offset) {
1781 Rel = Reloc.getRawDataRefImpl();
1782 RE = info->O->getRelocation(Rel);
1783 r_type = info->O->getAnyRelocationType(RE);
1784 r_scattered = info->O->isRelocationScattered(RE);
1786 r_value = info->O->getScatteredRelocationValue(RE);
1787 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1788 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
1789 DataRefImpl RelNext = Rel;
1790 info->O->moveRelocationNext(RelNext);
1791 MachO::any_relocation_info RENext;
1792 RENext = info->O->getRelocation(RelNext);
1793 if (info->O->isRelocationScattered(RENext))
1794 pair_r_value = info->O->getScatteredRelocationValue(RENext);
1799 isExtern = info->O->getPlainRelocationExternal(RE);
1801 symbol_iterator RelocSym = Reloc.getSymbol();
1809 if (reloc_found && isExtern) {
1811 Symbol.getName(SymName);
1812 const char *name = SymName.data();
1813 op_info->AddSymbol.Present = 1;
1814 op_info->AddSymbol.Name = name;
1815 // For i386 extern relocation entries the value in the instruction is
1816 // the offset from the symbol, and value is already set in op_info->Value.
1819 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1820 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
1821 const char *add = GuessSymbolName(r_value, info->AddrMap);
1822 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
1823 uint32_t offset = value - (r_value - pair_r_value);
1824 op_info->AddSymbol.Present = 1;
1826 op_info->AddSymbol.Name = add;
1828 op_info->AddSymbol.Value = r_value;
1829 op_info->SubtractSymbol.Present = 1;
1831 op_info->SubtractSymbol.Name = sub;
1833 op_info->SubtractSymbol.Value = pair_r_value;
1834 op_info->Value = offset;
1838 // Second search the external relocation entries of a fully linked image
1839 // (if any) for an entry that matches this segment offset.
1840 // uint32_t seg_offset = (Pc + Offset);
1843 if (Arch == Triple::x86_64) {
1844 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1846 // First search the section's relocation entries (if any) for an entry
1847 // for this section offset.
1848 uint64_t sect_addr = info->S.getAddress();
1849 uint64_t sect_offset = (Pc + Offset) - sect_addr;
1850 bool reloc_found = false;
1852 MachO::any_relocation_info RE;
1853 bool isExtern = false;
1855 for (const RelocationRef &Reloc : info->S.relocations()) {
1856 uint64_t RelocOffset;
1857 Reloc.getOffset(RelocOffset);
1858 if (RelocOffset == sect_offset) {
1859 Rel = Reloc.getRawDataRefImpl();
1860 RE = info->O->getRelocation(Rel);
1861 // NOTE: Scattered relocations don't exist on x86_64.
1862 isExtern = info->O->getPlainRelocationExternal(RE);
1864 symbol_iterator RelocSym = Reloc.getSymbol();
1871 if (reloc_found && isExtern) {
1872 // The Value passed in will be adjusted by the Pc if the instruction
1873 // adds the Pc. But for x86_64 external relocation entries the Value
1874 // is the offset from the external symbol.
1875 if (info->O->getAnyRelocationPCRel(RE))
1876 op_info->Value -= Pc + Offset + Size;
1878 Symbol.getName(SymName);
1879 const char *name = SymName.data();
1880 unsigned Type = info->O->getAnyRelocationType(RE);
1881 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
1882 DataRefImpl RelNext = Rel;
1883 info->O->moveRelocationNext(RelNext);
1884 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
1885 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
1886 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
1887 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
1888 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
1889 op_info->SubtractSymbol.Present = 1;
1890 op_info->SubtractSymbol.Name = name;
1891 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
1892 Symbol = *RelocSymNext;
1893 StringRef SymNameNext;
1894 Symbol.getName(SymNameNext);
1895 name = SymNameNext.data();
1898 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
1899 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
1900 op_info->AddSymbol.Present = 1;
1901 op_info->AddSymbol.Name = name;
1905 // Second search the external relocation entries of a fully linked image
1906 // (if any) for an entry that matches this segment offset.
1907 // uint64_t seg_offset = (Pc + Offset);
1910 if (Arch == Triple::arm) {
1911 if (Offset != 0 || (Size != 4 && Size != 2))
1913 // First search the section's relocation entries (if any) for an entry
1914 // for this section offset.
1915 uint32_t sect_addr = info->S.getAddress();
1916 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1918 MachO::any_relocation_info RE;
1919 bool isExtern = false;
1921 bool r_scattered = false;
1922 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
1924 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
1925 [&](const RelocationRef &Reloc) {
1926 uint64_t RelocOffset;
1927 Reloc.getOffset(RelocOffset);
1928 return RelocOffset == sect_offset;
1931 if (Reloc == info->S.relocations().end())
1934 Rel = Reloc->getRawDataRefImpl();
1935 RE = info->O->getRelocation(Rel);
1936 r_length = info->O->getAnyRelocationLength(RE);
1937 r_scattered = info->O->isRelocationScattered(RE);
1939 r_value = info->O->getScatteredRelocationValue(RE);
1940 r_type = info->O->getScatteredRelocationType(RE);
1942 r_type = info->O->getAnyRelocationType(RE);
1943 isExtern = info->O->getPlainRelocationExternal(RE);
1945 symbol_iterator RelocSym = Reloc->getSymbol();
1949 if (r_type == MachO::ARM_RELOC_HALF ||
1950 r_type == MachO::ARM_RELOC_SECTDIFF ||
1951 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
1952 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1953 DataRefImpl RelNext = Rel;
1954 info->O->moveRelocationNext(RelNext);
1955 MachO::any_relocation_info RENext;
1956 RENext = info->O->getRelocation(RelNext);
1957 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
1958 if (info->O->isRelocationScattered(RENext))
1959 pair_r_value = info->O->getScatteredRelocationValue(RENext);
1964 Symbol.getName(SymName);
1965 const char *name = SymName.data();
1966 op_info->AddSymbol.Present = 1;
1967 op_info->AddSymbol.Name = name;
1969 case MachO::ARM_RELOC_HALF:
1970 if ((r_length & 0x1) == 1) {
1971 op_info->Value = value << 16 | other_half;
1972 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1974 op_info->Value = other_half << 16 | value;
1975 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
1983 // If we have a branch that is not an external relocation entry then
1984 // return 0 so the code in tryAddingSymbolicOperand() can use the
1985 // SymbolLookUp call back with the branch target address to look up the
1986 // symbol and possiblity add an annotation for a symbol stub.
1987 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
1988 r_type == MachO::ARM_THUMB_RELOC_BR22))
1991 uint32_t offset = 0;
1992 if (r_type == MachO::ARM_RELOC_HALF ||
1993 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1994 if ((r_length & 0x1) == 1)
1995 value = value << 16 | other_half;
1997 value = other_half << 16 | value;
1999 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
2000 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
2001 offset = value - r_value;
2005 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2006 if ((r_length & 0x1) == 1)
2007 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2009 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2010 const char *add = GuessSymbolName(r_value, info->AddrMap);
2011 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
2012 int32_t offset = value - (r_value - pair_r_value);
2013 op_info->AddSymbol.Present = 1;
2015 op_info->AddSymbol.Name = add;
2017 op_info->AddSymbol.Value = r_value;
2018 op_info->SubtractSymbol.Present = 1;
2020 op_info->SubtractSymbol.Name = sub;
2022 op_info->SubtractSymbol.Value = pair_r_value;
2023 op_info->Value = offset;
2027 op_info->AddSymbol.Present = 1;
2028 op_info->Value = offset;
2029 if (r_type == MachO::ARM_RELOC_HALF) {
2030 if ((r_length & 0x1) == 1)
2031 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2033 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2035 const char *add = GuessSymbolName(value, info->AddrMap);
2036 if (add != nullptr) {
2037 op_info->AddSymbol.Name = add;
2040 op_info->AddSymbol.Value = value;
2043 if (Arch == Triple::aarch64) {
2044 if (Offset != 0 || Size != 4)
2046 // First search the section's relocation entries (if any) for an entry
2047 // for this section offset.
2048 uint64_t sect_addr = info->S.getAddress();
2049 uint64_t sect_offset = (Pc + Offset) - sect_addr;
2051 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
2052 [&](const RelocationRef &Reloc) {
2053 uint64_t RelocOffset;
2054 Reloc.getOffset(RelocOffset);
2055 return RelocOffset == sect_offset;
2058 if (Reloc == info->S.relocations().end())
2061 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2062 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2063 uint32_t r_type = info->O->getAnyRelocationType(RE);
2064 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2065 DataRefImpl RelNext = Rel;
2066 info->O->moveRelocationNext(RelNext);
2067 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2069 value = info->O->getPlainRelocationSymbolNum(RENext);
2070 op_info->Value = value;
2073 // NOTE: Scattered relocations don't exist on arm64.
2074 if (!info->O->getPlainRelocationExternal(RE))
2077 Reloc->getSymbol()->getName(SymName);
2078 const char *name = SymName.data();
2079 op_info->AddSymbol.Present = 1;
2080 op_info->AddSymbol.Name = name;
2083 case MachO::ARM64_RELOC_PAGE21:
2085 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2087 case MachO::ARM64_RELOC_PAGEOFF12:
2089 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2091 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2093 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2095 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2097 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2099 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2100 /* @tvlppage is not implemented in llvm-mc */
2101 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2103 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2104 /* @tvlppageoff is not implemented in llvm-mc */
2105 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2108 case MachO::ARM64_RELOC_BRANCH26:
2109 op_info->VariantKind = LLVMDisassembler_VariantKind_None;
2117 // GuessCstringPointer is passed the address of what might be a pointer to a
2118 // literal string in a cstring section. If that address is in a cstring section
2119 // it returns a pointer to that string. Else it returns nullptr.
2120 static const char *GuessCstringPointer(uint64_t ReferenceValue,
2121 struct DisassembleInfo *info) {
2122 uint32_t LoadCommandCount = info->O->getHeader().ncmds;
2123 MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo();
2124 for (unsigned I = 0;; ++I) {
2125 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2126 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2127 for (unsigned J = 0; J < Seg.nsects; ++J) {
2128 MachO::section_64 Sec = info->O->getSection64(Load, J);
2129 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2130 if (section_type == MachO::S_CSTRING_LITERALS &&
2131 ReferenceValue >= Sec.addr &&
2132 ReferenceValue < Sec.addr + Sec.size) {
2133 uint64_t sect_offset = ReferenceValue - Sec.addr;
2134 uint64_t object_offset = Sec.offset + sect_offset;
2135 StringRef MachOContents = info->O->getData();
2136 uint64_t object_size = MachOContents.size();
2137 const char *object_addr = (const char *)MachOContents.data();
2138 if (object_offset < object_size) {
2139 const char *name = object_addr + object_offset;
2146 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2147 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2148 for (unsigned J = 0; J < Seg.nsects; ++J) {
2149 MachO::section Sec = info->O->getSection(Load, J);
2150 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2151 if (section_type == MachO::S_CSTRING_LITERALS &&
2152 ReferenceValue >= Sec.addr &&
2153 ReferenceValue < Sec.addr + Sec.size) {
2154 uint64_t sect_offset = ReferenceValue - Sec.addr;
2155 uint64_t object_offset = Sec.offset + sect_offset;
2156 StringRef MachOContents = info->O->getData();
2157 uint64_t object_size = MachOContents.size();
2158 const char *object_addr = (const char *)MachOContents.data();
2159 if (object_offset < object_size) {
2160 const char *name = object_addr + object_offset;
2168 if (I == LoadCommandCount - 1)
2171 Load = info->O->getNextLoadCommandInfo(Load);
2176 // GuessIndirectSymbol returns the name of the indirect symbol for the
2177 // ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
2178 // an address of a symbol stub or a lazy or non-lazy pointer to associate the
2179 // symbol name being referenced by the stub or pointer.
2180 static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2181 struct DisassembleInfo *info) {
2182 uint32_t LoadCommandCount = info->O->getHeader().ncmds;
2183 MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo();
2184 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2185 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
2186 for (unsigned I = 0;; ++I) {
2187 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2188 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2189 for (unsigned J = 0; J < Seg.nsects; ++J) {
2190 MachO::section_64 Sec = info->O->getSection64(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) {
2200 if (section_type == MachO::S_SYMBOL_STUBS)
2201 stride = Sec.reserved2;
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;
2214 Symbol.getName(SymName);
2215 const char *name = SymName.data();
2221 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2222 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2223 for (unsigned J = 0; J < Seg.nsects; ++J) {
2224 MachO::section Sec = info->O->getSection(Load, J);
2225 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2226 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2227 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2228 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2229 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2230 section_type == MachO::S_SYMBOL_STUBS) &&
2231 ReferenceValue >= Sec.addr &&
2232 ReferenceValue < Sec.addr + Sec.size) {
2234 if (section_type == MachO::S_SYMBOL_STUBS)
2235 stride = Sec.reserved2;
2240 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2241 if (index < Dysymtab.nindirectsyms) {
2242 uint32_t indirect_symbol =
2243 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
2244 if (indirect_symbol < Symtab.nsyms) {
2245 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2246 SymbolRef Symbol = *Sym;
2248 Symbol.getName(SymName);
2249 const char *name = SymName.data();
2256 if (I == LoadCommandCount - 1)
2259 Load = info->O->getNextLoadCommandInfo(Load);
2264 // method_reference() is called passing it the ReferenceName that might be
2265 // a reference it to an Objective-C method call. If so then it allocates and
2266 // assembles a method call string with the values last seen and saved in
2267 // the DisassembleInfo's class_name and selector_name fields. This is saved
2268 // into the method field of the info and any previous string is free'ed.
2269 // Then the class_name field in the info is set to nullptr. The method call
2270 // string is set into ReferenceName and ReferenceType is set to
2271 // LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
2272 // then both ReferenceType and ReferenceName are left unchanged.
2273 static void method_reference(struct DisassembleInfo *info,
2274 uint64_t *ReferenceType,
2275 const char **ReferenceName) {
2276 unsigned int Arch = info->O->getArch();
2277 if (*ReferenceName != nullptr) {
2278 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
2279 if (info->selector_name != nullptr) {
2280 if (info->method != nullptr)
2282 if (info->class_name != nullptr) {
2283 info->method = (char *)malloc(5 + strlen(info->class_name) +
2284 strlen(info->selector_name));
2285 if (info->method != nullptr) {
2286 strcpy(info->method, "+[");
2287 strcat(info->method, info->class_name);
2288 strcat(info->method, " ");
2289 strcat(info->method, info->selector_name);
2290 strcat(info->method, "]");
2291 *ReferenceName = info->method;
2292 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2295 info->method = (char *)malloc(9 + strlen(info->selector_name));
2296 if (info->method != nullptr) {
2297 if (Arch == Triple::x86_64)
2298 strcpy(info->method, "-[%rdi ");
2299 else if (Arch == Triple::aarch64)
2300 strcpy(info->method, "-[x0 ");
2302 strcpy(info->method, "-[r? ");
2303 strcat(info->method, info->selector_name);
2304 strcat(info->method, "]");
2305 *ReferenceName = info->method;
2306 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2309 info->class_name = nullptr;
2311 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
2312 if (info->selector_name != nullptr) {
2313 if (info->method != nullptr)
2315 info->method = (char *)malloc(17 + strlen(info->selector_name));
2316 if (info->method != nullptr) {
2317 if (Arch == Triple::x86_64)
2318 strcpy(info->method, "-[[%rdi super] ");
2319 else if (Arch == Triple::aarch64)
2320 strcpy(info->method, "-[[x0 super] ");
2322 strcpy(info->method, "-[[r? super] ");
2323 strcat(info->method, info->selector_name);
2324 strcat(info->method, "]");
2325 *ReferenceName = info->method;
2326 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2328 info->class_name = nullptr;
2334 // GuessPointerPointer() is passed the address of what might be a pointer to
2335 // a reference to an Objective-C class, selector, message ref or cfstring.
2336 // If so the value of the pointer is returned and one of the booleans are set
2337 // to true. If not zero is returned and all the booleans are set to false.
2338 static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
2339 struct DisassembleInfo *info,
2340 bool &classref, bool &selref, bool &msgref,
2346 uint32_t LoadCommandCount = info->O->getHeader().ncmds;
2347 MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo();
2348 for (unsigned I = 0;; ++I) {
2349 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2350 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2351 for (unsigned J = 0; J < Seg.nsects; ++J) {
2352 MachO::section_64 Sec = info->O->getSection64(Load, J);
2353 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
2354 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2355 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
2356 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
2357 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
2358 ReferenceValue >= Sec.addr &&
2359 ReferenceValue < Sec.addr + Sec.size) {
2360 uint64_t sect_offset = ReferenceValue - Sec.addr;
2361 uint64_t object_offset = Sec.offset + sect_offset;
2362 StringRef MachOContents = info->O->getData();
2363 uint64_t object_size = MachOContents.size();
2364 const char *object_addr = (const char *)MachOContents.data();
2365 if (object_offset < object_size) {
2366 uint64_t pointer_value;
2367 memcpy(&pointer_value, object_addr + object_offset,
2369 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2370 sys::swapByteOrder(pointer_value);
2371 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
2373 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2374 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
2376 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
2377 ReferenceValue + 8 < Sec.addr + Sec.size) {
2379 memcpy(&pointer_value, object_addr + object_offset + 8,
2381 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2382 sys::swapByteOrder(pointer_value);
2383 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
2385 return pointer_value;
2392 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
2393 if (I == LoadCommandCount - 1)
2396 Load = info->O->getNextLoadCommandInfo(Load);
2401 // get_pointer_64 returns a pointer to the bytes in the object file at the
2402 // Address from a section in the Mach-O file. And indirectly returns the
2403 // offset into the section, number of bytes left in the section past the offset
2404 // and which section is was being referenced. If the Address is not in a
2405 // section nullptr is returned.
2406 static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
2407 uint32_t &left, SectionRef &S,
2408 DisassembleInfo *info,
2409 bool objc_only = false) {
2413 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
2414 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
2415 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
2418 ((*(info->Sections))[SectIdx]).getName(SectName);
2419 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
2420 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
2421 if (SegName != "__OBJC" && SectName != "__cstring")
2424 if (Address >= SectAddress && Address < SectAddress + SectSize) {
2425 S = (*(info->Sections))[SectIdx];
2426 offset = Address - SectAddress;
2427 left = SectSize - offset;
2428 StringRef SectContents;
2429 ((*(info->Sections))[SectIdx]).getContents(SectContents);
2430 return SectContents.data() + offset;
2436 static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
2437 uint32_t &left, SectionRef &S,
2438 DisassembleInfo *info,
2439 bool objc_only = false) {
2440 return get_pointer_64(Address, offset, left, S, info, objc_only);
2443 // get_symbol_64() returns the name of a symbol (or nullptr) and the address of
2444 // the symbol indirectly through n_value. Based on the relocation information
2445 // for the specified section offset in the specified section reference.
2446 // If no relocation information is found and a non-zero ReferenceValue for the
2447 // symbol is passed, look up that address in the info's AddrMap.
2449 get_symbol_64(uint32_t sect_offset, SectionRef S, DisassembleInfo *info,
2451 uint64_t ReferenceValue = UnknownAddressOrSize) {
2456 // See if there is an external relocation entry at the sect_offset.
2457 bool reloc_found = false;
2459 MachO::any_relocation_info RE;
2460 bool isExtern = false;
2462 for (const RelocationRef &Reloc : S.relocations()) {
2463 uint64_t RelocOffset;
2464 Reloc.getOffset(RelocOffset);
2465 if (RelocOffset == sect_offset) {
2466 Rel = Reloc.getRawDataRefImpl();
2467 RE = info->O->getRelocation(Rel);
2468 if (info->O->isRelocationScattered(RE))
2470 isExtern = info->O->getPlainRelocationExternal(RE);
2472 symbol_iterator RelocSym = Reloc.getSymbol();
2479 // If there is an external relocation entry for a symbol in this section
2480 // at this section_offset then use that symbol's value for the n_value
2481 // and return its name.
2482 const char *SymbolName = nullptr;
2483 if (reloc_found && isExtern) {
2484 Symbol.getAddress(n_value);
2485 if (n_value == UnknownAddressOrSize)
2488 Symbol.getName(name);
2489 if (!name.empty()) {
2490 SymbolName = name.data();
2495 // TODO: For fully linked images, look through the external relocation
2496 // entries off the dynamic symtab command. For these the r_offset is from the
2497 // start of the first writeable segment in the Mach-O file. So the offset
2498 // to this section from that segment is passed to this routine by the caller,
2499 // as the database_offset. Which is the difference of the section's starting
2500 // address and the first writable segment.
2502 // NOTE: need add passing the database_offset to this routine.
2504 // We did not find an external relocation entry so look up the ReferenceValue
2505 // as an address of a symbol and if found return that symbol's name.
2506 if (ReferenceValue != UnknownAddressOrSize)
2507 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
2512 static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
2513 DisassembleInfo *info,
2514 uint32_t ReferenceValue) {
2516 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
2519 // These are structs in the Objective-C meta data and read to produce the
2520 // comments for disassembly. While these are part of the ABI they are no
2521 // public defintions. So the are here not in include/llvm/Support/MachO.h .
2523 // The cfstring object in a 64-bit Mach-O file.
2524 struct cfstring64_t {
2525 uint64_t isa; // class64_t * (64-bit pointer)
2526 uint64_t flags; // flag bits
2527 uint64_t characters; // char * (64-bit pointer)
2528 uint64_t length; // number of non-NULL characters in above
2531 // The class object in a 64-bit Mach-O file.
2533 uint64_t isa; // class64_t * (64-bit pointer)
2534 uint64_t superclass; // class64_t * (64-bit pointer)
2535 uint64_t cache; // Cache (64-bit pointer)
2536 uint64_t vtable; // IMP * (64-bit pointer)
2537 uint64_t data; // class_ro64_t * (64-bit pointer)
2541 uint32_t isa; /* class32_t * (32-bit pointer) */
2542 uint32_t superclass; /* class32_t * (32-bit pointer) */
2543 uint32_t cache; /* Cache (32-bit pointer) */
2544 uint32_t vtable; /* IMP * (32-bit pointer) */
2545 uint32_t data; /* class_ro32_t * (32-bit pointer) */
2548 struct class_ro64_t {
2550 uint32_t instanceStart;
2551 uint32_t instanceSize;
2553 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
2554 uint64_t name; // const char * (64-bit pointer)
2555 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
2556 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
2557 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
2558 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
2559 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
2562 struct class_ro32_t {
2564 uint32_t instanceStart;
2565 uint32_t instanceSize;
2566 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
2567 uint32_t name; /* const char * (32-bit pointer) */
2568 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
2569 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
2570 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
2571 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
2572 uint32_t baseProperties; /* const struct objc_property_list *
2576 /* Values for class_ro{64,32}_t->flags */
2577 #define RO_META (1 << 0)
2578 #define RO_ROOT (1 << 1)
2579 #define RO_HAS_CXX_STRUCTORS (1 << 2)
2581 struct method_list64_t {
2584 /* struct method64_t first; These structures follow inline */
2587 struct method_list32_t {
2590 /* struct method32_t first; These structures follow inline */
2594 uint64_t name; /* SEL (64-bit pointer) */
2595 uint64_t types; /* const char * (64-bit pointer) */
2596 uint64_t imp; /* IMP (64-bit pointer) */
2600 uint32_t name; /* SEL (32-bit pointer) */
2601 uint32_t types; /* const char * (32-bit pointer) */
2602 uint32_t imp; /* IMP (32-bit pointer) */
2605 struct protocol_list64_t {
2606 uint64_t count; /* uintptr_t (a 64-bit value) */
2607 /* struct protocol64_t * list[0]; These pointers follow inline */
2610 struct protocol_list32_t {
2611 uint32_t count; /* uintptr_t (a 32-bit value) */
2612 /* struct protocol32_t * list[0]; These pointers follow inline */
2615 struct protocol64_t {
2616 uint64_t isa; /* id * (64-bit pointer) */
2617 uint64_t name; /* const char * (64-bit pointer) */
2618 uint64_t protocols; /* struct protocol_list64_t *
2620 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
2621 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
2622 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
2623 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
2624 uint64_t instanceProperties; /* struct objc_property_list *
2628 struct protocol32_t {
2629 uint32_t isa; /* id * (32-bit pointer) */
2630 uint32_t name; /* const char * (32-bit pointer) */
2631 uint32_t protocols; /* struct protocol_list_t *
2633 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
2634 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
2635 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
2636 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
2637 uint32_t instanceProperties; /* struct objc_property_list *
2641 struct ivar_list64_t {
2644 /* struct ivar64_t first; These structures follow inline */
2647 struct ivar_list32_t {
2650 /* struct ivar32_t first; These structures follow inline */
2654 uint64_t offset; /* uintptr_t * (64-bit pointer) */
2655 uint64_t name; /* const char * (64-bit pointer) */
2656 uint64_t type; /* const char * (64-bit pointer) */
2662 uint32_t offset; /* uintptr_t * (32-bit pointer) */
2663 uint32_t name; /* const char * (32-bit pointer) */
2664 uint32_t type; /* const char * (32-bit pointer) */
2669 struct objc_property_list64 {
2672 /* struct objc_property64 first; These structures follow inline */
2675 struct objc_property_list32 {
2678 /* struct objc_property32 first; These structures follow inline */
2681 struct objc_property64 {
2682 uint64_t name; /* const char * (64-bit pointer) */
2683 uint64_t attributes; /* const char * (64-bit pointer) */
2686 struct objc_property32 {
2687 uint32_t name; /* const char * (32-bit pointer) */
2688 uint32_t attributes; /* const char * (32-bit pointer) */
2691 struct category64_t {
2692 uint64_t name; /* const char * (64-bit pointer) */
2693 uint64_t cls; /* struct class_t * (64-bit pointer) */
2694 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
2695 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
2696 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
2697 uint64_t instanceProperties; /* struct objc_property_list *
2701 struct category32_t {
2702 uint32_t name; /* const char * (32-bit pointer) */
2703 uint32_t cls; /* struct class_t * (32-bit pointer) */
2704 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
2705 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
2706 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
2707 uint32_t instanceProperties; /* struct objc_property_list *
2711 struct objc_image_info64 {
2715 struct objc_image_info32 {
2719 struct imageInfo_t {
2723 /* masks for objc_image_info.flags */
2724 #define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
2725 #define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
2727 struct message_ref64 {
2728 uint64_t imp; /* IMP (64-bit pointer) */
2729 uint64_t sel; /* SEL (64-bit pointer) */
2732 struct message_ref32 {
2733 uint32_t imp; /* IMP (32-bit pointer) */
2734 uint32_t sel; /* SEL (32-bit pointer) */
2737 // Objective-C 1 (32-bit only) meta data structs.
2739 struct objc_module_t {
2742 uint32_t name; /* char * (32-bit pointer) */
2743 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
2746 struct objc_symtab_t {
2747 uint32_t sel_ref_cnt;
2748 uint32_t refs; /* SEL * (32-bit pointer) */
2749 uint16_t cls_def_cnt;
2750 uint16_t cat_def_cnt;
2751 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
2754 struct objc_class_t {
2755 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2756 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
2757 uint32_t name; /* const char * (32-bit pointer) */
2760 int32_t instance_size;
2761 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
2762 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
2763 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
2764 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
2767 #define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
2768 // class is not a metaclass
2769 #define CLS_CLASS 0x1
2770 // class is a metaclass
2771 #define CLS_META 0x2
2773 struct objc_category_t {
2774 uint32_t category_name; /* char * (32-bit pointer) */
2775 uint32_t class_name; /* char * (32-bit pointer) */
2776 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
2777 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
2778 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
2781 struct objc_ivar_t {
2782 uint32_t ivar_name; /* char * (32-bit pointer) */
2783 uint32_t ivar_type; /* char * (32-bit pointer) */
2784 int32_t ivar_offset;
2787 struct objc_ivar_list_t {
2789 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
2792 struct objc_method_list_t {
2793 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
2794 int32_t method_count;
2795 // struct objc_method_t method_list[1]; /* variable length structure */
2798 struct objc_method_t {
2799 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2800 uint32_t method_types; /* char * (32-bit pointer) */
2801 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
2805 struct objc_protocol_list_t {
2806 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
2808 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
2809 // (32-bit pointer) */
2812 struct objc_protocol_t {
2813 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2814 uint32_t protocol_name; /* char * (32-bit pointer) */
2815 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
2816 uint32_t instance_methods; /* struct objc_method_description_list *
2818 uint32_t class_methods; /* struct objc_method_description_list *
2822 struct objc_method_description_list_t {
2824 // struct objc_method_description_t list[1];
2827 struct objc_method_description_t {
2828 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2829 uint32_t types; /* char * (32-bit pointer) */
2832 inline void swapStruct(struct cfstring64_t &cfs) {
2833 sys::swapByteOrder(cfs.isa);
2834 sys::swapByteOrder(cfs.flags);
2835 sys::swapByteOrder(cfs.characters);
2836 sys::swapByteOrder(cfs.length);
2839 inline void swapStruct(struct class64_t &c) {
2840 sys::swapByteOrder(c.isa);
2841 sys::swapByteOrder(c.superclass);
2842 sys::swapByteOrder(c.cache);
2843 sys::swapByteOrder(c.vtable);
2844 sys::swapByteOrder(c.data);
2847 inline void swapStruct(struct class32_t &c) {
2848 sys::swapByteOrder(c.isa);
2849 sys::swapByteOrder(c.superclass);
2850 sys::swapByteOrder(c.cache);
2851 sys::swapByteOrder(c.vtable);
2852 sys::swapByteOrder(c.data);
2855 inline void swapStruct(struct class_ro64_t &cro) {
2856 sys::swapByteOrder(cro.flags);
2857 sys::swapByteOrder(cro.instanceStart);
2858 sys::swapByteOrder(cro.instanceSize);
2859 sys::swapByteOrder(cro.reserved);
2860 sys::swapByteOrder(cro.ivarLayout);
2861 sys::swapByteOrder(cro.name);
2862 sys::swapByteOrder(cro.baseMethods);
2863 sys::swapByteOrder(cro.baseProtocols);
2864 sys::swapByteOrder(cro.ivars);
2865 sys::swapByteOrder(cro.weakIvarLayout);
2866 sys::swapByteOrder(cro.baseProperties);
2869 inline void swapStruct(struct class_ro32_t &cro) {
2870 sys::swapByteOrder(cro.flags);
2871 sys::swapByteOrder(cro.instanceStart);
2872 sys::swapByteOrder(cro.instanceSize);
2873 sys::swapByteOrder(cro.ivarLayout);
2874 sys::swapByteOrder(cro.name);
2875 sys::swapByteOrder(cro.baseMethods);
2876 sys::swapByteOrder(cro.baseProtocols);
2877 sys::swapByteOrder(cro.ivars);
2878 sys::swapByteOrder(cro.weakIvarLayout);
2879 sys::swapByteOrder(cro.baseProperties);
2882 inline void swapStruct(struct method_list64_t &ml) {
2883 sys::swapByteOrder(ml.entsize);
2884 sys::swapByteOrder(ml.count);
2887 inline void swapStruct(struct method_list32_t &ml) {
2888 sys::swapByteOrder(ml.entsize);
2889 sys::swapByteOrder(ml.count);
2892 inline void swapStruct(struct method64_t &m) {
2893 sys::swapByteOrder(m.name);
2894 sys::swapByteOrder(m.types);
2895 sys::swapByteOrder(m.imp);
2898 inline void swapStruct(struct method32_t &m) {
2899 sys::swapByteOrder(m.name);
2900 sys::swapByteOrder(m.types);
2901 sys::swapByteOrder(m.imp);
2904 inline void swapStruct(struct protocol_list64_t &pl) {
2905 sys::swapByteOrder(pl.count);
2908 inline void swapStruct(struct protocol_list32_t &pl) {
2909 sys::swapByteOrder(pl.count);
2912 inline void swapStruct(struct protocol64_t &p) {
2913 sys::swapByteOrder(p.isa);
2914 sys::swapByteOrder(p.name);
2915 sys::swapByteOrder(p.protocols);
2916 sys::swapByteOrder(p.instanceMethods);
2917 sys::swapByteOrder(p.classMethods);
2918 sys::swapByteOrder(p.optionalInstanceMethods);
2919 sys::swapByteOrder(p.optionalClassMethods);
2920 sys::swapByteOrder(p.instanceProperties);
2923 inline void swapStruct(struct protocol32_t &p) {
2924 sys::swapByteOrder(p.isa);
2925 sys::swapByteOrder(p.name);
2926 sys::swapByteOrder(p.protocols);
2927 sys::swapByteOrder(p.instanceMethods);
2928 sys::swapByteOrder(p.classMethods);
2929 sys::swapByteOrder(p.optionalInstanceMethods);
2930 sys::swapByteOrder(p.optionalClassMethods);
2931 sys::swapByteOrder(p.instanceProperties);
2934 inline void swapStruct(struct ivar_list64_t &il) {
2935 sys::swapByteOrder(il.entsize);
2936 sys::swapByteOrder(il.count);
2939 inline void swapStruct(struct ivar_list32_t &il) {
2940 sys::swapByteOrder(il.entsize);
2941 sys::swapByteOrder(il.count);
2944 inline void swapStruct(struct ivar64_t &i) {
2945 sys::swapByteOrder(i.offset);
2946 sys::swapByteOrder(i.name);
2947 sys::swapByteOrder(i.type);
2948 sys::swapByteOrder(i.alignment);
2949 sys::swapByteOrder(i.size);
2952 inline void swapStruct(struct ivar32_t &i) {
2953 sys::swapByteOrder(i.offset);
2954 sys::swapByteOrder(i.name);
2955 sys::swapByteOrder(i.type);
2956 sys::swapByteOrder(i.alignment);
2957 sys::swapByteOrder(i.size);
2960 inline void swapStruct(struct objc_property_list64 &pl) {
2961 sys::swapByteOrder(pl.entsize);
2962 sys::swapByteOrder(pl.count);
2965 inline void swapStruct(struct objc_property_list32 &pl) {
2966 sys::swapByteOrder(pl.entsize);
2967 sys::swapByteOrder(pl.count);
2970 inline void swapStruct(struct objc_property64 &op) {
2971 sys::swapByteOrder(op.name);
2972 sys::swapByteOrder(op.attributes);
2975 inline void swapStruct(struct objc_property32 &op) {
2976 sys::swapByteOrder(op.name);
2977 sys::swapByteOrder(op.attributes);
2980 inline void swapStruct(struct category64_t &c) {
2981 sys::swapByteOrder(c.name);
2982 sys::swapByteOrder(c.cls);
2983 sys::swapByteOrder(c.instanceMethods);
2984 sys::swapByteOrder(c.classMethods);
2985 sys::swapByteOrder(c.protocols);
2986 sys::swapByteOrder(c.instanceProperties);
2989 inline void swapStruct(struct category32_t &c) {
2990 sys::swapByteOrder(c.name);
2991 sys::swapByteOrder(c.cls);
2992 sys::swapByteOrder(c.instanceMethods);
2993 sys::swapByteOrder(c.classMethods);
2994 sys::swapByteOrder(c.protocols);
2995 sys::swapByteOrder(c.instanceProperties);
2998 inline void swapStruct(struct objc_image_info64 &o) {
2999 sys::swapByteOrder(o.version);
3000 sys::swapByteOrder(o.flags);
3003 inline void swapStruct(struct objc_image_info32 &o) {
3004 sys::swapByteOrder(o.version);
3005 sys::swapByteOrder(o.flags);
3008 inline void swapStruct(struct imageInfo_t &o) {
3009 sys::swapByteOrder(o.version);
3010 sys::swapByteOrder(o.flags);
3013 inline void swapStruct(struct message_ref64 &mr) {
3014 sys::swapByteOrder(mr.imp);
3015 sys::swapByteOrder(mr.sel);
3018 inline void swapStruct(struct message_ref32 &mr) {
3019 sys::swapByteOrder(mr.imp);
3020 sys::swapByteOrder(mr.sel);
3023 inline void swapStruct(struct objc_module_t &module) {
3024 sys::swapByteOrder(module.version);
3025 sys::swapByteOrder(module.size);
3026 sys::swapByteOrder(module.name);
3027 sys::swapByteOrder(module.symtab);
3030 inline void swapStruct(struct objc_symtab_t &symtab) {
3031 sys::swapByteOrder(symtab.sel_ref_cnt);
3032 sys::swapByteOrder(symtab.refs);
3033 sys::swapByteOrder(symtab.cls_def_cnt);
3034 sys::swapByteOrder(symtab.cat_def_cnt);
3037 inline void swapStruct(struct objc_class_t &objc_class) {
3038 sys::swapByteOrder(objc_class.isa);
3039 sys::swapByteOrder(objc_class.super_class);
3040 sys::swapByteOrder(objc_class.name);
3041 sys::swapByteOrder(objc_class.version);
3042 sys::swapByteOrder(objc_class.info);
3043 sys::swapByteOrder(objc_class.instance_size);
3044 sys::swapByteOrder(objc_class.ivars);
3045 sys::swapByteOrder(objc_class.methodLists);
3046 sys::swapByteOrder(objc_class.cache);
3047 sys::swapByteOrder(objc_class.protocols);
3050 inline void swapStruct(struct objc_category_t &objc_category) {
3051 sys::swapByteOrder(objc_category.category_name);
3052 sys::swapByteOrder(objc_category.class_name);
3053 sys::swapByteOrder(objc_category.instance_methods);
3054 sys::swapByteOrder(objc_category.class_methods);
3055 sys::swapByteOrder(objc_category.protocols);
3058 inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3059 sys::swapByteOrder(objc_ivar_list.ivar_count);
3062 inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3063 sys::swapByteOrder(objc_ivar.ivar_name);
3064 sys::swapByteOrder(objc_ivar.ivar_type);
3065 sys::swapByteOrder(objc_ivar.ivar_offset);
3068 inline void swapStruct(struct objc_method_list_t &method_list) {
3069 sys::swapByteOrder(method_list.obsolete);
3070 sys::swapByteOrder(method_list.method_count);
3073 inline void swapStruct(struct objc_method_t &method) {
3074 sys::swapByteOrder(method.method_name);
3075 sys::swapByteOrder(method.method_types);
3076 sys::swapByteOrder(method.method_imp);
3079 inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3080 sys::swapByteOrder(protocol_list.next);
3081 sys::swapByteOrder(protocol_list.count);
3084 inline void swapStruct(struct objc_protocol_t &protocol) {
3085 sys::swapByteOrder(protocol.isa);
3086 sys::swapByteOrder(protocol.protocol_name);
3087 sys::swapByteOrder(protocol.protocol_list);
3088 sys::swapByteOrder(protocol.instance_methods);
3089 sys::swapByteOrder(protocol.class_methods);
3092 inline void swapStruct(struct objc_method_description_list_t &mdl) {
3093 sys::swapByteOrder(mdl.count);
3096 inline void swapStruct(struct objc_method_description_t &md) {
3097 sys::swapByteOrder(md.name);
3098 sys::swapByteOrder(md.types);
3101 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3102 struct DisassembleInfo *info);
3104 // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3105 // to an Objective-C class and returns the class name. It is also passed the
3106 // address of the pointer, so when the pointer is zero as it can be in an .o
3107 // file, that is used to look for an external relocation entry with a symbol
3109 static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3110 uint64_t ReferenceValue,
3111 struct DisassembleInfo *info) {
3113 uint32_t offset, left;
3116 // The pointer_value can be 0 in an object file and have a relocation
3117 // entry for the class symbol at the ReferenceValue (the address of the
3119 if (pointer_value == 0) {
3120 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3121 if (r == nullptr || left < sizeof(uint64_t))
3124 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3125 if (symbol_name == nullptr)
3127 const char *class_name = strrchr(symbol_name, '$');
3128 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3129 return class_name + 2;
3134 // The case were the pointer_value is non-zero and points to a class defined
3135 // in this Mach-O file.
3136 r = get_pointer_64(pointer_value, offset, left, S, info);
3137 if (r == nullptr || left < sizeof(struct class64_t))
3140 memcpy(&c, r, sizeof(struct class64_t));
3141 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3145 r = get_pointer_64(c.data, offset, left, S, info);
3146 if (r == nullptr || left < sizeof(struct class_ro64_t))
3148 struct class_ro64_t cro;
3149 memcpy(&cro, r, sizeof(struct class_ro64_t));
3150 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3154 const char *name = get_pointer_64(cro.name, offset, left, S, info);
3158 // get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3159 // pointer to a cfstring and returns its name or nullptr.
3160 static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3161 struct DisassembleInfo *info) {
3162 const char *r, *name;
3163 uint32_t offset, left;
3165 struct cfstring64_t cfs;
3166 uint64_t cfs_characters;
3168 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3169 if (r == nullptr || left < sizeof(struct cfstring64_t))
3171 memcpy(&cfs, r, sizeof(struct cfstring64_t));
3172 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3174 if (cfs.characters == 0) {
3176 const char *symbol_name = get_symbol_64(
3177 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
3178 if (symbol_name == nullptr)
3180 cfs_characters = n_value;
3182 cfs_characters = cfs.characters;
3183 name = get_pointer_64(cfs_characters, offset, left, S, info);
3188 // get_objc2_64bit_selref() is used for disassembly and is passed a the address
3189 // of a pointer to an Objective-C selector reference when the pointer value is
3190 // zero as in a .o file and is likely to have a external relocation entry with
3191 // who's symbol's n_value is the real pointer to the selector name. If that is
3192 // the case the real pointer to the selector name is returned else 0 is
3194 static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3195 struct DisassembleInfo *info) {
3196 uint32_t offset, left;
3199 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3200 if (r == nullptr || left < sizeof(uint64_t))
3203 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3204 if (symbol_name == nullptr)
3209 static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3210 const char *sectname) {
3211 for (const SectionRef &Section : O->sections()) {
3213 Section.getName(SectName);
3214 DataRefImpl Ref = Section.getRawDataRefImpl();
3215 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3216 if (SegName == segname && SectName == sectname)
3219 return SectionRef();
3223 walk_pointer_list_64(const char *listname, const SectionRef S,
3224 MachOObjectFile *O, struct DisassembleInfo *info,
3225 void (*func)(uint64_t, struct DisassembleInfo *info)) {
3226 if (S == SectionRef())
3230 S.getName(SectName);
3231 DataRefImpl Ref = S.getRawDataRefImpl();
3232 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3233 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3236 S.getContents(BytesStr);
3237 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3239 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3240 uint32_t left = S.getSize() - i;
3241 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3243 memcpy(&p, Contents + i, size);
3244 if (i + sizeof(uint64_t) > S.getSize())
3245 outs() << listname << " list pointer extends past end of (" << SegName
3246 << "," << SectName << ") section\n";
3247 outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
3249 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3250 sys::swapByteOrder(p);
3252 uint64_t n_value = 0;
3253 const char *name = get_symbol_64(i, S, info, n_value, p);
3254 if (name == nullptr)
3255 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
3258 outs() << format("0x%" PRIx64, n_value);
3260 outs() << " + " << format("0x%" PRIx64, p);
3262 outs() << format("0x%" PRIx64, p);
3263 if (name != nullptr)
3264 outs() << " " << name;
3274 walk_pointer_list_32(const char *listname, const SectionRef S,
3275 MachOObjectFile *O, struct DisassembleInfo *info,
3276 void (*func)(uint32_t, struct DisassembleInfo *info)) {
3277 if (S == SectionRef())
3281 S.getName(SectName);
3282 DataRefImpl Ref = S.getRawDataRefImpl();
3283 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3284 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3287 S.getContents(BytesStr);
3288 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3290 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
3291 uint32_t left = S.getSize() - i;
3292 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
3294 memcpy(&p, Contents + i, size);
3295 if (i + sizeof(uint32_t) > S.getSize())
3296 outs() << listname << " list pointer extends past end of (" << SegName
3297 << "," << SectName << ") section\n";
3298 uint32_t Address = S.getAddress() + i;
3299 outs() << format("%08" PRIx32, Address) << " ";
3301 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3302 sys::swapByteOrder(p);
3303 outs() << format("0x%" PRIx32, p);
3305 const char *name = get_symbol_32(i, S, info, p);
3306 if (name != nullptr)
3307 outs() << " " << name;
3315 static void print_layout_map(const char *layout_map, uint32_t left) {
3316 outs() << " layout map: ";
3318 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
3321 } while (*layout_map != '\0' && left != 0);
3325 static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
3326 uint32_t offset, left;
3328 const char *layout_map;
3332 layout_map = get_pointer_64(p, offset, left, S, info);
3333 print_layout_map(layout_map, left);
3336 static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
3337 uint32_t offset, left;
3339 const char *layout_map;
3343 layout_map = get_pointer_32(p, offset, left, S, info);
3344 print_layout_map(layout_map, left);
3347 static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
3348 const char *indent) {
3349 struct method_list64_t ml;
3350 struct method64_t m;
3352 uint32_t offset, xoffset, left, i;
3354 const char *name, *sym_name;
3357 r = get_pointer_64(p, offset, left, S, info);
3360 memset(&ml, '\0', sizeof(struct method_list64_t));
3361 if (left < sizeof(struct method_list64_t)) {
3362 memcpy(&ml, r, left);
3363 outs() << " (method_list_t entends past the end of the section)\n";
3365 memcpy(&ml, r, sizeof(struct method_list64_t));
3366 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3368 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3369 outs() << indent << "\t\t count " << ml.count << "\n";
3371 p += sizeof(struct method_list64_t);
3372 offset += sizeof(struct method_list64_t);
3373 for (i = 0; i < ml.count; i++) {
3374 r = get_pointer_64(p, offset, left, S, info);
3377 memset(&m, '\0', sizeof(struct method64_t));
3378 if (left < sizeof(struct method64_t)) {
3379 memcpy(&ml, r, left);
3380 outs() << indent << " (method_t entends past the end of the section)\n";
3382 memcpy(&m, r, sizeof(struct method64_t));
3383 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3386 outs() << indent << "\t\t name ";
3387 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
3388 info, n_value, m.name);
3390 if (info->verbose && sym_name != nullptr)
3393 outs() << format("0x%" PRIx64, n_value);
3395 outs() << " + " << format("0x%" PRIx64, m.name);
3397 outs() << format("0x%" PRIx64, m.name);
3398 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
3399 if (name != nullptr)
3400 outs() << format(" %.*s", left, name);
3403 outs() << indent << "\t\t types ";
3404 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
3405 info, n_value, m.types);
3407 if (info->verbose && sym_name != nullptr)
3410 outs() << format("0x%" PRIx64, n_value);
3412 outs() << " + " << format("0x%" PRIx64, m.types);
3414 outs() << format("0x%" PRIx64, m.types);
3415 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
3416 if (name != nullptr)
3417 outs() << format(" %.*s", left, name);
3420 outs() << indent << "\t\t imp ";
3421 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
3423 if (info->verbose && name == nullptr) {
3425 outs() << format("0x%" PRIx64, n_value) << " ";
3427 outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
3429 outs() << format("0x%" PRIx64, m.imp) << " ";
3431 if (name != nullptr)
3435 p += sizeof(struct method64_t);
3436 offset += sizeof(struct method64_t);
3440 static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
3441 const char *indent) {
3442 struct method_list32_t ml;
3443 struct method32_t m;
3444 const char *r, *name;
3445 uint32_t offset, xoffset, left, i;
3448 r = get_pointer_32(p, offset, left, S, info);
3451 memset(&ml, '\0', sizeof(struct method_list32_t));
3452 if (left < sizeof(struct method_list32_t)) {
3453 memcpy(&ml, r, left);
3454 outs() << " (method_list_t entends past the end of the section)\n";
3456 memcpy(&ml, r, sizeof(struct method_list32_t));
3457 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3459 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3460 outs() << indent << "\t\t count " << ml.count << "\n";
3462 p += sizeof(struct method_list32_t);
3463 offset += sizeof(struct method_list32_t);
3464 for (i = 0; i < ml.count; i++) {
3465 r = get_pointer_32(p, offset, left, S, info);
3468 memset(&m, '\0', sizeof(struct method32_t));
3469 if (left < sizeof(struct method32_t)) {
3470 memcpy(&ml, r, left);
3471 outs() << indent << " (method_t entends past the end of the section)\n";
3473 memcpy(&m, r, sizeof(struct method32_t));
3474 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3477 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
3478 name = get_pointer_32(m.name, xoffset, left, xS, info);
3479 if (name != nullptr)
3480 outs() << format(" %.*s", left, name);
3483 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
3484 name = get_pointer_32(m.types, xoffset, left, xS, info);
3485 if (name != nullptr)
3486 outs() << format(" %.*s", left, name);
3489 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
3490 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
3492 if (name != nullptr)
3493 outs() << " " << name;
3496 p += sizeof(struct method32_t);
3497 offset += sizeof(struct method32_t);
3501 static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
3502 uint32_t offset, left, xleft;
3504 struct objc_method_list_t method_list;
3505 struct objc_method_t method;
3506 const char *r, *methods, *name, *SymbolName;
3509 r = get_pointer_32(p, offset, left, S, info, true);
3514 if (left > sizeof(struct objc_method_list_t)) {
3515 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
3517 outs() << "\t\t objc_method_list extends past end of the section\n";
3518 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
3519 memcpy(&method_list, r, left);
3521 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3522 swapStruct(method_list);
3524 outs() << "\t\t obsolete "
3525 << format("0x%08" PRIx32, method_list.obsolete) << "\n";
3526 outs() << "\t\t method_count " << method_list.method_count << "\n";
3528 methods = r + sizeof(struct objc_method_list_t);
3529 for (i = 0; i < method_list.method_count; i++) {
3530 if ((i + 1) * sizeof(struct objc_method_t) > left) {
3531 outs() << "\t\t remaining method's extend past the of the section\n";
3534 memcpy(&method, methods + i * sizeof(struct objc_method_t),
3535 sizeof(struct objc_method_t));
3536 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3539 outs() << "\t\t method_name "
3540 << format("0x%08" PRIx32, method.method_name);
3541 if (info->verbose) {
3542 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
3543 if (name != nullptr)
3544 outs() << format(" %.*s", xleft, name);
3546 outs() << " (not in an __OBJC section)";
3550 outs() << "\t\t method_types "
3551 << format("0x%08" PRIx32, method.method_types);
3552 if (info->verbose) {
3553 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
3554 if (name != nullptr)
3555 outs() << format(" %.*s", xleft, name);
3557 outs() << " (not in an __OBJC section)";
3561 outs() << "\t\t method_imp "
3562 << format("0x%08" PRIx32, method.method_imp) << " ";
3563 if (info->verbose) {
3564 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
3565 if (SymbolName != nullptr)
3566 outs() << SymbolName;
3573 static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
3574 struct protocol_list64_t pl;
3575 uint64_t q, n_value;
3576 struct protocol64_t pc;
3578 uint32_t offset, xoffset, left, i;
3580 const char *name, *sym_name;
3582 r = get_pointer_64(p, offset, left, S, info);
3585 memset(&pl, '\0', sizeof(struct protocol_list64_t));
3586 if (left < sizeof(struct protocol_list64_t)) {
3587 memcpy(&pl, r, left);
3588 outs() << " (protocol_list_t entends past the end of the section)\n";
3590 memcpy(&pl, r, sizeof(struct protocol_list64_t));
3591 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3593 outs() << " count " << pl.count << "\n";
3595 p += sizeof(struct protocol_list64_t);
3596 offset += sizeof(struct protocol_list64_t);
3597 for (i = 0; i < pl.count; i++) {
3598 r = get_pointer_64(p, offset, left, S, info);
3602 if (left < sizeof(uint64_t)) {
3603 memcpy(&q, r, left);
3604 outs() << " (protocol_t * entends past the end of the section)\n";
3606 memcpy(&q, r, sizeof(uint64_t));
3607 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3608 sys::swapByteOrder(q);
3610 outs() << "\t\t list[" << i << "] ";
3611 sym_name = get_symbol_64(offset, S, info, n_value, q);
3613 if (info->verbose && sym_name != nullptr)
3616 outs() << format("0x%" PRIx64, n_value);
3618 outs() << " + " << format("0x%" PRIx64, q);
3620 outs() << format("0x%" PRIx64, q);
3621 outs() << " (struct protocol_t *)\n";
3623 r = get_pointer_64(q + n_value, offset, left, S, info);
3626 memset(&pc, '\0', sizeof(struct protocol64_t));
3627 if (left < sizeof(struct protocol64_t)) {
3628 memcpy(&pc, r, left);
3629 outs() << " (protocol_t entends past the end of the section)\n";
3631 memcpy(&pc, r, sizeof(struct protocol64_t));
3632 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3635 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
3637 outs() << "\t\t\t name ";
3638 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
3639 info, n_value, pc.name);
3641 if (info->verbose && sym_name != nullptr)
3644 outs() << format("0x%" PRIx64, n_value);
3646 outs() << " + " << format("0x%" PRIx64, pc.name);
3648 outs() << format("0x%" PRIx64, pc.name);
3649 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
3650 if (name != nullptr)
3651 outs() << format(" %.*s", left, name);
3654 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
3656 outs() << "\t\t instanceMethods ";
3658 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
3659 S, info, n_value, pc.instanceMethods);
3661 if (info->verbose && sym_name != nullptr)
3664 outs() << format("0x%" PRIx64, n_value);
3665 if (pc.instanceMethods != 0)
3666 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
3668 outs() << format("0x%" PRIx64, pc.instanceMethods);
3669 outs() << " (struct method_list_t *)\n";
3670 if (pc.instanceMethods + n_value != 0)
3671 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
3673 outs() << "\t\t classMethods ";
3675 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
3676 info, n_value, pc.classMethods);
3678 if (info->verbose && sym_name != nullptr)
3681 outs() << format("0x%" PRIx64, n_value);
3682 if (pc.classMethods != 0)
3683 outs() << " + " << format("0x%" PRIx64, pc.classMethods);
3685 outs() << format("0x%" PRIx64, pc.classMethods);
3686 outs() << " (struct method_list_t *)\n";
3687 if (pc.classMethods + n_value != 0)
3688 print_method_list64_t(pc.classMethods + n_value, info, "\t");
3690 outs() << "\t optionalInstanceMethods "
3691 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
3692 outs() << "\t optionalClassMethods "
3693 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
3694 outs() << "\t instanceProperties "
3695 << format("0x%" PRIx64, pc.instanceProperties) << "\n";
3697 p += sizeof(uint64_t);
3698 offset += sizeof(uint64_t);
3702 static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
3703 struct protocol_list32_t pl;
3705 struct protocol32_t pc;
3707 uint32_t offset, xoffset, left, i;
3711 r = get_pointer_32(p, offset, left, S, info);
3714 memset(&pl, '\0', sizeof(struct protocol_list32_t));
3715 if (left < sizeof(struct protocol_list32_t)) {
3716 memcpy(&pl, r, left);
3717 outs() << " (protocol_list_t entends past the end of the section)\n";
3719 memcpy(&pl, r, sizeof(struct protocol_list32_t));
3720 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3722 outs() << " count " << pl.count << "\n";
3724 p += sizeof(struct protocol_list32_t);
3725 offset += sizeof(struct protocol_list32_t);
3726 for (i = 0; i < pl.count; i++) {
3727 r = get_pointer_32(p, offset, left, S, info);
3731 if (left < sizeof(uint32_t)) {
3732 memcpy(&q, r, left);
3733 outs() << " (protocol_t * entends past the end of the section)\n";
3735 memcpy(&q, r, sizeof(uint32_t));
3736 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3737 sys::swapByteOrder(q);
3738 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
3739 << " (struct protocol_t *)\n";
3740 r = get_pointer_32(q, offset, left, S, info);
3743 memset(&pc, '\0', sizeof(struct protocol32_t));
3744 if (left < sizeof(struct protocol32_t)) {
3745 memcpy(&pc, r, left);
3746 outs() << " (protocol_t entends past the end of the section)\n";
3748 memcpy(&pc, r, sizeof(struct protocol32_t));
3749 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3751 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
3752 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
3753 name = get_pointer_32(pc.name, xoffset, left, xS, info);
3754 if (name != nullptr)
3755 outs() << format(" %.*s", left, name);
3757 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
3758 outs() << "\t\t instanceMethods "
3759 << format("0x%" PRIx32, pc.instanceMethods)
3760 << " (struct method_list_t *)\n";
3761 if (pc.instanceMethods != 0)
3762 print_method_list32_t(pc.instanceMethods, info, "\t");
3763 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
3764 << " (struct method_list_t *)\n";
3765 if (pc.classMethods != 0)
3766 print_method_list32_t(pc.classMethods, info, "\t");
3767 outs() << "\t optionalInstanceMethods "
3768 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
3769 outs() << "\t optionalClassMethods "
3770 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
3771 outs() << "\t instanceProperties "
3772 << format("0x%" PRIx32, pc.instanceProperties) << "\n";
3773 p += sizeof(uint32_t);
3774 offset += sizeof(uint32_t);
3778 static void print_indent(uint32_t indent) {
3779 for (uint32_t i = 0; i < indent;) {
3780 if (indent - i >= 8) {
3784 for (uint32_t j = i; j < indent; j++)
3791 static bool print_method_description_list(uint32_t p, uint32_t indent,
3792 struct DisassembleInfo *info) {
3793 uint32_t offset, left, xleft;
3795 struct objc_method_description_list_t mdl;
3796 struct objc_method_description_t md;
3797 const char *r, *list, *name;
3800 r = get_pointer_32(p, offset, left, S, info, true);
3805 if (left > sizeof(struct objc_method_description_list_t)) {
3806 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
3808 print_indent(indent);
3809 outs() << " objc_method_description_list extends past end of the section\n";
3810 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
3811 memcpy(&mdl, r, left);
3813 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3816 print_indent(indent);
3817 outs() << " count " << mdl.count << "\n";
3819 list = r + sizeof(struct objc_method_description_list_t);
3820 for (i = 0; i < mdl.count; i++) {
3821 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
3822 print_indent(indent);
3823 outs() << " remaining list entries extend past the of the section\n";
3826 print_indent(indent);
3827 outs() << " list[" << i << "]\n";
3828 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
3829 sizeof(struct objc_method_description_t));
3830 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3833 print_indent(indent);
3834 outs() << " name " << format("0x%08" PRIx32, md.name);
3835 if (info->verbose) {
3836 name = get_pointer_32(md.name, offset, xleft, S, info, true);
3837 if (name != nullptr)
3838 outs() << format(" %.*s", xleft, name);
3840 outs() << " (not in an __OBJC section)";
3844 print_indent(indent);
3845 outs() << " types " << format("0x%08" PRIx32, md.types);
3846 if (info->verbose) {
3847 name = get_pointer_32(md.types, offset, xleft, S, info, true);
3848 if (name != nullptr)
3849 outs() << format(" %.*s", xleft, name);
3851 outs() << " (not in an __OBJC section)";
3858 static bool print_protocol_list(uint32_t p, uint32_t indent,
3859 struct DisassembleInfo *info);
3861 static bool print_protocol(uint32_t p, uint32_t indent,
3862 struct DisassembleInfo *info) {
3863 uint32_t offset, left;
3865 struct objc_protocol_t protocol;
3866 const char *r, *name;
3868 r = get_pointer_32(p, offset, left, S, info, true);
3873 if (left >= sizeof(struct objc_protocol_t)) {
3874 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
3876 print_indent(indent);
3877 outs() << " Protocol extends past end of the section\n";
3878 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
3879 memcpy(&protocol, r, left);
3881 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3882 swapStruct(protocol);
3884 print_indent(indent);
3885 outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
3888 print_indent(indent);
3889 outs() << " protocol_name "
3890 << format("0x%08" PRIx32, protocol.protocol_name);
3891 if (info->verbose) {
3892 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
3893 if (name != nullptr)
3894 outs() << format(" %.*s", left, name);
3896 outs() << " (not in an __OBJC section)";
3900 print_indent(indent);
3901 outs() << " protocol_list "
3902 << format("0x%08" PRIx32, protocol.protocol_list);
3903 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
3904 outs() << " (not in an __OBJC section)\n";
3906 print_indent(indent);
3907 outs() << " instance_methods "
3908 << format("0x%08" PRIx32, protocol.instance_methods);
3909 if (print_method_description_list(protocol.instance_methods, indent, info))
3910 outs() << " (not in an __OBJC section)\n";
3912 print_indent(indent);
3913 outs() << " class_methods "
3914 << format("0x%08" PRIx32, protocol.class_methods);
3915 if (print_method_description_list(protocol.class_methods, indent, info))
3916 outs() << " (not in an __OBJC section)\n";
3921 static bool print_protocol_list(uint32_t p, uint32_t indent,
3922 struct DisassembleInfo *info) {
3923 uint32_t offset, left, l;
3925 struct objc_protocol_list_t protocol_list;
3926 const char *r, *list;
3929 r = get_pointer_32(p, offset, left, S, info, true);
3934 if (left > sizeof(struct objc_protocol_list_t)) {
3935 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
3937 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
3938 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
3939 memcpy(&protocol_list, r, left);
3941 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3942 swapStruct(protocol_list);
3944 print_indent(indent);
3945 outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
3947 print_indent(indent);
3948 outs() << " count " << protocol_list.count << "\n";
3950 list = r + sizeof(struct objc_protocol_list_t);
3951 for (i = 0; i < protocol_list.count; i++) {
3952 if ((i + 1) * sizeof(uint32_t) > left) {
3953 outs() << "\t\t remaining list entries extend past the of the section\n";
3956 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
3957 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3958 sys::swapByteOrder(l);
3960 print_indent(indent);
3961 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
3962 if (print_protocol(l, indent, info))
3963 outs() << "(not in an __OBJC section)\n";
3968 static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
3969 struct ivar_list64_t il;
3972 uint32_t offset, xoffset, left, j;
3974 const char *name, *sym_name, *ivar_offset_p;
3975 uint64_t ivar_offset, n_value;
3977 r = get_pointer_64(p, offset, left, S, info);
3980 memset(&il, '\0', sizeof(struct ivar_list64_t));
3981 if (left < sizeof(struct ivar_list64_t)) {
3982 memcpy(&il, r, left);
3983 outs() << " (ivar_list_t entends past the end of the section)\n";
3985 memcpy(&il, r, sizeof(struct ivar_list64_t));
3986 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3988 outs() << " entsize " << il.entsize << "\n";
3989 outs() << " count " << il.count << "\n";
3991 p += sizeof(struct ivar_list64_t);
3992 offset += sizeof(struct ivar_list64_t);
3993 for (j = 0; j < il.count; j++) {
3994 r = get_pointer_64(p, offset, left, S, info);
3997 memset(&i, '\0', sizeof(struct ivar64_t));
3998 if (left < sizeof(struct ivar64_t)) {
3999 memcpy(&i, r, left);
4000 outs() << " (ivar_t entends past the end of the section)\n";
4002 memcpy(&i, r, sizeof(struct ivar64_t));
4003 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4006 outs() << "\t\t\t offset ";
4007 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
4008 info, n_value, i.offset);
4010 if (info->verbose && sym_name != nullptr)
4013 outs() << format("0x%" PRIx64, n_value);
4015 outs() << " + " << format("0x%" PRIx64, i.offset);
4017 outs() << format("0x%" PRIx64, i.offset);
4018 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
4019 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4020 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4021 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4022 sys::swapByteOrder(ivar_offset);
4023 outs() << " " << ivar_offset << "\n";
4027 outs() << "\t\t\t name ";
4028 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
4031 if (info->verbose && sym_name != nullptr)
4034 outs() << format("0x%" PRIx64, n_value);
4036 outs() << " + " << format("0x%" PRIx64, i.name);
4038 outs() << format("0x%" PRIx64, i.name);
4039 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
4040 if (name != nullptr)
4041 outs() << format(" %.*s", left, name);
4044 outs() << "\t\t\t type ";
4045 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
4047 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
4049 if (info->verbose && sym_name != nullptr)
4052 outs() << format("0x%" PRIx64, n_value);
4054 outs() << " + " << format("0x%" PRIx64, i.type);
4056 outs() << format("0x%" PRIx64, i.type);
4057 if (name != nullptr)
4058 outs() << format(" %.*s", left, name);
4061 outs() << "\t\t\talignment " << i.alignment << "\n";
4062 outs() << "\t\t\t size " << i.size << "\n";
4064 p += sizeof(struct ivar64_t);
4065 offset += sizeof(struct ivar64_t);
4069 static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
4070 struct ivar_list32_t il;
4073 uint32_t offset, xoffset, left, j;
4075 const char *name, *ivar_offset_p;
4076 uint32_t ivar_offset;
4078 r = get_pointer_32(p, offset, left, S, info);
4081 memset(&il, '\0', sizeof(struct ivar_list32_t));
4082 if (left < sizeof(struct ivar_list32_t)) {
4083 memcpy(&il, r, left);
4084 outs() << " (ivar_list_t entends past the end of the section)\n";
4086 memcpy(&il, r, sizeof(struct ivar_list32_t));
4087 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4089 outs() << " entsize " << il.entsize << "\n";
4090 outs() << " count " << il.count << "\n";
4092 p += sizeof(struct ivar_list32_t);
4093 offset += sizeof(struct ivar_list32_t);
4094 for (j = 0; j < il.count; j++) {
4095 r = get_pointer_32(p, offset, left, S, info);
4098 memset(&i, '\0', sizeof(struct ivar32_t));
4099 if (left < sizeof(struct ivar32_t)) {
4100 memcpy(&i, r, left);
4101 outs() << " (ivar_t entends past the end of the section)\n";
4103 memcpy(&i, r, sizeof(struct ivar32_t));
4104 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4107 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
4108 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4109 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4110 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4111 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4112 sys::swapByteOrder(ivar_offset);
4113 outs() << " " << ivar_offset << "\n";
4117 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
4118 name = get_pointer_32(i.name, xoffset, left, xS, info);
4119 if (name != nullptr)
4120 outs() << format(" %.*s", left, name);
4123 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
4124 name = get_pointer_32(i.type, xoffset, left, xS, info);
4125 if (name != nullptr)
4126 outs() << format(" %.*s", left, name);
4129 outs() << "\t\t\talignment " << i.alignment << "\n";
4130 outs() << "\t\t\t size " << i.size << "\n";
4132 p += sizeof(struct ivar32_t);
4133 offset += sizeof(struct ivar32_t);
4137 static void print_objc_property_list64(uint64_t p,
4138 struct DisassembleInfo *info) {
4139 struct objc_property_list64 opl;
4140 struct objc_property64 op;
4142 uint32_t offset, xoffset, left, j;
4144 const char *name, *sym_name;
4147 r = get_pointer_64(p, offset, left, S, info);
4150 memset(&opl, '\0', sizeof(struct objc_property_list64));
4151 if (left < sizeof(struct objc_property_list64)) {
4152 memcpy(&opl, r, left);
4153 outs() << " (objc_property_list entends past the end of the section)\n";
4155 memcpy(&opl, r, sizeof(struct objc_property_list64));
4156 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4158 outs() << " entsize " << opl.entsize << "\n";
4159 outs() << " count " << opl.count << "\n";
4161 p += sizeof(struct objc_property_list64);
4162 offset += sizeof(struct objc_property_list64);
4163 for (j = 0; j < opl.count; j++) {
4164 r = get_pointer_64(p, offset, left, S, info);
4167 memset(&op, '\0', sizeof(struct objc_property64));
4168 if (left < sizeof(struct objc_property64)) {
4169 memcpy(&op, r, left);
4170 outs() << " (objc_property entends past the end of the section)\n";
4172 memcpy(&op, r, sizeof(struct objc_property64));
4173 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4176 outs() << "\t\t\t name ";
4177 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
4178 info, n_value, op.name);
4180 if (info->verbose && sym_name != nullptr)
4183 outs() << format("0x%" PRIx64, n_value);
4185 outs() << " + " << format("0x%" PRIx64, op.name);
4187 outs() << format("0x%" PRIx64, op.name);
4188 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
4189 if (name != nullptr)
4190 outs() << format(" %.*s", left, name);
4193 outs() << "\t\t\tattributes ";
4195 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
4196 info, n_value, op.attributes);
4198 if (info->verbose && sym_name != nullptr)
4201 outs() << format("0x%" PRIx64, n_value);
4202 if (op.attributes != 0)
4203 outs() << " + " << format("0x%" PRIx64, op.attributes);
4205 outs() << format("0x%" PRIx64, op.attributes);
4206 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
4207 if (name != nullptr)
4208 outs() << format(" %.*s", left, name);
4211 p += sizeof(struct objc_property64);
4212 offset += sizeof(struct objc_property64);
4216 static void print_objc_property_list32(uint32_t p,
4217 struct DisassembleInfo *info) {
4218 struct objc_property_list32 opl;
4219 struct objc_property32 op;
4221 uint32_t offset, xoffset, left, j;
4225 r = get_pointer_32(p, offset, left, S, info);
4228 memset(&opl, '\0', sizeof(struct objc_property_list32));
4229 if (left < sizeof(struct objc_property_list32)) {
4230 memcpy(&opl, r, left);
4231 outs() << " (objc_property_list entends past the end of the section)\n";
4233 memcpy(&opl, r, sizeof(struct objc_property_list32));
4234 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4236 outs() << " entsize " << opl.entsize << "\n";
4237 outs() << " count " << opl.count << "\n";
4239 p += sizeof(struct objc_property_list32);
4240 offset += sizeof(struct objc_property_list32);
4241 for (j = 0; j < opl.count; j++) {
4242 r = get_pointer_32(p, offset, left, S, info);
4245 memset(&op, '\0', sizeof(struct objc_property32));
4246 if (left < sizeof(struct objc_property32)) {
4247 memcpy(&op, r, left);
4248 outs() << " (objc_property entends past the end of the section)\n";
4250 memcpy(&op, r, sizeof(struct objc_property32));
4251 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4254 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
4255 name = get_pointer_32(op.name, xoffset, left, xS, info);
4256 if (name != nullptr)
4257 outs() << format(" %.*s", left, name);
4260 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
4261 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
4262 if (name != nullptr)
4263 outs() << format(" %.*s", left, name);
4266 p += sizeof(struct objc_property32);
4267 offset += sizeof(struct objc_property32);
4271 static void print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
4272 bool &is_meta_class) {
4273 struct class_ro64_t cro;
4275 uint32_t offset, xoffset, left;
4277 const char *name, *sym_name;
4280 r = get_pointer_64(p, offset, left, S, info);
4281 if (r == nullptr || left < sizeof(struct class_ro64_t))
4283 memset(&cro, '\0', sizeof(struct class_ro64_t));
4284 if (left < sizeof(struct class_ro64_t)) {
4285 memcpy(&cro, r, left);
4286 outs() << " (class_ro_t entends past the end of the section)\n";
4288 memcpy(&cro, r, sizeof(struct class_ro64_t));
4289 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4291 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4292 if (cro.flags & RO_META)
4293 outs() << " RO_META";
4294 if (cro.flags & RO_ROOT)
4295 outs() << " RO_ROOT";
4296 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4297 outs() << " RO_HAS_CXX_STRUCTORS";
4299 outs() << " instanceStart " << cro.instanceStart << "\n";
4300 outs() << " instanceSize " << cro.instanceSize << "\n";
4301 outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
4303 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
4305 print_layout_map64(cro.ivarLayout, info);
4308 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
4309 info, n_value, cro.name);
4311 if (info->verbose && sym_name != nullptr)
4314 outs() << format("0x%" PRIx64, n_value);
4316 outs() << " + " << format("0x%" PRIx64, cro.name);
4318 outs() << format("0x%" PRIx64, cro.name);
4319 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
4320 if (name != nullptr)
4321 outs() << format(" %.*s", left, name);
4324 outs() << " baseMethods ";
4325 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
4326 S, info, n_value, cro.baseMethods);
4328 if (info->verbose && sym_name != nullptr)
4331 outs() << format("0x%" PRIx64, n_value);
4332 if (cro.baseMethods != 0)
4333 outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
4335 outs() << format("0x%" PRIx64, cro.baseMethods);
4336 outs() << " (struct method_list_t *)\n";
4337 if (cro.baseMethods + n_value != 0)
4338 print_method_list64_t(cro.baseMethods + n_value, info, "");
4340 outs() << " baseProtocols ";
4342 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
4343 info, n_value, cro.baseProtocols);
4345 if (info->verbose && sym_name != nullptr)
4348 outs() << format("0x%" PRIx64, n_value);
4349 if (cro.baseProtocols != 0)
4350 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
4352 outs() << format("0x%" PRIx64, cro.baseProtocols);
4354 if (cro.baseProtocols + n_value != 0)
4355 print_protocol_list64_t(cro.baseProtocols + n_value, info);
4357 outs() << " ivars ";
4358 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
4359 info, n_value, cro.ivars);
4361 if (info->verbose && sym_name != nullptr)
4364 outs() << format("0x%" PRIx64, n_value);
4366 outs() << " + " << format("0x%" PRIx64, cro.ivars);
4368 outs() << format("0x%" PRIx64, cro.ivars);
4370 if (cro.ivars + n_value != 0)
4371 print_ivar_list64_t(cro.ivars + n_value, info);
4373 outs() << " weakIvarLayout ";
4375 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
4376 info, n_value, cro.weakIvarLayout);
4378 if (info->verbose && sym_name != nullptr)
4381 outs() << format("0x%" PRIx64, n_value);
4382 if (cro.weakIvarLayout != 0)
4383 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
4385 outs() << format("0x%" PRIx64, cro.weakIvarLayout);
4387 print_layout_map64(cro.weakIvarLayout + n_value, info);
4389 outs() << " baseProperties ";
4391 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
4392 info, n_value, cro.baseProperties);
4394 if (info->verbose && sym_name != nullptr)
4397 outs() << format("0x%" PRIx64, n_value);
4398 if (cro.baseProperties != 0)
4399 outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
4401 outs() << format("0x%" PRIx64, cro.baseProperties);
4403 if (cro.baseProperties + n_value != 0)
4404 print_objc_property_list64(cro.baseProperties + n_value, info);
4406 is_meta_class = (cro.flags & RO_META) ? true : false;
4409 static void print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
4410 bool &is_meta_class) {
4411 struct class_ro32_t cro;
4413 uint32_t offset, xoffset, left;
4417 r = get_pointer_32(p, offset, left, S, info);
4420 memset(&cro, '\0', sizeof(struct class_ro32_t));
4421 if (left < sizeof(struct class_ro32_t)) {
4422 memcpy(&cro, r, left);
4423 outs() << " (class_ro_t entends past the end of the section)\n";
4425 memcpy(&cro, r, sizeof(struct class_ro32_t));
4426 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4428 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4429 if (cro.flags & RO_META)
4430 outs() << " RO_META";
4431 if (cro.flags & RO_ROOT)
4432 outs() << " RO_ROOT";
4433 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4434 outs() << " RO_HAS_CXX_STRUCTORS";
4436 outs() << " instanceStart " << cro.instanceStart << "\n";
4437 outs() << " instanceSize " << cro.instanceSize << "\n";
4438 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
4440 print_layout_map32(cro.ivarLayout, info);
4442 outs() << " name " << format("0x%" PRIx32, cro.name);
4443 name = get_pointer_32(cro.name, xoffset, left, xS, info);
4444 if (name != nullptr)
4445 outs() << format(" %.*s", left, name);
4448 outs() << " baseMethods "
4449 << format("0x%" PRIx32, cro.baseMethods)
4450 << " (struct method_list_t *)\n";
4451 if (cro.baseMethods != 0)
4452 print_method_list32_t(cro.baseMethods, info, "");
4454 outs() << " baseProtocols "
4455 << format("0x%" PRIx32, cro.baseProtocols) << "\n";
4456 if (cro.baseProtocols != 0)
4457 print_protocol_list32_t(cro.baseProtocols, info);
4458 outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
4461 print_ivar_list32_t(cro.ivars, info);
4462 outs() << " weakIvarLayout "
4463 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
4464 print_layout_map32(cro.weakIvarLayout, info);
4465 outs() << " baseProperties "
4466 << format("0x%" PRIx32, cro.baseProperties) << "\n";
4467 if (cro.baseProperties != 0)
4468 print_objc_property_list32(cro.baseProperties, info);
4469 is_meta_class = (cro.flags & RO_META) ? true : false;
4472 static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
4475 uint32_t offset, left;
4478 uint64_t isa_n_value, n_value;
4480 r = get_pointer_64(p, offset, left, S, info);
4481 if (r == nullptr || left < sizeof(struct class64_t))
4483 memset(&c, '\0', sizeof(struct class64_t));
4484 if (left < sizeof(struct class64_t)) {
4485 memcpy(&c, r, left);
4486 outs() << " (class_t entends past the end of the section)\n";
4488 memcpy(&c, r, sizeof(struct class64_t));
4489 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4492 outs() << " isa " << format("0x%" PRIx64, c.isa);
4493 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
4494 isa_n_value, c.isa);
4495 if (name != nullptr)
4496 outs() << " " << name;
4499 outs() << " superclass " << format("0x%" PRIx64, c.superclass);
4500 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
4501 n_value, c.superclass);
4502 if (name != nullptr)
4503 outs() << " " << name;
4506 outs() << " cache " << format("0x%" PRIx64, c.cache);
4507 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
4509 if (name != nullptr)
4510 outs() << " " << name;
4513 outs() << " vtable " << format("0x%" PRIx64, c.vtable);
4514 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
4516 if (name != nullptr)
4517 outs() << " " << name;
4520 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
4524 if (info->verbose && name != nullptr)
4527 outs() << format("0x%" PRIx64, n_value);
4529 outs() << " + " << format("0x%" PRIx64, c.data);
4531 outs() << format("0x%" PRIx64, c.data);
4532 outs() << " (struct class_ro_t *)";
4534 // This is a Swift class if some of the low bits of the pointer are set.
4535 if ((c.data + n_value) & 0x7)
4536 outs() << " Swift class";
4539 print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class);
4541 if (is_meta_class == false) {
4542 outs() << "Meta Class\n";
4543 print_class64_t(c.isa + isa_n_value, info);
4547 static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
4550 uint32_t offset, left;
4554 r = get_pointer_32(p, offset, left, S, info);
4557 memset(&c, '\0', sizeof(struct class32_t));
4558 if (left < sizeof(struct class32_t)) {
4559 memcpy(&c, r, left);
4560 outs() << " (class_t entends past the end of the section)\n";
4562 memcpy(&c, r, sizeof(struct class32_t));
4563 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4566 outs() << " isa " << format("0x%" PRIx32, c.isa);
4568 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
4569 if (name != nullptr)
4570 outs() << " " << name;
4573 outs() << " superclass " << format("0x%" PRIx32, c.superclass);
4574 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
4576 if (name != nullptr)
4577 outs() << " " << name;
4580 outs() << " cache " << format("0x%" PRIx32, c.cache);
4581 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
4583 if (name != nullptr)
4584 outs() << " " << name;
4587 outs() << " vtable " << format("0x%" PRIx32, c.vtable);
4588 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
4590 if (name != nullptr)
4591 outs() << " " << name;
4595 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
4596 outs() << " data " << format("0x%" PRIx32, c.data)
4597 << " (struct class_ro_t *)";
4599 // This is a Swift class if some of the low bits of the pointer are set.
4601 outs() << " Swift class";
4604 print_class_ro32_t(c.data & ~0x3, info, is_meta_class);
4606 if (is_meta_class == false) {
4607 outs() << "Meta Class\n";
4608 print_class32_t(c.isa, info);
4612 static void print_objc_class_t(struct objc_class_t *objc_class,
4613 struct DisassembleInfo *info) {
4614 uint32_t offset, left, xleft;
4615 const char *name, *p, *ivar_list;
4618 struct objc_ivar_list_t objc_ivar_list;
4619 struct objc_ivar_t ivar;
4621 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
4622 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
4623 name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
4624 if (name != nullptr)
4625 outs() << format(" %.*s", left, name);
4627 outs() << " (not in an __OBJC section)";
4631 outs() << "\t super_class "
4632 << format("0x%08" PRIx32, objc_class->super_class);
4633 if (info->verbose) {
4634 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
4635 if (name != nullptr)
4636 outs() << format(" %.*s", left, name);
4638 outs() << " (not in an __OBJC section)";
4642 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name);
4643 if (info->verbose) {
4644 name = get_pointer_32(objc_class->name, offset, left, S, info, true);
4645 if (name != nullptr)
4646 outs() << format(" %.*s", left, name);
4648 outs() << " (not in an __OBJC section)";
4652 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
4655 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
4656 if (info->verbose) {
4657 if (CLS_GETINFO(objc_class, CLS_CLASS))
4658 outs() << " CLS_CLASS";
4659 else if (CLS_GETINFO(objc_class, CLS_META))
4660 outs() << " CLS_META";
4664 outs() << "\t instance_size "
4665 << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
4667 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
4668 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
4670 if (left > sizeof(struct objc_ivar_list_t)) {
4672 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
4674 outs() << " (entends past the end of the section)\n";
4675 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
4676 memcpy(&objc_ivar_list, p, left);
4678 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4679 swapStruct(objc_ivar_list);
4680 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
4681 ivar_list = p + sizeof(struct objc_ivar_list_t);
4682 for (i = 0; i < objc_ivar_list.ivar_count; i++) {
4683 if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
4684 outs() << "\t\t remaining ivar's extend past the of the section\n";
4687 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
4688 sizeof(struct objc_ivar_t));
4689 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4692 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
4693 if (info->verbose) {
4694 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
4695 if (name != nullptr)
4696 outs() << format(" %.*s", xleft, name);
4698 outs() << " (not in an __OBJC section)";
4702 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
4703 if (info->verbose) {
4704 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
4705 if (name != nullptr)
4706 outs() << format(" %.*s", xleft, name);
4708 outs() << " (not in an __OBJC section)";
4712 outs() << "\t\t ivar_offset "
4713 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
4716 outs() << " (not in an __OBJC section)\n";
4719 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
4720 if (print_method_list(objc_class->methodLists, info))
4721 outs() << " (not in an __OBJC section)\n";
4723 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
4726 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
4727 if (print_protocol_list(objc_class->protocols, 16, info))
4728 outs() << " (not in an __OBJC section)\n";
4731 static void print_objc_objc_category_t(struct objc_category_t *objc_category,
4732 struct DisassembleInfo *info) {
4733 uint32_t offset, left;
4737 outs() << "\t category name "
4738 << format("0x%08" PRIx32, objc_category->category_name);
4739 if (info->verbose) {
4740 name = get_pointer_32(objc_category->category_name, offset, left, S, info,
4742 if (name != nullptr)
4743 outs() << format(" %.*s", left, name);
4745 outs() << " (not in an __OBJC section)";
4749 outs() << "\t\t class name "
4750 << format("0x%08" PRIx32, objc_category->class_name);
4751 if (info->verbose) {
4753 get_pointer_32(objc_category->class_name, offset, left, S, info, true);
4754 if (name != nullptr)
4755 outs() << format(" %.*s", left, name);
4757 outs() << " (not in an __OBJC section)";
4761 outs() << "\t instance methods "
4762 << format("0x%08" PRIx32, objc_category->instance_methods);
4763 if (print_method_list(objc_category->instance_methods, info))
4764 outs() << " (not in an __OBJC section)\n";
4766 outs() << "\t class methods "
4767 << format("0x%08" PRIx32, objc_category->class_methods);
4768 if (print_method_list(objc_category->class_methods, info))
4769 outs() << " (not in an __OBJC section)\n";
4772 static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
4773 struct category64_t c;
4775 uint32_t offset, xoffset, left;
4777 const char *name, *sym_name;
4780 r = get_pointer_64(p, offset, left, S, info);
4783 memset(&c, '\0', sizeof(struct category64_t));
4784 if (left < sizeof(struct category64_t)) {
4785 memcpy(&c, r, left);
4786 outs() << " (category_t entends past the end of the section)\n";
4788 memcpy(&c, r, sizeof(struct category64_t));
4789 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4793 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
4794 info, n_value, c.name);
4796 if (info->verbose && sym_name != nullptr)
4799 outs() << format("0x%" PRIx64, n_value);
4801 outs() << " + " << format("0x%" PRIx64, c.name);
4803 outs() << format("0x%" PRIx64, c.name);
4804 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
4805 if (name != nullptr)
4806 outs() << format(" %.*s", left, name);
4810 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
4813 if (info->verbose && sym_name != nullptr)
4816 outs() << format("0x%" PRIx64, n_value);
4818 outs() << " + " << format("0x%" PRIx64, c.cls);
4820 outs() << format("0x%" PRIx64, c.cls);
4822 if (c.cls + n_value != 0)
4823 print_class64_t(c.cls + n_value, info);
4825 outs() << " instanceMethods ";
4827 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
4828 info, n_value, c.instanceMethods);
4830 if (info->verbose && sym_name != nullptr)
4833 outs() << format("0x%" PRIx64, n_value);
4834 if (c.instanceMethods != 0)
4835 outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
4837 outs() << format("0x%" PRIx64, c.instanceMethods);
4839 if (c.instanceMethods + n_value != 0)
4840 print_method_list64_t(c.instanceMethods + n_value, info, "");
4842 outs() << " classMethods ";
4843 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
4844 S, info, n_value, c.classMethods);
4846 if (info->verbose && sym_name != nullptr)
4849 outs() << format("0x%" PRIx64, n_value);
4850 if (c.classMethods != 0)
4851 outs() << " + " << format("0x%" PRIx64, c.classMethods);
4853 outs() << format("0x%" PRIx64, c.classMethods);
4855 if (c.classMethods + n_value != 0)
4856 print_method_list64_t(c.classMethods + n_value, info, "");
4858 outs() << " protocols ";
4859 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
4860 info, n_value, c.protocols);
4862 if (info->verbose && sym_name != nullptr)
4865 outs() << format("0x%" PRIx64, n_value);
4866 if (c.protocols != 0)
4867 outs() << " + " << format("0x%" PRIx64, c.protocols);
4869 outs() << format("0x%" PRIx64, c.protocols);
4871 if (c.protocols + n_value != 0)
4872 print_protocol_list64_t(c.protocols + n_value, info);
4874 outs() << "instanceProperties ";
4876 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
4877 S, info, n_value, c.instanceProperties);
4879 if (info->verbose && sym_name != nullptr)
4882 outs() << format("0x%" PRIx64, n_value);
4883 if (c.instanceProperties != 0)
4884 outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
4886 outs() << format("0x%" PRIx64, c.instanceProperties);
4888 if (c.instanceProperties + n_value != 0)
4889 print_objc_property_list64(c.instanceProperties + n_value, info);
4892 static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
4893 struct category32_t c;
4895 uint32_t offset, left;
4899 r = get_pointer_32(p, offset, left, S, info);
4902 memset(&c, '\0', sizeof(struct category32_t));
4903 if (left < sizeof(struct category32_t)) {
4904 memcpy(&c, r, left);
4905 outs() << " (category_t entends past the end of the section)\n";
4907 memcpy(&c, r, sizeof(struct category32_t));
4908 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4911 outs() << " name " << format("0x%" PRIx32, c.name);
4912 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
4915 outs() << " " << name;
4918 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
4920 print_class32_t(c.cls, info);
4921 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
4923 if (c.instanceMethods != 0)
4924 print_method_list32_t(c.instanceMethods, info, "");
4925 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
4927 if (c.classMethods != 0)
4928 print_method_list32_t(c.classMethods, info, "");
4929 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
4930 if (c.protocols != 0)
4931 print_protocol_list32_t(c.protocols, info);
4932 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
4934 if (c.instanceProperties != 0)
4935 print_objc_property_list32(c.instanceProperties, info);
4938 static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
4939 uint32_t i, left, offset, xoffset;
4940 uint64_t p, n_value;
4941 struct message_ref64 mr;
4942 const char *name, *sym_name;
4946 if (S == SectionRef())
4950 S.getName(SectName);
4951 DataRefImpl Ref = S.getRawDataRefImpl();
4952 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4953 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4955 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
4956 p = S.getAddress() + i;
4957 r = get_pointer_64(p, offset, left, S, info);
4960 memset(&mr, '\0', sizeof(struct message_ref64));
4961 if (left < sizeof(struct message_ref64)) {
4962 memcpy(&mr, r, left);
4963 outs() << " (message_ref entends past the end of the section)\n";
4965 memcpy(&mr, r, sizeof(struct message_ref64));
4966 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4970 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
4973 outs() << format("0x%" PRIx64, n_value) << " ";
4975 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
4977 outs() << format("0x%" PRIx64, mr.imp) << " ";
4978 if (name != nullptr)
4979 outs() << " " << name;
4983 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
4984 info, n_value, mr.sel);
4986 if (info->verbose && sym_name != nullptr)
4989 outs() << format("0x%" PRIx64, n_value);
4991 outs() << " + " << format("0x%" PRIx64, mr.sel);
4993 outs() << format("0x%" PRIx64, mr.sel);
4994 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
4995 if (name != nullptr)
4996 outs() << format(" %.*s", left, name);
4999 offset += sizeof(struct message_ref64);
5003 static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
5004 uint32_t i, left, offset, xoffset, p;
5005 struct message_ref32 mr;
5006 const char *name, *r;
5009 if (S == SectionRef())
5013 S.getName(SectName);
5014 DataRefImpl Ref = S.getRawDataRefImpl();
5015 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5016 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5018 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5019 p = S.getAddress() + i;
5020 r = get_pointer_32(p, offset, left, S, info);
5023 memset(&mr, '\0', sizeof(struct message_ref32));
5024 if (left < sizeof(struct message_ref32)) {
5025 memcpy(&mr, r, left);
5026 outs() << " (message_ref entends past the end of the section)\n";
5028 memcpy(&mr, r, sizeof(struct message_ref32));
5029 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5032 outs() << " imp " << format("0x%" PRIx32, mr.imp);
5033 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
5035 if (name != nullptr)
5036 outs() << " " << name;
5039 outs() << " sel " << format("0x%" PRIx32, mr.sel);
5040 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
5041 if (name != nullptr)
5042 outs() << " " << name;
5045 offset += sizeof(struct message_ref32);
5049 static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
5050 uint32_t left, offset, swift_version;
5052 struct objc_image_info64 o;
5056 S.getName(SectName);
5057 DataRefImpl Ref = S.getRawDataRefImpl();
5058 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5059 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5061 r = get_pointer_64(p, offset, left, S, info);
5064 memset(&o, '\0', sizeof(struct objc_image_info64));
5065 if (left < sizeof(struct objc_image_info64)) {
5066 memcpy(&o, r, left);
5067 outs() << " (objc_image_info entends past the end of the section)\n";
5069 memcpy(&o, r, sizeof(struct objc_image_info64));
5070 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5072 outs() << " version " << o.version << "\n";
5073 outs() << " flags " << format("0x%" PRIx32, o.flags);
5074 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5075 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5076 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5077 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5078 swift_version = (o.flags >> 8) & 0xff;
5079 if (swift_version != 0) {
5080 if (swift_version == 1)
5081 outs() << " Swift 1.0";
5082 else if (swift_version == 2)
5083 outs() << " Swift 1.1";
5085 outs() << " unknown future Swift version (" << swift_version << ")";
5090 static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5091 uint32_t left, offset, swift_version, p;
5092 struct objc_image_info32 o;
5096 S.getName(SectName);
5097 DataRefImpl Ref = S.getRawDataRefImpl();
5098 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5099 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5101 r = get_pointer_32(p, offset, left, S, info);
5104 memset(&o, '\0', sizeof(struct objc_image_info32));
5105 if (left < sizeof(struct objc_image_info32)) {
5106 memcpy(&o, r, left);
5107 outs() << " (objc_image_info entends past the end of the section)\n";
5109 memcpy(&o, r, sizeof(struct objc_image_info32));
5110 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5112 outs() << " version " << o.version << "\n";
5113 outs() << " flags " << format("0x%" PRIx32, o.flags);
5114 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5115 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5116 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5117 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5118 swift_version = (o.flags >> 8) & 0xff;
5119 if (swift_version != 0) {
5120 if (swift_version == 1)
5121 outs() << " Swift 1.0";
5122 else if (swift_version == 2)
5123 outs() << " Swift 1.1";
5125 outs() << " unknown future Swift version (" << swift_version << ")";
5130 static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5131 uint32_t left, offset, p;
5132 struct imageInfo_t o;
5136 S.getName(SectName);
5137 DataRefImpl Ref = S.getRawDataRefImpl();
5138 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5139 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5141 r = get_pointer_32(p, offset, left, S, info);
5144 memset(&o, '\0', sizeof(struct imageInfo_t));
5145 if (left < sizeof(struct imageInfo_t)) {
5146 memcpy(&o, r, left);
5147 outs() << " (imageInfo entends past the end of the section)\n";
5149 memcpy(&o, r, sizeof(struct imageInfo_t));
5150 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5152 outs() << " version " << o.version << "\n";
5153 outs() << " flags " << format("0x%" PRIx32, o.flags);
5159 outs() << " GC-only";
5165 static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
5166 SymbolAddressMap AddrMap;
5168 CreateSymbolAddressMap(O, &AddrMap);
5170 std::vector<SectionRef> Sections;
5171 for (const SectionRef &Section : O->sections()) {
5173 Section.getName(SectName);
5174 Sections.push_back(Section);
5177 struct DisassembleInfo info;
5178 // Set up the block of info used by the Symbolizer call backs.
5179 info.verbose = verbose;
5181 info.AddrMap = &AddrMap;
5182 info.Sections = &Sections;
5183 info.class_name = nullptr;
5184 info.selector_name = nullptr;
5185 info.method = nullptr;
5186 info.demangled_name = nullptr;
5187 info.bindtable = nullptr;
5191 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5192 if (CL != SectionRef()) {
5194 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5196 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5198 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5201 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5202 if (CR != SectionRef()) {
5204 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5206 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5208 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5211 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5212 if (SR != SectionRef()) {
5214 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5216 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5218 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5221 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5222 if (CA != SectionRef()) {
5224 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5226 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5228 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5231 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5232 if (PL != SectionRef()) {
5234 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5236 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5238 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5241 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5242 if (MR != SectionRef()) {
5244 print_message_refs64(MR, &info);
5246 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5248 print_message_refs64(MR, &info);
5251 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5252 if (II != SectionRef()) {
5254 print_image_info64(II, &info);
5256 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5258 print_image_info64(II, &info);
5261 if (info.bindtable != nullptr)
5262 delete info.bindtable;
5265 static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
5266 SymbolAddressMap AddrMap;
5268 CreateSymbolAddressMap(O, &AddrMap);
5270 std::vector<SectionRef> Sections;
5271 for (const SectionRef &Section : O->sections()) {
5273 Section.getName(SectName);
5274 Sections.push_back(Section);
5277 struct DisassembleInfo info;
5278 // Set up the block of info used by the Symbolizer call backs.
5279 info.verbose = verbose;
5281 info.AddrMap = &AddrMap;
5282 info.Sections = &Sections;
5283 info.class_name = nullptr;
5284 info.selector_name = nullptr;
5285 info.method = nullptr;
5286 info.demangled_name = nullptr;
5287 info.bindtable = nullptr;
5291 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5292 if (CL != SectionRef()) {
5294 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5296 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5298 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5301 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5302 if (CR != SectionRef()) {
5304 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5306 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5308 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5311 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5312 if (SR != SectionRef()) {
5314 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5316 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5318 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5321 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5322 if (CA != SectionRef()) {
5324 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5326 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5328 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5331 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5332 if (PL != SectionRef()) {
5334 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5336 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5338 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5341 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5342 if (MR != SectionRef()) {
5344 print_message_refs32(MR, &info);
5346 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5348 print_message_refs32(MR, &info);
5351 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5352 if (II != SectionRef()) {
5354 print_image_info32(II, &info);
5356 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5358 print_image_info32(II, &info);
5362 static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
5363 uint32_t i, j, p, offset, xoffset, left, defs_left, def;
5364 const char *r, *name, *defs;
5365 struct objc_module_t module;
5367 struct objc_symtab_t symtab;
5368 struct objc_class_t objc_class;
5369 struct objc_category_t objc_category;
5371 outs() << "Objective-C segment\n";
5372 S = get_section(O, "__OBJC", "__module_info");
5373 if (S == SectionRef())
5376 SymbolAddressMap AddrMap;
5378 CreateSymbolAddressMap(O, &AddrMap);
5380 std::vector<SectionRef> Sections;
5381 for (const SectionRef &Section : O->sections()) {
5383 Section.getName(SectName);
5384 Sections.push_back(Section);
5387 struct DisassembleInfo info;
5388 // Set up the block of info used by the Symbolizer call backs.
5389 info.verbose = verbose;
5391 info.AddrMap = &AddrMap;
5392 info.Sections = &Sections;
5393 info.class_name = nullptr;
5394 info.selector_name = nullptr;
5395 info.method = nullptr;
5396 info.demangled_name = nullptr;
5397 info.bindtable = nullptr;
5401 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
5402 p = S.getAddress() + i;
5403 r = get_pointer_32(p, offset, left, S, &info, true);
5406 memset(&module, '\0', sizeof(struct objc_module_t));
5407 if (left < sizeof(struct objc_module_t)) {
5408 memcpy(&module, r, left);
5409 outs() << " (module extends past end of __module_info section)\n";
5411 memcpy(&module, r, sizeof(struct objc_module_t));
5412 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5415 outs() << "Module " << format("0x%" PRIx32, p) << "\n";
5416 outs() << " version " << module.version << "\n";
5417 outs() << " size " << module.size << "\n";
5419 name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
5420 if (name != nullptr)
5421 outs() << format("%.*s", left, name);
5423 outs() << format("0x%08" PRIx32, module.name)
5424 << "(not in an __OBJC section)";
5427 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
5428 if (module.symtab == 0 || r == nullptr) {
5429 outs() << " symtab " << format("0x%08" PRIx32, module.symtab)
5430 << " (not in an __OBJC section)\n";
5433 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
5434 memset(&symtab, '\0', sizeof(struct objc_symtab_t));
5437 if (left < sizeof(struct objc_symtab_t)) {
5438 memcpy(&symtab, r, left);
5439 outs() << "\tsymtab extends past end of an __OBJC section)\n";
5441 memcpy(&symtab, r, sizeof(struct objc_symtab_t));
5442 if (left > sizeof(struct objc_symtab_t)) {
5443 defs_left = left - sizeof(struct objc_symtab_t);
5444 defs = r + sizeof(struct objc_symtab_t);
5447 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5450 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
5451 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
5452 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
5454 outs() << " (not in an __OBJC section)";
5456 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
5457 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
5458 if (symtab.cls_def_cnt > 0)
5459 outs() << "\tClass Definitions\n";
5460 for (j = 0; j < symtab.cls_def_cnt; j++) {
5461 if ((j + 1) * sizeof(uint32_t) > defs_left) {
5462 outs() << "\t(remaining class defs entries entends past the end of the "
5466 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
5467 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5468 sys::swapByteOrder(def);
5470 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5471 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
5473 if (left > sizeof(struct objc_class_t)) {
5475 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5477 outs() << " (entends past the end of the section)\n";
5478 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5479 memcpy(&objc_class, r, left);
5481 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5482 swapStruct(objc_class);
5483 print_objc_class_t(&objc_class, &info);
5485 outs() << "(not in an __OBJC section)\n";
5488 if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
5489 outs() << "\tMeta Class";
5490 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
5492 if (left > sizeof(struct objc_class_t)) {
5494 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5496 outs() << " (entends past the end of the section)\n";
5497 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5498 memcpy(&objc_class, r, left);
5500 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5501 swapStruct(objc_class);
5502 print_objc_class_t(&objc_class, &info);
5504 outs() << "(not in an __OBJC section)\n";
5508 if (symtab.cat_def_cnt > 0)
5509 outs() << "\tCategory Definitions\n";
5510 for (j = 0; j < symtab.cat_def_cnt; j++) {
5511 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
5512 outs() << "\t(remaining category defs entries entends past the end of "
5513 << "the section)\n";
5516 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
5518 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5519 sys::swapByteOrder(def);
5521 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5522 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
5523 << format("0x%08" PRIx32, def);
5525 if (left > sizeof(struct objc_category_t)) {
5527 memcpy(&objc_category, r, sizeof(struct objc_category_t));
5529 outs() << " (entends past the end of the section)\n";
5530 memset(&objc_category, '\0', sizeof(struct objc_category_t));
5531 memcpy(&objc_category, r, left);
5533 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5534 swapStruct(objc_category);
5535 print_objc_objc_category_t(&objc_category, &info);
5537 outs() << "(not in an __OBJC section)\n";
5541 const SectionRef II = get_section(O, "__OBJC", "__image_info");
5542 if (II != SectionRef())
5543 print_image_info(II, &info);
5548 static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
5550 printObjc2_64bit_MetaData(O, verbose);
5552 MachO::mach_header H;
5554 if (H.cputype == MachO::CPU_TYPE_ARM)
5555 printObjc2_32bit_MetaData(O, verbose);
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);
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.
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 .
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.
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 .
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;
5595 MachO::any_relocation_info RE;
5596 bool isExtern = false;
5598 for (const RelocationRef &Reloc : info->S.relocations()) {
5599 uint64_t RelocOffset;
5600 Reloc.getOffset(RelocOffset);
5601 if (RelocOffset == sect_offset) {
5602 Rel = Reloc.getRawDataRefImpl();
5603 RE = info->O->getRelocation(Rel);
5604 if (info->O->isRelocationScattered(RE))
5606 isExtern = info->O->getPlainRelocationExternal(RE);
5608 symbol_iterator RelocSym = Reloc.getSymbol();
5615 // If there is an external relocation entry for a symbol in a section
5616 // then used that symbol's value for the value of the reference.
5617 if (reloc_found && isExtern) {
5618 if (info->O->getAnyRelocationPCRel(RE)) {
5619 unsigned Type = info->O->getAnyRelocationType(RE);
5620 if (Type == MachO::X86_64_RELOC_SIGNED) {
5621 Symbol.getAddress(ReferenceValue);
5626 // Look for literals such as Objective-C CFStrings refs, Selector refs,
5627 // Message refs and Class refs.
5628 bool classref, selref, msgref, cfstring;
5629 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
5630 selref, msgref, cfstring);
5631 if (classref && pointer_value == 0) {
5632 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
5633 // And the pointer_value in that section is typically zero as it will be
5634 // set by dyld as part of the "bind information".
5635 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
5636 if (name != nullptr) {
5637 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
5638 const char *class_name = strrchr(name, '$');
5639 if (class_name != nullptr && class_name[1] == '_' &&
5640 class_name[2] != '\0') {
5641 info->class_name = class_name + 2;
5648 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
5650 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
5651 if (name != nullptr)
5652 info->class_name = name;
5654 name = "bad class ref";
5659 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
5660 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
5664 if (selref && pointer_value == 0)
5665 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
5667 if (pointer_value != 0)
5668 ReferenceValue = pointer_value;
5670 const char *name = GuessCstringPointer(ReferenceValue, info);
5672 if (pointer_value != 0 && selref) {
5673 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
5674 info->selector_name = name;
5675 } else if (pointer_value != 0 && msgref) {
5676 info->class_name = nullptr;
5677 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
5678 info->selector_name = name;
5680 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
5684 // Lastly look for an indirect symbol with this ReferenceValue which is in
5685 // a literal pool. If found return that symbol name.
5686 name = GuessIndirectSymbol(ReferenceValue, info);
5688 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
5695 // SymbolizerSymbolLookUp is the symbol lookup function passed when creating
5696 // the Symbolizer. It looks up the ReferenceValue using the info passed via the
5697 // pointer to the struct DisassembleInfo that was passed when MCSymbolizer
5698 // is created and returns the symbol name that matches the ReferenceValue or
5699 // nullptr if none. The ReferenceType is passed in for the IN type of
5700 // reference the instruction is making from the values in defined in the header
5701 // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
5702 // Out type and the ReferenceName will also be set which is added as a comment
5703 // to the disassembled instruction.
5706 // If the symbol name is a C++ mangled name then the demangled name is
5707 // returned through ReferenceName and ReferenceType is set to
5708 // LLVMDisassembler_ReferenceType_DeMangled_Name .
5711 // When this is called to get a symbol name for a branch target then the
5712 // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
5713 // SymbolValue will be looked for in the indirect symbol table to determine if
5714 // it is an address for a symbol stub. If so then the symbol name for that
5715 // stub is returned indirectly through ReferenceName and then ReferenceType is
5716 // set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
5718 // When this is called with an value loaded via a PC relative load then
5719 // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
5720 // SymbolValue is checked to be an address of literal pointer, symbol pointer,
5721 // or an Objective-C meta data reference. If so the output ReferenceType is
5722 // set to correspond to that as well as setting the ReferenceName.
5723 static const char *SymbolizerSymbolLookUp(void *DisInfo,
5724 uint64_t ReferenceValue,
5725 uint64_t *ReferenceType,
5726 uint64_t ReferencePC,
5727 const char **ReferenceName) {
5728 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
5729 // If no verbose symbolic information is wanted then just return nullptr.
5730 if (!info->verbose) {
5731 *ReferenceName = nullptr;
5732 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5736 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
5738 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
5739 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
5740 if (*ReferenceName != nullptr) {
5741 method_reference(info, ReferenceType, ReferenceName);
5742 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
5743 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
5746 if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
5747 if (info->demangled_name != nullptr)
5748 free(info->demangled_name);
5750 info->demangled_name =
5751 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
5752 if (info->demangled_name != nullptr) {
5753 *ReferenceName = info->demangled_name;
5754 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5756 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5759 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5760 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
5762 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5764 method_reference(info, ReferenceType, ReferenceName);
5766 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5767 // If this is arm64 and the reference is an adrp instruction save the
5768 // instruction, passed in ReferenceValue and the address of the instruction
5769 // for use later if we see and add immediate instruction.
5770 } else if (info->O->getArch() == Triple::aarch64 &&
5771 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
5772 info->adrp_inst = ReferenceValue;
5773 info->adrp_addr = ReferencePC;
5774 SymbolName = nullptr;
5775 *ReferenceName = nullptr;
5776 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5777 // If this is arm64 and reference is an add immediate instruction and we
5779 // seen an adrp instruction just before it and the adrp's Xd register
5781 // this add's Xn register reconstruct the value being referenced and look to
5782 // see if it is a literal pointer. Note the add immediate instruction is
5783 // passed in ReferenceValue.
5784 } else if (info->O->getArch() == Triple::aarch64 &&
5785 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
5786 ReferencePC - 4 == info->adrp_addr &&
5787 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5788 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5789 uint32_t addxri_inst;
5790 uint64_t adrp_imm, addxri_imm;
5793 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5794 if (info->adrp_inst & 0x0200000)
5795 adrp_imm |= 0xfffffffffc000000LL;
5797 addxri_inst = ReferenceValue;
5798 addxri_imm = (addxri_inst >> 10) & 0xfff;
5799 if (((addxri_inst >> 22) & 0x3) == 1)
5802 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5803 (adrp_imm << 12) + addxri_imm;
5806 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5807 if (*ReferenceName == nullptr)
5808 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5809 // If this is arm64 and the reference is a load register instruction and we
5810 // have seen an adrp instruction just before it and the adrp's Xd register
5811 // matches this add's Xn register reconstruct the value being referenced and
5812 // look to see if it is a literal pointer. Note the load register
5813 // instruction is passed in ReferenceValue.
5814 } else if (info->O->getArch() == Triple::aarch64 &&
5815 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
5816 ReferencePC - 4 == info->adrp_addr &&
5817 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5818 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5819 uint32_t ldrxui_inst;
5820 uint64_t adrp_imm, ldrxui_imm;
5823 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5824 if (info->adrp_inst & 0x0200000)
5825 adrp_imm |= 0xfffffffffc000000LL;
5827 ldrxui_inst = ReferenceValue;
5828 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
5830 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5831 (adrp_imm << 12) + (ldrxui_imm << 3);
5834 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5835 if (*ReferenceName == nullptr)
5836 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5838 // If this arm64 and is an load register (PC-relative) instruction the
5839 // ReferenceValue is the PC plus the immediate value.
5840 else if (info->O->getArch() == Triple::aarch64 &&
5841 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
5842 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
5844 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5845 if (*ReferenceName == nullptr)
5846 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5849 else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
5850 if (info->demangled_name != nullptr)
5851 free(info->demangled_name);
5853 info->demangled_name =
5854 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
5855 if (info->demangled_name != nullptr) {
5856 *ReferenceName = info->demangled_name;
5857 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5862 *ReferenceName = nullptr;
5863 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5869 /// \brief Emits the comments that are stored in the CommentStream.
5870 /// Each comment in the CommentStream must end with a newline.
5871 static void emitComments(raw_svector_ostream &CommentStream,
5872 SmallString<128> &CommentsToEmit,
5873 formatted_raw_ostream &FormattedOS,
5874 const MCAsmInfo &MAI) {
5875 // Flush the stream before taking its content.
5876 CommentStream.flush();
5877 StringRef Comments = CommentsToEmit.str();
5878 // Get the default information for printing a comment.
5879 const char *CommentBegin = MAI.getCommentString();
5880 unsigned CommentColumn = MAI.getCommentColumn();
5881 bool IsFirst = true;
5882 while (!Comments.empty()) {
5884 FormattedOS << '\n';
5885 // Emit a line of comments.
5886 FormattedOS.PadToColumn(CommentColumn);
5887 size_t Position = Comments.find('\n');
5888 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
5889 // Move after the newline character.
5890 Comments = Comments.substr(Position + 1);
5893 FormattedOS.flush();
5895 // Tell the comment stream that the vector changed underneath it.
5896 CommentsToEmit.clear();
5897 CommentStream.resync();
5900 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
5901 StringRef DisSegName, StringRef DisSectName) {
5902 const char *McpuDefault = nullptr;
5903 const Target *ThumbTarget = nullptr;
5904 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
5906 // GetTarget prints out stuff.
5909 if (MCPU.empty() && McpuDefault)
5912 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
5913 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
5915 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
5917 // Package up features to be passed to target/subtarget
5918 std::string FeaturesStr;
5919 if (MAttrs.size()) {
5920 SubtargetFeatures Features;
5921 for (unsigned i = 0; i != MAttrs.size(); ++i)
5922 Features.AddFeature(MAttrs[i]);
5923 FeaturesStr = Features.getString();
5926 // Set up disassembler.
5927 std::unique_ptr<const MCRegisterInfo> MRI(
5928 TheTarget->createMCRegInfo(TripleName));
5929 std::unique_ptr<const MCAsmInfo> AsmInfo(
5930 TheTarget->createMCAsmInfo(*MRI, TripleName));
5931 std::unique_ptr<const MCSubtargetInfo> STI(
5932 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
5933 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
5934 std::unique_ptr<MCDisassembler> DisAsm(
5935 TheTarget->createMCDisassembler(*STI, Ctx));
5936 std::unique_ptr<MCSymbolizer> Symbolizer;
5937 struct DisassembleInfo SymbolizerInfo;
5938 std::unique_ptr<MCRelocationInfo> RelInfo(
5939 TheTarget->createMCRelocationInfo(TripleName, Ctx));
5941 Symbolizer.reset(TheTarget->createMCSymbolizer(
5942 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
5943 &SymbolizerInfo, &Ctx, std::move(RelInfo)));
5944 DisAsm->setSymbolizer(std::move(Symbolizer));
5946 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
5947 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
5948 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
5949 // Set the display preference for hex vs. decimal immediates.
5950 IP->setPrintImmHex(PrintImmHex);
5951 // Comment stream and backing vector.
5952 SmallString<128> CommentsToEmit;
5953 raw_svector_ostream CommentStream(CommentsToEmit);
5954 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
5955 // if it is done then arm64 comments for string literals don't get printed
5956 // and some constant get printed instead and not setting it causes intel
5957 // (32-bit and 64-bit) comments printed with different spacing before the
5958 // comment causing different diffs with the 'C' disassembler library API.
5959 // IP->setCommentStream(CommentStream);
5961 if (!AsmInfo || !STI || !DisAsm || !IP) {
5962 errs() << "error: couldn't initialize disassembler for target "
5963 << TripleName << '\n';
5967 // Set up thumb disassembler.
5968 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
5969 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
5970 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
5971 std::unique_ptr<MCDisassembler> ThumbDisAsm;
5972 std::unique_ptr<MCInstPrinter> ThumbIP;
5973 std::unique_ptr<MCContext> ThumbCtx;
5974 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
5975 struct DisassembleInfo ThumbSymbolizerInfo;
5976 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
5978 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
5980 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
5982 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr));
5983 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
5984 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
5985 MCContext *PtrThumbCtx = ThumbCtx.get();
5987 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
5989 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
5990 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
5991 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
5992 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
5994 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
5995 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
5996 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
5997 *ThumbInstrInfo, *ThumbMRI));
5998 // Set the display preference for hex vs. decimal immediates.
5999 ThumbIP->setPrintImmHex(PrintImmHex);
6002 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
6003 errs() << "error: couldn't initialize disassembler for target "
6004 << ThumbTripleName << '\n';
6008 MachO::mach_header Header = MachOOF->getHeader();
6010 // FIXME: Using the -cfg command line option, this code used to be able to
6011 // annotate relocations with the referenced symbol's name, and if this was
6012 // inside a __[cf]string section, the data it points to. This is now replaced
6013 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
6014 std::vector<SectionRef> Sections;
6015 std::vector<SymbolRef> Symbols;
6016 SmallVector<uint64_t, 8> FoundFns;
6017 uint64_t BaseSegmentAddress;
6019 getSectionsAndSymbols(Header, MachOOF, Sections, Symbols, FoundFns,
6020 BaseSegmentAddress);
6022 // Sort the symbols by address, just in case they didn't come in that way.
6023 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
6025 // Build a data in code table that is sorted on by the address of each entry.
6026 uint64_t BaseAddress = 0;
6027 if (Header.filetype == MachO::MH_OBJECT)
6028 BaseAddress = Sections[0].getAddress();
6030 BaseAddress = BaseSegmentAddress;
6032 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
6035 DI->getOffset(Offset);
6036 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
6038 array_pod_sort(Dices.begin(), Dices.end());
6041 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
6043 raw_ostream &DebugOut = nulls();
6046 std::unique_ptr<DIContext> diContext;
6047 ObjectFile *DbgObj = MachOOF;
6048 // Try to find debug info and set up the DIContext for it.
6050 // A separate DSym file path was specified, parse it as a macho file,
6051 // get the sections and supply it to the section name parsing machinery.
6052 if (!DSYMFile.empty()) {
6053 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
6054 MemoryBuffer::getFileOrSTDIN(DSYMFile);
6055 if (std::error_code EC = BufOrErr.getError()) {
6056 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
6060 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
6065 // Setup the DIContext
6066 diContext.reset(DIContext::getDWARFContext(*DbgObj));
6069 if (DumpSections.size() == 0)
6070 outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
6072 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
6074 if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
6077 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
6079 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
6080 if (SegmentName != DisSegName)
6084 Sections[SectIdx].getContents(BytesStr);
6085 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
6087 uint64_t SectAddress = Sections[SectIdx].getAddress();
6089 bool symbolTableWorked = false;
6091 // Parse relocations.
6092 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
6093 for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) {
6094 uint64_t RelocOffset;
6095 Reloc.getOffset(RelocOffset);
6096 uint64_t SectionAddress = Sections[SectIdx].getAddress();
6097 RelocOffset -= SectionAddress;
6099 symbol_iterator RelocSym = Reloc.getSymbol();
6101 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
6103 array_pod_sort(Relocs.begin(), Relocs.end());
6105 // Create a map of symbol addresses to symbol names for use by
6106 // the SymbolizerSymbolLookUp() routine.
6107 SymbolAddressMap AddrMap;
6108 bool DisSymNameFound = false;
6109 for (const SymbolRef &Symbol : MachOOF->symbols()) {
6112 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
6113 ST == SymbolRef::ST_Other) {
6115 Symbol.getAddress(Address);
6117 Symbol.getName(SymName);
6118 AddrMap[Address] = SymName;
6119 if (!DisSymName.empty() && DisSymName == SymName)
6120 DisSymNameFound = true;
6123 if (!DisSymName.empty() && !DisSymNameFound) {
6124 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
6127 // Set up the block of info used by the Symbolizer call backs.
6128 SymbolizerInfo.verbose = !NoSymbolicOperands;
6129 SymbolizerInfo.O = MachOOF;
6130 SymbolizerInfo.S = Sections[SectIdx];
6131 SymbolizerInfo.AddrMap = &AddrMap;
6132 SymbolizerInfo.Sections = &Sections;
6133 SymbolizerInfo.class_name = nullptr;
6134 SymbolizerInfo.selector_name = nullptr;
6135 SymbolizerInfo.method = nullptr;
6136 SymbolizerInfo.demangled_name = nullptr;
6137 SymbolizerInfo.bindtable = nullptr;
6138 SymbolizerInfo.adrp_addr = 0;
6139 SymbolizerInfo.adrp_inst = 0;
6140 // Same for the ThumbSymbolizer
6141 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
6142 ThumbSymbolizerInfo.O = MachOOF;
6143 ThumbSymbolizerInfo.S = Sections[SectIdx];
6144 ThumbSymbolizerInfo.AddrMap = &AddrMap;
6145 ThumbSymbolizerInfo.Sections = &Sections;
6146 ThumbSymbolizerInfo.class_name = nullptr;
6147 ThumbSymbolizerInfo.selector_name = nullptr;
6148 ThumbSymbolizerInfo.method = nullptr;
6149 ThumbSymbolizerInfo.demangled_name = nullptr;
6150 ThumbSymbolizerInfo.bindtable = nullptr;
6151 ThumbSymbolizerInfo.adrp_addr = 0;
6152 ThumbSymbolizerInfo.adrp_inst = 0;
6154 // Disassemble symbol by symbol.
6155 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
6157 Symbols[SymIdx].getName(SymName);
6160 Symbols[SymIdx].getType(ST);
6161 if (ST != SymbolRef::ST_Function)
6164 // Make sure the symbol is defined in this section.
6165 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
6169 // If we are only disassembling one symbol see if this is that symbol.
6170 if (!DisSymName.empty() && DisSymName != SymName)
6173 // Start at the address of the symbol relative to the section's address.
6175 uint64_t SectionAddress = Sections[SectIdx].getAddress();
6176 Symbols[SymIdx].getAddress(Start);
6177 Start -= SectionAddress;
6179 // Stop disassembling either at the beginning of the next symbol or at
6180 // the end of the section.
6181 bool containsNextSym = false;
6182 uint64_t NextSym = 0;
6183 uint64_t NextSymIdx = SymIdx + 1;
6184 while (Symbols.size() > NextSymIdx) {
6185 SymbolRef::Type NextSymType;
6186 Symbols[NextSymIdx].getType(NextSymType);
6187 if (NextSymType == SymbolRef::ST_Function) {
6189 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
6190 Symbols[NextSymIdx].getAddress(NextSym);
6191 NextSym -= SectionAddress;
6197 uint64_t SectSize = Sections[SectIdx].getSize();
6198 uint64_t End = containsNextSym ? NextSym : SectSize;
6201 symbolTableWorked = true;
6203 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
6205 (MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget;
6207 outs() << SymName << ":\n";
6208 DILineInfo lastLine;
6209 for (uint64_t Index = Start; Index < End; Index += Size) {
6212 uint64_t PC = SectAddress + Index;
6213 if (!NoLeadingAddr) {
6214 if (FullLeadingAddr) {
6215 if (MachOOF->is64Bit())
6216 outs() << format("%016" PRIx64, PC);
6218 outs() << format("%08" PRIx64, PC);
6220 outs() << format("%8" PRIx64 ":", PC);
6226 // Check the data in code table here to see if this is data not an
6227 // instruction to be disassembled.
6229 Dice.push_back(std::make_pair(PC, DiceRef()));
6230 dice_table_iterator DTI =
6231 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
6232 compareDiceTableEntries);
6233 if (DTI != Dices.end()) {
6235 DTI->second.getLength(Length);
6237 DTI->second.getKind(Kind);
6238 Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
6239 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
6240 (PC == (DTI->first + Length - 1)) && (Length & 1))
6245 SmallVector<char, 64> AnnotationsBytes;
6246 raw_svector_ostream Annotations(AnnotationsBytes);
6250 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
6251 PC, DebugOut, Annotations);
6253 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
6254 DebugOut, Annotations);
6256 if (!NoShowRawInsn) {
6257 DumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, Size));
6259 formatted_raw_ostream FormattedOS(outs());
6260 Annotations.flush();
6261 StringRef AnnotationsStr = Annotations.str();
6263 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
6265 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
6266 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
6268 // Print debug info.
6270 DILineInfo dli = diContext->getLineInfoForAddress(PC);
6271 // Print valid line info if it changed.
6272 if (dli != lastLine && dli.Line != 0)
6273 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
6279 unsigned int Arch = MachOOF->getArch();
6280 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
6281 outs() << format("\t.byte 0x%02x #bad opcode\n",
6282 *(Bytes.data() + Index) & 0xff);
6283 Size = 1; // skip exactly one illegible byte and move on.
6284 } else if (Arch == Triple::aarch64) {
6285 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6286 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
6287 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
6288 (*(Bytes.data() + Index + 3) & 0xff) << 24;
6289 outs() << format("\t.long\t0x%08x\n", opcode);
6292 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6294 Size = 1; // skip illegible bytes
6299 if (!symbolTableWorked) {
6300 // Reading the symbol table didn't work, disassemble the whole section.
6301 uint64_t SectAddress = Sections[SectIdx].getAddress();
6302 uint64_t SectSize = Sections[SectIdx].getSize();
6304 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
6307 uint64_t PC = SectAddress + Index;
6308 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
6309 DebugOut, nulls())) {
6310 if (!NoLeadingAddr) {
6311 if (FullLeadingAddr) {
6312 if (MachOOF->is64Bit())
6313 outs() << format("%016" PRIx64, PC);
6315 outs() << format("%08" PRIx64, PC);
6317 outs() << format("%8" PRIx64 ":", PC);
6320 if (!NoShowRawInsn) {
6322 DumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, InstSize));
6324 IP->printInst(&Inst, outs(), "", *STI);
6327 unsigned int Arch = MachOOF->getArch();
6328 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
6329 outs() << format("\t.byte 0x%02x #bad opcode\n",
6330 *(Bytes.data() + Index) & 0xff);
6331 InstSize = 1; // skip exactly one illegible byte and move on.
6333 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6335 InstSize = 1; // skip illegible bytes
6340 // The TripleName's need to be reset if we are called again for a different
6343 ThumbTripleName = "";
6345 if (SymbolizerInfo.method != nullptr)
6346 free(SymbolizerInfo.method);
6347 if (SymbolizerInfo.demangled_name != nullptr)
6348 free(SymbolizerInfo.demangled_name);
6349 if (SymbolizerInfo.bindtable != nullptr)
6350 delete SymbolizerInfo.bindtable;
6351 if (ThumbSymbolizerInfo.method != nullptr)
6352 free(ThumbSymbolizerInfo.method);
6353 if (ThumbSymbolizerInfo.demangled_name != nullptr)
6354 free(ThumbSymbolizerInfo.demangled_name);
6355 if (ThumbSymbolizerInfo.bindtable != nullptr)
6356 delete ThumbSymbolizerInfo.bindtable;
6360 //===----------------------------------------------------------------------===//
6361 // __compact_unwind section dumping
6362 //===----------------------------------------------------------------------===//
6366 template <typename T> static uint64_t readNext(const char *&Buf) {
6367 using llvm::support::little;
6368 using llvm::support::unaligned;
6370 uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
6375 struct CompactUnwindEntry {
6376 uint32_t OffsetInSection;
6378 uint64_t FunctionAddr;
6380 uint32_t CompactEncoding;
6381 uint64_t PersonalityAddr;
6384 RelocationRef FunctionReloc;
6385 RelocationRef PersonalityReloc;
6386 RelocationRef LSDAReloc;
6388 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
6389 : OffsetInSection(Offset) {
6391 read<uint64_t>(Contents.data() + Offset);
6393 read<uint32_t>(Contents.data() + Offset);
6397 template <typename UIntPtr> void read(const char *Buf) {
6398 FunctionAddr = readNext<UIntPtr>(Buf);
6399 Length = readNext<uint32_t>(Buf);
6400 CompactEncoding = readNext<uint32_t>(Buf);
6401 PersonalityAddr = readNext<UIntPtr>(Buf);
6402 LSDAAddr = readNext<UIntPtr>(Buf);
6407 /// Given a relocation from __compact_unwind, consisting of the RelocationRef
6408 /// and data being relocated, determine the best base Name and Addend to use for
6409 /// display purposes.
6411 /// 1. An Extern relocation will directly reference a symbol (and the data is
6412 /// then already an addend), so use that.
6413 /// 2. Otherwise the data is an offset in the object file's layout; try to find
6414 // a symbol before it in the same section, and use the offset from there.
6415 /// 3. Finally, if all that fails, fall back to an offset from the start of the
6416 /// referenced section.
6417 static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
6418 std::map<uint64_t, SymbolRef> &Symbols,
6419 const RelocationRef &Reloc, uint64_t Addr,
6420 StringRef &Name, uint64_t &Addend) {
6421 if (Reloc.getSymbol() != Obj->symbol_end()) {
6422 Reloc.getSymbol()->getName(Name);
6427 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
6428 SectionRef RelocSection = Obj->getRelocationSection(RE);
6430 uint64_t SectionAddr = RelocSection.getAddress();
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;
6441 // Go back one so that SymbolAddress <= Addr.
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 Sym->second.getName(Name);
6449 Addend = Addr - Sym->first;
6453 // There is a symbol before this reference, but it's in a different
6454 // section. Probably not helpful to mention it, so use the section name.
6455 RelocSection.getName(Name);
6456 Addend = Addr - SectionAddr;
6459 static void printUnwindRelocDest(const MachOObjectFile *Obj,
6460 std::map<uint64_t, SymbolRef> &Symbols,
6461 const RelocationRef &Reloc, uint64_t Addr) {
6465 if (!Reloc.getObjectFile())
6468 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
6472 outs() << " + " << format("0x%" PRIx64, Addend);
6476 printMachOCompactUnwindSection(const MachOObjectFile *Obj,
6477 std::map<uint64_t, SymbolRef> &Symbols,
6478 const SectionRef &CompactUnwind) {
6480 assert(Obj->isLittleEndian() &&
6481 "There should not be a big-endian .o with __compact_unwind");
6483 bool Is64 = Obj->is64Bit();
6484 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
6485 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
6488 CompactUnwind.getContents(Contents);
6490 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
6492 // First populate the initial raw offsets, encodings and so on from the entry.
6493 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
6494 CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
6495 CompactUnwinds.push_back(Entry);
6498 // Next we need to look at the relocations to find out what objects are
6499 // actually being referred to.
6500 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
6501 uint64_t RelocAddress;
6502 Reloc.getOffset(RelocAddress);
6504 uint32_t EntryIdx = RelocAddress / EntrySize;
6505 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
6506 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
6508 if (OffsetInEntry == 0)
6509 Entry.FunctionReloc = Reloc;
6510 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
6511 Entry.PersonalityReloc = Reloc;
6512 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
6513 Entry.LSDAReloc = Reloc;
6515 llvm_unreachable("Unexpected relocation in __compact_unwind section");
6518 // Finally, we're ready to print the data we've gathered.
6519 outs() << "Contents of __compact_unwind section:\n";
6520 for (auto &Entry : CompactUnwinds) {
6521 outs() << " Entry at offset "
6522 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
6524 // 1. Start of the region this entry applies to.
6525 outs() << " start: " << format("0x%" PRIx64,
6526 Entry.FunctionAddr) << ' ';
6527 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
6530 // 2. Length of the region this entry applies to.
6531 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
6533 // 3. The 32-bit compact encoding.
6534 outs() << " compact encoding: "
6535 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
6537 // 4. The personality function, if present.
6538 if (Entry.PersonalityReloc.getObjectFile()) {
6539 outs() << " personality function: "
6540 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
6541 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
6542 Entry.PersonalityAddr);
6546 // 5. This entry's language-specific data area.
6547 if (Entry.LSDAReloc.getObjectFile()) {
6548 outs() << " LSDA: " << format("0x%" PRIx64,
6549 Entry.LSDAAddr) << ' ';
6550 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
6556 //===----------------------------------------------------------------------===//
6557 // __unwind_info section dumping
6558 //===----------------------------------------------------------------------===//
6560 static void printRegularSecondLevelUnwindPage(const char *PageStart) {
6561 const char *Pos = PageStart;
6562 uint32_t Kind = readNext<uint32_t>(Pos);
6564 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
6566 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6567 uint16_t NumEntries = readNext<uint16_t>(Pos);
6569 Pos = PageStart + EntriesStart;
6570 for (unsigned i = 0; i < NumEntries; ++i) {
6571 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6572 uint32_t Encoding = readNext<uint32_t>(Pos);
6574 outs() << " [" << i << "]: "
6575 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6577 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
6581 static void printCompressedSecondLevelUnwindPage(
6582 const char *PageStart, uint32_t FunctionBase,
6583 const SmallVectorImpl<uint32_t> &CommonEncodings) {
6584 const char *Pos = PageStart;
6585 uint32_t Kind = readNext<uint32_t>(Pos);
6587 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
6589 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6590 uint16_t NumEntries = readNext<uint16_t>(Pos);
6592 uint16_t EncodingsStart = readNext<uint16_t>(Pos);
6593 readNext<uint16_t>(Pos);
6594 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
6595 PageStart + EncodingsStart);
6597 Pos = PageStart + EntriesStart;
6598 for (unsigned i = 0; i < NumEntries; ++i) {
6599 uint32_t Entry = readNext<uint32_t>(Pos);
6600 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
6601 uint32_t EncodingIdx = Entry >> 24;
6604 if (EncodingIdx < CommonEncodings.size())
6605 Encoding = CommonEncodings[EncodingIdx];
6607 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
6609 outs() << " [" << i << "]: "
6610 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6612 << "encoding[" << EncodingIdx
6613 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
6617 static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
6618 std::map<uint64_t, SymbolRef> &Symbols,
6619 const SectionRef &UnwindInfo) {
6621 assert(Obj->isLittleEndian() &&
6622 "There should not be a big-endian .o with __unwind_info");
6624 outs() << "Contents of __unwind_info section:\n";
6627 UnwindInfo.getContents(Contents);
6628 const char *Pos = Contents.data();
6630 //===----------------------------------
6632 //===----------------------------------
6634 uint32_t Version = readNext<uint32_t>(Pos);
6635 outs() << " Version: "
6636 << format("0x%" PRIx32, Version) << '\n';
6637 assert(Version == 1 && "only understand version 1");
6639 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
6640 outs() << " Common encodings array section offset: "
6641 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
6642 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
6643 outs() << " Number of common encodings in array: "
6644 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
6646 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
6647 outs() << " Personality function array section offset: "
6648 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
6649 uint32_t NumPersonalities = readNext<uint32_t>(Pos);
6650 outs() << " Number of personality functions in array: "
6651 << format("0x%" PRIx32, NumPersonalities) << '\n';
6653 uint32_t IndicesStart = readNext<uint32_t>(Pos);
6654 outs() << " Index array section offset: "
6655 << format("0x%" PRIx32, IndicesStart) << '\n';
6656 uint32_t NumIndices = readNext<uint32_t>(Pos);
6657 outs() << " Number of indices in array: "
6658 << format("0x%" PRIx32, NumIndices) << '\n';
6660 //===----------------------------------
6661 // A shared list of common encodings
6662 //===----------------------------------
6664 // These occupy indices in the range [0, N] whenever an encoding is referenced
6665 // from a compressed 2nd level index table. In practice the linker only
6666 // creates ~128 of these, so that indices are available to embed encodings in
6667 // the 2nd level index.
6669 SmallVector<uint32_t, 64> CommonEncodings;
6670 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
6671 Pos = Contents.data() + CommonEncodingsStart;
6672 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
6673 uint32_t Encoding = readNext<uint32_t>(Pos);
6674 CommonEncodings.push_back(Encoding);
6676 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
6680 //===----------------------------------
6681 // Personality functions used in this executable
6682 //===----------------------------------
6684 // There should be only a handful of these (one per source language,
6685 // roughly). Particularly since they only get 2 bits in the compact encoding.
6687 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
6688 Pos = Contents.data() + PersonalitiesStart;
6689 for (unsigned i = 0; i < NumPersonalities; ++i) {
6690 uint32_t PersonalityFn = readNext<uint32_t>(Pos);
6691 outs() << " personality[" << i + 1
6692 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
6695 //===----------------------------------
6696 // The level 1 index entries
6697 //===----------------------------------
6699 // These specify an approximate place to start searching for the more detailed
6700 // information, sorted by PC.
6703 uint32_t FunctionOffset;
6704 uint32_t SecondLevelPageStart;
6708 SmallVector<IndexEntry, 4> IndexEntries;
6710 outs() << " Top level indices: (count = " << NumIndices << ")\n";
6711 Pos = Contents.data() + IndicesStart;
6712 for (unsigned i = 0; i < NumIndices; ++i) {
6715 Entry.FunctionOffset = readNext<uint32_t>(Pos);
6716 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
6717 Entry.LSDAStart = readNext<uint32_t>(Pos);
6718 IndexEntries.push_back(Entry);
6720 outs() << " [" << i << "]: "
6721 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
6723 << "2nd level page offset="
6724 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
6725 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
6728 //===----------------------------------
6729 // Next come the LSDA tables
6730 //===----------------------------------
6732 // The LSDA layout is rather implicit: it's a contiguous array of entries from
6733 // the first top-level index's LSDAOffset to the last (sentinel).
6735 outs() << " LSDA descriptors:\n";
6736 Pos = Contents.data() + IndexEntries[0].LSDAStart;
6737 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
6738 (2 * sizeof(uint32_t));
6739 for (int i = 0; i < NumLSDAs; ++i) {
6740 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6741 uint32_t LSDAOffset = readNext<uint32_t>(Pos);
6742 outs() << " [" << i << "]: "
6743 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6745 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
6748 //===----------------------------------
6749 // Finally, the 2nd level indices
6750 //===----------------------------------
6752 // Generally these are 4K in size, and have 2 possible forms:
6753 // + Regular stores up to 511 entries with disparate encodings
6754 // + Compressed stores up to 1021 entries if few enough compact encoding
6756 outs() << " Second level indices:\n";
6757 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
6758 // The final sentinel top-level index has no associated 2nd level page
6759 if (IndexEntries[i].SecondLevelPageStart == 0)
6762 outs() << " Second level index[" << i << "]: "
6763 << "offset in section="
6764 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
6766 << "base function offset="
6767 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
6769 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
6770 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
6772 printRegularSecondLevelUnwindPage(Pos);
6774 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
6777 llvm_unreachable("Do not know how to print this kind of 2nd level page");
6781 void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
6782 std::map<uint64_t, SymbolRef> Symbols;
6783 for (const SymbolRef &SymRef : Obj->symbols()) {
6784 // Discard any undefined or absolute symbols. They're not going to take part
6785 // in the convenience lookup for unwind info and just take up resources.
6786 section_iterator Section = Obj->section_end();
6787 SymRef.getSection(Section);
6788 if (Section == Obj->section_end())
6792 SymRef.getAddress(Addr);
6793 Symbols.insert(std::make_pair(Addr, SymRef));
6796 for (const SectionRef &Section : Obj->sections()) {
6798 Section.getName(SectName);
6799 if (SectName == "__compact_unwind")
6800 printMachOCompactUnwindSection(Obj, Symbols, Section);
6801 else if (SectName == "__unwind_info")
6802 printMachOUnwindInfoSection(Obj, Symbols, Section);
6803 else if (SectName == "__eh_frame")
6804 outs() << "llvm-objdump: warning: unhandled __eh_frame section\n";
6808 static void PrintMachHeader(uint32_t magic, uint32_t cputype,
6809 uint32_t cpusubtype, uint32_t filetype,
6810 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
6812 outs() << "Mach header\n";
6813 outs() << " magic cputype cpusubtype caps filetype ncmds "
6814 "sizeofcmds flags\n";
6816 if (magic == MachO::MH_MAGIC)
6817 outs() << " MH_MAGIC";
6818 else if (magic == MachO::MH_MAGIC_64)
6819 outs() << "MH_MAGIC_64";
6821 outs() << format(" 0x%08" PRIx32, magic);
6823 case MachO::CPU_TYPE_I386:
6825 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6826 case MachO::CPU_SUBTYPE_I386_ALL:
6830 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6834 case MachO::CPU_TYPE_X86_64:
6835 outs() << " X86_64";
6836 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6837 case MachO::CPU_SUBTYPE_X86_64_ALL:
6840 case MachO::CPU_SUBTYPE_X86_64_H:
6841 outs() << " Haswell";
6844 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6848 case MachO::CPU_TYPE_ARM:
6850 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6851 case MachO::CPU_SUBTYPE_ARM_ALL:
6854 case MachO::CPU_SUBTYPE_ARM_V4T:
6857 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
6860 case MachO::CPU_SUBTYPE_ARM_XSCALE:
6861 outs() << " XSCALE";
6863 case MachO::CPU_SUBTYPE_ARM_V6:
6866 case MachO::CPU_SUBTYPE_ARM_V6M:
6869 case MachO::CPU_SUBTYPE_ARM_V7:
6872 case MachO::CPU_SUBTYPE_ARM_V7EM:
6875 case MachO::CPU_SUBTYPE_ARM_V7K:
6878 case MachO::CPU_SUBTYPE_ARM_V7M:
6881 case MachO::CPU_SUBTYPE_ARM_V7S:
6885 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6889 case MachO::CPU_TYPE_ARM64:
6891 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6892 case MachO::CPU_SUBTYPE_ARM64_ALL:
6896 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6900 case MachO::CPU_TYPE_POWERPC:
6902 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6903 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6907 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6911 case MachO::CPU_TYPE_POWERPC64:
6913 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6914 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6918 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6923 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
6926 outs() << format(" 0x%02" PRIx32,
6927 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
6930 case MachO::MH_OBJECT:
6931 outs() << " OBJECT";
6933 case MachO::MH_EXECUTE:
6934 outs() << " EXECUTE";
6936 case MachO::MH_FVMLIB:
6937 outs() << " FVMLIB";
6939 case MachO::MH_CORE:
6942 case MachO::MH_PRELOAD:
6943 outs() << " PRELOAD";
6945 case MachO::MH_DYLIB:
6948 case MachO::MH_DYLIB_STUB:
6949 outs() << " DYLIB_STUB";
6951 case MachO::MH_DYLINKER:
6952 outs() << " DYLINKER";
6954 case MachO::MH_BUNDLE:
6955 outs() << " BUNDLE";
6957 case MachO::MH_DSYM:
6960 case MachO::MH_KEXT_BUNDLE:
6961 outs() << " KEXTBUNDLE";
6964 outs() << format(" %10u", filetype);
6967 outs() << format(" %5u", ncmds);
6968 outs() << format(" %10u", sizeofcmds);
6970 if (f & MachO::MH_NOUNDEFS) {
6971 outs() << " NOUNDEFS";
6972 f &= ~MachO::MH_NOUNDEFS;
6974 if (f & MachO::MH_INCRLINK) {
6975 outs() << " INCRLINK";
6976 f &= ~MachO::MH_INCRLINK;
6978 if (f & MachO::MH_DYLDLINK) {
6979 outs() << " DYLDLINK";
6980 f &= ~MachO::MH_DYLDLINK;
6982 if (f & MachO::MH_BINDATLOAD) {
6983 outs() << " BINDATLOAD";
6984 f &= ~MachO::MH_BINDATLOAD;
6986 if (f & MachO::MH_PREBOUND) {
6987 outs() << " PREBOUND";
6988 f &= ~MachO::MH_PREBOUND;
6990 if (f & MachO::MH_SPLIT_SEGS) {
6991 outs() << " SPLIT_SEGS";
6992 f &= ~MachO::MH_SPLIT_SEGS;
6994 if (f & MachO::MH_LAZY_INIT) {
6995 outs() << " LAZY_INIT";
6996 f &= ~MachO::MH_LAZY_INIT;
6998 if (f & MachO::MH_TWOLEVEL) {
6999 outs() << " TWOLEVEL";
7000 f &= ~MachO::MH_TWOLEVEL;
7002 if (f & MachO::MH_FORCE_FLAT) {
7003 outs() << " FORCE_FLAT";
7004 f &= ~MachO::MH_FORCE_FLAT;
7006 if (f & MachO::MH_NOMULTIDEFS) {
7007 outs() << " NOMULTIDEFS";
7008 f &= ~MachO::MH_NOMULTIDEFS;
7010 if (f & MachO::MH_NOFIXPREBINDING) {
7011 outs() << " NOFIXPREBINDING";
7012 f &= ~MachO::MH_NOFIXPREBINDING;
7014 if (f & MachO::MH_PREBINDABLE) {
7015 outs() << " PREBINDABLE";
7016 f &= ~MachO::MH_PREBINDABLE;
7018 if (f & MachO::MH_ALLMODSBOUND) {
7019 outs() << " ALLMODSBOUND";
7020 f &= ~MachO::MH_ALLMODSBOUND;
7022 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
7023 outs() << " SUBSECTIONS_VIA_SYMBOLS";
7024 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
7026 if (f & MachO::MH_CANONICAL) {
7027 outs() << " CANONICAL";
7028 f &= ~MachO::MH_CANONICAL;
7030 if (f & MachO::MH_WEAK_DEFINES) {
7031 outs() << " WEAK_DEFINES";
7032 f &= ~MachO::MH_WEAK_DEFINES;
7034 if (f & MachO::MH_BINDS_TO_WEAK) {
7035 outs() << " BINDS_TO_WEAK";
7036 f &= ~MachO::MH_BINDS_TO_WEAK;
7038 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
7039 outs() << " ALLOW_STACK_EXECUTION";
7040 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
7042 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
7043 outs() << " DEAD_STRIPPABLE_DYLIB";
7044 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
7046 if (f & MachO::MH_PIE) {
7048 f &= ~MachO::MH_PIE;
7050 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
7051 outs() << " NO_REEXPORTED_DYLIBS";
7052 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
7054 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
7055 outs() << " MH_HAS_TLV_DESCRIPTORS";
7056 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
7058 if (f & MachO::MH_NO_HEAP_EXECUTION) {
7059 outs() << " MH_NO_HEAP_EXECUTION";
7060 f &= ~MachO::MH_NO_HEAP_EXECUTION;
7062 if (f & MachO::MH_APP_EXTENSION_SAFE) {
7063 outs() << " APP_EXTENSION_SAFE";
7064 f &= ~MachO::MH_APP_EXTENSION_SAFE;
7066 if (f != 0 || flags == 0)
7067 outs() << format(" 0x%08" PRIx32, f);
7069 outs() << format(" 0x%08" PRIx32, magic);
7070 outs() << format(" %7d", cputype);
7071 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7072 outs() << format(" 0x%02" PRIx32,
7073 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7074 outs() << format(" %10u", filetype);
7075 outs() << format(" %5u", ncmds);
7076 outs() << format(" %10u", sizeofcmds);
7077 outs() << format(" 0x%08" PRIx32, flags);
7082 static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
7083 StringRef SegName, uint64_t vmaddr,
7084 uint64_t vmsize, uint64_t fileoff,
7085 uint64_t filesize, uint32_t maxprot,
7086 uint32_t initprot, uint32_t nsects,
7087 uint32_t flags, uint32_t object_size,
7089 uint64_t expected_cmdsize;
7090 if (cmd == MachO::LC_SEGMENT) {
7091 outs() << " cmd LC_SEGMENT\n";
7092 expected_cmdsize = nsects;
7093 expected_cmdsize *= sizeof(struct MachO::section);
7094 expected_cmdsize += sizeof(struct MachO::segment_command);
7096 outs() << " cmd LC_SEGMENT_64\n";
7097 expected_cmdsize = nsects;
7098 expected_cmdsize *= sizeof(struct MachO::section_64);
7099 expected_cmdsize += sizeof(struct MachO::segment_command_64);
7101 outs() << " cmdsize " << cmdsize;
7102 if (cmdsize != expected_cmdsize)
7103 outs() << " Inconsistent size\n";
7106 outs() << " segname " << SegName << "\n";
7107 if (cmd == MachO::LC_SEGMENT_64) {
7108 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
7109 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
7111 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
7112 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
7114 outs() << " fileoff " << fileoff;
7115 if (fileoff > object_size)
7116 outs() << " (past end of file)\n";
7119 outs() << " filesize " << filesize;
7120 if (fileoff + filesize > object_size)
7121 outs() << " (past end of file)\n";
7126 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7127 MachO::VM_PROT_EXECUTE)) != 0)
7128 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
7130 if (maxprot & MachO::VM_PROT_READ)
7131 outs() << " maxprot r";
7133 outs() << " maxprot -";
7134 if (maxprot & MachO::VM_PROT_WRITE)
7138 if (maxprot & MachO::VM_PROT_EXECUTE)
7144 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7145 MachO::VM_PROT_EXECUTE)) != 0)
7146 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
7148 if (initprot & MachO::VM_PROT_READ)
7149 outs() << " initprot r";
7151 outs() << " initprot -";
7152 if (initprot & MachO::VM_PROT_WRITE)
7156 if (initprot & MachO::VM_PROT_EXECUTE)
7162 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
7163 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
7165 outs() << " nsects " << nsects << "\n";
7169 outs() << " (none)\n";
7171 if (flags & MachO::SG_HIGHVM) {
7172 outs() << " HIGHVM";
7173 flags &= ~MachO::SG_HIGHVM;
7175 if (flags & MachO::SG_FVMLIB) {
7176 outs() << " FVMLIB";
7177 flags &= ~MachO::SG_FVMLIB;
7179 if (flags & MachO::SG_NORELOC) {
7180 outs() << " NORELOC";
7181 flags &= ~MachO::SG_NORELOC;
7183 if (flags & MachO::SG_PROTECTED_VERSION_1) {
7184 outs() << " PROTECTED_VERSION_1";
7185 flags &= ~MachO::SG_PROTECTED_VERSION_1;
7188 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
7193 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
7197 static void PrintSection(const char *sectname, const char *segname,
7198 uint64_t addr, uint64_t size, uint32_t offset,
7199 uint32_t align, uint32_t reloff, uint32_t nreloc,
7200 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
7201 uint32_t cmd, const char *sg_segname,
7202 uint32_t filetype, uint32_t object_size,
7204 outs() << "Section\n";
7205 outs() << " sectname " << format("%.16s\n", sectname);
7206 outs() << " segname " << format("%.16s", segname);
7207 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
7208 outs() << " (does not match segment)\n";
7211 if (cmd == MachO::LC_SEGMENT_64) {
7212 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
7213 outs() << " size " << format("0x%016" PRIx64, size);
7215 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
7216 outs() << " size " << format("0x%08" PRIx64, size);
7218 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
7219 outs() << " (past end of file)\n";
7222 outs() << " offset " << offset;
7223 if (offset > object_size)
7224 outs() << " (past end of file)\n";
7227 uint32_t align_shifted = 1 << align;
7228 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
7229 outs() << " reloff " << reloff;
7230 if (reloff > object_size)
7231 outs() << " (past end of file)\n";
7234 outs() << " nreloc " << nreloc;
7235 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
7236 outs() << " (past end of file)\n";
7239 uint32_t section_type = flags & MachO::SECTION_TYPE;
7242 if (section_type == MachO::S_REGULAR)
7243 outs() << " S_REGULAR\n";
7244 else if (section_type == MachO::S_ZEROFILL)
7245 outs() << " S_ZEROFILL\n";
7246 else if (section_type == MachO::S_CSTRING_LITERALS)
7247 outs() << " S_CSTRING_LITERALS\n";
7248 else if (section_type == MachO::S_4BYTE_LITERALS)
7249 outs() << " S_4BYTE_LITERALS\n";
7250 else if (section_type == MachO::S_8BYTE_LITERALS)
7251 outs() << " S_8BYTE_LITERALS\n";
7252 else if (section_type == MachO::S_16BYTE_LITERALS)
7253 outs() << " S_16BYTE_LITERALS\n";
7254 else if (section_type == MachO::S_LITERAL_POINTERS)
7255 outs() << " S_LITERAL_POINTERS\n";
7256 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
7257 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
7258 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
7259 outs() << " S_LAZY_SYMBOL_POINTERS\n";
7260 else if (section_type == MachO::S_SYMBOL_STUBS)
7261 outs() << " S_SYMBOL_STUBS\n";
7262 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
7263 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
7264 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
7265 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
7266 else if (section_type == MachO::S_COALESCED)
7267 outs() << " S_COALESCED\n";
7268 else if (section_type == MachO::S_INTERPOSING)
7269 outs() << " S_INTERPOSING\n";
7270 else if (section_type == MachO::S_DTRACE_DOF)
7271 outs() << " S_DTRACE_DOF\n";
7272 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
7273 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
7274 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
7275 outs() << " S_THREAD_LOCAL_REGULAR\n";
7276 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
7277 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
7278 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
7279 outs() << " S_THREAD_LOCAL_VARIABLES\n";
7280 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7281 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
7282 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
7283 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
7285 outs() << format("0x%08" PRIx32, section_type) << "\n";
7286 outs() << "attributes";
7287 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
7288 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
7289 outs() << " PURE_INSTRUCTIONS";
7290 if (section_attributes & MachO::S_ATTR_NO_TOC)
7291 outs() << " NO_TOC";
7292 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
7293 outs() << " STRIP_STATIC_SYMS";
7294 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
7295 outs() << " NO_DEAD_STRIP";
7296 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
7297 outs() << " LIVE_SUPPORT";
7298 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
7299 outs() << " SELF_MODIFYING_CODE";
7300 if (section_attributes & MachO::S_ATTR_DEBUG)
7302 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
7303 outs() << " SOME_INSTRUCTIONS";
7304 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
7305 outs() << " EXT_RELOC";
7306 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
7307 outs() << " LOC_RELOC";
7308 if (section_attributes == 0)
7309 outs() << " (none)";
7312 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
7313 outs() << " reserved1 " << reserved1;
7314 if (section_type == MachO::S_SYMBOL_STUBS ||
7315 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
7316 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
7317 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
7318 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7319 outs() << " (index into indirect symbol table)\n";
7322 outs() << " reserved2 " << reserved2;
7323 if (section_type == MachO::S_SYMBOL_STUBS)
7324 outs() << " (size of stubs)\n";
7329 static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
7330 uint32_t object_size) {
7331 outs() << " cmd LC_SYMTAB\n";
7332 outs() << " cmdsize " << st.cmdsize;
7333 if (st.cmdsize != sizeof(struct MachO::symtab_command))
7334 outs() << " Incorrect size\n";
7337 outs() << " symoff " << st.symoff;
7338 if (st.symoff > object_size)
7339 outs() << " (past end of file)\n";
7342 outs() << " nsyms " << st.nsyms;
7345 big_size = st.nsyms;
7346 big_size *= sizeof(struct MachO::nlist_64);
7347 big_size += st.symoff;
7348 if (big_size > object_size)
7349 outs() << " (past end of file)\n";
7353 big_size = st.nsyms;
7354 big_size *= sizeof(struct MachO::nlist);
7355 big_size += st.symoff;
7356 if (big_size > object_size)
7357 outs() << " (past end of file)\n";
7361 outs() << " stroff " << st.stroff;
7362 if (st.stroff > object_size)
7363 outs() << " (past end of file)\n";
7366 outs() << " strsize " << st.strsize;
7367 big_size = st.stroff;
7368 big_size += st.strsize;
7369 if (big_size > object_size)
7370 outs() << " (past end of file)\n";
7375 static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
7376 uint32_t nsyms, uint32_t object_size,
7378 outs() << " cmd LC_DYSYMTAB\n";
7379 outs() << " cmdsize " << dyst.cmdsize;
7380 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
7381 outs() << " Incorrect size\n";
7384 outs() << " ilocalsym " << dyst.ilocalsym;
7385 if (dyst.ilocalsym > nsyms)
7386 outs() << " (greater than the number of symbols)\n";
7389 outs() << " nlocalsym " << dyst.nlocalsym;
7391 big_size = dyst.ilocalsym;
7392 big_size += dyst.nlocalsym;
7393 if (big_size > nsyms)
7394 outs() << " (past the end of the symbol table)\n";
7397 outs() << " iextdefsym " << dyst.iextdefsym;
7398 if (dyst.iextdefsym > nsyms)
7399 outs() << " (greater than the number of symbols)\n";
7402 outs() << " nextdefsym " << dyst.nextdefsym;
7403 big_size = dyst.iextdefsym;
7404 big_size += dyst.nextdefsym;
7405 if (big_size > nsyms)
7406 outs() << " (past the end of the symbol table)\n";
7409 outs() << " iundefsym " << dyst.iundefsym;
7410 if (dyst.iundefsym > nsyms)
7411 outs() << " (greater than the number of symbols)\n";
7414 outs() << " nundefsym " << dyst.nundefsym;
7415 big_size = dyst.iundefsym;
7416 big_size += dyst.nundefsym;
7417 if (big_size > nsyms)
7418 outs() << " (past the end of the symbol table)\n";
7421 outs() << " tocoff " << dyst.tocoff;
7422 if (dyst.tocoff > object_size)
7423 outs() << " (past end of file)\n";
7426 outs() << " ntoc " << dyst.ntoc;
7427 big_size = dyst.ntoc;
7428 big_size *= sizeof(struct MachO::dylib_table_of_contents);
7429 big_size += dyst.tocoff;
7430 if (big_size > object_size)
7431 outs() << " (past end of file)\n";
7434 outs() << " modtaboff " << dyst.modtaboff;
7435 if (dyst.modtaboff > object_size)
7436 outs() << " (past end of file)\n";
7439 outs() << " nmodtab " << dyst.nmodtab;
7442 modtabend = dyst.nmodtab;
7443 modtabend *= sizeof(struct MachO::dylib_module_64);
7444 modtabend += dyst.modtaboff;
7446 modtabend = dyst.nmodtab;
7447 modtabend *= sizeof(struct MachO::dylib_module);
7448 modtabend += dyst.modtaboff;
7450 if (modtabend > object_size)
7451 outs() << " (past end of file)\n";
7454 outs() << " extrefsymoff " << dyst.extrefsymoff;
7455 if (dyst.extrefsymoff > object_size)
7456 outs() << " (past end of file)\n";
7459 outs() << " nextrefsyms " << dyst.nextrefsyms;
7460 big_size = dyst.nextrefsyms;
7461 big_size *= sizeof(struct MachO::dylib_reference);
7462 big_size += dyst.extrefsymoff;
7463 if (big_size > object_size)
7464 outs() << " (past end of file)\n";
7467 outs() << " indirectsymoff " << dyst.indirectsymoff;
7468 if (dyst.indirectsymoff > object_size)
7469 outs() << " (past end of file)\n";
7472 outs() << " nindirectsyms " << dyst.nindirectsyms;
7473 big_size = dyst.nindirectsyms;
7474 big_size *= sizeof(uint32_t);
7475 big_size += dyst.indirectsymoff;
7476 if (big_size > object_size)
7477 outs() << " (past end of file)\n";
7480 outs() << " extreloff " << dyst.extreloff;
7481 if (dyst.extreloff > object_size)
7482 outs() << " (past end of file)\n";
7485 outs() << " nextrel " << dyst.nextrel;
7486 big_size = dyst.nextrel;
7487 big_size *= sizeof(struct MachO::relocation_info);
7488 big_size += dyst.extreloff;
7489 if (big_size > object_size)
7490 outs() << " (past end of file)\n";
7493 outs() << " locreloff " << dyst.locreloff;
7494 if (dyst.locreloff > object_size)
7495 outs() << " (past end of file)\n";
7498 outs() << " nlocrel " << dyst.nlocrel;
7499 big_size = dyst.nlocrel;
7500 big_size *= sizeof(struct MachO::relocation_info);
7501 big_size += dyst.locreloff;
7502 if (big_size > object_size)
7503 outs() << " (past end of file)\n";
7508 static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
7509 uint32_t object_size) {
7510 if (dc.cmd == MachO::LC_DYLD_INFO)
7511 outs() << " cmd LC_DYLD_INFO\n";
7513 outs() << " cmd LC_DYLD_INFO_ONLY\n";
7514 outs() << " cmdsize " << dc.cmdsize;
7515 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
7516 outs() << " Incorrect size\n";
7519 outs() << " rebase_off " << dc.rebase_off;
7520 if (dc.rebase_off > object_size)
7521 outs() << " (past end of file)\n";
7524 outs() << " rebase_size " << dc.rebase_size;
7526 big_size = dc.rebase_off;
7527 big_size += dc.rebase_size;
7528 if (big_size > object_size)
7529 outs() << " (past end of file)\n";
7532 outs() << " bind_off " << dc.bind_off;
7533 if (dc.bind_off > object_size)
7534 outs() << " (past end of file)\n";
7537 outs() << " bind_size " << dc.bind_size;
7538 big_size = dc.bind_off;
7539 big_size += dc.bind_size;
7540 if (big_size > object_size)
7541 outs() << " (past end of file)\n";
7544 outs() << " weak_bind_off " << dc.weak_bind_off;
7545 if (dc.weak_bind_off > object_size)
7546 outs() << " (past end of file)\n";
7549 outs() << " weak_bind_size " << dc.weak_bind_size;
7550 big_size = dc.weak_bind_off;
7551 big_size += dc.weak_bind_size;
7552 if (big_size > object_size)
7553 outs() << " (past end of file)\n";
7556 outs() << " lazy_bind_off " << dc.lazy_bind_off;
7557 if (dc.lazy_bind_off > object_size)
7558 outs() << " (past end of file)\n";
7561 outs() << " lazy_bind_size " << dc.lazy_bind_size;
7562 big_size = dc.lazy_bind_off;
7563 big_size += dc.lazy_bind_size;
7564 if (big_size > object_size)
7565 outs() << " (past end of file)\n";
7568 outs() << " export_off " << dc.export_off;
7569 if (dc.export_off > object_size)
7570 outs() << " (past end of file)\n";
7573 outs() << " export_size " << dc.export_size;
7574 big_size = dc.export_off;
7575 big_size += dc.export_size;
7576 if (big_size > object_size)
7577 outs() << " (past end of file)\n";
7582 static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
7584 if (dyld.cmd == MachO::LC_ID_DYLINKER)
7585 outs() << " cmd LC_ID_DYLINKER\n";
7586 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
7587 outs() << " cmd LC_LOAD_DYLINKER\n";
7588 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
7589 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
7591 outs() << " cmd ?(" << dyld.cmd << ")\n";
7592 outs() << " cmdsize " << dyld.cmdsize;
7593 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
7594 outs() << " Incorrect size\n";
7597 if (dyld.name >= dyld.cmdsize)
7598 outs() << " name ?(bad offset " << dyld.name << ")\n";
7600 const char *P = (const char *)(Ptr) + dyld.name;
7601 outs() << " name " << P << " (offset " << dyld.name << ")\n";
7605 static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
7606 outs() << " cmd LC_UUID\n";
7607 outs() << " cmdsize " << uuid.cmdsize;
7608 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
7609 outs() << " Incorrect size\n";
7613 outs() << format("%02" PRIX32, uuid.uuid[0]);
7614 outs() << format("%02" PRIX32, uuid.uuid[1]);
7615 outs() << format("%02" PRIX32, uuid.uuid[2]);
7616 outs() << format("%02" PRIX32, uuid.uuid[3]);
7618 outs() << format("%02" PRIX32, uuid.uuid[4]);
7619 outs() << format("%02" PRIX32, uuid.uuid[5]);
7621 outs() << format("%02" PRIX32, uuid.uuid[6]);
7622 outs() << format("%02" PRIX32, uuid.uuid[7]);
7624 outs() << format("%02" PRIX32, uuid.uuid[8]);
7625 outs() << format("%02" PRIX32, uuid.uuid[9]);
7627 outs() << format("%02" PRIX32, uuid.uuid[10]);
7628 outs() << format("%02" PRIX32, uuid.uuid[11]);
7629 outs() << format("%02" PRIX32, uuid.uuid[12]);
7630 outs() << format("%02" PRIX32, uuid.uuid[13]);
7631 outs() << format("%02" PRIX32, uuid.uuid[14]);
7632 outs() << format("%02" PRIX32, uuid.uuid[15]);
7636 static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
7637 outs() << " cmd LC_RPATH\n";
7638 outs() << " cmdsize " << rpath.cmdsize;
7639 if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
7640 outs() << " Incorrect size\n";
7643 if (rpath.path >= rpath.cmdsize)
7644 outs() << " path ?(bad offset " << rpath.path << ")\n";
7646 const char *P = (const char *)(Ptr) + rpath.path;
7647 outs() << " path " << P << " (offset " << rpath.path << ")\n";
7651 static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
7652 if (vd.cmd == MachO::LC_VERSION_MIN_MACOSX)
7653 outs() << " cmd LC_VERSION_MIN_MACOSX\n";
7654 else if (vd.cmd == MachO::LC_VERSION_MIN_IPHONEOS)
7655 outs() << " cmd LC_VERSION_MIN_IPHONEOS\n";
7657 outs() << " cmd " << vd.cmd << " (?)\n";
7658 outs() << " cmdsize " << vd.cmdsize;
7659 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
7660 outs() << " Incorrect size\n";
7663 outs() << " version " << ((vd.version >> 16) & 0xffff) << "."
7664 << ((vd.version >> 8) & 0xff);
7665 if ((vd.version & 0xff) != 0)
7666 outs() << "." << (vd.version & 0xff);
7669 outs() << " sdk n/a";
7671 outs() << " sdk " << ((vd.sdk >> 16) & 0xffff) << "."
7672 << ((vd.sdk >> 8) & 0xff);
7674 if ((vd.sdk & 0xff) != 0)
7675 outs() << "." << (vd.sdk & 0xff);
7679 static void PrintSourceVersionCommand(MachO::source_version_command sd) {
7680 outs() << " cmd LC_SOURCE_VERSION\n";
7681 outs() << " cmdsize " << sd.cmdsize;
7682 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
7683 outs() << " Incorrect size\n";
7686 uint64_t a = (sd.version >> 40) & 0xffffff;
7687 uint64_t b = (sd.version >> 30) & 0x3ff;
7688 uint64_t c = (sd.version >> 20) & 0x3ff;
7689 uint64_t d = (sd.version >> 10) & 0x3ff;
7690 uint64_t e = sd.version & 0x3ff;
7691 outs() << " version " << a << "." << b;
7693 outs() << "." << c << "." << d << "." << e;
7695 outs() << "." << c << "." << d;
7701 static void PrintEntryPointCommand(MachO::entry_point_command ep) {
7702 outs() << " cmd LC_MAIN\n";
7703 outs() << " cmdsize " << ep.cmdsize;
7704 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
7705 outs() << " Incorrect size\n";
7708 outs() << " entryoff " << ep.entryoff << "\n";
7709 outs() << " stacksize " << ep.stacksize << "\n";
7712 static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
7713 uint32_t object_size) {
7714 outs() << " cmd LC_ENCRYPTION_INFO\n";
7715 outs() << " cmdsize " << ec.cmdsize;
7716 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
7717 outs() << " Incorrect size\n";
7720 outs() << " cryptoff " << ec.cryptoff;
7721 if (ec.cryptoff > object_size)
7722 outs() << " (past end of file)\n";
7725 outs() << " cryptsize " << ec.cryptsize;
7726 if (ec.cryptsize > object_size)
7727 outs() << " (past end of file)\n";
7730 outs() << " cryptid " << ec.cryptid << "\n";
7733 static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
7734 uint32_t object_size) {
7735 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
7736 outs() << " cmdsize " << ec.cmdsize;
7737 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
7738 outs() << " Incorrect size\n";
7741 outs() << " cryptoff " << ec.cryptoff;
7742 if (ec.cryptoff > object_size)
7743 outs() << " (past end of file)\n";
7746 outs() << " cryptsize " << ec.cryptsize;
7747 if (ec.cryptsize > object_size)
7748 outs() << " (past end of file)\n";
7751 outs() << " cryptid " << ec.cryptid << "\n";
7752 outs() << " pad " << ec.pad << "\n";
7755 static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
7757 outs() << " cmd LC_LINKER_OPTION\n";
7758 outs() << " cmdsize " << lo.cmdsize;
7759 if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
7760 outs() << " Incorrect size\n";
7763 outs() << " count " << lo.count << "\n";
7764 const char *string = Ptr + sizeof(struct MachO::linker_option_command);
7765 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
7768 while (*string == '\0' && left > 0) {
7774 outs() << " string #" << i << " " << format("%.*s\n", left, string);
7775 uint32_t NullPos = StringRef(string, left).find('\0');
7776 uint32_t len = std::min(NullPos, left) + 1;
7782 outs() << " count " << lo.count << " does not match number of strings "
7786 static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
7788 outs() << " cmd LC_SUB_FRAMEWORK\n";
7789 outs() << " cmdsize " << sub.cmdsize;
7790 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
7791 outs() << " Incorrect size\n";
7794 if (sub.umbrella < sub.cmdsize) {
7795 const char *P = Ptr + sub.umbrella;
7796 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
7798 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
7802 static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
7804 outs() << " cmd LC_SUB_UMBRELLA\n";
7805 outs() << " cmdsize " << sub.cmdsize;
7806 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
7807 outs() << " Incorrect size\n";
7810 if (sub.sub_umbrella < sub.cmdsize) {
7811 const char *P = Ptr + sub.sub_umbrella;
7812 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
7814 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
7818 static void PrintSubLibraryCommand(MachO::sub_library_command sub,
7820 outs() << " cmd LC_SUB_LIBRARY\n";
7821 outs() << " cmdsize " << sub.cmdsize;
7822 if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
7823 outs() << " Incorrect size\n";
7826 if (sub.sub_library < sub.cmdsize) {
7827 const char *P = Ptr + sub.sub_library;
7828 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
7830 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
7834 static void PrintSubClientCommand(MachO::sub_client_command sub,
7836 outs() << " cmd LC_SUB_CLIENT\n";
7837 outs() << " cmdsize " << sub.cmdsize;
7838 if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
7839 outs() << " Incorrect size\n";
7842 if (sub.client < sub.cmdsize) {
7843 const char *P = Ptr + sub.client;
7844 outs() << " client " << P << " (offset " << sub.client << ")\n";
7846 outs() << " client ?(bad offset " << sub.client << ")\n";
7850 static void PrintRoutinesCommand(MachO::routines_command r) {
7851 outs() << " cmd LC_ROUTINES\n";
7852 outs() << " cmdsize " << r.cmdsize;
7853 if (r.cmdsize != sizeof(struct MachO::routines_command))
7854 outs() << " Incorrect size\n";
7857 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
7858 outs() << " init_module " << r.init_module << "\n";
7859 outs() << " reserved1 " << r.reserved1 << "\n";
7860 outs() << " reserved2 " << r.reserved2 << "\n";
7861 outs() << " reserved3 " << r.reserved3 << "\n";
7862 outs() << " reserved4 " << r.reserved4 << "\n";
7863 outs() << " reserved5 " << r.reserved5 << "\n";
7864 outs() << " reserved6 " << r.reserved6 << "\n";
7867 static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
7868 outs() << " cmd LC_ROUTINES_64\n";
7869 outs() << " cmdsize " << r.cmdsize;
7870 if (r.cmdsize != sizeof(struct MachO::routines_command_64))
7871 outs() << " Incorrect size\n";
7874 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
7875 outs() << " init_module " << r.init_module << "\n";
7876 outs() << " reserved1 " << r.reserved1 << "\n";
7877 outs() << " reserved2 " << r.reserved2 << "\n";
7878 outs() << " reserved3 " << r.reserved3 << "\n";
7879 outs() << " reserved4 " << r.reserved4 << "\n";
7880 outs() << " reserved5 " << r.reserved5 << "\n";
7881 outs() << " reserved6 " << r.reserved6 << "\n";
7884 static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
7885 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
7886 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
7887 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
7888 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
7889 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
7890 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
7891 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
7892 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
7893 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
7894 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
7895 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
7896 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
7897 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
7898 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
7899 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
7900 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
7901 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
7902 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
7903 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
7904 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
7905 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
7908 static void Print_mmst_reg(MachO::mmst_reg_t &r) {
7910 outs() << "\t mmst_reg ";
7911 for (f = 0; f < 10; f++)
7912 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
7914 outs() << "\t mmst_rsrv ";
7915 for (f = 0; f < 6; f++)
7916 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
7920 static void Print_xmm_reg(MachO::xmm_reg_t &r) {
7922 outs() << "\t xmm_reg ";
7923 for (f = 0; f < 16; f++)
7924 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
7928 static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
7929 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
7930 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
7931 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
7932 outs() << " denorm " << fpu.fpu_fcw.denorm;
7933 outs() << " zdiv " << fpu.fpu_fcw.zdiv;
7934 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
7935 outs() << " undfl " << fpu.fpu_fcw.undfl;
7936 outs() << " precis " << fpu.fpu_fcw.precis << "\n";
7937 outs() << "\t\t pc ";
7938 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
7939 outs() << "FP_PREC_24B ";
7940 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
7941 outs() << "FP_PREC_53B ";
7942 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
7943 outs() << "FP_PREC_64B ";
7945 outs() << fpu.fpu_fcw.pc << " ";
7947 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
7948 outs() << "FP_RND_NEAR ";
7949 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
7950 outs() << "FP_RND_DOWN ";
7951 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
7952 outs() << "FP_RND_UP ";
7953 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
7954 outs() << "FP_CHOP ";
7956 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
7957 outs() << " denorm " << fpu.fpu_fsw.denorm;
7958 outs() << " zdiv " << fpu.fpu_fsw.zdiv;
7959 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
7960 outs() << " undfl " << fpu.fpu_fsw.undfl;
7961 outs() << " precis " << fpu.fpu_fsw.precis;
7962 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
7963 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
7964 outs() << " c0 " << fpu.fpu_fsw.c0;
7965 outs() << " c1 " << fpu.fpu_fsw.c1;
7966 outs() << " c2 " << fpu.fpu_fsw.c2;
7967 outs() << " tos " << fpu.fpu_fsw.tos;
7968 outs() << " c3 " << fpu.fpu_fsw.c3;
7969 outs() << " busy " << fpu.fpu_fsw.busy << "\n";
7970 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
7971 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
7972 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
7973 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
7974 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
7975 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
7976 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
7977 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
7978 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
7979 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
7980 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
7982 outs() << "\t fpu_stmm0:\n";
7983 Print_mmst_reg(fpu.fpu_stmm0);
7984 outs() << "\t fpu_stmm1:\n";
7985 Print_mmst_reg(fpu.fpu_stmm1);
7986 outs() << "\t fpu_stmm2:\n";
7987 Print_mmst_reg(fpu.fpu_stmm2);
7988 outs() << "\t fpu_stmm3:\n";
7989 Print_mmst_reg(fpu.fpu_stmm3);
7990 outs() << "\t fpu_stmm4:\n";
7991 Print_mmst_reg(fpu.fpu_stmm4);
7992 outs() << "\t fpu_stmm5:\n";
7993 Print_mmst_reg(fpu.fpu_stmm5);
7994 outs() << "\t fpu_stmm6:\n";
7995 Print_mmst_reg(fpu.fpu_stmm6);
7996 outs() << "\t fpu_stmm7:\n";
7997 Print_mmst_reg(fpu.fpu_stmm7);
7998 outs() << "\t fpu_xmm0:\n";
7999 Print_xmm_reg(fpu.fpu_xmm0);
8000 outs() << "\t fpu_xmm1:\n";
8001 Print_xmm_reg(fpu.fpu_xmm1);
8002 outs() << "\t fpu_xmm2:\n";
8003 Print_xmm_reg(fpu.fpu_xmm2);
8004 outs() << "\t fpu_xmm3:\n";
8005 Print_xmm_reg(fpu.fpu_xmm3);
8006 outs() << "\t fpu_xmm4:\n";
8007 Print_xmm_reg(fpu.fpu_xmm4);
8008 outs() << "\t fpu_xmm5:\n";
8009 Print_xmm_reg(fpu.fpu_xmm5);
8010 outs() << "\t fpu_xmm6:\n";
8011 Print_xmm_reg(fpu.fpu_xmm6);
8012 outs() << "\t fpu_xmm7:\n";
8013 Print_xmm_reg(fpu.fpu_xmm7);
8014 outs() << "\t fpu_xmm8:\n";
8015 Print_xmm_reg(fpu.fpu_xmm8);
8016 outs() << "\t fpu_xmm9:\n";
8017 Print_xmm_reg(fpu.fpu_xmm9);
8018 outs() << "\t fpu_xmm10:\n";
8019 Print_xmm_reg(fpu.fpu_xmm10);
8020 outs() << "\t fpu_xmm11:\n";
8021 Print_xmm_reg(fpu.fpu_xmm11);
8022 outs() << "\t fpu_xmm12:\n";
8023 Print_xmm_reg(fpu.fpu_xmm12);
8024 outs() << "\t fpu_xmm13:\n";
8025 Print_xmm_reg(fpu.fpu_xmm13);
8026 outs() << "\t fpu_xmm14:\n";
8027 Print_xmm_reg(fpu.fpu_xmm14);
8028 outs() << "\t fpu_xmm15:\n";
8029 Print_xmm_reg(fpu.fpu_xmm15);
8030 outs() << "\t fpu_rsrv4:\n";
8031 for (uint32_t f = 0; f < 6; f++) {
8033 for (uint32_t g = 0; g < 16; g++)
8034 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
8037 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
8041 static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
8042 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
8043 outs() << " err " << format("0x%08" PRIx32, exc64.err);
8044 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
8047 static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
8048 bool isLittleEndian, uint32_t cputype) {
8049 if (t.cmd == MachO::LC_THREAD)
8050 outs() << " cmd LC_THREAD\n";
8051 else if (t.cmd == MachO::LC_UNIXTHREAD)
8052 outs() << " cmd LC_UNIXTHREAD\n";
8054 outs() << " cmd " << t.cmd << " (unknown)\n";
8055 outs() << " cmdsize " << t.cmdsize;
8056 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
8057 outs() << " Incorrect size\n";
8061 const char *begin = Ptr + sizeof(struct MachO::thread_command);
8062 const char *end = Ptr + t.cmdsize;
8063 uint32_t flavor, count, left;
8064 if (cputype == MachO::CPU_TYPE_X86_64) {
8065 while (begin < end) {
8066 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8067 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8068 begin += sizeof(uint32_t);
8073 if (isLittleEndian != sys::IsLittleEndianHost)
8074 sys::swapByteOrder(flavor);
8075 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8076 memcpy((char *)&count, begin, sizeof(uint32_t));
8077 begin += sizeof(uint32_t);
8082 if (isLittleEndian != sys::IsLittleEndianHost)
8083 sys::swapByteOrder(count);
8084 if (flavor == MachO::x86_THREAD_STATE64) {
8085 outs() << " flavor x86_THREAD_STATE64\n";
8086 if (count == MachO::x86_THREAD_STATE64_COUNT)
8087 outs() << " count x86_THREAD_STATE64_COUNT\n";
8089 outs() << " count " << count
8090 << " (not x86_THREAD_STATE64_COUNT)\n";
8091 MachO::x86_thread_state64_t cpu64;
8093 if (left >= sizeof(MachO::x86_thread_state64_t)) {
8094 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
8095 begin += sizeof(MachO::x86_thread_state64_t);
8097 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
8098 memcpy(&cpu64, begin, left);
8101 if (isLittleEndian != sys::IsLittleEndianHost)
8103 Print_x86_thread_state64_t(cpu64);
8104 } else if (flavor == MachO::x86_THREAD_STATE) {
8105 outs() << " flavor x86_THREAD_STATE\n";
8106 if (count == MachO::x86_THREAD_STATE_COUNT)
8107 outs() << " count x86_THREAD_STATE_COUNT\n";
8109 outs() << " count " << count
8110 << " (not x86_THREAD_STATE_COUNT)\n";
8111 struct MachO::x86_thread_state_t ts;
8113 if (left >= sizeof(MachO::x86_thread_state_t)) {
8114 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
8115 begin += sizeof(MachO::x86_thread_state_t);
8117 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
8118 memcpy(&ts, begin, left);
8121 if (isLittleEndian != sys::IsLittleEndianHost)
8123 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
8124 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
8125 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
8126 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
8128 outs() << "tsh.count " << ts.tsh.count
8129 << " (not x86_THREAD_STATE64_COUNT\n";
8130 Print_x86_thread_state64_t(ts.uts.ts64);
8132 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
8133 << ts.tsh.count << "\n";
8135 } else if (flavor == MachO::x86_FLOAT_STATE) {
8136 outs() << " flavor x86_FLOAT_STATE\n";
8137 if (count == MachO::x86_FLOAT_STATE_COUNT)
8138 outs() << " count x86_FLOAT_STATE_COUNT\n";
8140 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
8141 struct MachO::x86_float_state_t fs;
8143 if (left >= sizeof(MachO::x86_float_state_t)) {
8144 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
8145 begin += sizeof(MachO::x86_float_state_t);
8147 memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
8148 memcpy(&fs, begin, left);
8151 if (isLittleEndian != sys::IsLittleEndianHost)
8153 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
8154 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
8155 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
8156 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
8158 outs() << "fsh.count " << fs.fsh.count
8159 << " (not x86_FLOAT_STATE64_COUNT\n";
8160 Print_x86_float_state_t(fs.ufs.fs64);
8162 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
8163 << fs.fsh.count << "\n";
8165 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
8166 outs() << " flavor x86_EXCEPTION_STATE\n";
8167 if (count == MachO::x86_EXCEPTION_STATE_COUNT)
8168 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
8170 outs() << " count " << count
8171 << " (not x86_EXCEPTION_STATE_COUNT)\n";
8172 struct MachO::x86_exception_state_t es;
8174 if (left >= sizeof(MachO::x86_exception_state_t)) {
8175 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
8176 begin += sizeof(MachO::x86_exception_state_t);
8178 memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
8179 memcpy(&es, begin, left);
8182 if (isLittleEndian != sys::IsLittleEndianHost)
8184 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
8185 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
8186 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
8187 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
8189 outs() << "\t esh.count " << es.esh.count
8190 << " (not x86_EXCEPTION_STATE64_COUNT\n";
8191 Print_x86_exception_state_t(es.ues.es64);
8193 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
8194 << es.esh.count << "\n";
8197 outs() << " flavor " << flavor << " (unknown)\n";
8198 outs() << " count " << count << "\n";
8199 outs() << " state (unknown)\n";
8200 begin += count * sizeof(uint32_t);
8204 while (begin < end) {
8205 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8206 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8207 begin += sizeof(uint32_t);
8212 if (isLittleEndian != sys::IsLittleEndianHost)
8213 sys::swapByteOrder(flavor);
8214 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8215 memcpy((char *)&count, begin, sizeof(uint32_t));
8216 begin += sizeof(uint32_t);
8221 if (isLittleEndian != sys::IsLittleEndianHost)
8222 sys::swapByteOrder(count);
8223 outs() << " flavor " << flavor << "\n";
8224 outs() << " count " << count << "\n";
8225 outs() << " state (Unknown cputype/cpusubtype)\n";
8226 begin += count * sizeof(uint32_t);
8231 static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
8232 if (dl.cmd == MachO::LC_ID_DYLIB)
8233 outs() << " cmd LC_ID_DYLIB\n";
8234 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
8235 outs() << " cmd LC_LOAD_DYLIB\n";
8236 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
8237 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
8238 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
8239 outs() << " cmd LC_REEXPORT_DYLIB\n";
8240 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
8241 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
8242 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
8243 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
8245 outs() << " cmd " << dl.cmd << " (unknown)\n";
8246 outs() << " cmdsize " << dl.cmdsize;
8247 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
8248 outs() << " Incorrect size\n";
8251 if (dl.dylib.name < dl.cmdsize) {
8252 const char *P = (const char *)(Ptr) + dl.dylib.name;
8253 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
8255 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
8257 outs() << " time stamp " << dl.dylib.timestamp << " ";
8258 time_t t = dl.dylib.timestamp;
8259 outs() << ctime(&t);
8260 outs() << " current version ";
8261 if (dl.dylib.current_version == 0xffffffff)
8264 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
8265 << ((dl.dylib.current_version >> 8) & 0xff) << "."
8266 << (dl.dylib.current_version & 0xff) << "\n";
8267 outs() << "compatibility version ";
8268 if (dl.dylib.compatibility_version == 0xffffffff)
8271 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
8272 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
8273 << (dl.dylib.compatibility_version & 0xff) << "\n";
8276 static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
8277 uint32_t object_size) {
8278 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
8279 outs() << " cmd LC_FUNCTION_STARTS\n";
8280 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
8281 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
8282 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
8283 outs() << " cmd LC_FUNCTION_STARTS\n";
8284 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
8285 outs() << " cmd LC_DATA_IN_CODE\n";
8286 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
8287 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
8288 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
8289 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
8291 outs() << " cmd " << ld.cmd << " (?)\n";
8292 outs() << " cmdsize " << ld.cmdsize;
8293 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
8294 outs() << " Incorrect size\n";
8297 outs() << " dataoff " << ld.dataoff;
8298 if (ld.dataoff > object_size)
8299 outs() << " (past end of file)\n";
8302 outs() << " datasize " << ld.datasize;
8303 uint64_t big_size = ld.dataoff;
8304 big_size += ld.datasize;
8305 if (big_size > object_size)
8306 outs() << " (past end of file)\n";
8311 static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t ncmds,
8312 uint32_t filetype, uint32_t cputype,
8316 StringRef Buf = Obj->getData();
8317 MachOObjectFile::LoadCommandInfo Command = Obj->getFirstLoadCommandInfo();
8318 for (unsigned i = 0;; ++i) {
8319 outs() << "Load command " << i << "\n";
8320 if (Command.C.cmd == MachO::LC_SEGMENT) {
8321 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
8322 const char *sg_segname = SLC.segname;
8323 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
8324 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
8325 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
8327 for (unsigned j = 0; j < SLC.nsects; j++) {
8328 MachO::section S = Obj->getSection(Command, j);
8329 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
8330 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
8331 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
8333 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
8334 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
8335 const char *sg_segname = SLC_64.segname;
8336 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
8337 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
8338 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
8339 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
8340 for (unsigned j = 0; j < SLC_64.nsects; j++) {
8341 MachO::section_64 S_64 = Obj->getSection64(Command, j);
8342 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
8343 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
8344 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
8345 sg_segname, filetype, Buf.size(), verbose);
8347 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
8348 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
8349 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
8350 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
8351 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
8352 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
8353 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
8355 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
8356 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
8357 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
8358 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
8359 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
8360 Command.C.cmd == MachO::LC_ID_DYLINKER ||
8361 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
8362 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
8363 PrintDyldLoadCommand(Dyld, Command.Ptr);
8364 } else if (Command.C.cmd == MachO::LC_UUID) {
8365 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
8366 PrintUuidLoadCommand(Uuid);
8367 } else if (Command.C.cmd == MachO::LC_RPATH) {
8368 MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
8369 PrintRpathLoadCommand(Rpath, Command.Ptr);
8370 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
8371 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS) {
8372 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
8373 PrintVersionMinLoadCommand(Vd);
8374 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
8375 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
8376 PrintSourceVersionCommand(Sd);
8377 } else if (Command.C.cmd == MachO::LC_MAIN) {
8378 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
8379 PrintEntryPointCommand(Ep);
8380 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
8381 MachO::encryption_info_command Ei =
8382 Obj->getEncryptionInfoCommand(Command);
8383 PrintEncryptionInfoCommand(Ei, Buf.size());
8384 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
8385 MachO::encryption_info_command_64 Ei =
8386 Obj->getEncryptionInfoCommand64(Command);
8387 PrintEncryptionInfoCommand64(Ei, Buf.size());
8388 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
8389 MachO::linker_option_command Lo =
8390 Obj->getLinkerOptionLoadCommand(Command);
8391 PrintLinkerOptionCommand(Lo, Command.Ptr);
8392 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
8393 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
8394 PrintSubFrameworkCommand(Sf, Command.Ptr);
8395 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
8396 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
8397 PrintSubUmbrellaCommand(Sf, Command.Ptr);
8398 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
8399 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
8400 PrintSubLibraryCommand(Sl, Command.Ptr);
8401 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
8402 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
8403 PrintSubClientCommand(Sc, Command.Ptr);
8404 } else if (Command.C.cmd == MachO::LC_ROUTINES) {
8405 MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
8406 PrintRoutinesCommand(Rc);
8407 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
8408 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
8409 PrintRoutinesCommand64(Rc);
8410 } else if (Command.C.cmd == MachO::LC_THREAD ||
8411 Command.C.cmd == MachO::LC_UNIXTHREAD) {
8412 MachO::thread_command Tc = Obj->getThreadCommand(Command);
8413 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
8414 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
8415 Command.C.cmd == MachO::LC_ID_DYLIB ||
8416 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
8417 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
8418 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
8419 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
8420 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
8421 PrintDylibCommand(Dl, Command.Ptr);
8422 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
8423 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
8424 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
8425 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
8426 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
8427 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
8428 MachO::linkedit_data_command Ld =
8429 Obj->getLinkeditDataLoadCommand(Command);
8430 PrintLinkEditDataCommand(Ld, Buf.size());
8432 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
8434 outs() << " cmdsize " << Command.C.cmdsize << "\n";
8435 // TODO: get and print the raw bytes of the load command.
8437 // TODO: print all the other kinds of load commands.
8441 Command = Obj->getNextLoadCommandInfo(Command);
8445 static void getAndPrintMachHeader(const MachOObjectFile *Obj, uint32_t &ncmds,
8446 uint32_t &filetype, uint32_t &cputype,
8448 if (Obj->is64Bit()) {
8449 MachO::mach_header_64 H_64;
8450 H_64 = Obj->getHeader64();
8451 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
8452 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
8454 filetype = H_64.filetype;
8455 cputype = H_64.cputype;
8457 MachO::mach_header H;
8458 H = Obj->getHeader();
8459 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
8460 H.sizeofcmds, H.flags, verbose);
8462 filetype = H.filetype;
8463 cputype = H.cputype;
8467 void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
8468 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
8470 uint32_t filetype = 0;
8471 uint32_t cputype = 0;
8472 getAndPrintMachHeader(file, ncmds, filetype, cputype, !NonVerbose);
8473 PrintLoadCommands(file, ncmds, filetype, cputype, !NonVerbose);
8476 //===----------------------------------------------------------------------===//
8477 // export trie dumping
8478 //===----------------------------------------------------------------------===//
8480 void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
8481 for (const llvm::object::ExportEntry &Entry : Obj->exports()) {
8482 uint64_t Flags = Entry.flags();
8483 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
8484 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
8485 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8486 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
8487 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8488 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
8489 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
8491 outs() << "[re-export] ";
8493 outs() << format("0x%08llX ",
8494 Entry.address()); // FIXME:add in base address
8495 outs() << Entry.name();
8496 if (WeakDef || ThreadLocal || Resolver || Abs) {
8497 bool NeedsComma = false;
8500 outs() << "weak_def";
8506 outs() << "per-thread";
8512 outs() << "absolute";
8518 outs() << format("resolver=0x%08llX", Entry.other());
8524 StringRef DylibName = "unknown";
8525 int Ordinal = Entry.other() - 1;
8526 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
8527 if (Entry.otherName().empty())
8528 outs() << " (from " << DylibName << ")";
8530 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
8536 //===----------------------------------------------------------------------===//
8537 // rebase table dumping
8538 //===----------------------------------------------------------------------===//
8543 SegInfo(const object::MachOObjectFile *Obj);
8545 StringRef segmentName(uint32_t SegIndex);
8546 StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset);
8547 uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
8550 struct SectionInfo {
8553 StringRef SectionName;
8554 StringRef SegmentName;
8555 uint64_t OffsetInSegment;
8556 uint64_t SegmentStartAddress;
8557 uint32_t SegmentIndex;
8559 const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset);
8560 SmallVector<SectionInfo, 32> Sections;
8564 SegInfo::SegInfo(const object::MachOObjectFile *Obj) {
8565 // Build table of sections so segIndex/offset pairs can be translated.
8566 uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
8567 StringRef CurSegName;
8568 uint64_t CurSegAddress;
8569 for (const SectionRef &Section : Obj->sections()) {
8571 if (error(Section.getName(Info.SectionName)))
8573 Info.Address = Section.getAddress();
8574 Info.Size = Section.getSize();
8576 Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
8577 if (!Info.SegmentName.equals(CurSegName)) {
8579 CurSegName = Info.SegmentName;
8580 CurSegAddress = Info.Address;
8582 Info.SegmentIndex = CurSegIndex - 1;
8583 Info.OffsetInSegment = Info.Address - CurSegAddress;
8584 Info.SegmentStartAddress = CurSegAddress;
8585 Sections.push_back(Info);
8589 StringRef SegInfo::segmentName(uint32_t SegIndex) {
8590 for (const SectionInfo &SI : Sections) {
8591 if (SI.SegmentIndex == SegIndex)
8592 return SI.SegmentName;
8594 llvm_unreachable("invalid segIndex");
8597 const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex,
8598 uint64_t OffsetInSeg) {
8599 for (const SectionInfo &SI : Sections) {
8600 if (SI.SegmentIndex != SegIndex)
8602 if (SI.OffsetInSegment > OffsetInSeg)
8604 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
8608 llvm_unreachable("segIndex and offset not in any section");
8611 StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) {
8612 return findSection(SegIndex, OffsetInSeg).SectionName;
8615 uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) {
8616 const SectionInfo &SI = findSection(SegIndex, OffsetInSeg);
8617 return SI.SegmentStartAddress + OffsetInSeg;
8620 void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) {
8621 // Build table of sections so names can used in final output.
8622 SegInfo sectionTable(Obj);
8624 outs() << "segment section address type\n";
8625 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) {
8626 uint32_t SegIndex = Entry.segmentIndex();
8627 uint64_t OffsetInSeg = Entry.segmentOffset();
8628 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8629 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8630 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8632 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
8633 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
8634 SegmentName.str().c_str(), SectionName.str().c_str(),
8635 Address, Entry.typeName().str().c_str());
8639 static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
8640 StringRef DylibName;
8642 case MachO::BIND_SPECIAL_DYLIB_SELF:
8643 return "this-image";
8644 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
8645 return "main-executable";
8646 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
8647 return "flat-namespace";
8650 std::error_code EC =
8651 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
8653 return "<<bad library ordinal>>";
8657 return "<<unknown special ordinal>>";
8660 //===----------------------------------------------------------------------===//
8661 // bind table dumping
8662 //===----------------------------------------------------------------------===//
8664 void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) {
8665 // Build table of sections so names can used in final output.
8666 SegInfo sectionTable(Obj);
8668 outs() << "segment section address type "
8669 "addend dylib symbol\n";
8670 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) {
8671 uint32_t SegIndex = Entry.segmentIndex();
8672 uint64_t OffsetInSeg = Entry.segmentOffset();
8673 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8674 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8675 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8677 // Table lines look like:
8678 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
8680 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
8681 Attr = " (weak_import)";
8682 outs() << left_justify(SegmentName, 8) << " "
8683 << left_justify(SectionName, 18) << " "
8684 << format_hex(Address, 10, true) << " "
8685 << left_justify(Entry.typeName(), 8) << " "
8686 << format_decimal(Entry.addend(), 8) << " "
8687 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
8688 << Entry.symbolName() << Attr << "\n";
8692 //===----------------------------------------------------------------------===//
8693 // lazy bind table dumping
8694 //===----------------------------------------------------------------------===//
8696 void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) {
8697 // Build table of sections so names can used in final output.
8698 SegInfo sectionTable(Obj);
8700 outs() << "segment section address "
8702 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) {
8703 uint32_t SegIndex = Entry.segmentIndex();
8704 uint64_t OffsetInSeg = Entry.segmentOffset();
8705 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8706 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8707 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8709 // Table lines look like:
8710 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
8711 outs() << left_justify(SegmentName, 8) << " "
8712 << left_justify(SectionName, 18) << " "
8713 << format_hex(Address, 10, true) << " "
8714 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
8715 << Entry.symbolName() << "\n";
8719 //===----------------------------------------------------------------------===//
8720 // weak bind table dumping
8721 //===----------------------------------------------------------------------===//
8723 void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) {
8724 // Build table of sections so names can used in final output.
8725 SegInfo sectionTable(Obj);
8727 outs() << "segment section address "
8728 "type addend symbol\n";
8729 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) {
8730 // Strong symbols don't have a location to update.
8731 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
8732 outs() << " strong "
8733 << Entry.symbolName() << "\n";
8736 uint32_t SegIndex = Entry.segmentIndex();
8737 uint64_t OffsetInSeg = Entry.segmentOffset();
8738 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8739 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8740 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8742 // Table lines look like:
8743 // __DATA __data 0x00001000 pointer 0 _foo
8744 outs() << left_justify(SegmentName, 8) << " "
8745 << left_justify(SectionName, 18) << " "
8746 << format_hex(Address, 10, true) << " "
8747 << left_justify(Entry.typeName(), 8) << " "
8748 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
8753 // get_dyld_bind_info_symbolname() is used for disassembly and passed an
8754 // address, ReferenceValue, in the Mach-O file and looks in the dyld bind
8755 // information for that address. If the address is found its binding symbol
8756 // name is returned. If not nullptr is returned.
8757 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
8758 struct DisassembleInfo *info) {
8759 if (info->bindtable == nullptr) {
8760 info->bindtable = new (BindTable);
8761 SegInfo sectionTable(info->O);
8762 for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable()) {
8763 uint32_t SegIndex = Entry.segmentIndex();
8764 uint64_t OffsetInSeg = Entry.segmentOffset();
8765 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8766 const char *SymbolName = nullptr;
8767 StringRef name = Entry.symbolName();
8769 SymbolName = name.data();
8770 info->bindtable->push_back(std::make_pair(Address, SymbolName));
8773 for (bind_table_iterator BI = info->bindtable->begin(),
8774 BE = info->bindtable->end();
8776 uint64_t Address = BI->first;
8777 if (ReferenceValue == Address) {
8778 const char *SymbolName = BI->second;