1 //===-- MachODump.cpp - Object file dumping utility for llvm --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the MachO-specific dumper for llvm-objdump.
12 //===----------------------------------------------------------------------===//
14 #include "llvm-objdump.h"
15 #include "llvm-c/Disassembler.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/Config/config.h"
20 #include "llvm/DebugInfo/DWARF/DIContext.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCDisassembler.h"
24 #include "llvm/MC/MCInst.h"
25 #include "llvm/MC/MCInstPrinter.h"
26 #include "llvm/MC/MCInstrDesc.h"
27 #include "llvm/MC/MCInstrInfo.h"
28 #include "llvm/MC/MCRegisterInfo.h"
29 #include "llvm/MC/MCSubtargetInfo.h"
30 #include "llvm/Object/MachO.h"
31 #include "llvm/Object/MachOUniversal.h"
32 #include "llvm/Support/Casting.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/Endian.h"
36 #include "llvm/Support/Format.h"
37 #include "llvm/Support/FormattedStream.h"
38 #include "llvm/Support/GraphWriter.h"
39 #include "llvm/Support/LEB128.h"
40 #include "llvm/Support/MachO.h"
41 #include "llvm/Support/MemoryBuffer.h"
42 #include "llvm/Support/TargetRegistry.h"
43 #include "llvm/Support/TargetSelect.h"
44 #include "llvm/Support/raw_ostream.h"
47 #include <system_error>
54 using namespace object;
58 cl::desc("Print line information from debug info if available"));
60 static cl::opt<std::string> DSYMFile("dsym",
61 cl::desc("Use .dSYM file for debug info"));
63 static cl::opt<bool> FullLeadingAddr("full-leading-addr",
64 cl::desc("Print full leading address"));
66 static cl::opt<bool> NoLeadingAddr("no-leading-addr",
67 cl::desc("Print no leading address"));
70 PrintImmHex("print-imm-hex",
71 cl::desc("Use hex format for immediate values"));
73 cl::opt<bool> llvm::UniversalHeaders("universal-headers",
74 cl::desc("Print Mach-O universal headers "
75 "(requires -macho)"));
78 llvm::ArchiveHeaders("archive-headers",
79 cl::desc("Print archive headers for Mach-O archives "
80 "(requires -macho)"));
83 llvm::IndirectSymbols("indirect-symbols",
84 cl::desc("Print indirect symbol table for Mach-O "
85 "objects (requires -macho)"));
88 llvm::DataInCode("data-in-code",
89 cl::desc("Print the data in code table for Mach-O objects "
90 "(requires -macho)"));
93 llvm::LinkOptHints("link-opt-hints",
94 cl::desc("Print the linker optimization hints for "
95 "Mach-O objects (requires -macho)"));
98 llvm::DumpSections("section",
99 cl::desc("Prints the specified segment,section for "
100 "Mach-O objects (requires -macho)"));
102 cl::opt<bool> llvm::Raw("raw",
103 cl::desc("Have -section dump the raw binary contents"));
106 llvm::InfoPlist("info-plist",
107 cl::desc("Print the info plist section as strings for "
108 "Mach-O objects (requires -macho)"));
111 llvm::DylibsUsed("dylibs-used",
112 cl::desc("Print the shared libraries used for linked "
113 "Mach-O files (requires -macho)"));
116 llvm::DylibId("dylib-id",
117 cl::desc("Print the shared library's id for the dylib Mach-O "
118 "file (requires -macho)"));
121 llvm::NonVerbose("non-verbose",
122 cl::desc("Print the info for Mach-O objects in "
123 "non-verbose or numeric form (requires -macho)"));
126 llvm::ObjcMetaData("objc-meta-data",
127 cl::desc("Print the Objective-C runtime meta data for "
128 "Mach-O files (requires -macho)"));
130 cl::opt<std::string> llvm::DisSymName(
132 cl::desc("disassemble just this symbol's instructions (requires -macho"));
134 static cl::opt<bool> NoSymbolicOperands(
135 "no-symbolic-operands",
136 cl::desc("do not symbolic operands when disassembling (requires -macho)"));
138 static cl::list<std::string>
139 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
141 bool ArchAll = false;
143 static std::string ThumbTripleName;
145 static const Target *GetTarget(const MachOObjectFile *MachOObj,
146 const char **McpuDefault,
147 const Target **ThumbTarget) {
148 // Figure out the target triple.
149 if (TripleName.empty()) {
150 llvm::Triple TT("unknown-unknown-unknown");
151 llvm::Triple ThumbTriple = Triple();
152 TT = MachOObj->getArch(McpuDefault, &ThumbTriple);
153 TripleName = TT.str();
154 ThumbTripleName = ThumbTriple.str();
157 // Get the target specific parser.
159 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
160 if (TheTarget && ThumbTripleName.empty())
163 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
167 errs() << "llvm-objdump: error: unable to get target for '";
169 errs() << TripleName;
171 errs() << ThumbTripleName;
172 errs() << "', see --version and --triple.\n";
176 struct SymbolSorter {
177 bool operator()(const SymbolRef &A, const SymbolRef &B) {
178 SymbolRef::Type AType, BType;
182 uint64_t AAddr, BAddr;
183 if (AType != SymbolRef::ST_Function)
187 if (BType != SymbolRef::ST_Function)
191 return AAddr < BAddr;
195 // Types for the storted data in code table that is built before disassembly
196 // and the predicate function to sort them.
197 typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
198 typedef std::vector<DiceTableEntry> DiceTable;
199 typedef DiceTable::iterator dice_table_iterator;
201 // This is used to search for a data in code table entry for the PC being
202 // disassembled. The j parameter has the PC in j.first. A single data in code
203 // table entry can cover many bytes for each of its Kind's. So if the offset,
204 // aka the i.first value, of the data in code table entry plus its Length
205 // covers the PC being searched for this will return true. If not it will
207 static bool compareDiceTableEntries(const DiceTableEntry &i,
208 const DiceTableEntry &j) {
210 i.second.getLength(Length);
212 return j.first >= i.first && j.first < i.first + Length;
215 static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
216 unsigned short Kind) {
217 uint32_t Value, Size = 1;
221 case MachO::DICE_KIND_DATA:
224 DumpBytes(ArrayRef<uint8_t>(bytes, 4));
225 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
226 outs() << "\t.long " << Value;
228 } else if (Length >= 2) {
230 DumpBytes(ArrayRef<uint8_t>(bytes, 2));
231 Value = bytes[1] << 8 | bytes[0];
232 outs() << "\t.short " << Value;
236 DumpBytes(ArrayRef<uint8_t>(bytes, 2));
238 outs() << "\t.byte " << Value;
241 if (Kind == MachO::DICE_KIND_DATA)
242 outs() << "\t@ KIND_DATA\n";
244 outs() << "\t@ data in code kind = " << Kind << "\n";
246 case MachO::DICE_KIND_JUMP_TABLE8:
248 DumpBytes(ArrayRef<uint8_t>(bytes, 1));
250 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
253 case MachO::DICE_KIND_JUMP_TABLE16:
255 DumpBytes(ArrayRef<uint8_t>(bytes, 2));
256 Value = bytes[1] << 8 | bytes[0];
257 outs() << "\t.short " << format("%5u", Value & 0xffff)
258 << "\t@ KIND_JUMP_TABLE16\n";
261 case MachO::DICE_KIND_JUMP_TABLE32:
262 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
264 DumpBytes(ArrayRef<uint8_t>(bytes, 4));
265 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
266 outs() << "\t.long " << Value;
267 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
268 outs() << "\t@ KIND_JUMP_TABLE32\n";
270 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
277 static void getSectionsAndSymbols(const MachO::mach_header Header,
278 MachOObjectFile *MachOObj,
279 std::vector<SectionRef> &Sections,
280 std::vector<SymbolRef> &Symbols,
281 SmallVectorImpl<uint64_t> &FoundFns,
282 uint64_t &BaseSegmentAddress) {
283 for (const SymbolRef &Symbol : MachOObj->symbols()) {
285 Symbol.getName(SymName);
286 if (!SymName.startswith("ltmp"))
287 Symbols.push_back(Symbol);
290 for (const SectionRef &Section : MachOObj->sections()) {
292 Section.getName(SectName);
293 Sections.push_back(Section);
296 MachOObjectFile::LoadCommandInfo Command =
297 MachOObj->getFirstLoadCommandInfo();
298 bool BaseSegmentAddressSet = false;
299 for (unsigned i = 0;; ++i) {
300 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
301 // We found a function starts segment, parse the addresses for later
303 MachO::linkedit_data_command LLC =
304 MachOObj->getLinkeditDataLoadCommand(Command);
306 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
307 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
308 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
309 StringRef SegName = SLC.segname;
310 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
311 BaseSegmentAddressSet = true;
312 BaseSegmentAddress = SLC.vmaddr;
316 if (i == Header.ncmds - 1)
319 Command = MachOObj->getNextLoadCommandInfo(Command);
323 static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
324 uint32_t n, uint32_t count,
325 uint32_t stride, uint64_t addr) {
326 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
327 uint32_t nindirectsyms = Dysymtab.nindirectsyms;
328 if (n > nindirectsyms)
329 outs() << " (entries start past the end of the indirect symbol "
330 "table) (reserved1 field greater than the table size)";
331 else if (n + count > nindirectsyms)
332 outs() << " (entries extends past the end of the indirect symbol "
335 uint32_t cputype = O->getHeader().cputype;
336 if (cputype & MachO::CPU_ARCH_ABI64)
337 outs() << "address index";
339 outs() << "address index";
344 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
345 if (cputype & MachO::CPU_ARCH_ABI64)
346 outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
348 outs() << format("0x%08" PRIx32, addr + j * stride) << " ";
349 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
350 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
351 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
355 if (indirect_symbol ==
356 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
357 outs() << "LOCAL ABSOLUTE\n";
360 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
361 outs() << "ABSOLUTE\n";
364 outs() << format("%5u ", indirect_symbol);
366 MachO::symtab_command Symtab = O->getSymtabLoadCommand();
367 if (indirect_symbol < Symtab.nsyms) {
368 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
369 SymbolRef Symbol = *Sym;
371 Symbol.getName(SymName);
381 static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
382 uint32_t LoadCommandCount = O->getHeader().ncmds;
383 MachOObjectFile::LoadCommandInfo Load = O->getFirstLoadCommandInfo();
384 for (unsigned I = 0;; ++I) {
385 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
386 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
387 for (unsigned J = 0; J < Seg.nsects; ++J) {
388 MachO::section_64 Sec = O->getSection64(Load, J);
389 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
390 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
391 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
392 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
393 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
394 section_type == MachO::S_SYMBOL_STUBS) {
396 if (section_type == MachO::S_SYMBOL_STUBS)
397 stride = Sec.reserved2;
401 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
402 << Sec.sectname << ") "
403 << "(size of stubs in reserved2 field is zero)\n";
406 uint32_t count = Sec.size / stride;
407 outs() << "Indirect symbols for (" << Sec.segname << ","
408 << Sec.sectname << ") " << count << " entries";
409 uint32_t n = Sec.reserved1;
410 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
413 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
414 MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
415 for (unsigned J = 0; J < Seg.nsects; ++J) {
416 MachO::section Sec = O->getSection(Load, J);
417 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
418 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
419 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
420 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
421 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
422 section_type == MachO::S_SYMBOL_STUBS) {
424 if (section_type == MachO::S_SYMBOL_STUBS)
425 stride = Sec.reserved2;
429 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
430 << Sec.sectname << ") "
431 << "(size of stubs in reserved2 field is zero)\n";
434 uint32_t count = Sec.size / stride;
435 outs() << "Indirect symbols for (" << Sec.segname << ","
436 << Sec.sectname << ") " << count << " entries";
437 uint32_t n = Sec.reserved1;
438 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
442 if (I == LoadCommandCount - 1)
445 Load = O->getNextLoadCommandInfo(Load);
449 static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
450 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
451 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
452 outs() << "Data in code table (" << nentries << " entries)\n";
453 outs() << "offset length kind\n";
454 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
457 DI->getOffset(Offset);
458 outs() << format("0x%08" PRIx32, Offset) << " ";
460 DI->getLength(Length);
461 outs() << format("%6u", Length) << " ";
466 case MachO::DICE_KIND_DATA:
469 case MachO::DICE_KIND_JUMP_TABLE8:
470 outs() << "JUMP_TABLE8";
472 case MachO::DICE_KIND_JUMP_TABLE16:
473 outs() << "JUMP_TABLE16";
475 case MachO::DICE_KIND_JUMP_TABLE32:
476 outs() << "JUMP_TABLE32";
478 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
479 outs() << "ABS_JUMP_TABLE32";
482 outs() << format("0x%04" PRIx32, Kind);
486 outs() << format("0x%04" PRIx32, Kind);
491 static void PrintLinkOptHints(MachOObjectFile *O) {
492 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
493 const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
494 uint32_t nloh = LohLC.datasize;
495 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
496 for (uint32_t i = 0; i < nloh;) {
498 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
500 outs() << " identifier " << identifier << " ";
503 switch (identifier) {
505 outs() << "AdrpAdrp\n";
508 outs() << "AdrpLdr\n";
511 outs() << "AdrpAddLdr\n";
514 outs() << "AdrpLdrGotLdr\n";
517 outs() << "AdrpAddStr\n";
520 outs() << "AdrpLdrGotStr\n";
523 outs() << "AdrpAdd\n";
526 outs() << "AdrpLdrGot\n";
529 outs() << "Unknown identifier value\n";
532 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
534 outs() << " narguments " << narguments << "\n";
538 for (uint32_t j = 0; j < narguments; j++) {
539 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
541 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
548 static void PrintDylibs(MachOObjectFile *O, bool JustId) {
549 uint32_t LoadCommandCount = O->getHeader().ncmds;
550 MachOObjectFile::LoadCommandInfo Load = O->getFirstLoadCommandInfo();
551 for (unsigned I = 0;; ++I) {
552 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
553 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
554 Load.C.cmd == MachO::LC_LOAD_DYLIB ||
555 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
556 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
557 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
558 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
559 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
560 if (dl.dylib.name < dl.cmdsize) {
561 const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
566 outs() << " (compatibility version "
567 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
568 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
569 << (dl.dylib.compatibility_version & 0xff) << ",";
570 outs() << " current version "
571 << ((dl.dylib.current_version >> 16) & 0xffff) << "."
572 << ((dl.dylib.current_version >> 8) & 0xff) << "."
573 << (dl.dylib.current_version & 0xff) << ")\n";
576 outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
577 if (Load.C.cmd == MachO::LC_ID_DYLIB)
578 outs() << "LC_ID_DYLIB ";
579 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
580 outs() << "LC_LOAD_DYLIB ";
581 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
582 outs() << "LC_LOAD_WEAK_DYLIB ";
583 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
584 outs() << "LC_LAZY_LOAD_DYLIB ";
585 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
586 outs() << "LC_REEXPORT_DYLIB ";
587 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
588 outs() << "LC_LOAD_UPWARD_DYLIB ";
591 outs() << "command " << I << "\n";
594 if (I == LoadCommandCount - 1)
597 Load = O->getNextLoadCommandInfo(Load);
601 typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
603 static void CreateSymbolAddressMap(MachOObjectFile *O,
604 SymbolAddressMap *AddrMap) {
605 // Create a map of symbol addresses to symbol names.
606 for (const SymbolRef &Symbol : O->symbols()) {
609 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
610 ST == SymbolRef::ST_Other) {
612 Symbol.getAddress(Address);
614 Symbol.getName(SymName);
615 if (!SymName.startswith(".objc"))
616 (*AddrMap)[Address] = SymName;
621 // GuessSymbolName is passed the address of what might be a symbol and a
622 // pointer to the SymbolAddressMap. It returns the name of a symbol
623 // with that address or nullptr if no symbol is found with that address.
624 static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
625 const char *SymbolName = nullptr;
626 // A DenseMap can't lookup up some values.
627 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
628 StringRef name = AddrMap->lookup(value);
630 SymbolName = name.data();
635 static void DumpCstringChar(const char c) {
639 outs().write_escaped(p);
642 static void DumpCstringSection(MachOObjectFile *O, const char *sect,
643 uint32_t sect_size, uint64_t sect_addr,
644 bool print_addresses) {
645 for (uint32_t i = 0; i < sect_size; i++) {
646 if (print_addresses) {
648 outs() << format("%016" PRIx64, sect_addr + i) << " ";
650 outs() << format("%08" PRIx64, sect_addr + i) << " ";
652 for (; i < sect_size && sect[i] != '\0'; i++)
653 DumpCstringChar(sect[i]);
654 if (i < sect_size && sect[i] == '\0')
659 static void DumpLiteral4(uint32_t l, float f) {
660 outs() << format("0x%08" PRIx32, l);
661 if ((l & 0x7f800000) != 0x7f800000)
662 outs() << format(" (%.16e)\n", f);
665 outs() << " (+Infinity)\n";
666 else if (l == 0xff800000)
667 outs() << " (-Infinity)\n";
668 else if ((l & 0x00400000) == 0x00400000)
669 outs() << " (non-signaling Not-a-Number)\n";
671 outs() << " (signaling Not-a-Number)\n";
675 static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
676 uint32_t sect_size, uint64_t sect_addr,
677 bool print_addresses) {
678 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
679 if (print_addresses) {
681 outs() << format("%016" PRIx64, sect_addr + i) << " ";
683 outs() << format("%08" PRIx64, sect_addr + i) << " ";
686 memcpy(&f, sect + i, sizeof(float));
687 if (O->isLittleEndian() != sys::IsLittleEndianHost)
688 sys::swapByteOrder(f);
690 memcpy(&l, sect + i, sizeof(uint32_t));
691 if (O->isLittleEndian() != sys::IsLittleEndianHost)
692 sys::swapByteOrder(l);
697 static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
699 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
701 if (O->isLittleEndian()) {
708 // Hi is the high word, so this is equivalent to if(isfinite(d))
709 if ((Hi & 0x7ff00000) != 0x7ff00000)
710 outs() << format(" (%.16e)\n", d);
712 if (Hi == 0x7ff00000 && Lo == 0)
713 outs() << " (+Infinity)\n";
714 else if (Hi == 0xfff00000 && Lo == 0)
715 outs() << " (-Infinity)\n";
716 else if ((Hi & 0x00080000) == 0x00080000)
717 outs() << " (non-signaling Not-a-Number)\n";
719 outs() << " (signaling Not-a-Number)\n";
723 static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
724 uint32_t sect_size, uint64_t sect_addr,
725 bool print_addresses) {
726 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
727 if (print_addresses) {
729 outs() << format("%016" PRIx64, sect_addr + i) << " ";
731 outs() << format("%08" PRIx64, sect_addr + i) << " ";
734 memcpy(&d, sect + i, sizeof(double));
735 if (O->isLittleEndian() != sys::IsLittleEndianHost)
736 sys::swapByteOrder(d);
738 memcpy(&l0, sect + i, sizeof(uint32_t));
739 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
740 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
741 sys::swapByteOrder(l0);
742 sys::swapByteOrder(l1);
744 DumpLiteral8(O, l0, l1, d);
748 static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
749 outs() << format("0x%08" PRIx32, l0) << " ";
750 outs() << format("0x%08" PRIx32, l1) << " ";
751 outs() << format("0x%08" PRIx32, l2) << " ";
752 outs() << format("0x%08" PRIx32, l3) << "\n";
755 static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
756 uint32_t sect_size, uint64_t sect_addr,
757 bool print_addresses) {
758 for (uint32_t i = 0; i < sect_size; i += 16) {
759 if (print_addresses) {
761 outs() << format("%016" PRIx64, sect_addr + i) << " ";
763 outs() << format("%08" PRIx64, sect_addr + i) << " ";
765 uint32_t l0, l1, l2, l3;
766 memcpy(&l0, sect + i, sizeof(uint32_t));
767 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
768 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
769 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
770 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
771 sys::swapByteOrder(l0);
772 sys::swapByteOrder(l1);
773 sys::swapByteOrder(l2);
774 sys::swapByteOrder(l3);
776 DumpLiteral16(l0, l1, l2, l3);
780 static void DumpLiteralPointerSection(MachOObjectFile *O,
781 const SectionRef &Section,
782 const char *sect, uint32_t sect_size,
784 bool print_addresses) {
785 // Collect the literal sections in this Mach-O file.
786 std::vector<SectionRef> LiteralSections;
787 for (const SectionRef &Section : O->sections()) {
788 DataRefImpl Ref = Section.getRawDataRefImpl();
789 uint32_t section_type;
791 const MachO::section_64 Sec = O->getSection64(Ref);
792 section_type = Sec.flags & MachO::SECTION_TYPE;
794 const MachO::section Sec = O->getSection(Ref);
795 section_type = Sec.flags & MachO::SECTION_TYPE;
797 if (section_type == MachO::S_CSTRING_LITERALS ||
798 section_type == MachO::S_4BYTE_LITERALS ||
799 section_type == MachO::S_8BYTE_LITERALS ||
800 section_type == MachO::S_16BYTE_LITERALS)
801 LiteralSections.push_back(Section);
804 // Set the size of the literal pointer.
805 uint32_t lp_size = O->is64Bit() ? 8 : 4;
807 // Collect the external relocation symbols for the the literal pointers.
808 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
809 for (const RelocationRef &Reloc : Section.relocations()) {
811 MachO::any_relocation_info RE;
812 bool isExtern = false;
813 Rel = Reloc.getRawDataRefImpl();
814 RE = O->getRelocation(Rel);
815 isExtern = O->getPlainRelocationExternal(RE);
817 uint64_t RelocOffset;
818 Reloc.getOffset(RelocOffset);
819 symbol_iterator RelocSym = Reloc.getSymbol();
820 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
823 array_pod_sort(Relocs.begin(), Relocs.end());
825 // Dump each literal pointer.
826 for (uint32_t i = 0; i < sect_size; i += lp_size) {
827 if (print_addresses) {
829 outs() << format("%016" PRIx64, sect_addr + i) << " ";
831 outs() << format("%08" PRIx64, sect_addr + i) << " ";
835 memcpy(&lp, sect + i, sizeof(uint64_t));
836 if (O->isLittleEndian() != sys::IsLittleEndianHost)
837 sys::swapByteOrder(lp);
840 memcpy(&li, sect + i, sizeof(uint32_t));
841 if (O->isLittleEndian() != sys::IsLittleEndianHost)
842 sys::swapByteOrder(li);
846 // First look for an external relocation entry for this literal pointer.
847 auto Reloc = std::find_if(
848 Relocs.begin(), Relocs.end(),
849 [&](const std::pair<uint64_t, SymbolRef> &P) { return P.first == i; });
850 if (Reloc != Relocs.end()) {
851 symbol_iterator RelocSym = Reloc->second;
853 RelocSym->getName(SymName);
854 outs() << "external relocation entry for symbol:" << SymName << "\n";
858 // For local references see what the section the literal pointer points to.
859 auto Sect = std::find_if(LiteralSections.begin(), LiteralSections.end(),
860 [&](const SectionRef &R) {
861 return lp >= R.getAddress() &&
862 lp < R.getAddress() + R.getSize();
864 if (Sect == LiteralSections.end()) {
865 outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
869 uint64_t SectAddress = Sect->getAddress();
870 uint64_t SectSize = Sect->getSize();
873 Sect->getName(SectName);
874 DataRefImpl Ref = Sect->getRawDataRefImpl();
875 StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
876 outs() << SegmentName << ":" << SectName << ":";
878 uint32_t section_type;
880 const MachO::section_64 Sec = O->getSection64(Ref);
881 section_type = Sec.flags & MachO::SECTION_TYPE;
883 const MachO::section Sec = O->getSection(Ref);
884 section_type = Sec.flags & MachO::SECTION_TYPE;
888 Sect->getContents(BytesStr);
889 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
891 switch (section_type) {
892 case MachO::S_CSTRING_LITERALS:
893 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
895 DumpCstringChar(Contents[i]);
899 case MachO::S_4BYTE_LITERALS:
901 memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
903 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
904 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
905 sys::swapByteOrder(f);
906 sys::swapByteOrder(l);
910 case MachO::S_8BYTE_LITERALS: {
912 memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
914 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
915 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
917 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
918 sys::swapByteOrder(f);
919 sys::swapByteOrder(l0);
920 sys::swapByteOrder(l1);
922 DumpLiteral8(O, l0, l1, d);
925 case MachO::S_16BYTE_LITERALS: {
926 uint32_t l0, l1, l2, l3;
927 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
928 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
930 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
932 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
934 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
935 sys::swapByteOrder(l0);
936 sys::swapByteOrder(l1);
937 sys::swapByteOrder(l2);
938 sys::swapByteOrder(l3);
940 DumpLiteral16(l0, l1, l2, l3);
947 static void DumpInitTermPointerSection(MachOObjectFile *O, const char *sect,
948 uint32_t sect_size, uint64_t sect_addr,
949 SymbolAddressMap *AddrMap,
953 stride = sizeof(uint64_t);
955 stride = sizeof(uint32_t);
956 for (uint32_t i = 0; i < sect_size; i += stride) {
957 const char *SymbolName = nullptr;
959 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
960 uint64_t pointer_value;
961 memcpy(&pointer_value, sect + i, stride);
962 if (O->isLittleEndian() != sys::IsLittleEndianHost)
963 sys::swapByteOrder(pointer_value);
964 outs() << format("0x%016" PRIx64, pointer_value);
966 SymbolName = GuessSymbolName(pointer_value, AddrMap);
968 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
969 uint32_t pointer_value;
970 memcpy(&pointer_value, sect + i, stride);
971 if (O->isLittleEndian() != sys::IsLittleEndianHost)
972 sys::swapByteOrder(pointer_value);
973 outs() << format("0x%08" PRIx32, pointer_value);
975 SymbolName = GuessSymbolName(pointer_value, AddrMap);
978 outs() << " " << SymbolName;
983 static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
984 uint32_t size, uint64_t addr) {
985 uint32_t cputype = O->getHeader().cputype;
986 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
988 for (uint32_t i = 0; i < size; i += j, addr += j) {
990 outs() << format("%016" PRIx64, addr) << "\t";
992 outs() << format("%08" PRIx64, addr) << "\t";
993 for (j = 0; j < 16 && i + j < size; j++) {
994 uint8_t byte_word = *(sect + i + j);
995 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
1001 for (uint32_t i = 0; i < size; i += j, addr += j) {
1003 outs() << format("%016" PRIx64, addr) << "\t";
1005 outs() << format("%08" PRIx64, sect) << "\t";
1006 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
1007 j += sizeof(int32_t)) {
1008 if (i + j + sizeof(int32_t) < size) {
1010 memcpy(&long_word, sect + i + j, sizeof(int32_t));
1011 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1012 sys::swapByteOrder(long_word);
1013 outs() << format("%08" PRIx32, long_word) << " ";
1015 for (uint32_t k = 0; i + j + k < size; k++) {
1016 uint8_t byte_word = *(sect + i + j);
1017 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
1026 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
1027 StringRef DisSegName, StringRef DisSectName);
1028 static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
1029 uint32_t size, uint32_t addr);
1031 static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
1033 SymbolAddressMap AddrMap;
1035 CreateSymbolAddressMap(O, &AddrMap);
1037 for (unsigned i = 0; i < DumpSections.size(); ++i) {
1038 StringRef DumpSection = DumpSections[i];
1039 std::pair<StringRef, StringRef> DumpSegSectName;
1040 DumpSegSectName = DumpSection.split(',');
1041 StringRef DumpSegName, DumpSectName;
1042 if (DumpSegSectName.second.size()) {
1043 DumpSegName = DumpSegSectName.first;
1044 DumpSectName = DumpSegSectName.second;
1047 DumpSectName = DumpSegSectName.first;
1049 for (const SectionRef &Section : O->sections()) {
1051 Section.getName(SectName);
1052 DataRefImpl Ref = Section.getRawDataRefImpl();
1053 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1054 if ((DumpSegName.empty() || SegName == DumpSegName) &&
1055 (SectName == DumpSectName)) {
1057 uint32_t section_flags;
1059 const MachO::section_64 Sec = O->getSection64(Ref);
1060 section_flags = Sec.flags;
1063 const MachO::section Sec = O->getSection(Ref);
1064 section_flags = Sec.flags;
1066 uint32_t section_type = section_flags & MachO::SECTION_TYPE;
1069 Section.getContents(BytesStr);
1070 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1071 uint32_t sect_size = BytesStr.size();
1072 uint64_t sect_addr = Section.getAddress();
1075 outs().write(BytesStr.data(), BytesStr.size());
1079 outs() << "Contents of (" << SegName << "," << SectName
1083 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1084 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1085 DisassembleMachO(Filename, O, SegName, SectName);
1088 if (SegName == "__TEXT" && SectName == "__info_plist") {
1092 if (SegName == "__OBJC" && SectName == "__protocol") {
1093 DumpProtocolSection(O, sect, sect_size, sect_addr);
1096 switch (section_type) {
1097 case MachO::S_REGULAR:
1098 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1100 case MachO::S_ZEROFILL:
1101 outs() << "zerofill section and has no contents in the file\n";
1103 case MachO::S_CSTRING_LITERALS:
1104 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1106 case MachO::S_4BYTE_LITERALS:
1107 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1109 case MachO::S_8BYTE_LITERALS:
1110 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1112 case MachO::S_16BYTE_LITERALS:
1113 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1115 case MachO::S_LITERAL_POINTERS:
1116 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
1119 case MachO::S_MOD_INIT_FUNC_POINTERS:
1120 case MachO::S_MOD_TERM_FUNC_POINTERS:
1121 DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap,
1125 outs() << "Unknown section type ("
1126 << format("0x%08" PRIx32, section_type) << ")\n";
1127 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1131 if (section_type == MachO::S_ZEROFILL)
1132 outs() << "zerofill section and has no contents in the file\n";
1134 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1141 static void DumpInfoPlistSectionContents(StringRef Filename,
1142 MachOObjectFile *O) {
1143 for (const SectionRef &Section : O->sections()) {
1145 Section.getName(SectName);
1146 DataRefImpl Ref = Section.getRawDataRefImpl();
1147 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1148 if (SegName == "__TEXT" && SectName == "__info_plist") {
1149 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
1151 Section.getContents(BytesStr);
1152 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1159 // checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1160 // and if it is and there is a list of architecture flags is specified then
1161 // check to make sure this Mach-O file is one of those architectures or all
1162 // architectures were specified. If not then an error is generated and this
1163 // routine returns false. Else it returns true.
1164 static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
1165 if (isa<MachOObjectFile>(O) && !ArchAll && ArchFlags.size() != 0) {
1166 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O);
1167 bool ArchFound = false;
1168 MachO::mach_header H;
1169 MachO::mach_header_64 H_64;
1171 if (MachO->is64Bit()) {
1172 H_64 = MachO->MachOObjectFile::getHeader64();
1173 T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype);
1175 H = MachO->MachOObjectFile::getHeader();
1176 T = MachOObjectFile::getArch(H.cputype, H.cpusubtype);
1179 for (i = 0; i < ArchFlags.size(); ++i) {
1180 if (ArchFlags[i] == T.getArchName())
1185 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1186 << "architecture: " + ArchFlags[i] + "\n";
1193 static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1195 // ProcessMachO() is passed a single opened Mach-O file, which may be an
1196 // archive member and or in a slice of a universal file. It prints the
1197 // the file name and header info and then processes it according to the
1198 // command line options.
1199 static void ProcessMachO(StringRef Filename, MachOObjectFile *MachOOF,
1200 StringRef ArchiveMemberName = StringRef(),
1201 StringRef ArchitectureName = StringRef()) {
1202 // If we are doing some processing here on the Mach-O file print the header
1203 // info. And don't print it otherwise like in the case of printing the
1204 // UniversalHeaders or ArchiveHeaders.
1205 if (Disassemble || PrivateHeaders || ExportsTrie || Rebase || Bind ||
1206 LazyBind || WeakBind || IndirectSymbols || DataInCode || LinkOptHints ||
1207 DylibsUsed || DylibId || ObjcMetaData ||
1208 (DumpSections.size() != 0 && !Raw)) {
1210 if (!ArchiveMemberName.empty())
1211 outs() << '(' << ArchiveMemberName << ')';
1212 if (!ArchitectureName.empty())
1213 outs() << " (architecture " << ArchitectureName << ")";
1218 DisassembleMachO(Filename, MachOOF, "__TEXT", "__text");
1219 if (IndirectSymbols)
1220 PrintIndirectSymbols(MachOOF, !NonVerbose);
1222 PrintDataInCodeTable(MachOOF, !NonVerbose);
1224 PrintLinkOptHints(MachOOF);
1226 PrintRelocations(MachOOF);
1228 PrintSectionHeaders(MachOOF);
1229 if (SectionContents)
1230 PrintSectionContents(MachOOF);
1231 if (DumpSections.size() != 0)
1232 DumpSectionContents(Filename, MachOOF, !NonVerbose);
1234 DumpInfoPlistSectionContents(Filename, MachOOF);
1236 PrintDylibs(MachOOF, false);
1238 PrintDylibs(MachOOF, true);
1240 PrintSymbolTable(MachOOF);
1242 printMachOUnwindInfo(MachOOF);
1244 printMachOFileHeader(MachOOF);
1246 printObjcMetaData(MachOOF, !NonVerbose);
1248 printExportsTrie(MachOOF);
1250 printRebaseTable(MachOOF);
1252 printBindTable(MachOOF);
1254 printLazyBindTable(MachOOF);
1256 printWeakBindTable(MachOOF);
1259 // printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1260 static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
1261 outs() << " cputype (" << cputype << ")\n";
1262 outs() << " cpusubtype (" << cpusubtype << ")\n";
1265 // printCPUType() helps print_fat_headers by printing the cputype and
1266 // pusubtype (symbolically for the one's it knows about).
1267 static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
1269 case MachO::CPU_TYPE_I386:
1270 switch (cpusubtype) {
1271 case MachO::CPU_SUBTYPE_I386_ALL:
1272 outs() << " cputype CPU_TYPE_I386\n";
1273 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
1276 printUnknownCPUType(cputype, cpusubtype);
1280 case MachO::CPU_TYPE_X86_64:
1281 switch (cpusubtype) {
1282 case MachO::CPU_SUBTYPE_X86_64_ALL:
1283 outs() << " cputype CPU_TYPE_X86_64\n";
1284 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1286 case MachO::CPU_SUBTYPE_X86_64_H:
1287 outs() << " cputype CPU_TYPE_X86_64\n";
1288 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
1291 printUnknownCPUType(cputype, cpusubtype);
1295 case MachO::CPU_TYPE_ARM:
1296 switch (cpusubtype) {
1297 case MachO::CPU_SUBTYPE_ARM_ALL:
1298 outs() << " cputype CPU_TYPE_ARM\n";
1299 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
1301 case MachO::CPU_SUBTYPE_ARM_V4T:
1302 outs() << " cputype CPU_TYPE_ARM\n";
1303 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
1305 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1306 outs() << " cputype CPU_TYPE_ARM\n";
1307 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
1309 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1310 outs() << " cputype CPU_TYPE_ARM\n";
1311 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
1313 case MachO::CPU_SUBTYPE_ARM_V6:
1314 outs() << " cputype CPU_TYPE_ARM\n";
1315 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
1317 case MachO::CPU_SUBTYPE_ARM_V6M:
1318 outs() << " cputype CPU_TYPE_ARM\n";
1319 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
1321 case MachO::CPU_SUBTYPE_ARM_V7:
1322 outs() << " cputype CPU_TYPE_ARM\n";
1323 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
1325 case MachO::CPU_SUBTYPE_ARM_V7EM:
1326 outs() << " cputype CPU_TYPE_ARM\n";
1327 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
1329 case MachO::CPU_SUBTYPE_ARM_V7K:
1330 outs() << " cputype CPU_TYPE_ARM\n";
1331 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
1333 case MachO::CPU_SUBTYPE_ARM_V7M:
1334 outs() << " cputype CPU_TYPE_ARM\n";
1335 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
1337 case MachO::CPU_SUBTYPE_ARM_V7S:
1338 outs() << " cputype CPU_TYPE_ARM\n";
1339 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
1342 printUnknownCPUType(cputype, cpusubtype);
1346 case MachO::CPU_TYPE_ARM64:
1347 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1348 case MachO::CPU_SUBTYPE_ARM64_ALL:
1349 outs() << " cputype CPU_TYPE_ARM64\n";
1350 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
1353 printUnknownCPUType(cputype, cpusubtype);
1358 printUnknownCPUType(cputype, cpusubtype);
1363 static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
1365 outs() << "Fat headers\n";
1367 outs() << "fat_magic FAT_MAGIC\n";
1369 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
1371 uint32_t nfat_arch = UB->getNumberOfObjects();
1372 StringRef Buf = UB->getData();
1373 uint64_t size = Buf.size();
1374 uint64_t big_size = sizeof(struct MachO::fat_header) +
1375 nfat_arch * sizeof(struct MachO::fat_arch);
1376 outs() << "nfat_arch " << UB->getNumberOfObjects();
1378 outs() << " (malformed, contains zero architecture types)\n";
1379 else if (big_size > size)
1380 outs() << " (malformed, architectures past end of file)\n";
1384 for (uint32_t i = 0; i < nfat_arch; ++i) {
1385 MachOUniversalBinary::ObjectForArch OFA(UB, i);
1386 uint32_t cputype = OFA.getCPUType();
1387 uint32_t cpusubtype = OFA.getCPUSubType();
1388 outs() << "architecture ";
1389 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
1390 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
1391 uint32_t other_cputype = other_OFA.getCPUType();
1392 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
1393 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
1394 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
1395 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
1396 outs() << "(illegal duplicate architecture) ";
1401 outs() << OFA.getArchTypeName() << "\n";
1402 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1404 outs() << i << "\n";
1405 outs() << " cputype " << cputype << "\n";
1406 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
1410 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
1411 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
1413 outs() << " capabilities "
1414 << format("0x%" PRIx32,
1415 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
1416 outs() << " offset " << OFA.getOffset();
1417 if (OFA.getOffset() > size)
1418 outs() << " (past end of file)";
1419 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
1420 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
1422 outs() << " size " << OFA.getSize();
1423 big_size = OFA.getOffset() + OFA.getSize();
1424 if (big_size > size)
1425 outs() << " (past end of file)";
1427 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
1432 static void printArchiveChild(Archive::Child &C, bool verbose,
1433 bool print_offset) {
1435 outs() << C.getChildOffset() << "\t";
1436 sys::fs::perms Mode = C.getAccessMode();
1438 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
1439 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
1441 if (Mode & sys::fs::owner_read)
1445 if (Mode & sys::fs::owner_write)
1449 if (Mode & sys::fs::owner_exe)
1453 if (Mode & sys::fs::group_read)
1457 if (Mode & sys::fs::group_write)
1461 if (Mode & sys::fs::group_exe)
1465 if (Mode & sys::fs::others_read)
1469 if (Mode & sys::fs::others_write)
1473 if (Mode & sys::fs::others_exe)
1478 outs() << format("0%o ", Mode);
1481 unsigned UID = C.getUID();
1482 outs() << format("%3d/", UID);
1483 unsigned GID = C.getGID();
1484 outs() << format("%-3d ", GID);
1485 uint64_t Size = C.getRawSize();
1486 outs() << format("%5" PRId64, Size) << " ";
1488 StringRef RawLastModified = C.getRawLastModified();
1491 if (RawLastModified.getAsInteger(10, Seconds))
1492 outs() << "(date: \"%s\" contains non-decimal chars) " << RawLastModified;
1494 // Since cime(3) returns a 26 character string of the form:
1495 // "Sun Sep 16 01:03:52 1973\n\0"
1496 // just print 24 characters.
1498 outs() << format("%.24s ", ctime(&t));
1501 outs() << RawLastModified << " ";
1505 ErrorOr<StringRef> NameOrErr = C.getName();
1506 if (NameOrErr.getError()) {
1507 StringRef RawName = C.getRawName();
1508 outs() << RawName << "\n";
1510 StringRef Name = NameOrErr.get();
1511 outs() << Name << "\n";
1514 StringRef RawName = C.getRawName();
1515 outs() << RawName << "\n";
1519 static void printArchiveHeaders(Archive *A, bool verbose, bool print_offset) {
1520 if (A->hasSymbolTable()) {
1521 Archive::child_iterator S = A->getSymbolTableChild();
1522 Archive::Child C = *S;
1523 printArchiveChild(C, verbose, print_offset);
1525 for (Archive::child_iterator I = A->child_begin(), E = A->child_end(); I != E;
1527 Archive::Child C = *I;
1528 printArchiveChild(C, verbose, print_offset);
1532 // ParseInputMachO() parses the named Mach-O file in Filename and handles the
1533 // -arch flags selecting just those slices as specified by them and also parses
1534 // archive files. Then for each individual Mach-O file ProcessMachO() is
1535 // called to process the file based on the command line options.
1536 void llvm::ParseInputMachO(StringRef Filename) {
1537 // Check for -arch all and verifiy the -arch flags are valid.
1538 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1539 if (ArchFlags[i] == "all") {
1542 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
1543 errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
1544 "'for the -arch option\n";
1550 // Attempt to open the binary.
1551 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
1552 if (std::error_code EC = BinaryOrErr.getError()) {
1553 errs() << "llvm-objdump: '" << Filename << "': " << EC.message() << ".\n";
1556 Binary &Bin = *BinaryOrErr.get().getBinary();
1558 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1559 outs() << "Archive : " << Filename << "\n";
1561 printArchiveHeaders(A, true, false);
1562 for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
1564 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = I->getAsBinary();
1565 if (ChildOrErr.getError())
1567 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1568 if (!checkMachOAndArchFlags(O, Filename))
1570 ProcessMachO(Filename, O, O->getFileName());
1575 if (UniversalHeaders) {
1576 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
1577 printMachOUniversalHeaders(UB, !NonVerbose);
1579 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1580 // If we have a list of architecture flags specified dump only those.
1581 if (!ArchAll && ArchFlags.size() != 0) {
1582 // Look for a slice in the universal binary that matches each ArchFlag.
1584 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1586 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1587 E = UB->end_objects();
1589 if (ArchFlags[i] == I->getArchTypeName()) {
1591 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
1592 I->getAsObjectFile();
1593 std::string ArchitectureName = "";
1594 if (ArchFlags.size() > 1)
1595 ArchitectureName = I->getArchTypeName();
1597 ObjectFile &O = *ObjOrErr.get();
1598 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
1599 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
1600 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1601 I->getAsArchive()) {
1602 std::unique_ptr<Archive> &A = *AOrErr;
1603 outs() << "Archive : " << Filename;
1604 if (!ArchitectureName.empty())
1605 outs() << " (architecture " << ArchitectureName << ")";
1608 printArchiveHeaders(A.get(), true, false);
1609 for (Archive::child_iterator AI = A->child_begin(),
1610 AE = A->child_end();
1612 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1613 if (ChildOrErr.getError())
1615 if (MachOObjectFile *O =
1616 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
1617 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
1623 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1624 << "architecture: " + ArchFlags[i] + "\n";
1630 // No architecture flags were specified so if this contains a slice that
1631 // matches the host architecture dump only that.
1633 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1634 E = UB->end_objects();
1636 if (MachOObjectFile::getHostArch().getArchName() ==
1637 I->getArchTypeName()) {
1638 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1639 std::string ArchiveName;
1640 ArchiveName.clear();
1642 ObjectFile &O = *ObjOrErr.get();
1643 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
1644 ProcessMachO(Filename, MachOOF);
1645 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1646 I->getAsArchive()) {
1647 std::unique_ptr<Archive> &A = *AOrErr;
1648 outs() << "Archive : " << Filename << "\n";
1650 printArchiveHeaders(A.get(), true, false);
1651 for (Archive::child_iterator AI = A->child_begin(),
1652 AE = A->child_end();
1654 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1655 if (ChildOrErr.getError())
1657 if (MachOObjectFile *O =
1658 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
1659 ProcessMachO(Filename, O, O->getFileName());
1666 // Either all architectures have been specified or none have been specified
1667 // and this does not contain the host architecture so dump all the slices.
1668 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1669 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1670 E = UB->end_objects();
1672 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1673 std::string ArchitectureName = "";
1674 if (moreThanOneArch)
1675 ArchitectureName = I->getArchTypeName();
1677 ObjectFile &Obj = *ObjOrErr.get();
1678 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
1679 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
1680 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
1681 std::unique_ptr<Archive> &A = *AOrErr;
1682 outs() << "Archive : " << Filename;
1683 if (!ArchitectureName.empty())
1684 outs() << " (architecture " << ArchitectureName << ")";
1687 printArchiveHeaders(A.get(), true, false);
1688 for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
1690 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1691 if (ChildOrErr.getError())
1693 if (MachOObjectFile *O =
1694 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1695 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
1696 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
1704 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
1705 if (!checkMachOAndArchFlags(O, Filename))
1707 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
1708 ProcessMachO(Filename, MachOOF);
1710 errs() << "llvm-objdump: '" << Filename << "': "
1711 << "Object is not a Mach-O file type.\n";
1713 errs() << "llvm-objdump: '" << Filename << "': "
1714 << "Unrecognized file type.\n";
1717 typedef std::pair<uint64_t, const char *> BindInfoEntry;
1718 typedef std::vector<BindInfoEntry> BindTable;
1719 typedef BindTable::iterator bind_table_iterator;
1721 // The block of info used by the Symbolizer call backs.
1722 struct DisassembleInfo {
1726 SymbolAddressMap *AddrMap;
1727 std::vector<SectionRef> *Sections;
1728 const char *class_name;
1729 const char *selector_name;
1731 char *demangled_name;
1734 BindTable *bindtable;
1737 // SymbolizerGetOpInfo() is the operand information call back function.
1738 // This is called to get the symbolic information for operand(s) of an
1739 // instruction when it is being done. This routine does this from
1740 // the relocation information, symbol table, etc. That block of information
1741 // is a pointer to the struct DisassembleInfo that was passed when the
1742 // disassembler context was created and passed to back to here when
1743 // called back by the disassembler for instruction operands that could have
1744 // relocation information. The address of the instruction containing operand is
1745 // at the Pc parameter. The immediate value the operand has is passed in
1746 // op_info->Value and is at Offset past the start of the instruction and has a
1747 // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
1748 // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
1749 // names and addends of the symbolic expression to add for the operand. The
1750 // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
1751 // information is returned then this function returns 1 else it returns 0.
1752 static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
1753 uint64_t Size, int TagType, void *TagBuf) {
1754 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
1755 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
1756 uint64_t value = op_info->Value;
1758 // Make sure all fields returned are zero if we don't set them.
1759 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
1760 op_info->Value = value;
1762 // If the TagType is not the value 1 which it code knows about or if no
1763 // verbose symbolic information is wanted then just return 0, indicating no
1764 // information is being returned.
1765 if (TagType != 1 || !info->verbose)
1768 unsigned int Arch = info->O->getArch();
1769 if (Arch == Triple::x86) {
1770 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1772 // First search the section's relocation entries (if any) for an entry
1773 // for this section offset.
1774 uint32_t sect_addr = info->S.getAddress();
1775 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1776 bool reloc_found = false;
1778 MachO::any_relocation_info RE;
1779 bool isExtern = false;
1781 bool r_scattered = false;
1782 uint32_t r_value, pair_r_value, r_type;
1783 for (const RelocationRef &Reloc : info->S.relocations()) {
1784 uint64_t RelocOffset;
1785 Reloc.getOffset(RelocOffset);
1786 if (RelocOffset == sect_offset) {
1787 Rel = Reloc.getRawDataRefImpl();
1788 RE = info->O->getRelocation(Rel);
1789 r_type = info->O->getAnyRelocationType(RE);
1790 r_scattered = info->O->isRelocationScattered(RE);
1792 r_value = info->O->getScatteredRelocationValue(RE);
1793 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1794 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
1795 DataRefImpl RelNext = Rel;
1796 info->O->moveRelocationNext(RelNext);
1797 MachO::any_relocation_info RENext;
1798 RENext = info->O->getRelocation(RelNext);
1799 if (info->O->isRelocationScattered(RENext))
1800 pair_r_value = info->O->getScatteredRelocationValue(RENext);
1805 isExtern = info->O->getPlainRelocationExternal(RE);
1807 symbol_iterator RelocSym = Reloc.getSymbol();
1815 if (reloc_found && isExtern) {
1817 Symbol.getName(SymName);
1818 const char *name = SymName.data();
1819 op_info->AddSymbol.Present = 1;
1820 op_info->AddSymbol.Name = name;
1821 // For i386 extern relocation entries the value in the instruction is
1822 // the offset from the symbol, and value is already set in op_info->Value.
1825 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1826 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
1827 const char *add = GuessSymbolName(r_value, info->AddrMap);
1828 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
1829 uint32_t offset = value - (r_value - pair_r_value);
1830 op_info->AddSymbol.Present = 1;
1832 op_info->AddSymbol.Name = add;
1834 op_info->AddSymbol.Value = r_value;
1835 op_info->SubtractSymbol.Present = 1;
1837 op_info->SubtractSymbol.Name = sub;
1839 op_info->SubtractSymbol.Value = pair_r_value;
1840 op_info->Value = offset;
1844 // Second search the external relocation entries of a fully linked image
1845 // (if any) for an entry that matches this segment offset.
1846 // uint32_t seg_offset = (Pc + Offset);
1849 if (Arch == Triple::x86_64) {
1850 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1852 // First search the section's relocation entries (if any) for an entry
1853 // for this section offset.
1854 uint64_t sect_addr = info->S.getAddress();
1855 uint64_t sect_offset = (Pc + Offset) - sect_addr;
1856 bool reloc_found = false;
1858 MachO::any_relocation_info RE;
1859 bool isExtern = false;
1861 for (const RelocationRef &Reloc : info->S.relocations()) {
1862 uint64_t RelocOffset;
1863 Reloc.getOffset(RelocOffset);
1864 if (RelocOffset == sect_offset) {
1865 Rel = Reloc.getRawDataRefImpl();
1866 RE = info->O->getRelocation(Rel);
1867 // NOTE: Scattered relocations don't exist on x86_64.
1868 isExtern = info->O->getPlainRelocationExternal(RE);
1870 symbol_iterator RelocSym = Reloc.getSymbol();
1877 if (reloc_found && isExtern) {
1878 // The Value passed in will be adjusted by the Pc if the instruction
1879 // adds the Pc. But for x86_64 external relocation entries the Value
1880 // is the offset from the external symbol.
1881 if (info->O->getAnyRelocationPCRel(RE))
1882 op_info->Value -= Pc + Offset + Size;
1884 Symbol.getName(SymName);
1885 const char *name = SymName.data();
1886 unsigned Type = info->O->getAnyRelocationType(RE);
1887 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
1888 DataRefImpl RelNext = Rel;
1889 info->O->moveRelocationNext(RelNext);
1890 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
1891 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
1892 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
1893 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
1894 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
1895 op_info->SubtractSymbol.Present = 1;
1896 op_info->SubtractSymbol.Name = name;
1897 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
1898 Symbol = *RelocSymNext;
1899 StringRef SymNameNext;
1900 Symbol.getName(SymNameNext);
1901 name = SymNameNext.data();
1904 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
1905 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
1906 op_info->AddSymbol.Present = 1;
1907 op_info->AddSymbol.Name = name;
1911 // Second search the external relocation entries of a fully linked image
1912 // (if any) for an entry that matches this segment offset.
1913 // uint64_t seg_offset = (Pc + Offset);
1916 if (Arch == Triple::arm) {
1917 if (Offset != 0 || (Size != 4 && Size != 2))
1919 // First search the section's relocation entries (if any) for an entry
1920 // for this section offset.
1921 uint32_t sect_addr = info->S.getAddress();
1922 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1924 MachO::any_relocation_info RE;
1925 bool isExtern = false;
1927 bool r_scattered = false;
1928 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
1930 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
1931 [&](const RelocationRef &Reloc) {
1932 uint64_t RelocOffset;
1933 Reloc.getOffset(RelocOffset);
1934 return RelocOffset == sect_offset;
1937 if (Reloc == info->S.relocations().end())
1940 Rel = Reloc->getRawDataRefImpl();
1941 RE = info->O->getRelocation(Rel);
1942 r_length = info->O->getAnyRelocationLength(RE);
1943 r_scattered = info->O->isRelocationScattered(RE);
1945 r_value = info->O->getScatteredRelocationValue(RE);
1946 r_type = info->O->getScatteredRelocationType(RE);
1948 r_type = info->O->getAnyRelocationType(RE);
1949 isExtern = info->O->getPlainRelocationExternal(RE);
1951 symbol_iterator RelocSym = Reloc->getSymbol();
1955 if (r_type == MachO::ARM_RELOC_HALF ||
1956 r_type == MachO::ARM_RELOC_SECTDIFF ||
1957 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
1958 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1959 DataRefImpl RelNext = Rel;
1960 info->O->moveRelocationNext(RelNext);
1961 MachO::any_relocation_info RENext;
1962 RENext = info->O->getRelocation(RelNext);
1963 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
1964 if (info->O->isRelocationScattered(RENext))
1965 pair_r_value = info->O->getScatteredRelocationValue(RENext);
1970 Symbol.getName(SymName);
1971 const char *name = SymName.data();
1972 op_info->AddSymbol.Present = 1;
1973 op_info->AddSymbol.Name = name;
1975 case MachO::ARM_RELOC_HALF:
1976 if ((r_length & 0x1) == 1) {
1977 op_info->Value = value << 16 | other_half;
1978 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1980 op_info->Value = other_half << 16 | value;
1981 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
1989 // If we have a branch that is not an external relocation entry then
1990 // return 0 so the code in tryAddingSymbolicOperand() can use the
1991 // SymbolLookUp call back with the branch target address to look up the
1992 // symbol and possiblity add an annotation for a symbol stub.
1993 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
1994 r_type == MachO::ARM_THUMB_RELOC_BR22))
1997 uint32_t offset = 0;
1998 if (r_type == MachO::ARM_RELOC_HALF ||
1999 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2000 if ((r_length & 0x1) == 1)
2001 value = value << 16 | other_half;
2003 value = other_half << 16 | value;
2005 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
2006 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
2007 offset = value - r_value;
2011 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2012 if ((r_length & 0x1) == 1)
2013 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2015 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2016 const char *add = GuessSymbolName(r_value, info->AddrMap);
2017 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
2018 int32_t offset = value - (r_value - pair_r_value);
2019 op_info->AddSymbol.Present = 1;
2021 op_info->AddSymbol.Name = add;
2023 op_info->AddSymbol.Value = r_value;
2024 op_info->SubtractSymbol.Present = 1;
2026 op_info->SubtractSymbol.Name = sub;
2028 op_info->SubtractSymbol.Value = pair_r_value;
2029 op_info->Value = offset;
2033 op_info->AddSymbol.Present = 1;
2034 op_info->Value = offset;
2035 if (r_type == MachO::ARM_RELOC_HALF) {
2036 if ((r_length & 0x1) == 1)
2037 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2039 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2041 const char *add = GuessSymbolName(value, info->AddrMap);
2042 if (add != nullptr) {
2043 op_info->AddSymbol.Name = add;
2046 op_info->AddSymbol.Value = value;
2049 if (Arch == Triple::aarch64) {
2050 if (Offset != 0 || Size != 4)
2052 // First search the section's relocation entries (if any) for an entry
2053 // for this section offset.
2054 uint64_t sect_addr = info->S.getAddress();
2055 uint64_t sect_offset = (Pc + Offset) - sect_addr;
2057 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
2058 [&](const RelocationRef &Reloc) {
2059 uint64_t RelocOffset;
2060 Reloc.getOffset(RelocOffset);
2061 return RelocOffset == sect_offset;
2064 if (Reloc == info->S.relocations().end())
2067 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2068 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2069 uint32_t r_type = info->O->getAnyRelocationType(RE);
2070 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2071 DataRefImpl RelNext = Rel;
2072 info->O->moveRelocationNext(RelNext);
2073 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2075 value = info->O->getPlainRelocationSymbolNum(RENext);
2076 op_info->Value = value;
2079 // NOTE: Scattered relocations don't exist on arm64.
2080 if (!info->O->getPlainRelocationExternal(RE))
2083 Reloc->getSymbol()->getName(SymName);
2084 const char *name = SymName.data();
2085 op_info->AddSymbol.Present = 1;
2086 op_info->AddSymbol.Name = name;
2089 case MachO::ARM64_RELOC_PAGE21:
2091 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2093 case MachO::ARM64_RELOC_PAGEOFF12:
2095 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2097 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2099 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2101 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2103 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2105 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2106 /* @tvlppage is not implemented in llvm-mc */
2107 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2109 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2110 /* @tvlppageoff is not implemented in llvm-mc */
2111 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2114 case MachO::ARM64_RELOC_BRANCH26:
2115 op_info->VariantKind = LLVMDisassembler_VariantKind_None;
2123 // GuessCstringPointer is passed the address of what might be a pointer to a
2124 // literal string in a cstring section. If that address is in a cstring section
2125 // it returns a pointer to that string. Else it returns nullptr.
2126 static const char *GuessCstringPointer(uint64_t ReferenceValue,
2127 struct DisassembleInfo *info) {
2128 uint32_t LoadCommandCount = info->O->getHeader().ncmds;
2129 MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo();
2130 for (unsigned I = 0;; ++I) {
2131 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2132 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2133 for (unsigned J = 0; J < Seg.nsects; ++J) {
2134 MachO::section_64 Sec = info->O->getSection64(Load, J);
2135 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2136 if (section_type == MachO::S_CSTRING_LITERALS &&
2137 ReferenceValue >= Sec.addr &&
2138 ReferenceValue < Sec.addr + Sec.size) {
2139 uint64_t sect_offset = ReferenceValue - Sec.addr;
2140 uint64_t object_offset = Sec.offset + sect_offset;
2141 StringRef MachOContents = info->O->getData();
2142 uint64_t object_size = MachOContents.size();
2143 const char *object_addr = (const char *)MachOContents.data();
2144 if (object_offset < object_size) {
2145 const char *name = object_addr + object_offset;
2152 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2153 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2154 for (unsigned J = 0; J < Seg.nsects; ++J) {
2155 MachO::section Sec = info->O->getSection(Load, J);
2156 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2157 if (section_type == MachO::S_CSTRING_LITERALS &&
2158 ReferenceValue >= Sec.addr &&
2159 ReferenceValue < Sec.addr + Sec.size) {
2160 uint64_t sect_offset = ReferenceValue - Sec.addr;
2161 uint64_t object_offset = Sec.offset + sect_offset;
2162 StringRef MachOContents = info->O->getData();
2163 uint64_t object_size = MachOContents.size();
2164 const char *object_addr = (const char *)MachOContents.data();
2165 if (object_offset < object_size) {
2166 const char *name = object_addr + object_offset;
2174 if (I == LoadCommandCount - 1)
2177 Load = info->O->getNextLoadCommandInfo(Load);
2182 // GuessIndirectSymbol returns the name of the indirect symbol for the
2183 // ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
2184 // an address of a symbol stub or a lazy or non-lazy pointer to associate the
2185 // symbol name being referenced by the stub or pointer.
2186 static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2187 struct DisassembleInfo *info) {
2188 uint32_t LoadCommandCount = info->O->getHeader().ncmds;
2189 MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo();
2190 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2191 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
2192 for (unsigned I = 0;; ++I) {
2193 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2194 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2195 for (unsigned J = 0; J < Seg.nsects; ++J) {
2196 MachO::section_64 Sec = info->O->getSection64(Load, J);
2197 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2198 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2199 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2200 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2201 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2202 section_type == MachO::S_SYMBOL_STUBS) &&
2203 ReferenceValue >= Sec.addr &&
2204 ReferenceValue < Sec.addr + Sec.size) {
2206 if (section_type == MachO::S_SYMBOL_STUBS)
2207 stride = Sec.reserved2;
2212 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2213 if (index < Dysymtab.nindirectsyms) {
2214 uint32_t indirect_symbol =
2215 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
2216 if (indirect_symbol < Symtab.nsyms) {
2217 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2218 SymbolRef Symbol = *Sym;
2220 Symbol.getName(SymName);
2221 const char *name = SymName.data();
2227 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2228 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2229 for (unsigned J = 0; J < Seg.nsects; ++J) {
2230 MachO::section Sec = info->O->getSection(Load, J);
2231 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2232 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2233 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2234 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2235 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2236 section_type == MachO::S_SYMBOL_STUBS) &&
2237 ReferenceValue >= Sec.addr &&
2238 ReferenceValue < Sec.addr + Sec.size) {
2240 if (section_type == MachO::S_SYMBOL_STUBS)
2241 stride = Sec.reserved2;
2246 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2247 if (index < Dysymtab.nindirectsyms) {
2248 uint32_t indirect_symbol =
2249 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
2250 if (indirect_symbol < Symtab.nsyms) {
2251 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2252 SymbolRef Symbol = *Sym;
2254 Symbol.getName(SymName);
2255 const char *name = SymName.data();
2262 if (I == LoadCommandCount - 1)
2265 Load = info->O->getNextLoadCommandInfo(Load);
2270 // method_reference() is called passing it the ReferenceName that might be
2271 // a reference it to an Objective-C method call. If so then it allocates and
2272 // assembles a method call string with the values last seen and saved in
2273 // the DisassembleInfo's class_name and selector_name fields. This is saved
2274 // into the method field of the info and any previous string is free'ed.
2275 // Then the class_name field in the info is set to nullptr. The method call
2276 // string is set into ReferenceName and ReferenceType is set to
2277 // LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
2278 // then both ReferenceType and ReferenceName are left unchanged.
2279 static void method_reference(struct DisassembleInfo *info,
2280 uint64_t *ReferenceType,
2281 const char **ReferenceName) {
2282 unsigned int Arch = info->O->getArch();
2283 if (*ReferenceName != nullptr) {
2284 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
2285 if (info->selector_name != nullptr) {
2286 if (info->method != nullptr)
2288 if (info->class_name != nullptr) {
2289 info->method = (char *)malloc(5 + strlen(info->class_name) +
2290 strlen(info->selector_name));
2291 if (info->method != nullptr) {
2292 strcpy(info->method, "+[");
2293 strcat(info->method, info->class_name);
2294 strcat(info->method, " ");
2295 strcat(info->method, info->selector_name);
2296 strcat(info->method, "]");
2297 *ReferenceName = info->method;
2298 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2301 info->method = (char *)malloc(9 + strlen(info->selector_name));
2302 if (info->method != nullptr) {
2303 if (Arch == Triple::x86_64)
2304 strcpy(info->method, "-[%rdi ");
2305 else if (Arch == Triple::aarch64)
2306 strcpy(info->method, "-[x0 ");
2308 strcpy(info->method, "-[r? ");
2309 strcat(info->method, info->selector_name);
2310 strcat(info->method, "]");
2311 *ReferenceName = info->method;
2312 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2315 info->class_name = nullptr;
2317 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
2318 if (info->selector_name != nullptr) {
2319 if (info->method != nullptr)
2321 info->method = (char *)malloc(17 + strlen(info->selector_name));
2322 if (info->method != nullptr) {
2323 if (Arch == Triple::x86_64)
2324 strcpy(info->method, "-[[%rdi super] ");
2325 else if (Arch == Triple::aarch64)
2326 strcpy(info->method, "-[[x0 super] ");
2328 strcpy(info->method, "-[[r? super] ");
2329 strcat(info->method, info->selector_name);
2330 strcat(info->method, "]");
2331 *ReferenceName = info->method;
2332 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2334 info->class_name = nullptr;
2340 // GuessPointerPointer() is passed the address of what might be a pointer to
2341 // a reference to an Objective-C class, selector, message ref or cfstring.
2342 // If so the value of the pointer is returned and one of the booleans are set
2343 // to true. If not zero is returned and all the booleans are set to false.
2344 static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
2345 struct DisassembleInfo *info,
2346 bool &classref, bool &selref, bool &msgref,
2352 uint32_t LoadCommandCount = info->O->getHeader().ncmds;
2353 MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo();
2354 for (unsigned I = 0;; ++I) {
2355 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2356 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2357 for (unsigned J = 0; J < Seg.nsects; ++J) {
2358 MachO::section_64 Sec = info->O->getSection64(Load, J);
2359 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
2360 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2361 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
2362 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
2363 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
2364 ReferenceValue >= Sec.addr &&
2365 ReferenceValue < Sec.addr + Sec.size) {
2366 uint64_t sect_offset = ReferenceValue - Sec.addr;
2367 uint64_t object_offset = Sec.offset + sect_offset;
2368 StringRef MachOContents = info->O->getData();
2369 uint64_t object_size = MachOContents.size();
2370 const char *object_addr = (const char *)MachOContents.data();
2371 if (object_offset < object_size) {
2372 uint64_t pointer_value;
2373 memcpy(&pointer_value, object_addr + object_offset,
2375 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2376 sys::swapByteOrder(pointer_value);
2377 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
2379 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2380 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
2382 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
2383 ReferenceValue + 8 < Sec.addr + Sec.size) {
2385 memcpy(&pointer_value, object_addr + object_offset + 8,
2387 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2388 sys::swapByteOrder(pointer_value);
2389 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
2391 return pointer_value;
2398 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
2399 if (I == LoadCommandCount - 1)
2402 Load = info->O->getNextLoadCommandInfo(Load);
2407 // get_pointer_64 returns a pointer to the bytes in the object file at the
2408 // Address from a section in the Mach-O file. And indirectly returns the
2409 // offset into the section, number of bytes left in the section past the offset
2410 // and which section is was being referenced. If the Address is not in a
2411 // section nullptr is returned.
2412 static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
2413 uint32_t &left, SectionRef &S,
2414 DisassembleInfo *info,
2415 bool objc_only = false) {
2419 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
2420 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
2421 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
2424 ((*(info->Sections))[SectIdx]).getName(SectName);
2425 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
2426 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
2427 if (SegName != "__OBJC" && SectName != "__cstring")
2430 if (Address >= SectAddress && Address < SectAddress + SectSize) {
2431 S = (*(info->Sections))[SectIdx];
2432 offset = Address - SectAddress;
2433 left = SectSize - offset;
2434 StringRef SectContents;
2435 ((*(info->Sections))[SectIdx]).getContents(SectContents);
2436 return SectContents.data() + offset;
2442 static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
2443 uint32_t &left, SectionRef &S,
2444 DisassembleInfo *info,
2445 bool objc_only = false) {
2446 return get_pointer_64(Address, offset, left, S, info, objc_only);
2449 // get_symbol_64() returns the name of a symbol (or nullptr) and the address of
2450 // the symbol indirectly through n_value. Based on the relocation information
2451 // for the specified section offset in the specified section reference.
2452 // If no relocation information is found and a non-zero ReferenceValue for the
2453 // symbol is passed, look up that address in the info's AddrMap.
2455 get_symbol_64(uint32_t sect_offset, SectionRef S, DisassembleInfo *info,
2457 uint64_t ReferenceValue = UnknownAddressOrSize) {
2462 // See if there is an external relocation entry at the sect_offset.
2463 bool reloc_found = false;
2465 MachO::any_relocation_info RE;
2466 bool isExtern = false;
2468 for (const RelocationRef &Reloc : S.relocations()) {
2469 uint64_t RelocOffset;
2470 Reloc.getOffset(RelocOffset);
2471 if (RelocOffset == sect_offset) {
2472 Rel = Reloc.getRawDataRefImpl();
2473 RE = info->O->getRelocation(Rel);
2474 if (info->O->isRelocationScattered(RE))
2476 isExtern = info->O->getPlainRelocationExternal(RE);
2478 symbol_iterator RelocSym = Reloc.getSymbol();
2485 // If there is an external relocation entry for a symbol in this section
2486 // at this section_offset then use that symbol's value for the n_value
2487 // and return its name.
2488 const char *SymbolName = nullptr;
2489 if (reloc_found && isExtern) {
2490 Symbol.getAddress(n_value);
2491 if (n_value == UnknownAddressOrSize)
2494 Symbol.getName(name);
2495 if (!name.empty()) {
2496 SymbolName = name.data();
2501 // TODO: For fully linked images, look through the external relocation
2502 // entries off the dynamic symtab command. For these the r_offset is from the
2503 // start of the first writeable segment in the Mach-O file. So the offset
2504 // to this section from that segment is passed to this routine by the caller,
2505 // as the database_offset. Which is the difference of the section's starting
2506 // address and the first writable segment.
2508 // NOTE: need add passing the database_offset to this routine.
2510 // We did not find an external relocation entry so look up the ReferenceValue
2511 // as an address of a symbol and if found return that symbol's name.
2512 if (ReferenceValue != UnknownAddressOrSize)
2513 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
2518 static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
2519 DisassembleInfo *info,
2520 uint32_t ReferenceValue) {
2522 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
2525 // These are structs in the Objective-C meta data and read to produce the
2526 // comments for disassembly. While these are part of the ABI they are no
2527 // public defintions. So the are here not in include/llvm/Support/MachO.h .
2529 // The cfstring object in a 64-bit Mach-O file.
2530 struct cfstring64_t {
2531 uint64_t isa; // class64_t * (64-bit pointer)
2532 uint64_t flags; // flag bits
2533 uint64_t characters; // char * (64-bit pointer)
2534 uint64_t length; // number of non-NULL characters in above
2537 // The class object in a 64-bit Mach-O file.
2539 uint64_t isa; // class64_t * (64-bit pointer)
2540 uint64_t superclass; // class64_t * (64-bit pointer)
2541 uint64_t cache; // Cache (64-bit pointer)
2542 uint64_t vtable; // IMP * (64-bit pointer)
2543 uint64_t data; // class_ro64_t * (64-bit pointer)
2547 uint32_t isa; /* class32_t * (32-bit pointer) */
2548 uint32_t superclass; /* class32_t * (32-bit pointer) */
2549 uint32_t cache; /* Cache (32-bit pointer) */
2550 uint32_t vtable; /* IMP * (32-bit pointer) */
2551 uint32_t data; /* class_ro32_t * (32-bit pointer) */
2554 struct class_ro64_t {
2556 uint32_t instanceStart;
2557 uint32_t instanceSize;
2559 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
2560 uint64_t name; // const char * (64-bit pointer)
2561 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
2562 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
2563 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
2564 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
2565 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
2568 struct class_ro32_t {
2570 uint32_t instanceStart;
2571 uint32_t instanceSize;
2572 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
2573 uint32_t name; /* const char * (32-bit pointer) */
2574 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
2575 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
2576 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
2577 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
2578 uint32_t baseProperties; /* const struct objc_property_list *
2582 /* Values for class_ro{64,32}_t->flags */
2583 #define RO_META (1 << 0)
2584 #define RO_ROOT (1 << 1)
2585 #define RO_HAS_CXX_STRUCTORS (1 << 2)
2587 struct method_list64_t {
2590 /* struct method64_t first; These structures follow inline */
2593 struct method_list32_t {
2596 /* struct method32_t first; These structures follow inline */
2600 uint64_t name; /* SEL (64-bit pointer) */
2601 uint64_t types; /* const char * (64-bit pointer) */
2602 uint64_t imp; /* IMP (64-bit pointer) */
2606 uint32_t name; /* SEL (32-bit pointer) */
2607 uint32_t types; /* const char * (32-bit pointer) */
2608 uint32_t imp; /* IMP (32-bit pointer) */
2611 struct protocol_list64_t {
2612 uint64_t count; /* uintptr_t (a 64-bit value) */
2613 /* struct protocol64_t * list[0]; These pointers follow inline */
2616 struct protocol_list32_t {
2617 uint32_t count; /* uintptr_t (a 32-bit value) */
2618 /* struct protocol32_t * list[0]; These pointers follow inline */
2621 struct protocol64_t {
2622 uint64_t isa; /* id * (64-bit pointer) */
2623 uint64_t name; /* const char * (64-bit pointer) */
2624 uint64_t protocols; /* struct protocol_list64_t *
2626 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
2627 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
2628 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
2629 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
2630 uint64_t instanceProperties; /* struct objc_property_list *
2634 struct protocol32_t {
2635 uint32_t isa; /* id * (32-bit pointer) */
2636 uint32_t name; /* const char * (32-bit pointer) */
2637 uint32_t protocols; /* struct protocol_list_t *
2639 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
2640 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
2641 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
2642 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
2643 uint32_t instanceProperties; /* struct objc_property_list *
2647 struct ivar_list64_t {
2650 /* struct ivar64_t first; These structures follow inline */
2653 struct ivar_list32_t {
2656 /* struct ivar32_t first; These structures follow inline */
2660 uint64_t offset; /* uintptr_t * (64-bit pointer) */
2661 uint64_t name; /* const char * (64-bit pointer) */
2662 uint64_t type; /* const char * (64-bit pointer) */
2668 uint32_t offset; /* uintptr_t * (32-bit pointer) */
2669 uint32_t name; /* const char * (32-bit pointer) */
2670 uint32_t type; /* const char * (32-bit pointer) */
2675 struct objc_property_list64 {
2678 /* struct objc_property64 first; These structures follow inline */
2681 struct objc_property_list32 {
2684 /* struct objc_property32 first; These structures follow inline */
2687 struct objc_property64 {
2688 uint64_t name; /* const char * (64-bit pointer) */
2689 uint64_t attributes; /* const char * (64-bit pointer) */
2692 struct objc_property32 {
2693 uint32_t name; /* const char * (32-bit pointer) */
2694 uint32_t attributes; /* const char * (32-bit pointer) */
2697 struct category64_t {
2698 uint64_t name; /* const char * (64-bit pointer) */
2699 uint64_t cls; /* struct class_t * (64-bit pointer) */
2700 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
2701 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
2702 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
2703 uint64_t instanceProperties; /* struct objc_property_list *
2707 struct category32_t {
2708 uint32_t name; /* const char * (32-bit pointer) */
2709 uint32_t cls; /* struct class_t * (32-bit pointer) */
2710 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
2711 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
2712 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
2713 uint32_t instanceProperties; /* struct objc_property_list *
2717 struct objc_image_info64 {
2721 struct objc_image_info32 {
2725 struct imageInfo_t {
2729 /* masks for objc_image_info.flags */
2730 #define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
2731 #define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
2733 struct message_ref64 {
2734 uint64_t imp; /* IMP (64-bit pointer) */
2735 uint64_t sel; /* SEL (64-bit pointer) */
2738 struct message_ref32 {
2739 uint32_t imp; /* IMP (32-bit pointer) */
2740 uint32_t sel; /* SEL (32-bit pointer) */
2743 // Objective-C 1 (32-bit only) meta data structs.
2745 struct objc_module_t {
2748 uint32_t name; /* char * (32-bit pointer) */
2749 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
2752 struct objc_symtab_t {
2753 uint32_t sel_ref_cnt;
2754 uint32_t refs; /* SEL * (32-bit pointer) */
2755 uint16_t cls_def_cnt;
2756 uint16_t cat_def_cnt;
2757 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
2760 struct objc_class_t {
2761 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2762 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
2763 uint32_t name; /* const char * (32-bit pointer) */
2766 int32_t instance_size;
2767 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
2768 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
2769 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
2770 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
2773 #define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
2774 // class is not a metaclass
2775 #define CLS_CLASS 0x1
2776 // class is a metaclass
2777 #define CLS_META 0x2
2779 struct objc_category_t {
2780 uint32_t category_name; /* char * (32-bit pointer) */
2781 uint32_t class_name; /* char * (32-bit pointer) */
2782 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
2783 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
2784 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
2787 struct objc_ivar_t {
2788 uint32_t ivar_name; /* char * (32-bit pointer) */
2789 uint32_t ivar_type; /* char * (32-bit pointer) */
2790 int32_t ivar_offset;
2793 struct objc_ivar_list_t {
2795 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
2798 struct objc_method_list_t {
2799 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
2800 int32_t method_count;
2801 // struct objc_method_t method_list[1]; /* variable length structure */
2804 struct objc_method_t {
2805 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2806 uint32_t method_types; /* char * (32-bit pointer) */
2807 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
2811 struct objc_protocol_list_t {
2812 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
2814 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
2815 // (32-bit pointer) */
2818 struct objc_protocol_t {
2819 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2820 uint32_t protocol_name; /* char * (32-bit pointer) */
2821 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
2822 uint32_t instance_methods; /* struct objc_method_description_list *
2824 uint32_t class_methods; /* struct objc_method_description_list *
2828 struct objc_method_description_list_t {
2830 // struct objc_method_description_t list[1];
2833 struct objc_method_description_t {
2834 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2835 uint32_t types; /* char * (32-bit pointer) */
2838 inline void swapStruct(struct cfstring64_t &cfs) {
2839 sys::swapByteOrder(cfs.isa);
2840 sys::swapByteOrder(cfs.flags);
2841 sys::swapByteOrder(cfs.characters);
2842 sys::swapByteOrder(cfs.length);
2845 inline void swapStruct(struct class64_t &c) {
2846 sys::swapByteOrder(c.isa);
2847 sys::swapByteOrder(c.superclass);
2848 sys::swapByteOrder(c.cache);
2849 sys::swapByteOrder(c.vtable);
2850 sys::swapByteOrder(c.data);
2853 inline void swapStruct(struct class32_t &c) {
2854 sys::swapByteOrder(c.isa);
2855 sys::swapByteOrder(c.superclass);
2856 sys::swapByteOrder(c.cache);
2857 sys::swapByteOrder(c.vtable);
2858 sys::swapByteOrder(c.data);
2861 inline void swapStruct(struct class_ro64_t &cro) {
2862 sys::swapByteOrder(cro.flags);
2863 sys::swapByteOrder(cro.instanceStart);
2864 sys::swapByteOrder(cro.instanceSize);
2865 sys::swapByteOrder(cro.reserved);
2866 sys::swapByteOrder(cro.ivarLayout);
2867 sys::swapByteOrder(cro.name);
2868 sys::swapByteOrder(cro.baseMethods);
2869 sys::swapByteOrder(cro.baseProtocols);
2870 sys::swapByteOrder(cro.ivars);
2871 sys::swapByteOrder(cro.weakIvarLayout);
2872 sys::swapByteOrder(cro.baseProperties);
2875 inline void swapStruct(struct class_ro32_t &cro) {
2876 sys::swapByteOrder(cro.flags);
2877 sys::swapByteOrder(cro.instanceStart);
2878 sys::swapByteOrder(cro.instanceSize);
2879 sys::swapByteOrder(cro.ivarLayout);
2880 sys::swapByteOrder(cro.name);
2881 sys::swapByteOrder(cro.baseMethods);
2882 sys::swapByteOrder(cro.baseProtocols);
2883 sys::swapByteOrder(cro.ivars);
2884 sys::swapByteOrder(cro.weakIvarLayout);
2885 sys::swapByteOrder(cro.baseProperties);
2888 inline void swapStruct(struct method_list64_t &ml) {
2889 sys::swapByteOrder(ml.entsize);
2890 sys::swapByteOrder(ml.count);
2893 inline void swapStruct(struct method_list32_t &ml) {
2894 sys::swapByteOrder(ml.entsize);
2895 sys::swapByteOrder(ml.count);
2898 inline void swapStruct(struct method64_t &m) {
2899 sys::swapByteOrder(m.name);
2900 sys::swapByteOrder(m.types);
2901 sys::swapByteOrder(m.imp);
2904 inline void swapStruct(struct method32_t &m) {
2905 sys::swapByteOrder(m.name);
2906 sys::swapByteOrder(m.types);
2907 sys::swapByteOrder(m.imp);
2910 inline void swapStruct(struct protocol_list64_t &pl) {
2911 sys::swapByteOrder(pl.count);
2914 inline void swapStruct(struct protocol_list32_t &pl) {
2915 sys::swapByteOrder(pl.count);
2918 inline void swapStruct(struct protocol64_t &p) {
2919 sys::swapByteOrder(p.isa);
2920 sys::swapByteOrder(p.name);
2921 sys::swapByteOrder(p.protocols);
2922 sys::swapByteOrder(p.instanceMethods);
2923 sys::swapByteOrder(p.classMethods);
2924 sys::swapByteOrder(p.optionalInstanceMethods);
2925 sys::swapByteOrder(p.optionalClassMethods);
2926 sys::swapByteOrder(p.instanceProperties);
2929 inline void swapStruct(struct protocol32_t &p) {
2930 sys::swapByteOrder(p.isa);
2931 sys::swapByteOrder(p.name);
2932 sys::swapByteOrder(p.protocols);
2933 sys::swapByteOrder(p.instanceMethods);
2934 sys::swapByteOrder(p.classMethods);
2935 sys::swapByteOrder(p.optionalInstanceMethods);
2936 sys::swapByteOrder(p.optionalClassMethods);
2937 sys::swapByteOrder(p.instanceProperties);
2940 inline void swapStruct(struct ivar_list64_t &il) {
2941 sys::swapByteOrder(il.entsize);
2942 sys::swapByteOrder(il.count);
2945 inline void swapStruct(struct ivar_list32_t &il) {
2946 sys::swapByteOrder(il.entsize);
2947 sys::swapByteOrder(il.count);
2950 inline void swapStruct(struct ivar64_t &i) {
2951 sys::swapByteOrder(i.offset);
2952 sys::swapByteOrder(i.name);
2953 sys::swapByteOrder(i.type);
2954 sys::swapByteOrder(i.alignment);
2955 sys::swapByteOrder(i.size);
2958 inline void swapStruct(struct ivar32_t &i) {
2959 sys::swapByteOrder(i.offset);
2960 sys::swapByteOrder(i.name);
2961 sys::swapByteOrder(i.type);
2962 sys::swapByteOrder(i.alignment);
2963 sys::swapByteOrder(i.size);
2966 inline void swapStruct(struct objc_property_list64 &pl) {
2967 sys::swapByteOrder(pl.entsize);
2968 sys::swapByteOrder(pl.count);
2971 inline void swapStruct(struct objc_property_list32 &pl) {
2972 sys::swapByteOrder(pl.entsize);
2973 sys::swapByteOrder(pl.count);
2976 inline void swapStruct(struct objc_property64 &op) {
2977 sys::swapByteOrder(op.name);
2978 sys::swapByteOrder(op.attributes);
2981 inline void swapStruct(struct objc_property32 &op) {
2982 sys::swapByteOrder(op.name);
2983 sys::swapByteOrder(op.attributes);
2986 inline void swapStruct(struct category64_t &c) {
2987 sys::swapByteOrder(c.name);
2988 sys::swapByteOrder(c.cls);
2989 sys::swapByteOrder(c.instanceMethods);
2990 sys::swapByteOrder(c.classMethods);
2991 sys::swapByteOrder(c.protocols);
2992 sys::swapByteOrder(c.instanceProperties);
2995 inline void swapStruct(struct category32_t &c) {
2996 sys::swapByteOrder(c.name);
2997 sys::swapByteOrder(c.cls);
2998 sys::swapByteOrder(c.instanceMethods);
2999 sys::swapByteOrder(c.classMethods);
3000 sys::swapByteOrder(c.protocols);
3001 sys::swapByteOrder(c.instanceProperties);
3004 inline void swapStruct(struct objc_image_info64 &o) {
3005 sys::swapByteOrder(o.version);
3006 sys::swapByteOrder(o.flags);
3009 inline void swapStruct(struct objc_image_info32 &o) {
3010 sys::swapByteOrder(o.version);
3011 sys::swapByteOrder(o.flags);
3014 inline void swapStruct(struct imageInfo_t &o) {
3015 sys::swapByteOrder(o.version);
3016 sys::swapByteOrder(o.flags);
3019 inline void swapStruct(struct message_ref64 &mr) {
3020 sys::swapByteOrder(mr.imp);
3021 sys::swapByteOrder(mr.sel);
3024 inline void swapStruct(struct message_ref32 &mr) {
3025 sys::swapByteOrder(mr.imp);
3026 sys::swapByteOrder(mr.sel);
3029 inline void swapStruct(struct objc_module_t &module) {
3030 sys::swapByteOrder(module.version);
3031 sys::swapByteOrder(module.size);
3032 sys::swapByteOrder(module.name);
3033 sys::swapByteOrder(module.symtab);
3036 inline void swapStruct(struct objc_symtab_t &symtab) {
3037 sys::swapByteOrder(symtab.sel_ref_cnt);
3038 sys::swapByteOrder(symtab.refs);
3039 sys::swapByteOrder(symtab.cls_def_cnt);
3040 sys::swapByteOrder(symtab.cat_def_cnt);
3043 inline void swapStruct(struct objc_class_t &objc_class) {
3044 sys::swapByteOrder(objc_class.isa);
3045 sys::swapByteOrder(objc_class.super_class);
3046 sys::swapByteOrder(objc_class.name);
3047 sys::swapByteOrder(objc_class.version);
3048 sys::swapByteOrder(objc_class.info);
3049 sys::swapByteOrder(objc_class.instance_size);
3050 sys::swapByteOrder(objc_class.ivars);
3051 sys::swapByteOrder(objc_class.methodLists);
3052 sys::swapByteOrder(objc_class.cache);
3053 sys::swapByteOrder(objc_class.protocols);
3056 inline void swapStruct(struct objc_category_t &objc_category) {
3057 sys::swapByteOrder(objc_category.category_name);
3058 sys::swapByteOrder(objc_category.class_name);
3059 sys::swapByteOrder(objc_category.instance_methods);
3060 sys::swapByteOrder(objc_category.class_methods);
3061 sys::swapByteOrder(objc_category.protocols);
3064 inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3065 sys::swapByteOrder(objc_ivar_list.ivar_count);
3068 inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3069 sys::swapByteOrder(objc_ivar.ivar_name);
3070 sys::swapByteOrder(objc_ivar.ivar_type);
3071 sys::swapByteOrder(objc_ivar.ivar_offset);
3074 inline void swapStruct(struct objc_method_list_t &method_list) {
3075 sys::swapByteOrder(method_list.obsolete);
3076 sys::swapByteOrder(method_list.method_count);
3079 inline void swapStruct(struct objc_method_t &method) {
3080 sys::swapByteOrder(method.method_name);
3081 sys::swapByteOrder(method.method_types);
3082 sys::swapByteOrder(method.method_imp);
3085 inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3086 sys::swapByteOrder(protocol_list.next);
3087 sys::swapByteOrder(protocol_list.count);
3090 inline void swapStruct(struct objc_protocol_t &protocol) {
3091 sys::swapByteOrder(protocol.isa);
3092 sys::swapByteOrder(protocol.protocol_name);
3093 sys::swapByteOrder(protocol.protocol_list);
3094 sys::swapByteOrder(protocol.instance_methods);
3095 sys::swapByteOrder(protocol.class_methods);
3098 inline void swapStruct(struct objc_method_description_list_t &mdl) {
3099 sys::swapByteOrder(mdl.count);
3102 inline void swapStruct(struct objc_method_description_t &md) {
3103 sys::swapByteOrder(md.name);
3104 sys::swapByteOrder(md.types);
3107 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3108 struct DisassembleInfo *info);
3110 // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3111 // to an Objective-C class and returns the class name. It is also passed the
3112 // address of the pointer, so when the pointer is zero as it can be in an .o
3113 // file, that is used to look for an external relocation entry with a symbol
3115 static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3116 uint64_t ReferenceValue,
3117 struct DisassembleInfo *info) {
3119 uint32_t offset, left;
3122 // The pointer_value can be 0 in an object file and have a relocation
3123 // entry for the class symbol at the ReferenceValue (the address of the
3125 if (pointer_value == 0) {
3126 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3127 if (r == nullptr || left < sizeof(uint64_t))
3130 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3131 if (symbol_name == nullptr)
3133 const char *class_name = strrchr(symbol_name, '$');
3134 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3135 return class_name + 2;
3140 // The case were the pointer_value is non-zero and points to a class defined
3141 // in this Mach-O file.
3142 r = get_pointer_64(pointer_value, offset, left, S, info);
3143 if (r == nullptr || left < sizeof(struct class64_t))
3146 memcpy(&c, r, sizeof(struct class64_t));
3147 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3151 r = get_pointer_64(c.data, offset, left, S, info);
3152 if (r == nullptr || left < sizeof(struct class_ro64_t))
3154 struct class_ro64_t cro;
3155 memcpy(&cro, r, sizeof(struct class_ro64_t));
3156 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3160 const char *name = get_pointer_64(cro.name, offset, left, S, info);
3164 // get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3165 // pointer to a cfstring and returns its name or nullptr.
3166 static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3167 struct DisassembleInfo *info) {
3168 const char *r, *name;
3169 uint32_t offset, left;
3171 struct cfstring64_t cfs;
3172 uint64_t cfs_characters;
3174 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3175 if (r == nullptr || left < sizeof(struct cfstring64_t))
3177 memcpy(&cfs, r, sizeof(struct cfstring64_t));
3178 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3180 if (cfs.characters == 0) {
3182 const char *symbol_name = get_symbol_64(
3183 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
3184 if (symbol_name == nullptr)
3186 cfs_characters = n_value;
3188 cfs_characters = cfs.characters;
3189 name = get_pointer_64(cfs_characters, offset, left, S, info);
3194 // get_objc2_64bit_selref() is used for disassembly and is passed a the address
3195 // of a pointer to an Objective-C selector reference when the pointer value is
3196 // zero as in a .o file and is likely to have a external relocation entry with
3197 // who's symbol's n_value is the real pointer to the selector name. If that is
3198 // the case the real pointer to the selector name is returned else 0 is
3200 static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3201 struct DisassembleInfo *info) {
3202 uint32_t offset, left;
3205 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3206 if (r == nullptr || left < sizeof(uint64_t))
3209 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3210 if (symbol_name == nullptr)
3215 static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3216 const char *sectname) {
3217 for (const SectionRef &Section : O->sections()) {
3219 Section.getName(SectName);
3220 DataRefImpl Ref = Section.getRawDataRefImpl();
3221 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3222 if (SegName == segname && SectName == sectname)
3225 return SectionRef();
3229 walk_pointer_list_64(const char *listname, const SectionRef S,
3230 MachOObjectFile *O, struct DisassembleInfo *info,
3231 void (*func)(uint64_t, struct DisassembleInfo *info)) {
3232 if (S == SectionRef())
3236 S.getName(SectName);
3237 DataRefImpl Ref = S.getRawDataRefImpl();
3238 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3239 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3242 S.getContents(BytesStr);
3243 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3245 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3246 uint32_t left = S.getSize() - i;
3247 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3249 memcpy(&p, Contents + i, size);
3250 if (i + sizeof(uint64_t) > S.getSize())
3251 outs() << listname << " list pointer extends past end of (" << SegName
3252 << "," << SectName << ") section\n";
3253 outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
3255 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3256 sys::swapByteOrder(p);
3258 uint64_t n_value = 0;
3259 const char *name = get_symbol_64(i, S, info, n_value, p);
3260 if (name == nullptr)
3261 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
3264 outs() << format("0x%" PRIx64, n_value);
3266 outs() << " + " << format("0x%" PRIx64, p);
3268 outs() << format("0x%" PRIx64, p);
3269 if (name != nullptr)
3270 outs() << " " << name;
3280 walk_pointer_list_32(const char *listname, const SectionRef S,
3281 MachOObjectFile *O, struct DisassembleInfo *info,
3282 void (*func)(uint32_t, struct DisassembleInfo *info)) {
3283 if (S == SectionRef())
3287 S.getName(SectName);
3288 DataRefImpl Ref = S.getRawDataRefImpl();
3289 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3290 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3293 S.getContents(BytesStr);
3294 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3296 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
3297 uint32_t left = S.getSize() - i;
3298 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
3300 memcpy(&p, Contents + i, size);
3301 if (i + sizeof(uint32_t) > S.getSize())
3302 outs() << listname << " list pointer extends past end of (" << SegName
3303 << "," << SectName << ") section\n";
3304 uint32_t Address = S.getAddress() + i;
3305 outs() << format("%08" PRIx32, Address) << " ";
3307 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3308 sys::swapByteOrder(p);
3309 outs() << format("0x%" PRIx32, p);
3311 const char *name = get_symbol_32(i, S, info, p);
3312 if (name != nullptr)
3313 outs() << " " << name;
3321 static void print_layout_map(const char *layout_map, uint32_t left) {
3322 outs() << " layout map: ";
3324 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
3327 } while (*layout_map != '\0' && left != 0);
3331 static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
3332 uint32_t offset, left;
3334 const char *layout_map;
3338 layout_map = get_pointer_64(p, offset, left, S, info);
3339 print_layout_map(layout_map, left);
3342 static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
3343 uint32_t offset, left;
3345 const char *layout_map;
3349 layout_map = get_pointer_32(p, offset, left, S, info);
3350 print_layout_map(layout_map, left);
3353 static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
3354 const char *indent) {
3355 struct method_list64_t ml;
3356 struct method64_t m;
3358 uint32_t offset, xoffset, left, i;
3360 const char *name, *sym_name;
3363 r = get_pointer_64(p, offset, left, S, info);
3366 memset(&ml, '\0', sizeof(struct method_list64_t));
3367 if (left < sizeof(struct method_list64_t)) {
3368 memcpy(&ml, r, left);
3369 outs() << " (method_list_t entends past the end of the section)\n";
3371 memcpy(&ml, r, sizeof(struct method_list64_t));
3372 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3374 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3375 outs() << indent << "\t\t count " << ml.count << "\n";
3377 p += sizeof(struct method_list64_t);
3378 offset += sizeof(struct method_list64_t);
3379 for (i = 0; i < ml.count; i++) {
3380 r = get_pointer_64(p, offset, left, S, info);
3383 memset(&m, '\0', sizeof(struct method64_t));
3384 if (left < sizeof(struct method64_t)) {
3385 memcpy(&ml, r, left);
3386 outs() << indent << " (method_t entends past the end of the section)\n";
3388 memcpy(&m, r, sizeof(struct method64_t));
3389 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3392 outs() << indent << "\t\t name ";
3393 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
3394 info, n_value, m.name);
3396 if (info->verbose && sym_name != nullptr)
3399 outs() << format("0x%" PRIx64, n_value);
3401 outs() << " + " << format("0x%" PRIx64, m.name);
3403 outs() << format("0x%" PRIx64, m.name);
3404 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
3405 if (name != nullptr)
3406 outs() << format(" %.*s", left, name);
3409 outs() << indent << "\t\t types ";
3410 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
3411 info, n_value, m.types);
3413 if (info->verbose && sym_name != nullptr)
3416 outs() << format("0x%" PRIx64, n_value);
3418 outs() << " + " << format("0x%" PRIx64, m.types);
3420 outs() << format("0x%" PRIx64, m.types);
3421 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
3422 if (name != nullptr)
3423 outs() << format(" %.*s", left, name);
3426 outs() << indent << "\t\t imp ";
3427 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
3429 if (info->verbose && name == nullptr) {
3431 outs() << format("0x%" PRIx64, n_value) << " ";
3433 outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
3435 outs() << format("0x%" PRIx64, m.imp) << " ";
3437 if (name != nullptr)
3441 p += sizeof(struct method64_t);
3442 offset += sizeof(struct method64_t);
3446 static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
3447 const char *indent) {
3448 struct method_list32_t ml;
3449 struct method32_t m;
3450 const char *r, *name;
3451 uint32_t offset, xoffset, left, i;
3454 r = get_pointer_32(p, offset, left, S, info);
3457 memset(&ml, '\0', sizeof(struct method_list32_t));
3458 if (left < sizeof(struct method_list32_t)) {
3459 memcpy(&ml, r, left);
3460 outs() << " (method_list_t entends past the end of the section)\n";
3462 memcpy(&ml, r, sizeof(struct method_list32_t));
3463 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3465 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3466 outs() << indent << "\t\t count " << ml.count << "\n";
3468 p += sizeof(struct method_list32_t);
3469 offset += sizeof(struct method_list32_t);
3470 for (i = 0; i < ml.count; i++) {
3471 r = get_pointer_32(p, offset, left, S, info);
3474 memset(&m, '\0', sizeof(struct method32_t));
3475 if (left < sizeof(struct method32_t)) {
3476 memcpy(&ml, r, left);
3477 outs() << indent << " (method_t entends past the end of the section)\n";
3479 memcpy(&m, r, sizeof(struct method32_t));
3480 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3483 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
3484 name = get_pointer_32(m.name, xoffset, left, xS, info);
3485 if (name != nullptr)
3486 outs() << format(" %.*s", left, name);
3489 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
3490 name = get_pointer_32(m.types, xoffset, left, xS, info);
3491 if (name != nullptr)
3492 outs() << format(" %.*s", left, name);
3495 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
3496 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
3498 if (name != nullptr)
3499 outs() << " " << name;
3502 p += sizeof(struct method32_t);
3503 offset += sizeof(struct method32_t);
3507 static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
3508 uint32_t offset, left, xleft;
3510 struct objc_method_list_t method_list;
3511 struct objc_method_t method;
3512 const char *r, *methods, *name, *SymbolName;
3515 r = get_pointer_32(p, offset, left, S, info, true);
3520 if (left > sizeof(struct objc_method_list_t)) {
3521 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
3523 outs() << "\t\t objc_method_list extends past end of the section\n";
3524 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
3525 memcpy(&method_list, r, left);
3527 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3528 swapStruct(method_list);
3530 outs() << "\t\t obsolete "
3531 << format("0x%08" PRIx32, method_list.obsolete) << "\n";
3532 outs() << "\t\t method_count " << method_list.method_count << "\n";
3534 methods = r + sizeof(struct objc_method_list_t);
3535 for (i = 0; i < method_list.method_count; i++) {
3536 if ((i + 1) * sizeof(struct objc_method_t) > left) {
3537 outs() << "\t\t remaining method's extend past the of the section\n";
3540 memcpy(&method, methods + i * sizeof(struct objc_method_t),
3541 sizeof(struct objc_method_t));
3542 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3545 outs() << "\t\t method_name "
3546 << format("0x%08" PRIx32, method.method_name);
3547 if (info->verbose) {
3548 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
3549 if (name != nullptr)
3550 outs() << format(" %.*s", xleft, name);
3552 outs() << " (not in an __OBJC section)";
3556 outs() << "\t\t method_types "
3557 << format("0x%08" PRIx32, method.method_types);
3558 if (info->verbose) {
3559 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
3560 if (name != nullptr)
3561 outs() << format(" %.*s", xleft, name);
3563 outs() << " (not in an __OBJC section)";
3567 outs() << "\t\t method_imp "
3568 << format("0x%08" PRIx32, method.method_imp) << " ";
3569 if (info->verbose) {
3570 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
3571 if (SymbolName != nullptr)
3572 outs() << SymbolName;
3579 static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
3580 struct protocol_list64_t pl;
3581 uint64_t q, n_value;
3582 struct protocol64_t pc;
3584 uint32_t offset, xoffset, left, i;
3586 const char *name, *sym_name;
3588 r = get_pointer_64(p, offset, left, S, info);
3591 memset(&pl, '\0', sizeof(struct protocol_list64_t));
3592 if (left < sizeof(struct protocol_list64_t)) {
3593 memcpy(&pl, r, left);
3594 outs() << " (protocol_list_t entends past the end of the section)\n";
3596 memcpy(&pl, r, sizeof(struct protocol_list64_t));
3597 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3599 outs() << " count " << pl.count << "\n";
3601 p += sizeof(struct protocol_list64_t);
3602 offset += sizeof(struct protocol_list64_t);
3603 for (i = 0; i < pl.count; i++) {
3604 r = get_pointer_64(p, offset, left, S, info);
3608 if (left < sizeof(uint64_t)) {
3609 memcpy(&q, r, left);
3610 outs() << " (protocol_t * entends past the end of the section)\n";
3612 memcpy(&q, r, sizeof(uint64_t));
3613 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3614 sys::swapByteOrder(q);
3616 outs() << "\t\t list[" << i << "] ";
3617 sym_name = get_symbol_64(offset, S, info, n_value, q);
3619 if (info->verbose && sym_name != nullptr)
3622 outs() << format("0x%" PRIx64, n_value);
3624 outs() << " + " << format("0x%" PRIx64, q);
3626 outs() << format("0x%" PRIx64, q);
3627 outs() << " (struct protocol_t *)\n";
3629 r = get_pointer_64(q + n_value, offset, left, S, info);
3632 memset(&pc, '\0', sizeof(struct protocol64_t));
3633 if (left < sizeof(struct protocol64_t)) {
3634 memcpy(&pc, r, left);
3635 outs() << " (protocol_t entends past the end of the section)\n";
3637 memcpy(&pc, r, sizeof(struct protocol64_t));
3638 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3641 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
3643 outs() << "\t\t\t name ";
3644 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
3645 info, n_value, pc.name);
3647 if (info->verbose && sym_name != nullptr)
3650 outs() << format("0x%" PRIx64, n_value);
3652 outs() << " + " << format("0x%" PRIx64, pc.name);
3654 outs() << format("0x%" PRIx64, pc.name);
3655 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
3656 if (name != nullptr)
3657 outs() << format(" %.*s", left, name);
3660 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
3662 outs() << "\t\t instanceMethods ";
3664 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
3665 S, info, n_value, pc.instanceMethods);
3667 if (info->verbose && sym_name != nullptr)
3670 outs() << format("0x%" PRIx64, n_value);
3671 if (pc.instanceMethods != 0)
3672 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
3674 outs() << format("0x%" PRIx64, pc.instanceMethods);
3675 outs() << " (struct method_list_t *)\n";
3676 if (pc.instanceMethods + n_value != 0)
3677 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
3679 outs() << "\t\t classMethods ";
3681 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
3682 info, n_value, pc.classMethods);
3684 if (info->verbose && sym_name != nullptr)
3687 outs() << format("0x%" PRIx64, n_value);
3688 if (pc.classMethods != 0)
3689 outs() << " + " << format("0x%" PRIx64, pc.classMethods);
3691 outs() << format("0x%" PRIx64, pc.classMethods);
3692 outs() << " (struct method_list_t *)\n";
3693 if (pc.classMethods + n_value != 0)
3694 print_method_list64_t(pc.classMethods + n_value, info, "\t");
3696 outs() << "\t optionalInstanceMethods "
3697 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
3698 outs() << "\t optionalClassMethods "
3699 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
3700 outs() << "\t instanceProperties "
3701 << format("0x%" PRIx64, pc.instanceProperties) << "\n";
3703 p += sizeof(uint64_t);
3704 offset += sizeof(uint64_t);
3708 static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
3709 struct protocol_list32_t pl;
3711 struct protocol32_t pc;
3713 uint32_t offset, xoffset, left, i;
3717 r = get_pointer_32(p, offset, left, S, info);
3720 memset(&pl, '\0', sizeof(struct protocol_list32_t));
3721 if (left < sizeof(struct protocol_list32_t)) {
3722 memcpy(&pl, r, left);
3723 outs() << " (protocol_list_t entends past the end of the section)\n";
3725 memcpy(&pl, r, sizeof(struct protocol_list32_t));
3726 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3728 outs() << " count " << pl.count << "\n";
3730 p += sizeof(struct protocol_list32_t);
3731 offset += sizeof(struct protocol_list32_t);
3732 for (i = 0; i < pl.count; i++) {
3733 r = get_pointer_32(p, offset, left, S, info);
3737 if (left < sizeof(uint32_t)) {
3738 memcpy(&q, r, left);
3739 outs() << " (protocol_t * entends past the end of the section)\n";
3741 memcpy(&q, r, sizeof(uint32_t));
3742 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3743 sys::swapByteOrder(q);
3744 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
3745 << " (struct protocol_t *)\n";
3746 r = get_pointer_32(q, offset, left, S, info);
3749 memset(&pc, '\0', sizeof(struct protocol32_t));
3750 if (left < sizeof(struct protocol32_t)) {
3751 memcpy(&pc, r, left);
3752 outs() << " (protocol_t entends past the end of the section)\n";
3754 memcpy(&pc, r, sizeof(struct protocol32_t));
3755 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3757 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
3758 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
3759 name = get_pointer_32(pc.name, xoffset, left, xS, info);
3760 if (name != nullptr)
3761 outs() << format(" %.*s", left, name);
3763 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
3764 outs() << "\t\t instanceMethods "
3765 << format("0x%" PRIx32, pc.instanceMethods)
3766 << " (struct method_list_t *)\n";
3767 if (pc.instanceMethods != 0)
3768 print_method_list32_t(pc.instanceMethods, info, "\t");
3769 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
3770 << " (struct method_list_t *)\n";
3771 if (pc.classMethods != 0)
3772 print_method_list32_t(pc.classMethods, info, "\t");
3773 outs() << "\t optionalInstanceMethods "
3774 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
3775 outs() << "\t optionalClassMethods "
3776 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
3777 outs() << "\t instanceProperties "
3778 << format("0x%" PRIx32, pc.instanceProperties) << "\n";
3779 p += sizeof(uint32_t);
3780 offset += sizeof(uint32_t);
3784 static void print_indent(uint32_t indent) {
3785 for (uint32_t i = 0; i < indent;) {
3786 if (indent - i >= 8) {
3790 for (uint32_t j = i; j < indent; j++)
3797 static bool print_method_description_list(uint32_t p, uint32_t indent,
3798 struct DisassembleInfo *info) {
3799 uint32_t offset, left, xleft;
3801 struct objc_method_description_list_t mdl;
3802 struct objc_method_description_t md;
3803 const char *r, *list, *name;
3806 r = get_pointer_32(p, offset, left, S, info, true);
3811 if (left > sizeof(struct objc_method_description_list_t)) {
3812 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
3814 print_indent(indent);
3815 outs() << " objc_method_description_list extends past end of the section\n";
3816 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
3817 memcpy(&mdl, r, left);
3819 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3822 print_indent(indent);
3823 outs() << " count " << mdl.count << "\n";
3825 list = r + sizeof(struct objc_method_description_list_t);
3826 for (i = 0; i < mdl.count; i++) {
3827 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
3828 print_indent(indent);
3829 outs() << " remaining list entries extend past the of the section\n";
3832 print_indent(indent);
3833 outs() << " list[" << i << "]\n";
3834 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
3835 sizeof(struct objc_method_description_t));
3836 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3839 print_indent(indent);
3840 outs() << " name " << format("0x%08" PRIx32, md.name);
3841 if (info->verbose) {
3842 name = get_pointer_32(md.name, offset, xleft, S, info, true);
3843 if (name != nullptr)
3844 outs() << format(" %.*s", xleft, name);
3846 outs() << " (not in an __OBJC section)";
3850 print_indent(indent);
3851 outs() << " types " << format("0x%08" PRIx32, md.types);
3852 if (info->verbose) {
3853 name = get_pointer_32(md.types, offset, xleft, S, info, true);
3854 if (name != nullptr)
3855 outs() << format(" %.*s", xleft, name);
3857 outs() << " (not in an __OBJC section)";
3864 static bool print_protocol_list(uint32_t p, uint32_t indent,
3865 struct DisassembleInfo *info);
3867 static bool print_protocol(uint32_t p, uint32_t indent,
3868 struct DisassembleInfo *info) {
3869 uint32_t offset, left;
3871 struct objc_protocol_t protocol;
3872 const char *r, *name;
3874 r = get_pointer_32(p, offset, left, S, info, true);
3879 if (left >= sizeof(struct objc_protocol_t)) {
3880 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
3882 print_indent(indent);
3883 outs() << " Protocol extends past end of the section\n";
3884 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
3885 memcpy(&protocol, r, left);
3887 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3888 swapStruct(protocol);
3890 print_indent(indent);
3891 outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
3894 print_indent(indent);
3895 outs() << " protocol_name "
3896 << format("0x%08" PRIx32, protocol.protocol_name);
3897 if (info->verbose) {
3898 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
3899 if (name != nullptr)
3900 outs() << format(" %.*s", left, name);
3902 outs() << " (not in an __OBJC section)";
3906 print_indent(indent);
3907 outs() << " protocol_list "
3908 << format("0x%08" PRIx32, protocol.protocol_list);
3909 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
3910 outs() << " (not in an __OBJC section)\n";
3912 print_indent(indent);
3913 outs() << " instance_methods "
3914 << format("0x%08" PRIx32, protocol.instance_methods);
3915 if (print_method_description_list(protocol.instance_methods, indent, info))
3916 outs() << " (not in an __OBJC section)\n";
3918 print_indent(indent);
3919 outs() << " class_methods "
3920 << format("0x%08" PRIx32, protocol.class_methods);
3921 if (print_method_description_list(protocol.class_methods, indent, info))
3922 outs() << " (not in an __OBJC section)\n";
3927 static bool print_protocol_list(uint32_t p, uint32_t indent,
3928 struct DisassembleInfo *info) {
3929 uint32_t offset, left, l;
3931 struct objc_protocol_list_t protocol_list;
3932 const char *r, *list;
3935 r = get_pointer_32(p, offset, left, S, info, true);
3940 if (left > sizeof(struct objc_protocol_list_t)) {
3941 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
3943 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
3944 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
3945 memcpy(&protocol_list, r, left);
3947 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3948 swapStruct(protocol_list);
3950 print_indent(indent);
3951 outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
3953 print_indent(indent);
3954 outs() << " count " << protocol_list.count << "\n";
3956 list = r + sizeof(struct objc_protocol_list_t);
3957 for (i = 0; i < protocol_list.count; i++) {
3958 if ((i + 1) * sizeof(uint32_t) > left) {
3959 outs() << "\t\t remaining list entries extend past the of the section\n";
3962 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
3963 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3964 sys::swapByteOrder(l);
3966 print_indent(indent);
3967 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
3968 if (print_protocol(l, indent, info))
3969 outs() << "(not in an __OBJC section)\n";
3974 static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
3975 struct ivar_list64_t il;
3978 uint32_t offset, xoffset, left, j;
3980 const char *name, *sym_name, *ivar_offset_p;
3981 uint64_t ivar_offset, n_value;
3983 r = get_pointer_64(p, offset, left, S, info);
3986 memset(&il, '\0', sizeof(struct ivar_list64_t));
3987 if (left < sizeof(struct ivar_list64_t)) {
3988 memcpy(&il, r, left);
3989 outs() << " (ivar_list_t entends past the end of the section)\n";
3991 memcpy(&il, r, sizeof(struct ivar_list64_t));
3992 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3994 outs() << " entsize " << il.entsize << "\n";
3995 outs() << " count " << il.count << "\n";
3997 p += sizeof(struct ivar_list64_t);
3998 offset += sizeof(struct ivar_list64_t);
3999 for (j = 0; j < il.count; j++) {
4000 r = get_pointer_64(p, offset, left, S, info);
4003 memset(&i, '\0', sizeof(struct ivar64_t));
4004 if (left < sizeof(struct ivar64_t)) {
4005 memcpy(&i, r, left);
4006 outs() << " (ivar_t entends past the end of the section)\n";
4008 memcpy(&i, r, sizeof(struct ivar64_t));
4009 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4012 outs() << "\t\t\t offset ";
4013 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
4014 info, n_value, i.offset);
4016 if (info->verbose && sym_name != nullptr)
4019 outs() << format("0x%" PRIx64, n_value);
4021 outs() << " + " << format("0x%" PRIx64, i.offset);
4023 outs() << format("0x%" PRIx64, i.offset);
4024 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
4025 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4026 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4027 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4028 sys::swapByteOrder(ivar_offset);
4029 outs() << " " << ivar_offset << "\n";
4033 outs() << "\t\t\t name ";
4034 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
4037 if (info->verbose && sym_name != nullptr)
4040 outs() << format("0x%" PRIx64, n_value);
4042 outs() << " + " << format("0x%" PRIx64, i.name);
4044 outs() << format("0x%" PRIx64, i.name);
4045 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
4046 if (name != nullptr)
4047 outs() << format(" %.*s", left, name);
4050 outs() << "\t\t\t type ";
4051 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
4053 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
4055 if (info->verbose && sym_name != nullptr)
4058 outs() << format("0x%" PRIx64, n_value);
4060 outs() << " + " << format("0x%" PRIx64, i.type);
4062 outs() << format("0x%" PRIx64, i.type);
4063 if (name != nullptr)
4064 outs() << format(" %.*s", left, name);
4067 outs() << "\t\t\talignment " << i.alignment << "\n";
4068 outs() << "\t\t\t size " << i.size << "\n";
4070 p += sizeof(struct ivar64_t);
4071 offset += sizeof(struct ivar64_t);
4075 static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
4076 struct ivar_list32_t il;
4079 uint32_t offset, xoffset, left, j;
4081 const char *name, *ivar_offset_p;
4082 uint32_t ivar_offset;
4084 r = get_pointer_32(p, offset, left, S, info);
4087 memset(&il, '\0', sizeof(struct ivar_list32_t));
4088 if (left < sizeof(struct ivar_list32_t)) {
4089 memcpy(&il, r, left);
4090 outs() << " (ivar_list_t entends past the end of the section)\n";
4092 memcpy(&il, r, sizeof(struct ivar_list32_t));
4093 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4095 outs() << " entsize " << il.entsize << "\n";
4096 outs() << " count " << il.count << "\n";
4098 p += sizeof(struct ivar_list32_t);
4099 offset += sizeof(struct ivar_list32_t);
4100 for (j = 0; j < il.count; j++) {
4101 r = get_pointer_32(p, offset, left, S, info);
4104 memset(&i, '\0', sizeof(struct ivar32_t));
4105 if (left < sizeof(struct ivar32_t)) {
4106 memcpy(&i, r, left);
4107 outs() << " (ivar_t entends past the end of the section)\n";
4109 memcpy(&i, r, sizeof(struct ivar32_t));
4110 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4113 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
4114 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4115 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4116 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4117 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4118 sys::swapByteOrder(ivar_offset);
4119 outs() << " " << ivar_offset << "\n";
4123 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
4124 name = get_pointer_32(i.name, xoffset, left, xS, info);
4125 if (name != nullptr)
4126 outs() << format(" %.*s", left, name);
4129 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
4130 name = get_pointer_32(i.type, xoffset, left, xS, info);
4131 if (name != nullptr)
4132 outs() << format(" %.*s", left, name);
4135 outs() << "\t\t\talignment " << i.alignment << "\n";
4136 outs() << "\t\t\t size " << i.size << "\n";
4138 p += sizeof(struct ivar32_t);
4139 offset += sizeof(struct ivar32_t);
4143 static void print_objc_property_list64(uint64_t p,
4144 struct DisassembleInfo *info) {
4145 struct objc_property_list64 opl;
4146 struct objc_property64 op;
4148 uint32_t offset, xoffset, left, j;
4150 const char *name, *sym_name;
4153 r = get_pointer_64(p, offset, left, S, info);
4156 memset(&opl, '\0', sizeof(struct objc_property_list64));
4157 if (left < sizeof(struct objc_property_list64)) {
4158 memcpy(&opl, r, left);
4159 outs() << " (objc_property_list entends past the end of the section)\n";
4161 memcpy(&opl, r, sizeof(struct objc_property_list64));
4162 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4164 outs() << " entsize " << opl.entsize << "\n";
4165 outs() << " count " << opl.count << "\n";
4167 p += sizeof(struct objc_property_list64);
4168 offset += sizeof(struct objc_property_list64);
4169 for (j = 0; j < opl.count; j++) {
4170 r = get_pointer_64(p, offset, left, S, info);
4173 memset(&op, '\0', sizeof(struct objc_property64));
4174 if (left < sizeof(struct objc_property64)) {
4175 memcpy(&op, r, left);
4176 outs() << " (objc_property entends past the end of the section)\n";
4178 memcpy(&op, r, sizeof(struct objc_property64));
4179 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4182 outs() << "\t\t\t name ";
4183 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
4184 info, n_value, op.name);
4186 if (info->verbose && sym_name != nullptr)
4189 outs() << format("0x%" PRIx64, n_value);
4191 outs() << " + " << format("0x%" PRIx64, op.name);
4193 outs() << format("0x%" PRIx64, op.name);
4194 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
4195 if (name != nullptr)
4196 outs() << format(" %.*s", left, name);
4199 outs() << "\t\t\tattributes ";
4201 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
4202 info, n_value, op.attributes);
4204 if (info->verbose && sym_name != nullptr)
4207 outs() << format("0x%" PRIx64, n_value);
4208 if (op.attributes != 0)
4209 outs() << " + " << format("0x%" PRIx64, op.attributes);
4211 outs() << format("0x%" PRIx64, op.attributes);
4212 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
4213 if (name != nullptr)
4214 outs() << format(" %.*s", left, name);
4217 p += sizeof(struct objc_property64);
4218 offset += sizeof(struct objc_property64);
4222 static void print_objc_property_list32(uint32_t p,
4223 struct DisassembleInfo *info) {
4224 struct objc_property_list32 opl;
4225 struct objc_property32 op;
4227 uint32_t offset, xoffset, left, j;
4231 r = get_pointer_32(p, offset, left, S, info);
4234 memset(&opl, '\0', sizeof(struct objc_property_list32));
4235 if (left < sizeof(struct objc_property_list32)) {
4236 memcpy(&opl, r, left);
4237 outs() << " (objc_property_list entends past the end of the section)\n";
4239 memcpy(&opl, r, sizeof(struct objc_property_list32));
4240 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4242 outs() << " entsize " << opl.entsize << "\n";
4243 outs() << " count " << opl.count << "\n";
4245 p += sizeof(struct objc_property_list32);
4246 offset += sizeof(struct objc_property_list32);
4247 for (j = 0; j < opl.count; j++) {
4248 r = get_pointer_32(p, offset, left, S, info);
4251 memset(&op, '\0', sizeof(struct objc_property32));
4252 if (left < sizeof(struct objc_property32)) {
4253 memcpy(&op, r, left);
4254 outs() << " (objc_property entends past the end of the section)\n";
4256 memcpy(&op, r, sizeof(struct objc_property32));
4257 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4260 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
4261 name = get_pointer_32(op.name, xoffset, left, xS, info);
4262 if (name != nullptr)
4263 outs() << format(" %.*s", left, name);
4266 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
4267 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
4268 if (name != nullptr)
4269 outs() << format(" %.*s", left, name);
4272 p += sizeof(struct objc_property32);
4273 offset += sizeof(struct objc_property32);
4277 static void print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
4278 bool &is_meta_class) {
4279 struct class_ro64_t cro;
4281 uint32_t offset, xoffset, left;
4283 const char *name, *sym_name;
4286 r = get_pointer_64(p, offset, left, S, info);
4287 if (r == nullptr || left < sizeof(struct class_ro64_t))
4289 memset(&cro, '\0', sizeof(struct class_ro64_t));
4290 if (left < sizeof(struct class_ro64_t)) {
4291 memcpy(&cro, r, left);
4292 outs() << " (class_ro_t entends past the end of the section)\n";
4294 memcpy(&cro, r, sizeof(struct class_ro64_t));
4295 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4297 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4298 if (cro.flags & RO_META)
4299 outs() << " RO_META";
4300 if (cro.flags & RO_ROOT)
4301 outs() << " RO_ROOT";
4302 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4303 outs() << " RO_HAS_CXX_STRUCTORS";
4305 outs() << " instanceStart " << cro.instanceStart << "\n";
4306 outs() << " instanceSize " << cro.instanceSize << "\n";
4307 outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
4309 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
4311 print_layout_map64(cro.ivarLayout, info);
4314 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
4315 info, n_value, cro.name);
4317 if (info->verbose && sym_name != nullptr)
4320 outs() << format("0x%" PRIx64, n_value);
4322 outs() << " + " << format("0x%" PRIx64, cro.name);
4324 outs() << format("0x%" PRIx64, cro.name);
4325 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
4326 if (name != nullptr)
4327 outs() << format(" %.*s", left, name);
4330 outs() << " baseMethods ";
4331 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
4332 S, info, n_value, cro.baseMethods);
4334 if (info->verbose && sym_name != nullptr)
4337 outs() << format("0x%" PRIx64, n_value);
4338 if (cro.baseMethods != 0)
4339 outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
4341 outs() << format("0x%" PRIx64, cro.baseMethods);
4342 outs() << " (struct method_list_t *)\n";
4343 if (cro.baseMethods + n_value != 0)
4344 print_method_list64_t(cro.baseMethods + n_value, info, "");
4346 outs() << " baseProtocols ";
4348 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
4349 info, n_value, cro.baseProtocols);
4351 if (info->verbose && sym_name != nullptr)
4354 outs() << format("0x%" PRIx64, n_value);
4355 if (cro.baseProtocols != 0)
4356 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
4358 outs() << format("0x%" PRIx64, cro.baseProtocols);
4360 if (cro.baseProtocols + n_value != 0)
4361 print_protocol_list64_t(cro.baseProtocols + n_value, info);
4363 outs() << " ivars ";
4364 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
4365 info, n_value, cro.ivars);
4367 if (info->verbose && sym_name != nullptr)
4370 outs() << format("0x%" PRIx64, n_value);
4372 outs() << " + " << format("0x%" PRIx64, cro.ivars);
4374 outs() << format("0x%" PRIx64, cro.ivars);
4376 if (cro.ivars + n_value != 0)
4377 print_ivar_list64_t(cro.ivars + n_value, info);
4379 outs() << " weakIvarLayout ";
4381 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
4382 info, n_value, cro.weakIvarLayout);
4384 if (info->verbose && sym_name != nullptr)
4387 outs() << format("0x%" PRIx64, n_value);
4388 if (cro.weakIvarLayout != 0)
4389 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
4391 outs() << format("0x%" PRIx64, cro.weakIvarLayout);
4393 print_layout_map64(cro.weakIvarLayout + n_value, info);
4395 outs() << " baseProperties ";
4397 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
4398 info, n_value, cro.baseProperties);
4400 if (info->verbose && sym_name != nullptr)
4403 outs() << format("0x%" PRIx64, n_value);
4404 if (cro.baseProperties != 0)
4405 outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
4407 outs() << format("0x%" PRIx64, cro.baseProperties);
4409 if (cro.baseProperties + n_value != 0)
4410 print_objc_property_list64(cro.baseProperties + n_value, info);
4412 is_meta_class = (cro.flags & RO_META) ? true : false;
4415 static void print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
4416 bool &is_meta_class) {
4417 struct class_ro32_t cro;
4419 uint32_t offset, xoffset, left;
4423 r = get_pointer_32(p, offset, left, S, info);
4426 memset(&cro, '\0', sizeof(struct class_ro32_t));
4427 if (left < sizeof(struct class_ro32_t)) {
4428 memcpy(&cro, r, left);
4429 outs() << " (class_ro_t entends past the end of the section)\n";
4431 memcpy(&cro, r, sizeof(struct class_ro32_t));
4432 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4434 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4435 if (cro.flags & RO_META)
4436 outs() << " RO_META";
4437 if (cro.flags & RO_ROOT)
4438 outs() << " RO_ROOT";
4439 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4440 outs() << " RO_HAS_CXX_STRUCTORS";
4442 outs() << " instanceStart " << cro.instanceStart << "\n";
4443 outs() << " instanceSize " << cro.instanceSize << "\n";
4444 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
4446 print_layout_map32(cro.ivarLayout, info);
4448 outs() << " name " << format("0x%" PRIx32, cro.name);
4449 name = get_pointer_32(cro.name, xoffset, left, xS, info);
4450 if (name != nullptr)
4451 outs() << format(" %.*s", left, name);
4454 outs() << " baseMethods "
4455 << format("0x%" PRIx32, cro.baseMethods)
4456 << " (struct method_list_t *)\n";
4457 if (cro.baseMethods != 0)
4458 print_method_list32_t(cro.baseMethods, info, "");
4460 outs() << " baseProtocols "
4461 << format("0x%" PRIx32, cro.baseProtocols) << "\n";
4462 if (cro.baseProtocols != 0)
4463 print_protocol_list32_t(cro.baseProtocols, info);
4464 outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
4467 print_ivar_list32_t(cro.ivars, info);
4468 outs() << " weakIvarLayout "
4469 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
4470 print_layout_map32(cro.weakIvarLayout, info);
4471 outs() << " baseProperties "
4472 << format("0x%" PRIx32, cro.baseProperties) << "\n";
4473 if (cro.baseProperties != 0)
4474 print_objc_property_list32(cro.baseProperties, info);
4475 is_meta_class = (cro.flags & RO_META) ? true : false;
4478 static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
4481 uint32_t offset, left;
4484 uint64_t isa_n_value, n_value;
4486 r = get_pointer_64(p, offset, left, S, info);
4487 if (r == nullptr || left < sizeof(struct class64_t))
4489 memset(&c, '\0', sizeof(struct class64_t));
4490 if (left < sizeof(struct class64_t)) {
4491 memcpy(&c, r, left);
4492 outs() << " (class_t entends past the end of the section)\n";
4494 memcpy(&c, r, sizeof(struct class64_t));
4495 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4498 outs() << " isa " << format("0x%" PRIx64, c.isa);
4499 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
4500 isa_n_value, c.isa);
4501 if (name != nullptr)
4502 outs() << " " << name;
4505 outs() << " superclass " << format("0x%" PRIx64, c.superclass);
4506 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
4507 n_value, c.superclass);
4508 if (name != nullptr)
4509 outs() << " " << name;
4512 outs() << " cache " << format("0x%" PRIx64, c.cache);
4513 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
4515 if (name != nullptr)
4516 outs() << " " << name;
4519 outs() << " vtable " << format("0x%" PRIx64, c.vtable);
4520 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
4522 if (name != nullptr)
4523 outs() << " " << name;
4526 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
4530 if (info->verbose && name != nullptr)
4533 outs() << format("0x%" PRIx64, n_value);
4535 outs() << " + " << format("0x%" PRIx64, c.data);
4537 outs() << format("0x%" PRIx64, c.data);
4538 outs() << " (struct class_ro_t *)";
4540 // This is a Swift class if some of the low bits of the pointer are set.
4541 if ((c.data + n_value) & 0x7)
4542 outs() << " Swift class";
4545 print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class);
4547 if (is_meta_class == false) {
4548 outs() << "Meta Class\n";
4549 print_class64_t(c.isa + isa_n_value, info);
4553 static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
4556 uint32_t offset, left;
4560 r = get_pointer_32(p, offset, left, S, info);
4563 memset(&c, '\0', sizeof(struct class32_t));
4564 if (left < sizeof(struct class32_t)) {
4565 memcpy(&c, r, left);
4566 outs() << " (class_t entends past the end of the section)\n";
4568 memcpy(&c, r, sizeof(struct class32_t));
4569 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4572 outs() << " isa " << format("0x%" PRIx32, c.isa);
4574 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
4575 if (name != nullptr)
4576 outs() << " " << name;
4579 outs() << " superclass " << format("0x%" PRIx32, c.superclass);
4580 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
4582 if (name != nullptr)
4583 outs() << " " << name;
4586 outs() << " cache " << format("0x%" PRIx32, c.cache);
4587 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
4589 if (name != nullptr)
4590 outs() << " " << name;
4593 outs() << " vtable " << format("0x%" PRIx32, c.vtable);
4594 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
4596 if (name != nullptr)
4597 outs() << " " << name;
4601 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
4602 outs() << " data " << format("0x%" PRIx32, c.data)
4603 << " (struct class_ro_t *)";
4605 // This is a Swift class if some of the low bits of the pointer are set.
4607 outs() << " Swift class";
4610 print_class_ro32_t(c.data & ~0x3, info, is_meta_class);
4612 if (is_meta_class == false) {
4613 outs() << "Meta Class\n";
4614 print_class32_t(c.isa, info);
4618 static void print_objc_class_t(struct objc_class_t *objc_class,
4619 struct DisassembleInfo *info) {
4620 uint32_t offset, left, xleft;
4621 const char *name, *p, *ivar_list;
4624 struct objc_ivar_list_t objc_ivar_list;
4625 struct objc_ivar_t ivar;
4627 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
4628 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
4629 name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
4630 if (name != nullptr)
4631 outs() << format(" %.*s", left, name);
4633 outs() << " (not in an __OBJC section)";
4637 outs() << "\t super_class "
4638 << format("0x%08" PRIx32, objc_class->super_class);
4639 if (info->verbose) {
4640 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
4641 if (name != nullptr)
4642 outs() << format(" %.*s", left, name);
4644 outs() << " (not in an __OBJC section)";
4648 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name);
4649 if (info->verbose) {
4650 name = get_pointer_32(objc_class->name, offset, left, S, info, true);
4651 if (name != nullptr)
4652 outs() << format(" %.*s", left, name);
4654 outs() << " (not in an __OBJC section)";
4658 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
4661 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
4662 if (info->verbose) {
4663 if (CLS_GETINFO(objc_class, CLS_CLASS))
4664 outs() << " CLS_CLASS";
4665 else if (CLS_GETINFO(objc_class, CLS_META))
4666 outs() << " CLS_META";
4670 outs() << "\t instance_size "
4671 << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
4673 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
4674 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
4676 if (left > sizeof(struct objc_ivar_list_t)) {
4678 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
4680 outs() << " (entends past the end of the section)\n";
4681 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
4682 memcpy(&objc_ivar_list, p, left);
4684 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4685 swapStruct(objc_ivar_list);
4686 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
4687 ivar_list = p + sizeof(struct objc_ivar_list_t);
4688 for (i = 0; i < objc_ivar_list.ivar_count; i++) {
4689 if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
4690 outs() << "\t\t remaining ivar's extend past the of the section\n";
4693 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
4694 sizeof(struct objc_ivar_t));
4695 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4698 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
4699 if (info->verbose) {
4700 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
4701 if (name != nullptr)
4702 outs() << format(" %.*s", xleft, name);
4704 outs() << " (not in an __OBJC section)";
4708 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
4709 if (info->verbose) {
4710 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
4711 if (name != nullptr)
4712 outs() << format(" %.*s", xleft, name);
4714 outs() << " (not in an __OBJC section)";
4718 outs() << "\t\t ivar_offset "
4719 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
4722 outs() << " (not in an __OBJC section)\n";
4725 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
4726 if (print_method_list(objc_class->methodLists, info))
4727 outs() << " (not in an __OBJC section)\n";
4729 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
4732 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
4733 if (print_protocol_list(objc_class->protocols, 16, info))
4734 outs() << " (not in an __OBJC section)\n";
4737 static void print_objc_objc_category_t(struct objc_category_t *objc_category,
4738 struct DisassembleInfo *info) {
4739 uint32_t offset, left;
4743 outs() << "\t category name "
4744 << format("0x%08" PRIx32, objc_category->category_name);
4745 if (info->verbose) {
4746 name = get_pointer_32(objc_category->category_name, offset, left, S, info,
4748 if (name != nullptr)
4749 outs() << format(" %.*s", left, name);
4751 outs() << " (not in an __OBJC section)";
4755 outs() << "\t\t class name "
4756 << format("0x%08" PRIx32, objc_category->class_name);
4757 if (info->verbose) {
4759 get_pointer_32(objc_category->class_name, offset, left, S, info, true);
4760 if (name != nullptr)
4761 outs() << format(" %.*s", left, name);
4763 outs() << " (not in an __OBJC section)";
4767 outs() << "\t instance methods "
4768 << format("0x%08" PRIx32, objc_category->instance_methods);
4769 if (print_method_list(objc_category->instance_methods, info))
4770 outs() << " (not in an __OBJC section)\n";
4772 outs() << "\t class methods "
4773 << format("0x%08" PRIx32, objc_category->class_methods);
4774 if (print_method_list(objc_category->class_methods, info))
4775 outs() << " (not in an __OBJC section)\n";
4778 static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
4779 struct category64_t c;
4781 uint32_t offset, xoffset, left;
4783 const char *name, *sym_name;
4786 r = get_pointer_64(p, offset, left, S, info);
4789 memset(&c, '\0', sizeof(struct category64_t));
4790 if (left < sizeof(struct category64_t)) {
4791 memcpy(&c, r, left);
4792 outs() << " (category_t entends past the end of the section)\n";
4794 memcpy(&c, r, sizeof(struct category64_t));
4795 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4799 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
4800 info, n_value, c.name);
4802 if (info->verbose && sym_name != nullptr)
4805 outs() << format("0x%" PRIx64, n_value);
4807 outs() << " + " << format("0x%" PRIx64, c.name);
4809 outs() << format("0x%" PRIx64, c.name);
4810 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
4811 if (name != nullptr)
4812 outs() << format(" %.*s", left, name);
4816 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
4819 if (info->verbose && sym_name != nullptr)
4822 outs() << format("0x%" PRIx64, n_value);
4824 outs() << " + " << format("0x%" PRIx64, c.cls);
4826 outs() << format("0x%" PRIx64, c.cls);
4828 if (c.cls + n_value != 0)
4829 print_class64_t(c.cls + n_value, info);
4831 outs() << " instanceMethods ";
4833 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
4834 info, n_value, c.instanceMethods);
4836 if (info->verbose && sym_name != nullptr)
4839 outs() << format("0x%" PRIx64, n_value);
4840 if (c.instanceMethods != 0)
4841 outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
4843 outs() << format("0x%" PRIx64, c.instanceMethods);
4845 if (c.instanceMethods + n_value != 0)
4846 print_method_list64_t(c.instanceMethods + n_value, info, "");
4848 outs() << " classMethods ";
4849 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
4850 S, info, n_value, c.classMethods);
4852 if (info->verbose && sym_name != nullptr)
4855 outs() << format("0x%" PRIx64, n_value);
4856 if (c.classMethods != 0)
4857 outs() << " + " << format("0x%" PRIx64, c.classMethods);
4859 outs() << format("0x%" PRIx64, c.classMethods);
4861 if (c.classMethods + n_value != 0)
4862 print_method_list64_t(c.classMethods + n_value, info, "");
4864 outs() << " protocols ";
4865 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
4866 info, n_value, c.protocols);
4868 if (info->verbose && sym_name != nullptr)
4871 outs() << format("0x%" PRIx64, n_value);
4872 if (c.protocols != 0)
4873 outs() << " + " << format("0x%" PRIx64, c.protocols);
4875 outs() << format("0x%" PRIx64, c.protocols);
4877 if (c.protocols + n_value != 0)
4878 print_protocol_list64_t(c.protocols + n_value, info);
4880 outs() << "instanceProperties ";
4882 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
4883 S, info, n_value, c.instanceProperties);
4885 if (info->verbose && sym_name != nullptr)
4888 outs() << format("0x%" PRIx64, n_value);
4889 if (c.instanceProperties != 0)
4890 outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
4892 outs() << format("0x%" PRIx64, c.instanceProperties);
4894 if (c.instanceProperties + n_value != 0)
4895 print_objc_property_list64(c.instanceProperties + n_value, info);
4898 static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
4899 struct category32_t c;
4901 uint32_t offset, left;
4905 r = get_pointer_32(p, offset, left, S, info);
4908 memset(&c, '\0', sizeof(struct category32_t));
4909 if (left < sizeof(struct category32_t)) {
4910 memcpy(&c, r, left);
4911 outs() << " (category_t entends past the end of the section)\n";
4913 memcpy(&c, r, sizeof(struct category32_t));
4914 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4917 outs() << " name " << format("0x%" PRIx32, c.name);
4918 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
4921 outs() << " " << name;
4924 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
4926 print_class32_t(c.cls, info);
4927 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
4929 if (c.instanceMethods != 0)
4930 print_method_list32_t(c.instanceMethods, info, "");
4931 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
4933 if (c.classMethods != 0)
4934 print_method_list32_t(c.classMethods, info, "");
4935 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
4936 if (c.protocols != 0)
4937 print_protocol_list32_t(c.protocols, info);
4938 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
4940 if (c.instanceProperties != 0)
4941 print_objc_property_list32(c.instanceProperties, info);
4944 static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
4945 uint32_t i, left, offset, xoffset;
4946 uint64_t p, n_value;
4947 struct message_ref64 mr;
4948 const char *name, *sym_name;
4952 if (S == SectionRef())
4956 S.getName(SectName);
4957 DataRefImpl Ref = S.getRawDataRefImpl();
4958 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4959 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4961 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
4962 p = S.getAddress() + i;
4963 r = get_pointer_64(p, offset, left, S, info);
4966 memset(&mr, '\0', sizeof(struct message_ref64));
4967 if (left < sizeof(struct message_ref64)) {
4968 memcpy(&mr, r, left);
4969 outs() << " (message_ref entends past the end of the section)\n";
4971 memcpy(&mr, r, sizeof(struct message_ref64));
4972 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4976 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
4979 outs() << format("0x%" PRIx64, n_value) << " ";
4981 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
4983 outs() << format("0x%" PRIx64, mr.imp) << " ";
4984 if (name != nullptr)
4985 outs() << " " << name;
4989 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
4990 info, n_value, mr.sel);
4992 if (info->verbose && sym_name != nullptr)
4995 outs() << format("0x%" PRIx64, n_value);
4997 outs() << " + " << format("0x%" PRIx64, mr.sel);
4999 outs() << format("0x%" PRIx64, mr.sel);
5000 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
5001 if (name != nullptr)
5002 outs() << format(" %.*s", left, name);
5005 offset += sizeof(struct message_ref64);
5009 static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
5010 uint32_t i, left, offset, xoffset, p;
5011 struct message_ref32 mr;
5012 const char *name, *r;
5015 if (S == SectionRef())
5019 S.getName(SectName);
5020 DataRefImpl Ref = S.getRawDataRefImpl();
5021 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5022 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5024 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5025 p = S.getAddress() + i;
5026 r = get_pointer_32(p, offset, left, S, info);
5029 memset(&mr, '\0', sizeof(struct message_ref32));
5030 if (left < sizeof(struct message_ref32)) {
5031 memcpy(&mr, r, left);
5032 outs() << " (message_ref entends past the end of the section)\n";
5034 memcpy(&mr, r, sizeof(struct message_ref32));
5035 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5038 outs() << " imp " << format("0x%" PRIx32, mr.imp);
5039 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
5041 if (name != nullptr)
5042 outs() << " " << name;
5045 outs() << " sel " << format("0x%" PRIx32, mr.sel);
5046 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
5047 if (name != nullptr)
5048 outs() << " " << name;
5051 offset += sizeof(struct message_ref32);
5055 static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
5056 uint32_t left, offset, swift_version;
5058 struct objc_image_info64 o;
5062 S.getName(SectName);
5063 DataRefImpl Ref = S.getRawDataRefImpl();
5064 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5065 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5067 r = get_pointer_64(p, offset, left, S, info);
5070 memset(&o, '\0', sizeof(struct objc_image_info64));
5071 if (left < sizeof(struct objc_image_info64)) {
5072 memcpy(&o, r, left);
5073 outs() << " (objc_image_info entends past the end of the section)\n";
5075 memcpy(&o, r, sizeof(struct objc_image_info64));
5076 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5078 outs() << " version " << o.version << "\n";
5079 outs() << " flags " << format("0x%" PRIx32, o.flags);
5080 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5081 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5082 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5083 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5084 swift_version = (o.flags >> 8) & 0xff;
5085 if (swift_version != 0) {
5086 if (swift_version == 1)
5087 outs() << " Swift 1.0";
5088 else if (swift_version == 2)
5089 outs() << " Swift 1.1";
5091 outs() << " unknown future Swift version (" << swift_version << ")";
5096 static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5097 uint32_t left, offset, swift_version, p;
5098 struct objc_image_info32 o;
5102 S.getName(SectName);
5103 DataRefImpl Ref = S.getRawDataRefImpl();
5104 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5105 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5107 r = get_pointer_32(p, offset, left, S, info);
5110 memset(&o, '\0', sizeof(struct objc_image_info32));
5111 if (left < sizeof(struct objc_image_info32)) {
5112 memcpy(&o, r, left);
5113 outs() << " (objc_image_info entends past the end of the section)\n";
5115 memcpy(&o, r, sizeof(struct objc_image_info32));
5116 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5118 outs() << " version " << o.version << "\n";
5119 outs() << " flags " << format("0x%" PRIx32, o.flags);
5120 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5121 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5122 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5123 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5124 swift_version = (o.flags >> 8) & 0xff;
5125 if (swift_version != 0) {
5126 if (swift_version == 1)
5127 outs() << " Swift 1.0";
5128 else if (swift_version == 2)
5129 outs() << " Swift 1.1";
5131 outs() << " unknown future Swift version (" << swift_version << ")";
5136 static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5137 uint32_t left, offset, p;
5138 struct imageInfo_t o;
5142 S.getName(SectName);
5143 DataRefImpl Ref = S.getRawDataRefImpl();
5144 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5145 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5147 r = get_pointer_32(p, offset, left, S, info);
5150 memset(&o, '\0', sizeof(struct imageInfo_t));
5151 if (left < sizeof(struct imageInfo_t)) {
5152 memcpy(&o, r, left);
5153 outs() << " (imageInfo entends past the end of the section)\n";
5155 memcpy(&o, r, sizeof(struct imageInfo_t));
5156 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5158 outs() << " version " << o.version << "\n";
5159 outs() << " flags " << format("0x%" PRIx32, o.flags);
5165 outs() << " GC-only";
5171 static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
5172 SymbolAddressMap AddrMap;
5174 CreateSymbolAddressMap(O, &AddrMap);
5176 std::vector<SectionRef> Sections;
5177 for (const SectionRef &Section : O->sections()) {
5179 Section.getName(SectName);
5180 Sections.push_back(Section);
5183 struct DisassembleInfo info;
5184 // Set up the block of info used by the Symbolizer call backs.
5185 info.verbose = verbose;
5187 info.AddrMap = &AddrMap;
5188 info.Sections = &Sections;
5189 info.class_name = nullptr;
5190 info.selector_name = nullptr;
5191 info.method = nullptr;
5192 info.demangled_name = nullptr;
5193 info.bindtable = nullptr;
5197 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5198 if (CL != SectionRef()) {
5200 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5202 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5204 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5207 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5208 if (CR != SectionRef()) {
5210 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5212 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5214 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5217 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5218 if (SR != SectionRef()) {
5220 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5222 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5224 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5227 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5228 if (CA != SectionRef()) {
5230 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5232 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5234 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5237 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5238 if (PL != SectionRef()) {
5240 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5242 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5244 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5247 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5248 if (MR != SectionRef()) {
5250 print_message_refs64(MR, &info);
5252 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5254 print_message_refs64(MR, &info);
5257 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5258 if (II != SectionRef()) {
5260 print_image_info64(II, &info);
5262 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5264 print_image_info64(II, &info);
5267 if (info.bindtable != nullptr)
5268 delete info.bindtable;
5271 static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
5272 SymbolAddressMap AddrMap;
5274 CreateSymbolAddressMap(O, &AddrMap);
5276 std::vector<SectionRef> Sections;
5277 for (const SectionRef &Section : O->sections()) {
5279 Section.getName(SectName);
5280 Sections.push_back(Section);
5283 struct DisassembleInfo info;
5284 // Set up the block of info used by the Symbolizer call backs.
5285 info.verbose = verbose;
5287 info.AddrMap = &AddrMap;
5288 info.Sections = &Sections;
5289 info.class_name = nullptr;
5290 info.selector_name = nullptr;
5291 info.method = nullptr;
5292 info.demangled_name = nullptr;
5293 info.bindtable = nullptr;
5297 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5298 if (CL != SectionRef()) {
5300 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5302 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5304 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5307 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5308 if (CR != SectionRef()) {
5310 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5312 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5314 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5317 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5318 if (SR != SectionRef()) {
5320 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5322 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5324 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5327 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5328 if (CA != SectionRef()) {
5330 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5332 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5334 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5337 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5338 if (PL != SectionRef()) {
5340 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5342 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5344 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5347 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5348 if (MR != SectionRef()) {
5350 print_message_refs32(MR, &info);
5352 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5354 print_message_refs32(MR, &info);
5357 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5358 if (II != SectionRef()) {
5360 print_image_info32(II, &info);
5362 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5364 print_image_info32(II, &info);
5368 static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
5369 uint32_t i, j, p, offset, xoffset, left, defs_left, def;
5370 const char *r, *name, *defs;
5371 struct objc_module_t module;
5373 struct objc_symtab_t symtab;
5374 struct objc_class_t objc_class;
5375 struct objc_category_t objc_category;
5377 outs() << "Objective-C segment\n";
5378 S = get_section(O, "__OBJC", "__module_info");
5379 if (S == SectionRef())
5382 SymbolAddressMap AddrMap;
5384 CreateSymbolAddressMap(O, &AddrMap);
5386 std::vector<SectionRef> Sections;
5387 for (const SectionRef &Section : O->sections()) {
5389 Section.getName(SectName);
5390 Sections.push_back(Section);
5393 struct DisassembleInfo info;
5394 // Set up the block of info used by the Symbolizer call backs.
5395 info.verbose = verbose;
5397 info.AddrMap = &AddrMap;
5398 info.Sections = &Sections;
5399 info.class_name = nullptr;
5400 info.selector_name = nullptr;
5401 info.method = nullptr;
5402 info.demangled_name = nullptr;
5403 info.bindtable = nullptr;
5407 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
5408 p = S.getAddress() + i;
5409 r = get_pointer_32(p, offset, left, S, &info, true);
5412 memset(&module, '\0', sizeof(struct objc_module_t));
5413 if (left < sizeof(struct objc_module_t)) {
5414 memcpy(&module, r, left);
5415 outs() << " (module extends past end of __module_info section)\n";
5417 memcpy(&module, r, sizeof(struct objc_module_t));
5418 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5421 outs() << "Module " << format("0x%" PRIx32, p) << "\n";
5422 outs() << " version " << module.version << "\n";
5423 outs() << " size " << module.size << "\n";
5425 name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
5426 if (name != nullptr)
5427 outs() << format("%.*s", left, name);
5429 outs() << format("0x%08" PRIx32, module.name)
5430 << "(not in an __OBJC section)";
5433 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
5434 if (module.symtab == 0 || r == nullptr) {
5435 outs() << " symtab " << format("0x%08" PRIx32, module.symtab)
5436 << " (not in an __OBJC section)\n";
5439 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
5440 memset(&symtab, '\0', sizeof(struct objc_symtab_t));
5443 if (left < sizeof(struct objc_symtab_t)) {
5444 memcpy(&symtab, r, left);
5445 outs() << "\tsymtab extends past end of an __OBJC section)\n";
5447 memcpy(&symtab, r, sizeof(struct objc_symtab_t));
5448 if (left > sizeof(struct objc_symtab_t)) {
5449 defs_left = left - sizeof(struct objc_symtab_t);
5450 defs = r + sizeof(struct objc_symtab_t);
5453 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5456 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
5457 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
5458 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
5460 outs() << " (not in an __OBJC section)";
5462 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
5463 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
5464 if (symtab.cls_def_cnt > 0)
5465 outs() << "\tClass Definitions\n";
5466 for (j = 0; j < symtab.cls_def_cnt; j++) {
5467 if ((j + 1) * sizeof(uint32_t) > defs_left) {
5468 outs() << "\t(remaining class defs entries entends past the end of the "
5472 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
5473 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5474 sys::swapByteOrder(def);
5476 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5477 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
5479 if (left > sizeof(struct objc_class_t)) {
5481 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5483 outs() << " (entends past the end of the section)\n";
5484 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5485 memcpy(&objc_class, r, left);
5487 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5488 swapStruct(objc_class);
5489 print_objc_class_t(&objc_class, &info);
5491 outs() << "(not in an __OBJC section)\n";
5494 if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
5495 outs() << "\tMeta Class";
5496 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
5498 if (left > sizeof(struct objc_class_t)) {
5500 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5502 outs() << " (entends past the end of the section)\n";
5503 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5504 memcpy(&objc_class, r, left);
5506 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5507 swapStruct(objc_class);
5508 print_objc_class_t(&objc_class, &info);
5510 outs() << "(not in an __OBJC section)\n";
5514 if (symtab.cat_def_cnt > 0)
5515 outs() << "\tCategory Definitions\n";
5516 for (j = 0; j < symtab.cat_def_cnt; j++) {
5517 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
5518 outs() << "\t(remaining category defs entries entends past the end of "
5519 << "the section)\n";
5522 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
5524 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5525 sys::swapByteOrder(def);
5527 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5528 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
5529 << format("0x%08" PRIx32, def);
5531 if (left > sizeof(struct objc_category_t)) {
5533 memcpy(&objc_category, r, sizeof(struct objc_category_t));
5535 outs() << " (entends past the end of the section)\n";
5536 memset(&objc_category, '\0', sizeof(struct objc_category_t));
5537 memcpy(&objc_category, r, left);
5539 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5540 swapStruct(objc_category);
5541 print_objc_objc_category_t(&objc_category, &info);
5543 outs() << "(not in an __OBJC section)\n";
5547 const SectionRef II = get_section(O, "__OBJC", "__image_info");
5548 if (II != SectionRef())
5549 print_image_info(II, &info);
5554 static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
5555 uint32_t size, uint32_t addr) {
5556 SymbolAddressMap AddrMap;
5557 CreateSymbolAddressMap(O, &AddrMap);
5559 std::vector<SectionRef> Sections;
5560 for (const SectionRef &Section : O->sections()) {
5562 Section.getName(SectName);
5563 Sections.push_back(Section);
5566 struct DisassembleInfo info;
5567 // Set up the block of info used by the Symbolizer call backs.
5568 info.verbose = true;
5570 info.AddrMap = &AddrMap;
5571 info.Sections = &Sections;
5572 info.class_name = nullptr;
5573 info.selector_name = nullptr;
5574 info.method = nullptr;
5575 info.demangled_name = nullptr;
5576 info.bindtable = nullptr;
5581 struct objc_protocol_t protocol;
5582 uint32_t left, paddr;
5583 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
5584 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
5585 left = size - (p - sect);
5586 if (left < sizeof(struct objc_protocol_t)) {
5587 outs() << "Protocol extends past end of __protocol section\n";
5588 memcpy(&protocol, p, left);
5590 memcpy(&protocol, p, sizeof(struct objc_protocol_t));
5591 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5592 swapStruct(protocol);
5593 paddr = addr + (p - sect);
5594 outs() << "Protocol " << format("0x%" PRIx32, paddr);
5595 if (print_protocol(paddr, 0, &info))
5596 outs() << "(not in an __OBJC section)\n";
5600 static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
5602 printObjc2_64bit_MetaData(O, verbose);
5604 MachO::mach_header H;
5606 if (H.cputype == MachO::CPU_TYPE_ARM)
5607 printObjc2_32bit_MetaData(O, verbose);
5609 // This is the 32-bit non-arm cputype case. Which is normally
5610 // the first Objective-C ABI. But it may be the case of a
5611 // binary for the iOS simulator which is the second Objective-C
5612 // ABI. In that case printObjc1_32bit_MetaData() will determine that
5613 // and return false.
5614 if (printObjc1_32bit_MetaData(O, verbose) == false)
5615 printObjc2_32bit_MetaData(O, verbose);
5620 // GuessLiteralPointer returns a string which for the item in the Mach-O file
5621 // for the address passed in as ReferenceValue for printing as a comment with
5622 // the instruction and also returns the corresponding type of that item
5623 // indirectly through ReferenceType.
5625 // If ReferenceValue is an address of literal cstring then a pointer to the
5626 // cstring is returned and ReferenceType is set to
5627 // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
5629 // If ReferenceValue is an address of an Objective-C CFString, Selector ref or
5630 // Class ref that name is returned and the ReferenceType is set accordingly.
5632 // Lastly, literals which are Symbol address in a literal pool are looked for
5633 // and if found the symbol name is returned and ReferenceType is set to
5634 // LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
5636 // If there is no item in the Mach-O file for the address passed in as
5637 // ReferenceValue nullptr is returned and ReferenceType is unchanged.
5638 static const char *GuessLiteralPointer(uint64_t ReferenceValue,
5639 uint64_t ReferencePC,
5640 uint64_t *ReferenceType,
5641 struct DisassembleInfo *info) {
5642 // First see if there is an external relocation entry at the ReferencePC.
5643 uint64_t sect_addr = info->S.getAddress();
5644 uint64_t sect_offset = ReferencePC - sect_addr;
5645 bool reloc_found = false;
5647 MachO::any_relocation_info RE;
5648 bool isExtern = false;
5650 for (const RelocationRef &Reloc : info->S.relocations()) {
5651 uint64_t RelocOffset;
5652 Reloc.getOffset(RelocOffset);
5653 if (RelocOffset == sect_offset) {
5654 Rel = Reloc.getRawDataRefImpl();
5655 RE = info->O->getRelocation(Rel);
5656 if (info->O->isRelocationScattered(RE))
5658 isExtern = info->O->getPlainRelocationExternal(RE);
5660 symbol_iterator RelocSym = Reloc.getSymbol();
5667 // If there is an external relocation entry for a symbol in a section
5668 // then used that symbol's value for the value of the reference.
5669 if (reloc_found && isExtern) {
5670 if (info->O->getAnyRelocationPCRel(RE)) {
5671 unsigned Type = info->O->getAnyRelocationType(RE);
5672 if (Type == MachO::X86_64_RELOC_SIGNED) {
5673 Symbol.getAddress(ReferenceValue);
5678 // Look for literals such as Objective-C CFStrings refs, Selector refs,
5679 // Message refs and Class refs.
5680 bool classref, selref, msgref, cfstring;
5681 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
5682 selref, msgref, cfstring);
5683 if (classref && pointer_value == 0) {
5684 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
5685 // And the pointer_value in that section is typically zero as it will be
5686 // set by dyld as part of the "bind information".
5687 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
5688 if (name != nullptr) {
5689 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
5690 const char *class_name = strrchr(name, '$');
5691 if (class_name != nullptr && class_name[1] == '_' &&
5692 class_name[2] != '\0') {
5693 info->class_name = class_name + 2;
5700 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
5702 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
5703 if (name != nullptr)
5704 info->class_name = name;
5706 name = "bad class ref";
5711 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
5712 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
5716 if (selref && pointer_value == 0)
5717 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
5719 if (pointer_value != 0)
5720 ReferenceValue = pointer_value;
5722 const char *name = GuessCstringPointer(ReferenceValue, info);
5724 if (pointer_value != 0 && selref) {
5725 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
5726 info->selector_name = name;
5727 } else if (pointer_value != 0 && msgref) {
5728 info->class_name = nullptr;
5729 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
5730 info->selector_name = name;
5732 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
5736 // Lastly look for an indirect symbol with this ReferenceValue which is in
5737 // a literal pool. If found return that symbol name.
5738 name = GuessIndirectSymbol(ReferenceValue, info);
5740 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
5747 // SymbolizerSymbolLookUp is the symbol lookup function passed when creating
5748 // the Symbolizer. It looks up the ReferenceValue using the info passed via the
5749 // pointer to the struct DisassembleInfo that was passed when MCSymbolizer
5750 // is created and returns the symbol name that matches the ReferenceValue or
5751 // nullptr if none. The ReferenceType is passed in for the IN type of
5752 // reference the instruction is making from the values in defined in the header
5753 // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
5754 // Out type and the ReferenceName will also be set which is added as a comment
5755 // to the disassembled instruction.
5758 // If the symbol name is a C++ mangled name then the demangled name is
5759 // returned through ReferenceName and ReferenceType is set to
5760 // LLVMDisassembler_ReferenceType_DeMangled_Name .
5763 // When this is called to get a symbol name for a branch target then the
5764 // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
5765 // SymbolValue will be looked for in the indirect symbol table to determine if
5766 // it is an address for a symbol stub. If so then the symbol name for that
5767 // stub is returned indirectly through ReferenceName and then ReferenceType is
5768 // set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
5770 // When this is called with an value loaded via a PC relative load then
5771 // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
5772 // SymbolValue is checked to be an address of literal pointer, symbol pointer,
5773 // or an Objective-C meta data reference. If so the output ReferenceType is
5774 // set to correspond to that as well as setting the ReferenceName.
5775 static const char *SymbolizerSymbolLookUp(void *DisInfo,
5776 uint64_t ReferenceValue,
5777 uint64_t *ReferenceType,
5778 uint64_t ReferencePC,
5779 const char **ReferenceName) {
5780 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
5781 // If no verbose symbolic information is wanted then just return nullptr.
5782 if (!info->verbose) {
5783 *ReferenceName = nullptr;
5784 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5788 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
5790 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
5791 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
5792 if (*ReferenceName != nullptr) {
5793 method_reference(info, ReferenceType, ReferenceName);
5794 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
5795 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
5798 if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
5799 if (info->demangled_name != nullptr)
5800 free(info->demangled_name);
5802 info->demangled_name =
5803 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
5804 if (info->demangled_name != nullptr) {
5805 *ReferenceName = info->demangled_name;
5806 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5808 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5811 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5812 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
5814 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5816 method_reference(info, ReferenceType, ReferenceName);
5818 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5819 // If this is arm64 and the reference is an adrp instruction save the
5820 // instruction, passed in ReferenceValue and the address of the instruction
5821 // for use later if we see and add immediate instruction.
5822 } else if (info->O->getArch() == Triple::aarch64 &&
5823 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
5824 info->adrp_inst = ReferenceValue;
5825 info->adrp_addr = ReferencePC;
5826 SymbolName = nullptr;
5827 *ReferenceName = nullptr;
5828 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5829 // If this is arm64 and reference is an add immediate instruction and we
5831 // seen an adrp instruction just before it and the adrp's Xd register
5833 // this add's Xn register reconstruct the value being referenced and look to
5834 // see if it is a literal pointer. Note the add immediate instruction is
5835 // passed in ReferenceValue.
5836 } else if (info->O->getArch() == Triple::aarch64 &&
5837 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
5838 ReferencePC - 4 == info->adrp_addr &&
5839 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5840 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5841 uint32_t addxri_inst;
5842 uint64_t adrp_imm, addxri_imm;
5845 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5846 if (info->adrp_inst & 0x0200000)
5847 adrp_imm |= 0xfffffffffc000000LL;
5849 addxri_inst = ReferenceValue;
5850 addxri_imm = (addxri_inst >> 10) & 0xfff;
5851 if (((addxri_inst >> 22) & 0x3) == 1)
5854 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5855 (adrp_imm << 12) + addxri_imm;
5858 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5859 if (*ReferenceName == nullptr)
5860 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5861 // If this is arm64 and the reference is a load register instruction and we
5862 // have seen an adrp instruction just before it and the adrp's Xd register
5863 // matches this add's Xn register reconstruct the value being referenced and
5864 // look to see if it is a literal pointer. Note the load register
5865 // instruction is passed in ReferenceValue.
5866 } else if (info->O->getArch() == Triple::aarch64 &&
5867 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
5868 ReferencePC - 4 == info->adrp_addr &&
5869 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5870 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5871 uint32_t ldrxui_inst;
5872 uint64_t adrp_imm, ldrxui_imm;
5875 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5876 if (info->adrp_inst & 0x0200000)
5877 adrp_imm |= 0xfffffffffc000000LL;
5879 ldrxui_inst = ReferenceValue;
5880 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
5882 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5883 (adrp_imm << 12) + (ldrxui_imm << 3);
5886 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5887 if (*ReferenceName == nullptr)
5888 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5890 // If this arm64 and is an load register (PC-relative) instruction the
5891 // ReferenceValue is the PC plus the immediate value.
5892 else if (info->O->getArch() == Triple::aarch64 &&
5893 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
5894 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
5896 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5897 if (*ReferenceName == nullptr)
5898 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5901 else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
5902 if (info->demangled_name != nullptr)
5903 free(info->demangled_name);
5905 info->demangled_name =
5906 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
5907 if (info->demangled_name != nullptr) {
5908 *ReferenceName = info->demangled_name;
5909 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5914 *ReferenceName = nullptr;
5915 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5921 /// \brief Emits the comments that are stored in the CommentStream.
5922 /// Each comment in the CommentStream must end with a newline.
5923 static void emitComments(raw_svector_ostream &CommentStream,
5924 SmallString<128> &CommentsToEmit,
5925 formatted_raw_ostream &FormattedOS,
5926 const MCAsmInfo &MAI) {
5927 // Flush the stream before taking its content.
5928 CommentStream.flush();
5929 StringRef Comments = CommentsToEmit.str();
5930 // Get the default information for printing a comment.
5931 const char *CommentBegin = MAI.getCommentString();
5932 unsigned CommentColumn = MAI.getCommentColumn();
5933 bool IsFirst = true;
5934 while (!Comments.empty()) {
5936 FormattedOS << '\n';
5937 // Emit a line of comments.
5938 FormattedOS.PadToColumn(CommentColumn);
5939 size_t Position = Comments.find('\n');
5940 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
5941 // Move after the newline character.
5942 Comments = Comments.substr(Position + 1);
5945 FormattedOS.flush();
5947 // Tell the comment stream that the vector changed underneath it.
5948 CommentsToEmit.clear();
5949 CommentStream.resync();
5952 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
5953 StringRef DisSegName, StringRef DisSectName) {
5954 const char *McpuDefault = nullptr;
5955 const Target *ThumbTarget = nullptr;
5956 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
5958 // GetTarget prints out stuff.
5961 if (MCPU.empty() && McpuDefault)
5964 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
5965 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
5967 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
5969 // Package up features to be passed to target/subtarget
5970 std::string FeaturesStr;
5971 if (MAttrs.size()) {
5972 SubtargetFeatures Features;
5973 for (unsigned i = 0; i != MAttrs.size(); ++i)
5974 Features.AddFeature(MAttrs[i]);
5975 FeaturesStr = Features.getString();
5978 // Set up disassembler.
5979 std::unique_ptr<const MCRegisterInfo> MRI(
5980 TheTarget->createMCRegInfo(TripleName));
5981 std::unique_ptr<const MCAsmInfo> AsmInfo(
5982 TheTarget->createMCAsmInfo(*MRI, TripleName));
5983 std::unique_ptr<const MCSubtargetInfo> STI(
5984 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
5985 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
5986 std::unique_ptr<MCDisassembler> DisAsm(
5987 TheTarget->createMCDisassembler(*STI, Ctx));
5988 std::unique_ptr<MCSymbolizer> Symbolizer;
5989 struct DisassembleInfo SymbolizerInfo;
5990 std::unique_ptr<MCRelocationInfo> RelInfo(
5991 TheTarget->createMCRelocationInfo(TripleName, Ctx));
5993 Symbolizer.reset(TheTarget->createMCSymbolizer(
5994 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
5995 &SymbolizerInfo, &Ctx, std::move(RelInfo)));
5996 DisAsm->setSymbolizer(std::move(Symbolizer));
5998 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
5999 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
6000 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
6001 // Set the display preference for hex vs. decimal immediates.
6002 IP->setPrintImmHex(PrintImmHex);
6003 // Comment stream and backing vector.
6004 SmallString<128> CommentsToEmit;
6005 raw_svector_ostream CommentStream(CommentsToEmit);
6006 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
6007 // if it is done then arm64 comments for string literals don't get printed
6008 // and some constant get printed instead and not setting it causes intel
6009 // (32-bit and 64-bit) comments printed with different spacing before the
6010 // comment causing different diffs with the 'C' disassembler library API.
6011 // IP->setCommentStream(CommentStream);
6013 if (!AsmInfo || !STI || !DisAsm || !IP) {
6014 errs() << "error: couldn't initialize disassembler for target "
6015 << TripleName << '\n';
6019 // Set up thumb disassembler.
6020 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
6021 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
6022 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
6023 std::unique_ptr<MCDisassembler> ThumbDisAsm;
6024 std::unique_ptr<MCInstPrinter> ThumbIP;
6025 std::unique_ptr<MCContext> ThumbCtx;
6026 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
6027 struct DisassembleInfo ThumbSymbolizerInfo;
6028 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
6030 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
6032 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
6034 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr));
6035 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
6036 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
6037 MCContext *PtrThumbCtx = ThumbCtx.get();
6039 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
6041 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
6042 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
6043 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
6044 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
6046 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
6047 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
6048 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
6049 *ThumbInstrInfo, *ThumbMRI));
6050 // Set the display preference for hex vs. decimal immediates.
6051 ThumbIP->setPrintImmHex(PrintImmHex);
6054 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
6055 errs() << "error: couldn't initialize disassembler for target "
6056 << ThumbTripleName << '\n';
6060 MachO::mach_header Header = MachOOF->getHeader();
6062 // FIXME: Using the -cfg command line option, this code used to be able to
6063 // annotate relocations with the referenced symbol's name, and if this was
6064 // inside a __[cf]string section, the data it points to. This is now replaced
6065 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
6066 std::vector<SectionRef> Sections;
6067 std::vector<SymbolRef> Symbols;
6068 SmallVector<uint64_t, 8> FoundFns;
6069 uint64_t BaseSegmentAddress;
6071 getSectionsAndSymbols(Header, MachOOF, Sections, Symbols, FoundFns,
6072 BaseSegmentAddress);
6074 // Sort the symbols by address, just in case they didn't come in that way.
6075 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
6077 // Build a data in code table that is sorted on by the address of each entry.
6078 uint64_t BaseAddress = 0;
6079 if (Header.filetype == MachO::MH_OBJECT)
6080 BaseAddress = Sections[0].getAddress();
6082 BaseAddress = BaseSegmentAddress;
6084 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
6087 DI->getOffset(Offset);
6088 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
6090 array_pod_sort(Dices.begin(), Dices.end());
6093 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
6095 raw_ostream &DebugOut = nulls();
6098 std::unique_ptr<DIContext> diContext;
6099 ObjectFile *DbgObj = MachOOF;
6100 // Try to find debug info and set up the DIContext for it.
6102 // A separate DSym file path was specified, parse it as a macho file,
6103 // get the sections and supply it to the section name parsing machinery.
6104 if (!DSYMFile.empty()) {
6105 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
6106 MemoryBuffer::getFileOrSTDIN(DSYMFile);
6107 if (std::error_code EC = BufOrErr.getError()) {
6108 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
6112 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
6117 // Setup the DIContext
6118 diContext.reset(DIContext::getDWARFContext(*DbgObj));
6121 if (DumpSections.size() == 0)
6122 outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
6124 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
6126 if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
6129 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
6131 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
6132 if (SegmentName != DisSegName)
6136 Sections[SectIdx].getContents(BytesStr);
6137 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
6139 uint64_t SectAddress = Sections[SectIdx].getAddress();
6141 bool symbolTableWorked = false;
6143 // Parse relocations.
6144 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
6145 for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) {
6146 uint64_t RelocOffset;
6147 Reloc.getOffset(RelocOffset);
6148 uint64_t SectionAddress = Sections[SectIdx].getAddress();
6149 RelocOffset -= SectionAddress;
6151 symbol_iterator RelocSym = Reloc.getSymbol();
6153 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
6155 array_pod_sort(Relocs.begin(), Relocs.end());
6157 // Create a map of symbol addresses to symbol names for use by
6158 // the SymbolizerSymbolLookUp() routine.
6159 SymbolAddressMap AddrMap;
6160 bool DisSymNameFound = false;
6161 for (const SymbolRef &Symbol : MachOOF->symbols()) {
6164 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
6165 ST == SymbolRef::ST_Other) {
6167 Symbol.getAddress(Address);
6169 Symbol.getName(SymName);
6170 AddrMap[Address] = SymName;
6171 if (!DisSymName.empty() && DisSymName == SymName)
6172 DisSymNameFound = true;
6175 if (!DisSymName.empty() && !DisSymNameFound) {
6176 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
6179 // Set up the block of info used by the Symbolizer call backs.
6180 SymbolizerInfo.verbose = !NoSymbolicOperands;
6181 SymbolizerInfo.O = MachOOF;
6182 SymbolizerInfo.S = Sections[SectIdx];
6183 SymbolizerInfo.AddrMap = &AddrMap;
6184 SymbolizerInfo.Sections = &Sections;
6185 SymbolizerInfo.class_name = nullptr;
6186 SymbolizerInfo.selector_name = nullptr;
6187 SymbolizerInfo.method = nullptr;
6188 SymbolizerInfo.demangled_name = nullptr;
6189 SymbolizerInfo.bindtable = nullptr;
6190 SymbolizerInfo.adrp_addr = 0;
6191 SymbolizerInfo.adrp_inst = 0;
6192 // Same for the ThumbSymbolizer
6193 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
6194 ThumbSymbolizerInfo.O = MachOOF;
6195 ThumbSymbolizerInfo.S = Sections[SectIdx];
6196 ThumbSymbolizerInfo.AddrMap = &AddrMap;
6197 ThumbSymbolizerInfo.Sections = &Sections;
6198 ThumbSymbolizerInfo.class_name = nullptr;
6199 ThumbSymbolizerInfo.selector_name = nullptr;
6200 ThumbSymbolizerInfo.method = nullptr;
6201 ThumbSymbolizerInfo.demangled_name = nullptr;
6202 ThumbSymbolizerInfo.bindtable = nullptr;
6203 ThumbSymbolizerInfo.adrp_addr = 0;
6204 ThumbSymbolizerInfo.adrp_inst = 0;
6206 // Disassemble symbol by symbol.
6207 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
6209 Symbols[SymIdx].getName(SymName);
6212 Symbols[SymIdx].getType(ST);
6213 if (ST != SymbolRef::ST_Function)
6216 // Make sure the symbol is defined in this section.
6217 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
6221 // If we are only disassembling one symbol see if this is that symbol.
6222 if (!DisSymName.empty() && DisSymName != SymName)
6225 // Start at the address of the symbol relative to the section's address.
6227 uint64_t SectionAddress = Sections[SectIdx].getAddress();
6228 Symbols[SymIdx].getAddress(Start);
6229 Start -= SectionAddress;
6231 // Stop disassembling either at the beginning of the next symbol or at
6232 // the end of the section.
6233 bool containsNextSym = false;
6234 uint64_t NextSym = 0;
6235 uint64_t NextSymIdx = SymIdx + 1;
6236 while (Symbols.size() > NextSymIdx) {
6237 SymbolRef::Type NextSymType;
6238 Symbols[NextSymIdx].getType(NextSymType);
6239 if (NextSymType == SymbolRef::ST_Function) {
6241 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
6242 Symbols[NextSymIdx].getAddress(NextSym);
6243 NextSym -= SectionAddress;
6249 uint64_t SectSize = Sections[SectIdx].getSize();
6250 uint64_t End = containsNextSym ? NextSym : SectSize;
6253 symbolTableWorked = true;
6255 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
6257 (MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget;
6259 outs() << SymName << ":\n";
6260 DILineInfo lastLine;
6261 for (uint64_t Index = Start; Index < End; Index += Size) {
6264 uint64_t PC = SectAddress + Index;
6265 if (!NoLeadingAddr) {
6266 if (FullLeadingAddr) {
6267 if (MachOOF->is64Bit())
6268 outs() << format("%016" PRIx64, PC);
6270 outs() << format("%08" PRIx64, PC);
6272 outs() << format("%8" PRIx64 ":", PC);
6278 // Check the data in code table here to see if this is data not an
6279 // instruction to be disassembled.
6281 Dice.push_back(std::make_pair(PC, DiceRef()));
6282 dice_table_iterator DTI =
6283 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
6284 compareDiceTableEntries);
6285 if (DTI != Dices.end()) {
6287 DTI->second.getLength(Length);
6289 DTI->second.getKind(Kind);
6290 Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
6291 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
6292 (PC == (DTI->first + Length - 1)) && (Length & 1))
6297 SmallVector<char, 64> AnnotationsBytes;
6298 raw_svector_ostream Annotations(AnnotationsBytes);
6302 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
6303 PC, DebugOut, Annotations);
6305 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
6306 DebugOut, Annotations);
6308 if (!NoShowRawInsn) {
6309 DumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, Size));
6311 formatted_raw_ostream FormattedOS(outs());
6312 Annotations.flush();
6313 StringRef AnnotationsStr = Annotations.str();
6315 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
6317 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
6318 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
6320 // Print debug info.
6322 DILineInfo dli = diContext->getLineInfoForAddress(PC);
6323 // Print valid line info if it changed.
6324 if (dli != lastLine && dli.Line != 0)
6325 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
6331 unsigned int Arch = MachOOF->getArch();
6332 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
6333 outs() << format("\t.byte 0x%02x #bad opcode\n",
6334 *(Bytes.data() + Index) & 0xff);
6335 Size = 1; // skip exactly one illegible byte and move on.
6336 } else if (Arch == Triple::aarch64) {
6337 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6338 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
6339 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
6340 (*(Bytes.data() + Index + 3) & 0xff) << 24;
6341 outs() << format("\t.long\t0x%08x\n", opcode);
6344 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6346 Size = 1; // skip illegible bytes
6351 if (!symbolTableWorked) {
6352 // Reading the symbol table didn't work, disassemble the whole section.
6353 uint64_t SectAddress = Sections[SectIdx].getAddress();
6354 uint64_t SectSize = Sections[SectIdx].getSize();
6356 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
6359 uint64_t PC = SectAddress + Index;
6360 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
6361 DebugOut, nulls())) {
6362 if (!NoLeadingAddr) {
6363 if (FullLeadingAddr) {
6364 if (MachOOF->is64Bit())
6365 outs() << format("%016" PRIx64, PC);
6367 outs() << format("%08" PRIx64, PC);
6369 outs() << format("%8" PRIx64 ":", PC);
6372 if (!NoShowRawInsn) {
6374 DumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, InstSize));
6376 IP->printInst(&Inst, outs(), "", *STI);
6379 unsigned int Arch = MachOOF->getArch();
6380 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
6381 outs() << format("\t.byte 0x%02x #bad opcode\n",
6382 *(Bytes.data() + Index) & 0xff);
6383 InstSize = 1; // skip exactly one illegible byte and move on.
6385 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6387 InstSize = 1; // skip illegible bytes
6392 // The TripleName's need to be reset if we are called again for a different
6395 ThumbTripleName = "";
6397 if (SymbolizerInfo.method != nullptr)
6398 free(SymbolizerInfo.method);
6399 if (SymbolizerInfo.demangled_name != nullptr)
6400 free(SymbolizerInfo.demangled_name);
6401 if (SymbolizerInfo.bindtable != nullptr)
6402 delete SymbolizerInfo.bindtable;
6403 if (ThumbSymbolizerInfo.method != nullptr)
6404 free(ThumbSymbolizerInfo.method);
6405 if (ThumbSymbolizerInfo.demangled_name != nullptr)
6406 free(ThumbSymbolizerInfo.demangled_name);
6407 if (ThumbSymbolizerInfo.bindtable != nullptr)
6408 delete ThumbSymbolizerInfo.bindtable;
6412 //===----------------------------------------------------------------------===//
6413 // __compact_unwind section dumping
6414 //===----------------------------------------------------------------------===//
6418 template <typename T> static uint64_t readNext(const char *&Buf) {
6419 using llvm::support::little;
6420 using llvm::support::unaligned;
6422 uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
6427 struct CompactUnwindEntry {
6428 uint32_t OffsetInSection;
6430 uint64_t FunctionAddr;
6432 uint32_t CompactEncoding;
6433 uint64_t PersonalityAddr;
6436 RelocationRef FunctionReloc;
6437 RelocationRef PersonalityReloc;
6438 RelocationRef LSDAReloc;
6440 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
6441 : OffsetInSection(Offset) {
6443 read<uint64_t>(Contents.data() + Offset);
6445 read<uint32_t>(Contents.data() + Offset);
6449 template <typename UIntPtr> void read(const char *Buf) {
6450 FunctionAddr = readNext<UIntPtr>(Buf);
6451 Length = readNext<uint32_t>(Buf);
6452 CompactEncoding = readNext<uint32_t>(Buf);
6453 PersonalityAddr = readNext<UIntPtr>(Buf);
6454 LSDAAddr = readNext<UIntPtr>(Buf);
6459 /// Given a relocation from __compact_unwind, consisting of the RelocationRef
6460 /// and data being relocated, determine the best base Name and Addend to use for
6461 /// display purposes.
6463 /// 1. An Extern relocation will directly reference a symbol (and the data is
6464 /// then already an addend), so use that.
6465 /// 2. Otherwise the data is an offset in the object file's layout; try to find
6466 // a symbol before it in the same section, and use the offset from there.
6467 /// 3. Finally, if all that fails, fall back to an offset from the start of the
6468 /// referenced section.
6469 static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
6470 std::map<uint64_t, SymbolRef> &Symbols,
6471 const RelocationRef &Reloc, uint64_t Addr,
6472 StringRef &Name, uint64_t &Addend) {
6473 if (Reloc.getSymbol() != Obj->symbol_end()) {
6474 Reloc.getSymbol()->getName(Name);
6479 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
6480 SectionRef RelocSection = Obj->getRelocationSection(RE);
6482 uint64_t SectionAddr = RelocSection.getAddress();
6484 auto Sym = Symbols.upper_bound(Addr);
6485 if (Sym == Symbols.begin()) {
6486 // The first symbol in the object is after this reference, the best we can
6487 // do is section-relative notation.
6488 RelocSection.getName(Name);
6489 Addend = Addr - SectionAddr;
6493 // Go back one so that SymbolAddress <= Addr.
6496 section_iterator SymSection = Obj->section_end();
6497 Sym->second.getSection(SymSection);
6498 if (RelocSection == *SymSection) {
6499 // There's a valid symbol in the same section before this reference.
6500 Sym->second.getName(Name);
6501 Addend = Addr - Sym->first;
6505 // There is a symbol before this reference, but it's in a different
6506 // section. Probably not helpful to mention it, so use the section name.
6507 RelocSection.getName(Name);
6508 Addend = Addr - SectionAddr;
6511 static void printUnwindRelocDest(const MachOObjectFile *Obj,
6512 std::map<uint64_t, SymbolRef> &Symbols,
6513 const RelocationRef &Reloc, uint64_t Addr) {
6517 if (!Reloc.getObjectFile())
6520 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
6524 outs() << " + " << format("0x%" PRIx64, Addend);
6528 printMachOCompactUnwindSection(const MachOObjectFile *Obj,
6529 std::map<uint64_t, SymbolRef> &Symbols,
6530 const SectionRef &CompactUnwind) {
6532 assert(Obj->isLittleEndian() &&
6533 "There should not be a big-endian .o with __compact_unwind");
6535 bool Is64 = Obj->is64Bit();
6536 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
6537 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
6540 CompactUnwind.getContents(Contents);
6542 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
6544 // First populate the initial raw offsets, encodings and so on from the entry.
6545 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
6546 CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
6547 CompactUnwinds.push_back(Entry);
6550 // Next we need to look at the relocations to find out what objects are
6551 // actually being referred to.
6552 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
6553 uint64_t RelocAddress;
6554 Reloc.getOffset(RelocAddress);
6556 uint32_t EntryIdx = RelocAddress / EntrySize;
6557 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
6558 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
6560 if (OffsetInEntry == 0)
6561 Entry.FunctionReloc = Reloc;
6562 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
6563 Entry.PersonalityReloc = Reloc;
6564 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
6565 Entry.LSDAReloc = Reloc;
6567 llvm_unreachable("Unexpected relocation in __compact_unwind section");
6570 // Finally, we're ready to print the data we've gathered.
6571 outs() << "Contents of __compact_unwind section:\n";
6572 for (auto &Entry : CompactUnwinds) {
6573 outs() << " Entry at offset "
6574 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
6576 // 1. Start of the region this entry applies to.
6577 outs() << " start: " << format("0x%" PRIx64,
6578 Entry.FunctionAddr) << ' ';
6579 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
6582 // 2. Length of the region this entry applies to.
6583 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
6585 // 3. The 32-bit compact encoding.
6586 outs() << " compact encoding: "
6587 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
6589 // 4. The personality function, if present.
6590 if (Entry.PersonalityReloc.getObjectFile()) {
6591 outs() << " personality function: "
6592 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
6593 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
6594 Entry.PersonalityAddr);
6598 // 5. This entry's language-specific data area.
6599 if (Entry.LSDAReloc.getObjectFile()) {
6600 outs() << " LSDA: " << format("0x%" PRIx64,
6601 Entry.LSDAAddr) << ' ';
6602 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
6608 //===----------------------------------------------------------------------===//
6609 // __unwind_info section dumping
6610 //===----------------------------------------------------------------------===//
6612 static void printRegularSecondLevelUnwindPage(const char *PageStart) {
6613 const char *Pos = PageStart;
6614 uint32_t Kind = readNext<uint32_t>(Pos);
6616 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
6618 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6619 uint16_t NumEntries = readNext<uint16_t>(Pos);
6621 Pos = PageStart + EntriesStart;
6622 for (unsigned i = 0; i < NumEntries; ++i) {
6623 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6624 uint32_t Encoding = readNext<uint32_t>(Pos);
6626 outs() << " [" << i << "]: "
6627 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6629 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
6633 static void printCompressedSecondLevelUnwindPage(
6634 const char *PageStart, uint32_t FunctionBase,
6635 const SmallVectorImpl<uint32_t> &CommonEncodings) {
6636 const char *Pos = PageStart;
6637 uint32_t Kind = readNext<uint32_t>(Pos);
6639 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
6641 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6642 uint16_t NumEntries = readNext<uint16_t>(Pos);
6644 uint16_t EncodingsStart = readNext<uint16_t>(Pos);
6645 readNext<uint16_t>(Pos);
6646 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
6647 PageStart + EncodingsStart);
6649 Pos = PageStart + EntriesStart;
6650 for (unsigned i = 0; i < NumEntries; ++i) {
6651 uint32_t Entry = readNext<uint32_t>(Pos);
6652 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
6653 uint32_t EncodingIdx = Entry >> 24;
6656 if (EncodingIdx < CommonEncodings.size())
6657 Encoding = CommonEncodings[EncodingIdx];
6659 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
6661 outs() << " [" << i << "]: "
6662 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6664 << "encoding[" << EncodingIdx
6665 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
6669 static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
6670 std::map<uint64_t, SymbolRef> &Symbols,
6671 const SectionRef &UnwindInfo) {
6673 assert(Obj->isLittleEndian() &&
6674 "There should not be a big-endian .o with __unwind_info");
6676 outs() << "Contents of __unwind_info section:\n";
6679 UnwindInfo.getContents(Contents);
6680 const char *Pos = Contents.data();
6682 //===----------------------------------
6684 //===----------------------------------
6686 uint32_t Version = readNext<uint32_t>(Pos);
6687 outs() << " Version: "
6688 << format("0x%" PRIx32, Version) << '\n';
6689 assert(Version == 1 && "only understand version 1");
6691 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
6692 outs() << " Common encodings array section offset: "
6693 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
6694 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
6695 outs() << " Number of common encodings in array: "
6696 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
6698 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
6699 outs() << " Personality function array section offset: "
6700 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
6701 uint32_t NumPersonalities = readNext<uint32_t>(Pos);
6702 outs() << " Number of personality functions in array: "
6703 << format("0x%" PRIx32, NumPersonalities) << '\n';
6705 uint32_t IndicesStart = readNext<uint32_t>(Pos);
6706 outs() << " Index array section offset: "
6707 << format("0x%" PRIx32, IndicesStart) << '\n';
6708 uint32_t NumIndices = readNext<uint32_t>(Pos);
6709 outs() << " Number of indices in array: "
6710 << format("0x%" PRIx32, NumIndices) << '\n';
6712 //===----------------------------------
6713 // A shared list of common encodings
6714 //===----------------------------------
6716 // These occupy indices in the range [0, N] whenever an encoding is referenced
6717 // from a compressed 2nd level index table. In practice the linker only
6718 // creates ~128 of these, so that indices are available to embed encodings in
6719 // the 2nd level index.
6721 SmallVector<uint32_t, 64> CommonEncodings;
6722 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
6723 Pos = Contents.data() + CommonEncodingsStart;
6724 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
6725 uint32_t Encoding = readNext<uint32_t>(Pos);
6726 CommonEncodings.push_back(Encoding);
6728 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
6732 //===----------------------------------
6733 // Personality functions used in this executable
6734 //===----------------------------------
6736 // There should be only a handful of these (one per source language,
6737 // roughly). Particularly since they only get 2 bits in the compact encoding.
6739 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
6740 Pos = Contents.data() + PersonalitiesStart;
6741 for (unsigned i = 0; i < NumPersonalities; ++i) {
6742 uint32_t PersonalityFn = readNext<uint32_t>(Pos);
6743 outs() << " personality[" << i + 1
6744 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
6747 //===----------------------------------
6748 // The level 1 index entries
6749 //===----------------------------------
6751 // These specify an approximate place to start searching for the more detailed
6752 // information, sorted by PC.
6755 uint32_t FunctionOffset;
6756 uint32_t SecondLevelPageStart;
6760 SmallVector<IndexEntry, 4> IndexEntries;
6762 outs() << " Top level indices: (count = " << NumIndices << ")\n";
6763 Pos = Contents.data() + IndicesStart;
6764 for (unsigned i = 0; i < NumIndices; ++i) {
6767 Entry.FunctionOffset = readNext<uint32_t>(Pos);
6768 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
6769 Entry.LSDAStart = readNext<uint32_t>(Pos);
6770 IndexEntries.push_back(Entry);
6772 outs() << " [" << i << "]: "
6773 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
6775 << "2nd level page offset="
6776 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
6777 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
6780 //===----------------------------------
6781 // Next come the LSDA tables
6782 //===----------------------------------
6784 // The LSDA layout is rather implicit: it's a contiguous array of entries from
6785 // the first top-level index's LSDAOffset to the last (sentinel).
6787 outs() << " LSDA descriptors:\n";
6788 Pos = Contents.data() + IndexEntries[0].LSDAStart;
6789 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
6790 (2 * sizeof(uint32_t));
6791 for (int i = 0; i < NumLSDAs; ++i) {
6792 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6793 uint32_t LSDAOffset = readNext<uint32_t>(Pos);
6794 outs() << " [" << i << "]: "
6795 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6797 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
6800 //===----------------------------------
6801 // Finally, the 2nd level indices
6802 //===----------------------------------
6804 // Generally these are 4K in size, and have 2 possible forms:
6805 // + Regular stores up to 511 entries with disparate encodings
6806 // + Compressed stores up to 1021 entries if few enough compact encoding
6808 outs() << " Second level indices:\n";
6809 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
6810 // The final sentinel top-level index has no associated 2nd level page
6811 if (IndexEntries[i].SecondLevelPageStart == 0)
6814 outs() << " Second level index[" << i << "]: "
6815 << "offset in section="
6816 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
6818 << "base function offset="
6819 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
6821 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
6822 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
6824 printRegularSecondLevelUnwindPage(Pos);
6826 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
6829 llvm_unreachable("Do not know how to print this kind of 2nd level page");
6833 void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
6834 std::map<uint64_t, SymbolRef> Symbols;
6835 for (const SymbolRef &SymRef : Obj->symbols()) {
6836 // Discard any undefined or absolute symbols. They're not going to take part
6837 // in the convenience lookup for unwind info and just take up resources.
6838 section_iterator Section = Obj->section_end();
6839 SymRef.getSection(Section);
6840 if (Section == Obj->section_end())
6844 SymRef.getAddress(Addr);
6845 Symbols.insert(std::make_pair(Addr, SymRef));
6848 for (const SectionRef &Section : Obj->sections()) {
6850 Section.getName(SectName);
6851 if (SectName == "__compact_unwind")
6852 printMachOCompactUnwindSection(Obj, Symbols, Section);
6853 else if (SectName == "__unwind_info")
6854 printMachOUnwindInfoSection(Obj, Symbols, Section);
6855 else if (SectName == "__eh_frame")
6856 outs() << "llvm-objdump: warning: unhandled __eh_frame section\n";
6860 static void PrintMachHeader(uint32_t magic, uint32_t cputype,
6861 uint32_t cpusubtype, uint32_t filetype,
6862 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
6864 outs() << "Mach header\n";
6865 outs() << " magic cputype cpusubtype caps filetype ncmds "
6866 "sizeofcmds flags\n";
6868 if (magic == MachO::MH_MAGIC)
6869 outs() << " MH_MAGIC";
6870 else if (magic == MachO::MH_MAGIC_64)
6871 outs() << "MH_MAGIC_64";
6873 outs() << format(" 0x%08" PRIx32, magic);
6875 case MachO::CPU_TYPE_I386:
6877 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6878 case MachO::CPU_SUBTYPE_I386_ALL:
6882 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6886 case MachO::CPU_TYPE_X86_64:
6887 outs() << " X86_64";
6888 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6889 case MachO::CPU_SUBTYPE_X86_64_ALL:
6892 case MachO::CPU_SUBTYPE_X86_64_H:
6893 outs() << " Haswell";
6896 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6900 case MachO::CPU_TYPE_ARM:
6902 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6903 case MachO::CPU_SUBTYPE_ARM_ALL:
6906 case MachO::CPU_SUBTYPE_ARM_V4T:
6909 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
6912 case MachO::CPU_SUBTYPE_ARM_XSCALE:
6913 outs() << " XSCALE";
6915 case MachO::CPU_SUBTYPE_ARM_V6:
6918 case MachO::CPU_SUBTYPE_ARM_V6M:
6921 case MachO::CPU_SUBTYPE_ARM_V7:
6924 case MachO::CPU_SUBTYPE_ARM_V7EM:
6927 case MachO::CPU_SUBTYPE_ARM_V7K:
6930 case MachO::CPU_SUBTYPE_ARM_V7M:
6933 case MachO::CPU_SUBTYPE_ARM_V7S:
6937 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6941 case MachO::CPU_TYPE_ARM64:
6943 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6944 case MachO::CPU_SUBTYPE_ARM64_ALL:
6948 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6952 case MachO::CPU_TYPE_POWERPC:
6954 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6955 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6959 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6963 case MachO::CPU_TYPE_POWERPC64:
6965 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6966 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6970 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6975 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
6978 outs() << format(" 0x%02" PRIx32,
6979 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
6982 case MachO::MH_OBJECT:
6983 outs() << " OBJECT";
6985 case MachO::MH_EXECUTE:
6986 outs() << " EXECUTE";
6988 case MachO::MH_FVMLIB:
6989 outs() << " FVMLIB";
6991 case MachO::MH_CORE:
6994 case MachO::MH_PRELOAD:
6995 outs() << " PRELOAD";
6997 case MachO::MH_DYLIB:
7000 case MachO::MH_DYLIB_STUB:
7001 outs() << " DYLIB_STUB";
7003 case MachO::MH_DYLINKER:
7004 outs() << " DYLINKER";
7006 case MachO::MH_BUNDLE:
7007 outs() << " BUNDLE";
7009 case MachO::MH_DSYM:
7012 case MachO::MH_KEXT_BUNDLE:
7013 outs() << " KEXTBUNDLE";
7016 outs() << format(" %10u", filetype);
7019 outs() << format(" %5u", ncmds);
7020 outs() << format(" %10u", sizeofcmds);
7022 if (f & MachO::MH_NOUNDEFS) {
7023 outs() << " NOUNDEFS";
7024 f &= ~MachO::MH_NOUNDEFS;
7026 if (f & MachO::MH_INCRLINK) {
7027 outs() << " INCRLINK";
7028 f &= ~MachO::MH_INCRLINK;
7030 if (f & MachO::MH_DYLDLINK) {
7031 outs() << " DYLDLINK";
7032 f &= ~MachO::MH_DYLDLINK;
7034 if (f & MachO::MH_BINDATLOAD) {
7035 outs() << " BINDATLOAD";
7036 f &= ~MachO::MH_BINDATLOAD;
7038 if (f & MachO::MH_PREBOUND) {
7039 outs() << " PREBOUND";
7040 f &= ~MachO::MH_PREBOUND;
7042 if (f & MachO::MH_SPLIT_SEGS) {
7043 outs() << " SPLIT_SEGS";
7044 f &= ~MachO::MH_SPLIT_SEGS;
7046 if (f & MachO::MH_LAZY_INIT) {
7047 outs() << " LAZY_INIT";
7048 f &= ~MachO::MH_LAZY_INIT;
7050 if (f & MachO::MH_TWOLEVEL) {
7051 outs() << " TWOLEVEL";
7052 f &= ~MachO::MH_TWOLEVEL;
7054 if (f & MachO::MH_FORCE_FLAT) {
7055 outs() << " FORCE_FLAT";
7056 f &= ~MachO::MH_FORCE_FLAT;
7058 if (f & MachO::MH_NOMULTIDEFS) {
7059 outs() << " NOMULTIDEFS";
7060 f &= ~MachO::MH_NOMULTIDEFS;
7062 if (f & MachO::MH_NOFIXPREBINDING) {
7063 outs() << " NOFIXPREBINDING";
7064 f &= ~MachO::MH_NOFIXPREBINDING;
7066 if (f & MachO::MH_PREBINDABLE) {
7067 outs() << " PREBINDABLE";
7068 f &= ~MachO::MH_PREBINDABLE;
7070 if (f & MachO::MH_ALLMODSBOUND) {
7071 outs() << " ALLMODSBOUND";
7072 f &= ~MachO::MH_ALLMODSBOUND;
7074 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
7075 outs() << " SUBSECTIONS_VIA_SYMBOLS";
7076 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
7078 if (f & MachO::MH_CANONICAL) {
7079 outs() << " CANONICAL";
7080 f &= ~MachO::MH_CANONICAL;
7082 if (f & MachO::MH_WEAK_DEFINES) {
7083 outs() << " WEAK_DEFINES";
7084 f &= ~MachO::MH_WEAK_DEFINES;
7086 if (f & MachO::MH_BINDS_TO_WEAK) {
7087 outs() << " BINDS_TO_WEAK";
7088 f &= ~MachO::MH_BINDS_TO_WEAK;
7090 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
7091 outs() << " ALLOW_STACK_EXECUTION";
7092 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
7094 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
7095 outs() << " DEAD_STRIPPABLE_DYLIB";
7096 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
7098 if (f & MachO::MH_PIE) {
7100 f &= ~MachO::MH_PIE;
7102 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
7103 outs() << " NO_REEXPORTED_DYLIBS";
7104 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
7106 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
7107 outs() << " MH_HAS_TLV_DESCRIPTORS";
7108 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
7110 if (f & MachO::MH_NO_HEAP_EXECUTION) {
7111 outs() << " MH_NO_HEAP_EXECUTION";
7112 f &= ~MachO::MH_NO_HEAP_EXECUTION;
7114 if (f & MachO::MH_APP_EXTENSION_SAFE) {
7115 outs() << " APP_EXTENSION_SAFE";
7116 f &= ~MachO::MH_APP_EXTENSION_SAFE;
7118 if (f != 0 || flags == 0)
7119 outs() << format(" 0x%08" PRIx32, f);
7121 outs() << format(" 0x%08" PRIx32, magic);
7122 outs() << format(" %7d", cputype);
7123 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7124 outs() << format(" 0x%02" PRIx32,
7125 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7126 outs() << format(" %10u", filetype);
7127 outs() << format(" %5u", ncmds);
7128 outs() << format(" %10u", sizeofcmds);
7129 outs() << format(" 0x%08" PRIx32, flags);
7134 static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
7135 StringRef SegName, uint64_t vmaddr,
7136 uint64_t vmsize, uint64_t fileoff,
7137 uint64_t filesize, uint32_t maxprot,
7138 uint32_t initprot, uint32_t nsects,
7139 uint32_t flags, uint32_t object_size,
7141 uint64_t expected_cmdsize;
7142 if (cmd == MachO::LC_SEGMENT) {
7143 outs() << " cmd LC_SEGMENT\n";
7144 expected_cmdsize = nsects;
7145 expected_cmdsize *= sizeof(struct MachO::section);
7146 expected_cmdsize += sizeof(struct MachO::segment_command);
7148 outs() << " cmd LC_SEGMENT_64\n";
7149 expected_cmdsize = nsects;
7150 expected_cmdsize *= sizeof(struct MachO::section_64);
7151 expected_cmdsize += sizeof(struct MachO::segment_command_64);
7153 outs() << " cmdsize " << cmdsize;
7154 if (cmdsize != expected_cmdsize)
7155 outs() << " Inconsistent size\n";
7158 outs() << " segname " << SegName << "\n";
7159 if (cmd == MachO::LC_SEGMENT_64) {
7160 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
7161 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
7163 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
7164 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
7166 outs() << " fileoff " << fileoff;
7167 if (fileoff > object_size)
7168 outs() << " (past end of file)\n";
7171 outs() << " filesize " << filesize;
7172 if (fileoff + filesize > object_size)
7173 outs() << " (past end of file)\n";
7178 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7179 MachO::VM_PROT_EXECUTE)) != 0)
7180 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
7182 if (maxprot & MachO::VM_PROT_READ)
7183 outs() << " maxprot r";
7185 outs() << " maxprot -";
7186 if (maxprot & MachO::VM_PROT_WRITE)
7190 if (maxprot & MachO::VM_PROT_EXECUTE)
7196 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7197 MachO::VM_PROT_EXECUTE)) != 0)
7198 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
7200 if (initprot & MachO::VM_PROT_READ)
7201 outs() << " initprot r";
7203 outs() << " initprot -";
7204 if (initprot & MachO::VM_PROT_WRITE)
7208 if (initprot & MachO::VM_PROT_EXECUTE)
7214 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
7215 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
7217 outs() << " nsects " << nsects << "\n";
7221 outs() << " (none)\n";
7223 if (flags & MachO::SG_HIGHVM) {
7224 outs() << " HIGHVM";
7225 flags &= ~MachO::SG_HIGHVM;
7227 if (flags & MachO::SG_FVMLIB) {
7228 outs() << " FVMLIB";
7229 flags &= ~MachO::SG_FVMLIB;
7231 if (flags & MachO::SG_NORELOC) {
7232 outs() << " NORELOC";
7233 flags &= ~MachO::SG_NORELOC;
7235 if (flags & MachO::SG_PROTECTED_VERSION_1) {
7236 outs() << " PROTECTED_VERSION_1";
7237 flags &= ~MachO::SG_PROTECTED_VERSION_1;
7240 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
7245 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
7249 static void PrintSection(const char *sectname, const char *segname,
7250 uint64_t addr, uint64_t size, uint32_t offset,
7251 uint32_t align, uint32_t reloff, uint32_t nreloc,
7252 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
7253 uint32_t cmd, const char *sg_segname,
7254 uint32_t filetype, uint32_t object_size,
7256 outs() << "Section\n";
7257 outs() << " sectname " << format("%.16s\n", sectname);
7258 outs() << " segname " << format("%.16s", segname);
7259 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
7260 outs() << " (does not match segment)\n";
7263 if (cmd == MachO::LC_SEGMENT_64) {
7264 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
7265 outs() << " size " << format("0x%016" PRIx64, size);
7267 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
7268 outs() << " size " << format("0x%08" PRIx64, size);
7270 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
7271 outs() << " (past end of file)\n";
7274 outs() << " offset " << offset;
7275 if (offset > object_size)
7276 outs() << " (past end of file)\n";
7279 uint32_t align_shifted = 1 << align;
7280 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
7281 outs() << " reloff " << reloff;
7282 if (reloff > object_size)
7283 outs() << " (past end of file)\n";
7286 outs() << " nreloc " << nreloc;
7287 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
7288 outs() << " (past end of file)\n";
7291 uint32_t section_type = flags & MachO::SECTION_TYPE;
7294 if (section_type == MachO::S_REGULAR)
7295 outs() << " S_REGULAR\n";
7296 else if (section_type == MachO::S_ZEROFILL)
7297 outs() << " S_ZEROFILL\n";
7298 else if (section_type == MachO::S_CSTRING_LITERALS)
7299 outs() << " S_CSTRING_LITERALS\n";
7300 else if (section_type == MachO::S_4BYTE_LITERALS)
7301 outs() << " S_4BYTE_LITERALS\n";
7302 else if (section_type == MachO::S_8BYTE_LITERALS)
7303 outs() << " S_8BYTE_LITERALS\n";
7304 else if (section_type == MachO::S_16BYTE_LITERALS)
7305 outs() << " S_16BYTE_LITERALS\n";
7306 else if (section_type == MachO::S_LITERAL_POINTERS)
7307 outs() << " S_LITERAL_POINTERS\n";
7308 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
7309 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
7310 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
7311 outs() << " S_LAZY_SYMBOL_POINTERS\n";
7312 else if (section_type == MachO::S_SYMBOL_STUBS)
7313 outs() << " S_SYMBOL_STUBS\n";
7314 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
7315 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
7316 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
7317 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
7318 else if (section_type == MachO::S_COALESCED)
7319 outs() << " S_COALESCED\n";
7320 else if (section_type == MachO::S_INTERPOSING)
7321 outs() << " S_INTERPOSING\n";
7322 else if (section_type == MachO::S_DTRACE_DOF)
7323 outs() << " S_DTRACE_DOF\n";
7324 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
7325 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
7326 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
7327 outs() << " S_THREAD_LOCAL_REGULAR\n";
7328 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
7329 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
7330 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
7331 outs() << " S_THREAD_LOCAL_VARIABLES\n";
7332 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7333 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
7334 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
7335 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
7337 outs() << format("0x%08" PRIx32, section_type) << "\n";
7338 outs() << "attributes";
7339 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
7340 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
7341 outs() << " PURE_INSTRUCTIONS";
7342 if (section_attributes & MachO::S_ATTR_NO_TOC)
7343 outs() << " NO_TOC";
7344 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
7345 outs() << " STRIP_STATIC_SYMS";
7346 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
7347 outs() << " NO_DEAD_STRIP";
7348 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
7349 outs() << " LIVE_SUPPORT";
7350 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
7351 outs() << " SELF_MODIFYING_CODE";
7352 if (section_attributes & MachO::S_ATTR_DEBUG)
7354 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
7355 outs() << " SOME_INSTRUCTIONS";
7356 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
7357 outs() << " EXT_RELOC";
7358 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
7359 outs() << " LOC_RELOC";
7360 if (section_attributes == 0)
7361 outs() << " (none)";
7364 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
7365 outs() << " reserved1 " << reserved1;
7366 if (section_type == MachO::S_SYMBOL_STUBS ||
7367 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
7368 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
7369 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
7370 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7371 outs() << " (index into indirect symbol table)\n";
7374 outs() << " reserved2 " << reserved2;
7375 if (section_type == MachO::S_SYMBOL_STUBS)
7376 outs() << " (size of stubs)\n";
7381 static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
7382 uint32_t object_size) {
7383 outs() << " cmd LC_SYMTAB\n";
7384 outs() << " cmdsize " << st.cmdsize;
7385 if (st.cmdsize != sizeof(struct MachO::symtab_command))
7386 outs() << " Incorrect size\n";
7389 outs() << " symoff " << st.symoff;
7390 if (st.symoff > object_size)
7391 outs() << " (past end of file)\n";
7394 outs() << " nsyms " << st.nsyms;
7397 big_size = st.nsyms;
7398 big_size *= sizeof(struct MachO::nlist_64);
7399 big_size += st.symoff;
7400 if (big_size > object_size)
7401 outs() << " (past end of file)\n";
7405 big_size = st.nsyms;
7406 big_size *= sizeof(struct MachO::nlist);
7407 big_size += st.symoff;
7408 if (big_size > object_size)
7409 outs() << " (past end of file)\n";
7413 outs() << " stroff " << st.stroff;
7414 if (st.stroff > object_size)
7415 outs() << " (past end of file)\n";
7418 outs() << " strsize " << st.strsize;
7419 big_size = st.stroff;
7420 big_size += st.strsize;
7421 if (big_size > object_size)
7422 outs() << " (past end of file)\n";
7427 static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
7428 uint32_t nsyms, uint32_t object_size,
7430 outs() << " cmd LC_DYSYMTAB\n";
7431 outs() << " cmdsize " << dyst.cmdsize;
7432 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
7433 outs() << " Incorrect size\n";
7436 outs() << " ilocalsym " << dyst.ilocalsym;
7437 if (dyst.ilocalsym > nsyms)
7438 outs() << " (greater than the number of symbols)\n";
7441 outs() << " nlocalsym " << dyst.nlocalsym;
7443 big_size = dyst.ilocalsym;
7444 big_size += dyst.nlocalsym;
7445 if (big_size > nsyms)
7446 outs() << " (past the end of the symbol table)\n";
7449 outs() << " iextdefsym " << dyst.iextdefsym;
7450 if (dyst.iextdefsym > nsyms)
7451 outs() << " (greater than the number of symbols)\n";
7454 outs() << " nextdefsym " << dyst.nextdefsym;
7455 big_size = dyst.iextdefsym;
7456 big_size += dyst.nextdefsym;
7457 if (big_size > nsyms)
7458 outs() << " (past the end of the symbol table)\n";
7461 outs() << " iundefsym " << dyst.iundefsym;
7462 if (dyst.iundefsym > nsyms)
7463 outs() << " (greater than the number of symbols)\n";
7466 outs() << " nundefsym " << dyst.nundefsym;
7467 big_size = dyst.iundefsym;
7468 big_size += dyst.nundefsym;
7469 if (big_size > nsyms)
7470 outs() << " (past the end of the symbol table)\n";
7473 outs() << " tocoff " << dyst.tocoff;
7474 if (dyst.tocoff > object_size)
7475 outs() << " (past end of file)\n";
7478 outs() << " ntoc " << dyst.ntoc;
7479 big_size = dyst.ntoc;
7480 big_size *= sizeof(struct MachO::dylib_table_of_contents);
7481 big_size += dyst.tocoff;
7482 if (big_size > object_size)
7483 outs() << " (past end of file)\n";
7486 outs() << " modtaboff " << dyst.modtaboff;
7487 if (dyst.modtaboff > object_size)
7488 outs() << " (past end of file)\n";
7491 outs() << " nmodtab " << dyst.nmodtab;
7494 modtabend = dyst.nmodtab;
7495 modtabend *= sizeof(struct MachO::dylib_module_64);
7496 modtabend += dyst.modtaboff;
7498 modtabend = dyst.nmodtab;
7499 modtabend *= sizeof(struct MachO::dylib_module);
7500 modtabend += dyst.modtaboff;
7502 if (modtabend > object_size)
7503 outs() << " (past end of file)\n";
7506 outs() << " extrefsymoff " << dyst.extrefsymoff;
7507 if (dyst.extrefsymoff > object_size)
7508 outs() << " (past end of file)\n";
7511 outs() << " nextrefsyms " << dyst.nextrefsyms;
7512 big_size = dyst.nextrefsyms;
7513 big_size *= sizeof(struct MachO::dylib_reference);
7514 big_size += dyst.extrefsymoff;
7515 if (big_size > object_size)
7516 outs() << " (past end of file)\n";
7519 outs() << " indirectsymoff " << dyst.indirectsymoff;
7520 if (dyst.indirectsymoff > object_size)
7521 outs() << " (past end of file)\n";
7524 outs() << " nindirectsyms " << dyst.nindirectsyms;
7525 big_size = dyst.nindirectsyms;
7526 big_size *= sizeof(uint32_t);
7527 big_size += dyst.indirectsymoff;
7528 if (big_size > object_size)
7529 outs() << " (past end of file)\n";
7532 outs() << " extreloff " << dyst.extreloff;
7533 if (dyst.extreloff > object_size)
7534 outs() << " (past end of file)\n";
7537 outs() << " nextrel " << dyst.nextrel;
7538 big_size = dyst.nextrel;
7539 big_size *= sizeof(struct MachO::relocation_info);
7540 big_size += dyst.extreloff;
7541 if (big_size > object_size)
7542 outs() << " (past end of file)\n";
7545 outs() << " locreloff " << dyst.locreloff;
7546 if (dyst.locreloff > object_size)
7547 outs() << " (past end of file)\n";
7550 outs() << " nlocrel " << dyst.nlocrel;
7551 big_size = dyst.nlocrel;
7552 big_size *= sizeof(struct MachO::relocation_info);
7553 big_size += dyst.locreloff;
7554 if (big_size > object_size)
7555 outs() << " (past end of file)\n";
7560 static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
7561 uint32_t object_size) {
7562 if (dc.cmd == MachO::LC_DYLD_INFO)
7563 outs() << " cmd LC_DYLD_INFO\n";
7565 outs() << " cmd LC_DYLD_INFO_ONLY\n";
7566 outs() << " cmdsize " << dc.cmdsize;
7567 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
7568 outs() << " Incorrect size\n";
7571 outs() << " rebase_off " << dc.rebase_off;
7572 if (dc.rebase_off > object_size)
7573 outs() << " (past end of file)\n";
7576 outs() << " rebase_size " << dc.rebase_size;
7578 big_size = dc.rebase_off;
7579 big_size += dc.rebase_size;
7580 if (big_size > object_size)
7581 outs() << " (past end of file)\n";
7584 outs() << " bind_off " << dc.bind_off;
7585 if (dc.bind_off > object_size)
7586 outs() << " (past end of file)\n";
7589 outs() << " bind_size " << dc.bind_size;
7590 big_size = dc.bind_off;
7591 big_size += dc.bind_size;
7592 if (big_size > object_size)
7593 outs() << " (past end of file)\n";
7596 outs() << " weak_bind_off " << dc.weak_bind_off;
7597 if (dc.weak_bind_off > object_size)
7598 outs() << " (past end of file)\n";
7601 outs() << " weak_bind_size " << dc.weak_bind_size;
7602 big_size = dc.weak_bind_off;
7603 big_size += dc.weak_bind_size;
7604 if (big_size > object_size)
7605 outs() << " (past end of file)\n";
7608 outs() << " lazy_bind_off " << dc.lazy_bind_off;
7609 if (dc.lazy_bind_off > object_size)
7610 outs() << " (past end of file)\n";
7613 outs() << " lazy_bind_size " << dc.lazy_bind_size;
7614 big_size = dc.lazy_bind_off;
7615 big_size += dc.lazy_bind_size;
7616 if (big_size > object_size)
7617 outs() << " (past end of file)\n";
7620 outs() << " export_off " << dc.export_off;
7621 if (dc.export_off > object_size)
7622 outs() << " (past end of file)\n";
7625 outs() << " export_size " << dc.export_size;
7626 big_size = dc.export_off;
7627 big_size += dc.export_size;
7628 if (big_size > object_size)
7629 outs() << " (past end of file)\n";
7634 static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
7636 if (dyld.cmd == MachO::LC_ID_DYLINKER)
7637 outs() << " cmd LC_ID_DYLINKER\n";
7638 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
7639 outs() << " cmd LC_LOAD_DYLINKER\n";
7640 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
7641 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
7643 outs() << " cmd ?(" << dyld.cmd << ")\n";
7644 outs() << " cmdsize " << dyld.cmdsize;
7645 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
7646 outs() << " Incorrect size\n";
7649 if (dyld.name >= dyld.cmdsize)
7650 outs() << " name ?(bad offset " << dyld.name << ")\n";
7652 const char *P = (const char *)(Ptr) + dyld.name;
7653 outs() << " name " << P << " (offset " << dyld.name << ")\n";
7657 static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
7658 outs() << " cmd LC_UUID\n";
7659 outs() << " cmdsize " << uuid.cmdsize;
7660 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
7661 outs() << " Incorrect size\n";
7665 outs() << format("%02" PRIX32, uuid.uuid[0]);
7666 outs() << format("%02" PRIX32, uuid.uuid[1]);
7667 outs() << format("%02" PRIX32, uuid.uuid[2]);
7668 outs() << format("%02" PRIX32, uuid.uuid[3]);
7670 outs() << format("%02" PRIX32, uuid.uuid[4]);
7671 outs() << format("%02" PRIX32, uuid.uuid[5]);
7673 outs() << format("%02" PRIX32, uuid.uuid[6]);
7674 outs() << format("%02" PRIX32, uuid.uuid[7]);
7676 outs() << format("%02" PRIX32, uuid.uuid[8]);
7677 outs() << format("%02" PRIX32, uuid.uuid[9]);
7679 outs() << format("%02" PRIX32, uuid.uuid[10]);
7680 outs() << format("%02" PRIX32, uuid.uuid[11]);
7681 outs() << format("%02" PRIX32, uuid.uuid[12]);
7682 outs() << format("%02" PRIX32, uuid.uuid[13]);
7683 outs() << format("%02" PRIX32, uuid.uuid[14]);
7684 outs() << format("%02" PRIX32, uuid.uuid[15]);
7688 static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
7689 outs() << " cmd LC_RPATH\n";
7690 outs() << " cmdsize " << rpath.cmdsize;
7691 if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
7692 outs() << " Incorrect size\n";
7695 if (rpath.path >= rpath.cmdsize)
7696 outs() << " path ?(bad offset " << rpath.path << ")\n";
7698 const char *P = (const char *)(Ptr) + rpath.path;
7699 outs() << " path " << P << " (offset " << rpath.path << ")\n";
7703 static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
7704 if (vd.cmd == MachO::LC_VERSION_MIN_MACOSX)
7705 outs() << " cmd LC_VERSION_MIN_MACOSX\n";
7706 else if (vd.cmd == MachO::LC_VERSION_MIN_IPHONEOS)
7707 outs() << " cmd LC_VERSION_MIN_IPHONEOS\n";
7709 outs() << " cmd " << vd.cmd << " (?)\n";
7710 outs() << " cmdsize " << vd.cmdsize;
7711 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
7712 outs() << " Incorrect size\n";
7715 outs() << " version " << ((vd.version >> 16) & 0xffff) << "."
7716 << ((vd.version >> 8) & 0xff);
7717 if ((vd.version & 0xff) != 0)
7718 outs() << "." << (vd.version & 0xff);
7721 outs() << " sdk n/a";
7723 outs() << " sdk " << ((vd.sdk >> 16) & 0xffff) << "."
7724 << ((vd.sdk >> 8) & 0xff);
7726 if ((vd.sdk & 0xff) != 0)
7727 outs() << "." << (vd.sdk & 0xff);
7731 static void PrintSourceVersionCommand(MachO::source_version_command sd) {
7732 outs() << " cmd LC_SOURCE_VERSION\n";
7733 outs() << " cmdsize " << sd.cmdsize;
7734 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
7735 outs() << " Incorrect size\n";
7738 uint64_t a = (sd.version >> 40) & 0xffffff;
7739 uint64_t b = (sd.version >> 30) & 0x3ff;
7740 uint64_t c = (sd.version >> 20) & 0x3ff;
7741 uint64_t d = (sd.version >> 10) & 0x3ff;
7742 uint64_t e = sd.version & 0x3ff;
7743 outs() << " version " << a << "." << b;
7745 outs() << "." << c << "." << d << "." << e;
7747 outs() << "." << c << "." << d;
7753 static void PrintEntryPointCommand(MachO::entry_point_command ep) {
7754 outs() << " cmd LC_MAIN\n";
7755 outs() << " cmdsize " << ep.cmdsize;
7756 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
7757 outs() << " Incorrect size\n";
7760 outs() << " entryoff " << ep.entryoff << "\n";
7761 outs() << " stacksize " << ep.stacksize << "\n";
7764 static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
7765 uint32_t object_size) {
7766 outs() << " cmd LC_ENCRYPTION_INFO\n";
7767 outs() << " cmdsize " << ec.cmdsize;
7768 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
7769 outs() << " Incorrect size\n";
7772 outs() << " cryptoff " << ec.cryptoff;
7773 if (ec.cryptoff > object_size)
7774 outs() << " (past end of file)\n";
7777 outs() << " cryptsize " << ec.cryptsize;
7778 if (ec.cryptsize > object_size)
7779 outs() << " (past end of file)\n";
7782 outs() << " cryptid " << ec.cryptid << "\n";
7785 static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
7786 uint32_t object_size) {
7787 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
7788 outs() << " cmdsize " << ec.cmdsize;
7789 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
7790 outs() << " Incorrect size\n";
7793 outs() << " cryptoff " << ec.cryptoff;
7794 if (ec.cryptoff > object_size)
7795 outs() << " (past end of file)\n";
7798 outs() << " cryptsize " << ec.cryptsize;
7799 if (ec.cryptsize > object_size)
7800 outs() << " (past end of file)\n";
7803 outs() << " cryptid " << ec.cryptid << "\n";
7804 outs() << " pad " << ec.pad << "\n";
7807 static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
7809 outs() << " cmd LC_LINKER_OPTION\n";
7810 outs() << " cmdsize " << lo.cmdsize;
7811 if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
7812 outs() << " Incorrect size\n";
7815 outs() << " count " << lo.count << "\n";
7816 const char *string = Ptr + sizeof(struct MachO::linker_option_command);
7817 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
7820 while (*string == '\0' && left > 0) {
7826 outs() << " string #" << i << " " << format("%.*s\n", left, string);
7827 uint32_t NullPos = StringRef(string, left).find('\0');
7828 uint32_t len = std::min(NullPos, left) + 1;
7834 outs() << " count " << lo.count << " does not match number of strings "
7838 static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
7840 outs() << " cmd LC_SUB_FRAMEWORK\n";
7841 outs() << " cmdsize " << sub.cmdsize;
7842 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
7843 outs() << " Incorrect size\n";
7846 if (sub.umbrella < sub.cmdsize) {
7847 const char *P = Ptr + sub.umbrella;
7848 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
7850 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
7854 static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
7856 outs() << " cmd LC_SUB_UMBRELLA\n";
7857 outs() << " cmdsize " << sub.cmdsize;
7858 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
7859 outs() << " Incorrect size\n";
7862 if (sub.sub_umbrella < sub.cmdsize) {
7863 const char *P = Ptr + sub.sub_umbrella;
7864 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
7866 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
7870 static void PrintSubLibraryCommand(MachO::sub_library_command sub,
7872 outs() << " cmd LC_SUB_LIBRARY\n";
7873 outs() << " cmdsize " << sub.cmdsize;
7874 if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
7875 outs() << " Incorrect size\n";
7878 if (sub.sub_library < sub.cmdsize) {
7879 const char *P = Ptr + sub.sub_library;
7880 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
7882 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
7886 static void PrintSubClientCommand(MachO::sub_client_command sub,
7888 outs() << " cmd LC_SUB_CLIENT\n";
7889 outs() << " cmdsize " << sub.cmdsize;
7890 if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
7891 outs() << " Incorrect size\n";
7894 if (sub.client < sub.cmdsize) {
7895 const char *P = Ptr + sub.client;
7896 outs() << " client " << P << " (offset " << sub.client << ")\n";
7898 outs() << " client ?(bad offset " << sub.client << ")\n";
7902 static void PrintRoutinesCommand(MachO::routines_command r) {
7903 outs() << " cmd LC_ROUTINES\n";
7904 outs() << " cmdsize " << r.cmdsize;
7905 if (r.cmdsize != sizeof(struct MachO::routines_command))
7906 outs() << " Incorrect size\n";
7909 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
7910 outs() << " init_module " << r.init_module << "\n";
7911 outs() << " reserved1 " << r.reserved1 << "\n";
7912 outs() << " reserved2 " << r.reserved2 << "\n";
7913 outs() << " reserved3 " << r.reserved3 << "\n";
7914 outs() << " reserved4 " << r.reserved4 << "\n";
7915 outs() << " reserved5 " << r.reserved5 << "\n";
7916 outs() << " reserved6 " << r.reserved6 << "\n";
7919 static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
7920 outs() << " cmd LC_ROUTINES_64\n";
7921 outs() << " cmdsize " << r.cmdsize;
7922 if (r.cmdsize != sizeof(struct MachO::routines_command_64))
7923 outs() << " Incorrect size\n";
7926 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
7927 outs() << " init_module " << r.init_module << "\n";
7928 outs() << " reserved1 " << r.reserved1 << "\n";
7929 outs() << " reserved2 " << r.reserved2 << "\n";
7930 outs() << " reserved3 " << r.reserved3 << "\n";
7931 outs() << " reserved4 " << r.reserved4 << "\n";
7932 outs() << " reserved5 " << r.reserved5 << "\n";
7933 outs() << " reserved6 " << r.reserved6 << "\n";
7936 static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
7937 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
7938 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
7939 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
7940 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
7941 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
7942 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
7943 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
7944 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
7945 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
7946 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
7947 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
7948 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
7949 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
7950 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
7951 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
7952 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
7953 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
7954 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
7955 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
7956 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
7957 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
7960 static void Print_mmst_reg(MachO::mmst_reg_t &r) {
7962 outs() << "\t mmst_reg ";
7963 for (f = 0; f < 10; f++)
7964 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
7966 outs() << "\t mmst_rsrv ";
7967 for (f = 0; f < 6; f++)
7968 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
7972 static void Print_xmm_reg(MachO::xmm_reg_t &r) {
7974 outs() << "\t xmm_reg ";
7975 for (f = 0; f < 16; f++)
7976 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
7980 static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
7981 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
7982 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
7983 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
7984 outs() << " denorm " << fpu.fpu_fcw.denorm;
7985 outs() << " zdiv " << fpu.fpu_fcw.zdiv;
7986 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
7987 outs() << " undfl " << fpu.fpu_fcw.undfl;
7988 outs() << " precis " << fpu.fpu_fcw.precis << "\n";
7989 outs() << "\t\t pc ";
7990 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
7991 outs() << "FP_PREC_24B ";
7992 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
7993 outs() << "FP_PREC_53B ";
7994 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
7995 outs() << "FP_PREC_64B ";
7997 outs() << fpu.fpu_fcw.pc << " ";
7999 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
8000 outs() << "FP_RND_NEAR ";
8001 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
8002 outs() << "FP_RND_DOWN ";
8003 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
8004 outs() << "FP_RND_UP ";
8005 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
8006 outs() << "FP_CHOP ";
8008 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
8009 outs() << " denorm " << fpu.fpu_fsw.denorm;
8010 outs() << " zdiv " << fpu.fpu_fsw.zdiv;
8011 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
8012 outs() << " undfl " << fpu.fpu_fsw.undfl;
8013 outs() << " precis " << fpu.fpu_fsw.precis;
8014 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
8015 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
8016 outs() << " c0 " << fpu.fpu_fsw.c0;
8017 outs() << " c1 " << fpu.fpu_fsw.c1;
8018 outs() << " c2 " << fpu.fpu_fsw.c2;
8019 outs() << " tos " << fpu.fpu_fsw.tos;
8020 outs() << " c3 " << fpu.fpu_fsw.c3;
8021 outs() << " busy " << fpu.fpu_fsw.busy << "\n";
8022 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
8023 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
8024 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
8025 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
8026 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
8027 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
8028 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
8029 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
8030 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
8031 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
8032 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
8034 outs() << "\t fpu_stmm0:\n";
8035 Print_mmst_reg(fpu.fpu_stmm0);
8036 outs() << "\t fpu_stmm1:\n";
8037 Print_mmst_reg(fpu.fpu_stmm1);
8038 outs() << "\t fpu_stmm2:\n";
8039 Print_mmst_reg(fpu.fpu_stmm2);
8040 outs() << "\t fpu_stmm3:\n";
8041 Print_mmst_reg(fpu.fpu_stmm3);
8042 outs() << "\t fpu_stmm4:\n";
8043 Print_mmst_reg(fpu.fpu_stmm4);
8044 outs() << "\t fpu_stmm5:\n";
8045 Print_mmst_reg(fpu.fpu_stmm5);
8046 outs() << "\t fpu_stmm6:\n";
8047 Print_mmst_reg(fpu.fpu_stmm6);
8048 outs() << "\t fpu_stmm7:\n";
8049 Print_mmst_reg(fpu.fpu_stmm7);
8050 outs() << "\t fpu_xmm0:\n";
8051 Print_xmm_reg(fpu.fpu_xmm0);
8052 outs() << "\t fpu_xmm1:\n";
8053 Print_xmm_reg(fpu.fpu_xmm1);
8054 outs() << "\t fpu_xmm2:\n";
8055 Print_xmm_reg(fpu.fpu_xmm2);
8056 outs() << "\t fpu_xmm3:\n";
8057 Print_xmm_reg(fpu.fpu_xmm3);
8058 outs() << "\t fpu_xmm4:\n";
8059 Print_xmm_reg(fpu.fpu_xmm4);
8060 outs() << "\t fpu_xmm5:\n";
8061 Print_xmm_reg(fpu.fpu_xmm5);
8062 outs() << "\t fpu_xmm6:\n";
8063 Print_xmm_reg(fpu.fpu_xmm6);
8064 outs() << "\t fpu_xmm7:\n";
8065 Print_xmm_reg(fpu.fpu_xmm7);
8066 outs() << "\t fpu_xmm8:\n";
8067 Print_xmm_reg(fpu.fpu_xmm8);
8068 outs() << "\t fpu_xmm9:\n";
8069 Print_xmm_reg(fpu.fpu_xmm9);
8070 outs() << "\t fpu_xmm10:\n";
8071 Print_xmm_reg(fpu.fpu_xmm10);
8072 outs() << "\t fpu_xmm11:\n";
8073 Print_xmm_reg(fpu.fpu_xmm11);
8074 outs() << "\t fpu_xmm12:\n";
8075 Print_xmm_reg(fpu.fpu_xmm12);
8076 outs() << "\t fpu_xmm13:\n";
8077 Print_xmm_reg(fpu.fpu_xmm13);
8078 outs() << "\t fpu_xmm14:\n";
8079 Print_xmm_reg(fpu.fpu_xmm14);
8080 outs() << "\t fpu_xmm15:\n";
8081 Print_xmm_reg(fpu.fpu_xmm15);
8082 outs() << "\t fpu_rsrv4:\n";
8083 for (uint32_t f = 0; f < 6; f++) {
8085 for (uint32_t g = 0; g < 16; g++)
8086 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
8089 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
8093 static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
8094 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
8095 outs() << " err " << format("0x%08" PRIx32, exc64.err);
8096 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
8099 static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
8100 bool isLittleEndian, uint32_t cputype) {
8101 if (t.cmd == MachO::LC_THREAD)
8102 outs() << " cmd LC_THREAD\n";
8103 else if (t.cmd == MachO::LC_UNIXTHREAD)
8104 outs() << " cmd LC_UNIXTHREAD\n";
8106 outs() << " cmd " << t.cmd << " (unknown)\n";
8107 outs() << " cmdsize " << t.cmdsize;
8108 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
8109 outs() << " Incorrect size\n";
8113 const char *begin = Ptr + sizeof(struct MachO::thread_command);
8114 const char *end = Ptr + t.cmdsize;
8115 uint32_t flavor, count, left;
8116 if (cputype == MachO::CPU_TYPE_X86_64) {
8117 while (begin < end) {
8118 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8119 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8120 begin += sizeof(uint32_t);
8125 if (isLittleEndian != sys::IsLittleEndianHost)
8126 sys::swapByteOrder(flavor);
8127 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8128 memcpy((char *)&count, begin, sizeof(uint32_t));
8129 begin += sizeof(uint32_t);
8134 if (isLittleEndian != sys::IsLittleEndianHost)
8135 sys::swapByteOrder(count);
8136 if (flavor == MachO::x86_THREAD_STATE64) {
8137 outs() << " flavor x86_THREAD_STATE64\n";
8138 if (count == MachO::x86_THREAD_STATE64_COUNT)
8139 outs() << " count x86_THREAD_STATE64_COUNT\n";
8141 outs() << " count " << count
8142 << " (not x86_THREAD_STATE64_COUNT)\n";
8143 MachO::x86_thread_state64_t cpu64;
8145 if (left >= sizeof(MachO::x86_thread_state64_t)) {
8146 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
8147 begin += sizeof(MachO::x86_thread_state64_t);
8149 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
8150 memcpy(&cpu64, begin, left);
8153 if (isLittleEndian != sys::IsLittleEndianHost)
8155 Print_x86_thread_state64_t(cpu64);
8156 } else if (flavor == MachO::x86_THREAD_STATE) {
8157 outs() << " flavor x86_THREAD_STATE\n";
8158 if (count == MachO::x86_THREAD_STATE_COUNT)
8159 outs() << " count x86_THREAD_STATE_COUNT\n";
8161 outs() << " count " << count
8162 << " (not x86_THREAD_STATE_COUNT)\n";
8163 struct MachO::x86_thread_state_t ts;
8165 if (left >= sizeof(MachO::x86_thread_state_t)) {
8166 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
8167 begin += sizeof(MachO::x86_thread_state_t);
8169 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
8170 memcpy(&ts, begin, left);
8173 if (isLittleEndian != sys::IsLittleEndianHost)
8175 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
8176 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
8177 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
8178 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
8180 outs() << "tsh.count " << ts.tsh.count
8181 << " (not x86_THREAD_STATE64_COUNT\n";
8182 Print_x86_thread_state64_t(ts.uts.ts64);
8184 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
8185 << ts.tsh.count << "\n";
8187 } else if (flavor == MachO::x86_FLOAT_STATE) {
8188 outs() << " flavor x86_FLOAT_STATE\n";
8189 if (count == MachO::x86_FLOAT_STATE_COUNT)
8190 outs() << " count x86_FLOAT_STATE_COUNT\n";
8192 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
8193 struct MachO::x86_float_state_t fs;
8195 if (left >= sizeof(MachO::x86_float_state_t)) {
8196 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
8197 begin += sizeof(MachO::x86_float_state_t);
8199 memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
8200 memcpy(&fs, begin, left);
8203 if (isLittleEndian != sys::IsLittleEndianHost)
8205 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
8206 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
8207 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
8208 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
8210 outs() << "fsh.count " << fs.fsh.count
8211 << " (not x86_FLOAT_STATE64_COUNT\n";
8212 Print_x86_float_state_t(fs.ufs.fs64);
8214 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
8215 << fs.fsh.count << "\n";
8217 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
8218 outs() << " flavor x86_EXCEPTION_STATE\n";
8219 if (count == MachO::x86_EXCEPTION_STATE_COUNT)
8220 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
8222 outs() << " count " << count
8223 << " (not x86_EXCEPTION_STATE_COUNT)\n";
8224 struct MachO::x86_exception_state_t es;
8226 if (left >= sizeof(MachO::x86_exception_state_t)) {
8227 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
8228 begin += sizeof(MachO::x86_exception_state_t);
8230 memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
8231 memcpy(&es, begin, left);
8234 if (isLittleEndian != sys::IsLittleEndianHost)
8236 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
8237 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
8238 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
8239 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
8241 outs() << "\t esh.count " << es.esh.count
8242 << " (not x86_EXCEPTION_STATE64_COUNT\n";
8243 Print_x86_exception_state_t(es.ues.es64);
8245 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
8246 << es.esh.count << "\n";
8249 outs() << " flavor " << flavor << " (unknown)\n";
8250 outs() << " count " << count << "\n";
8251 outs() << " state (unknown)\n";
8252 begin += count * sizeof(uint32_t);
8256 while (begin < end) {
8257 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8258 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8259 begin += sizeof(uint32_t);
8264 if (isLittleEndian != sys::IsLittleEndianHost)
8265 sys::swapByteOrder(flavor);
8266 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8267 memcpy((char *)&count, begin, sizeof(uint32_t));
8268 begin += sizeof(uint32_t);
8273 if (isLittleEndian != sys::IsLittleEndianHost)
8274 sys::swapByteOrder(count);
8275 outs() << " flavor " << flavor << "\n";
8276 outs() << " count " << count << "\n";
8277 outs() << " state (Unknown cputype/cpusubtype)\n";
8278 begin += count * sizeof(uint32_t);
8283 static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
8284 if (dl.cmd == MachO::LC_ID_DYLIB)
8285 outs() << " cmd LC_ID_DYLIB\n";
8286 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
8287 outs() << " cmd LC_LOAD_DYLIB\n";
8288 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
8289 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
8290 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
8291 outs() << " cmd LC_REEXPORT_DYLIB\n";
8292 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
8293 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
8294 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
8295 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
8297 outs() << " cmd " << dl.cmd << " (unknown)\n";
8298 outs() << " cmdsize " << dl.cmdsize;
8299 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
8300 outs() << " Incorrect size\n";
8303 if (dl.dylib.name < dl.cmdsize) {
8304 const char *P = (const char *)(Ptr) + dl.dylib.name;
8305 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
8307 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
8309 outs() << " time stamp " << dl.dylib.timestamp << " ";
8310 time_t t = dl.dylib.timestamp;
8311 outs() << ctime(&t);
8312 outs() << " current version ";
8313 if (dl.dylib.current_version == 0xffffffff)
8316 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
8317 << ((dl.dylib.current_version >> 8) & 0xff) << "."
8318 << (dl.dylib.current_version & 0xff) << "\n";
8319 outs() << "compatibility version ";
8320 if (dl.dylib.compatibility_version == 0xffffffff)
8323 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
8324 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
8325 << (dl.dylib.compatibility_version & 0xff) << "\n";
8328 static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
8329 uint32_t object_size) {
8330 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
8331 outs() << " cmd LC_FUNCTION_STARTS\n";
8332 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
8333 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
8334 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
8335 outs() << " cmd LC_FUNCTION_STARTS\n";
8336 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
8337 outs() << " cmd LC_DATA_IN_CODE\n";
8338 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
8339 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
8340 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
8341 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
8343 outs() << " cmd " << ld.cmd << " (?)\n";
8344 outs() << " cmdsize " << ld.cmdsize;
8345 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
8346 outs() << " Incorrect size\n";
8349 outs() << " dataoff " << ld.dataoff;
8350 if (ld.dataoff > object_size)
8351 outs() << " (past end of file)\n";
8354 outs() << " datasize " << ld.datasize;
8355 uint64_t big_size = ld.dataoff;
8356 big_size += ld.datasize;
8357 if (big_size > object_size)
8358 outs() << " (past end of file)\n";
8363 static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t ncmds,
8364 uint32_t filetype, uint32_t cputype,
8368 StringRef Buf = Obj->getData();
8369 MachOObjectFile::LoadCommandInfo Command = Obj->getFirstLoadCommandInfo();
8370 for (unsigned i = 0;; ++i) {
8371 outs() << "Load command " << i << "\n";
8372 if (Command.C.cmd == MachO::LC_SEGMENT) {
8373 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
8374 const char *sg_segname = SLC.segname;
8375 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
8376 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
8377 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
8379 for (unsigned j = 0; j < SLC.nsects; j++) {
8380 MachO::section S = Obj->getSection(Command, j);
8381 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
8382 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
8383 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
8385 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
8386 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
8387 const char *sg_segname = SLC_64.segname;
8388 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
8389 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
8390 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
8391 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
8392 for (unsigned j = 0; j < SLC_64.nsects; j++) {
8393 MachO::section_64 S_64 = Obj->getSection64(Command, j);
8394 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
8395 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
8396 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
8397 sg_segname, filetype, Buf.size(), verbose);
8399 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
8400 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
8401 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
8402 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
8403 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
8404 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
8405 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
8407 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
8408 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
8409 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
8410 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
8411 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
8412 Command.C.cmd == MachO::LC_ID_DYLINKER ||
8413 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
8414 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
8415 PrintDyldLoadCommand(Dyld, Command.Ptr);
8416 } else if (Command.C.cmd == MachO::LC_UUID) {
8417 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
8418 PrintUuidLoadCommand(Uuid);
8419 } else if (Command.C.cmd == MachO::LC_RPATH) {
8420 MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
8421 PrintRpathLoadCommand(Rpath, Command.Ptr);
8422 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
8423 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS) {
8424 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
8425 PrintVersionMinLoadCommand(Vd);
8426 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
8427 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
8428 PrintSourceVersionCommand(Sd);
8429 } else if (Command.C.cmd == MachO::LC_MAIN) {
8430 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
8431 PrintEntryPointCommand(Ep);
8432 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
8433 MachO::encryption_info_command Ei =
8434 Obj->getEncryptionInfoCommand(Command);
8435 PrintEncryptionInfoCommand(Ei, Buf.size());
8436 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
8437 MachO::encryption_info_command_64 Ei =
8438 Obj->getEncryptionInfoCommand64(Command);
8439 PrintEncryptionInfoCommand64(Ei, Buf.size());
8440 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
8441 MachO::linker_option_command Lo =
8442 Obj->getLinkerOptionLoadCommand(Command);
8443 PrintLinkerOptionCommand(Lo, Command.Ptr);
8444 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
8445 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
8446 PrintSubFrameworkCommand(Sf, Command.Ptr);
8447 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
8448 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
8449 PrintSubUmbrellaCommand(Sf, Command.Ptr);
8450 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
8451 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
8452 PrintSubLibraryCommand(Sl, Command.Ptr);
8453 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
8454 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
8455 PrintSubClientCommand(Sc, Command.Ptr);
8456 } else if (Command.C.cmd == MachO::LC_ROUTINES) {
8457 MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
8458 PrintRoutinesCommand(Rc);
8459 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
8460 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
8461 PrintRoutinesCommand64(Rc);
8462 } else if (Command.C.cmd == MachO::LC_THREAD ||
8463 Command.C.cmd == MachO::LC_UNIXTHREAD) {
8464 MachO::thread_command Tc = Obj->getThreadCommand(Command);
8465 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
8466 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
8467 Command.C.cmd == MachO::LC_ID_DYLIB ||
8468 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
8469 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
8470 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
8471 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
8472 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
8473 PrintDylibCommand(Dl, Command.Ptr);
8474 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
8475 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
8476 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
8477 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
8478 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
8479 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
8480 MachO::linkedit_data_command Ld =
8481 Obj->getLinkeditDataLoadCommand(Command);
8482 PrintLinkEditDataCommand(Ld, Buf.size());
8484 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
8486 outs() << " cmdsize " << Command.C.cmdsize << "\n";
8487 // TODO: get and print the raw bytes of the load command.
8489 // TODO: print all the other kinds of load commands.
8493 Command = Obj->getNextLoadCommandInfo(Command);
8497 static void getAndPrintMachHeader(const MachOObjectFile *Obj, uint32_t &ncmds,
8498 uint32_t &filetype, uint32_t &cputype,
8500 if (Obj->is64Bit()) {
8501 MachO::mach_header_64 H_64;
8502 H_64 = Obj->getHeader64();
8503 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
8504 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
8506 filetype = H_64.filetype;
8507 cputype = H_64.cputype;
8509 MachO::mach_header H;
8510 H = Obj->getHeader();
8511 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
8512 H.sizeofcmds, H.flags, verbose);
8514 filetype = H.filetype;
8515 cputype = H.cputype;
8519 void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
8520 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
8522 uint32_t filetype = 0;
8523 uint32_t cputype = 0;
8524 getAndPrintMachHeader(file, ncmds, filetype, cputype, !NonVerbose);
8525 PrintLoadCommands(file, ncmds, filetype, cputype, !NonVerbose);
8528 //===----------------------------------------------------------------------===//
8529 // export trie dumping
8530 //===----------------------------------------------------------------------===//
8532 void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
8533 for (const llvm::object::ExportEntry &Entry : Obj->exports()) {
8534 uint64_t Flags = Entry.flags();
8535 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
8536 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
8537 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8538 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
8539 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8540 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
8541 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
8543 outs() << "[re-export] ";
8545 outs() << format("0x%08llX ",
8546 Entry.address()); // FIXME:add in base address
8547 outs() << Entry.name();
8548 if (WeakDef || ThreadLocal || Resolver || Abs) {
8549 bool NeedsComma = false;
8552 outs() << "weak_def";
8558 outs() << "per-thread";
8564 outs() << "absolute";
8570 outs() << format("resolver=0x%08llX", Entry.other());
8576 StringRef DylibName = "unknown";
8577 int Ordinal = Entry.other() - 1;
8578 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
8579 if (Entry.otherName().empty())
8580 outs() << " (from " << DylibName << ")";
8582 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
8588 //===----------------------------------------------------------------------===//
8589 // rebase table dumping
8590 //===----------------------------------------------------------------------===//
8595 SegInfo(const object::MachOObjectFile *Obj);
8597 StringRef segmentName(uint32_t SegIndex);
8598 StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset);
8599 uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
8602 struct SectionInfo {
8605 StringRef SectionName;
8606 StringRef SegmentName;
8607 uint64_t OffsetInSegment;
8608 uint64_t SegmentStartAddress;
8609 uint32_t SegmentIndex;
8611 const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset);
8612 SmallVector<SectionInfo, 32> Sections;
8616 SegInfo::SegInfo(const object::MachOObjectFile *Obj) {
8617 // Build table of sections so segIndex/offset pairs can be translated.
8618 uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
8619 StringRef CurSegName;
8620 uint64_t CurSegAddress;
8621 for (const SectionRef &Section : Obj->sections()) {
8623 if (error(Section.getName(Info.SectionName)))
8625 Info.Address = Section.getAddress();
8626 Info.Size = Section.getSize();
8628 Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
8629 if (!Info.SegmentName.equals(CurSegName)) {
8631 CurSegName = Info.SegmentName;
8632 CurSegAddress = Info.Address;
8634 Info.SegmentIndex = CurSegIndex - 1;
8635 Info.OffsetInSegment = Info.Address - CurSegAddress;
8636 Info.SegmentStartAddress = CurSegAddress;
8637 Sections.push_back(Info);
8641 StringRef SegInfo::segmentName(uint32_t SegIndex) {
8642 for (const SectionInfo &SI : Sections) {
8643 if (SI.SegmentIndex == SegIndex)
8644 return SI.SegmentName;
8646 llvm_unreachable("invalid segIndex");
8649 const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex,
8650 uint64_t OffsetInSeg) {
8651 for (const SectionInfo &SI : Sections) {
8652 if (SI.SegmentIndex != SegIndex)
8654 if (SI.OffsetInSegment > OffsetInSeg)
8656 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
8660 llvm_unreachable("segIndex and offset not in any section");
8663 StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) {
8664 return findSection(SegIndex, OffsetInSeg).SectionName;
8667 uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) {
8668 const SectionInfo &SI = findSection(SegIndex, OffsetInSeg);
8669 return SI.SegmentStartAddress + OffsetInSeg;
8672 void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) {
8673 // Build table of sections so names can used in final output.
8674 SegInfo sectionTable(Obj);
8676 outs() << "segment section address type\n";
8677 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) {
8678 uint32_t SegIndex = Entry.segmentIndex();
8679 uint64_t OffsetInSeg = Entry.segmentOffset();
8680 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8681 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8682 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8684 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
8685 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
8686 SegmentName.str().c_str(), SectionName.str().c_str(),
8687 Address, Entry.typeName().str().c_str());
8691 static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
8692 StringRef DylibName;
8694 case MachO::BIND_SPECIAL_DYLIB_SELF:
8695 return "this-image";
8696 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
8697 return "main-executable";
8698 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
8699 return "flat-namespace";
8702 std::error_code EC =
8703 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
8705 return "<<bad library ordinal>>";
8709 return "<<unknown special ordinal>>";
8712 //===----------------------------------------------------------------------===//
8713 // bind table dumping
8714 //===----------------------------------------------------------------------===//
8716 void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) {
8717 // Build table of sections so names can used in final output.
8718 SegInfo sectionTable(Obj);
8720 outs() << "segment section address type "
8721 "addend dylib symbol\n";
8722 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) {
8723 uint32_t SegIndex = Entry.segmentIndex();
8724 uint64_t OffsetInSeg = Entry.segmentOffset();
8725 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8726 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8727 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8729 // Table lines look like:
8730 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
8732 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
8733 Attr = " (weak_import)";
8734 outs() << left_justify(SegmentName, 8) << " "
8735 << left_justify(SectionName, 18) << " "
8736 << format_hex(Address, 10, true) << " "
8737 << left_justify(Entry.typeName(), 8) << " "
8738 << format_decimal(Entry.addend(), 8) << " "
8739 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
8740 << Entry.symbolName() << Attr << "\n";
8744 //===----------------------------------------------------------------------===//
8745 // lazy bind table dumping
8746 //===----------------------------------------------------------------------===//
8748 void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) {
8749 // Build table of sections so names can used in final output.
8750 SegInfo sectionTable(Obj);
8752 outs() << "segment section address "
8754 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) {
8755 uint32_t SegIndex = Entry.segmentIndex();
8756 uint64_t OffsetInSeg = Entry.segmentOffset();
8757 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8758 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8759 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8761 // Table lines look like:
8762 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
8763 outs() << left_justify(SegmentName, 8) << " "
8764 << left_justify(SectionName, 18) << " "
8765 << format_hex(Address, 10, true) << " "
8766 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
8767 << Entry.symbolName() << "\n";
8771 //===----------------------------------------------------------------------===//
8772 // weak bind table dumping
8773 //===----------------------------------------------------------------------===//
8775 void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) {
8776 // Build table of sections so names can used in final output.
8777 SegInfo sectionTable(Obj);
8779 outs() << "segment section address "
8780 "type addend symbol\n";
8781 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) {
8782 // Strong symbols don't have a location to update.
8783 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
8784 outs() << " strong "
8785 << Entry.symbolName() << "\n";
8788 uint32_t SegIndex = Entry.segmentIndex();
8789 uint64_t OffsetInSeg = Entry.segmentOffset();
8790 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8791 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8792 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8794 // Table lines look like:
8795 // __DATA __data 0x00001000 pointer 0 _foo
8796 outs() << left_justify(SegmentName, 8) << " "
8797 << left_justify(SectionName, 18) << " "
8798 << format_hex(Address, 10, true) << " "
8799 << left_justify(Entry.typeName(), 8) << " "
8800 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
8805 // get_dyld_bind_info_symbolname() is used for disassembly and passed an
8806 // address, ReferenceValue, in the Mach-O file and looks in the dyld bind
8807 // information for that address. If the address is found its binding symbol
8808 // name is returned. If not nullptr is returned.
8809 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
8810 struct DisassembleInfo *info) {
8811 if (info->bindtable == nullptr) {
8812 info->bindtable = new (BindTable);
8813 SegInfo sectionTable(info->O);
8814 for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable()) {
8815 uint32_t SegIndex = Entry.segmentIndex();
8816 uint64_t OffsetInSeg = Entry.segmentOffset();
8817 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8818 const char *SymbolName = nullptr;
8819 StringRef name = Entry.symbolName();
8821 SymbolName = name.data();
8822 info->bindtable->push_back(std::make_pair(Address, SymbolName));
8825 for (bind_table_iterator BI = info->bindtable->begin(),
8826 BE = info->bindtable->end();
8828 uint64_t Address = BI->first;
8829 if (ReferenceValue == Address) {
8830 const char *SymbolName = BI->second;