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/Optional.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/Config/config.h"
21 #include "llvm/DebugInfo/DIContext.h"
22 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/MC/MCContext.h"
25 #include "llvm/MC/MCDisassembler.h"
26 #include "llvm/MC/MCInst.h"
27 #include "llvm/MC/MCInstPrinter.h"
28 #include "llvm/MC/MCInstrDesc.h"
29 #include "llvm/MC/MCInstrInfo.h"
30 #include "llvm/MC/MCRegisterInfo.h"
31 #include "llvm/MC/MCSubtargetInfo.h"
32 #include "llvm/Object/MachO.h"
33 #include "llvm/Object/MachOUniversal.h"
34 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/Endian.h"
38 #include "llvm/Support/Format.h"
39 #include "llvm/Support/FormattedStream.h"
40 #include "llvm/Support/GraphWriter.h"
41 #include "llvm/Support/LEB128.h"
42 #include "llvm/Support/MachO.h"
43 #include "llvm/Support/MemoryBuffer.h"
44 #include "llvm/Support/TargetRegistry.h"
45 #include "llvm/Support/TargetSelect.h"
46 #include "llvm/Support/raw_ostream.h"
49 #include <system_error>
56 using namespace object;
60 cl::desc("Print line information from debug info if available"));
62 static cl::opt<std::string> DSYMFile("dsym",
63 cl::desc("Use .dSYM file for debug info"));
65 static cl::opt<bool> FullLeadingAddr("full-leading-addr",
66 cl::desc("Print full leading address"));
68 static cl::opt<bool> NoLeadingAddr("no-leading-addr",
69 cl::desc("Print no leading address"));
71 cl::opt<bool> llvm::UniversalHeaders("universal-headers",
72 cl::desc("Print Mach-O universal headers "
73 "(requires -macho)"));
76 llvm::ArchiveHeaders("archive-headers",
77 cl::desc("Print archive headers for Mach-O archives "
78 "(requires -macho)"));
81 ArchiveMemberOffsets("archive-member-offsets",
82 cl::desc("Print the offset to each archive member for "
83 "Mach-O archives (requires -macho and "
84 "-archive-headers)"));
87 llvm::IndirectSymbols("indirect-symbols",
88 cl::desc("Print indirect symbol table for Mach-O "
89 "objects (requires -macho)"));
92 llvm::DataInCode("data-in-code",
93 cl::desc("Print the data in code table for Mach-O objects "
94 "(requires -macho)"));
97 llvm::LinkOptHints("link-opt-hints",
98 cl::desc("Print the linker optimization hints for "
99 "Mach-O objects (requires -macho)"));
102 llvm::InfoPlist("info-plist",
103 cl::desc("Print the info plist section as strings for "
104 "Mach-O objects (requires -macho)"));
107 llvm::DylibsUsed("dylibs-used",
108 cl::desc("Print the shared libraries used for linked "
109 "Mach-O files (requires -macho)"));
112 llvm::DylibId("dylib-id",
113 cl::desc("Print the shared library's id for the dylib Mach-O "
114 "file (requires -macho)"));
117 llvm::NonVerbose("non-verbose",
118 cl::desc("Print the info for Mach-O objects in "
119 "non-verbose or numeric form (requires -macho)"));
122 llvm::ObjcMetaData("objc-meta-data",
123 cl::desc("Print the Objective-C runtime meta data for "
124 "Mach-O files (requires -macho)"));
126 cl::opt<std::string> llvm::DisSymName(
128 cl::desc("disassemble just this symbol's instructions (requires -macho"));
130 static cl::opt<bool> NoSymbolicOperands(
131 "no-symbolic-operands",
132 cl::desc("do not symbolic operands when disassembling (requires -macho)"));
134 static cl::list<std::string>
135 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
138 bool ArchAll = false;
140 static std::string ThumbTripleName;
142 static const Target *GetTarget(const MachOObjectFile *MachOObj,
143 const char **McpuDefault,
144 const Target **ThumbTarget) {
145 // Figure out the target triple.
146 if (TripleName.empty()) {
147 llvm::Triple TT("unknown-unknown-unknown");
148 llvm::Triple ThumbTriple = Triple();
149 TT = MachOObj->getArch(McpuDefault, &ThumbTriple);
150 TripleName = TT.str();
151 ThumbTripleName = ThumbTriple.str();
154 // Get the target specific parser.
156 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
157 if (TheTarget && ThumbTripleName.empty())
160 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
164 errs() << "llvm-objdump: error: unable to get target for '";
166 errs() << TripleName;
168 errs() << ThumbTripleName;
169 errs() << "', see --version and --triple.\n";
173 struct SymbolSorter {
174 bool operator()(const SymbolRef &A, const SymbolRef &B) {
175 uint64_t AAddr = (A.getType() != SymbolRef::ST_Function) ? 0 : A.getValue();
176 uint64_t BAddr = (B.getType() != SymbolRef::ST_Function) ? 0 : B.getValue();
177 return AAddr < BAddr;
181 // Types for the storted data in code table that is built before disassembly
182 // and the predicate function to sort them.
183 typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
184 typedef std::vector<DiceTableEntry> DiceTable;
185 typedef DiceTable::iterator dice_table_iterator;
187 // This is used to search for a data in code table entry for the PC being
188 // disassembled. The j parameter has the PC in j.first. A single data in code
189 // table entry can cover many bytes for each of its Kind's. So if the offset,
190 // aka the i.first value, of the data in code table entry plus its Length
191 // covers the PC being searched for this will return true. If not it will
193 static bool compareDiceTableEntries(const DiceTableEntry &i,
194 const DiceTableEntry &j) {
196 i.second.getLength(Length);
198 return j.first >= i.first && j.first < i.first + Length;
201 static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
202 unsigned short Kind) {
203 uint32_t Value, Size = 1;
207 case MachO::DICE_KIND_DATA:
210 dumpBytes(makeArrayRef(bytes, 4), outs());
211 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
212 outs() << "\t.long " << Value;
214 } else if (Length >= 2) {
216 dumpBytes(makeArrayRef(bytes, 2), outs());
217 Value = bytes[1] << 8 | bytes[0];
218 outs() << "\t.short " << Value;
222 dumpBytes(makeArrayRef(bytes, 2), outs());
224 outs() << "\t.byte " << Value;
227 if (Kind == MachO::DICE_KIND_DATA)
228 outs() << "\t@ KIND_DATA\n";
230 outs() << "\t@ data in code kind = " << Kind << "\n";
232 case MachO::DICE_KIND_JUMP_TABLE8:
234 dumpBytes(makeArrayRef(bytes, 1), outs());
236 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
239 case MachO::DICE_KIND_JUMP_TABLE16:
241 dumpBytes(makeArrayRef(bytes, 2), outs());
242 Value = bytes[1] << 8 | bytes[0];
243 outs() << "\t.short " << format("%5u", Value & 0xffff)
244 << "\t@ KIND_JUMP_TABLE16\n";
247 case MachO::DICE_KIND_JUMP_TABLE32:
248 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
250 dumpBytes(makeArrayRef(bytes, 4), outs());
251 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
252 outs() << "\t.long " << Value;
253 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
254 outs() << "\t@ KIND_JUMP_TABLE32\n";
256 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
263 static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
264 std::vector<SectionRef> &Sections,
265 std::vector<SymbolRef> &Symbols,
266 SmallVectorImpl<uint64_t> &FoundFns,
267 uint64_t &BaseSegmentAddress) {
268 for (const SymbolRef &Symbol : MachOObj->symbols()) {
269 ErrorOr<StringRef> SymName = Symbol.getName();
270 if (std::error_code EC = SymName.getError())
271 report_fatal_error(EC.message());
272 if (!SymName->startswith("ltmp"))
273 Symbols.push_back(Symbol);
276 for (const SectionRef &Section : MachOObj->sections()) {
278 Section.getName(SectName);
279 Sections.push_back(Section);
282 bool BaseSegmentAddressSet = false;
283 for (const auto &Command : MachOObj->load_commands()) {
284 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
285 // We found a function starts segment, parse the addresses for later
287 MachO::linkedit_data_command LLC =
288 MachOObj->getLinkeditDataLoadCommand(Command);
290 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
291 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
292 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
293 StringRef SegName = SLC.segname;
294 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
295 BaseSegmentAddressSet = true;
296 BaseSegmentAddress = SLC.vmaddr;
302 static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
303 uint32_t n, uint32_t count,
304 uint32_t stride, uint64_t addr) {
305 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
306 uint32_t nindirectsyms = Dysymtab.nindirectsyms;
307 if (n > nindirectsyms)
308 outs() << " (entries start past the end of the indirect symbol "
309 "table) (reserved1 field greater than the table size)";
310 else if (n + count > nindirectsyms)
311 outs() << " (entries extends past the end of the indirect symbol "
314 uint32_t cputype = O->getHeader().cputype;
315 if (cputype & MachO::CPU_ARCH_ABI64)
316 outs() << "address index";
318 outs() << "address index";
323 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
324 if (cputype & MachO::CPU_ARCH_ABI64)
325 outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
327 outs() << format("0x%08" PRIx32, addr + j * stride) << " ";
328 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
329 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
330 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
334 if (indirect_symbol ==
335 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
336 outs() << "LOCAL ABSOLUTE\n";
339 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
340 outs() << "ABSOLUTE\n";
343 outs() << format("%5u ", indirect_symbol);
345 MachO::symtab_command Symtab = O->getSymtabLoadCommand();
346 if (indirect_symbol < Symtab.nsyms) {
347 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
348 SymbolRef Symbol = *Sym;
349 ErrorOr<StringRef> SymName = Symbol.getName();
350 if (std::error_code EC = SymName.getError())
351 report_fatal_error(EC.message());
361 static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
362 for (const auto &Load : O->load_commands()) {
363 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
364 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
365 for (unsigned J = 0; J < Seg.nsects; ++J) {
366 MachO::section_64 Sec = O->getSection64(Load, J);
367 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
368 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
369 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
370 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
371 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
372 section_type == MachO::S_SYMBOL_STUBS) {
374 if (section_type == MachO::S_SYMBOL_STUBS)
375 stride = Sec.reserved2;
379 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
380 << Sec.sectname << ") "
381 << "(size of stubs in reserved2 field is zero)\n";
384 uint32_t count = Sec.size / stride;
385 outs() << "Indirect symbols for (" << Sec.segname << ","
386 << Sec.sectname << ") " << count << " entries";
387 uint32_t n = Sec.reserved1;
388 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
391 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
392 MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
393 for (unsigned J = 0; J < Seg.nsects; ++J) {
394 MachO::section Sec = O->getSection(Load, J);
395 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
396 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
397 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
398 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
399 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
400 section_type == MachO::S_SYMBOL_STUBS) {
402 if (section_type == MachO::S_SYMBOL_STUBS)
403 stride = Sec.reserved2;
407 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
408 << Sec.sectname << ") "
409 << "(size of stubs in reserved2 field is zero)\n";
412 uint32_t count = Sec.size / stride;
413 outs() << "Indirect symbols for (" << Sec.segname << ","
414 << Sec.sectname << ") " << count << " entries";
415 uint32_t n = Sec.reserved1;
416 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
423 static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
424 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
425 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
426 outs() << "Data in code table (" << nentries << " entries)\n";
427 outs() << "offset length kind\n";
428 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
431 DI->getOffset(Offset);
432 outs() << format("0x%08" PRIx32, Offset) << " ";
434 DI->getLength(Length);
435 outs() << format("%6u", Length) << " ";
440 case MachO::DICE_KIND_DATA:
443 case MachO::DICE_KIND_JUMP_TABLE8:
444 outs() << "JUMP_TABLE8";
446 case MachO::DICE_KIND_JUMP_TABLE16:
447 outs() << "JUMP_TABLE16";
449 case MachO::DICE_KIND_JUMP_TABLE32:
450 outs() << "JUMP_TABLE32";
452 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
453 outs() << "ABS_JUMP_TABLE32";
456 outs() << format("0x%04" PRIx32, Kind);
460 outs() << format("0x%04" PRIx32, Kind);
465 static void PrintLinkOptHints(MachOObjectFile *O) {
466 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
467 const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
468 uint32_t nloh = LohLC.datasize;
469 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
470 for (uint32_t i = 0; i < nloh;) {
472 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
474 outs() << " identifier " << identifier << " ";
477 switch (identifier) {
479 outs() << "AdrpAdrp\n";
482 outs() << "AdrpLdr\n";
485 outs() << "AdrpAddLdr\n";
488 outs() << "AdrpLdrGotLdr\n";
491 outs() << "AdrpAddStr\n";
494 outs() << "AdrpLdrGotStr\n";
497 outs() << "AdrpAdd\n";
500 outs() << "AdrpLdrGot\n";
503 outs() << "Unknown identifier value\n";
506 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
508 outs() << " narguments " << narguments << "\n";
512 for (uint32_t j = 0; j < narguments; j++) {
513 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
515 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
522 static void PrintDylibs(MachOObjectFile *O, bool JustId) {
524 for (const auto &Load : O->load_commands()) {
525 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
526 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
527 Load.C.cmd == MachO::LC_LOAD_DYLIB ||
528 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
529 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
530 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
531 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
532 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
533 if (dl.dylib.name < dl.cmdsize) {
534 const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
539 outs() << " (compatibility version "
540 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
541 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
542 << (dl.dylib.compatibility_version & 0xff) << ",";
543 outs() << " current version "
544 << ((dl.dylib.current_version >> 16) & 0xffff) << "."
545 << ((dl.dylib.current_version >> 8) & 0xff) << "."
546 << (dl.dylib.current_version & 0xff) << ")\n";
549 outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
550 if (Load.C.cmd == MachO::LC_ID_DYLIB)
551 outs() << "LC_ID_DYLIB ";
552 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
553 outs() << "LC_LOAD_DYLIB ";
554 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
555 outs() << "LC_LOAD_WEAK_DYLIB ";
556 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
557 outs() << "LC_LAZY_LOAD_DYLIB ";
558 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
559 outs() << "LC_REEXPORT_DYLIB ";
560 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
561 outs() << "LC_LOAD_UPWARD_DYLIB ";
564 outs() << "command " << Index++ << "\n";
570 typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
572 static void CreateSymbolAddressMap(MachOObjectFile *O,
573 SymbolAddressMap *AddrMap) {
574 // Create a map of symbol addresses to symbol names.
575 for (const SymbolRef &Symbol : O->symbols()) {
576 SymbolRef::Type ST = Symbol.getType();
577 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
578 ST == SymbolRef::ST_Other) {
579 uint64_t Address = Symbol.getValue();
580 ErrorOr<StringRef> SymNameOrErr = Symbol.getName();
581 if (std::error_code EC = SymNameOrErr.getError())
582 report_fatal_error(EC.message());
583 StringRef SymName = *SymNameOrErr;
584 if (!SymName.startswith(".objc"))
585 (*AddrMap)[Address] = SymName;
590 // GuessSymbolName is passed the address of what might be a symbol and a
591 // pointer to the SymbolAddressMap. It returns the name of a symbol
592 // with that address or nullptr if no symbol is found with that address.
593 static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
594 const char *SymbolName = nullptr;
595 // A DenseMap can't lookup up some values.
596 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
597 StringRef name = AddrMap->lookup(value);
599 SymbolName = name.data();
604 static void DumpCstringChar(const char c) {
608 outs().write_escaped(p);
611 static void DumpCstringSection(MachOObjectFile *O, const char *sect,
612 uint32_t sect_size, uint64_t sect_addr,
613 bool print_addresses) {
614 for (uint32_t i = 0; i < sect_size; i++) {
615 if (print_addresses) {
617 outs() << format("%016" PRIx64, sect_addr + i) << " ";
619 outs() << format("%08" PRIx64, sect_addr + i) << " ";
621 for (; i < sect_size && sect[i] != '\0'; i++)
622 DumpCstringChar(sect[i]);
623 if (i < sect_size && sect[i] == '\0')
628 static void DumpLiteral4(uint32_t l, float f) {
629 outs() << format("0x%08" PRIx32, l);
630 if ((l & 0x7f800000) != 0x7f800000)
631 outs() << format(" (%.16e)\n", f);
634 outs() << " (+Infinity)\n";
635 else if (l == 0xff800000)
636 outs() << " (-Infinity)\n";
637 else if ((l & 0x00400000) == 0x00400000)
638 outs() << " (non-signaling Not-a-Number)\n";
640 outs() << " (signaling Not-a-Number)\n";
644 static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
645 uint32_t sect_size, uint64_t sect_addr,
646 bool print_addresses) {
647 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
648 if (print_addresses) {
650 outs() << format("%016" PRIx64, sect_addr + i) << " ";
652 outs() << format("%08" PRIx64, sect_addr + i) << " ";
655 memcpy(&f, sect + i, sizeof(float));
656 if (O->isLittleEndian() != sys::IsLittleEndianHost)
657 sys::swapByteOrder(f);
659 memcpy(&l, sect + i, sizeof(uint32_t));
660 if (O->isLittleEndian() != sys::IsLittleEndianHost)
661 sys::swapByteOrder(l);
666 static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
668 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
670 Hi = (O->isLittleEndian()) ? l1 : l0;
671 Lo = (O->isLittleEndian()) ? l0 : l1;
673 // Hi is the high word, so this is equivalent to if(isfinite(d))
674 if ((Hi & 0x7ff00000) != 0x7ff00000)
675 outs() << format(" (%.16e)\n", d);
677 if (Hi == 0x7ff00000 && Lo == 0)
678 outs() << " (+Infinity)\n";
679 else if (Hi == 0xfff00000 && Lo == 0)
680 outs() << " (-Infinity)\n";
681 else if ((Hi & 0x00080000) == 0x00080000)
682 outs() << " (non-signaling Not-a-Number)\n";
684 outs() << " (signaling Not-a-Number)\n";
688 static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
689 uint32_t sect_size, uint64_t sect_addr,
690 bool print_addresses) {
691 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
692 if (print_addresses) {
694 outs() << format("%016" PRIx64, sect_addr + i) << " ";
696 outs() << format("%08" PRIx64, sect_addr + i) << " ";
699 memcpy(&d, sect + i, sizeof(double));
700 if (O->isLittleEndian() != sys::IsLittleEndianHost)
701 sys::swapByteOrder(d);
703 memcpy(&l0, sect + i, sizeof(uint32_t));
704 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
705 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
706 sys::swapByteOrder(l0);
707 sys::swapByteOrder(l1);
709 DumpLiteral8(O, l0, l1, d);
713 static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
714 outs() << format("0x%08" PRIx32, l0) << " ";
715 outs() << format("0x%08" PRIx32, l1) << " ";
716 outs() << format("0x%08" PRIx32, l2) << " ";
717 outs() << format("0x%08" PRIx32, l3) << "\n";
720 static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
721 uint32_t sect_size, uint64_t sect_addr,
722 bool print_addresses) {
723 for (uint32_t i = 0; i < sect_size; i += 16) {
724 if (print_addresses) {
726 outs() << format("%016" PRIx64, sect_addr + i) << " ";
728 outs() << format("%08" PRIx64, sect_addr + i) << " ";
730 uint32_t l0, l1, l2, l3;
731 memcpy(&l0, sect + i, sizeof(uint32_t));
732 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
733 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
734 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
735 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
736 sys::swapByteOrder(l0);
737 sys::swapByteOrder(l1);
738 sys::swapByteOrder(l2);
739 sys::swapByteOrder(l3);
741 DumpLiteral16(l0, l1, l2, l3);
745 static void DumpLiteralPointerSection(MachOObjectFile *O,
746 const SectionRef &Section,
747 const char *sect, uint32_t sect_size,
749 bool print_addresses) {
750 // Collect the literal sections in this Mach-O file.
751 std::vector<SectionRef> LiteralSections;
752 for (const SectionRef &Section : O->sections()) {
753 DataRefImpl Ref = Section.getRawDataRefImpl();
754 uint32_t section_type;
756 const MachO::section_64 Sec = O->getSection64(Ref);
757 section_type = Sec.flags & MachO::SECTION_TYPE;
759 const MachO::section Sec = O->getSection(Ref);
760 section_type = Sec.flags & MachO::SECTION_TYPE;
762 if (section_type == MachO::S_CSTRING_LITERALS ||
763 section_type == MachO::S_4BYTE_LITERALS ||
764 section_type == MachO::S_8BYTE_LITERALS ||
765 section_type == MachO::S_16BYTE_LITERALS)
766 LiteralSections.push_back(Section);
769 // Set the size of the literal pointer.
770 uint32_t lp_size = O->is64Bit() ? 8 : 4;
772 // Collect the external relocation symbols for the literal pointers.
773 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
774 for (const RelocationRef &Reloc : Section.relocations()) {
776 MachO::any_relocation_info RE;
777 bool isExtern = false;
778 Rel = Reloc.getRawDataRefImpl();
779 RE = O->getRelocation(Rel);
780 isExtern = O->getPlainRelocationExternal(RE);
782 uint64_t RelocOffset = Reloc.getOffset();
783 symbol_iterator RelocSym = Reloc.getSymbol();
784 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
787 array_pod_sort(Relocs.begin(), Relocs.end());
789 // Dump each literal pointer.
790 for (uint32_t i = 0; i < sect_size; i += lp_size) {
791 if (print_addresses) {
793 outs() << format("%016" PRIx64, sect_addr + i) << " ";
795 outs() << format("%08" PRIx64, sect_addr + i) << " ";
799 memcpy(&lp, sect + i, sizeof(uint64_t));
800 if (O->isLittleEndian() != sys::IsLittleEndianHost)
801 sys::swapByteOrder(lp);
804 memcpy(&li, sect + i, sizeof(uint32_t));
805 if (O->isLittleEndian() != sys::IsLittleEndianHost)
806 sys::swapByteOrder(li);
810 // First look for an external relocation entry for this literal pointer.
811 auto Reloc = std::find_if(
812 Relocs.begin(), Relocs.end(),
813 [&](const std::pair<uint64_t, SymbolRef> &P) { return P.first == i; });
814 if (Reloc != Relocs.end()) {
815 symbol_iterator RelocSym = Reloc->second;
816 ErrorOr<StringRef> SymName = RelocSym->getName();
817 if (std::error_code EC = SymName.getError())
818 report_fatal_error(EC.message());
819 outs() << "external relocation entry for symbol:" << *SymName << "\n";
823 // For local references see what the section the literal pointer points to.
824 auto Sect = std::find_if(LiteralSections.begin(), LiteralSections.end(),
825 [&](const SectionRef &R) {
826 return lp >= R.getAddress() &&
827 lp < R.getAddress() + R.getSize();
829 if (Sect == LiteralSections.end()) {
830 outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
834 uint64_t SectAddress = Sect->getAddress();
835 uint64_t SectSize = Sect->getSize();
838 Sect->getName(SectName);
839 DataRefImpl Ref = Sect->getRawDataRefImpl();
840 StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
841 outs() << SegmentName << ":" << SectName << ":";
843 uint32_t section_type;
845 const MachO::section_64 Sec = O->getSection64(Ref);
846 section_type = Sec.flags & MachO::SECTION_TYPE;
848 const MachO::section Sec = O->getSection(Ref);
849 section_type = Sec.flags & MachO::SECTION_TYPE;
853 Sect->getContents(BytesStr);
854 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
856 switch (section_type) {
857 case MachO::S_CSTRING_LITERALS:
858 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
860 DumpCstringChar(Contents[i]);
864 case MachO::S_4BYTE_LITERALS:
866 memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
868 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
869 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
870 sys::swapByteOrder(f);
871 sys::swapByteOrder(l);
875 case MachO::S_8BYTE_LITERALS: {
877 memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
879 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
880 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
882 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
883 sys::swapByteOrder(f);
884 sys::swapByteOrder(l0);
885 sys::swapByteOrder(l1);
887 DumpLiteral8(O, l0, l1, d);
890 case MachO::S_16BYTE_LITERALS: {
891 uint32_t l0, l1, l2, l3;
892 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
893 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
895 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
897 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
899 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
900 sys::swapByteOrder(l0);
901 sys::swapByteOrder(l1);
902 sys::swapByteOrder(l2);
903 sys::swapByteOrder(l3);
905 DumpLiteral16(l0, l1, l2, l3);
912 static void DumpInitTermPointerSection(MachOObjectFile *O, const char *sect,
913 uint32_t sect_size, uint64_t sect_addr,
914 SymbolAddressMap *AddrMap,
917 stride = (O->is64Bit()) ? sizeof(uint64_t) : sizeof(uint32_t);
918 for (uint32_t i = 0; i < sect_size; i += stride) {
919 const char *SymbolName = nullptr;
921 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
922 uint64_t pointer_value;
923 memcpy(&pointer_value, sect + i, stride);
924 if (O->isLittleEndian() != sys::IsLittleEndianHost)
925 sys::swapByteOrder(pointer_value);
926 outs() << format("0x%016" PRIx64, pointer_value);
928 SymbolName = GuessSymbolName(pointer_value, AddrMap);
930 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
931 uint32_t pointer_value;
932 memcpy(&pointer_value, sect + i, stride);
933 if (O->isLittleEndian() != sys::IsLittleEndianHost)
934 sys::swapByteOrder(pointer_value);
935 outs() << format("0x%08" PRIx32, pointer_value);
937 SymbolName = GuessSymbolName(pointer_value, AddrMap);
940 outs() << " " << SymbolName;
945 static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
946 uint32_t size, uint64_t addr) {
947 uint32_t cputype = O->getHeader().cputype;
948 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
950 for (uint32_t i = 0; i < size; i += j, addr += j) {
952 outs() << format("%016" PRIx64, addr) << "\t";
954 outs() << format("%08" PRIx64, addr) << "\t";
955 for (j = 0; j < 16 && i + j < size; j++) {
956 uint8_t byte_word = *(sect + i + j);
957 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
963 for (uint32_t i = 0; i < size; i += j, addr += j) {
965 outs() << format("%016" PRIx64, addr) << "\t";
967 outs() << format("%08" PRIx64, sect) << "\t";
968 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
969 j += sizeof(int32_t)) {
970 if (i + j + sizeof(int32_t) < size) {
972 memcpy(&long_word, sect + i + j, sizeof(int32_t));
973 if (O->isLittleEndian() != sys::IsLittleEndianHost)
974 sys::swapByteOrder(long_word);
975 outs() << format("%08" PRIx32, long_word) << " ";
977 for (uint32_t k = 0; i + j + k < size; k++) {
978 uint8_t byte_word = *(sect + i + j);
979 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
988 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
989 StringRef DisSegName, StringRef DisSectName);
990 static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
991 uint32_t size, uint32_t addr);
993 static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
995 SymbolAddressMap AddrMap;
997 CreateSymbolAddressMap(O, &AddrMap);
999 for (unsigned i = 0; i < FilterSections.size(); ++i) {
1000 StringRef DumpSection = FilterSections[i];
1001 std::pair<StringRef, StringRef> DumpSegSectName;
1002 DumpSegSectName = DumpSection.split(',');
1003 StringRef DumpSegName, DumpSectName;
1004 if (DumpSegSectName.second.size()) {
1005 DumpSegName = DumpSegSectName.first;
1006 DumpSectName = DumpSegSectName.second;
1009 DumpSectName = DumpSegSectName.first;
1011 for (const SectionRef &Section : O->sections()) {
1013 Section.getName(SectName);
1014 DataRefImpl Ref = Section.getRawDataRefImpl();
1015 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1016 if ((DumpSegName.empty() || SegName == DumpSegName) &&
1017 (SectName == DumpSectName)) {
1019 uint32_t section_flags;
1021 const MachO::section_64 Sec = O->getSection64(Ref);
1022 section_flags = Sec.flags;
1025 const MachO::section Sec = O->getSection(Ref);
1026 section_flags = Sec.flags;
1028 uint32_t section_type = section_flags & MachO::SECTION_TYPE;
1031 Section.getContents(BytesStr);
1032 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1033 uint32_t sect_size = BytesStr.size();
1034 uint64_t sect_addr = Section.getAddress();
1036 outs() << "Contents of (" << SegName << "," << SectName
1040 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1041 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1042 DisassembleMachO(Filename, O, SegName, SectName);
1045 if (SegName == "__TEXT" && SectName == "__info_plist") {
1049 if (SegName == "__OBJC" && SectName == "__protocol") {
1050 DumpProtocolSection(O, sect, sect_size, sect_addr);
1053 switch (section_type) {
1054 case MachO::S_REGULAR:
1055 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1057 case MachO::S_ZEROFILL:
1058 outs() << "zerofill section and has no contents in the file\n";
1060 case MachO::S_CSTRING_LITERALS:
1061 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1063 case MachO::S_4BYTE_LITERALS:
1064 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1066 case MachO::S_8BYTE_LITERALS:
1067 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1069 case MachO::S_16BYTE_LITERALS:
1070 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1072 case MachO::S_LITERAL_POINTERS:
1073 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
1076 case MachO::S_MOD_INIT_FUNC_POINTERS:
1077 case MachO::S_MOD_TERM_FUNC_POINTERS:
1078 DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap,
1082 outs() << "Unknown section type ("
1083 << format("0x%08" PRIx32, section_type) << ")\n";
1084 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1088 if (section_type == MachO::S_ZEROFILL)
1089 outs() << "zerofill section and has no contents in the file\n";
1091 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1098 static void DumpInfoPlistSectionContents(StringRef Filename,
1099 MachOObjectFile *O) {
1100 for (const SectionRef &Section : O->sections()) {
1102 Section.getName(SectName);
1103 DataRefImpl Ref = Section.getRawDataRefImpl();
1104 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1105 if (SegName == "__TEXT" && SectName == "__info_plist") {
1106 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
1108 Section.getContents(BytesStr);
1109 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1116 // checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1117 // and if it is and there is a list of architecture flags is specified then
1118 // check to make sure this Mach-O file is one of those architectures or all
1119 // architectures were specified. If not then an error is generated and this
1120 // routine returns false. Else it returns true.
1121 static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
1122 if (isa<MachOObjectFile>(O) && !ArchAll && ArchFlags.size() != 0) {
1123 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O);
1124 bool ArchFound = false;
1125 MachO::mach_header H;
1126 MachO::mach_header_64 H_64;
1128 if (MachO->is64Bit()) {
1129 H_64 = MachO->MachOObjectFile::getHeader64();
1130 T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype);
1132 H = MachO->MachOObjectFile::getHeader();
1133 T = MachOObjectFile::getArch(H.cputype, H.cpusubtype);
1136 for (i = 0; i < ArchFlags.size(); ++i) {
1137 if (ArchFlags[i] == T.getArchName())
1142 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1143 << "architecture: " + ArchFlags[i] + "\n";
1150 static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1152 // ProcessMachO() is passed a single opened Mach-O file, which may be an
1153 // archive member and or in a slice of a universal file. It prints the
1154 // the file name and header info and then processes it according to the
1155 // command line options.
1156 static void ProcessMachO(StringRef Filename, MachOObjectFile *MachOOF,
1157 StringRef ArchiveMemberName = StringRef(),
1158 StringRef ArchitectureName = StringRef()) {
1159 // If we are doing some processing here on the Mach-O file print the header
1160 // info. And don't print it otherwise like in the case of printing the
1161 // UniversalHeaders or ArchiveHeaders.
1162 if (Disassemble || PrivateHeaders || ExportsTrie || Rebase || Bind ||
1163 LazyBind || WeakBind || IndirectSymbols || DataInCode || LinkOptHints ||
1164 DylibsUsed || DylibId || ObjcMetaData || (FilterSections.size() != 0)) {
1166 if (!ArchiveMemberName.empty())
1167 outs() << '(' << ArchiveMemberName << ')';
1168 if (!ArchitectureName.empty())
1169 outs() << " (architecture " << ArchitectureName << ")";
1174 DisassembleMachO(Filename, MachOOF, "__TEXT", "__text");
1175 if (IndirectSymbols)
1176 PrintIndirectSymbols(MachOOF, !NonVerbose);
1178 PrintDataInCodeTable(MachOOF, !NonVerbose);
1180 PrintLinkOptHints(MachOOF);
1182 PrintRelocations(MachOOF);
1184 PrintSectionHeaders(MachOOF);
1185 if (SectionContents)
1186 PrintSectionContents(MachOOF);
1187 if (FilterSections.size() != 0)
1188 DumpSectionContents(Filename, MachOOF, !NonVerbose);
1190 DumpInfoPlistSectionContents(Filename, MachOOF);
1192 PrintDylibs(MachOOF, false);
1194 PrintDylibs(MachOOF, true);
1196 PrintSymbolTable(MachOOF);
1198 printMachOUnwindInfo(MachOOF);
1199 if (PrivateHeaders) {
1200 printMachOFileHeader(MachOOF);
1201 printMachOLoadCommands(MachOOF);
1203 if (FirstPrivateHeader)
1204 printMachOFileHeader(MachOOF);
1206 printObjcMetaData(MachOOF, !NonVerbose);
1208 printExportsTrie(MachOOF);
1210 printRebaseTable(MachOOF);
1212 printBindTable(MachOOF);
1214 printLazyBindTable(MachOOF);
1216 printWeakBindTable(MachOOF);
1219 // printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1220 static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
1221 outs() << " cputype (" << cputype << ")\n";
1222 outs() << " cpusubtype (" << cpusubtype << ")\n";
1225 // printCPUType() helps print_fat_headers by printing the cputype and
1226 // pusubtype (symbolically for the one's it knows about).
1227 static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
1229 case MachO::CPU_TYPE_I386:
1230 switch (cpusubtype) {
1231 case MachO::CPU_SUBTYPE_I386_ALL:
1232 outs() << " cputype CPU_TYPE_I386\n";
1233 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
1236 printUnknownCPUType(cputype, cpusubtype);
1240 case MachO::CPU_TYPE_X86_64:
1241 switch (cpusubtype) {
1242 case MachO::CPU_SUBTYPE_X86_64_ALL:
1243 outs() << " cputype CPU_TYPE_X86_64\n";
1244 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1246 case MachO::CPU_SUBTYPE_X86_64_H:
1247 outs() << " cputype CPU_TYPE_X86_64\n";
1248 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
1251 printUnknownCPUType(cputype, cpusubtype);
1255 case MachO::CPU_TYPE_ARM:
1256 switch (cpusubtype) {
1257 case MachO::CPU_SUBTYPE_ARM_ALL:
1258 outs() << " cputype CPU_TYPE_ARM\n";
1259 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
1261 case MachO::CPU_SUBTYPE_ARM_V4T:
1262 outs() << " cputype CPU_TYPE_ARM\n";
1263 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
1265 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1266 outs() << " cputype CPU_TYPE_ARM\n";
1267 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
1269 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1270 outs() << " cputype CPU_TYPE_ARM\n";
1271 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
1273 case MachO::CPU_SUBTYPE_ARM_V6:
1274 outs() << " cputype CPU_TYPE_ARM\n";
1275 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
1277 case MachO::CPU_SUBTYPE_ARM_V6M:
1278 outs() << " cputype CPU_TYPE_ARM\n";
1279 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
1281 case MachO::CPU_SUBTYPE_ARM_V7:
1282 outs() << " cputype CPU_TYPE_ARM\n";
1283 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
1285 case MachO::CPU_SUBTYPE_ARM_V7EM:
1286 outs() << " cputype CPU_TYPE_ARM\n";
1287 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
1289 case MachO::CPU_SUBTYPE_ARM_V7K:
1290 outs() << " cputype CPU_TYPE_ARM\n";
1291 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
1293 case MachO::CPU_SUBTYPE_ARM_V7M:
1294 outs() << " cputype CPU_TYPE_ARM\n";
1295 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
1297 case MachO::CPU_SUBTYPE_ARM_V7S:
1298 outs() << " cputype CPU_TYPE_ARM\n";
1299 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
1302 printUnknownCPUType(cputype, cpusubtype);
1306 case MachO::CPU_TYPE_ARM64:
1307 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1308 case MachO::CPU_SUBTYPE_ARM64_ALL:
1309 outs() << " cputype CPU_TYPE_ARM64\n";
1310 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
1313 printUnknownCPUType(cputype, cpusubtype);
1318 printUnknownCPUType(cputype, cpusubtype);
1323 static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
1325 outs() << "Fat headers\n";
1327 outs() << "fat_magic FAT_MAGIC\n";
1329 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
1331 uint32_t nfat_arch = UB->getNumberOfObjects();
1332 StringRef Buf = UB->getData();
1333 uint64_t size = Buf.size();
1334 uint64_t big_size = sizeof(struct MachO::fat_header) +
1335 nfat_arch * sizeof(struct MachO::fat_arch);
1336 outs() << "nfat_arch " << UB->getNumberOfObjects();
1338 outs() << " (malformed, contains zero architecture types)\n";
1339 else if (big_size > size)
1340 outs() << " (malformed, architectures past end of file)\n";
1344 for (uint32_t i = 0; i < nfat_arch; ++i) {
1345 MachOUniversalBinary::ObjectForArch OFA(UB, i);
1346 uint32_t cputype = OFA.getCPUType();
1347 uint32_t cpusubtype = OFA.getCPUSubType();
1348 outs() << "architecture ";
1349 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
1350 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
1351 uint32_t other_cputype = other_OFA.getCPUType();
1352 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
1353 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
1354 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
1355 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
1356 outs() << "(illegal duplicate architecture) ";
1361 outs() << OFA.getArchTypeName() << "\n";
1362 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1364 outs() << i << "\n";
1365 outs() << " cputype " << cputype << "\n";
1366 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
1370 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
1371 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
1373 outs() << " capabilities "
1374 << format("0x%" PRIx32,
1375 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
1376 outs() << " offset " << OFA.getOffset();
1377 if (OFA.getOffset() > size)
1378 outs() << " (past end of file)";
1379 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
1380 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
1382 outs() << " size " << OFA.getSize();
1383 big_size = OFA.getOffset() + OFA.getSize();
1384 if (big_size > size)
1385 outs() << " (past end of file)";
1387 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
1392 static void printArchiveChild(const Archive::Child &C, bool verbose,
1393 bool print_offset) {
1395 outs() << C.getChildOffset() << "\t";
1396 sys::fs::perms Mode = C.getAccessMode();
1398 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
1399 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
1401 outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
1402 outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
1403 outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
1404 outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
1405 outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
1406 outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
1407 outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
1408 outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
1409 outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
1411 outs() << format("0%o ", Mode);
1414 unsigned UID = C.getUID();
1415 outs() << format("%3d/", UID);
1416 unsigned GID = C.getGID();
1417 outs() << format("%-3d ", GID);
1418 ErrorOr<uint64_t> Size = C.getRawSize();
1419 if (std::error_code EC = Size.getError())
1420 report_fatal_error(EC.message());
1421 outs() << format("%5" PRId64, Size.get()) << " ";
1423 StringRef RawLastModified = C.getRawLastModified();
1426 if (RawLastModified.getAsInteger(10, Seconds))
1427 outs() << "(date: \"%s\" contains non-decimal chars) " << RawLastModified;
1429 // Since cime(3) returns a 26 character string of the form:
1430 // "Sun Sep 16 01:03:52 1973\n\0"
1431 // just print 24 characters.
1433 outs() << format("%.24s ", ctime(&t));
1436 outs() << RawLastModified << " ";
1440 ErrorOr<StringRef> NameOrErr = C.getName();
1441 if (NameOrErr.getError()) {
1442 StringRef RawName = C.getRawName();
1443 outs() << RawName << "\n";
1445 StringRef Name = NameOrErr.get();
1446 outs() << Name << "\n";
1449 StringRef RawName = C.getRawName();
1450 outs() << RawName << "\n";
1454 static void printArchiveHeaders(Archive *A, bool verbose, bool print_offset) {
1455 for (Archive::child_iterator I = A->child_begin(false), E = A->child_end();
1457 if (std::error_code EC = I->getError())
1458 report_fatal_error(EC.message());
1459 const Archive::Child &C = **I;
1460 printArchiveChild(C, verbose, print_offset);
1464 // ParseInputMachO() parses the named Mach-O file in Filename and handles the
1465 // -arch flags selecting just those slices as specified by them and also parses
1466 // archive files. Then for each individual Mach-O file ProcessMachO() is
1467 // called to process the file based on the command line options.
1468 void llvm::ParseInputMachO(StringRef Filename) {
1469 // Check for -arch all and verifiy the -arch flags are valid.
1470 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1471 if (ArchFlags[i] == "all") {
1474 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
1475 errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
1476 "'for the -arch option\n";
1482 // Attempt to open the binary.
1483 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
1484 if (std::error_code EC = BinaryOrErr.getError())
1485 report_error(Filename, EC);
1486 Binary &Bin = *BinaryOrErr.get().getBinary();
1488 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1489 outs() << "Archive : " << Filename << "\n";
1491 printArchiveHeaders(A, !NonVerbose, ArchiveMemberOffsets);
1492 for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
1494 if (std::error_code EC = I->getError())
1495 report_error(Filename, EC);
1497 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1498 if (ChildOrErr.getError())
1500 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1501 if (!checkMachOAndArchFlags(O, Filename))
1503 ProcessMachO(Filename, O, O->getFileName());
1508 if (UniversalHeaders) {
1509 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
1510 printMachOUniversalHeaders(UB, !NonVerbose);
1512 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1513 // If we have a list of architecture flags specified dump only those.
1514 if (!ArchAll && ArchFlags.size() != 0) {
1515 // Look for a slice in the universal binary that matches each ArchFlag.
1517 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1519 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1520 E = UB->end_objects();
1522 if (ArchFlags[i] == I->getArchTypeName()) {
1524 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
1525 I->getAsObjectFile();
1526 std::string ArchitectureName = "";
1527 if (ArchFlags.size() > 1)
1528 ArchitectureName = I->getArchTypeName();
1530 ObjectFile &O = *ObjOrErr.get();
1531 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
1532 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
1533 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1534 I->getAsArchive()) {
1535 std::unique_ptr<Archive> &A = *AOrErr;
1536 outs() << "Archive : " << Filename;
1537 if (!ArchitectureName.empty())
1538 outs() << " (architecture " << ArchitectureName << ")";
1541 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
1542 for (Archive::child_iterator AI = A->child_begin(),
1543 AE = A->child_end();
1545 if (std::error_code EC = AI->getError())
1546 report_error(Filename, EC);
1547 auto &C = AI->get();
1548 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1549 if (ChildOrErr.getError())
1551 if (MachOObjectFile *O =
1552 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
1553 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
1559 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1560 << "architecture: " + ArchFlags[i] + "\n";
1566 // No architecture flags were specified so if this contains a slice that
1567 // matches the host architecture dump only that.
1569 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1570 E = UB->end_objects();
1572 if (MachOObjectFile::getHostArch().getArchName() ==
1573 I->getArchTypeName()) {
1574 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1575 std::string ArchiveName;
1576 ArchiveName.clear();
1578 ObjectFile &O = *ObjOrErr.get();
1579 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
1580 ProcessMachO(Filename, MachOOF);
1581 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1582 I->getAsArchive()) {
1583 std::unique_ptr<Archive> &A = *AOrErr;
1584 outs() << "Archive : " << Filename << "\n";
1586 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
1587 for (Archive::child_iterator AI = A->child_begin(),
1588 AE = A->child_end();
1590 if (std::error_code EC = AI->getError())
1591 report_error(Filename, EC);
1592 auto &C = AI->get();
1593 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1594 if (ChildOrErr.getError())
1596 if (MachOObjectFile *O =
1597 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
1598 ProcessMachO(Filename, O, O->getFileName());
1605 // Either all architectures have been specified or none have been specified
1606 // and this does not contain the host architecture so dump all the slices.
1607 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1608 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1609 E = UB->end_objects();
1611 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1612 std::string ArchitectureName = "";
1613 if (moreThanOneArch)
1614 ArchitectureName = I->getArchTypeName();
1616 ObjectFile &Obj = *ObjOrErr.get();
1617 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
1618 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
1619 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
1620 std::unique_ptr<Archive> &A = *AOrErr;
1621 outs() << "Archive : " << Filename;
1622 if (!ArchitectureName.empty())
1623 outs() << " (architecture " << ArchitectureName << ")";
1626 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
1627 for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
1629 if (std::error_code EC = AI->getError())
1630 report_error(Filename, EC);
1631 auto &C = AI->get();
1632 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1633 if (ChildOrErr.getError())
1635 if (MachOObjectFile *O =
1636 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1637 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
1638 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
1646 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
1647 if (!checkMachOAndArchFlags(O, Filename))
1649 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
1650 ProcessMachO(Filename, MachOOF);
1652 errs() << "llvm-objdump: '" << Filename << "': "
1653 << "Object is not a Mach-O file type.\n";
1655 report_error(Filename, object_error::invalid_file_type);
1658 typedef std::pair<uint64_t, const char *> BindInfoEntry;
1659 typedef std::vector<BindInfoEntry> BindTable;
1660 typedef BindTable::iterator bind_table_iterator;
1662 // The block of info used by the Symbolizer call backs.
1663 struct DisassembleInfo {
1667 SymbolAddressMap *AddrMap;
1668 std::vector<SectionRef> *Sections;
1669 const char *class_name;
1670 const char *selector_name;
1672 char *demangled_name;
1675 BindTable *bindtable;
1679 // SymbolizerGetOpInfo() is the operand information call back function.
1680 // This is called to get the symbolic information for operand(s) of an
1681 // instruction when it is being done. This routine does this from
1682 // the relocation information, symbol table, etc. That block of information
1683 // is a pointer to the struct DisassembleInfo that was passed when the
1684 // disassembler context was created and passed to back to here when
1685 // called back by the disassembler for instruction operands that could have
1686 // relocation information. The address of the instruction containing operand is
1687 // at the Pc parameter. The immediate value the operand has is passed in
1688 // op_info->Value and is at Offset past the start of the instruction and has a
1689 // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
1690 // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
1691 // names and addends of the symbolic expression to add for the operand. The
1692 // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
1693 // information is returned then this function returns 1 else it returns 0.
1694 static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
1695 uint64_t Size, int TagType, void *TagBuf) {
1696 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
1697 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
1698 uint64_t value = op_info->Value;
1700 // Make sure all fields returned are zero if we don't set them.
1701 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
1702 op_info->Value = value;
1704 // If the TagType is not the value 1 which it code knows about or if no
1705 // verbose symbolic information is wanted then just return 0, indicating no
1706 // information is being returned.
1707 if (TagType != 1 || !info->verbose)
1710 unsigned int Arch = info->O->getArch();
1711 if (Arch == Triple::x86) {
1712 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1714 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
1716 // Search the external relocation entries of a fully linked image
1717 // (if any) for an entry that matches this segment offset.
1718 // uint32_t seg_offset = (Pc + Offset);
1721 // In MH_OBJECT filetypes search the section's relocation entries (if any)
1722 // for an entry for this section offset.
1723 uint32_t sect_addr = info->S.getAddress();
1724 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1725 bool reloc_found = false;
1727 MachO::any_relocation_info RE;
1728 bool isExtern = false;
1730 bool r_scattered = false;
1731 uint32_t r_value, pair_r_value, r_type;
1732 for (const RelocationRef &Reloc : info->S.relocations()) {
1733 uint64_t RelocOffset = Reloc.getOffset();
1734 if (RelocOffset == sect_offset) {
1735 Rel = Reloc.getRawDataRefImpl();
1736 RE = info->O->getRelocation(Rel);
1737 r_type = info->O->getAnyRelocationType(RE);
1738 r_scattered = info->O->isRelocationScattered(RE);
1740 r_value = info->O->getScatteredRelocationValue(RE);
1741 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1742 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
1743 DataRefImpl RelNext = Rel;
1744 info->O->moveRelocationNext(RelNext);
1745 MachO::any_relocation_info RENext;
1746 RENext = info->O->getRelocation(RelNext);
1747 if (info->O->isRelocationScattered(RENext))
1748 pair_r_value = info->O->getScatteredRelocationValue(RENext);
1753 isExtern = info->O->getPlainRelocationExternal(RE);
1755 symbol_iterator RelocSym = Reloc.getSymbol();
1763 if (reloc_found && isExtern) {
1764 ErrorOr<StringRef> SymName = Symbol.getName();
1765 if (std::error_code EC = SymName.getError())
1766 report_fatal_error(EC.message());
1767 const char *name = SymName->data();
1768 op_info->AddSymbol.Present = 1;
1769 op_info->AddSymbol.Name = name;
1770 // For i386 extern relocation entries the value in the instruction is
1771 // the offset from the symbol, and value is already set in op_info->Value.
1774 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1775 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
1776 const char *add = GuessSymbolName(r_value, info->AddrMap);
1777 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
1778 uint32_t offset = value - (r_value - pair_r_value);
1779 op_info->AddSymbol.Present = 1;
1781 op_info->AddSymbol.Name = add;
1783 op_info->AddSymbol.Value = r_value;
1784 op_info->SubtractSymbol.Present = 1;
1786 op_info->SubtractSymbol.Name = sub;
1788 op_info->SubtractSymbol.Value = pair_r_value;
1789 op_info->Value = offset;
1794 if (Arch == Triple::x86_64) {
1795 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1797 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
1799 // Search the external relocation entries of a fully linked image
1800 // (if any) for an entry that matches this segment offset.
1801 // uint64_t seg_offset = (Pc + Offset);
1804 // In MH_OBJECT filetypes search the section's relocation entries (if any)
1805 // for an entry for this section offset.
1806 uint64_t sect_addr = info->S.getAddress();
1807 uint64_t sect_offset = (Pc + Offset) - sect_addr;
1808 bool reloc_found = false;
1810 MachO::any_relocation_info RE;
1811 bool isExtern = false;
1813 for (const RelocationRef &Reloc : info->S.relocations()) {
1814 uint64_t RelocOffset = Reloc.getOffset();
1815 if (RelocOffset == sect_offset) {
1816 Rel = Reloc.getRawDataRefImpl();
1817 RE = info->O->getRelocation(Rel);
1818 // NOTE: Scattered relocations don't exist on x86_64.
1819 isExtern = info->O->getPlainRelocationExternal(RE);
1821 symbol_iterator RelocSym = Reloc.getSymbol();
1828 if (reloc_found && isExtern) {
1829 // The Value passed in will be adjusted by the Pc if the instruction
1830 // adds the Pc. But for x86_64 external relocation entries the Value
1831 // is the offset from the external symbol.
1832 if (info->O->getAnyRelocationPCRel(RE))
1833 op_info->Value -= Pc + Offset + Size;
1834 ErrorOr<StringRef> SymName = Symbol.getName();
1835 if (std::error_code EC = SymName.getError())
1836 report_fatal_error(EC.message());
1837 const char *name = SymName->data();
1838 unsigned Type = info->O->getAnyRelocationType(RE);
1839 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
1840 DataRefImpl RelNext = Rel;
1841 info->O->moveRelocationNext(RelNext);
1842 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
1843 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
1844 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
1845 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
1846 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
1847 op_info->SubtractSymbol.Present = 1;
1848 op_info->SubtractSymbol.Name = name;
1849 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
1850 Symbol = *RelocSymNext;
1851 ErrorOr<StringRef> SymNameNext = Symbol.getName();
1852 if (std::error_code EC = SymNameNext.getError())
1853 report_fatal_error(EC.message());
1854 name = SymNameNext->data();
1857 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
1858 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
1859 op_info->AddSymbol.Present = 1;
1860 op_info->AddSymbol.Name = name;
1865 if (Arch == Triple::arm) {
1866 if (Offset != 0 || (Size != 4 && Size != 2))
1868 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
1870 // Search the external relocation entries of a fully linked image
1871 // (if any) for an entry that matches this segment offset.
1872 // uint32_t seg_offset = (Pc + Offset);
1875 // In MH_OBJECT filetypes search the section's relocation entries (if any)
1876 // for an entry for this section offset.
1877 uint32_t sect_addr = info->S.getAddress();
1878 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1880 MachO::any_relocation_info RE;
1881 bool isExtern = false;
1883 bool r_scattered = false;
1884 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
1886 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
1887 [&](const RelocationRef &Reloc) {
1888 uint64_t RelocOffset = Reloc.getOffset();
1889 return RelocOffset == sect_offset;
1892 if (Reloc == info->S.relocations().end())
1895 Rel = Reloc->getRawDataRefImpl();
1896 RE = info->O->getRelocation(Rel);
1897 r_length = info->O->getAnyRelocationLength(RE);
1898 r_scattered = info->O->isRelocationScattered(RE);
1900 r_value = info->O->getScatteredRelocationValue(RE);
1901 r_type = info->O->getScatteredRelocationType(RE);
1903 r_type = info->O->getAnyRelocationType(RE);
1904 isExtern = info->O->getPlainRelocationExternal(RE);
1906 symbol_iterator RelocSym = Reloc->getSymbol();
1910 if (r_type == MachO::ARM_RELOC_HALF ||
1911 r_type == MachO::ARM_RELOC_SECTDIFF ||
1912 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
1913 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1914 DataRefImpl RelNext = Rel;
1915 info->O->moveRelocationNext(RelNext);
1916 MachO::any_relocation_info RENext;
1917 RENext = info->O->getRelocation(RelNext);
1918 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
1919 if (info->O->isRelocationScattered(RENext))
1920 pair_r_value = info->O->getScatteredRelocationValue(RENext);
1924 ErrorOr<StringRef> SymName = Symbol.getName();
1925 if (std::error_code EC = SymName.getError())
1926 report_fatal_error(EC.message());
1927 const char *name = SymName->data();
1928 op_info->AddSymbol.Present = 1;
1929 op_info->AddSymbol.Name = name;
1931 case MachO::ARM_RELOC_HALF:
1932 if ((r_length & 0x1) == 1) {
1933 op_info->Value = value << 16 | other_half;
1934 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1936 op_info->Value = other_half << 16 | value;
1937 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
1945 // If we have a branch that is not an external relocation entry then
1946 // return 0 so the code in tryAddingSymbolicOperand() can use the
1947 // SymbolLookUp call back with the branch target address to look up the
1948 // symbol and possiblity add an annotation for a symbol stub.
1949 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
1950 r_type == MachO::ARM_THUMB_RELOC_BR22))
1953 uint32_t offset = 0;
1954 if (r_type == MachO::ARM_RELOC_HALF ||
1955 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1956 if ((r_length & 0x1) == 1)
1957 value = value << 16 | other_half;
1959 value = other_half << 16 | value;
1961 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
1962 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
1963 offset = value - r_value;
1967 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1968 if ((r_length & 0x1) == 1)
1969 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1971 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
1972 const char *add = GuessSymbolName(r_value, info->AddrMap);
1973 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
1974 int32_t offset = value - (r_value - pair_r_value);
1975 op_info->AddSymbol.Present = 1;
1977 op_info->AddSymbol.Name = add;
1979 op_info->AddSymbol.Value = r_value;
1980 op_info->SubtractSymbol.Present = 1;
1982 op_info->SubtractSymbol.Name = sub;
1984 op_info->SubtractSymbol.Value = pair_r_value;
1985 op_info->Value = offset;
1989 op_info->AddSymbol.Present = 1;
1990 op_info->Value = offset;
1991 if (r_type == MachO::ARM_RELOC_HALF) {
1992 if ((r_length & 0x1) == 1)
1993 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1995 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
1997 const char *add = GuessSymbolName(value, info->AddrMap);
1998 if (add != nullptr) {
1999 op_info->AddSymbol.Name = add;
2002 op_info->AddSymbol.Value = value;
2005 if (Arch == Triple::aarch64) {
2006 if (Offset != 0 || Size != 4)
2008 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2010 // Search the external relocation entries of a fully linked image
2011 // (if any) for an entry that matches this segment offset.
2012 // uint64_t seg_offset = (Pc + Offset);
2015 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2016 // for an entry for this section offset.
2017 uint64_t sect_addr = info->S.getAddress();
2018 uint64_t sect_offset = (Pc + Offset) - sect_addr;
2020 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
2021 [&](const RelocationRef &Reloc) {
2022 uint64_t RelocOffset = Reloc.getOffset();
2023 return RelocOffset == sect_offset;
2026 if (Reloc == info->S.relocations().end())
2029 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2030 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2031 uint32_t r_type = info->O->getAnyRelocationType(RE);
2032 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2033 DataRefImpl RelNext = Rel;
2034 info->O->moveRelocationNext(RelNext);
2035 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2037 value = info->O->getPlainRelocationSymbolNum(RENext);
2038 op_info->Value = value;
2041 // NOTE: Scattered relocations don't exist on arm64.
2042 if (!info->O->getPlainRelocationExternal(RE))
2044 ErrorOr<StringRef> SymName = Reloc->getSymbol()->getName();
2045 if (std::error_code EC = SymName.getError())
2046 report_fatal_error(EC.message());
2047 const char *name = SymName->data();
2048 op_info->AddSymbol.Present = 1;
2049 op_info->AddSymbol.Name = name;
2052 case MachO::ARM64_RELOC_PAGE21:
2054 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2056 case MachO::ARM64_RELOC_PAGEOFF12:
2058 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2060 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2062 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2064 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2066 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2068 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2069 /* @tvlppage is not implemented in llvm-mc */
2070 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2072 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2073 /* @tvlppageoff is not implemented in llvm-mc */
2074 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2077 case MachO::ARM64_RELOC_BRANCH26:
2078 op_info->VariantKind = LLVMDisassembler_VariantKind_None;
2086 // GuessCstringPointer is passed the address of what might be a pointer to a
2087 // literal string in a cstring section. If that address is in a cstring section
2088 // it returns a pointer to that string. Else it returns nullptr.
2089 static const char *GuessCstringPointer(uint64_t ReferenceValue,
2090 struct DisassembleInfo *info) {
2091 for (const auto &Load : info->O->load_commands()) {
2092 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2093 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2094 for (unsigned J = 0; J < Seg.nsects; ++J) {
2095 MachO::section_64 Sec = info->O->getSection64(Load, J);
2096 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2097 if (section_type == MachO::S_CSTRING_LITERALS &&
2098 ReferenceValue >= Sec.addr &&
2099 ReferenceValue < Sec.addr + Sec.size) {
2100 uint64_t sect_offset = ReferenceValue - Sec.addr;
2101 uint64_t object_offset = Sec.offset + sect_offset;
2102 StringRef MachOContents = info->O->getData();
2103 uint64_t object_size = MachOContents.size();
2104 const char *object_addr = (const char *)MachOContents.data();
2105 if (object_offset < object_size) {
2106 const char *name = object_addr + object_offset;
2113 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2114 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2115 for (unsigned J = 0; J < Seg.nsects; ++J) {
2116 MachO::section Sec = info->O->getSection(Load, J);
2117 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2118 if (section_type == MachO::S_CSTRING_LITERALS &&
2119 ReferenceValue >= Sec.addr &&
2120 ReferenceValue < Sec.addr + Sec.size) {
2121 uint64_t sect_offset = ReferenceValue - Sec.addr;
2122 uint64_t object_offset = Sec.offset + sect_offset;
2123 StringRef MachOContents = info->O->getData();
2124 uint64_t object_size = MachOContents.size();
2125 const char *object_addr = (const char *)MachOContents.data();
2126 if (object_offset < object_size) {
2127 const char *name = object_addr + object_offset;
2139 // GuessIndirectSymbol returns the name of the indirect symbol for the
2140 // ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
2141 // an address of a symbol stub or a lazy or non-lazy pointer to associate the
2142 // symbol name being referenced by the stub or pointer.
2143 static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2144 struct DisassembleInfo *info) {
2145 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2146 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
2147 for (const auto &Load : info->O->load_commands()) {
2148 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2149 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2150 for (unsigned J = 0; J < Seg.nsects; ++J) {
2151 MachO::section_64 Sec = info->O->getSection64(Load, J);
2152 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2153 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2154 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2155 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2156 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2157 section_type == MachO::S_SYMBOL_STUBS) &&
2158 ReferenceValue >= Sec.addr &&
2159 ReferenceValue < Sec.addr + Sec.size) {
2161 if (section_type == MachO::S_SYMBOL_STUBS)
2162 stride = Sec.reserved2;
2167 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2168 if (index < Dysymtab.nindirectsyms) {
2169 uint32_t indirect_symbol =
2170 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
2171 if (indirect_symbol < Symtab.nsyms) {
2172 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2173 SymbolRef Symbol = *Sym;
2174 ErrorOr<StringRef> SymName = Symbol.getName();
2175 if (std::error_code EC = SymName.getError())
2176 report_fatal_error(EC.message());
2177 const char *name = SymName->data();
2183 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2184 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2185 for (unsigned J = 0; J < Seg.nsects; ++J) {
2186 MachO::section Sec = info->O->getSection(Load, J);
2187 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2188 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2189 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2190 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2191 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2192 section_type == MachO::S_SYMBOL_STUBS) &&
2193 ReferenceValue >= Sec.addr &&
2194 ReferenceValue < Sec.addr + Sec.size) {
2196 if (section_type == MachO::S_SYMBOL_STUBS)
2197 stride = Sec.reserved2;
2202 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2203 if (index < Dysymtab.nindirectsyms) {
2204 uint32_t indirect_symbol =
2205 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
2206 if (indirect_symbol < Symtab.nsyms) {
2207 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2208 SymbolRef Symbol = *Sym;
2209 ErrorOr<StringRef> SymName = Symbol.getName();
2210 if (std::error_code EC = SymName.getError())
2211 report_fatal_error(EC.message());
2212 const char *name = SymName->data();
2223 // method_reference() is called passing it the ReferenceName that might be
2224 // a reference it to an Objective-C method call. If so then it allocates and
2225 // assembles a method call string with the values last seen and saved in
2226 // the DisassembleInfo's class_name and selector_name fields. This is saved
2227 // into the method field of the info and any previous string is free'ed.
2228 // Then the class_name field in the info is set to nullptr. The method call
2229 // string is set into ReferenceName and ReferenceType is set to
2230 // LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
2231 // then both ReferenceType and ReferenceName are left unchanged.
2232 static void method_reference(struct DisassembleInfo *info,
2233 uint64_t *ReferenceType,
2234 const char **ReferenceName) {
2235 unsigned int Arch = info->O->getArch();
2236 if (*ReferenceName != nullptr) {
2237 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
2238 if (info->selector_name != nullptr) {
2239 if (info->method != nullptr)
2241 if (info->class_name != nullptr) {
2242 info->method = (char *)malloc(5 + strlen(info->class_name) +
2243 strlen(info->selector_name));
2244 if (info->method != nullptr) {
2245 strcpy(info->method, "+[");
2246 strcat(info->method, info->class_name);
2247 strcat(info->method, " ");
2248 strcat(info->method, info->selector_name);
2249 strcat(info->method, "]");
2250 *ReferenceName = info->method;
2251 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2254 info->method = (char *)malloc(9 + strlen(info->selector_name));
2255 if (info->method != nullptr) {
2256 if (Arch == Triple::x86_64)
2257 strcpy(info->method, "-[%rdi ");
2258 else if (Arch == Triple::aarch64)
2259 strcpy(info->method, "-[x0 ");
2261 strcpy(info->method, "-[r? ");
2262 strcat(info->method, info->selector_name);
2263 strcat(info->method, "]");
2264 *ReferenceName = info->method;
2265 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2268 info->class_name = nullptr;
2270 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
2271 if (info->selector_name != nullptr) {
2272 if (info->method != nullptr)
2274 info->method = (char *)malloc(17 + strlen(info->selector_name));
2275 if (info->method != nullptr) {
2276 if (Arch == Triple::x86_64)
2277 strcpy(info->method, "-[[%rdi super] ");
2278 else if (Arch == Triple::aarch64)
2279 strcpy(info->method, "-[[x0 super] ");
2281 strcpy(info->method, "-[[r? super] ");
2282 strcat(info->method, info->selector_name);
2283 strcat(info->method, "]");
2284 *ReferenceName = info->method;
2285 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2287 info->class_name = nullptr;
2293 // GuessPointerPointer() is passed the address of what might be a pointer to
2294 // a reference to an Objective-C class, selector, message ref or cfstring.
2295 // If so the value of the pointer is returned and one of the booleans are set
2296 // to true. If not zero is returned and all the booleans are set to false.
2297 static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
2298 struct DisassembleInfo *info,
2299 bool &classref, bool &selref, bool &msgref,
2305 for (const auto &Load : info->O->load_commands()) {
2306 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2307 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2308 for (unsigned J = 0; J < Seg.nsects; ++J) {
2309 MachO::section_64 Sec = info->O->getSection64(Load, J);
2310 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
2311 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2312 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
2313 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
2314 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
2315 ReferenceValue >= Sec.addr &&
2316 ReferenceValue < Sec.addr + Sec.size) {
2317 uint64_t sect_offset = ReferenceValue - Sec.addr;
2318 uint64_t object_offset = Sec.offset + sect_offset;
2319 StringRef MachOContents = info->O->getData();
2320 uint64_t object_size = MachOContents.size();
2321 const char *object_addr = (const char *)MachOContents.data();
2322 if (object_offset < object_size) {
2323 uint64_t pointer_value;
2324 memcpy(&pointer_value, object_addr + object_offset,
2326 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2327 sys::swapByteOrder(pointer_value);
2328 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
2330 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2331 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
2333 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
2334 ReferenceValue + 8 < Sec.addr + Sec.size) {
2336 memcpy(&pointer_value, object_addr + object_offset + 8,
2338 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2339 sys::swapByteOrder(pointer_value);
2340 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
2342 return pointer_value;
2349 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
2354 // get_pointer_64 returns a pointer to the bytes in the object file at the
2355 // Address from a section in the Mach-O file. And indirectly returns the
2356 // offset into the section, number of bytes left in the section past the offset
2357 // and which section is was being referenced. If the Address is not in a
2358 // section nullptr is returned.
2359 static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
2360 uint32_t &left, SectionRef &S,
2361 DisassembleInfo *info,
2362 bool objc_only = false) {
2366 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
2367 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
2368 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
2373 ((*(info->Sections))[SectIdx]).getName(SectName);
2374 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
2375 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
2376 if (SegName != "__OBJC" && SectName != "__cstring")
2379 if (Address >= SectAddress && Address < SectAddress + SectSize) {
2380 S = (*(info->Sections))[SectIdx];
2381 offset = Address - SectAddress;
2382 left = SectSize - offset;
2383 StringRef SectContents;
2384 ((*(info->Sections))[SectIdx]).getContents(SectContents);
2385 return SectContents.data() + offset;
2391 static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
2392 uint32_t &left, SectionRef &S,
2393 DisassembleInfo *info,
2394 bool objc_only = false) {
2395 return get_pointer_64(Address, offset, left, S, info, objc_only);
2398 // get_symbol_64() returns the name of a symbol (or nullptr) and the address of
2399 // the symbol indirectly through n_value. Based on the relocation information
2400 // for the specified section offset in the specified section reference.
2401 // If no relocation information is found and a non-zero ReferenceValue for the
2402 // symbol is passed, look up that address in the info's AddrMap.
2403 static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
2404 DisassembleInfo *info, uint64_t &n_value,
2405 uint64_t ReferenceValue = 0) {
2410 // See if there is an external relocation entry at the sect_offset.
2411 bool reloc_found = false;
2413 MachO::any_relocation_info RE;
2414 bool isExtern = false;
2416 for (const RelocationRef &Reloc : S.relocations()) {
2417 uint64_t RelocOffset = Reloc.getOffset();
2418 if (RelocOffset == sect_offset) {
2419 Rel = Reloc.getRawDataRefImpl();
2420 RE = info->O->getRelocation(Rel);
2421 if (info->O->isRelocationScattered(RE))
2423 isExtern = info->O->getPlainRelocationExternal(RE);
2425 symbol_iterator RelocSym = Reloc.getSymbol();
2432 // If there is an external relocation entry for a symbol in this section
2433 // at this section_offset then use that symbol's value for the n_value
2434 // and return its name.
2435 const char *SymbolName = nullptr;
2436 if (reloc_found && isExtern) {
2437 n_value = Symbol.getValue();
2438 ErrorOr<StringRef> NameOrError = Symbol.getName();
2439 if (std::error_code EC = NameOrError.getError())
2440 report_fatal_error(EC.message());
2441 StringRef Name = *NameOrError;
2442 if (!Name.empty()) {
2443 SymbolName = Name.data();
2448 // TODO: For fully linked images, look through the external relocation
2449 // entries off the dynamic symtab command. For these the r_offset is from the
2450 // start of the first writeable segment in the Mach-O file. So the offset
2451 // to this section from that segment is passed to this routine by the caller,
2452 // as the database_offset. Which is the difference of the section's starting
2453 // address and the first writable segment.
2455 // NOTE: need add passing the database_offset to this routine.
2457 // We did not find an external relocation entry so look up the ReferenceValue
2458 // as an address of a symbol and if found return that symbol's name.
2459 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
2464 static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
2465 DisassembleInfo *info,
2466 uint32_t ReferenceValue) {
2468 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
2471 // These are structs in the Objective-C meta data and read to produce the
2472 // comments for disassembly. While these are part of the ABI they are no
2473 // public defintions. So the are here not in include/llvm/Support/MachO.h .
2475 // The cfstring object in a 64-bit Mach-O file.
2476 struct cfstring64_t {
2477 uint64_t isa; // class64_t * (64-bit pointer)
2478 uint64_t flags; // flag bits
2479 uint64_t characters; // char * (64-bit pointer)
2480 uint64_t length; // number of non-NULL characters in above
2483 // The class object in a 64-bit Mach-O file.
2485 uint64_t isa; // class64_t * (64-bit pointer)
2486 uint64_t superclass; // class64_t * (64-bit pointer)
2487 uint64_t cache; // Cache (64-bit pointer)
2488 uint64_t vtable; // IMP * (64-bit pointer)
2489 uint64_t data; // class_ro64_t * (64-bit pointer)
2493 uint32_t isa; /* class32_t * (32-bit pointer) */
2494 uint32_t superclass; /* class32_t * (32-bit pointer) */
2495 uint32_t cache; /* Cache (32-bit pointer) */
2496 uint32_t vtable; /* IMP * (32-bit pointer) */
2497 uint32_t data; /* class_ro32_t * (32-bit pointer) */
2500 struct class_ro64_t {
2502 uint32_t instanceStart;
2503 uint32_t instanceSize;
2505 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
2506 uint64_t name; // const char * (64-bit pointer)
2507 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
2508 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
2509 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
2510 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
2511 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
2514 struct class_ro32_t {
2516 uint32_t instanceStart;
2517 uint32_t instanceSize;
2518 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
2519 uint32_t name; /* const char * (32-bit pointer) */
2520 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
2521 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
2522 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
2523 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
2524 uint32_t baseProperties; /* const struct objc_property_list *
2528 /* Values for class_ro{64,32}_t->flags */
2529 #define RO_META (1 << 0)
2530 #define RO_ROOT (1 << 1)
2531 #define RO_HAS_CXX_STRUCTORS (1 << 2)
2533 struct method_list64_t {
2536 /* struct method64_t first; These structures follow inline */
2539 struct method_list32_t {
2542 /* struct method32_t first; These structures follow inline */
2546 uint64_t name; /* SEL (64-bit pointer) */
2547 uint64_t types; /* const char * (64-bit pointer) */
2548 uint64_t imp; /* IMP (64-bit pointer) */
2552 uint32_t name; /* SEL (32-bit pointer) */
2553 uint32_t types; /* const char * (32-bit pointer) */
2554 uint32_t imp; /* IMP (32-bit pointer) */
2557 struct protocol_list64_t {
2558 uint64_t count; /* uintptr_t (a 64-bit value) */
2559 /* struct protocol64_t * list[0]; These pointers follow inline */
2562 struct protocol_list32_t {
2563 uint32_t count; /* uintptr_t (a 32-bit value) */
2564 /* struct protocol32_t * list[0]; These pointers follow inline */
2567 struct protocol64_t {
2568 uint64_t isa; /* id * (64-bit pointer) */
2569 uint64_t name; /* const char * (64-bit pointer) */
2570 uint64_t protocols; /* struct protocol_list64_t *
2572 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
2573 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
2574 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
2575 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
2576 uint64_t instanceProperties; /* struct objc_property_list *
2580 struct protocol32_t {
2581 uint32_t isa; /* id * (32-bit pointer) */
2582 uint32_t name; /* const char * (32-bit pointer) */
2583 uint32_t protocols; /* struct protocol_list_t *
2585 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
2586 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
2587 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
2588 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
2589 uint32_t instanceProperties; /* struct objc_property_list *
2593 struct ivar_list64_t {
2596 /* struct ivar64_t first; These structures follow inline */
2599 struct ivar_list32_t {
2602 /* struct ivar32_t first; These structures follow inline */
2606 uint64_t offset; /* uintptr_t * (64-bit pointer) */
2607 uint64_t name; /* const char * (64-bit pointer) */
2608 uint64_t type; /* const char * (64-bit pointer) */
2614 uint32_t offset; /* uintptr_t * (32-bit pointer) */
2615 uint32_t name; /* const char * (32-bit pointer) */
2616 uint32_t type; /* const char * (32-bit pointer) */
2621 struct objc_property_list64 {
2624 /* struct objc_property64 first; These structures follow inline */
2627 struct objc_property_list32 {
2630 /* struct objc_property32 first; These structures follow inline */
2633 struct objc_property64 {
2634 uint64_t name; /* const char * (64-bit pointer) */
2635 uint64_t attributes; /* const char * (64-bit pointer) */
2638 struct objc_property32 {
2639 uint32_t name; /* const char * (32-bit pointer) */
2640 uint32_t attributes; /* const char * (32-bit pointer) */
2643 struct category64_t {
2644 uint64_t name; /* const char * (64-bit pointer) */
2645 uint64_t cls; /* struct class_t * (64-bit pointer) */
2646 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
2647 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
2648 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
2649 uint64_t instanceProperties; /* struct objc_property_list *
2653 struct category32_t {
2654 uint32_t name; /* const char * (32-bit pointer) */
2655 uint32_t cls; /* struct class_t * (32-bit pointer) */
2656 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
2657 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
2658 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
2659 uint32_t instanceProperties; /* struct objc_property_list *
2663 struct objc_image_info64 {
2667 struct objc_image_info32 {
2671 struct imageInfo_t {
2675 /* masks for objc_image_info.flags */
2676 #define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
2677 #define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
2679 struct message_ref64 {
2680 uint64_t imp; /* IMP (64-bit pointer) */
2681 uint64_t sel; /* SEL (64-bit pointer) */
2684 struct message_ref32 {
2685 uint32_t imp; /* IMP (32-bit pointer) */
2686 uint32_t sel; /* SEL (32-bit pointer) */
2689 // Objective-C 1 (32-bit only) meta data structs.
2691 struct objc_module_t {
2694 uint32_t name; /* char * (32-bit pointer) */
2695 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
2698 struct objc_symtab_t {
2699 uint32_t sel_ref_cnt;
2700 uint32_t refs; /* SEL * (32-bit pointer) */
2701 uint16_t cls_def_cnt;
2702 uint16_t cat_def_cnt;
2703 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
2706 struct objc_class_t {
2707 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2708 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
2709 uint32_t name; /* const char * (32-bit pointer) */
2712 int32_t instance_size;
2713 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
2714 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
2715 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
2716 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
2719 #define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
2720 // class is not a metaclass
2721 #define CLS_CLASS 0x1
2722 // class is a metaclass
2723 #define CLS_META 0x2
2725 struct objc_category_t {
2726 uint32_t category_name; /* char * (32-bit pointer) */
2727 uint32_t class_name; /* char * (32-bit pointer) */
2728 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
2729 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
2730 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
2733 struct objc_ivar_t {
2734 uint32_t ivar_name; /* char * (32-bit pointer) */
2735 uint32_t ivar_type; /* char * (32-bit pointer) */
2736 int32_t ivar_offset;
2739 struct objc_ivar_list_t {
2741 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
2744 struct objc_method_list_t {
2745 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
2746 int32_t method_count;
2747 // struct objc_method_t method_list[1]; /* variable length structure */
2750 struct objc_method_t {
2751 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2752 uint32_t method_types; /* char * (32-bit pointer) */
2753 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
2757 struct objc_protocol_list_t {
2758 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
2760 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
2761 // (32-bit pointer) */
2764 struct objc_protocol_t {
2765 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2766 uint32_t protocol_name; /* char * (32-bit pointer) */
2767 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
2768 uint32_t instance_methods; /* struct objc_method_description_list *
2770 uint32_t class_methods; /* struct objc_method_description_list *
2774 struct objc_method_description_list_t {
2776 // struct objc_method_description_t list[1];
2779 struct objc_method_description_t {
2780 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2781 uint32_t types; /* char * (32-bit pointer) */
2784 inline void swapStruct(struct cfstring64_t &cfs) {
2785 sys::swapByteOrder(cfs.isa);
2786 sys::swapByteOrder(cfs.flags);
2787 sys::swapByteOrder(cfs.characters);
2788 sys::swapByteOrder(cfs.length);
2791 inline void swapStruct(struct class64_t &c) {
2792 sys::swapByteOrder(c.isa);
2793 sys::swapByteOrder(c.superclass);
2794 sys::swapByteOrder(c.cache);
2795 sys::swapByteOrder(c.vtable);
2796 sys::swapByteOrder(c.data);
2799 inline void swapStruct(struct class32_t &c) {
2800 sys::swapByteOrder(c.isa);
2801 sys::swapByteOrder(c.superclass);
2802 sys::swapByteOrder(c.cache);
2803 sys::swapByteOrder(c.vtable);
2804 sys::swapByteOrder(c.data);
2807 inline void swapStruct(struct class_ro64_t &cro) {
2808 sys::swapByteOrder(cro.flags);
2809 sys::swapByteOrder(cro.instanceStart);
2810 sys::swapByteOrder(cro.instanceSize);
2811 sys::swapByteOrder(cro.reserved);
2812 sys::swapByteOrder(cro.ivarLayout);
2813 sys::swapByteOrder(cro.name);
2814 sys::swapByteOrder(cro.baseMethods);
2815 sys::swapByteOrder(cro.baseProtocols);
2816 sys::swapByteOrder(cro.ivars);
2817 sys::swapByteOrder(cro.weakIvarLayout);
2818 sys::swapByteOrder(cro.baseProperties);
2821 inline void swapStruct(struct class_ro32_t &cro) {
2822 sys::swapByteOrder(cro.flags);
2823 sys::swapByteOrder(cro.instanceStart);
2824 sys::swapByteOrder(cro.instanceSize);
2825 sys::swapByteOrder(cro.ivarLayout);
2826 sys::swapByteOrder(cro.name);
2827 sys::swapByteOrder(cro.baseMethods);
2828 sys::swapByteOrder(cro.baseProtocols);
2829 sys::swapByteOrder(cro.ivars);
2830 sys::swapByteOrder(cro.weakIvarLayout);
2831 sys::swapByteOrder(cro.baseProperties);
2834 inline void swapStruct(struct method_list64_t &ml) {
2835 sys::swapByteOrder(ml.entsize);
2836 sys::swapByteOrder(ml.count);
2839 inline void swapStruct(struct method_list32_t &ml) {
2840 sys::swapByteOrder(ml.entsize);
2841 sys::swapByteOrder(ml.count);
2844 inline void swapStruct(struct method64_t &m) {
2845 sys::swapByteOrder(m.name);
2846 sys::swapByteOrder(m.types);
2847 sys::swapByteOrder(m.imp);
2850 inline void swapStruct(struct method32_t &m) {
2851 sys::swapByteOrder(m.name);
2852 sys::swapByteOrder(m.types);
2853 sys::swapByteOrder(m.imp);
2856 inline void swapStruct(struct protocol_list64_t &pl) {
2857 sys::swapByteOrder(pl.count);
2860 inline void swapStruct(struct protocol_list32_t &pl) {
2861 sys::swapByteOrder(pl.count);
2864 inline void swapStruct(struct protocol64_t &p) {
2865 sys::swapByteOrder(p.isa);
2866 sys::swapByteOrder(p.name);
2867 sys::swapByteOrder(p.protocols);
2868 sys::swapByteOrder(p.instanceMethods);
2869 sys::swapByteOrder(p.classMethods);
2870 sys::swapByteOrder(p.optionalInstanceMethods);
2871 sys::swapByteOrder(p.optionalClassMethods);
2872 sys::swapByteOrder(p.instanceProperties);
2875 inline void swapStruct(struct protocol32_t &p) {
2876 sys::swapByteOrder(p.isa);
2877 sys::swapByteOrder(p.name);
2878 sys::swapByteOrder(p.protocols);
2879 sys::swapByteOrder(p.instanceMethods);
2880 sys::swapByteOrder(p.classMethods);
2881 sys::swapByteOrder(p.optionalInstanceMethods);
2882 sys::swapByteOrder(p.optionalClassMethods);
2883 sys::swapByteOrder(p.instanceProperties);
2886 inline void swapStruct(struct ivar_list64_t &il) {
2887 sys::swapByteOrder(il.entsize);
2888 sys::swapByteOrder(il.count);
2891 inline void swapStruct(struct ivar_list32_t &il) {
2892 sys::swapByteOrder(il.entsize);
2893 sys::swapByteOrder(il.count);
2896 inline void swapStruct(struct ivar64_t &i) {
2897 sys::swapByteOrder(i.offset);
2898 sys::swapByteOrder(i.name);
2899 sys::swapByteOrder(i.type);
2900 sys::swapByteOrder(i.alignment);
2901 sys::swapByteOrder(i.size);
2904 inline void swapStruct(struct ivar32_t &i) {
2905 sys::swapByteOrder(i.offset);
2906 sys::swapByteOrder(i.name);
2907 sys::swapByteOrder(i.type);
2908 sys::swapByteOrder(i.alignment);
2909 sys::swapByteOrder(i.size);
2912 inline void swapStruct(struct objc_property_list64 &pl) {
2913 sys::swapByteOrder(pl.entsize);
2914 sys::swapByteOrder(pl.count);
2917 inline void swapStruct(struct objc_property_list32 &pl) {
2918 sys::swapByteOrder(pl.entsize);
2919 sys::swapByteOrder(pl.count);
2922 inline void swapStruct(struct objc_property64 &op) {
2923 sys::swapByteOrder(op.name);
2924 sys::swapByteOrder(op.attributes);
2927 inline void swapStruct(struct objc_property32 &op) {
2928 sys::swapByteOrder(op.name);
2929 sys::swapByteOrder(op.attributes);
2932 inline void swapStruct(struct category64_t &c) {
2933 sys::swapByteOrder(c.name);
2934 sys::swapByteOrder(c.cls);
2935 sys::swapByteOrder(c.instanceMethods);
2936 sys::swapByteOrder(c.classMethods);
2937 sys::swapByteOrder(c.protocols);
2938 sys::swapByteOrder(c.instanceProperties);
2941 inline void swapStruct(struct category32_t &c) {
2942 sys::swapByteOrder(c.name);
2943 sys::swapByteOrder(c.cls);
2944 sys::swapByteOrder(c.instanceMethods);
2945 sys::swapByteOrder(c.classMethods);
2946 sys::swapByteOrder(c.protocols);
2947 sys::swapByteOrder(c.instanceProperties);
2950 inline void swapStruct(struct objc_image_info64 &o) {
2951 sys::swapByteOrder(o.version);
2952 sys::swapByteOrder(o.flags);
2955 inline void swapStruct(struct objc_image_info32 &o) {
2956 sys::swapByteOrder(o.version);
2957 sys::swapByteOrder(o.flags);
2960 inline void swapStruct(struct imageInfo_t &o) {
2961 sys::swapByteOrder(o.version);
2962 sys::swapByteOrder(o.flags);
2965 inline void swapStruct(struct message_ref64 &mr) {
2966 sys::swapByteOrder(mr.imp);
2967 sys::swapByteOrder(mr.sel);
2970 inline void swapStruct(struct message_ref32 &mr) {
2971 sys::swapByteOrder(mr.imp);
2972 sys::swapByteOrder(mr.sel);
2975 inline void swapStruct(struct objc_module_t &module) {
2976 sys::swapByteOrder(module.version);
2977 sys::swapByteOrder(module.size);
2978 sys::swapByteOrder(module.name);
2979 sys::swapByteOrder(module.symtab);
2982 inline void swapStruct(struct objc_symtab_t &symtab) {
2983 sys::swapByteOrder(symtab.sel_ref_cnt);
2984 sys::swapByteOrder(symtab.refs);
2985 sys::swapByteOrder(symtab.cls_def_cnt);
2986 sys::swapByteOrder(symtab.cat_def_cnt);
2989 inline void swapStruct(struct objc_class_t &objc_class) {
2990 sys::swapByteOrder(objc_class.isa);
2991 sys::swapByteOrder(objc_class.super_class);
2992 sys::swapByteOrder(objc_class.name);
2993 sys::swapByteOrder(objc_class.version);
2994 sys::swapByteOrder(objc_class.info);
2995 sys::swapByteOrder(objc_class.instance_size);
2996 sys::swapByteOrder(objc_class.ivars);
2997 sys::swapByteOrder(objc_class.methodLists);
2998 sys::swapByteOrder(objc_class.cache);
2999 sys::swapByteOrder(objc_class.protocols);
3002 inline void swapStruct(struct objc_category_t &objc_category) {
3003 sys::swapByteOrder(objc_category.category_name);
3004 sys::swapByteOrder(objc_category.class_name);
3005 sys::swapByteOrder(objc_category.instance_methods);
3006 sys::swapByteOrder(objc_category.class_methods);
3007 sys::swapByteOrder(objc_category.protocols);
3010 inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3011 sys::swapByteOrder(objc_ivar_list.ivar_count);
3014 inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3015 sys::swapByteOrder(objc_ivar.ivar_name);
3016 sys::swapByteOrder(objc_ivar.ivar_type);
3017 sys::swapByteOrder(objc_ivar.ivar_offset);
3020 inline void swapStruct(struct objc_method_list_t &method_list) {
3021 sys::swapByteOrder(method_list.obsolete);
3022 sys::swapByteOrder(method_list.method_count);
3025 inline void swapStruct(struct objc_method_t &method) {
3026 sys::swapByteOrder(method.method_name);
3027 sys::swapByteOrder(method.method_types);
3028 sys::swapByteOrder(method.method_imp);
3031 inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3032 sys::swapByteOrder(protocol_list.next);
3033 sys::swapByteOrder(protocol_list.count);
3036 inline void swapStruct(struct objc_protocol_t &protocol) {
3037 sys::swapByteOrder(protocol.isa);
3038 sys::swapByteOrder(protocol.protocol_name);
3039 sys::swapByteOrder(protocol.protocol_list);
3040 sys::swapByteOrder(protocol.instance_methods);
3041 sys::swapByteOrder(protocol.class_methods);
3044 inline void swapStruct(struct objc_method_description_list_t &mdl) {
3045 sys::swapByteOrder(mdl.count);
3048 inline void swapStruct(struct objc_method_description_t &md) {
3049 sys::swapByteOrder(md.name);
3050 sys::swapByteOrder(md.types);
3053 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3054 struct DisassembleInfo *info);
3056 // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3057 // to an Objective-C class and returns the class name. It is also passed the
3058 // address of the pointer, so when the pointer is zero as it can be in an .o
3059 // file, that is used to look for an external relocation entry with a symbol
3061 static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3062 uint64_t ReferenceValue,
3063 struct DisassembleInfo *info) {
3065 uint32_t offset, left;
3068 // The pointer_value can be 0 in an object file and have a relocation
3069 // entry for the class symbol at the ReferenceValue (the address of the
3071 if (pointer_value == 0) {
3072 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3073 if (r == nullptr || left < sizeof(uint64_t))
3076 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3077 if (symbol_name == nullptr)
3079 const char *class_name = strrchr(symbol_name, '$');
3080 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3081 return class_name + 2;
3086 // The case were the pointer_value is non-zero and points to a class defined
3087 // in this Mach-O file.
3088 r = get_pointer_64(pointer_value, offset, left, S, info);
3089 if (r == nullptr || left < sizeof(struct class64_t))
3092 memcpy(&c, r, sizeof(struct class64_t));
3093 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3097 r = get_pointer_64(c.data, offset, left, S, info);
3098 if (r == nullptr || left < sizeof(struct class_ro64_t))
3100 struct class_ro64_t cro;
3101 memcpy(&cro, r, sizeof(struct class_ro64_t));
3102 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3106 const char *name = get_pointer_64(cro.name, offset, left, S, info);
3110 // get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3111 // pointer to a cfstring and returns its name or nullptr.
3112 static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3113 struct DisassembleInfo *info) {
3114 const char *r, *name;
3115 uint32_t offset, left;
3117 struct cfstring64_t cfs;
3118 uint64_t cfs_characters;
3120 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3121 if (r == nullptr || left < sizeof(struct cfstring64_t))
3123 memcpy(&cfs, r, sizeof(struct cfstring64_t));
3124 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3126 if (cfs.characters == 0) {
3128 const char *symbol_name = get_symbol_64(
3129 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
3130 if (symbol_name == nullptr)
3132 cfs_characters = n_value;
3134 cfs_characters = cfs.characters;
3135 name = get_pointer_64(cfs_characters, offset, left, S, info);
3140 // get_objc2_64bit_selref() is used for disassembly and is passed a the address
3141 // of a pointer to an Objective-C selector reference when the pointer value is
3142 // zero as in a .o file and is likely to have a external relocation entry with
3143 // who's symbol's n_value is the real pointer to the selector name. If that is
3144 // the case the real pointer to the selector name is returned else 0 is
3146 static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3147 struct DisassembleInfo *info) {
3148 uint32_t offset, left;
3151 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3152 if (r == nullptr || left < sizeof(uint64_t))
3155 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3156 if (symbol_name == nullptr)
3161 static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3162 const char *sectname) {
3163 for (const SectionRef &Section : O->sections()) {
3165 Section.getName(SectName);
3166 DataRefImpl Ref = Section.getRawDataRefImpl();
3167 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3168 if (SegName == segname && SectName == sectname)
3171 return SectionRef();
3175 walk_pointer_list_64(const char *listname, const SectionRef S,
3176 MachOObjectFile *O, struct DisassembleInfo *info,
3177 void (*func)(uint64_t, struct DisassembleInfo *info)) {
3178 if (S == SectionRef())
3182 S.getName(SectName);
3183 DataRefImpl Ref = S.getRawDataRefImpl();
3184 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3185 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3188 S.getContents(BytesStr);
3189 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3191 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3192 uint32_t left = S.getSize() - i;
3193 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3195 memcpy(&p, Contents + i, size);
3196 if (i + sizeof(uint64_t) > S.getSize())
3197 outs() << listname << " list pointer extends past end of (" << SegName
3198 << "," << SectName << ") section\n";
3199 outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
3201 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3202 sys::swapByteOrder(p);
3204 uint64_t n_value = 0;
3205 const char *name = get_symbol_64(i, S, info, n_value, p);
3206 if (name == nullptr)
3207 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
3210 outs() << format("0x%" PRIx64, n_value);
3212 outs() << " + " << format("0x%" PRIx64, p);
3214 outs() << format("0x%" PRIx64, p);
3215 if (name != nullptr)
3216 outs() << " " << name;
3226 walk_pointer_list_32(const char *listname, const SectionRef S,
3227 MachOObjectFile *O, struct DisassembleInfo *info,
3228 void (*func)(uint32_t, struct DisassembleInfo *info)) {
3229 if (S == SectionRef())
3233 S.getName(SectName);
3234 DataRefImpl Ref = S.getRawDataRefImpl();
3235 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3236 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3239 S.getContents(BytesStr);
3240 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3242 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
3243 uint32_t left = S.getSize() - i;
3244 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
3246 memcpy(&p, Contents + i, size);
3247 if (i + sizeof(uint32_t) > S.getSize())
3248 outs() << listname << " list pointer extends past end of (" << SegName
3249 << "," << SectName << ") section\n";
3250 uint32_t Address = S.getAddress() + i;
3251 outs() << format("%08" PRIx32, Address) << " ";
3253 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3254 sys::swapByteOrder(p);
3255 outs() << format("0x%" PRIx32, p);
3257 const char *name = get_symbol_32(i, S, info, p);
3258 if (name != nullptr)
3259 outs() << " " << name;
3267 static void print_layout_map(const char *layout_map, uint32_t left) {
3268 if (layout_map == nullptr)
3270 outs() << " layout map: ";
3272 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
3275 } while (*layout_map != '\0' && left != 0);
3279 static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
3280 uint32_t offset, left;
3282 const char *layout_map;
3286 layout_map = get_pointer_64(p, offset, left, S, info);
3287 print_layout_map(layout_map, left);
3290 static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
3291 uint32_t offset, left;
3293 const char *layout_map;
3297 layout_map = get_pointer_32(p, offset, left, S, info);
3298 print_layout_map(layout_map, left);
3301 static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
3302 const char *indent) {
3303 struct method_list64_t ml;
3304 struct method64_t m;
3306 uint32_t offset, xoffset, left, i;
3308 const char *name, *sym_name;
3311 r = get_pointer_64(p, offset, left, S, info);
3314 memset(&ml, '\0', sizeof(struct method_list64_t));
3315 if (left < sizeof(struct method_list64_t)) {
3316 memcpy(&ml, r, left);
3317 outs() << " (method_list_t entends past the end of the section)\n";
3319 memcpy(&ml, r, sizeof(struct method_list64_t));
3320 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3322 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3323 outs() << indent << "\t\t count " << ml.count << "\n";
3325 p += sizeof(struct method_list64_t);
3326 offset += sizeof(struct method_list64_t);
3327 for (i = 0; i < ml.count; i++) {
3328 r = get_pointer_64(p, offset, left, S, info);
3331 memset(&m, '\0', sizeof(struct method64_t));
3332 if (left < sizeof(struct method64_t)) {
3333 memcpy(&m, r, left);
3334 outs() << indent << " (method_t extends past the end of the section)\n";
3336 memcpy(&m, r, sizeof(struct method64_t));
3337 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3340 outs() << indent << "\t\t name ";
3341 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
3342 info, n_value, m.name);
3344 if (info->verbose && sym_name != nullptr)
3347 outs() << format("0x%" PRIx64, n_value);
3349 outs() << " + " << format("0x%" PRIx64, m.name);
3351 outs() << format("0x%" PRIx64, m.name);
3352 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
3353 if (name != nullptr)
3354 outs() << format(" %.*s", left, name);
3357 outs() << indent << "\t\t types ";
3358 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
3359 info, n_value, m.types);
3361 if (info->verbose && sym_name != nullptr)
3364 outs() << format("0x%" PRIx64, n_value);
3366 outs() << " + " << format("0x%" PRIx64, m.types);
3368 outs() << format("0x%" PRIx64, m.types);
3369 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
3370 if (name != nullptr)
3371 outs() << format(" %.*s", left, name);
3374 outs() << indent << "\t\t imp ";
3375 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
3377 if (info->verbose && name == nullptr) {
3379 outs() << format("0x%" PRIx64, n_value) << " ";
3381 outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
3383 outs() << format("0x%" PRIx64, m.imp) << " ";
3385 if (name != nullptr)
3389 p += sizeof(struct method64_t);
3390 offset += sizeof(struct method64_t);
3394 static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
3395 const char *indent) {
3396 struct method_list32_t ml;
3397 struct method32_t m;
3398 const char *r, *name;
3399 uint32_t offset, xoffset, left, i;
3402 r = get_pointer_32(p, offset, left, S, info);
3405 memset(&ml, '\0', sizeof(struct method_list32_t));
3406 if (left < sizeof(struct method_list32_t)) {
3407 memcpy(&ml, r, left);
3408 outs() << " (method_list_t entends past the end of the section)\n";
3410 memcpy(&ml, r, sizeof(struct method_list32_t));
3411 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3413 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3414 outs() << indent << "\t\t count " << ml.count << "\n";
3416 p += sizeof(struct method_list32_t);
3417 offset += sizeof(struct method_list32_t);
3418 for (i = 0; i < ml.count; i++) {
3419 r = get_pointer_32(p, offset, left, S, info);
3422 memset(&m, '\0', sizeof(struct method32_t));
3423 if (left < sizeof(struct method32_t)) {
3424 memcpy(&ml, r, left);
3425 outs() << indent << " (method_t entends past the end of the section)\n";
3427 memcpy(&m, r, sizeof(struct method32_t));
3428 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3431 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
3432 name = get_pointer_32(m.name, xoffset, left, xS, info);
3433 if (name != nullptr)
3434 outs() << format(" %.*s", left, name);
3437 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
3438 name = get_pointer_32(m.types, xoffset, left, xS, info);
3439 if (name != nullptr)
3440 outs() << format(" %.*s", left, name);
3443 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
3444 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
3446 if (name != nullptr)
3447 outs() << " " << name;
3450 p += sizeof(struct method32_t);
3451 offset += sizeof(struct method32_t);
3455 static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
3456 uint32_t offset, left, xleft;
3458 struct objc_method_list_t method_list;
3459 struct objc_method_t method;
3460 const char *r, *methods, *name, *SymbolName;
3463 r = get_pointer_32(p, offset, left, S, info, true);
3468 if (left > sizeof(struct objc_method_list_t)) {
3469 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
3471 outs() << "\t\t objc_method_list extends past end of the section\n";
3472 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
3473 memcpy(&method_list, r, left);
3475 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3476 swapStruct(method_list);
3478 outs() << "\t\t obsolete "
3479 << format("0x%08" PRIx32, method_list.obsolete) << "\n";
3480 outs() << "\t\t method_count " << method_list.method_count << "\n";
3482 methods = r + sizeof(struct objc_method_list_t);
3483 for (i = 0; i < method_list.method_count; i++) {
3484 if ((i + 1) * sizeof(struct objc_method_t) > left) {
3485 outs() << "\t\t remaining method's extend past the of the section\n";
3488 memcpy(&method, methods + i * sizeof(struct objc_method_t),
3489 sizeof(struct objc_method_t));
3490 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3493 outs() << "\t\t method_name "
3494 << format("0x%08" PRIx32, method.method_name);
3495 if (info->verbose) {
3496 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
3497 if (name != nullptr)
3498 outs() << format(" %.*s", xleft, name);
3500 outs() << " (not in an __OBJC section)";
3504 outs() << "\t\t method_types "
3505 << format("0x%08" PRIx32, method.method_types);
3506 if (info->verbose) {
3507 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
3508 if (name != nullptr)
3509 outs() << format(" %.*s", xleft, name);
3511 outs() << " (not in an __OBJC section)";
3515 outs() << "\t\t method_imp "
3516 << format("0x%08" PRIx32, method.method_imp) << " ";
3517 if (info->verbose) {
3518 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
3519 if (SymbolName != nullptr)
3520 outs() << SymbolName;
3527 static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
3528 struct protocol_list64_t pl;
3529 uint64_t q, n_value;
3530 struct protocol64_t pc;
3532 uint32_t offset, xoffset, left, i;
3534 const char *name, *sym_name;
3536 r = get_pointer_64(p, offset, left, S, info);
3539 memset(&pl, '\0', sizeof(struct protocol_list64_t));
3540 if (left < sizeof(struct protocol_list64_t)) {
3541 memcpy(&pl, r, left);
3542 outs() << " (protocol_list_t entends past the end of the section)\n";
3544 memcpy(&pl, r, sizeof(struct protocol_list64_t));
3545 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3547 outs() << " count " << pl.count << "\n";
3549 p += sizeof(struct protocol_list64_t);
3550 offset += sizeof(struct protocol_list64_t);
3551 for (i = 0; i < pl.count; i++) {
3552 r = get_pointer_64(p, offset, left, S, info);
3556 if (left < sizeof(uint64_t)) {
3557 memcpy(&q, r, left);
3558 outs() << " (protocol_t * entends past the end of the section)\n";
3560 memcpy(&q, r, sizeof(uint64_t));
3561 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3562 sys::swapByteOrder(q);
3564 outs() << "\t\t list[" << i << "] ";
3565 sym_name = get_symbol_64(offset, S, info, n_value, q);
3567 if (info->verbose && sym_name != nullptr)
3570 outs() << format("0x%" PRIx64, n_value);
3572 outs() << " + " << format("0x%" PRIx64, q);
3574 outs() << format("0x%" PRIx64, q);
3575 outs() << " (struct protocol_t *)\n";
3577 r = get_pointer_64(q + n_value, offset, left, S, info);
3580 memset(&pc, '\0', sizeof(struct protocol64_t));
3581 if (left < sizeof(struct protocol64_t)) {
3582 memcpy(&pc, r, left);
3583 outs() << " (protocol_t entends past the end of the section)\n";
3585 memcpy(&pc, r, sizeof(struct protocol64_t));
3586 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3589 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
3591 outs() << "\t\t\t name ";
3592 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
3593 info, n_value, pc.name);
3595 if (info->verbose && sym_name != nullptr)
3598 outs() << format("0x%" PRIx64, n_value);
3600 outs() << " + " << format("0x%" PRIx64, pc.name);
3602 outs() << format("0x%" PRIx64, pc.name);
3603 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
3604 if (name != nullptr)
3605 outs() << format(" %.*s", left, name);
3608 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
3610 outs() << "\t\t instanceMethods ";
3612 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
3613 S, info, n_value, pc.instanceMethods);
3615 if (info->verbose && sym_name != nullptr)
3618 outs() << format("0x%" PRIx64, n_value);
3619 if (pc.instanceMethods != 0)
3620 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
3622 outs() << format("0x%" PRIx64, pc.instanceMethods);
3623 outs() << " (struct method_list_t *)\n";
3624 if (pc.instanceMethods + n_value != 0)
3625 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
3627 outs() << "\t\t classMethods ";
3629 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
3630 info, n_value, pc.classMethods);
3632 if (info->verbose && sym_name != nullptr)
3635 outs() << format("0x%" PRIx64, n_value);
3636 if (pc.classMethods != 0)
3637 outs() << " + " << format("0x%" PRIx64, pc.classMethods);
3639 outs() << format("0x%" PRIx64, pc.classMethods);
3640 outs() << " (struct method_list_t *)\n";
3641 if (pc.classMethods + n_value != 0)
3642 print_method_list64_t(pc.classMethods + n_value, info, "\t");
3644 outs() << "\t optionalInstanceMethods "
3645 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
3646 outs() << "\t optionalClassMethods "
3647 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
3648 outs() << "\t instanceProperties "
3649 << format("0x%" PRIx64, pc.instanceProperties) << "\n";
3651 p += sizeof(uint64_t);
3652 offset += sizeof(uint64_t);
3656 static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
3657 struct protocol_list32_t pl;
3659 struct protocol32_t pc;
3661 uint32_t offset, xoffset, left, i;
3665 r = get_pointer_32(p, offset, left, S, info);
3668 memset(&pl, '\0', sizeof(struct protocol_list32_t));
3669 if (left < sizeof(struct protocol_list32_t)) {
3670 memcpy(&pl, r, left);
3671 outs() << " (protocol_list_t entends past the end of the section)\n";
3673 memcpy(&pl, r, sizeof(struct protocol_list32_t));
3674 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3676 outs() << " count " << pl.count << "\n";
3678 p += sizeof(struct protocol_list32_t);
3679 offset += sizeof(struct protocol_list32_t);
3680 for (i = 0; i < pl.count; i++) {
3681 r = get_pointer_32(p, offset, left, S, info);
3685 if (left < sizeof(uint32_t)) {
3686 memcpy(&q, r, left);
3687 outs() << " (protocol_t * entends past the end of the section)\n";
3689 memcpy(&q, r, sizeof(uint32_t));
3690 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3691 sys::swapByteOrder(q);
3692 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
3693 << " (struct protocol_t *)\n";
3694 r = get_pointer_32(q, offset, left, S, info);
3697 memset(&pc, '\0', sizeof(struct protocol32_t));
3698 if (left < sizeof(struct protocol32_t)) {
3699 memcpy(&pc, r, left);
3700 outs() << " (protocol_t entends past the end of the section)\n";
3702 memcpy(&pc, r, sizeof(struct protocol32_t));
3703 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3705 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
3706 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
3707 name = get_pointer_32(pc.name, xoffset, left, xS, info);
3708 if (name != nullptr)
3709 outs() << format(" %.*s", left, name);
3711 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
3712 outs() << "\t\t instanceMethods "
3713 << format("0x%" PRIx32, pc.instanceMethods)
3714 << " (struct method_list_t *)\n";
3715 if (pc.instanceMethods != 0)
3716 print_method_list32_t(pc.instanceMethods, info, "\t");
3717 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
3718 << " (struct method_list_t *)\n";
3719 if (pc.classMethods != 0)
3720 print_method_list32_t(pc.classMethods, info, "\t");
3721 outs() << "\t optionalInstanceMethods "
3722 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
3723 outs() << "\t optionalClassMethods "
3724 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
3725 outs() << "\t instanceProperties "
3726 << format("0x%" PRIx32, pc.instanceProperties) << "\n";
3727 p += sizeof(uint32_t);
3728 offset += sizeof(uint32_t);
3732 static void print_indent(uint32_t indent) {
3733 for (uint32_t i = 0; i < indent;) {
3734 if (indent - i >= 8) {
3738 for (uint32_t j = i; j < indent; j++)
3745 static bool print_method_description_list(uint32_t p, uint32_t indent,
3746 struct DisassembleInfo *info) {
3747 uint32_t offset, left, xleft;
3749 struct objc_method_description_list_t mdl;
3750 struct objc_method_description_t md;
3751 const char *r, *list, *name;
3754 r = get_pointer_32(p, offset, left, S, info, true);
3759 if (left > sizeof(struct objc_method_description_list_t)) {
3760 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
3762 print_indent(indent);
3763 outs() << " objc_method_description_list extends past end of the section\n";
3764 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
3765 memcpy(&mdl, r, left);
3767 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3770 print_indent(indent);
3771 outs() << " count " << mdl.count << "\n";
3773 list = r + sizeof(struct objc_method_description_list_t);
3774 for (i = 0; i < mdl.count; i++) {
3775 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
3776 print_indent(indent);
3777 outs() << " remaining list entries extend past the of the section\n";
3780 print_indent(indent);
3781 outs() << " list[" << i << "]\n";
3782 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
3783 sizeof(struct objc_method_description_t));
3784 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3787 print_indent(indent);
3788 outs() << " name " << format("0x%08" PRIx32, md.name);
3789 if (info->verbose) {
3790 name = get_pointer_32(md.name, offset, xleft, S, info, true);
3791 if (name != nullptr)
3792 outs() << format(" %.*s", xleft, name);
3794 outs() << " (not in an __OBJC section)";
3798 print_indent(indent);
3799 outs() << " types " << format("0x%08" PRIx32, md.types);
3800 if (info->verbose) {
3801 name = get_pointer_32(md.types, offset, xleft, S, info, true);
3802 if (name != nullptr)
3803 outs() << format(" %.*s", xleft, name);
3805 outs() << " (not in an __OBJC section)";
3812 static bool print_protocol_list(uint32_t p, uint32_t indent,
3813 struct DisassembleInfo *info);
3815 static bool print_protocol(uint32_t p, uint32_t indent,
3816 struct DisassembleInfo *info) {
3817 uint32_t offset, left;
3819 struct objc_protocol_t protocol;
3820 const char *r, *name;
3822 r = get_pointer_32(p, offset, left, S, info, true);
3827 if (left >= sizeof(struct objc_protocol_t)) {
3828 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
3830 print_indent(indent);
3831 outs() << " Protocol extends past end of the section\n";
3832 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
3833 memcpy(&protocol, r, left);
3835 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3836 swapStruct(protocol);
3838 print_indent(indent);
3839 outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
3842 print_indent(indent);
3843 outs() << " protocol_name "
3844 << format("0x%08" PRIx32, protocol.protocol_name);
3845 if (info->verbose) {
3846 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
3847 if (name != nullptr)
3848 outs() << format(" %.*s", left, name);
3850 outs() << " (not in an __OBJC section)";
3854 print_indent(indent);
3855 outs() << " protocol_list "
3856 << format("0x%08" PRIx32, protocol.protocol_list);
3857 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
3858 outs() << " (not in an __OBJC section)\n";
3860 print_indent(indent);
3861 outs() << " instance_methods "
3862 << format("0x%08" PRIx32, protocol.instance_methods);
3863 if (print_method_description_list(protocol.instance_methods, indent, info))
3864 outs() << " (not in an __OBJC section)\n";
3866 print_indent(indent);
3867 outs() << " class_methods "
3868 << format("0x%08" PRIx32, protocol.class_methods);
3869 if (print_method_description_list(protocol.class_methods, indent, info))
3870 outs() << " (not in an __OBJC section)\n";
3875 static bool print_protocol_list(uint32_t p, uint32_t indent,
3876 struct DisassembleInfo *info) {
3877 uint32_t offset, left, l;
3879 struct objc_protocol_list_t protocol_list;
3880 const char *r, *list;
3883 r = get_pointer_32(p, offset, left, S, info, true);
3888 if (left > sizeof(struct objc_protocol_list_t)) {
3889 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
3891 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
3892 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
3893 memcpy(&protocol_list, r, left);
3895 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3896 swapStruct(protocol_list);
3898 print_indent(indent);
3899 outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
3901 print_indent(indent);
3902 outs() << " count " << protocol_list.count << "\n";
3904 list = r + sizeof(struct objc_protocol_list_t);
3905 for (i = 0; i < protocol_list.count; i++) {
3906 if ((i + 1) * sizeof(uint32_t) > left) {
3907 outs() << "\t\t remaining list entries extend past the of the section\n";
3910 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
3911 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3912 sys::swapByteOrder(l);
3914 print_indent(indent);
3915 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
3916 if (print_protocol(l, indent, info))
3917 outs() << "(not in an __OBJC section)\n";
3922 static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
3923 struct ivar_list64_t il;
3926 uint32_t offset, xoffset, left, j;
3928 const char *name, *sym_name, *ivar_offset_p;
3929 uint64_t ivar_offset, n_value;
3931 r = get_pointer_64(p, offset, left, S, info);
3934 memset(&il, '\0', sizeof(struct ivar_list64_t));
3935 if (left < sizeof(struct ivar_list64_t)) {
3936 memcpy(&il, r, left);
3937 outs() << " (ivar_list_t entends past the end of the section)\n";
3939 memcpy(&il, r, sizeof(struct ivar_list64_t));
3940 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3942 outs() << " entsize " << il.entsize << "\n";
3943 outs() << " count " << il.count << "\n";
3945 p += sizeof(struct ivar_list64_t);
3946 offset += sizeof(struct ivar_list64_t);
3947 for (j = 0; j < il.count; j++) {
3948 r = get_pointer_64(p, offset, left, S, info);
3951 memset(&i, '\0', sizeof(struct ivar64_t));
3952 if (left < sizeof(struct ivar64_t)) {
3953 memcpy(&i, r, left);
3954 outs() << " (ivar_t entends past the end of the section)\n";
3956 memcpy(&i, r, sizeof(struct ivar64_t));
3957 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3960 outs() << "\t\t\t offset ";
3961 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
3962 info, n_value, i.offset);
3964 if (info->verbose && sym_name != nullptr)