1 //===-- MachODump.cpp - Object file dumping utility for llvm --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the MachO-specific dumper for llvm-objdump.
12 //===----------------------------------------------------------------------===//
14 #include "llvm-objdump.h"
15 #include "llvm-c/Disassembler.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/Config/config.h"
20 #include "llvm/DebugInfo/DIContext.h"
21 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCDisassembler.h"
25 #include "llvm/MC/MCInst.h"
26 #include "llvm/MC/MCInstPrinter.h"
27 #include "llvm/MC/MCInstrDesc.h"
28 #include "llvm/MC/MCInstrInfo.h"
29 #include "llvm/MC/MCRegisterInfo.h"
30 #include "llvm/MC/MCSubtargetInfo.h"
31 #include "llvm/Object/MachO.h"
32 #include "llvm/Object/MachOUniversal.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/Endian.h"
37 #include "llvm/Support/Format.h"
38 #include "llvm/Support/FormattedStream.h"
39 #include "llvm/Support/GraphWriter.h"
40 #include "llvm/Support/LEB128.h"
41 #include "llvm/Support/MachO.h"
42 #include "llvm/Support/MemoryBuffer.h"
43 #include "llvm/Support/TargetRegistry.h"
44 #include "llvm/Support/TargetSelect.h"
45 #include "llvm/Support/raw_ostream.h"
48 #include <system_error>
55 using namespace object;
59 cl::desc("Print line information from debug info if available"));
61 static cl::opt<std::string> DSYMFile("dsym",
62 cl::desc("Use .dSYM file for debug info"));
64 static cl::opt<bool> FullLeadingAddr("full-leading-addr",
65 cl::desc("Print full leading address"));
67 static cl::opt<bool> NoLeadingAddr("no-leading-addr",
68 cl::desc("Print no leading address"));
71 PrintImmHex("print-imm-hex",
72 cl::desc("Use hex format for immediate values"));
74 cl::opt<bool> llvm::UniversalHeaders("universal-headers",
75 cl::desc("Print Mach-O universal headers "
76 "(requires -macho)"));
79 llvm::ArchiveHeaders("archive-headers",
80 cl::desc("Print archive headers for Mach-O archives "
81 "(requires -macho)"));
84 ArchiveMemberOffsets("archive-member-offsets",
85 cl::desc("Print the offset to each archive member for "
86 "Mach-O archives (requires -macho and "
87 "-archive-headers)"));
90 llvm::IndirectSymbols("indirect-symbols",
91 cl::desc("Print indirect symbol table for Mach-O "
92 "objects (requires -macho)"));
95 llvm::DataInCode("data-in-code",
96 cl::desc("Print the data in code table for Mach-O objects "
97 "(requires -macho)"));
100 llvm::LinkOptHints("link-opt-hints",
101 cl::desc("Print the linker optimization hints for "
102 "Mach-O objects (requires -macho)"));
104 cl::list<std::string>
105 llvm::DumpSections("section",
106 cl::desc("Prints the specified segment,section for "
107 "Mach-O objects (requires -macho)"));
109 cl::opt<bool> llvm::Raw("raw",
110 cl::desc("Have -section dump the raw binary contents"));
113 llvm::InfoPlist("info-plist",
114 cl::desc("Print the info plist section as strings for "
115 "Mach-O objects (requires -macho)"));
118 llvm::DylibsUsed("dylibs-used",
119 cl::desc("Print the shared libraries used for linked "
120 "Mach-O files (requires -macho)"));
123 llvm::DylibId("dylib-id",
124 cl::desc("Print the shared library's id for the dylib Mach-O "
125 "file (requires -macho)"));
128 llvm::NonVerbose("non-verbose",
129 cl::desc("Print the info for Mach-O objects in "
130 "non-verbose or numeric form (requires -macho)"));
133 llvm::ObjcMetaData("objc-meta-data",
134 cl::desc("Print the Objective-C runtime meta data for "
135 "Mach-O files (requires -macho)"));
137 cl::opt<std::string> llvm::DisSymName(
139 cl::desc("disassemble just this symbol's instructions (requires -macho"));
141 static cl::opt<bool> NoSymbolicOperands(
142 "no-symbolic-operands",
143 cl::desc("do not symbolic operands when disassembling (requires -macho)"));
145 static cl::list<std::string>
146 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
148 bool ArchAll = false;
150 static std::string ThumbTripleName;
152 static const Target *GetTarget(const MachOObjectFile *MachOObj,
153 const char **McpuDefault,
154 const Target **ThumbTarget) {
155 // Figure out the target triple.
156 if (TripleName.empty()) {
157 llvm::Triple TT("unknown-unknown-unknown");
158 llvm::Triple ThumbTriple = Triple();
159 TT = MachOObj->getArch(McpuDefault, &ThumbTriple);
160 TripleName = TT.str();
161 ThumbTripleName = ThumbTriple.str();
164 // Get the target specific parser.
166 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
167 if (TheTarget && ThumbTripleName.empty())
170 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
174 errs() << "llvm-objdump: error: unable to get target for '";
176 errs() << TripleName;
178 errs() << ThumbTripleName;
179 errs() << "', see --version and --triple.\n";
183 struct SymbolSorter {
184 bool operator()(const SymbolRef &A, const SymbolRef &B) {
185 SymbolRef::Type AType, BType;
189 uint64_t AAddr, BAddr;
190 if (AType != SymbolRef::ST_Function)
194 if (BType != SymbolRef::ST_Function)
198 return AAddr < BAddr;
202 // Types for the storted data in code table that is built before disassembly
203 // and the predicate function to sort them.
204 typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
205 typedef std::vector<DiceTableEntry> DiceTable;
206 typedef DiceTable::iterator dice_table_iterator;
208 // This is used to search for a data in code table entry for the PC being
209 // disassembled. The j parameter has the PC in j.first. A single data in code
210 // table entry can cover many bytes for each of its Kind's. So if the offset,
211 // aka the i.first value, of the data in code table entry plus its Length
212 // covers the PC being searched for this will return true. If not it will
214 static bool compareDiceTableEntries(const DiceTableEntry &i,
215 const DiceTableEntry &j) {
217 i.second.getLength(Length);
219 return j.first >= i.first && j.first < i.first + Length;
222 static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
223 unsigned short Kind) {
224 uint32_t Value, Size = 1;
228 case MachO::DICE_KIND_DATA:
231 DumpBytes(ArrayRef<uint8_t>(bytes, 4));
232 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
233 outs() << "\t.long " << Value;
235 } else if (Length >= 2) {
237 DumpBytes(ArrayRef<uint8_t>(bytes, 2));
238 Value = bytes[1] << 8 | bytes[0];
239 outs() << "\t.short " << Value;
243 DumpBytes(ArrayRef<uint8_t>(bytes, 2));
245 outs() << "\t.byte " << Value;
248 if (Kind == MachO::DICE_KIND_DATA)
249 outs() << "\t@ KIND_DATA\n";
251 outs() << "\t@ data in code kind = " << Kind << "\n";
253 case MachO::DICE_KIND_JUMP_TABLE8:
255 DumpBytes(ArrayRef<uint8_t>(bytes, 1));
257 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
260 case MachO::DICE_KIND_JUMP_TABLE16:
262 DumpBytes(ArrayRef<uint8_t>(bytes, 2));
263 Value = bytes[1] << 8 | bytes[0];
264 outs() << "\t.short " << format("%5u", Value & 0xffff)
265 << "\t@ KIND_JUMP_TABLE16\n";
268 case MachO::DICE_KIND_JUMP_TABLE32:
269 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
271 DumpBytes(ArrayRef<uint8_t>(bytes, 4));
272 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
273 outs() << "\t.long " << Value;
274 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
275 outs() << "\t@ KIND_JUMP_TABLE32\n";
277 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
284 static void getSectionsAndSymbols(const MachO::mach_header Header,
285 MachOObjectFile *MachOObj,
286 std::vector<SectionRef> &Sections,
287 std::vector<SymbolRef> &Symbols,
288 SmallVectorImpl<uint64_t> &FoundFns,
289 uint64_t &BaseSegmentAddress) {
290 for (const SymbolRef &Symbol : MachOObj->symbols()) {
292 Symbol.getName(SymName);
293 if (!SymName.startswith("ltmp"))
294 Symbols.push_back(Symbol);
297 for (const SectionRef &Section : MachOObj->sections()) {
299 Section.getName(SectName);
300 Sections.push_back(Section);
303 MachOObjectFile::LoadCommandInfo Command =
304 MachOObj->getFirstLoadCommandInfo();
305 bool BaseSegmentAddressSet = false;
306 for (unsigned i = 0;; ++i) {
307 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
308 // We found a function starts segment, parse the addresses for later
310 MachO::linkedit_data_command LLC =
311 MachOObj->getLinkeditDataLoadCommand(Command);
313 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
314 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
315 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
316 StringRef SegName = SLC.segname;
317 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
318 BaseSegmentAddressSet = true;
319 BaseSegmentAddress = SLC.vmaddr;
323 if (i == Header.ncmds - 1)
326 Command = MachOObj->getNextLoadCommandInfo(Command);
330 static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
331 uint32_t n, uint32_t count,
332 uint32_t stride, uint64_t addr) {
333 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
334 uint32_t nindirectsyms = Dysymtab.nindirectsyms;
335 if (n > nindirectsyms)
336 outs() << " (entries start past the end of the indirect symbol "
337 "table) (reserved1 field greater than the table size)";
338 else if (n + count > nindirectsyms)
339 outs() << " (entries extends past the end of the indirect symbol "
342 uint32_t cputype = O->getHeader().cputype;
343 if (cputype & MachO::CPU_ARCH_ABI64)
344 outs() << "address index";
346 outs() << "address index";
351 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
352 if (cputype & MachO::CPU_ARCH_ABI64)
353 outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
355 outs() << format("0x%08" PRIx32, addr + j * stride) << " ";
356 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
357 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
358 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
362 if (indirect_symbol ==
363 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
364 outs() << "LOCAL ABSOLUTE\n";
367 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
368 outs() << "ABSOLUTE\n";
371 outs() << format("%5u ", indirect_symbol);
373 MachO::symtab_command Symtab = O->getSymtabLoadCommand();
374 if (indirect_symbol < Symtab.nsyms) {
375 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
376 SymbolRef Symbol = *Sym;
378 Symbol.getName(SymName);
388 static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
389 uint32_t LoadCommandCount = O->getHeader().ncmds;
390 MachOObjectFile::LoadCommandInfo Load = O->getFirstLoadCommandInfo();
391 for (unsigned I = 0;; ++I) {
392 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
393 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
394 for (unsigned J = 0; J < Seg.nsects; ++J) {
395 MachO::section_64 Sec = O->getSection64(Load, J);
396 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
397 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
398 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
399 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
400 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
401 section_type == MachO::S_SYMBOL_STUBS) {
403 if (section_type == MachO::S_SYMBOL_STUBS)
404 stride = Sec.reserved2;
408 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
409 << Sec.sectname << ") "
410 << "(size of stubs in reserved2 field is zero)\n";
413 uint32_t count = Sec.size / stride;
414 outs() << "Indirect symbols for (" << Sec.segname << ","
415 << Sec.sectname << ") " << count << " entries";
416 uint32_t n = Sec.reserved1;
417 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
420 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
421 MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
422 for (unsigned J = 0; J < Seg.nsects; ++J) {
423 MachO::section Sec = O->getSection(Load, J);
424 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
425 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
426 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
427 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
428 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
429 section_type == MachO::S_SYMBOL_STUBS) {
431 if (section_type == MachO::S_SYMBOL_STUBS)
432 stride = Sec.reserved2;
436 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
437 << Sec.sectname << ") "
438 << "(size of stubs in reserved2 field is zero)\n";
441 uint32_t count = Sec.size / stride;
442 outs() << "Indirect symbols for (" << Sec.segname << ","
443 << Sec.sectname << ") " << count << " entries";
444 uint32_t n = Sec.reserved1;
445 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
449 if (I == LoadCommandCount - 1)
452 Load = O->getNextLoadCommandInfo(Load);
456 static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
457 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
458 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
459 outs() << "Data in code table (" << nentries << " entries)\n";
460 outs() << "offset length kind\n";
461 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
464 DI->getOffset(Offset);
465 outs() << format("0x%08" PRIx32, Offset) << " ";
467 DI->getLength(Length);
468 outs() << format("%6u", Length) << " ";
473 case MachO::DICE_KIND_DATA:
476 case MachO::DICE_KIND_JUMP_TABLE8:
477 outs() << "JUMP_TABLE8";
479 case MachO::DICE_KIND_JUMP_TABLE16:
480 outs() << "JUMP_TABLE16";
482 case MachO::DICE_KIND_JUMP_TABLE32:
483 outs() << "JUMP_TABLE32";
485 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
486 outs() << "ABS_JUMP_TABLE32";
489 outs() << format("0x%04" PRIx32, Kind);
493 outs() << format("0x%04" PRIx32, Kind);
498 static void PrintLinkOptHints(MachOObjectFile *O) {
499 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
500 const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
501 uint32_t nloh = LohLC.datasize;
502 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
503 for (uint32_t i = 0; i < nloh;) {
505 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
507 outs() << " identifier " << identifier << " ";
510 switch (identifier) {
512 outs() << "AdrpAdrp\n";
515 outs() << "AdrpLdr\n";
518 outs() << "AdrpAddLdr\n";
521 outs() << "AdrpLdrGotLdr\n";
524 outs() << "AdrpAddStr\n";
527 outs() << "AdrpLdrGotStr\n";
530 outs() << "AdrpAdd\n";
533 outs() << "AdrpLdrGot\n";
536 outs() << "Unknown identifier value\n";
539 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
541 outs() << " narguments " << narguments << "\n";
545 for (uint32_t j = 0; j < narguments; j++) {
546 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
548 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
555 static void PrintDylibs(MachOObjectFile *O, bool JustId) {
556 uint32_t LoadCommandCount = O->getHeader().ncmds;
557 MachOObjectFile::LoadCommandInfo Load = O->getFirstLoadCommandInfo();
558 for (unsigned I = 0;; ++I) {
559 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
560 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
561 Load.C.cmd == MachO::LC_LOAD_DYLIB ||
562 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
563 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
564 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
565 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
566 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
567 if (dl.dylib.name < dl.cmdsize) {
568 const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
573 outs() << " (compatibility version "
574 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
575 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
576 << (dl.dylib.compatibility_version & 0xff) << ",";
577 outs() << " current version "
578 << ((dl.dylib.current_version >> 16) & 0xffff) << "."
579 << ((dl.dylib.current_version >> 8) & 0xff) << "."
580 << (dl.dylib.current_version & 0xff) << ")\n";
583 outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
584 if (Load.C.cmd == MachO::LC_ID_DYLIB)
585 outs() << "LC_ID_DYLIB ";
586 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
587 outs() << "LC_LOAD_DYLIB ";
588 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
589 outs() << "LC_LOAD_WEAK_DYLIB ";
590 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
591 outs() << "LC_LAZY_LOAD_DYLIB ";
592 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
593 outs() << "LC_REEXPORT_DYLIB ";
594 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
595 outs() << "LC_LOAD_UPWARD_DYLIB ";
598 outs() << "command " << I << "\n";
601 if (I == LoadCommandCount - 1)
604 Load = O->getNextLoadCommandInfo(Load);
608 typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
610 static void CreateSymbolAddressMap(MachOObjectFile *O,
611 SymbolAddressMap *AddrMap) {
612 // Create a map of symbol addresses to symbol names.
613 for (const SymbolRef &Symbol : O->symbols()) {
616 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
617 ST == SymbolRef::ST_Other) {
619 Symbol.getAddress(Address);
621 Symbol.getName(SymName);
622 if (!SymName.startswith(".objc"))
623 (*AddrMap)[Address] = SymName;
628 // GuessSymbolName is passed the address of what might be a symbol and a
629 // pointer to the SymbolAddressMap. It returns the name of a symbol
630 // with that address or nullptr if no symbol is found with that address.
631 static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
632 const char *SymbolName = nullptr;
633 // A DenseMap can't lookup up some values.
634 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
635 StringRef name = AddrMap->lookup(value);
637 SymbolName = name.data();
642 static void DumpCstringChar(const char c) {
646 outs().write_escaped(p);
649 static void DumpCstringSection(MachOObjectFile *O, const char *sect,
650 uint32_t sect_size, uint64_t sect_addr,
651 bool print_addresses) {
652 for (uint32_t i = 0; i < sect_size; i++) {
653 if (print_addresses) {
655 outs() << format("%016" PRIx64, sect_addr + i) << " ";
657 outs() << format("%08" PRIx64, sect_addr + i) << " ";
659 for (; i < sect_size && sect[i] != '\0'; i++)
660 DumpCstringChar(sect[i]);
661 if (i < sect_size && sect[i] == '\0')
666 static void DumpLiteral4(uint32_t l, float f) {
667 outs() << format("0x%08" PRIx32, l);
668 if ((l & 0x7f800000) != 0x7f800000)
669 outs() << format(" (%.16e)\n", f);
672 outs() << " (+Infinity)\n";
673 else if (l == 0xff800000)
674 outs() << " (-Infinity)\n";
675 else if ((l & 0x00400000) == 0x00400000)
676 outs() << " (non-signaling Not-a-Number)\n";
678 outs() << " (signaling Not-a-Number)\n";
682 static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
683 uint32_t sect_size, uint64_t sect_addr,
684 bool print_addresses) {
685 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
686 if (print_addresses) {
688 outs() << format("%016" PRIx64, sect_addr + i) << " ";
690 outs() << format("%08" PRIx64, sect_addr + i) << " ";
693 memcpy(&f, sect + i, sizeof(float));
694 if (O->isLittleEndian() != sys::IsLittleEndianHost)
695 sys::swapByteOrder(f);
697 memcpy(&l, sect + i, sizeof(uint32_t));
698 if (O->isLittleEndian() != sys::IsLittleEndianHost)
699 sys::swapByteOrder(l);
704 static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
706 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
708 if (O->isLittleEndian()) {
715 // Hi is the high word, so this is equivalent to if(isfinite(d))
716 if ((Hi & 0x7ff00000) != 0x7ff00000)
717 outs() << format(" (%.16e)\n", d);
719 if (Hi == 0x7ff00000 && Lo == 0)
720 outs() << " (+Infinity)\n";
721 else if (Hi == 0xfff00000 && Lo == 0)
722 outs() << " (-Infinity)\n";
723 else if ((Hi & 0x00080000) == 0x00080000)
724 outs() << " (non-signaling Not-a-Number)\n";
726 outs() << " (signaling Not-a-Number)\n";
730 static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
731 uint32_t sect_size, uint64_t sect_addr,
732 bool print_addresses) {
733 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
734 if (print_addresses) {
736 outs() << format("%016" PRIx64, sect_addr + i) << " ";
738 outs() << format("%08" PRIx64, sect_addr + i) << " ";
741 memcpy(&d, sect + i, sizeof(double));
742 if (O->isLittleEndian() != sys::IsLittleEndianHost)
743 sys::swapByteOrder(d);
745 memcpy(&l0, sect + i, sizeof(uint32_t));
746 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
747 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
748 sys::swapByteOrder(l0);
749 sys::swapByteOrder(l1);
751 DumpLiteral8(O, l0, l1, d);
755 static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
756 outs() << format("0x%08" PRIx32, l0) << " ";
757 outs() << format("0x%08" PRIx32, l1) << " ";
758 outs() << format("0x%08" PRIx32, l2) << " ";
759 outs() << format("0x%08" PRIx32, l3) << "\n";
762 static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
763 uint32_t sect_size, uint64_t sect_addr,
764 bool print_addresses) {
765 for (uint32_t i = 0; i < sect_size; i += 16) {
766 if (print_addresses) {
768 outs() << format("%016" PRIx64, sect_addr + i) << " ";
770 outs() << format("%08" PRIx64, sect_addr + i) << " ";
772 uint32_t l0, l1, l2, l3;
773 memcpy(&l0, sect + i, sizeof(uint32_t));
774 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
775 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
776 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
777 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
778 sys::swapByteOrder(l0);
779 sys::swapByteOrder(l1);
780 sys::swapByteOrder(l2);
781 sys::swapByteOrder(l3);
783 DumpLiteral16(l0, l1, l2, l3);
787 static void DumpLiteralPointerSection(MachOObjectFile *O,
788 const SectionRef &Section,
789 const char *sect, uint32_t sect_size,
791 bool print_addresses) {
792 // Collect the literal sections in this Mach-O file.
793 std::vector<SectionRef> LiteralSections;
794 for (const SectionRef &Section : O->sections()) {
795 DataRefImpl Ref = Section.getRawDataRefImpl();
796 uint32_t section_type;
798 const MachO::section_64 Sec = O->getSection64(Ref);
799 section_type = Sec.flags & MachO::SECTION_TYPE;
801 const MachO::section Sec = O->getSection(Ref);
802 section_type = Sec.flags & MachO::SECTION_TYPE;
804 if (section_type == MachO::S_CSTRING_LITERALS ||
805 section_type == MachO::S_4BYTE_LITERALS ||
806 section_type == MachO::S_8BYTE_LITERALS ||
807 section_type == MachO::S_16BYTE_LITERALS)
808 LiteralSections.push_back(Section);
811 // Set the size of the literal pointer.
812 uint32_t lp_size = O->is64Bit() ? 8 : 4;
814 // Collect the external relocation symbols for the the literal pointers.
815 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
816 for (const RelocationRef &Reloc : Section.relocations()) {
818 MachO::any_relocation_info RE;
819 bool isExtern = false;
820 Rel = Reloc.getRawDataRefImpl();
821 RE = O->getRelocation(Rel);
822 isExtern = O->getPlainRelocationExternal(RE);
824 uint64_t RelocOffset;
825 Reloc.getOffset(RelocOffset);
826 symbol_iterator RelocSym = Reloc.getSymbol();
827 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
830 array_pod_sort(Relocs.begin(), Relocs.end());
832 // Dump each literal pointer.
833 for (uint32_t i = 0; i < sect_size; i += lp_size) {
834 if (print_addresses) {
836 outs() << format("%016" PRIx64, sect_addr + i) << " ";
838 outs() << format("%08" PRIx64, sect_addr + i) << " ";
842 memcpy(&lp, sect + i, sizeof(uint64_t));
843 if (O->isLittleEndian() != sys::IsLittleEndianHost)
844 sys::swapByteOrder(lp);
847 memcpy(&li, sect + i, sizeof(uint32_t));
848 if (O->isLittleEndian() != sys::IsLittleEndianHost)
849 sys::swapByteOrder(li);
853 // First look for an external relocation entry for this literal pointer.
854 auto Reloc = std::find_if(
855 Relocs.begin(), Relocs.end(),
856 [&](const std::pair<uint64_t, SymbolRef> &P) { return P.first == i; });
857 if (Reloc != Relocs.end()) {
858 symbol_iterator RelocSym = Reloc->second;
860 RelocSym->getName(SymName);
861 outs() << "external relocation entry for symbol:" << SymName << "\n";
865 // For local references see what the section the literal pointer points to.
866 auto Sect = std::find_if(LiteralSections.begin(), LiteralSections.end(),
867 [&](const SectionRef &R) {
868 return lp >= R.getAddress() &&
869 lp < R.getAddress() + R.getSize();
871 if (Sect == LiteralSections.end()) {
872 outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
876 uint64_t SectAddress = Sect->getAddress();
877 uint64_t SectSize = Sect->getSize();
880 Sect->getName(SectName);
881 DataRefImpl Ref = Sect->getRawDataRefImpl();
882 StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
883 outs() << SegmentName << ":" << SectName << ":";
885 uint32_t section_type;
887 const MachO::section_64 Sec = O->getSection64(Ref);
888 section_type = Sec.flags & MachO::SECTION_TYPE;
890 const MachO::section Sec = O->getSection(Ref);
891 section_type = Sec.flags & MachO::SECTION_TYPE;
895 Sect->getContents(BytesStr);
896 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
898 switch (section_type) {
899 case MachO::S_CSTRING_LITERALS:
900 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
902 DumpCstringChar(Contents[i]);
906 case MachO::S_4BYTE_LITERALS:
908 memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
910 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
911 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
912 sys::swapByteOrder(f);
913 sys::swapByteOrder(l);
917 case MachO::S_8BYTE_LITERALS: {
919 memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
921 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
922 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
924 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
925 sys::swapByteOrder(f);
926 sys::swapByteOrder(l0);
927 sys::swapByteOrder(l1);
929 DumpLiteral8(O, l0, l1, d);
932 case MachO::S_16BYTE_LITERALS: {
933 uint32_t l0, l1, l2, l3;
934 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
935 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
937 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
939 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
941 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
942 sys::swapByteOrder(l0);
943 sys::swapByteOrder(l1);
944 sys::swapByteOrder(l2);
945 sys::swapByteOrder(l3);
947 DumpLiteral16(l0, l1, l2, l3);
954 static void DumpInitTermPointerSection(MachOObjectFile *O, const char *sect,
955 uint32_t sect_size, uint64_t sect_addr,
956 SymbolAddressMap *AddrMap,
960 stride = sizeof(uint64_t);
962 stride = sizeof(uint32_t);
963 for (uint32_t i = 0; i < sect_size; i += stride) {
964 const char *SymbolName = nullptr;
966 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
967 uint64_t pointer_value;
968 memcpy(&pointer_value, sect + i, stride);
969 if (O->isLittleEndian() != sys::IsLittleEndianHost)
970 sys::swapByteOrder(pointer_value);
971 outs() << format("0x%016" PRIx64, pointer_value);
973 SymbolName = GuessSymbolName(pointer_value, AddrMap);
975 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
976 uint32_t pointer_value;
977 memcpy(&pointer_value, sect + i, stride);
978 if (O->isLittleEndian() != sys::IsLittleEndianHost)
979 sys::swapByteOrder(pointer_value);
980 outs() << format("0x%08" PRIx32, pointer_value);
982 SymbolName = GuessSymbolName(pointer_value, AddrMap);
985 outs() << " " << SymbolName;
990 static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
991 uint32_t size, uint64_t addr) {
992 uint32_t cputype = O->getHeader().cputype;
993 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
995 for (uint32_t i = 0; i < size; i += j, addr += j) {
997 outs() << format("%016" PRIx64, addr) << "\t";
999 outs() << format("%08" PRIx64, addr) << "\t";
1000 for (j = 0; j < 16 && i + j < size; j++) {
1001 uint8_t byte_word = *(sect + i + j);
1002 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
1008 for (uint32_t i = 0; i < size; i += j, addr += j) {
1010 outs() << format("%016" PRIx64, addr) << "\t";
1012 outs() << format("%08" PRIx64, sect) << "\t";
1013 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
1014 j += sizeof(int32_t)) {
1015 if (i + j + sizeof(int32_t) < size) {
1017 memcpy(&long_word, sect + i + j, sizeof(int32_t));
1018 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1019 sys::swapByteOrder(long_word);
1020 outs() << format("%08" PRIx32, long_word) << " ";
1022 for (uint32_t k = 0; i + j + k < size; k++) {
1023 uint8_t byte_word = *(sect + i + j);
1024 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
1033 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
1034 StringRef DisSegName, StringRef DisSectName);
1035 static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
1036 uint32_t size, uint32_t addr);
1038 static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
1040 SymbolAddressMap AddrMap;
1042 CreateSymbolAddressMap(O, &AddrMap);
1044 for (unsigned i = 0; i < DumpSections.size(); ++i) {
1045 StringRef DumpSection = DumpSections[i];
1046 std::pair<StringRef, StringRef> DumpSegSectName;
1047 DumpSegSectName = DumpSection.split(',');
1048 StringRef DumpSegName, DumpSectName;
1049 if (DumpSegSectName.second.size()) {
1050 DumpSegName = DumpSegSectName.first;
1051 DumpSectName = DumpSegSectName.second;
1054 DumpSectName = DumpSegSectName.first;
1056 for (const SectionRef &Section : O->sections()) {
1058 Section.getName(SectName);
1059 DataRefImpl Ref = Section.getRawDataRefImpl();
1060 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1061 if ((DumpSegName.empty() || SegName == DumpSegName) &&
1062 (SectName == DumpSectName)) {
1064 uint32_t section_flags;
1066 const MachO::section_64 Sec = O->getSection64(Ref);
1067 section_flags = Sec.flags;
1070 const MachO::section Sec = O->getSection(Ref);
1071 section_flags = Sec.flags;
1073 uint32_t section_type = section_flags & MachO::SECTION_TYPE;
1076 Section.getContents(BytesStr);
1077 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1078 uint32_t sect_size = BytesStr.size();
1079 uint64_t sect_addr = Section.getAddress();
1082 outs().write(BytesStr.data(), BytesStr.size());
1086 outs() << "Contents of (" << SegName << "," << SectName
1090 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1091 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1092 DisassembleMachO(Filename, O, SegName, SectName);
1095 if (SegName == "__TEXT" && SectName == "__info_plist") {
1099 if (SegName == "__OBJC" && SectName == "__protocol") {
1100 DumpProtocolSection(O, sect, sect_size, sect_addr);
1103 switch (section_type) {
1104 case MachO::S_REGULAR:
1105 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1107 case MachO::S_ZEROFILL:
1108 outs() << "zerofill section and has no contents in the file\n";
1110 case MachO::S_CSTRING_LITERALS:
1111 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1113 case MachO::S_4BYTE_LITERALS:
1114 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1116 case MachO::S_8BYTE_LITERALS:
1117 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1119 case MachO::S_16BYTE_LITERALS:
1120 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1122 case MachO::S_LITERAL_POINTERS:
1123 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
1126 case MachO::S_MOD_INIT_FUNC_POINTERS:
1127 case MachO::S_MOD_TERM_FUNC_POINTERS:
1128 DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap,
1132 outs() << "Unknown section type ("
1133 << format("0x%08" PRIx32, section_type) << ")\n";
1134 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1138 if (section_type == MachO::S_ZEROFILL)
1139 outs() << "zerofill section and has no contents in the file\n";
1141 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1148 static void DumpInfoPlistSectionContents(StringRef Filename,
1149 MachOObjectFile *O) {
1150 for (const SectionRef &Section : O->sections()) {
1152 Section.getName(SectName);
1153 DataRefImpl Ref = Section.getRawDataRefImpl();
1154 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1155 if (SegName == "__TEXT" && SectName == "__info_plist") {
1156 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
1158 Section.getContents(BytesStr);
1159 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1166 // checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1167 // and if it is and there is a list of architecture flags is specified then
1168 // check to make sure this Mach-O file is one of those architectures or all
1169 // architectures were specified. If not then an error is generated and this
1170 // routine returns false. Else it returns true.
1171 static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
1172 if (isa<MachOObjectFile>(O) && !ArchAll && ArchFlags.size() != 0) {
1173 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O);
1174 bool ArchFound = false;
1175 MachO::mach_header H;
1176 MachO::mach_header_64 H_64;
1178 if (MachO->is64Bit()) {
1179 H_64 = MachO->MachOObjectFile::getHeader64();
1180 T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype);
1182 H = MachO->MachOObjectFile::getHeader();
1183 T = MachOObjectFile::getArch(H.cputype, H.cpusubtype);
1186 for (i = 0; i < ArchFlags.size(); ++i) {
1187 if (ArchFlags[i] == T.getArchName())
1192 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1193 << "architecture: " + ArchFlags[i] + "\n";
1200 static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1202 // ProcessMachO() is passed a single opened Mach-O file, which may be an
1203 // archive member and or in a slice of a universal file. It prints the
1204 // the file name and header info and then processes it according to the
1205 // command line options.
1206 static void ProcessMachO(StringRef Filename, MachOObjectFile *MachOOF,
1207 StringRef ArchiveMemberName = StringRef(),
1208 StringRef ArchitectureName = StringRef()) {
1209 // If we are doing some processing here on the Mach-O file print the header
1210 // info. And don't print it otherwise like in the case of printing the
1211 // UniversalHeaders or ArchiveHeaders.
1212 if (Disassemble || PrivateHeaders || ExportsTrie || Rebase || Bind ||
1213 LazyBind || WeakBind || IndirectSymbols || DataInCode || LinkOptHints ||
1214 DylibsUsed || DylibId || ObjcMetaData ||
1215 (DumpSections.size() != 0 && !Raw)) {
1217 if (!ArchiveMemberName.empty())
1218 outs() << '(' << ArchiveMemberName << ')';
1219 if (!ArchitectureName.empty())
1220 outs() << " (architecture " << ArchitectureName << ")";
1225 DisassembleMachO(Filename, MachOOF, "__TEXT", "__text");
1226 if (IndirectSymbols)
1227 PrintIndirectSymbols(MachOOF, !NonVerbose);
1229 PrintDataInCodeTable(MachOOF, !NonVerbose);
1231 PrintLinkOptHints(MachOOF);
1233 PrintRelocations(MachOOF);
1235 PrintSectionHeaders(MachOOF);
1236 if (SectionContents)
1237 PrintSectionContents(MachOOF);
1238 if (DumpSections.size() != 0)
1239 DumpSectionContents(Filename, MachOOF, !NonVerbose);
1241 DumpInfoPlistSectionContents(Filename, MachOOF);
1243 PrintDylibs(MachOOF, false);
1245 PrintDylibs(MachOOF, true);
1247 PrintSymbolTable(MachOOF);
1249 printMachOUnwindInfo(MachOOF);
1251 printMachOFileHeader(MachOOF);
1253 printObjcMetaData(MachOOF, !NonVerbose);
1255 printExportsTrie(MachOOF);
1257 printRebaseTable(MachOOF);
1259 printBindTable(MachOOF);
1261 printLazyBindTable(MachOOF);
1263 printWeakBindTable(MachOOF);
1266 // printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1267 static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
1268 outs() << " cputype (" << cputype << ")\n";
1269 outs() << " cpusubtype (" << cpusubtype << ")\n";
1272 // printCPUType() helps print_fat_headers by printing the cputype and
1273 // pusubtype (symbolically for the one's it knows about).
1274 static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
1276 case MachO::CPU_TYPE_I386:
1277 switch (cpusubtype) {
1278 case MachO::CPU_SUBTYPE_I386_ALL:
1279 outs() << " cputype CPU_TYPE_I386\n";
1280 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
1283 printUnknownCPUType(cputype, cpusubtype);
1287 case MachO::CPU_TYPE_X86_64:
1288 switch (cpusubtype) {
1289 case MachO::CPU_SUBTYPE_X86_64_ALL:
1290 outs() << " cputype CPU_TYPE_X86_64\n";
1291 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1293 case MachO::CPU_SUBTYPE_X86_64_H:
1294 outs() << " cputype CPU_TYPE_X86_64\n";
1295 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
1298 printUnknownCPUType(cputype, cpusubtype);
1302 case MachO::CPU_TYPE_ARM:
1303 switch (cpusubtype) {
1304 case MachO::CPU_SUBTYPE_ARM_ALL:
1305 outs() << " cputype CPU_TYPE_ARM\n";
1306 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
1308 case MachO::CPU_SUBTYPE_ARM_V4T:
1309 outs() << " cputype CPU_TYPE_ARM\n";
1310 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
1312 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1313 outs() << " cputype CPU_TYPE_ARM\n";
1314 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
1316 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1317 outs() << " cputype CPU_TYPE_ARM\n";
1318 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
1320 case MachO::CPU_SUBTYPE_ARM_V6:
1321 outs() << " cputype CPU_TYPE_ARM\n";
1322 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
1324 case MachO::CPU_SUBTYPE_ARM_V6M:
1325 outs() << " cputype CPU_TYPE_ARM\n";
1326 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
1328 case MachO::CPU_SUBTYPE_ARM_V7:
1329 outs() << " cputype CPU_TYPE_ARM\n";
1330 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
1332 case MachO::CPU_SUBTYPE_ARM_V7EM:
1333 outs() << " cputype CPU_TYPE_ARM\n";
1334 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
1336 case MachO::CPU_SUBTYPE_ARM_V7K:
1337 outs() << " cputype CPU_TYPE_ARM\n";
1338 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
1340 case MachO::CPU_SUBTYPE_ARM_V7M:
1341 outs() << " cputype CPU_TYPE_ARM\n";
1342 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
1344 case MachO::CPU_SUBTYPE_ARM_V7S:
1345 outs() << " cputype CPU_TYPE_ARM\n";
1346 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
1349 printUnknownCPUType(cputype, cpusubtype);
1353 case MachO::CPU_TYPE_ARM64:
1354 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1355 case MachO::CPU_SUBTYPE_ARM64_ALL:
1356 outs() << " cputype CPU_TYPE_ARM64\n";
1357 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
1360 printUnknownCPUType(cputype, cpusubtype);
1365 printUnknownCPUType(cputype, cpusubtype);
1370 static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
1372 outs() << "Fat headers\n";
1374 outs() << "fat_magic FAT_MAGIC\n";
1376 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
1378 uint32_t nfat_arch = UB->getNumberOfObjects();
1379 StringRef Buf = UB->getData();
1380 uint64_t size = Buf.size();
1381 uint64_t big_size = sizeof(struct MachO::fat_header) +
1382 nfat_arch * sizeof(struct MachO::fat_arch);
1383 outs() << "nfat_arch " << UB->getNumberOfObjects();
1385 outs() << " (malformed, contains zero architecture types)\n";
1386 else if (big_size > size)
1387 outs() << " (malformed, architectures past end of file)\n";
1391 for (uint32_t i = 0; i < nfat_arch; ++i) {
1392 MachOUniversalBinary::ObjectForArch OFA(UB, i);
1393 uint32_t cputype = OFA.getCPUType();
1394 uint32_t cpusubtype = OFA.getCPUSubType();
1395 outs() << "architecture ";
1396 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
1397 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
1398 uint32_t other_cputype = other_OFA.getCPUType();
1399 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
1400 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
1401 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
1402 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
1403 outs() << "(illegal duplicate architecture) ";
1408 outs() << OFA.getArchTypeName() << "\n";
1409 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1411 outs() << i << "\n";
1412 outs() << " cputype " << cputype << "\n";
1413 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
1417 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
1418 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
1420 outs() << " capabilities "
1421 << format("0x%" PRIx32,
1422 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
1423 outs() << " offset " << OFA.getOffset();
1424 if (OFA.getOffset() > size)
1425 outs() << " (past end of file)";
1426 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
1427 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
1429 outs() << " size " << OFA.getSize();
1430 big_size = OFA.getOffset() + OFA.getSize();
1431 if (big_size > size)
1432 outs() << " (past end of file)";
1434 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
1439 static void printArchiveChild(Archive::Child &C, bool verbose,
1440 bool print_offset) {
1442 outs() << C.getChildOffset() << "\t";
1443 sys::fs::perms Mode = C.getAccessMode();
1445 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
1446 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
1448 if (Mode & sys::fs::owner_read)
1452 if (Mode & sys::fs::owner_write)
1456 if (Mode & sys::fs::owner_exe)
1460 if (Mode & sys::fs::group_read)
1464 if (Mode & sys::fs::group_write)
1468 if (Mode & sys::fs::group_exe)
1472 if (Mode & sys::fs::others_read)
1476 if (Mode & sys::fs::others_write)
1480 if (Mode & sys::fs::others_exe)
1485 outs() << format("0%o ", Mode);
1488 unsigned UID = C.getUID();
1489 outs() << format("%3d/", UID);
1490 unsigned GID = C.getGID();
1491 outs() << format("%-3d ", GID);
1492 uint64_t Size = C.getRawSize();
1493 outs() << format("%5" PRId64, Size) << " ";
1495 StringRef RawLastModified = C.getRawLastModified();
1498 if (RawLastModified.getAsInteger(10, Seconds))
1499 outs() << "(date: \"%s\" contains non-decimal chars) " << RawLastModified;
1501 // Since cime(3) returns a 26 character string of the form:
1502 // "Sun Sep 16 01:03:52 1973\n\0"
1503 // just print 24 characters.
1505 outs() << format("%.24s ", ctime(&t));
1508 outs() << RawLastModified << " ";
1512 ErrorOr<StringRef> NameOrErr = C.getName();
1513 if (NameOrErr.getError()) {
1514 StringRef RawName = C.getRawName();
1515 outs() << RawName << "\n";
1517 StringRef Name = NameOrErr.get();
1518 outs() << Name << "\n";
1521 StringRef RawName = C.getRawName();
1522 outs() << RawName << "\n";
1526 static void printArchiveHeaders(Archive *A, bool verbose, bool print_offset) {
1527 if (A->hasSymbolTable()) {
1528 Archive::child_iterator S = A->getSymbolTableChild();
1529 Archive::Child C = *S;
1530 printArchiveChild(C, verbose, print_offset);
1532 for (Archive::child_iterator I = A->child_begin(), E = A->child_end(); I != E;
1534 Archive::Child C = *I;
1535 printArchiveChild(C, verbose, print_offset);
1539 // ParseInputMachO() parses the named Mach-O file in Filename and handles the
1540 // -arch flags selecting just those slices as specified by them and also parses
1541 // archive files. Then for each individual Mach-O file ProcessMachO() is
1542 // called to process the file based on the command line options.
1543 void llvm::ParseInputMachO(StringRef Filename) {
1544 // Check for -arch all and verifiy the -arch flags are valid.
1545 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1546 if (ArchFlags[i] == "all") {
1549 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
1550 errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
1551 "'for the -arch option\n";
1557 // Attempt to open the binary.
1558 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
1559 if (std::error_code EC = BinaryOrErr.getError()) {
1560 errs() << "llvm-objdump: '" << Filename << "': " << EC.message() << ".\n";
1563 Binary &Bin = *BinaryOrErr.get().getBinary();
1565 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1566 outs() << "Archive : " << Filename << "\n";
1568 printArchiveHeaders(A, !NonVerbose, ArchiveMemberOffsets);
1569 for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
1571 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = I->getAsBinary();
1572 if (ChildOrErr.getError())
1574 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1575 if (!checkMachOAndArchFlags(O, Filename))
1577 ProcessMachO(Filename, O, O->getFileName());
1582 if (UniversalHeaders) {
1583 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
1584 printMachOUniversalHeaders(UB, !NonVerbose);
1586 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1587 // If we have a list of architecture flags specified dump only those.
1588 if (!ArchAll && ArchFlags.size() != 0) {
1589 // Look for a slice in the universal binary that matches each ArchFlag.
1591 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1593 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1594 E = UB->end_objects();
1596 if (ArchFlags[i] == I->getArchTypeName()) {
1598 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
1599 I->getAsObjectFile();
1600 std::string ArchitectureName = "";
1601 if (ArchFlags.size() > 1)
1602 ArchitectureName = I->getArchTypeName();
1604 ObjectFile &O = *ObjOrErr.get();
1605 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
1606 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
1607 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1608 I->getAsArchive()) {
1609 std::unique_ptr<Archive> &A = *AOrErr;
1610 outs() << "Archive : " << Filename;
1611 if (!ArchitectureName.empty())
1612 outs() << " (architecture " << ArchitectureName << ")";
1615 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
1616 for (Archive::child_iterator AI = A->child_begin(),
1617 AE = A->child_end();
1619 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1620 if (ChildOrErr.getError())
1622 if (MachOObjectFile *O =
1623 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
1624 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
1630 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1631 << "architecture: " + ArchFlags[i] + "\n";
1637 // No architecture flags were specified so if this contains a slice that
1638 // matches the host architecture dump only that.
1640 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1641 E = UB->end_objects();
1643 if (MachOObjectFile::getHostArch().getArchName() ==
1644 I->getArchTypeName()) {
1645 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1646 std::string ArchiveName;
1647 ArchiveName.clear();
1649 ObjectFile &O = *ObjOrErr.get();
1650 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
1651 ProcessMachO(Filename, MachOOF);
1652 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1653 I->getAsArchive()) {
1654 std::unique_ptr<Archive> &A = *AOrErr;
1655 outs() << "Archive : " << Filename << "\n";
1657 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
1658 for (Archive::child_iterator AI = A->child_begin(),
1659 AE = A->child_end();
1661 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1662 if (ChildOrErr.getError())
1664 if (MachOObjectFile *O =
1665 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
1666 ProcessMachO(Filename, O, O->getFileName());
1673 // Either all architectures have been specified or none have been specified
1674 // and this does not contain the host architecture so dump all the slices.
1675 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1676 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1677 E = UB->end_objects();
1679 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1680 std::string ArchitectureName = "";
1681 if (moreThanOneArch)
1682 ArchitectureName = I->getArchTypeName();
1684 ObjectFile &Obj = *ObjOrErr.get();
1685 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
1686 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
1687 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
1688 std::unique_ptr<Archive> &A = *AOrErr;
1689 outs() << "Archive : " << Filename;
1690 if (!ArchitectureName.empty())
1691 outs() << " (architecture " << ArchitectureName << ")";
1694 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
1695 for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
1697 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1698 if (ChildOrErr.getError())
1700 if (MachOObjectFile *O =
1701 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1702 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
1703 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
1711 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
1712 if (!checkMachOAndArchFlags(O, Filename))
1714 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
1715 ProcessMachO(Filename, MachOOF);
1717 errs() << "llvm-objdump: '" << Filename << "': "
1718 << "Object is not a Mach-O file type.\n";
1720 errs() << "llvm-objdump: '" << Filename << "': "
1721 << "Unrecognized file type.\n";
1724 typedef std::pair<uint64_t, const char *> BindInfoEntry;
1725 typedef std::vector<BindInfoEntry> BindTable;
1726 typedef BindTable::iterator bind_table_iterator;
1728 // The block of info used by the Symbolizer call backs.
1729 struct DisassembleInfo {
1733 SymbolAddressMap *AddrMap;
1734 std::vector<SectionRef> *Sections;
1735 const char *class_name;
1736 const char *selector_name;
1738 char *demangled_name;
1741 BindTable *bindtable;
1744 // SymbolizerGetOpInfo() is the operand information call back function.
1745 // This is called to get the symbolic information for operand(s) of an
1746 // instruction when it is being done. This routine does this from
1747 // the relocation information, symbol table, etc. That block of information
1748 // is a pointer to the struct DisassembleInfo that was passed when the
1749 // disassembler context was created and passed to back to here when
1750 // called back by the disassembler for instruction operands that could have
1751 // relocation information. The address of the instruction containing operand is
1752 // at the Pc parameter. The immediate value the operand has is passed in
1753 // op_info->Value and is at Offset past the start of the instruction and has a
1754 // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
1755 // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
1756 // names and addends of the symbolic expression to add for the operand. The
1757 // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
1758 // information is returned then this function returns 1 else it returns 0.
1759 static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
1760 uint64_t Size, int TagType, void *TagBuf) {
1761 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
1762 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
1763 uint64_t value = op_info->Value;
1765 // Make sure all fields returned are zero if we don't set them.
1766 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
1767 op_info->Value = value;
1769 // If the TagType is not the value 1 which it code knows about or if no
1770 // verbose symbolic information is wanted then just return 0, indicating no
1771 // information is being returned.
1772 if (TagType != 1 || !info->verbose)
1775 unsigned int Arch = info->O->getArch();
1776 if (Arch == Triple::x86) {
1777 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1779 // First search the section's relocation entries (if any) for an entry
1780 // for this section offset.
1781 uint32_t sect_addr = info->S.getAddress();
1782 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1783 bool reloc_found = false;
1785 MachO::any_relocation_info RE;
1786 bool isExtern = false;
1788 bool r_scattered = false;
1789 uint32_t r_value, pair_r_value, r_type;
1790 for (const RelocationRef &Reloc : info->S.relocations()) {
1791 uint64_t RelocOffset;
1792 Reloc.getOffset(RelocOffset);
1793 if (RelocOffset == sect_offset) {
1794 Rel = Reloc.getRawDataRefImpl();
1795 RE = info->O->getRelocation(Rel);
1796 r_type = info->O->getAnyRelocationType(RE);
1797 r_scattered = info->O->isRelocationScattered(RE);
1799 r_value = info->O->getScatteredRelocationValue(RE);
1800 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1801 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
1802 DataRefImpl RelNext = Rel;
1803 info->O->moveRelocationNext(RelNext);
1804 MachO::any_relocation_info RENext;
1805 RENext = info->O->getRelocation(RelNext);
1806 if (info->O->isRelocationScattered(RENext))
1807 pair_r_value = info->O->getScatteredRelocationValue(RENext);
1812 isExtern = info->O->getPlainRelocationExternal(RE);
1814 symbol_iterator RelocSym = Reloc.getSymbol();
1822 if (reloc_found && isExtern) {
1824 Symbol.getName(SymName);
1825 const char *name = SymName.data();
1826 op_info->AddSymbol.Present = 1;
1827 op_info->AddSymbol.Name = name;
1828 // For i386 extern relocation entries the value in the instruction is
1829 // the offset from the symbol, and value is already set in op_info->Value.
1832 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1833 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
1834 const char *add = GuessSymbolName(r_value, info->AddrMap);
1835 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
1836 uint32_t offset = value - (r_value - pair_r_value);
1837 op_info->AddSymbol.Present = 1;
1839 op_info->AddSymbol.Name = add;
1841 op_info->AddSymbol.Value = r_value;
1842 op_info->SubtractSymbol.Present = 1;
1844 op_info->SubtractSymbol.Name = sub;
1846 op_info->SubtractSymbol.Value = pair_r_value;
1847 op_info->Value = offset;
1851 // Second search the external relocation entries of a fully linked image
1852 // (if any) for an entry that matches this segment offset.
1853 // uint32_t seg_offset = (Pc + Offset);
1856 if (Arch == Triple::x86_64) {
1857 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1859 // First search the section's relocation entries (if any) for an entry
1860 // for this section offset.
1861 uint64_t sect_addr = info->S.getAddress();
1862 uint64_t sect_offset = (Pc + Offset) - sect_addr;
1863 bool reloc_found = false;
1865 MachO::any_relocation_info RE;
1866 bool isExtern = false;
1868 for (const RelocationRef &Reloc : info->S.relocations()) {
1869 uint64_t RelocOffset;
1870 Reloc.getOffset(RelocOffset);
1871 if (RelocOffset == sect_offset) {
1872 Rel = Reloc.getRawDataRefImpl();
1873 RE = info->O->getRelocation(Rel);
1874 // NOTE: Scattered relocations don't exist on x86_64.
1875 isExtern = info->O->getPlainRelocationExternal(RE);
1877 symbol_iterator RelocSym = Reloc.getSymbol();
1884 if (reloc_found && isExtern) {
1885 // The Value passed in will be adjusted by the Pc if the instruction
1886 // adds the Pc. But for x86_64 external relocation entries the Value
1887 // is the offset from the external symbol.
1888 if (info->O->getAnyRelocationPCRel(RE))
1889 op_info->Value -= Pc + Offset + Size;
1891 Symbol.getName(SymName);
1892 const char *name = SymName.data();
1893 unsigned Type = info->O->getAnyRelocationType(RE);
1894 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
1895 DataRefImpl RelNext = Rel;
1896 info->O->moveRelocationNext(RelNext);
1897 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
1898 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
1899 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
1900 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
1901 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
1902 op_info->SubtractSymbol.Present = 1;
1903 op_info->SubtractSymbol.Name = name;
1904 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
1905 Symbol = *RelocSymNext;
1906 StringRef SymNameNext;
1907 Symbol.getName(SymNameNext);
1908 name = SymNameNext.data();
1911 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
1912 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
1913 op_info->AddSymbol.Present = 1;
1914 op_info->AddSymbol.Name = name;
1918 // Second search the external relocation entries of a fully linked image
1919 // (if any) for an entry that matches this segment offset.
1920 // uint64_t seg_offset = (Pc + Offset);
1923 if (Arch == Triple::arm) {
1924 if (Offset != 0 || (Size != 4 && Size != 2))
1926 // First search the section's relocation entries (if any) for an entry
1927 // for this section offset.
1928 uint32_t sect_addr = info->S.getAddress();
1929 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1931 MachO::any_relocation_info RE;
1932 bool isExtern = false;
1934 bool r_scattered = false;
1935 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
1937 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
1938 [&](const RelocationRef &Reloc) {
1939 uint64_t RelocOffset;
1940 Reloc.getOffset(RelocOffset);
1941 return RelocOffset == sect_offset;
1944 if (Reloc == info->S.relocations().end())
1947 Rel = Reloc->getRawDataRefImpl();
1948 RE = info->O->getRelocation(Rel);
1949 r_length = info->O->getAnyRelocationLength(RE);
1950 r_scattered = info->O->isRelocationScattered(RE);
1952 r_value = info->O->getScatteredRelocationValue(RE);
1953 r_type = info->O->getScatteredRelocationType(RE);
1955 r_type = info->O->getAnyRelocationType(RE);
1956 isExtern = info->O->getPlainRelocationExternal(RE);
1958 symbol_iterator RelocSym = Reloc->getSymbol();
1962 if (r_type == MachO::ARM_RELOC_HALF ||
1963 r_type == MachO::ARM_RELOC_SECTDIFF ||
1964 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
1965 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1966 DataRefImpl RelNext = Rel;
1967 info->O->moveRelocationNext(RelNext);
1968 MachO::any_relocation_info RENext;
1969 RENext = info->O->getRelocation(RelNext);
1970 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
1971 if (info->O->isRelocationScattered(RENext))
1972 pair_r_value = info->O->getScatteredRelocationValue(RENext);
1977 Symbol.getName(SymName);
1978 const char *name = SymName.data();
1979 op_info->AddSymbol.Present = 1;
1980 op_info->AddSymbol.Name = name;
1982 case MachO::ARM_RELOC_HALF:
1983 if ((r_length & 0x1) == 1) {
1984 op_info->Value = value << 16 | other_half;
1985 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1987 op_info->Value = other_half << 16 | value;
1988 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
1996 // If we have a branch that is not an external relocation entry then
1997 // return 0 so the code in tryAddingSymbolicOperand() can use the
1998 // SymbolLookUp call back with the branch target address to look up the
1999 // symbol and possiblity add an annotation for a symbol stub.
2000 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
2001 r_type == MachO::ARM_THUMB_RELOC_BR22))
2004 uint32_t offset = 0;
2005 if (r_type == MachO::ARM_RELOC_HALF ||
2006 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2007 if ((r_length & 0x1) == 1)
2008 value = value << 16 | other_half;
2010 value = other_half << 16 | value;
2012 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
2013 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
2014 offset = value - r_value;
2018 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2019 if ((r_length & 0x1) == 1)
2020 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2022 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2023 const char *add = GuessSymbolName(r_value, info->AddrMap);
2024 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
2025 int32_t offset = value - (r_value - pair_r_value);
2026 op_info->AddSymbol.Present = 1;
2028 op_info->AddSymbol.Name = add;
2030 op_info->AddSymbol.Value = r_value;
2031 op_info->SubtractSymbol.Present = 1;
2033 op_info->SubtractSymbol.Name = sub;
2035 op_info->SubtractSymbol.Value = pair_r_value;
2036 op_info->Value = offset;
2040 op_info->AddSymbol.Present = 1;
2041 op_info->Value = offset;
2042 if (r_type == MachO::ARM_RELOC_HALF) {
2043 if ((r_length & 0x1) == 1)
2044 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2046 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2048 const char *add = GuessSymbolName(value, info->AddrMap);
2049 if (add != nullptr) {
2050 op_info->AddSymbol.Name = add;
2053 op_info->AddSymbol.Value = value;
2056 if (Arch == Triple::aarch64) {
2057 if (Offset != 0 || Size != 4)
2059 // First search the section's relocation entries (if any) for an entry
2060 // for this section offset.
2061 uint64_t sect_addr = info->S.getAddress();
2062 uint64_t sect_offset = (Pc + Offset) - sect_addr;
2064 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
2065 [&](const RelocationRef &Reloc) {
2066 uint64_t RelocOffset;
2067 Reloc.getOffset(RelocOffset);
2068 return RelocOffset == sect_offset;
2071 if (Reloc == info->S.relocations().end())
2074 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2075 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2076 uint32_t r_type = info->O->getAnyRelocationType(RE);
2077 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2078 DataRefImpl RelNext = Rel;
2079 info->O->moveRelocationNext(RelNext);
2080 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2082 value = info->O->getPlainRelocationSymbolNum(RENext);
2083 op_info->Value = value;
2086 // NOTE: Scattered relocations don't exist on arm64.
2087 if (!info->O->getPlainRelocationExternal(RE))
2090 Reloc->getSymbol()->getName(SymName);
2091 const char *name = SymName.data();
2092 op_info->AddSymbol.Present = 1;
2093 op_info->AddSymbol.Name = name;
2096 case MachO::ARM64_RELOC_PAGE21:
2098 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2100 case MachO::ARM64_RELOC_PAGEOFF12:
2102 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2104 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2106 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2108 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2110 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2112 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2113 /* @tvlppage is not implemented in llvm-mc */
2114 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2116 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2117 /* @tvlppageoff is not implemented in llvm-mc */
2118 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2121 case MachO::ARM64_RELOC_BRANCH26:
2122 op_info->VariantKind = LLVMDisassembler_VariantKind_None;
2130 // GuessCstringPointer is passed the address of what might be a pointer to a
2131 // literal string in a cstring section. If that address is in a cstring section
2132 // it returns a pointer to that string. Else it returns nullptr.
2133 static const char *GuessCstringPointer(uint64_t ReferenceValue,
2134 struct DisassembleInfo *info) {
2135 uint32_t LoadCommandCount = info->O->getHeader().ncmds;
2136 MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo();
2137 for (unsigned I = 0;; ++I) {
2138 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2139 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2140 for (unsigned J = 0; J < Seg.nsects; ++J) {
2141 MachO::section_64 Sec = info->O->getSection64(Load, J);
2142 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2143 if (section_type == MachO::S_CSTRING_LITERALS &&
2144 ReferenceValue >= Sec.addr &&
2145 ReferenceValue < Sec.addr + Sec.size) {
2146 uint64_t sect_offset = ReferenceValue - Sec.addr;
2147 uint64_t object_offset = Sec.offset + sect_offset;
2148 StringRef MachOContents = info->O->getData();
2149 uint64_t object_size = MachOContents.size();
2150 const char *object_addr = (const char *)MachOContents.data();
2151 if (object_offset < object_size) {
2152 const char *name = object_addr + object_offset;
2159 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2160 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2161 for (unsigned J = 0; J < Seg.nsects; ++J) {
2162 MachO::section Sec = info->O->getSection(Load, J);
2163 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2164 if (section_type == MachO::S_CSTRING_LITERALS &&
2165 ReferenceValue >= Sec.addr &&
2166 ReferenceValue < Sec.addr + Sec.size) {
2167 uint64_t sect_offset = ReferenceValue - Sec.addr;
2168 uint64_t object_offset = Sec.offset + sect_offset;
2169 StringRef MachOContents = info->O->getData();
2170 uint64_t object_size = MachOContents.size();
2171 const char *object_addr = (const char *)MachOContents.data();
2172 if (object_offset < object_size) {
2173 const char *name = object_addr + object_offset;
2181 if (I == LoadCommandCount - 1)
2184 Load = info->O->getNextLoadCommandInfo(Load);
2189 // GuessIndirectSymbol returns the name of the indirect symbol for the
2190 // ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
2191 // an address of a symbol stub or a lazy or non-lazy pointer to associate the
2192 // symbol name being referenced by the stub or pointer.
2193 static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2194 struct DisassembleInfo *info) {
2195 uint32_t LoadCommandCount = info->O->getHeader().ncmds;
2196 MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo();
2197 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2198 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
2199 for (unsigned I = 0;; ++I) {
2200 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2201 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2202 for (unsigned J = 0; J < Seg.nsects; ++J) {
2203 MachO::section_64 Sec = info->O->getSection64(Load, J);
2204 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2205 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2206 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2207 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2208 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2209 section_type == MachO::S_SYMBOL_STUBS) &&
2210 ReferenceValue >= Sec.addr &&
2211 ReferenceValue < Sec.addr + Sec.size) {
2213 if (section_type == MachO::S_SYMBOL_STUBS)
2214 stride = Sec.reserved2;
2219 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2220 if (index < Dysymtab.nindirectsyms) {
2221 uint32_t indirect_symbol =
2222 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
2223 if (indirect_symbol < Symtab.nsyms) {
2224 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2225 SymbolRef Symbol = *Sym;
2227 Symbol.getName(SymName);
2228 const char *name = SymName.data();
2234 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2235 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2236 for (unsigned J = 0; J < Seg.nsects; ++J) {
2237 MachO::section Sec = info->O->getSection(Load, J);
2238 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2239 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2240 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2241 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2242 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2243 section_type == MachO::S_SYMBOL_STUBS) &&
2244 ReferenceValue >= Sec.addr &&
2245 ReferenceValue < Sec.addr + Sec.size) {
2247 if (section_type == MachO::S_SYMBOL_STUBS)
2248 stride = Sec.reserved2;
2253 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2254 if (index < Dysymtab.nindirectsyms) {
2255 uint32_t indirect_symbol =
2256 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
2257 if (indirect_symbol < Symtab.nsyms) {
2258 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2259 SymbolRef Symbol = *Sym;
2261 Symbol.getName(SymName);
2262 const char *name = SymName.data();
2269 if (I == LoadCommandCount - 1)
2272 Load = info->O->getNextLoadCommandInfo(Load);
2277 // method_reference() is called passing it the ReferenceName that might be
2278 // a reference it to an Objective-C method call. If so then it allocates and
2279 // assembles a method call string with the values last seen and saved in
2280 // the DisassembleInfo's class_name and selector_name fields. This is saved
2281 // into the method field of the info and any previous string is free'ed.
2282 // Then the class_name field in the info is set to nullptr. The method call
2283 // string is set into ReferenceName and ReferenceType is set to
2284 // LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
2285 // then both ReferenceType and ReferenceName are left unchanged.
2286 static void method_reference(struct DisassembleInfo *info,
2287 uint64_t *ReferenceType,
2288 const char **ReferenceName) {
2289 unsigned int Arch = info->O->getArch();
2290 if (*ReferenceName != nullptr) {
2291 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
2292 if (info->selector_name != nullptr) {
2293 if (info->method != nullptr)
2295 if (info->class_name != nullptr) {
2296 info->method = (char *)malloc(5 + strlen(info->class_name) +
2297 strlen(info->selector_name));
2298 if (info->method != nullptr) {
2299 strcpy(info->method, "+[");
2300 strcat(info->method, info->class_name);
2301 strcat(info->method, " ");
2302 strcat(info->method, info->selector_name);
2303 strcat(info->method, "]");
2304 *ReferenceName = info->method;
2305 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2308 info->method = (char *)malloc(9 + strlen(info->selector_name));
2309 if (info->method != nullptr) {
2310 if (Arch == Triple::x86_64)
2311 strcpy(info->method, "-[%rdi ");
2312 else if (Arch == Triple::aarch64)
2313 strcpy(info->method, "-[x0 ");
2315 strcpy(info->method, "-[r? ");
2316 strcat(info->method, info->selector_name);
2317 strcat(info->method, "]");
2318 *ReferenceName = info->method;
2319 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2322 info->class_name = nullptr;
2324 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
2325 if (info->selector_name != nullptr) {
2326 if (info->method != nullptr)
2328 info->method = (char *)malloc(17 + strlen(info->selector_name));
2329 if (info->method != nullptr) {
2330 if (Arch == Triple::x86_64)
2331 strcpy(info->method, "-[[%rdi super] ");
2332 else if (Arch == Triple::aarch64)
2333 strcpy(info->method, "-[[x0 super] ");
2335 strcpy(info->method, "-[[r? super] ");
2336 strcat(info->method, info->selector_name);
2337 strcat(info->method, "]");
2338 *ReferenceName = info->method;
2339 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2341 info->class_name = nullptr;
2347 // GuessPointerPointer() is passed the address of what might be a pointer to
2348 // a reference to an Objective-C class, selector, message ref or cfstring.
2349 // If so the value of the pointer is returned and one of the booleans are set
2350 // to true. If not zero is returned and all the booleans are set to false.
2351 static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
2352 struct DisassembleInfo *info,
2353 bool &classref, bool &selref, bool &msgref,
2359 uint32_t LoadCommandCount = info->O->getHeader().ncmds;
2360 MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo();
2361 for (unsigned I = 0;; ++I) {
2362 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2363 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2364 for (unsigned J = 0; J < Seg.nsects; ++J) {
2365 MachO::section_64 Sec = info->O->getSection64(Load, J);
2366 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
2367 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2368 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
2369 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
2370 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
2371 ReferenceValue >= Sec.addr &&
2372 ReferenceValue < Sec.addr + Sec.size) {
2373 uint64_t sect_offset = ReferenceValue - Sec.addr;
2374 uint64_t object_offset = Sec.offset + sect_offset;
2375 StringRef MachOContents = info->O->getData();
2376 uint64_t object_size = MachOContents.size();
2377 const char *object_addr = (const char *)MachOContents.data();
2378 if (object_offset < object_size) {
2379 uint64_t pointer_value;
2380 memcpy(&pointer_value, object_addr + object_offset,
2382 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2383 sys::swapByteOrder(pointer_value);
2384 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
2386 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2387 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
2389 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
2390 ReferenceValue + 8 < Sec.addr + Sec.size) {
2392 memcpy(&pointer_value, object_addr + object_offset + 8,
2394 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2395 sys::swapByteOrder(pointer_value);
2396 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
2398 return pointer_value;
2405 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
2406 if (I == LoadCommandCount - 1)
2409 Load = info->O->getNextLoadCommandInfo(Load);
2414 // get_pointer_64 returns a pointer to the bytes in the object file at the
2415 // Address from a section in the Mach-O file. And indirectly returns the
2416 // offset into the section, number of bytes left in the section past the offset
2417 // and which section is was being referenced. If the Address is not in a
2418 // section nullptr is returned.
2419 static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
2420 uint32_t &left, SectionRef &S,
2421 DisassembleInfo *info,
2422 bool objc_only = false) {
2426 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
2427 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
2428 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
2431 ((*(info->Sections))[SectIdx]).getName(SectName);
2432 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
2433 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
2434 if (SegName != "__OBJC" && SectName != "__cstring")
2437 if (Address >= SectAddress && Address < SectAddress + SectSize) {
2438 S = (*(info->Sections))[SectIdx];
2439 offset = Address - SectAddress;
2440 left = SectSize - offset;
2441 StringRef SectContents;
2442 ((*(info->Sections))[SectIdx]).getContents(SectContents);
2443 return SectContents.data() + offset;
2449 static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
2450 uint32_t &left, SectionRef &S,
2451 DisassembleInfo *info,
2452 bool objc_only = false) {
2453 return get_pointer_64(Address, offset, left, S, info, objc_only);
2456 // get_symbol_64() returns the name of a symbol (or nullptr) and the address of
2457 // the symbol indirectly through n_value. Based on the relocation information
2458 // for the specified section offset in the specified section reference.
2459 // If no relocation information is found and a non-zero ReferenceValue for the
2460 // symbol is passed, look up that address in the info's AddrMap.
2462 get_symbol_64(uint32_t sect_offset, SectionRef S, DisassembleInfo *info,
2464 uint64_t ReferenceValue = UnknownAddressOrSize) {
2469 // See if there is an external relocation entry at the sect_offset.
2470 bool reloc_found = false;
2472 MachO::any_relocation_info RE;
2473 bool isExtern = false;
2475 for (const RelocationRef &Reloc : S.relocations()) {
2476 uint64_t RelocOffset;
2477 Reloc.getOffset(RelocOffset);
2478 if (RelocOffset == sect_offset) {
2479 Rel = Reloc.getRawDataRefImpl();
2480 RE = info->O->getRelocation(Rel);
2481 if (info->O->isRelocationScattered(RE))
2483 isExtern = info->O->getPlainRelocationExternal(RE);
2485 symbol_iterator RelocSym = Reloc.getSymbol();
2492 // If there is an external relocation entry for a symbol in this section
2493 // at this section_offset then use that symbol's value for the n_value
2494 // and return its name.
2495 const char *SymbolName = nullptr;
2496 if (reloc_found && isExtern) {
2497 Symbol.getAddress(n_value);
2498 if (n_value == UnknownAddressOrSize)
2501 Symbol.getName(name);
2502 if (!name.empty()) {
2503 SymbolName = name.data();
2508 // TODO: For fully linked images, look through the external relocation
2509 // entries off the dynamic symtab command. For these the r_offset is from the
2510 // start of the first writeable segment in the Mach-O file. So the offset
2511 // to this section from that segment is passed to this routine by the caller,
2512 // as the database_offset. Which is the difference of the section's starting
2513 // address and the first writable segment.
2515 // NOTE: need add passing the database_offset to this routine.
2517 // We did not find an external relocation entry so look up the ReferenceValue
2518 // as an address of a symbol and if found return that symbol's name.
2519 if (ReferenceValue != UnknownAddressOrSize)
2520 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
2525 static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
2526 DisassembleInfo *info,
2527 uint32_t ReferenceValue) {
2529 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
2532 // These are structs in the Objective-C meta data and read to produce the
2533 // comments for disassembly. While these are part of the ABI they are no
2534 // public defintions. So the are here not in include/llvm/Support/MachO.h .
2536 // The cfstring object in a 64-bit Mach-O file.
2537 struct cfstring64_t {
2538 uint64_t isa; // class64_t * (64-bit pointer)
2539 uint64_t flags; // flag bits
2540 uint64_t characters; // char * (64-bit pointer)
2541 uint64_t length; // number of non-NULL characters in above
2544 // The class object in a 64-bit Mach-O file.
2546 uint64_t isa; // class64_t * (64-bit pointer)
2547 uint64_t superclass; // class64_t * (64-bit pointer)
2548 uint64_t cache; // Cache (64-bit pointer)
2549 uint64_t vtable; // IMP * (64-bit pointer)
2550 uint64_t data; // class_ro64_t * (64-bit pointer)
2554 uint32_t isa; /* class32_t * (32-bit pointer) */
2555 uint32_t superclass; /* class32_t * (32-bit pointer) */
2556 uint32_t cache; /* Cache (32-bit pointer) */
2557 uint32_t vtable; /* IMP * (32-bit pointer) */
2558 uint32_t data; /* class_ro32_t * (32-bit pointer) */
2561 struct class_ro64_t {
2563 uint32_t instanceStart;
2564 uint32_t instanceSize;
2566 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
2567 uint64_t name; // const char * (64-bit pointer)
2568 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
2569 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
2570 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
2571 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
2572 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
2575 struct class_ro32_t {
2577 uint32_t instanceStart;
2578 uint32_t instanceSize;
2579 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
2580 uint32_t name; /* const char * (32-bit pointer) */
2581 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
2582 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
2583 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
2584 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
2585 uint32_t baseProperties; /* const struct objc_property_list *
2589 /* Values for class_ro{64,32}_t->flags */
2590 #define RO_META (1 << 0)
2591 #define RO_ROOT (1 << 1)
2592 #define RO_HAS_CXX_STRUCTORS (1 << 2)
2594 struct method_list64_t {
2597 /* struct method64_t first; These structures follow inline */
2600 struct method_list32_t {
2603 /* struct method32_t first; These structures follow inline */
2607 uint64_t name; /* SEL (64-bit pointer) */
2608 uint64_t types; /* const char * (64-bit pointer) */
2609 uint64_t imp; /* IMP (64-bit pointer) */
2613 uint32_t name; /* SEL (32-bit pointer) */
2614 uint32_t types; /* const char * (32-bit pointer) */
2615 uint32_t imp; /* IMP (32-bit pointer) */
2618 struct protocol_list64_t {
2619 uint64_t count; /* uintptr_t (a 64-bit value) */
2620 /* struct protocol64_t * list[0]; These pointers follow inline */
2623 struct protocol_list32_t {
2624 uint32_t count; /* uintptr_t (a 32-bit value) */
2625 /* struct protocol32_t * list[0]; These pointers follow inline */
2628 struct protocol64_t {
2629 uint64_t isa; /* id * (64-bit pointer) */
2630 uint64_t name; /* const char * (64-bit pointer) */
2631 uint64_t protocols; /* struct protocol_list64_t *
2633 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
2634 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
2635 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
2636 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
2637 uint64_t instanceProperties; /* struct objc_property_list *
2641 struct protocol32_t {
2642 uint32_t isa; /* id * (32-bit pointer) */
2643 uint32_t name; /* const char * (32-bit pointer) */
2644 uint32_t protocols; /* struct protocol_list_t *
2646 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
2647 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
2648 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
2649 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
2650 uint32_t instanceProperties; /* struct objc_property_list *
2654 struct ivar_list64_t {
2657 /* struct ivar64_t first; These structures follow inline */
2660 struct ivar_list32_t {
2663 /* struct ivar32_t first; These structures follow inline */
2667 uint64_t offset; /* uintptr_t * (64-bit pointer) */
2668 uint64_t name; /* const char * (64-bit pointer) */
2669 uint64_t type; /* const char * (64-bit pointer) */
2675 uint32_t offset; /* uintptr_t * (32-bit pointer) */
2676 uint32_t name; /* const char * (32-bit pointer) */
2677 uint32_t type; /* const char * (32-bit pointer) */
2682 struct objc_property_list64 {
2685 /* struct objc_property64 first; These structures follow inline */
2688 struct objc_property_list32 {
2691 /* struct objc_property32 first; These structures follow inline */
2694 struct objc_property64 {
2695 uint64_t name; /* const char * (64-bit pointer) */
2696 uint64_t attributes; /* const char * (64-bit pointer) */
2699 struct objc_property32 {
2700 uint32_t name; /* const char * (32-bit pointer) */
2701 uint32_t attributes; /* const char * (32-bit pointer) */
2704 struct category64_t {
2705 uint64_t name; /* const char * (64-bit pointer) */
2706 uint64_t cls; /* struct class_t * (64-bit pointer) */
2707 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
2708 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
2709 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
2710 uint64_t instanceProperties; /* struct objc_property_list *
2714 struct category32_t {
2715 uint32_t name; /* const char * (32-bit pointer) */
2716 uint32_t cls; /* struct class_t * (32-bit pointer) */
2717 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
2718 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
2719 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
2720 uint32_t instanceProperties; /* struct objc_property_list *
2724 struct objc_image_info64 {
2728 struct objc_image_info32 {
2732 struct imageInfo_t {
2736 /* masks for objc_image_info.flags */
2737 #define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
2738 #define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
2740 struct message_ref64 {
2741 uint64_t imp; /* IMP (64-bit pointer) */
2742 uint64_t sel; /* SEL (64-bit pointer) */
2745 struct message_ref32 {
2746 uint32_t imp; /* IMP (32-bit pointer) */
2747 uint32_t sel; /* SEL (32-bit pointer) */
2750 // Objective-C 1 (32-bit only) meta data structs.
2752 struct objc_module_t {
2755 uint32_t name; /* char * (32-bit pointer) */
2756 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
2759 struct objc_symtab_t {
2760 uint32_t sel_ref_cnt;
2761 uint32_t refs; /* SEL * (32-bit pointer) */
2762 uint16_t cls_def_cnt;
2763 uint16_t cat_def_cnt;
2764 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
2767 struct objc_class_t {
2768 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2769 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
2770 uint32_t name; /* const char * (32-bit pointer) */
2773 int32_t instance_size;
2774 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
2775 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
2776 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
2777 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
2780 #define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
2781 // class is not a metaclass
2782 #define CLS_CLASS 0x1
2783 // class is a metaclass
2784 #define CLS_META 0x2
2786 struct objc_category_t {
2787 uint32_t category_name; /* char * (32-bit pointer) */
2788 uint32_t class_name; /* char * (32-bit pointer) */
2789 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
2790 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
2791 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
2794 struct objc_ivar_t {
2795 uint32_t ivar_name; /* char * (32-bit pointer) */
2796 uint32_t ivar_type; /* char * (32-bit pointer) */
2797 int32_t ivar_offset;
2800 struct objc_ivar_list_t {
2802 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
2805 struct objc_method_list_t {
2806 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
2807 int32_t method_count;
2808 // struct objc_method_t method_list[1]; /* variable length structure */
2811 struct objc_method_t {
2812 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2813 uint32_t method_types; /* char * (32-bit pointer) */
2814 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
2818 struct objc_protocol_list_t {
2819 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
2821 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
2822 // (32-bit pointer) */
2825 struct objc_protocol_t {
2826 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2827 uint32_t protocol_name; /* char * (32-bit pointer) */
2828 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
2829 uint32_t instance_methods; /* struct objc_method_description_list *
2831 uint32_t class_methods; /* struct objc_method_description_list *
2835 struct objc_method_description_list_t {
2837 // struct objc_method_description_t list[1];
2840 struct objc_method_description_t {
2841 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2842 uint32_t types; /* char * (32-bit pointer) */
2845 inline void swapStruct(struct cfstring64_t &cfs) {
2846 sys::swapByteOrder(cfs.isa);
2847 sys::swapByteOrder(cfs.flags);
2848 sys::swapByteOrder(cfs.characters);
2849 sys::swapByteOrder(cfs.length);
2852 inline void swapStruct(struct class64_t &c) {
2853 sys::swapByteOrder(c.isa);
2854 sys::swapByteOrder(c.superclass);
2855 sys::swapByteOrder(c.cache);
2856 sys::swapByteOrder(c.vtable);
2857 sys::swapByteOrder(c.data);
2860 inline void swapStruct(struct class32_t &c) {
2861 sys::swapByteOrder(c.isa);
2862 sys::swapByteOrder(c.superclass);
2863 sys::swapByteOrder(c.cache);
2864 sys::swapByteOrder(c.vtable);
2865 sys::swapByteOrder(c.data);
2868 inline void swapStruct(struct class_ro64_t &cro) {
2869 sys::swapByteOrder(cro.flags);
2870 sys::swapByteOrder(cro.instanceStart);
2871 sys::swapByteOrder(cro.instanceSize);
2872 sys::swapByteOrder(cro.reserved);
2873 sys::swapByteOrder(cro.ivarLayout);
2874 sys::swapByteOrder(cro.name);
2875 sys::swapByteOrder(cro.baseMethods);
2876 sys::swapByteOrder(cro.baseProtocols);
2877 sys::swapByteOrder(cro.ivars);
2878 sys::swapByteOrder(cro.weakIvarLayout);
2879 sys::swapByteOrder(cro.baseProperties);
2882 inline void swapStruct(struct class_ro32_t &cro) {
2883 sys::swapByteOrder(cro.flags);
2884 sys::swapByteOrder(cro.instanceStart);
2885 sys::swapByteOrder(cro.instanceSize);
2886 sys::swapByteOrder(cro.ivarLayout);
2887 sys::swapByteOrder(cro.name);
2888 sys::swapByteOrder(cro.baseMethods);
2889 sys::swapByteOrder(cro.baseProtocols);
2890 sys::swapByteOrder(cro.ivars);
2891 sys::swapByteOrder(cro.weakIvarLayout);
2892 sys::swapByteOrder(cro.baseProperties);
2895 inline void swapStruct(struct method_list64_t &ml) {
2896 sys::swapByteOrder(ml.entsize);
2897 sys::swapByteOrder(ml.count);
2900 inline void swapStruct(struct method_list32_t &ml) {
2901 sys::swapByteOrder(ml.entsize);
2902 sys::swapByteOrder(ml.count);
2905 inline void swapStruct(struct method64_t &m) {
2906 sys::swapByteOrder(m.name);
2907 sys::swapByteOrder(m.types);
2908 sys::swapByteOrder(m.imp);
2911 inline void swapStruct(struct method32_t &m) {
2912 sys::swapByteOrder(m.name);
2913 sys::swapByteOrder(m.types);
2914 sys::swapByteOrder(m.imp);
2917 inline void swapStruct(struct protocol_list64_t &pl) {
2918 sys::swapByteOrder(pl.count);
2921 inline void swapStruct(struct protocol_list32_t &pl) {
2922 sys::swapByteOrder(pl.count);
2925 inline void swapStruct(struct protocol64_t &p) {
2926 sys::swapByteOrder(p.isa);
2927 sys::swapByteOrder(p.name);
2928 sys::swapByteOrder(p.protocols);
2929 sys::swapByteOrder(p.instanceMethods);
2930 sys::swapByteOrder(p.classMethods);
2931 sys::swapByteOrder(p.optionalInstanceMethods);
2932 sys::swapByteOrder(p.optionalClassMethods);
2933 sys::swapByteOrder(p.instanceProperties);
2936 inline void swapStruct(struct protocol32_t &p) {
2937 sys::swapByteOrder(p.isa);
2938 sys::swapByteOrder(p.name);
2939 sys::swapByteOrder(p.protocols);
2940 sys::swapByteOrder(p.instanceMethods);
2941 sys::swapByteOrder(p.classMethods);
2942 sys::swapByteOrder(p.optionalInstanceMethods);
2943 sys::swapByteOrder(p.optionalClassMethods);
2944 sys::swapByteOrder(p.instanceProperties);
2947 inline void swapStruct(struct ivar_list64_t &il) {
2948 sys::swapByteOrder(il.entsize);
2949 sys::swapByteOrder(il.count);
2952 inline void swapStruct(struct ivar_list32_t &il) {
2953 sys::swapByteOrder(il.entsize);
2954 sys::swapByteOrder(il.count);
2957 inline void swapStruct(struct ivar64_t &i) {
2958 sys::swapByteOrder(i.offset);
2959 sys::swapByteOrder(i.name);
2960 sys::swapByteOrder(i.type);
2961 sys::swapByteOrder(i.alignment);
2962 sys::swapByteOrder(i.size);
2965 inline void swapStruct(struct ivar32_t &i) {
2966 sys::swapByteOrder(i.offset);
2967 sys::swapByteOrder(i.name);
2968 sys::swapByteOrder(i.type);
2969 sys::swapByteOrder(i.alignment);
2970 sys::swapByteOrder(i.size);
2973 inline void swapStruct(struct objc_property_list64 &pl) {
2974 sys::swapByteOrder(pl.entsize);
2975 sys::swapByteOrder(pl.count);
2978 inline void swapStruct(struct objc_property_list32 &pl) {
2979 sys::swapByteOrder(pl.entsize);
2980 sys::swapByteOrder(pl.count);
2983 inline void swapStruct(struct objc_property64 &op) {
2984 sys::swapByteOrder(op.name);
2985 sys::swapByteOrder(op.attributes);
2988 inline void swapStruct(struct objc_property32 &op) {
2989 sys::swapByteOrder(op.name);
2990 sys::swapByteOrder(op.attributes);
2993 inline void swapStruct(struct category64_t &c) {
2994 sys::swapByteOrder(c.name);
2995 sys::swapByteOrder(c.cls);
2996 sys::swapByteOrder(c.instanceMethods);
2997 sys::swapByteOrder(c.classMethods);
2998 sys::swapByteOrder(c.protocols);
2999 sys::swapByteOrder(c.instanceProperties);
3002 inline void swapStruct(struct category32_t &c) {
3003 sys::swapByteOrder(c.name);
3004 sys::swapByteOrder(c.cls);
3005 sys::swapByteOrder(c.instanceMethods);
3006 sys::swapByteOrder(c.classMethods);
3007 sys::swapByteOrder(c.protocols);
3008 sys::swapByteOrder(c.instanceProperties);
3011 inline void swapStruct(struct objc_image_info64 &o) {
3012 sys::swapByteOrder(o.version);
3013 sys::swapByteOrder(o.flags);
3016 inline void swapStruct(struct objc_image_info32 &o) {
3017 sys::swapByteOrder(o.version);
3018 sys::swapByteOrder(o.flags);
3021 inline void swapStruct(struct imageInfo_t &o) {
3022 sys::swapByteOrder(o.version);
3023 sys::swapByteOrder(o.flags);
3026 inline void swapStruct(struct message_ref64 &mr) {
3027 sys::swapByteOrder(mr.imp);
3028 sys::swapByteOrder(mr.sel);
3031 inline void swapStruct(struct message_ref32 &mr) {
3032 sys::swapByteOrder(mr.imp);
3033 sys::swapByteOrder(mr.sel);
3036 inline void swapStruct(struct objc_module_t &module) {
3037 sys::swapByteOrder(module.version);
3038 sys::swapByteOrder(module.size);
3039 sys::swapByteOrder(module.name);
3040 sys::swapByteOrder(module.symtab);
3043 inline void swapStruct(struct objc_symtab_t &symtab) {
3044 sys::swapByteOrder(symtab.sel_ref_cnt);
3045 sys::swapByteOrder(symtab.refs);
3046 sys::swapByteOrder(symtab.cls_def_cnt);
3047 sys::swapByteOrder(symtab.cat_def_cnt);
3050 inline void swapStruct(struct objc_class_t &objc_class) {
3051 sys::swapByteOrder(objc_class.isa);
3052 sys::swapByteOrder(objc_class.super_class);
3053 sys::swapByteOrder(objc_class.name);
3054 sys::swapByteOrder(objc_class.version);
3055 sys::swapByteOrder(objc_class.info);
3056 sys::swapByteOrder(objc_class.instance_size);
3057 sys::swapByteOrder(objc_class.ivars);
3058 sys::swapByteOrder(objc_class.methodLists);
3059 sys::swapByteOrder(objc_class.cache);
3060 sys::swapByteOrder(objc_class.protocols);
3063 inline void swapStruct(struct objc_category_t &objc_category) {
3064 sys::swapByteOrder(objc_category.category_name);
3065 sys::swapByteOrder(objc_category.class_name);
3066 sys::swapByteOrder(objc_category.instance_methods);
3067 sys::swapByteOrder(objc_category.class_methods);
3068 sys::swapByteOrder(objc_category.protocols);
3071 inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3072 sys::swapByteOrder(objc_ivar_list.ivar_count);
3075 inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3076 sys::swapByteOrder(objc_ivar.ivar_name);
3077 sys::swapByteOrder(objc_ivar.ivar_type);
3078 sys::swapByteOrder(objc_ivar.ivar_offset);
3081 inline void swapStruct(struct objc_method_list_t &method_list) {
3082 sys::swapByteOrder(method_list.obsolete);
3083 sys::swapByteOrder(method_list.method_count);
3086 inline void swapStruct(struct objc_method_t &method) {
3087 sys::swapByteOrder(method.method_name);
3088 sys::swapByteOrder(method.method_types);
3089 sys::swapByteOrder(method.method_imp);
3092 inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3093 sys::swapByteOrder(protocol_list.next);
3094 sys::swapByteOrder(protocol_list.count);
3097 inline void swapStruct(struct objc_protocol_t &protocol) {
3098 sys::swapByteOrder(protocol.isa);
3099 sys::swapByteOrder(protocol.protocol_name);
3100 sys::swapByteOrder(protocol.protocol_list);
3101 sys::swapByteOrder(protocol.instance_methods);
3102 sys::swapByteOrder(protocol.class_methods);
3105 inline void swapStruct(struct objc_method_description_list_t &mdl) {
3106 sys::swapByteOrder(mdl.count);
3109 inline void swapStruct(struct objc_method_description_t &md) {
3110 sys::swapByteOrder(md.name);
3111 sys::swapByteOrder(md.types);
3114 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3115 struct DisassembleInfo *info);
3117 // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3118 // to an Objective-C class and returns the class name. It is also passed the
3119 // address of the pointer, so when the pointer is zero as it can be in an .o
3120 // file, that is used to look for an external relocation entry with a symbol
3122 static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3123 uint64_t ReferenceValue,
3124 struct DisassembleInfo *info) {
3126 uint32_t offset, left;
3129 // The pointer_value can be 0 in an object file and have a relocation
3130 // entry for the class symbol at the ReferenceValue (the address of the
3132 if (pointer_value == 0) {
3133 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3134 if (r == nullptr || left < sizeof(uint64_t))
3137 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3138 if (symbol_name == nullptr)
3140 const char *class_name = strrchr(symbol_name, '$');
3141 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3142 return class_name + 2;
3147 // The case were the pointer_value is non-zero and points to a class defined
3148 // in this Mach-O file.
3149 r = get_pointer_64(pointer_value, offset, left, S, info);
3150 if (r == nullptr || left < sizeof(struct class64_t))
3153 memcpy(&c, r, sizeof(struct class64_t));
3154 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3158 r = get_pointer_64(c.data, offset, left, S, info);
3159 if (r == nullptr || left < sizeof(struct class_ro64_t))
3161 struct class_ro64_t cro;
3162 memcpy(&cro, r, sizeof(struct class_ro64_t));
3163 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3167 const char *name = get_pointer_64(cro.name, offset, left, S, info);
3171 // get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3172 // pointer to a cfstring and returns its name or nullptr.
3173 static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3174 struct DisassembleInfo *info) {
3175 const char *r, *name;
3176 uint32_t offset, left;
3178 struct cfstring64_t cfs;
3179 uint64_t cfs_characters;
3181 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3182 if (r == nullptr || left < sizeof(struct cfstring64_t))
3184 memcpy(&cfs, r, sizeof(struct cfstring64_t));
3185 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3187 if (cfs.characters == 0) {
3189 const char *symbol_name = get_symbol_64(
3190 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
3191 if (symbol_name == nullptr)
3193 cfs_characters = n_value;
3195 cfs_characters = cfs.characters;
3196 name = get_pointer_64(cfs_characters, offset, left, S, info);
3201 // get_objc2_64bit_selref() is used for disassembly and is passed a the address
3202 // of a pointer to an Objective-C selector reference when the pointer value is
3203 // zero as in a .o file and is likely to have a external relocation entry with
3204 // who's symbol's n_value is the real pointer to the selector name. If that is
3205 // the case the real pointer to the selector name is returned else 0 is
3207 static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3208 struct DisassembleInfo *info) {
3209 uint32_t offset, left;
3212 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3213 if (r == nullptr || left < sizeof(uint64_t))
3216 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3217 if (symbol_name == nullptr)
3222 static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3223 const char *sectname) {
3224 for (const SectionRef &Section : O->sections()) {
3226 Section.getName(SectName);
3227 DataRefImpl Ref = Section.getRawDataRefImpl();
3228 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3229 if (SegName == segname && SectName == sectname)
3232 return SectionRef();
3236 walk_pointer_list_64(const char *listname, const SectionRef S,
3237 MachOObjectFile *O, struct DisassembleInfo *info,
3238 void (*func)(uint64_t, struct DisassembleInfo *info)) {
3239 if (S == SectionRef())
3243 S.getName(SectName);
3244 DataRefImpl Ref = S.getRawDataRefImpl();
3245 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3246 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3249 S.getContents(BytesStr);
3250 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3252 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3253 uint32_t left = S.getSize() - i;
3254 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3256 memcpy(&p, Contents + i, size);
3257 if (i + sizeof(uint64_t) > S.getSize())
3258 outs() << listname << " list pointer extends past end of (" << SegName
3259 << "," << SectName << ") section\n";
3260 outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
3262 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3263 sys::swapByteOrder(p);
3265 uint64_t n_value = 0;
3266 const char *name = get_symbol_64(i, S, info, n_value, p);
3267 if (name == nullptr)
3268 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
3271 outs() << format("0x%" PRIx64, n_value);
3273 outs() << " + " << format("0x%" PRIx64, p);
3275 outs() << format("0x%" PRIx64, p);
3276 if (name != nullptr)
3277 outs() << " " << name;
3287 walk_pointer_list_32(const char *listname, const SectionRef S,
3288 MachOObjectFile *O, struct DisassembleInfo *info,
3289 void (*func)(uint32_t, struct DisassembleInfo *info)) {
3290 if (S == SectionRef())
3294 S.getName(SectName);
3295 DataRefImpl Ref = S.getRawDataRefImpl();
3296 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3297 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3300 S.getContents(BytesStr);
3301 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3303 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
3304 uint32_t left = S.getSize() - i;
3305 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
3307 memcpy(&p, Contents + i, size);
3308 if (i + sizeof(uint32_t) > S.getSize())
3309 outs() << listname << " list pointer extends past end of (" << SegName
3310 << "," << SectName << ") section\n";
3311 uint32_t Address = S.getAddress() + i;
3312 outs() << format("%08" PRIx32, Address) << " ";
3314 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3315 sys::swapByteOrder(p);
3316 outs() << format("0x%" PRIx32, p);
3318 const char *name = get_symbol_32(i, S, info, p);
3319 if (name != nullptr)
3320 outs() << " " << name;
3328 static void print_layout_map(const char *layout_map, uint32_t left) {
3329 outs() << " layout map: ";
3331 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
3334 } while (*layout_map != '\0' && left != 0);
3338 static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
3339 uint32_t offset, left;
3341 const char *layout_map;
3345 layout_map = get_pointer_64(p, offset, left, S, info);
3346 print_layout_map(layout_map, left);
3349 static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
3350 uint32_t offset, left;
3352 const char *layout_map;
3356 layout_map = get_pointer_32(p, offset, left, S, info);
3357 print_layout_map(layout_map, left);
3360 static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
3361 const char *indent) {
3362 struct method_list64_t ml;
3363 struct method64_t m;
3365 uint32_t offset, xoffset, left, i;
3367 const char *name, *sym_name;
3370 r = get_pointer_64(p, offset, left, S, info);
3373 memset(&ml, '\0', sizeof(struct method_list64_t));
3374 if (left < sizeof(struct method_list64_t)) {
3375 memcpy(&ml, r, left);
3376 outs() << " (method_list_t entends past the end of the section)\n";
3378 memcpy(&ml, r, sizeof(struct method_list64_t));
3379 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3381 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3382 outs() << indent << "\t\t count " << ml.count << "\n";
3384 p += sizeof(struct method_list64_t);
3385 offset += sizeof(struct method_list64_t);
3386 for (i = 0; i < ml.count; i++) {
3387 r = get_pointer_64(p, offset, left, S, info);
3390 memset(&m, '\0', sizeof(struct method64_t));
3391 if (left < sizeof(struct method64_t)) {
3392 memcpy(&ml, r, left);
3393 outs() << indent << " (method_t entends past the end of the section)\n";
3395 memcpy(&m, r, sizeof(struct method64_t));
3396 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3399 outs() << indent << "\t\t name ";
3400 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
3401 info, n_value, m.name);
3403 if (info->verbose && sym_name != nullptr)
3406 outs() << format("0x%" PRIx64, n_value);
3408 outs() << " + " << format("0x%" PRIx64, m.name);
3410 outs() << format("0x%" PRIx64, m.name);
3411 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
3412 if (name != nullptr)
3413 outs() << format(" %.*s", left, name);
3416 outs() << indent << "\t\t types ";
3417 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
3418 info, n_value, m.types);
3420 if (info->verbose && sym_name != nullptr)
3423 outs() << format("0x%" PRIx64, n_value);
3425 outs() << " + " << format("0x%" PRIx64, m.types);
3427 outs() << format("0x%" PRIx64, m.types);
3428 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
3429 if (name != nullptr)
3430 outs() << format(" %.*s", left, name);
3433 outs() << indent << "\t\t imp ";
3434 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
3436 if (info->verbose && name == nullptr) {
3438 outs() << format("0x%" PRIx64, n_value) << " ";
3440 outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
3442 outs() << format("0x%" PRIx64, m.imp) << " ";
3444 if (name != nullptr)
3448 p += sizeof(struct method64_t);
3449 offset += sizeof(struct method64_t);
3453 static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
3454 const char *indent) {
3455 struct method_list32_t ml;
3456 struct method32_t m;
3457 const char *r, *name;
3458 uint32_t offset, xoffset, left, i;
3461 r = get_pointer_32(p, offset, left, S, info);
3464 memset(&ml, '\0', sizeof(struct method_list32_t));
3465 if (left < sizeof(struct method_list32_t)) {
3466 memcpy(&ml, r, left);
3467 outs() << " (method_list_t entends past the end of the section)\n";
3469 memcpy(&ml, r, sizeof(struct method_list32_t));
3470 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3472 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3473 outs() << indent << "\t\t count " << ml.count << "\n";
3475 p += sizeof(struct method_list32_t);
3476 offset += sizeof(struct method_list32_t);
3477 for (i = 0; i < ml.count; i++) {
3478 r = get_pointer_32(p, offset, left, S, info);
3481 memset(&m, '\0', sizeof(struct method32_t));
3482 if (left < sizeof(struct method32_t)) {
3483 memcpy(&ml, r, left);
3484 outs() << indent << " (method_t entends past the end of the section)\n";
3486 memcpy(&m, r, sizeof(struct method32_t));
3487 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3490 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
3491 name = get_pointer_32(m.name, xoffset, left, xS, info);
3492 if (name != nullptr)
3493 outs() << format(" %.*s", left, name);
3496 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
3497 name = get_pointer_32(m.types, xoffset, left, xS, info);
3498 if (name != nullptr)
3499 outs() << format(" %.*s", left, name);
3502 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
3503 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
3505 if (name != nullptr)
3506 outs() << " " << name;
3509 p += sizeof(struct method32_t);
3510 offset += sizeof(struct method32_t);
3514 static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
3515 uint32_t offset, left, xleft;
3517 struct objc_method_list_t method_list;
3518 struct objc_method_t method;
3519 const char *r, *methods, *name, *SymbolName;
3522 r = get_pointer_32(p, offset, left, S, info, true);
3527 if (left > sizeof(struct objc_method_list_t)) {
3528 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
3530 outs() << "\t\t objc_method_list extends past end of the section\n";
3531 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
3532 memcpy(&method_list, r, left);
3534 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3535 swapStruct(method_list);
3537 outs() << "\t\t obsolete "
3538 << format("0x%08" PRIx32, method_list.obsolete) << "\n";
3539 outs() << "\t\t method_count " << method_list.method_count << "\n";
3541 methods = r + sizeof(struct objc_method_list_t);
3542 for (i = 0; i < method_list.method_count; i++) {
3543 if ((i + 1) * sizeof(struct objc_method_t) > left) {
3544 outs() << "\t\t remaining method's extend past the of the section\n";
3547 memcpy(&method, methods + i * sizeof(struct objc_method_t),
3548 sizeof(struct objc_method_t));
3549 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3552 outs() << "\t\t method_name "
3553 << format("0x%08" PRIx32, method.method_name);
3554 if (info->verbose) {
3555 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
3556 if (name != nullptr)
3557 outs() << format(" %.*s", xleft, name);
3559 outs() << " (not in an __OBJC section)";
3563 outs() << "\t\t method_types "
3564 << format("0x%08" PRIx32, method.method_types);
3565 if (info->verbose) {
3566 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
3567 if (name != nullptr)
3568 outs() << format(" %.*s", xleft, name);
3570 outs() << " (not in an __OBJC section)";
3574 outs() << "\t\t method_imp "
3575 << format("0x%08" PRIx32, method.method_imp) << " ";
3576 if (info->verbose) {
3577 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
3578 if (SymbolName != nullptr)
3579 outs() << SymbolName;
3586 static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
3587 struct protocol_list64_t pl;
3588 uint64_t q, n_value;
3589 struct protocol64_t pc;
3591 uint32_t offset, xoffset, left, i;
3593 const char *name, *sym_name;
3595 r = get_pointer_64(p, offset, left, S, info);
3598 memset(&pl, '\0', sizeof(struct protocol_list64_t));
3599 if (left < sizeof(struct protocol_list64_t)) {
3600 memcpy(&pl, r, left);
3601 outs() << " (protocol_list_t entends past the end of the section)\n";
3603 memcpy(&pl, r, sizeof(struct protocol_list64_t));
3604 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3606 outs() << " count " << pl.count << "\n";
3608 p += sizeof(struct protocol_list64_t);
3609 offset += sizeof(struct protocol_list64_t);
3610 for (i = 0; i < pl.count; i++) {
3611 r = get_pointer_64(p, offset, left, S, info);
3615 if (left < sizeof(uint64_t)) {
3616 memcpy(&q, r, left);
3617 outs() << " (protocol_t * entends past the end of the section)\n";
3619 memcpy(&q, r, sizeof(uint64_t));
3620 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3621 sys::swapByteOrder(q);
3623 outs() << "\t\t list[" << i << "] ";
3624 sym_name = get_symbol_64(offset, S, info, n_value, q);
3626 if (info->verbose && sym_name != nullptr)
3629 outs() << format("0x%" PRIx64, n_value);
3631 outs() << " + " << format("0x%" PRIx64, q);
3633 outs() << format("0x%" PRIx64, q);
3634 outs() << " (struct protocol_t *)\n";
3636 r = get_pointer_64(q + n_value, offset, left, S, info);
3639 memset(&pc, '\0', sizeof(struct protocol64_t));
3640 if (left < sizeof(struct protocol64_t)) {
3641 memcpy(&pc, r, left);
3642 outs() << " (protocol_t entends past the end of the section)\n";
3644 memcpy(&pc, r, sizeof(struct protocol64_t));
3645 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3648 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
3650 outs() << "\t\t\t name ";
3651 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
3652 info, n_value, pc.name);
3654 if (info->verbose && sym_name != nullptr)
3657 outs() << format("0x%" PRIx64, n_value);
3659 outs() << " + " << format("0x%" PRIx64, pc.name);
3661 outs() << format("0x%" PRIx64, pc.name);
3662 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
3663 if (name != nullptr)
3664 outs() << format(" %.*s", left, name);
3667 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
3669 outs() << "\t\t instanceMethods ";
3671 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
3672 S, info, n_value, pc.instanceMethods);
3674 if (info->verbose && sym_name != nullptr)
3677 outs() << format("0x%" PRIx64, n_value);
3678 if (pc.instanceMethods != 0)
3679 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
3681 outs() << format("0x%" PRIx64, pc.instanceMethods);
3682 outs() << " (struct method_list_t *)\n";
3683 if (pc.instanceMethods + n_value != 0)
3684 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
3686 outs() << "\t\t classMethods ";
3688 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
3689 info, n_value, pc.classMethods);
3691 if (info->verbose && sym_name != nullptr)
3694 outs() << format("0x%" PRIx64, n_value);
3695 if (pc.classMethods != 0)
3696 outs() << " + " << format("0x%" PRIx64, pc.classMethods);
3698 outs() << format("0x%" PRIx64, pc.classMethods);
3699 outs() << " (struct method_list_t *)\n";
3700 if (pc.classMethods + n_value != 0)
3701 print_method_list64_t(pc.classMethods + n_value, info, "\t");
3703 outs() << "\t optionalInstanceMethods "
3704 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
3705 outs() << "\t optionalClassMethods "
3706 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
3707 outs() << "\t instanceProperties "
3708 << format("0x%" PRIx64, pc.instanceProperties) << "\n";
3710 p += sizeof(uint64_t);
3711 offset += sizeof(uint64_t);
3715 static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
3716 struct protocol_list32_t pl;
3718 struct protocol32_t pc;
3720 uint32_t offset, xoffset, left, i;
3724 r = get_pointer_32(p, offset, left, S, info);
3727 memset(&pl, '\0', sizeof(struct protocol_list32_t));
3728 if (left < sizeof(struct protocol_list32_t)) {
3729 memcpy(&pl, r, left);
3730 outs() << " (protocol_list_t entends past the end of the section)\n";
3732 memcpy(&pl, r, sizeof(struct protocol_list32_t));
3733 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3735 outs() << " count " << pl.count << "\n";
3737 p += sizeof(struct protocol_list32_t);
3738 offset += sizeof(struct protocol_list32_t);
3739 for (i = 0; i < pl.count; i++) {
3740 r = get_pointer_32(p, offset, left, S, info);
3744 if (left < sizeof(uint32_t)) {
3745 memcpy(&q, r, left);
3746 outs() << " (protocol_t * entends past the end of the section)\n";
3748 memcpy(&q, r, sizeof(uint32_t));
3749 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3750 sys::swapByteOrder(q);
3751 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
3752 << " (struct protocol_t *)\n";
3753 r = get_pointer_32(q, offset, left, S, info);
3756 memset(&pc, '\0', sizeof(struct protocol32_t));
3757 if (left < sizeof(struct protocol32_t)) {
3758 memcpy(&pc, r, left);
3759 outs() << " (protocol_t entends past the end of the section)\n";
3761 memcpy(&pc, r, sizeof(struct protocol32_t));
3762 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3764 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
3765 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
3766 name = get_pointer_32(pc.name, xoffset, left, xS, info);
3767 if (name != nullptr)
3768 outs() << format(" %.*s", left, name);
3770 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
3771 outs() << "\t\t instanceMethods "
3772 << format("0x%" PRIx32, pc.instanceMethods)
3773 << " (struct method_list_t *)\n";
3774 if (pc.instanceMethods != 0)
3775 print_method_list32_t(pc.instanceMethods, info, "\t");
3776 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
3777 << " (struct method_list_t *)\n";
3778 if (pc.classMethods != 0)
3779 print_method_list32_t(pc.classMethods, info, "\t");
3780 outs() << "\t optionalInstanceMethods "
3781 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
3782 outs() << "\t optionalClassMethods "
3783 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
3784 outs() << "\t instanceProperties "
3785 << format("0x%" PRIx32, pc.instanceProperties) << "\n";
3786 p += sizeof(uint32_t);
3787 offset += sizeof(uint32_t);
3791 static void print_indent(uint32_t indent) {
3792 for (uint32_t i = 0; i < indent;) {
3793 if (indent - i >= 8) {
3797 for (uint32_t j = i; j < indent; j++)
3804 static bool print_method_description_list(uint32_t p, uint32_t indent,
3805 struct DisassembleInfo *info) {
3806 uint32_t offset, left, xleft;
3808 struct objc_method_description_list_t mdl;
3809 struct objc_method_description_t md;
3810 const char *r, *list, *name;
3813 r = get_pointer_32(p, offset, left, S, info, true);
3818 if (left > sizeof(struct objc_method_description_list_t)) {
3819 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
3821 print_indent(indent);
3822 outs() << " objc_method_description_list extends past end of the section\n";
3823 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
3824 memcpy(&mdl, r, left);
3826 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3829 print_indent(indent);
3830 outs() << " count " << mdl.count << "\n";
3832 list = r + sizeof(struct objc_method_description_list_t);
3833 for (i = 0; i < mdl.count; i++) {
3834 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
3835 print_indent(indent);
3836 outs() << " remaining list entries extend past the of the section\n";
3839 print_indent(indent);
3840 outs() << " list[" << i << "]\n";
3841 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
3842 sizeof(struct objc_method_description_t));
3843 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3846 print_indent(indent);
3847 outs() << " name " << format("0x%08" PRIx32, md.name);
3848 if (info->verbose) {
3849 name = get_pointer_32(md.name, offset, xleft, S, info, true);
3850 if (name != nullptr)
3851 outs() << format(" %.*s", xleft, name);
3853 outs() << " (not in an __OBJC section)";
3857 print_indent(indent);
3858 outs() << " types " << format("0x%08" PRIx32, md.types);
3859 if (info->verbose) {
3860 name = get_pointer_32(md.types, offset, xleft, S, info, true);
3861 if (name != nullptr)
3862 outs() << format(" %.*s", xleft, name);
3864 outs() << " (not in an __OBJC section)";
3871 static bool print_protocol_list(uint32_t p, uint32_t indent,
3872 struct DisassembleInfo *info);
3874 static bool print_protocol(uint32_t p, uint32_t indent,
3875 struct DisassembleInfo *info) {
3876 uint32_t offset, left;
3878 struct objc_protocol_t protocol;
3879 const char *r, *name;
3881 r = get_pointer_32(p, offset, left, S, info, true);
3886 if (left >= sizeof(struct objc_protocol_t)) {
3887 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
3889 print_indent(indent);
3890 outs() << " Protocol extends past end of the section\n";
3891 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
3892 memcpy(&protocol, r, left);
3894 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3895 swapStruct(protocol);
3897 print_indent(indent);
3898 outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
3901 print_indent(indent);
3902 outs() << " protocol_name "
3903 << format("0x%08" PRIx32, protocol.protocol_name);
3904 if (info->verbose) {
3905 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
3906 if (name != nullptr)
3907 outs() << format(" %.*s", left, name);
3909 outs() << " (not in an __OBJC section)";
3913 print_indent(indent);
3914 outs() << " protocol_list "
3915 << format("0x%08" PRIx32, protocol.protocol_list);
3916 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
3917 outs() << " (not in an __OBJC section)\n";
3919 print_indent(indent);
3920 outs() << " instance_methods "
3921 << format("0x%08" PRIx32, protocol.instance_methods);
3922 if (print_method_description_list(protocol.instance_methods, indent, info))
3923 outs() << " (not in an __OBJC section)\n";
3925 print_indent(indent);
3926 outs() << " class_methods "
3927 << format("0x%08" PRIx32, protocol.class_methods);
3928 if (print_method_description_list(protocol.class_methods, indent, info))
3929 outs() << " (not in an __OBJC section)\n";
3934 static bool print_protocol_list(uint32_t p, uint32_t indent,
3935 struct DisassembleInfo *info) {
3936 uint32_t offset, left, l;
3938 struct objc_protocol_list_t protocol_list;
3939 const char *r, *list;
3942 r = get_pointer_32(p, offset, left, S, info, true);
3947 if (left > sizeof(struct objc_protocol_list_t)) {
3948 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
3950 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
3951 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
3952 memcpy(&protocol_list, r, left);
3954 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3955 swapStruct(protocol_list);
3957 print_indent(indent);
3958 outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
3960 print_indent(indent);
3961 outs() << " count " << protocol_list.count << "\n";
3963 list = r + sizeof(struct objc_protocol_list_t);
3964 for (i = 0; i < protocol_list.count; i++) {
3965 if ((i + 1) * sizeof(uint32_t) > left) {
3966 outs() << "\t\t remaining list entries extend past the of the section\n";
3969 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
3970 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3971 sys::swapByteOrder(l);
3973 print_indent(indent);
3974 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
3975 if (print_protocol(l, indent, info))
3976 outs() << "(not in an __OBJC section)\n";
3981 static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
3982 struct ivar_list64_t il;
3985 uint32_t offset, xoffset, left, j;
3987 const char *name, *sym_name, *ivar_offset_p;
3988 uint64_t ivar_offset, n_value;
3990 r = get_pointer_64(p, offset, left, S, info);
3993 memset(&il, '\0', sizeof(struct ivar_list64_t));
3994 if (left < sizeof(struct ivar_list64_t)) {
3995 memcpy(&il, r, left);
3996 outs() << " (ivar_list_t entends past the end of the section)\n";
3998 memcpy(&il, r, sizeof(struct ivar_list64_t));
3999 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4001 outs() << " entsize " << il.entsize << "\n";
4002 outs() << " count " << il.count << "\n";
4004 p += sizeof(struct ivar_list64_t);
4005 offset += sizeof(struct ivar_list64_t);
4006 for (j = 0; j < il.count; j++) {
4007 r = get_pointer_64(p, offset, left, S, info);
4010 memset(&i, '\0', sizeof(struct ivar64_t));
4011 if (left < sizeof(struct ivar64_t)) {
4012 memcpy(&i, r, left);
4013 outs() << " (ivar_t entends past the end of the section)\n";
4015 memcpy(&i, r, sizeof(struct ivar64_t));
4016 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4019 outs() << "\t\t\t offset ";
4020 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
4021 info, n_value, i.offset);
4023 if (info->verbose && sym_name != nullptr)
4026 outs() << format("0x%" PRIx64, n_value);
4028 outs() << " + " << format("0x%" PRIx64, i.offset);
4030 outs() << format("0x%" PRIx64, i.offset);
4031 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
4032 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4033 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4034 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4035 sys::swapByteOrder(ivar_offset);
4036 outs() << " " << ivar_offset << "\n";
4040 outs() << "\t\t\t name ";
4041 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
4044 if (info->verbose && sym_name != nullptr)
4047 outs() << format("0x%" PRIx64, n_value);
4049 outs() << " + " << format("0x%" PRIx64, i.name);
4051 outs() << format("0x%" PRIx64, i.name);
4052 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
4053 if (name != nullptr)
4054 outs() << format(" %.*s", left, name);
4057 outs() << "\t\t\t type ";
4058 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
4060 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
4062 if (info->verbose && sym_name != nullptr)
4065 outs() << format("0x%" PRIx64, n_value);
4067 outs() << " + " << format("0x%" PRIx64, i.type);
4069 outs() << format("0x%" PRIx64, i.type);
4070 if (name != nullptr)
4071 outs() << format(" %.*s", left, name);
4074 outs() << "\t\t\talignment " << i.alignment << "\n";
4075 outs() << "\t\t\t size " << i.size << "\n";
4077 p += sizeof(struct ivar64_t);
4078 offset += sizeof(struct ivar64_t);
4082 static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
4083 struct ivar_list32_t il;
4086 uint32_t offset, xoffset, left, j;
4088 const char *name, *ivar_offset_p;
4089 uint32_t ivar_offset;
4091 r = get_pointer_32(p, offset, left, S, info);
4094 memset(&il, '\0', sizeof(struct ivar_list32_t));
4095 if (left < sizeof(struct ivar_list32_t)) {
4096 memcpy(&il, r, left);
4097 outs() << " (ivar_list_t entends past the end of the section)\n";
4099 memcpy(&il, r, sizeof(struct ivar_list32_t));
4100 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4102 outs() << " entsize " << il.entsize << "\n";
4103 outs() << " count " << il.count << "\n";
4105 p += sizeof(struct ivar_list32_t);
4106 offset += sizeof(struct ivar_list32_t);
4107 for (j = 0; j < il.count; j++) {
4108 r = get_pointer_32(p, offset, left, S, info);
4111 memset(&i, '\0', sizeof(struct ivar32_t));
4112 if (left < sizeof(struct ivar32_t)) {
4113 memcpy(&i, r, left);
4114 outs() << " (ivar_t entends past the end of the section)\n";
4116 memcpy(&i, r, sizeof(struct ivar32_t));
4117 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4120 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
4121 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4122 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4123 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4124 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4125 sys::swapByteOrder(ivar_offset);
4126 outs() << " " << ivar_offset << "\n";
4130 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
4131 name = get_pointer_32(i.name, xoffset, left, xS, info);
4132 if (name != nullptr)
4133 outs() << format(" %.*s", left, name);
4136 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
4137 name = get_pointer_32(i.type, xoffset, left, xS, info);
4138 if (name != nullptr)
4139 outs() << format(" %.*s", left, name);
4142 outs() << "\t\t\talignment " << i.alignment << "\n";
4143 outs() << "\t\t\t size " << i.size << "\n";
4145 p += sizeof(struct ivar32_t);
4146 offset += sizeof(struct ivar32_t);
4150 static void print_objc_property_list64(uint64_t p,
4151 struct DisassembleInfo *info) {
4152 struct objc_property_list64 opl;
4153 struct objc_property64 op;
4155 uint32_t offset, xoffset, left, j;
4157 const char *name, *sym_name;
4160 r = get_pointer_64(p, offset, left, S, info);
4163 memset(&opl, '\0', sizeof(struct objc_property_list64));
4164 if (left < sizeof(struct objc_property_list64)) {
4165 memcpy(&opl, r, left);
4166 outs() << " (objc_property_list entends past the end of the section)\n";
4168 memcpy(&opl, r, sizeof(struct objc_property_list64));
4169 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4171 outs() << " entsize " << opl.entsize << "\n";
4172 outs() << " count " << opl.count << "\n";
4174 p += sizeof(struct objc_property_list64);
4175 offset += sizeof(struct objc_property_list64);
4176 for (j = 0; j < opl.count; j++) {
4177 r = get_pointer_64(p, offset, left, S, info);
4180 memset(&op, '\0', sizeof(struct objc_property64));
4181 if (left < sizeof(struct objc_property64)) {
4182 memcpy(&op, r, left);
4183 outs() << " (objc_property entends past the end of the section)\n";
4185 memcpy(&op, r, sizeof(struct objc_property64));
4186 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4189 outs() << "\t\t\t name ";
4190 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
4191 info, n_value, op.name);
4193 if (info->verbose && sym_name != nullptr)
4196 outs() << format("0x%" PRIx64, n_value);
4198 outs() << " + " << format("0x%" PRIx64, op.name);
4200 outs() << format("0x%" PRIx64, op.name);
4201 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
4202 if (name != nullptr)
4203 outs() << format(" %.*s", left, name);
4206 outs() << "\t\t\tattributes ";
4208 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
4209 info, n_value, op.attributes);
4211 if (info->verbose && sym_name != nullptr)
4214 outs() << format("0x%" PRIx64, n_value);
4215 if (op.attributes != 0)
4216 outs() << " + " << format("0x%" PRIx64, op.attributes);
4218 outs() << format("0x%" PRIx64, op.attributes);
4219 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
4220 if (name != nullptr)
4221 outs() << format(" %.*s", left, name);
4224 p += sizeof(struct objc_property64);
4225 offset += sizeof(struct objc_property64);
4229 static void print_objc_property_list32(uint32_t p,
4230 struct DisassembleInfo *info) {
4231 struct objc_property_list32 opl;
4232 struct objc_property32 op;
4234 uint32_t offset, xoffset, left, j;
4238 r = get_pointer_32(p, offset, left, S, info);
4241 memset(&opl, '\0', sizeof(struct objc_property_list32));
4242 if (left < sizeof(struct objc_property_list32)) {
4243 memcpy(&opl, r, left);
4244 outs() << " (objc_property_list entends past the end of the section)\n";
4246 memcpy(&opl, r, sizeof(struct objc_property_list32));
4247 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4249 outs() << " entsize " << opl.entsize << "\n";
4250 outs() << " count " << opl.count << "\n";
4252 p += sizeof(struct objc_property_list32);
4253 offset += sizeof(struct objc_property_list32);
4254 for (j = 0; j < opl.count; j++) {
4255 r = get_pointer_32(p, offset, left, S, info);
4258 memset(&op, '\0', sizeof(struct objc_property32));
4259 if (left < sizeof(struct objc_property32)) {
4260 memcpy(&op, r, left);
4261 outs() << " (objc_property entends past the end of the section)\n";
4263 memcpy(&op, r, sizeof(struct objc_property32));
4264 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4267 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
4268 name = get_pointer_32(op.name, xoffset, left, xS, info);
4269 if (name != nullptr)
4270 outs() << format(" %.*s", left, name);
4273 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
4274 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
4275 if (name != nullptr)
4276 outs() << format(" %.*s", left, name);
4279 p += sizeof(struct objc_property32);
4280 offset += sizeof(struct objc_property32);
4284 static void print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
4285 bool &is_meta_class) {
4286 struct class_ro64_t cro;
4288 uint32_t offset, xoffset, left;
4290 const char *name, *sym_name;
4293 r = get_pointer_64(p, offset, left, S, info);
4294 if (r == nullptr || left < sizeof(struct class_ro64_t))
4296 memset(&cro, '\0', sizeof(struct class_ro64_t));
4297 if (left < sizeof(struct class_ro64_t)) {
4298 memcpy(&cro, r, left);
4299 outs() << " (class_ro_t entends past the end of the section)\n";
4301 memcpy(&cro, r, sizeof(struct class_ro64_t));
4302 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4304 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4305 if (cro.flags & RO_META)
4306 outs() << " RO_META";
4307 if (cro.flags & RO_ROOT)
4308 outs() << " RO_ROOT";
4309 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4310 outs() << " RO_HAS_CXX_STRUCTORS";
4312 outs() << " instanceStart " << cro.instanceStart << "\n";
4313 outs() << " instanceSize " << cro.instanceSize << "\n";
4314 outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
4316 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
4318 print_layout_map64(cro.ivarLayout, info);
4321 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
4322 info, n_value, cro.name);
4324 if (info->verbose && sym_name != nullptr)
4327 outs() << format("0x%" PRIx64, n_value);
4329 outs() << " + " << format("0x%" PRIx64, cro.name);
4331 outs() << format("0x%" PRIx64, cro.name);
4332 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
4333 if (name != nullptr)
4334 outs() << format(" %.*s", left, name);
4337 outs() << " baseMethods ";
4338 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
4339 S, info, n_value, cro.baseMethods);
4341 if (info->verbose && sym_name != nullptr)
4344 outs() << format("0x%" PRIx64, n_value);
4345 if (cro.baseMethods != 0)
4346 outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
4348 outs() << format("0x%" PRIx64, cro.baseMethods);
4349 outs() << " (struct method_list_t *)\n";
4350 if (cro.baseMethods + n_value != 0)
4351 print_method_list64_t(cro.baseMethods + n_value, info, "");
4353 outs() << " baseProtocols ";
4355 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
4356 info, n_value, cro.baseProtocols);
4358 if (info->verbose && sym_name != nullptr)
4361 outs() << format("0x%" PRIx64, n_value);
4362 if (cro.baseProtocols != 0)
4363 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
4365 outs() << format("0x%" PRIx64, cro.baseProtocols);
4367 if (cro.baseProtocols + n_value != 0)
4368 print_protocol_list64_t(cro.baseProtocols + n_value, info);
4370 outs() << " ivars ";
4371 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
4372 info, n_value, cro.ivars);
4374 if (info->verbose && sym_name != nullptr)
4377 outs() << format("0x%" PRIx64, n_value);
4379 outs() << " + " << format("0x%" PRIx64, cro.ivars);
4381 outs() << format("0x%" PRIx64, cro.ivars);
4383 if (cro.ivars + n_value != 0)
4384 print_ivar_list64_t(cro.ivars + n_value, info);
4386 outs() << " weakIvarLayout ";
4388 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
4389 info, n_value, cro.weakIvarLayout);
4391 if (info->verbose && sym_name != nullptr)
4394 outs() << format("0x%" PRIx64, n_value);
4395 if (cro.weakIvarLayout != 0)
4396 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
4398 outs() << format("0x%" PRIx64, cro.weakIvarLayout);
4400 print_layout_map64(cro.weakIvarLayout + n_value, info);
4402 outs() << " baseProperties ";
4404 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
4405 info, n_value, cro.baseProperties);
4407 if (info->verbose && sym_name != nullptr)
4410 outs() << format("0x%" PRIx64, n_value);
4411 if (cro.baseProperties != 0)
4412 outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
4414 outs() << format("0x%" PRIx64, cro.baseProperties);
4416 if (cro.baseProperties + n_value != 0)
4417 print_objc_property_list64(cro.baseProperties + n_value, info);
4419 is_meta_class = (cro.flags & RO_META) ? true : false;
4422 static void print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
4423 bool &is_meta_class) {
4424 struct class_ro32_t cro;
4426 uint32_t offset, xoffset, left;
4430 r = get_pointer_32(p, offset, left, S, info);
4433 memset(&cro, '\0', sizeof(struct class_ro32_t));
4434 if (left < sizeof(struct class_ro32_t)) {
4435 memcpy(&cro, r, left);
4436 outs() << " (class_ro_t entends past the end of the section)\n";
4438 memcpy(&cro, r, sizeof(struct class_ro32_t));
4439 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4441 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4442 if (cro.flags & RO_META)
4443 outs() << " RO_META";
4444 if (cro.flags & RO_ROOT)
4445 outs() << " RO_ROOT";
4446 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4447 outs() << " RO_HAS_CXX_STRUCTORS";
4449 outs() << " instanceStart " << cro.instanceStart << "\n";
4450 outs() << " instanceSize " << cro.instanceSize << "\n";
4451 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
4453 print_layout_map32(cro.ivarLayout, info);
4455 outs() << " name " << format("0x%" PRIx32, cro.name);
4456 name = get_pointer_32(cro.name, xoffset, left, xS, info);
4457 if (name != nullptr)
4458 outs() << format(" %.*s", left, name);
4461 outs() << " baseMethods "
4462 << format("0x%" PRIx32, cro.baseMethods)
4463 << " (struct method_list_t *)\n";
4464 if (cro.baseMethods != 0)
4465 print_method_list32_t(cro.baseMethods, info, "");
4467 outs() << " baseProtocols "
4468 << format("0x%" PRIx32, cro.baseProtocols) << "\n";
4469 if (cro.baseProtocols != 0)
4470 print_protocol_list32_t(cro.baseProtocols, info);
4471 outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
4474 print_ivar_list32_t(cro.ivars, info);
4475 outs() << " weakIvarLayout "
4476 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
4477 print_layout_map32(cro.weakIvarLayout, info);
4478 outs() << " baseProperties "
4479 << format("0x%" PRIx32, cro.baseProperties) << "\n";
4480 if (cro.baseProperties != 0)
4481 print_objc_property_list32(cro.baseProperties, info);
4482 is_meta_class = (cro.flags & RO_META) ? true : false;
4485 static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
4488 uint32_t offset, left;
4491 uint64_t isa_n_value, n_value;
4493 r = get_pointer_64(p, offset, left, S, info);
4494 if (r == nullptr || left < sizeof(struct class64_t))
4496 memset(&c, '\0', sizeof(struct class64_t));
4497 if (left < sizeof(struct class64_t)) {
4498 memcpy(&c, r, left);
4499 outs() << " (class_t entends past the end of the section)\n";
4501 memcpy(&c, r, sizeof(struct class64_t));
4502 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4505 outs() << " isa " << format("0x%" PRIx64, c.isa);
4506 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
4507 isa_n_value, c.isa);
4508 if (name != nullptr)
4509 outs() << " " << name;
4512 outs() << " superclass " << format("0x%" PRIx64, c.superclass);
4513 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
4514 n_value, c.superclass);
4515 if (name != nullptr)
4516 outs() << " " << name;
4519 outs() << " cache " << format("0x%" PRIx64, c.cache);
4520 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
4522 if (name != nullptr)
4523 outs() << " " << name;
4526 outs() << " vtable " << format("0x%" PRIx64, c.vtable);
4527 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
4529 if (name != nullptr)
4530 outs() << " " << name;
4533 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
4537 if (info->verbose && name != nullptr)
4540 outs() << format("0x%" PRIx64, n_value);
4542 outs() << " + " << format("0x%" PRIx64, c.data);
4544 outs() << format("0x%" PRIx64, c.data);
4545 outs() << " (struct class_ro_t *)";
4547 // This is a Swift class if some of the low bits of the pointer are set.
4548 if ((c.data + n_value) & 0x7)
4549 outs() << " Swift class";
4552 print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class);
4554 if (is_meta_class == false) {
4555 outs() << "Meta Class\n";
4556 print_class64_t(c.isa + isa_n_value, info);
4560 static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
4563 uint32_t offset, left;
4567 r = get_pointer_32(p, offset, left, S, info);
4570 memset(&c, '\0', sizeof(struct class32_t));
4571 if (left < sizeof(struct class32_t)) {
4572 memcpy(&c, r, left);
4573 outs() << " (class_t entends past the end of the section)\n";
4575 memcpy(&c, r, sizeof(struct class32_t));
4576 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4579 outs() << " isa " << format("0x%" PRIx32, c.isa);
4581 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
4582 if (name != nullptr)
4583 outs() << " " << name;
4586 outs() << " superclass " << format("0x%" PRIx32, c.superclass);
4587 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
4589 if (name != nullptr)
4590 outs() << " " << name;
4593 outs() << " cache " << format("0x%" PRIx32, c.cache);
4594 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
4596 if (name != nullptr)
4597 outs() << " " << name;
4600 outs() << " vtable " << format("0x%" PRIx32, c.vtable);
4601 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
4603 if (name != nullptr)
4604 outs() << " " << name;
4608 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
4609 outs() << " data " << format("0x%" PRIx32, c.data)
4610 << " (struct class_ro_t *)";
4612 // This is a Swift class if some of the low bits of the pointer are set.
4614 outs() << " Swift class";
4617 print_class_ro32_t(c.data & ~0x3, info, is_meta_class);
4619 if (is_meta_class == false) {
4620 outs() << "Meta Class\n";
4621 print_class32_t(c.isa, info);
4625 static void print_objc_class_t(struct objc_class_t *objc_class,
4626 struct DisassembleInfo *info) {
4627 uint32_t offset, left, xleft;
4628 const char *name, *p, *ivar_list;
4631 struct objc_ivar_list_t objc_ivar_list;
4632 struct objc_ivar_t ivar;
4634 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
4635 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
4636 name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
4637 if (name != nullptr)
4638 outs() << format(" %.*s", left, name);
4640 outs() << " (not in an __OBJC section)";
4644 outs() << "\t super_class "
4645 << format("0x%08" PRIx32, objc_class->super_class);
4646 if (info->verbose) {
4647 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
4648 if (name != nullptr)
4649 outs() << format(" %.*s", left, name);
4651 outs() << " (not in an __OBJC section)";
4655 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name);
4656 if (info->verbose) {
4657 name = get_pointer_32(objc_class->name, offset, left, S, info, true);
4658 if (name != nullptr)
4659 outs() << format(" %.*s", left, name);
4661 outs() << " (not in an __OBJC section)";
4665 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
4668 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
4669 if (info->verbose) {
4670 if (CLS_GETINFO(objc_class, CLS_CLASS))
4671 outs() << " CLS_CLASS";
4672 else if (CLS_GETINFO(objc_class, CLS_META))
4673 outs() << " CLS_META";
4677 outs() << "\t instance_size "
4678 << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
4680 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
4681 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
4683 if (left > sizeof(struct objc_ivar_list_t)) {
4685 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
4687 outs() << " (entends past the end of the section)\n";
4688 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
4689 memcpy(&objc_ivar_list, p, left);
4691 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4692 swapStruct(objc_ivar_list);
4693 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
4694 ivar_list = p + sizeof(struct objc_ivar_list_t);
4695 for (i = 0; i < objc_ivar_list.ivar_count; i++) {
4696 if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
4697 outs() << "\t\t remaining ivar's extend past the of the section\n";
4700 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
4701 sizeof(struct objc_ivar_t));
4702 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4705 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
4706 if (info->verbose) {
4707 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
4708 if (name != nullptr)
4709 outs() << format(" %.*s", xleft, name);
4711 outs() << " (not in an __OBJC section)";
4715 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
4716 if (info->verbose) {
4717 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
4718 if (name != nullptr)
4719 outs() << format(" %.*s", xleft, name);
4721 outs() << " (not in an __OBJC section)";
4725 outs() << "\t\t ivar_offset "
4726 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
4729 outs() << " (not in an __OBJC section)\n";
4732 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
4733 if (print_method_list(objc_class->methodLists, info))
4734 outs() << " (not in an __OBJC section)\n";
4736 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
4739 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
4740 if (print_protocol_list(objc_class->protocols, 16, info))
4741 outs() << " (not in an __OBJC section)\n";
4744 static void print_objc_objc_category_t(struct objc_category_t *objc_category,
4745 struct DisassembleInfo *info) {
4746 uint32_t offset, left;
4750 outs() << "\t category name "
4751 << format("0x%08" PRIx32, objc_category->category_name);
4752 if (info->verbose) {
4753 name = get_pointer_32(objc_category->category_name, offset, left, S, info,
4755 if (name != nullptr)
4756 outs() << format(" %.*s", left, name);
4758 outs() << " (not in an __OBJC section)";
4762 outs() << "\t\t class name "
4763 << format("0x%08" PRIx32, objc_category->class_name);
4764 if (info->verbose) {
4766 get_pointer_32(objc_category->class_name, offset, left, S, info, true);
4767 if (name != nullptr)
4768 outs() << format(" %.*s", left, name);
4770 outs() << " (not in an __OBJC section)";
4774 outs() << "\t instance methods "
4775 << format("0x%08" PRIx32, objc_category->instance_methods);
4776 if (print_method_list(objc_category->instance_methods, info))
4777 outs() << " (not in an __OBJC section)\n";
4779 outs() << "\t class methods "
4780 << format("0x%08" PRIx32, objc_category->class_methods);
4781 if (print_method_list(objc_category->class_methods, info))
4782 outs() << " (not in an __OBJC section)\n";
4785 static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
4786 struct category64_t c;
4788 uint32_t offset, xoffset, left;
4790 const char *name, *sym_name;
4793 r = get_pointer_64(p, offset, left, S, info);
4796 memset(&c, '\0', sizeof(struct category64_t));
4797 if (left < sizeof(struct category64_t)) {
4798 memcpy(&c, r, left);
4799 outs() << " (category_t entends past the end of the section)\n";
4801 memcpy(&c, r, sizeof(struct category64_t));
4802 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4806 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
4807 info, n_value, c.name);
4809 if (info->verbose && sym_name != nullptr)
4812 outs() << format("0x%" PRIx64, n_value);
4814 outs() << " + " << format("0x%" PRIx64, c.name);
4816 outs() << format("0x%" PRIx64, c.name);
4817 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
4818 if (name != nullptr)
4819 outs() << format(" %.*s", left, name);
4823 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
4826 if (info->verbose && sym_name != nullptr)
4829 outs() << format("0x%" PRIx64, n_value);
4831 outs() << " + " << format("0x%" PRIx64, c.cls);
4833 outs() << format("0x%" PRIx64, c.cls);
4835 if (c.cls + n_value != 0)
4836 print_class64_t(c.cls + n_value, info);
4838 outs() << " instanceMethods ";
4840 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
4841 info, n_value, c.instanceMethods);
4843 if (info->verbose && sym_name != nullptr)
4846 outs() << format("0x%" PRIx64, n_value);
4847 if (c.instanceMethods != 0)
4848 outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
4850 outs() << format("0x%" PRIx64, c.instanceMethods);
4852 if (c.instanceMethods + n_value != 0)
4853 print_method_list64_t(c.instanceMethods + n_value, info, "");
4855 outs() << " classMethods ";
4856 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
4857 S, info, n_value, c.classMethods);
4859 if (info->verbose && sym_name != nullptr)
4862 outs() << format("0x%" PRIx64, n_value);
4863 if (c.classMethods != 0)
4864 outs() << " + " << format("0x%" PRIx64, c.classMethods);
4866 outs() << format("0x%" PRIx64, c.classMethods);
4868 if (c.classMethods + n_value != 0)
4869 print_method_list64_t(c.classMethods + n_value, info, "");
4871 outs() << " protocols ";
4872 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
4873 info, n_value, c.protocols);
4875 if (info->verbose && sym_name != nullptr)
4878 outs() << format("0x%" PRIx64, n_value);
4879 if (c.protocols != 0)
4880 outs() << " + " << format("0x%" PRIx64, c.protocols);
4882 outs() << format("0x%" PRIx64, c.protocols);
4884 if (c.protocols + n_value != 0)
4885 print_protocol_list64_t(c.protocols + n_value, info);
4887 outs() << "instanceProperties ";
4889 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
4890 S, info, n_value, c.instanceProperties);
4892 if (info->verbose && sym_name != nullptr)
4895 outs() << format("0x%" PRIx64, n_value);
4896 if (c.instanceProperties != 0)
4897 outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
4899 outs() << format("0x%" PRIx64, c.instanceProperties);
4901 if (c.instanceProperties + n_value != 0)
4902 print_objc_property_list64(c.instanceProperties + n_value, info);
4905 static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
4906 struct category32_t c;
4908 uint32_t offset, left;
4912 r = get_pointer_32(p, offset, left, S, info);
4915 memset(&c, '\0', sizeof(struct category32_t));
4916 if (left < sizeof(struct category32_t)) {
4917 memcpy(&c, r, left);
4918 outs() << " (category_t entends past the end of the section)\n";
4920 memcpy(&c, r, sizeof(struct category32_t));
4921 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4924 outs() << " name " << format("0x%" PRIx32, c.name);
4925 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
4928 outs() << " " << name;
4931 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
4933 print_class32_t(c.cls, info);
4934 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
4936 if (c.instanceMethods != 0)
4937 print_method_list32_t(c.instanceMethods, info, "");
4938 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
4940 if (c.classMethods != 0)
4941 print_method_list32_t(c.classMethods, info, "");
4942 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
4943 if (c.protocols != 0)
4944 print_protocol_list32_t(c.protocols, info);
4945 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
4947 if (c.instanceProperties != 0)
4948 print_objc_property_list32(c.instanceProperties, info);
4951 static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
4952 uint32_t i, left, offset, xoffset;
4953 uint64_t p, n_value;
4954 struct message_ref64 mr;
4955 const char *name, *sym_name;
4959 if (S == SectionRef())
4963 S.getName(SectName);
4964 DataRefImpl Ref = S.getRawDataRefImpl();
4965 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4966 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4968 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
4969 p = S.getAddress() + i;
4970 r = get_pointer_64(p, offset, left, S, info);
4973 memset(&mr, '\0', sizeof(struct message_ref64));
4974 if (left < sizeof(struct message_ref64)) {
4975 memcpy(&mr, r, left);
4976 outs() << " (message_ref entends past the end of the section)\n";
4978 memcpy(&mr, r, sizeof(struct message_ref64));
4979 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4983 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
4986 outs() << format("0x%" PRIx64, n_value) << " ";
4988 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
4990 outs() << format("0x%" PRIx64, mr.imp) << " ";
4991 if (name != nullptr)
4992 outs() << " " << name;
4996 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
4997 info, n_value, mr.sel);
4999 if (info->verbose && sym_name != nullptr)
5002 outs() << format("0x%" PRIx64, n_value);
5004 outs() << " + " << format("0x%" PRIx64, mr.sel);
5006 outs() << format("0x%" PRIx64, mr.sel);
5007 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
5008 if (name != nullptr)
5009 outs() << format(" %.*s", left, name);
5012 offset += sizeof(struct message_ref64);
5016 static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
5017 uint32_t i, left, offset, xoffset, p;
5018 struct message_ref32 mr;
5019 const char *name, *r;
5022 if (S == SectionRef())
5026 S.getName(SectName);
5027 DataRefImpl Ref = S.getRawDataRefImpl();
5028 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5029 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5031 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5032 p = S.getAddress() + i;
5033 r = get_pointer_32(p, offset, left, S, info);
5036 memset(&mr, '\0', sizeof(struct message_ref32));
5037 if (left < sizeof(struct message_ref32)) {
5038 memcpy(&mr, r, left);
5039 outs() << " (message_ref entends past the end of the section)\n";
5041 memcpy(&mr, r, sizeof(struct message_ref32));
5042 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5045 outs() << " imp " << format("0x%" PRIx32, mr.imp);
5046 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
5048 if (name != nullptr)
5049 outs() << " " << name;
5052 outs() << " sel " << format("0x%" PRIx32, mr.sel);
5053 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
5054 if (name != nullptr)
5055 outs() << " " << name;
5058 offset += sizeof(struct message_ref32);
5062 static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
5063 uint32_t left, offset, swift_version;
5065 struct objc_image_info64 o;
5069 S.getName(SectName);
5070 DataRefImpl Ref = S.getRawDataRefImpl();
5071 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5072 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5074 r = get_pointer_64(p, offset, left, S, info);
5077 memset(&o, '\0', sizeof(struct objc_image_info64));
5078 if (left < sizeof(struct objc_image_info64)) {
5079 memcpy(&o, r, left);
5080 outs() << " (objc_image_info entends past the end of the section)\n";
5082 memcpy(&o, r, sizeof(struct objc_image_info64));
5083 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5085 outs() << " version " << o.version << "\n";
5086 outs() << " flags " << format("0x%" PRIx32, o.flags);
5087 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5088 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5089 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5090 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5091 swift_version = (o.flags >> 8) & 0xff;
5092 if (swift_version != 0) {
5093 if (swift_version == 1)
5094 outs() << " Swift 1.0";
5095 else if (swift_version == 2)
5096 outs() << " Swift 1.1";
5098 outs() << " unknown future Swift version (" << swift_version << ")";
5103 static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5104 uint32_t left, offset, swift_version, p;
5105 struct objc_image_info32 o;
5109 S.getName(SectName);
5110 DataRefImpl Ref = S.getRawDataRefImpl();
5111 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5112 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5114 r = get_pointer_32(p, offset, left, S, info);
5117 memset(&o, '\0', sizeof(struct objc_image_info32));
5118 if (left < sizeof(struct objc_image_info32)) {
5119 memcpy(&o, r, left);
5120 outs() << " (objc_image_info entends past the end of the section)\n";
5122 memcpy(&o, r, sizeof(struct objc_image_info32));
5123 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5125 outs() << " version " << o.version << "\n";
5126 outs() << " flags " << format("0x%" PRIx32, o.flags);
5127 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5128 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5129 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5130 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5131 swift_version = (o.flags >> 8) & 0xff;
5132 if (swift_version != 0) {
5133 if (swift_version == 1)
5134 outs() << " Swift 1.0";
5135 else if (swift_version == 2)
5136 outs() << " Swift 1.1";
5138 outs() << " unknown future Swift version (" << swift_version << ")";
5143 static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5144 uint32_t left, offset, p;
5145 struct imageInfo_t o;
5149 S.getName(SectName);
5150 DataRefImpl Ref = S.getRawDataRefImpl();
5151 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5152 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5154 r = get_pointer_32(p, offset, left, S, info);
5157 memset(&o, '\0', sizeof(struct imageInfo_t));
5158 if (left < sizeof(struct imageInfo_t)) {
5159 memcpy(&o, r, left);
5160 outs() << " (imageInfo entends past the end of the section)\n";
5162 memcpy(&o, r, sizeof(struct imageInfo_t));
5163 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5165 outs() << " version " << o.version << "\n";
5166 outs() << " flags " << format("0x%" PRIx32, o.flags);
5172 outs() << " GC-only";
5178 static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
5179 SymbolAddressMap AddrMap;
5181 CreateSymbolAddressMap(O, &AddrMap);
5183 std::vector<SectionRef> Sections;
5184 for (const SectionRef &Section : O->sections()) {
5186 Section.getName(SectName);
5187 Sections.push_back(Section);
5190 struct DisassembleInfo info;
5191 // Set up the block of info used by the Symbolizer call backs.
5192 info.verbose = verbose;
5194 info.AddrMap = &AddrMap;
5195 info.Sections = &Sections;
5196 info.class_name = nullptr;
5197 info.selector_name = nullptr;
5198 info.method = nullptr;
5199 info.demangled_name = nullptr;
5200 info.bindtable = nullptr;
5204 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5205 if (CL != SectionRef()) {
5207 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5209 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5211 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5214 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5215 if (CR != SectionRef()) {
5217 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5219 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5221 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5224 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5225 if (SR != SectionRef()) {
5227 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5229 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5231 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5234 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5235 if (CA != SectionRef()) {
5237 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5239 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5241 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5244 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5245 if (PL != SectionRef()) {
5247 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5249 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5251 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5254 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5255 if (MR != SectionRef()) {
5257 print_message_refs64(MR, &info);
5259 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5261 print_message_refs64(MR, &info);
5264 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5265 if (II != SectionRef()) {
5267 print_image_info64(II, &info);
5269 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5271 print_image_info64(II, &info);
5274 if (info.bindtable != nullptr)
5275 delete info.bindtable;
5278 static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
5279 SymbolAddressMap AddrMap;
5281 CreateSymbolAddressMap(O, &AddrMap);
5283 std::vector<SectionRef> Sections;
5284 for (const SectionRef &Section : O->sections()) {
5286 Section.getName(SectName);
5287 Sections.push_back(Section);
5290 struct DisassembleInfo info;
5291 // Set up the block of info used by the Symbolizer call backs.
5292 info.verbose = verbose;
5294 info.AddrMap = &AddrMap;
5295 info.Sections = &Sections;
5296 info.class_name = nullptr;
5297 info.selector_name = nullptr;
5298 info.method = nullptr;
5299 info.demangled_name = nullptr;
5300 info.bindtable = nullptr;
5304 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5305 if (CL != SectionRef()) {
5307 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5309 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5311 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5314 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5315 if (CR != SectionRef()) {
5317 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5319 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5321 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5324 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5325 if (SR != SectionRef()) {
5327 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5329 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5331 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5334 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5335 if (CA != SectionRef()) {
5337 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5339 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5341 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5344 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5345 if (PL != SectionRef()) {
5347 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5349 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5351 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5354 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5355 if (MR != SectionRef()) {
5357 print_message_refs32(MR, &info);
5359 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5361 print_message_refs32(MR, &info);
5364 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5365 if (II != SectionRef()) {
5367 print_image_info32(II, &info);
5369 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5371 print_image_info32(II, &info);
5375 static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
5376 uint32_t i, j, p, offset, xoffset, left, defs_left, def;
5377 const char *r, *name, *defs;
5378 struct objc_module_t module;
5380 struct objc_symtab_t symtab;
5381 struct objc_class_t objc_class;
5382 struct objc_category_t objc_category;
5384 outs() << "Objective-C segment\n";
5385 S = get_section(O, "__OBJC", "__module_info");
5386 if (S == SectionRef())
5389 SymbolAddressMap AddrMap;
5391 CreateSymbolAddressMap(O, &AddrMap);
5393 std::vector<SectionRef> Sections;
5394 for (const SectionRef &Section : O->sections()) {
5396 Section.getName(SectName);
5397 Sections.push_back(Section);
5400 struct DisassembleInfo info;
5401 // Set up the block of info used by the Symbolizer call backs.
5402 info.verbose = verbose;
5404 info.AddrMap = &AddrMap;
5405 info.Sections = &Sections;
5406 info.class_name = nullptr;
5407 info.selector_name = nullptr;
5408 info.method = nullptr;
5409 info.demangled_name = nullptr;
5410 info.bindtable = nullptr;
5414 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
5415 p = S.getAddress() + i;
5416 r = get_pointer_32(p, offset, left, S, &info, true);
5419 memset(&module, '\0', sizeof(struct objc_module_t));
5420 if (left < sizeof(struct objc_module_t)) {
5421 memcpy(&module, r, left);
5422 outs() << " (module extends past end of __module_info section)\n";
5424 memcpy(&module, r, sizeof(struct objc_module_t));
5425 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5428 outs() << "Module " << format("0x%" PRIx32, p) << "\n";
5429 outs() << " version " << module.version << "\n";
5430 outs() << " size " << module.size << "\n";
5432 name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
5433 if (name != nullptr)
5434 outs() << format("%.*s", left, name);
5436 outs() << format("0x%08" PRIx32, module.name)
5437 << "(not in an __OBJC section)";
5440 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
5441 if (module.symtab == 0 || r == nullptr) {
5442 outs() << " symtab " << format("0x%08" PRIx32, module.symtab)
5443 << " (not in an __OBJC section)\n";
5446 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
5447 memset(&symtab, '\0', sizeof(struct objc_symtab_t));
5450 if (left < sizeof(struct objc_symtab_t)) {
5451 memcpy(&symtab, r, left);
5452 outs() << "\tsymtab extends past end of an __OBJC section)\n";
5454 memcpy(&symtab, r, sizeof(struct objc_symtab_t));
5455 if (left > sizeof(struct objc_symtab_t)) {
5456 defs_left = left - sizeof(struct objc_symtab_t);
5457 defs = r + sizeof(struct objc_symtab_t);
5460 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5463 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
5464 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
5465 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
5467 outs() << " (not in an __OBJC section)";
5469 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
5470 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
5471 if (symtab.cls_def_cnt > 0)
5472 outs() << "\tClass Definitions\n";
5473 for (j = 0; j < symtab.cls_def_cnt; j++) {
5474 if ((j + 1) * sizeof(uint32_t) > defs_left) {
5475 outs() << "\t(remaining class defs entries entends past the end of the "
5479 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
5480 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5481 sys::swapByteOrder(def);
5483 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5484 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
5486 if (left > sizeof(struct objc_class_t)) {
5488 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5490 outs() << " (entends past the end of the section)\n";
5491 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5492 memcpy(&objc_class, r, left);
5494 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5495 swapStruct(objc_class);
5496 print_objc_class_t(&objc_class, &info);
5498 outs() << "(not in an __OBJC section)\n";
5501 if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
5502 outs() << "\tMeta Class";
5503 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
5505 if (left > sizeof(struct objc_class_t)) {
5507 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5509 outs() << " (entends past the end of the section)\n";
5510 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5511 memcpy(&objc_class, r, left);
5513 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5514 swapStruct(objc_class);
5515 print_objc_class_t(&objc_class, &info);
5517 outs() << "(not in an __OBJC section)\n";
5521 if (symtab.cat_def_cnt > 0)
5522 outs() << "\tCategory Definitions\n";
5523 for (j = 0; j < symtab.cat_def_cnt; j++) {
5524 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
5525 outs() << "\t(remaining category defs entries entends past the end of "
5526 << "the section)\n";
5529 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
5531 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5532 sys::swapByteOrder(def);
5534 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5535 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
5536 << format("0x%08" PRIx32, def);
5538 if (left > sizeof(struct objc_category_t)) {
5540 memcpy(&objc_category, r, sizeof(struct objc_category_t));
5542 outs() << " (entends past the end of the section)\n";
5543 memset(&objc_category, '\0', sizeof(struct objc_category_t));
5544 memcpy(&objc_category, r, left);
5546 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5547 swapStruct(objc_category);
5548 print_objc_objc_category_t(&objc_category, &info);
5550 outs() << "(not in an __OBJC section)\n";
5554 const SectionRef II = get_section(O, "__OBJC", "__image_info");
5555 if (II != SectionRef())
5556 print_image_info(II, &info);
5561 static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
5562 uint32_t size, uint32_t addr) {
5563 SymbolAddressMap AddrMap;
5564 CreateSymbolAddressMap(O, &AddrMap);
5566 std::vector<SectionRef> Sections;
5567 for (const SectionRef &Section : O->sections()) {
5569 Section.getName(SectName);
5570 Sections.push_back(Section);
5573 struct DisassembleInfo info;
5574 // Set up the block of info used by the Symbolizer call backs.
5575 info.verbose = true;
5577 info.AddrMap = &AddrMap;
5578 info.Sections = &Sections;
5579 info.class_name = nullptr;
5580 info.selector_name = nullptr;
5581 info.method = nullptr;
5582 info.demangled_name = nullptr;
5583 info.bindtable = nullptr;
5588 struct objc_protocol_t protocol;
5589 uint32_t left, paddr;
5590 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
5591 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
5592 left = size - (p - sect);
5593 if (left < sizeof(struct objc_protocol_t)) {
5594 outs() << "Protocol extends past end of __protocol section\n";
5595 memcpy(&protocol, p, left);
5597 memcpy(&protocol, p, sizeof(struct objc_protocol_t));
5598 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5599 swapStruct(protocol);
5600 paddr = addr + (p - sect);
5601 outs() << "Protocol " << format("0x%" PRIx32, paddr);
5602 if (print_protocol(paddr, 0, &info))
5603 outs() << "(not in an __OBJC section)\n";
5607 static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
5609 printObjc2_64bit_MetaData(O, verbose);
5611 MachO::mach_header H;
5613 if (H.cputype == MachO::CPU_TYPE_ARM)
5614 printObjc2_32bit_MetaData(O, verbose);
5616 // This is the 32-bit non-arm cputype case. Which is normally
5617 // the first Objective-C ABI. But it may be the case of a
5618 // binary for the iOS simulator which is the second Objective-C
5619 // ABI. In that case printObjc1_32bit_MetaData() will determine that
5620 // and return false.
5621 if (printObjc1_32bit_MetaData(O, verbose) == false)
5622 printObjc2_32bit_MetaData(O, verbose);
5627 // GuessLiteralPointer returns a string which for the item in the Mach-O file
5628 // for the address passed in as ReferenceValue for printing as a comment with
5629 // the instruction and also returns the corresponding type of that item
5630 // indirectly through ReferenceType.
5632 // If ReferenceValue is an address of literal cstring then a pointer to the
5633 // cstring is returned and ReferenceType is set to
5634 // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
5636 // If ReferenceValue is an address of an Objective-C CFString, Selector ref or
5637 // Class ref that name is returned and the ReferenceType is set accordingly.
5639 // Lastly, literals which are Symbol address in a literal pool are looked for
5640 // and if found the symbol name is returned and ReferenceType is set to
5641 // LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
5643 // If there is no item in the Mach-O file for the address passed in as
5644 // ReferenceValue nullptr is returned and ReferenceType is unchanged.
5645 static const char *GuessLiteralPointer(uint64_t ReferenceValue,
5646 uint64_t ReferencePC,
5647 uint64_t *ReferenceType,
5648 struct DisassembleInfo *info) {
5649 // First see if there is an external relocation entry at the ReferencePC.
5650 uint64_t sect_addr = info->S.getAddress();
5651 uint64_t sect_offset = ReferencePC - sect_addr;
5652 bool reloc_found = false;
5654 MachO::any_relocation_info RE;
5655 bool isExtern = false;
5657 for (const RelocationRef &Reloc : info->S.relocations()) {
5658 uint64_t RelocOffset;
5659 Reloc.getOffset(RelocOffset);
5660 if (RelocOffset == sect_offset) {
5661 Rel = Reloc.getRawDataRefImpl();
5662 RE = info->O->getRelocation(Rel);
5663 if (info->O->isRelocationScattered(RE))
5665 isExtern = info->O->getPlainRelocationExternal(RE);
5667 symbol_iterator RelocSym = Reloc.getSymbol();
5674 // If there is an external relocation entry for a symbol in a section
5675 // then used that symbol's value for the value of the reference.
5676 if (reloc_found && isExtern) {
5677 if (info->O->getAnyRelocationPCRel(RE)) {
5678 unsigned Type = info->O->getAnyRelocationType(RE);
5679 if (Type == MachO::X86_64_RELOC_SIGNED) {
5680 Symbol.getAddress(ReferenceValue);
5685 // Look for literals such as Objective-C CFStrings refs, Selector refs,
5686 // Message refs and Class refs.
5687 bool classref, selref, msgref, cfstring;
5688 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
5689 selref, msgref, cfstring);
5690 if (classref && pointer_value == 0) {
5691 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
5692 // And the pointer_value in that section is typically zero as it will be
5693 // set by dyld as part of the "bind information".
5694 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
5695 if (name != nullptr) {
5696 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
5697 const char *class_name = strrchr(name, '$');
5698 if (class_name != nullptr && class_name[1] == '_' &&
5699 class_name[2] != '\0') {
5700 info->class_name = class_name + 2;
5707 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
5709 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
5710 if (name != nullptr)
5711 info->class_name = name;
5713 name = "bad class ref";
5718 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
5719 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
5723 if (selref && pointer_value == 0)
5724 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
5726 if (pointer_value != 0)
5727 ReferenceValue = pointer_value;
5729 const char *name = GuessCstringPointer(ReferenceValue, info);
5731 if (pointer_value != 0 && selref) {
5732 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
5733 info->selector_name = name;
5734 } else if (pointer_value != 0 && msgref) {
5735 info->class_name = nullptr;
5736 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
5737 info->selector_name = name;
5739 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
5743 // Lastly look for an indirect symbol with this ReferenceValue which is in
5744 // a literal pool. If found return that symbol name.
5745 name = GuessIndirectSymbol(ReferenceValue, info);
5747 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
5754 // SymbolizerSymbolLookUp is the symbol lookup function passed when creating
5755 // the Symbolizer. It looks up the ReferenceValue using the info passed via the
5756 // pointer to the struct DisassembleInfo that was passed when MCSymbolizer
5757 // is created and returns the symbol name that matches the ReferenceValue or
5758 // nullptr if none. The ReferenceType is passed in for the IN type of
5759 // reference the instruction is making from the values in defined in the header
5760 // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
5761 // Out type and the ReferenceName will also be set which is added as a comment
5762 // to the disassembled instruction.
5765 // If the symbol name is a C++ mangled name then the demangled name is
5766 // returned through ReferenceName and ReferenceType is set to
5767 // LLVMDisassembler_ReferenceType_DeMangled_Name .
5770 // When this is called to get a symbol name for a branch target then the
5771 // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
5772 // SymbolValue will be looked for in the indirect symbol table to determine if
5773 // it is an address for a symbol stub. If so then the symbol name for that
5774 // stub is returned indirectly through ReferenceName and then ReferenceType is
5775 // set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
5777 // When this is called with an value loaded via a PC relative load then
5778 // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
5779 // SymbolValue is checked to be an address of literal pointer, symbol pointer,
5780 // or an Objective-C meta data reference. If so the output ReferenceType is
5781 // set to correspond to that as well as setting the ReferenceName.
5782 static const char *SymbolizerSymbolLookUp(void *DisInfo,
5783 uint64_t ReferenceValue,
5784 uint64_t *ReferenceType,
5785 uint64_t ReferencePC,
5786 const char **ReferenceName) {
5787 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
5788 // If no verbose symbolic information is wanted then just return nullptr.
5789 if (!info->verbose) {
5790 *ReferenceName = nullptr;
5791 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5795 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
5797 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
5798 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
5799 if (*ReferenceName != nullptr) {
5800 method_reference(info, ReferenceType, ReferenceName);
5801 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
5802 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
5805 if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
5806 if (info->demangled_name != nullptr)
5807 free(info->demangled_name);
5809 info->demangled_name =
5810 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
5811 if (info->demangled_name != nullptr) {
5812 *ReferenceName = info->demangled_name;
5813 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5815 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5818 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5819 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
5821 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5823 method_reference(info, ReferenceType, ReferenceName);
5825 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5826 // If this is arm64 and the reference is an adrp instruction save the
5827 // instruction, passed in ReferenceValue and the address of the instruction
5828 // for use later if we see and add immediate instruction.
5829 } else if (info->O->getArch() == Triple::aarch64 &&
5830 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
5831 info->adrp_inst = ReferenceValue;
5832 info->adrp_addr = ReferencePC;
5833 SymbolName = nullptr;
5834 *ReferenceName = nullptr;
5835 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5836 // If this is arm64 and reference is an add immediate instruction and we
5838 // seen an adrp instruction just before it and the adrp's Xd register
5840 // this add's Xn register reconstruct the value being referenced and look to
5841 // see if it is a literal pointer. Note the add immediate instruction is
5842 // passed in ReferenceValue.
5843 } else if (info->O->getArch() == Triple::aarch64 &&
5844 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
5845 ReferencePC - 4 == info->adrp_addr &&
5846 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5847 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5848 uint32_t addxri_inst;
5849 uint64_t adrp_imm, addxri_imm;
5852 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5853 if (info->adrp_inst & 0x0200000)
5854 adrp_imm |= 0xfffffffffc000000LL;
5856 addxri_inst = ReferenceValue;
5857 addxri_imm = (addxri_inst >> 10) & 0xfff;
5858 if (((addxri_inst >> 22) & 0x3) == 1)
5861 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5862 (adrp_imm << 12) + addxri_imm;
5865 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5866 if (*ReferenceName == nullptr)
5867 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5868 // If this is arm64 and the reference is a load register instruction and we
5869 // have seen an adrp instruction just before it and the adrp's Xd register
5870 // matches this add's Xn register reconstruct the value being referenced and
5871 // look to see if it is a literal pointer. Note the load register
5872 // instruction is passed in ReferenceValue.
5873 } else if (info->O->getArch() == Triple::aarch64 &&
5874 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
5875 ReferencePC - 4 == info->adrp_addr &&
5876 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5877 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5878 uint32_t ldrxui_inst;
5879 uint64_t adrp_imm, ldrxui_imm;
5882 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5883 if (info->adrp_inst & 0x0200000)
5884 adrp_imm |= 0xfffffffffc000000LL;
5886 ldrxui_inst = ReferenceValue;
5887 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
5889 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5890 (adrp_imm << 12) + (ldrxui_imm << 3);
5893 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5894 if (*ReferenceName == nullptr)
5895 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5897 // If this arm64 and is an load register (PC-relative) instruction the
5898 // ReferenceValue is the PC plus the immediate value.
5899 else if (info->O->getArch() == Triple::aarch64 &&
5900 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
5901 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
5903 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5904 if (*ReferenceName == nullptr)
5905 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5908 else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
5909 if (info->demangled_name != nullptr)
5910 free(info->demangled_name);
5912 info->demangled_name =
5913 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
5914 if (info->demangled_name != nullptr) {
5915 *ReferenceName = info->demangled_name;
5916 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5921 *ReferenceName = nullptr;
5922 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5928 /// \brief Emits the comments that are stored in the CommentStream.
5929 /// Each comment in the CommentStream must end with a newline.
5930 static void emitComments(raw_svector_ostream &CommentStream,
5931 SmallString<128> &CommentsToEmit,
5932 formatted_raw_ostream &FormattedOS,
5933 const MCAsmInfo &MAI) {
5934 // Flush the stream before taking its content.
5935 CommentStream.flush();
5936 StringRef Comments = CommentsToEmit.str();
5937 // Get the default information for printing a comment.
5938 const char *CommentBegin = MAI.getCommentString();
5939 unsigned CommentColumn = MAI.getCommentColumn();
5940 bool IsFirst = true;
5941 while (!Comments.empty()) {
5943 FormattedOS << '\n';
5944 // Emit a line of comments.
5945 FormattedOS.PadToColumn(CommentColumn);
5946 size_t Position = Comments.find('\n');
5947 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
5948 // Move after the newline character.
5949 Comments = Comments.substr(Position + 1);
5952 FormattedOS.flush();
5954 // Tell the comment stream that the vector changed underneath it.
5955 CommentsToEmit.clear();
5956 CommentStream.resync();
5959 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
5960 StringRef DisSegName, StringRef DisSectName) {
5961 const char *McpuDefault = nullptr;
5962 const Target *ThumbTarget = nullptr;
5963 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
5965 // GetTarget prints out stuff.
5968 if (MCPU.empty() && McpuDefault)
5971 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
5972 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
5974 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
5976 // Package up features to be passed to target/subtarget
5977 std::string FeaturesStr;
5978 if (MAttrs.size()) {
5979 SubtargetFeatures Features;
5980 for (unsigned i = 0; i != MAttrs.size(); ++i)
5981 Features.AddFeature(MAttrs[i]);
5982 FeaturesStr = Features.getString();
5985 // Set up disassembler.
5986 std::unique_ptr<const MCRegisterInfo> MRI(
5987 TheTarget->createMCRegInfo(TripleName));
5988 std::unique_ptr<const MCAsmInfo> AsmInfo(
5989 TheTarget->createMCAsmInfo(*MRI, TripleName));
5990 std::unique_ptr<const MCSubtargetInfo> STI(
5991 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
5992 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
5993 std::unique_ptr<MCDisassembler> DisAsm(
5994 TheTarget->createMCDisassembler(*STI, Ctx));
5995 std::unique_ptr<MCSymbolizer> Symbolizer;
5996 struct DisassembleInfo SymbolizerInfo;
5997 std::unique_ptr<MCRelocationInfo> RelInfo(
5998 TheTarget->createMCRelocationInfo(TripleName, Ctx));
6000 Symbolizer.reset(TheTarget->createMCSymbolizer(
6001 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
6002 &SymbolizerInfo, &Ctx, std::move(RelInfo)));
6003 DisAsm->setSymbolizer(std::move(Symbolizer));
6005 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
6006 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
6007 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
6008 // Set the display preference for hex vs. decimal immediates.
6009 IP->setPrintImmHex(PrintImmHex);
6010 // Comment stream and backing vector.
6011 SmallString<128> CommentsToEmit;
6012 raw_svector_ostream CommentStream(CommentsToEmit);
6013 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
6014 // if it is done then arm64 comments for string literals don't get printed
6015 // and some constant get printed instead and not setting it causes intel
6016 // (32-bit and 64-bit) comments printed with different spacing before the
6017 // comment causing different diffs with the 'C' disassembler library API.
6018 // IP->setCommentStream(CommentStream);
6020 if (!AsmInfo || !STI || !DisAsm || !IP) {
6021 errs() << "error: couldn't initialize disassembler for target "
6022 << TripleName << '\n';
6026 // Set up thumb disassembler.
6027 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
6028 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
6029 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
6030 std::unique_ptr<MCDisassembler> ThumbDisAsm;
6031 std::unique_ptr<MCInstPrinter> ThumbIP;
6032 std::unique_ptr<MCContext> ThumbCtx;
6033 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
6034 struct DisassembleInfo ThumbSymbolizerInfo;
6035 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
6037 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
6039 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
6041 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr));
6042 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
6043 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
6044 MCContext *PtrThumbCtx = ThumbCtx.get();
6046 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
6048 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
6049 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
6050 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
6051 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
6053 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
6054 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
6055 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
6056 *ThumbInstrInfo, *ThumbMRI));
6057 // Set the display preference for hex vs. decimal immediates.
6058 ThumbIP->setPrintImmHex(PrintImmHex);
6061 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
6062 errs() << "error: couldn't initialize disassembler for target "
6063 << ThumbTripleName << '\n';
6067 MachO::mach_header Header = MachOOF->getHeader();
6069 // FIXME: Using the -cfg command line option, this code used to be able to
6070 // annotate relocations with the referenced symbol's name, and if this was
6071 // inside a __[cf]string section, the data it points to. This is now replaced
6072 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
6073 std::vector<SectionRef> Sections;
6074 std::vector<SymbolRef> Symbols;
6075 SmallVector<uint64_t, 8> FoundFns;
6076 uint64_t BaseSegmentAddress;
6078 getSectionsAndSymbols(Header, MachOOF, Sections, Symbols, FoundFns,
6079 BaseSegmentAddress);
6081 // Sort the symbols by address, just in case they didn't come in that way.
6082 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
6084 // Build a data in code table that is sorted on by the address of each entry.
6085 uint64_t BaseAddress = 0;
6086 if (Header.filetype == MachO::MH_OBJECT)
6087 BaseAddress = Sections[0].getAddress();
6089 BaseAddress = BaseSegmentAddress;
6091 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
6094 DI->getOffset(Offset);
6095 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
6097 array_pod_sort(Dices.begin(), Dices.end());
6100 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
6102 raw_ostream &DebugOut = nulls();
6105 std::unique_ptr<DIContext> diContext;
6106 ObjectFile *DbgObj = MachOOF;
6107 // Try to find debug info and set up the DIContext for it.
6109 // A separate DSym file path was specified, parse it as a macho file,
6110 // get the sections and supply it to the section name parsing machinery.
6111 if (!DSYMFile.empty()) {
6112 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
6113 MemoryBuffer::getFileOrSTDIN(DSYMFile);
6114 if (std::error_code EC = BufOrErr.getError()) {
6115 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
6119 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
6124 // Setup the DIContext
6125 diContext.reset(new DWARFContextInMemory(*DbgObj));
6128 if (DumpSections.size() == 0)
6129 outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
6131 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
6133 if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
6136 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
6138 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
6139 if (SegmentName != DisSegName)
6143 Sections[SectIdx].getContents(BytesStr);
6144 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
6146 uint64_t SectAddress = Sections[SectIdx].getAddress();
6148 bool symbolTableWorked = false;
6150 // Parse relocations.
6151 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
6152 for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) {
6153 uint64_t RelocOffset;
6154 Reloc.getOffset(RelocOffset);
6155 uint64_t SectionAddress = Sections[SectIdx].getAddress();
6156 RelocOffset -= SectionAddress;
6158 symbol_iterator RelocSym = Reloc.getSymbol();
6160 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
6162 array_pod_sort(Relocs.begin(), Relocs.end());
6164 // Create a map of symbol addresses to symbol names for use by
6165 // the SymbolizerSymbolLookUp() routine.
6166 SymbolAddressMap AddrMap;
6167 bool DisSymNameFound = false;
6168 for (const SymbolRef &Symbol : MachOOF->symbols()) {
6171 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
6172 ST == SymbolRef::ST_Other) {
6174 Symbol.getAddress(Address);
6176 Symbol.getName(SymName);
6177 AddrMap[Address] = SymName;
6178 if (!DisSymName.empty() && DisSymName == SymName)
6179 DisSymNameFound = true;
6182 if (!DisSymName.empty() && !DisSymNameFound) {
6183 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
6186 // Set up the block of info used by the Symbolizer call backs.
6187 SymbolizerInfo.verbose = !NoSymbolicOperands;
6188 SymbolizerInfo.O = MachOOF;
6189 SymbolizerInfo.S = Sections[SectIdx];
6190 SymbolizerInfo.AddrMap = &AddrMap;
6191 SymbolizerInfo.Sections = &Sections;
6192 SymbolizerInfo.class_name = nullptr;
6193 SymbolizerInfo.selector_name = nullptr;
6194 SymbolizerInfo.method = nullptr;
6195 SymbolizerInfo.demangled_name = nullptr;
6196 SymbolizerInfo.bindtable = nullptr;
6197 SymbolizerInfo.adrp_addr = 0;
6198 SymbolizerInfo.adrp_inst = 0;
6199 // Same for the ThumbSymbolizer
6200 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
6201 ThumbSymbolizerInfo.O = MachOOF;
6202 ThumbSymbolizerInfo.S = Sections[SectIdx];
6203 ThumbSymbolizerInfo.AddrMap = &AddrMap;
6204 ThumbSymbolizerInfo.Sections = &Sections;
6205 ThumbSymbolizerInfo.class_name = nullptr;
6206 ThumbSymbolizerInfo.selector_name = nullptr;
6207 ThumbSymbolizerInfo.method = nullptr;
6208 ThumbSymbolizerInfo.demangled_name = nullptr;
6209 ThumbSymbolizerInfo.bindtable = nullptr;
6210 ThumbSymbolizerInfo.adrp_addr = 0;
6211 ThumbSymbolizerInfo.adrp_inst = 0;
6213 // Disassemble symbol by symbol.
6214 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
6216 Symbols[SymIdx].getName(SymName);
6219 Symbols[SymIdx].getType(ST);
6220 if (ST != SymbolRef::ST_Function)
6223 // Make sure the symbol is defined in this section.
6224 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
6228 // If we are only disassembling one symbol see if this is that symbol.
6229 if (!DisSymName.empty() && DisSymName != SymName)
6232 // Start at the address of the symbol relative to the section's address.
6234 uint64_t SectionAddress = Sections[SectIdx].getAddress();
6235 Symbols[SymIdx].getAddress(Start);
6236 Start -= SectionAddress;
6238 // Stop disassembling either at the beginning of the next symbol or at
6239 // the end of the section.
6240 bool containsNextSym = false;
6241 uint64_t NextSym = 0;
6242 uint64_t NextSymIdx = SymIdx + 1;
6243 while (Symbols.size() > NextSymIdx) {
6244 SymbolRef::Type NextSymType;
6245 Symbols[NextSymIdx].getType(NextSymType);
6246 if (NextSymType == SymbolRef::ST_Function) {
6248 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
6249 Symbols[NextSymIdx].getAddress(NextSym);
6250 NextSym -= SectionAddress;
6256 uint64_t SectSize = Sections[SectIdx].getSize();
6257 uint64_t End = containsNextSym ? NextSym : SectSize;
6260 symbolTableWorked = true;
6262 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
6264 (MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget;
6266 outs() << SymName << ":\n";
6267 DILineInfo lastLine;
6268 for (uint64_t Index = Start; Index < End; Index += Size) {
6271 uint64_t PC = SectAddress + Index;
6272 if (!NoLeadingAddr) {
6273 if (FullLeadingAddr) {
6274 if (MachOOF->is64Bit())
6275 outs() << format("%016" PRIx64, PC);
6277 outs() << format("%08" PRIx64, PC);
6279 outs() << format("%8" PRIx64 ":", PC);
6285 // Check the data in code table here to see if this is data not an
6286 // instruction to be disassembled.
6288 Dice.push_back(std::make_pair(PC, DiceRef()));
6289 dice_table_iterator DTI =
6290 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
6291 compareDiceTableEntries);
6292 if (DTI != Dices.end()) {
6294 DTI->second.getLength(Length);
6296 DTI->second.getKind(Kind);
6297 Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
6298 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
6299 (PC == (DTI->first + Length - 1)) && (Length & 1))
6304 SmallVector<char, 64> AnnotationsBytes;
6305 raw_svector_ostream Annotations(AnnotationsBytes);
6309 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
6310 PC, DebugOut, Annotations);
6312 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
6313 DebugOut, Annotations);
6315 if (!NoShowRawInsn) {
6316 DumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, Size));
6318 formatted_raw_ostream FormattedOS(outs());
6319 Annotations.flush();
6320 StringRef AnnotationsStr = Annotations.str();
6322 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
6324 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
6325 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
6327 // Print debug info.
6329 DILineInfo dli = diContext->getLineInfoForAddress(PC);
6330 // Print valid line info if it changed.
6331 if (dli != lastLine && dli.Line != 0)
6332 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
6338 unsigned int Arch = MachOOF->getArch();
6339 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
6340 outs() << format("\t.byte 0x%02x #bad opcode\n",
6341 *(Bytes.data() + Index) & 0xff);
6342 Size = 1; // skip exactly one illegible byte and move on.
6343 } else if (Arch == Triple::aarch64) {
6344 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6345 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
6346 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
6347 (*(Bytes.data() + Index + 3) & 0xff) << 24;
6348 outs() << format("\t.long\t0x%08x\n", opcode);
6351 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6353 Size = 1; // skip illegible bytes
6358 if (!symbolTableWorked) {
6359 // Reading the symbol table didn't work, disassemble the whole section.
6360 uint64_t SectAddress = Sections[SectIdx].getAddress();
6361 uint64_t SectSize = Sections[SectIdx].getSize();
6363 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
6366 uint64_t PC = SectAddress + Index;
6367 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
6368 DebugOut, nulls())) {
6369 if (!NoLeadingAddr) {
6370 if (FullLeadingAddr) {
6371 if (MachOOF->is64Bit())
6372 outs() << format("%016" PRIx64, PC);
6374 outs() << format("%08" PRIx64, PC);
6376 outs() << format("%8" PRIx64 ":", PC);
6379 if (!NoShowRawInsn) {
6381 DumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, InstSize));
6383 IP->printInst(&Inst, outs(), "", *STI);
6386 unsigned int Arch = MachOOF->getArch();
6387 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
6388 outs() << format("\t.byte 0x%02x #bad opcode\n",
6389 *(Bytes.data() + Index) & 0xff);
6390 InstSize = 1; // skip exactly one illegible byte and move on.
6392 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6394 InstSize = 1; // skip illegible bytes
6399 // The TripleName's need to be reset if we are called again for a different
6402 ThumbTripleName = "";
6404 if (SymbolizerInfo.method != nullptr)
6405 free(SymbolizerInfo.method);
6406 if (SymbolizerInfo.demangled_name != nullptr)
6407 free(SymbolizerInfo.demangled_name);
6408 if (SymbolizerInfo.bindtable != nullptr)
6409 delete SymbolizerInfo.bindtable;
6410 if (ThumbSymbolizerInfo.method != nullptr)
6411 free(ThumbSymbolizerInfo.method);
6412 if (ThumbSymbolizerInfo.demangled_name != nullptr)
6413 free(ThumbSymbolizerInfo.demangled_name);
6414 if (ThumbSymbolizerInfo.bindtable != nullptr)
6415 delete ThumbSymbolizerInfo.bindtable;
6419 //===----------------------------------------------------------------------===//
6420 // __compact_unwind section dumping
6421 //===----------------------------------------------------------------------===//
6425 template <typename T> static uint64_t readNext(const char *&Buf) {
6426 using llvm::support::little;
6427 using llvm::support::unaligned;
6429 uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
6434 struct CompactUnwindEntry {
6435 uint32_t OffsetInSection;
6437 uint64_t FunctionAddr;
6439 uint32_t CompactEncoding;
6440 uint64_t PersonalityAddr;
6443 RelocationRef FunctionReloc;
6444 RelocationRef PersonalityReloc;
6445 RelocationRef LSDAReloc;
6447 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
6448 : OffsetInSection(Offset) {
6450 read<uint64_t>(Contents.data() + Offset);
6452 read<uint32_t>(Contents.data() + Offset);
6456 template <typename UIntPtr> void read(const char *Buf) {
6457 FunctionAddr = readNext<UIntPtr>(Buf);
6458 Length = readNext<uint32_t>(Buf);
6459 CompactEncoding = readNext<uint32_t>(Buf);
6460 PersonalityAddr = readNext<UIntPtr>(Buf);
6461 LSDAAddr = readNext<UIntPtr>(Buf);
6466 /// Given a relocation from __compact_unwind, consisting of the RelocationRef
6467 /// and data being relocated, determine the best base Name and Addend to use for
6468 /// display purposes.
6470 /// 1. An Extern relocation will directly reference a symbol (and the data is
6471 /// then already an addend), so use that.
6472 /// 2. Otherwise the data is an offset in the object file's layout; try to find
6473 // a symbol before it in the same section, and use the offset from there.
6474 /// 3. Finally, if all that fails, fall back to an offset from the start of the
6475 /// referenced section.
6476 static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
6477 std::map<uint64_t, SymbolRef> &Symbols,
6478 const RelocationRef &Reloc, uint64_t Addr,
6479 StringRef &Name, uint64_t &Addend) {
6480 if (Reloc.getSymbol() != Obj->symbol_end()) {
6481 Reloc.getSymbol()->getName(Name);
6486 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
6487 SectionRef RelocSection = Obj->getRelocationSection(RE);
6489 uint64_t SectionAddr = RelocSection.getAddress();
6491 auto Sym = Symbols.upper_bound(Addr);
6492 if (Sym == Symbols.begin()) {
6493 // The first symbol in the object is after this reference, the best we can
6494 // do is section-relative notation.
6495 RelocSection.getName(Name);
6496 Addend = Addr - SectionAddr;
6500 // Go back one so that SymbolAddress <= Addr.
6503 section_iterator SymSection = Obj->section_end();
6504 Sym->second.getSection(SymSection);
6505 if (RelocSection == *SymSection) {
6506 // There's a valid symbol in the same section before this reference.
6507 Sym->second.getName(Name);
6508 Addend = Addr - Sym->first;
6512 // There is a symbol before this reference, but it's in a different
6513 // section. Probably not helpful to mention it, so use the section name.
6514 RelocSection.getName(Name);
6515 Addend = Addr - SectionAddr;
6518 static void printUnwindRelocDest(const MachOObjectFile *Obj,
6519 std::map<uint64_t, SymbolRef> &Symbols,
6520 const RelocationRef &Reloc, uint64_t Addr) {
6524 if (!Reloc.getObjectFile())
6527 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
6531 outs() << " + " << format("0x%" PRIx64, Addend);
6535 printMachOCompactUnwindSection(const MachOObjectFile *Obj,
6536 std::map<uint64_t, SymbolRef> &Symbols,
6537 const SectionRef &CompactUnwind) {
6539 assert(Obj->isLittleEndian() &&
6540 "There should not be a big-endian .o with __compact_unwind");
6542 bool Is64 = Obj->is64Bit();
6543 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
6544 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
6547 CompactUnwind.getContents(Contents);
6549 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
6551 // First populate the initial raw offsets, encodings and so on from the entry.
6552 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
6553 CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
6554 CompactUnwinds.push_back(Entry);
6557 // Next we need to look at the relocations to find out what objects are
6558 // actually being referred to.
6559 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
6560 uint64_t RelocAddress;
6561 Reloc.getOffset(RelocAddress);
6563 uint32_t EntryIdx = RelocAddress / EntrySize;
6564 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
6565 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
6567 if (OffsetInEntry == 0)
6568 Entry.FunctionReloc = Reloc;
6569 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
6570 Entry.PersonalityReloc = Reloc;
6571 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
6572 Entry.LSDAReloc = Reloc;
6574 llvm_unreachable("Unexpected relocation in __compact_unwind section");
6577 // Finally, we're ready to print the data we've gathered.
6578 outs() << "Contents of __compact_unwind section:\n";
6579 for (auto &Entry : CompactUnwinds) {
6580 outs() << " Entry at offset "
6581 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
6583 // 1. Start of the region this entry applies to.
6584 outs() << " start: " << format("0x%" PRIx64,
6585 Entry.FunctionAddr) << ' ';
6586 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
6589 // 2. Length of the region this entry applies to.
6590 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
6592 // 3. The 32-bit compact encoding.
6593 outs() << " compact encoding: "
6594 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
6596 // 4. The personality function, if present.
6597 if (Entry.PersonalityReloc.getObjectFile()) {
6598 outs() << " personality function: "
6599 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
6600 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
6601 Entry.PersonalityAddr);
6605 // 5. This entry's language-specific data area.
6606 if (Entry.LSDAReloc.getObjectFile()) {
6607 outs() << " LSDA: " << format("0x%" PRIx64,
6608 Entry.LSDAAddr) << ' ';
6609 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
6615 //===----------------------------------------------------------------------===//
6616 // __unwind_info section dumping
6617 //===----------------------------------------------------------------------===//
6619 static void printRegularSecondLevelUnwindPage(const char *PageStart) {
6620 const char *Pos = PageStart;
6621 uint32_t Kind = readNext<uint32_t>(Pos);
6623 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
6625 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6626 uint16_t NumEntries = readNext<uint16_t>(Pos);
6628 Pos = PageStart + EntriesStart;
6629 for (unsigned i = 0; i < NumEntries; ++i) {
6630 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6631 uint32_t Encoding = readNext<uint32_t>(Pos);
6633 outs() << " [" << i << "]: "
6634 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6636 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
6640 static void printCompressedSecondLevelUnwindPage(
6641 const char *PageStart, uint32_t FunctionBase,
6642 const SmallVectorImpl<uint32_t> &CommonEncodings) {
6643 const char *Pos = PageStart;
6644 uint32_t Kind = readNext<uint32_t>(Pos);
6646 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
6648 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6649 uint16_t NumEntries = readNext<uint16_t>(Pos);
6651 uint16_t EncodingsStart = readNext<uint16_t>(Pos);
6652 readNext<uint16_t>(Pos);
6653 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
6654 PageStart + EncodingsStart);
6656 Pos = PageStart + EntriesStart;
6657 for (unsigned i = 0; i < NumEntries; ++i) {
6658 uint32_t Entry = readNext<uint32_t>(Pos);
6659 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
6660 uint32_t EncodingIdx = Entry >> 24;
6663 if (EncodingIdx < CommonEncodings.size())
6664 Encoding = CommonEncodings[EncodingIdx];
6666 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
6668 outs() << " [" << i << "]: "
6669 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6671 << "encoding[" << EncodingIdx
6672 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
6676 static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
6677 std::map<uint64_t, SymbolRef> &Symbols,
6678 const SectionRef &UnwindInfo) {
6680 assert(Obj->isLittleEndian() &&
6681 "There should not be a big-endian .o with __unwind_info");
6683 outs() << "Contents of __unwind_info section:\n";
6686 UnwindInfo.getContents(Contents);
6687 const char *Pos = Contents.data();
6689 //===----------------------------------
6691 //===----------------------------------
6693 uint32_t Version = readNext<uint32_t>(Pos);
6694 outs() << " Version: "
6695 << format("0x%" PRIx32, Version) << '\n';
6696 assert(Version == 1 && "only understand version 1");
6698 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
6699 outs() << " Common encodings array section offset: "
6700 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
6701 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
6702 outs() << " Number of common encodings in array: "
6703 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
6705 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
6706 outs() << " Personality function array section offset: "
6707 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
6708 uint32_t NumPersonalities = readNext<uint32_t>(Pos);
6709 outs() << " Number of personality functions in array: "
6710 << format("0x%" PRIx32, NumPersonalities) << '\n';
6712 uint32_t IndicesStart = readNext<uint32_t>(Pos);
6713 outs() << " Index array section offset: "
6714 << format("0x%" PRIx32, IndicesStart) << '\n';
6715 uint32_t NumIndices = readNext<uint32_t>(Pos);
6716 outs() << " Number of indices in array: "
6717 << format("0x%" PRIx32, NumIndices) << '\n';
6719 //===----------------------------------
6720 // A shared list of common encodings
6721 //===----------------------------------
6723 // These occupy indices in the range [0, N] whenever an encoding is referenced
6724 // from a compressed 2nd level index table. In practice the linker only
6725 // creates ~128 of these, so that indices are available to embed encodings in
6726 // the 2nd level index.
6728 SmallVector<uint32_t, 64> CommonEncodings;
6729 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
6730 Pos = Contents.data() + CommonEncodingsStart;
6731 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
6732 uint32_t Encoding = readNext<uint32_t>(Pos);
6733 CommonEncodings.push_back(Encoding);
6735 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
6739 //===----------------------------------
6740 // Personality functions used in this executable
6741 //===----------------------------------
6743 // There should be only a handful of these (one per source language,
6744 // roughly). Particularly since they only get 2 bits in the compact encoding.
6746 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
6747 Pos = Contents.data() + PersonalitiesStart;
6748 for (unsigned i = 0; i < NumPersonalities; ++i) {
6749 uint32_t PersonalityFn = readNext<uint32_t>(Pos);
6750 outs() << " personality[" << i + 1
6751 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
6754 //===----------------------------------
6755 // The level 1 index entries
6756 //===----------------------------------
6758 // These specify an approximate place to start searching for the more detailed
6759 // information, sorted by PC.
6762 uint32_t FunctionOffset;
6763 uint32_t SecondLevelPageStart;
6767 SmallVector<IndexEntry, 4> IndexEntries;
6769 outs() << " Top level indices: (count = " << NumIndices << ")\n";
6770 Pos = Contents.data() + IndicesStart;
6771 for (unsigned i = 0; i < NumIndices; ++i) {
6774 Entry.FunctionOffset = readNext<uint32_t>(Pos);
6775 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
6776 Entry.LSDAStart = readNext<uint32_t>(Pos);
6777 IndexEntries.push_back(Entry);
6779 outs() << " [" << i << "]: "
6780 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
6782 << "2nd level page offset="
6783 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
6784 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
6787 //===----------------------------------
6788 // Next come the LSDA tables
6789 //===----------------------------------
6791 // The LSDA layout is rather implicit: it's a contiguous array of entries from
6792 // the first top-level index's LSDAOffset to the last (sentinel).
6794 outs() << " LSDA descriptors:\n";
6795 Pos = Contents.data() + IndexEntries[0].LSDAStart;
6796 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
6797 (2 * sizeof(uint32_t));
6798 for (int i = 0; i < NumLSDAs; ++i) {
6799 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6800 uint32_t LSDAOffset = readNext<uint32_t>(Pos);
6801 outs() << " [" << i << "]: "
6802 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6804 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
6807 //===----------------------------------
6808 // Finally, the 2nd level indices
6809 //===----------------------------------
6811 // Generally these are 4K in size, and have 2 possible forms:
6812 // + Regular stores up to 511 entries with disparate encodings
6813 // + Compressed stores up to 1021 entries if few enough compact encoding
6815 outs() << " Second level indices:\n";
6816 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
6817 // The final sentinel top-level index has no associated 2nd level page
6818 if (IndexEntries[i].SecondLevelPageStart == 0)
6821 outs() << " Second level index[" << i << "]: "
6822 << "offset in section="
6823 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
6825 << "base function offset="
6826 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
6828 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
6829 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
6831 printRegularSecondLevelUnwindPage(Pos);
6833 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
6836 llvm_unreachable("Do not know how to print this kind of 2nd level page");
6840 void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
6841 std::map<uint64_t, SymbolRef> Symbols;
6842 for (const SymbolRef &SymRef : Obj->symbols()) {
6843 // Discard any undefined or absolute symbols. They're not going to take part
6844 // in the convenience lookup for unwind info and just take up resources.
6845 section_iterator Section = Obj->section_end();
6846 SymRef.getSection(Section);
6847 if (Section == Obj->section_end())
6851 SymRef.getAddress(Addr);
6852 Symbols.insert(std::make_pair(Addr, SymRef));
6855 for (const SectionRef &Section : Obj->sections()) {
6857 Section.getName(SectName);
6858 if (SectName == "__compact_unwind")
6859 printMachOCompactUnwindSection(Obj, Symbols, Section);
6860 else if (SectName == "__unwind_info")
6861 printMachOUnwindInfoSection(Obj, Symbols, Section);
6862 else if (SectName == "__eh_frame")
6863 outs() << "llvm-objdump: warning: unhandled __eh_frame section\n";
6867 static void PrintMachHeader(uint32_t magic, uint32_t cputype,
6868 uint32_t cpusubtype, uint32_t filetype,
6869 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
6871 outs() << "Mach header\n";
6872 outs() << " magic cputype cpusubtype caps filetype ncmds "
6873 "sizeofcmds flags\n";
6875 if (magic == MachO::MH_MAGIC)
6876 outs() << " MH_MAGIC";
6877 else if (magic == MachO::MH_MAGIC_64)
6878 outs() << "MH_MAGIC_64";
6880 outs() << format(" 0x%08" PRIx32, magic);
6882 case MachO::CPU_TYPE_I386:
6884 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6885 case MachO::CPU_SUBTYPE_I386_ALL:
6889 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6893 case MachO::CPU_TYPE_X86_64:
6894 outs() << " X86_64";
6895 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6896 case MachO::CPU_SUBTYPE_X86_64_ALL:
6899 case MachO::CPU_SUBTYPE_X86_64_H:
6900 outs() << " Haswell";
6903 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6907 case MachO::CPU_TYPE_ARM:
6909 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6910 case MachO::CPU_SUBTYPE_ARM_ALL:
6913 case MachO::CPU_SUBTYPE_ARM_V4T:
6916 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
6919 case MachO::CPU_SUBTYPE_ARM_XSCALE:
6920 outs() << " XSCALE";
6922 case MachO::CPU_SUBTYPE_ARM_V6:
6925 case MachO::CPU_SUBTYPE_ARM_V6M:
6928 case MachO::CPU_SUBTYPE_ARM_V7:
6931 case MachO::CPU_SUBTYPE_ARM_V7EM:
6934 case MachO::CPU_SUBTYPE_ARM_V7K:
6937 case MachO::CPU_SUBTYPE_ARM_V7M:
6940 case MachO::CPU_SUBTYPE_ARM_V7S:
6944 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6948 case MachO::CPU_TYPE_ARM64:
6950 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6951 case MachO::CPU_SUBTYPE_ARM64_ALL:
6955 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6959 case MachO::CPU_TYPE_POWERPC:
6961 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6962 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6966 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6970 case MachO::CPU_TYPE_POWERPC64:
6972 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6973 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6977 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6982 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
6985 outs() << format(" 0x%02" PRIx32,
6986 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
6989 case MachO::MH_OBJECT:
6990 outs() << " OBJECT";
6992 case MachO::MH_EXECUTE:
6993 outs() << " EXECUTE";
6995 case MachO::MH_FVMLIB:
6996 outs() << " FVMLIB";
6998 case MachO::MH_CORE:
7001 case MachO::MH_PRELOAD:
7002 outs() << " PRELOAD";
7004 case MachO::MH_DYLIB:
7007 case MachO::MH_DYLIB_STUB:
7008 outs() << " DYLIB_STUB";
7010 case MachO::MH_DYLINKER:
7011 outs() << " DYLINKER";
7013 case MachO::MH_BUNDLE:
7014 outs() << " BUNDLE";
7016 case MachO::MH_DSYM:
7019 case MachO::MH_KEXT_BUNDLE:
7020 outs() << " KEXTBUNDLE";
7023 outs() << format(" %10u", filetype);
7026 outs() << format(" %5u", ncmds);
7027 outs() << format(" %10u", sizeofcmds);
7029 if (f & MachO::MH_NOUNDEFS) {
7030 outs() << " NOUNDEFS";
7031 f &= ~MachO::MH_NOUNDEFS;
7033 if (f & MachO::MH_INCRLINK) {
7034 outs() << " INCRLINK";
7035 f &= ~MachO::MH_INCRLINK;
7037 if (f & MachO::MH_DYLDLINK) {
7038 outs() << " DYLDLINK";
7039 f &= ~MachO::MH_DYLDLINK;
7041 if (f & MachO::MH_BINDATLOAD) {
7042 outs() << " BINDATLOAD";
7043 f &= ~MachO::MH_BINDATLOAD;
7045 if (f & MachO::MH_PREBOUND) {
7046 outs() << " PREBOUND";
7047 f &= ~MachO::MH_PREBOUND;
7049 if (f & MachO::MH_SPLIT_SEGS) {
7050 outs() << " SPLIT_SEGS";
7051 f &= ~MachO::MH_SPLIT_SEGS;
7053 if (f & MachO::MH_LAZY_INIT) {
7054 outs() << " LAZY_INIT";
7055 f &= ~MachO::MH_LAZY_INIT;
7057 if (f & MachO::MH_TWOLEVEL) {
7058 outs() << " TWOLEVEL";
7059 f &= ~MachO::MH_TWOLEVEL;
7061 if (f & MachO::MH_FORCE_FLAT) {
7062 outs() << " FORCE_FLAT";
7063 f &= ~MachO::MH_FORCE_FLAT;
7065 if (f & MachO::MH_NOMULTIDEFS) {
7066 outs() << " NOMULTIDEFS";
7067 f &= ~MachO::MH_NOMULTIDEFS;
7069 if (f & MachO::MH_NOFIXPREBINDING) {
7070 outs() << " NOFIXPREBINDING";
7071 f &= ~MachO::MH_NOFIXPREBINDING;
7073 if (f & MachO::MH_PREBINDABLE) {
7074 outs() << " PREBINDABLE";
7075 f &= ~MachO::MH_PREBINDABLE;
7077 if (f & MachO::MH_ALLMODSBOUND) {
7078 outs() << " ALLMODSBOUND";
7079 f &= ~MachO::MH_ALLMODSBOUND;
7081 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
7082 outs() << " SUBSECTIONS_VIA_SYMBOLS";
7083 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
7085 if (f & MachO::MH_CANONICAL) {
7086 outs() << " CANONICAL";
7087 f &= ~MachO::MH_CANONICAL;
7089 if (f & MachO::MH_WEAK_DEFINES) {
7090 outs() << " WEAK_DEFINES";
7091 f &= ~MachO::MH_WEAK_DEFINES;
7093 if (f & MachO::MH_BINDS_TO_WEAK) {
7094 outs() << " BINDS_TO_WEAK";
7095 f &= ~MachO::MH_BINDS_TO_WEAK;
7097 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
7098 outs() << " ALLOW_STACK_EXECUTION";
7099 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
7101 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
7102 outs() << " DEAD_STRIPPABLE_DYLIB";
7103 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
7105 if (f & MachO::MH_PIE) {
7107 f &= ~MachO::MH_PIE;
7109 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
7110 outs() << " NO_REEXPORTED_DYLIBS";
7111 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
7113 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
7114 outs() << " MH_HAS_TLV_DESCRIPTORS";
7115 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
7117 if (f & MachO::MH_NO_HEAP_EXECUTION) {
7118 outs() << " MH_NO_HEAP_EXECUTION";
7119 f &= ~MachO::MH_NO_HEAP_EXECUTION;
7121 if (f & MachO::MH_APP_EXTENSION_SAFE) {
7122 outs() << " APP_EXTENSION_SAFE";
7123 f &= ~MachO::MH_APP_EXTENSION_SAFE;
7125 if (f != 0 || flags == 0)
7126 outs() << format(" 0x%08" PRIx32, f);
7128 outs() << format(" 0x%08" PRIx32, magic);
7129 outs() << format(" %7d", cputype);
7130 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7131 outs() << format(" 0x%02" PRIx32,
7132 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7133 outs() << format(" %10u", filetype);
7134 outs() << format(" %5u", ncmds);
7135 outs() << format(" %10u", sizeofcmds);
7136 outs() << format(" 0x%08" PRIx32, flags);
7141 static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
7142 StringRef SegName, uint64_t vmaddr,
7143 uint64_t vmsize, uint64_t fileoff,
7144 uint64_t filesize, uint32_t maxprot,
7145 uint32_t initprot, uint32_t nsects,
7146 uint32_t flags, uint32_t object_size,
7148 uint64_t expected_cmdsize;
7149 if (cmd == MachO::LC_SEGMENT) {
7150 outs() << " cmd LC_SEGMENT\n";
7151 expected_cmdsize = nsects;
7152 expected_cmdsize *= sizeof(struct MachO::section);
7153 expected_cmdsize += sizeof(struct MachO::segment_command);
7155 outs() << " cmd LC_SEGMENT_64\n";
7156 expected_cmdsize = nsects;
7157 expected_cmdsize *= sizeof(struct MachO::section_64);
7158 expected_cmdsize += sizeof(struct MachO::segment_command_64);
7160 outs() << " cmdsize " << cmdsize;
7161 if (cmdsize != expected_cmdsize)
7162 outs() << " Inconsistent size\n";
7165 outs() << " segname " << SegName << "\n";
7166 if (cmd == MachO::LC_SEGMENT_64) {
7167 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
7168 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
7170 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
7171 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
7173 outs() << " fileoff " << fileoff;
7174 if (fileoff > object_size)
7175 outs() << " (past end of file)\n";
7178 outs() << " filesize " << filesize;
7179 if (fileoff + filesize > object_size)
7180 outs() << " (past end of file)\n";
7185 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7186 MachO::VM_PROT_EXECUTE)) != 0)
7187 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
7189 if (maxprot & MachO::VM_PROT_READ)
7190 outs() << " maxprot r";
7192 outs() << " maxprot -";
7193 if (maxprot & MachO::VM_PROT_WRITE)
7197 if (maxprot & MachO::VM_PROT_EXECUTE)
7203 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7204 MachO::VM_PROT_EXECUTE)) != 0)
7205 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
7207 if (initprot & MachO::VM_PROT_READ)
7208 outs() << " initprot r";
7210 outs() << " initprot -";
7211 if (initprot & MachO::VM_PROT_WRITE)
7215 if (initprot & MachO::VM_PROT_EXECUTE)
7221 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
7222 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
7224 outs() << " nsects " << nsects << "\n";
7228 outs() << " (none)\n";
7230 if (flags & MachO::SG_HIGHVM) {
7231 outs() << " HIGHVM";
7232 flags &= ~MachO::SG_HIGHVM;
7234 if (flags & MachO::SG_FVMLIB) {
7235 outs() << " FVMLIB";
7236 flags &= ~MachO::SG_FVMLIB;
7238 if (flags & MachO::SG_NORELOC) {
7239 outs() << " NORELOC";
7240 flags &= ~MachO::SG_NORELOC;
7242 if (flags & MachO::SG_PROTECTED_VERSION_1) {
7243 outs() << " PROTECTED_VERSION_1";
7244 flags &= ~MachO::SG_PROTECTED_VERSION_1;
7247 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
7252 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
7256 static void PrintSection(const char *sectname, const char *segname,
7257 uint64_t addr, uint64_t size, uint32_t offset,
7258 uint32_t align, uint32_t reloff, uint32_t nreloc,
7259 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
7260 uint32_t cmd, const char *sg_segname,
7261 uint32_t filetype, uint32_t object_size,
7263 outs() << "Section\n";
7264 outs() << " sectname " << format("%.16s\n", sectname);
7265 outs() << " segname " << format("%.16s", segname);
7266 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
7267 outs() << " (does not match segment)\n";
7270 if (cmd == MachO::LC_SEGMENT_64) {
7271 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
7272 outs() << " size " << format("0x%016" PRIx64, size);
7274 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
7275 outs() << " size " << format("0x%08" PRIx64, size);
7277 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
7278 outs() << " (past end of file)\n";
7281 outs() << " offset " << offset;
7282 if (offset > object_size)
7283 outs() << " (past end of file)\n";
7286 uint32_t align_shifted = 1 << align;
7287 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
7288 outs() << " reloff " << reloff;
7289 if (reloff > object_size)
7290 outs() << " (past end of file)\n";
7293 outs() << " nreloc " << nreloc;
7294 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
7295 outs() << " (past end of file)\n";
7298 uint32_t section_type = flags & MachO::SECTION_TYPE;
7301 if (section_type == MachO::S_REGULAR)
7302 outs() << " S_REGULAR\n";
7303 else if (section_type == MachO::S_ZEROFILL)
7304 outs() << " S_ZEROFILL\n";
7305 else if (section_type == MachO::S_CSTRING_LITERALS)
7306 outs() << " S_CSTRING_LITERALS\n";
7307 else if (section_type == MachO::S_4BYTE_LITERALS)
7308 outs() << " S_4BYTE_LITERALS\n";
7309 else if (section_type == MachO::S_8BYTE_LITERALS)
7310 outs() << " S_8BYTE_LITERALS\n";
7311 else if (section_type == MachO::S_16BYTE_LITERALS)
7312 outs() << " S_16BYTE_LITERALS\n";
7313 else if (section_type == MachO::S_LITERAL_POINTERS)
7314 outs() << " S_LITERAL_POINTERS\n";
7315 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
7316 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
7317 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
7318 outs() << " S_LAZY_SYMBOL_POINTERS\n";
7319 else if (section_type == MachO::S_SYMBOL_STUBS)
7320 outs() << " S_SYMBOL_STUBS\n";
7321 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
7322 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
7323 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
7324 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
7325 else if (section_type == MachO::S_COALESCED)
7326 outs() << " S_COALESCED\n";
7327 else if (section_type == MachO::S_INTERPOSING)
7328 outs() << " S_INTERPOSING\n";
7329 else if (section_type == MachO::S_DTRACE_DOF)
7330 outs() << " S_DTRACE_DOF\n";
7331 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
7332 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
7333 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
7334 outs() << " S_THREAD_LOCAL_REGULAR\n";
7335 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
7336 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
7337 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
7338 outs() << " S_THREAD_LOCAL_VARIABLES\n";
7339 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7340 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
7341 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
7342 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
7344 outs() << format("0x%08" PRIx32, section_type) << "\n";
7345 outs() << "attributes";
7346 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
7347 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
7348 outs() << " PURE_INSTRUCTIONS";
7349 if (section_attributes & MachO::S_ATTR_NO_TOC)
7350 outs() << " NO_TOC";
7351 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
7352 outs() << " STRIP_STATIC_SYMS";
7353 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
7354 outs() << " NO_DEAD_STRIP";
7355 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
7356 outs() << " LIVE_SUPPORT";
7357 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
7358 outs() << " SELF_MODIFYING_CODE";
7359 if (section_attributes & MachO::S_ATTR_DEBUG)
7361 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
7362 outs() << " SOME_INSTRUCTIONS";
7363 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
7364 outs() << " EXT_RELOC";
7365 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
7366 outs() << " LOC_RELOC";
7367 if (section_attributes == 0)
7368 outs() << " (none)";
7371 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
7372 outs() << " reserved1 " << reserved1;
7373 if (section_type == MachO::S_SYMBOL_STUBS ||
7374 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
7375 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
7376 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
7377 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7378 outs() << " (index into indirect symbol table)\n";
7381 outs() << " reserved2 " << reserved2;
7382 if (section_type == MachO::S_SYMBOL_STUBS)
7383 outs() << " (size of stubs)\n";
7388 static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
7389 uint32_t object_size) {
7390 outs() << " cmd LC_SYMTAB\n";
7391 outs() << " cmdsize " << st.cmdsize;
7392 if (st.cmdsize != sizeof(struct MachO::symtab_command))
7393 outs() << " Incorrect size\n";
7396 outs() << " symoff " << st.symoff;
7397 if (st.symoff > object_size)
7398 outs() << " (past end of file)\n";
7401 outs() << " nsyms " << st.nsyms;
7404 big_size = st.nsyms;
7405 big_size *= sizeof(struct MachO::nlist_64);
7406 big_size += st.symoff;
7407 if (big_size > object_size)
7408 outs() << " (past end of file)\n";
7412 big_size = st.nsyms;
7413 big_size *= sizeof(struct MachO::nlist);
7414 big_size += st.symoff;
7415 if (big_size > object_size)
7416 outs() << " (past end of file)\n";
7420 outs() << " stroff " << st.stroff;
7421 if (st.stroff > object_size)
7422 outs() << " (past end of file)\n";
7425 outs() << " strsize " << st.strsize;
7426 big_size = st.stroff;
7427 big_size += st.strsize;
7428 if (big_size > object_size)
7429 outs() << " (past end of file)\n";
7434 static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
7435 uint32_t nsyms, uint32_t object_size,
7437 outs() << " cmd LC_DYSYMTAB\n";
7438 outs() << " cmdsize " << dyst.cmdsize;
7439 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
7440 outs() << " Incorrect size\n";
7443 outs() << " ilocalsym " << dyst.ilocalsym;
7444 if (dyst.ilocalsym > nsyms)
7445 outs() << " (greater than the number of symbols)\n";
7448 outs() << " nlocalsym " << dyst.nlocalsym;
7450 big_size = dyst.ilocalsym;
7451 big_size += dyst.nlocalsym;
7452 if (big_size > nsyms)
7453 outs() << " (past the end of the symbol table)\n";
7456 outs() << " iextdefsym " << dyst.iextdefsym;
7457 if (dyst.iextdefsym > nsyms)
7458 outs() << " (greater than the number of symbols)\n";
7461 outs() << " nextdefsym " << dyst.nextdefsym;
7462 big_size = dyst.iextdefsym;
7463 big_size += dyst.nextdefsym;
7464 if (big_size > nsyms)
7465 outs() << " (past the end of the symbol table)\n";
7468 outs() << " iundefsym " << dyst.iundefsym;
7469 if (dyst.iundefsym > nsyms)
7470 outs() << " (greater than the number of symbols)\n";
7473 outs() << " nundefsym " << dyst.nundefsym;
7474 big_size = dyst.iundefsym;
7475 big_size += dyst.nundefsym;
7476 if (big_size > nsyms)
7477 outs() << " (past the end of the symbol table)\n";
7480 outs() << " tocoff " << dyst.tocoff;
7481 if (dyst.tocoff > object_size)
7482 outs() << " (past end of file)\n";
7485 outs() << " ntoc " << dyst.ntoc;
7486 big_size = dyst.ntoc;
7487 big_size *= sizeof(struct MachO::dylib_table_of_contents);
7488 big_size += dyst.tocoff;
7489 if (big_size > object_size)
7490 outs() << " (past end of file)\n";
7493 outs() << " modtaboff " << dyst.modtaboff;
7494 if (dyst.modtaboff > object_size)
7495 outs() << " (past end of file)\n";
7498 outs() << " nmodtab " << dyst.nmodtab;
7501 modtabend = dyst.nmodtab;
7502 modtabend *= sizeof(struct MachO::dylib_module_64);
7503 modtabend += dyst.modtaboff;
7505 modtabend = dyst.nmodtab;
7506 modtabend *= sizeof(struct MachO::dylib_module);
7507 modtabend += dyst.modtaboff;
7509 if (modtabend > object_size)
7510 outs() << " (past end of file)\n";
7513 outs() << " extrefsymoff " << dyst.extrefsymoff;
7514 if (dyst.extrefsymoff > object_size)
7515 outs() << " (past end of file)\n";
7518 outs() << " nextrefsyms " << dyst.nextrefsyms;
7519 big_size = dyst.nextrefsyms;
7520 big_size *= sizeof(struct MachO::dylib_reference);
7521 big_size += dyst.extrefsymoff;
7522 if (big_size > object_size)
7523 outs() << " (past end of file)\n";
7526 outs() << " indirectsymoff " << dyst.indirectsymoff;
7527 if (dyst.indirectsymoff > object_size)
7528 outs() << " (past end of file)\n";
7531 outs() << " nindirectsyms " << dyst.nindirectsyms;
7532 big_size = dyst.nindirectsyms;
7533 big_size *= sizeof(uint32_t);
7534 big_size += dyst.indirectsymoff;
7535 if (big_size > object_size)
7536 outs() << " (past end of file)\n";
7539 outs() << " extreloff " << dyst.extreloff;
7540 if (dyst.extreloff > object_size)
7541 outs() << " (past end of file)\n";
7544 outs() << " nextrel " << dyst.nextrel;
7545 big_size = dyst.nextrel;
7546 big_size *= sizeof(struct MachO::relocation_info);
7547 big_size += dyst.extreloff;
7548 if (big_size > object_size)
7549 outs() << " (past end of file)\n";
7552 outs() << " locreloff " << dyst.locreloff;
7553 if (dyst.locreloff > object_size)
7554 outs() << " (past end of file)\n";
7557 outs() << " nlocrel " << dyst.nlocrel;
7558 big_size = dyst.nlocrel;
7559 big_size *= sizeof(struct MachO::relocation_info);
7560 big_size += dyst.locreloff;
7561 if (big_size > object_size)
7562 outs() << " (past end of file)\n";
7567 static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
7568 uint32_t object_size) {
7569 if (dc.cmd == MachO::LC_DYLD_INFO)
7570 outs() << " cmd LC_DYLD_INFO\n";
7572 outs() << " cmd LC_DYLD_INFO_ONLY\n";
7573 outs() << " cmdsize " << dc.cmdsize;
7574 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
7575 outs() << " Incorrect size\n";
7578 outs() << " rebase_off " << dc.rebase_off;
7579 if (dc.rebase_off > object_size)
7580 outs() << " (past end of file)\n";
7583 outs() << " rebase_size " << dc.rebase_size;
7585 big_size = dc.rebase_off;
7586 big_size += dc.rebase_size;
7587 if (big_size > object_size)
7588 outs() << " (past end of file)\n";
7591 outs() << " bind_off " << dc.bind_off;
7592 if (dc.bind_off > object_size)
7593 outs() << " (past end of file)\n";
7596 outs() << " bind_size " << dc.bind_size;
7597 big_size = dc.bind_off;
7598 big_size += dc.bind_size;
7599 if (big_size > object_size)
7600 outs() << " (past end of file)\n";
7603 outs() << " weak_bind_off " << dc.weak_bind_off;
7604 if (dc.weak_bind_off > object_size)
7605 outs() << " (past end of file)\n";
7608 outs() << " weak_bind_size " << dc.weak_bind_size;
7609 big_size = dc.weak_bind_off;
7610 big_size += dc.weak_bind_size;
7611 if (big_size > object_size)
7612 outs() << " (past end of file)\n";
7615 outs() << " lazy_bind_off " << dc.lazy_bind_off;
7616 if (dc.lazy_bind_off > object_size)
7617 outs() << " (past end of file)\n";
7620 outs() << " lazy_bind_size " << dc.lazy_bind_size;
7621 big_size = dc.lazy_bind_off;
7622 big_size += dc.lazy_bind_size;
7623 if (big_size > object_size)
7624 outs() << " (past end of file)\n";
7627 outs() << " export_off " << dc.export_off;
7628 if (dc.export_off > object_size)
7629 outs() << " (past end of file)\n";
7632 outs() << " export_size " << dc.export_size;
7633 big_size = dc.export_off;
7634 big_size += dc.export_size;
7635 if (big_size > object_size)
7636 outs() << " (past end of file)\n";
7641 static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
7643 if (dyld.cmd == MachO::LC_ID_DYLINKER)
7644 outs() << " cmd LC_ID_DYLINKER\n";
7645 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
7646 outs() << " cmd LC_LOAD_DYLINKER\n";
7647 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
7648 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
7650 outs() << " cmd ?(" << dyld.cmd << ")\n";
7651 outs() << " cmdsize " << dyld.cmdsize;
7652 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
7653 outs() << " Incorrect size\n";
7656 if (dyld.name >= dyld.cmdsize)
7657 outs() << " name ?(bad offset " << dyld.name << ")\n";
7659 const char *P = (const char *)(Ptr) + dyld.name;
7660 outs() << " name " << P << " (offset " << dyld.name << ")\n";
7664 static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
7665 outs() << " cmd LC_UUID\n";
7666 outs() << " cmdsize " << uuid.cmdsize;
7667 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
7668 outs() << " Incorrect size\n";
7672 outs() << format("%02" PRIX32, uuid.uuid[0]);
7673 outs() << format("%02" PRIX32, uuid.uuid[1]);
7674 outs() << format("%02" PRIX32, uuid.uuid[2]);
7675 outs() << format("%02" PRIX32, uuid.uuid[3]);
7677 outs() << format("%02" PRIX32, uuid.uuid[4]);
7678 outs() << format("%02" PRIX32, uuid.uuid[5]);
7680 outs() << format("%02" PRIX32, uuid.uuid[6]);
7681 outs() << format("%02" PRIX32, uuid.uuid[7]);
7683 outs() << format("%02" PRIX32, uuid.uuid[8]);
7684 outs() << format("%02" PRIX32, uuid.uuid[9]);
7686 outs() << format("%02" PRIX32, uuid.uuid[10]);
7687 outs() << format("%02" PRIX32, uuid.uuid[11]);
7688 outs() << format("%02" PRIX32, uuid.uuid[12]);
7689 outs() << format("%02" PRIX32, uuid.uuid[13]);
7690 outs() << format("%02" PRIX32, uuid.uuid[14]);
7691 outs() << format("%02" PRIX32, uuid.uuid[15]);
7695 static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
7696 outs() << " cmd LC_RPATH\n";
7697 outs() << " cmdsize " << rpath.cmdsize;
7698 if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
7699 outs() << " Incorrect size\n";
7702 if (rpath.path >= rpath.cmdsize)
7703 outs() << " path ?(bad offset " << rpath.path << ")\n";
7705 const char *P = (const char *)(Ptr) + rpath.path;
7706 outs() << " path " << P << " (offset " << rpath.path << ")\n";
7710 static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
7711 if (vd.cmd == MachO::LC_VERSION_MIN_MACOSX)
7712 outs() << " cmd LC_VERSION_MIN_MACOSX\n";
7713 else if (vd.cmd == MachO::LC_VERSION_MIN_IPHONEOS)
7714 outs() << " cmd LC_VERSION_MIN_IPHONEOS\n";
7716 outs() << " cmd " << vd.cmd << " (?)\n";
7717 outs() << " cmdsize " << vd.cmdsize;
7718 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
7719 outs() << " Incorrect size\n";
7722 outs() << " version " << ((vd.version >> 16) & 0xffff) << "."
7723 << ((vd.version >> 8) & 0xff);
7724 if ((vd.version & 0xff) != 0)
7725 outs() << "." << (vd.version & 0xff);
7728 outs() << " sdk n/a";
7730 outs() << " sdk " << ((vd.sdk >> 16) & 0xffff) << "."
7731 << ((vd.sdk >> 8) & 0xff);
7733 if ((vd.sdk & 0xff) != 0)
7734 outs() << "." << (vd.sdk & 0xff);
7738 static void PrintSourceVersionCommand(MachO::source_version_command sd) {
7739 outs() << " cmd LC_SOURCE_VERSION\n";
7740 outs() << " cmdsize " << sd.cmdsize;
7741 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
7742 outs() << " Incorrect size\n";
7745 uint64_t a = (sd.version >> 40) & 0xffffff;
7746 uint64_t b = (sd.version >> 30) & 0x3ff;
7747 uint64_t c = (sd.version >> 20) & 0x3ff;
7748 uint64_t d = (sd.version >> 10) & 0x3ff;
7749 uint64_t e = sd.version & 0x3ff;
7750 outs() << " version " << a << "." << b;
7752 outs() << "." << c << "." << d << "." << e;
7754 outs() << "." << c << "." << d;
7760 static void PrintEntryPointCommand(MachO::entry_point_command ep) {
7761 outs() << " cmd LC_MAIN\n";
7762 outs() << " cmdsize " << ep.cmdsize;
7763 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
7764 outs() << " Incorrect size\n";
7767 outs() << " entryoff " << ep.entryoff << "\n";
7768 outs() << " stacksize " << ep.stacksize << "\n";
7771 static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
7772 uint32_t object_size) {
7773 outs() << " cmd LC_ENCRYPTION_INFO\n";
7774 outs() << " cmdsize " << ec.cmdsize;
7775 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
7776 outs() << " Incorrect size\n";
7779 outs() << " cryptoff " << ec.cryptoff;
7780 if (ec.cryptoff > object_size)
7781 outs() << " (past end of file)\n";
7784 outs() << " cryptsize " << ec.cryptsize;
7785 if (ec.cryptsize > object_size)
7786 outs() << " (past end of file)\n";
7789 outs() << " cryptid " << ec.cryptid << "\n";
7792 static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
7793 uint32_t object_size) {
7794 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
7795 outs() << " cmdsize " << ec.cmdsize;
7796 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
7797 outs() << " Incorrect size\n";
7800 outs() << " cryptoff " << ec.cryptoff;
7801 if (ec.cryptoff > object_size)
7802 outs() << " (past end of file)\n";
7805 outs() << " cryptsize " << ec.cryptsize;
7806 if (ec.cryptsize > object_size)
7807 outs() << " (past end of file)\n";
7810 outs() << " cryptid " << ec.cryptid << "\n";
7811 outs() << " pad " << ec.pad << "\n";
7814 static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
7816 outs() << " cmd LC_LINKER_OPTION\n";
7817 outs() << " cmdsize " << lo.cmdsize;
7818 if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
7819 outs() << " Incorrect size\n";
7822 outs() << " count " << lo.count << "\n";
7823 const char *string = Ptr + sizeof(struct MachO::linker_option_command);
7824 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
7827 while (*string == '\0' && left > 0) {
7833 outs() << " string #" << i << " " << format("%.*s\n", left, string);
7834 uint32_t NullPos = StringRef(string, left).find('\0');
7835 uint32_t len = std::min(NullPos, left) + 1;
7841 outs() << " count " << lo.count << " does not match number of strings "
7845 static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
7847 outs() << " cmd LC_SUB_FRAMEWORK\n";
7848 outs() << " cmdsize " << sub.cmdsize;
7849 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
7850 outs() << " Incorrect size\n";
7853 if (sub.umbrella < sub.cmdsize) {
7854 const char *P = Ptr + sub.umbrella;
7855 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
7857 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
7861 static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
7863 outs() << " cmd LC_SUB_UMBRELLA\n";
7864 outs() << " cmdsize " << sub.cmdsize;
7865 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
7866 outs() << " Incorrect size\n";
7869 if (sub.sub_umbrella < sub.cmdsize) {
7870 const char *P = Ptr + sub.sub_umbrella;
7871 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
7873 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
7877 static void PrintSubLibraryCommand(MachO::sub_library_command sub,
7879 outs() << " cmd LC_SUB_LIBRARY\n";
7880 outs() << " cmdsize " << sub.cmdsize;
7881 if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
7882 outs() << " Incorrect size\n";
7885 if (sub.sub_library < sub.cmdsize) {
7886 const char *P = Ptr + sub.sub_library;
7887 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
7889 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
7893 static void PrintSubClientCommand(MachO::sub_client_command sub,
7895 outs() << " cmd LC_SUB_CLIENT\n";
7896 outs() << " cmdsize " << sub.cmdsize;
7897 if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
7898 outs() << " Incorrect size\n";
7901 if (sub.client < sub.cmdsize) {
7902 const char *P = Ptr + sub.client;
7903 outs() << " client " << P << " (offset " << sub.client << ")\n";
7905 outs() << " client ?(bad offset " << sub.client << ")\n";
7909 static void PrintRoutinesCommand(MachO::routines_command r) {
7910 outs() << " cmd LC_ROUTINES\n";
7911 outs() << " cmdsize " << r.cmdsize;
7912 if (r.cmdsize != sizeof(struct MachO::routines_command))
7913 outs() << " Incorrect size\n";
7916 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
7917 outs() << " init_module " << r.init_module << "\n";
7918 outs() << " reserved1 " << r.reserved1 << "\n";
7919 outs() << " reserved2 " << r.reserved2 << "\n";
7920 outs() << " reserved3 " << r.reserved3 << "\n";
7921 outs() << " reserved4 " << r.reserved4 << "\n";
7922 outs() << " reserved5 " << r.reserved5 << "\n";
7923 outs() << " reserved6 " << r.reserved6 << "\n";
7926 static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
7927 outs() << " cmd LC_ROUTINES_64\n";
7928 outs() << " cmdsize " << r.cmdsize;
7929 if (r.cmdsize != sizeof(struct MachO::routines_command_64))
7930 outs() << " Incorrect size\n";
7933 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
7934 outs() << " init_module " << r.init_module << "\n";
7935 outs() << " reserved1 " << r.reserved1 << "\n";
7936 outs() << " reserved2 " << r.reserved2 << "\n";
7937 outs() << " reserved3 " << r.reserved3 << "\n";
7938 outs() << " reserved4 " << r.reserved4 << "\n";
7939 outs() << " reserved5 " << r.reserved5 << "\n";
7940 outs() << " reserved6 " << r.reserved6 << "\n";
7943 static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
7944 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
7945 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
7946 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
7947 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
7948 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
7949 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
7950 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
7951 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
7952 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
7953 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
7954 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
7955 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
7956 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
7957 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
7958 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
7959 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
7960 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
7961 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
7962 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
7963 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
7964 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
7967 static void Print_mmst_reg(MachO::mmst_reg_t &r) {
7969 outs() << "\t mmst_reg ";
7970 for (f = 0; f < 10; f++)
7971 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
7973 outs() << "\t mmst_rsrv ";
7974 for (f = 0; f < 6; f++)
7975 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
7979 static void Print_xmm_reg(MachO::xmm_reg_t &r) {
7981 outs() << "\t xmm_reg ";
7982 for (f = 0; f < 16; f++)
7983 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
7987 static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
7988 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
7989 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
7990 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
7991 outs() << " denorm " << fpu.fpu_fcw.denorm;
7992 outs() << " zdiv " << fpu.fpu_fcw.zdiv;
7993 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
7994 outs() << " undfl " << fpu.fpu_fcw.undfl;
7995 outs() << " precis " << fpu.fpu_fcw.precis << "\n";
7996 outs() << "\t\t pc ";
7997 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
7998 outs() << "FP_PREC_24B ";
7999 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
8000 outs() << "FP_PREC_53B ";
8001 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
8002 outs() << "FP_PREC_64B ";
8004 outs() << fpu.fpu_fcw.pc << " ";
8006 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
8007 outs() << "FP_RND_NEAR ";
8008 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
8009 outs() << "FP_RND_DOWN ";
8010 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
8011 outs() << "FP_RND_UP ";
8012 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
8013 outs() << "FP_CHOP ";
8015 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
8016 outs() << " denorm " << fpu.fpu_fsw.denorm;
8017 outs() << " zdiv " << fpu.fpu_fsw.zdiv;
8018 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
8019 outs() << " undfl " << fpu.fpu_fsw.undfl;
8020 outs() << " precis " << fpu.fpu_fsw.precis;
8021 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
8022 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
8023 outs() << " c0 " << fpu.fpu_fsw.c0;
8024 outs() << " c1 " << fpu.fpu_fsw.c1;
8025 outs() << " c2 " << fpu.fpu_fsw.c2;
8026 outs() << " tos " << fpu.fpu_fsw.tos;
8027 outs() << " c3 " << fpu.fpu_fsw.c3;
8028 outs() << " busy " << fpu.fpu_fsw.busy << "\n";
8029 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
8030 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
8031 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
8032 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
8033 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
8034 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
8035 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
8036 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
8037 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
8038 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
8039 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
8041 outs() << "\t fpu_stmm0:\n";
8042 Print_mmst_reg(fpu.fpu_stmm0);
8043 outs() << "\t fpu_stmm1:\n";
8044 Print_mmst_reg(fpu.fpu_stmm1);
8045 outs() << "\t fpu_stmm2:\n";
8046 Print_mmst_reg(fpu.fpu_stmm2);
8047 outs() << "\t fpu_stmm3:\n";
8048 Print_mmst_reg(fpu.fpu_stmm3);
8049 outs() << "\t fpu_stmm4:\n";
8050 Print_mmst_reg(fpu.fpu_stmm4);
8051 outs() << "\t fpu_stmm5:\n";
8052 Print_mmst_reg(fpu.fpu_stmm5);
8053 outs() << "\t fpu_stmm6:\n";
8054 Print_mmst_reg(fpu.fpu_stmm6);
8055 outs() << "\t fpu_stmm7:\n";
8056 Print_mmst_reg(fpu.fpu_stmm7);
8057 outs() << "\t fpu_xmm0:\n";
8058 Print_xmm_reg(fpu.fpu_xmm0);
8059 outs() << "\t fpu_xmm1:\n";
8060 Print_xmm_reg(fpu.fpu_xmm1);
8061 outs() << "\t fpu_xmm2:\n";
8062 Print_xmm_reg(fpu.fpu_xmm2);
8063 outs() << "\t fpu_xmm3:\n";
8064 Print_xmm_reg(fpu.fpu_xmm3);
8065 outs() << "\t fpu_xmm4:\n";
8066 Print_xmm_reg(fpu.fpu_xmm4);
8067 outs() << "\t fpu_xmm5:\n";
8068 Print_xmm_reg(fpu.fpu_xmm5);
8069 outs() << "\t fpu_xmm6:\n";
8070 Print_xmm_reg(fpu.fpu_xmm6);
8071 outs() << "\t fpu_xmm7:\n";
8072 Print_xmm_reg(fpu.fpu_xmm7);
8073 outs() << "\t fpu_xmm8:\n";
8074 Print_xmm_reg(fpu.fpu_xmm8);
8075 outs() << "\t fpu_xmm9:\n";
8076 Print_xmm_reg(fpu.fpu_xmm9);
8077 outs() << "\t fpu_xmm10:\n";
8078 Print_xmm_reg(fpu.fpu_xmm10);
8079 outs() << "\t fpu_xmm11:\n";
8080 Print_xmm_reg(fpu.fpu_xmm11);
8081 outs() << "\t fpu_xmm12:\n";
8082 Print_xmm_reg(fpu.fpu_xmm12);
8083 outs() << "\t fpu_xmm13:\n";
8084 Print_xmm_reg(fpu.fpu_xmm13);
8085 outs() << "\t fpu_xmm14:\n";
8086 Print_xmm_reg(fpu.fpu_xmm14);
8087 outs() << "\t fpu_xmm15:\n";
8088 Print_xmm_reg(fpu.fpu_xmm15);
8089 outs() << "\t fpu_rsrv4:\n";
8090 for (uint32_t f = 0; f < 6; f++) {
8092 for (uint32_t g = 0; g < 16; g++)
8093 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
8096 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
8100 static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
8101 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
8102 outs() << " err " << format("0x%08" PRIx32, exc64.err);
8103 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
8106 static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
8107 bool isLittleEndian, uint32_t cputype) {
8108 if (t.cmd == MachO::LC_THREAD)
8109 outs() << " cmd LC_THREAD\n";
8110 else if (t.cmd == MachO::LC_UNIXTHREAD)
8111 outs() << " cmd LC_UNIXTHREAD\n";
8113 outs() << " cmd " << t.cmd << " (unknown)\n";
8114 outs() << " cmdsize " << t.cmdsize;
8115 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
8116 outs() << " Incorrect size\n";
8120 const char *begin = Ptr + sizeof(struct MachO::thread_command);
8121 const char *end = Ptr + t.cmdsize;
8122 uint32_t flavor, count, left;
8123 if (cputype == MachO::CPU_TYPE_X86_64) {
8124 while (begin < end) {
8125 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8126 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8127 begin += sizeof(uint32_t);
8132 if (isLittleEndian != sys::IsLittleEndianHost)
8133 sys::swapByteOrder(flavor);
8134 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8135 memcpy((char *)&count, begin, sizeof(uint32_t));
8136 begin += sizeof(uint32_t);
8141 if (isLittleEndian != sys::IsLittleEndianHost)
8142 sys::swapByteOrder(count);
8143 if (flavor == MachO::x86_THREAD_STATE64) {
8144 outs() << " flavor x86_THREAD_STATE64\n";
8145 if (count == MachO::x86_THREAD_STATE64_COUNT)
8146 outs() << " count x86_THREAD_STATE64_COUNT\n";
8148 outs() << " count " << count
8149 << " (not x86_THREAD_STATE64_COUNT)\n";
8150 MachO::x86_thread_state64_t cpu64;
8152 if (left >= sizeof(MachO::x86_thread_state64_t)) {
8153 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
8154 begin += sizeof(MachO::x86_thread_state64_t);
8156 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
8157 memcpy(&cpu64, begin, left);
8160 if (isLittleEndian != sys::IsLittleEndianHost)
8162 Print_x86_thread_state64_t(cpu64);
8163 } else if (flavor == MachO::x86_THREAD_STATE) {
8164 outs() << " flavor x86_THREAD_STATE\n";
8165 if (count == MachO::x86_THREAD_STATE_COUNT)
8166 outs() << " count x86_THREAD_STATE_COUNT\n";
8168 outs() << " count " << count
8169 << " (not x86_THREAD_STATE_COUNT)\n";
8170 struct MachO::x86_thread_state_t ts;
8172 if (left >= sizeof(MachO::x86_thread_state_t)) {
8173 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
8174 begin += sizeof(MachO::x86_thread_state_t);
8176 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
8177 memcpy(&ts, begin, left);
8180 if (isLittleEndian != sys::IsLittleEndianHost)
8182 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
8183 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
8184 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
8185 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
8187 outs() << "tsh.count " << ts.tsh.count
8188 << " (not x86_THREAD_STATE64_COUNT\n";
8189 Print_x86_thread_state64_t(ts.uts.ts64);
8191 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
8192 << ts.tsh.count << "\n";
8194 } else if (flavor == MachO::x86_FLOAT_STATE) {
8195 outs() << " flavor x86_FLOAT_STATE\n";
8196 if (count == MachO::x86_FLOAT_STATE_COUNT)
8197 outs() << " count x86_FLOAT_STATE_COUNT\n";
8199 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
8200 struct MachO::x86_float_state_t fs;
8202 if (left >= sizeof(MachO::x86_float_state_t)) {
8203 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
8204 begin += sizeof(MachO::x86_float_state_t);
8206 memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
8207 memcpy(&fs, begin, left);
8210 if (isLittleEndian != sys::IsLittleEndianHost)
8212 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
8213 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
8214 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
8215 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
8217 outs() << "fsh.count " << fs.fsh.count
8218 << " (not x86_FLOAT_STATE64_COUNT\n";
8219 Print_x86_float_state_t(fs.ufs.fs64);
8221 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
8222 << fs.fsh.count << "\n";
8224 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
8225 outs() << " flavor x86_EXCEPTION_STATE\n";
8226 if (count == MachO::x86_EXCEPTION_STATE_COUNT)
8227 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
8229 outs() << " count " << count
8230 << " (not x86_EXCEPTION_STATE_COUNT)\n";
8231 struct MachO::x86_exception_state_t es;
8233 if (left >= sizeof(MachO::x86_exception_state_t)) {
8234 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
8235 begin += sizeof(MachO::x86_exception_state_t);
8237 memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
8238 memcpy(&es, begin, left);
8241 if (isLittleEndian != sys::IsLittleEndianHost)
8243 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
8244 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
8245 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
8246 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
8248 outs() << "\t esh.count " << es.esh.count
8249 << " (not x86_EXCEPTION_STATE64_COUNT\n";
8250 Print_x86_exception_state_t(es.ues.es64);
8252 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
8253 << es.esh.count << "\n";
8256 outs() << " flavor " << flavor << " (unknown)\n";
8257 outs() << " count " << count << "\n";
8258 outs() << " state (unknown)\n";
8259 begin += count * sizeof(uint32_t);
8263 while (begin < end) {
8264 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8265 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8266 begin += sizeof(uint32_t);
8271 if (isLittleEndian != sys::IsLittleEndianHost)
8272 sys::swapByteOrder(flavor);
8273 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8274 memcpy((char *)&count, begin, sizeof(uint32_t));
8275 begin += sizeof(uint32_t);
8280 if (isLittleEndian != sys::IsLittleEndianHost)
8281 sys::swapByteOrder(count);
8282 outs() << " flavor " << flavor << "\n";
8283 outs() << " count " << count << "\n";
8284 outs() << " state (Unknown cputype/cpusubtype)\n";
8285 begin += count * sizeof(uint32_t);
8290 static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
8291 if (dl.cmd == MachO::LC_ID_DYLIB)
8292 outs() << " cmd LC_ID_DYLIB\n";
8293 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
8294 outs() << " cmd LC_LOAD_DYLIB\n";
8295 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
8296 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
8297 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
8298 outs() << " cmd LC_REEXPORT_DYLIB\n";
8299 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
8300 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
8301 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
8302 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
8304 outs() << " cmd " << dl.cmd << " (unknown)\n";
8305 outs() << " cmdsize " << dl.cmdsize;
8306 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
8307 outs() << " Incorrect size\n";
8310 if (dl.dylib.name < dl.cmdsize) {
8311 const char *P = (const char *)(Ptr) + dl.dylib.name;
8312 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
8314 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
8316 outs() << " time stamp " << dl.dylib.timestamp << " ";
8317 time_t t = dl.dylib.timestamp;
8318 outs() << ctime(&t);
8319 outs() << " current version ";
8320 if (dl.dylib.current_version == 0xffffffff)
8323 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
8324 << ((dl.dylib.current_version >> 8) & 0xff) << "."
8325 << (dl.dylib.current_version & 0xff) << "\n";
8326 outs() << "compatibility version ";
8327 if (dl.dylib.compatibility_version == 0xffffffff)
8330 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
8331 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
8332 << (dl.dylib.compatibility_version & 0xff) << "\n";
8335 static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
8336 uint32_t object_size) {
8337 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
8338 outs() << " cmd LC_FUNCTION_STARTS\n";
8339 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
8340 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
8341 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
8342 outs() << " cmd LC_FUNCTION_STARTS\n";
8343 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
8344 outs() << " cmd LC_DATA_IN_CODE\n";
8345 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
8346 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
8347 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
8348 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
8350 outs() << " cmd " << ld.cmd << " (?)\n";
8351 outs() << " cmdsize " << ld.cmdsize;
8352 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
8353 outs() << " Incorrect size\n";
8356 outs() << " dataoff " << ld.dataoff;
8357 if (ld.dataoff > object_size)
8358 outs() << " (past end of file)\n";
8361 outs() << " datasize " << ld.datasize;
8362 uint64_t big_size = ld.dataoff;
8363 big_size += ld.datasize;
8364 if (big_size > object_size)
8365 outs() << " (past end of file)\n";
8370 static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t ncmds,
8371 uint32_t filetype, uint32_t cputype,
8375 StringRef Buf = Obj->getData();
8376 MachOObjectFile::LoadCommandInfo Command = Obj->getFirstLoadCommandInfo();
8377 for (unsigned i = 0;; ++i) {
8378 outs() << "Load command " << i << "\n";
8379 if (Command.C.cmd == MachO::LC_SEGMENT) {
8380 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
8381 const char *sg_segname = SLC.segname;
8382 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
8383 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
8384 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
8386 for (unsigned j = 0; j < SLC.nsects; j++) {
8387 MachO::section S = Obj->getSection(Command, j);
8388 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
8389 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
8390 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
8392 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
8393 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
8394 const char *sg_segname = SLC_64.segname;
8395 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
8396 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
8397 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
8398 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
8399 for (unsigned j = 0; j < SLC_64.nsects; j++) {
8400 MachO::section_64 S_64 = Obj->getSection64(Command, j);
8401 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
8402 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
8403 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
8404 sg_segname, filetype, Buf.size(), verbose);
8406 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
8407 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
8408 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
8409 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
8410 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
8411 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
8412 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
8414 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
8415 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
8416 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
8417 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
8418 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
8419 Command.C.cmd == MachO::LC_ID_DYLINKER ||
8420 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
8421 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
8422 PrintDyldLoadCommand(Dyld, Command.Ptr);
8423 } else if (Command.C.cmd == MachO::LC_UUID) {
8424 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
8425 PrintUuidLoadCommand(Uuid);
8426 } else if (Command.C.cmd == MachO::LC_RPATH) {
8427 MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
8428 PrintRpathLoadCommand(Rpath, Command.Ptr);
8429 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
8430 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS) {
8431 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
8432 PrintVersionMinLoadCommand(Vd);
8433 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
8434 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
8435 PrintSourceVersionCommand(Sd);
8436 } else if (Command.C.cmd == MachO::LC_MAIN) {
8437 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
8438 PrintEntryPointCommand(Ep);
8439 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
8440 MachO::encryption_info_command Ei =
8441 Obj->getEncryptionInfoCommand(Command);
8442 PrintEncryptionInfoCommand(Ei, Buf.size());
8443 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
8444 MachO::encryption_info_command_64 Ei =
8445 Obj->getEncryptionInfoCommand64(Command);
8446 PrintEncryptionInfoCommand64(Ei, Buf.size());
8447 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
8448 MachO::linker_option_command Lo =
8449 Obj->getLinkerOptionLoadCommand(Command);
8450 PrintLinkerOptionCommand(Lo, Command.Ptr);
8451 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
8452 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
8453 PrintSubFrameworkCommand(Sf, Command.Ptr);
8454 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
8455 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
8456 PrintSubUmbrellaCommand(Sf, Command.Ptr);
8457 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
8458 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
8459 PrintSubLibraryCommand(Sl, Command.Ptr);
8460 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
8461 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
8462 PrintSubClientCommand(Sc, Command.Ptr);
8463 } else if (Command.C.cmd == MachO::LC_ROUTINES) {
8464 MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
8465 PrintRoutinesCommand(Rc);
8466 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
8467 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
8468 PrintRoutinesCommand64(Rc);
8469 } else if (Command.C.cmd == MachO::LC_THREAD ||
8470 Command.C.cmd == MachO::LC_UNIXTHREAD) {
8471 MachO::thread_command Tc = Obj->getThreadCommand(Command);
8472 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
8473 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
8474 Command.C.cmd == MachO::LC_ID_DYLIB ||
8475 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
8476 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
8477 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
8478 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
8479 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
8480 PrintDylibCommand(Dl, Command.Ptr);
8481 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
8482 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
8483 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
8484 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
8485 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
8486 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
8487 MachO::linkedit_data_command Ld =
8488 Obj->getLinkeditDataLoadCommand(Command);
8489 PrintLinkEditDataCommand(Ld, Buf.size());
8491 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
8493 outs() << " cmdsize " << Command.C.cmdsize << "\n";
8494 // TODO: get and print the raw bytes of the load command.
8496 // TODO: print all the other kinds of load commands.
8500 Command = Obj->getNextLoadCommandInfo(Command);
8504 static void getAndPrintMachHeader(const MachOObjectFile *Obj, uint32_t &ncmds,
8505 uint32_t &filetype, uint32_t &cputype,
8507 if (Obj->is64Bit()) {
8508 MachO::mach_header_64 H_64;
8509 H_64 = Obj->getHeader64();
8510 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
8511 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
8513 filetype = H_64.filetype;
8514 cputype = H_64.cputype;
8516 MachO::mach_header H;
8517 H = Obj->getHeader();
8518 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
8519 H.sizeofcmds, H.flags, verbose);
8521 filetype = H.filetype;
8522 cputype = H.cputype;
8526 void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
8527 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
8529 uint32_t filetype = 0;
8530 uint32_t cputype = 0;
8531 getAndPrintMachHeader(file, ncmds, filetype, cputype, !NonVerbose);
8532 PrintLoadCommands(file, ncmds, filetype, cputype, !NonVerbose);
8535 //===----------------------------------------------------------------------===//
8536 // export trie dumping
8537 //===----------------------------------------------------------------------===//
8539 void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
8540 for (const llvm::object::ExportEntry &Entry : Obj->exports()) {
8541 uint64_t Flags = Entry.flags();
8542 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
8543 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
8544 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8545 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
8546 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8547 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
8548 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
8550 outs() << "[re-export] ";
8552 outs() << format("0x%08llX ",
8553 Entry.address()); // FIXME:add in base address
8554 outs() << Entry.name();
8555 if (WeakDef || ThreadLocal || Resolver || Abs) {
8556 bool NeedsComma = false;
8559 outs() << "weak_def";
8565 outs() << "per-thread";
8571 outs() << "absolute";
8577 outs() << format("resolver=0x%08llX", Entry.other());
8583 StringRef DylibName = "unknown";
8584 int Ordinal = Entry.other() - 1;
8585 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
8586 if (Entry.otherName().empty())
8587 outs() << " (from " << DylibName << ")";
8589 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
8595 //===----------------------------------------------------------------------===//
8596 // rebase table dumping
8597 //===----------------------------------------------------------------------===//
8602 SegInfo(const object::MachOObjectFile *Obj);
8604 StringRef segmentName(uint32_t SegIndex);
8605 StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset);
8606 uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
8609 struct SectionInfo {
8612 StringRef SectionName;
8613 StringRef SegmentName;
8614 uint64_t OffsetInSegment;
8615 uint64_t SegmentStartAddress;
8616 uint32_t SegmentIndex;
8618 const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset);
8619 SmallVector<SectionInfo, 32> Sections;
8623 SegInfo::SegInfo(const object::MachOObjectFile *Obj) {
8624 // Build table of sections so segIndex/offset pairs can be translated.
8625 uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
8626 StringRef CurSegName;
8627 uint64_t CurSegAddress;
8628 for (const SectionRef &Section : Obj->sections()) {
8630 if (error(Section.getName(Info.SectionName)))
8632 Info.Address = Section.getAddress();
8633 Info.Size = Section.getSize();
8635 Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
8636 if (!Info.SegmentName.equals(CurSegName)) {
8638 CurSegName = Info.SegmentName;
8639 CurSegAddress = Info.Address;
8641 Info.SegmentIndex = CurSegIndex - 1;
8642 Info.OffsetInSegment = Info.Address - CurSegAddress;
8643 Info.SegmentStartAddress = CurSegAddress;
8644 Sections.push_back(Info);
8648 StringRef SegInfo::segmentName(uint32_t SegIndex) {
8649 for (const SectionInfo &SI : Sections) {
8650 if (SI.SegmentIndex == SegIndex)
8651 return SI.SegmentName;
8653 llvm_unreachable("invalid segIndex");
8656 const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex,
8657 uint64_t OffsetInSeg) {
8658 for (const SectionInfo &SI : Sections) {
8659 if (SI.SegmentIndex != SegIndex)
8661 if (SI.OffsetInSegment > OffsetInSeg)
8663 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
8667 llvm_unreachable("segIndex and offset not in any section");
8670 StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) {
8671 return findSection(SegIndex, OffsetInSeg).SectionName;
8674 uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) {
8675 const SectionInfo &SI = findSection(SegIndex, OffsetInSeg);
8676 return SI.SegmentStartAddress + OffsetInSeg;
8679 void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) {
8680 // Build table of sections so names can used in final output.
8681 SegInfo sectionTable(Obj);
8683 outs() << "segment section address type\n";
8684 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) {
8685 uint32_t SegIndex = Entry.segmentIndex();
8686 uint64_t OffsetInSeg = Entry.segmentOffset();
8687 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8688 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8689 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8691 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
8692 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
8693 SegmentName.str().c_str(), SectionName.str().c_str(),
8694 Address, Entry.typeName().str().c_str());
8698 static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
8699 StringRef DylibName;
8701 case MachO::BIND_SPECIAL_DYLIB_SELF:
8702 return "this-image";
8703 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
8704 return "main-executable";
8705 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
8706 return "flat-namespace";
8709 std::error_code EC =
8710 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
8712 return "<<bad library ordinal>>";
8716 return "<<unknown special ordinal>>";
8719 //===----------------------------------------------------------------------===//
8720 // bind table dumping
8721 //===----------------------------------------------------------------------===//
8723 void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) {
8724 // Build table of sections so names can used in final output.
8725 SegInfo sectionTable(Obj);
8727 outs() << "segment section address type "
8728 "addend dylib symbol\n";
8729 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) {
8730 uint32_t SegIndex = Entry.segmentIndex();
8731 uint64_t OffsetInSeg = Entry.segmentOffset();
8732 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8733 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8734 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8736 // Table lines look like:
8737 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
8739 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
8740 Attr = " (weak_import)";
8741 outs() << left_justify(SegmentName, 8) << " "
8742 << left_justify(SectionName, 18) << " "
8743 << format_hex(Address, 10, true) << " "
8744 << left_justify(Entry.typeName(), 8) << " "
8745 << format_decimal(Entry.addend(), 8) << " "
8746 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
8747 << Entry.symbolName() << Attr << "\n";
8751 //===----------------------------------------------------------------------===//
8752 // lazy bind table dumping
8753 //===----------------------------------------------------------------------===//
8755 void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) {
8756 // Build table of sections so names can used in final output.
8757 SegInfo sectionTable(Obj);
8759 outs() << "segment section address "
8761 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) {
8762 uint32_t SegIndex = Entry.segmentIndex();
8763 uint64_t OffsetInSeg = Entry.segmentOffset();
8764 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8765 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8766 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8768 // Table lines look like:
8769 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
8770 outs() << left_justify(SegmentName, 8) << " "
8771 << left_justify(SectionName, 18) << " "
8772 << format_hex(Address, 10, true) << " "
8773 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
8774 << Entry.symbolName() << "\n";
8778 //===----------------------------------------------------------------------===//
8779 // weak bind table dumping
8780 //===----------------------------------------------------------------------===//
8782 void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) {
8783 // Build table of sections so names can used in final output.
8784 SegInfo sectionTable(Obj);
8786 outs() << "segment section address "
8787 "type addend symbol\n";
8788 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) {
8789 // Strong symbols don't have a location to update.
8790 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
8791 outs() << " strong "
8792 << Entry.symbolName() << "\n";
8795 uint32_t SegIndex = Entry.segmentIndex();
8796 uint64_t OffsetInSeg = Entry.segmentOffset();
8797 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8798 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8799 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8801 // Table lines look like:
8802 // __DATA __data 0x00001000 pointer 0 _foo
8803 outs() << left_justify(SegmentName, 8) << " "
8804 << left_justify(SectionName, 18) << " "
8805 << format_hex(Address, 10, true) << " "
8806 << left_justify(Entry.typeName(), 8) << " "
8807 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
8812 // get_dyld_bind_info_symbolname() is used for disassembly and passed an
8813 // address, ReferenceValue, in the Mach-O file and looks in the dyld bind
8814 // information for that address. If the address is found its binding symbol
8815 // name is returned. If not nullptr is returned.
8816 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
8817 struct DisassembleInfo *info) {
8818 if (info->bindtable == nullptr) {
8819 info->bindtable = new (BindTable);
8820 SegInfo sectionTable(info->O);
8821 for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable()) {
8822 uint32_t SegIndex = Entry.segmentIndex();
8823 uint64_t OffsetInSeg = Entry.segmentOffset();
8824 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8825 const char *SymbolName = nullptr;
8826 StringRef name = Entry.symbolName();
8828 SymbolName = name.data();
8829 info->bindtable->push_back(std::make_pair(Address, SymbolName));
8832 for (bind_table_iterator BI = info->bindtable->begin(),
8833 BE = info->bindtable->end();
8835 uint64_t Address = BI->first;
8836 if (ReferenceValue == Address) {
8837 const char *SymbolName = BI->second;