1 //===-- MachODump.cpp - Object file dumping utility for llvm --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the MachO-specific dumper for llvm-objdump.
12 //===----------------------------------------------------------------------===//
14 #include "llvm-objdump.h"
15 #include "llvm-c/Disassembler.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/Config/config.h"
20 #include "llvm/DebugInfo/DIContext.h"
21 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCDisassembler.h"
25 #include "llvm/MC/MCInst.h"
26 #include "llvm/MC/MCInstPrinter.h"
27 #include "llvm/MC/MCInstrDesc.h"
28 #include "llvm/MC/MCInstrInfo.h"
29 #include "llvm/MC/MCRegisterInfo.h"
30 #include "llvm/MC/MCSubtargetInfo.h"
31 #include "llvm/Object/MachO.h"
32 #include "llvm/Object/MachOUniversal.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/Endian.h"
37 #include "llvm/Support/Format.h"
38 #include "llvm/Support/FormattedStream.h"
39 #include "llvm/Support/GraphWriter.h"
40 #include "llvm/Support/LEB128.h"
41 #include "llvm/Support/MachO.h"
42 #include "llvm/Support/MemoryBuffer.h"
43 #include "llvm/Support/TargetRegistry.h"
44 #include "llvm/Support/TargetSelect.h"
45 #include "llvm/Support/raw_ostream.h"
48 #include <system_error>
55 using namespace object;
59 cl::desc("Print line information from debug info if available"));
61 static cl::opt<std::string> DSYMFile("dsym",
62 cl::desc("Use .dSYM file for debug info"));
64 static cl::opt<bool> FullLeadingAddr("full-leading-addr",
65 cl::desc("Print full leading address"));
67 static cl::opt<bool> NoLeadingAddr("no-leading-addr",
68 cl::desc("Print no leading address"));
70 cl::opt<bool> llvm::UniversalHeaders("universal-headers",
71 cl::desc("Print Mach-O universal headers "
72 "(requires -macho)"));
75 llvm::ArchiveHeaders("archive-headers",
76 cl::desc("Print archive headers for Mach-O archives "
77 "(requires -macho)"));
80 ArchiveMemberOffsets("archive-member-offsets",
81 cl::desc("Print the offset to each archive member for "
82 "Mach-O archives (requires -macho and "
83 "-archive-headers)"));
86 llvm::IndirectSymbols("indirect-symbols",
87 cl::desc("Print indirect symbol table for Mach-O "
88 "objects (requires -macho)"));
91 llvm::DataInCode("data-in-code",
92 cl::desc("Print the data in code table for Mach-O objects "
93 "(requires -macho)"));
96 llvm::LinkOptHints("link-opt-hints",
97 cl::desc("Print the linker optimization hints for "
98 "Mach-O objects (requires -macho)"));
100 cl::list<std::string>
101 llvm::DumpSections("section",
102 cl::desc("Prints the specified segment,section for "
103 "Mach-O objects (requires -macho)"));
105 cl::opt<bool> llvm::Raw("raw",
106 cl::desc("Have -section dump the raw binary contents"));
109 llvm::InfoPlist("info-plist",
110 cl::desc("Print the info plist section as strings for "
111 "Mach-O objects (requires -macho)"));
114 llvm::DylibsUsed("dylibs-used",
115 cl::desc("Print the shared libraries used for linked "
116 "Mach-O files (requires -macho)"));
119 llvm::DylibId("dylib-id",
120 cl::desc("Print the shared library's id for the dylib Mach-O "
121 "file (requires -macho)"));
124 llvm::NonVerbose("non-verbose",
125 cl::desc("Print the info for Mach-O objects in "
126 "non-verbose or numeric form (requires -macho)"));
129 llvm::ObjcMetaData("objc-meta-data",
130 cl::desc("Print the Objective-C runtime meta data for "
131 "Mach-O files (requires -macho)"));
133 cl::opt<std::string> llvm::DisSymName(
135 cl::desc("disassemble just this symbol's instructions (requires -macho"));
137 static cl::opt<bool> NoSymbolicOperands(
138 "no-symbolic-operands",
139 cl::desc("do not symbolic operands when disassembling (requires -macho)"));
141 static cl::list<std::string>
142 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
144 bool ArchAll = false;
146 static std::string ThumbTripleName;
148 static const Target *GetTarget(const MachOObjectFile *MachOObj,
149 const char **McpuDefault,
150 const Target **ThumbTarget) {
151 // Figure out the target triple.
152 if (TripleName.empty()) {
153 llvm::Triple TT("unknown-unknown-unknown");
154 llvm::Triple ThumbTriple = Triple();
155 TT = MachOObj->getArch(McpuDefault, &ThumbTriple);
156 TripleName = TT.str();
157 ThumbTripleName = ThumbTriple.str();
160 // Get the target specific parser.
162 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
163 if (TheTarget && ThumbTripleName.empty())
166 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
170 errs() << "llvm-objdump: error: unable to get target for '";
172 errs() << TripleName;
174 errs() << ThumbTripleName;
175 errs() << "', see --version and --triple.\n";
179 struct SymbolSorter {
180 bool operator()(const SymbolRef &A, const SymbolRef &B) {
181 SymbolRef::Type AType, BType;
185 uint64_t AAddr, BAddr;
186 if (AType != SymbolRef::ST_Function)
190 if (BType != SymbolRef::ST_Function)
194 return AAddr < BAddr;
198 // Types for the storted data in code table that is built before disassembly
199 // and the predicate function to sort them.
200 typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
201 typedef std::vector<DiceTableEntry> DiceTable;
202 typedef DiceTable::iterator dice_table_iterator;
204 // This is used to search for a data in code table entry for the PC being
205 // disassembled. The j parameter has the PC in j.first. A single data in code
206 // table entry can cover many bytes for each of its Kind's. So if the offset,
207 // aka the i.first value, of the data in code table entry plus its Length
208 // covers the PC being searched for this will return true. If not it will
210 static bool compareDiceTableEntries(const DiceTableEntry &i,
211 const DiceTableEntry &j) {
213 i.second.getLength(Length);
215 return j.first >= i.first && j.first < i.first + Length;
218 static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
219 unsigned short Kind) {
220 uint32_t Value, Size = 1;
224 case MachO::DICE_KIND_DATA:
227 dumpBytes(ArrayRef<uint8_t>(bytes, 4), outs());
228 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
229 outs() << "\t.long " << Value;
231 } else if (Length >= 2) {
233 dumpBytes(ArrayRef<uint8_t>(bytes, 2), outs());
234 Value = bytes[1] << 8 | bytes[0];
235 outs() << "\t.short " << Value;
239 dumpBytes(ArrayRef<uint8_t>(bytes, 2), outs());
241 outs() << "\t.byte " << Value;
244 if (Kind == MachO::DICE_KIND_DATA)
245 outs() << "\t@ KIND_DATA\n";
247 outs() << "\t@ data in code kind = " << Kind << "\n";
249 case MachO::DICE_KIND_JUMP_TABLE8:
251 dumpBytes(ArrayRef<uint8_t>(bytes, 1), outs());
253 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
256 case MachO::DICE_KIND_JUMP_TABLE16:
258 dumpBytes(ArrayRef<uint8_t>(bytes, 2), outs());
259 Value = bytes[1] << 8 | bytes[0];
260 outs() << "\t.short " << format("%5u", Value & 0xffff)
261 << "\t@ KIND_JUMP_TABLE16\n";
264 case MachO::DICE_KIND_JUMP_TABLE32:
265 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
267 dumpBytes(ArrayRef<uint8_t>(bytes, 4), outs());
268 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
269 outs() << "\t.long " << Value;
270 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
271 outs() << "\t@ KIND_JUMP_TABLE32\n";
273 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
280 static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
281 std::vector<SectionRef> &Sections,
282 std::vector<SymbolRef> &Symbols,
283 SmallVectorImpl<uint64_t> &FoundFns,
284 uint64_t &BaseSegmentAddress) {
285 for (const SymbolRef &Symbol : MachOObj->symbols()) {
287 Symbol.getName(SymName);
288 if (!SymName.startswith("ltmp"))
289 Symbols.push_back(Symbol);
292 for (const SectionRef &Section : MachOObj->sections()) {
294 Section.getName(SectName);
295 Sections.push_back(Section);
298 bool BaseSegmentAddressSet = false;
299 for (const auto &Command : MachOObj->load_commands()) {
300 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
301 // We found a function starts segment, parse the addresses for later
303 MachO::linkedit_data_command LLC =
304 MachOObj->getLinkeditDataLoadCommand(Command);
306 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
307 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
308 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
309 StringRef SegName = SLC.segname;
310 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
311 BaseSegmentAddressSet = true;
312 BaseSegmentAddress = SLC.vmaddr;
318 static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
319 uint32_t n, uint32_t count,
320 uint32_t stride, uint64_t addr) {
321 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
322 uint32_t nindirectsyms = Dysymtab.nindirectsyms;
323 if (n > nindirectsyms)
324 outs() << " (entries start past the end of the indirect symbol "
325 "table) (reserved1 field greater than the table size)";
326 else if (n + count > nindirectsyms)
327 outs() << " (entries extends past the end of the indirect symbol "
330 uint32_t cputype = O->getHeader().cputype;
331 if (cputype & MachO::CPU_ARCH_ABI64)
332 outs() << "address index";
334 outs() << "address index";
339 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
340 if (cputype & MachO::CPU_ARCH_ABI64)
341 outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
343 outs() << format("0x%08" PRIx32, addr + j * stride) << " ";
344 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
345 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
346 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
350 if (indirect_symbol ==
351 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
352 outs() << "LOCAL ABSOLUTE\n";
355 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
356 outs() << "ABSOLUTE\n";
359 outs() << format("%5u ", indirect_symbol);
361 MachO::symtab_command Symtab = O->getSymtabLoadCommand();
362 if (indirect_symbol < Symtab.nsyms) {
363 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
364 SymbolRef Symbol = *Sym;
366 Symbol.getName(SymName);
376 static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
377 for (const auto &Load : O->load_commands()) {
378 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
379 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
380 for (unsigned J = 0; J < Seg.nsects; ++J) {
381 MachO::section_64 Sec = O->getSection64(Load, J);
382 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
383 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
384 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
385 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
386 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
387 section_type == MachO::S_SYMBOL_STUBS) {
389 if (section_type == MachO::S_SYMBOL_STUBS)
390 stride = Sec.reserved2;
394 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
395 << Sec.sectname << ") "
396 << "(size of stubs in reserved2 field is zero)\n";
399 uint32_t count = Sec.size / stride;
400 outs() << "Indirect symbols for (" << Sec.segname << ","
401 << Sec.sectname << ") " << count << " entries";
402 uint32_t n = Sec.reserved1;
403 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
406 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
407 MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
408 for (unsigned J = 0; J < Seg.nsects; ++J) {
409 MachO::section Sec = O->getSection(Load, J);
410 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
411 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
412 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
413 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
414 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
415 section_type == MachO::S_SYMBOL_STUBS) {
417 if (section_type == MachO::S_SYMBOL_STUBS)
418 stride = Sec.reserved2;
422 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
423 << Sec.sectname << ") "
424 << "(size of stubs in reserved2 field is zero)\n";
427 uint32_t count = Sec.size / stride;
428 outs() << "Indirect symbols for (" << Sec.segname << ","
429 << Sec.sectname << ") " << count << " entries";
430 uint32_t n = Sec.reserved1;
431 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
438 static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
439 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
440 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
441 outs() << "Data in code table (" << nentries << " entries)\n";
442 outs() << "offset length kind\n";
443 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
446 DI->getOffset(Offset);
447 outs() << format("0x%08" PRIx32, Offset) << " ";
449 DI->getLength(Length);
450 outs() << format("%6u", Length) << " ";
455 case MachO::DICE_KIND_DATA:
458 case MachO::DICE_KIND_JUMP_TABLE8:
459 outs() << "JUMP_TABLE8";
461 case MachO::DICE_KIND_JUMP_TABLE16:
462 outs() << "JUMP_TABLE16";
464 case MachO::DICE_KIND_JUMP_TABLE32:
465 outs() << "JUMP_TABLE32";
467 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
468 outs() << "ABS_JUMP_TABLE32";
471 outs() << format("0x%04" PRIx32, Kind);
475 outs() << format("0x%04" PRIx32, Kind);
480 static void PrintLinkOptHints(MachOObjectFile *O) {
481 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
482 const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
483 uint32_t nloh = LohLC.datasize;
484 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
485 for (uint32_t i = 0; i < nloh;) {
487 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
489 outs() << " identifier " << identifier << " ";
492 switch (identifier) {
494 outs() << "AdrpAdrp\n";
497 outs() << "AdrpLdr\n";
500 outs() << "AdrpAddLdr\n";
503 outs() << "AdrpLdrGotLdr\n";
506 outs() << "AdrpAddStr\n";
509 outs() << "AdrpLdrGotStr\n";
512 outs() << "AdrpAdd\n";
515 outs() << "AdrpLdrGot\n";
518 outs() << "Unknown identifier value\n";
521 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
523 outs() << " narguments " << narguments << "\n";
527 for (uint32_t j = 0; j < narguments; j++) {
528 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
530 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
537 static void PrintDylibs(MachOObjectFile *O, bool JustId) {
539 for (const auto &Load : O->load_commands()) {
540 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
541 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
542 Load.C.cmd == MachO::LC_LOAD_DYLIB ||
543 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
544 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
545 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
546 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
547 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
548 if (dl.dylib.name < dl.cmdsize) {
549 const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
554 outs() << " (compatibility version "
555 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
556 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
557 << (dl.dylib.compatibility_version & 0xff) << ",";
558 outs() << " current version "
559 << ((dl.dylib.current_version >> 16) & 0xffff) << "."
560 << ((dl.dylib.current_version >> 8) & 0xff) << "."
561 << (dl.dylib.current_version & 0xff) << ")\n";
564 outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
565 if (Load.C.cmd == MachO::LC_ID_DYLIB)
566 outs() << "LC_ID_DYLIB ";
567 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
568 outs() << "LC_LOAD_DYLIB ";
569 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
570 outs() << "LC_LOAD_WEAK_DYLIB ";
571 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
572 outs() << "LC_LAZY_LOAD_DYLIB ";
573 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
574 outs() << "LC_REEXPORT_DYLIB ";
575 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
576 outs() << "LC_LOAD_UPWARD_DYLIB ";
579 outs() << "command " << Index++ << "\n";
585 typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
587 static void CreateSymbolAddressMap(MachOObjectFile *O,
588 SymbolAddressMap *AddrMap) {
589 // Create a map of symbol addresses to symbol names.
590 for (const SymbolRef &Symbol : O->symbols()) {
593 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
594 ST == SymbolRef::ST_Other) {
596 Symbol.getAddress(Address);
598 Symbol.getName(SymName);
599 if (!SymName.startswith(".objc"))
600 (*AddrMap)[Address] = SymName;
605 // GuessSymbolName is passed the address of what might be a symbol and a
606 // pointer to the SymbolAddressMap. It returns the name of a symbol
607 // with that address or nullptr if no symbol is found with that address.
608 static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
609 const char *SymbolName = nullptr;
610 // A DenseMap can't lookup up some values.
611 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
612 StringRef name = AddrMap->lookup(value);
614 SymbolName = name.data();
619 static void DumpCstringChar(const char c) {
623 outs().write_escaped(p);
626 static void DumpCstringSection(MachOObjectFile *O, const char *sect,
627 uint32_t sect_size, uint64_t sect_addr,
628 bool print_addresses) {
629 for (uint32_t i = 0; i < sect_size; i++) {
630 if (print_addresses) {
632 outs() << format("%016" PRIx64, sect_addr + i) << " ";
634 outs() << format("%08" PRIx64, sect_addr + i) << " ";
636 for (; i < sect_size && sect[i] != '\0'; i++)
637 DumpCstringChar(sect[i]);
638 if (i < sect_size && sect[i] == '\0')
643 static void DumpLiteral4(uint32_t l, float f) {
644 outs() << format("0x%08" PRIx32, l);
645 if ((l & 0x7f800000) != 0x7f800000)
646 outs() << format(" (%.16e)\n", f);
649 outs() << " (+Infinity)\n";
650 else if (l == 0xff800000)
651 outs() << " (-Infinity)\n";
652 else if ((l & 0x00400000) == 0x00400000)
653 outs() << " (non-signaling Not-a-Number)\n";
655 outs() << " (signaling Not-a-Number)\n";
659 static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
660 uint32_t sect_size, uint64_t sect_addr,
661 bool print_addresses) {
662 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
663 if (print_addresses) {
665 outs() << format("%016" PRIx64, sect_addr + i) << " ";
667 outs() << format("%08" PRIx64, sect_addr + i) << " ";
670 memcpy(&f, sect + i, sizeof(float));
671 if (O->isLittleEndian() != sys::IsLittleEndianHost)
672 sys::swapByteOrder(f);
674 memcpy(&l, sect + i, sizeof(uint32_t));
675 if (O->isLittleEndian() != sys::IsLittleEndianHost)
676 sys::swapByteOrder(l);
681 static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
683 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
685 if (O->isLittleEndian()) {
692 // Hi is the high word, so this is equivalent to if(isfinite(d))
693 if ((Hi & 0x7ff00000) != 0x7ff00000)
694 outs() << format(" (%.16e)\n", d);
696 if (Hi == 0x7ff00000 && Lo == 0)
697 outs() << " (+Infinity)\n";
698 else if (Hi == 0xfff00000 && Lo == 0)
699 outs() << " (-Infinity)\n";
700 else if ((Hi & 0x00080000) == 0x00080000)
701 outs() << " (non-signaling Not-a-Number)\n";
703 outs() << " (signaling Not-a-Number)\n";
707 static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
708 uint32_t sect_size, uint64_t sect_addr,
709 bool print_addresses) {
710 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
711 if (print_addresses) {
713 outs() << format("%016" PRIx64, sect_addr + i) << " ";
715 outs() << format("%08" PRIx64, sect_addr + i) << " ";
718 memcpy(&d, sect + i, sizeof(double));
719 if (O->isLittleEndian() != sys::IsLittleEndianHost)
720 sys::swapByteOrder(d);
722 memcpy(&l0, sect + i, sizeof(uint32_t));
723 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
724 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
725 sys::swapByteOrder(l0);
726 sys::swapByteOrder(l1);
728 DumpLiteral8(O, l0, l1, d);
732 static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
733 outs() << format("0x%08" PRIx32, l0) << " ";
734 outs() << format("0x%08" PRIx32, l1) << " ";
735 outs() << format("0x%08" PRIx32, l2) << " ";
736 outs() << format("0x%08" PRIx32, l3) << "\n";
739 static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
740 uint32_t sect_size, uint64_t sect_addr,
741 bool print_addresses) {
742 for (uint32_t i = 0; i < sect_size; i += 16) {
743 if (print_addresses) {
745 outs() << format("%016" PRIx64, sect_addr + i) << " ";
747 outs() << format("%08" PRIx64, sect_addr + i) << " ";
749 uint32_t l0, l1, l2, l3;
750 memcpy(&l0, sect + i, sizeof(uint32_t));
751 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
752 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
753 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
754 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
755 sys::swapByteOrder(l0);
756 sys::swapByteOrder(l1);
757 sys::swapByteOrder(l2);
758 sys::swapByteOrder(l3);
760 DumpLiteral16(l0, l1, l2, l3);
764 static void DumpLiteralPointerSection(MachOObjectFile *O,
765 const SectionRef &Section,
766 const char *sect, uint32_t sect_size,
768 bool print_addresses) {
769 // Collect the literal sections in this Mach-O file.
770 std::vector<SectionRef> LiteralSections;
771 for (const SectionRef &Section : O->sections()) {
772 DataRefImpl Ref = Section.getRawDataRefImpl();
773 uint32_t section_type;
775 const MachO::section_64 Sec = O->getSection64(Ref);
776 section_type = Sec.flags & MachO::SECTION_TYPE;
778 const MachO::section Sec = O->getSection(Ref);
779 section_type = Sec.flags & MachO::SECTION_TYPE;
781 if (section_type == MachO::S_CSTRING_LITERALS ||
782 section_type == MachO::S_4BYTE_LITERALS ||
783 section_type == MachO::S_8BYTE_LITERALS ||
784 section_type == MachO::S_16BYTE_LITERALS)
785 LiteralSections.push_back(Section);
788 // Set the size of the literal pointer.
789 uint32_t lp_size = O->is64Bit() ? 8 : 4;
791 // Collect the external relocation symbols for the literal pointers.
792 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
793 for (const RelocationRef &Reloc : Section.relocations()) {
795 MachO::any_relocation_info RE;
796 bool isExtern = false;
797 Rel = Reloc.getRawDataRefImpl();
798 RE = O->getRelocation(Rel);
799 isExtern = O->getPlainRelocationExternal(RE);
801 uint64_t RelocOffset;
802 Reloc.getOffset(RelocOffset);
803 symbol_iterator RelocSym = Reloc.getSymbol();
804 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
807 array_pod_sort(Relocs.begin(), Relocs.end());
809 // Dump each literal pointer.
810 for (uint32_t i = 0; i < sect_size; i += lp_size) {
811 if (print_addresses) {
813 outs() << format("%016" PRIx64, sect_addr + i) << " ";
815 outs() << format("%08" PRIx64, sect_addr + i) << " ";
819 memcpy(&lp, sect + i, sizeof(uint64_t));
820 if (O->isLittleEndian() != sys::IsLittleEndianHost)
821 sys::swapByteOrder(lp);
824 memcpy(&li, sect + i, sizeof(uint32_t));
825 if (O->isLittleEndian() != sys::IsLittleEndianHost)
826 sys::swapByteOrder(li);
830 // First look for an external relocation entry for this literal pointer.
831 auto Reloc = std::find_if(
832 Relocs.begin(), Relocs.end(),
833 [&](const std::pair<uint64_t, SymbolRef> &P) { return P.first == i; });
834 if (Reloc != Relocs.end()) {
835 symbol_iterator RelocSym = Reloc->second;
837 RelocSym->getName(SymName);
838 outs() << "external relocation entry for symbol:" << SymName << "\n";
842 // For local references see what the section the literal pointer points to.
843 auto Sect = std::find_if(LiteralSections.begin(), LiteralSections.end(),
844 [&](const SectionRef &R) {
845 return lp >= R.getAddress() &&
846 lp < R.getAddress() + R.getSize();
848 if (Sect == LiteralSections.end()) {
849 outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
853 uint64_t SectAddress = Sect->getAddress();
854 uint64_t SectSize = Sect->getSize();
857 Sect->getName(SectName);
858 DataRefImpl Ref = Sect->getRawDataRefImpl();
859 StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
860 outs() << SegmentName << ":" << SectName << ":";
862 uint32_t section_type;
864 const MachO::section_64 Sec = O->getSection64(Ref);
865 section_type = Sec.flags & MachO::SECTION_TYPE;
867 const MachO::section Sec = O->getSection(Ref);
868 section_type = Sec.flags & MachO::SECTION_TYPE;
872 Sect->getContents(BytesStr);
873 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
875 switch (section_type) {
876 case MachO::S_CSTRING_LITERALS:
877 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
879 DumpCstringChar(Contents[i]);
883 case MachO::S_4BYTE_LITERALS:
885 memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
887 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
888 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
889 sys::swapByteOrder(f);
890 sys::swapByteOrder(l);
894 case MachO::S_8BYTE_LITERALS: {
896 memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
898 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
899 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
901 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
902 sys::swapByteOrder(f);
903 sys::swapByteOrder(l0);
904 sys::swapByteOrder(l1);
906 DumpLiteral8(O, l0, l1, d);
909 case MachO::S_16BYTE_LITERALS: {
910 uint32_t l0, l1, l2, l3;
911 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
912 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
914 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
916 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
918 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
919 sys::swapByteOrder(l0);
920 sys::swapByteOrder(l1);
921 sys::swapByteOrder(l2);
922 sys::swapByteOrder(l3);
924 DumpLiteral16(l0, l1, l2, l3);
931 static void DumpInitTermPointerSection(MachOObjectFile *O, const char *sect,
932 uint32_t sect_size, uint64_t sect_addr,
933 SymbolAddressMap *AddrMap,
937 stride = sizeof(uint64_t);
939 stride = sizeof(uint32_t);
940 for (uint32_t i = 0; i < sect_size; i += stride) {
941 const char *SymbolName = nullptr;
943 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
944 uint64_t pointer_value;
945 memcpy(&pointer_value, sect + i, stride);
946 if (O->isLittleEndian() != sys::IsLittleEndianHost)
947 sys::swapByteOrder(pointer_value);
948 outs() << format("0x%016" PRIx64, pointer_value);
950 SymbolName = GuessSymbolName(pointer_value, AddrMap);
952 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
953 uint32_t pointer_value;
954 memcpy(&pointer_value, sect + i, stride);
955 if (O->isLittleEndian() != sys::IsLittleEndianHost)
956 sys::swapByteOrder(pointer_value);
957 outs() << format("0x%08" PRIx32, pointer_value);
959 SymbolName = GuessSymbolName(pointer_value, AddrMap);
962 outs() << " " << SymbolName;
967 static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
968 uint32_t size, uint64_t addr) {
969 uint32_t cputype = O->getHeader().cputype;
970 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
972 for (uint32_t i = 0; i < size; i += j, addr += j) {
974 outs() << format("%016" PRIx64, addr) << "\t";
976 outs() << format("%08" PRIx64, addr) << "\t";
977 for (j = 0; j < 16 && i + j < size; j++) {
978 uint8_t byte_word = *(sect + i + j);
979 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
985 for (uint32_t i = 0; i < size; i += j, addr += j) {
987 outs() << format("%016" PRIx64, addr) << "\t";
989 outs() << format("%08" PRIx64, sect) << "\t";
990 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
991 j += sizeof(int32_t)) {
992 if (i + j + sizeof(int32_t) < size) {
994 memcpy(&long_word, sect + i + j, sizeof(int32_t));
995 if (O->isLittleEndian() != sys::IsLittleEndianHost)
996 sys::swapByteOrder(long_word);
997 outs() << format("%08" PRIx32, long_word) << " ";
999 for (uint32_t k = 0; i + j + k < size; k++) {
1000 uint8_t byte_word = *(sect + i + j);
1001 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
1010 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
1011 StringRef DisSegName, StringRef DisSectName);
1012 static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
1013 uint32_t size, uint32_t addr);
1015 static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
1017 SymbolAddressMap AddrMap;
1019 CreateSymbolAddressMap(O, &AddrMap);
1021 for (unsigned i = 0; i < DumpSections.size(); ++i) {
1022 StringRef DumpSection = DumpSections[i];
1023 std::pair<StringRef, StringRef> DumpSegSectName;
1024 DumpSegSectName = DumpSection.split(',');
1025 StringRef DumpSegName, DumpSectName;
1026 if (DumpSegSectName.second.size()) {
1027 DumpSegName = DumpSegSectName.first;
1028 DumpSectName = DumpSegSectName.second;
1031 DumpSectName = DumpSegSectName.first;
1033 for (const SectionRef &Section : O->sections()) {
1035 Section.getName(SectName);
1036 DataRefImpl Ref = Section.getRawDataRefImpl();
1037 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1038 if ((DumpSegName.empty() || SegName == DumpSegName) &&
1039 (SectName == DumpSectName)) {
1041 uint32_t section_flags;
1043 const MachO::section_64 Sec = O->getSection64(Ref);
1044 section_flags = Sec.flags;
1047 const MachO::section Sec = O->getSection(Ref);
1048 section_flags = Sec.flags;
1050 uint32_t section_type = section_flags & MachO::SECTION_TYPE;
1053 Section.getContents(BytesStr);
1054 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1055 uint32_t sect_size = BytesStr.size();
1056 uint64_t sect_addr = Section.getAddress();
1059 outs().write(BytesStr.data(), BytesStr.size());
1063 outs() << "Contents of (" << SegName << "," << SectName
1067 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1068 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1069 DisassembleMachO(Filename, O, SegName, SectName);
1072 if (SegName == "__TEXT" && SectName == "__info_plist") {
1076 if (SegName == "__OBJC" && SectName == "__protocol") {
1077 DumpProtocolSection(O, sect, sect_size, sect_addr);
1080 switch (section_type) {
1081 case MachO::S_REGULAR:
1082 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1084 case MachO::S_ZEROFILL:
1085 outs() << "zerofill section and has no contents in the file\n";
1087 case MachO::S_CSTRING_LITERALS:
1088 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1090 case MachO::S_4BYTE_LITERALS:
1091 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1093 case MachO::S_8BYTE_LITERALS:
1094 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1096 case MachO::S_16BYTE_LITERALS:
1097 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1099 case MachO::S_LITERAL_POINTERS:
1100 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
1103 case MachO::S_MOD_INIT_FUNC_POINTERS:
1104 case MachO::S_MOD_TERM_FUNC_POINTERS:
1105 DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap,
1109 outs() << "Unknown section type ("
1110 << format("0x%08" PRIx32, section_type) << ")\n";
1111 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1115 if (section_type == MachO::S_ZEROFILL)
1116 outs() << "zerofill section and has no contents in the file\n";
1118 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1125 static void DumpInfoPlistSectionContents(StringRef Filename,
1126 MachOObjectFile *O) {
1127 for (const SectionRef &Section : O->sections()) {
1129 Section.getName(SectName);
1130 DataRefImpl Ref = Section.getRawDataRefImpl();
1131 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1132 if (SegName == "__TEXT" && SectName == "__info_plist") {
1133 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
1135 Section.getContents(BytesStr);
1136 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1143 // checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1144 // and if it is and there is a list of architecture flags is specified then
1145 // check to make sure this Mach-O file is one of those architectures or all
1146 // architectures were specified. If not then an error is generated and this
1147 // routine returns false. Else it returns true.
1148 static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
1149 if (isa<MachOObjectFile>(O) && !ArchAll && ArchFlags.size() != 0) {
1150 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O);
1151 bool ArchFound = false;
1152 MachO::mach_header H;
1153 MachO::mach_header_64 H_64;
1155 if (MachO->is64Bit()) {
1156 H_64 = MachO->MachOObjectFile::getHeader64();
1157 T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype);
1159 H = MachO->MachOObjectFile::getHeader();
1160 T = MachOObjectFile::getArch(H.cputype, H.cpusubtype);
1163 for (i = 0; i < ArchFlags.size(); ++i) {
1164 if (ArchFlags[i] == T.getArchName())
1169 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1170 << "architecture: " + ArchFlags[i] + "\n";
1177 static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1179 // ProcessMachO() is passed a single opened Mach-O file, which may be an
1180 // archive member and or in a slice of a universal file. It prints the
1181 // the file name and header info and then processes it according to the
1182 // command line options.
1183 static void ProcessMachO(StringRef Filename, MachOObjectFile *MachOOF,
1184 StringRef ArchiveMemberName = StringRef(),
1185 StringRef ArchitectureName = StringRef()) {
1186 // If we are doing some processing here on the Mach-O file print the header
1187 // info. And don't print it otherwise like in the case of printing the
1188 // UniversalHeaders or ArchiveHeaders.
1189 if (Disassemble || PrivateHeaders || ExportsTrie || Rebase || Bind ||
1190 LazyBind || WeakBind || IndirectSymbols || DataInCode || LinkOptHints ||
1191 DylibsUsed || DylibId || ObjcMetaData ||
1192 (DumpSections.size() != 0 && !Raw)) {
1194 if (!ArchiveMemberName.empty())
1195 outs() << '(' << ArchiveMemberName << ')';
1196 if (!ArchitectureName.empty())
1197 outs() << " (architecture " << ArchitectureName << ")";
1202 DisassembleMachO(Filename, MachOOF, "__TEXT", "__text");
1203 if (IndirectSymbols)
1204 PrintIndirectSymbols(MachOOF, !NonVerbose);
1206 PrintDataInCodeTable(MachOOF, !NonVerbose);
1208 PrintLinkOptHints(MachOOF);
1210 PrintRelocations(MachOOF);
1212 PrintSectionHeaders(MachOOF);
1213 if (SectionContents)
1214 PrintSectionContents(MachOOF);
1215 if (DumpSections.size() != 0)
1216 DumpSectionContents(Filename, MachOOF, !NonVerbose);
1218 DumpInfoPlistSectionContents(Filename, MachOOF);
1220 PrintDylibs(MachOOF, false);
1222 PrintDylibs(MachOOF, true);
1224 PrintSymbolTable(MachOOF);
1226 printMachOUnwindInfo(MachOOF);
1228 printMachOFileHeader(MachOOF);
1230 printObjcMetaData(MachOOF, !NonVerbose);
1232 printExportsTrie(MachOOF);
1234 printRebaseTable(MachOOF);
1236 printBindTable(MachOOF);
1238 printLazyBindTable(MachOOF);
1240 printWeakBindTable(MachOOF);
1243 // printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1244 static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
1245 outs() << " cputype (" << cputype << ")\n";
1246 outs() << " cpusubtype (" << cpusubtype << ")\n";
1249 // printCPUType() helps print_fat_headers by printing the cputype and
1250 // pusubtype (symbolically for the one's it knows about).
1251 static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
1253 case MachO::CPU_TYPE_I386:
1254 switch (cpusubtype) {
1255 case MachO::CPU_SUBTYPE_I386_ALL:
1256 outs() << " cputype CPU_TYPE_I386\n";
1257 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
1260 printUnknownCPUType(cputype, cpusubtype);
1264 case MachO::CPU_TYPE_X86_64:
1265 switch (cpusubtype) {
1266 case MachO::CPU_SUBTYPE_X86_64_ALL:
1267 outs() << " cputype CPU_TYPE_X86_64\n";
1268 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1270 case MachO::CPU_SUBTYPE_X86_64_H:
1271 outs() << " cputype CPU_TYPE_X86_64\n";
1272 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
1275 printUnknownCPUType(cputype, cpusubtype);
1279 case MachO::CPU_TYPE_ARM:
1280 switch (cpusubtype) {
1281 case MachO::CPU_SUBTYPE_ARM_ALL:
1282 outs() << " cputype CPU_TYPE_ARM\n";
1283 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
1285 case MachO::CPU_SUBTYPE_ARM_V4T:
1286 outs() << " cputype CPU_TYPE_ARM\n";
1287 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
1289 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1290 outs() << " cputype CPU_TYPE_ARM\n";
1291 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
1293 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1294 outs() << " cputype CPU_TYPE_ARM\n";
1295 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
1297 case MachO::CPU_SUBTYPE_ARM_V6:
1298 outs() << " cputype CPU_TYPE_ARM\n";
1299 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
1301 case MachO::CPU_SUBTYPE_ARM_V6M:
1302 outs() << " cputype CPU_TYPE_ARM\n";
1303 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
1305 case MachO::CPU_SUBTYPE_ARM_V7:
1306 outs() << " cputype CPU_TYPE_ARM\n";
1307 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
1309 case MachO::CPU_SUBTYPE_ARM_V7EM:
1310 outs() << " cputype CPU_TYPE_ARM\n";
1311 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
1313 case MachO::CPU_SUBTYPE_ARM_V7K:
1314 outs() << " cputype CPU_TYPE_ARM\n";
1315 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
1317 case MachO::CPU_SUBTYPE_ARM_V7M:
1318 outs() << " cputype CPU_TYPE_ARM\n";
1319 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
1321 case MachO::CPU_SUBTYPE_ARM_V7S:
1322 outs() << " cputype CPU_TYPE_ARM\n";
1323 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
1326 printUnknownCPUType(cputype, cpusubtype);
1330 case MachO::CPU_TYPE_ARM64:
1331 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1332 case MachO::CPU_SUBTYPE_ARM64_ALL:
1333 outs() << " cputype CPU_TYPE_ARM64\n";
1334 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
1337 printUnknownCPUType(cputype, cpusubtype);
1342 printUnknownCPUType(cputype, cpusubtype);
1347 static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
1349 outs() << "Fat headers\n";
1351 outs() << "fat_magic FAT_MAGIC\n";
1353 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
1355 uint32_t nfat_arch = UB->getNumberOfObjects();
1356 StringRef Buf = UB->getData();
1357 uint64_t size = Buf.size();
1358 uint64_t big_size = sizeof(struct MachO::fat_header) +
1359 nfat_arch * sizeof(struct MachO::fat_arch);
1360 outs() << "nfat_arch " << UB->getNumberOfObjects();
1362 outs() << " (malformed, contains zero architecture types)\n";
1363 else if (big_size > size)
1364 outs() << " (malformed, architectures past end of file)\n";
1368 for (uint32_t i = 0; i < nfat_arch; ++i) {
1369 MachOUniversalBinary::ObjectForArch OFA(UB, i);
1370 uint32_t cputype = OFA.getCPUType();
1371 uint32_t cpusubtype = OFA.getCPUSubType();
1372 outs() << "architecture ";
1373 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
1374 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
1375 uint32_t other_cputype = other_OFA.getCPUType();
1376 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
1377 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
1378 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
1379 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
1380 outs() << "(illegal duplicate architecture) ";
1385 outs() << OFA.getArchTypeName() << "\n";
1386 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1388 outs() << i << "\n";
1389 outs() << " cputype " << cputype << "\n";
1390 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
1394 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
1395 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
1397 outs() << " capabilities "
1398 << format("0x%" PRIx32,
1399 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
1400 outs() << " offset " << OFA.getOffset();
1401 if (OFA.getOffset() > size)
1402 outs() << " (past end of file)";
1403 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
1404 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
1406 outs() << " size " << OFA.getSize();
1407 big_size = OFA.getOffset() + OFA.getSize();
1408 if (big_size > size)
1409 outs() << " (past end of file)";
1411 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
1416 static void printArchiveChild(Archive::Child &C, bool verbose,
1417 bool print_offset) {
1419 outs() << C.getChildOffset() << "\t";
1420 sys::fs::perms Mode = C.getAccessMode();
1422 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
1423 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
1425 if (Mode & sys::fs::owner_read)
1429 if (Mode & sys::fs::owner_write)
1433 if (Mode & sys::fs::owner_exe)
1437 if (Mode & sys::fs::group_read)
1441 if (Mode & sys::fs::group_write)
1445 if (Mode & sys::fs::group_exe)
1449 if (Mode & sys::fs::others_read)
1453 if (Mode & sys::fs::others_write)
1457 if (Mode & sys::fs::others_exe)
1462 outs() << format("0%o ", Mode);
1465 unsigned UID = C.getUID();
1466 outs() << format("%3d/", UID);
1467 unsigned GID = C.getGID();
1468 outs() << format("%-3d ", GID);
1469 uint64_t Size = C.getRawSize();
1470 outs() << format("%5" PRId64, Size) << " ";
1472 StringRef RawLastModified = C.getRawLastModified();
1475 if (RawLastModified.getAsInteger(10, Seconds))
1476 outs() << "(date: \"%s\" contains non-decimal chars) " << RawLastModified;
1478 // Since cime(3) returns a 26 character string of the form:
1479 // "Sun Sep 16 01:03:52 1973\n\0"
1480 // just print 24 characters.
1482 outs() << format("%.24s ", ctime(&t));
1485 outs() << RawLastModified << " ";
1489 ErrorOr<StringRef> NameOrErr = C.getName();
1490 if (NameOrErr.getError()) {
1491 StringRef RawName = C.getRawName();
1492 outs() << RawName << "\n";
1494 StringRef Name = NameOrErr.get();
1495 outs() << Name << "\n";
1498 StringRef RawName = C.getRawName();
1499 outs() << RawName << "\n";
1503 static void printArchiveHeaders(Archive *A, bool verbose, bool print_offset) {
1504 if (A->hasSymbolTable()) {
1505 Archive::child_iterator S = A->getSymbolTableChild();
1506 Archive::Child C = *S;
1507 printArchiveChild(C, verbose, print_offset);
1509 for (Archive::child_iterator I = A->child_begin(), E = A->child_end(); I != E;
1511 Archive::Child C = *I;
1512 printArchiveChild(C, verbose, print_offset);
1516 // ParseInputMachO() parses the named Mach-O file in Filename and handles the
1517 // -arch flags selecting just those slices as specified by them and also parses
1518 // archive files. Then for each individual Mach-O file ProcessMachO() is
1519 // called to process the file based on the command line options.
1520 void llvm::ParseInputMachO(StringRef Filename) {
1521 // Check for -arch all and verifiy the -arch flags are valid.
1522 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1523 if (ArchFlags[i] == "all") {
1526 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
1527 errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
1528 "'for the -arch option\n";
1534 // Attempt to open the binary.
1535 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
1536 if (std::error_code EC = BinaryOrErr.getError()) {
1537 errs() << "llvm-objdump: '" << Filename << "': " << EC.message() << ".\n";
1540 Binary &Bin = *BinaryOrErr.get().getBinary();
1542 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1543 outs() << "Archive : " << Filename << "\n";
1545 printArchiveHeaders(A, !NonVerbose, ArchiveMemberOffsets);
1546 for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
1548 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = I->getAsBinary();
1549 if (ChildOrErr.getError())
1551 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1552 if (!checkMachOAndArchFlags(O, Filename))
1554 ProcessMachO(Filename, O, O->getFileName());
1559 if (UniversalHeaders) {
1560 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
1561 printMachOUniversalHeaders(UB, !NonVerbose);
1563 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1564 // If we have a list of architecture flags specified dump only those.
1565 if (!ArchAll && ArchFlags.size() != 0) {
1566 // Look for a slice in the universal binary that matches each ArchFlag.
1568 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1570 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1571 E = UB->end_objects();
1573 if (ArchFlags[i] == I->getArchTypeName()) {
1575 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
1576 I->getAsObjectFile();
1577 std::string ArchitectureName = "";
1578 if (ArchFlags.size() > 1)
1579 ArchitectureName = I->getArchTypeName();
1581 ObjectFile &O = *ObjOrErr.get();
1582 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
1583 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
1584 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1585 I->getAsArchive()) {
1586 std::unique_ptr<Archive> &A = *AOrErr;
1587 outs() << "Archive : " << Filename;
1588 if (!ArchitectureName.empty())
1589 outs() << " (architecture " << ArchitectureName << ")";
1592 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
1593 for (Archive::child_iterator AI = A->child_begin(),
1594 AE = A->child_end();
1596 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1597 if (ChildOrErr.getError())
1599 if (MachOObjectFile *O =
1600 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
1601 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
1607 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1608 << "architecture: " + ArchFlags[i] + "\n";
1614 // No architecture flags were specified so if this contains a slice that
1615 // matches the host architecture dump only that.
1617 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1618 E = UB->end_objects();
1620 if (MachOObjectFile::getHostArch().getArchName() ==
1621 I->getArchTypeName()) {
1622 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1623 std::string ArchiveName;
1624 ArchiveName.clear();
1626 ObjectFile &O = *ObjOrErr.get();
1627 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
1628 ProcessMachO(Filename, MachOOF);
1629 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1630 I->getAsArchive()) {
1631 std::unique_ptr<Archive> &A = *AOrErr;
1632 outs() << "Archive : " << Filename << "\n";
1634 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
1635 for (Archive::child_iterator AI = A->child_begin(),
1636 AE = A->child_end();
1638 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1639 if (ChildOrErr.getError())
1641 if (MachOObjectFile *O =
1642 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
1643 ProcessMachO(Filename, O, O->getFileName());
1650 // Either all architectures have been specified or none have been specified
1651 // and this does not contain the host architecture so dump all the slices.
1652 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1653 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1654 E = UB->end_objects();
1656 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1657 std::string ArchitectureName = "";
1658 if (moreThanOneArch)
1659 ArchitectureName = I->getArchTypeName();
1661 ObjectFile &Obj = *ObjOrErr.get();
1662 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
1663 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
1664 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
1665 std::unique_ptr<Archive> &A = *AOrErr;
1666 outs() << "Archive : " << Filename;
1667 if (!ArchitectureName.empty())
1668 outs() << " (architecture " << ArchitectureName << ")";
1671 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
1672 for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
1674 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary();
1675 if (ChildOrErr.getError())
1677 if (MachOObjectFile *O =
1678 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1679 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
1680 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
1688 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
1689 if (!checkMachOAndArchFlags(O, Filename))
1691 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
1692 ProcessMachO(Filename, MachOOF);
1694 errs() << "llvm-objdump: '" << Filename << "': "
1695 << "Object is not a Mach-O file type.\n";
1697 errs() << "llvm-objdump: '" << Filename << "': "
1698 << "Unrecognized file type.\n";
1701 typedef std::pair<uint64_t, const char *> BindInfoEntry;
1702 typedef std::vector<BindInfoEntry> BindTable;
1703 typedef BindTable::iterator bind_table_iterator;
1705 // The block of info used by the Symbolizer call backs.
1706 struct DisassembleInfo {
1710 SymbolAddressMap *AddrMap;
1711 std::vector<SectionRef> *Sections;
1712 const char *class_name;
1713 const char *selector_name;
1715 char *demangled_name;
1718 BindTable *bindtable;
1721 // SymbolizerGetOpInfo() is the operand information call back function.
1722 // This is called to get the symbolic information for operand(s) of an
1723 // instruction when it is being done. This routine does this from
1724 // the relocation information, symbol table, etc. That block of information
1725 // is a pointer to the struct DisassembleInfo that was passed when the
1726 // disassembler context was created and passed to back to here when
1727 // called back by the disassembler for instruction operands that could have
1728 // relocation information. The address of the instruction containing operand is
1729 // at the Pc parameter. The immediate value the operand has is passed in
1730 // op_info->Value and is at Offset past the start of the instruction and has a
1731 // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
1732 // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
1733 // names and addends of the symbolic expression to add for the operand. The
1734 // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
1735 // information is returned then this function returns 1 else it returns 0.
1736 static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
1737 uint64_t Size, int TagType, void *TagBuf) {
1738 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
1739 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
1740 uint64_t value = op_info->Value;
1742 // Make sure all fields returned are zero if we don't set them.
1743 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
1744 op_info->Value = value;
1746 // If the TagType is not the value 1 which it code knows about or if no
1747 // verbose symbolic information is wanted then just return 0, indicating no
1748 // information is being returned.
1749 if (TagType != 1 || !info->verbose)
1752 unsigned int Arch = info->O->getArch();
1753 if (Arch == Triple::x86) {
1754 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1756 // First search the section's relocation entries (if any) for an entry
1757 // for this section offset.
1758 uint32_t sect_addr = info->S.getAddress();
1759 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1760 bool reloc_found = false;
1762 MachO::any_relocation_info RE;
1763 bool isExtern = false;
1765 bool r_scattered = false;
1766 uint32_t r_value, pair_r_value, r_type;
1767 for (const RelocationRef &Reloc : info->S.relocations()) {
1768 uint64_t RelocOffset;
1769 Reloc.getOffset(RelocOffset);
1770 if (RelocOffset == sect_offset) {
1771 Rel = Reloc.getRawDataRefImpl();
1772 RE = info->O->getRelocation(Rel);
1773 r_type = info->O->getAnyRelocationType(RE);
1774 r_scattered = info->O->isRelocationScattered(RE);
1776 r_value = info->O->getScatteredRelocationValue(RE);
1777 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1778 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
1779 DataRefImpl RelNext = Rel;
1780 info->O->moveRelocationNext(RelNext);
1781 MachO::any_relocation_info RENext;
1782 RENext = info->O->getRelocation(RelNext);
1783 if (info->O->isRelocationScattered(RENext))
1784 pair_r_value = info->O->getScatteredRelocationValue(RENext);
1789 isExtern = info->O->getPlainRelocationExternal(RE);
1791 symbol_iterator RelocSym = Reloc.getSymbol();
1799 if (reloc_found && isExtern) {
1801 Symbol.getName(SymName);
1802 const char *name = SymName.data();
1803 op_info->AddSymbol.Present = 1;
1804 op_info->AddSymbol.Name = name;
1805 // For i386 extern relocation entries the value in the instruction is
1806 // the offset from the symbol, and value is already set in op_info->Value.
1809 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1810 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
1811 const char *add = GuessSymbolName(r_value, info->AddrMap);
1812 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
1813 uint32_t offset = value - (r_value - pair_r_value);
1814 op_info->AddSymbol.Present = 1;
1816 op_info->AddSymbol.Name = add;
1818 op_info->AddSymbol.Value = r_value;
1819 op_info->SubtractSymbol.Present = 1;
1821 op_info->SubtractSymbol.Name = sub;
1823 op_info->SubtractSymbol.Value = pair_r_value;
1824 op_info->Value = offset;
1828 // Second search the external relocation entries of a fully linked image
1829 // (if any) for an entry that matches this segment offset.
1830 // uint32_t seg_offset = (Pc + Offset);
1833 if (Arch == Triple::x86_64) {
1834 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1836 // First search the section's relocation entries (if any) for an entry
1837 // for this section offset.
1838 uint64_t sect_addr = info->S.getAddress();
1839 uint64_t sect_offset = (Pc + Offset) - sect_addr;
1840 bool reloc_found = false;
1842 MachO::any_relocation_info RE;
1843 bool isExtern = false;
1845 for (const RelocationRef &Reloc : info->S.relocations()) {
1846 uint64_t RelocOffset;
1847 Reloc.getOffset(RelocOffset);
1848 if (RelocOffset == sect_offset) {
1849 Rel = Reloc.getRawDataRefImpl();
1850 RE = info->O->getRelocation(Rel);
1851 // NOTE: Scattered relocations don't exist on x86_64.
1852 isExtern = info->O->getPlainRelocationExternal(RE);
1854 symbol_iterator RelocSym = Reloc.getSymbol();
1861 if (reloc_found && isExtern) {
1862 // The Value passed in will be adjusted by the Pc if the instruction
1863 // adds the Pc. But for x86_64 external relocation entries the Value
1864 // is the offset from the external symbol.
1865 if (info->O->getAnyRelocationPCRel(RE))
1866 op_info->Value -= Pc + Offset + Size;
1868 Symbol.getName(SymName);
1869 const char *name = SymName.data();
1870 unsigned Type = info->O->getAnyRelocationType(RE);
1871 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
1872 DataRefImpl RelNext = Rel;
1873 info->O->moveRelocationNext(RelNext);
1874 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
1875 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
1876 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
1877 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
1878 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
1879 op_info->SubtractSymbol.Present = 1;
1880 op_info->SubtractSymbol.Name = name;
1881 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
1882 Symbol = *RelocSymNext;
1883 StringRef SymNameNext;
1884 Symbol.getName(SymNameNext);
1885 name = SymNameNext.data();
1888 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
1889 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
1890 op_info->AddSymbol.Present = 1;
1891 op_info->AddSymbol.Name = name;
1895 // Second search the external relocation entries of a fully linked image
1896 // (if any) for an entry that matches this segment offset.
1897 // uint64_t seg_offset = (Pc + Offset);
1900 if (Arch == Triple::arm) {
1901 if (Offset != 0 || (Size != 4 && Size != 2))
1903 // First search the section's relocation entries (if any) for an entry
1904 // for this section offset.
1905 uint32_t sect_addr = info->S.getAddress();
1906 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1908 MachO::any_relocation_info RE;
1909 bool isExtern = false;
1911 bool r_scattered = false;
1912 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
1914 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
1915 [&](const RelocationRef &Reloc) {
1916 uint64_t RelocOffset;
1917 Reloc.getOffset(RelocOffset);
1918 return RelocOffset == sect_offset;
1921 if (Reloc == info->S.relocations().end())
1924 Rel = Reloc->getRawDataRefImpl();
1925 RE = info->O->getRelocation(Rel);
1926 r_length = info->O->getAnyRelocationLength(RE);
1927 r_scattered = info->O->isRelocationScattered(RE);
1929 r_value = info->O->getScatteredRelocationValue(RE);
1930 r_type = info->O->getScatteredRelocationType(RE);
1932 r_type = info->O->getAnyRelocationType(RE);
1933 isExtern = info->O->getPlainRelocationExternal(RE);
1935 symbol_iterator RelocSym = Reloc->getSymbol();
1939 if (r_type == MachO::ARM_RELOC_HALF ||
1940 r_type == MachO::ARM_RELOC_SECTDIFF ||
1941 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
1942 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1943 DataRefImpl RelNext = Rel;
1944 info->O->moveRelocationNext(RelNext);
1945 MachO::any_relocation_info RENext;
1946 RENext = info->O->getRelocation(RelNext);
1947 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
1948 if (info->O->isRelocationScattered(RENext))
1949 pair_r_value = info->O->getScatteredRelocationValue(RENext);
1954 Symbol.getName(SymName);
1955 const char *name = SymName.data();
1956 op_info->AddSymbol.Present = 1;
1957 op_info->AddSymbol.Name = name;
1959 case MachO::ARM_RELOC_HALF:
1960 if ((r_length & 0x1) == 1) {
1961 op_info->Value = value << 16 | other_half;
1962 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1964 op_info->Value = other_half << 16 | value;
1965 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
1973 // If we have a branch that is not an external relocation entry then
1974 // return 0 so the code in tryAddingSymbolicOperand() can use the
1975 // SymbolLookUp call back with the branch target address to look up the
1976 // symbol and possiblity add an annotation for a symbol stub.
1977 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
1978 r_type == MachO::ARM_THUMB_RELOC_BR22))
1981 uint32_t offset = 0;
1982 if (r_type == MachO::ARM_RELOC_HALF ||
1983 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1984 if ((r_length & 0x1) == 1)
1985 value = value << 16 | other_half;
1987 value = other_half << 16 | value;
1989 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
1990 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
1991 offset = value - r_value;
1995 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1996 if ((r_length & 0x1) == 1)
1997 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
1999 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2000 const char *add = GuessSymbolName(r_value, info->AddrMap);
2001 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
2002 int32_t offset = value - (r_value - pair_r_value);
2003 op_info->AddSymbol.Present = 1;
2005 op_info->AddSymbol.Name = add;
2007 op_info->AddSymbol.Value = r_value;
2008 op_info->SubtractSymbol.Present = 1;
2010 op_info->SubtractSymbol.Name = sub;
2012 op_info->SubtractSymbol.Value = pair_r_value;
2013 op_info->Value = offset;
2017 op_info->AddSymbol.Present = 1;
2018 op_info->Value = offset;
2019 if (r_type == MachO::ARM_RELOC_HALF) {
2020 if ((r_length & 0x1) == 1)
2021 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2023 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2025 const char *add = GuessSymbolName(value, info->AddrMap);
2026 if (add != nullptr) {
2027 op_info->AddSymbol.Name = add;
2030 op_info->AddSymbol.Value = value;
2033 if (Arch == Triple::aarch64) {
2034 if (Offset != 0 || Size != 4)
2036 // First search the section's relocation entries (if any) for an entry
2037 // for this section offset.
2038 uint64_t sect_addr = info->S.getAddress();
2039 uint64_t sect_offset = (Pc + Offset) - sect_addr;
2041 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
2042 [&](const RelocationRef &Reloc) {
2043 uint64_t RelocOffset;
2044 Reloc.getOffset(RelocOffset);
2045 return RelocOffset == sect_offset;
2048 if (Reloc == info->S.relocations().end())
2051 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2052 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2053 uint32_t r_type = info->O->getAnyRelocationType(RE);
2054 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2055 DataRefImpl RelNext = Rel;
2056 info->O->moveRelocationNext(RelNext);
2057 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2059 value = info->O->getPlainRelocationSymbolNum(RENext);
2060 op_info->Value = value;
2063 // NOTE: Scattered relocations don't exist on arm64.
2064 if (!info->O->getPlainRelocationExternal(RE))
2067 Reloc->getSymbol()->getName(SymName);
2068 const char *name = SymName.data();
2069 op_info->AddSymbol.Present = 1;
2070 op_info->AddSymbol.Name = name;
2073 case MachO::ARM64_RELOC_PAGE21:
2075 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2077 case MachO::ARM64_RELOC_PAGEOFF12:
2079 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2081 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2083 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2085 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2087 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2089 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2090 /* @tvlppage is not implemented in llvm-mc */
2091 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2093 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2094 /* @tvlppageoff is not implemented in llvm-mc */
2095 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2098 case MachO::ARM64_RELOC_BRANCH26:
2099 op_info->VariantKind = LLVMDisassembler_VariantKind_None;
2107 // GuessCstringPointer is passed the address of what might be a pointer to a
2108 // literal string in a cstring section. If that address is in a cstring section
2109 // it returns a pointer to that string. Else it returns nullptr.
2110 static const char *GuessCstringPointer(uint64_t ReferenceValue,
2111 struct DisassembleInfo *info) {
2112 for (const auto &Load : info->O->load_commands()) {
2113 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2114 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2115 for (unsigned J = 0; J < Seg.nsects; ++J) {
2116 MachO::section_64 Sec = info->O->getSection64(Load, J);
2117 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2118 if (section_type == MachO::S_CSTRING_LITERALS &&
2119 ReferenceValue >= Sec.addr &&
2120 ReferenceValue < Sec.addr + Sec.size) {
2121 uint64_t sect_offset = ReferenceValue - Sec.addr;
2122 uint64_t object_offset = Sec.offset + sect_offset;
2123 StringRef MachOContents = info->O->getData();
2124 uint64_t object_size = MachOContents.size();
2125 const char *object_addr = (const char *)MachOContents.data();
2126 if (object_offset < object_size) {
2127 const char *name = object_addr + object_offset;
2134 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2135 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2136 for (unsigned J = 0; J < Seg.nsects; ++J) {
2137 MachO::section Sec = info->O->getSection(Load, J);
2138 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2139 if (section_type == MachO::S_CSTRING_LITERALS &&
2140 ReferenceValue >= Sec.addr &&
2141 ReferenceValue < Sec.addr + Sec.size) {
2142 uint64_t sect_offset = ReferenceValue - Sec.addr;
2143 uint64_t object_offset = Sec.offset + sect_offset;
2144 StringRef MachOContents = info->O->getData();
2145 uint64_t object_size = MachOContents.size();
2146 const char *object_addr = (const char *)MachOContents.data();
2147 if (object_offset < object_size) {
2148 const char *name = object_addr + object_offset;
2160 // GuessIndirectSymbol returns the name of the indirect symbol for the
2161 // ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
2162 // an address of a symbol stub or a lazy or non-lazy pointer to associate the
2163 // symbol name being referenced by the stub or pointer.
2164 static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2165 struct DisassembleInfo *info) {
2166 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2167 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
2168 for (const auto &Load : info->O->load_commands()) {
2169 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2170 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2171 for (unsigned J = 0; J < Seg.nsects; ++J) {
2172 MachO::section_64 Sec = info->O->getSection64(Load, J);
2173 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2174 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2175 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2176 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2177 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2178 section_type == MachO::S_SYMBOL_STUBS) &&
2179 ReferenceValue >= Sec.addr &&
2180 ReferenceValue < Sec.addr + Sec.size) {
2182 if (section_type == MachO::S_SYMBOL_STUBS)
2183 stride = Sec.reserved2;
2188 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2189 if (index < Dysymtab.nindirectsyms) {
2190 uint32_t indirect_symbol =
2191 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
2192 if (indirect_symbol < Symtab.nsyms) {
2193 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2194 SymbolRef Symbol = *Sym;
2196 Symbol.getName(SymName);
2197 const char *name = SymName.data();
2203 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2204 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2205 for (unsigned J = 0; J < Seg.nsects; ++J) {
2206 MachO::section Sec = info->O->getSection(Load, J);
2207 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2208 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2209 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2210 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2211 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2212 section_type == MachO::S_SYMBOL_STUBS) &&
2213 ReferenceValue >= Sec.addr &&
2214 ReferenceValue < Sec.addr + Sec.size) {
2216 if (section_type == MachO::S_SYMBOL_STUBS)
2217 stride = Sec.reserved2;
2222 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2223 if (index < Dysymtab.nindirectsyms) {
2224 uint32_t indirect_symbol =
2225 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
2226 if (indirect_symbol < Symtab.nsyms) {
2227 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2228 SymbolRef Symbol = *Sym;
2230 Symbol.getName(SymName);
2231 const char *name = SymName.data();
2242 // method_reference() is called passing it the ReferenceName that might be
2243 // a reference it to an Objective-C method call. If so then it allocates and
2244 // assembles a method call string with the values last seen and saved in
2245 // the DisassembleInfo's class_name and selector_name fields. This is saved
2246 // into the method field of the info and any previous string is free'ed.
2247 // Then the class_name field in the info is set to nullptr. The method call
2248 // string is set into ReferenceName and ReferenceType is set to
2249 // LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
2250 // then both ReferenceType and ReferenceName are left unchanged.
2251 static void method_reference(struct DisassembleInfo *info,
2252 uint64_t *ReferenceType,
2253 const char **ReferenceName) {
2254 unsigned int Arch = info->O->getArch();
2255 if (*ReferenceName != nullptr) {
2256 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
2257 if (info->selector_name != nullptr) {
2258 if (info->method != nullptr)
2260 if (info->class_name != nullptr) {
2261 info->method = (char *)malloc(5 + strlen(info->class_name) +
2262 strlen(info->selector_name));
2263 if (info->method != nullptr) {
2264 strcpy(info->method, "+[");
2265 strcat(info->method, info->class_name);
2266 strcat(info->method, " ");
2267 strcat(info->method, info->selector_name);
2268 strcat(info->method, "]");
2269 *ReferenceName = info->method;
2270 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2273 info->method = (char *)malloc(9 + strlen(info->selector_name));
2274 if (info->method != nullptr) {
2275 if (Arch == Triple::x86_64)
2276 strcpy(info->method, "-[%rdi ");
2277 else if (Arch == Triple::aarch64)
2278 strcpy(info->method, "-[x0 ");
2280 strcpy(info->method, "-[r? ");
2281 strcat(info->method, info->selector_name);
2282 strcat(info->method, "]");
2283 *ReferenceName = info->method;
2284 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2287 info->class_name = nullptr;
2289 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
2290 if (info->selector_name != nullptr) {
2291 if (info->method != nullptr)
2293 info->method = (char *)malloc(17 + strlen(info->selector_name));
2294 if (info->method != nullptr) {
2295 if (Arch == Triple::x86_64)
2296 strcpy(info->method, "-[[%rdi super] ");
2297 else if (Arch == Triple::aarch64)
2298 strcpy(info->method, "-[[x0 super] ");
2300 strcpy(info->method, "-[[r? super] ");
2301 strcat(info->method, info->selector_name);
2302 strcat(info->method, "]");
2303 *ReferenceName = info->method;
2304 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2306 info->class_name = nullptr;
2312 // GuessPointerPointer() is passed the address of what might be a pointer to
2313 // a reference to an Objective-C class, selector, message ref or cfstring.
2314 // If so the value of the pointer is returned and one of the booleans are set
2315 // to true. If not zero is returned and all the booleans are set to false.
2316 static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
2317 struct DisassembleInfo *info,
2318 bool &classref, bool &selref, bool &msgref,
2324 for (const auto &Load : info->O->load_commands()) {
2325 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2326 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2327 for (unsigned J = 0; J < Seg.nsects; ++J) {
2328 MachO::section_64 Sec = info->O->getSection64(Load, J);
2329 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
2330 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2331 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
2332 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
2333 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
2334 ReferenceValue >= Sec.addr &&
2335 ReferenceValue < Sec.addr + Sec.size) {
2336 uint64_t sect_offset = ReferenceValue - Sec.addr;
2337 uint64_t object_offset = Sec.offset + sect_offset;
2338 StringRef MachOContents = info->O->getData();
2339 uint64_t object_size = MachOContents.size();
2340 const char *object_addr = (const char *)MachOContents.data();
2341 if (object_offset < object_size) {
2342 uint64_t pointer_value;
2343 memcpy(&pointer_value, object_addr + object_offset,
2345 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2346 sys::swapByteOrder(pointer_value);
2347 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
2349 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2350 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
2352 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
2353 ReferenceValue + 8 < Sec.addr + Sec.size) {
2355 memcpy(&pointer_value, object_addr + object_offset + 8,
2357 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2358 sys::swapByteOrder(pointer_value);
2359 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
2361 return pointer_value;
2368 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
2373 // get_pointer_64 returns a pointer to the bytes in the object file at the
2374 // Address from a section in the Mach-O file. And indirectly returns the
2375 // offset into the section, number of bytes left in the section past the offset
2376 // and which section is was being referenced. If the Address is not in a
2377 // section nullptr is returned.
2378 static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
2379 uint32_t &left, SectionRef &S,
2380 DisassembleInfo *info,
2381 bool objc_only = false) {
2385 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
2386 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
2387 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
2390 ((*(info->Sections))[SectIdx]).getName(SectName);
2391 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
2392 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
2393 if (SegName != "__OBJC" && SectName != "__cstring")
2396 if (Address >= SectAddress && Address < SectAddress + SectSize) {
2397 S = (*(info->Sections))[SectIdx];
2398 offset = Address - SectAddress;
2399 left = SectSize - offset;
2400 StringRef SectContents;
2401 ((*(info->Sections))[SectIdx]).getContents(SectContents);
2402 return SectContents.data() + offset;
2408 static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
2409 uint32_t &left, SectionRef &S,
2410 DisassembleInfo *info,
2411 bool objc_only = false) {
2412 return get_pointer_64(Address, offset, left, S, info, objc_only);
2415 // get_symbol_64() returns the name of a symbol (or nullptr) and the address of
2416 // the symbol indirectly through n_value. Based on the relocation information
2417 // for the specified section offset in the specified section reference.
2418 // If no relocation information is found and a non-zero ReferenceValue for the
2419 // symbol is passed, look up that address in the info's AddrMap.
2420 static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
2421 DisassembleInfo *info, uint64_t &n_value,
2422 uint64_t ReferenceValue = UnknownAddress) {
2427 // See if there is an external relocation entry at the sect_offset.
2428 bool reloc_found = false;
2430 MachO::any_relocation_info RE;
2431 bool isExtern = false;
2433 for (const RelocationRef &Reloc : S.relocations()) {
2434 uint64_t RelocOffset;
2435 Reloc.getOffset(RelocOffset);
2436 if (RelocOffset == sect_offset) {
2437 Rel = Reloc.getRawDataRefImpl();
2438 RE = info->O->getRelocation(Rel);
2439 if (info->O->isRelocationScattered(RE))
2441 isExtern = info->O->getPlainRelocationExternal(RE);
2443 symbol_iterator RelocSym = Reloc.getSymbol();
2450 // If there is an external relocation entry for a symbol in this section
2451 // at this section_offset then use that symbol's value for the n_value
2452 // and return its name.
2453 const char *SymbolName = nullptr;
2454 if (reloc_found && isExtern) {
2455 Symbol.getAddress(n_value);
2456 if (n_value == UnknownAddress)
2459 Symbol.getName(name);
2460 if (!name.empty()) {
2461 SymbolName = name.data();
2466 // TODO: For fully linked images, look through the external relocation
2467 // entries off the dynamic symtab command. For these the r_offset is from the
2468 // start of the first writeable segment in the Mach-O file. So the offset
2469 // to this section from that segment is passed to this routine by the caller,
2470 // as the database_offset. Which is the difference of the section's starting
2471 // address and the first writable segment.
2473 // NOTE: need add passing the database_offset to this routine.
2475 // We did not find an external relocation entry so look up the ReferenceValue
2476 // as an address of a symbol and if found return that symbol's name.
2477 if (ReferenceValue != UnknownAddress)
2478 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
2483 static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
2484 DisassembleInfo *info,
2485 uint32_t ReferenceValue) {
2487 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
2490 // These are structs in the Objective-C meta data and read to produce the
2491 // comments for disassembly. While these are part of the ABI they are no
2492 // public defintions. So the are here not in include/llvm/Support/MachO.h .
2494 // The cfstring object in a 64-bit Mach-O file.
2495 struct cfstring64_t {
2496 uint64_t isa; // class64_t * (64-bit pointer)
2497 uint64_t flags; // flag bits
2498 uint64_t characters; // char * (64-bit pointer)
2499 uint64_t length; // number of non-NULL characters in above
2502 // The class object in a 64-bit Mach-O file.
2504 uint64_t isa; // class64_t * (64-bit pointer)
2505 uint64_t superclass; // class64_t * (64-bit pointer)
2506 uint64_t cache; // Cache (64-bit pointer)
2507 uint64_t vtable; // IMP * (64-bit pointer)
2508 uint64_t data; // class_ro64_t * (64-bit pointer)
2512 uint32_t isa; /* class32_t * (32-bit pointer) */
2513 uint32_t superclass; /* class32_t * (32-bit pointer) */
2514 uint32_t cache; /* Cache (32-bit pointer) */
2515 uint32_t vtable; /* IMP * (32-bit pointer) */
2516 uint32_t data; /* class_ro32_t * (32-bit pointer) */
2519 struct class_ro64_t {
2521 uint32_t instanceStart;
2522 uint32_t instanceSize;
2524 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
2525 uint64_t name; // const char * (64-bit pointer)
2526 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
2527 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
2528 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
2529 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
2530 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
2533 struct class_ro32_t {
2535 uint32_t instanceStart;
2536 uint32_t instanceSize;
2537 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
2538 uint32_t name; /* const char * (32-bit pointer) */
2539 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
2540 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
2541 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
2542 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
2543 uint32_t baseProperties; /* const struct objc_property_list *
2547 /* Values for class_ro{64,32}_t->flags */
2548 #define RO_META (1 << 0)
2549 #define RO_ROOT (1 << 1)
2550 #define RO_HAS_CXX_STRUCTORS (1 << 2)
2552 struct method_list64_t {
2555 /* struct method64_t first; These structures follow inline */
2558 struct method_list32_t {
2561 /* struct method32_t first; These structures follow inline */
2565 uint64_t name; /* SEL (64-bit pointer) */
2566 uint64_t types; /* const char * (64-bit pointer) */
2567 uint64_t imp; /* IMP (64-bit pointer) */
2571 uint32_t name; /* SEL (32-bit pointer) */
2572 uint32_t types; /* const char * (32-bit pointer) */
2573 uint32_t imp; /* IMP (32-bit pointer) */
2576 struct protocol_list64_t {
2577 uint64_t count; /* uintptr_t (a 64-bit value) */
2578 /* struct protocol64_t * list[0]; These pointers follow inline */
2581 struct protocol_list32_t {
2582 uint32_t count; /* uintptr_t (a 32-bit value) */
2583 /* struct protocol32_t * list[0]; These pointers follow inline */
2586 struct protocol64_t {
2587 uint64_t isa; /* id * (64-bit pointer) */
2588 uint64_t name; /* const char * (64-bit pointer) */
2589 uint64_t protocols; /* struct protocol_list64_t *
2591 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
2592 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
2593 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
2594 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
2595 uint64_t instanceProperties; /* struct objc_property_list *
2599 struct protocol32_t {
2600 uint32_t isa; /* id * (32-bit pointer) */
2601 uint32_t name; /* const char * (32-bit pointer) */
2602 uint32_t protocols; /* struct protocol_list_t *
2604 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
2605 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
2606 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
2607 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
2608 uint32_t instanceProperties; /* struct objc_property_list *
2612 struct ivar_list64_t {
2615 /* struct ivar64_t first; These structures follow inline */
2618 struct ivar_list32_t {
2621 /* struct ivar32_t first; These structures follow inline */
2625 uint64_t offset; /* uintptr_t * (64-bit pointer) */
2626 uint64_t name; /* const char * (64-bit pointer) */
2627 uint64_t type; /* const char * (64-bit pointer) */
2633 uint32_t offset; /* uintptr_t * (32-bit pointer) */
2634 uint32_t name; /* const char * (32-bit pointer) */
2635 uint32_t type; /* const char * (32-bit pointer) */
2640 struct objc_property_list64 {
2643 /* struct objc_property64 first; These structures follow inline */
2646 struct objc_property_list32 {
2649 /* struct objc_property32 first; These structures follow inline */
2652 struct objc_property64 {
2653 uint64_t name; /* const char * (64-bit pointer) */
2654 uint64_t attributes; /* const char * (64-bit pointer) */
2657 struct objc_property32 {
2658 uint32_t name; /* const char * (32-bit pointer) */
2659 uint32_t attributes; /* const char * (32-bit pointer) */
2662 struct category64_t {
2663 uint64_t name; /* const char * (64-bit pointer) */
2664 uint64_t cls; /* struct class_t * (64-bit pointer) */
2665 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
2666 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
2667 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
2668 uint64_t instanceProperties; /* struct objc_property_list *
2672 struct category32_t {
2673 uint32_t name; /* const char * (32-bit pointer) */
2674 uint32_t cls; /* struct class_t * (32-bit pointer) */
2675 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
2676 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
2677 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
2678 uint32_t instanceProperties; /* struct objc_property_list *
2682 struct objc_image_info64 {
2686 struct objc_image_info32 {
2690 struct imageInfo_t {
2694 /* masks for objc_image_info.flags */
2695 #define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
2696 #define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
2698 struct message_ref64 {
2699 uint64_t imp; /* IMP (64-bit pointer) */
2700 uint64_t sel; /* SEL (64-bit pointer) */
2703 struct message_ref32 {
2704 uint32_t imp; /* IMP (32-bit pointer) */
2705 uint32_t sel; /* SEL (32-bit pointer) */
2708 // Objective-C 1 (32-bit only) meta data structs.
2710 struct objc_module_t {
2713 uint32_t name; /* char * (32-bit pointer) */
2714 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
2717 struct objc_symtab_t {
2718 uint32_t sel_ref_cnt;
2719 uint32_t refs; /* SEL * (32-bit pointer) */
2720 uint16_t cls_def_cnt;
2721 uint16_t cat_def_cnt;
2722 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
2725 struct objc_class_t {
2726 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2727 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
2728 uint32_t name; /* const char * (32-bit pointer) */
2731 int32_t instance_size;
2732 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
2733 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
2734 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
2735 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
2738 #define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
2739 // class is not a metaclass
2740 #define CLS_CLASS 0x1
2741 // class is a metaclass
2742 #define CLS_META 0x2
2744 struct objc_category_t {
2745 uint32_t category_name; /* char * (32-bit pointer) */
2746 uint32_t class_name; /* char * (32-bit pointer) */
2747 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
2748 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
2749 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
2752 struct objc_ivar_t {
2753 uint32_t ivar_name; /* char * (32-bit pointer) */
2754 uint32_t ivar_type; /* char * (32-bit pointer) */
2755 int32_t ivar_offset;
2758 struct objc_ivar_list_t {
2760 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
2763 struct objc_method_list_t {
2764 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
2765 int32_t method_count;
2766 // struct objc_method_t method_list[1]; /* variable length structure */
2769 struct objc_method_t {
2770 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2771 uint32_t method_types; /* char * (32-bit pointer) */
2772 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
2776 struct objc_protocol_list_t {
2777 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
2779 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
2780 // (32-bit pointer) */
2783 struct objc_protocol_t {
2784 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2785 uint32_t protocol_name; /* char * (32-bit pointer) */
2786 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
2787 uint32_t instance_methods; /* struct objc_method_description_list *
2789 uint32_t class_methods; /* struct objc_method_description_list *
2793 struct objc_method_description_list_t {
2795 // struct objc_method_description_t list[1];
2798 struct objc_method_description_t {
2799 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2800 uint32_t types; /* char * (32-bit pointer) */
2803 inline void swapStruct(struct cfstring64_t &cfs) {
2804 sys::swapByteOrder(cfs.isa);
2805 sys::swapByteOrder(cfs.flags);
2806 sys::swapByteOrder(cfs.characters);
2807 sys::swapByteOrder(cfs.length);
2810 inline void swapStruct(struct class64_t &c) {
2811 sys::swapByteOrder(c.isa);
2812 sys::swapByteOrder(c.superclass);
2813 sys::swapByteOrder(c.cache);
2814 sys::swapByteOrder(c.vtable);
2815 sys::swapByteOrder(c.data);
2818 inline void swapStruct(struct class32_t &c) {
2819 sys::swapByteOrder(c.isa);
2820 sys::swapByteOrder(c.superclass);
2821 sys::swapByteOrder(c.cache);
2822 sys::swapByteOrder(c.vtable);
2823 sys::swapByteOrder(c.data);
2826 inline void swapStruct(struct class_ro64_t &cro) {
2827 sys::swapByteOrder(cro.flags);
2828 sys::swapByteOrder(cro.instanceStart);
2829 sys::swapByteOrder(cro.instanceSize);
2830 sys::swapByteOrder(cro.reserved);
2831 sys::swapByteOrder(cro.ivarLayout);
2832 sys::swapByteOrder(cro.name);
2833 sys::swapByteOrder(cro.baseMethods);
2834 sys::swapByteOrder(cro.baseProtocols);
2835 sys::swapByteOrder(cro.ivars);
2836 sys::swapByteOrder(cro.weakIvarLayout);
2837 sys::swapByteOrder(cro.baseProperties);
2840 inline void swapStruct(struct class_ro32_t &cro) {
2841 sys::swapByteOrder(cro.flags);
2842 sys::swapByteOrder(cro.instanceStart);
2843 sys::swapByteOrder(cro.instanceSize);
2844 sys::swapByteOrder(cro.ivarLayout);
2845 sys::swapByteOrder(cro.name);
2846 sys::swapByteOrder(cro.baseMethods);
2847 sys::swapByteOrder(cro.baseProtocols);
2848 sys::swapByteOrder(cro.ivars);
2849 sys::swapByteOrder(cro.weakIvarLayout);
2850 sys::swapByteOrder(cro.baseProperties);
2853 inline void swapStruct(struct method_list64_t &ml) {
2854 sys::swapByteOrder(ml.entsize);
2855 sys::swapByteOrder(ml.count);
2858 inline void swapStruct(struct method_list32_t &ml) {
2859 sys::swapByteOrder(ml.entsize);
2860 sys::swapByteOrder(ml.count);
2863 inline void swapStruct(struct method64_t &m) {
2864 sys::swapByteOrder(m.name);
2865 sys::swapByteOrder(m.types);
2866 sys::swapByteOrder(m.imp);
2869 inline void swapStruct(struct method32_t &m) {
2870 sys::swapByteOrder(m.name);
2871 sys::swapByteOrder(m.types);
2872 sys::swapByteOrder(m.imp);
2875 inline void swapStruct(struct protocol_list64_t &pl) {
2876 sys::swapByteOrder(pl.count);
2879 inline void swapStruct(struct protocol_list32_t &pl) {
2880 sys::swapByteOrder(pl.count);
2883 inline void swapStruct(struct protocol64_t &p) {
2884 sys::swapByteOrder(p.isa);
2885 sys::swapByteOrder(p.name);
2886 sys::swapByteOrder(p.protocols);
2887 sys::swapByteOrder(p.instanceMethods);
2888 sys::swapByteOrder(p.classMethods);
2889 sys::swapByteOrder(p.optionalInstanceMethods);
2890 sys::swapByteOrder(p.optionalClassMethods);
2891 sys::swapByteOrder(p.instanceProperties);
2894 inline void swapStruct(struct protocol32_t &p) {
2895 sys::swapByteOrder(p.isa);
2896 sys::swapByteOrder(p.name);
2897 sys::swapByteOrder(p.protocols);
2898 sys::swapByteOrder(p.instanceMethods);
2899 sys::swapByteOrder(p.classMethods);
2900 sys::swapByteOrder(p.optionalInstanceMethods);
2901 sys::swapByteOrder(p.optionalClassMethods);
2902 sys::swapByteOrder(p.instanceProperties);
2905 inline void swapStruct(struct ivar_list64_t &il) {
2906 sys::swapByteOrder(il.entsize);
2907 sys::swapByteOrder(il.count);
2910 inline void swapStruct(struct ivar_list32_t &il) {
2911 sys::swapByteOrder(il.entsize);
2912 sys::swapByteOrder(il.count);
2915 inline void swapStruct(struct ivar64_t &i) {
2916 sys::swapByteOrder(i.offset);
2917 sys::swapByteOrder(i.name);
2918 sys::swapByteOrder(i.type);
2919 sys::swapByteOrder(i.alignment);
2920 sys::swapByteOrder(i.size);
2923 inline void swapStruct(struct ivar32_t &i) {
2924 sys::swapByteOrder(i.offset);
2925 sys::swapByteOrder(i.name);
2926 sys::swapByteOrder(i.type);
2927 sys::swapByteOrder(i.alignment);
2928 sys::swapByteOrder(i.size);
2931 inline void swapStruct(struct objc_property_list64 &pl) {
2932 sys::swapByteOrder(pl.entsize);
2933 sys::swapByteOrder(pl.count);
2936 inline void swapStruct(struct objc_property_list32 &pl) {
2937 sys::swapByteOrder(pl.entsize);
2938 sys::swapByteOrder(pl.count);
2941 inline void swapStruct(struct objc_property64 &op) {
2942 sys::swapByteOrder(op.name);
2943 sys::swapByteOrder(op.attributes);
2946 inline void swapStruct(struct objc_property32 &op) {
2947 sys::swapByteOrder(op.name);
2948 sys::swapByteOrder(op.attributes);
2951 inline void swapStruct(struct category64_t &c) {
2952 sys::swapByteOrder(c.name);
2953 sys::swapByteOrder(c.cls);
2954 sys::swapByteOrder(c.instanceMethods);
2955 sys::swapByteOrder(c.classMethods);
2956 sys::swapByteOrder(c.protocols);
2957 sys::swapByteOrder(c.instanceProperties);
2960 inline void swapStruct(struct category32_t &c) {
2961 sys::swapByteOrder(c.name);
2962 sys::swapByteOrder(c.cls);
2963 sys::swapByteOrder(c.instanceMethods);
2964 sys::swapByteOrder(c.classMethods);
2965 sys::swapByteOrder(c.protocols);
2966 sys::swapByteOrder(c.instanceProperties);
2969 inline void swapStruct(struct objc_image_info64 &o) {
2970 sys::swapByteOrder(o.version);
2971 sys::swapByteOrder(o.flags);
2974 inline void swapStruct(struct objc_image_info32 &o) {
2975 sys::swapByteOrder(o.version);
2976 sys::swapByteOrder(o.flags);
2979 inline void swapStruct(struct imageInfo_t &o) {
2980 sys::swapByteOrder(o.version);
2981 sys::swapByteOrder(o.flags);
2984 inline void swapStruct(struct message_ref64 &mr) {
2985 sys::swapByteOrder(mr.imp);
2986 sys::swapByteOrder(mr.sel);
2989 inline void swapStruct(struct message_ref32 &mr) {
2990 sys::swapByteOrder(mr.imp);
2991 sys::swapByteOrder(mr.sel);
2994 inline void swapStruct(struct objc_module_t &module) {
2995 sys::swapByteOrder(module.version);
2996 sys::swapByteOrder(module.size);
2997 sys::swapByteOrder(module.name);
2998 sys::swapByteOrder(module.symtab);
3001 inline void swapStruct(struct objc_symtab_t &symtab) {
3002 sys::swapByteOrder(symtab.sel_ref_cnt);
3003 sys::swapByteOrder(symtab.refs);
3004 sys::swapByteOrder(symtab.cls_def_cnt);
3005 sys::swapByteOrder(symtab.cat_def_cnt);
3008 inline void swapStruct(struct objc_class_t &objc_class) {
3009 sys::swapByteOrder(objc_class.isa);
3010 sys::swapByteOrder(objc_class.super_class);
3011 sys::swapByteOrder(objc_class.name);
3012 sys::swapByteOrder(objc_class.version);
3013 sys::swapByteOrder(objc_class.info);
3014 sys::swapByteOrder(objc_class.instance_size);
3015 sys::swapByteOrder(objc_class.ivars);
3016 sys::swapByteOrder(objc_class.methodLists);
3017 sys::swapByteOrder(objc_class.cache);
3018 sys::swapByteOrder(objc_class.protocols);
3021 inline void swapStruct(struct objc_category_t &objc_category) {
3022 sys::swapByteOrder(objc_category.category_name);
3023 sys::swapByteOrder(objc_category.class_name);
3024 sys::swapByteOrder(objc_category.instance_methods);
3025 sys::swapByteOrder(objc_category.class_methods);
3026 sys::swapByteOrder(objc_category.protocols);
3029 inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3030 sys::swapByteOrder(objc_ivar_list.ivar_count);
3033 inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3034 sys::swapByteOrder(objc_ivar.ivar_name);
3035 sys::swapByteOrder(objc_ivar.ivar_type);
3036 sys::swapByteOrder(objc_ivar.ivar_offset);
3039 inline void swapStruct(struct objc_method_list_t &method_list) {
3040 sys::swapByteOrder(method_list.obsolete);
3041 sys::swapByteOrder(method_list.method_count);
3044 inline void swapStruct(struct objc_method_t &method) {
3045 sys::swapByteOrder(method.method_name);
3046 sys::swapByteOrder(method.method_types);
3047 sys::swapByteOrder(method.method_imp);
3050 inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3051 sys::swapByteOrder(protocol_list.next);
3052 sys::swapByteOrder(protocol_list.count);
3055 inline void swapStruct(struct objc_protocol_t &protocol) {
3056 sys::swapByteOrder(protocol.isa);
3057 sys::swapByteOrder(protocol.protocol_name);
3058 sys::swapByteOrder(protocol.protocol_list);
3059 sys::swapByteOrder(protocol.instance_methods);
3060 sys::swapByteOrder(protocol.class_methods);
3063 inline void swapStruct(struct objc_method_description_list_t &mdl) {
3064 sys::swapByteOrder(mdl.count);
3067 inline void swapStruct(struct objc_method_description_t &md) {
3068 sys::swapByteOrder(md.name);
3069 sys::swapByteOrder(md.types);
3072 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3073 struct DisassembleInfo *info);
3075 // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3076 // to an Objective-C class and returns the class name. It is also passed the
3077 // address of the pointer, so when the pointer is zero as it can be in an .o
3078 // file, that is used to look for an external relocation entry with a symbol
3080 static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3081 uint64_t ReferenceValue,
3082 struct DisassembleInfo *info) {
3084 uint32_t offset, left;
3087 // The pointer_value can be 0 in an object file and have a relocation
3088 // entry for the class symbol at the ReferenceValue (the address of the
3090 if (pointer_value == 0) {
3091 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3092 if (r == nullptr || left < sizeof(uint64_t))
3095 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3096 if (symbol_name == nullptr)
3098 const char *class_name = strrchr(symbol_name, '$');
3099 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3100 return class_name + 2;
3105 // The case were the pointer_value is non-zero and points to a class defined
3106 // in this Mach-O file.
3107 r = get_pointer_64(pointer_value, offset, left, S, info);
3108 if (r == nullptr || left < sizeof(struct class64_t))
3111 memcpy(&c, r, sizeof(struct class64_t));
3112 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3116 r = get_pointer_64(c.data, offset, left, S, info);
3117 if (r == nullptr || left < sizeof(struct class_ro64_t))
3119 struct class_ro64_t cro;
3120 memcpy(&cro, r, sizeof(struct class_ro64_t));
3121 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3125 const char *name = get_pointer_64(cro.name, offset, left, S, info);
3129 // get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3130 // pointer to a cfstring and returns its name or nullptr.
3131 static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3132 struct DisassembleInfo *info) {
3133 const char *r, *name;
3134 uint32_t offset, left;
3136 struct cfstring64_t cfs;
3137 uint64_t cfs_characters;
3139 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3140 if (r == nullptr || left < sizeof(struct cfstring64_t))
3142 memcpy(&cfs, r, sizeof(struct cfstring64_t));
3143 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3145 if (cfs.characters == 0) {
3147 const char *symbol_name = get_symbol_64(
3148 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
3149 if (symbol_name == nullptr)
3151 cfs_characters = n_value;
3153 cfs_characters = cfs.characters;
3154 name = get_pointer_64(cfs_characters, offset, left, S, info);
3159 // get_objc2_64bit_selref() is used for disassembly and is passed a the address
3160 // of a pointer to an Objective-C selector reference when the pointer value is
3161 // zero as in a .o file and is likely to have a external relocation entry with
3162 // who's symbol's n_value is the real pointer to the selector name. If that is
3163 // the case the real pointer to the selector name is returned else 0 is
3165 static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3166 struct DisassembleInfo *info) {
3167 uint32_t offset, left;
3170 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3171 if (r == nullptr || left < sizeof(uint64_t))
3174 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3175 if (symbol_name == nullptr)
3180 static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3181 const char *sectname) {
3182 for (const SectionRef &Section : O->sections()) {
3184 Section.getName(SectName);
3185 DataRefImpl Ref = Section.getRawDataRefImpl();
3186 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3187 if (SegName == segname && SectName == sectname)
3190 return SectionRef();
3194 walk_pointer_list_64(const char *listname, const SectionRef S,
3195 MachOObjectFile *O, struct DisassembleInfo *info,
3196 void (*func)(uint64_t, struct DisassembleInfo *info)) {
3197 if (S == SectionRef())
3201 S.getName(SectName);
3202 DataRefImpl Ref = S.getRawDataRefImpl();
3203 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3204 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3207 S.getContents(BytesStr);
3208 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3210 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3211 uint32_t left = S.getSize() - i;
3212 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3214 memcpy(&p, Contents + i, size);
3215 if (i + sizeof(uint64_t) > S.getSize())
3216 outs() << listname << " list pointer extends past end of (" << SegName
3217 << "," << SectName << ") section\n";
3218 outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
3220 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3221 sys::swapByteOrder(p);
3223 uint64_t n_value = 0;
3224 const char *name = get_symbol_64(i, S, info, n_value, p);
3225 if (name == nullptr)
3226 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
3229 outs() << format("0x%" PRIx64, n_value);
3231 outs() << " + " << format("0x%" PRIx64, p);
3233 outs() << format("0x%" PRIx64, p);
3234 if (name != nullptr)
3235 outs() << " " << name;
3245 walk_pointer_list_32(const char *listname, const SectionRef S,
3246 MachOObjectFile *O, struct DisassembleInfo *info,
3247 void (*func)(uint32_t, struct DisassembleInfo *info)) {
3248 if (S == SectionRef())
3252 S.getName(SectName);
3253 DataRefImpl Ref = S.getRawDataRefImpl();
3254 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3255 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3258 S.getContents(BytesStr);
3259 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3261 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
3262 uint32_t left = S.getSize() - i;
3263 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
3265 memcpy(&p, Contents + i, size);
3266 if (i + sizeof(uint32_t) > S.getSize())
3267 outs() << listname << " list pointer extends past end of (" << SegName
3268 << "," << SectName << ") section\n";
3269 uint32_t Address = S.getAddress() + i;
3270 outs() << format("%08" PRIx32, Address) << " ";
3272 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3273 sys::swapByteOrder(p);
3274 outs() << format("0x%" PRIx32, p);
3276 const char *name = get_symbol_32(i, S, info, p);
3277 if (name != nullptr)
3278 outs() << " " << name;
3286 static void print_layout_map(const char *layout_map, uint32_t left) {
3287 outs() << " layout map: ";
3289 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
3292 } while (*layout_map != '\0' && left != 0);
3296 static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
3297 uint32_t offset, left;
3299 const char *layout_map;
3303 layout_map = get_pointer_64(p, offset, left, S, info);
3304 print_layout_map(layout_map, left);
3307 static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
3308 uint32_t offset, left;
3310 const char *layout_map;
3314 layout_map = get_pointer_32(p, offset, left, S, info);
3315 print_layout_map(layout_map, left);
3318 static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
3319 const char *indent) {
3320 struct method_list64_t ml;
3321 struct method64_t m;
3323 uint32_t offset, xoffset, left, i;
3325 const char *name, *sym_name;
3328 r = get_pointer_64(p, offset, left, S, info);
3331 memset(&ml, '\0', sizeof(struct method_list64_t));
3332 if (left < sizeof(struct method_list64_t)) {
3333 memcpy(&ml, r, left);
3334 outs() << " (method_list_t entends past the end of the section)\n";
3336 memcpy(&ml, r, sizeof(struct method_list64_t));
3337 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3339 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3340 outs() << indent << "\t\t count " << ml.count << "\n";
3342 p += sizeof(struct method_list64_t);
3343 offset += sizeof(struct method_list64_t);
3344 for (i = 0; i < ml.count; i++) {
3345 r = get_pointer_64(p, offset, left, S, info);
3348 memset(&m, '\0', sizeof(struct method64_t));
3349 if (left < sizeof(struct method64_t)) {
3350 memcpy(&ml, r, left);
3351 outs() << indent << " (method_t entends past the end of the section)\n";
3353 memcpy(&m, r, sizeof(struct method64_t));
3354 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3357 outs() << indent << "\t\t name ";
3358 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
3359 info, n_value, m.name);
3361 if (info->verbose && sym_name != nullptr)
3364 outs() << format("0x%" PRIx64, n_value);
3366 outs() << " + " << format("0x%" PRIx64, m.name);
3368 outs() << format("0x%" PRIx64, m.name);
3369 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
3370 if (name != nullptr)
3371 outs() << format(" %.*s", left, name);
3374 outs() << indent << "\t\t types ";
3375 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
3376 info, n_value, m.types);
3378 if (info->verbose && sym_name != nullptr)
3381 outs() << format("0x%" PRIx64, n_value);
3383 outs() << " + " << format("0x%" PRIx64, m.types);
3385 outs() << format("0x%" PRIx64, m.types);
3386 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
3387 if (name != nullptr)
3388 outs() << format(" %.*s", left, name);
3391 outs() << indent << "\t\t imp ";
3392 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
3394 if (info->verbose && name == nullptr) {
3396 outs() << format("0x%" PRIx64, n_value) << " ";
3398 outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
3400 outs() << format("0x%" PRIx64, m.imp) << " ";
3402 if (name != nullptr)
3406 p += sizeof(struct method64_t);
3407 offset += sizeof(struct method64_t);
3411 static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
3412 const char *indent) {
3413 struct method_list32_t ml;
3414 struct method32_t m;
3415 const char *r, *name;
3416 uint32_t offset, xoffset, left, i;
3419 r = get_pointer_32(p, offset, left, S, info);
3422 memset(&ml, '\0', sizeof(struct method_list32_t));
3423 if (left < sizeof(struct method_list32_t)) {
3424 memcpy(&ml, r, left);
3425 outs() << " (method_list_t entends past the end of the section)\n";
3427 memcpy(&ml, r, sizeof(struct method_list32_t));
3428 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3430 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3431 outs() << indent << "\t\t count " << ml.count << "\n";
3433 p += sizeof(struct method_list32_t);
3434 offset += sizeof(struct method_list32_t);
3435 for (i = 0; i < ml.count; i++) {
3436 r = get_pointer_32(p, offset, left, S, info);
3439 memset(&m, '\0', sizeof(struct method32_t));
3440 if (left < sizeof(struct method32_t)) {
3441 memcpy(&ml, r, left);
3442 outs() << indent << " (method_t entends past the end of the section)\n";
3444 memcpy(&m, r, sizeof(struct method32_t));
3445 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3448 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
3449 name = get_pointer_32(m.name, xoffset, left, xS, info);
3450 if (name != nullptr)
3451 outs() << format(" %.*s", left, name);
3454 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
3455 name = get_pointer_32(m.types, xoffset, left, xS, info);
3456 if (name != nullptr)
3457 outs() << format(" %.*s", left, name);
3460 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
3461 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
3463 if (name != nullptr)
3464 outs() << " " << name;
3467 p += sizeof(struct method32_t);
3468 offset += sizeof(struct method32_t);
3472 static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
3473 uint32_t offset, left, xleft;
3475 struct objc_method_list_t method_list;
3476 struct objc_method_t method;
3477 const char *r, *methods, *name, *SymbolName;
3480 r = get_pointer_32(p, offset, left, S, info, true);
3485 if (left > sizeof(struct objc_method_list_t)) {
3486 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
3488 outs() << "\t\t objc_method_list extends past end of the section\n";
3489 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
3490 memcpy(&method_list, r, left);
3492 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3493 swapStruct(method_list);
3495 outs() << "\t\t obsolete "
3496 << format("0x%08" PRIx32, method_list.obsolete) << "\n";
3497 outs() << "\t\t method_count " << method_list.method_count << "\n";
3499 methods = r + sizeof(struct objc_method_list_t);
3500 for (i = 0; i < method_list.method_count; i++) {
3501 if ((i + 1) * sizeof(struct objc_method_t) > left) {
3502 outs() << "\t\t remaining method's extend past the of the section\n";
3505 memcpy(&method, methods + i * sizeof(struct objc_method_t),
3506 sizeof(struct objc_method_t));
3507 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3510 outs() << "\t\t method_name "
3511 << format("0x%08" PRIx32, method.method_name);
3512 if (info->verbose) {
3513 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
3514 if (name != nullptr)
3515 outs() << format(" %.*s", xleft, name);
3517 outs() << " (not in an __OBJC section)";
3521 outs() << "\t\t method_types "
3522 << format("0x%08" PRIx32, method.method_types);
3523 if (info->verbose) {
3524 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
3525 if (name != nullptr)
3526 outs() << format(" %.*s", xleft, name);
3528 outs() << " (not in an __OBJC section)";
3532 outs() << "\t\t method_imp "
3533 << format("0x%08" PRIx32, method.method_imp) << " ";
3534 if (info->verbose) {
3535 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
3536 if (SymbolName != nullptr)
3537 outs() << SymbolName;
3544 static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
3545 struct protocol_list64_t pl;
3546 uint64_t q, n_value;
3547 struct protocol64_t pc;
3549 uint32_t offset, xoffset, left, i;
3551 const char *name, *sym_name;
3553 r = get_pointer_64(p, offset, left, S, info);
3556 memset(&pl, '\0', sizeof(struct protocol_list64_t));
3557 if (left < sizeof(struct protocol_list64_t)) {
3558 memcpy(&pl, r, left);
3559 outs() << " (protocol_list_t entends past the end of the section)\n";
3561 memcpy(&pl, r, sizeof(struct protocol_list64_t));
3562 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3564 outs() << " count " << pl.count << "\n";
3566 p += sizeof(struct protocol_list64_t);
3567 offset += sizeof(struct protocol_list64_t);
3568 for (i = 0; i < pl.count; i++) {
3569 r = get_pointer_64(p, offset, left, S, info);
3573 if (left < sizeof(uint64_t)) {
3574 memcpy(&q, r, left);
3575 outs() << " (protocol_t * entends past the end of the section)\n";
3577 memcpy(&q, r, sizeof(uint64_t));
3578 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3579 sys::swapByteOrder(q);
3581 outs() << "\t\t list[" << i << "] ";
3582 sym_name = get_symbol_64(offset, S, info, n_value, q);
3584 if (info->verbose && sym_name != nullptr)
3587 outs() << format("0x%" PRIx64, n_value);
3589 outs() << " + " << format("0x%" PRIx64, q);
3591 outs() << format("0x%" PRIx64, q);
3592 outs() << " (struct protocol_t *)\n";
3594 r = get_pointer_64(q + n_value, offset, left, S, info);
3597 memset(&pc, '\0', sizeof(struct protocol64_t));
3598 if (left < sizeof(struct protocol64_t)) {
3599 memcpy(&pc, r, left);
3600 outs() << " (protocol_t entends past the end of the section)\n";
3602 memcpy(&pc, r, sizeof(struct protocol64_t));
3603 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3606 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
3608 outs() << "\t\t\t name ";
3609 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
3610 info, n_value, pc.name);
3612 if (info->verbose && sym_name != nullptr)
3615 outs() << format("0x%" PRIx64, n_value);
3617 outs() << " + " << format("0x%" PRIx64, pc.name);
3619 outs() << format("0x%" PRIx64, pc.name);
3620 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
3621 if (name != nullptr)
3622 outs() << format(" %.*s", left, name);
3625 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
3627 outs() << "\t\t instanceMethods ";
3629 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
3630 S, info, n_value, pc.instanceMethods);
3632 if (info->verbose && sym_name != nullptr)
3635 outs() << format("0x%" PRIx64, n_value);
3636 if (pc.instanceMethods != 0)
3637 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
3639 outs() << format("0x%" PRIx64, pc.instanceMethods);
3640 outs() << " (struct method_list_t *)\n";
3641 if (pc.instanceMethods + n_value != 0)
3642 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
3644 outs() << "\t\t classMethods ";
3646 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
3647 info, n_value, pc.classMethods);
3649 if (info->verbose && sym_name != nullptr)
3652 outs() << format("0x%" PRIx64, n_value);
3653 if (pc.classMethods != 0)
3654 outs() << " + " << format("0x%" PRIx64, pc.classMethods);
3656 outs() << format("0x%" PRIx64, pc.classMethods);
3657 outs() << " (struct method_list_t *)\n";
3658 if (pc.classMethods + n_value != 0)
3659 print_method_list64_t(pc.classMethods + n_value, info, "\t");
3661 outs() << "\t optionalInstanceMethods "
3662 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
3663 outs() << "\t optionalClassMethods "
3664 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
3665 outs() << "\t instanceProperties "
3666 << format("0x%" PRIx64, pc.instanceProperties) << "\n";
3668 p += sizeof(uint64_t);
3669 offset += sizeof(uint64_t);
3673 static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
3674 struct protocol_list32_t pl;
3676 struct protocol32_t pc;
3678 uint32_t offset, xoffset, left, i;
3682 r = get_pointer_32(p, offset, left, S, info);
3685 memset(&pl, '\0', sizeof(struct protocol_list32_t));
3686 if (left < sizeof(struct protocol_list32_t)) {
3687 memcpy(&pl, r, left);
3688 outs() << " (protocol_list_t entends past the end of the section)\n";
3690 memcpy(&pl, r, sizeof(struct protocol_list32_t));
3691 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3693 outs() << " count " << pl.count << "\n";
3695 p += sizeof(struct protocol_list32_t);
3696 offset += sizeof(struct protocol_list32_t);
3697 for (i = 0; i < pl.count; i++) {
3698 r = get_pointer_32(p, offset, left, S, info);
3702 if (left < sizeof(uint32_t)) {
3703 memcpy(&q, r, left);
3704 outs() << " (protocol_t * entends past the end of the section)\n";
3706 memcpy(&q, r, sizeof(uint32_t));
3707 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3708 sys::swapByteOrder(q);
3709 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
3710 << " (struct protocol_t *)\n";
3711 r = get_pointer_32(q, offset, left, S, info);
3714 memset(&pc, '\0', sizeof(struct protocol32_t));
3715 if (left < sizeof(struct protocol32_t)) {
3716 memcpy(&pc, r, left);
3717 outs() << " (protocol_t entends past the end of the section)\n";
3719 memcpy(&pc, r, sizeof(struct protocol32_t));
3720 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3722 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
3723 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
3724 name = get_pointer_32(pc.name, xoffset, left, xS, info);
3725 if (name != nullptr)
3726 outs() << format(" %.*s", left, name);
3728 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
3729 outs() << "\t\t instanceMethods "
3730 << format("0x%" PRIx32, pc.instanceMethods)
3731 << " (struct method_list_t *)\n";
3732 if (pc.instanceMethods != 0)
3733 print_method_list32_t(pc.instanceMethods, info, "\t");
3734 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
3735 << " (struct method_list_t *)\n";
3736 if (pc.classMethods != 0)
3737 print_method_list32_t(pc.classMethods, info, "\t");
3738 outs() << "\t optionalInstanceMethods "
3739 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
3740 outs() << "\t optionalClassMethods "
3741 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
3742 outs() << "\t instanceProperties "
3743 << format("0x%" PRIx32, pc.instanceProperties) << "\n";
3744 p += sizeof(uint32_t);
3745 offset += sizeof(uint32_t);
3749 static void print_indent(uint32_t indent) {
3750 for (uint32_t i = 0; i < indent;) {
3751 if (indent - i >= 8) {
3755 for (uint32_t j = i; j < indent; j++)
3762 static bool print_method_description_list(uint32_t p, uint32_t indent,
3763 struct DisassembleInfo *info) {
3764 uint32_t offset, left, xleft;
3766 struct objc_method_description_list_t mdl;
3767 struct objc_method_description_t md;
3768 const char *r, *list, *name;
3771 r = get_pointer_32(p, offset, left, S, info, true);
3776 if (left > sizeof(struct objc_method_description_list_t)) {
3777 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
3779 print_indent(indent);
3780 outs() << " objc_method_description_list extends past end of the section\n";
3781 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
3782 memcpy(&mdl, r, left);
3784 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3787 print_indent(indent);
3788 outs() << " count " << mdl.count << "\n";
3790 list = r + sizeof(struct objc_method_description_list_t);
3791 for (i = 0; i < mdl.count; i++) {
3792 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
3793 print_indent(indent);
3794 outs() << " remaining list entries extend past the of the section\n";
3797 print_indent(indent);
3798 outs() << " list[" << i << "]\n";
3799 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
3800 sizeof(struct objc_method_description_t));
3801 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3804 print_indent(indent);
3805 outs() << " name " << format("0x%08" PRIx32, md.name);
3806 if (info->verbose) {
3807 name = get_pointer_32(md.name, offset, xleft, S, info, true);
3808 if (name != nullptr)
3809 outs() << format(" %.*s", xleft, name);
3811 outs() << " (not in an __OBJC section)";
3815 print_indent(indent);
3816 outs() << " types " << format("0x%08" PRIx32, md.types);
3817 if (info->verbose) {
3818 name = get_pointer_32(md.types, offset, xleft, S, info, true);
3819 if (name != nullptr)
3820 outs() << format(" %.*s", xleft, name);
3822 outs() << " (not in an __OBJC section)";
3829 static bool print_protocol_list(uint32_t p, uint32_t indent,
3830 struct DisassembleInfo *info);
3832 static bool print_protocol(uint32_t p, uint32_t indent,
3833 struct DisassembleInfo *info) {
3834 uint32_t offset, left;
3836 struct objc_protocol_t protocol;
3837 const char *r, *name;
3839 r = get_pointer_32(p, offset, left, S, info, true);
3844 if (left >= sizeof(struct objc_protocol_t)) {
3845 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
3847 print_indent(indent);
3848 outs() << " Protocol extends past end of the section\n";
3849 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
3850 memcpy(&protocol, r, left);
3852 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3853 swapStruct(protocol);
3855 print_indent(indent);
3856 outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
3859 print_indent(indent);
3860 outs() << " protocol_name "
3861 << format("0x%08" PRIx32, protocol.protocol_name);
3862 if (info->verbose) {
3863 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
3864 if (name != nullptr)
3865 outs() << format(" %.*s", left, name);
3867 outs() << " (not in an __OBJC section)";
3871 print_indent(indent);
3872 outs() << " protocol_list "
3873 << format("0x%08" PRIx32, protocol.protocol_list);
3874 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
3875 outs() << " (not in an __OBJC section)\n";
3877 print_indent(indent);
3878 outs() << " instance_methods "
3879 << format("0x%08" PRIx32, protocol.instance_methods);
3880 if (print_method_description_list(protocol.instance_methods, indent, info))
3881 outs() << " (not in an __OBJC section)\n";
3883 print_indent(indent);
3884 outs() << " class_methods "
3885 << format("0x%08" PRIx32, protocol.class_methods);
3886 if (print_method_description_list(protocol.class_methods, indent, info))
3887 outs() << " (not in an __OBJC section)\n";
3892 static bool print_protocol_list(uint32_t p, uint32_t indent,
3893 struct DisassembleInfo *info) {
3894 uint32_t offset, left, l;
3896 struct objc_protocol_list_t protocol_list;
3897 const char *r, *list;
3900 r = get_pointer_32(p, offset, left, S, info, true);
3905 if (left > sizeof(struct objc_protocol_list_t)) {
3906 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
3908 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
3909 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
3910 memcpy(&protocol_list, r, left);
3912 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3913 swapStruct(protocol_list);
3915 print_indent(indent);
3916 outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
3918 print_indent(indent);
3919 outs() << " count " << protocol_list.count << "\n";
3921 list = r + sizeof(struct objc_protocol_list_t);
3922 for (i = 0; i < protocol_list.count; i++) {
3923 if ((i + 1) * sizeof(uint32_t) > left) {
3924 outs() << "\t\t remaining list entries extend past the of the section\n";
3927 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
3928 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3929 sys::swapByteOrder(l);
3931 print_indent(indent);
3932 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
3933 if (print_protocol(l, indent, info))
3934 outs() << "(not in an __OBJC section)\n";
3939 static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
3940 struct ivar_list64_t il;
3943 uint32_t offset, xoffset, left, j;
3945 const char *name, *sym_name, *ivar_offset_p;
3946 uint64_t ivar_offset, n_value;
3948 r = get_pointer_64(p, offset, left, S, info);
3951 memset(&il, '\0', sizeof(struct ivar_list64_t));
3952 if (left < sizeof(struct ivar_list64_t)) {
3953 memcpy(&il, r, left);
3954 outs() << " (ivar_list_t entends past the end of the section)\n";
3956 memcpy(&il, r, sizeof(struct ivar_list64_t));
3957 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3959 outs() << " entsize " << il.entsize << "\n";
3960 outs() << " count " << il.count << "\n";
3962 p += sizeof(struct ivar_list64_t);
3963 offset += sizeof(struct ivar_list64_t);
3964 for (j = 0; j < il.count; j++) {
3965 r = get_pointer_64(p, offset, left, S, info);
3968 memset(&i, '\0', sizeof(struct ivar64_t));
3969 if (left < sizeof(struct ivar64_t)) {
3970 memcpy(&i, r, left);
3971 outs() << " (ivar_t entends past the end of the section)\n";
3973 memcpy(&i, r, sizeof(struct ivar64_t));
3974 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3977 outs() << "\t\t\t offset ";
3978 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
3979 info, n_value, i.offset);
3981 if (info->verbose && sym_name != nullptr)
3984 outs() << format("0x%" PRIx64, n_value);
3986 outs() << " + " << format("0x%" PRIx64, i.offset);
3988 outs() << format("0x%" PRIx64, i.offset);
3989 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
3990 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
3991 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
3992 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3993 sys::swapByteOrder(ivar_offset);
3994 outs() << " " << ivar_offset << "\n";
3998 outs() << "\t\t\t name ";
3999 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
4002 if (info->verbose && sym_name != nullptr)
4005 outs() << format("0x%" PRIx64, n_value);
4007 outs() << " + " << format("0x%" PRIx64, i.name);
4009 outs() << format("0x%" PRIx64, i.name);
4010 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
4011 if (name != nullptr)
4012 outs() << format(" %.*s", left, name);
4015 outs() << "\t\t\t type ";
4016 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
4018 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
4020 if (info->verbose && sym_name != nullptr)
4023 outs() << format("0x%" PRIx64, n_value);
4025 outs() << " + " << format("0x%" PRIx64, i.type);
4027 outs() << format("0x%" PRIx64, i.type);
4028 if (name != nullptr)
4029 outs() << format(" %.*s", left, name);
4032 outs() << "\t\t\talignment " << i.alignment << "\n";
4033 outs() << "\t\t\t size " << i.size << "\n";
4035 p += sizeof(struct ivar64_t);
4036 offset += sizeof(struct ivar64_t);
4040 static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
4041 struct ivar_list32_t il;
4044 uint32_t offset, xoffset, left, j;
4046 const char *name, *ivar_offset_p;
4047 uint32_t ivar_offset;
4049 r = get_pointer_32(p, offset, left, S, info);
4052 memset(&il, '\0', sizeof(struct ivar_list32_t));
4053 if (left < sizeof(struct ivar_list32_t)) {
4054 memcpy(&il, r, left);
4055 outs() << " (ivar_list_t entends past the end of the section)\n";
4057 memcpy(&il, r, sizeof(struct ivar_list32_t));
4058 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4060 outs() << " entsize " << il.entsize << "\n";
4061 outs() << " count " << il.count << "\n";
4063 p += sizeof(struct ivar_list32_t);
4064 offset += sizeof(struct ivar_list32_t);
4065 for (j = 0; j < il.count; j++) {
4066 r = get_pointer_32(p, offset, left, S, info);
4069 memset(&i, '\0', sizeof(struct ivar32_t));
4070 if (left < sizeof(struct ivar32_t)) {
4071 memcpy(&i, r, left);
4072 outs() << " (ivar_t entends past the end of the section)\n";
4074 memcpy(&i, r, sizeof(struct ivar32_t));
4075 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4078 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
4079 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4080 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4081 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4082 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4083 sys::swapByteOrder(ivar_offset);
4084 outs() << " " << ivar_offset << "\n";
4088 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
4089 name = get_pointer_32(i.name, xoffset, left, xS, info);
4090 if (name != nullptr)
4091 outs() << format(" %.*s", left, name);
4094 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
4095 name = get_pointer_32(i.type, xoffset, left, xS, info);
4096 if (name != nullptr)
4097 outs() << format(" %.*s", left, name);
4100 outs() << "\t\t\talignment " << i.alignment << "\n";
4101 outs() << "\t\t\t size " << i.size << "\n";
4103 p += sizeof(struct ivar32_t);
4104 offset += sizeof(struct ivar32_t);
4108 static void print_objc_property_list64(uint64_t p,
4109 struct DisassembleInfo *info) {
4110 struct objc_property_list64 opl;
4111 struct objc_property64 op;
4113 uint32_t offset, xoffset, left, j;
4115 const char *name, *sym_name;
4118 r = get_pointer_64(p, offset, left, S, info);
4121 memset(&opl, '\0', sizeof(struct objc_property_list64));
4122 if (left < sizeof(struct objc_property_list64)) {
4123 memcpy(&opl, r, left);
4124 outs() << " (objc_property_list entends past the end of the section)\n";
4126 memcpy(&opl, r, sizeof(struct objc_property_list64));
4127 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4129 outs() << " entsize " << opl.entsize << "\n";
4130 outs() << " count " << opl.count << "\n";
4132 p += sizeof(struct objc_property_list64);
4133 offset += sizeof(struct objc_property_list64);
4134 for (j = 0; j < opl.count; j++) {
4135 r = get_pointer_64(p, offset, left, S, info);
4138 memset(&op, '\0', sizeof(struct objc_property64));
4139 if (left < sizeof(struct objc_property64)) {
4140 memcpy(&op, r, left);
4141 outs() << " (objc_property entends past the end of the section)\n";
4143 memcpy(&op, r, sizeof(struct objc_property64));
4144 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4147 outs() << "\t\t\t name ";
4148 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
4149 info, n_value, op.name);
4151 if (info->verbose && sym_name != nullptr)
4154 outs() << format("0x%" PRIx64, n_value);
4156 outs() << " + " << format("0x%" PRIx64, op.name);
4158 outs() << format("0x%" PRIx64, op.name);
4159 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
4160 if (name != nullptr)
4161 outs() << format(" %.*s", left, name);
4164 outs() << "\t\t\tattributes ";
4166 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
4167 info, n_value, op.attributes);
4169 if (info->verbose && sym_name != nullptr)
4172 outs() << format("0x%" PRIx64, n_value);
4173 if (op.attributes != 0)
4174 outs() << " + " << format("0x%" PRIx64, op.attributes);
4176 outs() << format("0x%" PRIx64, op.attributes);
4177 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
4178 if (name != nullptr)
4179 outs() << format(" %.*s", left, name);
4182 p += sizeof(struct objc_property64);
4183 offset += sizeof(struct objc_property64);
4187 static void print_objc_property_list32(uint32_t p,
4188 struct DisassembleInfo *info) {
4189 struct objc_property_list32 opl;
4190 struct objc_property32 op;
4192 uint32_t offset, xoffset, left, j;
4196 r = get_pointer_32(p, offset, left, S, info);
4199 memset(&opl, '\0', sizeof(struct objc_property_list32));
4200 if (left < sizeof(struct objc_property_list32)) {
4201 memcpy(&opl, r, left);
4202 outs() << " (objc_property_list entends past the end of the section)\n";
4204 memcpy(&opl, r, sizeof(struct objc_property_list32));
4205 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4207 outs() << " entsize " << opl.entsize << "\n";
4208 outs() << " count " << opl.count << "\n";
4210 p += sizeof(struct objc_property_list32);
4211 offset += sizeof(struct objc_property_list32);
4212 for (j = 0; j < opl.count; j++) {
4213 r = get_pointer_32(p, offset, left, S, info);
4216 memset(&op, '\0', sizeof(struct objc_property32));
4217 if (left < sizeof(struct objc_property32)) {
4218 memcpy(&op, r, left);
4219 outs() << " (objc_property entends past the end of the section)\n";
4221 memcpy(&op, r, sizeof(struct objc_property32));
4222 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4225 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
4226 name = get_pointer_32(op.name, xoffset, left, xS, info);
4227 if (name != nullptr)
4228 outs() << format(" %.*s", left, name);
4231 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
4232 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
4233 if (name != nullptr)
4234 outs() << format(" %.*s", left, name);
4237 p += sizeof(struct objc_property32);
4238 offset += sizeof(struct objc_property32);
4242 static void print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
4243 bool &is_meta_class) {
4244 struct class_ro64_t cro;
4246 uint32_t offset, xoffset, left;
4248 const char *name, *sym_name;
4251 r = get_pointer_64(p, offset, left, S, info);
4252 if (r == nullptr || left < sizeof(struct class_ro64_t))
4254 memset(&cro, '\0', sizeof(struct class_ro64_t));
4255 if (left < sizeof(struct class_ro64_t)) {
4256 memcpy(&cro, r, left);
4257 outs() << " (class_ro_t entends past the end of the section)\n";
4259 memcpy(&cro, r, sizeof(struct class_ro64_t));
4260 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4262 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4263 if (cro.flags & RO_META)
4264 outs() << " RO_META";
4265 if (cro.flags & RO_ROOT)
4266 outs() << " RO_ROOT";
4267 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4268 outs() << " RO_HAS_CXX_STRUCTORS";
4270 outs() << " instanceStart " << cro.instanceStart << "\n";
4271 outs() << " instanceSize " << cro.instanceSize << "\n";
4272 outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
4274 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
4276 print_layout_map64(cro.ivarLayout, info);
4279 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
4280 info, n_value, cro.name);
4282 if (info->verbose && sym_name != nullptr)
4285 outs() << format("0x%" PRIx64, n_value);
4287 outs() << " + " << format("0x%" PRIx64, cro.name);
4289 outs() << format("0x%" PRIx64, cro.name);
4290 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
4291 if (name != nullptr)
4292 outs() << format(" %.*s", left, name);
4295 outs() << " baseMethods ";
4296 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
4297 S, info, n_value, cro.baseMethods);
4299 if (info->verbose && sym_name != nullptr)
4302 outs() << format("0x%" PRIx64, n_value);
4303 if (cro.baseMethods != 0)
4304 outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
4306 outs() << format("0x%" PRIx64, cro.baseMethods);
4307 outs() << " (struct method_list_t *)\n";
4308 if (cro.baseMethods + n_value != 0)
4309 print_method_list64_t(cro.baseMethods + n_value, info, "");
4311 outs() << " baseProtocols ";
4313 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
4314 info, n_value, cro.baseProtocols);
4316 if (info->verbose && sym_name != nullptr)
4319 outs() << format("0x%" PRIx64, n_value);
4320 if (cro.baseProtocols != 0)
4321 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
4323 outs() << format("0x%" PRIx64, cro.baseProtocols);
4325 if (cro.baseProtocols + n_value != 0)
4326 print_protocol_list64_t(cro.baseProtocols + n_value, info);
4328 outs() << " ivars ";
4329 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
4330 info, n_value, cro.ivars);
4332 if (info->verbose && sym_name != nullptr)
4335 outs() << format("0x%" PRIx64, n_value);
4337 outs() << " + " << format("0x%" PRIx64, cro.ivars);
4339 outs() << format("0x%" PRIx64, cro.ivars);
4341 if (cro.ivars + n_value != 0)
4342 print_ivar_list64_t(cro.ivars + n_value, info);
4344 outs() << " weakIvarLayout ";
4346 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
4347 info, n_value, cro.weakIvarLayout);
4349 if (info->verbose && sym_name != nullptr)
4352 outs() << format("0x%" PRIx64, n_value);
4353 if (cro.weakIvarLayout != 0)
4354 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
4356 outs() << format("0x%" PRIx64, cro.weakIvarLayout);
4358 print_layout_map64(cro.weakIvarLayout + n_value, info);
4360 outs() << " baseProperties ";
4362 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
4363 info, n_value, cro.baseProperties);
4365 if (info->verbose && sym_name != nullptr)
4368 outs() << format("0x%" PRIx64, n_value);
4369 if (cro.baseProperties != 0)
4370 outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
4372 outs() << format("0x%" PRIx64, cro.baseProperties);
4374 if (cro.baseProperties + n_value != 0)
4375 print_objc_property_list64(cro.baseProperties + n_value, info);
4377 is_meta_class = (cro.flags & RO_META) ? true : false;
4380 static void print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
4381 bool &is_meta_class) {
4382 struct class_ro32_t cro;
4384 uint32_t offset, xoffset, left;
4388 r = get_pointer_32(p, offset, left, S, info);
4391 memset(&cro, '\0', sizeof(struct class_ro32_t));
4392 if (left < sizeof(struct class_ro32_t)) {
4393 memcpy(&cro, r, left);
4394 outs() << " (class_ro_t entends past the end of the section)\n";
4396 memcpy(&cro, r, sizeof(struct class_ro32_t));
4397 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4399 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4400 if (cro.flags & RO_META)
4401 outs() << " RO_META";
4402 if (cro.flags & RO_ROOT)
4403 outs() << " RO_ROOT";
4404 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4405 outs() << " RO_HAS_CXX_STRUCTORS";
4407 outs() << " instanceStart " << cro.instanceStart << "\n";
4408 outs() << " instanceSize " << cro.instanceSize << "\n";
4409 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
4411 print_layout_map32(cro.ivarLayout, info);
4413 outs() << " name " << format("0x%" PRIx32, cro.name);
4414 name = get_pointer_32(cro.name, xoffset, left, xS, info);
4415 if (name != nullptr)
4416 outs() << format(" %.*s", left, name);
4419 outs() << " baseMethods "
4420 << format("0x%" PRIx32, cro.baseMethods)
4421 << " (struct method_list_t *)\n";
4422 if (cro.baseMethods != 0)
4423 print_method_list32_t(cro.baseMethods, info, "");
4425 outs() << " baseProtocols "
4426 << format("0x%" PRIx32, cro.baseProtocols) << "\n";
4427 if (cro.baseProtocols != 0)
4428 print_protocol_list32_t(cro.baseProtocols, info);
4429 outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
4432 print_ivar_list32_t(cro.ivars, info);
4433 outs() << " weakIvarLayout "
4434 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
4435 print_layout_map32(cro.weakIvarLayout, info);
4436 outs() << " baseProperties "
4437 << format("0x%" PRIx32, cro.baseProperties) << "\n";
4438 if (cro.baseProperties != 0)
4439 print_objc_property_list32(cro.baseProperties, info);
4440 is_meta_class = (cro.flags & RO_META) ? true : false;
4443 static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
4446 uint32_t offset, left;
4449 uint64_t isa_n_value, n_value;
4451 r = get_pointer_64(p, offset, left, S, info);
4452 if (r == nullptr || left < sizeof(struct class64_t))
4454 memset(&c, '\0', sizeof(struct class64_t));
4455 if (left < sizeof(struct class64_t)) {
4456 memcpy(&c, r, left);
4457 outs() << " (class_t entends past the end of the section)\n";
4459 memcpy(&c, r, sizeof(struct class64_t));
4460 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4463 outs() << " isa " << format("0x%" PRIx64, c.isa);
4464 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
4465 isa_n_value, c.isa);
4466 if (name != nullptr)
4467 outs() << " " << name;
4470 outs() << " superclass " << format("0x%" PRIx64, c.superclass);
4471 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
4472 n_value, c.superclass);
4473 if (name != nullptr)
4474 outs() << " " << name;
4477 outs() << " cache " << format("0x%" PRIx64, c.cache);
4478 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
4480 if (name != nullptr)
4481 outs() << " " << name;
4484 outs() << " vtable " << format("0x%" PRIx64, c.vtable);
4485 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
4487 if (name != nullptr)
4488 outs() << " " << name;
4491 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
4495 if (info->verbose && name != nullptr)
4498 outs() << format("0x%" PRIx64, n_value);
4500 outs() << " + " << format("0x%" PRIx64, c.data);
4502 outs() << format("0x%" PRIx64, c.data);
4503 outs() << " (struct class_ro_t *)";
4505 // This is a Swift class if some of the low bits of the pointer are set.
4506 if ((c.data + n_value) & 0x7)
4507 outs() << " Swift class";
4510 print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class);
4512 if (is_meta_class == false) {
4513 outs() << "Meta Class\n";
4514 print_class64_t(c.isa + isa_n_value, info);
4518 static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
4521 uint32_t offset, left;
4525 r = get_pointer_32(p, offset, left, S, info);
4528 memset(&c, '\0', sizeof(struct class32_t));
4529 if (left < sizeof(struct class32_t)) {
4530 memcpy(&c, r, left);
4531 outs() << " (class_t entends past the end of the section)\n";
4533 memcpy(&c, r, sizeof(struct class32_t));
4534 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4537 outs() << " isa " << format("0x%" PRIx32, c.isa);
4539 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
4540 if (name != nullptr)
4541 outs() << " " << name;
4544 outs() << " superclass " << format("0x%" PRIx32, c.superclass);
4545 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
4547 if (name != nullptr)
4548 outs() << " " << name;
4551 outs() << " cache " << format("0x%" PRIx32, c.cache);
4552 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
4554 if (name != nullptr)
4555 outs() << " " << name;
4558 outs() << " vtable " << format("0x%" PRIx32, c.vtable);
4559 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
4561 if (name != nullptr)
4562 outs() << " " << name;
4566 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
4567 outs() << " data " << format("0x%" PRIx32, c.data)
4568 << " (struct class_ro_t *)";
4570 // This is a Swift class if some of the low bits of the pointer are set.
4572 outs() << " Swift class";
4575 print_class_ro32_t(c.data & ~0x3, info, is_meta_class);
4577 if (is_meta_class == false) {
4578 outs() << "Meta Class\n";
4579 print_class32_t(c.isa, info);
4583 static void print_objc_class_t(struct objc_class_t *objc_class,
4584 struct DisassembleInfo *info) {
4585 uint32_t offset, left, xleft;
4586 const char *name, *p, *ivar_list;
4589 struct objc_ivar_list_t objc_ivar_list;
4590 struct objc_ivar_t ivar;
4592 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
4593 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
4594 name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
4595 if (name != nullptr)
4596 outs() << format(" %.*s", left, name);
4598 outs() << " (not in an __OBJC section)";
4602 outs() << "\t super_class "
4603 << format("0x%08" PRIx32, objc_class->super_class);
4604 if (info->verbose) {
4605 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
4606 if (name != nullptr)
4607 outs() << format(" %.*s", left, name);
4609 outs() << " (not in an __OBJC section)";
4613 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name);
4614 if (info->verbose) {
4615 name = get_pointer_32(objc_class->name, offset, left, S, info, true);
4616 if (name != nullptr)
4617 outs() << format(" %.*s", left, name);
4619 outs() << " (not in an __OBJC section)";
4623 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
4626 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
4627 if (info->verbose) {
4628 if (CLS_GETINFO(objc_class, CLS_CLASS))
4629 outs() << " CLS_CLASS";
4630 else if (CLS_GETINFO(objc_class, CLS_META))
4631 outs() << " CLS_META";
4635 outs() << "\t instance_size "
4636 << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
4638 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
4639 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
4641 if (left > sizeof(struct objc_ivar_list_t)) {
4643 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
4645 outs() << " (entends past the end of the section)\n";
4646 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
4647 memcpy(&objc_ivar_list, p, left);
4649 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4650 swapStruct(objc_ivar_list);
4651 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
4652 ivar_list = p + sizeof(struct objc_ivar_list_t);
4653 for (i = 0; i < objc_ivar_list.ivar_count; i++) {
4654 if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
4655 outs() << "\t\t remaining ivar's extend past the of the section\n";
4658 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
4659 sizeof(struct objc_ivar_t));
4660 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4663 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
4664 if (info->verbose) {
4665 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
4666 if (name != nullptr)
4667 outs() << format(" %.*s", xleft, name);
4669 outs() << " (not in an __OBJC section)";
4673 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
4674 if (info->verbose) {
4675 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
4676 if (name != nullptr)
4677 outs() << format(" %.*s", xleft, name);
4679 outs() << " (not in an __OBJC section)";
4683 outs() << "\t\t ivar_offset "
4684 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
4687 outs() << " (not in an __OBJC section)\n";
4690 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
4691 if (print_method_list(objc_class->methodLists, info))
4692 outs() << " (not in an __OBJC section)\n";
4694 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
4697 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
4698 if (print_protocol_list(objc_class->protocols, 16, info))
4699 outs() << " (not in an __OBJC section)\n";
4702 static void print_objc_objc_category_t(struct objc_category_t *objc_category,
4703 struct DisassembleInfo *info) {
4704 uint32_t offset, left;
4708 outs() << "\t category name "
4709 << format("0x%08" PRIx32, objc_category->category_name);
4710 if (info->verbose) {
4711 name = get_pointer_32(objc_category->category_name, offset, left, S, info,
4713 if (name != nullptr)
4714 outs() << format(" %.*s", left, name);
4716 outs() << " (not in an __OBJC section)";
4720 outs() << "\t\t class name "
4721 << format("0x%08" PRIx32, objc_category->class_name);
4722 if (info->verbose) {
4724 get_pointer_32(objc_category->class_name, offset, left, S, info, true);
4725 if (name != nullptr)
4726 outs() << format(" %.*s", left, name);
4728 outs() << " (not in an __OBJC section)";
4732 outs() << "\t instance methods "
4733 << format("0x%08" PRIx32, objc_category->instance_methods);
4734 if (print_method_list(objc_category->instance_methods, info))
4735 outs() << " (not in an __OBJC section)\n";
4737 outs() << "\t class methods "
4738 << format("0x%08" PRIx32, objc_category->class_methods);
4739 if (print_method_list(objc_category->class_methods, info))
4740 outs() << " (not in an __OBJC section)\n";
4743 static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
4744 struct category64_t c;
4746 uint32_t offset, xoffset, left;
4748 const char *name, *sym_name;
4751 r = get_pointer_64(p, offset, left, S, info);
4754 memset(&c, '\0', sizeof(struct category64_t));
4755 if (left < sizeof(struct category64_t)) {
4756 memcpy(&c, r, left);
4757 outs() << " (category_t entends past the end of the section)\n";
4759 memcpy(&c, r, sizeof(struct category64_t));
4760 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4764 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
4765 info, n_value, c.name);
4767 if (info->verbose && sym_name != nullptr)
4770 outs() << format("0x%" PRIx64, n_value);
4772 outs() << " + " << format("0x%" PRIx64, c.name);
4774 outs() << format("0x%" PRIx64, c.name);
4775 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
4776 if (name != nullptr)
4777 outs() << format(" %.*s", left, name);
4781 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
4784 if (info->verbose && sym_name != nullptr)
4787 outs() << format("0x%" PRIx64, n_value);
4789 outs() << " + " << format("0x%" PRIx64, c.cls);
4791 outs() << format("0x%" PRIx64, c.cls);
4793 if (c.cls + n_value != 0)
4794 print_class64_t(c.cls + n_value, info);
4796 outs() << " instanceMethods ";
4798 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
4799 info, n_value, c.instanceMethods);
4801 if (info->verbose && sym_name != nullptr)
4804 outs() << format("0x%" PRIx64, n_value);
4805 if (c.instanceMethods != 0)
4806 outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
4808 outs() << format("0x%" PRIx64, c.instanceMethods);
4810 if (c.instanceMethods + n_value != 0)
4811 print_method_list64_t(c.instanceMethods + n_value, info, "");
4813 outs() << " classMethods ";
4814 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
4815 S, info, n_value, c.classMethods);
4817 if (info->verbose && sym_name != nullptr)
4820 outs() << format("0x%" PRIx64, n_value);
4821 if (c.classMethods != 0)
4822 outs() << " + " << format("0x%" PRIx64, c.classMethods);
4824 outs() << format("0x%" PRIx64, c.classMethods);
4826 if (c.classMethods + n_value != 0)
4827 print_method_list64_t(c.classMethods + n_value, info, "");
4829 outs() << " protocols ";
4830 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
4831 info, n_value, c.protocols);
4833 if (info->verbose && sym_name != nullptr)
4836 outs() << format("0x%" PRIx64, n_value);
4837 if (c.protocols != 0)
4838 outs() << " + " << format("0x%" PRIx64, c.protocols);
4840 outs() << format("0x%" PRIx64, c.protocols);
4842 if (c.protocols + n_value != 0)
4843 print_protocol_list64_t(c.protocols + n_value, info);
4845 outs() << "instanceProperties ";
4847 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
4848 S, info, n_value, c.instanceProperties);
4850 if (info->verbose && sym_name != nullptr)
4853 outs() << format("0x%" PRIx64, n_value);
4854 if (c.instanceProperties != 0)
4855 outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
4857 outs() << format("0x%" PRIx64, c.instanceProperties);
4859 if (c.instanceProperties + n_value != 0)
4860 print_objc_property_list64(c.instanceProperties + n_value, info);
4863 static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
4864 struct category32_t c;
4866 uint32_t offset, left;
4870 r = get_pointer_32(p, offset, left, S, info);
4873 memset(&c, '\0', sizeof(struct category32_t));
4874 if (left < sizeof(struct category32_t)) {
4875 memcpy(&c, r, left);
4876 outs() << " (category_t entends past the end of the section)\n";
4878 memcpy(&c, r, sizeof(struct category32_t));
4879 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4882 outs() << " name " << format("0x%" PRIx32, c.name);
4883 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
4886 outs() << " " << name;
4889 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
4891 print_class32_t(c.cls, info);
4892 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
4894 if (c.instanceMethods != 0)
4895 print_method_list32_t(c.instanceMethods, info, "");
4896 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
4898 if (c.classMethods != 0)
4899 print_method_list32_t(c.classMethods, info, "");
4900 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
4901 if (c.protocols != 0)
4902 print_protocol_list32_t(c.protocols, info);
4903 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
4905 if (c.instanceProperties != 0)
4906 print_objc_property_list32(c.instanceProperties, info);
4909 static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
4910 uint32_t i, left, offset, xoffset;
4911 uint64_t p, n_value;
4912 struct message_ref64 mr;
4913 const char *name, *sym_name;
4917 if (S == SectionRef())
4921 S.getName(SectName);
4922 DataRefImpl Ref = S.getRawDataRefImpl();
4923 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4924 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4926 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
4927 p = S.getAddress() + i;
4928 r = get_pointer_64(p, offset, left, S, info);
4931 memset(&mr, '\0', sizeof(struct message_ref64));
4932 if (left < sizeof(struct message_ref64)) {
4933 memcpy(&mr, r, left);
4934 outs() << " (message_ref entends past the end of the section)\n";
4936 memcpy(&mr, r, sizeof(struct message_ref64));
4937 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4941 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
4944 outs() << format("0x%" PRIx64, n_value) << " ";
4946 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
4948 outs() << format("0x%" PRIx64, mr.imp) << " ";
4949 if (name != nullptr)
4950 outs() << " " << name;
4954 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
4955 info, n_value, mr.sel);
4957 if (info->verbose && sym_name != nullptr)
4960 outs() << format("0x%" PRIx64, n_value);
4962 outs() << " + " << format("0x%" PRIx64, mr.sel);
4964 outs() << format("0x%" PRIx64, mr.sel);
4965 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
4966 if (name != nullptr)
4967 outs() << format(" %.*s", left, name);
4970 offset += sizeof(struct message_ref64);
4974 static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
4975 uint32_t i, left, offset, xoffset, p;
4976 struct message_ref32 mr;
4977 const char *name, *r;
4980 if (S == SectionRef())
4984 S.getName(SectName);
4985 DataRefImpl Ref = S.getRawDataRefImpl();
4986 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4987 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4989 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
4990 p = S.getAddress() + i;
4991 r = get_pointer_32(p, offset, left, S, info);
4994 memset(&mr, '\0', sizeof(struct message_ref32));
4995 if (left < sizeof(struct message_ref32)) {
4996 memcpy(&mr, r, left);
4997 outs() << " (message_ref entends past the end of the section)\n";
4999 memcpy(&mr, r, sizeof(struct message_ref32));
5000 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5003 outs() << " imp " << format("0x%" PRIx32, mr.imp);
5004 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
5006 if (name != nullptr)
5007 outs() << " " << name;
5010 outs() << " sel " << format("0x%" PRIx32, mr.sel);
5011 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
5012 if (name != nullptr)
5013 outs() << " " << name;
5016 offset += sizeof(struct message_ref32);
5020 static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
5021 uint32_t left, offset, swift_version;
5023 struct objc_image_info64 o;
5027 S.getName(SectName);
5028 DataRefImpl Ref = S.getRawDataRefImpl();
5029 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5030 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5032 r = get_pointer_64(p, offset, left, S, info);
5035 memset(&o, '\0', sizeof(struct objc_image_info64));
5036 if (left < sizeof(struct objc_image_info64)) {
5037 memcpy(&o, r, left);
5038 outs() << " (objc_image_info entends past the end of the section)\n";
5040 memcpy(&o, r, sizeof(struct objc_image_info64));
5041 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5043 outs() << " version " << o.version << "\n";
5044 outs() << " flags " << format("0x%" PRIx32, o.flags);
5045 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5046 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5047 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5048 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5049 swift_version = (o.flags >> 8) & 0xff;
5050 if (swift_version != 0) {
5051 if (swift_version == 1)
5052 outs() << " Swift 1.0";
5053 else if (swift_version == 2)
5054 outs() << " Swift 1.1";
5056 outs() << " unknown future Swift version (" << swift_version << ")";
5061 static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5062 uint32_t left, offset, swift_version, p;
5063 struct objc_image_info32 o;
5067 S.getName(SectName);
5068 DataRefImpl Ref = S.getRawDataRefImpl();
5069 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5070 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5072 r = get_pointer_32(p, offset, left, S, info);
5075 memset(&o, '\0', sizeof(struct objc_image_info32));
5076 if (left < sizeof(struct objc_image_info32)) {
5077 memcpy(&o, r, left);
5078 outs() << " (objc_image_info entends past the end of the section)\n";
5080 memcpy(&o, r, sizeof(struct objc_image_info32));
5081 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5083 outs() << " version " << o.version << "\n";
5084 outs() << " flags " << format("0x%" PRIx32, o.flags);
5085 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5086 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5087 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5088 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5089 swift_version = (o.flags >> 8) & 0xff;
5090 if (swift_version != 0) {
5091 if (swift_version == 1)
5092 outs() << " Swift 1.0";
5093 else if (swift_version == 2)
5094 outs() << " Swift 1.1";
5096 outs() << " unknown future Swift version (" << swift_version << ")";
5101 static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5102 uint32_t left, offset, p;
5103 struct imageInfo_t o;
5107 S.getName(SectName);
5108 DataRefImpl Ref = S.getRawDataRefImpl();
5109 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5110 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5112 r = get_pointer_32(p, offset, left, S, info);
5115 memset(&o, '\0', sizeof(struct imageInfo_t));
5116 if (left < sizeof(struct imageInfo_t)) {
5117 memcpy(&o, r, left);
5118 outs() << " (imageInfo entends past the end of the section)\n";
5120 memcpy(&o, r, sizeof(struct imageInfo_t));
5121 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5123 outs() << " version " << o.version << "\n";
5124 outs() << " flags " << format("0x%" PRIx32, o.flags);
5130 outs() << " GC-only";
5136 static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
5137 SymbolAddressMap AddrMap;
5139 CreateSymbolAddressMap(O, &AddrMap);
5141 std::vector<SectionRef> Sections;
5142 for (const SectionRef &Section : O->sections()) {
5144 Section.getName(SectName);
5145 Sections.push_back(Section);
5148 struct DisassembleInfo info;
5149 // Set up the block of info used by the Symbolizer call backs.
5150 info.verbose = verbose;
5152 info.AddrMap = &AddrMap;
5153 info.Sections = &Sections;
5154 info.class_name = nullptr;
5155 info.selector_name = nullptr;
5156 info.method = nullptr;
5157 info.demangled_name = nullptr;
5158 info.bindtable = nullptr;
5162 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5163 if (CL != SectionRef()) {
5165 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5167 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5169 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5172 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5173 if (CR != SectionRef()) {
5175 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5177 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5179 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5182 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5183 if (SR != SectionRef()) {
5185 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5187 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5189 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5192 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5193 if (CA != SectionRef()) {
5195 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5197 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5199 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5202 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5203 if (PL != SectionRef()) {
5205 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5207 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5209 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5212 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5213 if (MR != SectionRef()) {
5215 print_message_refs64(MR, &info);
5217 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5219 print_message_refs64(MR, &info);
5222 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5223 if (II != SectionRef()) {
5225 print_image_info64(II, &info);
5227 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5229 print_image_info64(II, &info);
5232 if (info.bindtable != nullptr)
5233 delete info.bindtable;
5236 static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
5237 SymbolAddressMap AddrMap;
5239 CreateSymbolAddressMap(O, &AddrMap);
5241 std::vector<SectionRef> Sections;
5242 for (const SectionRef &Section : O->sections()) {
5244 Section.getName(SectName);
5245 Sections.push_back(Section);
5248 struct DisassembleInfo info;
5249 // Set up the block of info used by the Symbolizer call backs.
5250 info.verbose = verbose;
5252 info.AddrMap = &AddrMap;
5253 info.Sections = &Sections;
5254 info.class_name = nullptr;
5255 info.selector_name = nullptr;
5256 info.method = nullptr;
5257 info.demangled_name = nullptr;
5258 info.bindtable = nullptr;
5262 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5263 if (CL != SectionRef()) {
5265 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5267 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5269 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5272 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5273 if (CR != SectionRef()) {
5275 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5277 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5279 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5282 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5283 if (SR != SectionRef()) {
5285 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5287 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5289 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5292 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5293 if (CA != SectionRef()) {
5295 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5297 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5299 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5302 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5303 if (PL != SectionRef()) {
5305 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5307 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5309 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5312 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5313 if (MR != SectionRef()) {
5315 print_message_refs32(MR, &info);
5317 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5319 print_message_refs32(MR, &info);
5322 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5323 if (II != SectionRef()) {
5325 print_image_info32(II, &info);
5327 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5329 print_image_info32(II, &info);
5333 static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
5334 uint32_t i, j, p, offset, xoffset, left, defs_left, def;
5335 const char *r, *name, *defs;
5336 struct objc_module_t module;
5338 struct objc_symtab_t symtab;
5339 struct objc_class_t objc_class;
5340 struct objc_category_t objc_category;
5342 outs() << "Objective-C segment\n";
5343 S = get_section(O, "__OBJC", "__module_info");
5344 if (S == SectionRef())
5347 SymbolAddressMap AddrMap;
5349 CreateSymbolAddressMap(O, &AddrMap);
5351 std::vector<SectionRef> Sections;
5352 for (const SectionRef &Section : O->sections()) {
5354 Section.getName(SectName);
5355 Sections.push_back(Section);
5358 struct DisassembleInfo info;
5359 // Set up the block of info used by the Symbolizer call backs.
5360 info.verbose = verbose;
5362 info.AddrMap = &AddrMap;
5363 info.Sections = &Sections;
5364 info.class_name = nullptr;
5365 info.selector_name = nullptr;
5366 info.method = nullptr;
5367 info.demangled_name = nullptr;
5368 info.bindtable = nullptr;
5372 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
5373 p = S.getAddress() + i;
5374 r = get_pointer_32(p, offset, left, S, &info, true);
5377 memset(&module, '\0', sizeof(struct objc_module_t));
5378 if (left < sizeof(struct objc_module_t)) {
5379 memcpy(&module, r, left);
5380 outs() << " (module extends past end of __module_info section)\n";
5382 memcpy(&module, r, sizeof(struct objc_module_t));
5383 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5386 outs() << "Module " << format("0x%" PRIx32, p) << "\n";
5387 outs() << " version " << module.version << "\n";
5388 outs() << " size " << module.size << "\n";
5390 name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
5391 if (name != nullptr)
5392 outs() << format("%.*s", left, name);
5394 outs() << format("0x%08" PRIx32, module.name)
5395 << "(not in an __OBJC section)";
5398 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
5399 if (module.symtab == 0 || r == nullptr) {
5400 outs() << " symtab " << format("0x%08" PRIx32, module.symtab)
5401 << " (not in an __OBJC section)\n";
5404 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
5405 memset(&symtab, '\0', sizeof(struct objc_symtab_t));
5408 if (left < sizeof(struct objc_symtab_t)) {
5409 memcpy(&symtab, r, left);
5410 outs() << "\tsymtab extends past end of an __OBJC section)\n";
5412 memcpy(&symtab, r, sizeof(struct objc_symtab_t));
5413 if (left > sizeof(struct objc_symtab_t)) {
5414 defs_left = left - sizeof(struct objc_symtab_t);
5415 defs = r + sizeof(struct objc_symtab_t);
5418 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5421 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
5422 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
5423 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
5425 outs() << " (not in an __OBJC section)";
5427 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
5428 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
5429 if (symtab.cls_def_cnt > 0)
5430 outs() << "\tClass Definitions\n";
5431 for (j = 0; j < symtab.cls_def_cnt; j++) {
5432 if ((j + 1) * sizeof(uint32_t) > defs_left) {
5433 outs() << "\t(remaining class defs entries entends past the end of the "
5437 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
5438 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5439 sys::swapByteOrder(def);
5441 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5442 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
5444 if (left > sizeof(struct objc_class_t)) {
5446 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5448 outs() << " (entends past the end of the section)\n";
5449 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5450 memcpy(&objc_class, r, left);
5452 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5453 swapStruct(objc_class);
5454 print_objc_class_t(&objc_class, &info);
5456 outs() << "(not in an __OBJC section)\n";
5459 if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
5460 outs() << "\tMeta Class";
5461 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
5463 if (left > sizeof(struct objc_class_t)) {
5465 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5467 outs() << " (entends past the end of the section)\n";
5468 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5469 memcpy(&objc_class, r, left);
5471 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5472 swapStruct(objc_class);
5473 print_objc_class_t(&objc_class, &info);
5475 outs() << "(not in an __OBJC section)\n";
5479 if (symtab.cat_def_cnt > 0)
5480 outs() << "\tCategory Definitions\n";
5481 for (j = 0; j < symtab.cat_def_cnt; j++) {
5482 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
5483 outs() << "\t(remaining category defs entries entends past the end of "
5484 << "the section)\n";
5487 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
5489 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5490 sys::swapByteOrder(def);
5492 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5493 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
5494 << format("0x%08" PRIx32, def);
5496 if (left > sizeof(struct objc_category_t)) {
5498 memcpy(&objc_category, r, sizeof(struct objc_category_t));
5500 outs() << " (entends past the end of the section)\n";
5501 memset(&objc_category, '\0', sizeof(struct objc_category_t));
5502 memcpy(&objc_category, r, left);
5504 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5505 swapStruct(objc_category);
5506 print_objc_objc_category_t(&objc_category, &info);
5508 outs() << "(not in an __OBJC section)\n";
5512 const SectionRef II = get_section(O, "__OBJC", "__image_info");
5513 if (II != SectionRef())
5514 print_image_info(II, &info);
5519 static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
5520 uint32_t size, uint32_t addr) {
5521 SymbolAddressMap AddrMap;
5522 CreateSymbolAddressMap(O, &AddrMap);
5524 std::vector<SectionRef> Sections;
5525 for (const SectionRef &Section : O->sections()) {
5527 Section.getName(SectName);
5528 Sections.push_back(Section);
5531 struct DisassembleInfo info;
5532 // Set up the block of info used by the Symbolizer call backs.
5533 info.verbose = true;
5535 info.AddrMap = &AddrMap;
5536 info.Sections = &Sections;
5537 info.class_name = nullptr;
5538 info.selector_name = nullptr;
5539 info.method = nullptr;
5540 info.demangled_name = nullptr;
5541 info.bindtable = nullptr;
5546 struct objc_protocol_t protocol;
5547 uint32_t left, paddr;
5548 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
5549 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
5550 left = size - (p - sect);
5551 if (left < sizeof(struct objc_protocol_t)) {
5552 outs() << "Protocol extends past end of __protocol section\n";
5553 memcpy(&protocol, p, left);
5555 memcpy(&protocol, p, sizeof(struct objc_protocol_t));
5556 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5557 swapStruct(protocol);
5558 paddr = addr + (p - sect);
5559 outs() << "Protocol " << format("0x%" PRIx32, paddr);
5560 if (print_protocol(paddr, 0, &info))
5561 outs() << "(not in an __OBJC section)\n";
5565 static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
5567 printObjc2_64bit_MetaData(O, verbose);
5569 MachO::mach_header H;
5571 if (H.cputype == MachO::CPU_TYPE_ARM)
5572 printObjc2_32bit_MetaData(O, verbose);
5574 // This is the 32-bit non-arm cputype case. Which is normally
5575 // the first Objective-C ABI. But it may be the case of a
5576 // binary for the iOS simulator which is the second Objective-C
5577 // ABI. In that case printObjc1_32bit_MetaData() will determine that
5578 // and return false.
5579 if (printObjc1_32bit_MetaData(O, verbose) == false)
5580 printObjc2_32bit_MetaData(O, verbose);
5585 // GuessLiteralPointer returns a string which for the item in the Mach-O file
5586 // for the address passed in as ReferenceValue for printing as a comment with
5587 // the instruction and also returns the corresponding type of that item
5588 // indirectly through ReferenceType.
5590 // If ReferenceValue is an address of literal cstring then a pointer to the
5591 // cstring is returned and ReferenceType is set to
5592 // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
5594 // If ReferenceValue is an address of an Objective-C CFString, Selector ref or
5595 // Class ref that name is returned and the ReferenceType is set accordingly.
5597 // Lastly, literals which are Symbol address in a literal pool are looked for
5598 // and if found the symbol name is returned and ReferenceType is set to
5599 // LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
5601 // If there is no item in the Mach-O file for the address passed in as
5602 // ReferenceValue nullptr is returned and ReferenceType is unchanged.
5603 static const char *GuessLiteralPointer(uint64_t ReferenceValue,
5604 uint64_t ReferencePC,
5605 uint64_t *ReferenceType,
5606 struct DisassembleInfo *info) {
5607 // First see if there is an external relocation entry at the ReferencePC.
5608 uint64_t sect_addr = info->S.getAddress();
5609 uint64_t sect_offset = ReferencePC - sect_addr;
5610 bool reloc_found = false;
5612 MachO::any_relocation_info RE;
5613 bool isExtern = false;
5615 for (const RelocationRef &Reloc : info->S.relocations()) {
5616 uint64_t RelocOffset;
5617 Reloc.getOffset(RelocOffset);
5618 if (RelocOffset == sect_offset) {
5619 Rel = Reloc.getRawDataRefImpl();
5620 RE = info->O->getRelocation(Rel);
5621 if (info->O->isRelocationScattered(RE))
5623 isExtern = info->O->getPlainRelocationExternal(RE);
5625 symbol_iterator RelocSym = Reloc.getSymbol();
5632 // If there is an external relocation entry for a symbol in a section
5633 // then used that symbol's value for the value of the reference.
5634 if (reloc_found && isExtern) {
5635 if (info->O->getAnyRelocationPCRel(RE)) {
5636 unsigned Type = info->O->getAnyRelocationType(RE);
5637 if (Type == MachO::X86_64_RELOC_SIGNED) {
5638 Symbol.getAddress(ReferenceValue);
5643 // Look for literals such as Objective-C CFStrings refs, Selector refs,
5644 // Message refs and Class refs.
5645 bool classref, selref, msgref, cfstring;
5646 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
5647 selref, msgref, cfstring);
5648 if (classref && pointer_value == 0) {
5649 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
5650 // And the pointer_value in that section is typically zero as it will be
5651 // set by dyld as part of the "bind information".
5652 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
5653 if (name != nullptr) {
5654 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
5655 const char *class_name = strrchr(name, '$');
5656 if (class_name != nullptr && class_name[1] == '_' &&
5657 class_name[2] != '\0') {
5658 info->class_name = class_name + 2;
5665 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
5667 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
5668 if (name != nullptr)
5669 info->class_name = name;
5671 name = "bad class ref";
5676 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
5677 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
5681 if (selref && pointer_value == 0)
5682 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
5684 if (pointer_value != 0)
5685 ReferenceValue = pointer_value;
5687 const char *name = GuessCstringPointer(ReferenceValue, info);
5689 if (pointer_value != 0 && selref) {
5690 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
5691 info->selector_name = name;
5692 } else if (pointer_value != 0 && msgref) {
5693 info->class_name = nullptr;
5694 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
5695 info->selector_name = name;
5697 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
5701 // Lastly look for an indirect symbol with this ReferenceValue which is in
5702 // a literal pool. If found return that symbol name.
5703 name = GuessIndirectSymbol(ReferenceValue, info);
5705 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
5712 // SymbolizerSymbolLookUp is the symbol lookup function passed when creating
5713 // the Symbolizer. It looks up the ReferenceValue using the info passed via the
5714 // pointer to the struct DisassembleInfo that was passed when MCSymbolizer
5715 // is created and returns the symbol name that matches the ReferenceValue or
5716 // nullptr if none. The ReferenceType is passed in for the IN type of
5717 // reference the instruction is making from the values in defined in the header
5718 // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
5719 // Out type and the ReferenceName will also be set which is added as a comment
5720 // to the disassembled instruction.
5723 // If the symbol name is a C++ mangled name then the demangled name is
5724 // returned through ReferenceName and ReferenceType is set to
5725 // LLVMDisassembler_ReferenceType_DeMangled_Name .
5728 // When this is called to get a symbol name for a branch target then the
5729 // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
5730 // SymbolValue will be looked for in the indirect symbol table to determine if
5731 // it is an address for a symbol stub. If so then the symbol name for that
5732 // stub is returned indirectly through ReferenceName and then ReferenceType is
5733 // set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
5735 // When this is called with an value loaded via a PC relative load then
5736 // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
5737 // SymbolValue is checked to be an address of literal pointer, symbol pointer,
5738 // or an Objective-C meta data reference. If so the output ReferenceType is
5739 // set to correspond to that as well as setting the ReferenceName.
5740 static const char *SymbolizerSymbolLookUp(void *DisInfo,
5741 uint64_t ReferenceValue,
5742 uint64_t *ReferenceType,
5743 uint64_t ReferencePC,
5744 const char **ReferenceName) {
5745 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
5746 // If no verbose symbolic information is wanted then just return nullptr.
5747 if (!info->verbose) {
5748 *ReferenceName = nullptr;
5749 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5753 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
5755 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
5756 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
5757 if (*ReferenceName != nullptr) {
5758 method_reference(info, ReferenceType, ReferenceName);
5759 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
5760 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
5763 if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
5764 if (info->demangled_name != nullptr)
5765 free(info->demangled_name);
5767 info->demangled_name =
5768 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
5769 if (info->demangled_name != nullptr) {
5770 *ReferenceName = info->demangled_name;
5771 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5773 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5776 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5777 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
5779 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5781 method_reference(info, ReferenceType, ReferenceName);
5783 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5784 // If this is arm64 and the reference is an adrp instruction save the
5785 // instruction, passed in ReferenceValue and the address of the instruction
5786 // for use later if we see and add immediate instruction.
5787 } else if (info->O->getArch() == Triple::aarch64 &&
5788 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
5789 info->adrp_inst = ReferenceValue;
5790 info->adrp_addr = ReferencePC;
5791 SymbolName = nullptr;
5792 *ReferenceName = nullptr;
5793 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5794 // If this is arm64 and reference is an add immediate instruction and we
5796 // seen an adrp instruction just before it and the adrp's Xd register
5798 // this add's Xn register reconstruct the value being referenced and look to
5799 // see if it is a literal pointer. Note the add immediate instruction is
5800 // passed in ReferenceValue.
5801 } else if (info->O->getArch() == Triple::aarch64 &&
5802 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
5803 ReferencePC - 4 == info->adrp_addr &&
5804 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5805 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5806 uint32_t addxri_inst;
5807 uint64_t adrp_imm, addxri_imm;
5810 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5811 if (info->adrp_inst & 0x0200000)
5812 adrp_imm |= 0xfffffffffc000000LL;
5814 addxri_inst = ReferenceValue;
5815 addxri_imm = (addxri_inst >> 10) & 0xfff;
5816 if (((addxri_inst >> 22) & 0x3) == 1)
5819 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5820 (adrp_imm << 12) + addxri_imm;
5823 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5824 if (*ReferenceName == nullptr)
5825 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5826 // If this is arm64 and the reference is a load register instruction and we
5827 // have seen an adrp instruction just before it and the adrp's Xd register
5828 // matches this add's Xn register reconstruct the value being referenced and
5829 // look to see if it is a literal pointer. Note the load register
5830 // instruction is passed in ReferenceValue.
5831 } else if (info->O->getArch() == Triple::aarch64 &&
5832 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
5833 ReferencePC - 4 == info->adrp_addr &&
5834 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5835 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5836 uint32_t ldrxui_inst;
5837 uint64_t adrp_imm, ldrxui_imm;
5840 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5841 if (info->adrp_inst & 0x0200000)
5842 adrp_imm |= 0xfffffffffc000000LL;
5844 ldrxui_inst = ReferenceValue;
5845 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
5847 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5848 (adrp_imm << 12) + (ldrxui_imm << 3);
5851 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5852 if (*ReferenceName == nullptr)
5853 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5855 // If this arm64 and is an load register (PC-relative) instruction the
5856 // ReferenceValue is the PC plus the immediate value.
5857 else if (info->O->getArch() == Triple::aarch64 &&
5858 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
5859 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
5861 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5862 if (*ReferenceName == nullptr)
5863 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5866 else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
5867 if (info->demangled_name != nullptr)
5868 free(info->demangled_name);
5870 info->demangled_name =
5871 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
5872 if (info->demangled_name != nullptr) {
5873 *ReferenceName = info->demangled_name;
5874 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5879 *ReferenceName = nullptr;
5880 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5886 /// \brief Emits the comments that are stored in the CommentStream.
5887 /// Each comment in the CommentStream must end with a newline.
5888 static void emitComments(raw_svector_ostream &CommentStream,
5889 SmallString<128> &CommentsToEmit,
5890 formatted_raw_ostream &FormattedOS,
5891 const MCAsmInfo &MAI) {
5892 // Flush the stream before taking its content.
5893 CommentStream.flush();
5894 StringRef Comments = CommentsToEmit.str();
5895 // Get the default information for printing a comment.
5896 const char *CommentBegin = MAI.getCommentString();
5897 unsigned CommentColumn = MAI.getCommentColumn();
5898 bool IsFirst = true;
5899 while (!Comments.empty()) {
5901 FormattedOS << '\n';
5902 // Emit a line of comments.
5903 FormattedOS.PadToColumn(CommentColumn);
5904 size_t Position = Comments.find('\n');
5905 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
5906 // Move after the newline character.
5907 Comments = Comments.substr(Position + 1);
5910 FormattedOS.flush();
5912 // Tell the comment stream that the vector changed underneath it.
5913 CommentsToEmit.clear();
5914 CommentStream.resync();
5917 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
5918 StringRef DisSegName, StringRef DisSectName) {
5919 const char *McpuDefault = nullptr;
5920 const Target *ThumbTarget = nullptr;
5921 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
5923 // GetTarget prints out stuff.
5926 if (MCPU.empty() && McpuDefault)
5929 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
5930 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
5932 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
5934 // Package up features to be passed to target/subtarget
5935 std::string FeaturesStr;
5936 if (MAttrs.size()) {
5937 SubtargetFeatures Features;
5938 for (unsigned i = 0; i != MAttrs.size(); ++i)
5939 Features.AddFeature(MAttrs[i]);
5940 FeaturesStr = Features.getString();
5943 // Set up disassembler.
5944 std::unique_ptr<const MCRegisterInfo> MRI(
5945 TheTarget->createMCRegInfo(TripleName));
5946 std::unique_ptr<const MCAsmInfo> AsmInfo(
5947 TheTarget->createMCAsmInfo(*MRI, TripleName));
5948 std::unique_ptr<const MCSubtargetInfo> STI(
5949 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
5950 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
5951 std::unique_ptr<MCDisassembler> DisAsm(
5952 TheTarget->createMCDisassembler(*STI, Ctx));
5953 std::unique_ptr<MCSymbolizer> Symbolizer;
5954 struct DisassembleInfo SymbolizerInfo;
5955 std::unique_ptr<MCRelocationInfo> RelInfo(
5956 TheTarget->createMCRelocationInfo(TripleName, Ctx));
5958 Symbolizer.reset(TheTarget->createMCSymbolizer(
5959 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
5960 &SymbolizerInfo, &Ctx, std::move(RelInfo)));
5961 DisAsm->setSymbolizer(std::move(Symbolizer));
5963 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
5964 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
5965 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
5966 // Set the display preference for hex vs. decimal immediates.
5967 IP->setPrintImmHex(PrintImmHex);
5968 // Comment stream and backing vector.
5969 SmallString<128> CommentsToEmit;
5970 raw_svector_ostream CommentStream(CommentsToEmit);
5971 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
5972 // if it is done then arm64 comments for string literals don't get printed
5973 // and some constant get printed instead and not setting it causes intel
5974 // (32-bit and 64-bit) comments printed with different spacing before the
5975 // comment causing different diffs with the 'C' disassembler library API.
5976 // IP->setCommentStream(CommentStream);
5978 if (!AsmInfo || !STI || !DisAsm || !IP) {
5979 errs() << "error: couldn't initialize disassembler for target "
5980 << TripleName << '\n';
5984 // Set up thumb disassembler.
5985 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
5986 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
5987 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
5988 std::unique_ptr<MCDisassembler> ThumbDisAsm;
5989 std::unique_ptr<MCInstPrinter> ThumbIP;
5990 std::unique_ptr<MCContext> ThumbCtx;
5991 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
5992 struct DisassembleInfo ThumbSymbolizerInfo;
5993 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
5995 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
5997 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
5999 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr));
6000 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
6001 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
6002 MCContext *PtrThumbCtx = ThumbCtx.get();
6004 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
6006 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
6007 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
6008 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
6009 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
6011 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
6012 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
6013 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
6014 *ThumbInstrInfo, *ThumbMRI));
6015 // Set the display preference for hex vs. decimal immediates.
6016 ThumbIP->setPrintImmHex(PrintImmHex);
6019 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
6020 errs() << "error: couldn't initialize disassembler for target "
6021 << ThumbTripleName << '\n';
6025 MachO::mach_header Header = MachOOF->getHeader();
6027 // FIXME: Using the -cfg command line option, this code used to be able to
6028 // annotate relocations with the referenced symbol's name, and if this was
6029 // inside a __[cf]string section, the data it points to. This is now replaced
6030 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
6031 std::vector<SectionRef> Sections;
6032 std::vector<SymbolRef> Symbols;
6033 SmallVector<uint64_t, 8> FoundFns;
6034 uint64_t BaseSegmentAddress;
6036 getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
6037 BaseSegmentAddress);
6039 // Sort the symbols by address, just in case they didn't come in that way.
6040 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
6042 // Build a data in code table that is sorted on by the address of each entry.
6043 uint64_t BaseAddress = 0;
6044 if (Header.filetype == MachO::MH_OBJECT)
6045 BaseAddress = Sections[0].getAddress();
6047 BaseAddress = BaseSegmentAddress;
6049 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
6052 DI->getOffset(Offset);
6053 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
6055 array_pod_sort(Dices.begin(), Dices.end());
6058 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
6060 raw_ostream &DebugOut = nulls();
6063 std::unique_ptr<DIContext> diContext;
6064 ObjectFile *DbgObj = MachOOF;
6065 // Try to find debug info and set up the DIContext for it.
6067 // A separate DSym file path was specified, parse it as a macho file,
6068 // get the sections and supply it to the section name parsing machinery.
6069 if (!DSYMFile.empty()) {
6070 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
6071 MemoryBuffer::getFileOrSTDIN(DSYMFile);
6072 if (std::error_code EC = BufOrErr.getError()) {
6073 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
6077 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
6082 // Setup the DIContext
6083 diContext.reset(new DWARFContextInMemory(*DbgObj));
6086 if (DumpSections.size() == 0)
6087 outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
6089 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
6091 if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
6094 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
6096 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
6097 if (SegmentName != DisSegName)
6101 Sections[SectIdx].getContents(BytesStr);
6102 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
6104 uint64_t SectAddress = Sections[SectIdx].getAddress();
6106 bool symbolTableWorked = false;
6108 // Parse relocations.
6109 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
6110 for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) {
6111 uint64_t RelocOffset;
6112 Reloc.getOffset(RelocOffset);
6113 uint64_t SectionAddress = Sections[SectIdx].getAddress();
6114 RelocOffset -= SectionAddress;
6116 symbol_iterator RelocSym = Reloc.getSymbol();
6118 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
6120 array_pod_sort(Relocs.begin(), Relocs.end());
6122 // Create a map of symbol addresses to symbol names for use by
6123 // the SymbolizerSymbolLookUp() routine.
6124 SymbolAddressMap AddrMap;
6125 bool DisSymNameFound = false;
6126 for (const SymbolRef &Symbol : MachOOF->symbols()) {
6129 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
6130 ST == SymbolRef::ST_Other) {
6132 Symbol.getAddress(Address);
6134 Symbol.getName(SymName);
6135 AddrMap[Address] = SymName;
6136 if (!DisSymName.empty() && DisSymName == SymName)
6137 DisSymNameFound = true;
6140 if (!DisSymName.empty() && !DisSymNameFound) {
6141 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
6144 // Set up the block of info used by the Symbolizer call backs.
6145 SymbolizerInfo.verbose = !NoSymbolicOperands;
6146 SymbolizerInfo.O = MachOOF;
6147 SymbolizerInfo.S = Sections[SectIdx];
6148 SymbolizerInfo.AddrMap = &AddrMap;
6149 SymbolizerInfo.Sections = &Sections;
6150 SymbolizerInfo.class_name = nullptr;
6151 SymbolizerInfo.selector_name = nullptr;
6152 SymbolizerInfo.method = nullptr;
6153 SymbolizerInfo.demangled_name = nullptr;
6154 SymbolizerInfo.bindtable = nullptr;
6155 SymbolizerInfo.adrp_addr = 0;
6156 SymbolizerInfo.adrp_inst = 0;
6157 // Same for the ThumbSymbolizer
6158 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
6159 ThumbSymbolizerInfo.O = MachOOF;
6160 ThumbSymbolizerInfo.S = Sections[SectIdx];
6161 ThumbSymbolizerInfo.AddrMap = &AddrMap;
6162 ThumbSymbolizerInfo.Sections = &Sections;
6163 ThumbSymbolizerInfo.class_name = nullptr;
6164 ThumbSymbolizerInfo.selector_name = nullptr;
6165 ThumbSymbolizerInfo.method = nullptr;
6166 ThumbSymbolizerInfo.demangled_name = nullptr;
6167 ThumbSymbolizerInfo.bindtable = nullptr;
6168 ThumbSymbolizerInfo.adrp_addr = 0;
6169 ThumbSymbolizerInfo.adrp_inst = 0;
6171 // Disassemble symbol by symbol.
6172 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
6174 Symbols[SymIdx].getName(SymName);
6177 Symbols[SymIdx].getType(ST);
6178 if (ST != SymbolRef::ST_Function)
6181 // Make sure the symbol is defined in this section.
6182 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
6186 // If we are only disassembling one symbol see if this is that symbol.
6187 if (!DisSymName.empty() && DisSymName != SymName)
6190 // Start at the address of the symbol relative to the section's address.
6192 uint64_t SectionAddress = Sections[SectIdx].getAddress();
6193 Symbols[SymIdx].getAddress(Start);
6194 Start -= SectionAddress;
6196 // Stop disassembling either at the beginning of the next symbol or at
6197 // the end of the section.
6198 bool containsNextSym = false;
6199 uint64_t NextSym = 0;
6200 uint64_t NextSymIdx = SymIdx + 1;
6201 while (Symbols.size() > NextSymIdx) {
6202 SymbolRef::Type NextSymType;
6203 Symbols[NextSymIdx].getType(NextSymType);
6204 if (NextSymType == SymbolRef::ST_Function) {
6206 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
6207 Symbols[NextSymIdx].getAddress(NextSym);
6208 NextSym -= SectionAddress;
6214 uint64_t SectSize = Sections[SectIdx].getSize();
6215 uint64_t End = containsNextSym ? NextSym : SectSize;
6218 symbolTableWorked = true;
6220 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
6222 (MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget;
6224 outs() << SymName << ":\n";
6225 DILineInfo lastLine;
6226 for (uint64_t Index = Start; Index < End; Index += Size) {
6229 uint64_t PC = SectAddress + Index;
6230 if (!NoLeadingAddr) {
6231 if (FullLeadingAddr) {
6232 if (MachOOF->is64Bit())
6233 outs() << format("%016" PRIx64, PC);
6235 outs() << format("%08" PRIx64, PC);
6237 outs() << format("%8" PRIx64 ":", PC);
6243 // Check the data in code table here to see if this is data not an
6244 // instruction to be disassembled.
6246 Dice.push_back(std::make_pair(PC, DiceRef()));
6247 dice_table_iterator DTI =
6248 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
6249 compareDiceTableEntries);
6250 if (DTI != Dices.end()) {
6252 DTI->second.getLength(Length);
6254 DTI->second.getKind(Kind);
6255 Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
6256 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
6257 (PC == (DTI->first + Length - 1)) && (Length & 1))
6262 SmallVector<char, 64> AnnotationsBytes;
6263 raw_svector_ostream Annotations(AnnotationsBytes);
6267 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
6268 PC, DebugOut, Annotations);
6270 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
6271 DebugOut, Annotations);
6273 if (!NoShowRawInsn) {
6274 dumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, Size), outs());
6276 formatted_raw_ostream FormattedOS(outs());
6277 Annotations.flush();
6278 StringRef AnnotationsStr = Annotations.str();
6280 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
6282 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
6283 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
6285 // Print debug info.
6287 DILineInfo dli = diContext->getLineInfoForAddress(PC);
6288 // Print valid line info if it changed.
6289 if (dli != lastLine && dli.Line != 0)
6290 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
6296 unsigned int Arch = MachOOF->getArch();
6297 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
6298 outs() << format("\t.byte 0x%02x #bad opcode\n",
6299 *(Bytes.data() + Index) & 0xff);
6300 Size = 1; // skip exactly one illegible byte and move on.
6301 } else if (Arch == Triple::aarch64) {
6302 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6303 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
6304 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
6305 (*(Bytes.data() + Index + 3) & 0xff) << 24;
6306 outs() << format("\t.long\t0x%08x\n", opcode);
6309 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6311 Size = 1; // skip illegible bytes
6316 if (!symbolTableWorked) {
6317 // Reading the symbol table didn't work, disassemble the whole section.
6318 uint64_t SectAddress = Sections[SectIdx].getAddress();
6319 uint64_t SectSize = Sections[SectIdx].getSize();
6321 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
6324 uint64_t PC = SectAddress + Index;
6325 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
6326 DebugOut, nulls())) {
6327 if (!NoLeadingAddr) {
6328 if (FullLeadingAddr) {
6329 if (MachOOF->is64Bit())
6330 outs() << format("%016" PRIx64, PC);
6332 outs() << format("%08" PRIx64, PC);
6334 outs() << format("%8" PRIx64 ":", PC);
6337 if (!NoShowRawInsn) {
6339 dumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, InstSize), outs());
6341 IP->printInst(&Inst, outs(), "", *STI);
6344 unsigned int Arch = MachOOF->getArch();
6345 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
6346 outs() << format("\t.byte 0x%02x #bad opcode\n",
6347 *(Bytes.data() + Index) & 0xff);
6348 InstSize = 1; // skip exactly one illegible byte and move on.
6350 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6352 InstSize = 1; // skip illegible bytes
6357 // The TripleName's need to be reset if we are called again for a different
6360 ThumbTripleName = "";
6362 if (SymbolizerInfo.method != nullptr)
6363 free(SymbolizerInfo.method);
6364 if (SymbolizerInfo.demangled_name != nullptr)
6365 free(SymbolizerInfo.demangled_name);
6366 if (SymbolizerInfo.bindtable != nullptr)
6367 delete SymbolizerInfo.bindtable;
6368 if (ThumbSymbolizerInfo.method != nullptr)
6369 free(ThumbSymbolizerInfo.method);
6370 if (ThumbSymbolizerInfo.demangled_name != nullptr)
6371 free(ThumbSymbolizerInfo.demangled_name);
6372 if (ThumbSymbolizerInfo.bindtable != nullptr)
6373 delete ThumbSymbolizerInfo.bindtable;
6377 //===----------------------------------------------------------------------===//
6378 // __compact_unwind section dumping
6379 //===----------------------------------------------------------------------===//
6383 template <typename T> static uint64_t readNext(const char *&Buf) {
6384 using llvm::support::little;
6385 using llvm::support::unaligned;
6387 uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
6392 struct CompactUnwindEntry {
6393 uint32_t OffsetInSection;
6395 uint64_t FunctionAddr;
6397 uint32_t CompactEncoding;
6398 uint64_t PersonalityAddr;
6401 RelocationRef FunctionReloc;
6402 RelocationRef PersonalityReloc;
6403 RelocationRef LSDAReloc;
6405 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
6406 : OffsetInSection(Offset) {
6408 read<uint64_t>(Contents.data() + Offset);
6410 read<uint32_t>(Contents.data() + Offset);
6414 template <typename UIntPtr> void read(const char *Buf) {
6415 FunctionAddr = readNext<UIntPtr>(Buf);
6416 Length = readNext<uint32_t>(Buf);
6417 CompactEncoding = readNext<uint32_t>(Buf);
6418 PersonalityAddr = readNext<UIntPtr>(Buf);
6419 LSDAAddr = readNext<UIntPtr>(Buf);
6424 /// Given a relocation from __compact_unwind, consisting of the RelocationRef
6425 /// and data being relocated, determine the best base Name and Addend to use for
6426 /// display purposes.
6428 /// 1. An Extern relocation will directly reference a symbol (and the data is
6429 /// then already an addend), so use that.
6430 /// 2. Otherwise the data is an offset in the object file's layout; try to find
6431 // a symbol before it in the same section, and use the offset from there.
6432 /// 3. Finally, if all that fails, fall back to an offset from the start of the
6433 /// referenced section.
6434 static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
6435 std::map<uint64_t, SymbolRef> &Symbols,
6436 const RelocationRef &Reloc, uint64_t Addr,
6437 StringRef &Name, uint64_t &Addend) {
6438 if (Reloc.getSymbol() != Obj->symbol_end()) {
6439 Reloc.getSymbol()->getName(Name);
6444 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
6445 SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
6447 uint64_t SectionAddr = RelocSection.getAddress();
6449 auto Sym = Symbols.upper_bound(Addr);
6450 if (Sym == Symbols.begin()) {
6451 // The first symbol in the object is after this reference, the best we can
6452 // do is section-relative notation.
6453 RelocSection.getName(Name);
6454 Addend = Addr - SectionAddr;
6458 // Go back one so that SymbolAddress <= Addr.
6461 section_iterator SymSection = Obj->section_end();
6462 Sym->second.getSection(SymSection);
6463 if (RelocSection == *SymSection) {
6464 // There's a valid symbol in the same section before this reference.
6465 Sym->second.getName(Name);
6466 Addend = Addr - Sym->first;
6470 // There is a symbol before this reference, but it's in a different
6471 // section. Probably not helpful to mention it, so use the section name.
6472 RelocSection.getName(Name);
6473 Addend = Addr - SectionAddr;
6476 static void printUnwindRelocDest(const MachOObjectFile *Obj,
6477 std::map<uint64_t, SymbolRef> &Symbols,
6478 const RelocationRef &Reloc, uint64_t Addr) {
6482 if (!Reloc.getObjectFile())
6485 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
6489 outs() << " + " << format("0x%" PRIx64, Addend);
6493 printMachOCompactUnwindSection(const MachOObjectFile *Obj,
6494 std::map<uint64_t, SymbolRef> &Symbols,
6495 const SectionRef &CompactUnwind) {
6497 assert(Obj->isLittleEndian() &&
6498 "There should not be a big-endian .o with __compact_unwind");
6500 bool Is64 = Obj->is64Bit();
6501 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
6502 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
6505 CompactUnwind.getContents(Contents);
6507 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
6509 // First populate the initial raw offsets, encodings and so on from the entry.
6510 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
6511 CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
6512 CompactUnwinds.push_back(Entry);
6515 // Next we need to look at the relocations to find out what objects are
6516 // actually being referred to.
6517 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
6518 uint64_t RelocAddress;
6519 Reloc.getOffset(RelocAddress);
6521 uint32_t EntryIdx = RelocAddress / EntrySize;
6522 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
6523 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
6525 if (OffsetInEntry == 0)
6526 Entry.FunctionReloc = Reloc;
6527 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
6528 Entry.PersonalityReloc = Reloc;
6529 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
6530 Entry.LSDAReloc = Reloc;
6532 llvm_unreachable("Unexpected relocation in __compact_unwind section");
6535 // Finally, we're ready to print the data we've gathered.
6536 outs() << "Contents of __compact_unwind section:\n";
6537 for (auto &Entry : CompactUnwinds) {
6538 outs() << " Entry at offset "
6539 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
6541 // 1. Start of the region this entry applies to.
6542 outs() << " start: " << format("0x%" PRIx64,
6543 Entry.FunctionAddr) << ' ';
6544 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
6547 // 2. Length of the region this entry applies to.
6548 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
6550 // 3. The 32-bit compact encoding.
6551 outs() << " compact encoding: "
6552 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
6554 // 4. The personality function, if present.
6555 if (Entry.PersonalityReloc.getObjectFile()) {
6556 outs() << " personality function: "
6557 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
6558 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
6559 Entry.PersonalityAddr);
6563 // 5. This entry's language-specific data area.
6564 if (Entry.LSDAReloc.getObjectFile()) {
6565 outs() << " LSDA: " << format("0x%" PRIx64,
6566 Entry.LSDAAddr) << ' ';
6567 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
6573 //===----------------------------------------------------------------------===//
6574 // __unwind_info section dumping
6575 //===----------------------------------------------------------------------===//
6577 static void printRegularSecondLevelUnwindPage(const char *PageStart) {
6578 const char *Pos = PageStart;
6579 uint32_t Kind = readNext<uint32_t>(Pos);
6581 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
6583 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6584 uint16_t NumEntries = readNext<uint16_t>(Pos);
6586 Pos = PageStart + EntriesStart;
6587 for (unsigned i = 0; i < NumEntries; ++i) {
6588 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6589 uint32_t Encoding = readNext<uint32_t>(Pos);
6591 outs() << " [" << i << "]: "
6592 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6594 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
6598 static void printCompressedSecondLevelUnwindPage(
6599 const char *PageStart, uint32_t FunctionBase,
6600 const SmallVectorImpl<uint32_t> &CommonEncodings) {
6601 const char *Pos = PageStart;
6602 uint32_t Kind = readNext<uint32_t>(Pos);
6604 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
6606 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6607 uint16_t NumEntries = readNext<uint16_t>(Pos);
6609 uint16_t EncodingsStart = readNext<uint16_t>(Pos);
6610 readNext<uint16_t>(Pos);
6611 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
6612 PageStart + EncodingsStart);
6614 Pos = PageStart + EntriesStart;
6615 for (unsigned i = 0; i < NumEntries; ++i) {
6616 uint32_t Entry = readNext<uint32_t>(Pos);
6617 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
6618 uint32_t EncodingIdx = Entry >> 24;
6621 if (EncodingIdx < CommonEncodings.size())
6622 Encoding = CommonEncodings[EncodingIdx];
6624 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
6626 outs() << " [" << i << "]: "
6627 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6629 << "encoding[" << EncodingIdx
6630 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
6634 static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
6635 std::map<uint64_t, SymbolRef> &Symbols,
6636 const SectionRef &UnwindInfo) {
6638 assert(Obj->isLittleEndian() &&
6639 "There should not be a big-endian .o with __unwind_info");
6641 outs() << "Contents of __unwind_info section:\n";
6644 UnwindInfo.getContents(Contents);
6645 const char *Pos = Contents.data();
6647 //===----------------------------------
6649 //===----------------------------------
6651 uint32_t Version = readNext<uint32_t>(Pos);
6652 outs() << " Version: "
6653 << format("0x%" PRIx32, Version) << '\n';
6654 assert(Version == 1 && "only understand version 1");
6656 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
6657 outs() << " Common encodings array section offset: "
6658 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
6659 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
6660 outs() << " Number of common encodings in array: "
6661 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
6663 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
6664 outs() << " Personality function array section offset: "
6665 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
6666 uint32_t NumPersonalities = readNext<uint32_t>(Pos);
6667 outs() << " Number of personality functions in array: "
6668 << format("0x%" PRIx32, NumPersonalities) << '\n';
6670 uint32_t IndicesStart = readNext<uint32_t>(Pos);
6671 outs() << " Index array section offset: "
6672 << format("0x%" PRIx32, IndicesStart) << '\n';
6673 uint32_t NumIndices = readNext<uint32_t>(Pos);
6674 outs() << " Number of indices in array: "
6675 << format("0x%" PRIx32, NumIndices) << '\n';
6677 //===----------------------------------
6678 // A shared list of common encodings
6679 //===----------------------------------
6681 // These occupy indices in the range [0, N] whenever an encoding is referenced
6682 // from a compressed 2nd level index table. In practice the linker only
6683 // creates ~128 of these, so that indices are available to embed encodings in
6684 // the 2nd level index.
6686 SmallVector<uint32_t, 64> CommonEncodings;
6687 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
6688 Pos = Contents.data() + CommonEncodingsStart;
6689 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
6690 uint32_t Encoding = readNext<uint32_t>(Pos);
6691 CommonEncodings.push_back(Encoding);
6693 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
6697 //===----------------------------------
6698 // Personality functions used in this executable
6699 //===----------------------------------
6701 // There should be only a handful of these (one per source language,
6702 // roughly). Particularly since they only get 2 bits in the compact encoding.
6704 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
6705 Pos = Contents.data() + PersonalitiesStart;
6706 for (unsigned i = 0; i < NumPersonalities; ++i) {
6707 uint32_t PersonalityFn = readNext<uint32_t>(Pos);
6708 outs() << " personality[" << i + 1
6709 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
6712 //===----------------------------------
6713 // The level 1 index entries
6714 //===----------------------------------
6716 // These specify an approximate place to start searching for the more detailed
6717 // information, sorted by PC.
6720 uint32_t FunctionOffset;
6721 uint32_t SecondLevelPageStart;
6725 SmallVector<IndexEntry, 4> IndexEntries;
6727 outs() << " Top level indices: (count = " << NumIndices << ")\n";
6728 Pos = Contents.data() + IndicesStart;
6729 for (unsigned i = 0; i < NumIndices; ++i) {
6732 Entry.FunctionOffset = readNext<uint32_t>(Pos);
6733 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
6734 Entry.LSDAStart = readNext<uint32_t>(Pos);
6735 IndexEntries.push_back(Entry);
6737 outs() << " [" << i << "]: "
6738 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
6740 << "2nd level page offset="
6741 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
6742 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
6745 //===----------------------------------
6746 // Next come the LSDA tables
6747 //===----------------------------------
6749 // The LSDA layout is rather implicit: it's a contiguous array of entries from
6750 // the first top-level index's LSDAOffset to the last (sentinel).
6752 outs() << " LSDA descriptors:\n";
6753 Pos = Contents.data() + IndexEntries[0].LSDAStart;
6754 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
6755 (2 * sizeof(uint32_t));
6756 for (int i = 0; i < NumLSDAs; ++i) {
6757 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6758 uint32_t LSDAOffset = readNext<uint32_t>(Pos);
6759 outs() << " [" << i << "]: "
6760 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6762 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
6765 //===----------------------------------
6766 // Finally, the 2nd level indices
6767 //===----------------------------------
6769 // Generally these are 4K in size, and have 2 possible forms:
6770 // + Regular stores up to 511 entries with disparate encodings
6771 // + Compressed stores up to 1021 entries if few enough compact encoding
6773 outs() << " Second level indices:\n";
6774 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
6775 // The final sentinel top-level index has no associated 2nd level page
6776 if (IndexEntries[i].SecondLevelPageStart == 0)
6779 outs() << " Second level index[" << i << "]: "
6780 << "offset in section="
6781 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
6783 << "base function offset="
6784 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
6786 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
6787 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
6789 printRegularSecondLevelUnwindPage(Pos);
6791 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
6794 llvm_unreachable("Do not know how to print this kind of 2nd level page");
6798 void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
6799 std::map<uint64_t, SymbolRef> Symbols;
6800 for (const SymbolRef &SymRef : Obj->symbols()) {
6801 // Discard any undefined or absolute symbols. They're not going to take part
6802 // in the convenience lookup for unwind info and just take up resources.
6803 section_iterator Section = Obj->section_end();
6804 SymRef.getSection(Section);
6805 if (Section == Obj->section_end())
6809 SymRef.getAddress(Addr);
6810 Symbols.insert(std::make_pair(Addr, SymRef));
6813 for (const SectionRef &Section : Obj->sections()) {
6815 Section.getName(SectName);
6816 if (SectName == "__compact_unwind")
6817 printMachOCompactUnwindSection(Obj, Symbols, Section);
6818 else if (SectName == "__unwind_info")
6819 printMachOUnwindInfoSection(Obj, Symbols, Section);
6820 else if (SectName == "__eh_frame")
6821 outs() << "llvm-objdump: warning: unhandled __eh_frame section\n";
6825 static void PrintMachHeader(uint32_t magic, uint32_t cputype,
6826 uint32_t cpusubtype, uint32_t filetype,
6827 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
6829 outs() << "Mach header\n";
6830 outs() << " magic cputype cpusubtype caps filetype ncmds "
6831 "sizeofcmds flags\n";
6833 if (magic == MachO::MH_MAGIC)
6834 outs() << " MH_MAGIC";
6835 else if (magic == MachO::MH_MAGIC_64)
6836 outs() << "MH_MAGIC_64";
6838 outs() << format(" 0x%08" PRIx32, magic);
6840 case MachO::CPU_TYPE_I386:
6842 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6843 case MachO::CPU_SUBTYPE_I386_ALL:
6847 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6851 case MachO::CPU_TYPE_X86_64:
6852 outs() << " X86_64";
6853 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6854 case MachO::CPU_SUBTYPE_X86_64_ALL:
6857 case MachO::CPU_SUBTYPE_X86_64_H:
6858 outs() << " Haswell";
6861 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6865 case MachO::CPU_TYPE_ARM:
6867 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6868 case MachO::CPU_SUBTYPE_ARM_ALL:
6871 case MachO::CPU_SUBTYPE_ARM_V4T:
6874 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
6877 case MachO::CPU_SUBTYPE_ARM_XSCALE:
6878 outs() << " XSCALE";
6880 case MachO::CPU_SUBTYPE_ARM_V6:
6883 case MachO::CPU_SUBTYPE_ARM_V6M:
6886 case MachO::CPU_SUBTYPE_ARM_V7:
6889 case MachO::CPU_SUBTYPE_ARM_V7EM:
6892 case MachO::CPU_SUBTYPE_ARM_V7K:
6895 case MachO::CPU_SUBTYPE_ARM_V7M:
6898 case MachO::CPU_SUBTYPE_ARM_V7S:
6902 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6906 case MachO::CPU_TYPE_ARM64:
6908 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6909 case MachO::CPU_SUBTYPE_ARM64_ALL:
6913 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6917 case MachO::CPU_TYPE_POWERPC:
6919 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6920 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6924 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6928 case MachO::CPU_TYPE_POWERPC64:
6930 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6931 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6935 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6940 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
6943 outs() << format(" 0x%02" PRIx32,
6944 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
6947 case MachO::MH_OBJECT:
6948 outs() << " OBJECT";
6950 case MachO::MH_EXECUTE:
6951 outs() << " EXECUTE";
6953 case MachO::MH_FVMLIB:
6954 outs() << " FVMLIB";
6956 case MachO::MH_CORE:
6959 case MachO::MH_PRELOAD:
6960 outs() << " PRELOAD";
6962 case MachO::MH_DYLIB:
6965 case MachO::MH_DYLIB_STUB:
6966 outs() << " DYLIB_STUB";
6968 case MachO::MH_DYLINKER:
6969 outs() << " DYLINKER";
6971 case MachO::MH_BUNDLE:
6972 outs() << " BUNDLE";
6974 case MachO::MH_DSYM:
6977 case MachO::MH_KEXT_BUNDLE:
6978 outs() << " KEXTBUNDLE";
6981 outs() << format(" %10u", filetype);
6984 outs() << format(" %5u", ncmds);
6985 outs() << format(" %10u", sizeofcmds);
6987 if (f & MachO::MH_NOUNDEFS) {
6988 outs() << " NOUNDEFS";
6989 f &= ~MachO::MH_NOUNDEFS;
6991 if (f & MachO::MH_INCRLINK) {
6992 outs() << " INCRLINK";
6993 f &= ~MachO::MH_INCRLINK;
6995 if (f & MachO::MH_DYLDLINK) {
6996 outs() << " DYLDLINK";
6997 f &= ~MachO::MH_DYLDLINK;
6999 if (f & MachO::MH_BINDATLOAD) {
7000 outs() << " BINDATLOAD";
7001 f &= ~MachO::MH_BINDATLOAD;
7003 if (f & MachO::MH_PREBOUND) {
7004 outs() << " PREBOUND";
7005 f &= ~MachO::MH_PREBOUND;
7007 if (f & MachO::MH_SPLIT_SEGS) {
7008 outs() << " SPLIT_SEGS";
7009 f &= ~MachO::MH_SPLIT_SEGS;
7011 if (f & MachO::MH_LAZY_INIT) {
7012 outs() << " LAZY_INIT";
7013 f &= ~MachO::MH_LAZY_INIT;
7015 if (f & MachO::MH_TWOLEVEL) {
7016 outs() << " TWOLEVEL";
7017 f &= ~MachO::MH_TWOLEVEL;
7019 if (f & MachO::MH_FORCE_FLAT) {
7020 outs() << " FORCE_FLAT";
7021 f &= ~MachO::MH_FORCE_FLAT;
7023 if (f & MachO::MH_NOMULTIDEFS) {
7024 outs() << " NOMULTIDEFS";
7025 f &= ~MachO::MH_NOMULTIDEFS;
7027 if (f & MachO::MH_NOFIXPREBINDING) {
7028 outs() << " NOFIXPREBINDING";
7029 f &= ~MachO::MH_NOFIXPREBINDING;
7031 if (f & MachO::MH_PREBINDABLE) {
7032 outs() << " PREBINDABLE";
7033 f &= ~MachO::MH_PREBINDABLE;
7035 if (f & MachO::MH_ALLMODSBOUND) {
7036 outs() << " ALLMODSBOUND";
7037 f &= ~MachO::MH_ALLMODSBOUND;
7039 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
7040 outs() << " SUBSECTIONS_VIA_SYMBOLS";
7041 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
7043 if (f & MachO::MH_CANONICAL) {
7044 outs() << " CANONICAL";
7045 f &= ~MachO::MH_CANONICAL;
7047 if (f & MachO::MH_WEAK_DEFINES) {
7048 outs() << " WEAK_DEFINES";
7049 f &= ~MachO::MH_WEAK_DEFINES;
7051 if (f & MachO::MH_BINDS_TO_WEAK) {
7052 outs() << " BINDS_TO_WEAK";
7053 f &= ~MachO::MH_BINDS_TO_WEAK;
7055 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
7056 outs() << " ALLOW_STACK_EXECUTION";
7057 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
7059 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
7060 outs() << " DEAD_STRIPPABLE_DYLIB";
7061 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
7063 if (f & MachO::MH_PIE) {
7065 f &= ~MachO::MH_PIE;
7067 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
7068 outs() << " NO_REEXPORTED_DYLIBS";
7069 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
7071 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
7072 outs() << " MH_HAS_TLV_DESCRIPTORS";
7073 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
7075 if (f & MachO::MH_NO_HEAP_EXECUTION) {
7076 outs() << " MH_NO_HEAP_EXECUTION";
7077 f &= ~MachO::MH_NO_HEAP_EXECUTION;
7079 if (f & MachO::MH_APP_EXTENSION_SAFE) {
7080 outs() << " APP_EXTENSION_SAFE";
7081 f &= ~MachO::MH_APP_EXTENSION_SAFE;
7083 if (f != 0 || flags == 0)
7084 outs() << format(" 0x%08" PRIx32, f);
7086 outs() << format(" 0x%08" PRIx32, magic);
7087 outs() << format(" %7d", cputype);
7088 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7089 outs() << format(" 0x%02" PRIx32,
7090 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7091 outs() << format(" %10u", filetype);
7092 outs() << format(" %5u", ncmds);
7093 outs() << format(" %10u", sizeofcmds);
7094 outs() << format(" 0x%08" PRIx32, flags);
7099 static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
7100 StringRef SegName, uint64_t vmaddr,
7101 uint64_t vmsize, uint64_t fileoff,
7102 uint64_t filesize, uint32_t maxprot,
7103 uint32_t initprot, uint32_t nsects,
7104 uint32_t flags, uint32_t object_size,
7106 uint64_t expected_cmdsize;
7107 if (cmd == MachO::LC_SEGMENT) {
7108 outs() << " cmd LC_SEGMENT\n";
7109 expected_cmdsize = nsects;
7110 expected_cmdsize *= sizeof(struct MachO::section);
7111 expected_cmdsize += sizeof(struct MachO::segment_command);
7113 outs() << " cmd LC_SEGMENT_64\n";
7114 expected_cmdsize = nsects;
7115 expected_cmdsize *= sizeof(struct MachO::section_64);
7116 expected_cmdsize += sizeof(struct MachO::segment_command_64);
7118 outs() << " cmdsize " << cmdsize;
7119 if (cmdsize != expected_cmdsize)
7120 outs() << " Inconsistent size\n";
7123 outs() << " segname " << SegName << "\n";
7124 if (cmd == MachO::LC_SEGMENT_64) {
7125 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
7126 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
7128 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
7129 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
7131 outs() << " fileoff " << fileoff;
7132 if (fileoff > object_size)
7133 outs() << " (past end of file)\n";
7136 outs() << " filesize " << filesize;
7137 if (fileoff + filesize > object_size)
7138 outs() << " (past end of file)\n";
7143 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7144 MachO::VM_PROT_EXECUTE)) != 0)
7145 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
7147 if (maxprot & MachO::VM_PROT_READ)
7148 outs() << " maxprot r";
7150 outs() << " maxprot -";
7151 if (maxprot & MachO::VM_PROT_WRITE)
7155 if (maxprot & MachO::VM_PROT_EXECUTE)
7161 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7162 MachO::VM_PROT_EXECUTE)) != 0)
7163 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
7165 if (initprot & MachO::VM_PROT_READ)
7166 outs() << " initprot r";
7168 outs() << " initprot -";
7169 if (initprot & MachO::VM_PROT_WRITE)
7173 if (initprot & MachO::VM_PROT_EXECUTE)
7179 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
7180 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
7182 outs() << " nsects " << nsects << "\n";
7186 outs() << " (none)\n";
7188 if (flags & MachO::SG_HIGHVM) {
7189 outs() << " HIGHVM";
7190 flags &= ~MachO::SG_HIGHVM;
7192 if (flags & MachO::SG_FVMLIB) {
7193 outs() << " FVMLIB";
7194 flags &= ~MachO::SG_FVMLIB;
7196 if (flags & MachO::SG_NORELOC) {
7197 outs() << " NORELOC";
7198 flags &= ~MachO::SG_NORELOC;
7200 if (flags & MachO::SG_PROTECTED_VERSION_1) {
7201 outs() << " PROTECTED_VERSION_1";
7202 flags &= ~MachO::SG_PROTECTED_VERSION_1;
7205 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
7210 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
7214 static void PrintSection(const char *sectname, const char *segname,
7215 uint64_t addr, uint64_t size, uint32_t offset,
7216 uint32_t align, uint32_t reloff, uint32_t nreloc,
7217 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
7218 uint32_t cmd, const char *sg_segname,
7219 uint32_t filetype, uint32_t object_size,
7221 outs() << "Section\n";
7222 outs() << " sectname " << format("%.16s\n", sectname);
7223 outs() << " segname " << format("%.16s", segname);
7224 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
7225 outs() << " (does not match segment)\n";
7228 if (cmd == MachO::LC_SEGMENT_64) {
7229 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
7230 outs() << " size " << format("0x%016" PRIx64, size);
7232 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
7233 outs() << " size " << format("0x%08" PRIx64, size);
7235 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
7236 outs() << " (past end of file)\n";
7239 outs() << " offset " << offset;
7240 if (offset > object_size)
7241 outs() << " (past end of file)\n";
7244 uint32_t align_shifted = 1 << align;
7245 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
7246 outs() << " reloff " << reloff;
7247 if (reloff > object_size)
7248 outs() << " (past end of file)\n";
7251 outs() << " nreloc " << nreloc;
7252 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
7253 outs() << " (past end of file)\n";
7256 uint32_t section_type = flags & MachO::SECTION_TYPE;
7259 if (section_type == MachO::S_REGULAR)
7260 outs() << " S_REGULAR\n";
7261 else if (section_type == MachO::S_ZEROFILL)
7262 outs() << " S_ZEROFILL\n";
7263 else if (section_type == MachO::S_CSTRING_LITERALS)
7264 outs() << " S_CSTRING_LITERALS\n";
7265 else if (section_type == MachO::S_4BYTE_LITERALS)
7266 outs() << " S_4BYTE_LITERALS\n";
7267 else if (section_type == MachO::S_8BYTE_LITERALS)
7268 outs() << " S_8BYTE_LITERALS\n";
7269 else if (section_type == MachO::S_16BYTE_LITERALS)
7270 outs() << " S_16BYTE_LITERALS\n";
7271 else if (section_type == MachO::S_LITERAL_POINTERS)
7272 outs() << " S_LITERAL_POINTERS\n";
7273 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
7274 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
7275 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
7276 outs() << " S_LAZY_SYMBOL_POINTERS\n";
7277 else if (section_type == MachO::S_SYMBOL_STUBS)
7278 outs() << " S_SYMBOL_STUBS\n";
7279 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
7280 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
7281 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
7282 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
7283 else if (section_type == MachO::S_COALESCED)
7284 outs() << " S_COALESCED\n";
7285 else if (section_type == MachO::S_INTERPOSING)
7286 outs() << " S_INTERPOSING\n";
7287 else if (section_type == MachO::S_DTRACE_DOF)
7288 outs() << " S_DTRACE_DOF\n";
7289 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
7290 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
7291 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
7292 outs() << " S_THREAD_LOCAL_REGULAR\n";
7293 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
7294 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
7295 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
7296 outs() << " S_THREAD_LOCAL_VARIABLES\n";
7297 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7298 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
7299 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
7300 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
7302 outs() << format("0x%08" PRIx32, section_type) << "\n";
7303 outs() << "attributes";
7304 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
7305 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
7306 outs() << " PURE_INSTRUCTIONS";
7307 if (section_attributes & MachO::S_ATTR_NO_TOC)
7308 outs() << " NO_TOC";
7309 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
7310 outs() << " STRIP_STATIC_SYMS";
7311 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
7312 outs() << " NO_DEAD_STRIP";
7313 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
7314 outs() << " LIVE_SUPPORT";
7315 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
7316 outs() << " SELF_MODIFYING_CODE";
7317 if (section_attributes & MachO::S_ATTR_DEBUG)
7319 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
7320 outs() << " SOME_INSTRUCTIONS";
7321 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
7322 outs() << " EXT_RELOC";
7323 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
7324 outs() << " LOC_RELOC";
7325 if (section_attributes == 0)
7326 outs() << " (none)";
7329 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
7330 outs() << " reserved1 " << reserved1;
7331 if (section_type == MachO::S_SYMBOL_STUBS ||
7332 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
7333 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
7334 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
7335 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7336 outs() << " (index into indirect symbol table)\n";
7339 outs() << " reserved2 " << reserved2;
7340 if (section_type == MachO::S_SYMBOL_STUBS)
7341 outs() << " (size of stubs)\n";
7346 static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
7347 uint32_t object_size) {
7348 outs() << " cmd LC_SYMTAB\n";
7349 outs() << " cmdsize " << st.cmdsize;
7350 if (st.cmdsize != sizeof(struct MachO::symtab_command))
7351 outs() << " Incorrect size\n";
7354 outs() << " symoff " << st.symoff;
7355 if (st.symoff > object_size)
7356 outs() << " (past end of file)\n";
7359 outs() << " nsyms " << st.nsyms;
7362 big_size = st.nsyms;
7363 big_size *= sizeof(struct MachO::nlist_64);
7364 big_size += st.symoff;
7365 if (big_size > object_size)
7366 outs() << " (past end of file)\n";
7370 big_size = st.nsyms;
7371 big_size *= sizeof(struct MachO::nlist);
7372 big_size += st.symoff;
7373 if (big_size > object_size)
7374 outs() << " (past end of file)\n";
7378 outs() << " stroff " << st.stroff;
7379 if (st.stroff > object_size)
7380 outs() << " (past end of file)\n";
7383 outs() << " strsize " << st.strsize;
7384 big_size = st.stroff;
7385 big_size += st.strsize;
7386 if (big_size > object_size)
7387 outs() << " (past end of file)\n";
7392 static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
7393 uint32_t nsyms, uint32_t object_size,
7395 outs() << " cmd LC_DYSYMTAB\n";
7396 outs() << " cmdsize " << dyst.cmdsize;
7397 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
7398 outs() << " Incorrect size\n";
7401 outs() << " ilocalsym " << dyst.ilocalsym;
7402 if (dyst.ilocalsym > nsyms)
7403 outs() << " (greater than the number of symbols)\n";
7406 outs() << " nlocalsym " << dyst.nlocalsym;
7408 big_size = dyst.ilocalsym;
7409 big_size += dyst.nlocalsym;
7410 if (big_size > nsyms)
7411 outs() << " (past the end of the symbol table)\n";
7414 outs() << " iextdefsym " << dyst.iextdefsym;
7415 if (dyst.iextdefsym > nsyms)
7416 outs() << " (greater than the number of symbols)\n";
7419 outs() << " nextdefsym " << dyst.nextdefsym;
7420 big_size = dyst.iextdefsym;
7421 big_size += dyst.nextdefsym;
7422 if (big_size > nsyms)
7423 outs() << " (past the end of the symbol table)\n";
7426 outs() << " iundefsym " << dyst.iundefsym;
7427 if (dyst.iundefsym > nsyms)
7428 outs() << " (greater than the number of symbols)\n";
7431 outs() << " nundefsym " << dyst.nundefsym;
7432 big_size = dyst.iundefsym;
7433 big_size += dyst.nundefsym;
7434 if (big_size > nsyms)
7435 outs() << " (past the end of the symbol table)\n";
7438 outs() << " tocoff " << dyst.tocoff;
7439 if (dyst.tocoff > object_size)
7440 outs() << " (past end of file)\n";
7443 outs() << " ntoc " << dyst.ntoc;
7444 big_size = dyst.ntoc;
7445 big_size *= sizeof(struct MachO::dylib_table_of_contents);
7446 big_size += dyst.tocoff;
7447 if (big_size > object_size)
7448 outs() << " (past end of file)\n";
7451 outs() << " modtaboff " << dyst.modtaboff;
7452 if (dyst.modtaboff > object_size)
7453 outs() << " (past end of file)\n";
7456 outs() << " nmodtab " << dyst.nmodtab;
7459 modtabend = dyst.nmodtab;
7460 modtabend *= sizeof(struct MachO::dylib_module_64);
7461 modtabend += dyst.modtaboff;
7463 modtabend = dyst.nmodtab;
7464 modtabend *= sizeof(struct MachO::dylib_module);
7465 modtabend += dyst.modtaboff;
7467 if (modtabend > object_size)
7468 outs() << " (past end of file)\n";
7471 outs() << " extrefsymoff " << dyst.extrefsymoff;
7472 if (dyst.extrefsymoff > object_size)
7473 outs() << " (past end of file)\n";
7476 outs() << " nextrefsyms " << dyst.nextrefsyms;
7477 big_size = dyst.nextrefsyms;
7478 big_size *= sizeof(struct MachO::dylib_reference);
7479 big_size += dyst.extrefsymoff;
7480 if (big_size > object_size)
7481 outs() << " (past end of file)\n";
7484 outs() << " indirectsymoff " << dyst.indirectsymoff;
7485 if (dyst.indirectsymoff > object_size)
7486 outs() << " (past end of file)\n";
7489 outs() << " nindirectsyms " << dyst.nindirectsyms;
7490 big_size = dyst.nindirectsyms;
7491 big_size *= sizeof(uint32_t);
7492 big_size += dyst.indirectsymoff;
7493 if (big_size > object_size)
7494 outs() << " (past end of file)\n";
7497 outs() << " extreloff " << dyst.extreloff;
7498 if (dyst.extreloff > object_size)
7499 outs() << " (past end of file)\n";
7502 outs() << " nextrel " << dyst.nextrel;
7503 big_size = dyst.nextrel;
7504 big_size *= sizeof(struct MachO::relocation_info);
7505 big_size += dyst.extreloff;
7506 if (big_size > object_size)
7507 outs() << " (past end of file)\n";
7510 outs() << " locreloff " << dyst.locreloff;
7511 if (dyst.locreloff > object_size)
7512 outs() << " (past end of file)\n";
7515 outs() << " nlocrel " << dyst.nlocrel;
7516 big_size = dyst.nlocrel;
7517 big_size *= sizeof(struct MachO::relocation_info);
7518 big_size += dyst.locreloff;
7519 if (big_size > object_size)
7520 outs() << " (past end of file)\n";
7525 static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
7526 uint32_t object_size) {
7527 if (dc.cmd == MachO::LC_DYLD_INFO)
7528 outs() << " cmd LC_DYLD_INFO\n";
7530 outs() << " cmd LC_DYLD_INFO_ONLY\n";
7531 outs() << " cmdsize " << dc.cmdsize;
7532 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
7533 outs() << " Incorrect size\n";
7536 outs() << " rebase_off " << dc.rebase_off;
7537 if (dc.rebase_off > object_size)
7538 outs() << " (past end of file)\n";
7541 outs() << " rebase_size " << dc.rebase_size;
7543 big_size = dc.rebase_off;
7544 big_size += dc.rebase_size;
7545 if (big_size > object_size)
7546 outs() << " (past end of file)\n";
7549 outs() << " bind_off " << dc.bind_off;
7550 if (dc.bind_off > object_size)
7551 outs() << " (past end of file)\n";
7554 outs() << " bind_size " << dc.bind_size;
7555 big_size = dc.bind_off;
7556 big_size += dc.bind_size;
7557 if (big_size > object_size)
7558 outs() << " (past end of file)\n";
7561 outs() << " weak_bind_off " << dc.weak_bind_off;
7562 if (dc.weak_bind_off > object_size)
7563 outs() << " (past end of file)\n";
7566 outs() << " weak_bind_size " << dc.weak_bind_size;
7567 big_size = dc.weak_bind_off;
7568 big_size += dc.weak_bind_size;
7569 if (big_size > object_size)
7570 outs() << " (past end of file)\n";
7573 outs() << " lazy_bind_off " << dc.lazy_bind_off;
7574 if (dc.lazy_bind_off > object_size)
7575 outs() << " (past end of file)\n";
7578 outs() << " lazy_bind_size " << dc.lazy_bind_size;
7579 big_size = dc.lazy_bind_off;
7580 big_size += dc.lazy_bind_size;
7581 if (big_size > object_size)
7582 outs() << " (past end of file)\n";
7585 outs() << " export_off " << dc.export_off;
7586 if (dc.export_off > object_size)
7587 outs() << " (past end of file)\n";
7590 outs() << " export_size " << dc.export_size;
7591 big_size = dc.export_off;
7592 big_size += dc.export_size;
7593 if (big_size > object_size)
7594 outs() << " (past end of file)\n";
7599 static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
7601 if (dyld.cmd == MachO::LC_ID_DYLINKER)
7602 outs() << " cmd LC_ID_DYLINKER\n";
7603 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
7604 outs() << " cmd LC_LOAD_DYLINKER\n";
7605 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
7606 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
7608 outs() << " cmd ?(" << dyld.cmd << ")\n";
7609 outs() << " cmdsize " << dyld.cmdsize;
7610 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
7611 outs() << " Incorrect size\n";
7614 if (dyld.name >= dyld.cmdsize)
7615 outs() << " name ?(bad offset " << dyld.name << ")\n";
7617 const char *P = (const char *)(Ptr) + dyld.name;
7618 outs() << " name " << P << " (offset " << dyld.name << ")\n";
7622 static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
7623 outs() << " cmd LC_UUID\n";
7624 outs() << " cmdsize " << uuid.cmdsize;
7625 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
7626 outs() << " Incorrect size\n";
7630 outs() << format("%02" PRIX32, uuid.uuid[0]);
7631 outs() << format("%02" PRIX32, uuid.uuid[1]);
7632 outs() << format("%02" PRIX32, uuid.uuid[2]);
7633 outs() << format("%02" PRIX32, uuid.uuid[3]);
7635 outs() << format("%02" PRIX32, uuid.uuid[4]);
7636 outs() << format("%02" PRIX32, uuid.uuid[5]);
7638 outs() << format("%02" PRIX32, uuid.uuid[6]);
7639 outs() << format("%02" PRIX32, uuid.uuid[7]);
7641 outs() << format("%02" PRIX32, uuid.uuid[8]);
7642 outs() << format("%02" PRIX32, uuid.uuid[9]);
7644 outs() << format("%02" PRIX32, uuid.uuid[10]);
7645 outs() << format("%02" PRIX32, uuid.uuid[11]);
7646 outs() << format("%02" PRIX32, uuid.uuid[12]);
7647 outs() << format("%02" PRIX32, uuid.uuid[13]);
7648 outs() << format("%02" PRIX32, uuid.uuid[14]);
7649 outs() << format("%02" PRIX32, uuid.uuid[15]);
7653 static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
7654 outs() << " cmd LC_RPATH\n";
7655 outs() << " cmdsize " << rpath.cmdsize;
7656 if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
7657 outs() << " Incorrect size\n";
7660 if (rpath.path >= rpath.cmdsize)
7661 outs() << " path ?(bad offset " << rpath.path << ")\n";
7663 const char *P = (const char *)(Ptr) + rpath.path;
7664 outs() << " path " << P << " (offset " << rpath.path << ")\n";
7668 static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
7669 if (vd.cmd == MachO::LC_VERSION_MIN_MACOSX)
7670 outs() << " cmd LC_VERSION_MIN_MACOSX\n";
7671 else if (vd.cmd == MachO::LC_VERSION_MIN_IPHONEOS)
7672 outs() << " cmd LC_VERSION_MIN_IPHONEOS\n";
7674 outs() << " cmd " << vd.cmd << " (?)\n";
7675 outs() << " cmdsize " << vd.cmdsize;
7676 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
7677 outs() << " Incorrect size\n";
7680 outs() << " version " << ((vd.version >> 16) & 0xffff) << "."
7681 << ((vd.version >> 8) & 0xff);
7682 if ((vd.version & 0xff) != 0)
7683 outs() << "." << (vd.version & 0xff);
7686 outs() << " sdk n/a";
7688 outs() << " sdk " << ((vd.sdk >> 16) & 0xffff) << "."
7689 << ((vd.sdk >> 8) & 0xff);
7691 if ((vd.sdk & 0xff) != 0)
7692 outs() << "." << (vd.sdk & 0xff);
7696 static void PrintSourceVersionCommand(MachO::source_version_command sd) {
7697 outs() << " cmd LC_SOURCE_VERSION\n";
7698 outs() << " cmdsize " << sd.cmdsize;
7699 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
7700 outs() << " Incorrect size\n";
7703 uint64_t a = (sd.version >> 40) & 0xffffff;
7704 uint64_t b = (sd.version >> 30) & 0x3ff;
7705 uint64_t c = (sd.version >> 20) & 0x3ff;
7706 uint64_t d = (sd.version >> 10) & 0x3ff;
7707 uint64_t e = sd.version & 0x3ff;
7708 outs() << " version " << a << "." << b;
7710 outs() << "." << c << "." << d << "." << e;
7712 outs() << "." << c << "." << d;
7718 static void PrintEntryPointCommand(MachO::entry_point_command ep) {
7719 outs() << " cmd LC_MAIN\n";
7720 outs() << " cmdsize " << ep.cmdsize;
7721 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
7722 outs() << " Incorrect size\n";
7725 outs() << " entryoff " << ep.entryoff << "\n";
7726 outs() << " stacksize " << ep.stacksize << "\n";
7729 static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
7730 uint32_t object_size) {
7731 outs() << " cmd LC_ENCRYPTION_INFO\n";
7732 outs() << " cmdsize " << ec.cmdsize;
7733 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
7734 outs() << " Incorrect size\n";
7737 outs() << " cryptoff " << ec.cryptoff;
7738 if (ec.cryptoff > object_size)
7739 outs() << " (past end of file)\n";
7742 outs() << " cryptsize " << ec.cryptsize;
7743 if (ec.cryptsize > object_size)
7744 outs() << " (past end of file)\n";
7747 outs() << " cryptid " << ec.cryptid << "\n";
7750 static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
7751 uint32_t object_size) {
7752 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
7753 outs() << " cmdsize " << ec.cmdsize;
7754 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
7755 outs() << " Incorrect size\n";
7758 outs() << " cryptoff " << ec.cryptoff;
7759 if (ec.cryptoff > object_size)
7760 outs() << " (past end of file)\n";
7763 outs() << " cryptsize " << ec.cryptsize;
7764 if (ec.cryptsize > object_size)
7765 outs() << " (past end of file)\n";
7768 outs() << " cryptid " << ec.cryptid << "\n";
7769 outs() << " pad " << ec.pad << "\n";
7772 static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
7774 outs() << " cmd LC_LINKER_OPTION\n";
7775 outs() << " cmdsize " << lo.cmdsize;
7776 if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
7777 outs() << " Incorrect size\n";
7780 outs() << " count " << lo.count << "\n";
7781 const char *string = Ptr + sizeof(struct MachO::linker_option_command);
7782 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
7785 while (*string == '\0' && left > 0) {
7791 outs() << " string #" << i << " " << format("%.*s\n", left, string);
7792 uint32_t NullPos = StringRef(string, left).find('\0');
7793 uint32_t len = std::min(NullPos, left) + 1;
7799 outs() << " count " << lo.count << " does not match number of strings "
7803 static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
7805 outs() << " cmd LC_SUB_FRAMEWORK\n";
7806 outs() << " cmdsize " << sub.cmdsize;
7807 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
7808 outs() << " Incorrect size\n";
7811 if (sub.umbrella < sub.cmdsize) {
7812 const char *P = Ptr + sub.umbrella;
7813 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
7815 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
7819 static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
7821 outs() << " cmd LC_SUB_UMBRELLA\n";
7822 outs() << " cmdsize " << sub.cmdsize;
7823 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
7824 outs() << " Incorrect size\n";
7827 if (sub.sub_umbrella < sub.cmdsize) {
7828 const char *P = Ptr + sub.sub_umbrella;
7829 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
7831 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
7835 static void PrintSubLibraryCommand(MachO::sub_library_command sub,
7837 outs() << " cmd LC_SUB_LIBRARY\n";
7838 outs() << " cmdsize " << sub.cmdsize;
7839 if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
7840 outs() << " Incorrect size\n";
7843 if (sub.sub_library < sub.cmdsize) {
7844 const char *P = Ptr + sub.sub_library;
7845 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
7847 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
7851 static void PrintSubClientCommand(MachO::sub_client_command sub,
7853 outs() << " cmd LC_SUB_CLIENT\n";
7854 outs() << " cmdsize " << sub.cmdsize;
7855 if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
7856 outs() << " Incorrect size\n";
7859 if (sub.client < sub.cmdsize) {
7860 const char *P = Ptr + sub.client;
7861 outs() << " client " << P << " (offset " << sub.client << ")\n";
7863 outs() << " client ?(bad offset " << sub.client << ")\n";
7867 static void PrintRoutinesCommand(MachO::routines_command r) {
7868 outs() << " cmd LC_ROUTINES\n";
7869 outs() << " cmdsize " << r.cmdsize;
7870 if (r.cmdsize != sizeof(struct MachO::routines_command))
7871 outs() << " Incorrect size\n";
7874 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
7875 outs() << " init_module " << r.init_module << "\n";
7876 outs() << " reserved1 " << r.reserved1 << "\n";
7877 outs() << " reserved2 " << r.reserved2 << "\n";
7878 outs() << " reserved3 " << r.reserved3 << "\n";
7879 outs() << " reserved4 " << r.reserved4 << "\n";
7880 outs() << " reserved5 " << r.reserved5 << "\n";
7881 outs() << " reserved6 " << r.reserved6 << "\n";
7884 static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
7885 outs() << " cmd LC_ROUTINES_64\n";
7886 outs() << " cmdsize " << r.cmdsize;
7887 if (r.cmdsize != sizeof(struct MachO::routines_command_64))
7888 outs() << " Incorrect size\n";
7891 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
7892 outs() << " init_module " << r.init_module << "\n";
7893 outs() << " reserved1 " << r.reserved1 << "\n";
7894 outs() << " reserved2 " << r.reserved2 << "\n";
7895 outs() << " reserved3 " << r.reserved3 << "\n";
7896 outs() << " reserved4 " << r.reserved4 << "\n";
7897 outs() << " reserved5 " << r.reserved5 << "\n";
7898 outs() << " reserved6 " << r.reserved6 << "\n";
7901 static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
7902 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
7903 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
7904 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
7905 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
7906 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
7907 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
7908 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
7909 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
7910 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
7911 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
7912 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
7913 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
7914 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
7915 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
7916 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
7917 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
7918 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
7919 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
7920 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
7921 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
7922 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
7925 static void Print_mmst_reg(MachO::mmst_reg_t &r) {
7927 outs() << "\t mmst_reg ";
7928 for (f = 0; f < 10; f++)
7929 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
7931 outs() << "\t mmst_rsrv ";
7932 for (f = 0; f < 6; f++)
7933 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
7937 static void Print_xmm_reg(MachO::xmm_reg_t &r) {
7939 outs() << "\t xmm_reg ";
7940 for (f = 0; f < 16; f++)
7941 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
7945 static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
7946 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
7947 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
7948 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
7949 outs() << " denorm " << fpu.fpu_fcw.denorm;
7950 outs() << " zdiv " << fpu.fpu_fcw.zdiv;
7951 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
7952 outs() << " undfl " << fpu.fpu_fcw.undfl;
7953 outs() << " precis " << fpu.fpu_fcw.precis << "\n";
7954 outs() << "\t\t pc ";
7955 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
7956 outs() << "FP_PREC_24B ";
7957 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
7958 outs() << "FP_PREC_53B ";
7959 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
7960 outs() << "FP_PREC_64B ";
7962 outs() << fpu.fpu_fcw.pc << " ";
7964 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
7965 outs() << "FP_RND_NEAR ";
7966 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
7967 outs() << "FP_RND_DOWN ";
7968 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
7969 outs() << "FP_RND_UP ";
7970 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
7971 outs() << "FP_CHOP ";
7973 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
7974 outs() << " denorm " << fpu.fpu_fsw.denorm;
7975 outs() << " zdiv " << fpu.fpu_fsw.zdiv;
7976 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
7977 outs() << " undfl " << fpu.fpu_fsw.undfl;
7978 outs() << " precis " << fpu.fpu_fsw.precis;
7979 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
7980 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
7981 outs() << " c0 " << fpu.fpu_fsw.c0;
7982 outs() << " c1 " << fpu.fpu_fsw.c1;
7983 outs() << " c2 " << fpu.fpu_fsw.c2;
7984 outs() << " tos " << fpu.fpu_fsw.tos;
7985 outs() << " c3 " << fpu.fpu_fsw.c3;
7986 outs() << " busy " << fpu.fpu_fsw.busy << "\n";
7987 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
7988 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
7989 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
7990 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
7991 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
7992 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
7993 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
7994 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
7995 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
7996 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
7997 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
7999 outs() << "\t fpu_stmm0:\n";
8000 Print_mmst_reg(fpu.fpu_stmm0);
8001 outs() << "\t fpu_stmm1:\n";
8002 Print_mmst_reg(fpu.fpu_stmm1);
8003 outs() << "\t fpu_stmm2:\n";
8004 Print_mmst_reg(fpu.fpu_stmm2);
8005 outs() << "\t fpu_stmm3:\n";
8006 Print_mmst_reg(fpu.fpu_stmm3);
8007 outs() << "\t fpu_stmm4:\n";
8008 Print_mmst_reg(fpu.fpu_stmm4);
8009 outs() << "\t fpu_stmm5:\n";
8010 Print_mmst_reg(fpu.fpu_stmm5);
8011 outs() << "\t fpu_stmm6:\n";
8012 Print_mmst_reg(fpu.fpu_stmm6);
8013 outs() << "\t fpu_stmm7:\n";
8014 Print_mmst_reg(fpu.fpu_stmm7);
8015 outs() << "\t fpu_xmm0:\n";
8016 Print_xmm_reg(fpu.fpu_xmm0);
8017 outs() << "\t fpu_xmm1:\n";
8018 Print_xmm_reg(fpu.fpu_xmm1);
8019 outs() << "\t fpu_xmm2:\n";
8020 Print_xmm_reg(fpu.fpu_xmm2);
8021 outs() << "\t fpu_xmm3:\n";
8022 Print_xmm_reg(fpu.fpu_xmm3);
8023 outs() << "\t fpu_xmm4:\n";
8024 Print_xmm_reg(fpu.fpu_xmm4);
8025 outs() << "\t fpu_xmm5:\n";
8026 Print_xmm_reg(fpu.fpu_xmm5);
8027 outs() << "\t fpu_xmm6:\n";
8028 Print_xmm_reg(fpu.fpu_xmm6);
8029 outs() << "\t fpu_xmm7:\n";
8030 Print_xmm_reg(fpu.fpu_xmm7);
8031 outs() << "\t fpu_xmm8:\n";
8032 Print_xmm_reg(fpu.fpu_xmm8);
8033 outs() << "\t fpu_xmm9:\n";
8034 Print_xmm_reg(fpu.fpu_xmm9);
8035 outs() << "\t fpu_xmm10:\n";
8036 Print_xmm_reg(fpu.fpu_xmm10);
8037 outs() << "\t fpu_xmm11:\n";
8038 Print_xmm_reg(fpu.fpu_xmm11);
8039 outs() << "\t fpu_xmm12:\n";
8040 Print_xmm_reg(fpu.fpu_xmm12);
8041 outs() << "\t fpu_xmm13:\n";
8042 Print_xmm_reg(fpu.fpu_xmm13);
8043 outs() << "\t fpu_xmm14:\n";
8044 Print_xmm_reg(fpu.fpu_xmm14);
8045 outs() << "\t fpu_xmm15:\n";
8046 Print_xmm_reg(fpu.fpu_xmm15);
8047 outs() << "\t fpu_rsrv4:\n";
8048 for (uint32_t f = 0; f < 6; f++) {
8050 for (uint32_t g = 0; g < 16; g++)
8051 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
8054 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
8058 static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
8059 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
8060 outs() << " err " << format("0x%08" PRIx32, exc64.err);
8061 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
8064 static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
8065 bool isLittleEndian, uint32_t cputype) {
8066 if (t.cmd == MachO::LC_THREAD)
8067 outs() << " cmd LC_THREAD\n";
8068 else if (t.cmd == MachO::LC_UNIXTHREAD)
8069 outs() << " cmd LC_UNIXTHREAD\n";
8071 outs() << " cmd " << t.cmd << " (unknown)\n";
8072 outs() << " cmdsize " << t.cmdsize;
8073 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
8074 outs() << " Incorrect size\n";
8078 const char *begin = Ptr + sizeof(struct MachO::thread_command);
8079 const char *end = Ptr + t.cmdsize;
8080 uint32_t flavor, count, left;
8081 if (cputype == MachO::CPU_TYPE_X86_64) {
8082 while (begin < end) {
8083 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8084 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8085 begin += sizeof(uint32_t);
8090 if (isLittleEndian != sys::IsLittleEndianHost)
8091 sys::swapByteOrder(flavor);
8092 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8093 memcpy((char *)&count, begin, sizeof(uint32_t));
8094 begin += sizeof(uint32_t);
8099 if (isLittleEndian != sys::IsLittleEndianHost)
8100 sys::swapByteOrder(count);
8101 if (flavor == MachO::x86_THREAD_STATE64) {
8102 outs() << " flavor x86_THREAD_STATE64\n";
8103 if (count == MachO::x86_THREAD_STATE64_COUNT)
8104 outs() << " count x86_THREAD_STATE64_COUNT\n";
8106 outs() << " count " << count
8107 << " (not x86_THREAD_STATE64_COUNT)\n";
8108 MachO::x86_thread_state64_t cpu64;
8110 if (left >= sizeof(MachO::x86_thread_state64_t)) {
8111 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
8112 begin += sizeof(MachO::x86_thread_state64_t);
8114 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
8115 memcpy(&cpu64, begin, left);
8118 if (isLittleEndian != sys::IsLittleEndianHost)
8120 Print_x86_thread_state64_t(cpu64);
8121 } else if (flavor == MachO::x86_THREAD_STATE) {
8122 outs() << " flavor x86_THREAD_STATE\n";
8123 if (count == MachO::x86_THREAD_STATE_COUNT)
8124 outs() << " count x86_THREAD_STATE_COUNT\n";
8126 outs() << " count " << count
8127 << " (not x86_THREAD_STATE_COUNT)\n";
8128 struct MachO::x86_thread_state_t ts;
8130 if (left >= sizeof(MachO::x86_thread_state_t)) {
8131 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
8132 begin += sizeof(MachO::x86_thread_state_t);
8134 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
8135 memcpy(&ts, begin, left);
8138 if (isLittleEndian != sys::IsLittleEndianHost)
8140 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
8141 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
8142 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
8143 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
8145 outs() << "tsh.count " << ts.tsh.count
8146 << " (not x86_THREAD_STATE64_COUNT\n";
8147 Print_x86_thread_state64_t(ts.uts.ts64);
8149 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
8150 << ts.tsh.count << "\n";
8152 } else if (flavor == MachO::x86_FLOAT_STATE) {
8153 outs() << " flavor x86_FLOAT_STATE\n";
8154 if (count == MachO::x86_FLOAT_STATE_COUNT)
8155 outs() << " count x86_FLOAT_STATE_COUNT\n";
8157 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
8158 struct MachO::x86_float_state_t fs;
8160 if (left >= sizeof(MachO::x86_float_state_t)) {
8161 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
8162 begin += sizeof(MachO::x86_float_state_t);
8164 memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
8165 memcpy(&fs, begin, left);
8168 if (isLittleEndian != sys::IsLittleEndianHost)
8170 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
8171 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
8172 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
8173 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
8175 outs() << "fsh.count " << fs.fsh.count
8176 << " (not x86_FLOAT_STATE64_COUNT\n";
8177 Print_x86_float_state_t(fs.ufs.fs64);
8179 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
8180 << fs.fsh.count << "\n";
8182 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
8183 outs() << " flavor x86_EXCEPTION_STATE\n";
8184 if (count == MachO::x86_EXCEPTION_STATE_COUNT)
8185 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
8187 outs() << " count " << count
8188 << " (not x86_EXCEPTION_STATE_COUNT)\n";
8189 struct MachO::x86_exception_state_t es;
8191 if (left >= sizeof(MachO::x86_exception_state_t)) {
8192 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
8193 begin += sizeof(MachO::x86_exception_state_t);
8195 memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
8196 memcpy(&es, begin, left);
8199 if (isLittleEndian != sys::IsLittleEndianHost)
8201 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
8202 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
8203 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
8204 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
8206 outs() << "\t esh.count " << es.esh.count
8207 << " (not x86_EXCEPTION_STATE64_COUNT\n";
8208 Print_x86_exception_state_t(es.ues.es64);
8210 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
8211 << es.esh.count << "\n";
8214 outs() << " flavor " << flavor << " (unknown)\n";
8215 outs() << " count " << count << "\n";
8216 outs() << " state (unknown)\n";
8217 begin += count * sizeof(uint32_t);
8221 while (begin < end) {
8222 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8223 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8224 begin += sizeof(uint32_t);
8229 if (isLittleEndian != sys::IsLittleEndianHost)
8230 sys::swapByteOrder(flavor);
8231 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8232 memcpy((char *)&count, begin, sizeof(uint32_t));
8233 begin += sizeof(uint32_t);
8238 if (isLittleEndian != sys::IsLittleEndianHost)
8239 sys::swapByteOrder(count);
8240 outs() << " flavor " << flavor << "\n";
8241 outs() << " count " << count << "\n";
8242 outs() << " state (Unknown cputype/cpusubtype)\n";
8243 begin += count * sizeof(uint32_t);
8248 static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
8249 if (dl.cmd == MachO::LC_ID_DYLIB)
8250 outs() << " cmd LC_ID_DYLIB\n";
8251 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
8252 outs() << " cmd LC_LOAD_DYLIB\n";
8253 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
8254 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
8255 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
8256 outs() << " cmd LC_REEXPORT_DYLIB\n";
8257 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
8258 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
8259 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
8260 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
8262 outs() << " cmd " << dl.cmd << " (unknown)\n";
8263 outs() << " cmdsize " << dl.cmdsize;
8264 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
8265 outs() << " Incorrect size\n";
8268 if (dl.dylib.name < dl.cmdsize) {
8269 const char *P = (const char *)(Ptr) + dl.dylib.name;
8270 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
8272 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
8274 outs() << " time stamp " << dl.dylib.timestamp << " ";
8275 time_t t = dl.dylib.timestamp;
8276 outs() << ctime(&t);
8277 outs() << " current version ";
8278 if (dl.dylib.current_version == 0xffffffff)
8281 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
8282 << ((dl.dylib.current_version >> 8) & 0xff) << "."
8283 << (dl.dylib.current_version & 0xff) << "\n";
8284 outs() << "compatibility version ";
8285 if (dl.dylib.compatibility_version == 0xffffffff)
8288 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
8289 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
8290 << (dl.dylib.compatibility_version & 0xff) << "\n";
8293 static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
8294 uint32_t object_size) {
8295 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
8296 outs() << " cmd LC_FUNCTION_STARTS\n";
8297 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
8298 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
8299 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
8300 outs() << " cmd LC_FUNCTION_STARTS\n";
8301 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
8302 outs() << " cmd LC_DATA_IN_CODE\n";
8303 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
8304 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
8305 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
8306 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
8308 outs() << " cmd " << ld.cmd << " (?)\n";
8309 outs() << " cmdsize " << ld.cmdsize;
8310 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
8311 outs() << " Incorrect size\n";
8314 outs() << " dataoff " << ld.dataoff;
8315 if (ld.dataoff > object_size)
8316 outs() << " (past end of file)\n";
8319 outs() << " datasize " << ld.datasize;
8320 uint64_t big_size = ld.dataoff;
8321 big_size += ld.datasize;
8322 if (big_size > object_size)
8323 outs() << " (past end of file)\n";
8328 static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
8329 uint32_t cputype, bool verbose) {
8330 StringRef Buf = Obj->getData();
8332 for (const auto &Command : Obj->load_commands()) {
8333 outs() << "Load command " << Index++ << "\n";
8334 if (Command.C.cmd == MachO::LC_SEGMENT) {
8335 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
8336 const char *sg_segname = SLC.segname;
8337 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
8338 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
8339 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
8341 for (unsigned j = 0; j < SLC.nsects; j++) {
8342 MachO::section S = Obj->getSection(Command, j);
8343 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
8344 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
8345 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
8347 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
8348 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
8349 const char *sg_segname = SLC_64.segname;
8350 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
8351 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
8352 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
8353 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
8354 for (unsigned j = 0; j < SLC_64.nsects; j++) {
8355 MachO::section_64 S_64 = Obj->getSection64(Command, j);
8356 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
8357 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
8358 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
8359 sg_segname, filetype, Buf.size(), verbose);
8361 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
8362 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
8363 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
8364 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
8365 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
8366 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
8367 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
8369 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
8370 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
8371 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
8372 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
8373 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
8374 Command.C.cmd == MachO::LC_ID_DYLINKER ||
8375 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
8376 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
8377 PrintDyldLoadCommand(Dyld, Command.Ptr);
8378 } else if (Command.C.cmd == MachO::LC_UUID) {
8379 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
8380 PrintUuidLoadCommand(Uuid);
8381 } else if (Command.C.cmd == MachO::LC_RPATH) {
8382 MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
8383 PrintRpathLoadCommand(Rpath, Command.Ptr);
8384 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
8385 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS) {
8386 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
8387 PrintVersionMinLoadCommand(Vd);
8388 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
8389 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
8390 PrintSourceVersionCommand(Sd);
8391 } else if (Command.C.cmd == MachO::LC_MAIN) {
8392 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
8393 PrintEntryPointCommand(Ep);
8394 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
8395 MachO::encryption_info_command Ei =
8396 Obj->getEncryptionInfoCommand(Command);
8397 PrintEncryptionInfoCommand(Ei, Buf.size());
8398 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
8399 MachO::encryption_info_command_64 Ei =
8400 Obj->getEncryptionInfoCommand64(Command);
8401 PrintEncryptionInfoCommand64(Ei, Buf.size());
8402 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
8403 MachO::linker_option_command Lo =
8404 Obj->getLinkerOptionLoadCommand(Command);
8405 PrintLinkerOptionCommand(Lo, Command.Ptr);
8406 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
8407 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
8408 PrintSubFrameworkCommand(Sf, Command.Ptr);
8409 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
8410 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
8411 PrintSubUmbrellaCommand(Sf, Command.Ptr);
8412 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
8413 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
8414 PrintSubLibraryCommand(Sl, Command.Ptr);
8415 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
8416 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
8417 PrintSubClientCommand(Sc, Command.Ptr);
8418 } else if (Command.C.cmd == MachO::LC_ROUTINES) {
8419 MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
8420 PrintRoutinesCommand(Rc);
8421 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
8422 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
8423 PrintRoutinesCommand64(Rc);
8424 } else if (Command.C.cmd == MachO::LC_THREAD ||
8425 Command.C.cmd == MachO::LC_UNIXTHREAD) {
8426 MachO::thread_command Tc = Obj->getThreadCommand(Command);
8427 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
8428 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
8429 Command.C.cmd == MachO::LC_ID_DYLIB ||
8430 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
8431 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
8432 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
8433 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
8434 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
8435 PrintDylibCommand(Dl, Command.Ptr);
8436 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
8437 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
8438 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
8439 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
8440 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
8441 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
8442 MachO::linkedit_data_command Ld =
8443 Obj->getLinkeditDataLoadCommand(Command);
8444 PrintLinkEditDataCommand(Ld, Buf.size());
8446 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
8448 outs() << " cmdsize " << Command.C.cmdsize << "\n";
8449 // TODO: get and print the raw bytes of the load command.
8451 // TODO: print all the other kinds of load commands.
8455 static void getAndPrintMachHeader(const MachOObjectFile *Obj,
8456 uint32_t &filetype, uint32_t &cputype,
8458 if (Obj->is64Bit()) {
8459 MachO::mach_header_64 H_64;
8460 H_64 = Obj->getHeader64();
8461 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
8462 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
8463 filetype = H_64.filetype;
8464 cputype = H_64.cputype;
8466 MachO::mach_header H;
8467 H = Obj->getHeader();
8468 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
8469 H.sizeofcmds, H.flags, verbose);
8470 filetype = H.filetype;
8471 cputype = H.cputype;
8475 void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
8476 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
8477 uint32_t filetype = 0;
8478 uint32_t cputype = 0;
8479 getAndPrintMachHeader(file, filetype, cputype, !NonVerbose);
8480 PrintLoadCommands(file, filetype, cputype, !NonVerbose);
8483 //===----------------------------------------------------------------------===//
8484 // export trie dumping
8485 //===----------------------------------------------------------------------===//
8487 void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
8488 for (const llvm::object::ExportEntry &Entry : Obj->exports()) {
8489 uint64_t Flags = Entry.flags();
8490 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
8491 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
8492 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8493 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
8494 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8495 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
8496 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
8498 outs() << "[re-export] ";
8500 outs() << format("0x%08llX ",
8501 Entry.address()); // FIXME:add in base address
8502 outs() << Entry.name();
8503 if (WeakDef || ThreadLocal || Resolver || Abs) {
8504 bool NeedsComma = false;
8507 outs() << "weak_def";
8513 outs() << "per-thread";
8519 outs() << "absolute";
8525 outs() << format("resolver=0x%08llX", Entry.other());
8531 StringRef DylibName = "unknown";
8532 int Ordinal = Entry.other() - 1;
8533 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
8534 if (Entry.otherName().empty())
8535 outs() << " (from " << DylibName << ")";
8537 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
8543 //===----------------------------------------------------------------------===//
8544 // rebase table dumping
8545 //===----------------------------------------------------------------------===//
8550 SegInfo(const object::MachOObjectFile *Obj);
8552 StringRef segmentName(uint32_t SegIndex);
8553 StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset);
8554 uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
8557 struct SectionInfo {
8560 StringRef SectionName;
8561 StringRef SegmentName;
8562 uint64_t OffsetInSegment;
8563 uint64_t SegmentStartAddress;
8564 uint32_t SegmentIndex;
8566 const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset);
8567 SmallVector<SectionInfo, 32> Sections;
8571 SegInfo::SegInfo(const object::MachOObjectFile *Obj) {
8572 // Build table of sections so segIndex/offset pairs can be translated.
8573 uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
8574 StringRef CurSegName;
8575 uint64_t CurSegAddress;
8576 for (const SectionRef &Section : Obj->sections()) {
8578 if (error(Section.getName(Info.SectionName)))
8580 Info.Address = Section.getAddress();
8581 Info.Size = Section.getSize();
8583 Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
8584 if (!Info.SegmentName.equals(CurSegName)) {
8586 CurSegName = Info.SegmentName;
8587 CurSegAddress = Info.Address;
8589 Info.SegmentIndex = CurSegIndex - 1;
8590 Info.OffsetInSegment = Info.Address - CurSegAddress;
8591 Info.SegmentStartAddress = CurSegAddress;
8592 Sections.push_back(Info);
8596 StringRef SegInfo::segmentName(uint32_t SegIndex) {
8597 for (const SectionInfo &SI : Sections) {
8598 if (SI.SegmentIndex == SegIndex)
8599 return SI.SegmentName;
8601 llvm_unreachable("invalid segIndex");
8604 const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex,
8605 uint64_t OffsetInSeg) {
8606 for (const SectionInfo &SI : Sections) {
8607 if (SI.SegmentIndex != SegIndex)
8609 if (SI.OffsetInSegment > OffsetInSeg)
8611 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
8615 llvm_unreachable("segIndex and offset not in any section");
8618 StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) {
8619 return findSection(SegIndex, OffsetInSeg).SectionName;
8622 uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) {
8623 const SectionInfo &SI = findSection(SegIndex, OffsetInSeg);
8624 return SI.SegmentStartAddress + OffsetInSeg;
8627 void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) {
8628 // Build table of sections so names can used in final output.
8629 SegInfo sectionTable(Obj);
8631 outs() << "segment section address type\n";
8632 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) {
8633 uint32_t SegIndex = Entry.segmentIndex();
8634 uint64_t OffsetInSeg = Entry.segmentOffset();
8635 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8636 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8637 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8639 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
8640 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
8641 SegmentName.str().c_str(), SectionName.str().c_str(),
8642 Address, Entry.typeName().str().c_str());
8646 static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
8647 StringRef DylibName;
8649 case MachO::BIND_SPECIAL_DYLIB_SELF:
8650 return "this-image";
8651 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
8652 return "main-executable";
8653 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
8654 return "flat-namespace";
8657 std::error_code EC =
8658 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
8660 return "<<bad library ordinal>>";
8664 return "<<unknown special ordinal>>";
8667 //===----------------------------------------------------------------------===//
8668 // bind table dumping
8669 //===----------------------------------------------------------------------===//
8671 void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) {
8672 // Build table of sections so names can used in final output.
8673 SegInfo sectionTable(Obj);
8675 outs() << "segment section address type "
8676 "addend dylib symbol\n";
8677 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) {
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 __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
8687 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
8688 Attr = " (weak_import)";
8689 outs() << left_justify(SegmentName, 8) << " "
8690 << left_justify(SectionName, 18) << " "
8691 << format_hex(Address, 10, true) << " "
8692 << left_justify(Entry.typeName(), 8) << " "
8693 << format_decimal(Entry.addend(), 8) << " "
8694 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
8695 << Entry.symbolName() << Attr << "\n";
8699 //===----------------------------------------------------------------------===//
8700 // lazy bind table dumping
8701 //===----------------------------------------------------------------------===//
8703 void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) {
8704 // Build table of sections so names can used in final output.
8705 SegInfo sectionTable(Obj);
8707 outs() << "segment section address "
8709 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) {
8710 uint32_t SegIndex = Entry.segmentIndex();
8711 uint64_t OffsetInSeg = Entry.segmentOffset();
8712 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8713 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8714 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8716 // Table lines look like:
8717 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
8718 outs() << left_justify(SegmentName, 8) << " "
8719 << left_justify(SectionName, 18) << " "
8720 << format_hex(Address, 10, true) << " "
8721 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
8722 << Entry.symbolName() << "\n";
8726 //===----------------------------------------------------------------------===//
8727 // weak bind table dumping
8728 //===----------------------------------------------------------------------===//
8730 void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) {
8731 // Build table of sections so names can used in final output.
8732 SegInfo sectionTable(Obj);
8734 outs() << "segment section address "
8735 "type addend symbol\n";
8736 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) {
8737 // Strong symbols don't have a location to update.
8738 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
8739 outs() << " strong "
8740 << Entry.symbolName() << "\n";
8743 uint32_t SegIndex = Entry.segmentIndex();
8744 uint64_t OffsetInSeg = Entry.segmentOffset();
8745 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8746 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8747 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8749 // Table lines look like:
8750 // __DATA __data 0x00001000 pointer 0 _foo
8751 outs() << left_justify(SegmentName, 8) << " "
8752 << left_justify(SectionName, 18) << " "
8753 << format_hex(Address, 10, true) << " "
8754 << left_justify(Entry.typeName(), 8) << " "
8755 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
8760 // get_dyld_bind_info_symbolname() is used for disassembly and passed an
8761 // address, ReferenceValue, in the Mach-O file and looks in the dyld bind
8762 // information for that address. If the address is found its binding symbol
8763 // name is returned. If not nullptr is returned.
8764 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
8765 struct DisassembleInfo *info) {
8766 if (info->bindtable == nullptr) {
8767 info->bindtable = new (BindTable);
8768 SegInfo sectionTable(info->O);
8769 for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable()) {
8770 uint32_t SegIndex = Entry.segmentIndex();
8771 uint64_t OffsetInSeg = Entry.segmentOffset();
8772 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8773 const char *SymbolName = nullptr;
8774 StringRef name = Entry.symbolName();
8776 SymbolName = name.data();
8777 info->bindtable->push_back(std::make_pair(Address, SymbolName));
8780 for (bind_table_iterator BI = info->bindtable->begin(),
8781 BE = info->bindtable->end();
8783 uint64_t Address = BI->first;
8784 if (ReferenceValue == Address) {
8785 const char *SymbolName = BI->second;