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/BinaryFormat/MachO.h"
20 #include "llvm/Config/config.h"
21 #include "llvm/DebugInfo/DIContext.h"
22 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
23 #include "llvm/Demangle/Demangle.h"
24 #include "llvm/MC/MCAsmInfo.h"
25 #include "llvm/MC/MCContext.h"
26 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
27 #include "llvm/MC/MCInst.h"
28 #include "llvm/MC/MCInstPrinter.h"
29 #include "llvm/MC/MCInstrDesc.h"
30 #include "llvm/MC/MCInstrInfo.h"
31 #include "llvm/MC/MCRegisterInfo.h"
32 #include "llvm/MC/MCSubtargetInfo.h"
33 #include "llvm/Object/MachO.h"
34 #include "llvm/Object/MachOUniversal.h"
35 #include "llvm/Support/Casting.h"
36 #include "llvm/Support/CommandLine.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/Endian.h"
39 #include "llvm/Support/Format.h"
40 #include "llvm/Support/FormattedStream.h"
41 #include "llvm/Support/GraphWriter.h"
42 #include "llvm/Support/LEB128.h"
43 #include "llvm/Support/MemoryBuffer.h"
44 #include "llvm/Support/TargetRegistry.h"
45 #include "llvm/Support/TargetSelect.h"
46 #include "llvm/Support/ToolOutputFile.h"
47 #include "llvm/Support/raw_ostream.h"
50 #include <system_error>
59 using namespace object;
63 cl::desc("Print line information from debug info if available"));
65 static cl::opt<std::string> DSYMFile("dsym",
66 cl::desc("Use .dSYM file for debug info"));
68 static cl::opt<bool> FullLeadingAddr("full-leading-addr",
69 cl::desc("Print full leading address"));
71 static cl::opt<bool> NoLeadingHeaders("no-leading-headers",
72 cl::desc("Print no leading headers"));
74 cl::opt<bool> llvm::UniversalHeaders("universal-headers",
75 cl::desc("Print Mach-O universal headers "
76 "(requires -macho)"));
79 llvm::ArchiveHeaders("archive-headers",
80 cl::desc("Print archive headers for Mach-O archives "
81 "(requires -macho)"));
84 ArchiveMemberOffsets("archive-member-offsets",
85 cl::desc("Print the offset to each archive member for "
86 "Mach-O archives (requires -macho and "
87 "-archive-headers)"));
90 llvm::IndirectSymbols("indirect-symbols",
91 cl::desc("Print indirect symbol table for Mach-O "
92 "objects (requires -macho)"));
95 llvm::DataInCode("data-in-code",
96 cl::desc("Print the data in code table for Mach-O objects "
97 "(requires -macho)"));
100 llvm::LinkOptHints("link-opt-hints",
101 cl::desc("Print the linker optimization hints for "
102 "Mach-O objects (requires -macho)"));
105 llvm::InfoPlist("info-plist",
106 cl::desc("Print the info plist section as strings for "
107 "Mach-O objects (requires -macho)"));
110 llvm::DylibsUsed("dylibs-used",
111 cl::desc("Print the shared libraries used for linked "
112 "Mach-O files (requires -macho)"));
115 llvm::DylibId("dylib-id",
116 cl::desc("Print the shared library's id for the dylib Mach-O "
117 "file (requires -macho)"));
120 llvm::NonVerbose("non-verbose",
121 cl::desc("Print the info for Mach-O objects in "
122 "non-verbose or numeric form (requires -macho)"));
125 llvm::ObjcMetaData("objc-meta-data",
126 cl::desc("Print the Objective-C runtime meta data for "
127 "Mach-O files (requires -macho)"));
129 cl::opt<std::string> llvm::DisSymName(
131 cl::desc("disassemble just this symbol's instructions (requires -macho)"));
133 static cl::opt<bool> NoSymbolicOperands(
134 "no-symbolic-operands",
135 cl::desc("do not symbolic operands when disassembling (requires -macho)"));
137 static cl::list<std::string>
138 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
141 bool ArchAll = false;
143 static std::string ThumbTripleName;
145 static const Target *GetTarget(const MachOObjectFile *MachOObj,
146 const char **McpuDefault,
147 const Target **ThumbTarget) {
148 // Figure out the target triple.
149 llvm::Triple TT(TripleName);
150 if (TripleName.empty()) {
151 TT = MachOObj->getArchTriple(McpuDefault);
152 TripleName = TT.str();
155 if (TT.getArch() == Triple::arm) {
156 // We've inferred a 32-bit ARM target from the object file. All MachO CPUs
157 // that support ARM are also capable of Thumb mode.
158 llvm::Triple ThumbTriple = TT;
159 std::string ThumbName = (Twine("thumb") + TT.getArchName().substr(3)).str();
160 ThumbTriple.setArchName(ThumbName);
161 ThumbTripleName = ThumbTriple.str();
164 // Get the target specific parser.
166 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
167 if (TheTarget && ThumbTripleName.empty())
170 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
174 errs() << "llvm-objdump: error: unable to get target for '";
176 errs() << TripleName;
178 errs() << ThumbTripleName;
179 errs() << "', see --version and --triple.\n";
183 struct SymbolSorter {
184 bool operator()(const SymbolRef &A, const SymbolRef &B) {
185 Expected<SymbolRef::Type> ATypeOrErr = A.getType();
187 report_error(A.getObject()->getFileName(), ATypeOrErr.takeError());
188 SymbolRef::Type AType = *ATypeOrErr;
189 Expected<SymbolRef::Type> BTypeOrErr = B.getType();
191 report_error(B.getObject()->getFileName(), BTypeOrErr.takeError());
192 SymbolRef::Type BType = *BTypeOrErr;
193 uint64_t AAddr = (AType != SymbolRef::ST_Function) ? 0 : A.getValue();
194 uint64_t BAddr = (BType != SymbolRef::ST_Function) ? 0 : B.getValue();
195 return AAddr < BAddr;
199 // Types for the storted data in code table that is built before disassembly
200 // and the predicate function to sort them.
201 typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
202 typedef std::vector<DiceTableEntry> DiceTable;
203 typedef DiceTable::iterator dice_table_iterator;
205 // This is used to search for a data in code table entry for the PC being
206 // disassembled. The j parameter has the PC in j.first. A single data in code
207 // table entry can cover many bytes for each of its Kind's. So if the offset,
208 // aka the i.first value, of the data in code table entry plus its Length
209 // covers the PC being searched for this will return true. If not it will
211 static bool compareDiceTableEntries(const DiceTableEntry &i,
212 const DiceTableEntry &j) {
214 i.second.getLength(Length);
216 return j.first >= i.first && j.first < i.first + Length;
219 static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
220 unsigned short Kind) {
221 uint32_t Value, Size = 1;
225 case MachO::DICE_KIND_DATA:
228 dumpBytes(makeArrayRef(bytes, 4), outs());
229 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
230 outs() << "\t.long " << Value;
232 } else if (Length >= 2) {
234 dumpBytes(makeArrayRef(bytes, 2), outs());
235 Value = bytes[1] << 8 | bytes[0];
236 outs() << "\t.short " << Value;
240 dumpBytes(makeArrayRef(bytes, 2), outs());
242 outs() << "\t.byte " << Value;
245 if (Kind == MachO::DICE_KIND_DATA)
246 outs() << "\t@ KIND_DATA\n";
248 outs() << "\t@ data in code kind = " << Kind << "\n";
250 case MachO::DICE_KIND_JUMP_TABLE8:
252 dumpBytes(makeArrayRef(bytes, 1), outs());
254 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
257 case MachO::DICE_KIND_JUMP_TABLE16:
259 dumpBytes(makeArrayRef(bytes, 2), outs());
260 Value = bytes[1] << 8 | bytes[0];
261 outs() << "\t.short " << format("%5u", Value & 0xffff)
262 << "\t@ KIND_JUMP_TABLE16\n";
265 case MachO::DICE_KIND_JUMP_TABLE32:
266 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
268 dumpBytes(makeArrayRef(bytes, 4), outs());
269 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
270 outs() << "\t.long " << Value;
271 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
272 outs() << "\t@ KIND_JUMP_TABLE32\n";
274 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
281 static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
282 std::vector<SectionRef> &Sections,
283 std::vector<SymbolRef> &Symbols,
284 SmallVectorImpl<uint64_t> &FoundFns,
285 uint64_t &BaseSegmentAddress) {
286 for (const SymbolRef &Symbol : MachOObj->symbols()) {
287 Expected<StringRef> SymName = Symbol.getName();
289 report_error(MachOObj->getFileName(), SymName.takeError());
290 if (!SymName->startswith("ltmp"))
291 Symbols.push_back(Symbol);
294 for (const SectionRef &Section : MachOObj->sections()) {
296 Section.getName(SectName);
297 Sections.push_back(Section);
300 bool BaseSegmentAddressSet = false;
301 for (const auto &Command : MachOObj->load_commands()) {
302 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
303 // We found a function starts segment, parse the addresses for later
305 MachO::linkedit_data_command LLC =
306 MachOObj->getLinkeditDataLoadCommand(Command);
308 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
309 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
310 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
311 StringRef SegName = SLC.segname;
312 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
313 BaseSegmentAddressSet = true;
314 BaseSegmentAddress = SLC.vmaddr;
320 static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
321 uint32_t n, uint32_t count,
322 uint32_t stride, uint64_t addr) {
323 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
324 uint32_t nindirectsyms = Dysymtab.nindirectsyms;
325 if (n > nindirectsyms)
326 outs() << " (entries start past the end of the indirect symbol "
327 "table) (reserved1 field greater than the table size)";
328 else if (n + count > nindirectsyms)
329 outs() << " (entries extends past the end of the indirect symbol "
332 uint32_t cputype = O->getHeader().cputype;
333 if (cputype & MachO::CPU_ARCH_ABI64)
334 outs() << "address index";
336 outs() << "address index";
341 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
342 if (cputype & MachO::CPU_ARCH_ABI64)
343 outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
345 outs() << format("0x%08" PRIx32, (uint32_t)addr + j * stride) << " ";
346 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
347 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
348 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
352 if (indirect_symbol ==
353 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
354 outs() << "LOCAL ABSOLUTE\n";
357 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
358 outs() << "ABSOLUTE\n";
361 outs() << format("%5u ", indirect_symbol);
363 MachO::symtab_command Symtab = O->getSymtabLoadCommand();
364 if (indirect_symbol < Symtab.nsyms) {
365 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
366 SymbolRef Symbol = *Sym;
367 Expected<StringRef> SymName = Symbol.getName();
369 report_error(O->getFileName(), SymName.takeError());
379 static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
380 for (const auto &Load : O->load_commands()) {
381 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
382 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
383 for (unsigned J = 0; J < Seg.nsects; ++J) {
384 MachO::section_64 Sec = O->getSection64(Load, J);
385 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
386 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
387 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
388 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
389 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
390 section_type == MachO::S_SYMBOL_STUBS) {
392 if (section_type == MachO::S_SYMBOL_STUBS)
393 stride = Sec.reserved2;
397 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
398 << Sec.sectname << ") "
399 << "(size of stubs in reserved2 field is zero)\n";
402 uint32_t count = Sec.size / stride;
403 outs() << "Indirect symbols for (" << Sec.segname << ","
404 << Sec.sectname << ") " << count << " entries";
405 uint32_t n = Sec.reserved1;
406 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
409 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
410 MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
411 for (unsigned J = 0; J < Seg.nsects; ++J) {
412 MachO::section Sec = O->getSection(Load, J);
413 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
414 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
415 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
416 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
417 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
418 section_type == MachO::S_SYMBOL_STUBS) {
420 if (section_type == MachO::S_SYMBOL_STUBS)
421 stride = Sec.reserved2;
425 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
426 << Sec.sectname << ") "
427 << "(size of stubs in reserved2 field is zero)\n";
430 uint32_t count = Sec.size / stride;
431 outs() << "Indirect symbols for (" << Sec.segname << ","
432 << Sec.sectname << ") " << count << " entries";
433 uint32_t n = Sec.reserved1;
434 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
441 static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
442 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
443 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
444 outs() << "Data in code table (" << nentries << " entries)\n";
445 outs() << "offset length kind\n";
446 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
449 DI->getOffset(Offset);
450 outs() << format("0x%08" PRIx32, Offset) << " ";
452 DI->getLength(Length);
453 outs() << format("%6u", Length) << " ";
458 case MachO::DICE_KIND_DATA:
461 case MachO::DICE_KIND_JUMP_TABLE8:
462 outs() << "JUMP_TABLE8";
464 case MachO::DICE_KIND_JUMP_TABLE16:
465 outs() << "JUMP_TABLE16";
467 case MachO::DICE_KIND_JUMP_TABLE32:
468 outs() << "JUMP_TABLE32";
470 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
471 outs() << "ABS_JUMP_TABLE32";
474 outs() << format("0x%04" PRIx32, Kind);
478 outs() << format("0x%04" PRIx32, Kind);
483 static void PrintLinkOptHints(MachOObjectFile *O) {
484 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
485 const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
486 uint32_t nloh = LohLC.datasize;
487 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
488 for (uint32_t i = 0; i < nloh;) {
490 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
492 outs() << " identifier " << identifier << " ";
495 switch (identifier) {
497 outs() << "AdrpAdrp\n";
500 outs() << "AdrpLdr\n";
503 outs() << "AdrpAddLdr\n";
506 outs() << "AdrpLdrGotLdr\n";
509 outs() << "AdrpAddStr\n";
512 outs() << "AdrpLdrGotStr\n";
515 outs() << "AdrpAdd\n";
518 outs() << "AdrpLdrGot\n";
521 outs() << "Unknown identifier value\n";
524 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
526 outs() << " narguments " << narguments << "\n";
530 for (uint32_t j = 0; j < narguments; j++) {
531 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
533 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
540 static void PrintDylibs(MachOObjectFile *O, bool JustId) {
542 for (const auto &Load : O->load_commands()) {
543 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
544 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
545 Load.C.cmd == MachO::LC_LOAD_DYLIB ||
546 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
547 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
548 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
549 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
550 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
551 if (dl.dylib.name < dl.cmdsize) {
552 const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
557 outs() << " (compatibility version "
558 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
559 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
560 << (dl.dylib.compatibility_version & 0xff) << ",";
561 outs() << " current version "
562 << ((dl.dylib.current_version >> 16) & 0xffff) << "."
563 << ((dl.dylib.current_version >> 8) & 0xff) << "."
564 << (dl.dylib.current_version & 0xff) << ")\n";
567 outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
568 if (Load.C.cmd == MachO::LC_ID_DYLIB)
569 outs() << "LC_ID_DYLIB ";
570 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
571 outs() << "LC_LOAD_DYLIB ";
572 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
573 outs() << "LC_LOAD_WEAK_DYLIB ";
574 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
575 outs() << "LC_LAZY_LOAD_DYLIB ";
576 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
577 outs() << "LC_REEXPORT_DYLIB ";
578 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
579 outs() << "LC_LOAD_UPWARD_DYLIB ";
582 outs() << "command " << Index++ << "\n";
588 typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
590 static void CreateSymbolAddressMap(MachOObjectFile *O,
591 SymbolAddressMap *AddrMap) {
592 // Create a map of symbol addresses to symbol names.
593 for (const SymbolRef &Symbol : O->symbols()) {
594 Expected<SymbolRef::Type> STOrErr = Symbol.getType();
596 report_error(O->getFileName(), STOrErr.takeError());
597 SymbolRef::Type ST = *STOrErr;
598 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
599 ST == SymbolRef::ST_Other) {
600 uint64_t Address = Symbol.getValue();
601 Expected<StringRef> SymNameOrErr = Symbol.getName();
603 report_error(O->getFileName(), SymNameOrErr.takeError());
604 StringRef SymName = *SymNameOrErr;
605 if (!SymName.startswith(".objc"))
606 (*AddrMap)[Address] = SymName;
611 // GuessSymbolName is passed the address of what might be a symbol and a
612 // pointer to the SymbolAddressMap. It returns the name of a symbol
613 // with that address or nullptr if no symbol is found with that address.
614 static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
615 const char *SymbolName = nullptr;
616 // A DenseMap can't lookup up some values.
617 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
618 StringRef name = AddrMap->lookup(value);
620 SymbolName = name.data();
625 static void DumpCstringChar(const char c) {
629 outs().write_escaped(p);
632 static void DumpCstringSection(MachOObjectFile *O, const char *sect,
633 uint32_t sect_size, uint64_t sect_addr,
634 bool print_addresses) {
635 for (uint32_t i = 0; i < sect_size; i++) {
636 if (print_addresses) {
638 outs() << format("%016" PRIx64, sect_addr + i) << " ";
640 outs() << format("%08" PRIx64, sect_addr + i) << " ";
642 for (; i < sect_size && sect[i] != '\0'; i++)
643 DumpCstringChar(sect[i]);
644 if (i < sect_size && sect[i] == '\0')
649 static void DumpLiteral4(uint32_t l, float f) {
650 outs() << format("0x%08" PRIx32, l);
651 if ((l & 0x7f800000) != 0x7f800000)
652 outs() << format(" (%.16e)\n", f);
655 outs() << " (+Infinity)\n";
656 else if (l == 0xff800000)
657 outs() << " (-Infinity)\n";
658 else if ((l & 0x00400000) == 0x00400000)
659 outs() << " (non-signaling Not-a-Number)\n";
661 outs() << " (signaling Not-a-Number)\n";
665 static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
666 uint32_t sect_size, uint64_t sect_addr,
667 bool print_addresses) {
668 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
669 if (print_addresses) {
671 outs() << format("%016" PRIx64, sect_addr + i) << " ";
673 outs() << format("%08" PRIx64, sect_addr + i) << " ";
676 memcpy(&f, sect + i, sizeof(float));
677 if (O->isLittleEndian() != sys::IsLittleEndianHost)
678 sys::swapByteOrder(f);
680 memcpy(&l, sect + i, sizeof(uint32_t));
681 if (O->isLittleEndian() != sys::IsLittleEndianHost)
682 sys::swapByteOrder(l);
687 static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
689 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
691 Hi = (O->isLittleEndian()) ? l1 : l0;
692 Lo = (O->isLittleEndian()) ? l0 : l1;
694 // Hi is the high word, so this is equivalent to if(isfinite(d))
695 if ((Hi & 0x7ff00000) != 0x7ff00000)
696 outs() << format(" (%.16e)\n", d);
698 if (Hi == 0x7ff00000 && Lo == 0)
699 outs() << " (+Infinity)\n";
700 else if (Hi == 0xfff00000 && Lo == 0)
701 outs() << " (-Infinity)\n";
702 else if ((Hi & 0x00080000) == 0x00080000)
703 outs() << " (non-signaling Not-a-Number)\n";
705 outs() << " (signaling Not-a-Number)\n";
709 static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
710 uint32_t sect_size, uint64_t sect_addr,
711 bool print_addresses) {
712 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
713 if (print_addresses) {
715 outs() << format("%016" PRIx64, sect_addr + i) << " ";
717 outs() << format("%08" PRIx64, sect_addr + i) << " ";
720 memcpy(&d, sect + i, sizeof(double));
721 if (O->isLittleEndian() != sys::IsLittleEndianHost)
722 sys::swapByteOrder(d);
724 memcpy(&l0, sect + i, sizeof(uint32_t));
725 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
726 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
727 sys::swapByteOrder(l0);
728 sys::swapByteOrder(l1);
730 DumpLiteral8(O, l0, l1, d);
734 static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
735 outs() << format("0x%08" PRIx32, l0) << " ";
736 outs() << format("0x%08" PRIx32, l1) << " ";
737 outs() << format("0x%08" PRIx32, l2) << " ";
738 outs() << format("0x%08" PRIx32, l3) << "\n";
741 static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
742 uint32_t sect_size, uint64_t sect_addr,
743 bool print_addresses) {
744 for (uint32_t i = 0; i < sect_size; i += 16) {
745 if (print_addresses) {
747 outs() << format("%016" PRIx64, sect_addr + i) << " ";
749 outs() << format("%08" PRIx64, sect_addr + i) << " ";
751 uint32_t l0, l1, l2, l3;
752 memcpy(&l0, sect + i, sizeof(uint32_t));
753 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
754 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
755 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
756 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
757 sys::swapByteOrder(l0);
758 sys::swapByteOrder(l1);
759 sys::swapByteOrder(l2);
760 sys::swapByteOrder(l3);
762 DumpLiteral16(l0, l1, l2, l3);
766 static void DumpLiteralPointerSection(MachOObjectFile *O,
767 const SectionRef &Section,
768 const char *sect, uint32_t sect_size,
770 bool print_addresses) {
771 // Collect the literal sections in this Mach-O file.
772 std::vector<SectionRef> LiteralSections;
773 for (const SectionRef &Section : O->sections()) {
774 DataRefImpl Ref = Section.getRawDataRefImpl();
775 uint32_t section_type;
777 const MachO::section_64 Sec = O->getSection64(Ref);
778 section_type = Sec.flags & MachO::SECTION_TYPE;
780 const MachO::section Sec = O->getSection(Ref);
781 section_type = Sec.flags & MachO::SECTION_TYPE;
783 if (section_type == MachO::S_CSTRING_LITERALS ||
784 section_type == MachO::S_4BYTE_LITERALS ||
785 section_type == MachO::S_8BYTE_LITERALS ||
786 section_type == MachO::S_16BYTE_LITERALS)
787 LiteralSections.push_back(Section);
790 // Set the size of the literal pointer.
791 uint32_t lp_size = O->is64Bit() ? 8 : 4;
793 // Collect the external relocation symbols for the literal pointers.
794 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
795 for (const RelocationRef &Reloc : Section.relocations()) {
797 MachO::any_relocation_info RE;
798 bool isExtern = false;
799 Rel = Reloc.getRawDataRefImpl();
800 RE = O->getRelocation(Rel);
801 isExtern = O->getPlainRelocationExternal(RE);
803 uint64_t RelocOffset = Reloc.getOffset();
804 symbol_iterator RelocSym = Reloc.getSymbol();
805 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
808 array_pod_sort(Relocs.begin(), Relocs.end());
810 // Dump each literal pointer.
811 for (uint32_t i = 0; i < sect_size; i += lp_size) {
812 if (print_addresses) {
814 outs() << format("%016" PRIx64, sect_addr + i) << " ";
816 outs() << format("%08" PRIx64, sect_addr + i) << " ";
820 memcpy(&lp, sect + i, sizeof(uint64_t));
821 if (O->isLittleEndian() != sys::IsLittleEndianHost)
822 sys::swapByteOrder(lp);
825 memcpy(&li, sect + i, sizeof(uint32_t));
826 if (O->isLittleEndian() != sys::IsLittleEndianHost)
827 sys::swapByteOrder(li);
831 // First look for an external relocation entry for this literal pointer.
832 auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) {
835 if (Reloc != Relocs.end()) {
836 symbol_iterator RelocSym = Reloc->second;
837 Expected<StringRef> SymName = RelocSym->getName();
839 report_error(O->getFileName(), SymName.takeError());
840 outs() << "external relocation entry for symbol:" << *SymName << "\n";
844 // For local references see what the section the literal pointer points to.
845 auto Sect = find_if(LiteralSections, [&](const SectionRef &R) {
846 return lp >= R.getAddress() && 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,
936 stride = (O->is64Bit()) ? sizeof(uint64_t) : sizeof(uint32_t);
937 for (uint32_t i = 0; i < sect_size; i += stride) {
938 const char *SymbolName = nullptr;
940 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
941 uint64_t pointer_value;
942 memcpy(&pointer_value, sect + i, stride);
943 if (O->isLittleEndian() != sys::IsLittleEndianHost)
944 sys::swapByteOrder(pointer_value);
945 outs() << format("0x%016" PRIx64, pointer_value);
947 SymbolName = GuessSymbolName(pointer_value, AddrMap);
949 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
950 uint32_t pointer_value;
951 memcpy(&pointer_value, sect + i, stride);
952 if (O->isLittleEndian() != sys::IsLittleEndianHost)
953 sys::swapByteOrder(pointer_value);
954 outs() << format("0x%08" PRIx32, pointer_value);
956 SymbolName = GuessSymbolName(pointer_value, AddrMap);
959 outs() << " " << SymbolName;
964 static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
965 uint32_t size, uint64_t addr) {
966 uint32_t cputype = O->getHeader().cputype;
967 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
969 for (uint32_t i = 0; i < size; i += j, addr += j) {
971 outs() << format("%016" PRIx64, addr) << "\t";
973 outs() << format("%08" PRIx64, addr) << "\t";
974 for (j = 0; j < 16 && i + j < size; j++) {
975 uint8_t byte_word = *(sect + i + j);
976 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
982 for (uint32_t i = 0; i < size; i += j, addr += j) {
984 outs() << format("%016" PRIx64, addr) << "\t";
986 outs() << format("%08" PRIx64, addr) << "\t";
987 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
988 j += sizeof(int32_t)) {
989 if (i + j + sizeof(int32_t) <= size) {
991 memcpy(&long_word, sect + i + j, sizeof(int32_t));
992 if (O->isLittleEndian() != sys::IsLittleEndianHost)
993 sys::swapByteOrder(long_word);
994 outs() << format("%08" PRIx32, long_word) << " ";
996 for (uint32_t k = 0; i + j + k < size; k++) {
997 uint8_t byte_word = *(sect + i + j + k);
998 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
1007 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
1008 StringRef DisSegName, StringRef DisSectName);
1009 static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
1010 uint32_t size, uint32_t addr);
1012 static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
1013 uint32_t size, bool verbose,
1014 bool PrintXarHeader, bool PrintXarFileHeaders,
1015 std::string XarMemberName);
1016 #endif // defined(HAVE_LIBXAR)
1018 static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
1020 SymbolAddressMap AddrMap;
1022 CreateSymbolAddressMap(O, &AddrMap);
1024 for (unsigned i = 0; i < FilterSections.size(); ++i) {
1025 StringRef DumpSection = FilterSections[i];
1026 std::pair<StringRef, StringRef> DumpSegSectName;
1027 DumpSegSectName = DumpSection.split(',');
1028 StringRef DumpSegName, DumpSectName;
1029 if (DumpSegSectName.second.size()) {
1030 DumpSegName = DumpSegSectName.first;
1031 DumpSectName = DumpSegSectName.second;
1034 DumpSectName = DumpSegSectName.first;
1036 for (const SectionRef &Section : O->sections()) {
1038 Section.getName(SectName);
1039 DataRefImpl Ref = Section.getRawDataRefImpl();
1040 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1041 if ((DumpSegName.empty() || SegName == DumpSegName) &&
1042 (SectName == DumpSectName)) {
1044 uint32_t section_flags;
1046 const MachO::section_64 Sec = O->getSection64(Ref);
1047 section_flags = Sec.flags;
1050 const MachO::section Sec = O->getSection(Ref);
1051 section_flags = Sec.flags;
1053 uint32_t section_type = section_flags & MachO::SECTION_TYPE;
1056 Section.getContents(BytesStr);
1057 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1058 uint32_t sect_size = BytesStr.size();
1059 uint64_t sect_addr = Section.getAddress();
1061 outs() << "Contents of (" << SegName << "," << SectName
1065 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1066 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1067 DisassembleMachO(Filename, O, SegName, SectName);
1070 if (SegName == "__TEXT" && SectName == "__info_plist") {
1074 if (SegName == "__OBJC" && SectName == "__protocol") {
1075 DumpProtocolSection(O, sect, sect_size, sect_addr);
1079 if (SegName == "__LLVM" && SectName == "__bundle") {
1080 DumpBitcodeSection(O, sect, sect_size, verbose, !NoSymbolicOperands,
1081 ArchiveHeaders, "");
1084 #endif // defined(HAVE_LIBXAR)
1085 switch (section_type) {
1086 case MachO::S_REGULAR:
1087 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1089 case MachO::S_ZEROFILL:
1090 outs() << "zerofill section and has no contents in the file\n";
1092 case MachO::S_CSTRING_LITERALS:
1093 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1095 case MachO::S_4BYTE_LITERALS:
1096 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1098 case MachO::S_8BYTE_LITERALS:
1099 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1101 case MachO::S_16BYTE_LITERALS:
1102 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1104 case MachO::S_LITERAL_POINTERS:
1105 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
1108 case MachO::S_MOD_INIT_FUNC_POINTERS:
1109 case MachO::S_MOD_TERM_FUNC_POINTERS:
1110 DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap,
1114 outs() << "Unknown section type ("
1115 << format("0x%08" PRIx32, section_type) << ")\n";
1116 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1120 if (section_type == MachO::S_ZEROFILL)
1121 outs() << "zerofill section and has no contents in the file\n";
1123 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1130 static void DumpInfoPlistSectionContents(StringRef Filename,
1131 MachOObjectFile *O) {
1132 for (const SectionRef &Section : O->sections()) {
1134 Section.getName(SectName);
1135 DataRefImpl Ref = Section.getRawDataRefImpl();
1136 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1137 if (SegName == "__TEXT" && SectName == "__info_plist") {
1138 if (!NoLeadingHeaders)
1139 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
1141 Section.getContents(BytesStr);
1142 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1143 outs() << format("%.*s", BytesStr.size(), sect) << "\n";
1149 // checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1150 // and if it is and there is a list of architecture flags is specified then
1151 // check to make sure this Mach-O file is one of those architectures or all
1152 // architectures were specified. If not then an error is generated and this
1153 // routine returns false. Else it returns true.
1154 static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
1155 auto *MachO = dyn_cast<MachOObjectFile>(O);
1157 if (!MachO || ArchAll || ArchFlags.empty())
1160 MachO::mach_header H;
1161 MachO::mach_header_64 H_64;
1163 const char *McpuDefault, *ArchFlag;
1164 if (MachO->is64Bit()) {
1165 H_64 = MachO->MachOObjectFile::getHeader64();
1166 T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype,
1167 &McpuDefault, &ArchFlag);
1169 H = MachO->MachOObjectFile::getHeader();
1170 T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype,
1171 &McpuDefault, &ArchFlag);
1173 const std::string ArchFlagName(ArchFlag);
1174 if (none_of(ArchFlags, [&](const std::string &Name) {
1175 return Name == ArchFlagName;
1177 errs() << "llvm-objdump: " + Filename + ": No architecture specified.\n";
1183 static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1185 // ProcessMachO() is passed a single opened Mach-O file, which may be an
1186 // archive member and or in a slice of a universal file. It prints the
1187 // the file name and header info and then processes it according to the
1188 // command line options.
1189 static void ProcessMachO(StringRef Name, MachOObjectFile *MachOOF,
1190 StringRef ArchiveMemberName = StringRef(),
1191 StringRef ArchitectureName = StringRef()) {
1192 // If we are doing some processing here on the Mach-O file print the header
1193 // info. And don't print it otherwise like in the case of printing the
1194 // UniversalHeaders or ArchiveHeaders.
1195 if (Disassemble || PrivateHeaders || ExportsTrie || Rebase || Bind || SymbolTable ||
1196 LazyBind || WeakBind || IndirectSymbols || DataInCode || LinkOptHints ||
1197 DylibsUsed || DylibId || ObjcMetaData || (FilterSections.size() != 0)) {
1198 if (!NoLeadingHeaders) {
1200 if (!ArchiveMemberName.empty())
1201 outs() << '(' << ArchiveMemberName << ')';
1202 if (!ArchitectureName.empty())
1203 outs() << " (architecture " << ArchitectureName << ")";
1207 // To use the report_error() form with an ArchiveName and FileName set
1208 // these up based on what is passed for Name and ArchiveMemberName.
1209 StringRef ArchiveName;
1211 if (!ArchiveMemberName.empty()) {
1213 FileName = ArchiveMemberName;
1215 ArchiveName = StringRef();
1219 // If we need the symbol table to do the operation then check it here to
1220 // produce a good error message as to where the Mach-O file comes from in
1221 // the error message.
1222 if (Disassemble || IndirectSymbols || FilterSections.size() != 0 ||
1224 if (Error Err = MachOOF->checkSymbolTable())
1225 report_error(ArchiveName, FileName, std::move(Err), ArchitectureName);
1228 if (MachOOF->getHeader().filetype == MachO::MH_KEXT_BUNDLE &&
1229 MachOOF->getHeader().cputype == MachO::CPU_TYPE_ARM64)
1230 DisassembleMachO(FileName, MachOOF, "__TEXT_EXEC", "__text");
1232 DisassembleMachO(FileName, MachOOF, "__TEXT", "__text");
1234 if (IndirectSymbols)
1235 PrintIndirectSymbols(MachOOF, !NonVerbose);
1237 PrintDataInCodeTable(MachOOF, !NonVerbose);
1239 PrintLinkOptHints(MachOOF);
1241 PrintRelocations(MachOOF);
1243 PrintSectionHeaders(MachOOF);
1244 if (SectionContents)
1245 PrintSectionContents(MachOOF);
1246 if (FilterSections.size() != 0)
1247 DumpSectionContents(FileName, MachOOF, !NonVerbose);
1249 DumpInfoPlistSectionContents(FileName, MachOOF);
1251 PrintDylibs(MachOOF, false);
1253 PrintDylibs(MachOOF, true);
1255 PrintSymbolTable(MachOOF, ArchiveName, ArchitectureName);
1257 printMachOUnwindInfo(MachOOF);
1258 if (PrivateHeaders) {
1259 printMachOFileHeader(MachOOF);
1260 printMachOLoadCommands(MachOOF);
1262 if (FirstPrivateHeader)
1263 printMachOFileHeader(MachOOF);
1265 printObjcMetaData(MachOOF, !NonVerbose);
1267 printExportsTrie(MachOOF);
1269 printRebaseTable(MachOOF);
1271 printBindTable(MachOOF);
1273 printLazyBindTable(MachOOF);
1275 printWeakBindTable(MachOOF);
1277 if (DwarfDumpType != DIDT_Null) {
1278 std::unique_ptr<DIContext> DICtx(new DWARFContextInMemory(*MachOOF));
1279 // Dump the complete DWARF structure.
1280 DIDumpOptions DumpOpts;
1281 DumpOpts.DumpType = DwarfDumpType;
1282 DumpOpts.DumpEH = true;
1283 DICtx->dump(outs(), DumpOpts);
1287 // printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1288 static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
1289 outs() << " cputype (" << cputype << ")\n";
1290 outs() << " cpusubtype (" << cpusubtype << ")\n";
1293 // printCPUType() helps print_fat_headers by printing the cputype and
1294 // pusubtype (symbolically for the one's it knows about).
1295 static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
1297 case MachO::CPU_TYPE_I386:
1298 switch (cpusubtype) {
1299 case MachO::CPU_SUBTYPE_I386_ALL:
1300 outs() << " cputype CPU_TYPE_I386\n";
1301 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
1304 printUnknownCPUType(cputype, cpusubtype);
1308 case MachO::CPU_TYPE_X86_64:
1309 switch (cpusubtype) {
1310 case MachO::CPU_SUBTYPE_X86_64_ALL:
1311 outs() << " cputype CPU_TYPE_X86_64\n";
1312 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1314 case MachO::CPU_SUBTYPE_X86_64_H:
1315 outs() << " cputype CPU_TYPE_X86_64\n";
1316 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
1319 printUnknownCPUType(cputype, cpusubtype);
1323 case MachO::CPU_TYPE_ARM:
1324 switch (cpusubtype) {
1325 case MachO::CPU_SUBTYPE_ARM_ALL:
1326 outs() << " cputype CPU_TYPE_ARM\n";
1327 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
1329 case MachO::CPU_SUBTYPE_ARM_V4T:
1330 outs() << " cputype CPU_TYPE_ARM\n";
1331 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
1333 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1334 outs() << " cputype CPU_TYPE_ARM\n";
1335 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
1337 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1338 outs() << " cputype CPU_TYPE_ARM\n";
1339 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
1341 case MachO::CPU_SUBTYPE_ARM_V6:
1342 outs() << " cputype CPU_TYPE_ARM\n";
1343 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
1345 case MachO::CPU_SUBTYPE_ARM_V6M:
1346 outs() << " cputype CPU_TYPE_ARM\n";
1347 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
1349 case MachO::CPU_SUBTYPE_ARM_V7:
1350 outs() << " cputype CPU_TYPE_ARM\n";
1351 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
1353 case MachO::CPU_SUBTYPE_ARM_V7EM:
1354 outs() << " cputype CPU_TYPE_ARM\n";
1355 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
1357 case MachO::CPU_SUBTYPE_ARM_V7K:
1358 outs() << " cputype CPU_TYPE_ARM\n";
1359 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
1361 case MachO::CPU_SUBTYPE_ARM_V7M:
1362 outs() << " cputype CPU_TYPE_ARM\n";
1363 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
1365 case MachO::CPU_SUBTYPE_ARM_V7S:
1366 outs() << " cputype CPU_TYPE_ARM\n";
1367 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
1370 printUnknownCPUType(cputype, cpusubtype);
1374 case MachO::CPU_TYPE_ARM64:
1375 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1376 case MachO::CPU_SUBTYPE_ARM64_ALL:
1377 outs() << " cputype CPU_TYPE_ARM64\n";
1378 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
1381 printUnknownCPUType(cputype, cpusubtype);
1386 printUnknownCPUType(cputype, cpusubtype);
1391 static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
1393 outs() << "Fat headers\n";
1395 if (UB->getMagic() == MachO::FAT_MAGIC)
1396 outs() << "fat_magic FAT_MAGIC\n";
1397 else // UB->getMagic() == MachO::FAT_MAGIC_64
1398 outs() << "fat_magic FAT_MAGIC_64\n";
1400 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
1402 uint32_t nfat_arch = UB->getNumberOfObjects();
1403 StringRef Buf = UB->getData();
1404 uint64_t size = Buf.size();
1405 uint64_t big_size = sizeof(struct MachO::fat_header) +
1406 nfat_arch * sizeof(struct MachO::fat_arch);
1407 outs() << "nfat_arch " << UB->getNumberOfObjects();
1409 outs() << " (malformed, contains zero architecture types)\n";
1410 else if (big_size > size)
1411 outs() << " (malformed, architectures past end of file)\n";
1415 for (uint32_t i = 0; i < nfat_arch; ++i) {
1416 MachOUniversalBinary::ObjectForArch OFA(UB, i);
1417 uint32_t cputype = OFA.getCPUType();
1418 uint32_t cpusubtype = OFA.getCPUSubType();
1419 outs() << "architecture ";
1420 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
1421 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
1422 uint32_t other_cputype = other_OFA.getCPUType();
1423 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
1424 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
1425 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
1426 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
1427 outs() << "(illegal duplicate architecture) ";
1432 outs() << OFA.getArchFlagName() << "\n";
1433 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1435 outs() << i << "\n";
1436 outs() << " cputype " << cputype << "\n";
1437 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
1441 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
1442 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
1444 outs() << " capabilities "
1445 << format("0x%" PRIx32,
1446 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
1447 outs() << " offset " << OFA.getOffset();
1448 if (OFA.getOffset() > size)
1449 outs() << " (past end of file)";
1450 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
1451 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
1453 outs() << " size " << OFA.getSize();
1454 big_size = OFA.getOffset() + OFA.getSize();
1455 if (big_size > size)
1456 outs() << " (past end of file)";
1458 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
1463 static void printArchiveChild(StringRef Filename, const Archive::Child &C,
1464 bool verbose, bool print_offset,
1465 StringRef ArchitectureName = StringRef()) {
1467 outs() << C.getChildOffset() << "\t";
1468 Expected<sys::fs::perms> ModeOrErr = C.getAccessMode();
1470 report_error(Filename, C, ModeOrErr.takeError(), ArchitectureName);
1471 sys::fs::perms Mode = ModeOrErr.get();
1473 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
1474 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
1476 outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
1477 outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
1478 outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
1479 outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
1480 outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
1481 outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
1482 outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
1483 outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
1484 outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
1486 outs() << format("0%o ", Mode);
1489 Expected<unsigned> UIDOrErr = C.getUID();
1491 report_error(Filename, C, UIDOrErr.takeError(), ArchitectureName);
1492 unsigned UID = UIDOrErr.get();
1493 outs() << format("%3d/", UID);
1494 Expected<unsigned> GIDOrErr = C.getGID();
1496 report_error(Filename, C, GIDOrErr.takeError(), ArchitectureName);
1497 unsigned GID = GIDOrErr.get();
1498 outs() << format("%-3d ", GID);
1499 Expected<uint64_t> Size = C.getRawSize();
1501 report_error(Filename, C, Size.takeError(), ArchitectureName);
1502 outs() << format("%5" PRId64, Size.get()) << " ";
1504 StringRef RawLastModified = C.getRawLastModified();
1507 if (RawLastModified.getAsInteger(10, Seconds))
1508 outs() << "(date: \"" << RawLastModified
1509 << "\" contains non-decimal chars) ";
1511 // Since cime(3) returns a 26 character string of the form:
1512 // "Sun Sep 16 01:03:52 1973\n\0"
1513 // just print 24 characters.
1515 outs() << format("%.24s ", ctime(&t));
1518 outs() << RawLastModified << " ";
1522 Expected<StringRef> NameOrErr = C.getName();
1524 consumeError(NameOrErr.takeError());
1525 Expected<StringRef> NameOrErr = C.getRawName();
1527 report_error(Filename, C, NameOrErr.takeError(), ArchitectureName);
1528 StringRef RawName = NameOrErr.get();
1529 outs() << RawName << "\n";
1531 StringRef Name = NameOrErr.get();
1532 outs() << Name << "\n";
1535 Expected<StringRef> NameOrErr = C.getRawName();
1537 report_error(Filename, C, NameOrErr.takeError(), ArchitectureName);
1538 StringRef RawName = NameOrErr.get();
1539 outs() << RawName << "\n";
1543 static void printArchiveHeaders(StringRef Filename, Archive *A, bool verbose,
1545 StringRef ArchitectureName = StringRef()) {
1546 Error Err = Error::success();
1548 for (const auto &C : A->children(Err, false))
1549 printArchiveChild(Filename, C, verbose, print_offset, ArchitectureName);
1552 report_error(StringRef(), Filename, std::move(Err), ArchitectureName);
1555 // ParseInputMachO() parses the named Mach-O file in Filename and handles the
1556 // -arch flags selecting just those slices as specified by them and also parses
1557 // archive files. Then for each individual Mach-O file ProcessMachO() is
1558 // called to process the file based on the command line options.
1559 void llvm::ParseInputMachO(StringRef Filename) {
1560 // Check for -arch all and verifiy the -arch flags are valid.
1561 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1562 if (ArchFlags[i] == "all") {
1565 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
1566 errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
1567 "'for the -arch option\n";
1573 // Attempt to open the binary.
1574 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
1576 if (auto E = isNotObjectErrorInvalidFileType(BinaryOrErr.takeError()))
1577 report_error(Filename, std::move(E));
1579 outs() << Filename << ": is not an object file\n";
1582 Binary &Bin = *BinaryOrErr.get().getBinary();
1584 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1585 outs() << "Archive : " << Filename << "\n";
1587 printArchiveHeaders(Filename, A, !NonVerbose, ArchiveMemberOffsets);
1589 Error Err = Error::success();
1590 for (auto &C : A->children(Err)) {
1591 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1593 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1594 report_error(Filename, C, std::move(E));
1597 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1598 if (!checkMachOAndArchFlags(O, Filename))
1600 ProcessMachO(Filename, O, O->getFileName());
1604 report_error(Filename, std::move(Err));
1607 if (UniversalHeaders) {
1608 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
1609 printMachOUniversalHeaders(UB, !NonVerbose);
1611 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1612 // If we have a list of architecture flags specified dump only those.
1613 if (!ArchAll && ArchFlags.size() != 0) {
1614 // Look for a slice in the universal binary that matches each ArchFlag.
1616 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1618 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1619 E = UB->end_objects();
1621 if (ArchFlags[i] == I->getArchFlagName()) {
1623 Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
1624 I->getAsObjectFile();
1625 std::string ArchitectureName = "";
1626 if (ArchFlags.size() > 1)
1627 ArchitectureName = I->getArchFlagName();
1629 ObjectFile &O = *ObjOrErr.get();
1630 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
1631 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
1632 } else if (auto E = isNotObjectErrorInvalidFileType(
1633 ObjOrErr.takeError())) {
1634 report_error(Filename, StringRef(), std::move(E),
1637 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
1638 I->getAsArchive()) {
1639 std::unique_ptr<Archive> &A = *AOrErr;
1640 outs() << "Archive : " << Filename;
1641 if (!ArchitectureName.empty())
1642 outs() << " (architecture " << ArchitectureName << ")";
1645 printArchiveHeaders(Filename, A.get(), !NonVerbose,
1646 ArchiveMemberOffsets, ArchitectureName);
1647 Error Err = Error::success();
1648 for (auto &C : A->children(Err)) {
1649 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1651 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1652 report_error(Filename, C, std::move(E), ArchitectureName);
1655 if (MachOObjectFile *O =
1656 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
1657 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
1660 report_error(Filename, std::move(Err));
1662 consumeError(AOrErr.takeError());
1663 error("Mach-O universal file: " + Filename + " for " +
1664 "architecture " + StringRef(I->getArchFlagName()) +
1665 " is not a Mach-O file or an archive file");
1670 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1671 << "architecture: " + ArchFlags[i] + "\n";
1677 // No architecture flags were specified so if this contains a slice that
1678 // matches the host architecture dump only that.
1680 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1681 E = UB->end_objects();
1683 if (MachOObjectFile::getHostArch().getArchName() ==
1684 I->getArchFlagName()) {
1685 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1686 std::string ArchiveName;
1687 ArchiveName.clear();
1689 ObjectFile &O = *ObjOrErr.get();
1690 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
1691 ProcessMachO(Filename, MachOOF);
1692 } else if (auto E = isNotObjectErrorInvalidFileType(
1693 ObjOrErr.takeError())) {
1694 report_error(Filename, std::move(E));
1696 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
1697 I->getAsArchive()) {
1698 std::unique_ptr<Archive> &A = *AOrErr;
1699 outs() << "Archive : " << Filename << "\n";
1701 printArchiveHeaders(Filename, A.get(), !NonVerbose,
1702 ArchiveMemberOffsets);
1703 Error Err = Error::success();
1704 for (auto &C : A->children(Err)) {
1705 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1707 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1708 report_error(Filename, C, std::move(E));
1711 if (MachOObjectFile *O =
1712 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
1713 ProcessMachO(Filename, O, O->getFileName());
1716 report_error(Filename, std::move(Err));
1718 consumeError(AOrErr.takeError());
1719 error("Mach-O universal file: " + Filename + " for architecture " +
1720 StringRef(I->getArchFlagName()) +
1721 " is not a Mach-O file or an archive file");
1727 // Either all architectures have been specified or none have been specified
1728 // and this does not contain the host architecture so dump all the slices.
1729 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1730 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1731 E = UB->end_objects();
1733 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1734 std::string ArchitectureName = "";
1735 if (moreThanOneArch)
1736 ArchitectureName = I->getArchFlagName();
1738 ObjectFile &Obj = *ObjOrErr.get();
1739 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
1740 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
1741 } else if (auto E = isNotObjectErrorInvalidFileType(
1742 ObjOrErr.takeError())) {
1743 report_error(StringRef(), Filename, std::move(E), ArchitectureName);
1745 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
1746 I->getAsArchive()) {
1747 std::unique_ptr<Archive> &A = *AOrErr;
1748 outs() << "Archive : " << Filename;
1749 if (!ArchitectureName.empty())
1750 outs() << " (architecture " << ArchitectureName << ")";
1753 printArchiveHeaders(Filename, A.get(), !NonVerbose,
1754 ArchiveMemberOffsets, ArchitectureName);
1755 Error Err = Error::success();
1756 for (auto &C : A->children(Err)) {
1757 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1759 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1760 report_error(Filename, C, std::move(E), ArchitectureName);
1763 if (MachOObjectFile *O =
1764 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1765 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
1766 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
1771 report_error(Filename, std::move(Err));
1773 consumeError(AOrErr.takeError());
1774 error("Mach-O universal file: " + Filename + " for architecture " +
1775 StringRef(I->getArchFlagName()) +
1776 " is not a Mach-O file or an archive file");
1781 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
1782 if (!checkMachOAndArchFlags(O, Filename))
1784 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
1785 ProcessMachO(Filename, MachOOF);
1787 errs() << "llvm-objdump: '" << Filename << "': "
1788 << "Object is not a Mach-O file type.\n";
1791 llvm_unreachable("Input object can't be invalid at this point");
1794 // The block of info used by the Symbolizer call backs.
1795 struct DisassembleInfo {
1799 SymbolAddressMap *AddrMap;
1800 std::vector<SectionRef> *Sections;
1801 const char *class_name;
1802 const char *selector_name;
1804 char *demangled_name;
1807 std::unique_ptr<SymbolAddressMap> bindtable;
1811 // SymbolizerGetOpInfo() is the operand information call back function.
1812 // This is called to get the symbolic information for operand(s) of an
1813 // instruction when it is being done. This routine does this from
1814 // the relocation information, symbol table, etc. That block of information
1815 // is a pointer to the struct DisassembleInfo that was passed when the
1816 // disassembler context was created and passed to back to here when
1817 // called back by the disassembler for instruction operands that could have
1818 // relocation information. The address of the instruction containing operand is
1819 // at the Pc parameter. The immediate value the operand has is passed in
1820 // op_info->Value and is at Offset past the start of the instruction and has a
1821 // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
1822 // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
1823 // names and addends of the symbolic expression to add for the operand. The
1824 // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
1825 // information is returned then this function returns 1 else it returns 0.
1826 static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
1827 uint64_t Size, int TagType, void *TagBuf) {
1828 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
1829 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
1830 uint64_t value = op_info->Value;
1832 // Make sure all fields returned are zero if we don't set them.
1833 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
1834 op_info->Value = value;
1836 // If the TagType is not the value 1 which it code knows about or if no
1837 // verbose symbolic information is wanted then just return 0, indicating no
1838 // information is being returned.
1839 if (TagType != 1 || !info->verbose)
1842 unsigned int Arch = info->O->getArch();
1843 if (Arch == Triple::x86) {
1844 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1846 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
1848 // Search the external relocation entries of a fully linked image
1849 // (if any) for an entry that matches this segment offset.
1850 // uint32_t seg_offset = (Pc + Offset);
1853 // In MH_OBJECT filetypes search the section's relocation entries (if any)
1854 // for an entry for this section offset.
1855 uint32_t sect_addr = info->S.getAddress();
1856 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1857 bool reloc_found = false;
1859 MachO::any_relocation_info RE;
1860 bool isExtern = false;
1862 bool r_scattered = false;
1863 uint32_t r_value, pair_r_value, r_type;
1864 for (const RelocationRef &Reloc : info->S.relocations()) {
1865 uint64_t RelocOffset = Reloc.getOffset();
1866 if (RelocOffset == sect_offset) {
1867 Rel = Reloc.getRawDataRefImpl();
1868 RE = info->O->getRelocation(Rel);
1869 r_type = info->O->getAnyRelocationType(RE);
1870 r_scattered = info->O->isRelocationScattered(RE);
1872 r_value = info->O->getScatteredRelocationValue(RE);
1873 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1874 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
1875 DataRefImpl RelNext = Rel;
1876 info->O->moveRelocationNext(RelNext);
1877 MachO::any_relocation_info RENext;
1878 RENext = info->O->getRelocation(RelNext);
1879 if (info->O->isRelocationScattered(RENext))
1880 pair_r_value = info->O->getScatteredRelocationValue(RENext);
1885 isExtern = info->O->getPlainRelocationExternal(RE);
1887 symbol_iterator RelocSym = Reloc.getSymbol();
1895 if (reloc_found && isExtern) {
1896 Expected<StringRef> SymName = Symbol.getName();
1898 report_error(info->O->getFileName(), SymName.takeError());
1899 const char *name = SymName->data();
1900 op_info->AddSymbol.Present = 1;
1901 op_info->AddSymbol.Name = name;
1902 // For i386 extern relocation entries the value in the instruction is
1903 // the offset from the symbol, and value is already set in op_info->Value.
1906 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1907 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
1908 const char *add = GuessSymbolName(r_value, info->AddrMap);
1909 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
1910 uint32_t offset = value - (r_value - pair_r_value);
1911 op_info->AddSymbol.Present = 1;
1913 op_info->AddSymbol.Name = add;
1915 op_info->AddSymbol.Value = r_value;
1916 op_info->SubtractSymbol.Present = 1;
1918 op_info->SubtractSymbol.Name = sub;
1920 op_info->SubtractSymbol.Value = pair_r_value;
1921 op_info->Value = offset;
1926 if (Arch == Triple::x86_64) {
1927 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1929 // For non MH_OBJECT types, like MH_KEXT_BUNDLE, Search the external
1930 // relocation entries of a linked image (if any) for an entry that matches
1931 // this segment offset.
1932 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
1933 uint64_t seg_offset = Pc + Offset;
1934 bool reloc_found = false;
1936 MachO::any_relocation_info RE;
1937 bool isExtern = false;
1939 for (const RelocationRef &Reloc : info->O->external_relocations()) {
1940 uint64_t RelocOffset = Reloc.getOffset();
1941 if (RelocOffset == seg_offset) {
1942 Rel = Reloc.getRawDataRefImpl();
1943 RE = info->O->getRelocation(Rel);
1944 // external relocation entries should always be external.
1945 isExtern = info->O->getPlainRelocationExternal(RE);
1947 symbol_iterator RelocSym = Reloc.getSymbol();
1954 if (reloc_found && isExtern) {
1955 // The Value passed in will be adjusted by the Pc if the instruction
1956 // adds the Pc. But for x86_64 external relocation entries the Value
1957 // is the offset from the external symbol.
1958 if (info->O->getAnyRelocationPCRel(RE))
1959 op_info->Value -= Pc + Offset + Size;
1960 Expected<StringRef> SymName = Symbol.getName();
1962 report_error(info->O->getFileName(), SymName.takeError());
1963 const char *name = SymName->data();
1964 op_info->AddSymbol.Present = 1;
1965 op_info->AddSymbol.Name = name;
1970 // In MH_OBJECT filetypes search the section's relocation entries (if any)
1971 // for an entry for this section offset.
1972 uint64_t sect_addr = info->S.getAddress();
1973 uint64_t sect_offset = (Pc + Offset) - sect_addr;
1974 bool reloc_found = false;
1976 MachO::any_relocation_info RE;
1977 bool isExtern = false;
1979 for (const RelocationRef &Reloc : info->S.relocations()) {
1980 uint64_t RelocOffset = Reloc.getOffset();
1981 if (RelocOffset == sect_offset) {
1982 Rel = Reloc.getRawDataRefImpl();
1983 RE = info->O->getRelocation(Rel);
1984 // NOTE: Scattered relocations don't exist on x86_64.
1985 isExtern = info->O->getPlainRelocationExternal(RE);
1987 symbol_iterator RelocSym = Reloc.getSymbol();
1994 if (reloc_found && isExtern) {
1995 // The Value passed in will be adjusted by the Pc if the instruction
1996 // adds the Pc. But for x86_64 external relocation entries the Value
1997 // is the offset from the external symbol.
1998 if (info->O->getAnyRelocationPCRel(RE))
1999 op_info->Value -= Pc + Offset + Size;
2000 Expected<StringRef> SymName = Symbol.getName();
2002 report_error(info->O->getFileName(), SymName.takeError());
2003 const char *name = SymName->data();
2004 unsigned Type = info->O->getAnyRelocationType(RE);
2005 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
2006 DataRefImpl RelNext = Rel;
2007 info->O->moveRelocationNext(RelNext);
2008 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2009 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
2010 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
2011 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
2012 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
2013 op_info->SubtractSymbol.Present = 1;
2014 op_info->SubtractSymbol.Name = name;
2015 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
2016 Symbol = *RelocSymNext;
2017 Expected<StringRef> SymNameNext = Symbol.getName();
2019 report_error(info->O->getFileName(), SymNameNext.takeError());
2020 name = SymNameNext->data();
2023 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
2024 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
2025 op_info->AddSymbol.Present = 1;
2026 op_info->AddSymbol.Name = name;
2031 if (Arch == Triple::arm) {
2032 if (Offset != 0 || (Size != 4 && Size != 2))
2034 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2036 // Search the external relocation entries of a fully linked image
2037 // (if any) for an entry that matches this segment offset.
2038 // uint32_t seg_offset = (Pc + Offset);
2041 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2042 // for an entry for this section offset.
2043 uint32_t sect_addr = info->S.getAddress();
2044 uint32_t sect_offset = (Pc + Offset) - sect_addr;
2046 MachO::any_relocation_info RE;
2047 bool isExtern = false;
2049 bool r_scattered = false;
2050 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
2052 find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
2053 uint64_t RelocOffset = Reloc.getOffset();
2054 return RelocOffset == sect_offset;
2057 if (Reloc == info->S.relocations().end())
2060 Rel = Reloc->getRawDataRefImpl();
2061 RE = info->O->getRelocation(Rel);
2062 r_length = info->O->getAnyRelocationLength(RE);
2063 r_scattered = info->O->isRelocationScattered(RE);
2065 r_value = info->O->getScatteredRelocationValue(RE);
2066 r_type = info->O->getScatteredRelocationType(RE);
2068 r_type = info->O->getAnyRelocationType(RE);
2069 isExtern = info->O->getPlainRelocationExternal(RE);
2071 symbol_iterator RelocSym = Reloc->getSymbol();
2075 if (r_type == MachO::ARM_RELOC_HALF ||
2076 r_type == MachO::ARM_RELOC_SECTDIFF ||
2077 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
2078 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2079 DataRefImpl RelNext = Rel;
2080 info->O->moveRelocationNext(RelNext);
2081 MachO::any_relocation_info RENext;
2082 RENext = info->O->getRelocation(RelNext);
2083 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
2084 if (info->O->isRelocationScattered(RENext))
2085 pair_r_value = info->O->getScatteredRelocationValue(RENext);
2089 Expected<StringRef> SymName = Symbol.getName();
2091 report_error(info->O->getFileName(), SymName.takeError());
2092 const char *name = SymName->data();
2093 op_info->AddSymbol.Present = 1;
2094 op_info->AddSymbol.Name = name;
2096 case MachO::ARM_RELOC_HALF:
2097 if ((r_length & 0x1) == 1) {
2098 op_info->Value = value << 16 | other_half;
2099 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2101 op_info->Value = other_half << 16 | value;
2102 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2110 // If we have a branch that is not an external relocation entry then
2111 // return 0 so the code in tryAddingSymbolicOperand() can use the
2112 // SymbolLookUp call back with the branch target address to look up the
2113 // symbol and possibility add an annotation for a symbol stub.
2114 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
2115 r_type == MachO::ARM_THUMB_RELOC_BR22))
2118 uint32_t offset = 0;
2119 if (r_type == MachO::ARM_RELOC_HALF ||
2120 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2121 if ((r_length & 0x1) == 1)
2122 value = value << 16 | other_half;
2124 value = other_half << 16 | value;
2126 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
2127 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
2128 offset = value - r_value;
2132 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2133 if ((r_length & 0x1) == 1)
2134 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2136 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2137 const char *add = GuessSymbolName(r_value, info->AddrMap);
2138 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
2139 int32_t offset = value - (r_value - pair_r_value);
2140 op_info->AddSymbol.Present = 1;
2142 op_info->AddSymbol.Name = add;
2144 op_info->AddSymbol.Value = r_value;
2145 op_info->SubtractSymbol.Present = 1;
2147 op_info->SubtractSymbol.Name = sub;
2149 op_info->SubtractSymbol.Value = pair_r_value;
2150 op_info->Value = offset;
2154 op_info->AddSymbol.Present = 1;
2155 op_info->Value = offset;
2156 if (r_type == MachO::ARM_RELOC_HALF) {
2157 if ((r_length & 0x1) == 1)
2158 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2160 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2162 const char *add = GuessSymbolName(value, info->AddrMap);
2163 if (add != nullptr) {
2164 op_info->AddSymbol.Name = add;
2167 op_info->AddSymbol.Value = value;
2170 if (Arch == Triple::aarch64) {
2171 if (Offset != 0 || Size != 4)
2173 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2175 // Search the external relocation entries of a fully linked image
2176 // (if any) for an entry that matches this segment offset.
2177 // uint64_t seg_offset = (Pc + Offset);
2180 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2181 // for an entry for this section offset.
2182 uint64_t sect_addr = info->S.getAddress();
2183 uint64_t sect_offset = (Pc + Offset) - sect_addr;
2185 find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
2186 uint64_t RelocOffset = Reloc.getOffset();
2187 return RelocOffset == sect_offset;
2190 if (Reloc == info->S.relocations().end())
2193 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2194 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2195 uint32_t r_type = info->O->getAnyRelocationType(RE);
2196 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2197 DataRefImpl RelNext = Rel;
2198 info->O->moveRelocationNext(RelNext);
2199 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2201 value = info->O->getPlainRelocationSymbolNum(RENext);
2202 op_info->Value = value;
2205 // NOTE: Scattered relocations don't exist on arm64.
2206 if (!info->O->getPlainRelocationExternal(RE))
2208 Expected<StringRef> SymName = Reloc->getSymbol()->getName();
2210 report_error(info->O->getFileName(), SymName.takeError());
2211 const char *name = SymName->data();
2212 op_info->AddSymbol.Present = 1;
2213 op_info->AddSymbol.Name = name;
2216 case MachO::ARM64_RELOC_PAGE21:
2218 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2220 case MachO::ARM64_RELOC_PAGEOFF12:
2222 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2224 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2226 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2228 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2230 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2232 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2233 /* @tvlppage is not implemented in llvm-mc */
2234 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2236 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2237 /* @tvlppageoff is not implemented in llvm-mc */
2238 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2241 case MachO::ARM64_RELOC_BRANCH26:
2242 op_info->VariantKind = LLVMDisassembler_VariantKind_None;
2250 // GuessCstringPointer is passed the address of what might be a pointer to a
2251 // literal string in a cstring section. If that address is in a cstring section
2252 // it returns a pointer to that string. Else it returns nullptr.
2253 static const char *GuessCstringPointer(uint64_t ReferenceValue,
2254 struct DisassembleInfo *info) {
2255 for (const auto &Load : info->O->load_commands()) {
2256 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2257 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2258 for (unsigned J = 0; J < Seg.nsects; ++J) {
2259 MachO::section_64 Sec = info->O->getSection64(Load, J);
2260 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2261 if (section_type == MachO::S_CSTRING_LITERALS &&
2262 ReferenceValue >= Sec.addr &&
2263 ReferenceValue < Sec.addr + Sec.size) {
2264 uint64_t sect_offset = ReferenceValue - Sec.addr;
2265 uint64_t object_offset = Sec.offset + sect_offset;
2266 StringRef MachOContents = info->O->getData();
2267 uint64_t object_size = MachOContents.size();
2268 const char *object_addr = (const char *)MachOContents.data();
2269 if (object_offset < object_size) {
2270 const char *name = object_addr + object_offset;
2277 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2278 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2279 for (unsigned J = 0; J < Seg.nsects; ++J) {
2280 MachO::section Sec = info->O->getSection(Load, J);
2281 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2282 if (section_type == MachO::S_CSTRING_LITERALS &&
2283 ReferenceValue >= Sec.addr &&
2284 ReferenceValue < Sec.addr + Sec.size) {
2285 uint64_t sect_offset = ReferenceValue - Sec.addr;
2286 uint64_t object_offset = Sec.offset + sect_offset;
2287 StringRef MachOContents = info->O->getData();
2288 uint64_t object_size = MachOContents.size();
2289 const char *object_addr = (const char *)MachOContents.data();
2290 if (object_offset < object_size) {
2291 const char *name = object_addr + object_offset;
2303 // GuessIndirectSymbol returns the name of the indirect symbol for the
2304 // ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
2305 // an address of a symbol stub or a lazy or non-lazy pointer to associate the
2306 // symbol name being referenced by the stub or pointer.
2307 static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2308 struct DisassembleInfo *info) {
2309 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2310 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
2311 for (const auto &Load : info->O->load_commands()) {
2312 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2313 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2314 for (unsigned J = 0; J < Seg.nsects; ++J) {
2315 MachO::section_64 Sec = info->O->getSection64(Load, J);
2316 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2317 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2318 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2319 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2320 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2321 section_type == MachO::S_SYMBOL_STUBS) &&
2322 ReferenceValue >= Sec.addr &&
2323 ReferenceValue < Sec.addr + Sec.size) {
2325 if (section_type == MachO::S_SYMBOL_STUBS)
2326 stride = Sec.reserved2;
2331 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2332 if (index < Dysymtab.nindirectsyms) {
2333 uint32_t indirect_symbol =
2334 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
2335 if (indirect_symbol < Symtab.nsyms) {
2336 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2337 SymbolRef Symbol = *Sym;
2338 Expected<StringRef> SymName = Symbol.getName();
2340 report_error(info->O->getFileName(), SymName.takeError());
2341 const char *name = SymName->data();
2347 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2348 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2349 for (unsigned J = 0; J < Seg.nsects; ++J) {
2350 MachO::section Sec = info->O->getSection(Load, J);
2351 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2352 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2353 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2354 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2355 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2356 section_type == MachO::S_SYMBOL_STUBS) &&
2357 ReferenceValue >= Sec.addr &&
2358 ReferenceValue < Sec.addr + Sec.size) {
2360 if (section_type == MachO::S_SYMBOL_STUBS)
2361 stride = Sec.reserved2;
2366 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2367 if (index < Dysymtab.nindirectsyms) {
2368 uint32_t indirect_symbol =
2369 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
2370 if (indirect_symbol < Symtab.nsyms) {
2371 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2372 SymbolRef Symbol = *Sym;
2373 Expected<StringRef> SymName = Symbol.getName();
2375 report_error(info->O->getFileName(), SymName.takeError());
2376 const char *name = SymName->data();
2387 // method_reference() is called passing it the ReferenceName that might be
2388 // a reference it to an Objective-C method call. If so then it allocates and
2389 // assembles a method call string with the values last seen and saved in
2390 // the DisassembleInfo's class_name and selector_name fields. This is saved
2391 // into the method field of the info and any previous string is free'ed.
2392 // Then the class_name field in the info is set to nullptr. The method call
2393 // string is set into ReferenceName and ReferenceType is set to
2394 // LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
2395 // then both ReferenceType and ReferenceName are left unchanged.
2396 static void method_reference(struct DisassembleInfo *info,
2397 uint64_t *ReferenceType,
2398 const char **ReferenceName) {
2399 unsigned int Arch = info->O->getArch();
2400 if (*ReferenceName != nullptr) {
2401 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
2402 if (info->selector_name != nullptr) {
2403 if (info->method != nullptr)
2405 if (info->class_name != nullptr) {
2406 info->method = (char *)malloc(5 + strlen(info->class_name) +
2407 strlen(info->selector_name));
2408 if (info->method != nullptr) {
2409 strcpy(info->method, "+[");
2410 strcat(info->method, info->class_name);
2411 strcat(info->method, " ");
2412 strcat(info->method, info->selector_name);
2413 strcat(info->method, "]");
2414 *ReferenceName = info->method;
2415 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2418 info->method = (char *)malloc(9 + strlen(info->selector_name));
2419 if (info->method != nullptr) {
2420 if (Arch == Triple::x86_64)
2421 strcpy(info->method, "-[%rdi ");
2422 else if (Arch == Triple::aarch64)
2423 strcpy(info->method, "-[x0 ");
2425 strcpy(info->method, "-[r? ");
2426 strcat(info->method, info->selector_name);
2427 strcat(info->method, "]");
2428 *ReferenceName = info->method;
2429 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2432 info->class_name = nullptr;
2434 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
2435 if (info->selector_name != nullptr) {
2436 if (info->method != nullptr)
2438 info->method = (char *)malloc(17 + strlen(info->selector_name));
2439 if (info->method != nullptr) {
2440 if (Arch == Triple::x86_64)
2441 strcpy(info->method, "-[[%rdi super] ");
2442 else if (Arch == Triple::aarch64)
2443 strcpy(info->method, "-[[x0 super] ");
2445 strcpy(info->method, "-[[r? super] ");
2446 strcat(info->method, info->selector_name);
2447 strcat(info->method, "]");
2448 *ReferenceName = info->method;
2449 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2451 info->class_name = nullptr;
2457 // GuessPointerPointer() is passed the address of what might be a pointer to
2458 // a reference to an Objective-C class, selector, message ref or cfstring.
2459 // If so the value of the pointer is returned and one of the booleans are set
2460 // to true. If not zero is returned and all the booleans are set to false.
2461 static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
2462 struct DisassembleInfo *info,
2463 bool &classref, bool &selref, bool &msgref,
2469 for (const auto &Load : info->O->load_commands()) {
2470 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2471 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2472 for (unsigned J = 0; J < Seg.nsects; ++J) {
2473 MachO::section_64 Sec = info->O->getSection64(Load, J);
2474 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
2475 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2476 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
2477 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
2478 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
2479 ReferenceValue >= Sec.addr &&
2480 ReferenceValue < Sec.addr + Sec.size) {
2481 uint64_t sect_offset = ReferenceValue - Sec.addr;
2482 uint64_t object_offset = Sec.offset + sect_offset;
2483 StringRef MachOContents = info->O->getData();
2484 uint64_t object_size = MachOContents.size();
2485 const char *object_addr = (const char *)MachOContents.data();
2486 if (object_offset < object_size) {
2487 uint64_t pointer_value;
2488 memcpy(&pointer_value, object_addr + object_offset,
2490 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2491 sys::swapByteOrder(pointer_value);
2492 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
2494 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2495 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
2497 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
2498 ReferenceValue + 8 < Sec.addr + Sec.size) {
2500 memcpy(&pointer_value, object_addr + object_offset + 8,
2502 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2503 sys::swapByteOrder(pointer_value);
2504 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
2506 return pointer_value;
2513 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
2518 // get_pointer_64 returns a pointer to the bytes in the object file at the
2519 // Address from a section in the Mach-O file. And indirectly returns the
2520 // offset into the section, number of bytes left in the section past the offset
2521 // and which section is was being referenced. If the Address is not in a
2522 // section nullptr is returned.
2523 static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
2524 uint32_t &left, SectionRef &S,
2525 DisassembleInfo *info,
2526 bool objc_only = false) {
2530 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
2531 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
2532 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
2537 ((*(info->Sections))[SectIdx]).getName(SectName);
2538 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
2539 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
2540 if (SegName != "__OBJC" && SectName != "__cstring")
2543 if (Address >= SectAddress && Address < SectAddress + SectSize) {
2544 S = (*(info->Sections))[SectIdx];
2545 offset = Address - SectAddress;
2546 left = SectSize - offset;
2547 StringRef SectContents;
2548 ((*(info->Sections))[SectIdx]).getContents(SectContents);
2549 return SectContents.data() + offset;
2555 static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
2556 uint32_t &left, SectionRef &S,
2557 DisassembleInfo *info,
2558 bool objc_only = false) {
2559 return get_pointer_64(Address, offset, left, S, info, objc_only);
2562 // get_symbol_64() returns the name of a symbol (or nullptr) and the address of
2563 // the symbol indirectly through n_value. Based on the relocation information
2564 // for the specified section offset in the specified section reference.
2565 // If no relocation information is found and a non-zero ReferenceValue for the
2566 // symbol is passed, look up that address in the info's AddrMap.
2567 static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
2568 DisassembleInfo *info, uint64_t &n_value,
2569 uint64_t ReferenceValue = 0) {
2574 // See if there is an external relocation entry at the sect_offset.
2575 bool reloc_found = false;
2577 MachO::any_relocation_info RE;
2578 bool isExtern = false;
2580 for (const RelocationRef &Reloc : S.relocations()) {
2581 uint64_t RelocOffset = Reloc.getOffset();
2582 if (RelocOffset == sect_offset) {
2583 Rel = Reloc.getRawDataRefImpl();
2584 RE = info->O->getRelocation(Rel);
2585 if (info->O->isRelocationScattered(RE))
2587 isExtern = info->O->getPlainRelocationExternal(RE);
2589 symbol_iterator RelocSym = Reloc.getSymbol();
2596 // If there is an external relocation entry for a symbol in this section
2597 // at this section_offset then use that symbol's value for the n_value
2598 // and return its name.
2599 const char *SymbolName = nullptr;
2600 if (reloc_found && isExtern) {
2601 n_value = Symbol.getValue();
2602 Expected<StringRef> NameOrError = Symbol.getName();
2604 report_error(info->O->getFileName(), NameOrError.takeError());
2605 StringRef Name = *NameOrError;
2606 if (!Name.empty()) {
2607 SymbolName = Name.data();
2612 // TODO: For fully linked images, look through the external relocation
2613 // entries off the dynamic symtab command. For these the r_offset is from the
2614 // start of the first writeable segment in the Mach-O file. So the offset
2615 // to this section from that segment is passed to this routine by the caller,
2616 // as the database_offset. Which is the difference of the section's starting
2617 // address and the first writable segment.
2619 // NOTE: need add passing the database_offset to this routine.
2621 // We did not find an external relocation entry so look up the ReferenceValue
2622 // as an address of a symbol and if found return that symbol's name.
2623 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
2628 static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
2629 DisassembleInfo *info,
2630 uint32_t ReferenceValue) {
2632 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
2635 // These are structs in the Objective-C meta data and read to produce the
2636 // comments for disassembly. While these are part of the ABI they are no
2637 // public defintions. So the are here not in include/llvm/BinaryFormat/MachO.h
2640 // The cfstring object in a 64-bit Mach-O file.
2641 struct cfstring64_t {
2642 uint64_t isa; // class64_t * (64-bit pointer)
2643 uint64_t flags; // flag bits
2644 uint64_t characters; // char * (64-bit pointer)
2645 uint64_t length; // number of non-NULL characters in above
2648 // The class object in a 64-bit Mach-O file.
2650 uint64_t isa; // class64_t * (64-bit pointer)
2651 uint64_t superclass; // class64_t * (64-bit pointer)
2652 uint64_t cache; // Cache (64-bit pointer)
2653 uint64_t vtable; // IMP * (64-bit pointer)
2654 uint64_t data; // class_ro64_t * (64-bit pointer)
2658 uint32_t isa; /* class32_t * (32-bit pointer) */
2659 uint32_t superclass; /* class32_t * (32-bit pointer) */
2660 uint32_t cache; /* Cache (32-bit pointer) */
2661 uint32_t vtable; /* IMP * (32-bit pointer) */
2662 uint32_t data; /* class_ro32_t * (32-bit pointer) */
2665 struct class_ro64_t {
2667 uint32_t instanceStart;
2668 uint32_t instanceSize;
2670 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
2671 uint64_t name; // const char * (64-bit pointer)
2672 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
2673 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
2674 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
2675 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
2676 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
2679 struct class_ro32_t {
2681 uint32_t instanceStart;
2682 uint32_t instanceSize;
2683 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
2684 uint32_t name; /* const char * (32-bit pointer) */
2685 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
2686 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
2687 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
2688 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
2689 uint32_t baseProperties; /* const struct objc_property_list *
2693 /* Values for class_ro{64,32}_t->flags */
2694 #define RO_META (1 << 0)
2695 #define RO_ROOT (1 << 1)
2696 #define RO_HAS_CXX_STRUCTORS (1 << 2)
2698 struct method_list64_t {
2701 /* struct method64_t first; These structures follow inline */
2704 struct method_list32_t {
2707 /* struct method32_t first; These structures follow inline */
2711 uint64_t name; /* SEL (64-bit pointer) */
2712 uint64_t types; /* const char * (64-bit pointer) */
2713 uint64_t imp; /* IMP (64-bit pointer) */
2717 uint32_t name; /* SEL (32-bit pointer) */
2718 uint32_t types; /* const char * (32-bit pointer) */
2719 uint32_t imp; /* IMP (32-bit pointer) */
2722 struct protocol_list64_t {
2723 uint64_t count; /* uintptr_t (a 64-bit value) */
2724 /* struct protocol64_t * list[0]; These pointers follow inline */
2727 struct protocol_list32_t {
2728 uint32_t count; /* uintptr_t (a 32-bit value) */
2729 /* struct protocol32_t * list[0]; These pointers follow inline */
2732 struct protocol64_t {
2733 uint64_t isa; /* id * (64-bit pointer) */
2734 uint64_t name; /* const char * (64-bit pointer) */
2735 uint64_t protocols; /* struct protocol_list64_t *
2737 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
2738 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
2739 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
2740 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
2741 uint64_t instanceProperties; /* struct objc_property_list *
2745 struct protocol32_t {
2746 uint32_t isa; /* id * (32-bit pointer) */
2747 uint32_t name; /* const char * (32-bit pointer) */
2748 uint32_t protocols; /* struct protocol_list_t *
2750 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
2751 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
2752 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
2753 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
2754 uint32_t instanceProperties; /* struct objc_property_list *
2758 struct ivar_list64_t {
2761 /* struct ivar64_t first; These structures follow inline */
2764 struct ivar_list32_t {
2767 /* struct ivar32_t first; These structures follow inline */
2771 uint64_t offset; /* uintptr_t * (64-bit pointer) */
2772 uint64_t name; /* const char * (64-bit pointer) */
2773 uint64_t type; /* const char * (64-bit pointer) */
2779 uint32_t offset; /* uintptr_t * (32-bit pointer) */
2780 uint32_t name; /* const char * (32-bit pointer) */
2781 uint32_t type; /* const char * (32-bit pointer) */
2786 struct objc_property_list64 {
2789 /* struct objc_property64 first; These structures follow inline */
2792 struct objc_property_list32 {
2795 /* struct objc_property32 first; These structures follow inline */
2798 struct objc_property64 {
2799 uint64_t name; /* const char * (64-bit pointer) */
2800 uint64_t attributes; /* const char * (64-bit pointer) */
2803 struct objc_property32 {
2804 uint32_t name; /* const char * (32-bit pointer) */
2805 uint32_t attributes; /* const char * (32-bit pointer) */
2808 struct category64_t {
2809 uint64_t name; /* const char * (64-bit pointer) */
2810 uint64_t cls; /* struct class_t * (64-bit pointer) */
2811 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
2812 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
2813 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
2814 uint64_t instanceProperties; /* struct objc_property_list *
2818 struct category32_t {
2819 uint32_t name; /* const char * (32-bit pointer) */
2820 uint32_t cls; /* struct class_t * (32-bit pointer) */
2821 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
2822 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
2823 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
2824 uint32_t instanceProperties; /* struct objc_property_list *
2828 struct objc_image_info64 {
2832 struct objc_image_info32 {
2836 struct imageInfo_t {
2840 /* masks for objc_image_info.flags */
2841 #define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
2842 #define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
2844 struct message_ref64 {
2845 uint64_t imp; /* IMP (64-bit pointer) */
2846 uint64_t sel; /* SEL (64-bit pointer) */
2849 struct message_ref32 {
2850 uint32_t imp; /* IMP (32-bit pointer) */
2851 uint32_t sel; /* SEL (32-bit pointer) */
2854 // Objective-C 1 (32-bit only) meta data structs.
2856 struct objc_module_t {
2859 uint32_t name; /* char * (32-bit pointer) */
2860 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
2863 struct objc_symtab_t {
2864 uint32_t sel_ref_cnt;
2865 uint32_t refs; /* SEL * (32-bit pointer) */
2866 uint16_t cls_def_cnt;
2867 uint16_t cat_def_cnt;
2868 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
2871 struct objc_class_t {
2872 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2873 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
2874 uint32_t name; /* const char * (32-bit pointer) */
2877 int32_t instance_size;
2878 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
2879 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
2880 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
2881 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
2884 #define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
2885 // class is not a metaclass
2886 #define CLS_CLASS 0x1
2887 // class is a metaclass
2888 #define CLS_META 0x2
2890 struct objc_category_t {
2891 uint32_t category_name; /* char * (32-bit pointer) */
2892 uint32_t class_name; /* char * (32-bit pointer) */
2893 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
2894 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
2895 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
2898 struct objc_ivar_t {
2899 uint32_t ivar_name; /* char * (32-bit pointer) */
2900 uint32_t ivar_type; /* char * (32-bit pointer) */
2901 int32_t ivar_offset;
2904 struct objc_ivar_list_t {
2906 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
2909 struct objc_method_list_t {
2910 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
2911 int32_t method_count;
2912 // struct objc_method_t method_list[1]; /* variable length structure */
2915 struct objc_method_t {
2916 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2917 uint32_t method_types; /* char * (32-bit pointer) */
2918 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
2922 struct objc_protocol_list_t {
2923 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
2925 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
2926 // (32-bit pointer) */
2929 struct objc_protocol_t {
2930 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2931 uint32_t protocol_name; /* char * (32-bit pointer) */
2932 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
2933 uint32_t instance_methods; /* struct objc_method_description_list *
2935 uint32_t class_methods; /* struct objc_method_description_list *
2939 struct objc_method_description_list_t {
2941 // struct objc_method_description_t list[1];
2944 struct objc_method_description_t {
2945 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2946 uint32_t types; /* char * (32-bit pointer) */
2949 inline void swapStruct(struct cfstring64_t &cfs) {
2950 sys::swapByteOrder(cfs.isa);
2951 sys::swapByteOrder(cfs.flags);
2952 sys::swapByteOrder(cfs.characters);
2953 sys::swapByteOrder(cfs.length);
2956 inline void swapStruct(struct class64_t &c) {
2957 sys::swapByteOrder(c.isa);
2958 sys::swapByteOrder(c.superclass);
2959 sys::swapByteOrder(c.cache);
2960 sys::swapByteOrder(c.vtable);
2961 sys::swapByteOrder(c.data);
2964 inline void swapStruct(struct class32_t &c) {
2965 sys::swapByteOrder(c.isa);
2966 sys::swapByteOrder(c.superclass);
2967 sys::swapByteOrder(c.cache);
2968 sys::swapByteOrder(c.vtable);
2969 sys::swapByteOrder(c.data);
2972 inline void swapStruct(struct class_ro64_t &cro) {
2973 sys::swapByteOrder(cro.flags);
2974 sys::swapByteOrder(cro.instanceStart);
2975 sys::swapByteOrder(cro.instanceSize);
2976 sys::swapByteOrder(cro.reserved);
2977 sys::swapByteOrder(cro.ivarLayout);
2978 sys::swapByteOrder(cro.name);
2979 sys::swapByteOrder(cro.baseMethods);
2980 sys::swapByteOrder(cro.baseProtocols);
2981 sys::swapByteOrder(cro.ivars);
2982 sys::swapByteOrder(cro.weakIvarLayout);
2983 sys::swapByteOrder(cro.baseProperties);
2986 inline void swapStruct(struct class_ro32_t &cro) {
2987 sys::swapByteOrder(cro.flags);
2988 sys::swapByteOrder(cro.instanceStart);
2989 sys::swapByteOrder(cro.instanceSize);
2990 sys::swapByteOrder(cro.ivarLayout);
2991 sys::swapByteOrder(cro.name);
2992 sys::swapByteOrder(cro.baseMethods);
2993 sys::swapByteOrder(cro.baseProtocols);
2994 sys::swapByteOrder(cro.ivars);
2995 sys::swapByteOrder(cro.weakIvarLayout);
2996 sys::swapByteOrder(cro.baseProperties);
2999 inline void swapStruct(struct method_list64_t &ml) {
3000 sys::swapByteOrder(ml.entsize);
3001 sys::swapByteOrder(ml.count);
3004 inline void swapStruct(struct method_list32_t &ml) {
3005 sys::swapByteOrder(ml.entsize);
3006 sys::swapByteOrder(ml.count);
3009 inline void swapStruct(struct method64_t &m) {
3010 sys::swapByteOrder(m.name);
3011 sys::swapByteOrder(m.types);
3012 sys::swapByteOrder(m.imp);
3015 inline void swapStruct(struct method32_t &m) {
3016 sys::swapByteOrder(m.name);
3017 sys::swapByteOrder(m.types);
3018 sys::swapByteOrder(m.imp);
3021 inline void swapStruct(struct protocol_list64_t &pl) {
3022 sys::swapByteOrder(pl.count);
3025 inline void swapStruct(struct protocol_list32_t &pl) {
3026 sys::swapByteOrder(pl.count);
3029 inline void swapStruct(struct protocol64_t &p) {
3030 sys::swapByteOrder(p.isa);
3031 sys::swapByteOrder(p.name);
3032 sys::swapByteOrder(p.protocols);
3033 sys::swapByteOrder(p.instanceMethods);
3034 sys::swapByteOrder(p.classMethods);
3035 sys::swapByteOrder(p.optionalInstanceMethods);
3036 sys::swapByteOrder(p.optionalClassMethods);
3037 sys::swapByteOrder(p.instanceProperties);
3040 inline void swapStruct(struct protocol32_t &p) {
3041 sys::swapByteOrder(p.isa);
3042 sys::swapByteOrder(p.name);
3043 sys::swapByteOrder(p.protocols);
3044 sys::swapByteOrder(p.instanceMethods);
3045 sys::swapByteOrder(p.classMethods);
3046 sys::swapByteOrder(p.optionalInstanceMethods);
3047 sys::swapByteOrder(p.optionalClassMethods);
3048 sys::swapByteOrder(p.instanceProperties);
3051 inline void swapStruct(struct ivar_list64_t &il) {
3052 sys::swapByteOrder(il.entsize);
3053 sys::swapByteOrder(il.count);
3056 inline void swapStruct(struct ivar_list32_t &il) {
3057 sys::swapByteOrder(il.entsize);
3058 sys::swapByteOrder(il.count);
3061 inline void swapStruct(struct ivar64_t &i) {
3062 sys::swapByteOrder(i.offset);
3063 sys::swapByteOrder(i.name);
3064 sys::swapByteOrder(i.type);
3065 sys::swapByteOrder(i.alignment);
3066 sys::swapByteOrder(i.size);
3069 inline void swapStruct(struct ivar32_t &i) {
3070 sys::swapByteOrder(i.offset);
3071 sys::swapByteOrder(i.name);
3072 sys::swapByteOrder(i.type);
3073 sys::swapByteOrder(i.alignment);
3074 sys::swapByteOrder(i.size);
3077 inline void swapStruct(struct objc_property_list64 &pl) {
3078 sys::swapByteOrder(pl.entsize);
3079 sys::swapByteOrder(pl.count);
3082 inline void swapStruct(struct objc_property_list32 &pl) {
3083 sys::swapByteOrder(pl.entsize);
3084 sys::swapByteOrder(pl.count);
3087 inline void swapStruct(struct objc_property64 &op) {
3088 sys::swapByteOrder(op.name);
3089 sys::swapByteOrder(op.attributes);
3092 inline void swapStruct(struct objc_property32 &op) {
3093 sys::swapByteOrder(op.name);
3094 sys::swapByteOrder(op.attributes);
3097 inline void swapStruct(struct category64_t &c) {
3098 sys::swapByteOrder(c.name);
3099 sys::swapByteOrder(c.cls);
3100 sys::swapByteOrder(c.instanceMethods);
3101 sys::swapByteOrder(c.classMethods);
3102 sys::swapByteOrder(c.protocols);
3103 sys::swapByteOrder(c.instanceProperties);
3106 inline void swapStruct(struct category32_t &c) {
3107 sys::swapByteOrder(c.name);
3108 sys::swapByteOrder(c.cls);
3109 sys::swapByteOrder(c.instanceMethods);
3110 sys::swapByteOrder(c.classMethods);
3111 sys::swapByteOrder(c.protocols);
3112 sys::swapByteOrder(c.instanceProperties);
3115 inline void swapStruct(struct objc_image_info64 &o) {
3116 sys::swapByteOrder(o.version);
3117 sys::swapByteOrder(o.flags);
3120 inline void swapStruct(struct objc_image_info32 &o) {
3121 sys::swapByteOrder(o.version);
3122 sys::swapByteOrder(o.flags);
3125 inline void swapStruct(struct imageInfo_t &o) {
3126 sys::swapByteOrder(o.version);
3127 sys::swapByteOrder(o.flags);
3130 inline void swapStruct(struct message_ref64 &mr) {
3131 sys::swapByteOrder(mr.imp);
3132 sys::swapByteOrder(mr.sel);
3135 inline void swapStruct(struct message_ref32 &mr) {
3136 sys::swapByteOrder(mr.imp);
3137 sys::swapByteOrder(mr.sel);
3140 inline void swapStruct(struct objc_module_t &module) {
3141 sys::swapByteOrder(module.version);
3142 sys::swapByteOrder(module.size);
3143 sys::swapByteOrder(module.name);
3144 sys::swapByteOrder(module.symtab);
3147 inline void swapStruct(struct objc_symtab_t &symtab) {
3148 sys::swapByteOrder(symtab.sel_ref_cnt);
3149 sys::swapByteOrder(symtab.refs);
3150 sys::swapByteOrder(symtab.cls_def_cnt);
3151 sys::swapByteOrder(symtab.cat_def_cnt);
3154 inline void swapStruct(struct objc_class_t &objc_class) {
3155 sys::swapByteOrder(objc_class.isa);
3156 sys::swapByteOrder(objc_class.super_class);
3157 sys::swapByteOrder(objc_class.name);
3158 sys::swapByteOrder(objc_class.version);
3159 sys::swapByteOrder(objc_class.info);
3160 sys::swapByteOrder(objc_class.instance_size);
3161 sys::swapByteOrder(objc_class.ivars);
3162 sys::swapByteOrder(objc_class.methodLists);
3163 sys::swapByteOrder(objc_class.cache);
3164 sys::swapByteOrder(objc_class.protocols);
3167 inline void swapStruct(struct objc_category_t &objc_category) {
3168 sys::swapByteOrder(objc_category.category_name);
3169 sys::swapByteOrder(objc_category.class_name);
3170 sys::swapByteOrder(objc_category.instance_methods);
3171 sys::swapByteOrder(objc_category.class_methods);
3172 sys::swapByteOrder(objc_category.protocols);
3175 inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3176 sys::swapByteOrder(objc_ivar_list.ivar_count);
3179 inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3180 sys::swapByteOrder(objc_ivar.ivar_name);
3181 sys::swapByteOrder(objc_ivar.ivar_type);
3182 sys::swapByteOrder(objc_ivar.ivar_offset);
3185 inline void swapStruct(struct objc_method_list_t &method_list) {
3186 sys::swapByteOrder(method_list.obsolete);
3187 sys::swapByteOrder(method_list.method_count);
3190 inline void swapStruct(struct objc_method_t &method) {
3191 sys::swapByteOrder(method.method_name);
3192 sys::swapByteOrder(method.method_types);
3193 sys::swapByteOrder(method.method_imp);
3196 inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3197 sys::swapByteOrder(protocol_list.next);
3198 sys::swapByteOrder(protocol_list.count);
3201 inline void swapStruct(struct objc_protocol_t &protocol) {
3202 sys::swapByteOrder(protocol.isa);
3203 sys::swapByteOrder(protocol.protocol_name);
3204 sys::swapByteOrder(protocol.protocol_list);
3205 sys::swapByteOrder(protocol.instance_methods);
3206 sys::swapByteOrder(protocol.class_methods);
3209 inline void swapStruct(struct objc_method_description_list_t &mdl) {
3210 sys::swapByteOrder(mdl.count);
3213 inline void swapStruct(struct objc_method_description_t &md) {
3214 sys::swapByteOrder(md.name);
3215 sys::swapByteOrder(md.types);
3218 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3219 struct DisassembleInfo *info);
3221 // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3222 // to an Objective-C class and returns the class name. It is also passed the
3223 // address of the pointer, so when the pointer is zero as it can be in an .o
3224 // file, that is used to look for an external relocation entry with a symbol
3226 static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3227 uint64_t ReferenceValue,
3228 struct DisassembleInfo *info) {
3230 uint32_t offset, left;
3233 // The pointer_value can be 0 in an object file and have a relocation
3234 // entry for the class symbol at the ReferenceValue (the address of the
3236 if (pointer_value == 0) {
3237 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3238 if (r == nullptr || left < sizeof(uint64_t))
3241 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3242 if (symbol_name == nullptr)
3244 const char *class_name = strrchr(symbol_name, '$');
3245 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3246 return class_name + 2;
3251 // The case were the pointer_value is non-zero and points to a class defined
3252 // in this Mach-O file.
3253 r = get_pointer_64(pointer_value, offset, left, S, info);
3254 if (r == nullptr || left < sizeof(struct class64_t))
3257 memcpy(&c, r, sizeof(struct class64_t));
3258 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3262 r = get_pointer_64(c.data, offset, left, S, info);
3263 if (r == nullptr || left < sizeof(struct class_ro64_t))
3265 struct class_ro64_t cro;
3266 memcpy(&cro, r, sizeof(struct class_ro64_t));
3267 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3271 const char *name = get_pointer_64(cro.name, offset, left, S, info);
3275 // get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3276 // pointer to a cfstring and returns its name or nullptr.
3277 static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3278 struct DisassembleInfo *info) {
3279 const char *r, *name;
3280 uint32_t offset, left;
3282 struct cfstring64_t cfs;
3283 uint64_t cfs_characters;
3285 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3286 if (r == nullptr || left < sizeof(struct cfstring64_t))
3288 memcpy(&cfs, r, sizeof(struct cfstring64_t));
3289 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3291 if (cfs.characters == 0) {
3293 const char *symbol_name = get_symbol_64(
3294 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
3295 if (symbol_name == nullptr)
3297 cfs_characters = n_value;
3299 cfs_characters = cfs.characters;
3300 name = get_pointer_64(cfs_characters, offset, left, S, info);
3305 // get_objc2_64bit_selref() is used for disassembly and is passed a the address
3306 // of a pointer to an Objective-C selector reference when the pointer value is
3307 // zero as in a .o file and is likely to have a external relocation entry with
3308 // who's symbol's n_value is the real pointer to the selector name. If that is
3309 // the case the real pointer to the selector name is returned else 0 is
3311 static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3312 struct DisassembleInfo *info) {
3313 uint32_t offset, left;
3316 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3317 if (r == nullptr || left < sizeof(uint64_t))
3320 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3321 if (symbol_name == nullptr)
3326 static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3327 const char *sectname) {
3328 for (const SectionRef &Section : O->sections()) {
3330 Section.getName(SectName);
3331 DataRefImpl Ref = Section.getRawDataRefImpl();
3332 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3333 if (SegName == segname && SectName == sectname)
3336 return SectionRef();
3340 walk_pointer_list_64(const char *listname, const SectionRef S,
3341 MachOObjectFile *O, struct DisassembleInfo *info,
3342 void (*func)(uint64_t, struct DisassembleInfo *info)) {
3343 if (S == SectionRef())
3347 S.getName(SectName);
3348 DataRefImpl Ref = S.getRawDataRefImpl();
3349 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3350 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3353 S.getContents(BytesStr);
3354 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3356 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3357 uint32_t left = S.getSize() - i;
3358 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3360 memcpy(&p, Contents + i, size);
3361 if (i + sizeof(uint64_t) > S.getSize())
3362 outs() << listname << " list pointer extends past end of (" << SegName
3363 << "," << SectName << ") section\n";
3364 outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
3366 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3367 sys::swapByteOrder(p);
3369 uint64_t n_value = 0;
3370 const char *name = get_symbol_64(i, S, info, n_value, p);
3371 if (name == nullptr)
3372 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
3375 outs() << format("0x%" PRIx64, n_value);
3377 outs() << " + " << format("0x%" PRIx64, p);
3379 outs() << format("0x%" PRIx64, p);
3380 if (name != nullptr)
3381 outs() << " " << name;
3391 walk_pointer_list_32(const char *listname, const SectionRef S,
3392 MachOObjectFile *O, struct DisassembleInfo *info,
3393 void (*func)(uint32_t, struct DisassembleInfo *info)) {
3394 if (S == SectionRef())
3398 S.getName(SectName);
3399 DataRefImpl Ref = S.getRawDataRefImpl();
3400 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3401 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3404 S.getContents(BytesStr);
3405 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3407 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
3408 uint32_t left = S.getSize() - i;
3409 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
3411 memcpy(&p, Contents + i, size);
3412 if (i + sizeof(uint32_t) > S.getSize())
3413 outs() << listname << " list pointer extends past end of (" << SegName
3414 << "," << SectName << ") section\n";
3415 uint32_t Address = S.getAddress() + i;
3416 outs() << format("%08" PRIx32, Address) << " ";
3418 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3419 sys::swapByteOrder(p);
3420 outs() << format("0x%" PRIx32, p);
3422 const char *name = get_symbol_32(i, S, info, p);
3423 if (name != nullptr)
3424 outs() << " " << name;
3432 static void print_layout_map(const char *layout_map, uint32_t left) {
3433 if (layout_map == nullptr)
3435 outs() << " layout map: ";
3437 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
3440 } while (*layout_map != '\0' && left != 0);
3444 static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
3445 uint32_t offset, left;
3447 const char *layout_map;
3451 layout_map = get_pointer_64(p, offset, left, S, info);
3452 print_layout_map(layout_map, left);
3455 static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
3456 uint32_t offset, left;
3458 const char *layout_map;
3462 layout_map = get_pointer_32(p, offset, left, S, info);
3463 print_layout_map(layout_map, left);
3466 static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
3467 const char *indent) {
3468 struct method_list64_t ml;
3469 struct method64_t m;
3471 uint32_t offset, xoffset, left, i;
3473 const char *name, *sym_name;
3476 r = get_pointer_64(p, offset, left, S, info);
3479 memset(&ml, '\0', sizeof(struct method_list64_t));
3480 if (left < sizeof(struct method_list64_t)) {
3481 memcpy(&ml, r, left);
3482 outs() << " (method_list_t entends past the end of the section)\n";
3484 memcpy(&ml, r, sizeof(struct method_list64_t));
3485 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3487 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3488 outs() << indent << "\t\t count " << ml.count << "\n";
3490 p += sizeof(struct method_list64_t);
3491 offset += sizeof(struct method_list64_t);
3492 for (i = 0; i < ml.count; i++) {
3493 r = get_pointer_64(p, offset, left, S, info);
3496 memset(&m, '\0', sizeof(struct method64_t));
3497 if (left < sizeof(struct method64_t)) {
3498 memcpy(&m, r, left);
3499 outs() << indent << " (method_t extends past the end of the section)\n";
3501 memcpy(&m, r, sizeof(struct method64_t));
3502 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3505 outs() << indent << "\t\t name ";
3506 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
3507 info, n_value, m.name);
3509 if (info->verbose && sym_name != nullptr)
3512 outs() << format("0x%" PRIx64, n_value);
3514 outs() << " + " << format("0x%" PRIx64, m.name);
3516 outs() << format("0x%" PRIx64, m.name);
3517 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
3518 if (name != nullptr)
3519 outs() << format(" %.*s", left, name);
3522 outs() << indent << "\t\t types ";
3523 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
3524 info, n_value, m.types);
3526 if (info->verbose && sym_name != nullptr)
3529 outs() << format("0x%" PRIx64, n_value);
3531 outs() << " + " << format("0x%" PRIx64, m.types);
3533 outs() << format("0x%" PRIx64, m.types);
3534 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
3535 if (name != nullptr)
3536 outs() << format(" %.*s", left, name);
3539 outs() << indent << "\t\t imp ";
3540 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
3542 if (info->verbose && name == nullptr) {
3544 outs() << format("0x%" PRIx64, n_value) << " ";
3546 outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
3548 outs() << format("0x%" PRIx64, m.imp) << " ";
3550 if (name != nullptr)
3554 p += sizeof(struct method64_t);
3555 offset += sizeof(struct method64_t);
3559 static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
3560 const char *indent) {
3561 struct method_list32_t ml;
3562 struct method32_t m;
3563 const char *r, *name;
3564 uint32_t offset, xoffset, left, i;
3567 r = get_pointer_32(p, offset, left, S, info);
3570 memset(&ml, '\0', sizeof(struct method_list32_t));
3571 if (left < sizeof(struct method_list32_t)) {
3572 memcpy(&ml, r, left);
3573 outs() << " (method_list_t entends past the end of the section)\n";
3575 memcpy(&ml, r, sizeof(struct method_list32_t));
3576 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3578 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3579 outs() << indent << "\t\t count " << ml.count << "\n";
3581 p += sizeof(struct method_list32_t);
3582 offset += sizeof(struct method_list32_t);
3583 for (i = 0; i < ml.count; i++) {
3584 r = get_pointer_32(p, offset, left, S, info);
3587 memset(&m, '\0', sizeof(struct method32_t));
3588 if (left < sizeof(struct method32_t)) {
3589 memcpy(&ml, r, left);
3590 outs() << indent << " (method_t entends past the end of the section)\n";
3592 memcpy(&m, r, sizeof(struct method32_t));
3593 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3596 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
3597 name = get_pointer_32(m.name, xoffset, left, xS, info);
3598 if (name != nullptr)
3599 outs() << format(" %.*s", left, name);
3602 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
3603 name = get_pointer_32(m.types, xoffset, left, xS, info);
3604 if (name != nullptr)
3605 outs() << format(" %.*s", left, name);
3608 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
3609 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
3611 if (name != nullptr)
3612 outs() << " " << name;
3615 p += sizeof(struct method32_t);
3616 offset += sizeof(struct method32_t);
3620 static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
3621 uint32_t offset, left, xleft;
3623 struct objc_method_list_t method_list;
3624 struct objc_method_t method;
3625 const char *r, *methods, *name, *SymbolName;
3628 r = get_pointer_32(p, offset, left, S, info, true);
3633 if (left > sizeof(struct objc_method_list_t)) {
3634 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
3636 outs() << "\t\t objc_method_list extends past end of the section\n";
3637 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
3638 memcpy(&method_list, r, left);
3640 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3641 swapStruct(method_list);
3643 outs() << "\t\t obsolete "
3644 << format("0x%08" PRIx32, method_list.obsolete) << "\n";
3645 outs() << "\t\t method_count " << method_list.method_count << "\n";
3647 methods = r + sizeof(struct objc_method_list_t);
3648 for (i = 0; i < method_list.method_count; i++) {
3649 if ((i + 1) * sizeof(struct objc_method_t) > left) {
3650 outs() << "\t\t remaining method's extend past the of the section\n";
3653 memcpy(&method, methods + i * sizeof(struct objc_method_t),
3654 sizeof(struct objc_method_t));
3655 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3658 outs() << "\t\t method_name "
3659 << format("0x%08" PRIx32, method.method_name);
3660 if (info->verbose) {
3661 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
3662 if (name != nullptr)
3663 outs() << format(" %.*s", xleft, name);
3665 outs() << " (not in an __OBJC section)";
3669 outs() << "\t\t method_types "
3670 << format("0x%08" PRIx32, method.method_types);
3671 if (info->verbose) {
3672 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
3673 if (name != nullptr)
3674 outs() << format(" %.*s", xleft, name);
3676 outs() << " (not in an __OBJC section)";
3680 outs() << "\t\t method_imp "
3681 << format("0x%08" PRIx32, method.method_imp) << " ";
3682 if (info->verbose) {
3683 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
3684 if (SymbolName != nullptr)
3685 outs() << SymbolName;
3692 static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
3693 struct protocol_list64_t pl;
3694 uint64_t q, n_value;
3695 struct protocol64_t pc;
3697 uint32_t offset, xoffset, left, i;
3699 const char *name, *sym_name;
3701 r = get_pointer_64(p, offset, left, S, info);
3704 memset(&pl, '\0', sizeof(struct protocol_list64_t));
3705 if (left < sizeof(struct protocol_list64_t)) {
3706 memcpy(&pl, r, left);
3707 outs() << " (protocol_list_t entends past the end of the section)\n";
3709 memcpy(&pl, r, sizeof(struct protocol_list64_t));
3710 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3712 outs() << " count " << pl.count << "\n";
3714 p += sizeof(struct protocol_list64_t);
3715 offset += sizeof(struct protocol_list64_t);
3716 for (i = 0; i < pl.count; i++) {
3717 r = get_pointer_64(p, offset, left, S, info);
3721 if (left < sizeof(uint64_t)) {
3722 memcpy(&q, r, left);
3723 outs() << " (protocol_t * entends past the end of the section)\n";
3725 memcpy(&q, r, sizeof(uint64_t));
3726 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3727 sys::swapByteOrder(q);
3729 outs() << "\t\t list[" << i << "] ";
3730 sym_name = get_symbol_64(offset, S, info, n_value, q);
3732 if (info->verbose && sym_name != nullptr)
3735 outs() << format("0x%" PRIx64, n_value);
3737 outs() << " + " << format("0x%" PRIx64, q);
3739 outs() << format("0x%" PRIx64, q);
3740 outs() << " (struct protocol_t *)\n";
3742 r = get_pointer_64(q + n_value, offset, left, S, info);
3745 memset(&pc, '\0', sizeof(struct protocol64_t));
3746 if (left < sizeof(struct protocol64_t)) {
3747 memcpy(&pc, r, left);
3748 outs() << " (protocol_t entends past the end of the section)\n";
3750 memcpy(&pc, r, sizeof(struct protocol64_t));
3751 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3754 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
3756 outs() << "\t\t\t name ";
3757 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
3758 info, n_value, pc.name);
3760 if (info->verbose && sym_name != nullptr)
3763 outs() << format("0x%" PRIx64, n_value);
3765 outs() << " + " << format("0x%" PRIx64, pc.name);
3767 outs() << format("0x%" PRIx64, pc.name);
3768 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
3769 if (name != nullptr)
3770 outs() << format(" %.*s", left, name);
3773 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
3775 outs() << "\t\t instanceMethods ";
3777 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
3778 S, info, n_value, pc.instanceMethods);
3780 if (info->verbose && sym_name != nullptr)
3783 outs() << format("0x%" PRIx64, n_value);
3784 if (pc.instanceMethods != 0)
3785 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
3787 outs() << format("0x%" PRIx64, pc.instanceMethods);
3788 outs() << " (struct method_list_t *)\n";
3789 if (pc.instanceMethods + n_value != 0)
3790 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
3792 outs() << "\t\t classMethods ";
3794 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
3795 info, n_value, pc.classMethods);
3797 if (info->verbose && sym_name != nullptr)
3800 outs() << format("0x%" PRIx64, n_value);
3801 if (pc.classMethods != 0)
3802 outs() << " + " << format("0x%" PRIx64, pc.classMethods);
3804 outs() << format("0x%" PRIx64, pc.classMethods);
3805 outs() << " (struct method_list_t *)\n";
3806 if (pc.classMethods + n_value != 0)
3807 print_method_list64_t(pc.classMethods + n_value, info, "\t");
3809 outs() << "\t optionalInstanceMethods "
3810 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
3811 outs() << "\t optionalClassMethods "
3812 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
3813 outs() << "\t instanceProperties "
3814 << format("0x%" PRIx64, pc.instanceProperties) << "\n";
3816 p += sizeof(uint64_t);
3817 offset += sizeof(uint64_t);
3821 static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
3822 struct protocol_list32_t pl;
3824 struct protocol32_t pc;
3826 uint32_t offset, xoffset, left, i;
3830 r = get_pointer_32(p, offset, left, S, info);
3833 memset(&pl, '\0', sizeof(struct protocol_list32_t));
3834 if (left < sizeof(struct protocol_list32_t)) {
3835 memcpy(&pl, r, left);
3836 outs() << " (protocol_list_t entends past the end of the section)\n";
3838 memcpy(&pl, r, sizeof(struct protocol_list32_t));
3839 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3841 outs() << " count " << pl.count << "\n";
3843 p += sizeof(struct protocol_list32_t);
3844 offset += sizeof(struct protocol_list32_t);
3845 for (i = 0; i < pl.count; i++) {
3846 r = get_pointer_32(p, offset, left, S, info);
3850 if (left < sizeof(uint32_t)) {
3851 memcpy(&q, r, left);
3852 outs() << " (protocol_t * entends past the end of the section)\n";
3854 memcpy(&q, r, sizeof(uint32_t));
3855 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3856 sys::swapByteOrder(q);
3857 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
3858 << " (struct protocol_t *)\n";
3859 r = get_pointer_32(q, offset, left, S, info);
3862 memset(&pc, '\0', sizeof(struct protocol32_t));
3863 if (left < sizeof(struct protocol32_t)) {
3864 memcpy(&pc, r, left);
3865 outs() << " (protocol_t entends past the end of the section)\n";
3867 memcpy(&pc, r, sizeof(struct protocol32_t));
3868 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3870 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
3871 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
3872 name = get_pointer_32(pc.name, xoffset, left, xS, info);
3873 if (name != nullptr)
3874 outs() << format(" %.*s", left, name);
3876 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
3877 outs() << "\t\t instanceMethods "
3878 << format("0x%" PRIx32, pc.instanceMethods)
3879 << " (struct method_list_t *)\n";
3880 if (pc.instanceMethods != 0)
3881 print_method_list32_t(pc.instanceMethods, info, "\t");
3882 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
3883 << " (struct method_list_t *)\n";
3884 if (pc.classMethods != 0)
3885 print_method_list32_t(pc.classMethods, info, "\t");
3886 outs() << "\t optionalInstanceMethods "
3887 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
3888 outs() << "\t optionalClassMethods "
3889 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
3890 outs() << "\t instanceProperties "
3891 << format("0x%" PRIx32, pc.instanceProperties) << "\n";
3892 p += sizeof(uint32_t);
3893 offset += sizeof(uint32_t);
3897 static void print_indent(uint32_t indent) {
3898 for (uint32_t i = 0; i < indent;) {
3899 if (indent - i >= 8) {
3903 for (uint32_t j = i; j < indent; j++)
3910 static bool print_method_description_list(uint32_t p, uint32_t indent,
3911 struct DisassembleInfo *info) {
3912 uint32_t offset, left, xleft;
3914 struct objc_method_description_list_t mdl;
3915 struct objc_method_description_t md;
3916 const char *r, *list, *name;
3919 r = get_pointer_32(p, offset, left, S, info, true);
3924 if (left > sizeof(struct objc_method_description_list_t)) {
3925 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
3927 print_indent(indent);
3928 outs() << " objc_method_description_list extends past end of the section\n";
3929 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
3930 memcpy(&mdl, r, left);
3932 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3935 print_indent(indent);
3936 outs() << " count " << mdl.count << "\n";
3938 list = r + sizeof(struct objc_method_description_list_t);
3939 for (i = 0; i < mdl.count; i++) {
3940 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
3941 print_indent(indent);
3942 outs() << " remaining list entries extend past the of the section\n";
3945 print_indent(indent);
3946 outs() << " list[" << i << "]\n";
3947 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
3948 sizeof(struct objc_method_description_t));
3949 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3952 print_indent(indent);
3953 outs() << " name " << format("0x%08" PRIx32, md.name);
3954 if (info->verbose) {
3955 name = get_pointer_32(md.name, offset, xleft, S, info, true);
3956 if (name != nullptr)
3957 outs() << format(" %.*s", xleft, name);
3959 outs() << " (not in an __OBJC section)";
3963 print_indent(indent);
3964 outs() << " types " << format("0x%08" PRIx32, md.types);
3965 if (info->verbose) {
3966 name = get_pointer_32(md.types, offset, xleft, S, info, true);
3967 if (name != nullptr)
3968 outs() << format(" %.*s", xleft, name);
3970 outs() << " (not in an __OBJC section)";
3977 static bool print_protocol_list(uint32_t p, uint32_t indent,
3978 struct DisassembleInfo *info);
3980 static bool print_protocol(uint32_t p, uint32_t indent,
3981 struct DisassembleInfo *info) {
3982 uint32_t offset, left;
3984 struct objc_protocol_t protocol;
3985 const char *r, *name;
3987 r = get_pointer_32(p, offset, left, S, info, true);
3992 if (left >= sizeof(struct objc_protocol_t)) {
3993 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
3995 print_indent(indent);
3996 outs() << " Protocol extends past end of the section\n";
3997 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
3998 memcpy(&protocol, r, left);
4000 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4001 swapStruct(protocol);
4003 print_indent(indent);
4004 outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
4007 print_indent(indent);
4008 outs() << " protocol_name "
4009 << format("0x%08" PRIx32, protocol.protocol_name);
4010 if (info->verbose) {
4011 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
4012 if (name != nullptr)
4013 outs() << format(" %.*s", left, name);
4015 outs() << " (not in an __OBJC section)";
4019 print_indent(indent);
4020 outs() << " protocol_list "
4021 << format("0x%08" PRIx32, protocol.protocol_list);
4022 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
4023 outs() << " (not in an __OBJC section)\n";
4025 print_indent(indent);
4026 outs() << " instance_methods "
4027 << format("0x%08" PRIx32, protocol.instance_methods);
4028 if (print_method_description_list(protocol.instance_methods, indent, info))
4029 outs() << " (not in an __OBJC section)\n";
4031 print_indent(indent);
4032 outs() << " class_methods "
4033 << format("0x%08" PRIx32, protocol.class_methods);
4034 if (print_method_description_list(protocol.class_methods, indent, info))
4035 outs() << " (not in an __OBJC section)\n";
4040 static bool print_protocol_list(uint32_t p, uint32_t indent,
4041 struct DisassembleInfo *info) {
4042 uint32_t offset, left, l;
4044 struct objc_protocol_list_t protocol_list;
4045 const char *r, *list;
4048 r = get_pointer_32(p, offset, left, S, info, true);
4053 if (left > sizeof(struct objc_protocol_list_t)) {
4054 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
4056 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
4057 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
4058 memcpy(&protocol_list, r, left);
4060 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4061 swapStruct(protocol_list);
4063 print_indent(indent);
4064 outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
4066 print_indent(indent);
4067 outs() << " count " << protocol_list.count << "\n";
4069 list = r + sizeof(struct objc_protocol_list_t);
4070 for (i = 0; i < protocol_list.count; i++) {
4071 if ((i + 1) * sizeof(uint32_t) > left) {
4072 outs() << "\t\t remaining list entries extend past the of the section\n";
4075 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
4076 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4077 sys::swapByteOrder(l);
4079 print_indent(indent);
4080 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
4081 if (print_protocol(l, indent, info))
4082 outs() << "(not in an __OBJC section)\n";
4087 static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
4088 struct ivar_list64_t il;
4091 uint32_t offset, xoffset, left, j;
4093 const char *name, *sym_name, *ivar_offset_p;
4094 uint64_t ivar_offset, n_value;
4096 r = get_pointer_64(p, offset, left, S, info);
4099 memset(&il, '\0', sizeof(struct ivar_list64_t));
4100 if (left < sizeof(struct ivar_list64_t)) {
4101 memcpy(&il, r, left);
4102 outs() << " (ivar_list_t entends past the end of the section)\n";
4104 memcpy(&il, r, sizeof(struct ivar_list64_t));
4105 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4107 outs() << " entsize " << il.entsize << "\n";
4108 outs() << " count " << il.count << "\n";
4110 p += sizeof(struct ivar_list64_t);
4111 offset += sizeof(struct ivar_list64_t);
4112 for (j = 0; j < il.count; j++) {
4113 r = get_pointer_64(p, offset, left, S, info);
4116 memset(&i, '\0', sizeof(struct ivar64_t));
4117 if (left < sizeof(struct ivar64_t)) {
4118 memcpy(&i, r, left);
4119 outs() << " (ivar_t entends past the end of the section)\n";
4121 memcpy(&i, r, sizeof(struct ivar64_t));
4122 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4125 outs() << "\t\t\t offset ";
4126 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
4127 info, n_value, i.offset);
4129 if (info->verbose && sym_name != nullptr)
4132 outs() << format("0x%" PRIx64, n_value);
4134 outs() << " + " << format("0x%" PRIx64, i.offset);
4136 outs() << format("0x%" PRIx64, i.offset);
4137 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
4138 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4139 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4140 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4141 sys::swapByteOrder(ivar_offset);
4142 outs() << " " << ivar_offset << "\n";
4146 outs() << "\t\t\t name ";
4147 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
4150 if (info->verbose && sym_name != nullptr)
4153 outs() << format("0x%" PRIx64, n_value);
4155 outs() << " + " << format("0x%" PRIx64, i.name);
4157 outs() << format("0x%" PRIx64, i.name);
4158 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
4159 if (name != nullptr)
4160 outs() << format(" %.*s", left, name);
4163 outs() << "\t\t\t type ";
4164 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
4166 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
4168 if (info->verbose && sym_name != nullptr)
4171 outs() << format("0x%" PRIx64, n_value);
4173 outs() << " + " << format("0x%" PRIx64, i.type);
4175 outs() << format("0x%" PRIx64, i.type);
4176 if (name != nullptr)
4177 outs() << format(" %.*s", left, name);
4180 outs() << "\t\t\talignment " << i.alignment << "\n";
4181 outs() << "\t\t\t size " << i.size << "\n";
4183 p += sizeof(struct ivar64_t);
4184 offset += sizeof(struct ivar64_t);
4188 static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
4189 struct ivar_list32_t il;
4192 uint32_t offset, xoffset, left, j;
4194 const char *name, *ivar_offset_p;
4195 uint32_t ivar_offset;
4197 r = get_pointer_32(p, offset, left, S, info);
4200 memset(&il, '\0', sizeof(struct ivar_list32_t));
4201 if (left < sizeof(struct ivar_list32_t)) {
4202 memcpy(&il, r, left);
4203 outs() << " (ivar_list_t entends past the end of the section)\n";
4205 memcpy(&il, r, sizeof(struct ivar_list32_t));
4206 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4208 outs() << " entsize " << il.entsize << "\n";
4209 outs() << " count " << il.count << "\n";
4211 p += sizeof(struct ivar_list32_t);
4212 offset += sizeof(struct ivar_list32_t);
4213 for (j = 0; j < il.count; j++) {
4214 r = get_pointer_32(p, offset, left, S, info);
4217 memset(&i, '\0', sizeof(struct ivar32_t));
4218 if (left < sizeof(struct ivar32_t)) {
4219 memcpy(&i, r, left);
4220 outs() << " (ivar_t entends past the end of the section)\n";
4222 memcpy(&i, r, sizeof(struct ivar32_t));
4223 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4226 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
4227 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4228 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4229 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4230 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4231 sys::swapByteOrder(ivar_offset);
4232 outs() << " " << ivar_offset << "\n";
4236 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
4237 name = get_pointer_32(i.name, xoffset, left, xS, info);
4238 if (name != nullptr)
4239 outs() << format(" %.*s", left, name);
4242 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
4243 name = get_pointer_32(i.type, xoffset, left, xS, info);
4244 if (name != nullptr)
4245 outs() << format(" %.*s", left, name);
4248 outs() << "\t\t\talignment " << i.alignment << "\n";
4249 outs() << "\t\t\t size " << i.size << "\n";
4251 p += sizeof(struct ivar32_t);
4252 offset += sizeof(struct ivar32_t);
4256 static void print_objc_property_list64(uint64_t p,
4257 struct DisassembleInfo *info) {
4258 struct objc_property_list64 opl;
4259 struct objc_property64 op;
4261 uint32_t offset, xoffset, left, j;
4263 const char *name, *sym_name;
4266 r = get_pointer_64(p, offset, left, S, info);
4269 memset(&opl, '\0', sizeof(struct objc_property_list64));
4270 if (left < sizeof(struct objc_property_list64)) {
4271 memcpy(&opl, r, left);
4272 outs() << " (objc_property_list entends past the end of the section)\n";
4274 memcpy(&opl, r, sizeof(struct objc_property_list64));
4275 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4277 outs() << " entsize " << opl.entsize << "\n";
4278 outs() << " count " << opl.count << "\n";
4280 p += sizeof(struct objc_property_list64);
4281 offset += sizeof(struct objc_property_list64);
4282 for (j = 0; j < opl.count; j++) {
4283 r = get_pointer_64(p, offset, left, S, info);
4286 memset(&op, '\0', sizeof(struct objc_property64));
4287 if (left < sizeof(struct objc_property64)) {
4288 memcpy(&op, r, left);
4289 outs() << " (objc_property entends past the end of the section)\n";
4291 memcpy(&op, r, sizeof(struct objc_property64));
4292 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4295 outs() << "\t\t\t name ";
4296 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
4297 info, n_value, op.name);
4299 if (info->verbose && sym_name != nullptr)
4302 outs() << format("0x%" PRIx64, n_value);
4304 outs() << " + " << format("0x%" PRIx64, op.name);
4306 outs() << format("0x%" PRIx64, op.name);
4307 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
4308 if (name != nullptr)
4309 outs() << format(" %.*s", left, name);
4312 outs() << "\t\t\tattributes ";
4314 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
4315 info, n_value, op.attributes);
4317 if (info->verbose && sym_name != nullptr)
4320 outs() << format("0x%" PRIx64, n_value);
4321 if (op.attributes != 0)
4322 outs() << " + " << format("0x%" PRIx64, op.attributes);
4324 outs() << format("0x%" PRIx64, op.attributes);
4325 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
4326 if (name != nullptr)
4327 outs() << format(" %.*s", left, name);
4330 p += sizeof(struct objc_property64);
4331 offset += sizeof(struct objc_property64);
4335 static void print_objc_property_list32(uint32_t p,
4336 struct DisassembleInfo *info) {
4337 struct objc_property_list32 opl;
4338 struct objc_property32 op;
4340 uint32_t offset, xoffset, left, j;
4344 r = get_pointer_32(p, offset, left, S, info);
4347 memset(&opl, '\0', sizeof(struct objc_property_list32));
4348 if (left < sizeof(struct objc_property_list32)) {
4349 memcpy(&opl, r, left);
4350 outs() << " (objc_property_list entends past the end of the section)\n";
4352 memcpy(&opl, r, sizeof(struct objc_property_list32));
4353 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4355 outs() << " entsize " << opl.entsize << "\n";
4356 outs() << " count " << opl.count << "\n";
4358 p += sizeof(struct objc_property_list32);
4359 offset += sizeof(struct objc_property_list32);
4360 for (j = 0; j < opl.count; j++) {
4361 r = get_pointer_32(p, offset, left, S, info);
4364 memset(&op, '\0', sizeof(struct objc_property32));
4365 if (left < sizeof(struct objc_property32)) {
4366 memcpy(&op, r, left);
4367 outs() << " (objc_property entends past the end of the section)\n";
4369 memcpy(&op, r, sizeof(struct objc_property32));
4370 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4373 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
4374 name = get_pointer_32(op.name, xoffset, left, xS, info);
4375 if (name != nullptr)
4376 outs() << format(" %.*s", left, name);
4379 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
4380 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
4381 if (name != nullptr)
4382 outs() << format(" %.*s", left, name);
4385 p += sizeof(struct objc_property32);
4386 offset += sizeof(struct objc_property32);
4390 static bool print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
4391 bool &is_meta_class) {
4392 struct class_ro64_t cro;
4394 uint32_t offset, xoffset, left;
4396 const char *name, *sym_name;
4399 r = get_pointer_64(p, offset, left, S, info);
4400 if (r == nullptr || left < sizeof(struct class_ro64_t))
4402 memcpy(&cro, r, sizeof(struct class_ro64_t));
4403 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4405 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4406 if (cro.flags & RO_META)
4407 outs() << " RO_META";
4408 if (cro.flags & RO_ROOT)
4409 outs() << " RO_ROOT";
4410 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4411 outs() << " RO_HAS_CXX_STRUCTORS";
4413 outs() << " instanceStart " << cro.instanceStart << "\n";
4414 outs() << " instanceSize " << cro.instanceSize << "\n";
4415 outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
4417 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
4419 print_layout_map64(cro.ivarLayout, info);
4422 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
4423 info, n_value, cro.name);
4425 if (info->verbose && sym_name != nullptr)
4428 outs() << format("0x%" PRIx64, n_value);
4430 outs() << " + " << format("0x%" PRIx64, cro.name);
4432 outs() << format("0x%" PRIx64, cro.name);
4433 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
4434 if (name != nullptr)
4435 outs() << format(" %.*s", left, name);
4438 outs() << " baseMethods ";
4439 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
4440 S, info, n_value, cro.baseMethods);
4442 if (info->verbose && sym_name != nullptr)
4445 outs() << format("0x%" PRIx64, n_value);
4446 if (cro.baseMethods != 0)
4447 outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
4449 outs() << format("0x%" PRIx64, cro.baseMethods);
4450 outs() << " (struct method_list_t *)\n";
4451 if (cro.baseMethods + n_value != 0)
4452 print_method_list64_t(cro.baseMethods + n_value, info, "");
4454 outs() << " baseProtocols ";
4456 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
4457 info, n_value, cro.baseProtocols);
4459 if (info->verbose && sym_name != nullptr)
4462 outs() << format("0x%" PRIx64, n_value);
4463 if (cro.baseProtocols != 0)
4464 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
4466 outs() << format("0x%" PRIx64, cro.baseProtocols);
4468 if (cro.baseProtocols + n_value != 0)
4469 print_protocol_list64_t(cro.baseProtocols + n_value, info);
4471 outs() << " ivars ";
4472 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
4473 info, n_value, cro.ivars);
4475 if (info->verbose && sym_name != nullptr)
4478 outs() << format("0x%" PRIx64, n_value);
4480 outs() << " + " << format("0x%" PRIx64, cro.ivars);
4482 outs() << format("0x%" PRIx64, cro.ivars);
4484 if (cro.ivars + n_value != 0)
4485 print_ivar_list64_t(cro.ivars + n_value, info);
4487 outs() << " weakIvarLayout ";
4489 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
4490 info, n_value, cro.weakIvarLayout);
4492 if (info->verbose && sym_name != nullptr)
4495 outs() << format("0x%" PRIx64, n_value);
4496 if (cro.weakIvarLayout != 0)
4497 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
4499 outs() << format("0x%" PRIx64, cro.weakIvarLayout);
4501 print_layout_map64(cro.weakIvarLayout + n_value, info);
4503 outs() << " baseProperties ";
4505 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
4506 info, n_value, cro.baseProperties);
4508 if (info->verbose && sym_name != nullptr)
4511 outs() << format("0x%" PRIx64, n_value);
4512 if (cro.baseProperties != 0)
4513 outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
4515 outs() << format("0x%" PRIx64, cro.baseProperties);
4517 if (cro.baseProperties + n_value != 0)
4518 print_objc_property_list64(cro.baseProperties + n_value, info);
4520 is_meta_class = (cro.flags & RO_META) != 0;
4524 static bool print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
4525 bool &is_meta_class) {
4526 struct class_ro32_t cro;
4528 uint32_t offset, xoffset, left;
4532 r = get_pointer_32(p, offset, left, S, info);
4535 memset(&cro, '\0', sizeof(struct class_ro32_t));
4536 if (left < sizeof(struct class_ro32_t)) {
4537 memcpy(&cro, r, left);
4538 outs() << " (class_ro_t entends past the end of the section)\n";
4540 memcpy(&cro, r, sizeof(struct class_ro32_t));
4541 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4543 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4544 if (cro.flags & RO_META)
4545 outs() << " RO_META";
4546 if (cro.flags & RO_ROOT)
4547 outs() << " RO_ROOT";
4548 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4549 outs() << " RO_HAS_CXX_STRUCTORS";
4551 outs() << " instanceStart " << cro.instanceStart << "\n";
4552 outs() << " instanceSize " << cro.instanceSize << "\n";
4553 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
4555 print_layout_map32(cro.ivarLayout, info);
4557 outs() << " name " << format("0x%" PRIx32, cro.name);
4558 name = get_pointer_32(cro.name, xoffset, left, xS, info);
4559 if (name != nullptr)
4560 outs() << format(" %.*s", left, name);
4563 outs() << " baseMethods "
4564 << format("0x%" PRIx32, cro.baseMethods)
4565 << " (struct method_list_t *)\n";
4566 if (cro.baseMethods != 0)
4567 print_method_list32_t(cro.baseMethods, info, "");
4569 outs() << " baseProtocols "
4570 << format("0x%" PRIx32, cro.baseProtocols) << "\n";
4571 if (cro.baseProtocols != 0)
4572 print_protocol_list32_t(cro.baseProtocols, info);
4573 outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
4576 print_ivar_list32_t(cro.ivars, info);
4577 outs() << " weakIvarLayout "
4578 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
4579 print_layout_map32(cro.weakIvarLayout, info);
4580 outs() << " baseProperties "
4581 << format("0x%" PRIx32, cro.baseProperties) << "\n";
4582 if (cro.baseProperties != 0)
4583 print_objc_property_list32(cro.baseProperties, info);
4584 is_meta_class = (cro.flags & RO_META) != 0;
4588 static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
4591 uint32_t offset, left;
4594 uint64_t isa_n_value, n_value;
4596 r = get_pointer_64(p, offset, left, S, info);
4597 if (r == nullptr || left < sizeof(struct class64_t))
4599 memcpy(&c, r, sizeof(struct class64_t));
4600 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4603 outs() << " isa " << format("0x%" PRIx64, c.isa);
4604 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
4605 isa_n_value, c.isa);
4606 if (name != nullptr)
4607 outs() << " " << name;
4610 outs() << " superclass " << format("0x%" PRIx64, c.superclass);
4611 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
4612 n_value, c.superclass);
4613 if (name != nullptr)
4614 outs() << " " << name;
4616 name = get_dyld_bind_info_symbolname(S.getAddress() +
4617 offset + offsetof(struct class64_t, superclass), info);
4618 if (name != nullptr)
4619 outs() << " " << name;
4623 outs() << " cache " << format("0x%" PRIx64, c.cache);
4624 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
4626 if (name != nullptr)
4627 outs() << " " << name;
4630 outs() << " vtable " << format("0x%" PRIx64, c.vtable);
4631 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
4633 if (name != nullptr)
4634 outs() << " " << name;
4637 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
4641 if (info->verbose && name != nullptr)
4644 outs() << format("0x%" PRIx64, n_value);
4646 outs() << " + " << format("0x%" PRIx64, c.data);
4648 outs() << format("0x%" PRIx64, c.data);
4649 outs() << " (struct class_ro_t *)";
4651 // This is a Swift class if some of the low bits of the pointer are set.
4652 if ((c.data + n_value) & 0x7)
4653 outs() << " Swift class";
4656 if (!print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class))
4659 if (!is_meta_class &&
4660 c.isa + isa_n_value != p &&
4661 c.isa + isa_n_value != 0 &&
4662 info->depth < 100) {
4664 outs() << "Meta Class\n";
4665 print_class64_t(c.isa + isa_n_value, info);
4669 static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
4672 uint32_t offset, left;
4676 r = get_pointer_32(p, offset, left, S, info);
4679 memset(&c, '\0', sizeof(struct class32_t));
4680 if (left < sizeof(struct class32_t)) {
4681 memcpy(&c, r, left);
4682 outs() << " (class_t entends past the end of the section)\n";
4684 memcpy(&c, r, sizeof(struct class32_t));
4685 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4688 outs() << " isa " << format("0x%" PRIx32, c.isa);
4690 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
4691 if (name != nullptr)
4692 outs() << " " << name;
4695 outs() << " superclass " << format("0x%" PRIx32, c.superclass);
4696 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
4698 if (name != nullptr)
4699 outs() << " " << name;
4702 outs() << " cache " << format("0x%" PRIx32, c.cache);
4703 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
4705 if (name != nullptr)
4706 outs() << " " << name;
4709 outs() << " vtable " << format("0x%" PRIx32, c.vtable);
4710 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
4712 if (name != nullptr)
4713 outs() << " " << name;
4717 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
4718 outs() << " data " << format("0x%" PRIx32, c.data)
4719 << " (struct class_ro_t *)";
4721 // This is a Swift class if some of the low bits of the pointer are set.
4723 outs() << " Swift class";
4726 if (!print_class_ro32_t(c.data & ~0x3, info, is_meta_class))
4729 if (!is_meta_class) {
4730 outs() << "Meta Class\n";
4731 print_class32_t(c.isa, info);
4735 static void print_objc_class_t(struct objc_class_t *objc_class,
4736 struct DisassembleInfo *info) {
4737 uint32_t offset, left, xleft;
4738 const char *name, *p, *ivar_list;
4741 struct objc_ivar_list_t objc_ivar_list;
4742 struct objc_ivar_t ivar;
4744 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
4745 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
4746 name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
4747 if (name != nullptr)
4748 outs() << format(" %.*s", left, name);
4750 outs() << " (not in an __OBJC section)";
4754 outs() << "\t super_class "
4755 << format("0x%08" PRIx32, objc_class->super_class);
4756 if (info->verbose) {
4757 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
4758 if (name != nullptr)
4759 outs() << format(" %.*s", left, name);
4761 outs() << " (not in an __OBJC section)";
4765 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name);
4766 if (info->verbose) {
4767 name = get_pointer_32(objc_class->name, offset, left, S, info, true);
4768 if (name != nullptr)
4769 outs() << format(" %.*s", left, name);
4771 outs() << " (not in an __OBJC section)";
4775 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
4778 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
4779 if (info->verbose) {
4780 if (CLS_GETINFO(objc_class, CLS_CLASS))
4781 outs() << " CLS_CLASS";
4782 else if (CLS_GETINFO(objc_class, CLS_META))
4783 outs() << " CLS_META";
4787 outs() << "\t instance_size "
4788 << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
4790 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
4791 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
4793 if (left > sizeof(struct objc_ivar_list_t)) {
4795 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
4797 outs() << " (entends past the end of the section)\n";
4798 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
4799 memcpy(&objc_ivar_list, p, left);
4801 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4802 swapStruct(objc_ivar_list);
4803 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
4804 ivar_list = p + sizeof(struct objc_ivar_list_t);
4805 for (i = 0; i < objc_ivar_list.ivar_count; i++) {
4806 if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
4807 outs() << "\t\t remaining ivar's extend past the of the section\n";
4810 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
4811 sizeof(struct objc_ivar_t));
4812 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4815 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
4816 if (info->verbose) {
4817 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
4818 if (name != nullptr)
4819 outs() << format(" %.*s", xleft, name);
4821 outs() << " (not in an __OBJC section)";
4825 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
4826 if (info->verbose) {
4827 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
4828 if (name != nullptr)
4829 outs() << format(" %.*s", xleft, name);
4831 outs() << " (not in an __OBJC section)";
4835 outs() << "\t\t ivar_offset "
4836 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
4839 outs() << " (not in an __OBJC section)\n";
4842 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
4843 if (print_method_list(objc_class->methodLists, info))
4844 outs() << " (not in an __OBJC section)\n";
4846 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
4849 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
4850 if (print_protocol_list(objc_class->protocols, 16, info))
4851 outs() << " (not in an __OBJC section)\n";
4854 static void print_objc_objc_category_t(struct objc_category_t *objc_category,
4855 struct DisassembleInfo *info) {
4856 uint32_t offset, left;
4860 outs() << "\t category name "
4861 << format("0x%08" PRIx32, objc_category->category_name);
4862 if (info->verbose) {
4863 name = get_pointer_32(objc_category->category_name, offset, left, S, info,
4865 if (name != nullptr)
4866 outs() << format(" %.*s", left, name);
4868 outs() << " (not in an __OBJC section)";
4872 outs() << "\t\t class name "
4873 << format("0x%08" PRIx32, objc_category->class_name);
4874 if (info->verbose) {
4876 get_pointer_32(objc_category->class_name, offset, left, S, info, true);
4877 if (name != nullptr)
4878 outs() << format(" %.*s", left, name);
4880 outs() << " (not in an __OBJC section)";
4884 outs() << "\t instance methods "
4885 << format("0x%08" PRIx32, objc_category->instance_methods);
4886 if (print_method_list(objc_category->instance_methods, info))
4887 outs() << " (not in an __OBJC section)\n";
4889 outs() << "\t class methods "
4890 << format("0x%08" PRIx32, objc_category->class_methods);
4891 if (print_method_list(objc_category->class_methods, info))
4892 outs() << " (not in an __OBJC section)\n";
4895 static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
4896 struct category64_t c;
4898 uint32_t offset, xoffset, left;
4900 const char *name, *sym_name;
4903 r = get_pointer_64(p, offset, left, S, info);
4906 memset(&c, '\0', sizeof(struct category64_t));
4907 if (left < sizeof(struct category64_t)) {
4908 memcpy(&c, r, left);
4909 outs() << " (category_t entends past the end of the section)\n";
4911 memcpy(&c, r, sizeof(struct category64_t));
4912 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4916 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
4917 info, n_value, c.name);
4919 if (info->verbose && sym_name != nullptr)
4922 outs() << format("0x%" PRIx64, n_value);
4924 outs() << " + " << format("0x%" PRIx64, c.name);
4926 outs() << format("0x%" PRIx64, c.name);
4927 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
4928 if (name != nullptr)
4929 outs() << format(" %.*s", left, name);
4933 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
4936 if (info->verbose && sym_name != nullptr)
4939 outs() << format("0x%" PRIx64, n_value);
4941 outs() << " + " << format("0x%" PRIx64, c.cls);
4943 outs() << format("0x%" PRIx64, c.cls);
4945 if (c.cls + n_value != 0)
4946 print_class64_t(c.cls + n_value, info);
4948 outs() << " instanceMethods ";
4950 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
4951 info, n_value, c.instanceMethods);
4953 if (info->verbose && sym_name != nullptr)
4956 outs() << format("0x%" PRIx64, n_value);
4957 if (c.instanceMethods != 0)
4958 outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
4960 outs() << format("0x%" PRIx64, c.instanceMethods);
4962 if (c.instanceMethods + n_value != 0)
4963 print_method_list64_t(c.instanceMethods + n_value, info, "");
4965 outs() << " classMethods ";
4966 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
4967 S, info, n_value, c.classMethods);
4969 if (info->verbose && sym_name != nullptr)
4972 outs() << format("0x%" PRIx64, n_value);
4973 if (c.classMethods != 0)
4974 outs() << " + " << format("0x%" PRIx64, c.classMethods);
4976 outs() << format("0x%" PRIx64, c.classMethods);
4978 if (c.classMethods + n_value != 0)
4979 print_method_list64_t(c.classMethods + n_value, info, "");
4981 outs() << " protocols ";
4982 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
4983 info, n_value, c.protocols);
4985 if (info->verbose && sym_name != nullptr)
4988 outs() << format("0x%" PRIx64, n_value);
4989 if (c.protocols != 0)
4990 outs() << " + " << format("0x%" PRIx64, c.protocols);
4992 outs() << format("0x%" PRIx64, c.protocols);
4994 if (c.protocols + n_value != 0)
4995 print_protocol_list64_t(c.protocols + n_value, info);
4997 outs() << "instanceProperties ";
4999 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
5000 S, info, n_value, c.instanceProperties);
5002 if (info->verbose && sym_name != nullptr)
5005 outs() << format("0x%" PRIx64, n_value);
5006 if (c.instanceProperties != 0)
5007 outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
5009 outs() << format("0x%" PRIx64, c.instanceProperties);
5011 if (c.instanceProperties + n_value != 0)
5012 print_objc_property_list64(c.instanceProperties + n_value, info);
5015 static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
5016 struct category32_t c;
5018 uint32_t offset, left;
5022 r = get_pointer_32(p, offset, left, S, info);
5025 memset(&c, '\0', sizeof(struct category32_t));
5026 if (left < sizeof(struct category32_t)) {
5027 memcpy(&c, r, left);
5028 outs() << " (category_t entends past the end of the section)\n";
5030 memcpy(&c, r, sizeof(struct category32_t));
5031 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5034 outs() << " name " << format("0x%" PRIx32, c.name);
5035 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
5038 outs() << " " << name;
5041 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
5043 print_class32_t(c.cls, info);
5044 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
5046 if (c.instanceMethods != 0)
5047 print_method_list32_t(c.instanceMethods, info, "");
5048 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
5050 if (c.classMethods != 0)
5051 print_method_list32_t(c.classMethods, info, "");
5052 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
5053 if (c.protocols != 0)
5054 print_protocol_list32_t(c.protocols, info);
5055 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
5057 if (c.instanceProperties != 0)
5058 print_objc_property_list32(c.instanceProperties, info);
5061 static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
5062 uint32_t i, left, offset, xoffset;
5063 uint64_t p, n_value;
5064 struct message_ref64 mr;
5065 const char *name, *sym_name;
5069 if (S == SectionRef())
5073 S.getName(SectName);
5074 DataRefImpl Ref = S.getRawDataRefImpl();
5075 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5076 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5078 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5079 p = S.getAddress() + i;
5080 r = get_pointer_64(p, offset, left, S, info);
5083 memset(&mr, '\0', sizeof(struct message_ref64));
5084 if (left < sizeof(struct message_ref64)) {
5085 memcpy(&mr, r, left);
5086 outs() << " (message_ref entends past the end of the section)\n";
5088 memcpy(&mr, r, sizeof(struct message_ref64));
5089 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5093 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
5096 outs() << format("0x%" PRIx64, n_value) << " ";
5098 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
5100 outs() << format("0x%" PRIx64, mr.imp) << " ";
5101 if (name != nullptr)
5102 outs() << " " << name;
5106 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
5107 info, n_value, mr.sel);
5109 if (info->verbose && sym_name != nullptr)
5112 outs() << format("0x%" PRIx64, n_value);
5114 outs() << " + " << format("0x%" PRIx64, mr.sel);
5116 outs() << format("0x%" PRIx64, mr.sel);
5117 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
5118 if (name != nullptr)
5119 outs() << format(" %.*s", left, name);
5122 offset += sizeof(struct message_ref64);
5126 static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
5127 uint32_t i, left, offset, xoffset, p;
5128 struct message_ref32 mr;
5129 const char *name, *r;
5132 if (S == SectionRef())
5136 S.getName(SectName);
5137 DataRefImpl Ref = S.getRawDataRefImpl();
5138 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5139 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5141 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5142 p = S.getAddress() + i;
5143 r = get_pointer_32(p, offset, left, S, info);
5146 memset(&mr, '\0', sizeof(struct message_ref32));
5147 if (left < sizeof(struct message_ref32)) {
5148 memcpy(&mr, r, left);
5149 outs() << " (message_ref entends past the end of the section)\n";
5151 memcpy(&mr, r, sizeof(struct message_ref32));
5152 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5155 outs() << " imp " << format("0x%" PRIx32, mr.imp);
5156 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
5158 if (name != nullptr)
5159 outs() << " " << name;
5162 outs() << " sel " << format("0x%" PRIx32, mr.sel);
5163 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
5164 if (name != nullptr)
5165 outs() << " " << name;
5168 offset += sizeof(struct message_ref32);
5172 static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
5173 uint32_t left, offset, swift_version;
5175 struct objc_image_info64 o;
5178 if (S == SectionRef())
5182 S.getName(SectName);
5183 DataRefImpl Ref = S.getRawDataRefImpl();
5184 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5185 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5187 r = get_pointer_64(p, offset, left, S, info);
5190 memset(&o, '\0', sizeof(struct objc_image_info64));
5191 if (left < sizeof(struct objc_image_info64)) {
5192 memcpy(&o, r, left);
5193 outs() << " (objc_image_info entends past the end of the section)\n";
5195 memcpy(&o, r, sizeof(struct objc_image_info64));
5196 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5198 outs() << " version " << o.version << "\n";
5199 outs() << " flags " << format("0x%" PRIx32, o.flags);
5200 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5201 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5202 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5203 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5204 swift_version = (o.flags >> 8) & 0xff;
5205 if (swift_version != 0) {
5206 if (swift_version == 1)
5207 outs() << " Swift 1.0";
5208 else if (swift_version == 2)
5209 outs() << " Swift 1.1";
5211 outs() << " unknown future Swift version (" << swift_version << ")";
5216 static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5217 uint32_t left, offset, swift_version, p;
5218 struct objc_image_info32 o;
5221 if (S == SectionRef())
5225 S.getName(SectName);
5226 DataRefImpl Ref = S.getRawDataRefImpl();
5227 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5228 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5230 r = get_pointer_32(p, offset, left, S, info);
5233 memset(&o, '\0', sizeof(struct objc_image_info32));
5234 if (left < sizeof(struct objc_image_info32)) {
5235 memcpy(&o, r, left);
5236 outs() << " (objc_image_info entends past the end of the section)\n";
5238 memcpy(&o, r, sizeof(struct objc_image_info32));
5239 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5241 outs() << " version " << o.version << "\n";
5242 outs() << " flags " << format("0x%" PRIx32, o.flags);
5243 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5244 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5245 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5246 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5247 swift_version = (o.flags >> 8) & 0xff;
5248 if (swift_version != 0) {
5249 if (swift_version == 1)
5250 outs() << " Swift 1.0";
5251 else if (swift_version == 2)
5252 outs() << " Swift 1.1";
5254 outs() << " unknown future Swift version (" << swift_version << ")";
5259 static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5260 uint32_t left, offset, p;
5261 struct imageInfo_t o;
5265 S.getName(SectName);
5266 DataRefImpl Ref = S.getRawDataRefImpl();
5267 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5268 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5270 r = get_pointer_32(p, offset, left, S, info);
5273 memset(&o, '\0', sizeof(struct imageInfo_t));
5274 if (left < sizeof(struct imageInfo_t)) {
5275 memcpy(&o, r, left);
5276 outs() << " (imageInfo entends past the end of the section)\n";
5278 memcpy(&o, r, sizeof(struct imageInfo_t));
5279 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5281 outs() << " version " << o.version << "\n";
5282 outs() << " flags " << format("0x%" PRIx32, o.flags);
5288 outs() << " GC-only";
5294 static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
5295 SymbolAddressMap AddrMap;
5297 CreateSymbolAddressMap(O, &AddrMap);
5299 std::vector<SectionRef> Sections;
5300 for (const SectionRef &Section : O->sections()) {
5302 Section.getName(SectName);
5303 Sections.push_back(Section);
5306 struct DisassembleInfo info;
5307 // Set up the block of info used by the Symbolizer call backs.
5308 info.verbose = verbose;
5310 info.AddrMap = &AddrMap;
5311 info.Sections = &Sections;
5312 info.class_name = nullptr;
5313 info.selector_name = nullptr;
5314 info.method = nullptr;
5315 info.demangled_name = nullptr;
5316 info.bindtable = nullptr;
5321 SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5322 if (CL == SectionRef())
5323 CL = get_section(O, "__DATA", "__objc_classlist");
5324 if (CL == SectionRef())
5325 CL = get_section(O, "__DATA_CONST", "__objc_classlist");
5326 if (CL == SectionRef())
5327 CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
5329 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5331 SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5332 if (CR == SectionRef())
5333 CR = get_section(O, "__DATA", "__objc_classrefs");
5334 if (CR == SectionRef())
5335 CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
5336 if (CR == SectionRef())
5337 CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
5339 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5341 SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5342 if (SR == SectionRef())
5343 SR = get_section(O, "__DATA", "__objc_superrefs");
5344 if (SR == SectionRef())
5345 SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
5346 if (SR == SectionRef())
5347 SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
5349 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5351 SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5352 if (CA == SectionRef())
5353 CA = get_section(O, "__DATA", "__objc_catlist");
5354 if (CA == SectionRef())
5355 CA = get_section(O, "__DATA_CONST", "__objc_catlist");
5356 if (CA == SectionRef())
5357 CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
5359 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5361 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5362 if (PL == SectionRef())
5363 PL = get_section(O, "__DATA", "__objc_protolist");
5364 if (PL == SectionRef())
5365 PL = get_section(O, "__DATA_CONST", "__objc_protolist");
5366 if (PL == SectionRef())
5367 PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
5369 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5371 SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5372 if (MR == SectionRef())
5373 MR = get_section(O, "__DATA", "__objc_msgrefs");
5374 if (MR == SectionRef())
5375 MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
5376 if (MR == SectionRef())
5377 MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
5379 print_message_refs64(MR, &info);
5381 SectionRef II = get_section(O, "__OBJC2", "__image_info");
5382 if (II == SectionRef())
5383 II = get_section(O, "__DATA", "__objc_imageinfo");
5384 if (II == SectionRef())
5385 II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
5386 if (II == SectionRef())
5387 II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
5389 print_image_info64(II, &info);
5392 static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
5393 SymbolAddressMap AddrMap;
5395 CreateSymbolAddressMap(O, &AddrMap);
5397 std::vector<SectionRef> Sections;
5398 for (const SectionRef &Section : O->sections()) {
5400 Section.getName(SectName);
5401 Sections.push_back(Section);
5404 struct DisassembleInfo info;
5405 // Set up the block of info used by the Symbolizer call backs.
5406 info.verbose = verbose;
5408 info.AddrMap = &AddrMap;
5409 info.Sections = &Sections;
5410 info.class_name = nullptr;
5411 info.selector_name = nullptr;
5412 info.method = nullptr;
5413 info.demangled_name = nullptr;
5414 info.bindtable = nullptr;
5418 SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5419 if (CL == SectionRef())
5420 CL = get_section(O, "__DATA", "__objc_classlist");
5421 if (CL == SectionRef())
5422 CL = get_section(O, "__DATA_CONST", "__objc_classlist");
5423 if (CL == SectionRef())
5424 CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
5426 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5428 SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5429 if (CR == SectionRef())
5430 CR = get_section(O, "__DATA", "__objc_classrefs");
5431 if (CR == SectionRef())
5432 CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
5433 if (CR == SectionRef())
5434 CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
5436 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5438 SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5439 if (SR == SectionRef())
5440 SR = get_section(O, "__DATA", "__objc_superrefs");
5441 if (SR == SectionRef())
5442 SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
5443 if (SR == SectionRef())
5444 SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
5446 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5448 SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5449 if (CA == SectionRef())
5450 CA = get_section(O, "__DATA", "__objc_catlist");
5451 if (CA == SectionRef())
5452 CA = get_section(O, "__DATA_CONST", "__objc_catlist");
5453 if (CA == SectionRef())
5454 CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
5456 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5458 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5459 if (PL == SectionRef())
5460 PL = get_section(O, "__DATA", "__objc_protolist");
5461 if (PL == SectionRef())
5462 PL = get_section(O, "__DATA_CONST", "__objc_protolist");
5463 if (PL == SectionRef())
5464 PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
5466 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5468 SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5469 if (MR == SectionRef())
5470 MR = get_section(O, "__DATA", "__objc_msgrefs");
5471 if (MR == SectionRef())
5472 MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
5473 if (MR == SectionRef())
5474 MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
5476 print_message_refs32(MR, &info);
5478 SectionRef II = get_section(O, "__OBJC2", "__image_info");
5479 if (II == SectionRef())
5480 II = get_section(O, "__DATA", "__objc_imageinfo");
5481 if (II == SectionRef())
5482 II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
5483 if (II == SectionRef())
5484 II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
5486 print_image_info32(II, &info);
5489 static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
5490 uint32_t i, j, p, offset, xoffset, left, defs_left, def;
5491 const char *r, *name, *defs;
5492 struct objc_module_t module;
5494 struct objc_symtab_t symtab;
5495 struct objc_class_t objc_class;
5496 struct objc_category_t objc_category;
5498 outs() << "Objective-C segment\n";
5499 S = get_section(O, "__OBJC", "__module_info");
5500 if (S == SectionRef())
5503 SymbolAddressMap AddrMap;
5505 CreateSymbolAddressMap(O, &AddrMap);
5507 std::vector<SectionRef> Sections;
5508 for (const SectionRef &Section : O->sections()) {
5510 Section.getName(SectName);
5511 Sections.push_back(Section);
5514 struct DisassembleInfo info;
5515 // Set up the block of info used by the Symbolizer call backs.
5516 info.verbose = verbose;
5518 info.AddrMap = &AddrMap;
5519 info.Sections = &Sections;
5520 info.class_name = nullptr;
5521 info.selector_name = nullptr;
5522 info.method = nullptr;
5523 info.demangled_name = nullptr;
5524 info.bindtable = nullptr;
5528 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
5529 p = S.getAddress() + i;
5530 r = get_pointer_32(p, offset, left, S, &info, true);
5533 memset(&module, '\0', sizeof(struct objc_module_t));
5534 if (left < sizeof(struct objc_module_t)) {
5535 memcpy(&module, r, left);
5536 outs() << " (module extends past end of __module_info section)\n";
5538 memcpy(&module, r, sizeof(struct objc_module_t));
5539 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5542 outs() << "Module " << format("0x%" PRIx32, p) << "\n";
5543 outs() << " version " << module.version << "\n";
5544 outs() << " size " << module.size << "\n";
5546 name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
5547 if (name != nullptr)
5548 outs() << format("%.*s", left, name);
5550 outs() << format("0x%08" PRIx32, module.name)
5551 << "(not in an __OBJC section)";
5554 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
5555 if (module.symtab == 0 || r == nullptr) {
5556 outs() << " symtab " << format("0x%08" PRIx32, module.symtab)
5557 << " (not in an __OBJC section)\n";
5560 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
5561 memset(&symtab, '\0', sizeof(struct objc_symtab_t));
5564 if (left < sizeof(struct objc_symtab_t)) {
5565 memcpy(&symtab, r, left);
5566 outs() << "\tsymtab extends past end of an __OBJC section)\n";
5568 memcpy(&symtab, r, sizeof(struct objc_symtab_t));
5569 if (left > sizeof(struct objc_symtab_t)) {
5570 defs_left = left - sizeof(struct objc_symtab_t);
5571 defs = r + sizeof(struct objc_symtab_t);
5574 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5577 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
5578 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
5579 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
5581 outs() << " (not in an __OBJC section)";
5583 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
5584 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
5585 if (symtab.cls_def_cnt > 0)
5586 outs() << "\tClass Definitions\n";
5587 for (j = 0; j < symtab.cls_def_cnt; j++) {
5588 if ((j + 1) * sizeof(uint32_t) > defs_left) {
5589 outs() << "\t(remaining class defs entries entends past the end of the "
5593 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
5594 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5595 sys::swapByteOrder(def);
5597 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5598 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
5600 if (left > sizeof(struct objc_class_t)) {
5602 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5604 outs() << " (entends past the end of the section)\n";
5605 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5606 memcpy(&objc_class, r, left);
5608 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5609 swapStruct(objc_class);
5610 print_objc_class_t(&objc_class, &info);
5612 outs() << "(not in an __OBJC section)\n";
5615 if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
5616 outs() << "\tMeta Class";
5617 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
5619 if (left > sizeof(struct objc_class_t)) {
5621 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5623 outs() << " (entends past the end of the section)\n";
5624 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5625 memcpy(&objc_class, r, left);
5627 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5628 swapStruct(objc_class);
5629 print_objc_class_t(&objc_class, &info);
5631 outs() << "(not in an __OBJC section)\n";
5635 if (symtab.cat_def_cnt > 0)
5636 outs() << "\tCategory Definitions\n";
5637 for (j = 0; j < symtab.cat_def_cnt; j++) {
5638 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
5639 outs() << "\t(remaining category defs entries entends past the end of "
5640 << "the section)\n";
5643 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
5645 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5646 sys::swapByteOrder(def);
5648 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5649 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
5650 << format("0x%08" PRIx32, def);
5652 if (left > sizeof(struct objc_category_t)) {
5654 memcpy(&objc_category, r, sizeof(struct objc_category_t));
5656 outs() << " (entends past the end of the section)\n";
5657 memset(&objc_category, '\0', sizeof(struct objc_category_t));
5658 memcpy(&objc_category, r, left);
5660 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5661 swapStruct(objc_category);
5662 print_objc_objc_category_t(&objc_category, &info);
5664 outs() << "(not in an __OBJC section)\n";
5668 const SectionRef II = get_section(O, "__OBJC", "__image_info");
5669 if (II != SectionRef())
5670 print_image_info(II, &info);
5675 static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
5676 uint32_t size, uint32_t addr) {
5677 SymbolAddressMap AddrMap;
5678 CreateSymbolAddressMap(O, &AddrMap);
5680 std::vector<SectionRef> Sections;
5681 for (const SectionRef &Section : O->sections()) {
5683 Section.getName(SectName);
5684 Sections.push_back(Section);
5687 struct DisassembleInfo info;
5688 // Set up the block of info used by the Symbolizer call backs.
5689 info.verbose = true;
5691 info.AddrMap = &AddrMap;
5692 info.Sections = &Sections;
5693 info.class_name = nullptr;
5694 info.selector_name = nullptr;
5695 info.method = nullptr;
5696 info.demangled_name = nullptr;
5697 info.bindtable = nullptr;
5702 struct objc_protocol_t protocol;
5703 uint32_t left, paddr;
5704 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
5705 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
5706 left = size - (p - sect);
5707 if (left < sizeof(struct objc_protocol_t)) {
5708 outs() << "Protocol extends past end of __protocol section\n";
5709 memcpy(&protocol, p, left);
5711 memcpy(&protocol, p, sizeof(struct objc_protocol_t));
5712 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5713 swapStruct(protocol);
5714 paddr = addr + (p - sect);
5715 outs() << "Protocol " << format("0x%" PRIx32, paddr);
5716 if (print_protocol(paddr, 0, &info))
5717 outs() << "(not in an __OBJC section)\n";
5722 inline void swapStruct(struct xar_header &xar) {
5723 sys::swapByteOrder(xar.magic);
5724 sys::swapByteOrder(xar.size);
5725 sys::swapByteOrder(xar.version);
5726 sys::swapByteOrder(xar.toc_length_compressed);
5727 sys::swapByteOrder(xar.toc_length_uncompressed);
5728 sys::swapByteOrder(xar.cksum_alg);
5731 static void PrintModeVerbose(uint32_t mode) {
5732 switch(mode & S_IFMT){
5756 /* owner permissions */
5767 else if(mode & S_IEXEC)
5772 /* group permissions */
5773 if(mode & (S_IREAD >> 3))
5777 if(mode & (S_IWRITE >> 3))
5783 else if(mode & (S_IEXEC >> 3))
5788 /* other permissions */
5789 if(mode & (S_IREAD >> 6))
5793 if(mode & (S_IWRITE >> 6))
5799 else if(mode & (S_IEXEC >> 6))
5805 static void PrintXarFilesSummary(const char *XarFilename, xar_t xar) {
5809 const char *key, *type, *mode, *user, *group, *size, *mtime, *name, *m;
5811 uint32_t mode_value;
5813 xi = xar_iter_new();
5815 errs() << "Can't obtain an xar iterator for xar archive "
5816 << XarFilename << "\n";
5820 // Go through the xar's files.
5821 for (xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)) {
5822 xp = xar_iter_new();
5824 errs() << "Can't obtain an xar iterator for xar archive "
5825 << XarFilename << "\n";
5835 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
5836 const char *val = nullptr;
5837 xar_prop_get(xf, key, &val);
5838 #if 0 // Useful for debugging.
5839 outs() << "key: " << key << " value: " << val << "\n";
5841 if(strcmp(key, "type") == 0)
5843 if(strcmp(key, "mode") == 0)
5845 if(strcmp(key, "user") == 0)
5847 if(strcmp(key, "group") == 0)
5849 if(strcmp(key, "data/size") == 0)
5851 if(strcmp(key, "mtime") == 0)
5853 if(strcmp(key, "name") == 0)
5856 if(mode != nullptr){
5857 mode_value = strtoul(mode, &endp, 8);
5859 outs() << "(mode: \"" << mode << "\" contains non-octal chars) ";
5860 if(strcmp(type, "file") == 0)
5861 mode_value |= S_IFREG;
5862 PrintModeVerbose(mode_value);
5866 outs() << format("%10s/", user);
5867 if(group != nullptr)
5868 outs() << format("%-10s ", group);
5870 outs() << format("%7s ", size);
5871 if(mtime != nullptr){
5872 for(m = mtime; *m != 'T' && *m != '\0'; m++)
5877 for( ; *m != 'Z' && *m != '\0'; m++)
5887 static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
5888 uint32_t size, bool verbose,
5889 bool PrintXarHeader, bool PrintXarFileHeaders,
5890 std::string XarMemberName) {
5891 if(size < sizeof(struct xar_header)) {
5892 outs() << "size of (__LLVM,__bundle) section too small (smaller than size "
5893 "of struct xar_header)\n";
5896 struct xar_header XarHeader;
5897 memcpy(&XarHeader, sect, sizeof(struct xar_header));
5898 if (sys::IsLittleEndianHost)
5899 swapStruct(XarHeader);
5900 if (PrintXarHeader) {
5901 if (!XarMemberName.empty())
5902 outs() << "In xar member " << XarMemberName << ": ";
5904 outs() << "For (__LLVM,__bundle) section: ";
5905 outs() << "xar header\n";
5906 if (XarHeader.magic == XAR_HEADER_MAGIC)
5907 outs() << " magic XAR_HEADER_MAGIC\n";
5910 << format_hex(XarHeader.magic, 10, true)
5911 << " (not XAR_HEADER_MAGIC)\n";
5912 outs() << " size " << XarHeader.size << "\n";
5913 outs() << " version " << XarHeader.version << "\n";
5914 outs() << " toc_length_compressed " << XarHeader.toc_length_compressed
5916 outs() << "toc_length_uncompressed " << XarHeader.toc_length_uncompressed
5918 outs() << " cksum_alg ";
5919 switch (XarHeader.cksum_alg) {
5920 case XAR_CKSUM_NONE:
5921 outs() << "XAR_CKSUM_NONE\n";
5923 case XAR_CKSUM_SHA1:
5924 outs() << "XAR_CKSUM_SHA1\n";
5927 outs() << "XAR_CKSUM_MD5\n";
5929 #ifdef XAR_CKSUM_SHA256
5930 case XAR_CKSUM_SHA256:
5931 outs() << "XAR_CKSUM_SHA256\n";
5934 #ifdef XAR_CKSUM_SHA512
5935 case XAR_CKSUM_SHA512:
5936 outs() << "XAR_CKSUM_SHA512\n";
5940 outs() << XarHeader.cksum_alg << "\n";
5944 SmallString<128> XarFilename;
5946 std::error_code XarEC =
5947 sys::fs::createTemporaryFile("llvm-objdump", "xar", FD, XarFilename);
5949 errs() << XarEC.message() << "\n";
5952 tool_output_file XarFile(XarFilename, FD);
5953 raw_fd_ostream &XarOut = XarFile.os();
5954 StringRef XarContents(sect, size);
5955 XarOut << XarContents;
5957 if (XarOut.has_error())
5960 xar_t xar = xar_open(XarFilename.c_str(), READ);
5962 errs() << "Can't create temporary xar archive " << XarFilename << "\n";
5966 SmallString<128> TocFilename;
5967 std::error_code TocEC =
5968 sys::fs::createTemporaryFile("llvm-objdump", "toc", TocFilename);
5970 errs() << TocEC.message() << "\n";
5973 xar_serialize(xar, TocFilename.c_str());
5975 if (PrintXarFileHeaders) {
5976 if (!XarMemberName.empty())
5977 outs() << "In xar member " << XarMemberName << ": ";
5979 outs() << "For (__LLVM,__bundle) section: ";
5980 outs() << "xar archive files:\n";
5981 PrintXarFilesSummary(XarFilename.c_str(), xar);
5984 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
5985 MemoryBuffer::getFileOrSTDIN(TocFilename.c_str());
5986 if (std::error_code EC = FileOrErr.getError()) {
5987 errs() << EC.message() << "\n";
5990 std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
5992 if (!XarMemberName.empty())
5993 outs() << "In xar member " << XarMemberName << ": ";
5995 outs() << "For (__LLVM,__bundle) section: ";
5996 outs() << "xar table of contents:\n";
5997 outs() << Buffer->getBuffer() << "\n";
5999 // TODO: Go through the xar's files.
6000 xar_iter_t xi = xar_iter_new();
6002 errs() << "Can't obtain an xar iterator for xar archive "
6003 << XarFilename.c_str() << "\n";
6007 for(xar_file_t xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)){
6010 const char *member_name, *member_type, *member_size_string;
6013 xp = xar_iter_new();
6015 errs() << "Can't obtain an xar iterator for xar archive "
6016 << XarFilename.c_str() << "\n";
6022 member_size_string = NULL;
6023 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
6024 const char *val = nullptr;
6025 xar_prop_get(xf, key, &val);
6026 #if 0 // Useful for debugging.
6027 outs() << "key: " << key << " value: " << val << "\n";
6029 if(strcmp(key, "name") == 0)
6031 if(strcmp(key, "type") == 0)
6033 if(strcmp(key, "data/size") == 0)
6034 member_size_string = val;
6037 * If we find a file with a name, date/size and type properties
6038 * and with the type being "file" see if that is a xar file.
6040 if (member_name != NULL && member_type != NULL &&
6041 strcmp(member_type, "file") == 0 &&
6042 member_size_string != NULL){
6043 // Extract the file into a buffer.
6045 member_size = strtoul(member_size_string, &endptr, 10);
6046 if (*endptr == '\0' && member_size != 0) {
6047 char *buffer = (char *) ::operator new (member_size);
6048 if (xar_extract_tobuffersz(xar, xf, &buffer, &member_size) == 0) {
6049 #if 0 // Useful for debugging.
6050 outs() << "xar member: " << member_name << " extracted\n";
6052 // Set the XarMemberName we want to see printed in the header.
6053 std::string OldXarMemberName;
6054 // If XarMemberName is already set this is nested. So
6055 // save the old name and create the nested name.
6056 if (!XarMemberName.empty()) {
6057 OldXarMemberName = XarMemberName;
6059 (Twine("[") + XarMemberName + "]" + member_name).str();
6061 OldXarMemberName = "";
6062 XarMemberName = member_name;
6064 // See if this is could be a xar file (nested).
6065 if (member_size >= sizeof(struct xar_header)) {
6066 #if 0 // Useful for debugging.
6067 outs() << "could be a xar file: " << member_name << "\n";
6069 memcpy((char *)&XarHeader, buffer, sizeof(struct xar_header));
6070 if (sys::IsLittleEndianHost)
6071 swapStruct(XarHeader);
6072 if(XarHeader.magic == XAR_HEADER_MAGIC)
6073 DumpBitcodeSection(O, buffer, member_size, verbose,
6074 PrintXarHeader, PrintXarFileHeaders,
6077 XarMemberName = OldXarMemberName;
6086 #endif // defined(HAVE_LIBXAR)
6088 static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
6090 printObjc2_64bit_MetaData(O, verbose);
6092 MachO::mach_header H;
6094 if (H.cputype == MachO::CPU_TYPE_ARM)
6095 printObjc2_32bit_MetaData(O, verbose);
6097 // This is the 32-bit non-arm cputype case. Which is normally
6098 // the first Objective-C ABI. But it may be the case of a
6099 // binary for the iOS simulator which is the second Objective-C
6100 // ABI. In that case printObjc1_32bit_MetaData() will determine that
6101 // and return false.
6102 if (!printObjc1_32bit_MetaData(O, verbose))
6103 printObjc2_32bit_MetaData(O, verbose);
6108 // GuessLiteralPointer returns a string which for the item in the Mach-O file
6109 // for the address passed in as ReferenceValue for printing as a comment with
6110 // the instruction and also returns the corresponding type of that item
6111 // indirectly through ReferenceType.
6113 // If ReferenceValue is an address of literal cstring then a pointer to the
6114 // cstring is returned and ReferenceType is set to
6115 // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
6117 // If ReferenceValue is an address of an Objective-C CFString, Selector ref or
6118 // Class ref that name is returned and the ReferenceType is set accordingly.
6120 // Lastly, literals which are Symbol address in a literal pool are looked for
6121 // and if found the symbol name is returned and ReferenceType is set to
6122 // LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
6124 // If there is no item in the Mach-O file for the address passed in as
6125 // ReferenceValue nullptr is returned and ReferenceType is unchanged.
6126 static const char *GuessLiteralPointer(uint64_t ReferenceValue,
6127 uint64_t ReferencePC,
6128 uint64_t *ReferenceType,
6129 struct DisassembleInfo *info) {
6130 // First see if there is an external relocation entry at the ReferencePC.
6131 if (info->O->getHeader().filetype == MachO::MH_OBJECT) {
6132 uint64_t sect_addr = info->S.getAddress();
6133 uint64_t sect_offset = ReferencePC - sect_addr;
6134 bool reloc_found = false;
6136 MachO::any_relocation_info RE;
6137 bool isExtern = false;
6139 for (const RelocationRef &Reloc : info->S.relocations()) {
6140 uint64_t RelocOffset = Reloc.getOffset();
6141 if (RelocOffset == sect_offset) {
6142 Rel = Reloc.getRawDataRefImpl();
6143 RE = info->O->getRelocation(Rel);
6144 if (info->O->isRelocationScattered(RE))
6146 isExtern = info->O->getPlainRelocationExternal(RE);
6148 symbol_iterator RelocSym = Reloc.getSymbol();
6155 // If there is an external relocation entry for a symbol in a section
6156 // then used that symbol's value for the value of the reference.
6157 if (reloc_found && isExtern) {
6158 if (info->O->getAnyRelocationPCRel(RE)) {
6159 unsigned Type = info->O->getAnyRelocationType(RE);
6160 if (Type == MachO::X86_64_RELOC_SIGNED) {
6161 ReferenceValue = Symbol.getValue();
6167 // Look for literals such as Objective-C CFStrings refs, Selector refs,
6168 // Message refs and Class refs.
6169 bool classref, selref, msgref, cfstring;
6170 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
6171 selref, msgref, cfstring);
6172 if (classref && pointer_value == 0) {
6173 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
6174 // And the pointer_value in that section is typically zero as it will be
6175 // set by dyld as part of the "bind information".
6176 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
6177 if (name != nullptr) {
6178 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
6179 const char *class_name = strrchr(name, '$');
6180 if (class_name != nullptr && class_name[1] == '_' &&
6181 class_name[2] != '\0') {
6182 info->class_name = class_name + 2;
6189 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
6191 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
6192 if (name != nullptr)
6193 info->class_name = name;
6195 name = "bad class ref";
6200 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
6201 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
6205 if (selref && pointer_value == 0)
6206 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
6208 if (pointer_value != 0)
6209 ReferenceValue = pointer_value;
6211 const char *name = GuessCstringPointer(ReferenceValue, info);
6213 if (pointer_value != 0 && selref) {
6214 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
6215 info->selector_name = name;
6216 } else if (pointer_value != 0 && msgref) {
6217 info->class_name = nullptr;
6218 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
6219 info->selector_name = name;
6221 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
6225 // Lastly look for an indirect symbol with this ReferenceValue which is in
6226 // a literal pool. If found return that symbol name.
6227 name = GuessIndirectSymbol(ReferenceValue, info);
6229 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
6236 // SymbolizerSymbolLookUp is the symbol lookup function passed when creating
6237 // the Symbolizer. It looks up the ReferenceValue using the info passed via the
6238 // pointer to the struct DisassembleInfo that was passed when MCSymbolizer
6239 // is created and returns the symbol name that matches the ReferenceValue or
6240 // nullptr if none. The ReferenceType is passed in for the IN type of
6241 // reference the instruction is making from the values in defined in the header
6242 // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
6243 // Out type and the ReferenceName will also be set which is added as a comment
6244 // to the disassembled instruction.
6246 // If the symbol name is a C++ mangled name then the demangled name is
6247 // returned through ReferenceName and ReferenceType is set to
6248 // LLVMDisassembler_ReferenceType_DeMangled_Name .
6250 // When this is called to get a symbol name for a branch target then the
6251 // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
6252 // SymbolValue will be looked for in the indirect symbol table to determine if
6253 // it is an address for a symbol stub. If so then the symbol name for that
6254 // stub is returned indirectly through ReferenceName and then ReferenceType is
6255 // set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
6257 // When this is called with an value loaded via a PC relative load then
6258 // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
6259 // SymbolValue is checked to be an address of literal pointer, symbol pointer,
6260 // or an Objective-C meta data reference. If so the output ReferenceType is
6261 // set to correspond to that as well as setting the ReferenceName.
6262 static const char *SymbolizerSymbolLookUp(void *DisInfo,
6263 uint64_t ReferenceValue,
6264 uint64_t *ReferenceType,
6265 uint64_t ReferencePC,
6266 const char **ReferenceName) {
6267 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
6268 // If no verbose symbolic information is wanted then just return nullptr.
6269 if (!info->verbose) {
6270 *ReferenceName = nullptr;
6271 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6275 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
6277 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
6278 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
6279 if (*ReferenceName != nullptr) {
6280 method_reference(info, ReferenceType, ReferenceName);
6281 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
6282 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
6283 } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
6284 if (info->demangled_name != nullptr)
6285 free(info->demangled_name);
6287 info->demangled_name =
6288 itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
6289 if (info->demangled_name != nullptr) {
6290 *ReferenceName = info->demangled_name;
6291 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
6293 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6295 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6296 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
6298 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6300 method_reference(info, ReferenceType, ReferenceName);
6302 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6303 // If this is arm64 and the reference is an adrp instruction save the
6304 // instruction, passed in ReferenceValue and the address of the instruction
6305 // for use later if we see and add immediate instruction.
6306 } else if (info->O->getArch() == Triple::aarch64 &&
6307 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
6308 info->adrp_inst = ReferenceValue;
6309 info->adrp_addr = ReferencePC;
6310 SymbolName = nullptr;
6311 *ReferenceName = nullptr;
6312 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6313 // If this is arm64 and reference is an add immediate instruction and we
6315 // seen an adrp instruction just before it and the adrp's Xd register
6317 // this add's Xn register reconstruct the value being referenced and look to
6318 // see if it is a literal pointer. Note the add immediate instruction is
6319 // passed in ReferenceValue.
6320 } else if (info->O->getArch() == Triple::aarch64 &&
6321 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
6322 ReferencePC - 4 == info->adrp_addr &&
6323 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
6324 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
6325 uint32_t addxri_inst;
6326 uint64_t adrp_imm, addxri_imm;
6329 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
6330 if (info->adrp_inst & 0x0200000)
6331 adrp_imm |= 0xfffffffffc000000LL;
6333 addxri_inst = ReferenceValue;
6334 addxri_imm = (addxri_inst >> 10) & 0xfff;
6335 if (((addxri_inst >> 22) & 0x3) == 1)
6338 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
6339 (adrp_imm << 12) + addxri_imm;
6342 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6343 if (*ReferenceName == nullptr)
6344 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6345 // If this is arm64 and the reference is a load register instruction and we
6346 // have seen an adrp instruction just before it and the adrp's Xd register
6347 // matches this add's Xn register reconstruct the value being referenced and
6348 // look to see if it is a literal pointer. Note the load register
6349 // instruction is passed in ReferenceValue.
6350 } else if (info->O->getArch() == Triple::aarch64 &&
6351 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
6352 ReferencePC - 4 == info->adrp_addr &&
6353 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
6354 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
6355 uint32_t ldrxui_inst;
6356 uint64_t adrp_imm, ldrxui_imm;
6359 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
6360 if (info->adrp_inst & 0x0200000)
6361 adrp_imm |= 0xfffffffffc000000LL;
6363 ldrxui_inst = ReferenceValue;
6364 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
6366 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
6367 (adrp_imm << 12) + (ldrxui_imm << 3);
6370 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6371 if (*ReferenceName == nullptr)
6372 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6374 // If this arm64 and is an load register (PC-relative) instruction the
6375 // ReferenceValue is the PC plus the immediate value.
6376 else if (info->O->getArch() == Triple::aarch64 &&
6377 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
6378 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
6380 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6381 if (*ReferenceName == nullptr)
6382 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6383 } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
6384 if (info->demangled_name != nullptr)
6385 free(info->demangled_name);
6387 info->demangled_name =
6388 itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
6389 if (info->demangled_name != nullptr) {
6390 *ReferenceName = info->demangled_name;
6391 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
6395 *ReferenceName = nullptr;
6396 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6402 /// \brief Emits the comments that are stored in the CommentStream.
6403 /// Each comment in the CommentStream must end with a newline.
6404 static void emitComments(raw_svector_ostream &CommentStream,
6405 SmallString<128> &CommentsToEmit,
6406 formatted_raw_ostream &FormattedOS,
6407 const MCAsmInfo &MAI) {
6408 // Flush the stream before taking its content.
6409 StringRef Comments = CommentsToEmit.str();
6410 // Get the default information for printing a comment.
6411 StringRef CommentBegin = MAI.getCommentString();
6412 unsigned CommentColumn = MAI.getCommentColumn();
6413 bool IsFirst = true;
6414 while (!Comments.empty()) {
6416 FormattedOS << '\n';
6417 // Emit a line of comments.
6418 FormattedOS.PadToColumn(CommentColumn);
6419 size_t Position = Comments.find('\n');
6420 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
6421 // Move after the newline character.
6422 Comments = Comments.substr(Position + 1);
6425 FormattedOS.flush();
6427 // Tell the comment stream that the vector changed underneath it.
6428 CommentsToEmit.clear();
6431 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
6432 StringRef DisSegName, StringRef DisSectName) {
6433 const char *McpuDefault = nullptr;
6434 const Target *ThumbTarget = nullptr;
6435 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
6437 // GetTarget prints out stuff.
6440 if (MCPU.empty() && McpuDefault)
6443 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
6444 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
6446 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
6448 // Package up features to be passed to target/subtarget
6449 std::string FeaturesStr;
6450 if (MAttrs.size()) {
6451 SubtargetFeatures Features;
6452 for (unsigned i = 0; i != MAttrs.size(); ++i)
6453 Features.AddFeature(MAttrs[i]);
6454 FeaturesStr = Features.getString();
6457 // Set up disassembler.
6458 std::unique_ptr<const MCRegisterInfo> MRI(
6459 TheTarget->createMCRegInfo(TripleName));
6460 std::unique_ptr<const MCAsmInfo> AsmInfo(
6461 TheTarget->createMCAsmInfo(*MRI, TripleName));
6462 std::unique_ptr<const MCSubtargetInfo> STI(
6463 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
6464 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
6465 std::unique_ptr<MCDisassembler> DisAsm(
6466 TheTarget->createMCDisassembler(*STI, Ctx));
6467 std::unique_ptr<MCSymbolizer> Symbolizer;
6468 struct DisassembleInfo SymbolizerInfo;
6469 std::unique_ptr<MCRelocationInfo> RelInfo(
6470 TheTarget->createMCRelocationInfo(TripleName, Ctx));
6472 Symbolizer.reset(TheTarget->createMCSymbolizer(
6473 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
6474 &SymbolizerInfo, &Ctx, std::move(RelInfo)));
6475 DisAsm->setSymbolizer(std::move(Symbolizer));
6477 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
6478 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
6479 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
6480 // Set the display preference for hex vs. decimal immediates.
6481 IP->setPrintImmHex(PrintImmHex);
6482 // Comment stream and backing vector.
6483 SmallString<128> CommentsToEmit;
6484 raw_svector_ostream CommentStream(CommentsToEmit);
6485 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
6486 // if it is done then arm64 comments for string literals don't get printed
6487 // and some constant get printed instead and not setting it causes intel
6488 // (32-bit and 64-bit) comments printed with different spacing before the
6489 // comment causing different diffs with the 'C' disassembler library API.
6490 // IP->setCommentStream(CommentStream);
6492 if (!AsmInfo || !STI || !DisAsm || !IP) {
6493 errs() << "error: couldn't initialize disassembler for target "
6494 << TripleName << '\n';
6498 // Set up separate thumb disassembler if needed.
6499 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
6500 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
6501 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
6502 std::unique_ptr<MCDisassembler> ThumbDisAsm;
6503 std::unique_ptr<MCInstPrinter> ThumbIP;
6504 std::unique_ptr<MCContext> ThumbCtx;
6505 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
6506 struct DisassembleInfo ThumbSymbolizerInfo;
6507 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
6509 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
6511 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
6513 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr));
6514 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
6515 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
6516 MCContext *PtrThumbCtx = ThumbCtx.get();
6518 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
6520 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
6521 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
6522 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
6523 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
6525 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
6526 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
6527 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
6528 *ThumbInstrInfo, *ThumbMRI));
6529 // Set the display preference for hex vs. decimal immediates.
6530 ThumbIP->setPrintImmHex(PrintImmHex);
6533 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
6534 errs() << "error: couldn't initialize disassembler for target "
6535 << ThumbTripleName << '\n';
6539 MachO::mach_header Header = MachOOF->getHeader();
6541 // FIXME: Using the -cfg command line option, this code used to be able to
6542 // annotate relocations with the referenced symbol's name, and if this was
6543 // inside a __[cf]string section, the data it points to. This is now replaced
6544 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
6545 std::vector<SectionRef> Sections;
6546 std::vector<SymbolRef> Symbols;
6547 SmallVector<uint64_t, 8> FoundFns;
6548 uint64_t BaseSegmentAddress;
6550 getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
6551 BaseSegmentAddress);
6553 // Sort the symbols by address, just in case they didn't come in that way.
6554 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
6556 // Build a data in code table that is sorted on by the address of each entry.
6557 uint64_t BaseAddress = 0;
6558 if (Header.filetype == MachO::MH_OBJECT)
6559 BaseAddress = Sections[0].getAddress();
6561 BaseAddress = BaseSegmentAddress;
6563 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
6566 DI->getOffset(Offset);
6567 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
6569 array_pod_sort(Dices.begin(), Dices.end());
6572 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
6574 raw_ostream &DebugOut = nulls();
6577 std::unique_ptr<DIContext> diContext;
6578 ObjectFile *DbgObj = MachOOF;
6579 // Try to find debug info and set up the DIContext for it.
6581 // A separate DSym file path was specified, parse it as a macho file,
6582 // get the sections and supply it to the section name parsing machinery.
6583 if (!DSYMFile.empty()) {
6584 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
6585 MemoryBuffer::getFileOrSTDIN(DSYMFile);
6586 if (std::error_code EC = BufOrErr.getError()) {
6587 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
6591 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
6596 // Setup the DIContext
6597 diContext.reset(new DWARFContextInMemory(*DbgObj));
6600 if (FilterSections.size() == 0)
6601 outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
6603 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
6605 if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
6608 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
6610 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
6611 if (SegmentName != DisSegName)
6615 Sections[SectIdx].getContents(BytesStr);
6616 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
6618 uint64_t SectAddress = Sections[SectIdx].getAddress();
6620 bool symbolTableWorked = false;
6622 // Create a map of symbol addresses to symbol names for use by
6623 // the SymbolizerSymbolLookUp() routine.
6624 SymbolAddressMap AddrMap;
6625 bool DisSymNameFound = false;
6626 for (const SymbolRef &Symbol : MachOOF->symbols()) {
6627 Expected<SymbolRef::Type> STOrErr = Symbol.getType();
6629 report_error(MachOOF->getFileName(), STOrErr.takeError());
6630 SymbolRef::Type ST = *STOrErr;
6631 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
6632 ST == SymbolRef::ST_Other) {
6633 uint64_t Address = Symbol.getValue();
6634 Expected<StringRef> SymNameOrErr = Symbol.getName();
6636 report_error(MachOOF->getFileName(), SymNameOrErr.takeError());
6637 StringRef SymName = *SymNameOrErr;
6638 AddrMap[Address] = SymName;
6639 if (!DisSymName.empty() && DisSymName == SymName)
6640 DisSymNameFound = true;
6643 if (!DisSymName.empty() && !DisSymNameFound) {
6644 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
6647 // Set up the block of info used by the Symbolizer call backs.
6648 SymbolizerInfo.verbose = !NoSymbolicOperands;
6649 SymbolizerInfo.O = MachOOF;
6650 SymbolizerInfo.S = Sections[SectIdx];
6651 SymbolizerInfo.AddrMap = &AddrMap;
6652 SymbolizerInfo.Sections = &Sections;
6653 SymbolizerInfo.class_name = nullptr;
6654 SymbolizerInfo.selector_name = nullptr;
6655 SymbolizerInfo.method = nullptr;
6656 SymbolizerInfo.demangled_name = nullptr;
6657 SymbolizerInfo.bindtable = nullptr;
6658 SymbolizerInfo.adrp_addr = 0;
6659 SymbolizerInfo.adrp_inst = 0;
6660 // Same for the ThumbSymbolizer
6661 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
6662 ThumbSymbolizerInfo.O = MachOOF;
6663 ThumbSymbolizerInfo.S = Sections[SectIdx];
6664 ThumbSymbolizerInfo.AddrMap = &AddrMap;
6665 ThumbSymbolizerInfo.Sections = &Sections;
6666 ThumbSymbolizerInfo.class_name = nullptr;
6667 ThumbSymbolizerInfo.selector_name = nullptr;
6668 ThumbSymbolizerInfo.method = nullptr;
6669 ThumbSymbolizerInfo.demangled_name = nullptr;
6670 ThumbSymbolizerInfo.bindtable = nullptr;
6671 ThumbSymbolizerInfo.adrp_addr = 0;
6672 ThumbSymbolizerInfo.adrp_inst = 0;
6674 unsigned int Arch = MachOOF->getArch();
6676 // Skip all symbols if this is a stubs file.
6677 if (Bytes.size() == 0)
6680 // If the section has symbols but no symbol at the start of the section
6681 // these are used to make sure the bytes before the first symbol are
6683 bool FirstSymbol = true;
6684 bool FirstSymbolAtSectionStart = true;
6686 // Disassemble symbol by symbol.
6687 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
6688 Expected<StringRef> SymNameOrErr = Symbols[SymIdx].getName();
6690 report_error(MachOOF->getFileName(), SymNameOrErr.takeError());
6691 StringRef SymName = *SymNameOrErr;
6693 Expected<SymbolRef::Type> STOrErr = Symbols[SymIdx].getType();
6695 report_error(MachOOF->getFileName(), STOrErr.takeError());
6696 SymbolRef::Type ST = *STOrErr;
6697 if (ST != SymbolRef::ST_Function && ST != SymbolRef::ST_Data)
6700 // Make sure the symbol is defined in this section.
6701 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
6703 if (!DisSymName.empty() && DisSymName == SymName) {
6704 outs() << "-dis-symname: " << DisSymName << " not in the section\n";
6709 // The __mh_execute_header is special and we need to deal with that fact
6710 // this symbol is before the start of the (__TEXT,__text) section and at the
6711 // address of the start of the __TEXT segment. This is because this symbol
6712 // is an N_SECT symbol in the (__TEXT,__text) but its address is before the
6713 // start of the section in a standard MH_EXECUTE filetype.
6714 if (!DisSymName.empty() && DisSymName == "__mh_execute_header") {
6715 outs() << "-dis-symname: __mh_execute_header not in any section\n";
6718 // When this code is trying to disassemble a symbol at a time and in the
6719 // case there is only the __mh_execute_header symbol left as in a stripped
6720 // executable, we need to deal with this by ignoring this symbol so the
6721 // whole section is disassembled and this symbol is then not displayed.
6722 if (SymName == "__mh_execute_header" || SymName == "__mh_dylib_header" ||
6723 SymName == "__mh_bundle_header" || SymName == "__mh_object_header" ||
6724 SymName == "__mh_preload_header" || SymName == "__mh_dylinker_header")
6727 // If we are only disassembling one symbol see if this is that symbol.
6728 if (!DisSymName.empty() && DisSymName != SymName)
6731 // Start at the address of the symbol relative to the section's address.
6732 uint64_t SectSize = Sections[SectIdx].getSize();
6733 uint64_t Start = Symbols[SymIdx].getValue();
6734 uint64_t SectionAddress = Sections[SectIdx].getAddress();
6735 Start -= SectionAddress;
6737 if (Start > SectSize) {
6738 outs() << "section data ends, " << SymName
6739 << " lies outside valid range\n";
6743 // Stop disassembling either at the beginning of the next symbol or at
6744 // the end of the section.
6745 bool containsNextSym = false;
6746 uint64_t NextSym = 0;
6747 uint64_t NextSymIdx = SymIdx + 1;
6748 while (Symbols.size() > NextSymIdx) {
6749 Expected<SymbolRef::Type> STOrErr = Symbols[NextSymIdx].getType();
6751 report_error(MachOOF->getFileName(), STOrErr.takeError());
6752 SymbolRef::Type NextSymType = *STOrErr;
6753 if (NextSymType == SymbolRef::ST_Function) {
6755 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
6756 NextSym = Symbols[NextSymIdx].getValue();
6757 NextSym -= SectionAddress;
6763 uint64_t End = containsNextSym ? std::min(NextSym, SectSize) : SectSize;
6766 symbolTableWorked = true;
6768 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
6769 bool IsThumb = MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb;
6771 // We only need the dedicated Thumb target if there's a real choice
6772 // (i.e. we're not targeting M-class) and the function is Thumb.
6773 bool UseThumbTarget = IsThumb && ThumbTarget;
6775 // If we are not specifying a symbol to start disassembly with and this
6776 // is the first symbol in the section but not at the start of the section
6777 // then move the disassembly index to the start of the section and
6778 // don't print the symbol name just yet. This is so the bytes before the
6779 // first symbol are disassembled.
6780 uint64_t SymbolStart = Start;
6781 if (DisSymName.empty() && FirstSymbol && Start != 0) {
6782 FirstSymbolAtSectionStart = false;
6786 outs() << SymName << ":\n";
6788 DILineInfo lastLine;
6789 for (uint64_t Index = Start; Index < End; Index += Size) {
6792 // If this is the first symbol in the section and it was not at the
6793 // start of the section, see if we are at its Index now and if so print
6795 if (FirstSymbol && !FirstSymbolAtSectionStart && Index == SymbolStart)
6796 outs() << SymName << ":\n";
6798 uint64_t PC = SectAddress + Index;
6799 if (!NoLeadingAddr) {
6800 if (FullLeadingAddr) {
6801 if (MachOOF->is64Bit())
6802 outs() << format("%016" PRIx64, PC);
6804 outs() << format("%08" PRIx64, PC);
6806 outs() << format("%8" PRIx64 ":", PC);
6809 if (!NoShowRawInsn || Arch == Triple::arm)
6812 // Check the data in code table here to see if this is data not an
6813 // instruction to be disassembled.
6815 Dice.push_back(std::make_pair(PC, DiceRef()));
6816 dice_table_iterator DTI =
6817 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
6818 compareDiceTableEntries);
6819 if (DTI != Dices.end()) {
6821 DTI->second.getLength(Length);
6823 DTI->second.getKind(Kind);
6824 Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
6825 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
6826 (PC == (DTI->first + Length - 1)) && (Length & 1))
6831 SmallVector<char, 64> AnnotationsBytes;
6832 raw_svector_ostream Annotations(AnnotationsBytes);
6836 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
6837 PC, DebugOut, Annotations);
6839 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
6840 DebugOut, Annotations);
6842 if (!NoShowRawInsn || Arch == Triple::arm) {
6843 dumpBytes(makeArrayRef(Bytes.data() + Index, Size), outs());
6845 formatted_raw_ostream FormattedOS(outs());
6846 StringRef AnnotationsStr = Annotations.str();
6848 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
6850 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
6851 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
6853 // Print debug info.
6855 DILineInfo dli = diContext->getLineInfoForAddress(PC);
6856 // Print valid line info if it changed.
6857 if (dli != lastLine && dli.Line != 0)
6858 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
6864 unsigned int Arch = MachOOF->getArch();
6865 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
6866 outs() << format("\t.byte 0x%02x #bad opcode\n",
6867 *(Bytes.data() + Index) & 0xff);
6868 Size = 1; // skip exactly one illegible byte and move on.
6869 } else if (Arch == Triple::aarch64 ||
6870 (Arch == Triple::arm && !IsThumb)) {
6871 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6872 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
6873 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
6874 (*(Bytes.data() + Index + 3) & 0xff) << 24;
6875 outs() << format("\t.long\t0x%08x\n", opcode);
6877 } else if (Arch == Triple::arm) {
6878 assert(IsThumb && "ARM mode should have been dealt with above");
6879 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6880 (*(Bytes.data() + Index + 1) & 0xff) << 8;
6881 outs() << format("\t.short\t0x%04x\n", opcode);
6884 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6886 Size = 1; // skip illegible bytes
6890 // Now that we are done disassembled the first symbol set the bool that
6891 // were doing this to false.
6892 FirstSymbol = false;
6894 if (!symbolTableWorked) {
6895 // Reading the symbol table didn't work, disassemble the whole section.
6896 uint64_t SectAddress = Sections[SectIdx].getAddress();
6897 uint64_t SectSize = Sections[SectIdx].getSize();
6899 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
6902 uint64_t PC = SectAddress + Index;
6903 SmallVector<char, 64> AnnotationsBytes;
6904 raw_svector_ostream Annotations(AnnotationsBytes);
6905 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
6906 DebugOut, Annotations)) {
6907 if (!NoLeadingAddr) {
6908 if (FullLeadingAddr) {
6909 if (MachOOF->is64Bit())
6910 outs() << format("%016" PRIx64, PC);
6912 outs() << format("%08" PRIx64, PC);
6914 outs() << format("%8" PRIx64 ":", PC);
6917 if (!NoShowRawInsn || Arch == Triple::arm) {
6919 dumpBytes(makeArrayRef(Bytes.data() + Index, InstSize), outs());
6921 StringRef AnnotationsStr = Annotations.str();
6922 IP->printInst(&Inst, outs(), AnnotationsStr, *STI);
6925 unsigned int Arch = MachOOF->getArch();
6926 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
6927 outs() << format("\t.byte 0x%02x #bad opcode\n",
6928 *(Bytes.data() + Index) & 0xff);
6929 InstSize = 1; // skip exactly one illegible byte and move on.
6931 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6933 InstSize = 1; // skip illegible bytes
6938 // The TripleName's need to be reset if we are called again for a different
6941 ThumbTripleName = "";
6943 if (SymbolizerInfo.method != nullptr)
6944 free(SymbolizerInfo.method);
6945 if (SymbolizerInfo.demangled_name != nullptr)
6946 free(SymbolizerInfo.demangled_name);
6947 if (ThumbSymbolizerInfo.method != nullptr)
6948 free(ThumbSymbolizerInfo.method);
6949 if (ThumbSymbolizerInfo.demangled_name != nullptr)
6950 free(ThumbSymbolizerInfo.demangled_name);
6954 //===----------------------------------------------------------------------===//
6955 // __compact_unwind section dumping
6956 //===----------------------------------------------------------------------===//
6960 template <typename T> static uint64_t readNext(const char *&Buf) {
6961 using llvm::support::little;
6962 using llvm::support::unaligned;
6964 uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
6969 struct CompactUnwindEntry {
6970 uint32_t OffsetInSection;
6972 uint64_t FunctionAddr;
6974 uint32_t CompactEncoding;
6975 uint64_t PersonalityAddr;
6978 RelocationRef FunctionReloc;
6979 RelocationRef PersonalityReloc;
6980 RelocationRef LSDAReloc;
6982 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
6983 : OffsetInSection(Offset) {
6985 read<uint64_t>(Contents.data() + Offset);
6987 read<uint32_t>(Contents.data() + Offset);
6991 template <typename UIntPtr> void read(const char *Buf) {
6992 FunctionAddr = readNext<UIntPtr>(Buf);
6993 Length = readNext<uint32_t>(Buf);
6994 CompactEncoding = readNext<uint32_t>(Buf);
6995 PersonalityAddr = readNext<UIntPtr>(Buf);
6996 LSDAAddr = readNext<UIntPtr>(Buf);
7001 /// Given a relocation from __compact_unwind, consisting of the RelocationRef
7002 /// and data being relocated, determine the best base Name and Addend to use for
7003 /// display purposes.
7005 /// 1. An Extern relocation will directly reference a symbol (and the data is
7006 /// then already an addend), so use that.
7007 /// 2. Otherwise the data is an offset in the object file's layout; try to find
7008 // a symbol before it in the same section, and use the offset from there.
7009 /// 3. Finally, if all that fails, fall back to an offset from the start of the
7010 /// referenced section.
7011 static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
7012 std::map<uint64_t, SymbolRef> &Symbols,
7013 const RelocationRef &Reloc, uint64_t Addr,
7014 StringRef &Name, uint64_t &Addend) {
7015 if (Reloc.getSymbol() != Obj->symbol_end()) {
7016 Expected<StringRef> NameOrErr = Reloc.getSymbol()->getName();
7018 report_error(Obj->getFileName(), NameOrErr.takeError());
7024 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
7025 SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
7027 uint64_t SectionAddr = RelocSection.getAddress();
7029 auto Sym = Symbols.upper_bound(Addr);
7030 if (Sym == Symbols.begin()) {
7031 // The first symbol in the object is after this reference, the best we can
7032 // do is section-relative notation.
7033 RelocSection.getName(Name);
7034 Addend = Addr - SectionAddr;
7038 // Go back one so that SymbolAddress <= Addr.
7041 auto SectOrErr = Sym->second.getSection();
7043 report_error(Obj->getFileName(), SectOrErr.takeError());
7044 section_iterator SymSection = *SectOrErr;
7045 if (RelocSection == *SymSection) {
7046 // There's a valid symbol in the same section before this reference.
7047 Expected<StringRef> NameOrErr = Sym->second.getName();
7049 report_error(Obj->getFileName(), NameOrErr.takeError());
7051 Addend = Addr - Sym->first;
7055 // There is a symbol before this reference, but it's in a different
7056 // section. Probably not helpful to mention it, so use the section name.
7057 RelocSection.getName(Name);
7058 Addend = Addr - SectionAddr;
7061 static void printUnwindRelocDest(const MachOObjectFile *Obj,
7062 std::map<uint64_t, SymbolRef> &Symbols,
7063 const RelocationRef &Reloc, uint64_t Addr) {
7067 if (!Reloc.getObject())
7070 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
7074 outs() << " + " << format("0x%" PRIx64, Addend);
7078 printMachOCompactUnwindSection(const MachOObjectFile *Obj,
7079 std::map<uint64_t, SymbolRef> &Symbols,
7080 const SectionRef &CompactUnwind) {
7082 if (!Obj->isLittleEndian()) {
7083 outs() << "Skipping big-endian __compact_unwind section\n";
7087 bool Is64 = Obj->is64Bit();
7088 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
7089 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
7092 CompactUnwind.getContents(Contents);
7094 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
7096 // First populate the initial raw offsets, encodings and so on from the entry.
7097 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
7098 CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
7099 CompactUnwinds.push_back(Entry);
7102 // Next we need to look at the relocations to find out what objects are
7103 // actually being referred to.
7104 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
7105 uint64_t RelocAddress = Reloc.getOffset();
7107 uint32_t EntryIdx = RelocAddress / EntrySize;
7108 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
7109 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
7111 if (OffsetInEntry == 0)
7112 Entry.FunctionReloc = Reloc;
7113 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
7114 Entry.PersonalityReloc = Reloc;
7115 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
7116 Entry.LSDAReloc = Reloc;
7118 outs() << "Invalid relocation in __compact_unwind section\n";
7123 // Finally, we're ready to print the data we've gathered.
7124 outs() << "Contents of __compact_unwind section:\n";
7125 for (auto &Entry : CompactUnwinds) {
7126 outs() << " Entry at offset "
7127 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
7129 // 1. Start of the region this entry applies to.
7130 outs() << " start: " << format("0x%" PRIx64,
7131 Entry.FunctionAddr) << ' ';
7132 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
7135 // 2. Length of the region this entry applies to.
7136 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
7138 // 3. The 32-bit compact encoding.
7139 outs() << " compact encoding: "
7140 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
7142 // 4. The personality function, if present.
7143 if (Entry.PersonalityReloc.getObject()) {
7144 outs() << " personality function: "
7145 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
7146 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
7147 Entry.PersonalityAddr);
7151 // 5. This entry's language-specific data area.
7152 if (Entry.LSDAReloc.getObject()) {
7153 outs() << " LSDA: " << format("0x%" PRIx64,
7154 Entry.LSDAAddr) << ' ';
7155 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
7161 //===----------------------------------------------------------------------===//
7162 // __unwind_info section dumping
7163 //===----------------------------------------------------------------------===//
7165 static void printRegularSecondLevelUnwindPage(const char *PageStart) {
7166 const char *Pos = PageStart;
7167 uint32_t Kind = readNext<uint32_t>(Pos);
7169 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
7171 uint16_t EntriesStart = readNext<uint16_t>(Pos);
7172 uint16_t NumEntries = readNext<uint16_t>(Pos);
7174 Pos = PageStart + EntriesStart;
7175 for (unsigned i = 0; i < NumEntries; ++i) {
7176 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
7177 uint32_t Encoding = readNext<uint32_t>(Pos);
7179 outs() << " [" << i << "]: "
7180 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7182 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
7186 static void printCompressedSecondLevelUnwindPage(
7187 const char *PageStart, uint32_t FunctionBase,
7188 const SmallVectorImpl<uint32_t> &CommonEncodings) {
7189 const char *Pos = PageStart;
7190 uint32_t Kind = readNext<uint32_t>(Pos);
7192 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
7194 uint16_t EntriesStart = readNext<uint16_t>(Pos);
7195 uint16_t NumEntries = readNext<uint16_t>(Pos);
7197 uint16_t EncodingsStart = readNext<uint16_t>(Pos);
7198 readNext<uint16_t>(Pos);
7199 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
7200 PageStart + EncodingsStart);
7202 Pos = PageStart + EntriesStart;
7203 for (unsigned i = 0; i < NumEntries; ++i) {
7204 uint32_t Entry = readNext<uint32_t>(Pos);
7205 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
7206 uint32_t EncodingIdx = Entry >> 24;
7209 if (EncodingIdx < CommonEncodings.size())
7210 Encoding = CommonEncodings[EncodingIdx];
7212 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
7214 outs() << " [" << i << "]: "
7215 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7217 << "encoding[" << EncodingIdx
7218 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
7222 static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
7223 std::map<uint64_t, SymbolRef> &Symbols,
7224 const SectionRef &UnwindInfo) {
7226 if (!Obj->isLittleEndian()) {
7227 outs() << "Skipping big-endian __unwind_info section\n";
7231 outs() << "Contents of __unwind_info section:\n";
7234 UnwindInfo.getContents(Contents);
7235 const char *Pos = Contents.data();
7237 //===----------------------------------
7239 //===----------------------------------
7241 uint32_t Version = readNext<uint32_t>(Pos);
7242 outs() << " Version: "
7243 << format("0x%" PRIx32, Version) << '\n';
7245 outs() << " Skipping section with unknown version\n";
7249 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
7250 outs() << " Common encodings array section offset: "
7251 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
7252 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
7253 outs() << " Number of common encodings in array: "
7254 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
7256 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
7257 outs() << " Personality function array section offset: "
7258 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
7259 uint32_t NumPersonalities = readNext<uint32_t>(Pos);
7260 outs() << " Number of personality functions in array: "
7261 << format("0x%" PRIx32, NumPersonalities) << '\n';
7263 uint32_t IndicesStart = readNext<uint32_t>(Pos);
7264 outs() << " Index array section offset: "
7265 << format("0x%" PRIx32, IndicesStart) << '\n';
7266 uint32_t NumIndices = readNext<uint32_t>(Pos);
7267 outs() << " Number of indices in array: "
7268 << format("0x%" PRIx32, NumIndices) << '\n';
7270 //===----------------------------------
7271 // A shared list of common encodings
7272 //===----------------------------------
7274 // These occupy indices in the range [0, N] whenever an encoding is referenced
7275 // from a compressed 2nd level index table. In practice the linker only
7276 // creates ~128 of these, so that indices are available to embed encodings in
7277 // the 2nd level index.
7279 SmallVector<uint32_t, 64> CommonEncodings;
7280 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
7281 Pos = Contents.data() + CommonEncodingsStart;
7282 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
7283 uint32_t Encoding = readNext<uint32_t>(Pos);
7284 CommonEncodings.push_back(Encoding);
7286 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
7290 //===----------------------------------
7291 // Personality functions used in this executable
7292 //===----------------------------------
7294 // There should be only a handful of these (one per source language,
7295 // roughly). Particularly since they only get 2 bits in the compact encoding.
7297 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
7298 Pos = Contents.data() + PersonalitiesStart;
7299 for (unsigned i = 0; i < NumPersonalities; ++i) {
7300 uint32_t PersonalityFn = readNext<uint32_t>(Pos);
7301 outs() << " personality[" << i + 1
7302 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
7305 //===----------------------------------
7306 // The level 1 index entries
7307 //===----------------------------------
7309 // These specify an approximate place to start searching for the more detailed
7310 // information, sorted by PC.
7313 uint32_t FunctionOffset;
7314 uint32_t SecondLevelPageStart;
7318 SmallVector<IndexEntry, 4> IndexEntries;
7320 outs() << " Top level indices: (count = " << NumIndices << ")\n";
7321 Pos = Contents.data() + IndicesStart;
7322 for (unsigned i = 0; i < NumIndices; ++i) {
7325 Entry.FunctionOffset = readNext<uint32_t>(Pos);
7326 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
7327 Entry.LSDAStart = readNext<uint32_t>(Pos);
7328 IndexEntries.push_back(Entry);
7330 outs() << " [" << i << "]: "
7331 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
7333 << "2nd level page offset="
7334 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
7335 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
7338 //===----------------------------------
7339 // Next come the LSDA tables
7340 //===----------------------------------
7342 // The LSDA layout is rather implicit: it's a contiguous array of entries from
7343 // the first top-level index's LSDAOffset to the last (sentinel).
7345 outs() << " LSDA descriptors:\n";
7346 Pos = Contents.data() + IndexEntries[0].LSDAStart;
7347 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
7348 (2 * sizeof(uint32_t));
7349 for (int i = 0; i < NumLSDAs; ++i) {
7350 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
7351 uint32_t LSDAOffset = readNext<uint32_t>(Pos);
7352 outs() << " [" << i << "]: "
7353 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7355 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
7358 //===----------------------------------
7359 // Finally, the 2nd level indices
7360 //===----------------------------------
7362 // Generally these are 4K in size, and have 2 possible forms:
7363 // + Regular stores up to 511 entries with disparate encodings
7364 // + Compressed stores up to 1021 entries if few enough compact encoding
7366 outs() << " Second level indices:\n";
7367 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
7368 // The final sentinel top-level index has no associated 2nd level page
7369 if (IndexEntries[i].SecondLevelPageStart == 0)
7372 outs() << " Second level index[" << i << "]: "
7373 << "offset in section="
7374 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
7376 << "base function offset="
7377 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
7379 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
7380 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
7382 printRegularSecondLevelUnwindPage(Pos);
7384 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
7387 outs() << " Skipping 2nd level page with unknown kind " << Kind
7392 void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
7393 std::map<uint64_t, SymbolRef> Symbols;
7394 for (const SymbolRef &SymRef : Obj->symbols()) {
7395 // Discard any undefined or absolute symbols. They're not going to take part
7396 // in the convenience lookup for unwind info and just take up resources.
7397 auto SectOrErr = SymRef.getSection();
7399 // TODO: Actually report errors helpfully.
7400 consumeError(SectOrErr.takeError());
7403 section_iterator Section = *SectOrErr;
7404 if (Section == Obj->section_end())
7407 uint64_t Addr = SymRef.getValue();
7408 Symbols.insert(std::make_pair(Addr, SymRef));
7411 for (const SectionRef &Section : Obj->sections()) {
7413 Section.getName(SectName);
7414 if (SectName == "__compact_unwind")
7415 printMachOCompactUnwindSection(Obj, Symbols, Section);
7416 else if (SectName == "__unwind_info")
7417 printMachOUnwindInfoSection(Obj, Symbols, Section);
7421 static void PrintMachHeader(uint32_t magic, uint32_t cputype,
7422 uint32_t cpusubtype, uint32_t filetype,
7423 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
7425 outs() << "Mach header\n";
7426 outs() << " magic cputype cpusubtype caps filetype ncmds "
7427 "sizeofcmds flags\n";
7429 if (magic == MachO::MH_MAGIC)
7430 outs() << " MH_MAGIC";
7431 else if (magic == MachO::MH_MAGIC_64)
7432 outs() << "MH_MAGIC_64";
7434 outs() << format(" 0x%08" PRIx32, magic);
7436 case MachO::CPU_TYPE_I386:
7438 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7439 case MachO::CPU_SUBTYPE_I386_ALL:
7443 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7447 case MachO::CPU_TYPE_X86_64:
7448 outs() << " X86_64";
7449 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7450 case MachO::CPU_SUBTYPE_X86_64_ALL:
7453 case MachO::CPU_SUBTYPE_X86_64_H:
7454 outs() << " Haswell";
7457 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7461 case MachO::CPU_TYPE_ARM:
7463 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7464 case MachO::CPU_SUBTYPE_ARM_ALL:
7467 case MachO::CPU_SUBTYPE_ARM_V4T:
7470 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
7473 case MachO::CPU_SUBTYPE_ARM_XSCALE:
7474 outs() << " XSCALE";
7476 case MachO::CPU_SUBTYPE_ARM_V6:
7479 case MachO::CPU_SUBTYPE_ARM_V6M:
7482 case MachO::CPU_SUBTYPE_ARM_V7:
7485 case MachO::CPU_SUBTYPE_ARM_V7EM:
7488 case MachO::CPU_SUBTYPE_ARM_V7K:
7491 case MachO::CPU_SUBTYPE_ARM_V7M:
7494 case MachO::CPU_SUBTYPE_ARM_V7S:
7498 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7502 case MachO::CPU_TYPE_ARM64:
7504 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7505 case MachO::CPU_SUBTYPE_ARM64_ALL:
7509 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7513 case MachO::CPU_TYPE_POWERPC:
7515 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7516 case MachO::CPU_SUBTYPE_POWERPC_ALL:
7520 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7524 case MachO::CPU_TYPE_POWERPC64:
7526 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7527 case MachO::CPU_SUBTYPE_POWERPC_ALL:
7531 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7536 outs() << format(" %7d", cputype);
7537 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7540 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
7543 outs() << format(" 0x%02" PRIx32,
7544 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7547 case MachO::MH_OBJECT:
7548 outs() << " OBJECT";
7550 case MachO::MH_EXECUTE:
7551 outs() << " EXECUTE";
7553 case MachO::MH_FVMLIB:
7554 outs() << " FVMLIB";
7556 case MachO::MH_CORE:
7559 case MachO::MH_PRELOAD:
7560 outs() << " PRELOAD";
7562 case MachO::MH_DYLIB:
7565 case MachO::MH_DYLIB_STUB:
7566 outs() << " DYLIB_STUB";
7568 case MachO::MH_DYLINKER:
7569 outs() << " DYLINKER";
7571 case MachO::MH_BUNDLE:
7572 outs() << " BUNDLE";
7574 case MachO::MH_DSYM:
7577 case MachO::MH_KEXT_BUNDLE:
7578 outs() << " KEXTBUNDLE";
7581 outs() << format(" %10u", filetype);
7584 outs() << format(" %5u", ncmds);
7585 outs() << format(" %10u", sizeofcmds);
7587 if (f & MachO::MH_NOUNDEFS) {
7588 outs() << " NOUNDEFS";
7589 f &= ~MachO::MH_NOUNDEFS;
7591 if (f & MachO::MH_INCRLINK) {
7592 outs() << " INCRLINK";
7593 f &= ~MachO::MH_INCRLINK;
7595 if (f & MachO::MH_DYLDLINK) {
7596 outs() << " DYLDLINK";
7597 f &= ~MachO::MH_DYLDLINK;
7599 if (f & MachO::MH_BINDATLOAD) {
7600 outs() << " BINDATLOAD";
7601 f &= ~MachO::MH_BINDATLOAD;
7603 if (f & MachO::MH_PREBOUND) {
7604 outs() << " PREBOUND";
7605 f &= ~MachO::MH_PREBOUND;
7607 if (f & MachO::MH_SPLIT_SEGS) {
7608 outs() << " SPLIT_SEGS";
7609 f &= ~MachO::MH_SPLIT_SEGS;
7611 if (f & MachO::MH_LAZY_INIT) {
7612 outs() << " LAZY_INIT";
7613 f &= ~MachO::MH_LAZY_INIT;
7615 if (f & MachO::MH_TWOLEVEL) {
7616 outs() << " TWOLEVEL";
7617 f &= ~MachO::MH_TWOLEVEL;
7619 if (f & MachO::MH_FORCE_FLAT) {
7620 outs() << " FORCE_FLAT";
7621 f &= ~MachO::MH_FORCE_FLAT;
7623 if (f & MachO::MH_NOMULTIDEFS) {
7624 outs() << " NOMULTIDEFS";
7625 f &= ~MachO::MH_NOMULTIDEFS;
7627 if (f & MachO::MH_NOFIXPREBINDING) {
7628 outs() << " NOFIXPREBINDING";
7629 f &= ~MachO::MH_NOFIXPREBINDING;
7631 if (f & MachO::MH_PREBINDABLE) {
7632 outs() << " PREBINDABLE";
7633 f &= ~MachO::MH_PREBINDABLE;
7635 if (f & MachO::MH_ALLMODSBOUND) {
7636 outs() << " ALLMODSBOUND";
7637 f &= ~MachO::MH_ALLMODSBOUND;
7639 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
7640 outs() << " SUBSECTIONS_VIA_SYMBOLS";
7641 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
7643 if (f & MachO::MH_CANONICAL) {
7644 outs() << " CANONICAL";
7645 f &= ~MachO::MH_CANONICAL;
7647 if (f & MachO::MH_WEAK_DEFINES) {
7648 outs() << " WEAK_DEFINES";
7649 f &= ~MachO::MH_WEAK_DEFINES;
7651 if (f & MachO::MH_BINDS_TO_WEAK) {
7652 outs() << " BINDS_TO_WEAK";
7653 f &= ~MachO::MH_BINDS_TO_WEAK;
7655 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
7656 outs() << " ALLOW_STACK_EXECUTION";
7657 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
7659 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
7660 outs() << " DEAD_STRIPPABLE_DYLIB";
7661 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
7663 if (f & MachO::MH_PIE) {
7665 f &= ~MachO::MH_PIE;
7667 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
7668 outs() << " NO_REEXPORTED_DYLIBS";
7669 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
7671 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
7672 outs() << " MH_HAS_TLV_DESCRIPTORS";
7673 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
7675 if (f & MachO::MH_NO_HEAP_EXECUTION) {
7676 outs() << " MH_NO_HEAP_EXECUTION";
7677 f &= ~MachO::MH_NO_HEAP_EXECUTION;
7679 if (f & MachO::MH_APP_EXTENSION_SAFE) {
7680 outs() << " APP_EXTENSION_SAFE";
7681 f &= ~MachO::MH_APP_EXTENSION_SAFE;
7683 if (f & MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO) {
7684 outs() << " NLIST_OUTOFSYNC_WITH_DYLDINFO";
7685 f &= ~MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO;
7687 if (f != 0 || flags == 0)
7688 outs() << format(" 0x%08" PRIx32, f);
7690 outs() << format(" 0x%08" PRIx32, magic);
7691 outs() << format(" %7d", cputype);
7692 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7693 outs() << format(" 0x%02" PRIx32,
7694 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7695 outs() << format(" %10u", filetype);
7696 outs() << format(" %5u", ncmds);
7697 outs() << format(" %10u", sizeofcmds);
7698 outs() << format(" 0x%08" PRIx32, flags);
7703 static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
7704 StringRef SegName, uint64_t vmaddr,
7705 uint64_t vmsize, uint64_t fileoff,
7706 uint64_t filesize, uint32_t maxprot,
7707 uint32_t initprot, uint32_t nsects,
7708 uint32_t flags, uint32_t object_size,
7710 uint64_t expected_cmdsize;
7711 if (cmd == MachO::LC_SEGMENT) {
7712 outs() << " cmd LC_SEGMENT\n";
7713 expected_cmdsize = nsects;
7714 expected_cmdsize *= sizeof(struct MachO::section);
7715 expected_cmdsize += sizeof(struct MachO::segment_command);
7717 outs() << " cmd LC_SEGMENT_64\n";
7718 expected_cmdsize = nsects;
7719 expected_cmdsize *= sizeof(struct MachO::section_64);
7720 expected_cmdsize += sizeof(struct MachO::segment_command_64);
7722 outs() << " cmdsize " << cmdsize;
7723 if (cmdsize != expected_cmdsize)
7724 outs() << " Inconsistent size\n";
7727 outs() << " segname " << SegName << "\n";
7728 if (cmd == MachO::LC_SEGMENT_64) {
7729 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
7730 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
7732 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
7733 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
7735 outs() << " fileoff " << fileoff;
7736 if (fileoff > object_size)
7737 outs() << " (past end of file)\n";
7740 outs() << " filesize " << filesize;
7741 if (fileoff + filesize > object_size)
7742 outs() << " (past end of file)\n";
7747 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7748 MachO::VM_PROT_EXECUTE)) != 0)
7749 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
7751 outs() << " maxprot ";
7752 outs() << ((maxprot & MachO::VM_PROT_READ) ? "r" : "-");
7753 outs() << ((maxprot & MachO::VM_PROT_WRITE) ? "w" : "-");
7754 outs() << ((maxprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
7757 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7758 MachO::VM_PROT_EXECUTE)) != 0)
7759 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
7761 outs() << " initprot ";
7762 outs() << ((initprot & MachO::VM_PROT_READ) ? "r" : "-");
7763 outs() << ((initprot & MachO::VM_PROT_WRITE) ? "w" : "-");
7764 outs() << ((initprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
7767 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
7768 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
7770 outs() << " nsects " << nsects << "\n";
7774 outs() << " (none)\n";
7776 if (flags & MachO::SG_HIGHVM) {
7777 outs() << " HIGHVM";
7778 flags &= ~MachO::SG_HIGHVM;
7780 if (flags & MachO::SG_FVMLIB) {
7781 outs() << " FVMLIB";
7782 flags &= ~MachO::SG_FVMLIB;
7784 if (flags & MachO::SG_NORELOC) {
7785 outs() << " NORELOC";
7786 flags &= ~MachO::SG_NORELOC;
7788 if (flags & MachO::SG_PROTECTED_VERSION_1) {
7789 outs() << " PROTECTED_VERSION_1";
7790 flags &= ~MachO::SG_PROTECTED_VERSION_1;
7793 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
7798 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
7802 static void PrintSection(const char *sectname, const char *segname,
7803 uint64_t addr, uint64_t size, uint32_t offset,
7804 uint32_t align, uint32_t reloff, uint32_t nreloc,
7805 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
7806 uint32_t cmd, const char *sg_segname,
7807 uint32_t filetype, uint32_t object_size,
7809 outs() << "Section\n";
7810 outs() << " sectname " << format("%.16s\n", sectname);
7811 outs() << " segname " << format("%.16s", segname);
7812 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
7813 outs() << " (does not match segment)\n";
7816 if (cmd == MachO::LC_SEGMENT_64) {
7817 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
7818 outs() << " size " << format("0x%016" PRIx64, size);
7820 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
7821 outs() << " size " << format("0x%08" PRIx64, size);
7823 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
7824 outs() << " (past end of file)\n";
7827 outs() << " offset " << offset;
7828 if (offset > object_size)
7829 outs() << " (past end of file)\n";
7832 uint32_t align_shifted = 1 << align;
7833 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
7834 outs() << " reloff " << reloff;
7835 if (reloff > object_size)
7836 outs() << " (past end of file)\n";
7839 outs() << " nreloc " << nreloc;
7840 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
7841 outs() << " (past end of file)\n";
7844 uint32_t section_type = flags & MachO::SECTION_TYPE;
7847 if (section_type == MachO::S_REGULAR)
7848 outs() << " S_REGULAR\n";
7849 else if (section_type == MachO::S_ZEROFILL)
7850 outs() << " S_ZEROFILL\n";
7851 else if (section_type == MachO::S_CSTRING_LITERALS)
7852 outs() << " S_CSTRING_LITERALS\n";
7853 else if (section_type == MachO::S_4BYTE_LITERALS)
7854 outs() << " S_4BYTE_LITERALS\n";
7855 else if (section_type == MachO::S_8BYTE_LITERALS)
7856 outs() << " S_8BYTE_LITERALS\n";
7857 else if (section_type == MachO::S_16BYTE_LITERALS)
7858 outs() << " S_16BYTE_LITERALS\n";
7859 else if (section_type == MachO::S_LITERAL_POINTERS)
7860 outs() << " S_LITERAL_POINTERS\n";
7861 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
7862 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
7863 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
7864 outs() << " S_LAZY_SYMBOL_POINTERS\n";
7865 else if (section_type == MachO::S_SYMBOL_STUBS)
7866 outs() << " S_SYMBOL_STUBS\n";
7867 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
7868 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
7869 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
7870 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
7871 else if (section_type == MachO::S_COALESCED)
7872 outs() << " S_COALESCED\n";
7873 else if (section_type == MachO::S_INTERPOSING)
7874 outs() << " S_INTERPOSING\n";
7875 else if (section_type == MachO::S_DTRACE_DOF)
7876 outs() << " S_DTRACE_DOF\n";
7877 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
7878 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
7879 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
7880 outs() << " S_THREAD_LOCAL_REGULAR\n";
7881 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
7882 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
7883 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
7884 outs() << " S_THREAD_LOCAL_VARIABLES\n";
7885 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7886 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
7887 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
7888 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
7890 outs() << format("0x%08" PRIx32, section_type) << "\n";
7891 outs() << "attributes";
7892 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
7893 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
7894 outs() << " PURE_INSTRUCTIONS";
7895 if (section_attributes & MachO::S_ATTR_NO_TOC)
7896 outs() << " NO_TOC";
7897 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
7898 outs() << " STRIP_STATIC_SYMS";
7899 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
7900 outs() << " NO_DEAD_STRIP";
7901 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
7902 outs() << " LIVE_SUPPORT";
7903 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
7904 outs() << " SELF_MODIFYING_CODE";
7905 if (section_attributes & MachO::S_ATTR_DEBUG)
7907 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
7908 outs() << " SOME_INSTRUCTIONS";
7909 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
7910 outs() << " EXT_RELOC";
7911 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
7912 outs() << " LOC_RELOC";
7913 if (section_attributes == 0)
7914 outs() << " (none)";
7917 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
7918 outs() << " reserved1 " << reserved1;
7919 if (section_type == MachO::S_SYMBOL_STUBS ||
7920 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
7921 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
7922 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
7923 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7924 outs() << " (index into indirect symbol table)\n";
7927 outs() << " reserved2 " << reserved2;
7928 if (section_type == MachO::S_SYMBOL_STUBS)
7929 outs() << " (size of stubs)\n";
7934 static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
7935 uint32_t object_size) {
7936 outs() << " cmd LC_SYMTAB\n";
7937 outs() << " cmdsize " << st.cmdsize;
7938 if (st.cmdsize != sizeof(struct MachO::symtab_command))
7939 outs() << " Incorrect size\n";
7942 outs() << " symoff " << st.symoff;
7943 if (st.symoff > object_size)
7944 outs() << " (past end of file)\n";
7947 outs() << " nsyms " << st.nsyms;
7950 big_size = st.nsyms;
7951 big_size *= sizeof(struct MachO::nlist_64);
7952 big_size += st.symoff;
7953 if (big_size > object_size)
7954 outs() << " (past end of file)\n";
7958 big_size = st.nsyms;
7959 big_size *= sizeof(struct MachO::nlist);
7960 big_size += st.symoff;
7961 if (big_size > object_size)
7962 outs() << " (past end of file)\n";
7966 outs() << " stroff " << st.stroff;
7967 if (st.stroff > object_size)
7968 outs() << " (past end of file)\n";
7971 outs() << " strsize " << st.strsize;
7972 big_size = st.stroff;
7973 big_size += st.strsize;
7974 if (big_size > object_size)
7975 outs() << " (past end of file)\n";
7980 static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
7981 uint32_t nsyms, uint32_t object_size,
7983 outs() << " cmd LC_DYSYMTAB\n";
7984 outs() << " cmdsize " << dyst.cmdsize;
7985 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
7986 outs() << " Incorrect size\n";
7989 outs() << " ilocalsym " << dyst.ilocalsym;
7990 if (dyst.ilocalsym > nsyms)
7991 outs() << " (greater than the number of symbols)\n";
7994 outs() << " nlocalsym " << dyst.nlocalsym;
7996 big_size = dyst.ilocalsym;
7997 big_size += dyst.nlocalsym;
7998 if (big_size > nsyms)
7999 outs() << " (past the end of the symbol table)\n";
8002 outs() << " iextdefsym " << dyst.iextdefsym;
8003 if (dyst.iextdefsym > nsyms)
8004 outs() << " (greater than the number of symbols)\n";
8007 outs() << " nextdefsym " << dyst.nextdefsym;
8008 big_size = dyst.iextdefsym;
8009 big_size += dyst.nextdefsym;
8010 if (big_size > nsyms)
8011 outs() << " (past the end of the symbol table)\n";
8014 outs() << " iundefsym " << dyst.iundefsym;
8015 if (dyst.iundefsym > nsyms)
8016 outs() << " (greater than the number of symbols)\n";
8019 outs() << " nundefsym " << dyst.nundefsym;
8020 big_size = dyst.iundefsym;
8021 big_size += dyst.nundefsym;
8022 if (big_size > nsyms)
8023 outs() << " (past the end of the symbol table)\n";
8026 outs() << " tocoff " << dyst.tocoff;
8027 if (dyst.tocoff > object_size)
8028 outs() << " (past end of file)\n";
8031 outs() << " ntoc " << dyst.ntoc;
8032 big_size = dyst.ntoc;
8033 big_size *= sizeof(struct MachO::dylib_table_of_contents);
8034 big_size += dyst.tocoff;
8035 if (big_size > object_size)
8036 outs() << " (past end of file)\n";
8039 outs() << " modtaboff " << dyst.modtaboff;
8040 if (dyst.modtaboff > object_size)
8041 outs() << " (past end of file)\n";
8044 outs() << " nmodtab " << dyst.nmodtab;
8047 modtabend = dyst.nmodtab;
8048 modtabend *= sizeof(struct MachO::dylib_module_64);
8049 modtabend += dyst.modtaboff;
8051 modtabend = dyst.nmodtab;
8052 modtabend *= sizeof(struct MachO::dylib_module);
8053 modtabend += dyst.modtaboff;
8055 if (modtabend > object_size)
8056 outs() << " (past end of file)\n";
8059 outs() << " extrefsymoff " << dyst.extrefsymoff;
8060 if (dyst.extrefsymoff > object_size)
8061 outs() << " (past end of file)\n";
8064 outs() << " nextrefsyms " << dyst.nextrefsyms;
8065 big_size = dyst.nextrefsyms;
8066 big_size *= sizeof(struct MachO::dylib_reference);
8067 big_size += dyst.extrefsymoff;
8068 if (big_size > object_size)
8069 outs() << " (past end of file)\n";
8072 outs() << " indirectsymoff " << dyst.indirectsymoff;
8073 if (dyst.indirectsymoff > object_size)
8074 outs() << " (past end of file)\n";
8077 outs() << " nindirectsyms " << dyst.nindirectsyms;
8078 big_size = dyst.nindirectsyms;
8079 big_size *= sizeof(uint32_t);
8080 big_size += dyst.indirectsymoff;
8081 if (big_size > object_size)
8082 outs() << " (past end of file)\n";
8085 outs() << " extreloff " << dyst.extreloff;
8086 if (dyst.extreloff > object_size)
8087 outs() << " (past end of file)\n";
8090 outs() << " nextrel " << dyst.nextrel;
8091 big_size = dyst.nextrel;
8092 big_size *= sizeof(struct MachO::relocation_info);
8093 big_size += dyst.extreloff;
8094 if (big_size > object_size)
8095 outs() << " (past end of file)\n";
8098 outs() << " locreloff " << dyst.locreloff;
8099 if (dyst.locreloff > object_size)
8100 outs() << " (past end of file)\n";
8103 outs() << " nlocrel " << dyst.nlocrel;
8104 big_size = dyst.nlocrel;
8105 big_size *= sizeof(struct MachO::relocation_info);
8106 big_size += dyst.locreloff;
8107 if (big_size > object_size)
8108 outs() << " (past end of file)\n";
8113 static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
8114 uint32_t object_size) {
8115 if (dc.cmd == MachO::LC_DYLD_INFO)
8116 outs() << " cmd LC_DYLD_INFO\n";
8118 outs() << " cmd LC_DYLD_INFO_ONLY\n";
8119 outs() << " cmdsize " << dc.cmdsize;
8120 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
8121 outs() << " Incorrect size\n";
8124 outs() << " rebase_off " << dc.rebase_off;
8125 if (dc.rebase_off > object_size)
8126 outs() << " (past end of file)\n";
8129 outs() << " rebase_size " << dc.rebase_size;
8131 big_size = dc.rebase_off;
8132 big_size += dc.rebase_size;
8133 if (big_size > object_size)
8134 outs() << " (past end of file)\n";
8137 outs() << " bind_off " << dc.bind_off;
8138 if (dc.bind_off > object_size)
8139 outs() << " (past end of file)\n";
8142 outs() << " bind_size " << dc.bind_size;
8143 big_size = dc.bind_off;
8144 big_size += dc.bind_size;
8145 if (big_size > object_size)
8146 outs() << " (past end of file)\n";
8149 outs() << " weak_bind_off " << dc.weak_bind_off;
8150 if (dc.weak_bind_off > object_size)
8151 outs() << " (past end of file)\n";
8154 outs() << " weak_bind_size " << dc.weak_bind_size;
8155 big_size = dc.weak_bind_off;
8156 big_size += dc.weak_bind_size;
8157 if (big_size > object_size)
8158 outs() << " (past end of file)\n";
8161 outs() << " lazy_bind_off " << dc.lazy_bind_off;
8162 if (dc.lazy_bind_off > object_size)
8163 outs() << " (past end of file)\n";
8166 outs() << " lazy_bind_size " << dc.lazy_bind_size;
8167 big_size = dc.lazy_bind_off;
8168 big_size += dc.lazy_bind_size;
8169 if (big_size > object_size)
8170 outs() << " (past end of file)\n";
8173 outs() << " export_off " << dc.export_off;
8174 if (dc.export_off > object_size)
8175 outs() << " (past end of file)\n";
8178 outs() << " export_size " << dc.export_size;
8179 big_size = dc.export_off;
8180 big_size += dc.export_size;
8181 if (big_size > object_size)
8182 outs() << " (past end of file)\n";
8187 static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
8189 if (dyld.cmd == MachO::LC_ID_DYLINKER)
8190 outs() << " cmd LC_ID_DYLINKER\n";
8191 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
8192 outs() << " cmd LC_LOAD_DYLINKER\n";
8193 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
8194 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
8196 outs() << " cmd ?(" << dyld.cmd << ")\n";
8197 outs() << " cmdsize " << dyld.cmdsize;
8198 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
8199 outs() << " Incorrect size\n";
8202 if (dyld.name >= dyld.cmdsize)
8203 outs() << " name ?(bad offset " << dyld.name << ")\n";
8205 const char *P = (const char *)(Ptr) + dyld.name;
8206 outs() << " name " << P << " (offset " << dyld.name << ")\n";
8210 static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
8211 outs() << " cmd LC_UUID\n";
8212 outs() << " cmdsize " << uuid.cmdsize;
8213 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
8214 outs() << " Incorrect size\n";
8218 for (int i = 0; i < 16; ++i) {
8219 outs() << format("%02" PRIX32, uuid.uuid[i]);
8220 if (i == 3 || i == 5 || i == 7 || i == 9)
8226 static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
8227 outs() << " cmd LC_RPATH\n";
8228 outs() << " cmdsize " << rpath.cmdsize;
8229 if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
8230 outs() << " Incorrect size\n";
8233 if (rpath.path >= rpath.cmdsize)
8234 outs() << " path ?(bad offset " << rpath.path << ")\n";
8236 const char *P = (const char *)(Ptr) + rpath.path;
8237 outs() << " path " << P << " (offset " << rpath.path << ")\n";
8241 static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
8242 StringRef LoadCmdName;
8244 case MachO::LC_VERSION_MIN_MACOSX:
8245 LoadCmdName = "LC_VERSION_MIN_MACOSX";
8247 case MachO::LC_VERSION_MIN_IPHONEOS:
8248 LoadCmdName = "LC_VERSION_MIN_IPHONEOS";
8250 case MachO::LC_VERSION_MIN_TVOS:
8251 LoadCmdName = "LC_VERSION_MIN_TVOS";
8253 case MachO::LC_VERSION_MIN_WATCHOS:
8254 LoadCmdName = "LC_VERSION_MIN_WATCHOS";
8257 llvm_unreachable("Unknown version min load command");
8260 outs() << " cmd " << LoadCmdName << '\n';
8261 outs() << " cmdsize " << vd.cmdsize;
8262 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
8263 outs() << " Incorrect size\n";
8266 outs() << " version "
8267 << MachOObjectFile::getVersionMinMajor(vd, false) << "."
8268 << MachOObjectFile::getVersionMinMinor(vd, false);
8269 uint32_t Update = MachOObjectFile::getVersionMinUpdate(vd, false);
8271 outs() << "." << Update;
8274 outs() << " sdk n/a";
8277 << MachOObjectFile::getVersionMinMajor(vd, true) << "."
8278 << MachOObjectFile::getVersionMinMinor(vd, true);
8280 Update = MachOObjectFile::getVersionMinUpdate(vd, true);
8282 outs() << "." << Update;
8286 static void PrintNoteLoadCommand(MachO::note_command Nt) {
8287 outs() << " cmd LC_NOTE\n";
8288 outs() << " cmdsize " << Nt.cmdsize;
8289 if (Nt.cmdsize != sizeof(struct MachO::note_command))
8290 outs() << " Incorrect size\n";
8293 const char *d = Nt.data_owner;
8294 outs() << "data_owner " << format("%.16s\n", d);
8295 outs() << " offset " << Nt.offset << "\n";
8296 outs() << " size " << Nt.size << "\n";
8299 static void PrintBuildToolVersion(MachO::build_tool_version bv) {
8300 outs() << " tool " << MachOObjectFile::getBuildTool(bv.tool) << "\n";
8301 outs() << " version " << MachOObjectFile::getVersionString(bv.version)
8305 static void PrintBuildVersionLoadCommand(const MachOObjectFile *obj,
8306 MachO::build_version_command bd) {
8307 outs() << " cmd LC_BUILD_VERSION\n";
8308 outs() << " cmdsize " << bd.cmdsize;
8310 sizeof(struct MachO::build_version_command) +
8311 bd.ntools * sizeof(struct MachO::build_tool_version))
8312 outs() << " Incorrect size\n";
8315 outs() << " platform " << MachOObjectFile::getBuildPlatform(bd.platform)
8318 outs() << " sdk " << MachOObjectFile::getVersionString(bd.sdk)
8321 outs() << " sdk n/a\n";
8322 outs() << " minos " << MachOObjectFile::getVersionString(bd.minos)
8324 outs() << " ntools " << bd.ntools << "\n";
8325 for (unsigned i = 0; i < bd.ntools; ++i) {
8326 MachO::build_tool_version bv = obj->getBuildToolVersion(i);
8327 PrintBuildToolVersion(bv);
8331 static void PrintSourceVersionCommand(MachO::source_version_command sd) {
8332 outs() << " cmd LC_SOURCE_VERSION\n";
8333 outs() << " cmdsize " << sd.cmdsize;
8334 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
8335 outs() << " Incorrect size\n";
8338 uint64_t a = (sd.version >> 40) & 0xffffff;
8339 uint64_t b = (sd.version >> 30) & 0x3ff;
8340 uint64_t c = (sd.version >> 20) & 0x3ff;
8341 uint64_t d = (sd.version >> 10) & 0x3ff;
8342 uint64_t e = sd.version & 0x3ff;
8343 outs() << " version " << a << "." << b;
8345 outs() << "." << c << "." << d << "." << e;
8347 outs() << "." << c << "." << d;
8353 static void PrintEntryPointCommand(MachO::entry_point_command ep) {
8354 outs() << " cmd LC_MAIN\n";
8355 outs() << " cmdsize " << ep.cmdsize;
8356 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
8357 outs() << " Incorrect size\n";
8360 outs() << " entryoff " << ep.entryoff << "\n";
8361 outs() << " stacksize " << ep.stacksize << "\n";
8364 static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
8365 uint32_t object_size) {
8366 outs() << " cmd LC_ENCRYPTION_INFO\n";
8367 outs() << " cmdsize " << ec.cmdsize;
8368 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
8369 outs() << " Incorrect size\n";
8372 outs() << " cryptoff " << ec.cryptoff;
8373 if (ec.cryptoff > object_size)
8374 outs() << " (past end of file)\n";
8377 outs() << " cryptsize " << ec.cryptsize;
8378 if (ec.cryptsize > object_size)
8379 outs() << " (past end of file)\n";
8382 outs() << " cryptid " << ec.cryptid << "\n";
8385 static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
8386 uint32_t object_size) {
8387 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
8388 outs() << " cmdsize " << ec.cmdsize;
8389 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
8390 outs() << " Incorrect size\n";
8393 outs() << " cryptoff " << ec.cryptoff;
8394 if (ec.cryptoff > object_size)
8395 outs() << " (past end of file)\n";
8398 outs() << " cryptsize " << ec.cryptsize;
8399 if (ec.cryptsize > object_size)
8400 outs() << " (past end of file)\n";
8403 outs() << " cryptid " << ec.cryptid << "\n";
8404 outs() << " pad " << ec.pad << "\n";
8407 static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
8409 outs() << " cmd LC_LINKER_OPTION\n";
8410 outs() << " cmdsize " << lo.cmdsize;
8411 if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
8412 outs() << " Incorrect size\n";
8415 outs() << " count " << lo.count << "\n";
8416 const char *string = Ptr + sizeof(struct MachO::linker_option_command);
8417 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
8420 while (*string == '\0' && left > 0) {
8426 outs() << " string #" << i << " " << format("%.*s\n", left, string);
8427 uint32_t NullPos = StringRef(string, left).find('\0');
8428 uint32_t len = std::min(NullPos, left) + 1;
8434 outs() << " count " << lo.count << " does not match number of strings "
8438 static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
8440 outs() << " cmd LC_SUB_FRAMEWORK\n";
8441 outs() << " cmdsize " << sub.cmdsize;
8442 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
8443 outs() << " Incorrect size\n";
8446 if (sub.umbrella < sub.cmdsize) {
8447 const char *P = Ptr + sub.umbrella;
8448 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
8450 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
8454 static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
8456 outs() << " cmd LC_SUB_UMBRELLA\n";
8457 outs() << " cmdsize " << sub.cmdsize;
8458 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
8459 outs() << " Incorrect size\n";
8462 if (sub.sub_umbrella < sub.cmdsize) {
8463 const char *P = Ptr + sub.sub_umbrella;
8464 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
8466 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
8470 static void PrintSubLibraryCommand(MachO::sub_library_command sub,
8472 outs() << " cmd LC_SUB_LIBRARY\n";
8473 outs() << " cmdsize " << sub.cmdsize;
8474 if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
8475 outs() << " Incorrect size\n";
8478 if (sub.sub_library < sub.cmdsize) {
8479 const char *P = Ptr + sub.sub_library;
8480 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
8482 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
8486 static void PrintSubClientCommand(MachO::sub_client_command sub,
8488 outs() << " cmd LC_SUB_CLIENT\n";
8489 outs() << " cmdsize " << sub.cmdsize;
8490 if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
8491 outs() << " Incorrect size\n";
8494 if (sub.client < sub.cmdsize) {
8495 const char *P = Ptr + sub.client;
8496 outs() << " client " << P << " (offset " << sub.client << ")\n";
8498 outs() << " client ?(bad offset " << sub.client << ")\n";
8502 static void PrintRoutinesCommand(MachO::routines_command r) {
8503 outs() << " cmd LC_ROUTINES\n";
8504 outs() << " cmdsize " << r.cmdsize;
8505 if (r.cmdsize != sizeof(struct MachO::routines_command))
8506 outs() << " Incorrect size\n";
8509 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
8510 outs() << " init_module " << r.init_module << "\n";
8511 outs() << " reserved1 " << r.reserved1 << "\n";
8512 outs() << " reserved2 " << r.reserved2 << "\n";
8513 outs() << " reserved3 " << r.reserved3 << "\n";
8514 outs() << " reserved4 " << r.reserved4 << "\n";
8515 outs() << " reserved5 " << r.reserved5 << "\n";
8516 outs() << " reserved6 " << r.reserved6 << "\n";
8519 static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
8520 outs() << " cmd LC_ROUTINES_64\n";
8521 outs() << " cmdsize " << r.cmdsize;
8522 if (r.cmdsize != sizeof(struct MachO::routines_command_64))
8523 outs() << " Incorrect size\n";
8526 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
8527 outs() << " init_module " << r.init_module << "\n";
8528 outs() << " reserved1 " << r.reserved1 << "\n";
8529 outs() << " reserved2 " << r.reserved2 << "\n";
8530 outs() << " reserved3 " << r.reserved3 << "\n";
8531 outs() << " reserved4 " << r.reserved4 << "\n";
8532 outs() << " reserved5 " << r.reserved5 << "\n";
8533 outs() << " reserved6 " << r.reserved6 << "\n";
8536 static void Print_x86_thread_state32_t(MachO::x86_thread_state32_t &cpu32) {
8537 outs() << "\t eax " << format("0x%08" PRIx32, cpu32.eax);
8538 outs() << " ebx " << format("0x%08" PRIx32, cpu32.ebx);
8539 outs() << " ecx " << format("0x%08" PRIx32, cpu32.ecx);
8540 outs() << " edx " << format("0x%08" PRIx32, cpu32.edx) << "\n";
8541 outs() << "\t edi " << format("0x%08" PRIx32, cpu32.edi);
8542 outs() << " esi " << format("0x%08" PRIx32, cpu32.esi);
8543 outs() << " ebp " << format("0x%08" PRIx32, cpu32.ebp);
8544 outs() << " esp " << format("0x%08" PRIx32, cpu32.esp) << "\n";
8545 outs() << "\t ss " << format("0x%08" PRIx32, cpu32.ss);
8546 outs() << " eflags " << format("0x%08" PRIx32, cpu32.eflags);
8547 outs() << " eip " << format("0x%08" PRIx32, cpu32.eip);
8548 outs() << " cs " << format("0x%08" PRIx32, cpu32.cs) << "\n";
8549 outs() << "\t ds " << format("0x%08" PRIx32, cpu32.ds);
8550 outs() << " es " << format("0x%08" PRIx32, cpu32.es);
8551 outs() << " fs " << format("0x%08" PRIx32, cpu32.fs);
8552 outs() << " gs " << format("0x%08" PRIx32, cpu32.gs) << "\n";
8555 static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
8556 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
8557 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
8558 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
8559 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
8560 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
8561 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
8562 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
8563 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
8564 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
8565 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
8566 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
8567 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
8568 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
8569 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
8570 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
8571 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
8572 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
8573 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
8574 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
8575 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
8576 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
8579 static void Print_mmst_reg(MachO::mmst_reg_t &r) {
8581 outs() << "\t mmst_reg ";
8582 for (f = 0; f < 10; f++)
8583 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
8585 outs() << "\t mmst_rsrv ";
8586 for (f = 0; f < 6; f++)
8587 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
8591 static void Print_xmm_reg(MachO::xmm_reg_t &r) {
8593 outs() << "\t xmm_reg ";
8594 for (f = 0; f < 16; f++)
8595 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
8599 static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
8600 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
8601 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
8602 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
8603 outs() << " denorm " << fpu.fpu_fcw.denorm;
8604 outs() << " zdiv " << fpu.fpu_fcw.zdiv;
8605 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
8606 outs() << " undfl " << fpu.fpu_fcw.undfl;
8607 outs() << " precis " << fpu.fpu_fcw.precis << "\n";
8608 outs() << "\t\t pc ";
8609 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
8610 outs() << "FP_PREC_24B ";
8611 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
8612 outs() << "FP_PREC_53B ";
8613 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
8614 outs() << "FP_PREC_64B ";
8616 outs() << fpu.fpu_fcw.pc << " ";
8618 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
8619 outs() << "FP_RND_NEAR ";
8620 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
8621 outs() << "FP_RND_DOWN ";
8622 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
8623 outs() << "FP_RND_UP ";
8624 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
8625 outs() << "FP_CHOP ";
8627 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
8628 outs() << " denorm " << fpu.fpu_fsw.denorm;
8629 outs() << " zdiv " << fpu.fpu_fsw.zdiv;
8630 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
8631 outs() << " undfl " << fpu.fpu_fsw.undfl;
8632 outs() << " precis " << fpu.fpu_fsw.precis;
8633 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
8634 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
8635 outs() << " c0 " << fpu.fpu_fsw.c0;
8636 outs() << " c1 " << fpu.fpu_fsw.c1;
8637 outs() << " c2 " << fpu.fpu_fsw.c2;
8638 outs() << " tos " << fpu.fpu_fsw.tos;
8639 outs() << " c3 " << fpu.fpu_fsw.c3;
8640 outs() << " busy " << fpu.fpu_fsw.busy << "\n";
8641 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
8642 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
8643 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
8644 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
8645 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
8646 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
8647 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
8648 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
8649 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
8650 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
8651 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
8653 outs() << "\t fpu_stmm0:\n";
8654 Print_mmst_reg(fpu.fpu_stmm0);
8655 outs() << "\t fpu_stmm1:\n";
8656 Print_mmst_reg(fpu.fpu_stmm1);
8657 outs() << "\t fpu_stmm2:\n";
8658 Print_mmst_reg(fpu.fpu_stmm2);
8659 outs() << "\t fpu_stmm3:\n";
8660 Print_mmst_reg(fpu.fpu_stmm3);
8661 outs() << "\t fpu_stmm4:\n";
8662 Print_mmst_reg(fpu.fpu_stmm4);
8663 outs() << "\t fpu_stmm5:\n";
8664 Print_mmst_reg(fpu.fpu_stmm5);
8665 outs() << "\t fpu_stmm6:\n";
8666 Print_mmst_reg(fpu.fpu_stmm6);
8667 outs() << "\t fpu_stmm7:\n";
8668 Print_mmst_reg(fpu.fpu_stmm7);
8669 outs() << "\t fpu_xmm0:\n";
8670 Print_xmm_reg(fpu.fpu_xmm0);
8671 outs() << "\t fpu_xmm1:\n";
8672 Print_xmm_reg(fpu.fpu_xmm1);
8673 outs() << "\t fpu_xmm2:\n";
8674 Print_xmm_reg(fpu.fpu_xmm2);
8675 outs() << "\t fpu_xmm3:\n";
8676 Print_xmm_reg(fpu.fpu_xmm3);
8677 outs() << "\t fpu_xmm4:\n";
8678 Print_xmm_reg(fpu.fpu_xmm4);
8679 outs() << "\t fpu_xmm5:\n";
8680 Print_xmm_reg(fpu.fpu_xmm5);
8681 outs() << "\t fpu_xmm6:\n";
8682 Print_xmm_reg(fpu.fpu_xmm6);
8683 outs() << "\t fpu_xmm7:\n";
8684 Print_xmm_reg(fpu.fpu_xmm7);
8685 outs() << "\t fpu_xmm8:\n";
8686 Print_xmm_reg(fpu.fpu_xmm8);
8687 outs() << "\t fpu_xmm9:\n";
8688 Print_xmm_reg(fpu.fpu_xmm9);
8689 outs() << "\t fpu_xmm10:\n";
8690 Print_xmm_reg(fpu.fpu_xmm10);
8691 outs() << "\t fpu_xmm11:\n";
8692 Print_xmm_reg(fpu.fpu_xmm11);
8693 outs() << "\t fpu_xmm12:\n";
8694 Print_xmm_reg(fpu.fpu_xmm12);
8695 outs() << "\t fpu_xmm13:\n";
8696 Print_xmm_reg(fpu.fpu_xmm13);
8697 outs() << "\t fpu_xmm14:\n";
8698 Print_xmm_reg(fpu.fpu_xmm14);
8699 outs() << "\t fpu_xmm15:\n";
8700 Print_xmm_reg(fpu.fpu_xmm15);
8701 outs() << "\t fpu_rsrv4:\n";
8702 for (uint32_t f = 0; f < 6; f++) {
8704 for (uint32_t g = 0; g < 16; g++)
8705 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
8708 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
8712 static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
8713 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
8714 outs() << " err " << format("0x%08" PRIx32, exc64.err);
8715 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
8718 static void Print_arm_thread_state32_t(MachO::arm_thread_state32_t &cpu32) {
8719 outs() << "\t r0 " << format("0x%08" PRIx32, cpu32.r[0]);
8720 outs() << " r1 " << format("0x%08" PRIx32, cpu32.r[1]);
8721 outs() << " r2 " << format("0x%08" PRIx32, cpu32.r[2]);
8722 outs() << " r3 " << format("0x%08" PRIx32, cpu32.r[3]) << "\n";
8723 outs() << "\t r4 " << format("0x%08" PRIx32, cpu32.r[4]);
8724 outs() << " r5 " << format("0x%08" PRIx32, cpu32.r[5]);
8725 outs() << " r6 " << format("0x%08" PRIx32, cpu32.r[6]);
8726 outs() << " r7 " << format("0x%08" PRIx32, cpu32.r[7]) << "\n";
8727 outs() << "\t r8 " << format("0x%08" PRIx32, cpu32.r[8]);
8728 outs() << " r9 " << format("0x%08" PRIx32, cpu32.r[9]);
8729 outs() << " r10 " << format("0x%08" PRIx32, cpu32.r[10]);
8730 outs() << " r11 " << format("0x%08" PRIx32, cpu32.r[11]) << "\n";
8731 outs() << "\t r12 " << format("0x%08" PRIx32, cpu32.r[12]);
8732 outs() << " sp " << format("0x%08" PRIx32, cpu32.sp);
8733 outs() << " lr " << format("0x%08" PRIx32, cpu32.lr);
8734 outs() << " pc " << format("0x%08" PRIx32, cpu32.pc) << "\n";
8735 outs() << "\t cpsr " << format("0x%08" PRIx32, cpu32.cpsr) << "\n";
8738 static void Print_arm_thread_state64_t(MachO::arm_thread_state64_t &cpu64) {
8739 outs() << "\t x0 " << format("0x%016" PRIx64, cpu64.x[0]);
8740 outs() << " x1 " << format("0x%016" PRIx64, cpu64.x[1]);
8741 outs() << " x2 " << format("0x%016" PRIx64, cpu64.x[2]) << "\n";
8742 outs() << "\t x3 " << format("0x%016" PRIx64, cpu64.x[3]);
8743 outs() << " x4 " << format("0x%016" PRIx64, cpu64.x[4]);
8744 outs() << " x5 " << format("0x%016" PRIx64, cpu64.x[5]) << "\n";
8745 outs() << "\t x6 " << format("0x%016" PRIx64, cpu64.x[6]);
8746 outs() << " x7 " << format("0x%016" PRIx64, cpu64.x[7]);
8747 outs() << " x8 " << format("0x%016" PRIx64, cpu64.x[8]) << "\n";
8748 outs() << "\t x9 " << format("0x%016" PRIx64, cpu64.x[9]);
8749 outs() << " x10 " << format("0x%016" PRIx64, cpu64.x[10]);
8750 outs() << " x11 " << format("0x%016" PRIx64, cpu64.x[11]) << "\n";
8751 outs() << "\t x12 " << format("0x%016" PRIx64, cpu64.x[12]);
8752 outs() << " x13 " << format("0x%016" PRIx64, cpu64.x[13]);
8753 outs() << " x14 " << format("0x%016" PRIx64, cpu64.x[14]) << "\n";
8754 outs() << "\t x15 " << format("0x%016" PRIx64, cpu64.x[15]);
8755 outs() << " x16 " << format("0x%016" PRIx64, cpu64.x[16]);
8756 outs() << " x17 " << format("0x%016" PRIx64, cpu64.x[17]) << "\n";
8757 outs() << "\t x18 " << format("0x%016" PRIx64, cpu64.x[18]);
8758 outs() << " x19 " << format("0x%016" PRIx64, cpu64.x[19]);
8759 outs() << " x20 " << format("0x%016" PRIx64, cpu64.x[20]) << "\n";
8760 outs() << "\t x21 " << format("0x%016" PRIx64, cpu64.x[21]);
8761 outs() << " x22 " << format("0x%016" PRIx64, cpu64.x[22]);
8762 outs() << " x23 " << format("0x%016" PRIx64, cpu64.x[23]) << "\n";
8763 outs() << "\t x24 " << format("0x%016" PRIx64, cpu64.x[24]);
8764 outs() << " x25 " << format("0x%016" PRIx64, cpu64.x[25]);
8765 outs() << " x26 " << format("0x%016" PRIx64, cpu64.x[26]) << "\n";
8766 outs() << "\t x27 " << format("0x%016" PRIx64, cpu64.x[27]);
8767 outs() << " x28 " << format("0x%016" PRIx64, cpu64.x[28]);
8768 outs() << " fp " << format("0x%016" PRIx64, cpu64.fp) << "\n";
8769 outs() << "\t lr " << format("0x%016" PRIx64, cpu64.lr);
8770 outs() << " sp " << format("0x%016" PRIx64, cpu64.sp);
8771 outs() << " pc " << format("0x%016" PRIx64, cpu64.pc) << "\n";
8772 outs() << "\t cpsr " << format("0x%08" PRIx32, cpu64.cpsr) << "\n";
8775 static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
8776 bool isLittleEndian, uint32_t cputype) {
8777 if (t.cmd == MachO::LC_THREAD)
8778 outs() << " cmd LC_THREAD\n";
8779 else if (t.cmd == MachO::LC_UNIXTHREAD)
8780 outs() << " cmd LC_UNIXTHREAD\n";
8782 outs() << " cmd " << t.cmd << " (unknown)\n";
8783 outs() << " cmdsize " << t.cmdsize;
8784 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
8785 outs() << " Incorrect size\n";
8789 const char *begin = Ptr + sizeof(struct MachO::thread_command);
8790 const char *end = Ptr + t.cmdsize;
8791 uint32_t flavor, count, left;
8792 if (cputype == MachO::CPU_TYPE_I386) {
8793 while (begin < end) {
8794 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8795 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8796 begin += sizeof(uint32_t);
8801 if (isLittleEndian != sys::IsLittleEndianHost)
8802 sys::swapByteOrder(flavor);
8803 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8804 memcpy((char *)&count, begin, sizeof(uint32_t));
8805 begin += sizeof(uint32_t);
8810 if (isLittleEndian != sys::IsLittleEndianHost)
8811 sys::swapByteOrder(count);
8812 if (flavor == MachO::x86_THREAD_STATE32) {
8813 outs() << " flavor i386_THREAD_STATE\n";
8814 if (count == MachO::x86_THREAD_STATE32_COUNT)
8815 outs() << " count i386_THREAD_STATE_COUNT\n";
8817 outs() << " count " << count
8818 << " (not x86_THREAD_STATE32_COUNT)\n";
8819 MachO::x86_thread_state32_t cpu32;
8821 if (left >= sizeof(MachO::x86_thread_state32_t)) {
8822 memcpy(&cpu32, begin, sizeof(MachO::x86_thread_state32_t));
8823 begin += sizeof(MachO::x86_thread_state32_t);
8825 memset(&cpu32, '\0', sizeof(MachO::x86_thread_state32_t));
8826 memcpy(&cpu32, begin, left);
8829 if (isLittleEndian != sys::IsLittleEndianHost)
8831 Print_x86_thread_state32_t(cpu32);
8832 } else if (flavor == MachO::x86_THREAD_STATE) {
8833 outs() << " flavor x86_THREAD_STATE\n";
8834 if (count == MachO::x86_THREAD_STATE_COUNT)
8835 outs() << " count x86_THREAD_STATE_COUNT\n";
8837 outs() << " count " << count
8838 << " (not x86_THREAD_STATE_COUNT)\n";
8839 struct MachO::x86_thread_state_t ts;
8841 if (left >= sizeof(MachO::x86_thread_state_t)) {
8842 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
8843 begin += sizeof(MachO::x86_thread_state_t);
8845 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
8846 memcpy(&ts, begin, left);
8849 if (isLittleEndian != sys::IsLittleEndianHost)
8851 if (ts.tsh.flavor == MachO::x86_THREAD_STATE32) {
8852 outs() << "\t tsh.flavor x86_THREAD_STATE32 ";
8853 if (ts.tsh.count == MachO::x86_THREAD_STATE32_COUNT)
8854 outs() << "tsh.count x86_THREAD_STATE32_COUNT\n";
8856 outs() << "tsh.count " << ts.tsh.count
8857 << " (not x86_THREAD_STATE32_COUNT\n";
8858 Print_x86_thread_state32_t(ts.uts.ts32);
8860 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
8861 << ts.tsh.count << "\n";
8864 outs() << " flavor " << flavor << " (unknown)\n";
8865 outs() << " count " << count << "\n";
8866 outs() << " state (unknown)\n";
8867 begin += count * sizeof(uint32_t);
8870 } else if (cputype == MachO::CPU_TYPE_X86_64) {
8871 while (begin < end) {
8872 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8873 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8874 begin += sizeof(uint32_t);
8879 if (isLittleEndian != sys::IsLittleEndianHost)
8880 sys::swapByteOrder(flavor);
8881 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8882 memcpy((char *)&count, begin, sizeof(uint32_t));
8883 begin += sizeof(uint32_t);
8888 if (isLittleEndian != sys::IsLittleEndianHost)
8889 sys::swapByteOrder(count);
8890 if (flavor == MachO::x86_THREAD_STATE64) {
8891 outs() << " flavor x86_THREAD_STATE64\n";
8892 if (count == MachO::x86_THREAD_STATE64_COUNT)
8893 outs() << " count x86_THREAD_STATE64_COUNT\n";
8895 outs() << " count " << count
8896 << " (not x86_THREAD_STATE64_COUNT)\n";
8897 MachO::x86_thread_state64_t cpu64;
8899 if (left >= sizeof(MachO::x86_thread_state64_t)) {
8900 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
8901 begin += sizeof(MachO::x86_thread_state64_t);
8903 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
8904 memcpy(&cpu64, begin, left);
8907 if (isLittleEndian != sys::IsLittleEndianHost)
8909 Print_x86_thread_state64_t(cpu64);
8910 } else if (flavor == MachO::x86_THREAD_STATE) {
8911 outs() << " flavor x86_THREAD_STATE\n";
8912 if (count == MachO::x86_THREAD_STATE_COUNT)
8913 outs() << " count x86_THREAD_STATE_COUNT\n";
8915 outs() << " count " << count
8916 << " (not x86_THREAD_STATE_COUNT)\n";
8917 struct MachO::x86_thread_state_t ts;
8919 if (left >= sizeof(MachO::x86_thread_state_t)) {
8920 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
8921 begin += sizeof(MachO::x86_thread_state_t);
8923 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
8924 memcpy(&ts, begin, left);
8927 if (isLittleEndian != sys::IsLittleEndianHost)
8929 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
8930 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
8931 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
8932 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
8934 outs() << "tsh.count " << ts.tsh.count
8935 << " (not x86_THREAD_STATE64_COUNT\n";
8936 Print_x86_thread_state64_t(ts.uts.ts64);
8938 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
8939 << ts.tsh.count << "\n";
8941 } else if (flavor == MachO::x86_FLOAT_STATE) {
8942 outs() << " flavor x86_FLOAT_STATE\n";
8943 if (count == MachO::x86_FLOAT_STATE_COUNT)
8944 outs() << " count x86_FLOAT_STATE_COUNT\n";
8946 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
8947 struct MachO::x86_float_state_t fs;
8949 if (left >= sizeof(MachO::x86_float_state_t)) {
8950 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
8951 begin += sizeof(MachO::x86_float_state_t);
8953 memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
8954 memcpy(&fs, begin, left);
8957 if (isLittleEndian != sys::IsLittleEndianHost)
8959 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
8960 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
8961 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
8962 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
8964 outs() << "fsh.count " << fs.fsh.count
8965 << " (not x86_FLOAT_STATE64_COUNT\n";
8966 Print_x86_float_state_t(fs.ufs.fs64);
8968 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
8969 << fs.fsh.count << "\n";
8971 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
8972 outs() << " flavor x86_EXCEPTION_STATE\n";
8973 if (count == MachO::x86_EXCEPTION_STATE_COUNT)
8974 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
8976 outs() << " count " << count
8977 << " (not x86_EXCEPTION_STATE_COUNT)\n";
8978 struct MachO::x86_exception_state_t es;
8980 if (left >= sizeof(MachO::x86_exception_state_t)) {
8981 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
8982 begin += sizeof(MachO::x86_exception_state_t);
8984 memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
8985 memcpy(&es, begin, left);
8988 if (isLittleEndian != sys::IsLittleEndianHost)
8990 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
8991 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
8992 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
8993 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
8995 outs() << "\t esh.count " << es.esh.count
8996 << " (not x86_EXCEPTION_STATE64_COUNT\n";
8997 Print_x86_exception_state_t(es.ues.es64);
8999 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
9000 << es.esh.count << "\n";
9003 outs() << " flavor " << flavor << " (unknown)\n";
9004 outs() << " count " << count << "\n";
9005 outs() << " state (unknown)\n";
9006 begin += count * sizeof(uint32_t);
9009 } else if (cputype == MachO::CPU_TYPE_ARM) {
9010 while (begin < end) {
9011 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9012 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9013 begin += sizeof(uint32_t);
9018 if (isLittleEndian != sys::IsLittleEndianHost)
9019 sys::swapByteOrder(flavor);
9020 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9021 memcpy((char *)&count, begin, sizeof(uint32_t));
9022 begin += sizeof(uint32_t);
9027 if (isLittleEndian != sys::IsLittleEndianHost)
9028 sys::swapByteOrder(count);
9029 if (flavor == MachO::ARM_THREAD_STATE) {
9030 outs() << " flavor ARM_THREAD_STATE\n";
9031 if (count == MachO::ARM_THREAD_STATE_COUNT)
9032 outs() << " count ARM_THREAD_STATE_COUNT\n";
9034 outs() << " count " << count
9035 << " (not ARM_THREAD_STATE_COUNT)\n";
9036 MachO::arm_thread_state32_t cpu32;
9038 if (left >= sizeof(MachO::arm_thread_state32_t)) {
9039 memcpy(&cpu32, begin, sizeof(MachO::arm_thread_state32_t));
9040 begin += sizeof(MachO::arm_thread_state32_t);
9042 memset(&cpu32, '\0', sizeof(MachO::arm_thread_state32_t));
9043 memcpy(&cpu32, begin, left);
9046 if (isLittleEndian != sys::IsLittleEndianHost)
9048 Print_arm_thread_state32_t(cpu32);
9050 outs() << " flavor " << flavor << " (unknown)\n";
9051 outs() << " count " << count << "\n";
9052 outs() << " state (unknown)\n";
9053 begin += count * sizeof(uint32_t);
9056 } else if (cputype == MachO::CPU_TYPE_ARM64) {
9057 while (begin < end) {
9058 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9059 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9060 begin += sizeof(uint32_t);
9065 if (isLittleEndian != sys::IsLittleEndianHost)
9066 sys::swapByteOrder(flavor);
9067 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9068 memcpy((char *)&count, begin, sizeof(uint32_t));
9069 begin += sizeof(uint32_t);
9074 if (isLittleEndian != sys::IsLittleEndianHost)
9075 sys::swapByteOrder(count);
9076 if (flavor == MachO::ARM_THREAD_STATE64) {
9077 outs() << " flavor ARM_THREAD_STATE64\n";
9078 if (count == MachO::ARM_THREAD_STATE64_COUNT)
9079 outs() << " count ARM_THREAD_STATE64_COUNT\n";
9081 outs() << " count " << count
9082 << " (not ARM_THREAD_STATE64_COUNT)\n";
9083 MachO::arm_thread_state64_t cpu64;
9085 if (left >= sizeof(MachO::arm_thread_state64_t)) {
9086 memcpy(&cpu64, begin, sizeof(MachO::arm_thread_state64_t));
9087 begin += sizeof(MachO::arm_thread_state64_t);
9089 memset(&cpu64, '\0', sizeof(MachO::arm_thread_state64_t));
9090 memcpy(&cpu64, begin, left);
9093 if (isLittleEndian != sys::IsLittleEndianHost)
9095 Print_arm_thread_state64_t(cpu64);
9097 outs() << " flavor " << flavor << " (unknown)\n";
9098 outs() << " count " << count << "\n";
9099 outs() << " state (unknown)\n";
9100 begin += count * sizeof(uint32_t);
9104 while (begin < end) {
9105 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9106 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9107 begin += sizeof(uint32_t);
9112 if (isLittleEndian != sys::IsLittleEndianHost)
9113 sys::swapByteOrder(flavor);
9114 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9115 memcpy((char *)&count, begin, sizeof(uint32_t));
9116 begin += sizeof(uint32_t);
9121 if (isLittleEndian != sys::IsLittleEndianHost)
9122 sys::swapByteOrder(count);
9123 outs() << " flavor " << flavor << "\n";
9124 outs() << " count " << count << "\n";
9125 outs() << " state (Unknown cputype/cpusubtype)\n";
9126 begin += count * sizeof(uint32_t);
9131 static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
9132 if (dl.cmd == MachO::LC_ID_DYLIB)
9133 outs() << " cmd LC_ID_DYLIB\n";
9134 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
9135 outs() << " cmd LC_LOAD_DYLIB\n";
9136 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
9137 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
9138 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
9139 outs() << " cmd LC_REEXPORT_DYLIB\n";
9140 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
9141 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
9142 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
9143 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
9145 outs() << " cmd " << dl.cmd << " (unknown)\n";
9146 outs() << " cmdsize " << dl.cmdsize;
9147 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
9148 outs() << " Incorrect size\n";
9151 if (dl.dylib.name < dl.cmdsize) {
9152 const char *P = (const char *)(Ptr) + dl.dylib.name;
9153 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
9155 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
9157 outs() << " time stamp " << dl.dylib.timestamp << " ";
9158 time_t t = dl.dylib.timestamp;
9159 outs() << ctime(&t);
9160 outs() << " current version ";
9161 if (dl.dylib.current_version == 0xffffffff)
9164 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
9165 << ((dl.dylib.current_version >> 8) & 0xff) << "."
9166 << (dl.dylib.current_version & 0xff) << "\n";
9167 outs() << "compatibility version ";
9168 if (dl.dylib.compatibility_version == 0xffffffff)
9171 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
9172 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
9173 << (dl.dylib.compatibility_version & 0xff) << "\n";
9176 static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
9177 uint32_t object_size) {
9178 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
9179 outs() << " cmd LC_CODE_SIGNATURE\n";
9180 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
9181 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
9182 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
9183 outs() << " cmd LC_FUNCTION_STARTS\n";
9184 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
9185 outs() << " cmd LC_DATA_IN_CODE\n";
9186 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
9187 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
9188 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
9189 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
9191 outs() << " cmd " << ld.cmd << " (?)\n";
9192 outs() << " cmdsize " << ld.cmdsize;
9193 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
9194 outs() << " Incorrect size\n";
9197 outs() << " dataoff " << ld.dataoff;
9198 if (ld.dataoff > object_size)
9199 outs() << " (past end of file)\n";
9202 outs() << " datasize " << ld.datasize;
9203 uint64_t big_size = ld.dataoff;
9204 big_size += ld.datasize;
9205 if (big_size > object_size)
9206 outs() << " (past end of file)\n";
9211 static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
9212 uint32_t cputype, bool verbose) {
9213 StringRef Buf = Obj->getData();
9215 for (const auto &Command : Obj->load_commands()) {
9216 outs() << "Load command " << Index++ << "\n";
9217 if (Command.C.cmd == MachO::LC_SEGMENT) {
9218 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
9219 const char *sg_segname = SLC.segname;
9220 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
9221 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
9222 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
9224 for (unsigned j = 0; j < SLC.nsects; j++) {
9225 MachO::section S = Obj->getSection(Command, j);
9226 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
9227 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
9228 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
9230 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
9231 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
9232 const char *sg_segname = SLC_64.segname;
9233 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
9234 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
9235 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
9236 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
9237 for (unsigned j = 0; j < SLC_64.nsects; j++) {
9238 MachO::section_64 S_64 = Obj->getSection64(Command, j);
9239 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
9240 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
9241 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
9242 sg_segname, filetype, Buf.size(), verbose);
9244 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
9245 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
9246 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
9247 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
9248 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
9249 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
9250 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
9252 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
9253 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
9254 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
9255 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
9256 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
9257 Command.C.cmd == MachO::LC_ID_DYLINKER ||
9258 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
9259 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
9260 PrintDyldLoadCommand(Dyld, Command.Ptr);
9261 } else if (Command.C.cmd == MachO::LC_UUID) {
9262 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
9263 PrintUuidLoadCommand(Uuid);
9264 } else if (Command.C.cmd == MachO::LC_RPATH) {
9265 MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
9266 PrintRpathLoadCommand(Rpath, Command.Ptr);
9267 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
9268 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS ||
9269 Command.C.cmd == MachO::LC_VERSION_MIN_TVOS ||
9270 Command.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) {
9271 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
9272 PrintVersionMinLoadCommand(Vd);
9273 } else if (Command.C.cmd == MachO::LC_NOTE) {
9274 MachO::note_command Nt = Obj->getNoteLoadCommand(Command);
9275 PrintNoteLoadCommand(Nt);
9276 } else if (Command.C.cmd == MachO::LC_BUILD_VERSION) {
9277 MachO::build_version_command Bv =
9278 Obj->getBuildVersionLoadCommand(Command);
9279 PrintBuildVersionLoadCommand(Obj, Bv);
9280 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
9281 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
9282 PrintSourceVersionCommand(Sd);
9283 } else if (Command.C.cmd == MachO::LC_MAIN) {
9284 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
9285 PrintEntryPointCommand(Ep);
9286 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
9287 MachO::encryption_info_command Ei =
9288 Obj->getEncryptionInfoCommand(Command);
9289 PrintEncryptionInfoCommand(Ei, Buf.size());
9290 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
9291 MachO::encryption_info_command_64 Ei =
9292 Obj->getEncryptionInfoCommand64(Command);
9293 PrintEncryptionInfoCommand64(Ei, Buf.size());
9294 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
9295 MachO::linker_option_command Lo =
9296 Obj->getLinkerOptionLoadCommand(Command);
9297 PrintLinkerOptionCommand(Lo, Command.Ptr);
9298 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
9299 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
9300 PrintSubFrameworkCommand(Sf, Command.Ptr);
9301 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
9302 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
9303 PrintSubUmbrellaCommand(Sf, Command.Ptr);
9304 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
9305 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
9306 PrintSubLibraryCommand(Sl, Command.Ptr);
9307 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
9308 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
9309 PrintSubClientCommand(Sc, Command.Ptr);
9310 } else if (Command.C.cmd == MachO::LC_ROUTINES) {
9311 MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
9312 PrintRoutinesCommand(Rc);
9313 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
9314 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
9315 PrintRoutinesCommand64(Rc);
9316 } else if (Command.C.cmd == MachO::LC_THREAD ||
9317 Command.C.cmd == MachO::LC_UNIXTHREAD) {
9318 MachO::thread_command Tc = Obj->getThreadCommand(Command);
9319 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
9320 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
9321 Command.C.cmd == MachO::LC_ID_DYLIB ||
9322 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
9323 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
9324 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
9325 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
9326 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
9327 PrintDylibCommand(Dl, Command.Ptr);
9328 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
9329 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
9330 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
9331 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
9332 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
9333 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
9334 MachO::linkedit_data_command Ld =
9335 Obj->getLinkeditDataLoadCommand(Command);
9336 PrintLinkEditDataCommand(Ld, Buf.size());
9338 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
9340 outs() << " cmdsize " << Command.C.cmdsize << "\n";
9341 // TODO: get and print the raw bytes of the load command.
9343 // TODO: print all the other kinds of load commands.
9347 static void PrintMachHeader(const MachOObjectFile *Obj, bool verbose) {
9348 if (Obj->is64Bit()) {
9349 MachO::mach_header_64 H_64;
9350 H_64 = Obj->getHeader64();
9351 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
9352 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
9354 MachO::mach_header H;
9355 H = Obj->getHeader();
9356 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
9357 H.sizeofcmds, H.flags, verbose);
9361 void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
9362 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
9363 PrintMachHeader(file, !NonVerbose);
9366 void llvm::printMachOLoadCommands(const object::ObjectFile *Obj) {
9367 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
9368 uint32_t filetype = 0;
9369 uint32_t cputype = 0;
9370 if (file->is64Bit()) {
9371 MachO::mach_header_64 H_64;
9372 H_64 = file->getHeader64();
9373 filetype = H_64.filetype;
9374 cputype = H_64.cputype;
9376 MachO::mach_header H;
9377 H = file->getHeader();
9378 filetype = H.filetype;
9379 cputype = H.cputype;
9381 PrintLoadCommands(file, filetype, cputype, !NonVerbose);
9384 //===----------------------------------------------------------------------===//
9385 // export trie dumping
9386 //===----------------------------------------------------------------------===//
9388 void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
9389 uint64_t BaseSegmentAddress = 0;
9390 for (const auto &Command : Obj->load_commands()) {
9391 if (Command.C.cmd == MachO::LC_SEGMENT) {
9392 MachO::segment_command Seg = Obj->getSegmentLoadCommand(Command);
9393 if (Seg.fileoff == 0 && Seg.filesize != 0) {
9394 BaseSegmentAddress = Seg.vmaddr;
9397 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
9398 MachO::segment_command_64 Seg = Obj->getSegment64LoadCommand(Command);
9399 if (Seg.fileoff == 0 && Seg.filesize != 0) {
9400 BaseSegmentAddress = Seg.vmaddr;
9405 for (const llvm::object::ExportEntry &Entry : Obj->exports()) {
9406 uint64_t Flags = Entry.flags();
9407 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
9408 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
9409 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
9410 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
9411 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
9412 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
9413 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
9415 outs() << "[re-export] ";
9417 outs() << format("0x%08llX ",
9418 Entry.address() + BaseSegmentAddress);
9419 outs() << Entry.name();
9420 if (WeakDef || ThreadLocal || Resolver || Abs) {
9421 bool NeedsComma = false;
9424 outs() << "weak_def";
9430 outs() << "per-thread";
9436 outs() << "absolute";
9442 outs() << format("resolver=0x%08llX", Entry.other());
9448 StringRef DylibName = "unknown";
9449 int Ordinal = Entry.other() - 1;
9450 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
9451 if (Entry.otherName().empty())
9452 outs() << " (from " << DylibName << ")";
9454 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
9460 //===----------------------------------------------------------------------===//
9461 // rebase table dumping
9462 //===----------------------------------------------------------------------===//
9464 void llvm::printMachORebaseTable(object::MachOObjectFile *Obj) {
9465 outs() << "segment section address type\n";
9466 Error Err = Error::success();
9467 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable(Err)) {
9468 StringRef SegmentName = Entry.segmentName();
9469 StringRef SectionName = Entry.sectionName();
9470 uint64_t Address = Entry.address();
9472 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
9473 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
9474 SegmentName.str().c_str(), SectionName.str().c_str(),
9475 Address, Entry.typeName().str().c_str());
9478 report_error(Obj->getFileName(), std::move(Err));
9481 static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
9482 StringRef DylibName;
9484 case MachO::BIND_SPECIAL_DYLIB_SELF:
9485 return "this-image";
9486 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
9487 return "main-executable";
9488 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
9489 return "flat-namespace";
9492 std::error_code EC =
9493 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
9495 return "<<bad library ordinal>>";
9499 return "<<unknown special ordinal>>";
9502 //===----------------------------------------------------------------------===//
9503 // bind table dumping
9504 //===----------------------------------------------------------------------===//
9506 void llvm::printMachOBindTable(object::MachOObjectFile *Obj) {
9507 // Build table of sections so names can used in final output.
9508 outs() << "segment section address type "
9509 "addend dylib symbol\n";
9510 Error Err = Error::success();
9511 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable(Err)) {
9512 StringRef SegmentName = Entry.segmentName();
9513 StringRef SectionName = Entry.sectionName();
9514 uint64_t Address = Entry.address();
9516 // Table lines look like:
9517 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
9519 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
9520 Attr = " (weak_import)";
9521 outs() << left_justify(SegmentName, 8) << " "
9522 << left_justify(SectionName, 18) << " "
9523 << format_hex(Address, 10, true) << " "
9524 << left_justify(Entry.typeName(), 8) << " "
9525 << format_decimal(Entry.addend(), 8) << " "
9526 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
9527 << Entry.symbolName() << Attr << "\n";
9530 report_error(Obj->getFileName(), std::move(Err));
9533 //===----------------------------------------------------------------------===//
9534 // lazy bind table dumping
9535 //===----------------------------------------------------------------------===//
9537 void llvm::printMachOLazyBindTable(object::MachOObjectFile *Obj) {
9538 outs() << "segment section address "
9540 Error Err = Error::success();
9541 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable(Err)) {
9542 StringRef SegmentName = Entry.segmentName();
9543 StringRef SectionName = Entry.sectionName();
9544 uint64_t Address = Entry.address();
9546 // Table lines look like:
9547 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
9548 outs() << left_justify(SegmentName, 8) << " "
9549 << left_justify(SectionName, 18) << " "
9550 << format_hex(Address, 10, true) << " "
9551 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
9552 << Entry.symbolName() << "\n";
9555 report_error(Obj->getFileName(), std::move(Err));
9558 //===----------------------------------------------------------------------===//
9559 // weak bind table dumping
9560 //===----------------------------------------------------------------------===//
9562 void llvm::printMachOWeakBindTable(object::MachOObjectFile *Obj) {
9563 outs() << "segment section address "
9564 "type addend symbol\n";
9565 Error Err = Error::success();
9566 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable(Err)) {
9567 // Strong symbols don't have a location to update.
9568 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
9569 outs() << " strong "
9570 << Entry.symbolName() << "\n";
9573 StringRef SegmentName = Entry.segmentName();
9574 StringRef SectionName = Entry.sectionName();
9575 uint64_t Address = Entry.address();
9577 // Table lines look like:
9578 // __DATA __data 0x00001000 pointer 0 _foo
9579 outs() << left_justify(SegmentName, 8) << " "
9580 << left_justify(SectionName, 18) << " "
9581 << format_hex(Address, 10, true) << " "
9582 << left_justify(Entry.typeName(), 8) << " "
9583 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
9587 report_error(Obj->getFileName(), std::move(Err));
9590 // get_dyld_bind_info_symbolname() is used for disassembly and passed an
9591 // address, ReferenceValue, in the Mach-O file and looks in the dyld bind
9592 // information for that address. If the address is found its binding symbol
9593 // name is returned. If not nullptr is returned.
9594 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
9595 struct DisassembleInfo *info) {
9596 if (info->bindtable == nullptr) {
9597 info->bindtable = llvm::make_unique<SymbolAddressMap>();
9598 Error Err = Error::success();
9599 for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable(Err)) {
9600 uint64_t Address = Entry.address();
9601 StringRef name = Entry.symbolName();
9603 (*info->bindtable)[Address] = name;
9606 report_error(info->O->getFileName(), std::move(Err));
9608 auto name = info->bindtable->lookup(ReferenceValue);
9609 return !name.empty() ? name.data() : nullptr;