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 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.
2421 get_symbol_64(uint32_t sect_offset, SectionRef S, DisassembleInfo *info,
2423 uint64_t ReferenceValue = UnknownAddressOrSize) {
2428 // See if there is an external relocation entry at the sect_offset.
2429 bool reloc_found = false;
2431 MachO::any_relocation_info RE;
2432 bool isExtern = false;
2434 for (const RelocationRef &Reloc : S.relocations()) {
2435 uint64_t RelocOffset;
2436 Reloc.getOffset(RelocOffset);
2437 if (RelocOffset == sect_offset) {
2438 Rel = Reloc.getRawDataRefImpl();
2439 RE = info->O->getRelocation(Rel);
2440 if (info->O->isRelocationScattered(RE))
2442 isExtern = info->O->getPlainRelocationExternal(RE);
2444 symbol_iterator RelocSym = Reloc.getSymbol();
2451 // If there is an external relocation entry for a symbol in this section
2452 // at this section_offset then use that symbol's value for the n_value
2453 // and return its name.
2454 const char *SymbolName = nullptr;
2455 if (reloc_found && isExtern) {
2456 Symbol.getAddress(n_value);
2457 if (n_value == UnknownAddressOrSize)
2460 Symbol.getName(name);
2461 if (!name.empty()) {
2462 SymbolName = name.data();
2467 // TODO: For fully linked images, look through the external relocation
2468 // entries off the dynamic symtab command. For these the r_offset is from the
2469 // start of the first writeable segment in the Mach-O file. So the offset
2470 // to this section from that segment is passed to this routine by the caller,
2471 // as the database_offset. Which is the difference of the section's starting
2472 // address and the first writable segment.
2474 // NOTE: need add passing the database_offset to this routine.
2476 // We did not find an external relocation entry so look up the ReferenceValue
2477 // as an address of a symbol and if found return that symbol's name.
2478 if (ReferenceValue != UnknownAddressOrSize)
2479 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
2484 static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
2485 DisassembleInfo *info,
2486 uint32_t ReferenceValue) {
2488 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
2491 // These are structs in the Objective-C meta data and read to produce the
2492 // comments for disassembly. While these are part of the ABI they are no
2493 // public defintions. So the are here not in include/llvm/Support/MachO.h .
2495 // The cfstring object in a 64-bit Mach-O file.
2496 struct cfstring64_t {
2497 uint64_t isa; // class64_t * (64-bit pointer)
2498 uint64_t flags; // flag bits
2499 uint64_t characters; // char * (64-bit pointer)
2500 uint64_t length; // number of non-NULL characters in above
2503 // The class object in a 64-bit Mach-O file.
2505 uint64_t isa; // class64_t * (64-bit pointer)
2506 uint64_t superclass; // class64_t * (64-bit pointer)
2507 uint64_t cache; // Cache (64-bit pointer)
2508 uint64_t vtable; // IMP * (64-bit pointer)
2509 uint64_t data; // class_ro64_t * (64-bit pointer)
2513 uint32_t isa; /* class32_t * (32-bit pointer) */
2514 uint32_t superclass; /* class32_t * (32-bit pointer) */
2515 uint32_t cache; /* Cache (32-bit pointer) */
2516 uint32_t vtable; /* IMP * (32-bit pointer) */
2517 uint32_t data; /* class_ro32_t * (32-bit pointer) */
2520 struct class_ro64_t {
2522 uint32_t instanceStart;
2523 uint32_t instanceSize;
2525 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
2526 uint64_t name; // const char * (64-bit pointer)
2527 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
2528 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
2529 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
2530 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
2531 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
2534 struct class_ro32_t {
2536 uint32_t instanceStart;
2537 uint32_t instanceSize;
2538 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
2539 uint32_t name; /* const char * (32-bit pointer) */
2540 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
2541 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
2542 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
2543 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
2544 uint32_t baseProperties; /* const struct objc_property_list *
2548 /* Values for class_ro{64,32}_t->flags */
2549 #define RO_META (1 << 0)
2550 #define RO_ROOT (1 << 1)
2551 #define RO_HAS_CXX_STRUCTORS (1 << 2)
2553 struct method_list64_t {
2556 /* struct method64_t first; These structures follow inline */
2559 struct method_list32_t {
2562 /* struct method32_t first; These structures follow inline */
2566 uint64_t name; /* SEL (64-bit pointer) */
2567 uint64_t types; /* const char * (64-bit pointer) */
2568 uint64_t imp; /* IMP (64-bit pointer) */
2572 uint32_t name; /* SEL (32-bit pointer) */
2573 uint32_t types; /* const char * (32-bit pointer) */
2574 uint32_t imp; /* IMP (32-bit pointer) */
2577 struct protocol_list64_t {
2578 uint64_t count; /* uintptr_t (a 64-bit value) */
2579 /* struct protocol64_t * list[0]; These pointers follow inline */
2582 struct protocol_list32_t {
2583 uint32_t count; /* uintptr_t (a 32-bit value) */
2584 /* struct protocol32_t * list[0]; These pointers follow inline */
2587 struct protocol64_t {
2588 uint64_t isa; /* id * (64-bit pointer) */
2589 uint64_t name; /* const char * (64-bit pointer) */
2590 uint64_t protocols; /* struct protocol_list64_t *
2592 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
2593 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
2594 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
2595 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
2596 uint64_t instanceProperties; /* struct objc_property_list *
2600 struct protocol32_t {
2601 uint32_t isa; /* id * (32-bit pointer) */
2602 uint32_t name; /* const char * (32-bit pointer) */
2603 uint32_t protocols; /* struct protocol_list_t *
2605 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
2606 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
2607 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
2608 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
2609 uint32_t instanceProperties; /* struct objc_property_list *
2613 struct ivar_list64_t {
2616 /* struct ivar64_t first; These structures follow inline */
2619 struct ivar_list32_t {
2622 /* struct ivar32_t first; These structures follow inline */
2626 uint64_t offset; /* uintptr_t * (64-bit pointer) */
2627 uint64_t name; /* const char * (64-bit pointer) */
2628 uint64_t type; /* const char * (64-bit pointer) */
2634 uint32_t offset; /* uintptr_t * (32-bit pointer) */
2635 uint32_t name; /* const char * (32-bit pointer) */
2636 uint32_t type; /* const char * (32-bit pointer) */
2641 struct objc_property_list64 {
2644 /* struct objc_property64 first; These structures follow inline */
2647 struct objc_property_list32 {
2650 /* struct objc_property32 first; These structures follow inline */
2653 struct objc_property64 {
2654 uint64_t name; /* const char * (64-bit pointer) */
2655 uint64_t attributes; /* const char * (64-bit pointer) */
2658 struct objc_property32 {
2659 uint32_t name; /* const char * (32-bit pointer) */
2660 uint32_t attributes; /* const char * (32-bit pointer) */
2663 struct category64_t {
2664 uint64_t name; /* const char * (64-bit pointer) */
2665 uint64_t cls; /* struct class_t * (64-bit pointer) */
2666 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
2667 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
2668 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
2669 uint64_t instanceProperties; /* struct objc_property_list *
2673 struct category32_t {
2674 uint32_t name; /* const char * (32-bit pointer) */
2675 uint32_t cls; /* struct class_t * (32-bit pointer) */
2676 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
2677 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
2678 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
2679 uint32_t instanceProperties; /* struct objc_property_list *
2683 struct objc_image_info64 {
2687 struct objc_image_info32 {
2691 struct imageInfo_t {
2695 /* masks for objc_image_info.flags */
2696 #define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
2697 #define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
2699 struct message_ref64 {
2700 uint64_t imp; /* IMP (64-bit pointer) */
2701 uint64_t sel; /* SEL (64-bit pointer) */
2704 struct message_ref32 {
2705 uint32_t imp; /* IMP (32-bit pointer) */
2706 uint32_t sel; /* SEL (32-bit pointer) */
2709 // Objective-C 1 (32-bit only) meta data structs.
2711 struct objc_module_t {
2714 uint32_t name; /* char * (32-bit pointer) */
2715 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
2718 struct objc_symtab_t {
2719 uint32_t sel_ref_cnt;
2720 uint32_t refs; /* SEL * (32-bit pointer) */
2721 uint16_t cls_def_cnt;
2722 uint16_t cat_def_cnt;
2723 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
2726 struct objc_class_t {
2727 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2728 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
2729 uint32_t name; /* const char * (32-bit pointer) */
2732 int32_t instance_size;
2733 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
2734 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
2735 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
2736 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
2739 #define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
2740 // class is not a metaclass
2741 #define CLS_CLASS 0x1
2742 // class is a metaclass
2743 #define CLS_META 0x2
2745 struct objc_category_t {
2746 uint32_t category_name; /* char * (32-bit pointer) */
2747 uint32_t class_name; /* char * (32-bit pointer) */
2748 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
2749 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
2750 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
2753 struct objc_ivar_t {
2754 uint32_t ivar_name; /* char * (32-bit pointer) */
2755 uint32_t ivar_type; /* char * (32-bit pointer) */
2756 int32_t ivar_offset;
2759 struct objc_ivar_list_t {
2761 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
2764 struct objc_method_list_t {
2765 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
2766 int32_t method_count;
2767 // struct objc_method_t method_list[1]; /* variable length structure */
2770 struct objc_method_t {
2771 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2772 uint32_t method_types; /* char * (32-bit pointer) */
2773 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
2777 struct objc_protocol_list_t {
2778 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
2780 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
2781 // (32-bit pointer) */
2784 struct objc_protocol_t {
2785 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2786 uint32_t protocol_name; /* char * (32-bit pointer) */
2787 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
2788 uint32_t instance_methods; /* struct objc_method_description_list *
2790 uint32_t class_methods; /* struct objc_method_description_list *
2794 struct objc_method_description_list_t {
2796 // struct objc_method_description_t list[1];
2799 struct objc_method_description_t {
2800 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2801 uint32_t types; /* char * (32-bit pointer) */
2804 inline void swapStruct(struct cfstring64_t &cfs) {
2805 sys::swapByteOrder(cfs.isa);
2806 sys::swapByteOrder(cfs.flags);
2807 sys::swapByteOrder(cfs.characters);
2808 sys::swapByteOrder(cfs.length);
2811 inline void swapStruct(struct class64_t &c) {
2812 sys::swapByteOrder(c.isa);
2813 sys::swapByteOrder(c.superclass);
2814 sys::swapByteOrder(c.cache);
2815 sys::swapByteOrder(c.vtable);
2816 sys::swapByteOrder(c.data);
2819 inline void swapStruct(struct class32_t &c) {
2820 sys::swapByteOrder(c.isa);
2821 sys::swapByteOrder(c.superclass);
2822 sys::swapByteOrder(c.cache);
2823 sys::swapByteOrder(c.vtable);
2824 sys::swapByteOrder(c.data);
2827 inline void swapStruct(struct class_ro64_t &cro) {
2828 sys::swapByteOrder(cro.flags);
2829 sys::swapByteOrder(cro.instanceStart);
2830 sys::swapByteOrder(cro.instanceSize);
2831 sys::swapByteOrder(cro.reserved);
2832 sys::swapByteOrder(cro.ivarLayout);
2833 sys::swapByteOrder(cro.name);
2834 sys::swapByteOrder(cro.baseMethods);
2835 sys::swapByteOrder(cro.baseProtocols);
2836 sys::swapByteOrder(cro.ivars);
2837 sys::swapByteOrder(cro.weakIvarLayout);
2838 sys::swapByteOrder(cro.baseProperties);
2841 inline void swapStruct(struct class_ro32_t &cro) {
2842 sys::swapByteOrder(cro.flags);
2843 sys::swapByteOrder(cro.instanceStart);
2844 sys::swapByteOrder(cro.instanceSize);
2845 sys::swapByteOrder(cro.ivarLayout);
2846 sys::swapByteOrder(cro.name);
2847 sys::swapByteOrder(cro.baseMethods);
2848 sys::swapByteOrder(cro.baseProtocols);
2849 sys::swapByteOrder(cro.ivars);
2850 sys::swapByteOrder(cro.weakIvarLayout);
2851 sys::swapByteOrder(cro.baseProperties);
2854 inline void swapStruct(struct method_list64_t &ml) {
2855 sys::swapByteOrder(ml.entsize);
2856 sys::swapByteOrder(ml.count);
2859 inline void swapStruct(struct method_list32_t &ml) {
2860 sys::swapByteOrder(ml.entsize);
2861 sys::swapByteOrder(ml.count);
2864 inline void swapStruct(struct method64_t &m) {
2865 sys::swapByteOrder(m.name);
2866 sys::swapByteOrder(m.types);
2867 sys::swapByteOrder(m.imp);
2870 inline void swapStruct(struct method32_t &m) {
2871 sys::swapByteOrder(m.name);
2872 sys::swapByteOrder(m.types);
2873 sys::swapByteOrder(m.imp);
2876 inline void swapStruct(struct protocol_list64_t &pl) {
2877 sys::swapByteOrder(pl.count);
2880 inline void swapStruct(struct protocol_list32_t &pl) {
2881 sys::swapByteOrder(pl.count);
2884 inline void swapStruct(struct protocol64_t &p) {
2885 sys::swapByteOrder(p.isa);
2886 sys::swapByteOrder(p.name);
2887 sys::swapByteOrder(p.protocols);
2888 sys::swapByteOrder(p.instanceMethods);
2889 sys::swapByteOrder(p.classMethods);
2890 sys::swapByteOrder(p.optionalInstanceMethods);
2891 sys::swapByteOrder(p.optionalClassMethods);
2892 sys::swapByteOrder(p.instanceProperties);
2895 inline void swapStruct(struct protocol32_t &p) {
2896 sys::swapByteOrder(p.isa);
2897 sys::swapByteOrder(p.name);
2898 sys::swapByteOrder(p.protocols);
2899 sys::swapByteOrder(p.instanceMethods);
2900 sys::swapByteOrder(p.classMethods);
2901 sys::swapByteOrder(p.optionalInstanceMethods);
2902 sys::swapByteOrder(p.optionalClassMethods);
2903 sys::swapByteOrder(p.instanceProperties);
2906 inline void swapStruct(struct ivar_list64_t &il) {
2907 sys::swapByteOrder(il.entsize);
2908 sys::swapByteOrder(il.count);
2911 inline void swapStruct(struct ivar_list32_t &il) {
2912 sys::swapByteOrder(il.entsize);
2913 sys::swapByteOrder(il.count);
2916 inline void swapStruct(struct ivar64_t &i) {
2917 sys::swapByteOrder(i.offset);
2918 sys::swapByteOrder(i.name);
2919 sys::swapByteOrder(i.type);
2920 sys::swapByteOrder(i.alignment);
2921 sys::swapByteOrder(i.size);
2924 inline void swapStruct(struct ivar32_t &i) {
2925 sys::swapByteOrder(i.offset);
2926 sys::swapByteOrder(i.name);
2927 sys::swapByteOrder(i.type);
2928 sys::swapByteOrder(i.alignment);
2929 sys::swapByteOrder(i.size);
2932 inline void swapStruct(struct objc_property_list64 &pl) {
2933 sys::swapByteOrder(pl.entsize);
2934 sys::swapByteOrder(pl.count);
2937 inline void swapStruct(struct objc_property_list32 &pl) {
2938 sys::swapByteOrder(pl.entsize);
2939 sys::swapByteOrder(pl.count);
2942 inline void swapStruct(struct objc_property64 &op) {
2943 sys::swapByteOrder(op.name);
2944 sys::swapByteOrder(op.attributes);
2947 inline void swapStruct(struct objc_property32 &op) {
2948 sys::swapByteOrder(op.name);
2949 sys::swapByteOrder(op.attributes);
2952 inline void swapStruct(struct category64_t &c) {
2953 sys::swapByteOrder(c.name);
2954 sys::swapByteOrder(c.cls);
2955 sys::swapByteOrder(c.instanceMethods);
2956 sys::swapByteOrder(c.classMethods);
2957 sys::swapByteOrder(c.protocols);
2958 sys::swapByteOrder(c.instanceProperties);
2961 inline void swapStruct(struct category32_t &c) {
2962 sys::swapByteOrder(c.name);
2963 sys::swapByteOrder(c.cls);
2964 sys::swapByteOrder(c.instanceMethods);
2965 sys::swapByteOrder(c.classMethods);
2966 sys::swapByteOrder(c.protocols);
2967 sys::swapByteOrder(c.instanceProperties);
2970 inline void swapStruct(struct objc_image_info64 &o) {
2971 sys::swapByteOrder(o.version);
2972 sys::swapByteOrder(o.flags);
2975 inline void swapStruct(struct objc_image_info32 &o) {
2976 sys::swapByteOrder(o.version);
2977 sys::swapByteOrder(o.flags);
2980 inline void swapStruct(struct imageInfo_t &o) {
2981 sys::swapByteOrder(o.version);
2982 sys::swapByteOrder(o.flags);
2985 inline void swapStruct(struct message_ref64 &mr) {
2986 sys::swapByteOrder(mr.imp);
2987 sys::swapByteOrder(mr.sel);
2990 inline void swapStruct(struct message_ref32 &mr) {
2991 sys::swapByteOrder(mr.imp);
2992 sys::swapByteOrder(mr.sel);
2995 inline void swapStruct(struct objc_module_t &module) {
2996 sys::swapByteOrder(module.version);
2997 sys::swapByteOrder(module.size);
2998 sys::swapByteOrder(module.name);
2999 sys::swapByteOrder(module.symtab);
3002 inline void swapStruct(struct objc_symtab_t &symtab) {
3003 sys::swapByteOrder(symtab.sel_ref_cnt);
3004 sys::swapByteOrder(symtab.refs);
3005 sys::swapByteOrder(symtab.cls_def_cnt);
3006 sys::swapByteOrder(symtab.cat_def_cnt);
3009 inline void swapStruct(struct objc_class_t &objc_class) {
3010 sys::swapByteOrder(objc_class.isa);
3011 sys::swapByteOrder(objc_class.super_class);
3012 sys::swapByteOrder(objc_class.name);
3013 sys::swapByteOrder(objc_class.version);
3014 sys::swapByteOrder(objc_class.info);
3015 sys::swapByteOrder(objc_class.instance_size);
3016 sys::swapByteOrder(objc_class.ivars);
3017 sys::swapByteOrder(objc_class.methodLists);
3018 sys::swapByteOrder(objc_class.cache);
3019 sys::swapByteOrder(objc_class.protocols);
3022 inline void swapStruct(struct objc_category_t &objc_category) {
3023 sys::swapByteOrder(objc_category.category_name);
3024 sys::swapByteOrder(objc_category.class_name);
3025 sys::swapByteOrder(objc_category.instance_methods);
3026 sys::swapByteOrder(objc_category.class_methods);
3027 sys::swapByteOrder(objc_category.protocols);
3030 inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3031 sys::swapByteOrder(objc_ivar_list.ivar_count);
3034 inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3035 sys::swapByteOrder(objc_ivar.ivar_name);
3036 sys::swapByteOrder(objc_ivar.ivar_type);
3037 sys::swapByteOrder(objc_ivar.ivar_offset);
3040 inline void swapStruct(struct objc_method_list_t &method_list) {
3041 sys::swapByteOrder(method_list.obsolete);
3042 sys::swapByteOrder(method_list.method_count);
3045 inline void swapStruct(struct objc_method_t &method) {
3046 sys::swapByteOrder(method.method_name);
3047 sys::swapByteOrder(method.method_types);
3048 sys::swapByteOrder(method.method_imp);
3051 inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3052 sys::swapByteOrder(protocol_list.next);
3053 sys::swapByteOrder(protocol_list.count);
3056 inline void swapStruct(struct objc_protocol_t &protocol) {
3057 sys::swapByteOrder(protocol.isa);
3058 sys::swapByteOrder(protocol.protocol_name);
3059 sys::swapByteOrder(protocol.protocol_list);
3060 sys::swapByteOrder(protocol.instance_methods);
3061 sys::swapByteOrder(protocol.class_methods);
3064 inline void swapStruct(struct objc_method_description_list_t &mdl) {
3065 sys::swapByteOrder(mdl.count);
3068 inline void swapStruct(struct objc_method_description_t &md) {
3069 sys::swapByteOrder(md.name);
3070 sys::swapByteOrder(md.types);
3073 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3074 struct DisassembleInfo *info);
3076 // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3077 // to an Objective-C class and returns the class name. It is also passed the
3078 // address of the pointer, so when the pointer is zero as it can be in an .o
3079 // file, that is used to look for an external relocation entry with a symbol
3081 static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3082 uint64_t ReferenceValue,
3083 struct DisassembleInfo *info) {
3085 uint32_t offset, left;
3088 // The pointer_value can be 0 in an object file and have a relocation
3089 // entry for the class symbol at the ReferenceValue (the address of the
3091 if (pointer_value == 0) {
3092 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3093 if (r == nullptr || left < sizeof(uint64_t))
3096 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3097 if (symbol_name == nullptr)
3099 const char *class_name = strrchr(symbol_name, '$');
3100 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3101 return class_name + 2;
3106 // The case were the pointer_value is non-zero and points to a class defined
3107 // in this Mach-O file.
3108 r = get_pointer_64(pointer_value, offset, left, S, info);
3109 if (r == nullptr || left < sizeof(struct class64_t))
3112 memcpy(&c, r, sizeof(struct class64_t));
3113 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3117 r = get_pointer_64(c.data, offset, left, S, info);
3118 if (r == nullptr || left < sizeof(struct class_ro64_t))
3120 struct class_ro64_t cro;
3121 memcpy(&cro, r, sizeof(struct class_ro64_t));
3122 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3126 const char *name = get_pointer_64(cro.name, offset, left, S, info);
3130 // get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3131 // pointer to a cfstring and returns its name or nullptr.
3132 static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3133 struct DisassembleInfo *info) {
3134 const char *r, *name;
3135 uint32_t offset, left;
3137 struct cfstring64_t cfs;
3138 uint64_t cfs_characters;
3140 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3141 if (r == nullptr || left < sizeof(struct cfstring64_t))
3143 memcpy(&cfs, r, sizeof(struct cfstring64_t));
3144 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3146 if (cfs.characters == 0) {
3148 const char *symbol_name = get_symbol_64(
3149 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
3150 if (symbol_name == nullptr)
3152 cfs_characters = n_value;
3154 cfs_characters = cfs.characters;
3155 name = get_pointer_64(cfs_characters, offset, left, S, info);
3160 // get_objc2_64bit_selref() is used for disassembly and is passed a the address
3161 // of a pointer to an Objective-C selector reference when the pointer value is
3162 // zero as in a .o file and is likely to have a external relocation entry with
3163 // who's symbol's n_value is the real pointer to the selector name. If that is
3164 // the case the real pointer to the selector name is returned else 0 is
3166 static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3167 struct DisassembleInfo *info) {
3168 uint32_t offset, left;
3171 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3172 if (r == nullptr || left < sizeof(uint64_t))
3175 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3176 if (symbol_name == nullptr)
3181 static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3182 const char *sectname) {
3183 for (const SectionRef &Section : O->sections()) {
3185 Section.getName(SectName);
3186 DataRefImpl Ref = Section.getRawDataRefImpl();
3187 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3188 if (SegName == segname && SectName == sectname)
3191 return SectionRef();
3195 walk_pointer_list_64(const char *listname, const SectionRef S,
3196 MachOObjectFile *O, struct DisassembleInfo *info,
3197 void (*func)(uint64_t, struct DisassembleInfo *info)) {
3198 if (S == SectionRef())
3202 S.getName(SectName);
3203 DataRefImpl Ref = S.getRawDataRefImpl();
3204 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3205 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3208 S.getContents(BytesStr);
3209 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3211 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3212 uint32_t left = S.getSize() - i;
3213 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3215 memcpy(&p, Contents + i, size);
3216 if (i + sizeof(uint64_t) > S.getSize())
3217 outs() << listname << " list pointer extends past end of (" << SegName
3218 << "," << SectName << ") section\n";
3219 outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
3221 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3222 sys::swapByteOrder(p);
3224 uint64_t n_value = 0;
3225 const char *name = get_symbol_64(i, S, info, n_value, p);
3226 if (name == nullptr)
3227 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
3230 outs() << format("0x%" PRIx64, n_value);
3232 outs() << " + " << format("0x%" PRIx64, p);
3234 outs() << format("0x%" PRIx64, p);
3235 if (name != nullptr)
3236 outs() << " " << name;
3246 walk_pointer_list_32(const char *listname, const SectionRef S,
3247 MachOObjectFile *O, struct DisassembleInfo *info,
3248 void (*func)(uint32_t, struct DisassembleInfo *info)) {
3249 if (S == SectionRef())
3253 S.getName(SectName);
3254 DataRefImpl Ref = S.getRawDataRefImpl();
3255 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3256 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3259 S.getContents(BytesStr);
3260 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3262 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
3263 uint32_t left = S.getSize() - i;
3264 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
3266 memcpy(&p, Contents + i, size);
3267 if (i + sizeof(uint32_t) > S.getSize())
3268 outs() << listname << " list pointer extends past end of (" << SegName
3269 << "," << SectName << ") section\n";
3270 uint32_t Address = S.getAddress() + i;
3271 outs() << format("%08" PRIx32, Address) << " ";
3273 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3274 sys::swapByteOrder(p);
3275 outs() << format("0x%" PRIx32, p);
3277 const char *name = get_symbol_32(i, S, info, p);
3278 if (name != nullptr)
3279 outs() << " " << name;
3287 static void print_layout_map(const char *layout_map, uint32_t left) {
3288 outs() << " layout map: ";
3290 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
3293 } while (*layout_map != '\0' && left != 0);
3297 static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
3298 uint32_t offset, left;
3300 const char *layout_map;
3304 layout_map = get_pointer_64(p, offset, left, S, info);
3305 print_layout_map(layout_map, left);
3308 static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
3309 uint32_t offset, left;
3311 const char *layout_map;
3315 layout_map = get_pointer_32(p, offset, left, S, info);
3316 print_layout_map(layout_map, left);
3319 static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
3320 const char *indent) {
3321 struct method_list64_t ml;
3322 struct method64_t m;
3324 uint32_t offset, xoffset, left, i;
3326 const char *name, *sym_name;
3329 r = get_pointer_64(p, offset, left, S, info);
3332 memset(&ml, '\0', sizeof(struct method_list64_t));
3333 if (left < sizeof(struct method_list64_t)) {
3334 memcpy(&ml, r, left);
3335 outs() << " (method_list_t entends past the end of the section)\n";
3337 memcpy(&ml, r, sizeof(struct method_list64_t));
3338 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3340 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3341 outs() << indent << "\t\t count " << ml.count << "\n";
3343 p += sizeof(struct method_list64_t);
3344 offset += sizeof(struct method_list64_t);
3345 for (i = 0; i < ml.count; i++) {
3346 r = get_pointer_64(p, offset, left, S, info);
3349 memset(&m, '\0', sizeof(struct method64_t));
3350 if (left < sizeof(struct method64_t)) {
3351 memcpy(&ml, r, left);
3352 outs() << indent << " (method_t entends past the end of the section)\n";
3354 memcpy(&m, r, sizeof(struct method64_t));
3355 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3358 outs() << indent << "\t\t name ";
3359 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
3360 info, n_value, m.name);
3362 if (info->verbose && sym_name != nullptr)
3365 outs() << format("0x%" PRIx64, n_value);
3367 outs() << " + " << format("0x%" PRIx64, m.name);
3369 outs() << format("0x%" PRIx64, m.name);
3370 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
3371 if (name != nullptr)
3372 outs() << format(" %.*s", left, name);
3375 outs() << indent << "\t\t types ";
3376 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
3377 info, n_value, m.types);
3379 if (info->verbose && sym_name != nullptr)
3382 outs() << format("0x%" PRIx64, n_value);
3384 outs() << " + " << format("0x%" PRIx64, m.types);
3386 outs() << format("0x%" PRIx64, m.types);
3387 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
3388 if (name != nullptr)
3389 outs() << format(" %.*s", left, name);
3392 outs() << indent << "\t\t imp ";
3393 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
3395 if (info->verbose && name == nullptr) {
3397 outs() << format("0x%" PRIx64, n_value) << " ";
3399 outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
3401 outs() << format("0x%" PRIx64, m.imp) << " ";
3403 if (name != nullptr)
3407 p += sizeof(struct method64_t);
3408 offset += sizeof(struct method64_t);
3412 static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
3413 const char *indent) {
3414 struct method_list32_t ml;
3415 struct method32_t m;
3416 const char *r, *name;
3417 uint32_t offset, xoffset, left, i;
3420 r = get_pointer_32(p, offset, left, S, info);
3423 memset(&ml, '\0', sizeof(struct method_list32_t));
3424 if (left < sizeof(struct method_list32_t)) {
3425 memcpy(&ml, r, left);
3426 outs() << " (method_list_t entends past the end of the section)\n";
3428 memcpy(&ml, r, sizeof(struct method_list32_t));
3429 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3431 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3432 outs() << indent << "\t\t count " << ml.count << "\n";
3434 p += sizeof(struct method_list32_t);
3435 offset += sizeof(struct method_list32_t);
3436 for (i = 0; i < ml.count; i++) {
3437 r = get_pointer_32(p, offset, left, S, info);
3440 memset(&m, '\0', sizeof(struct method32_t));
3441 if (left < sizeof(struct method32_t)) {
3442 memcpy(&ml, r, left);
3443 outs() << indent << " (method_t entends past the end of the section)\n";
3445 memcpy(&m, r, sizeof(struct method32_t));
3446 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3449 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
3450 name = get_pointer_32(m.name, xoffset, left, xS, info);
3451 if (name != nullptr)
3452 outs() << format(" %.*s", left, name);
3455 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
3456 name = get_pointer_32(m.types, xoffset, left, xS, info);
3457 if (name != nullptr)
3458 outs() << format(" %.*s", left, name);
3461 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
3462 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
3464 if (name != nullptr)
3465 outs() << " " << name;
3468 p += sizeof(struct method32_t);
3469 offset += sizeof(struct method32_t);
3473 static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
3474 uint32_t offset, left, xleft;
3476 struct objc_method_list_t method_list;
3477 struct objc_method_t method;
3478 const char *r, *methods, *name, *SymbolName;
3481 r = get_pointer_32(p, offset, left, S, info, true);
3486 if (left > sizeof(struct objc_method_list_t)) {
3487 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
3489 outs() << "\t\t objc_method_list extends past end of the section\n";
3490 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
3491 memcpy(&method_list, r, left);
3493 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3494 swapStruct(method_list);
3496 outs() << "\t\t obsolete "
3497 << format("0x%08" PRIx32, method_list.obsolete) << "\n";
3498 outs() << "\t\t method_count " << method_list.method_count << "\n";
3500 methods = r + sizeof(struct objc_method_list_t);
3501 for (i = 0; i < method_list.method_count; i++) {
3502 if ((i + 1) * sizeof(struct objc_method_t) > left) {
3503 outs() << "\t\t remaining method's extend past the of the section\n";
3506 memcpy(&method, methods + i * sizeof(struct objc_method_t),
3507 sizeof(struct objc_method_t));
3508 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3511 outs() << "\t\t method_name "
3512 << format("0x%08" PRIx32, method.method_name);
3513 if (info->verbose) {
3514 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
3515 if (name != nullptr)
3516 outs() << format(" %.*s", xleft, name);
3518 outs() << " (not in an __OBJC section)";
3522 outs() << "\t\t method_types "
3523 << format("0x%08" PRIx32, method.method_types);
3524 if (info->verbose) {
3525 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
3526 if (name != nullptr)
3527 outs() << format(" %.*s", xleft, name);
3529 outs() << " (not in an __OBJC section)";
3533 outs() << "\t\t method_imp "
3534 << format("0x%08" PRIx32, method.method_imp) << " ";
3535 if (info->verbose) {
3536 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
3537 if (SymbolName != nullptr)
3538 outs() << SymbolName;
3545 static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
3546 struct protocol_list64_t pl;
3547 uint64_t q, n_value;
3548 struct protocol64_t pc;
3550 uint32_t offset, xoffset, left, i;
3552 const char *name, *sym_name;
3554 r = get_pointer_64(p, offset, left, S, info);
3557 memset(&pl, '\0', sizeof(struct protocol_list64_t));
3558 if (left < sizeof(struct protocol_list64_t)) {
3559 memcpy(&pl, r, left);
3560 outs() << " (protocol_list_t entends past the end of the section)\n";
3562 memcpy(&pl, r, sizeof(struct protocol_list64_t));
3563 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3565 outs() << " count " << pl.count << "\n";
3567 p += sizeof(struct protocol_list64_t);
3568 offset += sizeof(struct protocol_list64_t);
3569 for (i = 0; i < pl.count; i++) {
3570 r = get_pointer_64(p, offset, left, S, info);
3574 if (left < sizeof(uint64_t)) {
3575 memcpy(&q, r, left);
3576 outs() << " (protocol_t * entends past the end of the section)\n";
3578 memcpy(&q, r, sizeof(uint64_t));
3579 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3580 sys::swapByteOrder(q);
3582 outs() << "\t\t list[" << i << "] ";
3583 sym_name = get_symbol_64(offset, S, info, n_value, q);
3585 if (info->verbose && sym_name != nullptr)
3588 outs() << format("0x%" PRIx64, n_value);
3590 outs() << " + " << format("0x%" PRIx64, q);
3592 outs() << format("0x%" PRIx64, q);
3593 outs() << " (struct protocol_t *)\n";
3595 r = get_pointer_64(q + n_value, offset, left, S, info);
3598 memset(&pc, '\0', sizeof(struct protocol64_t));
3599 if (left < sizeof(struct protocol64_t)) {
3600 memcpy(&pc, r, left);
3601 outs() << " (protocol_t entends past the end of the section)\n";
3603 memcpy(&pc, r, sizeof(struct protocol64_t));
3604 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3607 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
3609 outs() << "\t\t\t name ";
3610 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
3611 info, n_value, pc.name);
3613 if (info->verbose && sym_name != nullptr)
3616 outs() << format("0x%" PRIx64, n_value);
3618 outs() << " + " << format("0x%" PRIx64, pc.name);
3620 outs() << format("0x%" PRIx64, pc.name);
3621 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
3622 if (name != nullptr)
3623 outs() << format(" %.*s", left, name);
3626 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
3628 outs() << "\t\t instanceMethods ";
3630 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
3631 S, info, n_value, pc.instanceMethods);
3633 if (info->verbose && sym_name != nullptr)
3636 outs() << format("0x%" PRIx64, n_value);
3637 if (pc.instanceMethods != 0)
3638 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
3640 outs() << format("0x%" PRIx64, pc.instanceMethods);
3641 outs() << " (struct method_list_t *)\n";
3642 if (pc.instanceMethods + n_value != 0)
3643 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
3645 outs() << "\t\t classMethods ";
3647 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
3648 info, n_value, pc.classMethods);
3650 if (info->verbose && sym_name != nullptr)
3653 outs() << format("0x%" PRIx64, n_value);
3654 if (pc.classMethods != 0)
3655 outs() << " + " << format("0x%" PRIx64, pc.classMethods);
3657 outs() << format("0x%" PRIx64, pc.classMethods);
3658 outs() << " (struct method_list_t *)\n";
3659 if (pc.classMethods + n_value != 0)
3660 print_method_list64_t(pc.classMethods + n_value, info, "\t");
3662 outs() << "\t optionalInstanceMethods "
3663 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
3664 outs() << "\t optionalClassMethods "
3665 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
3666 outs() << "\t instanceProperties "
3667 << format("0x%" PRIx64, pc.instanceProperties) << "\n";
3669 p += sizeof(uint64_t);
3670 offset += sizeof(uint64_t);
3674 static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
3675 struct protocol_list32_t pl;
3677 struct protocol32_t pc;
3679 uint32_t offset, xoffset, left, i;
3683 r = get_pointer_32(p, offset, left, S, info);
3686 memset(&pl, '\0', sizeof(struct protocol_list32_t));
3687 if (left < sizeof(struct protocol_list32_t)) {
3688 memcpy(&pl, r, left);
3689 outs() << " (protocol_list_t entends past the end of the section)\n";
3691 memcpy(&pl, r, sizeof(struct protocol_list32_t));
3692 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3694 outs() << " count " << pl.count << "\n";
3696 p += sizeof(struct protocol_list32_t);
3697 offset += sizeof(struct protocol_list32_t);
3698 for (i = 0; i < pl.count; i++) {
3699 r = get_pointer_32(p, offset, left, S, info);
3703 if (left < sizeof(uint32_t)) {
3704 memcpy(&q, r, left);
3705 outs() << " (protocol_t * entends past the end of the section)\n";
3707 memcpy(&q, r, sizeof(uint32_t));
3708 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3709 sys::swapByteOrder(q);
3710 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
3711 << " (struct protocol_t *)\n";
3712 r = get_pointer_32(q, offset, left, S, info);
3715 memset(&pc, '\0', sizeof(struct protocol32_t));
3716 if (left < sizeof(struct protocol32_t)) {
3717 memcpy(&pc, r, left);
3718 outs() << " (protocol_t entends past the end of the section)\n";
3720 memcpy(&pc, r, sizeof(struct protocol32_t));
3721 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3723 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
3724 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
3725 name = get_pointer_32(pc.name, xoffset, left, xS, info);
3726 if (name != nullptr)
3727 outs() << format(" %.*s", left, name);
3729 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
3730 outs() << "\t\t instanceMethods "
3731 << format("0x%" PRIx32, pc.instanceMethods)
3732 << " (struct method_list_t *)\n";
3733 if (pc.instanceMethods != 0)
3734 print_method_list32_t(pc.instanceMethods, info, "\t");
3735 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
3736 << " (struct method_list_t *)\n";
3737 if (pc.classMethods != 0)
3738 print_method_list32_t(pc.classMethods, info, "\t");
3739 outs() << "\t optionalInstanceMethods "
3740 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
3741 outs() << "\t optionalClassMethods "
3742 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
3743 outs() << "\t instanceProperties "
3744 << format("0x%" PRIx32, pc.instanceProperties) << "\n";
3745 p += sizeof(uint32_t);
3746 offset += sizeof(uint32_t);
3750 static void print_indent(uint32_t indent) {
3751 for (uint32_t i = 0; i < indent;) {
3752 if (indent - i >= 8) {
3756 for (uint32_t j = i; j < indent; j++)
3763 static bool print_method_description_list(uint32_t p, uint32_t indent,
3764 struct DisassembleInfo *info) {
3765 uint32_t offset, left, xleft;
3767 struct objc_method_description_list_t mdl;
3768 struct objc_method_description_t md;
3769 const char *r, *list, *name;
3772 r = get_pointer_32(p, offset, left, S, info, true);
3777 if (left > sizeof(struct objc_method_description_list_t)) {
3778 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
3780 print_indent(indent);
3781 outs() << " objc_method_description_list extends past end of the section\n";
3782 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
3783 memcpy(&mdl, r, left);
3785 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3788 print_indent(indent);
3789 outs() << " count " << mdl.count << "\n";
3791 list = r + sizeof(struct objc_method_description_list_t);
3792 for (i = 0; i < mdl.count; i++) {
3793 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
3794 print_indent(indent);
3795 outs() << " remaining list entries extend past the of the section\n";
3798 print_indent(indent);
3799 outs() << " list[" << i << "]\n";
3800 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
3801 sizeof(struct objc_method_description_t));
3802 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3805 print_indent(indent);
3806 outs() << " name " << format("0x%08" PRIx32, md.name);
3807 if (info->verbose) {
3808 name = get_pointer_32(md.name, offset, xleft, S, info, true);
3809 if (name != nullptr)
3810 outs() << format(" %.*s", xleft, name);
3812 outs() << " (not in an __OBJC section)";
3816 print_indent(indent);
3817 outs() << " types " << format("0x%08" PRIx32, md.types);
3818 if (info->verbose) {
3819 name = get_pointer_32(md.types, offset, xleft, S, info, true);
3820 if (name != nullptr)
3821 outs() << format(" %.*s", xleft, name);
3823 outs() << " (not in an __OBJC section)";
3830 static bool print_protocol_list(uint32_t p, uint32_t indent,
3831 struct DisassembleInfo *info);
3833 static bool print_protocol(uint32_t p, uint32_t indent,
3834 struct DisassembleInfo *info) {
3835 uint32_t offset, left;
3837 struct objc_protocol_t protocol;
3838 const char *r, *name;
3840 r = get_pointer_32(p, offset, left, S, info, true);
3845 if (left >= sizeof(struct objc_protocol_t)) {
3846 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
3848 print_indent(indent);
3849 outs() << " Protocol extends past end of the section\n";
3850 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
3851 memcpy(&protocol, r, left);
3853 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3854 swapStruct(protocol);
3856 print_indent(indent);
3857 outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
3860 print_indent(indent);
3861 outs() << " protocol_name "
3862 << format("0x%08" PRIx32, protocol.protocol_name);
3863 if (info->verbose) {
3864 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
3865 if (name != nullptr)
3866 outs() << format(" %.*s", left, name);
3868 outs() << " (not in an __OBJC section)";
3872 print_indent(indent);
3873 outs() << " protocol_list "
3874 << format("0x%08" PRIx32, protocol.protocol_list);
3875 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
3876 outs() << " (not in an __OBJC section)\n";
3878 print_indent(indent);
3879 outs() << " instance_methods "
3880 << format("0x%08" PRIx32, protocol.instance_methods);
3881 if (print_method_description_list(protocol.instance_methods, indent, info))
3882 outs() << " (not in an __OBJC section)\n";
3884 print_indent(indent);
3885 outs() << " class_methods "
3886 << format("0x%08" PRIx32, protocol.class_methods);
3887 if (print_method_description_list(protocol.class_methods, indent, info))
3888 outs() << " (not in an __OBJC section)\n";
3893 static bool print_protocol_list(uint32_t p, uint32_t indent,
3894 struct DisassembleInfo *info) {
3895 uint32_t offset, left, l;
3897 struct objc_protocol_list_t protocol_list;
3898 const char *r, *list;
3901 r = get_pointer_32(p, offset, left, S, info, true);
3906 if (left > sizeof(struct objc_protocol_list_t)) {
3907 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
3909 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
3910 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
3911 memcpy(&protocol_list, r, left);
3913 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3914 swapStruct(protocol_list);
3916 print_indent(indent);
3917 outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
3919 print_indent(indent);
3920 outs() << " count " << protocol_list.count << "\n";
3922 list = r + sizeof(struct objc_protocol_list_t);
3923 for (i = 0; i < protocol_list.count; i++) {
3924 if ((i + 1) * sizeof(uint32_t) > left) {
3925 outs() << "\t\t remaining list entries extend past the of the section\n";
3928 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
3929 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3930 sys::swapByteOrder(l);
3932 print_indent(indent);
3933 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
3934 if (print_protocol(l, indent, info))
3935 outs() << "(not in an __OBJC section)\n";
3940 static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
3941 struct ivar_list64_t il;
3944 uint32_t offset, xoffset, left, j;
3946 const char *name, *sym_name, *ivar_offset_p;
3947 uint64_t ivar_offset, n_value;
3949 r = get_pointer_64(p, offset, left, S, info);
3952 memset(&il, '\0', sizeof(struct ivar_list64_t));
3953 if (left < sizeof(struct ivar_list64_t)) {
3954 memcpy(&il, r, left);
3955 outs() << " (ivar_list_t entends past the end of the section)\n";
3957 memcpy(&il, r, sizeof(struct ivar_list64_t));
3958 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3960 outs() << " entsize " << il.entsize << "\n";
3961 outs() << " count " << il.count << "\n";
3963 p += sizeof(struct ivar_list64_t);
3964 offset += sizeof(struct ivar_list64_t);
3965 for (j = 0; j < il.count; j++) {
3966 r = get_pointer_64(p, offset, left, S, info);
3969 memset(&i, '\0', sizeof(struct ivar64_t));
3970 if (left < sizeof(struct ivar64_t)) {
3971 memcpy(&i, r, left);
3972 outs() << " (ivar_t entends past the end of the section)\n";
3974 memcpy(&i, r, sizeof(struct ivar64_t));
3975 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3978 outs() << "\t\t\t offset ";
3979 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
3980 info, n_value, i.offset);
3982 if (info->verbose && sym_name != nullptr)
3985 outs() << format("0x%" PRIx64, n_value);
3987 outs() << " + " << format("0x%" PRIx64, i.offset);
3989 outs() << format("0x%" PRIx64, i.offset);
3990 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
3991 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
3992 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
3993 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3994 sys::swapByteOrder(ivar_offset);
3995 outs() << " " << ivar_offset << "\n";
3999 outs() << "\t\t\t name ";
4000 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
4003 if (info->verbose && sym_name != nullptr)
4006 outs() << format("0x%" PRIx64, n_value);
4008 outs() << " + " << format("0x%" PRIx64, i.name);
4010 outs() << format("0x%" PRIx64, i.name);
4011 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
4012 if (name != nullptr)
4013 outs() << format(" %.*s", left, name);
4016 outs() << "\t\t\t type ";
4017 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
4019 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
4021 if (info->verbose && sym_name != nullptr)
4024 outs() << format("0x%" PRIx64, n_value);
4026 outs() << " + " << format("0x%" PRIx64, i.type);
4028 outs() << format("0x%" PRIx64, i.type);
4029 if (name != nullptr)
4030 outs() << format(" %.*s", left, name);
4033 outs() << "\t\t\talignment " << i.alignment << "\n";
4034 outs() << "\t\t\t size " << i.size << "\n";
4036 p += sizeof(struct ivar64_t);
4037 offset += sizeof(struct ivar64_t);
4041 static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
4042 struct ivar_list32_t il;
4045 uint32_t offset, xoffset, left, j;
4047 const char *name, *ivar_offset_p;
4048 uint32_t ivar_offset;
4050 r = get_pointer_32(p, offset, left, S, info);
4053 memset(&il, '\0', sizeof(struct ivar_list32_t));
4054 if (left < sizeof(struct ivar_list32_t)) {
4055 memcpy(&il, r, left);
4056 outs() << " (ivar_list_t entends past the end of the section)\n";
4058 memcpy(&il, r, sizeof(struct ivar_list32_t));
4059 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4061 outs() << " entsize " << il.entsize << "\n";
4062 outs() << " count " << il.count << "\n";
4064 p += sizeof(struct ivar_list32_t);
4065 offset += sizeof(struct ivar_list32_t);
4066 for (j = 0; j < il.count; j++) {
4067 r = get_pointer_32(p, offset, left, S, info);
4070 memset(&i, '\0', sizeof(struct ivar32_t));
4071 if (left < sizeof(struct ivar32_t)) {
4072 memcpy(&i, r, left);
4073 outs() << " (ivar_t entends past the end of the section)\n";
4075 memcpy(&i, r, sizeof(struct ivar32_t));
4076 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4079 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
4080 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4081 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4082 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4083 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4084 sys::swapByteOrder(ivar_offset);
4085 outs() << " " << ivar_offset << "\n";
4089 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
4090 name = get_pointer_32(i.name, xoffset, left, xS, info);
4091 if (name != nullptr)
4092 outs() << format(" %.*s", left, name);
4095 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
4096 name = get_pointer_32(i.type, xoffset, left, xS, info);
4097 if (name != nullptr)
4098 outs() << format(" %.*s", left, name);
4101 outs() << "\t\t\talignment " << i.alignment << "\n";
4102 outs() << "\t\t\t size " << i.size << "\n";
4104 p += sizeof(struct ivar32_t);
4105 offset += sizeof(struct ivar32_t);
4109 static void print_objc_property_list64(uint64_t p,
4110 struct DisassembleInfo *info) {
4111 struct objc_property_list64 opl;
4112 struct objc_property64 op;
4114 uint32_t offset, xoffset, left, j;
4116 const char *name, *sym_name;
4119 r = get_pointer_64(p, offset, left, S, info);
4122 memset(&opl, '\0', sizeof(struct objc_property_list64));
4123 if (left < sizeof(struct objc_property_list64)) {
4124 memcpy(&opl, r, left);
4125 outs() << " (objc_property_list entends past the end of the section)\n";
4127 memcpy(&opl, r, sizeof(struct objc_property_list64));
4128 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4130 outs() << " entsize " << opl.entsize << "\n";
4131 outs() << " count " << opl.count << "\n";
4133 p += sizeof(struct objc_property_list64);
4134 offset += sizeof(struct objc_property_list64);
4135 for (j = 0; j < opl.count; j++) {
4136 r = get_pointer_64(p, offset, left, S, info);
4139 memset(&op, '\0', sizeof(struct objc_property64));
4140 if (left < sizeof(struct objc_property64)) {
4141 memcpy(&op, r, left);
4142 outs() << " (objc_property entends past the end of the section)\n";
4144 memcpy(&op, r, sizeof(struct objc_property64));
4145 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4148 outs() << "\t\t\t name ";
4149 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
4150 info, n_value, op.name);
4152 if (info->verbose && sym_name != nullptr)
4155 outs() << format("0x%" PRIx64, n_value);
4157 outs() << " + " << format("0x%" PRIx64, op.name);
4159 outs() << format("0x%" PRIx64, op.name);
4160 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
4161 if (name != nullptr)
4162 outs() << format(" %.*s", left, name);
4165 outs() << "\t\t\tattributes ";
4167 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
4168 info, n_value, op.attributes);
4170 if (info->verbose && sym_name != nullptr)
4173 outs() << format("0x%" PRIx64, n_value);
4174 if (op.attributes != 0)
4175 outs() << " + " << format("0x%" PRIx64, op.attributes);
4177 outs() << format("0x%" PRIx64, op.attributes);
4178 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
4179 if (name != nullptr)
4180 outs() << format(" %.*s", left, name);
4183 p += sizeof(struct objc_property64);
4184 offset += sizeof(struct objc_property64);
4188 static void print_objc_property_list32(uint32_t p,
4189 struct DisassembleInfo *info) {
4190 struct objc_property_list32 opl;
4191 struct objc_property32 op;
4193 uint32_t offset, xoffset, left, j;
4197 r = get_pointer_32(p, offset, left, S, info);
4200 memset(&opl, '\0', sizeof(struct objc_property_list32));
4201 if (left < sizeof(struct objc_property_list32)) {
4202 memcpy(&opl, r, left);
4203 outs() << " (objc_property_list entends past the end of the section)\n";
4205 memcpy(&opl, r, sizeof(struct objc_property_list32));
4206 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4208 outs() << " entsize " << opl.entsize << "\n";
4209 outs() << " count " << opl.count << "\n";
4211 p += sizeof(struct objc_property_list32);
4212 offset += sizeof(struct objc_property_list32);
4213 for (j = 0; j < opl.count; j++) {
4214 r = get_pointer_32(p, offset, left, S, info);
4217 memset(&op, '\0', sizeof(struct objc_property32));
4218 if (left < sizeof(struct objc_property32)) {
4219 memcpy(&op, r, left);
4220 outs() << " (objc_property entends past the end of the section)\n";
4222 memcpy(&op, r, sizeof(struct objc_property32));
4223 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4226 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
4227 name = get_pointer_32(op.name, xoffset, left, xS, info);
4228 if (name != nullptr)
4229 outs() << format(" %.*s", left, name);
4232 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
4233 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
4234 if (name != nullptr)
4235 outs() << format(" %.*s", left, name);
4238 p += sizeof(struct objc_property32);
4239 offset += sizeof(struct objc_property32);
4243 static void print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
4244 bool &is_meta_class) {
4245 struct class_ro64_t cro;
4247 uint32_t offset, xoffset, left;
4249 const char *name, *sym_name;
4252 r = get_pointer_64(p, offset, left, S, info);
4253 if (r == nullptr || left < sizeof(struct class_ro64_t))
4255 memset(&cro, '\0', sizeof(struct class_ro64_t));
4256 if (left < sizeof(struct class_ro64_t)) {
4257 memcpy(&cro, r, left);
4258 outs() << " (class_ro_t entends past the end of the section)\n";
4260 memcpy(&cro, r, sizeof(struct class_ro64_t));
4261 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4263 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4264 if (cro.flags & RO_META)
4265 outs() << " RO_META";
4266 if (cro.flags & RO_ROOT)
4267 outs() << " RO_ROOT";
4268 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4269 outs() << " RO_HAS_CXX_STRUCTORS";
4271 outs() << " instanceStart " << cro.instanceStart << "\n";
4272 outs() << " instanceSize " << cro.instanceSize << "\n";
4273 outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
4275 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
4277 print_layout_map64(cro.ivarLayout, info);
4280 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
4281 info, n_value, cro.name);
4283 if (info->verbose && sym_name != nullptr)
4286 outs() << format("0x%" PRIx64, n_value);
4288 outs() << " + " << format("0x%" PRIx64, cro.name);
4290 outs() << format("0x%" PRIx64, cro.name);
4291 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
4292 if (name != nullptr)
4293 outs() << format(" %.*s", left, name);
4296 outs() << " baseMethods ";
4297 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
4298 S, info, n_value, cro.baseMethods);
4300 if (info->verbose && sym_name != nullptr)
4303 outs() << format("0x%" PRIx64, n_value);
4304 if (cro.baseMethods != 0)
4305 outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
4307 outs() << format("0x%" PRIx64, cro.baseMethods);
4308 outs() << " (struct method_list_t *)\n";
4309 if (cro.baseMethods + n_value != 0)
4310 print_method_list64_t(cro.baseMethods + n_value, info, "");
4312 outs() << " baseProtocols ";
4314 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
4315 info, n_value, cro.baseProtocols);
4317 if (info->verbose && sym_name != nullptr)
4320 outs() << format("0x%" PRIx64, n_value);
4321 if (cro.baseProtocols != 0)
4322 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
4324 outs() << format("0x%" PRIx64, cro.baseProtocols);
4326 if (cro.baseProtocols + n_value != 0)
4327 print_protocol_list64_t(cro.baseProtocols + n_value, info);
4329 outs() << " ivars ";
4330 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
4331 info, n_value, cro.ivars);
4333 if (info->verbose && sym_name != nullptr)
4336 outs() << format("0x%" PRIx64, n_value);
4338 outs() << " + " << format("0x%" PRIx64, cro.ivars);
4340 outs() << format("0x%" PRIx64, cro.ivars);
4342 if (cro.ivars + n_value != 0)
4343 print_ivar_list64_t(cro.ivars + n_value, info);
4345 outs() << " weakIvarLayout ";
4347 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
4348 info, n_value, cro.weakIvarLayout);
4350 if (info->verbose && sym_name != nullptr)
4353 outs() << format("0x%" PRIx64, n_value);
4354 if (cro.weakIvarLayout != 0)
4355 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
4357 outs() << format("0x%" PRIx64, cro.weakIvarLayout);
4359 print_layout_map64(cro.weakIvarLayout + n_value, info);
4361 outs() << " baseProperties ";
4363 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
4364 info, n_value, cro.baseProperties);
4366 if (info->verbose && sym_name != nullptr)
4369 outs() << format("0x%" PRIx64, n_value);
4370 if (cro.baseProperties != 0)
4371 outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
4373 outs() << format("0x%" PRIx64, cro.baseProperties);
4375 if (cro.baseProperties + n_value != 0)
4376 print_objc_property_list64(cro.baseProperties + n_value, info);
4378 is_meta_class = (cro.flags & RO_META) ? true : false;
4381 static void print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
4382 bool &is_meta_class) {
4383 struct class_ro32_t cro;
4385 uint32_t offset, xoffset, left;
4389 r = get_pointer_32(p, offset, left, S, info);
4392 memset(&cro, '\0', sizeof(struct class_ro32_t));
4393 if (left < sizeof(struct class_ro32_t)) {
4394 memcpy(&cro, r, left);
4395 outs() << " (class_ro_t entends past the end of the section)\n";
4397 memcpy(&cro, r, sizeof(struct class_ro32_t));
4398 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4400 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4401 if (cro.flags & RO_META)
4402 outs() << " RO_META";
4403 if (cro.flags & RO_ROOT)
4404 outs() << " RO_ROOT";
4405 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4406 outs() << " RO_HAS_CXX_STRUCTORS";
4408 outs() << " instanceStart " << cro.instanceStart << "\n";
4409 outs() << " instanceSize " << cro.instanceSize << "\n";
4410 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
4412 print_layout_map32(cro.ivarLayout, info);
4414 outs() << " name " << format("0x%" PRIx32, cro.name);
4415 name = get_pointer_32(cro.name, xoffset, left, xS, info);
4416 if (name != nullptr)
4417 outs() << format(" %.*s", left, name);
4420 outs() << " baseMethods "
4421 << format("0x%" PRIx32, cro.baseMethods)
4422 << " (struct method_list_t *)\n";
4423 if (cro.baseMethods != 0)
4424 print_method_list32_t(cro.baseMethods, info, "");
4426 outs() << " baseProtocols "
4427 << format("0x%" PRIx32, cro.baseProtocols) << "\n";
4428 if (cro.baseProtocols != 0)
4429 print_protocol_list32_t(cro.baseProtocols, info);
4430 outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
4433 print_ivar_list32_t(cro.ivars, info);
4434 outs() << " weakIvarLayout "
4435 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
4436 print_layout_map32(cro.weakIvarLayout, info);
4437 outs() << " baseProperties "
4438 << format("0x%" PRIx32, cro.baseProperties) << "\n";
4439 if (cro.baseProperties != 0)
4440 print_objc_property_list32(cro.baseProperties, info);
4441 is_meta_class = (cro.flags & RO_META) ? true : false;
4444 static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
4447 uint32_t offset, left;
4450 uint64_t isa_n_value, n_value;
4452 r = get_pointer_64(p, offset, left, S, info);
4453 if (r == nullptr || left < sizeof(struct class64_t))
4455 memset(&c, '\0', sizeof(struct class64_t));
4456 if (left < sizeof(struct class64_t)) {
4457 memcpy(&c, r, left);
4458 outs() << " (class_t entends past the end of the section)\n";
4460 memcpy(&c, r, sizeof(struct class64_t));
4461 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4464 outs() << " isa " << format("0x%" PRIx64, c.isa);
4465 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
4466 isa_n_value, c.isa);
4467 if (name != nullptr)
4468 outs() << " " << name;
4471 outs() << " superclass " << format("0x%" PRIx64, c.superclass);
4472 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
4473 n_value, c.superclass);
4474 if (name != nullptr)
4475 outs() << " " << name;
4478 outs() << " cache " << format("0x%" PRIx64, c.cache);
4479 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
4481 if (name != nullptr)
4482 outs() << " " << name;
4485 outs() << " vtable " << format("0x%" PRIx64, c.vtable);
4486 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
4488 if (name != nullptr)
4489 outs() << " " << name;
4492 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
4496 if (info->verbose && name != nullptr)
4499 outs() << format("0x%" PRIx64, n_value);
4501 outs() << " + " << format("0x%" PRIx64, c.data);
4503 outs() << format("0x%" PRIx64, c.data);
4504 outs() << " (struct class_ro_t *)";
4506 // This is a Swift class if some of the low bits of the pointer are set.
4507 if ((c.data + n_value) & 0x7)
4508 outs() << " Swift class";
4511 print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class);
4513 if (is_meta_class == false) {
4514 outs() << "Meta Class\n";
4515 print_class64_t(c.isa + isa_n_value, info);
4519 static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
4522 uint32_t offset, left;
4526 r = get_pointer_32(p, offset, left, S, info);
4529 memset(&c, '\0', sizeof(struct class32_t));
4530 if (left < sizeof(struct class32_t)) {
4531 memcpy(&c, r, left);
4532 outs() << " (class_t entends past the end of the section)\n";
4534 memcpy(&c, r, sizeof(struct class32_t));
4535 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4538 outs() << " isa " << format("0x%" PRIx32, c.isa);
4540 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
4541 if (name != nullptr)
4542 outs() << " " << name;
4545 outs() << " superclass " << format("0x%" PRIx32, c.superclass);
4546 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
4548 if (name != nullptr)
4549 outs() << " " << name;
4552 outs() << " cache " << format("0x%" PRIx32, c.cache);
4553 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
4555 if (name != nullptr)
4556 outs() << " " << name;
4559 outs() << " vtable " << format("0x%" PRIx32, c.vtable);
4560 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
4562 if (name != nullptr)
4563 outs() << " " << name;
4567 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
4568 outs() << " data " << format("0x%" PRIx32, c.data)
4569 << " (struct class_ro_t *)";
4571 // This is a Swift class if some of the low bits of the pointer are set.
4573 outs() << " Swift class";
4576 print_class_ro32_t(c.data & ~0x3, info, is_meta_class);
4578 if (is_meta_class == false) {
4579 outs() << "Meta Class\n";
4580 print_class32_t(c.isa, info);
4584 static void print_objc_class_t(struct objc_class_t *objc_class,
4585 struct DisassembleInfo *info) {
4586 uint32_t offset, left, xleft;
4587 const char *name, *p, *ivar_list;
4590 struct objc_ivar_list_t objc_ivar_list;
4591 struct objc_ivar_t ivar;
4593 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
4594 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
4595 name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
4596 if (name != nullptr)
4597 outs() << format(" %.*s", left, name);
4599 outs() << " (not in an __OBJC section)";
4603 outs() << "\t super_class "
4604 << format("0x%08" PRIx32, objc_class->super_class);
4605 if (info->verbose) {
4606 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
4607 if (name != nullptr)
4608 outs() << format(" %.*s", left, name);
4610 outs() << " (not in an __OBJC section)";
4614 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name);
4615 if (info->verbose) {
4616 name = get_pointer_32(objc_class->name, offset, left, S, info, true);
4617 if (name != nullptr)
4618 outs() << format(" %.*s", left, name);
4620 outs() << " (not in an __OBJC section)";
4624 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
4627 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
4628 if (info->verbose) {
4629 if (CLS_GETINFO(objc_class, CLS_CLASS))
4630 outs() << " CLS_CLASS";
4631 else if (CLS_GETINFO(objc_class, CLS_META))
4632 outs() << " CLS_META";
4636 outs() << "\t instance_size "
4637 << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
4639 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
4640 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
4642 if (left > sizeof(struct objc_ivar_list_t)) {
4644 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
4646 outs() << " (entends past the end of the section)\n";
4647 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
4648 memcpy(&objc_ivar_list, p, left);
4650 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4651 swapStruct(objc_ivar_list);
4652 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
4653 ivar_list = p + sizeof(struct objc_ivar_list_t);
4654 for (i = 0; i < objc_ivar_list.ivar_count; i++) {
4655 if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
4656 outs() << "\t\t remaining ivar's extend past the of the section\n";
4659 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
4660 sizeof(struct objc_ivar_t));
4661 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4664 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
4665 if (info->verbose) {
4666 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
4667 if (name != nullptr)
4668 outs() << format(" %.*s", xleft, name);
4670 outs() << " (not in an __OBJC section)";
4674 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
4675 if (info->verbose) {
4676 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
4677 if (name != nullptr)
4678 outs() << format(" %.*s", xleft, name);
4680 outs() << " (not in an __OBJC section)";
4684 outs() << "\t\t ivar_offset "
4685 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
4688 outs() << " (not in an __OBJC section)\n";
4691 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
4692 if (print_method_list(objc_class->methodLists, info))
4693 outs() << " (not in an __OBJC section)\n";
4695 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
4698 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
4699 if (print_protocol_list(objc_class->protocols, 16, info))
4700 outs() << " (not in an __OBJC section)\n";
4703 static void print_objc_objc_category_t(struct objc_category_t *objc_category,
4704 struct DisassembleInfo *info) {
4705 uint32_t offset, left;
4709 outs() << "\t category name "
4710 << format("0x%08" PRIx32, objc_category->category_name);
4711 if (info->verbose) {
4712 name = get_pointer_32(objc_category->category_name, offset, left, S, info,
4714 if (name != nullptr)
4715 outs() << format(" %.*s", left, name);
4717 outs() << " (not in an __OBJC section)";
4721 outs() << "\t\t class name "
4722 << format("0x%08" PRIx32, objc_category->class_name);
4723 if (info->verbose) {
4725 get_pointer_32(objc_category->class_name, offset, left, S, info, true);
4726 if (name != nullptr)
4727 outs() << format(" %.*s", left, name);
4729 outs() << " (not in an __OBJC section)";
4733 outs() << "\t instance methods "
4734 << format("0x%08" PRIx32, objc_category->instance_methods);
4735 if (print_method_list(objc_category->instance_methods, info))
4736 outs() << " (not in an __OBJC section)\n";
4738 outs() << "\t class methods "
4739 << format("0x%08" PRIx32, objc_category->class_methods);
4740 if (print_method_list(objc_category->class_methods, info))
4741 outs() << " (not in an __OBJC section)\n";
4744 static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
4745 struct category64_t c;
4747 uint32_t offset, xoffset, left;
4749 const char *name, *sym_name;
4752 r = get_pointer_64(p, offset, left, S, info);
4755 memset(&c, '\0', sizeof(struct category64_t));
4756 if (left < sizeof(struct category64_t)) {
4757 memcpy(&c, r, left);
4758 outs() << " (category_t entends past the end of the section)\n";
4760 memcpy(&c, r, sizeof(struct category64_t));
4761 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4765 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
4766 info, n_value, c.name);
4768 if (info->verbose && sym_name != nullptr)
4771 outs() << format("0x%" PRIx64, n_value);
4773 outs() << " + " << format("0x%" PRIx64, c.name);
4775 outs() << format("0x%" PRIx64, c.name);
4776 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
4777 if (name != nullptr)
4778 outs() << format(" %.*s", left, name);
4782 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
4785 if (info->verbose && sym_name != nullptr)
4788 outs() << format("0x%" PRIx64, n_value);
4790 outs() << " + " << format("0x%" PRIx64, c.cls);
4792 outs() << format("0x%" PRIx64, c.cls);
4794 if (c.cls + n_value != 0)
4795 print_class64_t(c.cls + n_value, info);
4797 outs() << " instanceMethods ";
4799 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
4800 info, n_value, c.instanceMethods);
4802 if (info->verbose && sym_name != nullptr)
4805 outs() << format("0x%" PRIx64, n_value);
4806 if (c.instanceMethods != 0)
4807 outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
4809 outs() << format("0x%" PRIx64, c.instanceMethods);
4811 if (c.instanceMethods + n_value != 0)
4812 print_method_list64_t(c.instanceMethods + n_value, info, "");
4814 outs() << " classMethods ";
4815 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
4816 S, info, n_value, c.classMethods);
4818 if (info->verbose && sym_name != nullptr)
4821 outs() << format("0x%" PRIx64, n_value);
4822 if (c.classMethods != 0)
4823 outs() << " + " << format("0x%" PRIx64, c.classMethods);
4825 outs() << format("0x%" PRIx64, c.classMethods);
4827 if (c.classMethods + n_value != 0)
4828 print_method_list64_t(c.classMethods + n_value, info, "");
4830 outs() << " protocols ";
4831 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
4832 info, n_value, c.protocols);
4834 if (info->verbose && sym_name != nullptr)
4837 outs() << format("0x%" PRIx64, n_value);
4838 if (c.protocols != 0)
4839 outs() << " + " << format("0x%" PRIx64, c.protocols);
4841 outs() << format("0x%" PRIx64, c.protocols);
4843 if (c.protocols + n_value != 0)
4844 print_protocol_list64_t(c.protocols + n_value, info);
4846 outs() << "instanceProperties ";
4848 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
4849 S, info, n_value, c.instanceProperties);
4851 if (info->verbose && sym_name != nullptr)
4854 outs() << format("0x%" PRIx64, n_value);
4855 if (c.instanceProperties != 0)
4856 outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
4858 outs() << format("0x%" PRIx64, c.instanceProperties);
4860 if (c.instanceProperties + n_value != 0)
4861 print_objc_property_list64(c.instanceProperties + n_value, info);
4864 static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
4865 struct category32_t c;
4867 uint32_t offset, left;
4871 r = get_pointer_32(p, offset, left, S, info);
4874 memset(&c, '\0', sizeof(struct category32_t));
4875 if (left < sizeof(struct category32_t)) {
4876 memcpy(&c, r, left);
4877 outs() << " (category_t entends past the end of the section)\n";
4879 memcpy(&c, r, sizeof(struct category32_t));
4880 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4883 outs() << " name " << format("0x%" PRIx32, c.name);
4884 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
4887 outs() << " " << name;
4890 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
4892 print_class32_t(c.cls, info);
4893 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
4895 if (c.instanceMethods != 0)
4896 print_method_list32_t(c.instanceMethods, info, "");
4897 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
4899 if (c.classMethods != 0)
4900 print_method_list32_t(c.classMethods, info, "");
4901 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
4902 if (c.protocols != 0)
4903 print_protocol_list32_t(c.protocols, info);
4904 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
4906 if (c.instanceProperties != 0)
4907 print_objc_property_list32(c.instanceProperties, info);
4910 static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
4911 uint32_t i, left, offset, xoffset;
4912 uint64_t p, n_value;
4913 struct message_ref64 mr;
4914 const char *name, *sym_name;
4918 if (S == SectionRef())
4922 S.getName(SectName);
4923 DataRefImpl Ref = S.getRawDataRefImpl();
4924 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4925 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4927 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
4928 p = S.getAddress() + i;
4929 r = get_pointer_64(p, offset, left, S, info);
4932 memset(&mr, '\0', sizeof(struct message_ref64));
4933 if (left < sizeof(struct message_ref64)) {
4934 memcpy(&mr, r, left);
4935 outs() << " (message_ref entends past the end of the section)\n";
4937 memcpy(&mr, r, sizeof(struct message_ref64));
4938 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4942 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
4945 outs() << format("0x%" PRIx64, n_value) << " ";
4947 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
4949 outs() << format("0x%" PRIx64, mr.imp) << " ";
4950 if (name != nullptr)
4951 outs() << " " << name;
4955 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
4956 info, n_value, mr.sel);
4958 if (info->verbose && sym_name != nullptr)
4961 outs() << format("0x%" PRIx64, n_value);
4963 outs() << " + " << format("0x%" PRIx64, mr.sel);
4965 outs() << format("0x%" PRIx64, mr.sel);
4966 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
4967 if (name != nullptr)
4968 outs() << format(" %.*s", left, name);
4971 offset += sizeof(struct message_ref64);
4975 static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
4976 uint32_t i, left, offset, xoffset, p;
4977 struct message_ref32 mr;
4978 const char *name, *r;
4981 if (S == SectionRef())
4985 S.getName(SectName);
4986 DataRefImpl Ref = S.getRawDataRefImpl();
4987 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
4988 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4990 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
4991 p = S.getAddress() + i;
4992 r = get_pointer_32(p, offset, left, S, info);
4995 memset(&mr, '\0', sizeof(struct message_ref32));
4996 if (left < sizeof(struct message_ref32)) {
4997 memcpy(&mr, r, left);
4998 outs() << " (message_ref entends past the end of the section)\n";
5000 memcpy(&mr, r, sizeof(struct message_ref32));
5001 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5004 outs() << " imp " << format("0x%" PRIx32, mr.imp);
5005 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
5007 if (name != nullptr)
5008 outs() << " " << name;
5011 outs() << " sel " << format("0x%" PRIx32, mr.sel);
5012 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
5013 if (name != nullptr)
5014 outs() << " " << name;
5017 offset += sizeof(struct message_ref32);
5021 static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
5022 uint32_t left, offset, swift_version;
5024 struct objc_image_info64 o;
5028 S.getName(SectName);
5029 DataRefImpl Ref = S.getRawDataRefImpl();
5030 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5031 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5033 r = get_pointer_64(p, offset, left, S, info);
5036 memset(&o, '\0', sizeof(struct objc_image_info64));
5037 if (left < sizeof(struct objc_image_info64)) {
5038 memcpy(&o, r, left);
5039 outs() << " (objc_image_info entends past the end of the section)\n";
5041 memcpy(&o, r, sizeof(struct objc_image_info64));
5042 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5044 outs() << " version " << o.version << "\n";
5045 outs() << " flags " << format("0x%" PRIx32, o.flags);
5046 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5047 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5048 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5049 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5050 swift_version = (o.flags >> 8) & 0xff;
5051 if (swift_version != 0) {
5052 if (swift_version == 1)
5053 outs() << " Swift 1.0";
5054 else if (swift_version == 2)
5055 outs() << " Swift 1.1";
5057 outs() << " unknown future Swift version (" << swift_version << ")";
5062 static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5063 uint32_t left, offset, swift_version, p;
5064 struct objc_image_info32 o;
5068 S.getName(SectName);
5069 DataRefImpl Ref = S.getRawDataRefImpl();
5070 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5071 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5073 r = get_pointer_32(p, offset, left, S, info);
5076 memset(&o, '\0', sizeof(struct objc_image_info32));
5077 if (left < sizeof(struct objc_image_info32)) {
5078 memcpy(&o, r, left);
5079 outs() << " (objc_image_info entends past the end of the section)\n";
5081 memcpy(&o, r, sizeof(struct objc_image_info32));
5082 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5084 outs() << " version " << o.version << "\n";
5085 outs() << " flags " << format("0x%" PRIx32, o.flags);
5086 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5087 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5088 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5089 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5090 swift_version = (o.flags >> 8) & 0xff;
5091 if (swift_version != 0) {
5092 if (swift_version == 1)
5093 outs() << " Swift 1.0";
5094 else if (swift_version == 2)
5095 outs() << " Swift 1.1";
5097 outs() << " unknown future Swift version (" << swift_version << ")";
5102 static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5103 uint32_t left, offset, p;
5104 struct imageInfo_t o;
5108 S.getName(SectName);
5109 DataRefImpl Ref = S.getRawDataRefImpl();
5110 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5111 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5113 r = get_pointer_32(p, offset, left, S, info);
5116 memset(&o, '\0', sizeof(struct imageInfo_t));
5117 if (left < sizeof(struct imageInfo_t)) {
5118 memcpy(&o, r, left);
5119 outs() << " (imageInfo entends past the end of the section)\n";
5121 memcpy(&o, r, sizeof(struct imageInfo_t));
5122 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5124 outs() << " version " << o.version << "\n";
5125 outs() << " flags " << format("0x%" PRIx32, o.flags);
5131 outs() << " GC-only";
5137 static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
5138 SymbolAddressMap AddrMap;
5140 CreateSymbolAddressMap(O, &AddrMap);
5142 std::vector<SectionRef> Sections;
5143 for (const SectionRef &Section : O->sections()) {
5145 Section.getName(SectName);
5146 Sections.push_back(Section);
5149 struct DisassembleInfo info;
5150 // Set up the block of info used by the Symbolizer call backs.
5151 info.verbose = verbose;
5153 info.AddrMap = &AddrMap;
5154 info.Sections = &Sections;
5155 info.class_name = nullptr;
5156 info.selector_name = nullptr;
5157 info.method = nullptr;
5158 info.demangled_name = nullptr;
5159 info.bindtable = nullptr;
5163 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5164 if (CL != SectionRef()) {
5166 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5168 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5170 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5173 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5174 if (CR != SectionRef()) {
5176 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5178 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5180 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5183 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5184 if (SR != SectionRef()) {
5186 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5188 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5190 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5193 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5194 if (CA != SectionRef()) {
5196 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5198 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5200 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5203 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5204 if (PL != SectionRef()) {
5206 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5208 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5210 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5213 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5214 if (MR != SectionRef()) {
5216 print_message_refs64(MR, &info);
5218 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5220 print_message_refs64(MR, &info);
5223 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5224 if (II != SectionRef()) {
5226 print_image_info64(II, &info);
5228 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5230 print_image_info64(II, &info);
5233 if (info.bindtable != nullptr)
5234 delete info.bindtable;
5237 static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
5238 SymbolAddressMap AddrMap;
5240 CreateSymbolAddressMap(O, &AddrMap);
5242 std::vector<SectionRef> Sections;
5243 for (const SectionRef &Section : O->sections()) {
5245 Section.getName(SectName);
5246 Sections.push_back(Section);
5249 struct DisassembleInfo info;
5250 // Set up the block of info used by the Symbolizer call backs.
5251 info.verbose = verbose;
5253 info.AddrMap = &AddrMap;
5254 info.Sections = &Sections;
5255 info.class_name = nullptr;
5256 info.selector_name = nullptr;
5257 info.method = nullptr;
5258 info.demangled_name = nullptr;
5259 info.bindtable = nullptr;
5263 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5264 if (CL != SectionRef()) {
5266 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5268 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5270 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5273 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5274 if (CR != SectionRef()) {
5276 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5278 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5280 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5283 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5284 if (SR != SectionRef()) {
5286 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5288 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5290 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5293 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5294 if (CA != SectionRef()) {
5296 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5298 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5300 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5303 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5304 if (PL != SectionRef()) {
5306 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5308 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5310 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5313 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5314 if (MR != SectionRef()) {
5316 print_message_refs32(MR, &info);
5318 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5320 print_message_refs32(MR, &info);
5323 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5324 if (II != SectionRef()) {
5326 print_image_info32(II, &info);
5328 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5330 print_image_info32(II, &info);
5334 static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
5335 uint32_t i, j, p, offset, xoffset, left, defs_left, def;
5336 const char *r, *name, *defs;
5337 struct objc_module_t module;
5339 struct objc_symtab_t symtab;
5340 struct objc_class_t objc_class;
5341 struct objc_category_t objc_category;
5343 outs() << "Objective-C segment\n";
5344 S = get_section(O, "__OBJC", "__module_info");
5345 if (S == SectionRef())
5348 SymbolAddressMap AddrMap;
5350 CreateSymbolAddressMap(O, &AddrMap);
5352 std::vector<SectionRef> Sections;
5353 for (const SectionRef &Section : O->sections()) {
5355 Section.getName(SectName);
5356 Sections.push_back(Section);
5359 struct DisassembleInfo info;
5360 // Set up the block of info used by the Symbolizer call backs.
5361 info.verbose = verbose;
5363 info.AddrMap = &AddrMap;
5364 info.Sections = &Sections;
5365 info.class_name = nullptr;
5366 info.selector_name = nullptr;
5367 info.method = nullptr;
5368 info.demangled_name = nullptr;
5369 info.bindtable = nullptr;
5373 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
5374 p = S.getAddress() + i;
5375 r = get_pointer_32(p, offset, left, S, &info, true);
5378 memset(&module, '\0', sizeof(struct objc_module_t));
5379 if (left < sizeof(struct objc_module_t)) {
5380 memcpy(&module, r, left);
5381 outs() << " (module extends past end of __module_info section)\n";
5383 memcpy(&module, r, sizeof(struct objc_module_t));
5384 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5387 outs() << "Module " << format("0x%" PRIx32, p) << "\n";
5388 outs() << " version " << module.version << "\n";
5389 outs() << " size " << module.size << "\n";
5391 name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
5392 if (name != nullptr)
5393 outs() << format("%.*s", left, name);
5395 outs() << format("0x%08" PRIx32, module.name)
5396 << "(not in an __OBJC section)";
5399 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
5400 if (module.symtab == 0 || r == nullptr) {
5401 outs() << " symtab " << format("0x%08" PRIx32, module.symtab)
5402 << " (not in an __OBJC section)\n";
5405 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
5406 memset(&symtab, '\0', sizeof(struct objc_symtab_t));
5409 if (left < sizeof(struct objc_symtab_t)) {
5410 memcpy(&symtab, r, left);
5411 outs() << "\tsymtab extends past end of an __OBJC section)\n";
5413 memcpy(&symtab, r, sizeof(struct objc_symtab_t));
5414 if (left > sizeof(struct objc_symtab_t)) {
5415 defs_left = left - sizeof(struct objc_symtab_t);
5416 defs = r + sizeof(struct objc_symtab_t);
5419 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5422 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
5423 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
5424 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
5426 outs() << " (not in an __OBJC section)";
5428 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
5429 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
5430 if (symtab.cls_def_cnt > 0)
5431 outs() << "\tClass Definitions\n";
5432 for (j = 0; j < symtab.cls_def_cnt; j++) {
5433 if ((j + 1) * sizeof(uint32_t) > defs_left) {
5434 outs() << "\t(remaining class defs entries entends past the end of the "
5438 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
5439 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5440 sys::swapByteOrder(def);
5442 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5443 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
5445 if (left > sizeof(struct objc_class_t)) {
5447 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5449 outs() << " (entends past the end of the section)\n";
5450 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5451 memcpy(&objc_class, r, left);
5453 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5454 swapStruct(objc_class);
5455 print_objc_class_t(&objc_class, &info);
5457 outs() << "(not in an __OBJC section)\n";
5460 if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
5461 outs() << "\tMeta Class";
5462 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
5464 if (left > sizeof(struct objc_class_t)) {
5466 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5468 outs() << " (entends past the end of the section)\n";
5469 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5470 memcpy(&objc_class, r, left);
5472 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5473 swapStruct(objc_class);
5474 print_objc_class_t(&objc_class, &info);
5476 outs() << "(not in an __OBJC section)\n";
5480 if (symtab.cat_def_cnt > 0)
5481 outs() << "\tCategory Definitions\n";
5482 for (j = 0; j < symtab.cat_def_cnt; j++) {
5483 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
5484 outs() << "\t(remaining category defs entries entends past the end of "
5485 << "the section)\n";
5488 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
5490 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5491 sys::swapByteOrder(def);
5493 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5494 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
5495 << format("0x%08" PRIx32, def);
5497 if (left > sizeof(struct objc_category_t)) {
5499 memcpy(&objc_category, r, sizeof(struct objc_category_t));
5501 outs() << " (entends past the end of the section)\n";
5502 memset(&objc_category, '\0', sizeof(struct objc_category_t));
5503 memcpy(&objc_category, r, left);
5505 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5506 swapStruct(objc_category);
5507 print_objc_objc_category_t(&objc_category, &info);
5509 outs() << "(not in an __OBJC section)\n";
5513 const SectionRef II = get_section(O, "__OBJC", "__image_info");
5514 if (II != SectionRef())
5515 print_image_info(II, &info);
5520 static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
5521 uint32_t size, uint32_t addr) {
5522 SymbolAddressMap AddrMap;
5523 CreateSymbolAddressMap(O, &AddrMap);
5525 std::vector<SectionRef> Sections;
5526 for (const SectionRef &Section : O->sections()) {
5528 Section.getName(SectName);
5529 Sections.push_back(Section);
5532 struct DisassembleInfo info;
5533 // Set up the block of info used by the Symbolizer call backs.
5534 info.verbose = true;
5536 info.AddrMap = &AddrMap;
5537 info.Sections = &Sections;
5538 info.class_name = nullptr;
5539 info.selector_name = nullptr;
5540 info.method = nullptr;
5541 info.demangled_name = nullptr;
5542 info.bindtable = nullptr;
5547 struct objc_protocol_t protocol;
5548 uint32_t left, paddr;
5549 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
5550 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
5551 left = size - (p - sect);
5552 if (left < sizeof(struct objc_protocol_t)) {
5553 outs() << "Protocol extends past end of __protocol section\n";
5554 memcpy(&protocol, p, left);
5556 memcpy(&protocol, p, sizeof(struct objc_protocol_t));
5557 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5558 swapStruct(protocol);
5559 paddr = addr + (p - sect);
5560 outs() << "Protocol " << format("0x%" PRIx32, paddr);
5561 if (print_protocol(paddr, 0, &info))
5562 outs() << "(not in an __OBJC section)\n";
5566 static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
5568 printObjc2_64bit_MetaData(O, verbose);
5570 MachO::mach_header H;
5572 if (H.cputype == MachO::CPU_TYPE_ARM)
5573 printObjc2_32bit_MetaData(O, verbose);
5575 // This is the 32-bit non-arm cputype case. Which is normally
5576 // the first Objective-C ABI. But it may be the case of a
5577 // binary for the iOS simulator which is the second Objective-C
5578 // ABI. In that case printObjc1_32bit_MetaData() will determine that
5579 // and return false.
5580 if (printObjc1_32bit_MetaData(O, verbose) == false)
5581 printObjc2_32bit_MetaData(O, verbose);
5586 // GuessLiteralPointer returns a string which for the item in the Mach-O file
5587 // for the address passed in as ReferenceValue for printing as a comment with
5588 // the instruction and also returns the corresponding type of that item
5589 // indirectly through ReferenceType.
5591 // If ReferenceValue is an address of literal cstring then a pointer to the
5592 // cstring is returned and ReferenceType is set to
5593 // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
5595 // If ReferenceValue is an address of an Objective-C CFString, Selector ref or
5596 // Class ref that name is returned and the ReferenceType is set accordingly.
5598 // Lastly, literals which are Symbol address in a literal pool are looked for
5599 // and if found the symbol name is returned and ReferenceType is set to
5600 // LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
5602 // If there is no item in the Mach-O file for the address passed in as
5603 // ReferenceValue nullptr is returned and ReferenceType is unchanged.
5604 static const char *GuessLiteralPointer(uint64_t ReferenceValue,
5605 uint64_t ReferencePC,
5606 uint64_t *ReferenceType,
5607 struct DisassembleInfo *info) {
5608 // First see if there is an external relocation entry at the ReferencePC.
5609 uint64_t sect_addr = info->S.getAddress();
5610 uint64_t sect_offset = ReferencePC - sect_addr;
5611 bool reloc_found = false;
5613 MachO::any_relocation_info RE;
5614 bool isExtern = false;
5616 for (const RelocationRef &Reloc : info->S.relocations()) {
5617 uint64_t RelocOffset;
5618 Reloc.getOffset(RelocOffset);
5619 if (RelocOffset == sect_offset) {
5620 Rel = Reloc.getRawDataRefImpl();
5621 RE = info->O->getRelocation(Rel);
5622 if (info->O->isRelocationScattered(RE))
5624 isExtern = info->O->getPlainRelocationExternal(RE);
5626 symbol_iterator RelocSym = Reloc.getSymbol();
5633 // If there is an external relocation entry for a symbol in a section
5634 // then used that symbol's value for the value of the reference.
5635 if (reloc_found && isExtern) {
5636 if (info->O->getAnyRelocationPCRel(RE)) {
5637 unsigned Type = info->O->getAnyRelocationType(RE);
5638 if (Type == MachO::X86_64_RELOC_SIGNED) {
5639 Symbol.getAddress(ReferenceValue);
5644 // Look for literals such as Objective-C CFStrings refs, Selector refs,
5645 // Message refs and Class refs.
5646 bool classref, selref, msgref, cfstring;
5647 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
5648 selref, msgref, cfstring);
5649 if (classref && pointer_value == 0) {
5650 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
5651 // And the pointer_value in that section is typically zero as it will be
5652 // set by dyld as part of the "bind information".
5653 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
5654 if (name != nullptr) {
5655 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
5656 const char *class_name = strrchr(name, '$');
5657 if (class_name != nullptr && class_name[1] == '_' &&
5658 class_name[2] != '\0') {
5659 info->class_name = class_name + 2;
5666 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
5668 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
5669 if (name != nullptr)
5670 info->class_name = name;
5672 name = "bad class ref";
5677 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
5678 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
5682 if (selref && pointer_value == 0)
5683 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
5685 if (pointer_value != 0)
5686 ReferenceValue = pointer_value;
5688 const char *name = GuessCstringPointer(ReferenceValue, info);
5690 if (pointer_value != 0 && selref) {
5691 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
5692 info->selector_name = name;
5693 } else if (pointer_value != 0 && msgref) {
5694 info->class_name = nullptr;
5695 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
5696 info->selector_name = name;
5698 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
5702 // Lastly look for an indirect symbol with this ReferenceValue which is in
5703 // a literal pool. If found return that symbol name.
5704 name = GuessIndirectSymbol(ReferenceValue, info);
5706 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
5713 // SymbolizerSymbolLookUp is the symbol lookup function passed when creating
5714 // the Symbolizer. It looks up the ReferenceValue using the info passed via the
5715 // pointer to the struct DisassembleInfo that was passed when MCSymbolizer
5716 // is created and returns the symbol name that matches the ReferenceValue or
5717 // nullptr if none. The ReferenceType is passed in for the IN type of
5718 // reference the instruction is making from the values in defined in the header
5719 // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
5720 // Out type and the ReferenceName will also be set which is added as a comment
5721 // to the disassembled instruction.
5724 // If the symbol name is a C++ mangled name then the demangled name is
5725 // returned through ReferenceName and ReferenceType is set to
5726 // LLVMDisassembler_ReferenceType_DeMangled_Name .
5729 // When this is called to get a symbol name for a branch target then the
5730 // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
5731 // SymbolValue will be looked for in the indirect symbol table to determine if
5732 // it is an address for a symbol stub. If so then the symbol name for that
5733 // stub is returned indirectly through ReferenceName and then ReferenceType is
5734 // set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
5736 // When this is called with an value loaded via a PC relative load then
5737 // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
5738 // SymbolValue is checked to be an address of literal pointer, symbol pointer,
5739 // or an Objective-C meta data reference. If so the output ReferenceType is
5740 // set to correspond to that as well as setting the ReferenceName.
5741 static const char *SymbolizerSymbolLookUp(void *DisInfo,
5742 uint64_t ReferenceValue,
5743 uint64_t *ReferenceType,
5744 uint64_t ReferencePC,
5745 const char **ReferenceName) {
5746 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
5747 // If no verbose symbolic information is wanted then just return nullptr.
5748 if (!info->verbose) {
5749 *ReferenceName = nullptr;
5750 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5754 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
5756 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
5757 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
5758 if (*ReferenceName != nullptr) {
5759 method_reference(info, ReferenceType, ReferenceName);
5760 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
5761 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
5764 if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
5765 if (info->demangled_name != nullptr)
5766 free(info->demangled_name);
5768 info->demangled_name =
5769 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
5770 if (info->demangled_name != nullptr) {
5771 *ReferenceName = info->demangled_name;
5772 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5774 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5777 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5778 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
5780 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5782 method_reference(info, ReferenceType, ReferenceName);
5784 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5785 // If this is arm64 and the reference is an adrp instruction save the
5786 // instruction, passed in ReferenceValue and the address of the instruction
5787 // for use later if we see and add immediate instruction.
5788 } else if (info->O->getArch() == Triple::aarch64 &&
5789 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
5790 info->adrp_inst = ReferenceValue;
5791 info->adrp_addr = ReferencePC;
5792 SymbolName = nullptr;
5793 *ReferenceName = nullptr;
5794 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5795 // If this is arm64 and reference is an add immediate instruction and we
5797 // seen an adrp instruction just before it and the adrp's Xd register
5799 // this add's Xn register reconstruct the value being referenced and look to
5800 // see if it is a literal pointer. Note the add immediate instruction is
5801 // passed in ReferenceValue.
5802 } else if (info->O->getArch() == Triple::aarch64 &&
5803 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
5804 ReferencePC - 4 == info->adrp_addr &&
5805 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5806 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5807 uint32_t addxri_inst;
5808 uint64_t adrp_imm, addxri_imm;
5811 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5812 if (info->adrp_inst & 0x0200000)
5813 adrp_imm |= 0xfffffffffc000000LL;
5815 addxri_inst = ReferenceValue;
5816 addxri_imm = (addxri_inst >> 10) & 0xfff;
5817 if (((addxri_inst >> 22) & 0x3) == 1)
5820 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5821 (adrp_imm << 12) + addxri_imm;
5824 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5825 if (*ReferenceName == nullptr)
5826 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5827 // If this is arm64 and the reference is a load register instruction and we
5828 // have seen an adrp instruction just before it and the adrp's Xd register
5829 // matches this add's Xn register reconstruct the value being referenced and
5830 // look to see if it is a literal pointer. Note the load register
5831 // instruction is passed in ReferenceValue.
5832 } else if (info->O->getArch() == Triple::aarch64 &&
5833 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
5834 ReferencePC - 4 == info->adrp_addr &&
5835 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
5836 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
5837 uint32_t ldrxui_inst;
5838 uint64_t adrp_imm, ldrxui_imm;
5841 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
5842 if (info->adrp_inst & 0x0200000)
5843 adrp_imm |= 0xfffffffffc000000LL;
5845 ldrxui_inst = ReferenceValue;
5846 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
5848 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
5849 (adrp_imm << 12) + (ldrxui_imm << 3);
5852 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5853 if (*ReferenceName == nullptr)
5854 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5856 // If this arm64 and is an load register (PC-relative) instruction the
5857 // ReferenceValue is the PC plus the immediate value.
5858 else if (info->O->getArch() == Triple::aarch64 &&
5859 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
5860 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
5862 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
5863 if (*ReferenceName == nullptr)
5864 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5867 else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
5868 if (info->demangled_name != nullptr)
5869 free(info->demangled_name);
5871 info->demangled_name =
5872 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
5873 if (info->demangled_name != nullptr) {
5874 *ReferenceName = info->demangled_name;
5875 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
5880 *ReferenceName = nullptr;
5881 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
5887 /// \brief Emits the comments that are stored in the CommentStream.
5888 /// Each comment in the CommentStream must end with a newline.
5889 static void emitComments(raw_svector_ostream &CommentStream,
5890 SmallString<128> &CommentsToEmit,
5891 formatted_raw_ostream &FormattedOS,
5892 const MCAsmInfo &MAI) {
5893 // Flush the stream before taking its content.
5894 CommentStream.flush();
5895 StringRef Comments = CommentsToEmit.str();
5896 // Get the default information for printing a comment.
5897 const char *CommentBegin = MAI.getCommentString();
5898 unsigned CommentColumn = MAI.getCommentColumn();
5899 bool IsFirst = true;
5900 while (!Comments.empty()) {
5902 FormattedOS << '\n';
5903 // Emit a line of comments.
5904 FormattedOS.PadToColumn(CommentColumn);
5905 size_t Position = Comments.find('\n');
5906 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
5907 // Move after the newline character.
5908 Comments = Comments.substr(Position + 1);
5911 FormattedOS.flush();
5913 // Tell the comment stream that the vector changed underneath it.
5914 CommentsToEmit.clear();
5915 CommentStream.resync();
5918 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
5919 StringRef DisSegName, StringRef DisSectName) {
5920 const char *McpuDefault = nullptr;
5921 const Target *ThumbTarget = nullptr;
5922 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
5924 // GetTarget prints out stuff.
5927 if (MCPU.empty() && McpuDefault)
5930 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
5931 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
5933 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
5935 // Package up features to be passed to target/subtarget
5936 std::string FeaturesStr;
5937 if (MAttrs.size()) {
5938 SubtargetFeatures Features;
5939 for (unsigned i = 0; i != MAttrs.size(); ++i)
5940 Features.AddFeature(MAttrs[i]);
5941 FeaturesStr = Features.getString();
5944 // Set up disassembler.
5945 std::unique_ptr<const MCRegisterInfo> MRI(
5946 TheTarget->createMCRegInfo(TripleName));
5947 std::unique_ptr<const MCAsmInfo> AsmInfo(
5948 TheTarget->createMCAsmInfo(*MRI, TripleName));
5949 std::unique_ptr<const MCSubtargetInfo> STI(
5950 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
5951 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
5952 std::unique_ptr<MCDisassembler> DisAsm(
5953 TheTarget->createMCDisassembler(*STI, Ctx));
5954 std::unique_ptr<MCSymbolizer> Symbolizer;
5955 struct DisassembleInfo SymbolizerInfo;
5956 std::unique_ptr<MCRelocationInfo> RelInfo(
5957 TheTarget->createMCRelocationInfo(TripleName, Ctx));
5959 Symbolizer.reset(TheTarget->createMCSymbolizer(
5960 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
5961 &SymbolizerInfo, &Ctx, std::move(RelInfo)));
5962 DisAsm->setSymbolizer(std::move(Symbolizer));
5964 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
5965 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
5966 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
5967 // Set the display preference for hex vs. decimal immediates.
5968 IP->setPrintImmHex(PrintImmHex);
5969 // Comment stream and backing vector.
5970 SmallString<128> CommentsToEmit;
5971 raw_svector_ostream CommentStream(CommentsToEmit);
5972 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
5973 // if it is done then arm64 comments for string literals don't get printed
5974 // and some constant get printed instead and not setting it causes intel
5975 // (32-bit and 64-bit) comments printed with different spacing before the
5976 // comment causing different diffs with the 'C' disassembler library API.
5977 // IP->setCommentStream(CommentStream);
5979 if (!AsmInfo || !STI || !DisAsm || !IP) {
5980 errs() << "error: couldn't initialize disassembler for target "
5981 << TripleName << '\n';
5985 // Set up thumb disassembler.
5986 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
5987 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
5988 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
5989 std::unique_ptr<MCDisassembler> ThumbDisAsm;
5990 std::unique_ptr<MCInstPrinter> ThumbIP;
5991 std::unique_ptr<MCContext> ThumbCtx;
5992 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
5993 struct DisassembleInfo ThumbSymbolizerInfo;
5994 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
5996 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
5998 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
6000 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr));
6001 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
6002 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
6003 MCContext *PtrThumbCtx = ThumbCtx.get();
6005 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
6007 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
6008 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
6009 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
6010 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
6012 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
6013 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
6014 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
6015 *ThumbInstrInfo, *ThumbMRI));
6016 // Set the display preference for hex vs. decimal immediates.
6017 ThumbIP->setPrintImmHex(PrintImmHex);
6020 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
6021 errs() << "error: couldn't initialize disassembler for target "
6022 << ThumbTripleName << '\n';
6026 MachO::mach_header Header = MachOOF->getHeader();
6028 // FIXME: Using the -cfg command line option, this code used to be able to
6029 // annotate relocations with the referenced symbol's name, and if this was
6030 // inside a __[cf]string section, the data it points to. This is now replaced
6031 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
6032 std::vector<SectionRef> Sections;
6033 std::vector<SymbolRef> Symbols;
6034 SmallVector<uint64_t, 8> FoundFns;
6035 uint64_t BaseSegmentAddress;
6037 getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
6038 BaseSegmentAddress);
6040 // Sort the symbols by address, just in case they didn't come in that way.
6041 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
6043 // Build a data in code table that is sorted on by the address of each entry.
6044 uint64_t BaseAddress = 0;
6045 if (Header.filetype == MachO::MH_OBJECT)
6046 BaseAddress = Sections[0].getAddress();
6048 BaseAddress = BaseSegmentAddress;
6050 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
6053 DI->getOffset(Offset);
6054 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
6056 array_pod_sort(Dices.begin(), Dices.end());
6059 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
6061 raw_ostream &DebugOut = nulls();
6064 std::unique_ptr<DIContext> diContext;
6065 ObjectFile *DbgObj = MachOOF;
6066 // Try to find debug info and set up the DIContext for it.
6068 // A separate DSym file path was specified, parse it as a macho file,
6069 // get the sections and supply it to the section name parsing machinery.
6070 if (!DSYMFile.empty()) {
6071 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
6072 MemoryBuffer::getFileOrSTDIN(DSYMFile);
6073 if (std::error_code EC = BufOrErr.getError()) {
6074 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
6078 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
6083 // Setup the DIContext
6084 diContext.reset(new DWARFContextInMemory(*DbgObj));
6087 if (DumpSections.size() == 0)
6088 outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
6090 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
6092 if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
6095 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
6097 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
6098 if (SegmentName != DisSegName)
6102 Sections[SectIdx].getContents(BytesStr);
6103 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
6105 uint64_t SectAddress = Sections[SectIdx].getAddress();
6107 bool symbolTableWorked = false;
6109 // Parse relocations.
6110 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
6111 for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) {
6112 uint64_t RelocOffset;
6113 Reloc.getOffset(RelocOffset);
6114 uint64_t SectionAddress = Sections[SectIdx].getAddress();
6115 RelocOffset -= SectionAddress;
6117 symbol_iterator RelocSym = Reloc.getSymbol();
6119 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
6121 array_pod_sort(Relocs.begin(), Relocs.end());
6123 // Create a map of symbol addresses to symbol names for use by
6124 // the SymbolizerSymbolLookUp() routine.
6125 SymbolAddressMap AddrMap;
6126 bool DisSymNameFound = false;
6127 for (const SymbolRef &Symbol : MachOOF->symbols()) {
6130 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
6131 ST == SymbolRef::ST_Other) {
6133 Symbol.getAddress(Address);
6135 Symbol.getName(SymName);
6136 AddrMap[Address] = SymName;
6137 if (!DisSymName.empty() && DisSymName == SymName)
6138 DisSymNameFound = true;
6141 if (!DisSymName.empty() && !DisSymNameFound) {
6142 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
6145 // Set up the block of info used by the Symbolizer call backs.
6146 SymbolizerInfo.verbose = !NoSymbolicOperands;
6147 SymbolizerInfo.O = MachOOF;
6148 SymbolizerInfo.S = Sections[SectIdx];
6149 SymbolizerInfo.AddrMap = &AddrMap;
6150 SymbolizerInfo.Sections = &Sections;
6151 SymbolizerInfo.class_name = nullptr;
6152 SymbolizerInfo.selector_name = nullptr;
6153 SymbolizerInfo.method = nullptr;
6154 SymbolizerInfo.demangled_name = nullptr;
6155 SymbolizerInfo.bindtable = nullptr;
6156 SymbolizerInfo.adrp_addr = 0;
6157 SymbolizerInfo.adrp_inst = 0;
6158 // Same for the ThumbSymbolizer
6159 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
6160 ThumbSymbolizerInfo.O = MachOOF;
6161 ThumbSymbolizerInfo.S = Sections[SectIdx];
6162 ThumbSymbolizerInfo.AddrMap = &AddrMap;
6163 ThumbSymbolizerInfo.Sections = &Sections;
6164 ThumbSymbolizerInfo.class_name = nullptr;
6165 ThumbSymbolizerInfo.selector_name = nullptr;
6166 ThumbSymbolizerInfo.method = nullptr;
6167 ThumbSymbolizerInfo.demangled_name = nullptr;
6168 ThumbSymbolizerInfo.bindtable = nullptr;
6169 ThumbSymbolizerInfo.adrp_addr = 0;
6170 ThumbSymbolizerInfo.adrp_inst = 0;
6172 // Disassemble symbol by symbol.
6173 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
6175 Symbols[SymIdx].getName(SymName);
6178 Symbols[SymIdx].getType(ST);
6179 if (ST != SymbolRef::ST_Function)
6182 // Make sure the symbol is defined in this section.
6183 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
6187 // If we are only disassembling one symbol see if this is that symbol.
6188 if (!DisSymName.empty() && DisSymName != SymName)
6191 // Start at the address of the symbol relative to the section's address.
6193 uint64_t SectionAddress = Sections[SectIdx].getAddress();
6194 Symbols[SymIdx].getAddress(Start);
6195 Start -= SectionAddress;
6197 // Stop disassembling either at the beginning of the next symbol or at
6198 // the end of the section.
6199 bool containsNextSym = false;
6200 uint64_t NextSym = 0;
6201 uint64_t NextSymIdx = SymIdx + 1;
6202 while (Symbols.size() > NextSymIdx) {
6203 SymbolRef::Type NextSymType;
6204 Symbols[NextSymIdx].getType(NextSymType);
6205 if (NextSymType == SymbolRef::ST_Function) {
6207 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
6208 Symbols[NextSymIdx].getAddress(NextSym);
6209 NextSym -= SectionAddress;
6215 uint64_t SectSize = Sections[SectIdx].getSize();
6216 uint64_t End = containsNextSym ? NextSym : SectSize;
6219 symbolTableWorked = true;
6221 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
6223 (MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget;
6225 outs() << SymName << ":\n";
6226 DILineInfo lastLine;
6227 for (uint64_t Index = Start; Index < End; Index += Size) {
6230 uint64_t PC = SectAddress + Index;
6231 if (!NoLeadingAddr) {
6232 if (FullLeadingAddr) {
6233 if (MachOOF->is64Bit())
6234 outs() << format("%016" PRIx64, PC);
6236 outs() << format("%08" PRIx64, PC);
6238 outs() << format("%8" PRIx64 ":", PC);
6244 // Check the data in code table here to see if this is data not an
6245 // instruction to be disassembled.
6247 Dice.push_back(std::make_pair(PC, DiceRef()));
6248 dice_table_iterator DTI =
6249 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
6250 compareDiceTableEntries);
6251 if (DTI != Dices.end()) {
6253 DTI->second.getLength(Length);
6255 DTI->second.getKind(Kind);
6256 Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
6257 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
6258 (PC == (DTI->first + Length - 1)) && (Length & 1))
6263 SmallVector<char, 64> AnnotationsBytes;
6264 raw_svector_ostream Annotations(AnnotationsBytes);
6268 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
6269 PC, DebugOut, Annotations);
6271 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
6272 DebugOut, Annotations);
6274 if (!NoShowRawInsn) {
6275 dumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, Size), outs());
6277 formatted_raw_ostream FormattedOS(outs());
6278 Annotations.flush();
6279 StringRef AnnotationsStr = Annotations.str();
6281 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
6283 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
6284 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
6286 // Print debug info.
6288 DILineInfo dli = diContext->getLineInfoForAddress(PC);
6289 // Print valid line info if it changed.
6290 if (dli != lastLine && dli.Line != 0)
6291 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
6297 unsigned int Arch = MachOOF->getArch();
6298 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
6299 outs() << format("\t.byte 0x%02x #bad opcode\n",
6300 *(Bytes.data() + Index) & 0xff);
6301 Size = 1; // skip exactly one illegible byte and move on.
6302 } else if (Arch == Triple::aarch64) {
6303 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6304 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
6305 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
6306 (*(Bytes.data() + Index + 3) & 0xff) << 24;
6307 outs() << format("\t.long\t0x%08x\n", opcode);
6310 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6312 Size = 1; // skip illegible bytes
6317 if (!symbolTableWorked) {
6318 // Reading the symbol table didn't work, disassemble the whole section.
6319 uint64_t SectAddress = Sections[SectIdx].getAddress();
6320 uint64_t SectSize = Sections[SectIdx].getSize();
6322 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
6325 uint64_t PC = SectAddress + Index;
6326 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
6327 DebugOut, nulls())) {
6328 if (!NoLeadingAddr) {
6329 if (FullLeadingAddr) {
6330 if (MachOOF->is64Bit())
6331 outs() << format("%016" PRIx64, PC);
6333 outs() << format("%08" PRIx64, PC);
6335 outs() << format("%8" PRIx64 ":", PC);
6338 if (!NoShowRawInsn) {
6340 dumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, InstSize), outs());
6342 IP->printInst(&Inst, outs(), "", *STI);
6345 unsigned int Arch = MachOOF->getArch();
6346 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
6347 outs() << format("\t.byte 0x%02x #bad opcode\n",
6348 *(Bytes.data() + Index) & 0xff);
6349 InstSize = 1; // skip exactly one illegible byte and move on.
6351 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6353 InstSize = 1; // skip illegible bytes
6358 // The TripleName's need to be reset if we are called again for a different
6361 ThumbTripleName = "";
6363 if (SymbolizerInfo.method != nullptr)
6364 free(SymbolizerInfo.method);
6365 if (SymbolizerInfo.demangled_name != nullptr)
6366 free(SymbolizerInfo.demangled_name);
6367 if (SymbolizerInfo.bindtable != nullptr)
6368 delete SymbolizerInfo.bindtable;
6369 if (ThumbSymbolizerInfo.method != nullptr)
6370 free(ThumbSymbolizerInfo.method);
6371 if (ThumbSymbolizerInfo.demangled_name != nullptr)
6372 free(ThumbSymbolizerInfo.demangled_name);
6373 if (ThumbSymbolizerInfo.bindtable != nullptr)
6374 delete ThumbSymbolizerInfo.bindtable;
6378 //===----------------------------------------------------------------------===//
6379 // __compact_unwind section dumping
6380 //===----------------------------------------------------------------------===//
6384 template <typename T> static uint64_t readNext(const char *&Buf) {
6385 using llvm::support::little;
6386 using llvm::support::unaligned;
6388 uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
6393 struct CompactUnwindEntry {
6394 uint32_t OffsetInSection;
6396 uint64_t FunctionAddr;
6398 uint32_t CompactEncoding;
6399 uint64_t PersonalityAddr;
6402 RelocationRef FunctionReloc;
6403 RelocationRef PersonalityReloc;
6404 RelocationRef LSDAReloc;
6406 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
6407 : OffsetInSection(Offset) {
6409 read<uint64_t>(Contents.data() + Offset);
6411 read<uint32_t>(Contents.data() + Offset);
6415 template <typename UIntPtr> void read(const char *Buf) {
6416 FunctionAddr = readNext<UIntPtr>(Buf);
6417 Length = readNext<uint32_t>(Buf);
6418 CompactEncoding = readNext<uint32_t>(Buf);
6419 PersonalityAddr = readNext<UIntPtr>(Buf);
6420 LSDAAddr = readNext<UIntPtr>(Buf);
6425 /// Given a relocation from __compact_unwind, consisting of the RelocationRef
6426 /// and data being relocated, determine the best base Name and Addend to use for
6427 /// display purposes.
6429 /// 1. An Extern relocation will directly reference a symbol (and the data is
6430 /// then already an addend), so use that.
6431 /// 2. Otherwise the data is an offset in the object file's layout; try to find
6432 // a symbol before it in the same section, and use the offset from there.
6433 /// 3. Finally, if all that fails, fall back to an offset from the start of the
6434 /// referenced section.
6435 static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
6436 std::map<uint64_t, SymbolRef> &Symbols,
6437 const RelocationRef &Reloc, uint64_t Addr,
6438 StringRef &Name, uint64_t &Addend) {
6439 if (Reloc.getSymbol() != Obj->symbol_end()) {
6440 Reloc.getSymbol()->getName(Name);
6445 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
6446 SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
6448 uint64_t SectionAddr = RelocSection.getAddress();
6450 auto Sym = Symbols.upper_bound(Addr);
6451 if (Sym == Symbols.begin()) {
6452 // The first symbol in the object is after this reference, the best we can
6453 // do is section-relative notation.
6454 RelocSection.getName(Name);
6455 Addend = Addr - SectionAddr;
6459 // Go back one so that SymbolAddress <= Addr.
6462 section_iterator SymSection = Obj->section_end();
6463 Sym->second.getSection(SymSection);
6464 if (RelocSection == *SymSection) {
6465 // There's a valid symbol in the same section before this reference.
6466 Sym->second.getName(Name);
6467 Addend = Addr - Sym->first;
6471 // There is a symbol before this reference, but it's in a different
6472 // section. Probably not helpful to mention it, so use the section name.
6473 RelocSection.getName(Name);
6474 Addend = Addr - SectionAddr;
6477 static void printUnwindRelocDest(const MachOObjectFile *Obj,
6478 std::map<uint64_t, SymbolRef> &Symbols,
6479 const RelocationRef &Reloc, uint64_t Addr) {
6483 if (!Reloc.getObjectFile())
6486 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
6490 outs() << " + " << format("0x%" PRIx64, Addend);
6494 printMachOCompactUnwindSection(const MachOObjectFile *Obj,
6495 std::map<uint64_t, SymbolRef> &Symbols,
6496 const SectionRef &CompactUnwind) {
6498 assert(Obj->isLittleEndian() &&
6499 "There should not be a big-endian .o with __compact_unwind");
6501 bool Is64 = Obj->is64Bit();
6502 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
6503 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
6506 CompactUnwind.getContents(Contents);
6508 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
6510 // First populate the initial raw offsets, encodings and so on from the entry.
6511 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
6512 CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
6513 CompactUnwinds.push_back(Entry);
6516 // Next we need to look at the relocations to find out what objects are
6517 // actually being referred to.
6518 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
6519 uint64_t RelocAddress;
6520 Reloc.getOffset(RelocAddress);
6522 uint32_t EntryIdx = RelocAddress / EntrySize;
6523 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
6524 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
6526 if (OffsetInEntry == 0)
6527 Entry.FunctionReloc = Reloc;
6528 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
6529 Entry.PersonalityReloc = Reloc;
6530 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
6531 Entry.LSDAReloc = Reloc;
6533 llvm_unreachable("Unexpected relocation in __compact_unwind section");
6536 // Finally, we're ready to print the data we've gathered.
6537 outs() << "Contents of __compact_unwind section:\n";
6538 for (auto &Entry : CompactUnwinds) {
6539 outs() << " Entry at offset "
6540 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
6542 // 1. Start of the region this entry applies to.
6543 outs() << " start: " << format("0x%" PRIx64,
6544 Entry.FunctionAddr) << ' ';
6545 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
6548 // 2. Length of the region this entry applies to.
6549 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
6551 // 3. The 32-bit compact encoding.
6552 outs() << " compact encoding: "
6553 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
6555 // 4. The personality function, if present.
6556 if (Entry.PersonalityReloc.getObjectFile()) {
6557 outs() << " personality function: "
6558 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
6559 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
6560 Entry.PersonalityAddr);
6564 // 5. This entry's language-specific data area.
6565 if (Entry.LSDAReloc.getObjectFile()) {
6566 outs() << " LSDA: " << format("0x%" PRIx64,
6567 Entry.LSDAAddr) << ' ';
6568 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
6574 //===----------------------------------------------------------------------===//
6575 // __unwind_info section dumping
6576 //===----------------------------------------------------------------------===//
6578 static void printRegularSecondLevelUnwindPage(const char *PageStart) {
6579 const char *Pos = PageStart;
6580 uint32_t Kind = readNext<uint32_t>(Pos);
6582 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
6584 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6585 uint16_t NumEntries = readNext<uint16_t>(Pos);
6587 Pos = PageStart + EntriesStart;
6588 for (unsigned i = 0; i < NumEntries; ++i) {
6589 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6590 uint32_t Encoding = readNext<uint32_t>(Pos);
6592 outs() << " [" << i << "]: "
6593 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6595 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
6599 static void printCompressedSecondLevelUnwindPage(
6600 const char *PageStart, uint32_t FunctionBase,
6601 const SmallVectorImpl<uint32_t> &CommonEncodings) {
6602 const char *Pos = PageStart;
6603 uint32_t Kind = readNext<uint32_t>(Pos);
6605 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
6607 uint16_t EntriesStart = readNext<uint16_t>(Pos);
6608 uint16_t NumEntries = readNext<uint16_t>(Pos);
6610 uint16_t EncodingsStart = readNext<uint16_t>(Pos);
6611 readNext<uint16_t>(Pos);
6612 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
6613 PageStart + EncodingsStart);
6615 Pos = PageStart + EntriesStart;
6616 for (unsigned i = 0; i < NumEntries; ++i) {
6617 uint32_t Entry = readNext<uint32_t>(Pos);
6618 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
6619 uint32_t EncodingIdx = Entry >> 24;
6622 if (EncodingIdx < CommonEncodings.size())
6623 Encoding = CommonEncodings[EncodingIdx];
6625 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
6627 outs() << " [" << i << "]: "
6628 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6630 << "encoding[" << EncodingIdx
6631 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
6635 static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
6636 std::map<uint64_t, SymbolRef> &Symbols,
6637 const SectionRef &UnwindInfo) {
6639 assert(Obj->isLittleEndian() &&
6640 "There should not be a big-endian .o with __unwind_info");
6642 outs() << "Contents of __unwind_info section:\n";
6645 UnwindInfo.getContents(Contents);
6646 const char *Pos = Contents.data();
6648 //===----------------------------------
6650 //===----------------------------------
6652 uint32_t Version = readNext<uint32_t>(Pos);
6653 outs() << " Version: "
6654 << format("0x%" PRIx32, Version) << '\n';
6655 assert(Version == 1 && "only understand version 1");
6657 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
6658 outs() << " Common encodings array section offset: "
6659 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
6660 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
6661 outs() << " Number of common encodings in array: "
6662 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
6664 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
6665 outs() << " Personality function array section offset: "
6666 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
6667 uint32_t NumPersonalities = readNext<uint32_t>(Pos);
6668 outs() << " Number of personality functions in array: "
6669 << format("0x%" PRIx32, NumPersonalities) << '\n';
6671 uint32_t IndicesStart = readNext<uint32_t>(Pos);
6672 outs() << " Index array section offset: "
6673 << format("0x%" PRIx32, IndicesStart) << '\n';
6674 uint32_t NumIndices = readNext<uint32_t>(Pos);
6675 outs() << " Number of indices in array: "
6676 << format("0x%" PRIx32, NumIndices) << '\n';
6678 //===----------------------------------
6679 // A shared list of common encodings
6680 //===----------------------------------
6682 // These occupy indices in the range [0, N] whenever an encoding is referenced
6683 // from a compressed 2nd level index table. In practice the linker only
6684 // creates ~128 of these, so that indices are available to embed encodings in
6685 // the 2nd level index.
6687 SmallVector<uint32_t, 64> CommonEncodings;
6688 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
6689 Pos = Contents.data() + CommonEncodingsStart;
6690 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
6691 uint32_t Encoding = readNext<uint32_t>(Pos);
6692 CommonEncodings.push_back(Encoding);
6694 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
6698 //===----------------------------------
6699 // Personality functions used in this executable
6700 //===----------------------------------
6702 // There should be only a handful of these (one per source language,
6703 // roughly). Particularly since they only get 2 bits in the compact encoding.
6705 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
6706 Pos = Contents.data() + PersonalitiesStart;
6707 for (unsigned i = 0; i < NumPersonalities; ++i) {
6708 uint32_t PersonalityFn = readNext<uint32_t>(Pos);
6709 outs() << " personality[" << i + 1
6710 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
6713 //===----------------------------------
6714 // The level 1 index entries
6715 //===----------------------------------
6717 // These specify an approximate place to start searching for the more detailed
6718 // information, sorted by PC.
6721 uint32_t FunctionOffset;
6722 uint32_t SecondLevelPageStart;
6726 SmallVector<IndexEntry, 4> IndexEntries;
6728 outs() << " Top level indices: (count = " << NumIndices << ")\n";
6729 Pos = Contents.data() + IndicesStart;
6730 for (unsigned i = 0; i < NumIndices; ++i) {
6733 Entry.FunctionOffset = readNext<uint32_t>(Pos);
6734 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
6735 Entry.LSDAStart = readNext<uint32_t>(Pos);
6736 IndexEntries.push_back(Entry);
6738 outs() << " [" << i << "]: "
6739 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
6741 << "2nd level page offset="
6742 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
6743 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
6746 //===----------------------------------
6747 // Next come the LSDA tables
6748 //===----------------------------------
6750 // The LSDA layout is rather implicit: it's a contiguous array of entries from
6751 // the first top-level index's LSDAOffset to the last (sentinel).
6753 outs() << " LSDA descriptors:\n";
6754 Pos = Contents.data() + IndexEntries[0].LSDAStart;
6755 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
6756 (2 * sizeof(uint32_t));
6757 for (int i = 0; i < NumLSDAs; ++i) {
6758 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
6759 uint32_t LSDAOffset = readNext<uint32_t>(Pos);
6760 outs() << " [" << i << "]: "
6761 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
6763 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
6766 //===----------------------------------
6767 // Finally, the 2nd level indices
6768 //===----------------------------------
6770 // Generally these are 4K in size, and have 2 possible forms:
6771 // + Regular stores up to 511 entries with disparate encodings
6772 // + Compressed stores up to 1021 entries if few enough compact encoding
6774 outs() << " Second level indices:\n";
6775 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
6776 // The final sentinel top-level index has no associated 2nd level page
6777 if (IndexEntries[i].SecondLevelPageStart == 0)
6780 outs() << " Second level index[" << i << "]: "
6781 << "offset in section="
6782 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
6784 << "base function offset="
6785 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
6787 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
6788 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
6790 printRegularSecondLevelUnwindPage(Pos);
6792 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
6795 llvm_unreachable("Do not know how to print this kind of 2nd level page");
6799 void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
6800 std::map<uint64_t, SymbolRef> Symbols;
6801 for (const SymbolRef &SymRef : Obj->symbols()) {
6802 // Discard any undefined or absolute symbols. They're not going to take part
6803 // in the convenience lookup for unwind info and just take up resources.
6804 section_iterator Section = Obj->section_end();
6805 SymRef.getSection(Section);
6806 if (Section == Obj->section_end())
6810 SymRef.getAddress(Addr);
6811 Symbols.insert(std::make_pair(Addr, SymRef));
6814 for (const SectionRef &Section : Obj->sections()) {
6816 Section.getName(SectName);
6817 if (SectName == "__compact_unwind")
6818 printMachOCompactUnwindSection(Obj, Symbols, Section);
6819 else if (SectName == "__unwind_info")
6820 printMachOUnwindInfoSection(Obj, Symbols, Section);
6821 else if (SectName == "__eh_frame")
6822 outs() << "llvm-objdump: warning: unhandled __eh_frame section\n";
6826 static void PrintMachHeader(uint32_t magic, uint32_t cputype,
6827 uint32_t cpusubtype, uint32_t filetype,
6828 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
6830 outs() << "Mach header\n";
6831 outs() << " magic cputype cpusubtype caps filetype ncmds "
6832 "sizeofcmds flags\n";
6834 if (magic == MachO::MH_MAGIC)
6835 outs() << " MH_MAGIC";
6836 else if (magic == MachO::MH_MAGIC_64)
6837 outs() << "MH_MAGIC_64";
6839 outs() << format(" 0x%08" PRIx32, magic);
6841 case MachO::CPU_TYPE_I386:
6843 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6844 case MachO::CPU_SUBTYPE_I386_ALL:
6848 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6852 case MachO::CPU_TYPE_X86_64:
6853 outs() << " X86_64";
6854 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6855 case MachO::CPU_SUBTYPE_X86_64_ALL:
6858 case MachO::CPU_SUBTYPE_X86_64_H:
6859 outs() << " Haswell";
6862 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6866 case MachO::CPU_TYPE_ARM:
6868 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6869 case MachO::CPU_SUBTYPE_ARM_ALL:
6872 case MachO::CPU_SUBTYPE_ARM_V4T:
6875 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
6878 case MachO::CPU_SUBTYPE_ARM_XSCALE:
6879 outs() << " XSCALE";
6881 case MachO::CPU_SUBTYPE_ARM_V6:
6884 case MachO::CPU_SUBTYPE_ARM_V6M:
6887 case MachO::CPU_SUBTYPE_ARM_V7:
6890 case MachO::CPU_SUBTYPE_ARM_V7EM:
6893 case MachO::CPU_SUBTYPE_ARM_V7K:
6896 case MachO::CPU_SUBTYPE_ARM_V7M:
6899 case MachO::CPU_SUBTYPE_ARM_V7S:
6903 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6907 case MachO::CPU_TYPE_ARM64:
6909 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6910 case MachO::CPU_SUBTYPE_ARM64_ALL:
6914 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6918 case MachO::CPU_TYPE_POWERPC:
6920 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6921 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6925 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6929 case MachO::CPU_TYPE_POWERPC64:
6931 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
6932 case MachO::CPU_SUBTYPE_POWERPC_ALL:
6936 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
6941 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
6944 outs() << format(" 0x%02" PRIx32,
6945 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
6948 case MachO::MH_OBJECT:
6949 outs() << " OBJECT";
6951 case MachO::MH_EXECUTE:
6952 outs() << " EXECUTE";
6954 case MachO::MH_FVMLIB:
6955 outs() << " FVMLIB";
6957 case MachO::MH_CORE:
6960 case MachO::MH_PRELOAD:
6961 outs() << " PRELOAD";
6963 case MachO::MH_DYLIB:
6966 case MachO::MH_DYLIB_STUB:
6967 outs() << " DYLIB_STUB";
6969 case MachO::MH_DYLINKER:
6970 outs() << " DYLINKER";
6972 case MachO::MH_BUNDLE:
6973 outs() << " BUNDLE";
6975 case MachO::MH_DSYM:
6978 case MachO::MH_KEXT_BUNDLE:
6979 outs() << " KEXTBUNDLE";
6982 outs() << format(" %10u", filetype);
6985 outs() << format(" %5u", ncmds);
6986 outs() << format(" %10u", sizeofcmds);
6988 if (f & MachO::MH_NOUNDEFS) {
6989 outs() << " NOUNDEFS";
6990 f &= ~MachO::MH_NOUNDEFS;
6992 if (f & MachO::MH_INCRLINK) {
6993 outs() << " INCRLINK";
6994 f &= ~MachO::MH_INCRLINK;
6996 if (f & MachO::MH_DYLDLINK) {
6997 outs() << " DYLDLINK";
6998 f &= ~MachO::MH_DYLDLINK;
7000 if (f & MachO::MH_BINDATLOAD) {
7001 outs() << " BINDATLOAD";
7002 f &= ~MachO::MH_BINDATLOAD;
7004 if (f & MachO::MH_PREBOUND) {
7005 outs() << " PREBOUND";
7006 f &= ~MachO::MH_PREBOUND;
7008 if (f & MachO::MH_SPLIT_SEGS) {
7009 outs() << " SPLIT_SEGS";
7010 f &= ~MachO::MH_SPLIT_SEGS;
7012 if (f & MachO::MH_LAZY_INIT) {
7013 outs() << " LAZY_INIT";
7014 f &= ~MachO::MH_LAZY_INIT;
7016 if (f & MachO::MH_TWOLEVEL) {
7017 outs() << " TWOLEVEL";
7018 f &= ~MachO::MH_TWOLEVEL;
7020 if (f & MachO::MH_FORCE_FLAT) {
7021 outs() << " FORCE_FLAT";
7022 f &= ~MachO::MH_FORCE_FLAT;
7024 if (f & MachO::MH_NOMULTIDEFS) {
7025 outs() << " NOMULTIDEFS";
7026 f &= ~MachO::MH_NOMULTIDEFS;
7028 if (f & MachO::MH_NOFIXPREBINDING) {
7029 outs() << " NOFIXPREBINDING";
7030 f &= ~MachO::MH_NOFIXPREBINDING;
7032 if (f & MachO::MH_PREBINDABLE) {
7033 outs() << " PREBINDABLE";
7034 f &= ~MachO::MH_PREBINDABLE;
7036 if (f & MachO::MH_ALLMODSBOUND) {
7037 outs() << " ALLMODSBOUND";
7038 f &= ~MachO::MH_ALLMODSBOUND;
7040 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
7041 outs() << " SUBSECTIONS_VIA_SYMBOLS";
7042 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
7044 if (f & MachO::MH_CANONICAL) {
7045 outs() << " CANONICAL";
7046 f &= ~MachO::MH_CANONICAL;
7048 if (f & MachO::MH_WEAK_DEFINES) {
7049 outs() << " WEAK_DEFINES";
7050 f &= ~MachO::MH_WEAK_DEFINES;
7052 if (f & MachO::MH_BINDS_TO_WEAK) {
7053 outs() << " BINDS_TO_WEAK";
7054 f &= ~MachO::MH_BINDS_TO_WEAK;
7056 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
7057 outs() << " ALLOW_STACK_EXECUTION";
7058 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
7060 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
7061 outs() << " DEAD_STRIPPABLE_DYLIB";
7062 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
7064 if (f & MachO::MH_PIE) {
7066 f &= ~MachO::MH_PIE;
7068 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
7069 outs() << " NO_REEXPORTED_DYLIBS";
7070 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
7072 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
7073 outs() << " MH_HAS_TLV_DESCRIPTORS";
7074 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
7076 if (f & MachO::MH_NO_HEAP_EXECUTION) {
7077 outs() << " MH_NO_HEAP_EXECUTION";
7078 f &= ~MachO::MH_NO_HEAP_EXECUTION;
7080 if (f & MachO::MH_APP_EXTENSION_SAFE) {
7081 outs() << " APP_EXTENSION_SAFE";
7082 f &= ~MachO::MH_APP_EXTENSION_SAFE;
7084 if (f != 0 || flags == 0)
7085 outs() << format(" 0x%08" PRIx32, f);
7087 outs() << format(" 0x%08" PRIx32, magic);
7088 outs() << format(" %7d", cputype);
7089 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7090 outs() << format(" 0x%02" PRIx32,
7091 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7092 outs() << format(" %10u", filetype);
7093 outs() << format(" %5u", ncmds);
7094 outs() << format(" %10u", sizeofcmds);
7095 outs() << format(" 0x%08" PRIx32, flags);
7100 static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
7101 StringRef SegName, uint64_t vmaddr,
7102 uint64_t vmsize, uint64_t fileoff,
7103 uint64_t filesize, uint32_t maxprot,
7104 uint32_t initprot, uint32_t nsects,
7105 uint32_t flags, uint32_t object_size,
7107 uint64_t expected_cmdsize;
7108 if (cmd == MachO::LC_SEGMENT) {
7109 outs() << " cmd LC_SEGMENT\n";
7110 expected_cmdsize = nsects;
7111 expected_cmdsize *= sizeof(struct MachO::section);
7112 expected_cmdsize += sizeof(struct MachO::segment_command);
7114 outs() << " cmd LC_SEGMENT_64\n";
7115 expected_cmdsize = nsects;
7116 expected_cmdsize *= sizeof(struct MachO::section_64);
7117 expected_cmdsize += sizeof(struct MachO::segment_command_64);
7119 outs() << " cmdsize " << cmdsize;
7120 if (cmdsize != expected_cmdsize)
7121 outs() << " Inconsistent size\n";
7124 outs() << " segname " << SegName << "\n";
7125 if (cmd == MachO::LC_SEGMENT_64) {
7126 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
7127 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
7129 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
7130 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
7132 outs() << " fileoff " << fileoff;
7133 if (fileoff > object_size)
7134 outs() << " (past end of file)\n";
7137 outs() << " filesize " << filesize;
7138 if (fileoff + filesize > object_size)
7139 outs() << " (past end of file)\n";
7144 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7145 MachO::VM_PROT_EXECUTE)) != 0)
7146 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
7148 if (maxprot & MachO::VM_PROT_READ)
7149 outs() << " maxprot r";
7151 outs() << " maxprot -";
7152 if (maxprot & MachO::VM_PROT_WRITE)
7156 if (maxprot & MachO::VM_PROT_EXECUTE)
7162 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7163 MachO::VM_PROT_EXECUTE)) != 0)
7164 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
7166 if (initprot & MachO::VM_PROT_READ)
7167 outs() << " initprot r";
7169 outs() << " initprot -";
7170 if (initprot & MachO::VM_PROT_WRITE)
7174 if (initprot & MachO::VM_PROT_EXECUTE)
7180 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
7181 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
7183 outs() << " nsects " << nsects << "\n";
7187 outs() << " (none)\n";
7189 if (flags & MachO::SG_HIGHVM) {
7190 outs() << " HIGHVM";
7191 flags &= ~MachO::SG_HIGHVM;
7193 if (flags & MachO::SG_FVMLIB) {
7194 outs() << " FVMLIB";
7195 flags &= ~MachO::SG_FVMLIB;
7197 if (flags & MachO::SG_NORELOC) {
7198 outs() << " NORELOC";
7199 flags &= ~MachO::SG_NORELOC;
7201 if (flags & MachO::SG_PROTECTED_VERSION_1) {
7202 outs() << " PROTECTED_VERSION_1";
7203 flags &= ~MachO::SG_PROTECTED_VERSION_1;
7206 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
7211 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
7215 static void PrintSection(const char *sectname, const char *segname,
7216 uint64_t addr, uint64_t size, uint32_t offset,
7217 uint32_t align, uint32_t reloff, uint32_t nreloc,
7218 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
7219 uint32_t cmd, const char *sg_segname,
7220 uint32_t filetype, uint32_t object_size,
7222 outs() << "Section\n";
7223 outs() << " sectname " << format("%.16s\n", sectname);
7224 outs() << " segname " << format("%.16s", segname);
7225 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
7226 outs() << " (does not match segment)\n";
7229 if (cmd == MachO::LC_SEGMENT_64) {
7230 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
7231 outs() << " size " << format("0x%016" PRIx64, size);
7233 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
7234 outs() << " size " << format("0x%08" PRIx64, size);
7236 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
7237 outs() << " (past end of file)\n";
7240 outs() << " offset " << offset;
7241 if (offset > object_size)
7242 outs() << " (past end of file)\n";
7245 uint32_t align_shifted = 1 << align;
7246 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
7247 outs() << " reloff " << reloff;
7248 if (reloff > object_size)
7249 outs() << " (past end of file)\n";
7252 outs() << " nreloc " << nreloc;
7253 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
7254 outs() << " (past end of file)\n";
7257 uint32_t section_type = flags & MachO::SECTION_TYPE;
7260 if (section_type == MachO::S_REGULAR)
7261 outs() << " S_REGULAR\n";
7262 else if (section_type == MachO::S_ZEROFILL)
7263 outs() << " S_ZEROFILL\n";
7264 else if (section_type == MachO::S_CSTRING_LITERALS)
7265 outs() << " S_CSTRING_LITERALS\n";
7266 else if (section_type == MachO::S_4BYTE_LITERALS)
7267 outs() << " S_4BYTE_LITERALS\n";
7268 else if (section_type == MachO::S_8BYTE_LITERALS)
7269 outs() << " S_8BYTE_LITERALS\n";
7270 else if (section_type == MachO::S_16BYTE_LITERALS)
7271 outs() << " S_16BYTE_LITERALS\n";
7272 else if (section_type == MachO::S_LITERAL_POINTERS)
7273 outs() << " S_LITERAL_POINTERS\n";
7274 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
7275 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
7276 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
7277 outs() << " S_LAZY_SYMBOL_POINTERS\n";
7278 else if (section_type == MachO::S_SYMBOL_STUBS)
7279 outs() << " S_SYMBOL_STUBS\n";
7280 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
7281 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
7282 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
7283 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
7284 else if (section_type == MachO::S_COALESCED)
7285 outs() << " S_COALESCED\n";
7286 else if (section_type == MachO::S_INTERPOSING)
7287 outs() << " S_INTERPOSING\n";
7288 else if (section_type == MachO::S_DTRACE_DOF)
7289 outs() << " S_DTRACE_DOF\n";
7290 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
7291 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
7292 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
7293 outs() << " S_THREAD_LOCAL_REGULAR\n";
7294 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
7295 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
7296 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
7297 outs() << " S_THREAD_LOCAL_VARIABLES\n";
7298 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7299 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
7300 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
7301 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
7303 outs() << format("0x%08" PRIx32, section_type) << "\n";
7304 outs() << "attributes";
7305 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
7306 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
7307 outs() << " PURE_INSTRUCTIONS";
7308 if (section_attributes & MachO::S_ATTR_NO_TOC)
7309 outs() << " NO_TOC";
7310 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
7311 outs() << " STRIP_STATIC_SYMS";
7312 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
7313 outs() << " NO_DEAD_STRIP";
7314 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
7315 outs() << " LIVE_SUPPORT";
7316 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
7317 outs() << " SELF_MODIFYING_CODE";
7318 if (section_attributes & MachO::S_ATTR_DEBUG)
7320 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
7321 outs() << " SOME_INSTRUCTIONS";
7322 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
7323 outs() << " EXT_RELOC";
7324 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
7325 outs() << " LOC_RELOC";
7326 if (section_attributes == 0)
7327 outs() << " (none)";
7330 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
7331 outs() << " reserved1 " << reserved1;
7332 if (section_type == MachO::S_SYMBOL_STUBS ||
7333 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
7334 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
7335 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
7336 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7337 outs() << " (index into indirect symbol table)\n";
7340 outs() << " reserved2 " << reserved2;
7341 if (section_type == MachO::S_SYMBOL_STUBS)
7342 outs() << " (size of stubs)\n";
7347 static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
7348 uint32_t object_size) {
7349 outs() << " cmd LC_SYMTAB\n";
7350 outs() << " cmdsize " << st.cmdsize;
7351 if (st.cmdsize != sizeof(struct MachO::symtab_command))
7352 outs() << " Incorrect size\n";
7355 outs() << " symoff " << st.symoff;
7356 if (st.symoff > object_size)
7357 outs() << " (past end of file)\n";
7360 outs() << " nsyms " << st.nsyms;
7363 big_size = st.nsyms;
7364 big_size *= sizeof(struct MachO::nlist_64);
7365 big_size += st.symoff;
7366 if (big_size > object_size)
7367 outs() << " (past end of file)\n";
7371 big_size = st.nsyms;
7372 big_size *= sizeof(struct MachO::nlist);
7373 big_size += st.symoff;
7374 if (big_size > object_size)
7375 outs() << " (past end of file)\n";
7379 outs() << " stroff " << st.stroff;
7380 if (st.stroff > object_size)
7381 outs() << " (past end of file)\n";
7384 outs() << " strsize " << st.strsize;
7385 big_size = st.stroff;
7386 big_size += st.strsize;
7387 if (big_size > object_size)
7388 outs() << " (past end of file)\n";
7393 static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
7394 uint32_t nsyms, uint32_t object_size,
7396 outs() << " cmd LC_DYSYMTAB\n";
7397 outs() << " cmdsize " << dyst.cmdsize;
7398 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
7399 outs() << " Incorrect size\n";
7402 outs() << " ilocalsym " << dyst.ilocalsym;
7403 if (dyst.ilocalsym > nsyms)
7404 outs() << " (greater than the number of symbols)\n";
7407 outs() << " nlocalsym " << dyst.nlocalsym;
7409 big_size = dyst.ilocalsym;
7410 big_size += dyst.nlocalsym;
7411 if (big_size > nsyms)
7412 outs() << " (past the end of the symbol table)\n";
7415 outs() << " iextdefsym " << dyst.iextdefsym;
7416 if (dyst.iextdefsym > nsyms)
7417 outs() << " (greater than the number of symbols)\n";
7420 outs() << " nextdefsym " << dyst.nextdefsym;
7421 big_size = dyst.iextdefsym;
7422 big_size += dyst.nextdefsym;
7423 if (big_size > nsyms)
7424 outs() << " (past the end of the symbol table)\n";
7427 outs() << " iundefsym " << dyst.iundefsym;
7428 if (dyst.iundefsym > nsyms)
7429 outs() << " (greater than the number of symbols)\n";
7432 outs() << " nundefsym " << dyst.nundefsym;
7433 big_size = dyst.iundefsym;
7434 big_size += dyst.nundefsym;
7435 if (big_size > nsyms)
7436 outs() << " (past the end of the symbol table)\n";
7439 outs() << " tocoff " << dyst.tocoff;
7440 if (dyst.tocoff > object_size)
7441 outs() << " (past end of file)\n";
7444 outs() << " ntoc " << dyst.ntoc;
7445 big_size = dyst.ntoc;
7446 big_size *= sizeof(struct MachO::dylib_table_of_contents);
7447 big_size += dyst.tocoff;
7448 if (big_size > object_size)
7449 outs() << " (past end of file)\n";
7452 outs() << " modtaboff " << dyst.modtaboff;
7453 if (dyst.modtaboff > object_size)
7454 outs() << " (past end of file)\n";
7457 outs() << " nmodtab " << dyst.nmodtab;
7460 modtabend = dyst.nmodtab;
7461 modtabend *= sizeof(struct MachO::dylib_module_64);
7462 modtabend += dyst.modtaboff;
7464 modtabend = dyst.nmodtab;
7465 modtabend *= sizeof(struct MachO::dylib_module);
7466 modtabend += dyst.modtaboff;
7468 if (modtabend > object_size)
7469 outs() << " (past end of file)\n";
7472 outs() << " extrefsymoff " << dyst.extrefsymoff;
7473 if (dyst.extrefsymoff > object_size)
7474 outs() << " (past end of file)\n";
7477 outs() << " nextrefsyms " << dyst.nextrefsyms;
7478 big_size = dyst.nextrefsyms;
7479 big_size *= sizeof(struct MachO::dylib_reference);
7480 big_size += dyst.extrefsymoff;
7481 if (big_size > object_size)
7482 outs() << " (past end of file)\n";
7485 outs() << " indirectsymoff " << dyst.indirectsymoff;
7486 if (dyst.indirectsymoff > object_size)
7487 outs() << " (past end of file)\n";
7490 outs() << " nindirectsyms " << dyst.nindirectsyms;
7491 big_size = dyst.nindirectsyms;
7492 big_size *= sizeof(uint32_t);
7493 big_size += dyst.indirectsymoff;
7494 if (big_size > object_size)
7495 outs() << " (past end of file)\n";
7498 outs() << " extreloff " << dyst.extreloff;
7499 if (dyst.extreloff > object_size)
7500 outs() << " (past end of file)\n";
7503 outs() << " nextrel " << dyst.nextrel;
7504 big_size = dyst.nextrel;
7505 big_size *= sizeof(struct MachO::relocation_info);
7506 big_size += dyst.extreloff;
7507 if (big_size > object_size)
7508 outs() << " (past end of file)\n";
7511 outs() << " locreloff " << dyst.locreloff;
7512 if (dyst.locreloff > object_size)
7513 outs() << " (past end of file)\n";
7516 outs() << " nlocrel " << dyst.nlocrel;
7517 big_size = dyst.nlocrel;
7518 big_size *= sizeof(struct MachO::relocation_info);
7519 big_size += dyst.locreloff;
7520 if (big_size > object_size)
7521 outs() << " (past end of file)\n";
7526 static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
7527 uint32_t object_size) {
7528 if (dc.cmd == MachO::LC_DYLD_INFO)
7529 outs() << " cmd LC_DYLD_INFO\n";
7531 outs() << " cmd LC_DYLD_INFO_ONLY\n";
7532 outs() << " cmdsize " << dc.cmdsize;
7533 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
7534 outs() << " Incorrect size\n";
7537 outs() << " rebase_off " << dc.rebase_off;
7538 if (dc.rebase_off > object_size)
7539 outs() << " (past end of file)\n";
7542 outs() << " rebase_size " << dc.rebase_size;
7544 big_size = dc.rebase_off;
7545 big_size += dc.rebase_size;
7546 if (big_size > object_size)
7547 outs() << " (past end of file)\n";
7550 outs() << " bind_off " << dc.bind_off;
7551 if (dc.bind_off > object_size)
7552 outs() << " (past end of file)\n";
7555 outs() << " bind_size " << dc.bind_size;
7556 big_size = dc.bind_off;
7557 big_size += dc.bind_size;
7558 if (big_size > object_size)
7559 outs() << " (past end of file)\n";
7562 outs() << " weak_bind_off " << dc.weak_bind_off;
7563 if (dc.weak_bind_off > object_size)
7564 outs() << " (past end of file)\n";
7567 outs() << " weak_bind_size " << dc.weak_bind_size;
7568 big_size = dc.weak_bind_off;
7569 big_size += dc.weak_bind_size;
7570 if (big_size > object_size)
7571 outs() << " (past end of file)\n";
7574 outs() << " lazy_bind_off " << dc.lazy_bind_off;
7575 if (dc.lazy_bind_off > object_size)
7576 outs() << " (past end of file)\n";
7579 outs() << " lazy_bind_size " << dc.lazy_bind_size;
7580 big_size = dc.lazy_bind_off;
7581 big_size += dc.lazy_bind_size;
7582 if (big_size > object_size)
7583 outs() << " (past end of file)\n";
7586 outs() << " export_off " << dc.export_off;
7587 if (dc.export_off > object_size)
7588 outs() << " (past end of file)\n";
7591 outs() << " export_size " << dc.export_size;
7592 big_size = dc.export_off;
7593 big_size += dc.export_size;
7594 if (big_size > object_size)
7595 outs() << " (past end of file)\n";
7600 static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
7602 if (dyld.cmd == MachO::LC_ID_DYLINKER)
7603 outs() << " cmd LC_ID_DYLINKER\n";
7604 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
7605 outs() << " cmd LC_LOAD_DYLINKER\n";
7606 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
7607 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
7609 outs() << " cmd ?(" << dyld.cmd << ")\n";
7610 outs() << " cmdsize " << dyld.cmdsize;
7611 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
7612 outs() << " Incorrect size\n";
7615 if (dyld.name >= dyld.cmdsize)
7616 outs() << " name ?(bad offset " << dyld.name << ")\n";
7618 const char *P = (const char *)(Ptr) + dyld.name;
7619 outs() << " name " << P << " (offset " << dyld.name << ")\n";
7623 static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
7624 outs() << " cmd LC_UUID\n";
7625 outs() << " cmdsize " << uuid.cmdsize;
7626 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
7627 outs() << " Incorrect size\n";
7631 outs() << format("%02" PRIX32, uuid.uuid[0]);
7632 outs() << format("%02" PRIX32, uuid.uuid[1]);
7633 outs() << format("%02" PRIX32, uuid.uuid[2]);
7634 outs() << format("%02" PRIX32, uuid.uuid[3]);
7636 outs() << format("%02" PRIX32, uuid.uuid[4]);
7637 outs() << format("%02" PRIX32, uuid.uuid[5]);
7639 outs() << format("%02" PRIX32, uuid.uuid[6]);
7640 outs() << format("%02" PRIX32, uuid.uuid[7]);
7642 outs() << format("%02" PRIX32, uuid.uuid[8]);
7643 outs() << format("%02" PRIX32, uuid.uuid[9]);
7645 outs() << format("%02" PRIX32, uuid.uuid[10]);
7646 outs() << format("%02" PRIX32, uuid.uuid[11]);
7647 outs() << format("%02" PRIX32, uuid.uuid[12]);
7648 outs() << format("%02" PRIX32, uuid.uuid[13]);
7649 outs() << format("%02" PRIX32, uuid.uuid[14]);
7650 outs() << format("%02" PRIX32, uuid.uuid[15]);
7654 static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
7655 outs() << " cmd LC_RPATH\n";
7656 outs() << " cmdsize " << rpath.cmdsize;
7657 if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
7658 outs() << " Incorrect size\n";
7661 if (rpath.path >= rpath.cmdsize)
7662 outs() << " path ?(bad offset " << rpath.path << ")\n";
7664 const char *P = (const char *)(Ptr) + rpath.path;
7665 outs() << " path " << P << " (offset " << rpath.path << ")\n";
7669 static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
7670 if (vd.cmd == MachO::LC_VERSION_MIN_MACOSX)
7671 outs() << " cmd LC_VERSION_MIN_MACOSX\n";
7672 else if (vd.cmd == MachO::LC_VERSION_MIN_IPHONEOS)
7673 outs() << " cmd LC_VERSION_MIN_IPHONEOS\n";
7675 outs() << " cmd " << vd.cmd << " (?)\n";
7676 outs() << " cmdsize " << vd.cmdsize;
7677 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
7678 outs() << " Incorrect size\n";
7681 outs() << " version " << ((vd.version >> 16) & 0xffff) << "."
7682 << ((vd.version >> 8) & 0xff);
7683 if ((vd.version & 0xff) != 0)
7684 outs() << "." << (vd.version & 0xff);
7687 outs() << " sdk n/a";
7689 outs() << " sdk " << ((vd.sdk >> 16) & 0xffff) << "."
7690 << ((vd.sdk >> 8) & 0xff);
7692 if ((vd.sdk & 0xff) != 0)
7693 outs() << "." << (vd.sdk & 0xff);
7697 static void PrintSourceVersionCommand(MachO::source_version_command sd) {
7698 outs() << " cmd LC_SOURCE_VERSION\n";
7699 outs() << " cmdsize " << sd.cmdsize;
7700 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
7701 outs() << " Incorrect size\n";
7704 uint64_t a = (sd.version >> 40) & 0xffffff;
7705 uint64_t b = (sd.version >> 30) & 0x3ff;
7706 uint64_t c = (sd.version >> 20) & 0x3ff;
7707 uint64_t d = (sd.version >> 10) & 0x3ff;
7708 uint64_t e = sd.version & 0x3ff;
7709 outs() << " version " << a << "." << b;
7711 outs() << "." << c << "." << d << "." << e;
7713 outs() << "." << c << "." << d;
7719 static void PrintEntryPointCommand(MachO::entry_point_command ep) {
7720 outs() << " cmd LC_MAIN\n";
7721 outs() << " cmdsize " << ep.cmdsize;
7722 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
7723 outs() << " Incorrect size\n";
7726 outs() << " entryoff " << ep.entryoff << "\n";
7727 outs() << " stacksize " << ep.stacksize << "\n";
7730 static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
7731 uint32_t object_size) {
7732 outs() << " cmd LC_ENCRYPTION_INFO\n";
7733 outs() << " cmdsize " << ec.cmdsize;
7734 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
7735 outs() << " Incorrect size\n";
7738 outs() << " cryptoff " << ec.cryptoff;
7739 if (ec.cryptoff > object_size)
7740 outs() << " (past end of file)\n";
7743 outs() << " cryptsize " << ec.cryptsize;
7744 if (ec.cryptsize > object_size)
7745 outs() << " (past end of file)\n";
7748 outs() << " cryptid " << ec.cryptid << "\n";
7751 static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
7752 uint32_t object_size) {
7753 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
7754 outs() << " cmdsize " << ec.cmdsize;
7755 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
7756 outs() << " Incorrect size\n";
7759 outs() << " cryptoff " << ec.cryptoff;
7760 if (ec.cryptoff > object_size)
7761 outs() << " (past end of file)\n";
7764 outs() << " cryptsize " << ec.cryptsize;
7765 if (ec.cryptsize > object_size)
7766 outs() << " (past end of file)\n";
7769 outs() << " cryptid " << ec.cryptid << "\n";
7770 outs() << " pad " << ec.pad << "\n";
7773 static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
7775 outs() << " cmd LC_LINKER_OPTION\n";
7776 outs() << " cmdsize " << lo.cmdsize;
7777 if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
7778 outs() << " Incorrect size\n";
7781 outs() << " count " << lo.count << "\n";
7782 const char *string = Ptr + sizeof(struct MachO::linker_option_command);
7783 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
7786 while (*string == '\0' && left > 0) {
7792 outs() << " string #" << i << " " << format("%.*s\n", left, string);
7793 uint32_t NullPos = StringRef(string, left).find('\0');
7794 uint32_t len = std::min(NullPos, left) + 1;
7800 outs() << " count " << lo.count << " does not match number of strings "
7804 static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
7806 outs() << " cmd LC_SUB_FRAMEWORK\n";
7807 outs() << " cmdsize " << sub.cmdsize;
7808 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
7809 outs() << " Incorrect size\n";
7812 if (sub.umbrella < sub.cmdsize) {
7813 const char *P = Ptr + sub.umbrella;
7814 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
7816 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
7820 static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
7822 outs() << " cmd LC_SUB_UMBRELLA\n";
7823 outs() << " cmdsize " << sub.cmdsize;
7824 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
7825 outs() << " Incorrect size\n";
7828 if (sub.sub_umbrella < sub.cmdsize) {
7829 const char *P = Ptr + sub.sub_umbrella;
7830 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
7832 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
7836 static void PrintSubLibraryCommand(MachO::sub_library_command sub,
7838 outs() << " cmd LC_SUB_LIBRARY\n";
7839 outs() << " cmdsize " << sub.cmdsize;
7840 if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
7841 outs() << " Incorrect size\n";
7844 if (sub.sub_library < sub.cmdsize) {
7845 const char *P = Ptr + sub.sub_library;
7846 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
7848 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
7852 static void PrintSubClientCommand(MachO::sub_client_command sub,
7854 outs() << " cmd LC_SUB_CLIENT\n";
7855 outs() << " cmdsize " << sub.cmdsize;
7856 if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
7857 outs() << " Incorrect size\n";
7860 if (sub.client < sub.cmdsize) {
7861 const char *P = Ptr + sub.client;
7862 outs() << " client " << P << " (offset " << sub.client << ")\n";
7864 outs() << " client ?(bad offset " << sub.client << ")\n";
7868 static void PrintRoutinesCommand(MachO::routines_command r) {
7869 outs() << " cmd LC_ROUTINES\n";
7870 outs() << " cmdsize " << r.cmdsize;
7871 if (r.cmdsize != sizeof(struct MachO::routines_command))
7872 outs() << " Incorrect size\n";
7875 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
7876 outs() << " init_module " << r.init_module << "\n";
7877 outs() << " reserved1 " << r.reserved1 << "\n";
7878 outs() << " reserved2 " << r.reserved2 << "\n";
7879 outs() << " reserved3 " << r.reserved3 << "\n";
7880 outs() << " reserved4 " << r.reserved4 << "\n";
7881 outs() << " reserved5 " << r.reserved5 << "\n";
7882 outs() << " reserved6 " << r.reserved6 << "\n";
7885 static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
7886 outs() << " cmd LC_ROUTINES_64\n";
7887 outs() << " cmdsize " << r.cmdsize;
7888 if (r.cmdsize != sizeof(struct MachO::routines_command_64))
7889 outs() << " Incorrect size\n";
7892 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
7893 outs() << " init_module " << r.init_module << "\n";
7894 outs() << " reserved1 " << r.reserved1 << "\n";
7895 outs() << " reserved2 " << r.reserved2 << "\n";
7896 outs() << " reserved3 " << r.reserved3 << "\n";
7897 outs() << " reserved4 " << r.reserved4 << "\n";
7898 outs() << " reserved5 " << r.reserved5 << "\n";
7899 outs() << " reserved6 " << r.reserved6 << "\n";
7902 static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
7903 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
7904 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
7905 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
7906 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
7907 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
7908 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
7909 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
7910 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
7911 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
7912 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
7913 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
7914 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
7915 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
7916 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
7917 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
7918 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
7919 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
7920 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
7921 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
7922 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
7923 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
7926 static void Print_mmst_reg(MachO::mmst_reg_t &r) {
7928 outs() << "\t mmst_reg ";
7929 for (f = 0; f < 10; f++)
7930 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
7932 outs() << "\t mmst_rsrv ";
7933 for (f = 0; f < 6; f++)
7934 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
7938 static void Print_xmm_reg(MachO::xmm_reg_t &r) {
7940 outs() << "\t xmm_reg ";
7941 for (f = 0; f < 16; f++)
7942 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
7946 static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
7947 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
7948 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
7949 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
7950 outs() << " denorm " << fpu.fpu_fcw.denorm;
7951 outs() << " zdiv " << fpu.fpu_fcw.zdiv;
7952 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
7953 outs() << " undfl " << fpu.fpu_fcw.undfl;
7954 outs() << " precis " << fpu.fpu_fcw.precis << "\n";
7955 outs() << "\t\t pc ";
7956 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
7957 outs() << "FP_PREC_24B ";
7958 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
7959 outs() << "FP_PREC_53B ";
7960 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
7961 outs() << "FP_PREC_64B ";
7963 outs() << fpu.fpu_fcw.pc << " ";
7965 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
7966 outs() << "FP_RND_NEAR ";
7967 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
7968 outs() << "FP_RND_DOWN ";
7969 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
7970 outs() << "FP_RND_UP ";
7971 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
7972 outs() << "FP_CHOP ";
7974 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
7975 outs() << " denorm " << fpu.fpu_fsw.denorm;
7976 outs() << " zdiv " << fpu.fpu_fsw.zdiv;
7977 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
7978 outs() << " undfl " << fpu.fpu_fsw.undfl;
7979 outs() << " precis " << fpu.fpu_fsw.precis;
7980 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
7981 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
7982 outs() << " c0 " << fpu.fpu_fsw.c0;
7983 outs() << " c1 " << fpu.fpu_fsw.c1;
7984 outs() << " c2 " << fpu.fpu_fsw.c2;
7985 outs() << " tos " << fpu.fpu_fsw.tos;
7986 outs() << " c3 " << fpu.fpu_fsw.c3;
7987 outs() << " busy " << fpu.fpu_fsw.busy << "\n";
7988 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
7989 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
7990 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
7991 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
7992 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
7993 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
7994 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
7995 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
7996 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
7997 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
7998 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
8000 outs() << "\t fpu_stmm0:\n";
8001 Print_mmst_reg(fpu.fpu_stmm0);
8002 outs() << "\t fpu_stmm1:\n";
8003 Print_mmst_reg(fpu.fpu_stmm1);
8004 outs() << "\t fpu_stmm2:\n";
8005 Print_mmst_reg(fpu.fpu_stmm2);
8006 outs() << "\t fpu_stmm3:\n";
8007 Print_mmst_reg(fpu.fpu_stmm3);
8008 outs() << "\t fpu_stmm4:\n";
8009 Print_mmst_reg(fpu.fpu_stmm4);
8010 outs() << "\t fpu_stmm5:\n";
8011 Print_mmst_reg(fpu.fpu_stmm5);
8012 outs() << "\t fpu_stmm6:\n";
8013 Print_mmst_reg(fpu.fpu_stmm6);
8014 outs() << "\t fpu_stmm7:\n";
8015 Print_mmst_reg(fpu.fpu_stmm7);
8016 outs() << "\t fpu_xmm0:\n";
8017 Print_xmm_reg(fpu.fpu_xmm0);
8018 outs() << "\t fpu_xmm1:\n";
8019 Print_xmm_reg(fpu.fpu_xmm1);
8020 outs() << "\t fpu_xmm2:\n";
8021 Print_xmm_reg(fpu.fpu_xmm2);
8022 outs() << "\t fpu_xmm3:\n";
8023 Print_xmm_reg(fpu.fpu_xmm3);
8024 outs() << "\t fpu_xmm4:\n";
8025 Print_xmm_reg(fpu.fpu_xmm4);
8026 outs() << "\t fpu_xmm5:\n";
8027 Print_xmm_reg(fpu.fpu_xmm5);
8028 outs() << "\t fpu_xmm6:\n";
8029 Print_xmm_reg(fpu.fpu_xmm6);
8030 outs() << "\t fpu_xmm7:\n";
8031 Print_xmm_reg(fpu.fpu_xmm7);
8032 outs() << "\t fpu_xmm8:\n";
8033 Print_xmm_reg(fpu.fpu_xmm8);
8034 outs() << "\t fpu_xmm9:\n";
8035 Print_xmm_reg(fpu.fpu_xmm9);
8036 outs() << "\t fpu_xmm10:\n";
8037 Print_xmm_reg(fpu.fpu_xmm10);
8038 outs() << "\t fpu_xmm11:\n";
8039 Print_xmm_reg(fpu.fpu_xmm11);
8040 outs() << "\t fpu_xmm12:\n";
8041 Print_xmm_reg(fpu.fpu_xmm12);
8042 outs() << "\t fpu_xmm13:\n";
8043 Print_xmm_reg(fpu.fpu_xmm13);
8044 outs() << "\t fpu_xmm14:\n";
8045 Print_xmm_reg(fpu.fpu_xmm14);
8046 outs() << "\t fpu_xmm15:\n";
8047 Print_xmm_reg(fpu.fpu_xmm15);
8048 outs() << "\t fpu_rsrv4:\n";
8049 for (uint32_t f = 0; f < 6; f++) {
8051 for (uint32_t g = 0; g < 16; g++)
8052 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
8055 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
8059 static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
8060 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
8061 outs() << " err " << format("0x%08" PRIx32, exc64.err);
8062 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
8065 static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
8066 bool isLittleEndian, uint32_t cputype) {
8067 if (t.cmd == MachO::LC_THREAD)
8068 outs() << " cmd LC_THREAD\n";
8069 else if (t.cmd == MachO::LC_UNIXTHREAD)
8070 outs() << " cmd LC_UNIXTHREAD\n";
8072 outs() << " cmd " << t.cmd << " (unknown)\n";
8073 outs() << " cmdsize " << t.cmdsize;
8074 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
8075 outs() << " Incorrect size\n";
8079 const char *begin = Ptr + sizeof(struct MachO::thread_command);
8080 const char *end = Ptr + t.cmdsize;
8081 uint32_t flavor, count, left;
8082 if (cputype == MachO::CPU_TYPE_X86_64) {
8083 while (begin < end) {
8084 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8085 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8086 begin += sizeof(uint32_t);
8091 if (isLittleEndian != sys::IsLittleEndianHost)
8092 sys::swapByteOrder(flavor);
8093 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8094 memcpy((char *)&count, begin, sizeof(uint32_t));
8095 begin += sizeof(uint32_t);
8100 if (isLittleEndian != sys::IsLittleEndianHost)
8101 sys::swapByteOrder(count);
8102 if (flavor == MachO::x86_THREAD_STATE64) {
8103 outs() << " flavor x86_THREAD_STATE64\n";
8104 if (count == MachO::x86_THREAD_STATE64_COUNT)
8105 outs() << " count x86_THREAD_STATE64_COUNT\n";
8107 outs() << " count " << count
8108 << " (not x86_THREAD_STATE64_COUNT)\n";
8109 MachO::x86_thread_state64_t cpu64;
8111 if (left >= sizeof(MachO::x86_thread_state64_t)) {
8112 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
8113 begin += sizeof(MachO::x86_thread_state64_t);
8115 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
8116 memcpy(&cpu64, begin, left);
8119 if (isLittleEndian != sys::IsLittleEndianHost)
8121 Print_x86_thread_state64_t(cpu64);
8122 } else if (flavor == MachO::x86_THREAD_STATE) {
8123 outs() << " flavor x86_THREAD_STATE\n";
8124 if (count == MachO::x86_THREAD_STATE_COUNT)
8125 outs() << " count x86_THREAD_STATE_COUNT\n";
8127 outs() << " count " << count
8128 << " (not x86_THREAD_STATE_COUNT)\n";
8129 struct MachO::x86_thread_state_t ts;
8131 if (left >= sizeof(MachO::x86_thread_state_t)) {
8132 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
8133 begin += sizeof(MachO::x86_thread_state_t);
8135 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
8136 memcpy(&ts, begin, left);
8139 if (isLittleEndian != sys::IsLittleEndianHost)
8141 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
8142 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
8143 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
8144 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
8146 outs() << "tsh.count " << ts.tsh.count
8147 << " (not x86_THREAD_STATE64_COUNT\n";
8148 Print_x86_thread_state64_t(ts.uts.ts64);
8150 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
8151 << ts.tsh.count << "\n";
8153 } else if (flavor == MachO::x86_FLOAT_STATE) {
8154 outs() << " flavor x86_FLOAT_STATE\n";
8155 if (count == MachO::x86_FLOAT_STATE_COUNT)
8156 outs() << " count x86_FLOAT_STATE_COUNT\n";
8158 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
8159 struct MachO::x86_float_state_t fs;
8161 if (left >= sizeof(MachO::x86_float_state_t)) {
8162 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
8163 begin += sizeof(MachO::x86_float_state_t);
8165 memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
8166 memcpy(&fs, begin, left);
8169 if (isLittleEndian != sys::IsLittleEndianHost)
8171 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
8172 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
8173 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
8174 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
8176 outs() << "fsh.count " << fs.fsh.count
8177 << " (not x86_FLOAT_STATE64_COUNT\n";
8178 Print_x86_float_state_t(fs.ufs.fs64);
8180 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
8181 << fs.fsh.count << "\n";
8183 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
8184 outs() << " flavor x86_EXCEPTION_STATE\n";
8185 if (count == MachO::x86_EXCEPTION_STATE_COUNT)
8186 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
8188 outs() << " count " << count
8189 << " (not x86_EXCEPTION_STATE_COUNT)\n";
8190 struct MachO::x86_exception_state_t es;
8192 if (left >= sizeof(MachO::x86_exception_state_t)) {
8193 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
8194 begin += sizeof(MachO::x86_exception_state_t);
8196 memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
8197 memcpy(&es, begin, left);
8200 if (isLittleEndian != sys::IsLittleEndianHost)
8202 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
8203 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
8204 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
8205 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
8207 outs() << "\t esh.count " << es.esh.count
8208 << " (not x86_EXCEPTION_STATE64_COUNT\n";
8209 Print_x86_exception_state_t(es.ues.es64);
8211 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
8212 << es.esh.count << "\n";
8215 outs() << " flavor " << flavor << " (unknown)\n";
8216 outs() << " count " << count << "\n";
8217 outs() << " state (unknown)\n";
8218 begin += count * sizeof(uint32_t);
8222 while (begin < end) {
8223 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8224 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8225 begin += sizeof(uint32_t);
8230 if (isLittleEndian != sys::IsLittleEndianHost)
8231 sys::swapByteOrder(flavor);
8232 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8233 memcpy((char *)&count, begin, sizeof(uint32_t));
8234 begin += sizeof(uint32_t);
8239 if (isLittleEndian != sys::IsLittleEndianHost)
8240 sys::swapByteOrder(count);
8241 outs() << " flavor " << flavor << "\n";
8242 outs() << " count " << count << "\n";
8243 outs() << " state (Unknown cputype/cpusubtype)\n";
8244 begin += count * sizeof(uint32_t);
8249 static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
8250 if (dl.cmd == MachO::LC_ID_DYLIB)
8251 outs() << " cmd LC_ID_DYLIB\n";
8252 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
8253 outs() << " cmd LC_LOAD_DYLIB\n";
8254 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
8255 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
8256 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
8257 outs() << " cmd LC_REEXPORT_DYLIB\n";
8258 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
8259 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
8260 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
8261 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
8263 outs() << " cmd " << dl.cmd << " (unknown)\n";
8264 outs() << " cmdsize " << dl.cmdsize;
8265 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
8266 outs() << " Incorrect size\n";
8269 if (dl.dylib.name < dl.cmdsize) {
8270 const char *P = (const char *)(Ptr) + dl.dylib.name;
8271 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
8273 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
8275 outs() << " time stamp " << dl.dylib.timestamp << " ";
8276 time_t t = dl.dylib.timestamp;
8277 outs() << ctime(&t);
8278 outs() << " current version ";
8279 if (dl.dylib.current_version == 0xffffffff)
8282 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
8283 << ((dl.dylib.current_version >> 8) & 0xff) << "."
8284 << (dl.dylib.current_version & 0xff) << "\n";
8285 outs() << "compatibility version ";
8286 if (dl.dylib.compatibility_version == 0xffffffff)
8289 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
8290 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
8291 << (dl.dylib.compatibility_version & 0xff) << "\n";
8294 static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
8295 uint32_t object_size) {
8296 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
8297 outs() << " cmd LC_FUNCTION_STARTS\n";
8298 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
8299 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
8300 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
8301 outs() << " cmd LC_FUNCTION_STARTS\n";
8302 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
8303 outs() << " cmd LC_DATA_IN_CODE\n";
8304 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
8305 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
8306 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
8307 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
8309 outs() << " cmd " << ld.cmd << " (?)\n";
8310 outs() << " cmdsize " << ld.cmdsize;
8311 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
8312 outs() << " Incorrect size\n";
8315 outs() << " dataoff " << ld.dataoff;
8316 if (ld.dataoff > object_size)
8317 outs() << " (past end of file)\n";
8320 outs() << " datasize " << ld.datasize;
8321 uint64_t big_size = ld.dataoff;
8322 big_size += ld.datasize;
8323 if (big_size > object_size)
8324 outs() << " (past end of file)\n";
8329 static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
8330 uint32_t cputype, bool verbose) {
8331 StringRef Buf = Obj->getData();
8333 for (const auto &Command : Obj->load_commands()) {
8334 outs() << "Load command " << Index++ << "\n";
8335 if (Command.C.cmd == MachO::LC_SEGMENT) {
8336 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
8337 const char *sg_segname = SLC.segname;
8338 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
8339 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
8340 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
8342 for (unsigned j = 0; j < SLC.nsects; j++) {
8343 MachO::section S = Obj->getSection(Command, j);
8344 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
8345 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
8346 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
8348 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
8349 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
8350 const char *sg_segname = SLC_64.segname;
8351 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
8352 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
8353 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
8354 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
8355 for (unsigned j = 0; j < SLC_64.nsects; j++) {
8356 MachO::section_64 S_64 = Obj->getSection64(Command, j);
8357 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
8358 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
8359 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
8360 sg_segname, filetype, Buf.size(), verbose);
8362 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
8363 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
8364 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
8365 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
8366 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
8367 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
8368 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
8370 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
8371 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
8372 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
8373 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
8374 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
8375 Command.C.cmd == MachO::LC_ID_DYLINKER ||
8376 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
8377 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
8378 PrintDyldLoadCommand(Dyld, Command.Ptr);
8379 } else if (Command.C.cmd == MachO::LC_UUID) {
8380 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
8381 PrintUuidLoadCommand(Uuid);
8382 } else if (Command.C.cmd == MachO::LC_RPATH) {
8383 MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
8384 PrintRpathLoadCommand(Rpath, Command.Ptr);
8385 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
8386 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS) {
8387 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
8388 PrintVersionMinLoadCommand(Vd);
8389 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
8390 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
8391 PrintSourceVersionCommand(Sd);
8392 } else if (Command.C.cmd == MachO::LC_MAIN) {
8393 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
8394 PrintEntryPointCommand(Ep);
8395 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
8396 MachO::encryption_info_command Ei =
8397 Obj->getEncryptionInfoCommand(Command);
8398 PrintEncryptionInfoCommand(Ei, Buf.size());
8399 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
8400 MachO::encryption_info_command_64 Ei =
8401 Obj->getEncryptionInfoCommand64(Command);
8402 PrintEncryptionInfoCommand64(Ei, Buf.size());
8403 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
8404 MachO::linker_option_command Lo =
8405 Obj->getLinkerOptionLoadCommand(Command);
8406 PrintLinkerOptionCommand(Lo, Command.Ptr);
8407 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
8408 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
8409 PrintSubFrameworkCommand(Sf, Command.Ptr);
8410 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
8411 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
8412 PrintSubUmbrellaCommand(Sf, Command.Ptr);
8413 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
8414 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
8415 PrintSubLibraryCommand(Sl, Command.Ptr);
8416 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
8417 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
8418 PrintSubClientCommand(Sc, Command.Ptr);
8419 } else if (Command.C.cmd == MachO::LC_ROUTINES) {
8420 MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
8421 PrintRoutinesCommand(Rc);
8422 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
8423 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
8424 PrintRoutinesCommand64(Rc);
8425 } else if (Command.C.cmd == MachO::LC_THREAD ||
8426 Command.C.cmd == MachO::LC_UNIXTHREAD) {
8427 MachO::thread_command Tc = Obj->getThreadCommand(Command);
8428 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
8429 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
8430 Command.C.cmd == MachO::LC_ID_DYLIB ||
8431 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
8432 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
8433 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
8434 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
8435 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
8436 PrintDylibCommand(Dl, Command.Ptr);
8437 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
8438 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
8439 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
8440 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
8441 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
8442 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
8443 MachO::linkedit_data_command Ld =
8444 Obj->getLinkeditDataLoadCommand(Command);
8445 PrintLinkEditDataCommand(Ld, Buf.size());
8447 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
8449 outs() << " cmdsize " << Command.C.cmdsize << "\n";
8450 // TODO: get and print the raw bytes of the load command.
8452 // TODO: print all the other kinds of load commands.
8456 static void getAndPrintMachHeader(const MachOObjectFile *Obj,
8457 uint32_t &filetype, uint32_t &cputype,
8459 if (Obj->is64Bit()) {
8460 MachO::mach_header_64 H_64;
8461 H_64 = Obj->getHeader64();
8462 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
8463 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
8464 filetype = H_64.filetype;
8465 cputype = H_64.cputype;
8467 MachO::mach_header H;
8468 H = Obj->getHeader();
8469 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
8470 H.sizeofcmds, H.flags, verbose);
8471 filetype = H.filetype;
8472 cputype = H.cputype;
8476 void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
8477 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
8478 uint32_t filetype = 0;
8479 uint32_t cputype = 0;
8480 getAndPrintMachHeader(file, filetype, cputype, !NonVerbose);
8481 PrintLoadCommands(file, filetype, cputype, !NonVerbose);
8484 //===----------------------------------------------------------------------===//
8485 // export trie dumping
8486 //===----------------------------------------------------------------------===//
8488 void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
8489 for (const llvm::object::ExportEntry &Entry : Obj->exports()) {
8490 uint64_t Flags = Entry.flags();
8491 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
8492 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
8493 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8494 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
8495 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
8496 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
8497 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
8499 outs() << "[re-export] ";
8501 outs() << format("0x%08llX ",
8502 Entry.address()); // FIXME:add in base address
8503 outs() << Entry.name();
8504 if (WeakDef || ThreadLocal || Resolver || Abs) {
8505 bool NeedsComma = false;
8508 outs() << "weak_def";
8514 outs() << "per-thread";
8520 outs() << "absolute";
8526 outs() << format("resolver=0x%08llX", Entry.other());
8532 StringRef DylibName = "unknown";
8533 int Ordinal = Entry.other() - 1;
8534 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
8535 if (Entry.otherName().empty())
8536 outs() << " (from " << DylibName << ")";
8538 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
8544 //===----------------------------------------------------------------------===//
8545 // rebase table dumping
8546 //===----------------------------------------------------------------------===//
8551 SegInfo(const object::MachOObjectFile *Obj);
8553 StringRef segmentName(uint32_t SegIndex);
8554 StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset);
8555 uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
8558 struct SectionInfo {
8561 StringRef SectionName;
8562 StringRef SegmentName;
8563 uint64_t OffsetInSegment;
8564 uint64_t SegmentStartAddress;
8565 uint32_t SegmentIndex;
8567 const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset);
8568 SmallVector<SectionInfo, 32> Sections;
8572 SegInfo::SegInfo(const object::MachOObjectFile *Obj) {
8573 // Build table of sections so segIndex/offset pairs can be translated.
8574 uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
8575 StringRef CurSegName;
8576 uint64_t CurSegAddress;
8577 for (const SectionRef &Section : Obj->sections()) {
8579 if (error(Section.getName(Info.SectionName)))
8581 Info.Address = Section.getAddress();
8582 Info.Size = Section.getSize();
8584 Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
8585 if (!Info.SegmentName.equals(CurSegName)) {
8587 CurSegName = Info.SegmentName;
8588 CurSegAddress = Info.Address;
8590 Info.SegmentIndex = CurSegIndex - 1;
8591 Info.OffsetInSegment = Info.Address - CurSegAddress;
8592 Info.SegmentStartAddress = CurSegAddress;
8593 Sections.push_back(Info);
8597 StringRef SegInfo::segmentName(uint32_t SegIndex) {
8598 for (const SectionInfo &SI : Sections) {
8599 if (SI.SegmentIndex == SegIndex)
8600 return SI.SegmentName;
8602 llvm_unreachable("invalid segIndex");
8605 const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex,
8606 uint64_t OffsetInSeg) {
8607 for (const SectionInfo &SI : Sections) {
8608 if (SI.SegmentIndex != SegIndex)
8610 if (SI.OffsetInSegment > OffsetInSeg)
8612 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
8616 llvm_unreachable("segIndex and offset not in any section");
8619 StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) {
8620 return findSection(SegIndex, OffsetInSeg).SectionName;
8623 uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) {
8624 const SectionInfo &SI = findSection(SegIndex, OffsetInSeg);
8625 return SI.SegmentStartAddress + OffsetInSeg;
8628 void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) {
8629 // Build table of sections so names can used in final output.
8630 SegInfo sectionTable(Obj);
8632 outs() << "segment section address type\n";
8633 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) {
8634 uint32_t SegIndex = Entry.segmentIndex();
8635 uint64_t OffsetInSeg = Entry.segmentOffset();
8636 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8637 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8638 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8640 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
8641 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
8642 SegmentName.str().c_str(), SectionName.str().c_str(),
8643 Address, Entry.typeName().str().c_str());
8647 static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
8648 StringRef DylibName;
8650 case MachO::BIND_SPECIAL_DYLIB_SELF:
8651 return "this-image";
8652 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
8653 return "main-executable";
8654 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
8655 return "flat-namespace";
8658 std::error_code EC =
8659 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
8661 return "<<bad library ordinal>>";
8665 return "<<unknown special ordinal>>";
8668 //===----------------------------------------------------------------------===//
8669 // bind table dumping
8670 //===----------------------------------------------------------------------===//
8672 void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) {
8673 // Build table of sections so names can used in final output.
8674 SegInfo sectionTable(Obj);
8676 outs() << "segment section address type "
8677 "addend dylib symbol\n";
8678 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) {
8679 uint32_t SegIndex = Entry.segmentIndex();
8680 uint64_t OffsetInSeg = Entry.segmentOffset();
8681 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8682 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8683 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8685 // Table lines look like:
8686 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
8688 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
8689 Attr = " (weak_import)";
8690 outs() << left_justify(SegmentName, 8) << " "
8691 << left_justify(SectionName, 18) << " "
8692 << format_hex(Address, 10, true) << " "
8693 << left_justify(Entry.typeName(), 8) << " "
8694 << format_decimal(Entry.addend(), 8) << " "
8695 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
8696 << Entry.symbolName() << Attr << "\n";
8700 //===----------------------------------------------------------------------===//
8701 // lazy bind table dumping
8702 //===----------------------------------------------------------------------===//
8704 void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) {
8705 // Build table of sections so names can used in final output.
8706 SegInfo sectionTable(Obj);
8708 outs() << "segment section address "
8710 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) {
8711 uint32_t SegIndex = Entry.segmentIndex();
8712 uint64_t OffsetInSeg = Entry.segmentOffset();
8713 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8714 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8715 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8717 // Table lines look like:
8718 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
8719 outs() << left_justify(SegmentName, 8) << " "
8720 << left_justify(SectionName, 18) << " "
8721 << format_hex(Address, 10, true) << " "
8722 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
8723 << Entry.symbolName() << "\n";
8727 //===----------------------------------------------------------------------===//
8728 // weak bind table dumping
8729 //===----------------------------------------------------------------------===//
8731 void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) {
8732 // Build table of sections so names can used in final output.
8733 SegInfo sectionTable(Obj);
8735 outs() << "segment section address "
8736 "type addend symbol\n";
8737 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) {
8738 // Strong symbols don't have a location to update.
8739 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
8740 outs() << " strong "
8741 << Entry.symbolName() << "\n";
8744 uint32_t SegIndex = Entry.segmentIndex();
8745 uint64_t OffsetInSeg = Entry.segmentOffset();
8746 StringRef SegmentName = sectionTable.segmentName(SegIndex);
8747 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
8748 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8750 // Table lines look like:
8751 // __DATA __data 0x00001000 pointer 0 _foo
8752 outs() << left_justify(SegmentName, 8) << " "
8753 << left_justify(SectionName, 18) << " "
8754 << format_hex(Address, 10, true) << " "
8755 << left_justify(Entry.typeName(), 8) << " "
8756 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
8761 // get_dyld_bind_info_symbolname() is used for disassembly and passed an
8762 // address, ReferenceValue, in the Mach-O file and looks in the dyld bind
8763 // information for that address. If the address is found its binding symbol
8764 // name is returned. If not nullptr is returned.
8765 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
8766 struct DisassembleInfo *info) {
8767 if (info->bindtable == nullptr) {
8768 info->bindtable = new (BindTable);
8769 SegInfo sectionTable(info->O);
8770 for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable()) {
8771 uint32_t SegIndex = Entry.segmentIndex();
8772 uint64_t OffsetInSeg = Entry.segmentOffset();
8773 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
8774 const char *SymbolName = nullptr;
8775 StringRef name = Entry.symbolName();
8777 SymbolName = name.data();
8778 info->bindtable->push_back(std::make_pair(Address, SymbolName));
8781 for (bind_table_iterator BI = info->bindtable->begin(),
8782 BE = info->bindtable->end();
8784 uint64_t Address = BI->first;
8785 if (ReferenceValue == Address) {
8786 const char *SymbolName = BI->second;