1 //===- MachODumper.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-readobj.
11 //===----------------------------------------------------------------------===//
14 #include "ObjDumper.h"
15 #include "StackMapPrinter.h"
16 #include "llvm-readobj.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/Object/MachO.h"
20 #include "llvm/Support/Casting.h"
21 #include "llvm/Support/ScopedPrinter.h"
24 using namespace object
;
28 class MachODumper
: public ObjDumper
{
30 MachODumper(const MachOObjectFile
*Obj
, ScopedPrinter
&Writer
)
31 : ObjDumper(Writer
), Obj(Obj
) {}
33 void printFileHeaders() override
;
34 void printSectionHeaders() override
;
35 void printRelocations() override
;
36 void printUnwindInfo() override
;
37 void printStackMap() const override
;
39 void printNeededLibraries() override
;
42 void printMachODataInCode() override
;
43 void printMachOVersionMin() override
;
44 void printMachODysymtab() override
;
45 void printMachOSegment() override
;
46 void printMachOIndirectSymbols() override
;
47 void printMachOLinkerOptions () override
;
50 template<class MachHeader
>
51 void printFileHeaders(const MachHeader
&Header
);
53 void printSymbols() override
;
54 void printDynamicSymbols() override
;
55 void printSymbol(const SymbolRef
&Symbol
);
57 void printRelocation(const RelocationRef
&Reloc
);
59 void printRelocation(const MachOObjectFile
*Obj
, const RelocationRef
&Reloc
);
61 void printSectionHeaders(const MachOObjectFile
*Obj
);
63 const MachOObjectFile
*Obj
;
71 std::error_code
createMachODumper(const object::ObjectFile
*Obj
,
72 ScopedPrinter
&Writer
,
73 std::unique_ptr
<ObjDumper
> &Result
) {
74 const MachOObjectFile
*MachOObj
= dyn_cast
<MachOObjectFile
>(Obj
);
76 return readobj_error::unsupported_obj_file_format
;
78 Result
.reset(new MachODumper(MachOObj
, Writer
));
79 return readobj_error::success
;
84 static const EnumEntry
<uint32_t> MachOMagics
[] = {
85 { "Magic", MachO::MH_MAGIC
},
86 { "Cigam", MachO::MH_CIGAM
},
87 { "Magic64", MachO::MH_MAGIC_64
},
88 { "Cigam64", MachO::MH_CIGAM_64
},
89 { "FatMagic", MachO::FAT_MAGIC
},
90 { "FatCigam", MachO::FAT_CIGAM
},
93 static const EnumEntry
<uint32_t> MachOHeaderFileTypes
[] = {
94 { "Relocatable", MachO::MH_OBJECT
},
95 { "Executable", MachO::MH_EXECUTE
},
96 { "FixedVMLibrary", MachO::MH_FVMLIB
},
97 { "Core", MachO::MH_CORE
},
98 { "PreloadedExecutable", MachO::MH_PRELOAD
},
99 { "DynamicLibrary", MachO::MH_DYLIB
},
100 { "DynamicLinker", MachO::MH_DYLINKER
},
101 { "Bundle", MachO::MH_BUNDLE
},
102 { "DynamicLibraryStub", MachO::MH_DYLIB_STUB
},
103 { "DWARFSymbol", MachO::MH_DSYM
},
104 { "KextBundle", MachO::MH_KEXT_BUNDLE
},
107 static const EnumEntry
<uint32_t> MachOHeaderCpuTypes
[] = {
108 { "Any" , static_cast<uint32_t>(MachO::CPU_TYPE_ANY
) },
109 { "X86" , MachO::CPU_TYPE_X86
},
110 { "X86-64" , MachO::CPU_TYPE_X86_64
},
111 { "Mc98000" , MachO::CPU_TYPE_MC98000
},
112 { "Arm" , MachO::CPU_TYPE_ARM
},
113 { "Arm64" , MachO::CPU_TYPE_ARM64
},
114 { "Sparc" , MachO::CPU_TYPE_SPARC
},
115 { "PowerPC" , MachO::CPU_TYPE_POWERPC
},
116 { "PowerPC64" , MachO::CPU_TYPE_POWERPC64
},
119 static const EnumEntry
<uint32_t> MachOHeaderCpuSubtypesX86
[] = {
120 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_I386_ALL
),
121 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_386
),
122 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_486
),
123 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_486SX
),
124 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_586
),
125 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_PENTPRO
),
126 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_PENTII_M3
),
127 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_PENTII_M5
),
128 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_CELERON
),
129 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_CELERON_MOBILE
),
130 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_PENTIUM_3
),
131 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_PENTIUM_3_M
),
132 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_PENTIUM_3_XEON
),
133 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_PENTIUM_M
),
134 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_PENTIUM_4
),
135 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_PENTIUM_4_M
),
136 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_ITANIUM
),
137 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_ITANIUM_2
),
138 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_XEON
),
139 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_XEON_MP
),
142 static const EnumEntry
<uint32_t> MachOHeaderCpuSubtypesX64
[] = {
143 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_X86_64_ALL
),
144 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_X86_ARCH1
),
145 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_X86_64_H
),
148 static const EnumEntry
<uint32_t> MachOHeaderCpuSubtypesARM
[] = {
149 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_ARM_ALL
),
150 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_ARM_V4T
),
151 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_ARM_V6
),
152 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_ARM_V5
),
153 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_ARM_V5TEJ
),
154 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_ARM_XSCALE
),
155 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_ARM_V7
),
156 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_ARM_V7S
),
157 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_ARM_V7K
),
158 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_ARM_V6M
),
159 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_ARM_V7M
),
160 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_ARM_V7EM
),
163 static const EnumEntry
<uint32_t> MachOHeaderCpuSubtypesARM64
[] = {
164 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_ARM64_ALL
),
165 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_ARM64E
),
168 static const EnumEntry
<uint32_t> MachOHeaderCpuSubtypesSPARC
[] = {
169 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_SPARC_ALL
),
172 static const EnumEntry
<uint32_t> MachOHeaderCpuSubtypesPPC
[] = {
173 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_POWERPC_ALL
),
174 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_POWERPC_601
),
175 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_POWERPC_602
),
176 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_POWERPC_603
),
177 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_POWERPC_603e
),
178 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_POWERPC_603ev
),
179 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_POWERPC_604
),
180 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_POWERPC_604e
),
181 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_POWERPC_620
),
182 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_POWERPC_750
),
183 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_POWERPC_7400
),
184 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_POWERPC_7450
),
185 LLVM_READOBJ_ENUM_ENT(MachO
, CPU_SUBTYPE_POWERPC_970
),
188 static const EnumEntry
<uint32_t> MachOHeaderFlags
[] = {
189 LLVM_READOBJ_ENUM_ENT(MachO
, MH_NOUNDEFS
),
190 LLVM_READOBJ_ENUM_ENT(MachO
, MH_INCRLINK
),
191 LLVM_READOBJ_ENUM_ENT(MachO
, MH_DYLDLINK
),
192 LLVM_READOBJ_ENUM_ENT(MachO
, MH_BINDATLOAD
),
193 LLVM_READOBJ_ENUM_ENT(MachO
, MH_PREBOUND
),
194 LLVM_READOBJ_ENUM_ENT(MachO
, MH_SPLIT_SEGS
),
195 LLVM_READOBJ_ENUM_ENT(MachO
, MH_LAZY_INIT
),
196 LLVM_READOBJ_ENUM_ENT(MachO
, MH_TWOLEVEL
),
197 LLVM_READOBJ_ENUM_ENT(MachO
, MH_FORCE_FLAT
),
198 LLVM_READOBJ_ENUM_ENT(MachO
, MH_NOMULTIDEFS
),
199 LLVM_READOBJ_ENUM_ENT(MachO
, MH_NOFIXPREBINDING
),
200 LLVM_READOBJ_ENUM_ENT(MachO
, MH_PREBINDABLE
),
201 LLVM_READOBJ_ENUM_ENT(MachO
, MH_ALLMODSBOUND
),
202 LLVM_READOBJ_ENUM_ENT(MachO
, MH_SUBSECTIONS_VIA_SYMBOLS
),
203 LLVM_READOBJ_ENUM_ENT(MachO
, MH_CANONICAL
),
204 LLVM_READOBJ_ENUM_ENT(MachO
, MH_WEAK_DEFINES
),
205 LLVM_READOBJ_ENUM_ENT(MachO
, MH_BINDS_TO_WEAK
),
206 LLVM_READOBJ_ENUM_ENT(MachO
, MH_ALLOW_STACK_EXECUTION
),
207 LLVM_READOBJ_ENUM_ENT(MachO
, MH_ROOT_SAFE
),
208 LLVM_READOBJ_ENUM_ENT(MachO
, MH_SETUID_SAFE
),
209 LLVM_READOBJ_ENUM_ENT(MachO
, MH_NO_REEXPORTED_DYLIBS
),
210 LLVM_READOBJ_ENUM_ENT(MachO
, MH_PIE
),
211 LLVM_READOBJ_ENUM_ENT(MachO
, MH_DEAD_STRIPPABLE_DYLIB
),
212 LLVM_READOBJ_ENUM_ENT(MachO
, MH_HAS_TLV_DESCRIPTORS
),
213 LLVM_READOBJ_ENUM_ENT(MachO
, MH_NO_HEAP_EXECUTION
),
214 LLVM_READOBJ_ENUM_ENT(MachO
, MH_APP_EXTENSION_SAFE
),
217 static const EnumEntry
<unsigned> MachOSectionAttributes
[] = {
218 { "LocReloc" , 1 << 0 /*S_ATTR_LOC_RELOC */ },
219 { "ExtReloc" , 1 << 1 /*S_ATTR_EXT_RELOC */ },
220 { "SomeInstructions" , 1 << 2 /*S_ATTR_SOME_INSTRUCTIONS */ },
221 { "Debug" , 1 << 17 /*S_ATTR_DEBUG */ },
222 { "SelfModifyingCode", 1 << 18 /*S_ATTR_SELF_MODIFYING_CODE*/ },
223 { "LiveSupport" , 1 << 19 /*S_ATTR_LIVE_SUPPORT */ },
224 { "NoDeadStrip" , 1 << 20 /*S_ATTR_NO_DEAD_STRIP */ },
225 { "StripStaticSyms" , 1 << 21 /*S_ATTR_STRIP_STATIC_SYMS */ },
226 { "NoTOC" , 1 << 22 /*S_ATTR_NO_TOC */ },
227 { "PureInstructions" , 1 << 23 /*S_ATTR_PURE_INSTRUCTIONS */ },
230 static const EnumEntry
<unsigned> MachOSymbolRefTypes
[] = {
231 { "UndefinedNonLazy", 0 },
232 { "ReferenceFlagUndefinedLazy", 1 },
233 { "ReferenceFlagDefined", 2 },
234 { "ReferenceFlagPrivateDefined", 3 },
235 { "ReferenceFlagPrivateUndefinedNonLazy", 4 },
236 { "ReferenceFlagPrivateUndefinedLazy", 5 }
239 static const EnumEntry
<unsigned> MachOSymbolFlags
[] = {
240 { "ReferencedDynamically", 0x10 },
241 { "NoDeadStrip", 0x20 },
244 { "AltEntry", 0x200 },
247 static const EnumEntry
<unsigned> MachOSymbolTypes
[] = {
251 { "PreboundUndef", 0xC },
256 struct MachOSection
{
258 ArrayRef
<char> SegmentName
;
263 uint32_t RelocationTableOffset
;
264 uint32_t NumRelocationTableEntries
;
271 struct MachOSegment
{
286 uint32_t StringIndex
;
288 uint8_t SectionIndex
;
294 static std::string
getMask(uint32_t prot
)
296 // TODO (davide): This always assumes prot is valid.
297 // Catch mistakes and report if needed.
300 Prot
+= (prot
& MachO::VM_PROT_READ
) ? "r" : "-";
301 Prot
+= (prot
& MachO::VM_PROT_WRITE
) ? "w" : "-";
302 Prot
+= (prot
& MachO::VM_PROT_EXECUTE
) ? "x" : "-";
306 static void getSection(const MachOObjectFile
*Obj
,
308 MachOSection
&Section
) {
309 if (!Obj
->is64Bit()) {
310 MachO::section Sect
= Obj
->getSection(Sec
);
311 Section
.Address
= Sect
.addr
;
312 Section
.Size
= Sect
.size
;
313 Section
.Offset
= Sect
.offset
;
314 Section
.Alignment
= Sect
.align
;
315 Section
.RelocationTableOffset
= Sect
.reloff
;
316 Section
.NumRelocationTableEntries
= Sect
.nreloc
;
317 Section
.Flags
= Sect
.flags
;
318 Section
.Reserved1
= Sect
.reserved1
;
319 Section
.Reserved2
= Sect
.reserved2
;
322 MachO::section_64 Sect
= Obj
->getSection64(Sec
);
323 Section
.Address
= Sect
.addr
;
324 Section
.Size
= Sect
.size
;
325 Section
.Offset
= Sect
.offset
;
326 Section
.Alignment
= Sect
.align
;
327 Section
.RelocationTableOffset
= Sect
.reloff
;
328 Section
.NumRelocationTableEntries
= Sect
.nreloc
;
329 Section
.Flags
= Sect
.flags
;
330 Section
.Reserved1
= Sect
.reserved1
;
331 Section
.Reserved2
= Sect
.reserved2
;
332 Section
.Reserved3
= Sect
.reserved3
;
335 static void getSegment(const MachOObjectFile
*Obj
,
336 const MachOObjectFile::LoadCommandInfo
&L
,
337 MachOSegment
&Segment
) {
338 if (!Obj
->is64Bit()) {
339 MachO::segment_command SC
= Obj
->getSegmentLoadCommand(L
);
340 Segment
.CmdName
= "LC_SEGMENT";
341 Segment
.SegName
= SC
.segname
;
342 Segment
.cmdsize
= SC
.cmdsize
;
343 Segment
.vmaddr
= SC
.vmaddr
;
344 Segment
.vmsize
= SC
.vmsize
;
345 Segment
.fileoff
= SC
.fileoff
;
346 Segment
.filesize
= SC
.filesize
;
347 Segment
.maxprot
= SC
.maxprot
;
348 Segment
.initprot
= SC
.initprot
;
349 Segment
.nsects
= SC
.nsects
;
350 Segment
.flags
= SC
.flags
;
353 MachO::segment_command_64 SC
= Obj
->getSegment64LoadCommand(L
);
354 Segment
.CmdName
= "LC_SEGMENT_64";
355 Segment
.SegName
= SC
.segname
;
356 Segment
.cmdsize
= SC
.cmdsize
;
357 Segment
.vmaddr
= SC
.vmaddr
;
358 Segment
.vmsize
= SC
.vmsize
;
359 Segment
.fileoff
= SC
.fileoff
;
360 Segment
.filesize
= SC
.filesize
;
361 Segment
.maxprot
= SC
.maxprot
;
362 Segment
.initprot
= SC
.initprot
;
363 Segment
.nsects
= SC
.nsects
;
364 Segment
.flags
= SC
.flags
;
367 static void getSymbol(const MachOObjectFile
*Obj
,
369 MachOSymbol
&Symbol
) {
370 if (!Obj
->is64Bit()) {
371 MachO::nlist Entry
= Obj
->getSymbolTableEntry(DRI
);
372 Symbol
.StringIndex
= Entry
.n_strx
;
373 Symbol
.Type
= Entry
.n_type
;
374 Symbol
.SectionIndex
= Entry
.n_sect
;
375 Symbol
.Flags
= Entry
.n_desc
;
376 Symbol
.Value
= Entry
.n_value
;
379 MachO::nlist_64 Entry
= Obj
->getSymbol64TableEntry(DRI
);
380 Symbol
.StringIndex
= Entry
.n_strx
;
381 Symbol
.Type
= Entry
.n_type
;
382 Symbol
.SectionIndex
= Entry
.n_sect
;
383 Symbol
.Flags
= Entry
.n_desc
;
384 Symbol
.Value
= Entry
.n_value
;
387 void MachODumper::printFileHeaders() {
388 DictScope
H(W
, "MachHeader");
389 if (!Obj
->is64Bit()) {
390 printFileHeaders(Obj
->getHeader());
392 printFileHeaders(Obj
->getHeader64());
393 W
.printHex("Reserved", Obj
->getHeader64().reserved
);
397 template<class MachHeader
>
398 void MachODumper::printFileHeaders(const MachHeader
&Header
) {
399 W
.printEnum("Magic", Header
.magic
, makeArrayRef(MachOMagics
));
400 W
.printEnum("CpuType", Header
.cputype
, makeArrayRef(MachOHeaderCpuTypes
));
401 uint32_t subtype
= Header
.cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
;
402 switch (Header
.cputype
) {
403 case MachO::CPU_TYPE_X86
:
404 W
.printEnum("CpuSubType", subtype
, makeArrayRef(MachOHeaderCpuSubtypesX86
));
406 case MachO::CPU_TYPE_X86_64
:
407 W
.printEnum("CpuSubType", subtype
, makeArrayRef(MachOHeaderCpuSubtypesX64
));
409 case MachO::CPU_TYPE_ARM
:
410 W
.printEnum("CpuSubType", subtype
, makeArrayRef(MachOHeaderCpuSubtypesARM
));
412 case MachO::CPU_TYPE_POWERPC
:
413 W
.printEnum("CpuSubType", subtype
, makeArrayRef(MachOHeaderCpuSubtypesPPC
));
415 case MachO::CPU_TYPE_SPARC
:
416 W
.printEnum("CpuSubType", subtype
, makeArrayRef(MachOHeaderCpuSubtypesSPARC
));
418 case MachO::CPU_TYPE_ARM64
:
419 W
.printEnum("CpuSubType", subtype
, makeArrayRef(MachOHeaderCpuSubtypesARM64
));
421 case MachO::CPU_TYPE_POWERPC64
:
423 W
.printHex("CpuSubtype", subtype
);
425 W
.printEnum("FileType", Header
.filetype
, makeArrayRef(MachOHeaderFileTypes
));
426 W
.printNumber("NumOfLoadCommands", Header
.ncmds
);
427 W
.printNumber("SizeOfLoadCommands", Header
.sizeofcmds
);
428 W
.printFlags("Flags", Header
.flags
, makeArrayRef(MachOHeaderFlags
));
431 void MachODumper::printSectionHeaders() { return printSectionHeaders(Obj
); }
433 void MachODumper::printSectionHeaders(const MachOObjectFile
*Obj
) {
434 ListScope
Group(W
, "Sections");
436 int SectionIndex
= -1;
437 for (const SectionRef
&Section
: Obj
->sections()) {
440 MachOSection MOSection
;
441 getSection(Obj
, Section
.getRawDataRefImpl(), MOSection
);
442 DataRefImpl DR
= Section
.getRawDataRefImpl();
445 error(Section
.getName(Name
));
447 ArrayRef
<char> RawName
= Obj
->getSectionRawName(DR
);
448 StringRef SegmentName
= Obj
->getSectionFinalSegmentName(DR
);
449 ArrayRef
<char> RawSegmentName
= Obj
->getSectionRawFinalSegmentName(DR
);
451 DictScope
SectionD(W
, "Section");
452 W
.printNumber("Index", SectionIndex
);
453 W
.printBinary("Name", Name
, RawName
);
454 W
.printBinary("Segment", SegmentName
, RawSegmentName
);
455 W
.printHex("Address", MOSection
.Address
);
456 W
.printHex("Size", MOSection
.Size
);
457 W
.printNumber("Offset", MOSection
.Offset
);
458 W
.printNumber("Alignment", MOSection
.Alignment
);
459 W
.printHex("RelocationOffset", MOSection
.RelocationTableOffset
);
460 W
.printNumber("RelocationCount", MOSection
.NumRelocationTableEntries
);
461 W
.printEnum("Type", MOSection
.Flags
& 0xFF,
462 makeArrayRef(MachOSectionAttributes
));
463 W
.printFlags("Attributes", MOSection
.Flags
>> 8,
464 makeArrayRef(MachOSectionAttributes
));
465 W
.printHex("Reserved1", MOSection
.Reserved1
);
466 W
.printHex("Reserved2", MOSection
.Reserved2
);
468 W
.printHex("Reserved3", MOSection
.Reserved3
);
470 if (opts::SectionRelocations
) {
471 ListScope
D(W
, "Relocations");
472 for (const RelocationRef
&Reloc
: Section
.relocations())
473 printRelocation(Reloc
);
476 if (opts::SectionSymbols
) {
477 ListScope
D(W
, "Symbols");
478 for (const SymbolRef
&Symbol
: Obj
->symbols()) {
479 if (!Section
.containsSymbol(Symbol
))
486 if (opts::SectionData
&& !Section
.isBSS())
487 W
.printBinaryBlock("SectionData", unwrapOrError(Obj
->getFileName(),
488 Section
.getContents()));
492 void MachODumper::printRelocations() {
493 ListScope
D(W
, "Relocations");
496 for (const SectionRef
&Section
: Obj
->sections()) {
498 error(Section
.getName(Name
));
500 bool PrintedGroup
= false;
501 for (const RelocationRef
&Reloc
: Section
.relocations()) {
503 W
.startLine() << "Section " << Name
<< " {\n";
508 printRelocation(Reloc
);
513 W
.startLine() << "}\n";
518 void MachODumper::printRelocation(const RelocationRef
&Reloc
) {
519 return printRelocation(Obj
, Reloc
);
522 void MachODumper::printRelocation(const MachOObjectFile
*Obj
,
523 const RelocationRef
&Reloc
) {
524 uint64_t Offset
= Reloc
.getOffset();
525 SmallString
<32> RelocName
;
526 Reloc
.getTypeName(RelocName
);
528 DataRefImpl DR
= Reloc
.getRawDataRefImpl();
529 MachO::any_relocation_info RE
= Obj
->getRelocation(DR
);
530 bool IsScattered
= Obj
->isRelocationScattered(RE
);
531 bool IsExtern
= !IsScattered
&& Obj
->getPlainRelocationExternal(RE
);
533 StringRef TargetName
;
535 symbol_iterator Symbol
= Reloc
.getSymbol();
536 if (Symbol
!= Obj
->symbol_end()) {
537 Expected
<StringRef
> TargetNameOrErr
= Symbol
->getName();
538 if (!TargetNameOrErr
)
539 error(errorToErrorCode(TargetNameOrErr
.takeError()));
540 TargetName
= *TargetNameOrErr
;
542 } else if (!IsScattered
) {
543 section_iterator SecI
= Obj
->getRelocationSection(DR
);
544 if (SecI
!= Obj
->section_end()) {
545 error(SecI
->getName(TargetName
));
548 if (TargetName
.empty())
551 if (opts::ExpandRelocs
) {
552 DictScope
Group(W
, "Relocation");
553 W
.printHex("Offset", Offset
);
554 W
.printNumber("PCRel", Obj
->getAnyRelocationPCRel(RE
));
555 W
.printNumber("Length", Obj
->getAnyRelocationLength(RE
));
556 W
.printNumber("Type", RelocName
, Obj
->getAnyRelocationType(RE
));
558 W
.printHex("Value", Obj
->getScatteredRelocationValue(RE
));
560 const char *Kind
= IsExtern
? "Symbol" : "Section";
561 W
.printNumber(Kind
, TargetName
, Obj
->getPlainRelocationSymbolNum(RE
));
564 SmallString
<32> SymbolNameOrOffset("0x");
566 // Scattered relocations don't really have an associated symbol for some
567 // reason, even if one exists in the symtab at the correct address.
568 SymbolNameOrOffset
+= utohexstr(Obj
->getScatteredRelocationValue(RE
));
570 SymbolNameOrOffset
= TargetName
;
573 raw_ostream
& OS
= W
.startLine();
575 << " " << Obj
->getAnyRelocationPCRel(RE
)
576 << " " << Obj
->getAnyRelocationLength(RE
);
580 OS
<< " " << Obj
->getPlainRelocationExternal(RE
);
581 OS
<< " " << RelocName
582 << " " << IsScattered
583 << " " << SymbolNameOrOffset
588 void MachODumper::printSymbols() {
589 ListScope
Group(W
, "Symbols");
591 for (const SymbolRef
&Symbol
: Obj
->symbols()) {
596 void MachODumper::printDynamicSymbols() {
597 ListScope
Group(W
, "DynamicSymbols");
600 void MachODumper::printSymbol(const SymbolRef
&Symbol
) {
601 StringRef SymbolName
;
602 Expected
<StringRef
> SymbolNameOrErr
= Symbol
.getName();
603 if (!SymbolNameOrErr
) {
604 // TODO: Actually report errors helpfully.
605 consumeError(SymbolNameOrErr
.takeError());
607 SymbolName
= *SymbolNameOrErr
;
609 MachOSymbol MOSymbol
;
610 getSymbol(Obj
, Symbol
.getRawDataRefImpl(), MOSymbol
);
612 StringRef SectionName
= "";
613 Expected
<section_iterator
> SecIOrErr
= Symbol
.getSection();
614 error(errorToErrorCode(SecIOrErr
.takeError()));
615 section_iterator SecI
= *SecIOrErr
;
616 if (SecI
!= Obj
->section_end())
617 error(SecI
->getName(SectionName
));
619 DictScope
D(W
, "Symbol");
620 W
.printNumber("Name", SymbolName
, MOSymbol
.StringIndex
);
621 if (MOSymbol
.Type
& MachO::N_STAB
) {
622 W
.printHex("Type", "SymDebugTable", MOSymbol
.Type
);
624 if (MOSymbol
.Type
& MachO::N_PEXT
)
625 W
.startLine() << "PrivateExtern\n";
626 if (MOSymbol
.Type
& MachO::N_EXT
)
627 W
.startLine() << "Extern\n";
628 W
.printEnum("Type", uint8_t(MOSymbol
.Type
& MachO::N_TYPE
),
629 makeArrayRef(MachOSymbolTypes
));
631 W
.printHex("Section", SectionName
, MOSymbol
.SectionIndex
);
632 W
.printEnum("RefType", static_cast<uint16_t>(MOSymbol
.Flags
& 0xF),
633 makeArrayRef(MachOSymbolRefTypes
));
634 W
.printFlags("Flags", static_cast<uint16_t>(MOSymbol
.Flags
& ~0xF),
635 makeArrayRef(MachOSymbolFlags
));
636 W
.printHex("Value", MOSymbol
.Value
);
639 void MachODumper::printUnwindInfo() {
640 W
.startLine() << "UnwindInfo not implemented.\n";
643 void MachODumper::printStackMap() const {
644 object::SectionRef StackMapSection
;
645 for (auto Sec
: Obj
->sections()) {
648 if (Name
== "__llvm_stackmaps") {
649 StackMapSection
= Sec
;
654 if (StackMapSection
== object::SectionRef())
657 StringRef StackMapContents
=
658 unwrapOrError(Obj
->getFileName(), StackMapSection
.getContents());
659 ArrayRef
<uint8_t> StackMapContentsArray
=
660 arrayRefFromStringRef(StackMapContents
);
662 if (Obj
->isLittleEndian())
664 W
, StackMapParser
<support::little
>(StackMapContentsArray
));
667 W
, StackMapParser
<support::big
>(StackMapContentsArray
));
670 void MachODumper::printNeededLibraries() {
671 ListScope
D(W
, "NeededLibraries");
673 using LibsTy
= std::vector
<StringRef
>;
676 for (const auto &Command
: Obj
->load_commands()) {
677 if (Command
.C
.cmd
== MachO::LC_LOAD_DYLIB
||
678 Command
.C
.cmd
== MachO::LC_ID_DYLIB
||
679 Command
.C
.cmd
== MachO::LC_LOAD_WEAK_DYLIB
||
680 Command
.C
.cmd
== MachO::LC_REEXPORT_DYLIB
||
681 Command
.C
.cmd
== MachO::LC_LAZY_LOAD_DYLIB
||
682 Command
.C
.cmd
== MachO::LC_LOAD_UPWARD_DYLIB
) {
683 MachO::dylib_command Dl
= Obj
->getDylibIDLoadCommand(Command
);
684 if (Dl
.dylib
.name
< Dl
.cmdsize
) {
685 auto *P
= static_cast<const char*>(Command
.Ptr
) + Dl
.dylib
.name
;
691 llvm::stable_sort(Libs
);
693 for (const auto &L
: Libs
) {
694 W
.startLine() << L
<< "\n";
698 void MachODumper::printMachODataInCode() {
699 for (const auto &Load
: Obj
->load_commands()) {
700 if (Load
.C
.cmd
== MachO::LC_DATA_IN_CODE
) {
701 MachO::linkedit_data_command LLC
= Obj
->getLinkeditDataLoadCommand(Load
);
702 DictScope
Group(W
, "DataInCode");
703 W
.printNumber("Data offset", LLC
.dataoff
);
704 W
.printNumber("Data size", LLC
.datasize
);
705 ListScope
D(W
, "Data entries");
706 unsigned NumRegions
= LLC
.datasize
/ sizeof(MachO::data_in_code_entry
);
707 for (unsigned i
= 0; i
< NumRegions
; ++i
) {
708 MachO::data_in_code_entry DICE
= Obj
->getDataInCodeTableEntry(
710 DictScope
Group(W
, "Entry");
711 W
.printNumber("Index", i
);
712 W
.printNumber("Offset", DICE
.offset
);
713 W
.printNumber("Length", DICE
.length
);
714 W
.printNumber("Kind", DICE
.kind
);
720 void MachODumper::printMachOVersionMin() {
721 for (const auto &Load
: Obj
->load_commands()) {
723 switch (Load
.C
.cmd
) {
724 case MachO::LC_VERSION_MIN_MACOSX
:
725 Cmd
= "LC_VERSION_MIN_MACOSX";
727 case MachO::LC_VERSION_MIN_IPHONEOS
:
728 Cmd
= "LC_VERSION_MIN_IPHONEOS";
730 case MachO::LC_VERSION_MIN_TVOS
:
731 Cmd
= "LC_VERSION_MIN_TVOS";
733 case MachO::LC_VERSION_MIN_WATCHOS
:
734 Cmd
= "LC_VERSION_MIN_WATCHOS";
736 case MachO::LC_BUILD_VERSION
:
737 Cmd
= "LC_BUILD_VERSION";
743 DictScope
Group(W
, "MinVersion");
744 // Handle LC_BUILD_VERSION.
745 if (Load
.C
.cmd
== MachO::LC_BUILD_VERSION
) {
746 MachO::build_version_command BVC
= Obj
->getBuildVersionLoadCommand(Load
);
747 W
.printString("Cmd", Cmd
);
748 W
.printNumber("Size", BVC
.cmdsize
);
749 W
.printString("Platform",
750 MachOObjectFile::getBuildPlatform(BVC
.platform
));
751 W
.printString("Version", MachOObjectFile::getVersionString(BVC
.minos
));
753 W
.printString("SDK", MachOObjectFile::getVersionString(BVC
.sdk
));
755 W
.printString("SDK", StringRef("n/a"));
759 MachO::version_min_command VMC
= Obj
->getVersionMinLoadCommand(Load
);
760 W
.printString("Cmd", Cmd
);
761 W
.printNumber("Size", VMC
.cmdsize
);
762 SmallString
<32> Version
;
763 Version
= utostr(MachOObjectFile::getVersionMinMajor(VMC
, false)) + "." +
764 utostr(MachOObjectFile::getVersionMinMinor(VMC
, false));
765 uint32_t Update
= MachOObjectFile::getVersionMinUpdate(VMC
, false);
767 Version
+= "." + utostr(MachOObjectFile::getVersionMinUpdate(VMC
, false));
768 W
.printString("Version", Version
);
773 SDK
= utostr(MachOObjectFile::getVersionMinMajor(VMC
, true)) + "." +
774 utostr(MachOObjectFile::getVersionMinMinor(VMC
, true));
775 uint32_t Update
= MachOObjectFile::getVersionMinUpdate(VMC
, true);
777 SDK
+= "." + utostr(MachOObjectFile::getVersionMinUpdate(VMC
, true));
779 W
.printString("SDK", SDK
);
783 void MachODumper::printMachODysymtab() {
784 for (const auto &Load
: Obj
->load_commands()) {
785 if (Load
.C
.cmd
== MachO::LC_DYSYMTAB
) {
786 MachO::dysymtab_command DLC
= Obj
->getDysymtabLoadCommand();
787 DictScope
Group(W
, "Dysymtab");
788 W
.printNumber("ilocalsym", DLC
.ilocalsym
);
789 W
.printNumber("nlocalsym", DLC
.nlocalsym
);
790 W
.printNumber("iextdefsym", DLC
.iextdefsym
);
791 W
.printNumber("nextdefsym", DLC
.nextdefsym
);
792 W
.printNumber("iundefsym", DLC
.iundefsym
);
793 W
.printNumber("nundefsym", DLC
.nundefsym
);
794 W
.printNumber("tocoff", DLC
.tocoff
);
795 W
.printNumber("ntoc", DLC
.ntoc
);
796 W
.printNumber("modtaboff", DLC
.modtaboff
);
797 W
.printNumber("nmodtab", DLC
.nmodtab
);
798 W
.printNumber("extrefsymoff", DLC
.extrefsymoff
);
799 W
.printNumber("nextrefsyms", DLC
.nextrefsyms
);
800 W
.printNumber("indirectsymoff", DLC
.indirectsymoff
);
801 W
.printNumber("nindirectsyms", DLC
.nindirectsyms
);
802 W
.printNumber("extreloff", DLC
.extreloff
);
803 W
.printNumber("nextrel", DLC
.nextrel
);
804 W
.printNumber("locreloff", DLC
.locreloff
);
805 W
.printNumber("nlocrel", DLC
.nlocrel
);
810 void MachODumper::printMachOSegment() {
811 for (const auto &Load
: Obj
->load_commands()) {
812 if (Load
.C
.cmd
== MachO::LC_SEGMENT
|| Load
.C
.cmd
== MachO::LC_SEGMENT_64
) {
813 MachOSegment MOSegment
;
814 getSegment(Obj
, Load
, MOSegment
);
815 DictScope
Group(W
, "Segment");
816 W
.printString("Cmd", MOSegment
.CmdName
);
817 W
.printString("Name", MOSegment
.SegName
);
818 W
.printNumber("Size", MOSegment
.cmdsize
);
819 W
.printHex("vmaddr", MOSegment
.vmaddr
);
820 W
.printHex("vmsize", MOSegment
.vmsize
);
821 W
.printNumber("fileoff", MOSegment
.fileoff
);
822 W
.printNumber("filesize", MOSegment
.filesize
);
823 W
.printString("maxprot", getMask(MOSegment
.maxprot
));
824 W
.printString("initprot", getMask(MOSegment
.initprot
));
825 W
.printNumber("nsects", MOSegment
.nsects
);
826 W
.printHex("flags", MOSegment
.flags
);
831 void MachODumper::printMachOIndirectSymbols() {
832 for (const auto &Load
: Obj
->load_commands()) {
833 if (Load
.C
.cmd
== MachO::LC_DYSYMTAB
) {
834 MachO::dysymtab_command DLC
= Obj
->getDysymtabLoadCommand();
835 DictScope
Group(W
, "Indirect Symbols");
836 W
.printNumber("Number", DLC
.nindirectsyms
);
837 ListScope
D(W
, "Symbols");
838 for (unsigned i
= 0; i
< DLC
.nindirectsyms
; ++i
) {
839 DictScope
Group(W
, "Entry");
840 W
.printNumber("Entry Index", i
);
841 W
.printHex("Symbol Index", Obj
->getIndirectSymbolTableEntry(DLC
, i
));
847 void MachODumper::printMachOLinkerOptions() {
848 for (const auto &Load
: Obj
->load_commands()) {
849 if (Load
.C
.cmd
== MachO::LC_LINKER_OPTION
) {
850 MachO::linker_option_command LOLC
= Obj
->getLinkerOptionLoadCommand(Load
);
851 DictScope
Group(W
, "Linker Options");
852 W
.printNumber("Size", LOLC
.cmdsize
);
853 ListScope
D(W
, "Strings");
854 uint64_t DataSize
= LOLC
.cmdsize
- sizeof(MachO::linker_option_command
);
855 const char *P
= Load
.Ptr
+ sizeof(MachO::linker_option_command
);
856 StringRef
Data(P
, DataSize
);
857 for (unsigned i
= 0; i
< LOLC
.count
; ++i
) {
858 std::pair
<StringRef
,StringRef
> Split
= Data
.split('\0');
859 W
.printString("Value", Split
.first
);