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> MachOSectionTypes
[] = {
218 { "Regular" , MachO::S_REGULAR
},
219 { "ZeroFill" , MachO::S_ZEROFILL
},
220 { "CStringLiterals" , MachO::S_CSTRING_LITERALS
},
221 { "4ByteLiterals" , MachO::S_4BYTE_LITERALS
},
222 { "8ByteLiterals" , MachO::S_8BYTE_LITERALS
},
223 { "LiteralPointers" , MachO::S_LITERAL_POINTERS
},
224 { "NonLazySymbolPointers" , MachO::S_NON_LAZY_SYMBOL_POINTERS
},
225 { "LazySymbolPointers" , MachO::S_LAZY_SYMBOL_POINTERS
},
226 { "SymbolStubs" , MachO::S_SYMBOL_STUBS
},
227 { "ModInitFuncPointers" , MachO::S_MOD_INIT_FUNC_POINTERS
},
228 { "ModTermFuncPointers" , MachO::S_MOD_TERM_FUNC_POINTERS
},
229 { "Coalesced" , MachO::S_COALESCED
},
230 { "GBZeroFill" , MachO::S_GB_ZEROFILL
},
231 { "Interposing" , MachO::S_INTERPOSING
},
232 { "16ByteLiterals" , MachO::S_16BYTE_LITERALS
},
233 { "DTraceDOF" , MachO::S_DTRACE_DOF
},
234 { "LazyDylibSymbolPointers" , MachO::S_LAZY_DYLIB_SYMBOL_POINTERS
},
235 { "ThreadLocalRegular" , MachO::S_THREAD_LOCAL_REGULAR
},
236 { "ThreadLocalZerofill" , MachO::S_THREAD_LOCAL_ZEROFILL
},
237 { "ThreadLocalVariables" , MachO::S_THREAD_LOCAL_VARIABLES
},
238 { "ThreadLocalVariablePointers" , MachO::S_THREAD_LOCAL_VARIABLE_POINTERS
},
239 { "ThreadLocalInitFunctionPointers", MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
}
242 static const EnumEntry
<unsigned> MachOSectionAttributes
[] = {
243 { "LocReloc" , 1 << 0 /*S_ATTR_LOC_RELOC */ },
244 { "ExtReloc" , 1 << 1 /*S_ATTR_EXT_RELOC */ },
245 { "SomeInstructions" , 1 << 2 /*S_ATTR_SOME_INSTRUCTIONS */ },
246 { "Debug" , 1 << 17 /*S_ATTR_DEBUG */ },
247 { "SelfModifyingCode", 1 << 18 /*S_ATTR_SELF_MODIFYING_CODE*/ },
248 { "LiveSupport" , 1 << 19 /*S_ATTR_LIVE_SUPPORT */ },
249 { "NoDeadStrip" , 1 << 20 /*S_ATTR_NO_DEAD_STRIP */ },
250 { "StripStaticSyms" , 1 << 21 /*S_ATTR_STRIP_STATIC_SYMS */ },
251 { "NoTOC" , 1 << 22 /*S_ATTR_NO_TOC */ },
252 { "PureInstructions" , 1 << 23 /*S_ATTR_PURE_INSTRUCTIONS */ },
255 static const EnumEntry
<unsigned> MachOSymbolRefTypes
[] = {
256 { "UndefinedNonLazy", 0 },
257 { "ReferenceFlagUndefinedLazy", 1 },
258 { "ReferenceFlagDefined", 2 },
259 { "ReferenceFlagPrivateDefined", 3 },
260 { "ReferenceFlagPrivateUndefinedNonLazy", 4 },
261 { "ReferenceFlagPrivateUndefinedLazy", 5 }
264 static const EnumEntry
<unsigned> MachOSymbolFlags
[] = {
265 { "ReferencedDynamically", 0x10 },
266 { "NoDeadStrip", 0x20 },
269 { "AltEntry", 0x200 },
272 static const EnumEntry
<unsigned> MachOSymbolTypes
[] = {
276 { "PreboundUndef", 0xC },
281 struct MachOSection
{
283 ArrayRef
<char> SegmentName
;
288 uint32_t RelocationTableOffset
;
289 uint32_t NumRelocationTableEntries
;
296 struct MachOSegment
{
311 uint32_t StringIndex
;
313 uint8_t SectionIndex
;
319 static std::string
getMask(uint32_t prot
)
321 // TODO (davide): This always assumes prot is valid.
322 // Catch mistakes and report if needed.
325 Prot
+= (prot
& MachO::VM_PROT_READ
) ? "r" : "-";
326 Prot
+= (prot
& MachO::VM_PROT_WRITE
) ? "w" : "-";
327 Prot
+= (prot
& MachO::VM_PROT_EXECUTE
) ? "x" : "-";
331 static void getSection(const MachOObjectFile
*Obj
,
333 MachOSection
&Section
) {
334 if (!Obj
->is64Bit()) {
335 MachO::section Sect
= Obj
->getSection(Sec
);
336 Section
.Address
= Sect
.addr
;
337 Section
.Size
= Sect
.size
;
338 Section
.Offset
= Sect
.offset
;
339 Section
.Alignment
= Sect
.align
;
340 Section
.RelocationTableOffset
= Sect
.reloff
;
341 Section
.NumRelocationTableEntries
= Sect
.nreloc
;
342 Section
.Flags
= Sect
.flags
;
343 Section
.Reserved1
= Sect
.reserved1
;
344 Section
.Reserved2
= Sect
.reserved2
;
347 MachO::section_64 Sect
= Obj
->getSection64(Sec
);
348 Section
.Address
= Sect
.addr
;
349 Section
.Size
= Sect
.size
;
350 Section
.Offset
= Sect
.offset
;
351 Section
.Alignment
= Sect
.align
;
352 Section
.RelocationTableOffset
= Sect
.reloff
;
353 Section
.NumRelocationTableEntries
= Sect
.nreloc
;
354 Section
.Flags
= Sect
.flags
;
355 Section
.Reserved1
= Sect
.reserved1
;
356 Section
.Reserved2
= Sect
.reserved2
;
357 Section
.Reserved3
= Sect
.reserved3
;
360 static void getSegment(const MachOObjectFile
*Obj
,
361 const MachOObjectFile::LoadCommandInfo
&L
,
362 MachOSegment
&Segment
) {
363 if (!Obj
->is64Bit()) {
364 MachO::segment_command SC
= Obj
->getSegmentLoadCommand(L
);
365 Segment
.CmdName
= "LC_SEGMENT";
366 Segment
.SegName
= SC
.segname
;
367 Segment
.cmdsize
= SC
.cmdsize
;
368 Segment
.vmaddr
= SC
.vmaddr
;
369 Segment
.vmsize
= SC
.vmsize
;
370 Segment
.fileoff
= SC
.fileoff
;
371 Segment
.filesize
= SC
.filesize
;
372 Segment
.maxprot
= SC
.maxprot
;
373 Segment
.initprot
= SC
.initprot
;
374 Segment
.nsects
= SC
.nsects
;
375 Segment
.flags
= SC
.flags
;
378 MachO::segment_command_64 SC
= Obj
->getSegment64LoadCommand(L
);
379 Segment
.CmdName
= "LC_SEGMENT_64";
380 Segment
.SegName
= SC
.segname
;
381 Segment
.cmdsize
= SC
.cmdsize
;
382 Segment
.vmaddr
= SC
.vmaddr
;
383 Segment
.vmsize
= SC
.vmsize
;
384 Segment
.fileoff
= SC
.fileoff
;
385 Segment
.filesize
= SC
.filesize
;
386 Segment
.maxprot
= SC
.maxprot
;
387 Segment
.initprot
= SC
.initprot
;
388 Segment
.nsects
= SC
.nsects
;
389 Segment
.flags
= SC
.flags
;
392 static void getSymbol(const MachOObjectFile
*Obj
,
394 MachOSymbol
&Symbol
) {
395 if (!Obj
->is64Bit()) {
396 MachO::nlist Entry
= Obj
->getSymbolTableEntry(DRI
);
397 Symbol
.StringIndex
= Entry
.n_strx
;
398 Symbol
.Type
= Entry
.n_type
;
399 Symbol
.SectionIndex
= Entry
.n_sect
;
400 Symbol
.Flags
= Entry
.n_desc
;
401 Symbol
.Value
= Entry
.n_value
;
404 MachO::nlist_64 Entry
= Obj
->getSymbol64TableEntry(DRI
);
405 Symbol
.StringIndex
= Entry
.n_strx
;
406 Symbol
.Type
= Entry
.n_type
;
407 Symbol
.SectionIndex
= Entry
.n_sect
;
408 Symbol
.Flags
= Entry
.n_desc
;
409 Symbol
.Value
= Entry
.n_value
;
412 void MachODumper::printFileHeaders() {
413 DictScope
H(W
, "MachHeader");
414 if (!Obj
->is64Bit()) {
415 printFileHeaders(Obj
->getHeader());
417 printFileHeaders(Obj
->getHeader64());
418 W
.printHex("Reserved", Obj
->getHeader64().reserved
);
422 template<class MachHeader
>
423 void MachODumper::printFileHeaders(const MachHeader
&Header
) {
424 W
.printEnum("Magic", Header
.magic
, makeArrayRef(MachOMagics
));
425 W
.printEnum("CpuType", Header
.cputype
, makeArrayRef(MachOHeaderCpuTypes
));
426 uint32_t subtype
= Header
.cpusubtype
& ~MachO::CPU_SUBTYPE_MASK
;
427 switch (Header
.cputype
) {
428 case MachO::CPU_TYPE_X86
:
429 W
.printEnum("CpuSubType", subtype
, makeArrayRef(MachOHeaderCpuSubtypesX86
));
431 case MachO::CPU_TYPE_X86_64
:
432 W
.printEnum("CpuSubType", subtype
, makeArrayRef(MachOHeaderCpuSubtypesX64
));
434 case MachO::CPU_TYPE_ARM
:
435 W
.printEnum("CpuSubType", subtype
, makeArrayRef(MachOHeaderCpuSubtypesARM
));
437 case MachO::CPU_TYPE_POWERPC
:
438 W
.printEnum("CpuSubType", subtype
, makeArrayRef(MachOHeaderCpuSubtypesPPC
));
440 case MachO::CPU_TYPE_SPARC
:
441 W
.printEnum("CpuSubType", subtype
, makeArrayRef(MachOHeaderCpuSubtypesSPARC
));
443 case MachO::CPU_TYPE_ARM64
:
444 W
.printEnum("CpuSubType", subtype
, makeArrayRef(MachOHeaderCpuSubtypesARM64
));
446 case MachO::CPU_TYPE_POWERPC64
:
448 W
.printHex("CpuSubtype", subtype
);
450 W
.printEnum("FileType", Header
.filetype
, makeArrayRef(MachOHeaderFileTypes
));
451 W
.printNumber("NumOfLoadCommands", Header
.ncmds
);
452 W
.printNumber("SizeOfLoadCommands", Header
.sizeofcmds
);
453 W
.printFlags("Flags", Header
.flags
, makeArrayRef(MachOHeaderFlags
));
456 void MachODumper::printSectionHeaders() { return printSectionHeaders(Obj
); }
458 void MachODumper::printSectionHeaders(const MachOObjectFile
*Obj
) {
459 ListScope
Group(W
, "Sections");
461 int SectionIndex
= -1;
462 for (const SectionRef
&Section
: Obj
->sections()) {
465 MachOSection MOSection
;
466 getSection(Obj
, Section
.getRawDataRefImpl(), MOSection
);
467 DataRefImpl DR
= Section
.getRawDataRefImpl();
468 StringRef Name
= unwrapOrError(Obj
->getFileName(), Section
.getName());
469 ArrayRef
<char> RawName
= Obj
->getSectionRawName(DR
);
470 StringRef SegmentName
= Obj
->getSectionFinalSegmentName(DR
);
471 ArrayRef
<char> RawSegmentName
= Obj
->getSectionRawFinalSegmentName(DR
);
473 DictScope
SectionD(W
, "Section");
474 W
.printNumber("Index", SectionIndex
);
475 W
.printBinary("Name", Name
, RawName
);
476 W
.printBinary("Segment", SegmentName
, RawSegmentName
);
477 W
.printHex("Address", MOSection
.Address
);
478 W
.printHex("Size", MOSection
.Size
);
479 W
.printNumber("Offset", MOSection
.Offset
);
480 W
.printNumber("Alignment", MOSection
.Alignment
);
481 W
.printHex("RelocationOffset", MOSection
.RelocationTableOffset
);
482 W
.printNumber("RelocationCount", MOSection
.NumRelocationTableEntries
);
483 W
.printEnum("Type", MOSection
.Flags
& 0xFF,
484 makeArrayRef(MachOSectionTypes
));
485 W
.printFlags("Attributes", MOSection
.Flags
>> 8,
486 makeArrayRef(MachOSectionAttributes
));
487 W
.printHex("Reserved1", MOSection
.Reserved1
);
488 W
.printHex("Reserved2", MOSection
.Reserved2
);
490 W
.printHex("Reserved3", MOSection
.Reserved3
);
492 if (opts::SectionRelocations
) {
493 ListScope
D(W
, "Relocations");
494 for (const RelocationRef
&Reloc
: Section
.relocations())
495 printRelocation(Reloc
);
498 if (opts::SectionSymbols
) {
499 ListScope
D(W
, "Symbols");
500 for (const SymbolRef
&Symbol
: Obj
->symbols()) {
501 if (!Section
.containsSymbol(Symbol
))
508 if (opts::SectionData
&& !Section
.isBSS())
509 W
.printBinaryBlock("SectionData", unwrapOrError(Obj
->getFileName(),
510 Section
.getContents()));
514 void MachODumper::printRelocations() {
515 ListScope
D(W
, "Relocations");
518 for (const SectionRef
&Section
: Obj
->sections()) {
519 StringRef Name
= unwrapOrError(Obj
->getFileName(), Section
.getName());
520 bool PrintedGroup
= false;
521 for (const RelocationRef
&Reloc
: Section
.relocations()) {
523 W
.startLine() << "Section " << Name
<< " {\n";
528 printRelocation(Reloc
);
533 W
.startLine() << "}\n";
538 void MachODumper::printRelocation(const RelocationRef
&Reloc
) {
539 return printRelocation(Obj
, Reloc
);
542 void MachODumper::printRelocation(const MachOObjectFile
*Obj
,
543 const RelocationRef
&Reloc
) {
544 uint64_t Offset
= Reloc
.getOffset();
545 SmallString
<32> RelocName
;
546 Reloc
.getTypeName(RelocName
);
548 DataRefImpl DR
= Reloc
.getRawDataRefImpl();
549 MachO::any_relocation_info RE
= Obj
->getRelocation(DR
);
550 bool IsScattered
= Obj
->isRelocationScattered(RE
);
551 bool IsExtern
= !IsScattered
&& Obj
->getPlainRelocationExternal(RE
);
553 StringRef TargetName
;
555 symbol_iterator Symbol
= Reloc
.getSymbol();
556 if (Symbol
!= Obj
->symbol_end()) {
557 Expected
<StringRef
> TargetNameOrErr
= Symbol
->getName();
558 if (!TargetNameOrErr
)
559 reportError(TargetNameOrErr
.takeError(), Obj
->getFileName());
560 TargetName
= *TargetNameOrErr
;
562 } else if (!IsScattered
) {
563 section_iterator SecI
= Obj
->getRelocationSection(DR
);
564 if (SecI
!= Obj
->section_end())
565 TargetName
= unwrapOrError(Obj
->getFileName(), SecI
->getName());
567 if (TargetName
.empty())
570 if (opts::ExpandRelocs
) {
571 DictScope
Group(W
, "Relocation");
572 W
.printHex("Offset", Offset
);
573 W
.printNumber("PCRel", Obj
->getAnyRelocationPCRel(RE
));
574 W
.printNumber("Length", Obj
->getAnyRelocationLength(RE
));
575 W
.printNumber("Type", RelocName
, Obj
->getAnyRelocationType(RE
));
577 W
.printHex("Value", Obj
->getScatteredRelocationValue(RE
));
579 const char *Kind
= IsExtern
? "Symbol" : "Section";
580 W
.printNumber(Kind
, TargetName
, Obj
->getPlainRelocationSymbolNum(RE
));
583 SmallString
<32> SymbolNameOrOffset("0x");
585 // Scattered relocations don't really have an associated symbol for some
586 // reason, even if one exists in the symtab at the correct address.
587 SymbolNameOrOffset
+= utohexstr(Obj
->getScatteredRelocationValue(RE
));
589 SymbolNameOrOffset
= TargetName
;
592 raw_ostream
& OS
= W
.startLine();
594 << " " << Obj
->getAnyRelocationPCRel(RE
)
595 << " " << Obj
->getAnyRelocationLength(RE
);
599 OS
<< " " << Obj
->getPlainRelocationExternal(RE
);
600 OS
<< " " << RelocName
601 << " " << IsScattered
602 << " " << SymbolNameOrOffset
607 void MachODumper::printSymbols() {
608 ListScope
Group(W
, "Symbols");
610 for (const SymbolRef
&Symbol
: Obj
->symbols()) {
615 void MachODumper::printDynamicSymbols() {
616 ListScope
Group(W
, "DynamicSymbols");
619 void MachODumper::printSymbol(const SymbolRef
&Symbol
) {
620 StringRef SymbolName
;
621 Expected
<StringRef
> SymbolNameOrErr
= Symbol
.getName();
622 if (!SymbolNameOrErr
) {
623 // TODO: Actually report errors helpfully.
624 consumeError(SymbolNameOrErr
.takeError());
626 SymbolName
= *SymbolNameOrErr
;
628 MachOSymbol MOSymbol
;
629 getSymbol(Obj
, Symbol
.getRawDataRefImpl(), MOSymbol
);
631 StringRef SectionName
= "";
632 Expected
<section_iterator
> SecIOrErr
= Symbol
.getSection();
634 reportError(SecIOrErr
.takeError(), Obj
->getFileName());
636 section_iterator SecI
= *SecIOrErr
;
637 if (SecI
!= Obj
->section_end())
638 SectionName
= unwrapOrError(Obj
->getFileName(), SecI
->getName());
640 DictScope
D(W
, "Symbol");
641 W
.printNumber("Name", SymbolName
, MOSymbol
.StringIndex
);
642 if (MOSymbol
.Type
& MachO::N_STAB
) {
643 W
.printHex("Type", "SymDebugTable", MOSymbol
.Type
);
645 if (MOSymbol
.Type
& MachO::N_PEXT
)
646 W
.startLine() << "PrivateExtern\n";
647 if (MOSymbol
.Type
& MachO::N_EXT
)
648 W
.startLine() << "Extern\n";
649 W
.printEnum("Type", uint8_t(MOSymbol
.Type
& MachO::N_TYPE
),
650 makeArrayRef(MachOSymbolTypes
));
652 W
.printHex("Section", SectionName
, MOSymbol
.SectionIndex
);
653 W
.printEnum("RefType", static_cast<uint16_t>(MOSymbol
.Flags
& 0xF),
654 makeArrayRef(MachOSymbolRefTypes
));
655 W
.printFlags("Flags", static_cast<uint16_t>(MOSymbol
.Flags
& ~0xF),
656 makeArrayRef(MachOSymbolFlags
));
657 W
.printHex("Value", MOSymbol
.Value
);
660 void MachODumper::printUnwindInfo() {
661 W
.startLine() << "UnwindInfo not implemented.\n";
664 void MachODumper::printStackMap() const {
665 object::SectionRef StackMapSection
;
666 for (auto Sec
: Obj
->sections()) {
668 if (Expected
<StringRef
> NameOrErr
= Sec
.getName())
671 consumeError(NameOrErr
.takeError());
673 if (Name
== "__llvm_stackmaps") {
674 StackMapSection
= Sec
;
679 if (StackMapSection
== object::SectionRef())
682 StringRef StackMapContents
=
683 unwrapOrError(Obj
->getFileName(), StackMapSection
.getContents());
684 ArrayRef
<uint8_t> StackMapContentsArray
=
685 arrayRefFromStringRef(StackMapContents
);
687 if (Obj
->isLittleEndian())
689 W
, StackMapParser
<support::little
>(StackMapContentsArray
));
692 W
, StackMapParser
<support::big
>(StackMapContentsArray
));
695 void MachODumper::printNeededLibraries() {
696 ListScope
D(W
, "NeededLibraries");
698 using LibsTy
= std::vector
<StringRef
>;
701 for (const auto &Command
: Obj
->load_commands()) {
702 if (Command
.C
.cmd
== MachO::LC_LOAD_DYLIB
||
703 Command
.C
.cmd
== MachO::LC_ID_DYLIB
||
704 Command
.C
.cmd
== MachO::LC_LOAD_WEAK_DYLIB
||
705 Command
.C
.cmd
== MachO::LC_REEXPORT_DYLIB
||
706 Command
.C
.cmd
== MachO::LC_LAZY_LOAD_DYLIB
||
707 Command
.C
.cmd
== MachO::LC_LOAD_UPWARD_DYLIB
) {
708 MachO::dylib_command Dl
= Obj
->getDylibIDLoadCommand(Command
);
709 if (Dl
.dylib
.name
< Dl
.cmdsize
) {
710 auto *P
= static_cast<const char*>(Command
.Ptr
) + Dl
.dylib
.name
;
716 llvm::stable_sort(Libs
);
718 for (const auto &L
: Libs
) {
719 W
.startLine() << L
<< "\n";
723 void MachODumper::printMachODataInCode() {
724 for (const auto &Load
: Obj
->load_commands()) {
725 if (Load
.C
.cmd
== MachO::LC_DATA_IN_CODE
) {
726 MachO::linkedit_data_command LLC
= Obj
->getLinkeditDataLoadCommand(Load
);
727 DictScope
Group(W
, "DataInCode");
728 W
.printNumber("Data offset", LLC
.dataoff
);
729 W
.printNumber("Data size", LLC
.datasize
);
730 ListScope
D(W
, "Data entries");
731 unsigned NumRegions
= LLC
.datasize
/ sizeof(MachO::data_in_code_entry
);
732 for (unsigned i
= 0; i
< NumRegions
; ++i
) {
733 MachO::data_in_code_entry DICE
= Obj
->getDataInCodeTableEntry(
735 DictScope
Group(W
, "Entry");
736 W
.printNumber("Index", i
);
737 W
.printNumber("Offset", DICE
.offset
);
738 W
.printNumber("Length", DICE
.length
);
739 W
.printNumber("Kind", DICE
.kind
);
745 void MachODumper::printMachOVersionMin() {
746 for (const auto &Load
: Obj
->load_commands()) {
748 switch (Load
.C
.cmd
) {
749 case MachO::LC_VERSION_MIN_MACOSX
:
750 Cmd
= "LC_VERSION_MIN_MACOSX";
752 case MachO::LC_VERSION_MIN_IPHONEOS
:
753 Cmd
= "LC_VERSION_MIN_IPHONEOS";
755 case MachO::LC_VERSION_MIN_TVOS
:
756 Cmd
= "LC_VERSION_MIN_TVOS";
758 case MachO::LC_VERSION_MIN_WATCHOS
:
759 Cmd
= "LC_VERSION_MIN_WATCHOS";
761 case MachO::LC_BUILD_VERSION
:
762 Cmd
= "LC_BUILD_VERSION";
768 DictScope
Group(W
, "MinVersion");
769 // Handle LC_BUILD_VERSION.
770 if (Load
.C
.cmd
== MachO::LC_BUILD_VERSION
) {
771 MachO::build_version_command BVC
= Obj
->getBuildVersionLoadCommand(Load
);
772 W
.printString("Cmd", Cmd
);
773 W
.printNumber("Size", BVC
.cmdsize
);
774 W
.printString("Platform",
775 MachOObjectFile::getBuildPlatform(BVC
.platform
));
776 W
.printString("Version", MachOObjectFile::getVersionString(BVC
.minos
));
778 W
.printString("SDK", MachOObjectFile::getVersionString(BVC
.sdk
));
780 W
.printString("SDK", StringRef("n/a"));
784 MachO::version_min_command VMC
= Obj
->getVersionMinLoadCommand(Load
);
785 W
.printString("Cmd", Cmd
);
786 W
.printNumber("Size", VMC
.cmdsize
);
787 SmallString
<32> Version
;
788 Version
= utostr(MachOObjectFile::getVersionMinMajor(VMC
, false)) + "." +
789 utostr(MachOObjectFile::getVersionMinMinor(VMC
, false));
790 uint32_t Update
= MachOObjectFile::getVersionMinUpdate(VMC
, false);
792 Version
+= "." + utostr(MachOObjectFile::getVersionMinUpdate(VMC
, false));
793 W
.printString("Version", Version
);
798 SDK
= utostr(MachOObjectFile::getVersionMinMajor(VMC
, true)) + "." +
799 utostr(MachOObjectFile::getVersionMinMinor(VMC
, true));
800 uint32_t Update
= MachOObjectFile::getVersionMinUpdate(VMC
, true);
802 SDK
+= "." + utostr(MachOObjectFile::getVersionMinUpdate(VMC
, true));
804 W
.printString("SDK", SDK
);
808 void MachODumper::printMachODysymtab() {
809 for (const auto &Load
: Obj
->load_commands()) {
810 if (Load
.C
.cmd
== MachO::LC_DYSYMTAB
) {
811 MachO::dysymtab_command DLC
= Obj
->getDysymtabLoadCommand();
812 DictScope
Group(W
, "Dysymtab");
813 W
.printNumber("ilocalsym", DLC
.ilocalsym
);
814 W
.printNumber("nlocalsym", DLC
.nlocalsym
);
815 W
.printNumber("iextdefsym", DLC
.iextdefsym
);
816 W
.printNumber("nextdefsym", DLC
.nextdefsym
);
817 W
.printNumber("iundefsym", DLC
.iundefsym
);
818 W
.printNumber("nundefsym", DLC
.nundefsym
);
819 W
.printNumber("tocoff", DLC
.tocoff
);
820 W
.printNumber("ntoc", DLC
.ntoc
);
821 W
.printNumber("modtaboff", DLC
.modtaboff
);
822 W
.printNumber("nmodtab", DLC
.nmodtab
);
823 W
.printNumber("extrefsymoff", DLC
.extrefsymoff
);
824 W
.printNumber("nextrefsyms", DLC
.nextrefsyms
);
825 W
.printNumber("indirectsymoff", DLC
.indirectsymoff
);
826 W
.printNumber("nindirectsyms", DLC
.nindirectsyms
);
827 W
.printNumber("extreloff", DLC
.extreloff
);
828 W
.printNumber("nextrel", DLC
.nextrel
);
829 W
.printNumber("locreloff", DLC
.locreloff
);
830 W
.printNumber("nlocrel", DLC
.nlocrel
);
835 void MachODumper::printMachOSegment() {
836 for (const auto &Load
: Obj
->load_commands()) {
837 if (Load
.C
.cmd
== MachO::LC_SEGMENT
|| Load
.C
.cmd
== MachO::LC_SEGMENT_64
) {
838 MachOSegment MOSegment
;
839 getSegment(Obj
, Load
, MOSegment
);
840 DictScope
Group(W
, "Segment");
841 W
.printString("Cmd", MOSegment
.CmdName
);
842 W
.printString("Name", MOSegment
.SegName
);
843 W
.printNumber("Size", MOSegment
.cmdsize
);
844 W
.printHex("vmaddr", MOSegment
.vmaddr
);
845 W
.printHex("vmsize", MOSegment
.vmsize
);
846 W
.printNumber("fileoff", MOSegment
.fileoff
);
847 W
.printNumber("filesize", MOSegment
.filesize
);
848 W
.printString("maxprot", getMask(MOSegment
.maxprot
));
849 W
.printString("initprot", getMask(MOSegment
.initprot
));
850 W
.printNumber("nsects", MOSegment
.nsects
);
851 W
.printHex("flags", MOSegment
.flags
);
856 void MachODumper::printMachOIndirectSymbols() {
857 for (const auto &Load
: Obj
->load_commands()) {
858 if (Load
.C
.cmd
== MachO::LC_DYSYMTAB
) {
859 MachO::dysymtab_command DLC
= Obj
->getDysymtabLoadCommand();
860 DictScope
Group(W
, "Indirect Symbols");
861 W
.printNumber("Number", DLC
.nindirectsyms
);
862 ListScope
D(W
, "Symbols");
863 for (unsigned i
= 0; i
< DLC
.nindirectsyms
; ++i
) {
864 DictScope
Group(W
, "Entry");
865 W
.printNumber("Entry Index", i
);
866 W
.printHex("Symbol Index", Obj
->getIndirectSymbolTableEntry(DLC
, i
));
872 void MachODumper::printMachOLinkerOptions() {
873 for (const auto &Load
: Obj
->load_commands()) {
874 if (Load
.C
.cmd
== MachO::LC_LINKER_OPTION
) {
875 MachO::linker_option_command LOLC
= Obj
->getLinkerOptionLoadCommand(Load
);
876 DictScope
Group(W
, "Linker Options");
877 W
.printNumber("Size", LOLC
.cmdsize
);
878 ListScope
D(W
, "Strings");
879 uint64_t DataSize
= LOLC
.cmdsize
- sizeof(MachO::linker_option_command
);
880 const char *P
= Load
.Ptr
+ sizeof(MachO::linker_option_command
);
881 StringRef
Data(P
, DataSize
);
882 for (unsigned i
= 0; i
< LOLC
.count
; ++i
) {
883 std::pair
<StringRef
,StringRef
> Split
= Data
.split('\0');
884 W
.printString("Value", Split
.first
);