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 ArchiveMemberOffsets("archive-member-offsets",
80 cl::desc("Print the offset to each archive member for "
81 "Mach-O archives (requires -macho and "
82 "-archive-headers)"));
85 llvm::IndirectSymbols("indirect-symbols",
86 cl::desc("Print indirect symbol table for Mach-O "
87 "objects (requires -macho)"));
90 llvm::DataInCode("data-in-code",
91 cl::desc("Print the data in code table for Mach-O objects "
92 "(requires -macho)"));
95 llvm::LinkOptHints("link-opt-hints",
96 cl::desc("Print the linker optimization hints for "
97 "Mach-O objects (requires -macho)"));
100 llvm::InfoPlist("info-plist",
101 cl::desc("Print the info plist section as strings for "
102 "Mach-O objects (requires -macho)"));
105 llvm::DylibsUsed("dylibs-used",
106 cl::desc("Print the shared libraries used for linked "
107 "Mach-O files (requires -macho)"));
110 llvm::DylibId("dylib-id",
111 cl::desc("Print the shared library's id for the dylib Mach-O "
112 "file (requires -macho)"));
115 llvm::NonVerbose("non-verbose",
116 cl::desc("Print the info for Mach-O objects in "
117 "non-verbose or numeric form (requires -macho)"));
120 llvm::ObjcMetaData("objc-meta-data",
121 cl::desc("Print the Objective-C runtime meta data for "
122 "Mach-O files (requires -macho)"));
124 cl::opt
<std::string
> llvm::DisSymName(
126 cl::desc("disassemble just this symbol's instructions (requires -macho)"));
128 static cl::opt
<bool> NoSymbolicOperands(
129 "no-symbolic-operands",
130 cl::desc("do not symbolic operands when disassembling (requires -macho)"));
132 static cl::list
<std::string
>
133 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
136 bool ArchAll
= false;
138 static std::string ThumbTripleName
;
140 static const Target
*GetTarget(const MachOObjectFile
*MachOObj
,
141 const char **McpuDefault
,
142 const Target
**ThumbTarget
) {
143 // Figure out the target triple.
144 llvm::Triple
TT(TripleName
);
145 if (TripleName
.empty()) {
146 TT
= MachOObj
->getArchTriple(McpuDefault
);
147 TripleName
= TT
.str();
150 if (TT
.getArch() == Triple::arm
) {
151 // We've inferred a 32-bit ARM target from the object file. All MachO CPUs
152 // that support ARM are also capable of Thumb mode.
153 llvm::Triple ThumbTriple
= TT
;
154 std::string ThumbName
= (Twine("thumb") + TT
.getArchName().substr(3)).str();
155 ThumbTriple
.setArchName(ThumbName
);
156 ThumbTripleName
= ThumbTriple
.str();
159 // Get the target specific parser.
161 const Target
*TheTarget
= TargetRegistry::lookupTarget(TripleName
, Error
);
162 if (TheTarget
&& ThumbTripleName
.empty())
165 *ThumbTarget
= TargetRegistry::lookupTarget(ThumbTripleName
, Error
);
169 errs() << "llvm-objdump: error: unable to get target for '";
171 errs() << TripleName
;
173 errs() << ThumbTripleName
;
174 errs() << "', see --version and --triple.\n";
178 struct SymbolSorter
{
179 bool operator()(const SymbolRef
&A
, const SymbolRef
&B
) {
180 Expected
<SymbolRef::Type
> ATypeOrErr
= A
.getType();
182 report_error(A
.getObject()->getFileName(), ATypeOrErr
.takeError());
183 SymbolRef::Type AType
= *ATypeOrErr
;
184 Expected
<SymbolRef::Type
> BTypeOrErr
= B
.getType();
186 report_error(B
.getObject()->getFileName(), BTypeOrErr
.takeError());
187 SymbolRef::Type BType
= *BTypeOrErr
;
188 uint64_t AAddr
= (AType
!= SymbolRef::ST_Function
) ? 0 : A
.getValue();
189 uint64_t BAddr
= (BType
!= SymbolRef::ST_Function
) ? 0 : B
.getValue();
190 return AAddr
< BAddr
;
194 // Types for the storted data in code table that is built before disassembly
195 // and the predicate function to sort them.
196 typedef std::pair
<uint64_t, DiceRef
> DiceTableEntry
;
197 typedef std::vector
<DiceTableEntry
> DiceTable
;
198 typedef DiceTable::iterator dice_table_iterator
;
202 struct ScopedXarFile
{
204 ScopedXarFile(const char *filename
, int32_t flags
)
205 : xar(xar_open(filename
, flags
)) {}
210 ScopedXarFile(const ScopedXarFile
&) = delete;
211 ScopedXarFile
&operator=(const ScopedXarFile
&) = delete;
212 operator xar_t() { return xar
; }
215 struct ScopedXarIter
{
217 ScopedXarIter() : iter(xar_iter_new()) {}
222 ScopedXarIter(const ScopedXarIter
&) = delete;
223 ScopedXarIter
&operator=(const ScopedXarIter
&) = delete;
224 operator xar_iter_t() { return iter
; }
227 #endif // defined(HAVE_LIBXAR)
229 // This is used to search for a data in code table entry for the PC being
230 // disassembled. The j parameter has the PC in j.first. A single data in code
231 // table entry can cover many bytes for each of its Kind's. So if the offset,
232 // aka the i.first value, of the data in code table entry plus its Length
233 // covers the PC being searched for this will return true. If not it will
235 static bool compareDiceTableEntries(const DiceTableEntry
&i
,
236 const DiceTableEntry
&j
) {
238 i
.second
.getLength(Length
);
240 return j
.first
>= i
.first
&& j
.first
< i
.first
+ Length
;
243 static uint64_t DumpDataInCode(const uint8_t *bytes
, uint64_t Length
,
244 unsigned short Kind
) {
245 uint32_t Value
, Size
= 1;
249 case MachO::DICE_KIND_DATA
:
252 dumpBytes(makeArrayRef(bytes
, 4), outs());
253 Value
= bytes
[3] << 24 | bytes
[2] << 16 | bytes
[1] << 8 | bytes
[0];
254 outs() << "\t.long " << Value
;
256 } else if (Length
>= 2) {
258 dumpBytes(makeArrayRef(bytes
, 2), outs());
259 Value
= bytes
[1] << 8 | bytes
[0];
260 outs() << "\t.short " << Value
;
264 dumpBytes(makeArrayRef(bytes
, 2), outs());
266 outs() << "\t.byte " << Value
;
269 if (Kind
== MachO::DICE_KIND_DATA
)
270 outs() << "\t@ KIND_DATA\n";
272 outs() << "\t@ data in code kind = " << Kind
<< "\n";
274 case MachO::DICE_KIND_JUMP_TABLE8
:
276 dumpBytes(makeArrayRef(bytes
, 1), outs());
278 outs() << "\t.byte " << format("%3u", Value
) << "\t@ KIND_JUMP_TABLE8\n";
281 case MachO::DICE_KIND_JUMP_TABLE16
:
283 dumpBytes(makeArrayRef(bytes
, 2), outs());
284 Value
= bytes
[1] << 8 | bytes
[0];
285 outs() << "\t.short " << format("%5u", Value
& 0xffff)
286 << "\t@ KIND_JUMP_TABLE16\n";
289 case MachO::DICE_KIND_JUMP_TABLE32
:
290 case MachO::DICE_KIND_ABS_JUMP_TABLE32
:
292 dumpBytes(makeArrayRef(bytes
, 4), outs());
293 Value
= bytes
[3] << 24 | bytes
[2] << 16 | bytes
[1] << 8 | bytes
[0];
294 outs() << "\t.long " << Value
;
295 if (Kind
== MachO::DICE_KIND_JUMP_TABLE32
)
296 outs() << "\t@ KIND_JUMP_TABLE32\n";
298 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
305 static void getSectionsAndSymbols(MachOObjectFile
*MachOObj
,
306 std::vector
<SectionRef
> &Sections
,
307 std::vector
<SymbolRef
> &Symbols
,
308 SmallVectorImpl
<uint64_t> &FoundFns
,
309 uint64_t &BaseSegmentAddress
) {
310 for (const SymbolRef
&Symbol
: MachOObj
->symbols()) {
311 Expected
<StringRef
> SymName
= Symbol
.getName();
313 report_error(MachOObj
->getFileName(), SymName
.takeError());
314 if (!SymName
->startswith("ltmp"))
315 Symbols
.push_back(Symbol
);
318 for (const SectionRef
&Section
: MachOObj
->sections()) {
320 Section
.getName(SectName
);
321 Sections
.push_back(Section
);
324 bool BaseSegmentAddressSet
= false;
325 for (const auto &Command
: MachOObj
->load_commands()) {
326 if (Command
.C
.cmd
== MachO::LC_FUNCTION_STARTS
) {
327 // We found a function starts segment, parse the addresses for later
329 MachO::linkedit_data_command LLC
=
330 MachOObj
->getLinkeditDataLoadCommand(Command
);
332 MachOObj
->ReadULEB128s(LLC
.dataoff
, FoundFns
);
333 } else if (Command
.C
.cmd
== MachO::LC_SEGMENT
) {
334 MachO::segment_command SLC
= MachOObj
->getSegmentLoadCommand(Command
);
335 StringRef SegName
= SLC
.segname
;
336 if (!BaseSegmentAddressSet
&& SegName
!= "__PAGEZERO") {
337 BaseSegmentAddressSet
= true;
338 BaseSegmentAddress
= SLC
.vmaddr
;
344 static void PrintIndirectSymbolTable(MachOObjectFile
*O
, bool verbose
,
345 uint32_t n
, uint32_t count
,
346 uint32_t stride
, uint64_t addr
) {
347 MachO::dysymtab_command Dysymtab
= O
->getDysymtabLoadCommand();
348 uint32_t nindirectsyms
= Dysymtab
.nindirectsyms
;
349 if (n
> nindirectsyms
)
350 outs() << " (entries start past the end of the indirect symbol "
351 "table) (reserved1 field greater than the table size)";
352 else if (n
+ count
> nindirectsyms
)
353 outs() << " (entries extends past the end of the indirect symbol "
356 uint32_t cputype
= O
->getHeader().cputype
;
357 if (cputype
& MachO::CPU_ARCH_ABI64
)
358 outs() << "address index";
360 outs() << "address index";
365 for (uint32_t j
= 0; j
< count
&& n
+ j
< nindirectsyms
; j
++) {
366 if (cputype
& MachO::CPU_ARCH_ABI64
)
367 outs() << format("0x%016" PRIx64
, addr
+ j
* stride
) << " ";
369 outs() << format("0x%08" PRIx32
, (uint32_t)addr
+ j
* stride
) << " ";
370 MachO::dysymtab_command Dysymtab
= O
->getDysymtabLoadCommand();
371 uint32_t indirect_symbol
= O
->getIndirectSymbolTableEntry(Dysymtab
, n
+ j
);
372 if (indirect_symbol
== MachO::INDIRECT_SYMBOL_LOCAL
) {
376 if (indirect_symbol
==
377 (MachO::INDIRECT_SYMBOL_LOCAL
| MachO::INDIRECT_SYMBOL_ABS
)) {
378 outs() << "LOCAL ABSOLUTE\n";
381 if (indirect_symbol
== MachO::INDIRECT_SYMBOL_ABS
) {
382 outs() << "ABSOLUTE\n";
385 outs() << format("%5u ", indirect_symbol
);
387 MachO::symtab_command Symtab
= O
->getSymtabLoadCommand();
388 if (indirect_symbol
< Symtab
.nsyms
) {
389 symbol_iterator Sym
= O
->getSymbolByIndex(indirect_symbol
);
390 SymbolRef Symbol
= *Sym
;
391 Expected
<StringRef
> SymName
= Symbol
.getName();
393 report_error(O
->getFileName(), SymName
.takeError());
403 static void PrintIndirectSymbols(MachOObjectFile
*O
, bool verbose
) {
404 for (const auto &Load
: O
->load_commands()) {
405 if (Load
.C
.cmd
== MachO::LC_SEGMENT_64
) {
406 MachO::segment_command_64 Seg
= O
->getSegment64LoadCommand(Load
);
407 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
408 MachO::section_64 Sec
= O
->getSection64(Load
, J
);
409 uint32_t section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
410 if (section_type
== MachO::S_NON_LAZY_SYMBOL_POINTERS
||
411 section_type
== MachO::S_LAZY_SYMBOL_POINTERS
||
412 section_type
== MachO::S_LAZY_DYLIB_SYMBOL_POINTERS
||
413 section_type
== MachO::S_THREAD_LOCAL_VARIABLE_POINTERS
||
414 section_type
== MachO::S_SYMBOL_STUBS
) {
416 if (section_type
== MachO::S_SYMBOL_STUBS
)
417 stride
= Sec
.reserved2
;
421 outs() << "Can't print indirect symbols for (" << Sec
.segname
<< ","
422 << Sec
.sectname
<< ") "
423 << "(size of stubs in reserved2 field is zero)\n";
426 uint32_t count
= Sec
.size
/ stride
;
427 outs() << "Indirect symbols for (" << Sec
.segname
<< ","
428 << Sec
.sectname
<< ") " << count
<< " entries";
429 uint32_t n
= Sec
.reserved1
;
430 PrintIndirectSymbolTable(O
, verbose
, n
, count
, stride
, Sec
.addr
);
433 } else if (Load
.C
.cmd
== MachO::LC_SEGMENT
) {
434 MachO::segment_command Seg
= O
->getSegmentLoadCommand(Load
);
435 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
436 MachO::section Sec
= O
->getSection(Load
, J
);
437 uint32_t section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
438 if (section_type
== MachO::S_NON_LAZY_SYMBOL_POINTERS
||
439 section_type
== MachO::S_LAZY_SYMBOL_POINTERS
||
440 section_type
== MachO::S_LAZY_DYLIB_SYMBOL_POINTERS
||
441 section_type
== MachO::S_THREAD_LOCAL_VARIABLE_POINTERS
||
442 section_type
== MachO::S_SYMBOL_STUBS
) {
444 if (section_type
== MachO::S_SYMBOL_STUBS
)
445 stride
= Sec
.reserved2
;
449 outs() << "Can't print indirect symbols for (" << Sec
.segname
<< ","
450 << Sec
.sectname
<< ") "
451 << "(size of stubs in reserved2 field is zero)\n";
454 uint32_t count
= Sec
.size
/ stride
;
455 outs() << "Indirect symbols for (" << Sec
.segname
<< ","
456 << Sec
.sectname
<< ") " << count
<< " entries";
457 uint32_t n
= Sec
.reserved1
;
458 PrintIndirectSymbolTable(O
, verbose
, n
, count
, stride
, Sec
.addr
);
465 static void PrintRType(const uint64_t cputype
, const unsigned r_type
) {
466 static char const *generic_r_types
[] = {
467 "VANILLA ", "PAIR ", "SECTDIF ", "PBLAPTR ", "LOCSDIF ", "TLV ",
468 " 6 (?) ", " 7 (?) ", " 8 (?) ", " 9 (?) ", " 10 (?) ", " 11 (?) ",
469 " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
471 static char const *x86_64_r_types
[] = {
472 "UNSIGND ", "SIGNED ", "BRANCH ", "GOT_LD ", "GOT ", "SUB ",
473 "SIGNED1 ", "SIGNED2 ", "SIGNED4 ", "TLV ", " 10 (?) ", " 11 (?) ",
474 " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
476 static char const *arm_r_types
[] = {
477 "VANILLA ", "PAIR ", "SECTDIFF", "LOCSDIF ", "PBLAPTR ",
478 "BR24 ", "T_BR22 ", "T_BR32 ", "HALF ", "HALFDIF ",
479 " 10 (?) ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
481 static char const *arm64_r_types
[] = {
482 "UNSIGND ", "SUB ", "BR26 ", "PAGE21 ", "PAGOF12 ",
483 "GOTLDP ", "GOTLDPOF", "PTRTGOT ", "TLVLDP ", "TLVLDPOF",
484 "ADDEND ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
488 outs() << format("%-7u", r_type
) << " ";
492 case MachO::CPU_TYPE_I386
:
493 outs() << generic_r_types
[r_type
];
495 case MachO::CPU_TYPE_X86_64
:
496 outs() << x86_64_r_types
[r_type
];
498 case MachO::CPU_TYPE_ARM
:
499 outs() << arm_r_types
[r_type
];
501 case MachO::CPU_TYPE_ARM64
:
502 outs() << arm64_r_types
[r_type
];
505 outs() << format("%-7u ", r_type
);
509 static void PrintRLength(const uint64_t cputype
, const unsigned r_type
,
510 const unsigned r_length
, const bool previous_arm_half
){
511 if (cputype
== MachO::CPU_TYPE_ARM
&&
512 (r_type
== llvm::MachO::ARM_RELOC_HALF
||
513 r_type
== llvm::MachO::ARM_RELOC_HALF_SECTDIFF
||
514 previous_arm_half
== true)) {
515 if ((r_length
& 0x1) == 0)
519 if ((r_length
& 0x1) == 0)
535 if (cputype
== MachO::CPU_TYPE_X86_64
)
538 outs() << format("?(%2d) ", r_length
);
541 outs() << format("?(%2d) ", r_length
);
546 static void PrintRelocationEntries(const MachOObjectFile
*O
,
547 const relocation_iterator Begin
,
548 const relocation_iterator End
,
549 const uint64_t cputype
,
550 const bool verbose
) {
551 const MachO::symtab_command Symtab
= O
->getSymtabLoadCommand();
552 bool previous_arm_half
= false;
553 bool previous_sectdiff
= false;
554 uint32_t sectdiff_r_type
= 0;
556 for (relocation_iterator Reloc
= Begin
; Reloc
!= End
; ++Reloc
) {
557 const DataRefImpl Rel
= Reloc
->getRawDataRefImpl();
558 const MachO::any_relocation_info RE
= O
->getRelocation(Rel
);
559 const unsigned r_type
= O
->getAnyRelocationType(RE
);
560 const bool r_scattered
= O
->isRelocationScattered(RE
);
561 const unsigned r_pcrel
= O
->getAnyRelocationPCRel(RE
);
562 const unsigned r_length
= O
->getAnyRelocationLength(RE
);
563 const unsigned r_address
= O
->getAnyRelocationAddress(RE
);
564 const bool r_extern
= (r_scattered
? false :
565 O
->getPlainRelocationExternal(RE
));
566 const uint32_t r_value
= (r_scattered
?
567 O
->getScatteredRelocationValue(RE
) : 0);
568 const unsigned r_symbolnum
= (r_scattered
? 0 :
569 O
->getPlainRelocationSymbolNum(RE
));
571 if (r_scattered
&& cputype
!= MachO::CPU_TYPE_X86_64
) {
573 // scattered: address
574 if ((cputype
== MachO::CPU_TYPE_I386
&&
575 r_type
== llvm::MachO::GENERIC_RELOC_PAIR
) ||
576 (cputype
== MachO::CPU_TYPE_ARM
&&
577 r_type
== llvm::MachO::ARM_RELOC_PAIR
))
580 outs() << format("%08x ", (unsigned int)r_address
);
589 PrintRLength(cputype
, r_type
, r_length
, previous_arm_half
);
591 // scattered: extern & type
593 PrintRType(cputype
, r_type
);
595 // scattered: scattered & value
596 outs() << format("True 0x%08x", (unsigned int)r_value
);
597 if (previous_sectdiff
== false) {
598 if ((cputype
== MachO::CPU_TYPE_ARM
&&
599 r_type
== llvm::MachO::ARM_RELOC_PAIR
))
600 outs() << format(" half = 0x%04x ", (unsigned int)r_address
);
602 else if (cputype
== MachO::CPU_TYPE_ARM
&&
603 sectdiff_r_type
== llvm::MachO::ARM_RELOC_HALF_SECTDIFF
)
604 outs() << format(" other_half = 0x%04x ", (unsigned int)r_address
);
605 if ((cputype
== MachO::CPU_TYPE_I386
&&
606 (r_type
== llvm::MachO::GENERIC_RELOC_SECTDIFF
||
607 r_type
== llvm::MachO::GENERIC_RELOC_LOCAL_SECTDIFF
)) ||
608 (cputype
== MachO::CPU_TYPE_ARM
&&
609 (sectdiff_r_type
== llvm::MachO::ARM_RELOC_SECTDIFF
||
610 sectdiff_r_type
== llvm::MachO::ARM_RELOC_LOCAL_SECTDIFF
||
611 sectdiff_r_type
== llvm::MachO::ARM_RELOC_HALF_SECTDIFF
))) {
612 previous_sectdiff
= true;
613 sectdiff_r_type
= r_type
;
616 previous_sectdiff
= false;
619 if (cputype
== MachO::CPU_TYPE_ARM
&&
620 (r_type
== llvm::MachO::ARM_RELOC_HALF
||
621 r_type
== llvm::MachO::ARM_RELOC_HALF_SECTDIFF
))
622 previous_arm_half
= true;
624 previous_arm_half
= false;
628 // scattered: address pcrel length extern type scattered value
629 outs() << format("%08x %1d %-2d n/a %-7d 1 0x%08x\n",
630 (unsigned int)r_address
, r_pcrel
, r_length
, r_type
,
631 (unsigned int)r_value
);
637 if (cputype
== MachO::CPU_TYPE_ARM
&&
638 r_type
== llvm::MachO::ARM_RELOC_PAIR
)
641 outs() << format("%08x ", (unsigned int)r_address
);
650 PrintRLength(cputype
, r_type
, r_length
, previous_arm_half
);
653 // plain: extern & type & scattered
655 PrintRType(cputype
, r_type
);
658 // plain: symbolnum/value
659 if (r_symbolnum
> Symtab
.nsyms
)
660 outs() << format("?(%d)\n", r_symbolnum
);
662 SymbolRef Symbol
= *O
->getSymbolByIndex(r_symbolnum
);
663 Expected
<StringRef
> SymNameNext
= Symbol
.getName();
664 const char *name
= NULL
;
666 name
= SymNameNext
->data();
668 outs() << format("?(%d)\n", r_symbolnum
);
670 outs() << name
<< "\n";
674 // plain: extern & type & scattered
676 PrintRType(cputype
, r_type
);
679 // plain: symbolnum/value
680 if (cputype
== MachO::CPU_TYPE_ARM
&&
681 r_type
== llvm::MachO::ARM_RELOC_PAIR
)
682 outs() << format("other_half = 0x%04x\n", (unsigned int)r_address
);
683 else if (cputype
== MachO::CPU_TYPE_ARM64
&&
684 r_type
== llvm::MachO::ARM64_RELOC_ADDEND
)
685 outs() << format("addend = 0x%06x\n", (unsigned int)r_symbolnum
);
687 outs() << format("%d ", r_symbolnum
);
688 if (r_symbolnum
== llvm::MachO::R_ABS
)
691 // in this case, r_symbolnum is actually a 1-based section number
692 uint32_t nsects
= O
->section_end()->getRawDataRefImpl().d
.a
;
693 if (r_symbolnum
> 0 && r_symbolnum
<= nsects
) {
694 llvm::object::DataRefImpl DRI
;
695 DRI
.d
.a
= r_symbolnum
-1;
696 StringRef SegName
= O
->getSectionFinalSegmentName(DRI
);
698 if (O
->getSectionName(DRI
, SectName
))
701 outs() << "(" << SegName
<< "," << SectName
<< ")\n";
709 if (cputype
== MachO::CPU_TYPE_ARM
&&
710 (r_type
== llvm::MachO::ARM_RELOC_HALF
||
711 r_type
== llvm::MachO::ARM_RELOC_HALF_SECTDIFF
))
712 previous_arm_half
= true;
714 previous_arm_half
= false;
717 // plain: address pcrel length extern type scattered symbolnum/section
718 outs() << format("%08x %1d %-2d %1d %-7d 0 %d\n",
719 (unsigned int)r_address
, r_pcrel
, r_length
, r_extern
,
720 r_type
, r_symbolnum
);
726 static void PrintRelocations(const MachOObjectFile
*O
, const bool verbose
) {
727 const uint64_t cputype
= O
->getHeader().cputype
;
728 const MachO::dysymtab_command Dysymtab
= O
->getDysymtabLoadCommand();
729 if (Dysymtab
.nextrel
!= 0) {
730 outs() << "External relocation information " << Dysymtab
.nextrel
732 outs() << "\naddress pcrel length extern type scattered "
734 PrintRelocationEntries(O
, O
->extrel_begin(), O
->extrel_end(), cputype
,
737 if (Dysymtab
.nlocrel
!= 0) {
738 outs() << format("Local relocation information %u entries",
740 outs() << "\naddress pcrel length extern type scattered "
742 PrintRelocationEntries(O
, O
->locrel_begin(), O
->locrel_end(), cputype
,
745 for (const auto &Load
: O
->load_commands()) {
746 if (Load
.C
.cmd
== MachO::LC_SEGMENT_64
) {
747 const MachO::segment_command_64 Seg
= O
->getSegment64LoadCommand(Load
);
748 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
749 const MachO::section_64 Sec
= O
->getSection64(Load
, J
);
750 if (Sec
.nreloc
!= 0) {
753 const StringRef SegName
= O
->getSectionFinalSegmentName(DRI
);
755 if (O
->getSectionName(DRI
, SectName
))
756 outs() << "Relocation information (" << SegName
<< ",?) "
757 << format("%u entries", Sec
.nreloc
);
759 outs() << "Relocation information (" << SegName
<< ","
760 << SectName
<< format(") %u entries", Sec
.nreloc
);
761 outs() << "\naddress pcrel length extern type scattered "
763 PrintRelocationEntries(O
, O
->section_rel_begin(DRI
),
764 O
->section_rel_end(DRI
), cputype
, verbose
);
767 } else if (Load
.C
.cmd
== MachO::LC_SEGMENT
) {
768 const MachO::segment_command Seg
= O
->getSegmentLoadCommand(Load
);
769 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
770 const MachO::section Sec
= O
->getSection(Load
, J
);
771 if (Sec
.nreloc
!= 0) {
774 const StringRef SegName
= O
->getSectionFinalSegmentName(DRI
);
776 if (O
->getSectionName(DRI
, SectName
))
777 outs() << "Relocation information (" << SegName
<< ",?) "
778 << format("%u entries", Sec
.nreloc
);
780 outs() << "Relocation information (" << SegName
<< ","
781 << SectName
<< format(") %u entries", Sec
.nreloc
);
782 outs() << "\naddress pcrel length extern type scattered "
784 PrintRelocationEntries(O
, O
->section_rel_begin(DRI
),
785 O
->section_rel_end(DRI
), cputype
, verbose
);
792 static void PrintDataInCodeTable(MachOObjectFile
*O
, bool verbose
) {
793 MachO::linkedit_data_command DIC
= O
->getDataInCodeLoadCommand();
794 uint32_t nentries
= DIC
.datasize
/ sizeof(struct MachO::data_in_code_entry
);
795 outs() << "Data in code table (" << nentries
<< " entries)\n";
796 outs() << "offset length kind\n";
797 for (dice_iterator DI
= O
->begin_dices(), DE
= O
->end_dices(); DI
!= DE
;
800 DI
->getOffset(Offset
);
801 outs() << format("0x%08" PRIx32
, Offset
) << " ";
803 DI
->getLength(Length
);
804 outs() << format("%6u", Length
) << " ";
809 case MachO::DICE_KIND_DATA
:
812 case MachO::DICE_KIND_JUMP_TABLE8
:
813 outs() << "JUMP_TABLE8";
815 case MachO::DICE_KIND_JUMP_TABLE16
:
816 outs() << "JUMP_TABLE16";
818 case MachO::DICE_KIND_JUMP_TABLE32
:
819 outs() << "JUMP_TABLE32";
821 case MachO::DICE_KIND_ABS_JUMP_TABLE32
:
822 outs() << "ABS_JUMP_TABLE32";
825 outs() << format("0x%04" PRIx32
, Kind
);
829 outs() << format("0x%04" PRIx32
, Kind
);
834 static void PrintLinkOptHints(MachOObjectFile
*O
) {
835 MachO::linkedit_data_command LohLC
= O
->getLinkOptHintsLoadCommand();
836 const char *loh
= O
->getData().substr(LohLC
.dataoff
, 1).data();
837 uint32_t nloh
= LohLC
.datasize
;
838 outs() << "Linker optimiztion hints (" << nloh
<< " total bytes)\n";
839 for (uint32_t i
= 0; i
< nloh
;) {
841 uint64_t identifier
= decodeULEB128((const uint8_t *)(loh
+ i
), &n
);
843 outs() << " identifier " << identifier
<< " ";
846 switch (identifier
) {
848 outs() << "AdrpAdrp\n";
851 outs() << "AdrpLdr\n";
854 outs() << "AdrpAddLdr\n";
857 outs() << "AdrpLdrGotLdr\n";
860 outs() << "AdrpAddStr\n";
863 outs() << "AdrpLdrGotStr\n";
866 outs() << "AdrpAdd\n";
869 outs() << "AdrpLdrGot\n";
872 outs() << "Unknown identifier value\n";
875 uint64_t narguments
= decodeULEB128((const uint8_t *)(loh
+ i
), &n
);
877 outs() << " narguments " << narguments
<< "\n";
881 for (uint32_t j
= 0; j
< narguments
; j
++) {
882 uint64_t value
= decodeULEB128((const uint8_t *)(loh
+ i
), &n
);
884 outs() << "\tvalue " << format("0x%" PRIx64
, value
) << "\n";
891 static void PrintDylibs(MachOObjectFile
*O
, bool JustId
) {
893 for (const auto &Load
: O
->load_commands()) {
894 if ((JustId
&& Load
.C
.cmd
== MachO::LC_ID_DYLIB
) ||
895 (!JustId
&& (Load
.C
.cmd
== MachO::LC_ID_DYLIB
||
896 Load
.C
.cmd
== MachO::LC_LOAD_DYLIB
||
897 Load
.C
.cmd
== MachO::LC_LOAD_WEAK_DYLIB
||
898 Load
.C
.cmd
== MachO::LC_REEXPORT_DYLIB
||
899 Load
.C
.cmd
== MachO::LC_LAZY_LOAD_DYLIB
||
900 Load
.C
.cmd
== MachO::LC_LOAD_UPWARD_DYLIB
))) {
901 MachO::dylib_command dl
= O
->getDylibIDLoadCommand(Load
);
902 if (dl
.dylib
.name
< dl
.cmdsize
) {
903 const char *p
= (const char *)(Load
.Ptr
) + dl
.dylib
.name
;
908 outs() << " (compatibility version "
909 << ((dl
.dylib
.compatibility_version
>> 16) & 0xffff) << "."
910 << ((dl
.dylib
.compatibility_version
>> 8) & 0xff) << "."
911 << (dl
.dylib
.compatibility_version
& 0xff) << ",";
912 outs() << " current version "
913 << ((dl
.dylib
.current_version
>> 16) & 0xffff) << "."
914 << ((dl
.dylib
.current_version
>> 8) & 0xff) << "."
915 << (dl
.dylib
.current_version
& 0xff) << ")\n";
918 outs() << "\tBad offset (" << dl
.dylib
.name
<< ") for name of ";
919 if (Load
.C
.cmd
== MachO::LC_ID_DYLIB
)
920 outs() << "LC_ID_DYLIB ";
921 else if (Load
.C
.cmd
== MachO::LC_LOAD_DYLIB
)
922 outs() << "LC_LOAD_DYLIB ";
923 else if (Load
.C
.cmd
== MachO::LC_LOAD_WEAK_DYLIB
)
924 outs() << "LC_LOAD_WEAK_DYLIB ";
925 else if (Load
.C
.cmd
== MachO::LC_LAZY_LOAD_DYLIB
)
926 outs() << "LC_LAZY_LOAD_DYLIB ";
927 else if (Load
.C
.cmd
== MachO::LC_REEXPORT_DYLIB
)
928 outs() << "LC_REEXPORT_DYLIB ";
929 else if (Load
.C
.cmd
== MachO::LC_LOAD_UPWARD_DYLIB
)
930 outs() << "LC_LOAD_UPWARD_DYLIB ";
933 outs() << "command " << Index
++ << "\n";
939 typedef DenseMap
<uint64_t, StringRef
> SymbolAddressMap
;
941 static void CreateSymbolAddressMap(MachOObjectFile
*O
,
942 SymbolAddressMap
*AddrMap
) {
943 // Create a map of symbol addresses to symbol names.
944 for (const SymbolRef
&Symbol
: O
->symbols()) {
945 Expected
<SymbolRef::Type
> STOrErr
= Symbol
.getType();
947 report_error(O
->getFileName(), STOrErr
.takeError());
948 SymbolRef::Type ST
= *STOrErr
;
949 if (ST
== SymbolRef::ST_Function
|| ST
== SymbolRef::ST_Data
||
950 ST
== SymbolRef::ST_Other
) {
951 uint64_t Address
= Symbol
.getValue();
952 Expected
<StringRef
> SymNameOrErr
= Symbol
.getName();
954 report_error(O
->getFileName(), SymNameOrErr
.takeError());
955 StringRef SymName
= *SymNameOrErr
;
956 if (!SymName
.startswith(".objc"))
957 (*AddrMap
)[Address
] = SymName
;
962 // GuessSymbolName is passed the address of what might be a symbol and a
963 // pointer to the SymbolAddressMap. It returns the name of a symbol
964 // with that address or nullptr if no symbol is found with that address.
965 static const char *GuessSymbolName(uint64_t value
, SymbolAddressMap
*AddrMap
) {
966 const char *SymbolName
= nullptr;
967 // A DenseMap can't lookup up some values.
968 if (value
!= 0xffffffffffffffffULL
&& value
!= 0xfffffffffffffffeULL
) {
969 StringRef name
= AddrMap
->lookup(value
);
971 SymbolName
= name
.data();
976 static void DumpCstringChar(const char c
) {
980 outs().write_escaped(p
);
983 static void DumpCstringSection(MachOObjectFile
*O
, const char *sect
,
984 uint32_t sect_size
, uint64_t sect_addr
,
985 bool print_addresses
) {
986 for (uint32_t i
= 0; i
< sect_size
; i
++) {
987 if (print_addresses
) {
989 outs() << format("%016" PRIx64
, sect_addr
+ i
) << " ";
991 outs() << format("%08" PRIx64
, sect_addr
+ i
) << " ";
993 for (; i
< sect_size
&& sect
[i
] != '\0'; i
++)
994 DumpCstringChar(sect
[i
]);
995 if (i
< sect_size
&& sect
[i
] == '\0')
1000 static void DumpLiteral4(uint32_t l
, float f
) {
1001 outs() << format("0x%08" PRIx32
, l
);
1002 if ((l
& 0x7f800000) != 0x7f800000)
1003 outs() << format(" (%.16e)\n", f
);
1005 if (l
== 0x7f800000)
1006 outs() << " (+Infinity)\n";
1007 else if (l
== 0xff800000)
1008 outs() << " (-Infinity)\n";
1009 else if ((l
& 0x00400000) == 0x00400000)
1010 outs() << " (non-signaling Not-a-Number)\n";
1012 outs() << " (signaling Not-a-Number)\n";
1016 static void DumpLiteral4Section(MachOObjectFile
*O
, const char *sect
,
1017 uint32_t sect_size
, uint64_t sect_addr
,
1018 bool print_addresses
) {
1019 for (uint32_t i
= 0; i
< sect_size
; i
+= sizeof(float)) {
1020 if (print_addresses
) {
1022 outs() << format("%016" PRIx64
, sect_addr
+ i
) << " ";
1024 outs() << format("%08" PRIx64
, sect_addr
+ i
) << " ";
1027 memcpy(&f
, sect
+ i
, sizeof(float));
1028 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
1029 sys::swapByteOrder(f
);
1031 memcpy(&l
, sect
+ i
, sizeof(uint32_t));
1032 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
1033 sys::swapByteOrder(l
);
1038 static void DumpLiteral8(MachOObjectFile
*O
, uint32_t l0
, uint32_t l1
,
1040 outs() << format("0x%08" PRIx32
, l0
) << " " << format("0x%08" PRIx32
, l1
);
1042 Hi
= (O
->isLittleEndian()) ? l1
: l0
;
1043 Lo
= (O
->isLittleEndian()) ? l0
: l1
;
1045 // Hi is the high word, so this is equivalent to if(isfinite(d))
1046 if ((Hi
& 0x7ff00000) != 0x7ff00000)
1047 outs() << format(" (%.16e)\n", d
);
1049 if (Hi
== 0x7ff00000 && Lo
== 0)
1050 outs() << " (+Infinity)\n";
1051 else if (Hi
== 0xfff00000 && Lo
== 0)
1052 outs() << " (-Infinity)\n";
1053 else if ((Hi
& 0x00080000) == 0x00080000)
1054 outs() << " (non-signaling Not-a-Number)\n";
1056 outs() << " (signaling Not-a-Number)\n";
1060 static void DumpLiteral8Section(MachOObjectFile
*O
, const char *sect
,
1061 uint32_t sect_size
, uint64_t sect_addr
,
1062 bool print_addresses
) {
1063 for (uint32_t i
= 0; i
< sect_size
; i
+= sizeof(double)) {
1064 if (print_addresses
) {
1066 outs() << format("%016" PRIx64
, sect_addr
+ i
) << " ";
1068 outs() << format("%08" PRIx64
, sect_addr
+ i
) << " ";
1071 memcpy(&d
, sect
+ i
, sizeof(double));
1072 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
1073 sys::swapByteOrder(d
);
1075 memcpy(&l0
, sect
+ i
, sizeof(uint32_t));
1076 memcpy(&l1
, sect
+ i
+ sizeof(uint32_t), sizeof(uint32_t));
1077 if (O
->isLittleEndian() != sys::IsLittleEndianHost
) {
1078 sys::swapByteOrder(l0
);
1079 sys::swapByteOrder(l1
);
1081 DumpLiteral8(O
, l0
, l1
, d
);
1085 static void DumpLiteral16(uint32_t l0
, uint32_t l1
, uint32_t l2
, uint32_t l3
) {
1086 outs() << format("0x%08" PRIx32
, l0
) << " ";
1087 outs() << format("0x%08" PRIx32
, l1
) << " ";
1088 outs() << format("0x%08" PRIx32
, l2
) << " ";
1089 outs() << format("0x%08" PRIx32
, l3
) << "\n";
1092 static void DumpLiteral16Section(MachOObjectFile
*O
, const char *sect
,
1093 uint32_t sect_size
, uint64_t sect_addr
,
1094 bool print_addresses
) {
1095 for (uint32_t i
= 0; i
< sect_size
; i
+= 16) {
1096 if (print_addresses
) {
1098 outs() << format("%016" PRIx64
, sect_addr
+ i
) << " ";
1100 outs() << format("%08" PRIx64
, sect_addr
+ i
) << " ";
1102 uint32_t l0
, l1
, l2
, l3
;
1103 memcpy(&l0
, sect
+ i
, sizeof(uint32_t));
1104 memcpy(&l1
, sect
+ i
+ sizeof(uint32_t), sizeof(uint32_t));
1105 memcpy(&l2
, sect
+ i
+ 2 * sizeof(uint32_t), sizeof(uint32_t));
1106 memcpy(&l3
, sect
+ i
+ 3 * sizeof(uint32_t), sizeof(uint32_t));
1107 if (O
->isLittleEndian() != sys::IsLittleEndianHost
) {
1108 sys::swapByteOrder(l0
);
1109 sys::swapByteOrder(l1
);
1110 sys::swapByteOrder(l2
);
1111 sys::swapByteOrder(l3
);
1113 DumpLiteral16(l0
, l1
, l2
, l3
);
1117 static void DumpLiteralPointerSection(MachOObjectFile
*O
,
1118 const SectionRef
&Section
,
1119 const char *sect
, uint32_t sect_size
,
1121 bool print_addresses
) {
1122 // Collect the literal sections in this Mach-O file.
1123 std::vector
<SectionRef
> LiteralSections
;
1124 for (const SectionRef
&Section
: O
->sections()) {
1125 DataRefImpl Ref
= Section
.getRawDataRefImpl();
1126 uint32_t section_type
;
1128 const MachO::section_64 Sec
= O
->getSection64(Ref
);
1129 section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
1131 const MachO::section Sec
= O
->getSection(Ref
);
1132 section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
1134 if (section_type
== MachO::S_CSTRING_LITERALS
||
1135 section_type
== MachO::S_4BYTE_LITERALS
||
1136 section_type
== MachO::S_8BYTE_LITERALS
||
1137 section_type
== MachO::S_16BYTE_LITERALS
)
1138 LiteralSections
.push_back(Section
);
1141 // Set the size of the literal pointer.
1142 uint32_t lp_size
= O
->is64Bit() ? 8 : 4;
1144 // Collect the external relocation symbols for the literal pointers.
1145 std::vector
<std::pair
<uint64_t, SymbolRef
>> Relocs
;
1146 for (const RelocationRef
&Reloc
: Section
.relocations()) {
1148 MachO::any_relocation_info RE
;
1149 bool isExtern
= false;
1150 Rel
= Reloc
.getRawDataRefImpl();
1151 RE
= O
->getRelocation(Rel
);
1152 isExtern
= O
->getPlainRelocationExternal(RE
);
1154 uint64_t RelocOffset
= Reloc
.getOffset();
1155 symbol_iterator RelocSym
= Reloc
.getSymbol();
1156 Relocs
.push_back(std::make_pair(RelocOffset
, *RelocSym
));
1159 array_pod_sort(Relocs
.begin(), Relocs
.end());
1161 // Dump each literal pointer.
1162 for (uint32_t i
= 0; i
< sect_size
; i
+= lp_size
) {
1163 if (print_addresses
) {
1165 outs() << format("%016" PRIx64
, sect_addr
+ i
) << " ";
1167 outs() << format("%08" PRIx64
, sect_addr
+ i
) << " ";
1171 memcpy(&lp
, sect
+ i
, sizeof(uint64_t));
1172 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
1173 sys::swapByteOrder(lp
);
1176 memcpy(&li
, sect
+ i
, sizeof(uint32_t));
1177 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
1178 sys::swapByteOrder(li
);
1182 // First look for an external relocation entry for this literal pointer.
1183 auto Reloc
= find_if(Relocs
, [&](const std::pair
<uint64_t, SymbolRef
> &P
) {
1184 return P
.first
== i
;
1186 if (Reloc
!= Relocs
.end()) {
1187 symbol_iterator RelocSym
= Reloc
->second
;
1188 Expected
<StringRef
> SymName
= RelocSym
->getName();
1190 report_error(O
->getFileName(), SymName
.takeError());
1191 outs() << "external relocation entry for symbol:" << *SymName
<< "\n";
1195 // For local references see what the section the literal pointer points to.
1196 auto Sect
= find_if(LiteralSections
, [&](const SectionRef
&R
) {
1197 return lp
>= R
.getAddress() && lp
< R
.getAddress() + R
.getSize();
1199 if (Sect
== LiteralSections
.end()) {
1200 outs() << format("0x%" PRIx64
, lp
) << " (not in a literal section)\n";
1204 uint64_t SectAddress
= Sect
->getAddress();
1205 uint64_t SectSize
= Sect
->getSize();
1208 Sect
->getName(SectName
);
1209 DataRefImpl Ref
= Sect
->getRawDataRefImpl();
1210 StringRef SegmentName
= O
->getSectionFinalSegmentName(Ref
);
1211 outs() << SegmentName
<< ":" << SectName
<< ":";
1213 uint32_t section_type
;
1215 const MachO::section_64 Sec
= O
->getSection64(Ref
);
1216 section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
1218 const MachO::section Sec
= O
->getSection(Ref
);
1219 section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
1223 Sect
->getContents(BytesStr
);
1224 const char *Contents
= reinterpret_cast<const char *>(BytesStr
.data());
1226 switch (section_type
) {
1227 case MachO::S_CSTRING_LITERALS
:
1228 for (uint64_t i
= lp
- SectAddress
; i
< SectSize
&& Contents
[i
] != '\0';
1230 DumpCstringChar(Contents
[i
]);
1234 case MachO::S_4BYTE_LITERALS
:
1236 memcpy(&f
, Contents
+ (lp
- SectAddress
), sizeof(float));
1238 memcpy(&l
, Contents
+ (lp
- SectAddress
), sizeof(uint32_t));
1239 if (O
->isLittleEndian() != sys::IsLittleEndianHost
) {
1240 sys::swapByteOrder(f
);
1241 sys::swapByteOrder(l
);
1245 case MachO::S_8BYTE_LITERALS
: {
1247 memcpy(&d
, Contents
+ (lp
- SectAddress
), sizeof(double));
1249 memcpy(&l0
, Contents
+ (lp
- SectAddress
), sizeof(uint32_t));
1250 memcpy(&l1
, Contents
+ (lp
- SectAddress
) + sizeof(uint32_t),
1252 if (O
->isLittleEndian() != sys::IsLittleEndianHost
) {
1253 sys::swapByteOrder(f
);
1254 sys::swapByteOrder(l0
);
1255 sys::swapByteOrder(l1
);
1257 DumpLiteral8(O
, l0
, l1
, d
);
1260 case MachO::S_16BYTE_LITERALS
: {
1261 uint32_t l0
, l1
, l2
, l3
;
1262 memcpy(&l0
, Contents
+ (lp
- SectAddress
), sizeof(uint32_t));
1263 memcpy(&l1
, Contents
+ (lp
- SectAddress
) + sizeof(uint32_t),
1265 memcpy(&l2
, Contents
+ (lp
- SectAddress
) + 2 * sizeof(uint32_t),
1267 memcpy(&l3
, Contents
+ (lp
- SectAddress
) + 3 * sizeof(uint32_t),
1269 if (O
->isLittleEndian() != sys::IsLittleEndianHost
) {
1270 sys::swapByteOrder(l0
);
1271 sys::swapByteOrder(l1
);
1272 sys::swapByteOrder(l2
);
1273 sys::swapByteOrder(l3
);
1275 DumpLiteral16(l0
, l1
, l2
, l3
);
1282 static void DumpInitTermPointerSection(MachOObjectFile
*O
,
1283 const SectionRef
&Section
,
1285 uint32_t sect_size
, uint64_t sect_addr
,
1286 SymbolAddressMap
*AddrMap
,
1289 stride
= (O
->is64Bit()) ? sizeof(uint64_t) : sizeof(uint32_t);
1291 // Collect the external relocation symbols for the pointers.
1292 std::vector
<std::pair
<uint64_t, SymbolRef
>> Relocs
;
1293 for (const RelocationRef
&Reloc
: Section
.relocations()) {
1295 MachO::any_relocation_info RE
;
1296 bool isExtern
= false;
1297 Rel
= Reloc
.getRawDataRefImpl();
1298 RE
= O
->getRelocation(Rel
);
1299 isExtern
= O
->getPlainRelocationExternal(RE
);
1301 uint64_t RelocOffset
= Reloc
.getOffset();
1302 symbol_iterator RelocSym
= Reloc
.getSymbol();
1303 Relocs
.push_back(std::make_pair(RelocOffset
, *RelocSym
));
1306 array_pod_sort(Relocs
.begin(), Relocs
.end());
1308 for (uint32_t i
= 0; i
< sect_size
; i
+= stride
) {
1309 const char *SymbolName
= nullptr;
1312 outs() << format("0x%016" PRIx64
, sect_addr
+ i
* stride
) << " ";
1313 uint64_t pointer_value
;
1314 memcpy(&pointer_value
, sect
+ i
, stride
);
1315 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
1316 sys::swapByteOrder(pointer_value
);
1317 outs() << format("0x%016" PRIx64
, pointer_value
);
1320 outs() << format("0x%08" PRIx64
, sect_addr
+ i
* stride
) << " ";
1321 uint32_t pointer_value
;
1322 memcpy(&pointer_value
, sect
+ i
, stride
);
1323 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
1324 sys::swapByteOrder(pointer_value
);
1325 outs() << format("0x%08" PRIx32
, pointer_value
);
1329 // First look for an external relocation entry for this pointer.
1330 auto Reloc
= find_if(Relocs
, [&](const std::pair
<uint64_t, SymbolRef
> &P
) {
1331 return P
.first
== i
;
1333 if (Reloc
!= Relocs
.end()) {
1334 symbol_iterator RelocSym
= Reloc
->second
;
1335 Expected
<StringRef
> SymName
= RelocSym
->getName();
1337 report_error(O
->getFileName(), SymName
.takeError());
1338 outs() << " " << *SymName
;
1340 SymbolName
= GuessSymbolName(p
, AddrMap
);
1342 outs() << " " << SymbolName
;
1349 static void DumpRawSectionContents(MachOObjectFile
*O
, const char *sect
,
1350 uint32_t size
, uint64_t addr
) {
1351 uint32_t cputype
= O
->getHeader().cputype
;
1352 if (cputype
== MachO::CPU_TYPE_I386
|| cputype
== MachO::CPU_TYPE_X86_64
) {
1354 for (uint32_t i
= 0; i
< size
; i
+= j
, addr
+= j
) {
1356 outs() << format("%016" PRIx64
, addr
) << "\t";
1358 outs() << format("%08" PRIx64
, addr
) << "\t";
1359 for (j
= 0; j
< 16 && i
+ j
< size
; j
++) {
1360 uint8_t byte_word
= *(sect
+ i
+ j
);
1361 outs() << format("%02" PRIx32
, (uint32_t)byte_word
) << " ";
1367 for (uint32_t i
= 0; i
< size
; i
+= j
, addr
+= j
) {
1369 outs() << format("%016" PRIx64
, addr
) << "\t";
1371 outs() << format("%08" PRIx64
, addr
) << "\t";
1372 for (j
= 0; j
< 4 * sizeof(int32_t) && i
+ j
< size
;
1373 j
+= sizeof(int32_t)) {
1374 if (i
+ j
+ sizeof(int32_t) <= size
) {
1376 memcpy(&long_word
, sect
+ i
+ j
, sizeof(int32_t));
1377 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
1378 sys::swapByteOrder(long_word
);
1379 outs() << format("%08" PRIx32
, long_word
) << " ";
1381 for (uint32_t k
= 0; i
+ j
+ k
< size
; k
++) {
1382 uint8_t byte_word
= *(sect
+ i
+ j
+ k
);
1383 outs() << format("%02" PRIx32
, (uint32_t)byte_word
) << " ";
1392 static void DisassembleMachO(StringRef Filename
, MachOObjectFile
*MachOOF
,
1393 StringRef DisSegName
, StringRef DisSectName
);
1394 static void DumpProtocolSection(MachOObjectFile
*O
, const char *sect
,
1395 uint32_t size
, uint32_t addr
);
1397 static void DumpBitcodeSection(MachOObjectFile
*O
, const char *sect
,
1398 uint32_t size
, bool verbose
,
1399 bool PrintXarHeader
, bool PrintXarFileHeaders
,
1400 std::string XarMemberName
);
1401 #endif // defined(HAVE_LIBXAR)
1403 static void DumpSectionContents(StringRef Filename
, MachOObjectFile
*O
,
1405 SymbolAddressMap AddrMap
;
1407 CreateSymbolAddressMap(O
, &AddrMap
);
1409 for (unsigned i
= 0; i
< FilterSections
.size(); ++i
) {
1410 StringRef DumpSection
= FilterSections
[i
];
1411 std::pair
<StringRef
, StringRef
> DumpSegSectName
;
1412 DumpSegSectName
= DumpSection
.split(',');
1413 StringRef DumpSegName
, DumpSectName
;
1414 if (DumpSegSectName
.second
.size()) {
1415 DumpSegName
= DumpSegSectName
.first
;
1416 DumpSectName
= DumpSegSectName
.second
;
1419 DumpSectName
= DumpSegSectName
.first
;
1421 for (const SectionRef
&Section
: O
->sections()) {
1423 Section
.getName(SectName
);
1424 DataRefImpl Ref
= Section
.getRawDataRefImpl();
1425 StringRef SegName
= O
->getSectionFinalSegmentName(Ref
);
1426 if ((DumpSegName
.empty() || SegName
== DumpSegName
) &&
1427 (SectName
== DumpSectName
)) {
1429 uint32_t section_flags
;
1431 const MachO::section_64 Sec
= O
->getSection64(Ref
);
1432 section_flags
= Sec
.flags
;
1435 const MachO::section Sec
= O
->getSection(Ref
);
1436 section_flags
= Sec
.flags
;
1438 uint32_t section_type
= section_flags
& MachO::SECTION_TYPE
;
1441 Section
.getContents(BytesStr
);
1442 const char *sect
= reinterpret_cast<const char *>(BytesStr
.data());
1443 uint32_t sect_size
= BytesStr
.size();
1444 uint64_t sect_addr
= Section
.getAddress();
1446 outs() << "Contents of (" << SegName
<< "," << SectName
1450 if ((section_flags
& MachO::S_ATTR_PURE_INSTRUCTIONS
) ||
1451 (section_flags
& MachO::S_ATTR_SOME_INSTRUCTIONS
)) {
1452 DisassembleMachO(Filename
, O
, SegName
, SectName
);
1455 if (SegName
== "__TEXT" && SectName
== "__info_plist") {
1459 if (SegName
== "__OBJC" && SectName
== "__protocol") {
1460 DumpProtocolSection(O
, sect
, sect_size
, sect_addr
);
1464 if (SegName
== "__LLVM" && SectName
== "__bundle") {
1465 DumpBitcodeSection(O
, sect
, sect_size
, verbose
, !NoSymbolicOperands
,
1466 ArchiveHeaders
, "");
1469 #endif // defined(HAVE_LIBXAR)
1470 switch (section_type
) {
1471 case MachO::S_REGULAR
:
1472 DumpRawSectionContents(O
, sect
, sect_size
, sect_addr
);
1474 case MachO::S_ZEROFILL
:
1475 outs() << "zerofill section and has no contents in the file\n";
1477 case MachO::S_CSTRING_LITERALS
:
1478 DumpCstringSection(O
, sect
, sect_size
, sect_addr
, !NoLeadingAddr
);
1480 case MachO::S_4BYTE_LITERALS
:
1481 DumpLiteral4Section(O
, sect
, sect_size
, sect_addr
, !NoLeadingAddr
);
1483 case MachO::S_8BYTE_LITERALS
:
1484 DumpLiteral8Section(O
, sect
, sect_size
, sect_addr
, !NoLeadingAddr
);
1486 case MachO::S_16BYTE_LITERALS
:
1487 DumpLiteral16Section(O
, sect
, sect_size
, sect_addr
, !NoLeadingAddr
);
1489 case MachO::S_LITERAL_POINTERS
:
1490 DumpLiteralPointerSection(O
, Section
, sect
, sect_size
, sect_addr
,
1493 case MachO::S_MOD_INIT_FUNC_POINTERS
:
1494 case MachO::S_MOD_TERM_FUNC_POINTERS
:
1495 DumpInitTermPointerSection(O
, Section
, sect
, sect_size
, sect_addr
,
1499 outs() << "Unknown section type ("
1500 << format("0x%08" PRIx32
, section_type
) << ")\n";
1501 DumpRawSectionContents(O
, sect
, sect_size
, sect_addr
);
1505 if (section_type
== MachO::S_ZEROFILL
)
1506 outs() << "zerofill section and has no contents in the file\n";
1508 DumpRawSectionContents(O
, sect
, sect_size
, sect_addr
);
1515 static void DumpInfoPlistSectionContents(StringRef Filename
,
1516 MachOObjectFile
*O
) {
1517 for (const SectionRef
&Section
: O
->sections()) {
1519 Section
.getName(SectName
);
1520 DataRefImpl Ref
= Section
.getRawDataRefImpl();
1521 StringRef SegName
= O
->getSectionFinalSegmentName(Ref
);
1522 if (SegName
== "__TEXT" && SectName
== "__info_plist") {
1523 if (!NoLeadingHeaders
)
1524 outs() << "Contents of (" << SegName
<< "," << SectName
<< ") section\n";
1526 Section
.getContents(BytesStr
);
1527 const char *sect
= reinterpret_cast<const char *>(BytesStr
.data());
1528 outs() << format("%.*s", BytesStr
.size(), sect
) << "\n";
1534 // checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1535 // and if it is and there is a list of architecture flags is specified then
1536 // check to make sure this Mach-O file is one of those architectures or all
1537 // architectures were specified. If not then an error is generated and this
1538 // routine returns false. Else it returns true.
1539 static bool checkMachOAndArchFlags(ObjectFile
*O
, StringRef Filename
) {
1540 auto *MachO
= dyn_cast
<MachOObjectFile
>(O
);
1542 if (!MachO
|| ArchAll
|| ArchFlags
.empty())
1545 MachO::mach_header H
;
1546 MachO::mach_header_64 H_64
;
1548 const char *McpuDefault
, *ArchFlag
;
1549 if (MachO
->is64Bit()) {
1550 H_64
= MachO
->MachOObjectFile::getHeader64();
1551 T
= MachOObjectFile::getArchTriple(H_64
.cputype
, H_64
.cpusubtype
,
1552 &McpuDefault
, &ArchFlag
);
1554 H
= MachO
->MachOObjectFile::getHeader();
1555 T
= MachOObjectFile::getArchTriple(H
.cputype
, H
.cpusubtype
,
1556 &McpuDefault
, &ArchFlag
);
1558 const std::string
ArchFlagName(ArchFlag
);
1559 if (none_of(ArchFlags
, [&](const std::string
&Name
) {
1560 return Name
== ArchFlagName
;
1562 errs() << "llvm-objdump: " + Filename
+ ": No architecture specified.\n";
1568 static void printObjcMetaData(MachOObjectFile
*O
, bool verbose
);
1570 // ProcessMachO() is passed a single opened Mach-O file, which may be an
1571 // archive member and or in a slice of a universal file. It prints the
1572 // the file name and header info and then processes it according to the
1573 // command line options.
1574 static void ProcessMachO(StringRef Name
, MachOObjectFile
*MachOOF
,
1575 StringRef ArchiveMemberName
= StringRef(),
1576 StringRef ArchitectureName
= StringRef()) {
1577 // If we are doing some processing here on the Mach-O file print the header
1578 // info. And don't print it otherwise like in the case of printing the
1579 // UniversalHeaders or ArchiveHeaders.
1580 if (Disassemble
|| Relocations
|| PrivateHeaders
|| ExportsTrie
|| Rebase
||
1581 Bind
|| SymbolTable
|| LazyBind
|| WeakBind
|| IndirectSymbols
||
1582 DataInCode
|| LinkOptHints
|| DylibsUsed
|| DylibId
|| ObjcMetaData
||
1583 (FilterSections
.size() != 0)) {
1584 if (!NoLeadingHeaders
) {
1586 if (!ArchiveMemberName
.empty())
1587 outs() << '(' << ArchiveMemberName
<< ')';
1588 if (!ArchitectureName
.empty())
1589 outs() << " (architecture " << ArchitectureName
<< ")";
1593 // To use the report_error() form with an ArchiveName and FileName set
1594 // these up based on what is passed for Name and ArchiveMemberName.
1595 StringRef ArchiveName
;
1597 if (!ArchiveMemberName
.empty()) {
1599 FileName
= ArchiveMemberName
;
1601 ArchiveName
= StringRef();
1605 // If we need the symbol table to do the operation then check it here to
1606 // produce a good error message as to where the Mach-O file comes from in
1607 // the error message.
1608 if (Disassemble
|| IndirectSymbols
|| FilterSections
.size() != 0 ||
1610 if (Error Err
= MachOOF
->checkSymbolTable())
1611 report_error(ArchiveName
, FileName
, std::move(Err
), ArchitectureName
);
1614 if (MachOOF
->getHeader().filetype
== MachO::MH_KEXT_BUNDLE
&&
1615 MachOOF
->getHeader().cputype
== MachO::CPU_TYPE_ARM64
)
1616 DisassembleMachO(FileName
, MachOOF
, "__TEXT_EXEC", "__text");
1618 DisassembleMachO(FileName
, MachOOF
, "__TEXT", "__text");
1620 if (IndirectSymbols
)
1621 PrintIndirectSymbols(MachOOF
, !NonVerbose
);
1623 PrintDataInCodeTable(MachOOF
, !NonVerbose
);
1625 PrintLinkOptHints(MachOOF
);
1627 PrintRelocations(MachOOF
, !NonVerbose
);
1629 PrintSectionHeaders(MachOOF
);
1630 if (SectionContents
)
1631 PrintSectionContents(MachOOF
);
1632 if (FilterSections
.size() != 0)
1633 DumpSectionContents(FileName
, MachOOF
, !NonVerbose
);
1635 DumpInfoPlistSectionContents(FileName
, MachOOF
);
1637 PrintDylibs(MachOOF
, false);
1639 PrintDylibs(MachOOF
, true);
1641 PrintSymbolTable(MachOOF
, ArchiveName
, ArchitectureName
);
1643 printMachOUnwindInfo(MachOOF
);
1644 if (PrivateHeaders
) {
1645 printMachOFileHeader(MachOOF
);
1646 printMachOLoadCommands(MachOOF
);
1648 if (FirstPrivateHeader
)
1649 printMachOFileHeader(MachOOF
);
1651 printObjcMetaData(MachOOF
, !NonVerbose
);
1653 printExportsTrie(MachOOF
);
1655 printRebaseTable(MachOOF
);
1657 printBindTable(MachOOF
);
1659 printLazyBindTable(MachOOF
);
1661 printWeakBindTable(MachOOF
);
1663 if (DwarfDumpType
!= DIDT_Null
) {
1664 std::unique_ptr
<DIContext
> DICtx
= DWARFContext::create(*MachOOF
);
1665 // Dump the complete DWARF structure.
1666 DIDumpOptions DumpOpts
;
1667 DumpOpts
.DumpType
= DwarfDumpType
;
1668 DICtx
->dump(outs(), DumpOpts
);
1672 // printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1673 static void printUnknownCPUType(uint32_t cputype
, uint32_t cpusubtype
) {
1674 outs() << " cputype (" << cputype
<< ")\n";
1675 outs() << " cpusubtype (" << cpusubtype
<< ")\n";
1678 // printCPUType() helps print_fat_headers by printing the cputype and
1679 // pusubtype (symbolically for the one's it knows about).
1680 static void printCPUType(uint32_t cputype
, uint32_t cpusubtype
) {
1682 case MachO::CPU_TYPE_I386
:
1683 switch (cpusubtype
) {
1684 case MachO::CPU_SUBTYPE_I386_ALL
:
1685 outs() << " cputype CPU_TYPE_I386\n";
1686 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
1689 printUnknownCPUType(cputype
, cpusubtype
);
1693 case MachO::CPU_TYPE_X86_64
:
1694 switch (cpusubtype
) {
1695 case MachO::CPU_SUBTYPE_X86_64_ALL
:
1696 outs() << " cputype CPU_TYPE_X86_64\n";
1697 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1699 case MachO::CPU_SUBTYPE_X86_64_H
:
1700 outs() << " cputype CPU_TYPE_X86_64\n";
1701 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
1704 printUnknownCPUType(cputype
, cpusubtype
);
1708 case MachO::CPU_TYPE_ARM
:
1709 switch (cpusubtype
) {
1710 case MachO::CPU_SUBTYPE_ARM_ALL
:
1711 outs() << " cputype CPU_TYPE_ARM\n";
1712 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
1714 case MachO::CPU_SUBTYPE_ARM_V4T
:
1715 outs() << " cputype CPU_TYPE_ARM\n";
1716 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
1718 case MachO::CPU_SUBTYPE_ARM_V5TEJ
:
1719 outs() << " cputype CPU_TYPE_ARM\n";
1720 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
1722 case MachO::CPU_SUBTYPE_ARM_XSCALE
:
1723 outs() << " cputype CPU_TYPE_ARM\n";
1724 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
1726 case MachO::CPU_SUBTYPE_ARM_V6
:
1727 outs() << " cputype CPU_TYPE_ARM\n";
1728 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
1730 case MachO::CPU_SUBTYPE_ARM_V6M
:
1731 outs() << " cputype CPU_TYPE_ARM\n";
1732 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
1734 case MachO::CPU_SUBTYPE_ARM_V7
:
1735 outs() << " cputype CPU_TYPE_ARM\n";
1736 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
1738 case MachO::CPU_SUBTYPE_ARM_V7EM
:
1739 outs() << " cputype CPU_TYPE_ARM\n";
1740 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
1742 case MachO::CPU_SUBTYPE_ARM_V7K
:
1743 outs() << " cputype CPU_TYPE_ARM\n";
1744 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
1746 case MachO::CPU_SUBTYPE_ARM_V7M
:
1747 outs() << " cputype CPU_TYPE_ARM\n";
1748 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
1750 case MachO::CPU_SUBTYPE_ARM_V7S
:
1751 outs() << " cputype CPU_TYPE_ARM\n";
1752 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
1755 printUnknownCPUType(cputype
, cpusubtype
);
1759 case MachO::CPU_TYPE_ARM64
:
1760 switch (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) {
1761 case MachO::CPU_SUBTYPE_ARM64_ALL
:
1762 outs() << " cputype CPU_TYPE_ARM64\n";
1763 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
1766 printUnknownCPUType(cputype
, cpusubtype
);
1771 printUnknownCPUType(cputype
, cpusubtype
);
1776 static void printMachOUniversalHeaders(const object::MachOUniversalBinary
*UB
,
1778 outs() << "Fat headers\n";
1780 if (UB
->getMagic() == MachO::FAT_MAGIC
)
1781 outs() << "fat_magic FAT_MAGIC\n";
1782 else // UB->getMagic() == MachO::FAT_MAGIC_64
1783 outs() << "fat_magic FAT_MAGIC_64\n";
1785 outs() << "fat_magic " << format("0x%" PRIx32
, MachO::FAT_MAGIC
) << "\n";
1787 uint32_t nfat_arch
= UB
->getNumberOfObjects();
1788 StringRef Buf
= UB
->getData();
1789 uint64_t size
= Buf
.size();
1790 uint64_t big_size
= sizeof(struct MachO::fat_header
) +
1791 nfat_arch
* sizeof(struct MachO::fat_arch
);
1792 outs() << "nfat_arch " << UB
->getNumberOfObjects();
1794 outs() << " (malformed, contains zero architecture types)\n";
1795 else if (big_size
> size
)
1796 outs() << " (malformed, architectures past end of file)\n";
1800 for (uint32_t i
= 0; i
< nfat_arch
; ++i
) {
1801 MachOUniversalBinary::ObjectForArch
OFA(UB
, i
);
1802 uint32_t cputype
= OFA
.getCPUType();
1803 uint32_t cpusubtype
= OFA
.getCPUSubType();
1804 outs() << "architecture ";
1805 for (uint32_t j
= 0; i
!= 0 && j
<= i
- 1; j
++) {
1806 MachOUniversalBinary::ObjectForArch
other_OFA(UB
, j
);
1807 uint32_t other_cputype
= other_OFA
.getCPUType();
1808 uint32_t other_cpusubtype
= other_OFA
.getCPUSubType();
1809 if (cputype
!= 0 && cpusubtype
!= 0 && cputype
== other_cputype
&&
1810 (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) ==
1811 (other_cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
)) {
1812 outs() << "(illegal duplicate architecture) ";
1817 outs() << OFA
.getArchFlagName() << "\n";
1818 printCPUType(cputype
, cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
1820 outs() << i
<< "\n";
1821 outs() << " cputype " << cputype
<< "\n";
1822 outs() << " cpusubtype " << (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
)
1826 (cpusubtype
& MachO::CPU_SUBTYPE_MASK
) == MachO::CPU_SUBTYPE_LIB64
)
1827 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
1829 outs() << " capabilities "
1830 << format("0x%" PRIx32
,
1831 (cpusubtype
& MachO::CPU_SUBTYPE_MASK
) >> 24) << "\n";
1832 outs() << " offset " << OFA
.getOffset();
1833 if (OFA
.getOffset() > size
)
1834 outs() << " (past end of file)";
1835 if (OFA
.getOffset() % (1 << OFA
.getAlign()) != 0)
1836 outs() << " (not aligned on it's alignment (2^" << OFA
.getAlign() << ")";
1838 outs() << " size " << OFA
.getSize();
1839 big_size
= OFA
.getOffset() + OFA
.getSize();
1840 if (big_size
> size
)
1841 outs() << " (past end of file)";
1843 outs() << " align 2^" << OFA
.getAlign() << " (" << (1 << OFA
.getAlign())
1848 static void printArchiveChild(StringRef Filename
, const Archive::Child
&C
,
1849 bool verbose
, bool print_offset
,
1850 StringRef ArchitectureName
= StringRef()) {
1852 outs() << C
.getChildOffset() << "\t";
1853 Expected
<sys::fs::perms
> ModeOrErr
= C
.getAccessMode();
1855 report_error(Filename
, C
, ModeOrErr
.takeError(), ArchitectureName
);
1856 sys::fs::perms Mode
= ModeOrErr
.get();
1858 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
1859 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
1861 outs() << ((Mode
& sys::fs::owner_read
) ? "r" : "-");
1862 outs() << ((Mode
& sys::fs::owner_write
) ? "w" : "-");
1863 outs() << ((Mode
& sys::fs::owner_exe
) ? "x" : "-");
1864 outs() << ((Mode
& sys::fs::group_read
) ? "r" : "-");
1865 outs() << ((Mode
& sys::fs::group_write
) ? "w" : "-");
1866 outs() << ((Mode
& sys::fs::group_exe
) ? "x" : "-");
1867 outs() << ((Mode
& sys::fs::others_read
) ? "r" : "-");
1868 outs() << ((Mode
& sys::fs::others_write
) ? "w" : "-");
1869 outs() << ((Mode
& sys::fs::others_exe
) ? "x" : "-");
1871 outs() << format("0%o ", Mode
);
1874 Expected
<unsigned> UIDOrErr
= C
.getUID();
1876 report_error(Filename
, C
, UIDOrErr
.takeError(), ArchitectureName
);
1877 unsigned UID
= UIDOrErr
.get();
1878 outs() << format("%3d/", UID
);
1879 Expected
<unsigned> GIDOrErr
= C
.getGID();
1881 report_error(Filename
, C
, GIDOrErr
.takeError(), ArchitectureName
);
1882 unsigned GID
= GIDOrErr
.get();
1883 outs() << format("%-3d ", GID
);
1884 Expected
<uint64_t> Size
= C
.getRawSize();
1886 report_error(Filename
, C
, Size
.takeError(), ArchitectureName
);
1887 outs() << format("%5" PRId64
, Size
.get()) << " ";
1889 StringRef RawLastModified
= C
.getRawLastModified();
1892 if (RawLastModified
.getAsInteger(10, Seconds
))
1893 outs() << "(date: \"" << RawLastModified
1894 << "\" contains non-decimal chars) ";
1896 // Since cime(3) returns a 26 character string of the form:
1897 // "Sun Sep 16 01:03:52 1973\n\0"
1898 // just print 24 characters.
1900 outs() << format("%.24s ", ctime(&t
));
1903 outs() << RawLastModified
<< " ";
1907 Expected
<StringRef
> NameOrErr
= C
.getName();
1909 consumeError(NameOrErr
.takeError());
1910 Expected
<StringRef
> NameOrErr
= C
.getRawName();
1912 report_error(Filename
, C
, NameOrErr
.takeError(), ArchitectureName
);
1913 StringRef RawName
= NameOrErr
.get();
1914 outs() << RawName
<< "\n";
1916 StringRef Name
= NameOrErr
.get();
1917 outs() << Name
<< "\n";
1920 Expected
<StringRef
> NameOrErr
= C
.getRawName();
1922 report_error(Filename
, C
, NameOrErr
.takeError(), ArchitectureName
);
1923 StringRef RawName
= NameOrErr
.get();
1924 outs() << RawName
<< "\n";
1928 static void printArchiveHeaders(StringRef Filename
, Archive
*A
, bool verbose
,
1930 StringRef ArchitectureName
= StringRef()) {
1931 Error Err
= Error::success();
1933 for (const auto &C
: A
->children(Err
, false))
1934 printArchiveChild(Filename
, C
, verbose
, print_offset
, ArchitectureName
);
1937 report_error(StringRef(), Filename
, std::move(Err
), ArchitectureName
);
1940 static bool ValidateArchFlags() {
1941 // Check for -arch all and verifiy the -arch flags are valid.
1942 for (unsigned i
= 0; i
< ArchFlags
.size(); ++i
) {
1943 if (ArchFlags
[i
] == "all") {
1946 if (!MachOObjectFile::isValidArch(ArchFlags
[i
])) {
1947 errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags
[i
] +
1948 "'for the -arch option\n";
1956 // ParseInputMachO() parses the named Mach-O file in Filename and handles the
1957 // -arch flags selecting just those slices as specified by them and also parses
1958 // archive files. Then for each individual Mach-O file ProcessMachO() is
1959 // called to process the file based on the command line options.
1960 void llvm::ParseInputMachO(StringRef Filename
) {
1961 if (!ValidateArchFlags())
1964 // Attempt to open the binary.
1965 Expected
<OwningBinary
<Binary
>> BinaryOrErr
= createBinary(Filename
);
1967 if (auto E
= isNotObjectErrorInvalidFileType(BinaryOrErr
.takeError()))
1968 report_error(Filename
, std::move(E
));
1970 outs() << Filename
<< ": is not an object file\n";
1973 Binary
&Bin
= *BinaryOrErr
.get().getBinary();
1975 if (Archive
*A
= dyn_cast
<Archive
>(&Bin
)) {
1976 outs() << "Archive : " << Filename
<< "\n";
1978 printArchiveHeaders(Filename
, A
, !NonVerbose
, ArchiveMemberOffsets
);
1980 Error Err
= Error::success();
1981 for (auto &C
: A
->children(Err
)) {
1982 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
= C
.getAsBinary();
1984 if (auto E
= isNotObjectErrorInvalidFileType(ChildOrErr
.takeError()))
1985 report_error(Filename
, C
, std::move(E
));
1988 if (MachOObjectFile
*O
= dyn_cast
<MachOObjectFile
>(&*ChildOrErr
.get())) {
1989 if (!checkMachOAndArchFlags(O
, Filename
))
1991 ProcessMachO(Filename
, O
, O
->getFileName());
1995 report_error(Filename
, std::move(Err
));
1998 if (MachOUniversalBinary
*UB
= dyn_cast
<MachOUniversalBinary
>(&Bin
)) {
1999 ParseInputMachO(UB
);
2002 if (ObjectFile
*O
= dyn_cast
<ObjectFile
>(&Bin
)) {
2003 if (!checkMachOAndArchFlags(O
, Filename
))
2005 if (MachOObjectFile
*MachOOF
= dyn_cast
<MachOObjectFile
>(&*O
))
2006 ProcessMachO(Filename
, MachOOF
);
2008 errs() << "llvm-objdump: '" << Filename
<< "': "
2009 << "Object is not a Mach-O file type.\n";
2012 llvm_unreachable("Input object can't be invalid at this point");
2015 void llvm::ParseInputMachO(MachOUniversalBinary
*UB
) {
2016 if (!ValidateArchFlags())
2019 auto Filename
= UB
->getFileName();
2021 if (UniversalHeaders
)
2022 printMachOUniversalHeaders(UB
, !NonVerbose
);
2024 // If we have a list of architecture flags specified dump only those.
2025 if (!ArchAll
&& ArchFlags
.size() != 0) {
2026 // Look for a slice in the universal binary that matches each ArchFlag.
2028 for (unsigned i
= 0; i
< ArchFlags
.size(); ++i
) {
2030 for (MachOUniversalBinary::object_iterator I
= UB
->begin_objects(),
2031 E
= UB
->end_objects();
2033 if (ArchFlags
[i
] == I
->getArchFlagName()) {
2035 Expected
<std::unique_ptr
<ObjectFile
>> ObjOrErr
=
2036 I
->getAsObjectFile();
2037 std::string ArchitectureName
= "";
2038 if (ArchFlags
.size() > 1)
2039 ArchitectureName
= I
->getArchFlagName();
2041 ObjectFile
&O
= *ObjOrErr
.get();
2042 if (MachOObjectFile
*MachOOF
= dyn_cast
<MachOObjectFile
>(&O
))
2043 ProcessMachO(Filename
, MachOOF
, "", ArchitectureName
);
2044 } else if (auto E
= isNotObjectErrorInvalidFileType(
2045 ObjOrErr
.takeError())) {
2046 report_error(Filename
, StringRef(), std::move(E
),
2049 } else if (Expected
<std::unique_ptr
<Archive
>> AOrErr
=
2050 I
->getAsArchive()) {
2051 std::unique_ptr
<Archive
> &A
= *AOrErr
;
2052 outs() << "Archive : " << Filename
;
2053 if (!ArchitectureName
.empty())
2054 outs() << " (architecture " << ArchitectureName
<< ")";
2057 printArchiveHeaders(Filename
, A
.get(), !NonVerbose
,
2058 ArchiveMemberOffsets
, ArchitectureName
);
2059 Error Err
= Error::success();
2060 for (auto &C
: A
->children(Err
)) {
2061 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
= C
.getAsBinary();
2063 if (auto E
= isNotObjectErrorInvalidFileType(ChildOrErr
.takeError()))
2064 report_error(Filename
, C
, std::move(E
), ArchitectureName
);
2067 if (MachOObjectFile
*O
=
2068 dyn_cast
<MachOObjectFile
>(&*ChildOrErr
.get()))
2069 ProcessMachO(Filename
, O
, O
->getFileName(), ArchitectureName
);
2072 report_error(Filename
, std::move(Err
));
2074 consumeError(AOrErr
.takeError());
2075 error("Mach-O universal file: " + Filename
+ " for " +
2076 "architecture " + StringRef(I
->getArchFlagName()) +
2077 " is not a Mach-O file or an archive file");
2082 errs() << "llvm-objdump: file: " + Filename
+ " does not contain "
2083 << "architecture: " + ArchFlags
[i
] + "\n";
2089 // No architecture flags were specified so if this contains a slice that
2090 // matches the host architecture dump only that.
2092 for (MachOUniversalBinary::object_iterator I
= UB
->begin_objects(),
2093 E
= UB
->end_objects();
2095 if (MachOObjectFile::getHostArch().getArchName() ==
2096 I
->getArchFlagName()) {
2097 Expected
<std::unique_ptr
<ObjectFile
>> ObjOrErr
= I
->getAsObjectFile();
2098 std::string ArchiveName
;
2099 ArchiveName
.clear();
2101 ObjectFile
&O
= *ObjOrErr
.get();
2102 if (MachOObjectFile
*MachOOF
= dyn_cast
<MachOObjectFile
>(&O
))
2103 ProcessMachO(Filename
, MachOOF
);
2104 } else if (auto E
= isNotObjectErrorInvalidFileType(
2105 ObjOrErr
.takeError())) {
2106 report_error(Filename
, std::move(E
));
2107 } else if (Expected
<std::unique_ptr
<Archive
>> AOrErr
=
2108 I
->getAsArchive()) {
2109 std::unique_ptr
<Archive
> &A
= *AOrErr
;
2110 outs() << "Archive : " << Filename
<< "\n";
2112 printArchiveHeaders(Filename
, A
.get(), !NonVerbose
,
2113 ArchiveMemberOffsets
);
2114 Error Err
= Error::success();
2115 for (auto &C
: A
->children(Err
)) {
2116 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
= C
.getAsBinary();
2118 if (auto E
= isNotObjectErrorInvalidFileType(ChildOrErr
.takeError()))
2119 report_error(Filename
, C
, std::move(E
));
2122 if (MachOObjectFile
*O
=
2123 dyn_cast
<MachOObjectFile
>(&*ChildOrErr
.get()))
2124 ProcessMachO(Filename
, O
, O
->getFileName());
2127 report_error(Filename
, std::move(Err
));
2129 consumeError(AOrErr
.takeError());
2130 error("Mach-O universal file: " + Filename
+ " for architecture " +
2131 StringRef(I
->getArchFlagName()) +
2132 " is not a Mach-O file or an archive file");
2138 // Either all architectures have been specified or none have been specified
2139 // and this does not contain the host architecture so dump all the slices.
2140 bool moreThanOneArch
= UB
->getNumberOfObjects() > 1;
2141 for (MachOUniversalBinary::object_iterator I
= UB
->begin_objects(),
2142 E
= UB
->end_objects();
2144 Expected
<std::unique_ptr
<ObjectFile
>> ObjOrErr
= I
->getAsObjectFile();
2145 std::string ArchitectureName
= "";
2146 if (moreThanOneArch
)
2147 ArchitectureName
= I
->getArchFlagName();
2149 ObjectFile
&Obj
= *ObjOrErr
.get();
2150 if (MachOObjectFile
*MachOOF
= dyn_cast
<MachOObjectFile
>(&Obj
))
2151 ProcessMachO(Filename
, MachOOF
, "", ArchitectureName
);
2152 } else if (auto E
= isNotObjectErrorInvalidFileType(
2153 ObjOrErr
.takeError())) {
2154 report_error(StringRef(), Filename
, std::move(E
), ArchitectureName
);
2155 } else if (Expected
<std::unique_ptr
<Archive
>> AOrErr
=
2156 I
->getAsArchive()) {
2157 std::unique_ptr
<Archive
> &A
= *AOrErr
;
2158 outs() << "Archive : " << Filename
;
2159 if (!ArchitectureName
.empty())
2160 outs() << " (architecture " << ArchitectureName
<< ")";
2163 printArchiveHeaders(Filename
, A
.get(), !NonVerbose
,
2164 ArchiveMemberOffsets
, ArchitectureName
);
2165 Error Err
= Error::success();
2166 for (auto &C
: A
->children(Err
)) {
2167 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
= C
.getAsBinary();
2169 if (auto E
= isNotObjectErrorInvalidFileType(ChildOrErr
.takeError()))
2170 report_error(Filename
, C
, std::move(E
), ArchitectureName
);
2173 if (MachOObjectFile
*O
=
2174 dyn_cast
<MachOObjectFile
>(&*ChildOrErr
.get())) {
2175 if (MachOObjectFile
*MachOOF
= dyn_cast
<MachOObjectFile
>(O
))
2176 ProcessMachO(Filename
, MachOOF
, MachOOF
->getFileName(),
2181 report_error(Filename
, std::move(Err
));
2183 consumeError(AOrErr
.takeError());
2184 error("Mach-O universal file: " + Filename
+ " for architecture " +
2185 StringRef(I
->getArchFlagName()) +
2186 " is not a Mach-O file or an archive file");
2191 // The block of info used by the Symbolizer call backs.
2192 struct DisassembleInfo
{
2193 DisassembleInfo(MachOObjectFile
*O
, SymbolAddressMap
*AddrMap
,
2194 std::vector
<SectionRef
> *Sections
, bool verbose
)
2195 : verbose(verbose
), O(O
), AddrMap(AddrMap
), Sections(Sections
) {}
2199 SymbolAddressMap
*AddrMap
;
2200 std::vector
<SectionRef
> *Sections
;
2201 const char *class_name
= nullptr;
2202 const char *selector_name
= nullptr;
2203 std::unique_ptr
<char[]> method
= nullptr;
2204 char *demangled_name
= nullptr;
2205 uint64_t adrp_addr
= 0;
2206 uint32_t adrp_inst
= 0;
2207 std::unique_ptr
<SymbolAddressMap
> bindtable
;
2211 // SymbolizerGetOpInfo() is the operand information call back function.
2212 // This is called to get the symbolic information for operand(s) of an
2213 // instruction when it is being done. This routine does this from
2214 // the relocation information, symbol table, etc. That block of information
2215 // is a pointer to the struct DisassembleInfo that was passed when the
2216 // disassembler context was created and passed to back to here when
2217 // called back by the disassembler for instruction operands that could have
2218 // relocation information. The address of the instruction containing operand is
2219 // at the Pc parameter. The immediate value the operand has is passed in
2220 // op_info->Value and is at Offset past the start of the instruction and has a
2221 // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
2222 // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
2223 // names and addends of the symbolic expression to add for the operand. The
2224 // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
2225 // information is returned then this function returns 1 else it returns 0.
2226 static int SymbolizerGetOpInfo(void *DisInfo
, uint64_t Pc
, uint64_t Offset
,
2227 uint64_t Size
, int TagType
, void *TagBuf
) {
2228 struct DisassembleInfo
*info
= (struct DisassembleInfo
*)DisInfo
;
2229 struct LLVMOpInfo1
*op_info
= (struct LLVMOpInfo1
*)TagBuf
;
2230 uint64_t value
= op_info
->Value
;
2232 // Make sure all fields returned are zero if we don't set them.
2233 memset((void *)op_info
, '\0', sizeof(struct LLVMOpInfo1
));
2234 op_info
->Value
= value
;
2236 // If the TagType is not the value 1 which it code knows about or if no
2237 // verbose symbolic information is wanted then just return 0, indicating no
2238 // information is being returned.
2239 if (TagType
!= 1 || !info
->verbose
)
2242 unsigned int Arch
= info
->O
->getArch();
2243 if (Arch
== Triple::x86
) {
2244 if (Size
!= 1 && Size
!= 2 && Size
!= 4 && Size
!= 0)
2246 if (info
->O
->getHeader().filetype
!= MachO::MH_OBJECT
) {
2248 // Search the external relocation entries of a fully linked image
2249 // (if any) for an entry that matches this segment offset.
2250 // uint32_t seg_offset = (Pc + Offset);
2253 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2254 // for an entry for this section offset.
2255 uint32_t sect_addr
= info
->S
.getAddress();
2256 uint32_t sect_offset
= (Pc
+ Offset
) - sect_addr
;
2257 bool reloc_found
= false;
2259 MachO::any_relocation_info RE
;
2260 bool isExtern
= false;
2262 bool r_scattered
= false;
2263 uint32_t r_value
, pair_r_value
, r_type
;
2264 for (const RelocationRef
&Reloc
: info
->S
.relocations()) {
2265 uint64_t RelocOffset
= Reloc
.getOffset();
2266 if (RelocOffset
== sect_offset
) {
2267 Rel
= Reloc
.getRawDataRefImpl();
2268 RE
= info
->O
->getRelocation(Rel
);
2269 r_type
= info
->O
->getAnyRelocationType(RE
);
2270 r_scattered
= info
->O
->isRelocationScattered(RE
);
2272 r_value
= info
->O
->getScatteredRelocationValue(RE
);
2273 if (r_type
== MachO::GENERIC_RELOC_SECTDIFF
||
2274 r_type
== MachO::GENERIC_RELOC_LOCAL_SECTDIFF
) {
2275 DataRefImpl RelNext
= Rel
;
2276 info
->O
->moveRelocationNext(RelNext
);
2277 MachO::any_relocation_info RENext
;
2278 RENext
= info
->O
->getRelocation(RelNext
);
2279 if (info
->O
->isRelocationScattered(RENext
))
2280 pair_r_value
= info
->O
->getScatteredRelocationValue(RENext
);
2285 isExtern
= info
->O
->getPlainRelocationExternal(RE
);
2287 symbol_iterator RelocSym
= Reloc
.getSymbol();
2295 if (reloc_found
&& isExtern
) {
2296 Expected
<StringRef
> SymName
= Symbol
.getName();
2298 report_error(info
->O
->getFileName(), SymName
.takeError());
2299 const char *name
= SymName
->data();
2300 op_info
->AddSymbol
.Present
= 1;
2301 op_info
->AddSymbol
.Name
= name
;
2302 // For i386 extern relocation entries the value in the instruction is
2303 // the offset from the symbol, and value is already set in op_info->Value.
2306 if (reloc_found
&& (r_type
== MachO::GENERIC_RELOC_SECTDIFF
||
2307 r_type
== MachO::GENERIC_RELOC_LOCAL_SECTDIFF
)) {
2308 const char *add
= GuessSymbolName(r_value
, info
->AddrMap
);
2309 const char *sub
= GuessSymbolName(pair_r_value
, info
->AddrMap
);
2310 uint32_t offset
= value
- (r_value
- pair_r_value
);
2311 op_info
->AddSymbol
.Present
= 1;
2313 op_info
->AddSymbol
.Name
= add
;
2315 op_info
->AddSymbol
.Value
= r_value
;
2316 op_info
->SubtractSymbol
.Present
= 1;
2318 op_info
->SubtractSymbol
.Name
= sub
;
2320 op_info
->SubtractSymbol
.Value
= pair_r_value
;
2321 op_info
->Value
= offset
;
2326 if (Arch
== Triple::x86_64
) {
2327 if (Size
!= 1 && Size
!= 2 && Size
!= 4 && Size
!= 0)
2329 // For non MH_OBJECT types, like MH_KEXT_BUNDLE, Search the external
2330 // relocation entries of a linked image (if any) for an entry that matches
2331 // this segment offset.
2332 if (info
->O
->getHeader().filetype
!= MachO::MH_OBJECT
) {
2333 uint64_t seg_offset
= Pc
+ Offset
;
2334 bool reloc_found
= false;
2336 MachO::any_relocation_info RE
;
2337 bool isExtern
= false;
2339 for (const RelocationRef
&Reloc
: info
->O
->external_relocations()) {
2340 uint64_t RelocOffset
= Reloc
.getOffset();
2341 if (RelocOffset
== seg_offset
) {
2342 Rel
= Reloc
.getRawDataRefImpl();
2343 RE
= info
->O
->getRelocation(Rel
);
2344 // external relocation entries should always be external.
2345 isExtern
= info
->O
->getPlainRelocationExternal(RE
);
2347 symbol_iterator RelocSym
= Reloc
.getSymbol();
2354 if (reloc_found
&& isExtern
) {
2355 // The Value passed in will be adjusted by the Pc if the instruction
2356 // adds the Pc. But for x86_64 external relocation entries the Value
2357 // is the offset from the external symbol.
2358 if (info
->O
->getAnyRelocationPCRel(RE
))
2359 op_info
->Value
-= Pc
+ Offset
+ Size
;
2360 Expected
<StringRef
> SymName
= Symbol
.getName();
2362 report_error(info
->O
->getFileName(), SymName
.takeError());
2363 const char *name
= SymName
->data();
2364 op_info
->AddSymbol
.Present
= 1;
2365 op_info
->AddSymbol
.Name
= name
;
2370 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2371 // for an entry for this section offset.
2372 uint64_t sect_addr
= info
->S
.getAddress();
2373 uint64_t sect_offset
= (Pc
+ Offset
) - sect_addr
;
2374 bool reloc_found
= false;
2376 MachO::any_relocation_info RE
;
2377 bool isExtern
= false;
2379 for (const RelocationRef
&Reloc
: info
->S
.relocations()) {
2380 uint64_t RelocOffset
= Reloc
.getOffset();
2381 if (RelocOffset
== sect_offset
) {
2382 Rel
= Reloc
.getRawDataRefImpl();
2383 RE
= info
->O
->getRelocation(Rel
);
2384 // NOTE: Scattered relocations don't exist on x86_64.
2385 isExtern
= info
->O
->getPlainRelocationExternal(RE
);
2387 symbol_iterator RelocSym
= Reloc
.getSymbol();
2394 if (reloc_found
&& isExtern
) {
2395 // The Value passed in will be adjusted by the Pc if the instruction
2396 // adds the Pc. But for x86_64 external relocation entries the Value
2397 // is the offset from the external symbol.
2398 if (info
->O
->getAnyRelocationPCRel(RE
))
2399 op_info
->Value
-= Pc
+ Offset
+ Size
;
2400 Expected
<StringRef
> SymName
= Symbol
.getName();
2402 report_error(info
->O
->getFileName(), SymName
.takeError());
2403 const char *name
= SymName
->data();
2404 unsigned Type
= info
->O
->getAnyRelocationType(RE
);
2405 if (Type
== MachO::X86_64_RELOC_SUBTRACTOR
) {
2406 DataRefImpl RelNext
= Rel
;
2407 info
->O
->moveRelocationNext(RelNext
);
2408 MachO::any_relocation_info RENext
= info
->O
->getRelocation(RelNext
);
2409 unsigned TypeNext
= info
->O
->getAnyRelocationType(RENext
);
2410 bool isExternNext
= info
->O
->getPlainRelocationExternal(RENext
);
2411 unsigned SymbolNum
= info
->O
->getPlainRelocationSymbolNum(RENext
);
2412 if (TypeNext
== MachO::X86_64_RELOC_UNSIGNED
&& isExternNext
) {
2413 op_info
->SubtractSymbol
.Present
= 1;
2414 op_info
->SubtractSymbol
.Name
= name
;
2415 symbol_iterator RelocSymNext
= info
->O
->getSymbolByIndex(SymbolNum
);
2416 Symbol
= *RelocSymNext
;
2417 Expected
<StringRef
> SymNameNext
= Symbol
.getName();
2419 report_error(info
->O
->getFileName(), SymNameNext
.takeError());
2420 name
= SymNameNext
->data();
2423 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
2424 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
2425 op_info
->AddSymbol
.Present
= 1;
2426 op_info
->AddSymbol
.Name
= name
;
2431 if (Arch
== Triple::arm
) {
2432 if (Offset
!= 0 || (Size
!= 4 && Size
!= 2))
2434 if (info
->O
->getHeader().filetype
!= MachO::MH_OBJECT
) {
2436 // Search the external relocation entries of a fully linked image
2437 // (if any) for an entry that matches this segment offset.
2438 // uint32_t seg_offset = (Pc + Offset);
2441 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2442 // for an entry for this section offset.
2443 uint32_t sect_addr
= info
->S
.getAddress();
2444 uint32_t sect_offset
= (Pc
+ Offset
) - sect_addr
;
2446 MachO::any_relocation_info RE
;
2447 bool isExtern
= false;
2449 bool r_scattered
= false;
2450 uint32_t r_value
, pair_r_value
, r_type
, r_length
, other_half
;
2452 find_if(info
->S
.relocations(), [&](const RelocationRef
&Reloc
) {
2453 uint64_t RelocOffset
= Reloc
.getOffset();
2454 return RelocOffset
== sect_offset
;
2457 if (Reloc
== info
->S
.relocations().end())
2460 Rel
= Reloc
->getRawDataRefImpl();
2461 RE
= info
->O
->getRelocation(Rel
);
2462 r_length
= info
->O
->getAnyRelocationLength(RE
);
2463 r_scattered
= info
->O
->isRelocationScattered(RE
);
2465 r_value
= info
->O
->getScatteredRelocationValue(RE
);
2466 r_type
= info
->O
->getScatteredRelocationType(RE
);
2468 r_type
= info
->O
->getAnyRelocationType(RE
);
2469 isExtern
= info
->O
->getPlainRelocationExternal(RE
);
2471 symbol_iterator RelocSym
= Reloc
->getSymbol();
2475 if (r_type
== MachO::ARM_RELOC_HALF
||
2476 r_type
== MachO::ARM_RELOC_SECTDIFF
||
2477 r_type
== MachO::ARM_RELOC_LOCAL_SECTDIFF
||
2478 r_type
== MachO::ARM_RELOC_HALF_SECTDIFF
) {
2479 DataRefImpl RelNext
= Rel
;
2480 info
->O
->moveRelocationNext(RelNext
);
2481 MachO::any_relocation_info RENext
;
2482 RENext
= info
->O
->getRelocation(RelNext
);
2483 other_half
= info
->O
->getAnyRelocationAddress(RENext
) & 0xffff;
2484 if (info
->O
->isRelocationScattered(RENext
))
2485 pair_r_value
= info
->O
->getScatteredRelocationValue(RENext
);
2489 Expected
<StringRef
> SymName
= Symbol
.getName();
2491 report_error(info
->O
->getFileName(), SymName
.takeError());
2492 const char *name
= SymName
->data();
2493 op_info
->AddSymbol
.Present
= 1;
2494 op_info
->AddSymbol
.Name
= name
;
2496 case MachO::ARM_RELOC_HALF
:
2497 if ((r_length
& 0x1) == 1) {
2498 op_info
->Value
= value
<< 16 | other_half
;
2499 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM_HI16
;
2501 op_info
->Value
= other_half
<< 16 | value
;
2502 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM_LO16
;
2510 // If we have a branch that is not an external relocation entry then
2511 // return 0 so the code in tryAddingSymbolicOperand() can use the
2512 // SymbolLookUp call back with the branch target address to look up the
2513 // symbol and possibility add an annotation for a symbol stub.
2514 if (isExtern
== 0 && (r_type
== MachO::ARM_RELOC_BR24
||
2515 r_type
== MachO::ARM_THUMB_RELOC_BR22
))
2518 uint32_t offset
= 0;
2519 if (r_type
== MachO::ARM_RELOC_HALF
||
2520 r_type
== MachO::ARM_RELOC_HALF_SECTDIFF
) {
2521 if ((r_length
& 0x1) == 1)
2522 value
= value
<< 16 | other_half
;
2524 value
= other_half
<< 16 | value
;
2526 if (r_scattered
&& (r_type
!= MachO::ARM_RELOC_HALF
&&
2527 r_type
!= MachO::ARM_RELOC_HALF_SECTDIFF
)) {
2528 offset
= value
- r_value
;
2532 if (r_type
== MachO::ARM_RELOC_HALF_SECTDIFF
) {
2533 if ((r_length
& 0x1) == 1)
2534 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM_HI16
;
2536 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM_LO16
;
2537 const char *add
= GuessSymbolName(r_value
, info
->AddrMap
);
2538 const char *sub
= GuessSymbolName(pair_r_value
, info
->AddrMap
);
2539 int32_t offset
= value
- (r_value
- pair_r_value
);
2540 op_info
->AddSymbol
.Present
= 1;
2542 op_info
->AddSymbol
.Name
= add
;
2544 op_info
->AddSymbol
.Value
= r_value
;
2545 op_info
->SubtractSymbol
.Present
= 1;
2547 op_info
->SubtractSymbol
.Name
= sub
;
2549 op_info
->SubtractSymbol
.Value
= pair_r_value
;
2550 op_info
->Value
= offset
;
2554 op_info
->AddSymbol
.Present
= 1;
2555 op_info
->Value
= offset
;
2556 if (r_type
== MachO::ARM_RELOC_HALF
) {
2557 if ((r_length
& 0x1) == 1)
2558 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM_HI16
;
2560 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM_LO16
;
2562 const char *add
= GuessSymbolName(value
, info
->AddrMap
);
2563 if (add
!= nullptr) {
2564 op_info
->AddSymbol
.Name
= add
;
2567 op_info
->AddSymbol
.Value
= value
;
2570 if (Arch
== Triple::aarch64
) {
2571 if (Offset
!= 0 || Size
!= 4)
2573 if (info
->O
->getHeader().filetype
!= MachO::MH_OBJECT
) {
2575 // Search the external relocation entries of a fully linked image
2576 // (if any) for an entry that matches this segment offset.
2577 // uint64_t seg_offset = (Pc + Offset);
2580 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2581 // for an entry for this section offset.
2582 uint64_t sect_addr
= info
->S
.getAddress();
2583 uint64_t sect_offset
= (Pc
+ Offset
) - sect_addr
;
2585 find_if(info
->S
.relocations(), [&](const RelocationRef
&Reloc
) {
2586 uint64_t RelocOffset
= Reloc
.getOffset();
2587 return RelocOffset
== sect_offset
;
2590 if (Reloc
== info
->S
.relocations().end())
2593 DataRefImpl Rel
= Reloc
->getRawDataRefImpl();
2594 MachO::any_relocation_info RE
= info
->O
->getRelocation(Rel
);
2595 uint32_t r_type
= info
->O
->getAnyRelocationType(RE
);
2596 if (r_type
== MachO::ARM64_RELOC_ADDEND
) {
2597 DataRefImpl RelNext
= Rel
;
2598 info
->O
->moveRelocationNext(RelNext
);
2599 MachO::any_relocation_info RENext
= info
->O
->getRelocation(RelNext
);
2601 value
= info
->O
->getPlainRelocationSymbolNum(RENext
);
2602 op_info
->Value
= value
;
2605 // NOTE: Scattered relocations don't exist on arm64.
2606 if (!info
->O
->getPlainRelocationExternal(RE
))
2608 Expected
<StringRef
> SymName
= Reloc
->getSymbol()->getName();
2610 report_error(info
->O
->getFileName(), SymName
.takeError());
2611 const char *name
= SymName
->data();
2612 op_info
->AddSymbol
.Present
= 1;
2613 op_info
->AddSymbol
.Name
= name
;
2616 case MachO::ARM64_RELOC_PAGE21
:
2618 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM64_PAGE
;
2620 case MachO::ARM64_RELOC_PAGEOFF12
:
2622 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM64_PAGEOFF
;
2624 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21
:
2626 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM64_GOTPAGE
;
2628 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12
:
2630 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF
;
2632 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21
:
2633 /* @tvlppage is not implemented in llvm-mc */
2634 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM64_TLVP
;
2636 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12
:
2637 /* @tvlppageoff is not implemented in llvm-mc */
2638 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM64_TLVOFF
;
2641 case MachO::ARM64_RELOC_BRANCH26
:
2642 op_info
->VariantKind
= LLVMDisassembler_VariantKind_None
;
2650 // GuessCstringPointer is passed the address of what might be a pointer to a
2651 // literal string in a cstring section. If that address is in a cstring section
2652 // it returns a pointer to that string. Else it returns nullptr.
2653 static const char *GuessCstringPointer(uint64_t ReferenceValue
,
2654 struct DisassembleInfo
*info
) {
2655 for (const auto &Load
: info
->O
->load_commands()) {
2656 if (Load
.C
.cmd
== MachO::LC_SEGMENT_64
) {
2657 MachO::segment_command_64 Seg
= info
->O
->getSegment64LoadCommand(Load
);
2658 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
2659 MachO::section_64 Sec
= info
->O
->getSection64(Load
, J
);
2660 uint32_t section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
2661 if (section_type
== MachO::S_CSTRING_LITERALS
&&
2662 ReferenceValue
>= Sec
.addr
&&
2663 ReferenceValue
< Sec
.addr
+ Sec
.size
) {
2664 uint64_t sect_offset
= ReferenceValue
- Sec
.addr
;
2665 uint64_t object_offset
= Sec
.offset
+ sect_offset
;
2666 StringRef MachOContents
= info
->O
->getData();
2667 uint64_t object_size
= MachOContents
.size();
2668 const char *object_addr
= (const char *)MachOContents
.data();
2669 if (object_offset
< object_size
) {
2670 const char *name
= object_addr
+ object_offset
;
2677 } else if (Load
.C
.cmd
== MachO::LC_SEGMENT
) {
2678 MachO::segment_command Seg
= info
->O
->getSegmentLoadCommand(Load
);
2679 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
2680 MachO::section Sec
= info
->O
->getSection(Load
, J
);
2681 uint32_t section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
2682 if (section_type
== MachO::S_CSTRING_LITERALS
&&
2683 ReferenceValue
>= Sec
.addr
&&
2684 ReferenceValue
< Sec
.addr
+ Sec
.size
) {
2685 uint64_t sect_offset
= ReferenceValue
- Sec
.addr
;
2686 uint64_t object_offset
= Sec
.offset
+ sect_offset
;
2687 StringRef MachOContents
= info
->O
->getData();
2688 uint64_t object_size
= MachOContents
.size();
2689 const char *object_addr
= (const char *)MachOContents
.data();
2690 if (object_offset
< object_size
) {
2691 const char *name
= object_addr
+ object_offset
;
2703 // GuessIndirectSymbol returns the name of the indirect symbol for the
2704 // ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
2705 // an address of a symbol stub or a lazy or non-lazy pointer to associate the
2706 // symbol name being referenced by the stub or pointer.
2707 static const char *GuessIndirectSymbol(uint64_t ReferenceValue
,
2708 struct DisassembleInfo
*info
) {
2709 MachO::dysymtab_command Dysymtab
= info
->O
->getDysymtabLoadCommand();
2710 MachO::symtab_command Symtab
= info
->O
->getSymtabLoadCommand();
2711 for (const auto &Load
: info
->O
->load_commands()) {
2712 if (Load
.C
.cmd
== MachO::LC_SEGMENT_64
) {
2713 MachO::segment_command_64 Seg
= info
->O
->getSegment64LoadCommand(Load
);
2714 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
2715 MachO::section_64 Sec
= info
->O
->getSection64(Load
, J
);
2716 uint32_t section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
2717 if ((section_type
== MachO::S_NON_LAZY_SYMBOL_POINTERS
||
2718 section_type
== MachO::S_LAZY_SYMBOL_POINTERS
||
2719 section_type
== MachO::S_LAZY_DYLIB_SYMBOL_POINTERS
||
2720 section_type
== MachO::S_THREAD_LOCAL_VARIABLE_POINTERS
||
2721 section_type
== MachO::S_SYMBOL_STUBS
) &&
2722 ReferenceValue
>= Sec
.addr
&&
2723 ReferenceValue
< Sec
.addr
+ Sec
.size
) {
2725 if (section_type
== MachO::S_SYMBOL_STUBS
)
2726 stride
= Sec
.reserved2
;
2731 uint32_t index
= Sec
.reserved1
+ (ReferenceValue
- Sec
.addr
) / stride
;
2732 if (index
< Dysymtab
.nindirectsyms
) {
2733 uint32_t indirect_symbol
=
2734 info
->O
->getIndirectSymbolTableEntry(Dysymtab
, index
);
2735 if (indirect_symbol
< Symtab
.nsyms
) {
2736 symbol_iterator Sym
= info
->O
->getSymbolByIndex(indirect_symbol
);
2737 SymbolRef Symbol
= *Sym
;
2738 Expected
<StringRef
> SymName
= Symbol
.getName();
2740 report_error(info
->O
->getFileName(), SymName
.takeError());
2741 const char *name
= SymName
->data();
2747 } else if (Load
.C
.cmd
== MachO::LC_SEGMENT
) {
2748 MachO::segment_command Seg
= info
->O
->getSegmentLoadCommand(Load
);
2749 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
2750 MachO::section Sec
= info
->O
->getSection(Load
, J
);
2751 uint32_t section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
2752 if ((section_type
== MachO::S_NON_LAZY_SYMBOL_POINTERS
||
2753 section_type
== MachO::S_LAZY_SYMBOL_POINTERS
||
2754 section_type
== MachO::S_LAZY_DYLIB_SYMBOL_POINTERS
||
2755 section_type
== MachO::S_THREAD_LOCAL_VARIABLE_POINTERS
||
2756 section_type
== MachO::S_SYMBOL_STUBS
) &&
2757 ReferenceValue
>= Sec
.addr
&&
2758 ReferenceValue
< Sec
.addr
+ Sec
.size
) {
2760 if (section_type
== MachO::S_SYMBOL_STUBS
)
2761 stride
= Sec
.reserved2
;
2766 uint32_t index
= Sec
.reserved1
+ (ReferenceValue
- Sec
.addr
) / stride
;
2767 if (index
< Dysymtab
.nindirectsyms
) {
2768 uint32_t indirect_symbol
=
2769 info
->O
->getIndirectSymbolTableEntry(Dysymtab
, index
);
2770 if (indirect_symbol
< Symtab
.nsyms
) {
2771 symbol_iterator Sym
= info
->O
->getSymbolByIndex(indirect_symbol
);
2772 SymbolRef Symbol
= *Sym
;
2773 Expected
<StringRef
> SymName
= Symbol
.getName();
2775 report_error(info
->O
->getFileName(), SymName
.takeError());
2776 const char *name
= SymName
->data();
2787 // method_reference() is called passing it the ReferenceName that might be
2788 // a reference it to an Objective-C method call. If so then it allocates and
2789 // assembles a method call string with the values last seen and saved in
2790 // the DisassembleInfo's class_name and selector_name fields. This is saved
2791 // into the method field of the info and any previous string is free'ed.
2792 // Then the class_name field in the info is set to nullptr. The method call
2793 // string is set into ReferenceName and ReferenceType is set to
2794 // LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
2795 // then both ReferenceType and ReferenceName are left unchanged.
2796 static void method_reference(struct DisassembleInfo
*info
,
2797 uint64_t *ReferenceType
,
2798 const char **ReferenceName
) {
2799 unsigned int Arch
= info
->O
->getArch();
2800 if (*ReferenceName
!= nullptr) {
2801 if (strcmp(*ReferenceName
, "_objc_msgSend") == 0) {
2802 if (info
->selector_name
!= nullptr) {
2803 if (info
->class_name
!= nullptr) {
2804 info
->method
= llvm::make_unique
<char[]>(
2805 5 + strlen(info
->class_name
) + strlen(info
->selector_name
));
2806 char *method
= info
->method
.get();
2807 if (method
!= nullptr) {
2808 strcpy(method
, "+[");
2809 strcat(method
, info
->class_name
);
2810 strcat(method
, " ");
2811 strcat(method
, info
->selector_name
);
2812 strcat(method
, "]");
2813 *ReferenceName
= method
;
2814 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_Objc_Message
;
2818 llvm::make_unique
<char[]>(9 + strlen(info
->selector_name
));
2819 char *method
= info
->method
.get();
2820 if (method
!= nullptr) {
2821 if (Arch
== Triple::x86_64
)
2822 strcpy(method
, "-[%rdi ");
2823 else if (Arch
== Triple::aarch64
)
2824 strcpy(method
, "-[x0 ");
2826 strcpy(method
, "-[r? ");
2827 strcat(method
, info
->selector_name
);
2828 strcat(method
, "]");
2829 *ReferenceName
= method
;
2830 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_Objc_Message
;
2833 info
->class_name
= nullptr;
2835 } else if (strcmp(*ReferenceName
, "_objc_msgSendSuper2") == 0) {
2836 if (info
->selector_name
!= nullptr) {
2838 llvm::make_unique
<char[]>(17 + strlen(info
->selector_name
));
2839 char *method
= info
->method
.get();
2840 if (method
!= nullptr) {
2841 if (Arch
== Triple::x86_64
)
2842 strcpy(method
, "-[[%rdi super] ");
2843 else if (Arch
== Triple::aarch64
)
2844 strcpy(method
, "-[[x0 super] ");
2846 strcpy(method
, "-[[r? super] ");
2847 strcat(method
, info
->selector_name
);
2848 strcat(method
, "]");
2849 *ReferenceName
= method
;
2850 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_Objc_Message
;
2852 info
->class_name
= nullptr;
2858 // GuessPointerPointer() is passed the address of what might be a pointer to
2859 // a reference to an Objective-C class, selector, message ref or cfstring.
2860 // If so the value of the pointer is returned and one of the booleans are set
2861 // to true. If not zero is returned and all the booleans are set to false.
2862 static uint64_t GuessPointerPointer(uint64_t ReferenceValue
,
2863 struct DisassembleInfo
*info
,
2864 bool &classref
, bool &selref
, bool &msgref
,
2870 for (const auto &Load
: info
->O
->load_commands()) {
2871 if (Load
.C
.cmd
== MachO::LC_SEGMENT_64
) {
2872 MachO::segment_command_64 Seg
= info
->O
->getSegment64LoadCommand(Load
);
2873 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
2874 MachO::section_64 Sec
= info
->O
->getSection64(Load
, J
);
2875 if ((strncmp(Sec
.sectname
, "__objc_selrefs", 16) == 0 ||
2876 strncmp(Sec
.sectname
, "__objc_classrefs", 16) == 0 ||
2877 strncmp(Sec
.sectname
, "__objc_superrefs", 16) == 0 ||
2878 strncmp(Sec
.sectname
, "__objc_msgrefs", 16) == 0 ||
2879 strncmp(Sec
.sectname
, "__cfstring", 16) == 0) &&
2880 ReferenceValue
>= Sec
.addr
&&
2881 ReferenceValue
< Sec
.addr
+ Sec
.size
) {
2882 uint64_t sect_offset
= ReferenceValue
- Sec
.addr
;
2883 uint64_t object_offset
= Sec
.offset
+ sect_offset
;
2884 StringRef MachOContents
= info
->O
->getData();
2885 uint64_t object_size
= MachOContents
.size();
2886 const char *object_addr
= (const char *)MachOContents
.data();
2887 if (object_offset
< object_size
) {
2888 uint64_t pointer_value
;
2889 memcpy(&pointer_value
, object_addr
+ object_offset
,
2891 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
2892 sys::swapByteOrder(pointer_value
);
2893 if (strncmp(Sec
.sectname
, "__objc_selrefs", 16) == 0)
2895 else if (strncmp(Sec
.sectname
, "__objc_classrefs", 16) == 0 ||
2896 strncmp(Sec
.sectname
, "__objc_superrefs", 16) == 0)
2898 else if (strncmp(Sec
.sectname
, "__objc_msgrefs", 16) == 0 &&
2899 ReferenceValue
+ 8 < Sec
.addr
+ Sec
.size
) {
2901 memcpy(&pointer_value
, object_addr
+ object_offset
+ 8,
2903 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
2904 sys::swapByteOrder(pointer_value
);
2905 } else if (strncmp(Sec
.sectname
, "__cfstring", 16) == 0)
2907 return pointer_value
;
2914 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
2919 // get_pointer_64 returns a pointer to the bytes in the object file at the
2920 // Address from a section in the Mach-O file. And indirectly returns the
2921 // offset into the section, number of bytes left in the section past the offset
2922 // and which section is was being referenced. If the Address is not in a
2923 // section nullptr is returned.
2924 static const char *get_pointer_64(uint64_t Address
, uint32_t &offset
,
2925 uint32_t &left
, SectionRef
&S
,
2926 DisassembleInfo
*info
,
2927 bool objc_only
= false) {
2931 for (unsigned SectIdx
= 0; SectIdx
!= info
->Sections
->size(); SectIdx
++) {
2932 uint64_t SectAddress
= ((*(info
->Sections
))[SectIdx
]).getAddress();
2933 uint64_t SectSize
= ((*(info
->Sections
))[SectIdx
]).getSize();
2938 ((*(info
->Sections
))[SectIdx
]).getName(SectName
);
2939 DataRefImpl Ref
= ((*(info
->Sections
))[SectIdx
]).getRawDataRefImpl();
2940 StringRef SegName
= info
->O
->getSectionFinalSegmentName(Ref
);
2941 if (SegName
!= "__OBJC" && SectName
!= "__cstring")
2944 if (Address
>= SectAddress
&& Address
< SectAddress
+ SectSize
) {
2945 S
= (*(info
->Sections
))[SectIdx
];
2946 offset
= Address
- SectAddress
;
2947 left
= SectSize
- offset
;
2948 StringRef SectContents
;
2949 ((*(info
->Sections
))[SectIdx
]).getContents(SectContents
);
2950 return SectContents
.data() + offset
;
2956 static const char *get_pointer_32(uint32_t Address
, uint32_t &offset
,
2957 uint32_t &left
, SectionRef
&S
,
2958 DisassembleInfo
*info
,
2959 bool objc_only
= false) {
2960 return get_pointer_64(Address
, offset
, left
, S
, info
, objc_only
);
2963 // get_symbol_64() returns the name of a symbol (or nullptr) and the address of
2964 // the symbol indirectly through n_value. Based on the relocation information
2965 // for the specified section offset in the specified section reference.
2966 // If no relocation information is found and a non-zero ReferenceValue for the
2967 // symbol is passed, look up that address in the info's AddrMap.
2968 static const char *get_symbol_64(uint32_t sect_offset
, SectionRef S
,
2969 DisassembleInfo
*info
, uint64_t &n_value
,
2970 uint64_t ReferenceValue
= 0) {
2975 // See if there is an external relocation entry at the sect_offset.
2976 bool reloc_found
= false;
2978 MachO::any_relocation_info RE
;
2979 bool isExtern
= false;
2981 for (const RelocationRef
&Reloc
: S
.relocations()) {
2982 uint64_t RelocOffset
= Reloc
.getOffset();
2983 if (RelocOffset
== sect_offset
) {
2984 Rel
= Reloc
.getRawDataRefImpl();
2985 RE
= info
->O
->getRelocation(Rel
);
2986 if (info
->O
->isRelocationScattered(RE
))
2988 isExtern
= info
->O
->getPlainRelocationExternal(RE
);
2990 symbol_iterator RelocSym
= Reloc
.getSymbol();
2997 // If there is an external relocation entry for a symbol in this section
2998 // at this section_offset then use that symbol's value for the n_value
2999 // and return its name.
3000 const char *SymbolName
= nullptr;
3001 if (reloc_found
&& isExtern
) {
3002 n_value
= Symbol
.getValue();
3003 Expected
<StringRef
> NameOrError
= Symbol
.getName();
3005 report_error(info
->O
->getFileName(), NameOrError
.takeError());
3006 StringRef Name
= *NameOrError
;
3007 if (!Name
.empty()) {
3008 SymbolName
= Name
.data();
3013 // TODO: For fully linked images, look through the external relocation
3014 // entries off the dynamic symtab command. For these the r_offset is from the
3015 // start of the first writeable segment in the Mach-O file. So the offset
3016 // to this section from that segment is passed to this routine by the caller,
3017 // as the database_offset. Which is the difference of the section's starting
3018 // address and the first writable segment.
3020 // NOTE: need add passing the database_offset to this routine.
3022 // We did not find an external relocation entry so look up the ReferenceValue
3023 // as an address of a symbol and if found return that symbol's name.
3024 SymbolName
= GuessSymbolName(ReferenceValue
, info
->AddrMap
);
3029 static const char *get_symbol_32(uint32_t sect_offset
, SectionRef S
,
3030 DisassembleInfo
*info
,
3031 uint32_t ReferenceValue
) {
3033 return get_symbol_64(sect_offset
, S
, info
, n_value64
, ReferenceValue
);
3036 // These are structs in the Objective-C meta data and read to produce the
3037 // comments for disassembly. While these are part of the ABI they are no
3038 // public defintions. So the are here not in include/llvm/BinaryFormat/MachO.h
3041 // The cfstring object in a 64-bit Mach-O file.
3042 struct cfstring64_t
{
3043 uint64_t isa
; // class64_t * (64-bit pointer)
3044 uint64_t flags
; // flag bits
3045 uint64_t characters
; // char * (64-bit pointer)
3046 uint64_t length
; // number of non-NULL characters in above
3049 // The class object in a 64-bit Mach-O file.
3051 uint64_t isa
; // class64_t * (64-bit pointer)
3052 uint64_t superclass
; // class64_t * (64-bit pointer)
3053 uint64_t cache
; // Cache (64-bit pointer)
3054 uint64_t vtable
; // IMP * (64-bit pointer)
3055 uint64_t data
; // class_ro64_t * (64-bit pointer)
3059 uint32_t isa
; /* class32_t * (32-bit pointer) */
3060 uint32_t superclass
; /* class32_t * (32-bit pointer) */
3061 uint32_t cache
; /* Cache (32-bit pointer) */
3062 uint32_t vtable
; /* IMP * (32-bit pointer) */
3063 uint32_t data
; /* class_ro32_t * (32-bit pointer) */
3066 struct class_ro64_t
{
3068 uint32_t instanceStart
;
3069 uint32_t instanceSize
;
3071 uint64_t ivarLayout
; // const uint8_t * (64-bit pointer)
3072 uint64_t name
; // const char * (64-bit pointer)
3073 uint64_t baseMethods
; // const method_list_t * (64-bit pointer)
3074 uint64_t baseProtocols
; // const protocol_list_t * (64-bit pointer)
3075 uint64_t ivars
; // const ivar_list_t * (64-bit pointer)
3076 uint64_t weakIvarLayout
; // const uint8_t * (64-bit pointer)
3077 uint64_t baseProperties
; // const struct objc_property_list (64-bit pointer)
3080 struct class_ro32_t
{
3082 uint32_t instanceStart
;
3083 uint32_t instanceSize
;
3084 uint32_t ivarLayout
; /* const uint8_t * (32-bit pointer) */
3085 uint32_t name
; /* const char * (32-bit pointer) */
3086 uint32_t baseMethods
; /* const method_list_t * (32-bit pointer) */
3087 uint32_t baseProtocols
; /* const protocol_list_t * (32-bit pointer) */
3088 uint32_t ivars
; /* const ivar_list_t * (32-bit pointer) */
3089 uint32_t weakIvarLayout
; /* const uint8_t * (32-bit pointer) */
3090 uint32_t baseProperties
; /* const struct objc_property_list *
3094 /* Values for class_ro{64,32}_t->flags */
3095 #define RO_META (1 << 0)
3096 #define RO_ROOT (1 << 1)
3097 #define RO_HAS_CXX_STRUCTORS (1 << 2)
3099 struct method_list64_t
{
3102 /* struct method64_t first; These structures follow inline */
3105 struct method_list32_t
{
3108 /* struct method32_t first; These structures follow inline */
3112 uint64_t name
; /* SEL (64-bit pointer) */
3113 uint64_t types
; /* const char * (64-bit pointer) */
3114 uint64_t imp
; /* IMP (64-bit pointer) */
3118 uint32_t name
; /* SEL (32-bit pointer) */
3119 uint32_t types
; /* const char * (32-bit pointer) */
3120 uint32_t imp
; /* IMP (32-bit pointer) */
3123 struct protocol_list64_t
{
3124 uint64_t count
; /* uintptr_t (a 64-bit value) */
3125 /* struct protocol64_t * list[0]; These pointers follow inline */
3128 struct protocol_list32_t
{
3129 uint32_t count
; /* uintptr_t (a 32-bit value) */
3130 /* struct protocol32_t * list[0]; These pointers follow inline */
3133 struct protocol64_t
{
3134 uint64_t isa
; /* id * (64-bit pointer) */
3135 uint64_t name
; /* const char * (64-bit pointer) */
3136 uint64_t protocols
; /* struct protocol_list64_t *
3138 uint64_t instanceMethods
; /* method_list_t * (64-bit pointer) */
3139 uint64_t classMethods
; /* method_list_t * (64-bit pointer) */
3140 uint64_t optionalInstanceMethods
; /* method_list_t * (64-bit pointer) */
3141 uint64_t optionalClassMethods
; /* method_list_t * (64-bit pointer) */
3142 uint64_t instanceProperties
; /* struct objc_property_list *
3146 struct protocol32_t
{
3147 uint32_t isa
; /* id * (32-bit pointer) */
3148 uint32_t name
; /* const char * (32-bit pointer) */
3149 uint32_t protocols
; /* struct protocol_list_t *
3151 uint32_t instanceMethods
; /* method_list_t * (32-bit pointer) */
3152 uint32_t classMethods
; /* method_list_t * (32-bit pointer) */
3153 uint32_t optionalInstanceMethods
; /* method_list_t * (32-bit pointer) */
3154 uint32_t optionalClassMethods
; /* method_list_t * (32-bit pointer) */
3155 uint32_t instanceProperties
; /* struct objc_property_list *
3159 struct ivar_list64_t
{
3162 /* struct ivar64_t first; These structures follow inline */
3165 struct ivar_list32_t
{
3168 /* struct ivar32_t first; These structures follow inline */
3172 uint64_t offset
; /* uintptr_t * (64-bit pointer) */
3173 uint64_t name
; /* const char * (64-bit pointer) */
3174 uint64_t type
; /* const char * (64-bit pointer) */
3180 uint32_t offset
; /* uintptr_t * (32-bit pointer) */
3181 uint32_t name
; /* const char * (32-bit pointer) */
3182 uint32_t type
; /* const char * (32-bit pointer) */
3187 struct objc_property_list64
{
3190 /* struct objc_property64 first; These structures follow inline */
3193 struct objc_property_list32
{
3196 /* struct objc_property32 first; These structures follow inline */
3199 struct objc_property64
{
3200 uint64_t name
; /* const char * (64-bit pointer) */
3201 uint64_t attributes
; /* const char * (64-bit pointer) */
3204 struct objc_property32
{
3205 uint32_t name
; /* const char * (32-bit pointer) */
3206 uint32_t attributes
; /* const char * (32-bit pointer) */
3209 struct category64_t
{
3210 uint64_t name
; /* const char * (64-bit pointer) */
3211 uint64_t cls
; /* struct class_t * (64-bit pointer) */
3212 uint64_t instanceMethods
; /* struct method_list_t * (64-bit pointer) */
3213 uint64_t classMethods
; /* struct method_list_t * (64-bit pointer) */
3214 uint64_t protocols
; /* struct protocol_list_t * (64-bit pointer) */
3215 uint64_t instanceProperties
; /* struct objc_property_list *
3219 struct category32_t
{
3220 uint32_t name
; /* const char * (32-bit pointer) */
3221 uint32_t cls
; /* struct class_t * (32-bit pointer) */
3222 uint32_t instanceMethods
; /* struct method_list_t * (32-bit pointer) */
3223 uint32_t classMethods
; /* struct method_list_t * (32-bit pointer) */
3224 uint32_t protocols
; /* struct protocol_list_t * (32-bit pointer) */
3225 uint32_t instanceProperties
; /* struct objc_property_list *
3229 struct objc_image_info64
{
3233 struct objc_image_info32
{
3237 struct imageInfo_t
{
3241 /* masks for objc_image_info.flags */
3242 #define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
3243 #define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
3244 #define OBJC_IMAGE_IS_SIMULATED (1 << 5)
3245 #define OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES (1 << 6)
3247 struct message_ref64
{
3248 uint64_t imp
; /* IMP (64-bit pointer) */
3249 uint64_t sel
; /* SEL (64-bit pointer) */
3252 struct message_ref32
{
3253 uint32_t imp
; /* IMP (32-bit pointer) */
3254 uint32_t sel
; /* SEL (32-bit pointer) */
3257 // Objective-C 1 (32-bit only) meta data structs.
3259 struct objc_module_t
{
3262 uint32_t name
; /* char * (32-bit pointer) */
3263 uint32_t symtab
; /* struct objc_symtab * (32-bit pointer) */
3266 struct objc_symtab_t
{
3267 uint32_t sel_ref_cnt
;
3268 uint32_t refs
; /* SEL * (32-bit pointer) */
3269 uint16_t cls_def_cnt
;
3270 uint16_t cat_def_cnt
;
3271 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
3274 struct objc_class_t
{
3275 uint32_t isa
; /* struct objc_class * (32-bit pointer) */
3276 uint32_t super_class
; /* struct objc_class * (32-bit pointer) */
3277 uint32_t name
; /* const char * (32-bit pointer) */
3280 int32_t instance_size
;
3281 uint32_t ivars
; /* struct objc_ivar_list * (32-bit pointer) */
3282 uint32_t methodLists
; /* struct objc_method_list ** (32-bit pointer) */
3283 uint32_t cache
; /* struct objc_cache * (32-bit pointer) */
3284 uint32_t protocols
; /* struct objc_protocol_list * (32-bit pointer) */
3287 #define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
3288 // class is not a metaclass
3289 #define CLS_CLASS 0x1
3290 // class is a metaclass
3291 #define CLS_META 0x2
3293 struct objc_category_t
{
3294 uint32_t category_name
; /* char * (32-bit pointer) */
3295 uint32_t class_name
; /* char * (32-bit pointer) */
3296 uint32_t instance_methods
; /* struct objc_method_list * (32-bit pointer) */
3297 uint32_t class_methods
; /* struct objc_method_list * (32-bit pointer) */
3298 uint32_t protocols
; /* struct objc_protocol_list * (32-bit ptr) */
3301 struct objc_ivar_t
{
3302 uint32_t ivar_name
; /* char * (32-bit pointer) */
3303 uint32_t ivar_type
; /* char * (32-bit pointer) */
3304 int32_t ivar_offset
;
3307 struct objc_ivar_list_t
{
3309 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
3312 struct objc_method_list_t
{
3313 uint32_t obsolete
; /* struct objc_method_list * (32-bit pointer) */
3314 int32_t method_count
;
3315 // struct objc_method_t method_list[1]; /* variable length structure */
3318 struct objc_method_t
{
3319 uint32_t method_name
; /* SEL, aka struct objc_selector * (32-bit pointer) */
3320 uint32_t method_types
; /* char * (32-bit pointer) */
3321 uint32_t method_imp
; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
3325 struct objc_protocol_list_t
{
3326 uint32_t next
; /* struct objc_protocol_list * (32-bit pointer) */
3328 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
3329 // (32-bit pointer) */
3332 struct objc_protocol_t
{
3333 uint32_t isa
; /* struct objc_class * (32-bit pointer) */
3334 uint32_t protocol_name
; /* char * (32-bit pointer) */
3335 uint32_t protocol_list
; /* struct objc_protocol_list * (32-bit pointer) */
3336 uint32_t instance_methods
; /* struct objc_method_description_list *
3338 uint32_t class_methods
; /* struct objc_method_description_list *
3342 struct objc_method_description_list_t
{
3344 // struct objc_method_description_t list[1];
3347 struct objc_method_description_t
{
3348 uint32_t name
; /* SEL, aka struct objc_selector * (32-bit pointer) */
3349 uint32_t types
; /* char * (32-bit pointer) */
3352 inline void swapStruct(struct cfstring64_t
&cfs
) {
3353 sys::swapByteOrder(cfs
.isa
);
3354 sys::swapByteOrder(cfs
.flags
);
3355 sys::swapByteOrder(cfs
.characters
);
3356 sys::swapByteOrder(cfs
.length
);
3359 inline void swapStruct(struct class64_t
&c
) {
3360 sys::swapByteOrder(c
.isa
);
3361 sys::swapByteOrder(c
.superclass
);
3362 sys::swapByteOrder(c
.cache
);
3363 sys::swapByteOrder(c
.vtable
);
3364 sys::swapByteOrder(c
.data
);
3367 inline void swapStruct(struct class32_t
&c
) {
3368 sys::swapByteOrder(c
.isa
);
3369 sys::swapByteOrder(c
.superclass
);
3370 sys::swapByteOrder(c
.cache
);
3371 sys::swapByteOrder(c
.vtable
);
3372 sys::swapByteOrder(c
.data
);
3375 inline void swapStruct(struct class_ro64_t
&cro
) {
3376 sys::swapByteOrder(cro
.flags
);
3377 sys::swapByteOrder(cro
.instanceStart
);
3378 sys::swapByteOrder(cro
.instanceSize
);
3379 sys::swapByteOrder(cro
.reserved
);
3380 sys::swapByteOrder(cro
.ivarLayout
);
3381 sys::swapByteOrder(cro
.name
);
3382 sys::swapByteOrder(cro
.baseMethods
);
3383 sys::swapByteOrder(cro
.baseProtocols
);
3384 sys::swapByteOrder(cro
.ivars
);
3385 sys::swapByteOrder(cro
.weakIvarLayout
);
3386 sys::swapByteOrder(cro
.baseProperties
);
3389 inline void swapStruct(struct class_ro32_t
&cro
) {
3390 sys::swapByteOrder(cro
.flags
);
3391 sys::swapByteOrder(cro
.instanceStart
);
3392 sys::swapByteOrder(cro
.instanceSize
);
3393 sys::swapByteOrder(cro
.ivarLayout
);
3394 sys::swapByteOrder(cro
.name
);
3395 sys::swapByteOrder(cro
.baseMethods
);
3396 sys::swapByteOrder(cro
.baseProtocols
);
3397 sys::swapByteOrder(cro
.ivars
);
3398 sys::swapByteOrder(cro
.weakIvarLayout
);
3399 sys::swapByteOrder(cro
.baseProperties
);
3402 inline void swapStruct(struct method_list64_t
&ml
) {
3403 sys::swapByteOrder(ml
.entsize
);
3404 sys::swapByteOrder(ml
.count
);
3407 inline void swapStruct(struct method_list32_t
&ml
) {
3408 sys::swapByteOrder(ml
.entsize
);
3409 sys::swapByteOrder(ml
.count
);
3412 inline void swapStruct(struct method64_t
&m
) {
3413 sys::swapByteOrder(m
.name
);
3414 sys::swapByteOrder(m
.types
);
3415 sys::swapByteOrder(m
.imp
);
3418 inline void swapStruct(struct method32_t
&m
) {
3419 sys::swapByteOrder(m
.name
);
3420 sys::swapByteOrder(m
.types
);
3421 sys::swapByteOrder(m
.imp
);
3424 inline void swapStruct(struct protocol_list64_t
&pl
) {
3425 sys::swapByteOrder(pl
.count
);
3428 inline void swapStruct(struct protocol_list32_t
&pl
) {
3429 sys::swapByteOrder(pl
.count
);
3432 inline void swapStruct(struct protocol64_t
&p
) {
3433 sys::swapByteOrder(p
.isa
);
3434 sys::swapByteOrder(p
.name
);
3435 sys::swapByteOrder(p
.protocols
);
3436 sys::swapByteOrder(p
.instanceMethods
);
3437 sys::swapByteOrder(p
.classMethods
);
3438 sys::swapByteOrder(p
.optionalInstanceMethods
);
3439 sys::swapByteOrder(p
.optionalClassMethods
);
3440 sys::swapByteOrder(p
.instanceProperties
);
3443 inline void swapStruct(struct protocol32_t
&p
) {
3444 sys::swapByteOrder(p
.isa
);
3445 sys::swapByteOrder(p
.name
);
3446 sys::swapByteOrder(p
.protocols
);
3447 sys::swapByteOrder(p
.instanceMethods
);
3448 sys::swapByteOrder(p
.classMethods
);
3449 sys::swapByteOrder(p
.optionalInstanceMethods
);
3450 sys::swapByteOrder(p
.optionalClassMethods
);
3451 sys::swapByteOrder(p
.instanceProperties
);
3454 inline void swapStruct(struct ivar_list64_t
&il
) {
3455 sys::swapByteOrder(il
.entsize
);
3456 sys::swapByteOrder(il
.count
);
3459 inline void swapStruct(struct ivar_list32_t
&il
) {
3460 sys::swapByteOrder(il
.entsize
);
3461 sys::swapByteOrder(il
.count
);
3464 inline void swapStruct(struct ivar64_t
&i
) {
3465 sys::swapByteOrder(i
.offset
);
3466 sys::swapByteOrder(i
.name
);
3467 sys::swapByteOrder(i
.type
);
3468 sys::swapByteOrder(i
.alignment
);
3469 sys::swapByteOrder(i
.size
);
3472 inline void swapStruct(struct ivar32_t
&i
) {
3473 sys::swapByteOrder(i
.offset
);
3474 sys::swapByteOrder(i
.name
);
3475 sys::swapByteOrder(i
.type
);
3476 sys::swapByteOrder(i
.alignment
);
3477 sys::swapByteOrder(i
.size
);
3480 inline void swapStruct(struct objc_property_list64
&pl
) {
3481 sys::swapByteOrder(pl
.entsize
);
3482 sys::swapByteOrder(pl
.count
);
3485 inline void swapStruct(struct objc_property_list32
&pl
) {
3486 sys::swapByteOrder(pl
.entsize
);
3487 sys::swapByteOrder(pl
.count
);
3490 inline void swapStruct(struct objc_property64
&op
) {
3491 sys::swapByteOrder(op
.name
);
3492 sys::swapByteOrder(op
.attributes
);
3495 inline void swapStruct(struct objc_property32
&op
) {
3496 sys::swapByteOrder(op
.name
);
3497 sys::swapByteOrder(op
.attributes
);
3500 inline void swapStruct(struct category64_t
&c
) {
3501 sys::swapByteOrder(c
.name
);
3502 sys::swapByteOrder(c
.cls
);
3503 sys::swapByteOrder(c
.instanceMethods
);
3504 sys::swapByteOrder(c
.classMethods
);
3505 sys::swapByteOrder(c
.protocols
);
3506 sys::swapByteOrder(c
.instanceProperties
);
3509 inline void swapStruct(struct category32_t
&c
) {
3510 sys::swapByteOrder(c
.name
);
3511 sys::swapByteOrder(c
.cls
);
3512 sys::swapByteOrder(c
.instanceMethods
);
3513 sys::swapByteOrder(c
.classMethods
);
3514 sys::swapByteOrder(c
.protocols
);
3515 sys::swapByteOrder(c
.instanceProperties
);
3518 inline void swapStruct(struct objc_image_info64
&o
) {
3519 sys::swapByteOrder(o
.version
);
3520 sys::swapByteOrder(o
.flags
);
3523 inline void swapStruct(struct objc_image_info32
&o
) {
3524 sys::swapByteOrder(o
.version
);
3525 sys::swapByteOrder(o
.flags
);
3528 inline void swapStruct(struct imageInfo_t
&o
) {
3529 sys::swapByteOrder(o
.version
);
3530 sys::swapByteOrder(o
.flags
);
3533 inline void swapStruct(struct message_ref64
&mr
) {
3534 sys::swapByteOrder(mr
.imp
);
3535 sys::swapByteOrder(mr
.sel
);
3538 inline void swapStruct(struct message_ref32
&mr
) {
3539 sys::swapByteOrder(mr
.imp
);
3540 sys::swapByteOrder(mr
.sel
);
3543 inline void swapStruct(struct objc_module_t
&module
) {
3544 sys::swapByteOrder(module
.version
);
3545 sys::swapByteOrder(module
.size
);
3546 sys::swapByteOrder(module
.name
);
3547 sys::swapByteOrder(module
.symtab
);
3550 inline void swapStruct(struct objc_symtab_t
&symtab
) {
3551 sys::swapByteOrder(symtab
.sel_ref_cnt
);
3552 sys::swapByteOrder(symtab
.refs
);
3553 sys::swapByteOrder(symtab
.cls_def_cnt
);
3554 sys::swapByteOrder(symtab
.cat_def_cnt
);
3557 inline void swapStruct(struct objc_class_t
&objc_class
) {
3558 sys::swapByteOrder(objc_class
.isa
);
3559 sys::swapByteOrder(objc_class
.super_class
);
3560 sys::swapByteOrder(objc_class
.name
);
3561 sys::swapByteOrder(objc_class
.version
);
3562 sys::swapByteOrder(objc_class
.info
);
3563 sys::swapByteOrder(objc_class
.instance_size
);
3564 sys::swapByteOrder(objc_class
.ivars
);
3565 sys::swapByteOrder(objc_class
.methodLists
);
3566 sys::swapByteOrder(objc_class
.cache
);
3567 sys::swapByteOrder(objc_class
.protocols
);
3570 inline void swapStruct(struct objc_category_t
&objc_category
) {
3571 sys::swapByteOrder(objc_category
.category_name
);
3572 sys::swapByteOrder(objc_category
.class_name
);
3573 sys::swapByteOrder(objc_category
.instance_methods
);
3574 sys::swapByteOrder(objc_category
.class_methods
);
3575 sys::swapByteOrder(objc_category
.protocols
);
3578 inline void swapStruct(struct objc_ivar_list_t
&objc_ivar_list
) {
3579 sys::swapByteOrder(objc_ivar_list
.ivar_count
);
3582 inline void swapStruct(struct objc_ivar_t
&objc_ivar
) {
3583 sys::swapByteOrder(objc_ivar
.ivar_name
);
3584 sys::swapByteOrder(objc_ivar
.ivar_type
);
3585 sys::swapByteOrder(objc_ivar
.ivar_offset
);
3588 inline void swapStruct(struct objc_method_list_t
&method_list
) {
3589 sys::swapByteOrder(method_list
.obsolete
);
3590 sys::swapByteOrder(method_list
.method_count
);
3593 inline void swapStruct(struct objc_method_t
&method
) {
3594 sys::swapByteOrder(method
.method_name
);
3595 sys::swapByteOrder(method
.method_types
);
3596 sys::swapByteOrder(method
.method_imp
);
3599 inline void swapStruct(struct objc_protocol_list_t
&protocol_list
) {
3600 sys::swapByteOrder(protocol_list
.next
);
3601 sys::swapByteOrder(protocol_list
.count
);
3604 inline void swapStruct(struct objc_protocol_t
&protocol
) {
3605 sys::swapByteOrder(protocol
.isa
);
3606 sys::swapByteOrder(protocol
.protocol_name
);
3607 sys::swapByteOrder(protocol
.protocol_list
);
3608 sys::swapByteOrder(protocol
.instance_methods
);
3609 sys::swapByteOrder(protocol
.class_methods
);
3612 inline void swapStruct(struct objc_method_description_list_t
&mdl
) {
3613 sys::swapByteOrder(mdl
.count
);
3616 inline void swapStruct(struct objc_method_description_t
&md
) {
3617 sys::swapByteOrder(md
.name
);
3618 sys::swapByteOrder(md
.types
);
3621 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue
,
3622 struct DisassembleInfo
*info
);
3624 // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3625 // to an Objective-C class and returns the class name. It is also passed the
3626 // address of the pointer, so when the pointer is zero as it can be in an .o
3627 // file, that is used to look for an external relocation entry with a symbol
3629 static const char *get_objc2_64bit_class_name(uint64_t pointer_value
,
3630 uint64_t ReferenceValue
,
3631 struct DisassembleInfo
*info
) {
3633 uint32_t offset
, left
;
3636 // The pointer_value can be 0 in an object file and have a relocation
3637 // entry for the class symbol at the ReferenceValue (the address of the
3639 if (pointer_value
== 0) {
3640 r
= get_pointer_64(ReferenceValue
, offset
, left
, S
, info
);
3641 if (r
== nullptr || left
< sizeof(uint64_t))
3644 const char *symbol_name
= get_symbol_64(offset
, S
, info
, n_value
);
3645 if (symbol_name
== nullptr)
3647 const char *class_name
= strrchr(symbol_name
, '$');
3648 if (class_name
!= nullptr && class_name
[1] == '_' && class_name
[2] != '\0')
3649 return class_name
+ 2;
3654 // The case were the pointer_value is non-zero and points to a class defined
3655 // in this Mach-O file.
3656 r
= get_pointer_64(pointer_value
, offset
, left
, S
, info
);
3657 if (r
== nullptr || left
< sizeof(struct class64_t
))
3660 memcpy(&c
, r
, sizeof(struct class64_t
));
3661 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
3665 r
= get_pointer_64(c
.data
, offset
, left
, S
, info
);
3666 if (r
== nullptr || left
< sizeof(struct class_ro64_t
))
3668 struct class_ro64_t cro
;
3669 memcpy(&cro
, r
, sizeof(struct class_ro64_t
));
3670 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
3674 const char *name
= get_pointer_64(cro
.name
, offset
, left
, S
, info
);
3678 // get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3679 // pointer to a cfstring and returns its name or nullptr.
3680 static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue
,
3681 struct DisassembleInfo
*info
) {
3682 const char *r
, *name
;
3683 uint32_t offset
, left
;
3685 struct cfstring64_t cfs
;
3686 uint64_t cfs_characters
;
3688 r
= get_pointer_64(ReferenceValue
, offset
, left
, S
, info
);
3689 if (r
== nullptr || left
< sizeof(struct cfstring64_t
))
3691 memcpy(&cfs
, r
, sizeof(struct cfstring64_t
));
3692 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
3694 if (cfs
.characters
== 0) {
3696 const char *symbol_name
= get_symbol_64(
3697 offset
+ offsetof(struct cfstring64_t
, characters
), S
, info
, n_value
);
3698 if (symbol_name
== nullptr)
3700 cfs_characters
= n_value
;
3702 cfs_characters
= cfs
.characters
;
3703 name
= get_pointer_64(cfs_characters
, offset
, left
, S
, info
);
3708 // get_objc2_64bit_selref() is used for disassembly and is passed a the address
3709 // of a pointer to an Objective-C selector reference when the pointer value is
3710 // zero as in a .o file and is likely to have a external relocation entry with
3711 // who's symbol's n_value is the real pointer to the selector name. If that is
3712 // the case the real pointer to the selector name is returned else 0 is
3714 static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue
,
3715 struct DisassembleInfo
*info
) {
3716 uint32_t offset
, left
;
3719 const char *r
= get_pointer_64(ReferenceValue
, offset
, left
, S
, info
);
3720 if (r
== nullptr || left
< sizeof(uint64_t))
3723 const char *symbol_name
= get_symbol_64(offset
, S
, info
, n_value
);
3724 if (symbol_name
== nullptr)
3729 static const SectionRef
get_section(MachOObjectFile
*O
, const char *segname
,
3730 const char *sectname
) {
3731 for (const SectionRef
&Section
: O
->sections()) {
3733 Section
.getName(SectName
);
3734 DataRefImpl Ref
= Section
.getRawDataRefImpl();
3735 StringRef SegName
= O
->getSectionFinalSegmentName(Ref
);
3736 if (SegName
== segname
&& SectName
== sectname
)
3739 return SectionRef();
3743 walk_pointer_list_64(const char *listname
, const SectionRef S
,
3744 MachOObjectFile
*O
, struct DisassembleInfo
*info
,
3745 void (*func
)(uint64_t, struct DisassembleInfo
*info
)) {
3746 if (S
== SectionRef())
3750 S
.getName(SectName
);
3751 DataRefImpl Ref
= S
.getRawDataRefImpl();
3752 StringRef SegName
= O
->getSectionFinalSegmentName(Ref
);
3753 outs() << "Contents of (" << SegName
<< "," << SectName
<< ") section\n";
3756 S
.getContents(BytesStr
);
3757 const char *Contents
= reinterpret_cast<const char *>(BytesStr
.data());
3759 for (uint32_t i
= 0; i
< S
.getSize(); i
+= sizeof(uint64_t)) {
3760 uint32_t left
= S
.getSize() - i
;
3761 uint32_t size
= left
< sizeof(uint64_t) ? left
: sizeof(uint64_t);
3763 memcpy(&p
, Contents
+ i
, size
);
3764 if (i
+ sizeof(uint64_t) > S
.getSize())
3765 outs() << listname
<< " list pointer extends past end of (" << SegName
3766 << "," << SectName
<< ") section\n";
3767 outs() << format("%016" PRIx64
, S
.getAddress() + i
) << " ";
3769 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
3770 sys::swapByteOrder(p
);
3772 uint64_t n_value
= 0;
3773 const char *name
= get_symbol_64(i
, S
, info
, n_value
, p
);
3774 if (name
== nullptr)
3775 name
= get_dyld_bind_info_symbolname(S
.getAddress() + i
, info
);
3778 outs() << format("0x%" PRIx64
, n_value
);
3780 outs() << " + " << format("0x%" PRIx64
, p
);
3782 outs() << format("0x%" PRIx64
, p
);
3783 if (name
!= nullptr)
3784 outs() << " " << name
;
3794 walk_pointer_list_32(const char *listname
, const SectionRef S
,
3795 MachOObjectFile
*O
, struct DisassembleInfo
*info
,
3796 void (*func
)(uint32_t, struct DisassembleInfo
*info
)) {
3797 if (S
== SectionRef())
3801 S
.getName(SectName
);
3802 DataRefImpl Ref
= S
.getRawDataRefImpl();
3803 StringRef SegName
= O
->getSectionFinalSegmentName(Ref
);
3804 outs() << "Contents of (" << SegName
<< "," << SectName
<< ") section\n";
3807 S
.getContents(BytesStr
);
3808 const char *Contents
= reinterpret_cast<const char *>(BytesStr
.data());
3810 for (uint32_t i
= 0; i
< S
.getSize(); i
+= sizeof(uint32_t)) {
3811 uint32_t left
= S
.getSize() - i
;
3812 uint32_t size
= left
< sizeof(uint32_t) ? left
: sizeof(uint32_t);
3814 memcpy(&p
, Contents
+ i
, size
);
3815 if (i
+ sizeof(uint32_t) > S
.getSize())
3816 outs() << listname
<< " list pointer extends past end of (" << SegName
3817 << "," << SectName
<< ") section\n";
3818 uint32_t Address
= S
.getAddress() + i
;
3819 outs() << format("%08" PRIx32
, Address
) << " ";
3821 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
3822 sys::swapByteOrder(p
);
3823 outs() << format("0x%" PRIx32
, p
);
3825 const char *name
= get_symbol_32(i
, S
, info
, p
);
3826 if (name
!= nullptr)
3827 outs() << " " << name
;
3835 static void print_layout_map(const char *layout_map
, uint32_t left
) {
3836 if (layout_map
== nullptr)
3838 outs() << " layout map: ";
3840 outs() << format("0x%02" PRIx32
, (*layout_map
) & 0xff) << " ";
3843 } while (*layout_map
!= '\0' && left
!= 0);
3847 static void print_layout_map64(uint64_t p
, struct DisassembleInfo
*info
) {
3848 uint32_t offset
, left
;
3850 const char *layout_map
;
3854 layout_map
= get_pointer_64(p
, offset
, left
, S
, info
);
3855 print_layout_map(layout_map
, left
);
3858 static void print_layout_map32(uint32_t p
, struct DisassembleInfo
*info
) {
3859 uint32_t offset
, left
;
3861 const char *layout_map
;
3865 layout_map
= get_pointer_32(p
, offset
, left
, S
, info
);
3866 print_layout_map(layout_map
, left
);
3869 static void print_method_list64_t(uint64_t p
, struct DisassembleInfo
*info
,
3870 const char *indent
) {
3871 struct method_list64_t ml
;
3872 struct method64_t m
;
3874 uint32_t offset
, xoffset
, left
, i
;
3876 const char *name
, *sym_name
;
3879 r
= get_pointer_64(p
, offset
, left
, S
, info
);
3882 memset(&ml
, '\0', sizeof(struct method_list64_t
));
3883 if (left
< sizeof(struct method_list64_t
)) {
3884 memcpy(&ml
, r
, left
);
3885 outs() << " (method_list_t entends past the end of the section)\n";
3887 memcpy(&ml
, r
, sizeof(struct method_list64_t
));
3888 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
3890 outs() << indent
<< "\t\t entsize " << ml
.entsize
<< "\n";
3891 outs() << indent
<< "\t\t count " << ml
.count
<< "\n";
3893 p
+= sizeof(struct method_list64_t
);
3894 offset
+= sizeof(struct method_list64_t
);
3895 for (i
= 0; i
< ml
.count
; i
++) {
3896 r
= get_pointer_64(p
, offset
, left
, S
, info
);
3899 memset(&m
, '\0', sizeof(struct method64_t
));
3900 if (left
< sizeof(struct method64_t
)) {
3901 memcpy(&m
, r
, left
);
3902 outs() << indent
<< " (method_t extends past the end of the section)\n";
3904 memcpy(&m
, r
, sizeof(struct method64_t
));
3905 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
3908 outs() << indent
<< "\t\t name ";
3909 sym_name
= get_symbol_64(offset
+ offsetof(struct method64_t
, name
), S
,
3910 info
, n_value
, m
.name
);
3912 if (info
->verbose
&& sym_name
!= nullptr)
3915 outs() << format("0x%" PRIx64
, n_value
);
3917 outs() << " + " << format("0x%" PRIx64
, m
.name
);
3919 outs() << format("0x%" PRIx64
, m
.name
);
3920 name
= get_pointer_64(m
.name
+ n_value
, xoffset
, left
, xS
, info
);
3921 if (name
!= nullptr)
3922 outs() << format(" %.*s", left
, name
);
3925 outs() << indent
<< "\t\t types ";
3926 sym_name
= get_symbol_64(offset
+ offsetof(struct method64_t
, types
), S
,
3927 info
, n_value
, m
.types
);
3929 if (info
->verbose
&& sym_name
!= nullptr)
3932 outs() << format("0x%" PRIx64
, n_value
);
3934 outs() << " + " << format("0x%" PRIx64
, m
.types
);
3936 outs() << format("0x%" PRIx64
, m
.types
);
3937 name
= get_pointer_64(m
.types
+ n_value
, xoffset
, left
, xS
, info
);
3938 if (name
!= nullptr)
3939 outs() << format(" %.*s", left
, name
);
3942 outs() << indent
<< "\t\t imp ";
3943 name
= get_symbol_64(offset
+ offsetof(struct method64_t
, imp
), S
, info
,
3945 if (info
->verbose
&& name
== nullptr) {
3947 outs() << format("0x%" PRIx64
, n_value
) << " ";
3949 outs() << "+ " << format("0x%" PRIx64
, m
.imp
) << " ";
3951 outs() << format("0x%" PRIx64
, m
.imp
) << " ";
3953 if (name
!= nullptr)
3957 p
+= sizeof(struct method64_t
);
3958 offset
+= sizeof(struct method64_t
);
3962 static void print_method_list32_t(uint64_t p
, struct DisassembleInfo
*info
,
3963 const char *indent
) {
3964 struct method_list32_t ml
;
3965 struct method32_t m
;
3966 const char *r
, *name
;
3967 uint32_t offset
, xoffset
, left
, i
;
3970 r
= get_pointer_32(p
, offset
, left
, S
, info
);
3973 memset(&ml
, '\0', sizeof(struct method_list32_t
));
3974 if (left
< sizeof(struct method_list32_t
)) {
3975 memcpy(&ml
, r
, left
);
3976 outs() << " (method_list_t entends past the end of the section)\n";
3978 memcpy(&ml
, r
, sizeof(struct method_list32_t
));
3979 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
3981 outs() << indent
<< "\t\t entsize " << ml
.entsize
<< "\n";
3982 outs() << indent
<< "\t\t count " << ml
.count
<< "\n";
3984 p
+= sizeof(struct method_list32_t
);
3985 offset
+= sizeof(struct method_list32_t
);
3986 for (i
= 0; i
< ml
.count
; i
++) {
3987 r
= get_pointer_32(p
, offset
, left
, S
, info
);
3990 memset(&m
, '\0', sizeof(struct method32_t
));
3991 if (left
< sizeof(struct method32_t
)) {
3992 memcpy(&ml
, r
, left
);
3993 outs() << indent
<< " (method_t entends past the end of the section)\n";
3995 memcpy(&m
, r
, sizeof(struct method32_t
));
3996 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
3999 outs() << indent
<< "\t\t name " << format("0x%" PRIx32
, m
.name
);
4000 name
= get_pointer_32(m
.name
, xoffset
, left
, xS
, info
);
4001 if (name
!= nullptr)
4002 outs() << format(" %.*s", left
, name
);
4005 outs() << indent
<< "\t\t types " << format("0x%" PRIx32
, m
.types
);
4006 name
= get_pointer_32(m
.types
, xoffset
, left
, xS
, info
);
4007 if (name
!= nullptr)
4008 outs() << format(" %.*s", left
, name
);
4011 outs() << indent
<< "\t\t imp " << format("0x%" PRIx32
, m
.imp
);
4012 name
= get_symbol_32(offset
+ offsetof(struct method32_t
, imp
), S
, info
,
4014 if (name
!= nullptr)
4015 outs() << " " << name
;
4018 p
+= sizeof(struct method32_t
);
4019 offset
+= sizeof(struct method32_t
);
4023 static bool print_method_list(uint32_t p
, struct DisassembleInfo
*info
) {
4024 uint32_t offset
, left
, xleft
;
4026 struct objc_method_list_t method_list
;
4027 struct objc_method_t method
;
4028 const char *r
, *methods
, *name
, *SymbolName
;
4031 r
= get_pointer_32(p
, offset
, left
, S
, info
, true);
4036 if (left
> sizeof(struct objc_method_list_t
)) {
4037 memcpy(&method_list
, r
, sizeof(struct objc_method_list_t
));
4039 outs() << "\t\t objc_method_list extends past end of the section\n";
4040 memset(&method_list
, '\0', sizeof(struct objc_method_list_t
));
4041 memcpy(&method_list
, r
, left
);
4043 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4044 swapStruct(method_list
);
4046 outs() << "\t\t obsolete "
4047 << format("0x%08" PRIx32
, method_list
.obsolete
) << "\n";
4048 outs() << "\t\t method_count " << method_list
.method_count
<< "\n";
4050 methods
= r
+ sizeof(struct objc_method_list_t
);
4051 for (i
= 0; i
< method_list
.method_count
; i
++) {
4052 if ((i
+ 1) * sizeof(struct objc_method_t
) > left
) {
4053 outs() << "\t\t remaining method's extend past the of the section\n";
4056 memcpy(&method
, methods
+ i
* sizeof(struct objc_method_t
),
4057 sizeof(struct objc_method_t
));
4058 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4061 outs() << "\t\t method_name "
4062 << format("0x%08" PRIx32
, method
.method_name
);
4063 if (info
->verbose
) {
4064 name
= get_pointer_32(method
.method_name
, offset
, xleft
, S
, info
, true);
4065 if (name
!= nullptr)
4066 outs() << format(" %.*s", xleft
, name
);
4068 outs() << " (not in an __OBJC section)";
4072 outs() << "\t\t method_types "
4073 << format("0x%08" PRIx32
, method
.method_types
);
4074 if (info
->verbose
) {
4075 name
= get_pointer_32(method
.method_types
, offset
, xleft
, S
, info
, true);
4076 if (name
!= nullptr)
4077 outs() << format(" %.*s", xleft
, name
);
4079 outs() << " (not in an __OBJC section)";
4083 outs() << "\t\t method_imp "
4084 << format("0x%08" PRIx32
, method
.method_imp
) << " ";
4085 if (info
->verbose
) {
4086 SymbolName
= GuessSymbolName(method
.method_imp
, info
->AddrMap
);
4087 if (SymbolName
!= nullptr)
4088 outs() << SymbolName
;
4095 static void print_protocol_list64_t(uint64_t p
, struct DisassembleInfo
*info
) {
4096 struct protocol_list64_t pl
;
4097 uint64_t q
, n_value
;
4098 struct protocol64_t pc
;
4100 uint32_t offset
, xoffset
, left
, i
;
4102 const char *name
, *sym_name
;
4104 r
= get_pointer_64(p
, offset
, left
, S
, info
);
4107 memset(&pl
, '\0', sizeof(struct protocol_list64_t
));
4108 if (left
< sizeof(struct protocol_list64_t
)) {
4109 memcpy(&pl
, r
, left
);
4110 outs() << " (protocol_list_t entends past the end of the section)\n";
4112 memcpy(&pl
, r
, sizeof(struct protocol_list64_t
));
4113 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4115 outs() << " count " << pl
.count
<< "\n";
4117 p
+= sizeof(struct protocol_list64_t
);
4118 offset
+= sizeof(struct protocol_list64_t
);
4119 for (i
= 0; i
< pl
.count
; i
++) {
4120 r
= get_pointer_64(p
, offset
, left
, S
, info
);
4124 if (left
< sizeof(uint64_t)) {
4125 memcpy(&q
, r
, left
);
4126 outs() << " (protocol_t * entends past the end of the section)\n";
4128 memcpy(&q
, r
, sizeof(uint64_t));
4129 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4130 sys::swapByteOrder(q
);
4132 outs() << "\t\t list[" << i
<< "] ";
4133 sym_name
= get_symbol_64(offset
, S
, info
, n_value
, q
);
4135 if (info
->verbose
&& sym_name
!= nullptr)
4138 outs() << format("0x%" PRIx64
, n_value
);
4140 outs() << " + " << format("0x%" PRIx64
, q
);
4142 outs() << format("0x%" PRIx64
, q
);
4143 outs() << " (struct protocol_t *)\n";
4145 r
= get_pointer_64(q
+ n_value
, offset
, left
, S
, info
);
4148 memset(&pc
, '\0', sizeof(struct protocol64_t
));
4149 if (left
< sizeof(struct protocol64_t
)) {
4150 memcpy(&pc
, r
, left
);
4151 outs() << " (protocol_t entends past the end of the section)\n";
4153 memcpy(&pc
, r
, sizeof(struct protocol64_t
));
4154 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4157 outs() << "\t\t\t isa " << format("0x%" PRIx64
, pc
.isa
) << "\n";
4159 outs() << "\t\t\t name ";
4160 sym_name
= get_symbol_64(offset
+ offsetof(struct protocol64_t
, name
), S
,
4161 info
, n_value
, pc
.name
);
4163 if (info
->verbose
&& sym_name
!= nullptr)
4166 outs() << format("0x%" PRIx64
, n_value
);
4168 outs() << " + " << format("0x%" PRIx64
, pc
.name
);
4170 outs() << format("0x%" PRIx64
, pc
.name
);
4171 name
= get_pointer_64(pc
.name
+ n_value
, xoffset
, left
, xS
, info
);
4172 if (name
!= nullptr)
4173 outs() << format(" %.*s", left
, name
);
4176 outs() << "\t\t\tprotocols " << format("0x%" PRIx64
, pc
.protocols
) << "\n";
4178 outs() << "\t\t instanceMethods ";
4180 get_symbol_64(offset
+ offsetof(struct protocol64_t
, instanceMethods
),
4181 S
, info
, n_value
, pc
.instanceMethods
);
4183 if (info
->verbose
&& sym_name
!= nullptr)
4186 outs() << format("0x%" PRIx64
, n_value
);
4187 if (pc
.instanceMethods
!= 0)
4188 outs() << " + " << format("0x%" PRIx64
, pc
.instanceMethods
);
4190 outs() << format("0x%" PRIx64
, pc
.instanceMethods
);
4191 outs() << " (struct method_list_t *)\n";
4192 if (pc
.instanceMethods
+ n_value
!= 0)
4193 print_method_list64_t(pc
.instanceMethods
+ n_value
, info
, "\t");
4195 outs() << "\t\t classMethods ";
4197 get_symbol_64(offset
+ offsetof(struct protocol64_t
, classMethods
), S
,
4198 info
, n_value
, pc
.classMethods
);
4200 if (info
->verbose
&& sym_name
!= nullptr)
4203 outs() << format("0x%" PRIx64
, n_value
);
4204 if (pc
.classMethods
!= 0)
4205 outs() << " + " << format("0x%" PRIx64
, pc
.classMethods
);
4207 outs() << format("0x%" PRIx64
, pc
.classMethods
);
4208 outs() << " (struct method_list_t *)\n";
4209 if (pc
.classMethods
+ n_value
!= 0)
4210 print_method_list64_t(pc
.classMethods
+ n_value
, info
, "\t");
4212 outs() << "\t optionalInstanceMethods "
4213 << format("0x%" PRIx64
, pc
.optionalInstanceMethods
) << "\n";
4214 outs() << "\t optionalClassMethods "
4215 << format("0x%" PRIx64
, pc
.optionalClassMethods
) << "\n";
4216 outs() << "\t instanceProperties "
4217 << format("0x%" PRIx64
, pc
.instanceProperties
) << "\n";
4219 p
+= sizeof(uint64_t);
4220 offset
+= sizeof(uint64_t);
4224 static void print_protocol_list32_t(uint32_t p
, struct DisassembleInfo
*info
) {
4225 struct protocol_list32_t pl
;
4227 struct protocol32_t pc
;
4229 uint32_t offset
, xoffset
, left
, i
;
4233 r
= get_pointer_32(p
, offset
, left
, S
, info
);
4236 memset(&pl
, '\0', sizeof(struct protocol_list32_t
));
4237 if (left
< sizeof(struct protocol_list32_t
)) {
4238 memcpy(&pl
, r
, left
);
4239 outs() << " (protocol_list_t entends past the end of the section)\n";
4241 memcpy(&pl
, r
, sizeof(struct protocol_list32_t
));
4242 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4244 outs() << " count " << pl
.count
<< "\n";
4246 p
+= sizeof(struct protocol_list32_t
);
4247 offset
+= sizeof(struct protocol_list32_t
);
4248 for (i
= 0; i
< pl
.count
; i
++) {
4249 r
= get_pointer_32(p
, offset
, left
, S
, info
);
4253 if (left
< sizeof(uint32_t)) {
4254 memcpy(&q
, r
, left
);
4255 outs() << " (protocol_t * entends past the end of the section)\n";
4257 memcpy(&q
, r
, sizeof(uint32_t));
4258 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4259 sys::swapByteOrder(q
);
4260 outs() << "\t\t list[" << i
<< "] " << format("0x%" PRIx32
, q
)
4261 << " (struct protocol_t *)\n";
4262 r
= get_pointer_32(q
, offset
, left
, S
, info
);
4265 memset(&pc
, '\0', sizeof(struct protocol32_t
));
4266 if (left
< sizeof(struct protocol32_t
)) {
4267 memcpy(&pc
, r
, left
);
4268 outs() << " (protocol_t entends past the end of the section)\n";
4270 memcpy(&pc
, r
, sizeof(struct protocol32_t
));
4271 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4273 outs() << "\t\t\t isa " << format("0x%" PRIx32
, pc
.isa
) << "\n";
4274 outs() << "\t\t\t name " << format("0x%" PRIx32
, pc
.name
);
4275 name
= get_pointer_32(pc
.name
, xoffset
, left
, xS
, info
);
4276 if (name
!= nullptr)
4277 outs() << format(" %.*s", left
, name
);
4279 outs() << "\t\t\tprotocols " << format("0x%" PRIx32
, pc
.protocols
) << "\n";
4280 outs() << "\t\t instanceMethods "
4281 << format("0x%" PRIx32
, pc
.instanceMethods
)
4282 << " (struct method_list_t *)\n";
4283 if (pc
.instanceMethods
!= 0)
4284 print_method_list32_t(pc
.instanceMethods
, info
, "\t");
4285 outs() << "\t\t classMethods " << format("0x%" PRIx32
, pc
.classMethods
)
4286 << " (struct method_list_t *)\n";
4287 if (pc
.classMethods
!= 0)
4288 print_method_list32_t(pc
.classMethods
, info
, "\t");
4289 outs() << "\t optionalInstanceMethods "
4290 << format("0x%" PRIx32
, pc
.optionalInstanceMethods
) << "\n";
4291 outs() << "\t optionalClassMethods "
4292 << format("0x%" PRIx32
, pc
.optionalClassMethods
) << "\n";
4293 outs() << "\t instanceProperties "
4294 << format("0x%" PRIx32
, pc
.instanceProperties
) << "\n";
4295 p
+= sizeof(uint32_t);
4296 offset
+= sizeof(uint32_t);
4300 static void print_indent(uint32_t indent
) {
4301 for (uint32_t i
= 0; i
< indent
;) {
4302 if (indent
- i
>= 8) {
4306 for (uint32_t j
= i
; j
< indent
; j
++)
4313 static bool print_method_description_list(uint32_t p
, uint32_t indent
,
4314 struct DisassembleInfo
*info
) {
4315 uint32_t offset
, left
, xleft
;
4317 struct objc_method_description_list_t mdl
;
4318 struct objc_method_description_t md
;
4319 const char *r
, *list
, *name
;
4322 r
= get_pointer_32(p
, offset
, left
, S
, info
, true);
4327 if (left
> sizeof(struct objc_method_description_list_t
)) {
4328 memcpy(&mdl
, r
, sizeof(struct objc_method_description_list_t
));
4330 print_indent(indent
);
4331 outs() << " objc_method_description_list extends past end of the section\n";
4332 memset(&mdl
, '\0', sizeof(struct objc_method_description_list_t
));
4333 memcpy(&mdl
, r
, left
);
4335 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4338 print_indent(indent
);
4339 outs() << " count " << mdl
.count
<< "\n";
4341 list
= r
+ sizeof(struct objc_method_description_list_t
);
4342 for (i
= 0; i
< mdl
.count
; i
++) {
4343 if ((i
+ 1) * sizeof(struct objc_method_description_t
) > left
) {
4344 print_indent(indent
);
4345 outs() << " remaining list entries extend past the of the section\n";
4348 print_indent(indent
);
4349 outs() << " list[" << i
<< "]\n";
4350 memcpy(&md
, list
+ i
* sizeof(struct objc_method_description_t
),
4351 sizeof(struct objc_method_description_t
));
4352 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4355 print_indent(indent
);
4356 outs() << " name " << format("0x%08" PRIx32
, md
.name
);
4357 if (info
->verbose
) {
4358 name
= get_pointer_32(md
.name
, offset
, xleft
, S
, info
, true);
4359 if (name
!= nullptr)
4360 outs() << format(" %.*s", xleft
, name
);
4362 outs() << " (not in an __OBJC section)";
4366 print_indent(indent
);
4367 outs() << " types " << format("0x%08" PRIx32
, md
.types
);
4368 if (info
->verbose
) {
4369 name
= get_pointer_32(md
.types
, offset
, xleft
, S
, info
, true);
4370 if (name
!= nullptr)
4371 outs() << format(" %.*s", xleft
, name
);
4373 outs() << " (not in an __OBJC section)";
4380 static bool print_protocol_list(uint32_t p
, uint32_t indent
,
4381 struct DisassembleInfo
*info
);
4383 static bool print_protocol(uint32_t p
, uint32_t indent
,
4384 struct DisassembleInfo
*info
) {
4385 uint32_t offset
, left
;
4387 struct objc_protocol_t protocol
;
4388 const char *r
, *name
;
4390 r
= get_pointer_32(p
, offset
, left
, S
, info
, true);
4395 if (left
>= sizeof(struct objc_protocol_t
)) {
4396 memcpy(&protocol
, r
, sizeof(struct objc_protocol_t
));
4398 print_indent(indent
);
4399 outs() << " Protocol extends past end of the section\n";
4400 memset(&protocol
, '\0', sizeof(struct objc_protocol_t
));
4401 memcpy(&protocol
, r
, left
);
4403 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4404 swapStruct(protocol
);
4406 print_indent(indent
);
4407 outs() << " isa " << format("0x%08" PRIx32
, protocol
.isa
)
4410 print_indent(indent
);
4411 outs() << " protocol_name "
4412 << format("0x%08" PRIx32
, protocol
.protocol_name
);
4413 if (info
->verbose
) {
4414 name
= get_pointer_32(protocol
.protocol_name
, offset
, left
, S
, info
, true);
4415 if (name
!= nullptr)
4416 outs() << format(" %.*s", left
, name
);
4418 outs() << " (not in an __OBJC section)";
4422 print_indent(indent
);
4423 outs() << " protocol_list "
4424 << format("0x%08" PRIx32
, protocol
.protocol_list
);
4425 if (print_protocol_list(protocol
.protocol_list
, indent
+ 4, info
))
4426 outs() << " (not in an __OBJC section)\n";
4428 print_indent(indent
);
4429 outs() << " instance_methods "
4430 << format("0x%08" PRIx32
, protocol
.instance_methods
);
4431 if (print_method_description_list(protocol
.instance_methods
, indent
, info
))
4432 outs() << " (not in an __OBJC section)\n";
4434 print_indent(indent
);
4435 outs() << " class_methods "
4436 << format("0x%08" PRIx32
, protocol
.class_methods
);
4437 if (print_method_description_list(protocol
.class_methods
, indent
, info
))
4438 outs() << " (not in an __OBJC section)\n";
4443 static bool print_protocol_list(uint32_t p
, uint32_t indent
,
4444 struct DisassembleInfo
*info
) {
4445 uint32_t offset
, left
, l
;
4447 struct objc_protocol_list_t protocol_list
;
4448 const char *r
, *list
;
4451 r
= get_pointer_32(p
, offset
, left
, S
, info
, true);
4456 if (left
> sizeof(struct objc_protocol_list_t
)) {
4457 memcpy(&protocol_list
, r
, sizeof(struct objc_protocol_list_t
));
4459 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
4460 memset(&protocol_list
, '\0', sizeof(struct objc_protocol_list_t
));
4461 memcpy(&protocol_list
, r
, left
);
4463 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4464 swapStruct(protocol_list
);
4466 print_indent(indent
);
4467 outs() << " next " << format("0x%08" PRIx32
, protocol_list
.next
)
4469 print_indent(indent
);
4470 outs() << " count " << protocol_list
.count
<< "\n";
4472 list
= r
+ sizeof(struct objc_protocol_list_t
);
4473 for (i
= 0; i
< protocol_list
.count
; i
++) {
4474 if ((i
+ 1) * sizeof(uint32_t) > left
) {
4475 outs() << "\t\t remaining list entries extend past the of the section\n";
4478 memcpy(&l
, list
+ i
* sizeof(uint32_t), sizeof(uint32_t));
4479 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4480 sys::swapByteOrder(l
);
4482 print_indent(indent
);
4483 outs() << " list[" << i
<< "] " << format("0x%08" PRIx32
, l
);
4484 if (print_protocol(l
, indent
, info
))
4485 outs() << "(not in an __OBJC section)\n";
4490 static void print_ivar_list64_t(uint64_t p
, struct DisassembleInfo
*info
) {
4491 struct ivar_list64_t il
;
4494 uint32_t offset
, xoffset
, left
, j
;
4496 const char *name
, *sym_name
, *ivar_offset_p
;
4497 uint64_t ivar_offset
, n_value
;
4499 r
= get_pointer_64(p
, offset
, left
, S
, info
);
4502 memset(&il
, '\0', sizeof(struct ivar_list64_t
));
4503 if (left
< sizeof(struct ivar_list64_t
)) {
4504 memcpy(&il
, r
, left
);
4505 outs() << " (ivar_list_t entends past the end of the section)\n";
4507 memcpy(&il
, r
, sizeof(struct ivar_list64_t
));
4508 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4510 outs() << " entsize " << il
.entsize
<< "\n";
4511 outs() << " count " << il
.count
<< "\n";
4513 p
+= sizeof(struct ivar_list64_t
);
4514 offset
+= sizeof(struct ivar_list64_t
);
4515 for (j
= 0; j
< il
.count
; j
++) {
4516 r
= get_pointer_64(p
, offset
, left
, S
, info
);
4519 memset(&i
, '\0', sizeof(struct ivar64_t
));
4520 if (left
< sizeof(struct ivar64_t
)) {
4521 memcpy(&i
, r
, left
);
4522 outs() << " (ivar_t entends past the end of the section)\n";
4524 memcpy(&i
, r
, sizeof(struct ivar64_t
));
4525 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4528 outs() << "\t\t\t offset ";
4529 sym_name
= get_symbol_64(offset
+ offsetof(struct ivar64_t
, offset
), S
,
4530 info
, n_value
, i
.offset
);
4532 if (info
->verbose
&& sym_name
!= nullptr)
4535 outs() << format("0x%" PRIx64
, n_value
);
4537 outs() << " + " << format("0x%" PRIx64
, i
.offset
);
4539 outs() << format("0x%" PRIx64
, i
.offset
);
4540 ivar_offset_p
= get_pointer_64(i
.offset
+ n_value
, xoffset
, left
, xS
, info
);
4541 if (ivar_offset_p
!= nullptr && left
>= sizeof(*ivar_offset_p
)) {
4542 memcpy(&ivar_offset
, ivar_offset_p
, sizeof(ivar_offset
));
4543 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4544 sys::swapByteOrder(ivar_offset
);
4545 outs() << " " << ivar_offset
<< "\n";
4549 outs() << "\t\t\t name ";
4550 sym_name
= get_symbol_64(offset
+ offsetof(struct ivar64_t
, name
), S
, info
,
4553 if (info
->verbose
&& sym_name
!= nullptr)
4556 outs() << format("0x%" PRIx64
, n_value
);
4558 outs() << " + " << format("0x%" PRIx64
, i
.name
);
4560 outs() << format("0x%" PRIx64
, i
.name
);
4561 name
= get_pointer_64(i
.name
+ n_value
, xoffset
, left
, xS
, info
);
4562 if (name
!= nullptr)
4563 outs() << format(" %.*s", left
, name
);
4566 outs() << "\t\t\t type ";
4567 sym_name
= get_symbol_64(offset
+ offsetof(struct ivar64_t
, type
), S
, info
,
4569 name
= get_pointer_64(i
.type
+ n_value
, xoffset
, left
, xS
, info
);
4571 if (info
->verbose
&& sym_name
!= nullptr)
4574 outs() << format("0x%" PRIx64
, n_value
);
4576 outs() << " + " << format("0x%" PRIx64
, i
.type
);
4578 outs() << format("0x%" PRIx64
, i
.type
);
4579 if (name
!= nullptr)
4580 outs() << format(" %.*s", left
, name
);
4583 outs() << "\t\t\talignment " << i
.alignment
<< "\n";
4584 outs() << "\t\t\t size " << i
.size
<< "\n";
4586 p
+= sizeof(struct ivar64_t
);
4587 offset
+= sizeof(struct ivar64_t
);
4591 static void print_ivar_list32_t(uint32_t p
, struct DisassembleInfo
*info
) {
4592 struct ivar_list32_t il
;
4595 uint32_t offset
, xoffset
, left
, j
;
4597 const char *name
, *ivar_offset_p
;
4598 uint32_t ivar_offset
;
4600 r
= get_pointer_32(p
, offset
, left
, S
, info
);
4603 memset(&il
, '\0', sizeof(struct ivar_list32_t
));
4604 if (left
< sizeof(struct ivar_list32_t
)) {
4605 memcpy(&il
, r
, left
);
4606 outs() << " (ivar_list_t entends past the end of the section)\n";
4608 memcpy(&il
, r
, sizeof(struct ivar_list32_t
));
4609 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4611 outs() << " entsize " << il
.entsize
<< "\n";
4612 outs() << " count " << il
.count
<< "\n";
4614 p
+= sizeof(struct ivar_list32_t
);
4615 offset
+= sizeof(struct ivar_list32_t
);
4616 for (j
= 0; j
< il
.count
; j
++) {
4617 r
= get_pointer_32(p
, offset
, left
, S
, info
);
4620 memset(&i
, '\0', sizeof(struct ivar32_t
));
4621 if (left
< sizeof(struct ivar32_t
)) {
4622 memcpy(&i
, r
, left
);
4623 outs() << " (ivar_t entends past the end of the section)\n";
4625 memcpy(&i
, r
, sizeof(struct ivar32_t
));
4626 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4629 outs() << "\t\t\t offset " << format("0x%" PRIx32
, i
.offset
);
4630 ivar_offset_p
= get_pointer_32(i
.offset
, xoffset
, left
, xS
, info
);
4631 if (ivar_offset_p
!= nullptr && left
>= sizeof(*ivar_offset_p
)) {
4632 memcpy(&ivar_offset
, ivar_offset_p
, sizeof(ivar_offset
));
4633 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4634 sys::swapByteOrder(ivar_offset
);
4635 outs() << " " << ivar_offset
<< "\n";
4639 outs() << "\t\t\t name " << format("0x%" PRIx32
, i
.name
);
4640 name
= get_pointer_32(i
.name
, xoffset
, left
, xS
, info
);
4641 if (name
!= nullptr)
4642 outs() << format(" %.*s", left
, name
);
4645 outs() << "\t\t\t type " << format("0x%" PRIx32
, i
.type
);
4646 name
= get_pointer_32(i
.type
, xoffset
, left
, xS
, info
);
4647 if (name
!= nullptr)
4648 outs() << format(" %.*s", left
, name
);
4651 outs() << "\t\t\talignment " << i
.alignment
<< "\n";
4652 outs() << "\t\t\t size " << i
.size
<< "\n";
4654 p
+= sizeof(struct ivar32_t
);
4655 offset
+= sizeof(struct ivar32_t
);
4659 static void print_objc_property_list64(uint64_t p
,
4660 struct DisassembleInfo
*info
) {
4661 struct objc_property_list64 opl
;
4662 struct objc_property64 op
;
4664 uint32_t offset
, xoffset
, left
, j
;
4666 const char *name
, *sym_name
;
4669 r
= get_pointer_64(p
, offset
, left
, S
, info
);
4672 memset(&opl
, '\0', sizeof(struct objc_property_list64
));
4673 if (left
< sizeof(struct objc_property_list64
)) {
4674 memcpy(&opl
, r
, left
);
4675 outs() << " (objc_property_list entends past the end of the section)\n";
4677 memcpy(&opl
, r
, sizeof(struct objc_property_list64
));
4678 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4680 outs() << " entsize " << opl
.entsize
<< "\n";
4681 outs() << " count " << opl
.count
<< "\n";
4683 p
+= sizeof(struct objc_property_list64
);
4684 offset
+= sizeof(struct objc_property_list64
);
4685 for (j
= 0; j
< opl
.count
; j
++) {
4686 r
= get_pointer_64(p
, offset
, left
, S
, info
);
4689 memset(&op
, '\0', sizeof(struct objc_property64
));
4690 if (left
< sizeof(struct objc_property64
)) {
4691 memcpy(&op
, r
, left
);
4692 outs() << " (objc_property entends past the end of the section)\n";
4694 memcpy(&op
, r
, sizeof(struct objc_property64
));
4695 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4698 outs() << "\t\t\t name ";
4699 sym_name
= get_symbol_64(offset
+ offsetof(struct objc_property64
, name
), S
,
4700 info
, n_value
, op
.name
);
4702 if (info
->verbose
&& sym_name
!= nullptr)
4705 outs() << format("0x%" PRIx64
, n_value
);
4707 outs() << " + " << format("0x%" PRIx64
, op
.name
);
4709 outs() << format("0x%" PRIx64
, op
.name
);
4710 name
= get_pointer_64(op
.name
+ n_value
, xoffset
, left
, xS
, info
);
4711 if (name
!= nullptr)
4712 outs() << format(" %.*s", left
, name
);
4715 outs() << "\t\t\tattributes ";
4717 get_symbol_64(offset
+ offsetof(struct objc_property64
, attributes
), S
,
4718 info
, n_value
, op
.attributes
);
4720 if (info
->verbose
&& sym_name
!= nullptr)
4723 outs() << format("0x%" PRIx64
, n_value
);
4724 if (op
.attributes
!= 0)
4725 outs() << " + " << format("0x%" PRIx64
, op
.attributes
);
4727 outs() << format("0x%" PRIx64
, op
.attributes
);
4728 name
= get_pointer_64(op
.attributes
+ n_value
, xoffset
, left
, xS
, info
);
4729 if (name
!= nullptr)
4730 outs() << format(" %.*s", left
, name
);
4733 p
+= sizeof(struct objc_property64
);
4734 offset
+= sizeof(struct objc_property64
);
4738 static void print_objc_property_list32(uint32_t p
,
4739 struct DisassembleInfo
*info
) {
4740 struct objc_property_list32 opl
;
4741 struct objc_property32 op
;
4743 uint32_t offset
, xoffset
, left
, j
;
4747 r
= get_pointer_32(p
, offset
, left
, S
, info
);
4750 memset(&opl
, '\0', sizeof(struct objc_property_list32
));
4751 if (left
< sizeof(struct objc_property_list32
)) {
4752 memcpy(&opl
, r
, left
);
4753 outs() << " (objc_property_list entends past the end of the section)\n";
4755 memcpy(&opl
, r
, sizeof(struct objc_property_list32
));
4756 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4758 outs() << " entsize " << opl
.entsize
<< "\n";
4759 outs() << " count " << opl
.count
<< "\n";
4761 p
+= sizeof(struct objc_property_list32
);
4762 offset
+= sizeof(struct objc_property_list32
);
4763 for (j
= 0; j
< opl
.count
; j
++) {
4764 r
= get_pointer_32(p
, offset
, left
, S
, info
);
4767 memset(&op
, '\0', sizeof(struct objc_property32
));
4768 if (left
< sizeof(struct objc_property32
)) {
4769 memcpy(&op
, r
, left
);
4770 outs() << " (objc_property entends past the end of the section)\n";
4772 memcpy(&op
, r
, sizeof(struct objc_property32
));
4773 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4776 outs() << "\t\t\t name " << format("0x%" PRIx32
, op
.name
);
4777 name
= get_pointer_32(op
.name
, xoffset
, left
, xS
, info
);
4778 if (name
!= nullptr)
4779 outs() << format(" %.*s", left
, name
);
4782 outs() << "\t\t\tattributes " << format("0x%" PRIx32
, op
.attributes
);
4783 name
= get_pointer_32(op
.attributes
, xoffset
, left
, xS
, info
);
4784 if (name
!= nullptr)
4785 outs() << format(" %.*s", left
, name
);
4788 p
+= sizeof(struct objc_property32
);
4789 offset
+= sizeof(struct objc_property32
);
4793 static bool print_class_ro64_t(uint64_t p
, struct DisassembleInfo
*info
,
4794 bool &is_meta_class
) {
4795 struct class_ro64_t cro
;
4797 uint32_t offset
, xoffset
, left
;
4799 const char *name
, *sym_name
;
4802 r
= get_pointer_64(p
, offset
, left
, S
, info
);
4803 if (r
== nullptr || left
< sizeof(struct class_ro64_t
))
4805 memcpy(&cro
, r
, sizeof(struct class_ro64_t
));
4806 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4808 outs() << " flags " << format("0x%" PRIx32
, cro
.flags
);
4809 if (cro
.flags
& RO_META
)
4810 outs() << " RO_META";
4811 if (cro
.flags
& RO_ROOT
)
4812 outs() << " RO_ROOT";
4813 if (cro
.flags
& RO_HAS_CXX_STRUCTORS
)
4814 outs() << " RO_HAS_CXX_STRUCTORS";
4816 outs() << " instanceStart " << cro
.instanceStart
<< "\n";
4817 outs() << " instanceSize " << cro
.instanceSize
<< "\n";
4818 outs() << " reserved " << format("0x%" PRIx32
, cro
.reserved
)
4820 outs() << " ivarLayout " << format("0x%" PRIx64
, cro
.ivarLayout
)
4822 print_layout_map64(cro
.ivarLayout
, info
);
4825 sym_name
= get_symbol_64(offset
+ offsetof(struct class_ro64_t
, name
), S
,
4826 info
, n_value
, cro
.name
);
4828 if (info
->verbose
&& sym_name
!= nullptr)
4831 outs() << format("0x%" PRIx64
, n_value
);
4833 outs() << " + " << format("0x%" PRIx64
, cro
.name
);
4835 outs() << format("0x%" PRIx64
, cro
.name
);
4836 name
= get_pointer_64(cro
.name
+ n_value
, xoffset
, left
, xS
, info
);
4837 if (name
!= nullptr)
4838 outs() << format(" %.*s", left
, name
);
4841 outs() << " baseMethods ";
4842 sym_name
= get_symbol_64(offset
+ offsetof(struct class_ro64_t
, baseMethods
),
4843 S
, info
, n_value
, cro
.baseMethods
);
4845 if (info
->verbose
&& sym_name
!= nullptr)
4848 outs() << format("0x%" PRIx64
, n_value
);
4849 if (cro
.baseMethods
!= 0)
4850 outs() << " + " << format("0x%" PRIx64
, cro
.baseMethods
);
4852 outs() << format("0x%" PRIx64
, cro
.baseMethods
);
4853 outs() << " (struct method_list_t *)\n";
4854 if (cro
.baseMethods
+ n_value
!= 0)
4855 print_method_list64_t(cro
.baseMethods
+ n_value
, info
, "");
4857 outs() << " baseProtocols ";
4859 get_symbol_64(offset
+ offsetof(struct class_ro64_t
, baseProtocols
), S
,
4860 info
, n_value
, cro
.baseProtocols
);
4862 if (info
->verbose
&& sym_name
!= nullptr)
4865 outs() << format("0x%" PRIx64
, n_value
);
4866 if (cro
.baseProtocols
!= 0)
4867 outs() << " + " << format("0x%" PRIx64
, cro
.baseProtocols
);
4869 outs() << format("0x%" PRIx64
, cro
.baseProtocols
);
4871 if (cro
.baseProtocols
+ n_value
!= 0)
4872 print_protocol_list64_t(cro
.baseProtocols
+ n_value
, info
);
4874 outs() << " ivars ";
4875 sym_name
= get_symbol_64(offset
+ offsetof(struct class_ro64_t
, ivars
), S
,
4876 info
, n_value
, cro
.ivars
);
4878 if (info
->verbose
&& sym_name
!= nullptr)
4881 outs() << format("0x%" PRIx64
, n_value
);
4883 outs() << " + " << format("0x%" PRIx64
, cro
.ivars
);
4885 outs() << format("0x%" PRIx64
, cro
.ivars
);
4887 if (cro
.ivars
+ n_value
!= 0)
4888 print_ivar_list64_t(cro
.ivars
+ n_value
, info
);
4890 outs() << " weakIvarLayout ";
4892 get_symbol_64(offset
+ offsetof(struct class_ro64_t
, weakIvarLayout
), S
,
4893 info
, n_value
, cro
.weakIvarLayout
);
4895 if (info
->verbose
&& sym_name
!= nullptr)
4898 outs() << format("0x%" PRIx64
, n_value
);
4899 if (cro
.weakIvarLayout
!= 0)
4900 outs() << " + " << format("0x%" PRIx64
, cro
.weakIvarLayout
);
4902 outs() << format("0x%" PRIx64
, cro
.weakIvarLayout
);
4904 print_layout_map64(cro
.weakIvarLayout
+ n_value
, info
);
4906 outs() << " baseProperties ";
4908 get_symbol_64(offset
+ offsetof(struct class_ro64_t
, baseProperties
), S
,
4909 info
, n_value
, cro
.baseProperties
);
4911 if (info
->verbose
&& sym_name
!= nullptr)
4914 outs() << format("0x%" PRIx64
, n_value
);
4915 if (cro
.baseProperties
!= 0)
4916 outs() << " + " << format("0x%" PRIx64
, cro
.baseProperties
);
4918 outs() << format("0x%" PRIx64
, cro
.baseProperties
);
4920 if (cro
.baseProperties
+ n_value
!= 0)
4921 print_objc_property_list64(cro
.baseProperties
+ n_value
, info
);
4923 is_meta_class
= (cro
.flags
& RO_META
) != 0;
4927 static bool print_class_ro32_t(uint32_t p
, struct DisassembleInfo
*info
,
4928 bool &is_meta_class
) {
4929 struct class_ro32_t cro
;
4931 uint32_t offset
, xoffset
, left
;
4935 r
= get_pointer_32(p
, offset
, left
, S
, info
);
4938 memset(&cro
, '\0', sizeof(struct class_ro32_t
));
4939 if (left
< sizeof(struct class_ro32_t
)) {
4940 memcpy(&cro
, r
, left
);
4941 outs() << " (class_ro_t entends past the end of the section)\n";
4943 memcpy(&cro
, r
, sizeof(struct class_ro32_t
));
4944 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4946 outs() << " flags " << format("0x%" PRIx32
, cro
.flags
);
4947 if (cro
.flags
& RO_META
)
4948 outs() << " RO_META";
4949 if (cro
.flags
& RO_ROOT
)
4950 outs() << " RO_ROOT";
4951 if (cro
.flags
& RO_HAS_CXX_STRUCTORS
)
4952 outs() << " RO_HAS_CXX_STRUCTORS";
4954 outs() << " instanceStart " << cro
.instanceStart
<< "\n";
4955 outs() << " instanceSize " << cro
.instanceSize
<< "\n";
4956 outs() << " ivarLayout " << format("0x%" PRIx32
, cro
.ivarLayout
)
4958 print_layout_map32(cro
.ivarLayout
, info
);
4960 outs() << " name " << format("0x%" PRIx32
, cro
.name
);
4961 name
= get_pointer_32(cro
.name
, xoffset
, left
, xS
, info
);
4962 if (name
!= nullptr)
4963 outs() << format(" %.*s", left
, name
);
4966 outs() << " baseMethods "
4967 << format("0x%" PRIx32
, cro
.baseMethods
)
4968 << " (struct method_list_t *)\n";
4969 if (cro
.baseMethods
!= 0)
4970 print_method_list32_t(cro
.baseMethods
, info
, "");
4972 outs() << " baseProtocols "
4973 << format("0x%" PRIx32
, cro
.baseProtocols
) << "\n";
4974 if (cro
.baseProtocols
!= 0)
4975 print_protocol_list32_t(cro
.baseProtocols
, info
);
4976 outs() << " ivars " << format("0x%" PRIx32
, cro
.ivars
)
4979 print_ivar_list32_t(cro
.ivars
, info
);
4980 outs() << " weakIvarLayout "
4981 << format("0x%" PRIx32
, cro
.weakIvarLayout
) << "\n";
4982 print_layout_map32(cro
.weakIvarLayout
, info
);
4983 outs() << " baseProperties "
4984 << format("0x%" PRIx32
, cro
.baseProperties
) << "\n";
4985 if (cro
.baseProperties
!= 0)
4986 print_objc_property_list32(cro
.baseProperties
, info
);
4987 is_meta_class
= (cro
.flags
& RO_META
) != 0;
4991 static void print_class64_t(uint64_t p
, struct DisassembleInfo
*info
) {
4994 uint32_t offset
, left
;
4997 uint64_t isa_n_value
, n_value
;
4999 r
= get_pointer_64(p
, offset
, left
, S
, info
);
5000 if (r
== nullptr || left
< sizeof(struct class64_t
))
5002 memcpy(&c
, r
, sizeof(struct class64_t
));
5003 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5006 outs() << " isa " << format("0x%" PRIx64
, c
.isa
);
5007 name
= get_symbol_64(offset
+ offsetof(struct class64_t
, isa
), S
, info
,
5008 isa_n_value
, c
.isa
);
5009 if (name
!= nullptr)
5010 outs() << " " << name
;
5013 outs() << " superclass " << format("0x%" PRIx64
, c
.superclass
);
5014 name
= get_symbol_64(offset
+ offsetof(struct class64_t
, superclass
), S
, info
,
5015 n_value
, c
.superclass
);
5016 if (name
!= nullptr)
5017 outs() << " " << name
;
5019 name
= get_dyld_bind_info_symbolname(S
.getAddress() +
5020 offset
+ offsetof(struct class64_t
, superclass
), info
);
5021 if (name
!= nullptr)
5022 outs() << " " << name
;
5026 outs() << " cache " << format("0x%" PRIx64
, c
.cache
);
5027 name
= get_symbol_64(offset
+ offsetof(struct class64_t
, cache
), S
, info
,
5029 if (name
!= nullptr)
5030 outs() << " " << name
;
5033 outs() << " vtable " << format("0x%" PRIx64
, c
.vtable
);
5034 name
= get_symbol_64(offset
+ offsetof(struct class64_t
, vtable
), S
, info
,
5036 if (name
!= nullptr)
5037 outs() << " " << name
;
5040 name
= get_symbol_64(offset
+ offsetof(struct class64_t
, data
), S
, info
,
5044 if (info
->verbose
&& name
!= nullptr)
5047 outs() << format("0x%" PRIx64
, n_value
);
5049 outs() << " + " << format("0x%" PRIx64
, c
.data
);
5051 outs() << format("0x%" PRIx64
, c
.data
);
5052 outs() << " (struct class_ro_t *)";
5054 // This is a Swift class if some of the low bits of the pointer are set.
5055 if ((c
.data
+ n_value
) & 0x7)
5056 outs() << " Swift class";
5059 if (!print_class_ro64_t((c
.data
+ n_value
) & ~0x7, info
, is_meta_class
))
5062 if (!is_meta_class
&&
5063 c
.isa
+ isa_n_value
!= p
&&
5064 c
.isa
+ isa_n_value
!= 0 &&
5065 info
->depth
< 100) {
5067 outs() << "Meta Class\n";
5068 print_class64_t(c
.isa
+ isa_n_value
, info
);
5072 static void print_class32_t(uint32_t p
, struct DisassembleInfo
*info
) {
5075 uint32_t offset
, left
;
5079 r
= get_pointer_32(p
, offset
, left
, S
, info
);
5082 memset(&c
, '\0', sizeof(struct class32_t
));
5083 if (left
< sizeof(struct class32_t
)) {
5084 memcpy(&c
, r
, left
);
5085 outs() << " (class_t entends past the end of the section)\n";
5087 memcpy(&c
, r
, sizeof(struct class32_t
));
5088 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5091 outs() << " isa " << format("0x%" PRIx32
, c
.isa
);
5093 get_symbol_32(offset
+ offsetof(struct class32_t
, isa
), S
, info
, c
.isa
);
5094 if (name
!= nullptr)
5095 outs() << " " << name
;
5098 outs() << " superclass " << format("0x%" PRIx32
, c
.superclass
);
5099 name
= get_symbol_32(offset
+ offsetof(struct class32_t
, superclass
), S
, info
,
5101 if (name
!= nullptr)
5102 outs() << " " << name
;
5105 outs() << " cache " << format("0x%" PRIx32
, c
.cache
);
5106 name
= get_symbol_32(offset
+ offsetof(struct class32_t
, cache
), S
, info
,
5108 if (name
!= nullptr)
5109 outs() << " " << name
;
5112 outs() << " vtable " << format("0x%" PRIx32
, c
.vtable
);
5113 name
= get_symbol_32(offset
+ offsetof(struct class32_t
, vtable
), S
, info
,
5115 if (name
!= nullptr)
5116 outs() << " " << name
;
5120 get_symbol_32(offset
+ offsetof(struct class32_t
, data
), S
, info
, c
.data
);
5121 outs() << " data " << format("0x%" PRIx32
, c
.data
)
5122 << " (struct class_ro_t *)";
5124 // This is a Swift class if some of the low bits of the pointer are set.
5126 outs() << " Swift class";
5129 if (!print_class_ro32_t(c
.data
& ~0x3, info
, is_meta_class
))
5132 if (!is_meta_class
) {
5133 outs() << "Meta Class\n";
5134 print_class32_t(c
.isa
, info
);
5138 static void print_objc_class_t(struct objc_class_t
*objc_class
,
5139 struct DisassembleInfo
*info
) {
5140 uint32_t offset
, left
, xleft
;
5141 const char *name
, *p
, *ivar_list
;
5144 struct objc_ivar_list_t objc_ivar_list
;
5145 struct objc_ivar_t ivar
;
5147 outs() << "\t\t isa " << format("0x%08" PRIx32
, objc_class
->isa
);
5148 if (info
->verbose
&& CLS_GETINFO(objc_class
, CLS_META
)) {
5149 name
= get_pointer_32(objc_class
->isa
, offset
, left
, S
, info
, true);
5150 if (name
!= nullptr)
5151 outs() << format(" %.*s", left
, name
);
5153 outs() << " (not in an __OBJC section)";
5157 outs() << "\t super_class "
5158 << format("0x%08" PRIx32
, objc_class
->super_class
);
5159 if (info
->verbose
) {
5160 name
= get_pointer_32(objc_class
->super_class
, offset
, left
, S
, info
, true);
5161 if (name
!= nullptr)
5162 outs() << format(" %.*s", left
, name
);
5164 outs() << " (not in an __OBJC section)";
5168 outs() << "\t\t name " << format("0x%08" PRIx32
, objc_class
->name
);
5169 if (info
->verbose
) {
5170 name
= get_pointer_32(objc_class
->name
, offset
, left
, S
, info
, true);
5171 if (name
!= nullptr)
5172 outs() << format(" %.*s", left
, name
);
5174 outs() << " (not in an __OBJC section)";
5178 outs() << "\t\t version " << format("0x%08" PRIx32
, objc_class
->version
)
5181 outs() << "\t\t info " << format("0x%08" PRIx32
, objc_class
->info
);
5182 if (info
->verbose
) {
5183 if (CLS_GETINFO(objc_class
, CLS_CLASS
))
5184 outs() << " CLS_CLASS";
5185 else if (CLS_GETINFO(objc_class
, CLS_META
))
5186 outs() << " CLS_META";
5190 outs() << "\t instance_size "
5191 << format("0x%08" PRIx32
, objc_class
->instance_size
) << "\n";
5193 p
= get_pointer_32(objc_class
->ivars
, offset
, left
, S
, info
, true);
5194 outs() << "\t\t ivars " << format("0x%08" PRIx32
, objc_class
->ivars
);
5196 if (left
> sizeof(struct objc_ivar_list_t
)) {
5198 memcpy(&objc_ivar_list
, p
, sizeof(struct objc_ivar_list_t
));
5200 outs() << " (entends past the end of the section)\n";
5201 memset(&objc_ivar_list
, '\0', sizeof(struct objc_ivar_list_t
));
5202 memcpy(&objc_ivar_list
, p
, left
);
5204 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5205 swapStruct(objc_ivar_list
);
5206 outs() << "\t\t ivar_count " << objc_ivar_list
.ivar_count
<< "\n";
5207 ivar_list
= p
+ sizeof(struct objc_ivar_list_t
);
5208 for (i
= 0; i
< objc_ivar_list
.ivar_count
; i
++) {
5209 if ((i
+ 1) * sizeof(struct objc_ivar_t
) > left
) {
5210 outs() << "\t\t remaining ivar's extend past the of the section\n";
5213 memcpy(&ivar
, ivar_list
+ i
* sizeof(struct objc_ivar_t
),
5214 sizeof(struct objc_ivar_t
));
5215 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5218 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32
, ivar
.ivar_name
);
5219 if (info
->verbose
) {
5220 name
= get_pointer_32(ivar
.ivar_name
, offset
, xleft
, S
, info
, true);
5221 if (name
!= nullptr)
5222 outs() << format(" %.*s", xleft
, name
);
5224 outs() << " (not in an __OBJC section)";
5228 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32
, ivar
.ivar_type
);
5229 if (info
->verbose
) {
5230 name
= get_pointer_32(ivar
.ivar_type
, offset
, xleft
, S
, info
, true);
5231 if (name
!= nullptr)
5232 outs() << format(" %.*s", xleft
, name
);
5234 outs() << " (not in an __OBJC section)";
5238 outs() << "\t\t ivar_offset "
5239 << format("0x%08" PRIx32
, ivar
.ivar_offset
) << "\n";
5242 outs() << " (not in an __OBJC section)\n";
5245 outs() << "\t\t methods " << format("0x%08" PRIx32
, objc_class
->methodLists
);
5246 if (print_method_list(objc_class
->methodLists
, info
))
5247 outs() << " (not in an __OBJC section)\n";
5249 outs() << "\t\t cache " << format("0x%08" PRIx32
, objc_class
->cache
)
5252 outs() << "\t\tprotocols " << format("0x%08" PRIx32
, objc_class
->protocols
);
5253 if (print_protocol_list(objc_class
->protocols
, 16, info
))
5254 outs() << " (not in an __OBJC section)\n";
5257 static void print_objc_objc_category_t(struct objc_category_t
*objc_category
,
5258 struct DisassembleInfo
*info
) {
5259 uint32_t offset
, left
;
5263 outs() << "\t category name "
5264 << format("0x%08" PRIx32
, objc_category
->category_name
);
5265 if (info
->verbose
) {
5266 name
= get_pointer_32(objc_category
->category_name
, offset
, left
, S
, info
,
5268 if (name
!= nullptr)
5269 outs() << format(" %.*s", left
, name
);
5271 outs() << " (not in an __OBJC section)";
5275 outs() << "\t\t class name "
5276 << format("0x%08" PRIx32
, objc_category
->class_name
);
5277 if (info
->verbose
) {
5279 get_pointer_32(objc_category
->class_name
, offset
, left
, S
, info
, true);
5280 if (name
!= nullptr)
5281 outs() << format(" %.*s", left
, name
);
5283 outs() << " (not in an __OBJC section)";
5287 outs() << "\t instance methods "
5288 << format("0x%08" PRIx32
, objc_category
->instance_methods
);
5289 if (print_method_list(objc_category
->instance_methods
, info
))
5290 outs() << " (not in an __OBJC section)\n";
5292 outs() << "\t class methods "
5293 << format("0x%08" PRIx32
, objc_category
->class_methods
);
5294 if (print_method_list(objc_category
->class_methods
, info
))
5295 outs() << " (not in an __OBJC section)\n";
5298 static void print_category64_t(uint64_t p
, struct DisassembleInfo
*info
) {
5299 struct category64_t c
;
5301 uint32_t offset
, xoffset
, left
;
5303 const char *name
, *sym_name
;
5306 r
= get_pointer_64(p
, offset
, left
, S
, info
);
5309 memset(&c
, '\0', sizeof(struct category64_t
));
5310 if (left
< sizeof(struct category64_t
)) {
5311 memcpy(&c
, r
, left
);
5312 outs() << " (category_t entends past the end of the section)\n";
5314 memcpy(&c
, r
, sizeof(struct category64_t
));
5315 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5319 sym_name
= get_symbol_64(offset
+ offsetof(struct category64_t
, name
), S
,
5320 info
, n_value
, c
.name
);
5322 if (info
->verbose
&& sym_name
!= nullptr)
5325 outs() << format("0x%" PRIx64
, n_value
);
5327 outs() << " + " << format("0x%" PRIx64
, c
.name
);
5329 outs() << format("0x%" PRIx64
, c
.name
);
5330 name
= get_pointer_64(c
.name
+ n_value
, xoffset
, left
, xS
, info
);
5331 if (name
!= nullptr)
5332 outs() << format(" %.*s", left
, name
);
5336 sym_name
= get_symbol_64(offset
+ offsetof(struct category64_t
, cls
), S
, info
,
5339 if (info
->verbose
&& sym_name
!= nullptr)
5342 outs() << format("0x%" PRIx64
, n_value
);
5344 outs() << " + " << format("0x%" PRIx64
, c
.cls
);
5346 outs() << format("0x%" PRIx64
, c
.cls
);
5348 if (c
.cls
+ n_value
!= 0)
5349 print_class64_t(c
.cls
+ n_value
, info
);
5351 outs() << " instanceMethods ";
5353 get_symbol_64(offset
+ offsetof(struct category64_t
, instanceMethods
), S
,
5354 info
, n_value
, c
.instanceMethods
);
5356 if (info
->verbose
&& sym_name
!= nullptr)
5359 outs() << format("0x%" PRIx64
, n_value
);
5360 if (c
.instanceMethods
!= 0)
5361 outs() << " + " << format("0x%" PRIx64
, c
.instanceMethods
);
5363 outs() << format("0x%" PRIx64
, c
.instanceMethods
);
5365 if (c
.instanceMethods
+ n_value
!= 0)
5366 print_method_list64_t(c
.instanceMethods
+ n_value
, info
, "");
5368 outs() << " classMethods ";
5369 sym_name
= get_symbol_64(offset
+ offsetof(struct category64_t
, classMethods
),
5370 S
, info
, n_value
, c
.classMethods
);
5372 if (info
->verbose
&& sym_name
!= nullptr)
5375 outs() << format("0x%" PRIx64
, n_value
);
5376 if (c
.classMethods
!= 0)
5377 outs() << " + " << format("0x%" PRIx64
, c
.classMethods
);
5379 outs() << format("0x%" PRIx64
, c
.classMethods
);
5381 if (c
.classMethods
+ n_value
!= 0)
5382 print_method_list64_t(c
.classMethods
+ n_value
, info
, "");
5384 outs() << " protocols ";
5385 sym_name
= get_symbol_64(offset
+ offsetof(struct category64_t
, protocols
), S
,
5386 info
, n_value
, c
.protocols
);
5388 if (info
->verbose
&& sym_name
!= nullptr)
5391 outs() << format("0x%" PRIx64
, n_value
);
5392 if (c
.protocols
!= 0)
5393 outs() << " + " << format("0x%" PRIx64
, c
.protocols
);
5395 outs() << format("0x%" PRIx64
, c
.protocols
);
5397 if (c
.protocols
+ n_value
!= 0)
5398 print_protocol_list64_t(c
.protocols
+ n_value
, info
);
5400 outs() << "instanceProperties ";
5402 get_symbol_64(offset
+ offsetof(struct category64_t
, instanceProperties
),
5403 S
, info
, n_value
, c
.instanceProperties
);
5405 if (info
->verbose
&& sym_name
!= nullptr)
5408 outs() << format("0x%" PRIx64
, n_value
);
5409 if (c
.instanceProperties
!= 0)
5410 outs() << " + " << format("0x%" PRIx64
, c
.instanceProperties
);
5412 outs() << format("0x%" PRIx64
, c
.instanceProperties
);
5414 if (c
.instanceProperties
+ n_value
!= 0)
5415 print_objc_property_list64(c
.instanceProperties
+ n_value
, info
);
5418 static void print_category32_t(uint32_t p
, struct DisassembleInfo
*info
) {
5419 struct category32_t c
;
5421 uint32_t offset
, left
;
5425 r
= get_pointer_32(p
, offset
, left
, S
, info
);
5428 memset(&c
, '\0', sizeof(struct category32_t
));
5429 if (left
< sizeof(struct category32_t
)) {
5430 memcpy(&c
, r
, left
);
5431 outs() << " (category_t entends past the end of the section)\n";
5433 memcpy(&c
, r
, sizeof(struct category32_t
));
5434 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5437 outs() << " name " << format("0x%" PRIx32
, c
.name
);
5438 name
= get_symbol_32(offset
+ offsetof(struct category32_t
, name
), S
, info
,
5441 outs() << " " << name
;
5444 outs() << " cls " << format("0x%" PRIx32
, c
.cls
) << "\n";
5446 print_class32_t(c
.cls
, info
);
5447 outs() << " instanceMethods " << format("0x%" PRIx32
, c
.instanceMethods
)
5449 if (c
.instanceMethods
!= 0)
5450 print_method_list32_t(c
.instanceMethods
, info
, "");
5451 outs() << " classMethods " << format("0x%" PRIx32
, c
.classMethods
)
5453 if (c
.classMethods
!= 0)
5454 print_method_list32_t(c
.classMethods
, info
, "");
5455 outs() << " protocols " << format("0x%" PRIx32
, c
.protocols
) << "\n";
5456 if (c
.protocols
!= 0)
5457 print_protocol_list32_t(c
.protocols
, info
);
5458 outs() << "instanceProperties " << format("0x%" PRIx32
, c
.instanceProperties
)
5460 if (c
.instanceProperties
!= 0)
5461 print_objc_property_list32(c
.instanceProperties
, info
);
5464 static void print_message_refs64(SectionRef S
, struct DisassembleInfo
*info
) {
5465 uint32_t i
, left
, offset
, xoffset
;
5466 uint64_t p
, n_value
;
5467 struct message_ref64 mr
;
5468 const char *name
, *sym_name
;
5472 if (S
== SectionRef())
5476 S
.getName(SectName
);
5477 DataRefImpl Ref
= S
.getRawDataRefImpl();
5478 StringRef SegName
= info
->O
->getSectionFinalSegmentName(Ref
);
5479 outs() << "Contents of (" << SegName
<< "," << SectName
<< ") section\n";
5481 for (i
= 0; i
< S
.getSize(); i
+= sizeof(struct message_ref64
)) {
5482 p
= S
.getAddress() + i
;
5483 r
= get_pointer_64(p
, offset
, left
, S
, info
);
5486 memset(&mr
, '\0', sizeof(struct message_ref64
));
5487 if (left
< sizeof(struct message_ref64
)) {
5488 memcpy(&mr
, r
, left
);
5489 outs() << " (message_ref entends past the end of the section)\n";
5491 memcpy(&mr
, r
, sizeof(struct message_ref64
));
5492 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5496 name
= get_symbol_64(offset
+ offsetof(struct message_ref64
, imp
), S
, info
,
5499 outs() << format("0x%" PRIx64
, n_value
) << " ";
5501 outs() << "+ " << format("0x%" PRIx64
, mr
.imp
) << " ";
5503 outs() << format("0x%" PRIx64
, mr
.imp
) << " ";
5504 if (name
!= nullptr)
5505 outs() << " " << name
;
5509 sym_name
= get_symbol_64(offset
+ offsetof(struct message_ref64
, sel
), S
,
5510 info
, n_value
, mr
.sel
);
5512 if (info
->verbose
&& sym_name
!= nullptr)
5515 outs() << format("0x%" PRIx64
, n_value
);
5517 outs() << " + " << format("0x%" PRIx64
, mr
.sel
);
5519 outs() << format("0x%" PRIx64
, mr
.sel
);
5520 name
= get_pointer_64(mr
.sel
+ n_value
, xoffset
, left
, xS
, info
);
5521 if (name
!= nullptr)
5522 outs() << format(" %.*s", left
, name
);
5525 offset
+= sizeof(struct message_ref64
);
5529 static void print_message_refs32(SectionRef S
, struct DisassembleInfo
*info
) {
5530 uint32_t i
, left
, offset
, xoffset
, p
;
5531 struct message_ref32 mr
;
5532 const char *name
, *r
;
5535 if (S
== SectionRef())
5539 S
.getName(SectName
);
5540 DataRefImpl Ref
= S
.getRawDataRefImpl();
5541 StringRef SegName
= info
->O
->getSectionFinalSegmentName(Ref
);
5542 outs() << "Contents of (" << SegName
<< "," << SectName
<< ") section\n";
5544 for (i
= 0; i
< S
.getSize(); i
+= sizeof(struct message_ref64
)) {
5545 p
= S
.getAddress() + i
;
5546 r
= get_pointer_32(p
, offset
, left
, S
, info
);
5549 memset(&mr
, '\0', sizeof(struct message_ref32
));
5550 if (left
< sizeof(struct message_ref32
)) {
5551 memcpy(&mr
, r
, left
);
5552 outs() << " (message_ref entends past the end of the section)\n";
5554 memcpy(&mr
, r
, sizeof(struct message_ref32
));
5555 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5558 outs() << " imp " << format("0x%" PRIx32
, mr
.imp
);
5559 name
= get_symbol_32(offset
+ offsetof(struct message_ref32
, imp
), S
, info
,
5561 if (name
!= nullptr)
5562 outs() << " " << name
;
5565 outs() << " sel " << format("0x%" PRIx32
, mr
.sel
);
5566 name
= get_pointer_32(mr
.sel
, xoffset
, left
, xS
, info
);
5567 if (name
!= nullptr)
5568 outs() << " " << name
;
5571 offset
+= sizeof(struct message_ref32
);
5575 static void print_image_info64(SectionRef S
, struct DisassembleInfo
*info
) {
5576 uint32_t left
, offset
, swift_version
;
5578 struct objc_image_info64 o
;
5581 if (S
== SectionRef())
5585 S
.getName(SectName
);
5586 DataRefImpl Ref
= S
.getRawDataRefImpl();
5587 StringRef SegName
= info
->O
->getSectionFinalSegmentName(Ref
);
5588 outs() << "Contents of (" << SegName
<< "," << SectName
<< ") section\n";
5590 r
= get_pointer_64(p
, offset
, left
, S
, info
);
5593 memset(&o
, '\0', sizeof(struct objc_image_info64
));
5594 if (left
< sizeof(struct objc_image_info64
)) {
5595 memcpy(&o
, r
, left
);
5596 outs() << " (objc_image_info entends past the end of the section)\n";
5598 memcpy(&o
, r
, sizeof(struct objc_image_info64
));
5599 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5601 outs() << " version " << o
.version
<< "\n";
5602 outs() << " flags " << format("0x%" PRIx32
, o
.flags
);
5603 if (o
.flags
& OBJC_IMAGE_IS_REPLACEMENT
)
5604 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5605 if (o
.flags
& OBJC_IMAGE_SUPPORTS_GC
)
5606 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5607 if (o
.flags
& OBJC_IMAGE_IS_SIMULATED
)
5608 outs() << " OBJC_IMAGE_IS_SIMULATED";
5609 if (o
.flags
& OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES
)
5610 outs() << " OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES";
5611 swift_version
= (o
.flags
>> 8) & 0xff;
5612 if (swift_version
!= 0) {
5613 if (swift_version
== 1)
5614 outs() << " Swift 1.0";
5615 else if (swift_version
== 2)
5616 outs() << " Swift 1.1";
5617 else if(swift_version
== 3)
5618 outs() << " Swift 2.0";
5619 else if(swift_version
== 4)
5620 outs() << " Swift 3.0";
5621 else if(swift_version
== 5)
5622 outs() << " Swift 4.0";
5623 else if(swift_version
== 6)
5624 outs() << " Swift 4.1";
5626 outs() << " unknown future Swift version (" << swift_version
<< ")";
5631 static void print_image_info32(SectionRef S
, struct DisassembleInfo
*info
) {
5632 uint32_t left
, offset
, swift_version
, p
;
5633 struct objc_image_info32 o
;
5636 if (S
== SectionRef())
5640 S
.getName(SectName
);
5641 DataRefImpl Ref
= S
.getRawDataRefImpl();
5642 StringRef SegName
= info
->O
->getSectionFinalSegmentName(Ref
);
5643 outs() << "Contents of (" << SegName
<< "," << SectName
<< ") section\n";
5645 r
= get_pointer_32(p
, offset
, left
, S
, info
);
5648 memset(&o
, '\0', sizeof(struct objc_image_info32
));
5649 if (left
< sizeof(struct objc_image_info32
)) {
5650 memcpy(&o
, r
, left
);
5651 outs() << " (objc_image_info entends past the end of the section)\n";
5653 memcpy(&o
, r
, sizeof(struct objc_image_info32
));
5654 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5656 outs() << " version " << o
.version
<< "\n";
5657 outs() << " flags " << format("0x%" PRIx32
, o
.flags
);
5658 if (o
.flags
& OBJC_IMAGE_IS_REPLACEMENT
)
5659 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5660 if (o
.flags
& OBJC_IMAGE_SUPPORTS_GC
)
5661 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5662 swift_version
= (o
.flags
>> 8) & 0xff;
5663 if (swift_version
!= 0) {
5664 if (swift_version
== 1)
5665 outs() << " Swift 1.0";
5666 else if (swift_version
== 2)
5667 outs() << " Swift 1.1";
5668 else if(swift_version
== 3)
5669 outs() << " Swift 2.0";
5670 else if(swift_version
== 4)
5671 outs() << " Swift 3.0";
5672 else if(swift_version
== 5)
5673 outs() << " Swift 4.0";
5674 else if(swift_version
== 6)
5675 outs() << " Swift 4.1";
5677 outs() << " unknown future Swift version (" << swift_version
<< ")";
5682 static void print_image_info(SectionRef S
, struct DisassembleInfo
*info
) {
5683 uint32_t left
, offset
, p
;
5684 struct imageInfo_t o
;
5688 S
.getName(SectName
);
5689 DataRefImpl Ref
= S
.getRawDataRefImpl();
5690 StringRef SegName
= info
->O
->getSectionFinalSegmentName(Ref
);
5691 outs() << "Contents of (" << SegName
<< "," << SectName
<< ") section\n";
5693 r
= get_pointer_32(p
, offset
, left
, S
, info
);
5696 memset(&o
, '\0', sizeof(struct imageInfo_t
));
5697 if (left
< sizeof(struct imageInfo_t
)) {
5698 memcpy(&o
, r
, left
);
5699 outs() << " (imageInfo entends past the end of the section)\n";
5701 memcpy(&o
, r
, sizeof(struct imageInfo_t
));
5702 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5704 outs() << " version " << o
.version
<< "\n";
5705 outs() << " flags " << format("0x%" PRIx32
, o
.flags
);
5711 outs() << " GC-only";
5717 static void printObjc2_64bit_MetaData(MachOObjectFile
*O
, bool verbose
) {
5718 SymbolAddressMap AddrMap
;
5720 CreateSymbolAddressMap(O
, &AddrMap
);
5722 std::vector
<SectionRef
> Sections
;
5723 for (const SectionRef
&Section
: O
->sections()) {
5725 Section
.getName(SectName
);
5726 Sections
.push_back(Section
);
5729 struct DisassembleInfo
info(O
, &AddrMap
, &Sections
, verbose
);
5731 SectionRef CL
= get_section(O
, "__OBJC2", "__class_list");
5732 if (CL
== SectionRef())
5733 CL
= get_section(O
, "__DATA", "__objc_classlist");
5734 if (CL
== SectionRef())
5735 CL
= get_section(O
, "__DATA_CONST", "__objc_classlist");
5736 if (CL
== SectionRef())
5737 CL
= get_section(O
, "__DATA_DIRTY", "__objc_classlist");
5739 walk_pointer_list_64("class", CL
, O
, &info
, print_class64_t
);
5741 SectionRef CR
= get_section(O
, "__OBJC2", "__class_refs");
5742 if (CR
== SectionRef())
5743 CR
= get_section(O
, "__DATA", "__objc_classrefs");
5744 if (CR
== SectionRef())
5745 CR
= get_section(O
, "__DATA_CONST", "__objc_classrefs");
5746 if (CR
== SectionRef())
5747 CR
= get_section(O
, "__DATA_DIRTY", "__objc_classrefs");
5749 walk_pointer_list_64("class refs", CR
, O
, &info
, nullptr);
5751 SectionRef SR
= get_section(O
, "__OBJC2", "__super_refs");
5752 if (SR
== SectionRef())
5753 SR
= get_section(O
, "__DATA", "__objc_superrefs");
5754 if (SR
== SectionRef())
5755 SR
= get_section(O
, "__DATA_CONST", "__objc_superrefs");
5756 if (SR
== SectionRef())
5757 SR
= get_section(O
, "__DATA_DIRTY", "__objc_superrefs");
5759 walk_pointer_list_64("super refs", SR
, O
, &info
, nullptr);
5761 SectionRef CA
= get_section(O
, "__OBJC2", "__category_list");
5762 if (CA
== SectionRef())
5763 CA
= get_section(O
, "__DATA", "__objc_catlist");
5764 if (CA
== SectionRef())
5765 CA
= get_section(O
, "__DATA_CONST", "__objc_catlist");
5766 if (CA
== SectionRef())
5767 CA
= get_section(O
, "__DATA_DIRTY", "__objc_catlist");
5769 walk_pointer_list_64("category", CA
, O
, &info
, print_category64_t
);
5771 SectionRef PL
= get_section(O
, "__OBJC2", "__protocol_list");
5772 if (PL
== SectionRef())
5773 PL
= get_section(O
, "__DATA", "__objc_protolist");
5774 if (PL
== SectionRef())
5775 PL
= get_section(O
, "__DATA_CONST", "__objc_protolist");
5776 if (PL
== SectionRef())
5777 PL
= get_section(O
, "__DATA_DIRTY", "__objc_protolist");
5779 walk_pointer_list_64("protocol", PL
, O
, &info
, nullptr);
5781 SectionRef MR
= get_section(O
, "__OBJC2", "__message_refs");
5782 if (MR
== SectionRef())
5783 MR
= get_section(O
, "__DATA", "__objc_msgrefs");
5784 if (MR
== SectionRef())
5785 MR
= get_section(O
, "__DATA_CONST", "__objc_msgrefs");
5786 if (MR
== SectionRef())
5787 MR
= get_section(O
, "__DATA_DIRTY", "__objc_msgrefs");
5789 print_message_refs64(MR
, &info
);
5791 SectionRef II
= get_section(O
, "__OBJC2", "__image_info");
5792 if (II
== SectionRef())
5793 II
= get_section(O
, "__DATA", "__objc_imageinfo");
5794 if (II
== SectionRef())
5795 II
= get_section(O
, "__DATA_CONST", "__objc_imageinfo");
5796 if (II
== SectionRef())
5797 II
= get_section(O
, "__DATA_DIRTY", "__objc_imageinfo");
5799 print_image_info64(II
, &info
);
5802 static void printObjc2_32bit_MetaData(MachOObjectFile
*O
, bool verbose
) {
5803 SymbolAddressMap AddrMap
;
5805 CreateSymbolAddressMap(O
, &AddrMap
);
5807 std::vector
<SectionRef
> Sections
;
5808 for (const SectionRef
&Section
: O
->sections()) {
5810 Section
.getName(SectName
);
5811 Sections
.push_back(Section
);
5814 struct DisassembleInfo
info(O
, &AddrMap
, &Sections
, verbose
);
5816 SectionRef CL
= get_section(O
, "__OBJC2", "__class_list");
5817 if (CL
== SectionRef())
5818 CL
= get_section(O
, "__DATA", "__objc_classlist");
5819 if (CL
== SectionRef())
5820 CL
= get_section(O
, "__DATA_CONST", "__objc_classlist");
5821 if (CL
== SectionRef())
5822 CL
= get_section(O
, "__DATA_DIRTY", "__objc_classlist");
5824 walk_pointer_list_32("class", CL
, O
, &info
, print_class32_t
);
5826 SectionRef CR
= get_section(O
, "__OBJC2", "__class_refs");
5827 if (CR
== SectionRef())
5828 CR
= get_section(O
, "__DATA", "__objc_classrefs");
5829 if (CR
== SectionRef())
5830 CR
= get_section(O
, "__DATA_CONST", "__objc_classrefs");
5831 if (CR
== SectionRef())
5832 CR
= get_section(O
, "__DATA_DIRTY", "__objc_classrefs");
5834 walk_pointer_list_32("class refs", CR
, O
, &info
, nullptr);
5836 SectionRef SR
= get_section(O
, "__OBJC2", "__super_refs");
5837 if (SR
== SectionRef())
5838 SR
= get_section(O
, "__DATA", "__objc_superrefs");
5839 if (SR
== SectionRef())
5840 SR
= get_section(O
, "__DATA_CONST", "__objc_superrefs");
5841 if (SR
== SectionRef())
5842 SR
= get_section(O
, "__DATA_DIRTY", "__objc_superrefs");
5844 walk_pointer_list_32("super refs", SR
, O
, &info
, nullptr);
5846 SectionRef CA
= get_section(O
, "__OBJC2", "__category_list");
5847 if (CA
== SectionRef())
5848 CA
= get_section(O
, "__DATA", "__objc_catlist");
5849 if (CA
== SectionRef())
5850 CA
= get_section(O
, "__DATA_CONST", "__objc_catlist");
5851 if (CA
== SectionRef())
5852 CA
= get_section(O
, "__DATA_DIRTY", "__objc_catlist");
5854 walk_pointer_list_32("category", CA
, O
, &info
, print_category32_t
);
5856 SectionRef PL
= get_section(O
, "__OBJC2", "__protocol_list");
5857 if (PL
== SectionRef())
5858 PL
= get_section(O
, "__DATA", "__objc_protolist");
5859 if (PL
== SectionRef())
5860 PL
= get_section(O
, "__DATA_CONST", "__objc_protolist");
5861 if (PL
== SectionRef())
5862 PL
= get_section(O
, "__DATA_DIRTY", "__objc_protolist");
5864 walk_pointer_list_32("protocol", PL
, O
, &info
, nullptr);
5866 SectionRef MR
= get_section(O
, "__OBJC2", "__message_refs");
5867 if (MR
== SectionRef())
5868 MR
= get_section(O
, "__DATA", "__objc_msgrefs");
5869 if (MR
== SectionRef())
5870 MR
= get_section(O
, "__DATA_CONST", "__objc_msgrefs");
5871 if (MR
== SectionRef())
5872 MR
= get_section(O
, "__DATA_DIRTY", "__objc_msgrefs");
5874 print_message_refs32(MR
, &info
);
5876 SectionRef II
= get_section(O
, "__OBJC2", "__image_info");
5877 if (II
== SectionRef())
5878 II
= get_section(O
, "__DATA", "__objc_imageinfo");
5879 if (II
== SectionRef())
5880 II
= get_section(O
, "__DATA_CONST", "__objc_imageinfo");
5881 if (II
== SectionRef())
5882 II
= get_section(O
, "__DATA_DIRTY", "__objc_imageinfo");
5884 print_image_info32(II
, &info
);
5887 static bool printObjc1_32bit_MetaData(MachOObjectFile
*O
, bool verbose
) {
5888 uint32_t i
, j
, p
, offset
, xoffset
, left
, defs_left
, def
;
5889 const char *r
, *name
, *defs
;
5890 struct objc_module_t module
;
5892 struct objc_symtab_t symtab
;
5893 struct objc_class_t objc_class
;
5894 struct objc_category_t objc_category
;
5896 outs() << "Objective-C segment\n";
5897 S
= get_section(O
, "__OBJC", "__module_info");
5898 if (S
== SectionRef())
5901 SymbolAddressMap AddrMap
;
5903 CreateSymbolAddressMap(O
, &AddrMap
);
5905 std::vector
<SectionRef
> Sections
;
5906 for (const SectionRef
&Section
: O
->sections()) {
5908 Section
.getName(SectName
);
5909 Sections
.push_back(Section
);
5912 struct DisassembleInfo
info(O
, &AddrMap
, &Sections
, verbose
);
5914 for (i
= 0; i
< S
.getSize(); i
+= sizeof(struct objc_module_t
)) {
5915 p
= S
.getAddress() + i
;
5916 r
= get_pointer_32(p
, offset
, left
, S
, &info
, true);
5919 memset(&module
, '\0', sizeof(struct objc_module_t
));
5920 if (left
< sizeof(struct objc_module_t
)) {
5921 memcpy(&module
, r
, left
);
5922 outs() << " (module extends past end of __module_info section)\n";
5924 memcpy(&module
, r
, sizeof(struct objc_module_t
));
5925 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
5928 outs() << "Module " << format("0x%" PRIx32
, p
) << "\n";
5929 outs() << " version " << module
.version
<< "\n";
5930 outs() << " size " << module
.size
<< "\n";
5932 name
= get_pointer_32(module
.name
, xoffset
, left
, xS
, &info
, true);
5933 if (name
!= nullptr)
5934 outs() << format("%.*s", left
, name
);
5936 outs() << format("0x%08" PRIx32
, module
.name
)
5937 << "(not in an __OBJC section)";
5940 r
= get_pointer_32(module
.symtab
, xoffset
, left
, xS
, &info
, true);
5941 if (module
.symtab
== 0 || r
== nullptr) {
5942 outs() << " symtab " << format("0x%08" PRIx32
, module
.symtab
)
5943 << " (not in an __OBJC section)\n";
5946 outs() << " symtab " << format("0x%08" PRIx32
, module
.symtab
) << "\n";
5947 memset(&symtab
, '\0', sizeof(struct objc_symtab_t
));
5950 if (left
< sizeof(struct objc_symtab_t
)) {
5951 memcpy(&symtab
, r
, left
);
5952 outs() << "\tsymtab extends past end of an __OBJC section)\n";
5954 memcpy(&symtab
, r
, sizeof(struct objc_symtab_t
));
5955 if (left
> sizeof(struct objc_symtab_t
)) {
5956 defs_left
= left
- sizeof(struct objc_symtab_t
);
5957 defs
= r
+ sizeof(struct objc_symtab_t
);
5960 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
5963 outs() << "\tsel_ref_cnt " << symtab
.sel_ref_cnt
<< "\n";
5964 r
= get_pointer_32(symtab
.refs
, xoffset
, left
, xS
, &info
, true);
5965 outs() << "\trefs " << format("0x%08" PRIx32
, symtab
.refs
);
5967 outs() << " (not in an __OBJC section)";
5969 outs() << "\tcls_def_cnt " << symtab
.cls_def_cnt
<< "\n";
5970 outs() << "\tcat_def_cnt " << symtab
.cat_def_cnt
<< "\n";
5971 if (symtab
.cls_def_cnt
> 0)
5972 outs() << "\tClass Definitions\n";
5973 for (j
= 0; j
< symtab
.cls_def_cnt
; j
++) {
5974 if ((j
+ 1) * sizeof(uint32_t) > defs_left
) {
5975 outs() << "\t(remaining class defs entries entends past the end of the "
5979 memcpy(&def
, defs
+ j
* sizeof(uint32_t), sizeof(uint32_t));
5980 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
5981 sys::swapByteOrder(def
);
5983 r
= get_pointer_32(def
, xoffset
, left
, xS
, &info
, true);
5984 outs() << "\tdefs[" << j
<< "] " << format("0x%08" PRIx32
, def
);
5986 if (left
> sizeof(struct objc_class_t
)) {
5988 memcpy(&objc_class
, r
, sizeof(struct objc_class_t
));
5990 outs() << " (entends past the end of the section)\n";
5991 memset(&objc_class
, '\0', sizeof(struct objc_class_t
));
5992 memcpy(&objc_class
, r
, left
);
5994 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
5995 swapStruct(objc_class
);
5996 print_objc_class_t(&objc_class
, &info
);
5998 outs() << "(not in an __OBJC section)\n";
6001 if (CLS_GETINFO(&objc_class
, CLS_CLASS
)) {
6002 outs() << "\tMeta Class";
6003 r
= get_pointer_32(objc_class
.isa
, xoffset
, left
, xS
, &info
, true);
6005 if (left
> sizeof(struct objc_class_t
)) {
6007 memcpy(&objc_class
, r
, sizeof(struct objc_class_t
));
6009 outs() << " (entends past the end of the section)\n";
6010 memset(&objc_class
, '\0', sizeof(struct objc_class_t
));
6011 memcpy(&objc_class
, r
, left
);
6013 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
6014 swapStruct(objc_class
);
6015 print_objc_class_t(&objc_class
, &info
);
6017 outs() << "(not in an __OBJC section)\n";
6021 if (symtab
.cat_def_cnt
> 0)
6022 outs() << "\tCategory Definitions\n";
6023 for (j
= 0; j
< symtab
.cat_def_cnt
; j
++) {
6024 if ((j
+ symtab
.cls_def_cnt
+ 1) * sizeof(uint32_t) > defs_left
) {
6025 outs() << "\t(remaining category defs entries entends past the end of "
6026 << "the section)\n";
6029 memcpy(&def
, defs
+ (j
+ symtab
.cls_def_cnt
) * sizeof(uint32_t),
6031 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
6032 sys::swapByteOrder(def
);
6034 r
= get_pointer_32(def
, xoffset
, left
, xS
, &info
, true);
6035 outs() << "\tdefs[" << j
+ symtab
.cls_def_cnt
<< "] "
6036 << format("0x%08" PRIx32
, def
);
6038 if (left
> sizeof(struct objc_category_t
)) {
6040 memcpy(&objc_category
, r
, sizeof(struct objc_category_t
));
6042 outs() << " (entends past the end of the section)\n";
6043 memset(&objc_category
, '\0', sizeof(struct objc_category_t
));
6044 memcpy(&objc_category
, r
, left
);
6046 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
6047 swapStruct(objc_category
);
6048 print_objc_objc_category_t(&objc_category
, &info
);
6050 outs() << "(not in an __OBJC section)\n";
6054 const SectionRef II
= get_section(O
, "__OBJC", "__image_info");
6055 if (II
!= SectionRef())
6056 print_image_info(II
, &info
);
6061 static void DumpProtocolSection(MachOObjectFile
*O
, const char *sect
,
6062 uint32_t size
, uint32_t addr
) {
6063 SymbolAddressMap AddrMap
;
6064 CreateSymbolAddressMap(O
, &AddrMap
);
6066 std::vector
<SectionRef
> Sections
;
6067 for (const SectionRef
&Section
: O
->sections()) {
6069 Section
.getName(SectName
);
6070 Sections
.push_back(Section
);
6073 struct DisassembleInfo
info(O
, &AddrMap
, &Sections
, true);
6076 struct objc_protocol_t protocol
;
6077 uint32_t left
, paddr
;
6078 for (p
= sect
; p
< sect
+ size
; p
+= sizeof(struct objc_protocol_t
)) {
6079 memset(&protocol
, '\0', sizeof(struct objc_protocol_t
));
6080 left
= size
- (p
- sect
);
6081 if (left
< sizeof(struct objc_protocol_t
)) {
6082 outs() << "Protocol extends past end of __protocol section\n";
6083 memcpy(&protocol
, p
, left
);
6085 memcpy(&protocol
, p
, sizeof(struct objc_protocol_t
));
6086 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
6087 swapStruct(protocol
);
6088 paddr
= addr
+ (p
- sect
);
6089 outs() << "Protocol " << format("0x%" PRIx32
, paddr
);
6090 if (print_protocol(paddr
, 0, &info
))
6091 outs() << "(not in an __OBJC section)\n";
6096 inline void swapStruct(struct xar_header
&xar
) {
6097 sys::swapByteOrder(xar
.magic
);
6098 sys::swapByteOrder(xar
.size
);
6099 sys::swapByteOrder(xar
.version
);
6100 sys::swapByteOrder(xar
.toc_length_compressed
);
6101 sys::swapByteOrder(xar
.toc_length_uncompressed
);
6102 sys::swapByteOrder(xar
.cksum_alg
);
6105 static void PrintModeVerbose(uint32_t mode
) {
6106 switch(mode
& S_IFMT
){
6130 /* owner permissions */
6141 else if(mode
& S_IEXEC
)
6146 /* group permissions */
6147 if(mode
& (S_IREAD
>> 3))
6151 if(mode
& (S_IWRITE
>> 3))
6157 else if(mode
& (S_IEXEC
>> 3))
6162 /* other permissions */
6163 if(mode
& (S_IREAD
>> 6))
6167 if(mode
& (S_IWRITE
>> 6))
6173 else if(mode
& (S_IEXEC
>> 6))
6179 static void PrintXarFilesSummary(const char *XarFilename
, xar_t xar
) {
6181 const char *key
, *type
, *mode
, *user
, *group
, *size
, *mtime
, *name
, *m
;
6183 uint32_t mode_value
;
6187 errs() << "Can't obtain an xar iterator for xar archive "
6188 << XarFilename
<< "\n";
6192 // Go through the xar's files.
6193 for (xf
= xar_file_first(xar
, xi
); xf
; xf
= xar_file_next(xi
)) {
6196 errs() << "Can't obtain an xar iterator for xar archive "
6197 << XarFilename
<< "\n";
6207 for(key
= xar_prop_first(xf
, xp
); key
; key
= xar_prop_next(xp
)){
6208 const char *val
= nullptr;
6209 xar_prop_get(xf
, key
, &val
);
6210 #if 0 // Useful for debugging.
6211 outs() << "key: " << key
<< " value: " << val
<< "\n";
6213 if(strcmp(key
, "type") == 0)
6215 if(strcmp(key
, "mode") == 0)
6217 if(strcmp(key
, "user") == 0)
6219 if(strcmp(key
, "group") == 0)
6221 if(strcmp(key
, "data/size") == 0)
6223 if(strcmp(key
, "mtime") == 0)
6225 if(strcmp(key
, "name") == 0)
6228 if(mode
!= nullptr){
6229 mode_value
= strtoul(mode
, &endp
, 8);
6231 outs() << "(mode: \"" << mode
<< "\" contains non-octal chars) ";
6232 if(strcmp(type
, "file") == 0)
6233 mode_value
|= S_IFREG
;
6234 PrintModeVerbose(mode_value
);
6238 outs() << format("%10s/", user
);
6239 if(group
!= nullptr)
6240 outs() << format("%-10s ", group
);
6242 outs() << format("%7s ", size
);
6243 if(mtime
!= nullptr){
6244 for(m
= mtime
; *m
!= 'T' && *m
!= '\0'; m
++)
6249 for( ; *m
!= 'Z' && *m
!= '\0'; m
++)
6259 static void DumpBitcodeSection(MachOObjectFile
*O
, const char *sect
,
6260 uint32_t size
, bool verbose
,
6261 bool PrintXarHeader
, bool PrintXarFileHeaders
,
6262 std::string XarMemberName
) {
6263 if(size
< sizeof(struct xar_header
)) {
6264 outs() << "size of (__LLVM,__bundle) section too small (smaller than size "
6265 "of struct xar_header)\n";
6268 struct xar_header XarHeader
;
6269 memcpy(&XarHeader
, sect
, sizeof(struct xar_header
));
6270 if (sys::IsLittleEndianHost
)
6271 swapStruct(XarHeader
);
6272 if (PrintXarHeader
) {
6273 if (!XarMemberName
.empty())
6274 outs() << "In xar member " << XarMemberName
<< ": ";
6276 outs() << "For (__LLVM,__bundle) section: ";
6277 outs() << "xar header\n";
6278 if (XarHeader
.magic
== XAR_HEADER_MAGIC
)
6279 outs() << " magic XAR_HEADER_MAGIC\n";
6282 << format_hex(XarHeader
.magic
, 10, true)
6283 << " (not XAR_HEADER_MAGIC)\n";
6284 outs() << " size " << XarHeader
.size
<< "\n";
6285 outs() << " version " << XarHeader
.version
<< "\n";
6286 outs() << " toc_length_compressed " << XarHeader
.toc_length_compressed
6288 outs() << "toc_length_uncompressed " << XarHeader
.toc_length_uncompressed
6290 outs() << " cksum_alg ";
6291 switch (XarHeader
.cksum_alg
) {
6292 case XAR_CKSUM_NONE
:
6293 outs() << "XAR_CKSUM_NONE\n";
6295 case XAR_CKSUM_SHA1
:
6296 outs() << "XAR_CKSUM_SHA1\n";
6299 outs() << "XAR_CKSUM_MD5\n";
6301 #ifdef XAR_CKSUM_SHA256
6302 case XAR_CKSUM_SHA256
:
6303 outs() << "XAR_CKSUM_SHA256\n";
6306 #ifdef XAR_CKSUM_SHA512
6307 case XAR_CKSUM_SHA512
:
6308 outs() << "XAR_CKSUM_SHA512\n";
6312 outs() << XarHeader
.cksum_alg
<< "\n";
6316 SmallString
<128> XarFilename
;
6318 std::error_code XarEC
=
6319 sys::fs::createTemporaryFile("llvm-objdump", "xar", FD
, XarFilename
);
6321 errs() << XarEC
.message() << "\n";
6324 ToolOutputFile
XarFile(XarFilename
, FD
);
6325 raw_fd_ostream
&XarOut
= XarFile
.os();
6326 StringRef
XarContents(sect
, size
);
6327 XarOut
<< XarContents
;
6329 if (XarOut
.has_error())
6332 ScopedXarFile
xar(XarFilename
.c_str(), READ
);
6334 errs() << "Can't create temporary xar archive " << XarFilename
<< "\n";
6338 SmallString
<128> TocFilename
;
6339 std::error_code TocEC
=
6340 sys::fs::createTemporaryFile("llvm-objdump", "toc", TocFilename
);
6342 errs() << TocEC
.message() << "\n";
6345 xar_serialize(xar
, TocFilename
.c_str());
6347 if (PrintXarFileHeaders
) {
6348 if (!XarMemberName
.empty())
6349 outs() << "In xar member " << XarMemberName
<< ": ";
6351 outs() << "For (__LLVM,__bundle) section: ";
6352 outs() << "xar archive files:\n";
6353 PrintXarFilesSummary(XarFilename
.c_str(), xar
);
6356 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> FileOrErr
=
6357 MemoryBuffer::getFileOrSTDIN(TocFilename
.c_str());
6358 if (std::error_code EC
= FileOrErr
.getError()) {
6359 errs() << EC
.message() << "\n";
6362 std::unique_ptr
<MemoryBuffer
> &Buffer
= FileOrErr
.get();
6364 if (!XarMemberName
.empty())
6365 outs() << "In xar member " << XarMemberName
<< ": ";
6367 outs() << "For (__LLVM,__bundle) section: ";
6368 outs() << "xar table of contents:\n";
6369 outs() << Buffer
->getBuffer() << "\n";
6371 // TODO: Go through the xar's files.
6374 errs() << "Can't obtain an xar iterator for xar archive "
6375 << XarFilename
.c_str() << "\n";
6378 for(xar_file_t xf
= xar_file_first(xar
, xi
); xf
; xf
= xar_file_next(xi
)){
6380 const char *member_name
, *member_type
, *member_size_string
;
6385 errs() << "Can't obtain an xar iterator for xar archive "
6386 << XarFilename
.c_str() << "\n";
6391 member_size_string
= NULL
;
6392 for(key
= xar_prop_first(xf
, xp
); key
; key
= xar_prop_next(xp
)){
6393 const char *val
= nullptr;
6394 xar_prop_get(xf
, key
, &val
);
6395 #if 0 // Useful for debugging.
6396 outs() << "key: " << key
<< " value: " << val
<< "\n";
6398 if (strcmp(key
, "name") == 0)
6400 if (strcmp(key
, "type") == 0)
6402 if (strcmp(key
, "data/size") == 0)
6403 member_size_string
= val
;
6406 * If we find a file with a name, date/size and type properties
6407 * and with the type being "file" see if that is a xar file.
6409 if (member_name
!= NULL
&& member_type
!= NULL
&&
6410 strcmp(member_type
, "file") == 0 &&
6411 member_size_string
!= NULL
){
6412 // Extract the file into a buffer.
6414 member_size
= strtoul(member_size_string
, &endptr
, 10);
6415 if (*endptr
== '\0' && member_size
!= 0) {
6417 if (xar_extract_tobuffersz(xar
, xf
, &buffer
, &member_size
) == 0) {
6418 #if 0 // Useful for debugging.
6419 outs() << "xar member: " << member_name
<< " extracted\n";
6421 // Set the XarMemberName we want to see printed in the header.
6422 std::string OldXarMemberName
;
6423 // If XarMemberName is already set this is nested. So
6424 // save the old name and create the nested name.
6425 if (!XarMemberName
.empty()) {
6426 OldXarMemberName
= XarMemberName
;
6428 (Twine("[") + XarMemberName
+ "]" + member_name
).str();
6430 OldXarMemberName
= "";
6431 XarMemberName
= member_name
;
6433 // See if this is could be a xar file (nested).
6434 if (member_size
>= sizeof(struct xar_header
)) {
6435 #if 0 // Useful for debugging.
6436 outs() << "could be a xar file: " << member_name
<< "\n";
6438 memcpy((char *)&XarHeader
, buffer
, sizeof(struct xar_header
));
6439 if (sys::IsLittleEndianHost
)
6440 swapStruct(XarHeader
);
6441 if (XarHeader
.magic
== XAR_HEADER_MAGIC
)
6442 DumpBitcodeSection(O
, buffer
, member_size
, verbose
,
6443 PrintXarHeader
, PrintXarFileHeaders
,
6446 XarMemberName
= OldXarMemberName
;
6453 #endif // defined(HAVE_LIBXAR)
6455 static void printObjcMetaData(MachOObjectFile
*O
, bool verbose
) {
6457 printObjc2_64bit_MetaData(O
, verbose
);
6459 MachO::mach_header H
;
6461 if (H
.cputype
== MachO::CPU_TYPE_ARM
)
6462 printObjc2_32bit_MetaData(O
, verbose
);
6464 // This is the 32-bit non-arm cputype case. Which is normally
6465 // the first Objective-C ABI. But it may be the case of a
6466 // binary for the iOS simulator which is the second Objective-C
6467 // ABI. In that case printObjc1_32bit_MetaData() will determine that
6468 // and return false.
6469 if (!printObjc1_32bit_MetaData(O
, verbose
))
6470 printObjc2_32bit_MetaData(O
, verbose
);
6475 // GuessLiteralPointer returns a string which for the item in the Mach-O file
6476 // for the address passed in as ReferenceValue for printing as a comment with
6477 // the instruction and also returns the corresponding type of that item
6478 // indirectly through ReferenceType.
6480 // If ReferenceValue is an address of literal cstring then a pointer to the
6481 // cstring is returned and ReferenceType is set to
6482 // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
6484 // If ReferenceValue is an address of an Objective-C CFString, Selector ref or
6485 // Class ref that name is returned and the ReferenceType is set accordingly.
6487 // Lastly, literals which are Symbol address in a literal pool are looked for
6488 // and if found the symbol name is returned and ReferenceType is set to
6489 // LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
6491 // If there is no item in the Mach-O file for the address passed in as
6492 // ReferenceValue nullptr is returned and ReferenceType is unchanged.
6493 static const char *GuessLiteralPointer(uint64_t ReferenceValue
,
6494 uint64_t ReferencePC
,
6495 uint64_t *ReferenceType
,
6496 struct DisassembleInfo
*info
) {
6497 // First see if there is an external relocation entry at the ReferencePC.
6498 if (info
->O
->getHeader().filetype
== MachO::MH_OBJECT
) {
6499 uint64_t sect_addr
= info
->S
.getAddress();
6500 uint64_t sect_offset
= ReferencePC
- sect_addr
;
6501 bool reloc_found
= false;
6503 MachO::any_relocation_info RE
;
6504 bool isExtern
= false;
6506 for (const RelocationRef
&Reloc
: info
->S
.relocations()) {
6507 uint64_t RelocOffset
= Reloc
.getOffset();
6508 if (RelocOffset
== sect_offset
) {
6509 Rel
= Reloc
.getRawDataRefImpl();
6510 RE
= info
->O
->getRelocation(Rel
);
6511 if (info
->O
->isRelocationScattered(RE
))
6513 isExtern
= info
->O
->getPlainRelocationExternal(RE
);
6515 symbol_iterator RelocSym
= Reloc
.getSymbol();
6522 // If there is an external relocation entry for a symbol in a section
6523 // then used that symbol's value for the value of the reference.
6524 if (reloc_found
&& isExtern
) {
6525 if (info
->O
->getAnyRelocationPCRel(RE
)) {
6526 unsigned Type
= info
->O
->getAnyRelocationType(RE
);
6527 if (Type
== MachO::X86_64_RELOC_SIGNED
) {
6528 ReferenceValue
= Symbol
.getValue();
6534 // Look for literals such as Objective-C CFStrings refs, Selector refs,
6535 // Message refs and Class refs.
6536 bool classref
, selref
, msgref
, cfstring
;
6537 uint64_t pointer_value
= GuessPointerPointer(ReferenceValue
, info
, classref
,
6538 selref
, msgref
, cfstring
);
6539 if (classref
&& pointer_value
== 0) {
6540 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
6541 // And the pointer_value in that section is typically zero as it will be
6542 // set by dyld as part of the "bind information".
6543 const char *name
= get_dyld_bind_info_symbolname(ReferenceValue
, info
);
6544 if (name
!= nullptr) {
6545 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref
;
6546 const char *class_name
= strrchr(name
, '$');
6547 if (class_name
!= nullptr && class_name
[1] == '_' &&
6548 class_name
[2] != '\0') {
6549 info
->class_name
= class_name
+ 2;
6556 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref
;
6558 get_objc2_64bit_class_name(pointer_value
, ReferenceValue
, info
);
6559 if (name
!= nullptr)
6560 info
->class_name
= name
;
6562 name
= "bad class ref";
6567 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref
;
6568 const char *name
= get_objc2_64bit_cfstring_name(ReferenceValue
, info
);
6572 if (selref
&& pointer_value
== 0)
6573 pointer_value
= get_objc2_64bit_selref(ReferenceValue
, info
);
6575 if (pointer_value
!= 0)
6576 ReferenceValue
= pointer_value
;
6578 const char *name
= GuessCstringPointer(ReferenceValue
, info
);
6580 if (pointer_value
!= 0 && selref
) {
6581 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref
;
6582 info
->selector_name
= name
;
6583 } else if (pointer_value
!= 0 && msgref
) {
6584 info
->class_name
= nullptr;
6585 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref
;
6586 info
->selector_name
= name
;
6588 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr
;
6592 // Lastly look for an indirect symbol with this ReferenceValue which is in
6593 // a literal pool. If found return that symbol name.
6594 name
= GuessIndirectSymbol(ReferenceValue
, info
);
6596 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr
;
6603 // SymbolizerSymbolLookUp is the symbol lookup function passed when creating
6604 // the Symbolizer. It looks up the ReferenceValue using the info passed via the
6605 // pointer to the struct DisassembleInfo that was passed when MCSymbolizer
6606 // is created and returns the symbol name that matches the ReferenceValue or
6607 // nullptr if none. The ReferenceType is passed in for the IN type of
6608 // reference the instruction is making from the values in defined in the header
6609 // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
6610 // Out type and the ReferenceName will also be set which is added as a comment
6611 // to the disassembled instruction.
6613 // If the symbol name is a C++ mangled name then the demangled name is
6614 // returned through ReferenceName and ReferenceType is set to
6615 // LLVMDisassembler_ReferenceType_DeMangled_Name .
6617 // When this is called to get a symbol name for a branch target then the
6618 // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
6619 // SymbolValue will be looked for in the indirect symbol table to determine if
6620 // it is an address for a symbol stub. If so then the symbol name for that
6621 // stub is returned indirectly through ReferenceName and then ReferenceType is
6622 // set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
6624 // When this is called with an value loaded via a PC relative load then
6625 // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
6626 // SymbolValue is checked to be an address of literal pointer, symbol pointer,
6627 // or an Objective-C meta data reference. If so the output ReferenceType is
6628 // set to correspond to that as well as setting the ReferenceName.
6629 static const char *SymbolizerSymbolLookUp(void *DisInfo
,
6630 uint64_t ReferenceValue
,
6631 uint64_t *ReferenceType
,
6632 uint64_t ReferencePC
,
6633 const char **ReferenceName
) {
6634 struct DisassembleInfo
*info
= (struct DisassembleInfo
*)DisInfo
;
6635 // If no verbose symbolic information is wanted then just return nullptr.
6636 if (!info
->verbose
) {
6637 *ReferenceName
= nullptr;
6638 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
6642 const char *SymbolName
= GuessSymbolName(ReferenceValue
, info
->AddrMap
);
6644 if (*ReferenceType
== LLVMDisassembler_ReferenceType_In_Branch
) {
6645 *ReferenceName
= GuessIndirectSymbol(ReferenceValue
, info
);
6646 if (*ReferenceName
!= nullptr) {
6647 method_reference(info
, ReferenceType
, ReferenceName
);
6648 if (*ReferenceType
!= LLVMDisassembler_ReferenceType_Out_Objc_Message
)
6649 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_SymbolStub
;
6650 } else if (SymbolName
!= nullptr && strncmp(SymbolName
, "__Z", 3) == 0) {
6651 if (info
->demangled_name
!= nullptr)
6652 free(info
->demangled_name
);
6654 info
->demangled_name
=
6655 itaniumDemangle(SymbolName
+ 1, nullptr, nullptr, &status
);
6656 if (info
->demangled_name
!= nullptr) {
6657 *ReferenceName
= info
->demangled_name
;
6658 *ReferenceType
= LLVMDisassembler_ReferenceType_DeMangled_Name
;
6660 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
6662 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
6663 } else if (*ReferenceType
== LLVMDisassembler_ReferenceType_In_PCrel_Load
) {
6665 GuessLiteralPointer(ReferenceValue
, ReferencePC
, ReferenceType
, info
);
6667 method_reference(info
, ReferenceType
, ReferenceName
);
6669 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
6670 // If this is arm64 and the reference is an adrp instruction save the
6671 // instruction, passed in ReferenceValue and the address of the instruction
6672 // for use later if we see and add immediate instruction.
6673 } else if (info
->O
->getArch() == Triple::aarch64
&&
6674 *ReferenceType
== LLVMDisassembler_ReferenceType_In_ARM64_ADRP
) {
6675 info
->adrp_inst
= ReferenceValue
;
6676 info
->adrp_addr
= ReferencePC
;
6677 SymbolName
= nullptr;
6678 *ReferenceName
= nullptr;
6679 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
6680 // If this is arm64 and reference is an add immediate instruction and we
6682 // seen an adrp instruction just before it and the adrp's Xd register
6684 // this add's Xn register reconstruct the value being referenced and look to
6685 // see if it is a literal pointer. Note the add immediate instruction is
6686 // passed in ReferenceValue.
6687 } else if (info
->O
->getArch() == Triple::aarch64
&&
6688 *ReferenceType
== LLVMDisassembler_ReferenceType_In_ARM64_ADDXri
&&
6689 ReferencePC
- 4 == info
->adrp_addr
&&
6690 (info
->adrp_inst
& 0x9f000000) == 0x90000000 &&
6691 (info
->adrp_inst
& 0x1f) == ((ReferenceValue
>> 5) & 0x1f)) {
6692 uint32_t addxri_inst
;
6693 uint64_t adrp_imm
, addxri_imm
;
6696 ((info
->adrp_inst
& 0x00ffffe0) >> 3) | ((info
->adrp_inst
>> 29) & 0x3);
6697 if (info
->adrp_inst
& 0x0200000)
6698 adrp_imm
|= 0xfffffffffc000000LL
;
6700 addxri_inst
= ReferenceValue
;
6701 addxri_imm
= (addxri_inst
>> 10) & 0xfff;
6702 if (((addxri_inst
>> 22) & 0x3) == 1)
6705 ReferenceValue
= (info
->adrp_addr
& 0xfffffffffffff000LL
) +
6706 (adrp_imm
<< 12) + addxri_imm
;
6709 GuessLiteralPointer(ReferenceValue
, ReferencePC
, ReferenceType
, info
);
6710 if (*ReferenceName
== nullptr)
6711 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
6712 // If this is arm64 and the reference is a load register instruction and we
6713 // have seen an adrp instruction just before it and the adrp's Xd register
6714 // matches this add's Xn register reconstruct the value being referenced and
6715 // look to see if it is a literal pointer. Note the load register
6716 // instruction is passed in ReferenceValue.
6717 } else if (info
->O
->getArch() == Triple::aarch64
&&
6718 *ReferenceType
== LLVMDisassembler_ReferenceType_In_ARM64_LDRXui
&&
6719 ReferencePC
- 4 == info
->adrp_addr
&&
6720 (info
->adrp_inst
& 0x9f000000) == 0x90000000 &&
6721 (info
->adrp_inst
& 0x1f) == ((ReferenceValue
>> 5) & 0x1f)) {
6722 uint32_t ldrxui_inst
;
6723 uint64_t adrp_imm
, ldrxui_imm
;
6726 ((info
->adrp_inst
& 0x00ffffe0) >> 3) | ((info
->adrp_inst
>> 29) & 0x3);
6727 if (info
->adrp_inst
& 0x0200000)
6728 adrp_imm
|= 0xfffffffffc000000LL
;
6730 ldrxui_inst
= ReferenceValue
;
6731 ldrxui_imm
= (ldrxui_inst
>> 10) & 0xfff;
6733 ReferenceValue
= (info
->adrp_addr
& 0xfffffffffffff000LL
) +
6734 (adrp_imm
<< 12) + (ldrxui_imm
<< 3);
6737 GuessLiteralPointer(ReferenceValue
, ReferencePC
, ReferenceType
, info
);
6738 if (*ReferenceName
== nullptr)
6739 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
6741 // If this arm64 and is an load register (PC-relative) instruction the
6742 // ReferenceValue is the PC plus the immediate value.
6743 else if (info
->O
->getArch() == Triple::aarch64
&&
6744 (*ReferenceType
== LLVMDisassembler_ReferenceType_In_ARM64_LDRXl
||
6745 *ReferenceType
== LLVMDisassembler_ReferenceType_In_ARM64_ADR
)) {
6747 GuessLiteralPointer(ReferenceValue
, ReferencePC
, ReferenceType
, info
);
6748 if (*ReferenceName
== nullptr)
6749 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
6750 } else if (SymbolName
!= nullptr && strncmp(SymbolName
, "__Z", 3) == 0) {
6751 if (info
->demangled_name
!= nullptr)
6752 free(info
->demangled_name
);
6754 info
->demangled_name
=
6755 itaniumDemangle(SymbolName
+ 1, nullptr, nullptr, &status
);
6756 if (info
->demangled_name
!= nullptr) {
6757 *ReferenceName
= info
->demangled_name
;
6758 *ReferenceType
= LLVMDisassembler_ReferenceType_DeMangled_Name
;
6762 *ReferenceName
= nullptr;
6763 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
6769 /// Emits the comments that are stored in the CommentStream.
6770 /// Each comment in the CommentStream must end with a newline.
6771 static void emitComments(raw_svector_ostream
&CommentStream
,
6772 SmallString
<128> &CommentsToEmit
,
6773 formatted_raw_ostream
&FormattedOS
,
6774 const MCAsmInfo
&MAI
) {
6775 // Flush the stream before taking its content.
6776 StringRef Comments
= CommentsToEmit
.str();
6777 // Get the default information for printing a comment.
6778 StringRef CommentBegin
= MAI
.getCommentString();
6779 unsigned CommentColumn
= MAI
.getCommentColumn();
6780 bool IsFirst
= true;
6781 while (!Comments
.empty()) {
6783 FormattedOS
<< '\n';
6784 // Emit a line of comments.
6785 FormattedOS
.PadToColumn(CommentColumn
);
6786 size_t Position
= Comments
.find('\n');
6787 FormattedOS
<< CommentBegin
<< ' ' << Comments
.substr(0, Position
);
6788 // Move after the newline character.
6789 Comments
= Comments
.substr(Position
+ 1);
6792 FormattedOS
.flush();
6794 // Tell the comment stream that the vector changed underneath it.
6795 CommentsToEmit
.clear();
6798 static void DisassembleMachO(StringRef Filename
, MachOObjectFile
*MachOOF
,
6799 StringRef DisSegName
, StringRef DisSectName
) {
6800 const char *McpuDefault
= nullptr;
6801 const Target
*ThumbTarget
= nullptr;
6802 const Target
*TheTarget
= GetTarget(MachOOF
, &McpuDefault
, &ThumbTarget
);
6804 // GetTarget prints out stuff.
6807 std::string MachOMCPU
;
6808 if (MCPU
.empty() && McpuDefault
)
6809 MachOMCPU
= McpuDefault
;
6813 std::unique_ptr
<const MCInstrInfo
> InstrInfo(TheTarget
->createMCInstrInfo());
6814 std::unique_ptr
<const MCInstrInfo
> ThumbInstrInfo
;
6816 ThumbInstrInfo
.reset(ThumbTarget
->createMCInstrInfo());
6818 // Package up features to be passed to target/subtarget
6819 std::string FeaturesStr
;
6820 if (MAttrs
.size()) {
6821 SubtargetFeatures Features
;
6822 for (unsigned i
= 0; i
!= MAttrs
.size(); ++i
)
6823 Features
.AddFeature(MAttrs
[i
]);
6824 FeaturesStr
= Features
.getString();
6827 // Set up disassembler.
6828 std::unique_ptr
<const MCRegisterInfo
> MRI(
6829 TheTarget
->createMCRegInfo(TripleName
));
6830 std::unique_ptr
<const MCAsmInfo
> AsmInfo(
6831 TheTarget
->createMCAsmInfo(*MRI
, TripleName
));
6832 std::unique_ptr
<const MCSubtargetInfo
> STI(
6833 TheTarget
->createMCSubtargetInfo(TripleName
, MachOMCPU
, FeaturesStr
));
6834 MCContext
Ctx(AsmInfo
.get(), MRI
.get(), nullptr);
6835 std::unique_ptr
<MCDisassembler
> DisAsm(
6836 TheTarget
->createMCDisassembler(*STI
, Ctx
));
6837 std::unique_ptr
<MCSymbolizer
> Symbolizer
;
6838 struct DisassembleInfo
SymbolizerInfo(nullptr, nullptr, nullptr, false);
6839 std::unique_ptr
<MCRelocationInfo
> RelInfo(
6840 TheTarget
->createMCRelocationInfo(TripleName
, Ctx
));
6842 Symbolizer
.reset(TheTarget
->createMCSymbolizer(
6843 TripleName
, SymbolizerGetOpInfo
, SymbolizerSymbolLookUp
,
6844 &SymbolizerInfo
, &Ctx
, std::move(RelInfo
)));
6845 DisAsm
->setSymbolizer(std::move(Symbolizer
));
6847 int AsmPrinterVariant
= AsmInfo
->getAssemblerDialect();
6848 std::unique_ptr
<MCInstPrinter
> IP(TheTarget
->createMCInstPrinter(
6849 Triple(TripleName
), AsmPrinterVariant
, *AsmInfo
, *InstrInfo
, *MRI
));
6850 // Set the display preference for hex vs. decimal immediates.
6851 IP
->setPrintImmHex(PrintImmHex
);
6852 // Comment stream and backing vector.
6853 SmallString
<128> CommentsToEmit
;
6854 raw_svector_ostream
CommentStream(CommentsToEmit
);
6855 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
6856 // if it is done then arm64 comments for string literals don't get printed
6857 // and some constant get printed instead and not setting it causes intel
6858 // (32-bit and 64-bit) comments printed with different spacing before the
6859 // comment causing different diffs with the 'C' disassembler library API.
6860 // IP->setCommentStream(CommentStream);
6862 if (!AsmInfo
|| !STI
|| !DisAsm
|| !IP
) {
6863 errs() << "error: couldn't initialize disassembler for target "
6864 << TripleName
<< '\n';
6868 // Set up separate thumb disassembler if needed.
6869 std::unique_ptr
<const MCRegisterInfo
> ThumbMRI
;
6870 std::unique_ptr
<const MCAsmInfo
> ThumbAsmInfo
;
6871 std::unique_ptr
<const MCSubtargetInfo
> ThumbSTI
;
6872 std::unique_ptr
<MCDisassembler
> ThumbDisAsm
;
6873 std::unique_ptr
<MCInstPrinter
> ThumbIP
;
6874 std::unique_ptr
<MCContext
> ThumbCtx
;
6875 std::unique_ptr
<MCSymbolizer
> ThumbSymbolizer
;
6876 struct DisassembleInfo
ThumbSymbolizerInfo(nullptr, nullptr, nullptr, false);
6877 std::unique_ptr
<MCRelocationInfo
> ThumbRelInfo
;
6879 ThumbMRI
.reset(ThumbTarget
->createMCRegInfo(ThumbTripleName
));
6881 ThumbTarget
->createMCAsmInfo(*ThumbMRI
, ThumbTripleName
));
6883 ThumbTarget
->createMCSubtargetInfo(ThumbTripleName
, MachOMCPU
,
6885 ThumbCtx
.reset(new MCContext(ThumbAsmInfo
.get(), ThumbMRI
.get(), nullptr));
6886 ThumbDisAsm
.reset(ThumbTarget
->createMCDisassembler(*ThumbSTI
, *ThumbCtx
));
6887 MCContext
*PtrThumbCtx
= ThumbCtx
.get();
6889 ThumbTarget
->createMCRelocationInfo(ThumbTripleName
, *PtrThumbCtx
));
6891 ThumbSymbolizer
.reset(ThumbTarget
->createMCSymbolizer(
6892 ThumbTripleName
, SymbolizerGetOpInfo
, SymbolizerSymbolLookUp
,
6893 &ThumbSymbolizerInfo
, PtrThumbCtx
, std::move(ThumbRelInfo
)));
6894 ThumbDisAsm
->setSymbolizer(std::move(ThumbSymbolizer
));
6896 int ThumbAsmPrinterVariant
= ThumbAsmInfo
->getAssemblerDialect();
6897 ThumbIP
.reset(ThumbTarget
->createMCInstPrinter(
6898 Triple(ThumbTripleName
), ThumbAsmPrinterVariant
, *ThumbAsmInfo
,
6899 *ThumbInstrInfo
, *ThumbMRI
));
6900 // Set the display preference for hex vs. decimal immediates.
6901 ThumbIP
->setPrintImmHex(PrintImmHex
);
6904 if (ThumbTarget
&& (!ThumbAsmInfo
|| !ThumbSTI
|| !ThumbDisAsm
|| !ThumbIP
)) {
6905 errs() << "error: couldn't initialize disassembler for target "
6906 << ThumbTripleName
<< '\n';
6910 MachO::mach_header Header
= MachOOF
->getHeader();
6912 // FIXME: Using the -cfg command line option, this code used to be able to
6913 // annotate relocations with the referenced symbol's name, and if this was
6914 // inside a __[cf]string section, the data it points to. This is now replaced
6915 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
6916 std::vector
<SectionRef
> Sections
;
6917 std::vector
<SymbolRef
> Symbols
;
6918 SmallVector
<uint64_t, 8> FoundFns
;
6919 uint64_t BaseSegmentAddress
;
6921 getSectionsAndSymbols(MachOOF
, Sections
, Symbols
, FoundFns
,
6922 BaseSegmentAddress
);
6924 // Sort the symbols by address, just in case they didn't come in that way.
6925 llvm::sort(Symbols
.begin(), Symbols
.end(), SymbolSorter());
6927 // Build a data in code table that is sorted on by the address of each entry.
6928 uint64_t BaseAddress
= 0;
6929 if (Header
.filetype
== MachO::MH_OBJECT
)
6930 BaseAddress
= Sections
[0].getAddress();
6932 BaseAddress
= BaseSegmentAddress
;
6934 for (dice_iterator DI
= MachOOF
->begin_dices(), DE
= MachOOF
->end_dices();
6937 DI
->getOffset(Offset
);
6938 Dices
.push_back(std::make_pair(BaseAddress
+ Offset
, *DI
));
6940 array_pod_sort(Dices
.begin(), Dices
.end());
6943 raw_ostream
&DebugOut
= DebugFlag
? dbgs() : nulls();
6945 raw_ostream
&DebugOut
= nulls();
6948 std::unique_ptr
<DIContext
> diContext
;
6949 ObjectFile
*DbgObj
= MachOOF
;
6950 std::unique_ptr
<MemoryBuffer
> DSYMBuf
;
6951 // Try to find debug info and set up the DIContext for it.
6953 // A separate DSym file path was specified, parse it as a macho file,
6954 // get the sections and supply it to the section name parsing machinery.
6955 if (!DSYMFile
.empty()) {
6956 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> BufOrErr
=
6957 MemoryBuffer::getFileOrSTDIN(DSYMFile
);
6958 if (std::error_code EC
= BufOrErr
.getError()) {
6959 errs() << "llvm-objdump: " << Filename
<< ": " << EC
.message() << '\n';
6962 Expected
<std::unique_ptr
<MachOObjectFile
>> DbgObjCheck
=
6963 ObjectFile::createMachOObjectFile(BufOrErr
.get()->getMemBufferRef());
6965 if (DbgObjCheck
.takeError())
6966 report_error(MachOOF
->getFileName(), DbgObjCheck
.takeError());
6967 DbgObj
= DbgObjCheck
.get().release();
6968 // We need to keep the file alive, because we're replacing DbgObj with it.
6969 DSYMBuf
= std::move(BufOrErr
.get());
6972 // Setup the DIContext
6973 diContext
= DWARFContext::create(*DbgObj
);
6976 if (FilterSections
.size() == 0)
6977 outs() << "(" << DisSegName
<< "," << DisSectName
<< ") section\n";
6979 for (unsigned SectIdx
= 0; SectIdx
!= Sections
.size(); SectIdx
++) {
6981 if (Sections
[SectIdx
].getName(SectName
) || SectName
!= DisSectName
)
6984 DataRefImpl DR
= Sections
[SectIdx
].getRawDataRefImpl();
6986 StringRef SegmentName
= MachOOF
->getSectionFinalSegmentName(DR
);
6987 if (SegmentName
!= DisSegName
)
6991 Sections
[SectIdx
].getContents(BytesStr
);
6992 ArrayRef
<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr
.data()),
6994 uint64_t SectAddress
= Sections
[SectIdx
].getAddress();
6996 bool symbolTableWorked
= false;
6998 // Create a map of symbol addresses to symbol names for use by
6999 // the SymbolizerSymbolLookUp() routine.
7000 SymbolAddressMap AddrMap
;
7001 bool DisSymNameFound
= false;
7002 for (const SymbolRef
&Symbol
: MachOOF
->symbols()) {
7003 Expected
<SymbolRef::Type
> STOrErr
= Symbol
.getType();
7005 report_error(MachOOF
->getFileName(), STOrErr
.takeError());
7006 SymbolRef::Type ST
= *STOrErr
;
7007 if (ST
== SymbolRef::ST_Function
|| ST
== SymbolRef::ST_Data
||
7008 ST
== SymbolRef::ST_Other
) {
7009 uint64_t Address
= Symbol
.getValue();
7010 Expected
<StringRef
> SymNameOrErr
= Symbol
.getName();
7012 report_error(MachOOF
->getFileName(), SymNameOrErr
.takeError());
7013 StringRef SymName
= *SymNameOrErr
;
7014 AddrMap
[Address
] = SymName
;
7015 if (!DisSymName
.empty() && DisSymName
== SymName
)
7016 DisSymNameFound
= true;
7019 if (!DisSymName
.empty() && !DisSymNameFound
) {
7020 outs() << "Can't find -dis-symname: " << DisSymName
<< "\n";
7023 // Set up the block of info used by the Symbolizer call backs.
7024 SymbolizerInfo
.verbose
= !NoSymbolicOperands
;
7025 SymbolizerInfo
.O
= MachOOF
;
7026 SymbolizerInfo
.S
= Sections
[SectIdx
];
7027 SymbolizerInfo
.AddrMap
= &AddrMap
;
7028 SymbolizerInfo
.Sections
= &Sections
;
7029 // Same for the ThumbSymbolizer
7030 ThumbSymbolizerInfo
.verbose
= !NoSymbolicOperands
;
7031 ThumbSymbolizerInfo
.O
= MachOOF
;
7032 ThumbSymbolizerInfo
.S
= Sections
[SectIdx
];
7033 ThumbSymbolizerInfo
.AddrMap
= &AddrMap
;
7034 ThumbSymbolizerInfo
.Sections
= &Sections
;
7036 unsigned int Arch
= MachOOF
->getArch();
7038 // Skip all symbols if this is a stubs file.
7039 if (Bytes
.size() == 0)
7042 // If the section has symbols but no symbol at the start of the section
7043 // these are used to make sure the bytes before the first symbol are
7045 bool FirstSymbol
= true;
7046 bool FirstSymbolAtSectionStart
= true;
7048 // Disassemble symbol by symbol.
7049 for (unsigned SymIdx
= 0; SymIdx
!= Symbols
.size(); SymIdx
++) {
7050 Expected
<StringRef
> SymNameOrErr
= Symbols
[SymIdx
].getName();
7052 report_error(MachOOF
->getFileName(), SymNameOrErr
.takeError());
7053 StringRef SymName
= *SymNameOrErr
;
7055 Expected
<SymbolRef::Type
> STOrErr
= Symbols
[SymIdx
].getType();
7057 report_error(MachOOF
->getFileName(), STOrErr
.takeError());
7058 SymbolRef::Type ST
= *STOrErr
;
7059 if (ST
!= SymbolRef::ST_Function
&& ST
!= SymbolRef::ST_Data
)
7062 // Make sure the symbol is defined in this section.
7063 bool containsSym
= Sections
[SectIdx
].containsSymbol(Symbols
[SymIdx
]);
7065 if (!DisSymName
.empty() && DisSymName
== SymName
) {
7066 outs() << "-dis-symname: " << DisSymName
<< " not in the section\n";
7071 // The __mh_execute_header is special and we need to deal with that fact
7072 // this symbol is before the start of the (__TEXT,__text) section and at the
7073 // address of the start of the __TEXT segment. This is because this symbol
7074 // is an N_SECT symbol in the (__TEXT,__text) but its address is before the
7075 // start of the section in a standard MH_EXECUTE filetype.
7076 if (!DisSymName
.empty() && DisSymName
== "__mh_execute_header") {
7077 outs() << "-dis-symname: __mh_execute_header not in any section\n";
7080 // When this code is trying to disassemble a symbol at a time and in the
7081 // case there is only the __mh_execute_header symbol left as in a stripped
7082 // executable, we need to deal with this by ignoring this symbol so the
7083 // whole section is disassembled and this symbol is then not displayed.
7084 if (SymName
== "__mh_execute_header" || SymName
== "__mh_dylib_header" ||
7085 SymName
== "__mh_bundle_header" || SymName
== "__mh_object_header" ||
7086 SymName
== "__mh_preload_header" || SymName
== "__mh_dylinker_header")
7089 // If we are only disassembling one symbol see if this is that symbol.
7090 if (!DisSymName
.empty() && DisSymName
!= SymName
)
7093 // Start at the address of the symbol relative to the section's address.
7094 uint64_t SectSize
= Sections
[SectIdx
].getSize();
7095 uint64_t Start
= Symbols
[SymIdx
].getValue();
7096 uint64_t SectionAddress
= Sections
[SectIdx
].getAddress();
7097 Start
-= SectionAddress
;
7099 if (Start
> SectSize
) {
7100 outs() << "section data ends, " << SymName
7101 << " lies outside valid range\n";
7105 // Stop disassembling either at the beginning of the next symbol or at
7106 // the end of the section.
7107 bool containsNextSym
= false;
7108 uint64_t NextSym
= 0;
7109 uint64_t NextSymIdx
= SymIdx
+ 1;
7110 while (Symbols
.size() > NextSymIdx
) {
7111 Expected
<SymbolRef::Type
> STOrErr
= Symbols
[NextSymIdx
].getType();
7113 report_error(MachOOF
->getFileName(), STOrErr
.takeError());
7114 SymbolRef::Type NextSymType
= *STOrErr
;
7115 if (NextSymType
== SymbolRef::ST_Function
) {
7117 Sections
[SectIdx
].containsSymbol(Symbols
[NextSymIdx
]);
7118 NextSym
= Symbols
[NextSymIdx
].getValue();
7119 NextSym
-= SectionAddress
;
7125 uint64_t End
= containsNextSym
? std::min(NextSym
, SectSize
) : SectSize
;
7128 symbolTableWorked
= true;
7130 DataRefImpl Symb
= Symbols
[SymIdx
].getRawDataRefImpl();
7131 bool IsThumb
= MachOOF
->getSymbolFlags(Symb
) & SymbolRef::SF_Thumb
;
7133 // We only need the dedicated Thumb target if there's a real choice
7134 // (i.e. we're not targeting M-class) and the function is Thumb.
7135 bool UseThumbTarget
= IsThumb
&& ThumbTarget
;
7137 // If we are not specifying a symbol to start disassembly with and this
7138 // is the first symbol in the section but not at the start of the section
7139 // then move the disassembly index to the start of the section and
7140 // don't print the symbol name just yet. This is so the bytes before the
7141 // first symbol are disassembled.
7142 uint64_t SymbolStart
= Start
;
7143 if (DisSymName
.empty() && FirstSymbol
&& Start
!= 0) {
7144 FirstSymbolAtSectionStart
= false;
7148 outs() << SymName
<< ":\n";
7150 DILineInfo lastLine
;
7151 for (uint64_t Index
= Start
; Index
< End
; Index
+= Size
) {
7154 // If this is the first symbol in the section and it was not at the
7155 // start of the section, see if we are at its Index now and if so print
7157 if (FirstSymbol
&& !FirstSymbolAtSectionStart
&& Index
== SymbolStart
)
7158 outs() << SymName
<< ":\n";
7160 uint64_t PC
= SectAddress
+ Index
;
7161 if (!NoLeadingAddr
) {
7162 if (FullLeadingAddr
) {
7163 if (MachOOF
->is64Bit())
7164 outs() << format("%016" PRIx64
, PC
);
7166 outs() << format("%08" PRIx64
, PC
);
7168 outs() << format("%8" PRIx64
":", PC
);
7171 if (!NoShowRawInsn
|| Arch
== Triple::arm
)
7174 // Check the data in code table here to see if this is data not an
7175 // instruction to be disassembled.
7177 Dice
.push_back(std::make_pair(PC
, DiceRef()));
7178 dice_table_iterator DTI
=
7179 std::search(Dices
.begin(), Dices
.end(), Dice
.begin(), Dice
.end(),
7180 compareDiceTableEntries
);
7181 if (DTI
!= Dices
.end()) {
7183 DTI
->second
.getLength(Length
);
7185 DTI
->second
.getKind(Kind
);
7186 Size
= DumpDataInCode(Bytes
.data() + Index
, Length
, Kind
);
7187 if ((Kind
== MachO::DICE_KIND_JUMP_TABLE8
) &&
7188 (PC
== (DTI
->first
+ Length
- 1)) && (Length
& 1))
7193 SmallVector
<char, 64> AnnotationsBytes
;
7194 raw_svector_ostream
Annotations(AnnotationsBytes
);
7198 gotInst
= ThumbDisAsm
->getInstruction(Inst
, Size
, Bytes
.slice(Index
),
7199 PC
, DebugOut
, Annotations
);
7201 gotInst
= DisAsm
->getInstruction(Inst
, Size
, Bytes
.slice(Index
), PC
,
7202 DebugOut
, Annotations
);
7204 if (!NoShowRawInsn
|| Arch
== Triple::arm
) {
7205 dumpBytes(makeArrayRef(Bytes
.data() + Index
, Size
), outs());
7207 formatted_raw_ostream
FormattedOS(outs());
7208 StringRef AnnotationsStr
= Annotations
.str();
7210 ThumbIP
->printInst(&Inst
, FormattedOS
, AnnotationsStr
, *ThumbSTI
);
7212 IP
->printInst(&Inst
, FormattedOS
, AnnotationsStr
, *STI
);
7213 emitComments(CommentStream
, CommentsToEmit
, FormattedOS
, *AsmInfo
);
7215 // Print debug info.
7217 DILineInfo dli
= diContext
->getLineInfoForAddress(PC
);
7218 // Print valid line info if it changed.
7219 if (dli
!= lastLine
&& dli
.Line
!= 0)
7220 outs() << "\t## " << dli
.FileName
<< ':' << dli
.Line
<< ':'
7226 unsigned int Arch
= MachOOF
->getArch();
7227 if (Arch
== Triple::x86_64
|| Arch
== Triple::x86
) {
7228 outs() << format("\t.byte 0x%02x #bad opcode\n",
7229 *(Bytes
.data() + Index
) & 0xff);
7230 Size
= 1; // skip exactly one illegible byte and move on.
7231 } else if (Arch
== Triple::aarch64
||
7232 (Arch
== Triple::arm
&& !IsThumb
)) {
7233 uint32_t opcode
= (*(Bytes
.data() + Index
) & 0xff) |
7234 (*(Bytes
.data() + Index
+ 1) & 0xff) << 8 |
7235 (*(Bytes
.data() + Index
+ 2) & 0xff) << 16 |
7236 (*(Bytes
.data() + Index
+ 3) & 0xff) << 24;
7237 outs() << format("\t.long\t0x%08x\n", opcode
);
7239 } else if (Arch
== Triple::arm
) {
7240 assert(IsThumb
&& "ARM mode should have been dealt with above");
7241 uint32_t opcode
= (*(Bytes
.data() + Index
) & 0xff) |
7242 (*(Bytes
.data() + Index
+ 1) & 0xff) << 8;
7243 outs() << format("\t.short\t0x%04x\n", opcode
);
7246 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
7248 Size
= 1; // skip illegible bytes
7252 // Now that we are done disassembled the first symbol set the bool that
7253 // were doing this to false.
7254 FirstSymbol
= false;
7256 if (!symbolTableWorked
) {
7257 // Reading the symbol table didn't work, disassemble the whole section.
7258 uint64_t SectAddress
= Sections
[SectIdx
].getAddress();
7259 uint64_t SectSize
= Sections
[SectIdx
].getSize();
7261 for (uint64_t Index
= 0; Index
< SectSize
; Index
+= InstSize
) {
7264 uint64_t PC
= SectAddress
+ Index
;
7265 SmallVector
<char, 64> AnnotationsBytes
;
7266 raw_svector_ostream
Annotations(AnnotationsBytes
);
7267 if (DisAsm
->getInstruction(Inst
, InstSize
, Bytes
.slice(Index
), PC
,
7268 DebugOut
, Annotations
)) {
7269 if (!NoLeadingAddr
) {
7270 if (FullLeadingAddr
) {
7271 if (MachOOF
->is64Bit())
7272 outs() << format("%016" PRIx64
, PC
);
7274 outs() << format("%08" PRIx64
, PC
);
7276 outs() << format("%8" PRIx64
":", PC
);
7279 if (!NoShowRawInsn
|| Arch
== Triple::arm
) {
7281 dumpBytes(makeArrayRef(Bytes
.data() + Index
, InstSize
), outs());
7283 StringRef AnnotationsStr
= Annotations
.str();
7284 IP
->printInst(&Inst
, outs(), AnnotationsStr
, *STI
);
7287 unsigned int Arch
= MachOOF
->getArch();
7288 if (Arch
== Triple::x86_64
|| Arch
== Triple::x86
) {
7289 outs() << format("\t.byte 0x%02x #bad opcode\n",
7290 *(Bytes
.data() + Index
) & 0xff);
7291 InstSize
= 1; // skip exactly one illegible byte and move on.
7293 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
7295 InstSize
= 1; // skip illegible bytes
7300 // The TripleName's need to be reset if we are called again for a different
7303 ThumbTripleName
= "";
7305 if (SymbolizerInfo
.demangled_name
!= nullptr)
7306 free(SymbolizerInfo
.demangled_name
);
7307 if (ThumbSymbolizerInfo
.demangled_name
!= nullptr)
7308 free(ThumbSymbolizerInfo
.demangled_name
);
7312 //===----------------------------------------------------------------------===//
7313 // __compact_unwind section dumping
7314 //===----------------------------------------------------------------------===//
7318 template <typename T
>
7319 static uint64_t read(StringRef Contents
, ptrdiff_t Offset
) {
7320 using llvm::support::little
;
7321 using llvm::support::unaligned
;
7323 if (Offset
+ sizeof(T
) > Contents
.size()) {
7324 outs() << "warning: attempt to read past end of buffer\n";
7329 support::endian::read
<T
, little
, unaligned
>(Contents
.data() + Offset
);
7333 template <typename T
>
7334 static uint64_t readNext(StringRef Contents
, ptrdiff_t &Offset
) {
7335 T Val
= read
<T
>(Contents
, Offset
);
7336 Offset
+= sizeof(T
);
7340 struct CompactUnwindEntry
{
7341 uint32_t OffsetInSection
;
7343 uint64_t FunctionAddr
;
7345 uint32_t CompactEncoding
;
7346 uint64_t PersonalityAddr
;
7349 RelocationRef FunctionReloc
;
7350 RelocationRef PersonalityReloc
;
7351 RelocationRef LSDAReloc
;
7353 CompactUnwindEntry(StringRef Contents
, unsigned Offset
, bool Is64
)
7354 : OffsetInSection(Offset
) {
7356 read
<uint64_t>(Contents
, Offset
);
7358 read
<uint32_t>(Contents
, Offset
);
7362 template <typename UIntPtr
> void read(StringRef Contents
, ptrdiff_t Offset
) {
7363 FunctionAddr
= readNext
<UIntPtr
>(Contents
, Offset
);
7364 Length
= readNext
<uint32_t>(Contents
, Offset
);
7365 CompactEncoding
= readNext
<uint32_t>(Contents
, Offset
);
7366 PersonalityAddr
= readNext
<UIntPtr
>(Contents
, Offset
);
7367 LSDAAddr
= readNext
<UIntPtr
>(Contents
, Offset
);
7372 /// Given a relocation from __compact_unwind, consisting of the RelocationRef
7373 /// and data being relocated, determine the best base Name and Addend to use for
7374 /// display purposes.
7376 /// 1. An Extern relocation will directly reference a symbol (and the data is
7377 /// then already an addend), so use that.
7378 /// 2. Otherwise the data is an offset in the object file's layout; try to find
7379 // a symbol before it in the same section, and use the offset from there.
7380 /// 3. Finally, if all that fails, fall back to an offset from the start of the
7381 /// referenced section.
7382 static void findUnwindRelocNameAddend(const MachOObjectFile
*Obj
,
7383 std::map
<uint64_t, SymbolRef
> &Symbols
,
7384 const RelocationRef
&Reloc
, uint64_t Addr
,
7385 StringRef
&Name
, uint64_t &Addend
) {
7386 if (Reloc
.getSymbol() != Obj
->symbol_end()) {
7387 Expected
<StringRef
> NameOrErr
= Reloc
.getSymbol()->getName();
7389 report_error(Obj
->getFileName(), NameOrErr
.takeError());
7395 auto RE
= Obj
->getRelocation(Reloc
.getRawDataRefImpl());
7396 SectionRef RelocSection
= Obj
->getAnyRelocationSection(RE
);
7398 uint64_t SectionAddr
= RelocSection
.getAddress();
7400 auto Sym
= Symbols
.upper_bound(Addr
);
7401 if (Sym
== Symbols
.begin()) {
7402 // The first symbol in the object is after this reference, the best we can
7403 // do is section-relative notation.
7404 RelocSection
.getName(Name
);
7405 Addend
= Addr
- SectionAddr
;
7409 // Go back one so that SymbolAddress <= Addr.
7412 auto SectOrErr
= Sym
->second
.getSection();
7414 report_error(Obj
->getFileName(), SectOrErr
.takeError());
7415 section_iterator SymSection
= *SectOrErr
;
7416 if (RelocSection
== *SymSection
) {
7417 // There's a valid symbol in the same section before this reference.
7418 Expected
<StringRef
> NameOrErr
= Sym
->second
.getName();
7420 report_error(Obj
->getFileName(), NameOrErr
.takeError());
7422 Addend
= Addr
- Sym
->first
;
7426 // There is a symbol before this reference, but it's in a different
7427 // section. Probably not helpful to mention it, so use the section name.
7428 RelocSection
.getName(Name
);
7429 Addend
= Addr
- SectionAddr
;
7432 static void printUnwindRelocDest(const MachOObjectFile
*Obj
,
7433 std::map
<uint64_t, SymbolRef
> &Symbols
,
7434 const RelocationRef
&Reloc
, uint64_t Addr
) {
7438 if (!Reloc
.getObject())
7441 findUnwindRelocNameAddend(Obj
, Symbols
, Reloc
, Addr
, Name
, Addend
);
7445 outs() << " + " << format("0x%" PRIx64
, Addend
);
7449 printMachOCompactUnwindSection(const MachOObjectFile
*Obj
,
7450 std::map
<uint64_t, SymbolRef
> &Symbols
,
7451 const SectionRef
&CompactUnwind
) {
7453 if (!Obj
->isLittleEndian()) {
7454 outs() << "Skipping big-endian __compact_unwind section\n";
7458 bool Is64
= Obj
->is64Bit();
7459 uint32_t PointerSize
= Is64
? sizeof(uint64_t) : sizeof(uint32_t);
7460 uint32_t EntrySize
= 3 * PointerSize
+ 2 * sizeof(uint32_t);
7463 CompactUnwind
.getContents(Contents
);
7465 SmallVector
<CompactUnwindEntry
, 4> CompactUnwinds
;
7467 // First populate the initial raw offsets, encodings and so on from the entry.
7468 for (unsigned Offset
= 0; Offset
< Contents
.size(); Offset
+= EntrySize
) {
7469 CompactUnwindEntry
Entry(Contents
, Offset
, Is64
);
7470 CompactUnwinds
.push_back(Entry
);
7473 // Next we need to look at the relocations to find out what objects are
7474 // actually being referred to.
7475 for (const RelocationRef
&Reloc
: CompactUnwind
.relocations()) {
7476 uint64_t RelocAddress
= Reloc
.getOffset();
7478 uint32_t EntryIdx
= RelocAddress
/ EntrySize
;
7479 uint32_t OffsetInEntry
= RelocAddress
- EntryIdx
* EntrySize
;
7480 CompactUnwindEntry
&Entry
= CompactUnwinds
[EntryIdx
];
7482 if (OffsetInEntry
== 0)
7483 Entry
.FunctionReloc
= Reloc
;
7484 else if (OffsetInEntry
== PointerSize
+ 2 * sizeof(uint32_t))
7485 Entry
.PersonalityReloc
= Reloc
;
7486 else if (OffsetInEntry
== 2 * PointerSize
+ 2 * sizeof(uint32_t))
7487 Entry
.LSDAReloc
= Reloc
;
7489 outs() << "Invalid relocation in __compact_unwind section\n";
7494 // Finally, we're ready to print the data we've gathered.
7495 outs() << "Contents of __compact_unwind section:\n";
7496 for (auto &Entry
: CompactUnwinds
) {
7497 outs() << " Entry at offset "
7498 << format("0x%" PRIx32
, Entry
.OffsetInSection
) << ":\n";
7500 // 1. Start of the region this entry applies to.
7501 outs() << " start: " << format("0x%" PRIx64
,
7502 Entry
.FunctionAddr
) << ' ';
7503 printUnwindRelocDest(Obj
, Symbols
, Entry
.FunctionReloc
, Entry
.FunctionAddr
);
7506 // 2. Length of the region this entry applies to.
7507 outs() << " length: " << format("0x%" PRIx32
, Entry
.Length
)
7509 // 3. The 32-bit compact encoding.
7510 outs() << " compact encoding: "
7511 << format("0x%08" PRIx32
, Entry
.CompactEncoding
) << '\n';
7513 // 4. The personality function, if present.
7514 if (Entry
.PersonalityReloc
.getObject()) {
7515 outs() << " personality function: "
7516 << format("0x%" PRIx64
, Entry
.PersonalityAddr
) << ' ';
7517 printUnwindRelocDest(Obj
, Symbols
, Entry
.PersonalityReloc
,
7518 Entry
.PersonalityAddr
);
7522 // 5. This entry's language-specific data area.
7523 if (Entry
.LSDAReloc
.getObject()) {
7524 outs() << " LSDA: " << format("0x%" PRIx64
,
7525 Entry
.LSDAAddr
) << ' ';
7526 printUnwindRelocDest(Obj
, Symbols
, Entry
.LSDAReloc
, Entry
.LSDAAddr
);
7532 //===----------------------------------------------------------------------===//
7533 // __unwind_info section dumping
7534 //===----------------------------------------------------------------------===//
7536 static void printRegularSecondLevelUnwindPage(StringRef PageData
) {
7538 uint32_t Kind
= readNext
<uint32_t>(PageData
, Pos
);
7540 assert(Kind
== 2 && "kind for a regular 2nd level index should be 2");
7542 uint16_t EntriesStart
= readNext
<uint16_t>(PageData
, Pos
);
7543 uint16_t NumEntries
= readNext
<uint16_t>(PageData
, Pos
);
7546 for (unsigned i
= 0; i
< NumEntries
; ++i
) {
7547 uint32_t FunctionOffset
= readNext
<uint32_t>(PageData
, Pos
);
7548 uint32_t Encoding
= readNext
<uint32_t>(PageData
, Pos
);
7550 outs() << " [" << i
<< "]: "
7551 << "function offset=" << format("0x%08" PRIx32
, FunctionOffset
)
7553 << "encoding=" << format("0x%08" PRIx32
, Encoding
) << '\n';
7557 static void printCompressedSecondLevelUnwindPage(
7558 StringRef PageData
, uint32_t FunctionBase
,
7559 const SmallVectorImpl
<uint32_t> &CommonEncodings
) {
7561 uint32_t Kind
= readNext
<uint32_t>(PageData
, Pos
);
7563 assert(Kind
== 3 && "kind for a compressed 2nd level index should be 3");
7565 uint16_t EntriesStart
= readNext
<uint16_t>(PageData
, Pos
);
7566 uint16_t NumEntries
= readNext
<uint16_t>(PageData
, Pos
);
7568 uint16_t EncodingsStart
= readNext
<uint16_t>(PageData
, Pos
);
7569 readNext
<uint16_t>(PageData
, Pos
);
7570 StringRef PageEncodings
= PageData
.substr(EncodingsStart
, StringRef::npos
);
7573 for (unsigned i
= 0; i
< NumEntries
; ++i
) {
7574 uint32_t Entry
= readNext
<uint32_t>(PageData
, Pos
);
7575 uint32_t FunctionOffset
= FunctionBase
+ (Entry
& 0xffffff);
7576 uint32_t EncodingIdx
= Entry
>> 24;
7579 if (EncodingIdx
< CommonEncodings
.size())
7580 Encoding
= CommonEncodings
[EncodingIdx
];
7582 Encoding
= read
<uint32_t>(PageEncodings
,
7584 (EncodingIdx
- CommonEncodings
.size()));
7586 outs() << " [" << i
<< "]: "
7587 << "function offset=" << format("0x%08" PRIx32
, FunctionOffset
)
7589 << "encoding[" << EncodingIdx
7590 << "]=" << format("0x%08" PRIx32
, Encoding
) << '\n';
7594 static void printMachOUnwindInfoSection(const MachOObjectFile
*Obj
,
7595 std::map
<uint64_t, SymbolRef
> &Symbols
,
7596 const SectionRef
&UnwindInfo
) {
7598 if (!Obj
->isLittleEndian()) {
7599 outs() << "Skipping big-endian __unwind_info section\n";
7603 outs() << "Contents of __unwind_info section:\n";
7606 UnwindInfo
.getContents(Contents
);
7609 //===----------------------------------
7611 //===----------------------------------
7613 uint32_t Version
= readNext
<uint32_t>(Contents
, Pos
);
7614 outs() << " Version: "
7615 << format("0x%" PRIx32
, Version
) << '\n';
7617 outs() << " Skipping section with unknown version\n";
7621 uint32_t CommonEncodingsStart
= readNext
<uint32_t>(Contents
, Pos
);
7622 outs() << " Common encodings array section offset: "
7623 << format("0x%" PRIx32
, CommonEncodingsStart
) << '\n';
7624 uint32_t NumCommonEncodings
= readNext
<uint32_t>(Contents
, Pos
);
7625 outs() << " Number of common encodings in array: "
7626 << format("0x%" PRIx32
, NumCommonEncodings
) << '\n';
7628 uint32_t PersonalitiesStart
= readNext
<uint32_t>(Contents
, Pos
);
7629 outs() << " Personality function array section offset: "
7630 << format("0x%" PRIx32
, PersonalitiesStart
) << '\n';
7631 uint32_t NumPersonalities
= readNext
<uint32_t>(Contents
, Pos
);
7632 outs() << " Number of personality functions in array: "
7633 << format("0x%" PRIx32
, NumPersonalities
) << '\n';
7635 uint32_t IndicesStart
= readNext
<uint32_t>(Contents
, Pos
);
7636 outs() << " Index array section offset: "
7637 << format("0x%" PRIx32
, IndicesStart
) << '\n';
7638 uint32_t NumIndices
= readNext
<uint32_t>(Contents
, Pos
);
7639 outs() << " Number of indices in array: "
7640 << format("0x%" PRIx32
, NumIndices
) << '\n';
7642 //===----------------------------------
7643 // A shared list of common encodings
7644 //===----------------------------------
7646 // These occupy indices in the range [0, N] whenever an encoding is referenced
7647 // from a compressed 2nd level index table. In practice the linker only
7648 // creates ~128 of these, so that indices are available to embed encodings in
7649 // the 2nd level index.
7651 SmallVector
<uint32_t, 64> CommonEncodings
;
7652 outs() << " Common encodings: (count = " << NumCommonEncodings
<< ")\n";
7653 Pos
= CommonEncodingsStart
;
7654 for (unsigned i
= 0; i
< NumCommonEncodings
; ++i
) {
7655 uint32_t Encoding
= readNext
<uint32_t>(Contents
, Pos
);
7656 CommonEncodings
.push_back(Encoding
);
7658 outs() << " encoding[" << i
<< "]: " << format("0x%08" PRIx32
, Encoding
)
7662 //===----------------------------------
7663 // Personality functions used in this executable
7664 //===----------------------------------
7666 // There should be only a handful of these (one per source language,
7667 // roughly). Particularly since they only get 2 bits in the compact encoding.
7669 outs() << " Personality functions: (count = " << NumPersonalities
<< ")\n";
7670 Pos
= PersonalitiesStart
;
7671 for (unsigned i
= 0; i
< NumPersonalities
; ++i
) {
7672 uint32_t PersonalityFn
= readNext
<uint32_t>(Contents
, Pos
);
7673 outs() << " personality[" << i
+ 1
7674 << "]: " << format("0x%08" PRIx32
, PersonalityFn
) << '\n';
7677 //===----------------------------------
7678 // The level 1 index entries
7679 //===----------------------------------
7681 // These specify an approximate place to start searching for the more detailed
7682 // information, sorted by PC.
7685 uint32_t FunctionOffset
;
7686 uint32_t SecondLevelPageStart
;
7690 SmallVector
<IndexEntry
, 4> IndexEntries
;
7692 outs() << " Top level indices: (count = " << NumIndices
<< ")\n";
7694 for (unsigned i
= 0; i
< NumIndices
; ++i
) {
7697 Entry
.FunctionOffset
= readNext
<uint32_t>(Contents
, Pos
);
7698 Entry
.SecondLevelPageStart
= readNext
<uint32_t>(Contents
, Pos
);
7699 Entry
.LSDAStart
= readNext
<uint32_t>(Contents
, Pos
);
7700 IndexEntries
.push_back(Entry
);
7702 outs() << " [" << i
<< "]: "
7703 << "function offset=" << format("0x%08" PRIx32
, Entry
.FunctionOffset
)
7705 << "2nd level page offset="
7706 << format("0x%08" PRIx32
, Entry
.SecondLevelPageStart
) << ", "
7707 << "LSDA offset=" << format("0x%08" PRIx32
, Entry
.LSDAStart
) << '\n';
7710 //===----------------------------------
7711 // Next come the LSDA tables
7712 //===----------------------------------
7714 // The LSDA layout is rather implicit: it's a contiguous array of entries from
7715 // the first top-level index's LSDAOffset to the last (sentinel).
7717 outs() << " LSDA descriptors:\n";
7718 Pos
= IndexEntries
[0].LSDAStart
;
7719 const uint32_t LSDASize
= 2 * sizeof(uint32_t);
7721 (IndexEntries
.back().LSDAStart
- IndexEntries
[0].LSDAStart
) / LSDASize
;
7723 for (int i
= 0; i
< NumLSDAs
; ++i
) {
7724 uint32_t FunctionOffset
= readNext
<uint32_t>(Contents
, Pos
);
7725 uint32_t LSDAOffset
= readNext
<uint32_t>(Contents
, Pos
);
7726 outs() << " [" << i
<< "]: "
7727 << "function offset=" << format("0x%08" PRIx32
, FunctionOffset
)
7729 << "LSDA offset=" << format("0x%08" PRIx32
, LSDAOffset
) << '\n';
7732 //===----------------------------------
7733 // Finally, the 2nd level indices
7734 //===----------------------------------
7736 // Generally these are 4K in size, and have 2 possible forms:
7737 // + Regular stores up to 511 entries with disparate encodings
7738 // + Compressed stores up to 1021 entries if few enough compact encoding
7740 outs() << " Second level indices:\n";
7741 for (unsigned i
= 0; i
< IndexEntries
.size() - 1; ++i
) {
7742 // The final sentinel top-level index has no associated 2nd level page
7743 if (IndexEntries
[i
].SecondLevelPageStart
== 0)
7746 outs() << " Second level index[" << i
<< "]: "
7747 << "offset in section="
7748 << format("0x%08" PRIx32
, IndexEntries
[i
].SecondLevelPageStart
)
7750 << "base function offset="
7751 << format("0x%08" PRIx32
, IndexEntries
[i
].FunctionOffset
) << '\n';
7753 Pos
= IndexEntries
[i
].SecondLevelPageStart
;
7754 if (Pos
+ sizeof(uint32_t) > Contents
.size()) {
7755 outs() << "warning: invalid offset for second level page: " << Pos
<< '\n';
7760 *reinterpret_cast<const support::ulittle32_t
*>(Contents
.data() + Pos
);
7762 printRegularSecondLevelUnwindPage(Contents
.substr(Pos
, 4096));
7764 printCompressedSecondLevelUnwindPage(Contents
.substr(Pos
, 4096),
7765 IndexEntries
[i
].FunctionOffset
,
7768 outs() << " Skipping 2nd level page with unknown kind " << Kind
7773 void llvm::printMachOUnwindInfo(const MachOObjectFile
*Obj
) {
7774 std::map
<uint64_t, SymbolRef
> Symbols
;
7775 for (const SymbolRef
&SymRef
: Obj
->symbols()) {
7776 // Discard any undefined or absolute symbols. They're not going to take part
7777 // in the convenience lookup for unwind info and just take up resources.
7778 auto SectOrErr
= SymRef
.getSection();
7780 // TODO: Actually report errors helpfully.
7781 consumeError(SectOrErr
.takeError());
7784 section_iterator Section
= *SectOrErr
;
7785 if (Section
== Obj
->section_end())
7788 uint64_t Addr
= SymRef
.getValue();
7789 Symbols
.insert(std::make_pair(Addr
, SymRef
));
7792 for (const SectionRef
&Section
: Obj
->sections()) {
7794 Section
.getName(SectName
);
7795 if (SectName
== "__compact_unwind")
7796 printMachOCompactUnwindSection(Obj
, Symbols
, Section
);
7797 else if (SectName
== "__unwind_info")
7798 printMachOUnwindInfoSection(Obj
, Symbols
, Section
);
7802 static void PrintMachHeader(uint32_t magic
, uint32_t cputype
,
7803 uint32_t cpusubtype
, uint32_t filetype
,
7804 uint32_t ncmds
, uint32_t sizeofcmds
, uint32_t flags
,
7806 outs() << "Mach header\n";
7807 outs() << " magic cputype cpusubtype caps filetype ncmds "
7808 "sizeofcmds flags\n";
7810 if (magic
== MachO::MH_MAGIC
)
7811 outs() << " MH_MAGIC";
7812 else if (magic
== MachO::MH_MAGIC_64
)
7813 outs() << "MH_MAGIC_64";
7815 outs() << format(" 0x%08" PRIx32
, magic
);
7817 case MachO::CPU_TYPE_I386
:
7819 switch (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) {
7820 case MachO::CPU_SUBTYPE_I386_ALL
:
7824 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
7828 case MachO::CPU_TYPE_X86_64
:
7829 outs() << " X86_64";
7830 switch (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) {
7831 case MachO::CPU_SUBTYPE_X86_64_ALL
:
7834 case MachO::CPU_SUBTYPE_X86_64_H
:
7835 outs() << " Haswell";
7838 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
7842 case MachO::CPU_TYPE_ARM
:
7844 switch (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) {
7845 case MachO::CPU_SUBTYPE_ARM_ALL
:
7848 case MachO::CPU_SUBTYPE_ARM_V4T
:
7851 case MachO::CPU_SUBTYPE_ARM_V5TEJ
:
7854 case MachO::CPU_SUBTYPE_ARM_XSCALE
:
7855 outs() << " XSCALE";
7857 case MachO::CPU_SUBTYPE_ARM_V6
:
7860 case MachO::CPU_SUBTYPE_ARM_V6M
:
7863 case MachO::CPU_SUBTYPE_ARM_V7
:
7866 case MachO::CPU_SUBTYPE_ARM_V7EM
:
7869 case MachO::CPU_SUBTYPE_ARM_V7K
:
7872 case MachO::CPU_SUBTYPE_ARM_V7M
:
7875 case MachO::CPU_SUBTYPE_ARM_V7S
:
7879 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
7883 case MachO::CPU_TYPE_ARM64
:
7885 switch (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) {
7886 case MachO::CPU_SUBTYPE_ARM64_ALL
:
7890 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
7894 case MachO::CPU_TYPE_POWERPC
:
7896 switch (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) {
7897 case MachO::CPU_SUBTYPE_POWERPC_ALL
:
7901 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
7905 case MachO::CPU_TYPE_POWERPC64
:
7907 switch (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) {
7908 case MachO::CPU_SUBTYPE_POWERPC_ALL
:
7912 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
7917 outs() << format(" %7d", cputype
);
7918 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
7921 if ((cpusubtype
& MachO::CPU_SUBTYPE_MASK
) == MachO::CPU_SUBTYPE_LIB64
) {
7924 outs() << format(" 0x%02" PRIx32
,
7925 (cpusubtype
& MachO::CPU_SUBTYPE_MASK
) >> 24);
7928 case MachO::MH_OBJECT
:
7929 outs() << " OBJECT";
7931 case MachO::MH_EXECUTE
:
7932 outs() << " EXECUTE";
7934 case MachO::MH_FVMLIB
:
7935 outs() << " FVMLIB";
7937 case MachO::MH_CORE
:
7940 case MachO::MH_PRELOAD
:
7941 outs() << " PRELOAD";
7943 case MachO::MH_DYLIB
:
7946 case MachO::MH_DYLIB_STUB
:
7947 outs() << " DYLIB_STUB";
7949 case MachO::MH_DYLINKER
:
7950 outs() << " DYLINKER";
7952 case MachO::MH_BUNDLE
:
7953 outs() << " BUNDLE";
7955 case MachO::MH_DSYM
:
7958 case MachO::MH_KEXT_BUNDLE
:
7959 outs() << " KEXTBUNDLE";
7962 outs() << format(" %10u", filetype
);
7965 outs() << format(" %5u", ncmds
);
7966 outs() << format(" %10u", sizeofcmds
);
7968 if (f
& MachO::MH_NOUNDEFS
) {
7969 outs() << " NOUNDEFS";
7970 f
&= ~MachO::MH_NOUNDEFS
;
7972 if (f
& MachO::MH_INCRLINK
) {
7973 outs() << " INCRLINK";
7974 f
&= ~MachO::MH_INCRLINK
;
7976 if (f
& MachO::MH_DYLDLINK
) {
7977 outs() << " DYLDLINK";
7978 f
&= ~MachO::MH_DYLDLINK
;
7980 if (f
& MachO::MH_BINDATLOAD
) {
7981 outs() << " BINDATLOAD";
7982 f
&= ~MachO::MH_BINDATLOAD
;
7984 if (f
& MachO::MH_PREBOUND
) {
7985 outs() << " PREBOUND";
7986 f
&= ~MachO::MH_PREBOUND
;
7988 if (f
& MachO::MH_SPLIT_SEGS
) {
7989 outs() << " SPLIT_SEGS";
7990 f
&= ~MachO::MH_SPLIT_SEGS
;
7992 if (f
& MachO::MH_LAZY_INIT
) {
7993 outs() << " LAZY_INIT";
7994 f
&= ~MachO::MH_LAZY_INIT
;
7996 if (f
& MachO::MH_TWOLEVEL
) {
7997 outs() << " TWOLEVEL";
7998 f
&= ~MachO::MH_TWOLEVEL
;
8000 if (f
& MachO::MH_FORCE_FLAT
) {
8001 outs() << " FORCE_FLAT";
8002 f
&= ~MachO::MH_FORCE_FLAT
;
8004 if (f
& MachO::MH_NOMULTIDEFS
) {
8005 outs() << " NOMULTIDEFS";
8006 f
&= ~MachO::MH_NOMULTIDEFS
;
8008 if (f
& MachO::MH_NOFIXPREBINDING
) {
8009 outs() << " NOFIXPREBINDING";
8010 f
&= ~MachO::MH_NOFIXPREBINDING
;
8012 if (f
& MachO::MH_PREBINDABLE
) {
8013 outs() << " PREBINDABLE";
8014 f
&= ~MachO::MH_PREBINDABLE
;
8016 if (f
& MachO::MH_ALLMODSBOUND
) {
8017 outs() << " ALLMODSBOUND";
8018 f
&= ~MachO::MH_ALLMODSBOUND
;
8020 if (f
& MachO::MH_SUBSECTIONS_VIA_SYMBOLS
) {
8021 outs() << " SUBSECTIONS_VIA_SYMBOLS";
8022 f
&= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS
;
8024 if (f
& MachO::MH_CANONICAL
) {
8025 outs() << " CANONICAL";
8026 f
&= ~MachO::MH_CANONICAL
;
8028 if (f
& MachO::MH_WEAK_DEFINES
) {
8029 outs() << " WEAK_DEFINES";
8030 f
&= ~MachO::MH_WEAK_DEFINES
;
8032 if (f
& MachO::MH_BINDS_TO_WEAK
) {
8033 outs() << " BINDS_TO_WEAK";
8034 f
&= ~MachO::MH_BINDS_TO_WEAK
;
8036 if (f
& MachO::MH_ALLOW_STACK_EXECUTION
) {
8037 outs() << " ALLOW_STACK_EXECUTION";
8038 f
&= ~MachO::MH_ALLOW_STACK_EXECUTION
;
8040 if (f
& MachO::MH_DEAD_STRIPPABLE_DYLIB
) {
8041 outs() << " DEAD_STRIPPABLE_DYLIB";
8042 f
&= ~MachO::MH_DEAD_STRIPPABLE_DYLIB
;
8044 if (f
& MachO::MH_PIE
) {
8046 f
&= ~MachO::MH_PIE
;
8048 if (f
& MachO::MH_NO_REEXPORTED_DYLIBS
) {
8049 outs() << " NO_REEXPORTED_DYLIBS";
8050 f
&= ~MachO::MH_NO_REEXPORTED_DYLIBS
;
8052 if (f
& MachO::MH_HAS_TLV_DESCRIPTORS
) {
8053 outs() << " MH_HAS_TLV_DESCRIPTORS";
8054 f
&= ~MachO::MH_HAS_TLV_DESCRIPTORS
;
8056 if (f
& MachO::MH_NO_HEAP_EXECUTION
) {
8057 outs() << " MH_NO_HEAP_EXECUTION";
8058 f
&= ~MachO::MH_NO_HEAP_EXECUTION
;
8060 if (f
& MachO::MH_APP_EXTENSION_SAFE
) {
8061 outs() << " APP_EXTENSION_SAFE";
8062 f
&= ~MachO::MH_APP_EXTENSION_SAFE
;
8064 if (f
& MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO
) {
8065 outs() << " NLIST_OUTOFSYNC_WITH_DYLDINFO";
8066 f
&= ~MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO
;
8068 if (f
!= 0 || flags
== 0)
8069 outs() << format(" 0x%08" PRIx32
, f
);
8071 outs() << format(" 0x%08" PRIx32
, magic
);
8072 outs() << format(" %7d", cputype
);
8073 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
8074 outs() << format(" 0x%02" PRIx32
,
8075 (cpusubtype
& MachO::CPU_SUBTYPE_MASK
) >> 24);
8076 outs() << format(" %10u", filetype
);
8077 outs() << format(" %5u", ncmds
);
8078 outs() << format(" %10u", sizeofcmds
);
8079 outs() << format(" 0x%08" PRIx32
, flags
);
8084 static void PrintSegmentCommand(uint32_t cmd
, uint32_t cmdsize
,
8085 StringRef SegName
, uint64_t vmaddr
,
8086 uint64_t vmsize
, uint64_t fileoff
,
8087 uint64_t filesize
, uint32_t maxprot
,
8088 uint32_t initprot
, uint32_t nsects
,
8089 uint32_t flags
, uint32_t object_size
,
8091 uint64_t expected_cmdsize
;
8092 if (cmd
== MachO::LC_SEGMENT
) {
8093 outs() << " cmd LC_SEGMENT\n";
8094 expected_cmdsize
= nsects
;
8095 expected_cmdsize
*= sizeof(struct MachO::section
);
8096 expected_cmdsize
+= sizeof(struct MachO::segment_command
);
8098 outs() << " cmd LC_SEGMENT_64\n";
8099 expected_cmdsize
= nsects
;
8100 expected_cmdsize
*= sizeof(struct MachO::section_64
);
8101 expected_cmdsize
+= sizeof(struct MachO::segment_command_64
);
8103 outs() << " cmdsize " << cmdsize
;
8104 if (cmdsize
!= expected_cmdsize
)
8105 outs() << " Inconsistent size\n";
8108 outs() << " segname " << SegName
<< "\n";
8109 if (cmd
== MachO::LC_SEGMENT_64
) {
8110 outs() << " vmaddr " << format("0x%016" PRIx64
, vmaddr
) << "\n";
8111 outs() << " vmsize " << format("0x%016" PRIx64
, vmsize
) << "\n";
8113 outs() << " vmaddr " << format("0x%08" PRIx64
, vmaddr
) << "\n";
8114 outs() << " vmsize " << format("0x%08" PRIx64
, vmsize
) << "\n";
8116 outs() << " fileoff " << fileoff
;
8117 if (fileoff
> object_size
)
8118 outs() << " (past end of file)\n";
8121 outs() << " filesize " << filesize
;
8122 if (fileoff
+ filesize
> object_size
)
8123 outs() << " (past end of file)\n";
8128 ~(MachO::VM_PROT_READ
| MachO::VM_PROT_WRITE
|
8129 MachO::VM_PROT_EXECUTE
)) != 0)
8130 outs() << " maxprot ?" << format("0x%08" PRIx32
, maxprot
) << "\n";
8132 outs() << " maxprot ";
8133 outs() << ((maxprot
& MachO::VM_PROT_READ
) ? "r" : "-");
8134 outs() << ((maxprot
& MachO::VM_PROT_WRITE
) ? "w" : "-");
8135 outs() << ((maxprot
& MachO::VM_PROT_EXECUTE
) ? "x\n" : "-\n");
8138 ~(MachO::VM_PROT_READ
| MachO::VM_PROT_WRITE
|
8139 MachO::VM_PROT_EXECUTE
)) != 0)
8140 outs() << " initprot ?" << format("0x%08" PRIx32
, initprot
) << "\n";
8142 outs() << " initprot ";
8143 outs() << ((initprot
& MachO::VM_PROT_READ
) ? "r" : "-");
8144 outs() << ((initprot
& MachO::VM_PROT_WRITE
) ? "w" : "-");
8145 outs() << ((initprot
& MachO::VM_PROT_EXECUTE
) ? "x\n" : "-\n");
8148 outs() << " maxprot " << format("0x%08" PRIx32
, maxprot
) << "\n";
8149 outs() << " initprot " << format("0x%08" PRIx32
, initprot
) << "\n";
8151 outs() << " nsects " << nsects
<< "\n";
8155 outs() << " (none)\n";
8157 if (flags
& MachO::SG_HIGHVM
) {
8158 outs() << " HIGHVM";
8159 flags
&= ~MachO::SG_HIGHVM
;
8161 if (flags
& MachO::SG_FVMLIB
) {
8162 outs() << " FVMLIB";
8163 flags
&= ~MachO::SG_FVMLIB
;
8165 if (flags
& MachO::SG_NORELOC
) {
8166 outs() << " NORELOC";
8167 flags
&= ~MachO::SG_NORELOC
;
8169 if (flags
& MachO::SG_PROTECTED_VERSION_1
) {
8170 outs() << " PROTECTED_VERSION_1";
8171 flags
&= ~MachO::SG_PROTECTED_VERSION_1
;
8174 outs() << format(" 0x%08" PRIx32
, flags
) << " (unknown flags)\n";
8179 outs() << " flags " << format("0x%" PRIx32
, flags
) << "\n";
8183 static void PrintSection(const char *sectname
, const char *segname
,
8184 uint64_t addr
, uint64_t size
, uint32_t offset
,
8185 uint32_t align
, uint32_t reloff
, uint32_t nreloc
,
8186 uint32_t flags
, uint32_t reserved1
, uint32_t reserved2
,
8187 uint32_t cmd
, const char *sg_segname
,
8188 uint32_t filetype
, uint32_t object_size
,
8190 outs() << "Section\n";
8191 outs() << " sectname " << format("%.16s\n", sectname
);
8192 outs() << " segname " << format("%.16s", segname
);
8193 if (filetype
!= MachO::MH_OBJECT
&& strncmp(sg_segname
, segname
, 16) != 0)
8194 outs() << " (does not match segment)\n";
8197 if (cmd
== MachO::LC_SEGMENT_64
) {
8198 outs() << " addr " << format("0x%016" PRIx64
, addr
) << "\n";
8199 outs() << " size " << format("0x%016" PRIx64
, size
);
8201 outs() << " addr " << format("0x%08" PRIx64
, addr
) << "\n";
8202 outs() << " size " << format("0x%08" PRIx64
, size
);
8204 if ((flags
& MachO::S_ZEROFILL
) != 0 && offset
+ size
> object_size
)
8205 outs() << " (past end of file)\n";
8208 outs() << " offset " << offset
;
8209 if (offset
> object_size
)
8210 outs() << " (past end of file)\n";
8213 uint32_t align_shifted
= 1 << align
;
8214 outs() << " align 2^" << align
<< " (" << align_shifted
<< ")\n";
8215 outs() << " reloff " << reloff
;
8216 if (reloff
> object_size
)
8217 outs() << " (past end of file)\n";
8220 outs() << " nreloc " << nreloc
;
8221 if (reloff
+ nreloc
* sizeof(struct MachO::relocation_info
) > object_size
)
8222 outs() << " (past end of file)\n";
8225 uint32_t section_type
= flags
& MachO::SECTION_TYPE
;
8228 if (section_type
== MachO::S_REGULAR
)
8229 outs() << " S_REGULAR\n";
8230 else if (section_type
== MachO::S_ZEROFILL
)
8231 outs() << " S_ZEROFILL\n";
8232 else if (section_type
== MachO::S_CSTRING_LITERALS
)
8233 outs() << " S_CSTRING_LITERALS\n";
8234 else if (section_type
== MachO::S_4BYTE_LITERALS
)
8235 outs() << " S_4BYTE_LITERALS\n";
8236 else if (section_type
== MachO::S_8BYTE_LITERALS
)
8237 outs() << " S_8BYTE_LITERALS\n";
8238 else if (section_type
== MachO::S_16BYTE_LITERALS
)
8239 outs() << " S_16BYTE_LITERALS\n";
8240 else if (section_type
== MachO::S_LITERAL_POINTERS
)
8241 outs() << " S_LITERAL_POINTERS\n";
8242 else if (section_type
== MachO::S_NON_LAZY_SYMBOL_POINTERS
)
8243 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
8244 else if (section_type
== MachO::S_LAZY_SYMBOL_POINTERS
)
8245 outs() << " S_LAZY_SYMBOL_POINTERS\n";
8246 else if (section_type
== MachO::S_SYMBOL_STUBS
)
8247 outs() << " S_SYMBOL_STUBS\n";
8248 else if (section_type
== MachO::S_MOD_INIT_FUNC_POINTERS
)
8249 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
8250 else if (section_type
== MachO::S_MOD_TERM_FUNC_POINTERS
)
8251 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
8252 else if (section_type
== MachO::S_COALESCED
)
8253 outs() << " S_COALESCED\n";
8254 else if (section_type
== MachO::S_INTERPOSING
)
8255 outs() << " S_INTERPOSING\n";
8256 else if (section_type
== MachO::S_DTRACE_DOF
)
8257 outs() << " S_DTRACE_DOF\n";
8258 else if (section_type
== MachO::S_LAZY_DYLIB_SYMBOL_POINTERS
)
8259 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
8260 else if (section_type
== MachO::S_THREAD_LOCAL_REGULAR
)
8261 outs() << " S_THREAD_LOCAL_REGULAR\n";
8262 else if (section_type
== MachO::S_THREAD_LOCAL_ZEROFILL
)
8263 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
8264 else if (section_type
== MachO::S_THREAD_LOCAL_VARIABLES
)
8265 outs() << " S_THREAD_LOCAL_VARIABLES\n";
8266 else if (section_type
== MachO::S_THREAD_LOCAL_VARIABLE_POINTERS
)
8267 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
8268 else if (section_type
== MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
)
8269 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
8271 outs() << format("0x%08" PRIx32
, section_type
) << "\n";
8272 outs() << "attributes";
8273 uint32_t section_attributes
= flags
& MachO::SECTION_ATTRIBUTES
;
8274 if (section_attributes
& MachO::S_ATTR_PURE_INSTRUCTIONS
)
8275 outs() << " PURE_INSTRUCTIONS";
8276 if (section_attributes
& MachO::S_ATTR_NO_TOC
)
8277 outs() << " NO_TOC";
8278 if (section_attributes
& MachO::S_ATTR_STRIP_STATIC_SYMS
)
8279 outs() << " STRIP_STATIC_SYMS";
8280 if (section_attributes
& MachO::S_ATTR_NO_DEAD_STRIP
)
8281 outs() << " NO_DEAD_STRIP";
8282 if (section_attributes
& MachO::S_ATTR_LIVE_SUPPORT
)
8283 outs() << " LIVE_SUPPORT";
8284 if (section_attributes
& MachO::S_ATTR_SELF_MODIFYING_CODE
)
8285 outs() << " SELF_MODIFYING_CODE";
8286 if (section_attributes
& MachO::S_ATTR_DEBUG
)
8288 if (section_attributes
& MachO::S_ATTR_SOME_INSTRUCTIONS
)
8289 outs() << " SOME_INSTRUCTIONS";
8290 if (section_attributes
& MachO::S_ATTR_EXT_RELOC
)
8291 outs() << " EXT_RELOC";
8292 if (section_attributes
& MachO::S_ATTR_LOC_RELOC
)
8293 outs() << " LOC_RELOC";
8294 if (section_attributes
== 0)
8295 outs() << " (none)";
8298 outs() << " flags " << format("0x%08" PRIx32
, flags
) << "\n";
8299 outs() << " reserved1 " << reserved1
;
8300 if (section_type
== MachO::S_SYMBOL_STUBS
||
8301 section_type
== MachO::S_LAZY_SYMBOL_POINTERS
||
8302 section_type
== MachO::S_LAZY_DYLIB_SYMBOL_POINTERS
||
8303 section_type
== MachO::S_NON_LAZY_SYMBOL_POINTERS
||
8304 section_type
== MachO::S_THREAD_LOCAL_VARIABLE_POINTERS
)
8305 outs() << " (index into indirect symbol table)\n";
8308 outs() << " reserved2 " << reserved2
;
8309 if (section_type
== MachO::S_SYMBOL_STUBS
)
8310 outs() << " (size of stubs)\n";
8315 static void PrintSymtabLoadCommand(MachO::symtab_command st
, bool Is64Bit
,
8316 uint32_t object_size
) {
8317 outs() << " cmd LC_SYMTAB\n";
8318 outs() << " cmdsize " << st
.cmdsize
;
8319 if (st
.cmdsize
!= sizeof(struct MachO::symtab_command
))
8320 outs() << " Incorrect size\n";
8323 outs() << " symoff " << st
.symoff
;
8324 if (st
.symoff
> object_size
)
8325 outs() << " (past end of file)\n";
8328 outs() << " nsyms " << st
.nsyms
;
8331 big_size
= st
.nsyms
;
8332 big_size
*= sizeof(struct MachO::nlist_64
);
8333 big_size
+= st
.symoff
;
8334 if (big_size
> object_size
)
8335 outs() << " (past end of file)\n";
8339 big_size
= st
.nsyms
;
8340 big_size
*= sizeof(struct MachO::nlist
);
8341 big_size
+= st
.symoff
;
8342 if (big_size
> object_size
)
8343 outs() << " (past end of file)\n";
8347 outs() << " stroff " << st
.stroff
;
8348 if (st
.stroff
> object_size
)
8349 outs() << " (past end of file)\n";
8352 outs() << " strsize " << st
.strsize
;
8353 big_size
= st
.stroff
;
8354 big_size
+= st
.strsize
;
8355 if (big_size
> object_size
)
8356 outs() << " (past end of file)\n";
8361 static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst
,
8362 uint32_t nsyms
, uint32_t object_size
,
8364 outs() << " cmd LC_DYSYMTAB\n";
8365 outs() << " cmdsize " << dyst
.cmdsize
;
8366 if (dyst
.cmdsize
!= sizeof(struct MachO::dysymtab_command
))
8367 outs() << " Incorrect size\n";
8370 outs() << " ilocalsym " << dyst
.ilocalsym
;
8371 if (dyst
.ilocalsym
> nsyms
)
8372 outs() << " (greater than the number of symbols)\n";
8375 outs() << " nlocalsym " << dyst
.nlocalsym
;
8377 big_size
= dyst
.ilocalsym
;
8378 big_size
+= dyst
.nlocalsym
;
8379 if (big_size
> nsyms
)
8380 outs() << " (past the end of the symbol table)\n";
8383 outs() << " iextdefsym " << dyst
.iextdefsym
;
8384 if (dyst
.iextdefsym
> nsyms
)
8385 outs() << " (greater than the number of symbols)\n";
8388 outs() << " nextdefsym " << dyst
.nextdefsym
;
8389 big_size
= dyst
.iextdefsym
;
8390 big_size
+= dyst
.nextdefsym
;
8391 if (big_size
> nsyms
)
8392 outs() << " (past the end of the symbol table)\n";
8395 outs() << " iundefsym " << dyst
.iundefsym
;
8396 if (dyst
.iundefsym
> nsyms
)
8397 outs() << " (greater than the number of symbols)\n";
8400 outs() << " nundefsym " << dyst
.nundefsym
;
8401 big_size
= dyst
.iundefsym
;
8402 big_size
+= dyst
.nundefsym
;
8403 if (big_size
> nsyms
)
8404 outs() << " (past the end of the symbol table)\n";
8407 outs() << " tocoff " << dyst
.tocoff
;
8408 if (dyst
.tocoff
> object_size
)
8409 outs() << " (past end of file)\n";
8412 outs() << " ntoc " << dyst
.ntoc
;
8413 big_size
= dyst
.ntoc
;
8414 big_size
*= sizeof(struct MachO::dylib_table_of_contents
);
8415 big_size
+= dyst
.tocoff
;
8416 if (big_size
> object_size
)
8417 outs() << " (past end of file)\n";
8420 outs() << " modtaboff " << dyst
.modtaboff
;
8421 if (dyst
.modtaboff
> object_size
)
8422 outs() << " (past end of file)\n";
8425 outs() << " nmodtab " << dyst
.nmodtab
;
8428 modtabend
= dyst
.nmodtab
;
8429 modtabend
*= sizeof(struct MachO::dylib_module_64
);
8430 modtabend
+= dyst
.modtaboff
;
8432 modtabend
= dyst
.nmodtab
;
8433 modtabend
*= sizeof(struct MachO::dylib_module
);
8434 modtabend
+= dyst
.modtaboff
;
8436 if (modtabend
> object_size
)
8437 outs() << " (past end of file)\n";
8440 outs() << " extrefsymoff " << dyst
.extrefsymoff
;
8441 if (dyst
.extrefsymoff
> object_size
)
8442 outs() << " (past end of file)\n";
8445 outs() << " nextrefsyms " << dyst
.nextrefsyms
;
8446 big_size
= dyst
.nextrefsyms
;
8447 big_size
*= sizeof(struct MachO::dylib_reference
);
8448 big_size
+= dyst
.extrefsymoff
;
8449 if (big_size
> object_size
)
8450 outs() << " (past end of file)\n";
8453 outs() << " indirectsymoff " << dyst
.indirectsymoff
;
8454 if (dyst
.indirectsymoff
> object_size
)
8455 outs() << " (past end of file)\n";
8458 outs() << " nindirectsyms " << dyst
.nindirectsyms
;
8459 big_size
= dyst
.nindirectsyms
;
8460 big_size
*= sizeof(uint32_t);
8461 big_size
+= dyst
.indirectsymoff
;
8462 if (big_size
> object_size
)
8463 outs() << " (past end of file)\n";
8466 outs() << " extreloff " << dyst
.extreloff
;
8467 if (dyst
.extreloff
> object_size
)
8468 outs() << " (past end of file)\n";
8471 outs() << " nextrel " << dyst
.nextrel
;
8472 big_size
= dyst
.nextrel
;
8473 big_size
*= sizeof(struct MachO::relocation_info
);
8474 big_size
+= dyst
.extreloff
;
8475 if (big_size
> object_size
)
8476 outs() << " (past end of file)\n";
8479 outs() << " locreloff " << dyst
.locreloff
;
8480 if (dyst
.locreloff
> object_size
)
8481 outs() << " (past end of file)\n";
8484 outs() << " nlocrel " << dyst
.nlocrel
;
8485 big_size
= dyst
.nlocrel
;
8486 big_size
*= sizeof(struct MachO::relocation_info
);
8487 big_size
+= dyst
.locreloff
;
8488 if (big_size
> object_size
)
8489 outs() << " (past end of file)\n";
8494 static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc
,
8495 uint32_t object_size
) {
8496 if (dc
.cmd
== MachO::LC_DYLD_INFO
)
8497 outs() << " cmd LC_DYLD_INFO\n";
8499 outs() << " cmd LC_DYLD_INFO_ONLY\n";
8500 outs() << " cmdsize " << dc
.cmdsize
;
8501 if (dc
.cmdsize
!= sizeof(struct MachO::dyld_info_command
))
8502 outs() << " Incorrect size\n";
8505 outs() << " rebase_off " << dc
.rebase_off
;
8506 if (dc
.rebase_off
> object_size
)
8507 outs() << " (past end of file)\n";
8510 outs() << " rebase_size " << dc
.rebase_size
;
8512 big_size
= dc
.rebase_off
;
8513 big_size
+= dc
.rebase_size
;
8514 if (big_size
> object_size
)
8515 outs() << " (past end of file)\n";
8518 outs() << " bind_off " << dc
.bind_off
;
8519 if (dc
.bind_off
> object_size
)
8520 outs() << " (past end of file)\n";
8523 outs() << " bind_size " << dc
.bind_size
;
8524 big_size
= dc
.bind_off
;
8525 big_size
+= dc
.bind_size
;
8526 if (big_size
> object_size
)
8527 outs() << " (past end of file)\n";
8530 outs() << " weak_bind_off " << dc
.weak_bind_off
;
8531 if (dc
.weak_bind_off
> object_size
)
8532 outs() << " (past end of file)\n";
8535 outs() << " weak_bind_size " << dc
.weak_bind_size
;
8536 big_size
= dc
.weak_bind_off
;
8537 big_size
+= dc
.weak_bind_size
;
8538 if (big_size
> object_size
)
8539 outs() << " (past end of file)\n";
8542 outs() << " lazy_bind_off " << dc
.lazy_bind_off
;
8543 if (dc
.lazy_bind_off
> object_size
)
8544 outs() << " (past end of file)\n";
8547 outs() << " lazy_bind_size " << dc
.lazy_bind_size
;
8548 big_size
= dc
.lazy_bind_off
;
8549 big_size
+= dc
.lazy_bind_size
;
8550 if (big_size
> object_size
)
8551 outs() << " (past end of file)\n";
8554 outs() << " export_off " << dc
.export_off
;
8555 if (dc
.export_off
> object_size
)
8556 outs() << " (past end of file)\n";
8559 outs() << " export_size " << dc
.export_size
;
8560 big_size
= dc
.export_off
;
8561 big_size
+= dc
.export_size
;
8562 if (big_size
> object_size
)
8563 outs() << " (past end of file)\n";
8568 static void PrintDyldLoadCommand(MachO::dylinker_command dyld
,
8570 if (dyld
.cmd
== MachO::LC_ID_DYLINKER
)
8571 outs() << " cmd LC_ID_DYLINKER\n";
8572 else if (dyld
.cmd
== MachO::LC_LOAD_DYLINKER
)
8573 outs() << " cmd LC_LOAD_DYLINKER\n";
8574 else if (dyld
.cmd
== MachO::LC_DYLD_ENVIRONMENT
)
8575 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
8577 outs() << " cmd ?(" << dyld
.cmd
<< ")\n";
8578 outs() << " cmdsize " << dyld
.cmdsize
;
8579 if (dyld
.cmdsize
< sizeof(struct MachO::dylinker_command
))
8580 outs() << " Incorrect size\n";
8583 if (dyld
.name
>= dyld
.cmdsize
)
8584 outs() << " name ?(bad offset " << dyld
.name
<< ")\n";
8586 const char *P
= (const char *)(Ptr
) + dyld
.name
;
8587 outs() << " name " << P
<< " (offset " << dyld
.name
<< ")\n";
8591 static void PrintUuidLoadCommand(MachO::uuid_command uuid
) {
8592 outs() << " cmd LC_UUID\n";
8593 outs() << " cmdsize " << uuid
.cmdsize
;
8594 if (uuid
.cmdsize
!= sizeof(struct MachO::uuid_command
))
8595 outs() << " Incorrect size\n";
8599 for (int i
= 0; i
< 16; ++i
) {
8600 outs() << format("%02" PRIX32
, uuid
.uuid
[i
]);
8601 if (i
== 3 || i
== 5 || i
== 7 || i
== 9)
8607 static void PrintRpathLoadCommand(MachO::rpath_command rpath
, const char *Ptr
) {
8608 outs() << " cmd LC_RPATH\n";
8609 outs() << " cmdsize " << rpath
.cmdsize
;
8610 if (rpath
.cmdsize
< sizeof(struct MachO::rpath_command
))
8611 outs() << " Incorrect size\n";
8614 if (rpath
.path
>= rpath
.cmdsize
)
8615 outs() << " path ?(bad offset " << rpath
.path
<< ")\n";
8617 const char *P
= (const char *)(Ptr
) + rpath
.path
;
8618 outs() << " path " << P
<< " (offset " << rpath
.path
<< ")\n";
8622 static void PrintVersionMinLoadCommand(MachO::version_min_command vd
) {
8623 StringRef LoadCmdName
;
8625 case MachO::LC_VERSION_MIN_MACOSX
:
8626 LoadCmdName
= "LC_VERSION_MIN_MACOSX";
8628 case MachO::LC_VERSION_MIN_IPHONEOS
:
8629 LoadCmdName
= "LC_VERSION_MIN_IPHONEOS";
8631 case MachO::LC_VERSION_MIN_TVOS
:
8632 LoadCmdName
= "LC_VERSION_MIN_TVOS";
8634 case MachO::LC_VERSION_MIN_WATCHOS
:
8635 LoadCmdName
= "LC_VERSION_MIN_WATCHOS";
8638 llvm_unreachable("Unknown version min load command");
8641 outs() << " cmd " << LoadCmdName
<< '\n';
8642 outs() << " cmdsize " << vd
.cmdsize
;
8643 if (vd
.cmdsize
!= sizeof(struct MachO::version_min_command
))
8644 outs() << " Incorrect size\n";
8647 outs() << " version "
8648 << MachOObjectFile::getVersionMinMajor(vd
, false) << "."
8649 << MachOObjectFile::getVersionMinMinor(vd
, false);
8650 uint32_t Update
= MachOObjectFile::getVersionMinUpdate(vd
, false);
8652 outs() << "." << Update
;
8655 outs() << " sdk n/a";
8658 << MachOObjectFile::getVersionMinMajor(vd
, true) << "."
8659 << MachOObjectFile::getVersionMinMinor(vd
, true);
8661 Update
= MachOObjectFile::getVersionMinUpdate(vd
, true);
8663 outs() << "." << Update
;
8667 static void PrintNoteLoadCommand(MachO::note_command Nt
) {
8668 outs() << " cmd LC_NOTE\n";
8669 outs() << " cmdsize " << Nt
.cmdsize
;
8670 if (Nt
.cmdsize
!= sizeof(struct MachO::note_command
))
8671 outs() << " Incorrect size\n";
8674 const char *d
= Nt
.data_owner
;
8675 outs() << "data_owner " << format("%.16s\n", d
);
8676 outs() << " offset " << Nt
.offset
<< "\n";
8677 outs() << " size " << Nt
.size
<< "\n";
8680 static void PrintBuildToolVersion(MachO::build_tool_version bv
) {
8681 outs() << " tool " << MachOObjectFile::getBuildTool(bv
.tool
) << "\n";
8682 outs() << " version " << MachOObjectFile::getVersionString(bv
.version
)
8686 static void PrintBuildVersionLoadCommand(const MachOObjectFile
*obj
,
8687 MachO::build_version_command bd
) {
8688 outs() << " cmd LC_BUILD_VERSION\n";
8689 outs() << " cmdsize " << bd
.cmdsize
;
8691 sizeof(struct MachO::build_version_command
) +
8692 bd
.ntools
* sizeof(struct MachO::build_tool_version
))
8693 outs() << " Incorrect size\n";
8696 outs() << " platform " << MachOObjectFile::getBuildPlatform(bd
.platform
)
8699 outs() << " sdk " << MachOObjectFile::getVersionString(bd
.sdk
)
8702 outs() << " sdk n/a\n";
8703 outs() << " minos " << MachOObjectFile::getVersionString(bd
.minos
)
8705 outs() << " ntools " << bd
.ntools
<< "\n";
8706 for (unsigned i
= 0; i
< bd
.ntools
; ++i
) {
8707 MachO::build_tool_version bv
= obj
->getBuildToolVersion(i
);
8708 PrintBuildToolVersion(bv
);
8712 static void PrintSourceVersionCommand(MachO::source_version_command sd
) {
8713 outs() << " cmd LC_SOURCE_VERSION\n";
8714 outs() << " cmdsize " << sd
.cmdsize
;
8715 if (sd
.cmdsize
!= sizeof(struct MachO::source_version_command
))
8716 outs() << " Incorrect size\n";
8719 uint64_t a
= (sd
.version
>> 40) & 0xffffff;
8720 uint64_t b
= (sd
.version
>> 30) & 0x3ff;
8721 uint64_t c
= (sd
.version
>> 20) & 0x3ff;
8722 uint64_t d
= (sd
.version
>> 10) & 0x3ff;
8723 uint64_t e
= sd
.version
& 0x3ff;
8724 outs() << " version " << a
<< "." << b
;
8726 outs() << "." << c
<< "." << d
<< "." << e
;
8728 outs() << "." << c
<< "." << d
;
8734 static void PrintEntryPointCommand(MachO::entry_point_command ep
) {
8735 outs() << " cmd LC_MAIN\n";
8736 outs() << " cmdsize " << ep
.cmdsize
;
8737 if (ep
.cmdsize
!= sizeof(struct MachO::entry_point_command
))
8738 outs() << " Incorrect size\n";
8741 outs() << " entryoff " << ep
.entryoff
<< "\n";
8742 outs() << " stacksize " << ep
.stacksize
<< "\n";
8745 static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec
,
8746 uint32_t object_size
) {
8747 outs() << " cmd LC_ENCRYPTION_INFO\n";
8748 outs() << " cmdsize " << ec
.cmdsize
;
8749 if (ec
.cmdsize
!= sizeof(struct MachO::encryption_info_command
))
8750 outs() << " Incorrect size\n";
8753 outs() << " cryptoff " << ec
.cryptoff
;
8754 if (ec
.cryptoff
> object_size
)
8755 outs() << " (past end of file)\n";
8758 outs() << " cryptsize " << ec
.cryptsize
;
8759 if (ec
.cryptsize
> object_size
)
8760 outs() << " (past end of file)\n";
8763 outs() << " cryptid " << ec
.cryptid
<< "\n";
8766 static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec
,
8767 uint32_t object_size
) {
8768 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
8769 outs() << " cmdsize " << ec
.cmdsize
;
8770 if (ec
.cmdsize
!= sizeof(struct MachO::encryption_info_command_64
))
8771 outs() << " Incorrect size\n";
8774 outs() << " cryptoff " << ec
.cryptoff
;
8775 if (ec
.cryptoff
> object_size
)
8776 outs() << " (past end of file)\n";
8779 outs() << " cryptsize " << ec
.cryptsize
;
8780 if (ec
.cryptsize
> object_size
)
8781 outs() << " (past end of file)\n";
8784 outs() << " cryptid " << ec
.cryptid
<< "\n";
8785 outs() << " pad " << ec
.pad
<< "\n";
8788 static void PrintLinkerOptionCommand(MachO::linker_option_command lo
,
8790 outs() << " cmd LC_LINKER_OPTION\n";
8791 outs() << " cmdsize " << lo
.cmdsize
;
8792 if (lo
.cmdsize
< sizeof(struct MachO::linker_option_command
))
8793 outs() << " Incorrect size\n";
8796 outs() << " count " << lo
.count
<< "\n";
8797 const char *string
= Ptr
+ sizeof(struct MachO::linker_option_command
);
8798 uint32_t left
= lo
.cmdsize
- sizeof(struct MachO::linker_option_command
);
8801 while (*string
== '\0' && left
> 0) {
8807 outs() << " string #" << i
<< " " << format("%.*s\n", left
, string
);
8808 uint32_t NullPos
= StringRef(string
, left
).find('\0');
8809 uint32_t len
= std::min(NullPos
, left
) + 1;
8815 outs() << " count " << lo
.count
<< " does not match number of strings "
8819 static void PrintSubFrameworkCommand(MachO::sub_framework_command sub
,
8821 outs() << " cmd LC_SUB_FRAMEWORK\n";
8822 outs() << " cmdsize " << sub
.cmdsize
;
8823 if (sub
.cmdsize
< sizeof(struct MachO::sub_framework_command
))
8824 outs() << " Incorrect size\n";
8827 if (sub
.umbrella
< sub
.cmdsize
) {
8828 const char *P
= Ptr
+ sub
.umbrella
;
8829 outs() << " umbrella " << P
<< " (offset " << sub
.umbrella
<< ")\n";
8831 outs() << " umbrella ?(bad offset " << sub
.umbrella
<< ")\n";
8835 static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub
,
8837 outs() << " cmd LC_SUB_UMBRELLA\n";
8838 outs() << " cmdsize " << sub
.cmdsize
;
8839 if (sub
.cmdsize
< sizeof(struct MachO::sub_umbrella_command
))
8840 outs() << " Incorrect size\n";
8843 if (sub
.sub_umbrella
< sub
.cmdsize
) {
8844 const char *P
= Ptr
+ sub
.sub_umbrella
;
8845 outs() << " sub_umbrella " << P
<< " (offset " << sub
.sub_umbrella
<< ")\n";
8847 outs() << " sub_umbrella ?(bad offset " << sub
.sub_umbrella
<< ")\n";
8851 static void PrintSubLibraryCommand(MachO::sub_library_command sub
,
8853 outs() << " cmd LC_SUB_LIBRARY\n";
8854 outs() << " cmdsize " << sub
.cmdsize
;
8855 if (sub
.cmdsize
< sizeof(struct MachO::sub_library_command
))
8856 outs() << " Incorrect size\n";
8859 if (sub
.sub_library
< sub
.cmdsize
) {
8860 const char *P
= Ptr
+ sub
.sub_library
;
8861 outs() << " sub_library " << P
<< " (offset " << sub
.sub_library
<< ")\n";
8863 outs() << " sub_library ?(bad offset " << sub
.sub_library
<< ")\n";
8867 static void PrintSubClientCommand(MachO::sub_client_command sub
,
8869 outs() << " cmd LC_SUB_CLIENT\n";
8870 outs() << " cmdsize " << sub
.cmdsize
;
8871 if (sub
.cmdsize
< sizeof(struct MachO::sub_client_command
))
8872 outs() << " Incorrect size\n";
8875 if (sub
.client
< sub
.cmdsize
) {
8876 const char *P
= Ptr
+ sub
.client
;
8877 outs() << " client " << P
<< " (offset " << sub
.client
<< ")\n";
8879 outs() << " client ?(bad offset " << sub
.client
<< ")\n";
8883 static void PrintRoutinesCommand(MachO::routines_command r
) {
8884 outs() << " cmd LC_ROUTINES\n";
8885 outs() << " cmdsize " << r
.cmdsize
;
8886 if (r
.cmdsize
!= sizeof(struct MachO::routines_command
))
8887 outs() << " Incorrect size\n";
8890 outs() << " init_address " << format("0x%08" PRIx32
, r
.init_address
) << "\n";
8891 outs() << " init_module " << r
.init_module
<< "\n";
8892 outs() << " reserved1 " << r
.reserved1
<< "\n";
8893 outs() << " reserved2 " << r
.reserved2
<< "\n";
8894 outs() << " reserved3 " << r
.reserved3
<< "\n";
8895 outs() << " reserved4 " << r
.reserved4
<< "\n";
8896 outs() << " reserved5 " << r
.reserved5
<< "\n";
8897 outs() << " reserved6 " << r
.reserved6
<< "\n";
8900 static void PrintRoutinesCommand64(MachO::routines_command_64 r
) {
8901 outs() << " cmd LC_ROUTINES_64\n";
8902 outs() << " cmdsize " << r
.cmdsize
;
8903 if (r
.cmdsize
!= sizeof(struct MachO::routines_command_64
))
8904 outs() << " Incorrect size\n";
8907 outs() << " init_address " << format("0x%016" PRIx64
, r
.init_address
) << "\n";
8908 outs() << " init_module " << r
.init_module
<< "\n";
8909 outs() << " reserved1 " << r
.reserved1
<< "\n";
8910 outs() << " reserved2 " << r
.reserved2
<< "\n";
8911 outs() << " reserved3 " << r
.reserved3
<< "\n";
8912 outs() << " reserved4 " << r
.reserved4
<< "\n";
8913 outs() << " reserved5 " << r
.reserved5
<< "\n";
8914 outs() << " reserved6 " << r
.reserved6
<< "\n";
8917 static void Print_x86_thread_state32_t(MachO::x86_thread_state32_t
&cpu32
) {
8918 outs() << "\t eax " << format("0x%08" PRIx32
, cpu32
.eax
);
8919 outs() << " ebx " << format("0x%08" PRIx32
, cpu32
.ebx
);
8920 outs() << " ecx " << format("0x%08" PRIx32
, cpu32
.ecx
);
8921 outs() << " edx " << format("0x%08" PRIx32
, cpu32
.edx
) << "\n";
8922 outs() << "\t edi " << format("0x%08" PRIx32
, cpu32
.edi
);
8923 outs() << " esi " << format("0x%08" PRIx32
, cpu32
.esi
);
8924 outs() << " ebp " << format("0x%08" PRIx32
, cpu32
.ebp
);
8925 outs() << " esp " << format("0x%08" PRIx32
, cpu32
.esp
) << "\n";
8926 outs() << "\t ss " << format("0x%08" PRIx32
, cpu32
.ss
);
8927 outs() << " eflags " << format("0x%08" PRIx32
, cpu32
.eflags
);
8928 outs() << " eip " << format("0x%08" PRIx32
, cpu32
.eip
);
8929 outs() << " cs " << format("0x%08" PRIx32
, cpu32
.cs
) << "\n";
8930 outs() << "\t ds " << format("0x%08" PRIx32
, cpu32
.ds
);
8931 outs() << " es " << format("0x%08" PRIx32
, cpu32
.es
);
8932 outs() << " fs " << format("0x%08" PRIx32
, cpu32
.fs
);
8933 outs() << " gs " << format("0x%08" PRIx32
, cpu32
.gs
) << "\n";
8936 static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t
&cpu64
) {
8937 outs() << " rax " << format("0x%016" PRIx64
, cpu64
.rax
);
8938 outs() << " rbx " << format("0x%016" PRIx64
, cpu64
.rbx
);
8939 outs() << " rcx " << format("0x%016" PRIx64
, cpu64
.rcx
) << "\n";
8940 outs() << " rdx " << format("0x%016" PRIx64
, cpu64
.rdx
);
8941 outs() << " rdi " << format("0x%016" PRIx64
, cpu64
.rdi
);
8942 outs() << " rsi " << format("0x%016" PRIx64
, cpu64
.rsi
) << "\n";
8943 outs() << " rbp " << format("0x%016" PRIx64
, cpu64
.rbp
);
8944 outs() << " rsp " << format("0x%016" PRIx64
, cpu64
.rsp
);
8945 outs() << " r8 " << format("0x%016" PRIx64
, cpu64
.r8
) << "\n";
8946 outs() << " r9 " << format("0x%016" PRIx64
, cpu64
.r9
);
8947 outs() << " r10 " << format("0x%016" PRIx64
, cpu64
.r10
);
8948 outs() << " r11 " << format("0x%016" PRIx64
, cpu64
.r11
) << "\n";
8949 outs() << " r12 " << format("0x%016" PRIx64
, cpu64
.r12
);
8950 outs() << " r13 " << format("0x%016" PRIx64
, cpu64
.r13
);
8951 outs() << " r14 " << format("0x%016" PRIx64
, cpu64
.r14
) << "\n";
8952 outs() << " r15 " << format("0x%016" PRIx64
, cpu64
.r15
);
8953 outs() << " rip " << format("0x%016" PRIx64
, cpu64
.rip
) << "\n";
8954 outs() << "rflags " << format("0x%016" PRIx64
, cpu64
.rflags
);
8955 outs() << " cs " << format("0x%016" PRIx64
, cpu64
.cs
);
8956 outs() << " fs " << format("0x%016" PRIx64
, cpu64
.fs
) << "\n";
8957 outs() << " gs " << format("0x%016" PRIx64
, cpu64
.gs
) << "\n";
8960 static void Print_mmst_reg(MachO::mmst_reg_t
&r
) {
8962 outs() << "\t mmst_reg ";
8963 for (f
= 0; f
< 10; f
++)
8964 outs() << format("%02" PRIx32
, (r
.mmst_reg
[f
] & 0xff)) << " ";
8966 outs() << "\t mmst_rsrv ";
8967 for (f
= 0; f
< 6; f
++)
8968 outs() << format("%02" PRIx32
, (r
.mmst_rsrv
[f
] & 0xff)) << " ";
8972 static void Print_xmm_reg(MachO::xmm_reg_t
&r
) {
8974 outs() << "\t xmm_reg ";
8975 for (f
= 0; f
< 16; f
++)
8976 outs() << format("%02" PRIx32
, (r
.xmm_reg
[f
] & 0xff)) << " ";
8980 static void Print_x86_float_state_t(MachO::x86_float_state64_t
&fpu
) {
8981 outs() << "\t fpu_reserved[0] " << fpu
.fpu_reserved
[0];
8982 outs() << " fpu_reserved[1] " << fpu
.fpu_reserved
[1] << "\n";
8983 outs() << "\t control: invalid " << fpu
.fpu_fcw
.invalid
;
8984 outs() << " denorm " << fpu
.fpu_fcw
.denorm
;
8985 outs() << " zdiv " << fpu
.fpu_fcw
.zdiv
;
8986 outs() << " ovrfl " << fpu
.fpu_fcw
.ovrfl
;
8987 outs() << " undfl " << fpu
.fpu_fcw
.undfl
;
8988 outs() << " precis " << fpu
.fpu_fcw
.precis
<< "\n";
8989 outs() << "\t\t pc ";
8990 if (fpu
.fpu_fcw
.pc
== MachO::x86_FP_PREC_24B
)
8991 outs() << "FP_PREC_24B ";
8992 else if (fpu
.fpu_fcw
.pc
== MachO::x86_FP_PREC_53B
)
8993 outs() << "FP_PREC_53B ";
8994 else if (fpu
.fpu_fcw
.pc
== MachO::x86_FP_PREC_64B
)
8995 outs() << "FP_PREC_64B ";
8997 outs() << fpu
.fpu_fcw
.pc
<< " ";
8999 if (fpu
.fpu_fcw
.rc
== MachO::x86_FP_RND_NEAR
)
9000 outs() << "FP_RND_NEAR ";
9001 else if (fpu
.fpu_fcw
.rc
== MachO::x86_FP_RND_DOWN
)
9002 outs() << "FP_RND_DOWN ";
9003 else if (fpu
.fpu_fcw
.rc
== MachO::x86_FP_RND_UP
)
9004 outs() << "FP_RND_UP ";
9005 else if (fpu
.fpu_fcw
.rc
== MachO::x86_FP_CHOP
)
9006 outs() << "FP_CHOP ";
9008 outs() << "\t status: invalid " << fpu
.fpu_fsw
.invalid
;
9009 outs() << " denorm " << fpu
.fpu_fsw
.denorm
;
9010 outs() << " zdiv " << fpu
.fpu_fsw
.zdiv
;
9011 outs() << " ovrfl " << fpu
.fpu_fsw
.ovrfl
;
9012 outs() << " undfl " << fpu
.fpu_fsw
.undfl
;
9013 outs() << " precis " << fpu
.fpu_fsw
.precis
;
9014 outs() << " stkflt " << fpu
.fpu_fsw
.stkflt
<< "\n";
9015 outs() << "\t errsumm " << fpu
.fpu_fsw
.errsumm
;
9016 outs() << " c0 " << fpu
.fpu_fsw
.c0
;
9017 outs() << " c1 " << fpu
.fpu_fsw
.c1
;
9018 outs() << " c2 " << fpu
.fpu_fsw
.c2
;
9019 outs() << " tos " << fpu
.fpu_fsw
.tos
;
9020 outs() << " c3 " << fpu
.fpu_fsw
.c3
;
9021 outs() << " busy " << fpu
.fpu_fsw
.busy
<< "\n";
9022 outs() << "\t fpu_ftw " << format("0x%02" PRIx32
, fpu
.fpu_ftw
);
9023 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32
, fpu
.fpu_rsrv1
);
9024 outs() << " fpu_fop " << format("0x%04" PRIx32
, fpu
.fpu_fop
);
9025 outs() << " fpu_ip " << format("0x%08" PRIx32
, fpu
.fpu_ip
) << "\n";
9026 outs() << "\t fpu_cs " << format("0x%04" PRIx32
, fpu
.fpu_cs
);
9027 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32
, fpu
.fpu_rsrv2
);
9028 outs() << " fpu_dp " << format("0x%08" PRIx32
, fpu
.fpu_dp
);
9029 outs() << " fpu_ds " << format("0x%04" PRIx32
, fpu
.fpu_ds
) << "\n";
9030 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32
, fpu
.fpu_rsrv3
);
9031 outs() << " fpu_mxcsr " << format("0x%08" PRIx32
, fpu
.fpu_mxcsr
);
9032 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32
, fpu
.fpu_mxcsrmask
);
9034 outs() << "\t fpu_stmm0:\n";
9035 Print_mmst_reg(fpu
.fpu_stmm0
);
9036 outs() << "\t fpu_stmm1:\n";
9037 Print_mmst_reg(fpu
.fpu_stmm1
);
9038 outs() << "\t fpu_stmm2:\n";
9039 Print_mmst_reg(fpu
.fpu_stmm2
);
9040 outs() << "\t fpu_stmm3:\n";
9041 Print_mmst_reg(fpu
.fpu_stmm3
);
9042 outs() << "\t fpu_stmm4:\n";
9043 Print_mmst_reg(fpu
.fpu_stmm4
);
9044 outs() << "\t fpu_stmm5:\n";
9045 Print_mmst_reg(fpu
.fpu_stmm5
);
9046 outs() << "\t fpu_stmm6:\n";
9047 Print_mmst_reg(fpu
.fpu_stmm6
);
9048 outs() << "\t fpu_stmm7:\n";
9049 Print_mmst_reg(fpu
.fpu_stmm7
);
9050 outs() << "\t fpu_xmm0:\n";
9051 Print_xmm_reg(fpu
.fpu_xmm0
);
9052 outs() << "\t fpu_xmm1:\n";
9053 Print_xmm_reg(fpu
.fpu_xmm1
);
9054 outs() << "\t fpu_xmm2:\n";
9055 Print_xmm_reg(fpu
.fpu_xmm2
);
9056 outs() << "\t fpu_xmm3:\n";
9057 Print_xmm_reg(fpu
.fpu_xmm3
);
9058 outs() << "\t fpu_xmm4:\n";
9059 Print_xmm_reg(fpu
.fpu_xmm4
);
9060 outs() << "\t fpu_xmm5:\n";
9061 Print_xmm_reg(fpu
.fpu_xmm5
);
9062 outs() << "\t fpu_xmm6:\n";
9063 Print_xmm_reg(fpu
.fpu_xmm6
);
9064 outs() << "\t fpu_xmm7:\n";
9065 Print_xmm_reg(fpu
.fpu_xmm7
);
9066 outs() << "\t fpu_xmm8:\n";
9067 Print_xmm_reg(fpu
.fpu_xmm8
);
9068 outs() << "\t fpu_xmm9:\n";
9069 Print_xmm_reg(fpu
.fpu_xmm9
);
9070 outs() << "\t fpu_xmm10:\n";
9071 Print_xmm_reg(fpu
.fpu_xmm10
);
9072 outs() << "\t fpu_xmm11:\n";
9073 Print_xmm_reg(fpu
.fpu_xmm11
);
9074 outs() << "\t fpu_xmm12:\n";
9075 Print_xmm_reg(fpu
.fpu_xmm12
);
9076 outs() << "\t fpu_xmm13:\n";
9077 Print_xmm_reg(fpu
.fpu_xmm13
);
9078 outs() << "\t fpu_xmm14:\n";
9079 Print_xmm_reg(fpu
.fpu_xmm14
);
9080 outs() << "\t fpu_xmm15:\n";
9081 Print_xmm_reg(fpu
.fpu_xmm15
);
9082 outs() << "\t fpu_rsrv4:\n";
9083 for (uint32_t f
= 0; f
< 6; f
++) {
9085 for (uint32_t g
= 0; g
< 16; g
++)
9086 outs() << format("%02" PRIx32
, fpu
.fpu_rsrv4
[f
* g
]) << " ";
9089 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32
, fpu
.fpu_reserved1
);
9093 static void Print_x86_exception_state_t(MachO::x86_exception_state64_t
&exc64
) {
9094 outs() << "\t trapno " << format("0x%08" PRIx32
, exc64
.trapno
);
9095 outs() << " err " << format("0x%08" PRIx32
, exc64
.err
);
9096 outs() << " faultvaddr " << format("0x%016" PRIx64
, exc64
.faultvaddr
) << "\n";
9099 static void Print_arm_thread_state32_t(MachO::arm_thread_state32_t
&cpu32
) {
9100 outs() << "\t r0 " << format("0x%08" PRIx32
, cpu32
.r
[0]);
9101 outs() << " r1 " << format("0x%08" PRIx32
, cpu32
.r
[1]);
9102 outs() << " r2 " << format("0x%08" PRIx32
, cpu32
.r
[2]);
9103 outs() << " r3 " << format("0x%08" PRIx32
, cpu32
.r
[3]) << "\n";
9104 outs() << "\t r4 " << format("0x%08" PRIx32
, cpu32
.r
[4]);
9105 outs() << " r5 " << format("0x%08" PRIx32
, cpu32
.r
[5]);
9106 outs() << " r6 " << format("0x%08" PRIx32
, cpu32
.r
[6]);
9107 outs() << " r7 " << format("0x%08" PRIx32
, cpu32
.r
[7]) << "\n";
9108 outs() << "\t r8 " << format("0x%08" PRIx32
, cpu32
.r
[8]);
9109 outs() << " r9 " << format("0x%08" PRIx32
, cpu32
.r
[9]);
9110 outs() << " r10 " << format("0x%08" PRIx32
, cpu32
.r
[10]);
9111 outs() << " r11 " << format("0x%08" PRIx32
, cpu32
.r
[11]) << "\n";
9112 outs() << "\t r12 " << format("0x%08" PRIx32
, cpu32
.r
[12]);
9113 outs() << " sp " << format("0x%08" PRIx32
, cpu32
.sp
);
9114 outs() << " lr " << format("0x%08" PRIx32
, cpu32
.lr
);
9115 outs() << " pc " << format("0x%08" PRIx32
, cpu32
.pc
) << "\n";
9116 outs() << "\t cpsr " << format("0x%08" PRIx32
, cpu32
.cpsr
) << "\n";
9119 static void Print_arm_thread_state64_t(MachO::arm_thread_state64_t
&cpu64
) {
9120 outs() << "\t x0 " << format("0x%016" PRIx64
, cpu64
.x
[0]);
9121 outs() << " x1 " << format("0x%016" PRIx64
, cpu64
.x
[1]);
9122 outs() << " x2 " << format("0x%016" PRIx64
, cpu64
.x
[2]) << "\n";
9123 outs() << "\t x3 " << format("0x%016" PRIx64
, cpu64
.x
[3]);
9124 outs() << " x4 " << format("0x%016" PRIx64
, cpu64
.x
[4]);
9125 outs() << " x5 " << format("0x%016" PRIx64
, cpu64
.x
[5]) << "\n";
9126 outs() << "\t x6 " << format("0x%016" PRIx64
, cpu64
.x
[6]);
9127 outs() << " x7 " << format("0x%016" PRIx64
, cpu64
.x
[7]);
9128 outs() << " x8 " << format("0x%016" PRIx64
, cpu64
.x
[8]) << "\n";
9129 outs() << "\t x9 " << format("0x%016" PRIx64
, cpu64
.x
[9]);
9130 outs() << " x10 " << format("0x%016" PRIx64
, cpu64
.x
[10]);
9131 outs() << " x11 " << format("0x%016" PRIx64
, cpu64
.x
[11]) << "\n";
9132 outs() << "\t x12 " << format("0x%016" PRIx64
, cpu64
.x
[12]);
9133 outs() << " x13 " << format("0x%016" PRIx64
, cpu64
.x
[13]);
9134 outs() << " x14 " << format("0x%016" PRIx64
, cpu64
.x
[14]) << "\n";
9135 outs() << "\t x15 " << format("0x%016" PRIx64
, cpu64
.x
[15]);
9136 outs() << " x16 " << format("0x%016" PRIx64
, cpu64
.x
[16]);
9137 outs() << " x17 " << format("0x%016" PRIx64
, cpu64
.x
[17]) << "\n";
9138 outs() << "\t x18 " << format("0x%016" PRIx64
, cpu64
.x
[18]);
9139 outs() << " x19 " << format("0x%016" PRIx64
, cpu64
.x
[19]);
9140 outs() << " x20 " << format("0x%016" PRIx64
, cpu64
.x
[20]) << "\n";
9141 outs() << "\t x21 " << format("0x%016" PRIx64
, cpu64
.x
[21]);
9142 outs() << " x22 " << format("0x%016" PRIx64
, cpu64
.x
[22]);
9143 outs() << " x23 " << format("0x%016" PRIx64
, cpu64
.x
[23]) << "\n";
9144 outs() << "\t x24 " << format("0x%016" PRIx64
, cpu64
.x
[24]);
9145 outs() << " x25 " << format("0x%016" PRIx64
, cpu64
.x
[25]);
9146 outs() << " x26 " << format("0x%016" PRIx64
, cpu64
.x
[26]) << "\n";
9147 outs() << "\t x27 " << format("0x%016" PRIx64
, cpu64
.x
[27]);
9148 outs() << " x28 " << format("0x%016" PRIx64
, cpu64
.x
[28]);
9149 outs() << " fp " << format("0x%016" PRIx64
, cpu64
.fp
) << "\n";
9150 outs() << "\t lr " << format("0x%016" PRIx64
, cpu64
.lr
);
9151 outs() << " sp " << format("0x%016" PRIx64
, cpu64
.sp
);
9152 outs() << " pc " << format("0x%016" PRIx64
, cpu64
.pc
) << "\n";
9153 outs() << "\t cpsr " << format("0x%08" PRIx32
, cpu64
.cpsr
) << "\n";
9156 static void PrintThreadCommand(MachO::thread_command t
, const char *Ptr
,
9157 bool isLittleEndian
, uint32_t cputype
) {
9158 if (t
.cmd
== MachO::LC_THREAD
)
9159 outs() << " cmd LC_THREAD\n";
9160 else if (t
.cmd
== MachO::LC_UNIXTHREAD
)
9161 outs() << " cmd LC_UNIXTHREAD\n";
9163 outs() << " cmd " << t
.cmd
<< " (unknown)\n";
9164 outs() << " cmdsize " << t
.cmdsize
;
9165 if (t
.cmdsize
< sizeof(struct MachO::thread_command
) + 2 * sizeof(uint32_t))
9166 outs() << " Incorrect size\n";
9170 const char *begin
= Ptr
+ sizeof(struct MachO::thread_command
);
9171 const char *end
= Ptr
+ t
.cmdsize
;
9172 uint32_t flavor
, count
, left
;
9173 if (cputype
== MachO::CPU_TYPE_I386
) {
9174 while (begin
< end
) {
9175 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9176 memcpy((char *)&flavor
, begin
, sizeof(uint32_t));
9177 begin
+= sizeof(uint32_t);
9182 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9183 sys::swapByteOrder(flavor
);
9184 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9185 memcpy((char *)&count
, begin
, sizeof(uint32_t));
9186 begin
+= sizeof(uint32_t);
9191 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9192 sys::swapByteOrder(count
);
9193 if (flavor
== MachO::x86_THREAD_STATE32
) {
9194 outs() << " flavor i386_THREAD_STATE\n";
9195 if (count
== MachO::x86_THREAD_STATE32_COUNT
)
9196 outs() << " count i386_THREAD_STATE_COUNT\n";
9198 outs() << " count " << count
9199 << " (not x86_THREAD_STATE32_COUNT)\n";
9200 MachO::x86_thread_state32_t cpu32
;
9202 if (left
>= sizeof(MachO::x86_thread_state32_t
)) {
9203 memcpy(&cpu32
, begin
, sizeof(MachO::x86_thread_state32_t
));
9204 begin
+= sizeof(MachO::x86_thread_state32_t
);
9206 memset(&cpu32
, '\0', sizeof(MachO::x86_thread_state32_t
));
9207 memcpy(&cpu32
, begin
, left
);
9210 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9212 Print_x86_thread_state32_t(cpu32
);
9213 } else if (flavor
== MachO::x86_THREAD_STATE
) {
9214 outs() << " flavor x86_THREAD_STATE\n";
9215 if (count
== MachO::x86_THREAD_STATE_COUNT
)
9216 outs() << " count x86_THREAD_STATE_COUNT\n";
9218 outs() << " count " << count
9219 << " (not x86_THREAD_STATE_COUNT)\n";
9220 struct MachO::x86_thread_state_t ts
;
9222 if (left
>= sizeof(MachO::x86_thread_state_t
)) {
9223 memcpy(&ts
, begin
, sizeof(MachO::x86_thread_state_t
));
9224 begin
+= sizeof(MachO::x86_thread_state_t
);
9226 memset(&ts
, '\0', sizeof(MachO::x86_thread_state_t
));
9227 memcpy(&ts
, begin
, left
);
9230 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9232 if (ts
.tsh
.flavor
== MachO::x86_THREAD_STATE32
) {
9233 outs() << "\t tsh.flavor x86_THREAD_STATE32 ";
9234 if (ts
.tsh
.count
== MachO::x86_THREAD_STATE32_COUNT
)
9235 outs() << "tsh.count x86_THREAD_STATE32_COUNT\n";
9237 outs() << "tsh.count " << ts
.tsh
.count
9238 << " (not x86_THREAD_STATE32_COUNT\n";
9239 Print_x86_thread_state32_t(ts
.uts
.ts32
);
9241 outs() << "\t tsh.flavor " << ts
.tsh
.flavor
<< " tsh.count "
9242 << ts
.tsh
.count
<< "\n";
9245 outs() << " flavor " << flavor
<< " (unknown)\n";
9246 outs() << " count " << count
<< "\n";
9247 outs() << " state (unknown)\n";
9248 begin
+= count
* sizeof(uint32_t);
9251 } else if (cputype
== MachO::CPU_TYPE_X86_64
) {
9252 while (begin
< end
) {
9253 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9254 memcpy((char *)&flavor
, begin
, sizeof(uint32_t));
9255 begin
+= sizeof(uint32_t);
9260 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9261 sys::swapByteOrder(flavor
);
9262 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9263 memcpy((char *)&count
, begin
, sizeof(uint32_t));
9264 begin
+= sizeof(uint32_t);
9269 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9270 sys::swapByteOrder(count
);
9271 if (flavor
== MachO::x86_THREAD_STATE64
) {
9272 outs() << " flavor x86_THREAD_STATE64\n";
9273 if (count
== MachO::x86_THREAD_STATE64_COUNT
)
9274 outs() << " count x86_THREAD_STATE64_COUNT\n";
9276 outs() << " count " << count
9277 << " (not x86_THREAD_STATE64_COUNT)\n";
9278 MachO::x86_thread_state64_t cpu64
;
9280 if (left
>= sizeof(MachO::x86_thread_state64_t
)) {
9281 memcpy(&cpu64
, begin
, sizeof(MachO::x86_thread_state64_t
));
9282 begin
+= sizeof(MachO::x86_thread_state64_t
);
9284 memset(&cpu64
, '\0', sizeof(MachO::x86_thread_state64_t
));
9285 memcpy(&cpu64
, begin
, left
);
9288 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9290 Print_x86_thread_state64_t(cpu64
);
9291 } else if (flavor
== MachO::x86_THREAD_STATE
) {
9292 outs() << " flavor x86_THREAD_STATE\n";
9293 if (count
== MachO::x86_THREAD_STATE_COUNT
)
9294 outs() << " count x86_THREAD_STATE_COUNT\n";
9296 outs() << " count " << count
9297 << " (not x86_THREAD_STATE_COUNT)\n";
9298 struct MachO::x86_thread_state_t ts
;
9300 if (left
>= sizeof(MachO::x86_thread_state_t
)) {
9301 memcpy(&ts
, begin
, sizeof(MachO::x86_thread_state_t
));
9302 begin
+= sizeof(MachO::x86_thread_state_t
);
9304 memset(&ts
, '\0', sizeof(MachO::x86_thread_state_t
));
9305 memcpy(&ts
, begin
, left
);
9308 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9310 if (ts
.tsh
.flavor
== MachO::x86_THREAD_STATE64
) {
9311 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
9312 if (ts
.tsh
.count
== MachO::x86_THREAD_STATE64_COUNT
)
9313 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
9315 outs() << "tsh.count " << ts
.tsh
.count
9316 << " (not x86_THREAD_STATE64_COUNT\n";
9317 Print_x86_thread_state64_t(ts
.uts
.ts64
);
9319 outs() << "\t tsh.flavor " << ts
.tsh
.flavor
<< " tsh.count "
9320 << ts
.tsh
.count
<< "\n";
9322 } else if (flavor
== MachO::x86_FLOAT_STATE
) {
9323 outs() << " flavor x86_FLOAT_STATE\n";
9324 if (count
== MachO::x86_FLOAT_STATE_COUNT
)
9325 outs() << " count x86_FLOAT_STATE_COUNT\n";
9327 outs() << " count " << count
<< " (not x86_FLOAT_STATE_COUNT)\n";
9328 struct MachO::x86_float_state_t fs
;
9330 if (left
>= sizeof(MachO::x86_float_state_t
)) {
9331 memcpy(&fs
, begin
, sizeof(MachO::x86_float_state_t
));
9332 begin
+= sizeof(MachO::x86_float_state_t
);
9334 memset(&fs
, '\0', sizeof(MachO::x86_float_state_t
));
9335 memcpy(&fs
, begin
, left
);
9338 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9340 if (fs
.fsh
.flavor
== MachO::x86_FLOAT_STATE64
) {
9341 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
9342 if (fs
.fsh
.count
== MachO::x86_FLOAT_STATE64_COUNT
)
9343 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
9345 outs() << "fsh.count " << fs
.fsh
.count
9346 << " (not x86_FLOAT_STATE64_COUNT\n";
9347 Print_x86_float_state_t(fs
.ufs
.fs64
);
9349 outs() << "\t fsh.flavor " << fs
.fsh
.flavor
<< " fsh.count "
9350 << fs
.fsh
.count
<< "\n";
9352 } else if (flavor
== MachO::x86_EXCEPTION_STATE
) {
9353 outs() << " flavor x86_EXCEPTION_STATE\n";
9354 if (count
== MachO::x86_EXCEPTION_STATE_COUNT
)
9355 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
9357 outs() << " count " << count
9358 << " (not x86_EXCEPTION_STATE_COUNT)\n";
9359 struct MachO::x86_exception_state_t es
;
9361 if (left
>= sizeof(MachO::x86_exception_state_t
)) {
9362 memcpy(&es
, begin
, sizeof(MachO::x86_exception_state_t
));
9363 begin
+= sizeof(MachO::x86_exception_state_t
);
9365 memset(&es
, '\0', sizeof(MachO::x86_exception_state_t
));
9366 memcpy(&es
, begin
, left
);
9369 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9371 if (es
.esh
.flavor
== MachO::x86_EXCEPTION_STATE64
) {
9372 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
9373 if (es
.esh
.count
== MachO::x86_EXCEPTION_STATE64_COUNT
)
9374 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
9376 outs() << "\t esh.count " << es
.esh
.count
9377 << " (not x86_EXCEPTION_STATE64_COUNT\n";
9378 Print_x86_exception_state_t(es
.ues
.es64
);
9380 outs() << "\t esh.flavor " << es
.esh
.flavor
<< " esh.count "
9381 << es
.esh
.count
<< "\n";
9383 } else if (flavor
== MachO::x86_EXCEPTION_STATE64
) {
9384 outs() << " flavor x86_EXCEPTION_STATE64\n";
9385 if (count
== MachO::x86_EXCEPTION_STATE64_COUNT
)
9386 outs() << " count x86_EXCEPTION_STATE64_COUNT\n";
9388 outs() << " count " << count
9389 << " (not x86_EXCEPTION_STATE64_COUNT)\n";
9390 struct MachO::x86_exception_state64_t es64
;
9392 if (left
>= sizeof(MachO::x86_exception_state64_t
)) {
9393 memcpy(&es64
, begin
, sizeof(MachO::x86_exception_state64_t
));
9394 begin
+= sizeof(MachO::x86_exception_state64_t
);
9396 memset(&es64
, '\0', sizeof(MachO::x86_exception_state64_t
));
9397 memcpy(&es64
, begin
, left
);
9400 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9402 Print_x86_exception_state_t(es64
);
9404 outs() << " flavor " << flavor
<< " (unknown)\n";
9405 outs() << " count " << count
<< "\n";
9406 outs() << " state (unknown)\n";
9407 begin
+= count
* sizeof(uint32_t);
9410 } else if (cputype
== MachO::CPU_TYPE_ARM
) {
9411 while (begin
< end
) {
9412 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9413 memcpy((char *)&flavor
, begin
, sizeof(uint32_t));
9414 begin
+= sizeof(uint32_t);
9419 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9420 sys::swapByteOrder(flavor
);
9421 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9422 memcpy((char *)&count
, begin
, sizeof(uint32_t));
9423 begin
+= sizeof(uint32_t);
9428 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9429 sys::swapByteOrder(count
);
9430 if (flavor
== MachO::ARM_THREAD_STATE
) {
9431 outs() << " flavor ARM_THREAD_STATE\n";
9432 if (count
== MachO::ARM_THREAD_STATE_COUNT
)
9433 outs() << " count ARM_THREAD_STATE_COUNT\n";
9435 outs() << " count " << count
9436 << " (not ARM_THREAD_STATE_COUNT)\n";
9437 MachO::arm_thread_state32_t cpu32
;
9439 if (left
>= sizeof(MachO::arm_thread_state32_t
)) {
9440 memcpy(&cpu32
, begin
, sizeof(MachO::arm_thread_state32_t
));
9441 begin
+= sizeof(MachO::arm_thread_state32_t
);
9443 memset(&cpu32
, '\0', sizeof(MachO::arm_thread_state32_t
));
9444 memcpy(&cpu32
, begin
, left
);
9447 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9449 Print_arm_thread_state32_t(cpu32
);
9451 outs() << " flavor " << flavor
<< " (unknown)\n";
9452 outs() << " count " << count
<< "\n";
9453 outs() << " state (unknown)\n";
9454 begin
+= count
* sizeof(uint32_t);
9457 } else if (cputype
== MachO::CPU_TYPE_ARM64
) {
9458 while (begin
< end
) {
9459 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9460 memcpy((char *)&flavor
, begin
, sizeof(uint32_t));
9461 begin
+= sizeof(uint32_t);
9466 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9467 sys::swapByteOrder(flavor
);
9468 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9469 memcpy((char *)&count
, begin
, sizeof(uint32_t));
9470 begin
+= sizeof(uint32_t);
9475 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9476 sys::swapByteOrder(count
);
9477 if (flavor
== MachO::ARM_THREAD_STATE64
) {
9478 outs() << " flavor ARM_THREAD_STATE64\n";
9479 if (count
== MachO::ARM_THREAD_STATE64_COUNT
)
9480 outs() << " count ARM_THREAD_STATE64_COUNT\n";
9482 outs() << " count " << count
9483 << " (not ARM_THREAD_STATE64_COUNT)\n";
9484 MachO::arm_thread_state64_t cpu64
;
9486 if (left
>= sizeof(MachO::arm_thread_state64_t
)) {
9487 memcpy(&cpu64
, begin
, sizeof(MachO::arm_thread_state64_t
));
9488 begin
+= sizeof(MachO::arm_thread_state64_t
);
9490 memset(&cpu64
, '\0', sizeof(MachO::arm_thread_state64_t
));
9491 memcpy(&cpu64
, begin
, left
);
9494 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9496 Print_arm_thread_state64_t(cpu64
);
9498 outs() << " flavor " << flavor
<< " (unknown)\n";
9499 outs() << " count " << count
<< "\n";
9500 outs() << " state (unknown)\n";
9501 begin
+= count
* sizeof(uint32_t);
9505 while (begin
< end
) {
9506 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9507 memcpy((char *)&flavor
, begin
, sizeof(uint32_t));
9508 begin
+= sizeof(uint32_t);
9513 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9514 sys::swapByteOrder(flavor
);
9515 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9516 memcpy((char *)&count
, begin
, sizeof(uint32_t));
9517 begin
+= sizeof(uint32_t);
9522 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9523 sys::swapByteOrder(count
);
9524 outs() << " flavor " << flavor
<< "\n";
9525 outs() << " count " << count
<< "\n";
9526 outs() << " state (Unknown cputype/cpusubtype)\n";
9527 begin
+= count
* sizeof(uint32_t);
9532 static void PrintDylibCommand(MachO::dylib_command dl
, const char *Ptr
) {
9533 if (dl
.cmd
== MachO::LC_ID_DYLIB
)
9534 outs() << " cmd LC_ID_DYLIB\n";
9535 else if (dl
.cmd
== MachO::LC_LOAD_DYLIB
)
9536 outs() << " cmd LC_LOAD_DYLIB\n";
9537 else if (dl
.cmd
== MachO::LC_LOAD_WEAK_DYLIB
)
9538 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
9539 else if (dl
.cmd
== MachO::LC_REEXPORT_DYLIB
)
9540 outs() << " cmd LC_REEXPORT_DYLIB\n";
9541 else if (dl
.cmd
== MachO::LC_LAZY_LOAD_DYLIB
)
9542 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
9543 else if (dl
.cmd
== MachO::LC_LOAD_UPWARD_DYLIB
)
9544 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
9546 outs() << " cmd " << dl
.cmd
<< " (unknown)\n";
9547 outs() << " cmdsize " << dl
.cmdsize
;
9548 if (dl
.cmdsize
< sizeof(struct MachO::dylib_command
))
9549 outs() << " Incorrect size\n";
9552 if (dl
.dylib
.name
< dl
.cmdsize
) {
9553 const char *P
= (const char *)(Ptr
) + dl
.dylib
.name
;
9554 outs() << " name " << P
<< " (offset " << dl
.dylib
.name
<< ")\n";
9556 outs() << " name ?(bad offset " << dl
.dylib
.name
<< ")\n";
9558 outs() << " time stamp " << dl
.dylib
.timestamp
<< " ";
9559 time_t t
= dl
.dylib
.timestamp
;
9560 outs() << ctime(&t
);
9561 outs() << " current version ";
9562 if (dl
.dylib
.current_version
== 0xffffffff)
9565 outs() << ((dl
.dylib
.current_version
>> 16) & 0xffff) << "."
9566 << ((dl
.dylib
.current_version
>> 8) & 0xff) << "."
9567 << (dl
.dylib
.current_version
& 0xff) << "\n";
9568 outs() << "compatibility version ";
9569 if (dl
.dylib
.compatibility_version
== 0xffffffff)
9572 outs() << ((dl
.dylib
.compatibility_version
>> 16) & 0xffff) << "."
9573 << ((dl
.dylib
.compatibility_version
>> 8) & 0xff) << "."
9574 << (dl
.dylib
.compatibility_version
& 0xff) << "\n";
9577 static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld
,
9578 uint32_t object_size
) {
9579 if (ld
.cmd
== MachO::LC_CODE_SIGNATURE
)
9580 outs() << " cmd LC_CODE_SIGNATURE\n";
9581 else if (ld
.cmd
== MachO::LC_SEGMENT_SPLIT_INFO
)
9582 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
9583 else if (ld
.cmd
== MachO::LC_FUNCTION_STARTS
)
9584 outs() << " cmd LC_FUNCTION_STARTS\n";
9585 else if (ld
.cmd
== MachO::LC_DATA_IN_CODE
)
9586 outs() << " cmd LC_DATA_IN_CODE\n";
9587 else if (ld
.cmd
== MachO::LC_DYLIB_CODE_SIGN_DRS
)
9588 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
9589 else if (ld
.cmd
== MachO::LC_LINKER_OPTIMIZATION_HINT
)
9590 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
9592 outs() << " cmd " << ld
.cmd
<< " (?)\n";
9593 outs() << " cmdsize " << ld
.cmdsize
;
9594 if (ld
.cmdsize
!= sizeof(struct MachO::linkedit_data_command
))
9595 outs() << " Incorrect size\n";
9598 outs() << " dataoff " << ld
.dataoff
;
9599 if (ld
.dataoff
> object_size
)
9600 outs() << " (past end of file)\n";
9603 outs() << " datasize " << ld
.datasize
;
9604 uint64_t big_size
= ld
.dataoff
;
9605 big_size
+= ld
.datasize
;
9606 if (big_size
> object_size
)
9607 outs() << " (past end of file)\n";
9612 static void PrintLoadCommands(const MachOObjectFile
*Obj
, uint32_t filetype
,
9613 uint32_t cputype
, bool verbose
) {
9614 StringRef Buf
= Obj
->getData();
9616 for (const auto &Command
: Obj
->load_commands()) {
9617 outs() << "Load command " << Index
++ << "\n";
9618 if (Command
.C
.cmd
== MachO::LC_SEGMENT
) {
9619 MachO::segment_command SLC
= Obj
->getSegmentLoadCommand(Command
);
9620 const char *sg_segname
= SLC
.segname
;
9621 PrintSegmentCommand(SLC
.cmd
, SLC
.cmdsize
, SLC
.segname
, SLC
.vmaddr
,
9622 SLC
.vmsize
, SLC
.fileoff
, SLC
.filesize
, SLC
.maxprot
,
9623 SLC
.initprot
, SLC
.nsects
, SLC
.flags
, Buf
.size(),
9625 for (unsigned j
= 0; j
< SLC
.nsects
; j
++) {
9626 MachO::section S
= Obj
->getSection(Command
, j
);
9627 PrintSection(S
.sectname
, S
.segname
, S
.addr
, S
.size
, S
.offset
, S
.align
,
9628 S
.reloff
, S
.nreloc
, S
.flags
, S
.reserved1
, S
.reserved2
,
9629 SLC
.cmd
, sg_segname
, filetype
, Buf
.size(), verbose
);
9631 } else if (Command
.C
.cmd
== MachO::LC_SEGMENT_64
) {
9632 MachO::segment_command_64 SLC_64
= Obj
->getSegment64LoadCommand(Command
);
9633 const char *sg_segname
= SLC_64
.segname
;
9634 PrintSegmentCommand(SLC_64
.cmd
, SLC_64
.cmdsize
, SLC_64
.segname
,
9635 SLC_64
.vmaddr
, SLC_64
.vmsize
, SLC_64
.fileoff
,
9636 SLC_64
.filesize
, SLC_64
.maxprot
, SLC_64
.initprot
,
9637 SLC_64
.nsects
, SLC_64
.flags
, Buf
.size(), verbose
);
9638 for (unsigned j
= 0; j
< SLC_64
.nsects
; j
++) {
9639 MachO::section_64 S_64
= Obj
->getSection64(Command
, j
);
9640 PrintSection(S_64
.sectname
, S_64
.segname
, S_64
.addr
, S_64
.size
,
9641 S_64
.offset
, S_64
.align
, S_64
.reloff
, S_64
.nreloc
,
9642 S_64
.flags
, S_64
.reserved1
, S_64
.reserved2
, SLC_64
.cmd
,
9643 sg_segname
, filetype
, Buf
.size(), verbose
);
9645 } else if (Command
.C
.cmd
== MachO::LC_SYMTAB
) {
9646 MachO::symtab_command Symtab
= Obj
->getSymtabLoadCommand();
9647 PrintSymtabLoadCommand(Symtab
, Obj
->is64Bit(), Buf
.size());
9648 } else if (Command
.C
.cmd
== MachO::LC_DYSYMTAB
) {
9649 MachO::dysymtab_command Dysymtab
= Obj
->getDysymtabLoadCommand();
9650 MachO::symtab_command Symtab
= Obj
->getSymtabLoadCommand();
9651 PrintDysymtabLoadCommand(Dysymtab
, Symtab
.nsyms
, Buf
.size(),
9653 } else if (Command
.C
.cmd
== MachO::LC_DYLD_INFO
||
9654 Command
.C
.cmd
== MachO::LC_DYLD_INFO_ONLY
) {
9655 MachO::dyld_info_command DyldInfo
= Obj
->getDyldInfoLoadCommand(Command
);
9656 PrintDyldInfoLoadCommand(DyldInfo
, Buf
.size());
9657 } else if (Command
.C
.cmd
== MachO::LC_LOAD_DYLINKER
||
9658 Command
.C
.cmd
== MachO::LC_ID_DYLINKER
||
9659 Command
.C
.cmd
== MachO::LC_DYLD_ENVIRONMENT
) {
9660 MachO::dylinker_command Dyld
= Obj
->getDylinkerCommand(Command
);
9661 PrintDyldLoadCommand(Dyld
, Command
.Ptr
);
9662 } else if (Command
.C
.cmd
== MachO::LC_UUID
) {
9663 MachO::uuid_command Uuid
= Obj
->getUuidCommand(Command
);
9664 PrintUuidLoadCommand(Uuid
);
9665 } else if (Command
.C
.cmd
== MachO::LC_RPATH
) {
9666 MachO::rpath_command Rpath
= Obj
->getRpathCommand(Command
);
9667 PrintRpathLoadCommand(Rpath
, Command
.Ptr
);
9668 } else if (Command
.C
.cmd
== MachO::LC_VERSION_MIN_MACOSX
||
9669 Command
.C
.cmd
== MachO::LC_VERSION_MIN_IPHONEOS
||
9670 Command
.C
.cmd
== MachO::LC_VERSION_MIN_TVOS
||
9671 Command
.C
.cmd
== MachO::LC_VERSION_MIN_WATCHOS
) {
9672 MachO::version_min_command Vd
= Obj
->getVersionMinLoadCommand(Command
);
9673 PrintVersionMinLoadCommand(Vd
);
9674 } else if (Command
.C
.cmd
== MachO::LC_NOTE
) {
9675 MachO::note_command Nt
= Obj
->getNoteLoadCommand(Command
);
9676 PrintNoteLoadCommand(Nt
);
9677 } else if (Command
.C
.cmd
== MachO::LC_BUILD_VERSION
) {
9678 MachO::build_version_command Bv
=
9679 Obj
->getBuildVersionLoadCommand(Command
);
9680 PrintBuildVersionLoadCommand(Obj
, Bv
);
9681 } else if (Command
.C
.cmd
== MachO::LC_SOURCE_VERSION
) {
9682 MachO::source_version_command Sd
= Obj
->getSourceVersionCommand(Command
);
9683 PrintSourceVersionCommand(Sd
);
9684 } else if (Command
.C
.cmd
== MachO::LC_MAIN
) {
9685 MachO::entry_point_command Ep
= Obj
->getEntryPointCommand(Command
);
9686 PrintEntryPointCommand(Ep
);
9687 } else if (Command
.C
.cmd
== MachO::LC_ENCRYPTION_INFO
) {
9688 MachO::encryption_info_command Ei
=
9689 Obj
->getEncryptionInfoCommand(Command
);
9690 PrintEncryptionInfoCommand(Ei
, Buf
.size());
9691 } else if (Command
.C
.cmd
== MachO::LC_ENCRYPTION_INFO_64
) {
9692 MachO::encryption_info_command_64 Ei
=
9693 Obj
->getEncryptionInfoCommand64(Command
);
9694 PrintEncryptionInfoCommand64(Ei
, Buf
.size());
9695 } else if (Command
.C
.cmd
== MachO::LC_LINKER_OPTION
) {
9696 MachO::linker_option_command Lo
=
9697 Obj
->getLinkerOptionLoadCommand(Command
);
9698 PrintLinkerOptionCommand(Lo
, Command
.Ptr
);
9699 } else if (Command
.C
.cmd
== MachO::LC_SUB_FRAMEWORK
) {
9700 MachO::sub_framework_command Sf
= Obj
->getSubFrameworkCommand(Command
);
9701 PrintSubFrameworkCommand(Sf
, Command
.Ptr
);
9702 } else if (Command
.C
.cmd
== MachO::LC_SUB_UMBRELLA
) {
9703 MachO::sub_umbrella_command Sf
= Obj
->getSubUmbrellaCommand(Command
);
9704 PrintSubUmbrellaCommand(Sf
, Command
.Ptr
);
9705 } else if (Command
.C
.cmd
== MachO::LC_SUB_LIBRARY
) {
9706 MachO::sub_library_command Sl
= Obj
->getSubLibraryCommand(Command
);
9707 PrintSubLibraryCommand(Sl
, Command
.Ptr
);
9708 } else if (Command
.C
.cmd
== MachO::LC_SUB_CLIENT
) {
9709 MachO::sub_client_command Sc
= Obj
->getSubClientCommand(Command
);
9710 PrintSubClientCommand(Sc
, Command
.Ptr
);
9711 } else if (Command
.C
.cmd
== MachO::LC_ROUTINES
) {
9712 MachO::routines_command Rc
= Obj
->getRoutinesCommand(Command
);
9713 PrintRoutinesCommand(Rc
);
9714 } else if (Command
.C
.cmd
== MachO::LC_ROUTINES_64
) {
9715 MachO::routines_command_64 Rc
= Obj
->getRoutinesCommand64(Command
);
9716 PrintRoutinesCommand64(Rc
);
9717 } else if (Command
.C
.cmd
== MachO::LC_THREAD
||
9718 Command
.C
.cmd
== MachO::LC_UNIXTHREAD
) {
9719 MachO::thread_command Tc
= Obj
->getThreadCommand(Command
);
9720 PrintThreadCommand(Tc
, Command
.Ptr
, Obj
->isLittleEndian(), cputype
);
9721 } else if (Command
.C
.cmd
== MachO::LC_LOAD_DYLIB
||
9722 Command
.C
.cmd
== MachO::LC_ID_DYLIB
||
9723 Command
.C
.cmd
== MachO::LC_LOAD_WEAK_DYLIB
||
9724 Command
.C
.cmd
== MachO::LC_REEXPORT_DYLIB
||
9725 Command
.C
.cmd
== MachO::LC_LAZY_LOAD_DYLIB
||
9726 Command
.C
.cmd
== MachO::LC_LOAD_UPWARD_DYLIB
) {
9727 MachO::dylib_command Dl
= Obj
->getDylibIDLoadCommand(Command
);
9728 PrintDylibCommand(Dl
, Command
.Ptr
);
9729 } else if (Command
.C
.cmd
== MachO::LC_CODE_SIGNATURE
||
9730 Command
.C
.cmd
== MachO::LC_SEGMENT_SPLIT_INFO
||
9731 Command
.C
.cmd
== MachO::LC_FUNCTION_STARTS
||
9732 Command
.C
.cmd
== MachO::LC_DATA_IN_CODE
||
9733 Command
.C
.cmd
== MachO::LC_DYLIB_CODE_SIGN_DRS
||
9734 Command
.C
.cmd
== MachO::LC_LINKER_OPTIMIZATION_HINT
) {
9735 MachO::linkedit_data_command Ld
=
9736 Obj
->getLinkeditDataLoadCommand(Command
);
9737 PrintLinkEditDataCommand(Ld
, Buf
.size());
9739 outs() << " cmd ?(" << format("0x%08" PRIx32
, Command
.C
.cmd
)
9741 outs() << " cmdsize " << Command
.C
.cmdsize
<< "\n";
9742 // TODO: get and print the raw bytes of the load command.
9744 // TODO: print all the other kinds of load commands.
9748 static void PrintMachHeader(const MachOObjectFile
*Obj
, bool verbose
) {
9749 if (Obj
->is64Bit()) {
9750 MachO::mach_header_64 H_64
;
9751 H_64
= Obj
->getHeader64();
9752 PrintMachHeader(H_64
.magic
, H_64
.cputype
, H_64
.cpusubtype
, H_64
.filetype
,
9753 H_64
.ncmds
, H_64
.sizeofcmds
, H_64
.flags
, verbose
);
9755 MachO::mach_header H
;
9756 H
= Obj
->getHeader();
9757 PrintMachHeader(H
.magic
, H
.cputype
, H
.cpusubtype
, H
.filetype
, H
.ncmds
,
9758 H
.sizeofcmds
, H
.flags
, verbose
);
9762 void llvm::printMachOFileHeader(const object::ObjectFile
*Obj
) {
9763 const MachOObjectFile
*file
= dyn_cast
<const MachOObjectFile
>(Obj
);
9764 PrintMachHeader(file
, !NonVerbose
);
9767 void llvm::printMachOLoadCommands(const object::ObjectFile
*Obj
) {
9768 const MachOObjectFile
*file
= dyn_cast
<const MachOObjectFile
>(Obj
);
9769 uint32_t filetype
= 0;
9770 uint32_t cputype
= 0;
9771 if (file
->is64Bit()) {
9772 MachO::mach_header_64 H_64
;
9773 H_64
= file
->getHeader64();
9774 filetype
= H_64
.filetype
;
9775 cputype
= H_64
.cputype
;
9777 MachO::mach_header H
;
9778 H
= file
->getHeader();
9779 filetype
= H
.filetype
;
9780 cputype
= H
.cputype
;
9782 PrintLoadCommands(file
, filetype
, cputype
, !NonVerbose
);
9785 //===----------------------------------------------------------------------===//
9786 // export trie dumping
9787 //===----------------------------------------------------------------------===//
9789 void llvm::printMachOExportsTrie(const object::MachOObjectFile
*Obj
) {
9790 uint64_t BaseSegmentAddress
= 0;
9791 for (const auto &Command
: Obj
->load_commands()) {
9792 if (Command
.C
.cmd
== MachO::LC_SEGMENT
) {
9793 MachO::segment_command Seg
= Obj
->getSegmentLoadCommand(Command
);
9794 if (Seg
.fileoff
== 0 && Seg
.filesize
!= 0) {
9795 BaseSegmentAddress
= Seg
.vmaddr
;
9798 } else if (Command
.C
.cmd
== MachO::LC_SEGMENT_64
) {
9799 MachO::segment_command_64 Seg
= Obj
->getSegment64LoadCommand(Command
);
9800 if (Seg
.fileoff
== 0 && Seg
.filesize
!= 0) {
9801 BaseSegmentAddress
= Seg
.vmaddr
;
9806 Error Err
= Error::success();
9807 for (const llvm::object::ExportEntry
&Entry
: Obj
->exports(Err
)) {
9808 uint64_t Flags
= Entry
.flags();
9809 bool ReExport
= (Flags
& MachO::EXPORT_SYMBOL_FLAGS_REEXPORT
);
9810 bool WeakDef
= (Flags
& MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION
);
9811 bool ThreadLocal
= ((Flags
& MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK
) ==
9812 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL
);
9813 bool Abs
= ((Flags
& MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK
) ==
9814 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE
);
9815 bool Resolver
= (Flags
& MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER
);
9817 outs() << "[re-export] ";
9819 outs() << format("0x%08llX ",
9820 Entry
.address() + BaseSegmentAddress
);
9821 outs() << Entry
.name();
9822 if (WeakDef
|| ThreadLocal
|| Resolver
|| Abs
) {
9823 bool NeedsComma
= false;
9826 outs() << "weak_def";
9832 outs() << "per-thread";
9838 outs() << "absolute";
9844 outs() << format("resolver=0x%08llX", Entry
.other());
9850 StringRef DylibName
= "unknown";
9851 int Ordinal
= Entry
.other() - 1;
9852 Obj
->getLibraryShortNameByIndex(Ordinal
, DylibName
);
9853 if (Entry
.otherName().empty())
9854 outs() << " (from " << DylibName
<< ")";
9856 outs() << " (" << Entry
.otherName() << " from " << DylibName
<< ")";
9861 report_error(Obj
->getFileName(), std::move(Err
));
9864 //===----------------------------------------------------------------------===//
9865 // rebase table dumping
9866 //===----------------------------------------------------------------------===//
9868 void llvm::printMachORebaseTable(object::MachOObjectFile
*Obj
) {
9869 outs() << "segment section address type\n";
9870 Error Err
= Error::success();
9871 for (const llvm::object::MachORebaseEntry
&Entry
: Obj
->rebaseTable(Err
)) {
9872 StringRef SegmentName
= Entry
.segmentName();
9873 StringRef SectionName
= Entry
.sectionName();
9874 uint64_t Address
= Entry
.address();
9876 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
9877 outs() << format("%-8s %-18s 0x%08" PRIX64
" %s\n",
9878 SegmentName
.str().c_str(), SectionName
.str().c_str(),
9879 Address
, Entry
.typeName().str().c_str());
9882 report_error(Obj
->getFileName(), std::move(Err
));
9885 static StringRef
ordinalName(const object::MachOObjectFile
*Obj
, int Ordinal
) {
9886 StringRef DylibName
;
9888 case MachO::BIND_SPECIAL_DYLIB_SELF
:
9889 return "this-image";
9890 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE
:
9891 return "main-executable";
9892 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP
:
9893 return "flat-namespace";
9896 std::error_code EC
=
9897 Obj
->getLibraryShortNameByIndex(Ordinal
- 1, DylibName
);
9899 return "<<bad library ordinal>>";
9903 return "<<unknown special ordinal>>";
9906 //===----------------------------------------------------------------------===//
9907 // bind table dumping
9908 //===----------------------------------------------------------------------===//
9910 void llvm::printMachOBindTable(object::MachOObjectFile
*Obj
) {
9911 // Build table of sections so names can used in final output.
9912 outs() << "segment section address type "
9913 "addend dylib symbol\n";
9914 Error Err
= Error::success();
9915 for (const llvm::object::MachOBindEntry
&Entry
: Obj
->bindTable(Err
)) {
9916 StringRef SegmentName
= Entry
.segmentName();
9917 StringRef SectionName
= Entry
.sectionName();
9918 uint64_t Address
= Entry
.address();
9920 // Table lines look like:
9921 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
9923 if (Entry
.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT
)
9924 Attr
= " (weak_import)";
9925 outs() << left_justify(SegmentName
, 8) << " "
9926 << left_justify(SectionName
, 18) << " "
9927 << format_hex(Address
, 10, true) << " "
9928 << left_justify(Entry
.typeName(), 8) << " "
9929 << format_decimal(Entry
.addend(), 8) << " "
9930 << left_justify(ordinalName(Obj
, Entry
.ordinal()), 16) << " "
9931 << Entry
.symbolName() << Attr
<< "\n";
9934 report_error(Obj
->getFileName(), std::move(Err
));
9937 //===----------------------------------------------------------------------===//
9938 // lazy bind table dumping
9939 //===----------------------------------------------------------------------===//
9941 void llvm::printMachOLazyBindTable(object::MachOObjectFile
*Obj
) {
9942 outs() << "segment section address "
9944 Error Err
= Error::success();
9945 for (const llvm::object::MachOBindEntry
&Entry
: Obj
->lazyBindTable(Err
)) {
9946 StringRef SegmentName
= Entry
.segmentName();
9947 StringRef SectionName
= Entry
.sectionName();
9948 uint64_t Address
= Entry
.address();
9950 // Table lines look like:
9951 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
9952 outs() << left_justify(SegmentName
, 8) << " "
9953 << left_justify(SectionName
, 18) << " "
9954 << format_hex(Address
, 10, true) << " "
9955 << left_justify(ordinalName(Obj
, Entry
.ordinal()), 16) << " "
9956 << Entry
.symbolName() << "\n";
9959 report_error(Obj
->getFileName(), std::move(Err
));
9962 //===----------------------------------------------------------------------===//
9963 // weak bind table dumping
9964 //===----------------------------------------------------------------------===//
9966 void llvm::printMachOWeakBindTable(object::MachOObjectFile
*Obj
) {
9967 outs() << "segment section address "
9968 "type addend symbol\n";
9969 Error Err
= Error::success();
9970 for (const llvm::object::MachOBindEntry
&Entry
: Obj
->weakBindTable(Err
)) {
9971 // Strong symbols don't have a location to update.
9972 if (Entry
.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION
) {
9973 outs() << " strong "
9974 << Entry
.symbolName() << "\n";
9977 StringRef SegmentName
= Entry
.segmentName();
9978 StringRef SectionName
= Entry
.sectionName();
9979 uint64_t Address
= Entry
.address();
9981 // Table lines look like:
9982 // __DATA __data 0x00001000 pointer 0 _foo
9983 outs() << left_justify(SegmentName
, 8) << " "
9984 << left_justify(SectionName
, 18) << " "
9985 << format_hex(Address
, 10, true) << " "
9986 << left_justify(Entry
.typeName(), 8) << " "
9987 << format_decimal(Entry
.addend(), 8) << " " << Entry
.symbolName()
9991 report_error(Obj
->getFileName(), std::move(Err
));
9994 // get_dyld_bind_info_symbolname() is used for disassembly and passed an
9995 // address, ReferenceValue, in the Mach-O file and looks in the dyld bind
9996 // information for that address. If the address is found its binding symbol
9997 // name is returned. If not nullptr is returned.
9998 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue
,
9999 struct DisassembleInfo
*info
) {
10000 if (info
->bindtable
== nullptr) {
10001 info
->bindtable
= llvm::make_unique
<SymbolAddressMap
>();
10002 Error Err
= Error::success();
10003 for (const llvm::object::MachOBindEntry
&Entry
: info
->O
->bindTable(Err
)) {
10004 uint64_t Address
= Entry
.address();
10005 StringRef name
= Entry
.symbolName();
10007 (*info
->bindtable
)[Address
] = name
;
10010 report_error(info
->O
->getFileName(), std::move(Err
));
10012 auto name
= info
->bindtable
->lookup(ReferenceValue
);
10013 return !name
.empty() ? name
.data() : nullptr;