1 //===-- MachODump.cpp - Object file dumping utility for llvm --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the MachO-specific dumper for llvm-objdump.
12 //===----------------------------------------------------------------------===//
14 #include "llvm-objdump.h"
15 #include "llvm-c/Disassembler.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/Config/config.h"
20 #include "llvm/DebugInfo/DIContext.h"
21 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCDisassembler.h"
25 #include "llvm/MC/MCInst.h"
26 #include "llvm/MC/MCInstPrinter.h"
27 #include "llvm/MC/MCInstrDesc.h"
28 #include "llvm/MC/MCInstrInfo.h"
29 #include "llvm/MC/MCRegisterInfo.h"
30 #include "llvm/MC/MCSubtargetInfo.h"
31 #include "llvm/Object/MachO.h"
32 #include "llvm/Object/MachOUniversal.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/Endian.h"
37 #include "llvm/Support/Format.h"
38 #include "llvm/Support/FormattedStream.h"
39 #include "llvm/Support/GraphWriter.h"
40 #include "llvm/Support/LEB128.h"
41 #include "llvm/Support/MachO.h"
42 #include "llvm/Support/MemoryBuffer.h"
43 #include "llvm/Support/TargetRegistry.h"
44 #include "llvm/Support/TargetSelect.h"
45 #include "llvm/Support/raw_ostream.h"
48 #include <system_error>
55 using namespace object;
59 cl::desc("Print line information from debug info if available"));
61 static cl::opt<std::string> DSYMFile("dsym",
62 cl::desc("Use .dSYM file for debug info"));
64 static cl::opt<bool> FullLeadingAddr("full-leading-addr",
65 cl::desc("Print full leading address"));
67 static cl::opt<bool> NoLeadingAddr("no-leading-addr",
68 cl::desc("Print no leading address"));
70 cl::opt<bool> llvm::UniversalHeaders("universal-headers",
71 cl::desc("Print Mach-O universal headers "
72 "(requires -macho)"));
75 llvm::ArchiveHeaders("archive-headers",
76 cl::desc("Print archive headers for Mach-O archives "
77 "(requires -macho)"));
80 ArchiveMemberOffsets("archive-member-offsets",
81 cl::desc("Print the offset to each archive member for "
82 "Mach-O archives (requires -macho and "
83 "-archive-headers)"));
86 llvm::IndirectSymbols("indirect-symbols",
87 cl::desc("Print indirect symbol table for Mach-O "
88 "objects (requires -macho)"));
91 llvm::DataInCode("data-in-code",
92 cl::desc("Print the data in code table for Mach-O objects "
93 "(requires -macho)"));
96 llvm::LinkOptHints("link-opt-hints",
97 cl::desc("Print the linker optimization hints for "
98 "Mach-O objects (requires -macho)"));
100 cl::list<std::string>
101 llvm::DumpSections("section",
102 cl::desc("Prints the specified segment,section for "
103 "Mach-O objects (requires -macho)"));
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 uint64_t AAddr = (A.getType() != SymbolRef::ST_Function) ? 0 : A.getValue();
179 uint64_t BAddr = (B.getType() != SymbolRef::ST_Function) ? 0 : B.getValue();
180 return AAddr < BAddr;
184 // Types for the storted data in code table that is built before disassembly
185 // and the predicate function to sort them.
186 typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
187 typedef std::vector<DiceTableEntry> DiceTable;
188 typedef DiceTable::iterator dice_table_iterator;
190 // This is used to search for a data in code table entry for the PC being
191 // disassembled. The j parameter has the PC in j.first. A single data in code
192 // table entry can cover many bytes for each of its Kind's. So if the offset,
193 // aka the i.first value, of the data in code table entry plus its Length
194 // covers the PC being searched for this will return true. If not it will
196 static bool compareDiceTableEntries(const DiceTableEntry &i,
197 const DiceTableEntry &j) {
199 i.second.getLength(Length);
201 return j.first >= i.first && j.first < i.first + Length;
204 static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
205 unsigned short Kind) {
206 uint32_t Value, Size = 1;
210 case MachO::DICE_KIND_DATA:
213 dumpBytes(ArrayRef<uint8_t>(bytes, 4), outs());
214 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
215 outs() << "\t.long " << Value;
217 } else if (Length >= 2) {
219 dumpBytes(ArrayRef<uint8_t>(bytes, 2), outs());
220 Value = bytes[1] << 8 | bytes[0];
221 outs() << "\t.short " << Value;
225 dumpBytes(ArrayRef<uint8_t>(bytes, 2), outs());
227 outs() << "\t.byte " << Value;
230 if (Kind == MachO::DICE_KIND_DATA)
231 outs() << "\t@ KIND_DATA\n";
233 outs() << "\t@ data in code kind = " << Kind << "\n";
235 case MachO::DICE_KIND_JUMP_TABLE8:
237 dumpBytes(ArrayRef<uint8_t>(bytes, 1), outs());
239 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
242 case MachO::DICE_KIND_JUMP_TABLE16:
244 dumpBytes(ArrayRef<uint8_t>(bytes, 2), outs());
245 Value = bytes[1] << 8 | bytes[0];
246 outs() << "\t.short " << format("%5u", Value & 0xffff)
247 << "\t@ KIND_JUMP_TABLE16\n";
250 case MachO::DICE_KIND_JUMP_TABLE32:
251 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
253 dumpBytes(ArrayRef<uint8_t>(bytes, 4), outs());
254 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
255 outs() << "\t.long " << Value;
256 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
257 outs() << "\t@ KIND_JUMP_TABLE32\n";
259 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
266 static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
267 std::vector<SectionRef> &Sections,
268 std::vector<SymbolRef> &Symbols,
269 SmallVectorImpl<uint64_t> &FoundFns,
270 uint64_t &BaseSegmentAddress) {
271 for (const SymbolRef &Symbol : MachOObj->symbols()) {
272 ErrorOr<StringRef> SymName = Symbol.getName();
273 if (std::error_code EC = SymName.getError())
274 report_fatal_error(EC.message());
275 if (!SymName->startswith("ltmp"))
276 Symbols.push_back(Symbol);
279 for (const SectionRef &Section : MachOObj->sections()) {
281 Section.getName(SectName);
282 Sections.push_back(Section);
285 bool BaseSegmentAddressSet = false;
286 for (const auto &Command : MachOObj->load_commands()) {
287 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
288 // We found a function starts segment, parse the addresses for later
290 MachO::linkedit_data_command LLC =
291 MachOObj->getLinkeditDataLoadCommand(Command);
293 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
294 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
295 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
296 StringRef SegName = SLC.segname;
297 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
298 BaseSegmentAddressSet = true;
299 BaseSegmentAddress = SLC.vmaddr;
305 static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
306 uint32_t n, uint32_t count,
307 uint32_t stride, uint64_t addr) {
308 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
309 uint32_t nindirectsyms = Dysymtab.nindirectsyms;
310 if (n > nindirectsyms)
311 outs() << " (entries start past the end of the indirect symbol "
312 "table) (reserved1 field greater than the table size)";
313 else if (n + count > nindirectsyms)
314 outs() << " (entries extends past the end of the indirect symbol "
317 uint32_t cputype = O->getHeader().cputype;
318 if (cputype & MachO::CPU_ARCH_ABI64)
319 outs() << "address index";
321 outs() << "address index";
326 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
327 if (cputype & MachO::CPU_ARCH_ABI64)
328 outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
330 outs() << format("0x%08" PRIx32, addr + j * stride) << " ";
331 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
332 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
333 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
337 if (indirect_symbol ==
338 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
339 outs() << "LOCAL ABSOLUTE\n";
342 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
343 outs() << "ABSOLUTE\n";
346 outs() << format("%5u ", indirect_symbol);
348 MachO::symtab_command Symtab = O->getSymtabLoadCommand();
349 if (indirect_symbol < Symtab.nsyms) {
350 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
351 SymbolRef Symbol = *Sym;
352 ErrorOr<StringRef> SymName = Symbol.getName();
353 if (std::error_code EC = SymName.getError())
354 report_fatal_error(EC.message());
364 static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
365 for (const auto &Load : O->load_commands()) {
366 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
367 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
368 for (unsigned J = 0; J < Seg.nsects; ++J) {
369 MachO::section_64 Sec = O->getSection64(Load, J);
370 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
371 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
372 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
373 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
374 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
375 section_type == MachO::S_SYMBOL_STUBS) {
377 if (section_type == MachO::S_SYMBOL_STUBS)
378 stride = Sec.reserved2;
382 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
383 << Sec.sectname << ") "
384 << "(size of stubs in reserved2 field is zero)\n";
387 uint32_t count = Sec.size / stride;
388 outs() << "Indirect symbols for (" << Sec.segname << ","
389 << Sec.sectname << ") " << count << " entries";
390 uint32_t n = Sec.reserved1;
391 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
394 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
395 MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
396 for (unsigned J = 0; J < Seg.nsects; ++J) {
397 MachO::section Sec = O->getSection(Load, J);
398 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
399 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
400 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
401 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
402 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
403 section_type == MachO::S_SYMBOL_STUBS) {
405 if (section_type == MachO::S_SYMBOL_STUBS)
406 stride = Sec.reserved2;
410 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
411 << Sec.sectname << ") "
412 << "(size of stubs in reserved2 field is zero)\n";
415 uint32_t count = Sec.size / stride;
416 outs() << "Indirect symbols for (" << Sec.segname << ","
417 << Sec.sectname << ") " << count << " entries";
418 uint32_t n = Sec.reserved1;
419 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
426 static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
427 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
428 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
429 outs() << "Data in code table (" << nentries << " entries)\n";
430 outs() << "offset length kind\n";
431 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
434 DI->getOffset(Offset);
435 outs() << format("0x%08" PRIx32, Offset) << " ";
437 DI->getLength(Length);
438 outs() << format("%6u", Length) << " ";
443 case MachO::DICE_KIND_DATA:
446 case MachO::DICE_KIND_JUMP_TABLE8:
447 outs() << "JUMP_TABLE8";
449 case MachO::DICE_KIND_JUMP_TABLE16:
450 outs() << "JUMP_TABLE16";
452 case MachO::DICE_KIND_JUMP_TABLE32:
453 outs() << "JUMP_TABLE32";
455 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
456 outs() << "ABS_JUMP_TABLE32";
459 outs() << format("0x%04" PRIx32, Kind);
463 outs() << format("0x%04" PRIx32, Kind);
468 static void PrintLinkOptHints(MachOObjectFile *O) {
469 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
470 const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
471 uint32_t nloh = LohLC.datasize;
472 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
473 for (uint32_t i = 0; i < nloh;) {
475 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
477 outs() << " identifier " << identifier << " ";
480 switch (identifier) {
482 outs() << "AdrpAdrp\n";
485 outs() << "AdrpLdr\n";
488 outs() << "AdrpAddLdr\n";
491 outs() << "AdrpLdrGotLdr\n";
494 outs() << "AdrpAddStr\n";
497 outs() << "AdrpLdrGotStr\n";
500 outs() << "AdrpAdd\n";
503 outs() << "AdrpLdrGot\n";
506 outs() << "Unknown identifier value\n";
509 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
511 outs() << " narguments " << narguments << "\n";
515 for (uint32_t j = 0; j < narguments; j++) {
516 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
518 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
525 static void PrintDylibs(MachOObjectFile *O, bool JustId) {
527 for (const auto &Load : O->load_commands()) {
528 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
529 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
530 Load.C.cmd == MachO::LC_LOAD_DYLIB ||
531 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
532 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
533 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
534 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
535 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
536 if (dl.dylib.name < dl.cmdsize) {
537 const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
542 outs() << " (compatibility version "
543 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
544 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
545 << (dl.dylib.compatibility_version & 0xff) << ",";
546 outs() << " current version "
547 << ((dl.dylib.current_version >> 16) & 0xffff) << "."
548 << ((dl.dylib.current_version >> 8) & 0xff) << "."
549 << (dl.dylib.current_version & 0xff) << ")\n";
552 outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
553 if (Load.C.cmd == MachO::LC_ID_DYLIB)
554 outs() << "LC_ID_DYLIB ";
555 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
556 outs() << "LC_LOAD_DYLIB ";
557 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
558 outs() << "LC_LOAD_WEAK_DYLIB ";
559 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
560 outs() << "LC_LAZY_LOAD_DYLIB ";
561 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
562 outs() << "LC_REEXPORT_DYLIB ";
563 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
564 outs() << "LC_LOAD_UPWARD_DYLIB ";
567 outs() << "command " << Index++ << "\n";
573 typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
575 static void CreateSymbolAddressMap(MachOObjectFile *O,
576 SymbolAddressMap *AddrMap) {
577 // Create a map of symbol addresses to symbol names.
578 for (const SymbolRef &Symbol : O->symbols()) {
579 SymbolRef::Type ST = Symbol.getType();
580 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
581 ST == SymbolRef::ST_Other) {
582 uint64_t Address = Symbol.getValue();
583 ErrorOr<StringRef> SymNameOrErr = Symbol.getName();
584 if (std::error_code EC = SymNameOrErr.getError())
585 report_fatal_error(EC.message());
586 StringRef SymName = *SymNameOrErr;
587 if (!SymName.startswith(".objc"))
588 (*AddrMap)[Address] = SymName;
593 // GuessSymbolName is passed the address of what might be a symbol and a
594 // pointer to the SymbolAddressMap. It returns the name of a symbol
595 // with that address or nullptr if no symbol is found with that address.
596 static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
597 const char *SymbolName = nullptr;
598 // A DenseMap can't lookup up some values.
599 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
600 StringRef name = AddrMap->lookup(value);
602 SymbolName = name.data();
607 static void DumpCstringChar(const char c) {
611 outs().write_escaped(p);
614 static void DumpCstringSection(MachOObjectFile *O, const char *sect,
615 uint32_t sect_size, uint64_t sect_addr,
616 bool print_addresses) {
617 for (uint32_t i = 0; i < sect_size; i++) {
618 if (print_addresses) {
620 outs() << format("%016" PRIx64, sect_addr + i) << " ";
622 outs() << format("%08" PRIx64, sect_addr + i) << " ";
624 for (; i < sect_size && sect[i] != '\0'; i++)
625 DumpCstringChar(sect[i]);
626 if (i < sect_size && sect[i] == '\0')
631 static void DumpLiteral4(uint32_t l, float f) {
632 outs() << format("0x%08" PRIx32, l);
633 if ((l & 0x7f800000) != 0x7f800000)
634 outs() << format(" (%.16e)\n", f);
637 outs() << " (+Infinity)\n";
638 else if (l == 0xff800000)
639 outs() << " (-Infinity)\n";
640 else if ((l & 0x00400000) == 0x00400000)
641 outs() << " (non-signaling Not-a-Number)\n";
643 outs() << " (signaling Not-a-Number)\n";
647 static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
648 uint32_t sect_size, uint64_t sect_addr,
649 bool print_addresses) {
650 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
651 if (print_addresses) {
653 outs() << format("%016" PRIx64, sect_addr + i) << " ";
655 outs() << format("%08" PRIx64, sect_addr + i) << " ";
658 memcpy(&f, sect + i, sizeof(float));
659 if (O->isLittleEndian() != sys::IsLittleEndianHost)
660 sys::swapByteOrder(f);
662 memcpy(&l, sect + i, sizeof(uint32_t));
663 if (O->isLittleEndian() != sys::IsLittleEndianHost)
664 sys::swapByteOrder(l);
669 static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
671 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
673 if (O->isLittleEndian()) {
680 // Hi is the high word, so this is equivalent to if(isfinite(d))
681 if ((Hi & 0x7ff00000) != 0x7ff00000)
682 outs() << format(" (%.16e)\n", d);
684 if (Hi == 0x7ff00000 && Lo == 0)
685 outs() << " (+Infinity)\n";
686 else if (Hi == 0xfff00000 && Lo == 0)
687 outs() << " (-Infinity)\n";
688 else if ((Hi & 0x00080000) == 0x00080000)
689 outs() << " (non-signaling Not-a-Number)\n";
691 outs() << " (signaling Not-a-Number)\n";
695 static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
696 uint32_t sect_size, uint64_t sect_addr,
697 bool print_addresses) {
698 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
699 if (print_addresses) {
701 outs() << format("%016" PRIx64, sect_addr + i) << " ";
703 outs() << format("%08" PRIx64, sect_addr + i) << " ";
706 memcpy(&d, sect + i, sizeof(double));
707 if (O->isLittleEndian() != sys::IsLittleEndianHost)
708 sys::swapByteOrder(d);
710 memcpy(&l0, sect + i, sizeof(uint32_t));
711 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
712 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
713 sys::swapByteOrder(l0);
714 sys::swapByteOrder(l1);
716 DumpLiteral8(O, l0, l1, d);
720 static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
721 outs() << format("0x%08" PRIx32, l0) << " ";
722 outs() << format("0x%08" PRIx32, l1) << " ";
723 outs() << format("0x%08" PRIx32, l2) << " ";
724 outs() << format("0x%08" PRIx32, l3) << "\n";
727 static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
728 uint32_t sect_size, uint64_t sect_addr,
729 bool print_addresses) {
730 for (uint32_t i = 0; i < sect_size; i += 16) {
731 if (print_addresses) {
733 outs() << format("%016" PRIx64, sect_addr + i) << " ";
735 outs() << format("%08" PRIx64, sect_addr + i) << " ";
737 uint32_t l0, l1, l2, l3;
738 memcpy(&l0, sect + i, sizeof(uint32_t));
739 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
740 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
741 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
742 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
743 sys::swapByteOrder(l0);
744 sys::swapByteOrder(l1);
745 sys::swapByteOrder(l2);
746 sys::swapByteOrder(l3);
748 DumpLiteral16(l0, l1, l2, l3);
752 static void DumpLiteralPointerSection(MachOObjectFile *O,
753 const SectionRef &Section,
754 const char *sect, uint32_t sect_size,
756 bool print_addresses) {
757 // Collect the literal sections in this Mach-O file.
758 std::vector<SectionRef> LiteralSections;
759 for (const SectionRef &Section : O->sections()) {
760 DataRefImpl Ref = Section.getRawDataRefImpl();
761 uint32_t section_type;
763 const MachO::section_64 Sec = O->getSection64(Ref);
764 section_type = Sec.flags & MachO::SECTION_TYPE;
766 const MachO::section Sec = O->getSection(Ref);
767 section_type = Sec.flags & MachO::SECTION_TYPE;
769 if (section_type == MachO::S_CSTRING_LITERALS ||
770 section_type == MachO::S_4BYTE_LITERALS ||
771 section_type == MachO::S_8BYTE_LITERALS ||
772 section_type == MachO::S_16BYTE_LITERALS)
773 LiteralSections.push_back(Section);
776 // Set the size of the literal pointer.
777 uint32_t lp_size = O->is64Bit() ? 8 : 4;
779 // Collect the external relocation symbols for the literal pointers.
780 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
781 for (const RelocationRef &Reloc : Section.relocations()) {
783 MachO::any_relocation_info RE;
784 bool isExtern = false;
785 Rel = Reloc.getRawDataRefImpl();
786 RE = O->getRelocation(Rel);
787 isExtern = O->getPlainRelocationExternal(RE);
789 uint64_t RelocOffset = Reloc.getOffset();
790 symbol_iterator RelocSym = Reloc.getSymbol();
791 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
794 array_pod_sort(Relocs.begin(), Relocs.end());
796 // Dump each literal pointer.
797 for (uint32_t i = 0; i < sect_size; i += lp_size) {
798 if (print_addresses) {
800 outs() << format("%016" PRIx64, sect_addr + i) << " ";
802 outs() << format("%08" PRIx64, sect_addr + i) << " ";
806 memcpy(&lp, sect + i, sizeof(uint64_t));
807 if (O->isLittleEndian() != sys::IsLittleEndianHost)
808 sys::swapByteOrder(lp);
811 memcpy(&li, sect + i, sizeof(uint32_t));
812 if (O->isLittleEndian() != sys::IsLittleEndianHost)
813 sys::swapByteOrder(li);
817 // First look for an external relocation entry for this literal pointer.
818 auto Reloc = std::find_if(
819 Relocs.begin(), Relocs.end(),
820 [&](const std::pair<uint64_t, SymbolRef> &P) { return P.first == i; });
821 if (Reloc != Relocs.end()) {
822 symbol_iterator RelocSym = Reloc->second;
823 ErrorOr<StringRef> SymName = RelocSym->getName();
824 if (std::error_code EC = SymName.getError())
825 report_fatal_error(EC.message());
826 outs() << "external relocation entry for symbol:" << *SymName << "\n";
830 // For local references see what the section the literal pointer points to.
831 auto Sect = std::find_if(LiteralSections.begin(), LiteralSections.end(),
832 [&](const SectionRef &R) {
833 return lp >= R.getAddress() &&
834 lp < R.getAddress() + R.getSize();
836 if (Sect == LiteralSections.end()) {
837 outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
841 uint64_t SectAddress = Sect->getAddress();
842 uint64_t SectSize = Sect->getSize();
845 Sect->getName(SectName);
846 DataRefImpl Ref = Sect->getRawDataRefImpl();
847 StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
848 outs() << SegmentName << ":" << SectName << ":";
850 uint32_t section_type;
852 const MachO::section_64 Sec = O->getSection64(Ref);
853 section_type = Sec.flags & MachO::SECTION_TYPE;
855 const MachO::section Sec = O->getSection(Ref);
856 section_type = Sec.flags & MachO::SECTION_TYPE;
860 Sect->getContents(BytesStr);
861 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
863 switch (section_type) {
864 case MachO::S_CSTRING_LITERALS:
865 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
867 DumpCstringChar(Contents[i]);
871 case MachO::S_4BYTE_LITERALS:
873 memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
875 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
876 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
877 sys::swapByteOrder(f);
878 sys::swapByteOrder(l);
882 case MachO::S_8BYTE_LITERALS: {
884 memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
886 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
887 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
889 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
890 sys::swapByteOrder(f);
891 sys::swapByteOrder(l0);
892 sys::swapByteOrder(l1);
894 DumpLiteral8(O, l0, l1, d);
897 case MachO::S_16BYTE_LITERALS: {
898 uint32_t l0, l1, l2, l3;
899 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
900 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
902 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
904 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
906 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
907 sys::swapByteOrder(l0);
908 sys::swapByteOrder(l1);
909 sys::swapByteOrder(l2);
910 sys::swapByteOrder(l3);
912 DumpLiteral16(l0, l1, l2, l3);
919 static void DumpInitTermPointerSection(MachOObjectFile *O, const char *sect,
920 uint32_t sect_size, uint64_t sect_addr,
921 SymbolAddressMap *AddrMap,
925 stride = sizeof(uint64_t);
927 stride = sizeof(uint32_t);
928 for (uint32_t i = 0; i < sect_size; i += stride) {
929 const char *SymbolName = nullptr;
931 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
932 uint64_t pointer_value;
933 memcpy(&pointer_value, sect + i, stride);
934 if (O->isLittleEndian() != sys::IsLittleEndianHost)
935 sys::swapByteOrder(pointer_value);
936 outs() << format("0x%016" PRIx64, pointer_value);
938 SymbolName = GuessSymbolName(pointer_value, AddrMap);
940 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
941 uint32_t pointer_value;
942 memcpy(&pointer_value, sect + i, stride);
943 if (O->isLittleEndian() != sys::IsLittleEndianHost)
944 sys::swapByteOrder(pointer_value);
945 outs() << format("0x%08" PRIx32, pointer_value);
947 SymbolName = GuessSymbolName(pointer_value, AddrMap);
950 outs() << " " << SymbolName;
955 static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
956 uint32_t size, uint64_t addr) {
957 uint32_t cputype = O->getHeader().cputype;
958 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
960 for (uint32_t i = 0; i < size; i += j, addr += j) {
962 outs() << format("%016" PRIx64, addr) << "\t";
964 outs() << format("%08" PRIx64, addr) << "\t";
965 for (j = 0; j < 16 && i + j < size; j++) {
966 uint8_t byte_word = *(sect + i + j);
967 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
973 for (uint32_t i = 0; i < size; i += j, addr += j) {
975 outs() << format("%016" PRIx64, addr) << "\t";
977 outs() << format("%08" PRIx64, sect) << "\t";
978 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
979 j += sizeof(int32_t)) {
980 if (i + j + sizeof(int32_t) < size) {
982 memcpy(&long_word, sect + i + j, sizeof(int32_t));
983 if (O->isLittleEndian() != sys::IsLittleEndianHost)
984 sys::swapByteOrder(long_word);
985 outs() << format("%08" PRIx32, long_word) << " ";
987 for (uint32_t k = 0; i + j + k < size; k++) {
988 uint8_t byte_word = *(sect + i + j);
989 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
998 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
999 StringRef DisSegName, StringRef DisSectName);
1000 static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
1001 uint32_t size, uint32_t addr);
1003 static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
1005 SymbolAddressMap AddrMap;
1007 CreateSymbolAddressMap(O, &AddrMap);
1009 for (unsigned i = 0; i < DumpSections.size(); ++i) {
1010 StringRef DumpSection = DumpSections[i];
1011 std::pair<StringRef, StringRef> DumpSegSectName;
1012 DumpSegSectName = DumpSection.split(',');
1013 StringRef DumpSegName, DumpSectName;
1014 if (DumpSegSectName.second.size()) {
1015 DumpSegName = DumpSegSectName.first;
1016 DumpSectName = DumpSegSectName.second;
1019 DumpSectName = DumpSegSectName.first;
1021 for (const SectionRef &Section : O->sections()) {
1023 Section.getName(SectName);
1024 DataRefImpl Ref = Section.getRawDataRefImpl();
1025 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1026 if ((DumpSegName.empty() || SegName == DumpSegName) &&
1027 (SectName == DumpSectName)) {
1029 uint32_t section_flags;
1031 const MachO::section_64 Sec = O->getSection64(Ref);
1032 section_flags = Sec.flags;
1035 const MachO::section Sec = O->getSection(Ref);
1036 section_flags = Sec.flags;
1038 uint32_t section_type = section_flags & MachO::SECTION_TYPE;
1041 Section.getContents(BytesStr);
1042 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1043 uint32_t sect_size = BytesStr.size();
1044 uint64_t sect_addr = Section.getAddress();
1046 outs() << "Contents of (" << SegName << "," << SectName
1050 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1051 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1052 DisassembleMachO(Filename, O, SegName, SectName);
1055 if (SegName == "__TEXT" && SectName == "__info_plist") {
1059 if (SegName == "__OBJC" && SectName == "__protocol") {
1060 DumpProtocolSection(O, sect, sect_size, sect_addr);
1063 switch (section_type) {
1064 case MachO::S_REGULAR:
1065 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1067 case MachO::S_ZEROFILL:
1068 outs() << "zerofill section and has no contents in the file\n";
1070 case MachO::S_CSTRING_LITERALS:
1071 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1073 case MachO::S_4BYTE_LITERALS:
1074 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1076 case MachO::S_8BYTE_LITERALS:
1077 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1079 case MachO::S_16BYTE_LITERALS:
1080 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1082 case MachO::S_LITERAL_POINTERS:
1083 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
1086 case MachO::S_MOD_INIT_FUNC_POINTERS:
1087 case MachO::S_MOD_TERM_FUNC_POINTERS:
1088 DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap,
1092 outs() << "Unknown section type ("
1093 << format("0x%08" PRIx32, section_type) << ")\n";
1094 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1098 if (section_type == MachO::S_ZEROFILL)
1099 outs() << "zerofill section and has no contents in the file\n";
1101 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1108 static void DumpInfoPlistSectionContents(StringRef Filename,
1109 MachOObjectFile *O) {
1110 for (const SectionRef &Section : O->sections()) {
1112 Section.getName(SectName);
1113 DataRefImpl Ref = Section.getRawDataRefImpl();
1114 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1115 if (SegName == "__TEXT" && SectName == "__info_plist") {
1116 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
1118 Section.getContents(BytesStr);
1119 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1126 // checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1127 // and if it is and there is a list of architecture flags is specified then
1128 // check to make sure this Mach-O file is one of those architectures or all
1129 // architectures were specified. If not then an error is generated and this
1130 // routine returns false. Else it returns true.
1131 static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
1132 if (isa<MachOObjectFile>(O) && !ArchAll && ArchFlags.size() != 0) {
1133 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O);
1134 bool ArchFound = false;
1135 MachO::mach_header H;
1136 MachO::mach_header_64 H_64;
1138 if (MachO->is64Bit()) {
1139 H_64 = MachO->MachOObjectFile::getHeader64();
1140 T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype);
1142 H = MachO->MachOObjectFile::getHeader();
1143 T = MachOObjectFile::getArch(H.cputype, H.cpusubtype);
1146 for (i = 0; i < ArchFlags.size(); ++i) {
1147 if (ArchFlags[i] == T.getArchName())
1152 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1153 << "architecture: " + ArchFlags[i] + "\n";
1160 static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1162 // ProcessMachO() is passed a single opened Mach-O file, which may be an
1163 // archive member and or in a slice of a universal file. It prints the
1164 // the file name and header info and then processes it according to the
1165 // command line options.
1166 static void ProcessMachO(StringRef Filename, MachOObjectFile *MachOOF,
1167 StringRef ArchiveMemberName = StringRef(),
1168 StringRef ArchitectureName = StringRef()) {
1169 // If we are doing some processing here on the Mach-O file print the header
1170 // info. And don't print it otherwise like in the case of printing the
1171 // UniversalHeaders or ArchiveHeaders.
1172 if (Disassemble || PrivateHeaders || ExportsTrie || Rebase || Bind ||
1173 LazyBind || WeakBind || IndirectSymbols || DataInCode || LinkOptHints ||
1174 DylibsUsed || DylibId || ObjcMetaData || (DumpSections.size() != 0)) {
1176 if (!ArchiveMemberName.empty())
1177 outs() << '(' << ArchiveMemberName << ')';
1178 if (!ArchitectureName.empty())
1179 outs() << " (architecture " << ArchitectureName << ")";
1184 DisassembleMachO(Filename, MachOOF, "__TEXT", "__text");
1185 if (IndirectSymbols)
1186 PrintIndirectSymbols(MachOOF, !NonVerbose);
1188 PrintDataInCodeTable(MachOOF, !NonVerbose);
1190 PrintLinkOptHints(MachOOF);
1192 PrintRelocations(MachOOF);
1194 PrintSectionHeaders(MachOOF);
1195 if (SectionContents)
1196 PrintSectionContents(MachOOF);
1197 if (DumpSections.size() != 0)
1198 DumpSectionContents(Filename, MachOOF, !NonVerbose);
1200 DumpInfoPlistSectionContents(Filename, MachOOF);
1202 PrintDylibs(MachOOF, false);
1204 PrintDylibs(MachOOF, true);
1206 PrintSymbolTable(MachOOF);
1208 printMachOUnwindInfo(MachOOF);
1210 printMachOFileHeader(MachOOF);
1212 printObjcMetaData(MachOOF, !NonVerbose);
1214 printExportsTrie(MachOOF);
1216 printRebaseTable(MachOOF);
1218 printBindTable(MachOOF);
1220 printLazyBindTable(MachOOF);
1222 printWeakBindTable(MachOOF);
1225 // printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1226 static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
1227 outs() << " cputype (" << cputype << ")\n";
1228 outs() << " cpusubtype (" << cpusubtype << ")\n";
1231 // printCPUType() helps print_fat_headers by printing the cputype and
1232 // pusubtype (symbolically for the one's it knows about).
1233 static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
1235 case MachO::CPU_TYPE_I386:
1236 switch (cpusubtype) {
1237 case MachO::CPU_SUBTYPE_I386_ALL:
1238 outs() << " cputype CPU_TYPE_I386\n";
1239 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
1242 printUnknownCPUType(cputype, cpusubtype);
1246 case MachO::CPU_TYPE_X86_64:
1247 switch (cpusubtype) {
1248 case MachO::CPU_SUBTYPE_X86_64_ALL:
1249 outs() << " cputype CPU_TYPE_X86_64\n";
1250 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1252 case MachO::CPU_SUBTYPE_X86_64_H:
1253 outs() << " cputype CPU_TYPE_X86_64\n";
1254 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
1257 printUnknownCPUType(cputype, cpusubtype);
1261 case MachO::CPU_TYPE_ARM:
1262 switch (cpusubtype) {
1263 case MachO::CPU_SUBTYPE_ARM_ALL:
1264 outs() << " cputype CPU_TYPE_ARM\n";
1265 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
1267 case MachO::CPU_SUBTYPE_ARM_V4T:
1268 outs() << " cputype CPU_TYPE_ARM\n";
1269 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
1271 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1272 outs() << " cputype CPU_TYPE_ARM\n";
1273 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
1275 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1276 outs() << " cputype CPU_TYPE_ARM\n";
1277 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
1279 case MachO::CPU_SUBTYPE_ARM_V6:
1280 outs() << " cputype CPU_TYPE_ARM\n";
1281 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
1283 case MachO::CPU_SUBTYPE_ARM_V6M:
1284 outs() << " cputype CPU_TYPE_ARM\n";
1285 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
1287 case MachO::CPU_SUBTYPE_ARM_V7:
1288 outs() << " cputype CPU_TYPE_ARM\n";
1289 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
1291 case MachO::CPU_SUBTYPE_ARM_V7EM:
1292 outs() << " cputype CPU_TYPE_ARM\n";
1293 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
1295 case MachO::CPU_SUBTYPE_ARM_V7K:
1296 outs() << " cputype CPU_TYPE_ARM\n";
1297 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
1299 case MachO::CPU_SUBTYPE_ARM_V7M:
1300 outs() << " cputype CPU_TYPE_ARM\n";
1301 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
1303 case MachO::CPU_SUBTYPE_ARM_V7S:
1304 outs() << " cputype CPU_TYPE_ARM\n";
1305 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
1308 printUnknownCPUType(cputype, cpusubtype);
1312 case MachO::CPU_TYPE_ARM64:
1313 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1314 case MachO::CPU_SUBTYPE_ARM64_ALL:
1315 outs() << " cputype CPU_TYPE_ARM64\n";
1316 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
1319 printUnknownCPUType(cputype, cpusubtype);
1324 printUnknownCPUType(cputype, cpusubtype);
1329 static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
1331 outs() << "Fat headers\n";
1333 outs() << "fat_magic FAT_MAGIC\n";
1335 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
1337 uint32_t nfat_arch = UB->getNumberOfObjects();
1338 StringRef Buf = UB->getData();
1339 uint64_t size = Buf.size();
1340 uint64_t big_size = sizeof(struct MachO::fat_header) +
1341 nfat_arch * sizeof(struct MachO::fat_arch);
1342 outs() << "nfat_arch " << UB->getNumberOfObjects();
1344 outs() << " (malformed, contains zero architecture types)\n";
1345 else if (big_size > size)
1346 outs() << " (malformed, architectures past end of file)\n";
1350 for (uint32_t i = 0; i < nfat_arch; ++i) {
1351 MachOUniversalBinary::ObjectForArch OFA(UB, i);
1352 uint32_t cputype = OFA.getCPUType();
1353 uint32_t cpusubtype = OFA.getCPUSubType();
1354 outs() << "architecture ";
1355 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
1356 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
1357 uint32_t other_cputype = other_OFA.getCPUType();
1358 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
1359 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
1360 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
1361 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
1362 outs() << "(illegal duplicate architecture) ";
1367 outs() << OFA.getArchTypeName() << "\n";
1368 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1370 outs() << i << "\n";
1371 outs() << " cputype " << cputype << "\n";
1372 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
1376 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
1377 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
1379 outs() << " capabilities "
1380 << format("0x%" PRIx32,
1381 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
1382 outs() << " offset " << OFA.getOffset();
1383 if (OFA.getOffset() > size)
1384 outs() << " (past end of file)";
1385 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
1386 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
1388 outs() << " size " << OFA.getSize();
1389 big_size = OFA.getOffset() + OFA.getSize();
1390 if (big_size > size)
1391 outs() << " (past end of file)";
1393 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
1398 static void printArchiveChild(Archive::Child &C, bool verbose,
1399 bool print_offset) {
1401 outs() << C.getChildOffset() << "\t";
1402 sys::fs::perms Mode = C.getAccessMode();
1404 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
1405 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
1407 if (Mode & sys::fs::owner_read)
1411 if (Mode & sys::fs::owner_write)
1415 if (Mode & sys::fs::owner_exe)
1419 if (Mode & sys::fs::group_read)
1423 if (Mode & sys::fs::group_write)
1427 if (Mode & sys::fs::group_exe)
1431 if (Mode & sys::fs::others_read)
1435 if (Mode & sys::fs::others_write)
1439 if (Mode & sys::fs::others_exe)
1444 outs() << format("0%o ", Mode);
1447 unsigned UID = C.getUID();
1448 outs() << format("%3d/", UID);
1449 unsigned GID = C.getGID();
1450 outs() << format("%-3d ", GID);
1451 uint64_t Size = C.getRawSize();
1452 outs() << format("%5" PRId64, Size) << " ";
1454 StringRef RawLastModified = C.getRawLastModified();
1457 if (RawLastModified.getAsInteger(10, Seconds))
1458 outs() << "(date: \"%s\" contains non-decimal chars) " << RawLastModified;
1460 // Since cime(3) returns a 26 character string of the form:
1461 // "Sun Sep 16 01:03:52 1973\n\0"
1462 // just print 24 characters.
1464 outs() << format("%.24s ", ctime(&t));
1467 outs() << RawLastModified << " ";
1471 ErrorOr<StringRef> NameOrErr = C.getName();
1472 if (NameOrErr.getError()) {
1473 StringRef RawName = C.getRawName();
1474 outs() << RawName << "\n";
1476 StringRef Name = NameOrErr.get();
1477 outs() << Name << "\n";
1480 StringRef RawName = C.getRawName();
1481 outs() << RawName << "\n";
1485 static void printArchiveHeaders(Archive *A, bool verbose, bool print_offset) {
1486 if (A->hasSymbolTable()) {
1487 Archive::child_iterator S = A->getSymbolTableChild();
1488 Archive::Child C = *S;
1489 printArchiveChild(C, verbose, print_offset);
1491 for (Archive::child_iterator I = A->child_begin(), E = A->child_end(); I != E;
1493 Archive::Child C = *I;
1494 printArchiveChild(C, verbose, print_offset);
1498 // ParseInputMachO() parses the named Mach-O file in Filename and handles the
1499 // -arch flags selecting just those slices as specified by them and also parses
1500 // archive files. Then for each individual Mach-O file ProcessMachO() is
1501 // called to process the file based on the command line options.
1502 void llvm::ParseInputMachO(StringRef Filename) {
1503 // Check for -arch all and verifiy the -arch flags are valid.
1504 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1505 if (ArchFlags[i] == "all") {
1508 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
1509 errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
1510 "'for the -arch option\n";
1516 // Attempt to open the binary.
1517 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
1518 if (std::error_code EC = BinaryOrErr.getError()) {
1519 errs() << "llvm-objdump: '" << Filename << "': " << EC.message() << ".\n";
1522 Binary &Bin = *BinaryOrErr.get().getBinary();
1524 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1525 outs() << "Archive : " << Filename << "\n";
1527 printArchiveHeaders(A, !NonVerbose, ArchiveMemberOffsets);
1528 for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
1530 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = I->getAsBinary();
1531 if (ChildOrErr.getError())
1533 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1534 if (!checkMachOAndArchFlags(O, Filename))
1536 ProcessMachO(Filename, O, O->getFileName());
1541 if (UniversalHeaders) {
1542 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
1543 printMachOUniversalHeaders(UB, !NonVerbose);
1545 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1546 // If we have a list of architecture flags specified dump only those.
1547 if (!ArchAll && ArchFlags.size() != 0) {
1548 // Look for a slice in the universal binary that matches each ArchFlag.
1550 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1552 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1553 E = UB->end_objects();
1555 if (ArchFlags[i] == I->getArchTypeName()) {
1557 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
1558 I->getAsObjectFile();
1559 std::string ArchitectureName = "";
1560 if (ArchFlags.size() > 1)
1561 ArchitectureName = I->getArchTypeName();
1563 ObjectFile &O = *ObjOrErr.get();
1564 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
1565 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
1566 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1567 I->getAsArchive()) {
1568 std::unique_ptr<Archive> &A = *AOrErr;
1569 outs() << "Archive : " << Filename;
1570 if (!ArchitectureName.empty())
1571 outs() << " (architecture " << ArchitectureName << ")";
1574 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
1575 for (Archive::child_iterator AI = A->child_begin(),
1576 AE = A->child_end();
1578 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1579 if (ChildOrErr.getError())
1581 if (MachOObjectFile *O =
1582 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
1583 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
1589 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1590 << "architecture: " + ArchFlags[i] + "\n";
1596 // No architecture flags were specified so if this contains a slice that
1597 // matches the host architecture dump only that.
1599 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1600 E = UB->end_objects();
1602 if (MachOObjectFile::getHostArch().getArchName() ==
1603 I->getArchTypeName()) {
1604 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1605 std::string ArchiveName;
1606 ArchiveName.clear();
1608 ObjectFile &O = *ObjOrErr.get();
1609 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
1610 ProcessMachO(Filename, MachOOF);
1611 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1612 I->getAsArchive()) {
1613 std::unique_ptr<Archive> &A = *AOrErr;
1614 outs() << "Archive : " << Filename << "\n";
1616 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
1617 for (Archive::child_iterator AI = A->child_begin(),
1618 AE = A->child_end();
1620 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1621 if (ChildOrErr.getError())
1623 if (MachOObjectFile *O =
1624 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
1625 ProcessMachO(Filename, O, O->getFileName());
1632 // Either all architectures have been specified or none have been specified
1633 // and this does not contain the host architecture so dump all the slices.
1634 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1635 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1636 E = UB->end_objects();
1638 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1639 std::string ArchitectureName = "";
1640 if (moreThanOneArch)
1641 ArchitectureName = I->getArchTypeName();
1643 ObjectFile &Obj = *ObjOrErr.get();
1644 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
1645 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
1646 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
1647 std::unique_ptr<Archive> &A = *AOrErr;
1648 outs() << "Archive : " << Filename;
1649 if (!ArchitectureName.empty())
1650 outs() << " (architecture " << ArchitectureName << ")";
1653 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
1654 for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
1656 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1657 if (ChildOrErr.getError())
1659 if (MachOObjectFile *O =
1660 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1661 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
1662 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
1670 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
1671 if (!checkMachOAndArchFlags(O, Filename))
1673 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
1674 ProcessMachO(Filename, MachOOF);
1676 errs() << "llvm-objdump: '" << Filename << "': "
1677 << "Object is not a Mach-O file type.\n";
1679 errs() << "llvm-objdump: '" << Filename << "': "
1680 << "Unrecognized file type.\n";
1683 typedef std::pair<uint64_t, const char *> BindInfoEntry;
1684 typedef std::vector<BindInfoEntry> BindTable;
1685 typedef BindTable::iterator bind_table_iterator;
1687 // The block of info used by the Symbolizer call backs.
1688 struct DisassembleInfo {
1692 SymbolAddressMap *AddrMap;
1693 std::vector<SectionRef> *Sections;
1694 const char *class_name;
1695 const char *selector_name;
1697 char *demangled_name;
1700 BindTable *bindtable;
1703 // SymbolizerGetOpInfo() is the operand information call back function.
1704 // This is called to get the symbolic information for operand(s) of an
1705 // instruction when it is being done. This routine does this from
1706 // the relocation information, symbol table, etc. That block of information
1707 // is a pointer to the struct DisassembleInfo that was passed when the
1708 // disassembler context was created and passed to back to here when
1709 // called back by the disassembler for instruction operands that could have
1710 // relocation information. The address of the instruction containing operand is
1711 // at the Pc parameter. The immediate value the operand has is passed in
1712 // op_info->Value and is at Offset past the start of the instruction and has a
1713 // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
1714 // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
1715 // names and addends of the symbolic expression to add for the operand. The
1716 // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
1717 // information is returned then this function returns 1 else it returns 0.
1718 static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
1719 uint64_t Size, int TagType, void *TagBuf) {
1720 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
1721 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
1722 uint64_t value = op_info->Value;
1724 // Make sure all fields returned are zero if we don't set them.
1725 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
1726 op_info->Value = value;
1728 // If the TagType is not the value 1 which it code knows about or if no
1729 // verbose symbolic information is wanted then just return 0, indicating no
1730 // information is being returned.
1731 if (TagType != 1 || !info->verbose)
1734 unsigned int Arch = info->O->getArch();
1735 if (Arch == Triple::x86) {
1736 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1738 // First search the section's relocation entries (if any) for an entry
1739 // for this section offset.
1740 uint32_t sect_addr = info->S.getAddress();
1741 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1742 bool reloc_found = false;
1744 MachO::any_relocation_info RE;
1745 bool isExtern = false;
1747 bool r_scattered = false;
1748 uint32_t r_value, pair_r_value, r_type;
1749 for (const RelocationRef &Reloc : info->S.relocations()) {
1750 uint64_t RelocOffset = Reloc.getOffset();
1751 if (RelocOffset == sect_offset) {
1752 Rel = Reloc.getRawDataRefImpl();
1753 RE = info->O->getRelocation(Rel);
1754 r_type = info->O->getAnyRelocationType(RE);
1755 r_scattered = info->O->isRelocationScattered(RE);
1757 r_value = info->O->getScatteredRelocationValue(RE);
1758 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1759 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
1760 DataRefImpl RelNext = Rel;
1761 info->O->moveRelocationNext(RelNext);
1762 MachO::any_relocation_info RENext;
1763 RENext = info->O->getRelocation(RelNext);
1764 if (info->O->isRelocationScattered(RENext))
1765 pair_r_value = info->O->getScatteredRelocationValue(RENext);
1770 isExtern = info->O->getPlainRelocationExternal(RE);
1772 symbol_iterator RelocSym = Reloc.getSymbol();
1780 if (reloc_found && isExtern) {
1781 ErrorOr<StringRef> SymName = Symbol.getName();
1782 if (std::error_code EC = SymName.getError())
1783 report_fatal_error(EC.message());
1784 const char *name = SymName->data();
1785 op_info->AddSymbol.Present = 1;
1786 op_info->AddSymbol.Name = name;
1787 // For i386 extern relocation entries the value in the instruction is
1788 // the offset from the symbol, and value is already set in op_info->Value.
1791 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1792 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
1793 const char *add = GuessSymbolName(r_value, info->AddrMap);
1794 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
1795 uint32_t offset = value - (r_value - pair_r_value);
1796 op_info->AddSymbol.Present = 1;
1798 op_info->AddSymbol.Name = add;
1800 op_info->AddSymbol.Value = r_value;
1801 op_info->SubtractSymbol.Present = 1;
1803 op_info->SubtractSymbol.Name = sub;
1805 op_info->SubtractSymbol.Value = pair_r_value;
1806 op_info->Value = offset;
1810 // Second search the external relocation entries of a fully linked image
1811 // (if any) for an entry that matches this segment offset.
1812 // uint32_t seg_offset = (Pc + Offset);
1815 if (Arch == Triple::x86_64) {
1816 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1818 // First search the section's relocation entries (if any) for an entry
1819 // for this section offset.
1820 uint64_t sect_addr = info->S.getAddress();
1821 uint64_t sect_offset = (Pc + Offset) - sect_addr;
1822 bool reloc_found = false;
1824 MachO::any_relocation_info RE;
1825 bool isExtern = false;
1827 for (const RelocationRef &Reloc : info->S.relocations()) {
1828 uint64_t RelocOffset = Reloc.getOffset();
1829 if (RelocOffset == sect_offset) {
1830 Rel = Reloc.getRawDataRefImpl();
1831 RE = info->O->getRelocation(Rel);
1832 // NOTE: Scattered relocations don't exist on x86_64.
1833 isExtern = info->O->getPlainRelocationExternal(RE);
1835 symbol_iterator RelocSym = Reloc.getSymbol();
1842 if (reloc_found && isExtern) {
1843 // The Value passed in will be adjusted by the Pc if the instruction
1844 // adds the Pc. But for x86_64 external relocation entries the Value
1845 // is the offset from the external symbol.
1846 if (info->O->getAnyRelocationPCRel(RE))
1847 op_info->Value -= Pc + Offset + Size;
1848 ErrorOr<StringRef> SymName = Symbol.getName();
1849 if (std::error_code EC = SymName.getError())
1850 report_fatal_error(EC.message());
1851 const char *name = SymName->data();
1852 unsigned Type = info->O->getAnyRelocationType(RE);
1853 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
1854 DataRefImpl RelNext = Rel;
1855 info->O->moveRelocationNext(RelNext);
1856 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
1857 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
1858 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
1859 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
1860 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
1861 op_info->SubtractSymbol.Present = 1;
1862 op_info->SubtractSymbol.Name = name;
1863 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
1864 Symbol = *RelocSymNext;
1865 ErrorOr<StringRef> SymNameNext = Symbol.getName();
1866 if (std::error_code EC = SymNameNext.getError())
1867 report_fatal_error(EC.message());
1868 name = SymNameNext->data();
1871 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
1872 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
1873 op_info->AddSymbol.Present = 1;
1874 op_info->AddSymbol.Name = name;
1878 // Second search the external relocation entries of a fully linked image
1879 // (if any) for an entry that matches this segment offset.
1880 // uint64_t seg_offset = (Pc + Offset);
1883 if (Arch == Triple::arm) {
1884 if (Offset != 0 || (Size != 4 && Size != 2))
1886 // First search the section's relocation entries (if any) for an entry
1887 // for this section offset.
1888 uint32_t sect_addr = info->S.getAddress();
1889 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1891 MachO::any_relocation_info RE;
1892 bool isExtern = false;
1894 bool r_scattered = false;
1895 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
1897 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
1898 [&](const RelocationRef &Reloc) {
1899 uint64_t RelocOffset = Reloc.getOffset();
1900 return RelocOffset == sect_offset;
1903 if (Reloc == info->S.relocations().end())
1906 Rel = Reloc->getRawDataRefImpl();
1907 RE = info->O->getRelocation(Rel);
1908 r_length = info->O->getAnyRelocationLength(RE);
1909 r_scattered = info->O->isRelocationScattered(RE);
1911 r_value = info->O->getScatteredRelocationValue(RE);
1912 r_type = info->O->getScatteredRelocationType(RE);
1914 r_type = info->O->getAnyRelocationType(RE);
1915 isExtern = info->O->getPlainRelocationExternal(RE);
1917 symbol_iterator RelocSym = Reloc->getSymbol();
1921 if (r_type == MachO::ARM_RELOC_HALF ||
1922 r_type == MachO::ARM_RELOC_SECTDIFF ||
1923 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
1924 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1925 DataRefImpl RelNext = Rel;
1926 info->O->moveRelocationNext(RelNext);
1927 MachO::any_relocation_info RENext;
1928 RENext = info->O->getRelocation(RelNext);
1929 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
1930 if (info->O->isRelocationScattered(RENext))
1931 pair_r_value = info->O->getScatteredRelocationValue(RENext);
1935 ErrorOr<StringRef> SymName = Symbol.getName();
1936 if (std::error_code EC = SymName.getError())
1937 report_fatal_error(EC.message());
1938 const char *name = SymName->data();
1939 op_info->AddSymbol.Present = 1;
1940 op_info->AddSymbol.Name = name;
1942 case MachO::ARM_RELOC_HALF:
1943 if ((r_length & 0x1) == 1) {
1944 op_info->Value = value << 16 | other_half;
1945 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1947 op_info->Value = other_half << 16 | value;
1948 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
1956 // If we have a branch that is not an external relocation entry then
1957 // return 0 so the code in tryAddingSymbolicOperand() can use the
1958 // SymbolLookUp call back with the branch target address to look up the
1959 // symbol and possiblity add an annotation for a symbol stub.
1960 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
1961 r_type == MachO::ARM_THUMB_RELOC_BR22))
1964 uint32_t offset = 0;
1965 if (r_type == MachO::ARM_RELOC_HALF ||
1966 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1967 if ((r_length & 0x1) == 1)
1968 value = value << 16 | other_half;
1970 value = other_half << 16 | value;
1972 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
1973 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
1974 offset = value - r_value;
1978 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1979 if ((r_length & 0x1) == 1)
1980 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1982 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
1983 const char *add = GuessSymbolName(r_value, info->AddrMap);
1984 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
1985 int32_t offset = value - (r_value - pair_r_value);
1986 op_info->AddSymbol.Present = 1;
1988 op_info->AddSymbol.Name = add;
1990 op_info->AddSymbol.Value = r_value;
1991 op_info->SubtractSymbol.Present = 1;
1993 op_info->SubtractSymbol.Name = sub;
1995 op_info->SubtractSymbol.Value = pair_r_value;
1996 op_info->Value = offset;
2000 op_info->AddSymbol.Present = 1;
2001 op_info->Value = offset;
2002 if (r_type == MachO::ARM_RELOC_HALF) {
2003 if ((r_length & 0x1) == 1)
2004 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2006 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2008 const char *add = GuessSymbolName(value, info->AddrMap);
2009 if (add != nullptr) {
2010 op_info->AddSymbol.Name = add;
2013 op_info->AddSymbol.Value = value;
2016 if (Arch == Triple::aarch64) {
2017 if (Offset != 0 || Size != 4)
2019 // First search the section's relocation entries (if any) for an entry
2020 // for this section offset.
2021 uint64_t sect_addr = info->S.getAddress();
2022 uint64_t sect_offset = (Pc + Offset) - sect_addr;
2024 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
2025 [&](const RelocationRef &Reloc) {
2026 uint64_t RelocOffset = Reloc.getOffset();
2027 return RelocOffset == sect_offset;
2030 if (Reloc == info->S.relocations().end())
2033 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2034 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2035 uint32_t r_type = info->O->getAnyRelocationType(RE);
2036 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2037 DataRefImpl RelNext = Rel;
2038 info->O->moveRelocationNext(RelNext);
2039 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2041 value = info->O->getPlainRelocationSymbolNum(RENext);
2042 op_info->Value = value;
2045 // NOTE: Scattered relocations don't exist on arm64.
2046 if (!info->O->getPlainRelocationExternal(RE))
2048 ErrorOr<StringRef> SymName = Reloc->getSymbol()->getName();
2049 if (std::error_code EC = SymName.getError())
2050 report_fatal_error(EC.message());
2051 const char *name = SymName->data();
2052 op_info->AddSymbol.Present = 1;
2053 op_info->AddSymbol.Name = name;
2056 case MachO::ARM64_RELOC_PAGE21:
2058 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2060 case MachO::ARM64_RELOC_PAGEOFF12:
2062 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2064 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2066 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2068 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2070 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2072 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2073 /* @tvlppage is not implemented in llvm-mc */
2074 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2076 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2077 /* @tvlppageoff is not implemented in llvm-mc */
2078 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2081 case MachO::ARM64_RELOC_BRANCH26:
2082 op_info->VariantKind = LLVMDisassembler_VariantKind_None;
2090 // GuessCstringPointer is passed the address of what might be a pointer to a
2091 // literal string in a cstring section. If that address is in a cstring section
2092 // it returns a pointer to that string. Else it returns nullptr.
2093 static const char *GuessCstringPointer(uint64_t ReferenceValue,
2094 struct DisassembleInfo *info) {
2095 for (const auto &Load : info->O->load_commands()) {
2096 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2097 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2098 for (unsigned J = 0; J < Seg.nsects; ++J) {
2099 MachO::section_64 Sec = info->O->getSection64(Load, J);
2100 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2101 if (section_type == MachO::S_CSTRING_LITERALS &&
2102 ReferenceValue >= Sec.addr &&
2103 ReferenceValue < Sec.addr + Sec.size) {
2104 uint64_t sect_offset = ReferenceValue - Sec.addr;
2105 uint64_t object_offset = Sec.offset + sect_offset;
2106 StringRef MachOContents = info->O->getData();
2107 uint64_t object_size = MachOContents.size();
2108 const char *object_addr = (const char *)MachOContents.data();
2109 if (object_offset < object_size) {
2110 const char *name = object_addr + object_offset;
2117 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2118 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2119 for (unsigned J = 0; J < Seg.nsects; ++J) {
2120 MachO::section Sec = info->O->getSection(Load, J);
2121 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2122 if (section_type == MachO::S_CSTRING_LITERALS &&
2123 ReferenceValue >= Sec.addr &&
2124 ReferenceValue < Sec.addr + Sec.size) {
2125 uint64_t sect_offset = ReferenceValue - Sec.addr;
2126 uint64_t object_offset = Sec.offset + sect_offset;
2127 StringRef MachOContents = info->O->getData();
2128 uint64_t object_size = MachOContents.size();
2129 const char *object_addr = (const char *)MachOContents.data();
2130 if (object_offset < object_size) {
2131 const char *name = object_addr + object_offset;
2143 // GuessIndirectSymbol returns the name of the indirect symbol for the
2144 // ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
2145 // an address of a symbol stub or a lazy or non-lazy pointer to associate the
2146 // symbol name being referenced by the stub or pointer.
2147 static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2148 struct DisassembleInfo *info) {
2149 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2150 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
2151 for (const auto &Load : info->O->load_commands()) {
2152 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2153 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2154 for (unsigned J = 0; J < Seg.nsects; ++J) {
2155 MachO::section_64 Sec = info->O->getSection64(Load, J);
2156 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2157 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2158 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2159 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2160 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2161 section_type == MachO::S_SYMBOL_STUBS) &&
2162 ReferenceValue >= Sec.addr &&
2163 ReferenceValue < Sec.addr + Sec.size) {
2165 if (section_type == MachO::S_SYMBOL_STUBS)
2166 stride = Sec.reserved2;
2171 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2172 if (index < Dysymtab.nindirectsyms) {
2173 uint32_t indirect_symbol =
2174 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
2175 if (indirect_symbol < Symtab.nsyms) {
2176 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2177 SymbolRef Symbol = *Sym;
2178 ErrorOr<StringRef> SymName = Symbol.getName();
2179 if (std::error_code EC = SymName.getError())
2180 report_fatal_error(EC.message());
2181 const char *name = SymName->data();
2187 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2188 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2189 for (unsigned J = 0; J < Seg.nsects; ++J) {
2190 MachO::section Sec = info->O->getSection(Load, J);
2191 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2192 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2193 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2194 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2195 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2196 section_type == MachO::S_SYMBOL_STUBS) &&
2197 ReferenceValue >= Sec.addr &&
2198 ReferenceValue < Sec.addr + Sec.size) {
2200 if (section_type == MachO::S_SYMBOL_STUBS)
2201 stride = Sec.reserved2;
2206 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2207 if (index < Dysymtab.nindirectsyms) {
2208 uint32_t indirect_symbol =
2209 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
2210 if (indirect_symbol < Symtab.nsyms) {
2211 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2212 SymbolRef Symbol = *Sym;
2213 ErrorOr<StringRef> SymName = Symbol.getName();
2214 if (std::error_code EC = SymName.getError())
2215 report_fatal_error(EC.message());
2216 const char *name = SymName->data();
2227 // method_reference() is called passing it the ReferenceName that might be
2228 // a reference it to an Objective-C method call. If so then it allocates and
2229 // assembles a method call string with the values last seen and saved in
2230 // the DisassembleInfo's class_name and selector_name fields. This is saved
2231 // into the method field of the info and any previous string is free'ed.
2232 // Then the class_name field in the info is set to nullptr. The method call
2233 // string is set into ReferenceName and ReferenceType is set to
2234 // LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
2235 // then both ReferenceType and ReferenceName are left unchanged.
2236 static void method_reference(struct DisassembleInfo *info,
2237 uint64_t *ReferenceType,
2238 const char **ReferenceName) {
2239 unsigned int Arch = info->O->getArch();
2240 if (*ReferenceName != nullptr) {
2241 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
2242 if (info->selector_name != nullptr) {
2243 if (info->method != nullptr)
2245 if (info->class_name != nullptr) {
2246 info->method = (char *)malloc(5 + strlen(info->class_name) +
2247 strlen(info->selector_name));
2248 if (info->method != nullptr) {
2249 strcpy(info->method, "+[");
2250 strcat(info->method, info->class_name);
2251 strcat(info->method, " ");
2252 strcat(info->method, info->selector_name);
2253 strcat(info->method, "]");
2254 *ReferenceName = info->method;
2255 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2258 info->method = (char *)malloc(9 + strlen(info->selector_name));
2259 if (info->method != nullptr) {
2260 if (Arch == Triple::x86_64)
2261 strcpy(info->method, "-[%rdi ");
2262 else if (Arch == Triple::aarch64)
2263 strcpy(info->method, "-[x0 ");
2265 strcpy(info->method, "-[r? ");
2266 strcat(info->method, info->selector_name);
2267 strcat(info->method, "]");
2268 *ReferenceName = info->method;
2269 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2272 info->class_name = nullptr;
2274 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
2275 if (info->selector_name != nullptr) {
2276 if (info->method != nullptr)
2278 info->method = (char *)malloc(17 + strlen(info->selector_name));
2279 if (info->method != nullptr) {
2280 if (Arch == Triple::x86_64)
2281 strcpy(info->method, "-[[%rdi super] ");
2282 else if (Arch == Triple::aarch64)
2283 strcpy(info->method, "-[[x0 super] ");
2285 strcpy(info->method, "-[[r? super] ");
2286 strcat(info->method, info->selector_name);
2287 strcat(info->method, "]");
2288 *ReferenceName = info->method;
2289 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2291 info->class_name = nullptr;
2297 // GuessPointerPointer() is passed the address of what might be a pointer to
2298 // a reference to an Objective-C class, selector, message ref or cfstring.
2299 // If so the value of the pointer is returned and one of the booleans are set
2300 // to true. If not zero is returned and all the booleans are set to false.
2301 static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
2302 struct DisassembleInfo *info,
2303 bool &classref, bool &selref, bool &msgref,
2309 for (const auto &Load : info->O->load_commands()) {
2310 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2311 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2312 for (unsigned J = 0; J < Seg.nsects; ++J) {
2313 MachO::section_64 Sec = info->O->getSection64(Load, J);
2314 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
2315 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2316 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
2317 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
2318 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
2319 ReferenceValue >= Sec.addr &&
2320 ReferenceValue < Sec.addr + Sec.size) {
2321 uint64_t sect_offset = ReferenceValue - Sec.addr;
2322 uint64_t object_offset = Sec.offset + sect_offset;
2323 StringRef MachOContents = info->O->getData();
2324 uint64_t object_size = MachOContents.size();
2325 const char *object_addr = (const char *)MachOContents.data();
2326 if (object_offset < object_size) {
2327 uint64_t pointer_value;
2328 memcpy(&pointer_value, object_addr + object_offset,
2330 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2331 sys::swapByteOrder(pointer_value);
2332 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
2334 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2335 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
2337 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
2338 ReferenceValue + 8 < Sec.addr + Sec.size) {
2340 memcpy(&pointer_value, object_addr + object_offset + 8,
2342 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2343 sys::swapByteOrder(pointer_value);
2344 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
2346 return pointer_value;
2353 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
2358 // get_pointer_64 returns a pointer to the bytes in the object file at the
2359 // Address from a section in the Mach-O file. And indirectly returns the
2360 // offset into the section, number of bytes left in the section past the offset
2361 // and which section is was being referenced. If the Address is not in a
2362 // section nullptr is returned.
2363 static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
2364 uint32_t &left, SectionRef &S,
2365 DisassembleInfo *info,
2366 bool objc_only = false) {
2370 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
2371 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
2372 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
2375 ((*(info->Sections))[SectIdx]).getName(SectName);
2376 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
2377 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
2378 if (SegName != "__OBJC" && SectName != "__cstring")
2381 if (Address >= SectAddress && Address < SectAddress + SectSize) {
2382 S = (*(info->Sections))[SectIdx];
2383 offset = Address - SectAddress;
2384 left = SectSize - offset;
2385 StringRef SectContents;
2386 ((*(info->Sections))[SectIdx]).getContents(SectContents);
2387 return SectContents.data() + offset;
2393 static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
2394 uint32_t &left, SectionRef &S,
2395 DisassembleInfo *info,
2396 bool objc_only = false) {
2397 return get_pointer_64(Address, offset, left, S, info, objc_only);
2400 // get_symbol_64() returns the name of a symbol (or nullptr) and the address of
2401 // the symbol indirectly through n_value. Based on the relocation information
2402 // for the specified section offset in the specified section reference.
2403 // If no relocation information is found and a non-zero ReferenceValue for the
2404 // symbol is passed, look up that address in the info's AddrMap.
2405 static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
2406 DisassembleInfo *info, uint64_t &n_value,
2407 uint64_t ReferenceValue = 0) {
2412 // See if there is an external relocation entry at the sect_offset.
2413 bool reloc_found = false;
2415 MachO::any_relocation_info RE;
2416 bool isExtern = false;
2418 for (const RelocationRef &Reloc : S.relocations()) {
2419 uint64_t RelocOffset = Reloc.getOffset();
2420 if (RelocOffset == sect_offset) {
2421 Rel = Reloc.getRawDataRefImpl();
2422 RE = info->O->getRelocation(Rel);
2423 if (info->O->isRelocationScattered(RE))
2425 isExtern = info->O->getPlainRelocationExternal(RE);
2427 symbol_iterator RelocSym = Reloc.getSymbol();
2434 // If there is an external relocation entry for a symbol in this section
2435 // at this section_offset then use that symbol's value for the n_value
2436 // and return its name.
2437 const char *SymbolName = nullptr;
2438 if (reloc_found && isExtern) {
2439 n_value = Symbol.getValue();
2440 ErrorOr<StringRef> NameOrError = Symbol.getName();
2441 if (std::error_code EC = NameOrError.getError())
2442 report_fatal_error(EC.message());
2443 StringRef Name = *NameOrError;
2444 if (!Name.empty()) {
2445 SymbolName = Name.data();
2450 // TODO: For fully linked images, look through the external relocation
2451 // entries off the dynamic symtab command. For these the r_offset is from the
2452 // start of the first writeable segment in the Mach-O file. So the offset
2453 // to this section from that segment is passed to this routine by the caller,
2454 // as the database_offset. Which is the difference of the section's starting
2455 // address and the first writable segment.
2457 // NOTE: need add passing the database_offset to this routine.
2459 // We did not find an external relocation entry so look up the ReferenceValue
2460 // as an address of a symbol and if found return that symbol's name.
2461 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
2466 static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
2467 DisassembleInfo *info,
2468 uint32_t ReferenceValue) {
2470 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
2473 // These are structs in the Objective-C meta data and read to produce the
2474 // comments for disassembly. While these are part of the ABI they are no
2475 // public defintions. So the are here not in include/llvm/Support/MachO.h .
2477 // The cfstring object in a 64-bit Mach-O file.
2478 struct cfstring64_t {
2479 uint64_t isa; // class64_t * (64-bit pointer)
2480 uint64_t flags; // flag bits
2481 uint64_t characters; // char * (64-bit pointer)
2482 uint64_t length; // number of non-NULL characters in above
2485 // The class object in a 64-bit Mach-O file.
2487 uint64_t isa; // class64_t * (64-bit pointer)
2488 uint64_t superclass; // class64_t * (64-bit pointer)
2489 uint64_t cache; // Cache (64-bit pointer)
2490 uint64_t vtable; // IMP * (64-bit pointer)
2491 uint64_t data; // class_ro64_t * (64-bit pointer)
2495 uint32_t isa; /* class32_t * (32-bit pointer) */
2496 uint32_t superclass; /* class32_t * (32-bit pointer) */
2497 uint32_t cache; /* Cache (32-bit pointer) */
2498 uint32_t vtable; /* IMP * (32-bit pointer) */
2499 uint32_t data; /* class_ro32_t * (32-bit pointer) */
2502 struct class_ro64_t {
2504 uint32_t instanceStart;
2505 uint32_t instanceSize;
2507 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
2508 uint64_t name; // const char * (64-bit pointer)
2509 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
2510 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
2511 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
2512 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
2513 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
2516 struct class_ro32_t {
2518 uint32_t instanceStart;
2519 uint32_t instanceSize;
2520 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
2521 uint32_t name; /* const char * (32-bit pointer) */
2522 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
2523 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
2524 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
2525 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
2526 uint32_t baseProperties; /* const struct objc_property_list *
2530 /* Values for class_ro{64,32}_t->flags */
2531 #define RO_META (1 << 0)
2532 #define RO_ROOT (1 << 1)
2533 #define RO_HAS_CXX_STRUCTORS (1 << 2)
2535 struct method_list64_t {
2538 /* struct method64_t first; These structures follow inline */
2541 struct method_list32_t {
2544 /* struct method32_t first; These structures follow inline */
2548 uint64_t name; /* SEL (64-bit pointer) */
2549 uint64_t types; /* const char * (64-bit pointer) */
2550 uint64_t imp; /* IMP (64-bit pointer) */
2554 uint32_t name; /* SEL (32-bit pointer) */
2555 uint32_t types; /* const char * (32-bit pointer) */
2556 uint32_t imp; /* IMP (32-bit pointer) */
2559 struct protocol_list64_t {
2560 uint64_t count; /* uintptr_t (a 64-bit value) */
2561 /* struct protocol64_t * list[0]; These pointers follow inline */
2564 struct protocol_list32_t {
2565 uint32_t count; /* uintptr_t (a 32-bit value) */
2566 /* struct protocol32_t * list[0]; These pointers follow inline */
2569 struct protocol64_t {
2570 uint64_t isa; /* id * (64-bit pointer) */
2571 uint64_t name; /* const char * (64-bit pointer) */
2572 uint64_t protocols; /* struct protocol_list64_t *
2574 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
2575 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
2576 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
2577 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
2578 uint64_t instanceProperties; /* struct objc_property_list *
2582 struct protocol32_t {
2583 uint32_t isa; /* id * (32-bit pointer) */
2584 uint32_t name; /* const char * (32-bit pointer) */
2585 uint32_t protocols; /* struct protocol_list_t *
2587 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
2588 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
2589 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
2590 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
2591 uint32_t instanceProperties; /* struct objc_property_list *
2595 struct ivar_list64_t {
2598 /* struct ivar64_t first; These structures follow inline */
2601 struct ivar_list32_t {
2604 /* struct ivar32_t first; These structures follow inline */
2608 uint64_t offset; /* uintptr_t * (64-bit pointer) */
2609 uint64_t name; /* const char * (64-bit pointer) */
2610 uint64_t type; /* const char * (64-bit pointer) */
2616 uint32_t offset; /* uintptr_t * (32-bit pointer) */
2617 uint32_t name; /* const char * (32-bit pointer) */
2618 uint32_t type; /* const char * (32-bit pointer) */
2623 struct objc_property_list64 {
2626 /* struct objc_property64 first; These structures follow inline */
2629 struct objc_property_list32 {
2632 /* struct objc_property32 first; These structures follow inline */
2635 struct objc_property64 {
2636 uint64_t name; /* const char * (64-bit pointer) */
2637 uint64_t attributes; /* const char * (64-bit pointer) */
2640 struct objc_property32 {
2641 uint32_t name; /* const char * (32-bit pointer) */
2642 uint32_t attributes; /* const char * (32-bit pointer) */
2645 struct category64_t {
2646 uint64_t name; /* const char * (64-bit pointer) */
2647 uint64_t cls; /* struct class_t * (64-bit pointer) */
2648 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
2649 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
2650 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
2651 uint64_t instanceProperties; /* struct objc_property_list *
2655 struct category32_t {
2656 uint32_t name; /* const char * (32-bit pointer) */
2657 uint32_t cls; /* struct class_t * (32-bit pointer) */
2658 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
2659 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
2660 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
2661 uint32_t instanceProperties; /* struct objc_property_list *
2665 struct objc_image_info64 {
2669 struct objc_image_info32 {
2673 struct imageInfo_t {
2677 /* masks for objc_image_info.flags */
2678 #define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
2679 #define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
2681 struct message_ref64 {
2682 uint64_t imp; /* IMP (64-bit pointer) */
2683 uint64_t sel; /* SEL (64-bit pointer) */
2686 struct message_ref32 {
2687 uint32_t imp; /* IMP (32-bit pointer) */
2688 uint32_t sel; /* SEL (32-bit pointer) */
2691 // Objective-C 1 (32-bit only) meta data structs.
2693 struct objc_module_t {
2696 uint32_t name; /* char * (32-bit pointer) */
2697 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
2700 struct objc_symtab_t {
2701 uint32_t sel_ref_cnt;
2702 uint32_t refs; /* SEL * (32-bit pointer) */
2703 uint16_t cls_def_cnt;
2704 uint16_t cat_def_cnt;
2705 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
2708 struct objc_class_t {
2709 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2710 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
2711 uint32_t name; /* const char * (32-bit pointer) */
2714 int32_t instance_size;
2715 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
2716 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
2717 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
2718 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
2721 #define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
2722 // class is not a metaclass
2723 #define CLS_CLASS 0x1
2724 // class is a metaclass
2725 #define CLS_META 0x2
2727 struct objc_category_t {
2728 uint32_t category_name; /* char * (32-bit pointer) */
2729 uint32_t class_name; /* char * (32-bit pointer) */
2730 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
2731 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
2732 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
2735 struct objc_ivar_t {
2736 uint32_t ivar_name; /* char * (32-bit pointer) */
2737 uint32_t ivar_type; /* char * (32-bit pointer) */
2738 int32_t ivar_offset;
2741 struct objc_ivar_list_t {
2743 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
2746 struct objc_method_list_t {
2747 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
2748 int32_t method_count;
2749 // struct objc_method_t method_list[1]; /* variable length structure */
2752 struct objc_method_t {
2753 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2754 uint32_t method_types; /* char * (32-bit pointer) */
2755 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
2759 struct objc_protocol_list_t {
2760 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
2762 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
2763 // (32-bit pointer) */
2766 struct objc_protocol_t {
2767 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2768 uint32_t protocol_name; /* char * (32-bit pointer) */
2769 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
2770 uint32_t instance_methods; /* struct objc_method_description_list *
2772 uint32_t class_methods; /* struct objc_method_description_list *
2776 struct objc_method_description_list_t {
2778 // struct objc_method_description_t list[1];
2781 struct objc_method_description_t {
2782 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2783 uint32_t types; /* char * (32-bit pointer) */
2786 inline void swapStruct(struct cfstring64_t &cfs) {
2787 sys::swapByteOrder(cfs.isa);
2788 sys::swapByteOrder(cfs.flags);
2789 sys::swapByteOrder(cfs.characters);
2790 sys::swapByteOrder(cfs.length);
2793 inline void swapStruct(struct class64_t &c) {
2794 sys::swapByteOrder(c.isa);
2795 sys::swapByteOrder(c.superclass);
2796 sys::swapByteOrder(c.cache);
2797 sys::swapByteOrder(c.vtable);
2798 sys::swapByteOrder(c.data);
2801 inline void swapStruct(struct class32_t &c) {
2802 sys::swapByteOrder(c.isa);
2803 sys::swapByteOrder(c.superclass);
2804 sys::swapByteOrder(c.cache);
2805 sys::swapByteOrder(c.vtable);
2806 sys::swapByteOrder(c.data);
2809 inline void swapStruct(struct class_ro64_t &cro) {
2810 sys::swapByteOrder(cro.flags);
2811 sys::swapByteOrder(cro.instanceStart);
2812 sys::swapByteOrder(cro.instanceSize);
2813 sys::swapByteOrder(cro.reserved);
2814 sys::swapByteOrder(cro.ivarLayout);
2815 sys::swapByteOrder(cro.name);
2816 sys::swapByteOrder(cro.baseMethods);
2817 sys::swapByteOrder(cro.baseProtocols);
2818 sys::swapByteOrder(cro.ivars);
2819 sys::swapByteOrder(cro.weakIvarLayout);
2820 sys::swapByteOrder(cro.baseProperties);
2823 inline void swapStruct(struct class_ro32_t &cro) {
2824 sys::swapByteOrder(cro.flags);
2825 sys::swapByteOrder(cro.instanceStart);
2826 sys::swapByteOrder(cro.instanceSize);
2827 sys::swapByteOrder(cro.ivarLayout);
2828 sys::swapByteOrder(cro.name);
2829 sys::swapByteOrder(cro.baseMethods);
2830 sys::swapByteOrder(cro.baseProtocols);
2831 sys::swapByteOrder(cro.ivars);
2832 sys::swapByteOrder(cro.weakIvarLayout);
2833 sys::swapByteOrder(cro.baseProperties);
2836 inline void swapStruct(struct method_list64_t &ml) {
2837 sys::swapByteOrder(ml.entsize);
2838 sys::swapByteOrder(ml.count);
2841 inline void swapStruct(struct method_list32_t &ml) {
2842 sys::swapByteOrder(ml.entsize);
2843 sys::swapByteOrder(ml.count);
2846 inline void swapStruct(struct method64_t &m) {
2847 sys::swapByteOrder(m.name);
2848 sys::swapByteOrder(m.types);
2849 sys::swapByteOrder(m.imp);
2852 inline void swapStruct(struct method32_t &m) {
2853 sys::swapByteOrder(m.name);
2854 sys::swapByteOrder(m.types);
2855 sys::swapByteOrder(m.imp);
2858 inline void swapStruct(struct protocol_list64_t &pl) {
2859 sys::swapByteOrder(pl.count);
2862 inline void swapStruct(struct protocol_list32_t &pl) {
2863 sys::swapByteOrder(pl.count);
2866 inline void swapStruct(struct protocol64_t &p) {
2867 sys::swapByteOrder(p.isa);
2868 sys::swapByteOrder(p.name);
2869 sys::swapByteOrder(p.protocols);
2870 sys::swapByteOrder(p.instanceMethods);
2871 sys::swapByteOrder(p.classMethods);
2872 sys::swapByteOrder(p.optionalInstanceMethods);
2873 sys::swapByteOrder(p.optionalClassMethods);
2874 sys::swapByteOrder(p.instanceProperties);
2877 inline void swapStruct(struct protocol32_t &p) {
2878 sys::swapByteOrder(p.isa);
2879 sys::swapByteOrder(p.name);
2880 sys::swapByteOrder(p.protocols);
2881 sys::swapByteOrder(p.instanceMethods);
2882 sys::swapByteOrder(p.classMethods);
2883 sys::swapByteOrder(p.optionalInstanceMethods);
2884 sys::swapByteOrder(p.optionalClassMethods);
2885 sys::swapByteOrder(p.instanceProperties);
2888 inline void swapStruct(struct ivar_list64_t &il) {
2889 sys::swapByteOrder(il.entsize);
2890 sys::swapByteOrder(il.count);
2893 inline void swapStruct(struct ivar_list32_t &il) {
2894 sys::swapByteOrder(il.entsize);
2895 sys::swapByteOrder(il.count);
2898 inline void swapStruct(struct ivar64_t &i) {
2899 sys::swapByteOrder(i.offset);
2900 sys::swapByteOrder(i.name);
2901 sys::swapByteOrder(i.type);
2902 sys::swapByteOrder(i.alignment);
2903 sys::swapByteOrder(i.size);
2906 inline void swapStruct(struct ivar32_t &i) {
2907 sys::swapByteOrder(i.offset);
2908 sys::swapByteOrder(i.name);
2909 sys::swapByteOrder(i.type);
2910 sys::swapByteOrder(i.alignment);
2911 sys::swapByteOrder(i.size);
2914 inline void swapStruct(struct objc_property_list64 &pl) {
2915 sys::swapByteOrder(pl.entsize);
2916 sys::swapByteOrder(pl.count);
2919 inline void swapStruct(struct objc_property_list32 &pl) {
2920 sys::swapByteOrder(pl.entsize);
2921 sys::swapByteOrder(pl.count);
2924 inline void swapStruct(struct objc_property64 &op) {
2925 sys::swapByteOrder(op.name);
2926 sys::swapByteOrder(op.attributes);
2929 inline void swapStruct(struct objc_property32 &op) {
2930 sys::swapByteOrder(op.name);
2931 sys::swapByteOrder(op.attributes);
2934 inline void swapStruct(struct category64_t &c) {
2935 sys::swapByteOrder(c.name);
2936 sys::swapByteOrder(c.cls);
2937 sys::swapByteOrder(c.instanceMethods);
2938 sys::swapByteOrder(c.classMethods);
2939 sys::swapByteOrder(c.protocols);
2940 sys::swapByteOrder(c.instanceProperties);
2943 inline void swapStruct(struct category32_t &c) {
2944 sys::swapByteOrder(c.name);
2945 sys::swapByteOrder(c.cls);
2946 sys::swapByteOrder(c.instanceMethods);
2947 sys::swapByteOrder(c.classMethods);
2948 sys::swapByteOrder(c.protocols);
2949 sys::swapByteOrder(c.instanceProperties);
2952 inline void swapStruct(struct objc_image_info64 &o) {
2953 sys::swapByteOrder(o.version);
2954 sys::swapByteOrder(o.flags);
2957 inline void swapStruct(struct objc_image_info32 &o) {
2958 sys::swapByteOrder(o.version);
2959 sys::swapByteOrder(o.flags);
2962 inline void swapStruct(struct imageInfo_t &o) {
2963 sys::swapByteOrder(o.version);
2964 sys::swapByteOrder(o.flags);
2967 inline void swapStruct(struct message_ref64 &mr) {
2968 sys::swapByteOrder(mr.imp);
2969 sys::swapByteOrder(mr.sel);
2972 inline void swapStruct(struct message_ref32 &mr) {
2973 sys::swapByteOrder(mr.imp);
2974 sys::swapByteOrder(mr.sel);
2977 inline void swapStruct(struct objc_module_t &module) {
2978 sys::swapByteOrder(module.version);
2979 sys::swapByteOrder(module.size);
2980 sys::swapByteOrder(module.name);
2981 sys::swapByteOrder(module.symtab);
2984 inline void swapStruct(struct objc_symtab_t &symtab) {
2985 sys::swapByteOrder(symtab.sel_ref_cnt);
2986 sys::swapByteOrder(symtab.refs);
2987 sys::swapByteOrder(symtab.cls_def_cnt);
2988 sys::swapByteOrder(symtab.cat_def_cnt);
2991 inline void swapStruct(struct objc_class_t &objc_class) {
2992 sys::swapByteOrder(objc_class.isa);
2993 sys::swapByteOrder(objc_class.super_class);
2994 sys::swapByteOrder(objc_class.name);
2995 sys::swapByteOrder(objc_class.version);
2996 sys::swapByteOrder(objc_class.info);
2997 sys::swapByteOrder(objc_class.instance_size);
2998 sys::swapByteOrder(objc_class.ivars);
2999 sys::swapByteOrder(objc_class.methodLists);
3000 sys::swapByteOrder(objc_class.cache);
3001 sys::swapByteOrder(objc_class.protocols);
3004 inline void swapStruct(struct objc_category_t &objc_category) {
3005 sys::swapByteOrder(objc_category.category_name);
3006 sys::swapByteOrder(objc_category.class_name);
3007 sys::swapByteOrder(objc_category.instance_methods);
3008 sys::swapByteOrder(objc_category.class_methods);
3009 sys::swapByteOrder(objc_category.protocols);
3012 inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3013 sys::swapByteOrder(objc_ivar_list.ivar_count);
3016 inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3017 sys::swapByteOrder(objc_ivar.ivar_name);
3018 sys::swapByteOrder(objc_ivar.ivar_type);
3019 sys::swapByteOrder(objc_ivar.ivar_offset);
3022 inline void swapStruct(struct objc_method_list_t &method_list) {
3023 sys::swapByteOrder(method_list.obsolete);
3024 sys::swapByteOrder(method_list.method_count);
3027 inline void swapStruct(struct objc_method_t &method) {
3028 sys::swapByteOrder(method.method_name);
3029 sys::swapByteOrder(method.method_types);
3030 sys::swapByteOrder(method.method_imp);
3033 inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3034 sys::swapByteOrder(protocol_list.next);
3035 sys::swapByteOrder(protocol_list.count);
3038 inline void swapStruct(struct objc_protocol_t &protocol) {
3039 sys::swapByteOrder(protocol.isa);
3040 sys::swapByteOrder(protocol.protocol_name);
3041 sys::swapByteOrder(protocol.protocol_list);
3042 sys::swapByteOrder(protocol.instance_methods);
3043 sys::swapByteOrder(protocol.class_methods);
3046 inline void swapStruct(struct objc_method_description_list_t &mdl) {
3047 sys::swapByteOrder(mdl.count);
3050 inline void swapStruct(struct objc_method_description_t &md) {
3051 sys::swapByteOrder(md.name);
3052 sys::swapByteOrder(md.types);
3055 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3056 struct DisassembleInfo *info);
3058 // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3059 // to an Objective-C class and returns the class name. It is also passed the
3060 // address of the pointer, so when the pointer is zero as it can be in an .o
3061 // file, that is used to look for an external relocation entry with a symbol
3063 static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3064 uint64_t ReferenceValue,
3065 struct DisassembleInfo *info) {
3067 uint32_t offset, left;
3070 // The pointer_value can be 0 in an object file and have a relocation
3071 // entry for the class symbol at the ReferenceValue (the address of the
3073 if (pointer_value == 0) {
3074 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3075 if (r == nullptr || left < sizeof(uint64_t))
3078 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3079 if (symbol_name == nullptr)
3081 const char *class_name = strrchr(symbol_name, '$');
3082 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3083 return class_name + 2;
3088 // The case were the pointer_value is non-zero and points to a class defined
3089 // in this Mach-O file.
3090 r = get_pointer_64(pointer_value, offset, left, S, info);
3091 if (r == nullptr || left < sizeof(struct class64_t))
3094 memcpy(&c, r, sizeof(struct class64_t));
3095 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3099 r = get_pointer_64(c.data, offset, left, S, info);
3100 if (r == nullptr || left < sizeof(struct class_ro64_t))
3102 struct class_ro64_t cro;
3103 memcpy(&cro, r, sizeof(struct class_ro64_t));
3104 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3108 const char *name = get_pointer_64(cro.name, offset, left, S, info);
3112 // get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3113 // pointer to a cfstring and returns its name or nullptr.
3114 static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3115 struct DisassembleInfo *info) {
3116 const char *r, *name;
3117 uint32_t offset, left;
3119 struct cfstring64_t cfs;
3120 uint64_t cfs_characters;
3122 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3123 if (r == nullptr || left < sizeof(struct cfstring64_t))
3125 memcpy(&cfs, r, sizeof(struct cfstring64_t));
3126 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3128 if (cfs.characters == 0) {
3130 const char *symbol_name = get_symbol_64(
3131 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
3132 if (symbol_name == nullptr)
3134 cfs_characters = n_value;
3136 cfs_characters = cfs.characters;
3137 name = get_pointer_64(cfs_characters, offset, left, S, info);
3142 // get_objc2_64bit_selref() is used for disassembly and is passed a the address
3143 // of a pointer to an Objective-C selector reference when the pointer value is
3144 // zero as in a .o file and is likely to have a external relocation entry with
3145 // who's symbol's n_value is the real pointer to the selector name. If that is
3146 // the case the real pointer to the selector name is returned else 0 is
3148 static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3149 struct DisassembleInfo *info) {
3150 uint32_t offset, left;
3153 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3154 if (r == nullptr || left < sizeof(uint64_t))
3157 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3158 if (symbol_name == nullptr)
3163 static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3164 const char *sectname) {
3165 for (const SectionRef &Section : O->sections()) {
3167 Section.getName(SectName);
3168 DataRefImpl Ref = Section.getRawDataRefImpl();
3169 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3170 if (SegName == segname && SectName == sectname)
3173 return SectionRef();
3177 walk_pointer_list_64(const char *listname, const SectionRef S,
3178 MachOObjectFile *O, struct DisassembleInfo *info,
3179 void (*func)(uint64_t, struct DisassembleInfo *info)) {
3180 if (S == SectionRef())
3184 S.getName(SectName);
3185 DataRefImpl Ref = S.getRawDataRefImpl();
3186 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3187 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3190 S.getContents(BytesStr);
3191 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3193 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3194 uint32_t left = S.getSize() - i;
3195 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3197 memcpy(&p, Contents + i, size);
3198 if (i + sizeof(uint64_t) > S.getSize())
3199 outs() << listname << " list pointer extends past end of (" << SegName
3200 << "," << SectName << ") section\n";
3201 outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
3203 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3204 sys::swapByteOrder(p);
3206 uint64_t n_value = 0;
3207 const char *name = get_symbol_64(i, S, info, n_value, p);
3208 if (name == nullptr)
3209 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
3212 outs() << format("0x%" PRIx64, n_value);
3214 outs() << " + " << format("0x%" PRIx64, p);
3216 outs() << format("0x%" PRIx64, p);
3217 if (name != nullptr)
3218 outs() << " " << name;
3228 walk_pointer_list_32(const char *listname, const SectionRef S,
3229 MachOObjectFile *O, struct DisassembleInfo *info,
3230 void (*func)(uint32_t, struct DisassembleInfo *info)) {
3231 if (S == SectionRef())
3235 S.getName(SectName);
3236 DataRefImpl Ref = S.getRawDataRefImpl();
3237 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3238 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3241 S.getContents(BytesStr);
3242 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3244 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
3245 uint32_t left = S.getSize() - i;
3246 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
3248 memcpy(&p, Contents + i, size);
3249 if (i + sizeof(uint32_t) > S.getSize())
3250 outs() << listname << " list pointer extends past end of (" << SegName
3251 << "," << SectName << ") section\n";
3252 uint32_t Address = S.getAddress() + i;
3253 outs() << format("%08" PRIx32, Address) << " ";
3255 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3256 sys::swapByteOrder(p);
3257 outs() << format("0x%" PRIx32, p);
3259 const char *name = get_symbol_32(i, S, info, p);
3260 if (name != nullptr)
3261 outs() << " " << name;
3269 static void print_layout_map(const char *layout_map, uint32_t left) {
3270 outs() << " layout map: ";
3272 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
3275 } while (*layout_map != '\0' && left != 0);
3279 static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
3280 uint32_t offset, left;
3282 const char *layout_map;
3286 layout_map = get_pointer_64(p, offset, left, S, info);
3287 print_layout_map(layout_map, left);
3290 static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
3291 uint32_t offset, left;
3293 const char *layout_map;
3297 layout_map = get_pointer_32(p, offset, left, S, info);
3298 print_layout_map(layout_map, left);
3301 static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
3302 const char *indent) {
3303 struct method_list64_t ml;
3304 struct method64_t m;
3306 uint32_t offset, xoffset, left, i;
3308 const char *name, *sym_name;
3311 r = get_pointer_64(p, offset, left, S, info);
3314 memset(&ml, '\0', sizeof(struct method_list64_t));
3315 if (left < sizeof(struct method_list64_t)) {
3316 memcpy(&ml, r, left);
3317 outs() << " (method_list_t entends past the end of the section)\n";
3319 memcpy(&ml, r, sizeof(struct method_list64_t));
3320 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3322 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3323 outs() << indent << "\t\t count " << ml.count << "\n";
3325 p += sizeof(struct method_list64_t);
3326 offset += sizeof(struct method_list64_t);
3327 for (i = 0; i < ml.count; i++) {
3328 r = get_pointer_64(p, offset, left, S, info);
3331 memset(&m, '\0', sizeof(struct method64_t));
3332 if (left < sizeof(struct method64_t)) {
3333 memcpy(&ml, r, left);
3334 outs() << indent << " (method_t entends past the end of the section)\n";
3336 memcpy(&m, r, sizeof(struct method64_t));
3337 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3340 outs() << indent << "\t\t name ";
3341 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
3342 info, n_value, m.name);
3344 if (info->verbose && sym_name != nullptr)
3347 outs() << format("0x%" PRIx64, n_value);
3349 outs() << " + " << format("0x%" PRIx64, m.name);
3351 outs() << format("0x%" PRIx64, m.name);
3352 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
3353 if (name != nullptr)
3354 outs() << format(" %.*s", left, name);
3357 outs() << indent << "\t\t types ";
3358 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
3359 info, n_value, m.types);
3361 if (info->verbose && sym_name != nullptr)
3364 outs() << format("0x%" PRIx64, n_value);
3366 outs() << " + " << format("0x%" PRIx64, m.types);
3368 outs() << format("0x%" PRIx64, m.types);
3369 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
3370 if (name != nullptr)
3371 outs() << format(" %.*s", left, name);
3374 outs() << indent << "\t\t imp ";
3375 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
3377 if (info->verbose && name == nullptr) {
3379 outs() << format("0x%" PRIx64, n_value) << " ";
3381 outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
3383 outs() << format("0x%" PRIx64, m.imp) << " ";
3385 if (name != nullptr)
3389 p += sizeof(struct method64_t);
3390 offset += sizeof(struct method64_t);
3394 static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
3395 const char *indent) {
3396 struct method_list32_t ml;
3397 struct method32_t m;
3398 const char *r, *name;
3399 uint32_t offset, xoffset, left, i;
3402 r = get_pointer_32(p, offset, left, S, info);
3405 memset(&ml, '\0', sizeof(struct method_list32_t));
3406 if (left < sizeof(struct method_list32_t)) {
3407 memcpy(&ml, r, left);
3408 outs() << " (method_list_t entends past the end of the section)\n";
3410 memcpy(&ml, r, sizeof(struct method_list32_t));
3411 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3413 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3414 outs() << indent << "\t\t count " << ml.count << "\n";
3416 p += sizeof(struct method_list32_t);
3417 offset += sizeof(struct method_list32_t);
3418 for (i = 0; i < ml.count; i++) {
3419 r = get_pointer_32(p, offset, left, S, info);
3422 memset(&m, '\0', sizeof(struct method32_t));
3423 if (left < sizeof(struct method32_t)) {
3424 memcpy(&ml, r, left);
3425 outs() << indent << " (method_t entends past the end of the section)\n";
3427 memcpy(&m, r, sizeof(struct method32_t));
3428 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3431 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
3432 name = get_pointer_32(m.name, xoffset, left, xS, info);
3433 if (name != nullptr)
3434 outs() << format(" %.*s", left, name);
3437 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
3438 name = get_pointer_32(m.types, xoffset, left, xS, info);
3439 if (name != nullptr)
3440 outs() << format(" %.*s", left, name);
3443 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
3444 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
3446 if (name != nullptr)
3447 outs() << " " << name;
3450 p += sizeof(struct method32_t);
3451 offset += sizeof(struct method32_t);
3455 static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
3456 uint32_t offset, left, xleft;
3458 struct objc_method_list_t method_list;
3459 struct objc_method_t method;
3460 const char *r, *methods, *name, *SymbolName;
3463 r = get_pointer_32(p, offset, left, S, info, true);
3468 if (left > sizeof(struct objc_method_list_t)) {
3469 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
3471 outs() << "\t\t objc_method_list extends past end of the section\n";
3472 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
3473 memcpy(&method_list, r, left);
3475 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3476 swapStruct(method_list);
3478 outs() << "\t\t obsolete "
3479 << format("0x%08" PRIx32, method_list.obsolete) << "\n";
3480 outs() << "\t\t method_count " << method_list.method_count << "\n";
3482 methods = r + sizeof(struct objc_method_list_t);
3483 for (i = 0; i < method_list.method_count; i++) {
3484 if ((i + 1) * sizeof(struct objc_method_t) > left) {
3485 outs() << "\t\t remaining method's extend past the of the section\n";
3488 memcpy(&method, methods + i * sizeof(struct objc_method_t),
3489 sizeof(struct objc_method_t));
3490 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3493 outs() << "\t\t method_name "
3494 << format("0x%08" PRIx32, method.method_name);
3495 if (info->verbose) {
3496 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
3497 if (name != nullptr)
3498 outs() << format(" %.*s", xleft, name);
3500 outs() << " (not in an __OBJC section)";
3504 outs() << "\t\t method_types "
3505 << format("0x%08" PRIx32, method.method_types);
3506 if (info->verbose) {
3507 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
3508 if (name != nullptr)
3509 outs() << format(" %.*s", xleft, name);
3511 outs() << " (not in an __OBJC section)";
3515 outs() << "\t\t method_imp "
3516 << format("0x%08" PRIx32, method.method_imp) << " ";
3517 if (info->verbose) {
3518 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
3519 if (SymbolName != nullptr)
3520 outs() << SymbolName;
3527 static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
3528 struct protocol_list64_t pl;
3529 uint64_t q, n_value;
3530 struct protocol64_t pc;
3532 uint32_t offset, xoffset, left, i;
3534 const char *name, *sym_name;
3536 r = get_pointer_64(p, offset, left, S, info);
3539 memset(&pl, '\0', sizeof(struct protocol_list64_t));
3540 if (left < sizeof(struct protocol_list64_t)) {
3541 memcpy(&pl, r, left);
3542 outs() << " (protocol_list_t entends past the end of the section)\n";
3544 memcpy(&pl, r, sizeof(struct protocol_list64_t));
3545 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3547 outs() << " count " << pl.count << "\n";
3549 p += sizeof(struct protocol_list64_t);
3550 offset += sizeof(struct protocol_list64_t);
3551 for (i = 0; i < pl.count; i++) {
3552 r = get_pointer_64(p, offset, left, S, info);
3556 if (left < sizeof(uint64_t)) {
3557 memcpy(&q, r, left);
3558 outs() << " (protocol_t * entends past the end of the section)\n";
3560 memcpy(&q, r, sizeof(uint64_t));
3561 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3562 sys::swapByteOrder(q);
3564 outs() << "\t\t list[" << i << "] ";
3565 sym_name = get_symbol_64(offset, S, info, n_value, q);
3567 if (info->verbose && sym_name != nullptr)
3570 outs() << format("0x%" PRIx64, n_value);
3572 outs() << " + " << format("0x%" PRIx64, q);
3574 outs() << format("0x%" PRIx64, q);
3575 outs() << " (struct protocol_t *)\n";
3577 r = get_pointer_64(q + n_value, offset, left, S, info);
3580 memset(&pc, '\0', sizeof(struct protocol64_t));
3581 if (left < sizeof(struct protocol64_t)) {
3582 memcpy(&pc, r, left);
3583 outs() << " (protocol_t entends past the end of the section)\n";
3585 memcpy(&pc, r, sizeof(struct protocol64_t));
3586 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3589 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
3591 outs() << "\t\t\t name ";
3592 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
3593 info, n_value, pc.name);
3595 if (info->verbose && sym_name != nullptr)
3598 outs() << format("0x%" PRIx64, n_value);
3600 outs() << " + " << format("0x%" PRIx64, pc.name);
3602 outs() << format("0x%" PRIx64, pc.name);
3603 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
3604 if (name != nullptr)
3605 outs() << format(" %.*s", left, name);
3608 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
3610 outs() << "\t\t instanceMethods ";
3612 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
3613 S, info, n_value, pc.instanceMethods);
3615 if (info->verbose && sym_name != nullptr)
3618 outs() << format("0x%" PRIx64, n_value);
3619 if (pc.instanceMethods != 0)
3620 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
3622 outs() << format("0x%" PRIx64, pc.instanceMethods);
3623 outs() << " (struct method_list_t *)\n";
3624 if (pc.instanceMethods + n_value != 0)
3625 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
3627 outs() << "\t\t classMethods ";
3629 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
3630 info, n_value, pc.classMethods);
3632 if (info->verbose && sym_name != nullptr)
3635 outs() << format("0x%" PRIx64, n_value);
3636 if (pc.classMethods != 0)
3637 outs() << " + " << format("0x%" PRIx64, pc.classMethods);
3639 outs() << format("0x%" PRIx64, pc.classMethods);
3640 outs() << " (struct method_list_t *)\n";
3641 if (pc.classMethods + n_value != 0)
3642 print_method_list64_t(pc.classMethods + n_value, info, "\t");
3644 outs() << "\t optionalInstanceMethods "
3645 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
3646 outs() << "\t optionalClassMethods "
3647 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
3648 outs() << "\t instanceProperties "
3649 << format("0x%" PRIx64, pc.instanceProperties) << "\n";
3651 p += sizeof(uint64_t);
3652 offset += sizeof(uint64_t);
3656 static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
3657 struct protocol_list32_t pl;
3659 struct protocol32_t pc;
3661 uint32_t offset, xoffset, left, i;
3665 r = get_pointer_32(p, offset, left, S, info);
3668 memset(&pl, '\0', sizeof(struct protocol_list32_t));
3669 if (left < sizeof(struct protocol_list32_t)) {
3670 memcpy(&pl, r, left);
3671 outs() << " (protocol_list_t entends past the end of the section)\n";
3673 memcpy(&pl, r, sizeof(struct protocol_list32_t));
3674 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3676 outs() << " count " << pl.count << "\n";
3678 p += sizeof(struct protocol_list32_t);
3679 offset += sizeof(struct protocol_list32_t);
3680 for (i = 0; i < pl.count; i++) {
3681 r = get_pointer_32(p, offset, left, S, info);
3685 if (left < sizeof(uint32_t)) {
3686 memcpy(&q, r, left);
3687 outs() << " (protocol_t * entends past the end of the section)\n";
3689 memcpy(&q, r, sizeof(uint32_t));
3690 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3691 sys::swapByteOrder(q);
3692 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
3693 << " (struct protocol_t *)\n";
3694 r = get_pointer_32(q, offset, left, S, info);
3697 memset(&pc, '\0', sizeof(struct protocol32_t));
3698 if (left < sizeof(struct protocol32_t)) {
3699 memcpy(&pc, r, left);
3700 outs() << " (protocol_t entends past the end of the section)\n";
3702 memcpy(&pc, r, sizeof(struct protocol32_t));
3703 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3705 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
3706 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
3707 name = get_pointer_32(pc.name, xoffset, left, xS, info);
3708 if (name != nullptr)
3709 outs() << format(" %.*s", left, name);
3711 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
3712 outs() << "\t\t instanceMethods "
3713 << format("0x%" PRIx32, pc.instanceMethods)
3714 << " (struct method_list_t *)\n";
3715 if (pc.instanceMethods != 0)
3716 print_method_list32_t(pc.instanceMethods, info, "\t");
3717 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
3718 << " (struct method_list_t *)\n";
3719 if (pc.classMethods != 0)
3720 print_method_list32_t(pc.classMethods, info, "\t");
3721 outs() << "\t optionalInstanceMethods "
3722 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
3723 outs() << "\t optionalClassMethods "
3724 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
3725 outs() << "\t instanceProperties "
3726 << format("0x%" PRIx32, pc.instanceProperties) << "\n";
3727 p += sizeof(uint32_t);
3728 offset += sizeof(uint32_t);
3732 static void print_indent(uint32_t indent) {
3733 for (uint32_t i = 0; i < indent;) {
3734 if (indent - i >= 8) {
3738 for (uint32_t j = i; j < indent; j++)
3745 static bool print_method_description_list(uint32_t p, uint32_t indent,
3746 struct DisassembleInfo *info) {
3747 uint32_t offset, left, xleft;
3749 struct objc_method_description_list_t mdl;
3750 struct objc_method_description_t md;
3751 const char *r, *list, *name;
3754 r = get_pointer_32(p, offset, left, S, info, true);
3759 if (left > sizeof(struct objc_method_description_list_t)) {
3760 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
3762 print_indent(indent);
3763 outs() << " objc_method_description_list extends past end of the section\n";
3764 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
3765 memcpy(&mdl, r, left);
3767 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3770 print_indent(indent);
3771 outs() << " count " << mdl.count << "\n";
3773 list = r + sizeof(struct objc_method_description_list_t);
3774 for (i = 0; i < mdl.count; i++) {
3775 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
3776 print_indent(indent);
3777 outs() << " remaining list entries extend past the of the section\n";
3780 print_indent(indent);
3781 outs() << " list[" << i << "]\n";
3782 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
3783 sizeof(struct objc_method_description_t));
3784 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3787 print_indent(indent);
3788 outs() << " name " << format("0x%08" PRIx32, md.name);
3789 if (info->verbose) {
3790 name = get_pointer_32(md.name, offset, xleft, S, info, true);
3791 if (name != nullptr)
3792 outs() << format(" %.*s", xleft, name);
3794 outs() << " (not in an __OBJC section)";
3798 print_indent(indent);
3799 outs() << " types " << format("0x%08" PRIx32, md.types);
3800 if (info->verbose) {
3801 name = get_pointer_32(md.types, offset, xleft, S, info, true);
3802 if (name != nullptr)
3803 outs() << format(" %.*s", xleft, name);
3805 outs() << " (not in an __OBJC section)";
3812 static bool print_protocol_list(uint32_t p, uint32_t indent,
3813 struct DisassembleInfo *info);
3815 static bool print_protocol(uint32_t p, uint32_t indent,
3816 struct DisassembleInfo *info) {
3817 uint32_t offset, left;
3819 struct objc_protocol_t protocol;
3820 const char *r, *name;
3822 r = get_pointer_32(p, offset, left, S, info, true);
3827 if (left >= sizeof(struct objc_protocol_t)) {
3828 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
3830 print_indent(indent);
3831 outs() << " Protocol extends past end of the section\n";
3832 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
3833 memcpy(&protocol, r, left);
3835 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3836 swapStruct(protocol);
3838 print_indent(indent);
3839 outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
3842 print_indent(indent);
3843 outs() << " protocol_name "
3844 << format("0x%08" PRIx32, protocol.protocol_name);
3845 if (info->verbose) {
3846 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
3847 if (name != nullptr)
3848 outs() << format(" %.*s", left, name);
3850 outs() << " (not in an __OBJC section)";
3854 print_indent(indent);
3855 outs() << " protocol_list "
3856 << format("0x%08" PRIx32, protocol.protocol_list);
3857 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
3858 outs() << " (not in an __OBJC section)\n";
3860 print_indent(indent);
3861 outs() << " instance_methods "
3862 << format("0x%08" PRIx32, protocol.instance_methods);
3863 if (print_method_description_list(protocol.instance_methods, indent, info))
3864 outs() << " (not in an __OBJC section)\n";
3866 print_indent(indent);
3867 outs() << " class_methods "
3868 << format("0x%08" PRIx32, protocol.class_methods);
3869 if (print_method_description_list(protocol.class_methods, indent, info))
3870 outs() << " (not in an __OBJC section)\n";
3875 static bool print_protocol_list(uint32_t p, uint32_t indent,
3876 struct DisassembleInfo *info) {
3877 uint32_t offset, left, l;
3879 struct objc_protocol_list_t protocol_list;
3880 const char *r, *list;
3883 r = get_pointer_32(p, offset, left, S, info, true);
3888 if (left > sizeof(struct objc_protocol_list_t)) {
3889 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
3891 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
3892 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
3893 memcpy(&protocol_list, r, left);
3895 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3896 swapStruct(protocol_list);
3898 print_indent(indent);
3899 outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
3901 print_indent(indent);
3902 outs() << " count " << protocol_list.count << "\n";
3904 list = r + sizeof(struct objc_protocol_list_t);
3905 for (i = 0; i < protocol_list.count; i++) {
3906 if ((i + 1) * sizeof(uint32_t) > left) {
3907 outs() << "\t\t remaining list entries extend past the of the section\n";
3910 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
3911 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3912 sys::swapByteOrder(l);
3914 print_indent(indent);
3915 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
3916 if (print_protocol(l, indent, info))
3917 outs() << "(not in an __OBJC section)\n";
3922 static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
3923 struct ivar_list64_t il;
3926 uint32_t offset, xoffset, left, j;
3928 const char *name, *sym_name, *ivar_offset_p;
3929 uint64_t ivar_offset, n_value;
3931 r = get_pointer_64(p, offset, left, S, info);
3934 memset(&il, '\0', sizeof(struct ivar_list64_t));
3935 if (left < sizeof(struct ivar_list64_t)) {
3936 memcpy(&il, r, left);
3937 outs() << " (ivar_list_t entends past the end of the section)\n";
3939 memcpy(&il, r, sizeof(struct ivar_list64_t));
3940 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3942 outs() << " entsize " << il.entsize << "\n";
3943 outs() << " count " << il.count << "\n";
3945 p += sizeof(struct ivar_list64_t);
3946 offset += sizeof(struct ivar_list64_t);
3947 for (j = 0; j < il.count; j++) {
3948 r = get_pointer_64(p, offset, left, S, info);
3951 memset(&i, '\0', sizeof(struct ivar64_t));
3952 if (left < sizeof(struct ivar64_t)) {
3953 memcpy(&i, r, left);
3954 outs() << " (ivar_t entends past the end of the section)\n";
3956 memcpy(&i, r, sizeof(struct ivar64_t));
3957 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3960 outs() << "\t\t\t offset ";
3961 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
3962 info, n_value, i.offset);
3964 if (info->verbose && sym_name != nullptr)
3967 outs() << format("0x%" PRIx64, n_value);
3969 outs() << " + " << format("0x%" PRIx64, i.offset);
3971 outs() << format("0x%" PRIx64, i.offset);
3972 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
3973 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
3974 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
3975 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3976 sys::swapByteOrder(ivar_offset);
3977 outs() << " " << ivar_offset << "\n";
3981 outs() << "\t\t\t name ";
3982 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
3985 if (info->verbose && sym_name != nullptr)
3988 outs() << format("0x%" PRIx64, n_value);
3990 outs() << " + " << format("0x%" PRIx64, i.name);
3992 outs() << format("0x%" PRIx64, i.name);
3993 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
3994 if (name != nullptr)
3995 outs() << format(" %.*s", left, name);
3998 outs() << "\t\t\t type ";
3999 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
4001 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
4003 if (info->verbose && sym_name != nullptr)
4006 outs() << format("0x%" PRIx64, n_value);
4008 outs() << " + " << format("0x%" PRIx64, i.type);
4010 outs() << format("0x%" PRIx64, i.type);
4011 if (name != nullptr)
4012 outs() << format(" %.*s", left, name);
4015 outs() << "\t\t\talignment " << i.alignment << "\n";
4016 outs() << "\t\t\t size " << i.size << "\n";
4018 p += sizeof(struct ivar64_t);
4019 offset += sizeof(struct ivar64_t);
4023 static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
4024 struct ivar_list32_t il;
4027 uint32_t offset, xoffset, left, j;
4029 const char *name, *ivar_offset_p;
4030 uint32_t ivar_offset;
4032 r = get_pointer_32(p, offset, left, S, info);
4035 memset(&il, '\0', sizeof(struct ivar_list32_t));
4036 if (left < sizeof(struct ivar_list32_t)) {
4037 memcpy(&il, r, left);
4038 outs() << " (ivar_list_t entends past the end of the section)\n";
4040 memcpy(&il, r, sizeof(struct ivar_list32_t));
4041 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4043 outs() << " entsize " << il.entsize << "\n";
4044 outs() << " count " << il.count << "\n";
4046 p += sizeof(struct ivar_list32_t);
4047 offset += sizeof(struct ivar_list32_t);
4048 for (j = 0; j < il.count; j++) {
4049 r = get_pointer_32(p, offset, left, S, info);
4052 memset(&i, '\0', sizeof(struct ivar32_t));
4053 if (left < sizeof(struct ivar32_t)) {
4054 memcpy(&i, r, left);
4055 outs() << " (ivar_t entends past the end of the section)\n";
4057 memcpy(&i, r, sizeof(struct ivar32_t));
4058 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4061 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
4062 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4063 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4064 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4065 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4066 sys::swapByteOrder(ivar_offset);
4067 outs() << " " << ivar_offset << "\n";
4071 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
4072 name = get_pointer_32(i.name, xoffset, left, xS, info);
4073 if (name != nullptr)
4074 outs() << format(" %.*s", left, name);
4077 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
4078 name = get_pointer_32(i.type, xoffset, left, xS, info);
4079 if (name != nullptr)
4080 outs() << format(" %.*s", left, name);
4083 outs() << "\t\t\talignment " << i.alignment << "\n";
4084 outs() << "\t\t\t size " << i.size << "\n";
4086 p += sizeof(struct ivar32_t);
4087 offset += sizeof(struct ivar32_t);
4091 static void print_objc_property_list64(uint64_t p,
4092 struct DisassembleInfo *info) {
4093 struct objc_property_list64 opl;
4094 struct objc_property64 op;
4096 uint32_t offset, xoffset, left, j;
4098 const char *name, *sym_name;
4101 r = get_pointer_64(p, offset, left, S, info);
4104 memset(&opl, '\0', sizeof(struct objc_property_list64));
4105 if (left < sizeof(struct objc_property_list64)) {
4106 memcpy(&opl, r, left);
4107 outs() << " (objc_property_list entends past the end of the section)\n";
4109 memcpy(&opl, r, sizeof(struct objc_property_list64));
4110 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4112 outs() << " entsize " << opl.entsize << "\n";
4113 outs() << " count " << opl.count << "\n";
4115 p += sizeof(struct objc_property_list64);
4116 offset += sizeof(struct objc_property_list64);
4117 for (j = 0; j < opl.count; j++) {
4118 r = get_pointer_64(p, offset, left, S, info);
4121 memset(&op, '\0', sizeof(struct objc_property64));
4122 if (left < sizeof(struct objc_property64)) {
4123 memcpy(&op, r, left);
4124 outs() << " (objc_property entends past the end of the section)\n";
4126 memcpy(&op, r, sizeof(struct objc_property64));
4127 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4130 outs() << "\t\t\t name ";
4131 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
4132 info, n_value, op.name);
4134 if (info->verbose && sym_name != nullptr)
4137 outs() << format("0x%" PRIx64, n_value);
4139 outs() << " + " << format("0x%" PRIx64, op.name);
4141 outs() << format("0x%" PRIx64, op.name);
4142 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
4143 if (name != nullptr)
4144 outs() << format(" %.*s", left, name);
4147 outs() << "\t\t\tattributes ";
4149 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
4150 info, n_value, op.attributes);
4152 if (info->verbose && sym_name != nullptr)
4155 outs() << format("0x%" PRIx64, n_value);
4156 if (op.attributes != 0)
4157 outs() << " + " << format("0x%" PRIx64, op.attributes);
4159 outs() << format("0x%" PRIx64, op.attributes);
4160 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
4161 if (name != nullptr)
4162 outs() << format(" %.*s", left, name);
4165 p += sizeof(struct objc_property64);
4166 offset += sizeof(struct objc_property64);
4170 static void print_objc_property_list32(uint32_t p,
4171 struct DisassembleInfo *info) {
4172 struct objc_property_list32 opl;
4173 struct objc_property32 op;
4175 uint32_t offset, xoffset, left, j;
4179 r = get_pointer_32(p, offset, left, S, info);
4182 memset(&opl, '\0', sizeof(struct objc_property_list32));
4183 if (left < sizeof(struct objc_property_list32)) {
4184 memcpy(&opl, r, left);
4185 outs() << " (objc_property_list entends past the end of the section)\n";
4187 memcpy(&opl, r, sizeof(struct objc_property_list32));
4188 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4190 outs() << " entsize " << opl.entsize << "\n";
4191 outs() << " count " << opl.count << "\n";
4193 p += sizeof(struct objc_property_list32);
4194 offset += sizeof(struct objc_property_list32);
4195 for (j = 0; j < opl.count; j++) {
4196 r = get_pointer_32(p, offset, left, S, info);
4199 memset(&op, '\0', sizeof(struct objc_property32));
4200 if (left < sizeof(struct objc_property32)) {
4201 memcpy(&op, r, left);
4202 outs() << " (objc_property entends past the end of the section)\n";
4204 memcpy(&op, r, sizeof(struct objc_property32));
4205 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4208 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
4209 name = get_pointer_32(op.name, xoffset, left, xS, info);
4210 if (name != nullptr)
4211 outs() << format(" %.*s", left, name);
4214 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
4215 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
4216 if (name != nullptr)
4217 outs() << format(" %.*s", left, name);
4220 p += sizeof(struct objc_property32);
4221 offset += sizeof(struct objc_property32);
4225 static void print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
4226 bool &is_meta_class) {
4227 struct class_ro64_t cro;
4229 uint32_t offset, xoffset, left;
4231 const char *name, *sym_name;
4234 r = get_pointer_64(p, offset, left, S, info);
4235 if (r == nullptr || left < sizeof(struct class_ro64_t))
4237 memset(&cro, '\0', sizeof(struct class_ro64_t));
4238 if (left < sizeof(struct class_ro64_t)) {
4239 memcpy(&cro, r, left);
4240 outs() << " (class_ro_t entends past the end of the section)\n";
4242 memcpy(&cro, r, sizeof(struct class_ro64_t));
4243 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4245 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4246 if (cro.flags & RO_META)
4247 outs() << " RO_META";
4248 if (cro.flags & RO_ROOT)
4249 outs() << " RO_ROOT";
4250 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4251 outs() << " RO_HAS_CXX_STRUCTORS";
4253 outs() << " instanceStart " << cro.instanceStart << "\n";
4254 outs() << " instanceSize " << cro.instanceSize << "\n";
4255 outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
4257 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
4259 print_layout_map64(cro.ivarLayout, info);
4262 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
4263 info, n_value, cro.name);
4265 if (info->verbose && sym_name != nullptr)
4268 outs() << format("0x%" PRIx64, n_value);
4270 outs() << " + " << format("0x%" PRIx64, cro.name);
4272 outs() << format("0x%" PRIx64, cro.name);
4273 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
4274 if (name != nullptr)
4275 outs() << format(" %.*s", left, name);
4278 outs() << " baseMethods ";
4279 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
4280 S, info, n_value, cro.baseMethods);
4282 if (info->verbose && sym_name != nullptr)
4285 outs() << format("0x%" PRIx64, n_value);
4286 if (cro.baseMethods != 0)
4287 outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
4289 outs() << format("0x%" PRIx64, cro.baseMethods);
4290 outs() << " (struct method_list_t *)\n";
4291 if (cro.baseMethods + n_value != 0)
4292 print_method_list64_t(cro.baseMethods + n_value, info, "");
4294 outs() << " baseProtocols ";
4296 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
4297 info, n_value, cro.baseProtocols);
4299 if (info->verbose && sym_name != nullptr)
4302 outs() << format("0x%" PRIx64, n_value);
4303 if (cro.baseProtocols != 0)
4304 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
4306 outs() << format("0x%" PRIx64, cro.baseProtocols);
4308 if (cro.baseProtocols + n_value != 0)
4309 print_protocol_list64_t(cro.baseProtocols + n_value, info);
4311 outs() << " ivars ";
4312 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
4313 info, n_value, cro.ivars);
4315 if (info->verbose && sym_name != nullptr)
4318 outs() << format("0x%" PRIx64, n_value);
4320 outs() << " + " << format("0x%" PRIx64, cro.ivars);
4322 outs() << format("0x%" PRIx64, cro.ivars);
4324 if (cro.ivars + n_value != 0)
4325 print_ivar_list64_t(cro.ivars + n_value, info);
4327 outs() << " weakIvarLayout ";
4329 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
4330 info, n_value, cro.weakIvarLayout);
4332 if (info->verbose && sym_name != nullptr)
4335 outs() << format("0x%" PRIx64, n_value);
4336 if (cro.weakIvarLayout != 0)
4337 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
4339 outs() << format("0x%" PRIx64, cro.weakIvarLayout);
4341 print_layout_map64(cro.weakIvarLayout + n_value, info);
4343 outs() << " baseProperties ";
4345 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
4346 info, n_value, cro.baseProperties);
4348 if (info->verbose && sym_name != nullptr)
4351 outs() << format("0x%" PRIx64, n_value);
4352 if (cro.baseProperties != 0)
4353 outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
4355 outs() << format("0x%" PRIx64, cro.baseProperties);
4357 if (cro.baseProperties + n_value != 0)
4358 print_objc_property_list64(cro.baseProperties + n_value, info);
4360 is_meta_class = (cro.flags & RO_META) ? true : false;
4363 static void print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
4364 bool &is_meta_class) {
4365 struct class_ro32_t cro;
4367 uint32_t offset, xoffset, left;
4371 r = get_pointer_32(p, offset, left, S, info);
4374 memset(&cro, '\0', sizeof(struct class_ro32_t));
4375 if (left < sizeof(struct class_ro32_t)) {
4376 memcpy(&cro, r, left);
4377 outs() << " (class_ro_t entends past the end of the section)\n";
4379 memcpy(&cro, r, sizeof(struct class_ro32_t));
4380 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4382 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4383 if (cro.flags & RO_META)
4384 outs() << " RO_META";
4385 if (cro.flags & RO_ROOT)
4386 outs() << " RO_ROOT";
4387 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4388 outs() << " RO_HAS_CXX_STRUCTORS";
4390 outs() << " instanceStart " << cro.instanceStart << "\n";
4391 outs() << " instanceSize " << cro.instanceSize << "\n";
4392 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
4394 print_layout_map32(cro.ivarLayout, info);
4396 outs() << " name " << format("0x%" PRIx32, cro.name);
4397 name = get_pointer_32(cro.name, xoffset, left, xS, info);
4398 if (name != nullptr)
4399 outs() << format(" %.*s", left, name);
4402 outs() << " baseMethods "
4403 << format("0x%" PRIx32, cro.baseMethods)
4404 << " (struct method_list_t *)\n";
4405 if (cro.baseMethods != 0)
4406 print_method_list32_t(cro.baseMethods, info, "");
4408 outs() << " baseProtocols "
4409 << format("0x%" PRIx32, cro.baseProtocols) << "\n";
4410 if (cro.baseProtocols != 0)
4411 print_protocol_list32_t(cro.baseProtocols, info);
4412 outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
4415 print_ivar_list32_t(cro.ivars, info);
4416 outs() << " weakIvarLayout "
4417 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
4418 print_layout_map32(cro.weakIvarLayout, info);
4419 outs() << " baseProperties "
4420 << format("0x%" PRIx32, cro.baseProperties) << "\n";
4421 if (cro.baseProperties != 0)
4422 print_objc_property_list32(cro.baseProperties, info);
4423 is_meta_class = (cro.flags & RO_META) ? true : false;
4426 static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
4429 uint32_t offset, left;
4432 uint64_t isa_n_value, n_value;
4434 r = get_pointer_64(p, offset, left, S, info);
4435 if (r == nullptr || left < sizeof(struct class64_t))
4437 memset(&c, '\0', sizeof(struct class64_t));
4438 if (left < sizeof(struct class64_t)) {
4439 memcpy(&c, r, left);
4440 outs() << " (class_t entends past the end of the section)\n";
4442 memcpy(&c, r, sizeof(struct class64_t));
4443 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4446 outs() << " isa " << format("0x%" PRIx64, c.isa);
4447 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
4448 isa_n_value, c.isa);
4449 if (name != nullptr)
4450 outs() << " " << name;
4453 outs() << " superclass " << format("0x%" PRIx64, c.superclass);
4454 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
4455 n_value, c.superclass);
4456 if (name != nullptr)
4457 outs() << " " << name;
4460 outs() << " cache " << format("0x%" PRIx64, c.cache);
4461 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
4463 if (name != nullptr)
4464 outs() << " " << name;
4467 outs() << " vtable " << format("0x%" PRIx64, c.vtable);
4468 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
4470 if (name != nullptr)
4471 outs() << " " << name;
4474 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
4478 if (info->verbose && name != nullptr)
4481 outs() << format("0x%" PRIx64, n_value);
4483 outs() << " + " << format("0x%" PRIx64, c.data);
4485 outs() << format("0x%" PRIx64, c.data);
4486 outs() << " (struct class_ro_t *)";
4488 // This is a Swift class if some of the low bits of the pointer are set.
4489 if ((c.data + n_value) & 0x7)
4490 outs() << " Swift class";
4493 print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class);
4495 if (is_meta_class == false) {
4496 outs() << "Meta Class\n";
4497 print_class64_t(c.isa + isa_n_value, info);
4501 static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
4504 uint32_t offset, left;
4508 r = get_pointer_32(p, offset, left, S, info);
4511 memset(&c, '\0', sizeof(struct class32_t));
4512 if (left < sizeof(struct class32_t)) {
4513 memcpy(&c, r, left);
4514 outs() << " (class_t entends past the end of the section)\n";
4516 memcpy(&c, r, sizeof(struct class32_t));
4517 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4520 outs() << " isa " << format("0x%" PRIx32, c.isa);
4522 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
4523 if (name != nullptr)
4524 outs() << " " << name;
4527 outs() << " superclass " << format("0x%" PRIx32, c.superclass);
4528 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
4530 if (name != nullptr)
4531 outs() << " " << name;
4534 outs() << " cache " << format("0x%" PRIx32, c.cache);
4535 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
4537 if (name != nullptr)
4538 outs() << " " << name;
4541 outs() << " vtable " << format("0x%" PRIx32, c.vtable);
4542 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
4544 if (name != nullptr)
4545 outs() << " " << name;
4549 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
4550 outs() << " data " << format("0x%" PRIx32, c.data)
4551 << " (struct class_ro_t *)";
4553 // This is a Swift class if some of the low bits of the pointer are set.
4555 outs() << " Swift class";
4558 print_class_ro32_t(c.data & ~0x3, info, is_meta_class);
4560 if (is_meta_class == false) {
4561 outs() << "Meta Class\n";
4562 print_class32_t(c.isa, info);
4566 static void print_objc_class_t(struct objc_class_t *objc_class,
4567 struct DisassembleInfo *info) {
4568 uint32_t offset, left, xleft;
4569 const char *name, *p, *ivar_list;
4572 struct objc_ivar_list_t objc_ivar_list;
4573 struct objc_ivar_t ivar;
4575 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
4576 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
4577 name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
4578 if (name != nullptr)
4579 outs() << format(" %.*s", left, name);
4581 outs() << " (not in an __OBJC section)";
4585 outs() << "\t super_class "
4586 << format("0x%08" PRIx32, objc_class->super_class);
4587 if (info->verbose) {
4588 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
4589 if (name != nullptr)
4590 outs() << format(" %.*s", left, name);
4592 outs() << " (not in an __OBJC section)";
4596 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name);
4597 if (info->verbose) {
4598 name = get_pointer_32(objc_class->name, offset, left, S, info, true);
4599 if (name != nullptr)
4600 outs() << format(" %.*s", left, name);
4602 outs() << " (not in an __OBJC section)";
4606 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
4609 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
4610 if (info->verbose) {
4611 if (CLS_GETINFO(objc_class, CLS_CLASS))
4612 outs() << " CLS_CLASS";
4613 else if (CLS_GETINFO(objc_class, CLS_META))
4614 outs() << " CLS_META";
4618 outs() << "\t instance_size "
4619 << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
4621 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
4622 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
4624 if (left > sizeof(struct objc_ivar_list_t)) {
4626 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
4628 outs() << " (entends past the end of the section)\n";
4629 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
4630 memcpy(&objc_ivar_list, p, left);
4632 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4633 swapStruct(objc_ivar_list);
4634 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
4635 ivar_list = p + sizeof(struct objc_ivar_list_t);
4636 for (i = 0; i < objc_ivar_list.ivar_count; i++) {
4637 if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
4638 outs() << "\t\t remaining ivar's extend past the of the section\n";
4641 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
4642 sizeof(struct objc_ivar_t));
4643 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4646 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
4647 if (info->verbose) {
4648 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
4649 if (name != nullptr)
4650 outs() << format(" %.*s", xleft, name);
4652 outs() << " (not in an __OBJC section)";
4656 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
4657 if (info->verbose) {
4658 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
4659 if (name != nullptr)
4660 outs() << format(" %.*s", xleft, name);
4662 outs() << " (not in an __OBJC section)";
4666 outs() << "\t\t ivar_offset "
4667 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
4670 outs() << " (not in an __OBJC section)\n";
4673 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
4674 if (print_method_list(objc_class->methodLists, info))
4675 outs() << " (not in an __OBJC section)\n";
4677 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
4680 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
4681 if (print_protocol_list(objc_class->protocols, 16, info))
4682 outs() << " (not in an __OBJC section)\n";
4685 static void print_objc_objc_category_t(struct objc_category_t *objc_category,
4686 struct DisassembleInfo *info) {
4687 uint32_t offset, left;
4691 outs() << "\t category name "
4692 << format("0x%08" PRIx32, objc_category->category_name);
4693 if (info->verbose) {
4694 name = get_pointer_32(objc_category->category_name, offset, left, S, info,
4696 if (name != nullptr)
4697 outs() << format(" %.*s", left, name);
4699 outs() << " (not in an __OBJC section)";
4703 outs() << "\t\t class name "
4704 << format("0x%08" PRIx32, objc_category->class_name);
4705 if (info->verbose) {
4707 get_pointer_32(objc_category->class_name, offset, left, S, info, true);
4708 if (name != nullptr)
4709 outs() << format(" %.*s", left, name);
4711 outs() << " (not in an __OBJC section)";
4715 outs() << "\t instance methods "
4716 << format("0x%08" PRIx32, objc_category->instance_methods);
4717 if (print_method_list(objc_category->instance_methods, info))
4718 outs() << " (not in an __OBJC section)\n";
4720 outs() << "\t class methods "
4721 << format("0x%08" PRIx32, objc_category->class_methods);
4722 if (print_method_list(objc_category->class_methods, info))
4723 outs() << " (not in an __OBJC section)\n";
4726 static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
4727 struct category64_t c;
4729 uint32_t offset, xoffset, left;
4731 const char *name, *sym_name;
4734 r = get_pointer_64(p, offset, left, S, info);
4737 memset(&c, '\0', sizeof(struct category64_t));
4738 if (left < sizeof(struct category64_t)) {
4739 memcpy(&c, r, left);
4740 outs() << " (category_t entends past the end of the section)\n";
4742 memcpy(&c, r, sizeof(struct category64_t));
4743 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4747 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
4748 info, n_value, c.name);
4750 if (info->verbose && sym_name != nullptr)
4753 outs() << format("0x%" PRIx64, n_value);
4755 outs() << " + " << format("0x%" PRIx64, c.name);
4757 outs() << format("0x%" PRIx64, c.name);
4758 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
4759 if (name != nullptr)
4760 outs() << format(" %.*s", left, name);
4764 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
4767 if (info->verbose && sym_name != nullptr)
4770 outs() << format("0x%" PRIx64, n_value);
4772 outs() << " + " << format("0x%" PRIx64, c.cls);
4774 outs() << format("0x%" PRIx64, c.cls);
4776 if (c.cls + n_value != 0)
4777 print_class64_t(c.cls + n_value, info);
4779 outs() << " instanceMethods ";
4781 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
4782 info, n_value, c.instanceMethods);
4784 if (info->verbose && sym_name != nullptr)
4787 outs() << format("0x%" PRIx64, n_value);
4788 if (c.instanceMethods != 0)
4789 outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
4791 outs() << format("0x%" PRIx64, c.instanceMethods);
4793 if (c.instanceMethods + n_value != 0)
4794 print_method_list64_t(c.instanceMethods + n_value, info, "");
4796 outs() << " classMethods ";
4797 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
4798 S, info, n_value, c.classMethods);
4800 if (info->verbose && sym_name != nullptr)
4803 outs() << format("0x%" PRIx64, n_value);
4804 if (c.classMethods != 0)
4805 outs() << " + " << format("0x%" PRIx64, c.classMethods);
4807 outs() << format("0x%" PRIx64, c.classMethods);
4809 if (c.classMethods + n_value != 0)
4810 print_method_list64_t(c.classMethods + n_value, info, "");
4812 outs() << " protocols ";
4813 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
4814 info, n_value, c.protocols);
4816 if (info->verbose && sym_name != nullptr)
4819 outs() << format("0x%" PRIx64, n_value);
4820 if (c.protocols != 0)
4821 outs() << " + " << format("0x%" PRIx64, c.protocols);
4823 outs() << format("0x%" PRIx64, c.protocols);
4825 if (c.protocols + n_value != 0)
4826 print_protocol_list64_t(c.protocols + n_value, info);
4828 outs() << "instanceProperties ";
4830 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
4831 S, info, n_value, c.instanceProperties);
4833 if (info->verbose && sym_name != nullptr)
4836 outs() << format("0x%" PRIx64, n_value);
4837 if (c.instanceProperties != 0)
4838 outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
4840 outs() << format("0x%" PRIx64, c.instanceProperties);
4842 if (c.instanceProperties + n_value != 0)
4843 print_objc_property_list64(c.instanceProperties + n_value, info);
4846 static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
4847 struct category32_t c;
4849 uint32_t offset, left;
4853 r = get_pointer_32(p, offset, left, S, info);
4856 memset(&c, '\0', sizeof(struct category32_t));
4857 if (left < sizeof(struct category32_t)) {
4858 memcpy(&c, r, left);
4859 outs() << " (category_t entends past the end of the section)\n";
4861 memcpy(&c, r, sizeof(struct category32_t));
4862 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4865 outs() << " name " << format("0x%" PRIx32, c.name);
4866 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
4869 outs() << " " << name;
4872 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
4874 print_class32_t(c.cls, info);
4875 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
4877 if (c.instanceMethods != 0)
4878 print_method_list32_t(c.instanceMethods, info, "");
4879 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
4881 if (c.classMethods != 0)
4882 print_method_list32_t(c.classMethods, info, "");
4883 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
4884 if (c.protocols != 0)
4885 print_protocol_list32_t(c.protocols, info);
4886 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
4888 if (c.instanceProperties != 0)
4889 print_objc_property_list32(c.instanceProperties, info);
4892 static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
4893 uint32_t i, left, offset, xoffset;
4894 uint64_t p, n_value;
4895 struct message_ref64 mr;
4896 const char *name, *sym_name;
4900 if (S == SectionRef())
4904 S.getName(SectName);
4905 DataRefImpl Ref = S.getRawDataRefImpl();
4906 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4907 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4909 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
4910 p = S.getAddress() + i;
4911 r = get_pointer_64(p, offset, left, S, info);
4914 memset(&mr, '\0', sizeof(struct message_ref64));
4915 if (left < sizeof(struct message_ref64)) {
4916 memcpy(&mr, r, left);
4917 outs() << " (message_ref entends past the end of the section)\n";
4919 memcpy(&mr, r, sizeof(struct message_ref64));
4920 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4924 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
4927 outs() << format("0x%" PRIx64, n_value) << " ";
4929 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
4931 outs() << format("0x%" PRIx64, mr.imp) << " ";
4932 if (name != nullptr)
4933 outs() << " " << name;
4937 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
4938 info, n_value, mr.sel);
4940 if (info->verbose && sym_name != nullptr)
4943 outs() << format("0x%" PRIx64, n_value);
4945 outs() << " + " << format("0x%" PRIx64, mr.sel);
4947 outs() << format("0x%" PRIx64, mr.sel);
4948 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
4949 if (name != nullptr)
4950 outs() << format(" %.*s", left, name);
4953 offset += sizeof(struct message_ref64);
4957 static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
4958 uint32_t i, left, offset, xoffset, p;
4959 struct message_ref32 mr;
4960 const char *name, *r;
4963 if (S == SectionRef())
4967 S.getName(SectName);
4968 DataRefImpl Ref = S.getRawDataRefImpl();
4969 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4970 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4972 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
4973 p = S.getAddress() + i;
4974 r = get_pointer_32(p, offset, left, S, info);
4977 memset(&mr, '\0', sizeof(struct message_ref32));
4978 if (left < sizeof(struct message_ref32)) {
4979 memcpy(&mr, r, left);
4980 outs() << " (message_ref entends past the end of the section)\n";
4982 memcpy(&mr, r, sizeof(struct message_ref32));
4983 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4986 outs() << " imp " << format("0x%" PRIx32, mr.imp);
4987 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
4989 if (name != nullptr)
4990 outs() << " " << name;
4993 outs() << " sel " << format("0x%" PRIx32, mr.sel);
4994 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
4995 if (name != nullptr)
4996 outs() << " " << name;
4999 offset += sizeof(struct message_ref32);
5003 static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
5004 uint32_t left, offset, swift_version;
5006 struct objc_image_info64 o;
5010 S.getName(SectName);
5011 DataRefImpl Ref = S.getRawDataRefImpl();
5012 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5013 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5015 r = get_pointer_64(p, offset, left, S, info);
5018 memset(&o, '\0', sizeof(struct objc_image_info64));
5019 if (left < sizeof(struct objc_image_info64)) {
5020 memcpy(&o, r, left);
5021 outs() << " (objc_image_info entends past the end of the section)\n";
5023 memcpy(&o, r, sizeof(struct objc_image_info64));
5024 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5026 outs() << " version " << o.version << "\n";
5027 outs() << " flags " << format("0x%" PRIx32, o.flags);
5028 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5029 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5030 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5031 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5032 swift_version = (o.flags >> 8) & 0xff;
5033 if (swift_version != 0) {
5034 if (swift_version == 1)
5035 outs() << " Swift 1.0";
5036 else if (swift_version == 2)
5037 outs() << " Swift 1.1";
5039 outs() << " unknown future Swift version (" << swift_version << ")";
5044 static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5045 uint32_t left, offset, swift_version, p;
5046 struct objc_image_info32 o;
5050 S.getName(SectName);
5051 DataRefImpl Ref = S.getRawDataRefImpl();
5052 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5053 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5055 r = get_pointer_32(p, offset, left, S, info);
5058 memset(&o, '\0', sizeof(struct objc_image_info32));
5059 if (left < sizeof(struct objc_image_info32)) {
5060 memcpy(&o, r, left);
5061 outs() << " (objc_image_info entends past the end of the section)\n";
5063 memcpy(&o, r, sizeof(struct objc_image_info32));
5064 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5066 outs() << " version " << o.version << "\n";
5067 outs() << " flags " << format("0x%" PRIx32, o.flags);
5068 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5069 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5070 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5071 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5072 swift_version = (o.flags >> 8) & 0xff;
5073 if (swift_version != 0) {
5074 if (swift_version == 1)
5075 outs() << " Swift 1.0";
5076 else if (swift_version == 2)
5077 outs() << " Swift 1.1";
5079 outs() << " unknown future Swift version (" << swift_version << ")";
5084 static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5085 uint32_t left, offset, p;
5086 struct imageInfo_t o;
5090 S.getName(SectName);
5091 DataRefImpl Ref = S.getRawDataRefImpl();
5092 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5093 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5095 r = get_pointer_32(p, offset, left, S, info);
5098 memset(&o, '\0', sizeof(struct imageInfo_t));
5099 if (left < sizeof(struct imageInfo_t)) {
5100 memcpy(&o, r, left);
5101 outs() << " (imageInfo entends past the end of the section)\n";
5103 memcpy(&o, r, sizeof(struct imageInfo_t));
5104 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5106 outs() << " version " << o.version << "\n";
5107 outs() << " flags " << format("0x%" PRIx32, o.flags);
5113 outs() << " GC-only";
5119 static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
5120 SymbolAddressMap AddrMap;
5122 CreateSymbolAddressMap(O, &AddrMap);
5124 std::vector<SectionRef> Sections;
5125 for (const SectionRef &Section : O->sections()) {
5127 Section.getName(SectName);
5128 Sections.push_back(Section);
5131 struct DisassembleInfo info;
5132 // Set up the block of info used by the Symbolizer call backs.
5133 info.verbose = verbose;
5135 info.AddrMap = &AddrMap;
5136 info.Sections = &Sections;
5137 info.class_name = nullptr;
5138 info.selector_name = nullptr;
5139 info.method = nullptr;
5140 info.demangled_name = nullptr;
5141 info.bindtable = nullptr;
5145 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5146 if (CL != SectionRef()) {
5148 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5150 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5152 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5155 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5156 if (CR != SectionRef()) {
5158 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5160 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5162 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5165 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5166 if (SR != SectionRef()) {
5168 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5170 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5172 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5175 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5176 if (CA != SectionRef()) {
5178 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5180 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5182 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5185 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5186 if (PL != SectionRef()) {
5188 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5190 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5192 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5195 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5196 if (MR != SectionRef()) {
5198 print_message_refs64(MR, &info);
5200 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5202 print_message_refs64(MR, &info);
5205 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5206 if (II != SectionRef()) {
5208 print_image_info64(II, &info);
5210 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5212 print_image_info64(II, &info);
5215 if (info.bindtable != nullptr)
5216 delete info.bindtable;
5219 static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
5220 SymbolAddressMap AddrMap;
5222 CreateSymbolAddressMap(O, &AddrMap);
5224 std::vector<SectionRef> Sections;
5225 for (const SectionRef &Section : O->sections()) {
5227 Section.getName(SectName);
5228 Sections.push_back(Section);
5231 struct DisassembleInfo info;
5232 // Set up the block of info used by the Symbolizer call backs.
5233 info.verbose = verbose;
5235 info.AddrMap = &AddrMap;
5236 info.Sections = &Sections;
5237 info.class_name = nullptr;
5238 info.selector_name = nullptr;
5239 info.method = nullptr;
5240 info.demangled_name = nullptr;
5241 info.bindtable = nullptr;
5245 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5246 if (CL != SectionRef()) {
5248 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5250 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5252 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5255 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5256 if (CR != SectionRef()) {
5258 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5260 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5262 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5265 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5266 if (SR != SectionRef()) {
5268 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5270 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5272 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5275 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5276 if (CA != SectionRef()) {
5278 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5280 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5282 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5285 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5286 if (PL != SectionRef()) {
5288 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5290 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5292 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5295 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5296 if (MR != SectionRef()) {
5298 print_message_refs32(MR, &info);
5300 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5302 print_message_refs32(MR, &info);
5305 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5306 if (II != SectionRef()) {
5308 print_image_info32(II, &info);
5310 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5312 print_image_info32(II, &info);
5316 static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
5317 uint32_t i, j, p, offset, xoffset, left, defs_left, def;
5318 const char *r, *name, *defs;
5319 struct objc_module_t module;
5321 struct objc_symtab_t symtab;
5322 struct objc_class_t objc_class;
5323 struct objc_category_t objc_category;
5325 outs() << "Objective-C segment\n";
5326 S = get_section(O, "__OBJC", "__module_info");
5327 if (S == SectionRef())
5330 SymbolAddressMap AddrMap;
5332 CreateSymbolAddressMap(O, &AddrMap);
5334 std::vector<SectionRef> Sections;
5335 for (const SectionRef &Section : O->sections()) {
5337 Section.getName(SectName);
5338 Sections.push_back(Section);
5341 struct DisassembleInfo info;
5342 // Set up the block of info used by the Symbolizer call backs.
5343 info.verbose = verbose;
5345 info.AddrMap = &AddrMap;
5346 info.Sections = &Sections;
5347 info.class_name = nullptr;
5348 info.selector_name = nullptr;
5349 info.method = nullptr;
5350 info.demangled_name = nullptr;
5351 info.bindtable = nullptr;
5355 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
5356 p = S.getAddress() + i;
5357 r = get_pointer_32(p, offset, left, S, &info, true);
5360 memset(&module, '\0', sizeof(struct objc_module_t));
5361 if (left < sizeof(struct objc_module_t)) {
5362 memcpy(&module, r, left);
5363 outs() << " (module extends past end of __module_info section)\n";
5365 memcpy(&module, r, sizeof(struct objc_module_t));
5366 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5369 outs() << "Module " << format("0x%" PRIx32, p) << "\n";
5370 outs() << " version " << module.version << "\n";
5371 outs() << " size " << module.size << "\n";
5373 name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
5374 if (name != nullptr)
5375 outs() << format("%.*s", left, name);
5377 outs() << format("0x%08" PRIx32, module.name)
5378 << "(not in an __OBJC section)";
5381 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
5382 if (module.symtab == 0 || r == nullptr) {
5383 outs() << " symtab " << format("0x%08" PRIx32, module.symtab)
5384 << " (not in an __OBJC section)\n";
5387 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
5388 memset(&symtab, '\0', sizeof(struct objc_symtab_t));
5391 if (left < sizeof(struct objc_symtab_t)) {
5392 memcpy(&symtab, r, left);
5393 outs() << "\tsymtab extends past end of an __OBJC section)\n";
5395 memcpy(&symtab, r, sizeof(struct objc_symtab_t));
5396 if (left > sizeof(struct objc_symtab_t)) {
5397 defs_left = left - sizeof(struct objc_symtab_t);
5398 defs = r + sizeof(struct objc_symtab_t);
5401 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5404 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
5405 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
5406 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
5408 outs() << " (not in an __OBJC section)";
5410 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
5411 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
5412 if (symtab.cls_def_cnt > 0)
5413 outs() << "\tClass Definitions\n";
5414 for (j = 0; j < symtab.cls_def_cnt; j++) {
5415 if ((j + 1) * sizeof(uint32_t) > defs_left) {
5416 outs() << "\t(remaining class defs entries entends past the end of the "
5420 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
5421 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5422 sys::swapByteOrder(def);
5424 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5425 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
5427 if (left > sizeof(struct objc_class_t)) {
5429 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5431 outs() << " (entends past the end of the section)\n";
5432 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5433 memcpy(&objc_class, r, left);
5435 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5436 swapStruct(objc_class);
5437 print_objc_class_t(&objc_class, &info);
5439 outs() << "(not in an __OBJC section)\n";
5442 if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
5443 outs() << "\tMeta Class";
5444 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
5446 if (left > sizeof(struct objc_class_t)) {
5448 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5450 outs() << " (entends past the end of the section)\n";
5451 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5452 memcpy(&objc_class, r, left);
5454 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5455 swapStruct(objc_class);
5456 print_objc_class_t(&objc_class, &info);
5458 outs() << "(not in an __OBJC section)\n";
5462 if (symtab.cat_def_cnt > 0)
5463 outs() << "\tCategory Definitions\n";
5464 for (j = 0; j < symtab.cat_def_cnt; j++) {
5465 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
5466 outs() << "\t(remaining category defs entries entends past the end of "
5467 << "the section)\n";
5470 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
5472 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5473 sys::swapByteOrder(def);
5475 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5476 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
5477 << format("0x%08" PRIx32, def);
5479 if (left > sizeof(struct objc_category_t)) {
5481 memcpy(&objc_category, r, sizeof(struct objc_category_t));
5483 outs() << " (entends past the end of the section)\n";
5484 memset(&objc_category, '\0', sizeof(struct objc_category_t));
5485 memcpy(&objc_category, r, left);
5487 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5488 swapStruct(objc_category);
5489 print_objc_objc_category_t(&objc_category, &info);
5491 outs() << "(not in an __OBJC section)\n";
5495 const SectionRef II = get_section(O, "__OBJC", "__image_info");
5496 if (II != SectionRef())
5497 print_image_info(II, &info);
5502 static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
5503 uint32_t size, uint32_t addr) {
5504 SymbolAddressMap AddrMap;
5505 CreateSymbolAddressMap(O, &AddrMap);
5507 std::vector<SectionRef> Sections;
5508 for (const SectionRef &Section : O->sections()) {
5510 Section.getName(SectName);
5511 Sections.push_back(Section);
5514 struct DisassembleInfo info;
5515 // Set up the block of info used by the Symbolizer call backs.
5516 info.verbose = true;
5518 info.AddrMap = &AddrMap;
5519 info.Sections = &Sections;
5520 info.class_name = nullptr;
5521 info.selector_name = nullptr;
5522 info.method = nullptr;
5523 info.demangled_name = nullptr;
5524 info.bindtable = nullptr;
5529 struct objc_protocol_t protocol;
5530 uint32_t left, paddr;
5531 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
5532 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
5533 left = size - (p - sect);
5534 if (left < sizeof(struct objc_protocol_t)) {
5535 outs() << "Protocol extends past end of __protocol section\n";
5536 memcpy(&protocol, p, left);
5538 memcpy(&protocol, p, sizeof(struct objc_protocol_t));
5539 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5540 swapStruct(protocol);
5541 paddr = addr + (p - sect);
5542 outs() << "Protocol " << format("0x%" PRIx32, paddr);
5543 if (print_protocol(paddr, 0, &info))
5544 outs() << "(not in an __OBJC section)\n";
5548 static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
5550 printObjc2_64bit_MetaData(O, verbose);
5552 MachO::mach_header H;
5554 if (H.cputype == MachO::CPU_TYPE_ARM)
5555 printObjc2_32bit_MetaData(O, verbose);
5557 // This is the 32-bit non-arm cputype case. Which is normally
5558 // the first Objective-C ABI. But it may be the case of a
5559 // binary for the iOS simulator which is the second Objective-C
5560 // ABI. In that case printObjc1_32bit_MetaData() will determine that
5561 // and return false.
5562 if (printObjc1_32bit_MetaData(O, verbose) == false)
5563 printObjc2_32bit_MetaData(O, verbose);
5568 // GuessLiteralPointer returns a string which for the item in the Mach-O file
5569 // for the address passed in as ReferenceValue for printing as a comment with
5570 // the instruction and also returns the corresponding type of that item
5571 // indirectly through ReferenceType.
5573 // If ReferenceValue is an address of literal cstring then a pointer to the
5574 // cstring is returned and ReferenceType is set to
5575 // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
5577 // If ReferenceValue is an address of an Objective-C CFString, Selector ref or
5578 // Class ref that name is returned and the ReferenceType is set accordingly.
5580 // Lastly, literals which are Symbol address in a literal pool are looked for
5581 // and if found the symbol name is returned and ReferenceType is set to
5582 // LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
5584 // If there is no item in the Mach-O file for the address passed in as
5585 // ReferenceValue nullptr is returned and ReferenceType is unchanged.
5586 static const char *GuessLiteralPointer(uint64_t ReferenceValue,
5587 uint64_t ReferencePC,
5588 uint64_t *ReferenceType,
5589 struct DisassembleInfo *info) {
5590 // First see if there is an external relocation entry at the ReferencePC.
5591 uint64_t sect_addr = info->S.getAddress();
5592 uint64_t sect_offset = ReferencePC - sect_addr;
5593 bool reloc_found = false;
5595 MachO::any_relocation_info RE;
5596 bool isExtern = false;
5598 for (const RelocationRef &Reloc : info->S.relocations()) {
5599 uint64_t RelocOffset = Reloc.getOffset();
5600 if (RelocOffset == sect_offset) {
5601 Rel = Reloc.getRawDataRefImpl();
5602 RE = info->O->getRelocation(Rel);
5603 if (info->O->isRelocationScattered(RE))
5605 isExtern = info->O->getPlainRelocationExternal(RE);
5607 symbol_iterator RelocSym = Reloc.getSymbol();
5614 // If there is an external relocation entry for a symbol in a section
5615 // then used that symbol's value for the value of the reference.
5616 if (reloc_found && isExtern) {
5617 if (info->O->getAnyRelocationPCRel(RE)) {
5618 unsigned Type = info->O->getAnyRelocationType(RE);
5619 if (Type == MachO::X86_64_RELOC_SIGNED) {
5620 ReferenceValue = Symbol.getValue();
5625 // Look for literals such as Objective-C CFStrings refs, Selector refs,
5626 // Message refs and Class refs.
5627 bool classref, selref, msgref, cfstring;
5628 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
5629 selref, msgref, cfstring);
5630 if (classref && pointer_value == 0) {
5631 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
5632 // And the pointer_value in that section is typically zero as it will be
5633 // set by dyld as part of the "bind information".
5634 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
5635 if (name != nullptr) {
5636 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
5637 const char *class_name = strrchr(name, '$');
5638 if (class_name != nullptr && class_name[1] == '_' &&
5639 class_name[2] != '\0') {
5640 info->class_name = class_name + 2;
5647 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
5649 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
5650 if (name != nullptr)
5651 info->class_name = name;
5653 name = "bad class ref";
5658 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
5659 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
5663 if (selref && pointer_value == 0)
5664 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
5666 if (pointer_value != 0)
5667 ReferenceValue = pointer_value;
5669 const char *name = GuessCstringPointer(ReferenceValue, info);
5671 if (pointer_value != 0 && selref) {
5672 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
5673 info->selector_name = name;
5674 } else if (pointer_value != 0 && msgref) {
5675 info->class_name = nullptr;
5676 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
5677 info->selector_name = name;
5679 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
5683 // Lastly look for an indirect symbol with this ReferenceValue which is in
5684 // a literal pool. If found return that symbol name.
5685 name = GuessIndirectSymbol(ReferenceValue, info);
5687 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
5694 // SymbolizerSymbolLookUp is the symbol lookup function passed when creating
5695 // the Symbolizer. It looks up the ReferenceValue using the info passed via the
5696 // pointer to the struct DisassembleInfo that was passed when MCSymbolizer
5697 // is created and returns the symbol name that matches the ReferenceValue or
5698 // nullptr if none. The ReferenceType is passed in for the IN type of
5699 // reference the instruction is making from the values in defined in the header
5700 // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
5701 // Out type and the ReferenceName will also be set which is added as a comment
5702 // to the disassembled instruction.
5705 // If the symbol name is a C++ mangled name then the demangled name is
5706 // returned through ReferenceName and ReferenceType is set to
5707 // LLVMDisassembler_ReferenceType_DeMangled_Name .
5710 // When this is called to get a symbol name for a branch target then the
5711 // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
5712 // SymbolValue will be looked for in the indirect symbol table to determine if
5713 // it is an address for a symbol stub. If so then the symbol name for that
5714 // stub is returned indirectly through ReferenceName and then ReferenceType is
5715 // set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
5717 // When this is called with an value loaded via a PC relative load then
5718 // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
5719 // SymbolValue is checked to be an address of literal pointer, symbol pointer,
5720 // or an Objective-C meta data reference. If so the output ReferenceType is
5721 // set to correspond to that as well as setting the ReferenceName.
5722 static const char *SymbolizerSymbolLookUp(void *DisInfo,
5723 uint64_t ReferenceValue,
5724 uint64_t *ReferenceType,
5725 uint64_t ReferencePC,
5726 const char **ReferenceName) {
5727 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
5728 // If no verbose symbolic information is wanted then just return nullptr.
5729 if (!info->verbose) {
5730 *ReferenceName = nullptr;
5731 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5735 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
5737 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
5738 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
5739 if (*ReferenceName != nullptr) {
5740 method_reference(info, ReferenceType, ReferenceName);
5741 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
5742 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
5745 if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
5746 if (info->demangled_name != nullptr)
5747 free(info->demangled_name);
5749 info->demangled_name =
5750 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
5751 if (info->demangled_name != nullptr) {
5752 *ReferenceName = info->demangled_name;
5753 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5755 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5758 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5759 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
5761 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5763 method_reference(info, ReferenceType, ReferenceName);
5765 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5766 // If this is arm64 and the reference is an adrp instruction save the
5767 // instruction, passed in ReferenceValue and the address of the instruction
5768 // for use later if we see and add immediate instruction.
5769 } else if (info->O->getArch() == Triple::aarch64 &&
5770 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
5771 info->adrp_inst = ReferenceValue;
5772 info->adrp_addr = ReferencePC;
5773 SymbolName = nullptr;
5774 *ReferenceName = nullptr;
5775 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5776 // If this is arm64 and reference is an add immediate instruction and we
5778 // seen an adrp instruction just before it and the adrp's Xd register
5780 // this add's Xn register reconstruct the value being referenced and look to
5781 // see if it is a literal pointer. Note the add immediate instruction is
5782 // passed in ReferenceValue.
5783 } else if (info->O->getArch() == Triple::aarch64 &&
5784 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
5785 ReferencePC - 4 == info->adrp_addr &&
5786 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5787 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5788 uint32_t addxri_inst;
5789 uint64_t adrp_imm, addxri_imm;
5792 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5793 if (info->adrp_inst & 0x0200000)
5794 adrp_imm |= 0xfffffffffc000000LL;
5796 addxri_inst = ReferenceValue;
5797 addxri_imm = (addxri_inst >> 10) & 0xfff;
5798 if (((addxri_inst >> 22) & 0x3) == 1)
5801 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5802 (adrp_imm << 12) + addxri_imm;
5805 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5806 if (*ReferenceName == nullptr)
5807 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5808 // If this is arm64 and the reference is a load register instruction and we
5809 // have seen an adrp instruction just before it and the adrp's Xd register
5810 // matches this add's Xn register reconstruct the value being referenced and
5811 // look to see if it is a literal pointer. Note the load register
5812 // instruction is passed in ReferenceValue.
5813 } else if (info->O->getArch() == Triple::aarch64 &&
5814 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
5815 ReferencePC - 4 == info->adrp_addr &&
5816 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5817 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5818 uint32_t ldrxui_inst;
5819 uint64_t adrp_imm, ldrxui_imm;
5822 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5823 if (info->adrp_inst & 0x0200000)
5824 adrp_imm |= 0xfffffffffc000000LL;
5826 ldrxui_inst = ReferenceValue;
5827 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
5829 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5830 (adrp_imm << 12) + (ldrxui_imm << 3);
5833 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5834 if (*ReferenceName == nullptr)
5835 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5837 // If this arm64 and is an load register (PC-relative) instruction the
5838 // ReferenceValue is the PC plus the immediate value.
5839 else if (info->O->getArch() == Triple::aarch64 &&
5840 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
5841 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
5843 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5844 if (*ReferenceName == nullptr)
5845 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5848 else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
5849 if (info->demangled_name != nullptr)
5850 free(info->demangled_name);
5852 info->demangled_name =
5853 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
5854 if (info->demangled_name != nullptr) {
5855 *ReferenceName = info->demangled_name;
5856 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5861 *ReferenceName = nullptr;
5862 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5868 /// \brief Emits the comments that are stored in the CommentStream.
5869 /// Each comment in the CommentStream must end with a newline.
5870 static void emitComments(raw_svector_ostream &CommentStream,
5871 SmallString<128> &CommentsToEmit,
5872 formatted_raw_ostream &FormattedOS,
5873 const MCAsmInfo &MAI) {
5874 // Flush the stream before taking its content.
5875 CommentStream.flush();
5876 StringRef Comments = CommentsToEmit.str();
5877 // Get the default information for printing a comment.
5878 const char *CommentBegin = MAI.getCommentString();
5879 unsigned CommentColumn = MAI.getCommentColumn();
5880 bool IsFirst = true;
5881 while (!Comments.empty()) {
5883 FormattedOS << '\n';
5884 // Emit a line of comments.
5885 FormattedOS.PadToColumn(CommentColumn);
5886 size_t Position = Comments.find('\n');
5887 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
5888 // Move after the newline character.
5889 Comments = Comments.substr(Position + 1);
5892 FormattedOS.flush();
5894 // Tell the comment stream that the vector changed underneath it.
5895 CommentsToEmit.clear();
5896 CommentStream.resync();
5899 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
5900 StringRef DisSegName, StringRef DisSectName) {
5901 const char *McpuDefault = nullptr;
5902 const Target *ThumbTarget = nullptr;
5903 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
5905 // GetTarget prints out stuff.
5908 if (MCPU.empty() && McpuDefault)
5911 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
5912 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
5914 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
5916 // Package up features to be passed to target/subtarget
5917 std::string FeaturesStr;
5918 if (MAttrs.size()) {
5919 SubtargetFeatures Features;
5920 for (unsigned i = 0; i != MAttrs.size(); ++i)
5921 Features.AddFeature(MAttrs[i]);
5922 FeaturesStr = Features.getString();
5925 // Set up disassembler.
5926 std::unique_ptr<const MCRegisterInfo> MRI(
5927 TheTarget->createMCRegInfo(TripleName));
5928 std::unique_ptr<const MCAsmInfo> AsmInfo(
5929 TheTarget->createMCAsmInfo(*MRI, TripleName));
5930 std::unique_ptr<const MCSubtargetInfo> STI(
5931 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
5932 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
5933 std::unique_ptr<MCDisassembler> DisAsm(
5934 TheTarget->createMCDisassembler(*STI, Ctx));
5935 std::unique_ptr<MCSymbolizer> Symbolizer;
5936 struct DisassembleInfo SymbolizerInfo;
5937 std::unique_ptr<MCRelocationInfo> RelInfo(
5938 TheTarget->createMCRelocationInfo(TripleName, Ctx));
5940 Symbolizer.reset(TheTarget->createMCSymbolizer(
5941 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
5942 &SymbolizerInfo, &Ctx, std::move(RelInfo)));
5943 DisAsm->setSymbolizer(std::move(Symbolizer));
5945 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
5946 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
5947 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
5948 // Set the display preference for hex vs. decimal immediates.
5949 IP->setPrintImmHex(PrintImmHex);
5950 // Comment stream and backing vector.
5951 SmallString<128> CommentsToEmit;
5952 raw_svector_ostream CommentStream(CommentsToEmit);
5953 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
5954 // if it is done then arm64 comments for string literals don't get printed
5955 // and some constant get printed instead and not setting it causes intel
5956 // (32-bit and 64-bit) comments printed with different spacing before the
5957 // comment causing different diffs with the 'C' disassembler library API.
5958 // IP->setCommentStream(CommentStream);
5960 if (!AsmInfo || !STI || !DisAsm || !IP) {
5961 errs() << "error: couldn't initialize disassembler for target "
5962 << TripleName << '\n';
5966 // Set up thumb disassembler.
5967 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
5968 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
5969 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
5970 std::unique_ptr<MCDisassembler> ThumbDisAsm;
5971 std::unique_ptr<MCInstPrinter> ThumbIP;
5972 std::unique_ptr<MCContext> ThumbCtx;
5973 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
5974 struct DisassembleInfo ThumbSymbolizerInfo;
5975 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
5977 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
5979 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
5981 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr));
5982 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
5983 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
5984 MCContext *PtrThumbCtx = ThumbCtx.get();
5986 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
5988 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
5989 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
5990 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
5991 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
5993 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
5994 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
5995 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
5996 *ThumbInstrInfo, *ThumbMRI));
5997 // Set the display preference for hex vs. decimal immediates.
5998 ThumbIP->setPrintImmHex(PrintImmHex);
6001 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
6002 errs() << "error: couldn't initialize disassembler for target "
6003 << ThumbTripleName << '\n';
6007 MachO::mach_header Header = MachOOF->getHeader();
6009 // FIXME: Using the -cfg command line option, this code used to be able to
6010 // annotate relocations with the referenced symbol's name, and if this was
6011 // inside a __[cf]string section, the data it points to. This is now replaced
6012 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
6013 std::vector<SectionRef> Sections;
6014 std::vector<SymbolRef> Symbols;
6015 SmallVector<uint64_t, 8> FoundFns;
6016 uint64_t BaseSegmentAddress;
6018 getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
6019 BaseSegmentAddress);
6021 // Sort the symbols by address, just in case they didn't come in that way.
6022 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
6024 // Build a data in code table that is sorted on by the address of each entry.
6025 uint64_t BaseAddress = 0;
6026 if (Header.filetype == MachO::MH_OBJECT)
6027 BaseAddress = Sections[0].getAddress();
6029 BaseAddress = BaseSegmentAddress;
6031 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
6034 DI->getOffset(Offset);
6035 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
6037 array_pod_sort(Dices.begin(), Dices.end());
6040 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
6042 raw_ostream &DebugOut = nulls();
6045 std::unique_ptr<DIContext> diContext;
6046 ObjectFile *DbgObj = MachOOF;
6047 // Try to find debug info and set up the DIContext for it.
6049 // A separate DSym file path was specified, parse it as a macho file,
6050 // get the sections and supply it to the section name parsing machinery.
6051 if (!DSYMFile.empty()) {
6052 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
6053 MemoryBuffer::getFileOrSTDIN(DSYMFile);
6054 if (std::error_code EC = BufOrErr.getError()) {
6055 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
6059 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
6064 // Setup the DIContext
6065 diContext.reset(new DWARFContextInMemory(*DbgObj));
6068 if (DumpSections.size() == 0)
6069 outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
6071 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
6073 if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
6076 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
6078 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
6079 if (SegmentName != DisSegName)
6083 Sections[SectIdx].getContents(BytesStr);
6084 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
6086 uint64_t SectAddress = Sections[SectIdx].getAddress();
6088 bool symbolTableWorked = false;
6090 // Parse relocations.
6091 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
6092 for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) {
6093 uint64_t RelocOffset = Reloc.getOffset();
6094 uint64_t SectionAddress = Sections[SectIdx].getAddress();
6095 RelocOffset -= SectionAddress;
6097 symbol_iterator RelocSym = Reloc.getSymbol();
6099 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
6101 array_pod_sort(Relocs.begin(), Relocs.end());
6103 // Create a map of symbol addresses to symbol names for use by
6104 // the SymbolizerSymbolLookUp() routine.
6105 SymbolAddressMap AddrMap;
6106 bool DisSymNameFound = false;
6107 for (const SymbolRef &Symbol : MachOOF->symbols()) {
6108 SymbolRef::Type ST = Symbol.getType();
6109 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
6110 ST == SymbolRef::ST_Other) {
6111 uint64_t Address = Symbol.getValue();
6112 ErrorOr<StringRef> SymNameOrErr = Symbol.getName();
6113 if (std::error_code EC = SymNameOrErr.getError())
6114 report_fatal_error(EC.message());
6115 StringRef SymName = *SymNameOrErr;
6116 AddrMap[Address] = SymName;
6117 if (!DisSymName.empty() && DisSymName == SymName)
6118 DisSymNameFound = true;
6121 if (!DisSymName.empty() && !DisSymNameFound) {
6122 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
6125 // Set up the block of info used by the Symbolizer call backs.
6126 SymbolizerInfo.verbose = !NoSymbolicOperands;
6127 SymbolizerInfo.O = MachOOF;
6128 SymbolizerInfo.S = Sections[SectIdx];
6129 SymbolizerInfo.AddrMap = &AddrMap;
6130 SymbolizerInfo.Sections = &Sections;
6131 SymbolizerInfo.class_name = nullptr;
6132 SymbolizerInfo.selector_name = nullptr;
6133 SymbolizerInfo.method = nullptr;
6134 SymbolizerInfo.demangled_name = nullptr;
6135 SymbolizerInfo.bindtable = nullptr;
6136 SymbolizerInfo.adrp_addr = 0;
6137 SymbolizerInfo.adrp_inst = 0;
6138 // Same for the ThumbSymbolizer
6139 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
6140 ThumbSymbolizerInfo.O = MachOOF;
6141 ThumbSymbolizerInfo.S = Sections[SectIdx];
6142 ThumbSymbolizerInfo.AddrMap = &AddrMap;
6143 ThumbSymbolizerInfo.Sections = &Sections;
6144 ThumbSymbolizerInfo.class_name = nullptr;
6145 ThumbSymbolizerInfo.selector_name = nullptr;
6146 ThumbSymbolizerInfo.method = nullptr;
6147 ThumbSymbolizerInfo.demangled_name = nullptr;
6148 ThumbSymbolizerInfo.bindtable = nullptr;
6149 ThumbSymbolizerInfo.adrp_addr = 0;
6150 ThumbSymbolizerInfo.adrp_inst = 0;
6152 // Disassemble symbol by symbol.
6153 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
6154 ErrorOr<StringRef> SymNameOrErr = Symbols[SymIdx].getName();
6155 if (std::error_code EC = SymNameOrErr.getError())
6156 report_fatal_error(EC.message());
6157 StringRef SymName = *SymNameOrErr;
6159 SymbolRef::Type ST = Symbols[SymIdx].getType();
6160 if (ST != SymbolRef::ST_Function)
6163 // Make sure the symbol is defined in this section.
6164 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
6168 // If we are only disassembling one symbol see if this is that symbol.
6169 if (!DisSymName.empty() && DisSymName != SymName)
6172 // Start at the address of the symbol relative to the section's address.
6173 uint64_t Start = Symbols[SymIdx].getValue();
6174 uint64_t SectionAddress = Sections[SectIdx].getAddress();
6175 Start -= SectionAddress;
6177 // Stop disassembling either at the beginning of the next symbol or at
6178 // the end of the section.
6179 bool containsNextSym = false;
6180 uint64_t NextSym = 0;
6181 uint64_t NextSymIdx = SymIdx + 1;
6182 while (Symbols.size() > NextSymIdx) {
6183 SymbolRef::Type NextSymType = Symbols[NextSymIdx].getType();
6184 if (NextSymType == SymbolRef::ST_Function) {
6186 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
6187 NextSym = Symbols[NextSymIdx].getValue();
6188 NextSym -= SectionAddress;
6194 uint64_t SectSize = Sections[SectIdx].getSize();
6195 uint64_t End = containsNextSym ? NextSym : SectSize;
6198 symbolTableWorked = true;
6200 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
6202 (MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget;
6204 outs() << SymName << ":\n";
6205 DILineInfo lastLine;
6206 for (uint64_t Index = Start; Index < End; Index += Size) {
6209 uint64_t PC = SectAddress + Index;
6210 if (!NoLeadingAddr) {
6211 if (FullLeadingAddr) {
6212 if (MachOOF->is64Bit())
6213 outs() << format("%016" PRIx64, PC);
6215 outs() << format("%08" PRIx64, PC);
6217 outs() << format("%8" PRIx64 ":", PC);
6223 // Check the data in code table here to see if this is data not an
6224 // instruction to be disassembled.
6226 Dice.push_back(std::make_pair(PC, DiceRef()));
6227 dice_table_iterator DTI =
6228 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
6229 compareDiceTableEntries);
6230 if (DTI != Dices.end()) {
6232 DTI->second.getLength(Length);
6234 DTI->second.getKind(Kind);
6235 Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
6236 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
6237 (PC == (DTI->first + Length - 1)) && (Length & 1))
6242 SmallVector<char, 64> AnnotationsBytes;
6243 raw_svector_ostream Annotations(AnnotationsBytes);
6247 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
6248 PC, DebugOut, Annotations);
6250 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
6251 DebugOut, Annotations);
6253 if (!NoShowRawInsn) {
6254 dumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, Size), outs());
6256 formatted_raw_ostream FormattedOS(outs());
6257 Annotations.flush();
6258 StringRef AnnotationsStr = Annotations.str();
6260 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
6262 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
6263 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
6265 // Print debug info.
6267 DILineInfo dli = diContext->getLineInfoForAddress(PC);
6268 // Print valid line info if it changed.
6269 if (dli != lastLine && dli.Line != 0)
6270 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
6276 unsigned int Arch = MachOOF->getArch();
6277 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
6278 outs() << format("\t.byte 0x%02x #bad opcode\n",
6279 *(Bytes.data() + Index) & 0xff);
6280 Size = 1; // skip exactly one illegible byte and move on.
6281 } else if (Arch == Triple::aarch64) {
6282 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6283 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
6284 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
6285 (*(Bytes.data() + Index + 3) & 0xff) << 24;
6286 outs() << format("\t.long\t0x%08x\n", opcode);
6289 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6291 Size = 1; // skip illegible bytes
6296 if (!symbolTableWorked) {
6297 // Reading the symbol table didn't work, disassemble the whole section.
6298 uint64_t SectAddress = Sections[SectIdx].getAddress();
6299 uint64_t SectSize = Sections[SectIdx].getSize();
6301 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
6304 uint64_t PC = SectAddress + Index;
6305 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
6306 DebugOut, nulls())) {
6307 if (!NoLeadingAddr) {
6308 if (FullLeadingAddr) {
6309 if (MachOOF->is64Bit())
6310 outs() << format("%016" PRIx64, PC);
6312 outs() << format("%08" PRIx64, PC);
6314 outs() << format("%8" PRIx64 ":", PC);
6317 if (!NoShowRawInsn) {
6319 dumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, InstSize), outs());
6321 IP->printInst(&Inst, outs(), "", *STI);
6324 unsigned int Arch = MachOOF->getArch();
6325 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
6326 outs() << format("\t.byte 0x%02x #bad opcode\n",
6327 *(Bytes.data() + Index) & 0xff);
6328 InstSize = 1; // skip exactly one illegible byte and move on.
6330 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6332 InstSize = 1; // skip illegible bytes
6337 // The TripleName's need to be reset if we are called again for a different
6340 ThumbTripleName = "";
6342 if (SymbolizerInfo.method != nullptr)
6343 free(SymbolizerInfo.method);
6344 if (SymbolizerInfo.demangled_name != nullptr)
6345 free(SymbolizerInfo.demangled_name);
6346 if (SymbolizerInfo.bindtable != nullptr)
6347 delete SymbolizerInfo.bindtable;
6348 if (ThumbSymbolizerInfo.method != nullptr)
6349 free(ThumbSymbolizerInfo.method);
6350 if (ThumbSymbolizerInfo.demangled_name != nullptr)
6351 free(ThumbSymbolizerInfo.demangled_name);
6352 if (ThumbSymbolizerInfo.bindtable != nullptr)
6353 delete ThumbSymbolizerInfo.bindtable;
6357 //===----------------------------------------------------------------------===//
6358 // __compact_unwind section dumping
6359 //===----------------------------------------------------------------------===//
6363 template <typename T> static uint64_t readNext(const char *&Buf) {
6364 using llvm::support::little;
6365 using llvm::support::unaligned;
6367 uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
6372 struct CompactUnwindEntry {
6373 uint32_t OffsetInSection;
6375 uint64_t FunctionAddr;
6377 uint32_t CompactEncoding;
6378 uint64_t PersonalityAddr;
6381 RelocationRef FunctionReloc;
6382 RelocationRef PersonalityReloc;
6383 RelocationRef LSDAReloc;
6385 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
6386 : OffsetInSection(Offset) {
6388 read<uint64_t>(Contents.data() + Offset);
6390 read<uint32_t>(Contents.data() + Offset);
6394 template <typename UIntPtr> void read(const char *Buf) {
6395 FunctionAddr = readNext<UIntPtr>(Buf);
6396 Length = readNext<uint32_t>(Buf);
6397 CompactEncoding = readNext<uint32_t>(Buf);
6398 PersonalityAddr = readNext<UIntPtr>(Buf);
6399 LSDAAddr = readNext<UIntPtr>(Buf);
6404 /// Given a relocation from __compact_unwind, consisting of the RelocationRef
6405 /// and data being relocated, determine the best base Name and Addend to use for
6406 /// display purposes.
6408 /// 1. An Extern relocation will directly reference a symbol (and the data is
6409 /// then already an addend), so use that.
6410 /// 2. Otherwise the data is an offset in the object file's layout; try to find
6411 // a symbol before it in the same section, and use the offset from there.
6412 /// 3. Finally, if all that fails, fall back to an offset from the start of the
6413 /// referenced section.
6414 static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
6415 std::map<uint64_t, SymbolRef> &Symbols,
6416 const RelocationRef &Reloc, uint64_t Addr,
6417 StringRef &Name, uint64_t &Addend) {
6418 if (Reloc.getSymbol() != Obj->symbol_end()) {
6419 ErrorOr<StringRef> NameOrErr = Reloc.getSymbol()->getName();
6420 if (std::error_code EC = NameOrErr.getError())
6421 report_fatal_error(EC.message());
6427 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
6428 SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
6430 uint64_t SectionAddr = RelocSection.getAddress();
6432 auto Sym = Symbols.upper_bound(Addr);
6433 if (Sym == Symbols.begin()) {
6434 // The first symbol in the object is after this reference, the best we can
6435 // do is section-relative notation.
6436 RelocSection.getName(Name);
6437 Addend = Addr - SectionAddr;
6441 // Go back one so that SymbolAddress <= Addr.
6444 section_iterator SymSection = Obj->section_end();
6445 Sym->second.getSection(SymSection);
6446 if (RelocSection == *SymSection) {
6447 // There's a valid symbol in the same section before this reference.
6448 ErrorOr<StringRef> NameOrErr = Sym->second.getName();
6449 if (std::error_code EC = NameOrErr.getError())
6450 report_fatal_error(EC.message());
6452 Addend = Addr - Sym->first;
6456 // There is a symbol before this reference, but it's in a different
6457 // section. Probably not helpful to mention it, so use the section name.
6458 RelocSection.getName(Name);
6459 Addend = Addr - SectionAddr;
6462 static void printUnwindRelocDest(const MachOObjectFile *Obj,
6463 std::map<uint64_t, SymbolRef> &Symbols,
6464 const RelocationRef &Reloc, uint64_t Addr) {
6468 if (!Reloc.getObject())
6471 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
6475 outs() << " + " << format("0x%" PRIx64, Addend);
6479 printMachOCompactUnwindSection(const MachOObjectFile *Obj,
6480 std::map<uint64_t, SymbolRef> &Symbols,
6481 const SectionRef &CompactUnwind) {
6483 assert(Obj->isLittleEndian() &&
6484 "There should not be a big-endian .o with __compact_unwind");
6486 bool Is64 = Obj->is64Bit();
6487 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
6488 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
6491 CompactUnwind.getContents(Contents);
6493 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
6495 // First populate the initial raw offsets, encodings and so on from the entry.
6496 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
6497 CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
6498 CompactUnwinds.push_back(Entry);
6501 // Next we need to look at the relocations to find out what objects are
6502 // actually being referred to.
6503 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
6504 uint64_t RelocAddress = Reloc.getOffset();
6506 uint32_t EntryIdx = RelocAddress / EntrySize;
6507 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
6508 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
6510 if (OffsetInEntry == 0)
6511 Entry.FunctionReloc = Reloc;
6512 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
6513 Entry.PersonalityReloc = Reloc;
6514 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
6515 Entry.LSDAReloc = Reloc;
6517 llvm_unreachable("Unexpected relocation in __compact_unwind section");
6520 // Finally, we're ready to print the data we've gathered.
6521 outs() << "Contents of __compact_unwind section:\n";
6522 for (auto &Entry : CompactUnwinds) {
6523 outs() << " Entry at offset "
6524 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
6526 // 1. Start of the region this entry applies to.
6527 outs() << " start: " << format("0x%" PRIx64,
6528 Entry.FunctionAddr) << ' ';
6529 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
6532 // 2. Length of the region this entry applies to.
6533 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
6535 // 3. The 32-bit compact encoding.
6536 outs() << " compact encoding: "
6537 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
6539 // 4. The personality function, if present.
6540 if (Entry.PersonalityReloc.getObject()) {
6541 outs() << " personality function: "
6542 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
6543 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
6544 Entry.PersonalityAddr);
6548 // 5. This entry's language-specific data area.
6549 if (Entry.LSDAReloc.getObject()) {
6550 outs() << " LSDA: " << format("0x%" PRIx64,
6551 Entry.LSDAAddr) << ' ';
6552 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
6558 //===----------------------------------------------------------------------===//
6559 // __unwind_info section dumping
6560 //===----------------------------------------------------------------------===//
6562 static void printRegularSecondLevelUnwindPage(const char *PageStart) {
6563 const char *Pos = PageStart;
6564 uint32_t Kind = readNext<uint32_t>(Pos);
6566 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
6568 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6569 uint16_t NumEntries = readNext<uint16_t>(Pos);
6571 Pos = PageStart + EntriesStart;
6572 for (unsigned i = 0; i < NumEntries; ++i) {
6573 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6574 uint32_t Encoding = readNext<uint32_t>(Pos);
6576 outs() << " [" << i << "]: "
6577 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6579 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
6583 static void printCompressedSecondLevelUnwindPage(
6584 const char *PageStart, uint32_t FunctionBase,
6585 const SmallVectorImpl<uint32_t> &CommonEncodings) {
6586 const char *Pos = PageStart;
6587 uint32_t Kind = readNext<uint32_t>(Pos);
6589 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
6591 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6592 uint16_t NumEntries = readNext<uint16_t>(Pos);
6594 uint16_t EncodingsStart = readNext<uint16_t>(Pos);
6595 readNext<uint16_t>(Pos);
6596 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
6597 PageStart + EncodingsStart);
6599 Pos = PageStart + EntriesStart;
6600 for (unsigned i = 0; i < NumEntries; ++i) {
6601 uint32_t Entry = readNext<uint32_t>(Pos);
6602 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
6603 uint32_t EncodingIdx = Entry >> 24;
6606 if (EncodingIdx < CommonEncodings.size())
6607 Encoding = CommonEncodings[EncodingIdx];
6609 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
6611 outs() << " [" << i << "]: "
6612 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6614 << "encoding[" << EncodingIdx
6615 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
6619 static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
6620 std::map<uint64_t, SymbolRef> &Symbols,
6621 const SectionRef &UnwindInfo) {
6623 assert(Obj->isLittleEndian() &&
6624 "There should not be a big-endian .o with __unwind_info");
6626 outs() << "Contents of __unwind_info section:\n";
6629 UnwindInfo.getContents(Contents);
6630 const char *Pos = Contents.data();
6632 //===----------------------------------
6634 //===----------------------------------
6636 uint32_t Version = readNext<uint32_t>(Pos);
6637 outs() << " Version: "
6638 << format("0x%" PRIx32, Version) << '\n';
6639 assert(Version == 1 && "only understand version 1");
6641 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
6642 outs() << " Common encodings array section offset: "
6643 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
6644 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
6645 outs() << " Number of common encodings in array: "
6646 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
6648 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
6649 outs() << " Personality function array section offset: "
6650 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
6651 uint32_t NumPersonalities = readNext<uint32_t>(Pos);
6652 outs() << " Number of personality functions in array: "
6653 << format("0x%" PRIx32, NumPersonalities) << '\n';
6655 uint32_t IndicesStart = readNext<uint32_t>(Pos);
6656 outs() << " Index array section offset: "
6657 << format("0x%" PRIx32, IndicesStart) << '\n';
6658 uint32_t NumIndices = readNext<uint32_t>(Pos);
6659 outs() << " Number of indices in array: "
6660 << format("0x%" PRIx32, NumIndices) << '\n';
6662 //===----------------------------------
6663 // A shared list of common encodings
6664 //===----------------------------------
6666 // These occupy indices in the range [0, N] whenever an encoding is referenced
6667 // from a compressed 2nd level index table. In practice the linker only
6668 // creates ~128 of these, so that indices are available to embed encodings in
6669 // the 2nd level index.
6671 SmallVector<uint32_t, 64> CommonEncodings;
6672 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
6673 Pos = Contents.data() + CommonEncodingsStart;
6674 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
6675 uint32_t Encoding = readNext<uint32_t>(Pos);
6676 CommonEncodings.push_back(Encoding);
6678 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
6682 //===----------------------------------
6683 // Personality functions used in this executable
6684 //===----------------------------------
6686 // There should be only a handful of these (one per source language,
6687 // roughly). Particularly since they only get 2 bits in the compact encoding.
6689 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
6690 Pos = Contents.data() + PersonalitiesStart;
6691 for (unsigned i = 0; i < NumPersonalities; ++i) {
6692 uint32_t PersonalityFn = readNext<uint32_t>(Pos);
6693 outs() << " personality[" << i + 1
6694 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
6697 //===----------------------------------
6698 // The level 1 index entries
6699 //===----------------------------------
6701 // These specify an approximate place to start searching for the more detailed
6702 // information, sorted by PC.
6705 uint32_t FunctionOffset;
6706 uint32_t SecondLevelPageStart;
6710 SmallVector<IndexEntry, 4> IndexEntries;
6712 outs() << " Top level indices: (count = " << NumIndices << ")\n";
6713 Pos = Contents.data() + IndicesStart;
6714 for (unsigned i = 0; i < NumIndices; ++i) {
6717 Entry.FunctionOffset = readNext<uint32_t>(Pos);
6718 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
6719 Entry.LSDAStart = readNext<uint32_t>(Pos);
6720 IndexEntries.push_back(Entry);
6722 outs() << " [" << i << "]: "
6723 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
6725 << "2nd level page offset="
6726 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
6727 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
6730 //===----------------------------------
6731 // Next come the LSDA tables
6732 //===----------------------------------
6734 // The LSDA layout is rather implicit: it's a contiguous array of entries from
6735 // the first top-level index's LSDAOffset to the last (sentinel).
6737 outs() << " LSDA descriptors:\n";
6738 Pos = Contents.data() + IndexEntries[0].LSDAStart;
6739 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
6740 (2 * sizeof(uint32_t));
6741 for (int i = 0; i < NumLSDAs; ++i) {
6742 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6743 uint32_t LSDAOffset = readNext<uint32_t>(Pos);
6744 outs() << " [" << i << "]: "
6745 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6747 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
6750 //===----------------------------------
6751 // Finally, the 2nd level indices
6752 //===----------------------------------
6754 // Generally these are 4K in size, and have 2 possible forms:
6755 // + Regular stores up to 511 entries with disparate encodings
6756 // + Compressed stores up to 1021 entries if few enough compact encoding
6758 outs() << " Second level indices:\n";
6759 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
6760 // The final sentinel top-level index has no associated 2nd level page
6761 if (IndexEntries[i].SecondLevelPageStart == 0)
6764 outs() << " Second level index[" << i << "]: "
6765 << "offset in section="
6766 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
6768 << "base function offset="
6769 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
6771 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
6772 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
6774 printRegularSecondLevelUnwindPage(Pos);
6776 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
6779 llvm_unreachable("Do not know how to print this kind of 2nd level page");
6783 void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
6784 std::map<uint64_t, SymbolRef> Symbols;
6785 for (const SymbolRef &SymRef : Obj->symbols()) {
6786 // Discard any undefined or absolute symbols. They're not going to take part
6787 // in the convenience lookup for unwind info and just take up resources.
6788 section_iterator Section = Obj->section_end();
6789 SymRef.getSection(Section);
6790 if (Section == Obj->section_end())
6793 uint64_t Addr = SymRef.getValue();
6794 Symbols.insert(std::make_pair(Addr, SymRef));
6797 for (const SectionRef &Section : Obj->sections()) {
6799 Section.getName(SectName);
6800 if (SectName == "__compact_unwind")
6801 printMachOCompactUnwindSection(Obj, Symbols, Section);
6802 else if (SectName == "__unwind_info")
6803 printMachOUnwindInfoSection(Obj, Symbols, Section);
6804 else if (SectName == "__eh_frame")
6805 outs() << "llvm-objdump: warning: unhandled __eh_frame section\n";
6809 static void PrintMachHeader(uint32_t magic, uint32_t cputype,
6810 uint32_t cpusubtype, uint32_t filetype,
6811 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
6813 outs() << "Mach header\n";
6814 outs() << " magic cputype cpusubtype caps filetype ncmds "
6815 "sizeofcmds flags\n";
6817 if (magic == MachO::MH_MAGIC)
6818 outs() << " MH_MAGIC";
6819 else if (magic == MachO::MH_MAGIC_64)
6820 outs() << "MH_MAGIC_64";
6822 outs() << format(" 0x%08" PRIx32, magic);
6824 case MachO::CPU_TYPE_I386:
6826 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6827 case MachO::CPU_SUBTYPE_I386_ALL:
6831 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6835 case MachO::CPU_TYPE_X86_64:
6836 outs() << " X86_64";
6837 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6838 case MachO::CPU_SUBTYPE_X86_64_ALL:
6841 case MachO::CPU_SUBTYPE_X86_64_H:
6842 outs() << " Haswell";
6845 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6849 case MachO::CPU_TYPE_ARM:
6851 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6852 case MachO::CPU_SUBTYPE_ARM_ALL:
6855 case MachO::CPU_SUBTYPE_ARM_V4T:
6858 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
6861 case MachO::CPU_SUBTYPE_ARM_XSCALE:
6862 outs() << " XSCALE";
6864 case MachO::CPU_SUBTYPE_ARM_V6:
6867 case MachO::CPU_SUBTYPE_ARM_V6M:
6870 case MachO::CPU_SUBTYPE_ARM_V7:
6873 case MachO::CPU_SUBTYPE_ARM_V7EM:
6876 case MachO::CPU_SUBTYPE_ARM_V7K:
6879 case MachO::CPU_SUBTYPE_ARM_V7M:
6882 case MachO::CPU_SUBTYPE_ARM_V7S:
6886 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6890 case MachO::CPU_TYPE_ARM64:
6892 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6893 case MachO::CPU_SUBTYPE_ARM64_ALL:
6897 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6901 case MachO::CPU_TYPE_POWERPC:
6903 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6904 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6908 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6912 case MachO::CPU_TYPE_POWERPC64:
6914 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6915 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6919 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6924 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
6927 outs() << format(" 0x%02" PRIx32,
6928 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
6931 case MachO::MH_OBJECT:
6932 outs() << " OBJECT";
6934 case MachO::MH_EXECUTE:
6935 outs() << " EXECUTE";
6937 case MachO::MH_FVMLIB:
6938 outs() << " FVMLIB";
6940 case MachO::MH_CORE:
6943 case MachO::MH_PRELOAD:
6944 outs() << " PRELOAD";
6946 case MachO::MH_DYLIB:
6949 case MachO::MH_DYLIB_STUB:
6950 outs() << " DYLIB_STUB";
6952 case MachO::MH_DYLINKER:
6953 outs() << " DYLINKER";
6955 case MachO::MH_BUNDLE:
6956 outs() << " BUNDLE";
6958 case MachO::MH_DSYM:
6961 case MachO::MH_KEXT_BUNDLE:
6962 outs() << " KEXTBUNDLE";
6965 outs() << format(" %10u", filetype);
6968 outs() << format(" %5u", ncmds);
6969 outs() << format(" %10u", sizeofcmds);
6971 if (f & MachO::MH_NOUNDEFS) {
6972 outs() << " NOUNDEFS";
6973 f &= ~MachO::MH_NOUNDEFS;
6975 if (f & MachO::MH_INCRLINK) {
6976 outs() << " INCRLINK";
6977 f &= ~MachO::MH_INCRLINK;
6979 if (f & MachO::MH_DYLDLINK) {
6980 outs() << " DYLDLINK";
6981 f &= ~MachO::MH_DYLDLINK;
6983 if (f & MachO::MH_BINDATLOAD) {
6984 outs() << " BINDATLOAD";
6985 f &= ~MachO::MH_BINDATLOAD;
6987 if (f & MachO::MH_PREBOUND) {
6988 outs() << " PREBOUND";
6989 f &= ~MachO::MH_PREBOUND;
6991 if (f & MachO::MH_SPLIT_SEGS) {
6992 outs() << " SPLIT_SEGS";
6993 f &= ~MachO::MH_SPLIT_SEGS;
6995 if (f & MachO::MH_LAZY_INIT) {
6996 outs() << " LAZY_INIT";
6997 f &= ~MachO::MH_LAZY_INIT;
6999 if (f & MachO::MH_TWOLEVEL) {
7000 outs() << " TWOLEVEL";
7001 f &= ~MachO::MH_TWOLEVEL;
7003 if (f & MachO::MH_FORCE_FLAT) {
7004 outs() << " FORCE_FLAT";
7005 f &= ~MachO::MH_FORCE_FLAT;
7007 if (f & MachO::MH_NOMULTIDEFS) {
7008 outs() << " NOMULTIDEFS";
7009 f &= ~MachO::MH_NOMULTIDEFS;
7011 if (f & MachO::MH_NOFIXPREBINDING) {
7012 outs() << " NOFIXPREBINDING";
7013 f &= ~MachO::MH_NOFIXPREBINDING;
7015 if (f & MachO::MH_PREBINDABLE) {
7016 outs() << " PREBINDABLE";
7017 f &= ~MachO::MH_PREBINDABLE;
7019 if (f & MachO::MH_ALLMODSBOUND) {
7020 outs() << " ALLMODSBOUND";
7021 f &= ~MachO::MH_ALLMODSBOUND;
7023 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
7024 outs() << " SUBSECTIONS_VIA_SYMBOLS";
7025 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
7027 if (f & MachO::MH_CANONICAL) {
7028 outs() << " CANONICAL";
7029 f &= ~MachO::MH_CANONICAL;
7031 if (f & MachO::MH_WEAK_DEFINES) {
7032 outs() << " WEAK_DEFINES";
7033 f &= ~MachO::MH_WEAK_DEFINES;
7035 if (f & MachO::MH_BINDS_TO_WEAK) {
7036 outs() << " BINDS_TO_WEAK";
7037 f &= ~MachO::MH_BINDS_TO_WEAK;
7039 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
7040 outs() << " ALLOW_STACK_EXECUTION";
7041 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
7043 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
7044 outs() << " DEAD_STRIPPABLE_DYLIB";
7045 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
7047 if (f & MachO::MH_PIE) {
7049 f &= ~MachO::MH_PIE;
7051 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
7052 outs() << " NO_REEXPORTED_DYLIBS";
7053 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
7055 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
7056 outs() << " MH_HAS_TLV_DESCRIPTORS";
7057 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
7059 if (f & MachO::MH_NO_HEAP_EXECUTION) {
7060 outs() << " MH_NO_HEAP_EXECUTION";
7061 f &= ~MachO::MH_NO_HEAP_EXECUTION;
7063 if (f & MachO::MH_APP_EXTENSION_SAFE) {
7064 outs() << " APP_EXTENSION_SAFE";
7065 f &= ~MachO::MH_APP_EXTENSION_SAFE;
7067 if (f != 0 || flags == 0)
7068 outs() << format(" 0x%08" PRIx32, f);
7070 outs() << format(" 0x%08" PRIx32, magic);
7071 outs() << format(" %7d", cputype);
7072 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7073 outs() << format(" 0x%02" PRIx32,
7074 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7075 outs() << format(" %10u", filetype);
7076 outs() << format(" %5u", ncmds);
7077 outs() << format(" %10u", sizeofcmds);
7078 outs() << format(" 0x%08" PRIx32, flags);
7083 static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
7084 StringRef SegName, uint64_t vmaddr,
7085 uint64_t vmsize, uint64_t fileoff,
7086 uint64_t filesize, uint32_t maxprot,
7087 uint32_t initprot, uint32_t nsects,
7088 uint32_t flags, uint32_t object_size,
7090 uint64_t expected_cmdsize;
7091 if (cmd == MachO::LC_SEGMENT) {
7092 outs() << " cmd LC_SEGMENT\n";
7093 expected_cmdsize = nsects;
7094 expected_cmdsize *= sizeof(struct MachO::section);
7095 expected_cmdsize += sizeof(struct MachO::segment_command);
7097 outs() << " cmd LC_SEGMENT_64\n";
7098 expected_cmdsize = nsects;
7099 expected_cmdsize *= sizeof(struct MachO::section_64);
7100 expected_cmdsize += sizeof(struct MachO::segment_command_64);
7102 outs() << " cmdsize " << cmdsize;
7103 if (cmdsize != expected_cmdsize)
7104 outs() << " Inconsistent size\n";
7107 outs() << " segname " << SegName << "\n";
7108 if (cmd == MachO::LC_SEGMENT_64) {
7109 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
7110 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
7112 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
7113 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
7115 outs() << " fileoff " << fileoff;
7116 if (fileoff > object_size)
7117 outs() << " (past end of file)\n";
7120 outs() << " filesize " << filesize;
7121 if (fileoff + filesize > object_size)
7122 outs() << " (past end of file)\n";
7127 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7128 MachO::VM_PROT_EXECUTE)) != 0)
7129 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
7131 if (maxprot & MachO::VM_PROT_READ)
7132 outs() << " maxprot r";
7134 outs() << " maxprot -";
7135 if (maxprot & MachO::VM_PROT_WRITE)
7139 if (maxprot & MachO::VM_PROT_EXECUTE)
7145 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7146 MachO::VM_PROT_EXECUTE)) != 0)
7147 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
7149 if (initprot & MachO::VM_PROT_READ)
7150 outs() << " initprot r";
7152 outs() << " initprot -";
7153 if (initprot & MachO::VM_PROT_WRITE)
7157 if (initprot & MachO::VM_PROT_EXECUTE)
7163 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
7164 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
7166 outs() << " nsects " << nsects << "\n";
7170 outs() << " (none)\n";
7172 if (flags & MachO::SG_HIGHVM) {
7173 outs() << " HIGHVM";
7174 flags &= ~MachO::SG_HIGHVM;
7176 if (flags & MachO::SG_FVMLIB) {
7177 outs() << " FVMLIB";
7178 flags &= ~MachO::SG_FVMLIB;
7180 if (flags & MachO::SG_NORELOC) {
7181 outs() << " NORELOC";
7182 flags &= ~MachO::SG_NORELOC;
7184 if (flags & MachO::SG_PROTECTED_VERSION_1) {
7185 outs() << " PROTECTED_VERSION_1";
7186 flags &= ~MachO::SG_PROTECTED_VERSION_1;
7189 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
7194 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
7198 static void PrintSection(const char *sectname, const char *segname,
7199 uint64_t addr, uint64_t size, uint32_t offset,
7200 uint32_t align, uint32_t reloff, uint32_t nreloc,
7201 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
7202 uint32_t cmd, const char *sg_segname,
7203 uint32_t filetype, uint32_t object_size,
7205 outs() << "Section\n";
7206 outs() << " sectname " << format("%.16s\n", sectname);
7207 outs() << " segname " << format("%.16s", segname);
7208 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
7209 outs() << " (does not match segment)\n";
7212 if (cmd == MachO::LC_SEGMENT_64) {
7213 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
7214 outs() << " size " << format("0x%016" PRIx64, size);
7216 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
7217 outs() << " size " << format("0x%08" PRIx64, size);
7219 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
7220 outs() << " (past end of file)\n";
7223 outs() << " offset " << offset;
7224 if (offset > object_size)
7225 outs() << " (past end of file)\n";
7228 uint32_t align_shifted = 1 << align;
7229 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
7230 outs() << " reloff " << reloff;
7231 if (reloff > object_size)
7232 outs() << " (past end of file)\n";
7235 outs() << " nreloc " << nreloc;
7236 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
7237 outs() << " (past end of file)\n";
7240 uint32_t section_type = flags & MachO::SECTION_TYPE;
7243 if (section_type == MachO::S_REGULAR)
7244 outs() << " S_REGULAR\n";
7245 else if (section_type == MachO::S_ZEROFILL)
7246 outs() << " S_ZEROFILL\n";
7247 else if (section_type == MachO::S_CSTRING_LITERALS)
7248 outs() << " S_CSTRING_LITERALS\n";
7249 else if (section_type == MachO::S_4BYTE_LITERALS)
7250 outs() << " S_4BYTE_LITERALS\n";
7251 else if (section_type == MachO::S_8BYTE_LITERALS)
7252 outs() << " S_8BYTE_LITERALS\n";
7253 else if (section_type == MachO::S_16BYTE_LITERALS)
7254 outs() << " S_16BYTE_LITERALS\n";
7255 else if (section_type == MachO::S_LITERAL_POINTERS)
7256 outs() << " S_LITERAL_POINTERS\n";
7257 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
7258 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
7259 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
7260 outs() << " S_LAZY_SYMBOL_POINTERS\n";
7261 else if (section_type == MachO::S_SYMBOL_STUBS)
7262 outs() << " S_SYMBOL_STUBS\n";
7263 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
7264 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
7265 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
7266 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
7267 else if (section_type == MachO::S_COALESCED)
7268 outs() << " S_COALESCED\n";
7269 else if (section_type == MachO::S_INTERPOSING)
7270 outs() << " S_INTERPOSING\n";
7271 else if (section_type == MachO::S_DTRACE_DOF)
7272 outs() << " S_DTRACE_DOF\n";
7273 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
7274 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
7275 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
7276 outs() << " S_THREAD_LOCAL_REGULAR\n";
7277 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
7278 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
7279 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
7280 outs() << " S_THREAD_LOCAL_VARIABLES\n";
7281 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7282 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
7283 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
7284 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
7286 outs() << format("0x%08" PRIx32, section_type) << "\n";
7287 outs() << "attributes";
7288 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
7289 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
7290 outs() << " PURE_INSTRUCTIONS";
7291 if (section_attributes & MachO::S_ATTR_NO_TOC)
7292 outs() << " NO_TOC";
7293 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
7294 outs() << " STRIP_STATIC_SYMS";
7295 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
7296 outs() << " NO_DEAD_STRIP";
7297 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
7298 outs() << " LIVE_SUPPORT";
7299 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
7300 outs() << " SELF_MODIFYING_CODE";
7301 if (section_attributes & MachO::S_ATTR_DEBUG)
7303 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
7304 outs() << " SOME_INSTRUCTIONS";
7305 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
7306 outs() << " EXT_RELOC";
7307 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
7308 outs() << " LOC_RELOC";
7309 if (section_attributes == 0)
7310 outs() << " (none)";
7313 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
7314 outs() << " reserved1 " << reserved1;
7315 if (section_type == MachO::S_SYMBOL_STUBS ||
7316 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
7317 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
7318 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
7319 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7320 outs() << " (index into indirect symbol table)\n";
7323 outs() << " reserved2 " << reserved2;
7324 if (section_type == MachO::S_SYMBOL_STUBS)
7325 outs() << " (size of stubs)\n";
7330 static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
7331 uint32_t object_size) {
7332 outs() << " cmd LC_SYMTAB\n";
7333 outs() << " cmdsize " << st.cmdsize;
7334 if (st.cmdsize != sizeof(struct MachO::symtab_command))
7335 outs() << " Incorrect size\n";
7338 outs() << " symoff " << st.symoff;
7339 if (st.symoff > object_size)
7340 outs() << " (past end of file)\n";
7343 outs() << " nsyms " << st.nsyms;
7346 big_size = st.nsyms;
7347 big_size *= sizeof(struct MachO::nlist_64);
7348 big_size += st.symoff;
7349 if (big_size > object_size)
7350 outs() << " (past end of file)\n";
7354 big_size = st.nsyms;
7355 big_size *= sizeof(struct MachO::nlist);
7356 big_size += st.symoff;
7357 if (big_size > object_size)
7358 outs() << " (past end of file)\n";
7362 outs() << " stroff " << st.stroff;
7363 if (st.stroff > object_size)
7364 outs() << " (past end of file)\n";
7367 outs() << " strsize " << st.strsize;
7368 big_size = st.stroff;
7369 big_size += st.strsize;
7370 if (big_size > object_size)
7371 outs() << " (past end of file)\n";
7376 static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
7377 uint32_t nsyms, uint32_t object_size,
7379 outs() << " cmd LC_DYSYMTAB\n";
7380 outs() << " cmdsize " << dyst.cmdsize;
7381 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
7382 outs() << " Incorrect size\n";
7385 outs() << " ilocalsym " << dyst.ilocalsym;
7386 if (dyst.ilocalsym > nsyms)
7387 outs() << " (greater than the number of symbols)\n";
7390 outs() << " nlocalsym " << dyst.nlocalsym;
7392 big_size = dyst.ilocalsym;
7393 big_size += dyst.nlocalsym;
7394 if (big_size > nsyms)
7395 outs() << " (past the end of the symbol table)\n";
7398 outs() << " iextdefsym " << dyst.iextdefsym;
7399 if (dyst.iextdefsym > nsyms)
7400 outs() << " (greater than the number of symbols)\n";
7403 outs() << " nextdefsym " << dyst.nextdefsym;
7404 big_size = dyst.iextdefsym;
7405 big_size += dyst.nextdefsym;
7406 if (big_size > nsyms)
7407 outs() << " (past the end of the symbol table)\n";
7410 outs() << " iundefsym " << dyst.iundefsym;
7411 if (dyst.iundefsym > nsyms)
7412 outs() << " (greater than the number of symbols)\n";
7415 outs() << " nundefsym " << dyst.nundefsym;
7416 big_size = dyst.iundefsym;
7417 big_size += dyst.nundefsym;
7418 if (big_size > nsyms)
7419 outs() << " (past the end of the symbol table)\n";
7422 outs() << " tocoff " << dyst.tocoff;
7423 if (dyst.tocoff > object_size)
7424 outs() << " (past end of file)\n";
7427 outs() << " ntoc " << dyst.ntoc;
7428 big_size = dyst.ntoc;
7429 big_size *= sizeof(struct MachO::dylib_table_of_contents);
7430 big_size += dyst.tocoff;
7431 if (big_size > object_size)
7432 outs() << " (past end of file)\n";
7435 outs() << " modtaboff " << dyst.modtaboff;
7436 if (dyst.modtaboff > object_size)
7437 outs() << " (past end of file)\n";
7440 outs() << " nmodtab " << dyst.nmodtab;
7443 modtabend = dyst.nmodtab;
7444 modtabend *= sizeof(struct MachO::dylib_module_64);
7445 modtabend += dyst.modtaboff;
7447 modtabend = dyst.nmodtab;
7448 modtabend *= sizeof(struct MachO::dylib_module);
7449 modtabend += dyst.modtaboff;
7451 if (modtabend > object_size)
7452 outs() << " (past end of file)\n";
7455 outs() << " extrefsymoff " << dyst.extrefsymoff;
7456 if (dyst.extrefsymoff > object_size)
7457 outs() << " (past end of file)\n";
7460 outs() << " nextrefsyms " << dyst.nextrefsyms;
7461 big_size = dyst.nextrefsyms;
7462 big_size *= sizeof(struct MachO::dylib_reference);
7463 big_size += dyst.extrefsymoff;
7464 if (big_size > object_size)
7465 outs() << " (past end of file)\n";
7468 outs() << " indirectsymoff " << dyst.indirectsymoff;
7469 if (dyst.indirectsymoff > object_size)
7470 outs() << " (past end of file)\n";
7473 outs() << " nindirectsyms " << dyst.nindirectsyms;
7474 big_size = dyst.nindirectsyms;
7475 big_size *= sizeof(uint32_t);
7476 big_size += dyst.indirectsymoff;
7477 if (big_size > object_size)
7478 outs() << " (past end of file)\n";
7481 outs() << " extreloff " << dyst.extreloff;
7482 if (dyst.extreloff > object_size)
7483 outs() << " (past end of file)\n";
7486 outs() << " nextrel " << dyst.nextrel;
7487 big_size = dyst.nextrel;
7488 big_size *= sizeof(struct MachO::relocation_info);
7489 big_size += dyst.extreloff;
7490 if (big_size > object_size)
7491 outs() << " (past end of file)\n";
7494 outs() << " locreloff " << dyst.locreloff;
7495 if (dyst.locreloff > object_size)
7496 outs() << " (past end of file)\n";
7499 outs() << " nlocrel " << dyst.nlocrel;
7500 big_size = dyst.nlocrel;
7501 big_size *= sizeof(struct MachO::relocation_info);
7502 big_size += dyst.locreloff;
7503 if (big_size > object_size)
7504 outs() << " (past end of file)\n";
7509 static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
7510 uint32_t object_size) {
7511 if (dc.cmd == MachO::LC_DYLD_INFO)
7512 outs() << " cmd LC_DYLD_INFO\n";
7514 outs() << " cmd LC_DYLD_INFO_ONLY\n";
7515 outs() << " cmdsize " << dc.cmdsize;
7516 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
7517 outs() << " Incorrect size\n";
7520 outs() << " rebase_off " << dc.rebase_off;
7521 if (dc.rebase_off > object_size)
7522 outs() << " (past end of file)\n";
7525 outs() << " rebase_size " << dc.rebase_size;
7527 big_size = dc.rebase_off;
7528 big_size += dc.rebase_size;
7529 if (big_size > object_size)
7530 outs() << " (past end of file)\n";
7533 outs() << " bind_off " << dc.bind_off;
7534 if (dc.bind_off > object_size)
7535 outs() << " (past end of file)\n";
7538 outs() << " bind_size " << dc.bind_size;
7539 big_size = dc.bind_off;
7540 big_size += dc.bind_size;
7541 if (big_size > object_size)
7542 outs() << " (past end of file)\n";
7545 outs() << " weak_bind_off " << dc.weak_bind_off;
7546 if (dc.weak_bind_off > object_size)
7547 outs() << " (past end of file)\n";
7550 outs() << " weak_bind_size " << dc.weak_bind_size;
7551 big_size = dc.weak_bind_off;
7552 big_size += dc.weak_bind_size;
7553 if (big_size > object_size)
7554 outs() << " (past end of file)\n";
7557 outs() << " lazy_bind_off " << dc.lazy_bind_off;
7558 if (dc.lazy_bind_off > object_size)
7559 outs() << " (past end of file)\n";
7562 outs() << " lazy_bind_size " << dc.lazy_bind_size;
7563 big_size = dc.lazy_bind_off;
7564 big_size += dc.lazy_bind_size;
7565 if (big_size > object_size)
7566 outs() << " (past end of file)\n";
7569 outs() << " export_off " << dc.export_off;
7570 if (dc.export_off > object_size)
7571 outs() << " (past end of file)\n";
7574 outs() << " export_size " << dc.export_size;
7575 big_size = dc.export_off;
7576 big_size += dc.export_size;
7577 if (big_size > object_size)
7578 outs() << " (past end of file)\n";
7583 static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
7585 if (dyld.cmd == MachO::LC_ID_DYLINKER)
7586 outs() << " cmd LC_ID_DYLINKER\n";
7587 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
7588 outs() << " cmd LC_LOAD_DYLINKER\n";
7589 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
7590 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
7592 outs() << " cmd ?(" << dyld.cmd << ")\n";
7593 outs() << " cmdsize " << dyld.cmdsize;
7594 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
7595 outs() << " Incorrect size\n";
7598 if (dyld.name >= dyld.cmdsize)
7599 outs() << " name ?(bad offset " << dyld.name << ")\n";
7601 const char *P = (const char *)(Ptr) + dyld.name;
7602 outs() << " name " << P << " (offset " << dyld.name << ")\n";
7606 static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
7607 outs() << " cmd LC_UUID\n";
7608 outs() << " cmdsize " << uuid.cmdsize;
7609 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
7610 outs() << " Incorrect size\n";
7614 outs() << format("%02" PRIX32, uuid.uuid[0]);
7615 outs() << format("%02" PRIX32, uuid.uuid[1]);
7616 outs() << format("%02" PRIX32, uuid.uuid[2]);
7617 outs() << format("%02" PRIX32, uuid.uuid[3]);
7619 outs() << format("%02" PRIX32, uuid.uuid[4]);
7620 outs() << format("%02" PRIX32, uuid.uuid[5]);
7622 outs() << format("%02" PRIX32, uuid.uuid[6]);
7623 outs() << format("%02" PRIX32, uuid.uuid[7]);
7625 outs() << format("%02" PRIX32, uuid.uuid[8]);
7626 outs() << format("%02" PRIX32, uuid.uuid[9]);
7628 outs() << format("%02" PRIX32, uuid.uuid[10]);
7629 outs() << format("%02" PRIX32, uuid.uuid[11]);
7630 outs() << format("%02" PRIX32, uuid.uuid[12]);
7631 outs() << format("%02" PRIX32, uuid.uuid[13]);
7632 outs() << format("%02" PRIX32, uuid.uuid[14]);
7633 outs() << format("%02" PRIX32, uuid.uuid[15]);
7637 static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
7638 outs() << " cmd LC_RPATH\n";
7639 outs() << " cmdsize " << rpath.cmdsize;
7640 if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
7641 outs() << " Incorrect size\n";
7644 if (rpath.path >= rpath.cmdsize)
7645 outs() << " path ?(bad offset " << rpath.path << ")\n";
7647 const char *P = (const char *)(Ptr) + rpath.path;
7648 outs() << " path " << P << " (offset " << rpath.path << ")\n";
7652 static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
7653 if (vd.cmd == MachO::LC_VERSION_MIN_MACOSX)
7654 outs() << " cmd LC_VERSION_MIN_MACOSX\n";
7655 else if (vd.cmd == MachO::LC_VERSION_MIN_IPHONEOS)
7656 outs() << " cmd LC_VERSION_MIN_IPHONEOS\n";
7658 outs() << " cmd " << vd.cmd << " (?)\n";
7659 outs() << " cmdsize " << vd.cmdsize;
7660 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
7661 outs() << " Incorrect size\n";
7664 outs() << " version " << ((vd.version >> 16) & 0xffff) << "."
7665 << ((vd.version >> 8) & 0xff);
7666 if ((vd.version & 0xff) != 0)
7667 outs() << "." << (vd.version & 0xff);
7670 outs() << " sdk n/a";
7672 outs() << " sdk " << ((vd.sdk >> 16) & 0xffff) << "."
7673 << ((vd.sdk >> 8) & 0xff);
7675 if ((vd.sdk & 0xff) != 0)
7676 outs() << "." << (vd.sdk & 0xff);
7680 static void PrintSourceVersionCommand(MachO::source_version_command sd) {
7681 outs() << " cmd LC_SOURCE_VERSION\n";
7682 outs() << " cmdsize " << sd.cmdsize;
7683 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
7684 outs() << " Incorrect size\n";
7687 uint64_t a = (sd.version >> 40) & 0xffffff;
7688 uint64_t b = (sd.version >> 30) & 0x3ff;
7689 uint64_t c = (sd.version >> 20) & 0x3ff;
7690 uint64_t d = (sd.version >> 10) & 0x3ff;
7691 uint64_t e = sd.version & 0x3ff;
7692 outs() << " version " << a << "." << b;
7694 outs() << "." << c << "." << d << "." << e;
7696 outs() << "." << c << "." << d;
7702 static void PrintEntryPointCommand(MachO::entry_point_command ep) {
7703 outs() << " cmd LC_MAIN\n";
7704 outs() << " cmdsize " << ep.cmdsize;
7705 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
7706 outs() << " Incorrect size\n";
7709 outs() << " entryoff " << ep.entryoff << "\n";
7710 outs() << " stacksize " << ep.stacksize << "\n";
7713 static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
7714 uint32_t object_size) {
7715 outs() << " cmd LC_ENCRYPTION_INFO\n";
7716 outs() << " cmdsize " << ec.cmdsize;
7717 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
7718 outs() << " Incorrect size\n";
7721 outs() << " cryptoff " << ec.cryptoff;
7722 if (ec.cryptoff > object_size)
7723 outs() << " (past end of file)\n";
7726 outs() << " cryptsize " << ec.cryptsize;
7727 if (ec.cryptsize > object_size)
7728 outs() << " (past end of file)\n";
7731 outs() << " cryptid " << ec.cryptid << "\n";
7734 static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
7735 uint32_t object_size) {
7736 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
7737 outs() << " cmdsize " << ec.cmdsize;
7738 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
7739 outs() << " Incorrect size\n";
7742 outs() << " cryptoff " << ec.cryptoff;
7743 if (ec.cryptoff > object_size)
7744 outs() << " (past end of file)\n";
7747 outs() << " cryptsize " << ec.cryptsize;
7748 if (ec.cryptsize > object_size)
7749 outs() << " (past end of file)\n";
7752 outs() << " cryptid " << ec.cryptid << "\n";
7753 outs() << " pad " << ec.pad << "\n";
7756 static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
7758 outs() << " cmd LC_LINKER_OPTION\n";
7759 outs() << " cmdsize " << lo.cmdsize;
7760 if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
7761 outs() << " Incorrect size\n";
7764 outs() << " count " << lo.count << "\n";
7765 const char *string = Ptr + sizeof(struct MachO::linker_option_command);
7766 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
7769 while (*string == '\0' && left > 0) {
7775 outs() << " string #" << i << " " << format("%.*s\n", left, string);
7776 uint32_t NullPos = StringRef(string, left).find('\0');
7777 uint32_t len = std::min(NullPos, left) + 1;
7783 outs() << " count " << lo.count << " does not match number of strings "
7787 static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
7789 outs() << " cmd LC_SUB_FRAMEWORK\n";
7790 outs() << " cmdsize " << sub.cmdsize;
7791 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
7792 outs() << " Incorrect size\n";
7795 if (sub.umbrella < sub.cmdsize) {
7796 const char *P = Ptr + sub.umbrella;
7797 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
7799 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
7803 static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
7805 outs() << " cmd LC_SUB_UMBRELLA\n";
7806 outs() << " cmdsize " << sub.cmdsize;
7807 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
7808 outs() << " Incorrect size\n";
7811 if (sub.sub_umbrella < sub.cmdsize) {
7812 const char *P = Ptr + sub.sub_umbrella;
7813 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
7815 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
7819 static void PrintSubLibraryCommand(MachO::sub_library_command sub,
7821 outs() << " cmd LC_SUB_LIBRARY\n";
7822 outs() << " cmdsize " << sub.cmdsize;
7823 if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
7824 outs() << " Incorrect size\n";
7827 if (sub.sub_library < sub.cmdsize) {
7828 const char *P = Ptr + sub.sub_library;
7829 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
7831 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
7835 static void PrintSubClientCommand(MachO::sub_client_command sub,
7837 outs() << " cmd LC_SUB_CLIENT\n";
7838 outs() << " cmdsize " << sub.cmdsize;
7839 if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
7840 outs() << " Incorrect size\n";
7843 if (sub.client < sub.cmdsize) {
7844 const char *P = Ptr + sub.client;
7845 outs() << " client " << P << " (offset " << sub.client << ")\n";
7847 outs() << " client ?(bad offset " << sub.client << ")\n";
7851 static void PrintRoutinesCommand(MachO::routines_command r) {
7852 outs() << " cmd LC_ROUTINES\n";
7853 outs() << " cmdsize " << r.cmdsize;
7854 if (r.cmdsize != sizeof(struct MachO::routines_command))
7855 outs() << " Incorrect size\n";
7858 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
7859 outs() << " init_module " << r.init_module << "\n";
7860 outs() << " reserved1 " << r.reserved1 << "\n";
7861 outs() << " reserved2 " << r.reserved2 << "\n";
7862 outs() << " reserved3 " << r.reserved3 << "\n";
7863 outs() << " reserved4 " << r.reserved4 << "\n";
7864 outs() << " reserved5 " << r.reserved5 << "\n";
7865 outs() << " reserved6 " << r.reserved6 << "\n";
7868 static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
7869 outs() << " cmd LC_ROUTINES_64\n";
7870 outs() << " cmdsize " << r.cmdsize;
7871 if (r.cmdsize != sizeof(struct MachO::routines_command_64))
7872 outs() << " Incorrect size\n";
7875 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
7876 outs() << " init_module " << r.init_module << "\n";
7877 outs() << " reserved1 " << r.reserved1 << "\n";
7878 outs() << " reserved2 " << r.reserved2 << "\n";
7879 outs() << " reserved3 " << r.reserved3 << "\n";
7880 outs() << " reserved4 " << r.reserved4 << "\n";
7881 outs() << " reserved5 " << r.reserved5 << "\n";
7882 outs() << " reserved6 " << r.reserved6 << "\n";
7885 static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
7886 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
7887 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
7888 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
7889 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
7890 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
7891 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
7892 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
7893 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
7894 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
7895 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
7896 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
7897 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
7898 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
7899 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
7900 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
7901 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
7902 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
7903 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
7904 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
7905 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
7906 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
7909 static void Print_mmst_reg(MachO::mmst_reg_t &r) {
7911 outs() << "\t mmst_reg ";
7912 for (f = 0; f < 10; f++)
7913 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
7915 outs() << "\t mmst_rsrv ";
7916 for (f = 0; f < 6; f++)
7917 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
7921 static void Print_xmm_reg(MachO::xmm_reg_t &r) {
7923 outs() << "\t xmm_reg ";
7924 for (f = 0; f < 16; f++)
7925 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
7929 static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
7930 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
7931 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
7932 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
7933 outs() << " denorm " << fpu.fpu_fcw.denorm;
7934 outs() << " zdiv " << fpu.fpu_fcw.zdiv;
7935 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
7936 outs() << " undfl " << fpu.fpu_fcw.undfl;
7937 outs() << " precis " << fpu.fpu_fcw.precis << "\n";
7938 outs() << "\t\t pc ";
7939 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
7940 outs() << "FP_PREC_24B ";
7941 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
7942 outs() << "FP_PREC_53B ";
7943 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
7944 outs() << "FP_PREC_64B ";
7946 outs() << fpu.fpu_fcw.pc << " ";
7948 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
7949 outs() << "FP_RND_NEAR ";
7950 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
7951 outs() << "FP_RND_DOWN ";
7952 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
7953 outs() << "FP_RND_UP ";
7954 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
7955 outs() << "FP_CHOP ";
7957 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
7958 outs() << " denorm " << fpu.fpu_fsw.denorm;
7959 outs() << " zdiv " << fpu.fpu_fsw.zdiv;
7960 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
7961 outs() << " undfl " << fpu.fpu_fsw.undfl;
7962 outs() << " precis " << fpu.fpu_fsw.precis;
7963 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
7964 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
7965 outs() << " c0 " << fpu.fpu_fsw.c0;
7966 outs() << " c1 " << fpu.fpu_fsw.c1;
7967 outs() << " c2 " << fpu.fpu_fsw.c2;
7968 outs() << " tos " << fpu.fpu_fsw.tos;
7969 outs() << " c3 " << fpu.fpu_fsw.c3;
7970 outs() << " busy " << fpu.fpu_fsw.busy << "\n";
7971 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
7972 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
7973 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
7974 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
7975 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
7976 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
7977 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
7978 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
7979 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
7980 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
7981 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
7983 outs() << "\t fpu_stmm0:\n";
7984 Print_mmst_reg(fpu.fpu_stmm0);
7985 outs() << "\t fpu_stmm1:\n";
7986 Print_mmst_reg(fpu.fpu_stmm1);
7987 outs() << "\t fpu_stmm2:\n";
7988 Print_mmst_reg(fpu.fpu_stmm2);
7989 outs() << "\t fpu_stmm3:\n";
7990 Print_mmst_reg(fpu.fpu_stmm3);
7991 outs() << "\t fpu_stmm4:\n";
7992 Print_mmst_reg(fpu.fpu_stmm4);
7993 outs() << "\t fpu_stmm5:\n";
7994 Print_mmst_reg(fpu.fpu_stmm5);
7995 outs() << "\t fpu_stmm6:\n";
7996 Print_mmst_reg(fpu.fpu_stmm6);
7997 outs() << "\t fpu_stmm7:\n";
7998 Print_mmst_reg(fpu.fpu_stmm7);
7999 outs() << "\t fpu_xmm0:\n";
8000 Print_xmm_reg(fpu.fpu_xmm0);
8001 outs() << "\t fpu_xmm1:\n";
8002 Print_xmm_reg(fpu.fpu_xmm1);
8003 outs() << "\t fpu_xmm2:\n";
8004 Print_xmm_reg(fpu.fpu_xmm2);
8005 outs() << "\t fpu_xmm3:\n";
8006 Print_xmm_reg(fpu.fpu_xmm3);
8007 outs() << "\t fpu_xmm4:\n";
8008 Print_xmm_reg(fpu.fpu_xmm4);
8009 outs() << "\t fpu_xmm5:\n";
8010 Print_xmm_reg(fpu.fpu_xmm5);
8011 outs() << "\t fpu_xmm6:\n";
8012 Print_xmm_reg(fpu.fpu_xmm6);
8013 outs() << "\t fpu_xmm7:\n";
8014 Print_xmm_reg(fpu.fpu_xmm7);
8015 outs() << "\t fpu_xmm8:\n";
8016 Print_xmm_reg(fpu.fpu_xmm8);
8017 outs() << "\t fpu_xmm9:\n";
8018 Print_xmm_reg(fpu.fpu_xmm9);
8019 outs() << "\t fpu_xmm10:\n";
8020 Print_xmm_reg(fpu.fpu_xmm10);
8021 outs() << "\t fpu_xmm11:\n";
8022 Print_xmm_reg(fpu.fpu_xmm11);
8023 outs() << "\t fpu_xmm12:\n";
8024 Print_xmm_reg(fpu.fpu_xmm12);
8025 outs() << "\t fpu_xmm13:\n";
8026 Print_xmm_reg(fpu.fpu_xmm13);
8027 outs() << "\t fpu_xmm14:\n";
8028 Print_xmm_reg(fpu.fpu_xmm14);
8029 outs() << "\t fpu_xmm15:\n";
8030 Print_xmm_reg(fpu.fpu_xmm15);
8031 outs() << "\t fpu_rsrv4:\n";
8032 for (uint32_t f = 0; f < 6; f++) {
8034 for (uint32_t g = 0; g < 16; g++)
8035 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
8038 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
8042 static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
8043 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
8044 outs() << " err " << format("0x%08" PRIx32, exc64.err);
8045 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
8048 static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
8049 bool isLittleEndian, uint32_t cputype) {
8050 if (t.cmd == MachO::LC_THREAD)
8051 outs() << " cmd LC_THREAD\n";
8052 else if (t.cmd == MachO::LC_UNIXTHREAD)
8053 outs() << " cmd LC_UNIXTHREAD\n";
8055 outs() << " cmd " << t.cmd << " (unknown)\n";
8056 outs() << " cmdsize " << t.cmdsize;
8057 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
8058 outs() << " Incorrect size\n";
8062 const char *begin = Ptr + sizeof(struct MachO::thread_command);
8063 const char *end = Ptr + t.cmdsize;
8064 uint32_t flavor, count, left;
8065 if (cputype == MachO::CPU_TYPE_X86_64) {
8066 while (begin < end) {
8067 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8068 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8069 begin += sizeof(uint32_t);
8074 if (isLittleEndian != sys::IsLittleEndianHost)
8075 sys::swapByteOrder(flavor);
8076 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8077 memcpy((char *)&count, begin, sizeof(uint32_t));
8078 begin += sizeof(uint32_t);
8083 if (isLittleEndian != sys::IsLittleEndianHost)
8084 sys::swapByteOrder(count);
8085 if (flavor == MachO::x86_THREAD_STATE64) {
8086 outs() << " flavor x86_THREAD_STATE64\n";
8087 if (count == MachO::x86_THREAD_STATE64_COUNT)
8088 outs() << " count x86_THREAD_STATE64_COUNT\n";
8090 outs() << " count " << count
8091 << " (not x86_THREAD_STATE64_COUNT)\n";
8092 MachO::x86_thread_state64_t cpu64;
8094 if (left >= sizeof(MachO::x86_thread_state64_t)) {
8095 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
8096 begin += sizeof(MachO::x86_thread_state64_t);
8098 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
8099 memcpy(&cpu64, begin, left);
8102 if (isLittleEndian != sys::IsLittleEndianHost)
8104 Print_x86_thread_state64_t(cpu64);
8105 } else if (flavor == MachO::x86_THREAD_STATE) {
8106 outs() << " flavor x86_THREAD_STATE\n";
8107 if (count == MachO::x86_THREAD_STATE_COUNT)
8108 outs() << " count x86_THREAD_STATE_COUNT\n";
8110 outs() << " count " << count
8111 << " (not x86_THREAD_STATE_COUNT)\n";
8112 struct MachO::x86_thread_state_t ts;
8114 if (left >= sizeof(MachO::x86_thread_state_t)) {
8115 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
8116 begin += sizeof(MachO::x86_thread_state_t);
8118 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
8119 memcpy(&ts, begin, left);
8122 if (isLittleEndian != sys::IsLittleEndianHost)
8124 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
8125 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
8126 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
8127 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
8129 outs() << "tsh.count " << ts.tsh.count
8130 << " (not x86_THREAD_STATE64_COUNT\n";
8131 Print_x86_thread_state64_t(ts.uts.ts64);
8133 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
8134 << ts.tsh.count << "\n";
8136 } else if (flavor == MachO::x86_FLOAT_STATE) {
8137 outs() << " flavor x86_FLOAT_STATE\n";
8138 if (count == MachO::x86_FLOAT_STATE_COUNT)
8139 outs() << " count x86_FLOAT_STATE_COUNT\n";
8141 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
8142 struct MachO::x86_float_state_t fs;
8144 if (left >= sizeof(MachO::x86_float_state_t)) {
8145 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
8146 begin += sizeof(MachO::x86_float_state_t);
8148 memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
8149 memcpy(&fs, begin, left);
8152 if (isLittleEndian != sys::IsLittleEndianHost)
8154 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
8155 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
8156 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
8157 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
8159 outs() << "fsh.count " << fs.fsh.count
8160 << " (not x86_FLOAT_STATE64_COUNT\n";
8161 Print_x86_float_state_t(fs.ufs.fs64);
8163 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
8164 << fs.fsh.count << "\n";
8166 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
8167 outs() << " flavor x86_EXCEPTION_STATE\n";
8168 if (count == MachO::x86_EXCEPTION_STATE_COUNT)
8169 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
8171 outs() << " count " << count
8172 << " (not x86_EXCEPTION_STATE_COUNT)\n";
8173 struct MachO::x86_exception_state_t es;
8175 if (left >= sizeof(MachO::x86_exception_state_t)) {
8176 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
8177 begin += sizeof(MachO::x86_exception_state_t);
8179 memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
8180 memcpy(&es, begin, left);
8183 if (isLittleEndian != sys::IsLittleEndianHost)
8185 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
8186 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
8187 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
8188 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
8190 outs() << "\t esh.count " << es.esh.count
8191 << " (not x86_EXCEPTION_STATE64_COUNT\n";
8192 Print_x86_exception_state_t(es.ues.es64);
8194 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
8195 << es.esh.count << "\n";
8198 outs() << " flavor " << flavor << " (unknown)\n";
8199 outs() << " count " << count << "\n";
8200 outs() << " state (unknown)\n";
8201 begin += count * sizeof(uint32_t);
8205 while (begin < end) {
8206 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8207 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8208 begin += sizeof(uint32_t);
8213 if (isLittleEndian != sys::IsLittleEndianHost)
8214 sys::swapByteOrder(flavor);
8215 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8216 memcpy((char *)&count, begin, sizeof(uint32_t));
8217 begin += sizeof(uint32_t);
8222 if (isLittleEndian != sys::IsLittleEndianHost)
8223 sys::swapByteOrder(count);
8224 outs() << " flavor " << flavor << "\n";
8225 outs() << " count " << count << "\n";
8226 outs() << " state (Unknown cputype/cpusubtype)\n";
8227 begin += count * sizeof(uint32_t);
8232 static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
8233 if (dl.cmd == MachO::LC_ID_DYLIB)
8234 outs() << " cmd LC_ID_DYLIB\n";
8235 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
8236 outs() << " cmd LC_LOAD_DYLIB\n";
8237 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
8238 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
8239 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
8240 outs() << " cmd LC_REEXPORT_DYLIB\n";
8241 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
8242 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
8243 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
8244 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
8246 outs() << " cmd " << dl.cmd << " (unknown)\n";
8247 outs() << " cmdsize " << dl.cmdsize;
8248 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
8249 outs() << " Incorrect size\n";
8252 if (dl.dylib.name < dl.cmdsize) {
8253 const char *P = (const char *)(Ptr) + dl.dylib.name;
8254 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
8256 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
8258 outs() << " time stamp " << dl.dylib.timestamp << " ";
8259 time_t t = dl.dylib.timestamp;
8260 outs() << ctime(&t);
8261 outs() << " current version ";
8262 if (dl.dylib.current_version == 0xffffffff)
8265 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
8266 << ((dl.dylib.current_version >> 8) & 0xff) << "."
8267 << (dl.dylib.current_version & 0xff) << "\n";
8268 outs() << "compatibility version ";
8269 if (dl.dylib.compatibility_version == 0xffffffff)
8272 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
8273 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
8274 << (dl.dylib.compatibility_version & 0xff) << "\n";
8277 static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
8278 uint32_t object_size) {
8279 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
8280 outs() << " cmd LC_FUNCTION_STARTS\n";
8281 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
8282 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
8283 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
8284 outs() << " cmd LC_FUNCTION_STARTS\n";
8285 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
8286 outs() << " cmd LC_DATA_IN_CODE\n";
8287 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
8288 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
8289 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
8290 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
8292 outs() << " cmd " << ld.cmd << " (?)\n";
8293 outs() << " cmdsize " << ld.cmdsize;
8294 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
8295 outs() << " Incorrect size\n";
8298 outs() << " dataoff " << ld.dataoff;
8299 if (ld.dataoff > object_size)
8300 outs() << " (past end of file)\n";
8303 outs() << " datasize " << ld.datasize;
8304 uint64_t big_size = ld.dataoff;
8305 big_size += ld.datasize;
8306 if (big_size > object_size)
8307 outs() << " (past end of file)\n";
8312 static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
8313 uint32_t cputype, bool verbose) {
8314 StringRef Buf = Obj->getData();
8316 for (const auto &Command : Obj->load_commands()) {
8317 outs() << "Load command " << Index++ << "\n";
8318 if (Command.C.cmd == MachO::LC_SEGMENT) {
8319 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
8320 const char *sg_segname = SLC.segname;
8321 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
8322 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
8323 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
8325 for (unsigned j = 0; j < SLC.nsects; j++) {
8326 MachO::section S = Obj->getSection(Command, j);
8327 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
8328 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
8329 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
8331 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
8332 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
8333 const char *sg_segname = SLC_64.segname;
8334 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
8335 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
8336 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
8337 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
8338 for (unsigned j = 0; j < SLC_64.nsects; j++) {
8339 MachO::section_64 S_64 = Obj->getSection64(Command, j);
8340 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
8341 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
8342 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
8343 sg_segname, filetype, Buf.size(), verbose);
8345 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
8346 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
8347 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
8348 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
8349 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
8350 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
8351 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
8353 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
8354 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
8355 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
8356 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
8357 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
8358 Command.C.cmd == MachO::LC_ID_DYLINKER ||
8359 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
8360 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
8361 PrintDyldLoadCommand(Dyld, Command.Ptr);
8362 } else if (Command.C.cmd == MachO::LC_UUID) {
8363 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
8364 PrintUuidLoadCommand(Uuid);
8365 } else if (Command.C.cmd == MachO::LC_RPATH) {
8366 MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
8367 PrintRpathLoadCommand(Rpath, Command.Ptr);
8368 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
8369 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS) {
8370 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
8371 PrintVersionMinLoadCommand(Vd);
8372 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
8373 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
8374 PrintSourceVersionCommand(Sd);
8375 } else if (Command.C.cmd == MachO::LC_MAIN) {
8376 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
8377 PrintEntryPointCommand(Ep);
8378 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
8379 MachO::encryption_info_command Ei =
8380 Obj->getEncryptionInfoCommand(Command);
8381 PrintEncryptionInfoCommand(Ei, Buf.size());
8382 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
8383 MachO::encryption_info_command_64 Ei =
8384 Obj->getEncryptionInfoCommand64(Command);
8385 PrintEncryptionInfoCommand64(Ei, Buf.size());
8386 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
8387 MachO::linker_option_command Lo =
8388 Obj->getLinkerOptionLoadCommand(Command);
8389 PrintLinkerOptionCommand(Lo, Command.Ptr);
8390 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
8391 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
8392 PrintSubFrameworkCommand(Sf, Command.Ptr);
8393 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
8394 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
8395 PrintSubUmbrellaCommand(Sf, Command.Ptr);
8396 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
8397 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
8398 PrintSubLibraryCommand(Sl, Command.Ptr);
8399 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
8400 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
8401 PrintSubClientCommand(Sc, Command.Ptr);
8402 } else if (Command.C.cmd == MachO::LC_ROUTINES) {
8403 MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
8404 PrintRoutinesCommand(Rc);
8405 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
8406 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
8407 PrintRoutinesCommand64(Rc);
8408 } else if (Command.C.cmd == MachO::LC_THREAD ||
8409 Command.C.cmd == MachO::LC_UNIXTHREAD) {
8410 MachO::thread_command Tc = Obj->getThreadCommand(Command);
8411 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
8412 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
8413 Command.C.cmd == MachO::LC_ID_DYLIB ||
8414 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
8415 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
8416 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
8417 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
8418 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
8419 PrintDylibCommand(Dl, Command.Ptr);
8420 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
8421 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
8422 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
8423 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
8424 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
8425 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
8426 MachO::linkedit_data_command Ld =
8427 Obj->getLinkeditDataLoadCommand(Command);
8428 PrintLinkEditDataCommand(Ld, Buf.size());
8430 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
8432 outs() << " cmdsize " << Command.C.cmdsize << "\n";
8433 // TODO: get and print the raw bytes of the load command.
8435 // TODO: print all the other kinds of load commands.
8439 static void getAndPrintMachHeader(const MachOObjectFile *Obj,
8440 uint32_t &filetype, uint32_t &cputype,
8442 if (Obj->is64Bit()) {
8443 MachO::mach_header_64 H_64;
8444 H_64 = Obj->getHeader64();
8445 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
8446 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
8447 filetype = H_64.filetype;
8448 cputype = H_64.cputype;
8450 MachO::mach_header H;
8451 H = Obj->getHeader();
8452 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
8453 H.sizeofcmds, H.flags, verbose);
8454 filetype = H.filetype;
8455 cputype = H.cputype;
8459 void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
8460 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
8461 uint32_t filetype = 0;
8462 uint32_t cputype = 0;
8463 getAndPrintMachHeader(file, filetype, cputype, !NonVerbose);
8464 PrintLoadCommands(file, filetype, cputype, !NonVerbose);
8467 //===----------------------------------------------------------------------===//
8468 // export trie dumping
8469 //===----------------------------------------------------------------------===//
8471 void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
8472 for (const llvm::object::ExportEntry &Entry : Obj->exports()) {
8473 uint64_t Flags = Entry.flags();
8474 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
8475 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
8476 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8477 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
8478 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8479 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
8480 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
8482 outs() << "[re-export] ";
8484 outs() << format("0x%08llX ",
8485 Entry.address()); // FIXME:add in base address
8486 outs() << Entry.name();
8487 if (WeakDef || ThreadLocal || Resolver || Abs) {
8488 bool NeedsComma = false;
8491 outs() << "weak_def";
8497 outs() << "per-thread";
8503 outs() << "absolute";
8509 outs() << format("resolver=0x%08llX", Entry.other());
8515 StringRef DylibName = "unknown";
8516 int Ordinal = Entry.other() - 1;
8517 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
8518 if (Entry.otherName().empty())
8519 outs() << " (from " << DylibName << ")";
8521 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
8527 //===----------------------------------------------------------------------===//
8528 // rebase table dumping
8529 //===----------------------------------------------------------------------===//
8534 SegInfo(const object::MachOObjectFile *Obj);
8536 StringRef segmentName(uint32_t SegIndex);
8537 StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset);
8538 uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
8541 struct SectionInfo {
8544 StringRef SectionName;
8545 StringRef SegmentName;
8546 uint64_t OffsetInSegment;
8547 uint64_t SegmentStartAddress;
8548 uint32_t SegmentIndex;
8550 const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset);
8551 SmallVector<SectionInfo, 32> Sections;
8555 SegInfo::SegInfo(const object::MachOObjectFile *Obj) {
8556 // Build table of sections so segIndex/offset pairs can be translated.
8557 uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
8558 StringRef CurSegName;
8559 uint64_t CurSegAddress;
8560 for (const SectionRef &Section : Obj->sections()) {
8562 if (error(Section.getName(Info.SectionName)))
8564 Info.Address = Section.getAddress();
8565 Info.Size = Section.getSize();
8567 Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
8568 if (!Info.SegmentName.equals(CurSegName)) {
8570 CurSegName = Info.SegmentName;
8571 CurSegAddress = Info.Address;
8573 Info.SegmentIndex = CurSegIndex - 1;
8574 Info.OffsetInSegment = Info.Address - CurSegAddress;
8575 Info.SegmentStartAddress = CurSegAddress;
8576 Sections.push_back(Info);
8580 StringRef SegInfo::segmentName(uint32_t SegIndex) {
8581 for (const SectionInfo &SI : Sections) {
8582 if (SI.SegmentIndex == SegIndex)
8583 return SI.SegmentName;
8585 llvm_unreachable("invalid segIndex");
8588 const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex,
8589 uint64_t OffsetInSeg) {
8590 for (const SectionInfo &SI : Sections) {
8591 if (SI.SegmentIndex != SegIndex)
8593 if (SI.OffsetInSegment > OffsetInSeg)
8595 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
8599 llvm_unreachable("segIndex and offset not in any section");
8602 StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) {
8603 return findSection(SegIndex, OffsetInSeg).SectionName;
8606 uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) {
8607 const SectionInfo &SI = findSection(SegIndex, OffsetInSeg);
8608 return SI.SegmentStartAddress + OffsetInSeg;
8611 void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) {
8612 // Build table of sections so names can used in final output.
8613 SegInfo sectionTable(Obj);
8615 outs() << "segment section address type\n";
8616 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) {
8617 uint32_t SegIndex = Entry.segmentIndex();
8618 uint64_t OffsetInSeg = Entry.segmentOffset();
8619 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8620 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8621 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8623 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
8624 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
8625 SegmentName.str().c_str(), SectionName.str().c_str(),
8626 Address, Entry.typeName().str().c_str());
8630 static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
8631 StringRef DylibName;
8633 case MachO::BIND_SPECIAL_DYLIB_SELF:
8634 return "this-image";
8635 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
8636 return "main-executable";
8637 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
8638 return "flat-namespace";
8641 std::error_code EC =
8642 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
8644 return "<<bad library ordinal>>";
8648 return "<<unknown special ordinal>>";
8651 //===----------------------------------------------------------------------===//
8652 // bind table dumping
8653 //===----------------------------------------------------------------------===//
8655 void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) {
8656 // Build table of sections so names can used in final output.
8657 SegInfo sectionTable(Obj);
8659 outs() << "segment section address type "
8660 "addend dylib symbol\n";
8661 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) {
8662 uint32_t SegIndex = Entry.segmentIndex();
8663 uint64_t OffsetInSeg = Entry.segmentOffset();
8664 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8665 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8666 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8668 // Table lines look like:
8669 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
8671 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
8672 Attr = " (weak_import)";
8673 outs() << left_justify(SegmentName, 8) << " "
8674 << left_justify(SectionName, 18) << " "
8675 << format_hex(Address, 10, true) << " "
8676 << left_justify(Entry.typeName(), 8) << " "
8677 << format_decimal(Entry.addend(), 8) << " "
8678 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
8679 << Entry.symbolName() << Attr << "\n";
8683 //===----------------------------------------------------------------------===//
8684 // lazy bind table dumping
8685 //===----------------------------------------------------------------------===//
8687 void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) {
8688 // Build table of sections so names can used in final output.
8689 SegInfo sectionTable(Obj);
8691 outs() << "segment section address "
8693 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) {
8694 uint32_t SegIndex = Entry.segmentIndex();
8695 uint64_t OffsetInSeg = Entry.segmentOffset();
8696 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8697 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8698 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8700 // Table lines look like:
8701 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
8702 outs() << left_justify(SegmentName, 8) << " "
8703 << left_justify(SectionName, 18) << " "
8704 << format_hex(Address, 10, true) << " "
8705 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
8706 << Entry.symbolName() << "\n";
8710 //===----------------------------------------------------------------------===//
8711 // weak bind table dumping
8712 //===----------------------------------------------------------------------===//
8714 void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) {
8715 // Build table of sections so names can used in final output.
8716 SegInfo sectionTable(Obj);
8718 outs() << "segment section address "
8719 "type addend symbol\n";
8720 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) {
8721 // Strong symbols don't have a location to update.
8722 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
8723 outs() << " strong "
8724 << Entry.symbolName() << "\n";
8727 uint32_t SegIndex = Entry.segmentIndex();
8728 uint64_t OffsetInSeg = Entry.segmentOffset();
8729 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8730 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8731 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8733 // Table lines look like:
8734 // __DATA __data 0x00001000 pointer 0 _foo
8735 outs() << left_justify(SegmentName, 8) << " "
8736 << left_justify(SectionName, 18) << " "
8737 << format_hex(Address, 10, true) << " "
8738 << left_justify(Entry.typeName(), 8) << " "
8739 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
8744 // get_dyld_bind_info_symbolname() is used for disassembly and passed an
8745 // address, ReferenceValue, in the Mach-O file and looks in the dyld bind
8746 // information for that address. If the address is found its binding symbol
8747 // name is returned. If not nullptr is returned.
8748 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
8749 struct DisassembleInfo *info) {
8750 if (info->bindtable == nullptr) {
8751 info->bindtable = new (BindTable);
8752 SegInfo sectionTable(info->O);
8753 for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable()) {
8754 uint32_t SegIndex = Entry.segmentIndex();
8755 uint64_t OffsetInSeg = Entry.segmentOffset();
8756 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8757 const char *SymbolName = nullptr;
8758 StringRef name = Entry.symbolName();
8760 SymbolName = name.data();
8761 info->bindtable->push_back(std::make_pair(Address, SymbolName));
8764 for (bind_table_iterator BI = info->bindtable->begin(),
8765 BE = info->bindtable->end();
8767 uint64_t Address = BI->first;
8768 if (ReferenceValue == Address) {
8769 const char *SymbolName = BI->second;