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)"));
101 llvm::InfoPlist("info-plist",
102 cl::desc("Print the info plist section as strings for "
103 "Mach-O objects (requires -macho)"));
106 llvm::DylibsUsed("dylibs-used",
107 cl::desc("Print the shared libraries used for linked "
108 "Mach-O files (requires -macho)"));
111 llvm::DylibId("dylib-id",
112 cl::desc("Print the shared library's id for the dylib Mach-O "
113 "file (requires -macho)"));
116 llvm::NonVerbose("non-verbose",
117 cl::desc("Print the info for Mach-O objects in "
118 "non-verbose or numeric form (requires -macho)"));
121 llvm::ObjcMetaData("objc-meta-data",
122 cl::desc("Print the Objective-C runtime meta data for "
123 "Mach-O files (requires -macho)"));
125 cl::opt<std::string> llvm::DisSymName(
127 cl::desc("disassemble just this symbol's instructions (requires -macho"));
129 static cl::opt<bool> NoSymbolicOperands(
130 "no-symbolic-operands",
131 cl::desc("do not symbolic operands when disassembling (requires -macho)"));
133 static cl::list<std::string>
134 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
136 bool ArchAll = false;
138 static std::string ThumbTripleName;
140 static const Target *GetTarget(const MachOObjectFile *MachOObj,
141 const char **McpuDefault,
142 const Target **ThumbTarget) {
143 // Figure out the target triple.
144 if (TripleName.empty()) {
145 llvm::Triple TT("unknown-unknown-unknown");
146 llvm::Triple ThumbTriple = Triple();
147 TT = MachOObj->getArch(McpuDefault, &ThumbTriple);
148 TripleName = TT.str();
149 ThumbTripleName = ThumbTriple.str();
152 // Get the target specific parser.
154 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
155 if (TheTarget && ThumbTripleName.empty())
158 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
162 errs() << "llvm-objdump: error: unable to get target for '";
164 errs() << TripleName;
166 errs() << ThumbTripleName;
167 errs() << "', see --version and --triple.\n";
171 struct SymbolSorter {
172 bool operator()(const SymbolRef &A, const SymbolRef &B) {
173 uint64_t AAddr = (A.getType() != SymbolRef::ST_Function) ? 0 : A.getValue();
174 uint64_t BAddr = (B.getType() != SymbolRef::ST_Function) ? 0 : B.getValue();
175 return AAddr < BAddr;
179 // Types for the storted data in code table that is built before disassembly
180 // and the predicate function to sort them.
181 typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
182 typedef std::vector<DiceTableEntry> DiceTable;
183 typedef DiceTable::iterator dice_table_iterator;
185 // This is used to search for a data in code table entry for the PC being
186 // disassembled. The j parameter has the PC in j.first. A single data in code
187 // table entry can cover many bytes for each of its Kind's. So if the offset,
188 // aka the i.first value, of the data in code table entry plus its Length
189 // covers the PC being searched for this will return true. If not it will
191 static bool compareDiceTableEntries(const DiceTableEntry &i,
192 const DiceTableEntry &j) {
194 i.second.getLength(Length);
196 return j.first >= i.first && j.first < i.first + Length;
199 static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
200 unsigned short Kind) {
201 uint32_t Value, Size = 1;
205 case MachO::DICE_KIND_DATA:
208 dumpBytes(ArrayRef<uint8_t>(bytes, 4), outs());
209 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
210 outs() << "\t.long " << Value;
212 } else if (Length >= 2) {
214 dumpBytes(ArrayRef<uint8_t>(bytes, 2), outs());
215 Value = bytes[1] << 8 | bytes[0];
216 outs() << "\t.short " << Value;
220 dumpBytes(ArrayRef<uint8_t>(bytes, 2), outs());
222 outs() << "\t.byte " << Value;
225 if (Kind == MachO::DICE_KIND_DATA)
226 outs() << "\t@ KIND_DATA\n";
228 outs() << "\t@ data in code kind = " << Kind << "\n";
230 case MachO::DICE_KIND_JUMP_TABLE8:
232 dumpBytes(ArrayRef<uint8_t>(bytes, 1), outs());
234 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
237 case MachO::DICE_KIND_JUMP_TABLE16:
239 dumpBytes(ArrayRef<uint8_t>(bytes, 2), outs());
240 Value = bytes[1] << 8 | bytes[0];
241 outs() << "\t.short " << format("%5u", Value & 0xffff)
242 << "\t@ KIND_JUMP_TABLE16\n";
245 case MachO::DICE_KIND_JUMP_TABLE32:
246 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
248 dumpBytes(ArrayRef<uint8_t>(bytes, 4), outs());
249 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
250 outs() << "\t.long " << Value;
251 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
252 outs() << "\t@ KIND_JUMP_TABLE32\n";
254 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
261 static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
262 std::vector<SectionRef> &Sections,
263 std::vector<SymbolRef> &Symbols,
264 SmallVectorImpl<uint64_t> &FoundFns,
265 uint64_t &BaseSegmentAddress) {
266 for (const SymbolRef &Symbol : MachOObj->symbols()) {
267 ErrorOr<StringRef> SymName = Symbol.getName();
268 if (std::error_code EC = SymName.getError())
269 report_fatal_error(EC.message());
270 if (!SymName->startswith("ltmp"))
271 Symbols.push_back(Symbol);
274 for (const SectionRef &Section : MachOObj->sections()) {
276 Section.getName(SectName);
277 Sections.push_back(Section);
280 bool BaseSegmentAddressSet = false;
281 for (const auto &Command : MachOObj->load_commands()) {
282 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
283 // We found a function starts segment, parse the addresses for later
285 MachO::linkedit_data_command LLC =
286 MachOObj->getLinkeditDataLoadCommand(Command);
288 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
289 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
290 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
291 StringRef SegName = SLC.segname;
292 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
293 BaseSegmentAddressSet = true;
294 BaseSegmentAddress = SLC.vmaddr;
300 static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
301 uint32_t n, uint32_t count,
302 uint32_t stride, uint64_t addr) {
303 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
304 uint32_t nindirectsyms = Dysymtab.nindirectsyms;
305 if (n > nindirectsyms)
306 outs() << " (entries start past the end of the indirect symbol "
307 "table) (reserved1 field greater than the table size)";
308 else if (n + count > nindirectsyms)
309 outs() << " (entries extends past the end of the indirect symbol "
312 uint32_t cputype = O->getHeader().cputype;
313 if (cputype & MachO::CPU_ARCH_ABI64)
314 outs() << "address index";
316 outs() << "address index";
321 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
322 if (cputype & MachO::CPU_ARCH_ABI64)
323 outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
325 outs() << format("0x%08" PRIx32, addr + j * stride) << " ";
326 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
327 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
328 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
332 if (indirect_symbol ==
333 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
334 outs() << "LOCAL ABSOLUTE\n";
337 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
338 outs() << "ABSOLUTE\n";
341 outs() << format("%5u ", indirect_symbol);
343 MachO::symtab_command Symtab = O->getSymtabLoadCommand();
344 if (indirect_symbol < Symtab.nsyms) {
345 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
346 SymbolRef Symbol = *Sym;
347 ErrorOr<StringRef> SymName = Symbol.getName();
348 if (std::error_code EC = SymName.getError())
349 report_fatal_error(EC.message());
359 static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
360 for (const auto &Load : O->load_commands()) {
361 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
362 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
363 for (unsigned J = 0; J < Seg.nsects; ++J) {
364 MachO::section_64 Sec = O->getSection64(Load, J);
365 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
366 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
367 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
368 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
369 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
370 section_type == MachO::S_SYMBOL_STUBS) {
372 if (section_type == MachO::S_SYMBOL_STUBS)
373 stride = Sec.reserved2;
377 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
378 << Sec.sectname << ") "
379 << "(size of stubs in reserved2 field is zero)\n";
382 uint32_t count = Sec.size / stride;
383 outs() << "Indirect symbols for (" << Sec.segname << ","
384 << Sec.sectname << ") " << count << " entries";
385 uint32_t n = Sec.reserved1;
386 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
389 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
390 MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
391 for (unsigned J = 0; J < Seg.nsects; ++J) {
392 MachO::section Sec = O->getSection(Load, J);
393 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
394 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
395 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
396 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
397 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
398 section_type == MachO::S_SYMBOL_STUBS) {
400 if (section_type == MachO::S_SYMBOL_STUBS)
401 stride = Sec.reserved2;
405 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
406 << Sec.sectname << ") "
407 << "(size of stubs in reserved2 field is zero)\n";
410 uint32_t count = Sec.size / stride;
411 outs() << "Indirect symbols for (" << Sec.segname << ","
412 << Sec.sectname << ") " << count << " entries";
413 uint32_t n = Sec.reserved1;
414 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
421 static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
422 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
423 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
424 outs() << "Data in code table (" << nentries << " entries)\n";
425 outs() << "offset length kind\n";
426 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
429 DI->getOffset(Offset);
430 outs() << format("0x%08" PRIx32, Offset) << " ";
432 DI->getLength(Length);
433 outs() << format("%6u", Length) << " ";
438 case MachO::DICE_KIND_DATA:
441 case MachO::DICE_KIND_JUMP_TABLE8:
442 outs() << "JUMP_TABLE8";
444 case MachO::DICE_KIND_JUMP_TABLE16:
445 outs() << "JUMP_TABLE16";
447 case MachO::DICE_KIND_JUMP_TABLE32:
448 outs() << "JUMP_TABLE32";
450 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
451 outs() << "ABS_JUMP_TABLE32";
454 outs() << format("0x%04" PRIx32, Kind);
458 outs() << format("0x%04" PRIx32, Kind);
463 static void PrintLinkOptHints(MachOObjectFile *O) {
464 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
465 const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
466 uint32_t nloh = LohLC.datasize;
467 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
468 for (uint32_t i = 0; i < nloh;) {
470 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
472 outs() << " identifier " << identifier << " ";
475 switch (identifier) {
477 outs() << "AdrpAdrp\n";
480 outs() << "AdrpLdr\n";
483 outs() << "AdrpAddLdr\n";
486 outs() << "AdrpLdrGotLdr\n";
489 outs() << "AdrpAddStr\n";
492 outs() << "AdrpLdrGotStr\n";
495 outs() << "AdrpAdd\n";
498 outs() << "AdrpLdrGot\n";
501 outs() << "Unknown identifier value\n";
504 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
506 outs() << " narguments " << narguments << "\n";
510 for (uint32_t j = 0; j < narguments; j++) {
511 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
513 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
520 static void PrintDylibs(MachOObjectFile *O, bool JustId) {
522 for (const auto &Load : O->load_commands()) {
523 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
524 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
525 Load.C.cmd == MachO::LC_LOAD_DYLIB ||
526 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
527 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
528 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
529 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
530 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
531 if (dl.dylib.name < dl.cmdsize) {
532 const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
537 outs() << " (compatibility version "
538 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
539 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
540 << (dl.dylib.compatibility_version & 0xff) << ",";
541 outs() << " current version "
542 << ((dl.dylib.current_version >> 16) & 0xffff) << "."
543 << ((dl.dylib.current_version >> 8) & 0xff) << "."
544 << (dl.dylib.current_version & 0xff) << ")\n";
547 outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
548 if (Load.C.cmd == MachO::LC_ID_DYLIB)
549 outs() << "LC_ID_DYLIB ";
550 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
551 outs() << "LC_LOAD_DYLIB ";
552 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
553 outs() << "LC_LOAD_WEAK_DYLIB ";
554 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
555 outs() << "LC_LAZY_LOAD_DYLIB ";
556 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
557 outs() << "LC_REEXPORT_DYLIB ";
558 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
559 outs() << "LC_LOAD_UPWARD_DYLIB ";
562 outs() << "command " << Index++ << "\n";
568 typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
570 static void CreateSymbolAddressMap(MachOObjectFile *O,
571 SymbolAddressMap *AddrMap) {
572 // Create a map of symbol addresses to symbol names.
573 for (const SymbolRef &Symbol : O->symbols()) {
574 SymbolRef::Type ST = Symbol.getType();
575 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
576 ST == SymbolRef::ST_Other) {
577 uint64_t Address = Symbol.getValue();
578 ErrorOr<StringRef> SymNameOrErr = Symbol.getName();
579 if (std::error_code EC = SymNameOrErr.getError())
580 report_fatal_error(EC.message());
581 StringRef SymName = *SymNameOrErr;
582 if (!SymName.startswith(".objc"))
583 (*AddrMap)[Address] = SymName;
588 // GuessSymbolName is passed the address of what might be a symbol and a
589 // pointer to the SymbolAddressMap. It returns the name of a symbol
590 // with that address or nullptr if no symbol is found with that address.
591 static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
592 const char *SymbolName = nullptr;
593 // A DenseMap can't lookup up some values.
594 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
595 StringRef name = AddrMap->lookup(value);
597 SymbolName = name.data();
602 static void DumpCstringChar(const char c) {
606 outs().write_escaped(p);
609 static void DumpCstringSection(MachOObjectFile *O, const char *sect,
610 uint32_t sect_size, uint64_t sect_addr,
611 bool print_addresses) {
612 for (uint32_t i = 0; i < sect_size; i++) {
613 if (print_addresses) {
615 outs() << format("%016" PRIx64, sect_addr + i) << " ";
617 outs() << format("%08" PRIx64, sect_addr + i) << " ";
619 for (; i < sect_size && sect[i] != '\0'; i++)
620 DumpCstringChar(sect[i]);
621 if (i < sect_size && sect[i] == '\0')
626 static void DumpLiteral4(uint32_t l, float f) {
627 outs() << format("0x%08" PRIx32, l);
628 if ((l & 0x7f800000) != 0x7f800000)
629 outs() << format(" (%.16e)\n", f);
632 outs() << " (+Infinity)\n";
633 else if (l == 0xff800000)
634 outs() << " (-Infinity)\n";
635 else if ((l & 0x00400000) == 0x00400000)
636 outs() << " (non-signaling Not-a-Number)\n";
638 outs() << " (signaling Not-a-Number)\n";
642 static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
643 uint32_t sect_size, uint64_t sect_addr,
644 bool print_addresses) {
645 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
646 if (print_addresses) {
648 outs() << format("%016" PRIx64, sect_addr + i) << " ";
650 outs() << format("%08" PRIx64, sect_addr + i) << " ";
653 memcpy(&f, sect + i, sizeof(float));
654 if (O->isLittleEndian() != sys::IsLittleEndianHost)
655 sys::swapByteOrder(f);
657 memcpy(&l, sect + i, sizeof(uint32_t));
658 if (O->isLittleEndian() != sys::IsLittleEndianHost)
659 sys::swapByteOrder(l);
664 static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
666 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
668 if (O->isLittleEndian()) {
675 // Hi is the high word, so this is equivalent to if(isfinite(d))
676 if ((Hi & 0x7ff00000) != 0x7ff00000)
677 outs() << format(" (%.16e)\n", d);
679 if (Hi == 0x7ff00000 && Lo == 0)
680 outs() << " (+Infinity)\n";
681 else if (Hi == 0xfff00000 && Lo == 0)
682 outs() << " (-Infinity)\n";
683 else if ((Hi & 0x00080000) == 0x00080000)
684 outs() << " (non-signaling Not-a-Number)\n";
686 outs() << " (signaling Not-a-Number)\n";
690 static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
691 uint32_t sect_size, uint64_t sect_addr,
692 bool print_addresses) {
693 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
694 if (print_addresses) {
696 outs() << format("%016" PRIx64, sect_addr + i) << " ";
698 outs() << format("%08" PRIx64, sect_addr + i) << " ";
701 memcpy(&d, sect + i, sizeof(double));
702 if (O->isLittleEndian() != sys::IsLittleEndianHost)
703 sys::swapByteOrder(d);
705 memcpy(&l0, sect + i, sizeof(uint32_t));
706 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
707 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
708 sys::swapByteOrder(l0);
709 sys::swapByteOrder(l1);
711 DumpLiteral8(O, l0, l1, d);
715 static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
716 outs() << format("0x%08" PRIx32, l0) << " ";
717 outs() << format("0x%08" PRIx32, l1) << " ";
718 outs() << format("0x%08" PRIx32, l2) << " ";
719 outs() << format("0x%08" PRIx32, l3) << "\n";
722 static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
723 uint32_t sect_size, uint64_t sect_addr,
724 bool print_addresses) {
725 for (uint32_t i = 0; i < sect_size; i += 16) {
726 if (print_addresses) {
728 outs() << format("%016" PRIx64, sect_addr + i) << " ";
730 outs() << format("%08" PRIx64, sect_addr + i) << " ";
732 uint32_t l0, l1, l2, l3;
733 memcpy(&l0, sect + i, sizeof(uint32_t));
734 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
735 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
736 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
737 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
738 sys::swapByteOrder(l0);
739 sys::swapByteOrder(l1);
740 sys::swapByteOrder(l2);
741 sys::swapByteOrder(l3);
743 DumpLiteral16(l0, l1, l2, l3);
747 static void DumpLiteralPointerSection(MachOObjectFile *O,
748 const SectionRef &Section,
749 const char *sect, uint32_t sect_size,
751 bool print_addresses) {
752 // Collect the literal sections in this Mach-O file.
753 std::vector<SectionRef> LiteralSections;
754 for (const SectionRef &Section : O->sections()) {
755 DataRefImpl Ref = Section.getRawDataRefImpl();
756 uint32_t section_type;
758 const MachO::section_64 Sec = O->getSection64(Ref);
759 section_type = Sec.flags & MachO::SECTION_TYPE;
761 const MachO::section Sec = O->getSection(Ref);
762 section_type = Sec.flags & MachO::SECTION_TYPE;
764 if (section_type == MachO::S_CSTRING_LITERALS ||
765 section_type == MachO::S_4BYTE_LITERALS ||
766 section_type == MachO::S_8BYTE_LITERALS ||
767 section_type == MachO::S_16BYTE_LITERALS)
768 LiteralSections.push_back(Section);
771 // Set the size of the literal pointer.
772 uint32_t lp_size = O->is64Bit() ? 8 : 4;
774 // Collect the external relocation symbols for the literal pointers.
775 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
776 for (const RelocationRef &Reloc : Section.relocations()) {
778 MachO::any_relocation_info RE;
779 bool isExtern = false;
780 Rel = Reloc.getRawDataRefImpl();
781 RE = O->getRelocation(Rel);
782 isExtern = O->getPlainRelocationExternal(RE);
784 uint64_t RelocOffset = Reloc.getOffset();
785 symbol_iterator RelocSym = Reloc.getSymbol();
786 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
789 array_pod_sort(Relocs.begin(), Relocs.end());
791 // Dump each literal pointer.
792 for (uint32_t i = 0; i < sect_size; i += lp_size) {
793 if (print_addresses) {
795 outs() << format("%016" PRIx64, sect_addr + i) << " ";
797 outs() << format("%08" PRIx64, sect_addr + i) << " ";
801 memcpy(&lp, sect + i, sizeof(uint64_t));
802 if (O->isLittleEndian() != sys::IsLittleEndianHost)
803 sys::swapByteOrder(lp);
806 memcpy(&li, sect + i, sizeof(uint32_t));
807 if (O->isLittleEndian() != sys::IsLittleEndianHost)
808 sys::swapByteOrder(li);
812 // First look for an external relocation entry for this literal pointer.
813 auto Reloc = std::find_if(
814 Relocs.begin(), Relocs.end(),
815 [&](const std::pair<uint64_t, SymbolRef> &P) { return P.first == i; });
816 if (Reloc != Relocs.end()) {
817 symbol_iterator RelocSym = Reloc->second;
818 ErrorOr<StringRef> SymName = RelocSym->getName();
819 if (std::error_code EC = SymName.getError())
820 report_fatal_error(EC.message());
821 outs() << "external relocation entry for symbol:" << *SymName << "\n";
825 // For local references see what the section the literal pointer points to.
826 auto Sect = std::find_if(LiteralSections.begin(), LiteralSections.end(),
827 [&](const SectionRef &R) {
828 return lp >= R.getAddress() &&
829 lp < R.getAddress() + R.getSize();
831 if (Sect == LiteralSections.end()) {
832 outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
836 uint64_t SectAddress = Sect->getAddress();
837 uint64_t SectSize = Sect->getSize();
840 Sect->getName(SectName);
841 DataRefImpl Ref = Sect->getRawDataRefImpl();
842 StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
843 outs() << SegmentName << ":" << SectName << ":";
845 uint32_t section_type;
847 const MachO::section_64 Sec = O->getSection64(Ref);
848 section_type = Sec.flags & MachO::SECTION_TYPE;
850 const MachO::section Sec = O->getSection(Ref);
851 section_type = Sec.flags & MachO::SECTION_TYPE;
855 Sect->getContents(BytesStr);
856 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
858 switch (section_type) {
859 case MachO::S_CSTRING_LITERALS:
860 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
862 DumpCstringChar(Contents[i]);
866 case MachO::S_4BYTE_LITERALS:
868 memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
870 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
871 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
872 sys::swapByteOrder(f);
873 sys::swapByteOrder(l);
877 case MachO::S_8BYTE_LITERALS: {
879 memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
881 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
882 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
884 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
885 sys::swapByteOrder(f);
886 sys::swapByteOrder(l0);
887 sys::swapByteOrder(l1);
889 DumpLiteral8(O, l0, l1, d);
892 case MachO::S_16BYTE_LITERALS: {
893 uint32_t l0, l1, l2, l3;
894 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
895 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
897 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
899 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
901 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
902 sys::swapByteOrder(l0);
903 sys::swapByteOrder(l1);
904 sys::swapByteOrder(l2);
905 sys::swapByteOrder(l3);
907 DumpLiteral16(l0, l1, l2, l3);
914 static void DumpInitTermPointerSection(MachOObjectFile *O, const char *sect,
915 uint32_t sect_size, uint64_t sect_addr,
916 SymbolAddressMap *AddrMap,
920 stride = sizeof(uint64_t);
922 stride = sizeof(uint32_t);
923 for (uint32_t i = 0; i < sect_size; i += stride) {
924 const char *SymbolName = nullptr;
926 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
927 uint64_t pointer_value;
928 memcpy(&pointer_value, sect + i, stride);
929 if (O->isLittleEndian() != sys::IsLittleEndianHost)
930 sys::swapByteOrder(pointer_value);
931 outs() << format("0x%016" PRIx64, pointer_value);
933 SymbolName = GuessSymbolName(pointer_value, AddrMap);
935 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
936 uint32_t pointer_value;
937 memcpy(&pointer_value, sect + i, stride);
938 if (O->isLittleEndian() != sys::IsLittleEndianHost)
939 sys::swapByteOrder(pointer_value);
940 outs() << format("0x%08" PRIx32, pointer_value);
942 SymbolName = GuessSymbolName(pointer_value, AddrMap);
945 outs() << " " << SymbolName;
950 static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
951 uint32_t size, uint64_t addr) {
952 uint32_t cputype = O->getHeader().cputype;
953 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
955 for (uint32_t i = 0; i < size; i += j, addr += j) {
957 outs() << format("%016" PRIx64, addr) << "\t";
959 outs() << format("%08" PRIx64, addr) << "\t";
960 for (j = 0; j < 16 && i + j < size; j++) {
961 uint8_t byte_word = *(sect + i + j);
962 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
968 for (uint32_t i = 0; i < size; i += j, addr += j) {
970 outs() << format("%016" PRIx64, addr) << "\t";
972 outs() << format("%08" PRIx64, sect) << "\t";
973 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
974 j += sizeof(int32_t)) {
975 if (i + j + sizeof(int32_t) < size) {
977 memcpy(&long_word, sect + i + j, sizeof(int32_t));
978 if (O->isLittleEndian() != sys::IsLittleEndianHost)
979 sys::swapByteOrder(long_word);
980 outs() << format("%08" PRIx32, long_word) << " ";
982 for (uint32_t k = 0; i + j + k < size; k++) {
983 uint8_t byte_word = *(sect + i + j);
984 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
993 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
994 StringRef DisSegName, StringRef DisSectName);
995 static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
996 uint32_t size, uint32_t addr);
998 static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
1000 SymbolAddressMap AddrMap;
1002 CreateSymbolAddressMap(O, &AddrMap);
1004 for (unsigned i = 0; i < FilterSections.size(); ++i) {
1005 StringRef DumpSection = FilterSections[i];
1006 std::pair<StringRef, StringRef> DumpSegSectName;
1007 DumpSegSectName = DumpSection.split(',');
1008 StringRef DumpSegName, DumpSectName;
1009 if (DumpSegSectName.second.size()) {
1010 DumpSegName = DumpSegSectName.first;
1011 DumpSectName = DumpSegSectName.second;
1014 DumpSectName = DumpSegSectName.first;
1016 for (const SectionRef &Section : O->sections()) {
1018 Section.getName(SectName);
1019 DataRefImpl Ref = Section.getRawDataRefImpl();
1020 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1021 if ((DumpSegName.empty() || SegName == DumpSegName) &&
1022 (SectName == DumpSectName)) {
1024 uint32_t section_flags;
1026 const MachO::section_64 Sec = O->getSection64(Ref);
1027 section_flags = Sec.flags;
1030 const MachO::section Sec = O->getSection(Ref);
1031 section_flags = Sec.flags;
1033 uint32_t section_type = section_flags & MachO::SECTION_TYPE;
1036 Section.getContents(BytesStr);
1037 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1038 uint32_t sect_size = BytesStr.size();
1039 uint64_t sect_addr = Section.getAddress();
1041 outs() << "Contents of (" << SegName << "," << SectName
1045 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1046 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1047 DisassembleMachO(Filename, O, SegName, SectName);
1050 if (SegName == "__TEXT" && SectName == "__info_plist") {
1054 if (SegName == "__OBJC" && SectName == "__protocol") {
1055 DumpProtocolSection(O, sect, sect_size, sect_addr);
1058 switch (section_type) {
1059 case MachO::S_REGULAR:
1060 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1062 case MachO::S_ZEROFILL:
1063 outs() << "zerofill section and has no contents in the file\n";
1065 case MachO::S_CSTRING_LITERALS:
1066 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1068 case MachO::S_4BYTE_LITERALS:
1069 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1071 case MachO::S_8BYTE_LITERALS:
1072 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1074 case MachO::S_16BYTE_LITERALS:
1075 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1077 case MachO::S_LITERAL_POINTERS:
1078 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
1081 case MachO::S_MOD_INIT_FUNC_POINTERS:
1082 case MachO::S_MOD_TERM_FUNC_POINTERS:
1083 DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap,
1087 outs() << "Unknown section type ("
1088 << format("0x%08" PRIx32, section_type) << ")\n";
1089 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1093 if (section_type == MachO::S_ZEROFILL)
1094 outs() << "zerofill section and has no contents in the file\n";
1096 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1103 static void DumpInfoPlistSectionContents(StringRef Filename,
1104 MachOObjectFile *O) {
1105 for (const SectionRef &Section : O->sections()) {
1107 Section.getName(SectName);
1108 DataRefImpl Ref = Section.getRawDataRefImpl();
1109 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1110 if (SegName == "__TEXT" && SectName == "__info_plist") {
1111 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
1113 Section.getContents(BytesStr);
1114 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1121 // checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1122 // and if it is and there is a list of architecture flags is specified then
1123 // check to make sure this Mach-O file is one of those architectures or all
1124 // architectures were specified. If not then an error is generated and this
1125 // routine returns false. Else it returns true.
1126 static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
1127 if (isa<MachOObjectFile>(O) && !ArchAll && ArchFlags.size() != 0) {
1128 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O);
1129 bool ArchFound = false;
1130 MachO::mach_header H;
1131 MachO::mach_header_64 H_64;
1133 if (MachO->is64Bit()) {
1134 H_64 = MachO->MachOObjectFile::getHeader64();
1135 T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype);
1137 H = MachO->MachOObjectFile::getHeader();
1138 T = MachOObjectFile::getArch(H.cputype, H.cpusubtype);
1141 for (i = 0; i < ArchFlags.size(); ++i) {
1142 if (ArchFlags[i] == T.getArchName())
1147 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1148 << "architecture: " + ArchFlags[i] + "\n";
1155 static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1157 // ProcessMachO() is passed a single opened Mach-O file, which may be an
1158 // archive member and or in a slice of a universal file. It prints the
1159 // the file name and header info and then processes it according to the
1160 // command line options.
1161 static void ProcessMachO(StringRef Filename, MachOObjectFile *MachOOF,
1162 StringRef ArchiveMemberName = StringRef(),
1163 StringRef ArchitectureName = StringRef()) {
1164 // If we are doing some processing here on the Mach-O file print the header
1165 // info. And don't print it otherwise like in the case of printing the
1166 // UniversalHeaders or ArchiveHeaders.
1167 if (Disassemble || PrivateHeaders || ExportsTrie || Rebase || Bind ||
1168 LazyBind || WeakBind || IndirectSymbols || DataInCode || LinkOptHints ||
1169 DylibsUsed || DylibId || ObjcMetaData || (FilterSections.size() != 0)) {
1171 if (!ArchiveMemberName.empty())
1172 outs() << '(' << ArchiveMemberName << ')';
1173 if (!ArchitectureName.empty())
1174 outs() << " (architecture " << ArchitectureName << ")";
1179 DisassembleMachO(Filename, MachOOF, "__TEXT", "__text");
1180 if (IndirectSymbols)
1181 PrintIndirectSymbols(MachOOF, !NonVerbose);
1183 PrintDataInCodeTable(MachOOF, !NonVerbose);
1185 PrintLinkOptHints(MachOOF);
1187 PrintRelocations(MachOOF);
1189 PrintSectionHeaders(MachOOF);
1190 if (SectionContents)
1191 PrintSectionContents(MachOOF);
1192 if (FilterSections.size() != 0)
1193 DumpSectionContents(Filename, MachOOF, !NonVerbose);
1195 DumpInfoPlistSectionContents(Filename, MachOOF);
1197 PrintDylibs(MachOOF, false);
1199 PrintDylibs(MachOOF, true);
1201 PrintSymbolTable(MachOOF);
1203 printMachOUnwindInfo(MachOOF);
1205 printMachOFileHeader(MachOOF);
1207 printObjcMetaData(MachOOF, !NonVerbose);
1209 printExportsTrie(MachOOF);
1211 printRebaseTable(MachOOF);
1213 printBindTable(MachOOF);
1215 printLazyBindTable(MachOOF);
1217 printWeakBindTable(MachOOF);
1220 // printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1221 static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
1222 outs() << " cputype (" << cputype << ")\n";
1223 outs() << " cpusubtype (" << cpusubtype << ")\n";
1226 // printCPUType() helps print_fat_headers by printing the cputype and
1227 // pusubtype (symbolically for the one's it knows about).
1228 static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
1230 case MachO::CPU_TYPE_I386:
1231 switch (cpusubtype) {
1232 case MachO::CPU_SUBTYPE_I386_ALL:
1233 outs() << " cputype CPU_TYPE_I386\n";
1234 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
1237 printUnknownCPUType(cputype, cpusubtype);
1241 case MachO::CPU_TYPE_X86_64:
1242 switch (cpusubtype) {
1243 case MachO::CPU_SUBTYPE_X86_64_ALL:
1244 outs() << " cputype CPU_TYPE_X86_64\n";
1245 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1247 case MachO::CPU_SUBTYPE_X86_64_H:
1248 outs() << " cputype CPU_TYPE_X86_64\n";
1249 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
1252 printUnknownCPUType(cputype, cpusubtype);
1256 case MachO::CPU_TYPE_ARM:
1257 switch (cpusubtype) {
1258 case MachO::CPU_SUBTYPE_ARM_ALL:
1259 outs() << " cputype CPU_TYPE_ARM\n";
1260 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
1262 case MachO::CPU_SUBTYPE_ARM_V4T:
1263 outs() << " cputype CPU_TYPE_ARM\n";
1264 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
1266 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1267 outs() << " cputype CPU_TYPE_ARM\n";
1268 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
1270 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1271 outs() << " cputype CPU_TYPE_ARM\n";
1272 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
1274 case MachO::CPU_SUBTYPE_ARM_V6:
1275 outs() << " cputype CPU_TYPE_ARM\n";
1276 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
1278 case MachO::CPU_SUBTYPE_ARM_V6M:
1279 outs() << " cputype CPU_TYPE_ARM\n";
1280 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
1282 case MachO::CPU_SUBTYPE_ARM_V7:
1283 outs() << " cputype CPU_TYPE_ARM\n";
1284 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
1286 case MachO::CPU_SUBTYPE_ARM_V7EM:
1287 outs() << " cputype CPU_TYPE_ARM\n";
1288 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
1290 case MachO::CPU_SUBTYPE_ARM_V7K:
1291 outs() << " cputype CPU_TYPE_ARM\n";
1292 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
1294 case MachO::CPU_SUBTYPE_ARM_V7M:
1295 outs() << " cputype CPU_TYPE_ARM\n";
1296 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
1298 case MachO::CPU_SUBTYPE_ARM_V7S:
1299 outs() << " cputype CPU_TYPE_ARM\n";
1300 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
1303 printUnknownCPUType(cputype, cpusubtype);
1307 case MachO::CPU_TYPE_ARM64:
1308 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1309 case MachO::CPU_SUBTYPE_ARM64_ALL:
1310 outs() << " cputype CPU_TYPE_ARM64\n";
1311 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
1314 printUnknownCPUType(cputype, cpusubtype);
1319 printUnknownCPUType(cputype, cpusubtype);
1324 static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
1326 outs() << "Fat headers\n";
1328 outs() << "fat_magic FAT_MAGIC\n";
1330 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
1332 uint32_t nfat_arch = UB->getNumberOfObjects();
1333 StringRef Buf = UB->getData();
1334 uint64_t size = Buf.size();
1335 uint64_t big_size = sizeof(struct MachO::fat_header) +
1336 nfat_arch * sizeof(struct MachO::fat_arch);
1337 outs() << "nfat_arch " << UB->getNumberOfObjects();
1339 outs() << " (malformed, contains zero architecture types)\n";
1340 else if (big_size > size)
1341 outs() << " (malformed, architectures past end of file)\n";
1345 for (uint32_t i = 0; i < nfat_arch; ++i) {
1346 MachOUniversalBinary::ObjectForArch OFA(UB, i);
1347 uint32_t cputype = OFA.getCPUType();
1348 uint32_t cpusubtype = OFA.getCPUSubType();
1349 outs() << "architecture ";
1350 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
1351 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
1352 uint32_t other_cputype = other_OFA.getCPUType();
1353 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
1354 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
1355 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
1356 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
1357 outs() << "(illegal duplicate architecture) ";
1362 outs() << OFA.getArchTypeName() << "\n";
1363 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1365 outs() << i << "\n";
1366 outs() << " cputype " << cputype << "\n";
1367 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
1371 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
1372 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
1374 outs() << " capabilities "
1375 << format("0x%" PRIx32,
1376 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
1377 outs() << " offset " << OFA.getOffset();
1378 if (OFA.getOffset() > size)
1379 outs() << " (past end of file)";
1380 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
1381 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
1383 outs() << " size " << OFA.getSize();
1384 big_size = OFA.getOffset() + OFA.getSize();
1385 if (big_size > size)
1386 outs() << " (past end of file)";
1388 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
1393 static void printArchiveChild(Archive::Child &C, bool verbose,
1394 bool print_offset) {
1396 outs() << C.getChildOffset() << "\t";
1397 sys::fs::perms Mode = C.getAccessMode();
1399 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
1400 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
1402 outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
1403 outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
1404 outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
1405 outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
1406 outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
1407 outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
1408 outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
1409 outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
1410 outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
1412 outs() << format("0%o ", Mode);
1415 unsigned UID = C.getUID();
1416 outs() << format("%3d/", UID);
1417 unsigned GID = C.getGID();
1418 outs() << format("%-3d ", GID);
1419 uint64_t Size = C.getRawSize();
1420 outs() << format("%5" PRId64, Size) << " ";
1422 StringRef RawLastModified = C.getRawLastModified();
1425 if (RawLastModified.getAsInteger(10, Seconds))
1426 outs() << "(date: \"%s\" contains non-decimal chars) " << RawLastModified;
1428 // Since cime(3) returns a 26 character string of the form:
1429 // "Sun Sep 16 01:03:52 1973\n\0"
1430 // just print 24 characters.
1432 outs() << format("%.24s ", ctime(&t));
1435 outs() << RawLastModified << " ";
1439 ErrorOr<StringRef> NameOrErr = C.getName();
1440 if (NameOrErr.getError()) {
1441 StringRef RawName = C.getRawName();
1442 outs() << RawName << "\n";
1444 StringRef Name = NameOrErr.get();
1445 outs() << Name << "\n";
1448 StringRef RawName = C.getRawName();
1449 outs() << RawName << "\n";
1453 static void printArchiveHeaders(Archive *A, bool verbose, bool print_offset) {
1454 if (A->hasSymbolTable()) {
1455 Archive::child_iterator S = A->getSymbolTableChild();
1456 Archive::Child C = *S;
1457 printArchiveChild(C, verbose, print_offset);
1459 for (Archive::child_iterator I = A->child_begin(), E = A->child_end(); I != E;
1461 Archive::Child C = *I;
1462 printArchiveChild(C, verbose, print_offset);
1466 // ParseInputMachO() parses the named Mach-O file in Filename and handles the
1467 // -arch flags selecting just those slices as specified by them and also parses
1468 // archive files. Then for each individual Mach-O file ProcessMachO() is
1469 // called to process the file based on the command line options.
1470 void llvm::ParseInputMachO(StringRef Filename) {
1471 // Check for -arch all and verifiy the -arch flags are valid.
1472 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1473 if (ArchFlags[i] == "all") {
1476 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
1477 errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
1478 "'for the -arch option\n";
1484 // Attempt to open the binary.
1485 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
1486 if (std::error_code EC = BinaryOrErr.getError()) {
1487 errs() << "llvm-objdump: '" << Filename << "': " << EC.message() << ".\n";
1490 Binary &Bin = *BinaryOrErr.get().getBinary();
1492 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1493 outs() << "Archive : " << Filename << "\n";
1495 printArchiveHeaders(A, !NonVerbose, ArchiveMemberOffsets);
1496 for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
1498 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = I->getAsBinary();
1499 if (ChildOrErr.getError())
1501 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1502 if (!checkMachOAndArchFlags(O, Filename))
1504 ProcessMachO(Filename, O, O->getFileName());
1509 if (UniversalHeaders) {
1510 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
1511 printMachOUniversalHeaders(UB, !NonVerbose);
1513 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1514 // If we have a list of architecture flags specified dump only those.
1515 if (!ArchAll && ArchFlags.size() != 0) {
1516 // Look for a slice in the universal binary that matches each ArchFlag.
1518 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1520 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1521 E = UB->end_objects();
1523 if (ArchFlags[i] == I->getArchTypeName()) {
1525 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
1526 I->getAsObjectFile();
1527 std::string ArchitectureName = "";
1528 if (ArchFlags.size() > 1)
1529 ArchitectureName = I->getArchTypeName();
1531 ObjectFile &O = *ObjOrErr.get();
1532 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
1533 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
1534 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1535 I->getAsArchive()) {
1536 std::unique_ptr<Archive> &A = *AOrErr;
1537 outs() << "Archive : " << Filename;
1538 if (!ArchitectureName.empty())
1539 outs() << " (architecture " << ArchitectureName << ")";
1542 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
1543 for (Archive::child_iterator AI = A->child_begin(),
1544 AE = A->child_end();
1546 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1547 if (ChildOrErr.getError())
1549 if (MachOObjectFile *O =
1550 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
1551 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
1557 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1558 << "architecture: " + ArchFlags[i] + "\n";
1564 // No architecture flags were specified so if this contains a slice that
1565 // matches the host architecture dump only that.
1567 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1568 E = UB->end_objects();
1570 if (MachOObjectFile::getHostArch().getArchName() ==
1571 I->getArchTypeName()) {
1572 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1573 std::string ArchiveName;
1574 ArchiveName.clear();
1576 ObjectFile &O = *ObjOrErr.get();
1577 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
1578 ProcessMachO(Filename, MachOOF);
1579 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1580 I->getAsArchive()) {
1581 std::unique_ptr<Archive> &A = *AOrErr;
1582 outs() << "Archive : " << Filename << "\n";
1584 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
1585 for (Archive::child_iterator AI = A->child_begin(),
1586 AE = A->child_end();
1588 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1589 if (ChildOrErr.getError())
1591 if (MachOObjectFile *O =
1592 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
1593 ProcessMachO(Filename, O, O->getFileName());
1600 // Either all architectures have been specified or none have been specified
1601 // and this does not contain the host architecture so dump all the slices.
1602 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1603 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1604 E = UB->end_objects();
1606 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1607 std::string ArchitectureName = "";
1608 if (moreThanOneArch)
1609 ArchitectureName = I->getArchTypeName();
1611 ObjectFile &Obj = *ObjOrErr.get();
1612 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
1613 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
1614 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
1615 std::unique_ptr<Archive> &A = *AOrErr;
1616 outs() << "Archive : " << Filename;
1617 if (!ArchitectureName.empty())
1618 outs() << " (architecture " << ArchitectureName << ")";
1621 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
1622 for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
1624 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1625 if (ChildOrErr.getError())
1627 if (MachOObjectFile *O =
1628 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1629 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
1630 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
1638 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
1639 if (!checkMachOAndArchFlags(O, Filename))
1641 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
1642 ProcessMachO(Filename, MachOOF);
1644 errs() << "llvm-objdump: '" << Filename << "': "
1645 << "Object is not a Mach-O file type.\n";
1647 errs() << "llvm-objdump: '" << Filename << "': "
1648 << "Unrecognized file type.\n";
1651 typedef std::pair<uint64_t, const char *> BindInfoEntry;
1652 typedef std::vector<BindInfoEntry> BindTable;
1653 typedef BindTable::iterator bind_table_iterator;
1655 // The block of info used by the Symbolizer call backs.
1656 struct DisassembleInfo {
1660 SymbolAddressMap *AddrMap;
1661 std::vector<SectionRef> *Sections;
1662 const char *class_name;
1663 const char *selector_name;
1665 char *demangled_name;
1668 BindTable *bindtable;
1671 // SymbolizerGetOpInfo() is the operand information call back function.
1672 // This is called to get the symbolic information for operand(s) of an
1673 // instruction when it is being done. This routine does this from
1674 // the relocation information, symbol table, etc. That block of information
1675 // is a pointer to the struct DisassembleInfo that was passed when the
1676 // disassembler context was created and passed to back to here when
1677 // called back by the disassembler for instruction operands that could have
1678 // relocation information. The address of the instruction containing operand is
1679 // at the Pc parameter. The immediate value the operand has is passed in
1680 // op_info->Value and is at Offset past the start of the instruction and has a
1681 // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
1682 // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
1683 // names and addends of the symbolic expression to add for the operand. The
1684 // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
1685 // information is returned then this function returns 1 else it returns 0.
1686 static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
1687 uint64_t Size, int TagType, void *TagBuf) {
1688 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
1689 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
1690 uint64_t value = op_info->Value;
1692 // Make sure all fields returned are zero if we don't set them.
1693 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
1694 op_info->Value = value;
1696 // If the TagType is not the value 1 which it code knows about or if no
1697 // verbose symbolic information is wanted then just return 0, indicating no
1698 // information is being returned.
1699 if (TagType != 1 || !info->verbose)
1702 unsigned int Arch = info->O->getArch();
1703 if (Arch == Triple::x86) {
1704 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1706 // First search the section's relocation entries (if any) for an entry
1707 // for this section offset.
1708 uint32_t sect_addr = info->S.getAddress();
1709 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1710 bool reloc_found = false;
1712 MachO::any_relocation_info RE;
1713 bool isExtern = false;
1715 bool r_scattered = false;
1716 uint32_t r_value, pair_r_value, r_type;
1717 for (const RelocationRef &Reloc : info->S.relocations()) {
1718 uint64_t RelocOffset = Reloc.getOffset();
1719 if (RelocOffset == sect_offset) {
1720 Rel = Reloc.getRawDataRefImpl();
1721 RE = info->O->getRelocation(Rel);
1722 r_type = info->O->getAnyRelocationType(RE);
1723 r_scattered = info->O->isRelocationScattered(RE);
1725 r_value = info->O->getScatteredRelocationValue(RE);
1726 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1727 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
1728 DataRefImpl RelNext = Rel;
1729 info->O->moveRelocationNext(RelNext);
1730 MachO::any_relocation_info RENext;
1731 RENext = info->O->getRelocation(RelNext);
1732 if (info->O->isRelocationScattered(RENext))
1733 pair_r_value = info->O->getScatteredRelocationValue(RENext);
1738 isExtern = info->O->getPlainRelocationExternal(RE);
1740 symbol_iterator RelocSym = Reloc.getSymbol();
1748 if (reloc_found && isExtern) {
1749 ErrorOr<StringRef> SymName = Symbol.getName();
1750 if (std::error_code EC = SymName.getError())
1751 report_fatal_error(EC.message());
1752 const char *name = SymName->data();
1753 op_info->AddSymbol.Present = 1;
1754 op_info->AddSymbol.Name = name;
1755 // For i386 extern relocation entries the value in the instruction is
1756 // the offset from the symbol, and value is already set in op_info->Value.
1759 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1760 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
1761 const char *add = GuessSymbolName(r_value, info->AddrMap);
1762 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
1763 uint32_t offset = value - (r_value - pair_r_value);
1764 op_info->AddSymbol.Present = 1;
1766 op_info->AddSymbol.Name = add;
1768 op_info->AddSymbol.Value = r_value;
1769 op_info->SubtractSymbol.Present = 1;
1771 op_info->SubtractSymbol.Name = sub;
1773 op_info->SubtractSymbol.Value = pair_r_value;
1774 op_info->Value = offset;
1778 // Second search the external relocation entries of a fully linked image
1779 // (if any) for an entry that matches this segment offset.
1780 // uint32_t seg_offset = (Pc + Offset);
1783 if (Arch == Triple::x86_64) {
1784 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1786 // First search the section's relocation entries (if any) for an entry
1787 // for this section offset.
1788 uint64_t sect_addr = info->S.getAddress();
1789 uint64_t sect_offset = (Pc + Offset) - sect_addr;
1790 bool reloc_found = false;
1792 MachO::any_relocation_info RE;
1793 bool isExtern = false;
1795 for (const RelocationRef &Reloc : info->S.relocations()) {
1796 uint64_t RelocOffset = Reloc.getOffset();
1797 if (RelocOffset == sect_offset) {
1798 Rel = Reloc.getRawDataRefImpl();
1799 RE = info->O->getRelocation(Rel);
1800 // NOTE: Scattered relocations don't exist on x86_64.
1801 isExtern = info->O->getPlainRelocationExternal(RE);
1803 symbol_iterator RelocSym = Reloc.getSymbol();
1810 if (reloc_found && isExtern) {
1811 // The Value passed in will be adjusted by the Pc if the instruction
1812 // adds the Pc. But for x86_64 external relocation entries the Value
1813 // is the offset from the external symbol.
1814 if (info->O->getAnyRelocationPCRel(RE))
1815 op_info->Value -= Pc + Offset + Size;
1816 ErrorOr<StringRef> SymName = Symbol.getName();
1817 if (std::error_code EC = SymName.getError())
1818 report_fatal_error(EC.message());
1819 const char *name = SymName->data();
1820 unsigned Type = info->O->getAnyRelocationType(RE);
1821 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
1822 DataRefImpl RelNext = Rel;
1823 info->O->moveRelocationNext(RelNext);
1824 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
1825 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
1826 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
1827 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
1828 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
1829 op_info->SubtractSymbol.Present = 1;
1830 op_info->SubtractSymbol.Name = name;
1831 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
1832 Symbol = *RelocSymNext;
1833 ErrorOr<StringRef> SymNameNext = Symbol.getName();
1834 if (std::error_code EC = SymNameNext.getError())
1835 report_fatal_error(EC.message());
1836 name = SymNameNext->data();
1839 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
1840 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
1841 op_info->AddSymbol.Present = 1;
1842 op_info->AddSymbol.Name = name;
1846 // Second search the external relocation entries of a fully linked image
1847 // (if any) for an entry that matches this segment offset.
1848 // uint64_t seg_offset = (Pc + Offset);
1851 if (Arch == Triple::arm) {
1852 if (Offset != 0 || (Size != 4 && Size != 2))
1854 // First search the section's relocation entries (if any) for an entry
1855 // for this section offset.
1856 uint32_t sect_addr = info->S.getAddress();
1857 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1859 MachO::any_relocation_info RE;
1860 bool isExtern = false;
1862 bool r_scattered = false;
1863 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
1865 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
1866 [&](const RelocationRef &Reloc) {
1867 uint64_t RelocOffset = Reloc.getOffset();
1868 return RelocOffset == sect_offset;
1871 if (Reloc == info->S.relocations().end())
1874 Rel = Reloc->getRawDataRefImpl();
1875 RE = info->O->getRelocation(Rel);
1876 r_length = info->O->getAnyRelocationLength(RE);
1877 r_scattered = info->O->isRelocationScattered(RE);
1879 r_value = info->O->getScatteredRelocationValue(RE);
1880 r_type = info->O->getScatteredRelocationType(RE);
1882 r_type = info->O->getAnyRelocationType(RE);
1883 isExtern = info->O->getPlainRelocationExternal(RE);
1885 symbol_iterator RelocSym = Reloc->getSymbol();
1889 if (r_type == MachO::ARM_RELOC_HALF ||
1890 r_type == MachO::ARM_RELOC_SECTDIFF ||
1891 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
1892 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1893 DataRefImpl RelNext = Rel;
1894 info->O->moveRelocationNext(RelNext);
1895 MachO::any_relocation_info RENext;
1896 RENext = info->O->getRelocation(RelNext);
1897 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
1898 if (info->O->isRelocationScattered(RENext))
1899 pair_r_value = info->O->getScatteredRelocationValue(RENext);
1903 ErrorOr<StringRef> SymName = Symbol.getName();
1904 if (std::error_code EC = SymName.getError())
1905 report_fatal_error(EC.message());
1906 const char *name = SymName->data();
1907 op_info->AddSymbol.Present = 1;
1908 op_info->AddSymbol.Name = name;
1910 case MachO::ARM_RELOC_HALF:
1911 if ((r_length & 0x1) == 1) {
1912 op_info->Value = value << 16 | other_half;
1913 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1915 op_info->Value = other_half << 16 | value;
1916 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
1924 // If we have a branch that is not an external relocation entry then
1925 // return 0 so the code in tryAddingSymbolicOperand() can use the
1926 // SymbolLookUp call back with the branch target address to look up the
1927 // symbol and possiblity add an annotation for a symbol stub.
1928 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
1929 r_type == MachO::ARM_THUMB_RELOC_BR22))
1932 uint32_t offset = 0;
1933 if (r_type == MachO::ARM_RELOC_HALF ||
1934 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1935 if ((r_length & 0x1) == 1)
1936 value = value << 16 | other_half;
1938 value = other_half << 16 | value;
1940 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
1941 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
1942 offset = value - r_value;
1946 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1947 if ((r_length & 0x1) == 1)
1948 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1950 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
1951 const char *add = GuessSymbolName(r_value, info->AddrMap);
1952 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
1953 int32_t offset = value - (r_value - pair_r_value);
1954 op_info->AddSymbol.Present = 1;
1956 op_info->AddSymbol.Name = add;
1958 op_info->AddSymbol.Value = r_value;
1959 op_info->SubtractSymbol.Present = 1;
1961 op_info->SubtractSymbol.Name = sub;
1963 op_info->SubtractSymbol.Value = pair_r_value;
1964 op_info->Value = offset;
1968 op_info->AddSymbol.Present = 1;
1969 op_info->Value = offset;
1970 if (r_type == MachO::ARM_RELOC_HALF) {
1971 if ((r_length & 0x1) == 1)
1972 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1974 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
1976 const char *add = GuessSymbolName(value, info->AddrMap);
1977 if (add != nullptr) {
1978 op_info->AddSymbol.Name = add;
1981 op_info->AddSymbol.Value = value;
1984 if (Arch == Triple::aarch64) {
1985 if (Offset != 0 || Size != 4)
1987 // First search the section's relocation entries (if any) for an entry
1988 // for this section offset.
1989 uint64_t sect_addr = info->S.getAddress();
1990 uint64_t sect_offset = (Pc + Offset) - sect_addr;
1992 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
1993 [&](const RelocationRef &Reloc) {
1994 uint64_t RelocOffset = Reloc.getOffset();
1995 return RelocOffset == sect_offset;
1998 if (Reloc == info->S.relocations().end())
2001 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2002 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2003 uint32_t r_type = info->O->getAnyRelocationType(RE);
2004 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2005 DataRefImpl RelNext = Rel;
2006 info->O->moveRelocationNext(RelNext);
2007 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2009 value = info->O->getPlainRelocationSymbolNum(RENext);
2010 op_info->Value = value;
2013 // NOTE: Scattered relocations don't exist on arm64.
2014 if (!info->O->getPlainRelocationExternal(RE))
2016 ErrorOr<StringRef> SymName = Reloc->getSymbol()->getName();
2017 if (std::error_code EC = SymName.getError())
2018 report_fatal_error(EC.message());
2019 const char *name = SymName->data();
2020 op_info->AddSymbol.Present = 1;
2021 op_info->AddSymbol.Name = name;
2024 case MachO::ARM64_RELOC_PAGE21:
2026 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2028 case MachO::ARM64_RELOC_PAGEOFF12:
2030 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2032 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2034 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2036 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2038 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2040 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2041 /* @tvlppage is not implemented in llvm-mc */
2042 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2044 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2045 /* @tvlppageoff is not implemented in llvm-mc */
2046 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2049 case MachO::ARM64_RELOC_BRANCH26:
2050 op_info->VariantKind = LLVMDisassembler_VariantKind_None;
2058 // GuessCstringPointer is passed the address of what might be a pointer to a
2059 // literal string in a cstring section. If that address is in a cstring section
2060 // it returns a pointer to that string. Else it returns nullptr.
2061 static const char *GuessCstringPointer(uint64_t ReferenceValue,
2062 struct DisassembleInfo *info) {
2063 for (const auto &Load : info->O->load_commands()) {
2064 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2065 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2066 for (unsigned J = 0; J < Seg.nsects; ++J) {
2067 MachO::section_64 Sec = info->O->getSection64(Load, J);
2068 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2069 if (section_type == MachO::S_CSTRING_LITERALS &&
2070 ReferenceValue >= Sec.addr &&
2071 ReferenceValue < Sec.addr + Sec.size) {
2072 uint64_t sect_offset = ReferenceValue - Sec.addr;
2073 uint64_t object_offset = Sec.offset + sect_offset;
2074 StringRef MachOContents = info->O->getData();
2075 uint64_t object_size = MachOContents.size();
2076 const char *object_addr = (const char *)MachOContents.data();
2077 if (object_offset < object_size) {
2078 const char *name = object_addr + object_offset;
2085 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2086 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2087 for (unsigned J = 0; J < Seg.nsects; ++J) {
2088 MachO::section Sec = info->O->getSection(Load, J);
2089 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2090 if (section_type == MachO::S_CSTRING_LITERALS &&
2091 ReferenceValue >= Sec.addr &&
2092 ReferenceValue < Sec.addr + Sec.size) {
2093 uint64_t sect_offset = ReferenceValue - Sec.addr;
2094 uint64_t object_offset = Sec.offset + sect_offset;
2095 StringRef MachOContents = info->O->getData();
2096 uint64_t object_size = MachOContents.size();
2097 const char *object_addr = (const char *)MachOContents.data();
2098 if (object_offset < object_size) {
2099 const char *name = object_addr + object_offset;
2111 // GuessIndirectSymbol returns the name of the indirect symbol for the
2112 // ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
2113 // an address of a symbol stub or a lazy or non-lazy pointer to associate the
2114 // symbol name being referenced by the stub or pointer.
2115 static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2116 struct DisassembleInfo *info) {
2117 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2118 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
2119 for (const auto &Load : info->O->load_commands()) {
2120 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2121 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2122 for (unsigned J = 0; J < Seg.nsects; ++J) {
2123 MachO::section_64 Sec = info->O->getSection64(Load, J);
2124 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2125 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2126 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2127 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2128 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2129 section_type == MachO::S_SYMBOL_STUBS) &&
2130 ReferenceValue >= Sec.addr &&
2131 ReferenceValue < Sec.addr + Sec.size) {
2133 if (section_type == MachO::S_SYMBOL_STUBS)
2134 stride = Sec.reserved2;
2139 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2140 if (index < Dysymtab.nindirectsyms) {
2141 uint32_t indirect_symbol =
2142 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
2143 if (indirect_symbol < Symtab.nsyms) {
2144 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2145 SymbolRef Symbol = *Sym;
2146 ErrorOr<StringRef> SymName = Symbol.getName();
2147 if (std::error_code EC = SymName.getError())
2148 report_fatal_error(EC.message());
2149 const char *name = SymName->data();
2155 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2156 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2157 for (unsigned J = 0; J < Seg.nsects; ++J) {
2158 MachO::section Sec = info->O->getSection(Load, J);
2159 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2160 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2161 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2162 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2163 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2164 section_type == MachO::S_SYMBOL_STUBS) &&
2165 ReferenceValue >= Sec.addr &&
2166 ReferenceValue < Sec.addr + Sec.size) {
2168 if (section_type == MachO::S_SYMBOL_STUBS)
2169 stride = Sec.reserved2;
2174 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2175 if (index < Dysymtab.nindirectsyms) {
2176 uint32_t indirect_symbol =
2177 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
2178 if (indirect_symbol < Symtab.nsyms) {
2179 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2180 SymbolRef Symbol = *Sym;
2181 ErrorOr<StringRef> SymName = Symbol.getName();
2182 if (std::error_code EC = SymName.getError())
2183 report_fatal_error(EC.message());
2184 const char *name = SymName->data();
2195 // method_reference() is called passing it the ReferenceName that might be
2196 // a reference it to an Objective-C method call. If so then it allocates and
2197 // assembles a method call string with the values last seen and saved in
2198 // the DisassembleInfo's class_name and selector_name fields. This is saved
2199 // into the method field of the info and any previous string is free'ed.
2200 // Then the class_name field in the info is set to nullptr. The method call
2201 // string is set into ReferenceName and ReferenceType is set to
2202 // LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
2203 // then both ReferenceType and ReferenceName are left unchanged.
2204 static void method_reference(struct DisassembleInfo *info,
2205 uint64_t *ReferenceType,
2206 const char **ReferenceName) {
2207 unsigned int Arch = info->O->getArch();
2208 if (*ReferenceName != nullptr) {
2209 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
2210 if (info->selector_name != nullptr) {
2211 if (info->method != nullptr)
2213 if (info->class_name != nullptr) {
2214 info->method = (char *)malloc(5 + strlen(info->class_name) +
2215 strlen(info->selector_name));
2216 if (info->method != nullptr) {
2217 strcpy(info->method, "+[");
2218 strcat(info->method, info->class_name);
2219 strcat(info->method, " ");
2220 strcat(info->method, info->selector_name);
2221 strcat(info->method, "]");
2222 *ReferenceName = info->method;
2223 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2226 info->method = (char *)malloc(9 + strlen(info->selector_name));
2227 if (info->method != nullptr) {
2228 if (Arch == Triple::x86_64)
2229 strcpy(info->method, "-[%rdi ");
2230 else if (Arch == Triple::aarch64)
2231 strcpy(info->method, "-[x0 ");
2233 strcpy(info->method, "-[r? ");
2234 strcat(info->method, info->selector_name);
2235 strcat(info->method, "]");
2236 *ReferenceName = info->method;
2237 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2240 info->class_name = nullptr;
2242 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
2243 if (info->selector_name != nullptr) {
2244 if (info->method != nullptr)
2246 info->method = (char *)malloc(17 + strlen(info->selector_name));
2247 if (info->method != nullptr) {
2248 if (Arch == Triple::x86_64)
2249 strcpy(info->method, "-[[%rdi super] ");
2250 else if (Arch == Triple::aarch64)
2251 strcpy(info->method, "-[[x0 super] ");
2253 strcpy(info->method, "-[[r? super] ");
2254 strcat(info->method, info->selector_name);
2255 strcat(info->method, "]");
2256 *ReferenceName = info->method;
2257 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2259 info->class_name = nullptr;
2265 // GuessPointerPointer() is passed the address of what might be a pointer to
2266 // a reference to an Objective-C class, selector, message ref or cfstring.
2267 // If so the value of the pointer is returned and one of the booleans are set
2268 // to true. If not zero is returned and all the booleans are set to false.
2269 static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
2270 struct DisassembleInfo *info,
2271 bool &classref, bool &selref, bool &msgref,
2277 for (const auto &Load : info->O->load_commands()) {
2278 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2279 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2280 for (unsigned J = 0; J < Seg.nsects; ++J) {
2281 MachO::section_64 Sec = info->O->getSection64(Load, J);
2282 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
2283 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2284 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
2285 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
2286 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
2287 ReferenceValue >= Sec.addr &&
2288 ReferenceValue < Sec.addr + Sec.size) {
2289 uint64_t sect_offset = ReferenceValue - Sec.addr;
2290 uint64_t object_offset = Sec.offset + sect_offset;
2291 StringRef MachOContents = info->O->getData();
2292 uint64_t object_size = MachOContents.size();
2293 const char *object_addr = (const char *)MachOContents.data();
2294 if (object_offset < object_size) {
2295 uint64_t pointer_value;
2296 memcpy(&pointer_value, object_addr + object_offset,
2298 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2299 sys::swapByteOrder(pointer_value);
2300 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
2302 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2303 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
2305 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
2306 ReferenceValue + 8 < Sec.addr + Sec.size) {
2308 memcpy(&pointer_value, object_addr + object_offset + 8,
2310 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2311 sys::swapByteOrder(pointer_value);
2312 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
2314 return pointer_value;
2321 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
2326 // get_pointer_64 returns a pointer to the bytes in the object file at the
2327 // Address from a section in the Mach-O file. And indirectly returns the
2328 // offset into the section, number of bytes left in the section past the offset
2329 // and which section is was being referenced. If the Address is not in a
2330 // section nullptr is returned.
2331 static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
2332 uint32_t &left, SectionRef &S,
2333 DisassembleInfo *info,
2334 bool objc_only = false) {
2338 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
2339 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
2340 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
2343 ((*(info->Sections))[SectIdx]).getName(SectName);
2344 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
2345 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
2346 if (SegName != "__OBJC" && SectName != "__cstring")
2349 if (Address >= SectAddress && Address < SectAddress + SectSize) {
2350 S = (*(info->Sections))[SectIdx];
2351 offset = Address - SectAddress;
2352 left = SectSize - offset;
2353 StringRef SectContents;
2354 ((*(info->Sections))[SectIdx]).getContents(SectContents);
2355 return SectContents.data() + offset;
2361 static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
2362 uint32_t &left, SectionRef &S,
2363 DisassembleInfo *info,
2364 bool objc_only = false) {
2365 return get_pointer_64(Address, offset, left, S, info, objc_only);
2368 // get_symbol_64() returns the name of a symbol (or nullptr) and the address of
2369 // the symbol indirectly through n_value. Based on the relocation information
2370 // for the specified section offset in the specified section reference.
2371 // If no relocation information is found and a non-zero ReferenceValue for the
2372 // symbol is passed, look up that address in the info's AddrMap.
2373 static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
2374 DisassembleInfo *info, uint64_t &n_value,
2375 uint64_t ReferenceValue = 0) {
2380 // See if there is an external relocation entry at the sect_offset.
2381 bool reloc_found = false;
2383 MachO::any_relocation_info RE;
2384 bool isExtern = false;
2386 for (const RelocationRef &Reloc : S.relocations()) {
2387 uint64_t RelocOffset = Reloc.getOffset();
2388 if (RelocOffset == sect_offset) {
2389 Rel = Reloc.getRawDataRefImpl();
2390 RE = info->O->getRelocation(Rel);
2391 if (info->O->isRelocationScattered(RE))
2393 isExtern = info->O->getPlainRelocationExternal(RE);
2395 symbol_iterator RelocSym = Reloc.getSymbol();
2402 // If there is an external relocation entry for a symbol in this section
2403 // at this section_offset then use that symbol's value for the n_value
2404 // and return its name.
2405 const char *SymbolName = nullptr;
2406 if (reloc_found && isExtern) {
2407 n_value = Symbol.getValue();
2408 ErrorOr<StringRef> NameOrError = Symbol.getName();
2409 if (std::error_code EC = NameOrError.getError())
2410 report_fatal_error(EC.message());
2411 StringRef Name = *NameOrError;
2412 if (!Name.empty()) {
2413 SymbolName = Name.data();
2418 // TODO: For fully linked images, look through the external relocation
2419 // entries off the dynamic symtab command. For these the r_offset is from the
2420 // start of the first writeable segment in the Mach-O file. So the offset
2421 // to this section from that segment is passed to this routine by the caller,
2422 // as the database_offset. Which is the difference of the section's starting
2423 // address and the first writable segment.
2425 // NOTE: need add passing the database_offset to this routine.
2427 // We did not find an external relocation entry so look up the ReferenceValue
2428 // as an address of a symbol and if found return that symbol's name.
2429 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
2434 static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
2435 DisassembleInfo *info,
2436 uint32_t ReferenceValue) {
2438 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
2441 // These are structs in the Objective-C meta data and read to produce the
2442 // comments for disassembly. While these are part of the ABI they are no
2443 // public defintions. So the are here not in include/llvm/Support/MachO.h .
2445 // The cfstring object in a 64-bit Mach-O file.
2446 struct cfstring64_t {
2447 uint64_t isa; // class64_t * (64-bit pointer)
2448 uint64_t flags; // flag bits
2449 uint64_t characters; // char * (64-bit pointer)
2450 uint64_t length; // number of non-NULL characters in above
2453 // The class object in a 64-bit Mach-O file.
2455 uint64_t isa; // class64_t * (64-bit pointer)
2456 uint64_t superclass; // class64_t * (64-bit pointer)
2457 uint64_t cache; // Cache (64-bit pointer)
2458 uint64_t vtable; // IMP * (64-bit pointer)
2459 uint64_t data; // class_ro64_t * (64-bit pointer)
2463 uint32_t isa; /* class32_t * (32-bit pointer) */
2464 uint32_t superclass; /* class32_t * (32-bit pointer) */
2465 uint32_t cache; /* Cache (32-bit pointer) */
2466 uint32_t vtable; /* IMP * (32-bit pointer) */
2467 uint32_t data; /* class_ro32_t * (32-bit pointer) */
2470 struct class_ro64_t {
2472 uint32_t instanceStart;
2473 uint32_t instanceSize;
2475 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
2476 uint64_t name; // const char * (64-bit pointer)
2477 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
2478 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
2479 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
2480 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
2481 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
2484 struct class_ro32_t {
2486 uint32_t instanceStart;
2487 uint32_t instanceSize;
2488 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
2489 uint32_t name; /* const char * (32-bit pointer) */
2490 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
2491 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
2492 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
2493 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
2494 uint32_t baseProperties; /* const struct objc_property_list *
2498 /* Values for class_ro{64,32}_t->flags */
2499 #define RO_META (1 << 0)
2500 #define RO_ROOT (1 << 1)
2501 #define RO_HAS_CXX_STRUCTORS (1 << 2)
2503 struct method_list64_t {
2506 /* struct method64_t first; These structures follow inline */
2509 struct method_list32_t {
2512 /* struct method32_t first; These structures follow inline */
2516 uint64_t name; /* SEL (64-bit pointer) */
2517 uint64_t types; /* const char * (64-bit pointer) */
2518 uint64_t imp; /* IMP (64-bit pointer) */
2522 uint32_t name; /* SEL (32-bit pointer) */
2523 uint32_t types; /* const char * (32-bit pointer) */
2524 uint32_t imp; /* IMP (32-bit pointer) */
2527 struct protocol_list64_t {
2528 uint64_t count; /* uintptr_t (a 64-bit value) */
2529 /* struct protocol64_t * list[0]; These pointers follow inline */
2532 struct protocol_list32_t {
2533 uint32_t count; /* uintptr_t (a 32-bit value) */
2534 /* struct protocol32_t * list[0]; These pointers follow inline */
2537 struct protocol64_t {
2538 uint64_t isa; /* id * (64-bit pointer) */
2539 uint64_t name; /* const char * (64-bit pointer) */
2540 uint64_t protocols; /* struct protocol_list64_t *
2542 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
2543 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
2544 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
2545 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
2546 uint64_t instanceProperties; /* struct objc_property_list *
2550 struct protocol32_t {
2551 uint32_t isa; /* id * (32-bit pointer) */
2552 uint32_t name; /* const char * (32-bit pointer) */
2553 uint32_t protocols; /* struct protocol_list_t *
2555 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
2556 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
2557 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
2558 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
2559 uint32_t instanceProperties; /* struct objc_property_list *
2563 struct ivar_list64_t {
2566 /* struct ivar64_t first; These structures follow inline */
2569 struct ivar_list32_t {
2572 /* struct ivar32_t first; These structures follow inline */
2576 uint64_t offset; /* uintptr_t * (64-bit pointer) */
2577 uint64_t name; /* const char * (64-bit pointer) */
2578 uint64_t type; /* const char * (64-bit pointer) */
2584 uint32_t offset; /* uintptr_t * (32-bit pointer) */
2585 uint32_t name; /* const char * (32-bit pointer) */
2586 uint32_t type; /* const char * (32-bit pointer) */
2591 struct objc_property_list64 {
2594 /* struct objc_property64 first; These structures follow inline */
2597 struct objc_property_list32 {
2600 /* struct objc_property32 first; These structures follow inline */
2603 struct objc_property64 {
2604 uint64_t name; /* const char * (64-bit pointer) */
2605 uint64_t attributes; /* const char * (64-bit pointer) */
2608 struct objc_property32 {
2609 uint32_t name; /* const char * (32-bit pointer) */
2610 uint32_t attributes; /* const char * (32-bit pointer) */
2613 struct category64_t {
2614 uint64_t name; /* const char * (64-bit pointer) */
2615 uint64_t cls; /* struct class_t * (64-bit pointer) */
2616 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
2617 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
2618 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
2619 uint64_t instanceProperties; /* struct objc_property_list *
2623 struct category32_t {
2624 uint32_t name; /* const char * (32-bit pointer) */
2625 uint32_t cls; /* struct class_t * (32-bit pointer) */
2626 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
2627 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
2628 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
2629 uint32_t instanceProperties; /* struct objc_property_list *
2633 struct objc_image_info64 {
2637 struct objc_image_info32 {
2641 struct imageInfo_t {
2645 /* masks for objc_image_info.flags */
2646 #define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
2647 #define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
2649 struct message_ref64 {
2650 uint64_t imp; /* IMP (64-bit pointer) */
2651 uint64_t sel; /* SEL (64-bit pointer) */
2654 struct message_ref32 {
2655 uint32_t imp; /* IMP (32-bit pointer) */
2656 uint32_t sel; /* SEL (32-bit pointer) */
2659 // Objective-C 1 (32-bit only) meta data structs.
2661 struct objc_module_t {
2664 uint32_t name; /* char * (32-bit pointer) */
2665 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
2668 struct objc_symtab_t {
2669 uint32_t sel_ref_cnt;
2670 uint32_t refs; /* SEL * (32-bit pointer) */
2671 uint16_t cls_def_cnt;
2672 uint16_t cat_def_cnt;
2673 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
2676 struct objc_class_t {
2677 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2678 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
2679 uint32_t name; /* const char * (32-bit pointer) */
2682 int32_t instance_size;
2683 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
2684 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
2685 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
2686 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
2689 #define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
2690 // class is not a metaclass
2691 #define CLS_CLASS 0x1
2692 // class is a metaclass
2693 #define CLS_META 0x2
2695 struct objc_category_t {
2696 uint32_t category_name; /* char * (32-bit pointer) */
2697 uint32_t class_name; /* char * (32-bit pointer) */
2698 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
2699 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
2700 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
2703 struct objc_ivar_t {
2704 uint32_t ivar_name; /* char * (32-bit pointer) */
2705 uint32_t ivar_type; /* char * (32-bit pointer) */
2706 int32_t ivar_offset;
2709 struct objc_ivar_list_t {
2711 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
2714 struct objc_method_list_t {
2715 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
2716 int32_t method_count;
2717 // struct objc_method_t method_list[1]; /* variable length structure */
2720 struct objc_method_t {
2721 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2722 uint32_t method_types; /* char * (32-bit pointer) */
2723 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
2727 struct objc_protocol_list_t {
2728 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
2730 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
2731 // (32-bit pointer) */
2734 struct objc_protocol_t {
2735 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2736 uint32_t protocol_name; /* char * (32-bit pointer) */
2737 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
2738 uint32_t instance_methods; /* struct objc_method_description_list *
2740 uint32_t class_methods; /* struct objc_method_description_list *
2744 struct objc_method_description_list_t {
2746 // struct objc_method_description_t list[1];
2749 struct objc_method_description_t {
2750 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2751 uint32_t types; /* char * (32-bit pointer) */
2754 inline void swapStruct(struct cfstring64_t &cfs) {
2755 sys::swapByteOrder(cfs.isa);
2756 sys::swapByteOrder(cfs.flags);
2757 sys::swapByteOrder(cfs.characters);
2758 sys::swapByteOrder(cfs.length);
2761 inline void swapStruct(struct class64_t &c) {
2762 sys::swapByteOrder(c.isa);
2763 sys::swapByteOrder(c.superclass);
2764 sys::swapByteOrder(c.cache);
2765 sys::swapByteOrder(c.vtable);
2766 sys::swapByteOrder(c.data);
2769 inline void swapStruct(struct class32_t &c) {
2770 sys::swapByteOrder(c.isa);
2771 sys::swapByteOrder(c.superclass);
2772 sys::swapByteOrder(c.cache);
2773 sys::swapByteOrder(c.vtable);
2774 sys::swapByteOrder(c.data);
2777 inline void swapStruct(struct class_ro64_t &cro) {
2778 sys::swapByteOrder(cro.flags);
2779 sys::swapByteOrder(cro.instanceStart);
2780 sys::swapByteOrder(cro.instanceSize);
2781 sys::swapByteOrder(cro.reserved);
2782 sys::swapByteOrder(cro.ivarLayout);
2783 sys::swapByteOrder(cro.name);
2784 sys::swapByteOrder(cro.baseMethods);
2785 sys::swapByteOrder(cro.baseProtocols);
2786 sys::swapByteOrder(cro.ivars);
2787 sys::swapByteOrder(cro.weakIvarLayout);
2788 sys::swapByteOrder(cro.baseProperties);
2791 inline void swapStruct(struct class_ro32_t &cro) {
2792 sys::swapByteOrder(cro.flags);
2793 sys::swapByteOrder(cro.instanceStart);
2794 sys::swapByteOrder(cro.instanceSize);
2795 sys::swapByteOrder(cro.ivarLayout);
2796 sys::swapByteOrder(cro.name);
2797 sys::swapByteOrder(cro.baseMethods);
2798 sys::swapByteOrder(cro.baseProtocols);
2799 sys::swapByteOrder(cro.ivars);
2800 sys::swapByteOrder(cro.weakIvarLayout);
2801 sys::swapByteOrder(cro.baseProperties);
2804 inline void swapStruct(struct method_list64_t &ml) {
2805 sys::swapByteOrder(ml.entsize);
2806 sys::swapByteOrder(ml.count);
2809 inline void swapStruct(struct method_list32_t &ml) {
2810 sys::swapByteOrder(ml.entsize);
2811 sys::swapByteOrder(ml.count);
2814 inline void swapStruct(struct method64_t &m) {
2815 sys::swapByteOrder(m.name);
2816 sys::swapByteOrder(m.types);
2817 sys::swapByteOrder(m.imp);
2820 inline void swapStruct(struct method32_t &m) {
2821 sys::swapByteOrder(m.name);
2822 sys::swapByteOrder(m.types);
2823 sys::swapByteOrder(m.imp);
2826 inline void swapStruct(struct protocol_list64_t &pl) {
2827 sys::swapByteOrder(pl.count);
2830 inline void swapStruct(struct protocol_list32_t &pl) {
2831 sys::swapByteOrder(pl.count);
2834 inline void swapStruct(struct protocol64_t &p) {
2835 sys::swapByteOrder(p.isa);
2836 sys::swapByteOrder(p.name);
2837 sys::swapByteOrder(p.protocols);
2838 sys::swapByteOrder(p.instanceMethods);
2839 sys::swapByteOrder(p.classMethods);
2840 sys::swapByteOrder(p.optionalInstanceMethods);
2841 sys::swapByteOrder(p.optionalClassMethods);
2842 sys::swapByteOrder(p.instanceProperties);
2845 inline void swapStruct(struct protocol32_t &p) {
2846 sys::swapByteOrder(p.isa);
2847 sys::swapByteOrder(p.name);
2848 sys::swapByteOrder(p.protocols);
2849 sys::swapByteOrder(p.instanceMethods);
2850 sys::swapByteOrder(p.classMethods);
2851 sys::swapByteOrder(p.optionalInstanceMethods);
2852 sys::swapByteOrder(p.optionalClassMethods);
2853 sys::swapByteOrder(p.instanceProperties);
2856 inline void swapStruct(struct ivar_list64_t &il) {
2857 sys::swapByteOrder(il.entsize);
2858 sys::swapByteOrder(il.count);
2861 inline void swapStruct(struct ivar_list32_t &il) {
2862 sys::swapByteOrder(il.entsize);
2863 sys::swapByteOrder(il.count);
2866 inline void swapStruct(struct ivar64_t &i) {
2867 sys::swapByteOrder(i.offset);
2868 sys::swapByteOrder(i.name);
2869 sys::swapByteOrder(i.type);
2870 sys::swapByteOrder(i.alignment);
2871 sys::swapByteOrder(i.size);
2874 inline void swapStruct(struct ivar32_t &i) {
2875 sys::swapByteOrder(i.offset);
2876 sys::swapByteOrder(i.name);
2877 sys::swapByteOrder(i.type);
2878 sys::swapByteOrder(i.alignment);
2879 sys::swapByteOrder(i.size);
2882 inline void swapStruct(struct objc_property_list64 &pl) {
2883 sys::swapByteOrder(pl.entsize);
2884 sys::swapByteOrder(pl.count);
2887 inline void swapStruct(struct objc_property_list32 &pl) {
2888 sys::swapByteOrder(pl.entsize);
2889 sys::swapByteOrder(pl.count);
2892 inline void swapStruct(struct objc_property64 &op) {
2893 sys::swapByteOrder(op.name);
2894 sys::swapByteOrder(op.attributes);
2897 inline void swapStruct(struct objc_property32 &op) {
2898 sys::swapByteOrder(op.name);
2899 sys::swapByteOrder(op.attributes);
2902 inline void swapStruct(struct category64_t &c) {
2903 sys::swapByteOrder(c.name);
2904 sys::swapByteOrder(c.cls);
2905 sys::swapByteOrder(c.instanceMethods);
2906 sys::swapByteOrder(c.classMethods);
2907 sys::swapByteOrder(c.protocols);
2908 sys::swapByteOrder(c.instanceProperties);
2911 inline void swapStruct(struct category32_t &c) {
2912 sys::swapByteOrder(c.name);
2913 sys::swapByteOrder(c.cls);
2914 sys::swapByteOrder(c.instanceMethods);
2915 sys::swapByteOrder(c.classMethods);
2916 sys::swapByteOrder(c.protocols);
2917 sys::swapByteOrder(c.instanceProperties);
2920 inline void swapStruct(struct objc_image_info64 &o) {
2921 sys::swapByteOrder(o.version);
2922 sys::swapByteOrder(o.flags);
2925 inline void swapStruct(struct objc_image_info32 &o) {
2926 sys::swapByteOrder(o.version);
2927 sys::swapByteOrder(o.flags);
2930 inline void swapStruct(struct imageInfo_t &o) {
2931 sys::swapByteOrder(o.version);
2932 sys::swapByteOrder(o.flags);
2935 inline void swapStruct(struct message_ref64 &mr) {
2936 sys::swapByteOrder(mr.imp);
2937 sys::swapByteOrder(mr.sel);
2940 inline void swapStruct(struct message_ref32 &mr) {
2941 sys::swapByteOrder(mr.imp);
2942 sys::swapByteOrder(mr.sel);
2945 inline void swapStruct(struct objc_module_t &module) {
2946 sys::swapByteOrder(module.version);
2947 sys::swapByteOrder(module.size);
2948 sys::swapByteOrder(module.name);
2949 sys::swapByteOrder(module.symtab);
2952 inline void swapStruct(struct objc_symtab_t &symtab) {
2953 sys::swapByteOrder(symtab.sel_ref_cnt);
2954 sys::swapByteOrder(symtab.refs);
2955 sys::swapByteOrder(symtab.cls_def_cnt);
2956 sys::swapByteOrder(symtab.cat_def_cnt);
2959 inline void swapStruct(struct objc_class_t &objc_class) {
2960 sys::swapByteOrder(objc_class.isa);
2961 sys::swapByteOrder(objc_class.super_class);
2962 sys::swapByteOrder(objc_class.name);
2963 sys::swapByteOrder(objc_class.version);
2964 sys::swapByteOrder(objc_class.info);
2965 sys::swapByteOrder(objc_class.instance_size);
2966 sys::swapByteOrder(objc_class.ivars);
2967 sys::swapByteOrder(objc_class.methodLists);
2968 sys::swapByteOrder(objc_class.cache);
2969 sys::swapByteOrder(objc_class.protocols);
2972 inline void swapStruct(struct objc_category_t &objc_category) {
2973 sys::swapByteOrder(objc_category.category_name);
2974 sys::swapByteOrder(objc_category.class_name);
2975 sys::swapByteOrder(objc_category.instance_methods);
2976 sys::swapByteOrder(objc_category.class_methods);
2977 sys::swapByteOrder(objc_category.protocols);
2980 inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
2981 sys::swapByteOrder(objc_ivar_list.ivar_count);
2984 inline void swapStruct(struct objc_ivar_t &objc_ivar) {
2985 sys::swapByteOrder(objc_ivar.ivar_name);
2986 sys::swapByteOrder(objc_ivar.ivar_type);
2987 sys::swapByteOrder(objc_ivar.ivar_offset);
2990 inline void swapStruct(struct objc_method_list_t &method_list) {
2991 sys::swapByteOrder(method_list.obsolete);
2992 sys::swapByteOrder(method_list.method_count);
2995 inline void swapStruct(struct objc_method_t &method) {
2996 sys::swapByteOrder(method.method_name);
2997 sys::swapByteOrder(method.method_types);
2998 sys::swapByteOrder(method.method_imp);
3001 inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3002 sys::swapByteOrder(protocol_list.next);
3003 sys::swapByteOrder(protocol_list.count);
3006 inline void swapStruct(struct objc_protocol_t &protocol) {
3007 sys::swapByteOrder(protocol.isa);
3008 sys::swapByteOrder(protocol.protocol_name);
3009 sys::swapByteOrder(protocol.protocol_list);
3010 sys::swapByteOrder(protocol.instance_methods);
3011 sys::swapByteOrder(protocol.class_methods);
3014 inline void swapStruct(struct objc_method_description_list_t &mdl) {
3015 sys::swapByteOrder(mdl.count);
3018 inline void swapStruct(struct objc_method_description_t &md) {
3019 sys::swapByteOrder(md.name);
3020 sys::swapByteOrder(md.types);
3023 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3024 struct DisassembleInfo *info);
3026 // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3027 // to an Objective-C class and returns the class name. It is also passed the
3028 // address of the pointer, so when the pointer is zero as it can be in an .o
3029 // file, that is used to look for an external relocation entry with a symbol
3031 static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3032 uint64_t ReferenceValue,
3033 struct DisassembleInfo *info) {
3035 uint32_t offset, left;
3038 // The pointer_value can be 0 in an object file and have a relocation
3039 // entry for the class symbol at the ReferenceValue (the address of the
3041 if (pointer_value == 0) {
3042 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3043 if (r == nullptr || left < sizeof(uint64_t))
3046 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3047 if (symbol_name == nullptr)
3049 const char *class_name = strrchr(symbol_name, '$');
3050 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3051 return class_name + 2;
3056 // The case were the pointer_value is non-zero and points to a class defined
3057 // in this Mach-O file.
3058 r = get_pointer_64(pointer_value, offset, left, S, info);
3059 if (r == nullptr || left < sizeof(struct class64_t))
3062 memcpy(&c, r, sizeof(struct class64_t));
3063 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3067 r = get_pointer_64(c.data, offset, left, S, info);
3068 if (r == nullptr || left < sizeof(struct class_ro64_t))
3070 struct class_ro64_t cro;
3071 memcpy(&cro, r, sizeof(struct class_ro64_t));
3072 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3076 const char *name = get_pointer_64(cro.name, offset, left, S, info);
3080 // get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3081 // pointer to a cfstring and returns its name or nullptr.
3082 static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3083 struct DisassembleInfo *info) {
3084 const char *r, *name;
3085 uint32_t offset, left;
3087 struct cfstring64_t cfs;
3088 uint64_t cfs_characters;
3090 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3091 if (r == nullptr || left < sizeof(struct cfstring64_t))
3093 memcpy(&cfs, r, sizeof(struct cfstring64_t));
3094 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3096 if (cfs.characters == 0) {
3098 const char *symbol_name = get_symbol_64(
3099 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
3100 if (symbol_name == nullptr)
3102 cfs_characters = n_value;
3104 cfs_characters = cfs.characters;
3105 name = get_pointer_64(cfs_characters, offset, left, S, info);
3110 // get_objc2_64bit_selref() is used for disassembly and is passed a the address
3111 // of a pointer to an Objective-C selector reference when the pointer value is
3112 // zero as in a .o file and is likely to have a external relocation entry with
3113 // who's symbol's n_value is the real pointer to the selector name. If that is
3114 // the case the real pointer to the selector name is returned else 0 is
3116 static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3117 struct DisassembleInfo *info) {
3118 uint32_t offset, left;
3121 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3122 if (r == nullptr || left < sizeof(uint64_t))
3125 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3126 if (symbol_name == nullptr)
3131 static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3132 const char *sectname) {
3133 for (const SectionRef &Section : O->sections()) {
3135 Section.getName(SectName);
3136 DataRefImpl Ref = Section.getRawDataRefImpl();
3137 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3138 if (SegName == segname && SectName == sectname)
3141 return SectionRef();
3145 walk_pointer_list_64(const char *listname, const SectionRef S,
3146 MachOObjectFile *O, struct DisassembleInfo *info,
3147 void (*func)(uint64_t, struct DisassembleInfo *info)) {
3148 if (S == SectionRef())
3152 S.getName(SectName);
3153 DataRefImpl Ref = S.getRawDataRefImpl();
3154 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3155 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3158 S.getContents(BytesStr);
3159 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3161 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3162 uint32_t left = S.getSize() - i;
3163 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3165 memcpy(&p, Contents + i, size);
3166 if (i + sizeof(uint64_t) > S.getSize())
3167 outs() << listname << " list pointer extends past end of (" << SegName
3168 << "," << SectName << ") section\n";
3169 outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
3171 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3172 sys::swapByteOrder(p);
3174 uint64_t n_value = 0;
3175 const char *name = get_symbol_64(i, S, info, n_value, p);
3176 if (name == nullptr)
3177 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
3180 outs() << format("0x%" PRIx64, n_value);
3182 outs() << " + " << format("0x%" PRIx64, p);
3184 outs() << format("0x%" PRIx64, p);
3185 if (name != nullptr)
3186 outs() << " " << name;
3196 walk_pointer_list_32(const char *listname, const SectionRef S,
3197 MachOObjectFile *O, struct DisassembleInfo *info,
3198 void (*func)(uint32_t, struct DisassembleInfo *info)) {
3199 if (S == SectionRef())
3203 S.getName(SectName);
3204 DataRefImpl Ref = S.getRawDataRefImpl();
3205 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3206 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3209 S.getContents(BytesStr);
3210 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3212 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
3213 uint32_t left = S.getSize() - i;
3214 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
3216 memcpy(&p, Contents + i, size);
3217 if (i + sizeof(uint32_t) > S.getSize())
3218 outs() << listname << " list pointer extends past end of (" << SegName
3219 << "," << SectName << ") section\n";
3220 uint32_t Address = S.getAddress() + i;
3221 outs() << format("%08" PRIx32, Address) << " ";
3223 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3224 sys::swapByteOrder(p);
3225 outs() << format("0x%" PRIx32, p);
3227 const char *name = get_symbol_32(i, S, info, p);
3228 if (name != nullptr)
3229 outs() << " " << name;
3237 static void print_layout_map(const char *layout_map, uint32_t left) {
3238 outs() << " layout map: ";
3240 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
3243 } while (*layout_map != '\0' && left != 0);
3247 static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
3248 uint32_t offset, left;
3250 const char *layout_map;
3254 layout_map = get_pointer_64(p, offset, left, S, info);
3255 print_layout_map(layout_map, left);
3258 static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
3259 uint32_t offset, left;
3261 const char *layout_map;
3265 layout_map = get_pointer_32(p, offset, left, S, info);
3266 print_layout_map(layout_map, left);
3269 static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
3270 const char *indent) {
3271 struct method_list64_t ml;
3272 struct method64_t m;
3274 uint32_t offset, xoffset, left, i;
3276 const char *name, *sym_name;
3279 r = get_pointer_64(p, offset, left, S, info);
3282 memset(&ml, '\0', sizeof(struct method_list64_t));
3283 if (left < sizeof(struct method_list64_t)) {
3284 memcpy(&ml, r, left);
3285 outs() << " (method_list_t entends past the end of the section)\n";
3287 memcpy(&ml, r, sizeof(struct method_list64_t));
3288 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3290 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3291 outs() << indent << "\t\t count " << ml.count << "\n";
3293 p += sizeof(struct method_list64_t);
3294 offset += sizeof(struct method_list64_t);
3295 for (i = 0; i < ml.count; i++) {
3296 r = get_pointer_64(p, offset, left, S, info);
3299 memset(&m, '\0', sizeof(struct method64_t));
3300 if (left < sizeof(struct method64_t)) {
3301 memcpy(&ml, r, left);
3302 outs() << indent << " (method_t entends past the end of the section)\n";
3304 memcpy(&m, r, sizeof(struct method64_t));
3305 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3308 outs() << indent << "\t\t name ";
3309 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
3310 info, n_value, m.name);
3312 if (info->verbose && sym_name != nullptr)
3315 outs() << format("0x%" PRIx64, n_value);
3317 outs() << " + " << format("0x%" PRIx64, m.name);
3319 outs() << format("0x%" PRIx64, m.name);
3320 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
3321 if (name != nullptr)
3322 outs() << format(" %.*s", left, name);
3325 outs() << indent << "\t\t types ";
3326 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
3327 info, n_value, m.types);
3329 if (info->verbose && sym_name != nullptr)
3332 outs() << format("0x%" PRIx64, n_value);
3334 outs() << " + " << format("0x%" PRIx64, m.types);
3336 outs() << format("0x%" PRIx64, m.types);
3337 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
3338 if (name != nullptr)
3339 outs() << format(" %.*s", left, name);
3342 outs() << indent << "\t\t imp ";
3343 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
3345 if (info->verbose && name == nullptr) {
3347 outs() << format("0x%" PRIx64, n_value) << " ";
3349 outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
3351 outs() << format("0x%" PRIx64, m.imp) << " ";
3353 if (name != nullptr)
3357 p += sizeof(struct method64_t);
3358 offset += sizeof(struct method64_t);
3362 static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
3363 const char *indent) {
3364 struct method_list32_t ml;
3365 struct method32_t m;
3366 const char *r, *name;
3367 uint32_t offset, xoffset, left, i;
3370 r = get_pointer_32(p, offset, left, S, info);
3373 memset(&ml, '\0', sizeof(struct method_list32_t));
3374 if (left < sizeof(struct method_list32_t)) {
3375 memcpy(&ml, r, left);
3376 outs() << " (method_list_t entends past the end of the section)\n";
3378 memcpy(&ml, r, sizeof(struct method_list32_t));
3379 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3381 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3382 outs() << indent << "\t\t count " << ml.count << "\n";
3384 p += sizeof(struct method_list32_t);
3385 offset += sizeof(struct method_list32_t);
3386 for (i = 0; i < ml.count; i++) {
3387 r = get_pointer_32(p, offset, left, S, info);
3390 memset(&m, '\0', sizeof(struct method32_t));
3391 if (left < sizeof(struct method32_t)) {
3392 memcpy(&ml, r, left);
3393 outs() << indent << " (method_t entends past the end of the section)\n";
3395 memcpy(&m, r, sizeof(struct method32_t));
3396 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3399 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
3400 name = get_pointer_32(m.name, xoffset, left, xS, info);
3401 if (name != nullptr)
3402 outs() << format(" %.*s", left, name);
3405 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
3406 name = get_pointer_32(m.types, xoffset, left, xS, info);
3407 if (name != nullptr)
3408 outs() << format(" %.*s", left, name);
3411 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
3412 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
3414 if (name != nullptr)
3415 outs() << " " << name;
3418 p += sizeof(struct method32_t);
3419 offset += sizeof(struct method32_t);
3423 static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
3424 uint32_t offset, left, xleft;
3426 struct objc_method_list_t method_list;
3427 struct objc_method_t method;
3428 const char *r, *methods, *name, *SymbolName;
3431 r = get_pointer_32(p, offset, left, S, info, true);
3436 if (left > sizeof(struct objc_method_list_t)) {
3437 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
3439 outs() << "\t\t objc_method_list extends past end of the section\n";
3440 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
3441 memcpy(&method_list, r, left);
3443 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3444 swapStruct(method_list);
3446 outs() << "\t\t obsolete "
3447 << format("0x%08" PRIx32, method_list.obsolete) << "\n";
3448 outs() << "\t\t method_count " << method_list.method_count << "\n";
3450 methods = r + sizeof(struct objc_method_list_t);
3451 for (i = 0; i < method_list.method_count; i++) {
3452 if ((i + 1) * sizeof(struct objc_method_t) > left) {
3453 outs() << "\t\t remaining method's extend past the of the section\n";
3456 memcpy(&method, methods + i * sizeof(struct objc_method_t),
3457 sizeof(struct objc_method_t));
3458 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3461 outs() << "\t\t method_name "
3462 << format("0x%08" PRIx32, method.method_name);
3463 if (info->verbose) {
3464 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
3465 if (name != nullptr)
3466 outs() << format(" %.*s", xleft, name);
3468 outs() << " (not in an __OBJC section)";
3472 outs() << "\t\t method_types "
3473 << format("0x%08" PRIx32, method.method_types);
3474 if (info->verbose) {
3475 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
3476 if (name != nullptr)
3477 outs() << format(" %.*s", xleft, name);
3479 outs() << " (not in an __OBJC section)";
3483 outs() << "\t\t method_imp "
3484 << format("0x%08" PRIx32, method.method_imp) << " ";
3485 if (info->verbose) {
3486 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
3487 if (SymbolName != nullptr)
3488 outs() << SymbolName;
3495 static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
3496 struct protocol_list64_t pl;
3497 uint64_t q, n_value;
3498 struct protocol64_t pc;
3500 uint32_t offset, xoffset, left, i;
3502 const char *name, *sym_name;
3504 r = get_pointer_64(p, offset, left, S, info);
3507 memset(&pl, '\0', sizeof(struct protocol_list64_t));
3508 if (left < sizeof(struct protocol_list64_t)) {
3509 memcpy(&pl, r, left);
3510 outs() << " (protocol_list_t entends past the end of the section)\n";
3512 memcpy(&pl, r, sizeof(struct protocol_list64_t));
3513 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3515 outs() << " count " << pl.count << "\n";
3517 p += sizeof(struct protocol_list64_t);
3518 offset += sizeof(struct protocol_list64_t);
3519 for (i = 0; i < pl.count; i++) {
3520 r = get_pointer_64(p, offset, left, S, info);
3524 if (left < sizeof(uint64_t)) {
3525 memcpy(&q, r, left);
3526 outs() << " (protocol_t * entends past the end of the section)\n";
3528 memcpy(&q, r, sizeof(uint64_t));
3529 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3530 sys::swapByteOrder(q);
3532 outs() << "\t\t list[" << i << "] ";
3533 sym_name = get_symbol_64(offset, S, info, n_value, q);
3535 if (info->verbose && sym_name != nullptr)
3538 outs() << format("0x%" PRIx64, n_value);
3540 outs() << " + " << format("0x%" PRIx64, q);
3542 outs() << format("0x%" PRIx64, q);
3543 outs() << " (struct protocol_t *)\n";
3545 r = get_pointer_64(q + n_value, offset, left, S, info);
3548 memset(&pc, '\0', sizeof(struct protocol64_t));
3549 if (left < sizeof(struct protocol64_t)) {
3550 memcpy(&pc, r, left);
3551 outs() << " (protocol_t entends past the end of the section)\n";
3553 memcpy(&pc, r, sizeof(struct protocol64_t));
3554 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3557 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
3559 outs() << "\t\t\t name ";
3560 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
3561 info, n_value, pc.name);
3563 if (info->verbose && sym_name != nullptr)
3566 outs() << format("0x%" PRIx64, n_value);
3568 outs() << " + " << format("0x%" PRIx64, pc.name);
3570 outs() << format("0x%" PRIx64, pc.name);
3571 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
3572 if (name != nullptr)
3573 outs() << format(" %.*s", left, name);
3576 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
3578 outs() << "\t\t instanceMethods ";
3580 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
3581 S, info, n_value, pc.instanceMethods);
3583 if (info->verbose && sym_name != nullptr)
3586 outs() << format("0x%" PRIx64, n_value);
3587 if (pc.instanceMethods != 0)
3588 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
3590 outs() << format("0x%" PRIx64, pc.instanceMethods);
3591 outs() << " (struct method_list_t *)\n";
3592 if (pc.instanceMethods + n_value != 0)
3593 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
3595 outs() << "\t\t classMethods ";
3597 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
3598 info, n_value, pc.classMethods);
3600 if (info->verbose && sym_name != nullptr)
3603 outs() << format("0x%" PRIx64, n_value);
3604 if (pc.classMethods != 0)
3605 outs() << " + " << format("0x%" PRIx64, pc.classMethods);
3607 outs() << format("0x%" PRIx64, pc.classMethods);
3608 outs() << " (struct method_list_t *)\n";
3609 if (pc.classMethods + n_value != 0)
3610 print_method_list64_t(pc.classMethods + n_value, info, "\t");
3612 outs() << "\t optionalInstanceMethods "
3613 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
3614 outs() << "\t optionalClassMethods "
3615 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
3616 outs() << "\t instanceProperties "
3617 << format("0x%" PRIx64, pc.instanceProperties) << "\n";
3619 p += sizeof(uint64_t);
3620 offset += sizeof(uint64_t);
3624 static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
3625 struct protocol_list32_t pl;
3627 struct protocol32_t pc;
3629 uint32_t offset, xoffset, left, i;
3633 r = get_pointer_32(p, offset, left, S, info);
3636 memset(&pl, '\0', sizeof(struct protocol_list32_t));
3637 if (left < sizeof(struct protocol_list32_t)) {
3638 memcpy(&pl, r, left);
3639 outs() << " (protocol_list_t entends past the end of the section)\n";
3641 memcpy(&pl, r, sizeof(struct protocol_list32_t));
3642 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3644 outs() << " count " << pl.count << "\n";
3646 p += sizeof(struct protocol_list32_t);
3647 offset += sizeof(struct protocol_list32_t);
3648 for (i = 0; i < pl.count; i++) {
3649 r = get_pointer_32(p, offset, left, S, info);
3653 if (left < sizeof(uint32_t)) {
3654 memcpy(&q, r, left);
3655 outs() << " (protocol_t * entends past the end of the section)\n";
3657 memcpy(&q, r, sizeof(uint32_t));
3658 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3659 sys::swapByteOrder(q);
3660 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
3661 << " (struct protocol_t *)\n";
3662 r = get_pointer_32(q, offset, left, S, info);
3665 memset(&pc, '\0', sizeof(struct protocol32_t));
3666 if (left < sizeof(struct protocol32_t)) {
3667 memcpy(&pc, r, left);
3668 outs() << " (protocol_t entends past the end of the section)\n";
3670 memcpy(&pc, r, sizeof(struct protocol32_t));
3671 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3673 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
3674 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
3675 name = get_pointer_32(pc.name, xoffset, left, xS, info);
3676 if (name != nullptr)
3677 outs() << format(" %.*s", left, name);
3679 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
3680 outs() << "\t\t instanceMethods "
3681 << format("0x%" PRIx32, pc.instanceMethods)
3682 << " (struct method_list_t *)\n";
3683 if (pc.instanceMethods != 0)
3684 print_method_list32_t(pc.instanceMethods, info, "\t");
3685 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
3686 << " (struct method_list_t *)\n";
3687 if (pc.classMethods != 0)
3688 print_method_list32_t(pc.classMethods, info, "\t");
3689 outs() << "\t optionalInstanceMethods "
3690 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
3691 outs() << "\t optionalClassMethods "
3692 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
3693 outs() << "\t instanceProperties "
3694 << format("0x%" PRIx32, pc.instanceProperties) << "\n";
3695 p += sizeof(uint32_t);
3696 offset += sizeof(uint32_t);
3700 static void print_indent(uint32_t indent) {
3701 for (uint32_t i = 0; i < indent;) {
3702 if (indent - i >= 8) {
3706 for (uint32_t j = i; j < indent; j++)
3713 static bool print_method_description_list(uint32_t p, uint32_t indent,
3714 struct DisassembleInfo *info) {
3715 uint32_t offset, left, xleft;
3717 struct objc_method_description_list_t mdl;
3718 struct objc_method_description_t md;
3719 const char *r, *list, *name;
3722 r = get_pointer_32(p, offset, left, S, info, true);
3727 if (left > sizeof(struct objc_method_description_list_t)) {
3728 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
3730 print_indent(indent);
3731 outs() << " objc_method_description_list extends past end of the section\n";
3732 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
3733 memcpy(&mdl, r, left);
3735 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3738 print_indent(indent);
3739 outs() << " count " << mdl.count << "\n";
3741 list = r + sizeof(struct objc_method_description_list_t);
3742 for (i = 0; i < mdl.count; i++) {
3743 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
3744 print_indent(indent);
3745 outs() << " remaining list entries extend past the of the section\n";
3748 print_indent(indent);
3749 outs() << " list[" << i << "]\n";
3750 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
3751 sizeof(struct objc_method_description_t));
3752 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3755 print_indent(indent);
3756 outs() << " name " << format("0x%08" PRIx32, md.name);
3757 if (info->verbose) {
3758 name = get_pointer_32(md.name, offset, xleft, S, info, true);
3759 if (name != nullptr)
3760 outs() << format(" %.*s", xleft, name);
3762 outs() << " (not in an __OBJC section)";
3766 print_indent(indent);
3767 outs() << " types " << format("0x%08" PRIx32, md.types);
3768 if (info->verbose) {
3769 name = get_pointer_32(md.types, offset, xleft, S, info, true);
3770 if (name != nullptr)
3771 outs() << format(" %.*s", xleft, name);
3773 outs() << " (not in an __OBJC section)";
3780 static bool print_protocol_list(uint32_t p, uint32_t indent,
3781 struct DisassembleInfo *info);
3783 static bool print_protocol(uint32_t p, uint32_t indent,
3784 struct DisassembleInfo *info) {
3785 uint32_t offset, left;
3787 struct objc_protocol_t protocol;
3788 const char *r, *name;
3790 r = get_pointer_32(p, offset, left, S, info, true);
3795 if (left >= sizeof(struct objc_protocol_t)) {
3796 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
3798 print_indent(indent);
3799 outs() << " Protocol extends past end of the section\n";
3800 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
3801 memcpy(&protocol, r, left);
3803 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3804 swapStruct(protocol);
3806 print_indent(indent);
3807 outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
3810 print_indent(indent);
3811 outs() << " protocol_name "
3812 << format("0x%08" PRIx32, protocol.protocol_name);
3813 if (info->verbose) {
3814 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
3815 if (name != nullptr)
3816 outs() << format(" %.*s", left, name);
3818 outs() << " (not in an __OBJC section)";
3822 print_indent(indent);
3823 outs() << " protocol_list "
3824 << format("0x%08" PRIx32, protocol.protocol_list);
3825 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
3826 outs() << " (not in an __OBJC section)\n";
3828 print_indent(indent);
3829 outs() << " instance_methods "
3830 << format("0x%08" PRIx32, protocol.instance_methods);
3831 if (print_method_description_list(protocol.instance_methods, indent, info))
3832 outs() << " (not in an __OBJC section)\n";
3834 print_indent(indent);
3835 outs() << " class_methods "
3836 << format("0x%08" PRIx32, protocol.class_methods);
3837 if (print_method_description_list(protocol.class_methods, indent, info))
3838 outs() << " (not in an __OBJC section)\n";
3843 static bool print_protocol_list(uint32_t p, uint32_t indent,
3844 struct DisassembleInfo *info) {
3845 uint32_t offset, left, l;
3847 struct objc_protocol_list_t protocol_list;
3848 const char *r, *list;
3851 r = get_pointer_32(p, offset, left, S, info, true);
3856 if (left > sizeof(struct objc_protocol_list_t)) {
3857 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
3859 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
3860 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
3861 memcpy(&protocol_list, r, left);
3863 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3864 swapStruct(protocol_list);
3866 print_indent(indent);
3867 outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
3869 print_indent(indent);
3870 outs() << " count " << protocol_list.count << "\n";
3872 list = r + sizeof(struct objc_protocol_list_t);
3873 for (i = 0; i < protocol_list.count; i++) {
3874 if ((i + 1) * sizeof(uint32_t) > left) {
3875 outs() << "\t\t remaining list entries extend past the of the section\n";
3878 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
3879 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3880 sys::swapByteOrder(l);
3882 print_indent(indent);
3883 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
3884 if (print_protocol(l, indent, info))
3885 outs() << "(not in an __OBJC section)\n";
3890 static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
3891 struct ivar_list64_t il;
3894 uint32_t offset, xoffset, left, j;
3896 const char *name, *sym_name, *ivar_offset_p;
3897 uint64_t ivar_offset, n_value;
3899 r = get_pointer_64(p, offset, left, S, info);
3902 memset(&il, '\0', sizeof(struct ivar_list64_t));
3903 if (left < sizeof(struct ivar_list64_t)) {
3904 memcpy(&il, r, left);
3905 outs() << " (ivar_list_t entends past the end of the section)\n";
3907 memcpy(&il, r, sizeof(struct ivar_list64_t));
3908 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3910 outs() << " entsize " << il.entsize << "\n";
3911 outs() << " count " << il.count << "\n";
3913 p += sizeof(struct ivar_list64_t);
3914 offset += sizeof(struct ivar_list64_t);
3915 for (j = 0; j < il.count; j++) {
3916 r = get_pointer_64(p, offset, left, S, info);
3919 memset(&i, '\0', sizeof(struct ivar64_t));
3920 if (left < sizeof(struct ivar64_t)) {
3921 memcpy(&i, r, left);
3922 outs() << " (ivar_t entends past the end of the section)\n";
3924 memcpy(&i, r, sizeof(struct ivar64_t));
3925 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3928 outs() << "\t\t\t offset ";
3929 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
3930 info, n_value, i.offset);
3932 if (info->verbose && sym_name != nullptr)
3935 outs() << format("0x%" PRIx64, n_value);
3937 outs() << " + " << format("0x%" PRIx64, i.offset);
3939 outs() << format("0x%" PRIx64, i.offset);
3940 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
3941 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
3942 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
3943 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3944 sys::swapByteOrder(ivar_offset);
3945 outs() << " " << ivar_offset << "\n";
3949 outs() << "\t\t\t name ";
3950 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
3953 if (info->verbose && sym_name != nullptr)
3956 outs() << format("0x%" PRIx64, n_value);
3958 outs() << " + " << format("0x%" PRIx64, i.name);
3960 outs() << format("0x%" PRIx64, i.name);
3961 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
3962 if (name != nullptr)
3963 outs() << format(" %.*s", left, name);
3966 outs() << "\t\t\t type ";
3967 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
3969 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
3971 if (info->verbose && sym_name != nullptr)
3974 outs() << format("0x%" PRIx64, n_value);
3976 outs() << " + " << format("0x%" PRIx64, i.type);
3978 outs() << format("0x%" PRIx64, i.type);
3979 if (name != nullptr)
3980 outs() << format(" %.*s", left, name);
3983 outs() << "\t\t\talignment " << i.alignment << "\n";
3984 outs() << "\t\t\t size " << i.size << "\n";
3986 p += sizeof(struct ivar64_t);
3987 offset += sizeof(struct ivar64_t);
3991 static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
3992 struct ivar_list32_t il;
3995 uint32_t offset, xoffset, left, j;
3997 const char *name, *ivar_offset_p;
3998 uint32_t ivar_offset;
4000 r = get_pointer_32(p, offset, left, S, info);
4003 memset(&il, '\0', sizeof(struct ivar_list32_t));
4004 if (left < sizeof(struct ivar_list32_t)) {
4005 memcpy(&il, r, left);
4006 outs() << " (ivar_list_t entends past the end of the section)\n";
4008 memcpy(&il, r, sizeof(struct ivar_list32_t));
4009 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4011 outs() << " entsize " << il.entsize << "\n";
4012 outs() << " count " << il.count << "\n";
4014 p += sizeof(struct ivar_list32_t);
4015 offset += sizeof(struct ivar_list32_t);
4016 for (j = 0; j < il.count; j++) {
4017 r = get_pointer_32(p, offset, left, S, info);
4020 memset(&i, '\0', sizeof(struct ivar32_t));
4021 if (left < sizeof(struct ivar32_t)) {
4022 memcpy(&i, r, left);
4023 outs() << " (ivar_t entends past the end of the section)\n";
4025 memcpy(&i, r, sizeof(struct ivar32_t));
4026 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4029 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
4030 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4031 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4032 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4033 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4034 sys::swapByteOrder(ivar_offset);
4035 outs() << " " << ivar_offset << "\n";
4039 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
4040 name = get_pointer_32(i.name, xoffset, left, xS, info);
4041 if (name != nullptr)
4042 outs() << format(" %.*s", left, name);
4045 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
4046 name = get_pointer_32(i.type, xoffset, left, xS, info);
4047 if (name != nullptr)
4048 outs() << format(" %.*s", left, name);
4051 outs() << "\t\t\talignment " << i.alignment << "\n";
4052 outs() << "\t\t\t size " << i.size << "\n";
4054 p += sizeof(struct ivar32_t);
4055 offset += sizeof(struct ivar32_t);
4059 static void print_objc_property_list64(uint64_t p,
4060 struct DisassembleInfo *info) {
4061 struct objc_property_list64 opl;
4062 struct objc_property64 op;
4064 uint32_t offset, xoffset, left, j;
4066 const char *name, *sym_name;
4069 r = get_pointer_64(p, offset, left, S, info);
4072 memset(&opl, '\0', sizeof(struct objc_property_list64));
4073 if (left < sizeof(struct objc_property_list64)) {
4074 memcpy(&opl, r, left);
4075 outs() << " (objc_property_list entends past the end of the section)\n";
4077 memcpy(&opl, r, sizeof(struct objc_property_list64));
4078 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4080 outs() << " entsize " << opl.entsize << "\n";
4081 outs() << " count " << opl.count << "\n";
4083 p += sizeof(struct objc_property_list64);
4084 offset += sizeof(struct objc_property_list64);
4085 for (j = 0; j < opl.count; j++) {
4086 r = get_pointer_64(p, offset, left, S, info);
4089 memset(&op, '\0', sizeof(struct objc_property64));
4090 if (left < sizeof(struct objc_property64)) {
4091 memcpy(&op, r, left);
4092 outs() << " (objc_property entends past the end of the section)\n";
4094 memcpy(&op, r, sizeof(struct objc_property64));
4095 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4098 outs() << "\t\t\t name ";
4099 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
4100 info, n_value, op.name);
4102 if (info->verbose && sym_name != nullptr)
4105 outs() << format("0x%" PRIx64, n_value);
4107 outs() << " + " << format("0x%" PRIx64, op.name);
4109 outs() << format("0x%" PRIx64, op.name);
4110 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
4111 if (name != nullptr)
4112 outs() << format(" %.*s", left, name);
4115 outs() << "\t\t\tattributes ";
4117 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
4118 info, n_value, op.attributes);
4120 if (info->verbose && sym_name != nullptr)
4123 outs() << format("0x%" PRIx64, n_value);
4124 if (op.attributes != 0)
4125 outs() << " + " << format("0x%" PRIx64, op.attributes);
4127 outs() << format("0x%" PRIx64, op.attributes);
4128 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
4129 if (name != nullptr)
4130 outs() << format(" %.*s", left, name);
4133 p += sizeof(struct objc_property64);
4134 offset += sizeof(struct objc_property64);
4138 static void print_objc_property_list32(uint32_t p,
4139 struct DisassembleInfo *info) {
4140 struct objc_property_list32 opl;
4141 struct objc_property32 op;
4143 uint32_t offset, xoffset, left, j;
4147 r = get_pointer_32(p, offset, left, S, info);
4150 memset(&opl, '\0', sizeof(struct objc_property_list32));
4151 if (left < sizeof(struct objc_property_list32)) {
4152 memcpy(&opl, r, left);
4153 outs() << " (objc_property_list entends past the end of the section)\n";
4155 memcpy(&opl, r, sizeof(struct objc_property_list32));
4156 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4158 outs() << " entsize " << opl.entsize << "\n";
4159 outs() << " count " << opl.count << "\n";
4161 p += sizeof(struct objc_property_list32);
4162 offset += sizeof(struct objc_property_list32);
4163 for (j = 0; j < opl.count; j++) {
4164 r = get_pointer_32(p, offset, left, S, info);
4167 memset(&op, '\0', sizeof(struct objc_property32));
4168 if (left < sizeof(struct objc_property32)) {
4169 memcpy(&op, r, left);
4170 outs() << " (objc_property entends past the end of the section)\n";
4172 memcpy(&op, r, sizeof(struct objc_property32));
4173 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4176 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
4177 name = get_pointer_32(op.name, xoffset, left, xS, info);
4178 if (name != nullptr)
4179 outs() << format(" %.*s", left, name);
4182 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
4183 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
4184 if (name != nullptr)
4185 outs() << format(" %.*s", left, name);
4188 p += sizeof(struct objc_property32);
4189 offset += sizeof(struct objc_property32);
4193 static void print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
4194 bool &is_meta_class) {
4195 struct class_ro64_t cro;
4197 uint32_t offset, xoffset, left;
4199 const char *name, *sym_name;
4202 r = get_pointer_64(p, offset, left, S, info);
4203 if (r == nullptr || left < sizeof(struct class_ro64_t))
4205 memset(&cro, '\0', sizeof(struct class_ro64_t));
4206 if (left < sizeof(struct class_ro64_t)) {
4207 memcpy(&cro, r, left);
4208 outs() << " (class_ro_t entends past the end of the section)\n";
4210 memcpy(&cro, r, sizeof(struct class_ro64_t));
4211 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4213 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4214 if (cro.flags & RO_META)
4215 outs() << " RO_META";
4216 if (cro.flags & RO_ROOT)
4217 outs() << " RO_ROOT";
4218 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4219 outs() << " RO_HAS_CXX_STRUCTORS";
4221 outs() << " instanceStart " << cro.instanceStart << "\n";
4222 outs() << " instanceSize " << cro.instanceSize << "\n";
4223 outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
4225 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
4227 print_layout_map64(cro.ivarLayout, info);
4230 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
4231 info, n_value, cro.name);
4233 if (info->verbose && sym_name != nullptr)
4236 outs() << format("0x%" PRIx64, n_value);
4238 outs() << " + " << format("0x%" PRIx64, cro.name);
4240 outs() << format("0x%" PRIx64, cro.name);
4241 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
4242 if (name != nullptr)
4243 outs() << format(" %.*s", left, name);
4246 outs() << " baseMethods ";
4247 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
4248 S, info, n_value, cro.baseMethods);
4250 if (info->verbose && sym_name != nullptr)
4253 outs() << format("0x%" PRIx64, n_value);
4254 if (cro.baseMethods != 0)
4255 outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
4257 outs() << format("0x%" PRIx64, cro.baseMethods);
4258 outs() << " (struct method_list_t *)\n";
4259 if (cro.baseMethods + n_value != 0)
4260 print_method_list64_t(cro.baseMethods + n_value, info, "");
4262 outs() << " baseProtocols ";
4264 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
4265 info, n_value, cro.baseProtocols);
4267 if (info->verbose && sym_name != nullptr)
4270 outs() << format("0x%" PRIx64, n_value);
4271 if (cro.baseProtocols != 0)
4272 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
4274 outs() << format("0x%" PRIx64, cro.baseProtocols);
4276 if (cro.baseProtocols + n_value != 0)
4277 print_protocol_list64_t(cro.baseProtocols + n_value, info);
4279 outs() << " ivars ";
4280 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
4281 info, n_value, cro.ivars);
4283 if (info->verbose && sym_name != nullptr)
4286 outs() << format("0x%" PRIx64, n_value);
4288 outs() << " + " << format("0x%" PRIx64, cro.ivars);
4290 outs() << format("0x%" PRIx64, cro.ivars);
4292 if (cro.ivars + n_value != 0)
4293 print_ivar_list64_t(cro.ivars + n_value, info);
4295 outs() << " weakIvarLayout ";
4297 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
4298 info, n_value, cro.weakIvarLayout);
4300 if (info->verbose && sym_name != nullptr)
4303 outs() << format("0x%" PRIx64, n_value);
4304 if (cro.weakIvarLayout != 0)
4305 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
4307 outs() << format("0x%" PRIx64, cro.weakIvarLayout);
4309 print_layout_map64(cro.weakIvarLayout + n_value, info);
4311 outs() << " baseProperties ";
4313 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
4314 info, n_value, cro.baseProperties);
4316 if (info->verbose && sym_name != nullptr)
4319 outs() << format("0x%" PRIx64, n_value);
4320 if (cro.baseProperties != 0)
4321 outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
4323 outs() << format("0x%" PRIx64, cro.baseProperties);
4325 if (cro.baseProperties + n_value != 0)
4326 print_objc_property_list64(cro.baseProperties + n_value, info);
4328 is_meta_class = (cro.flags & RO_META) ? true : false;
4331 static void print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
4332 bool &is_meta_class) {
4333 struct class_ro32_t cro;
4335 uint32_t offset, xoffset, left;
4339 r = get_pointer_32(p, offset, left, S, info);
4342 memset(&cro, '\0', sizeof(struct class_ro32_t));
4343 if (left < sizeof(struct class_ro32_t)) {
4344 memcpy(&cro, r, left);
4345 outs() << " (class_ro_t entends past the end of the section)\n";
4347 memcpy(&cro, r, sizeof(struct class_ro32_t));
4348 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4350 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4351 if (cro.flags & RO_META)
4352 outs() << " RO_META";
4353 if (cro.flags & RO_ROOT)
4354 outs() << " RO_ROOT";
4355 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4356 outs() << " RO_HAS_CXX_STRUCTORS";
4358 outs() << " instanceStart " << cro.instanceStart << "\n";
4359 outs() << " instanceSize " << cro.instanceSize << "\n";
4360 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
4362 print_layout_map32(cro.ivarLayout, info);
4364 outs() << " name " << format("0x%" PRIx32, cro.name);
4365 name = get_pointer_32(cro.name, xoffset, left, xS, info);
4366 if (name != nullptr)
4367 outs() << format(" %.*s", left, name);
4370 outs() << " baseMethods "
4371 << format("0x%" PRIx32, cro.baseMethods)
4372 << " (struct method_list_t *)\n";
4373 if (cro.baseMethods != 0)
4374 print_method_list32_t(cro.baseMethods, info, "");
4376 outs() << " baseProtocols "
4377 << format("0x%" PRIx32, cro.baseProtocols) << "\n";
4378 if (cro.baseProtocols != 0)
4379 print_protocol_list32_t(cro.baseProtocols, info);
4380 outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
4383 print_ivar_list32_t(cro.ivars, info);
4384 outs() << " weakIvarLayout "
4385 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
4386 print_layout_map32(cro.weakIvarLayout, info);
4387 outs() << " baseProperties "
4388 << format("0x%" PRIx32, cro.baseProperties) << "\n";
4389 if (cro.baseProperties != 0)
4390 print_objc_property_list32(cro.baseProperties, info);
4391 is_meta_class = (cro.flags & RO_META) ? true : false;
4394 static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
4397 uint32_t offset, left;
4400 uint64_t isa_n_value, n_value;
4402 r = get_pointer_64(p, offset, left, S, info);
4403 if (r == nullptr || left < sizeof(struct class64_t))
4405 memset(&c, '\0', sizeof(struct class64_t));
4406 if (left < sizeof(struct class64_t)) {
4407 memcpy(&c, r, left);
4408 outs() << " (class_t entends past the end of the section)\n";
4410 memcpy(&c, r, sizeof(struct class64_t));
4411 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4414 outs() << " isa " << format("0x%" PRIx64, c.isa);
4415 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
4416 isa_n_value, c.isa);
4417 if (name != nullptr)
4418 outs() << " " << name;
4421 outs() << " superclass " << format("0x%" PRIx64, c.superclass);
4422 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
4423 n_value, c.superclass);
4424 if (name != nullptr)
4425 outs() << " " << name;
4428 outs() << " cache " << format("0x%" PRIx64, c.cache);
4429 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
4431 if (name != nullptr)
4432 outs() << " " << name;
4435 outs() << " vtable " << format("0x%" PRIx64, c.vtable);
4436 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
4438 if (name != nullptr)
4439 outs() << " " << name;
4442 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
4446 if (info->verbose && name != nullptr)
4449 outs() << format("0x%" PRIx64, n_value);
4451 outs() << " + " << format("0x%" PRIx64, c.data);
4453 outs() << format("0x%" PRIx64, c.data);
4454 outs() << " (struct class_ro_t *)";
4456 // This is a Swift class if some of the low bits of the pointer are set.
4457 if ((c.data + n_value) & 0x7)
4458 outs() << " Swift class";
4461 print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class);
4463 if (is_meta_class == false) {
4464 outs() << "Meta Class\n";
4465 print_class64_t(c.isa + isa_n_value, info);
4469 static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
4472 uint32_t offset, left;
4476 r = get_pointer_32(p, offset, left, S, info);
4479 memset(&c, '\0', sizeof(struct class32_t));
4480 if (left < sizeof(struct class32_t)) {
4481 memcpy(&c, r, left);
4482 outs() << " (class_t entends past the end of the section)\n";
4484 memcpy(&c, r, sizeof(struct class32_t));
4485 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4488 outs() << " isa " << format("0x%" PRIx32, c.isa);
4490 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
4491 if (name != nullptr)
4492 outs() << " " << name;
4495 outs() << " superclass " << format("0x%" PRIx32, c.superclass);
4496 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
4498 if (name != nullptr)
4499 outs() << " " << name;
4502 outs() << " cache " << format("0x%" PRIx32, c.cache);
4503 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
4505 if (name != nullptr)
4506 outs() << " " << name;
4509 outs() << " vtable " << format("0x%" PRIx32, c.vtable);
4510 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
4512 if (name != nullptr)
4513 outs() << " " << name;
4517 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
4518 outs() << " data " << format("0x%" PRIx32, c.data)
4519 << " (struct class_ro_t *)";
4521 // This is a Swift class if some of the low bits of the pointer are set.
4523 outs() << " Swift class";
4526 print_class_ro32_t(c.data & ~0x3, info, is_meta_class);
4528 if (is_meta_class == false) {
4529 outs() << "Meta Class\n";
4530 print_class32_t(c.isa, info);
4534 static void print_objc_class_t(struct objc_class_t *objc_class,
4535 struct DisassembleInfo *info) {
4536 uint32_t offset, left, xleft;
4537 const char *name, *p, *ivar_list;
4540 struct objc_ivar_list_t objc_ivar_list;
4541 struct objc_ivar_t ivar;
4543 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
4544 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
4545 name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
4546 if (name != nullptr)
4547 outs() << format(" %.*s", left, name);
4549 outs() << " (not in an __OBJC section)";
4553 outs() << "\t super_class "
4554 << format("0x%08" PRIx32, objc_class->super_class);
4555 if (info->verbose) {
4556 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
4557 if (name != nullptr)
4558 outs() << format(" %.*s", left, name);
4560 outs() << " (not in an __OBJC section)";
4564 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name);
4565 if (info->verbose) {
4566 name = get_pointer_32(objc_class->name, offset, left, S, info, true);
4567 if (name != nullptr)
4568 outs() << format(" %.*s", left, name);
4570 outs() << " (not in an __OBJC section)";
4574 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
4577 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
4578 if (info->verbose) {
4579 if (CLS_GETINFO(objc_class, CLS_CLASS))
4580 outs() << " CLS_CLASS";
4581 else if (CLS_GETINFO(objc_class, CLS_META))
4582 outs() << " CLS_META";
4586 outs() << "\t instance_size "
4587 << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
4589 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
4590 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
4592 if (left > sizeof(struct objc_ivar_list_t)) {
4594 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
4596 outs() << " (entends past the end of the section)\n";
4597 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
4598 memcpy(&objc_ivar_list, p, left);
4600 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4601 swapStruct(objc_ivar_list);
4602 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
4603 ivar_list = p + sizeof(struct objc_ivar_list_t);
4604 for (i = 0; i < objc_ivar_list.ivar_count; i++) {
4605 if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
4606 outs() << "\t\t remaining ivar's extend past the of the section\n";
4609 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
4610 sizeof(struct objc_ivar_t));
4611 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4614 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
4615 if (info->verbose) {
4616 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
4617 if (name != nullptr)
4618 outs() << format(" %.*s", xleft, name);
4620 outs() << " (not in an __OBJC section)";
4624 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
4625 if (info->verbose) {
4626 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
4627 if (name != nullptr)
4628 outs() << format(" %.*s", xleft, name);
4630 outs() << " (not in an __OBJC section)";
4634 outs() << "\t\t ivar_offset "
4635 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
4638 outs() << " (not in an __OBJC section)\n";
4641 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
4642 if (print_method_list(objc_class->methodLists, info))
4643 outs() << " (not in an __OBJC section)\n";
4645 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
4648 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
4649 if (print_protocol_list(objc_class->protocols, 16, info))
4650 outs() << " (not in an __OBJC section)\n";
4653 static void print_objc_objc_category_t(struct objc_category_t *objc_category,
4654 struct DisassembleInfo *info) {
4655 uint32_t offset, left;
4659 outs() << "\t category name "
4660 << format("0x%08" PRIx32, objc_category->category_name);
4661 if (info->verbose) {
4662 name = get_pointer_32(objc_category->category_name, offset, left, S, info,
4664 if (name != nullptr)
4665 outs() << format(" %.*s", left, name);
4667 outs() << " (not in an __OBJC section)";
4671 outs() << "\t\t class name "
4672 << format("0x%08" PRIx32, objc_category->class_name);
4673 if (info->verbose) {
4675 get_pointer_32(objc_category->class_name, offset, left, S, info, true);
4676 if (name != nullptr)
4677 outs() << format(" %.*s", left, name);
4679 outs() << " (not in an __OBJC section)";
4683 outs() << "\t instance methods "
4684 << format("0x%08" PRIx32, objc_category->instance_methods);
4685 if (print_method_list(objc_category->instance_methods, info))
4686 outs() << " (not in an __OBJC section)\n";
4688 outs() << "\t class methods "
4689 << format("0x%08" PRIx32, objc_category->class_methods);
4690 if (print_method_list(objc_category->class_methods, info))
4691 outs() << " (not in an __OBJC section)\n";
4694 static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
4695 struct category64_t c;
4697 uint32_t offset, xoffset, left;
4699 const char *name, *sym_name;
4702 r = get_pointer_64(p, offset, left, S, info);
4705 memset(&c, '\0', sizeof(struct category64_t));
4706 if (left < sizeof(struct category64_t)) {
4707 memcpy(&c, r, left);
4708 outs() << " (category_t entends past the end of the section)\n";
4710 memcpy(&c, r, sizeof(struct category64_t));
4711 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4715 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
4716 info, n_value, c.name);
4718 if (info->verbose && sym_name != nullptr)
4721 outs() << format("0x%" PRIx64, n_value);
4723 outs() << " + " << format("0x%" PRIx64, c.name);
4725 outs() << format("0x%" PRIx64, c.name);
4726 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
4727 if (name != nullptr)
4728 outs() << format(" %.*s", left, name);
4732 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
4735 if (info->verbose && sym_name != nullptr)
4738 outs() << format("0x%" PRIx64, n_value);
4740 outs() << " + " << format("0x%" PRIx64, c.cls);
4742 outs() << format("0x%" PRIx64, c.cls);
4744 if (c.cls + n_value != 0)
4745 print_class64_t(c.cls + n_value, info);
4747 outs() << " instanceMethods ";
4749 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
4750 info, n_value, c.instanceMethods);
4752 if (info->verbose && sym_name != nullptr)
4755 outs() << format("0x%" PRIx64, n_value);
4756 if (c.instanceMethods != 0)
4757 outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
4759 outs() << format("0x%" PRIx64, c.instanceMethods);
4761 if (c.instanceMethods + n_value != 0)
4762 print_method_list64_t(c.instanceMethods + n_value, info, "");
4764 outs() << " classMethods ";
4765 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
4766 S, info, n_value, c.classMethods);
4768 if (info->verbose && sym_name != nullptr)
4771 outs() << format("0x%" PRIx64, n_value);
4772 if (c.classMethods != 0)
4773 outs() << " + " << format("0x%" PRIx64, c.classMethods);
4775 outs() << format("0x%" PRIx64, c.classMethods);
4777 if (c.classMethods + n_value != 0)
4778 print_method_list64_t(c.classMethods + n_value, info, "");
4780 outs() << " protocols ";
4781 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
4782 info, n_value, c.protocols);
4784 if (info->verbose && sym_name != nullptr)
4787 outs() << format("0x%" PRIx64, n_value);
4788 if (c.protocols != 0)
4789 outs() << " + " << format("0x%" PRIx64, c.protocols);
4791 outs() << format("0x%" PRIx64, c.protocols);
4793 if (c.protocols + n_value != 0)
4794 print_protocol_list64_t(c.protocols + n_value, info);
4796 outs() << "instanceProperties ";
4798 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
4799 S, info, n_value, c.instanceProperties);
4801 if (info->verbose && sym_name != nullptr)
4804 outs() << format("0x%" PRIx64, n_value);
4805 if (c.instanceProperties != 0)
4806 outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
4808 outs() << format("0x%" PRIx64, c.instanceProperties);
4810 if (c.instanceProperties + n_value != 0)
4811 print_objc_property_list64(c.instanceProperties + n_value, info);
4814 static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
4815 struct category32_t c;
4817 uint32_t offset, left;
4821 r = get_pointer_32(p, offset, left, S, info);
4824 memset(&c, '\0', sizeof(struct category32_t));
4825 if (left < sizeof(struct category32_t)) {
4826 memcpy(&c, r, left);
4827 outs() << " (category_t entends past the end of the section)\n";
4829 memcpy(&c, r, sizeof(struct category32_t));
4830 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4833 outs() << " name " << format("0x%" PRIx32, c.name);
4834 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
4837 outs() << " " << name;
4840 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
4842 print_class32_t(c.cls, info);
4843 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
4845 if (c.instanceMethods != 0)
4846 print_method_list32_t(c.instanceMethods, info, "");
4847 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
4849 if (c.classMethods != 0)
4850 print_method_list32_t(c.classMethods, info, "");
4851 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
4852 if (c.protocols != 0)
4853 print_protocol_list32_t(c.protocols, info);
4854 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
4856 if (c.instanceProperties != 0)
4857 print_objc_property_list32(c.instanceProperties, info);
4860 static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
4861 uint32_t i, left, offset, xoffset;
4862 uint64_t p, n_value;
4863 struct message_ref64 mr;
4864 const char *name, *sym_name;
4868 if (S == SectionRef())
4872 S.getName(SectName);
4873 DataRefImpl Ref = S.getRawDataRefImpl();
4874 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4875 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4877 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
4878 p = S.getAddress() + i;
4879 r = get_pointer_64(p, offset, left, S, info);
4882 memset(&mr, '\0', sizeof(struct message_ref64));
4883 if (left < sizeof(struct message_ref64)) {
4884 memcpy(&mr, r, left);
4885 outs() << " (message_ref entends past the end of the section)\n";
4887 memcpy(&mr, r, sizeof(struct message_ref64));
4888 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4892 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
4895 outs() << format("0x%" PRIx64, n_value) << " ";
4897 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
4899 outs() << format("0x%" PRIx64, mr.imp) << " ";
4900 if (name != nullptr)
4901 outs() << " " << name;
4905 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
4906 info, n_value, mr.sel);
4908 if (info->verbose && sym_name != nullptr)
4911 outs() << format("0x%" PRIx64, n_value);
4913 outs() << " + " << format("0x%" PRIx64, mr.sel);
4915 outs() << format("0x%" PRIx64, mr.sel);
4916 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
4917 if (name != nullptr)
4918 outs() << format(" %.*s", left, name);
4921 offset += sizeof(struct message_ref64);
4925 static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
4926 uint32_t i, left, offset, xoffset, p;
4927 struct message_ref32 mr;
4928 const char *name, *r;
4931 if (S == SectionRef())
4935 S.getName(SectName);
4936 DataRefImpl Ref = S.getRawDataRefImpl();
4937 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4938 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4940 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
4941 p = S.getAddress() + i;
4942 r = get_pointer_32(p, offset, left, S, info);
4945 memset(&mr, '\0', sizeof(struct message_ref32));
4946 if (left < sizeof(struct message_ref32)) {
4947 memcpy(&mr, r, left);
4948 outs() << " (message_ref entends past the end of the section)\n";
4950 memcpy(&mr, r, sizeof(struct message_ref32));
4951 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4954 outs() << " imp " << format("0x%" PRIx32, mr.imp);
4955 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
4957 if (name != nullptr)
4958 outs() << " " << name;
4961 outs() << " sel " << format("0x%" PRIx32, mr.sel);
4962 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
4963 if (name != nullptr)
4964 outs() << " " << name;
4967 offset += sizeof(struct message_ref32);
4971 static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
4972 uint32_t left, offset, swift_version;
4974 struct objc_image_info64 o;
4978 S.getName(SectName);
4979 DataRefImpl Ref = S.getRawDataRefImpl();
4980 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4981 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4983 r = get_pointer_64(p, offset, left, S, info);
4986 memset(&o, '\0', sizeof(struct objc_image_info64));
4987 if (left < sizeof(struct objc_image_info64)) {
4988 memcpy(&o, r, left);
4989 outs() << " (objc_image_info entends past the end of the section)\n";
4991 memcpy(&o, r, sizeof(struct objc_image_info64));
4992 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4994 outs() << " version " << o.version << "\n";
4995 outs() << " flags " << format("0x%" PRIx32, o.flags);
4996 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
4997 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
4998 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
4999 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5000 swift_version = (o.flags >> 8) & 0xff;
5001 if (swift_version != 0) {
5002 if (swift_version == 1)
5003 outs() << " Swift 1.0";
5004 else if (swift_version == 2)
5005 outs() << " Swift 1.1";
5007 outs() << " unknown future Swift version (" << swift_version << ")";
5012 static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5013 uint32_t left, offset, swift_version, p;
5014 struct objc_image_info32 o;
5018 S.getName(SectName);
5019 DataRefImpl Ref = S.getRawDataRefImpl();
5020 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5021 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5023 r = get_pointer_32(p, offset, left, S, info);
5026 memset(&o, '\0', sizeof(struct objc_image_info32));
5027 if (left < sizeof(struct objc_image_info32)) {
5028 memcpy(&o, r, left);
5029 outs() << " (objc_image_info entends past the end of the section)\n";
5031 memcpy(&o, r, sizeof(struct objc_image_info32));
5032 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5034 outs() << " version " << o.version << "\n";
5035 outs() << " flags " << format("0x%" PRIx32, o.flags);
5036 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5037 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5038 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5039 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5040 swift_version = (o.flags >> 8) & 0xff;
5041 if (swift_version != 0) {
5042 if (swift_version == 1)
5043 outs() << " Swift 1.0";
5044 else if (swift_version == 2)
5045 outs() << " Swift 1.1";
5047 outs() << " unknown future Swift version (" << swift_version << ")";
5052 static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5053 uint32_t left, offset, p;
5054 struct imageInfo_t o;
5058 S.getName(SectName);
5059 DataRefImpl Ref = S.getRawDataRefImpl();
5060 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5061 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5063 r = get_pointer_32(p, offset, left, S, info);
5066 memset(&o, '\0', sizeof(struct imageInfo_t));
5067 if (left < sizeof(struct imageInfo_t)) {
5068 memcpy(&o, r, left);
5069 outs() << " (imageInfo entends past the end of the section)\n";
5071 memcpy(&o, r, sizeof(struct imageInfo_t));
5072 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5074 outs() << " version " << o.version << "\n";
5075 outs() << " flags " << format("0x%" PRIx32, o.flags);
5081 outs() << " GC-only";
5087 static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
5088 SymbolAddressMap AddrMap;
5090 CreateSymbolAddressMap(O, &AddrMap);
5092 std::vector<SectionRef> Sections;
5093 for (const SectionRef &Section : O->sections()) {
5095 Section.getName(SectName);
5096 Sections.push_back(Section);
5099 struct DisassembleInfo info;
5100 // Set up the block of info used by the Symbolizer call backs.
5101 info.verbose = verbose;
5103 info.AddrMap = &AddrMap;
5104 info.Sections = &Sections;
5105 info.class_name = nullptr;
5106 info.selector_name = nullptr;
5107 info.method = nullptr;
5108 info.demangled_name = nullptr;
5109 info.bindtable = nullptr;
5113 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5114 if (CL != SectionRef()) {
5116 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5118 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5120 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5123 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5124 if (CR != SectionRef()) {
5126 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5128 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5130 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5133 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5134 if (SR != SectionRef()) {
5136 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5138 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5140 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5143 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5144 if (CA != SectionRef()) {
5146 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5148 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5150 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5153 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5154 if (PL != SectionRef()) {
5156 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5158 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5160 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5163 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5164 if (MR != SectionRef()) {
5166 print_message_refs64(MR, &info);
5168 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5170 print_message_refs64(MR, &info);
5173 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5174 if (II != SectionRef()) {
5176 print_image_info64(II, &info);
5178 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5180 print_image_info64(II, &info);
5183 if (info.bindtable != nullptr)
5184 delete info.bindtable;
5187 static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
5188 SymbolAddressMap AddrMap;
5190 CreateSymbolAddressMap(O, &AddrMap);
5192 std::vector<SectionRef> Sections;
5193 for (const SectionRef &Section : O->sections()) {
5195 Section.getName(SectName);
5196 Sections.push_back(Section);
5199 struct DisassembleInfo info;
5200 // Set up the block of info used by the Symbolizer call backs.
5201 info.verbose = verbose;
5203 info.AddrMap = &AddrMap;
5204 info.Sections = &Sections;
5205 info.class_name = nullptr;
5206 info.selector_name = nullptr;
5207 info.method = nullptr;
5208 info.demangled_name = nullptr;
5209 info.bindtable = nullptr;
5213 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5214 if (CL != SectionRef()) {
5216 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5218 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5220 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5223 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5224 if (CR != SectionRef()) {
5226 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5228 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5230 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5233 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5234 if (SR != SectionRef()) {
5236 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5238 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5240 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5243 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5244 if (CA != SectionRef()) {
5246 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5248 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5250 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5253 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5254 if (PL != SectionRef()) {
5256 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5258 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5260 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5263 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5264 if (MR != SectionRef()) {
5266 print_message_refs32(MR, &info);
5268 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5270 print_message_refs32(MR, &info);
5273 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5274 if (II != SectionRef()) {
5276 print_image_info32(II, &info);
5278 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5280 print_image_info32(II, &info);
5284 static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
5285 uint32_t i, j, p, offset, xoffset, left, defs_left, def;
5286 const char *r, *name, *defs;
5287 struct objc_module_t module;
5289 struct objc_symtab_t symtab;
5290 struct objc_class_t objc_class;
5291 struct objc_category_t objc_category;
5293 outs() << "Objective-C segment\n";
5294 S = get_section(O, "__OBJC", "__module_info");
5295 if (S == SectionRef())
5298 SymbolAddressMap AddrMap;
5300 CreateSymbolAddressMap(O, &AddrMap);
5302 std::vector<SectionRef> Sections;
5303 for (const SectionRef &Section : O->sections()) {
5305 Section.getName(SectName);
5306 Sections.push_back(Section);
5309 struct DisassembleInfo info;
5310 // Set up the block of info used by the Symbolizer call backs.
5311 info.verbose = verbose;
5313 info.AddrMap = &AddrMap;
5314 info.Sections = &Sections;
5315 info.class_name = nullptr;
5316 info.selector_name = nullptr;
5317 info.method = nullptr;
5318 info.demangled_name = nullptr;
5319 info.bindtable = nullptr;
5323 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
5324 p = S.getAddress() + i;
5325 r = get_pointer_32(p, offset, left, S, &info, true);
5328 memset(&module, '\0', sizeof(struct objc_module_t));
5329 if (left < sizeof(struct objc_module_t)) {
5330 memcpy(&module, r, left);
5331 outs() << " (module extends past end of __module_info section)\n";
5333 memcpy(&module, r, sizeof(struct objc_module_t));
5334 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5337 outs() << "Module " << format("0x%" PRIx32, p) << "\n";
5338 outs() << " version " << module.version << "\n";
5339 outs() << " size " << module.size << "\n";
5341 name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
5342 if (name != nullptr)
5343 outs() << format("%.*s", left, name);
5345 outs() << format("0x%08" PRIx32, module.name)
5346 << "(not in an __OBJC section)";
5349 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
5350 if (module.symtab == 0 || r == nullptr) {
5351 outs() << " symtab " << format("0x%08" PRIx32, module.symtab)
5352 << " (not in an __OBJC section)\n";
5355 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
5356 memset(&symtab, '\0', sizeof(struct objc_symtab_t));
5359 if (left < sizeof(struct objc_symtab_t)) {
5360 memcpy(&symtab, r, left);
5361 outs() << "\tsymtab extends past end of an __OBJC section)\n";
5363 memcpy(&symtab, r, sizeof(struct objc_symtab_t));
5364 if (left > sizeof(struct objc_symtab_t)) {
5365 defs_left = left - sizeof(struct objc_symtab_t);
5366 defs = r + sizeof(struct objc_symtab_t);
5369 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5372 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
5373 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
5374 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
5376 outs() << " (not in an __OBJC section)";
5378 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
5379 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
5380 if (symtab.cls_def_cnt > 0)
5381 outs() << "\tClass Definitions\n";
5382 for (j = 0; j < symtab.cls_def_cnt; j++) {
5383 if ((j + 1) * sizeof(uint32_t) > defs_left) {
5384 outs() << "\t(remaining class defs entries entends past the end of the "
5388 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
5389 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5390 sys::swapByteOrder(def);
5392 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5393 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
5395 if (left > sizeof(struct objc_class_t)) {
5397 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5399 outs() << " (entends past the end of the section)\n";
5400 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5401 memcpy(&objc_class, r, left);
5403 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5404 swapStruct(objc_class);
5405 print_objc_class_t(&objc_class, &info);
5407 outs() << "(not in an __OBJC section)\n";
5410 if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
5411 outs() << "\tMeta Class";
5412 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
5414 if (left > sizeof(struct objc_class_t)) {
5416 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5418 outs() << " (entends past the end of the section)\n";
5419 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5420 memcpy(&objc_class, r, left);
5422 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5423 swapStruct(objc_class);
5424 print_objc_class_t(&objc_class, &info);
5426 outs() << "(not in an __OBJC section)\n";
5430 if (symtab.cat_def_cnt > 0)
5431 outs() << "\tCategory Definitions\n";
5432 for (j = 0; j < symtab.cat_def_cnt; j++) {
5433 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
5434 outs() << "\t(remaining category defs entries entends past the end of "
5435 << "the section)\n";
5438 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
5440 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5441 sys::swapByteOrder(def);
5443 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5444 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
5445 << format("0x%08" PRIx32, def);
5447 if (left > sizeof(struct objc_category_t)) {
5449 memcpy(&objc_category, r, sizeof(struct objc_category_t));
5451 outs() << " (entends past the end of the section)\n";
5452 memset(&objc_category, '\0', sizeof(struct objc_category_t));
5453 memcpy(&objc_category, r, left);
5455 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5456 swapStruct(objc_category);
5457 print_objc_objc_category_t(&objc_category, &info);
5459 outs() << "(not in an __OBJC section)\n";
5463 const SectionRef II = get_section(O, "__OBJC", "__image_info");
5464 if (II != SectionRef())
5465 print_image_info(II, &info);
5470 static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
5471 uint32_t size, uint32_t addr) {
5472 SymbolAddressMap AddrMap;
5473 CreateSymbolAddressMap(O, &AddrMap);
5475 std::vector<SectionRef> Sections;
5476 for (const SectionRef &Section : O->sections()) {
5478 Section.getName(SectName);
5479 Sections.push_back(Section);
5482 struct DisassembleInfo info;
5483 // Set up the block of info used by the Symbolizer call backs.
5484 info.verbose = true;
5486 info.AddrMap = &AddrMap;
5487 info.Sections = &Sections;
5488 info.class_name = nullptr;
5489 info.selector_name = nullptr;
5490 info.method = nullptr;
5491 info.demangled_name = nullptr;
5492 info.bindtable = nullptr;
5497 struct objc_protocol_t protocol;
5498 uint32_t left, paddr;
5499 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
5500 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
5501 left = size - (p - sect);
5502 if (left < sizeof(struct objc_protocol_t)) {
5503 outs() << "Protocol extends past end of __protocol section\n";
5504 memcpy(&protocol, p, left);
5506 memcpy(&protocol, p, sizeof(struct objc_protocol_t));
5507 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5508 swapStruct(protocol);
5509 paddr = addr + (p - sect);
5510 outs() << "Protocol " << format("0x%" PRIx32, paddr);
5511 if (print_protocol(paddr, 0, &info))
5512 outs() << "(not in an __OBJC section)\n";
5516 static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
5518 printObjc2_64bit_MetaData(O, verbose);
5520 MachO::mach_header H;
5522 if (H.cputype == MachO::CPU_TYPE_ARM)
5523 printObjc2_32bit_MetaData(O, verbose);
5525 // This is the 32-bit non-arm cputype case. Which is normally
5526 // the first Objective-C ABI. But it may be the case of a
5527 // binary for the iOS simulator which is the second Objective-C
5528 // ABI. In that case printObjc1_32bit_MetaData() will determine that
5529 // and return false.
5530 if (printObjc1_32bit_MetaData(O, verbose) == false)
5531 printObjc2_32bit_MetaData(O, verbose);
5536 // GuessLiteralPointer returns a string which for the item in the Mach-O file
5537 // for the address passed in as ReferenceValue for printing as a comment with
5538 // the instruction and also returns the corresponding type of that item
5539 // indirectly through ReferenceType.
5541 // If ReferenceValue is an address of literal cstring then a pointer to the
5542 // cstring is returned and ReferenceType is set to
5543 // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
5545 // If ReferenceValue is an address of an Objective-C CFString, Selector ref or
5546 // Class ref that name is returned and the ReferenceType is set accordingly.
5548 // Lastly, literals which are Symbol address in a literal pool are looked for
5549 // and if found the symbol name is returned and ReferenceType is set to
5550 // LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
5552 // If there is no item in the Mach-O file for the address passed in as
5553 // ReferenceValue nullptr is returned and ReferenceType is unchanged.
5554 static const char *GuessLiteralPointer(uint64_t ReferenceValue,
5555 uint64_t ReferencePC,
5556 uint64_t *ReferenceType,
5557 struct DisassembleInfo *info) {
5558 // First see if there is an external relocation entry at the ReferencePC.
5559 uint64_t sect_addr = info->S.getAddress();
5560 uint64_t sect_offset = ReferencePC - sect_addr;
5561 bool reloc_found = false;
5563 MachO::any_relocation_info RE;
5564 bool isExtern = false;
5566 for (const RelocationRef &Reloc : info->S.relocations()) {
5567 uint64_t RelocOffset = Reloc.getOffset();
5568 if (RelocOffset == sect_offset) {
5569 Rel = Reloc.getRawDataRefImpl();
5570 RE = info->O->getRelocation(Rel);
5571 if (info->O->isRelocationScattered(RE))
5573 isExtern = info->O->getPlainRelocationExternal(RE);
5575 symbol_iterator RelocSym = Reloc.getSymbol();
5582 // If there is an external relocation entry for a symbol in a section
5583 // then used that symbol's value for the value of the reference.
5584 if (reloc_found && isExtern) {
5585 if (info->O->getAnyRelocationPCRel(RE)) {
5586 unsigned Type = info->O->getAnyRelocationType(RE);
5587 if (Type == MachO::X86_64_RELOC_SIGNED) {
5588 ReferenceValue = Symbol.getValue();
5593 // Look for literals such as Objective-C CFStrings refs, Selector refs,
5594 // Message refs and Class refs.
5595 bool classref, selref, msgref, cfstring;
5596 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
5597 selref, msgref, cfstring);
5598 if (classref && pointer_value == 0) {
5599 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
5600 // And the pointer_value in that section is typically zero as it will be
5601 // set by dyld as part of the "bind information".
5602 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
5603 if (name != nullptr) {
5604 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
5605 const char *class_name = strrchr(name, '$');
5606 if (class_name != nullptr && class_name[1] == '_' &&
5607 class_name[2] != '\0') {
5608 info->class_name = class_name + 2;
5615 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
5617 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
5618 if (name != nullptr)
5619 info->class_name = name;
5621 name = "bad class ref";
5626 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
5627 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
5631 if (selref && pointer_value == 0)
5632 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
5634 if (pointer_value != 0)
5635 ReferenceValue = pointer_value;
5637 const char *name = GuessCstringPointer(ReferenceValue, info);
5639 if (pointer_value != 0 && selref) {
5640 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
5641 info->selector_name = name;
5642 } else if (pointer_value != 0 && msgref) {
5643 info->class_name = nullptr;
5644 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
5645 info->selector_name = name;
5647 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
5651 // Lastly look for an indirect symbol with this ReferenceValue which is in
5652 // a literal pool. If found return that symbol name.
5653 name = GuessIndirectSymbol(ReferenceValue, info);
5655 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
5662 // SymbolizerSymbolLookUp is the symbol lookup function passed when creating
5663 // the Symbolizer. It looks up the ReferenceValue using the info passed via the
5664 // pointer to the struct DisassembleInfo that was passed when MCSymbolizer
5665 // is created and returns the symbol name that matches the ReferenceValue or
5666 // nullptr if none. The ReferenceType is passed in for the IN type of
5667 // reference the instruction is making from the values in defined in the header
5668 // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
5669 // Out type and the ReferenceName will also be set which is added as a comment
5670 // to the disassembled instruction.
5673 // If the symbol name is a C++ mangled name then the demangled name is
5674 // returned through ReferenceName and ReferenceType is set to
5675 // LLVMDisassembler_ReferenceType_DeMangled_Name .
5678 // When this is called to get a symbol name for a branch target then the
5679 // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
5680 // SymbolValue will be looked for in the indirect symbol table to determine if
5681 // it is an address for a symbol stub. If so then the symbol name for that
5682 // stub is returned indirectly through ReferenceName and then ReferenceType is
5683 // set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
5685 // When this is called with an value loaded via a PC relative load then
5686 // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
5687 // SymbolValue is checked to be an address of literal pointer, symbol pointer,
5688 // or an Objective-C meta data reference. If so the output ReferenceType is
5689 // set to correspond to that as well as setting the ReferenceName.
5690 static const char *SymbolizerSymbolLookUp(void *DisInfo,
5691 uint64_t ReferenceValue,
5692 uint64_t *ReferenceType,
5693 uint64_t ReferencePC,
5694 const char **ReferenceName) {
5695 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
5696 // If no verbose symbolic information is wanted then just return nullptr.
5697 if (!info->verbose) {
5698 *ReferenceName = nullptr;
5699 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5703 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
5705 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
5706 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
5707 if (*ReferenceName != nullptr) {
5708 method_reference(info, ReferenceType, ReferenceName);
5709 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
5710 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
5713 if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
5714 if (info->demangled_name != nullptr)
5715 free(info->demangled_name);
5717 info->demangled_name =
5718 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
5719 if (info->demangled_name != nullptr) {
5720 *ReferenceName = info->demangled_name;
5721 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5723 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5726 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5727 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
5729 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5731 method_reference(info, ReferenceType, ReferenceName);
5733 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5734 // If this is arm64 and the reference is an adrp instruction save the
5735 // instruction, passed in ReferenceValue and the address of the instruction
5736 // for use later if we see and add immediate instruction.
5737 } else if (info->O->getArch() == Triple::aarch64 &&
5738 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
5739 info->adrp_inst = ReferenceValue;
5740 info->adrp_addr = ReferencePC;
5741 SymbolName = nullptr;
5742 *ReferenceName = nullptr;
5743 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5744 // If this is arm64 and reference is an add immediate instruction and we
5746 // seen an adrp instruction just before it and the adrp's Xd register
5748 // this add's Xn register reconstruct the value being referenced and look to
5749 // see if it is a literal pointer. Note the add immediate instruction is
5750 // passed in ReferenceValue.
5751 } else if (info->O->getArch() == Triple::aarch64 &&
5752 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
5753 ReferencePC - 4 == info->adrp_addr &&
5754 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5755 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5756 uint32_t addxri_inst;
5757 uint64_t adrp_imm, addxri_imm;
5760 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5761 if (info->adrp_inst & 0x0200000)
5762 adrp_imm |= 0xfffffffffc000000LL;
5764 addxri_inst = ReferenceValue;
5765 addxri_imm = (addxri_inst >> 10) & 0xfff;
5766 if (((addxri_inst >> 22) & 0x3) == 1)
5769 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5770 (adrp_imm << 12) + addxri_imm;
5773 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5774 if (*ReferenceName == nullptr)
5775 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5776 // If this is arm64 and the reference is a load register instruction and we
5777 // have seen an adrp instruction just before it and the adrp's Xd register
5778 // matches this add's Xn register reconstruct the value being referenced and
5779 // look to see if it is a literal pointer. Note the load register
5780 // instruction is passed in ReferenceValue.
5781 } else if (info->O->getArch() == Triple::aarch64 &&
5782 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
5783 ReferencePC - 4 == info->adrp_addr &&
5784 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5785 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5786 uint32_t ldrxui_inst;
5787 uint64_t adrp_imm, ldrxui_imm;
5790 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5791 if (info->adrp_inst & 0x0200000)
5792 adrp_imm |= 0xfffffffffc000000LL;
5794 ldrxui_inst = ReferenceValue;
5795 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
5797 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5798 (adrp_imm << 12) + (ldrxui_imm << 3);
5801 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5802 if (*ReferenceName == nullptr)
5803 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5805 // If this arm64 and is an load register (PC-relative) instruction the
5806 // ReferenceValue is the PC plus the immediate value.
5807 else if (info->O->getArch() == Triple::aarch64 &&
5808 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
5809 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
5811 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5812 if (*ReferenceName == nullptr)
5813 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5816 else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
5817 if (info->demangled_name != nullptr)
5818 free(info->demangled_name);
5820 info->demangled_name =
5821 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
5822 if (info->demangled_name != nullptr) {
5823 *ReferenceName = info->demangled_name;
5824 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5829 *ReferenceName = nullptr;
5830 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5836 /// \brief Emits the comments that are stored in the CommentStream.
5837 /// Each comment in the CommentStream must end with a newline.
5838 static void emitComments(raw_svector_ostream &CommentStream,
5839 SmallString<128> &CommentsToEmit,
5840 formatted_raw_ostream &FormattedOS,
5841 const MCAsmInfo &MAI) {
5842 // Flush the stream before taking its content.
5843 StringRef Comments = CommentsToEmit.str();
5844 // Get the default information for printing a comment.
5845 const char *CommentBegin = MAI.getCommentString();
5846 unsigned CommentColumn = MAI.getCommentColumn();
5847 bool IsFirst = true;
5848 while (!Comments.empty()) {
5850 FormattedOS << '\n';
5851 // Emit a line of comments.
5852 FormattedOS.PadToColumn(CommentColumn);
5853 size_t Position = Comments.find('\n');
5854 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
5855 // Move after the newline character.
5856 Comments = Comments.substr(Position + 1);
5859 FormattedOS.flush();
5861 // Tell the comment stream that the vector changed underneath it.
5862 CommentsToEmit.clear();
5865 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
5866 StringRef DisSegName, StringRef DisSectName) {
5867 const char *McpuDefault = nullptr;
5868 const Target *ThumbTarget = nullptr;
5869 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
5871 // GetTarget prints out stuff.
5874 if (MCPU.empty() && McpuDefault)
5877 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
5878 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
5880 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
5882 // Package up features to be passed to target/subtarget
5883 std::string FeaturesStr;
5884 if (MAttrs.size()) {
5885 SubtargetFeatures Features;
5886 for (unsigned i = 0; i != MAttrs.size(); ++i)
5887 Features.AddFeature(MAttrs[i]);
5888 FeaturesStr = Features.getString();
5891 // Set up disassembler.
5892 std::unique_ptr<const MCRegisterInfo> MRI(
5893 TheTarget->createMCRegInfo(TripleName));
5894 std::unique_ptr<const MCAsmInfo> AsmInfo(
5895 TheTarget->createMCAsmInfo(*MRI, TripleName));
5896 std::unique_ptr<const MCSubtargetInfo> STI(
5897 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
5898 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
5899 std::unique_ptr<MCDisassembler> DisAsm(
5900 TheTarget->createMCDisassembler(*STI, Ctx));
5901 std::unique_ptr<MCSymbolizer> Symbolizer;
5902 struct DisassembleInfo SymbolizerInfo;
5903 std::unique_ptr<MCRelocationInfo> RelInfo(
5904 TheTarget->createMCRelocationInfo(TripleName, Ctx));
5906 Symbolizer.reset(TheTarget->createMCSymbolizer(
5907 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
5908 &SymbolizerInfo, &Ctx, std::move(RelInfo)));
5909 DisAsm->setSymbolizer(std::move(Symbolizer));
5911 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
5912 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
5913 TargetTuple(Triple(TripleName)), AsmPrinterVariant, *AsmInfo, *InstrInfo,
5915 // Set the display preference for hex vs. decimal immediates.
5916 IP->setPrintImmHex(PrintImmHex);
5917 // Comment stream and backing vector.
5918 SmallString<128> CommentsToEmit;
5919 raw_svector_ostream CommentStream(CommentsToEmit);
5920 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
5921 // if it is done then arm64 comments for string literals don't get printed
5922 // and some constant get printed instead and not setting it causes intel
5923 // (32-bit and 64-bit) comments printed with different spacing before the
5924 // comment causing different diffs with the 'C' disassembler library API.
5925 // IP->setCommentStream(CommentStream);
5927 if (!AsmInfo || !STI || !DisAsm || !IP) {
5928 errs() << "error: couldn't initialize disassembler for target "
5929 << TripleName << '\n';
5933 // Set up thumb disassembler.
5934 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
5935 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
5936 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
5937 std::unique_ptr<MCDisassembler> ThumbDisAsm;
5938 std::unique_ptr<MCInstPrinter> ThumbIP;
5939 std::unique_ptr<MCContext> ThumbCtx;
5940 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
5941 struct DisassembleInfo ThumbSymbolizerInfo;
5942 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
5944 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
5946 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
5948 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr));
5949 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
5950 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
5951 MCContext *PtrThumbCtx = ThumbCtx.get();
5953 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
5955 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
5956 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
5957 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
5958 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
5960 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
5961 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
5962 TargetTuple(Triple(ThumbTripleName)), ThumbAsmPrinterVariant,
5963 *ThumbAsmInfo, *ThumbInstrInfo, *ThumbMRI));
5964 // Set the display preference for hex vs. decimal immediates.
5965 ThumbIP->setPrintImmHex(PrintImmHex);
5968 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
5969 errs() << "error: couldn't initialize disassembler for target "
5970 << ThumbTripleName << '\n';
5974 MachO::mach_header Header = MachOOF->getHeader();
5976 // FIXME: Using the -cfg command line option, this code used to be able to
5977 // annotate relocations with the referenced symbol's name, and if this was
5978 // inside a __[cf]string section, the data it points to. This is now replaced
5979 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
5980 std::vector<SectionRef> Sections;
5981 std::vector<SymbolRef> Symbols;
5982 SmallVector<uint64_t, 8> FoundFns;
5983 uint64_t BaseSegmentAddress;
5985 getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
5986 BaseSegmentAddress);
5988 // Sort the symbols by address, just in case they didn't come in that way.
5989 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
5991 // Build a data in code table that is sorted on by the address of each entry.
5992 uint64_t BaseAddress = 0;
5993 if (Header.filetype == MachO::MH_OBJECT)
5994 BaseAddress = Sections[0].getAddress();
5996 BaseAddress = BaseSegmentAddress;
5998 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
6001 DI->getOffset(Offset);
6002 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
6004 array_pod_sort(Dices.begin(), Dices.end());
6007 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
6009 raw_ostream &DebugOut = nulls();
6012 std::unique_ptr<DIContext> diContext;
6013 ObjectFile *DbgObj = MachOOF;
6014 // Try to find debug info and set up the DIContext for it.
6016 // A separate DSym file path was specified, parse it as a macho file,
6017 // get the sections and supply it to the section name parsing machinery.
6018 if (!DSYMFile.empty()) {
6019 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
6020 MemoryBuffer::getFileOrSTDIN(DSYMFile);
6021 if (std::error_code EC = BufOrErr.getError()) {
6022 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
6026 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
6031 // Setup the DIContext
6032 diContext.reset(new DWARFContextInMemory(*DbgObj));
6035 if (FilterSections.size() == 0)
6036 outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
6038 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
6040 if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
6043 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
6045 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
6046 if (SegmentName != DisSegName)
6050 Sections[SectIdx].getContents(BytesStr);
6051 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
6053 uint64_t SectAddress = Sections[SectIdx].getAddress();
6055 bool symbolTableWorked = false;
6057 // Parse relocations.
6058 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
6059 for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) {
6060 uint64_t RelocOffset = Reloc.getOffset();
6061 uint64_t SectionAddress = Sections[SectIdx].getAddress();
6062 RelocOffset -= SectionAddress;
6064 symbol_iterator RelocSym = Reloc.getSymbol();
6066 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
6068 array_pod_sort(Relocs.begin(), Relocs.end());
6070 // Create a map of symbol addresses to symbol names for use by
6071 // the SymbolizerSymbolLookUp() routine.
6072 SymbolAddressMap AddrMap;
6073 bool DisSymNameFound = false;
6074 for (const SymbolRef &Symbol : MachOOF->symbols()) {
6075 SymbolRef::Type ST = Symbol.getType();
6076 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
6077 ST == SymbolRef::ST_Other) {
6078 uint64_t Address = Symbol.getValue();
6079 ErrorOr<StringRef> SymNameOrErr = Symbol.getName();
6080 if (std::error_code EC = SymNameOrErr.getError())
6081 report_fatal_error(EC.message());
6082 StringRef SymName = *SymNameOrErr;
6083 AddrMap[Address] = SymName;
6084 if (!DisSymName.empty() && DisSymName == SymName)
6085 DisSymNameFound = true;
6088 if (!DisSymName.empty() && !DisSymNameFound) {
6089 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
6092 // Set up the block of info used by the Symbolizer call backs.
6093 SymbolizerInfo.verbose = !NoSymbolicOperands;
6094 SymbolizerInfo.O = MachOOF;
6095 SymbolizerInfo.S = Sections[SectIdx];
6096 SymbolizerInfo.AddrMap = &AddrMap;
6097 SymbolizerInfo.Sections = &Sections;
6098 SymbolizerInfo.class_name = nullptr;
6099 SymbolizerInfo.selector_name = nullptr;
6100 SymbolizerInfo.method = nullptr;
6101 SymbolizerInfo.demangled_name = nullptr;
6102 SymbolizerInfo.bindtable = nullptr;
6103 SymbolizerInfo.adrp_addr = 0;
6104 SymbolizerInfo.adrp_inst = 0;
6105 // Same for the ThumbSymbolizer
6106 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
6107 ThumbSymbolizerInfo.O = MachOOF;
6108 ThumbSymbolizerInfo.S = Sections[SectIdx];
6109 ThumbSymbolizerInfo.AddrMap = &AddrMap;
6110 ThumbSymbolizerInfo.Sections = &Sections;
6111 ThumbSymbolizerInfo.class_name = nullptr;
6112 ThumbSymbolizerInfo.selector_name = nullptr;
6113 ThumbSymbolizerInfo.method = nullptr;
6114 ThumbSymbolizerInfo.demangled_name = nullptr;
6115 ThumbSymbolizerInfo.bindtable = nullptr;
6116 ThumbSymbolizerInfo.adrp_addr = 0;
6117 ThumbSymbolizerInfo.adrp_inst = 0;
6119 // Disassemble symbol by symbol.
6120 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
6121 ErrorOr<StringRef> SymNameOrErr = Symbols[SymIdx].getName();
6122 if (std::error_code EC = SymNameOrErr.getError())
6123 report_fatal_error(EC.message());
6124 StringRef SymName = *SymNameOrErr;
6126 SymbolRef::Type ST = Symbols[SymIdx].getType();
6127 if (ST != SymbolRef::ST_Function)
6130 // Make sure the symbol is defined in this section.
6131 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
6135 // If we are only disassembling one symbol see if this is that symbol.
6136 if (!DisSymName.empty() && DisSymName != SymName)
6139 // Start at the address of the symbol relative to the section's address.
6140 uint64_t Start = Symbols[SymIdx].getValue();
6141 uint64_t SectionAddress = Sections[SectIdx].getAddress();
6142 Start -= SectionAddress;
6144 // Stop disassembling either at the beginning of the next symbol or at
6145 // the end of the section.
6146 bool containsNextSym = false;
6147 uint64_t NextSym = 0;
6148 uint64_t NextSymIdx = SymIdx + 1;
6149 while (Symbols.size() > NextSymIdx) {
6150 SymbolRef::Type NextSymType = Symbols[NextSymIdx].getType();
6151 if (NextSymType == SymbolRef::ST_Function) {
6153 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
6154 NextSym = Symbols[NextSymIdx].getValue();
6155 NextSym -= SectionAddress;
6161 uint64_t SectSize = Sections[SectIdx].getSize();
6162 uint64_t End = containsNextSym ? NextSym : SectSize;
6165 symbolTableWorked = true;
6167 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
6169 (MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget;
6171 outs() << SymName << ":\n";
6172 DILineInfo lastLine;
6173 for (uint64_t Index = Start; Index < End; Index += Size) {
6176 uint64_t PC = SectAddress + Index;
6177 if (!NoLeadingAddr) {
6178 if (FullLeadingAddr) {
6179 if (MachOOF->is64Bit())
6180 outs() << format("%016" PRIx64, PC);
6182 outs() << format("%08" PRIx64, PC);
6184 outs() << format("%8" PRIx64 ":", PC);
6190 // Check the data in code table here to see if this is data not an
6191 // instruction to be disassembled.
6193 Dice.push_back(std::make_pair(PC, DiceRef()));
6194 dice_table_iterator DTI =
6195 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
6196 compareDiceTableEntries);
6197 if (DTI != Dices.end()) {
6199 DTI->second.getLength(Length);
6201 DTI->second.getKind(Kind);
6202 Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
6203 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
6204 (PC == (DTI->first + Length - 1)) && (Length & 1))
6209 SmallVector<char, 64> AnnotationsBytes;
6210 raw_svector_ostream Annotations(AnnotationsBytes);
6214 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
6215 PC, DebugOut, Annotations);
6217 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
6218 DebugOut, Annotations);
6220 if (!NoShowRawInsn) {
6221 dumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, Size), outs());
6223 formatted_raw_ostream FormattedOS(outs());
6224 StringRef AnnotationsStr = Annotations.str();
6226 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
6228 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
6229 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
6231 // Print debug info.
6233 DILineInfo dli = diContext->getLineInfoForAddress(PC);
6234 // Print valid line info if it changed.
6235 if (dli != lastLine && dli.Line != 0)
6236 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
6242 unsigned int Arch = MachOOF->getArch();
6243 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
6244 outs() << format("\t.byte 0x%02x #bad opcode\n",
6245 *(Bytes.data() + Index) & 0xff);
6246 Size = 1; // skip exactly one illegible byte and move on.
6247 } else if (Arch == Triple::aarch64) {
6248 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6249 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
6250 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
6251 (*(Bytes.data() + Index + 3) & 0xff) << 24;
6252 outs() << format("\t.long\t0x%08x\n", opcode);
6255 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6257 Size = 1; // skip illegible bytes
6262 if (!symbolTableWorked) {
6263 // Reading the symbol table didn't work, disassemble the whole section.
6264 uint64_t SectAddress = Sections[SectIdx].getAddress();
6265 uint64_t SectSize = Sections[SectIdx].getSize();
6267 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
6270 uint64_t PC = SectAddress + Index;
6271 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
6272 DebugOut, nulls())) {
6273 if (!NoLeadingAddr) {
6274 if (FullLeadingAddr) {
6275 if (MachOOF->is64Bit())
6276 outs() << format("%016" PRIx64, PC);
6278 outs() << format("%08" PRIx64, PC);
6280 outs() << format("%8" PRIx64 ":", PC);
6283 if (!NoShowRawInsn) {
6285 dumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, InstSize), outs());
6287 IP->printInst(&Inst, outs(), "", *STI);
6290 unsigned int Arch = MachOOF->getArch();
6291 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
6292 outs() << format("\t.byte 0x%02x #bad opcode\n",
6293 *(Bytes.data() + Index) & 0xff);
6294 InstSize = 1; // skip exactly one illegible byte and move on.
6296 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6298 InstSize = 1; // skip illegible bytes
6303 // The TripleName's need to be reset if we are called again for a different
6306 ThumbTripleName = "";
6308 if (SymbolizerInfo.method != nullptr)
6309 free(SymbolizerInfo.method);
6310 if (SymbolizerInfo.demangled_name != nullptr)
6311 free(SymbolizerInfo.demangled_name);
6312 if (SymbolizerInfo.bindtable != nullptr)
6313 delete SymbolizerInfo.bindtable;
6314 if (ThumbSymbolizerInfo.method != nullptr)
6315 free(ThumbSymbolizerInfo.method);
6316 if (ThumbSymbolizerInfo.demangled_name != nullptr)
6317 free(ThumbSymbolizerInfo.demangled_name);
6318 if (ThumbSymbolizerInfo.bindtable != nullptr)
6319 delete ThumbSymbolizerInfo.bindtable;
6323 //===----------------------------------------------------------------------===//
6324 // __compact_unwind section dumping
6325 //===----------------------------------------------------------------------===//
6329 template <typename T> static uint64_t readNext(const char *&Buf) {
6330 using llvm::support::little;
6331 using llvm::support::unaligned;
6333 uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
6338 struct CompactUnwindEntry {
6339 uint32_t OffsetInSection;
6341 uint64_t FunctionAddr;
6343 uint32_t CompactEncoding;
6344 uint64_t PersonalityAddr;
6347 RelocationRef FunctionReloc;
6348 RelocationRef PersonalityReloc;
6349 RelocationRef LSDAReloc;
6351 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
6352 : OffsetInSection(Offset) {
6354 read<uint64_t>(Contents.data() + Offset);
6356 read<uint32_t>(Contents.data() + Offset);
6360 template <typename UIntPtr> void read(const char *Buf) {
6361 FunctionAddr = readNext<UIntPtr>(Buf);
6362 Length = readNext<uint32_t>(Buf);
6363 CompactEncoding = readNext<uint32_t>(Buf);
6364 PersonalityAddr = readNext<UIntPtr>(Buf);
6365 LSDAAddr = readNext<UIntPtr>(Buf);
6370 /// Given a relocation from __compact_unwind, consisting of the RelocationRef
6371 /// and data being relocated, determine the best base Name and Addend to use for
6372 /// display purposes.
6374 /// 1. An Extern relocation will directly reference a symbol (and the data is
6375 /// then already an addend), so use that.
6376 /// 2. Otherwise the data is an offset in the object file's layout; try to find
6377 // a symbol before it in the same section, and use the offset from there.
6378 /// 3. Finally, if all that fails, fall back to an offset from the start of the
6379 /// referenced section.
6380 static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
6381 std::map<uint64_t, SymbolRef> &Symbols,
6382 const RelocationRef &Reloc, uint64_t Addr,
6383 StringRef &Name, uint64_t &Addend) {
6384 if (Reloc.getSymbol() != Obj->symbol_end()) {
6385 ErrorOr<StringRef> NameOrErr = Reloc.getSymbol()->getName();
6386 if (std::error_code EC = NameOrErr.getError())
6387 report_fatal_error(EC.message());
6393 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
6394 SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
6396 uint64_t SectionAddr = RelocSection.getAddress();
6398 auto Sym = Symbols.upper_bound(Addr);
6399 if (Sym == Symbols.begin()) {
6400 // The first symbol in the object is after this reference, the best we can
6401 // do is section-relative notation.
6402 RelocSection.getName(Name);
6403 Addend = Addr - SectionAddr;
6407 // Go back one so that SymbolAddress <= Addr.
6410 section_iterator SymSection = *Sym->second.getSection();
6411 if (RelocSection == *SymSection) {
6412 // There's a valid symbol in the same section before this reference.
6413 ErrorOr<StringRef> NameOrErr = Sym->second.getName();
6414 if (std::error_code EC = NameOrErr.getError())
6415 report_fatal_error(EC.message());
6417 Addend = Addr - Sym->first;
6421 // There is a symbol before this reference, but it's in a different
6422 // section. Probably not helpful to mention it, so use the section name.
6423 RelocSection.getName(Name);
6424 Addend = Addr - SectionAddr;
6427 static void printUnwindRelocDest(const MachOObjectFile *Obj,
6428 std::map<uint64_t, SymbolRef> &Symbols,
6429 const RelocationRef &Reloc, uint64_t Addr) {
6433 if (!Reloc.getObject())
6436 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
6440 outs() << " + " << format("0x%" PRIx64, Addend);
6444 printMachOCompactUnwindSection(const MachOObjectFile *Obj,
6445 std::map<uint64_t, SymbolRef> &Symbols,
6446 const SectionRef &CompactUnwind) {
6448 assert(Obj->isLittleEndian() &&
6449 "There should not be a big-endian .o with __compact_unwind");
6451 bool Is64 = Obj->is64Bit();
6452 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
6453 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
6456 CompactUnwind.getContents(Contents);
6458 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
6460 // First populate the initial raw offsets, encodings and so on from the entry.
6461 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
6462 CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
6463 CompactUnwinds.push_back(Entry);
6466 // Next we need to look at the relocations to find out what objects are
6467 // actually being referred to.
6468 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
6469 uint64_t RelocAddress = Reloc.getOffset();
6471 uint32_t EntryIdx = RelocAddress / EntrySize;
6472 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
6473 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
6475 if (OffsetInEntry == 0)
6476 Entry.FunctionReloc = Reloc;
6477 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
6478 Entry.PersonalityReloc = Reloc;
6479 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
6480 Entry.LSDAReloc = Reloc;
6482 llvm_unreachable("Unexpected relocation in __compact_unwind section");
6485 // Finally, we're ready to print the data we've gathered.
6486 outs() << "Contents of __compact_unwind section:\n";
6487 for (auto &Entry : CompactUnwinds) {
6488 outs() << " Entry at offset "
6489 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
6491 // 1. Start of the region this entry applies to.
6492 outs() << " start: " << format("0x%" PRIx64,
6493 Entry.FunctionAddr) << ' ';
6494 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
6497 // 2. Length of the region this entry applies to.
6498 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
6500 // 3. The 32-bit compact encoding.
6501 outs() << " compact encoding: "
6502 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
6504 // 4. The personality function, if present.
6505 if (Entry.PersonalityReloc.getObject()) {
6506 outs() << " personality function: "
6507 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
6508 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
6509 Entry.PersonalityAddr);
6513 // 5. This entry's language-specific data area.
6514 if (Entry.LSDAReloc.getObject()) {
6515 outs() << " LSDA: " << format("0x%" PRIx64,
6516 Entry.LSDAAddr) << ' ';
6517 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
6523 //===----------------------------------------------------------------------===//
6524 // __unwind_info section dumping
6525 //===----------------------------------------------------------------------===//
6527 static void printRegularSecondLevelUnwindPage(const char *PageStart) {
6528 const char *Pos = PageStart;
6529 uint32_t Kind = readNext<uint32_t>(Pos);
6531 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
6533 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6534 uint16_t NumEntries = readNext<uint16_t>(Pos);
6536 Pos = PageStart + EntriesStart;
6537 for (unsigned i = 0; i < NumEntries; ++i) {
6538 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6539 uint32_t Encoding = readNext<uint32_t>(Pos);
6541 outs() << " [" << i << "]: "
6542 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6544 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
6548 static void printCompressedSecondLevelUnwindPage(
6549 const char *PageStart, uint32_t FunctionBase,
6550 const SmallVectorImpl<uint32_t> &CommonEncodings) {
6551 const char *Pos = PageStart;
6552 uint32_t Kind = readNext<uint32_t>(Pos);
6554 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
6556 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6557 uint16_t NumEntries = readNext<uint16_t>(Pos);
6559 uint16_t EncodingsStart = readNext<uint16_t>(Pos);
6560 readNext<uint16_t>(Pos);
6561 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
6562 PageStart + EncodingsStart);
6564 Pos = PageStart + EntriesStart;
6565 for (unsigned i = 0; i < NumEntries; ++i) {
6566 uint32_t Entry = readNext<uint32_t>(Pos);
6567 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
6568 uint32_t EncodingIdx = Entry >> 24;
6571 if (EncodingIdx < CommonEncodings.size())
6572 Encoding = CommonEncodings[EncodingIdx];
6574 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
6576 outs() << " [" << i << "]: "
6577 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6579 << "encoding[" << EncodingIdx
6580 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
6584 static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
6585 std::map<uint64_t, SymbolRef> &Symbols,
6586 const SectionRef &UnwindInfo) {
6588 assert(Obj->isLittleEndian() &&
6589 "There should not be a big-endian .o with __unwind_info");
6591 outs() << "Contents of __unwind_info section:\n";
6594 UnwindInfo.getContents(Contents);
6595 const char *Pos = Contents.data();
6597 //===----------------------------------
6599 //===----------------------------------
6601 uint32_t Version = readNext<uint32_t>(Pos);
6602 outs() << " Version: "
6603 << format("0x%" PRIx32, Version) << '\n';
6604 assert(Version == 1 && "only understand version 1");
6606 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
6607 outs() << " Common encodings array section offset: "
6608 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
6609 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
6610 outs() << " Number of common encodings in array: "
6611 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
6613 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
6614 outs() << " Personality function array section offset: "
6615 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
6616 uint32_t NumPersonalities = readNext<uint32_t>(Pos);
6617 outs() << " Number of personality functions in array: "
6618 << format("0x%" PRIx32, NumPersonalities) << '\n';
6620 uint32_t IndicesStart = readNext<uint32_t>(Pos);
6621 outs() << " Index array section offset: "
6622 << format("0x%" PRIx32, IndicesStart) << '\n';
6623 uint32_t NumIndices = readNext<uint32_t>(Pos);
6624 outs() << " Number of indices in array: "
6625 << format("0x%" PRIx32, NumIndices) << '\n';
6627 //===----------------------------------
6628 // A shared list of common encodings
6629 //===----------------------------------
6631 // These occupy indices in the range [0, N] whenever an encoding is referenced
6632 // from a compressed 2nd level index table. In practice the linker only
6633 // creates ~128 of these, so that indices are available to embed encodings in
6634 // the 2nd level index.
6636 SmallVector<uint32_t, 64> CommonEncodings;
6637 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
6638 Pos = Contents.data() + CommonEncodingsStart;
6639 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
6640 uint32_t Encoding = readNext<uint32_t>(Pos);
6641 CommonEncodings.push_back(Encoding);
6643 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
6647 //===----------------------------------
6648 // Personality functions used in this executable
6649 //===----------------------------------
6651 // There should be only a handful of these (one per source language,
6652 // roughly). Particularly since they only get 2 bits in the compact encoding.
6654 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
6655 Pos = Contents.data() + PersonalitiesStart;
6656 for (unsigned i = 0; i < NumPersonalities; ++i) {
6657 uint32_t PersonalityFn = readNext<uint32_t>(Pos);
6658 outs() << " personality[" << i + 1
6659 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
6662 //===----------------------------------
6663 // The level 1 index entries
6664 //===----------------------------------
6666 // These specify an approximate place to start searching for the more detailed
6667 // information, sorted by PC.
6670 uint32_t FunctionOffset;
6671 uint32_t SecondLevelPageStart;
6675 SmallVector<IndexEntry, 4> IndexEntries;
6677 outs() << " Top level indices: (count = " << NumIndices << ")\n";
6678 Pos = Contents.data() + IndicesStart;
6679 for (unsigned i = 0; i < NumIndices; ++i) {
6682 Entry.FunctionOffset = readNext<uint32_t>(Pos);
6683 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
6684 Entry.LSDAStart = readNext<uint32_t>(Pos);
6685 IndexEntries.push_back(Entry);
6687 outs() << " [" << i << "]: "
6688 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
6690 << "2nd level page offset="
6691 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
6692 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
6695 //===----------------------------------
6696 // Next come the LSDA tables
6697 //===----------------------------------
6699 // The LSDA layout is rather implicit: it's a contiguous array of entries from
6700 // the first top-level index's LSDAOffset to the last (sentinel).
6702 outs() << " LSDA descriptors:\n";
6703 Pos = Contents.data() + IndexEntries[0].LSDAStart;
6704 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
6705 (2 * sizeof(uint32_t));
6706 for (int i = 0; i < NumLSDAs; ++i) {
6707 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6708 uint32_t LSDAOffset = readNext<uint32_t>(Pos);
6709 outs() << " [" << i << "]: "
6710 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6712 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
6715 //===----------------------------------
6716 // Finally, the 2nd level indices
6717 //===----------------------------------
6719 // Generally these are 4K in size, and have 2 possible forms:
6720 // + Regular stores up to 511 entries with disparate encodings
6721 // + Compressed stores up to 1021 entries if few enough compact encoding
6723 outs() << " Second level indices:\n";
6724 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
6725 // The final sentinel top-level index has no associated 2nd level page
6726 if (IndexEntries[i].SecondLevelPageStart == 0)
6729 outs() << " Second level index[" << i << "]: "
6730 << "offset in section="
6731 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
6733 << "base function offset="
6734 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
6736 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
6737 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
6739 printRegularSecondLevelUnwindPage(Pos);
6741 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
6744 llvm_unreachable("Do not know how to print this kind of 2nd level page");
6748 void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
6749 std::map<uint64_t, SymbolRef> Symbols;
6750 for (const SymbolRef &SymRef : Obj->symbols()) {
6751 // Discard any undefined or absolute symbols. They're not going to take part
6752 // in the convenience lookup for unwind info and just take up resources.
6753 section_iterator Section = *SymRef.getSection();
6754 if (Section == Obj->section_end())
6757 uint64_t Addr = SymRef.getValue();
6758 Symbols.insert(std::make_pair(Addr, SymRef));
6761 for (const SectionRef &Section : Obj->sections()) {
6763 Section.getName(SectName);
6764 if (SectName == "__compact_unwind")
6765 printMachOCompactUnwindSection(Obj, Symbols, Section);
6766 else if (SectName == "__unwind_info")
6767 printMachOUnwindInfoSection(Obj, Symbols, Section);
6768 else if (SectName == "__eh_frame")
6769 outs() << "llvm-objdump: warning: unhandled __eh_frame section\n";
6773 static void PrintMachHeader(uint32_t magic, uint32_t cputype,
6774 uint32_t cpusubtype, uint32_t filetype,
6775 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
6777 outs() << "Mach header\n";
6778 outs() << " magic cputype cpusubtype caps filetype ncmds "
6779 "sizeofcmds flags\n";
6781 if (magic == MachO::MH_MAGIC)
6782 outs() << " MH_MAGIC";
6783 else if (magic == MachO::MH_MAGIC_64)
6784 outs() << "MH_MAGIC_64";
6786 outs() << format(" 0x%08" PRIx32, magic);
6788 case MachO::CPU_TYPE_I386:
6790 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6791 case MachO::CPU_SUBTYPE_I386_ALL:
6795 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6799 case MachO::CPU_TYPE_X86_64:
6800 outs() << " X86_64";
6801 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6802 case MachO::CPU_SUBTYPE_X86_64_ALL:
6805 case MachO::CPU_SUBTYPE_X86_64_H:
6806 outs() << " Haswell";
6809 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6813 case MachO::CPU_TYPE_ARM:
6815 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6816 case MachO::CPU_SUBTYPE_ARM_ALL:
6819 case MachO::CPU_SUBTYPE_ARM_V4T:
6822 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
6825 case MachO::CPU_SUBTYPE_ARM_XSCALE:
6826 outs() << " XSCALE";
6828 case MachO::CPU_SUBTYPE_ARM_V6:
6831 case MachO::CPU_SUBTYPE_ARM_V6M:
6834 case MachO::CPU_SUBTYPE_ARM_V7:
6837 case MachO::CPU_SUBTYPE_ARM_V7EM:
6840 case MachO::CPU_SUBTYPE_ARM_V7K:
6843 case MachO::CPU_SUBTYPE_ARM_V7M:
6846 case MachO::CPU_SUBTYPE_ARM_V7S:
6850 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6854 case MachO::CPU_TYPE_ARM64:
6856 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6857 case MachO::CPU_SUBTYPE_ARM64_ALL:
6861 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6865 case MachO::CPU_TYPE_POWERPC:
6867 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6868 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6872 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6876 case MachO::CPU_TYPE_POWERPC64:
6878 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6879 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6883 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6888 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
6891 outs() << format(" 0x%02" PRIx32,
6892 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
6895 case MachO::MH_OBJECT:
6896 outs() << " OBJECT";
6898 case MachO::MH_EXECUTE:
6899 outs() << " EXECUTE";
6901 case MachO::MH_FVMLIB:
6902 outs() << " FVMLIB";
6904 case MachO::MH_CORE:
6907 case MachO::MH_PRELOAD:
6908 outs() << " PRELOAD";
6910 case MachO::MH_DYLIB:
6913 case MachO::MH_DYLIB_STUB:
6914 outs() << " DYLIB_STUB";
6916 case MachO::MH_DYLINKER:
6917 outs() << " DYLINKER";
6919 case MachO::MH_BUNDLE:
6920 outs() << " BUNDLE";
6922 case MachO::MH_DSYM:
6925 case MachO::MH_KEXT_BUNDLE:
6926 outs() << " KEXTBUNDLE";
6929 outs() << format(" %10u", filetype);
6932 outs() << format(" %5u", ncmds);
6933 outs() << format(" %10u", sizeofcmds);
6935 if (f & MachO::MH_NOUNDEFS) {
6936 outs() << " NOUNDEFS";
6937 f &= ~MachO::MH_NOUNDEFS;
6939 if (f & MachO::MH_INCRLINK) {
6940 outs() << " INCRLINK";
6941 f &= ~MachO::MH_INCRLINK;
6943 if (f & MachO::MH_DYLDLINK) {
6944 outs() << " DYLDLINK";
6945 f &= ~MachO::MH_DYLDLINK;
6947 if (f & MachO::MH_BINDATLOAD) {
6948 outs() << " BINDATLOAD";
6949 f &= ~MachO::MH_BINDATLOAD;
6951 if (f & MachO::MH_PREBOUND) {
6952 outs() << " PREBOUND";
6953 f &= ~MachO::MH_PREBOUND;
6955 if (f & MachO::MH_SPLIT_SEGS) {
6956 outs() << " SPLIT_SEGS";
6957 f &= ~MachO::MH_SPLIT_SEGS;
6959 if (f & MachO::MH_LAZY_INIT) {
6960 outs() << " LAZY_INIT";
6961 f &= ~MachO::MH_LAZY_INIT;
6963 if (f & MachO::MH_TWOLEVEL) {
6964 outs() << " TWOLEVEL";
6965 f &= ~MachO::MH_TWOLEVEL;
6967 if (f & MachO::MH_FORCE_FLAT) {
6968 outs() << " FORCE_FLAT";
6969 f &= ~MachO::MH_FORCE_FLAT;
6971 if (f & MachO::MH_NOMULTIDEFS) {
6972 outs() << " NOMULTIDEFS";
6973 f &= ~MachO::MH_NOMULTIDEFS;
6975 if (f & MachO::MH_NOFIXPREBINDING) {
6976 outs() << " NOFIXPREBINDING";
6977 f &= ~MachO::MH_NOFIXPREBINDING;
6979 if (f & MachO::MH_PREBINDABLE) {
6980 outs() << " PREBINDABLE";
6981 f &= ~MachO::MH_PREBINDABLE;
6983 if (f & MachO::MH_ALLMODSBOUND) {
6984 outs() << " ALLMODSBOUND";
6985 f &= ~MachO::MH_ALLMODSBOUND;
6987 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
6988 outs() << " SUBSECTIONS_VIA_SYMBOLS";
6989 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
6991 if (f & MachO::MH_CANONICAL) {
6992 outs() << " CANONICAL";
6993 f &= ~MachO::MH_CANONICAL;
6995 if (f & MachO::MH_WEAK_DEFINES) {
6996 outs() << " WEAK_DEFINES";
6997 f &= ~MachO::MH_WEAK_DEFINES;
6999 if (f & MachO::MH_BINDS_TO_WEAK) {
7000 outs() << " BINDS_TO_WEAK";
7001 f &= ~MachO::MH_BINDS_TO_WEAK;
7003 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
7004 outs() << " ALLOW_STACK_EXECUTION";
7005 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
7007 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
7008 outs() << " DEAD_STRIPPABLE_DYLIB";
7009 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
7011 if (f & MachO::MH_PIE) {
7013 f &= ~MachO::MH_PIE;
7015 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
7016 outs() << " NO_REEXPORTED_DYLIBS";
7017 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
7019 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
7020 outs() << " MH_HAS_TLV_DESCRIPTORS";
7021 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
7023 if (f & MachO::MH_NO_HEAP_EXECUTION) {
7024 outs() << " MH_NO_HEAP_EXECUTION";
7025 f &= ~MachO::MH_NO_HEAP_EXECUTION;
7027 if (f & MachO::MH_APP_EXTENSION_SAFE) {
7028 outs() << " APP_EXTENSION_SAFE";
7029 f &= ~MachO::MH_APP_EXTENSION_SAFE;
7031 if (f != 0 || flags == 0)
7032 outs() << format(" 0x%08" PRIx32, f);
7034 outs() << format(" 0x%08" PRIx32, magic);
7035 outs() << format(" %7d", cputype);
7036 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7037 outs() << format(" 0x%02" PRIx32,
7038 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7039 outs() << format(" %10u", filetype);
7040 outs() << format(" %5u", ncmds);
7041 outs() << format(" %10u", sizeofcmds);
7042 outs() << format(" 0x%08" PRIx32, flags);
7047 static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
7048 StringRef SegName, uint64_t vmaddr,
7049 uint64_t vmsize, uint64_t fileoff,
7050 uint64_t filesize, uint32_t maxprot,
7051 uint32_t initprot, uint32_t nsects,
7052 uint32_t flags, uint32_t object_size,
7054 uint64_t expected_cmdsize;
7055 if (cmd == MachO::LC_SEGMENT) {
7056 outs() << " cmd LC_SEGMENT\n";
7057 expected_cmdsize = nsects;
7058 expected_cmdsize *= sizeof(struct MachO::section);
7059 expected_cmdsize += sizeof(struct MachO::segment_command);
7061 outs() << " cmd LC_SEGMENT_64\n";
7062 expected_cmdsize = nsects;
7063 expected_cmdsize *= sizeof(struct MachO::section_64);
7064 expected_cmdsize += sizeof(struct MachO::segment_command_64);
7066 outs() << " cmdsize " << cmdsize;
7067 if (cmdsize != expected_cmdsize)
7068 outs() << " Inconsistent size\n";
7071 outs() << " segname " << SegName << "\n";
7072 if (cmd == MachO::LC_SEGMENT_64) {
7073 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
7074 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
7076 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
7077 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
7079 outs() << " fileoff " << fileoff;
7080 if (fileoff > object_size)
7081 outs() << " (past end of file)\n";
7084 outs() << " filesize " << filesize;
7085 if (fileoff + filesize > object_size)
7086 outs() << " (past end of file)\n";
7091 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7092 MachO::VM_PROT_EXECUTE)) != 0)
7093 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
7095 outs() << " maxprot ";
7096 outs() << ((maxprot & MachO::VM_PROT_READ) ? "r" : "-");
7097 outs() << ((maxprot & MachO::VM_PROT_WRITE) ? "w" : "-");
7098 outs() << ((maxprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
7101 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7102 MachO::VM_PROT_EXECUTE)) != 0)
7103 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
7105 outs() << " initprot ";
7106 outs() << ((initprot & MachO::VM_PROT_READ) ? "r" : "-");
7107 outs() << ((initprot & MachO::VM_PROT_WRITE) ? "w" : "-");
7108 outs() << ((initprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
7111 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
7112 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
7114 outs() << " nsects " << nsects << "\n";
7118 outs() << " (none)\n";
7120 if (flags & MachO::SG_HIGHVM) {
7121 outs() << " HIGHVM";
7122 flags &= ~MachO::SG_HIGHVM;
7124 if (flags & MachO::SG_FVMLIB) {
7125 outs() << " FVMLIB";
7126 flags &= ~MachO::SG_FVMLIB;
7128 if (flags & MachO::SG_NORELOC) {
7129 outs() << " NORELOC";
7130 flags &= ~MachO::SG_NORELOC;
7132 if (flags & MachO::SG_PROTECTED_VERSION_1) {
7133 outs() << " PROTECTED_VERSION_1";
7134 flags &= ~MachO::SG_PROTECTED_VERSION_1;
7137 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
7142 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
7146 static void PrintSection(const char *sectname, const char *segname,
7147 uint64_t addr, uint64_t size, uint32_t offset,
7148 uint32_t align, uint32_t reloff, uint32_t nreloc,
7149 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
7150 uint32_t cmd, const char *sg_segname,
7151 uint32_t filetype, uint32_t object_size,
7153 outs() << "Section\n";
7154 outs() << " sectname " << format("%.16s\n", sectname);
7155 outs() << " segname " << format("%.16s", segname);
7156 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
7157 outs() << " (does not match segment)\n";
7160 if (cmd == MachO::LC_SEGMENT_64) {
7161 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
7162 outs() << " size " << format("0x%016" PRIx64, size);
7164 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
7165 outs() << " size " << format("0x%08" PRIx64, size);
7167 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
7168 outs() << " (past end of file)\n";
7171 outs() << " offset " << offset;
7172 if (offset > object_size)
7173 outs() << " (past end of file)\n";
7176 uint32_t align_shifted = 1 << align;
7177 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
7178 outs() << " reloff " << reloff;
7179 if (reloff > object_size)
7180 outs() << " (past end of file)\n";
7183 outs() << " nreloc " << nreloc;
7184 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
7185 outs() << " (past end of file)\n";
7188 uint32_t section_type = flags & MachO::SECTION_TYPE;
7191 if (section_type == MachO::S_REGULAR)
7192 outs() << " S_REGULAR\n";
7193 else if (section_type == MachO::S_ZEROFILL)
7194 outs() << " S_ZEROFILL\n";
7195 else if (section_type == MachO::S_CSTRING_LITERALS)
7196 outs() << " S_CSTRING_LITERALS\n";
7197 else if (section_type == MachO::S_4BYTE_LITERALS)
7198 outs() << " S_4BYTE_LITERALS\n";
7199 else if (section_type == MachO::S_8BYTE_LITERALS)
7200 outs() << " S_8BYTE_LITERALS\n";
7201 else if (section_type == MachO::S_16BYTE_LITERALS)
7202 outs() << " S_16BYTE_LITERALS\n";
7203 else if (section_type == MachO::S_LITERAL_POINTERS)
7204 outs() << " S_LITERAL_POINTERS\n";
7205 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
7206 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
7207 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
7208 outs() << " S_LAZY_SYMBOL_POINTERS\n";
7209 else if (section_type == MachO::S_SYMBOL_STUBS)
7210 outs() << " S_SYMBOL_STUBS\n";
7211 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
7212 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
7213 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
7214 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
7215 else if (section_type == MachO::S_COALESCED)
7216 outs() << " S_COALESCED\n";
7217 else if (section_type == MachO::S_INTERPOSING)
7218 outs() << " S_INTERPOSING\n";
7219 else if (section_type == MachO::S_DTRACE_DOF)
7220 outs() << " S_DTRACE_DOF\n";
7221 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
7222 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
7223 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
7224 outs() << " S_THREAD_LOCAL_REGULAR\n";
7225 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
7226 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
7227 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
7228 outs() << " S_THREAD_LOCAL_VARIABLES\n";
7229 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7230 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
7231 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
7232 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
7234 outs() << format("0x%08" PRIx32, section_type) << "\n";
7235 outs() << "attributes";
7236 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
7237 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
7238 outs() << " PURE_INSTRUCTIONS";
7239 if (section_attributes & MachO::S_ATTR_NO_TOC)
7240 outs() << " NO_TOC";
7241 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
7242 outs() << " STRIP_STATIC_SYMS";
7243 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
7244 outs() << " NO_DEAD_STRIP";
7245 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
7246 outs() << " LIVE_SUPPORT";
7247 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
7248 outs() << " SELF_MODIFYING_CODE";
7249 if (section_attributes & MachO::S_ATTR_DEBUG)
7251 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
7252 outs() << " SOME_INSTRUCTIONS";
7253 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
7254 outs() << " EXT_RELOC";
7255 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
7256 outs() << " LOC_RELOC";
7257 if (section_attributes == 0)
7258 outs() << " (none)";
7261 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
7262 outs() << " reserved1 " << reserved1;
7263 if (section_type == MachO::S_SYMBOL_STUBS ||
7264 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
7265 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
7266 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
7267 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7268 outs() << " (index into indirect symbol table)\n";
7271 outs() << " reserved2 " << reserved2;
7272 if (section_type == MachO::S_SYMBOL_STUBS)
7273 outs() << " (size of stubs)\n";
7278 static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
7279 uint32_t object_size) {
7280 outs() << " cmd LC_SYMTAB\n";
7281 outs() << " cmdsize " << st.cmdsize;
7282 if (st.cmdsize != sizeof(struct MachO::symtab_command))
7283 outs() << " Incorrect size\n";
7286 outs() << " symoff " << st.symoff;
7287 if (st.symoff > object_size)
7288 outs() << " (past end of file)\n";
7291 outs() << " nsyms " << st.nsyms;
7294 big_size = st.nsyms;
7295 big_size *= sizeof(struct MachO::nlist_64);
7296 big_size += st.symoff;
7297 if (big_size > object_size)
7298 outs() << " (past end of file)\n";
7302 big_size = st.nsyms;
7303 big_size *= sizeof(struct MachO::nlist);
7304 big_size += st.symoff;
7305 if (big_size > object_size)
7306 outs() << " (past end of file)\n";
7310 outs() << " stroff " << st.stroff;
7311 if (st.stroff > object_size)
7312 outs() << " (past end of file)\n";
7315 outs() << " strsize " << st.strsize;
7316 big_size = st.stroff;
7317 big_size += st.strsize;
7318 if (big_size > object_size)
7319 outs() << " (past end of file)\n";
7324 static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
7325 uint32_t nsyms, uint32_t object_size,
7327 outs() << " cmd LC_DYSYMTAB\n";
7328 outs() << " cmdsize " << dyst.cmdsize;
7329 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
7330 outs() << " Incorrect size\n";
7333 outs() << " ilocalsym " << dyst.ilocalsym;
7334 if (dyst.ilocalsym > nsyms)
7335 outs() << " (greater than the number of symbols)\n";
7338 outs() << " nlocalsym " << dyst.nlocalsym;
7340 big_size = dyst.ilocalsym;
7341 big_size += dyst.nlocalsym;
7342 if (big_size > nsyms)
7343 outs() << " (past the end of the symbol table)\n";
7346 outs() << " iextdefsym " << dyst.iextdefsym;
7347 if (dyst.iextdefsym > nsyms)
7348 outs() << " (greater than the number of symbols)\n";
7351 outs() << " nextdefsym " << dyst.nextdefsym;
7352 big_size = dyst.iextdefsym;
7353 big_size += dyst.nextdefsym;
7354 if (big_size > nsyms)
7355 outs() << " (past the end of the symbol table)\n";
7358 outs() << " iundefsym " << dyst.iundefsym;
7359 if (dyst.iundefsym > nsyms)
7360 outs() << " (greater than the number of symbols)\n";
7363 outs() << " nundefsym " << dyst.nundefsym;
7364 big_size = dyst.iundefsym;
7365 big_size += dyst.nundefsym;
7366 if (big_size > nsyms)
7367 outs() << " (past the end of the symbol table)\n";
7370 outs() << " tocoff " << dyst.tocoff;
7371 if (dyst.tocoff > object_size)
7372 outs() << " (past end of file)\n";
7375 outs() << " ntoc " << dyst.ntoc;
7376 big_size = dyst.ntoc;
7377 big_size *= sizeof(struct MachO::dylib_table_of_contents);
7378 big_size += dyst.tocoff;
7379 if (big_size > object_size)
7380 outs() << " (past end of file)\n";
7383 outs() << " modtaboff " << dyst.modtaboff;
7384 if (dyst.modtaboff > object_size)
7385 outs() << " (past end of file)\n";
7388 outs() << " nmodtab " << dyst.nmodtab;
7391 modtabend = dyst.nmodtab;
7392 modtabend *= sizeof(struct MachO::dylib_module_64);
7393 modtabend += dyst.modtaboff;
7395 modtabend = dyst.nmodtab;
7396 modtabend *= sizeof(struct MachO::dylib_module);
7397 modtabend += dyst.modtaboff;
7399 if (modtabend > object_size)
7400 outs() << " (past end of file)\n";
7403 outs() << " extrefsymoff " << dyst.extrefsymoff;
7404 if (dyst.extrefsymoff > object_size)
7405 outs() << " (past end of file)\n";
7408 outs() << " nextrefsyms " << dyst.nextrefsyms;
7409 big_size = dyst.nextrefsyms;
7410 big_size *= sizeof(struct MachO::dylib_reference);
7411 big_size += dyst.extrefsymoff;
7412 if (big_size > object_size)
7413 outs() << " (past end of file)\n";
7416 outs() << " indirectsymoff " << dyst.indirectsymoff;
7417 if (dyst.indirectsymoff > object_size)
7418 outs() << " (past end of file)\n";
7421 outs() << " nindirectsyms " << dyst.nindirectsyms;
7422 big_size = dyst.nindirectsyms;
7423 big_size *= sizeof(uint32_t);
7424 big_size += dyst.indirectsymoff;
7425 if (big_size > object_size)
7426 outs() << " (past end of file)\n";
7429 outs() << " extreloff " << dyst.extreloff;
7430 if (dyst.extreloff > object_size)
7431 outs() << " (past end of file)\n";
7434 outs() << " nextrel " << dyst.nextrel;
7435 big_size = dyst.nextrel;
7436 big_size *= sizeof(struct MachO::relocation_info);
7437 big_size += dyst.extreloff;
7438 if (big_size > object_size)
7439 outs() << " (past end of file)\n";
7442 outs() << " locreloff " << dyst.locreloff;
7443 if (dyst.locreloff > object_size)
7444 outs() << " (past end of file)\n";
7447 outs() << " nlocrel " << dyst.nlocrel;
7448 big_size = dyst.nlocrel;
7449 big_size *= sizeof(struct MachO::relocation_info);
7450 big_size += dyst.locreloff;
7451 if (big_size > object_size)
7452 outs() << " (past end of file)\n";
7457 static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
7458 uint32_t object_size) {
7459 if (dc.cmd == MachO::LC_DYLD_INFO)
7460 outs() << " cmd LC_DYLD_INFO\n";
7462 outs() << " cmd LC_DYLD_INFO_ONLY\n";
7463 outs() << " cmdsize " << dc.cmdsize;
7464 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
7465 outs() << " Incorrect size\n";
7468 outs() << " rebase_off " << dc.rebase_off;
7469 if (dc.rebase_off > object_size)
7470 outs() << " (past end of file)\n";
7473 outs() << " rebase_size " << dc.rebase_size;
7475 big_size = dc.rebase_off;
7476 big_size += dc.rebase_size;
7477 if (big_size > object_size)
7478 outs() << " (past end of file)\n";
7481 outs() << " bind_off " << dc.bind_off;
7482 if (dc.bind_off > object_size)
7483 outs() << " (past end of file)\n";
7486 outs() << " bind_size " << dc.bind_size;
7487 big_size = dc.bind_off;
7488 big_size += dc.bind_size;
7489 if (big_size > object_size)
7490 outs() << " (past end of file)\n";
7493 outs() << " weak_bind_off " << dc.weak_bind_off;
7494 if (dc.weak_bind_off > object_size)
7495 outs() << " (past end of file)\n";
7498 outs() << " weak_bind_size " << dc.weak_bind_size;
7499 big_size = dc.weak_bind_off;
7500 big_size += dc.weak_bind_size;
7501 if (big_size > object_size)
7502 outs() << " (past end of file)\n";
7505 outs() << " lazy_bind_off " << dc.lazy_bind_off;
7506 if (dc.lazy_bind_off > object_size)
7507 outs() << " (past end of file)\n";
7510 outs() << " lazy_bind_size " << dc.lazy_bind_size;
7511 big_size = dc.lazy_bind_off;
7512 big_size += dc.lazy_bind_size;
7513 if (big_size > object_size)
7514 outs() << " (past end of file)\n";
7517 outs() << " export_off " << dc.export_off;
7518 if (dc.export_off > object_size)
7519 outs() << " (past end of file)\n";
7522 outs() << " export_size " << dc.export_size;
7523 big_size = dc.export_off;
7524 big_size += dc.export_size;
7525 if (big_size > object_size)
7526 outs() << " (past end of file)\n";
7531 static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
7533 if (dyld.cmd == MachO::LC_ID_DYLINKER)
7534 outs() << " cmd LC_ID_DYLINKER\n";
7535 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
7536 outs() << " cmd LC_LOAD_DYLINKER\n";
7537 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
7538 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
7540 outs() << " cmd ?(" << dyld.cmd << ")\n";
7541 outs() << " cmdsize " << dyld.cmdsize;
7542 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
7543 outs() << " Incorrect size\n";
7546 if (dyld.name >= dyld.cmdsize)
7547 outs() << " name ?(bad offset " << dyld.name << ")\n";
7549 const char *P = (const char *)(Ptr) + dyld.name;
7550 outs() << " name " << P << " (offset " << dyld.name << ")\n";
7554 static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
7555 outs() << " cmd LC_UUID\n";
7556 outs() << " cmdsize " << uuid.cmdsize;
7557 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
7558 outs() << " Incorrect size\n";
7562 outs() << format("%02" PRIX32, uuid.uuid[0]);
7563 outs() << format("%02" PRIX32, uuid.uuid[1]);
7564 outs() << format("%02" PRIX32, uuid.uuid[2]);
7565 outs() << format("%02" PRIX32, uuid.uuid[3]);
7567 outs() << format("%02" PRIX32, uuid.uuid[4]);
7568 outs() << format("%02" PRIX32, uuid.uuid[5]);
7570 outs() << format("%02" PRIX32, uuid.uuid[6]);
7571 outs() << format("%02" PRIX32, uuid.uuid[7]);
7573 outs() << format("%02" PRIX32, uuid.uuid[8]);
7574 outs() << format("%02" PRIX32, uuid.uuid[9]);
7576 outs() << format("%02" PRIX32, uuid.uuid[10]);
7577 outs() << format("%02" PRIX32, uuid.uuid[11]);
7578 outs() << format("%02" PRIX32, uuid.uuid[12]);
7579 outs() << format("%02" PRIX32, uuid.uuid[13]);
7580 outs() << format("%02" PRIX32, uuid.uuid[14]);
7581 outs() << format("%02" PRIX32, uuid.uuid[15]);
7585 static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
7586 outs() << " cmd LC_RPATH\n";
7587 outs() << " cmdsize " << rpath.cmdsize;
7588 if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
7589 outs() << " Incorrect size\n";
7592 if (rpath.path >= rpath.cmdsize)
7593 outs() << " path ?(bad offset " << rpath.path << ")\n";
7595 const char *P = (const char *)(Ptr) + rpath.path;
7596 outs() << " path " << P << " (offset " << rpath.path << ")\n";
7600 static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
7601 if (vd.cmd == MachO::LC_VERSION_MIN_MACOSX)
7602 outs() << " cmd LC_VERSION_MIN_MACOSX\n";
7603 else if (vd.cmd == MachO::LC_VERSION_MIN_IPHONEOS)
7604 outs() << " cmd LC_VERSION_MIN_IPHONEOS\n";
7606 outs() << " cmd " << vd.cmd << " (?)\n";
7607 outs() << " cmdsize " << vd.cmdsize;
7608 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
7609 outs() << " Incorrect size\n";
7612 outs() << " version "
7613 << MachOObjectFile::getVersionMinMajor(vd, false) << "."
7614 << MachOObjectFile::getVersionMinMinor(vd, false);
7615 uint32_t Update = MachOObjectFile::getVersionMinUpdate(vd, false);
7617 outs() << "." << Update;
7620 outs() << " sdk n/a";
7623 << MachOObjectFile::getVersionMinMajor(vd, true) << "."
7624 << MachOObjectFile::getVersionMinMinor(vd, true);
7626 Update = MachOObjectFile::getVersionMinUpdate(vd, true);
7628 outs() << "." << Update;
7632 static void PrintSourceVersionCommand(MachO::source_version_command sd) {
7633 outs() << " cmd LC_SOURCE_VERSION\n";
7634 outs() << " cmdsize " << sd.cmdsize;
7635 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
7636 outs() << " Incorrect size\n";
7639 uint64_t a = (sd.version >> 40) & 0xffffff;
7640 uint64_t b = (sd.version >> 30) & 0x3ff;
7641 uint64_t c = (sd.version >> 20) & 0x3ff;
7642 uint64_t d = (sd.version >> 10) & 0x3ff;
7643 uint64_t e = sd.version & 0x3ff;
7644 outs() << " version " << a << "." << b;
7646 outs() << "." << c << "." << d << "." << e;
7648 outs() << "." << c << "." << d;
7654 static void PrintEntryPointCommand(MachO::entry_point_command ep) {
7655 outs() << " cmd LC_MAIN\n";
7656 outs() << " cmdsize " << ep.cmdsize;
7657 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
7658 outs() << " Incorrect size\n";
7661 outs() << " entryoff " << ep.entryoff << "\n";
7662 outs() << " stacksize " << ep.stacksize << "\n";
7665 static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
7666 uint32_t object_size) {
7667 outs() << " cmd LC_ENCRYPTION_INFO\n";
7668 outs() << " cmdsize " << ec.cmdsize;
7669 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
7670 outs() << " Incorrect size\n";
7673 outs() << " cryptoff " << ec.cryptoff;
7674 if (ec.cryptoff > object_size)
7675 outs() << " (past end of file)\n";
7678 outs() << " cryptsize " << ec.cryptsize;
7679 if (ec.cryptsize > object_size)
7680 outs() << " (past end of file)\n";
7683 outs() << " cryptid " << ec.cryptid << "\n";
7686 static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
7687 uint32_t object_size) {
7688 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
7689 outs() << " cmdsize " << ec.cmdsize;
7690 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
7691 outs() << " Incorrect size\n";
7694 outs() << " cryptoff " << ec.cryptoff;
7695 if (ec.cryptoff > object_size)
7696 outs() << " (past end of file)\n";
7699 outs() << " cryptsize " << ec.cryptsize;
7700 if (ec.cryptsize > object_size)
7701 outs() << " (past end of file)\n";
7704 outs() << " cryptid " << ec.cryptid << "\n";
7705 outs() << " pad " << ec.pad << "\n";
7708 static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
7710 outs() << " cmd LC_LINKER_OPTION\n";
7711 outs() << " cmdsize " << lo.cmdsize;
7712 if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
7713 outs() << " Incorrect size\n";
7716 outs() << " count " << lo.count << "\n";
7717 const char *string = Ptr + sizeof(struct MachO::linker_option_command);
7718 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
7721 while (*string == '\0' && left > 0) {
7727 outs() << " string #" << i << " " << format("%.*s\n", left, string);
7728 uint32_t NullPos = StringRef(string, left).find('\0');
7729 uint32_t len = std::min(NullPos, left) + 1;
7735 outs() << " count " << lo.count << " does not match number of strings "
7739 static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
7741 outs() << " cmd LC_SUB_FRAMEWORK\n";
7742 outs() << " cmdsize " << sub.cmdsize;
7743 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
7744 outs() << " Incorrect size\n";
7747 if (sub.umbrella < sub.cmdsize) {
7748 const char *P = Ptr + sub.umbrella;
7749 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
7751 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
7755 static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
7757 outs() << " cmd LC_SUB_UMBRELLA\n";
7758 outs() << " cmdsize " << sub.cmdsize;
7759 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
7760 outs() << " Incorrect size\n";
7763 if (sub.sub_umbrella < sub.cmdsize) {
7764 const char *P = Ptr + sub.sub_umbrella;
7765 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
7767 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
7771 static void PrintSubLibraryCommand(MachO::sub_library_command sub,
7773 outs() << " cmd LC_SUB_LIBRARY\n";
7774 outs() << " cmdsize " << sub.cmdsize;
7775 if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
7776 outs() << " Incorrect size\n";
7779 if (sub.sub_library < sub.cmdsize) {
7780 const char *P = Ptr + sub.sub_library;
7781 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
7783 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
7787 static void PrintSubClientCommand(MachO::sub_client_command sub,
7789 outs() << " cmd LC_SUB_CLIENT\n";
7790 outs() << " cmdsize " << sub.cmdsize;
7791 if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
7792 outs() << " Incorrect size\n";
7795 if (sub.client < sub.cmdsize) {
7796 const char *P = Ptr + sub.client;
7797 outs() << " client " << P << " (offset " << sub.client << ")\n";
7799 outs() << " client ?(bad offset " << sub.client << ")\n";
7803 static void PrintRoutinesCommand(MachO::routines_command r) {
7804 outs() << " cmd LC_ROUTINES\n";
7805 outs() << " cmdsize " << r.cmdsize;
7806 if (r.cmdsize != sizeof(struct MachO::routines_command))
7807 outs() << " Incorrect size\n";
7810 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
7811 outs() << " init_module " << r.init_module << "\n";
7812 outs() << " reserved1 " << r.reserved1 << "\n";
7813 outs() << " reserved2 " << r.reserved2 << "\n";
7814 outs() << " reserved3 " << r.reserved3 << "\n";
7815 outs() << " reserved4 " << r.reserved4 << "\n";
7816 outs() << " reserved5 " << r.reserved5 << "\n";
7817 outs() << " reserved6 " << r.reserved6 << "\n";
7820 static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
7821 outs() << " cmd LC_ROUTINES_64\n";
7822 outs() << " cmdsize " << r.cmdsize;
7823 if (r.cmdsize != sizeof(struct MachO::routines_command_64))
7824 outs() << " Incorrect size\n";
7827 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
7828 outs() << " init_module " << r.init_module << "\n";
7829 outs() << " reserved1 " << r.reserved1 << "\n";
7830 outs() << " reserved2 " << r.reserved2 << "\n";
7831 outs() << " reserved3 " << r.reserved3 << "\n";
7832 outs() << " reserved4 " << r.reserved4 << "\n";
7833 outs() << " reserved5 " << r.reserved5 << "\n";
7834 outs() << " reserved6 " << r.reserved6 << "\n";
7837 static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
7838 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
7839 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
7840 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
7841 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
7842 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
7843 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
7844 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
7845 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
7846 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
7847 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
7848 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
7849 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
7850 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
7851 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
7852 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
7853 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
7854 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
7855 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
7856 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
7857 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
7858 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
7861 static void Print_mmst_reg(MachO::mmst_reg_t &r) {
7863 outs() << "\t mmst_reg ";
7864 for (f = 0; f < 10; f++)
7865 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
7867 outs() << "\t mmst_rsrv ";
7868 for (f = 0; f < 6; f++)
7869 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
7873 static void Print_xmm_reg(MachO::xmm_reg_t &r) {
7875 outs() << "\t xmm_reg ";
7876 for (f = 0; f < 16; f++)
7877 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
7881 static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
7882 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
7883 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
7884 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
7885 outs() << " denorm " << fpu.fpu_fcw.denorm;
7886 outs() << " zdiv " << fpu.fpu_fcw.zdiv;
7887 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
7888 outs() << " undfl " << fpu.fpu_fcw.undfl;
7889 outs() << " precis " << fpu.fpu_fcw.precis << "\n";
7890 outs() << "\t\t pc ";
7891 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
7892 outs() << "FP_PREC_24B ";
7893 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
7894 outs() << "FP_PREC_53B ";
7895 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
7896 outs() << "FP_PREC_64B ";
7898 outs() << fpu.fpu_fcw.pc << " ";
7900 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
7901 outs() << "FP_RND_NEAR ";
7902 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
7903 outs() << "FP_RND_DOWN ";
7904 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
7905 outs() << "FP_RND_UP ";
7906 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
7907 outs() << "FP_CHOP ";
7909 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
7910 outs() << " denorm " << fpu.fpu_fsw.denorm;
7911 outs() << " zdiv " << fpu.fpu_fsw.zdiv;
7912 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
7913 outs() << " undfl " << fpu.fpu_fsw.undfl;
7914 outs() << " precis " << fpu.fpu_fsw.precis;
7915 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
7916 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
7917 outs() << " c0 " << fpu.fpu_fsw.c0;
7918 outs() << " c1 " << fpu.fpu_fsw.c1;
7919 outs() << " c2 " << fpu.fpu_fsw.c2;
7920 outs() << " tos " << fpu.fpu_fsw.tos;
7921 outs() << " c3 " << fpu.fpu_fsw.c3;
7922 outs() << " busy " << fpu.fpu_fsw.busy << "\n";
7923 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
7924 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
7925 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
7926 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
7927 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
7928 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
7929 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
7930 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
7931 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
7932 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
7933 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
7935 outs() << "\t fpu_stmm0:\n";
7936 Print_mmst_reg(fpu.fpu_stmm0);
7937 outs() << "\t fpu_stmm1:\n";
7938 Print_mmst_reg(fpu.fpu_stmm1);
7939 outs() << "\t fpu_stmm2:\n";
7940 Print_mmst_reg(fpu.fpu_stmm2);
7941 outs() << "\t fpu_stmm3:\n";
7942 Print_mmst_reg(fpu.fpu_stmm3);
7943 outs() << "\t fpu_stmm4:\n";
7944 Print_mmst_reg(fpu.fpu_stmm4);
7945 outs() << "\t fpu_stmm5:\n";
7946 Print_mmst_reg(fpu.fpu_stmm5);
7947 outs() << "\t fpu_stmm6:\n";
7948 Print_mmst_reg(fpu.fpu_stmm6);
7949 outs() << "\t fpu_stmm7:\n";
7950 Print_mmst_reg(fpu.fpu_stmm7);
7951 outs() << "\t fpu_xmm0:\n";
7952 Print_xmm_reg(fpu.fpu_xmm0);
7953 outs() << "\t fpu_xmm1:\n";
7954 Print_xmm_reg(fpu.fpu_xmm1);
7955 outs() << "\t fpu_xmm2:\n";
7956 Print_xmm_reg(fpu.fpu_xmm2);
7957 outs() << "\t fpu_xmm3:\n";
7958 Print_xmm_reg(fpu.fpu_xmm3);
7959 outs() << "\t fpu_xmm4:\n";
7960 Print_xmm_reg(fpu.fpu_xmm4);
7961 outs() << "\t fpu_xmm5:\n";
7962 Print_xmm_reg(fpu.fpu_xmm5);
7963 outs() << "\t fpu_xmm6:\n";
7964 Print_xmm_reg(fpu.fpu_xmm6);
7965 outs() << "\t fpu_xmm7:\n";
7966 Print_xmm_reg(fpu.fpu_xmm7);
7967 outs() << "\t fpu_xmm8:\n";
7968 Print_xmm_reg(fpu.fpu_xmm8);
7969 outs() << "\t fpu_xmm9:\n";
7970 Print_xmm_reg(fpu.fpu_xmm9);
7971 outs() << "\t fpu_xmm10:\n";
7972 Print_xmm_reg(fpu.fpu_xmm10);
7973 outs() << "\t fpu_xmm11:\n";
7974 Print_xmm_reg(fpu.fpu_xmm11);
7975 outs() << "\t fpu_xmm12:\n";
7976 Print_xmm_reg(fpu.fpu_xmm12);
7977 outs() << "\t fpu_xmm13:\n";
7978 Print_xmm_reg(fpu.fpu_xmm13);
7979 outs() << "\t fpu_xmm14:\n";
7980 Print_xmm_reg(fpu.fpu_xmm14);
7981 outs() << "\t fpu_xmm15:\n";
7982 Print_xmm_reg(fpu.fpu_xmm15);
7983 outs() << "\t fpu_rsrv4:\n";
7984 for (uint32_t f = 0; f < 6; f++) {
7986 for (uint32_t g = 0; g < 16; g++)
7987 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
7990 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
7994 static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
7995 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
7996 outs() << " err " << format("0x%08" PRIx32, exc64.err);
7997 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
8000 static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
8001 bool isLittleEndian, uint32_t cputype) {
8002 if (t.cmd == MachO::LC_THREAD)
8003 outs() << " cmd LC_THREAD\n";
8004 else if (t.cmd == MachO::LC_UNIXTHREAD)
8005 outs() << " cmd LC_UNIXTHREAD\n";
8007 outs() << " cmd " << t.cmd << " (unknown)\n";
8008 outs() << " cmdsize " << t.cmdsize;
8009 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
8010 outs() << " Incorrect size\n";
8014 const char *begin = Ptr + sizeof(struct MachO::thread_command);
8015 const char *end = Ptr + t.cmdsize;
8016 uint32_t flavor, count, left;
8017 if (cputype == MachO::CPU_TYPE_X86_64) {
8018 while (begin < end) {
8019 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8020 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8021 begin += sizeof(uint32_t);
8026 if (isLittleEndian != sys::IsLittleEndianHost)
8027 sys::swapByteOrder(flavor);
8028 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8029 memcpy((char *)&count, begin, sizeof(uint32_t));
8030 begin += sizeof(uint32_t);
8035 if (isLittleEndian != sys::IsLittleEndianHost)
8036 sys::swapByteOrder(count);
8037 if (flavor == MachO::x86_THREAD_STATE64) {
8038 outs() << " flavor x86_THREAD_STATE64\n";
8039 if (count == MachO::x86_THREAD_STATE64_COUNT)
8040 outs() << " count x86_THREAD_STATE64_COUNT\n";
8042 outs() << " count " << count
8043 << " (not x86_THREAD_STATE64_COUNT)\n";
8044 MachO::x86_thread_state64_t cpu64;
8046 if (left >= sizeof(MachO::x86_thread_state64_t)) {
8047 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
8048 begin += sizeof(MachO::x86_thread_state64_t);
8050 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
8051 memcpy(&cpu64, begin, left);
8054 if (isLittleEndian != sys::IsLittleEndianHost)
8056 Print_x86_thread_state64_t(cpu64);
8057 } else if (flavor == MachO::x86_THREAD_STATE) {
8058 outs() << " flavor x86_THREAD_STATE\n";
8059 if (count == MachO::x86_THREAD_STATE_COUNT)
8060 outs() << " count x86_THREAD_STATE_COUNT\n";
8062 outs() << " count " << count
8063 << " (not x86_THREAD_STATE_COUNT)\n";
8064 struct MachO::x86_thread_state_t ts;
8066 if (left >= sizeof(MachO::x86_thread_state_t)) {
8067 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
8068 begin += sizeof(MachO::x86_thread_state_t);
8070 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
8071 memcpy(&ts, begin, left);
8074 if (isLittleEndian != sys::IsLittleEndianHost)
8076 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
8077 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
8078 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
8079 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
8081 outs() << "tsh.count " << ts.tsh.count
8082 << " (not x86_THREAD_STATE64_COUNT\n";
8083 Print_x86_thread_state64_t(ts.uts.ts64);
8085 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
8086 << ts.tsh.count << "\n";
8088 } else if (flavor == MachO::x86_FLOAT_STATE) {
8089 outs() << " flavor x86_FLOAT_STATE\n";
8090 if (count == MachO::x86_FLOAT_STATE_COUNT)
8091 outs() << " count x86_FLOAT_STATE_COUNT\n";
8093 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
8094 struct MachO::x86_float_state_t fs;
8096 if (left >= sizeof(MachO::x86_float_state_t)) {
8097 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
8098 begin += sizeof(MachO::x86_float_state_t);
8100 memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
8101 memcpy(&fs, begin, left);
8104 if (isLittleEndian != sys::IsLittleEndianHost)
8106 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
8107 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
8108 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
8109 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
8111 outs() << "fsh.count " << fs.fsh.count
8112 << " (not x86_FLOAT_STATE64_COUNT\n";
8113 Print_x86_float_state_t(fs.ufs.fs64);
8115 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
8116 << fs.fsh.count << "\n";
8118 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
8119 outs() << " flavor x86_EXCEPTION_STATE\n";
8120 if (count == MachO::x86_EXCEPTION_STATE_COUNT)
8121 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
8123 outs() << " count " << count
8124 << " (not x86_EXCEPTION_STATE_COUNT)\n";
8125 struct MachO::x86_exception_state_t es;
8127 if (left >= sizeof(MachO::x86_exception_state_t)) {
8128 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
8129 begin += sizeof(MachO::x86_exception_state_t);
8131 memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
8132 memcpy(&es, begin, left);
8135 if (isLittleEndian != sys::IsLittleEndianHost)
8137 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
8138 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
8139 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
8140 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
8142 outs() << "\t esh.count " << es.esh.count
8143 << " (not x86_EXCEPTION_STATE64_COUNT\n";
8144 Print_x86_exception_state_t(es.ues.es64);
8146 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
8147 << es.esh.count << "\n";
8150 outs() << " flavor " << flavor << " (unknown)\n";
8151 outs() << " count " << count << "\n";
8152 outs() << " state (unknown)\n";
8153 begin += count * sizeof(uint32_t);
8157 while (begin < end) {
8158 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8159 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8160 begin += sizeof(uint32_t);
8165 if (isLittleEndian != sys::IsLittleEndianHost)
8166 sys::swapByteOrder(flavor);
8167 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8168 memcpy((char *)&count, begin, sizeof(uint32_t));
8169 begin += sizeof(uint32_t);
8174 if (isLittleEndian != sys::IsLittleEndianHost)
8175 sys::swapByteOrder(count);
8176 outs() << " flavor " << flavor << "\n";
8177 outs() << " count " << count << "\n";
8178 outs() << " state (Unknown cputype/cpusubtype)\n";
8179 begin += count * sizeof(uint32_t);
8184 static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
8185 if (dl.cmd == MachO::LC_ID_DYLIB)
8186 outs() << " cmd LC_ID_DYLIB\n";
8187 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
8188 outs() << " cmd LC_LOAD_DYLIB\n";
8189 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
8190 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
8191 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
8192 outs() << " cmd LC_REEXPORT_DYLIB\n";
8193 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
8194 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
8195 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
8196 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
8198 outs() << " cmd " << dl.cmd << " (unknown)\n";
8199 outs() << " cmdsize " << dl.cmdsize;
8200 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
8201 outs() << " Incorrect size\n";
8204 if (dl.dylib.name < dl.cmdsize) {
8205 const char *P = (const char *)(Ptr) + dl.dylib.name;
8206 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
8208 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
8210 outs() << " time stamp " << dl.dylib.timestamp << " ";
8211 time_t t = dl.dylib.timestamp;
8212 outs() << ctime(&t);
8213 outs() << " current version ";
8214 if (dl.dylib.current_version == 0xffffffff)
8217 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
8218 << ((dl.dylib.current_version >> 8) & 0xff) << "."
8219 << (dl.dylib.current_version & 0xff) << "\n";
8220 outs() << "compatibility version ";
8221 if (dl.dylib.compatibility_version == 0xffffffff)
8224 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
8225 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
8226 << (dl.dylib.compatibility_version & 0xff) << "\n";
8229 static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
8230 uint32_t object_size) {
8231 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
8232 outs() << " cmd LC_FUNCTION_STARTS\n";
8233 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
8234 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
8235 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
8236 outs() << " cmd LC_FUNCTION_STARTS\n";
8237 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
8238 outs() << " cmd LC_DATA_IN_CODE\n";
8239 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
8240 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
8241 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
8242 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
8244 outs() << " cmd " << ld.cmd << " (?)\n";
8245 outs() << " cmdsize " << ld.cmdsize;
8246 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
8247 outs() << " Incorrect size\n";
8250 outs() << " dataoff " << ld.dataoff;
8251 if (ld.dataoff > object_size)
8252 outs() << " (past end of file)\n";
8255 outs() << " datasize " << ld.datasize;
8256 uint64_t big_size = ld.dataoff;
8257 big_size += ld.datasize;
8258 if (big_size > object_size)
8259 outs() << " (past end of file)\n";
8264 static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
8265 uint32_t cputype, bool verbose) {
8266 StringRef Buf = Obj->getData();
8268 for (const auto &Command : Obj->load_commands()) {
8269 outs() << "Load command " << Index++ << "\n";
8270 if (Command.C.cmd == MachO::LC_SEGMENT) {
8271 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
8272 const char *sg_segname = SLC.segname;
8273 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
8274 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
8275 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
8277 for (unsigned j = 0; j < SLC.nsects; j++) {
8278 MachO::section S = Obj->getSection(Command, j);
8279 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
8280 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
8281 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
8283 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
8284 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
8285 const char *sg_segname = SLC_64.segname;
8286 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
8287 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
8288 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
8289 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
8290 for (unsigned j = 0; j < SLC_64.nsects; j++) {
8291 MachO::section_64 S_64 = Obj->getSection64(Command, j);
8292 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
8293 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
8294 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
8295 sg_segname, filetype, Buf.size(), verbose);
8297 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
8298 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
8299 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
8300 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
8301 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
8302 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
8303 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
8305 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
8306 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
8307 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
8308 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
8309 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
8310 Command.C.cmd == MachO::LC_ID_DYLINKER ||
8311 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
8312 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
8313 PrintDyldLoadCommand(Dyld, Command.Ptr);
8314 } else if (Command.C.cmd == MachO::LC_UUID) {
8315 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
8316 PrintUuidLoadCommand(Uuid);
8317 } else if (Command.C.cmd == MachO::LC_RPATH) {
8318 MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
8319 PrintRpathLoadCommand(Rpath, Command.Ptr);
8320 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
8321 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS) {
8322 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
8323 PrintVersionMinLoadCommand(Vd);
8324 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
8325 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
8326 PrintSourceVersionCommand(Sd);
8327 } else if (Command.C.cmd == MachO::LC_MAIN) {
8328 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
8329 PrintEntryPointCommand(Ep);
8330 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
8331 MachO::encryption_info_command Ei =
8332 Obj->getEncryptionInfoCommand(Command);
8333 PrintEncryptionInfoCommand(Ei, Buf.size());
8334 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
8335 MachO::encryption_info_command_64 Ei =
8336 Obj->getEncryptionInfoCommand64(Command);
8337 PrintEncryptionInfoCommand64(Ei, Buf.size());
8338 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
8339 MachO::linker_option_command Lo =
8340 Obj->getLinkerOptionLoadCommand(Command);
8341 PrintLinkerOptionCommand(Lo, Command.Ptr);
8342 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
8343 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
8344 PrintSubFrameworkCommand(Sf, Command.Ptr);
8345 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
8346 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
8347 PrintSubUmbrellaCommand(Sf, Command.Ptr);
8348 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
8349 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
8350 PrintSubLibraryCommand(Sl, Command.Ptr);
8351 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
8352 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
8353 PrintSubClientCommand(Sc, Command.Ptr);
8354 } else if (Command.C.cmd == MachO::LC_ROUTINES) {
8355 MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
8356 PrintRoutinesCommand(Rc);
8357 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
8358 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
8359 PrintRoutinesCommand64(Rc);
8360 } else if (Command.C.cmd == MachO::LC_THREAD ||
8361 Command.C.cmd == MachO::LC_UNIXTHREAD) {
8362 MachO::thread_command Tc = Obj->getThreadCommand(Command);
8363 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
8364 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
8365 Command.C.cmd == MachO::LC_ID_DYLIB ||
8366 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
8367 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
8368 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
8369 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
8370 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
8371 PrintDylibCommand(Dl, Command.Ptr);
8372 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
8373 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
8374 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
8375 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
8376 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
8377 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
8378 MachO::linkedit_data_command Ld =
8379 Obj->getLinkeditDataLoadCommand(Command);
8380 PrintLinkEditDataCommand(Ld, Buf.size());
8382 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
8384 outs() << " cmdsize " << Command.C.cmdsize << "\n";
8385 // TODO: get and print the raw bytes of the load command.
8387 // TODO: print all the other kinds of load commands.
8391 static void getAndPrintMachHeader(const MachOObjectFile *Obj,
8392 uint32_t &filetype, uint32_t &cputype,
8394 if (Obj->is64Bit()) {
8395 MachO::mach_header_64 H_64;
8396 H_64 = Obj->getHeader64();
8397 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
8398 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
8399 filetype = H_64.filetype;
8400 cputype = H_64.cputype;
8402 MachO::mach_header H;
8403 H = Obj->getHeader();
8404 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
8405 H.sizeofcmds, H.flags, verbose);
8406 filetype = H.filetype;
8407 cputype = H.cputype;
8411 void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
8412 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
8413 uint32_t filetype = 0;
8414 uint32_t cputype = 0;
8415 getAndPrintMachHeader(file, filetype, cputype, !NonVerbose);
8416 PrintLoadCommands(file, filetype, cputype, !NonVerbose);
8419 //===----------------------------------------------------------------------===//
8420 // export trie dumping
8421 //===----------------------------------------------------------------------===//
8423 void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
8424 for (const llvm::object::ExportEntry &Entry : Obj->exports()) {
8425 uint64_t Flags = Entry.flags();
8426 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
8427 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
8428 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8429 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
8430 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8431 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
8432 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
8434 outs() << "[re-export] ";
8436 outs() << format("0x%08llX ",
8437 Entry.address()); // FIXME:add in base address
8438 outs() << Entry.name();
8439 if (WeakDef || ThreadLocal || Resolver || Abs) {
8440 bool NeedsComma = false;
8443 outs() << "weak_def";
8449 outs() << "per-thread";
8455 outs() << "absolute";
8461 outs() << format("resolver=0x%08llX", Entry.other());
8467 StringRef DylibName = "unknown";
8468 int Ordinal = Entry.other() - 1;
8469 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
8470 if (Entry.otherName().empty())
8471 outs() << " (from " << DylibName << ")";
8473 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
8479 //===----------------------------------------------------------------------===//
8480 // rebase table dumping
8481 //===----------------------------------------------------------------------===//
8486 SegInfo(const object::MachOObjectFile *Obj);
8488 StringRef segmentName(uint32_t SegIndex);
8489 StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset);
8490 uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
8493 struct SectionInfo {
8496 StringRef SectionName;
8497 StringRef SegmentName;
8498 uint64_t OffsetInSegment;
8499 uint64_t SegmentStartAddress;
8500 uint32_t SegmentIndex;
8502 const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset);
8503 SmallVector<SectionInfo, 32> Sections;
8507 SegInfo::SegInfo(const object::MachOObjectFile *Obj) {
8508 // Build table of sections so segIndex/offset pairs can be translated.
8509 uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
8510 StringRef CurSegName;
8511 uint64_t CurSegAddress;
8512 for (const SectionRef &Section : Obj->sections()) {
8514 error(Section.getName(Info.SectionName));
8515 Info.Address = Section.getAddress();
8516 Info.Size = Section.getSize();
8518 Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
8519 if (!Info.SegmentName.equals(CurSegName)) {
8521 CurSegName = Info.SegmentName;
8522 CurSegAddress = Info.Address;
8524 Info.SegmentIndex = CurSegIndex - 1;
8525 Info.OffsetInSegment = Info.Address - CurSegAddress;
8526 Info.SegmentStartAddress = CurSegAddress;
8527 Sections.push_back(Info);
8531 StringRef SegInfo::segmentName(uint32_t SegIndex) {
8532 for (const SectionInfo &SI : Sections) {
8533 if (SI.SegmentIndex == SegIndex)
8534 return SI.SegmentName;
8536 llvm_unreachable("invalid segIndex");
8539 const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex,
8540 uint64_t OffsetInSeg) {
8541 for (const SectionInfo &SI : Sections) {
8542 if (SI.SegmentIndex != SegIndex)
8544 if (SI.OffsetInSegment > OffsetInSeg)
8546 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
8550 llvm_unreachable("segIndex and offset not in any section");
8553 StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) {
8554 return findSection(SegIndex, OffsetInSeg).SectionName;
8557 uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) {
8558 const SectionInfo &SI = findSection(SegIndex, OffsetInSeg);
8559 return SI.SegmentStartAddress + OffsetInSeg;
8562 void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) {
8563 // Build table of sections so names can used in final output.
8564 SegInfo sectionTable(Obj);
8566 outs() << "segment section address type\n";
8567 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) {
8568 uint32_t SegIndex = Entry.segmentIndex();
8569 uint64_t OffsetInSeg = Entry.segmentOffset();
8570 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8571 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8572 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8574 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
8575 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
8576 SegmentName.str().c_str(), SectionName.str().c_str(),
8577 Address, Entry.typeName().str().c_str());
8581 static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
8582 StringRef DylibName;
8584 case MachO::BIND_SPECIAL_DYLIB_SELF:
8585 return "this-image";
8586 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
8587 return "main-executable";
8588 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
8589 return "flat-namespace";
8592 std::error_code EC =
8593 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
8595 return "<<bad library ordinal>>";
8599 return "<<unknown special ordinal>>";
8602 //===----------------------------------------------------------------------===//
8603 // bind table dumping
8604 //===----------------------------------------------------------------------===//
8606 void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) {
8607 // Build table of sections so names can used in final output.
8608 SegInfo sectionTable(Obj);
8610 outs() << "segment section address type "
8611 "addend dylib symbol\n";
8612 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) {
8613 uint32_t SegIndex = Entry.segmentIndex();
8614 uint64_t OffsetInSeg = Entry.segmentOffset();
8615 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8616 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8617 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8619 // Table lines look like:
8620 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
8622 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
8623 Attr = " (weak_import)";
8624 outs() << left_justify(SegmentName, 8) << " "
8625 << left_justify(SectionName, 18) << " "
8626 << format_hex(Address, 10, true) << " "
8627 << left_justify(Entry.typeName(), 8) << " "
8628 << format_decimal(Entry.addend(), 8) << " "
8629 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
8630 << Entry.symbolName() << Attr << "\n";
8634 //===----------------------------------------------------------------------===//
8635 // lazy bind table dumping
8636 //===----------------------------------------------------------------------===//
8638 void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) {
8639 // Build table of sections so names can used in final output.
8640 SegInfo sectionTable(Obj);
8642 outs() << "segment section address "
8644 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) {
8645 uint32_t SegIndex = Entry.segmentIndex();
8646 uint64_t OffsetInSeg = Entry.segmentOffset();
8647 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8648 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8649 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8651 // Table lines look like:
8652 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
8653 outs() << left_justify(SegmentName, 8) << " "
8654 << left_justify(SectionName, 18) << " "
8655 << format_hex(Address, 10, true) << " "
8656 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
8657 << Entry.symbolName() << "\n";
8661 //===----------------------------------------------------------------------===//
8662 // weak bind table dumping
8663 //===----------------------------------------------------------------------===//
8665 void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) {
8666 // Build table of sections so names can used in final output.
8667 SegInfo sectionTable(Obj);
8669 outs() << "segment section address "
8670 "type addend symbol\n";
8671 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) {
8672 // Strong symbols don't have a location to update.
8673 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
8674 outs() << " strong "
8675 << Entry.symbolName() << "\n";
8678 uint32_t SegIndex = Entry.segmentIndex();
8679 uint64_t OffsetInSeg = Entry.segmentOffset();
8680 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8681 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8682 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8684 // Table lines look like:
8685 // __DATA __data 0x00001000 pointer 0 _foo
8686 outs() << left_justify(SegmentName, 8) << " "
8687 << left_justify(SectionName, 18) << " "
8688 << format_hex(Address, 10, true) << " "
8689 << left_justify(Entry.typeName(), 8) << " "
8690 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
8695 // get_dyld_bind_info_symbolname() is used for disassembly and passed an
8696 // address, ReferenceValue, in the Mach-O file and looks in the dyld bind
8697 // information for that address. If the address is found its binding symbol
8698 // name is returned. If not nullptr is returned.
8699 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
8700 struct DisassembleInfo *info) {
8701 if (info->bindtable == nullptr) {
8702 info->bindtable = new (BindTable);
8703 SegInfo sectionTable(info->O);
8704 for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable()) {
8705 uint32_t SegIndex = Entry.segmentIndex();
8706 uint64_t OffsetInSeg = Entry.segmentOffset();
8707 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8708 const char *SymbolName = nullptr;
8709 StringRef name = Entry.symbolName();
8711 SymbolName = name.data();
8712 info->bindtable->push_back(std::make_pair(Address, SymbolName));
8715 for (bind_table_iterator BI = info->bindtable->begin(),
8716 BE = info->bindtable->end();
8718 uint64_t Address = BI->first;
8719 if (ReferenceValue == Address) {
8720 const char *SymbolName = BI->second;