1 //===-- MachODump.cpp - Object file dumping utility for llvm --------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the MachO-specific dumper for llvm-objdump.
11 //===----------------------------------------------------------------------===//
13 #include "llvm-objdump.h"
14 #include "llvm-c/Disassembler.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/Triple.h"
18 #include "llvm/BinaryFormat/MachO.h"
19 #include "llvm/Config/config.h"
20 #include "llvm/DebugInfo/DIContext.h"
21 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
22 #include "llvm/Demangle/Demangle.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/MC/MCContext.h"
25 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
26 #include "llvm/MC/MCInst.h"
27 #include "llvm/MC/MCInstPrinter.h"
28 #include "llvm/MC/MCInstrDesc.h"
29 #include "llvm/MC/MCInstrInfo.h"
30 #include "llvm/MC/MCRegisterInfo.h"
31 #include "llvm/MC/MCSubtargetInfo.h"
32 #include "llvm/Object/MachO.h"
33 #include "llvm/Object/MachOUniversal.h"
34 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/Endian.h"
38 #include "llvm/Support/Format.h"
39 #include "llvm/Support/FormattedStream.h"
40 #include "llvm/Support/GraphWriter.h"
41 #include "llvm/Support/LEB128.h"
42 #include "llvm/Support/MemoryBuffer.h"
43 #include "llvm/Support/TargetRegistry.h"
44 #include "llvm/Support/TargetSelect.h"
45 #include "llvm/Support/ToolOutputFile.h"
46 #include "llvm/Support/WithColor.h"
47 #include "llvm/Support/raw_ostream.h"
50 #include <system_error>
58 using namespace llvm::object
;
62 cl::OptionCategory
MachOCat("llvm-objdump MachO Specific Options");
64 extern cl::opt
<bool> ArchiveHeaders
;
65 extern cl::opt
<bool> Disassemble
;
66 extern cl::opt
<bool> DisassembleAll
;
67 extern cl::opt
<DIDumpType
> DwarfDumpType
;
68 extern cl::list
<std::string
> FilterSections
;
69 extern cl::list
<std::string
> MAttrs
;
70 extern cl::opt
<std::string
> MCPU
;
71 extern cl::opt
<bool> NoShowRawInsn
;
72 extern cl::opt
<bool> NoLeadingAddr
;
73 extern cl::opt
<bool> PrintImmHex
;
74 extern cl::opt
<bool> PrivateHeaders
;
75 extern cl::opt
<bool> Relocations
;
76 extern cl::opt
<bool> SectionHeaders
;
77 extern cl::opt
<bool> SectionContents
;
78 extern cl::opt
<bool> SymbolTable
;
79 extern cl::opt
<std::string
> TripleName
;
80 extern cl::opt
<bool> UnwindInfo
;
83 FirstPrivateHeader("private-header",
84 cl::desc("Display only the first format specific file "
88 cl::opt
<bool> ExportsTrie("exports-trie",
89 cl::desc("Display mach-o exported symbols"),
92 cl::opt
<bool> Rebase("rebase", cl::desc("Display mach-o rebasing info"),
95 cl::opt
<bool> Bind("bind", cl::desc("Display mach-o binding info"),
98 cl::opt
<bool> LazyBind("lazy-bind",
99 cl::desc("Display mach-o lazy binding info"),
102 cl::opt
<bool> WeakBind("weak-bind",
103 cl::desc("Display mach-o weak binding info"),
107 UseDbg("g", cl::Grouping
,
108 cl::desc("Print line information from debug info if available"),
111 static cl::opt
<std::string
> DSYMFile("dsym",
112 cl::desc("Use .dSYM file for debug info"),
115 static cl::opt
<bool> FullLeadingAddr("full-leading-addr",
116 cl::desc("Print full leading address"),
119 static cl::opt
<bool> NoLeadingHeaders("no-leading-headers",
120 cl::desc("Print no leading headers"),
123 cl::opt
<bool> UniversalHeaders("universal-headers",
124 cl::desc("Print Mach-O universal headers "
125 "(requires -macho)"),
129 ArchiveMemberOffsets("archive-member-offsets",
130 cl::desc("Print the offset to each archive member for "
131 "Mach-O archives (requires -macho and "
132 "-archive-headers)"),
135 cl::opt
<bool> IndirectSymbols("indirect-symbols",
136 cl::desc("Print indirect symbol table for Mach-O "
137 "objects (requires -macho)"),
141 DataInCode("data-in-code",
142 cl::desc("Print the data in code table for Mach-O objects "
143 "(requires -macho)"),
146 cl::opt
<bool> LinkOptHints("link-opt-hints",
147 cl::desc("Print the linker optimization hints for "
148 "Mach-O objects (requires -macho)"),
151 cl::opt
<bool> InfoPlist("info-plist",
152 cl::desc("Print the info plist section as strings for "
153 "Mach-O objects (requires -macho)"),
156 cl::opt
<bool> DylibsUsed("dylibs-used",
157 cl::desc("Print the shared libraries used for linked "
158 "Mach-O files (requires -macho)"),
163 cl::desc("Print the shared library's id for the dylib Mach-O "
164 "file (requires -macho)"),
168 NonVerbose("non-verbose",
169 cl::desc("Print the info for Mach-O objects in "
170 "non-verbose or numeric form (requires -macho)"),
174 ObjcMetaData("objc-meta-data",
175 cl::desc("Print the Objective-C runtime meta data for "
176 "Mach-O files (requires -macho)"),
179 cl::opt
<std::string
> DisSymName(
181 cl::desc("disassemble just this symbol's instructions (requires -macho)"),
184 static cl::opt
<bool> NoSymbolicOperands(
185 "no-symbolic-operands",
186 cl::desc("do not symbolic operands when disassembling (requires -macho)"),
189 static cl::list
<std::string
>
190 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
191 cl::ZeroOrMore
, cl::cat(MachOCat
));
193 bool ArchAll
= false;
195 static std::string ThumbTripleName
;
197 static const Target
*GetTarget(const MachOObjectFile
*MachOObj
,
198 const char **McpuDefault
,
199 const Target
**ThumbTarget
) {
200 // Figure out the target triple.
201 Triple
TT(TripleName
);
202 if (TripleName
.empty()) {
203 TT
= MachOObj
->getArchTriple(McpuDefault
);
204 TripleName
= TT
.str();
207 if (TT
.getArch() == Triple::arm
) {
208 // We've inferred a 32-bit ARM target from the object file. All MachO CPUs
209 // that support ARM are also capable of Thumb mode.
210 Triple ThumbTriple
= TT
;
211 std::string ThumbName
= (Twine("thumb") + TT
.getArchName().substr(3)).str();
212 ThumbTriple
.setArchName(ThumbName
);
213 ThumbTripleName
= ThumbTriple
.str();
216 // Get the target specific parser.
218 const Target
*TheTarget
= TargetRegistry::lookupTarget(TripleName
, Error
);
219 if (TheTarget
&& ThumbTripleName
.empty())
222 *ThumbTarget
= TargetRegistry::lookupTarget(ThumbTripleName
, Error
);
226 WithColor::error(errs(), "llvm-objdump") << "unable to get target for '";
228 errs() << TripleName
;
230 errs() << ThumbTripleName
;
231 errs() << "', see --version and --triple.\n";
235 struct SymbolSorter
{
236 bool operator()(const SymbolRef
&A
, const SymbolRef
&B
) {
237 Expected
<SymbolRef::Type
> ATypeOrErr
= A
.getType();
239 reportError(ATypeOrErr
.takeError(), A
.getObject()->getFileName());
240 SymbolRef::Type AType
= *ATypeOrErr
;
241 Expected
<SymbolRef::Type
> BTypeOrErr
= B
.getType();
243 reportError(BTypeOrErr
.takeError(), B
.getObject()->getFileName());
244 SymbolRef::Type BType
= *BTypeOrErr
;
245 uint64_t AAddr
= (AType
!= SymbolRef::ST_Function
) ? 0 : A
.getValue();
246 uint64_t BAddr
= (BType
!= SymbolRef::ST_Function
) ? 0 : B
.getValue();
247 return AAddr
< BAddr
;
251 // Types for the storted data in code table that is built before disassembly
252 // and the predicate function to sort them.
253 typedef std::pair
<uint64_t, DiceRef
> DiceTableEntry
;
254 typedef std::vector
<DiceTableEntry
> DiceTable
;
255 typedef DiceTable::iterator dice_table_iterator
;
259 struct ScopedXarFile
{
261 ScopedXarFile(const char *filename
, int32_t flags
)
262 : xar(xar_open(filename
, flags
)) {}
267 ScopedXarFile(const ScopedXarFile
&) = delete;
268 ScopedXarFile
&operator=(const ScopedXarFile
&) = delete;
269 operator xar_t() { return xar
; }
272 struct ScopedXarIter
{
274 ScopedXarIter() : iter(xar_iter_new()) {}
279 ScopedXarIter(const ScopedXarIter
&) = delete;
280 ScopedXarIter
&operator=(const ScopedXarIter
&) = delete;
281 operator xar_iter_t() { return iter
; }
284 #endif // defined(HAVE_LIBXAR)
286 // This is used to search for a data in code table entry for the PC being
287 // disassembled. The j parameter has the PC in j.first. A single data in code
288 // table entry can cover many bytes for each of its Kind's. So if the offset,
289 // aka the i.first value, of the data in code table entry plus its Length
290 // covers the PC being searched for this will return true. If not it will
292 static bool compareDiceTableEntries(const DiceTableEntry
&i
,
293 const DiceTableEntry
&j
) {
295 i
.second
.getLength(Length
);
297 return j
.first
>= i
.first
&& j
.first
< i
.first
+ Length
;
300 static uint64_t DumpDataInCode(const uint8_t *bytes
, uint64_t Length
,
301 unsigned short Kind
) {
302 uint32_t Value
, Size
= 1;
306 case MachO::DICE_KIND_DATA
:
309 dumpBytes(makeArrayRef(bytes
, 4), outs());
310 Value
= bytes
[3] << 24 | bytes
[2] << 16 | bytes
[1] << 8 | bytes
[0];
311 outs() << "\t.long " << Value
;
313 } else if (Length
>= 2) {
315 dumpBytes(makeArrayRef(bytes
, 2), outs());
316 Value
= bytes
[1] << 8 | bytes
[0];
317 outs() << "\t.short " << Value
;
321 dumpBytes(makeArrayRef(bytes
, 2), outs());
323 outs() << "\t.byte " << Value
;
326 if (Kind
== MachO::DICE_KIND_DATA
)
327 outs() << "\t@ KIND_DATA\n";
329 outs() << "\t@ data in code kind = " << Kind
<< "\n";
331 case MachO::DICE_KIND_JUMP_TABLE8
:
333 dumpBytes(makeArrayRef(bytes
, 1), outs());
335 outs() << "\t.byte " << format("%3u", Value
) << "\t@ KIND_JUMP_TABLE8\n";
338 case MachO::DICE_KIND_JUMP_TABLE16
:
340 dumpBytes(makeArrayRef(bytes
, 2), outs());
341 Value
= bytes
[1] << 8 | bytes
[0];
342 outs() << "\t.short " << format("%5u", Value
& 0xffff)
343 << "\t@ KIND_JUMP_TABLE16\n";
346 case MachO::DICE_KIND_JUMP_TABLE32
:
347 case MachO::DICE_KIND_ABS_JUMP_TABLE32
:
349 dumpBytes(makeArrayRef(bytes
, 4), outs());
350 Value
= bytes
[3] << 24 | bytes
[2] << 16 | bytes
[1] << 8 | bytes
[0];
351 outs() << "\t.long " << Value
;
352 if (Kind
== MachO::DICE_KIND_JUMP_TABLE32
)
353 outs() << "\t@ KIND_JUMP_TABLE32\n";
355 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
362 static void getSectionsAndSymbols(MachOObjectFile
*MachOObj
,
363 std::vector
<SectionRef
> &Sections
,
364 std::vector
<SymbolRef
> &Symbols
,
365 SmallVectorImpl
<uint64_t> &FoundFns
,
366 uint64_t &BaseSegmentAddress
) {
367 const StringRef FileName
= MachOObj
->getFileName();
368 for (const SymbolRef
&Symbol
: MachOObj
->symbols()) {
369 StringRef SymName
= unwrapOrError(Symbol
.getName(), FileName
);
370 if (!SymName
.startswith("ltmp"))
371 Symbols
.push_back(Symbol
);
374 for (const SectionRef
&Section
: MachOObj
->sections())
375 Sections
.push_back(Section
);
377 bool BaseSegmentAddressSet
= false;
378 for (const auto &Command
: MachOObj
->load_commands()) {
379 if (Command
.C
.cmd
== MachO::LC_FUNCTION_STARTS
) {
380 // We found a function starts segment, parse the addresses for later
382 MachO::linkedit_data_command LLC
=
383 MachOObj
->getLinkeditDataLoadCommand(Command
);
385 MachOObj
->ReadULEB128s(LLC
.dataoff
, FoundFns
);
386 } else if (Command
.C
.cmd
== MachO::LC_SEGMENT
) {
387 MachO::segment_command SLC
= MachOObj
->getSegmentLoadCommand(Command
);
388 StringRef SegName
= SLC
.segname
;
389 if (!BaseSegmentAddressSet
&& SegName
!= "__PAGEZERO") {
390 BaseSegmentAddressSet
= true;
391 BaseSegmentAddress
= SLC
.vmaddr
;
393 } else if (Command
.C
.cmd
== MachO::LC_SEGMENT_64
) {
394 MachO::segment_command_64 SLC
= MachOObj
->getSegment64LoadCommand(Command
);
395 StringRef SegName
= SLC
.segname
;
396 if (!BaseSegmentAddressSet
&& SegName
!= "__PAGEZERO") {
397 BaseSegmentAddressSet
= true;
398 BaseSegmentAddress
= SLC
.vmaddr
;
404 static bool DumpAndSkipDataInCode(uint64_t PC
, const uint8_t *bytes
,
405 DiceTable
&Dices
, uint64_t &InstSize
) {
406 // Check the data in code table here to see if this is data not an
407 // instruction to be disassembled.
409 Dice
.push_back(std::make_pair(PC
, DiceRef()));
410 dice_table_iterator DTI
=
411 std::search(Dices
.begin(), Dices
.end(), Dice
.begin(), Dice
.end(),
412 compareDiceTableEntries
);
413 if (DTI
!= Dices
.end()) {
415 DTI
->second
.getLength(Length
);
417 DTI
->second
.getKind(Kind
);
418 InstSize
= DumpDataInCode(bytes
, Length
, Kind
);
419 if ((Kind
== MachO::DICE_KIND_JUMP_TABLE8
) &&
420 (PC
== (DTI
->first
+ Length
- 1)) && (Length
& 1))
427 static void printRelocationTargetName(const MachOObjectFile
*O
,
428 const MachO::any_relocation_info
&RE
,
429 raw_string_ostream
&Fmt
) {
430 // Target of a scattered relocation is an address. In the interest of
431 // generating pretty output, scan through the symbol table looking for a
432 // symbol that aligns with that address. If we find one, print it.
433 // Otherwise, we just print the hex address of the target.
434 const StringRef FileName
= O
->getFileName();
435 if (O
->isRelocationScattered(RE
)) {
436 uint32_t Val
= O
->getPlainRelocationSymbolNum(RE
);
438 for (const SymbolRef
&Symbol
: O
->symbols()) {
439 uint64_t Addr
= unwrapOrError(Symbol
.getAddress(), FileName
);
442 Fmt
<< unwrapOrError(Symbol
.getName(), FileName
);
446 // If we couldn't find a symbol that this relocation refers to, try
447 // to find a section beginning instead.
448 for (const SectionRef
&Section
: ToolSectionFilter(*O
)) {
449 uint64_t Addr
= Section
.getAddress();
452 StringRef NameOrErr
= unwrapOrError(Section
.getName(), O
->getFileName());
457 Fmt
<< format("0x%x", Val
);
462 bool isExtern
= O
->getPlainRelocationExternal(RE
);
463 uint64_t Val
= O
->getPlainRelocationSymbolNum(RE
);
465 if (O
->getAnyRelocationType(RE
) == MachO::ARM64_RELOC_ADDEND
) {
466 Fmt
<< format("0x%0" PRIx64
, Val
);
471 symbol_iterator SI
= O
->symbol_begin();
473 S
= unwrapOrError(SI
->getName(), FileName
);
475 section_iterator SI
= O
->section_begin();
476 // Adjust for the fact that sections are 1-indexed.
481 uint32_t I
= Val
- 1;
482 while (I
!= 0 && SI
!= O
->section_end()) {
486 if (SI
== O
->section_end()) {
487 Fmt
<< Val
<< " (?,?)";
489 if (Expected
<StringRef
> NameOrErr
= SI
->getName())
492 consumeError(NameOrErr
.takeError());
499 Error
getMachORelocationValueString(const MachOObjectFile
*Obj
,
500 const RelocationRef
&RelRef
,
501 SmallVectorImpl
<char> &Result
) {
502 DataRefImpl Rel
= RelRef
.getRawDataRefImpl();
503 MachO::any_relocation_info RE
= Obj
->getRelocation(Rel
);
505 unsigned Arch
= Obj
->getArch();
508 raw_string_ostream
Fmt(FmtBuf
);
509 unsigned Type
= Obj
->getAnyRelocationType(RE
);
510 bool IsPCRel
= Obj
->getAnyRelocationPCRel(RE
);
512 // Determine any addends that should be displayed with the relocation.
513 // These require decoding the relocation type, which is triple-specific.
515 // X86_64 has entirely custom relocation types.
516 if (Arch
== Triple::x86_64
) {
518 case MachO::X86_64_RELOC_GOT_LOAD
:
519 case MachO::X86_64_RELOC_GOT
: {
520 printRelocationTargetName(Obj
, RE
, Fmt
);
526 case MachO::X86_64_RELOC_SUBTRACTOR
: {
527 DataRefImpl RelNext
= Rel
;
528 Obj
->moveRelocationNext(RelNext
);
529 MachO::any_relocation_info RENext
= Obj
->getRelocation(RelNext
);
531 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
532 // X86_64_RELOC_UNSIGNED.
533 // NOTE: Scattered relocations don't exist on x86_64.
534 unsigned RType
= Obj
->getAnyRelocationType(RENext
);
535 if (RType
!= MachO::X86_64_RELOC_UNSIGNED
)
536 reportError(Obj
->getFileName(), "Expected X86_64_RELOC_UNSIGNED after "
537 "X86_64_RELOC_SUBTRACTOR.");
539 // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
540 // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
541 printRelocationTargetName(Obj
, RENext
, Fmt
);
543 printRelocationTargetName(Obj
, RE
, Fmt
);
546 case MachO::X86_64_RELOC_TLV
:
547 printRelocationTargetName(Obj
, RE
, Fmt
);
552 case MachO::X86_64_RELOC_SIGNED_1
:
553 printRelocationTargetName(Obj
, RE
, Fmt
);
556 case MachO::X86_64_RELOC_SIGNED_2
:
557 printRelocationTargetName(Obj
, RE
, Fmt
);
560 case MachO::X86_64_RELOC_SIGNED_4
:
561 printRelocationTargetName(Obj
, RE
, Fmt
);
565 printRelocationTargetName(Obj
, RE
, Fmt
);
568 // X86 and ARM share some relocation types in common.
569 } else if (Arch
== Triple::x86
|| Arch
== Triple::arm
||
570 Arch
== Triple::ppc
) {
571 // Generic relocation types...
573 case MachO::GENERIC_RELOC_PAIR
: // prints no info
574 return Error::success();
575 case MachO::GENERIC_RELOC_SECTDIFF
: {
576 DataRefImpl RelNext
= Rel
;
577 Obj
->moveRelocationNext(RelNext
);
578 MachO::any_relocation_info RENext
= Obj
->getRelocation(RelNext
);
580 // X86 sect diff's must be followed by a relocation of type
581 // GENERIC_RELOC_PAIR.
582 unsigned RType
= Obj
->getAnyRelocationType(RENext
);
584 if (RType
!= MachO::GENERIC_RELOC_PAIR
)
585 reportError(Obj
->getFileName(), "Expected GENERIC_RELOC_PAIR after "
586 "GENERIC_RELOC_SECTDIFF.");
588 printRelocationTargetName(Obj
, RE
, Fmt
);
590 printRelocationTargetName(Obj
, RENext
, Fmt
);
595 if (Arch
== Triple::x86
|| Arch
== Triple::ppc
) {
597 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF
: {
598 DataRefImpl RelNext
= Rel
;
599 Obj
->moveRelocationNext(RelNext
);
600 MachO::any_relocation_info RENext
= Obj
->getRelocation(RelNext
);
602 // X86 sect diff's must be followed by a relocation of type
603 // GENERIC_RELOC_PAIR.
604 unsigned RType
= Obj
->getAnyRelocationType(RENext
);
605 if (RType
!= MachO::GENERIC_RELOC_PAIR
)
606 reportError(Obj
->getFileName(), "Expected GENERIC_RELOC_PAIR after "
607 "GENERIC_RELOC_LOCAL_SECTDIFF.");
609 printRelocationTargetName(Obj
, RE
, Fmt
);
611 printRelocationTargetName(Obj
, RENext
, Fmt
);
614 case MachO::GENERIC_RELOC_TLV
: {
615 printRelocationTargetName(Obj
, RE
, Fmt
);
622 printRelocationTargetName(Obj
, RE
, Fmt
);
624 } else { // ARM-specific relocations
626 case MachO::ARM_RELOC_HALF
:
627 case MachO::ARM_RELOC_HALF_SECTDIFF
: {
628 // Half relocations steal a bit from the length field to encode
629 // whether this is an upper16 or a lower16 relocation.
630 bool isUpper
= (Obj
->getAnyRelocationLength(RE
) & 0x1) == 1;
636 printRelocationTargetName(Obj
, RE
, Fmt
);
638 DataRefImpl RelNext
= Rel
;
639 Obj
->moveRelocationNext(RelNext
);
640 MachO::any_relocation_info RENext
= Obj
->getRelocation(RelNext
);
642 // ARM half relocs must be followed by a relocation of type
644 unsigned RType
= Obj
->getAnyRelocationType(RENext
);
645 if (RType
!= MachO::ARM_RELOC_PAIR
)
646 reportError(Obj
->getFileName(), "Expected ARM_RELOC_PAIR after "
649 // NOTE: The half of the target virtual address is stashed in the
650 // address field of the secondary relocation, but we can't reverse
651 // engineer the constant offset from it without decoding the movw/movt
652 // instruction to find the other half in its immediate field.
654 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
655 // symbol/section pointer of the follow-on relocation.
656 if (Type
== MachO::ARM_RELOC_HALF_SECTDIFF
) {
658 printRelocationTargetName(Obj
, RENext
, Fmt
);
665 printRelocationTargetName(Obj
, RE
, Fmt
);
670 printRelocationTargetName(Obj
, RE
, Fmt
);
673 Result
.append(FmtBuf
.begin(), FmtBuf
.end());
674 return Error::success();
677 static void PrintIndirectSymbolTable(MachOObjectFile
*O
, bool verbose
,
678 uint32_t n
, uint32_t count
,
679 uint32_t stride
, uint64_t addr
) {
680 MachO::dysymtab_command Dysymtab
= O
->getDysymtabLoadCommand();
681 uint32_t nindirectsyms
= Dysymtab
.nindirectsyms
;
682 if (n
> nindirectsyms
)
683 outs() << " (entries start past the end of the indirect symbol "
684 "table) (reserved1 field greater than the table size)";
685 else if (n
+ count
> nindirectsyms
)
686 outs() << " (entries extends past the end of the indirect symbol "
689 uint32_t cputype
= O
->getHeader().cputype
;
690 if (cputype
& MachO::CPU_ARCH_ABI64
)
691 outs() << "address index";
693 outs() << "address index";
698 for (uint32_t j
= 0; j
< count
&& n
+ j
< nindirectsyms
; j
++) {
699 if (cputype
& MachO::CPU_ARCH_ABI64
)
700 outs() << format("0x%016" PRIx64
, addr
+ j
* stride
) << " ";
702 outs() << format("0x%08" PRIx32
, (uint32_t)addr
+ j
* stride
) << " ";
703 MachO::dysymtab_command Dysymtab
= O
->getDysymtabLoadCommand();
704 uint32_t indirect_symbol
= O
->getIndirectSymbolTableEntry(Dysymtab
, n
+ j
);
705 if (indirect_symbol
== MachO::INDIRECT_SYMBOL_LOCAL
) {
709 if (indirect_symbol
==
710 (MachO::INDIRECT_SYMBOL_LOCAL
| MachO::INDIRECT_SYMBOL_ABS
)) {
711 outs() << "LOCAL ABSOLUTE\n";
714 if (indirect_symbol
== MachO::INDIRECT_SYMBOL_ABS
) {
715 outs() << "ABSOLUTE\n";
718 outs() << format("%5u ", indirect_symbol
);
720 MachO::symtab_command Symtab
= O
->getSymtabLoadCommand();
721 if (indirect_symbol
< Symtab
.nsyms
) {
722 symbol_iterator Sym
= O
->getSymbolByIndex(indirect_symbol
);
723 SymbolRef Symbol
= *Sym
;
724 outs() << unwrapOrError(Symbol
.getName(), O
->getFileName());
733 static void PrintIndirectSymbols(MachOObjectFile
*O
, bool verbose
) {
734 for (const auto &Load
: O
->load_commands()) {
735 if (Load
.C
.cmd
== MachO::LC_SEGMENT_64
) {
736 MachO::segment_command_64 Seg
= O
->getSegment64LoadCommand(Load
);
737 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
738 MachO::section_64 Sec
= O
->getSection64(Load
, J
);
739 uint32_t section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
740 if (section_type
== MachO::S_NON_LAZY_SYMBOL_POINTERS
||
741 section_type
== MachO::S_LAZY_SYMBOL_POINTERS
||
742 section_type
== MachO::S_LAZY_DYLIB_SYMBOL_POINTERS
||
743 section_type
== MachO::S_THREAD_LOCAL_VARIABLE_POINTERS
||
744 section_type
== MachO::S_SYMBOL_STUBS
) {
746 if (section_type
== MachO::S_SYMBOL_STUBS
)
747 stride
= Sec
.reserved2
;
751 outs() << "Can't print indirect symbols for (" << Sec
.segname
<< ","
752 << Sec
.sectname
<< ") "
753 << "(size of stubs in reserved2 field is zero)\n";
756 uint32_t count
= Sec
.size
/ stride
;
757 outs() << "Indirect symbols for (" << Sec
.segname
<< ","
758 << Sec
.sectname
<< ") " << count
<< " entries";
759 uint32_t n
= Sec
.reserved1
;
760 PrintIndirectSymbolTable(O
, verbose
, n
, count
, stride
, Sec
.addr
);
763 } else if (Load
.C
.cmd
== MachO::LC_SEGMENT
) {
764 MachO::segment_command Seg
= O
->getSegmentLoadCommand(Load
);
765 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
766 MachO::section Sec
= O
->getSection(Load
, J
);
767 uint32_t section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
768 if (section_type
== MachO::S_NON_LAZY_SYMBOL_POINTERS
||
769 section_type
== MachO::S_LAZY_SYMBOL_POINTERS
||
770 section_type
== MachO::S_LAZY_DYLIB_SYMBOL_POINTERS
||
771 section_type
== MachO::S_THREAD_LOCAL_VARIABLE_POINTERS
||
772 section_type
== MachO::S_SYMBOL_STUBS
) {
774 if (section_type
== MachO::S_SYMBOL_STUBS
)
775 stride
= Sec
.reserved2
;
779 outs() << "Can't print indirect symbols for (" << Sec
.segname
<< ","
780 << Sec
.sectname
<< ") "
781 << "(size of stubs in reserved2 field is zero)\n";
784 uint32_t count
= Sec
.size
/ stride
;
785 outs() << "Indirect symbols for (" << Sec
.segname
<< ","
786 << Sec
.sectname
<< ") " << count
<< " entries";
787 uint32_t n
= Sec
.reserved1
;
788 PrintIndirectSymbolTable(O
, verbose
, n
, count
, stride
, Sec
.addr
);
795 static void PrintRType(const uint64_t cputype
, const unsigned r_type
) {
796 static char const *generic_r_types
[] = {
797 "VANILLA ", "PAIR ", "SECTDIF ", "PBLAPTR ", "LOCSDIF ", "TLV ",
798 " 6 (?) ", " 7 (?) ", " 8 (?) ", " 9 (?) ", " 10 (?) ", " 11 (?) ",
799 " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
801 static char const *x86_64_r_types
[] = {
802 "UNSIGND ", "SIGNED ", "BRANCH ", "GOT_LD ", "GOT ", "SUB ",
803 "SIGNED1 ", "SIGNED2 ", "SIGNED4 ", "TLV ", " 10 (?) ", " 11 (?) ",
804 " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
806 static char const *arm_r_types
[] = {
807 "VANILLA ", "PAIR ", "SECTDIFF", "LOCSDIF ", "PBLAPTR ",
808 "BR24 ", "T_BR22 ", "T_BR32 ", "HALF ", "HALFDIF ",
809 " 10 (?) ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
811 static char const *arm64_r_types
[] = {
812 "UNSIGND ", "SUB ", "BR26 ", "PAGE21 ", "PAGOF12 ",
813 "GOTLDP ", "GOTLDPOF", "PTRTGOT ", "TLVLDP ", "TLVLDPOF",
814 "ADDEND ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
818 outs() << format("%-7u", r_type
) << " ";
822 case MachO::CPU_TYPE_I386
:
823 outs() << generic_r_types
[r_type
];
825 case MachO::CPU_TYPE_X86_64
:
826 outs() << x86_64_r_types
[r_type
];
828 case MachO::CPU_TYPE_ARM
:
829 outs() << arm_r_types
[r_type
];
831 case MachO::CPU_TYPE_ARM64
:
832 case MachO::CPU_TYPE_ARM64_32
:
833 outs() << arm64_r_types
[r_type
];
836 outs() << format("%-7u ", r_type
);
840 static void PrintRLength(const uint64_t cputype
, const unsigned r_type
,
841 const unsigned r_length
, const bool previous_arm_half
){
842 if (cputype
== MachO::CPU_TYPE_ARM
&&
843 (r_type
== MachO::ARM_RELOC_HALF
||
844 r_type
== MachO::ARM_RELOC_HALF_SECTDIFF
|| previous_arm_half
== true)) {
845 if ((r_length
& 0x1) == 0)
849 if ((r_length
& 0x1) == 0)
865 if (cputype
== MachO::CPU_TYPE_X86_64
)
868 outs() << format("?(%2d) ", r_length
);
871 outs() << format("?(%2d) ", r_length
);
876 static void PrintRelocationEntries(const MachOObjectFile
*O
,
877 const relocation_iterator Begin
,
878 const relocation_iterator End
,
879 const uint64_t cputype
,
880 const bool verbose
) {
881 const MachO::symtab_command Symtab
= O
->getSymtabLoadCommand();
882 bool previous_arm_half
= false;
883 bool previous_sectdiff
= false;
884 uint32_t sectdiff_r_type
= 0;
886 for (relocation_iterator Reloc
= Begin
; Reloc
!= End
; ++Reloc
) {
887 const DataRefImpl Rel
= Reloc
->getRawDataRefImpl();
888 const MachO::any_relocation_info RE
= O
->getRelocation(Rel
);
889 const unsigned r_type
= O
->getAnyRelocationType(RE
);
890 const bool r_scattered
= O
->isRelocationScattered(RE
);
891 const unsigned r_pcrel
= O
->getAnyRelocationPCRel(RE
);
892 const unsigned r_length
= O
->getAnyRelocationLength(RE
);
893 const unsigned r_address
= O
->getAnyRelocationAddress(RE
);
894 const bool r_extern
= (r_scattered
? false :
895 O
->getPlainRelocationExternal(RE
));
896 const uint32_t r_value
= (r_scattered
?
897 O
->getScatteredRelocationValue(RE
) : 0);
898 const unsigned r_symbolnum
= (r_scattered
? 0 :
899 O
->getPlainRelocationSymbolNum(RE
));
901 if (r_scattered
&& cputype
!= MachO::CPU_TYPE_X86_64
) {
903 // scattered: address
904 if ((cputype
== MachO::CPU_TYPE_I386
&&
905 r_type
== MachO::GENERIC_RELOC_PAIR
) ||
906 (cputype
== MachO::CPU_TYPE_ARM
&& r_type
== MachO::ARM_RELOC_PAIR
))
909 outs() << format("%08x ", (unsigned int)r_address
);
918 PrintRLength(cputype
, r_type
, r_length
, previous_arm_half
);
920 // scattered: extern & type
922 PrintRType(cputype
, r_type
);
924 // scattered: scattered & value
925 outs() << format("True 0x%08x", (unsigned int)r_value
);
926 if (previous_sectdiff
== false) {
927 if ((cputype
== MachO::CPU_TYPE_ARM
&&
928 r_type
== MachO::ARM_RELOC_PAIR
))
929 outs() << format(" half = 0x%04x ", (unsigned int)r_address
);
930 } else if (cputype
== MachO::CPU_TYPE_ARM
&&
931 sectdiff_r_type
== MachO::ARM_RELOC_HALF_SECTDIFF
)
932 outs() << format(" other_half = 0x%04x ", (unsigned int)r_address
);
933 if ((cputype
== MachO::CPU_TYPE_I386
&&
934 (r_type
== MachO::GENERIC_RELOC_SECTDIFF
||
935 r_type
== MachO::GENERIC_RELOC_LOCAL_SECTDIFF
)) ||
936 (cputype
== MachO::CPU_TYPE_ARM
&&
937 (sectdiff_r_type
== MachO::ARM_RELOC_SECTDIFF
||
938 sectdiff_r_type
== MachO::ARM_RELOC_LOCAL_SECTDIFF
||
939 sectdiff_r_type
== MachO::ARM_RELOC_HALF_SECTDIFF
))) {
940 previous_sectdiff
= true;
941 sectdiff_r_type
= r_type
;
943 previous_sectdiff
= false;
946 if (cputype
== MachO::CPU_TYPE_ARM
&&
947 (r_type
== MachO::ARM_RELOC_HALF
||
948 r_type
== MachO::ARM_RELOC_HALF_SECTDIFF
))
949 previous_arm_half
= true;
951 previous_arm_half
= false;
955 // scattered: address pcrel length extern type scattered value
956 outs() << format("%08x %1d %-2d n/a %-7d 1 0x%08x\n",
957 (unsigned int)r_address
, r_pcrel
, r_length
, r_type
,
958 (unsigned int)r_value
);
964 if (cputype
== MachO::CPU_TYPE_ARM
&& r_type
== MachO::ARM_RELOC_PAIR
)
967 outs() << format("%08x ", (unsigned int)r_address
);
976 PrintRLength(cputype
, r_type
, r_length
, previous_arm_half
);
979 // plain: extern & type & scattered
981 PrintRType(cputype
, r_type
);
984 // plain: symbolnum/value
985 if (r_symbolnum
> Symtab
.nsyms
)
986 outs() << format("?(%d)\n", r_symbolnum
);
988 SymbolRef Symbol
= *O
->getSymbolByIndex(r_symbolnum
);
989 Expected
<StringRef
> SymNameNext
= Symbol
.getName();
990 const char *name
= NULL
;
992 name
= SymNameNext
->data();
994 outs() << format("?(%d)\n", r_symbolnum
);
996 outs() << name
<< "\n";
1000 // plain: extern & type & scattered
1002 PrintRType(cputype
, r_type
);
1005 // plain: symbolnum/value
1006 if (cputype
== MachO::CPU_TYPE_ARM
&& r_type
== MachO::ARM_RELOC_PAIR
)
1007 outs() << format("other_half = 0x%04x\n", (unsigned int)r_address
);
1008 else if ((cputype
== MachO::CPU_TYPE_ARM64
||
1009 cputype
== MachO::CPU_TYPE_ARM64_32
) &&
1010 r_type
== MachO::ARM64_RELOC_ADDEND
)
1011 outs() << format("addend = 0x%06x\n", (unsigned int)r_symbolnum
);
1013 outs() << format("%d ", r_symbolnum
);
1014 if (r_symbolnum
== MachO::R_ABS
)
1015 outs() << "R_ABS\n";
1017 // in this case, r_symbolnum is actually a 1-based section number
1018 uint32_t nsects
= O
->section_end()->getRawDataRefImpl().d
.a
;
1019 if (r_symbolnum
> 0 && r_symbolnum
<= nsects
) {
1020 object::DataRefImpl DRI
;
1021 DRI
.d
.a
= r_symbolnum
-1;
1022 StringRef SegName
= O
->getSectionFinalSegmentName(DRI
);
1023 if (Expected
<StringRef
> NameOrErr
= O
->getSectionName(DRI
))
1024 outs() << "(" << SegName
<< "," << *NameOrErr
<< ")\n";
1026 outs() << "(?,?)\n";
1029 outs() << "(?,?)\n";
1034 if (cputype
== MachO::CPU_TYPE_ARM
&&
1035 (r_type
== MachO::ARM_RELOC_HALF
||
1036 r_type
== MachO::ARM_RELOC_HALF_SECTDIFF
))
1037 previous_arm_half
= true;
1039 previous_arm_half
= false;
1042 // plain: address pcrel length extern type scattered symbolnum/section
1043 outs() << format("%08x %1d %-2d %1d %-7d 0 %d\n",
1044 (unsigned int)r_address
, r_pcrel
, r_length
, r_extern
,
1045 r_type
, r_symbolnum
);
1051 static void PrintRelocations(const MachOObjectFile
*O
, const bool verbose
) {
1052 const uint64_t cputype
= O
->getHeader().cputype
;
1053 const MachO::dysymtab_command Dysymtab
= O
->getDysymtabLoadCommand();
1054 if (Dysymtab
.nextrel
!= 0) {
1055 outs() << "External relocation information " << Dysymtab
.nextrel
1057 outs() << "\naddress pcrel length extern type scattered "
1058 "symbolnum/value\n";
1059 PrintRelocationEntries(O
, O
->extrel_begin(), O
->extrel_end(), cputype
,
1062 if (Dysymtab
.nlocrel
!= 0) {
1063 outs() << format("Local relocation information %u entries",
1065 outs() << "\naddress pcrel length extern type scattered "
1066 "symbolnum/value\n";
1067 PrintRelocationEntries(O
, O
->locrel_begin(), O
->locrel_end(), cputype
,
1070 for (const auto &Load
: O
->load_commands()) {
1071 if (Load
.C
.cmd
== MachO::LC_SEGMENT_64
) {
1072 const MachO::segment_command_64 Seg
= O
->getSegment64LoadCommand(Load
);
1073 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
1074 const MachO::section_64 Sec
= O
->getSection64(Load
, J
);
1075 if (Sec
.nreloc
!= 0) {
1078 const StringRef SegName
= O
->getSectionFinalSegmentName(DRI
);
1079 if (Expected
<StringRef
> NameOrErr
= O
->getSectionName(DRI
))
1080 outs() << "Relocation information (" << SegName
<< "," << *NameOrErr
1081 << format(") %u entries", Sec
.nreloc
);
1083 outs() << "Relocation information (" << SegName
<< ",?) "
1084 << format("%u entries", Sec
.nreloc
);
1085 outs() << "\naddress pcrel length extern type scattered "
1086 "symbolnum/value\n";
1087 PrintRelocationEntries(O
, O
->section_rel_begin(DRI
),
1088 O
->section_rel_end(DRI
), cputype
, verbose
);
1091 } else if (Load
.C
.cmd
== MachO::LC_SEGMENT
) {
1092 const MachO::segment_command Seg
= O
->getSegmentLoadCommand(Load
);
1093 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
1094 const MachO::section Sec
= O
->getSection(Load
, J
);
1095 if (Sec
.nreloc
!= 0) {
1098 const StringRef SegName
= O
->getSectionFinalSegmentName(DRI
);
1099 if (Expected
<StringRef
> NameOrErr
= O
->getSectionName(DRI
))
1100 outs() << "Relocation information (" << SegName
<< "," << *NameOrErr
1101 << format(") %u entries", Sec
.nreloc
);
1103 outs() << "Relocation information (" << SegName
<< ",?) "
1104 << format("%u entries", Sec
.nreloc
);
1105 outs() << "\naddress pcrel length extern type scattered "
1106 "symbolnum/value\n";
1107 PrintRelocationEntries(O
, O
->section_rel_begin(DRI
),
1108 O
->section_rel_end(DRI
), cputype
, verbose
);
1115 static void PrintDataInCodeTable(MachOObjectFile
*O
, bool verbose
) {
1116 MachO::linkedit_data_command DIC
= O
->getDataInCodeLoadCommand();
1117 uint32_t nentries
= DIC
.datasize
/ sizeof(struct MachO::data_in_code_entry
);
1118 outs() << "Data in code table (" << nentries
<< " entries)\n";
1119 outs() << "offset length kind\n";
1120 for (dice_iterator DI
= O
->begin_dices(), DE
= O
->end_dices(); DI
!= DE
;
1123 DI
->getOffset(Offset
);
1124 outs() << format("0x%08" PRIx32
, Offset
) << " ";
1126 DI
->getLength(Length
);
1127 outs() << format("%6u", Length
) << " ";
1132 case MachO::DICE_KIND_DATA
:
1135 case MachO::DICE_KIND_JUMP_TABLE8
:
1136 outs() << "JUMP_TABLE8";
1138 case MachO::DICE_KIND_JUMP_TABLE16
:
1139 outs() << "JUMP_TABLE16";
1141 case MachO::DICE_KIND_JUMP_TABLE32
:
1142 outs() << "JUMP_TABLE32";
1144 case MachO::DICE_KIND_ABS_JUMP_TABLE32
:
1145 outs() << "ABS_JUMP_TABLE32";
1148 outs() << format("0x%04" PRIx32
, Kind
);
1152 outs() << format("0x%04" PRIx32
, Kind
);
1157 static void PrintLinkOptHints(MachOObjectFile
*O
) {
1158 MachO::linkedit_data_command LohLC
= O
->getLinkOptHintsLoadCommand();
1159 const char *loh
= O
->getData().substr(LohLC
.dataoff
, 1).data();
1160 uint32_t nloh
= LohLC
.datasize
;
1161 outs() << "Linker optimiztion hints (" << nloh
<< " total bytes)\n";
1162 for (uint32_t i
= 0; i
< nloh
;) {
1164 uint64_t identifier
= decodeULEB128((const uint8_t *)(loh
+ i
), &n
);
1166 outs() << " identifier " << identifier
<< " ";
1169 switch (identifier
) {
1171 outs() << "AdrpAdrp\n";
1174 outs() << "AdrpLdr\n";
1177 outs() << "AdrpAddLdr\n";
1180 outs() << "AdrpLdrGotLdr\n";
1183 outs() << "AdrpAddStr\n";
1186 outs() << "AdrpLdrGotStr\n";
1189 outs() << "AdrpAdd\n";
1192 outs() << "AdrpLdrGot\n";
1195 outs() << "Unknown identifier value\n";
1198 uint64_t narguments
= decodeULEB128((const uint8_t *)(loh
+ i
), &n
);
1200 outs() << " narguments " << narguments
<< "\n";
1204 for (uint32_t j
= 0; j
< narguments
; j
++) {
1205 uint64_t value
= decodeULEB128((const uint8_t *)(loh
+ i
), &n
);
1207 outs() << "\tvalue " << format("0x%" PRIx64
, value
) << "\n";
1214 static void PrintDylibs(MachOObjectFile
*O
, bool JustId
) {
1216 for (const auto &Load
: O
->load_commands()) {
1217 if ((JustId
&& Load
.C
.cmd
== MachO::LC_ID_DYLIB
) ||
1218 (!JustId
&& (Load
.C
.cmd
== MachO::LC_ID_DYLIB
||
1219 Load
.C
.cmd
== MachO::LC_LOAD_DYLIB
||
1220 Load
.C
.cmd
== MachO::LC_LOAD_WEAK_DYLIB
||
1221 Load
.C
.cmd
== MachO::LC_REEXPORT_DYLIB
||
1222 Load
.C
.cmd
== MachO::LC_LAZY_LOAD_DYLIB
||
1223 Load
.C
.cmd
== MachO::LC_LOAD_UPWARD_DYLIB
))) {
1224 MachO::dylib_command dl
= O
->getDylibIDLoadCommand(Load
);
1225 if (dl
.dylib
.name
< dl
.cmdsize
) {
1226 const char *p
= (const char *)(Load
.Ptr
) + dl
.dylib
.name
;
1228 outs() << p
<< "\n";
1230 outs() << "\t" << p
;
1231 outs() << " (compatibility version "
1232 << ((dl
.dylib
.compatibility_version
>> 16) & 0xffff) << "."
1233 << ((dl
.dylib
.compatibility_version
>> 8) & 0xff) << "."
1234 << (dl
.dylib
.compatibility_version
& 0xff) << ",";
1235 outs() << " current version "
1236 << ((dl
.dylib
.current_version
>> 16) & 0xffff) << "."
1237 << ((dl
.dylib
.current_version
>> 8) & 0xff) << "."
1238 << (dl
.dylib
.current_version
& 0xff);
1239 if (Load
.C
.cmd
== MachO::LC_LOAD_WEAK_DYLIB
)
1241 if (Load
.C
.cmd
== MachO::LC_REEXPORT_DYLIB
)
1242 outs() << ", reexport";
1243 if (Load
.C
.cmd
== MachO::LC_LOAD_UPWARD_DYLIB
)
1244 outs() << ", upward";
1245 if (Load
.C
.cmd
== MachO::LC_LAZY_LOAD_DYLIB
)
1250 outs() << "\tBad offset (" << dl
.dylib
.name
<< ") for name of ";
1251 if (Load
.C
.cmd
== MachO::LC_ID_DYLIB
)
1252 outs() << "LC_ID_DYLIB ";
1253 else if (Load
.C
.cmd
== MachO::LC_LOAD_DYLIB
)
1254 outs() << "LC_LOAD_DYLIB ";
1255 else if (Load
.C
.cmd
== MachO::LC_LOAD_WEAK_DYLIB
)
1256 outs() << "LC_LOAD_WEAK_DYLIB ";
1257 else if (Load
.C
.cmd
== MachO::LC_LAZY_LOAD_DYLIB
)
1258 outs() << "LC_LAZY_LOAD_DYLIB ";
1259 else if (Load
.C
.cmd
== MachO::LC_REEXPORT_DYLIB
)
1260 outs() << "LC_REEXPORT_DYLIB ";
1261 else if (Load
.C
.cmd
== MachO::LC_LOAD_UPWARD_DYLIB
)
1262 outs() << "LC_LOAD_UPWARD_DYLIB ";
1264 outs() << "LC_??? ";
1265 outs() << "command " << Index
++ << "\n";
1271 typedef DenseMap
<uint64_t, StringRef
> SymbolAddressMap
;
1273 static void CreateSymbolAddressMap(MachOObjectFile
*O
,
1274 SymbolAddressMap
*AddrMap
) {
1275 // Create a map of symbol addresses to symbol names.
1276 const StringRef FileName
= O
->getFileName();
1277 for (const SymbolRef
&Symbol
: O
->symbols()) {
1278 SymbolRef::Type ST
= unwrapOrError(Symbol
.getType(), FileName
);
1279 if (ST
== SymbolRef::ST_Function
|| ST
== SymbolRef::ST_Data
||
1280 ST
== SymbolRef::ST_Other
) {
1281 uint64_t Address
= Symbol
.getValue();
1282 StringRef SymName
= unwrapOrError(Symbol
.getName(), FileName
);
1283 if (!SymName
.startswith(".objc"))
1284 (*AddrMap
)[Address
] = SymName
;
1289 // GuessSymbolName is passed the address of what might be a symbol and a
1290 // pointer to the SymbolAddressMap. It returns the name of a symbol
1291 // with that address or nullptr if no symbol is found with that address.
1292 static const char *GuessSymbolName(uint64_t value
, SymbolAddressMap
*AddrMap
) {
1293 const char *SymbolName
= nullptr;
1294 // A DenseMap can't lookup up some values.
1295 if (value
!= 0xffffffffffffffffULL
&& value
!= 0xfffffffffffffffeULL
) {
1296 StringRef name
= AddrMap
->lookup(value
);
1298 SymbolName
= name
.data();
1303 static void DumpCstringChar(const char c
) {
1307 outs().write_escaped(p
);
1310 static void DumpCstringSection(MachOObjectFile
*O
, const char *sect
,
1311 uint32_t sect_size
, uint64_t sect_addr
,
1312 bool print_addresses
) {
1313 for (uint32_t i
= 0; i
< sect_size
; i
++) {
1314 if (print_addresses
) {
1316 outs() << format("%016" PRIx64
, sect_addr
+ i
) << " ";
1318 outs() << format("%08" PRIx64
, sect_addr
+ i
) << " ";
1320 for (; i
< sect_size
&& sect
[i
] != '\0'; i
++)
1321 DumpCstringChar(sect
[i
]);
1322 if (i
< sect_size
&& sect
[i
] == '\0')
1327 static void DumpLiteral4(uint32_t l
, float f
) {
1328 outs() << format("0x%08" PRIx32
, l
);
1329 if ((l
& 0x7f800000) != 0x7f800000)
1330 outs() << format(" (%.16e)\n", f
);
1332 if (l
== 0x7f800000)
1333 outs() << " (+Infinity)\n";
1334 else if (l
== 0xff800000)
1335 outs() << " (-Infinity)\n";
1336 else if ((l
& 0x00400000) == 0x00400000)
1337 outs() << " (non-signaling Not-a-Number)\n";
1339 outs() << " (signaling Not-a-Number)\n";
1343 static void DumpLiteral4Section(MachOObjectFile
*O
, const char *sect
,
1344 uint32_t sect_size
, uint64_t sect_addr
,
1345 bool print_addresses
) {
1346 for (uint32_t i
= 0; i
< sect_size
; i
+= sizeof(float)) {
1347 if (print_addresses
) {
1349 outs() << format("%016" PRIx64
, sect_addr
+ i
) << " ";
1351 outs() << format("%08" PRIx64
, sect_addr
+ i
) << " ";
1354 memcpy(&f
, sect
+ i
, sizeof(float));
1355 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
1356 sys::swapByteOrder(f
);
1358 memcpy(&l
, sect
+ i
, sizeof(uint32_t));
1359 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
1360 sys::swapByteOrder(l
);
1365 static void DumpLiteral8(MachOObjectFile
*O
, uint32_t l0
, uint32_t l1
,
1367 outs() << format("0x%08" PRIx32
, l0
) << " " << format("0x%08" PRIx32
, l1
);
1369 Hi
= (O
->isLittleEndian()) ? l1
: l0
;
1370 Lo
= (O
->isLittleEndian()) ? l0
: l1
;
1372 // Hi is the high word, so this is equivalent to if(isfinite(d))
1373 if ((Hi
& 0x7ff00000) != 0x7ff00000)
1374 outs() << format(" (%.16e)\n", d
);
1376 if (Hi
== 0x7ff00000 && Lo
== 0)
1377 outs() << " (+Infinity)\n";
1378 else if (Hi
== 0xfff00000 && Lo
== 0)
1379 outs() << " (-Infinity)\n";
1380 else if ((Hi
& 0x00080000) == 0x00080000)
1381 outs() << " (non-signaling Not-a-Number)\n";
1383 outs() << " (signaling Not-a-Number)\n";
1387 static void DumpLiteral8Section(MachOObjectFile
*O
, const char *sect
,
1388 uint32_t sect_size
, uint64_t sect_addr
,
1389 bool print_addresses
) {
1390 for (uint32_t i
= 0; i
< sect_size
; i
+= sizeof(double)) {
1391 if (print_addresses
) {
1393 outs() << format("%016" PRIx64
, sect_addr
+ i
) << " ";
1395 outs() << format("%08" PRIx64
, sect_addr
+ i
) << " ";
1398 memcpy(&d
, sect
+ i
, sizeof(double));
1399 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
1400 sys::swapByteOrder(d
);
1402 memcpy(&l0
, sect
+ i
, sizeof(uint32_t));
1403 memcpy(&l1
, sect
+ i
+ sizeof(uint32_t), sizeof(uint32_t));
1404 if (O
->isLittleEndian() != sys::IsLittleEndianHost
) {
1405 sys::swapByteOrder(l0
);
1406 sys::swapByteOrder(l1
);
1408 DumpLiteral8(O
, l0
, l1
, d
);
1412 static void DumpLiteral16(uint32_t l0
, uint32_t l1
, uint32_t l2
, uint32_t l3
) {
1413 outs() << format("0x%08" PRIx32
, l0
) << " ";
1414 outs() << format("0x%08" PRIx32
, l1
) << " ";
1415 outs() << format("0x%08" PRIx32
, l2
) << " ";
1416 outs() << format("0x%08" PRIx32
, l3
) << "\n";
1419 static void DumpLiteral16Section(MachOObjectFile
*O
, const char *sect
,
1420 uint32_t sect_size
, uint64_t sect_addr
,
1421 bool print_addresses
) {
1422 for (uint32_t i
= 0; i
< sect_size
; i
+= 16) {
1423 if (print_addresses
) {
1425 outs() << format("%016" PRIx64
, sect_addr
+ i
) << " ";
1427 outs() << format("%08" PRIx64
, sect_addr
+ i
) << " ";
1429 uint32_t l0
, l1
, l2
, l3
;
1430 memcpy(&l0
, sect
+ i
, sizeof(uint32_t));
1431 memcpy(&l1
, sect
+ i
+ sizeof(uint32_t), sizeof(uint32_t));
1432 memcpy(&l2
, sect
+ i
+ 2 * sizeof(uint32_t), sizeof(uint32_t));
1433 memcpy(&l3
, sect
+ i
+ 3 * sizeof(uint32_t), sizeof(uint32_t));
1434 if (O
->isLittleEndian() != sys::IsLittleEndianHost
) {
1435 sys::swapByteOrder(l0
);
1436 sys::swapByteOrder(l1
);
1437 sys::swapByteOrder(l2
);
1438 sys::swapByteOrder(l3
);
1440 DumpLiteral16(l0
, l1
, l2
, l3
);
1444 static void DumpLiteralPointerSection(MachOObjectFile
*O
,
1445 const SectionRef
&Section
,
1446 const char *sect
, uint32_t sect_size
,
1448 bool print_addresses
) {
1449 // Collect the literal sections in this Mach-O file.
1450 std::vector
<SectionRef
> LiteralSections
;
1451 for (const SectionRef
&Section
: O
->sections()) {
1452 DataRefImpl Ref
= Section
.getRawDataRefImpl();
1453 uint32_t section_type
;
1455 const MachO::section_64 Sec
= O
->getSection64(Ref
);
1456 section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
1458 const MachO::section Sec
= O
->getSection(Ref
);
1459 section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
1461 if (section_type
== MachO::S_CSTRING_LITERALS
||
1462 section_type
== MachO::S_4BYTE_LITERALS
||
1463 section_type
== MachO::S_8BYTE_LITERALS
||
1464 section_type
== MachO::S_16BYTE_LITERALS
)
1465 LiteralSections
.push_back(Section
);
1468 // Set the size of the literal pointer.
1469 uint32_t lp_size
= O
->is64Bit() ? 8 : 4;
1471 // Collect the external relocation symbols for the literal pointers.
1472 std::vector
<std::pair
<uint64_t, SymbolRef
>> Relocs
;
1473 for (const RelocationRef
&Reloc
: Section
.relocations()) {
1475 MachO::any_relocation_info RE
;
1476 bool isExtern
= false;
1477 Rel
= Reloc
.getRawDataRefImpl();
1478 RE
= O
->getRelocation(Rel
);
1479 isExtern
= O
->getPlainRelocationExternal(RE
);
1481 uint64_t RelocOffset
= Reloc
.getOffset();
1482 symbol_iterator RelocSym
= Reloc
.getSymbol();
1483 Relocs
.push_back(std::make_pair(RelocOffset
, *RelocSym
));
1486 array_pod_sort(Relocs
.begin(), Relocs
.end());
1488 // Dump each literal pointer.
1489 for (uint32_t i
= 0; i
< sect_size
; i
+= lp_size
) {
1490 if (print_addresses
) {
1492 outs() << format("%016" PRIx64
, sect_addr
+ i
) << " ";
1494 outs() << format("%08" PRIx64
, sect_addr
+ i
) << " ";
1498 memcpy(&lp
, sect
+ i
, sizeof(uint64_t));
1499 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
1500 sys::swapByteOrder(lp
);
1503 memcpy(&li
, sect
+ i
, sizeof(uint32_t));
1504 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
1505 sys::swapByteOrder(li
);
1509 // First look for an external relocation entry for this literal pointer.
1510 auto Reloc
= find_if(Relocs
, [&](const std::pair
<uint64_t, SymbolRef
> &P
) {
1511 return P
.first
== i
;
1513 if (Reloc
!= Relocs
.end()) {
1514 symbol_iterator RelocSym
= Reloc
->second
;
1515 StringRef SymName
= unwrapOrError(RelocSym
->getName(), O
->getFileName());
1516 outs() << "external relocation entry for symbol:" << SymName
<< "\n";
1520 // For local references see what the section the literal pointer points to.
1521 auto Sect
= find_if(LiteralSections
, [&](const SectionRef
&R
) {
1522 return lp
>= R
.getAddress() && lp
< R
.getAddress() + R
.getSize();
1524 if (Sect
== LiteralSections
.end()) {
1525 outs() << format("0x%" PRIx64
, lp
) << " (not in a literal section)\n";
1529 uint64_t SectAddress
= Sect
->getAddress();
1530 uint64_t SectSize
= Sect
->getSize();
1533 Expected
<StringRef
> SectNameOrErr
= Sect
->getName();
1535 SectName
= *SectNameOrErr
;
1537 consumeError(SectNameOrErr
.takeError());
1539 DataRefImpl Ref
= Sect
->getRawDataRefImpl();
1540 StringRef SegmentName
= O
->getSectionFinalSegmentName(Ref
);
1541 outs() << SegmentName
<< ":" << SectName
<< ":";
1543 uint32_t section_type
;
1545 const MachO::section_64 Sec
= O
->getSection64(Ref
);
1546 section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
1548 const MachO::section Sec
= O
->getSection(Ref
);
1549 section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
1552 StringRef BytesStr
= unwrapOrError(Sect
->getContents(), O
->getFileName());
1554 const char *Contents
= reinterpret_cast<const char *>(BytesStr
.data());
1556 switch (section_type
) {
1557 case MachO::S_CSTRING_LITERALS
:
1558 for (uint64_t i
= lp
- SectAddress
; i
< SectSize
&& Contents
[i
] != '\0';
1560 DumpCstringChar(Contents
[i
]);
1564 case MachO::S_4BYTE_LITERALS
:
1566 memcpy(&f
, Contents
+ (lp
- SectAddress
), sizeof(float));
1568 memcpy(&l
, Contents
+ (lp
- SectAddress
), sizeof(uint32_t));
1569 if (O
->isLittleEndian() != sys::IsLittleEndianHost
) {
1570 sys::swapByteOrder(f
);
1571 sys::swapByteOrder(l
);
1575 case MachO::S_8BYTE_LITERALS
: {
1577 memcpy(&d
, Contents
+ (lp
- SectAddress
), sizeof(double));
1579 memcpy(&l0
, Contents
+ (lp
- SectAddress
), sizeof(uint32_t));
1580 memcpy(&l1
, Contents
+ (lp
- SectAddress
) + sizeof(uint32_t),
1582 if (O
->isLittleEndian() != sys::IsLittleEndianHost
) {
1583 sys::swapByteOrder(f
);
1584 sys::swapByteOrder(l0
);
1585 sys::swapByteOrder(l1
);
1587 DumpLiteral8(O
, l0
, l1
, d
);
1590 case MachO::S_16BYTE_LITERALS
: {
1591 uint32_t l0
, l1
, l2
, l3
;
1592 memcpy(&l0
, Contents
+ (lp
- SectAddress
), sizeof(uint32_t));
1593 memcpy(&l1
, Contents
+ (lp
- SectAddress
) + sizeof(uint32_t),
1595 memcpy(&l2
, Contents
+ (lp
- SectAddress
) + 2 * sizeof(uint32_t),
1597 memcpy(&l3
, Contents
+ (lp
- SectAddress
) + 3 * sizeof(uint32_t),
1599 if (O
->isLittleEndian() != sys::IsLittleEndianHost
) {
1600 sys::swapByteOrder(l0
);
1601 sys::swapByteOrder(l1
);
1602 sys::swapByteOrder(l2
);
1603 sys::swapByteOrder(l3
);
1605 DumpLiteral16(l0
, l1
, l2
, l3
);
1612 static void DumpInitTermPointerSection(MachOObjectFile
*O
,
1613 const SectionRef
&Section
,
1615 uint32_t sect_size
, uint64_t sect_addr
,
1616 SymbolAddressMap
*AddrMap
,
1619 stride
= (O
->is64Bit()) ? sizeof(uint64_t) : sizeof(uint32_t);
1621 // Collect the external relocation symbols for the pointers.
1622 std::vector
<std::pair
<uint64_t, SymbolRef
>> Relocs
;
1623 for (const RelocationRef
&Reloc
: Section
.relocations()) {
1625 MachO::any_relocation_info RE
;
1626 bool isExtern
= false;
1627 Rel
= Reloc
.getRawDataRefImpl();
1628 RE
= O
->getRelocation(Rel
);
1629 isExtern
= O
->getPlainRelocationExternal(RE
);
1631 uint64_t RelocOffset
= Reloc
.getOffset();
1632 symbol_iterator RelocSym
= Reloc
.getSymbol();
1633 Relocs
.push_back(std::make_pair(RelocOffset
, *RelocSym
));
1636 array_pod_sort(Relocs
.begin(), Relocs
.end());
1638 for (uint32_t i
= 0; i
< sect_size
; i
+= stride
) {
1639 const char *SymbolName
= nullptr;
1642 outs() << format("0x%016" PRIx64
, sect_addr
+ i
* stride
) << " ";
1643 uint64_t pointer_value
;
1644 memcpy(&pointer_value
, sect
+ i
, stride
);
1645 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
1646 sys::swapByteOrder(pointer_value
);
1647 outs() << format("0x%016" PRIx64
, pointer_value
);
1650 outs() << format("0x%08" PRIx64
, sect_addr
+ i
* stride
) << " ";
1651 uint32_t pointer_value
;
1652 memcpy(&pointer_value
, sect
+ i
, stride
);
1653 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
1654 sys::swapByteOrder(pointer_value
);
1655 outs() << format("0x%08" PRIx32
, pointer_value
);
1659 // First look for an external relocation entry for this pointer.
1660 auto Reloc
= find_if(Relocs
, [&](const std::pair
<uint64_t, SymbolRef
> &P
) {
1661 return P
.first
== i
;
1663 if (Reloc
!= Relocs
.end()) {
1664 symbol_iterator RelocSym
= Reloc
->second
;
1665 outs() << " " << unwrapOrError(RelocSym
->getName(), O
->getFileName());
1667 SymbolName
= GuessSymbolName(p
, AddrMap
);
1669 outs() << " " << SymbolName
;
1676 static void DumpRawSectionContents(MachOObjectFile
*O
, const char *sect
,
1677 uint32_t size
, uint64_t addr
) {
1678 uint32_t cputype
= O
->getHeader().cputype
;
1679 if (cputype
== MachO::CPU_TYPE_I386
|| cputype
== MachO::CPU_TYPE_X86_64
) {
1681 for (uint32_t i
= 0; i
< size
; i
+= j
, addr
+= j
) {
1683 outs() << format("%016" PRIx64
, addr
) << "\t";
1685 outs() << format("%08" PRIx64
, addr
) << "\t";
1686 for (j
= 0; j
< 16 && i
+ j
< size
; j
++) {
1687 uint8_t byte_word
= *(sect
+ i
+ j
);
1688 outs() << format("%02" PRIx32
, (uint32_t)byte_word
) << " ";
1694 for (uint32_t i
= 0; i
< size
; i
+= j
, addr
+= j
) {
1696 outs() << format("%016" PRIx64
, addr
) << "\t";
1698 outs() << format("%08" PRIx64
, addr
) << "\t";
1699 for (j
= 0; j
< 4 * sizeof(int32_t) && i
+ j
< size
;
1700 j
+= sizeof(int32_t)) {
1701 if (i
+ j
+ sizeof(int32_t) <= size
) {
1703 memcpy(&long_word
, sect
+ i
+ j
, sizeof(int32_t));
1704 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
1705 sys::swapByteOrder(long_word
);
1706 outs() << format("%08" PRIx32
, long_word
) << " ";
1708 for (uint32_t k
= 0; i
+ j
+ k
< size
; k
++) {
1709 uint8_t byte_word
= *(sect
+ i
+ j
+ k
);
1710 outs() << format("%02" PRIx32
, (uint32_t)byte_word
) << " ";
1719 static void DisassembleMachO(StringRef Filename
, MachOObjectFile
*MachOOF
,
1720 StringRef DisSegName
, StringRef DisSectName
);
1721 static void DumpProtocolSection(MachOObjectFile
*O
, const char *sect
,
1722 uint32_t size
, uint32_t addr
);
1724 static void DumpBitcodeSection(MachOObjectFile
*O
, const char *sect
,
1725 uint32_t size
, bool verbose
,
1726 bool PrintXarHeader
, bool PrintXarFileHeaders
,
1727 std::string XarMemberName
);
1728 #endif // defined(HAVE_LIBXAR)
1730 static void DumpSectionContents(StringRef Filename
, MachOObjectFile
*O
,
1732 SymbolAddressMap AddrMap
;
1734 CreateSymbolAddressMap(O
, &AddrMap
);
1736 for (unsigned i
= 0; i
< FilterSections
.size(); ++i
) {
1737 StringRef DumpSection
= FilterSections
[i
];
1738 std::pair
<StringRef
, StringRef
> DumpSegSectName
;
1739 DumpSegSectName
= DumpSection
.split(',');
1740 StringRef DumpSegName
, DumpSectName
;
1741 if (!DumpSegSectName
.second
.empty()) {
1742 DumpSegName
= DumpSegSectName
.first
;
1743 DumpSectName
= DumpSegSectName
.second
;
1746 DumpSectName
= DumpSegSectName
.first
;
1748 for (const SectionRef
&Section
: O
->sections()) {
1750 Expected
<StringRef
> SecNameOrErr
= Section
.getName();
1752 SectName
= *SecNameOrErr
;
1754 consumeError(SecNameOrErr
.takeError());
1756 DataRefImpl Ref
= Section
.getRawDataRefImpl();
1757 StringRef SegName
= O
->getSectionFinalSegmentName(Ref
);
1758 if ((DumpSegName
.empty() || SegName
== DumpSegName
) &&
1759 (SectName
== DumpSectName
)) {
1761 uint32_t section_flags
;
1763 const MachO::section_64 Sec
= O
->getSection64(Ref
);
1764 section_flags
= Sec
.flags
;
1767 const MachO::section Sec
= O
->getSection(Ref
);
1768 section_flags
= Sec
.flags
;
1770 uint32_t section_type
= section_flags
& MachO::SECTION_TYPE
;
1772 StringRef BytesStr
=
1773 unwrapOrError(Section
.getContents(), O
->getFileName());
1774 const char *sect
= reinterpret_cast<const char *>(BytesStr
.data());
1775 uint32_t sect_size
= BytesStr
.size();
1776 uint64_t sect_addr
= Section
.getAddress();
1778 outs() << "Contents of (" << SegName
<< "," << SectName
1782 if ((section_flags
& MachO::S_ATTR_PURE_INSTRUCTIONS
) ||
1783 (section_flags
& MachO::S_ATTR_SOME_INSTRUCTIONS
)) {
1784 DisassembleMachO(Filename
, O
, SegName
, SectName
);
1787 if (SegName
== "__TEXT" && SectName
== "__info_plist") {
1791 if (SegName
== "__OBJC" && SectName
== "__protocol") {
1792 DumpProtocolSection(O
, sect
, sect_size
, sect_addr
);
1796 if (SegName
== "__LLVM" && SectName
== "__bundle") {
1797 DumpBitcodeSection(O
, sect
, sect_size
, verbose
, !NoSymbolicOperands
,
1798 ArchiveHeaders
, "");
1801 #endif // defined(HAVE_LIBXAR)
1802 switch (section_type
) {
1803 case MachO::S_REGULAR
:
1804 DumpRawSectionContents(O
, sect
, sect_size
, sect_addr
);
1806 case MachO::S_ZEROFILL
:
1807 outs() << "zerofill section and has no contents in the file\n";
1809 case MachO::S_CSTRING_LITERALS
:
1810 DumpCstringSection(O
, sect
, sect_size
, sect_addr
, !NoLeadingAddr
);
1812 case MachO::S_4BYTE_LITERALS
:
1813 DumpLiteral4Section(O
, sect
, sect_size
, sect_addr
, !NoLeadingAddr
);
1815 case MachO::S_8BYTE_LITERALS
:
1816 DumpLiteral8Section(O
, sect
, sect_size
, sect_addr
, !NoLeadingAddr
);
1818 case MachO::S_16BYTE_LITERALS
:
1819 DumpLiteral16Section(O
, sect
, sect_size
, sect_addr
, !NoLeadingAddr
);
1821 case MachO::S_LITERAL_POINTERS
:
1822 DumpLiteralPointerSection(O
, Section
, sect
, sect_size
, sect_addr
,
1825 case MachO::S_MOD_INIT_FUNC_POINTERS
:
1826 case MachO::S_MOD_TERM_FUNC_POINTERS
:
1827 DumpInitTermPointerSection(O
, Section
, sect
, sect_size
, sect_addr
,
1831 outs() << "Unknown section type ("
1832 << format("0x%08" PRIx32
, section_type
) << ")\n";
1833 DumpRawSectionContents(O
, sect
, sect_size
, sect_addr
);
1837 if (section_type
== MachO::S_ZEROFILL
)
1838 outs() << "zerofill section and has no contents in the file\n";
1840 DumpRawSectionContents(O
, sect
, sect_size
, sect_addr
);
1847 static void DumpInfoPlistSectionContents(StringRef Filename
,
1848 MachOObjectFile
*O
) {
1849 for (const SectionRef
&Section
: O
->sections()) {
1851 Expected
<StringRef
> SecNameOrErr
= Section
.getName();
1853 SectName
= *SecNameOrErr
;
1855 consumeError(SecNameOrErr
.takeError());
1857 DataRefImpl Ref
= Section
.getRawDataRefImpl();
1858 StringRef SegName
= O
->getSectionFinalSegmentName(Ref
);
1859 if (SegName
== "__TEXT" && SectName
== "__info_plist") {
1860 if (!NoLeadingHeaders
)
1861 outs() << "Contents of (" << SegName
<< "," << SectName
<< ") section\n";
1862 StringRef BytesStr
=
1863 unwrapOrError(Section
.getContents(), O
->getFileName());
1864 const char *sect
= reinterpret_cast<const char *>(BytesStr
.data());
1865 outs() << format("%.*s", BytesStr
.size(), sect
) << "\n";
1871 // checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1872 // and if it is and there is a list of architecture flags is specified then
1873 // check to make sure this Mach-O file is one of those architectures or all
1874 // architectures were specified. If not then an error is generated and this
1875 // routine returns false. Else it returns true.
1876 static bool checkMachOAndArchFlags(ObjectFile
*O
, StringRef Filename
) {
1877 auto *MachO
= dyn_cast
<MachOObjectFile
>(O
);
1879 if (!MachO
|| ArchAll
|| ArchFlags
.empty())
1882 MachO::mach_header H
;
1883 MachO::mach_header_64 H_64
;
1885 const char *McpuDefault
, *ArchFlag
;
1886 if (MachO
->is64Bit()) {
1887 H_64
= MachO
->MachOObjectFile::getHeader64();
1888 T
= MachOObjectFile::getArchTriple(H_64
.cputype
, H_64
.cpusubtype
,
1889 &McpuDefault
, &ArchFlag
);
1891 H
= MachO
->MachOObjectFile::getHeader();
1892 T
= MachOObjectFile::getArchTriple(H
.cputype
, H
.cpusubtype
,
1893 &McpuDefault
, &ArchFlag
);
1895 const std::string
ArchFlagName(ArchFlag
);
1896 if (none_of(ArchFlags
, [&](const std::string
&Name
) {
1897 return Name
== ArchFlagName
;
1899 WithColor::error(errs(), "llvm-objdump")
1900 << Filename
<< ": no architecture specified.\n";
1906 static void printObjcMetaData(MachOObjectFile
*O
, bool verbose
);
1908 // ProcessMachO() is passed a single opened Mach-O file, which may be an
1909 // archive member and or in a slice of a universal file. It prints the
1910 // the file name and header info and then processes it according to the
1911 // command line options.
1912 static void ProcessMachO(StringRef Name
, MachOObjectFile
*MachOOF
,
1913 StringRef ArchiveMemberName
= StringRef(),
1914 StringRef ArchitectureName
= StringRef()) {
1915 // If we are doing some processing here on the Mach-O file print the header
1916 // info. And don't print it otherwise like in the case of printing the
1917 // UniversalHeaders or ArchiveHeaders.
1918 if (Disassemble
|| Relocations
|| PrivateHeaders
|| ExportsTrie
|| Rebase
||
1919 Bind
|| SymbolTable
|| LazyBind
|| WeakBind
|| IndirectSymbols
||
1920 DataInCode
|| LinkOptHints
|| DylibsUsed
|| DylibId
|| ObjcMetaData
||
1921 (!FilterSections
.empty())) {
1922 if (!NoLeadingHeaders
) {
1924 if (!ArchiveMemberName
.empty())
1925 outs() << '(' << ArchiveMemberName
<< ')';
1926 if (!ArchitectureName
.empty())
1927 outs() << " (architecture " << ArchitectureName
<< ")";
1931 // To use the report_error() form with an ArchiveName and FileName set
1932 // these up based on what is passed for Name and ArchiveMemberName.
1933 StringRef ArchiveName
;
1935 if (!ArchiveMemberName
.empty()) {
1937 FileName
= ArchiveMemberName
;
1939 ArchiveName
= StringRef();
1943 // If we need the symbol table to do the operation then check it here to
1944 // produce a good error message as to where the Mach-O file comes from in
1945 // the error message.
1946 if (Disassemble
|| IndirectSymbols
|| !FilterSections
.empty() || UnwindInfo
)
1947 if (Error Err
= MachOOF
->checkSymbolTable())
1948 reportError(std::move(Err
), FileName
, ArchiveName
, ArchitectureName
);
1950 if (DisassembleAll
) {
1951 for (const SectionRef
&Section
: MachOOF
->sections()) {
1953 if (Expected
<StringRef
> NameOrErr
= Section
.getName())
1954 SectName
= *NameOrErr
;
1956 consumeError(NameOrErr
.takeError());
1958 if (SectName
.equals("__text")) {
1959 DataRefImpl Ref
= Section
.getRawDataRefImpl();
1960 StringRef SegName
= MachOOF
->getSectionFinalSegmentName(Ref
);
1961 DisassembleMachO(FileName
, MachOOF
, SegName
, SectName
);
1965 else if (Disassemble
) {
1966 if (MachOOF
->getHeader().filetype
== MachO::MH_KEXT_BUNDLE
&&
1967 MachOOF
->getHeader().cputype
== MachO::CPU_TYPE_ARM64
)
1968 DisassembleMachO(FileName
, MachOOF
, "__TEXT_EXEC", "__text");
1970 DisassembleMachO(FileName
, MachOOF
, "__TEXT", "__text");
1972 if (IndirectSymbols
)
1973 PrintIndirectSymbols(MachOOF
, !NonVerbose
);
1975 PrintDataInCodeTable(MachOOF
, !NonVerbose
);
1977 PrintLinkOptHints(MachOOF
);
1979 PrintRelocations(MachOOF
, !NonVerbose
);
1981 printSectionHeaders(MachOOF
);
1982 if (SectionContents
)
1983 printSectionContents(MachOOF
);
1984 if (!FilterSections
.empty())
1985 DumpSectionContents(FileName
, MachOOF
, !NonVerbose
);
1987 DumpInfoPlistSectionContents(FileName
, MachOOF
);
1989 PrintDylibs(MachOOF
, false);
1991 PrintDylibs(MachOOF
, true);
1993 printSymbolTable(MachOOF
, ArchiveName
, ArchitectureName
);
1995 printMachOUnwindInfo(MachOOF
);
1996 if (PrivateHeaders
) {
1997 printMachOFileHeader(MachOOF
);
1998 printMachOLoadCommands(MachOOF
);
2000 if (FirstPrivateHeader
)
2001 printMachOFileHeader(MachOOF
);
2003 printObjcMetaData(MachOOF
, !NonVerbose
);
2005 printExportsTrie(MachOOF
);
2007 printRebaseTable(MachOOF
);
2009 printBindTable(MachOOF
);
2011 printLazyBindTable(MachOOF
);
2013 printWeakBindTable(MachOOF
);
2015 if (DwarfDumpType
!= DIDT_Null
) {
2016 std::unique_ptr
<DIContext
> DICtx
= DWARFContext::create(*MachOOF
);
2017 // Dump the complete DWARF structure.
2018 DIDumpOptions DumpOpts
;
2019 DumpOpts
.DumpType
= DwarfDumpType
;
2020 DICtx
->dump(outs(), DumpOpts
);
2024 // printUnknownCPUType() helps print_fat_headers for unknown CPU's.
2025 static void printUnknownCPUType(uint32_t cputype
, uint32_t cpusubtype
) {
2026 outs() << " cputype (" << cputype
<< ")\n";
2027 outs() << " cpusubtype (" << cpusubtype
<< ")\n";
2030 // printCPUType() helps print_fat_headers by printing the cputype and
2031 // pusubtype (symbolically for the one's it knows about).
2032 static void printCPUType(uint32_t cputype
, uint32_t cpusubtype
) {
2034 case MachO::CPU_TYPE_I386
:
2035 switch (cpusubtype
) {
2036 case MachO::CPU_SUBTYPE_I386_ALL
:
2037 outs() << " cputype CPU_TYPE_I386\n";
2038 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
2041 printUnknownCPUType(cputype
, cpusubtype
);
2045 case MachO::CPU_TYPE_X86_64
:
2046 switch (cpusubtype
) {
2047 case MachO::CPU_SUBTYPE_X86_64_ALL
:
2048 outs() << " cputype CPU_TYPE_X86_64\n";
2049 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
2051 case MachO::CPU_SUBTYPE_X86_64_H
:
2052 outs() << " cputype CPU_TYPE_X86_64\n";
2053 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
2056 printUnknownCPUType(cputype
, cpusubtype
);
2060 case MachO::CPU_TYPE_ARM
:
2061 switch (cpusubtype
) {
2062 case MachO::CPU_SUBTYPE_ARM_ALL
:
2063 outs() << " cputype CPU_TYPE_ARM\n";
2064 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
2066 case MachO::CPU_SUBTYPE_ARM_V4T
:
2067 outs() << " cputype CPU_TYPE_ARM\n";
2068 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
2070 case MachO::CPU_SUBTYPE_ARM_V5TEJ
:
2071 outs() << " cputype CPU_TYPE_ARM\n";
2072 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
2074 case MachO::CPU_SUBTYPE_ARM_XSCALE
:
2075 outs() << " cputype CPU_TYPE_ARM\n";
2076 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
2078 case MachO::CPU_SUBTYPE_ARM_V6
:
2079 outs() << " cputype CPU_TYPE_ARM\n";
2080 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
2082 case MachO::CPU_SUBTYPE_ARM_V6M
:
2083 outs() << " cputype CPU_TYPE_ARM\n";
2084 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
2086 case MachO::CPU_SUBTYPE_ARM_V7
:
2087 outs() << " cputype CPU_TYPE_ARM\n";
2088 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
2090 case MachO::CPU_SUBTYPE_ARM_V7EM
:
2091 outs() << " cputype CPU_TYPE_ARM\n";
2092 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
2094 case MachO::CPU_SUBTYPE_ARM_V7K
:
2095 outs() << " cputype CPU_TYPE_ARM\n";
2096 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
2098 case MachO::CPU_SUBTYPE_ARM_V7M
:
2099 outs() << " cputype CPU_TYPE_ARM\n";
2100 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
2102 case MachO::CPU_SUBTYPE_ARM_V7S
:
2103 outs() << " cputype CPU_TYPE_ARM\n";
2104 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
2107 printUnknownCPUType(cputype
, cpusubtype
);
2111 case MachO::CPU_TYPE_ARM64
:
2112 switch (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) {
2113 case MachO::CPU_SUBTYPE_ARM64_ALL
:
2114 outs() << " cputype CPU_TYPE_ARM64\n";
2115 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
2117 case MachO::CPU_SUBTYPE_ARM64E
:
2118 outs() << " cputype CPU_TYPE_ARM64\n";
2119 outs() << " cpusubtype CPU_SUBTYPE_ARM64E\n";
2122 printUnknownCPUType(cputype
, cpusubtype
);
2126 case MachO::CPU_TYPE_ARM64_32
:
2127 switch (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) {
2128 case MachO::CPU_SUBTYPE_ARM64_32_V8
:
2129 outs() << " cputype CPU_TYPE_ARM64_32\n";
2130 outs() << " cpusubtype CPU_SUBTYPE_ARM64_32_V8\n";
2133 printUnknownCPUType(cputype
, cpusubtype
);
2138 printUnknownCPUType(cputype
, cpusubtype
);
2143 static void printMachOUniversalHeaders(const object::MachOUniversalBinary
*UB
,
2145 outs() << "Fat headers\n";
2147 if (UB
->getMagic() == MachO::FAT_MAGIC
)
2148 outs() << "fat_magic FAT_MAGIC\n";
2149 else // UB->getMagic() == MachO::FAT_MAGIC_64
2150 outs() << "fat_magic FAT_MAGIC_64\n";
2152 outs() << "fat_magic " << format("0x%" PRIx32
, MachO::FAT_MAGIC
) << "\n";
2154 uint32_t nfat_arch
= UB
->getNumberOfObjects();
2155 StringRef Buf
= UB
->getData();
2156 uint64_t size
= Buf
.size();
2157 uint64_t big_size
= sizeof(struct MachO::fat_header
) +
2158 nfat_arch
* sizeof(struct MachO::fat_arch
);
2159 outs() << "nfat_arch " << UB
->getNumberOfObjects();
2161 outs() << " (malformed, contains zero architecture types)\n";
2162 else if (big_size
> size
)
2163 outs() << " (malformed, architectures past end of file)\n";
2167 for (uint32_t i
= 0; i
< nfat_arch
; ++i
) {
2168 MachOUniversalBinary::ObjectForArch
OFA(UB
, i
);
2169 uint32_t cputype
= OFA
.getCPUType();
2170 uint32_t cpusubtype
= OFA
.getCPUSubType();
2171 outs() << "architecture ";
2172 for (uint32_t j
= 0; i
!= 0 && j
<= i
- 1; j
++) {
2173 MachOUniversalBinary::ObjectForArch
other_OFA(UB
, j
);
2174 uint32_t other_cputype
= other_OFA
.getCPUType();
2175 uint32_t other_cpusubtype
= other_OFA
.getCPUSubType();
2176 if (cputype
!= 0 && cpusubtype
!= 0 && cputype
== other_cputype
&&
2177 (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) ==
2178 (other_cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
)) {
2179 outs() << "(illegal duplicate architecture) ";
2184 outs() << OFA
.getArchFlagName() << "\n";
2185 printCPUType(cputype
, cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
2187 outs() << i
<< "\n";
2188 outs() << " cputype " << cputype
<< "\n";
2189 outs() << " cpusubtype " << (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
)
2193 (cpusubtype
& MachO::CPU_SUBTYPE_MASK
) == MachO::CPU_SUBTYPE_LIB64
)
2194 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
2196 outs() << " capabilities "
2197 << format("0x%" PRIx32
,
2198 (cpusubtype
& MachO::CPU_SUBTYPE_MASK
) >> 24) << "\n";
2199 outs() << " offset " << OFA
.getOffset();
2200 if (OFA
.getOffset() > size
)
2201 outs() << " (past end of file)";
2202 if (OFA
.getOffset() % (1 << OFA
.getAlign()) != 0)
2203 outs() << " (not aligned on it's alignment (2^" << OFA
.getAlign() << ")";
2205 outs() << " size " << OFA
.getSize();
2206 big_size
= OFA
.getOffset() + OFA
.getSize();
2207 if (big_size
> size
)
2208 outs() << " (past end of file)";
2210 outs() << " align 2^" << OFA
.getAlign() << " (" << (1 << OFA
.getAlign())
2215 static void printArchiveChild(StringRef Filename
, const Archive::Child
&C
,
2216 size_t ChildIndex
, bool verbose
,
2218 StringRef ArchitectureName
= StringRef()) {
2220 outs() << C
.getChildOffset() << "\t";
2221 sys::fs::perms Mode
=
2222 unwrapOrError(C
.getAccessMode(), getFileNameForError(C
, ChildIndex
),
2223 Filename
, ArchitectureName
);
2225 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
2226 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
2228 outs() << ((Mode
& sys::fs::owner_read
) ? "r" : "-");
2229 outs() << ((Mode
& sys::fs::owner_write
) ? "w" : "-");
2230 outs() << ((Mode
& sys::fs::owner_exe
) ? "x" : "-");
2231 outs() << ((Mode
& sys::fs::group_read
) ? "r" : "-");
2232 outs() << ((Mode
& sys::fs::group_write
) ? "w" : "-");
2233 outs() << ((Mode
& sys::fs::group_exe
) ? "x" : "-");
2234 outs() << ((Mode
& sys::fs::others_read
) ? "r" : "-");
2235 outs() << ((Mode
& sys::fs::others_write
) ? "w" : "-");
2236 outs() << ((Mode
& sys::fs::others_exe
) ? "x" : "-");
2238 outs() << format("0%o ", Mode
);
2241 outs() << format("%3d/%-3d %5" PRId64
" ",
2242 unwrapOrError(C
.getUID(), getFileNameForError(C
, ChildIndex
),
2243 Filename
, ArchitectureName
),
2244 unwrapOrError(C
.getGID(), getFileNameForError(C
, ChildIndex
),
2245 Filename
, ArchitectureName
),
2246 unwrapOrError(C
.getRawSize(),
2247 getFileNameForError(C
, ChildIndex
), Filename
,
2250 StringRef RawLastModified
= C
.getRawLastModified();
2253 if (RawLastModified
.getAsInteger(10, Seconds
))
2254 outs() << "(date: \"" << RawLastModified
2255 << "\" contains non-decimal chars) ";
2257 // Since cime(3) returns a 26 character string of the form:
2258 // "Sun Sep 16 01:03:52 1973\n\0"
2259 // just print 24 characters.
2261 outs() << format("%.24s ", ctime(&t
));
2264 outs() << RawLastModified
<< " ";
2268 Expected
<StringRef
> NameOrErr
= C
.getName();
2270 consumeError(NameOrErr
.takeError());
2271 outs() << unwrapOrError(C
.getRawName(),
2272 getFileNameForError(C
, ChildIndex
), Filename
,
2276 StringRef Name
= NameOrErr
.get();
2277 outs() << Name
<< "\n";
2280 outs() << unwrapOrError(C
.getRawName(), getFileNameForError(C
, ChildIndex
),
2281 Filename
, ArchitectureName
)
2286 static void printArchiveHeaders(StringRef Filename
, Archive
*A
, bool verbose
,
2288 StringRef ArchitectureName
= StringRef()) {
2289 Error Err
= Error::success();
2291 for (const auto &C
: A
->children(Err
, false))
2292 printArchiveChild(Filename
, C
, I
++, verbose
, print_offset
,
2296 reportError(std::move(Err
), Filename
, "", ArchitectureName
);
2299 static bool ValidateArchFlags() {
2300 // Check for -arch all and verifiy the -arch flags are valid.
2301 for (unsigned i
= 0; i
< ArchFlags
.size(); ++i
) {
2302 if (ArchFlags
[i
] == "all") {
2305 if (!MachOObjectFile::isValidArch(ArchFlags
[i
])) {
2306 WithColor::error(errs(), "llvm-objdump")
2307 << "unknown architecture named '" + ArchFlags
[i
] +
2308 "'for the -arch option\n";
2316 // ParseInputMachO() parses the named Mach-O file in Filename and handles the
2317 // -arch flags selecting just those slices as specified by them and also parses
2318 // archive files. Then for each individual Mach-O file ProcessMachO() is
2319 // called to process the file based on the command line options.
2320 void parseInputMachO(StringRef Filename
) {
2321 if (!ValidateArchFlags())
2324 // Attempt to open the binary.
2325 Expected
<OwningBinary
<Binary
>> BinaryOrErr
= createBinary(Filename
);
2327 if (Error E
= isNotObjectErrorInvalidFileType(BinaryOrErr
.takeError()))
2328 reportError(std::move(E
), Filename
);
2330 outs() << Filename
<< ": is not an object file\n";
2333 Binary
&Bin
= *BinaryOrErr
.get().getBinary();
2335 if (Archive
*A
= dyn_cast
<Archive
>(&Bin
)) {
2336 outs() << "Archive : " << Filename
<< "\n";
2338 printArchiveHeaders(Filename
, A
, !NonVerbose
, ArchiveMemberOffsets
);
2340 Error Err
= Error::success();
2342 for (auto &C
: A
->children(Err
)) {
2344 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
= C
.getAsBinary();
2346 if (Error E
= isNotObjectErrorInvalidFileType(ChildOrErr
.takeError()))
2347 reportError(std::move(E
), getFileNameForError(C
, I
), Filename
);
2350 if (MachOObjectFile
*O
= dyn_cast
<MachOObjectFile
>(&*ChildOrErr
.get())) {
2351 if (!checkMachOAndArchFlags(O
, Filename
))
2353 ProcessMachO(Filename
, O
, O
->getFileName());
2357 reportError(std::move(Err
), Filename
);
2360 if (MachOUniversalBinary
*UB
= dyn_cast
<MachOUniversalBinary
>(&Bin
)) {
2361 parseInputMachO(UB
);
2364 if (ObjectFile
*O
= dyn_cast
<ObjectFile
>(&Bin
)) {
2365 if (!checkMachOAndArchFlags(O
, Filename
))
2367 if (MachOObjectFile
*MachOOF
= dyn_cast
<MachOObjectFile
>(&*O
))
2368 ProcessMachO(Filename
, MachOOF
);
2370 WithColor::error(errs(), "llvm-objdump")
2371 << Filename
<< "': "
2372 << "object is not a Mach-O file type.\n";
2375 llvm_unreachable("Input object can't be invalid at this point");
2378 void parseInputMachO(MachOUniversalBinary
*UB
) {
2379 if (!ValidateArchFlags())
2382 auto Filename
= UB
->getFileName();
2384 if (UniversalHeaders
)
2385 printMachOUniversalHeaders(UB
, !NonVerbose
);
2387 // If we have a list of architecture flags specified dump only those.
2388 if (!ArchAll
&& !ArchFlags
.empty()) {
2389 // Look for a slice in the universal binary that matches each ArchFlag.
2391 for (unsigned i
= 0; i
< ArchFlags
.size(); ++i
) {
2393 for (MachOUniversalBinary::object_iterator I
= UB
->begin_objects(),
2394 E
= UB
->end_objects();
2396 if (ArchFlags
[i
] == I
->getArchFlagName()) {
2398 Expected
<std::unique_ptr
<ObjectFile
>> ObjOrErr
=
2399 I
->getAsObjectFile();
2400 std::string ArchitectureName
= "";
2401 if (ArchFlags
.size() > 1)
2402 ArchitectureName
= I
->getArchFlagName();
2404 ObjectFile
&O
= *ObjOrErr
.get();
2405 if (MachOObjectFile
*MachOOF
= dyn_cast
<MachOObjectFile
>(&O
))
2406 ProcessMachO(Filename
, MachOOF
, "", ArchitectureName
);
2407 } else if (Error E
= isNotObjectErrorInvalidFileType(
2408 ObjOrErr
.takeError())) {
2409 reportError(std::move(E
), "", Filename
, ArchitectureName
);
2411 } else if (Expected
<std::unique_ptr
<Archive
>> AOrErr
=
2412 I
->getAsArchive()) {
2413 std::unique_ptr
<Archive
> &A
= *AOrErr
;
2414 outs() << "Archive : " << Filename
;
2415 if (!ArchitectureName
.empty())
2416 outs() << " (architecture " << ArchitectureName
<< ")";
2419 printArchiveHeaders(Filename
, A
.get(), !NonVerbose
,
2420 ArchiveMemberOffsets
, ArchitectureName
);
2421 Error Err
= Error::success();
2423 for (auto &C
: A
->children(Err
)) {
2425 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
= C
.getAsBinary();
2428 isNotObjectErrorInvalidFileType(ChildOrErr
.takeError()))
2429 reportError(std::move(E
), getFileNameForError(C
, I
), Filename
,
2433 if (MachOObjectFile
*O
=
2434 dyn_cast
<MachOObjectFile
>(&*ChildOrErr
.get()))
2435 ProcessMachO(Filename
, O
, O
->getFileName(), ArchitectureName
);
2438 reportError(std::move(Err
), Filename
);
2440 consumeError(AOrErr
.takeError());
2441 reportError(Filename
,
2442 "Mach-O universal file for architecture " +
2443 StringRef(I
->getArchFlagName()) +
2444 " is not a Mach-O file or an archive file");
2449 WithColor::error(errs(), "llvm-objdump")
2450 << "file: " + Filename
+ " does not contain "
2451 << "architecture: " + ArchFlags
[i
] + "\n";
2457 // No architecture flags were specified so if this contains a slice that
2458 // matches the host architecture dump only that.
2460 for (MachOUniversalBinary::object_iterator I
= UB
->begin_objects(),
2461 E
= UB
->end_objects();
2463 if (MachOObjectFile::getHostArch().getArchName() ==
2464 I
->getArchFlagName()) {
2465 Expected
<std::unique_ptr
<ObjectFile
>> ObjOrErr
= I
->getAsObjectFile();
2466 std::string ArchiveName
;
2467 ArchiveName
.clear();
2469 ObjectFile
&O
= *ObjOrErr
.get();
2470 if (MachOObjectFile
*MachOOF
= dyn_cast
<MachOObjectFile
>(&O
))
2471 ProcessMachO(Filename
, MachOOF
);
2472 } else if (Error E
=
2473 isNotObjectErrorInvalidFileType(ObjOrErr
.takeError())) {
2474 reportError(std::move(E
), Filename
);
2475 } else if (Expected
<std::unique_ptr
<Archive
>> AOrErr
=
2476 I
->getAsArchive()) {
2477 std::unique_ptr
<Archive
> &A
= *AOrErr
;
2478 outs() << "Archive : " << Filename
<< "\n";
2480 printArchiveHeaders(Filename
, A
.get(), !NonVerbose
,
2481 ArchiveMemberOffsets
);
2482 Error Err
= Error::success();
2484 for (auto &C
: A
->children(Err
)) {
2486 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
= C
.getAsBinary();
2489 isNotObjectErrorInvalidFileType(ChildOrErr
.takeError()))
2490 reportError(std::move(E
), getFileNameForError(C
, I
), Filename
);
2493 if (MachOObjectFile
*O
=
2494 dyn_cast
<MachOObjectFile
>(&*ChildOrErr
.get()))
2495 ProcessMachO(Filename
, O
, O
->getFileName());
2498 reportError(std::move(Err
), Filename
);
2500 consumeError(AOrErr
.takeError());
2501 reportError(Filename
, "Mach-O universal file for architecture " +
2502 StringRef(I
->getArchFlagName()) +
2503 " is not a Mach-O file or an archive file");
2509 // Either all architectures have been specified or none have been specified
2510 // and this does not contain the host architecture so dump all the slices.
2511 bool moreThanOneArch
= UB
->getNumberOfObjects() > 1;
2512 for (MachOUniversalBinary::object_iterator I
= UB
->begin_objects(),
2513 E
= UB
->end_objects();
2515 Expected
<std::unique_ptr
<ObjectFile
>> ObjOrErr
= I
->getAsObjectFile();
2516 std::string ArchitectureName
= "";
2517 if (moreThanOneArch
)
2518 ArchitectureName
= I
->getArchFlagName();
2520 ObjectFile
&Obj
= *ObjOrErr
.get();
2521 if (MachOObjectFile
*MachOOF
= dyn_cast
<MachOObjectFile
>(&Obj
))
2522 ProcessMachO(Filename
, MachOOF
, "", ArchitectureName
);
2523 } else if (Error E
=
2524 isNotObjectErrorInvalidFileType(ObjOrErr
.takeError())) {
2525 reportError(std::move(E
), Filename
, "", ArchitectureName
);
2526 } else if (Expected
<std::unique_ptr
<Archive
>> AOrErr
= I
->getAsArchive()) {
2527 std::unique_ptr
<Archive
> &A
= *AOrErr
;
2528 outs() << "Archive : " << Filename
;
2529 if (!ArchitectureName
.empty())
2530 outs() << " (architecture " << ArchitectureName
<< ")";
2533 printArchiveHeaders(Filename
, A
.get(), !NonVerbose
,
2534 ArchiveMemberOffsets
, ArchitectureName
);
2535 Error Err
= Error::success();
2537 for (auto &C
: A
->children(Err
)) {
2539 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
= C
.getAsBinary();
2541 if (Error E
= isNotObjectErrorInvalidFileType(ChildOrErr
.takeError()))
2542 reportError(std::move(E
), getFileNameForError(C
, I
), Filename
,
2546 if (MachOObjectFile
*O
=
2547 dyn_cast
<MachOObjectFile
>(&*ChildOrErr
.get())) {
2548 if (MachOObjectFile
*MachOOF
= dyn_cast
<MachOObjectFile
>(O
))
2549 ProcessMachO(Filename
, MachOOF
, MachOOF
->getFileName(),
2554 reportError(std::move(Err
), Filename
);
2556 consumeError(AOrErr
.takeError());
2557 reportError(Filename
, "Mach-O universal file for architecture " +
2558 StringRef(I
->getArchFlagName()) +
2559 " is not a Mach-O file or an archive file");
2564 // The block of info used by the Symbolizer call backs.
2565 struct DisassembleInfo
{
2566 DisassembleInfo(MachOObjectFile
*O
, SymbolAddressMap
*AddrMap
,
2567 std::vector
<SectionRef
> *Sections
, bool verbose
)
2568 : verbose(verbose
), O(O
), AddrMap(AddrMap
), Sections(Sections
) {}
2572 SymbolAddressMap
*AddrMap
;
2573 std::vector
<SectionRef
> *Sections
;
2574 const char *class_name
= nullptr;
2575 const char *selector_name
= nullptr;
2576 std::unique_ptr
<char[]> method
= nullptr;
2577 char *demangled_name
= nullptr;
2578 uint64_t adrp_addr
= 0;
2579 uint32_t adrp_inst
= 0;
2580 std::unique_ptr
<SymbolAddressMap
> bindtable
;
2584 // SymbolizerGetOpInfo() is the operand information call back function.
2585 // This is called to get the symbolic information for operand(s) of an
2586 // instruction when it is being done. This routine does this from
2587 // the relocation information, symbol table, etc. That block of information
2588 // is a pointer to the struct DisassembleInfo that was passed when the
2589 // disassembler context was created and passed to back to here when
2590 // called back by the disassembler for instruction operands that could have
2591 // relocation information. The address of the instruction containing operand is
2592 // at the Pc parameter. The immediate value the operand has is passed in
2593 // op_info->Value and is at Offset past the start of the instruction and has a
2594 // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
2595 // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
2596 // names and addends of the symbolic expression to add for the operand. The
2597 // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
2598 // information is returned then this function returns 1 else it returns 0.
2599 static int SymbolizerGetOpInfo(void *DisInfo
, uint64_t Pc
, uint64_t Offset
,
2600 uint64_t Size
, int TagType
, void *TagBuf
) {
2601 struct DisassembleInfo
*info
= (struct DisassembleInfo
*)DisInfo
;
2602 struct LLVMOpInfo1
*op_info
= (struct LLVMOpInfo1
*)TagBuf
;
2603 uint64_t value
= op_info
->Value
;
2605 // Make sure all fields returned are zero if we don't set them.
2606 memset((void *)op_info
, '\0', sizeof(struct LLVMOpInfo1
));
2607 op_info
->Value
= value
;
2609 // If the TagType is not the value 1 which it code knows about or if no
2610 // verbose symbolic information is wanted then just return 0, indicating no
2611 // information is being returned.
2612 if (TagType
!= 1 || !info
->verbose
)
2615 unsigned int Arch
= info
->O
->getArch();
2616 if (Arch
== Triple::x86
) {
2617 if (Size
!= 1 && Size
!= 2 && Size
!= 4 && Size
!= 0)
2619 if (info
->O
->getHeader().filetype
!= MachO::MH_OBJECT
) {
2621 // Search the external relocation entries of a fully linked image
2622 // (if any) for an entry that matches this segment offset.
2623 // uint32_t seg_offset = (Pc + Offset);
2626 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2627 // for an entry for this section offset.
2628 uint32_t sect_addr
= info
->S
.getAddress();
2629 uint32_t sect_offset
= (Pc
+ Offset
) - sect_addr
;
2630 bool reloc_found
= false;
2632 MachO::any_relocation_info RE
;
2633 bool isExtern
= false;
2635 bool r_scattered
= false;
2636 uint32_t r_value
, pair_r_value
, r_type
;
2637 for (const RelocationRef
&Reloc
: info
->S
.relocations()) {
2638 uint64_t RelocOffset
= Reloc
.getOffset();
2639 if (RelocOffset
== sect_offset
) {
2640 Rel
= Reloc
.getRawDataRefImpl();
2641 RE
= info
->O
->getRelocation(Rel
);
2642 r_type
= info
->O
->getAnyRelocationType(RE
);
2643 r_scattered
= info
->O
->isRelocationScattered(RE
);
2645 r_value
= info
->O
->getScatteredRelocationValue(RE
);
2646 if (r_type
== MachO::GENERIC_RELOC_SECTDIFF
||
2647 r_type
== MachO::GENERIC_RELOC_LOCAL_SECTDIFF
) {
2648 DataRefImpl RelNext
= Rel
;
2649 info
->O
->moveRelocationNext(RelNext
);
2650 MachO::any_relocation_info RENext
;
2651 RENext
= info
->O
->getRelocation(RelNext
);
2652 if (info
->O
->isRelocationScattered(RENext
))
2653 pair_r_value
= info
->O
->getScatteredRelocationValue(RENext
);
2658 isExtern
= info
->O
->getPlainRelocationExternal(RE
);
2660 symbol_iterator RelocSym
= Reloc
.getSymbol();
2668 if (reloc_found
&& isExtern
) {
2669 op_info
->AddSymbol
.Present
= 1;
2670 op_info
->AddSymbol
.Name
=
2671 unwrapOrError(Symbol
.getName(), info
->O
->getFileName()).data();
2672 // For i386 extern relocation entries the value in the instruction is
2673 // the offset from the symbol, and value is already set in op_info->Value.
2676 if (reloc_found
&& (r_type
== MachO::GENERIC_RELOC_SECTDIFF
||
2677 r_type
== MachO::GENERIC_RELOC_LOCAL_SECTDIFF
)) {
2678 const char *add
= GuessSymbolName(r_value
, info
->AddrMap
);
2679 const char *sub
= GuessSymbolName(pair_r_value
, info
->AddrMap
);
2680 uint32_t offset
= value
- (r_value
- pair_r_value
);
2681 op_info
->AddSymbol
.Present
= 1;
2683 op_info
->AddSymbol
.Name
= add
;
2685 op_info
->AddSymbol
.Value
= r_value
;
2686 op_info
->SubtractSymbol
.Present
= 1;
2688 op_info
->SubtractSymbol
.Name
= sub
;
2690 op_info
->SubtractSymbol
.Value
= pair_r_value
;
2691 op_info
->Value
= offset
;
2696 if (Arch
== Triple::x86_64
) {
2697 if (Size
!= 1 && Size
!= 2 && Size
!= 4 && Size
!= 0)
2699 // For non MH_OBJECT types, like MH_KEXT_BUNDLE, Search the external
2700 // relocation entries of a linked image (if any) for an entry that matches
2701 // this segment offset.
2702 if (info
->O
->getHeader().filetype
!= MachO::MH_OBJECT
) {
2703 uint64_t seg_offset
= Pc
+ Offset
;
2704 bool reloc_found
= false;
2706 MachO::any_relocation_info RE
;
2707 bool isExtern
= false;
2709 for (const RelocationRef
&Reloc
: info
->O
->external_relocations()) {
2710 uint64_t RelocOffset
= Reloc
.getOffset();
2711 if (RelocOffset
== seg_offset
) {
2712 Rel
= Reloc
.getRawDataRefImpl();
2713 RE
= info
->O
->getRelocation(Rel
);
2714 // external relocation entries should always be external.
2715 isExtern
= info
->O
->getPlainRelocationExternal(RE
);
2717 symbol_iterator RelocSym
= Reloc
.getSymbol();
2724 if (reloc_found
&& isExtern
) {
2725 // The Value passed in will be adjusted by the Pc if the instruction
2726 // adds the Pc. But for x86_64 external relocation entries the Value
2727 // is the offset from the external symbol.
2728 if (info
->O
->getAnyRelocationPCRel(RE
))
2729 op_info
->Value
-= Pc
+ Offset
+ Size
;
2731 unwrapOrError(Symbol
.getName(), info
->O
->getFileName()).data();
2732 op_info
->AddSymbol
.Present
= 1;
2733 op_info
->AddSymbol
.Name
= name
;
2738 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2739 // for an entry for this section offset.
2740 uint64_t sect_addr
= info
->S
.getAddress();
2741 uint64_t sect_offset
= (Pc
+ Offset
) - sect_addr
;
2742 bool reloc_found
= false;
2744 MachO::any_relocation_info RE
;
2745 bool isExtern
= false;
2747 for (const RelocationRef
&Reloc
: info
->S
.relocations()) {
2748 uint64_t RelocOffset
= Reloc
.getOffset();
2749 if (RelocOffset
== sect_offset
) {
2750 Rel
= Reloc
.getRawDataRefImpl();
2751 RE
= info
->O
->getRelocation(Rel
);
2752 // NOTE: Scattered relocations don't exist on x86_64.
2753 isExtern
= info
->O
->getPlainRelocationExternal(RE
);
2755 symbol_iterator RelocSym
= Reloc
.getSymbol();
2762 if (reloc_found
&& isExtern
) {
2763 // The Value passed in will be adjusted by the Pc if the instruction
2764 // adds the Pc. But for x86_64 external relocation entries the Value
2765 // is the offset from the external symbol.
2766 if (info
->O
->getAnyRelocationPCRel(RE
))
2767 op_info
->Value
-= Pc
+ Offset
+ Size
;
2769 unwrapOrError(Symbol
.getName(), info
->O
->getFileName()).data();
2770 unsigned Type
= info
->O
->getAnyRelocationType(RE
);
2771 if (Type
== MachO::X86_64_RELOC_SUBTRACTOR
) {
2772 DataRefImpl RelNext
= Rel
;
2773 info
->O
->moveRelocationNext(RelNext
);
2774 MachO::any_relocation_info RENext
= info
->O
->getRelocation(RelNext
);
2775 unsigned TypeNext
= info
->O
->getAnyRelocationType(RENext
);
2776 bool isExternNext
= info
->O
->getPlainRelocationExternal(RENext
);
2777 unsigned SymbolNum
= info
->O
->getPlainRelocationSymbolNum(RENext
);
2778 if (TypeNext
== MachO::X86_64_RELOC_UNSIGNED
&& isExternNext
) {
2779 op_info
->SubtractSymbol
.Present
= 1;
2780 op_info
->SubtractSymbol
.Name
= name
;
2781 symbol_iterator RelocSymNext
= info
->O
->getSymbolByIndex(SymbolNum
);
2782 Symbol
= *RelocSymNext
;
2783 name
= unwrapOrError(Symbol
.getName(), info
->O
->getFileName()).data();
2786 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
2787 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
2788 op_info
->AddSymbol
.Present
= 1;
2789 op_info
->AddSymbol
.Name
= name
;
2794 if (Arch
== Triple::arm
) {
2795 if (Offset
!= 0 || (Size
!= 4 && Size
!= 2))
2797 if (info
->O
->getHeader().filetype
!= MachO::MH_OBJECT
) {
2799 // Search the external relocation entries of a fully linked image
2800 // (if any) for an entry that matches this segment offset.
2801 // uint32_t seg_offset = (Pc + Offset);
2804 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2805 // for an entry for this section offset.
2806 uint32_t sect_addr
= info
->S
.getAddress();
2807 uint32_t sect_offset
= (Pc
+ Offset
) - sect_addr
;
2809 MachO::any_relocation_info RE
;
2810 bool isExtern
= false;
2812 bool r_scattered
= false;
2813 uint32_t r_value
, pair_r_value
, r_type
, r_length
, other_half
;
2815 find_if(info
->S
.relocations(), [&](const RelocationRef
&Reloc
) {
2816 uint64_t RelocOffset
= Reloc
.getOffset();
2817 return RelocOffset
== sect_offset
;
2820 if (Reloc
== info
->S
.relocations().end())
2823 Rel
= Reloc
->getRawDataRefImpl();
2824 RE
= info
->O
->getRelocation(Rel
);
2825 r_length
= info
->O
->getAnyRelocationLength(RE
);
2826 r_scattered
= info
->O
->isRelocationScattered(RE
);
2828 r_value
= info
->O
->getScatteredRelocationValue(RE
);
2829 r_type
= info
->O
->getScatteredRelocationType(RE
);
2831 r_type
= info
->O
->getAnyRelocationType(RE
);
2832 isExtern
= info
->O
->getPlainRelocationExternal(RE
);
2834 symbol_iterator RelocSym
= Reloc
->getSymbol();
2838 if (r_type
== MachO::ARM_RELOC_HALF
||
2839 r_type
== MachO::ARM_RELOC_SECTDIFF
||
2840 r_type
== MachO::ARM_RELOC_LOCAL_SECTDIFF
||
2841 r_type
== MachO::ARM_RELOC_HALF_SECTDIFF
) {
2842 DataRefImpl RelNext
= Rel
;
2843 info
->O
->moveRelocationNext(RelNext
);
2844 MachO::any_relocation_info RENext
;
2845 RENext
= info
->O
->getRelocation(RelNext
);
2846 other_half
= info
->O
->getAnyRelocationAddress(RENext
) & 0xffff;
2847 if (info
->O
->isRelocationScattered(RENext
))
2848 pair_r_value
= info
->O
->getScatteredRelocationValue(RENext
);
2853 unwrapOrError(Symbol
.getName(), info
->O
->getFileName()).data();
2854 op_info
->AddSymbol
.Present
= 1;
2855 op_info
->AddSymbol
.Name
= name
;
2857 case MachO::ARM_RELOC_HALF
:
2858 if ((r_length
& 0x1) == 1) {
2859 op_info
->Value
= value
<< 16 | other_half
;
2860 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM_HI16
;
2862 op_info
->Value
= other_half
<< 16 | value
;
2863 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM_LO16
;
2871 // If we have a branch that is not an external relocation entry then
2872 // return 0 so the code in tryAddingSymbolicOperand() can use the
2873 // SymbolLookUp call back with the branch target address to look up the
2874 // symbol and possibility add an annotation for a symbol stub.
2875 if (isExtern
== 0 && (r_type
== MachO::ARM_RELOC_BR24
||
2876 r_type
== MachO::ARM_THUMB_RELOC_BR22
))
2879 uint32_t offset
= 0;
2880 if (r_type
== MachO::ARM_RELOC_HALF
||
2881 r_type
== MachO::ARM_RELOC_HALF_SECTDIFF
) {
2882 if ((r_length
& 0x1) == 1)
2883 value
= value
<< 16 | other_half
;
2885 value
= other_half
<< 16 | value
;
2887 if (r_scattered
&& (r_type
!= MachO::ARM_RELOC_HALF
&&
2888 r_type
!= MachO::ARM_RELOC_HALF_SECTDIFF
)) {
2889 offset
= value
- r_value
;
2893 if (r_type
== MachO::ARM_RELOC_HALF_SECTDIFF
) {
2894 if ((r_length
& 0x1) == 1)
2895 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM_HI16
;
2897 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM_LO16
;
2898 const char *add
= GuessSymbolName(r_value
, info
->AddrMap
);
2899 const char *sub
= GuessSymbolName(pair_r_value
, info
->AddrMap
);
2900 int32_t offset
= value
- (r_value
- pair_r_value
);
2901 op_info
->AddSymbol
.Present
= 1;
2903 op_info
->AddSymbol
.Name
= add
;
2905 op_info
->AddSymbol
.Value
= r_value
;
2906 op_info
->SubtractSymbol
.Present
= 1;
2908 op_info
->SubtractSymbol
.Name
= sub
;
2910 op_info
->SubtractSymbol
.Value
= pair_r_value
;
2911 op_info
->Value
= offset
;
2915 op_info
->AddSymbol
.Present
= 1;
2916 op_info
->Value
= offset
;
2917 if (r_type
== MachO::ARM_RELOC_HALF
) {
2918 if ((r_length
& 0x1) == 1)
2919 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM_HI16
;
2921 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM_LO16
;
2923 const char *add
= GuessSymbolName(value
, info
->AddrMap
);
2924 if (add
!= nullptr) {
2925 op_info
->AddSymbol
.Name
= add
;
2928 op_info
->AddSymbol
.Value
= value
;
2931 if (Arch
== Triple::aarch64
) {
2932 if (Offset
!= 0 || Size
!= 4)
2934 if (info
->O
->getHeader().filetype
!= MachO::MH_OBJECT
) {
2936 // Search the external relocation entries of a fully linked image
2937 // (if any) for an entry that matches this segment offset.
2938 // uint64_t seg_offset = (Pc + Offset);
2941 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2942 // for an entry for this section offset.
2943 uint64_t sect_addr
= info
->S
.getAddress();
2944 uint64_t sect_offset
= (Pc
+ Offset
) - sect_addr
;
2946 find_if(info
->S
.relocations(), [&](const RelocationRef
&Reloc
) {
2947 uint64_t RelocOffset
= Reloc
.getOffset();
2948 return RelocOffset
== sect_offset
;
2951 if (Reloc
== info
->S
.relocations().end())
2954 DataRefImpl Rel
= Reloc
->getRawDataRefImpl();
2955 MachO::any_relocation_info RE
= info
->O
->getRelocation(Rel
);
2956 uint32_t r_type
= info
->O
->getAnyRelocationType(RE
);
2957 if (r_type
== MachO::ARM64_RELOC_ADDEND
) {
2958 DataRefImpl RelNext
= Rel
;
2959 info
->O
->moveRelocationNext(RelNext
);
2960 MachO::any_relocation_info RENext
= info
->O
->getRelocation(RelNext
);
2962 value
= info
->O
->getPlainRelocationSymbolNum(RENext
);
2963 op_info
->Value
= value
;
2966 // NOTE: Scattered relocations don't exist on arm64.
2967 if (!info
->O
->getPlainRelocationExternal(RE
))
2970 unwrapOrError(Reloc
->getSymbol()->getName(), info
->O
->getFileName())
2972 op_info
->AddSymbol
.Present
= 1;
2973 op_info
->AddSymbol
.Name
= name
;
2976 case MachO::ARM64_RELOC_PAGE21
:
2978 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM64_PAGE
;
2980 case MachO::ARM64_RELOC_PAGEOFF12
:
2982 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM64_PAGEOFF
;
2984 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21
:
2986 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM64_GOTPAGE
;
2988 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12
:
2990 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF
;
2992 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21
:
2993 /* @tvlppage is not implemented in llvm-mc */
2994 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM64_TLVP
;
2996 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12
:
2997 /* @tvlppageoff is not implemented in llvm-mc */
2998 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM64_TLVOFF
;
3001 case MachO::ARM64_RELOC_BRANCH26
:
3002 op_info
->VariantKind
= LLVMDisassembler_VariantKind_None
;
3010 // GuessCstringPointer is passed the address of what might be a pointer to a
3011 // literal string in a cstring section. If that address is in a cstring section
3012 // it returns a pointer to that string. Else it returns nullptr.
3013 static const char *GuessCstringPointer(uint64_t ReferenceValue
,
3014 struct DisassembleInfo
*info
) {
3015 for (const auto &Load
: info
->O
->load_commands()) {
3016 if (Load
.C
.cmd
== MachO::LC_SEGMENT_64
) {
3017 MachO::segment_command_64 Seg
= info
->O
->getSegment64LoadCommand(Load
);
3018 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
3019 MachO::section_64 Sec
= info
->O
->getSection64(Load
, J
);
3020 uint32_t section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
3021 if (section_type
== MachO::S_CSTRING_LITERALS
&&
3022 ReferenceValue
>= Sec
.addr
&&
3023 ReferenceValue
< Sec
.addr
+ Sec
.size
) {
3024 uint64_t sect_offset
= ReferenceValue
- Sec
.addr
;
3025 uint64_t object_offset
= Sec
.offset
+ sect_offset
;
3026 StringRef MachOContents
= info
->O
->getData();
3027 uint64_t object_size
= MachOContents
.size();
3028 const char *object_addr
= (const char *)MachOContents
.data();
3029 if (object_offset
< object_size
) {
3030 const char *name
= object_addr
+ object_offset
;
3037 } else if (Load
.C
.cmd
== MachO::LC_SEGMENT
) {
3038 MachO::segment_command Seg
= info
->O
->getSegmentLoadCommand(Load
);
3039 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
3040 MachO::section Sec
= info
->O
->getSection(Load
, J
);
3041 uint32_t section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
3042 if (section_type
== MachO::S_CSTRING_LITERALS
&&
3043 ReferenceValue
>= Sec
.addr
&&
3044 ReferenceValue
< Sec
.addr
+ Sec
.size
) {
3045 uint64_t sect_offset
= ReferenceValue
- Sec
.addr
;
3046 uint64_t object_offset
= Sec
.offset
+ sect_offset
;
3047 StringRef MachOContents
= info
->O
->getData();
3048 uint64_t object_size
= MachOContents
.size();
3049 const char *object_addr
= (const char *)MachOContents
.data();
3050 if (object_offset
< object_size
) {
3051 const char *name
= object_addr
+ object_offset
;
3063 // GuessIndirectSymbol returns the name of the indirect symbol for the
3064 // ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
3065 // an address of a symbol stub or a lazy or non-lazy pointer to associate the
3066 // symbol name being referenced by the stub or pointer.
3067 static const char *GuessIndirectSymbol(uint64_t ReferenceValue
,
3068 struct DisassembleInfo
*info
) {
3069 MachO::dysymtab_command Dysymtab
= info
->O
->getDysymtabLoadCommand();
3070 MachO::symtab_command Symtab
= info
->O
->getSymtabLoadCommand();
3071 for (const auto &Load
: info
->O
->load_commands()) {
3072 if (Load
.C
.cmd
== MachO::LC_SEGMENT_64
) {
3073 MachO::segment_command_64 Seg
= info
->O
->getSegment64LoadCommand(Load
);
3074 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
3075 MachO::section_64 Sec
= info
->O
->getSection64(Load
, J
);
3076 uint32_t section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
3077 if ((section_type
== MachO::S_NON_LAZY_SYMBOL_POINTERS
||
3078 section_type
== MachO::S_LAZY_SYMBOL_POINTERS
||
3079 section_type
== MachO::S_LAZY_DYLIB_SYMBOL_POINTERS
||
3080 section_type
== MachO::S_THREAD_LOCAL_VARIABLE_POINTERS
||
3081 section_type
== MachO::S_SYMBOL_STUBS
) &&
3082 ReferenceValue
>= Sec
.addr
&&
3083 ReferenceValue
< Sec
.addr
+ Sec
.size
) {
3085 if (section_type
== MachO::S_SYMBOL_STUBS
)
3086 stride
= Sec
.reserved2
;
3091 uint32_t index
= Sec
.reserved1
+ (ReferenceValue
- Sec
.addr
) / stride
;
3092 if (index
< Dysymtab
.nindirectsyms
) {
3093 uint32_t indirect_symbol
=
3094 info
->O
->getIndirectSymbolTableEntry(Dysymtab
, index
);
3095 if (indirect_symbol
< Symtab
.nsyms
) {
3096 symbol_iterator Sym
= info
->O
->getSymbolByIndex(indirect_symbol
);
3097 return unwrapOrError(Sym
->getName(), info
->O
->getFileName())
3103 } else if (Load
.C
.cmd
== MachO::LC_SEGMENT
) {
3104 MachO::segment_command Seg
= info
->O
->getSegmentLoadCommand(Load
);
3105 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
3106 MachO::section Sec
= info
->O
->getSection(Load
, J
);
3107 uint32_t section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
3108 if ((section_type
== MachO::S_NON_LAZY_SYMBOL_POINTERS
||
3109 section_type
== MachO::S_LAZY_SYMBOL_POINTERS
||
3110 section_type
== MachO::S_LAZY_DYLIB_SYMBOL_POINTERS
||
3111 section_type
== MachO::S_THREAD_LOCAL_VARIABLE_POINTERS
||
3112 section_type
== MachO::S_SYMBOL_STUBS
) &&
3113 ReferenceValue
>= Sec
.addr
&&
3114 ReferenceValue
< Sec
.addr
+ Sec
.size
) {
3116 if (section_type
== MachO::S_SYMBOL_STUBS
)
3117 stride
= Sec
.reserved2
;
3122 uint32_t index
= Sec
.reserved1
+ (ReferenceValue
- Sec
.addr
) / stride
;
3123 if (index
< Dysymtab
.nindirectsyms
) {
3124 uint32_t indirect_symbol
=
3125 info
->O
->getIndirectSymbolTableEntry(Dysymtab
, index
);
3126 if (indirect_symbol
< Symtab
.nsyms
) {
3127 symbol_iterator Sym
= info
->O
->getSymbolByIndex(indirect_symbol
);
3128 return unwrapOrError(Sym
->getName(), info
->O
->getFileName())
3139 // method_reference() is called passing it the ReferenceName that might be
3140 // a reference it to an Objective-C method call. If so then it allocates and
3141 // assembles a method call string with the values last seen and saved in
3142 // the DisassembleInfo's class_name and selector_name fields. This is saved
3143 // into the method field of the info and any previous string is free'ed.
3144 // Then the class_name field in the info is set to nullptr. The method call
3145 // string is set into ReferenceName and ReferenceType is set to
3146 // LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
3147 // then both ReferenceType and ReferenceName are left unchanged.
3148 static void method_reference(struct DisassembleInfo
*info
,
3149 uint64_t *ReferenceType
,
3150 const char **ReferenceName
) {
3151 unsigned int Arch
= info
->O
->getArch();
3152 if (*ReferenceName
!= nullptr) {
3153 if (strcmp(*ReferenceName
, "_objc_msgSend") == 0) {
3154 if (info
->selector_name
!= nullptr) {
3155 if (info
->class_name
!= nullptr) {
3156 info
->method
= std::make_unique
<char[]>(
3157 5 + strlen(info
->class_name
) + strlen(info
->selector_name
));
3158 char *method
= info
->method
.get();
3159 if (method
!= nullptr) {
3160 strcpy(method
, "+[");
3161 strcat(method
, info
->class_name
);
3162 strcat(method
, " ");
3163 strcat(method
, info
->selector_name
);
3164 strcat(method
, "]");
3165 *ReferenceName
= method
;
3166 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_Objc_Message
;
3170 std::make_unique
<char[]>(9 + strlen(info
->selector_name
));
3171 char *method
= info
->method
.get();
3172 if (method
!= nullptr) {
3173 if (Arch
== Triple::x86_64
)
3174 strcpy(method
, "-[%rdi ");
3175 else if (Arch
== Triple::aarch64
)
3176 strcpy(method
, "-[x0 ");
3178 strcpy(method
, "-[r? ");
3179 strcat(method
, info
->selector_name
);
3180 strcat(method
, "]");
3181 *ReferenceName
= method
;
3182 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_Objc_Message
;
3185 info
->class_name
= nullptr;
3187 } else if (strcmp(*ReferenceName
, "_objc_msgSendSuper2") == 0) {
3188 if (info
->selector_name
!= nullptr) {
3190 std::make_unique
<char[]>(17 + strlen(info
->selector_name
));
3191 char *method
= info
->method
.get();
3192 if (method
!= nullptr) {
3193 if (Arch
== Triple::x86_64
)
3194 strcpy(method
, "-[[%rdi super] ");
3195 else if (Arch
== Triple::aarch64
)
3196 strcpy(method
, "-[[x0 super] ");
3198 strcpy(method
, "-[[r? super] ");
3199 strcat(method
, info
->selector_name
);
3200 strcat(method
, "]");
3201 *ReferenceName
= method
;
3202 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_Objc_Message
;
3204 info
->class_name
= nullptr;
3210 // GuessPointerPointer() is passed the address of what might be a pointer to
3211 // a reference to an Objective-C class, selector, message ref or cfstring.
3212 // If so the value of the pointer is returned and one of the booleans are set
3213 // to true. If not zero is returned and all the booleans are set to false.
3214 static uint64_t GuessPointerPointer(uint64_t ReferenceValue
,
3215 struct DisassembleInfo
*info
,
3216 bool &classref
, bool &selref
, bool &msgref
,
3222 for (const auto &Load
: info
->O
->load_commands()) {
3223 if (Load
.C
.cmd
== MachO::LC_SEGMENT_64
) {
3224 MachO::segment_command_64 Seg
= info
->O
->getSegment64LoadCommand(Load
);
3225 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
3226 MachO::section_64 Sec
= info
->O
->getSection64(Load
, J
);
3227 if ((strncmp(Sec
.sectname
, "__objc_selrefs", 16) == 0 ||
3228 strncmp(Sec
.sectname
, "__objc_classrefs", 16) == 0 ||
3229 strncmp(Sec
.sectname
, "__objc_superrefs", 16) == 0 ||
3230 strncmp(Sec
.sectname
, "__objc_msgrefs", 16) == 0 ||
3231 strncmp(Sec
.sectname
, "__cfstring", 16) == 0) &&
3232 ReferenceValue
>= Sec
.addr
&&
3233 ReferenceValue
< Sec
.addr
+ Sec
.size
) {
3234 uint64_t sect_offset
= ReferenceValue
- Sec
.addr
;
3235 uint64_t object_offset
= Sec
.offset
+ sect_offset
;
3236 StringRef MachOContents
= info
->O
->getData();
3237 uint64_t object_size
= MachOContents
.size();
3238 const char *object_addr
= (const char *)MachOContents
.data();
3239 if (object_offset
< object_size
) {
3240 uint64_t pointer_value
;
3241 memcpy(&pointer_value
, object_addr
+ object_offset
,
3243 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
3244 sys::swapByteOrder(pointer_value
);
3245 if (strncmp(Sec
.sectname
, "__objc_selrefs", 16) == 0)
3247 else if (strncmp(Sec
.sectname
, "__objc_classrefs", 16) == 0 ||
3248 strncmp(Sec
.sectname
, "__objc_superrefs", 16) == 0)
3250 else if (strncmp(Sec
.sectname
, "__objc_msgrefs", 16) == 0 &&
3251 ReferenceValue
+ 8 < Sec
.addr
+ Sec
.size
) {
3253 memcpy(&pointer_value
, object_addr
+ object_offset
+ 8,
3255 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
3256 sys::swapByteOrder(pointer_value
);
3257 } else if (strncmp(Sec
.sectname
, "__cfstring", 16) == 0)
3259 return pointer_value
;
3266 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
3271 // get_pointer_64 returns a pointer to the bytes in the object file at the
3272 // Address from a section in the Mach-O file. And indirectly returns the
3273 // offset into the section, number of bytes left in the section past the offset
3274 // and which section is was being referenced. If the Address is not in a
3275 // section nullptr is returned.
3276 static const char *get_pointer_64(uint64_t Address
, uint32_t &offset
,
3277 uint32_t &left
, SectionRef
&S
,
3278 DisassembleInfo
*info
,
3279 bool objc_only
= false) {
3283 for (unsigned SectIdx
= 0; SectIdx
!= info
->Sections
->size(); SectIdx
++) {
3284 uint64_t SectAddress
= ((*(info
->Sections
))[SectIdx
]).getAddress();
3285 uint64_t SectSize
= ((*(info
->Sections
))[SectIdx
]).getSize();
3290 Expected
<StringRef
> SecNameOrErr
=
3291 ((*(info
->Sections
))[SectIdx
]).getName();
3293 SectName
= *SecNameOrErr
;
3295 consumeError(SecNameOrErr
.takeError());
3297 DataRefImpl Ref
= ((*(info
->Sections
))[SectIdx
]).getRawDataRefImpl();
3298 StringRef SegName
= info
->O
->getSectionFinalSegmentName(Ref
);
3299 if (SegName
!= "__OBJC" && SectName
!= "__cstring")
3302 if (Address
>= SectAddress
&& Address
< SectAddress
+ SectSize
) {
3303 S
= (*(info
->Sections
))[SectIdx
];
3304 offset
= Address
- SectAddress
;
3305 left
= SectSize
- offset
;
3306 StringRef SectContents
= unwrapOrError(
3307 ((*(info
->Sections
))[SectIdx
]).getContents(), info
->O
->getFileName());
3308 return SectContents
.data() + offset
;
3314 static const char *get_pointer_32(uint32_t Address
, uint32_t &offset
,
3315 uint32_t &left
, SectionRef
&S
,
3316 DisassembleInfo
*info
,
3317 bool objc_only
= false) {
3318 return get_pointer_64(Address
, offset
, left
, S
, info
, objc_only
);
3321 // get_symbol_64() returns the name of a symbol (or nullptr) and the address of
3322 // the symbol indirectly through n_value. Based on the relocation information
3323 // for the specified section offset in the specified section reference.
3324 // If no relocation information is found and a non-zero ReferenceValue for the
3325 // symbol is passed, look up that address in the info's AddrMap.
3326 static const char *get_symbol_64(uint32_t sect_offset
, SectionRef S
,
3327 DisassembleInfo
*info
, uint64_t &n_value
,
3328 uint64_t ReferenceValue
= 0) {
3333 // See if there is an external relocation entry at the sect_offset.
3334 bool reloc_found
= false;
3336 MachO::any_relocation_info RE
;
3337 bool isExtern
= false;
3339 for (const RelocationRef
&Reloc
: S
.relocations()) {
3340 uint64_t RelocOffset
= Reloc
.getOffset();
3341 if (RelocOffset
== sect_offset
) {
3342 Rel
= Reloc
.getRawDataRefImpl();
3343 RE
= info
->O
->getRelocation(Rel
);
3344 if (info
->O
->isRelocationScattered(RE
))
3346 isExtern
= info
->O
->getPlainRelocationExternal(RE
);
3348 symbol_iterator RelocSym
= Reloc
.getSymbol();
3355 // If there is an external relocation entry for a symbol in this section
3356 // at this section_offset then use that symbol's value for the n_value
3357 // and return its name.
3358 const char *SymbolName
= nullptr;
3359 if (reloc_found
&& isExtern
) {
3360 n_value
= Symbol
.getValue();
3361 StringRef Name
= unwrapOrError(Symbol
.getName(), info
->O
->getFileName());
3362 if (!Name
.empty()) {
3363 SymbolName
= Name
.data();
3368 // TODO: For fully linked images, look through the external relocation
3369 // entries off the dynamic symtab command. For these the r_offset is from the
3370 // start of the first writeable segment in the Mach-O file. So the offset
3371 // to this section from that segment is passed to this routine by the caller,
3372 // as the database_offset. Which is the difference of the section's starting
3373 // address and the first writable segment.
3375 // NOTE: need add passing the database_offset to this routine.
3377 // We did not find an external relocation entry so look up the ReferenceValue
3378 // as an address of a symbol and if found return that symbol's name.
3379 SymbolName
= GuessSymbolName(ReferenceValue
, info
->AddrMap
);
3384 static const char *get_symbol_32(uint32_t sect_offset
, SectionRef S
,
3385 DisassembleInfo
*info
,
3386 uint32_t ReferenceValue
) {
3388 return get_symbol_64(sect_offset
, S
, info
, n_value64
, ReferenceValue
);
3391 // These are structs in the Objective-C meta data and read to produce the
3392 // comments for disassembly. While these are part of the ABI they are no
3393 // public defintions. So the are here not in include/llvm/BinaryFormat/MachO.h
3396 // The cfstring object in a 64-bit Mach-O file.
3397 struct cfstring64_t
{
3398 uint64_t isa
; // class64_t * (64-bit pointer)
3399 uint64_t flags
; // flag bits
3400 uint64_t characters
; // char * (64-bit pointer)
3401 uint64_t length
; // number of non-NULL characters in above
3404 // The class object in a 64-bit Mach-O file.
3406 uint64_t isa
; // class64_t * (64-bit pointer)
3407 uint64_t superclass
; // class64_t * (64-bit pointer)
3408 uint64_t cache
; // Cache (64-bit pointer)
3409 uint64_t vtable
; // IMP * (64-bit pointer)
3410 uint64_t data
; // class_ro64_t * (64-bit pointer)
3414 uint32_t isa
; /* class32_t * (32-bit pointer) */
3415 uint32_t superclass
; /* class32_t * (32-bit pointer) */
3416 uint32_t cache
; /* Cache (32-bit pointer) */
3417 uint32_t vtable
; /* IMP * (32-bit pointer) */
3418 uint32_t data
; /* class_ro32_t * (32-bit pointer) */
3421 struct class_ro64_t
{
3423 uint32_t instanceStart
;
3424 uint32_t instanceSize
;
3426 uint64_t ivarLayout
; // const uint8_t * (64-bit pointer)
3427 uint64_t name
; // const char * (64-bit pointer)
3428 uint64_t baseMethods
; // const method_list_t * (64-bit pointer)
3429 uint64_t baseProtocols
; // const protocol_list_t * (64-bit pointer)
3430 uint64_t ivars
; // const ivar_list_t * (64-bit pointer)
3431 uint64_t weakIvarLayout
; // const uint8_t * (64-bit pointer)
3432 uint64_t baseProperties
; // const struct objc_property_list (64-bit pointer)
3435 struct class_ro32_t
{
3437 uint32_t instanceStart
;
3438 uint32_t instanceSize
;
3439 uint32_t ivarLayout
; /* const uint8_t * (32-bit pointer) */
3440 uint32_t name
; /* const char * (32-bit pointer) */
3441 uint32_t baseMethods
; /* const method_list_t * (32-bit pointer) */
3442 uint32_t baseProtocols
; /* const protocol_list_t * (32-bit pointer) */
3443 uint32_t ivars
; /* const ivar_list_t * (32-bit pointer) */
3444 uint32_t weakIvarLayout
; /* const uint8_t * (32-bit pointer) */
3445 uint32_t baseProperties
; /* const struct objc_property_list *
3449 /* Values for class_ro{64,32}_t->flags */
3450 #define RO_META (1 << 0)
3451 #define RO_ROOT (1 << 1)
3452 #define RO_HAS_CXX_STRUCTORS (1 << 2)
3454 struct method_list64_t
{
3457 /* struct method64_t first; These structures follow inline */
3460 struct method_list32_t
{
3463 /* struct method32_t first; These structures follow inline */
3467 uint64_t name
; /* SEL (64-bit pointer) */
3468 uint64_t types
; /* const char * (64-bit pointer) */
3469 uint64_t imp
; /* IMP (64-bit pointer) */
3473 uint32_t name
; /* SEL (32-bit pointer) */
3474 uint32_t types
; /* const char * (32-bit pointer) */
3475 uint32_t imp
; /* IMP (32-bit pointer) */
3478 struct protocol_list64_t
{
3479 uint64_t count
; /* uintptr_t (a 64-bit value) */
3480 /* struct protocol64_t * list[0]; These pointers follow inline */
3483 struct protocol_list32_t
{
3484 uint32_t count
; /* uintptr_t (a 32-bit value) */
3485 /* struct protocol32_t * list[0]; These pointers follow inline */
3488 struct protocol64_t
{
3489 uint64_t isa
; /* id * (64-bit pointer) */
3490 uint64_t name
; /* const char * (64-bit pointer) */
3491 uint64_t protocols
; /* struct protocol_list64_t *
3493 uint64_t instanceMethods
; /* method_list_t * (64-bit pointer) */
3494 uint64_t classMethods
; /* method_list_t * (64-bit pointer) */
3495 uint64_t optionalInstanceMethods
; /* method_list_t * (64-bit pointer) */
3496 uint64_t optionalClassMethods
; /* method_list_t * (64-bit pointer) */
3497 uint64_t instanceProperties
; /* struct objc_property_list *
3501 struct protocol32_t
{
3502 uint32_t isa
; /* id * (32-bit pointer) */
3503 uint32_t name
; /* const char * (32-bit pointer) */
3504 uint32_t protocols
; /* struct protocol_list_t *
3506 uint32_t instanceMethods
; /* method_list_t * (32-bit pointer) */
3507 uint32_t classMethods
; /* method_list_t * (32-bit pointer) */
3508 uint32_t optionalInstanceMethods
; /* method_list_t * (32-bit pointer) */
3509 uint32_t optionalClassMethods
; /* method_list_t * (32-bit pointer) */
3510 uint32_t instanceProperties
; /* struct objc_property_list *
3514 struct ivar_list64_t
{
3517 /* struct ivar64_t first; These structures follow inline */
3520 struct ivar_list32_t
{
3523 /* struct ivar32_t first; These structures follow inline */
3527 uint64_t offset
; /* uintptr_t * (64-bit pointer) */
3528 uint64_t name
; /* const char * (64-bit pointer) */
3529 uint64_t type
; /* const char * (64-bit pointer) */
3535 uint32_t offset
; /* uintptr_t * (32-bit pointer) */
3536 uint32_t name
; /* const char * (32-bit pointer) */
3537 uint32_t type
; /* const char * (32-bit pointer) */
3542 struct objc_property_list64
{
3545 /* struct objc_property64 first; These structures follow inline */
3548 struct objc_property_list32
{
3551 /* struct objc_property32 first; These structures follow inline */
3554 struct objc_property64
{
3555 uint64_t name
; /* const char * (64-bit pointer) */
3556 uint64_t attributes
; /* const char * (64-bit pointer) */
3559 struct objc_property32
{
3560 uint32_t name
; /* const char * (32-bit pointer) */
3561 uint32_t attributes
; /* const char * (32-bit pointer) */
3564 struct category64_t
{
3565 uint64_t name
; /* const char * (64-bit pointer) */
3566 uint64_t cls
; /* struct class_t * (64-bit pointer) */
3567 uint64_t instanceMethods
; /* struct method_list_t * (64-bit pointer) */
3568 uint64_t classMethods
; /* struct method_list_t * (64-bit pointer) */
3569 uint64_t protocols
; /* struct protocol_list_t * (64-bit pointer) */
3570 uint64_t instanceProperties
; /* struct objc_property_list *
3574 struct category32_t
{
3575 uint32_t name
; /* const char * (32-bit pointer) */
3576 uint32_t cls
; /* struct class_t * (32-bit pointer) */
3577 uint32_t instanceMethods
; /* struct method_list_t * (32-bit pointer) */
3578 uint32_t classMethods
; /* struct method_list_t * (32-bit pointer) */
3579 uint32_t protocols
; /* struct protocol_list_t * (32-bit pointer) */
3580 uint32_t instanceProperties
; /* struct objc_property_list *
3584 struct objc_image_info64
{
3588 struct objc_image_info32
{
3592 struct imageInfo_t
{
3596 /* masks for objc_image_info.flags */
3597 #define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
3598 #define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
3599 #define OBJC_IMAGE_IS_SIMULATED (1 << 5)
3600 #define OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES (1 << 6)
3602 struct message_ref64
{
3603 uint64_t imp
; /* IMP (64-bit pointer) */
3604 uint64_t sel
; /* SEL (64-bit pointer) */
3607 struct message_ref32
{
3608 uint32_t imp
; /* IMP (32-bit pointer) */
3609 uint32_t sel
; /* SEL (32-bit pointer) */
3612 // Objective-C 1 (32-bit only) meta data structs.
3614 struct objc_module_t
{
3617 uint32_t name
; /* char * (32-bit pointer) */
3618 uint32_t symtab
; /* struct objc_symtab * (32-bit pointer) */
3621 struct objc_symtab_t
{
3622 uint32_t sel_ref_cnt
;
3623 uint32_t refs
; /* SEL * (32-bit pointer) */
3624 uint16_t cls_def_cnt
;
3625 uint16_t cat_def_cnt
;
3626 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
3629 struct objc_class_t
{
3630 uint32_t isa
; /* struct objc_class * (32-bit pointer) */
3631 uint32_t super_class
; /* struct objc_class * (32-bit pointer) */
3632 uint32_t name
; /* const char * (32-bit pointer) */
3635 int32_t instance_size
;
3636 uint32_t ivars
; /* struct objc_ivar_list * (32-bit pointer) */
3637 uint32_t methodLists
; /* struct objc_method_list ** (32-bit pointer) */
3638 uint32_t cache
; /* struct objc_cache * (32-bit pointer) */
3639 uint32_t protocols
; /* struct objc_protocol_list * (32-bit pointer) */
3642 #define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
3643 // class is not a metaclass
3644 #define CLS_CLASS 0x1
3645 // class is a metaclass
3646 #define CLS_META 0x2
3648 struct objc_category_t
{
3649 uint32_t category_name
; /* char * (32-bit pointer) */
3650 uint32_t class_name
; /* char * (32-bit pointer) */
3651 uint32_t instance_methods
; /* struct objc_method_list * (32-bit pointer) */
3652 uint32_t class_methods
; /* struct objc_method_list * (32-bit pointer) */
3653 uint32_t protocols
; /* struct objc_protocol_list * (32-bit ptr) */
3656 struct objc_ivar_t
{
3657 uint32_t ivar_name
; /* char * (32-bit pointer) */
3658 uint32_t ivar_type
; /* char * (32-bit pointer) */
3659 int32_t ivar_offset
;
3662 struct objc_ivar_list_t
{
3664 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
3667 struct objc_method_list_t
{
3668 uint32_t obsolete
; /* struct objc_method_list * (32-bit pointer) */
3669 int32_t method_count
;
3670 // struct objc_method_t method_list[1]; /* variable length structure */
3673 struct objc_method_t
{
3674 uint32_t method_name
; /* SEL, aka struct objc_selector * (32-bit pointer) */
3675 uint32_t method_types
; /* char * (32-bit pointer) */
3676 uint32_t method_imp
; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
3680 struct objc_protocol_list_t
{
3681 uint32_t next
; /* struct objc_protocol_list * (32-bit pointer) */
3683 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
3684 // (32-bit pointer) */
3687 struct objc_protocol_t
{
3688 uint32_t isa
; /* struct objc_class * (32-bit pointer) */
3689 uint32_t protocol_name
; /* char * (32-bit pointer) */
3690 uint32_t protocol_list
; /* struct objc_protocol_list * (32-bit pointer) */
3691 uint32_t instance_methods
; /* struct objc_method_description_list *
3693 uint32_t class_methods
; /* struct objc_method_description_list *
3697 struct objc_method_description_list_t
{
3699 // struct objc_method_description_t list[1];
3702 struct objc_method_description_t
{
3703 uint32_t name
; /* SEL, aka struct objc_selector * (32-bit pointer) */
3704 uint32_t types
; /* char * (32-bit pointer) */
3707 inline void swapStruct(struct cfstring64_t
&cfs
) {
3708 sys::swapByteOrder(cfs
.isa
);
3709 sys::swapByteOrder(cfs
.flags
);
3710 sys::swapByteOrder(cfs
.characters
);
3711 sys::swapByteOrder(cfs
.length
);
3714 inline void swapStruct(struct class64_t
&c
) {
3715 sys::swapByteOrder(c
.isa
);
3716 sys::swapByteOrder(c
.superclass
);
3717 sys::swapByteOrder(c
.cache
);
3718 sys::swapByteOrder(c
.vtable
);
3719 sys::swapByteOrder(c
.data
);
3722 inline void swapStruct(struct class32_t
&c
) {
3723 sys::swapByteOrder(c
.isa
);
3724 sys::swapByteOrder(c
.superclass
);
3725 sys::swapByteOrder(c
.cache
);
3726 sys::swapByteOrder(c
.vtable
);
3727 sys::swapByteOrder(c
.data
);
3730 inline void swapStruct(struct class_ro64_t
&cro
) {
3731 sys::swapByteOrder(cro
.flags
);
3732 sys::swapByteOrder(cro
.instanceStart
);
3733 sys::swapByteOrder(cro
.instanceSize
);
3734 sys::swapByteOrder(cro
.reserved
);
3735 sys::swapByteOrder(cro
.ivarLayout
);
3736 sys::swapByteOrder(cro
.name
);
3737 sys::swapByteOrder(cro
.baseMethods
);
3738 sys::swapByteOrder(cro
.baseProtocols
);
3739 sys::swapByteOrder(cro
.ivars
);
3740 sys::swapByteOrder(cro
.weakIvarLayout
);
3741 sys::swapByteOrder(cro
.baseProperties
);
3744 inline void swapStruct(struct class_ro32_t
&cro
) {
3745 sys::swapByteOrder(cro
.flags
);
3746 sys::swapByteOrder(cro
.instanceStart
);
3747 sys::swapByteOrder(cro
.instanceSize
);
3748 sys::swapByteOrder(cro
.ivarLayout
);
3749 sys::swapByteOrder(cro
.name
);
3750 sys::swapByteOrder(cro
.baseMethods
);
3751 sys::swapByteOrder(cro
.baseProtocols
);
3752 sys::swapByteOrder(cro
.ivars
);
3753 sys::swapByteOrder(cro
.weakIvarLayout
);
3754 sys::swapByteOrder(cro
.baseProperties
);
3757 inline void swapStruct(struct method_list64_t
&ml
) {
3758 sys::swapByteOrder(ml
.entsize
);
3759 sys::swapByteOrder(ml
.count
);
3762 inline void swapStruct(struct method_list32_t
&ml
) {
3763 sys::swapByteOrder(ml
.entsize
);
3764 sys::swapByteOrder(ml
.count
);
3767 inline void swapStruct(struct method64_t
&m
) {
3768 sys::swapByteOrder(m
.name
);
3769 sys::swapByteOrder(m
.types
);
3770 sys::swapByteOrder(m
.imp
);
3773 inline void swapStruct(struct method32_t
&m
) {
3774 sys::swapByteOrder(m
.name
);
3775 sys::swapByteOrder(m
.types
);
3776 sys::swapByteOrder(m
.imp
);
3779 inline void swapStruct(struct protocol_list64_t
&pl
) {
3780 sys::swapByteOrder(pl
.count
);
3783 inline void swapStruct(struct protocol_list32_t
&pl
) {
3784 sys::swapByteOrder(pl
.count
);
3787 inline void swapStruct(struct protocol64_t
&p
) {
3788 sys::swapByteOrder(p
.isa
);
3789 sys::swapByteOrder(p
.name
);
3790 sys::swapByteOrder(p
.protocols
);
3791 sys::swapByteOrder(p
.instanceMethods
);
3792 sys::swapByteOrder(p
.classMethods
);
3793 sys::swapByteOrder(p
.optionalInstanceMethods
);
3794 sys::swapByteOrder(p
.optionalClassMethods
);
3795 sys::swapByteOrder(p
.instanceProperties
);
3798 inline void swapStruct(struct protocol32_t
&p
) {
3799 sys::swapByteOrder(p
.isa
);
3800 sys::swapByteOrder(p
.name
);
3801 sys::swapByteOrder(p
.protocols
);
3802 sys::swapByteOrder(p
.instanceMethods
);
3803 sys::swapByteOrder(p
.classMethods
);
3804 sys::swapByteOrder(p
.optionalInstanceMethods
);
3805 sys::swapByteOrder(p
.optionalClassMethods
);
3806 sys::swapByteOrder(p
.instanceProperties
);
3809 inline void swapStruct(struct ivar_list64_t
&il
) {
3810 sys::swapByteOrder(il
.entsize
);
3811 sys::swapByteOrder(il
.count
);
3814 inline void swapStruct(struct ivar_list32_t
&il
) {
3815 sys::swapByteOrder(il
.entsize
);
3816 sys::swapByteOrder(il
.count
);
3819 inline void swapStruct(struct ivar64_t
&i
) {
3820 sys::swapByteOrder(i
.offset
);
3821 sys::swapByteOrder(i
.name
);
3822 sys::swapByteOrder(i
.type
);
3823 sys::swapByteOrder(i
.alignment
);
3824 sys::swapByteOrder(i
.size
);
3827 inline void swapStruct(struct ivar32_t
&i
) {
3828 sys::swapByteOrder(i
.offset
);
3829 sys::swapByteOrder(i
.name
);
3830 sys::swapByteOrder(i
.type
);
3831 sys::swapByteOrder(i
.alignment
);
3832 sys::swapByteOrder(i
.size
);
3835 inline void swapStruct(struct objc_property_list64
&pl
) {
3836 sys::swapByteOrder(pl
.entsize
);
3837 sys::swapByteOrder(pl
.count
);
3840 inline void swapStruct(struct objc_property_list32
&pl
) {
3841 sys::swapByteOrder(pl
.entsize
);
3842 sys::swapByteOrder(pl
.count
);
3845 inline void swapStruct(struct objc_property64
&op
) {
3846 sys::swapByteOrder(op
.name
);
3847 sys::swapByteOrder(op
.attributes
);
3850 inline void swapStruct(struct objc_property32
&op
) {
3851 sys::swapByteOrder(op
.name
);
3852 sys::swapByteOrder(op
.attributes
);
3855 inline void swapStruct(struct category64_t
&c
) {
3856 sys::swapByteOrder(c
.name
);
3857 sys::swapByteOrder(c
.cls
);
3858 sys::swapByteOrder(c
.instanceMethods
);
3859 sys::swapByteOrder(c
.classMethods
);
3860 sys::swapByteOrder(c
.protocols
);
3861 sys::swapByteOrder(c
.instanceProperties
);
3864 inline void swapStruct(struct category32_t
&c
) {
3865 sys::swapByteOrder(c
.name
);
3866 sys::swapByteOrder(c
.cls
);
3867 sys::swapByteOrder(c
.instanceMethods
);
3868 sys::swapByteOrder(c
.classMethods
);
3869 sys::swapByteOrder(c
.protocols
);
3870 sys::swapByteOrder(c
.instanceProperties
);
3873 inline void swapStruct(struct objc_image_info64
&o
) {
3874 sys::swapByteOrder(o
.version
);
3875 sys::swapByteOrder(o
.flags
);
3878 inline void swapStruct(struct objc_image_info32
&o
) {
3879 sys::swapByteOrder(o
.version
);
3880 sys::swapByteOrder(o
.flags
);
3883 inline void swapStruct(struct imageInfo_t
&o
) {
3884 sys::swapByteOrder(o
.version
);
3885 sys::swapByteOrder(o
.flags
);
3888 inline void swapStruct(struct message_ref64
&mr
) {
3889 sys::swapByteOrder(mr
.imp
);
3890 sys::swapByteOrder(mr
.sel
);
3893 inline void swapStruct(struct message_ref32
&mr
) {
3894 sys::swapByteOrder(mr
.imp
);
3895 sys::swapByteOrder(mr
.sel
);
3898 inline void swapStruct(struct objc_module_t
&module
) {
3899 sys::swapByteOrder(module
.version
);
3900 sys::swapByteOrder(module
.size
);
3901 sys::swapByteOrder(module
.name
);
3902 sys::swapByteOrder(module
.symtab
);
3905 inline void swapStruct(struct objc_symtab_t
&symtab
) {
3906 sys::swapByteOrder(symtab
.sel_ref_cnt
);
3907 sys::swapByteOrder(symtab
.refs
);
3908 sys::swapByteOrder(symtab
.cls_def_cnt
);
3909 sys::swapByteOrder(symtab
.cat_def_cnt
);
3912 inline void swapStruct(struct objc_class_t
&objc_class
) {
3913 sys::swapByteOrder(objc_class
.isa
);
3914 sys::swapByteOrder(objc_class
.super_class
);
3915 sys::swapByteOrder(objc_class
.name
);
3916 sys::swapByteOrder(objc_class
.version
);
3917 sys::swapByteOrder(objc_class
.info
);
3918 sys::swapByteOrder(objc_class
.instance_size
);
3919 sys::swapByteOrder(objc_class
.ivars
);
3920 sys::swapByteOrder(objc_class
.methodLists
);
3921 sys::swapByteOrder(objc_class
.cache
);
3922 sys::swapByteOrder(objc_class
.protocols
);
3925 inline void swapStruct(struct objc_category_t
&objc_category
) {
3926 sys::swapByteOrder(objc_category
.category_name
);
3927 sys::swapByteOrder(objc_category
.class_name
);
3928 sys::swapByteOrder(objc_category
.instance_methods
);
3929 sys::swapByteOrder(objc_category
.class_methods
);
3930 sys::swapByteOrder(objc_category
.protocols
);
3933 inline void swapStruct(struct objc_ivar_list_t
&objc_ivar_list
) {
3934 sys::swapByteOrder(objc_ivar_list
.ivar_count
);
3937 inline void swapStruct(struct objc_ivar_t
&objc_ivar
) {
3938 sys::swapByteOrder(objc_ivar
.ivar_name
);
3939 sys::swapByteOrder(objc_ivar
.ivar_type
);
3940 sys::swapByteOrder(objc_ivar
.ivar_offset
);
3943 inline void swapStruct(struct objc_method_list_t
&method_list
) {
3944 sys::swapByteOrder(method_list
.obsolete
);
3945 sys::swapByteOrder(method_list
.method_count
);
3948 inline void swapStruct(struct objc_method_t
&method
) {
3949 sys::swapByteOrder(method
.method_name
);
3950 sys::swapByteOrder(method
.method_types
);
3951 sys::swapByteOrder(method
.method_imp
);
3954 inline void swapStruct(struct objc_protocol_list_t
&protocol_list
) {
3955 sys::swapByteOrder(protocol_list
.next
);
3956 sys::swapByteOrder(protocol_list
.count
);
3959 inline void swapStruct(struct objc_protocol_t
&protocol
) {
3960 sys::swapByteOrder(protocol
.isa
);
3961 sys::swapByteOrder(protocol
.protocol_name
);
3962 sys::swapByteOrder(protocol
.protocol_list
);
3963 sys::swapByteOrder(protocol
.instance_methods
);
3964 sys::swapByteOrder(protocol
.class_methods
);
3967 inline void swapStruct(struct objc_method_description_list_t
&mdl
) {
3968 sys::swapByteOrder(mdl
.count
);
3971 inline void swapStruct(struct objc_method_description_t
&md
) {
3972 sys::swapByteOrder(md
.name
);
3973 sys::swapByteOrder(md
.types
);
3976 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue
,
3977 struct DisassembleInfo
*info
);
3979 // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3980 // to an Objective-C class and returns the class name. It is also passed the
3981 // address of the pointer, so when the pointer is zero as it can be in an .o
3982 // file, that is used to look for an external relocation entry with a symbol
3984 static const char *get_objc2_64bit_class_name(uint64_t pointer_value
,
3985 uint64_t ReferenceValue
,
3986 struct DisassembleInfo
*info
) {
3988 uint32_t offset
, left
;
3991 // The pointer_value can be 0 in an object file and have a relocation
3992 // entry for the class symbol at the ReferenceValue (the address of the
3994 if (pointer_value
== 0) {
3995 r
= get_pointer_64(ReferenceValue
, offset
, left
, S
, info
);
3996 if (r
== nullptr || left
< sizeof(uint64_t))
3999 const char *symbol_name
= get_symbol_64(offset
, S
, info
, n_value
);
4000 if (symbol_name
== nullptr)
4002 const char *class_name
= strrchr(symbol_name
, '$');
4003 if (class_name
!= nullptr && class_name
[1] == '_' && class_name
[2] != '\0')
4004 return class_name
+ 2;
4009 // The case were the pointer_value is non-zero and points to a class defined
4010 // in this Mach-O file.
4011 r
= get_pointer_64(pointer_value
, offset
, left
, S
, info
);
4012 if (r
== nullptr || left
< sizeof(struct class64_t
))
4015 memcpy(&c
, r
, sizeof(struct class64_t
));
4016 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4020 r
= get_pointer_64(c
.data
, offset
, left
, S
, info
);
4021 if (r
== nullptr || left
< sizeof(struct class_ro64_t
))
4023 struct class_ro64_t cro
;
4024 memcpy(&cro
, r
, sizeof(struct class_ro64_t
));
4025 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4029 const char *name
= get_pointer_64(cro
.name
, offset
, left
, S
, info
);
4033 // get_objc2_64bit_cfstring_name is used for disassembly and is passed a
4034 // pointer to a cfstring and returns its name or nullptr.
4035 static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue
,
4036 struct DisassembleInfo
*info
) {
4037 const char *r
, *name
;
4038 uint32_t offset
, left
;
4040 struct cfstring64_t cfs
;
4041 uint64_t cfs_characters
;
4043 r
= get_pointer_64(ReferenceValue
, offset
, left
, S
, info
);
4044 if (r
== nullptr || left
< sizeof(struct cfstring64_t
))
4046 memcpy(&cfs
, r
, sizeof(struct cfstring64_t
));
4047 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4049 if (cfs
.characters
== 0) {
4051 const char *symbol_name
= get_symbol_64(
4052 offset
+ offsetof(struct cfstring64_t
, characters
), S
, info
, n_value
);
4053 if (symbol_name
== nullptr)
4055 cfs_characters
= n_value
;
4057 cfs_characters
= cfs
.characters
;
4058 name
= get_pointer_64(cfs_characters
, offset
, left
, S
, info
);
4063 // get_objc2_64bit_selref() is used for disassembly and is passed a the address
4064 // of a pointer to an Objective-C selector reference when the pointer value is
4065 // zero as in a .o file and is likely to have a external relocation entry with
4066 // who's symbol's n_value is the real pointer to the selector name. If that is
4067 // the case the real pointer to the selector name is returned else 0 is
4069 static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue
,
4070 struct DisassembleInfo
*info
) {
4071 uint32_t offset
, left
;
4074 const char *r
= get_pointer_64(ReferenceValue
, offset
, left
, S
, info
);
4075 if (r
== nullptr || left
< sizeof(uint64_t))
4078 const char *symbol_name
= get_symbol_64(offset
, S
, info
, n_value
);
4079 if (symbol_name
== nullptr)
4084 static const SectionRef
get_section(MachOObjectFile
*O
, const char *segname
,
4085 const char *sectname
) {
4086 for (const SectionRef
&Section
: O
->sections()) {
4088 Expected
<StringRef
> SecNameOrErr
= Section
.getName();
4090 SectName
= *SecNameOrErr
;
4092 consumeError(SecNameOrErr
.takeError());
4094 DataRefImpl Ref
= Section
.getRawDataRefImpl();
4095 StringRef SegName
= O
->getSectionFinalSegmentName(Ref
);
4096 if (SegName
== segname
&& SectName
== sectname
)
4099 return SectionRef();
4103 walk_pointer_list_64(const char *listname
, const SectionRef S
,
4104 MachOObjectFile
*O
, struct DisassembleInfo
*info
,
4105 void (*func
)(uint64_t, struct DisassembleInfo
*info
)) {
4106 if (S
== SectionRef())
4110 Expected
<StringRef
> SecNameOrErr
= S
.getName();
4112 SectName
= *SecNameOrErr
;
4114 consumeError(SecNameOrErr
.takeError());
4116 DataRefImpl Ref
= S
.getRawDataRefImpl();
4117 StringRef SegName
= O
->getSectionFinalSegmentName(Ref
);
4118 outs() << "Contents of (" << SegName
<< "," << SectName
<< ") section\n";
4120 StringRef BytesStr
= unwrapOrError(S
.getContents(), O
->getFileName());
4121 const char *Contents
= reinterpret_cast<const char *>(BytesStr
.data());
4123 for (uint32_t i
= 0; i
< S
.getSize(); i
+= sizeof(uint64_t)) {
4124 uint32_t left
= S
.getSize() - i
;
4125 uint32_t size
= left
< sizeof(uint64_t) ? left
: sizeof(uint64_t);
4127 memcpy(&p
, Contents
+ i
, size
);
4128 if (i
+ sizeof(uint64_t) > S
.getSize())
4129 outs() << listname
<< " list pointer extends past end of (" << SegName
4130 << "," << SectName
<< ") section\n";
4131 outs() << format("%016" PRIx64
, S
.getAddress() + i
) << " ";
4133 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
4134 sys::swapByteOrder(p
);
4136 uint64_t n_value
= 0;
4137 const char *name
= get_symbol_64(i
, S
, info
, n_value
, p
);
4138 if (name
== nullptr)
4139 name
= get_dyld_bind_info_symbolname(S
.getAddress() + i
, info
);
4142 outs() << format("0x%" PRIx64
, n_value
);
4144 outs() << " + " << format("0x%" PRIx64
, p
);
4146 outs() << format("0x%" PRIx64
, p
);
4147 if (name
!= nullptr)
4148 outs() << " " << name
;
4158 walk_pointer_list_32(const char *listname
, const SectionRef S
,
4159 MachOObjectFile
*O
, struct DisassembleInfo
*info
,
4160 void (*func
)(uint32_t, struct DisassembleInfo
*info
)) {
4161 if (S
== SectionRef())
4164 StringRef SectName
= unwrapOrError(S
.getName(), O
->getFileName());
4165 DataRefImpl Ref
= S
.getRawDataRefImpl();
4166 StringRef SegName
= O
->getSectionFinalSegmentName(Ref
);
4167 outs() << "Contents of (" << SegName
<< "," << SectName
<< ") section\n";
4169 StringRef BytesStr
= unwrapOrError(S
.getContents(), O
->getFileName());
4170 const char *Contents
= reinterpret_cast<const char *>(BytesStr
.data());
4172 for (uint32_t i
= 0; i
< S
.getSize(); i
+= sizeof(uint32_t)) {
4173 uint32_t left
= S
.getSize() - i
;
4174 uint32_t size
= left
< sizeof(uint32_t) ? left
: sizeof(uint32_t);
4176 memcpy(&p
, Contents
+ i
, size
);
4177 if (i
+ sizeof(uint32_t) > S
.getSize())
4178 outs() << listname
<< " list pointer extends past end of (" << SegName
4179 << "," << SectName
<< ") section\n";
4180 uint32_t Address
= S
.getAddress() + i
;
4181 outs() << format("%08" PRIx32
, Address
) << " ";
4183 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
4184 sys::swapByteOrder(p
);
4185 outs() << format("0x%" PRIx32
, p
);
4187 const char *name
= get_symbol_32(i
, S
, info
, p
);
4188 if (name
!= nullptr)
4189 outs() << " " << name
;
4197 static void print_layout_map(const char *layout_map
, uint32_t left
) {
4198 if (layout_map
== nullptr)
4200 outs() << " layout map: ";
4202 outs() << format("0x%02" PRIx32
, (*layout_map
) & 0xff) << " ";
4205 } while (*layout_map
!= '\0' && left
!= 0);
4209 static void print_layout_map64(uint64_t p
, struct DisassembleInfo
*info
) {
4210 uint32_t offset
, left
;
4212 const char *layout_map
;
4216 layout_map
= get_pointer_64(p
, offset
, left
, S
, info
);
4217 print_layout_map(layout_map
, left
);
4220 static void print_layout_map32(uint32_t p
, struct DisassembleInfo
*info
) {
4221 uint32_t offset
, left
;
4223 const char *layout_map
;
4227 layout_map
= get_pointer_32(p
, offset
, left
, S
, info
);
4228 print_layout_map(layout_map
, left
);
4231 static void print_method_list64_t(uint64_t p
, struct DisassembleInfo
*info
,
4232 const char *indent
) {
4233 struct method_list64_t ml
;
4234 struct method64_t m
;
4236 uint32_t offset
, xoffset
, left
, i
;
4238 const char *name
, *sym_name
;
4241 r
= get_pointer_64(p
, offset
, left
, S
, info
);
4244 memset(&ml
, '\0', sizeof(struct method_list64_t
));
4245 if (left
< sizeof(struct method_list64_t
)) {
4246 memcpy(&ml
, r
, left
);
4247 outs() << " (method_list_t entends past the end of the section)\n";
4249 memcpy(&ml
, r
, sizeof(struct method_list64_t
));
4250 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4252 outs() << indent
<< "\t\t entsize " << ml
.entsize
<< "\n";
4253 outs() << indent
<< "\t\t count " << ml
.count
<< "\n";
4255 p
+= sizeof(struct method_list64_t
);
4256 offset
+= sizeof(struct method_list64_t
);
4257 for (i
= 0; i
< ml
.count
; i
++) {
4258 r
= get_pointer_64(p
, offset
, left
, S
, info
);
4261 memset(&m
, '\0', sizeof(struct method64_t
));
4262 if (left
< sizeof(struct method64_t
)) {
4263 memcpy(&m
, r
, left
);
4264 outs() << indent
<< " (method_t extends past the end of the section)\n";
4266 memcpy(&m
, r
, sizeof(struct method64_t
));
4267 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4270 outs() << indent
<< "\t\t name ";
4271 sym_name
= get_symbol_64(offset
+ offsetof(struct method64_t
, name
), S
,
4272 info
, n_value
, m
.name
);
4274 if (info
->verbose
&& sym_name
!= nullptr)
4277 outs() << format("0x%" PRIx64
, n_value
);
4279 outs() << " + " << format("0x%" PRIx64
, m
.name
);
4281 outs() << format("0x%" PRIx64
, m
.name
);
4282 name
= get_pointer_64(m
.name
+ n_value
, xoffset
, left
, xS
, info
);
4283 if (name
!= nullptr)
4284 outs() << format(" %.*s", left
, name
);
4287 outs() << indent
<< "\t\t types ";
4288 sym_name
= get_symbol_64(offset
+ offsetof(struct method64_t
, types
), S
,
4289 info
, n_value
, m
.types
);
4291 if (info
->verbose
&& sym_name
!= nullptr)
4294 outs() << format("0x%" PRIx64
, n_value
);
4296 outs() << " + " << format("0x%" PRIx64
, m
.types
);
4298 outs() << format("0x%" PRIx64
, m
.types
);
4299 name
= get_pointer_64(m
.types
+ n_value
, xoffset
, left
, xS
, info
);
4300 if (name
!= nullptr)
4301 outs() << format(" %.*s", left
, name
);
4304 outs() << indent
<< "\t\t imp ";
4305 name
= get_symbol_64(offset
+ offsetof(struct method64_t
, imp
), S
, info
,
4307 if (info
->verbose
&& name
== nullptr) {
4309 outs() << format("0x%" PRIx64
, n_value
) << " ";
4311 outs() << "+ " << format("0x%" PRIx64
, m
.imp
) << " ";
4313 outs() << format("0x%" PRIx64
, m
.imp
) << " ";
4315 if (name
!= nullptr)
4319 p
+= sizeof(struct method64_t
);
4320 offset
+= sizeof(struct method64_t
);
4324 static void print_method_list32_t(uint64_t p
, struct DisassembleInfo
*info
,
4325 const char *indent
) {
4326 struct method_list32_t ml
;
4327 struct method32_t m
;
4328 const char *r
, *name
;
4329 uint32_t offset
, xoffset
, left
, i
;
4332 r
= get_pointer_32(p
, offset
, left
, S
, info
);
4335 memset(&ml
, '\0', sizeof(struct method_list32_t
));
4336 if (left
< sizeof(struct method_list32_t
)) {
4337 memcpy(&ml
, r
, left
);
4338 outs() << " (method_list_t entends past the end of the section)\n";
4340 memcpy(&ml
, r
, sizeof(struct method_list32_t
));
4341 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4343 outs() << indent
<< "\t\t entsize " << ml
.entsize
<< "\n";
4344 outs() << indent
<< "\t\t count " << ml
.count
<< "\n";
4346 p
+= sizeof(struct method_list32_t
);
4347 offset
+= sizeof(struct method_list32_t
);
4348 for (i
= 0; i
< ml
.count
; i
++) {
4349 r
= get_pointer_32(p
, offset
, left
, S
, info
);
4352 memset(&m
, '\0', sizeof(struct method32_t
));
4353 if (left
< sizeof(struct method32_t
)) {
4354 memcpy(&ml
, r
, left
);
4355 outs() << indent
<< " (method_t entends past the end of the section)\n";
4357 memcpy(&m
, r
, sizeof(struct method32_t
));
4358 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4361 outs() << indent
<< "\t\t name " << format("0x%" PRIx32
, m
.name
);
4362 name
= get_pointer_32(m
.name
, xoffset
, left
, xS
, info
);
4363 if (name
!= nullptr)
4364 outs() << format(" %.*s", left
, name
);
4367 outs() << indent
<< "\t\t types " << format("0x%" PRIx32
, m
.types
);
4368 name
= get_pointer_32(m
.types
, xoffset
, left
, xS
, info
);
4369 if (name
!= nullptr)
4370 outs() << format(" %.*s", left
, name
);
4373 outs() << indent
<< "\t\t imp " << format("0x%" PRIx32
, m
.imp
);
4374 name
= get_symbol_32(offset
+ offsetof(struct method32_t
, imp
), S
, info
,
4376 if (name
!= nullptr)
4377 outs() << " " << name
;
4380 p
+= sizeof(struct method32_t
);
4381 offset
+= sizeof(struct method32_t
);
4385 static bool print_method_list(uint32_t p
, struct DisassembleInfo
*info
) {
4386 uint32_t offset
, left
, xleft
;
4388 struct objc_method_list_t method_list
;
4389 struct objc_method_t method
;
4390 const char *r
, *methods
, *name
, *SymbolName
;
4393 r
= get_pointer_32(p
, offset
, left
, S
, info
, true);
4398 if (left
> sizeof(struct objc_method_list_t
)) {
4399 memcpy(&method_list
, r
, sizeof(struct objc_method_list_t
));
4401 outs() << "\t\t objc_method_list extends past end of the section\n";
4402 memset(&method_list
, '\0', sizeof(struct objc_method_list_t
));
4403 memcpy(&method_list
, r
, left
);
4405 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4406 swapStruct(method_list
);
4408 outs() << "\t\t obsolete "
4409 << format("0x%08" PRIx32
, method_list
.obsolete
) << "\n";
4410 outs() << "\t\t method_count " << method_list
.method_count
<< "\n";
4412 methods
= r
+ sizeof(struct objc_method_list_t
);
4413 for (i
= 0; i
< method_list
.method_count
; i
++) {
4414 if ((i
+ 1) * sizeof(struct objc_method_t
) > left
) {
4415 outs() << "\t\t remaining method's extend past the of the section\n";
4418 memcpy(&method
, methods
+ i
* sizeof(struct objc_method_t
),
4419 sizeof(struct objc_method_t
));
4420 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4423 outs() << "\t\t method_name "
4424 << format("0x%08" PRIx32
, method
.method_name
);
4425 if (info
->verbose
) {
4426 name
= get_pointer_32(method
.method_name
, offset
, xleft
, S
, info
, true);
4427 if (name
!= nullptr)
4428 outs() << format(" %.*s", xleft
, name
);
4430 outs() << " (not in an __OBJC section)";
4434 outs() << "\t\t method_types "
4435 << format("0x%08" PRIx32
, method
.method_types
);
4436 if (info
->verbose
) {
4437 name
= get_pointer_32(method
.method_types
, offset
, xleft
, S
, info
, true);
4438 if (name
!= nullptr)
4439 outs() << format(" %.*s", xleft
, name
);
4441 outs() << " (not in an __OBJC section)";
4445 outs() << "\t\t method_imp "
4446 << format("0x%08" PRIx32
, method
.method_imp
) << " ";
4447 if (info
->verbose
) {
4448 SymbolName
= GuessSymbolName(method
.method_imp
, info
->AddrMap
);
4449 if (SymbolName
!= nullptr)
4450 outs() << SymbolName
;
4457 static void print_protocol_list64_t(uint64_t p
, struct DisassembleInfo
*info
) {
4458 struct protocol_list64_t pl
;
4459 uint64_t q
, n_value
;
4460 struct protocol64_t pc
;
4462 uint32_t offset
, xoffset
, left
, i
;
4464 const char *name
, *sym_name
;
4466 r
= get_pointer_64(p
, offset
, left
, S
, info
);
4469 memset(&pl
, '\0', sizeof(struct protocol_list64_t
));
4470 if (left
< sizeof(struct protocol_list64_t
)) {
4471 memcpy(&pl
, r
, left
);
4472 outs() << " (protocol_list_t entends past the end of the section)\n";
4474 memcpy(&pl
, r
, sizeof(struct protocol_list64_t
));
4475 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4477 outs() << " count " << pl
.count
<< "\n";
4479 p
+= sizeof(struct protocol_list64_t
);
4480 offset
+= sizeof(struct protocol_list64_t
);
4481 for (i
= 0; i
< pl
.count
; i
++) {
4482 r
= get_pointer_64(p
, offset
, left
, S
, info
);
4486 if (left
< sizeof(uint64_t)) {
4487 memcpy(&q
, r
, left
);
4488 outs() << " (protocol_t * entends past the end of the section)\n";
4490 memcpy(&q
, r
, sizeof(uint64_t));
4491 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4492 sys::swapByteOrder(q
);
4494 outs() << "\t\t list[" << i
<< "] ";
4495 sym_name
= get_symbol_64(offset
, S
, info
, n_value
, q
);
4497 if (info
->verbose
&& sym_name
!= nullptr)
4500 outs() << format("0x%" PRIx64
, n_value
);
4502 outs() << " + " << format("0x%" PRIx64
, q
);
4504 outs() << format("0x%" PRIx64
, q
);
4505 outs() << " (struct protocol_t *)\n";
4507 r
= get_pointer_64(q
+ n_value
, offset
, left
, S
, info
);
4510 memset(&pc
, '\0', sizeof(struct protocol64_t
));
4511 if (left
< sizeof(struct protocol64_t
)) {
4512 memcpy(&pc
, r
, left
);
4513 outs() << " (protocol_t entends past the end of the section)\n";
4515 memcpy(&pc
, r
, sizeof(struct protocol64_t
));
4516 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4519 outs() << "\t\t\t isa " << format("0x%" PRIx64
, pc
.isa
) << "\n";
4521 outs() << "\t\t\t name ";
4522 sym_name
= get_symbol_64(offset
+ offsetof(struct protocol64_t
, name
), S
,
4523 info
, n_value
, pc
.name
);
4525 if (info
->verbose
&& sym_name
!= nullptr)
4528 outs() << format("0x%" PRIx64
, n_value
);
4530 outs() << " + " << format("0x%" PRIx64
, pc
.name
);
4532 outs() << format("0x%" PRIx64
, pc
.name
);
4533 name
= get_pointer_64(pc
.name
+ n_value
, xoffset
, left
, xS
, info
);
4534 if (name
!= nullptr)
4535 outs() << format(" %.*s", left
, name
);
4538 outs() << "\t\t\tprotocols " << format("0x%" PRIx64
, pc
.protocols
) << "\n";
4540 outs() << "\t\t instanceMethods ";
4542 get_symbol_64(offset
+ offsetof(struct protocol64_t
, instanceMethods
),
4543 S
, info
, n_value
, pc
.instanceMethods
);
4545 if (info
->verbose
&& sym_name
!= nullptr)
4548 outs() << format("0x%" PRIx64
, n_value
);
4549 if (pc
.instanceMethods
!= 0)
4550 outs() << " + " << format("0x%" PRIx64
, pc
.instanceMethods
);
4552 outs() << format("0x%" PRIx64
, pc
.instanceMethods
);
4553 outs() << " (struct method_list_t *)\n";
4554 if (pc
.instanceMethods
+ n_value
!= 0)
4555 print_method_list64_t(pc
.instanceMethods
+ n_value
, info
, "\t");
4557 outs() << "\t\t classMethods ";
4559 get_symbol_64(offset
+ offsetof(struct protocol64_t
, classMethods
), S
,
4560 info
, n_value
, pc
.classMethods
);
4562 if (info
->verbose
&& sym_name
!= nullptr)
4565 outs() << format("0x%" PRIx64
, n_value
);
4566 if (pc
.classMethods
!= 0)
4567 outs() << " + " << format("0x%" PRIx64
, pc
.classMethods
);
4569 outs() << format("0x%" PRIx64
, pc
.classMethods
);
4570 outs() << " (struct method_list_t *)\n";
4571 if (pc
.classMethods
+ n_value
!= 0)
4572 print_method_list64_t(pc
.classMethods
+ n_value
, info
, "\t");
4574 outs() << "\t optionalInstanceMethods "
4575 << format("0x%" PRIx64
, pc
.optionalInstanceMethods
) << "\n";
4576 outs() << "\t optionalClassMethods "
4577 << format("0x%" PRIx64
, pc
.optionalClassMethods
) << "\n";
4578 outs() << "\t instanceProperties "
4579 << format("0x%" PRIx64
, pc
.instanceProperties
) << "\n";
4581 p
+= sizeof(uint64_t);
4582 offset
+= sizeof(uint64_t);
4586 static void print_protocol_list32_t(uint32_t p
, struct DisassembleInfo
*info
) {
4587 struct protocol_list32_t pl
;
4589 struct protocol32_t pc
;
4591 uint32_t offset
, xoffset
, left
, i
;
4595 r
= get_pointer_32(p
, offset
, left
, S
, info
);
4598 memset(&pl
, '\0', sizeof(struct protocol_list32_t
));
4599 if (left
< sizeof(struct protocol_list32_t
)) {
4600 memcpy(&pl
, r
, left
);
4601 outs() << " (protocol_list_t entends past the end of the section)\n";
4603 memcpy(&pl
, r
, sizeof(struct protocol_list32_t
));
4604 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4606 outs() << " count " << pl
.count
<< "\n";
4608 p
+= sizeof(struct protocol_list32_t
);
4609 offset
+= sizeof(struct protocol_list32_t
);
4610 for (i
= 0; i
< pl
.count
; i
++) {
4611 r
= get_pointer_32(p
, offset
, left
, S
, info
);
4615 if (left
< sizeof(uint32_t)) {
4616 memcpy(&q
, r
, left
);
4617 outs() << " (protocol_t * entends past the end of the section)\n";
4619 memcpy(&q
, r
, sizeof(uint32_t));
4620 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4621 sys::swapByteOrder(q
);
4622 outs() << "\t\t list[" << i
<< "] " << format("0x%" PRIx32
, q
)
4623 << " (struct protocol_t *)\n";
4624 r
= get_pointer_32(q
, offset
, left
, S
, info
);
4627 memset(&pc
, '\0', sizeof(struct protocol32_t
));
4628 if (left
< sizeof(struct protocol32_t
)) {
4629 memcpy(&pc
, r
, left
);
4630 outs() << " (protocol_t entends past the end of the section)\n";
4632 memcpy(&pc
, r
, sizeof(struct protocol32_t
));
4633 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4635 outs() << "\t\t\t isa " << format("0x%" PRIx32
, pc
.isa
) << "\n";
4636 outs() << "\t\t\t name " << format("0x%" PRIx32
, pc
.name
);
4637 name
= get_pointer_32(pc
.name
, xoffset
, left
, xS
, info
);
4638 if (name
!= nullptr)
4639 outs() << format(" %.*s", left
, name
);
4641 outs() << "\t\t\tprotocols " << format("0x%" PRIx32
, pc
.protocols
) << "\n";
4642 outs() << "\t\t instanceMethods "
4643 << format("0x%" PRIx32
, pc
.instanceMethods
)
4644 << " (struct method_list_t *)\n";
4645 if (pc
.instanceMethods
!= 0)
4646 print_method_list32_t(pc
.instanceMethods
, info
, "\t");
4647 outs() << "\t\t classMethods " << format("0x%" PRIx32
, pc
.classMethods
)
4648 << " (struct method_list_t *)\n";
4649 if (pc
.classMethods
!= 0)
4650 print_method_list32_t(pc
.classMethods
, info
, "\t");
4651 outs() << "\t optionalInstanceMethods "
4652 << format("0x%" PRIx32
, pc
.optionalInstanceMethods
) << "\n";
4653 outs() << "\t optionalClassMethods "
4654 << format("0x%" PRIx32
, pc
.optionalClassMethods
) << "\n";
4655 outs() << "\t instanceProperties "
4656 << format("0x%" PRIx32
, pc
.instanceProperties
) << "\n";
4657 p
+= sizeof(uint32_t);
4658 offset
+= sizeof(uint32_t);
4662 static void print_indent(uint32_t indent
) {
4663 for (uint32_t i
= 0; i
< indent
;) {
4664 if (indent
- i
>= 8) {
4668 for (uint32_t j
= i
; j
< indent
; j
++)
4675 static bool print_method_description_list(uint32_t p
, uint32_t indent
,
4676 struct DisassembleInfo
*info
) {
4677 uint32_t offset
, left
, xleft
;
4679 struct objc_method_description_list_t mdl
;
4680 struct objc_method_description_t md
;
4681 const char *r
, *list
, *name
;
4684 r
= get_pointer_32(p
, offset
, left
, S
, info
, true);
4689 if (left
> sizeof(struct objc_method_description_list_t
)) {
4690 memcpy(&mdl
, r
, sizeof(struct objc_method_description_list_t
));
4692 print_indent(indent
);
4693 outs() << " objc_method_description_list extends past end of the section\n";
4694 memset(&mdl
, '\0', sizeof(struct objc_method_description_list_t
));
4695 memcpy(&mdl
, r
, left
);
4697 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4700 print_indent(indent
);
4701 outs() << " count " << mdl
.count
<< "\n";
4703 list
= r
+ sizeof(struct objc_method_description_list_t
);
4704 for (i
= 0; i
< mdl
.count
; i
++) {
4705 if ((i
+ 1) * sizeof(struct objc_method_description_t
) > left
) {
4706 print_indent(indent
);
4707 outs() << " remaining list entries extend past the of the section\n";
4710 print_indent(indent
);
4711 outs() << " list[" << i
<< "]\n";
4712 memcpy(&md
, list
+ i
* sizeof(struct objc_method_description_t
),
4713 sizeof(struct objc_method_description_t
));
4714 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4717 print_indent(indent
);
4718 outs() << " name " << format("0x%08" PRIx32
, md
.name
);
4719 if (info
->verbose
) {
4720 name
= get_pointer_32(md
.name
, offset
, xleft
, S
, info
, true);
4721 if (name
!= nullptr)
4722 outs() << format(" %.*s", xleft
, name
);
4724 outs() << " (not in an __OBJC section)";
4728 print_indent(indent
);
4729 outs() << " types " << format("0x%08" PRIx32
, md
.types
);
4730 if (info
->verbose
) {
4731 name
= get_pointer_32(md
.types
, offset
, xleft
, S
, info
, true);
4732 if (name
!= nullptr)
4733 outs() << format(" %.*s", xleft
, name
);
4735 outs() << " (not in an __OBJC section)";
4742 static bool print_protocol_list(uint32_t p
, uint32_t indent
,
4743 struct DisassembleInfo
*info
);
4745 static bool print_protocol(uint32_t p
, uint32_t indent
,
4746 struct DisassembleInfo
*info
) {
4747 uint32_t offset
, left
;
4749 struct objc_protocol_t protocol
;
4750 const char *r
, *name
;
4752 r
= get_pointer_32(p
, offset
, left
, S
, info
, true);
4757 if (left
>= sizeof(struct objc_protocol_t
)) {
4758 memcpy(&protocol
, r
, sizeof(struct objc_protocol_t
));
4760 print_indent(indent
);
4761 outs() << " Protocol extends past end of the section\n";
4762 memset(&protocol
, '\0', sizeof(struct objc_protocol_t
));
4763 memcpy(&protocol
, r
, left
);
4765 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4766 swapStruct(protocol
);
4768 print_indent(indent
);
4769 outs() << " isa " << format("0x%08" PRIx32
, protocol
.isa
)
4772 print_indent(indent
);
4773 outs() << " protocol_name "
4774 << format("0x%08" PRIx32
, protocol
.protocol_name
);
4775 if (info
->verbose
) {
4776 name
= get_pointer_32(protocol
.protocol_name
, offset
, left
, S
, info
, true);
4777 if (name
!= nullptr)
4778 outs() << format(" %.*s", left
, name
);
4780 outs() << " (not in an __OBJC section)";
4784 print_indent(indent
);
4785 outs() << " protocol_list "
4786 << format("0x%08" PRIx32
, protocol
.protocol_list
);
4787 if (print_protocol_list(protocol
.protocol_list
, indent
+ 4, info
))
4788 outs() << " (not in an __OBJC section)\n";
4790 print_indent(indent
);
4791 outs() << " instance_methods "
4792 << format("0x%08" PRIx32
, protocol
.instance_methods
);
4793 if (print_method_description_list(protocol
.instance_methods
, indent
, info
))
4794 outs() << " (not in an __OBJC section)\n";
4796 print_indent(indent
);
4797 outs() << " class_methods "
4798 << format("0x%08" PRIx32
, protocol
.class_methods
);
4799 if (print_method_description_list(protocol
.class_methods
, indent
, info
))
4800 outs() << " (not in an __OBJC section)\n";
4805 static bool print_protocol_list(uint32_t p
, uint32_t indent
,
4806 struct DisassembleInfo
*info
) {
4807 uint32_t offset
, left
, l
;
4809 struct objc_protocol_list_t protocol_list
;
4810 const char *r
, *list
;
4813 r
= get_pointer_32(p
, offset
, left
, S
, info
, true);
4818 if (left
> sizeof(struct objc_protocol_list_t
)) {
4819 memcpy(&protocol_list
, r
, sizeof(struct objc_protocol_list_t
));
4821 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
4822 memset(&protocol_list
, '\0', sizeof(struct objc_protocol_list_t
));
4823 memcpy(&protocol_list
, r
, left
);
4825 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4826 swapStruct(protocol_list
);
4828 print_indent(indent
);
4829 outs() << " next " << format("0x%08" PRIx32
, protocol_list
.next
)
4831 print_indent(indent
);
4832 outs() << " count " << protocol_list
.count
<< "\n";
4834 list
= r
+ sizeof(struct objc_protocol_list_t
);
4835 for (i
= 0; i
< protocol_list
.count
; i
++) {
4836 if ((i
+ 1) * sizeof(uint32_t) > left
) {
4837 outs() << "\t\t remaining list entries extend past the of the section\n";
4840 memcpy(&l
, list
+ i
* sizeof(uint32_t), sizeof(uint32_t));
4841 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4842 sys::swapByteOrder(l
);
4844 print_indent(indent
);
4845 outs() << " list[" << i
<< "] " << format("0x%08" PRIx32
, l
);
4846 if (print_protocol(l
, indent
, info
))
4847 outs() << "(not in an __OBJC section)\n";
4852 static void print_ivar_list64_t(uint64_t p
, struct DisassembleInfo
*info
) {
4853 struct ivar_list64_t il
;
4856 uint32_t offset
, xoffset
, left
, j
;
4858 const char *name
, *sym_name
, *ivar_offset_p
;
4859 uint64_t ivar_offset
, n_value
;
4861 r
= get_pointer_64(p
, offset
, left
, S
, info
);
4864 memset(&il
, '\0', sizeof(struct ivar_list64_t
));
4865 if (left
< sizeof(struct ivar_list64_t
)) {
4866 memcpy(&il
, r
, left
);
4867 outs() << " (ivar_list_t entends past the end of the section)\n";
4869 memcpy(&il
, r
, sizeof(struct ivar_list64_t
));
4870 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4872 outs() << " entsize " << il
.entsize
<< "\n";
4873 outs() << " count " << il
.count
<< "\n";
4875 p
+= sizeof(struct ivar_list64_t
);
4876 offset
+= sizeof(struct ivar_list64_t
);
4877 for (j
= 0; j
< il
.count
; j
++) {
4878 r
= get_pointer_64(p
, offset
, left
, S
, info
);
4881 memset(&i
, '\0', sizeof(struct ivar64_t
));
4882 if (left
< sizeof(struct ivar64_t
)) {
4883 memcpy(&i
, r
, left
);
4884 outs() << " (ivar_t entends past the end of the section)\n";
4886 memcpy(&i
, r
, sizeof(struct ivar64_t
));
4887 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4890 outs() << "\t\t\t offset ";
4891 sym_name
= get_symbol_64(offset
+ offsetof(struct ivar64_t
, offset
), S
,
4892 info
, n_value
, i
.offset
);
4894 if (info
->verbose
&& sym_name
!= nullptr)
4897 outs() << format("0x%" PRIx64
, n_value
);
4899 outs() << " + " << format("0x%" PRIx64
, i
.offset
);
4901 outs() << format("0x%" PRIx64
, i
.offset
);
4902 ivar_offset_p
= get_pointer_64(i
.offset
+ n_value
, xoffset
, left
, xS
, info
);
4903 if (ivar_offset_p
!= nullptr && left
>= sizeof(*ivar_offset_p
)) {
4904 memcpy(&ivar_offset
, ivar_offset_p
, sizeof(ivar_offset
));
4905 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4906 sys::swapByteOrder(ivar_offset
);
4907 outs() << " " << ivar_offset
<< "\n";
4911 outs() << "\t\t\t name ";
4912 sym_name
= get_symbol_64(offset
+ offsetof(struct ivar64_t
, name
), S
, info
,
4915 if (info
->verbose
&& sym_name
!= nullptr)
4918 outs() << format("0x%" PRIx64
, n_value
);
4920 outs() << " + " << format("0x%" PRIx64
, i
.name
);
4922 outs() << format("0x%" PRIx64
, i
.name
);
4923 name
= get_pointer_64(i
.name
+ n_value
, xoffset
, left
, xS
, info
);
4924 if (name
!= nullptr)
4925 outs() << format(" %.*s", left
, name
);
4928 outs() << "\t\t\t type ";
4929 sym_name
= get_symbol_64(offset
+ offsetof(struct ivar64_t
, type
), S
, info
,
4931 name
= get_pointer_64(i
.type
+ n_value
, xoffset
, left
, xS
, info
);
4933 if (info
->verbose
&& sym_name
!= nullptr)
4936 outs() << format("0x%" PRIx64
, n_value
);
4938 outs() << " + " << format("0x%" PRIx64
, i
.type
);
4940 outs() << format("0x%" PRIx64
, i
.type
);
4941 if (name
!= nullptr)
4942 outs() << format(" %.*s", left
, name
);
4945 outs() << "\t\t\talignment " << i
.alignment
<< "\n";
4946 outs() << "\t\t\t size " << i
.size
<< "\n";
4948 p
+= sizeof(struct ivar64_t
);
4949 offset
+= sizeof(struct ivar64_t
);
4953 static void print_ivar_list32_t(uint32_t p
, struct DisassembleInfo
*info
) {
4954 struct ivar_list32_t il
;
4957 uint32_t offset
, xoffset
, left
, j
;
4959 const char *name
, *ivar_offset_p
;
4960 uint32_t ivar_offset
;
4962 r
= get_pointer_32(p
, offset
, left
, S
, info
);
4965 memset(&il
, '\0', sizeof(struct ivar_list32_t
));
4966 if (left
< sizeof(struct ivar_list32_t
)) {
4967 memcpy(&il
, r
, left
);
4968 outs() << " (ivar_list_t entends past the end of the section)\n";
4970 memcpy(&il
, r
, sizeof(struct ivar_list32_t
));
4971 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4973 outs() << " entsize " << il
.entsize
<< "\n";
4974 outs() << " count " << il
.count
<< "\n";
4976 p
+= sizeof(struct ivar_list32_t
);
4977 offset
+= sizeof(struct ivar_list32_t
);
4978 for (j
= 0; j
< il
.count
; j
++) {
4979 r
= get_pointer_32(p
, offset
, left
, S
, info
);
4982 memset(&i
, '\0', sizeof(struct ivar32_t
));
4983 if (left
< sizeof(struct ivar32_t
)) {
4984 memcpy(&i
, r
, left
);
4985 outs() << " (ivar_t entends past the end of the section)\n";
4987 memcpy(&i
, r
, sizeof(struct ivar32_t
));
4988 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4991 outs() << "\t\t\t offset " << format("0x%" PRIx32
, i
.offset
);
4992 ivar_offset_p
= get_pointer_32(i
.offset
, xoffset
, left
, xS
, info
);
4993 if (ivar_offset_p
!= nullptr && left
>= sizeof(*ivar_offset_p
)) {
4994 memcpy(&ivar_offset
, ivar_offset_p
, sizeof(ivar_offset
));
4995 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4996 sys::swapByteOrder(ivar_offset
);
4997 outs() << " " << ivar_offset
<< "\n";
5001 outs() << "\t\t\t name " << format("0x%" PRIx32
, i
.name
);
5002 name
= get_pointer_32(i
.name
, xoffset
, left
, xS
, info
);
5003 if (name
!= nullptr)
5004 outs() << format(" %.*s", left
, name
);
5007 outs() << "\t\t\t type " << format("0x%" PRIx32
, i
.type
);
5008 name
= get_pointer_32(i
.type
, xoffset
, left
, xS
, info
);
5009 if (name
!= nullptr)
5010 outs() << format(" %.*s", left
, name
);
5013 outs() << "\t\t\talignment " << i
.alignment
<< "\n";
5014 outs() << "\t\t\t size " << i
.size
<< "\n";
5016 p
+= sizeof(struct ivar32_t
);
5017 offset
+= sizeof(struct ivar32_t
);
5021 static void print_objc_property_list64(uint64_t p
,
5022 struct DisassembleInfo
*info
) {
5023 struct objc_property_list64 opl
;
5024 struct objc_property64 op
;
5026 uint32_t offset
, xoffset
, left
, j
;
5028 const char *name
, *sym_name
;
5031 r
= get_pointer_64(p
, offset
, left
, S
, info
);
5034 memset(&opl
, '\0', sizeof(struct objc_property_list64
));
5035 if (left
< sizeof(struct objc_property_list64
)) {
5036 memcpy(&opl
, r
, left
);
5037 outs() << " (objc_property_list entends past the end of the section)\n";
5039 memcpy(&opl
, r
, sizeof(struct objc_property_list64
));
5040 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5042 outs() << " entsize " << opl
.entsize
<< "\n";
5043 outs() << " count " << opl
.count
<< "\n";
5045 p
+= sizeof(struct objc_property_list64
);
5046 offset
+= sizeof(struct objc_property_list64
);
5047 for (j
= 0; j
< opl
.count
; j
++) {
5048 r
= get_pointer_64(p
, offset
, left
, S
, info
);
5051 memset(&op
, '\0', sizeof(struct objc_property64
));
5052 if (left
< sizeof(struct objc_property64
)) {
5053 memcpy(&op
, r
, left
);
5054 outs() << " (objc_property entends past the end of the section)\n";
5056 memcpy(&op
, r
, sizeof(struct objc_property64
));
5057 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5060 outs() << "\t\t\t name ";
5061 sym_name
= get_symbol_64(offset
+ offsetof(struct objc_property64
, name
), S
,
5062 info
, n_value
, op
.name
);
5064 if (info
->verbose
&& sym_name
!= nullptr)
5067 outs() << format("0x%" PRIx64
, n_value
);
5069 outs() << " + " << format("0x%" PRIx64
, op
.name
);
5071 outs() << format("0x%" PRIx64
, op
.name
);
5072 name
= get_pointer_64(op
.name
+ n_value
, xoffset
, left
, xS
, info
);
5073 if (name
!= nullptr)
5074 outs() << format(" %.*s", left
, name
);
5077 outs() << "\t\t\tattributes ";
5079 get_symbol_64(offset
+ offsetof(struct objc_property64
, attributes
), S
,
5080 info
, n_value
, op
.attributes
);
5082 if (info
->verbose
&& sym_name
!= nullptr)
5085 outs() << format("0x%" PRIx64
, n_value
);
5086 if (op
.attributes
!= 0)
5087 outs() << " + " << format("0x%" PRIx64
, op
.attributes
);
5089 outs() << format("0x%" PRIx64
, op
.attributes
);
5090 name
= get_pointer_64(op
.attributes
+ n_value
, xoffset
, left
, xS
, info
);
5091 if (name
!= nullptr)
5092 outs() << format(" %.*s", left
, name
);
5095 p
+= sizeof(struct objc_property64
);
5096 offset
+= sizeof(struct objc_property64
);
5100 static void print_objc_property_list32(uint32_t p
,
5101 struct DisassembleInfo
*info
) {
5102 struct objc_property_list32 opl
;
5103 struct objc_property32 op
;
5105 uint32_t offset
, xoffset
, left
, j
;
5109 r
= get_pointer_32(p
, offset
, left
, S
, info
);
5112 memset(&opl
, '\0', sizeof(struct objc_property_list32
));
5113 if (left
< sizeof(struct objc_property_list32
)) {
5114 memcpy(&opl
, r
, left
);
5115 outs() << " (objc_property_list entends past the end of the section)\n";
5117 memcpy(&opl
, r
, sizeof(struct objc_property_list32
));
5118 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5120 outs() << " entsize " << opl
.entsize
<< "\n";
5121 outs() << " count " << opl
.count
<< "\n";
5123 p
+= sizeof(struct objc_property_list32
);
5124 offset
+= sizeof(struct objc_property_list32
);
5125 for (j
= 0; j
< opl
.count
; j
++) {
5126 r
= get_pointer_32(p
, offset
, left
, S
, info
);
5129 memset(&op
, '\0', sizeof(struct objc_property32
));
5130 if (left
< sizeof(struct objc_property32
)) {
5131 memcpy(&op
, r
, left
);
5132 outs() << " (objc_property entends past the end of the section)\n";
5134 memcpy(&op
, r
, sizeof(struct objc_property32
));
5135 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5138 outs() << "\t\t\t name " << format("0x%" PRIx32
, op
.name
);
5139 name
= get_pointer_32(op
.name
, xoffset
, left
, xS
, info
);
5140 if (name
!= nullptr)
5141 outs() << format(" %.*s", left
, name
);
5144 outs() << "\t\t\tattributes " << format("0x%" PRIx32
, op
.attributes
);
5145 name
= get_pointer_32(op
.attributes
, xoffset
, left
, xS
, info
);
5146 if (name
!= nullptr)
5147 outs() << format(" %.*s", left
, name
);
5150 p
+= sizeof(struct objc_property32
);
5151 offset
+= sizeof(struct objc_property32
);
5155 static bool print_class_ro64_t(uint64_t p
, struct DisassembleInfo
*info
,
5156 bool &is_meta_class
) {
5157 struct class_ro64_t cro
;
5159 uint32_t offset
, xoffset
, left
;
5161 const char *name
, *sym_name
;
5164 r
= get_pointer_64(p
, offset
, left
, S
, info
);
5165 if (r
== nullptr || left
< sizeof(struct class_ro64_t
))
5167 memcpy(&cro
, r
, sizeof(struct class_ro64_t
));
5168 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5170 outs() << " flags " << format("0x%" PRIx32
, cro
.flags
);
5171 if (cro
.flags
& RO_META
)
5172 outs() << " RO_META";
5173 if (cro
.flags
& RO_ROOT
)
5174 outs() << " RO_ROOT";
5175 if (cro
.flags
& RO_HAS_CXX_STRUCTORS
)
5176 outs() << " RO_HAS_CXX_STRUCTORS";
5178 outs() << " instanceStart " << cro
.instanceStart
<< "\n";
5179 outs() << " instanceSize " << cro
.instanceSize
<< "\n";
5180 outs() << " reserved " << format("0x%" PRIx32
, cro
.reserved
)
5182 outs() << " ivarLayout " << format("0x%" PRIx64
, cro
.ivarLayout
)
5184 print_layout_map64(cro
.ivarLayout
, info
);
5187 sym_name
= get_symbol_64(offset
+ offsetof(struct class_ro64_t
, name
), S
,
5188 info
, n_value
, cro
.name
);
5190 if (info
->verbose
&& sym_name
!= nullptr)
5193 outs() << format("0x%" PRIx64
, n_value
);
5195 outs() << " + " << format("0x%" PRIx64
, cro
.name
);
5197 outs() << format("0x%" PRIx64
, cro
.name
);
5198 name
= get_pointer_64(cro
.name
+ n_value
, xoffset
, left
, xS
, info
);
5199 if (name
!= nullptr)
5200 outs() << format(" %.*s", left
, name
);
5203 outs() << " baseMethods ";
5204 sym_name
= get_symbol_64(offset
+ offsetof(struct class_ro64_t
, baseMethods
),
5205 S
, info
, n_value
, cro
.baseMethods
);
5207 if (info
->verbose
&& sym_name
!= nullptr)
5210 outs() << format("0x%" PRIx64
, n_value
);
5211 if (cro
.baseMethods
!= 0)
5212 outs() << " + " << format("0x%" PRIx64
, cro
.baseMethods
);
5214 outs() << format("0x%" PRIx64
, cro
.baseMethods
);
5215 outs() << " (struct method_list_t *)\n";
5216 if (cro
.baseMethods
+ n_value
!= 0)
5217 print_method_list64_t(cro
.baseMethods
+ n_value
, info
, "");
5219 outs() << " baseProtocols ";
5221 get_symbol_64(offset
+ offsetof(struct class_ro64_t
, baseProtocols
), S
,
5222 info
, n_value
, cro
.baseProtocols
);
5224 if (info
->verbose
&& sym_name
!= nullptr)
5227 outs() << format("0x%" PRIx64
, n_value
);
5228 if (cro
.baseProtocols
!= 0)
5229 outs() << " + " << format("0x%" PRIx64
, cro
.baseProtocols
);
5231 outs() << format("0x%" PRIx64
, cro
.baseProtocols
);
5233 if (cro
.baseProtocols
+ n_value
!= 0)
5234 print_protocol_list64_t(cro
.baseProtocols
+ n_value
, info
);
5236 outs() << " ivars ";
5237 sym_name
= get_symbol_64(offset
+ offsetof(struct class_ro64_t
, ivars
), S
,
5238 info
, n_value
, cro
.ivars
);
5240 if (info
->verbose
&& sym_name
!= nullptr)
5243 outs() << format("0x%" PRIx64
, n_value
);
5245 outs() << " + " << format("0x%" PRIx64
, cro
.ivars
);
5247 outs() << format("0x%" PRIx64
, cro
.ivars
);
5249 if (cro
.ivars
+ n_value
!= 0)
5250 print_ivar_list64_t(cro
.ivars
+ n_value
, info
);
5252 outs() << " weakIvarLayout ";
5254 get_symbol_64(offset
+ offsetof(struct class_ro64_t
, weakIvarLayout
), S
,
5255 info
, n_value
, cro
.weakIvarLayout
);
5257 if (info
->verbose
&& sym_name
!= nullptr)
5260 outs() << format("0x%" PRIx64
, n_value
);
5261 if (cro
.weakIvarLayout
!= 0)
5262 outs() << " + " << format("0x%" PRIx64
, cro
.weakIvarLayout
);
5264 outs() << format("0x%" PRIx64
, cro
.weakIvarLayout
);
5266 print_layout_map64(cro
.weakIvarLayout
+ n_value
, info
);
5268 outs() << " baseProperties ";
5270 get_symbol_64(offset
+ offsetof(struct class_ro64_t
, baseProperties
), S
,
5271 info
, n_value
, cro
.baseProperties
);
5273 if (info
->verbose
&& sym_name
!= nullptr)
5276 outs() << format("0x%" PRIx64
, n_value
);
5277 if (cro
.baseProperties
!= 0)
5278 outs() << " + " << format("0x%" PRIx64
, cro
.baseProperties
);
5280 outs() << format("0x%" PRIx64
, cro
.baseProperties
);
5282 if (cro
.baseProperties
+ n_value
!= 0)
5283 print_objc_property_list64(cro
.baseProperties
+ n_value
, info
);
5285 is_meta_class
= (cro
.flags
& RO_META
) != 0;
5289 static bool print_class_ro32_t(uint32_t p
, struct DisassembleInfo
*info
,
5290 bool &is_meta_class
) {
5291 struct class_ro32_t cro
;
5293 uint32_t offset
, xoffset
, left
;
5297 r
= get_pointer_32(p
, offset
, left
, S
, info
);
5300 memset(&cro
, '\0', sizeof(struct class_ro32_t
));
5301 if (left
< sizeof(struct class_ro32_t
)) {
5302 memcpy(&cro
, r
, left
);
5303 outs() << " (class_ro_t entends past the end of the section)\n";
5305 memcpy(&cro
, r
, sizeof(struct class_ro32_t
));
5306 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5308 outs() << " flags " << format("0x%" PRIx32
, cro
.flags
);
5309 if (cro
.flags
& RO_META
)
5310 outs() << " RO_META";
5311 if (cro
.flags
& RO_ROOT
)
5312 outs() << " RO_ROOT";
5313 if (cro
.flags
& RO_HAS_CXX_STRUCTORS
)
5314 outs() << " RO_HAS_CXX_STRUCTORS";
5316 outs() << " instanceStart " << cro
.instanceStart
<< "\n";
5317 outs() << " instanceSize " << cro
.instanceSize
<< "\n";
5318 outs() << " ivarLayout " << format("0x%" PRIx32
, cro
.ivarLayout
)
5320 print_layout_map32(cro
.ivarLayout
, info
);
5322 outs() << " name " << format("0x%" PRIx32
, cro
.name
);
5323 name
= get_pointer_32(cro
.name
, xoffset
, left
, xS
, info
);
5324 if (name
!= nullptr)
5325 outs() << format(" %.*s", left
, name
);
5328 outs() << " baseMethods "
5329 << format("0x%" PRIx32
, cro
.baseMethods
)
5330 << " (struct method_list_t *)\n";
5331 if (cro
.baseMethods
!= 0)
5332 print_method_list32_t(cro
.baseMethods
, info
, "");
5334 outs() << " baseProtocols "
5335 << format("0x%" PRIx32
, cro
.baseProtocols
) << "\n";
5336 if (cro
.baseProtocols
!= 0)
5337 print_protocol_list32_t(cro
.baseProtocols
, info
);
5338 outs() << " ivars " << format("0x%" PRIx32
, cro
.ivars
)
5341 print_ivar_list32_t(cro
.ivars
, info
);
5342 outs() << " weakIvarLayout "
5343 << format("0x%" PRIx32
, cro
.weakIvarLayout
) << "\n";
5344 print_layout_map32(cro
.weakIvarLayout
, info
);
5345 outs() << " baseProperties "
5346 << format("0x%" PRIx32
, cro
.baseProperties
) << "\n";
5347 if (cro
.baseProperties
!= 0)
5348 print_objc_property_list32(cro
.baseProperties
, info
);
5349 is_meta_class
= (cro
.flags
& RO_META
) != 0;
5353 static void print_class64_t(uint64_t p
, struct DisassembleInfo
*info
) {
5356 uint32_t offset
, left
;
5359 uint64_t isa_n_value
, n_value
;
5361 r
= get_pointer_64(p
, offset
, left
, S
, info
);
5362 if (r
== nullptr || left
< sizeof(struct class64_t
))
5364 memcpy(&c
, r
, sizeof(struct class64_t
));
5365 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5368 outs() << " isa " << format("0x%" PRIx64
, c
.isa
);
5369 name
= get_symbol_64(offset
+ offsetof(struct class64_t
, isa
), S
, info
,
5370 isa_n_value
, c
.isa
);
5371 if (name
!= nullptr)
5372 outs() << " " << name
;
5375 outs() << " superclass " << format("0x%" PRIx64
, c
.superclass
);
5376 name
= get_symbol_64(offset
+ offsetof(struct class64_t
, superclass
), S
, info
,
5377 n_value
, c
.superclass
);
5378 if (name
!= nullptr)
5379 outs() << " " << name
;
5381 name
= get_dyld_bind_info_symbolname(S
.getAddress() +
5382 offset
+ offsetof(struct class64_t
, superclass
), info
);
5383 if (name
!= nullptr)
5384 outs() << " " << name
;
5388 outs() << " cache " << format("0x%" PRIx64
, c
.cache
);
5389 name
= get_symbol_64(offset
+ offsetof(struct class64_t
, cache
), S
, info
,
5391 if (name
!= nullptr)
5392 outs() << " " << name
;
5395 outs() << " vtable " << format("0x%" PRIx64
, c
.vtable
);
5396 name
= get_symbol_64(offset
+ offsetof(struct class64_t
, vtable
), S
, info
,
5398 if (name
!= nullptr)
5399 outs() << " " << name
;
5402 name
= get_symbol_64(offset
+ offsetof(struct class64_t
, data
), S
, info
,
5406 if (info
->verbose
&& name
!= nullptr)
5409 outs() << format("0x%" PRIx64
, n_value
);
5411 outs() << " + " << format("0x%" PRIx64
, c
.data
);
5413 outs() << format("0x%" PRIx64
, c
.data
);
5414 outs() << " (struct class_ro_t *)";
5416 // This is a Swift class if some of the low bits of the pointer are set.
5417 if ((c
.data
+ n_value
) & 0x7)
5418 outs() << " Swift class";
5421 if (!print_class_ro64_t((c
.data
+ n_value
) & ~0x7, info
, is_meta_class
))
5424 if (!is_meta_class
&&
5425 c
.isa
+ isa_n_value
!= p
&&
5426 c
.isa
+ isa_n_value
!= 0 &&
5427 info
->depth
< 100) {
5429 outs() << "Meta Class\n";
5430 print_class64_t(c
.isa
+ isa_n_value
, info
);
5434 static void print_class32_t(uint32_t p
, struct DisassembleInfo
*info
) {
5437 uint32_t offset
, left
;
5441 r
= get_pointer_32(p
, offset
, left
, S
, info
);
5444 memset(&c
, '\0', sizeof(struct class32_t
));
5445 if (left
< sizeof(struct class32_t
)) {
5446 memcpy(&c
, r
, left
);
5447 outs() << " (class_t entends past the end of the section)\n";
5449 memcpy(&c
, r
, sizeof(struct class32_t
));
5450 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5453 outs() << " isa " << format("0x%" PRIx32
, c
.isa
);
5455 get_symbol_32(offset
+ offsetof(struct class32_t
, isa
), S
, info
, c
.isa
);
5456 if (name
!= nullptr)
5457 outs() << " " << name
;
5460 outs() << " superclass " << format("0x%" PRIx32
, c
.superclass
);
5461 name
= get_symbol_32(offset
+ offsetof(struct class32_t
, superclass
), S
, info
,
5463 if (name
!= nullptr)
5464 outs() << " " << name
;
5467 outs() << " cache " << format("0x%" PRIx32
, c
.cache
);
5468 name
= get_symbol_32(offset
+ offsetof(struct class32_t
, cache
), S
, info
,
5470 if (name
!= nullptr)
5471 outs() << " " << name
;
5474 outs() << " vtable " << format("0x%" PRIx32
, c
.vtable
);
5475 name
= get_symbol_32(offset
+ offsetof(struct class32_t
, vtable
), S
, info
,
5477 if (name
!= nullptr)
5478 outs() << " " << name
;
5482 get_symbol_32(offset
+ offsetof(struct class32_t
, data
), S
, info
, c
.data
);
5483 outs() << " data " << format("0x%" PRIx32
, c
.data
)
5484 << " (struct class_ro_t *)";
5486 // This is a Swift class if some of the low bits of the pointer are set.
5488 outs() << " Swift class";
5491 if (!print_class_ro32_t(c
.data
& ~0x3, info
, is_meta_class
))
5494 if (!is_meta_class
) {
5495 outs() << "Meta Class\n";
5496 print_class32_t(c
.isa
, info
);
5500 static void print_objc_class_t(struct objc_class_t
*objc_class
,
5501 struct DisassembleInfo
*info
) {
5502 uint32_t offset
, left
, xleft
;
5503 const char *name
, *p
, *ivar_list
;
5506 struct objc_ivar_list_t objc_ivar_list
;
5507 struct objc_ivar_t ivar
;
5509 outs() << "\t\t isa " << format("0x%08" PRIx32
, objc_class
->isa
);
5510 if (info
->verbose
&& CLS_GETINFO(objc_class
, CLS_META
)) {
5511 name
= get_pointer_32(objc_class
->isa
, offset
, left
, S
, info
, true);
5512 if (name
!= nullptr)
5513 outs() << format(" %.*s", left
, name
);
5515 outs() << " (not in an __OBJC section)";
5519 outs() << "\t super_class "
5520 << format("0x%08" PRIx32
, objc_class
->super_class
);
5521 if (info
->verbose
) {
5522 name
= get_pointer_32(objc_class
->super_class
, offset
, left
, S
, info
, true);
5523 if (name
!= nullptr)
5524 outs() << format(" %.*s", left
, name
);
5526 outs() << " (not in an __OBJC section)";
5530 outs() << "\t\t name " << format("0x%08" PRIx32
, objc_class
->name
);
5531 if (info
->verbose
) {
5532 name
= get_pointer_32(objc_class
->name
, offset
, left
, S
, info
, true);
5533 if (name
!= nullptr)
5534 outs() << format(" %.*s", left
, name
);
5536 outs() << " (not in an __OBJC section)";
5540 outs() << "\t\t version " << format("0x%08" PRIx32
, objc_class
->version
)
5543 outs() << "\t\t info " << format("0x%08" PRIx32
, objc_class
->info
);
5544 if (info
->verbose
) {
5545 if (CLS_GETINFO(objc_class
, CLS_CLASS
))
5546 outs() << " CLS_CLASS";
5547 else if (CLS_GETINFO(objc_class
, CLS_META
))
5548 outs() << " CLS_META";
5552 outs() << "\t instance_size "
5553 << format("0x%08" PRIx32
, objc_class
->instance_size
) << "\n";
5555 p
= get_pointer_32(objc_class
->ivars
, offset
, left
, S
, info
, true);
5556 outs() << "\t\t ivars " << format("0x%08" PRIx32
, objc_class
->ivars
);
5558 if (left
> sizeof(struct objc_ivar_list_t
)) {
5560 memcpy(&objc_ivar_list
, p
, sizeof(struct objc_ivar_list_t
));
5562 outs() << " (entends past the end of the section)\n";
5563 memset(&objc_ivar_list
, '\0', sizeof(struct objc_ivar_list_t
));
5564 memcpy(&objc_ivar_list
, p
, left
);
5566 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5567 swapStruct(objc_ivar_list
);
5568 outs() << "\t\t ivar_count " << objc_ivar_list
.ivar_count
<< "\n";
5569 ivar_list
= p
+ sizeof(struct objc_ivar_list_t
);
5570 for (i
= 0; i
< objc_ivar_list
.ivar_count
; i
++) {
5571 if ((i
+ 1) * sizeof(struct objc_ivar_t
) > left
) {
5572 outs() << "\t\t remaining ivar's extend past the of the section\n";
5575 memcpy(&ivar
, ivar_list
+ i
* sizeof(struct objc_ivar_t
),
5576 sizeof(struct objc_ivar_t
));
5577 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5580 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32
, ivar
.ivar_name
);
5581 if (info
->verbose
) {
5582 name
= get_pointer_32(ivar
.ivar_name
, offset
, xleft
, S
, info
, true);
5583 if (name
!= nullptr)
5584 outs() << format(" %.*s", xleft
, name
);
5586 outs() << " (not in an __OBJC section)";
5590 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32
, ivar
.ivar_type
);
5591 if (info
->verbose
) {
5592 name
= get_pointer_32(ivar
.ivar_type
, offset
, xleft
, S
, info
, true);
5593 if (name
!= nullptr)
5594 outs() << format(" %.*s", xleft
, name
);
5596 outs() << " (not in an __OBJC section)";
5600 outs() << "\t\t ivar_offset "
5601 << format("0x%08" PRIx32
, ivar
.ivar_offset
) << "\n";
5604 outs() << " (not in an __OBJC section)\n";
5607 outs() << "\t\t methods " << format("0x%08" PRIx32
, objc_class
->methodLists
);
5608 if (print_method_list(objc_class
->methodLists
, info
))
5609 outs() << " (not in an __OBJC section)\n";
5611 outs() << "\t\t cache " << format("0x%08" PRIx32
, objc_class
->cache
)
5614 outs() << "\t\tprotocols " << format("0x%08" PRIx32
, objc_class
->protocols
);
5615 if (print_protocol_list(objc_class
->protocols
, 16, info
))
5616 outs() << " (not in an __OBJC section)\n";
5619 static void print_objc_objc_category_t(struct objc_category_t
*objc_category
,
5620 struct DisassembleInfo
*info
) {
5621 uint32_t offset
, left
;
5625 outs() << "\t category name "
5626 << format("0x%08" PRIx32
, objc_category
->category_name
);
5627 if (info
->verbose
) {
5628 name
= get_pointer_32(objc_category
->category_name
, offset
, left
, S
, info
,
5630 if (name
!= nullptr)
5631 outs() << format(" %.*s", left
, name
);
5633 outs() << " (not in an __OBJC section)";
5637 outs() << "\t\t class name "
5638 << format("0x%08" PRIx32
, objc_category
->class_name
);
5639 if (info
->verbose
) {
5641 get_pointer_32(objc_category
->class_name
, offset
, left
, S
, info
, true);
5642 if (name
!= nullptr)
5643 outs() << format(" %.*s", left
, name
);
5645 outs() << " (not in an __OBJC section)";
5649 outs() << "\t instance methods "
5650 << format("0x%08" PRIx32
, objc_category
->instance_methods
);
5651 if (print_method_list(objc_category
->instance_methods
, info
))
5652 outs() << " (not in an __OBJC section)\n";
5654 outs() << "\t class methods "
5655 << format("0x%08" PRIx32
, objc_category
->class_methods
);
5656 if (print_method_list(objc_category
->class_methods
, info
))
5657 outs() << " (not in an __OBJC section)\n";
5660 static void print_category64_t(uint64_t p
, struct DisassembleInfo
*info
) {
5661 struct category64_t c
;
5663 uint32_t offset
, xoffset
, left
;
5665 const char *name
, *sym_name
;
5668 r
= get_pointer_64(p
, offset
, left
, S
, info
);
5671 memset(&c
, '\0', sizeof(struct category64_t
));
5672 if (left
< sizeof(struct category64_t
)) {
5673 memcpy(&c
, r
, left
);
5674 outs() << " (category_t entends past the end of the section)\n";
5676 memcpy(&c
, r
, sizeof(struct category64_t
));
5677 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5681 sym_name
= get_symbol_64(offset
+ offsetof(struct category64_t
, name
), S
,
5682 info
, n_value
, c
.name
);
5684 if (info
->verbose
&& sym_name
!= nullptr)
5687 outs() << format("0x%" PRIx64
, n_value
);
5689 outs() << " + " << format("0x%" PRIx64
, c
.name
);
5691 outs() << format("0x%" PRIx64
, c
.name
);
5692 name
= get_pointer_64(c
.name
+ n_value
, xoffset
, left
, xS
, info
);
5693 if (name
!= nullptr)
5694 outs() << format(" %.*s", left
, name
);
5698 sym_name
= get_symbol_64(offset
+ offsetof(struct category64_t
, cls
), S
, info
,
5701 if (info
->verbose
&& sym_name
!= nullptr)
5704 outs() << format("0x%" PRIx64
, n_value
);
5706 outs() << " + " << format("0x%" PRIx64
, c
.cls
);
5708 outs() << format("0x%" PRIx64
, c
.cls
);
5710 if (c
.cls
+ n_value
!= 0)
5711 print_class64_t(c
.cls
+ n_value
, info
);
5713 outs() << " instanceMethods ";
5715 get_symbol_64(offset
+ offsetof(struct category64_t
, instanceMethods
), S
,
5716 info
, n_value
, c
.instanceMethods
);
5718 if (info
->verbose
&& sym_name
!= nullptr)
5721 outs() << format("0x%" PRIx64
, n_value
);
5722 if (c
.instanceMethods
!= 0)
5723 outs() << " + " << format("0x%" PRIx64
, c
.instanceMethods
);
5725 outs() << format("0x%" PRIx64
, c
.instanceMethods
);
5727 if (c
.instanceMethods
+ n_value
!= 0)
5728 print_method_list64_t(c
.instanceMethods
+ n_value
, info
, "");
5730 outs() << " classMethods ";
5731 sym_name
= get_symbol_64(offset
+ offsetof(struct category64_t
, classMethods
),
5732 S
, info
, n_value
, c
.classMethods
);
5734 if (info
->verbose
&& sym_name
!= nullptr)
5737 outs() << format("0x%" PRIx64
, n_value
);
5738 if (c
.classMethods
!= 0)
5739 outs() << " + " << format("0x%" PRIx64
, c
.classMethods
);
5741 outs() << format("0x%" PRIx64
, c
.classMethods
);
5743 if (c
.classMethods
+ n_value
!= 0)
5744 print_method_list64_t(c
.classMethods
+ n_value
, info
, "");
5746 outs() << " protocols ";
5747 sym_name
= get_symbol_64(offset
+ offsetof(struct category64_t
, protocols
), S
,
5748 info
, n_value
, c
.protocols
);
5750 if (info
->verbose
&& sym_name
!= nullptr)
5753 outs() << format("0x%" PRIx64
, n_value
);
5754 if (c
.protocols
!= 0)
5755 outs() << " + " << format("0x%" PRIx64
, c
.protocols
);
5757 outs() << format("0x%" PRIx64
, c
.protocols
);
5759 if (c
.protocols
+ n_value
!= 0)
5760 print_protocol_list64_t(c
.protocols
+ n_value
, info
);
5762 outs() << "instanceProperties ";
5764 get_symbol_64(offset
+ offsetof(struct category64_t
, instanceProperties
),
5765 S
, info
, n_value
, c
.instanceProperties
);
5767 if (info
->verbose
&& sym_name
!= nullptr)
5770 outs() << format("0x%" PRIx64
, n_value
);
5771 if (c
.instanceProperties
!= 0)
5772 outs() << " + " << format("0x%" PRIx64
, c
.instanceProperties
);
5774 outs() << format("0x%" PRIx64
, c
.instanceProperties
);
5776 if (c
.instanceProperties
+ n_value
!= 0)
5777 print_objc_property_list64(c
.instanceProperties
+ n_value
, info
);
5780 static void print_category32_t(uint32_t p
, struct DisassembleInfo
*info
) {
5781 struct category32_t c
;
5783 uint32_t offset
, left
;
5787 r
= get_pointer_32(p
, offset
, left
, S
, info
);
5790 memset(&c
, '\0', sizeof(struct category32_t
));
5791 if (left
< sizeof(struct category32_t
)) {
5792 memcpy(&c
, r
, left
);
5793 outs() << " (category_t entends past the end of the section)\n";
5795 memcpy(&c
, r
, sizeof(struct category32_t
));
5796 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5799 outs() << " name " << format("0x%" PRIx32
, c
.name
);
5800 name
= get_symbol_32(offset
+ offsetof(struct category32_t
, name
), S
, info
,
5803 outs() << " " << name
;
5806 outs() << " cls " << format("0x%" PRIx32
, c
.cls
) << "\n";
5808 print_class32_t(c
.cls
, info
);
5809 outs() << " instanceMethods " << format("0x%" PRIx32
, c
.instanceMethods
)
5811 if (c
.instanceMethods
!= 0)
5812 print_method_list32_t(c
.instanceMethods
, info
, "");
5813 outs() << " classMethods " << format("0x%" PRIx32
, c
.classMethods
)
5815 if (c
.classMethods
!= 0)
5816 print_method_list32_t(c
.classMethods
, info
, "");
5817 outs() << " protocols " << format("0x%" PRIx32
, c
.protocols
) << "\n";
5818 if (c
.protocols
!= 0)
5819 print_protocol_list32_t(c
.protocols
, info
);
5820 outs() << "instanceProperties " << format("0x%" PRIx32
, c
.instanceProperties
)
5822 if (c
.instanceProperties
!= 0)
5823 print_objc_property_list32(c
.instanceProperties
, info
);
5826 static void print_message_refs64(SectionRef S
, struct DisassembleInfo
*info
) {
5827 uint32_t i
, left
, offset
, xoffset
;
5828 uint64_t p
, n_value
;
5829 struct message_ref64 mr
;
5830 const char *name
, *sym_name
;
5834 if (S
== SectionRef())
5838 Expected
<StringRef
> SecNameOrErr
= S
.getName();
5840 SectName
= *SecNameOrErr
;
5842 consumeError(SecNameOrErr
.takeError());
5844 DataRefImpl Ref
= S
.getRawDataRefImpl();
5845 StringRef SegName
= info
->O
->getSectionFinalSegmentName(Ref
);
5846 outs() << "Contents of (" << SegName
<< "," << SectName
<< ") section\n";
5848 for (i
= 0; i
< S
.getSize(); i
+= sizeof(struct message_ref64
)) {
5849 p
= S
.getAddress() + i
;
5850 r
= get_pointer_64(p
, offset
, left
, S
, info
);
5853 memset(&mr
, '\0', sizeof(struct message_ref64
));
5854 if (left
< sizeof(struct message_ref64
)) {
5855 memcpy(&mr
, r
, left
);
5856 outs() << " (message_ref entends past the end of the section)\n";
5858 memcpy(&mr
, r
, sizeof(struct message_ref64
));
5859 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5863 name
= get_symbol_64(offset
+ offsetof(struct message_ref64
, imp
), S
, info
,
5866 outs() << format("0x%" PRIx64
, n_value
) << " ";
5868 outs() << "+ " << format("0x%" PRIx64
, mr
.imp
) << " ";
5870 outs() << format("0x%" PRIx64
, mr
.imp
) << " ";
5871 if (name
!= nullptr)
5872 outs() << " " << name
;
5876 sym_name
= get_symbol_64(offset
+ offsetof(struct message_ref64
, sel
), S
,
5877 info
, n_value
, mr
.sel
);
5879 if (info
->verbose
&& sym_name
!= nullptr)
5882 outs() << format("0x%" PRIx64
, n_value
);
5884 outs() << " + " << format("0x%" PRIx64
, mr
.sel
);
5886 outs() << format("0x%" PRIx64
, mr
.sel
);
5887 name
= get_pointer_64(mr
.sel
+ n_value
, xoffset
, left
, xS
, info
);
5888 if (name
!= nullptr)
5889 outs() << format(" %.*s", left
, name
);
5892 offset
+= sizeof(struct message_ref64
);
5896 static void print_message_refs32(SectionRef S
, struct DisassembleInfo
*info
) {
5897 uint32_t i
, left
, offset
, xoffset
, p
;
5898 struct message_ref32 mr
;
5899 const char *name
, *r
;
5902 if (S
== SectionRef())
5906 Expected
<StringRef
> SecNameOrErr
= S
.getName();
5908 SectName
= *SecNameOrErr
;
5910 consumeError(SecNameOrErr
.takeError());
5912 DataRefImpl Ref
= S
.getRawDataRefImpl();
5913 StringRef SegName
= info
->O
->getSectionFinalSegmentName(Ref
);
5914 outs() << "Contents of (" << SegName
<< "," << SectName
<< ") section\n";
5916 for (i
= 0; i
< S
.getSize(); i
+= sizeof(struct message_ref64
)) {
5917 p
= S
.getAddress() + i
;
5918 r
= get_pointer_32(p
, offset
, left
, S
, info
);
5921 memset(&mr
, '\0', sizeof(struct message_ref32
));
5922 if (left
< sizeof(struct message_ref32
)) {
5923 memcpy(&mr
, r
, left
);
5924 outs() << " (message_ref entends past the end of the section)\n";
5926 memcpy(&mr
, r
, sizeof(struct message_ref32
));
5927 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5930 outs() << " imp " << format("0x%" PRIx32
, mr
.imp
);
5931 name
= get_symbol_32(offset
+ offsetof(struct message_ref32
, imp
), S
, info
,
5933 if (name
!= nullptr)
5934 outs() << " " << name
;
5937 outs() << " sel " << format("0x%" PRIx32
, mr
.sel
);
5938 name
= get_pointer_32(mr
.sel
, xoffset
, left
, xS
, info
);
5939 if (name
!= nullptr)
5940 outs() << " " << name
;
5943 offset
+= sizeof(struct message_ref32
);
5947 static void print_image_info64(SectionRef S
, struct DisassembleInfo
*info
) {
5948 uint32_t left
, offset
, swift_version
;
5950 struct objc_image_info64 o
;
5953 if (S
== SectionRef())
5957 Expected
<StringRef
> SecNameOrErr
= S
.getName();
5959 SectName
= *SecNameOrErr
;
5961 consumeError(SecNameOrErr
.takeError());
5963 DataRefImpl Ref
= S
.getRawDataRefImpl();
5964 StringRef SegName
= info
->O
->getSectionFinalSegmentName(Ref
);
5965 outs() << "Contents of (" << SegName
<< "," << SectName
<< ") section\n";
5967 r
= get_pointer_64(p
, offset
, left
, S
, info
);
5970 memset(&o
, '\0', sizeof(struct objc_image_info64
));
5971 if (left
< sizeof(struct objc_image_info64
)) {
5972 memcpy(&o
, r
, left
);
5973 outs() << " (objc_image_info entends past the end of the section)\n";
5975 memcpy(&o
, r
, sizeof(struct objc_image_info64
));
5976 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5978 outs() << " version " << o
.version
<< "\n";
5979 outs() << " flags " << format("0x%" PRIx32
, o
.flags
);
5980 if (o
.flags
& OBJC_IMAGE_IS_REPLACEMENT
)
5981 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5982 if (o
.flags
& OBJC_IMAGE_SUPPORTS_GC
)
5983 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5984 if (o
.flags
& OBJC_IMAGE_IS_SIMULATED
)
5985 outs() << " OBJC_IMAGE_IS_SIMULATED";
5986 if (o
.flags
& OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES
)
5987 outs() << " OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES";
5988 swift_version
= (o
.flags
>> 8) & 0xff;
5989 if (swift_version
!= 0) {
5990 if (swift_version
== 1)
5991 outs() << " Swift 1.0";
5992 else if (swift_version
== 2)
5993 outs() << " Swift 1.1";
5994 else if(swift_version
== 3)
5995 outs() << " Swift 2.0";
5996 else if(swift_version
== 4)
5997 outs() << " Swift 3.0";
5998 else if(swift_version
== 5)
5999 outs() << " Swift 4.0";
6000 else if(swift_version
== 6)
6001 outs() << " Swift 4.1/Swift 4.2";
6002 else if(swift_version
== 7)
6003 outs() << " Swift 5 or later";
6005 outs() << " unknown future Swift version (" << swift_version
<< ")";
6010 static void print_image_info32(SectionRef S
, struct DisassembleInfo
*info
) {
6011 uint32_t left
, offset
, swift_version
, p
;
6012 struct objc_image_info32 o
;
6015 if (S
== SectionRef())
6019 Expected
<StringRef
> SecNameOrErr
= S
.getName();
6021 SectName
= *SecNameOrErr
;
6023 consumeError(SecNameOrErr
.takeError());
6025 DataRefImpl Ref
= S
.getRawDataRefImpl();
6026 StringRef SegName
= info
->O
->getSectionFinalSegmentName(Ref
);
6027 outs() << "Contents of (" << SegName
<< "," << SectName
<< ") section\n";
6029 r
= get_pointer_32(p
, offset
, left
, S
, info
);
6032 memset(&o
, '\0', sizeof(struct objc_image_info32
));
6033 if (left
< sizeof(struct objc_image_info32
)) {
6034 memcpy(&o
, r
, left
);
6035 outs() << " (objc_image_info entends past the end of the section)\n";
6037 memcpy(&o
, r
, sizeof(struct objc_image_info32
));
6038 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
6040 outs() << " version " << o
.version
<< "\n";
6041 outs() << " flags " << format("0x%" PRIx32
, o
.flags
);
6042 if (o
.flags
& OBJC_IMAGE_IS_REPLACEMENT
)
6043 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
6044 if (o
.flags
& OBJC_IMAGE_SUPPORTS_GC
)
6045 outs() << " OBJC_IMAGE_SUPPORTS_GC";
6046 swift_version
= (o
.flags
>> 8) & 0xff;
6047 if (swift_version
!= 0) {
6048 if (swift_version
== 1)
6049 outs() << " Swift 1.0";
6050 else if (swift_version
== 2)
6051 outs() << " Swift 1.1";
6052 else if(swift_version
== 3)
6053 outs() << " Swift 2.0";
6054 else if(swift_version
== 4)
6055 outs() << " Swift 3.0";
6056 else if(swift_version
== 5)
6057 outs() << " Swift 4.0";
6058 else if(swift_version
== 6)
6059 outs() << " Swift 4.1/Swift 4.2";
6060 else if(swift_version
== 7)
6061 outs() << " Swift 5 or later";
6063 outs() << " unknown future Swift version (" << swift_version
<< ")";
6068 static void print_image_info(SectionRef S
, struct DisassembleInfo
*info
) {
6069 uint32_t left
, offset
, p
;
6070 struct imageInfo_t o
;
6074 Expected
<StringRef
> SecNameOrErr
= S
.getName();
6076 SectName
= *SecNameOrErr
;
6078 consumeError(SecNameOrErr
.takeError());
6080 DataRefImpl Ref
= S
.getRawDataRefImpl();
6081 StringRef SegName
= info
->O
->getSectionFinalSegmentName(Ref
);
6082 outs() << "Contents of (" << SegName
<< "," << SectName
<< ") section\n";
6084 r
= get_pointer_32(p
, offset
, left
, S
, info
);
6087 memset(&o
, '\0', sizeof(struct imageInfo_t
));
6088 if (left
< sizeof(struct imageInfo_t
)) {
6089 memcpy(&o
, r
, left
);
6090 outs() << " (imageInfo entends past the end of the section)\n";
6092 memcpy(&o
, r
, sizeof(struct imageInfo_t
));
6093 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
6095 outs() << " version " << o
.version
<< "\n";
6096 outs() << " flags " << format("0x%" PRIx32
, o
.flags
);
6102 outs() << " GC-only";
6108 static void printObjc2_64bit_MetaData(MachOObjectFile
*O
, bool verbose
) {
6109 SymbolAddressMap AddrMap
;
6111 CreateSymbolAddressMap(O
, &AddrMap
);
6113 std::vector
<SectionRef
> Sections
;
6114 for (const SectionRef
&Section
: O
->sections())
6115 Sections
.push_back(Section
);
6117 struct DisassembleInfo
info(O
, &AddrMap
, &Sections
, verbose
);
6119 SectionRef CL
= get_section(O
, "__OBJC2", "__class_list");
6120 if (CL
== SectionRef())
6121 CL
= get_section(O
, "__DATA", "__objc_classlist");
6122 if (CL
== SectionRef())
6123 CL
= get_section(O
, "__DATA_CONST", "__objc_classlist");
6124 if (CL
== SectionRef())
6125 CL
= get_section(O
, "__DATA_DIRTY", "__objc_classlist");
6127 walk_pointer_list_64("class", CL
, O
, &info
, print_class64_t
);
6129 SectionRef CR
= get_section(O
, "__OBJC2", "__class_refs");
6130 if (CR
== SectionRef())
6131 CR
= get_section(O
, "__DATA", "__objc_classrefs");
6132 if (CR
== SectionRef())
6133 CR
= get_section(O
, "__DATA_CONST", "__objc_classrefs");
6134 if (CR
== SectionRef())
6135 CR
= get_section(O
, "__DATA_DIRTY", "__objc_classrefs");
6137 walk_pointer_list_64("class refs", CR
, O
, &info
, nullptr);
6139 SectionRef SR
= get_section(O
, "__OBJC2", "__super_refs");
6140 if (SR
== SectionRef())
6141 SR
= get_section(O
, "__DATA", "__objc_superrefs");
6142 if (SR
== SectionRef())
6143 SR
= get_section(O
, "__DATA_CONST", "__objc_superrefs");
6144 if (SR
== SectionRef())
6145 SR
= get_section(O
, "__DATA_DIRTY", "__objc_superrefs");
6147 walk_pointer_list_64("super refs", SR
, O
, &info
, nullptr);
6149 SectionRef CA
= get_section(O
, "__OBJC2", "__category_list");
6150 if (CA
== SectionRef())
6151 CA
= get_section(O
, "__DATA", "__objc_catlist");
6152 if (CA
== SectionRef())
6153 CA
= get_section(O
, "__DATA_CONST", "__objc_catlist");
6154 if (CA
== SectionRef())
6155 CA
= get_section(O
, "__DATA_DIRTY", "__objc_catlist");
6157 walk_pointer_list_64("category", CA
, O
, &info
, print_category64_t
);
6159 SectionRef PL
= get_section(O
, "__OBJC2", "__protocol_list");
6160 if (PL
== SectionRef())
6161 PL
= get_section(O
, "__DATA", "__objc_protolist");
6162 if (PL
== SectionRef())
6163 PL
= get_section(O
, "__DATA_CONST", "__objc_protolist");
6164 if (PL
== SectionRef())
6165 PL
= get_section(O
, "__DATA_DIRTY", "__objc_protolist");
6167 walk_pointer_list_64("protocol", PL
, O
, &info
, nullptr);
6169 SectionRef MR
= get_section(O
, "__OBJC2", "__message_refs");
6170 if (MR
== SectionRef())
6171 MR
= get_section(O
, "__DATA", "__objc_msgrefs");
6172 if (MR
== SectionRef())
6173 MR
= get_section(O
, "__DATA_CONST", "__objc_msgrefs");
6174 if (MR
== SectionRef())
6175 MR
= get_section(O
, "__DATA_DIRTY", "__objc_msgrefs");
6177 print_message_refs64(MR
, &info
);
6179 SectionRef II
= get_section(O
, "__OBJC2", "__image_info");
6180 if (II
== SectionRef())
6181 II
= get_section(O
, "__DATA", "__objc_imageinfo");
6182 if (II
== SectionRef())
6183 II
= get_section(O
, "__DATA_CONST", "__objc_imageinfo");
6184 if (II
== SectionRef())
6185 II
= get_section(O
, "__DATA_DIRTY", "__objc_imageinfo");
6187 print_image_info64(II
, &info
);
6190 static void printObjc2_32bit_MetaData(MachOObjectFile
*O
, bool verbose
) {
6191 SymbolAddressMap AddrMap
;
6193 CreateSymbolAddressMap(O
, &AddrMap
);
6195 std::vector
<SectionRef
> Sections
;
6196 for (const SectionRef
&Section
: O
->sections())
6197 Sections
.push_back(Section
);
6199 struct DisassembleInfo
info(O
, &AddrMap
, &Sections
, verbose
);
6201 SectionRef CL
= get_section(O
, "__OBJC2", "__class_list");
6202 if (CL
== SectionRef())
6203 CL
= get_section(O
, "__DATA", "__objc_classlist");
6204 if (CL
== SectionRef())
6205 CL
= get_section(O
, "__DATA_CONST", "__objc_classlist");
6206 if (CL
== SectionRef())
6207 CL
= get_section(O
, "__DATA_DIRTY", "__objc_classlist");
6209 walk_pointer_list_32("class", CL
, O
, &info
, print_class32_t
);
6211 SectionRef CR
= get_section(O
, "__OBJC2", "__class_refs");
6212 if (CR
== SectionRef())
6213 CR
= get_section(O
, "__DATA", "__objc_classrefs");
6214 if (CR
== SectionRef())
6215 CR
= get_section(O
, "__DATA_CONST", "__objc_classrefs");
6216 if (CR
== SectionRef())
6217 CR
= get_section(O
, "__DATA_DIRTY", "__objc_classrefs");
6219 walk_pointer_list_32("class refs", CR
, O
, &info
, nullptr);
6221 SectionRef SR
= get_section(O
, "__OBJC2", "__super_refs");
6222 if (SR
== SectionRef())
6223 SR
= get_section(O
, "__DATA", "__objc_superrefs");
6224 if (SR
== SectionRef())
6225 SR
= get_section(O
, "__DATA_CONST", "__objc_superrefs");
6226 if (SR
== SectionRef())
6227 SR
= get_section(O
, "__DATA_DIRTY", "__objc_superrefs");
6229 walk_pointer_list_32("super refs", SR
, O
, &info
, nullptr);
6231 SectionRef CA
= get_section(O
, "__OBJC2", "__category_list");
6232 if (CA
== SectionRef())
6233 CA
= get_section(O
, "__DATA", "__objc_catlist");
6234 if (CA
== SectionRef())
6235 CA
= get_section(O
, "__DATA_CONST", "__objc_catlist");
6236 if (CA
== SectionRef())
6237 CA
= get_section(O
, "__DATA_DIRTY", "__objc_catlist");
6239 walk_pointer_list_32("category", CA
, O
, &info
, print_category32_t
);
6241 SectionRef PL
= get_section(O
, "__OBJC2", "__protocol_list");
6242 if (PL
== SectionRef())
6243 PL
= get_section(O
, "__DATA", "__objc_protolist");
6244 if (PL
== SectionRef())
6245 PL
= get_section(O
, "__DATA_CONST", "__objc_protolist");
6246 if (PL
== SectionRef())
6247 PL
= get_section(O
, "__DATA_DIRTY", "__objc_protolist");
6249 walk_pointer_list_32("protocol", PL
, O
, &info
, nullptr);
6251 SectionRef MR
= get_section(O
, "__OBJC2", "__message_refs");
6252 if (MR
== SectionRef())
6253 MR
= get_section(O
, "__DATA", "__objc_msgrefs");
6254 if (MR
== SectionRef())
6255 MR
= get_section(O
, "__DATA_CONST", "__objc_msgrefs");
6256 if (MR
== SectionRef())
6257 MR
= get_section(O
, "__DATA_DIRTY", "__objc_msgrefs");
6259 print_message_refs32(MR
, &info
);
6261 SectionRef II
= get_section(O
, "__OBJC2", "__image_info");
6262 if (II
== SectionRef())
6263 II
= get_section(O
, "__DATA", "__objc_imageinfo");
6264 if (II
== SectionRef())
6265 II
= get_section(O
, "__DATA_CONST", "__objc_imageinfo");
6266 if (II
== SectionRef())
6267 II
= get_section(O
, "__DATA_DIRTY", "__objc_imageinfo");
6269 print_image_info32(II
, &info
);
6272 static bool printObjc1_32bit_MetaData(MachOObjectFile
*O
, bool verbose
) {
6273 uint32_t i
, j
, p
, offset
, xoffset
, left
, defs_left
, def
;
6274 const char *r
, *name
, *defs
;
6275 struct objc_module_t module
;
6277 struct objc_symtab_t symtab
;
6278 struct objc_class_t objc_class
;
6279 struct objc_category_t objc_category
;
6281 outs() << "Objective-C segment\n";
6282 S
= get_section(O
, "__OBJC", "__module_info");
6283 if (S
== SectionRef())
6286 SymbolAddressMap AddrMap
;
6288 CreateSymbolAddressMap(O
, &AddrMap
);
6290 std::vector
<SectionRef
> Sections
;
6291 for (const SectionRef
&Section
: O
->sections())
6292 Sections
.push_back(Section
);
6294 struct DisassembleInfo
info(O
, &AddrMap
, &Sections
, verbose
);
6296 for (i
= 0; i
< S
.getSize(); i
+= sizeof(struct objc_module_t
)) {
6297 p
= S
.getAddress() + i
;
6298 r
= get_pointer_32(p
, offset
, left
, S
, &info
, true);
6301 memset(&module
, '\0', sizeof(struct objc_module_t
));
6302 if (left
< sizeof(struct objc_module_t
)) {
6303 memcpy(&module
, r
, left
);
6304 outs() << " (module extends past end of __module_info section)\n";
6306 memcpy(&module
, r
, sizeof(struct objc_module_t
));
6307 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
6310 outs() << "Module " << format("0x%" PRIx32
, p
) << "\n";
6311 outs() << " version " << module
.version
<< "\n";
6312 outs() << " size " << module
.size
<< "\n";
6314 name
= get_pointer_32(module
.name
, xoffset
, left
, xS
, &info
, true);
6315 if (name
!= nullptr)
6316 outs() << format("%.*s", left
, name
);
6318 outs() << format("0x%08" PRIx32
, module
.name
)
6319 << "(not in an __OBJC section)";
6322 r
= get_pointer_32(module
.symtab
, xoffset
, left
, xS
, &info
, true);
6323 if (module
.symtab
== 0 || r
== nullptr) {
6324 outs() << " symtab " << format("0x%08" PRIx32
, module
.symtab
)
6325 << " (not in an __OBJC section)\n";
6328 outs() << " symtab " << format("0x%08" PRIx32
, module
.symtab
) << "\n";
6329 memset(&symtab
, '\0', sizeof(struct objc_symtab_t
));
6332 if (left
< sizeof(struct objc_symtab_t
)) {
6333 memcpy(&symtab
, r
, left
);
6334 outs() << "\tsymtab extends past end of an __OBJC section)\n";
6336 memcpy(&symtab
, r
, sizeof(struct objc_symtab_t
));
6337 if (left
> sizeof(struct objc_symtab_t
)) {
6338 defs_left
= left
- sizeof(struct objc_symtab_t
);
6339 defs
= r
+ sizeof(struct objc_symtab_t
);
6342 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
6345 outs() << "\tsel_ref_cnt " << symtab
.sel_ref_cnt
<< "\n";
6346 r
= get_pointer_32(symtab
.refs
, xoffset
, left
, xS
, &info
, true);
6347 outs() << "\trefs " << format("0x%08" PRIx32
, symtab
.refs
);
6349 outs() << " (not in an __OBJC section)";
6351 outs() << "\tcls_def_cnt " << symtab
.cls_def_cnt
<< "\n";
6352 outs() << "\tcat_def_cnt " << symtab
.cat_def_cnt
<< "\n";
6353 if (symtab
.cls_def_cnt
> 0)
6354 outs() << "\tClass Definitions\n";
6355 for (j
= 0; j
< symtab
.cls_def_cnt
; j
++) {
6356 if ((j
+ 1) * sizeof(uint32_t) > defs_left
) {
6357 outs() << "\t(remaining class defs entries entends past the end of the "
6361 memcpy(&def
, defs
+ j
* sizeof(uint32_t), sizeof(uint32_t));
6362 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
6363 sys::swapByteOrder(def
);
6365 r
= get_pointer_32(def
, xoffset
, left
, xS
, &info
, true);
6366 outs() << "\tdefs[" << j
<< "] " << format("0x%08" PRIx32
, def
);
6368 if (left
> sizeof(struct objc_class_t
)) {
6370 memcpy(&objc_class
, r
, sizeof(struct objc_class_t
));
6372 outs() << " (entends past the end of the section)\n";
6373 memset(&objc_class
, '\0', sizeof(struct objc_class_t
));
6374 memcpy(&objc_class
, r
, left
);
6376 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
6377 swapStruct(objc_class
);
6378 print_objc_class_t(&objc_class
, &info
);
6380 outs() << "(not in an __OBJC section)\n";
6383 if (CLS_GETINFO(&objc_class
, CLS_CLASS
)) {
6384 outs() << "\tMeta Class";
6385 r
= get_pointer_32(objc_class
.isa
, xoffset
, left
, xS
, &info
, true);
6387 if (left
> sizeof(struct objc_class_t
)) {
6389 memcpy(&objc_class
, r
, sizeof(struct objc_class_t
));
6391 outs() << " (entends past the end of the section)\n";
6392 memset(&objc_class
, '\0', sizeof(struct objc_class_t
));
6393 memcpy(&objc_class
, r
, left
);
6395 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
6396 swapStruct(objc_class
);
6397 print_objc_class_t(&objc_class
, &info
);
6399 outs() << "(not in an __OBJC section)\n";
6403 if (symtab
.cat_def_cnt
> 0)
6404 outs() << "\tCategory Definitions\n";
6405 for (j
= 0; j
< symtab
.cat_def_cnt
; j
++) {
6406 if ((j
+ symtab
.cls_def_cnt
+ 1) * sizeof(uint32_t) > defs_left
) {
6407 outs() << "\t(remaining category defs entries entends past the end of "
6408 << "the section)\n";
6411 memcpy(&def
, defs
+ (j
+ symtab
.cls_def_cnt
) * sizeof(uint32_t),
6413 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
6414 sys::swapByteOrder(def
);
6416 r
= get_pointer_32(def
, xoffset
, left
, xS
, &info
, true);
6417 outs() << "\tdefs[" << j
+ symtab
.cls_def_cnt
<< "] "
6418 << format("0x%08" PRIx32
, def
);
6420 if (left
> sizeof(struct objc_category_t
)) {
6422 memcpy(&objc_category
, r
, sizeof(struct objc_category_t
));
6424 outs() << " (entends past the end of the section)\n";
6425 memset(&objc_category
, '\0', sizeof(struct objc_category_t
));
6426 memcpy(&objc_category
, r
, left
);
6428 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
6429 swapStruct(objc_category
);
6430 print_objc_objc_category_t(&objc_category
, &info
);
6432 outs() << "(not in an __OBJC section)\n";
6436 const SectionRef II
= get_section(O
, "__OBJC", "__image_info");
6437 if (II
!= SectionRef())
6438 print_image_info(II
, &info
);
6443 static void DumpProtocolSection(MachOObjectFile
*O
, const char *sect
,
6444 uint32_t size
, uint32_t addr
) {
6445 SymbolAddressMap AddrMap
;
6446 CreateSymbolAddressMap(O
, &AddrMap
);
6448 std::vector
<SectionRef
> Sections
;
6449 for (const SectionRef
&Section
: O
->sections())
6450 Sections
.push_back(Section
);
6452 struct DisassembleInfo
info(O
, &AddrMap
, &Sections
, true);
6455 struct objc_protocol_t protocol
;
6456 uint32_t left
, paddr
;
6457 for (p
= sect
; p
< sect
+ size
; p
+= sizeof(struct objc_protocol_t
)) {
6458 memset(&protocol
, '\0', sizeof(struct objc_protocol_t
));
6459 left
= size
- (p
- sect
);
6460 if (left
< sizeof(struct objc_protocol_t
)) {
6461 outs() << "Protocol extends past end of __protocol section\n";
6462 memcpy(&protocol
, p
, left
);
6464 memcpy(&protocol
, p
, sizeof(struct objc_protocol_t
));
6465 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
6466 swapStruct(protocol
);
6467 paddr
= addr
+ (p
- sect
);
6468 outs() << "Protocol " << format("0x%" PRIx32
, paddr
);
6469 if (print_protocol(paddr
, 0, &info
))
6470 outs() << "(not in an __OBJC section)\n";
6475 inline void swapStruct(struct xar_header
&xar
) {
6476 sys::swapByteOrder(xar
.magic
);
6477 sys::swapByteOrder(xar
.size
);
6478 sys::swapByteOrder(xar
.version
);
6479 sys::swapByteOrder(xar
.toc_length_compressed
);
6480 sys::swapByteOrder(xar
.toc_length_uncompressed
);
6481 sys::swapByteOrder(xar
.cksum_alg
);
6484 static void PrintModeVerbose(uint32_t mode
) {
6485 switch(mode
& S_IFMT
){
6509 /* owner permissions */
6520 else if(mode
& S_IEXEC
)
6525 /* group permissions */
6526 if(mode
& (S_IREAD
>> 3))
6530 if(mode
& (S_IWRITE
>> 3))
6536 else if(mode
& (S_IEXEC
>> 3))
6541 /* other permissions */
6542 if(mode
& (S_IREAD
>> 6))
6546 if(mode
& (S_IWRITE
>> 6))
6552 else if(mode
& (S_IEXEC
>> 6))
6558 static void PrintXarFilesSummary(const char *XarFilename
, xar_t xar
) {
6560 const char *key
, *type
, *mode
, *user
, *group
, *size
, *mtime
, *name
, *m
;
6562 uint32_t mode_value
;
6566 WithColor::error(errs(), "llvm-objdump")
6567 << "can't obtain an xar iterator for xar archive " << XarFilename
6572 // Go through the xar's files.
6573 for (xf
= xar_file_first(xar
, xi
); xf
; xf
= xar_file_next(xi
)) {
6576 WithColor::error(errs(), "llvm-objdump")
6577 << "can't obtain an xar iterator for xar archive " << XarFilename
6588 for(key
= xar_prop_first(xf
, xp
); key
; key
= xar_prop_next(xp
)){
6589 const char *val
= nullptr;
6590 xar_prop_get(xf
, key
, &val
);
6591 #if 0 // Useful for debugging.
6592 outs() << "key: " << key
<< " value: " << val
<< "\n";
6594 if(strcmp(key
, "type") == 0)
6596 if(strcmp(key
, "mode") == 0)
6598 if(strcmp(key
, "user") == 0)
6600 if(strcmp(key
, "group") == 0)
6602 if(strcmp(key
, "data/size") == 0)
6604 if(strcmp(key
, "mtime") == 0)
6606 if(strcmp(key
, "name") == 0)
6609 if(mode
!= nullptr){
6610 mode_value
= strtoul(mode
, &endp
, 8);
6612 outs() << "(mode: \"" << mode
<< "\" contains non-octal chars) ";
6613 if(strcmp(type
, "file") == 0)
6614 mode_value
|= S_IFREG
;
6615 PrintModeVerbose(mode_value
);
6619 outs() << format("%10s/", user
);
6620 if(group
!= nullptr)
6621 outs() << format("%-10s ", group
);
6623 outs() << format("%7s ", size
);
6624 if(mtime
!= nullptr){
6625 for(m
= mtime
; *m
!= 'T' && *m
!= '\0'; m
++)
6630 for( ; *m
!= 'Z' && *m
!= '\0'; m
++)
6640 static void DumpBitcodeSection(MachOObjectFile
*O
, const char *sect
,
6641 uint32_t size
, bool verbose
,
6642 bool PrintXarHeader
, bool PrintXarFileHeaders
,
6643 std::string XarMemberName
) {
6644 if(size
< sizeof(struct xar_header
)) {
6645 outs() << "size of (__LLVM,__bundle) section too small (smaller than size "
6646 "of struct xar_header)\n";
6649 struct xar_header XarHeader
;
6650 memcpy(&XarHeader
, sect
, sizeof(struct xar_header
));
6651 if (sys::IsLittleEndianHost
)
6652 swapStruct(XarHeader
);
6653 if (PrintXarHeader
) {
6654 if (!XarMemberName
.empty())
6655 outs() << "In xar member " << XarMemberName
<< ": ";
6657 outs() << "For (__LLVM,__bundle) section: ";
6658 outs() << "xar header\n";
6659 if (XarHeader
.magic
== XAR_HEADER_MAGIC
)
6660 outs() << " magic XAR_HEADER_MAGIC\n";
6663 << format_hex(XarHeader
.magic
, 10, true)
6664 << " (not XAR_HEADER_MAGIC)\n";
6665 outs() << " size " << XarHeader
.size
<< "\n";
6666 outs() << " version " << XarHeader
.version
<< "\n";
6667 outs() << " toc_length_compressed " << XarHeader
.toc_length_compressed
6669 outs() << "toc_length_uncompressed " << XarHeader
.toc_length_uncompressed
6671 outs() << " cksum_alg ";
6672 switch (XarHeader
.cksum_alg
) {
6673 case XAR_CKSUM_NONE
:
6674 outs() << "XAR_CKSUM_NONE\n";
6676 case XAR_CKSUM_SHA1
:
6677 outs() << "XAR_CKSUM_SHA1\n";
6680 outs() << "XAR_CKSUM_MD5\n";
6682 #ifdef XAR_CKSUM_SHA256
6683 case XAR_CKSUM_SHA256
:
6684 outs() << "XAR_CKSUM_SHA256\n";
6687 #ifdef XAR_CKSUM_SHA512
6688 case XAR_CKSUM_SHA512
:
6689 outs() << "XAR_CKSUM_SHA512\n";
6693 outs() << XarHeader
.cksum_alg
<< "\n";
6697 SmallString
<128> XarFilename
;
6699 std::error_code XarEC
=
6700 sys::fs::createTemporaryFile("llvm-objdump", "xar", FD
, XarFilename
);
6702 WithColor::error(errs(), "llvm-objdump") << XarEC
.message() << "\n";
6705 ToolOutputFile
XarFile(XarFilename
, FD
);
6706 raw_fd_ostream
&XarOut
= XarFile
.os();
6707 StringRef
XarContents(sect
, size
);
6708 XarOut
<< XarContents
;
6710 if (XarOut
.has_error())
6713 ScopedXarFile
xar(XarFilename
.c_str(), READ
);
6715 WithColor::error(errs(), "llvm-objdump")
6716 << "can't create temporary xar archive " << XarFilename
<< "\n";
6720 SmallString
<128> TocFilename
;
6721 std::error_code TocEC
=
6722 sys::fs::createTemporaryFile("llvm-objdump", "toc", TocFilename
);
6724 WithColor::error(errs(), "llvm-objdump") << TocEC
.message() << "\n";
6727 xar_serialize(xar
, TocFilename
.c_str());
6729 if (PrintXarFileHeaders
) {
6730 if (!XarMemberName
.empty())
6731 outs() << "In xar member " << XarMemberName
<< ": ";
6733 outs() << "For (__LLVM,__bundle) section: ";
6734 outs() << "xar archive files:\n";
6735 PrintXarFilesSummary(XarFilename
.c_str(), xar
);
6738 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> FileOrErr
=
6739 MemoryBuffer::getFileOrSTDIN(TocFilename
.c_str());
6740 if (std::error_code EC
= FileOrErr
.getError()) {
6741 WithColor::error(errs(), "llvm-objdump") << EC
.message() << "\n";
6744 std::unique_ptr
<MemoryBuffer
> &Buffer
= FileOrErr
.get();
6746 if (!XarMemberName
.empty())
6747 outs() << "In xar member " << XarMemberName
<< ": ";
6749 outs() << "For (__LLVM,__bundle) section: ";
6750 outs() << "xar table of contents:\n";
6751 outs() << Buffer
->getBuffer() << "\n";
6753 // TODO: Go through the xar's files.
6756 WithColor::error(errs(), "llvm-objdump")
6757 << "can't obtain an xar iterator for xar archive "
6758 << XarFilename
.c_str() << "\n";
6761 for(xar_file_t xf
= xar_file_first(xar
, xi
); xf
; xf
= xar_file_next(xi
)){
6763 const char *member_name
, *member_type
, *member_size_string
;
6768 WithColor::error(errs(), "llvm-objdump")
6769 << "can't obtain an xar iterator for xar archive "
6770 << XarFilename
.c_str() << "\n";
6775 member_size_string
= NULL
;
6776 for(key
= xar_prop_first(xf
, xp
); key
; key
= xar_prop_next(xp
)){
6777 const char *val
= nullptr;
6778 xar_prop_get(xf
, key
, &val
);
6779 #if 0 // Useful for debugging.
6780 outs() << "key: " << key
<< " value: " << val
<< "\n";
6782 if (strcmp(key
, "name") == 0)
6784 if (strcmp(key
, "type") == 0)
6786 if (strcmp(key
, "data/size") == 0)
6787 member_size_string
= val
;
6790 * If we find a file with a name, date/size and type properties
6791 * and with the type being "file" see if that is a xar file.
6793 if (member_name
!= NULL
&& member_type
!= NULL
&&
6794 strcmp(member_type
, "file") == 0 &&
6795 member_size_string
!= NULL
){
6796 // Extract the file into a buffer.
6798 member_size
= strtoul(member_size_string
, &endptr
, 10);
6799 if (*endptr
== '\0' && member_size
!= 0) {
6801 if (xar_extract_tobuffersz(xar
, xf
, &buffer
, &member_size
) == 0) {
6802 #if 0 // Useful for debugging.
6803 outs() << "xar member: " << member_name
<< " extracted\n";
6805 // Set the XarMemberName we want to see printed in the header.
6806 std::string OldXarMemberName
;
6807 // If XarMemberName is already set this is nested. So
6808 // save the old name and create the nested name.
6809 if (!XarMemberName
.empty()) {
6810 OldXarMemberName
= XarMemberName
;
6812 (Twine("[") + XarMemberName
+ "]" + member_name
).str();
6814 OldXarMemberName
= "";
6815 XarMemberName
= member_name
;
6817 // See if this is could be a xar file (nested).
6818 if (member_size
>= sizeof(struct xar_header
)) {
6819 #if 0 // Useful for debugging.
6820 outs() << "could be a xar file: " << member_name
<< "\n";
6822 memcpy((char *)&XarHeader
, buffer
, sizeof(struct xar_header
));
6823 if (sys::IsLittleEndianHost
)
6824 swapStruct(XarHeader
);
6825 if (XarHeader
.magic
== XAR_HEADER_MAGIC
)
6826 DumpBitcodeSection(O
, buffer
, member_size
, verbose
,
6827 PrintXarHeader
, PrintXarFileHeaders
,
6830 XarMemberName
= OldXarMemberName
;
6837 #endif // defined(HAVE_LIBXAR)
6839 static void printObjcMetaData(MachOObjectFile
*O
, bool verbose
) {
6841 printObjc2_64bit_MetaData(O
, verbose
);
6843 MachO::mach_header H
;
6845 if (H
.cputype
== MachO::CPU_TYPE_ARM
)
6846 printObjc2_32bit_MetaData(O
, verbose
);
6848 // This is the 32-bit non-arm cputype case. Which is normally
6849 // the first Objective-C ABI. But it may be the case of a
6850 // binary for the iOS simulator which is the second Objective-C
6851 // ABI. In that case printObjc1_32bit_MetaData() will determine that
6852 // and return false.
6853 if (!printObjc1_32bit_MetaData(O
, verbose
))
6854 printObjc2_32bit_MetaData(O
, verbose
);
6859 // GuessLiteralPointer returns a string which for the item in the Mach-O file
6860 // for the address passed in as ReferenceValue for printing as a comment with
6861 // the instruction and also returns the corresponding type of that item
6862 // indirectly through ReferenceType.
6864 // If ReferenceValue is an address of literal cstring then a pointer to the
6865 // cstring is returned and ReferenceType is set to
6866 // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
6868 // If ReferenceValue is an address of an Objective-C CFString, Selector ref or
6869 // Class ref that name is returned and the ReferenceType is set accordingly.
6871 // Lastly, literals which are Symbol address in a literal pool are looked for
6872 // and if found the symbol name is returned and ReferenceType is set to
6873 // LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
6875 // If there is no item in the Mach-O file for the address passed in as
6876 // ReferenceValue nullptr is returned and ReferenceType is unchanged.
6877 static const char *GuessLiteralPointer(uint64_t ReferenceValue
,
6878 uint64_t ReferencePC
,
6879 uint64_t *ReferenceType
,
6880 struct DisassembleInfo
*info
) {
6881 // First see if there is an external relocation entry at the ReferencePC.
6882 if (info
->O
->getHeader().filetype
== MachO::MH_OBJECT
) {
6883 uint64_t sect_addr
= info
->S
.getAddress();
6884 uint64_t sect_offset
= ReferencePC
- sect_addr
;
6885 bool reloc_found
= false;
6887 MachO::any_relocation_info RE
;
6888 bool isExtern
= false;
6890 for (const RelocationRef
&Reloc
: info
->S
.relocations()) {
6891 uint64_t RelocOffset
= Reloc
.getOffset();
6892 if (RelocOffset
== sect_offset
) {
6893 Rel
= Reloc
.getRawDataRefImpl();
6894 RE
= info
->O
->getRelocation(Rel
);
6895 if (info
->O
->isRelocationScattered(RE
))
6897 isExtern
= info
->O
->getPlainRelocationExternal(RE
);
6899 symbol_iterator RelocSym
= Reloc
.getSymbol();
6906 // If there is an external relocation entry for a symbol in a section
6907 // then used that symbol's value for the value of the reference.
6908 if (reloc_found
&& isExtern
) {
6909 if (info
->O
->getAnyRelocationPCRel(RE
)) {
6910 unsigned Type
= info
->O
->getAnyRelocationType(RE
);
6911 if (Type
== MachO::X86_64_RELOC_SIGNED
) {
6912 ReferenceValue
= Symbol
.getValue();
6918 // Look for literals such as Objective-C CFStrings refs, Selector refs,
6919 // Message refs and Class refs.
6920 bool classref
, selref
, msgref
, cfstring
;
6921 uint64_t pointer_value
= GuessPointerPointer(ReferenceValue
, info
, classref
,
6922 selref
, msgref
, cfstring
);
6923 if (classref
&& pointer_value
== 0) {
6924 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
6925 // And the pointer_value in that section is typically zero as it will be
6926 // set by dyld as part of the "bind information".
6927 const char *name
= get_dyld_bind_info_symbolname(ReferenceValue
, info
);
6928 if (name
!= nullptr) {
6929 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref
;
6930 const char *class_name
= strrchr(name
, '$');
6931 if (class_name
!= nullptr && class_name
[1] == '_' &&
6932 class_name
[2] != '\0') {
6933 info
->class_name
= class_name
+ 2;
6940 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref
;
6942 get_objc2_64bit_class_name(pointer_value
, ReferenceValue
, info
);
6943 if (name
!= nullptr)
6944 info
->class_name
= name
;
6946 name
= "bad class ref";
6951 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref
;
6952 const char *name
= get_objc2_64bit_cfstring_name(ReferenceValue
, info
);
6956 if (selref
&& pointer_value
== 0)
6957 pointer_value
= get_objc2_64bit_selref(ReferenceValue
, info
);
6959 if (pointer_value
!= 0)
6960 ReferenceValue
= pointer_value
;
6962 const char *name
= GuessCstringPointer(ReferenceValue
, info
);
6964 if (pointer_value
!= 0 && selref
) {
6965 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref
;
6966 info
->selector_name
= name
;
6967 } else if (pointer_value
!= 0 && msgref
) {
6968 info
->class_name
= nullptr;
6969 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref
;
6970 info
->selector_name
= name
;
6972 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr
;
6976 // Lastly look for an indirect symbol with this ReferenceValue which is in
6977 // a literal pool. If found return that symbol name.
6978 name
= GuessIndirectSymbol(ReferenceValue
, info
);
6980 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr
;
6987 // SymbolizerSymbolLookUp is the symbol lookup function passed when creating
6988 // the Symbolizer. It looks up the ReferenceValue using the info passed via the
6989 // pointer to the struct DisassembleInfo that was passed when MCSymbolizer
6990 // is created and returns the symbol name that matches the ReferenceValue or
6991 // nullptr if none. The ReferenceType is passed in for the IN type of
6992 // reference the instruction is making from the values in defined in the header
6993 // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
6994 // Out type and the ReferenceName will also be set which is added as a comment
6995 // to the disassembled instruction.
6997 // If the symbol name is a C++ mangled name then the demangled name is
6998 // returned through ReferenceName and ReferenceType is set to
6999 // LLVMDisassembler_ReferenceType_DeMangled_Name .
7001 // When this is called to get a symbol name for a branch target then the
7002 // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
7003 // SymbolValue will be looked for in the indirect symbol table to determine if
7004 // it is an address for a symbol stub. If so then the symbol name for that
7005 // stub is returned indirectly through ReferenceName and then ReferenceType is
7006 // set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
7008 // When this is called with an value loaded via a PC relative load then
7009 // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
7010 // SymbolValue is checked to be an address of literal pointer, symbol pointer,
7011 // or an Objective-C meta data reference. If so the output ReferenceType is
7012 // set to correspond to that as well as setting the ReferenceName.
7013 static const char *SymbolizerSymbolLookUp(void *DisInfo
,
7014 uint64_t ReferenceValue
,
7015 uint64_t *ReferenceType
,
7016 uint64_t ReferencePC
,
7017 const char **ReferenceName
) {
7018 struct DisassembleInfo
*info
= (struct DisassembleInfo
*)DisInfo
;
7019 // If no verbose symbolic information is wanted then just return nullptr.
7020 if (!info
->verbose
) {
7021 *ReferenceName
= nullptr;
7022 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
7026 const char *SymbolName
= GuessSymbolName(ReferenceValue
, info
->AddrMap
);
7028 if (*ReferenceType
== LLVMDisassembler_ReferenceType_In_Branch
) {
7029 *ReferenceName
= GuessIndirectSymbol(ReferenceValue
, info
);
7030 if (*ReferenceName
!= nullptr) {
7031 method_reference(info
, ReferenceType
, ReferenceName
);
7032 if (*ReferenceType
!= LLVMDisassembler_ReferenceType_Out_Objc_Message
)
7033 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_SymbolStub
;
7034 } else if (SymbolName
!= nullptr && strncmp(SymbolName
, "__Z", 3) == 0) {
7035 if (info
->demangled_name
!= nullptr)
7036 free(info
->demangled_name
);
7038 info
->demangled_name
=
7039 itaniumDemangle(SymbolName
+ 1, nullptr, nullptr, &status
);
7040 if (info
->demangled_name
!= nullptr) {
7041 *ReferenceName
= info
->demangled_name
;
7042 *ReferenceType
= LLVMDisassembler_ReferenceType_DeMangled_Name
;
7044 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
7046 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
7047 } else if (*ReferenceType
== LLVMDisassembler_ReferenceType_In_PCrel_Load
) {
7049 GuessLiteralPointer(ReferenceValue
, ReferencePC
, ReferenceType
, info
);
7051 method_reference(info
, ReferenceType
, ReferenceName
);
7053 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
7054 // If this is arm64 and the reference is an adrp instruction save the
7055 // instruction, passed in ReferenceValue and the address of the instruction
7056 // for use later if we see and add immediate instruction.
7057 } else if (info
->O
->getArch() == Triple::aarch64
&&
7058 *ReferenceType
== LLVMDisassembler_ReferenceType_In_ARM64_ADRP
) {
7059 info
->adrp_inst
= ReferenceValue
;
7060 info
->adrp_addr
= ReferencePC
;
7061 SymbolName
= nullptr;
7062 *ReferenceName
= nullptr;
7063 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
7064 // If this is arm64 and reference is an add immediate instruction and we
7066 // seen an adrp instruction just before it and the adrp's Xd register
7068 // this add's Xn register reconstruct the value being referenced and look to
7069 // see if it is a literal pointer. Note the add immediate instruction is
7070 // passed in ReferenceValue.
7071 } else if (info
->O
->getArch() == Triple::aarch64
&&
7072 *ReferenceType
== LLVMDisassembler_ReferenceType_In_ARM64_ADDXri
&&
7073 ReferencePC
- 4 == info
->adrp_addr
&&
7074 (info
->adrp_inst
& 0x9f000000) == 0x90000000 &&
7075 (info
->adrp_inst
& 0x1f) == ((ReferenceValue
>> 5) & 0x1f)) {
7076 uint32_t addxri_inst
;
7077 uint64_t adrp_imm
, addxri_imm
;
7080 ((info
->adrp_inst
& 0x00ffffe0) >> 3) | ((info
->adrp_inst
>> 29) & 0x3);
7081 if (info
->adrp_inst
& 0x0200000)
7082 adrp_imm
|= 0xfffffffffc000000LL
;
7084 addxri_inst
= ReferenceValue
;
7085 addxri_imm
= (addxri_inst
>> 10) & 0xfff;
7086 if (((addxri_inst
>> 22) & 0x3) == 1)
7089 ReferenceValue
= (info
->adrp_addr
& 0xfffffffffffff000LL
) +
7090 (adrp_imm
<< 12) + addxri_imm
;
7093 GuessLiteralPointer(ReferenceValue
, ReferencePC
, ReferenceType
, info
);
7094 if (*ReferenceName
== nullptr)
7095 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
7096 // If this is arm64 and the reference is a load register instruction and we
7097 // have seen an adrp instruction just before it and the adrp's Xd register
7098 // matches this add's Xn register reconstruct the value being referenced and
7099 // look to see if it is a literal pointer. Note the load register
7100 // instruction is passed in ReferenceValue.
7101 } else if (info
->O
->getArch() == Triple::aarch64
&&
7102 *ReferenceType
== LLVMDisassembler_ReferenceType_In_ARM64_LDRXui
&&
7103 ReferencePC
- 4 == info
->adrp_addr
&&
7104 (info
->adrp_inst
& 0x9f000000) == 0x90000000 &&
7105 (info
->adrp_inst
& 0x1f) == ((ReferenceValue
>> 5) & 0x1f)) {
7106 uint32_t ldrxui_inst
;
7107 uint64_t adrp_imm
, ldrxui_imm
;
7110 ((info
->adrp_inst
& 0x00ffffe0) >> 3) | ((info
->adrp_inst
>> 29) & 0x3);
7111 if (info
->adrp_inst
& 0x0200000)
7112 adrp_imm
|= 0xfffffffffc000000LL
;
7114 ldrxui_inst
= ReferenceValue
;
7115 ldrxui_imm
= (ldrxui_inst
>> 10) & 0xfff;
7117 ReferenceValue
= (info
->adrp_addr
& 0xfffffffffffff000LL
) +
7118 (adrp_imm
<< 12) + (ldrxui_imm
<< 3);
7121 GuessLiteralPointer(ReferenceValue
, ReferencePC
, ReferenceType
, info
);
7122 if (*ReferenceName
== nullptr)
7123 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
7125 // If this arm64 and is an load register (PC-relative) instruction the
7126 // ReferenceValue is the PC plus the immediate value.
7127 else if (info
->O
->getArch() == Triple::aarch64
&&
7128 (*ReferenceType
== LLVMDisassembler_ReferenceType_In_ARM64_LDRXl
||
7129 *ReferenceType
== LLVMDisassembler_ReferenceType_In_ARM64_ADR
)) {
7131 GuessLiteralPointer(ReferenceValue
, ReferencePC
, ReferenceType
, info
);
7132 if (*ReferenceName
== nullptr)
7133 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
7134 } else if (SymbolName
!= nullptr && strncmp(SymbolName
, "__Z", 3) == 0) {
7135 if (info
->demangled_name
!= nullptr)
7136 free(info
->demangled_name
);
7138 info
->demangled_name
=
7139 itaniumDemangle(SymbolName
+ 1, nullptr, nullptr, &status
);
7140 if (info
->demangled_name
!= nullptr) {
7141 *ReferenceName
= info
->demangled_name
;
7142 *ReferenceType
= LLVMDisassembler_ReferenceType_DeMangled_Name
;
7146 *ReferenceName
= nullptr;
7147 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
7153 /// Emits the comments that are stored in the CommentStream.
7154 /// Each comment in the CommentStream must end with a newline.
7155 static void emitComments(raw_svector_ostream
&CommentStream
,
7156 SmallString
<128> &CommentsToEmit
,
7157 formatted_raw_ostream
&FormattedOS
,
7158 const MCAsmInfo
&MAI
) {
7159 // Flush the stream before taking its content.
7160 StringRef Comments
= CommentsToEmit
.str();
7161 // Get the default information for printing a comment.
7162 StringRef CommentBegin
= MAI
.getCommentString();
7163 unsigned CommentColumn
= MAI
.getCommentColumn();
7164 bool IsFirst
= true;
7165 while (!Comments
.empty()) {
7167 FormattedOS
<< '\n';
7168 // Emit a line of comments.
7169 FormattedOS
.PadToColumn(CommentColumn
);
7170 size_t Position
= Comments
.find('\n');
7171 FormattedOS
<< CommentBegin
<< ' ' << Comments
.substr(0, Position
);
7172 // Move after the newline character.
7173 Comments
= Comments
.substr(Position
+ 1);
7176 FormattedOS
.flush();
7178 // Tell the comment stream that the vector changed underneath it.
7179 CommentsToEmit
.clear();
7182 static void DisassembleMachO(StringRef Filename
, MachOObjectFile
*MachOOF
,
7183 StringRef DisSegName
, StringRef DisSectName
) {
7184 const char *McpuDefault
= nullptr;
7185 const Target
*ThumbTarget
= nullptr;
7186 const Target
*TheTarget
= GetTarget(MachOOF
, &McpuDefault
, &ThumbTarget
);
7188 // GetTarget prints out stuff.
7191 std::string MachOMCPU
;
7192 if (MCPU
.empty() && McpuDefault
)
7193 MachOMCPU
= McpuDefault
;
7197 std::unique_ptr
<const MCInstrInfo
> InstrInfo(TheTarget
->createMCInstrInfo());
7198 std::unique_ptr
<const MCInstrInfo
> ThumbInstrInfo
;
7200 ThumbInstrInfo
.reset(ThumbTarget
->createMCInstrInfo());
7202 // Package up features to be passed to target/subtarget
7203 std::string FeaturesStr
;
7204 if (!MAttrs
.empty()) {
7205 SubtargetFeatures Features
;
7206 for (unsigned i
= 0; i
!= MAttrs
.size(); ++i
)
7207 Features
.AddFeature(MAttrs
[i
]);
7208 FeaturesStr
= Features
.getString();
7211 // Set up disassembler.
7212 std::unique_ptr
<const MCRegisterInfo
> MRI(
7213 TheTarget
->createMCRegInfo(TripleName
));
7214 std::unique_ptr
<const MCAsmInfo
> AsmInfo(
7215 TheTarget
->createMCAsmInfo(*MRI
, TripleName
));
7216 std::unique_ptr
<const MCSubtargetInfo
> STI(
7217 TheTarget
->createMCSubtargetInfo(TripleName
, MachOMCPU
, FeaturesStr
));
7218 MCContext
Ctx(AsmInfo
.get(), MRI
.get(), nullptr);
7219 std::unique_ptr
<MCDisassembler
> DisAsm(
7220 TheTarget
->createMCDisassembler(*STI
, Ctx
));
7221 std::unique_ptr
<MCSymbolizer
> Symbolizer
;
7222 struct DisassembleInfo
SymbolizerInfo(nullptr, nullptr, nullptr, false);
7223 std::unique_ptr
<MCRelocationInfo
> RelInfo(
7224 TheTarget
->createMCRelocationInfo(TripleName
, Ctx
));
7226 Symbolizer
.reset(TheTarget
->createMCSymbolizer(
7227 TripleName
, SymbolizerGetOpInfo
, SymbolizerSymbolLookUp
,
7228 &SymbolizerInfo
, &Ctx
, std::move(RelInfo
)));
7229 DisAsm
->setSymbolizer(std::move(Symbolizer
));
7231 int AsmPrinterVariant
= AsmInfo
->getAssemblerDialect();
7232 std::unique_ptr
<MCInstPrinter
> IP(TheTarget
->createMCInstPrinter(
7233 Triple(TripleName
), AsmPrinterVariant
, *AsmInfo
, *InstrInfo
, *MRI
));
7234 // Set the display preference for hex vs. decimal immediates.
7235 IP
->setPrintImmHex(PrintImmHex
);
7236 // Comment stream and backing vector.
7237 SmallString
<128> CommentsToEmit
;
7238 raw_svector_ostream
CommentStream(CommentsToEmit
);
7239 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
7240 // if it is done then arm64 comments for string literals don't get printed
7241 // and some constant get printed instead and not setting it causes intel
7242 // (32-bit and 64-bit) comments printed with different spacing before the
7243 // comment causing different diffs with the 'C' disassembler library API.
7244 // IP->setCommentStream(CommentStream);
7246 if (!AsmInfo
|| !STI
|| !DisAsm
|| !IP
) {
7247 WithColor::error(errs(), "llvm-objdump")
7248 << "couldn't initialize disassembler for target " << TripleName
<< '\n';
7252 // Set up separate thumb disassembler if needed.
7253 std::unique_ptr
<const MCRegisterInfo
> ThumbMRI
;
7254 std::unique_ptr
<const MCAsmInfo
> ThumbAsmInfo
;
7255 std::unique_ptr
<const MCSubtargetInfo
> ThumbSTI
;
7256 std::unique_ptr
<MCDisassembler
> ThumbDisAsm
;
7257 std::unique_ptr
<MCInstPrinter
> ThumbIP
;
7258 std::unique_ptr
<MCContext
> ThumbCtx
;
7259 std::unique_ptr
<MCSymbolizer
> ThumbSymbolizer
;
7260 struct DisassembleInfo
ThumbSymbolizerInfo(nullptr, nullptr, nullptr, false);
7261 std::unique_ptr
<MCRelocationInfo
> ThumbRelInfo
;
7263 ThumbMRI
.reset(ThumbTarget
->createMCRegInfo(ThumbTripleName
));
7265 ThumbTarget
->createMCAsmInfo(*ThumbMRI
, ThumbTripleName
));
7267 ThumbTarget
->createMCSubtargetInfo(ThumbTripleName
, MachOMCPU
,
7269 ThumbCtx
.reset(new MCContext(ThumbAsmInfo
.get(), ThumbMRI
.get(), nullptr));
7270 ThumbDisAsm
.reset(ThumbTarget
->createMCDisassembler(*ThumbSTI
, *ThumbCtx
));
7271 MCContext
*PtrThumbCtx
= ThumbCtx
.get();
7273 ThumbTarget
->createMCRelocationInfo(ThumbTripleName
, *PtrThumbCtx
));
7275 ThumbSymbolizer
.reset(ThumbTarget
->createMCSymbolizer(
7276 ThumbTripleName
, SymbolizerGetOpInfo
, SymbolizerSymbolLookUp
,
7277 &ThumbSymbolizerInfo
, PtrThumbCtx
, std::move(ThumbRelInfo
)));
7278 ThumbDisAsm
->setSymbolizer(std::move(ThumbSymbolizer
));
7280 int ThumbAsmPrinterVariant
= ThumbAsmInfo
->getAssemblerDialect();
7281 ThumbIP
.reset(ThumbTarget
->createMCInstPrinter(
7282 Triple(ThumbTripleName
), ThumbAsmPrinterVariant
, *ThumbAsmInfo
,
7283 *ThumbInstrInfo
, *ThumbMRI
));
7284 // Set the display preference for hex vs. decimal immediates.
7285 ThumbIP
->setPrintImmHex(PrintImmHex
);
7288 if (ThumbTarget
&& (!ThumbAsmInfo
|| !ThumbSTI
|| !ThumbDisAsm
|| !ThumbIP
)) {
7289 WithColor::error(errs(), "llvm-objdump")
7290 << "couldn't initialize disassembler for target " << ThumbTripleName
7295 MachO::mach_header Header
= MachOOF
->getHeader();
7297 // FIXME: Using the -cfg command line option, this code used to be able to
7298 // annotate relocations with the referenced symbol's name, and if this was
7299 // inside a __[cf]string section, the data it points to. This is now replaced
7300 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
7301 std::vector
<SectionRef
> Sections
;
7302 std::vector
<SymbolRef
> Symbols
;
7303 SmallVector
<uint64_t, 8> FoundFns
;
7304 uint64_t BaseSegmentAddress
= 0;
7306 getSectionsAndSymbols(MachOOF
, Sections
, Symbols
, FoundFns
,
7307 BaseSegmentAddress
);
7309 // Sort the symbols by address, just in case they didn't come in that way.
7310 llvm::sort(Symbols
, SymbolSorter());
7312 // Build a data in code table that is sorted on by the address of each entry.
7313 uint64_t BaseAddress
= 0;
7314 if (Header
.filetype
== MachO::MH_OBJECT
)
7315 BaseAddress
= Sections
[0].getAddress();
7317 BaseAddress
= BaseSegmentAddress
;
7319 for (dice_iterator DI
= MachOOF
->begin_dices(), DE
= MachOOF
->end_dices();
7322 DI
->getOffset(Offset
);
7323 Dices
.push_back(std::make_pair(BaseAddress
+ Offset
, *DI
));
7325 array_pod_sort(Dices
.begin(), Dices
.end());
7328 raw_ostream
&DebugOut
= DebugFlag
? dbgs() : nulls();
7330 raw_ostream
&DebugOut
= nulls();
7333 // Try to find debug info and set up the DIContext for it.
7334 std::unique_ptr
<DIContext
> diContext
;
7335 std::unique_ptr
<Binary
> DSYMBinary
;
7336 std::unique_ptr
<MemoryBuffer
> DSYMBuf
;
7338 ObjectFile
*DbgObj
= MachOOF
;
7340 // A separate DSym file path was specified, parse it as a macho file,
7341 // get the sections and supply it to the section name parsing machinery.
7342 if (!DSYMFile
.empty()) {
7343 std::string
DSYMPath(DSYMFile
);
7345 // If DSYMPath is a .dSYM directory, append the Mach-O file.
7346 if (llvm::sys::fs::is_directory(DSYMPath
) &&
7347 llvm::sys::path::extension(DSYMPath
) == ".dSYM") {
7348 SmallString
<128> ShortName(llvm::sys::path::filename(DSYMPath
));
7349 llvm::sys::path::replace_extension(ShortName
, "");
7350 SmallString
<1024> FullPath(DSYMPath
);
7351 llvm::sys::path::append(FullPath
, "Contents", "Resources", "DWARF",
7353 DSYMPath
= FullPath
.str();
7357 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> BufOrErr
=
7358 MemoryBuffer::getFileOrSTDIN(DSYMPath
);
7359 if (std::error_code EC
= BufOrErr
.getError()) {
7360 reportError(errorCodeToError(EC
), DSYMPath
);
7364 // We need to keep the file alive, because we're replacing DbgObj with it.
7365 DSYMBuf
= std::move(BufOrErr
.get());
7367 Expected
<std::unique_ptr
<Binary
>> BinaryOrErr
=
7368 createBinary(DSYMBuf
.get()->getMemBufferRef());
7370 reportError(BinaryOrErr
.takeError(), DSYMPath
);
7374 // We need to keep the Binary alive with the buffer
7375 DSYMBinary
= std::move(BinaryOrErr
.get());
7376 if (ObjectFile
*O
= dyn_cast
<ObjectFile
>(DSYMBinary
.get())) {
7377 // this is a Mach-O object file, use it
7378 if (MachOObjectFile
*MachDSYM
= dyn_cast
<MachOObjectFile
>(&*O
)) {
7382 WithColor::error(errs(), "llvm-objdump")
7383 << DSYMPath
<< " is not a Mach-O file type.\n";
7387 else if (auto UB
= dyn_cast
<MachOUniversalBinary
>(DSYMBinary
.get())){
7388 // this is a Universal Binary, find a Mach-O for this architecture
7389 uint32_t CPUType
, CPUSubType
;
7390 const char *ArchFlag
;
7391 if (MachOOF
->is64Bit()) {
7392 const MachO::mach_header_64 H_64
= MachOOF
->getHeader64();
7393 CPUType
= H_64
.cputype
;
7394 CPUSubType
= H_64
.cpusubtype
;
7396 const MachO::mach_header H
= MachOOF
->getHeader();
7397 CPUType
= H
.cputype
;
7398 CPUSubType
= H
.cpusubtype
;
7400 Triple T
= MachOObjectFile::getArchTriple(CPUType
, CPUSubType
, nullptr,
7402 Expected
<std::unique_ptr
<MachOObjectFile
>> MachDSYM
=
7403 UB
->getMachOObjectForArch(ArchFlag
);
7405 reportError(MachDSYM
.takeError(), DSYMPath
);
7409 // We need to keep the Binary alive with the buffer
7410 DbgObj
= &*MachDSYM
.get();
7411 DSYMBinary
= std::move(*MachDSYM
);
7414 WithColor::error(errs(), "llvm-objdump")
7415 << DSYMPath
<< " is not a Mach-O or Universal file type.\n";
7420 // Setup the DIContext
7421 diContext
= DWARFContext::create(*DbgObj
);
7424 if (FilterSections
.empty())
7425 outs() << "(" << DisSegName
<< "," << DisSectName
<< ") section\n";
7427 for (unsigned SectIdx
= 0; SectIdx
!= Sections
.size(); SectIdx
++) {
7428 Expected
<StringRef
> SecNameOrErr
= Sections
[SectIdx
].getName();
7429 if (!SecNameOrErr
) {
7430 consumeError(SecNameOrErr
.takeError());
7433 if (*SecNameOrErr
!= DisSectName
)
7436 DataRefImpl DR
= Sections
[SectIdx
].getRawDataRefImpl();
7438 StringRef SegmentName
= MachOOF
->getSectionFinalSegmentName(DR
);
7439 if (SegmentName
!= DisSegName
)
7442 StringRef BytesStr
=
7443 unwrapOrError(Sections
[SectIdx
].getContents(), Filename
);
7444 ArrayRef
<uint8_t> Bytes
= arrayRefFromStringRef(BytesStr
);
7445 uint64_t SectAddress
= Sections
[SectIdx
].getAddress();
7447 bool symbolTableWorked
= false;
7449 // Create a map of symbol addresses to symbol names for use by
7450 // the SymbolizerSymbolLookUp() routine.
7451 SymbolAddressMap AddrMap
;
7452 bool DisSymNameFound
= false;
7453 for (const SymbolRef
&Symbol
: MachOOF
->symbols()) {
7454 SymbolRef::Type ST
=
7455 unwrapOrError(Symbol
.getType(), MachOOF
->getFileName());
7456 if (ST
== SymbolRef::ST_Function
|| ST
== SymbolRef::ST_Data
||
7457 ST
== SymbolRef::ST_Other
) {
7458 uint64_t Address
= Symbol
.getValue();
7460 unwrapOrError(Symbol
.getName(), MachOOF
->getFileName());
7461 AddrMap
[Address
] = SymName
;
7462 if (!DisSymName
.empty() && DisSymName
== SymName
)
7463 DisSymNameFound
= true;
7466 if (!DisSymName
.empty() && !DisSymNameFound
) {
7467 outs() << "Can't find -dis-symname: " << DisSymName
<< "\n";
7470 // Set up the block of info used by the Symbolizer call backs.
7471 SymbolizerInfo
.verbose
= !NoSymbolicOperands
;
7472 SymbolizerInfo
.O
= MachOOF
;
7473 SymbolizerInfo
.S
= Sections
[SectIdx
];
7474 SymbolizerInfo
.AddrMap
= &AddrMap
;
7475 SymbolizerInfo
.Sections
= &Sections
;
7476 // Same for the ThumbSymbolizer
7477 ThumbSymbolizerInfo
.verbose
= !NoSymbolicOperands
;
7478 ThumbSymbolizerInfo
.O
= MachOOF
;
7479 ThumbSymbolizerInfo
.S
= Sections
[SectIdx
];
7480 ThumbSymbolizerInfo
.AddrMap
= &AddrMap
;
7481 ThumbSymbolizerInfo
.Sections
= &Sections
;
7483 unsigned int Arch
= MachOOF
->getArch();
7485 // Skip all symbols if this is a stubs file.
7489 // If the section has symbols but no symbol at the start of the section
7490 // these are used to make sure the bytes before the first symbol are
7492 bool FirstSymbol
= true;
7493 bool FirstSymbolAtSectionStart
= true;
7495 // Disassemble symbol by symbol.
7496 for (unsigned SymIdx
= 0; SymIdx
!= Symbols
.size(); SymIdx
++) {
7498 unwrapOrError(Symbols
[SymIdx
].getName(), MachOOF
->getFileName());
7499 SymbolRef::Type ST
=
7500 unwrapOrError(Symbols
[SymIdx
].getType(), MachOOF
->getFileName());
7501 if (ST
!= SymbolRef::ST_Function
&& ST
!= SymbolRef::ST_Data
)
7504 // Make sure the symbol is defined in this section.
7505 bool containsSym
= Sections
[SectIdx
].containsSymbol(Symbols
[SymIdx
]);
7507 if (!DisSymName
.empty() && DisSymName
== SymName
) {
7508 outs() << "-dis-symname: " << DisSymName
<< " not in the section\n";
7513 // The __mh_execute_header is special and we need to deal with that fact
7514 // this symbol is before the start of the (__TEXT,__text) section and at the
7515 // address of the start of the __TEXT segment. This is because this symbol
7516 // is an N_SECT symbol in the (__TEXT,__text) but its address is before the
7517 // start of the section in a standard MH_EXECUTE filetype.
7518 if (!DisSymName
.empty() && DisSymName
== "__mh_execute_header") {
7519 outs() << "-dis-symname: __mh_execute_header not in any section\n";
7522 // When this code is trying to disassemble a symbol at a time and in the
7523 // case there is only the __mh_execute_header symbol left as in a stripped
7524 // executable, we need to deal with this by ignoring this symbol so the
7525 // whole section is disassembled and this symbol is then not displayed.
7526 if (SymName
== "__mh_execute_header" || SymName
== "__mh_dylib_header" ||
7527 SymName
== "__mh_bundle_header" || SymName
== "__mh_object_header" ||
7528 SymName
== "__mh_preload_header" || SymName
== "__mh_dylinker_header")
7531 // If we are only disassembling one symbol see if this is that symbol.
7532 if (!DisSymName
.empty() && DisSymName
!= SymName
)
7535 // Start at the address of the symbol relative to the section's address.
7536 uint64_t SectSize
= Sections
[SectIdx
].getSize();
7537 uint64_t Start
= Symbols
[SymIdx
].getValue();
7538 uint64_t SectionAddress
= Sections
[SectIdx
].getAddress();
7539 Start
-= SectionAddress
;
7541 if (Start
> SectSize
) {
7542 outs() << "section data ends, " << SymName
7543 << " lies outside valid range\n";
7547 // Stop disassembling either at the beginning of the next symbol or at
7548 // the end of the section.
7549 bool containsNextSym
= false;
7550 uint64_t NextSym
= 0;
7551 uint64_t NextSymIdx
= SymIdx
+ 1;
7552 while (Symbols
.size() > NextSymIdx
) {
7553 SymbolRef::Type NextSymType
= unwrapOrError(
7554 Symbols
[NextSymIdx
].getType(), MachOOF
->getFileName());
7555 if (NextSymType
== SymbolRef::ST_Function
) {
7557 Sections
[SectIdx
].containsSymbol(Symbols
[NextSymIdx
]);
7558 NextSym
= Symbols
[NextSymIdx
].getValue();
7559 NextSym
-= SectionAddress
;
7565 uint64_t End
= containsNextSym
? std::min(NextSym
, SectSize
) : SectSize
;
7568 symbolTableWorked
= true;
7570 DataRefImpl Symb
= Symbols
[SymIdx
].getRawDataRefImpl();
7571 bool IsThumb
= MachOOF
->getSymbolFlags(Symb
) & SymbolRef::SF_Thumb
;
7573 // We only need the dedicated Thumb target if there's a real choice
7574 // (i.e. we're not targeting M-class) and the function is Thumb.
7575 bool UseThumbTarget
= IsThumb
&& ThumbTarget
;
7577 // If we are not specifying a symbol to start disassembly with and this
7578 // is the first symbol in the section but not at the start of the section
7579 // then move the disassembly index to the start of the section and
7580 // don't print the symbol name just yet. This is so the bytes before the
7581 // first symbol are disassembled.
7582 uint64_t SymbolStart
= Start
;
7583 if (DisSymName
.empty() && FirstSymbol
&& Start
!= 0) {
7584 FirstSymbolAtSectionStart
= false;
7588 outs() << SymName
<< ":\n";
7590 DILineInfo lastLine
;
7591 for (uint64_t Index
= Start
; Index
< End
; Index
+= Size
) {
7594 // If this is the first symbol in the section and it was not at the
7595 // start of the section, see if we are at its Index now and if so print
7597 if (FirstSymbol
&& !FirstSymbolAtSectionStart
&& Index
== SymbolStart
)
7598 outs() << SymName
<< ":\n";
7600 uint64_t PC
= SectAddress
+ Index
;
7601 if (!NoLeadingAddr
) {
7602 if (FullLeadingAddr
) {
7603 if (MachOOF
->is64Bit())
7604 outs() << format("%016" PRIx64
, PC
);
7606 outs() << format("%08" PRIx64
, PC
);
7608 outs() << format("%8" PRIx64
":", PC
);
7611 if (!NoShowRawInsn
|| Arch
== Triple::arm
)
7614 if (DumpAndSkipDataInCode(PC
, Bytes
.data() + Index
, Dices
, Size
))
7617 SmallVector
<char, 64> AnnotationsBytes
;
7618 raw_svector_ostream
Annotations(AnnotationsBytes
);
7622 gotInst
= ThumbDisAsm
->getInstruction(Inst
, Size
, Bytes
.slice(Index
),
7623 PC
, DebugOut
, Annotations
);
7625 gotInst
= DisAsm
->getInstruction(Inst
, Size
, Bytes
.slice(Index
), PC
,
7626 DebugOut
, Annotations
);
7628 if (!NoShowRawInsn
|| Arch
== Triple::arm
) {
7629 dumpBytes(makeArrayRef(Bytes
.data() + Index
, Size
), outs());
7631 formatted_raw_ostream
FormattedOS(outs());
7632 StringRef AnnotationsStr
= Annotations
.str();
7634 ThumbIP
->printInst(&Inst
, FormattedOS
, AnnotationsStr
, *ThumbSTI
);
7636 IP
->printInst(&Inst
, FormattedOS
, AnnotationsStr
, *STI
);
7637 emitComments(CommentStream
, CommentsToEmit
, FormattedOS
, *AsmInfo
);
7639 // Print debug info.
7641 DILineInfo dli
= diContext
->getLineInfoForAddress({PC
, SectIdx
});
7642 // Print valid line info if it changed.
7643 if (dli
!= lastLine
&& dli
.Line
!= 0)
7644 outs() << "\t## " << dli
.FileName
<< ':' << dli
.Line
<< ':'
7650 unsigned int Arch
= MachOOF
->getArch();
7651 if (Arch
== Triple::x86_64
|| Arch
== Triple::x86
) {
7652 outs() << format("\t.byte 0x%02x #bad opcode\n",
7653 *(Bytes
.data() + Index
) & 0xff);
7654 Size
= 1; // skip exactly one illegible byte and move on.
7655 } else if (Arch
== Triple::aarch64
||
7656 (Arch
== Triple::arm
&& !IsThumb
)) {
7657 uint32_t opcode
= (*(Bytes
.data() + Index
) & 0xff) |
7658 (*(Bytes
.data() + Index
+ 1) & 0xff) << 8 |
7659 (*(Bytes
.data() + Index
+ 2) & 0xff) << 16 |
7660 (*(Bytes
.data() + Index
+ 3) & 0xff) << 24;
7661 outs() << format("\t.long\t0x%08x\n", opcode
);
7663 } else if (Arch
== Triple::arm
) {
7664 assert(IsThumb
&& "ARM mode should have been dealt with above");
7665 uint32_t opcode
= (*(Bytes
.data() + Index
) & 0xff) |
7666 (*(Bytes
.data() + Index
+ 1) & 0xff) << 8;
7667 outs() << format("\t.short\t0x%04x\n", opcode
);
7670 WithColor::warning(errs(), "llvm-objdump")
7671 << "invalid instruction encoding\n";
7673 Size
= 1; // skip illegible bytes
7677 // Now that we are done disassembled the first symbol set the bool that
7678 // were doing this to false.
7679 FirstSymbol
= false;
7681 if (!symbolTableWorked
) {
7682 // Reading the symbol table didn't work, disassemble the whole section.
7683 uint64_t SectAddress
= Sections
[SectIdx
].getAddress();
7684 uint64_t SectSize
= Sections
[SectIdx
].getSize();
7686 for (uint64_t Index
= 0; Index
< SectSize
; Index
+= InstSize
) {
7689 uint64_t PC
= SectAddress
+ Index
;
7691 if (DumpAndSkipDataInCode(PC
, Bytes
.data() + Index
, Dices
, InstSize
))
7694 SmallVector
<char, 64> AnnotationsBytes
;
7695 raw_svector_ostream
Annotations(AnnotationsBytes
);
7696 if (DisAsm
->getInstruction(Inst
, InstSize
, Bytes
.slice(Index
), PC
,
7697 DebugOut
, Annotations
)) {
7698 if (!NoLeadingAddr
) {
7699 if (FullLeadingAddr
) {
7700 if (MachOOF
->is64Bit())
7701 outs() << format("%016" PRIx64
, PC
);
7703 outs() << format("%08" PRIx64
, PC
);
7705 outs() << format("%8" PRIx64
":", PC
);
7708 if (!NoShowRawInsn
|| Arch
== Triple::arm
) {
7710 dumpBytes(makeArrayRef(Bytes
.data() + Index
, InstSize
), outs());
7712 StringRef AnnotationsStr
= Annotations
.str();
7713 IP
->printInst(&Inst
, outs(), AnnotationsStr
, *STI
);
7716 unsigned int Arch
= MachOOF
->getArch();
7717 if (Arch
== Triple::x86_64
|| Arch
== Triple::x86
) {
7718 outs() << format("\t.byte 0x%02x #bad opcode\n",
7719 *(Bytes
.data() + Index
) & 0xff);
7720 InstSize
= 1; // skip exactly one illegible byte and move on.
7722 WithColor::warning(errs(), "llvm-objdump")
7723 << "invalid instruction encoding\n";
7725 InstSize
= 1; // skip illegible bytes
7730 // The TripleName's need to be reset if we are called again for a different
7733 ThumbTripleName
= "";
7735 if (SymbolizerInfo
.demangled_name
!= nullptr)
7736 free(SymbolizerInfo
.demangled_name
);
7737 if (ThumbSymbolizerInfo
.demangled_name
!= nullptr)
7738 free(ThumbSymbolizerInfo
.demangled_name
);
7742 //===----------------------------------------------------------------------===//
7743 // __compact_unwind section dumping
7744 //===----------------------------------------------------------------------===//
7748 template <typename T
>
7749 static uint64_t read(StringRef Contents
, ptrdiff_t Offset
) {
7750 using llvm::support::little
;
7751 using llvm::support::unaligned
;
7753 if (Offset
+ sizeof(T
) > Contents
.size()) {
7754 outs() << "warning: attempt to read past end of buffer\n";
7759 support::endian::read
<T
, little
, unaligned
>(Contents
.data() + Offset
);
7763 template <typename T
>
7764 static uint64_t readNext(StringRef Contents
, ptrdiff_t &Offset
) {
7765 T Val
= read
<T
>(Contents
, Offset
);
7766 Offset
+= sizeof(T
);
7770 struct CompactUnwindEntry
{
7771 uint32_t OffsetInSection
;
7773 uint64_t FunctionAddr
;
7775 uint32_t CompactEncoding
;
7776 uint64_t PersonalityAddr
;
7779 RelocationRef FunctionReloc
;
7780 RelocationRef PersonalityReloc
;
7781 RelocationRef LSDAReloc
;
7783 CompactUnwindEntry(StringRef Contents
, unsigned Offset
, bool Is64
)
7784 : OffsetInSection(Offset
) {
7786 read
<uint64_t>(Contents
, Offset
);
7788 read
<uint32_t>(Contents
, Offset
);
7792 template <typename UIntPtr
> void read(StringRef Contents
, ptrdiff_t Offset
) {
7793 FunctionAddr
= readNext
<UIntPtr
>(Contents
, Offset
);
7794 Length
= readNext
<uint32_t>(Contents
, Offset
);
7795 CompactEncoding
= readNext
<uint32_t>(Contents
, Offset
);
7796 PersonalityAddr
= readNext
<UIntPtr
>(Contents
, Offset
);
7797 LSDAAddr
= readNext
<UIntPtr
>(Contents
, Offset
);
7802 /// Given a relocation from __compact_unwind, consisting of the RelocationRef
7803 /// and data being relocated, determine the best base Name and Addend to use for
7804 /// display purposes.
7806 /// 1. An Extern relocation will directly reference a symbol (and the data is
7807 /// then already an addend), so use that.
7808 /// 2. Otherwise the data is an offset in the object file's layout; try to find
7809 // a symbol before it in the same section, and use the offset from there.
7810 /// 3. Finally, if all that fails, fall back to an offset from the start of the
7811 /// referenced section.
7812 static void findUnwindRelocNameAddend(const MachOObjectFile
*Obj
,
7813 std::map
<uint64_t, SymbolRef
> &Symbols
,
7814 const RelocationRef
&Reloc
, uint64_t Addr
,
7815 StringRef
&Name
, uint64_t &Addend
) {
7816 if (Reloc
.getSymbol() != Obj
->symbol_end()) {
7817 Name
= unwrapOrError(Reloc
.getSymbol()->getName(), Obj
->getFileName());
7822 auto RE
= Obj
->getRelocation(Reloc
.getRawDataRefImpl());
7823 SectionRef RelocSection
= Obj
->getAnyRelocationSection(RE
);
7825 uint64_t SectionAddr
= RelocSection
.getAddress();
7827 auto Sym
= Symbols
.upper_bound(Addr
);
7828 if (Sym
== Symbols
.begin()) {
7829 // The first symbol in the object is after this reference, the best we can
7830 // do is section-relative notation.
7831 if (Expected
<StringRef
> NameOrErr
= RelocSection
.getName())
7834 consumeError(NameOrErr
.takeError());
7836 Addend
= Addr
- SectionAddr
;
7840 // Go back one so that SymbolAddress <= Addr.
7843 section_iterator SymSection
=
7844 unwrapOrError(Sym
->second
.getSection(), Obj
->getFileName());
7845 if (RelocSection
== *SymSection
) {
7846 // There's a valid symbol in the same section before this reference.
7847 Name
= unwrapOrError(Sym
->second
.getName(), Obj
->getFileName());
7848 Addend
= Addr
- Sym
->first
;
7852 // There is a symbol before this reference, but it's in a different
7853 // section. Probably not helpful to mention it, so use the section name.
7854 if (Expected
<StringRef
> NameOrErr
= RelocSection
.getName())
7857 consumeError(NameOrErr
.takeError());
7859 Addend
= Addr
- SectionAddr
;
7862 static void printUnwindRelocDest(const MachOObjectFile
*Obj
,
7863 std::map
<uint64_t, SymbolRef
> &Symbols
,
7864 const RelocationRef
&Reloc
, uint64_t Addr
) {
7868 if (!Reloc
.getObject())
7871 findUnwindRelocNameAddend(Obj
, Symbols
, Reloc
, Addr
, Name
, Addend
);
7875 outs() << " + " << format("0x%" PRIx64
, Addend
);
7879 printMachOCompactUnwindSection(const MachOObjectFile
*Obj
,
7880 std::map
<uint64_t, SymbolRef
> &Symbols
,
7881 const SectionRef
&CompactUnwind
) {
7883 if (!Obj
->isLittleEndian()) {
7884 outs() << "Skipping big-endian __compact_unwind section\n";
7888 bool Is64
= Obj
->is64Bit();
7889 uint32_t PointerSize
= Is64
? sizeof(uint64_t) : sizeof(uint32_t);
7890 uint32_t EntrySize
= 3 * PointerSize
+ 2 * sizeof(uint32_t);
7892 StringRef Contents
=
7893 unwrapOrError(CompactUnwind
.getContents(), Obj
->getFileName());
7894 SmallVector
<CompactUnwindEntry
, 4> CompactUnwinds
;
7896 // First populate the initial raw offsets, encodings and so on from the entry.
7897 for (unsigned Offset
= 0; Offset
< Contents
.size(); Offset
+= EntrySize
) {
7898 CompactUnwindEntry
Entry(Contents
, Offset
, Is64
);
7899 CompactUnwinds
.push_back(Entry
);
7902 // Next we need to look at the relocations to find out what objects are
7903 // actually being referred to.
7904 for (const RelocationRef
&Reloc
: CompactUnwind
.relocations()) {
7905 uint64_t RelocAddress
= Reloc
.getOffset();
7907 uint32_t EntryIdx
= RelocAddress
/ EntrySize
;
7908 uint32_t OffsetInEntry
= RelocAddress
- EntryIdx
* EntrySize
;
7909 CompactUnwindEntry
&Entry
= CompactUnwinds
[EntryIdx
];
7911 if (OffsetInEntry
== 0)
7912 Entry
.FunctionReloc
= Reloc
;
7913 else if (OffsetInEntry
== PointerSize
+ 2 * sizeof(uint32_t))
7914 Entry
.PersonalityReloc
= Reloc
;
7915 else if (OffsetInEntry
== 2 * PointerSize
+ 2 * sizeof(uint32_t))
7916 Entry
.LSDAReloc
= Reloc
;
7918 outs() << "Invalid relocation in __compact_unwind section\n";
7923 // Finally, we're ready to print the data we've gathered.
7924 outs() << "Contents of __compact_unwind section:\n";
7925 for (auto &Entry
: CompactUnwinds
) {
7926 outs() << " Entry at offset "
7927 << format("0x%" PRIx32
, Entry
.OffsetInSection
) << ":\n";
7929 // 1. Start of the region this entry applies to.
7930 outs() << " start: " << format("0x%" PRIx64
,
7931 Entry
.FunctionAddr
) << ' ';
7932 printUnwindRelocDest(Obj
, Symbols
, Entry
.FunctionReloc
, Entry
.FunctionAddr
);
7935 // 2. Length of the region this entry applies to.
7936 outs() << " length: " << format("0x%" PRIx32
, Entry
.Length
)
7938 // 3. The 32-bit compact encoding.
7939 outs() << " compact encoding: "
7940 << format("0x%08" PRIx32
, Entry
.CompactEncoding
) << '\n';
7942 // 4. The personality function, if present.
7943 if (Entry
.PersonalityReloc
.getObject()) {
7944 outs() << " personality function: "
7945 << format("0x%" PRIx64
, Entry
.PersonalityAddr
) << ' ';
7946 printUnwindRelocDest(Obj
, Symbols
, Entry
.PersonalityReloc
,
7947 Entry
.PersonalityAddr
);
7951 // 5. This entry's language-specific data area.
7952 if (Entry
.LSDAReloc
.getObject()) {
7953 outs() << " LSDA: " << format("0x%" PRIx64
,
7954 Entry
.LSDAAddr
) << ' ';
7955 printUnwindRelocDest(Obj
, Symbols
, Entry
.LSDAReloc
, Entry
.LSDAAddr
);
7961 //===----------------------------------------------------------------------===//
7962 // __unwind_info section dumping
7963 //===----------------------------------------------------------------------===//
7965 static void printRegularSecondLevelUnwindPage(StringRef PageData
) {
7967 uint32_t Kind
= readNext
<uint32_t>(PageData
, Pos
);
7969 assert(Kind
== 2 && "kind for a regular 2nd level index should be 2");
7971 uint16_t EntriesStart
= readNext
<uint16_t>(PageData
, Pos
);
7972 uint16_t NumEntries
= readNext
<uint16_t>(PageData
, Pos
);
7975 for (unsigned i
= 0; i
< NumEntries
; ++i
) {
7976 uint32_t FunctionOffset
= readNext
<uint32_t>(PageData
, Pos
);
7977 uint32_t Encoding
= readNext
<uint32_t>(PageData
, Pos
);
7979 outs() << " [" << i
<< "]: "
7980 << "function offset=" << format("0x%08" PRIx32
, FunctionOffset
)
7982 << "encoding=" << format("0x%08" PRIx32
, Encoding
) << '\n';
7986 static void printCompressedSecondLevelUnwindPage(
7987 StringRef PageData
, uint32_t FunctionBase
,
7988 const SmallVectorImpl
<uint32_t> &CommonEncodings
) {
7990 uint32_t Kind
= readNext
<uint32_t>(PageData
, Pos
);
7992 assert(Kind
== 3 && "kind for a compressed 2nd level index should be 3");
7994 uint16_t EntriesStart
= readNext
<uint16_t>(PageData
, Pos
);
7995 uint16_t NumEntries
= readNext
<uint16_t>(PageData
, Pos
);
7997 uint16_t EncodingsStart
= readNext
<uint16_t>(PageData
, Pos
);
7998 readNext
<uint16_t>(PageData
, Pos
);
7999 StringRef PageEncodings
= PageData
.substr(EncodingsStart
, StringRef::npos
);
8002 for (unsigned i
= 0; i
< NumEntries
; ++i
) {
8003 uint32_t Entry
= readNext
<uint32_t>(PageData
, Pos
);
8004 uint32_t FunctionOffset
= FunctionBase
+ (Entry
& 0xffffff);
8005 uint32_t EncodingIdx
= Entry
>> 24;
8008 if (EncodingIdx
< CommonEncodings
.size())
8009 Encoding
= CommonEncodings
[EncodingIdx
];
8011 Encoding
= read
<uint32_t>(PageEncodings
,
8013 (EncodingIdx
- CommonEncodings
.size()));
8015 outs() << " [" << i
<< "]: "
8016 << "function offset=" << format("0x%08" PRIx32
, FunctionOffset
)
8018 << "encoding[" << EncodingIdx
8019 << "]=" << format("0x%08" PRIx32
, Encoding
) << '\n';
8023 static void printMachOUnwindInfoSection(const MachOObjectFile
*Obj
,
8024 std::map
<uint64_t, SymbolRef
> &Symbols
,
8025 const SectionRef
&UnwindInfo
) {
8027 if (!Obj
->isLittleEndian()) {
8028 outs() << "Skipping big-endian __unwind_info section\n";
8032 outs() << "Contents of __unwind_info section:\n";
8034 StringRef Contents
=
8035 unwrapOrError(UnwindInfo
.getContents(), Obj
->getFileName());
8038 //===----------------------------------
8040 //===----------------------------------
8042 uint32_t Version
= readNext
<uint32_t>(Contents
, Pos
);
8043 outs() << " Version: "
8044 << format("0x%" PRIx32
, Version
) << '\n';
8046 outs() << " Skipping section with unknown version\n";
8050 uint32_t CommonEncodingsStart
= readNext
<uint32_t>(Contents
, Pos
);
8051 outs() << " Common encodings array section offset: "
8052 << format("0x%" PRIx32
, CommonEncodingsStart
) << '\n';
8053 uint32_t NumCommonEncodings
= readNext
<uint32_t>(Contents
, Pos
);
8054 outs() << " Number of common encodings in array: "
8055 << format("0x%" PRIx32
, NumCommonEncodings
) << '\n';
8057 uint32_t PersonalitiesStart
= readNext
<uint32_t>(Contents
, Pos
);
8058 outs() << " Personality function array section offset: "
8059 << format("0x%" PRIx32
, PersonalitiesStart
) << '\n';
8060 uint32_t NumPersonalities
= readNext
<uint32_t>(Contents
, Pos
);
8061 outs() << " Number of personality functions in array: "
8062 << format("0x%" PRIx32
, NumPersonalities
) << '\n';
8064 uint32_t IndicesStart
= readNext
<uint32_t>(Contents
, Pos
);
8065 outs() << " Index array section offset: "
8066 << format("0x%" PRIx32
, IndicesStart
) << '\n';
8067 uint32_t NumIndices
= readNext
<uint32_t>(Contents
, Pos
);
8068 outs() << " Number of indices in array: "
8069 << format("0x%" PRIx32
, NumIndices
) << '\n';
8071 //===----------------------------------
8072 // A shared list of common encodings
8073 //===----------------------------------
8075 // These occupy indices in the range [0, N] whenever an encoding is referenced
8076 // from a compressed 2nd level index table. In practice the linker only
8077 // creates ~128 of these, so that indices are available to embed encodings in
8078 // the 2nd level index.
8080 SmallVector
<uint32_t, 64> CommonEncodings
;
8081 outs() << " Common encodings: (count = " << NumCommonEncodings
<< ")\n";
8082 Pos
= CommonEncodingsStart
;
8083 for (unsigned i
= 0; i
< NumCommonEncodings
; ++i
) {
8084 uint32_t Encoding
= readNext
<uint32_t>(Contents
, Pos
);
8085 CommonEncodings
.push_back(Encoding
);
8087 outs() << " encoding[" << i
<< "]: " << format("0x%08" PRIx32
, Encoding
)
8091 //===----------------------------------
8092 // Personality functions used in this executable
8093 //===----------------------------------
8095 // There should be only a handful of these (one per source language,
8096 // roughly). Particularly since they only get 2 bits in the compact encoding.
8098 outs() << " Personality functions: (count = " << NumPersonalities
<< ")\n";
8099 Pos
= PersonalitiesStart
;
8100 for (unsigned i
= 0; i
< NumPersonalities
; ++i
) {
8101 uint32_t PersonalityFn
= readNext
<uint32_t>(Contents
, Pos
);
8102 outs() << " personality[" << i
+ 1
8103 << "]: " << format("0x%08" PRIx32
, PersonalityFn
) << '\n';
8106 //===----------------------------------
8107 // The level 1 index entries
8108 //===----------------------------------
8110 // These specify an approximate place to start searching for the more detailed
8111 // information, sorted by PC.
8114 uint32_t FunctionOffset
;
8115 uint32_t SecondLevelPageStart
;
8119 SmallVector
<IndexEntry
, 4> IndexEntries
;
8121 outs() << " Top level indices: (count = " << NumIndices
<< ")\n";
8123 for (unsigned i
= 0; i
< NumIndices
; ++i
) {
8126 Entry
.FunctionOffset
= readNext
<uint32_t>(Contents
, Pos
);
8127 Entry
.SecondLevelPageStart
= readNext
<uint32_t>(Contents
, Pos
);
8128 Entry
.LSDAStart
= readNext
<uint32_t>(Contents
, Pos
);
8129 IndexEntries
.push_back(Entry
);
8131 outs() << " [" << i
<< "]: "
8132 << "function offset=" << format("0x%08" PRIx32
, Entry
.FunctionOffset
)
8134 << "2nd level page offset="
8135 << format("0x%08" PRIx32
, Entry
.SecondLevelPageStart
) << ", "
8136 << "LSDA offset=" << format("0x%08" PRIx32
, Entry
.LSDAStart
) << '\n';
8139 //===----------------------------------
8140 // Next come the LSDA tables
8141 //===----------------------------------
8143 // The LSDA layout is rather implicit: it's a contiguous array of entries from
8144 // the first top-level index's LSDAOffset to the last (sentinel).
8146 outs() << " LSDA descriptors:\n";
8147 Pos
= IndexEntries
[0].LSDAStart
;
8148 const uint32_t LSDASize
= 2 * sizeof(uint32_t);
8150 (IndexEntries
.back().LSDAStart
- IndexEntries
[0].LSDAStart
) / LSDASize
;
8152 for (int i
= 0; i
< NumLSDAs
; ++i
) {
8153 uint32_t FunctionOffset
= readNext
<uint32_t>(Contents
, Pos
);
8154 uint32_t LSDAOffset
= readNext
<uint32_t>(Contents
, Pos
);
8155 outs() << " [" << i
<< "]: "
8156 << "function offset=" << format("0x%08" PRIx32
, FunctionOffset
)
8158 << "LSDA offset=" << format("0x%08" PRIx32
, LSDAOffset
) << '\n';
8161 //===----------------------------------
8162 // Finally, the 2nd level indices
8163 //===----------------------------------
8165 // Generally these are 4K in size, and have 2 possible forms:
8166 // + Regular stores up to 511 entries with disparate encodings
8167 // + Compressed stores up to 1021 entries if few enough compact encoding
8169 outs() << " Second level indices:\n";
8170 for (unsigned i
= 0; i
< IndexEntries
.size() - 1; ++i
) {
8171 // The final sentinel top-level index has no associated 2nd level page
8172 if (IndexEntries
[i
].SecondLevelPageStart
== 0)
8175 outs() << " Second level index[" << i
<< "]: "
8176 << "offset in section="
8177 << format("0x%08" PRIx32
, IndexEntries
[i
].SecondLevelPageStart
)
8179 << "base function offset="
8180 << format("0x%08" PRIx32
, IndexEntries
[i
].FunctionOffset
) << '\n';
8182 Pos
= IndexEntries
[i
].SecondLevelPageStart
;
8183 if (Pos
+ sizeof(uint32_t) > Contents
.size()) {
8184 outs() << "warning: invalid offset for second level page: " << Pos
<< '\n';
8189 *reinterpret_cast<const support::ulittle32_t
*>(Contents
.data() + Pos
);
8191 printRegularSecondLevelUnwindPage(Contents
.substr(Pos
, 4096));
8193 printCompressedSecondLevelUnwindPage(Contents
.substr(Pos
, 4096),
8194 IndexEntries
[i
].FunctionOffset
,
8197 outs() << " Skipping 2nd level page with unknown kind " << Kind
8202 void printMachOUnwindInfo(const MachOObjectFile
*Obj
) {
8203 std::map
<uint64_t, SymbolRef
> Symbols
;
8204 for (const SymbolRef
&SymRef
: Obj
->symbols()) {
8205 // Discard any undefined or absolute symbols. They're not going to take part
8206 // in the convenience lookup for unwind info and just take up resources.
8207 auto SectOrErr
= SymRef
.getSection();
8209 // TODO: Actually report errors helpfully.
8210 consumeError(SectOrErr
.takeError());
8213 section_iterator Section
= *SectOrErr
;
8214 if (Section
== Obj
->section_end())
8217 uint64_t Addr
= SymRef
.getValue();
8218 Symbols
.insert(std::make_pair(Addr
, SymRef
));
8221 for (const SectionRef
&Section
: Obj
->sections()) {
8223 if (Expected
<StringRef
> NameOrErr
= Section
.getName())
8224 SectName
= *NameOrErr
;
8226 consumeError(NameOrErr
.takeError());
8228 if (SectName
== "__compact_unwind")
8229 printMachOCompactUnwindSection(Obj
, Symbols
, Section
);
8230 else if (SectName
== "__unwind_info")
8231 printMachOUnwindInfoSection(Obj
, Symbols
, Section
);
8235 static void PrintMachHeader(uint32_t magic
, uint32_t cputype
,
8236 uint32_t cpusubtype
, uint32_t filetype
,
8237 uint32_t ncmds
, uint32_t sizeofcmds
, uint32_t flags
,
8239 outs() << "Mach header\n";
8240 outs() << " magic cputype cpusubtype caps filetype ncmds "
8241 "sizeofcmds flags\n";
8243 if (magic
== MachO::MH_MAGIC
)
8244 outs() << " MH_MAGIC";
8245 else if (magic
== MachO::MH_MAGIC_64
)
8246 outs() << "MH_MAGIC_64";
8248 outs() << format(" 0x%08" PRIx32
, magic
);
8250 case MachO::CPU_TYPE_I386
:
8252 switch (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) {
8253 case MachO::CPU_SUBTYPE_I386_ALL
:
8257 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
8261 case MachO::CPU_TYPE_X86_64
:
8262 outs() << " X86_64";
8263 switch (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) {
8264 case MachO::CPU_SUBTYPE_X86_64_ALL
:
8267 case MachO::CPU_SUBTYPE_X86_64_H
:
8268 outs() << " Haswell";
8271 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
8275 case MachO::CPU_TYPE_ARM
:
8277 switch (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) {
8278 case MachO::CPU_SUBTYPE_ARM_ALL
:
8281 case MachO::CPU_SUBTYPE_ARM_V4T
:
8284 case MachO::CPU_SUBTYPE_ARM_V5TEJ
:
8287 case MachO::CPU_SUBTYPE_ARM_XSCALE
:
8288 outs() << " XSCALE";
8290 case MachO::CPU_SUBTYPE_ARM_V6
:
8293 case MachO::CPU_SUBTYPE_ARM_V6M
:
8296 case MachO::CPU_SUBTYPE_ARM_V7
:
8299 case MachO::CPU_SUBTYPE_ARM_V7EM
:
8302 case MachO::CPU_SUBTYPE_ARM_V7K
:
8305 case MachO::CPU_SUBTYPE_ARM_V7M
:
8308 case MachO::CPU_SUBTYPE_ARM_V7S
:
8312 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
8316 case MachO::CPU_TYPE_ARM64
:
8318 switch (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) {
8319 case MachO::CPU_SUBTYPE_ARM64_ALL
:
8322 case MachO::CPU_SUBTYPE_ARM64E
:
8326 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
8330 case MachO::CPU_TYPE_ARM64_32
:
8331 outs() << " ARM64_32";
8332 switch (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) {
8333 case MachO::CPU_SUBTYPE_ARM64_32_V8
:
8337 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
8341 case MachO::CPU_TYPE_POWERPC
:
8343 switch (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) {
8344 case MachO::CPU_SUBTYPE_POWERPC_ALL
:
8348 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
8352 case MachO::CPU_TYPE_POWERPC64
:
8354 switch (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) {
8355 case MachO::CPU_SUBTYPE_POWERPC_ALL
:
8359 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
8364 outs() << format(" %7d", cputype
);
8365 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
8368 if ((cpusubtype
& MachO::CPU_SUBTYPE_MASK
) == MachO::CPU_SUBTYPE_LIB64
) {
8371 outs() << format(" 0x%02" PRIx32
,
8372 (cpusubtype
& MachO::CPU_SUBTYPE_MASK
) >> 24);
8375 case MachO::MH_OBJECT
:
8376 outs() << " OBJECT";
8378 case MachO::MH_EXECUTE
:
8379 outs() << " EXECUTE";
8381 case MachO::MH_FVMLIB
:
8382 outs() << " FVMLIB";
8384 case MachO::MH_CORE
:
8387 case MachO::MH_PRELOAD
:
8388 outs() << " PRELOAD";
8390 case MachO::MH_DYLIB
:
8393 case MachO::MH_DYLIB_STUB
:
8394 outs() << " DYLIB_STUB";
8396 case MachO::MH_DYLINKER
:
8397 outs() << " DYLINKER";
8399 case MachO::MH_BUNDLE
:
8400 outs() << " BUNDLE";
8402 case MachO::MH_DSYM
:
8405 case MachO::MH_KEXT_BUNDLE
:
8406 outs() << " KEXTBUNDLE";
8409 outs() << format(" %10u", filetype
);
8412 outs() << format(" %5u", ncmds
);
8413 outs() << format(" %10u", sizeofcmds
);
8415 if (f
& MachO::MH_NOUNDEFS
) {
8416 outs() << " NOUNDEFS";
8417 f
&= ~MachO::MH_NOUNDEFS
;
8419 if (f
& MachO::MH_INCRLINK
) {
8420 outs() << " INCRLINK";
8421 f
&= ~MachO::MH_INCRLINK
;
8423 if (f
& MachO::MH_DYLDLINK
) {
8424 outs() << " DYLDLINK";
8425 f
&= ~MachO::MH_DYLDLINK
;
8427 if (f
& MachO::MH_BINDATLOAD
) {
8428 outs() << " BINDATLOAD";
8429 f
&= ~MachO::MH_BINDATLOAD
;
8431 if (f
& MachO::MH_PREBOUND
) {
8432 outs() << " PREBOUND";
8433 f
&= ~MachO::MH_PREBOUND
;
8435 if (f
& MachO::MH_SPLIT_SEGS
) {
8436 outs() << " SPLIT_SEGS";
8437 f
&= ~MachO::MH_SPLIT_SEGS
;
8439 if (f
& MachO::MH_LAZY_INIT
) {
8440 outs() << " LAZY_INIT";
8441 f
&= ~MachO::MH_LAZY_INIT
;
8443 if (f
& MachO::MH_TWOLEVEL
) {
8444 outs() << " TWOLEVEL";
8445 f
&= ~MachO::MH_TWOLEVEL
;
8447 if (f
& MachO::MH_FORCE_FLAT
) {
8448 outs() << " FORCE_FLAT";
8449 f
&= ~MachO::MH_FORCE_FLAT
;
8451 if (f
& MachO::MH_NOMULTIDEFS
) {
8452 outs() << " NOMULTIDEFS";
8453 f
&= ~MachO::MH_NOMULTIDEFS
;
8455 if (f
& MachO::MH_NOFIXPREBINDING
) {
8456 outs() << " NOFIXPREBINDING";
8457 f
&= ~MachO::MH_NOFIXPREBINDING
;
8459 if (f
& MachO::MH_PREBINDABLE
) {
8460 outs() << " PREBINDABLE";
8461 f
&= ~MachO::MH_PREBINDABLE
;
8463 if (f
& MachO::MH_ALLMODSBOUND
) {
8464 outs() << " ALLMODSBOUND";
8465 f
&= ~MachO::MH_ALLMODSBOUND
;
8467 if (f
& MachO::MH_SUBSECTIONS_VIA_SYMBOLS
) {
8468 outs() << " SUBSECTIONS_VIA_SYMBOLS";
8469 f
&= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS
;
8471 if (f
& MachO::MH_CANONICAL
) {
8472 outs() << " CANONICAL";
8473 f
&= ~MachO::MH_CANONICAL
;
8475 if (f
& MachO::MH_WEAK_DEFINES
) {
8476 outs() << " WEAK_DEFINES";
8477 f
&= ~MachO::MH_WEAK_DEFINES
;
8479 if (f
& MachO::MH_BINDS_TO_WEAK
) {
8480 outs() << " BINDS_TO_WEAK";
8481 f
&= ~MachO::MH_BINDS_TO_WEAK
;
8483 if (f
& MachO::MH_ALLOW_STACK_EXECUTION
) {
8484 outs() << " ALLOW_STACK_EXECUTION";
8485 f
&= ~MachO::MH_ALLOW_STACK_EXECUTION
;
8487 if (f
& MachO::MH_DEAD_STRIPPABLE_DYLIB
) {
8488 outs() << " DEAD_STRIPPABLE_DYLIB";
8489 f
&= ~MachO::MH_DEAD_STRIPPABLE_DYLIB
;
8491 if (f
& MachO::MH_PIE
) {
8493 f
&= ~MachO::MH_PIE
;
8495 if (f
& MachO::MH_NO_REEXPORTED_DYLIBS
) {
8496 outs() << " NO_REEXPORTED_DYLIBS";
8497 f
&= ~MachO::MH_NO_REEXPORTED_DYLIBS
;
8499 if (f
& MachO::MH_HAS_TLV_DESCRIPTORS
) {
8500 outs() << " MH_HAS_TLV_DESCRIPTORS";
8501 f
&= ~MachO::MH_HAS_TLV_DESCRIPTORS
;
8503 if (f
& MachO::MH_NO_HEAP_EXECUTION
) {
8504 outs() << " MH_NO_HEAP_EXECUTION";
8505 f
&= ~MachO::MH_NO_HEAP_EXECUTION
;
8507 if (f
& MachO::MH_APP_EXTENSION_SAFE
) {
8508 outs() << " APP_EXTENSION_SAFE";
8509 f
&= ~MachO::MH_APP_EXTENSION_SAFE
;
8511 if (f
& MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO
) {
8512 outs() << " NLIST_OUTOFSYNC_WITH_DYLDINFO";
8513 f
&= ~MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO
;
8515 if (f
!= 0 || flags
== 0)
8516 outs() << format(" 0x%08" PRIx32
, f
);
8518 outs() << format(" 0x%08" PRIx32
, magic
);
8519 outs() << format(" %7d", cputype
);
8520 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
8521 outs() << format(" 0x%02" PRIx32
,
8522 (cpusubtype
& MachO::CPU_SUBTYPE_MASK
) >> 24);
8523 outs() << format(" %10u", filetype
);
8524 outs() << format(" %5u", ncmds
);
8525 outs() << format(" %10u", sizeofcmds
);
8526 outs() << format(" 0x%08" PRIx32
, flags
);
8531 static void PrintSegmentCommand(uint32_t cmd
, uint32_t cmdsize
,
8532 StringRef SegName
, uint64_t vmaddr
,
8533 uint64_t vmsize
, uint64_t fileoff
,
8534 uint64_t filesize
, uint32_t maxprot
,
8535 uint32_t initprot
, uint32_t nsects
,
8536 uint32_t flags
, uint32_t object_size
,
8538 uint64_t expected_cmdsize
;
8539 if (cmd
== MachO::LC_SEGMENT
) {
8540 outs() << " cmd LC_SEGMENT\n";
8541 expected_cmdsize
= nsects
;
8542 expected_cmdsize
*= sizeof(struct MachO::section
);
8543 expected_cmdsize
+= sizeof(struct MachO::segment_command
);
8545 outs() << " cmd LC_SEGMENT_64\n";
8546 expected_cmdsize
= nsects
;
8547 expected_cmdsize
*= sizeof(struct MachO::section_64
);
8548 expected_cmdsize
+= sizeof(struct MachO::segment_command_64
);
8550 outs() << " cmdsize " << cmdsize
;
8551 if (cmdsize
!= expected_cmdsize
)
8552 outs() << " Inconsistent size\n";
8555 outs() << " segname " << SegName
<< "\n";
8556 if (cmd
== MachO::LC_SEGMENT_64
) {
8557 outs() << " vmaddr " << format("0x%016" PRIx64
, vmaddr
) << "\n";
8558 outs() << " vmsize " << format("0x%016" PRIx64
, vmsize
) << "\n";
8560 outs() << " vmaddr " << format("0x%08" PRIx64
, vmaddr
) << "\n";
8561 outs() << " vmsize " << format("0x%08" PRIx64
, vmsize
) << "\n";
8563 outs() << " fileoff " << fileoff
;
8564 if (fileoff
> object_size
)
8565 outs() << " (past end of file)\n";
8568 outs() << " filesize " << filesize
;
8569 if (fileoff
+ filesize
> object_size
)
8570 outs() << " (past end of file)\n";
8575 ~(MachO::VM_PROT_READ
| MachO::VM_PROT_WRITE
|
8576 MachO::VM_PROT_EXECUTE
)) != 0)
8577 outs() << " maxprot ?" << format("0x%08" PRIx32
, maxprot
) << "\n";
8579 outs() << " maxprot ";
8580 outs() << ((maxprot
& MachO::VM_PROT_READ
) ? "r" : "-");
8581 outs() << ((maxprot
& MachO::VM_PROT_WRITE
) ? "w" : "-");
8582 outs() << ((maxprot
& MachO::VM_PROT_EXECUTE
) ? "x\n" : "-\n");
8585 ~(MachO::VM_PROT_READ
| MachO::VM_PROT_WRITE
|
8586 MachO::VM_PROT_EXECUTE
)) != 0)
8587 outs() << " initprot ?" << format("0x%08" PRIx32
, initprot
) << "\n";
8589 outs() << " initprot ";
8590 outs() << ((initprot
& MachO::VM_PROT_READ
) ? "r" : "-");
8591 outs() << ((initprot
& MachO::VM_PROT_WRITE
) ? "w" : "-");
8592 outs() << ((initprot
& MachO::VM_PROT_EXECUTE
) ? "x\n" : "-\n");
8595 outs() << " maxprot " << format("0x%08" PRIx32
, maxprot
) << "\n";
8596 outs() << " initprot " << format("0x%08" PRIx32
, initprot
) << "\n";
8598 outs() << " nsects " << nsects
<< "\n";
8602 outs() << " (none)\n";
8604 if (flags
& MachO::SG_HIGHVM
) {
8605 outs() << " HIGHVM";
8606 flags
&= ~MachO::SG_HIGHVM
;
8608 if (flags
& MachO::SG_FVMLIB
) {
8609 outs() << " FVMLIB";
8610 flags
&= ~MachO::SG_FVMLIB
;
8612 if (flags
& MachO::SG_NORELOC
) {
8613 outs() << " NORELOC";
8614 flags
&= ~MachO::SG_NORELOC
;
8616 if (flags
& MachO::SG_PROTECTED_VERSION_1
) {
8617 outs() << " PROTECTED_VERSION_1";
8618 flags
&= ~MachO::SG_PROTECTED_VERSION_1
;
8621 outs() << format(" 0x%08" PRIx32
, flags
) << " (unknown flags)\n";
8626 outs() << " flags " << format("0x%" PRIx32
, flags
) << "\n";
8630 static void PrintSection(const char *sectname
, const char *segname
,
8631 uint64_t addr
, uint64_t size
, uint32_t offset
,
8632 uint32_t align
, uint32_t reloff
, uint32_t nreloc
,
8633 uint32_t flags
, uint32_t reserved1
, uint32_t reserved2
,
8634 uint32_t cmd
, const char *sg_segname
,
8635 uint32_t filetype
, uint32_t object_size
,
8637 outs() << "Section\n";
8638 outs() << " sectname " << format("%.16s\n", sectname
);
8639 outs() << " segname " << format("%.16s", segname
);
8640 if (filetype
!= MachO::MH_OBJECT
&& strncmp(sg_segname
, segname
, 16) != 0)
8641 outs() << " (does not match segment)\n";
8644 if (cmd
== MachO::LC_SEGMENT_64
) {
8645 outs() << " addr " << format("0x%016" PRIx64
, addr
) << "\n";
8646 outs() << " size " << format("0x%016" PRIx64
, size
);
8648 outs() << " addr " << format("0x%08" PRIx64
, addr
) << "\n";
8649 outs() << " size " << format("0x%08" PRIx64
, size
);
8651 if ((flags
& MachO::S_ZEROFILL
) != 0 && offset
+ size
> object_size
)
8652 outs() << " (past end of file)\n";
8655 outs() << " offset " << offset
;
8656 if (offset
> object_size
)
8657 outs() << " (past end of file)\n";
8660 uint32_t align_shifted
= 1 << align
;
8661 outs() << " align 2^" << align
<< " (" << align_shifted
<< ")\n";
8662 outs() << " reloff " << reloff
;
8663 if (reloff
> object_size
)
8664 outs() << " (past end of file)\n";
8667 outs() << " nreloc " << nreloc
;
8668 if (reloff
+ nreloc
* sizeof(struct MachO::relocation_info
) > object_size
)
8669 outs() << " (past end of file)\n";
8672 uint32_t section_type
= flags
& MachO::SECTION_TYPE
;
8675 if (section_type
== MachO::S_REGULAR
)
8676 outs() << " S_REGULAR\n";
8677 else if (section_type
== MachO::S_ZEROFILL
)
8678 outs() << " S_ZEROFILL\n";
8679 else if (section_type
== MachO::S_CSTRING_LITERALS
)
8680 outs() << " S_CSTRING_LITERALS\n";
8681 else if (section_type
== MachO::S_4BYTE_LITERALS
)
8682 outs() << " S_4BYTE_LITERALS\n";
8683 else if (section_type
== MachO::S_8BYTE_LITERALS
)
8684 outs() << " S_8BYTE_LITERALS\n";
8685 else if (section_type
== MachO::S_16BYTE_LITERALS
)
8686 outs() << " S_16BYTE_LITERALS\n";
8687 else if (section_type
== MachO::S_LITERAL_POINTERS
)
8688 outs() << " S_LITERAL_POINTERS\n";
8689 else if (section_type
== MachO::S_NON_LAZY_SYMBOL_POINTERS
)
8690 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
8691 else if (section_type
== MachO::S_LAZY_SYMBOL_POINTERS
)
8692 outs() << " S_LAZY_SYMBOL_POINTERS\n";
8693 else if (section_type
== MachO::S_SYMBOL_STUBS
)
8694 outs() << " S_SYMBOL_STUBS\n";
8695 else if (section_type
== MachO::S_MOD_INIT_FUNC_POINTERS
)
8696 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
8697 else if (section_type
== MachO::S_MOD_TERM_FUNC_POINTERS
)
8698 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
8699 else if (section_type
== MachO::S_COALESCED
)
8700 outs() << " S_COALESCED\n";
8701 else if (section_type
== MachO::S_INTERPOSING
)
8702 outs() << " S_INTERPOSING\n";
8703 else if (section_type
== MachO::S_DTRACE_DOF
)
8704 outs() << " S_DTRACE_DOF\n";
8705 else if (section_type
== MachO::S_LAZY_DYLIB_SYMBOL_POINTERS
)
8706 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
8707 else if (section_type
== MachO::S_THREAD_LOCAL_REGULAR
)
8708 outs() << " S_THREAD_LOCAL_REGULAR\n";
8709 else if (section_type
== MachO::S_THREAD_LOCAL_ZEROFILL
)
8710 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
8711 else if (section_type
== MachO::S_THREAD_LOCAL_VARIABLES
)
8712 outs() << " S_THREAD_LOCAL_VARIABLES\n";
8713 else if (section_type
== MachO::S_THREAD_LOCAL_VARIABLE_POINTERS
)
8714 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
8715 else if (section_type
== MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
)
8716 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
8718 outs() << format("0x%08" PRIx32
, section_type
) << "\n";
8719 outs() << "attributes";
8720 uint32_t section_attributes
= flags
& MachO::SECTION_ATTRIBUTES
;
8721 if (section_attributes
& MachO::S_ATTR_PURE_INSTRUCTIONS
)
8722 outs() << " PURE_INSTRUCTIONS";
8723 if (section_attributes
& MachO::S_ATTR_NO_TOC
)
8724 outs() << " NO_TOC";
8725 if (section_attributes
& MachO::S_ATTR_STRIP_STATIC_SYMS
)
8726 outs() << " STRIP_STATIC_SYMS";
8727 if (section_attributes
& MachO::S_ATTR_NO_DEAD_STRIP
)
8728 outs() << " NO_DEAD_STRIP";
8729 if (section_attributes
& MachO::S_ATTR_LIVE_SUPPORT
)
8730 outs() << " LIVE_SUPPORT";
8731 if (section_attributes
& MachO::S_ATTR_SELF_MODIFYING_CODE
)
8732 outs() << " SELF_MODIFYING_CODE";
8733 if (section_attributes
& MachO::S_ATTR_DEBUG
)
8735 if (section_attributes
& MachO::S_ATTR_SOME_INSTRUCTIONS
)
8736 outs() << " SOME_INSTRUCTIONS";
8737 if (section_attributes
& MachO::S_ATTR_EXT_RELOC
)
8738 outs() << " EXT_RELOC";
8739 if (section_attributes
& MachO::S_ATTR_LOC_RELOC
)
8740 outs() << " LOC_RELOC";
8741 if (section_attributes
== 0)
8742 outs() << " (none)";
8745 outs() << " flags " << format("0x%08" PRIx32
, flags
) << "\n";
8746 outs() << " reserved1 " << reserved1
;
8747 if (section_type
== MachO::S_SYMBOL_STUBS
||
8748 section_type
== MachO::S_LAZY_SYMBOL_POINTERS
||
8749 section_type
== MachO::S_LAZY_DYLIB_SYMBOL_POINTERS
||
8750 section_type
== MachO::S_NON_LAZY_SYMBOL_POINTERS
||
8751 section_type
== MachO::S_THREAD_LOCAL_VARIABLE_POINTERS
)
8752 outs() << " (index into indirect symbol table)\n";
8755 outs() << " reserved2 " << reserved2
;
8756 if (section_type
== MachO::S_SYMBOL_STUBS
)
8757 outs() << " (size of stubs)\n";
8762 static void PrintSymtabLoadCommand(MachO::symtab_command st
, bool Is64Bit
,
8763 uint32_t object_size
) {
8764 outs() << " cmd LC_SYMTAB\n";
8765 outs() << " cmdsize " << st
.cmdsize
;
8766 if (st
.cmdsize
!= sizeof(struct MachO::symtab_command
))
8767 outs() << " Incorrect size\n";
8770 outs() << " symoff " << st
.symoff
;
8771 if (st
.symoff
> object_size
)
8772 outs() << " (past end of file)\n";
8775 outs() << " nsyms " << st
.nsyms
;
8778 big_size
= st
.nsyms
;
8779 big_size
*= sizeof(struct MachO::nlist_64
);
8780 big_size
+= st
.symoff
;
8781 if (big_size
> object_size
)
8782 outs() << " (past end of file)\n";
8786 big_size
= st
.nsyms
;
8787 big_size
*= sizeof(struct MachO::nlist
);
8788 big_size
+= st
.symoff
;
8789 if (big_size
> object_size
)
8790 outs() << " (past end of file)\n";
8794 outs() << " stroff " << st
.stroff
;
8795 if (st
.stroff
> object_size
)
8796 outs() << " (past end of file)\n";
8799 outs() << " strsize " << st
.strsize
;
8800 big_size
= st
.stroff
;
8801 big_size
+= st
.strsize
;
8802 if (big_size
> object_size
)
8803 outs() << " (past end of file)\n";
8808 static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst
,
8809 uint32_t nsyms
, uint32_t object_size
,
8811 outs() << " cmd LC_DYSYMTAB\n";
8812 outs() << " cmdsize " << dyst
.cmdsize
;
8813 if (dyst
.cmdsize
!= sizeof(struct MachO::dysymtab_command
))
8814 outs() << " Incorrect size\n";
8817 outs() << " ilocalsym " << dyst
.ilocalsym
;
8818 if (dyst
.ilocalsym
> nsyms
)
8819 outs() << " (greater than the number of symbols)\n";
8822 outs() << " nlocalsym " << dyst
.nlocalsym
;
8824 big_size
= dyst
.ilocalsym
;
8825 big_size
+= dyst
.nlocalsym
;
8826 if (big_size
> nsyms
)
8827 outs() << " (past the end of the symbol table)\n";
8830 outs() << " iextdefsym " << dyst
.iextdefsym
;
8831 if (dyst
.iextdefsym
> nsyms
)
8832 outs() << " (greater than the number of symbols)\n";
8835 outs() << " nextdefsym " << dyst
.nextdefsym
;
8836 big_size
= dyst
.iextdefsym
;
8837 big_size
+= dyst
.nextdefsym
;
8838 if (big_size
> nsyms
)
8839 outs() << " (past the end of the symbol table)\n";
8842 outs() << " iundefsym " << dyst
.iundefsym
;
8843 if (dyst
.iundefsym
> nsyms
)
8844 outs() << " (greater than the number of symbols)\n";
8847 outs() << " nundefsym " << dyst
.nundefsym
;
8848 big_size
= dyst
.iundefsym
;
8849 big_size
+= dyst
.nundefsym
;
8850 if (big_size
> nsyms
)
8851 outs() << " (past the end of the symbol table)\n";
8854 outs() << " tocoff " << dyst
.tocoff
;
8855 if (dyst
.tocoff
> object_size
)
8856 outs() << " (past end of file)\n";
8859 outs() << " ntoc " << dyst
.ntoc
;
8860 big_size
= dyst
.ntoc
;
8861 big_size
*= sizeof(struct MachO::dylib_table_of_contents
);
8862 big_size
+= dyst
.tocoff
;
8863 if (big_size
> object_size
)
8864 outs() << " (past end of file)\n";
8867 outs() << " modtaboff " << dyst
.modtaboff
;
8868 if (dyst
.modtaboff
> object_size
)
8869 outs() << " (past end of file)\n";
8872 outs() << " nmodtab " << dyst
.nmodtab
;
8875 modtabend
= dyst
.nmodtab
;
8876 modtabend
*= sizeof(struct MachO::dylib_module_64
);
8877 modtabend
+= dyst
.modtaboff
;
8879 modtabend
= dyst
.nmodtab
;
8880 modtabend
*= sizeof(struct MachO::dylib_module
);
8881 modtabend
+= dyst
.modtaboff
;
8883 if (modtabend
> object_size
)
8884 outs() << " (past end of file)\n";
8887 outs() << " extrefsymoff " << dyst
.extrefsymoff
;
8888 if (dyst
.extrefsymoff
> object_size
)
8889 outs() << " (past end of file)\n";
8892 outs() << " nextrefsyms " << dyst
.nextrefsyms
;
8893 big_size
= dyst
.nextrefsyms
;
8894 big_size
*= sizeof(struct MachO::dylib_reference
);
8895 big_size
+= dyst
.extrefsymoff
;
8896 if (big_size
> object_size
)
8897 outs() << " (past end of file)\n";
8900 outs() << " indirectsymoff " << dyst
.indirectsymoff
;
8901 if (dyst
.indirectsymoff
> object_size
)
8902 outs() << " (past end of file)\n";
8905 outs() << " nindirectsyms " << dyst
.nindirectsyms
;
8906 big_size
= dyst
.nindirectsyms
;
8907 big_size
*= sizeof(uint32_t);
8908 big_size
+= dyst
.indirectsymoff
;
8909 if (big_size
> object_size
)
8910 outs() << " (past end of file)\n";
8913 outs() << " extreloff " << dyst
.extreloff
;
8914 if (dyst
.extreloff
> object_size
)
8915 outs() << " (past end of file)\n";
8918 outs() << " nextrel " << dyst
.nextrel
;
8919 big_size
= dyst
.nextrel
;
8920 big_size
*= sizeof(struct MachO::relocation_info
);
8921 big_size
+= dyst
.extreloff
;
8922 if (big_size
> object_size
)
8923 outs() << " (past end of file)\n";
8926 outs() << " locreloff " << dyst
.locreloff
;
8927 if (dyst
.locreloff
> object_size
)
8928 outs() << " (past end of file)\n";
8931 outs() << " nlocrel " << dyst
.nlocrel
;
8932 big_size
= dyst
.nlocrel
;
8933 big_size
*= sizeof(struct MachO::relocation_info
);
8934 big_size
+= dyst
.locreloff
;
8935 if (big_size
> object_size
)
8936 outs() << " (past end of file)\n";
8941 static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc
,
8942 uint32_t object_size
) {
8943 if (dc
.cmd
== MachO::LC_DYLD_INFO
)
8944 outs() << " cmd LC_DYLD_INFO\n";
8946 outs() << " cmd LC_DYLD_INFO_ONLY\n";
8947 outs() << " cmdsize " << dc
.cmdsize
;
8948 if (dc
.cmdsize
!= sizeof(struct MachO::dyld_info_command
))
8949 outs() << " Incorrect size\n";
8952 outs() << " rebase_off " << dc
.rebase_off
;
8953 if (dc
.rebase_off
> object_size
)
8954 outs() << " (past end of file)\n";
8957 outs() << " rebase_size " << dc
.rebase_size
;
8959 big_size
= dc
.rebase_off
;
8960 big_size
+= dc
.rebase_size
;
8961 if (big_size
> object_size
)
8962 outs() << " (past end of file)\n";
8965 outs() << " bind_off " << dc
.bind_off
;
8966 if (dc
.bind_off
> object_size
)
8967 outs() << " (past end of file)\n";
8970 outs() << " bind_size " << dc
.bind_size
;
8971 big_size
= dc
.bind_off
;
8972 big_size
+= dc
.bind_size
;
8973 if (big_size
> object_size
)
8974 outs() << " (past end of file)\n";
8977 outs() << " weak_bind_off " << dc
.weak_bind_off
;
8978 if (dc
.weak_bind_off
> object_size
)
8979 outs() << " (past end of file)\n";
8982 outs() << " weak_bind_size " << dc
.weak_bind_size
;
8983 big_size
= dc
.weak_bind_off
;
8984 big_size
+= dc
.weak_bind_size
;
8985 if (big_size
> object_size
)
8986 outs() << " (past end of file)\n";
8989 outs() << " lazy_bind_off " << dc
.lazy_bind_off
;
8990 if (dc
.lazy_bind_off
> object_size
)
8991 outs() << " (past end of file)\n";
8994 outs() << " lazy_bind_size " << dc
.lazy_bind_size
;
8995 big_size
= dc
.lazy_bind_off
;
8996 big_size
+= dc
.lazy_bind_size
;
8997 if (big_size
> object_size
)
8998 outs() << " (past end of file)\n";
9001 outs() << " export_off " << dc
.export_off
;
9002 if (dc
.export_off
> object_size
)
9003 outs() << " (past end of file)\n";
9006 outs() << " export_size " << dc
.export_size
;
9007 big_size
= dc
.export_off
;
9008 big_size
+= dc
.export_size
;
9009 if (big_size
> object_size
)
9010 outs() << " (past end of file)\n";
9015 static void PrintDyldLoadCommand(MachO::dylinker_command dyld
,
9017 if (dyld
.cmd
== MachO::LC_ID_DYLINKER
)
9018 outs() << " cmd LC_ID_DYLINKER\n";
9019 else if (dyld
.cmd
== MachO::LC_LOAD_DYLINKER
)
9020 outs() << " cmd LC_LOAD_DYLINKER\n";
9021 else if (dyld
.cmd
== MachO::LC_DYLD_ENVIRONMENT
)
9022 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
9024 outs() << " cmd ?(" << dyld
.cmd
<< ")\n";
9025 outs() << " cmdsize " << dyld
.cmdsize
;
9026 if (dyld
.cmdsize
< sizeof(struct MachO::dylinker_command
))
9027 outs() << " Incorrect size\n";
9030 if (dyld
.name
>= dyld
.cmdsize
)
9031 outs() << " name ?(bad offset " << dyld
.name
<< ")\n";
9033 const char *P
= (const char *)(Ptr
) + dyld
.name
;
9034 outs() << " name " << P
<< " (offset " << dyld
.name
<< ")\n";
9038 static void PrintUuidLoadCommand(MachO::uuid_command uuid
) {
9039 outs() << " cmd LC_UUID\n";
9040 outs() << " cmdsize " << uuid
.cmdsize
;
9041 if (uuid
.cmdsize
!= sizeof(struct MachO::uuid_command
))
9042 outs() << " Incorrect size\n";
9046 for (int i
= 0; i
< 16; ++i
) {
9047 outs() << format("%02" PRIX32
, uuid
.uuid
[i
]);
9048 if (i
== 3 || i
== 5 || i
== 7 || i
== 9)
9054 static void PrintRpathLoadCommand(MachO::rpath_command rpath
, const char *Ptr
) {
9055 outs() << " cmd LC_RPATH\n";
9056 outs() << " cmdsize " << rpath
.cmdsize
;
9057 if (rpath
.cmdsize
< sizeof(struct MachO::rpath_command
))
9058 outs() << " Incorrect size\n";
9061 if (rpath
.path
>= rpath
.cmdsize
)
9062 outs() << " path ?(bad offset " << rpath
.path
<< ")\n";
9064 const char *P
= (const char *)(Ptr
) + rpath
.path
;
9065 outs() << " path " << P
<< " (offset " << rpath
.path
<< ")\n";
9069 static void PrintVersionMinLoadCommand(MachO::version_min_command vd
) {
9070 StringRef LoadCmdName
;
9072 case MachO::LC_VERSION_MIN_MACOSX
:
9073 LoadCmdName
= "LC_VERSION_MIN_MACOSX";
9075 case MachO::LC_VERSION_MIN_IPHONEOS
:
9076 LoadCmdName
= "LC_VERSION_MIN_IPHONEOS";
9078 case MachO::LC_VERSION_MIN_TVOS
:
9079 LoadCmdName
= "LC_VERSION_MIN_TVOS";
9081 case MachO::LC_VERSION_MIN_WATCHOS
:
9082 LoadCmdName
= "LC_VERSION_MIN_WATCHOS";
9085 llvm_unreachable("Unknown version min load command");
9088 outs() << " cmd " << LoadCmdName
<< '\n';
9089 outs() << " cmdsize " << vd
.cmdsize
;
9090 if (vd
.cmdsize
!= sizeof(struct MachO::version_min_command
))
9091 outs() << " Incorrect size\n";
9094 outs() << " version "
9095 << MachOObjectFile::getVersionMinMajor(vd
, false) << "."
9096 << MachOObjectFile::getVersionMinMinor(vd
, false);
9097 uint32_t Update
= MachOObjectFile::getVersionMinUpdate(vd
, false);
9099 outs() << "." << Update
;
9102 outs() << " sdk n/a";
9105 << MachOObjectFile::getVersionMinMajor(vd
, true) << "."
9106 << MachOObjectFile::getVersionMinMinor(vd
, true);
9108 Update
= MachOObjectFile::getVersionMinUpdate(vd
, true);
9110 outs() << "." << Update
;
9114 static void PrintNoteLoadCommand(MachO::note_command Nt
) {
9115 outs() << " cmd LC_NOTE\n";
9116 outs() << " cmdsize " << Nt
.cmdsize
;
9117 if (Nt
.cmdsize
!= sizeof(struct MachO::note_command
))
9118 outs() << " Incorrect size\n";
9121 const char *d
= Nt
.data_owner
;
9122 outs() << "data_owner " << format("%.16s\n", d
);
9123 outs() << " offset " << Nt
.offset
<< "\n";
9124 outs() << " size " << Nt
.size
<< "\n";
9127 static void PrintBuildToolVersion(MachO::build_tool_version bv
) {
9128 outs() << " tool " << MachOObjectFile::getBuildTool(bv
.tool
) << "\n";
9129 outs() << " version " << MachOObjectFile::getVersionString(bv
.version
)
9133 static void PrintBuildVersionLoadCommand(const MachOObjectFile
*obj
,
9134 MachO::build_version_command bd
) {
9135 outs() << " cmd LC_BUILD_VERSION\n";
9136 outs() << " cmdsize " << bd
.cmdsize
;
9138 sizeof(struct MachO::build_version_command
) +
9139 bd
.ntools
* sizeof(struct MachO::build_tool_version
))
9140 outs() << " Incorrect size\n";
9143 outs() << " platform " << MachOObjectFile::getBuildPlatform(bd
.platform
)
9146 outs() << " sdk " << MachOObjectFile::getVersionString(bd
.sdk
)
9149 outs() << " sdk n/a\n";
9150 outs() << " minos " << MachOObjectFile::getVersionString(bd
.minos
)
9152 outs() << " ntools " << bd
.ntools
<< "\n";
9153 for (unsigned i
= 0; i
< bd
.ntools
; ++i
) {
9154 MachO::build_tool_version bv
= obj
->getBuildToolVersion(i
);
9155 PrintBuildToolVersion(bv
);
9159 static void PrintSourceVersionCommand(MachO::source_version_command sd
) {
9160 outs() << " cmd LC_SOURCE_VERSION\n";
9161 outs() << " cmdsize " << sd
.cmdsize
;
9162 if (sd
.cmdsize
!= sizeof(struct MachO::source_version_command
))
9163 outs() << " Incorrect size\n";
9166 uint64_t a
= (sd
.version
>> 40) & 0xffffff;
9167 uint64_t b
= (sd
.version
>> 30) & 0x3ff;
9168 uint64_t c
= (sd
.version
>> 20) & 0x3ff;
9169 uint64_t d
= (sd
.version
>> 10) & 0x3ff;
9170 uint64_t e
= sd
.version
& 0x3ff;
9171 outs() << " version " << a
<< "." << b
;
9173 outs() << "." << c
<< "." << d
<< "." << e
;
9175 outs() << "." << c
<< "." << d
;
9181 static void PrintEntryPointCommand(MachO::entry_point_command ep
) {
9182 outs() << " cmd LC_MAIN\n";
9183 outs() << " cmdsize " << ep
.cmdsize
;
9184 if (ep
.cmdsize
!= sizeof(struct MachO::entry_point_command
))
9185 outs() << " Incorrect size\n";
9188 outs() << " entryoff " << ep
.entryoff
<< "\n";
9189 outs() << " stacksize " << ep
.stacksize
<< "\n";
9192 static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec
,
9193 uint32_t object_size
) {
9194 outs() << " cmd LC_ENCRYPTION_INFO\n";
9195 outs() << " cmdsize " << ec
.cmdsize
;
9196 if (ec
.cmdsize
!= sizeof(struct MachO::encryption_info_command
))
9197 outs() << " Incorrect size\n";
9200 outs() << " cryptoff " << ec
.cryptoff
;
9201 if (ec
.cryptoff
> object_size
)
9202 outs() << " (past end of file)\n";
9205 outs() << " cryptsize " << ec
.cryptsize
;
9206 if (ec
.cryptsize
> object_size
)
9207 outs() << " (past end of file)\n";
9210 outs() << " cryptid " << ec
.cryptid
<< "\n";
9213 static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec
,
9214 uint32_t object_size
) {
9215 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
9216 outs() << " cmdsize " << ec
.cmdsize
;
9217 if (ec
.cmdsize
!= sizeof(struct MachO::encryption_info_command_64
))
9218 outs() << " Incorrect size\n";
9221 outs() << " cryptoff " << ec
.cryptoff
;
9222 if (ec
.cryptoff
> object_size
)
9223 outs() << " (past end of file)\n";
9226 outs() << " cryptsize " << ec
.cryptsize
;
9227 if (ec
.cryptsize
> object_size
)
9228 outs() << " (past end of file)\n";
9231 outs() << " cryptid " << ec
.cryptid
<< "\n";
9232 outs() << " pad " << ec
.pad
<< "\n";
9235 static void PrintLinkerOptionCommand(MachO::linker_option_command lo
,
9237 outs() << " cmd LC_LINKER_OPTION\n";
9238 outs() << " cmdsize " << lo
.cmdsize
;
9239 if (lo
.cmdsize
< sizeof(struct MachO::linker_option_command
))
9240 outs() << " Incorrect size\n";
9243 outs() << " count " << lo
.count
<< "\n";
9244 const char *string
= Ptr
+ sizeof(struct MachO::linker_option_command
);
9245 uint32_t left
= lo
.cmdsize
- sizeof(struct MachO::linker_option_command
);
9248 while (*string
== '\0' && left
> 0) {
9254 outs() << " string #" << i
<< " " << format("%.*s\n", left
, string
);
9255 uint32_t NullPos
= StringRef(string
, left
).find('\0');
9256 uint32_t len
= std::min(NullPos
, left
) + 1;
9262 outs() << " count " << lo
.count
<< " does not match number of strings "
9266 static void PrintSubFrameworkCommand(MachO::sub_framework_command sub
,
9268 outs() << " cmd LC_SUB_FRAMEWORK\n";
9269 outs() << " cmdsize " << sub
.cmdsize
;
9270 if (sub
.cmdsize
< sizeof(struct MachO::sub_framework_command
))
9271 outs() << " Incorrect size\n";
9274 if (sub
.umbrella
< sub
.cmdsize
) {
9275 const char *P
= Ptr
+ sub
.umbrella
;
9276 outs() << " umbrella " << P
<< " (offset " << sub
.umbrella
<< ")\n";
9278 outs() << " umbrella ?(bad offset " << sub
.umbrella
<< ")\n";
9282 static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub
,
9284 outs() << " cmd LC_SUB_UMBRELLA\n";
9285 outs() << " cmdsize " << sub
.cmdsize
;
9286 if (sub
.cmdsize
< sizeof(struct MachO::sub_umbrella_command
))
9287 outs() << " Incorrect size\n";
9290 if (sub
.sub_umbrella
< sub
.cmdsize
) {
9291 const char *P
= Ptr
+ sub
.sub_umbrella
;
9292 outs() << " sub_umbrella " << P
<< " (offset " << sub
.sub_umbrella
<< ")\n";
9294 outs() << " sub_umbrella ?(bad offset " << sub
.sub_umbrella
<< ")\n";
9298 static void PrintSubLibraryCommand(MachO::sub_library_command sub
,
9300 outs() << " cmd LC_SUB_LIBRARY\n";
9301 outs() << " cmdsize " << sub
.cmdsize
;
9302 if (sub
.cmdsize
< sizeof(struct MachO::sub_library_command
))
9303 outs() << " Incorrect size\n";
9306 if (sub
.sub_library
< sub
.cmdsize
) {
9307 const char *P
= Ptr
+ sub
.sub_library
;
9308 outs() << " sub_library " << P
<< " (offset " << sub
.sub_library
<< ")\n";
9310 outs() << " sub_library ?(bad offset " << sub
.sub_library
<< ")\n";
9314 static void PrintSubClientCommand(MachO::sub_client_command sub
,
9316 outs() << " cmd LC_SUB_CLIENT\n";
9317 outs() << " cmdsize " << sub
.cmdsize
;
9318 if (sub
.cmdsize
< sizeof(struct MachO::sub_client_command
))
9319 outs() << " Incorrect size\n";
9322 if (sub
.client
< sub
.cmdsize
) {
9323 const char *P
= Ptr
+ sub
.client
;
9324 outs() << " client " << P
<< " (offset " << sub
.client
<< ")\n";
9326 outs() << " client ?(bad offset " << sub
.client
<< ")\n";
9330 static void PrintRoutinesCommand(MachO::routines_command r
) {
9331 outs() << " cmd LC_ROUTINES\n";
9332 outs() << " cmdsize " << r
.cmdsize
;
9333 if (r
.cmdsize
!= sizeof(struct MachO::routines_command
))
9334 outs() << " Incorrect size\n";
9337 outs() << " init_address " << format("0x%08" PRIx32
, r
.init_address
) << "\n";
9338 outs() << " init_module " << r
.init_module
<< "\n";
9339 outs() << " reserved1 " << r
.reserved1
<< "\n";
9340 outs() << " reserved2 " << r
.reserved2
<< "\n";
9341 outs() << " reserved3 " << r
.reserved3
<< "\n";
9342 outs() << " reserved4 " << r
.reserved4
<< "\n";
9343 outs() << " reserved5 " << r
.reserved5
<< "\n";
9344 outs() << " reserved6 " << r
.reserved6
<< "\n";
9347 static void PrintRoutinesCommand64(MachO::routines_command_64 r
) {
9348 outs() << " cmd LC_ROUTINES_64\n";
9349 outs() << " cmdsize " << r
.cmdsize
;
9350 if (r
.cmdsize
!= sizeof(struct MachO::routines_command_64
))
9351 outs() << " Incorrect size\n";
9354 outs() << " init_address " << format("0x%016" PRIx64
, r
.init_address
) << "\n";
9355 outs() << " init_module " << r
.init_module
<< "\n";
9356 outs() << " reserved1 " << r
.reserved1
<< "\n";
9357 outs() << " reserved2 " << r
.reserved2
<< "\n";
9358 outs() << " reserved3 " << r
.reserved3
<< "\n";
9359 outs() << " reserved4 " << r
.reserved4
<< "\n";
9360 outs() << " reserved5 " << r
.reserved5
<< "\n";
9361 outs() << " reserved6 " << r
.reserved6
<< "\n";
9364 static void Print_x86_thread_state32_t(MachO::x86_thread_state32_t
&cpu32
) {
9365 outs() << "\t eax " << format("0x%08" PRIx32
, cpu32
.eax
);
9366 outs() << " ebx " << format("0x%08" PRIx32
, cpu32
.ebx
);
9367 outs() << " ecx " << format("0x%08" PRIx32
, cpu32
.ecx
);
9368 outs() << " edx " << format("0x%08" PRIx32
, cpu32
.edx
) << "\n";
9369 outs() << "\t edi " << format("0x%08" PRIx32
, cpu32
.edi
);
9370 outs() << " esi " << format("0x%08" PRIx32
, cpu32
.esi
);
9371 outs() << " ebp " << format("0x%08" PRIx32
, cpu32
.ebp
);
9372 outs() << " esp " << format("0x%08" PRIx32
, cpu32
.esp
) << "\n";
9373 outs() << "\t ss " << format("0x%08" PRIx32
, cpu32
.ss
);
9374 outs() << " eflags " << format("0x%08" PRIx32
, cpu32
.eflags
);
9375 outs() << " eip " << format("0x%08" PRIx32
, cpu32
.eip
);
9376 outs() << " cs " << format("0x%08" PRIx32
, cpu32
.cs
) << "\n";
9377 outs() << "\t ds " << format("0x%08" PRIx32
, cpu32
.ds
);
9378 outs() << " es " << format("0x%08" PRIx32
, cpu32
.es
);
9379 outs() << " fs " << format("0x%08" PRIx32
, cpu32
.fs
);
9380 outs() << " gs " << format("0x%08" PRIx32
, cpu32
.gs
) << "\n";
9383 static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t
&cpu64
) {
9384 outs() << " rax " << format("0x%016" PRIx64
, cpu64
.rax
);
9385 outs() << " rbx " << format("0x%016" PRIx64
, cpu64
.rbx
);
9386 outs() << " rcx " << format("0x%016" PRIx64
, cpu64
.rcx
) << "\n";
9387 outs() << " rdx " << format("0x%016" PRIx64
, cpu64
.rdx
);
9388 outs() << " rdi " << format("0x%016" PRIx64
, cpu64
.rdi
);
9389 outs() << " rsi " << format("0x%016" PRIx64
, cpu64
.rsi
) << "\n";
9390 outs() << " rbp " << format("0x%016" PRIx64
, cpu64
.rbp
);
9391 outs() << " rsp " << format("0x%016" PRIx64
, cpu64
.rsp
);
9392 outs() << " r8 " << format("0x%016" PRIx64
, cpu64
.r8
) << "\n";
9393 outs() << " r9 " << format("0x%016" PRIx64
, cpu64
.r9
);
9394 outs() << " r10 " << format("0x%016" PRIx64
, cpu64
.r10
);
9395 outs() << " r11 " << format("0x%016" PRIx64
, cpu64
.r11
) << "\n";
9396 outs() << " r12 " << format("0x%016" PRIx64
, cpu64
.r12
);
9397 outs() << " r13 " << format("0x%016" PRIx64
, cpu64
.r13
);
9398 outs() << " r14 " << format("0x%016" PRIx64
, cpu64
.r14
) << "\n";
9399 outs() << " r15 " << format("0x%016" PRIx64
, cpu64
.r15
);
9400 outs() << " rip " << format("0x%016" PRIx64
, cpu64
.rip
) << "\n";
9401 outs() << "rflags " << format("0x%016" PRIx64
, cpu64
.rflags
);
9402 outs() << " cs " << format("0x%016" PRIx64
, cpu64
.cs
);
9403 outs() << " fs " << format("0x%016" PRIx64
, cpu64
.fs
) << "\n";
9404 outs() << " gs " << format("0x%016" PRIx64
, cpu64
.gs
) << "\n";
9407 static void Print_mmst_reg(MachO::mmst_reg_t
&r
) {
9409 outs() << "\t mmst_reg ";
9410 for (f
= 0; f
< 10; f
++)
9411 outs() << format("%02" PRIx32
, (r
.mmst_reg
[f
] & 0xff)) << " ";
9413 outs() << "\t mmst_rsrv ";
9414 for (f
= 0; f
< 6; f
++)
9415 outs() << format("%02" PRIx32
, (r
.mmst_rsrv
[f
] & 0xff)) << " ";
9419 static void Print_xmm_reg(MachO::xmm_reg_t
&r
) {
9421 outs() << "\t xmm_reg ";
9422 for (f
= 0; f
< 16; f
++)
9423 outs() << format("%02" PRIx32
, (r
.xmm_reg
[f
] & 0xff)) << " ";
9427 static void Print_x86_float_state_t(MachO::x86_float_state64_t
&fpu
) {
9428 outs() << "\t fpu_reserved[0] " << fpu
.fpu_reserved
[0];
9429 outs() << " fpu_reserved[1] " << fpu
.fpu_reserved
[1] << "\n";
9430 outs() << "\t control: invalid " << fpu
.fpu_fcw
.invalid
;
9431 outs() << " denorm " << fpu
.fpu_fcw
.denorm
;
9432 outs() << " zdiv " << fpu
.fpu_fcw
.zdiv
;
9433 outs() << " ovrfl " << fpu
.fpu_fcw
.ovrfl
;
9434 outs() << " undfl " << fpu
.fpu_fcw
.undfl
;
9435 outs() << " precis " << fpu
.fpu_fcw
.precis
<< "\n";
9436 outs() << "\t\t pc ";
9437 if (fpu
.fpu_fcw
.pc
== MachO::x86_FP_PREC_24B
)
9438 outs() << "FP_PREC_24B ";
9439 else if (fpu
.fpu_fcw
.pc
== MachO::x86_FP_PREC_53B
)
9440 outs() << "FP_PREC_53B ";
9441 else if (fpu
.fpu_fcw
.pc
== MachO::x86_FP_PREC_64B
)
9442 outs() << "FP_PREC_64B ";
9444 outs() << fpu
.fpu_fcw
.pc
<< " ";
9446 if (fpu
.fpu_fcw
.rc
== MachO::x86_FP_RND_NEAR
)
9447 outs() << "FP_RND_NEAR ";
9448 else if (fpu
.fpu_fcw
.rc
== MachO::x86_FP_RND_DOWN
)
9449 outs() << "FP_RND_DOWN ";
9450 else if (fpu
.fpu_fcw
.rc
== MachO::x86_FP_RND_UP
)
9451 outs() << "FP_RND_UP ";
9452 else if (fpu
.fpu_fcw
.rc
== MachO::x86_FP_CHOP
)
9453 outs() << "FP_CHOP ";
9455 outs() << "\t status: invalid " << fpu
.fpu_fsw
.invalid
;
9456 outs() << " denorm " << fpu
.fpu_fsw
.denorm
;
9457 outs() << " zdiv " << fpu
.fpu_fsw
.zdiv
;
9458 outs() << " ovrfl " << fpu
.fpu_fsw
.ovrfl
;
9459 outs() << " undfl " << fpu
.fpu_fsw
.undfl
;
9460 outs() << " precis " << fpu
.fpu_fsw
.precis
;
9461 outs() << " stkflt " << fpu
.fpu_fsw
.stkflt
<< "\n";
9462 outs() << "\t errsumm " << fpu
.fpu_fsw
.errsumm
;
9463 outs() << " c0 " << fpu
.fpu_fsw
.c0
;
9464 outs() << " c1 " << fpu
.fpu_fsw
.c1
;
9465 outs() << " c2 " << fpu
.fpu_fsw
.c2
;
9466 outs() << " tos " << fpu
.fpu_fsw
.tos
;
9467 outs() << " c3 " << fpu
.fpu_fsw
.c3
;
9468 outs() << " busy " << fpu
.fpu_fsw
.busy
<< "\n";
9469 outs() << "\t fpu_ftw " << format("0x%02" PRIx32
, fpu
.fpu_ftw
);
9470 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32
, fpu
.fpu_rsrv1
);
9471 outs() << " fpu_fop " << format("0x%04" PRIx32
, fpu
.fpu_fop
);
9472 outs() << " fpu_ip " << format("0x%08" PRIx32
, fpu
.fpu_ip
) << "\n";
9473 outs() << "\t fpu_cs " << format("0x%04" PRIx32
, fpu
.fpu_cs
);
9474 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32
, fpu
.fpu_rsrv2
);
9475 outs() << " fpu_dp " << format("0x%08" PRIx32
, fpu
.fpu_dp
);
9476 outs() << " fpu_ds " << format("0x%04" PRIx32
, fpu
.fpu_ds
) << "\n";
9477 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32
, fpu
.fpu_rsrv3
);
9478 outs() << " fpu_mxcsr " << format("0x%08" PRIx32
, fpu
.fpu_mxcsr
);
9479 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32
, fpu
.fpu_mxcsrmask
);
9481 outs() << "\t fpu_stmm0:\n";
9482 Print_mmst_reg(fpu
.fpu_stmm0
);
9483 outs() << "\t fpu_stmm1:\n";
9484 Print_mmst_reg(fpu
.fpu_stmm1
);
9485 outs() << "\t fpu_stmm2:\n";
9486 Print_mmst_reg(fpu
.fpu_stmm2
);
9487 outs() << "\t fpu_stmm3:\n";
9488 Print_mmst_reg(fpu
.fpu_stmm3
);
9489 outs() << "\t fpu_stmm4:\n";
9490 Print_mmst_reg(fpu
.fpu_stmm4
);
9491 outs() << "\t fpu_stmm5:\n";
9492 Print_mmst_reg(fpu
.fpu_stmm5
);
9493 outs() << "\t fpu_stmm6:\n";
9494 Print_mmst_reg(fpu
.fpu_stmm6
);
9495 outs() << "\t fpu_stmm7:\n";
9496 Print_mmst_reg(fpu
.fpu_stmm7
);
9497 outs() << "\t fpu_xmm0:\n";
9498 Print_xmm_reg(fpu
.fpu_xmm0
);
9499 outs() << "\t fpu_xmm1:\n";
9500 Print_xmm_reg(fpu
.fpu_xmm1
);
9501 outs() << "\t fpu_xmm2:\n";
9502 Print_xmm_reg(fpu
.fpu_xmm2
);
9503 outs() << "\t fpu_xmm3:\n";
9504 Print_xmm_reg(fpu
.fpu_xmm3
);
9505 outs() << "\t fpu_xmm4:\n";
9506 Print_xmm_reg(fpu
.fpu_xmm4
);
9507 outs() << "\t fpu_xmm5:\n";
9508 Print_xmm_reg(fpu
.fpu_xmm5
);
9509 outs() << "\t fpu_xmm6:\n";
9510 Print_xmm_reg(fpu
.fpu_xmm6
);
9511 outs() << "\t fpu_xmm7:\n";
9512 Print_xmm_reg(fpu
.fpu_xmm7
);
9513 outs() << "\t fpu_xmm8:\n";
9514 Print_xmm_reg(fpu
.fpu_xmm8
);
9515 outs() << "\t fpu_xmm9:\n";
9516 Print_xmm_reg(fpu
.fpu_xmm9
);
9517 outs() << "\t fpu_xmm10:\n";
9518 Print_xmm_reg(fpu
.fpu_xmm10
);
9519 outs() << "\t fpu_xmm11:\n";
9520 Print_xmm_reg(fpu
.fpu_xmm11
);
9521 outs() << "\t fpu_xmm12:\n";
9522 Print_xmm_reg(fpu
.fpu_xmm12
);
9523 outs() << "\t fpu_xmm13:\n";
9524 Print_xmm_reg(fpu
.fpu_xmm13
);
9525 outs() << "\t fpu_xmm14:\n";
9526 Print_xmm_reg(fpu
.fpu_xmm14
);
9527 outs() << "\t fpu_xmm15:\n";
9528 Print_xmm_reg(fpu
.fpu_xmm15
);
9529 outs() << "\t fpu_rsrv4:\n";
9530 for (uint32_t f
= 0; f
< 6; f
++) {
9532 for (uint32_t g
= 0; g
< 16; g
++)
9533 outs() << format("%02" PRIx32
, fpu
.fpu_rsrv4
[f
* g
]) << " ";
9536 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32
, fpu
.fpu_reserved1
);
9540 static void Print_x86_exception_state_t(MachO::x86_exception_state64_t
&exc64
) {
9541 outs() << "\t trapno " << format("0x%08" PRIx32
, exc64
.trapno
);
9542 outs() << " err " << format("0x%08" PRIx32
, exc64
.err
);
9543 outs() << " faultvaddr " << format("0x%016" PRIx64
, exc64
.faultvaddr
) << "\n";
9546 static void Print_arm_thread_state32_t(MachO::arm_thread_state32_t
&cpu32
) {
9547 outs() << "\t r0 " << format("0x%08" PRIx32
, cpu32
.r
[0]);
9548 outs() << " r1 " << format("0x%08" PRIx32
, cpu32
.r
[1]);
9549 outs() << " r2 " << format("0x%08" PRIx32
, cpu32
.r
[2]);
9550 outs() << " r3 " << format("0x%08" PRIx32
, cpu32
.r
[3]) << "\n";
9551 outs() << "\t r4 " << format("0x%08" PRIx32
, cpu32
.r
[4]);
9552 outs() << " r5 " << format("0x%08" PRIx32
, cpu32
.r
[5]);
9553 outs() << " r6 " << format("0x%08" PRIx32
, cpu32
.r
[6]);
9554 outs() << " r7 " << format("0x%08" PRIx32
, cpu32
.r
[7]) << "\n";
9555 outs() << "\t r8 " << format("0x%08" PRIx32
, cpu32
.r
[8]);
9556 outs() << " r9 " << format("0x%08" PRIx32
, cpu32
.r
[9]);
9557 outs() << " r10 " << format("0x%08" PRIx32
, cpu32
.r
[10]);
9558 outs() << " r11 " << format("0x%08" PRIx32
, cpu32
.r
[11]) << "\n";
9559 outs() << "\t r12 " << format("0x%08" PRIx32
, cpu32
.r
[12]);
9560 outs() << " sp " << format("0x%08" PRIx32
, cpu32
.sp
);
9561 outs() << " lr " << format("0x%08" PRIx32
, cpu32
.lr
);
9562 outs() << " pc " << format("0x%08" PRIx32
, cpu32
.pc
) << "\n";
9563 outs() << "\t cpsr " << format("0x%08" PRIx32
, cpu32
.cpsr
) << "\n";
9566 static void Print_arm_thread_state64_t(MachO::arm_thread_state64_t
&cpu64
) {
9567 outs() << "\t x0 " << format("0x%016" PRIx64
, cpu64
.x
[0]);
9568 outs() << " x1 " << format("0x%016" PRIx64
, cpu64
.x
[1]);
9569 outs() << " x2 " << format("0x%016" PRIx64
, cpu64
.x
[2]) << "\n";
9570 outs() << "\t x3 " << format("0x%016" PRIx64
, cpu64
.x
[3]);
9571 outs() << " x4 " << format("0x%016" PRIx64
, cpu64
.x
[4]);
9572 outs() << " x5 " << format("0x%016" PRIx64
, cpu64
.x
[5]) << "\n";
9573 outs() << "\t x6 " << format("0x%016" PRIx64
, cpu64
.x
[6]);
9574 outs() << " x7 " << format("0x%016" PRIx64
, cpu64
.x
[7]);
9575 outs() << " x8 " << format("0x%016" PRIx64
, cpu64
.x
[8]) << "\n";
9576 outs() << "\t x9 " << format("0x%016" PRIx64
, cpu64
.x
[9]);
9577 outs() << " x10 " << format("0x%016" PRIx64
, cpu64
.x
[10]);
9578 outs() << " x11 " << format("0x%016" PRIx64
, cpu64
.x
[11]) << "\n";
9579 outs() << "\t x12 " << format("0x%016" PRIx64
, cpu64
.x
[12]);
9580 outs() << " x13 " << format("0x%016" PRIx64
, cpu64
.x
[13]);
9581 outs() << " x14 " << format("0x%016" PRIx64
, cpu64
.x
[14]) << "\n";
9582 outs() << "\t x15 " << format("0x%016" PRIx64
, cpu64
.x
[15]);
9583 outs() << " x16 " << format("0x%016" PRIx64
, cpu64
.x
[16]);
9584 outs() << " x17 " << format("0x%016" PRIx64
, cpu64
.x
[17]) << "\n";
9585 outs() << "\t x18 " << format("0x%016" PRIx64
, cpu64
.x
[18]);
9586 outs() << " x19 " << format("0x%016" PRIx64
, cpu64
.x
[19]);
9587 outs() << " x20 " << format("0x%016" PRIx64
, cpu64
.x
[20]) << "\n";
9588 outs() << "\t x21 " << format("0x%016" PRIx64
, cpu64
.x
[21]);
9589 outs() << " x22 " << format("0x%016" PRIx64
, cpu64
.x
[22]);
9590 outs() << " x23 " << format("0x%016" PRIx64
, cpu64
.x
[23]) << "\n";
9591 outs() << "\t x24 " << format("0x%016" PRIx64
, cpu64
.x
[24]);
9592 outs() << " x25 " << format("0x%016" PRIx64
, cpu64
.x
[25]);
9593 outs() << " x26 " << format("0x%016" PRIx64
, cpu64
.x
[26]) << "\n";
9594 outs() << "\t x27 " << format("0x%016" PRIx64
, cpu64
.x
[27]);
9595 outs() << " x28 " << format("0x%016" PRIx64
, cpu64
.x
[28]);
9596 outs() << " fp " << format("0x%016" PRIx64
, cpu64
.fp
) << "\n";
9597 outs() << "\t lr " << format("0x%016" PRIx64
, cpu64
.lr
);
9598 outs() << " sp " << format("0x%016" PRIx64
, cpu64
.sp
);
9599 outs() << " pc " << format("0x%016" PRIx64
, cpu64
.pc
) << "\n";
9600 outs() << "\t cpsr " << format("0x%08" PRIx32
, cpu64
.cpsr
) << "\n";
9603 static void PrintThreadCommand(MachO::thread_command t
, const char *Ptr
,
9604 bool isLittleEndian
, uint32_t cputype
) {
9605 if (t
.cmd
== MachO::LC_THREAD
)
9606 outs() << " cmd LC_THREAD\n";
9607 else if (t
.cmd
== MachO::LC_UNIXTHREAD
)
9608 outs() << " cmd LC_UNIXTHREAD\n";
9610 outs() << " cmd " << t
.cmd
<< " (unknown)\n";
9611 outs() << " cmdsize " << t
.cmdsize
;
9612 if (t
.cmdsize
< sizeof(struct MachO::thread_command
) + 2 * sizeof(uint32_t))
9613 outs() << " Incorrect size\n";
9617 const char *begin
= Ptr
+ sizeof(struct MachO::thread_command
);
9618 const char *end
= Ptr
+ t
.cmdsize
;
9619 uint32_t flavor
, count
, left
;
9620 if (cputype
== MachO::CPU_TYPE_I386
) {
9621 while (begin
< end
) {
9622 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9623 memcpy((char *)&flavor
, begin
, sizeof(uint32_t));
9624 begin
+= sizeof(uint32_t);
9629 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9630 sys::swapByteOrder(flavor
);
9631 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9632 memcpy((char *)&count
, begin
, sizeof(uint32_t));
9633 begin
+= sizeof(uint32_t);
9638 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9639 sys::swapByteOrder(count
);
9640 if (flavor
== MachO::x86_THREAD_STATE32
) {
9641 outs() << " flavor i386_THREAD_STATE\n";
9642 if (count
== MachO::x86_THREAD_STATE32_COUNT
)
9643 outs() << " count i386_THREAD_STATE_COUNT\n";
9645 outs() << " count " << count
9646 << " (not x86_THREAD_STATE32_COUNT)\n";
9647 MachO::x86_thread_state32_t cpu32
;
9649 if (left
>= sizeof(MachO::x86_thread_state32_t
)) {
9650 memcpy(&cpu32
, begin
, sizeof(MachO::x86_thread_state32_t
));
9651 begin
+= sizeof(MachO::x86_thread_state32_t
);
9653 memset(&cpu32
, '\0', sizeof(MachO::x86_thread_state32_t
));
9654 memcpy(&cpu32
, begin
, left
);
9657 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9659 Print_x86_thread_state32_t(cpu32
);
9660 } else if (flavor
== MachO::x86_THREAD_STATE
) {
9661 outs() << " flavor x86_THREAD_STATE\n";
9662 if (count
== MachO::x86_THREAD_STATE_COUNT
)
9663 outs() << " count x86_THREAD_STATE_COUNT\n";
9665 outs() << " count " << count
9666 << " (not x86_THREAD_STATE_COUNT)\n";
9667 struct MachO::x86_thread_state_t ts
;
9669 if (left
>= sizeof(MachO::x86_thread_state_t
)) {
9670 memcpy(&ts
, begin
, sizeof(MachO::x86_thread_state_t
));
9671 begin
+= sizeof(MachO::x86_thread_state_t
);
9673 memset(&ts
, '\0', sizeof(MachO::x86_thread_state_t
));
9674 memcpy(&ts
, begin
, left
);
9677 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9679 if (ts
.tsh
.flavor
== MachO::x86_THREAD_STATE32
) {
9680 outs() << "\t tsh.flavor x86_THREAD_STATE32 ";
9681 if (ts
.tsh
.count
== MachO::x86_THREAD_STATE32_COUNT
)
9682 outs() << "tsh.count x86_THREAD_STATE32_COUNT\n";
9684 outs() << "tsh.count " << ts
.tsh
.count
9685 << " (not x86_THREAD_STATE32_COUNT\n";
9686 Print_x86_thread_state32_t(ts
.uts
.ts32
);
9688 outs() << "\t tsh.flavor " << ts
.tsh
.flavor
<< " tsh.count "
9689 << ts
.tsh
.count
<< "\n";
9692 outs() << " flavor " << flavor
<< " (unknown)\n";
9693 outs() << " count " << count
<< "\n";
9694 outs() << " state (unknown)\n";
9695 begin
+= count
* sizeof(uint32_t);
9698 } else if (cputype
== MachO::CPU_TYPE_X86_64
) {
9699 while (begin
< end
) {
9700 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9701 memcpy((char *)&flavor
, begin
, sizeof(uint32_t));
9702 begin
+= sizeof(uint32_t);
9707 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9708 sys::swapByteOrder(flavor
);
9709 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9710 memcpy((char *)&count
, begin
, sizeof(uint32_t));
9711 begin
+= sizeof(uint32_t);
9716 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9717 sys::swapByteOrder(count
);
9718 if (flavor
== MachO::x86_THREAD_STATE64
) {
9719 outs() << " flavor x86_THREAD_STATE64\n";
9720 if (count
== MachO::x86_THREAD_STATE64_COUNT
)
9721 outs() << " count x86_THREAD_STATE64_COUNT\n";
9723 outs() << " count " << count
9724 << " (not x86_THREAD_STATE64_COUNT)\n";
9725 MachO::x86_thread_state64_t cpu64
;
9727 if (left
>= sizeof(MachO::x86_thread_state64_t
)) {
9728 memcpy(&cpu64
, begin
, sizeof(MachO::x86_thread_state64_t
));
9729 begin
+= sizeof(MachO::x86_thread_state64_t
);
9731 memset(&cpu64
, '\0', sizeof(MachO::x86_thread_state64_t
));
9732 memcpy(&cpu64
, begin
, left
);
9735 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9737 Print_x86_thread_state64_t(cpu64
);
9738 } else if (flavor
== MachO::x86_THREAD_STATE
) {
9739 outs() << " flavor x86_THREAD_STATE\n";
9740 if (count
== MachO::x86_THREAD_STATE_COUNT
)
9741 outs() << " count x86_THREAD_STATE_COUNT\n";
9743 outs() << " count " << count
9744 << " (not x86_THREAD_STATE_COUNT)\n";
9745 struct MachO::x86_thread_state_t ts
;
9747 if (left
>= sizeof(MachO::x86_thread_state_t
)) {
9748 memcpy(&ts
, begin
, sizeof(MachO::x86_thread_state_t
));
9749 begin
+= sizeof(MachO::x86_thread_state_t
);
9751 memset(&ts
, '\0', sizeof(MachO::x86_thread_state_t
));
9752 memcpy(&ts
, begin
, left
);
9755 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9757 if (ts
.tsh
.flavor
== MachO::x86_THREAD_STATE64
) {
9758 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
9759 if (ts
.tsh
.count
== MachO::x86_THREAD_STATE64_COUNT
)
9760 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
9762 outs() << "tsh.count " << ts
.tsh
.count
9763 << " (not x86_THREAD_STATE64_COUNT\n";
9764 Print_x86_thread_state64_t(ts
.uts
.ts64
);
9766 outs() << "\t tsh.flavor " << ts
.tsh
.flavor
<< " tsh.count "
9767 << ts
.tsh
.count
<< "\n";
9769 } else if (flavor
== MachO::x86_FLOAT_STATE
) {
9770 outs() << " flavor x86_FLOAT_STATE\n";
9771 if (count
== MachO::x86_FLOAT_STATE_COUNT
)
9772 outs() << " count x86_FLOAT_STATE_COUNT\n";
9774 outs() << " count " << count
<< " (not x86_FLOAT_STATE_COUNT)\n";
9775 struct MachO::x86_float_state_t fs
;
9777 if (left
>= sizeof(MachO::x86_float_state_t
)) {
9778 memcpy(&fs
, begin
, sizeof(MachO::x86_float_state_t
));
9779 begin
+= sizeof(MachO::x86_float_state_t
);
9781 memset(&fs
, '\0', sizeof(MachO::x86_float_state_t
));
9782 memcpy(&fs
, begin
, left
);
9785 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9787 if (fs
.fsh
.flavor
== MachO::x86_FLOAT_STATE64
) {
9788 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
9789 if (fs
.fsh
.count
== MachO::x86_FLOAT_STATE64_COUNT
)
9790 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
9792 outs() << "fsh.count " << fs
.fsh
.count
9793 << " (not x86_FLOAT_STATE64_COUNT\n";
9794 Print_x86_float_state_t(fs
.ufs
.fs64
);
9796 outs() << "\t fsh.flavor " << fs
.fsh
.flavor
<< " fsh.count "
9797 << fs
.fsh
.count
<< "\n";
9799 } else if (flavor
== MachO::x86_EXCEPTION_STATE
) {
9800 outs() << " flavor x86_EXCEPTION_STATE\n";
9801 if (count
== MachO::x86_EXCEPTION_STATE_COUNT
)
9802 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
9804 outs() << " count " << count
9805 << " (not x86_EXCEPTION_STATE_COUNT)\n";
9806 struct MachO::x86_exception_state_t es
;
9808 if (left
>= sizeof(MachO::x86_exception_state_t
)) {
9809 memcpy(&es
, begin
, sizeof(MachO::x86_exception_state_t
));
9810 begin
+= sizeof(MachO::x86_exception_state_t
);
9812 memset(&es
, '\0', sizeof(MachO::x86_exception_state_t
));
9813 memcpy(&es
, begin
, left
);
9816 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9818 if (es
.esh
.flavor
== MachO::x86_EXCEPTION_STATE64
) {
9819 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
9820 if (es
.esh
.count
== MachO::x86_EXCEPTION_STATE64_COUNT
)
9821 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
9823 outs() << "\t esh.count " << es
.esh
.count
9824 << " (not x86_EXCEPTION_STATE64_COUNT\n";
9825 Print_x86_exception_state_t(es
.ues
.es64
);
9827 outs() << "\t esh.flavor " << es
.esh
.flavor
<< " esh.count "
9828 << es
.esh
.count
<< "\n";
9830 } else if (flavor
== MachO::x86_EXCEPTION_STATE64
) {
9831 outs() << " flavor x86_EXCEPTION_STATE64\n";
9832 if (count
== MachO::x86_EXCEPTION_STATE64_COUNT
)
9833 outs() << " count x86_EXCEPTION_STATE64_COUNT\n";
9835 outs() << " count " << count
9836 << " (not x86_EXCEPTION_STATE64_COUNT)\n";
9837 struct MachO::x86_exception_state64_t es64
;
9839 if (left
>= sizeof(MachO::x86_exception_state64_t
)) {
9840 memcpy(&es64
, begin
, sizeof(MachO::x86_exception_state64_t
));
9841 begin
+= sizeof(MachO::x86_exception_state64_t
);
9843 memset(&es64
, '\0', sizeof(MachO::x86_exception_state64_t
));
9844 memcpy(&es64
, begin
, left
);
9847 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9849 Print_x86_exception_state_t(es64
);
9851 outs() << " flavor " << flavor
<< " (unknown)\n";
9852 outs() << " count " << count
<< "\n";
9853 outs() << " state (unknown)\n";
9854 begin
+= count
* sizeof(uint32_t);
9857 } else if (cputype
== MachO::CPU_TYPE_ARM
) {
9858 while (begin
< end
) {
9859 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9860 memcpy((char *)&flavor
, begin
, sizeof(uint32_t));
9861 begin
+= sizeof(uint32_t);
9866 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9867 sys::swapByteOrder(flavor
);
9868 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9869 memcpy((char *)&count
, begin
, sizeof(uint32_t));
9870 begin
+= sizeof(uint32_t);
9875 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9876 sys::swapByteOrder(count
);
9877 if (flavor
== MachO::ARM_THREAD_STATE
) {
9878 outs() << " flavor ARM_THREAD_STATE\n";
9879 if (count
== MachO::ARM_THREAD_STATE_COUNT
)
9880 outs() << " count ARM_THREAD_STATE_COUNT\n";
9882 outs() << " count " << count
9883 << " (not ARM_THREAD_STATE_COUNT)\n";
9884 MachO::arm_thread_state32_t cpu32
;
9886 if (left
>= sizeof(MachO::arm_thread_state32_t
)) {
9887 memcpy(&cpu32
, begin
, sizeof(MachO::arm_thread_state32_t
));
9888 begin
+= sizeof(MachO::arm_thread_state32_t
);
9890 memset(&cpu32
, '\0', sizeof(MachO::arm_thread_state32_t
));
9891 memcpy(&cpu32
, begin
, left
);
9894 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9896 Print_arm_thread_state32_t(cpu32
);
9898 outs() << " flavor " << flavor
<< " (unknown)\n";
9899 outs() << " count " << count
<< "\n";
9900 outs() << " state (unknown)\n";
9901 begin
+= count
* sizeof(uint32_t);
9904 } else if (cputype
== MachO::CPU_TYPE_ARM64
||
9905 cputype
== MachO::CPU_TYPE_ARM64_32
) {
9906 while (begin
< end
) {
9907 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9908 memcpy((char *)&flavor
, begin
, sizeof(uint32_t));
9909 begin
+= sizeof(uint32_t);
9914 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9915 sys::swapByteOrder(flavor
);
9916 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9917 memcpy((char *)&count
, begin
, sizeof(uint32_t));
9918 begin
+= sizeof(uint32_t);
9923 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9924 sys::swapByteOrder(count
);
9925 if (flavor
== MachO::ARM_THREAD_STATE64
) {
9926 outs() << " flavor ARM_THREAD_STATE64\n";
9927 if (count
== MachO::ARM_THREAD_STATE64_COUNT
)
9928 outs() << " count ARM_THREAD_STATE64_COUNT\n";
9930 outs() << " count " << count
9931 << " (not ARM_THREAD_STATE64_COUNT)\n";
9932 MachO::arm_thread_state64_t cpu64
;
9934 if (left
>= sizeof(MachO::arm_thread_state64_t
)) {
9935 memcpy(&cpu64
, begin
, sizeof(MachO::arm_thread_state64_t
));
9936 begin
+= sizeof(MachO::arm_thread_state64_t
);
9938 memset(&cpu64
, '\0', sizeof(MachO::arm_thread_state64_t
));
9939 memcpy(&cpu64
, begin
, left
);
9942 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9944 Print_arm_thread_state64_t(cpu64
);
9946 outs() << " flavor " << flavor
<< " (unknown)\n";
9947 outs() << " count " << count
<< "\n";
9948 outs() << " state (unknown)\n";
9949 begin
+= count
* sizeof(uint32_t);
9953 while (begin
< end
) {
9954 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9955 memcpy((char *)&flavor
, begin
, sizeof(uint32_t));
9956 begin
+= sizeof(uint32_t);
9961 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9962 sys::swapByteOrder(flavor
);
9963 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9964 memcpy((char *)&count
, begin
, sizeof(uint32_t));
9965 begin
+= sizeof(uint32_t);
9970 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9971 sys::swapByteOrder(count
);
9972 outs() << " flavor " << flavor
<< "\n";
9973 outs() << " count " << count
<< "\n";
9974 outs() << " state (Unknown cputype/cpusubtype)\n";
9975 begin
+= count
* sizeof(uint32_t);
9980 static void PrintDylibCommand(MachO::dylib_command dl
, const char *Ptr
) {
9981 if (dl
.cmd
== MachO::LC_ID_DYLIB
)
9982 outs() << " cmd LC_ID_DYLIB\n";
9983 else if (dl
.cmd
== MachO::LC_LOAD_DYLIB
)
9984 outs() << " cmd LC_LOAD_DYLIB\n";
9985 else if (dl
.cmd
== MachO::LC_LOAD_WEAK_DYLIB
)
9986 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
9987 else if (dl
.cmd
== MachO::LC_REEXPORT_DYLIB
)
9988 outs() << " cmd LC_REEXPORT_DYLIB\n";
9989 else if (dl
.cmd
== MachO::LC_LAZY_LOAD_DYLIB
)
9990 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
9991 else if (dl
.cmd
== MachO::LC_LOAD_UPWARD_DYLIB
)
9992 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
9994 outs() << " cmd " << dl
.cmd
<< " (unknown)\n";
9995 outs() << " cmdsize " << dl
.cmdsize
;
9996 if (dl
.cmdsize
< sizeof(struct MachO::dylib_command
))
9997 outs() << " Incorrect size\n";
10000 if (dl
.dylib
.name
< dl
.cmdsize
) {
10001 const char *P
= (const char *)(Ptr
) + dl
.dylib
.name
;
10002 outs() << " name " << P
<< " (offset " << dl
.dylib
.name
<< ")\n";
10004 outs() << " name ?(bad offset " << dl
.dylib
.name
<< ")\n";
10006 outs() << " time stamp " << dl
.dylib
.timestamp
<< " ";
10007 time_t t
= dl
.dylib
.timestamp
;
10008 outs() << ctime(&t
);
10009 outs() << " current version ";
10010 if (dl
.dylib
.current_version
== 0xffffffff)
10013 outs() << ((dl
.dylib
.current_version
>> 16) & 0xffff) << "."
10014 << ((dl
.dylib
.current_version
>> 8) & 0xff) << "."
10015 << (dl
.dylib
.current_version
& 0xff) << "\n";
10016 outs() << "compatibility version ";
10017 if (dl
.dylib
.compatibility_version
== 0xffffffff)
10020 outs() << ((dl
.dylib
.compatibility_version
>> 16) & 0xffff) << "."
10021 << ((dl
.dylib
.compatibility_version
>> 8) & 0xff) << "."
10022 << (dl
.dylib
.compatibility_version
& 0xff) << "\n";
10025 static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld
,
10026 uint32_t object_size
) {
10027 if (ld
.cmd
== MachO::LC_CODE_SIGNATURE
)
10028 outs() << " cmd LC_CODE_SIGNATURE\n";
10029 else if (ld
.cmd
== MachO::LC_SEGMENT_SPLIT_INFO
)
10030 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
10031 else if (ld
.cmd
== MachO::LC_FUNCTION_STARTS
)
10032 outs() << " cmd LC_FUNCTION_STARTS\n";
10033 else if (ld
.cmd
== MachO::LC_DATA_IN_CODE
)
10034 outs() << " cmd LC_DATA_IN_CODE\n";
10035 else if (ld
.cmd
== MachO::LC_DYLIB_CODE_SIGN_DRS
)
10036 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
10037 else if (ld
.cmd
== MachO::LC_LINKER_OPTIMIZATION_HINT
)
10038 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
10040 outs() << " cmd " << ld
.cmd
<< " (?)\n";
10041 outs() << " cmdsize " << ld
.cmdsize
;
10042 if (ld
.cmdsize
!= sizeof(struct MachO::linkedit_data_command
))
10043 outs() << " Incorrect size\n";
10046 outs() << " dataoff " << ld
.dataoff
;
10047 if (ld
.dataoff
> object_size
)
10048 outs() << " (past end of file)\n";
10051 outs() << " datasize " << ld
.datasize
;
10052 uint64_t big_size
= ld
.dataoff
;
10053 big_size
+= ld
.datasize
;
10054 if (big_size
> object_size
)
10055 outs() << " (past end of file)\n";
10060 static void PrintLoadCommands(const MachOObjectFile
*Obj
, uint32_t filetype
,
10061 uint32_t cputype
, bool verbose
) {
10062 StringRef Buf
= Obj
->getData();
10063 unsigned Index
= 0;
10064 for (const auto &Command
: Obj
->load_commands()) {
10065 outs() << "Load command " << Index
++ << "\n";
10066 if (Command
.C
.cmd
== MachO::LC_SEGMENT
) {
10067 MachO::segment_command SLC
= Obj
->getSegmentLoadCommand(Command
);
10068 const char *sg_segname
= SLC
.segname
;
10069 PrintSegmentCommand(SLC
.cmd
, SLC
.cmdsize
, SLC
.segname
, SLC
.vmaddr
,
10070 SLC
.vmsize
, SLC
.fileoff
, SLC
.filesize
, SLC
.maxprot
,
10071 SLC
.initprot
, SLC
.nsects
, SLC
.flags
, Buf
.size(),
10073 for (unsigned j
= 0; j
< SLC
.nsects
; j
++) {
10074 MachO::section S
= Obj
->getSection(Command
, j
);
10075 PrintSection(S
.sectname
, S
.segname
, S
.addr
, S
.size
, S
.offset
, S
.align
,
10076 S
.reloff
, S
.nreloc
, S
.flags
, S
.reserved1
, S
.reserved2
,
10077 SLC
.cmd
, sg_segname
, filetype
, Buf
.size(), verbose
);
10079 } else if (Command
.C
.cmd
== MachO::LC_SEGMENT_64
) {
10080 MachO::segment_command_64 SLC_64
= Obj
->getSegment64LoadCommand(Command
);
10081 const char *sg_segname
= SLC_64
.segname
;
10082 PrintSegmentCommand(SLC_64
.cmd
, SLC_64
.cmdsize
, SLC_64
.segname
,
10083 SLC_64
.vmaddr
, SLC_64
.vmsize
, SLC_64
.fileoff
,
10084 SLC_64
.filesize
, SLC_64
.maxprot
, SLC_64
.initprot
,
10085 SLC_64
.nsects
, SLC_64
.flags
, Buf
.size(), verbose
);
10086 for (unsigned j
= 0; j
< SLC_64
.nsects
; j
++) {
10087 MachO::section_64 S_64
= Obj
->getSection64(Command
, j
);
10088 PrintSection(S_64
.sectname
, S_64
.segname
, S_64
.addr
, S_64
.size
,
10089 S_64
.offset
, S_64
.align
, S_64
.reloff
, S_64
.nreloc
,
10090 S_64
.flags
, S_64
.reserved1
, S_64
.reserved2
, SLC_64
.cmd
,
10091 sg_segname
, filetype
, Buf
.size(), verbose
);
10093 } else if (Command
.C
.cmd
== MachO::LC_SYMTAB
) {
10094 MachO::symtab_command Symtab
= Obj
->getSymtabLoadCommand();
10095 PrintSymtabLoadCommand(Symtab
, Obj
->is64Bit(), Buf
.size());
10096 } else if (Command
.C
.cmd
== MachO::LC_DYSYMTAB
) {
10097 MachO::dysymtab_command Dysymtab
= Obj
->getDysymtabLoadCommand();
10098 MachO::symtab_command Symtab
= Obj
->getSymtabLoadCommand();
10099 PrintDysymtabLoadCommand(Dysymtab
, Symtab
.nsyms
, Buf
.size(),
10101 } else if (Command
.C
.cmd
== MachO::LC_DYLD_INFO
||
10102 Command
.C
.cmd
== MachO::LC_DYLD_INFO_ONLY
) {
10103 MachO::dyld_info_command DyldInfo
= Obj
->getDyldInfoLoadCommand(Command
);
10104 PrintDyldInfoLoadCommand(DyldInfo
, Buf
.size());
10105 } else if (Command
.C
.cmd
== MachO::LC_LOAD_DYLINKER
||
10106 Command
.C
.cmd
== MachO::LC_ID_DYLINKER
||
10107 Command
.C
.cmd
== MachO::LC_DYLD_ENVIRONMENT
) {
10108 MachO::dylinker_command Dyld
= Obj
->getDylinkerCommand(Command
);
10109 PrintDyldLoadCommand(Dyld
, Command
.Ptr
);
10110 } else if (Command
.C
.cmd
== MachO::LC_UUID
) {
10111 MachO::uuid_command Uuid
= Obj
->getUuidCommand(Command
);
10112 PrintUuidLoadCommand(Uuid
);
10113 } else if (Command
.C
.cmd
== MachO::LC_RPATH
) {
10114 MachO::rpath_command Rpath
= Obj
->getRpathCommand(Command
);
10115 PrintRpathLoadCommand(Rpath
, Command
.Ptr
);
10116 } else if (Command
.C
.cmd
== MachO::LC_VERSION_MIN_MACOSX
||
10117 Command
.C
.cmd
== MachO::LC_VERSION_MIN_IPHONEOS
||
10118 Command
.C
.cmd
== MachO::LC_VERSION_MIN_TVOS
||
10119 Command
.C
.cmd
== MachO::LC_VERSION_MIN_WATCHOS
) {
10120 MachO::version_min_command Vd
= Obj
->getVersionMinLoadCommand(Command
);
10121 PrintVersionMinLoadCommand(Vd
);
10122 } else if (Command
.C
.cmd
== MachO::LC_NOTE
) {
10123 MachO::note_command Nt
= Obj
->getNoteLoadCommand(Command
);
10124 PrintNoteLoadCommand(Nt
);
10125 } else if (Command
.C
.cmd
== MachO::LC_BUILD_VERSION
) {
10126 MachO::build_version_command Bv
=
10127 Obj
->getBuildVersionLoadCommand(Command
);
10128 PrintBuildVersionLoadCommand(Obj
, Bv
);
10129 } else if (Command
.C
.cmd
== MachO::LC_SOURCE_VERSION
) {
10130 MachO::source_version_command Sd
= Obj
->getSourceVersionCommand(Command
);
10131 PrintSourceVersionCommand(Sd
);
10132 } else if (Command
.C
.cmd
== MachO::LC_MAIN
) {
10133 MachO::entry_point_command Ep
= Obj
->getEntryPointCommand(Command
);
10134 PrintEntryPointCommand(Ep
);
10135 } else if (Command
.C
.cmd
== MachO::LC_ENCRYPTION_INFO
) {
10136 MachO::encryption_info_command Ei
=
10137 Obj
->getEncryptionInfoCommand(Command
);
10138 PrintEncryptionInfoCommand(Ei
, Buf
.size());
10139 } else if (Command
.C
.cmd
== MachO::LC_ENCRYPTION_INFO_64
) {
10140 MachO::encryption_info_command_64 Ei
=
10141 Obj
->getEncryptionInfoCommand64(Command
);
10142 PrintEncryptionInfoCommand64(Ei
, Buf
.size());
10143 } else if (Command
.C
.cmd
== MachO::LC_LINKER_OPTION
) {
10144 MachO::linker_option_command Lo
=
10145 Obj
->getLinkerOptionLoadCommand(Command
);
10146 PrintLinkerOptionCommand(Lo
, Command
.Ptr
);
10147 } else if (Command
.C
.cmd
== MachO::LC_SUB_FRAMEWORK
) {
10148 MachO::sub_framework_command Sf
= Obj
->getSubFrameworkCommand(Command
);
10149 PrintSubFrameworkCommand(Sf
, Command
.Ptr
);
10150 } else if (Command
.C
.cmd
== MachO::LC_SUB_UMBRELLA
) {
10151 MachO::sub_umbrella_command Sf
= Obj
->getSubUmbrellaCommand(Command
);
10152 PrintSubUmbrellaCommand(Sf
, Command
.Ptr
);
10153 } else if (Command
.C
.cmd
== MachO::LC_SUB_LIBRARY
) {
10154 MachO::sub_library_command Sl
= Obj
->getSubLibraryCommand(Command
);
10155 PrintSubLibraryCommand(Sl
, Command
.Ptr
);
10156 } else if (Command
.C
.cmd
== MachO::LC_SUB_CLIENT
) {
10157 MachO::sub_client_command Sc
= Obj
->getSubClientCommand(Command
);
10158 PrintSubClientCommand(Sc
, Command
.Ptr
);
10159 } else if (Command
.C
.cmd
== MachO::LC_ROUTINES
) {
10160 MachO::routines_command Rc
= Obj
->getRoutinesCommand(Command
);
10161 PrintRoutinesCommand(Rc
);
10162 } else if (Command
.C
.cmd
== MachO::LC_ROUTINES_64
) {
10163 MachO::routines_command_64 Rc
= Obj
->getRoutinesCommand64(Command
);
10164 PrintRoutinesCommand64(Rc
);
10165 } else if (Command
.C
.cmd
== MachO::LC_THREAD
||
10166 Command
.C
.cmd
== MachO::LC_UNIXTHREAD
) {
10167 MachO::thread_command Tc
= Obj
->getThreadCommand(Command
);
10168 PrintThreadCommand(Tc
, Command
.Ptr
, Obj
->isLittleEndian(), cputype
);
10169 } else if (Command
.C
.cmd
== MachO::LC_LOAD_DYLIB
||
10170 Command
.C
.cmd
== MachO::LC_ID_DYLIB
||
10171 Command
.C
.cmd
== MachO::LC_LOAD_WEAK_DYLIB
||
10172 Command
.C
.cmd
== MachO::LC_REEXPORT_DYLIB
||
10173 Command
.C
.cmd
== MachO::LC_LAZY_LOAD_DYLIB
||
10174 Command
.C
.cmd
== MachO::LC_LOAD_UPWARD_DYLIB
) {
10175 MachO::dylib_command Dl
= Obj
->getDylibIDLoadCommand(Command
);
10176 PrintDylibCommand(Dl
, Command
.Ptr
);
10177 } else if (Command
.C
.cmd
== MachO::LC_CODE_SIGNATURE
||
10178 Command
.C
.cmd
== MachO::LC_SEGMENT_SPLIT_INFO
||
10179 Command
.C
.cmd
== MachO::LC_FUNCTION_STARTS
||
10180 Command
.C
.cmd
== MachO::LC_DATA_IN_CODE
||
10181 Command
.C
.cmd
== MachO::LC_DYLIB_CODE_SIGN_DRS
||
10182 Command
.C
.cmd
== MachO::LC_LINKER_OPTIMIZATION_HINT
) {
10183 MachO::linkedit_data_command Ld
=
10184 Obj
->getLinkeditDataLoadCommand(Command
);
10185 PrintLinkEditDataCommand(Ld
, Buf
.size());
10187 outs() << " cmd ?(" << format("0x%08" PRIx32
, Command
.C
.cmd
)
10189 outs() << " cmdsize " << Command
.C
.cmdsize
<< "\n";
10190 // TODO: get and print the raw bytes of the load command.
10192 // TODO: print all the other kinds of load commands.
10196 static void PrintMachHeader(const MachOObjectFile
*Obj
, bool verbose
) {
10197 if (Obj
->is64Bit()) {
10198 MachO::mach_header_64 H_64
;
10199 H_64
= Obj
->getHeader64();
10200 PrintMachHeader(H_64
.magic
, H_64
.cputype
, H_64
.cpusubtype
, H_64
.filetype
,
10201 H_64
.ncmds
, H_64
.sizeofcmds
, H_64
.flags
, verbose
);
10203 MachO::mach_header H
;
10204 H
= Obj
->getHeader();
10205 PrintMachHeader(H
.magic
, H
.cputype
, H
.cpusubtype
, H
.filetype
, H
.ncmds
,
10206 H
.sizeofcmds
, H
.flags
, verbose
);
10210 void printMachOFileHeader(const object::ObjectFile
*Obj
) {
10211 const MachOObjectFile
*file
= dyn_cast
<const MachOObjectFile
>(Obj
);
10212 PrintMachHeader(file
, !NonVerbose
);
10215 void printMachOLoadCommands(const object::ObjectFile
*Obj
) {
10216 const MachOObjectFile
*file
= dyn_cast
<const MachOObjectFile
>(Obj
);
10217 uint32_t filetype
= 0;
10218 uint32_t cputype
= 0;
10219 if (file
->is64Bit()) {
10220 MachO::mach_header_64 H_64
;
10221 H_64
= file
->getHeader64();
10222 filetype
= H_64
.filetype
;
10223 cputype
= H_64
.cputype
;
10225 MachO::mach_header H
;
10226 H
= file
->getHeader();
10227 filetype
= H
.filetype
;
10228 cputype
= H
.cputype
;
10230 PrintLoadCommands(file
, filetype
, cputype
, !NonVerbose
);
10233 //===----------------------------------------------------------------------===//
10234 // export trie dumping
10235 //===----------------------------------------------------------------------===//
10237 void printMachOExportsTrie(const object::MachOObjectFile
*Obj
) {
10238 uint64_t BaseSegmentAddress
= 0;
10239 for (const auto &Command
: Obj
->load_commands()) {
10240 if (Command
.C
.cmd
== MachO::LC_SEGMENT
) {
10241 MachO::segment_command Seg
= Obj
->getSegmentLoadCommand(Command
);
10242 if (Seg
.fileoff
== 0 && Seg
.filesize
!= 0) {
10243 BaseSegmentAddress
= Seg
.vmaddr
;
10246 } else if (Command
.C
.cmd
== MachO::LC_SEGMENT_64
) {
10247 MachO::segment_command_64 Seg
= Obj
->getSegment64LoadCommand(Command
);
10248 if (Seg
.fileoff
== 0 && Seg
.filesize
!= 0) {
10249 BaseSegmentAddress
= Seg
.vmaddr
;
10254 Error Err
= Error::success();
10255 for (const object::ExportEntry
&Entry
: Obj
->exports(Err
)) {
10256 uint64_t Flags
= Entry
.flags();
10257 bool ReExport
= (Flags
& MachO::EXPORT_SYMBOL_FLAGS_REEXPORT
);
10258 bool WeakDef
= (Flags
& MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION
);
10259 bool ThreadLocal
= ((Flags
& MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK
) ==
10260 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL
);
10261 bool Abs
= ((Flags
& MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK
) ==
10262 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE
);
10263 bool Resolver
= (Flags
& MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER
);
10265 outs() << "[re-export] ";
10267 outs() << format("0x%08llX ",
10268 Entry
.address() + BaseSegmentAddress
);
10269 outs() << Entry
.name();
10270 if (WeakDef
|| ThreadLocal
|| Resolver
|| Abs
) {
10271 bool NeedsComma
= false;
10274 outs() << "weak_def";
10280 outs() << "per-thread";
10286 outs() << "absolute";
10292 outs() << format("resolver=0x%08llX", Entry
.other());
10298 StringRef DylibName
= "unknown";
10299 int Ordinal
= Entry
.other() - 1;
10300 Obj
->getLibraryShortNameByIndex(Ordinal
, DylibName
);
10301 if (Entry
.otherName().empty())
10302 outs() << " (from " << DylibName
<< ")";
10304 outs() << " (" << Entry
.otherName() << " from " << DylibName
<< ")";
10309 reportError(std::move(Err
), Obj
->getFileName());
10312 //===----------------------------------------------------------------------===//
10313 // rebase table dumping
10314 //===----------------------------------------------------------------------===//
10316 void printMachORebaseTable(object::MachOObjectFile
*Obj
) {
10317 outs() << "segment section address type\n";
10318 Error Err
= Error::success();
10319 for (const object::MachORebaseEntry
&Entry
: Obj
->rebaseTable(Err
)) {
10320 StringRef SegmentName
= Entry
.segmentName();
10321 StringRef SectionName
= Entry
.sectionName();
10322 uint64_t Address
= Entry
.address();
10324 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
10325 outs() << format("%-8s %-18s 0x%08" PRIX64
" %s\n",
10326 SegmentName
.str().c_str(), SectionName
.str().c_str(),
10327 Address
, Entry
.typeName().str().c_str());
10330 reportError(std::move(Err
), Obj
->getFileName());
10333 static StringRef
ordinalName(const object::MachOObjectFile
*Obj
, int Ordinal
) {
10334 StringRef DylibName
;
10336 case MachO::BIND_SPECIAL_DYLIB_SELF
:
10337 return "this-image";
10338 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE
:
10339 return "main-executable";
10340 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP
:
10341 return "flat-namespace";
10344 std::error_code EC
=
10345 Obj
->getLibraryShortNameByIndex(Ordinal
- 1, DylibName
);
10347 return "<<bad library ordinal>>";
10351 return "<<unknown special ordinal>>";
10354 //===----------------------------------------------------------------------===//
10355 // bind table dumping
10356 //===----------------------------------------------------------------------===//
10358 void printMachOBindTable(object::MachOObjectFile
*Obj
) {
10359 // Build table of sections so names can used in final output.
10360 outs() << "segment section address type "
10361 "addend dylib symbol\n";
10362 Error Err
= Error::success();
10363 for (const object::MachOBindEntry
&Entry
: Obj
->bindTable(Err
)) {
10364 StringRef SegmentName
= Entry
.segmentName();
10365 StringRef SectionName
= Entry
.sectionName();
10366 uint64_t Address
= Entry
.address();
10368 // Table lines look like:
10369 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
10371 if (Entry
.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT
)
10372 Attr
= " (weak_import)";
10373 outs() << left_justify(SegmentName
, 8) << " "
10374 << left_justify(SectionName
, 18) << " "
10375 << format_hex(Address
, 10, true) << " "
10376 << left_justify(Entry
.typeName(), 8) << " "
10377 << format_decimal(Entry
.addend(), 8) << " "
10378 << left_justify(ordinalName(Obj
, Entry
.ordinal()), 16) << " "
10379 << Entry
.symbolName() << Attr
<< "\n";
10382 reportError(std::move(Err
), Obj
->getFileName());
10385 //===----------------------------------------------------------------------===//
10386 // lazy bind table dumping
10387 //===----------------------------------------------------------------------===//
10389 void printMachOLazyBindTable(object::MachOObjectFile
*Obj
) {
10390 outs() << "segment section address "
10392 Error Err
= Error::success();
10393 for (const object::MachOBindEntry
&Entry
: Obj
->lazyBindTable(Err
)) {
10394 StringRef SegmentName
= Entry
.segmentName();
10395 StringRef SectionName
= Entry
.sectionName();
10396 uint64_t Address
= Entry
.address();
10398 // Table lines look like:
10399 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
10400 outs() << left_justify(SegmentName
, 8) << " "
10401 << left_justify(SectionName
, 18) << " "
10402 << format_hex(Address
, 10, true) << " "
10403 << left_justify(ordinalName(Obj
, Entry
.ordinal()), 16) << " "
10404 << Entry
.symbolName() << "\n";
10407 reportError(std::move(Err
), Obj
->getFileName());
10410 //===----------------------------------------------------------------------===//
10411 // weak bind table dumping
10412 //===----------------------------------------------------------------------===//
10414 void printMachOWeakBindTable(object::MachOObjectFile
*Obj
) {
10415 outs() << "segment section address "
10416 "type addend symbol\n";
10417 Error Err
= Error::success();
10418 for (const object::MachOBindEntry
&Entry
: Obj
->weakBindTable(Err
)) {
10419 // Strong symbols don't have a location to update.
10420 if (Entry
.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION
) {
10421 outs() << " strong "
10422 << Entry
.symbolName() << "\n";
10425 StringRef SegmentName
= Entry
.segmentName();
10426 StringRef SectionName
= Entry
.sectionName();
10427 uint64_t Address
= Entry
.address();
10429 // Table lines look like:
10430 // __DATA __data 0x00001000 pointer 0 _foo
10431 outs() << left_justify(SegmentName
, 8) << " "
10432 << left_justify(SectionName
, 18) << " "
10433 << format_hex(Address
, 10, true) << " "
10434 << left_justify(Entry
.typeName(), 8) << " "
10435 << format_decimal(Entry
.addend(), 8) << " " << Entry
.symbolName()
10439 reportError(std::move(Err
), Obj
->getFileName());
10442 // get_dyld_bind_info_symbolname() is used for disassembly and passed an
10443 // address, ReferenceValue, in the Mach-O file and looks in the dyld bind
10444 // information for that address. If the address is found its binding symbol
10445 // name is returned. If not nullptr is returned.
10446 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue
,
10447 struct DisassembleInfo
*info
) {
10448 if (info
->bindtable
== nullptr) {
10449 info
->bindtable
= std::make_unique
<SymbolAddressMap
>();
10450 Error Err
= Error::success();
10451 for (const object::MachOBindEntry
&Entry
: info
->O
->bindTable(Err
)) {
10452 uint64_t Address
= Entry
.address();
10453 StringRef name
= Entry
.symbolName();
10455 (*info
->bindtable
)[Address
] = name
;
10458 reportError(std::move(Err
), info
->O
->getFileName());
10460 auto name
= info
->bindtable
->lookup(ReferenceValue
);
10461 return !name
.empty() ? name
.data() : nullptr;
10464 void printLazyBindTable(ObjectFile
*o
) {
10465 outs() << "Lazy bind table:\n";
10466 if (MachOObjectFile
*MachO
= dyn_cast
<MachOObjectFile
>(o
))
10467 printMachOLazyBindTable(MachO
);
10470 << "This operation is only currently supported "
10471 "for Mach-O executable files.\n";
10474 void printWeakBindTable(ObjectFile
*o
) {
10475 outs() << "Weak bind table:\n";
10476 if (MachOObjectFile
*MachO
= dyn_cast
<MachOObjectFile
>(o
))
10477 printMachOWeakBindTable(MachO
);
10480 << "This operation is only currently supported "
10481 "for Mach-O executable files.\n";
10484 void printExportsTrie(const ObjectFile
*o
) {
10485 outs() << "Exports trie:\n";
10486 if (const MachOObjectFile
*MachO
= dyn_cast
<MachOObjectFile
>(o
))
10487 printMachOExportsTrie(MachO
);
10490 << "This operation is only currently supported "
10491 "for Mach-O executable files.\n";
10494 void printRebaseTable(ObjectFile
*o
) {
10495 outs() << "Rebase table:\n";
10496 if (MachOObjectFile
*MachO
= dyn_cast
<MachOObjectFile
>(o
))
10497 printMachORebaseTable(MachO
);
10500 << "This operation is only currently supported "
10501 "for Mach-O executable files.\n";
10504 void printBindTable(ObjectFile
*o
) {
10505 outs() << "Bind table:\n";
10506 if (MachOObjectFile
*MachO
= dyn_cast
<MachOObjectFile
>(o
))
10507 printMachOBindTable(MachO
);
10510 << "This operation is only currently supported "
10511 "for Mach-O executable files.\n";
10513 } // namespace llvm