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 "MachODump.h"
15 #include "ObjdumpOptID.h"
16 #include "llvm-objdump.h"
17 #include "llvm-c/Disassembler.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/BinaryFormat/MachO.h"
21 #include "llvm/Config/config.h"
22 #include "llvm/DebugInfo/DIContext.h"
23 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
24 #include "llvm/Demangle/Demangle.h"
25 #include "llvm/MC/MCAsmInfo.h"
26 #include "llvm/MC/MCContext.h"
27 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
28 #include "llvm/MC/MCInst.h"
29 #include "llvm/MC/MCInstPrinter.h"
30 #include "llvm/MC/MCInstrDesc.h"
31 #include "llvm/MC/MCInstrInfo.h"
32 #include "llvm/MC/MCRegisterInfo.h"
33 #include "llvm/MC/MCSubtargetInfo.h"
34 #include "llvm/MC/MCTargetOptions.h"
35 #include "llvm/MC/TargetRegistry.h"
36 #include "llvm/Object/MachO.h"
37 #include "llvm/Object/MachOUniversal.h"
38 #include "llvm/Option/ArgList.h"
39 #include "llvm/Support/Casting.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/Endian.h"
42 #include "llvm/Support/Format.h"
43 #include "llvm/Support/FormattedStream.h"
44 #include "llvm/Support/GraphWriter.h"
45 #include "llvm/Support/LEB128.h"
46 #include "llvm/Support/MemoryBuffer.h"
47 #include "llvm/Support/TargetSelect.h"
48 #include "llvm/Support/ToolOutputFile.h"
49 #include "llvm/Support/WithColor.h"
50 #include "llvm/Support/raw_ostream.h"
51 #include "llvm/TargetParser/Triple.h"
54 #include <system_error>
57 using namespace llvm::object
;
58 using namespace llvm::objdump
;
60 bool objdump::FirstPrivateHeader
;
61 bool objdump::ExportsTrie
;
65 bool objdump::LazyBind
;
66 bool objdump::WeakBind
;
68 static std::string DSYMFile
;
69 bool objdump::FullLeadingAddr
;
70 bool objdump::LeadingHeaders
;
71 bool objdump::UniversalHeaders
;
72 static bool ArchiveMemberOffsets
;
73 bool objdump::IndirectSymbols
;
74 bool objdump::DataInCode
;
75 FunctionStartsMode
objdump::FunctionStartsType
=
76 objdump::FunctionStartsMode::None
;
77 bool objdump::LinkOptHints
;
78 bool objdump::InfoPlist
;
79 bool objdump::ChainedFixups
;
80 bool objdump::DyldInfo
;
81 bool objdump::DylibsUsed
;
82 bool objdump::DylibId
;
83 bool objdump::Verbose
;
84 bool objdump::ObjcMetaData
;
85 std::string
objdump::DisSymName
;
86 bool objdump::SymbolicOperands
;
87 static std::vector
<std::string
> ArchFlags
;
89 static bool ArchAll
= false;
90 static std::string ThumbTripleName
;
92 static StringRef
ordinalName(const object::MachOObjectFile
*, int);
94 void objdump::parseMachOOptions(const llvm::opt::InputArgList
&InputArgs
) {
95 FirstPrivateHeader
= InputArgs
.hasArg(OBJDUMP_private_header
);
96 ExportsTrie
= InputArgs
.hasArg(OBJDUMP_exports_trie
);
97 Rebase
= InputArgs
.hasArg(OBJDUMP_rebase
);
98 Rpaths
= InputArgs
.hasArg(OBJDUMP_rpaths
);
99 Bind
= InputArgs
.hasArg(OBJDUMP_bind
);
100 LazyBind
= InputArgs
.hasArg(OBJDUMP_lazy_bind
);
101 WeakBind
= InputArgs
.hasArg(OBJDUMP_weak_bind
);
102 UseDbg
= InputArgs
.hasArg(OBJDUMP_g
);
103 DSYMFile
= InputArgs
.getLastArgValue(OBJDUMP_dsym_EQ
).str();
104 FullLeadingAddr
= InputArgs
.hasArg(OBJDUMP_full_leading_addr
);
105 LeadingHeaders
= !InputArgs
.hasArg(OBJDUMP_no_leading_headers
);
106 UniversalHeaders
= InputArgs
.hasArg(OBJDUMP_universal_headers
);
107 ArchiveMemberOffsets
= InputArgs
.hasArg(OBJDUMP_archive_member_offsets
);
108 IndirectSymbols
= InputArgs
.hasArg(OBJDUMP_indirect_symbols
);
109 DataInCode
= InputArgs
.hasArg(OBJDUMP_data_in_code
);
110 if (const opt::Arg
*A
= InputArgs
.getLastArg(OBJDUMP_function_starts_EQ
)) {
111 FunctionStartsType
= StringSwitch
<FunctionStartsMode
>(A
->getValue())
112 .Case("addrs", FunctionStartsMode::Addrs
)
113 .Case("names", FunctionStartsMode::Names
)
114 .Case("both", FunctionStartsMode::Both
)
115 .Default(FunctionStartsMode::None
);
116 if (FunctionStartsType
== FunctionStartsMode::None
)
119 LinkOptHints
= InputArgs
.hasArg(OBJDUMP_link_opt_hints
);
120 InfoPlist
= InputArgs
.hasArg(OBJDUMP_info_plist
);
121 ChainedFixups
= InputArgs
.hasArg(OBJDUMP_chained_fixups
);
122 DyldInfo
= InputArgs
.hasArg(OBJDUMP_dyld_info
);
123 DylibsUsed
= InputArgs
.hasArg(OBJDUMP_dylibs_used
);
124 DylibId
= InputArgs
.hasArg(OBJDUMP_dylib_id
);
125 Verbose
= !InputArgs
.hasArg(OBJDUMP_non_verbose
);
126 ObjcMetaData
= InputArgs
.hasArg(OBJDUMP_objc_meta_data
);
127 DisSymName
= InputArgs
.getLastArgValue(OBJDUMP_dis_symname
).str();
128 SymbolicOperands
= !InputArgs
.hasArg(OBJDUMP_no_symbolic_operands
);
129 ArchFlags
= InputArgs
.getAllArgValues(OBJDUMP_arch_EQ
);
132 static const Target
*GetTarget(const MachOObjectFile
*MachOObj
,
133 const char **McpuDefault
,
134 const Target
**ThumbTarget
) {
135 // Figure out the target triple.
136 Triple
TT(TripleName
);
137 if (TripleName
.empty()) {
138 TT
= MachOObj
->getArchTriple(McpuDefault
);
139 TripleName
= TT
.str();
142 if (TT
.getArch() == Triple::arm
) {
143 // We've inferred a 32-bit ARM target from the object file. All MachO CPUs
144 // that support ARM are also capable of Thumb mode.
145 Triple ThumbTriple
= TT
;
146 std::string ThumbName
= (Twine("thumb") + TT
.getArchName().substr(3)).str();
147 ThumbTriple
.setArchName(ThumbName
);
148 ThumbTripleName
= ThumbTriple
.str();
151 // Get the target specific parser.
153 const Target
*TheTarget
= TargetRegistry::lookupTarget(TripleName
, Error
);
154 if (TheTarget
&& ThumbTripleName
.empty())
157 *ThumbTarget
= TargetRegistry::lookupTarget(ThumbTripleName
, Error
);
161 WithColor::error(errs(), "llvm-objdump") << "unable to get target for '";
163 errs() << TripleName
;
165 errs() << ThumbTripleName
;
166 errs() << "', see --version and --triple.\n";
171 struct SymbolSorter
{
172 bool operator()(const SymbolRef
&A
, const SymbolRef
&B
) {
173 Expected
<SymbolRef::Type
> ATypeOrErr
= A
.getType();
175 reportError(ATypeOrErr
.takeError(), A
.getObject()->getFileName());
176 SymbolRef::Type AType
= *ATypeOrErr
;
177 Expected
<SymbolRef::Type
> BTypeOrErr
= B
.getType();
179 reportError(BTypeOrErr
.takeError(), B
.getObject()->getFileName());
180 SymbolRef::Type BType
= *BTypeOrErr
;
182 (AType
!= SymbolRef::ST_Function
) ? 0 : cantFail(A
.getValue());
184 (BType
!= SymbolRef::ST_Function
) ? 0 : cantFail(B
.getValue());
185 return AAddr
< BAddr
;
189 class MachODumper
: public Dumper
{
190 const object::MachOObjectFile
&Obj
;
193 MachODumper(const object::MachOObjectFile
&O
) : Dumper(O
), Obj(O
) {}
194 void printPrivateHeaders() override
;
198 std::unique_ptr
<Dumper
>
199 objdump::createMachODumper(const object::MachOObjectFile
&Obj
) {
200 return std::make_unique
<MachODumper
>(Obj
);
203 // Types for the storted data in code table that is built before disassembly
204 // and the predicate function to sort them.
205 typedef std::pair
<uint64_t, DiceRef
> DiceTableEntry
;
206 typedef std::vector
<DiceTableEntry
> DiceTable
;
207 typedef DiceTable::iterator dice_table_iterator
;
209 // This is used to search for a data in code table entry for the PC being
210 // disassembled. The j parameter has the PC in j.first. A single data in code
211 // table entry can cover many bytes for each of its Kind's. So if the offset,
212 // aka the i.first value, of the data in code table entry plus its Length
213 // covers the PC being searched for this will return true. If not it will
215 static bool compareDiceTableEntries(const DiceTableEntry
&i
,
216 const DiceTableEntry
&j
) {
218 i
.second
.getLength(Length
);
220 return j
.first
>= i
.first
&& j
.first
< i
.first
+ Length
;
223 static uint64_t DumpDataInCode(const uint8_t *bytes
, uint64_t Length
,
224 unsigned short Kind
) {
225 uint32_t Value
, Size
= 1;
229 case MachO::DICE_KIND_DATA
:
232 dumpBytes(ArrayRef(bytes
, 4), outs());
233 Value
= bytes
[3] << 24 | bytes
[2] << 16 | bytes
[1] << 8 | bytes
[0];
234 outs() << "\t.long " << Value
;
236 } else if (Length
>= 2) {
238 dumpBytes(ArrayRef(bytes
, 2), outs());
239 Value
= bytes
[1] << 8 | bytes
[0];
240 outs() << "\t.short " << Value
;
244 dumpBytes(ArrayRef(bytes
, 2), outs());
246 outs() << "\t.byte " << Value
;
249 if (Kind
== MachO::DICE_KIND_DATA
)
250 outs() << "\t@ KIND_DATA\n";
252 outs() << "\t@ data in code kind = " << Kind
<< "\n";
254 case MachO::DICE_KIND_JUMP_TABLE8
:
256 dumpBytes(ArrayRef(bytes
, 1), outs());
258 outs() << "\t.byte " << format("%3u", Value
) << "\t@ KIND_JUMP_TABLE8\n";
261 case MachO::DICE_KIND_JUMP_TABLE16
:
263 dumpBytes(ArrayRef(bytes
, 2), outs());
264 Value
= bytes
[1] << 8 | bytes
[0];
265 outs() << "\t.short " << format("%5u", Value
& 0xffff)
266 << "\t@ KIND_JUMP_TABLE16\n";
269 case MachO::DICE_KIND_JUMP_TABLE32
:
270 case MachO::DICE_KIND_ABS_JUMP_TABLE32
:
272 dumpBytes(ArrayRef(bytes
, 4), outs());
273 Value
= bytes
[3] << 24 | bytes
[2] << 16 | bytes
[1] << 8 | bytes
[0];
274 outs() << "\t.long " << Value
;
275 if (Kind
== MachO::DICE_KIND_JUMP_TABLE32
)
276 outs() << "\t@ KIND_JUMP_TABLE32\n";
278 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
285 static void getSectionsAndSymbols(MachOObjectFile
*MachOObj
,
286 std::vector
<SectionRef
> &Sections
,
287 std::vector
<SymbolRef
> &Symbols
,
288 SmallVectorImpl
<uint64_t> &FoundFns
,
289 uint64_t &BaseSegmentAddress
) {
290 const StringRef FileName
= MachOObj
->getFileName();
291 for (const SymbolRef
&Symbol
: MachOObj
->symbols()) {
292 StringRef SymName
= unwrapOrError(Symbol
.getName(), FileName
);
293 if (!SymName
.startswith("ltmp"))
294 Symbols
.push_back(Symbol
);
297 append_range(Sections
, MachOObj
->sections());
299 bool BaseSegmentAddressSet
= false;
300 for (const auto &Command
: MachOObj
->load_commands()) {
301 if (Command
.C
.cmd
== MachO::LC_FUNCTION_STARTS
) {
302 // We found a function starts segment, parse the addresses for later
304 MachO::linkedit_data_command LLC
=
305 MachOObj
->getLinkeditDataLoadCommand(Command
);
307 MachOObj
->ReadULEB128s(LLC
.dataoff
, FoundFns
);
308 } else if (Command
.C
.cmd
== MachO::LC_SEGMENT
) {
309 MachO::segment_command SLC
= MachOObj
->getSegmentLoadCommand(Command
);
310 StringRef SegName
= SLC
.segname
;
311 if (!BaseSegmentAddressSet
&& SegName
!= "__PAGEZERO") {
312 BaseSegmentAddressSet
= true;
313 BaseSegmentAddress
= SLC
.vmaddr
;
315 } else if (Command
.C
.cmd
== MachO::LC_SEGMENT_64
) {
316 MachO::segment_command_64 SLC
= MachOObj
->getSegment64LoadCommand(Command
);
317 StringRef SegName
= SLC
.segname
;
318 if (!BaseSegmentAddressSet
&& SegName
!= "__PAGEZERO") {
319 BaseSegmentAddressSet
= true;
320 BaseSegmentAddress
= SLC
.vmaddr
;
326 static bool DumpAndSkipDataInCode(uint64_t PC
, const uint8_t *bytes
,
327 DiceTable
&Dices
, uint64_t &InstSize
) {
328 // Check the data in code table here to see if this is data not an
329 // instruction to be disassembled.
331 Dice
.push_back(std::make_pair(PC
, DiceRef()));
332 dice_table_iterator DTI
=
333 std::search(Dices
.begin(), Dices
.end(), Dice
.begin(), Dice
.end(),
334 compareDiceTableEntries
);
335 if (DTI
!= Dices
.end()) {
337 DTI
->second
.getLength(Length
);
339 DTI
->second
.getKind(Kind
);
340 InstSize
= DumpDataInCode(bytes
, Length
, Kind
);
341 if ((Kind
== MachO::DICE_KIND_JUMP_TABLE8
) &&
342 (PC
== (DTI
->first
+ Length
- 1)) && (Length
& 1))
349 static void printRelocationTargetName(const MachOObjectFile
*O
,
350 const MachO::any_relocation_info
&RE
,
351 raw_string_ostream
&Fmt
) {
352 // Target of a scattered relocation is an address. In the interest of
353 // generating pretty output, scan through the symbol table looking for a
354 // symbol that aligns with that address. If we find one, print it.
355 // Otherwise, we just print the hex address of the target.
356 const StringRef FileName
= O
->getFileName();
357 if (O
->isRelocationScattered(RE
)) {
358 uint32_t Val
= O
->getPlainRelocationSymbolNum(RE
);
360 for (const SymbolRef
&Symbol
: O
->symbols()) {
361 uint64_t Addr
= unwrapOrError(Symbol
.getAddress(), FileName
);
364 Fmt
<< unwrapOrError(Symbol
.getName(), FileName
);
368 // If we couldn't find a symbol that this relocation refers to, try
369 // to find a section beginning instead.
370 for (const SectionRef
&Section
: ToolSectionFilter(*O
)) {
371 uint64_t Addr
= Section
.getAddress();
374 StringRef NameOrErr
= unwrapOrError(Section
.getName(), O
->getFileName());
379 Fmt
<< format("0x%x", Val
);
384 bool isExtern
= O
->getPlainRelocationExternal(RE
);
385 uint64_t Val
= O
->getPlainRelocationSymbolNum(RE
);
387 if (O
->getAnyRelocationType(RE
) == MachO::ARM64_RELOC_ADDEND
&&
388 (O
->getArch() == Triple::aarch64
|| O
->getArch() == Triple::aarch64_be
)) {
389 Fmt
<< format("0x%0" PRIx64
, Val
);
394 symbol_iterator SI
= O
->symbol_begin();
395 std::advance(SI
, Val
);
396 S
= unwrapOrError(SI
->getName(), FileName
);
398 section_iterator SI
= O
->section_begin();
399 // Adjust for the fact that sections are 1-indexed.
404 uint32_t I
= Val
- 1;
405 while (I
!= 0 && SI
!= O
->section_end()) {
409 if (SI
== O
->section_end()) {
410 Fmt
<< Val
<< " (?,?)";
412 if (Expected
<StringRef
> NameOrErr
= SI
->getName())
415 consumeError(NameOrErr
.takeError());
422 Error
objdump::getMachORelocationValueString(const MachOObjectFile
*Obj
,
423 const RelocationRef
&RelRef
,
424 SmallVectorImpl
<char> &Result
) {
425 DataRefImpl Rel
= RelRef
.getRawDataRefImpl();
426 MachO::any_relocation_info RE
= Obj
->getRelocation(Rel
);
428 unsigned Arch
= Obj
->getArch();
431 raw_string_ostream
Fmt(FmtBuf
);
432 unsigned Type
= Obj
->getAnyRelocationType(RE
);
433 bool IsPCRel
= Obj
->getAnyRelocationPCRel(RE
);
435 // Determine any addends that should be displayed with the relocation.
436 // These require decoding the relocation type, which is triple-specific.
438 // X86_64 has entirely custom relocation types.
439 if (Arch
== Triple::x86_64
) {
441 case MachO::X86_64_RELOC_GOT_LOAD
:
442 case MachO::X86_64_RELOC_GOT
: {
443 printRelocationTargetName(Obj
, RE
, Fmt
);
449 case MachO::X86_64_RELOC_SUBTRACTOR
: {
450 DataRefImpl RelNext
= Rel
;
451 Obj
->moveRelocationNext(RelNext
);
452 MachO::any_relocation_info RENext
= Obj
->getRelocation(RelNext
);
454 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
455 // X86_64_RELOC_UNSIGNED.
456 // NOTE: Scattered relocations don't exist on x86_64.
457 unsigned RType
= Obj
->getAnyRelocationType(RENext
);
458 if (RType
!= MachO::X86_64_RELOC_UNSIGNED
)
459 reportError(Obj
->getFileName(), "Expected X86_64_RELOC_UNSIGNED after "
460 "X86_64_RELOC_SUBTRACTOR.");
462 // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
463 // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
464 printRelocationTargetName(Obj
, RENext
, Fmt
);
466 printRelocationTargetName(Obj
, RE
, Fmt
);
469 case MachO::X86_64_RELOC_TLV
:
470 printRelocationTargetName(Obj
, RE
, Fmt
);
475 case MachO::X86_64_RELOC_SIGNED_1
:
476 printRelocationTargetName(Obj
, RE
, Fmt
);
479 case MachO::X86_64_RELOC_SIGNED_2
:
480 printRelocationTargetName(Obj
, RE
, Fmt
);
483 case MachO::X86_64_RELOC_SIGNED_4
:
484 printRelocationTargetName(Obj
, RE
, Fmt
);
488 printRelocationTargetName(Obj
, RE
, Fmt
);
491 // X86 and ARM share some relocation types in common.
492 } else if (Arch
== Triple::x86
|| Arch
== Triple::arm
||
493 Arch
== Triple::ppc
) {
494 // Generic relocation types...
496 case MachO::GENERIC_RELOC_PAIR
: // prints no info
497 return Error::success();
498 case MachO::GENERIC_RELOC_SECTDIFF
: {
499 DataRefImpl RelNext
= Rel
;
500 Obj
->moveRelocationNext(RelNext
);
501 MachO::any_relocation_info RENext
= Obj
->getRelocation(RelNext
);
503 // X86 sect diff's must be followed by a relocation of type
504 // GENERIC_RELOC_PAIR.
505 unsigned RType
= Obj
->getAnyRelocationType(RENext
);
507 if (RType
!= MachO::GENERIC_RELOC_PAIR
)
508 reportError(Obj
->getFileName(), "Expected GENERIC_RELOC_PAIR after "
509 "GENERIC_RELOC_SECTDIFF.");
511 printRelocationTargetName(Obj
, RE
, Fmt
);
513 printRelocationTargetName(Obj
, RENext
, Fmt
);
518 if (Arch
== Triple::x86
|| Arch
== Triple::ppc
) {
520 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF
: {
521 DataRefImpl RelNext
= Rel
;
522 Obj
->moveRelocationNext(RelNext
);
523 MachO::any_relocation_info RENext
= Obj
->getRelocation(RelNext
);
525 // X86 sect diff's must be followed by a relocation of type
526 // GENERIC_RELOC_PAIR.
527 unsigned RType
= Obj
->getAnyRelocationType(RENext
);
528 if (RType
!= MachO::GENERIC_RELOC_PAIR
)
529 reportError(Obj
->getFileName(), "Expected GENERIC_RELOC_PAIR after "
530 "GENERIC_RELOC_LOCAL_SECTDIFF.");
532 printRelocationTargetName(Obj
, RE
, Fmt
);
534 printRelocationTargetName(Obj
, RENext
, Fmt
);
537 case MachO::GENERIC_RELOC_TLV
: {
538 printRelocationTargetName(Obj
, RE
, Fmt
);
545 printRelocationTargetName(Obj
, RE
, Fmt
);
547 } else { // ARM-specific relocations
549 case MachO::ARM_RELOC_HALF
:
550 case MachO::ARM_RELOC_HALF_SECTDIFF
: {
551 // Half relocations steal a bit from the length field to encode
552 // whether this is an upper16 or a lower16 relocation.
553 bool isUpper
= (Obj
->getAnyRelocationLength(RE
) & 0x1) == 1;
559 printRelocationTargetName(Obj
, RE
, Fmt
);
561 DataRefImpl RelNext
= Rel
;
562 Obj
->moveRelocationNext(RelNext
);
563 MachO::any_relocation_info RENext
= Obj
->getRelocation(RelNext
);
565 // ARM half relocs must be followed by a relocation of type
567 unsigned RType
= Obj
->getAnyRelocationType(RENext
);
568 if (RType
!= MachO::ARM_RELOC_PAIR
)
569 reportError(Obj
->getFileName(), "Expected ARM_RELOC_PAIR after "
572 // NOTE: The half of the target virtual address is stashed in the
573 // address field of the secondary relocation, but we can't reverse
574 // engineer the constant offset from it without decoding the movw/movt
575 // instruction to find the other half in its immediate field.
577 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
578 // symbol/section pointer of the follow-on relocation.
579 if (Type
== MachO::ARM_RELOC_HALF_SECTDIFF
) {
581 printRelocationTargetName(Obj
, RENext
, Fmt
);
588 printRelocationTargetName(Obj
, RE
, Fmt
);
593 printRelocationTargetName(Obj
, RE
, Fmt
);
596 Result
.append(FmtBuf
.begin(), FmtBuf
.end());
597 return Error::success();
600 static void PrintIndirectSymbolTable(MachOObjectFile
*O
, bool verbose
,
601 uint32_t n
, uint32_t count
,
602 uint32_t stride
, uint64_t addr
) {
603 MachO::dysymtab_command Dysymtab
= O
->getDysymtabLoadCommand();
604 uint32_t nindirectsyms
= Dysymtab
.nindirectsyms
;
605 if (n
> nindirectsyms
)
606 outs() << " (entries start past the end of the indirect symbol "
607 "table) (reserved1 field greater than the table size)";
608 else if (n
+ count
> nindirectsyms
)
609 outs() << " (entries extends past the end of the indirect symbol "
612 uint32_t cputype
= O
->getHeader().cputype
;
613 if (cputype
& MachO::CPU_ARCH_ABI64
)
614 outs() << "address index";
616 outs() << "address index";
621 for (uint32_t j
= 0; j
< count
&& n
+ j
< nindirectsyms
; j
++) {
622 if (cputype
& MachO::CPU_ARCH_ABI64
)
623 outs() << format("0x%016" PRIx64
, addr
+ j
* stride
) << " ";
625 outs() << format("0x%08" PRIx32
, (uint32_t)addr
+ j
* stride
) << " ";
626 MachO::dysymtab_command Dysymtab
= O
->getDysymtabLoadCommand();
627 uint32_t indirect_symbol
= O
->getIndirectSymbolTableEntry(Dysymtab
, n
+ j
);
628 if (indirect_symbol
== MachO::INDIRECT_SYMBOL_LOCAL
) {
632 if (indirect_symbol
==
633 (MachO::INDIRECT_SYMBOL_LOCAL
| MachO::INDIRECT_SYMBOL_ABS
)) {
634 outs() << "LOCAL ABSOLUTE\n";
637 if (indirect_symbol
== MachO::INDIRECT_SYMBOL_ABS
) {
638 outs() << "ABSOLUTE\n";
641 outs() << format("%5u ", indirect_symbol
);
643 MachO::symtab_command Symtab
= O
->getSymtabLoadCommand();
644 if (indirect_symbol
< Symtab
.nsyms
) {
645 symbol_iterator Sym
= O
->getSymbolByIndex(indirect_symbol
);
646 SymbolRef Symbol
= *Sym
;
647 outs() << unwrapOrError(Symbol
.getName(), O
->getFileName());
656 static void PrintIndirectSymbols(MachOObjectFile
*O
, bool verbose
) {
657 for (const auto &Load
: O
->load_commands()) {
658 if (Load
.C
.cmd
== MachO::LC_SEGMENT_64
) {
659 MachO::segment_command_64 Seg
= O
->getSegment64LoadCommand(Load
);
660 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
661 MachO::section_64 Sec
= O
->getSection64(Load
, J
);
662 uint32_t section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
663 if (section_type
== MachO::S_NON_LAZY_SYMBOL_POINTERS
||
664 section_type
== MachO::S_LAZY_SYMBOL_POINTERS
||
665 section_type
== MachO::S_LAZY_DYLIB_SYMBOL_POINTERS
||
666 section_type
== MachO::S_THREAD_LOCAL_VARIABLE_POINTERS
||
667 section_type
== MachO::S_SYMBOL_STUBS
) {
669 if (section_type
== MachO::S_SYMBOL_STUBS
)
670 stride
= Sec
.reserved2
;
674 outs() << "Can't print indirect symbols for (" << Sec
.segname
<< ","
675 << Sec
.sectname
<< ") "
676 << "(size of stubs in reserved2 field is zero)\n";
679 uint32_t count
= Sec
.size
/ stride
;
680 outs() << "Indirect symbols for (" << Sec
.segname
<< ","
681 << Sec
.sectname
<< ") " << count
<< " entries";
682 uint32_t n
= Sec
.reserved1
;
683 PrintIndirectSymbolTable(O
, verbose
, n
, count
, stride
, Sec
.addr
);
686 } else if (Load
.C
.cmd
== MachO::LC_SEGMENT
) {
687 MachO::segment_command Seg
= O
->getSegmentLoadCommand(Load
);
688 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
689 MachO::section Sec
= O
->getSection(Load
, J
);
690 uint32_t section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
691 if (section_type
== MachO::S_NON_LAZY_SYMBOL_POINTERS
||
692 section_type
== MachO::S_LAZY_SYMBOL_POINTERS
||
693 section_type
== MachO::S_LAZY_DYLIB_SYMBOL_POINTERS
||
694 section_type
== MachO::S_THREAD_LOCAL_VARIABLE_POINTERS
||
695 section_type
== MachO::S_SYMBOL_STUBS
) {
697 if (section_type
== MachO::S_SYMBOL_STUBS
)
698 stride
= Sec
.reserved2
;
702 outs() << "Can't print indirect symbols for (" << Sec
.segname
<< ","
703 << Sec
.sectname
<< ") "
704 << "(size of stubs in reserved2 field is zero)\n";
707 uint32_t count
= Sec
.size
/ stride
;
708 outs() << "Indirect symbols for (" << Sec
.segname
<< ","
709 << Sec
.sectname
<< ") " << count
<< " entries";
710 uint32_t n
= Sec
.reserved1
;
711 PrintIndirectSymbolTable(O
, verbose
, n
, count
, stride
, Sec
.addr
);
718 static void PrintRType(const uint64_t cputype
, const unsigned r_type
) {
719 static char const *generic_r_types
[] = {
720 "VANILLA ", "PAIR ", "SECTDIF ", "PBLAPTR ", "LOCSDIF ", "TLV ",
721 " 6 (?) ", " 7 (?) ", " 8 (?) ", " 9 (?) ", " 10 (?) ", " 11 (?) ",
722 " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
724 static char const *x86_64_r_types
[] = {
725 "UNSIGND ", "SIGNED ", "BRANCH ", "GOT_LD ", "GOT ", "SUB ",
726 "SIGNED1 ", "SIGNED2 ", "SIGNED4 ", "TLV ", " 10 (?) ", " 11 (?) ",
727 " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
729 static char const *arm_r_types
[] = {
730 "VANILLA ", "PAIR ", "SECTDIFF", "LOCSDIF ", "PBLAPTR ",
731 "BR24 ", "T_BR22 ", "T_BR32 ", "HALF ", "HALFDIF ",
732 " 10 (?) ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
734 static char const *arm64_r_types
[] = {
735 "UNSIGND ", "SUB ", "BR26 ", "PAGE21 ", "PAGOF12 ",
736 "GOTLDP ", "GOTLDPOF", "PTRTGOT ", "TLVLDP ", "TLVLDPOF",
737 "ADDEND ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
741 outs() << format("%-7u", r_type
) << " ";
745 case MachO::CPU_TYPE_I386
:
746 outs() << generic_r_types
[r_type
];
748 case MachO::CPU_TYPE_X86_64
:
749 outs() << x86_64_r_types
[r_type
];
751 case MachO::CPU_TYPE_ARM
:
752 outs() << arm_r_types
[r_type
];
754 case MachO::CPU_TYPE_ARM64
:
755 case MachO::CPU_TYPE_ARM64_32
:
756 outs() << arm64_r_types
[r_type
];
759 outs() << format("%-7u ", r_type
);
763 static void PrintRLength(const uint64_t cputype
, const unsigned r_type
,
764 const unsigned r_length
, const bool previous_arm_half
){
765 if (cputype
== MachO::CPU_TYPE_ARM
&&
766 (r_type
== MachO::ARM_RELOC_HALF
||
767 r_type
== MachO::ARM_RELOC_HALF_SECTDIFF
|| previous_arm_half
== true)) {
768 if ((r_length
& 0x1) == 0)
772 if ((r_length
& 0x1) == 0)
788 if (cputype
== MachO::CPU_TYPE_X86_64
)
791 outs() << format("?(%2d) ", r_length
);
794 outs() << format("?(%2d) ", r_length
);
799 static void PrintRelocationEntries(const MachOObjectFile
*O
,
800 const relocation_iterator Begin
,
801 const relocation_iterator End
,
802 const uint64_t cputype
,
803 const bool verbose
) {
804 const MachO::symtab_command Symtab
= O
->getSymtabLoadCommand();
805 bool previous_arm_half
= false;
806 bool previous_sectdiff
= false;
807 uint32_t sectdiff_r_type
= 0;
809 for (relocation_iterator Reloc
= Begin
; Reloc
!= End
; ++Reloc
) {
810 const DataRefImpl Rel
= Reloc
->getRawDataRefImpl();
811 const MachO::any_relocation_info RE
= O
->getRelocation(Rel
);
812 const unsigned r_type
= O
->getAnyRelocationType(RE
);
813 const bool r_scattered
= O
->isRelocationScattered(RE
);
814 const unsigned r_pcrel
= O
->getAnyRelocationPCRel(RE
);
815 const unsigned r_length
= O
->getAnyRelocationLength(RE
);
816 const unsigned r_address
= O
->getAnyRelocationAddress(RE
);
817 const bool r_extern
= (r_scattered
? false :
818 O
->getPlainRelocationExternal(RE
));
819 const uint32_t r_value
= (r_scattered
?
820 O
->getScatteredRelocationValue(RE
) : 0);
821 const unsigned r_symbolnum
= (r_scattered
? 0 :
822 O
->getPlainRelocationSymbolNum(RE
));
824 if (r_scattered
&& cputype
!= MachO::CPU_TYPE_X86_64
) {
826 // scattered: address
827 if ((cputype
== MachO::CPU_TYPE_I386
&&
828 r_type
== MachO::GENERIC_RELOC_PAIR
) ||
829 (cputype
== MachO::CPU_TYPE_ARM
&& r_type
== MachO::ARM_RELOC_PAIR
))
832 outs() << format("%08x ", (unsigned int)r_address
);
841 PrintRLength(cputype
, r_type
, r_length
, previous_arm_half
);
843 // scattered: extern & type
845 PrintRType(cputype
, r_type
);
847 // scattered: scattered & value
848 outs() << format("True 0x%08x", (unsigned int)r_value
);
849 if (previous_sectdiff
== false) {
850 if ((cputype
== MachO::CPU_TYPE_ARM
&&
851 r_type
== MachO::ARM_RELOC_PAIR
))
852 outs() << format(" half = 0x%04x ", (unsigned int)r_address
);
853 } else if (cputype
== MachO::CPU_TYPE_ARM
&&
854 sectdiff_r_type
== MachO::ARM_RELOC_HALF_SECTDIFF
)
855 outs() << format(" other_half = 0x%04x ", (unsigned int)r_address
);
856 if ((cputype
== MachO::CPU_TYPE_I386
&&
857 (r_type
== MachO::GENERIC_RELOC_SECTDIFF
||
858 r_type
== MachO::GENERIC_RELOC_LOCAL_SECTDIFF
)) ||
859 (cputype
== MachO::CPU_TYPE_ARM
&&
860 (sectdiff_r_type
== MachO::ARM_RELOC_SECTDIFF
||
861 sectdiff_r_type
== MachO::ARM_RELOC_LOCAL_SECTDIFF
||
862 sectdiff_r_type
== MachO::ARM_RELOC_HALF_SECTDIFF
))) {
863 previous_sectdiff
= true;
864 sectdiff_r_type
= r_type
;
866 previous_sectdiff
= false;
869 if (cputype
== MachO::CPU_TYPE_ARM
&&
870 (r_type
== MachO::ARM_RELOC_HALF
||
871 r_type
== MachO::ARM_RELOC_HALF_SECTDIFF
))
872 previous_arm_half
= true;
874 previous_arm_half
= false;
878 // scattered: address pcrel length extern type scattered value
879 outs() << format("%08x %1d %-2d n/a %-7d 1 0x%08x\n",
880 (unsigned int)r_address
, r_pcrel
, r_length
, r_type
,
881 (unsigned int)r_value
);
887 if (cputype
== MachO::CPU_TYPE_ARM
&& r_type
== MachO::ARM_RELOC_PAIR
)
890 outs() << format("%08x ", (unsigned int)r_address
);
899 PrintRLength(cputype
, r_type
, r_length
, previous_arm_half
);
902 // plain: extern & type & scattered
904 PrintRType(cputype
, r_type
);
907 // plain: symbolnum/value
908 if (r_symbolnum
> Symtab
.nsyms
)
909 outs() << format("?(%d)\n", r_symbolnum
);
911 SymbolRef Symbol
= *O
->getSymbolByIndex(r_symbolnum
);
912 Expected
<StringRef
> SymNameNext
= Symbol
.getName();
913 const char *name
= nullptr;
915 name
= SymNameNext
->data();
917 outs() << format("?(%d)\n", r_symbolnum
);
919 outs() << name
<< "\n";
923 // plain: extern & type & scattered
925 PrintRType(cputype
, r_type
);
928 // plain: symbolnum/value
929 if (cputype
== MachO::CPU_TYPE_ARM
&& r_type
== MachO::ARM_RELOC_PAIR
)
930 outs() << format("other_half = 0x%04x\n", (unsigned int)r_address
);
931 else if ((cputype
== MachO::CPU_TYPE_ARM64
||
932 cputype
== MachO::CPU_TYPE_ARM64_32
) &&
933 r_type
== MachO::ARM64_RELOC_ADDEND
)
934 outs() << format("addend = 0x%06x\n", (unsigned int)r_symbolnum
);
936 outs() << format("%d ", r_symbolnum
);
937 if (r_symbolnum
== MachO::R_ABS
)
940 // in this case, r_symbolnum is actually a 1-based section number
941 uint32_t nsects
= O
->section_end()->getRawDataRefImpl().d
.a
;
942 if (r_symbolnum
> 0 && r_symbolnum
<= nsects
) {
943 object::DataRefImpl DRI
;
944 DRI
.d
.a
= r_symbolnum
-1;
945 StringRef SegName
= O
->getSectionFinalSegmentName(DRI
);
946 if (Expected
<StringRef
> NameOrErr
= O
->getSectionName(DRI
))
947 outs() << "(" << SegName
<< "," << *NameOrErr
<< ")\n";
957 if (cputype
== MachO::CPU_TYPE_ARM
&&
958 (r_type
== MachO::ARM_RELOC_HALF
||
959 r_type
== MachO::ARM_RELOC_HALF_SECTDIFF
))
960 previous_arm_half
= true;
962 previous_arm_half
= false;
965 // plain: address pcrel length extern type scattered symbolnum/section
966 outs() << format("%08x %1d %-2d %1d %-7d 0 %d\n",
967 (unsigned int)r_address
, r_pcrel
, r_length
, r_extern
,
968 r_type
, r_symbolnum
);
974 static void PrintRelocations(const MachOObjectFile
*O
, const bool verbose
) {
975 const uint64_t cputype
= O
->getHeader().cputype
;
976 const MachO::dysymtab_command Dysymtab
= O
->getDysymtabLoadCommand();
977 if (Dysymtab
.nextrel
!= 0) {
978 outs() << "External relocation information " << Dysymtab
.nextrel
980 outs() << "\naddress pcrel length extern type scattered "
982 PrintRelocationEntries(O
, O
->extrel_begin(), O
->extrel_end(), cputype
,
985 if (Dysymtab
.nlocrel
!= 0) {
986 outs() << format("Local relocation information %u entries",
988 outs() << "\naddress pcrel length extern type scattered "
990 PrintRelocationEntries(O
, O
->locrel_begin(), O
->locrel_end(), cputype
,
993 for (const auto &Load
: O
->load_commands()) {
994 if (Load
.C
.cmd
== MachO::LC_SEGMENT_64
) {
995 const MachO::segment_command_64 Seg
= O
->getSegment64LoadCommand(Load
);
996 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
997 const MachO::section_64 Sec
= O
->getSection64(Load
, J
);
998 if (Sec
.nreloc
!= 0) {
1001 const StringRef SegName
= O
->getSectionFinalSegmentName(DRI
);
1002 if (Expected
<StringRef
> NameOrErr
= O
->getSectionName(DRI
))
1003 outs() << "Relocation information (" << SegName
<< "," << *NameOrErr
1004 << format(") %u entries", Sec
.nreloc
);
1006 outs() << "Relocation information (" << SegName
<< ",?) "
1007 << format("%u entries", Sec
.nreloc
);
1008 outs() << "\naddress pcrel length extern type scattered "
1009 "symbolnum/value\n";
1010 PrintRelocationEntries(O
, O
->section_rel_begin(DRI
),
1011 O
->section_rel_end(DRI
), cputype
, verbose
);
1014 } else if (Load
.C
.cmd
== MachO::LC_SEGMENT
) {
1015 const MachO::segment_command Seg
= O
->getSegmentLoadCommand(Load
);
1016 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
1017 const MachO::section Sec
= O
->getSection(Load
, J
);
1018 if (Sec
.nreloc
!= 0) {
1021 const StringRef SegName
= O
->getSectionFinalSegmentName(DRI
);
1022 if (Expected
<StringRef
> NameOrErr
= O
->getSectionName(DRI
))
1023 outs() << "Relocation information (" << SegName
<< "," << *NameOrErr
1024 << format(") %u entries", Sec
.nreloc
);
1026 outs() << "Relocation information (" << SegName
<< ",?) "
1027 << format("%u entries", Sec
.nreloc
);
1028 outs() << "\naddress pcrel length extern type scattered "
1029 "symbolnum/value\n";
1030 PrintRelocationEntries(O
, O
->section_rel_begin(DRI
),
1031 O
->section_rel_end(DRI
), cputype
, verbose
);
1038 static void PrintFunctionStarts(MachOObjectFile
*O
) {
1039 uint64_t BaseSegmentAddress
= 0;
1040 for (const MachOObjectFile::LoadCommandInfo
&Command
: O
->load_commands()) {
1041 if (Command
.C
.cmd
== MachO::LC_SEGMENT
) {
1042 MachO::segment_command SLC
= O
->getSegmentLoadCommand(Command
);
1043 if (StringRef(SLC
.segname
) == "__TEXT") {
1044 BaseSegmentAddress
= SLC
.vmaddr
;
1047 } else if (Command
.C
.cmd
== MachO::LC_SEGMENT_64
) {
1048 MachO::segment_command_64 SLC
= O
->getSegment64LoadCommand(Command
);
1049 if (StringRef(SLC
.segname
) == "__TEXT") {
1050 BaseSegmentAddress
= SLC
.vmaddr
;
1056 SmallVector
<uint64_t, 8> FunctionStarts
;
1057 for (const MachOObjectFile::LoadCommandInfo
&LC
: O
->load_commands()) {
1058 if (LC
.C
.cmd
== MachO::LC_FUNCTION_STARTS
) {
1059 MachO::linkedit_data_command FunctionStartsLC
=
1060 O
->getLinkeditDataLoadCommand(LC
);
1061 O
->ReadULEB128s(FunctionStartsLC
.dataoff
, FunctionStarts
);
1066 DenseMap
<uint64_t, StringRef
> SymbolNames
;
1067 if (FunctionStartsType
== FunctionStartsMode::Names
||
1068 FunctionStartsType
== FunctionStartsMode::Both
) {
1069 for (SymbolRef Sym
: O
->symbols()) {
1070 if (Expected
<uint64_t> Addr
= Sym
.getAddress()) {
1071 if (Expected
<StringRef
> Name
= Sym
.getName()) {
1072 SymbolNames
[*Addr
] = *Name
;
1078 for (uint64_t S
: FunctionStarts
) {
1079 uint64_t Addr
= BaseSegmentAddress
+ S
;
1080 if (FunctionStartsType
== FunctionStartsMode::Names
) {
1081 auto It
= SymbolNames
.find(Addr
);
1082 if (It
!= SymbolNames
.end())
1083 outs() << It
->second
<< "\n";
1086 outs() << format("%016" PRIx64
, Addr
);
1088 outs() << format("%08" PRIx32
, static_cast<uint32_t>(Addr
));
1090 if (FunctionStartsType
== FunctionStartsMode::Both
) {
1091 auto It
= SymbolNames
.find(Addr
);
1092 if (It
!= SymbolNames
.end())
1093 outs() << " " << It
->second
;
1102 static void PrintDataInCodeTable(MachOObjectFile
*O
, bool verbose
) {
1103 MachO::linkedit_data_command DIC
= O
->getDataInCodeLoadCommand();
1104 uint32_t nentries
= DIC
.datasize
/ sizeof(struct MachO::data_in_code_entry
);
1105 outs() << "Data in code table (" << nentries
<< " entries)\n";
1106 outs() << "offset length kind\n";
1107 for (dice_iterator DI
= O
->begin_dices(), DE
= O
->end_dices(); DI
!= DE
;
1110 DI
->getOffset(Offset
);
1111 outs() << format("0x%08" PRIx32
, Offset
) << " ";
1113 DI
->getLength(Length
);
1114 outs() << format("%6u", Length
) << " ";
1119 case MachO::DICE_KIND_DATA
:
1122 case MachO::DICE_KIND_JUMP_TABLE8
:
1123 outs() << "JUMP_TABLE8";
1125 case MachO::DICE_KIND_JUMP_TABLE16
:
1126 outs() << "JUMP_TABLE16";
1128 case MachO::DICE_KIND_JUMP_TABLE32
:
1129 outs() << "JUMP_TABLE32";
1131 case MachO::DICE_KIND_ABS_JUMP_TABLE32
:
1132 outs() << "ABS_JUMP_TABLE32";
1135 outs() << format("0x%04" PRIx32
, Kind
);
1139 outs() << format("0x%04" PRIx32
, Kind
);
1144 static void PrintLinkOptHints(MachOObjectFile
*O
) {
1145 MachO::linkedit_data_command LohLC
= O
->getLinkOptHintsLoadCommand();
1146 const char *loh
= O
->getData().substr(LohLC
.dataoff
, 1).data();
1147 uint32_t nloh
= LohLC
.datasize
;
1148 outs() << "Linker optimiztion hints (" << nloh
<< " total bytes)\n";
1149 for (uint32_t i
= 0; i
< nloh
;) {
1151 uint64_t identifier
= decodeULEB128((const uint8_t *)(loh
+ i
), &n
);
1153 outs() << " identifier " << identifier
<< " ";
1156 switch (identifier
) {
1158 outs() << "AdrpAdrp\n";
1161 outs() << "AdrpLdr\n";
1164 outs() << "AdrpAddLdr\n";
1167 outs() << "AdrpLdrGotLdr\n";
1170 outs() << "AdrpAddStr\n";
1173 outs() << "AdrpLdrGotStr\n";
1176 outs() << "AdrpAdd\n";
1179 outs() << "AdrpLdrGot\n";
1182 outs() << "Unknown identifier value\n";
1185 uint64_t narguments
= decodeULEB128((const uint8_t *)(loh
+ i
), &n
);
1187 outs() << " narguments " << narguments
<< "\n";
1191 for (uint32_t j
= 0; j
< narguments
; j
++) {
1192 uint64_t value
= decodeULEB128((const uint8_t *)(loh
+ i
), &n
);
1194 outs() << "\tvalue " << format("0x%" PRIx64
, value
) << "\n";
1201 static SmallVector
<std::string
> GetSegmentNames(object::MachOObjectFile
*O
) {
1202 SmallVector
<std::string
> Ret
;
1203 for (const MachOObjectFile::LoadCommandInfo
&Command
: O
->load_commands()) {
1204 if (Command
.C
.cmd
== MachO::LC_SEGMENT
) {
1205 MachO::segment_command SLC
= O
->getSegmentLoadCommand(Command
);
1206 Ret
.push_back(SLC
.segname
);
1207 } else if (Command
.C
.cmd
== MachO::LC_SEGMENT_64
) {
1208 MachO::segment_command_64 SLC
= O
->getSegment64LoadCommand(Command
);
1209 Ret
.push_back(SLC
.segname
);
1216 PrintChainedFixupsHeader(const MachO::dyld_chained_fixups_header
&H
) {
1217 outs() << "chained fixups header (LC_DYLD_CHAINED_FIXUPS)\n";
1218 outs() << " fixups_version = " << H
.fixups_version
<< '\n';
1219 outs() << " starts_offset = " << H
.starts_offset
<< '\n';
1220 outs() << " imports_offset = " << H
.imports_offset
<< '\n';
1221 outs() << " symbols_offset = " << H
.symbols_offset
<< '\n';
1222 outs() << " imports_count = " << H
.imports_count
<< '\n';
1224 outs() << " imports_format = " << H
.imports_format
;
1225 switch (H
.imports_format
) {
1226 case llvm::MachO::DYLD_CHAINED_IMPORT
:
1227 outs() << " (DYLD_CHAINED_IMPORT)";
1229 case llvm::MachO::DYLD_CHAINED_IMPORT_ADDEND
:
1230 outs() << " (DYLD_CHAINED_IMPORT_ADDEND)";
1232 case llvm::MachO::DYLD_CHAINED_IMPORT_ADDEND64
:
1233 outs() << " (DYLD_CHAINED_IMPORT_ADDEND64)";
1238 outs() << " symbols_format = " << H
.symbols_format
;
1239 if (H
.symbols_format
== llvm::MachO::DYLD_CHAINED_SYMBOL_ZLIB
)
1240 outs() << " (zlib compressed)";
1244 static constexpr std::array
<StringRef
, 13> PointerFormats
{
1245 "DYLD_CHAINED_PTR_ARM64E",
1246 "DYLD_CHAINED_PTR_64",
1247 "DYLD_CHAINED_PTR_32",
1248 "DYLD_CHAINED_PTR_32_CACHE",
1249 "DYLD_CHAINED_PTR_32_FIRMWARE",
1250 "DYLD_CHAINED_PTR_64_OFFSET",
1251 "DYLD_CHAINED_PTR_ARM64E_KERNEL",
1252 "DYLD_CHAINED_PTR_64_KERNEL_CACHE",
1253 "DYLD_CHAINED_PTR_ARM64E_USERLAND",
1254 "DYLD_CHAINED_PTR_ARM64E_FIRMWARE",
1255 "DYLD_CHAINED_PTR_X86_64_KERNEL_CACHE",
1256 "DYLD_CHAINED_PTR_ARM64E_USERLAND24",
1259 static void PrintChainedFixupsSegment(const ChainedFixupsSegment
&Segment
,
1260 StringRef SegName
) {
1261 outs() << "chained starts in segment " << Segment
.SegIdx
<< " (" << SegName
1263 outs() << " size = " << Segment
.Header
.size
<< '\n';
1264 outs() << " page_size = " << format("0x%0" PRIx16
, Segment
.Header
.page_size
)
1267 outs() << " pointer_format = " << Segment
.Header
.pointer_format
;
1268 if ((Segment
.Header
.pointer_format
- 1) <
1269 MachO::DYLD_CHAINED_PTR_ARM64E_USERLAND24
)
1270 outs() << " (" << PointerFormats
[Segment
.Header
.pointer_format
- 1] << ")";
1273 outs() << " segment_offset = "
1274 << format("0x%0" PRIx64
, Segment
.Header
.segment_offset
) << '\n';
1275 outs() << " max_valid_pointer = " << Segment
.Header
.max_valid_pointer
1277 outs() << " page_count = " << Segment
.Header
.page_count
<< '\n';
1278 for (auto [Index
, PageStart
] : enumerate(Segment
.PageStarts
)) {
1279 outs() << " page_start[" << Index
<< "] = " << PageStart
;
1280 // FIXME: Support DYLD_CHAINED_PTR_START_MULTI (32-bit only)
1281 if (PageStart
== MachO::DYLD_CHAINED_PTR_START_NONE
)
1282 outs() << " (DYLD_CHAINED_PTR_START_NONE)";
1287 static void PrintChainedFixupTarget(ChainedFixupTarget
&Target
, size_t Idx
,
1288 int Format
, MachOObjectFile
*O
) {
1289 if (Format
== MachO::DYLD_CHAINED_IMPORT
)
1290 outs() << "dyld chained import";
1291 else if (Format
== MachO::DYLD_CHAINED_IMPORT_ADDEND
)
1292 outs() << "dyld chained import addend";
1293 else if (Format
== MachO::DYLD_CHAINED_IMPORT_ADDEND64
)
1294 outs() << "dyld chained import addend64";
1295 // FIXME: otool prints the encoded value as well.
1296 outs() << '[' << Idx
<< "]\n";
1298 outs() << " lib_ordinal = " << Target
.libOrdinal() << " ("
1299 << ordinalName(O
, Target
.libOrdinal()) << ")\n";
1300 outs() << " weak_import = " << Target
.weakImport() << '\n';
1301 outs() << " name_offset = " << Target
.nameOffset() << " ("
1302 << Target
.symbolName() << ")\n";
1303 if (Format
!= MachO::DYLD_CHAINED_IMPORT
)
1304 outs() << " addend = " << (int64_t)Target
.addend() << '\n';
1307 static void PrintChainedFixups(MachOObjectFile
*O
) {
1308 // MachOObjectFile::getChainedFixupsHeader() reads LC_DYLD_CHAINED_FIXUPS.
1309 // FIXME: Support chained fixups in __TEXT,__chain_starts section too.
1310 auto ChainedFixupHeader
=
1311 unwrapOrError(O
->getChainedFixupsHeader(), O
->getFileName());
1312 if (!ChainedFixupHeader
)
1315 PrintChainedFixupsHeader(*ChainedFixupHeader
);
1317 auto [SegCount
, Segments
] =
1318 unwrapOrError(O
->getChainedFixupsSegments(), O
->getFileName());
1320 auto SegNames
= GetSegmentNames(O
);
1322 size_t StartsIdx
= 0;
1323 outs() << "chained starts in image\n";
1324 outs() << " seg_count = " << SegCount
<< '\n';
1325 for (size_t I
= 0; I
< SegCount
; ++I
) {
1326 uint64_t SegOffset
= 0;
1327 if (StartsIdx
< Segments
.size() && I
== Segments
[StartsIdx
].SegIdx
) {
1328 SegOffset
= Segments
[StartsIdx
].Offset
;
1332 outs() << " seg_offset[" << I
<< "] = " << SegOffset
<< " ("
1333 << SegNames
[I
] << ")\n";
1336 for (const ChainedFixupsSegment
&S
: Segments
)
1337 PrintChainedFixupsSegment(S
, SegNames
[S
.SegIdx
]);
1340 unwrapOrError(O
->getDyldChainedFixupTargets(), O
->getFileName());
1342 uint32_t ImportsFormat
= ChainedFixupHeader
->imports_format
;
1343 for (auto [Idx
, Target
] : enumerate(FixupTargets
))
1344 PrintChainedFixupTarget(Target
, Idx
, ImportsFormat
, O
);
1347 static void PrintDyldInfo(MachOObjectFile
*O
) {
1348 Error Err
= Error::success();
1350 size_t SegmentWidth
= strlen("segment");
1351 size_t SectionWidth
= strlen("section");
1352 size_t AddressWidth
= strlen("address");
1353 size_t AddendWidth
= strlen("addend");
1354 size_t DylibWidth
= strlen("dylib");
1355 const size_t PointerWidth
= 2 + O
->getBytesInAddress() * 2;
1357 auto HexLength
= [](uint64_t Num
) {
1358 return Num
? (size_t)divideCeil(Log2_64(Num
), 4) : 1;
1360 for (const object::MachOChainedFixupEntry
&Entry
: O
->fixupTable(Err
)) {
1361 SegmentWidth
= std::max(SegmentWidth
, Entry
.segmentName().size());
1362 SectionWidth
= std::max(SectionWidth
, Entry
.sectionName().size());
1363 AddressWidth
= std::max(AddressWidth
, HexLength(Entry
.address()) + 2);
1364 if (Entry
.isBind()) {
1365 AddendWidth
= std::max(AddendWidth
, HexLength(Entry
.addend()) + 2);
1366 DylibWidth
= std::max(DylibWidth
, Entry
.symbolName().size());
1369 // Errors will be handled when printing the table.
1371 consumeError(std::move(Err
));
1373 outs() << "dyld information:\n";
1374 outs() << left_justify("segment", SegmentWidth
) << ' '
1375 << left_justify("section", SectionWidth
) << ' '
1376 << left_justify("address", AddressWidth
) << ' '
1377 << left_justify("pointer", PointerWidth
) << " type "
1378 << left_justify("addend", AddendWidth
) << ' '
1379 << left_justify("dylib", DylibWidth
) << " symbol/vm address\n";
1380 for (const object::MachOChainedFixupEntry
&Entry
: O
->fixupTable(Err
)) {
1381 outs() << left_justify(Entry
.segmentName(), SegmentWidth
) << ' '
1382 << left_justify(Entry
.sectionName(), SectionWidth
) << ' ' << "0x"
1383 << left_justify(utohexstr(Entry
.address()), AddressWidth
- 2) << ' '
1384 << format_hex(Entry
.rawValue(), PointerWidth
, true) << ' ';
1385 if (Entry
.isBind()) {
1387 << "0x" << left_justify(utohexstr(Entry
.addend()), AddendWidth
- 2)
1388 << ' ' << left_justify(ordinalName(O
, Entry
.ordinal()), DylibWidth
)
1389 << ' ' << Entry
.symbolName();
1390 if (Entry
.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT
)
1391 outs() << " (weak import)";
1394 assert(Entry
.isRebase());
1396 outs().indent(AddendWidth
+ DylibWidth
+ 2);
1397 outs() << format("0x%" PRIX64
, Entry
.pointerValue()) << '\n';
1401 reportError(std::move(Err
), O
->getFileName());
1403 // TODO: Print opcode-based fixups if the object uses those.
1406 static void PrintDylibs(MachOObjectFile
*O
, bool JustId
) {
1408 for (const auto &Load
: O
->load_commands()) {
1409 if ((JustId
&& Load
.C
.cmd
== MachO::LC_ID_DYLIB
) ||
1410 (!JustId
&& (Load
.C
.cmd
== MachO::LC_ID_DYLIB
||
1411 Load
.C
.cmd
== MachO::LC_LOAD_DYLIB
||
1412 Load
.C
.cmd
== MachO::LC_LOAD_WEAK_DYLIB
||
1413 Load
.C
.cmd
== MachO::LC_REEXPORT_DYLIB
||
1414 Load
.C
.cmd
== MachO::LC_LAZY_LOAD_DYLIB
||
1415 Load
.C
.cmd
== MachO::LC_LOAD_UPWARD_DYLIB
))) {
1416 MachO::dylib_command dl
= O
->getDylibIDLoadCommand(Load
);
1417 if (dl
.dylib
.name
< dl
.cmdsize
) {
1418 const char *p
= (const char *)(Load
.Ptr
) + dl
.dylib
.name
;
1420 outs() << p
<< "\n";
1422 outs() << "\t" << p
;
1423 outs() << " (compatibility version "
1424 << ((dl
.dylib
.compatibility_version
>> 16) & 0xffff) << "."
1425 << ((dl
.dylib
.compatibility_version
>> 8) & 0xff) << "."
1426 << (dl
.dylib
.compatibility_version
& 0xff) << ",";
1427 outs() << " current version "
1428 << ((dl
.dylib
.current_version
>> 16) & 0xffff) << "."
1429 << ((dl
.dylib
.current_version
>> 8) & 0xff) << "."
1430 << (dl
.dylib
.current_version
& 0xff);
1431 if (Load
.C
.cmd
== MachO::LC_LOAD_WEAK_DYLIB
)
1433 if (Load
.C
.cmd
== MachO::LC_REEXPORT_DYLIB
)
1434 outs() << ", reexport";
1435 if (Load
.C
.cmd
== MachO::LC_LOAD_UPWARD_DYLIB
)
1436 outs() << ", upward";
1437 if (Load
.C
.cmd
== MachO::LC_LAZY_LOAD_DYLIB
)
1442 outs() << "\tBad offset (" << dl
.dylib
.name
<< ") for name of ";
1443 if (Load
.C
.cmd
== MachO::LC_ID_DYLIB
)
1444 outs() << "LC_ID_DYLIB ";
1445 else if (Load
.C
.cmd
== MachO::LC_LOAD_DYLIB
)
1446 outs() << "LC_LOAD_DYLIB ";
1447 else if (Load
.C
.cmd
== MachO::LC_LOAD_WEAK_DYLIB
)
1448 outs() << "LC_LOAD_WEAK_DYLIB ";
1449 else if (Load
.C
.cmd
== MachO::LC_LAZY_LOAD_DYLIB
)
1450 outs() << "LC_LAZY_LOAD_DYLIB ";
1451 else if (Load
.C
.cmd
== MachO::LC_REEXPORT_DYLIB
)
1452 outs() << "LC_REEXPORT_DYLIB ";
1453 else if (Load
.C
.cmd
== MachO::LC_LOAD_UPWARD_DYLIB
)
1454 outs() << "LC_LOAD_UPWARD_DYLIB ";
1456 outs() << "LC_??? ";
1457 outs() << "command " << Index
++ << "\n";
1463 static void printRpaths(MachOObjectFile
*O
) {
1464 for (const auto &Command
: O
->load_commands()) {
1465 if (Command
.C
.cmd
== MachO::LC_RPATH
) {
1466 auto Rpath
= O
->getRpathCommand(Command
);
1467 const char *P
= (const char *)(Command
.Ptr
) + Rpath
.path
;
1468 outs() << P
<< "\n";
1473 typedef DenseMap
<uint64_t, StringRef
> SymbolAddressMap
;
1475 static void CreateSymbolAddressMap(MachOObjectFile
*O
,
1476 SymbolAddressMap
*AddrMap
) {
1477 // Create a map of symbol addresses to symbol names.
1478 const StringRef FileName
= O
->getFileName();
1479 for (const SymbolRef
&Symbol
: O
->symbols()) {
1480 SymbolRef::Type ST
= unwrapOrError(Symbol
.getType(), FileName
);
1481 if (ST
== SymbolRef::ST_Function
|| ST
== SymbolRef::ST_Data
||
1482 ST
== SymbolRef::ST_Other
) {
1483 uint64_t Address
= cantFail(Symbol
.getValue());
1484 StringRef SymName
= unwrapOrError(Symbol
.getName(), FileName
);
1485 if (!SymName
.startswith(".objc"))
1486 (*AddrMap
)[Address
] = SymName
;
1491 // GuessSymbolName is passed the address of what might be a symbol and a
1492 // pointer to the SymbolAddressMap. It returns the name of a symbol
1493 // with that address or nullptr if no symbol is found with that address.
1494 static const char *GuessSymbolName(uint64_t value
, SymbolAddressMap
*AddrMap
) {
1495 const char *SymbolName
= nullptr;
1496 // A DenseMap can't lookup up some values.
1497 if (value
!= 0xffffffffffffffffULL
&& value
!= 0xfffffffffffffffeULL
) {
1498 StringRef name
= AddrMap
->lookup(value
);
1500 SymbolName
= name
.data();
1505 static void DumpCstringChar(const char c
) {
1509 outs().write_escaped(p
);
1512 static void DumpCstringSection(MachOObjectFile
*O
, const char *sect
,
1513 uint32_t sect_size
, uint64_t sect_addr
,
1514 bool print_addresses
) {
1515 for (uint32_t i
= 0; i
< sect_size
; i
++) {
1516 if (print_addresses
) {
1518 outs() << format("%016" PRIx64
, sect_addr
+ i
) << " ";
1520 outs() << format("%08" PRIx64
, sect_addr
+ i
) << " ";
1522 for (; i
< sect_size
&& sect
[i
] != '\0'; i
++)
1523 DumpCstringChar(sect
[i
]);
1524 if (i
< sect_size
&& sect
[i
] == '\0')
1529 static void DumpLiteral4(uint32_t l
, float f
) {
1530 outs() << format("0x%08" PRIx32
, l
);
1531 if ((l
& 0x7f800000) != 0x7f800000)
1532 outs() << format(" (%.16e)\n", f
);
1534 if (l
== 0x7f800000)
1535 outs() << " (+Infinity)\n";
1536 else if (l
== 0xff800000)
1537 outs() << " (-Infinity)\n";
1538 else if ((l
& 0x00400000) == 0x00400000)
1539 outs() << " (non-signaling Not-a-Number)\n";
1541 outs() << " (signaling Not-a-Number)\n";
1545 static void DumpLiteral4Section(MachOObjectFile
*O
, const char *sect
,
1546 uint32_t sect_size
, uint64_t sect_addr
,
1547 bool print_addresses
) {
1548 for (uint32_t i
= 0; i
< sect_size
; i
+= sizeof(float)) {
1549 if (print_addresses
) {
1551 outs() << format("%016" PRIx64
, sect_addr
+ i
) << " ";
1553 outs() << format("%08" PRIx64
, sect_addr
+ i
) << " ";
1556 memcpy(&f
, sect
+ i
, sizeof(float));
1557 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
1558 sys::swapByteOrder(f
);
1560 memcpy(&l
, sect
+ i
, sizeof(uint32_t));
1561 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
1562 sys::swapByteOrder(l
);
1567 static void DumpLiteral8(MachOObjectFile
*O
, uint32_t l0
, uint32_t l1
,
1569 outs() << format("0x%08" PRIx32
, l0
) << " " << format("0x%08" PRIx32
, l1
);
1571 Hi
= (O
->isLittleEndian()) ? l1
: l0
;
1572 Lo
= (O
->isLittleEndian()) ? l0
: l1
;
1574 // Hi is the high word, so this is equivalent to if(isfinite(d))
1575 if ((Hi
& 0x7ff00000) != 0x7ff00000)
1576 outs() << format(" (%.16e)\n", d
);
1578 if (Hi
== 0x7ff00000 && Lo
== 0)
1579 outs() << " (+Infinity)\n";
1580 else if (Hi
== 0xfff00000 && Lo
== 0)
1581 outs() << " (-Infinity)\n";
1582 else if ((Hi
& 0x00080000) == 0x00080000)
1583 outs() << " (non-signaling Not-a-Number)\n";
1585 outs() << " (signaling Not-a-Number)\n";
1589 static void DumpLiteral8Section(MachOObjectFile
*O
, const char *sect
,
1590 uint32_t sect_size
, uint64_t sect_addr
,
1591 bool print_addresses
) {
1592 for (uint32_t i
= 0; i
< sect_size
; i
+= sizeof(double)) {
1593 if (print_addresses
) {
1595 outs() << format("%016" PRIx64
, sect_addr
+ i
) << " ";
1597 outs() << format("%08" PRIx64
, sect_addr
+ i
) << " ";
1600 memcpy(&d
, sect
+ i
, sizeof(double));
1601 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
1602 sys::swapByteOrder(d
);
1604 memcpy(&l0
, sect
+ i
, sizeof(uint32_t));
1605 memcpy(&l1
, sect
+ i
+ sizeof(uint32_t), sizeof(uint32_t));
1606 if (O
->isLittleEndian() != sys::IsLittleEndianHost
) {
1607 sys::swapByteOrder(l0
);
1608 sys::swapByteOrder(l1
);
1610 DumpLiteral8(O
, l0
, l1
, d
);
1614 static void DumpLiteral16(uint32_t l0
, uint32_t l1
, uint32_t l2
, uint32_t l3
) {
1615 outs() << format("0x%08" PRIx32
, l0
) << " ";
1616 outs() << format("0x%08" PRIx32
, l1
) << " ";
1617 outs() << format("0x%08" PRIx32
, l2
) << " ";
1618 outs() << format("0x%08" PRIx32
, l3
) << "\n";
1621 static void DumpLiteral16Section(MachOObjectFile
*O
, const char *sect
,
1622 uint32_t sect_size
, uint64_t sect_addr
,
1623 bool print_addresses
) {
1624 for (uint32_t i
= 0; i
< sect_size
; i
+= 16) {
1625 if (print_addresses
) {
1627 outs() << format("%016" PRIx64
, sect_addr
+ i
) << " ";
1629 outs() << format("%08" PRIx64
, sect_addr
+ i
) << " ";
1631 uint32_t l0
, l1
, l2
, l3
;
1632 memcpy(&l0
, sect
+ i
, sizeof(uint32_t));
1633 memcpy(&l1
, sect
+ i
+ sizeof(uint32_t), sizeof(uint32_t));
1634 memcpy(&l2
, sect
+ i
+ 2 * sizeof(uint32_t), sizeof(uint32_t));
1635 memcpy(&l3
, sect
+ i
+ 3 * sizeof(uint32_t), sizeof(uint32_t));
1636 if (O
->isLittleEndian() != sys::IsLittleEndianHost
) {
1637 sys::swapByteOrder(l0
);
1638 sys::swapByteOrder(l1
);
1639 sys::swapByteOrder(l2
);
1640 sys::swapByteOrder(l3
);
1642 DumpLiteral16(l0
, l1
, l2
, l3
);
1646 static void DumpLiteralPointerSection(MachOObjectFile
*O
,
1647 const SectionRef
&Section
,
1648 const char *sect
, uint32_t sect_size
,
1650 bool print_addresses
) {
1651 // Collect the literal sections in this Mach-O file.
1652 std::vector
<SectionRef
> LiteralSections
;
1653 for (const SectionRef
&Section
: O
->sections()) {
1654 DataRefImpl Ref
= Section
.getRawDataRefImpl();
1655 uint32_t section_type
;
1657 const MachO::section_64 Sec
= O
->getSection64(Ref
);
1658 section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
1660 const MachO::section Sec
= O
->getSection(Ref
);
1661 section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
1663 if (section_type
== MachO::S_CSTRING_LITERALS
||
1664 section_type
== MachO::S_4BYTE_LITERALS
||
1665 section_type
== MachO::S_8BYTE_LITERALS
||
1666 section_type
== MachO::S_16BYTE_LITERALS
)
1667 LiteralSections
.push_back(Section
);
1670 // Set the size of the literal pointer.
1671 uint32_t lp_size
= O
->is64Bit() ? 8 : 4;
1673 // Collect the external relocation symbols for the literal pointers.
1674 std::vector
<std::pair
<uint64_t, SymbolRef
>> Relocs
;
1675 for (const RelocationRef
&Reloc
: Section
.relocations()) {
1677 MachO::any_relocation_info RE
;
1678 bool isExtern
= false;
1679 Rel
= Reloc
.getRawDataRefImpl();
1680 RE
= O
->getRelocation(Rel
);
1681 isExtern
= O
->getPlainRelocationExternal(RE
);
1683 uint64_t RelocOffset
= Reloc
.getOffset();
1684 symbol_iterator RelocSym
= Reloc
.getSymbol();
1685 Relocs
.push_back(std::make_pair(RelocOffset
, *RelocSym
));
1688 array_pod_sort(Relocs
.begin(), Relocs
.end());
1690 // Dump each literal pointer.
1691 for (uint32_t i
= 0; i
< sect_size
; i
+= lp_size
) {
1692 if (print_addresses
) {
1694 outs() << format("%016" PRIx64
, sect_addr
+ i
) << " ";
1696 outs() << format("%08" PRIx64
, sect_addr
+ i
) << " ";
1700 memcpy(&lp
, sect
+ i
, sizeof(uint64_t));
1701 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
1702 sys::swapByteOrder(lp
);
1705 memcpy(&li
, sect
+ i
, sizeof(uint32_t));
1706 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
1707 sys::swapByteOrder(li
);
1711 // First look for an external relocation entry for this literal pointer.
1712 auto Reloc
= find_if(Relocs
, [&](const std::pair
<uint64_t, SymbolRef
> &P
) {
1713 return P
.first
== i
;
1715 if (Reloc
!= Relocs
.end()) {
1716 symbol_iterator RelocSym
= Reloc
->second
;
1717 StringRef SymName
= unwrapOrError(RelocSym
->getName(), O
->getFileName());
1718 outs() << "external relocation entry for symbol:" << SymName
<< "\n";
1722 // For local references see what the section the literal pointer points to.
1723 auto Sect
= find_if(LiteralSections
, [&](const SectionRef
&R
) {
1724 return lp
>= R
.getAddress() && lp
< R
.getAddress() + R
.getSize();
1726 if (Sect
== LiteralSections
.end()) {
1727 outs() << format("0x%" PRIx64
, lp
) << " (not in a literal section)\n";
1731 uint64_t SectAddress
= Sect
->getAddress();
1732 uint64_t SectSize
= Sect
->getSize();
1735 Expected
<StringRef
> SectNameOrErr
= Sect
->getName();
1737 SectName
= *SectNameOrErr
;
1739 consumeError(SectNameOrErr
.takeError());
1741 DataRefImpl Ref
= Sect
->getRawDataRefImpl();
1742 StringRef SegmentName
= O
->getSectionFinalSegmentName(Ref
);
1743 outs() << SegmentName
<< ":" << SectName
<< ":";
1745 uint32_t section_type
;
1747 const MachO::section_64 Sec
= O
->getSection64(Ref
);
1748 section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
1750 const MachO::section Sec
= O
->getSection(Ref
);
1751 section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
1754 StringRef BytesStr
= unwrapOrError(Sect
->getContents(), O
->getFileName());
1756 const char *Contents
= reinterpret_cast<const char *>(BytesStr
.data());
1758 switch (section_type
) {
1759 case MachO::S_CSTRING_LITERALS
:
1760 for (uint64_t i
= lp
- SectAddress
; i
< SectSize
&& Contents
[i
] != '\0';
1762 DumpCstringChar(Contents
[i
]);
1766 case MachO::S_4BYTE_LITERALS
:
1768 memcpy(&f
, Contents
+ (lp
- SectAddress
), sizeof(float));
1770 memcpy(&l
, Contents
+ (lp
- SectAddress
), sizeof(uint32_t));
1771 if (O
->isLittleEndian() != sys::IsLittleEndianHost
) {
1772 sys::swapByteOrder(f
);
1773 sys::swapByteOrder(l
);
1777 case MachO::S_8BYTE_LITERALS
: {
1779 memcpy(&d
, Contents
+ (lp
- SectAddress
), sizeof(double));
1781 memcpy(&l0
, Contents
+ (lp
- SectAddress
), sizeof(uint32_t));
1782 memcpy(&l1
, Contents
+ (lp
- SectAddress
) + sizeof(uint32_t),
1784 if (O
->isLittleEndian() != sys::IsLittleEndianHost
) {
1785 sys::swapByteOrder(f
);
1786 sys::swapByteOrder(l0
);
1787 sys::swapByteOrder(l1
);
1789 DumpLiteral8(O
, l0
, l1
, d
);
1792 case MachO::S_16BYTE_LITERALS
: {
1793 uint32_t l0
, l1
, l2
, l3
;
1794 memcpy(&l0
, Contents
+ (lp
- SectAddress
), sizeof(uint32_t));
1795 memcpy(&l1
, Contents
+ (lp
- SectAddress
) + sizeof(uint32_t),
1797 memcpy(&l2
, Contents
+ (lp
- SectAddress
) + 2 * sizeof(uint32_t),
1799 memcpy(&l3
, Contents
+ (lp
- SectAddress
) + 3 * sizeof(uint32_t),
1801 if (O
->isLittleEndian() != sys::IsLittleEndianHost
) {
1802 sys::swapByteOrder(l0
);
1803 sys::swapByteOrder(l1
);
1804 sys::swapByteOrder(l2
);
1805 sys::swapByteOrder(l3
);
1807 DumpLiteral16(l0
, l1
, l2
, l3
);
1814 static void DumpInitTermPointerSection(MachOObjectFile
*O
,
1815 const SectionRef
&Section
,
1817 uint32_t sect_size
, uint64_t sect_addr
,
1818 SymbolAddressMap
*AddrMap
,
1821 stride
= (O
->is64Bit()) ? sizeof(uint64_t) : sizeof(uint32_t);
1823 // Collect the external relocation symbols for the pointers.
1824 std::vector
<std::pair
<uint64_t, SymbolRef
>> Relocs
;
1825 for (const RelocationRef
&Reloc
: Section
.relocations()) {
1827 MachO::any_relocation_info RE
;
1828 bool isExtern
= false;
1829 Rel
= Reloc
.getRawDataRefImpl();
1830 RE
= O
->getRelocation(Rel
);
1831 isExtern
= O
->getPlainRelocationExternal(RE
);
1833 uint64_t RelocOffset
= Reloc
.getOffset();
1834 symbol_iterator RelocSym
= Reloc
.getSymbol();
1835 Relocs
.push_back(std::make_pair(RelocOffset
, *RelocSym
));
1838 array_pod_sort(Relocs
.begin(), Relocs
.end());
1840 for (uint32_t i
= 0; i
< sect_size
; i
+= stride
) {
1841 const char *SymbolName
= nullptr;
1844 outs() << format("0x%016" PRIx64
, sect_addr
+ i
* stride
) << " ";
1845 uint64_t pointer_value
;
1846 memcpy(&pointer_value
, sect
+ i
, stride
);
1847 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
1848 sys::swapByteOrder(pointer_value
);
1849 outs() << format("0x%016" PRIx64
, pointer_value
);
1852 outs() << format("0x%08" PRIx64
, sect_addr
+ i
* stride
) << " ";
1853 uint32_t pointer_value
;
1854 memcpy(&pointer_value
, sect
+ i
, stride
);
1855 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
1856 sys::swapByteOrder(pointer_value
);
1857 outs() << format("0x%08" PRIx32
, pointer_value
);
1861 // First look for an external relocation entry for this pointer.
1862 auto Reloc
= find_if(Relocs
, [&](const std::pair
<uint64_t, SymbolRef
> &P
) {
1863 return P
.first
== i
;
1865 if (Reloc
!= Relocs
.end()) {
1866 symbol_iterator RelocSym
= Reloc
->second
;
1867 outs() << " " << unwrapOrError(RelocSym
->getName(), O
->getFileName());
1869 SymbolName
= GuessSymbolName(p
, AddrMap
);
1871 outs() << " " << SymbolName
;
1878 static void DumpRawSectionContents(MachOObjectFile
*O
, const char *sect
,
1879 uint32_t size
, uint64_t addr
) {
1880 uint32_t cputype
= O
->getHeader().cputype
;
1881 if (cputype
== MachO::CPU_TYPE_I386
|| cputype
== MachO::CPU_TYPE_X86_64
) {
1883 for (uint32_t i
= 0; i
< size
; i
+= j
, addr
+= j
) {
1885 outs() << format("%016" PRIx64
, addr
) << "\t";
1887 outs() << format("%08" PRIx64
, addr
) << "\t";
1888 for (j
= 0; j
< 16 && i
+ j
< size
; j
++) {
1889 uint8_t byte_word
= *(sect
+ i
+ j
);
1890 outs() << format("%02" PRIx32
, (uint32_t)byte_word
) << " ";
1896 for (uint32_t i
= 0; i
< size
; i
+= j
, addr
+= j
) {
1898 outs() << format("%016" PRIx64
, addr
) << "\t";
1900 outs() << format("%08" PRIx64
, addr
) << "\t";
1901 for (j
= 0; j
< 4 * sizeof(int32_t) && i
+ j
< size
;
1902 j
+= sizeof(int32_t)) {
1903 if (i
+ j
+ sizeof(int32_t) <= size
) {
1905 memcpy(&long_word
, sect
+ i
+ j
, sizeof(int32_t));
1906 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
1907 sys::swapByteOrder(long_word
);
1908 outs() << format("%08" PRIx32
, long_word
) << " ";
1910 for (uint32_t k
= 0; i
+ j
+ k
< size
; k
++) {
1911 uint8_t byte_word
= *(sect
+ i
+ j
+ k
);
1912 outs() << format("%02" PRIx32
, (uint32_t)byte_word
) << " ";
1921 static void DisassembleMachO(StringRef Filename
, MachOObjectFile
*MachOOF
,
1922 StringRef DisSegName
, StringRef DisSectName
);
1923 static void DumpProtocolSection(MachOObjectFile
*O
, const char *sect
,
1924 uint32_t size
, uint32_t addr
);
1925 static void DumpSectionContents(StringRef Filename
, MachOObjectFile
*O
,
1927 SymbolAddressMap AddrMap
;
1929 CreateSymbolAddressMap(O
, &AddrMap
);
1931 for (unsigned i
= 0; i
< FilterSections
.size(); ++i
) {
1932 StringRef DumpSection
= FilterSections
[i
];
1933 std::pair
<StringRef
, StringRef
> DumpSegSectName
;
1934 DumpSegSectName
= DumpSection
.split(',');
1935 StringRef DumpSegName
, DumpSectName
;
1936 if (!DumpSegSectName
.second
.empty()) {
1937 DumpSegName
= DumpSegSectName
.first
;
1938 DumpSectName
= DumpSegSectName
.second
;
1941 DumpSectName
= DumpSegSectName
.first
;
1943 for (const SectionRef
&Section
: O
->sections()) {
1945 Expected
<StringRef
> SecNameOrErr
= Section
.getName();
1947 SectName
= *SecNameOrErr
;
1949 consumeError(SecNameOrErr
.takeError());
1951 if (!DumpSection
.empty())
1952 FoundSectionSet
.insert(DumpSection
);
1954 DataRefImpl Ref
= Section
.getRawDataRefImpl();
1955 StringRef SegName
= O
->getSectionFinalSegmentName(Ref
);
1956 if ((DumpSegName
.empty() || SegName
== DumpSegName
) &&
1957 (SectName
== DumpSectName
)) {
1959 uint32_t section_flags
;
1961 const MachO::section_64 Sec
= O
->getSection64(Ref
);
1962 section_flags
= Sec
.flags
;
1965 const MachO::section Sec
= O
->getSection(Ref
);
1966 section_flags
= Sec
.flags
;
1968 uint32_t section_type
= section_flags
& MachO::SECTION_TYPE
;
1970 StringRef BytesStr
=
1971 unwrapOrError(Section
.getContents(), O
->getFileName());
1972 const char *sect
= reinterpret_cast<const char *>(BytesStr
.data());
1973 uint32_t sect_size
= BytesStr
.size();
1974 uint64_t sect_addr
= Section
.getAddress();
1977 outs() << "Contents of (" << SegName
<< "," << SectName
1981 if ((section_flags
& MachO::S_ATTR_PURE_INSTRUCTIONS
) ||
1982 (section_flags
& MachO::S_ATTR_SOME_INSTRUCTIONS
)) {
1983 DisassembleMachO(Filename
, O
, SegName
, SectName
);
1986 if (SegName
== "__TEXT" && SectName
== "__info_plist") {
1990 if (SegName
== "__OBJC" && SectName
== "__protocol") {
1991 DumpProtocolSection(O
, sect
, sect_size
, sect_addr
);
1994 switch (section_type
) {
1995 case MachO::S_REGULAR
:
1996 DumpRawSectionContents(O
, sect
, sect_size
, sect_addr
);
1998 case MachO::S_ZEROFILL
:
1999 outs() << "zerofill section and has no contents in the file\n";
2001 case MachO::S_CSTRING_LITERALS
:
2002 DumpCstringSection(O
, sect
, sect_size
, sect_addr
, LeadingAddr
);
2004 case MachO::S_4BYTE_LITERALS
:
2005 DumpLiteral4Section(O
, sect
, sect_size
, sect_addr
, LeadingAddr
);
2007 case MachO::S_8BYTE_LITERALS
:
2008 DumpLiteral8Section(O
, sect
, sect_size
, sect_addr
, LeadingAddr
);
2010 case MachO::S_16BYTE_LITERALS
:
2011 DumpLiteral16Section(O
, sect
, sect_size
, sect_addr
, LeadingAddr
);
2013 case MachO::S_LITERAL_POINTERS
:
2014 DumpLiteralPointerSection(O
, Section
, sect
, sect_size
, sect_addr
,
2017 case MachO::S_MOD_INIT_FUNC_POINTERS
:
2018 case MachO::S_MOD_TERM_FUNC_POINTERS
:
2019 DumpInitTermPointerSection(O
, Section
, sect
, sect_size
, sect_addr
,
2023 outs() << "Unknown section type ("
2024 << format("0x%08" PRIx32
, section_type
) << ")\n";
2025 DumpRawSectionContents(O
, sect
, sect_size
, sect_addr
);
2029 if (section_type
== MachO::S_ZEROFILL
)
2030 outs() << "zerofill section and has no contents in the file\n";
2032 DumpRawSectionContents(O
, sect
, sect_size
, sect_addr
);
2039 static void DumpInfoPlistSectionContents(StringRef Filename
,
2040 MachOObjectFile
*O
) {
2041 for (const SectionRef
&Section
: O
->sections()) {
2043 Expected
<StringRef
> SecNameOrErr
= Section
.getName();
2045 SectName
= *SecNameOrErr
;
2047 consumeError(SecNameOrErr
.takeError());
2049 DataRefImpl Ref
= Section
.getRawDataRefImpl();
2050 StringRef SegName
= O
->getSectionFinalSegmentName(Ref
);
2051 if (SegName
== "__TEXT" && SectName
== "__info_plist") {
2053 outs() << "Contents of (" << SegName
<< "," << SectName
<< ") section\n";
2054 StringRef BytesStr
=
2055 unwrapOrError(Section
.getContents(), O
->getFileName());
2056 const char *sect
= reinterpret_cast<const char *>(BytesStr
.data());
2057 outs() << format("%.*s", BytesStr
.size(), sect
) << "\n";
2063 // checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
2064 // and if it is and there is a list of architecture flags is specified then
2065 // check to make sure this Mach-O file is one of those architectures or all
2066 // architectures were specified. If not then an error is generated and this
2067 // routine returns false. Else it returns true.
2068 static bool checkMachOAndArchFlags(ObjectFile
*O
, StringRef Filename
) {
2069 auto *MachO
= dyn_cast
<MachOObjectFile
>(O
);
2071 if (!MachO
|| ArchAll
|| ArchFlags
.empty())
2074 MachO::mach_header H
;
2075 MachO::mach_header_64 H_64
;
2077 const char *McpuDefault
, *ArchFlag
;
2078 if (MachO
->is64Bit()) {
2079 H_64
= MachO
->MachOObjectFile::getHeader64();
2080 T
= MachOObjectFile::getArchTriple(H_64
.cputype
, H_64
.cpusubtype
,
2081 &McpuDefault
, &ArchFlag
);
2083 H
= MachO
->MachOObjectFile::getHeader();
2084 T
= MachOObjectFile::getArchTriple(H
.cputype
, H
.cpusubtype
,
2085 &McpuDefault
, &ArchFlag
);
2087 const std::string
ArchFlagName(ArchFlag
);
2088 if (!llvm::is_contained(ArchFlags
, ArchFlagName
)) {
2089 WithColor::error(errs(), "llvm-objdump")
2090 << Filename
<< ": no architecture specified.\n";
2096 static void printObjcMetaData(MachOObjectFile
*O
, bool verbose
);
2098 // ProcessMachO() is passed a single opened Mach-O file, which may be an
2099 // archive member and or in a slice of a universal file. It prints the
2100 // the file name and header info and then processes it according to the
2101 // command line options.
2102 static void ProcessMachO(StringRef Name
, MachOObjectFile
*MachOOF
,
2103 StringRef ArchiveMemberName
= StringRef(),
2104 StringRef ArchitectureName
= StringRef()) {
2105 std::unique_ptr
<Dumper
> D
= createMachODumper(*MachOOF
);
2107 // If we are doing some processing here on the Mach-O file print the header
2108 // info. And don't print it otherwise like in the case of printing the
2109 // UniversalHeaders or ArchiveHeaders.
2110 if (Disassemble
|| Relocations
|| PrivateHeaders
|| ExportsTrie
|| Rebase
||
2111 Bind
|| SymbolTable
|| LazyBind
|| WeakBind
|| IndirectSymbols
||
2112 DataInCode
|| FunctionStartsType
!= FunctionStartsMode::None
||
2113 LinkOptHints
|| ChainedFixups
|| DyldInfo
|| DylibsUsed
|| DylibId
||
2114 Rpaths
|| ObjcMetaData
|| (!FilterSections
.empty())) {
2115 if (LeadingHeaders
) {
2117 if (!ArchiveMemberName
.empty())
2118 outs() << '(' << ArchiveMemberName
<< ')';
2119 if (!ArchitectureName
.empty())
2120 outs() << " (architecture " << ArchitectureName
<< ")";
2124 // To use the report_error() form with an ArchiveName and FileName set
2125 // these up based on what is passed for Name and ArchiveMemberName.
2126 StringRef ArchiveName
;
2128 if (!ArchiveMemberName
.empty()) {
2130 FileName
= ArchiveMemberName
;
2132 ArchiveName
= StringRef();
2136 // If we need the symbol table to do the operation then check it here to
2137 // produce a good error message as to where the Mach-O file comes from in
2138 // the error message.
2139 if (Disassemble
|| IndirectSymbols
|| !FilterSections
.empty() || UnwindInfo
)
2140 if (Error Err
= MachOOF
->checkSymbolTable())
2141 reportError(std::move(Err
), FileName
, ArchiveName
, ArchitectureName
);
2143 if (DisassembleAll
) {
2144 for (const SectionRef
&Section
: MachOOF
->sections()) {
2146 if (Expected
<StringRef
> NameOrErr
= Section
.getName())
2147 SectName
= *NameOrErr
;
2149 consumeError(NameOrErr
.takeError());
2151 if (SectName
.equals("__text")) {
2152 DataRefImpl Ref
= Section
.getRawDataRefImpl();
2153 StringRef SegName
= MachOOF
->getSectionFinalSegmentName(Ref
);
2154 DisassembleMachO(FileName
, MachOOF
, SegName
, SectName
);
2158 else if (Disassemble
) {
2159 if (MachOOF
->getHeader().filetype
== MachO::MH_KEXT_BUNDLE
&&
2160 MachOOF
->getHeader().cputype
== MachO::CPU_TYPE_ARM64
)
2161 DisassembleMachO(FileName
, MachOOF
, "__TEXT_EXEC", "__text");
2163 DisassembleMachO(FileName
, MachOOF
, "__TEXT", "__text");
2165 if (IndirectSymbols
)
2166 PrintIndirectSymbols(MachOOF
, Verbose
);
2168 PrintDataInCodeTable(MachOOF
, Verbose
);
2169 if (FunctionStartsType
!= FunctionStartsMode::None
)
2170 PrintFunctionStarts(MachOOF
);
2172 PrintLinkOptHints(MachOOF
);
2174 PrintRelocations(MachOOF
, Verbose
);
2176 printSectionHeaders(*MachOOF
);
2177 if (SectionContents
)
2178 printSectionContents(MachOOF
);
2179 if (!FilterSections
.empty())
2180 DumpSectionContents(FileName
, MachOOF
, Verbose
);
2182 DumpInfoPlistSectionContents(FileName
, MachOOF
);
2184 PrintDyldInfo(MachOOF
);
2186 PrintChainedFixups(MachOOF
);
2188 PrintDylibs(MachOOF
, false);
2190 PrintDylibs(MachOOF
, true);
2192 D
->printSymbolTable(ArchiveName
, ArchitectureName
);
2194 printMachOUnwindInfo(MachOOF
);
2195 if (PrivateHeaders
) {
2196 printMachOFileHeader(MachOOF
);
2197 printMachOLoadCommands(MachOOF
);
2199 if (FirstPrivateHeader
)
2200 printMachOFileHeader(MachOOF
);
2202 printObjcMetaData(MachOOF
, Verbose
);
2204 printExportsTrie(MachOOF
);
2206 printRebaseTable(MachOOF
);
2208 printRpaths(MachOOF
);
2210 printBindTable(MachOOF
);
2212 printLazyBindTable(MachOOF
);
2214 printWeakBindTable(MachOOF
);
2216 if (DwarfDumpType
!= DIDT_Null
) {
2217 std::unique_ptr
<DIContext
> DICtx
= DWARFContext::create(*MachOOF
);
2218 // Dump the complete DWARF structure.
2219 DIDumpOptions DumpOpts
;
2220 DumpOpts
.DumpType
= DwarfDumpType
;
2221 DICtx
->dump(outs(), DumpOpts
);
2225 // printUnknownCPUType() helps print_fat_headers for unknown CPU's.
2226 static void printUnknownCPUType(uint32_t cputype
, uint32_t cpusubtype
) {
2227 outs() << " cputype (" << cputype
<< ")\n";
2228 outs() << " cpusubtype (" << cpusubtype
<< ")\n";
2231 // printCPUType() helps print_fat_headers by printing the cputype and
2232 // pusubtype (symbolically for the one's it knows about).
2233 static void printCPUType(uint32_t cputype
, uint32_t cpusubtype
) {
2235 case MachO::CPU_TYPE_I386
:
2236 switch (cpusubtype
) {
2237 case MachO::CPU_SUBTYPE_I386_ALL
:
2238 outs() << " cputype CPU_TYPE_I386\n";
2239 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
2242 printUnknownCPUType(cputype
, cpusubtype
);
2246 case MachO::CPU_TYPE_X86_64
:
2247 switch (cpusubtype
) {
2248 case MachO::CPU_SUBTYPE_X86_64_ALL
:
2249 outs() << " cputype CPU_TYPE_X86_64\n";
2250 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
2252 case MachO::CPU_SUBTYPE_X86_64_H
:
2253 outs() << " cputype CPU_TYPE_X86_64\n";
2254 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
2257 printUnknownCPUType(cputype
, cpusubtype
);
2261 case MachO::CPU_TYPE_ARM
:
2262 switch (cpusubtype
) {
2263 case MachO::CPU_SUBTYPE_ARM_ALL
:
2264 outs() << " cputype CPU_TYPE_ARM\n";
2265 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
2267 case MachO::CPU_SUBTYPE_ARM_V4T
:
2268 outs() << " cputype CPU_TYPE_ARM\n";
2269 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
2271 case MachO::CPU_SUBTYPE_ARM_V5TEJ
:
2272 outs() << " cputype CPU_TYPE_ARM\n";
2273 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
2275 case MachO::CPU_SUBTYPE_ARM_XSCALE
:
2276 outs() << " cputype CPU_TYPE_ARM\n";
2277 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
2279 case MachO::CPU_SUBTYPE_ARM_V6
:
2280 outs() << " cputype CPU_TYPE_ARM\n";
2281 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
2283 case MachO::CPU_SUBTYPE_ARM_V6M
:
2284 outs() << " cputype CPU_TYPE_ARM\n";
2285 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
2287 case MachO::CPU_SUBTYPE_ARM_V7
:
2288 outs() << " cputype CPU_TYPE_ARM\n";
2289 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
2291 case MachO::CPU_SUBTYPE_ARM_V7EM
:
2292 outs() << " cputype CPU_TYPE_ARM\n";
2293 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
2295 case MachO::CPU_SUBTYPE_ARM_V7K
:
2296 outs() << " cputype CPU_TYPE_ARM\n";
2297 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
2299 case MachO::CPU_SUBTYPE_ARM_V7M
:
2300 outs() << " cputype CPU_TYPE_ARM\n";
2301 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
2303 case MachO::CPU_SUBTYPE_ARM_V7S
:
2304 outs() << " cputype CPU_TYPE_ARM\n";
2305 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
2308 printUnknownCPUType(cputype
, cpusubtype
);
2312 case MachO::CPU_TYPE_ARM64
:
2313 switch (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) {
2314 case MachO::CPU_SUBTYPE_ARM64_ALL
:
2315 outs() << " cputype CPU_TYPE_ARM64\n";
2316 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
2318 case MachO::CPU_SUBTYPE_ARM64_V8
:
2319 outs() << " cputype CPU_TYPE_ARM64\n";
2320 outs() << " cpusubtype CPU_SUBTYPE_ARM64_V8\n";
2322 case MachO::CPU_SUBTYPE_ARM64E
:
2323 outs() << " cputype CPU_TYPE_ARM64\n";
2324 outs() << " cpusubtype CPU_SUBTYPE_ARM64E\n";
2327 printUnknownCPUType(cputype
, cpusubtype
);
2331 case MachO::CPU_TYPE_ARM64_32
:
2332 switch (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) {
2333 case MachO::CPU_SUBTYPE_ARM64_32_V8
:
2334 outs() << " cputype CPU_TYPE_ARM64_32\n";
2335 outs() << " cpusubtype CPU_SUBTYPE_ARM64_32_V8\n";
2338 printUnknownCPUType(cputype
, cpusubtype
);
2343 printUnknownCPUType(cputype
, cpusubtype
);
2348 static void printMachOUniversalHeaders(const object::MachOUniversalBinary
*UB
,
2350 outs() << "Fat headers\n";
2352 if (UB
->getMagic() == MachO::FAT_MAGIC
)
2353 outs() << "fat_magic FAT_MAGIC\n";
2354 else // UB->getMagic() == MachO::FAT_MAGIC_64
2355 outs() << "fat_magic FAT_MAGIC_64\n";
2357 outs() << "fat_magic " << format("0x%" PRIx32
, MachO::FAT_MAGIC
) << "\n";
2359 uint32_t nfat_arch
= UB
->getNumberOfObjects();
2360 StringRef Buf
= UB
->getData();
2361 uint64_t size
= Buf
.size();
2362 uint64_t big_size
= sizeof(struct MachO::fat_header
) +
2363 nfat_arch
* sizeof(struct MachO::fat_arch
);
2364 outs() << "nfat_arch " << UB
->getNumberOfObjects();
2366 outs() << " (malformed, contains zero architecture types)\n";
2367 else if (big_size
> size
)
2368 outs() << " (malformed, architectures past end of file)\n";
2372 for (uint32_t i
= 0; i
< nfat_arch
; ++i
) {
2373 MachOUniversalBinary::ObjectForArch
OFA(UB
, i
);
2374 uint32_t cputype
= OFA
.getCPUType();
2375 uint32_t cpusubtype
= OFA
.getCPUSubType();
2376 outs() << "architecture ";
2377 for (uint32_t j
= 0; i
!= 0 && j
<= i
- 1; j
++) {
2378 MachOUniversalBinary::ObjectForArch
other_OFA(UB
, j
);
2379 uint32_t other_cputype
= other_OFA
.getCPUType();
2380 uint32_t other_cpusubtype
= other_OFA
.getCPUSubType();
2381 if (cputype
!= 0 && cpusubtype
!= 0 && cputype
== other_cputype
&&
2382 (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) ==
2383 (other_cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
)) {
2384 outs() << "(illegal duplicate architecture) ";
2389 outs() << OFA
.getArchFlagName() << "\n";
2390 printCPUType(cputype
, cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
2392 outs() << i
<< "\n";
2393 outs() << " cputype " << cputype
<< "\n";
2394 outs() << " cpusubtype " << (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
)
2398 (cpusubtype
& MachO::CPU_SUBTYPE_MASK
) == MachO::CPU_SUBTYPE_LIB64
)
2399 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
2401 outs() << " capabilities "
2402 << format("0x%" PRIx32
,
2403 (cpusubtype
& MachO::CPU_SUBTYPE_MASK
) >> 24) << "\n";
2404 outs() << " offset " << OFA
.getOffset();
2405 if (OFA
.getOffset() > size
)
2406 outs() << " (past end of file)";
2407 if (OFA
.getOffset() % (1ull << OFA
.getAlign()) != 0)
2408 outs() << " (not aligned on it's alignment (2^" << OFA
.getAlign() << ")";
2410 outs() << " size " << OFA
.getSize();
2411 big_size
= OFA
.getOffset() + OFA
.getSize();
2412 if (big_size
> size
)
2413 outs() << " (past end of file)";
2415 outs() << " align 2^" << OFA
.getAlign() << " (" << (1 << OFA
.getAlign())
2420 static void printArchiveChild(StringRef Filename
, const Archive::Child
&C
,
2421 size_t ChildIndex
, bool verbose
,
2423 StringRef ArchitectureName
= StringRef()) {
2425 outs() << C
.getChildOffset() << "\t";
2426 sys::fs::perms Mode
=
2427 unwrapOrError(C
.getAccessMode(), getFileNameForError(C
, ChildIndex
),
2428 Filename
, ArchitectureName
);
2430 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
2431 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
2433 outs() << ((Mode
& sys::fs::owner_read
) ? "r" : "-");
2434 outs() << ((Mode
& sys::fs::owner_write
) ? "w" : "-");
2435 outs() << ((Mode
& sys::fs::owner_exe
) ? "x" : "-");
2436 outs() << ((Mode
& sys::fs::group_read
) ? "r" : "-");
2437 outs() << ((Mode
& sys::fs::group_write
) ? "w" : "-");
2438 outs() << ((Mode
& sys::fs::group_exe
) ? "x" : "-");
2439 outs() << ((Mode
& sys::fs::others_read
) ? "r" : "-");
2440 outs() << ((Mode
& sys::fs::others_write
) ? "w" : "-");
2441 outs() << ((Mode
& sys::fs::others_exe
) ? "x" : "-");
2443 outs() << format("0%o ", Mode
);
2446 outs() << format("%3d/%-3d %5" PRId64
" ",
2447 unwrapOrError(C
.getUID(), getFileNameForError(C
, ChildIndex
),
2448 Filename
, ArchitectureName
),
2449 unwrapOrError(C
.getGID(), getFileNameForError(C
, ChildIndex
),
2450 Filename
, ArchitectureName
),
2451 unwrapOrError(C
.getRawSize(),
2452 getFileNameForError(C
, ChildIndex
), Filename
,
2455 StringRef RawLastModified
= C
.getRawLastModified();
2458 if (RawLastModified
.getAsInteger(10, Seconds
))
2459 outs() << "(date: \"" << RawLastModified
2460 << "\" contains non-decimal chars) ";
2462 // Since cime(3) returns a 26 character string of the form:
2463 // "Sun Sep 16 01:03:52 1973\n\0"
2464 // just print 24 characters.
2466 outs() << format("%.24s ", ctime(&t
));
2469 outs() << RawLastModified
<< " ";
2473 Expected
<StringRef
> NameOrErr
= C
.getName();
2475 consumeError(NameOrErr
.takeError());
2476 outs() << unwrapOrError(C
.getRawName(),
2477 getFileNameForError(C
, ChildIndex
), Filename
,
2481 StringRef Name
= NameOrErr
.get();
2482 outs() << Name
<< "\n";
2485 outs() << unwrapOrError(C
.getRawName(), getFileNameForError(C
, ChildIndex
),
2486 Filename
, ArchitectureName
)
2491 static void printArchiveHeaders(StringRef Filename
, Archive
*A
, bool verbose
,
2493 StringRef ArchitectureName
= StringRef()) {
2494 Error Err
= Error::success();
2496 for (const auto &C
: A
->children(Err
, false))
2497 printArchiveChild(Filename
, C
, I
++, verbose
, print_offset
,
2501 reportError(std::move(Err
), Filename
, "", ArchitectureName
);
2504 static bool ValidateArchFlags() {
2505 // Check for -arch all and verifiy the -arch flags are valid.
2506 for (unsigned i
= 0; i
< ArchFlags
.size(); ++i
) {
2507 if (ArchFlags
[i
] == "all") {
2510 if (!MachOObjectFile::isValidArch(ArchFlags
[i
])) {
2511 WithColor::error(errs(), "llvm-objdump")
2512 << "unknown architecture named '" + ArchFlags
[i
] +
2513 "'for the -arch option\n";
2521 // ParseInputMachO() parses the named Mach-O file in Filename and handles the
2522 // -arch flags selecting just those slices as specified by them and also parses
2523 // archive files. Then for each individual Mach-O file ProcessMachO() is
2524 // called to process the file based on the command line options.
2525 void objdump::parseInputMachO(StringRef Filename
) {
2526 if (!ValidateArchFlags())
2529 // Attempt to open the binary.
2530 Expected
<OwningBinary
<Binary
>> BinaryOrErr
= createBinary(Filename
);
2532 if (Error E
= isNotObjectErrorInvalidFileType(BinaryOrErr
.takeError()))
2533 reportError(std::move(E
), Filename
);
2535 outs() << Filename
<< ": is not an object file\n";
2538 Binary
&Bin
= *BinaryOrErr
.get().getBinary();
2540 if (Archive
*A
= dyn_cast
<Archive
>(&Bin
)) {
2541 outs() << "Archive : " << Filename
<< "\n";
2543 printArchiveHeaders(Filename
, A
, Verbose
, ArchiveMemberOffsets
);
2545 Error Err
= Error::success();
2547 for (auto &C
: A
->children(Err
)) {
2549 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
= C
.getAsBinary();
2551 if (Error E
= isNotObjectErrorInvalidFileType(ChildOrErr
.takeError()))
2552 reportError(std::move(E
), getFileNameForError(C
, I
), Filename
);
2555 if (MachOObjectFile
*O
= dyn_cast
<MachOObjectFile
>(&*ChildOrErr
.get())) {
2556 if (!checkMachOAndArchFlags(O
, Filename
))
2558 ProcessMachO(Filename
, O
, O
->getFileName());
2562 reportError(std::move(Err
), Filename
);
2565 if (MachOUniversalBinary
*UB
= dyn_cast
<MachOUniversalBinary
>(&Bin
)) {
2566 parseInputMachO(UB
);
2569 if (ObjectFile
*O
= dyn_cast
<ObjectFile
>(&Bin
)) {
2570 if (!checkMachOAndArchFlags(O
, Filename
))
2572 if (MachOObjectFile
*MachOOF
= dyn_cast
<MachOObjectFile
>(&*O
))
2573 ProcessMachO(Filename
, MachOOF
);
2575 WithColor::error(errs(), "llvm-objdump")
2576 << Filename
<< "': "
2577 << "object is not a Mach-O file type.\n";
2580 llvm_unreachable("Input object can't be invalid at this point");
2583 void objdump::parseInputMachO(MachOUniversalBinary
*UB
) {
2584 if (!ValidateArchFlags())
2587 auto Filename
= UB
->getFileName();
2589 if (UniversalHeaders
)
2590 printMachOUniversalHeaders(UB
, Verbose
);
2592 // If we have a list of architecture flags specified dump only those.
2593 if (!ArchAll
&& !ArchFlags
.empty()) {
2594 // Look for a slice in the universal binary that matches each ArchFlag.
2596 for (unsigned i
= 0; i
< ArchFlags
.size(); ++i
) {
2598 for (MachOUniversalBinary::object_iterator I
= UB
->begin_objects(),
2599 E
= UB
->end_objects();
2601 if (ArchFlags
[i
] == I
->getArchFlagName()) {
2603 Expected
<std::unique_ptr
<ObjectFile
>> ObjOrErr
=
2604 I
->getAsObjectFile();
2605 std::string ArchitectureName
;
2606 if (ArchFlags
.size() > 1)
2607 ArchitectureName
= I
->getArchFlagName();
2609 ObjectFile
&O
= *ObjOrErr
.get();
2610 if (MachOObjectFile
*MachOOF
= dyn_cast
<MachOObjectFile
>(&O
))
2611 ProcessMachO(Filename
, MachOOF
, "", ArchitectureName
);
2612 } else if (Error E
= isNotObjectErrorInvalidFileType(
2613 ObjOrErr
.takeError())) {
2614 reportError(std::move(E
), "", Filename
, ArchitectureName
);
2616 } else if (Expected
<std::unique_ptr
<Archive
>> AOrErr
=
2617 I
->getAsArchive()) {
2618 std::unique_ptr
<Archive
> &A
= *AOrErr
;
2619 outs() << "Archive : " << Filename
;
2620 if (!ArchitectureName
.empty())
2621 outs() << " (architecture " << ArchitectureName
<< ")";
2624 printArchiveHeaders(Filename
, A
.get(), Verbose
,
2625 ArchiveMemberOffsets
, ArchitectureName
);
2626 Error Err
= Error::success();
2628 for (auto &C
: A
->children(Err
)) {
2630 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
= C
.getAsBinary();
2633 isNotObjectErrorInvalidFileType(ChildOrErr
.takeError()))
2634 reportError(std::move(E
), getFileNameForError(C
, I
), Filename
,
2638 if (MachOObjectFile
*O
=
2639 dyn_cast
<MachOObjectFile
>(&*ChildOrErr
.get()))
2640 ProcessMachO(Filename
, O
, O
->getFileName(), ArchitectureName
);
2643 reportError(std::move(Err
), Filename
);
2645 consumeError(AOrErr
.takeError());
2646 reportError(Filename
,
2647 "Mach-O universal file for architecture " +
2648 StringRef(I
->getArchFlagName()) +
2649 " is not a Mach-O file or an archive file");
2654 WithColor::error(errs(), "llvm-objdump")
2655 << "file: " + Filename
+ " does not contain "
2656 << "architecture: " + ArchFlags
[i
] + "\n";
2662 // No architecture flags were specified so if this contains a slice that
2663 // matches the host architecture dump only that.
2665 for (MachOUniversalBinary::object_iterator I
= UB
->begin_objects(),
2666 E
= UB
->end_objects();
2668 if (MachOObjectFile::getHostArch().getArchName() ==
2669 I
->getArchFlagName()) {
2670 Expected
<std::unique_ptr
<ObjectFile
>> ObjOrErr
= I
->getAsObjectFile();
2671 std::string ArchiveName
;
2672 ArchiveName
.clear();
2674 ObjectFile
&O
= *ObjOrErr
.get();
2675 if (MachOObjectFile
*MachOOF
= dyn_cast
<MachOObjectFile
>(&O
))
2676 ProcessMachO(Filename
, MachOOF
);
2677 } else if (Error E
=
2678 isNotObjectErrorInvalidFileType(ObjOrErr
.takeError())) {
2679 reportError(std::move(E
), Filename
);
2680 } else if (Expected
<std::unique_ptr
<Archive
>> AOrErr
=
2681 I
->getAsArchive()) {
2682 std::unique_ptr
<Archive
> &A
= *AOrErr
;
2683 outs() << "Archive : " << Filename
<< "\n";
2685 printArchiveHeaders(Filename
, A
.get(), Verbose
,
2686 ArchiveMemberOffsets
);
2687 Error Err
= Error::success();
2689 for (auto &C
: A
->children(Err
)) {
2691 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
= C
.getAsBinary();
2694 isNotObjectErrorInvalidFileType(ChildOrErr
.takeError()))
2695 reportError(std::move(E
), getFileNameForError(C
, I
), Filename
);
2698 if (MachOObjectFile
*O
=
2699 dyn_cast
<MachOObjectFile
>(&*ChildOrErr
.get()))
2700 ProcessMachO(Filename
, O
, O
->getFileName());
2703 reportError(std::move(Err
), Filename
);
2705 consumeError(AOrErr
.takeError());
2706 reportError(Filename
, "Mach-O universal file for architecture " +
2707 StringRef(I
->getArchFlagName()) +
2708 " is not a Mach-O file or an archive file");
2714 // Either all architectures have been specified or none have been specified
2715 // and this does not contain the host architecture so dump all the slices.
2716 bool moreThanOneArch
= UB
->getNumberOfObjects() > 1;
2717 for (MachOUniversalBinary::object_iterator I
= UB
->begin_objects(),
2718 E
= UB
->end_objects();
2720 Expected
<std::unique_ptr
<ObjectFile
>> ObjOrErr
= I
->getAsObjectFile();
2721 std::string ArchitectureName
;
2722 if (moreThanOneArch
)
2723 ArchitectureName
= I
->getArchFlagName();
2725 ObjectFile
&Obj
= *ObjOrErr
.get();
2726 if (MachOObjectFile
*MachOOF
= dyn_cast
<MachOObjectFile
>(&Obj
))
2727 ProcessMachO(Filename
, MachOOF
, "", ArchitectureName
);
2728 } else if (Error E
=
2729 isNotObjectErrorInvalidFileType(ObjOrErr
.takeError())) {
2730 reportError(std::move(E
), Filename
, "", ArchitectureName
);
2731 } else if (Expected
<std::unique_ptr
<Archive
>> AOrErr
= I
->getAsArchive()) {
2732 std::unique_ptr
<Archive
> &A
= *AOrErr
;
2733 outs() << "Archive : " << Filename
;
2734 if (!ArchitectureName
.empty())
2735 outs() << " (architecture " << ArchitectureName
<< ")";
2738 printArchiveHeaders(Filename
, A
.get(), Verbose
, ArchiveMemberOffsets
,
2740 Error Err
= Error::success();
2742 for (auto &C
: A
->children(Err
)) {
2744 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
= C
.getAsBinary();
2746 if (Error E
= isNotObjectErrorInvalidFileType(ChildOrErr
.takeError()))
2747 reportError(std::move(E
), getFileNameForError(C
, I
), Filename
,
2751 if (MachOObjectFile
*O
=
2752 dyn_cast
<MachOObjectFile
>(&*ChildOrErr
.get())) {
2753 if (MachOObjectFile
*MachOOF
= dyn_cast
<MachOObjectFile
>(O
))
2754 ProcessMachO(Filename
, MachOOF
, MachOOF
->getFileName(),
2759 reportError(std::move(Err
), Filename
);
2761 consumeError(AOrErr
.takeError());
2762 reportError(Filename
, "Mach-O universal file for architecture " +
2763 StringRef(I
->getArchFlagName()) +
2764 " is not a Mach-O file or an archive file");
2770 // The block of info used by the Symbolizer call backs.
2771 struct DisassembleInfo
{
2772 DisassembleInfo(MachOObjectFile
*O
, SymbolAddressMap
*AddrMap
,
2773 std::vector
<SectionRef
> *Sections
, bool verbose
)
2774 : verbose(verbose
), O(O
), AddrMap(AddrMap
), Sections(Sections
) {}
2778 SymbolAddressMap
*AddrMap
;
2779 std::vector
<SectionRef
> *Sections
;
2780 const char *class_name
= nullptr;
2781 const char *selector_name
= nullptr;
2782 std::unique_ptr
<char[]> method
= nullptr;
2783 char *demangled_name
= nullptr;
2784 uint64_t adrp_addr
= 0;
2785 uint32_t adrp_inst
= 0;
2786 std::unique_ptr
<SymbolAddressMap
> bindtable
;
2791 // SymbolizerGetOpInfo() is the operand information call back function.
2792 // This is called to get the symbolic information for operand(s) of an
2793 // instruction when it is being done. This routine does this from
2794 // the relocation information, symbol table, etc. That block of information
2795 // is a pointer to the struct DisassembleInfo that was passed when the
2796 // disassembler context was created and passed to back to here when
2797 // called back by the disassembler for instruction operands that could have
2798 // relocation information. The address of the instruction containing operand is
2799 // at the Pc parameter. The immediate value the operand has is passed in
2800 // op_info->Value and is at Offset past the start of the instruction and has a
2801 // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
2802 // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
2803 // names and addends of the symbolic expression to add for the operand. The
2804 // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
2805 // information is returned then this function returns 1 else it returns 0.
2806 static int SymbolizerGetOpInfo(void *DisInfo
, uint64_t Pc
, uint64_t Offset
,
2807 uint64_t OpSize
, uint64_t InstSize
, int TagType
,
2809 struct DisassembleInfo
*info
= (struct DisassembleInfo
*)DisInfo
;
2810 struct LLVMOpInfo1
*op_info
= (struct LLVMOpInfo1
*)TagBuf
;
2811 uint64_t value
= op_info
->Value
;
2813 // Make sure all fields returned are zero if we don't set them.
2814 memset((void *)op_info
, '\0', sizeof(struct LLVMOpInfo1
));
2815 op_info
->Value
= value
;
2817 // If the TagType is not the value 1 which it code knows about or if no
2818 // verbose symbolic information is wanted then just return 0, indicating no
2819 // information is being returned.
2820 if (TagType
!= 1 || !info
->verbose
)
2823 unsigned int Arch
= info
->O
->getArch();
2824 if (Arch
== Triple::x86
) {
2825 if (OpSize
!= 1 && OpSize
!= 2 && OpSize
!= 4 && OpSize
!= 0)
2827 if (info
->O
->getHeader().filetype
!= MachO::MH_OBJECT
) {
2829 // Search the external relocation entries of a fully linked image
2830 // (if any) for an entry that matches this segment offset.
2831 // uint32_t seg_offset = (Pc + Offset);
2834 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2835 // for an entry for this section offset.
2836 uint32_t sect_addr
= info
->S
.getAddress();
2837 uint32_t sect_offset
= (Pc
+ Offset
) - sect_addr
;
2838 bool reloc_found
= false;
2840 MachO::any_relocation_info RE
;
2841 bool isExtern
= false;
2843 bool r_scattered
= false;
2844 uint32_t r_value
, pair_r_value
, r_type
;
2845 for (const RelocationRef
&Reloc
: info
->S
.relocations()) {
2846 uint64_t RelocOffset
= Reloc
.getOffset();
2847 if (RelocOffset
== sect_offset
) {
2848 Rel
= Reloc
.getRawDataRefImpl();
2849 RE
= info
->O
->getRelocation(Rel
);
2850 r_type
= info
->O
->getAnyRelocationType(RE
);
2851 r_scattered
= info
->O
->isRelocationScattered(RE
);
2853 r_value
= info
->O
->getScatteredRelocationValue(RE
);
2854 if (r_type
== MachO::GENERIC_RELOC_SECTDIFF
||
2855 r_type
== MachO::GENERIC_RELOC_LOCAL_SECTDIFF
) {
2856 DataRefImpl RelNext
= Rel
;
2857 info
->O
->moveRelocationNext(RelNext
);
2858 MachO::any_relocation_info RENext
;
2859 RENext
= info
->O
->getRelocation(RelNext
);
2860 if (info
->O
->isRelocationScattered(RENext
))
2861 pair_r_value
= info
->O
->getScatteredRelocationValue(RENext
);
2866 isExtern
= info
->O
->getPlainRelocationExternal(RE
);
2868 symbol_iterator RelocSym
= Reloc
.getSymbol();
2876 if (reloc_found
&& isExtern
) {
2877 op_info
->AddSymbol
.Present
= 1;
2878 op_info
->AddSymbol
.Name
=
2879 unwrapOrError(Symbol
.getName(), info
->O
->getFileName()).data();
2880 // For i386 extern relocation entries the value in the instruction is
2881 // the offset from the symbol, and value is already set in op_info->Value.
2884 if (reloc_found
&& (r_type
== MachO::GENERIC_RELOC_SECTDIFF
||
2885 r_type
== MachO::GENERIC_RELOC_LOCAL_SECTDIFF
)) {
2886 const char *add
= GuessSymbolName(r_value
, info
->AddrMap
);
2887 const char *sub
= GuessSymbolName(pair_r_value
, info
->AddrMap
);
2888 uint32_t offset
= value
- (r_value
- pair_r_value
);
2889 op_info
->AddSymbol
.Present
= 1;
2891 op_info
->AddSymbol
.Name
= add
;
2893 op_info
->AddSymbol
.Value
= r_value
;
2894 op_info
->SubtractSymbol
.Present
= 1;
2896 op_info
->SubtractSymbol
.Name
= sub
;
2898 op_info
->SubtractSymbol
.Value
= pair_r_value
;
2899 op_info
->Value
= offset
;
2904 if (Arch
== Triple::x86_64
) {
2905 if (OpSize
!= 1 && OpSize
!= 2 && OpSize
!= 4 && OpSize
!= 0)
2907 // For non MH_OBJECT types, like MH_KEXT_BUNDLE, Search the external
2908 // relocation entries of a linked image (if any) for an entry that matches
2909 // this segment offset.
2910 if (info
->O
->getHeader().filetype
!= MachO::MH_OBJECT
) {
2911 uint64_t seg_offset
= Pc
+ Offset
;
2912 bool reloc_found
= false;
2914 MachO::any_relocation_info RE
;
2915 bool isExtern
= false;
2917 for (const RelocationRef
&Reloc
: info
->O
->external_relocations()) {
2918 uint64_t RelocOffset
= Reloc
.getOffset();
2919 if (RelocOffset
== seg_offset
) {
2920 Rel
= Reloc
.getRawDataRefImpl();
2921 RE
= info
->O
->getRelocation(Rel
);
2922 // external relocation entries should always be external.
2923 isExtern
= info
->O
->getPlainRelocationExternal(RE
);
2925 symbol_iterator RelocSym
= Reloc
.getSymbol();
2932 if (reloc_found
&& isExtern
) {
2933 // The Value passed in will be adjusted by the Pc if the instruction
2934 // adds the Pc. But for x86_64 external relocation entries the Value
2935 // is the offset from the external symbol.
2936 if (info
->O
->getAnyRelocationPCRel(RE
))
2937 op_info
->Value
-= Pc
+ InstSize
;
2939 unwrapOrError(Symbol
.getName(), info
->O
->getFileName()).data();
2940 op_info
->AddSymbol
.Present
= 1;
2941 op_info
->AddSymbol
.Name
= name
;
2946 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2947 // for an entry for this section offset.
2948 uint64_t sect_addr
= info
->S
.getAddress();
2949 uint64_t sect_offset
= (Pc
+ Offset
) - sect_addr
;
2950 bool reloc_found
= false;
2952 MachO::any_relocation_info RE
;
2953 bool isExtern
= false;
2955 for (const RelocationRef
&Reloc
: info
->S
.relocations()) {
2956 uint64_t RelocOffset
= Reloc
.getOffset();
2957 if (RelocOffset
== sect_offset
) {
2958 Rel
= Reloc
.getRawDataRefImpl();
2959 RE
= info
->O
->getRelocation(Rel
);
2960 // NOTE: Scattered relocations don't exist on x86_64.
2961 isExtern
= info
->O
->getPlainRelocationExternal(RE
);
2963 symbol_iterator RelocSym
= Reloc
.getSymbol();
2970 if (reloc_found
&& isExtern
) {
2971 // The Value passed in will be adjusted by the Pc if the instruction
2972 // adds the Pc. But for x86_64 external relocation entries the Value
2973 // is the offset from the external symbol.
2974 if (info
->O
->getAnyRelocationPCRel(RE
))
2975 op_info
->Value
-= Pc
+ InstSize
;
2977 unwrapOrError(Symbol
.getName(), info
->O
->getFileName()).data();
2978 unsigned Type
= info
->O
->getAnyRelocationType(RE
);
2979 if (Type
== MachO::X86_64_RELOC_SUBTRACTOR
) {
2980 DataRefImpl RelNext
= Rel
;
2981 info
->O
->moveRelocationNext(RelNext
);
2982 MachO::any_relocation_info RENext
= info
->O
->getRelocation(RelNext
);
2983 unsigned TypeNext
= info
->O
->getAnyRelocationType(RENext
);
2984 bool isExternNext
= info
->O
->getPlainRelocationExternal(RENext
);
2985 unsigned SymbolNum
= info
->O
->getPlainRelocationSymbolNum(RENext
);
2986 if (TypeNext
== MachO::X86_64_RELOC_UNSIGNED
&& isExternNext
) {
2987 op_info
->SubtractSymbol
.Present
= 1;
2988 op_info
->SubtractSymbol
.Name
= name
;
2989 symbol_iterator RelocSymNext
= info
->O
->getSymbolByIndex(SymbolNum
);
2990 Symbol
= *RelocSymNext
;
2991 name
= unwrapOrError(Symbol
.getName(), info
->O
->getFileName()).data();
2994 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
2995 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
2996 op_info
->AddSymbol
.Present
= 1;
2997 op_info
->AddSymbol
.Name
= name
;
3002 if (Arch
== Triple::arm
) {
3003 if (Offset
!= 0 || (InstSize
!= 4 && InstSize
!= 2))
3005 if (info
->O
->getHeader().filetype
!= MachO::MH_OBJECT
) {
3007 // Search the external relocation entries of a fully linked image
3008 // (if any) for an entry that matches this segment offset.
3009 // uint32_t seg_offset = (Pc + Offset);
3012 // In MH_OBJECT filetypes search the section's relocation entries (if any)
3013 // for an entry for this section offset.
3014 uint32_t sect_addr
= info
->S
.getAddress();
3015 uint32_t sect_offset
= (Pc
+ Offset
) - sect_addr
;
3017 MachO::any_relocation_info RE
;
3018 bool isExtern
= false;
3020 bool r_scattered
= false;
3021 uint32_t r_value
, pair_r_value
, r_type
, r_length
, other_half
;
3023 find_if(info
->S
.relocations(), [&](const RelocationRef
&Reloc
) {
3024 uint64_t RelocOffset
= Reloc
.getOffset();
3025 return RelocOffset
== sect_offset
;
3028 if (Reloc
== info
->S
.relocations().end())
3031 Rel
= Reloc
->getRawDataRefImpl();
3032 RE
= info
->O
->getRelocation(Rel
);
3033 r_length
= info
->O
->getAnyRelocationLength(RE
);
3034 r_scattered
= info
->O
->isRelocationScattered(RE
);
3036 r_value
= info
->O
->getScatteredRelocationValue(RE
);
3037 r_type
= info
->O
->getScatteredRelocationType(RE
);
3039 r_type
= info
->O
->getAnyRelocationType(RE
);
3040 isExtern
= info
->O
->getPlainRelocationExternal(RE
);
3042 symbol_iterator RelocSym
= Reloc
->getSymbol();
3046 if (r_type
== MachO::ARM_RELOC_HALF
||
3047 r_type
== MachO::ARM_RELOC_SECTDIFF
||
3048 r_type
== MachO::ARM_RELOC_LOCAL_SECTDIFF
||
3049 r_type
== MachO::ARM_RELOC_HALF_SECTDIFF
) {
3050 DataRefImpl RelNext
= Rel
;
3051 info
->O
->moveRelocationNext(RelNext
);
3052 MachO::any_relocation_info RENext
;
3053 RENext
= info
->O
->getRelocation(RelNext
);
3054 other_half
= info
->O
->getAnyRelocationAddress(RENext
) & 0xffff;
3055 if (info
->O
->isRelocationScattered(RENext
))
3056 pair_r_value
= info
->O
->getScatteredRelocationValue(RENext
);
3061 unwrapOrError(Symbol
.getName(), info
->O
->getFileName()).data();
3062 op_info
->AddSymbol
.Present
= 1;
3063 op_info
->AddSymbol
.Name
= name
;
3065 case MachO::ARM_RELOC_HALF
:
3066 if ((r_length
& 0x1) == 1) {
3067 op_info
->Value
= value
<< 16 | other_half
;
3068 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM_HI16
;
3070 op_info
->Value
= other_half
<< 16 | value
;
3071 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM_LO16
;
3079 // If we have a branch that is not an external relocation entry then
3080 // return 0 so the code in tryAddingSymbolicOperand() can use the
3081 // SymbolLookUp call back with the branch target address to look up the
3082 // symbol and possibility add an annotation for a symbol stub.
3083 if (isExtern
== 0 && (r_type
== MachO::ARM_RELOC_BR24
||
3084 r_type
== MachO::ARM_THUMB_RELOC_BR22
))
3087 uint32_t offset
= 0;
3088 if (r_type
== MachO::ARM_RELOC_HALF
||
3089 r_type
== MachO::ARM_RELOC_HALF_SECTDIFF
) {
3090 if ((r_length
& 0x1) == 1)
3091 value
= value
<< 16 | other_half
;
3093 value
= other_half
<< 16 | value
;
3095 if (r_scattered
&& (r_type
!= MachO::ARM_RELOC_HALF
&&
3096 r_type
!= MachO::ARM_RELOC_HALF_SECTDIFF
)) {
3097 offset
= value
- r_value
;
3101 if (r_type
== MachO::ARM_RELOC_HALF_SECTDIFF
) {
3102 if ((r_length
& 0x1) == 1)
3103 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM_HI16
;
3105 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM_LO16
;
3106 const char *add
= GuessSymbolName(r_value
, info
->AddrMap
);
3107 const char *sub
= GuessSymbolName(pair_r_value
, info
->AddrMap
);
3108 int32_t offset
= value
- (r_value
- pair_r_value
);
3109 op_info
->AddSymbol
.Present
= 1;
3111 op_info
->AddSymbol
.Name
= add
;
3113 op_info
->AddSymbol
.Value
= r_value
;
3114 op_info
->SubtractSymbol
.Present
= 1;
3116 op_info
->SubtractSymbol
.Name
= sub
;
3118 op_info
->SubtractSymbol
.Value
= pair_r_value
;
3119 op_info
->Value
= offset
;
3123 op_info
->AddSymbol
.Present
= 1;
3124 op_info
->Value
= offset
;
3125 if (r_type
== MachO::ARM_RELOC_HALF
) {
3126 if ((r_length
& 0x1) == 1)
3127 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM_HI16
;
3129 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM_LO16
;
3131 const char *add
= GuessSymbolName(value
, info
->AddrMap
);
3132 if (add
!= nullptr) {
3133 op_info
->AddSymbol
.Name
= add
;
3136 op_info
->AddSymbol
.Value
= value
;
3139 if (Arch
== Triple::aarch64
) {
3140 if (Offset
!= 0 || InstSize
!= 4)
3142 if (info
->O
->getHeader().filetype
!= MachO::MH_OBJECT
) {
3144 // Search the external relocation entries of a fully linked image
3145 // (if any) for an entry that matches this segment offset.
3146 // uint64_t seg_offset = (Pc + Offset);
3149 // In MH_OBJECT filetypes search the section's relocation entries (if any)
3150 // for an entry for this section offset.
3151 uint64_t sect_addr
= info
->S
.getAddress();
3152 uint64_t sect_offset
= (Pc
+ Offset
) - sect_addr
;
3154 find_if(info
->S
.relocations(), [&](const RelocationRef
&Reloc
) {
3155 uint64_t RelocOffset
= Reloc
.getOffset();
3156 return RelocOffset
== sect_offset
;
3159 if (Reloc
== info
->S
.relocations().end())
3162 DataRefImpl Rel
= Reloc
->getRawDataRefImpl();
3163 MachO::any_relocation_info RE
= info
->O
->getRelocation(Rel
);
3164 uint32_t r_type
= info
->O
->getAnyRelocationType(RE
);
3165 if (r_type
== MachO::ARM64_RELOC_ADDEND
) {
3166 DataRefImpl RelNext
= Rel
;
3167 info
->O
->moveRelocationNext(RelNext
);
3168 MachO::any_relocation_info RENext
= info
->O
->getRelocation(RelNext
);
3170 value
= info
->O
->getPlainRelocationSymbolNum(RENext
);
3171 op_info
->Value
= value
;
3174 // NOTE: Scattered relocations don't exist on arm64.
3175 if (!info
->O
->getPlainRelocationExternal(RE
))
3178 unwrapOrError(Reloc
->getSymbol()->getName(), info
->O
->getFileName())
3180 op_info
->AddSymbol
.Present
= 1;
3181 op_info
->AddSymbol
.Name
= name
;
3184 case MachO::ARM64_RELOC_PAGE21
:
3186 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM64_PAGE
;
3188 case MachO::ARM64_RELOC_PAGEOFF12
:
3190 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM64_PAGEOFF
;
3192 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21
:
3194 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM64_GOTPAGE
;
3196 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12
:
3198 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF
;
3200 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21
:
3201 /* @tvlppage is not implemented in llvm-mc */
3202 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM64_TLVP
;
3204 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12
:
3205 /* @tvlppageoff is not implemented in llvm-mc */
3206 op_info
->VariantKind
= LLVMDisassembler_VariantKind_ARM64_TLVOFF
;
3209 case MachO::ARM64_RELOC_BRANCH26
:
3210 op_info
->VariantKind
= LLVMDisassembler_VariantKind_None
;
3218 // GuessCstringPointer is passed the address of what might be a pointer to a
3219 // literal string in a cstring section. If that address is in a cstring section
3220 // it returns a pointer to that string. Else it returns nullptr.
3221 static const char *GuessCstringPointer(uint64_t ReferenceValue
,
3222 struct DisassembleInfo
*info
) {
3223 for (const auto &Load
: info
->O
->load_commands()) {
3224 if (Load
.C
.cmd
== MachO::LC_SEGMENT_64
) {
3225 MachO::segment_command_64 Seg
= info
->O
->getSegment64LoadCommand(Load
);
3226 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
3227 MachO::section_64 Sec
= info
->O
->getSection64(Load
, J
);
3228 uint32_t section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
3229 if (section_type
== MachO::S_CSTRING_LITERALS
&&
3230 ReferenceValue
>= Sec
.addr
&&
3231 ReferenceValue
< Sec
.addr
+ Sec
.size
) {
3232 uint64_t sect_offset
= ReferenceValue
- Sec
.addr
;
3233 uint64_t object_offset
= Sec
.offset
+ sect_offset
;
3234 StringRef MachOContents
= info
->O
->getData();
3235 uint64_t object_size
= MachOContents
.size();
3236 const char *object_addr
= (const char *)MachOContents
.data();
3237 if (object_offset
< object_size
) {
3238 const char *name
= object_addr
+ object_offset
;
3245 } else if (Load
.C
.cmd
== MachO::LC_SEGMENT
) {
3246 MachO::segment_command Seg
= info
->O
->getSegmentLoadCommand(Load
);
3247 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
3248 MachO::section Sec
= info
->O
->getSection(Load
, J
);
3249 uint32_t section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
3250 if (section_type
== MachO::S_CSTRING_LITERALS
&&
3251 ReferenceValue
>= Sec
.addr
&&
3252 ReferenceValue
< Sec
.addr
+ Sec
.size
) {
3253 uint64_t sect_offset
= ReferenceValue
- Sec
.addr
;
3254 uint64_t object_offset
= Sec
.offset
+ sect_offset
;
3255 StringRef MachOContents
= info
->O
->getData();
3256 uint64_t object_size
= MachOContents
.size();
3257 const char *object_addr
= (const char *)MachOContents
.data();
3258 if (object_offset
< object_size
) {
3259 const char *name
= object_addr
+ object_offset
;
3271 // GuessIndirectSymbol returns the name of the indirect symbol for the
3272 // ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
3273 // an address of a symbol stub or a lazy or non-lazy pointer to associate the
3274 // symbol name being referenced by the stub or pointer.
3275 static const char *GuessIndirectSymbol(uint64_t ReferenceValue
,
3276 struct DisassembleInfo
*info
) {
3277 MachO::dysymtab_command Dysymtab
= info
->O
->getDysymtabLoadCommand();
3278 MachO::symtab_command Symtab
= info
->O
->getSymtabLoadCommand();
3279 for (const auto &Load
: info
->O
->load_commands()) {
3280 if (Load
.C
.cmd
== MachO::LC_SEGMENT_64
) {
3281 MachO::segment_command_64 Seg
= info
->O
->getSegment64LoadCommand(Load
);
3282 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
3283 MachO::section_64 Sec
= info
->O
->getSection64(Load
, J
);
3284 uint32_t section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
3285 if ((section_type
== MachO::S_NON_LAZY_SYMBOL_POINTERS
||
3286 section_type
== MachO::S_LAZY_SYMBOL_POINTERS
||
3287 section_type
== MachO::S_LAZY_DYLIB_SYMBOL_POINTERS
||
3288 section_type
== MachO::S_THREAD_LOCAL_VARIABLE_POINTERS
||
3289 section_type
== MachO::S_SYMBOL_STUBS
) &&
3290 ReferenceValue
>= Sec
.addr
&&
3291 ReferenceValue
< Sec
.addr
+ Sec
.size
) {
3293 if (section_type
== MachO::S_SYMBOL_STUBS
)
3294 stride
= Sec
.reserved2
;
3299 uint32_t index
= Sec
.reserved1
+ (ReferenceValue
- Sec
.addr
) / stride
;
3300 if (index
< Dysymtab
.nindirectsyms
) {
3301 uint32_t indirect_symbol
=
3302 info
->O
->getIndirectSymbolTableEntry(Dysymtab
, index
);
3303 if (indirect_symbol
< Symtab
.nsyms
) {
3304 symbol_iterator Sym
= info
->O
->getSymbolByIndex(indirect_symbol
);
3305 return unwrapOrError(Sym
->getName(), info
->O
->getFileName())
3311 } else if (Load
.C
.cmd
== MachO::LC_SEGMENT
) {
3312 MachO::segment_command Seg
= info
->O
->getSegmentLoadCommand(Load
);
3313 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
3314 MachO::section Sec
= info
->O
->getSection(Load
, J
);
3315 uint32_t section_type
= Sec
.flags
& MachO::SECTION_TYPE
;
3316 if ((section_type
== MachO::S_NON_LAZY_SYMBOL_POINTERS
||
3317 section_type
== MachO::S_LAZY_SYMBOL_POINTERS
||
3318 section_type
== MachO::S_LAZY_DYLIB_SYMBOL_POINTERS
||
3319 section_type
== MachO::S_THREAD_LOCAL_VARIABLE_POINTERS
||
3320 section_type
== MachO::S_SYMBOL_STUBS
) &&
3321 ReferenceValue
>= Sec
.addr
&&
3322 ReferenceValue
< Sec
.addr
+ Sec
.size
) {
3324 if (section_type
== MachO::S_SYMBOL_STUBS
)
3325 stride
= Sec
.reserved2
;
3330 uint32_t index
= Sec
.reserved1
+ (ReferenceValue
- Sec
.addr
) / stride
;
3331 if (index
< Dysymtab
.nindirectsyms
) {
3332 uint32_t indirect_symbol
=
3333 info
->O
->getIndirectSymbolTableEntry(Dysymtab
, index
);
3334 if (indirect_symbol
< Symtab
.nsyms
) {
3335 symbol_iterator Sym
= info
->O
->getSymbolByIndex(indirect_symbol
);
3336 return unwrapOrError(Sym
->getName(), info
->O
->getFileName())
3347 // method_reference() is called passing it the ReferenceName that might be
3348 // a reference it to an Objective-C method call. If so then it allocates and
3349 // assembles a method call string with the values last seen and saved in
3350 // the DisassembleInfo's class_name and selector_name fields. This is saved
3351 // into the method field of the info and any previous string is free'ed.
3352 // Then the class_name field in the info is set to nullptr. The method call
3353 // string is set into ReferenceName and ReferenceType is set to
3354 // LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
3355 // then both ReferenceType and ReferenceName are left unchanged.
3356 static void method_reference(struct DisassembleInfo
*info
,
3357 uint64_t *ReferenceType
,
3358 const char **ReferenceName
) {
3359 unsigned int Arch
= info
->O
->getArch();
3360 if (*ReferenceName
!= nullptr) {
3361 if (strcmp(*ReferenceName
, "_objc_msgSend") == 0) {
3362 if (info
->selector_name
!= nullptr) {
3363 if (info
->class_name
!= nullptr) {
3364 info
->method
= std::make_unique
<char[]>(
3365 5 + strlen(info
->class_name
) + strlen(info
->selector_name
));
3366 char *method
= info
->method
.get();
3367 if (method
!= nullptr) {
3368 strcpy(method
, "+[");
3369 strcat(method
, info
->class_name
);
3370 strcat(method
, " ");
3371 strcat(method
, info
->selector_name
);
3372 strcat(method
, "]");
3373 *ReferenceName
= method
;
3374 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_Objc_Message
;
3378 std::make_unique
<char[]>(9 + strlen(info
->selector_name
));
3379 char *method
= info
->method
.get();
3380 if (method
!= nullptr) {
3381 if (Arch
== Triple::x86_64
)
3382 strcpy(method
, "-[%rdi ");
3383 else if (Arch
== Triple::aarch64
)
3384 strcpy(method
, "-[x0 ");
3386 strcpy(method
, "-[r? ");
3387 strcat(method
, info
->selector_name
);
3388 strcat(method
, "]");
3389 *ReferenceName
= method
;
3390 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_Objc_Message
;
3393 info
->class_name
= nullptr;
3395 } else if (strcmp(*ReferenceName
, "_objc_msgSendSuper2") == 0) {
3396 if (info
->selector_name
!= nullptr) {
3398 std::make_unique
<char[]>(17 + strlen(info
->selector_name
));
3399 char *method
= info
->method
.get();
3400 if (method
!= nullptr) {
3401 if (Arch
== Triple::x86_64
)
3402 strcpy(method
, "-[[%rdi super] ");
3403 else if (Arch
== Triple::aarch64
)
3404 strcpy(method
, "-[[x0 super] ");
3406 strcpy(method
, "-[[r? super] ");
3407 strcat(method
, info
->selector_name
);
3408 strcat(method
, "]");
3409 *ReferenceName
= method
;
3410 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_Objc_Message
;
3412 info
->class_name
= nullptr;
3418 // GuessPointerPointer() is passed the address of what might be a pointer to
3419 // a reference to an Objective-C class, selector, message ref or cfstring.
3420 // If so the value of the pointer is returned and one of the booleans are set
3421 // to true. If not zero is returned and all the booleans are set to false.
3422 static uint64_t GuessPointerPointer(uint64_t ReferenceValue
,
3423 struct DisassembleInfo
*info
,
3424 bool &classref
, bool &selref
, bool &msgref
,
3430 for (const auto &Load
: info
->O
->load_commands()) {
3431 if (Load
.C
.cmd
== MachO::LC_SEGMENT_64
) {
3432 MachO::segment_command_64 Seg
= info
->O
->getSegment64LoadCommand(Load
);
3433 for (unsigned J
= 0; J
< Seg
.nsects
; ++J
) {
3434 MachO::section_64 Sec
= info
->O
->getSection64(Load
, J
);
3435 if ((strncmp(Sec
.sectname
, "__objc_selrefs", 16) == 0 ||
3436 strncmp(Sec
.sectname
, "__objc_classrefs", 16) == 0 ||
3437 strncmp(Sec
.sectname
, "__objc_superrefs", 16) == 0 ||
3438 strncmp(Sec
.sectname
, "__objc_msgrefs", 16) == 0 ||
3439 strncmp(Sec
.sectname
, "__cfstring", 16) == 0) &&
3440 ReferenceValue
>= Sec
.addr
&&
3441 ReferenceValue
< Sec
.addr
+ Sec
.size
) {
3442 uint64_t sect_offset
= ReferenceValue
- Sec
.addr
;
3443 uint64_t object_offset
= Sec
.offset
+ sect_offset
;
3444 StringRef MachOContents
= info
->O
->getData();
3445 uint64_t object_size
= MachOContents
.size();
3446 const char *object_addr
= (const char *)MachOContents
.data();
3447 if (object_offset
< object_size
) {
3448 uint64_t pointer_value
;
3449 memcpy(&pointer_value
, object_addr
+ object_offset
,
3451 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
3452 sys::swapByteOrder(pointer_value
);
3453 if (strncmp(Sec
.sectname
, "__objc_selrefs", 16) == 0)
3455 else if (strncmp(Sec
.sectname
, "__objc_classrefs", 16) == 0 ||
3456 strncmp(Sec
.sectname
, "__objc_superrefs", 16) == 0)
3458 else if (strncmp(Sec
.sectname
, "__objc_msgrefs", 16) == 0 &&
3459 ReferenceValue
+ 8 < Sec
.addr
+ Sec
.size
) {
3461 memcpy(&pointer_value
, object_addr
+ object_offset
+ 8,
3463 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
3464 sys::swapByteOrder(pointer_value
);
3465 } else if (strncmp(Sec
.sectname
, "__cfstring", 16) == 0)
3467 return pointer_value
;
3474 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
3479 // get_pointer_64 returns a pointer to the bytes in the object file at the
3480 // Address from a section in the Mach-O file. And indirectly returns the
3481 // offset into the section, number of bytes left in the section past the offset
3482 // and which section is was being referenced. If the Address is not in a
3483 // section nullptr is returned.
3484 static const char *get_pointer_64(uint64_t Address
, uint32_t &offset
,
3485 uint32_t &left
, SectionRef
&S
,
3486 DisassembleInfo
*info
,
3487 bool objc_only
= false) {
3491 for (unsigned SectIdx
= 0; SectIdx
!= info
->Sections
->size(); SectIdx
++) {
3492 uint64_t SectAddress
= ((*(info
->Sections
))[SectIdx
]).getAddress();
3493 uint64_t SectSize
= ((*(info
->Sections
))[SectIdx
]).getSize();
3498 Expected
<StringRef
> SecNameOrErr
=
3499 ((*(info
->Sections
))[SectIdx
]).getName();
3501 SectName
= *SecNameOrErr
;
3503 consumeError(SecNameOrErr
.takeError());
3505 DataRefImpl Ref
= ((*(info
->Sections
))[SectIdx
]).getRawDataRefImpl();
3506 StringRef SegName
= info
->O
->getSectionFinalSegmentName(Ref
);
3507 if (SegName
!= "__OBJC" && SectName
!= "__cstring")
3510 if (Address
>= SectAddress
&& Address
< SectAddress
+ SectSize
) {
3511 S
= (*(info
->Sections
))[SectIdx
];
3512 offset
= Address
- SectAddress
;
3513 left
= SectSize
- offset
;
3514 StringRef SectContents
= unwrapOrError(
3515 ((*(info
->Sections
))[SectIdx
]).getContents(), info
->O
->getFileName());
3516 return SectContents
.data() + offset
;
3522 static const char *get_pointer_32(uint32_t Address
, uint32_t &offset
,
3523 uint32_t &left
, SectionRef
&S
,
3524 DisassembleInfo
*info
,
3525 bool objc_only
= false) {
3526 return get_pointer_64(Address
, offset
, left
, S
, info
, objc_only
);
3529 // get_symbol_64() returns the name of a symbol (or nullptr) and the address of
3530 // the symbol indirectly through n_value. Based on the relocation information
3531 // for the specified section offset in the specified section reference.
3532 // If no relocation information is found and a non-zero ReferenceValue for the
3533 // symbol is passed, look up that address in the info's AddrMap.
3534 static const char *get_symbol_64(uint32_t sect_offset
, SectionRef S
,
3535 DisassembleInfo
*info
, uint64_t &n_value
,
3536 uint64_t ReferenceValue
= 0) {
3541 // See if there is an external relocation entry at the sect_offset.
3542 bool reloc_found
= false;
3544 MachO::any_relocation_info RE
;
3545 bool isExtern
= false;
3547 for (const RelocationRef
&Reloc
: S
.relocations()) {
3548 uint64_t RelocOffset
= Reloc
.getOffset();
3549 if (RelocOffset
== sect_offset
) {
3550 Rel
= Reloc
.getRawDataRefImpl();
3551 RE
= info
->O
->getRelocation(Rel
);
3552 if (info
->O
->isRelocationScattered(RE
))
3554 isExtern
= info
->O
->getPlainRelocationExternal(RE
);
3556 symbol_iterator RelocSym
= Reloc
.getSymbol();
3563 // If there is an external relocation entry for a symbol in this section
3564 // at this section_offset then use that symbol's value for the n_value
3565 // and return its name.
3566 const char *SymbolName
= nullptr;
3567 if (reloc_found
&& isExtern
) {
3568 n_value
= cantFail(Symbol
.getValue());
3569 StringRef Name
= unwrapOrError(Symbol
.getName(), info
->O
->getFileName());
3570 if (!Name
.empty()) {
3571 SymbolName
= Name
.data();
3576 // TODO: For fully linked images, look through the external relocation
3577 // entries off the dynamic symtab command. For these the r_offset is from the
3578 // start of the first writeable segment in the Mach-O file. So the offset
3579 // to this section from that segment is passed to this routine by the caller,
3580 // as the database_offset. Which is the difference of the section's starting
3581 // address and the first writable segment.
3583 // NOTE: need add passing the database_offset to this routine.
3585 // We did not find an external relocation entry so look up the ReferenceValue
3586 // as an address of a symbol and if found return that symbol's name.
3587 SymbolName
= GuessSymbolName(ReferenceValue
, info
->AddrMap
);
3592 static const char *get_symbol_32(uint32_t sect_offset
, SectionRef S
,
3593 DisassembleInfo
*info
,
3594 uint32_t ReferenceValue
) {
3596 return get_symbol_64(sect_offset
, S
, info
, n_value64
, ReferenceValue
);
3601 // These are structs in the Objective-C meta data and read to produce the
3602 // comments for disassembly. While these are part of the ABI they are no
3603 // public defintions. So the are here not in include/llvm/BinaryFormat/MachO.h
3606 // The cfstring object in a 64-bit Mach-O file.
3607 struct cfstring64_t
{
3608 uint64_t isa
; // class64_t * (64-bit pointer)
3609 uint64_t flags
; // flag bits
3610 uint64_t characters
; // char * (64-bit pointer)
3611 uint64_t length
; // number of non-NULL characters in above
3614 // The class object in a 64-bit Mach-O file.
3616 uint64_t isa
; // class64_t * (64-bit pointer)
3617 uint64_t superclass
; // class64_t * (64-bit pointer)
3618 uint64_t cache
; // Cache (64-bit pointer)
3619 uint64_t vtable
; // IMP * (64-bit pointer)
3620 uint64_t data
; // class_ro64_t * (64-bit pointer)
3624 uint32_t isa
; /* class32_t * (32-bit pointer) */
3625 uint32_t superclass
; /* class32_t * (32-bit pointer) */
3626 uint32_t cache
; /* Cache (32-bit pointer) */
3627 uint32_t vtable
; /* IMP * (32-bit pointer) */
3628 uint32_t data
; /* class_ro32_t * (32-bit pointer) */
3631 struct class_ro64_t
{
3633 uint32_t instanceStart
;
3634 uint32_t instanceSize
;
3636 uint64_t ivarLayout
; // const uint8_t * (64-bit pointer)
3637 uint64_t name
; // const char * (64-bit pointer)
3638 uint64_t baseMethods
; // const method_list_t * (64-bit pointer)
3639 uint64_t baseProtocols
; // const protocol_list_t * (64-bit pointer)
3640 uint64_t ivars
; // const ivar_list_t * (64-bit pointer)
3641 uint64_t weakIvarLayout
; // const uint8_t * (64-bit pointer)
3642 uint64_t baseProperties
; // const struct objc_property_list (64-bit pointer)
3645 struct class_ro32_t
{
3647 uint32_t instanceStart
;
3648 uint32_t instanceSize
;
3649 uint32_t ivarLayout
; /* const uint8_t * (32-bit pointer) */
3650 uint32_t name
; /* const char * (32-bit pointer) */
3651 uint32_t baseMethods
; /* const method_list_t * (32-bit pointer) */
3652 uint32_t baseProtocols
; /* const protocol_list_t * (32-bit pointer) */
3653 uint32_t ivars
; /* const ivar_list_t * (32-bit pointer) */
3654 uint32_t weakIvarLayout
; /* const uint8_t * (32-bit pointer) */
3655 uint32_t baseProperties
; /* const struct objc_property_list *
3659 /* Values for class_ro{64,32}_t->flags */
3660 #define RO_META (1 << 0)
3661 #define RO_ROOT (1 << 1)
3662 #define RO_HAS_CXX_STRUCTORS (1 << 2)
3664 struct method_list64_t
{
3667 /* struct method64_t first; These structures follow inline */
3670 struct method_list32_t
{
3673 /* struct method32_t first; These structures follow inline */
3677 uint64_t name
; /* SEL (64-bit pointer) */
3678 uint64_t types
; /* const char * (64-bit pointer) */
3679 uint64_t imp
; /* IMP (64-bit pointer) */
3683 uint32_t name
; /* SEL (32-bit pointer) */
3684 uint32_t types
; /* const char * (32-bit pointer) */
3685 uint32_t imp
; /* IMP (32-bit pointer) */
3688 struct protocol_list64_t
{
3689 uint64_t count
; /* uintptr_t (a 64-bit value) */
3690 /* struct protocol64_t * list[0]; These pointers follow inline */
3693 struct protocol_list32_t
{
3694 uint32_t count
; /* uintptr_t (a 32-bit value) */
3695 /* struct protocol32_t * list[0]; These pointers follow inline */
3698 struct protocol64_t
{
3699 uint64_t isa
; /* id * (64-bit pointer) */
3700 uint64_t name
; /* const char * (64-bit pointer) */
3701 uint64_t protocols
; /* struct protocol_list64_t *
3703 uint64_t instanceMethods
; /* method_list_t * (64-bit pointer) */
3704 uint64_t classMethods
; /* method_list_t * (64-bit pointer) */
3705 uint64_t optionalInstanceMethods
; /* method_list_t * (64-bit pointer) */
3706 uint64_t optionalClassMethods
; /* method_list_t * (64-bit pointer) */
3707 uint64_t instanceProperties
; /* struct objc_property_list *
3711 struct protocol32_t
{
3712 uint32_t isa
; /* id * (32-bit pointer) */
3713 uint32_t name
; /* const char * (32-bit pointer) */
3714 uint32_t protocols
; /* struct protocol_list_t *
3716 uint32_t instanceMethods
; /* method_list_t * (32-bit pointer) */
3717 uint32_t classMethods
; /* method_list_t * (32-bit pointer) */
3718 uint32_t optionalInstanceMethods
; /* method_list_t * (32-bit pointer) */
3719 uint32_t optionalClassMethods
; /* method_list_t * (32-bit pointer) */
3720 uint32_t instanceProperties
; /* struct objc_property_list *
3724 struct ivar_list64_t
{
3727 /* struct ivar64_t first; These structures follow inline */
3730 struct ivar_list32_t
{
3733 /* struct ivar32_t first; These structures follow inline */
3737 uint64_t offset
; /* uintptr_t * (64-bit pointer) */
3738 uint64_t name
; /* const char * (64-bit pointer) */
3739 uint64_t type
; /* const char * (64-bit pointer) */
3745 uint32_t offset
; /* uintptr_t * (32-bit pointer) */
3746 uint32_t name
; /* const char * (32-bit pointer) */
3747 uint32_t type
; /* const char * (32-bit pointer) */
3752 struct objc_property_list64
{
3755 /* struct objc_property64 first; These structures follow inline */
3758 struct objc_property_list32
{
3761 /* struct objc_property32 first; These structures follow inline */
3764 struct objc_property64
{
3765 uint64_t name
; /* const char * (64-bit pointer) */
3766 uint64_t attributes
; /* const char * (64-bit pointer) */
3769 struct objc_property32
{
3770 uint32_t name
; /* const char * (32-bit pointer) */
3771 uint32_t attributes
; /* const char * (32-bit pointer) */
3774 struct category64_t
{
3775 uint64_t name
; /* const char * (64-bit pointer) */
3776 uint64_t cls
; /* struct class_t * (64-bit pointer) */
3777 uint64_t instanceMethods
; /* struct method_list_t * (64-bit pointer) */
3778 uint64_t classMethods
; /* struct method_list_t * (64-bit pointer) */
3779 uint64_t protocols
; /* struct protocol_list_t * (64-bit pointer) */
3780 uint64_t instanceProperties
; /* struct objc_property_list *
3784 struct category32_t
{
3785 uint32_t name
; /* const char * (32-bit pointer) */
3786 uint32_t cls
; /* struct class_t * (32-bit pointer) */
3787 uint32_t instanceMethods
; /* struct method_list_t * (32-bit pointer) */
3788 uint32_t classMethods
; /* struct method_list_t * (32-bit pointer) */
3789 uint32_t protocols
; /* struct protocol_list_t * (32-bit pointer) */
3790 uint32_t instanceProperties
; /* struct objc_property_list *
3794 struct objc_image_info64
{
3798 struct objc_image_info32
{
3802 struct imageInfo_t
{
3806 /* masks for objc_image_info.flags */
3807 #define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
3808 #define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
3809 #define OBJC_IMAGE_IS_SIMULATED (1 << 5)
3810 #define OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES (1 << 6)
3812 struct message_ref64
{
3813 uint64_t imp
; /* IMP (64-bit pointer) */
3814 uint64_t sel
; /* SEL (64-bit pointer) */
3817 struct message_ref32
{
3818 uint32_t imp
; /* IMP (32-bit pointer) */
3819 uint32_t sel
; /* SEL (32-bit pointer) */
3822 // Objective-C 1 (32-bit only) meta data structs.
3824 struct objc_module_t
{
3827 uint32_t name
; /* char * (32-bit pointer) */
3828 uint32_t symtab
; /* struct objc_symtab * (32-bit pointer) */
3831 struct objc_symtab_t
{
3832 uint32_t sel_ref_cnt
;
3833 uint32_t refs
; /* SEL * (32-bit pointer) */
3834 uint16_t cls_def_cnt
;
3835 uint16_t cat_def_cnt
;
3836 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
3839 struct objc_class_t
{
3840 uint32_t isa
; /* struct objc_class * (32-bit pointer) */
3841 uint32_t super_class
; /* struct objc_class * (32-bit pointer) */
3842 uint32_t name
; /* const char * (32-bit pointer) */
3845 int32_t instance_size
;
3846 uint32_t ivars
; /* struct objc_ivar_list * (32-bit pointer) */
3847 uint32_t methodLists
; /* struct objc_method_list ** (32-bit pointer) */
3848 uint32_t cache
; /* struct objc_cache * (32-bit pointer) */
3849 uint32_t protocols
; /* struct objc_protocol_list * (32-bit pointer) */
3852 #define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
3853 // class is not a metaclass
3854 #define CLS_CLASS 0x1
3855 // class is a metaclass
3856 #define CLS_META 0x2
3858 struct objc_category_t
{
3859 uint32_t category_name
; /* char * (32-bit pointer) */
3860 uint32_t class_name
; /* char * (32-bit pointer) */
3861 uint32_t instance_methods
; /* struct objc_method_list * (32-bit pointer) */
3862 uint32_t class_methods
; /* struct objc_method_list * (32-bit pointer) */
3863 uint32_t protocols
; /* struct objc_protocol_list * (32-bit ptr) */
3866 struct objc_ivar_t
{
3867 uint32_t ivar_name
; /* char * (32-bit pointer) */
3868 uint32_t ivar_type
; /* char * (32-bit pointer) */
3869 int32_t ivar_offset
;
3872 struct objc_ivar_list_t
{
3874 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
3877 struct objc_method_list_t
{
3878 uint32_t obsolete
; /* struct objc_method_list * (32-bit pointer) */
3879 int32_t method_count
;
3880 // struct objc_method_t method_list[1]; /* variable length structure */
3883 struct objc_method_t
{
3884 uint32_t method_name
; /* SEL, aka struct objc_selector * (32-bit pointer) */
3885 uint32_t method_types
; /* char * (32-bit pointer) */
3886 uint32_t method_imp
; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
3890 struct objc_protocol_list_t
{
3891 uint32_t next
; /* struct objc_protocol_list * (32-bit pointer) */
3893 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
3894 // (32-bit pointer) */
3897 struct objc_protocol_t
{
3898 uint32_t isa
; /* struct objc_class * (32-bit pointer) */
3899 uint32_t protocol_name
; /* char * (32-bit pointer) */
3900 uint32_t protocol_list
; /* struct objc_protocol_list * (32-bit pointer) */
3901 uint32_t instance_methods
; /* struct objc_method_description_list *
3903 uint32_t class_methods
; /* struct objc_method_description_list *
3907 struct objc_method_description_list_t
{
3909 // struct objc_method_description_t list[1];
3912 struct objc_method_description_t
{
3913 uint32_t name
; /* SEL, aka struct objc_selector * (32-bit pointer) */
3914 uint32_t types
; /* char * (32-bit pointer) */
3917 inline void swapStruct(struct cfstring64_t
&cfs
) {
3918 sys::swapByteOrder(cfs
.isa
);
3919 sys::swapByteOrder(cfs
.flags
);
3920 sys::swapByteOrder(cfs
.characters
);
3921 sys::swapByteOrder(cfs
.length
);
3924 inline void swapStruct(struct class64_t
&c
) {
3925 sys::swapByteOrder(c
.isa
);
3926 sys::swapByteOrder(c
.superclass
);
3927 sys::swapByteOrder(c
.cache
);
3928 sys::swapByteOrder(c
.vtable
);
3929 sys::swapByteOrder(c
.data
);
3932 inline void swapStruct(struct class32_t
&c
) {
3933 sys::swapByteOrder(c
.isa
);
3934 sys::swapByteOrder(c
.superclass
);
3935 sys::swapByteOrder(c
.cache
);
3936 sys::swapByteOrder(c
.vtable
);
3937 sys::swapByteOrder(c
.data
);
3940 inline void swapStruct(struct class_ro64_t
&cro
) {
3941 sys::swapByteOrder(cro
.flags
);
3942 sys::swapByteOrder(cro
.instanceStart
);
3943 sys::swapByteOrder(cro
.instanceSize
);
3944 sys::swapByteOrder(cro
.reserved
);
3945 sys::swapByteOrder(cro
.ivarLayout
);
3946 sys::swapByteOrder(cro
.name
);
3947 sys::swapByteOrder(cro
.baseMethods
);
3948 sys::swapByteOrder(cro
.baseProtocols
);
3949 sys::swapByteOrder(cro
.ivars
);
3950 sys::swapByteOrder(cro
.weakIvarLayout
);
3951 sys::swapByteOrder(cro
.baseProperties
);
3954 inline void swapStruct(struct class_ro32_t
&cro
) {
3955 sys::swapByteOrder(cro
.flags
);
3956 sys::swapByteOrder(cro
.instanceStart
);
3957 sys::swapByteOrder(cro
.instanceSize
);
3958 sys::swapByteOrder(cro
.ivarLayout
);
3959 sys::swapByteOrder(cro
.name
);
3960 sys::swapByteOrder(cro
.baseMethods
);
3961 sys::swapByteOrder(cro
.baseProtocols
);
3962 sys::swapByteOrder(cro
.ivars
);
3963 sys::swapByteOrder(cro
.weakIvarLayout
);
3964 sys::swapByteOrder(cro
.baseProperties
);
3967 inline void swapStruct(struct method_list64_t
&ml
) {
3968 sys::swapByteOrder(ml
.entsize
);
3969 sys::swapByteOrder(ml
.count
);
3972 inline void swapStruct(struct method_list32_t
&ml
) {
3973 sys::swapByteOrder(ml
.entsize
);
3974 sys::swapByteOrder(ml
.count
);
3977 inline void swapStruct(struct method64_t
&m
) {
3978 sys::swapByteOrder(m
.name
);
3979 sys::swapByteOrder(m
.types
);
3980 sys::swapByteOrder(m
.imp
);
3983 inline void swapStruct(struct method32_t
&m
) {
3984 sys::swapByteOrder(m
.name
);
3985 sys::swapByteOrder(m
.types
);
3986 sys::swapByteOrder(m
.imp
);
3989 inline void swapStruct(struct protocol_list64_t
&pl
) {
3990 sys::swapByteOrder(pl
.count
);
3993 inline void swapStruct(struct protocol_list32_t
&pl
) {
3994 sys::swapByteOrder(pl
.count
);
3997 inline void swapStruct(struct protocol64_t
&p
) {
3998 sys::swapByteOrder(p
.isa
);
3999 sys::swapByteOrder(p
.name
);
4000 sys::swapByteOrder(p
.protocols
);
4001 sys::swapByteOrder(p
.instanceMethods
);
4002 sys::swapByteOrder(p
.classMethods
);
4003 sys::swapByteOrder(p
.optionalInstanceMethods
);
4004 sys::swapByteOrder(p
.optionalClassMethods
);
4005 sys::swapByteOrder(p
.instanceProperties
);
4008 inline void swapStruct(struct protocol32_t
&p
) {
4009 sys::swapByteOrder(p
.isa
);
4010 sys::swapByteOrder(p
.name
);
4011 sys::swapByteOrder(p
.protocols
);
4012 sys::swapByteOrder(p
.instanceMethods
);
4013 sys::swapByteOrder(p
.classMethods
);
4014 sys::swapByteOrder(p
.optionalInstanceMethods
);
4015 sys::swapByteOrder(p
.optionalClassMethods
);
4016 sys::swapByteOrder(p
.instanceProperties
);
4019 inline void swapStruct(struct ivar_list64_t
&il
) {
4020 sys::swapByteOrder(il
.entsize
);
4021 sys::swapByteOrder(il
.count
);
4024 inline void swapStruct(struct ivar_list32_t
&il
) {
4025 sys::swapByteOrder(il
.entsize
);
4026 sys::swapByteOrder(il
.count
);
4029 inline void swapStruct(struct ivar64_t
&i
) {
4030 sys::swapByteOrder(i
.offset
);
4031 sys::swapByteOrder(i
.name
);
4032 sys::swapByteOrder(i
.type
);
4033 sys::swapByteOrder(i
.alignment
);
4034 sys::swapByteOrder(i
.size
);
4037 inline void swapStruct(struct ivar32_t
&i
) {
4038 sys::swapByteOrder(i
.offset
);
4039 sys::swapByteOrder(i
.name
);
4040 sys::swapByteOrder(i
.type
);
4041 sys::swapByteOrder(i
.alignment
);
4042 sys::swapByteOrder(i
.size
);
4045 inline void swapStruct(struct objc_property_list64
&pl
) {
4046 sys::swapByteOrder(pl
.entsize
);
4047 sys::swapByteOrder(pl
.count
);
4050 inline void swapStruct(struct objc_property_list32
&pl
) {
4051 sys::swapByteOrder(pl
.entsize
);
4052 sys::swapByteOrder(pl
.count
);
4055 inline void swapStruct(struct objc_property64
&op
) {
4056 sys::swapByteOrder(op
.name
);
4057 sys::swapByteOrder(op
.attributes
);
4060 inline void swapStruct(struct objc_property32
&op
) {
4061 sys::swapByteOrder(op
.name
);
4062 sys::swapByteOrder(op
.attributes
);
4065 inline void swapStruct(struct category64_t
&c
) {
4066 sys::swapByteOrder(c
.name
);
4067 sys::swapByteOrder(c
.cls
);
4068 sys::swapByteOrder(c
.instanceMethods
);
4069 sys::swapByteOrder(c
.classMethods
);
4070 sys::swapByteOrder(c
.protocols
);
4071 sys::swapByteOrder(c
.instanceProperties
);
4074 inline void swapStruct(struct category32_t
&c
) {
4075 sys::swapByteOrder(c
.name
);
4076 sys::swapByteOrder(c
.cls
);
4077 sys::swapByteOrder(c
.instanceMethods
);
4078 sys::swapByteOrder(c
.classMethods
);
4079 sys::swapByteOrder(c
.protocols
);
4080 sys::swapByteOrder(c
.instanceProperties
);
4083 inline void swapStruct(struct objc_image_info64
&o
) {
4084 sys::swapByteOrder(o
.version
);
4085 sys::swapByteOrder(o
.flags
);
4088 inline void swapStruct(struct objc_image_info32
&o
) {
4089 sys::swapByteOrder(o
.version
);
4090 sys::swapByteOrder(o
.flags
);
4093 inline void swapStruct(struct imageInfo_t
&o
) {
4094 sys::swapByteOrder(o
.version
);
4095 sys::swapByteOrder(o
.flags
);
4098 inline void swapStruct(struct message_ref64
&mr
) {
4099 sys::swapByteOrder(mr
.imp
);
4100 sys::swapByteOrder(mr
.sel
);
4103 inline void swapStruct(struct message_ref32
&mr
) {
4104 sys::swapByteOrder(mr
.imp
);
4105 sys::swapByteOrder(mr
.sel
);
4108 inline void swapStruct(struct objc_module_t
&module
) {
4109 sys::swapByteOrder(module
.version
);
4110 sys::swapByteOrder(module
.size
);
4111 sys::swapByteOrder(module
.name
);
4112 sys::swapByteOrder(module
.symtab
);
4115 inline void swapStruct(struct objc_symtab_t
&symtab
) {
4116 sys::swapByteOrder(symtab
.sel_ref_cnt
);
4117 sys::swapByteOrder(symtab
.refs
);
4118 sys::swapByteOrder(symtab
.cls_def_cnt
);
4119 sys::swapByteOrder(symtab
.cat_def_cnt
);
4122 inline void swapStruct(struct objc_class_t
&objc_class
) {
4123 sys::swapByteOrder(objc_class
.isa
);
4124 sys::swapByteOrder(objc_class
.super_class
);
4125 sys::swapByteOrder(objc_class
.name
);
4126 sys::swapByteOrder(objc_class
.version
);
4127 sys::swapByteOrder(objc_class
.info
);
4128 sys::swapByteOrder(objc_class
.instance_size
);
4129 sys::swapByteOrder(objc_class
.ivars
);
4130 sys::swapByteOrder(objc_class
.methodLists
);
4131 sys::swapByteOrder(objc_class
.cache
);
4132 sys::swapByteOrder(objc_class
.protocols
);
4135 inline void swapStruct(struct objc_category_t
&objc_category
) {
4136 sys::swapByteOrder(objc_category
.category_name
);
4137 sys::swapByteOrder(objc_category
.class_name
);
4138 sys::swapByteOrder(objc_category
.instance_methods
);
4139 sys::swapByteOrder(objc_category
.class_methods
);
4140 sys::swapByteOrder(objc_category
.protocols
);
4143 inline void swapStruct(struct objc_ivar_list_t
&objc_ivar_list
) {
4144 sys::swapByteOrder(objc_ivar_list
.ivar_count
);
4147 inline void swapStruct(struct objc_ivar_t
&objc_ivar
) {
4148 sys::swapByteOrder(objc_ivar
.ivar_name
);
4149 sys::swapByteOrder(objc_ivar
.ivar_type
);
4150 sys::swapByteOrder(objc_ivar
.ivar_offset
);
4153 inline void swapStruct(struct objc_method_list_t
&method_list
) {
4154 sys::swapByteOrder(method_list
.obsolete
);
4155 sys::swapByteOrder(method_list
.method_count
);
4158 inline void swapStruct(struct objc_method_t
&method
) {
4159 sys::swapByteOrder(method
.method_name
);
4160 sys::swapByteOrder(method
.method_types
);
4161 sys::swapByteOrder(method
.method_imp
);
4164 inline void swapStruct(struct objc_protocol_list_t
&protocol_list
) {
4165 sys::swapByteOrder(protocol_list
.next
);
4166 sys::swapByteOrder(protocol_list
.count
);
4169 inline void swapStruct(struct objc_protocol_t
&protocol
) {
4170 sys::swapByteOrder(protocol
.isa
);
4171 sys::swapByteOrder(protocol
.protocol_name
);
4172 sys::swapByteOrder(protocol
.protocol_list
);
4173 sys::swapByteOrder(protocol
.instance_methods
);
4174 sys::swapByteOrder(protocol
.class_methods
);
4177 inline void swapStruct(struct objc_method_description_list_t
&mdl
) {
4178 sys::swapByteOrder(mdl
.count
);
4181 inline void swapStruct(struct objc_method_description_t
&md
) {
4182 sys::swapByteOrder(md
.name
);
4183 sys::swapByteOrder(md
.types
);
4188 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue
,
4189 struct DisassembleInfo
*info
);
4191 // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
4192 // to an Objective-C class and returns the class name. It is also passed the
4193 // address of the pointer, so when the pointer is zero as it can be in an .o
4194 // file, that is used to look for an external relocation entry with a symbol
4196 static const char *get_objc2_64bit_class_name(uint64_t pointer_value
,
4197 uint64_t ReferenceValue
,
4198 struct DisassembleInfo
*info
) {
4200 uint32_t offset
, left
;
4203 // The pointer_value can be 0 in an object file and have a relocation
4204 // entry for the class symbol at the ReferenceValue (the address of the
4206 if (pointer_value
== 0) {
4207 r
= get_pointer_64(ReferenceValue
, offset
, left
, S
, info
);
4208 if (r
== nullptr || left
< sizeof(uint64_t))
4211 const char *symbol_name
= get_symbol_64(offset
, S
, info
, n_value
);
4212 if (symbol_name
== nullptr)
4214 const char *class_name
= strrchr(symbol_name
, '$');
4215 if (class_name
!= nullptr && class_name
[1] == '_' && class_name
[2] != '\0')
4216 return class_name
+ 2;
4221 // The case were the pointer_value is non-zero and points to a class defined
4222 // in this Mach-O file.
4223 r
= get_pointer_64(pointer_value
, offset
, left
, S
, info
);
4224 if (r
== nullptr || left
< sizeof(struct class64_t
))
4227 memcpy(&c
, r
, sizeof(struct class64_t
));
4228 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4232 r
= get_pointer_64(c
.data
, offset
, left
, S
, info
);
4233 if (r
== nullptr || left
< sizeof(struct class_ro64_t
))
4235 struct class_ro64_t cro
;
4236 memcpy(&cro
, r
, sizeof(struct class_ro64_t
));
4237 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4241 const char *name
= get_pointer_64(cro
.name
, offset
, left
, S
, info
);
4245 // get_objc2_64bit_cfstring_name is used for disassembly and is passed a
4246 // pointer to a cfstring and returns its name or nullptr.
4247 static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue
,
4248 struct DisassembleInfo
*info
) {
4249 const char *r
, *name
;
4250 uint32_t offset
, left
;
4252 struct cfstring64_t cfs
;
4253 uint64_t cfs_characters
;
4255 r
= get_pointer_64(ReferenceValue
, offset
, left
, S
, info
);
4256 if (r
== nullptr || left
< sizeof(struct cfstring64_t
))
4258 memcpy(&cfs
, r
, sizeof(struct cfstring64_t
));
4259 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4261 if (cfs
.characters
== 0) {
4263 const char *symbol_name
= get_symbol_64(
4264 offset
+ offsetof(struct cfstring64_t
, characters
), S
, info
, n_value
);
4265 if (symbol_name
== nullptr)
4267 cfs_characters
= n_value
;
4269 cfs_characters
= cfs
.characters
;
4270 name
= get_pointer_64(cfs_characters
, offset
, left
, S
, info
);
4275 // get_objc2_64bit_selref() is used for disassembly and is passed a the address
4276 // of a pointer to an Objective-C selector reference when the pointer value is
4277 // zero as in a .o file and is likely to have a external relocation entry with
4278 // who's symbol's n_value is the real pointer to the selector name. If that is
4279 // the case the real pointer to the selector name is returned else 0 is
4281 static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue
,
4282 struct DisassembleInfo
*info
) {
4283 uint32_t offset
, left
;
4286 const char *r
= get_pointer_64(ReferenceValue
, offset
, left
, S
, info
);
4287 if (r
== nullptr || left
< sizeof(uint64_t))
4290 const char *symbol_name
= get_symbol_64(offset
, S
, info
, n_value
);
4291 if (symbol_name
== nullptr)
4296 static const SectionRef
get_section(MachOObjectFile
*O
, const char *segname
,
4297 const char *sectname
) {
4298 for (const SectionRef
&Section
: O
->sections()) {
4300 Expected
<StringRef
> SecNameOrErr
= Section
.getName();
4302 SectName
= *SecNameOrErr
;
4304 consumeError(SecNameOrErr
.takeError());
4306 DataRefImpl Ref
= Section
.getRawDataRefImpl();
4307 StringRef SegName
= O
->getSectionFinalSegmentName(Ref
);
4308 if (SegName
== segname
&& SectName
== sectname
)
4311 return SectionRef();
4315 walk_pointer_list_64(const char *listname
, const SectionRef S
,
4316 MachOObjectFile
*O
, struct DisassembleInfo
*info
,
4317 void (*func
)(uint64_t, struct DisassembleInfo
*info
)) {
4318 if (S
== SectionRef())
4322 Expected
<StringRef
> SecNameOrErr
= S
.getName();
4324 SectName
= *SecNameOrErr
;
4326 consumeError(SecNameOrErr
.takeError());
4328 DataRefImpl Ref
= S
.getRawDataRefImpl();
4329 StringRef SegName
= O
->getSectionFinalSegmentName(Ref
);
4330 outs() << "Contents of (" << SegName
<< "," << SectName
<< ") section\n";
4332 StringRef BytesStr
= unwrapOrError(S
.getContents(), O
->getFileName());
4333 const char *Contents
= reinterpret_cast<const char *>(BytesStr
.data());
4335 for (uint32_t i
= 0; i
< S
.getSize(); i
+= sizeof(uint64_t)) {
4336 uint32_t left
= S
.getSize() - i
;
4337 uint32_t size
= left
< sizeof(uint64_t) ? left
: sizeof(uint64_t);
4339 memcpy(&p
, Contents
+ i
, size
);
4340 if (i
+ sizeof(uint64_t) > S
.getSize())
4341 outs() << listname
<< " list pointer extends past end of (" << SegName
4342 << "," << SectName
<< ") section\n";
4343 outs() << format("%016" PRIx64
, S
.getAddress() + i
) << " ";
4345 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
4346 sys::swapByteOrder(p
);
4348 uint64_t n_value
= 0;
4349 const char *name
= get_symbol_64(i
, S
, info
, n_value
, p
);
4350 if (name
== nullptr)
4351 name
= get_dyld_bind_info_symbolname(S
.getAddress() + i
, info
);
4354 outs() << format("0x%" PRIx64
, n_value
);
4356 outs() << " + " << format("0x%" PRIx64
, p
);
4358 outs() << format("0x%" PRIx64
, p
);
4359 if (name
!= nullptr)
4360 outs() << " " << name
;
4370 walk_pointer_list_32(const char *listname
, const SectionRef S
,
4371 MachOObjectFile
*O
, struct DisassembleInfo
*info
,
4372 void (*func
)(uint32_t, struct DisassembleInfo
*info
)) {
4373 if (S
== SectionRef())
4376 StringRef SectName
= unwrapOrError(S
.getName(), O
->getFileName());
4377 DataRefImpl Ref
= S
.getRawDataRefImpl();
4378 StringRef SegName
= O
->getSectionFinalSegmentName(Ref
);
4379 outs() << "Contents of (" << SegName
<< "," << SectName
<< ") section\n";
4381 StringRef BytesStr
= unwrapOrError(S
.getContents(), O
->getFileName());
4382 const char *Contents
= reinterpret_cast<const char *>(BytesStr
.data());
4384 for (uint32_t i
= 0; i
< S
.getSize(); i
+= sizeof(uint32_t)) {
4385 uint32_t left
= S
.getSize() - i
;
4386 uint32_t size
= left
< sizeof(uint32_t) ? left
: sizeof(uint32_t);
4388 memcpy(&p
, Contents
+ i
, size
);
4389 if (i
+ sizeof(uint32_t) > S
.getSize())
4390 outs() << listname
<< " list pointer extends past end of (" << SegName
4391 << "," << SectName
<< ") section\n";
4392 uint32_t Address
= S
.getAddress() + i
;
4393 outs() << format("%08" PRIx32
, Address
) << " ";
4395 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
4396 sys::swapByteOrder(p
);
4397 outs() << format("0x%" PRIx32
, p
);
4399 const char *name
= get_symbol_32(i
, S
, info
, p
);
4400 if (name
!= nullptr)
4401 outs() << " " << name
;
4409 static void print_layout_map(const char *layout_map
, uint32_t left
) {
4410 if (layout_map
== nullptr)
4412 outs() << " layout map: ";
4414 outs() << format("0x%02" PRIx32
, (*layout_map
) & 0xff) << " ";
4417 } while (*layout_map
!= '\0' && left
!= 0);
4421 static void print_layout_map64(uint64_t p
, struct DisassembleInfo
*info
) {
4422 uint32_t offset
, left
;
4424 const char *layout_map
;
4428 layout_map
= get_pointer_64(p
, offset
, left
, S
, info
);
4429 print_layout_map(layout_map
, left
);
4432 static void print_layout_map32(uint32_t p
, struct DisassembleInfo
*info
) {
4433 uint32_t offset
, left
;
4435 const char *layout_map
;
4439 layout_map
= get_pointer_32(p
, offset
, left
, S
, info
);
4440 print_layout_map(layout_map
, left
);
4443 static void print_method_list64_t(uint64_t p
, struct DisassembleInfo
*info
,
4444 const char *indent
) {
4445 struct method_list64_t ml
;
4446 struct method64_t m
;
4448 uint32_t offset
, xoffset
, left
, i
;
4450 const char *name
, *sym_name
;
4453 r
= get_pointer_64(p
, offset
, left
, S
, info
);
4456 memset(&ml
, '\0', sizeof(struct method_list64_t
));
4457 if (left
< sizeof(struct method_list64_t
)) {
4458 memcpy(&ml
, r
, left
);
4459 outs() << " (method_list_t entends past the end of the section)\n";
4461 memcpy(&ml
, r
, sizeof(struct method_list64_t
));
4462 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4464 outs() << indent
<< "\t\t entsize " << ml
.entsize
<< "\n";
4465 outs() << indent
<< "\t\t count " << ml
.count
<< "\n";
4467 p
+= sizeof(struct method_list64_t
);
4468 offset
+= sizeof(struct method_list64_t
);
4469 for (i
= 0; i
< ml
.count
; i
++) {
4470 r
= get_pointer_64(p
, offset
, left
, S
, info
);
4473 memset(&m
, '\0', sizeof(struct method64_t
));
4474 if (left
< sizeof(struct method64_t
)) {
4475 memcpy(&m
, r
, left
);
4476 outs() << indent
<< " (method_t extends past the end of the section)\n";
4478 memcpy(&m
, r
, sizeof(struct method64_t
));
4479 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4482 outs() << indent
<< "\t\t name ";
4483 sym_name
= get_symbol_64(offset
+ offsetof(struct method64_t
, name
), S
,
4484 info
, n_value
, m
.name
);
4486 if (info
->verbose
&& sym_name
!= nullptr)
4489 outs() << format("0x%" PRIx64
, n_value
);
4491 outs() << " + " << format("0x%" PRIx64
, m
.name
);
4493 outs() << format("0x%" PRIx64
, m
.name
);
4494 name
= get_pointer_64(m
.name
+ n_value
, xoffset
, left
, xS
, info
);
4495 if (name
!= nullptr)
4496 outs() << format(" %.*s", left
, name
);
4499 outs() << indent
<< "\t\t types ";
4500 sym_name
= get_symbol_64(offset
+ offsetof(struct method64_t
, types
), S
,
4501 info
, n_value
, m
.types
);
4503 if (info
->verbose
&& sym_name
!= nullptr)
4506 outs() << format("0x%" PRIx64
, n_value
);
4508 outs() << " + " << format("0x%" PRIx64
, m
.types
);
4510 outs() << format("0x%" PRIx64
, m
.types
);
4511 name
= get_pointer_64(m
.types
+ n_value
, xoffset
, left
, xS
, info
);
4512 if (name
!= nullptr)
4513 outs() << format(" %.*s", left
, name
);
4516 outs() << indent
<< "\t\t imp ";
4517 name
= get_symbol_64(offset
+ offsetof(struct method64_t
, imp
), S
, info
,
4519 if (info
->verbose
&& name
== nullptr) {
4521 outs() << format("0x%" PRIx64
, n_value
) << " ";
4523 outs() << "+ " << format("0x%" PRIx64
, m
.imp
) << " ";
4525 outs() << format("0x%" PRIx64
, m
.imp
) << " ";
4527 if (name
!= nullptr)
4531 p
+= sizeof(struct method64_t
);
4532 offset
+= sizeof(struct method64_t
);
4536 static void print_method_list32_t(uint64_t p
, struct DisassembleInfo
*info
,
4537 const char *indent
) {
4538 struct method_list32_t ml
;
4539 struct method32_t m
;
4540 const char *r
, *name
;
4541 uint32_t offset
, xoffset
, left
, i
;
4544 r
= get_pointer_32(p
, offset
, left
, S
, info
);
4547 memset(&ml
, '\0', sizeof(struct method_list32_t
));
4548 if (left
< sizeof(struct method_list32_t
)) {
4549 memcpy(&ml
, r
, left
);
4550 outs() << " (method_list_t entends past the end of the section)\n";
4552 memcpy(&ml
, r
, sizeof(struct method_list32_t
));
4553 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4555 outs() << indent
<< "\t\t entsize " << ml
.entsize
<< "\n";
4556 outs() << indent
<< "\t\t count " << ml
.count
<< "\n";
4558 p
+= sizeof(struct method_list32_t
);
4559 offset
+= sizeof(struct method_list32_t
);
4560 for (i
= 0; i
< ml
.count
; i
++) {
4561 r
= get_pointer_32(p
, offset
, left
, S
, info
);
4564 memset(&m
, '\0', sizeof(struct method32_t
));
4565 if (left
< sizeof(struct method32_t
)) {
4566 memcpy(&ml
, r
, left
);
4567 outs() << indent
<< " (method_t entends past the end of the section)\n";
4569 memcpy(&m
, r
, sizeof(struct method32_t
));
4570 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4573 outs() << indent
<< "\t\t name " << format("0x%" PRIx32
, m
.name
);
4574 name
= get_pointer_32(m
.name
, xoffset
, left
, xS
, info
);
4575 if (name
!= nullptr)
4576 outs() << format(" %.*s", left
, name
);
4579 outs() << indent
<< "\t\t types " << format("0x%" PRIx32
, m
.types
);
4580 name
= get_pointer_32(m
.types
, xoffset
, left
, xS
, info
);
4581 if (name
!= nullptr)
4582 outs() << format(" %.*s", left
, name
);
4585 outs() << indent
<< "\t\t imp " << format("0x%" PRIx32
, m
.imp
);
4586 name
= get_symbol_32(offset
+ offsetof(struct method32_t
, imp
), S
, info
,
4588 if (name
!= nullptr)
4589 outs() << " " << name
;
4592 p
+= sizeof(struct method32_t
);
4593 offset
+= sizeof(struct method32_t
);
4597 static bool print_method_list(uint32_t p
, struct DisassembleInfo
*info
) {
4598 uint32_t offset
, left
, xleft
;
4600 struct objc_method_list_t method_list
;
4601 struct objc_method_t method
;
4602 const char *r
, *methods
, *name
, *SymbolName
;
4605 r
= get_pointer_32(p
, offset
, left
, S
, info
, true);
4610 if (left
> sizeof(struct objc_method_list_t
)) {
4611 memcpy(&method_list
, r
, sizeof(struct objc_method_list_t
));
4613 outs() << "\t\t objc_method_list extends past end of the section\n";
4614 memset(&method_list
, '\0', sizeof(struct objc_method_list_t
));
4615 memcpy(&method_list
, r
, left
);
4617 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4618 swapStruct(method_list
);
4620 outs() << "\t\t obsolete "
4621 << format("0x%08" PRIx32
, method_list
.obsolete
) << "\n";
4622 outs() << "\t\t method_count " << method_list
.method_count
<< "\n";
4624 methods
= r
+ sizeof(struct objc_method_list_t
);
4625 for (i
= 0; i
< method_list
.method_count
; i
++) {
4626 if ((i
+ 1) * sizeof(struct objc_method_t
) > left
) {
4627 outs() << "\t\t remaining method's extend past the of the section\n";
4630 memcpy(&method
, methods
+ i
* sizeof(struct objc_method_t
),
4631 sizeof(struct objc_method_t
));
4632 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4635 outs() << "\t\t method_name "
4636 << format("0x%08" PRIx32
, method
.method_name
);
4637 if (info
->verbose
) {
4638 name
= get_pointer_32(method
.method_name
, offset
, xleft
, S
, info
, true);
4639 if (name
!= nullptr)
4640 outs() << format(" %.*s", xleft
, name
);
4642 outs() << " (not in an __OBJC section)";
4646 outs() << "\t\t method_types "
4647 << format("0x%08" PRIx32
, method
.method_types
);
4648 if (info
->verbose
) {
4649 name
= get_pointer_32(method
.method_types
, offset
, xleft
, S
, info
, true);
4650 if (name
!= nullptr)
4651 outs() << format(" %.*s", xleft
, name
);
4653 outs() << " (not in an __OBJC section)";
4657 outs() << "\t\t method_imp "
4658 << format("0x%08" PRIx32
, method
.method_imp
) << " ";
4659 if (info
->verbose
) {
4660 SymbolName
= GuessSymbolName(method
.method_imp
, info
->AddrMap
);
4661 if (SymbolName
!= nullptr)
4662 outs() << SymbolName
;
4669 static void print_protocol_list64_t(uint64_t p
, struct DisassembleInfo
*info
) {
4670 struct protocol_list64_t pl
;
4671 uint64_t q
, n_value
;
4672 struct protocol64_t pc
;
4674 uint32_t offset
, xoffset
, left
, i
;
4676 const char *name
, *sym_name
;
4678 r
= get_pointer_64(p
, offset
, left
, S
, info
);
4681 memset(&pl
, '\0', sizeof(struct protocol_list64_t
));
4682 if (left
< sizeof(struct protocol_list64_t
)) {
4683 memcpy(&pl
, r
, left
);
4684 outs() << " (protocol_list_t entends past the end of the section)\n";
4686 memcpy(&pl
, r
, sizeof(struct protocol_list64_t
));
4687 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4689 outs() << " count " << pl
.count
<< "\n";
4691 p
+= sizeof(struct protocol_list64_t
);
4692 offset
+= sizeof(struct protocol_list64_t
);
4693 for (i
= 0; i
< pl
.count
; i
++) {
4694 r
= get_pointer_64(p
, offset
, left
, S
, info
);
4698 if (left
< sizeof(uint64_t)) {
4699 memcpy(&q
, r
, left
);
4700 outs() << " (protocol_t * entends past the end of the section)\n";
4702 memcpy(&q
, r
, sizeof(uint64_t));
4703 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4704 sys::swapByteOrder(q
);
4706 outs() << "\t\t list[" << i
<< "] ";
4707 sym_name
= get_symbol_64(offset
, S
, info
, n_value
, q
);
4709 if (info
->verbose
&& sym_name
!= nullptr)
4712 outs() << format("0x%" PRIx64
, n_value
);
4714 outs() << " + " << format("0x%" PRIx64
, q
);
4716 outs() << format("0x%" PRIx64
, q
);
4717 outs() << " (struct protocol_t *)\n";
4719 r
= get_pointer_64(q
+ n_value
, offset
, left
, S
, info
);
4722 memset(&pc
, '\0', sizeof(struct protocol64_t
));
4723 if (left
< sizeof(struct protocol64_t
)) {
4724 memcpy(&pc
, r
, left
);
4725 outs() << " (protocol_t entends past the end of the section)\n";
4727 memcpy(&pc
, r
, sizeof(struct protocol64_t
));
4728 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4731 outs() << "\t\t\t isa " << format("0x%" PRIx64
, pc
.isa
) << "\n";
4733 outs() << "\t\t\t name ";
4734 sym_name
= get_symbol_64(offset
+ offsetof(struct protocol64_t
, name
), S
,
4735 info
, n_value
, pc
.name
);
4737 if (info
->verbose
&& sym_name
!= nullptr)
4740 outs() << format("0x%" PRIx64
, n_value
);
4742 outs() << " + " << format("0x%" PRIx64
, pc
.name
);
4744 outs() << format("0x%" PRIx64
, pc
.name
);
4745 name
= get_pointer_64(pc
.name
+ n_value
, xoffset
, left
, xS
, info
);
4746 if (name
!= nullptr)
4747 outs() << format(" %.*s", left
, name
);
4750 outs() << "\t\t\tprotocols " << format("0x%" PRIx64
, pc
.protocols
) << "\n";
4752 outs() << "\t\t instanceMethods ";
4754 get_symbol_64(offset
+ offsetof(struct protocol64_t
, instanceMethods
),
4755 S
, info
, n_value
, pc
.instanceMethods
);
4757 if (info
->verbose
&& sym_name
!= nullptr)
4760 outs() << format("0x%" PRIx64
, n_value
);
4761 if (pc
.instanceMethods
!= 0)
4762 outs() << " + " << format("0x%" PRIx64
, pc
.instanceMethods
);
4764 outs() << format("0x%" PRIx64
, pc
.instanceMethods
);
4765 outs() << " (struct method_list_t *)\n";
4766 if (pc
.instanceMethods
+ n_value
!= 0)
4767 print_method_list64_t(pc
.instanceMethods
+ n_value
, info
, "\t");
4769 outs() << "\t\t classMethods ";
4771 get_symbol_64(offset
+ offsetof(struct protocol64_t
, classMethods
), S
,
4772 info
, n_value
, pc
.classMethods
);
4774 if (info
->verbose
&& sym_name
!= nullptr)
4777 outs() << format("0x%" PRIx64
, n_value
);
4778 if (pc
.classMethods
!= 0)
4779 outs() << " + " << format("0x%" PRIx64
, pc
.classMethods
);
4781 outs() << format("0x%" PRIx64
, pc
.classMethods
);
4782 outs() << " (struct method_list_t *)\n";
4783 if (pc
.classMethods
+ n_value
!= 0)
4784 print_method_list64_t(pc
.classMethods
+ n_value
, info
, "\t");
4786 outs() << "\t optionalInstanceMethods "
4787 << format("0x%" PRIx64
, pc
.optionalInstanceMethods
) << "\n";
4788 outs() << "\t optionalClassMethods "
4789 << format("0x%" PRIx64
, pc
.optionalClassMethods
) << "\n";
4790 outs() << "\t instanceProperties "
4791 << format("0x%" PRIx64
, pc
.instanceProperties
) << "\n";
4793 p
+= sizeof(uint64_t);
4794 offset
+= sizeof(uint64_t);
4798 static void print_protocol_list32_t(uint32_t p
, struct DisassembleInfo
*info
) {
4799 struct protocol_list32_t pl
;
4801 struct protocol32_t pc
;
4803 uint32_t offset
, xoffset
, left
, i
;
4807 r
= get_pointer_32(p
, offset
, left
, S
, info
);
4810 memset(&pl
, '\0', sizeof(struct protocol_list32_t
));
4811 if (left
< sizeof(struct protocol_list32_t
)) {
4812 memcpy(&pl
, r
, left
);
4813 outs() << " (protocol_list_t entends past the end of the section)\n";
4815 memcpy(&pl
, r
, sizeof(struct protocol_list32_t
));
4816 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4818 outs() << " count " << pl
.count
<< "\n";
4820 p
+= sizeof(struct protocol_list32_t
);
4821 offset
+= sizeof(struct protocol_list32_t
);
4822 for (i
= 0; i
< pl
.count
; i
++) {
4823 r
= get_pointer_32(p
, offset
, left
, S
, info
);
4827 if (left
< sizeof(uint32_t)) {
4828 memcpy(&q
, r
, left
);
4829 outs() << " (protocol_t * entends past the end of the section)\n";
4831 memcpy(&q
, r
, sizeof(uint32_t));
4832 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4833 sys::swapByteOrder(q
);
4834 outs() << "\t\t list[" << i
<< "] " << format("0x%" PRIx32
, q
)
4835 << " (struct protocol_t *)\n";
4836 r
= get_pointer_32(q
, offset
, left
, S
, info
);
4839 memset(&pc
, '\0', sizeof(struct protocol32_t
));
4840 if (left
< sizeof(struct protocol32_t
)) {
4841 memcpy(&pc
, r
, left
);
4842 outs() << " (protocol_t entends past the end of the section)\n";
4844 memcpy(&pc
, r
, sizeof(struct protocol32_t
));
4845 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4847 outs() << "\t\t\t isa " << format("0x%" PRIx32
, pc
.isa
) << "\n";
4848 outs() << "\t\t\t name " << format("0x%" PRIx32
, pc
.name
);
4849 name
= get_pointer_32(pc
.name
, xoffset
, left
, xS
, info
);
4850 if (name
!= nullptr)
4851 outs() << format(" %.*s", left
, name
);
4853 outs() << "\t\t\tprotocols " << format("0x%" PRIx32
, pc
.protocols
) << "\n";
4854 outs() << "\t\t instanceMethods "
4855 << format("0x%" PRIx32
, pc
.instanceMethods
)
4856 << " (struct method_list_t *)\n";
4857 if (pc
.instanceMethods
!= 0)
4858 print_method_list32_t(pc
.instanceMethods
, info
, "\t");
4859 outs() << "\t\t classMethods " << format("0x%" PRIx32
, pc
.classMethods
)
4860 << " (struct method_list_t *)\n";
4861 if (pc
.classMethods
!= 0)
4862 print_method_list32_t(pc
.classMethods
, info
, "\t");
4863 outs() << "\t optionalInstanceMethods "
4864 << format("0x%" PRIx32
, pc
.optionalInstanceMethods
) << "\n";
4865 outs() << "\t optionalClassMethods "
4866 << format("0x%" PRIx32
, pc
.optionalClassMethods
) << "\n";
4867 outs() << "\t instanceProperties "
4868 << format("0x%" PRIx32
, pc
.instanceProperties
) << "\n";
4869 p
+= sizeof(uint32_t);
4870 offset
+= sizeof(uint32_t);
4874 static void print_indent(uint32_t indent
) {
4875 for (uint32_t i
= 0; i
< indent
;) {
4876 if (indent
- i
>= 8) {
4880 for (uint32_t j
= i
; j
< indent
; j
++)
4887 static bool print_method_description_list(uint32_t p
, uint32_t indent
,
4888 struct DisassembleInfo
*info
) {
4889 uint32_t offset
, left
, xleft
;
4891 struct objc_method_description_list_t mdl
;
4892 struct objc_method_description_t md
;
4893 const char *r
, *list
, *name
;
4896 r
= get_pointer_32(p
, offset
, left
, S
, info
, true);
4901 if (left
> sizeof(struct objc_method_description_list_t
)) {
4902 memcpy(&mdl
, r
, sizeof(struct objc_method_description_list_t
));
4904 print_indent(indent
);
4905 outs() << " objc_method_description_list extends past end of the section\n";
4906 memset(&mdl
, '\0', sizeof(struct objc_method_description_list_t
));
4907 memcpy(&mdl
, r
, left
);
4909 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4912 print_indent(indent
);
4913 outs() << " count " << mdl
.count
<< "\n";
4915 list
= r
+ sizeof(struct objc_method_description_list_t
);
4916 for (i
= 0; i
< mdl
.count
; i
++) {
4917 if ((i
+ 1) * sizeof(struct objc_method_description_t
) > left
) {
4918 print_indent(indent
);
4919 outs() << " remaining list entries extend past the of the section\n";
4922 print_indent(indent
);
4923 outs() << " list[" << i
<< "]\n";
4924 memcpy(&md
, list
+ i
* sizeof(struct objc_method_description_t
),
4925 sizeof(struct objc_method_description_t
));
4926 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4929 print_indent(indent
);
4930 outs() << " name " << format("0x%08" PRIx32
, md
.name
);
4931 if (info
->verbose
) {
4932 name
= get_pointer_32(md
.name
, offset
, xleft
, S
, info
, true);
4933 if (name
!= nullptr)
4934 outs() << format(" %.*s", xleft
, name
);
4936 outs() << " (not in an __OBJC section)";
4940 print_indent(indent
);
4941 outs() << " types " << format("0x%08" PRIx32
, md
.types
);
4942 if (info
->verbose
) {
4943 name
= get_pointer_32(md
.types
, offset
, xleft
, S
, info
, true);
4944 if (name
!= nullptr)
4945 outs() << format(" %.*s", xleft
, name
);
4947 outs() << " (not in an __OBJC section)";
4954 static bool print_protocol_list(uint32_t p
, uint32_t indent
,
4955 struct DisassembleInfo
*info
);
4957 static bool print_protocol(uint32_t p
, uint32_t indent
,
4958 struct DisassembleInfo
*info
) {
4959 uint32_t offset
, left
;
4961 struct objc_protocol_t protocol
;
4962 const char *r
, *name
;
4964 r
= get_pointer_32(p
, offset
, left
, S
, info
, true);
4969 if (left
>= sizeof(struct objc_protocol_t
)) {
4970 memcpy(&protocol
, r
, sizeof(struct objc_protocol_t
));
4972 print_indent(indent
);
4973 outs() << " Protocol extends past end of the section\n";
4974 memset(&protocol
, '\0', sizeof(struct objc_protocol_t
));
4975 memcpy(&protocol
, r
, left
);
4977 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
4978 swapStruct(protocol
);
4980 print_indent(indent
);
4981 outs() << " isa " << format("0x%08" PRIx32
, protocol
.isa
)
4984 print_indent(indent
);
4985 outs() << " protocol_name "
4986 << format("0x%08" PRIx32
, protocol
.protocol_name
);
4987 if (info
->verbose
) {
4988 name
= get_pointer_32(protocol
.protocol_name
, offset
, left
, S
, info
, true);
4989 if (name
!= nullptr)
4990 outs() << format(" %.*s", left
, name
);
4992 outs() << " (not in an __OBJC section)";
4996 print_indent(indent
);
4997 outs() << " protocol_list "
4998 << format("0x%08" PRIx32
, protocol
.protocol_list
);
4999 if (print_protocol_list(protocol
.protocol_list
, indent
+ 4, info
))
5000 outs() << " (not in an __OBJC section)\n";
5002 print_indent(indent
);
5003 outs() << " instance_methods "
5004 << format("0x%08" PRIx32
, protocol
.instance_methods
);
5005 if (print_method_description_list(protocol
.instance_methods
, indent
, info
))
5006 outs() << " (not in an __OBJC section)\n";
5008 print_indent(indent
);
5009 outs() << " class_methods "
5010 << format("0x%08" PRIx32
, protocol
.class_methods
);
5011 if (print_method_description_list(protocol
.class_methods
, indent
, info
))
5012 outs() << " (not in an __OBJC section)\n";
5017 static bool print_protocol_list(uint32_t p
, uint32_t indent
,
5018 struct DisassembleInfo
*info
) {
5019 uint32_t offset
, left
, l
;
5021 struct objc_protocol_list_t protocol_list
;
5022 const char *r
, *list
;
5025 r
= get_pointer_32(p
, offset
, left
, S
, info
, true);
5030 if (left
> sizeof(struct objc_protocol_list_t
)) {
5031 memcpy(&protocol_list
, r
, sizeof(struct objc_protocol_list_t
));
5033 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
5034 memset(&protocol_list
, '\0', sizeof(struct objc_protocol_list_t
));
5035 memcpy(&protocol_list
, r
, left
);
5037 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5038 swapStruct(protocol_list
);
5040 print_indent(indent
);
5041 outs() << " next " << format("0x%08" PRIx32
, protocol_list
.next
)
5043 print_indent(indent
);
5044 outs() << " count " << protocol_list
.count
<< "\n";
5046 list
= r
+ sizeof(struct objc_protocol_list_t
);
5047 for (i
= 0; i
< protocol_list
.count
; i
++) {
5048 if ((i
+ 1) * sizeof(uint32_t) > left
) {
5049 outs() << "\t\t remaining list entries extend past the of the section\n";
5052 memcpy(&l
, list
+ i
* sizeof(uint32_t), sizeof(uint32_t));
5053 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5054 sys::swapByteOrder(l
);
5056 print_indent(indent
);
5057 outs() << " list[" << i
<< "] " << format("0x%08" PRIx32
, l
);
5058 if (print_protocol(l
, indent
, info
))
5059 outs() << "(not in an __OBJC section)\n";
5064 static void print_ivar_list64_t(uint64_t p
, struct DisassembleInfo
*info
) {
5065 struct ivar_list64_t il
;
5068 uint32_t offset
, xoffset
, left
, j
;
5070 const char *name
, *sym_name
, *ivar_offset_p
;
5071 uint64_t ivar_offset
, n_value
;
5073 r
= get_pointer_64(p
, offset
, left
, S
, info
);
5076 memset(&il
, '\0', sizeof(struct ivar_list64_t
));
5077 if (left
< sizeof(struct ivar_list64_t
)) {
5078 memcpy(&il
, r
, left
);
5079 outs() << " (ivar_list_t entends past the end of the section)\n";
5081 memcpy(&il
, r
, sizeof(struct ivar_list64_t
));
5082 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5084 outs() << " entsize " << il
.entsize
<< "\n";
5085 outs() << " count " << il
.count
<< "\n";
5087 p
+= sizeof(struct ivar_list64_t
);
5088 offset
+= sizeof(struct ivar_list64_t
);
5089 for (j
= 0; j
< il
.count
; j
++) {
5090 r
= get_pointer_64(p
, offset
, left
, S
, info
);
5093 memset(&i
, '\0', sizeof(struct ivar64_t
));
5094 if (left
< sizeof(struct ivar64_t
)) {
5095 memcpy(&i
, r
, left
);
5096 outs() << " (ivar_t entends past the end of the section)\n";
5098 memcpy(&i
, r
, sizeof(struct ivar64_t
));
5099 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5102 outs() << "\t\t\t offset ";
5103 sym_name
= get_symbol_64(offset
+ offsetof(struct ivar64_t
, offset
), S
,
5104 info
, n_value
, i
.offset
);
5106 if (info
->verbose
&& sym_name
!= nullptr)
5109 outs() << format("0x%" PRIx64
, n_value
);
5111 outs() << " + " << format("0x%" PRIx64
, i
.offset
);
5113 outs() << format("0x%" PRIx64
, i
.offset
);
5114 ivar_offset_p
= get_pointer_64(i
.offset
+ n_value
, xoffset
, left
, xS
, info
);
5115 if (ivar_offset_p
!= nullptr && left
>= sizeof(*ivar_offset_p
)) {
5116 memcpy(&ivar_offset
, ivar_offset_p
, sizeof(ivar_offset
));
5117 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5118 sys::swapByteOrder(ivar_offset
);
5119 outs() << " " << ivar_offset
<< "\n";
5123 outs() << "\t\t\t name ";
5124 sym_name
= get_symbol_64(offset
+ offsetof(struct ivar64_t
, name
), S
, info
,
5127 if (info
->verbose
&& sym_name
!= nullptr)
5130 outs() << format("0x%" PRIx64
, n_value
);
5132 outs() << " + " << format("0x%" PRIx64
, i
.name
);
5134 outs() << format("0x%" PRIx64
, i
.name
);
5135 name
= get_pointer_64(i
.name
+ n_value
, xoffset
, left
, xS
, info
);
5136 if (name
!= nullptr)
5137 outs() << format(" %.*s", left
, name
);
5140 outs() << "\t\t\t type ";
5141 sym_name
= get_symbol_64(offset
+ offsetof(struct ivar64_t
, type
), S
, info
,
5143 name
= get_pointer_64(i
.type
+ n_value
, xoffset
, left
, xS
, info
);
5145 if (info
->verbose
&& sym_name
!= nullptr)
5148 outs() << format("0x%" PRIx64
, n_value
);
5150 outs() << " + " << format("0x%" PRIx64
, i
.type
);
5152 outs() << format("0x%" PRIx64
, i
.type
);
5153 if (name
!= nullptr)
5154 outs() << format(" %.*s", left
, name
);
5157 outs() << "\t\t\talignment " << i
.alignment
<< "\n";
5158 outs() << "\t\t\t size " << i
.size
<< "\n";
5160 p
+= sizeof(struct ivar64_t
);
5161 offset
+= sizeof(struct ivar64_t
);
5165 static void print_ivar_list32_t(uint32_t p
, struct DisassembleInfo
*info
) {
5166 struct ivar_list32_t il
;
5169 uint32_t offset
, xoffset
, left
, j
;
5171 const char *name
, *ivar_offset_p
;
5172 uint32_t ivar_offset
;
5174 r
= get_pointer_32(p
, offset
, left
, S
, info
);
5177 memset(&il
, '\0', sizeof(struct ivar_list32_t
));
5178 if (left
< sizeof(struct ivar_list32_t
)) {
5179 memcpy(&il
, r
, left
);
5180 outs() << " (ivar_list_t entends past the end of the section)\n";
5182 memcpy(&il
, r
, sizeof(struct ivar_list32_t
));
5183 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5185 outs() << " entsize " << il
.entsize
<< "\n";
5186 outs() << " count " << il
.count
<< "\n";
5188 p
+= sizeof(struct ivar_list32_t
);
5189 offset
+= sizeof(struct ivar_list32_t
);
5190 for (j
= 0; j
< il
.count
; j
++) {
5191 r
= get_pointer_32(p
, offset
, left
, S
, info
);
5194 memset(&i
, '\0', sizeof(struct ivar32_t
));
5195 if (left
< sizeof(struct ivar32_t
)) {
5196 memcpy(&i
, r
, left
);
5197 outs() << " (ivar_t entends past the end of the section)\n";
5199 memcpy(&i
, r
, sizeof(struct ivar32_t
));
5200 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5203 outs() << "\t\t\t offset " << format("0x%" PRIx32
, i
.offset
);
5204 ivar_offset_p
= get_pointer_32(i
.offset
, xoffset
, left
, xS
, info
);
5205 if (ivar_offset_p
!= nullptr && left
>= sizeof(*ivar_offset_p
)) {
5206 memcpy(&ivar_offset
, ivar_offset_p
, sizeof(ivar_offset
));
5207 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5208 sys::swapByteOrder(ivar_offset
);
5209 outs() << " " << ivar_offset
<< "\n";
5213 outs() << "\t\t\t name " << format("0x%" PRIx32
, i
.name
);
5214 name
= get_pointer_32(i
.name
, xoffset
, left
, xS
, info
);
5215 if (name
!= nullptr)
5216 outs() << format(" %.*s", left
, name
);
5219 outs() << "\t\t\t type " << format("0x%" PRIx32
, i
.type
);
5220 name
= get_pointer_32(i
.type
, xoffset
, left
, xS
, info
);
5221 if (name
!= nullptr)
5222 outs() << format(" %.*s", left
, name
);
5225 outs() << "\t\t\talignment " << i
.alignment
<< "\n";
5226 outs() << "\t\t\t size " << i
.size
<< "\n";
5228 p
+= sizeof(struct ivar32_t
);
5229 offset
+= sizeof(struct ivar32_t
);
5233 static void print_objc_property_list64(uint64_t p
,
5234 struct DisassembleInfo
*info
) {
5235 struct objc_property_list64 opl
;
5236 struct objc_property64 op
;
5238 uint32_t offset
, xoffset
, left
, j
;
5240 const char *name
, *sym_name
;
5243 r
= get_pointer_64(p
, offset
, left
, S
, info
);
5246 memset(&opl
, '\0', sizeof(struct objc_property_list64
));
5247 if (left
< sizeof(struct objc_property_list64
)) {
5248 memcpy(&opl
, r
, left
);
5249 outs() << " (objc_property_list entends past the end of the section)\n";
5251 memcpy(&opl
, r
, sizeof(struct objc_property_list64
));
5252 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5254 outs() << " entsize " << opl
.entsize
<< "\n";
5255 outs() << " count " << opl
.count
<< "\n";
5257 p
+= sizeof(struct objc_property_list64
);
5258 offset
+= sizeof(struct objc_property_list64
);
5259 for (j
= 0; j
< opl
.count
; j
++) {
5260 r
= get_pointer_64(p
, offset
, left
, S
, info
);
5263 memset(&op
, '\0', sizeof(struct objc_property64
));
5264 if (left
< sizeof(struct objc_property64
)) {
5265 memcpy(&op
, r
, left
);
5266 outs() << " (objc_property entends past the end of the section)\n";
5268 memcpy(&op
, r
, sizeof(struct objc_property64
));
5269 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5272 outs() << "\t\t\t name ";
5273 sym_name
= get_symbol_64(offset
+ offsetof(struct objc_property64
, name
), S
,
5274 info
, n_value
, op
.name
);
5276 if (info
->verbose
&& sym_name
!= nullptr)
5279 outs() << format("0x%" PRIx64
, n_value
);
5281 outs() << " + " << format("0x%" PRIx64
, op
.name
);
5283 outs() << format("0x%" PRIx64
, op
.name
);
5284 name
= get_pointer_64(op
.name
+ n_value
, xoffset
, left
, xS
, info
);
5285 if (name
!= nullptr)
5286 outs() << format(" %.*s", left
, name
);
5289 outs() << "\t\t\tattributes ";
5291 get_symbol_64(offset
+ offsetof(struct objc_property64
, attributes
), S
,
5292 info
, n_value
, op
.attributes
);
5294 if (info
->verbose
&& sym_name
!= nullptr)
5297 outs() << format("0x%" PRIx64
, n_value
);
5298 if (op
.attributes
!= 0)
5299 outs() << " + " << format("0x%" PRIx64
, op
.attributes
);
5301 outs() << format("0x%" PRIx64
, op
.attributes
);
5302 name
= get_pointer_64(op
.attributes
+ n_value
, xoffset
, left
, xS
, info
);
5303 if (name
!= nullptr)
5304 outs() << format(" %.*s", left
, name
);
5307 p
+= sizeof(struct objc_property64
);
5308 offset
+= sizeof(struct objc_property64
);
5312 static void print_objc_property_list32(uint32_t p
,
5313 struct DisassembleInfo
*info
) {
5314 struct objc_property_list32 opl
;
5315 struct objc_property32 op
;
5317 uint32_t offset
, xoffset
, left
, j
;
5321 r
= get_pointer_32(p
, offset
, left
, S
, info
);
5324 memset(&opl
, '\0', sizeof(struct objc_property_list32
));
5325 if (left
< sizeof(struct objc_property_list32
)) {
5326 memcpy(&opl
, r
, left
);
5327 outs() << " (objc_property_list entends past the end of the section)\n";
5329 memcpy(&opl
, r
, sizeof(struct objc_property_list32
));
5330 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5332 outs() << " entsize " << opl
.entsize
<< "\n";
5333 outs() << " count " << opl
.count
<< "\n";
5335 p
+= sizeof(struct objc_property_list32
);
5336 offset
+= sizeof(struct objc_property_list32
);
5337 for (j
= 0; j
< opl
.count
; j
++) {
5338 r
= get_pointer_32(p
, offset
, left
, S
, info
);
5341 memset(&op
, '\0', sizeof(struct objc_property32
));
5342 if (left
< sizeof(struct objc_property32
)) {
5343 memcpy(&op
, r
, left
);
5344 outs() << " (objc_property entends past the end of the section)\n";
5346 memcpy(&op
, r
, sizeof(struct objc_property32
));
5347 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5350 outs() << "\t\t\t name " << format("0x%" PRIx32
, op
.name
);
5351 name
= get_pointer_32(op
.name
, xoffset
, left
, xS
, info
);
5352 if (name
!= nullptr)
5353 outs() << format(" %.*s", left
, name
);
5356 outs() << "\t\t\tattributes " << format("0x%" PRIx32
, op
.attributes
);
5357 name
= get_pointer_32(op
.attributes
, xoffset
, left
, xS
, info
);
5358 if (name
!= nullptr)
5359 outs() << format(" %.*s", left
, name
);
5362 p
+= sizeof(struct objc_property32
);
5363 offset
+= sizeof(struct objc_property32
);
5367 static bool print_class_ro64_t(uint64_t p
, struct DisassembleInfo
*info
,
5368 bool &is_meta_class
) {
5369 struct class_ro64_t cro
;
5371 uint32_t offset
, xoffset
, left
;
5373 const char *name
, *sym_name
;
5376 r
= get_pointer_64(p
, offset
, left
, S
, info
);
5377 if (r
== nullptr || left
< sizeof(struct class_ro64_t
))
5379 memcpy(&cro
, r
, sizeof(struct class_ro64_t
));
5380 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5382 outs() << " flags " << format("0x%" PRIx32
, cro
.flags
);
5383 if (cro
.flags
& RO_META
)
5384 outs() << " RO_META";
5385 if (cro
.flags
& RO_ROOT
)
5386 outs() << " RO_ROOT";
5387 if (cro
.flags
& RO_HAS_CXX_STRUCTORS
)
5388 outs() << " RO_HAS_CXX_STRUCTORS";
5390 outs() << " instanceStart " << cro
.instanceStart
<< "\n";
5391 outs() << " instanceSize " << cro
.instanceSize
<< "\n";
5392 outs() << " reserved " << format("0x%" PRIx32
, cro
.reserved
)
5394 outs() << " ivarLayout " << format("0x%" PRIx64
, cro
.ivarLayout
)
5396 print_layout_map64(cro
.ivarLayout
, info
);
5399 sym_name
= get_symbol_64(offset
+ offsetof(struct class_ro64_t
, name
), S
,
5400 info
, n_value
, cro
.name
);
5402 if (info
->verbose
&& sym_name
!= nullptr)
5405 outs() << format("0x%" PRIx64
, n_value
);
5407 outs() << " + " << format("0x%" PRIx64
, cro
.name
);
5409 outs() << format("0x%" PRIx64
, cro
.name
);
5410 name
= get_pointer_64(cro
.name
+ n_value
, xoffset
, left
, xS
, info
);
5411 if (name
!= nullptr)
5412 outs() << format(" %.*s", left
, name
);
5415 outs() << " baseMethods ";
5416 sym_name
= get_symbol_64(offset
+ offsetof(struct class_ro64_t
, baseMethods
),
5417 S
, info
, n_value
, cro
.baseMethods
);
5419 if (info
->verbose
&& sym_name
!= nullptr)
5422 outs() << format("0x%" PRIx64
, n_value
);
5423 if (cro
.baseMethods
!= 0)
5424 outs() << " + " << format("0x%" PRIx64
, cro
.baseMethods
);
5426 outs() << format("0x%" PRIx64
, cro
.baseMethods
);
5427 outs() << " (struct method_list_t *)\n";
5428 if (cro
.baseMethods
+ n_value
!= 0)
5429 print_method_list64_t(cro
.baseMethods
+ n_value
, info
, "");
5431 outs() << " baseProtocols ";
5433 get_symbol_64(offset
+ offsetof(struct class_ro64_t
, baseProtocols
), S
,
5434 info
, n_value
, cro
.baseProtocols
);
5436 if (info
->verbose
&& sym_name
!= nullptr)
5439 outs() << format("0x%" PRIx64
, n_value
);
5440 if (cro
.baseProtocols
!= 0)
5441 outs() << " + " << format("0x%" PRIx64
, cro
.baseProtocols
);
5443 outs() << format("0x%" PRIx64
, cro
.baseProtocols
);
5445 if (cro
.baseProtocols
+ n_value
!= 0)
5446 print_protocol_list64_t(cro
.baseProtocols
+ n_value
, info
);
5448 outs() << " ivars ";
5449 sym_name
= get_symbol_64(offset
+ offsetof(struct class_ro64_t
, ivars
), S
,
5450 info
, n_value
, cro
.ivars
);
5452 if (info
->verbose
&& sym_name
!= nullptr)
5455 outs() << format("0x%" PRIx64
, n_value
);
5457 outs() << " + " << format("0x%" PRIx64
, cro
.ivars
);
5459 outs() << format("0x%" PRIx64
, cro
.ivars
);
5461 if (cro
.ivars
+ n_value
!= 0)
5462 print_ivar_list64_t(cro
.ivars
+ n_value
, info
);
5464 outs() << " weakIvarLayout ";
5466 get_symbol_64(offset
+ offsetof(struct class_ro64_t
, weakIvarLayout
), S
,
5467 info
, n_value
, cro
.weakIvarLayout
);
5469 if (info
->verbose
&& sym_name
!= nullptr)
5472 outs() << format("0x%" PRIx64
, n_value
);
5473 if (cro
.weakIvarLayout
!= 0)
5474 outs() << " + " << format("0x%" PRIx64
, cro
.weakIvarLayout
);
5476 outs() << format("0x%" PRIx64
, cro
.weakIvarLayout
);
5478 print_layout_map64(cro
.weakIvarLayout
+ n_value
, info
);
5480 outs() << " baseProperties ";
5482 get_symbol_64(offset
+ offsetof(struct class_ro64_t
, baseProperties
), S
,
5483 info
, n_value
, cro
.baseProperties
);
5485 if (info
->verbose
&& sym_name
!= nullptr)
5488 outs() << format("0x%" PRIx64
, n_value
);
5489 if (cro
.baseProperties
!= 0)
5490 outs() << " + " << format("0x%" PRIx64
, cro
.baseProperties
);
5492 outs() << format("0x%" PRIx64
, cro
.baseProperties
);
5494 if (cro
.baseProperties
+ n_value
!= 0)
5495 print_objc_property_list64(cro
.baseProperties
+ n_value
, info
);
5497 is_meta_class
= (cro
.flags
& RO_META
) != 0;
5501 static bool print_class_ro32_t(uint32_t p
, struct DisassembleInfo
*info
,
5502 bool &is_meta_class
) {
5503 struct class_ro32_t cro
;
5505 uint32_t offset
, xoffset
, left
;
5509 r
= get_pointer_32(p
, offset
, left
, S
, info
);
5512 memset(&cro
, '\0', sizeof(struct class_ro32_t
));
5513 if (left
< sizeof(struct class_ro32_t
)) {
5514 memcpy(&cro
, r
, left
);
5515 outs() << " (class_ro_t entends past the end of the section)\n";
5517 memcpy(&cro
, r
, sizeof(struct class_ro32_t
));
5518 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5520 outs() << " flags " << format("0x%" PRIx32
, cro
.flags
);
5521 if (cro
.flags
& RO_META
)
5522 outs() << " RO_META";
5523 if (cro
.flags
& RO_ROOT
)
5524 outs() << " RO_ROOT";
5525 if (cro
.flags
& RO_HAS_CXX_STRUCTORS
)
5526 outs() << " RO_HAS_CXX_STRUCTORS";
5528 outs() << " instanceStart " << cro
.instanceStart
<< "\n";
5529 outs() << " instanceSize " << cro
.instanceSize
<< "\n";
5530 outs() << " ivarLayout " << format("0x%" PRIx32
, cro
.ivarLayout
)
5532 print_layout_map32(cro
.ivarLayout
, info
);
5534 outs() << " name " << format("0x%" PRIx32
, cro
.name
);
5535 name
= get_pointer_32(cro
.name
, xoffset
, left
, xS
, info
);
5536 if (name
!= nullptr)
5537 outs() << format(" %.*s", left
, name
);
5540 outs() << " baseMethods "
5541 << format("0x%" PRIx32
, cro
.baseMethods
)
5542 << " (struct method_list_t *)\n";
5543 if (cro
.baseMethods
!= 0)
5544 print_method_list32_t(cro
.baseMethods
, info
, "");
5546 outs() << " baseProtocols "
5547 << format("0x%" PRIx32
, cro
.baseProtocols
) << "\n";
5548 if (cro
.baseProtocols
!= 0)
5549 print_protocol_list32_t(cro
.baseProtocols
, info
);
5550 outs() << " ivars " << format("0x%" PRIx32
, cro
.ivars
)
5553 print_ivar_list32_t(cro
.ivars
, info
);
5554 outs() << " weakIvarLayout "
5555 << format("0x%" PRIx32
, cro
.weakIvarLayout
) << "\n";
5556 print_layout_map32(cro
.weakIvarLayout
, info
);
5557 outs() << " baseProperties "
5558 << format("0x%" PRIx32
, cro
.baseProperties
) << "\n";
5559 if (cro
.baseProperties
!= 0)
5560 print_objc_property_list32(cro
.baseProperties
, info
);
5561 is_meta_class
= (cro
.flags
& RO_META
) != 0;
5565 static void print_class64_t(uint64_t p
, struct DisassembleInfo
*info
) {
5568 uint32_t offset
, left
;
5571 uint64_t isa_n_value
, n_value
;
5573 r
= get_pointer_64(p
, offset
, left
, S
, info
);
5574 if (r
== nullptr || left
< sizeof(struct class64_t
))
5576 memcpy(&c
, r
, sizeof(struct class64_t
));
5577 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5580 outs() << " isa " << format("0x%" PRIx64
, c
.isa
);
5581 name
= get_symbol_64(offset
+ offsetof(struct class64_t
, isa
), S
, info
,
5582 isa_n_value
, c
.isa
);
5583 if (name
!= nullptr)
5584 outs() << " " << name
;
5587 outs() << " superclass " << format("0x%" PRIx64
, c
.superclass
);
5588 name
= get_symbol_64(offset
+ offsetof(struct class64_t
, superclass
), S
, info
,
5589 n_value
, c
.superclass
);
5590 if (name
!= nullptr)
5591 outs() << " " << name
;
5593 name
= get_dyld_bind_info_symbolname(S
.getAddress() +
5594 offset
+ offsetof(struct class64_t
, superclass
), info
);
5595 if (name
!= nullptr)
5596 outs() << " " << name
;
5600 outs() << " cache " << format("0x%" PRIx64
, c
.cache
);
5601 name
= get_symbol_64(offset
+ offsetof(struct class64_t
, cache
), S
, info
,
5603 if (name
!= nullptr)
5604 outs() << " " << name
;
5607 outs() << " vtable " << format("0x%" PRIx64
, c
.vtable
);
5608 name
= get_symbol_64(offset
+ offsetof(struct class64_t
, vtable
), S
, info
,
5610 if (name
!= nullptr)
5611 outs() << " " << name
;
5614 name
= get_symbol_64(offset
+ offsetof(struct class64_t
, data
), S
, info
,
5618 if (info
->verbose
&& name
!= nullptr)
5621 outs() << format("0x%" PRIx64
, n_value
);
5623 outs() << " + " << format("0x%" PRIx64
, c
.data
);
5625 outs() << format("0x%" PRIx64
, c
.data
);
5626 outs() << " (struct class_ro_t *)";
5628 // This is a Swift class if some of the low bits of the pointer are set.
5629 if ((c
.data
+ n_value
) & 0x7)
5630 outs() << " Swift class";
5633 if (!print_class_ro64_t((c
.data
+ n_value
) & ~0x7, info
, is_meta_class
))
5636 if (!is_meta_class
&&
5637 c
.isa
+ isa_n_value
!= p
&&
5638 c
.isa
+ isa_n_value
!= 0 &&
5639 info
->depth
< 100) {
5641 outs() << "Meta Class\n";
5642 print_class64_t(c
.isa
+ isa_n_value
, info
);
5646 static void print_class32_t(uint32_t p
, struct DisassembleInfo
*info
) {
5649 uint32_t offset
, left
;
5653 r
= get_pointer_32(p
, offset
, left
, S
, info
);
5656 memset(&c
, '\0', sizeof(struct class32_t
));
5657 if (left
< sizeof(struct class32_t
)) {
5658 memcpy(&c
, r
, left
);
5659 outs() << " (class_t entends past the end of the section)\n";
5661 memcpy(&c
, r
, sizeof(struct class32_t
));
5662 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5665 outs() << " isa " << format("0x%" PRIx32
, c
.isa
);
5667 get_symbol_32(offset
+ offsetof(struct class32_t
, isa
), S
, info
, c
.isa
);
5668 if (name
!= nullptr)
5669 outs() << " " << name
;
5672 outs() << " superclass " << format("0x%" PRIx32
, c
.superclass
);
5673 name
= get_symbol_32(offset
+ offsetof(struct class32_t
, superclass
), S
, info
,
5675 if (name
!= nullptr)
5676 outs() << " " << name
;
5679 outs() << " cache " << format("0x%" PRIx32
, c
.cache
);
5680 name
= get_symbol_32(offset
+ offsetof(struct class32_t
, cache
), S
, info
,
5682 if (name
!= nullptr)
5683 outs() << " " << name
;
5686 outs() << " vtable " << format("0x%" PRIx32
, c
.vtable
);
5687 name
= get_symbol_32(offset
+ offsetof(struct class32_t
, vtable
), S
, info
,
5689 if (name
!= nullptr)
5690 outs() << " " << name
;
5694 get_symbol_32(offset
+ offsetof(struct class32_t
, data
), S
, info
, c
.data
);
5695 outs() << " data " << format("0x%" PRIx32
, c
.data
)
5696 << " (struct class_ro_t *)";
5698 // This is a Swift class if some of the low bits of the pointer are set.
5700 outs() << " Swift class";
5703 if (!print_class_ro32_t(c
.data
& ~0x3, info
, is_meta_class
))
5706 if (!is_meta_class
) {
5707 outs() << "Meta Class\n";
5708 print_class32_t(c
.isa
, info
);
5712 static void print_objc_class_t(struct objc_class_t
*objc_class
,
5713 struct DisassembleInfo
*info
) {
5714 uint32_t offset
, left
, xleft
;
5715 const char *name
, *p
, *ivar_list
;
5718 struct objc_ivar_list_t objc_ivar_list
;
5719 struct objc_ivar_t ivar
;
5721 outs() << "\t\t isa " << format("0x%08" PRIx32
, objc_class
->isa
);
5722 if (info
->verbose
&& CLS_GETINFO(objc_class
, CLS_META
)) {
5723 name
= get_pointer_32(objc_class
->isa
, offset
, left
, S
, info
, true);
5724 if (name
!= nullptr)
5725 outs() << format(" %.*s", left
, name
);
5727 outs() << " (not in an __OBJC section)";
5731 outs() << "\t super_class "
5732 << format("0x%08" PRIx32
, objc_class
->super_class
);
5733 if (info
->verbose
) {
5734 name
= get_pointer_32(objc_class
->super_class
, offset
, left
, S
, info
, true);
5735 if (name
!= nullptr)
5736 outs() << format(" %.*s", left
, name
);
5738 outs() << " (not in an __OBJC section)";
5742 outs() << "\t\t name " << format("0x%08" PRIx32
, objc_class
->name
);
5743 if (info
->verbose
) {
5744 name
= get_pointer_32(objc_class
->name
, offset
, left
, S
, info
, true);
5745 if (name
!= nullptr)
5746 outs() << format(" %.*s", left
, name
);
5748 outs() << " (not in an __OBJC section)";
5752 outs() << "\t\t version " << format("0x%08" PRIx32
, objc_class
->version
)
5755 outs() << "\t\t info " << format("0x%08" PRIx32
, objc_class
->info
);
5756 if (info
->verbose
) {
5757 if (CLS_GETINFO(objc_class
, CLS_CLASS
))
5758 outs() << " CLS_CLASS";
5759 else if (CLS_GETINFO(objc_class
, CLS_META
))
5760 outs() << " CLS_META";
5764 outs() << "\t instance_size "
5765 << format("0x%08" PRIx32
, objc_class
->instance_size
) << "\n";
5767 p
= get_pointer_32(objc_class
->ivars
, offset
, left
, S
, info
, true);
5768 outs() << "\t\t ivars " << format("0x%08" PRIx32
, objc_class
->ivars
);
5770 if (left
> sizeof(struct objc_ivar_list_t
)) {
5772 memcpy(&objc_ivar_list
, p
, sizeof(struct objc_ivar_list_t
));
5774 outs() << " (entends past the end of the section)\n";
5775 memset(&objc_ivar_list
, '\0', sizeof(struct objc_ivar_list_t
));
5776 memcpy(&objc_ivar_list
, p
, left
);
5778 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5779 swapStruct(objc_ivar_list
);
5780 outs() << "\t\t ivar_count " << objc_ivar_list
.ivar_count
<< "\n";
5781 ivar_list
= p
+ sizeof(struct objc_ivar_list_t
);
5782 for (i
= 0; i
< objc_ivar_list
.ivar_count
; i
++) {
5783 if ((i
+ 1) * sizeof(struct objc_ivar_t
) > left
) {
5784 outs() << "\t\t remaining ivar's extend past the of the section\n";
5787 memcpy(&ivar
, ivar_list
+ i
* sizeof(struct objc_ivar_t
),
5788 sizeof(struct objc_ivar_t
));
5789 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5792 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32
, ivar
.ivar_name
);
5793 if (info
->verbose
) {
5794 name
= get_pointer_32(ivar
.ivar_name
, offset
, xleft
, S
, info
, true);
5795 if (name
!= nullptr)
5796 outs() << format(" %.*s", xleft
, name
);
5798 outs() << " (not in an __OBJC section)";
5802 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32
, ivar
.ivar_type
);
5803 if (info
->verbose
) {
5804 name
= get_pointer_32(ivar
.ivar_type
, offset
, xleft
, S
, info
, true);
5805 if (name
!= nullptr)
5806 outs() << format(" %.*s", xleft
, name
);
5808 outs() << " (not in an __OBJC section)";
5812 outs() << "\t\t ivar_offset "
5813 << format("0x%08" PRIx32
, ivar
.ivar_offset
) << "\n";
5816 outs() << " (not in an __OBJC section)\n";
5819 outs() << "\t\t methods " << format("0x%08" PRIx32
, objc_class
->methodLists
);
5820 if (print_method_list(objc_class
->methodLists
, info
))
5821 outs() << " (not in an __OBJC section)\n";
5823 outs() << "\t\t cache " << format("0x%08" PRIx32
, objc_class
->cache
)
5826 outs() << "\t\tprotocols " << format("0x%08" PRIx32
, objc_class
->protocols
);
5827 if (print_protocol_list(objc_class
->protocols
, 16, info
))
5828 outs() << " (not in an __OBJC section)\n";
5831 static void print_objc_objc_category_t(struct objc_category_t
*objc_category
,
5832 struct DisassembleInfo
*info
) {
5833 uint32_t offset
, left
;
5837 outs() << "\t category name "
5838 << format("0x%08" PRIx32
, objc_category
->category_name
);
5839 if (info
->verbose
) {
5840 name
= get_pointer_32(objc_category
->category_name
, offset
, left
, S
, info
,
5842 if (name
!= nullptr)
5843 outs() << format(" %.*s", left
, name
);
5845 outs() << " (not in an __OBJC section)";
5849 outs() << "\t\t class name "
5850 << format("0x%08" PRIx32
, objc_category
->class_name
);
5851 if (info
->verbose
) {
5853 get_pointer_32(objc_category
->class_name
, offset
, left
, S
, info
, true);
5854 if (name
!= nullptr)
5855 outs() << format(" %.*s", left
, name
);
5857 outs() << " (not in an __OBJC section)";
5861 outs() << "\t instance methods "
5862 << format("0x%08" PRIx32
, objc_category
->instance_methods
);
5863 if (print_method_list(objc_category
->instance_methods
, info
))
5864 outs() << " (not in an __OBJC section)\n";
5866 outs() << "\t class methods "
5867 << format("0x%08" PRIx32
, objc_category
->class_methods
);
5868 if (print_method_list(objc_category
->class_methods
, info
))
5869 outs() << " (not in an __OBJC section)\n";
5872 static void print_category64_t(uint64_t p
, struct DisassembleInfo
*info
) {
5873 struct category64_t c
;
5875 uint32_t offset
, xoffset
, left
;
5877 const char *name
, *sym_name
;
5880 r
= get_pointer_64(p
, offset
, left
, S
, info
);
5883 memset(&c
, '\0', sizeof(struct category64_t
));
5884 if (left
< sizeof(struct category64_t
)) {
5885 memcpy(&c
, r
, left
);
5886 outs() << " (category_t entends past the end of the section)\n";
5888 memcpy(&c
, r
, sizeof(struct category64_t
));
5889 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
5893 sym_name
= get_symbol_64(offset
+ offsetof(struct category64_t
, name
), S
,
5894 info
, n_value
, c
.name
);
5896 if (info
->verbose
&& sym_name
!= nullptr)
5899 outs() << format("0x%" PRIx64
, n_value
);
5901 outs() << " + " << format("0x%" PRIx64
, c
.name
);
5903 outs() << format("0x%" PRIx64
, c
.name
);
5904 name
= get_pointer_64(c
.name
+ n_value
, xoffset
, left
, xS
, info
);
5905 if (name
!= nullptr)
5906 outs() << format(" %.*s", left
, name
);
5910 sym_name
= get_symbol_64(offset
+ offsetof(struct category64_t
, cls
), S
, info
,
5913 if (info
->verbose
&& sym_name
!= nullptr)
5916 outs() << format("0x%" PRIx64
, n_value
);
5918 outs() << " + " << format("0x%" PRIx64
, c
.cls
);
5920 outs() << format("0x%" PRIx64
, c
.cls
);
5922 if (c
.cls
+ n_value
!= 0)
5923 print_class64_t(c
.cls
+ n_value
, info
);
5925 outs() << " instanceMethods ";
5927 get_symbol_64(offset
+ offsetof(struct category64_t
, instanceMethods
), S
,
5928 info
, n_value
, c
.instanceMethods
);
5930 if (info
->verbose
&& sym_name
!= nullptr)
5933 outs() << format("0x%" PRIx64
, n_value
);
5934 if (c
.instanceMethods
!= 0)
5935 outs() << " + " << format("0x%" PRIx64
, c
.instanceMethods
);
5937 outs() << format("0x%" PRIx64
, c
.instanceMethods
);
5939 if (c
.instanceMethods
+ n_value
!= 0)
5940 print_method_list64_t(c
.instanceMethods
+ n_value
, info
, "");
5942 outs() << " classMethods ";
5943 sym_name
= get_symbol_64(offset
+ offsetof(struct category64_t
, classMethods
),
5944 S
, info
, n_value
, c
.classMethods
);
5946 if (info
->verbose
&& sym_name
!= nullptr)
5949 outs() << format("0x%" PRIx64
, n_value
);
5950 if (c
.classMethods
!= 0)
5951 outs() << " + " << format("0x%" PRIx64
, c
.classMethods
);
5953 outs() << format("0x%" PRIx64
, c
.classMethods
);
5955 if (c
.classMethods
+ n_value
!= 0)
5956 print_method_list64_t(c
.classMethods
+ n_value
, info
, "");
5958 outs() << " protocols ";
5959 sym_name
= get_symbol_64(offset
+ offsetof(struct category64_t
, protocols
), S
,
5960 info
, n_value
, c
.protocols
);
5962 if (info
->verbose
&& sym_name
!= nullptr)
5965 outs() << format("0x%" PRIx64
, n_value
);
5966 if (c
.protocols
!= 0)
5967 outs() << " + " << format("0x%" PRIx64
, c
.protocols
);
5969 outs() << format("0x%" PRIx64
, c
.protocols
);
5971 if (c
.protocols
+ n_value
!= 0)
5972 print_protocol_list64_t(c
.protocols
+ n_value
, info
);
5974 outs() << "instanceProperties ";
5976 get_symbol_64(offset
+ offsetof(struct category64_t
, instanceProperties
),
5977 S
, info
, n_value
, c
.instanceProperties
);
5979 if (info
->verbose
&& sym_name
!= nullptr)
5982 outs() << format("0x%" PRIx64
, n_value
);
5983 if (c
.instanceProperties
!= 0)
5984 outs() << " + " << format("0x%" PRIx64
, c
.instanceProperties
);
5986 outs() << format("0x%" PRIx64
, c
.instanceProperties
);
5988 if (c
.instanceProperties
+ n_value
!= 0)
5989 print_objc_property_list64(c
.instanceProperties
+ n_value
, info
);
5992 static void print_category32_t(uint32_t p
, struct DisassembleInfo
*info
) {
5993 struct category32_t c
;
5995 uint32_t offset
, left
;
5999 r
= get_pointer_32(p
, offset
, left
, S
, info
);
6002 memset(&c
, '\0', sizeof(struct category32_t
));
6003 if (left
< sizeof(struct category32_t
)) {
6004 memcpy(&c
, r
, left
);
6005 outs() << " (category_t entends past the end of the section)\n";
6007 memcpy(&c
, r
, sizeof(struct category32_t
));
6008 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
6011 outs() << " name " << format("0x%" PRIx32
, c
.name
);
6012 name
= get_symbol_32(offset
+ offsetof(struct category32_t
, name
), S
, info
,
6015 outs() << " " << name
;
6018 outs() << " cls " << format("0x%" PRIx32
, c
.cls
) << "\n";
6020 print_class32_t(c
.cls
, info
);
6021 outs() << " instanceMethods " << format("0x%" PRIx32
, c
.instanceMethods
)
6023 if (c
.instanceMethods
!= 0)
6024 print_method_list32_t(c
.instanceMethods
, info
, "");
6025 outs() << " classMethods " << format("0x%" PRIx32
, c
.classMethods
)
6027 if (c
.classMethods
!= 0)
6028 print_method_list32_t(c
.classMethods
, info
, "");
6029 outs() << " protocols " << format("0x%" PRIx32
, c
.protocols
) << "\n";
6030 if (c
.protocols
!= 0)
6031 print_protocol_list32_t(c
.protocols
, info
);
6032 outs() << "instanceProperties " << format("0x%" PRIx32
, c
.instanceProperties
)
6034 if (c
.instanceProperties
!= 0)
6035 print_objc_property_list32(c
.instanceProperties
, info
);
6038 static void print_message_refs64(SectionRef S
, struct DisassembleInfo
*info
) {
6039 uint32_t i
, left
, offset
, xoffset
;
6040 uint64_t p
, n_value
;
6041 struct message_ref64 mr
;
6042 const char *name
, *sym_name
;
6046 if (S
== SectionRef())
6050 Expected
<StringRef
> SecNameOrErr
= S
.getName();
6052 SectName
= *SecNameOrErr
;
6054 consumeError(SecNameOrErr
.takeError());
6056 DataRefImpl Ref
= S
.getRawDataRefImpl();
6057 StringRef SegName
= info
->O
->getSectionFinalSegmentName(Ref
);
6058 outs() << "Contents of (" << SegName
<< "," << SectName
<< ") section\n";
6060 for (i
= 0; i
< S
.getSize(); i
+= sizeof(struct message_ref64
)) {
6061 p
= S
.getAddress() + i
;
6062 r
= get_pointer_64(p
, offset
, left
, S
, info
);
6065 memset(&mr
, '\0', sizeof(struct message_ref64
));
6066 if (left
< sizeof(struct message_ref64
)) {
6067 memcpy(&mr
, r
, left
);
6068 outs() << " (message_ref entends past the end of the section)\n";
6070 memcpy(&mr
, r
, sizeof(struct message_ref64
));
6071 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
6075 name
= get_symbol_64(offset
+ offsetof(struct message_ref64
, imp
), S
, info
,
6078 outs() << format("0x%" PRIx64
, n_value
) << " ";
6080 outs() << "+ " << format("0x%" PRIx64
, mr
.imp
) << " ";
6082 outs() << format("0x%" PRIx64
, mr
.imp
) << " ";
6083 if (name
!= nullptr)
6084 outs() << " " << name
;
6088 sym_name
= get_symbol_64(offset
+ offsetof(struct message_ref64
, sel
), S
,
6089 info
, n_value
, mr
.sel
);
6091 if (info
->verbose
&& sym_name
!= nullptr)
6094 outs() << format("0x%" PRIx64
, n_value
);
6096 outs() << " + " << format("0x%" PRIx64
, mr
.sel
);
6098 outs() << format("0x%" PRIx64
, mr
.sel
);
6099 name
= get_pointer_64(mr
.sel
+ n_value
, xoffset
, left
, xS
, info
);
6100 if (name
!= nullptr)
6101 outs() << format(" %.*s", left
, name
);
6104 offset
+= sizeof(struct message_ref64
);
6108 static void print_message_refs32(SectionRef S
, struct DisassembleInfo
*info
) {
6109 uint32_t i
, left
, offset
, xoffset
, p
;
6110 struct message_ref32 mr
;
6111 const char *name
, *r
;
6114 if (S
== SectionRef())
6118 Expected
<StringRef
> SecNameOrErr
= S
.getName();
6120 SectName
= *SecNameOrErr
;
6122 consumeError(SecNameOrErr
.takeError());
6124 DataRefImpl Ref
= S
.getRawDataRefImpl();
6125 StringRef SegName
= info
->O
->getSectionFinalSegmentName(Ref
);
6126 outs() << "Contents of (" << SegName
<< "," << SectName
<< ") section\n";
6128 for (i
= 0; i
< S
.getSize(); i
+= sizeof(struct message_ref64
)) {
6129 p
= S
.getAddress() + i
;
6130 r
= get_pointer_32(p
, offset
, left
, S
, info
);
6133 memset(&mr
, '\0', sizeof(struct message_ref32
));
6134 if (left
< sizeof(struct message_ref32
)) {
6135 memcpy(&mr
, r
, left
);
6136 outs() << " (message_ref entends past the end of the section)\n";
6138 memcpy(&mr
, r
, sizeof(struct message_ref32
));
6139 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
6142 outs() << " imp " << format("0x%" PRIx32
, mr
.imp
);
6143 name
= get_symbol_32(offset
+ offsetof(struct message_ref32
, imp
), S
, info
,
6145 if (name
!= nullptr)
6146 outs() << " " << name
;
6149 outs() << " sel " << format("0x%" PRIx32
, mr
.sel
);
6150 name
= get_pointer_32(mr
.sel
, xoffset
, left
, xS
, info
);
6151 if (name
!= nullptr)
6152 outs() << " " << name
;
6155 offset
+= sizeof(struct message_ref32
);
6159 static void print_image_info64(SectionRef S
, struct DisassembleInfo
*info
) {
6160 uint32_t left
, offset
, swift_version
;
6162 struct objc_image_info64 o
;
6165 if (S
== SectionRef())
6169 Expected
<StringRef
> SecNameOrErr
= S
.getName();
6171 SectName
= *SecNameOrErr
;
6173 consumeError(SecNameOrErr
.takeError());
6175 DataRefImpl Ref
= S
.getRawDataRefImpl();
6176 StringRef SegName
= info
->O
->getSectionFinalSegmentName(Ref
);
6177 outs() << "Contents of (" << SegName
<< "," << SectName
<< ") section\n";
6179 r
= get_pointer_64(p
, offset
, left
, S
, info
);
6182 memset(&o
, '\0', sizeof(struct objc_image_info64
));
6183 if (left
< sizeof(struct objc_image_info64
)) {
6184 memcpy(&o
, r
, left
);
6185 outs() << " (objc_image_info entends past the end of the section)\n";
6187 memcpy(&o
, r
, sizeof(struct objc_image_info64
));
6188 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
6190 outs() << " version " << o
.version
<< "\n";
6191 outs() << " flags " << format("0x%" PRIx32
, o
.flags
);
6192 if (o
.flags
& OBJC_IMAGE_IS_REPLACEMENT
)
6193 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
6194 if (o
.flags
& OBJC_IMAGE_SUPPORTS_GC
)
6195 outs() << " OBJC_IMAGE_SUPPORTS_GC";
6196 if (o
.flags
& OBJC_IMAGE_IS_SIMULATED
)
6197 outs() << " OBJC_IMAGE_IS_SIMULATED";
6198 if (o
.flags
& OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES
)
6199 outs() << " OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES";
6200 swift_version
= (o
.flags
>> 8) & 0xff;
6201 if (swift_version
!= 0) {
6202 if (swift_version
== 1)
6203 outs() << " Swift 1.0";
6204 else if (swift_version
== 2)
6205 outs() << " Swift 1.1";
6206 else if(swift_version
== 3)
6207 outs() << " Swift 2.0";
6208 else if(swift_version
== 4)
6209 outs() << " Swift 3.0";
6210 else if(swift_version
== 5)
6211 outs() << " Swift 4.0";
6212 else if(swift_version
== 6)
6213 outs() << " Swift 4.1/Swift 4.2";
6214 else if(swift_version
== 7)
6215 outs() << " Swift 5 or later";
6217 outs() << " unknown future Swift version (" << swift_version
<< ")";
6222 static void print_image_info32(SectionRef S
, struct DisassembleInfo
*info
) {
6223 uint32_t left
, offset
, swift_version
, p
;
6224 struct objc_image_info32 o
;
6227 if (S
== SectionRef())
6231 Expected
<StringRef
> SecNameOrErr
= S
.getName();
6233 SectName
= *SecNameOrErr
;
6235 consumeError(SecNameOrErr
.takeError());
6237 DataRefImpl Ref
= S
.getRawDataRefImpl();
6238 StringRef SegName
= info
->O
->getSectionFinalSegmentName(Ref
);
6239 outs() << "Contents of (" << SegName
<< "," << SectName
<< ") section\n";
6241 r
= get_pointer_32(p
, offset
, left
, S
, info
);
6244 memset(&o
, '\0', sizeof(struct objc_image_info32
));
6245 if (left
< sizeof(struct objc_image_info32
)) {
6246 memcpy(&o
, r
, left
);
6247 outs() << " (objc_image_info entends past the end of the section)\n";
6249 memcpy(&o
, r
, sizeof(struct objc_image_info32
));
6250 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
6252 outs() << " version " << o
.version
<< "\n";
6253 outs() << " flags " << format("0x%" PRIx32
, o
.flags
);
6254 if (o
.flags
& OBJC_IMAGE_IS_REPLACEMENT
)
6255 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
6256 if (o
.flags
& OBJC_IMAGE_SUPPORTS_GC
)
6257 outs() << " OBJC_IMAGE_SUPPORTS_GC";
6258 swift_version
= (o
.flags
>> 8) & 0xff;
6259 if (swift_version
!= 0) {
6260 if (swift_version
== 1)
6261 outs() << " Swift 1.0";
6262 else if (swift_version
== 2)
6263 outs() << " Swift 1.1";
6264 else if(swift_version
== 3)
6265 outs() << " Swift 2.0";
6266 else if(swift_version
== 4)
6267 outs() << " Swift 3.0";
6268 else if(swift_version
== 5)
6269 outs() << " Swift 4.0";
6270 else if(swift_version
== 6)
6271 outs() << " Swift 4.1/Swift 4.2";
6272 else if(swift_version
== 7)
6273 outs() << " Swift 5 or later";
6275 outs() << " unknown future Swift version (" << swift_version
<< ")";
6280 static void print_image_info(SectionRef S
, struct DisassembleInfo
*info
) {
6281 uint32_t left
, offset
, p
;
6282 struct imageInfo_t o
;
6286 Expected
<StringRef
> SecNameOrErr
= S
.getName();
6288 SectName
= *SecNameOrErr
;
6290 consumeError(SecNameOrErr
.takeError());
6292 DataRefImpl Ref
= S
.getRawDataRefImpl();
6293 StringRef SegName
= info
->O
->getSectionFinalSegmentName(Ref
);
6294 outs() << "Contents of (" << SegName
<< "," << SectName
<< ") section\n";
6296 r
= get_pointer_32(p
, offset
, left
, S
, info
);
6299 memset(&o
, '\0', sizeof(struct imageInfo_t
));
6300 if (left
< sizeof(struct imageInfo_t
)) {
6301 memcpy(&o
, r
, left
);
6302 outs() << " (imageInfo entends past the end of the section)\n";
6304 memcpy(&o
, r
, sizeof(struct imageInfo_t
));
6305 if (info
->O
->isLittleEndian() != sys::IsLittleEndianHost
)
6307 outs() << " version " << o
.version
<< "\n";
6308 outs() << " flags " << format("0x%" PRIx32
, o
.flags
);
6314 outs() << " GC-only";
6320 static void printObjc2_64bit_MetaData(MachOObjectFile
*O
, bool verbose
) {
6321 SymbolAddressMap AddrMap
;
6323 CreateSymbolAddressMap(O
, &AddrMap
);
6325 std::vector
<SectionRef
> Sections
;
6326 append_range(Sections
, O
->sections());
6328 struct DisassembleInfo
info(O
, &AddrMap
, &Sections
, verbose
);
6330 SectionRef CL
= get_section(O
, "__OBJC2", "__class_list");
6331 if (CL
== SectionRef())
6332 CL
= get_section(O
, "__DATA", "__objc_classlist");
6333 if (CL
== SectionRef())
6334 CL
= get_section(O
, "__DATA_CONST", "__objc_classlist");
6335 if (CL
== SectionRef())
6336 CL
= get_section(O
, "__DATA_DIRTY", "__objc_classlist");
6338 walk_pointer_list_64("class", CL
, O
, &info
, print_class64_t
);
6340 SectionRef CR
= get_section(O
, "__OBJC2", "__class_refs");
6341 if (CR
== SectionRef())
6342 CR
= get_section(O
, "__DATA", "__objc_classrefs");
6343 if (CR
== SectionRef())
6344 CR
= get_section(O
, "__DATA_CONST", "__objc_classrefs");
6345 if (CR
== SectionRef())
6346 CR
= get_section(O
, "__DATA_DIRTY", "__objc_classrefs");
6348 walk_pointer_list_64("class refs", CR
, O
, &info
, nullptr);
6350 SectionRef SR
= get_section(O
, "__OBJC2", "__super_refs");
6351 if (SR
== SectionRef())
6352 SR
= get_section(O
, "__DATA", "__objc_superrefs");
6353 if (SR
== SectionRef())
6354 SR
= get_section(O
, "__DATA_CONST", "__objc_superrefs");
6355 if (SR
== SectionRef())
6356 SR
= get_section(O
, "__DATA_DIRTY", "__objc_superrefs");
6358 walk_pointer_list_64("super refs", SR
, O
, &info
, nullptr);
6360 SectionRef CA
= get_section(O
, "__OBJC2", "__category_list");
6361 if (CA
== SectionRef())
6362 CA
= get_section(O
, "__DATA", "__objc_catlist");
6363 if (CA
== SectionRef())
6364 CA
= get_section(O
, "__DATA_CONST", "__objc_catlist");
6365 if (CA
== SectionRef())
6366 CA
= get_section(O
, "__DATA_DIRTY", "__objc_catlist");
6368 walk_pointer_list_64("category", CA
, O
, &info
, print_category64_t
);
6370 SectionRef PL
= get_section(O
, "__OBJC2", "__protocol_list");
6371 if (PL
== SectionRef())
6372 PL
= get_section(O
, "__DATA", "__objc_protolist");
6373 if (PL
== SectionRef())
6374 PL
= get_section(O
, "__DATA_CONST", "__objc_protolist");
6375 if (PL
== SectionRef())
6376 PL
= get_section(O
, "__DATA_DIRTY", "__objc_protolist");
6378 walk_pointer_list_64("protocol", PL
, O
, &info
, nullptr);
6380 SectionRef MR
= get_section(O
, "__OBJC2", "__message_refs");
6381 if (MR
== SectionRef())
6382 MR
= get_section(O
, "__DATA", "__objc_msgrefs");
6383 if (MR
== SectionRef())
6384 MR
= get_section(O
, "__DATA_CONST", "__objc_msgrefs");
6385 if (MR
== SectionRef())
6386 MR
= get_section(O
, "__DATA_DIRTY", "__objc_msgrefs");
6388 print_message_refs64(MR
, &info
);
6390 SectionRef II
= get_section(O
, "__OBJC2", "__image_info");
6391 if (II
== SectionRef())
6392 II
= get_section(O
, "__DATA", "__objc_imageinfo");
6393 if (II
== SectionRef())
6394 II
= get_section(O
, "__DATA_CONST", "__objc_imageinfo");
6395 if (II
== SectionRef())
6396 II
= get_section(O
, "__DATA_DIRTY", "__objc_imageinfo");
6398 print_image_info64(II
, &info
);
6401 static void printObjc2_32bit_MetaData(MachOObjectFile
*O
, bool verbose
) {
6402 SymbolAddressMap AddrMap
;
6404 CreateSymbolAddressMap(O
, &AddrMap
);
6406 std::vector
<SectionRef
> Sections
;
6407 append_range(Sections
, O
->sections());
6409 struct DisassembleInfo
info(O
, &AddrMap
, &Sections
, verbose
);
6411 SectionRef CL
= get_section(O
, "__OBJC2", "__class_list");
6412 if (CL
== SectionRef())
6413 CL
= get_section(O
, "__DATA", "__objc_classlist");
6414 if (CL
== SectionRef())
6415 CL
= get_section(O
, "__DATA_CONST", "__objc_classlist");
6416 if (CL
== SectionRef())
6417 CL
= get_section(O
, "__DATA_DIRTY", "__objc_classlist");
6419 walk_pointer_list_32("class", CL
, O
, &info
, print_class32_t
);
6421 SectionRef CR
= get_section(O
, "__OBJC2", "__class_refs");
6422 if (CR
== SectionRef())
6423 CR
= get_section(O
, "__DATA", "__objc_classrefs");
6424 if (CR
== SectionRef())
6425 CR
= get_section(O
, "__DATA_CONST", "__objc_classrefs");
6426 if (CR
== SectionRef())
6427 CR
= get_section(O
, "__DATA_DIRTY", "__objc_classrefs");
6429 walk_pointer_list_32("class refs", CR
, O
, &info
, nullptr);
6431 SectionRef SR
= get_section(O
, "__OBJC2", "__super_refs");
6432 if (SR
== SectionRef())
6433 SR
= get_section(O
, "__DATA", "__objc_superrefs");
6434 if (SR
== SectionRef())
6435 SR
= get_section(O
, "__DATA_CONST", "__objc_superrefs");
6436 if (SR
== SectionRef())
6437 SR
= get_section(O
, "__DATA_DIRTY", "__objc_superrefs");
6439 walk_pointer_list_32("super refs", SR
, O
, &info
, nullptr);
6441 SectionRef CA
= get_section(O
, "__OBJC2", "__category_list");
6442 if (CA
== SectionRef())
6443 CA
= get_section(O
, "__DATA", "__objc_catlist");
6444 if (CA
== SectionRef())
6445 CA
= get_section(O
, "__DATA_CONST", "__objc_catlist");
6446 if (CA
== SectionRef())
6447 CA
= get_section(O
, "__DATA_DIRTY", "__objc_catlist");
6449 walk_pointer_list_32("category", CA
, O
, &info
, print_category32_t
);
6451 SectionRef PL
= get_section(O
, "__OBJC2", "__protocol_list");
6452 if (PL
== SectionRef())
6453 PL
= get_section(O
, "__DATA", "__objc_protolist");
6454 if (PL
== SectionRef())
6455 PL
= get_section(O
, "__DATA_CONST", "__objc_protolist");
6456 if (PL
== SectionRef())
6457 PL
= get_section(O
, "__DATA_DIRTY", "__objc_protolist");
6459 walk_pointer_list_32("protocol", PL
, O
, &info
, nullptr);
6461 SectionRef MR
= get_section(O
, "__OBJC2", "__message_refs");
6462 if (MR
== SectionRef())
6463 MR
= get_section(O
, "__DATA", "__objc_msgrefs");
6464 if (MR
== SectionRef())
6465 MR
= get_section(O
, "__DATA_CONST", "__objc_msgrefs");
6466 if (MR
== SectionRef())
6467 MR
= get_section(O
, "__DATA_DIRTY", "__objc_msgrefs");
6469 print_message_refs32(MR
, &info
);
6471 SectionRef II
= get_section(O
, "__OBJC2", "__image_info");
6472 if (II
== SectionRef())
6473 II
= get_section(O
, "__DATA", "__objc_imageinfo");
6474 if (II
== SectionRef())
6475 II
= get_section(O
, "__DATA_CONST", "__objc_imageinfo");
6476 if (II
== SectionRef())
6477 II
= get_section(O
, "__DATA_DIRTY", "__objc_imageinfo");
6479 print_image_info32(II
, &info
);
6482 static bool printObjc1_32bit_MetaData(MachOObjectFile
*O
, bool verbose
) {
6483 uint32_t i
, j
, p
, offset
, xoffset
, left
, defs_left
, def
;
6484 const char *r
, *name
, *defs
;
6485 struct objc_module_t module
;
6487 struct objc_symtab_t symtab
;
6488 struct objc_class_t objc_class
;
6489 struct objc_category_t objc_category
;
6491 outs() << "Objective-C segment\n";
6492 S
= get_section(O
, "__OBJC", "__module_info");
6493 if (S
== SectionRef())
6496 SymbolAddressMap AddrMap
;
6498 CreateSymbolAddressMap(O
, &AddrMap
);
6500 std::vector
<SectionRef
> Sections
;
6501 append_range(Sections
, O
->sections());
6503 struct DisassembleInfo
info(O
, &AddrMap
, &Sections
, verbose
);
6505 for (i
= 0; i
< S
.getSize(); i
+= sizeof(struct objc_module_t
)) {
6506 p
= S
.getAddress() + i
;
6507 r
= get_pointer_32(p
, offset
, left
, S
, &info
, true);
6510 memset(&module
, '\0', sizeof(struct objc_module_t
));
6511 if (left
< sizeof(struct objc_module_t
)) {
6512 memcpy(&module
, r
, left
);
6513 outs() << " (module extends past end of __module_info section)\n";
6515 memcpy(&module
, r
, sizeof(struct objc_module_t
));
6516 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
6519 outs() << "Module " << format("0x%" PRIx32
, p
) << "\n";
6520 outs() << " version " << module
.version
<< "\n";
6521 outs() << " size " << module
.size
<< "\n";
6523 name
= get_pointer_32(module
.name
, xoffset
, left
, xS
, &info
, true);
6524 if (name
!= nullptr)
6525 outs() << format("%.*s", left
, name
);
6527 outs() << format("0x%08" PRIx32
, module
.name
)
6528 << "(not in an __OBJC section)";
6531 r
= get_pointer_32(module
.symtab
, xoffset
, left
, xS
, &info
, true);
6532 if (module
.symtab
== 0 || r
== nullptr) {
6533 outs() << " symtab " << format("0x%08" PRIx32
, module
.symtab
)
6534 << " (not in an __OBJC section)\n";
6537 outs() << " symtab " << format("0x%08" PRIx32
, module
.symtab
) << "\n";
6538 memset(&symtab
, '\0', sizeof(struct objc_symtab_t
));
6541 if (left
< sizeof(struct objc_symtab_t
)) {
6542 memcpy(&symtab
, r
, left
);
6543 outs() << "\tsymtab extends past end of an __OBJC section)\n";
6545 memcpy(&symtab
, r
, sizeof(struct objc_symtab_t
));
6546 if (left
> sizeof(struct objc_symtab_t
)) {
6547 defs_left
= left
- sizeof(struct objc_symtab_t
);
6548 defs
= r
+ sizeof(struct objc_symtab_t
);
6551 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
6554 outs() << "\tsel_ref_cnt " << symtab
.sel_ref_cnt
<< "\n";
6555 r
= get_pointer_32(symtab
.refs
, xoffset
, left
, xS
, &info
, true);
6556 outs() << "\trefs " << format("0x%08" PRIx32
, symtab
.refs
);
6558 outs() << " (not in an __OBJC section)";
6560 outs() << "\tcls_def_cnt " << symtab
.cls_def_cnt
<< "\n";
6561 outs() << "\tcat_def_cnt " << symtab
.cat_def_cnt
<< "\n";
6562 if (symtab
.cls_def_cnt
> 0)
6563 outs() << "\tClass Definitions\n";
6564 for (j
= 0; j
< symtab
.cls_def_cnt
; j
++) {
6565 if ((j
+ 1) * sizeof(uint32_t) > defs_left
) {
6566 outs() << "\t(remaining class defs entries entends past the end of the "
6570 memcpy(&def
, defs
+ j
* sizeof(uint32_t), sizeof(uint32_t));
6571 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
6572 sys::swapByteOrder(def
);
6574 r
= get_pointer_32(def
, xoffset
, left
, xS
, &info
, true);
6575 outs() << "\tdefs[" << j
<< "] " << format("0x%08" PRIx32
, def
);
6577 if (left
> sizeof(struct objc_class_t
)) {
6579 memcpy(&objc_class
, r
, sizeof(struct objc_class_t
));
6581 outs() << " (entends past the end of the section)\n";
6582 memset(&objc_class
, '\0', sizeof(struct objc_class_t
));
6583 memcpy(&objc_class
, r
, left
);
6585 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
6586 swapStruct(objc_class
);
6587 print_objc_class_t(&objc_class
, &info
);
6589 outs() << "(not in an __OBJC section)\n";
6592 if (CLS_GETINFO(&objc_class
, CLS_CLASS
)) {
6593 outs() << "\tMeta Class";
6594 r
= get_pointer_32(objc_class
.isa
, xoffset
, left
, xS
, &info
, true);
6596 if (left
> sizeof(struct objc_class_t
)) {
6598 memcpy(&objc_class
, r
, sizeof(struct objc_class_t
));
6600 outs() << " (entends past the end of the section)\n";
6601 memset(&objc_class
, '\0', sizeof(struct objc_class_t
));
6602 memcpy(&objc_class
, r
, left
);
6604 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
6605 swapStruct(objc_class
);
6606 print_objc_class_t(&objc_class
, &info
);
6608 outs() << "(not in an __OBJC section)\n";
6612 if (symtab
.cat_def_cnt
> 0)
6613 outs() << "\tCategory Definitions\n";
6614 for (j
= 0; j
< symtab
.cat_def_cnt
; j
++) {
6615 if ((j
+ symtab
.cls_def_cnt
+ 1) * sizeof(uint32_t) > defs_left
) {
6616 outs() << "\t(remaining category defs entries entends past the end of "
6617 << "the section)\n";
6620 memcpy(&def
, defs
+ (j
+ symtab
.cls_def_cnt
) * sizeof(uint32_t),
6622 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
6623 sys::swapByteOrder(def
);
6625 r
= get_pointer_32(def
, xoffset
, left
, xS
, &info
, true);
6626 outs() << "\tdefs[" << j
+ symtab
.cls_def_cnt
<< "] "
6627 << format("0x%08" PRIx32
, def
);
6629 if (left
> sizeof(struct objc_category_t
)) {
6631 memcpy(&objc_category
, r
, sizeof(struct objc_category_t
));
6633 outs() << " (entends past the end of the section)\n";
6634 memset(&objc_category
, '\0', sizeof(struct objc_category_t
));
6635 memcpy(&objc_category
, r
, left
);
6637 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
6638 swapStruct(objc_category
);
6639 print_objc_objc_category_t(&objc_category
, &info
);
6641 outs() << "(not in an __OBJC section)\n";
6645 const SectionRef II
= get_section(O
, "__OBJC", "__image_info");
6646 if (II
!= SectionRef())
6647 print_image_info(II
, &info
);
6652 static void DumpProtocolSection(MachOObjectFile
*O
, const char *sect
,
6653 uint32_t size
, uint32_t addr
) {
6654 SymbolAddressMap AddrMap
;
6655 CreateSymbolAddressMap(O
, &AddrMap
);
6657 std::vector
<SectionRef
> Sections
;
6658 append_range(Sections
, O
->sections());
6660 struct DisassembleInfo
info(O
, &AddrMap
, &Sections
, true);
6663 struct objc_protocol_t protocol
;
6664 uint32_t left
, paddr
;
6665 for (p
= sect
; p
< sect
+ size
; p
+= sizeof(struct objc_protocol_t
)) {
6666 memset(&protocol
, '\0', sizeof(struct objc_protocol_t
));
6667 left
= size
- (p
- sect
);
6668 if (left
< sizeof(struct objc_protocol_t
)) {
6669 outs() << "Protocol extends past end of __protocol section\n";
6670 memcpy(&protocol
, p
, left
);
6672 memcpy(&protocol
, p
, sizeof(struct objc_protocol_t
));
6673 if (O
->isLittleEndian() != sys::IsLittleEndianHost
)
6674 swapStruct(protocol
);
6675 paddr
= addr
+ (p
- sect
);
6676 outs() << "Protocol " << format("0x%" PRIx32
, paddr
);
6677 if (print_protocol(paddr
, 0, &info
))
6678 outs() << "(not in an __OBJC section)\n";
6682 static void printObjcMetaData(MachOObjectFile
*O
, bool verbose
) {
6684 printObjc2_64bit_MetaData(O
, verbose
);
6686 MachO::mach_header H
;
6688 if (H
.cputype
== MachO::CPU_TYPE_ARM
)
6689 printObjc2_32bit_MetaData(O
, verbose
);
6691 // This is the 32-bit non-arm cputype case. Which is normally
6692 // the first Objective-C ABI. But it may be the case of a
6693 // binary for the iOS simulator which is the second Objective-C
6694 // ABI. In that case printObjc1_32bit_MetaData() will determine that
6695 // and return false.
6696 if (!printObjc1_32bit_MetaData(O
, verbose
))
6697 printObjc2_32bit_MetaData(O
, verbose
);
6702 // GuessLiteralPointer returns a string which for the item in the Mach-O file
6703 // for the address passed in as ReferenceValue for printing as a comment with
6704 // the instruction and also returns the corresponding type of that item
6705 // indirectly through ReferenceType.
6707 // If ReferenceValue is an address of literal cstring then a pointer to the
6708 // cstring is returned and ReferenceType is set to
6709 // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
6711 // If ReferenceValue is an address of an Objective-C CFString, Selector ref or
6712 // Class ref that name is returned and the ReferenceType is set accordingly.
6714 // Lastly, literals which are Symbol address in a literal pool are looked for
6715 // and if found the symbol name is returned and ReferenceType is set to
6716 // LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
6718 // If there is no item in the Mach-O file for the address passed in as
6719 // ReferenceValue nullptr is returned and ReferenceType is unchanged.
6720 static const char *GuessLiteralPointer(uint64_t ReferenceValue
,
6721 uint64_t ReferencePC
,
6722 uint64_t *ReferenceType
,
6723 struct DisassembleInfo
*info
) {
6724 // First see if there is an external relocation entry at the ReferencePC.
6725 if (info
->O
->getHeader().filetype
== MachO::MH_OBJECT
) {
6726 uint64_t sect_addr
= info
->S
.getAddress();
6727 uint64_t sect_offset
= ReferencePC
- sect_addr
;
6728 bool reloc_found
= false;
6730 MachO::any_relocation_info RE
;
6731 bool isExtern
= false;
6733 for (const RelocationRef
&Reloc
: info
->S
.relocations()) {
6734 uint64_t RelocOffset
= Reloc
.getOffset();
6735 if (RelocOffset
== sect_offset
) {
6736 Rel
= Reloc
.getRawDataRefImpl();
6737 RE
= info
->O
->getRelocation(Rel
);
6738 if (info
->O
->isRelocationScattered(RE
))
6740 isExtern
= info
->O
->getPlainRelocationExternal(RE
);
6742 symbol_iterator RelocSym
= Reloc
.getSymbol();
6749 // If there is an external relocation entry for a symbol in a section
6750 // then used that symbol's value for the value of the reference.
6751 if (reloc_found
&& isExtern
) {
6752 if (info
->O
->getAnyRelocationPCRel(RE
)) {
6753 unsigned Type
= info
->O
->getAnyRelocationType(RE
);
6754 if (Type
== MachO::X86_64_RELOC_SIGNED
) {
6755 ReferenceValue
= cantFail(Symbol
.getValue());
6761 // Look for literals such as Objective-C CFStrings refs, Selector refs,
6762 // Message refs and Class refs.
6763 bool classref
, selref
, msgref
, cfstring
;
6764 uint64_t pointer_value
= GuessPointerPointer(ReferenceValue
, info
, classref
,
6765 selref
, msgref
, cfstring
);
6766 if (classref
&& pointer_value
== 0) {
6767 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
6768 // And the pointer_value in that section is typically zero as it will be
6769 // set by dyld as part of the "bind information".
6770 const char *name
= get_dyld_bind_info_symbolname(ReferenceValue
, info
);
6771 if (name
!= nullptr) {
6772 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref
;
6773 const char *class_name
= strrchr(name
, '$');
6774 if (class_name
!= nullptr && class_name
[1] == '_' &&
6775 class_name
[2] != '\0') {
6776 info
->class_name
= class_name
+ 2;
6783 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref
;
6785 get_objc2_64bit_class_name(pointer_value
, ReferenceValue
, info
);
6786 if (name
!= nullptr)
6787 info
->class_name
= name
;
6789 name
= "bad class ref";
6794 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref
;
6795 const char *name
= get_objc2_64bit_cfstring_name(ReferenceValue
, info
);
6799 if (selref
&& pointer_value
== 0)
6800 pointer_value
= get_objc2_64bit_selref(ReferenceValue
, info
);
6802 if (pointer_value
!= 0)
6803 ReferenceValue
= pointer_value
;
6805 const char *name
= GuessCstringPointer(ReferenceValue
, info
);
6807 if (pointer_value
!= 0 && selref
) {
6808 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref
;
6809 info
->selector_name
= name
;
6810 } else if (pointer_value
!= 0 && msgref
) {
6811 info
->class_name
= nullptr;
6812 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref
;
6813 info
->selector_name
= name
;
6815 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr
;
6819 // Lastly look for an indirect symbol with this ReferenceValue which is in
6820 // a literal pool. If found return that symbol name.
6821 name
= GuessIndirectSymbol(ReferenceValue
, info
);
6823 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr
;
6830 // SymbolizerSymbolLookUp is the symbol lookup function passed when creating
6831 // the Symbolizer. It looks up the ReferenceValue using the info passed via the
6832 // pointer to the struct DisassembleInfo that was passed when MCSymbolizer
6833 // is created and returns the symbol name that matches the ReferenceValue or
6834 // nullptr if none. The ReferenceType is passed in for the IN type of
6835 // reference the instruction is making from the values in defined in the header
6836 // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
6837 // Out type and the ReferenceName will also be set which is added as a comment
6838 // to the disassembled instruction.
6840 // If the symbol name is a C++ mangled name then the demangled name is
6841 // returned through ReferenceName and ReferenceType is set to
6842 // LLVMDisassembler_ReferenceType_DeMangled_Name .
6844 // When this is called to get a symbol name for a branch target then the
6845 // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
6846 // SymbolValue will be looked for in the indirect symbol table to determine if
6847 // it is an address for a symbol stub. If so then the symbol name for that
6848 // stub is returned indirectly through ReferenceName and then ReferenceType is
6849 // set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
6851 // When this is called with an value loaded via a PC relative load then
6852 // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
6853 // SymbolValue is checked to be an address of literal pointer, symbol pointer,
6854 // or an Objective-C meta data reference. If so the output ReferenceType is
6855 // set to correspond to that as well as setting the ReferenceName.
6856 static const char *SymbolizerSymbolLookUp(void *DisInfo
,
6857 uint64_t ReferenceValue
,
6858 uint64_t *ReferenceType
,
6859 uint64_t ReferencePC
,
6860 const char **ReferenceName
) {
6861 struct DisassembleInfo
*info
= (struct DisassembleInfo
*)DisInfo
;
6862 // If no verbose symbolic information is wanted then just return nullptr.
6863 if (!info
->verbose
) {
6864 *ReferenceName
= nullptr;
6865 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
6869 const char *SymbolName
= GuessSymbolName(ReferenceValue
, info
->AddrMap
);
6871 if (*ReferenceType
== LLVMDisassembler_ReferenceType_In_Branch
) {
6872 *ReferenceName
= GuessIndirectSymbol(ReferenceValue
, info
);
6873 if (*ReferenceName
!= nullptr) {
6874 method_reference(info
, ReferenceType
, ReferenceName
);
6875 if (*ReferenceType
!= LLVMDisassembler_ReferenceType_Out_Objc_Message
)
6876 *ReferenceType
= LLVMDisassembler_ReferenceType_Out_SymbolStub
;
6877 } else if (SymbolName
!= nullptr && strncmp(SymbolName
, "__Z", 3) == 0) {
6878 if (info
->demangled_name
!= nullptr)
6879 free(info
->demangled_name
);
6880 info
->demangled_name
= itaniumDemangle(SymbolName
+ 1);
6881 if (info
->demangled_name
!= nullptr) {
6882 *ReferenceName
= info
->demangled_name
;
6883 *ReferenceType
= LLVMDisassembler_ReferenceType_DeMangled_Name
;
6885 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
6887 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
6888 } else if (*ReferenceType
== LLVMDisassembler_ReferenceType_In_PCrel_Load
) {
6890 GuessLiteralPointer(ReferenceValue
, ReferencePC
, ReferenceType
, info
);
6892 method_reference(info
, ReferenceType
, ReferenceName
);
6894 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
6895 // If this is arm64 and the reference is an adrp instruction save the
6896 // instruction, passed in ReferenceValue and the address of the instruction
6897 // for use later if we see and add immediate instruction.
6898 } else if (info
->O
->getArch() == Triple::aarch64
&&
6899 *ReferenceType
== LLVMDisassembler_ReferenceType_In_ARM64_ADRP
) {
6900 info
->adrp_inst
= ReferenceValue
;
6901 info
->adrp_addr
= ReferencePC
;
6902 SymbolName
= nullptr;
6903 *ReferenceName
= nullptr;
6904 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
6905 // If this is arm64 and reference is an add immediate instruction and we
6907 // seen an adrp instruction just before it and the adrp's Xd register
6909 // this add's Xn register reconstruct the value being referenced and look to
6910 // see if it is a literal pointer. Note the add immediate instruction is
6911 // passed in ReferenceValue.
6912 } else if (info
->O
->getArch() == Triple::aarch64
&&
6913 *ReferenceType
== LLVMDisassembler_ReferenceType_In_ARM64_ADDXri
&&
6914 ReferencePC
- 4 == info
->adrp_addr
&&
6915 (info
->adrp_inst
& 0x9f000000) == 0x90000000 &&
6916 (info
->adrp_inst
& 0x1f) == ((ReferenceValue
>> 5) & 0x1f)) {
6917 uint32_t addxri_inst
;
6918 uint64_t adrp_imm
, addxri_imm
;
6921 ((info
->adrp_inst
& 0x00ffffe0) >> 3) | ((info
->adrp_inst
>> 29) & 0x3);
6922 if (info
->adrp_inst
& 0x0200000)
6923 adrp_imm
|= 0xfffffffffc000000LL
;
6925 addxri_inst
= ReferenceValue
;
6926 addxri_imm
= (addxri_inst
>> 10) & 0xfff;
6927 if (((addxri_inst
>> 22) & 0x3) == 1)
6930 ReferenceValue
= (info
->adrp_addr
& 0xfffffffffffff000LL
) +
6931 (adrp_imm
<< 12) + addxri_imm
;
6934 GuessLiteralPointer(ReferenceValue
, ReferencePC
, ReferenceType
, info
);
6935 if (*ReferenceName
== nullptr)
6936 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
6937 // If this is arm64 and the reference is a load register instruction and we
6938 // have seen an adrp instruction just before it and the adrp's Xd register
6939 // matches this add's Xn register reconstruct the value being referenced and
6940 // look to see if it is a literal pointer. Note the load register
6941 // instruction is passed in ReferenceValue.
6942 } else if (info
->O
->getArch() == Triple::aarch64
&&
6943 *ReferenceType
== LLVMDisassembler_ReferenceType_In_ARM64_LDRXui
&&
6944 ReferencePC
- 4 == info
->adrp_addr
&&
6945 (info
->adrp_inst
& 0x9f000000) == 0x90000000 &&
6946 (info
->adrp_inst
& 0x1f) == ((ReferenceValue
>> 5) & 0x1f)) {
6947 uint32_t ldrxui_inst
;
6948 uint64_t adrp_imm
, ldrxui_imm
;
6951 ((info
->adrp_inst
& 0x00ffffe0) >> 3) | ((info
->adrp_inst
>> 29) & 0x3);
6952 if (info
->adrp_inst
& 0x0200000)
6953 adrp_imm
|= 0xfffffffffc000000LL
;
6955 ldrxui_inst
= ReferenceValue
;
6956 ldrxui_imm
= (ldrxui_inst
>> 10) & 0xfff;
6958 ReferenceValue
= (info
->adrp_addr
& 0xfffffffffffff000LL
) +
6959 (adrp_imm
<< 12) + (ldrxui_imm
<< 3);
6962 GuessLiteralPointer(ReferenceValue
, ReferencePC
, ReferenceType
, info
);
6963 if (*ReferenceName
== nullptr)
6964 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
6966 // If this arm64 and is an load register (PC-relative) instruction the
6967 // ReferenceValue is the PC plus the immediate value.
6968 else if (info
->O
->getArch() == Triple::aarch64
&&
6969 (*ReferenceType
== LLVMDisassembler_ReferenceType_In_ARM64_LDRXl
||
6970 *ReferenceType
== LLVMDisassembler_ReferenceType_In_ARM64_ADR
)) {
6972 GuessLiteralPointer(ReferenceValue
, ReferencePC
, ReferenceType
, info
);
6973 if (*ReferenceName
== nullptr)
6974 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
6975 } else if (SymbolName
!= nullptr && strncmp(SymbolName
, "__Z", 3) == 0) {
6976 if (info
->demangled_name
!= nullptr)
6977 free(info
->demangled_name
);
6978 info
->demangled_name
= itaniumDemangle(SymbolName
+ 1);
6979 if (info
->demangled_name
!= nullptr) {
6980 *ReferenceName
= info
->demangled_name
;
6981 *ReferenceType
= LLVMDisassembler_ReferenceType_DeMangled_Name
;
6985 *ReferenceName
= nullptr;
6986 *ReferenceType
= LLVMDisassembler_ReferenceType_InOut_None
;
6992 /// Emits the comments that are stored in the CommentStream.
6993 /// Each comment in the CommentStream must end with a newline.
6994 static void emitComments(raw_svector_ostream
&CommentStream
,
6995 SmallString
<128> &CommentsToEmit
,
6996 formatted_raw_ostream
&FormattedOS
,
6997 const MCAsmInfo
&MAI
) {
6998 // Flush the stream before taking its content.
6999 StringRef Comments
= CommentsToEmit
.str();
7000 // Get the default information for printing a comment.
7001 StringRef CommentBegin
= MAI
.getCommentString();
7002 unsigned CommentColumn
= MAI
.getCommentColumn();
7003 ListSeparator
LS("\n");
7004 while (!Comments
.empty()) {
7006 // Emit a line of comments.
7007 FormattedOS
.PadToColumn(CommentColumn
);
7008 size_t Position
= Comments
.find('\n');
7009 FormattedOS
<< CommentBegin
<< ' ' << Comments
.substr(0, Position
);
7010 // Move after the newline character.
7011 Comments
= Comments
.substr(Position
+ 1);
7013 FormattedOS
.flush();
7015 // Tell the comment stream that the vector changed underneath it.
7016 CommentsToEmit
.clear();
7019 const MachOObjectFile
*
7020 objdump::getMachODSymObject(const MachOObjectFile
*MachOOF
, StringRef Filename
,
7021 std::unique_ptr
<Binary
> &DSYMBinary
,
7022 std::unique_ptr
<MemoryBuffer
> &DSYMBuf
) {
7023 const MachOObjectFile
*DbgObj
= MachOOF
;
7024 std::string DSYMPath
;
7026 // Auto-detect w/o --dsym.
7027 if (DSYMFile
.empty()) {
7028 sys::fs::file_status DSYMStatus
;
7029 Twine FilenameDSYM
= Filename
+ ".dSYM";
7030 if (!status(FilenameDSYM
, DSYMStatus
)) {
7031 if (sys::fs::is_directory(DSYMStatus
)) {
7032 SmallString
<1024> Path
;
7033 FilenameDSYM
.toVector(Path
);
7034 sys::path::append(Path
, "Contents", "Resources", "DWARF",
7035 sys::path::filename(Filename
));
7036 DSYMPath
= std::string(Path
);
7037 } else if (sys::fs::is_regular_file(DSYMStatus
)) {
7038 DSYMPath
= FilenameDSYM
.str();
7043 if (DSYMPath
.empty() && !DSYMFile
.empty()) {
7044 // If DSYMPath is a .dSYM directory, append the Mach-O file.
7045 if (sys::fs::is_directory(DSYMFile
) &&
7046 sys::path::extension(DSYMFile
) == ".dSYM") {
7047 SmallString
<128> ShortName(sys::path::filename(DSYMFile
));
7048 sys::path::replace_extension(ShortName
, "");
7049 SmallString
<1024> FullPath(DSYMFile
);
7050 sys::path::append(FullPath
, "Contents", "Resources", "DWARF", ShortName
);
7051 DSYMPath
= FullPath
.str();
7053 DSYMPath
= DSYMFile
;
7057 if (!DSYMPath
.empty()) {
7059 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> BufOrErr
=
7060 MemoryBuffer::getFileOrSTDIN(DSYMPath
);
7061 if (std::error_code EC
= BufOrErr
.getError()) {
7062 reportError(errorCodeToError(EC
), DSYMPath
);
7066 // We need to keep the file alive, because we're replacing DbgObj with it.
7067 DSYMBuf
= std::move(BufOrErr
.get());
7069 Expected
<std::unique_ptr
<Binary
>> BinaryOrErr
=
7070 createBinary(DSYMBuf
.get()->getMemBufferRef());
7072 reportError(BinaryOrErr
.takeError(), DSYMPath
);
7076 // We need to keep the Binary alive with the buffer
7077 DSYMBinary
= std::move(BinaryOrErr
.get());
7078 if (ObjectFile
*O
= dyn_cast
<ObjectFile
>(DSYMBinary
.get())) {
7079 // this is a Mach-O object file, use it
7080 if (MachOObjectFile
*MachDSYM
= dyn_cast
<MachOObjectFile
>(&*O
)) {
7083 WithColor::error(errs(), "llvm-objdump")
7084 << DSYMPath
<< " is not a Mach-O file type.\n";
7087 } else if (auto *UB
= dyn_cast
<MachOUniversalBinary
>(DSYMBinary
.get())) {
7088 // this is a Universal Binary, find a Mach-O for this architecture
7089 uint32_t CPUType
, CPUSubType
;
7090 const char *ArchFlag
;
7091 if (MachOOF
->is64Bit()) {
7092 const MachO::mach_header_64 H_64
= MachOOF
->getHeader64();
7093 CPUType
= H_64
.cputype
;
7094 CPUSubType
= H_64
.cpusubtype
;
7096 const MachO::mach_header H
= MachOOF
->getHeader();
7097 CPUType
= H
.cputype
;
7098 CPUSubType
= H
.cpusubtype
;
7100 Triple T
= MachOObjectFile::getArchTriple(CPUType
, CPUSubType
, nullptr,
7102 Expected
<std::unique_ptr
<MachOObjectFile
>> MachDSYM
=
7103 UB
->getMachOObjectForArch(ArchFlag
);
7105 reportError(MachDSYM
.takeError(), DSYMPath
);
7109 // We need to keep the Binary alive with the buffer
7110 DbgObj
= &*MachDSYM
.get();
7111 DSYMBinary
= std::move(*MachDSYM
);
7113 WithColor::error(errs(), "llvm-objdump")
7114 << DSYMPath
<< " is not a Mach-O or Universal file type.\n";
7121 static void DisassembleMachO(StringRef Filename
, MachOObjectFile
*MachOOF
,
7122 StringRef DisSegName
, StringRef DisSectName
) {
7123 const char *McpuDefault
= nullptr;
7124 const Target
*ThumbTarget
= nullptr;
7125 const Target
*TheTarget
= GetTarget(MachOOF
, &McpuDefault
, &ThumbTarget
);
7127 // GetTarget prints out stuff.
7130 std::string MachOMCPU
;
7131 if (MCPU
.empty() && McpuDefault
)
7132 MachOMCPU
= McpuDefault
;
7136 #define CHECK_TARGET_INFO_CREATION(NAME) \
7139 WithColor::error(errs(), "llvm-objdump") \
7140 << "couldn't initialize disassembler for target " << TripleName \
7145 #define CHECK_THUMB_TARGET_INFO_CREATION(NAME) \
7148 WithColor::error(errs(), "llvm-objdump") \
7149 << "couldn't initialize disassembler for target " << ThumbTripleName \
7155 std::unique_ptr
<const MCInstrInfo
> InstrInfo(TheTarget
->createMCInstrInfo());
7156 CHECK_TARGET_INFO_CREATION(InstrInfo
);
7157 std::unique_ptr
<const MCInstrInfo
> ThumbInstrInfo
;
7159 ThumbInstrInfo
.reset(ThumbTarget
->createMCInstrInfo());
7160 CHECK_THUMB_TARGET_INFO_CREATION(ThumbInstrInfo
);
7163 // Package up features to be passed to target/subtarget
7164 std::string FeaturesStr
;
7165 if (!MAttrs
.empty()) {
7166 SubtargetFeatures Features
;
7167 for (unsigned i
= 0; i
!= MAttrs
.size(); ++i
)
7168 Features
.AddFeature(MAttrs
[i
]);
7169 FeaturesStr
= Features
.getString();
7172 MCTargetOptions MCOptions
;
7173 // Set up disassembler.
7174 std::unique_ptr
<const MCRegisterInfo
> MRI(
7175 TheTarget
->createMCRegInfo(TripleName
));
7176 CHECK_TARGET_INFO_CREATION(MRI
);
7177 std::unique_ptr
<const MCAsmInfo
> AsmInfo(
7178 TheTarget
->createMCAsmInfo(*MRI
, TripleName
, MCOptions
));
7179 CHECK_TARGET_INFO_CREATION(AsmInfo
);
7180 std::unique_ptr
<const MCSubtargetInfo
> STI(
7181 TheTarget
->createMCSubtargetInfo(TripleName
, MachOMCPU
, FeaturesStr
));
7182 CHECK_TARGET_INFO_CREATION(STI
);
7183 MCContext
Ctx(Triple(TripleName
), AsmInfo
.get(), MRI
.get(), STI
.get());
7184 std::unique_ptr
<MCDisassembler
> DisAsm(
7185 TheTarget
->createMCDisassembler(*STI
, Ctx
));
7186 CHECK_TARGET_INFO_CREATION(DisAsm
);
7187 std::unique_ptr
<MCSymbolizer
> Symbolizer
;
7188 struct DisassembleInfo
SymbolizerInfo(nullptr, nullptr, nullptr, false);
7189 std::unique_ptr
<MCRelocationInfo
> RelInfo(
7190 TheTarget
->createMCRelocationInfo(TripleName
, Ctx
));
7192 Symbolizer
.reset(TheTarget
->createMCSymbolizer(
7193 TripleName
, SymbolizerGetOpInfo
, SymbolizerSymbolLookUp
,
7194 &SymbolizerInfo
, &Ctx
, std::move(RelInfo
)));
7195 DisAsm
->setSymbolizer(std::move(Symbolizer
));
7197 int AsmPrinterVariant
= AsmInfo
->getAssemblerDialect();
7198 std::unique_ptr
<MCInstPrinter
> IP(TheTarget
->createMCInstPrinter(
7199 Triple(TripleName
), AsmPrinterVariant
, *AsmInfo
, *InstrInfo
, *MRI
));
7200 CHECK_TARGET_INFO_CREATION(IP
);
7201 // Set the display preference for hex vs. decimal immediates.
7202 IP
->setPrintImmHex(PrintImmHex
);
7203 // Comment stream and backing vector.
7204 SmallString
<128> CommentsToEmit
;
7205 raw_svector_ostream
CommentStream(CommentsToEmit
);
7206 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
7207 // if it is done then arm64 comments for string literals don't get printed
7208 // and some constant get printed instead and not setting it causes intel
7209 // (32-bit and 64-bit) comments printed with different spacing before the
7210 // comment causing different diffs with the 'C' disassembler library API.
7211 // IP->setCommentStream(CommentStream);
7213 // Set up separate thumb disassembler if needed.
7214 std::unique_ptr
<const MCRegisterInfo
> ThumbMRI
;
7215 std::unique_ptr
<const MCAsmInfo
> ThumbAsmInfo
;
7216 std::unique_ptr
<const MCSubtargetInfo
> ThumbSTI
;
7217 std::unique_ptr
<MCDisassembler
> ThumbDisAsm
;
7218 std::unique_ptr
<MCInstPrinter
> ThumbIP
;
7219 std::unique_ptr
<MCContext
> ThumbCtx
;
7220 std::unique_ptr
<MCSymbolizer
> ThumbSymbolizer
;
7221 struct DisassembleInfo
ThumbSymbolizerInfo(nullptr, nullptr, nullptr, false);
7222 std::unique_ptr
<MCRelocationInfo
> ThumbRelInfo
;
7224 ThumbMRI
.reset(ThumbTarget
->createMCRegInfo(ThumbTripleName
));
7225 CHECK_THUMB_TARGET_INFO_CREATION(ThumbMRI
);
7227 ThumbTarget
->createMCAsmInfo(*ThumbMRI
, ThumbTripleName
, MCOptions
));
7228 CHECK_THUMB_TARGET_INFO_CREATION(ThumbAsmInfo
);
7230 ThumbTarget
->createMCSubtargetInfo(ThumbTripleName
, MachOMCPU
,
7232 CHECK_THUMB_TARGET_INFO_CREATION(ThumbSTI
);
7233 ThumbCtx
.reset(new MCContext(Triple(ThumbTripleName
), ThumbAsmInfo
.get(),
7234 ThumbMRI
.get(), ThumbSTI
.get()));
7235 ThumbDisAsm
.reset(ThumbTarget
->createMCDisassembler(*ThumbSTI
, *ThumbCtx
));
7236 CHECK_THUMB_TARGET_INFO_CREATION(ThumbDisAsm
);
7237 MCContext
*PtrThumbCtx
= ThumbCtx
.get();
7239 ThumbTarget
->createMCRelocationInfo(ThumbTripleName
, *PtrThumbCtx
));
7241 ThumbSymbolizer
.reset(ThumbTarget
->createMCSymbolizer(
7242 ThumbTripleName
, SymbolizerGetOpInfo
, SymbolizerSymbolLookUp
,
7243 &ThumbSymbolizerInfo
, PtrThumbCtx
, std::move(ThumbRelInfo
)));
7244 ThumbDisAsm
->setSymbolizer(std::move(ThumbSymbolizer
));
7246 int ThumbAsmPrinterVariant
= ThumbAsmInfo
->getAssemblerDialect();
7247 ThumbIP
.reset(ThumbTarget
->createMCInstPrinter(
7248 Triple(ThumbTripleName
), ThumbAsmPrinterVariant
, *ThumbAsmInfo
,
7249 *ThumbInstrInfo
, *ThumbMRI
));
7250 CHECK_THUMB_TARGET_INFO_CREATION(ThumbIP
);
7251 // Set the display preference for hex vs. decimal immediates.
7252 ThumbIP
->setPrintImmHex(PrintImmHex
);
7255 #undef CHECK_TARGET_INFO_CREATION
7256 #undef CHECK_THUMB_TARGET_INFO_CREATION
7258 MachO::mach_header Header
= MachOOF
->getHeader();
7260 // FIXME: Using the -cfg command line option, this code used to be able to
7261 // annotate relocations with the referenced symbol's name, and if this was
7262 // inside a __[cf]string section, the data it points to. This is now replaced
7263 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
7264 std::vector
<SectionRef
> Sections
;
7265 std::vector
<SymbolRef
> Symbols
;
7266 SmallVector
<uint64_t, 8> FoundFns
;
7267 uint64_t BaseSegmentAddress
= 0;
7269 getSectionsAndSymbols(MachOOF
, Sections
, Symbols
, FoundFns
,
7270 BaseSegmentAddress
);
7272 // Sort the symbols by address, just in case they didn't come in that way.
7273 llvm::stable_sort(Symbols
, SymbolSorter());
7275 // Build a data in code table that is sorted on by the address of each entry.
7276 uint64_t BaseAddress
= 0;
7277 if (Header
.filetype
== MachO::MH_OBJECT
)
7278 BaseAddress
= Sections
[0].getAddress();
7280 BaseAddress
= BaseSegmentAddress
;
7282 for (dice_iterator DI
= MachOOF
->begin_dices(), DE
= MachOOF
->end_dices();
7285 DI
->getOffset(Offset
);
7286 Dices
.push_back(std::make_pair(BaseAddress
+ Offset
, *DI
));
7288 array_pod_sort(Dices
.begin(), Dices
.end());
7290 // Try to find debug info and set up the DIContext for it.
7291 std::unique_ptr
<DIContext
> diContext
;
7292 std::unique_ptr
<Binary
> DSYMBinary
;
7293 std::unique_ptr
<MemoryBuffer
> DSYMBuf
;
7295 // If separate DSym file path was specified, parse it as a macho file,
7296 // get the sections and supply it to the section name parsing machinery.
7297 if (const ObjectFile
*DbgObj
=
7298 getMachODSymObject(MachOOF
, Filename
, DSYMBinary
, DSYMBuf
)) {
7299 // Setup the DIContext
7300 diContext
= DWARFContext::create(*DbgObj
);
7306 if (FilterSections
.empty())
7307 outs() << "(" << DisSegName
<< "," << DisSectName
<< ") section\n";
7309 for (unsigned SectIdx
= 0; SectIdx
!= Sections
.size(); SectIdx
++) {
7310 Expected
<StringRef
> SecNameOrErr
= Sections
[SectIdx
].getName();
7311 if (!SecNameOrErr
) {
7312 consumeError(SecNameOrErr
.takeError());
7315 if (*SecNameOrErr
!= DisSectName
)
7318 DataRefImpl DR
= Sections
[SectIdx
].getRawDataRefImpl();
7320 StringRef SegmentName
= MachOOF
->getSectionFinalSegmentName(DR
);
7321 if (SegmentName
!= DisSegName
)
7324 StringRef BytesStr
=
7325 unwrapOrError(Sections
[SectIdx
].getContents(), Filename
);
7326 ArrayRef
<uint8_t> Bytes
= arrayRefFromStringRef(BytesStr
);
7327 uint64_t SectAddress
= Sections
[SectIdx
].getAddress();
7329 bool symbolTableWorked
= false;
7331 // Create a map of symbol addresses to symbol names for use by
7332 // the SymbolizerSymbolLookUp() routine.
7333 SymbolAddressMap AddrMap
;
7334 bool DisSymNameFound
= false;
7335 for (const SymbolRef
&Symbol
: MachOOF
->symbols()) {
7336 SymbolRef::Type ST
=
7337 unwrapOrError(Symbol
.getType(), MachOOF
->getFileName());
7338 if (ST
== SymbolRef::ST_Function
|| ST
== SymbolRef::ST_Data
||
7339 ST
== SymbolRef::ST_Other
) {
7340 uint64_t Address
= cantFail(Symbol
.getValue());
7342 unwrapOrError(Symbol
.getName(), MachOOF
->getFileName());
7343 AddrMap
[Address
] = SymName
;
7344 if (!DisSymName
.empty() && DisSymName
== SymName
)
7345 DisSymNameFound
= true;
7348 if (!DisSymName
.empty() && !DisSymNameFound
) {
7349 outs() << "Can't find -dis-symname: " << DisSymName
<< "\n";
7352 // Set up the block of info used by the Symbolizer call backs.
7353 SymbolizerInfo
.verbose
= SymbolicOperands
;
7354 SymbolizerInfo
.O
= MachOOF
;
7355 SymbolizerInfo
.S
= Sections
[SectIdx
];
7356 SymbolizerInfo
.AddrMap
= &AddrMap
;
7357 SymbolizerInfo
.Sections
= &Sections
;
7358 // Same for the ThumbSymbolizer
7359 ThumbSymbolizerInfo
.verbose
= SymbolicOperands
;
7360 ThumbSymbolizerInfo
.O
= MachOOF
;
7361 ThumbSymbolizerInfo
.S
= Sections
[SectIdx
];
7362 ThumbSymbolizerInfo
.AddrMap
= &AddrMap
;
7363 ThumbSymbolizerInfo
.Sections
= &Sections
;
7365 unsigned int Arch
= MachOOF
->getArch();
7367 // Skip all symbols if this is a stubs file.
7371 // If the section has symbols but no symbol at the start of the section
7372 // these are used to make sure the bytes before the first symbol are
7374 bool FirstSymbol
= true;
7375 bool FirstSymbolAtSectionStart
= true;
7377 // Disassemble symbol by symbol.
7378 for (unsigned SymIdx
= 0; SymIdx
!= Symbols
.size(); SymIdx
++) {
7380 unwrapOrError(Symbols
[SymIdx
].getName(), MachOOF
->getFileName());
7381 SymbolRef::Type ST
=
7382 unwrapOrError(Symbols
[SymIdx
].getType(), MachOOF
->getFileName());
7383 if (ST
!= SymbolRef::ST_Function
&& ST
!= SymbolRef::ST_Data
)
7386 // Make sure the symbol is defined in this section.
7387 bool containsSym
= Sections
[SectIdx
].containsSymbol(Symbols
[SymIdx
]);
7389 if (!DisSymName
.empty() && DisSymName
== SymName
) {
7390 outs() << "-dis-symname: " << DisSymName
<< " not in the section\n";
7395 // The __mh_execute_header is special and we need to deal with that fact
7396 // this symbol is before the start of the (__TEXT,__text) section and at the
7397 // address of the start of the __TEXT segment. This is because this symbol
7398 // is an N_SECT symbol in the (__TEXT,__text) but its address is before the
7399 // start of the section in a standard MH_EXECUTE filetype.
7400 if (!DisSymName
.empty() && DisSymName
== "__mh_execute_header") {
7401 outs() << "-dis-symname: __mh_execute_header not in any section\n";
7404 // When this code is trying to disassemble a symbol at a time and in the
7405 // case there is only the __mh_execute_header symbol left as in a stripped
7406 // executable, we need to deal with this by ignoring this symbol so the
7407 // whole section is disassembled and this symbol is then not displayed.
7408 if (SymName
== "__mh_execute_header" || SymName
== "__mh_dylib_header" ||
7409 SymName
== "__mh_bundle_header" || SymName
== "__mh_object_header" ||
7410 SymName
== "__mh_preload_header" || SymName
== "__mh_dylinker_header")
7413 // If we are only disassembling one symbol see if this is that symbol.
7414 if (!DisSymName
.empty() && DisSymName
!= SymName
)
7417 // Start at the address of the symbol relative to the section's address.
7418 uint64_t SectSize
= Sections
[SectIdx
].getSize();
7419 uint64_t Start
= cantFail(Symbols
[SymIdx
].getValue());
7420 uint64_t SectionAddress
= Sections
[SectIdx
].getAddress();
7421 Start
-= SectionAddress
;
7423 if (Start
> SectSize
) {
7424 outs() << "section data ends, " << SymName
7425 << " lies outside valid range\n";
7429 // Stop disassembling either at the beginning of the next symbol or at
7430 // the end of the section.
7431 bool containsNextSym
= false;
7432 uint64_t NextSym
= 0;
7433 uint64_t NextSymIdx
= SymIdx
+ 1;
7434 while (Symbols
.size() > NextSymIdx
) {
7435 SymbolRef::Type NextSymType
= unwrapOrError(
7436 Symbols
[NextSymIdx
].getType(), MachOOF
->getFileName());
7437 if (NextSymType
== SymbolRef::ST_Function
) {
7439 Sections
[SectIdx
].containsSymbol(Symbols
[NextSymIdx
]);
7440 NextSym
= cantFail(Symbols
[NextSymIdx
].getValue());
7441 NextSym
-= SectionAddress
;
7447 uint64_t End
= containsNextSym
? std::min(NextSym
, SectSize
) : SectSize
;
7450 symbolTableWorked
= true;
7452 DataRefImpl Symb
= Symbols
[SymIdx
].getRawDataRefImpl();
7453 uint32_t SymbolFlags
= cantFail(MachOOF
->getSymbolFlags(Symb
));
7454 bool IsThumb
= SymbolFlags
& SymbolRef::SF_Thumb
;
7456 // We only need the dedicated Thumb target if there's a real choice
7457 // (i.e. we're not targeting M-class) and the function is Thumb.
7458 bool UseThumbTarget
= IsThumb
&& ThumbTarget
;
7460 // If we are not specifying a symbol to start disassembly with and this
7461 // is the first symbol in the section but not at the start of the section
7462 // then move the disassembly index to the start of the section and
7463 // don't print the symbol name just yet. This is so the bytes before the
7464 // first symbol are disassembled.
7465 uint64_t SymbolStart
= Start
;
7466 if (DisSymName
.empty() && FirstSymbol
&& Start
!= 0) {
7467 FirstSymbolAtSectionStart
= false;
7471 outs() << SymName
<< ":\n";
7473 DILineInfo lastLine
;
7474 for (uint64_t Index
= Start
; Index
< End
; Index
+= Size
) {
7477 // If this is the first symbol in the section and it was not at the
7478 // start of the section, see if we are at its Index now and if so print
7480 if (FirstSymbol
&& !FirstSymbolAtSectionStart
&& Index
== SymbolStart
)
7481 outs() << SymName
<< ":\n";
7483 uint64_t PC
= SectAddress
+ Index
;
7485 if (FullLeadingAddr
) {
7486 if (MachOOF
->is64Bit())
7487 outs() << format("%016" PRIx64
, PC
);
7489 outs() << format("%08" PRIx64
, PC
);
7491 outs() << format("%8" PRIx64
":", PC
);
7494 if (ShowRawInsn
|| Arch
== Triple::arm
)
7497 if (DumpAndSkipDataInCode(PC
, Bytes
.data() + Index
, Dices
, Size
))
7500 SmallVector
<char, 64> AnnotationsBytes
;
7501 raw_svector_ostream
Annotations(AnnotationsBytes
);
7505 gotInst
= ThumbDisAsm
->getInstruction(Inst
, Size
, Bytes
.slice(Index
),
7508 gotInst
= DisAsm
->getInstruction(Inst
, Size
, Bytes
.slice(Index
), PC
,
7511 if (ShowRawInsn
|| Arch
== Triple::arm
) {
7512 dumpBytes(ArrayRef(Bytes
.data() + Index
, Size
), outs());
7514 formatted_raw_ostream
FormattedOS(outs());
7515 StringRef AnnotationsStr
= Annotations
.str();
7517 ThumbIP
->printInst(&Inst
, PC
, AnnotationsStr
, *ThumbSTI
,
7520 IP
->printInst(&Inst
, PC
, AnnotationsStr
, *STI
, FormattedOS
);
7521 emitComments(CommentStream
, CommentsToEmit
, FormattedOS
, *AsmInfo
);
7523 // Print debug info.
7525 DILineInfo dli
= diContext
->getLineInfoForAddress({PC
, SectIdx
});
7526 // Print valid line info if it changed.
7527 if (dli
!= lastLine
&& dli
.Line
!= 0)
7528 outs() << "\t## " << dli
.FileName
<< ':' << dli
.Line
<< ':'
7534 if (MachOOF
->getArchTriple().isX86()) {
7535 outs() << format("\t.byte 0x%02x #bad opcode\n",
7536 *(Bytes
.data() + Index
) & 0xff);
7537 Size
= 1; // skip exactly one illegible byte and move on.
7538 } else if (Arch
== Triple::aarch64
||
7539 (Arch
== Triple::arm
&& !IsThumb
)) {
7540 uint32_t opcode
= (*(Bytes
.data() + Index
) & 0xff) |
7541 (*(Bytes
.data() + Index
+ 1) & 0xff) << 8 |
7542 (*(Bytes
.data() + Index
+ 2) & 0xff) << 16 |
7543 (*(Bytes
.data() + Index
+ 3) & 0xff) << 24;
7544 outs() << format("\t.long\t0x%08x\n", opcode
);
7546 } else if (Arch
== Triple::arm
) {
7547 assert(IsThumb
&& "ARM mode should have been dealt with above");
7548 uint32_t opcode
= (*(Bytes
.data() + Index
) & 0xff) |
7549 (*(Bytes
.data() + Index
+ 1) & 0xff) << 8;
7550 outs() << format("\t.short\t0x%04x\n", opcode
);
7553 WithColor::warning(errs(), "llvm-objdump")
7554 << "invalid instruction encoding\n";
7556 Size
= 1; // skip illegible bytes
7560 // Now that we are done disassembled the first symbol set the bool that
7561 // were doing this to false.
7562 FirstSymbol
= false;
7564 if (!symbolTableWorked
) {
7565 // Reading the symbol table didn't work, disassemble the whole section.
7566 uint64_t SectAddress
= Sections
[SectIdx
].getAddress();
7567 uint64_t SectSize
= Sections
[SectIdx
].getSize();
7569 for (uint64_t Index
= 0; Index
< SectSize
; Index
+= InstSize
) {
7572 uint64_t PC
= SectAddress
+ Index
;
7574 if (DumpAndSkipDataInCode(PC
, Bytes
.data() + Index
, Dices
, InstSize
))
7577 SmallVector
<char, 64> AnnotationsBytes
;
7578 raw_svector_ostream
Annotations(AnnotationsBytes
);
7579 if (DisAsm
->getInstruction(Inst
, InstSize
, Bytes
.slice(Index
), PC
,
7582 if (FullLeadingAddr
) {
7583 if (MachOOF
->is64Bit())
7584 outs() << format("%016" PRIx64
, PC
);
7586 outs() << format("%08" PRIx64
, PC
);
7588 outs() << format("%8" PRIx64
":", PC
);
7591 if (ShowRawInsn
|| Arch
== Triple::arm
) {
7593 dumpBytes(ArrayRef(Bytes
.data() + Index
, InstSize
), outs());
7595 StringRef AnnotationsStr
= Annotations
.str();
7596 IP
->printInst(&Inst
, PC
, AnnotationsStr
, *STI
, outs());
7599 if (MachOOF
->getArchTriple().isX86()) {
7600 outs() << format("\t.byte 0x%02x #bad opcode\n",
7601 *(Bytes
.data() + Index
) & 0xff);
7602 InstSize
= 1; // skip exactly one illegible byte and move on.
7604 WithColor::warning(errs(), "llvm-objdump")
7605 << "invalid instruction encoding\n";
7607 InstSize
= 1; // skip illegible bytes
7612 // The TripleName's need to be reset if we are called again for a different
7615 ThumbTripleName
= "";
7617 if (SymbolizerInfo
.demangled_name
!= nullptr)
7618 free(SymbolizerInfo
.demangled_name
);
7619 if (ThumbSymbolizerInfo
.demangled_name
!= nullptr)
7620 free(ThumbSymbolizerInfo
.demangled_name
);
7624 //===----------------------------------------------------------------------===//
7625 // __compact_unwind section dumping
7626 //===----------------------------------------------------------------------===//
7630 template <typename T
>
7631 static uint64_t read(StringRef Contents
, ptrdiff_t Offset
) {
7632 if (Offset
+ sizeof(T
) > Contents
.size()) {
7633 outs() << "warning: attempt to read past end of buffer\n";
7637 uint64_t Val
= support::endian::read
<T
, llvm::endianness::little
>(
7638 Contents
.data() + Offset
);
7642 template <typename T
>
7643 static uint64_t readNext(StringRef Contents
, ptrdiff_t &Offset
) {
7644 T Val
= read
<T
>(Contents
, Offset
);
7645 Offset
+= sizeof(T
);
7649 struct CompactUnwindEntry
{
7650 uint32_t OffsetInSection
;
7652 uint64_t FunctionAddr
;
7654 uint32_t CompactEncoding
;
7655 uint64_t PersonalityAddr
;
7658 RelocationRef FunctionReloc
;
7659 RelocationRef PersonalityReloc
;
7660 RelocationRef LSDAReloc
;
7662 CompactUnwindEntry(StringRef Contents
, unsigned Offset
, bool Is64
)
7663 : OffsetInSection(Offset
) {
7665 read
<uint64_t>(Contents
, Offset
);
7667 read
<uint32_t>(Contents
, Offset
);
7671 template <typename UIntPtr
> void read(StringRef Contents
, ptrdiff_t Offset
) {
7672 FunctionAddr
= readNext
<UIntPtr
>(Contents
, Offset
);
7673 Length
= readNext
<uint32_t>(Contents
, Offset
);
7674 CompactEncoding
= readNext
<uint32_t>(Contents
, Offset
);
7675 PersonalityAddr
= readNext
<UIntPtr
>(Contents
, Offset
);
7676 LSDAAddr
= readNext
<UIntPtr
>(Contents
, Offset
);
7681 /// Given a relocation from __compact_unwind, consisting of the RelocationRef
7682 /// and data being relocated, determine the best base Name and Addend to use for
7683 /// display purposes.
7685 /// 1. An Extern relocation will directly reference a symbol (and the data is
7686 /// then already an addend), so use that.
7687 /// 2. Otherwise the data is an offset in the object file's layout; try to find
7688 // a symbol before it in the same section, and use the offset from there.
7689 /// 3. Finally, if all that fails, fall back to an offset from the start of the
7690 /// referenced section.
7691 static void findUnwindRelocNameAddend(const MachOObjectFile
*Obj
,
7692 std::map
<uint64_t, SymbolRef
> &Symbols
,
7693 const RelocationRef
&Reloc
, uint64_t Addr
,
7694 StringRef
&Name
, uint64_t &Addend
) {
7695 if (Reloc
.getSymbol() != Obj
->symbol_end()) {
7696 Name
= unwrapOrError(Reloc
.getSymbol()->getName(), Obj
->getFileName());
7701 auto RE
= Obj
->getRelocation(Reloc
.getRawDataRefImpl());
7702 SectionRef RelocSection
= Obj
->getAnyRelocationSection(RE
);
7704 uint64_t SectionAddr
= RelocSection
.getAddress();
7706 auto Sym
= Symbols
.upper_bound(Addr
);
7707 if (Sym
== Symbols
.begin()) {
7708 // The first symbol in the object is after this reference, the best we can
7709 // do is section-relative notation.
7710 if (Expected
<StringRef
> NameOrErr
= RelocSection
.getName())
7713 consumeError(NameOrErr
.takeError());
7715 Addend
= Addr
- SectionAddr
;
7719 // Go back one so that SymbolAddress <= Addr.
7722 section_iterator SymSection
=
7723 unwrapOrError(Sym
->second
.getSection(), Obj
->getFileName());
7724 if (RelocSection
== *SymSection
) {
7725 // There's a valid symbol in the same section before this reference.
7726 Name
= unwrapOrError(Sym
->second
.getName(), Obj
->getFileName());
7727 Addend
= Addr
- Sym
->first
;
7731 // There is a symbol before this reference, but it's in a different
7732 // section. Probably not helpful to mention it, so use the section name.
7733 if (Expected
<StringRef
> NameOrErr
= RelocSection
.getName())
7736 consumeError(NameOrErr
.takeError());
7738 Addend
= Addr
- SectionAddr
;
7741 static void printUnwindRelocDest(const MachOObjectFile
*Obj
,
7742 std::map
<uint64_t, SymbolRef
> &Symbols
,
7743 const RelocationRef
&Reloc
, uint64_t Addr
) {
7747 if (!Reloc
.getObject())
7750 findUnwindRelocNameAddend(Obj
, Symbols
, Reloc
, Addr
, Name
, Addend
);
7754 outs() << " + " << format("0x%" PRIx64
, Addend
);
7758 printMachOCompactUnwindSection(const MachOObjectFile
*Obj
,
7759 std::map
<uint64_t, SymbolRef
> &Symbols
,
7760 const SectionRef
&CompactUnwind
) {
7762 if (!Obj
->isLittleEndian()) {
7763 outs() << "Skipping big-endian __compact_unwind section\n";
7767 bool Is64
= Obj
->is64Bit();
7768 uint32_t PointerSize
= Is64
? sizeof(uint64_t) : sizeof(uint32_t);
7769 uint32_t EntrySize
= 3 * PointerSize
+ 2 * sizeof(uint32_t);
7771 StringRef Contents
=
7772 unwrapOrError(CompactUnwind
.getContents(), Obj
->getFileName());
7773 SmallVector
<CompactUnwindEntry
, 4> CompactUnwinds
;
7775 // First populate the initial raw offsets, encodings and so on from the entry.
7776 for (unsigned Offset
= 0; Offset
< Contents
.size(); Offset
+= EntrySize
) {
7777 CompactUnwindEntry
Entry(Contents
, Offset
, Is64
);
7778 CompactUnwinds
.push_back(Entry
);
7781 // Next we need to look at the relocations to find out what objects are
7782 // actually being referred to.
7783 for (const RelocationRef
&Reloc
: CompactUnwind
.relocations()) {
7784 uint64_t RelocAddress
= Reloc
.getOffset();
7786 uint32_t EntryIdx
= RelocAddress
/ EntrySize
;
7787 uint32_t OffsetInEntry
= RelocAddress
- EntryIdx
* EntrySize
;
7788 CompactUnwindEntry
&Entry
= CompactUnwinds
[EntryIdx
];
7790 if (OffsetInEntry
== 0)
7791 Entry
.FunctionReloc
= Reloc
;
7792 else if (OffsetInEntry
== PointerSize
+ 2 * sizeof(uint32_t))
7793 Entry
.PersonalityReloc
= Reloc
;
7794 else if (OffsetInEntry
== 2 * PointerSize
+ 2 * sizeof(uint32_t))
7795 Entry
.LSDAReloc
= Reloc
;
7797 outs() << "Invalid relocation in __compact_unwind section\n";
7802 // Finally, we're ready to print the data we've gathered.
7803 outs() << "Contents of __compact_unwind section:\n";
7804 for (auto &Entry
: CompactUnwinds
) {
7805 outs() << " Entry at offset "
7806 << format("0x%" PRIx32
, Entry
.OffsetInSection
) << ":\n";
7808 // 1. Start of the region this entry applies to.
7809 outs() << " start: " << format("0x%" PRIx64
,
7810 Entry
.FunctionAddr
) << ' ';
7811 printUnwindRelocDest(Obj
, Symbols
, Entry
.FunctionReloc
, Entry
.FunctionAddr
);
7814 // 2. Length of the region this entry applies to.
7815 outs() << " length: " << format("0x%" PRIx32
, Entry
.Length
)
7817 // 3. The 32-bit compact encoding.
7818 outs() << " compact encoding: "
7819 << format("0x%08" PRIx32
, Entry
.CompactEncoding
) << '\n';
7821 // 4. The personality function, if present.
7822 if (Entry
.PersonalityReloc
.getObject()) {
7823 outs() << " personality function: "
7824 << format("0x%" PRIx64
, Entry
.PersonalityAddr
) << ' ';
7825 printUnwindRelocDest(Obj
, Symbols
, Entry
.PersonalityReloc
,
7826 Entry
.PersonalityAddr
);
7830 // 5. This entry's language-specific data area.
7831 if (Entry
.LSDAReloc
.getObject()) {
7832 outs() << " LSDA: " << format("0x%" PRIx64
,
7833 Entry
.LSDAAddr
) << ' ';
7834 printUnwindRelocDest(Obj
, Symbols
, Entry
.LSDAReloc
, Entry
.LSDAAddr
);
7840 //===----------------------------------------------------------------------===//
7841 // __unwind_info section dumping
7842 //===----------------------------------------------------------------------===//
7844 static void printRegularSecondLevelUnwindPage(StringRef PageData
) {
7846 uint32_t Kind
= readNext
<uint32_t>(PageData
, Pos
);
7848 assert(Kind
== 2 && "kind for a regular 2nd level index should be 2");
7850 uint16_t EntriesStart
= readNext
<uint16_t>(PageData
, Pos
);
7851 uint16_t NumEntries
= readNext
<uint16_t>(PageData
, Pos
);
7854 for (unsigned i
= 0; i
< NumEntries
; ++i
) {
7855 uint32_t FunctionOffset
= readNext
<uint32_t>(PageData
, Pos
);
7856 uint32_t Encoding
= readNext
<uint32_t>(PageData
, Pos
);
7858 outs() << " [" << i
<< "]: "
7859 << "function offset=" << format("0x%08" PRIx32
, FunctionOffset
)
7861 << "encoding=" << format("0x%08" PRIx32
, Encoding
) << '\n';
7865 static void printCompressedSecondLevelUnwindPage(
7866 StringRef PageData
, uint32_t FunctionBase
,
7867 const SmallVectorImpl
<uint32_t> &CommonEncodings
) {
7869 uint32_t Kind
= readNext
<uint32_t>(PageData
, Pos
);
7871 assert(Kind
== 3 && "kind for a compressed 2nd level index should be 3");
7873 uint32_t NumCommonEncodings
= CommonEncodings
.size();
7874 uint16_t EntriesStart
= readNext
<uint16_t>(PageData
, Pos
);
7875 uint16_t NumEntries
= readNext
<uint16_t>(PageData
, Pos
);
7877 uint16_t PageEncodingsStart
= readNext
<uint16_t>(PageData
, Pos
);
7878 uint16_t NumPageEncodings
= readNext
<uint16_t>(PageData
, Pos
);
7879 SmallVector
<uint32_t, 64> PageEncodings
;
7880 if (NumPageEncodings
) {
7881 outs() << " Page encodings: (count = " << NumPageEncodings
<< ")\n";
7882 Pos
= PageEncodingsStart
;
7883 for (unsigned i
= 0; i
< NumPageEncodings
; ++i
) {
7884 uint32_t Encoding
= readNext
<uint32_t>(PageData
, Pos
);
7885 PageEncodings
.push_back(Encoding
);
7886 outs() << " encoding[" << (i
+ NumCommonEncodings
)
7887 << "]: " << format("0x%08" PRIx32
, Encoding
) << '\n';
7892 for (unsigned i
= 0; i
< NumEntries
; ++i
) {
7893 uint32_t Entry
= readNext
<uint32_t>(PageData
, Pos
);
7894 uint32_t FunctionOffset
= FunctionBase
+ (Entry
& 0xffffff);
7895 uint32_t EncodingIdx
= Entry
>> 24;
7898 if (EncodingIdx
< NumCommonEncodings
)
7899 Encoding
= CommonEncodings
[EncodingIdx
];
7901 Encoding
= PageEncodings
[EncodingIdx
- NumCommonEncodings
];
7903 outs() << " [" << i
<< "]: "
7904 << "function offset=" << format("0x%08" PRIx32
, FunctionOffset
)
7906 << "encoding[" << EncodingIdx
7907 << "]=" << format("0x%08" PRIx32
, Encoding
) << '\n';
7911 static void printMachOUnwindInfoSection(const MachOObjectFile
*Obj
,
7912 std::map
<uint64_t, SymbolRef
> &Symbols
,
7913 const SectionRef
&UnwindInfo
) {
7915 if (!Obj
->isLittleEndian()) {
7916 outs() << "Skipping big-endian __unwind_info section\n";
7920 outs() << "Contents of __unwind_info section:\n";
7922 StringRef Contents
=
7923 unwrapOrError(UnwindInfo
.getContents(), Obj
->getFileName());
7926 //===----------------------------------
7928 //===----------------------------------
7930 uint32_t Version
= readNext
<uint32_t>(Contents
, Pos
);
7931 outs() << " Version: "
7932 << format("0x%" PRIx32
, Version
) << '\n';
7934 outs() << " Skipping section with unknown version\n";
7938 uint32_t CommonEncodingsStart
= readNext
<uint32_t>(Contents
, Pos
);
7939 outs() << " Common encodings array section offset: "
7940 << format("0x%" PRIx32
, CommonEncodingsStart
) << '\n';
7941 uint32_t NumCommonEncodings
= readNext
<uint32_t>(Contents
, Pos
);
7942 outs() << " Number of common encodings in array: "
7943 << format("0x%" PRIx32
, NumCommonEncodings
) << '\n';
7945 uint32_t PersonalitiesStart
= readNext
<uint32_t>(Contents
, Pos
);
7946 outs() << " Personality function array section offset: "
7947 << format("0x%" PRIx32
, PersonalitiesStart
) << '\n';
7948 uint32_t NumPersonalities
= readNext
<uint32_t>(Contents
, Pos
);
7949 outs() << " Number of personality functions in array: "
7950 << format("0x%" PRIx32
, NumPersonalities
) << '\n';
7952 uint32_t IndicesStart
= readNext
<uint32_t>(Contents
, Pos
);
7953 outs() << " Index array section offset: "
7954 << format("0x%" PRIx32
, IndicesStart
) << '\n';
7955 uint32_t NumIndices
= readNext
<uint32_t>(Contents
, Pos
);
7956 outs() << " Number of indices in array: "
7957 << format("0x%" PRIx32
, NumIndices
) << '\n';
7959 //===----------------------------------
7960 // A shared list of common encodings
7961 //===----------------------------------
7963 // These occupy indices in the range [0, N] whenever an encoding is referenced
7964 // from a compressed 2nd level index table. In practice the linker only
7965 // creates ~128 of these, so that indices are available to embed encodings in
7966 // the 2nd level index.
7968 SmallVector
<uint32_t, 64> CommonEncodings
;
7969 outs() << " Common encodings: (count = " << NumCommonEncodings
<< ")\n";
7970 Pos
= CommonEncodingsStart
;
7971 for (unsigned i
= 0; i
< NumCommonEncodings
; ++i
) {
7972 uint32_t Encoding
= readNext
<uint32_t>(Contents
, Pos
);
7973 CommonEncodings
.push_back(Encoding
);
7975 outs() << " encoding[" << i
<< "]: " << format("0x%08" PRIx32
, Encoding
)
7979 //===----------------------------------
7980 // Personality functions used in this executable
7981 //===----------------------------------
7983 // There should be only a handful of these (one per source language,
7984 // roughly). Particularly since they only get 2 bits in the compact encoding.
7986 outs() << " Personality functions: (count = " << NumPersonalities
<< ")\n";
7987 Pos
= PersonalitiesStart
;
7988 for (unsigned i
= 0; i
< NumPersonalities
; ++i
) {
7989 uint32_t PersonalityFn
= readNext
<uint32_t>(Contents
, Pos
);
7990 outs() << " personality[" << i
+ 1
7991 << "]: " << format("0x%08" PRIx32
, PersonalityFn
) << '\n';
7994 //===----------------------------------
7995 // The level 1 index entries
7996 //===----------------------------------
7998 // These specify an approximate place to start searching for the more detailed
7999 // information, sorted by PC.
8002 uint32_t FunctionOffset
;
8003 uint32_t SecondLevelPageStart
;
8007 SmallVector
<IndexEntry
, 4> IndexEntries
;
8009 outs() << " Top level indices: (count = " << NumIndices
<< ")\n";
8011 for (unsigned i
= 0; i
< NumIndices
; ++i
) {
8014 Entry
.FunctionOffset
= readNext
<uint32_t>(Contents
, Pos
);
8015 Entry
.SecondLevelPageStart
= readNext
<uint32_t>(Contents
, Pos
);
8016 Entry
.LSDAStart
= readNext
<uint32_t>(Contents
, Pos
);
8017 IndexEntries
.push_back(Entry
);
8019 outs() << " [" << i
<< "]: "
8020 << "function offset=" << format("0x%08" PRIx32
, Entry
.FunctionOffset
)
8022 << "2nd level page offset="
8023 << format("0x%08" PRIx32
, Entry
.SecondLevelPageStart
) << ", "
8024 << "LSDA offset=" << format("0x%08" PRIx32
, Entry
.LSDAStart
) << '\n';
8027 //===----------------------------------
8028 // Next come the LSDA tables
8029 //===----------------------------------
8031 // The LSDA layout is rather implicit: it's a contiguous array of entries from
8032 // the first top-level index's LSDAOffset to the last (sentinel).
8034 outs() << " LSDA descriptors:\n";
8035 Pos
= IndexEntries
[0].LSDAStart
;
8036 const uint32_t LSDASize
= 2 * sizeof(uint32_t);
8038 (IndexEntries
.back().LSDAStart
- IndexEntries
[0].LSDAStart
) / LSDASize
;
8040 for (int i
= 0; i
< NumLSDAs
; ++i
) {
8041 uint32_t FunctionOffset
= readNext
<uint32_t>(Contents
, Pos
);
8042 uint32_t LSDAOffset
= readNext
<uint32_t>(Contents
, Pos
);
8043 outs() << " [" << i
<< "]: "
8044 << "function offset=" << format("0x%08" PRIx32
, FunctionOffset
)
8046 << "LSDA offset=" << format("0x%08" PRIx32
, LSDAOffset
) << '\n';
8049 //===----------------------------------
8050 // Finally, the 2nd level indices
8051 //===----------------------------------
8053 // Generally these are 4K in size, and have 2 possible forms:
8054 // + Regular stores up to 511 entries with disparate encodings
8055 // + Compressed stores up to 1021 entries if few enough compact encoding
8057 outs() << " Second level indices:\n";
8058 for (unsigned i
= 0; i
< IndexEntries
.size() - 1; ++i
) {
8059 // The final sentinel top-level index has no associated 2nd level page
8060 if (IndexEntries
[i
].SecondLevelPageStart
== 0)
8063 outs() << " Second level index[" << i
<< "]: "
8064 << "offset in section="
8065 << format("0x%08" PRIx32
, IndexEntries
[i
].SecondLevelPageStart
)
8067 << "base function offset="
8068 << format("0x%08" PRIx32
, IndexEntries
[i
].FunctionOffset
) << '\n';
8070 Pos
= IndexEntries
[i
].SecondLevelPageStart
;
8071 if (Pos
+ sizeof(uint32_t) > Contents
.size()) {
8072 outs() << "warning: invalid offset for second level page: " << Pos
<< '\n';
8077 *reinterpret_cast<const support::ulittle32_t
*>(Contents
.data() + Pos
);
8079 printRegularSecondLevelUnwindPage(Contents
.substr(Pos
, 4096));
8081 printCompressedSecondLevelUnwindPage(Contents
.substr(Pos
, 4096),
8082 IndexEntries
[i
].FunctionOffset
,
8085 outs() << " Skipping 2nd level page with unknown kind " << Kind
8090 void objdump::printMachOUnwindInfo(const MachOObjectFile
*Obj
) {
8091 std::map
<uint64_t, SymbolRef
> Symbols
;
8092 for (const SymbolRef
&SymRef
: Obj
->symbols()) {
8093 // Discard any undefined or absolute symbols. They're not going to take part
8094 // in the convenience lookup for unwind info and just take up resources.
8095 auto SectOrErr
= SymRef
.getSection();
8097 // TODO: Actually report errors helpfully.
8098 consumeError(SectOrErr
.takeError());
8101 section_iterator Section
= *SectOrErr
;
8102 if (Section
== Obj
->section_end())
8105 uint64_t Addr
= cantFail(SymRef
.getValue());
8106 Symbols
.insert(std::make_pair(Addr
, SymRef
));
8109 for (const SectionRef
&Section
: Obj
->sections()) {
8111 if (Expected
<StringRef
> NameOrErr
= Section
.getName())
8112 SectName
= *NameOrErr
;
8114 consumeError(NameOrErr
.takeError());
8116 if (SectName
== "__compact_unwind")
8117 printMachOCompactUnwindSection(Obj
, Symbols
, Section
);
8118 else if (SectName
== "__unwind_info")
8119 printMachOUnwindInfoSection(Obj
, Symbols
, Section
);
8123 static void PrintMachHeader(uint32_t magic
, uint32_t cputype
,
8124 uint32_t cpusubtype
, uint32_t filetype
,
8125 uint32_t ncmds
, uint32_t sizeofcmds
, uint32_t flags
,
8127 outs() << "Mach header\n";
8128 outs() << " magic cputype cpusubtype caps filetype ncmds "
8129 "sizeofcmds flags\n";
8131 if (magic
== MachO::MH_MAGIC
)
8132 outs() << " MH_MAGIC";
8133 else if (magic
== MachO::MH_MAGIC_64
)
8134 outs() << "MH_MAGIC_64";
8136 outs() << format(" 0x%08" PRIx32
, magic
);
8138 case MachO::CPU_TYPE_I386
:
8140 switch (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) {
8141 case MachO::CPU_SUBTYPE_I386_ALL
:
8145 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
8149 case MachO::CPU_TYPE_X86_64
:
8150 outs() << " X86_64";
8151 switch (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) {
8152 case MachO::CPU_SUBTYPE_X86_64_ALL
:
8155 case MachO::CPU_SUBTYPE_X86_64_H
:
8156 outs() << " Haswell";
8159 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
8163 case MachO::CPU_TYPE_ARM
:
8165 switch (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) {
8166 case MachO::CPU_SUBTYPE_ARM_ALL
:
8169 case MachO::CPU_SUBTYPE_ARM_V4T
:
8172 case MachO::CPU_SUBTYPE_ARM_V5TEJ
:
8175 case MachO::CPU_SUBTYPE_ARM_XSCALE
:
8176 outs() << " XSCALE";
8178 case MachO::CPU_SUBTYPE_ARM_V6
:
8181 case MachO::CPU_SUBTYPE_ARM_V6M
:
8184 case MachO::CPU_SUBTYPE_ARM_V7
:
8187 case MachO::CPU_SUBTYPE_ARM_V7EM
:
8190 case MachO::CPU_SUBTYPE_ARM_V7K
:
8193 case MachO::CPU_SUBTYPE_ARM_V7M
:
8196 case MachO::CPU_SUBTYPE_ARM_V7S
:
8200 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
8204 case MachO::CPU_TYPE_ARM64
:
8206 switch (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) {
8207 case MachO::CPU_SUBTYPE_ARM64_ALL
:
8210 case MachO::CPU_SUBTYPE_ARM64_V8
:
8213 case MachO::CPU_SUBTYPE_ARM64E
:
8217 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
8221 case MachO::CPU_TYPE_ARM64_32
:
8222 outs() << " ARM64_32";
8223 switch (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) {
8224 case MachO::CPU_SUBTYPE_ARM64_32_V8
:
8228 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
8232 case MachO::CPU_TYPE_POWERPC
:
8234 switch (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) {
8235 case MachO::CPU_SUBTYPE_POWERPC_ALL
:
8239 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
8243 case MachO::CPU_TYPE_POWERPC64
:
8245 switch (cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
) {
8246 case MachO::CPU_SUBTYPE_POWERPC_ALL
:
8250 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
8255 outs() << format(" %7d", cputype
);
8256 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
8259 if ((cpusubtype
& MachO::CPU_SUBTYPE_MASK
) == MachO::CPU_SUBTYPE_LIB64
) {
8262 outs() << format(" 0x%02" PRIx32
,
8263 (cpusubtype
& MachO::CPU_SUBTYPE_MASK
) >> 24);
8266 case MachO::MH_OBJECT
:
8267 outs() << " OBJECT";
8269 case MachO::MH_EXECUTE
:
8270 outs() << " EXECUTE";
8272 case MachO::MH_FVMLIB
:
8273 outs() << " FVMLIB";
8275 case MachO::MH_CORE
:
8278 case MachO::MH_PRELOAD
:
8279 outs() << " PRELOAD";
8281 case MachO::MH_DYLIB
:
8284 case MachO::MH_DYLIB_STUB
:
8285 outs() << " DYLIB_STUB";
8287 case MachO::MH_DYLINKER
:
8288 outs() << " DYLINKER";
8290 case MachO::MH_BUNDLE
:
8291 outs() << " BUNDLE";
8293 case MachO::MH_DSYM
:
8296 case MachO::MH_KEXT_BUNDLE
:
8297 outs() << " KEXTBUNDLE";
8299 case MachO::MH_FILESET
:
8300 outs() << " FILESET";
8303 outs() << format(" %10u", filetype
);
8306 outs() << format(" %5u", ncmds
);
8307 outs() << format(" %10u", sizeofcmds
);
8309 if (f
& MachO::MH_NOUNDEFS
) {
8310 outs() << " NOUNDEFS";
8311 f
&= ~MachO::MH_NOUNDEFS
;
8313 if (f
& MachO::MH_INCRLINK
) {
8314 outs() << " INCRLINK";
8315 f
&= ~MachO::MH_INCRLINK
;
8317 if (f
& MachO::MH_DYLDLINK
) {
8318 outs() << " DYLDLINK";
8319 f
&= ~MachO::MH_DYLDLINK
;
8321 if (f
& MachO::MH_BINDATLOAD
) {
8322 outs() << " BINDATLOAD";
8323 f
&= ~MachO::MH_BINDATLOAD
;
8325 if (f
& MachO::MH_PREBOUND
) {
8326 outs() << " PREBOUND";
8327 f
&= ~MachO::MH_PREBOUND
;
8329 if (f
& MachO::MH_SPLIT_SEGS
) {
8330 outs() << " SPLIT_SEGS";
8331 f
&= ~MachO::MH_SPLIT_SEGS
;
8333 if (f
& MachO::MH_LAZY_INIT
) {
8334 outs() << " LAZY_INIT";
8335 f
&= ~MachO::MH_LAZY_INIT
;
8337 if (f
& MachO::MH_TWOLEVEL
) {
8338 outs() << " TWOLEVEL";
8339 f
&= ~MachO::MH_TWOLEVEL
;
8341 if (f
& MachO::MH_FORCE_FLAT
) {
8342 outs() << " FORCE_FLAT";
8343 f
&= ~MachO::MH_FORCE_FLAT
;
8345 if (f
& MachO::MH_NOMULTIDEFS
) {
8346 outs() << " NOMULTIDEFS";
8347 f
&= ~MachO::MH_NOMULTIDEFS
;
8349 if (f
& MachO::MH_NOFIXPREBINDING
) {
8350 outs() << " NOFIXPREBINDING";
8351 f
&= ~MachO::MH_NOFIXPREBINDING
;
8353 if (f
& MachO::MH_PREBINDABLE
) {
8354 outs() << " PREBINDABLE";
8355 f
&= ~MachO::MH_PREBINDABLE
;
8357 if (f
& MachO::MH_ALLMODSBOUND
) {
8358 outs() << " ALLMODSBOUND";
8359 f
&= ~MachO::MH_ALLMODSBOUND
;
8361 if (f
& MachO::MH_SUBSECTIONS_VIA_SYMBOLS
) {
8362 outs() << " SUBSECTIONS_VIA_SYMBOLS";
8363 f
&= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS
;
8365 if (f
& MachO::MH_CANONICAL
) {
8366 outs() << " CANONICAL";
8367 f
&= ~MachO::MH_CANONICAL
;
8369 if (f
& MachO::MH_WEAK_DEFINES
) {
8370 outs() << " WEAK_DEFINES";
8371 f
&= ~MachO::MH_WEAK_DEFINES
;
8373 if (f
& MachO::MH_BINDS_TO_WEAK
) {
8374 outs() << " BINDS_TO_WEAK";
8375 f
&= ~MachO::MH_BINDS_TO_WEAK
;
8377 if (f
& MachO::MH_ALLOW_STACK_EXECUTION
) {
8378 outs() << " ALLOW_STACK_EXECUTION";
8379 f
&= ~MachO::MH_ALLOW_STACK_EXECUTION
;
8381 if (f
& MachO::MH_DEAD_STRIPPABLE_DYLIB
) {
8382 outs() << " DEAD_STRIPPABLE_DYLIB";
8383 f
&= ~MachO::MH_DEAD_STRIPPABLE_DYLIB
;
8385 if (f
& MachO::MH_PIE
) {
8387 f
&= ~MachO::MH_PIE
;
8389 if (f
& MachO::MH_NO_REEXPORTED_DYLIBS
) {
8390 outs() << " NO_REEXPORTED_DYLIBS";
8391 f
&= ~MachO::MH_NO_REEXPORTED_DYLIBS
;
8393 if (f
& MachO::MH_HAS_TLV_DESCRIPTORS
) {
8394 outs() << " MH_HAS_TLV_DESCRIPTORS";
8395 f
&= ~MachO::MH_HAS_TLV_DESCRIPTORS
;
8397 if (f
& MachO::MH_NO_HEAP_EXECUTION
) {
8398 outs() << " MH_NO_HEAP_EXECUTION";
8399 f
&= ~MachO::MH_NO_HEAP_EXECUTION
;
8401 if (f
& MachO::MH_APP_EXTENSION_SAFE
) {
8402 outs() << " APP_EXTENSION_SAFE";
8403 f
&= ~MachO::MH_APP_EXTENSION_SAFE
;
8405 if (f
& MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO
) {
8406 outs() << " NLIST_OUTOFSYNC_WITH_DYLDINFO";
8407 f
&= ~MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO
;
8409 if (f
!= 0 || flags
== 0)
8410 outs() << format(" 0x%08" PRIx32
, f
);
8412 outs() << format(" 0x%08" PRIx32
, magic
);
8413 outs() << format(" %7d", cputype
);
8414 outs() << format(" %10d", cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
);
8415 outs() << format(" 0x%02" PRIx32
,
8416 (cpusubtype
& MachO::CPU_SUBTYPE_MASK
) >> 24);
8417 outs() << format(" %10u", filetype
);
8418 outs() << format(" %5u", ncmds
);
8419 outs() << format(" %10u", sizeofcmds
);
8420 outs() << format(" 0x%08" PRIx32
, flags
);
8425 static void PrintSegmentCommand(uint32_t cmd
, uint32_t cmdsize
,
8426 StringRef SegName
, uint64_t vmaddr
,
8427 uint64_t vmsize
, uint64_t fileoff
,
8428 uint64_t filesize
, uint32_t maxprot
,
8429 uint32_t initprot
, uint32_t nsects
,
8430 uint32_t flags
, uint32_t object_size
,
8432 uint64_t expected_cmdsize
;
8433 if (cmd
== MachO::LC_SEGMENT
) {
8434 outs() << " cmd LC_SEGMENT\n";
8435 expected_cmdsize
= nsects
;
8436 expected_cmdsize
*= sizeof(struct MachO::section
);
8437 expected_cmdsize
+= sizeof(struct MachO::segment_command
);
8439 outs() << " cmd LC_SEGMENT_64\n";
8440 expected_cmdsize
= nsects
;
8441 expected_cmdsize
*= sizeof(struct MachO::section_64
);
8442 expected_cmdsize
+= sizeof(struct MachO::segment_command_64
);
8444 outs() << " cmdsize " << cmdsize
;
8445 if (cmdsize
!= expected_cmdsize
)
8446 outs() << " Inconsistent size\n";
8449 outs() << " segname " << SegName
<< "\n";
8450 if (cmd
== MachO::LC_SEGMENT_64
) {
8451 outs() << " vmaddr " << format("0x%016" PRIx64
, vmaddr
) << "\n";
8452 outs() << " vmsize " << format("0x%016" PRIx64
, vmsize
) << "\n";
8454 outs() << " vmaddr " << format("0x%08" PRIx64
, vmaddr
) << "\n";
8455 outs() << " vmsize " << format("0x%08" PRIx64
, vmsize
) << "\n";
8457 outs() << " fileoff " << fileoff
;
8458 if (fileoff
> object_size
)
8459 outs() << " (past end of file)\n";
8462 outs() << " filesize " << filesize
;
8463 if (fileoff
+ filesize
> object_size
)
8464 outs() << " (past end of file)\n";
8469 ~(MachO::VM_PROT_READ
| MachO::VM_PROT_WRITE
|
8470 MachO::VM_PROT_EXECUTE
)) != 0)
8471 outs() << " maxprot ?" << format("0x%08" PRIx32
, maxprot
) << "\n";
8473 outs() << " maxprot ";
8474 outs() << ((maxprot
& MachO::VM_PROT_READ
) ? "r" : "-");
8475 outs() << ((maxprot
& MachO::VM_PROT_WRITE
) ? "w" : "-");
8476 outs() << ((maxprot
& MachO::VM_PROT_EXECUTE
) ? "x\n" : "-\n");
8479 ~(MachO::VM_PROT_READ
| MachO::VM_PROT_WRITE
|
8480 MachO::VM_PROT_EXECUTE
)) != 0)
8481 outs() << " initprot ?" << format("0x%08" PRIx32
, initprot
) << "\n";
8483 outs() << " initprot ";
8484 outs() << ((initprot
& MachO::VM_PROT_READ
) ? "r" : "-");
8485 outs() << ((initprot
& MachO::VM_PROT_WRITE
) ? "w" : "-");
8486 outs() << ((initprot
& MachO::VM_PROT_EXECUTE
) ? "x\n" : "-\n");
8489 outs() << " maxprot " << format("0x%08" PRIx32
, maxprot
) << "\n";
8490 outs() << " initprot " << format("0x%08" PRIx32
, initprot
) << "\n";
8492 outs() << " nsects " << nsects
<< "\n";
8496 outs() << " (none)\n";
8498 if (flags
& MachO::SG_HIGHVM
) {
8499 outs() << " HIGHVM";
8500 flags
&= ~MachO::SG_HIGHVM
;
8502 if (flags
& MachO::SG_FVMLIB
) {
8503 outs() << " FVMLIB";
8504 flags
&= ~MachO::SG_FVMLIB
;
8506 if (flags
& MachO::SG_NORELOC
) {
8507 outs() << " NORELOC";
8508 flags
&= ~MachO::SG_NORELOC
;
8510 if (flags
& MachO::SG_PROTECTED_VERSION_1
) {
8511 outs() << " PROTECTED_VERSION_1";
8512 flags
&= ~MachO::SG_PROTECTED_VERSION_1
;
8514 if (flags
& MachO::SG_READ_ONLY
) {
8515 // Apple's otool prints the SG_ prefix for this flag, but not for the
8517 outs() << " SG_READ_ONLY";
8518 flags
&= ~MachO::SG_READ_ONLY
;
8521 outs() << format(" 0x%08" PRIx32
, flags
) << " (unknown flags)\n";
8526 outs() << " flags " << format("0x%" PRIx32
, flags
) << "\n";
8530 static void PrintSection(const char *sectname
, const char *segname
,
8531 uint64_t addr
, uint64_t size
, uint32_t offset
,
8532 uint32_t align
, uint32_t reloff
, uint32_t nreloc
,
8533 uint32_t flags
, uint32_t reserved1
, uint32_t reserved2
,
8534 uint32_t cmd
, const char *sg_segname
,
8535 uint32_t filetype
, uint32_t object_size
,
8537 outs() << "Section\n";
8538 outs() << " sectname " << format("%.16s\n", sectname
);
8539 outs() << " segname " << format("%.16s", segname
);
8540 if (filetype
!= MachO::MH_OBJECT
&& strncmp(sg_segname
, segname
, 16) != 0)
8541 outs() << " (does not match segment)\n";
8544 if (cmd
== MachO::LC_SEGMENT_64
) {
8545 outs() << " addr " << format("0x%016" PRIx64
, addr
) << "\n";
8546 outs() << " size " << format("0x%016" PRIx64
, size
);
8548 outs() << " addr " << format("0x%08" PRIx64
, addr
) << "\n";
8549 outs() << " size " << format("0x%08" PRIx64
, size
);
8551 if ((flags
& MachO::S_ZEROFILL
) != 0 && offset
+ size
> object_size
)
8552 outs() << " (past end of file)\n";
8555 outs() << " offset " << offset
;
8556 if (offset
> object_size
)
8557 outs() << " (past end of file)\n";
8560 uint32_t align_shifted
= 1 << align
;
8561 outs() << " align 2^" << align
<< " (" << align_shifted
<< ")\n";
8562 outs() << " reloff " << reloff
;
8563 if (reloff
> object_size
)
8564 outs() << " (past end of file)\n";
8567 outs() << " nreloc " << nreloc
;
8568 if (reloff
+ nreloc
* sizeof(struct MachO::relocation_info
) > object_size
)
8569 outs() << " (past end of file)\n";
8572 uint32_t section_type
= flags
& MachO::SECTION_TYPE
;
8575 if (section_type
== MachO::S_REGULAR
)
8576 outs() << " S_REGULAR\n";
8577 else if (section_type
== MachO::S_ZEROFILL
)
8578 outs() << " S_ZEROFILL\n";
8579 else if (section_type
== MachO::S_CSTRING_LITERALS
)
8580 outs() << " S_CSTRING_LITERALS\n";
8581 else if (section_type
== MachO::S_4BYTE_LITERALS
)
8582 outs() << " S_4BYTE_LITERALS\n";
8583 else if (section_type
== MachO::S_8BYTE_LITERALS
)
8584 outs() << " S_8BYTE_LITERALS\n";
8585 else if (section_type
== MachO::S_16BYTE_LITERALS
)
8586 outs() << " S_16BYTE_LITERALS\n";
8587 else if (section_type
== MachO::S_LITERAL_POINTERS
)
8588 outs() << " S_LITERAL_POINTERS\n";
8589 else if (section_type
== MachO::S_NON_LAZY_SYMBOL_POINTERS
)
8590 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
8591 else if (section_type
== MachO::S_LAZY_SYMBOL_POINTERS
)
8592 outs() << " S_LAZY_SYMBOL_POINTERS\n";
8593 else if (section_type
== MachO::S_SYMBOL_STUBS
)
8594 outs() << " S_SYMBOL_STUBS\n";
8595 else if (section_type
== MachO::S_MOD_INIT_FUNC_POINTERS
)
8596 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
8597 else if (section_type
== MachO::S_MOD_TERM_FUNC_POINTERS
)
8598 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
8599 else if (section_type
== MachO::S_COALESCED
)
8600 outs() << " S_COALESCED\n";
8601 else if (section_type
== MachO::S_INTERPOSING
)
8602 outs() << " S_INTERPOSING\n";
8603 else if (section_type
== MachO::S_DTRACE_DOF
)
8604 outs() << " S_DTRACE_DOF\n";
8605 else if (section_type
== MachO::S_LAZY_DYLIB_SYMBOL_POINTERS
)
8606 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
8607 else if (section_type
== MachO::S_THREAD_LOCAL_REGULAR
)
8608 outs() << " S_THREAD_LOCAL_REGULAR\n";
8609 else if (section_type
== MachO::S_THREAD_LOCAL_ZEROFILL
)
8610 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
8611 else if (section_type
== MachO::S_THREAD_LOCAL_VARIABLES
)
8612 outs() << " S_THREAD_LOCAL_VARIABLES\n";
8613 else if (section_type
== MachO::S_THREAD_LOCAL_VARIABLE_POINTERS
)
8614 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
8615 else if (section_type
== MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
)
8616 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
8617 else if (section_type
== MachO::S_INIT_FUNC_OFFSETS
)
8618 outs() << " S_INIT_FUNC_OFFSETS\n";
8620 outs() << format("0x%08" PRIx32
, section_type
) << "\n";
8621 outs() << "attributes";
8622 uint32_t section_attributes
= flags
& MachO::SECTION_ATTRIBUTES
;
8623 if (section_attributes
& MachO::S_ATTR_PURE_INSTRUCTIONS
)
8624 outs() << " PURE_INSTRUCTIONS";
8625 if (section_attributes
& MachO::S_ATTR_NO_TOC
)
8626 outs() << " NO_TOC";
8627 if (section_attributes
& MachO::S_ATTR_STRIP_STATIC_SYMS
)
8628 outs() << " STRIP_STATIC_SYMS";
8629 if (section_attributes
& MachO::S_ATTR_NO_DEAD_STRIP
)
8630 outs() << " NO_DEAD_STRIP";
8631 if (section_attributes
& MachO::S_ATTR_LIVE_SUPPORT
)
8632 outs() << " LIVE_SUPPORT";
8633 if (section_attributes
& MachO::S_ATTR_SELF_MODIFYING_CODE
)
8634 outs() << " SELF_MODIFYING_CODE";
8635 if (section_attributes
& MachO::S_ATTR_DEBUG
)
8637 if (section_attributes
& MachO::S_ATTR_SOME_INSTRUCTIONS
)
8638 outs() << " SOME_INSTRUCTIONS";
8639 if (section_attributes
& MachO::S_ATTR_EXT_RELOC
)
8640 outs() << " EXT_RELOC";
8641 if (section_attributes
& MachO::S_ATTR_LOC_RELOC
)
8642 outs() << " LOC_RELOC";
8643 if (section_attributes
== 0)
8644 outs() << " (none)";
8647 outs() << " flags " << format("0x%08" PRIx32
, flags
) << "\n";
8648 outs() << " reserved1 " << reserved1
;
8649 if (section_type
== MachO::S_SYMBOL_STUBS
||
8650 section_type
== MachO::S_LAZY_SYMBOL_POINTERS
||
8651 section_type
== MachO::S_LAZY_DYLIB_SYMBOL_POINTERS
||
8652 section_type
== MachO::S_NON_LAZY_SYMBOL_POINTERS
||
8653 section_type
== MachO::S_THREAD_LOCAL_VARIABLE_POINTERS
)
8654 outs() << " (index into indirect symbol table)\n";
8657 outs() << " reserved2 " << reserved2
;
8658 if (section_type
== MachO::S_SYMBOL_STUBS
)
8659 outs() << " (size of stubs)\n";
8664 static void PrintSymtabLoadCommand(MachO::symtab_command st
, bool Is64Bit
,
8665 uint32_t object_size
) {
8666 outs() << " cmd LC_SYMTAB\n";
8667 outs() << " cmdsize " << st
.cmdsize
;
8668 if (st
.cmdsize
!= sizeof(struct MachO::symtab_command
))
8669 outs() << " Incorrect size\n";
8672 outs() << " symoff " << st
.symoff
;
8673 if (st
.symoff
> object_size
)
8674 outs() << " (past end of file)\n";
8677 outs() << " nsyms " << st
.nsyms
;
8680 big_size
= st
.nsyms
;
8681 big_size
*= sizeof(struct MachO::nlist_64
);
8682 big_size
+= st
.symoff
;
8683 if (big_size
> object_size
)
8684 outs() << " (past end of file)\n";
8688 big_size
= st
.nsyms
;
8689 big_size
*= sizeof(struct MachO::nlist
);
8690 big_size
+= st
.symoff
;
8691 if (big_size
> object_size
)
8692 outs() << " (past end of file)\n";
8696 outs() << " stroff " << st
.stroff
;
8697 if (st
.stroff
> object_size
)
8698 outs() << " (past end of file)\n";
8701 outs() << " strsize " << st
.strsize
;
8702 big_size
= st
.stroff
;
8703 big_size
+= st
.strsize
;
8704 if (big_size
> object_size
)
8705 outs() << " (past end of file)\n";
8710 static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst
,
8711 uint32_t nsyms
, uint32_t object_size
,
8713 outs() << " cmd LC_DYSYMTAB\n";
8714 outs() << " cmdsize " << dyst
.cmdsize
;
8715 if (dyst
.cmdsize
!= sizeof(struct MachO::dysymtab_command
))
8716 outs() << " Incorrect size\n";
8719 outs() << " ilocalsym " << dyst
.ilocalsym
;
8720 if (dyst
.ilocalsym
> nsyms
)
8721 outs() << " (greater than the number of symbols)\n";
8724 outs() << " nlocalsym " << dyst
.nlocalsym
;
8726 big_size
= dyst
.ilocalsym
;
8727 big_size
+= dyst
.nlocalsym
;
8728 if (big_size
> nsyms
)
8729 outs() << " (past the end of the symbol table)\n";
8732 outs() << " iextdefsym " << dyst
.iextdefsym
;
8733 if (dyst
.iextdefsym
> nsyms
)
8734 outs() << " (greater than the number of symbols)\n";
8737 outs() << " nextdefsym " << dyst
.nextdefsym
;
8738 big_size
= dyst
.iextdefsym
;
8739 big_size
+= dyst
.nextdefsym
;
8740 if (big_size
> nsyms
)
8741 outs() << " (past the end of the symbol table)\n";
8744 outs() << " iundefsym " << dyst
.iundefsym
;
8745 if (dyst
.iundefsym
> nsyms
)
8746 outs() << " (greater than the number of symbols)\n";
8749 outs() << " nundefsym " << dyst
.nundefsym
;
8750 big_size
= dyst
.iundefsym
;
8751 big_size
+= dyst
.nundefsym
;
8752 if (big_size
> nsyms
)
8753 outs() << " (past the end of the symbol table)\n";
8756 outs() << " tocoff " << dyst
.tocoff
;
8757 if (dyst
.tocoff
> object_size
)
8758 outs() << " (past end of file)\n";
8761 outs() << " ntoc " << dyst
.ntoc
;
8762 big_size
= dyst
.ntoc
;
8763 big_size
*= sizeof(struct MachO::dylib_table_of_contents
);
8764 big_size
+= dyst
.tocoff
;
8765 if (big_size
> object_size
)
8766 outs() << " (past end of file)\n";
8769 outs() << " modtaboff " << dyst
.modtaboff
;
8770 if (dyst
.modtaboff
> object_size
)
8771 outs() << " (past end of file)\n";
8774 outs() << " nmodtab " << dyst
.nmodtab
;
8777 modtabend
= dyst
.nmodtab
;
8778 modtabend
*= sizeof(struct MachO::dylib_module_64
);
8779 modtabend
+= dyst
.modtaboff
;
8781 modtabend
= dyst
.nmodtab
;
8782 modtabend
*= sizeof(struct MachO::dylib_module
);
8783 modtabend
+= dyst
.modtaboff
;
8785 if (modtabend
> object_size
)
8786 outs() << " (past end of file)\n";
8789 outs() << " extrefsymoff " << dyst
.extrefsymoff
;
8790 if (dyst
.extrefsymoff
> object_size
)
8791 outs() << " (past end of file)\n";
8794 outs() << " nextrefsyms " << dyst
.nextrefsyms
;
8795 big_size
= dyst
.nextrefsyms
;
8796 big_size
*= sizeof(struct MachO::dylib_reference
);
8797 big_size
+= dyst
.extrefsymoff
;
8798 if (big_size
> object_size
)
8799 outs() << " (past end of file)\n";
8802 outs() << " indirectsymoff " << dyst
.indirectsymoff
;
8803 if (dyst
.indirectsymoff
> object_size
)
8804 outs() << " (past end of file)\n";
8807 outs() << " nindirectsyms " << dyst
.nindirectsyms
;
8808 big_size
= dyst
.nindirectsyms
;
8809 big_size
*= sizeof(uint32_t);
8810 big_size
+= dyst
.indirectsymoff
;
8811 if (big_size
> object_size
)
8812 outs() << " (past end of file)\n";
8815 outs() << " extreloff " << dyst
.extreloff
;
8816 if (dyst
.extreloff
> object_size
)
8817 outs() << " (past end of file)\n";
8820 outs() << " nextrel " << dyst
.nextrel
;
8821 big_size
= dyst
.nextrel
;
8822 big_size
*= sizeof(struct MachO::relocation_info
);
8823 big_size
+= dyst
.extreloff
;
8824 if (big_size
> object_size
)
8825 outs() << " (past end of file)\n";
8828 outs() << " locreloff " << dyst
.locreloff
;
8829 if (dyst
.locreloff
> object_size
)
8830 outs() << " (past end of file)\n";
8833 outs() << " nlocrel " << dyst
.nlocrel
;
8834 big_size
= dyst
.nlocrel
;
8835 big_size
*= sizeof(struct MachO::relocation_info
);
8836 big_size
+= dyst
.locreloff
;
8837 if (big_size
> object_size
)
8838 outs() << " (past end of file)\n";
8843 static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc
,
8844 uint32_t object_size
) {
8845 if (dc
.cmd
== MachO::LC_DYLD_INFO
)
8846 outs() << " cmd LC_DYLD_INFO\n";
8848 outs() << " cmd LC_DYLD_INFO_ONLY\n";
8849 outs() << " cmdsize " << dc
.cmdsize
;
8850 if (dc
.cmdsize
!= sizeof(struct MachO::dyld_info_command
))
8851 outs() << " Incorrect size\n";
8854 outs() << " rebase_off " << dc
.rebase_off
;
8855 if (dc
.rebase_off
> object_size
)
8856 outs() << " (past end of file)\n";
8859 outs() << " rebase_size " << dc
.rebase_size
;
8861 big_size
= dc
.rebase_off
;
8862 big_size
+= dc
.rebase_size
;
8863 if (big_size
> object_size
)
8864 outs() << " (past end of file)\n";
8867 outs() << " bind_off " << dc
.bind_off
;
8868 if (dc
.bind_off
> object_size
)
8869 outs() << " (past end of file)\n";
8872 outs() << " bind_size " << dc
.bind_size
;
8873 big_size
= dc
.bind_off
;
8874 big_size
+= dc
.bind_size
;
8875 if (big_size
> object_size
)
8876 outs() << " (past end of file)\n";
8879 outs() << " weak_bind_off " << dc
.weak_bind_off
;
8880 if (dc
.weak_bind_off
> object_size
)
8881 outs() << " (past end of file)\n";
8884 outs() << " weak_bind_size " << dc
.weak_bind_size
;
8885 big_size
= dc
.weak_bind_off
;
8886 big_size
+= dc
.weak_bind_size
;
8887 if (big_size
> object_size
)
8888 outs() << " (past end of file)\n";
8891 outs() << " lazy_bind_off " << dc
.lazy_bind_off
;
8892 if (dc
.lazy_bind_off
> object_size
)
8893 outs() << " (past end of file)\n";
8896 outs() << " lazy_bind_size " << dc
.lazy_bind_size
;
8897 big_size
= dc
.lazy_bind_off
;
8898 big_size
+= dc
.lazy_bind_size
;
8899 if (big_size
> object_size
)
8900 outs() << " (past end of file)\n";
8903 outs() << " export_off " << dc
.export_off
;
8904 if (dc
.export_off
> object_size
)
8905 outs() << " (past end of file)\n";
8908 outs() << " export_size " << dc
.export_size
;
8909 big_size
= dc
.export_off
;
8910 big_size
+= dc
.export_size
;
8911 if (big_size
> object_size
)
8912 outs() << " (past end of file)\n";
8917 static void PrintDyldLoadCommand(MachO::dylinker_command dyld
,
8919 if (dyld
.cmd
== MachO::LC_ID_DYLINKER
)
8920 outs() << " cmd LC_ID_DYLINKER\n";
8921 else if (dyld
.cmd
== MachO::LC_LOAD_DYLINKER
)
8922 outs() << " cmd LC_LOAD_DYLINKER\n";
8923 else if (dyld
.cmd
== MachO::LC_DYLD_ENVIRONMENT
)
8924 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
8926 outs() << " cmd ?(" << dyld
.cmd
<< ")\n";
8927 outs() << " cmdsize " << dyld
.cmdsize
;
8928 if (dyld
.cmdsize
< sizeof(struct MachO::dylinker_command
))
8929 outs() << " Incorrect size\n";
8932 if (dyld
.name
>= dyld
.cmdsize
)
8933 outs() << " name ?(bad offset " << dyld
.name
<< ")\n";
8935 const char *P
= (const char *)(Ptr
) + dyld
.name
;
8936 outs() << " name " << P
<< " (offset " << dyld
.name
<< ")\n";
8940 static void PrintUuidLoadCommand(MachO::uuid_command uuid
) {
8941 outs() << " cmd LC_UUID\n";
8942 outs() << " cmdsize " << uuid
.cmdsize
;
8943 if (uuid
.cmdsize
!= sizeof(struct MachO::uuid_command
))
8944 outs() << " Incorrect size\n";
8948 for (int i
= 0; i
< 16; ++i
) {
8949 outs() << format("%02" PRIX32
, uuid
.uuid
[i
]);
8950 if (i
== 3 || i
== 5 || i
== 7 || i
== 9)
8956 static void PrintRpathLoadCommand(MachO::rpath_command rpath
, const char *Ptr
) {
8957 outs() << " cmd LC_RPATH\n";
8958 outs() << " cmdsize " << rpath
.cmdsize
;
8959 if (rpath
.cmdsize
< sizeof(struct MachO::rpath_command
))
8960 outs() << " Incorrect size\n";
8963 if (rpath
.path
>= rpath
.cmdsize
)
8964 outs() << " path ?(bad offset " << rpath
.path
<< ")\n";
8966 const char *P
= (const char *)(Ptr
) + rpath
.path
;
8967 outs() << " path " << P
<< " (offset " << rpath
.path
<< ")\n";
8971 static void PrintVersionMinLoadCommand(MachO::version_min_command vd
) {
8972 StringRef LoadCmdName
;
8974 case MachO::LC_VERSION_MIN_MACOSX
:
8975 LoadCmdName
= "LC_VERSION_MIN_MACOSX";
8977 case MachO::LC_VERSION_MIN_IPHONEOS
:
8978 LoadCmdName
= "LC_VERSION_MIN_IPHONEOS";
8980 case MachO::LC_VERSION_MIN_TVOS
:
8981 LoadCmdName
= "LC_VERSION_MIN_TVOS";
8983 case MachO::LC_VERSION_MIN_WATCHOS
:
8984 LoadCmdName
= "LC_VERSION_MIN_WATCHOS";
8987 llvm_unreachable("Unknown version min load command");
8990 outs() << " cmd " << LoadCmdName
<< '\n';
8991 outs() << " cmdsize " << vd
.cmdsize
;
8992 if (vd
.cmdsize
!= sizeof(struct MachO::version_min_command
))
8993 outs() << " Incorrect size\n";
8996 outs() << " version "
8997 << MachOObjectFile::getVersionMinMajor(vd
, false) << "."
8998 << MachOObjectFile::getVersionMinMinor(vd
, false);
8999 uint32_t Update
= MachOObjectFile::getVersionMinUpdate(vd
, false);
9001 outs() << "." << Update
;
9004 outs() << " sdk n/a";
9007 << MachOObjectFile::getVersionMinMajor(vd
, true) << "."
9008 << MachOObjectFile::getVersionMinMinor(vd
, true);
9010 Update
= MachOObjectFile::getVersionMinUpdate(vd
, true);
9012 outs() << "." << Update
;
9016 static void PrintNoteLoadCommand(MachO::note_command Nt
) {
9017 outs() << " cmd LC_NOTE\n";
9018 outs() << " cmdsize " << Nt
.cmdsize
;
9019 if (Nt
.cmdsize
!= sizeof(struct MachO::note_command
))
9020 outs() << " Incorrect size\n";
9023 const char *d
= Nt
.data_owner
;
9024 outs() << "data_owner " << format("%.16s\n", d
);
9025 outs() << " offset " << Nt
.offset
<< "\n";
9026 outs() << " size " << Nt
.size
<< "\n";
9029 static void PrintBuildToolVersion(MachO::build_tool_version bv
, bool verbose
) {
9032 outs() << MachOObjectFile::getBuildTool(bv
.tool
);
9036 outs() << " version " << MachOObjectFile::getVersionString(bv
.version
)
9040 static void PrintBuildVersionLoadCommand(const MachOObjectFile
*obj
,
9041 MachO::build_version_command bd
,
9043 outs() << " cmd LC_BUILD_VERSION\n";
9044 outs() << " cmdsize " << bd
.cmdsize
;
9046 sizeof(struct MachO::build_version_command
) +
9047 bd
.ntools
* sizeof(struct MachO::build_tool_version
))
9048 outs() << " Incorrect size\n";
9051 outs() << " platform ";
9053 outs() << MachOObjectFile::getBuildPlatform(bd
.platform
);
9055 outs() << bd
.platform
;
9058 outs() << " sdk " << MachOObjectFile::getVersionString(bd
.sdk
)
9061 outs() << " sdk n/a\n";
9062 outs() << " minos " << MachOObjectFile::getVersionString(bd
.minos
)
9064 outs() << " ntools " << bd
.ntools
<< "\n";
9065 for (unsigned i
= 0; i
< bd
.ntools
; ++i
) {
9066 MachO::build_tool_version bv
= obj
->getBuildToolVersion(i
);
9067 PrintBuildToolVersion(bv
, verbose
);
9071 static void PrintSourceVersionCommand(MachO::source_version_command sd
) {
9072 outs() << " cmd LC_SOURCE_VERSION\n";
9073 outs() << " cmdsize " << sd
.cmdsize
;
9074 if (sd
.cmdsize
!= sizeof(struct MachO::source_version_command
))
9075 outs() << " Incorrect size\n";
9078 uint64_t a
= (sd
.version
>> 40) & 0xffffff;
9079 uint64_t b
= (sd
.version
>> 30) & 0x3ff;
9080 uint64_t c
= (sd
.version
>> 20) & 0x3ff;
9081 uint64_t d
= (sd
.version
>> 10) & 0x3ff;
9082 uint64_t e
= sd
.version
& 0x3ff;
9083 outs() << " version " << a
<< "." << b
;
9085 outs() << "." << c
<< "." << d
<< "." << e
;
9087 outs() << "." << c
<< "." << d
;
9093 static void PrintEntryPointCommand(MachO::entry_point_command ep
) {
9094 outs() << " cmd LC_MAIN\n";
9095 outs() << " cmdsize " << ep
.cmdsize
;
9096 if (ep
.cmdsize
!= sizeof(struct MachO::entry_point_command
))
9097 outs() << " Incorrect size\n";
9100 outs() << " entryoff " << ep
.entryoff
<< "\n";
9101 outs() << " stacksize " << ep
.stacksize
<< "\n";
9104 static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec
,
9105 uint32_t object_size
) {
9106 outs() << " cmd LC_ENCRYPTION_INFO\n";
9107 outs() << " cmdsize " << ec
.cmdsize
;
9108 if (ec
.cmdsize
!= sizeof(struct MachO::encryption_info_command
))
9109 outs() << " Incorrect size\n";
9112 outs() << " cryptoff " << ec
.cryptoff
;
9113 if (ec
.cryptoff
> object_size
)
9114 outs() << " (past end of file)\n";
9117 outs() << " cryptsize " << ec
.cryptsize
;
9118 if (ec
.cryptsize
> object_size
)
9119 outs() << " (past end of file)\n";
9122 outs() << " cryptid " << ec
.cryptid
<< "\n";
9125 static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec
,
9126 uint32_t object_size
) {
9127 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
9128 outs() << " cmdsize " << ec
.cmdsize
;
9129 if (ec
.cmdsize
!= sizeof(struct MachO::encryption_info_command_64
))
9130 outs() << " Incorrect size\n";
9133 outs() << " cryptoff " << ec
.cryptoff
;
9134 if (ec
.cryptoff
> object_size
)
9135 outs() << " (past end of file)\n";
9138 outs() << " cryptsize " << ec
.cryptsize
;
9139 if (ec
.cryptsize
> object_size
)
9140 outs() << " (past end of file)\n";
9143 outs() << " cryptid " << ec
.cryptid
<< "\n";
9144 outs() << " pad " << ec
.pad
<< "\n";
9147 static void PrintLinkerOptionCommand(MachO::linker_option_command lo
,
9149 outs() << " cmd LC_LINKER_OPTION\n";
9150 outs() << " cmdsize " << lo
.cmdsize
;
9151 if (lo
.cmdsize
< sizeof(struct MachO::linker_option_command
))
9152 outs() << " Incorrect size\n";
9155 outs() << " count " << lo
.count
<< "\n";
9156 const char *string
= Ptr
+ sizeof(struct MachO::linker_option_command
);
9157 uint32_t left
= lo
.cmdsize
- sizeof(struct MachO::linker_option_command
);
9160 while (*string
== '\0' && left
> 0) {
9166 outs() << " string #" << i
<< " " << format("%.*s\n", left
, string
);
9167 uint32_t NullPos
= StringRef(string
, left
).find('\0');
9168 uint32_t len
= std::min(NullPos
, left
) + 1;
9174 outs() << " count " << lo
.count
<< " does not match number of strings "
9178 static void PrintSubFrameworkCommand(MachO::sub_framework_command sub
,
9180 outs() << " cmd LC_SUB_FRAMEWORK\n";
9181 outs() << " cmdsize " << sub
.cmdsize
;
9182 if (sub
.cmdsize
< sizeof(struct MachO::sub_framework_command
))
9183 outs() << " Incorrect size\n";
9186 if (sub
.umbrella
< sub
.cmdsize
) {
9187 const char *P
= Ptr
+ sub
.umbrella
;
9188 outs() << " umbrella " << P
<< " (offset " << sub
.umbrella
<< ")\n";
9190 outs() << " umbrella ?(bad offset " << sub
.umbrella
<< ")\n";
9194 static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub
,
9196 outs() << " cmd LC_SUB_UMBRELLA\n";
9197 outs() << " cmdsize " << sub
.cmdsize
;
9198 if (sub
.cmdsize
< sizeof(struct MachO::sub_umbrella_command
))
9199 outs() << " Incorrect size\n";
9202 if (sub
.sub_umbrella
< sub
.cmdsize
) {
9203 const char *P
= Ptr
+ sub
.sub_umbrella
;
9204 outs() << " sub_umbrella " << P
<< " (offset " << sub
.sub_umbrella
<< ")\n";
9206 outs() << " sub_umbrella ?(bad offset " << sub
.sub_umbrella
<< ")\n";
9210 static void PrintSubLibraryCommand(MachO::sub_library_command sub
,
9212 outs() << " cmd LC_SUB_LIBRARY\n";
9213 outs() << " cmdsize " << sub
.cmdsize
;
9214 if (sub
.cmdsize
< sizeof(struct MachO::sub_library_command
))
9215 outs() << " Incorrect size\n";
9218 if (sub
.sub_library
< sub
.cmdsize
) {
9219 const char *P
= Ptr
+ sub
.sub_library
;
9220 outs() << " sub_library " << P
<< " (offset " << sub
.sub_library
<< ")\n";
9222 outs() << " sub_library ?(bad offset " << sub
.sub_library
<< ")\n";
9226 static void PrintSubClientCommand(MachO::sub_client_command sub
,
9228 outs() << " cmd LC_SUB_CLIENT\n";
9229 outs() << " cmdsize " << sub
.cmdsize
;
9230 if (sub
.cmdsize
< sizeof(struct MachO::sub_client_command
))
9231 outs() << " Incorrect size\n";
9234 if (sub
.client
< sub
.cmdsize
) {
9235 const char *P
= Ptr
+ sub
.client
;
9236 outs() << " client " << P
<< " (offset " << sub
.client
<< ")\n";
9238 outs() << " client ?(bad offset " << sub
.client
<< ")\n";
9242 static void PrintRoutinesCommand(MachO::routines_command r
) {
9243 outs() << " cmd LC_ROUTINES\n";
9244 outs() << " cmdsize " << r
.cmdsize
;
9245 if (r
.cmdsize
!= sizeof(struct MachO::routines_command
))
9246 outs() << " Incorrect size\n";
9249 outs() << " init_address " << format("0x%08" PRIx32
, r
.init_address
) << "\n";
9250 outs() << " init_module " << r
.init_module
<< "\n";
9251 outs() << " reserved1 " << r
.reserved1
<< "\n";
9252 outs() << " reserved2 " << r
.reserved2
<< "\n";
9253 outs() << " reserved3 " << r
.reserved3
<< "\n";
9254 outs() << " reserved4 " << r
.reserved4
<< "\n";
9255 outs() << " reserved5 " << r
.reserved5
<< "\n";
9256 outs() << " reserved6 " << r
.reserved6
<< "\n";
9259 static void PrintRoutinesCommand64(MachO::routines_command_64 r
) {
9260 outs() << " cmd LC_ROUTINES_64\n";
9261 outs() << " cmdsize " << r
.cmdsize
;
9262 if (r
.cmdsize
!= sizeof(struct MachO::routines_command_64
))
9263 outs() << " Incorrect size\n";
9266 outs() << " init_address " << format("0x%016" PRIx64
, r
.init_address
) << "\n";
9267 outs() << " init_module " << r
.init_module
<< "\n";
9268 outs() << " reserved1 " << r
.reserved1
<< "\n";
9269 outs() << " reserved2 " << r
.reserved2
<< "\n";
9270 outs() << " reserved3 " << r
.reserved3
<< "\n";
9271 outs() << " reserved4 " << r
.reserved4
<< "\n";
9272 outs() << " reserved5 " << r
.reserved5
<< "\n";
9273 outs() << " reserved6 " << r
.reserved6
<< "\n";
9276 static void Print_x86_thread_state32_t(MachO::x86_thread_state32_t
&cpu32
) {
9277 outs() << "\t eax " << format("0x%08" PRIx32
, cpu32
.eax
);
9278 outs() << " ebx " << format("0x%08" PRIx32
, cpu32
.ebx
);
9279 outs() << " ecx " << format("0x%08" PRIx32
, cpu32
.ecx
);
9280 outs() << " edx " << format("0x%08" PRIx32
, cpu32
.edx
) << "\n";
9281 outs() << "\t edi " << format("0x%08" PRIx32
, cpu32
.edi
);
9282 outs() << " esi " << format("0x%08" PRIx32
, cpu32
.esi
);
9283 outs() << " ebp " << format("0x%08" PRIx32
, cpu32
.ebp
);
9284 outs() << " esp " << format("0x%08" PRIx32
, cpu32
.esp
) << "\n";
9285 outs() << "\t ss " << format("0x%08" PRIx32
, cpu32
.ss
);
9286 outs() << " eflags " << format("0x%08" PRIx32
, cpu32
.eflags
);
9287 outs() << " eip " << format("0x%08" PRIx32
, cpu32
.eip
);
9288 outs() << " cs " << format("0x%08" PRIx32
, cpu32
.cs
) << "\n";
9289 outs() << "\t ds " << format("0x%08" PRIx32
, cpu32
.ds
);
9290 outs() << " es " << format("0x%08" PRIx32
, cpu32
.es
);
9291 outs() << " fs " << format("0x%08" PRIx32
, cpu32
.fs
);
9292 outs() << " gs " << format("0x%08" PRIx32
, cpu32
.gs
) << "\n";
9295 static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t
&cpu64
) {
9296 outs() << " rax " << format("0x%016" PRIx64
, cpu64
.rax
);
9297 outs() << " rbx " << format("0x%016" PRIx64
, cpu64
.rbx
);
9298 outs() << " rcx " << format("0x%016" PRIx64
, cpu64
.rcx
) << "\n";
9299 outs() << " rdx " << format("0x%016" PRIx64
, cpu64
.rdx
);
9300 outs() << " rdi " << format("0x%016" PRIx64
, cpu64
.rdi
);
9301 outs() << " rsi " << format("0x%016" PRIx64
, cpu64
.rsi
) << "\n";
9302 outs() << " rbp " << format("0x%016" PRIx64
, cpu64
.rbp
);
9303 outs() << " rsp " << format("0x%016" PRIx64
, cpu64
.rsp
);
9304 outs() << " r8 " << format("0x%016" PRIx64
, cpu64
.r8
) << "\n";
9305 outs() << " r9 " << format("0x%016" PRIx64
, cpu64
.r9
);
9306 outs() << " r10 " << format("0x%016" PRIx64
, cpu64
.r10
);
9307 outs() << " r11 " << format("0x%016" PRIx64
, cpu64
.r11
) << "\n";
9308 outs() << " r12 " << format("0x%016" PRIx64
, cpu64
.r12
);
9309 outs() << " r13 " << format("0x%016" PRIx64
, cpu64
.r13
);
9310 outs() << " r14 " << format("0x%016" PRIx64
, cpu64
.r14
) << "\n";
9311 outs() << " r15 " << format("0x%016" PRIx64
, cpu64
.r15
);
9312 outs() << " rip " << format("0x%016" PRIx64
, cpu64
.rip
) << "\n";
9313 outs() << "rflags " << format("0x%016" PRIx64
, cpu64
.rflags
);
9314 outs() << " cs " << format("0x%016" PRIx64
, cpu64
.cs
);
9315 outs() << " fs " << format("0x%016" PRIx64
, cpu64
.fs
) << "\n";
9316 outs() << " gs " << format("0x%016" PRIx64
, cpu64
.gs
) << "\n";
9319 static void Print_mmst_reg(MachO::mmst_reg_t
&r
) {
9321 outs() << "\t mmst_reg ";
9322 for (f
= 0; f
< 10; f
++)
9323 outs() << format("%02" PRIx32
, (r
.mmst_reg
[f
] & 0xff)) << " ";
9325 outs() << "\t mmst_rsrv ";
9326 for (f
= 0; f
< 6; f
++)
9327 outs() << format("%02" PRIx32
, (r
.mmst_rsrv
[f
] & 0xff)) << " ";
9331 static void Print_xmm_reg(MachO::xmm_reg_t
&r
) {
9333 outs() << "\t xmm_reg ";
9334 for (f
= 0; f
< 16; f
++)
9335 outs() << format("%02" PRIx32
, (r
.xmm_reg
[f
] & 0xff)) << " ";
9339 static void Print_x86_float_state_t(MachO::x86_float_state64_t
&fpu
) {
9340 outs() << "\t fpu_reserved[0] " << fpu
.fpu_reserved
[0];
9341 outs() << " fpu_reserved[1] " << fpu
.fpu_reserved
[1] << "\n";
9342 outs() << "\t control: invalid " << fpu
.fpu_fcw
.invalid
;
9343 outs() << " denorm " << fpu
.fpu_fcw
.denorm
;
9344 outs() << " zdiv " << fpu
.fpu_fcw
.zdiv
;
9345 outs() << " ovrfl " << fpu
.fpu_fcw
.ovrfl
;
9346 outs() << " undfl " << fpu
.fpu_fcw
.undfl
;
9347 outs() << " precis " << fpu
.fpu_fcw
.precis
<< "\n";
9348 outs() << "\t\t pc ";
9349 if (fpu
.fpu_fcw
.pc
== MachO::x86_FP_PREC_24B
)
9350 outs() << "FP_PREC_24B ";
9351 else if (fpu
.fpu_fcw
.pc
== MachO::x86_FP_PREC_53B
)
9352 outs() << "FP_PREC_53B ";
9353 else if (fpu
.fpu_fcw
.pc
== MachO::x86_FP_PREC_64B
)
9354 outs() << "FP_PREC_64B ";
9356 outs() << fpu
.fpu_fcw
.pc
<< " ";
9358 if (fpu
.fpu_fcw
.rc
== MachO::x86_FP_RND_NEAR
)
9359 outs() << "FP_RND_NEAR ";
9360 else if (fpu
.fpu_fcw
.rc
== MachO::x86_FP_RND_DOWN
)
9361 outs() << "FP_RND_DOWN ";
9362 else if (fpu
.fpu_fcw
.rc
== MachO::x86_FP_RND_UP
)
9363 outs() << "FP_RND_UP ";
9364 else if (fpu
.fpu_fcw
.rc
== MachO::x86_FP_CHOP
)
9365 outs() << "FP_CHOP ";
9367 outs() << "\t status: invalid " << fpu
.fpu_fsw
.invalid
;
9368 outs() << " denorm " << fpu
.fpu_fsw
.denorm
;
9369 outs() << " zdiv " << fpu
.fpu_fsw
.zdiv
;
9370 outs() << " ovrfl " << fpu
.fpu_fsw
.ovrfl
;
9371 outs() << " undfl " << fpu
.fpu_fsw
.undfl
;
9372 outs() << " precis " << fpu
.fpu_fsw
.precis
;
9373 outs() << " stkflt " << fpu
.fpu_fsw
.stkflt
<< "\n";
9374 outs() << "\t errsumm " << fpu
.fpu_fsw
.errsumm
;
9375 outs() << " c0 " << fpu
.fpu_fsw
.c0
;
9376 outs() << " c1 " << fpu
.fpu_fsw
.c1
;
9377 outs() << " c2 " << fpu
.fpu_fsw
.c2
;
9378 outs() << " tos " << fpu
.fpu_fsw
.tos
;
9379 outs() << " c3 " << fpu
.fpu_fsw
.c3
;
9380 outs() << " busy " << fpu
.fpu_fsw
.busy
<< "\n";
9381 outs() << "\t fpu_ftw " << format("0x%02" PRIx32
, fpu
.fpu_ftw
);
9382 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32
, fpu
.fpu_rsrv1
);
9383 outs() << " fpu_fop " << format("0x%04" PRIx32
, fpu
.fpu_fop
);
9384 outs() << " fpu_ip " << format("0x%08" PRIx32
, fpu
.fpu_ip
) << "\n";
9385 outs() << "\t fpu_cs " << format("0x%04" PRIx32
, fpu
.fpu_cs
);
9386 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32
, fpu
.fpu_rsrv2
);
9387 outs() << " fpu_dp " << format("0x%08" PRIx32
, fpu
.fpu_dp
);
9388 outs() << " fpu_ds " << format("0x%04" PRIx32
, fpu
.fpu_ds
) << "\n";
9389 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32
, fpu
.fpu_rsrv3
);
9390 outs() << " fpu_mxcsr " << format("0x%08" PRIx32
, fpu
.fpu_mxcsr
);
9391 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32
, fpu
.fpu_mxcsrmask
);
9393 outs() << "\t fpu_stmm0:\n";
9394 Print_mmst_reg(fpu
.fpu_stmm0
);
9395 outs() << "\t fpu_stmm1:\n";
9396 Print_mmst_reg(fpu
.fpu_stmm1
);
9397 outs() << "\t fpu_stmm2:\n";
9398 Print_mmst_reg(fpu
.fpu_stmm2
);
9399 outs() << "\t fpu_stmm3:\n";
9400 Print_mmst_reg(fpu
.fpu_stmm3
);
9401 outs() << "\t fpu_stmm4:\n";
9402 Print_mmst_reg(fpu
.fpu_stmm4
);
9403 outs() << "\t fpu_stmm5:\n";
9404 Print_mmst_reg(fpu
.fpu_stmm5
);
9405 outs() << "\t fpu_stmm6:\n";
9406 Print_mmst_reg(fpu
.fpu_stmm6
);
9407 outs() << "\t fpu_stmm7:\n";
9408 Print_mmst_reg(fpu
.fpu_stmm7
);
9409 outs() << "\t fpu_xmm0:\n";
9410 Print_xmm_reg(fpu
.fpu_xmm0
);
9411 outs() << "\t fpu_xmm1:\n";
9412 Print_xmm_reg(fpu
.fpu_xmm1
);
9413 outs() << "\t fpu_xmm2:\n";
9414 Print_xmm_reg(fpu
.fpu_xmm2
);
9415 outs() << "\t fpu_xmm3:\n";
9416 Print_xmm_reg(fpu
.fpu_xmm3
);
9417 outs() << "\t fpu_xmm4:\n";
9418 Print_xmm_reg(fpu
.fpu_xmm4
);
9419 outs() << "\t fpu_xmm5:\n";
9420 Print_xmm_reg(fpu
.fpu_xmm5
);
9421 outs() << "\t fpu_xmm6:\n";
9422 Print_xmm_reg(fpu
.fpu_xmm6
);
9423 outs() << "\t fpu_xmm7:\n";
9424 Print_xmm_reg(fpu
.fpu_xmm7
);
9425 outs() << "\t fpu_xmm8:\n";
9426 Print_xmm_reg(fpu
.fpu_xmm8
);
9427 outs() << "\t fpu_xmm9:\n";
9428 Print_xmm_reg(fpu
.fpu_xmm9
);
9429 outs() << "\t fpu_xmm10:\n";
9430 Print_xmm_reg(fpu
.fpu_xmm10
);
9431 outs() << "\t fpu_xmm11:\n";
9432 Print_xmm_reg(fpu
.fpu_xmm11
);
9433 outs() << "\t fpu_xmm12:\n";
9434 Print_xmm_reg(fpu
.fpu_xmm12
);
9435 outs() << "\t fpu_xmm13:\n";
9436 Print_xmm_reg(fpu
.fpu_xmm13
);
9437 outs() << "\t fpu_xmm14:\n";
9438 Print_xmm_reg(fpu
.fpu_xmm14
);
9439 outs() << "\t fpu_xmm15:\n";
9440 Print_xmm_reg(fpu
.fpu_xmm15
);
9441 outs() << "\t fpu_rsrv4:\n";
9442 for (uint32_t f
= 0; f
< 6; f
++) {
9444 for (uint32_t g
= 0; g
< 16; g
++)
9445 outs() << format("%02" PRIx32
, fpu
.fpu_rsrv4
[f
* g
]) << " ";
9448 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32
, fpu
.fpu_reserved1
);
9452 static void Print_x86_exception_state_t(MachO::x86_exception_state64_t
&exc64
) {
9453 outs() << "\t trapno " << format("0x%08" PRIx32
, exc64
.trapno
);
9454 outs() << " err " << format("0x%08" PRIx32
, exc64
.err
);
9455 outs() << " faultvaddr " << format("0x%016" PRIx64
, exc64
.faultvaddr
) << "\n";
9458 static void Print_arm_thread_state32_t(MachO::arm_thread_state32_t
&cpu32
) {
9459 outs() << "\t r0 " << format("0x%08" PRIx32
, cpu32
.r
[0]);
9460 outs() << " r1 " << format("0x%08" PRIx32
, cpu32
.r
[1]);
9461 outs() << " r2 " << format("0x%08" PRIx32
, cpu32
.r
[2]);
9462 outs() << " r3 " << format("0x%08" PRIx32
, cpu32
.r
[3]) << "\n";
9463 outs() << "\t r4 " << format("0x%08" PRIx32
, cpu32
.r
[4]);
9464 outs() << " r5 " << format("0x%08" PRIx32
, cpu32
.r
[5]);
9465 outs() << " r6 " << format("0x%08" PRIx32
, cpu32
.r
[6]);
9466 outs() << " r7 " << format("0x%08" PRIx32
, cpu32
.r
[7]) << "\n";
9467 outs() << "\t r8 " << format("0x%08" PRIx32
, cpu32
.r
[8]);
9468 outs() << " r9 " << format("0x%08" PRIx32
, cpu32
.r
[9]);
9469 outs() << " r10 " << format("0x%08" PRIx32
, cpu32
.r
[10]);
9470 outs() << " r11 " << format("0x%08" PRIx32
, cpu32
.r
[11]) << "\n";
9471 outs() << "\t r12 " << format("0x%08" PRIx32
, cpu32
.r
[12]);
9472 outs() << " sp " << format("0x%08" PRIx32
, cpu32
.sp
);
9473 outs() << " lr " << format("0x%08" PRIx32
, cpu32
.lr
);
9474 outs() << " pc " << format("0x%08" PRIx32
, cpu32
.pc
) << "\n";
9475 outs() << "\t cpsr " << format("0x%08" PRIx32
, cpu32
.cpsr
) << "\n";
9478 static void Print_arm_thread_state64_t(MachO::arm_thread_state64_t
&cpu64
) {
9479 outs() << "\t x0 " << format("0x%016" PRIx64
, cpu64
.x
[0]);
9480 outs() << " x1 " << format("0x%016" PRIx64
, cpu64
.x
[1]);
9481 outs() << " x2 " << format("0x%016" PRIx64
, cpu64
.x
[2]) << "\n";
9482 outs() << "\t x3 " << format("0x%016" PRIx64
, cpu64
.x
[3]);
9483 outs() << " x4 " << format("0x%016" PRIx64
, cpu64
.x
[4]);
9484 outs() << " x5 " << format("0x%016" PRIx64
, cpu64
.x
[5]) << "\n";
9485 outs() << "\t x6 " << format("0x%016" PRIx64
, cpu64
.x
[6]);
9486 outs() << " x7 " << format("0x%016" PRIx64
, cpu64
.x
[7]);
9487 outs() << " x8 " << format("0x%016" PRIx64
, cpu64
.x
[8]) << "\n";
9488 outs() << "\t x9 " << format("0x%016" PRIx64
, cpu64
.x
[9]);
9489 outs() << " x10 " << format("0x%016" PRIx64
, cpu64
.x
[10]);
9490 outs() << " x11 " << format("0x%016" PRIx64
, cpu64
.x
[11]) << "\n";
9491 outs() << "\t x12 " << format("0x%016" PRIx64
, cpu64
.x
[12]);
9492 outs() << " x13 " << format("0x%016" PRIx64
, cpu64
.x
[13]);
9493 outs() << " x14 " << format("0x%016" PRIx64
, cpu64
.x
[14]) << "\n";
9494 outs() << "\t x15 " << format("0x%016" PRIx64
, cpu64
.x
[15]);
9495 outs() << " x16 " << format("0x%016" PRIx64
, cpu64
.x
[16]);
9496 outs() << " x17 " << format("0x%016" PRIx64
, cpu64
.x
[17]) << "\n";
9497 outs() << "\t x18 " << format("0x%016" PRIx64
, cpu64
.x
[18]);
9498 outs() << " x19 " << format("0x%016" PRIx64
, cpu64
.x
[19]);
9499 outs() << " x20 " << format("0x%016" PRIx64
, cpu64
.x
[20]) << "\n";
9500 outs() << "\t x21 " << format("0x%016" PRIx64
, cpu64
.x
[21]);
9501 outs() << " x22 " << format("0x%016" PRIx64
, cpu64
.x
[22]);
9502 outs() << " x23 " << format("0x%016" PRIx64
, cpu64
.x
[23]) << "\n";
9503 outs() << "\t x24 " << format("0x%016" PRIx64
, cpu64
.x
[24]);
9504 outs() << " x25 " << format("0x%016" PRIx64
, cpu64
.x
[25]);
9505 outs() << " x26 " << format("0x%016" PRIx64
, cpu64
.x
[26]) << "\n";
9506 outs() << "\t x27 " << format("0x%016" PRIx64
, cpu64
.x
[27]);
9507 outs() << " x28 " << format("0x%016" PRIx64
, cpu64
.x
[28]);
9508 outs() << " fp " << format("0x%016" PRIx64
, cpu64
.fp
) << "\n";
9509 outs() << "\t lr " << format("0x%016" PRIx64
, cpu64
.lr
);
9510 outs() << " sp " << format("0x%016" PRIx64
, cpu64
.sp
);
9511 outs() << " pc " << format("0x%016" PRIx64
, cpu64
.pc
) << "\n";
9512 outs() << "\t cpsr " << format("0x%08" PRIx32
, cpu64
.cpsr
) << "\n";
9515 static void PrintThreadCommand(MachO::thread_command t
, const char *Ptr
,
9516 bool isLittleEndian
, uint32_t cputype
) {
9517 if (t
.cmd
== MachO::LC_THREAD
)
9518 outs() << " cmd LC_THREAD\n";
9519 else if (t
.cmd
== MachO::LC_UNIXTHREAD
)
9520 outs() << " cmd LC_UNIXTHREAD\n";
9522 outs() << " cmd " << t
.cmd
<< " (unknown)\n";
9523 outs() << " cmdsize " << t
.cmdsize
;
9524 if (t
.cmdsize
< sizeof(struct MachO::thread_command
) + 2 * sizeof(uint32_t))
9525 outs() << " Incorrect size\n";
9529 const char *begin
= Ptr
+ sizeof(struct MachO::thread_command
);
9530 const char *end
= Ptr
+ t
.cmdsize
;
9531 uint32_t flavor
, count
, left
;
9532 if (cputype
== MachO::CPU_TYPE_I386
) {
9533 while (begin
< end
) {
9534 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9535 memcpy((char *)&flavor
, begin
, sizeof(uint32_t));
9536 begin
+= sizeof(uint32_t);
9541 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9542 sys::swapByteOrder(flavor
);
9543 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9544 memcpy((char *)&count
, begin
, sizeof(uint32_t));
9545 begin
+= sizeof(uint32_t);
9550 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9551 sys::swapByteOrder(count
);
9552 if (flavor
== MachO::x86_THREAD_STATE32
) {
9553 outs() << " flavor i386_THREAD_STATE\n";
9554 if (count
== MachO::x86_THREAD_STATE32_COUNT
)
9555 outs() << " count i386_THREAD_STATE_COUNT\n";
9557 outs() << " count " << count
9558 << " (not x86_THREAD_STATE32_COUNT)\n";
9559 MachO::x86_thread_state32_t cpu32
;
9561 if (left
>= sizeof(MachO::x86_thread_state32_t
)) {
9562 memcpy(&cpu32
, begin
, sizeof(MachO::x86_thread_state32_t
));
9563 begin
+= sizeof(MachO::x86_thread_state32_t
);
9565 memset(&cpu32
, '\0', sizeof(MachO::x86_thread_state32_t
));
9566 memcpy(&cpu32
, begin
, left
);
9569 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9571 Print_x86_thread_state32_t(cpu32
);
9572 } else if (flavor
== MachO::x86_THREAD_STATE
) {
9573 outs() << " flavor x86_THREAD_STATE\n";
9574 if (count
== MachO::x86_THREAD_STATE_COUNT
)
9575 outs() << " count x86_THREAD_STATE_COUNT\n";
9577 outs() << " count " << count
9578 << " (not x86_THREAD_STATE_COUNT)\n";
9579 struct MachO::x86_thread_state_t ts
;
9581 if (left
>= sizeof(MachO::x86_thread_state_t
)) {
9582 memcpy(&ts
, begin
, sizeof(MachO::x86_thread_state_t
));
9583 begin
+= sizeof(MachO::x86_thread_state_t
);
9585 memset(&ts
, '\0', sizeof(MachO::x86_thread_state_t
));
9586 memcpy(&ts
, begin
, left
);
9589 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9591 if (ts
.tsh
.flavor
== MachO::x86_THREAD_STATE32
) {
9592 outs() << "\t tsh.flavor x86_THREAD_STATE32 ";
9593 if (ts
.tsh
.count
== MachO::x86_THREAD_STATE32_COUNT
)
9594 outs() << "tsh.count x86_THREAD_STATE32_COUNT\n";
9596 outs() << "tsh.count " << ts
.tsh
.count
9597 << " (not x86_THREAD_STATE32_COUNT\n";
9598 Print_x86_thread_state32_t(ts
.uts
.ts32
);
9600 outs() << "\t tsh.flavor " << ts
.tsh
.flavor
<< " tsh.count "
9601 << ts
.tsh
.count
<< "\n";
9604 outs() << " flavor " << flavor
<< " (unknown)\n";
9605 outs() << " count " << count
<< "\n";
9606 outs() << " state (unknown)\n";
9607 begin
+= count
* sizeof(uint32_t);
9610 } else if (cputype
== MachO::CPU_TYPE_X86_64
) {
9611 while (begin
< end
) {
9612 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9613 memcpy((char *)&flavor
, begin
, sizeof(uint32_t));
9614 begin
+= sizeof(uint32_t);
9619 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9620 sys::swapByteOrder(flavor
);
9621 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9622 memcpy((char *)&count
, begin
, sizeof(uint32_t));
9623 begin
+= sizeof(uint32_t);
9628 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9629 sys::swapByteOrder(count
);
9630 if (flavor
== MachO::x86_THREAD_STATE64
) {
9631 outs() << " flavor x86_THREAD_STATE64\n";
9632 if (count
== MachO::x86_THREAD_STATE64_COUNT
)
9633 outs() << " count x86_THREAD_STATE64_COUNT\n";
9635 outs() << " count " << count
9636 << " (not x86_THREAD_STATE64_COUNT)\n";
9637 MachO::x86_thread_state64_t cpu64
;
9639 if (left
>= sizeof(MachO::x86_thread_state64_t
)) {
9640 memcpy(&cpu64
, begin
, sizeof(MachO::x86_thread_state64_t
));
9641 begin
+= sizeof(MachO::x86_thread_state64_t
);
9643 memset(&cpu64
, '\0', sizeof(MachO::x86_thread_state64_t
));
9644 memcpy(&cpu64
, begin
, left
);
9647 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9649 Print_x86_thread_state64_t(cpu64
);
9650 } else if (flavor
== MachO::x86_THREAD_STATE
) {
9651 outs() << " flavor x86_THREAD_STATE\n";
9652 if (count
== MachO::x86_THREAD_STATE_COUNT
)
9653 outs() << " count x86_THREAD_STATE_COUNT\n";
9655 outs() << " count " << count
9656 << " (not x86_THREAD_STATE_COUNT)\n";
9657 struct MachO::x86_thread_state_t ts
;
9659 if (left
>= sizeof(MachO::x86_thread_state_t
)) {
9660 memcpy(&ts
, begin
, sizeof(MachO::x86_thread_state_t
));
9661 begin
+= sizeof(MachO::x86_thread_state_t
);
9663 memset(&ts
, '\0', sizeof(MachO::x86_thread_state_t
));
9664 memcpy(&ts
, begin
, left
);
9667 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9669 if (ts
.tsh
.flavor
== MachO::x86_THREAD_STATE64
) {
9670 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
9671 if (ts
.tsh
.count
== MachO::x86_THREAD_STATE64_COUNT
)
9672 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
9674 outs() << "tsh.count " << ts
.tsh
.count
9675 << " (not x86_THREAD_STATE64_COUNT\n";
9676 Print_x86_thread_state64_t(ts
.uts
.ts64
);
9678 outs() << "\t tsh.flavor " << ts
.tsh
.flavor
<< " tsh.count "
9679 << ts
.tsh
.count
<< "\n";
9681 } else if (flavor
== MachO::x86_FLOAT_STATE
) {
9682 outs() << " flavor x86_FLOAT_STATE\n";
9683 if (count
== MachO::x86_FLOAT_STATE_COUNT
)
9684 outs() << " count x86_FLOAT_STATE_COUNT\n";
9686 outs() << " count " << count
<< " (not x86_FLOAT_STATE_COUNT)\n";
9687 struct MachO::x86_float_state_t fs
;
9689 if (left
>= sizeof(MachO::x86_float_state_t
)) {
9690 memcpy(&fs
, begin
, sizeof(MachO::x86_float_state_t
));
9691 begin
+= sizeof(MachO::x86_float_state_t
);
9693 memset(&fs
, '\0', sizeof(MachO::x86_float_state_t
));
9694 memcpy(&fs
, begin
, left
);
9697 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9699 if (fs
.fsh
.flavor
== MachO::x86_FLOAT_STATE64
) {
9700 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
9701 if (fs
.fsh
.count
== MachO::x86_FLOAT_STATE64_COUNT
)
9702 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
9704 outs() << "fsh.count " << fs
.fsh
.count
9705 << " (not x86_FLOAT_STATE64_COUNT\n";
9706 Print_x86_float_state_t(fs
.ufs
.fs64
);
9708 outs() << "\t fsh.flavor " << fs
.fsh
.flavor
<< " fsh.count "
9709 << fs
.fsh
.count
<< "\n";
9711 } else if (flavor
== MachO::x86_EXCEPTION_STATE
) {
9712 outs() << " flavor x86_EXCEPTION_STATE\n";
9713 if (count
== MachO::x86_EXCEPTION_STATE_COUNT
)
9714 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
9716 outs() << " count " << count
9717 << " (not x86_EXCEPTION_STATE_COUNT)\n";
9718 struct MachO::x86_exception_state_t es
;
9720 if (left
>= sizeof(MachO::x86_exception_state_t
)) {
9721 memcpy(&es
, begin
, sizeof(MachO::x86_exception_state_t
));
9722 begin
+= sizeof(MachO::x86_exception_state_t
);
9724 memset(&es
, '\0', sizeof(MachO::x86_exception_state_t
));
9725 memcpy(&es
, begin
, left
);
9728 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9730 if (es
.esh
.flavor
== MachO::x86_EXCEPTION_STATE64
) {
9731 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
9732 if (es
.esh
.count
== MachO::x86_EXCEPTION_STATE64_COUNT
)
9733 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
9735 outs() << "\t esh.count " << es
.esh
.count
9736 << " (not x86_EXCEPTION_STATE64_COUNT\n";
9737 Print_x86_exception_state_t(es
.ues
.es64
);
9739 outs() << "\t esh.flavor " << es
.esh
.flavor
<< " esh.count "
9740 << es
.esh
.count
<< "\n";
9742 } else if (flavor
== MachO::x86_EXCEPTION_STATE64
) {
9743 outs() << " flavor x86_EXCEPTION_STATE64\n";
9744 if (count
== MachO::x86_EXCEPTION_STATE64_COUNT
)
9745 outs() << " count x86_EXCEPTION_STATE64_COUNT\n";
9747 outs() << " count " << count
9748 << " (not x86_EXCEPTION_STATE64_COUNT)\n";
9749 struct MachO::x86_exception_state64_t es64
;
9751 if (left
>= sizeof(MachO::x86_exception_state64_t
)) {
9752 memcpy(&es64
, begin
, sizeof(MachO::x86_exception_state64_t
));
9753 begin
+= sizeof(MachO::x86_exception_state64_t
);
9755 memset(&es64
, '\0', sizeof(MachO::x86_exception_state64_t
));
9756 memcpy(&es64
, begin
, left
);
9759 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9761 Print_x86_exception_state_t(es64
);
9763 outs() << " flavor " << flavor
<< " (unknown)\n";
9764 outs() << " count " << count
<< "\n";
9765 outs() << " state (unknown)\n";
9766 begin
+= count
* sizeof(uint32_t);
9769 } else if (cputype
== MachO::CPU_TYPE_ARM
) {
9770 while (begin
< end
) {
9771 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9772 memcpy((char *)&flavor
, begin
, sizeof(uint32_t));
9773 begin
+= sizeof(uint32_t);
9778 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9779 sys::swapByteOrder(flavor
);
9780 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9781 memcpy((char *)&count
, begin
, sizeof(uint32_t));
9782 begin
+= sizeof(uint32_t);
9787 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9788 sys::swapByteOrder(count
);
9789 if (flavor
== MachO::ARM_THREAD_STATE
) {
9790 outs() << " flavor ARM_THREAD_STATE\n";
9791 if (count
== MachO::ARM_THREAD_STATE_COUNT
)
9792 outs() << " count ARM_THREAD_STATE_COUNT\n";
9794 outs() << " count " << count
9795 << " (not ARM_THREAD_STATE_COUNT)\n";
9796 MachO::arm_thread_state32_t cpu32
;
9798 if (left
>= sizeof(MachO::arm_thread_state32_t
)) {
9799 memcpy(&cpu32
, begin
, sizeof(MachO::arm_thread_state32_t
));
9800 begin
+= sizeof(MachO::arm_thread_state32_t
);
9802 memset(&cpu32
, '\0', sizeof(MachO::arm_thread_state32_t
));
9803 memcpy(&cpu32
, begin
, left
);
9806 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9808 Print_arm_thread_state32_t(cpu32
);
9810 outs() << " flavor " << flavor
<< " (unknown)\n";
9811 outs() << " count " << count
<< "\n";
9812 outs() << " state (unknown)\n";
9813 begin
+= count
* sizeof(uint32_t);
9816 } else if (cputype
== MachO::CPU_TYPE_ARM64
||
9817 cputype
== MachO::CPU_TYPE_ARM64_32
) {
9818 while (begin
< end
) {
9819 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9820 memcpy((char *)&flavor
, begin
, sizeof(uint32_t));
9821 begin
+= sizeof(uint32_t);
9826 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9827 sys::swapByteOrder(flavor
);
9828 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9829 memcpy((char *)&count
, begin
, sizeof(uint32_t));
9830 begin
+= sizeof(uint32_t);
9835 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9836 sys::swapByteOrder(count
);
9837 if (flavor
== MachO::ARM_THREAD_STATE64
) {
9838 outs() << " flavor ARM_THREAD_STATE64\n";
9839 if (count
== MachO::ARM_THREAD_STATE64_COUNT
)
9840 outs() << " count ARM_THREAD_STATE64_COUNT\n";
9842 outs() << " count " << count
9843 << " (not ARM_THREAD_STATE64_COUNT)\n";
9844 MachO::arm_thread_state64_t cpu64
;
9846 if (left
>= sizeof(MachO::arm_thread_state64_t
)) {
9847 memcpy(&cpu64
, begin
, sizeof(MachO::arm_thread_state64_t
));
9848 begin
+= sizeof(MachO::arm_thread_state64_t
);
9850 memset(&cpu64
, '\0', sizeof(MachO::arm_thread_state64_t
));
9851 memcpy(&cpu64
, begin
, left
);
9854 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9856 Print_arm_thread_state64_t(cpu64
);
9858 outs() << " flavor " << flavor
<< " (unknown)\n";
9859 outs() << " count " << count
<< "\n";
9860 outs() << " state (unknown)\n";
9861 begin
+= count
* sizeof(uint32_t);
9865 while (begin
< end
) {
9866 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9867 memcpy((char *)&flavor
, begin
, sizeof(uint32_t));
9868 begin
+= sizeof(uint32_t);
9873 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9874 sys::swapByteOrder(flavor
);
9875 if (end
- begin
> (ptrdiff_t)sizeof(uint32_t)) {
9876 memcpy((char *)&count
, begin
, sizeof(uint32_t));
9877 begin
+= sizeof(uint32_t);
9882 if (isLittleEndian
!= sys::IsLittleEndianHost
)
9883 sys::swapByteOrder(count
);
9884 outs() << " flavor " << flavor
<< "\n";
9885 outs() << " count " << count
<< "\n";
9886 outs() << " state (Unknown cputype/cpusubtype)\n";
9887 begin
+= count
* sizeof(uint32_t);
9892 static void PrintDylibCommand(MachO::dylib_command dl
, const char *Ptr
) {
9893 if (dl
.cmd
== MachO::LC_ID_DYLIB
)
9894 outs() << " cmd LC_ID_DYLIB\n";
9895 else if (dl
.cmd
== MachO::LC_LOAD_DYLIB
)
9896 outs() << " cmd LC_LOAD_DYLIB\n";
9897 else if (dl
.cmd
== MachO::LC_LOAD_WEAK_DYLIB
)
9898 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
9899 else if (dl
.cmd
== MachO::LC_REEXPORT_DYLIB
)
9900 outs() << " cmd LC_REEXPORT_DYLIB\n";
9901 else if (dl
.cmd
== MachO::LC_LAZY_LOAD_DYLIB
)
9902 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
9903 else if (dl
.cmd
== MachO::LC_LOAD_UPWARD_DYLIB
)
9904 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
9906 outs() << " cmd " << dl
.cmd
<< " (unknown)\n";
9907 outs() << " cmdsize " << dl
.cmdsize
;
9908 if (dl
.cmdsize
< sizeof(struct MachO::dylib_command
))
9909 outs() << " Incorrect size\n";
9912 if (dl
.dylib
.name
< dl
.cmdsize
) {
9913 const char *P
= (const char *)(Ptr
) + dl
.dylib
.name
;
9914 outs() << " name " << P
<< " (offset " << dl
.dylib
.name
<< ")\n";
9916 outs() << " name ?(bad offset " << dl
.dylib
.name
<< ")\n";
9918 outs() << " time stamp " << dl
.dylib
.timestamp
<< " ";
9919 time_t t
= dl
.dylib
.timestamp
;
9920 outs() << ctime(&t
);
9921 outs() << " current version ";
9922 if (dl
.dylib
.current_version
== 0xffffffff)
9925 outs() << ((dl
.dylib
.current_version
>> 16) & 0xffff) << "."
9926 << ((dl
.dylib
.current_version
>> 8) & 0xff) << "."
9927 << (dl
.dylib
.current_version
& 0xff) << "\n";
9928 outs() << "compatibility version ";
9929 if (dl
.dylib
.compatibility_version
== 0xffffffff)
9932 outs() << ((dl
.dylib
.compatibility_version
>> 16) & 0xffff) << "."
9933 << ((dl
.dylib
.compatibility_version
>> 8) & 0xff) << "."
9934 << (dl
.dylib
.compatibility_version
& 0xff) << "\n";
9937 static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld
,
9938 uint32_t object_size
) {
9939 if (ld
.cmd
== MachO::LC_CODE_SIGNATURE
)
9940 outs() << " cmd LC_CODE_SIGNATURE\n";
9941 else if (ld
.cmd
== MachO::LC_SEGMENT_SPLIT_INFO
)
9942 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
9943 else if (ld
.cmd
== MachO::LC_FUNCTION_STARTS
)
9944 outs() << " cmd LC_FUNCTION_STARTS\n";
9945 else if (ld
.cmd
== MachO::LC_DATA_IN_CODE
)
9946 outs() << " cmd LC_DATA_IN_CODE\n";
9947 else if (ld
.cmd
== MachO::LC_DYLIB_CODE_SIGN_DRS
)
9948 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
9949 else if (ld
.cmd
== MachO::LC_LINKER_OPTIMIZATION_HINT
)
9950 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
9951 else if (ld
.cmd
== MachO::LC_DYLD_EXPORTS_TRIE
)
9952 outs() << " cmd LC_DYLD_EXPORTS_TRIE\n";
9953 else if (ld
.cmd
== MachO::LC_DYLD_CHAINED_FIXUPS
)
9954 outs() << " cmd LC_DYLD_CHAINED_FIXUPS\n";
9955 else if (ld
.cmd
== MachO::LC_ATOM_INFO
)
9956 outs() << " cmd LC_ATOM_INFO\n";
9958 outs() << " cmd " << ld
.cmd
<< " (?)\n";
9959 outs() << " cmdsize " << ld
.cmdsize
;
9960 if (ld
.cmdsize
!= sizeof(struct MachO::linkedit_data_command
))
9961 outs() << " Incorrect size\n";
9964 outs() << " dataoff " << ld
.dataoff
;
9965 if (ld
.dataoff
> object_size
)
9966 outs() << " (past end of file)\n";
9969 outs() << " datasize " << ld
.datasize
;
9970 uint64_t big_size
= ld
.dataoff
;
9971 big_size
+= ld
.datasize
;
9972 if (big_size
> object_size
)
9973 outs() << " (past end of file)\n";
9978 static void PrintLoadCommands(const MachOObjectFile
*Obj
, uint32_t filetype
,
9979 uint32_t cputype
, bool verbose
) {
9980 StringRef Buf
= Obj
->getData();
9982 for (const auto &Command
: Obj
->load_commands()) {
9983 outs() << "Load command " << Index
++ << "\n";
9984 if (Command
.C
.cmd
== MachO::LC_SEGMENT
) {
9985 MachO::segment_command SLC
= Obj
->getSegmentLoadCommand(Command
);
9986 const char *sg_segname
= SLC
.segname
;
9987 PrintSegmentCommand(SLC
.cmd
, SLC
.cmdsize
, SLC
.segname
, SLC
.vmaddr
,
9988 SLC
.vmsize
, SLC
.fileoff
, SLC
.filesize
, SLC
.maxprot
,
9989 SLC
.initprot
, SLC
.nsects
, SLC
.flags
, Buf
.size(),
9991 for (unsigned j
= 0; j
< SLC
.nsects
; j
++) {
9992 MachO::section S
= Obj
->getSection(Command
, j
);
9993 PrintSection(S
.sectname
, S
.segname
, S
.addr
, S
.size
, S
.offset
, S
.align
,
9994 S
.reloff
, S
.nreloc
, S
.flags
, S
.reserved1
, S
.reserved2
,
9995 SLC
.cmd
, sg_segname
, filetype
, Buf
.size(), verbose
);
9997 } else if (Command
.C
.cmd
== MachO::LC_SEGMENT_64
) {
9998 MachO::segment_command_64 SLC_64
= Obj
->getSegment64LoadCommand(Command
);
9999 const char *sg_segname
= SLC_64
.segname
;
10000 PrintSegmentCommand(SLC_64
.cmd
, SLC_64
.cmdsize
, SLC_64
.segname
,
10001 SLC_64
.vmaddr
, SLC_64
.vmsize
, SLC_64
.fileoff
,
10002 SLC_64
.filesize
, SLC_64
.maxprot
, SLC_64
.initprot
,
10003 SLC_64
.nsects
, SLC_64
.flags
, Buf
.size(), verbose
);
10004 for (unsigned j
= 0; j
< SLC_64
.nsects
; j
++) {
10005 MachO::section_64 S_64
= Obj
->getSection64(Command
, j
);
10006 PrintSection(S_64
.sectname
, S_64
.segname
, S_64
.addr
, S_64
.size
,
10007 S_64
.offset
, S_64
.align
, S_64
.reloff
, S_64
.nreloc
,
10008 S_64
.flags
, S_64
.reserved1
, S_64
.reserved2
, SLC_64
.cmd
,
10009 sg_segname
, filetype
, Buf
.size(), verbose
);
10011 } else if (Command
.C
.cmd
== MachO::LC_SYMTAB
) {
10012 MachO::symtab_command Symtab
= Obj
->getSymtabLoadCommand();
10013 PrintSymtabLoadCommand(Symtab
, Obj
->is64Bit(), Buf
.size());
10014 } else if (Command
.C
.cmd
== MachO::LC_DYSYMTAB
) {
10015 MachO::dysymtab_command Dysymtab
= Obj
->getDysymtabLoadCommand();
10016 MachO::symtab_command Symtab
= Obj
->getSymtabLoadCommand();
10017 PrintDysymtabLoadCommand(Dysymtab
, Symtab
.nsyms
, Buf
.size(),
10019 } else if (Command
.C
.cmd
== MachO::LC_DYLD_INFO
||
10020 Command
.C
.cmd
== MachO::LC_DYLD_INFO_ONLY
) {
10021 MachO::dyld_info_command DyldInfo
= Obj
->getDyldInfoLoadCommand(Command
);
10022 PrintDyldInfoLoadCommand(DyldInfo
, Buf
.size());
10023 } else if (Command
.C
.cmd
== MachO::LC_LOAD_DYLINKER
||
10024 Command
.C
.cmd
== MachO::LC_ID_DYLINKER
||
10025 Command
.C
.cmd
== MachO::LC_DYLD_ENVIRONMENT
) {
10026 MachO::dylinker_command Dyld
= Obj
->getDylinkerCommand(Command
);
10027 PrintDyldLoadCommand(Dyld
, Command
.Ptr
);
10028 } else if (Command
.C
.cmd
== MachO::LC_UUID
) {
10029 MachO::uuid_command Uuid
= Obj
->getUuidCommand(Command
);
10030 PrintUuidLoadCommand(Uuid
);
10031 } else if (Command
.C
.cmd
== MachO::LC_RPATH
) {
10032 MachO::rpath_command Rpath
= Obj
->getRpathCommand(Command
);
10033 PrintRpathLoadCommand(Rpath
, Command
.Ptr
);
10034 } else if (Command
.C
.cmd
== MachO::LC_VERSION_MIN_MACOSX
||
10035 Command
.C
.cmd
== MachO::LC_VERSION_MIN_IPHONEOS
||
10036 Command
.C
.cmd
== MachO::LC_VERSION_MIN_TVOS
||
10037 Command
.C
.cmd
== MachO::LC_VERSION_MIN_WATCHOS
) {
10038 MachO::version_min_command Vd
= Obj
->getVersionMinLoadCommand(Command
);
10039 PrintVersionMinLoadCommand(Vd
);
10040 } else if (Command
.C
.cmd
== MachO::LC_NOTE
) {
10041 MachO::note_command Nt
= Obj
->getNoteLoadCommand(Command
);
10042 PrintNoteLoadCommand(Nt
);
10043 } else if (Command
.C
.cmd
== MachO::LC_BUILD_VERSION
) {
10044 MachO::build_version_command Bv
=
10045 Obj
->getBuildVersionLoadCommand(Command
);
10046 PrintBuildVersionLoadCommand(Obj
, Bv
, verbose
);
10047 } else if (Command
.C
.cmd
== MachO::LC_SOURCE_VERSION
) {
10048 MachO::source_version_command Sd
= Obj
->getSourceVersionCommand(Command
);
10049 PrintSourceVersionCommand(Sd
);
10050 } else if (Command
.C
.cmd
== MachO::LC_MAIN
) {
10051 MachO::entry_point_command Ep
= Obj
->getEntryPointCommand(Command
);
10052 PrintEntryPointCommand(Ep
);
10053 } else if (Command
.C
.cmd
== MachO::LC_ENCRYPTION_INFO
) {
10054 MachO::encryption_info_command Ei
=
10055 Obj
->getEncryptionInfoCommand(Command
);
10056 PrintEncryptionInfoCommand(Ei
, Buf
.size());
10057 } else if (Command
.C
.cmd
== MachO::LC_ENCRYPTION_INFO_64
) {
10058 MachO::encryption_info_command_64 Ei
=
10059 Obj
->getEncryptionInfoCommand64(Command
);
10060 PrintEncryptionInfoCommand64(Ei
, Buf
.size());
10061 } else if (Command
.C
.cmd
== MachO::LC_LINKER_OPTION
) {
10062 MachO::linker_option_command Lo
=
10063 Obj
->getLinkerOptionLoadCommand(Command
);
10064 PrintLinkerOptionCommand(Lo
, Command
.Ptr
);
10065 } else if (Command
.C
.cmd
== MachO::LC_SUB_FRAMEWORK
) {
10066 MachO::sub_framework_command Sf
= Obj
->getSubFrameworkCommand(Command
);
10067 PrintSubFrameworkCommand(Sf
, Command
.Ptr
);
10068 } else if (Command
.C
.cmd
== MachO::LC_SUB_UMBRELLA
) {
10069 MachO::sub_umbrella_command Sf
= Obj
->getSubUmbrellaCommand(Command
);
10070 PrintSubUmbrellaCommand(Sf
, Command
.Ptr
);
10071 } else if (Command
.C
.cmd
== MachO::LC_SUB_LIBRARY
) {
10072 MachO::sub_library_command Sl
= Obj
->getSubLibraryCommand(Command
);
10073 PrintSubLibraryCommand(Sl
, Command
.Ptr
);
10074 } else if (Command
.C
.cmd
== MachO::LC_SUB_CLIENT
) {
10075 MachO::sub_client_command Sc
= Obj
->getSubClientCommand(Command
);
10076 PrintSubClientCommand(Sc
, Command
.Ptr
);
10077 } else if (Command
.C
.cmd
== MachO::LC_ROUTINES
) {
10078 MachO::routines_command Rc
= Obj
->getRoutinesCommand(Command
);
10079 PrintRoutinesCommand(Rc
);
10080 } else if (Command
.C
.cmd
== MachO::LC_ROUTINES_64
) {
10081 MachO::routines_command_64 Rc
= Obj
->getRoutinesCommand64(Command
);
10082 PrintRoutinesCommand64(Rc
);
10083 } else if (Command
.C
.cmd
== MachO::LC_THREAD
||
10084 Command
.C
.cmd
== MachO::LC_UNIXTHREAD
) {
10085 MachO::thread_command Tc
= Obj
->getThreadCommand(Command
);
10086 PrintThreadCommand(Tc
, Command
.Ptr
, Obj
->isLittleEndian(), cputype
);
10087 } else if (Command
.C
.cmd
== MachO::LC_LOAD_DYLIB
||
10088 Command
.C
.cmd
== MachO::LC_ID_DYLIB
||
10089 Command
.C
.cmd
== MachO::LC_LOAD_WEAK_DYLIB
||
10090 Command
.C
.cmd
== MachO::LC_REEXPORT_DYLIB
||
10091 Command
.C
.cmd
== MachO::LC_LAZY_LOAD_DYLIB
||
10092 Command
.C
.cmd
== MachO::LC_LOAD_UPWARD_DYLIB
) {
10093 MachO::dylib_command Dl
= Obj
->getDylibIDLoadCommand(Command
);
10094 PrintDylibCommand(Dl
, Command
.Ptr
);
10095 } else if (Command
.C
.cmd
== MachO::LC_CODE_SIGNATURE
||
10096 Command
.C
.cmd
== MachO::LC_SEGMENT_SPLIT_INFO
||
10097 Command
.C
.cmd
== MachO::LC_FUNCTION_STARTS
||
10098 Command
.C
.cmd
== MachO::LC_DATA_IN_CODE
||
10099 Command
.C
.cmd
== MachO::LC_DYLIB_CODE_SIGN_DRS
||
10100 Command
.C
.cmd
== MachO::LC_LINKER_OPTIMIZATION_HINT
||
10101 Command
.C
.cmd
== MachO::LC_DYLD_EXPORTS_TRIE
||
10102 Command
.C
.cmd
== MachO::LC_DYLD_CHAINED_FIXUPS
||
10103 Command
.C
.cmd
== MachO::LC_ATOM_INFO
) {
10104 MachO::linkedit_data_command Ld
=
10105 Obj
->getLinkeditDataLoadCommand(Command
);
10106 PrintLinkEditDataCommand(Ld
, Buf
.size());
10108 outs() << " cmd ?(" << format("0x%08" PRIx32
, Command
.C
.cmd
)
10110 outs() << " cmdsize " << Command
.C
.cmdsize
<< "\n";
10111 // TODO: get and print the raw bytes of the load command.
10113 // TODO: print all the other kinds of load commands.
10117 static void PrintMachHeader(const MachOObjectFile
*Obj
, bool verbose
) {
10118 if (Obj
->is64Bit()) {
10119 MachO::mach_header_64 H_64
;
10120 H_64
= Obj
->getHeader64();
10121 PrintMachHeader(H_64
.magic
, H_64
.cputype
, H_64
.cpusubtype
, H_64
.filetype
,
10122 H_64
.ncmds
, H_64
.sizeofcmds
, H_64
.flags
, verbose
);
10124 MachO::mach_header H
;
10125 H
= Obj
->getHeader();
10126 PrintMachHeader(H
.magic
, H
.cputype
, H
.cpusubtype
, H
.filetype
, H
.ncmds
,
10127 H
.sizeofcmds
, H
.flags
, verbose
);
10131 void objdump::printMachOFileHeader(const object::ObjectFile
*Obj
) {
10132 const MachOObjectFile
*file
= cast
<const MachOObjectFile
>(Obj
);
10133 PrintMachHeader(file
, Verbose
);
10136 void MachODumper::printPrivateHeaders() {
10137 printMachOFileHeader(&Obj
);
10138 if (!FirstPrivateHeader
)
10139 printMachOLoadCommands(&Obj
);
10142 void objdump::printMachOLoadCommands(const object::ObjectFile
*Obj
) {
10143 const MachOObjectFile
*file
= cast
<const MachOObjectFile
>(Obj
);
10144 uint32_t filetype
= 0;
10145 uint32_t cputype
= 0;
10146 if (file
->is64Bit()) {
10147 MachO::mach_header_64 H_64
;
10148 H_64
= file
->getHeader64();
10149 filetype
= H_64
.filetype
;
10150 cputype
= H_64
.cputype
;
10152 MachO::mach_header H
;
10153 H
= file
->getHeader();
10154 filetype
= H
.filetype
;
10155 cputype
= H
.cputype
;
10157 PrintLoadCommands(file
, filetype
, cputype
, Verbose
);
10160 //===----------------------------------------------------------------------===//
10161 // export trie dumping
10162 //===----------------------------------------------------------------------===//
10164 static void printMachOExportsTrie(const object::MachOObjectFile
*Obj
) {
10165 uint64_t BaseSegmentAddress
= 0;
10166 for (const auto &Command
: Obj
->load_commands()) {
10167 if (Command
.C
.cmd
== MachO::LC_SEGMENT
) {
10168 MachO::segment_command Seg
= Obj
->getSegmentLoadCommand(Command
);
10169 if (Seg
.fileoff
== 0 && Seg
.filesize
!= 0) {
10170 BaseSegmentAddress
= Seg
.vmaddr
;
10173 } else if (Command
.C
.cmd
== MachO::LC_SEGMENT_64
) {
10174 MachO::segment_command_64 Seg
= Obj
->getSegment64LoadCommand(Command
);
10175 if (Seg
.fileoff
== 0 && Seg
.filesize
!= 0) {
10176 BaseSegmentAddress
= Seg
.vmaddr
;
10181 Error Err
= Error::success();
10182 for (const object::ExportEntry
&Entry
: Obj
->exports(Err
)) {
10183 uint64_t Flags
= Entry
.flags();
10184 bool ReExport
= (Flags
& MachO::EXPORT_SYMBOL_FLAGS_REEXPORT
);
10185 bool WeakDef
= (Flags
& MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION
);
10186 bool ThreadLocal
= ((Flags
& MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK
) ==
10187 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL
);
10188 bool Abs
= ((Flags
& MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK
) ==
10189 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE
);
10190 bool Resolver
= (Flags
& MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER
);
10192 outs() << "[re-export] ";
10194 outs() << format("0x%08llX ",
10195 Entry
.address() + BaseSegmentAddress
);
10196 outs() << Entry
.name();
10197 if (WeakDef
|| ThreadLocal
|| Resolver
|| Abs
) {
10201 outs() << LS
<< "weak_def";
10203 outs() << LS
<< "per-thread";
10205 outs() << LS
<< "absolute";
10207 outs() << LS
<< format("resolver=0x%08llX", Entry
.other());
10211 StringRef DylibName
= "unknown";
10212 int Ordinal
= Entry
.other() - 1;
10213 Obj
->getLibraryShortNameByIndex(Ordinal
, DylibName
);
10214 if (Entry
.otherName().empty())
10215 outs() << " (from " << DylibName
<< ")";
10217 outs() << " (" << Entry
.otherName() << " from " << DylibName
<< ")";
10222 reportError(std::move(Err
), Obj
->getFileName());
10225 //===----------------------------------------------------------------------===//
10226 // rebase table dumping
10227 //===----------------------------------------------------------------------===//
10229 static void printMachORebaseTable(object::MachOObjectFile
*Obj
) {
10230 outs() << "segment section address type\n";
10231 Error Err
= Error::success();
10232 for (const object::MachORebaseEntry
&Entry
: Obj
->rebaseTable(Err
)) {
10233 StringRef SegmentName
= Entry
.segmentName();
10234 StringRef SectionName
= Entry
.sectionName();
10235 uint64_t Address
= Entry
.address();
10237 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
10238 outs() << format("%-8s %-18s 0x%08" PRIX64
" %s\n",
10239 SegmentName
.str().c_str(), SectionName
.str().c_str(),
10240 Address
, Entry
.typeName().str().c_str());
10243 reportError(std::move(Err
), Obj
->getFileName());
10246 static StringRef
ordinalName(const object::MachOObjectFile
*Obj
, int Ordinal
) {
10247 StringRef DylibName
;
10249 case MachO::BIND_SPECIAL_DYLIB_SELF
:
10250 return "this-image";
10251 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE
:
10252 return "main-executable";
10253 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP
:
10254 return "flat-namespace";
10255 case MachO::BIND_SPECIAL_DYLIB_WEAK_LOOKUP
:
10259 std::error_code EC
=
10260 Obj
->getLibraryShortNameByIndex(Ordinal
- 1, DylibName
);
10262 return "<<bad library ordinal>>";
10266 return "<<unknown special ordinal>>";
10269 //===----------------------------------------------------------------------===//
10270 // bind table dumping
10271 //===----------------------------------------------------------------------===//
10273 static void printMachOBindTable(object::MachOObjectFile
*Obj
) {
10274 // Build table of sections so names can used in final output.
10275 outs() << "segment section address type "
10276 "addend dylib symbol\n";
10277 Error Err
= Error::success();
10278 for (const object::MachOBindEntry
&Entry
: Obj
->bindTable(Err
)) {
10279 StringRef SegmentName
= Entry
.segmentName();
10280 StringRef SectionName
= Entry
.sectionName();
10281 uint64_t Address
= Entry
.address();
10283 // Table lines look like:
10284 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
10286 if (Entry
.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT
)
10287 Attr
= " (weak_import)";
10288 outs() << left_justify(SegmentName
, 8) << " "
10289 << left_justify(SectionName
, 18) << " "
10290 << format_hex(Address
, 10, true) << " "
10291 << left_justify(Entry
.typeName(), 8) << " "
10292 << format_decimal(Entry
.addend(), 8) << " "
10293 << left_justify(ordinalName(Obj
, Entry
.ordinal()), 16) << " "
10294 << Entry
.symbolName() << Attr
<< "\n";
10297 reportError(std::move(Err
), Obj
->getFileName());
10300 //===----------------------------------------------------------------------===//
10301 // lazy bind table dumping
10302 //===----------------------------------------------------------------------===//
10304 static void printMachOLazyBindTable(object::MachOObjectFile
*Obj
) {
10305 outs() << "segment section address "
10307 Error Err
= Error::success();
10308 for (const object::MachOBindEntry
&Entry
: Obj
->lazyBindTable(Err
)) {
10309 StringRef SegmentName
= Entry
.segmentName();
10310 StringRef SectionName
= Entry
.sectionName();
10311 uint64_t Address
= Entry
.address();
10313 // Table lines look like:
10314 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
10315 outs() << left_justify(SegmentName
, 8) << " "
10316 << left_justify(SectionName
, 18) << " "
10317 << format_hex(Address
, 10, true) << " "
10318 << left_justify(ordinalName(Obj
, Entry
.ordinal()), 16) << " "
10319 << Entry
.symbolName() << "\n";
10322 reportError(std::move(Err
), Obj
->getFileName());
10325 //===----------------------------------------------------------------------===//
10326 // weak bind table dumping
10327 //===----------------------------------------------------------------------===//
10329 static void printMachOWeakBindTable(object::MachOObjectFile
*Obj
) {
10330 outs() << "segment section address "
10331 "type addend symbol\n";
10332 Error Err
= Error::success();
10333 for (const object::MachOBindEntry
&Entry
: Obj
->weakBindTable(Err
)) {
10334 // Strong symbols don't have a location to update.
10335 if (Entry
.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION
) {
10336 outs() << " strong "
10337 << Entry
.symbolName() << "\n";
10340 StringRef SegmentName
= Entry
.segmentName();
10341 StringRef SectionName
= Entry
.sectionName();
10342 uint64_t Address
= Entry
.address();
10344 // Table lines look like:
10345 // __DATA __data 0x00001000 pointer 0 _foo
10346 outs() << left_justify(SegmentName
, 8) << " "
10347 << left_justify(SectionName
, 18) << " "
10348 << format_hex(Address
, 10, true) << " "
10349 << left_justify(Entry
.typeName(), 8) << " "
10350 << format_decimal(Entry
.addend(), 8) << " " << Entry
.symbolName()
10354 reportError(std::move(Err
), Obj
->getFileName());
10357 // get_dyld_bind_info_symbolname() is used for disassembly and passed an
10358 // address, ReferenceValue, in the Mach-O file and looks in the dyld bind
10359 // information for that address. If the address is found its binding symbol
10360 // name is returned. If not nullptr is returned.
10361 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue
,
10362 struct DisassembleInfo
*info
) {
10363 if (info
->bindtable
== nullptr) {
10364 info
->bindtable
= std::make_unique
<SymbolAddressMap
>();
10365 Error Err
= Error::success();
10366 for (const object::MachOBindEntry
&Entry
: info
->O
->bindTable(Err
)) {
10367 uint64_t Address
= Entry
.address();
10368 StringRef name
= Entry
.symbolName();
10370 (*info
->bindtable
)[Address
] = name
;
10373 reportError(std::move(Err
), info
->O
->getFileName());
10375 auto name
= info
->bindtable
->lookup(ReferenceValue
);
10376 return !name
.empty() ? name
.data() : nullptr;
10379 void objdump::printLazyBindTable(ObjectFile
*o
) {
10380 outs() << "\nLazy bind table:\n";
10381 if (MachOObjectFile
*MachO
= dyn_cast
<MachOObjectFile
>(o
))
10382 printMachOLazyBindTable(MachO
);
10385 << "This operation is only currently supported "
10386 "for Mach-O executable files.\n";
10389 void objdump::printWeakBindTable(ObjectFile
*o
) {
10390 outs() << "\nWeak bind table:\n";
10391 if (MachOObjectFile
*MachO
= dyn_cast
<MachOObjectFile
>(o
))
10392 printMachOWeakBindTable(MachO
);
10395 << "This operation is only currently supported "
10396 "for Mach-O executable files.\n";
10399 void objdump::printExportsTrie(const ObjectFile
*o
) {
10400 outs() << "\nExports trie:\n";
10401 if (const MachOObjectFile
*MachO
= dyn_cast
<MachOObjectFile
>(o
))
10402 printMachOExportsTrie(MachO
);
10405 << "This operation is only currently supported "
10406 "for Mach-O executable files.\n";
10409 void objdump::printRebaseTable(ObjectFile
*o
) {
10410 outs() << "\nRebase table:\n";
10411 if (MachOObjectFile
*MachO
= dyn_cast
<MachOObjectFile
>(o
))
10412 printMachORebaseTable(MachO
);
10415 << "This operation is only currently supported "
10416 "for Mach-O executable files.\n";
10419 void objdump::printBindTable(ObjectFile
*o
) {
10420 outs() << "\nBind table:\n";
10421 if (MachOObjectFile
*MachO
= dyn_cast
<MachOObjectFile
>(o
))
10422 printMachOBindTable(MachO
);
10425 << "This operation is only currently supported "
10426 "for Mach-O executable files.\n";