1 //===-- MachODump.cpp - Object file dumping utility for llvm --------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the MachO-specific dumper for llvm-readobj.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "Error.h"
15 #include "ObjDumper.h"
16 #include "StackMapPrinter.h"
17 #include "llvm-readobj.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/Object/MachO.h"
21 #include "llvm/Support/Casting.h"
22 #include "llvm/Support/ScopedPrinter.h"
23
24 using namespace llvm;
25 using namespace object;
26
27 namespace {
28
29 class MachODumper : public ObjDumper {
30 public:
MachODumper(const MachOObjectFile * Obj,ScopedPrinter & Writer)31 MachODumper(const MachOObjectFile *Obj, ScopedPrinter &Writer)
32 : ObjDumper(Writer), Obj(Obj) {}
33
34 void printFileHeaders() override;
35 void printSections() override;
36 void printRelocations() override;
37 void printSymbols() override;
38 void printDynamicSymbols() override;
39 void printUnwindInfo() override;
40 void printStackMap() const override;
41
42 // MachO-specific.
43 void printMachODataInCode() override;
44 void printMachOVersionMin() override;
45 void printMachODysymtab() override;
46 void printMachOSegment() override;
47 void printMachOIndirectSymbols() override;
48 void printMachOLinkerOptions () override;
49
50 private:
51 template<class MachHeader>
52 void printFileHeaders(const MachHeader &Header);
53
54 void printSymbol(const SymbolRef &Symbol);
55
56 void printRelocation(const RelocationRef &Reloc);
57
58 void printRelocation(const MachOObjectFile *Obj, const RelocationRef &Reloc);
59
60 void printSections(const MachOObjectFile *Obj);
61
62 const MachOObjectFile *Obj;
63 };
64
65 } // namespace
66
67
68 namespace llvm {
69
createMachODumper(const object::ObjectFile * Obj,ScopedPrinter & Writer,std::unique_ptr<ObjDumper> & Result)70 std::error_code createMachODumper(const object::ObjectFile *Obj,
71 ScopedPrinter &Writer,
72 std::unique_ptr<ObjDumper> &Result) {
73 const MachOObjectFile *MachOObj = dyn_cast<MachOObjectFile>(Obj);
74 if (!MachOObj)
75 return readobj_error::unsupported_obj_file_format;
76
77 Result.reset(new MachODumper(MachOObj, Writer));
78 return readobj_error::success;
79 }
80
81 } // namespace llvm
82
83 static const EnumEntry<uint32_t> MachOMagics[] = {
84 { "Magic", MachO::MH_MAGIC },
85 { "Cigam", MachO::MH_CIGAM },
86 { "Magic64", MachO::MH_MAGIC_64 },
87 { "Cigam64", MachO::MH_CIGAM_64 },
88 { "FatMagic", MachO::FAT_MAGIC },
89 { "FatCigam", MachO::FAT_CIGAM },
90 };
91
92 static const EnumEntry<uint32_t> MachOHeaderFileTypes[] = {
93 { "Relocatable", MachO::MH_OBJECT },
94 { "Executable", MachO::MH_EXECUTE },
95 { "FixedVMLibrary", MachO::MH_FVMLIB },
96 { "Core", MachO::MH_CORE },
97 { "PreloadedExecutable", MachO::MH_PRELOAD },
98 { "DynamicLibrary", MachO::MH_DYLIB },
99 { "DynamicLinker", MachO::MH_DYLINKER },
100 { "Bundle", MachO::MH_BUNDLE },
101 { "DynamicLibraryStub", MachO::MH_DYLIB_STUB },
102 { "DWARFSymbol", MachO::MH_DSYM },
103 { "KextBundle", MachO::MH_KEXT_BUNDLE },
104 };
105
106 static const EnumEntry<uint32_t> MachOHeaderCpuTypes[] = {
107 { "Any" , static_cast<uint32_t>(MachO::CPU_TYPE_ANY) },
108 { "X86" , MachO::CPU_TYPE_X86 },
109 { "X86-64" , MachO::CPU_TYPE_X86_64 },
110 { "Mc98000" , MachO::CPU_TYPE_MC98000 },
111 { "Arm" , MachO::CPU_TYPE_ARM },
112 { "Arm64" , MachO::CPU_TYPE_ARM64 },
113 { "Sparc" , MachO::CPU_TYPE_SPARC },
114 { "PowerPC" , MachO::CPU_TYPE_POWERPC },
115 { "PowerPC64" , MachO::CPU_TYPE_POWERPC64 },
116 };
117
118 static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesX86[] = {
119 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_I386_ALL),
120 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_386),
121 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_486),
122 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_486SX),
123 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_586),
124 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTPRO),
125 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTII_M3),
126 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTII_M5),
127 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_CELERON),
128 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_CELERON_MOBILE),
129 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_3),
130 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_3_M),
131 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_3_XEON),
132 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_M),
133 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_4),
134 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_4_M),
135 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ITANIUM),
136 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ITANIUM_2),
137 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_XEON),
138 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_XEON_MP),
139 };
140
141 static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesX64[] = {
142 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_X86_64_ALL),
143 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_X86_ARCH1),
144 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_X86_64_H),
145 };
146
147 static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesARM[] = {
148 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_ALL),
149 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V4T),
150 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V6),
151 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V5),
152 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V5TEJ),
153 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_XSCALE),
154 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7),
155 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7S),
156 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7K),
157 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V6M),
158 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7M),
159 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7EM),
160 };
161
162 static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesARM64[] = {
163 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM64_ALL),
164 };
165
166 static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesSPARC[] = {
167 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_SPARC_ALL),
168 };
169
170 static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesPPC[] = {
171 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_ALL),
172 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_601),
173 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_602),
174 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_603),
175 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_603e),
176 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_603ev),
177 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_604),
178 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_604e),
179 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_620),
180 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_750),
181 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_7400),
182 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_7450),
183 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_970),
184 };
185
186 static const EnumEntry<uint32_t> MachOHeaderFlags[] = {
187 LLVM_READOBJ_ENUM_ENT(MachO, MH_NOUNDEFS),
188 LLVM_READOBJ_ENUM_ENT(MachO, MH_INCRLINK),
189 LLVM_READOBJ_ENUM_ENT(MachO, MH_DYLDLINK),
190 LLVM_READOBJ_ENUM_ENT(MachO, MH_BINDATLOAD),
191 LLVM_READOBJ_ENUM_ENT(MachO, MH_PREBOUND),
192 LLVM_READOBJ_ENUM_ENT(MachO, MH_SPLIT_SEGS),
193 LLVM_READOBJ_ENUM_ENT(MachO, MH_LAZY_INIT),
194 LLVM_READOBJ_ENUM_ENT(MachO, MH_TWOLEVEL),
195 LLVM_READOBJ_ENUM_ENT(MachO, MH_FORCE_FLAT),
196 LLVM_READOBJ_ENUM_ENT(MachO, MH_NOMULTIDEFS),
197 LLVM_READOBJ_ENUM_ENT(MachO, MH_NOFIXPREBINDING),
198 LLVM_READOBJ_ENUM_ENT(MachO, MH_PREBINDABLE),
199 LLVM_READOBJ_ENUM_ENT(MachO, MH_ALLMODSBOUND),
200 LLVM_READOBJ_ENUM_ENT(MachO, MH_SUBSECTIONS_VIA_SYMBOLS),
201 LLVM_READOBJ_ENUM_ENT(MachO, MH_CANONICAL),
202 LLVM_READOBJ_ENUM_ENT(MachO, MH_WEAK_DEFINES),
203 LLVM_READOBJ_ENUM_ENT(MachO, MH_BINDS_TO_WEAK),
204 LLVM_READOBJ_ENUM_ENT(MachO, MH_ALLOW_STACK_EXECUTION),
205 LLVM_READOBJ_ENUM_ENT(MachO, MH_ROOT_SAFE),
206 LLVM_READOBJ_ENUM_ENT(MachO, MH_SETUID_SAFE),
207 LLVM_READOBJ_ENUM_ENT(MachO, MH_NO_REEXPORTED_DYLIBS),
208 LLVM_READOBJ_ENUM_ENT(MachO, MH_PIE),
209 LLVM_READOBJ_ENUM_ENT(MachO, MH_DEAD_STRIPPABLE_DYLIB),
210 LLVM_READOBJ_ENUM_ENT(MachO, MH_HAS_TLV_DESCRIPTORS),
211 LLVM_READOBJ_ENUM_ENT(MachO, MH_NO_HEAP_EXECUTION),
212 LLVM_READOBJ_ENUM_ENT(MachO, MH_APP_EXTENSION_SAFE),
213 };
214
215 static const EnumEntry<unsigned> MachOSectionAttributes[] = {
216 { "LocReloc" , 1 << 0 /*S_ATTR_LOC_RELOC */ },
217 { "ExtReloc" , 1 << 1 /*S_ATTR_EXT_RELOC */ },
218 { "SomeInstructions" , 1 << 2 /*S_ATTR_SOME_INSTRUCTIONS */ },
219 { "Debug" , 1 << 17 /*S_ATTR_DEBUG */ },
220 { "SelfModifyingCode", 1 << 18 /*S_ATTR_SELF_MODIFYING_CODE*/ },
221 { "LiveSupport" , 1 << 19 /*S_ATTR_LIVE_SUPPORT */ },
222 { "NoDeadStrip" , 1 << 20 /*S_ATTR_NO_DEAD_STRIP */ },
223 { "StripStaticSyms" , 1 << 21 /*S_ATTR_STRIP_STATIC_SYMS */ },
224 { "NoTOC" , 1 << 22 /*S_ATTR_NO_TOC */ },
225 { "PureInstructions" , 1 << 23 /*S_ATTR_PURE_INSTRUCTIONS */ },
226 };
227
228 static const EnumEntry<unsigned> MachOSymbolRefTypes[] = {
229 { "UndefinedNonLazy", 0 },
230 { "ReferenceFlagUndefinedLazy", 1 },
231 { "ReferenceFlagDefined", 2 },
232 { "ReferenceFlagPrivateDefined", 3 },
233 { "ReferenceFlagPrivateUndefinedNonLazy", 4 },
234 { "ReferenceFlagPrivateUndefinedLazy", 5 }
235 };
236
237 static const EnumEntry<unsigned> MachOSymbolFlags[] = {
238 { "ReferencedDynamically", 0x10 },
239 { "NoDeadStrip", 0x20 },
240 { "WeakRef", 0x40 },
241 { "WeakDef", 0x80 },
242 { "AltEntry", 0x200 },
243 };
244
245 static const EnumEntry<unsigned> MachOSymbolTypes[] = {
246 { "Undef", 0x0 },
247 { "Abs", 0x2 },
248 { "Indirect", 0xA },
249 { "PreboundUndef", 0xC },
250 { "Section", 0xE }
251 };
252
253 namespace {
254 struct MachOSection {
255 ArrayRef<char> Name;
256 ArrayRef<char> SegmentName;
257 uint64_t Address;
258 uint64_t Size;
259 uint32_t Offset;
260 uint32_t Alignment;
261 uint32_t RelocationTableOffset;
262 uint32_t NumRelocationTableEntries;
263 uint32_t Flags;
264 uint32_t Reserved1;
265 uint32_t Reserved2;
266 uint32_t Reserved3;
267 };
268
269 struct MachOSegment {
270 std::string CmdName;
271 std::string SegName;
272 uint64_t cmdsize;
273 uint64_t vmaddr;
274 uint64_t vmsize;
275 uint64_t fileoff;
276 uint64_t filesize;
277 uint32_t maxprot;
278 uint32_t initprot;
279 uint32_t nsects;
280 uint32_t flags;
281 };
282
283 struct MachOSymbol {
284 uint32_t StringIndex;
285 uint8_t Type;
286 uint8_t SectionIndex;
287 uint16_t Flags;
288 uint64_t Value;
289 };
290 }
291
getMask(uint32_t prot)292 static std::string getMask(uint32_t prot)
293 {
294 // TODO (davide): This always assumes prot is valid.
295 // Catch mistakes and report if needed.
296 std::string Prot;
297 Prot = "";
298 Prot += (prot & MachO::VM_PROT_READ) ? "r" : "-";
299 Prot += (prot & MachO::VM_PROT_WRITE) ? "w" : "-";
300 Prot += (prot & MachO::VM_PROT_EXECUTE) ? "x" : "-";
301 return Prot;
302 }
303
getSection(const MachOObjectFile * Obj,DataRefImpl Sec,MachOSection & Section)304 static void getSection(const MachOObjectFile *Obj,
305 DataRefImpl Sec,
306 MachOSection &Section) {
307 if (!Obj->is64Bit()) {
308 MachO::section Sect = Obj->getSection(Sec);
309 Section.Address = Sect.addr;
310 Section.Size = Sect.size;
311 Section.Offset = Sect.offset;
312 Section.Alignment = Sect.align;
313 Section.RelocationTableOffset = Sect.reloff;
314 Section.NumRelocationTableEntries = Sect.nreloc;
315 Section.Flags = Sect.flags;
316 Section.Reserved1 = Sect.reserved1;
317 Section.Reserved2 = Sect.reserved2;
318 return;
319 }
320 MachO::section_64 Sect = Obj->getSection64(Sec);
321 Section.Address = Sect.addr;
322 Section.Size = Sect.size;
323 Section.Offset = Sect.offset;
324 Section.Alignment = Sect.align;
325 Section.RelocationTableOffset = Sect.reloff;
326 Section.NumRelocationTableEntries = Sect.nreloc;
327 Section.Flags = Sect.flags;
328 Section.Reserved1 = Sect.reserved1;
329 Section.Reserved2 = Sect.reserved2;
330 Section.Reserved3 = Sect.reserved3;
331 }
332
getSegment(const MachOObjectFile * Obj,const MachOObjectFile::LoadCommandInfo & L,MachOSegment & Segment)333 static void getSegment(const MachOObjectFile *Obj,
334 const MachOObjectFile::LoadCommandInfo &L,
335 MachOSegment &Segment) {
336 if (!Obj->is64Bit()) {
337 MachO::segment_command SC = Obj->getSegmentLoadCommand(L);
338 Segment.CmdName = "LC_SEGMENT";
339 Segment.SegName = SC.segname;
340 Segment.cmdsize = SC.cmdsize;
341 Segment.vmaddr = SC.vmaddr;
342 Segment.vmsize = SC.vmsize;
343 Segment.fileoff = SC.fileoff;
344 Segment.filesize = SC.filesize;
345 Segment.maxprot = SC.maxprot;
346 Segment.initprot = SC.initprot;
347 Segment.nsects = SC.nsects;
348 Segment.flags = SC.flags;
349 return;
350 }
351 MachO::segment_command_64 SC = Obj->getSegment64LoadCommand(L);
352 Segment.CmdName = "LC_SEGMENT_64";
353 Segment.SegName = SC.segname;
354 Segment.cmdsize = SC.cmdsize;
355 Segment.vmaddr = SC.vmaddr;
356 Segment.vmsize = SC.vmsize;
357 Segment.fileoff = SC.fileoff;
358 Segment.filesize = SC.filesize;
359 Segment.maxprot = SC.maxprot;
360 Segment.initprot = SC.initprot;
361 Segment.nsects = SC.nsects;
362 Segment.flags = SC.flags;
363 }
364
getSymbol(const MachOObjectFile * Obj,DataRefImpl DRI,MachOSymbol & Symbol)365 static void getSymbol(const MachOObjectFile *Obj,
366 DataRefImpl DRI,
367 MachOSymbol &Symbol) {
368 if (!Obj->is64Bit()) {
369 MachO::nlist Entry = Obj->getSymbolTableEntry(DRI);
370 Symbol.StringIndex = Entry.n_strx;
371 Symbol.Type = Entry.n_type;
372 Symbol.SectionIndex = Entry.n_sect;
373 Symbol.Flags = Entry.n_desc;
374 Symbol.Value = Entry.n_value;
375 return;
376 }
377 MachO::nlist_64 Entry = Obj->getSymbol64TableEntry(DRI);
378 Symbol.StringIndex = Entry.n_strx;
379 Symbol.Type = Entry.n_type;
380 Symbol.SectionIndex = Entry.n_sect;
381 Symbol.Flags = Entry.n_desc;
382 Symbol.Value = Entry.n_value;
383 }
384
printFileHeaders()385 void MachODumper::printFileHeaders() {
386 DictScope H(W, "MachHeader");
387 if (!Obj->is64Bit()) {
388 printFileHeaders(Obj->getHeader());
389 } else {
390 printFileHeaders(Obj->getHeader64());
391 W.printHex("Reserved", Obj->getHeader64().reserved);
392 }
393 }
394
395 template<class MachHeader>
printFileHeaders(const MachHeader & Header)396 void MachODumper::printFileHeaders(const MachHeader &Header) {
397 W.printEnum("Magic", Header.magic, makeArrayRef(MachOMagics));
398 W.printEnum("CpuType", Header.cputype, makeArrayRef(MachOHeaderCpuTypes));
399 uint32_t subtype = Header.cpusubtype & ~MachO::CPU_SUBTYPE_MASK;
400 switch (Header.cputype) {
401 case MachO::CPU_TYPE_X86:
402 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesX86));
403 break;
404 case MachO::CPU_TYPE_X86_64:
405 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesX64));
406 break;
407 case MachO::CPU_TYPE_ARM:
408 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesARM));
409 break;
410 case MachO::CPU_TYPE_POWERPC:
411 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesPPC));
412 break;
413 case MachO::CPU_TYPE_SPARC:
414 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesSPARC));
415 break;
416 case MachO::CPU_TYPE_ARM64:
417 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesARM64));
418 break;
419 case MachO::CPU_TYPE_POWERPC64:
420 default:
421 W.printHex("CpuSubtype", subtype);
422 }
423 W.printEnum("FileType", Header.filetype, makeArrayRef(MachOHeaderFileTypes));
424 W.printNumber("NumOfLoadCommands", Header.ncmds);
425 W.printNumber("SizeOfLoadCommands", Header.sizeofcmds);
426 W.printFlags("Flags", Header.flags, makeArrayRef(MachOHeaderFlags));
427 }
428
printSections()429 void MachODumper::printSections() {
430 return printSections(Obj);
431 }
432
printSections(const MachOObjectFile * Obj)433 void MachODumper::printSections(const MachOObjectFile *Obj) {
434 ListScope Group(W, "Sections");
435
436 int SectionIndex = -1;
437 for (const SectionRef &Section : Obj->sections()) {
438 ++SectionIndex;
439
440 MachOSection MOSection;
441 getSection(Obj, Section.getRawDataRefImpl(), MOSection);
442 DataRefImpl DR = Section.getRawDataRefImpl();
443
444 StringRef Name;
445 error(Section.getName(Name));
446
447 ArrayRef<char> RawName = Obj->getSectionRawName(DR);
448 StringRef SegmentName = Obj->getSectionFinalSegmentName(DR);
449 ArrayRef<char> RawSegmentName = Obj->getSectionRawFinalSegmentName(DR);
450
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);
467 if (Obj->is64Bit())
468 W.printHex("Reserved3", MOSection.Reserved3);
469
470 if (opts::SectionRelocations) {
471 ListScope D(W, "Relocations");
472 for (const RelocationRef &Reloc : Section.relocations())
473 printRelocation(Reloc);
474 }
475
476 if (opts::SectionSymbols) {
477 ListScope D(W, "Symbols");
478 for (const SymbolRef &Symbol : Obj->symbols()) {
479 if (!Section.containsSymbol(Symbol))
480 continue;
481
482 printSymbol(Symbol);
483 }
484 }
485
486 if (opts::SectionData) {
487 bool IsBSS = Section.isBSS();
488 if (!IsBSS) {
489 StringRef Data;
490 error(Section.getContents(Data));
491
492 W.printBinaryBlock("SectionData", Data);
493 }
494 }
495 }
496 }
497
printRelocations()498 void MachODumper::printRelocations() {
499 ListScope D(W, "Relocations");
500
501 std::error_code EC;
502 for (const SectionRef &Section : Obj->sections()) {
503 StringRef Name;
504 error(Section.getName(Name));
505
506 bool PrintedGroup = false;
507 for (const RelocationRef &Reloc : Section.relocations()) {
508 if (!PrintedGroup) {
509 W.startLine() << "Section " << Name << " {\n";
510 W.indent();
511 PrintedGroup = true;
512 }
513
514 printRelocation(Reloc);
515 }
516
517 if (PrintedGroup) {
518 W.unindent();
519 W.startLine() << "}\n";
520 }
521 }
522 }
523
printRelocation(const RelocationRef & Reloc)524 void MachODumper::printRelocation(const RelocationRef &Reloc) {
525 return printRelocation(Obj, Reloc);
526 }
527
printRelocation(const MachOObjectFile * Obj,const RelocationRef & Reloc)528 void MachODumper::printRelocation(const MachOObjectFile *Obj,
529 const RelocationRef &Reloc) {
530 uint64_t Offset = Reloc.getOffset();
531 SmallString<32> RelocName;
532 Reloc.getTypeName(RelocName);
533
534 DataRefImpl DR = Reloc.getRawDataRefImpl();
535 MachO::any_relocation_info RE = Obj->getRelocation(DR);
536 bool IsScattered = Obj->isRelocationScattered(RE);
537 bool IsExtern = !IsScattered && Obj->getPlainRelocationExternal(RE);
538
539 StringRef TargetName;
540 if (IsExtern) {
541 symbol_iterator Symbol = Reloc.getSymbol();
542 if (Symbol != Obj->symbol_end()) {
543 Expected<StringRef> TargetNameOrErr = Symbol->getName();
544 if (!TargetNameOrErr)
545 error(errorToErrorCode(TargetNameOrErr.takeError()));
546 TargetName = *TargetNameOrErr;
547 }
548 } else if (!IsScattered) {
549 section_iterator SecI = Obj->getRelocationSection(DR);
550 if (SecI != Obj->section_end()) {
551 error(SecI->getName(TargetName));
552 }
553 }
554 if (TargetName.empty())
555 TargetName = "-";
556
557 if (opts::ExpandRelocs) {
558 DictScope Group(W, "Relocation");
559 W.printHex("Offset", Offset);
560 W.printNumber("PCRel", Obj->getAnyRelocationPCRel(RE));
561 W.printNumber("Length", Obj->getAnyRelocationLength(RE));
562 W.printNumber("Type", RelocName, Obj->getAnyRelocationType(RE));
563 if (IsScattered) {
564 W.printHex("Value", Obj->getScatteredRelocationValue(RE));
565 } else {
566 const char *Kind = IsExtern ? "Symbol" : "Section";
567 W.printNumber(Kind, TargetName, Obj->getPlainRelocationSymbolNum(RE));
568 }
569 } else {
570 SmallString<32> SymbolNameOrOffset("0x");
571 if (IsScattered) {
572 // Scattered relocations don't really have an associated symbol for some
573 // reason, even if one exists in the symtab at the correct address.
574 SymbolNameOrOffset += utohexstr(Obj->getScatteredRelocationValue(RE));
575 } else {
576 SymbolNameOrOffset = TargetName;
577 }
578
579 raw_ostream& OS = W.startLine();
580 OS << W.hex(Offset)
581 << " " << Obj->getAnyRelocationPCRel(RE)
582 << " " << Obj->getAnyRelocationLength(RE);
583 if (IsScattered)
584 OS << " n/a";
585 else
586 OS << " " << Obj->getPlainRelocationExternal(RE);
587 OS << " " << RelocName
588 << " " << IsScattered
589 << " " << SymbolNameOrOffset
590 << "\n";
591 }
592 }
593
printSymbols()594 void MachODumper::printSymbols() {
595 ListScope Group(W, "Symbols");
596
597 for (const SymbolRef &Symbol : Obj->symbols()) {
598 printSymbol(Symbol);
599 }
600 }
601
printDynamicSymbols()602 void MachODumper::printDynamicSymbols() {
603 ListScope Group(W, "DynamicSymbols");
604 }
605
printSymbol(const SymbolRef & Symbol)606 void MachODumper::printSymbol(const SymbolRef &Symbol) {
607 StringRef SymbolName;
608 Expected<StringRef> SymbolNameOrErr = Symbol.getName();
609 if (!SymbolNameOrErr) {
610 // TODO: Actually report errors helpfully.
611 consumeError(SymbolNameOrErr.takeError());
612 } else
613 SymbolName = *SymbolNameOrErr;
614
615 MachOSymbol MOSymbol;
616 getSymbol(Obj, Symbol.getRawDataRefImpl(), MOSymbol);
617
618 StringRef SectionName = "";
619 Expected<section_iterator> SecIOrErr = Symbol.getSection();
620 error(errorToErrorCode(SecIOrErr.takeError()));
621 section_iterator SecI = *SecIOrErr;
622 if (SecI != Obj->section_end())
623 error(SecI->getName(SectionName));
624
625 DictScope D(W, "Symbol");
626 W.printNumber("Name", SymbolName, MOSymbol.StringIndex);
627 if (MOSymbol.Type & MachO::N_STAB) {
628 W.printHex("Type", "SymDebugTable", MOSymbol.Type);
629 } else {
630 if (MOSymbol.Type & MachO::N_PEXT)
631 W.startLine() << "PrivateExtern\n";
632 if (MOSymbol.Type & MachO::N_EXT)
633 W.startLine() << "Extern\n";
634 W.printEnum("Type", uint8_t(MOSymbol.Type & MachO::N_TYPE),
635 makeArrayRef(MachOSymbolTypes));
636 }
637 W.printHex("Section", SectionName, MOSymbol.SectionIndex);
638 W.printEnum("RefType", static_cast<uint16_t>(MOSymbol.Flags & 0xF),
639 makeArrayRef(MachOSymbolRefTypes));
640 W.printFlags("Flags", static_cast<uint16_t>(MOSymbol.Flags & ~0xF),
641 makeArrayRef(MachOSymbolFlags));
642 W.printHex("Value", MOSymbol.Value);
643 }
644
printUnwindInfo()645 void MachODumper::printUnwindInfo() {
646 W.startLine() << "UnwindInfo not implemented.\n";
647 }
648
printStackMap() const649 void MachODumper::printStackMap() const {
650 object::SectionRef StackMapSection;
651 for (auto Sec : Obj->sections()) {
652 StringRef Name;
653 Sec.getName(Name);
654 if (Name == "__llvm_stackmaps") {
655 StackMapSection = Sec;
656 break;
657 }
658 }
659
660 if (StackMapSection == object::SectionRef())
661 return;
662
663 StringRef StackMapContents;
664 StackMapSection.getContents(StackMapContents);
665 ArrayRef<uint8_t> StackMapContentsArray(
666 reinterpret_cast<const uint8_t*>(StackMapContents.data()),
667 StackMapContents.size());
668
669 if (Obj->isLittleEndian())
670 prettyPrintStackMap(
671 llvm::outs(),
672 StackMapV1Parser<support::little>(StackMapContentsArray));
673 else
674 prettyPrintStackMap(llvm::outs(),
675 StackMapV1Parser<support::big>(StackMapContentsArray));
676 }
677
printMachODataInCode()678 void MachODumper::printMachODataInCode() {
679 for (const auto &Load : Obj->load_commands()) {
680 if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
681 MachO::linkedit_data_command LLC = Obj->getLinkeditDataLoadCommand(Load);
682 DictScope Group(W, "DataInCode");
683 W.printNumber("Data offset", LLC.dataoff);
684 W.printNumber("Data size", LLC.datasize);
685 ListScope D(W, "Data entries");
686 unsigned NumRegions = LLC.datasize / sizeof(MachO::data_in_code_entry);
687 for (unsigned i = 0; i < NumRegions; ++i) {
688 MachO::data_in_code_entry DICE = Obj->getDataInCodeTableEntry(
689 LLC.dataoff, i);
690 DictScope Group(W, "Entry");
691 W.printNumber("Index", i);
692 W.printNumber("Offset", DICE.offset);
693 W.printNumber("Length", DICE.length);
694 W.printNumber("Kind", DICE.kind);
695 }
696 }
697 }
698 }
699
printMachOVersionMin()700 void MachODumper::printMachOVersionMin() {
701 for (const auto &Load : Obj->load_commands()) {
702 StringRef Cmd;
703 switch (Load.C.cmd) {
704 case MachO::LC_VERSION_MIN_MACOSX:
705 Cmd = "LC_VERSION_MIN_MACOSX";
706 break;
707 case MachO::LC_VERSION_MIN_IPHONEOS:
708 Cmd = "LC_VERSION_MIN_IPHONEOS";
709 break;
710 case MachO::LC_VERSION_MIN_TVOS:
711 Cmd = "LC_VERSION_MIN_TVOS";
712 break;
713 case MachO::LC_VERSION_MIN_WATCHOS:
714 Cmd = "LC_VERSION_MIN_WATCHOS";
715 break;
716 default:
717 continue;
718 }
719
720 MachO::version_min_command VMC = Obj->getVersionMinLoadCommand(Load);
721 DictScope Group(W, "MinVersion");
722 W.printString("Cmd", Cmd);
723 W.printNumber("Size", VMC.cmdsize);
724 SmallString<32> Version;
725 Version = utostr(MachOObjectFile::getVersionMinMajor(VMC, false)) + "." +
726 utostr(MachOObjectFile::getVersionMinMinor(VMC, false));
727 uint32_t Update = MachOObjectFile::getVersionMinUpdate(VMC, false);
728 if (Update != 0)
729 Version += "." + utostr(MachOObjectFile::getVersionMinUpdate(VMC, false));
730 W.printString("Version", Version);
731 SmallString<32> SDK;
732 if (VMC.sdk == 0)
733 SDK = "n/a";
734 else {
735 SDK = utostr(MachOObjectFile::getVersionMinMajor(VMC, true)) + "." +
736 utostr(MachOObjectFile::getVersionMinMinor(VMC, true));
737 uint32_t Update = MachOObjectFile::getVersionMinUpdate(VMC, true);
738 if (Update != 0)
739 SDK += "." + utostr(MachOObjectFile::getVersionMinUpdate(VMC, true));
740 }
741 W.printString("SDK", SDK);
742 }
743 }
744
printMachODysymtab()745 void MachODumper::printMachODysymtab() {
746 for (const auto &Load : Obj->load_commands()) {
747 if (Load.C.cmd == MachO::LC_DYSYMTAB) {
748 MachO::dysymtab_command DLC = Obj->getDysymtabLoadCommand();
749 DictScope Group(W, "Dysymtab");
750 W.printNumber("ilocalsym", DLC.ilocalsym);
751 W.printNumber("nlocalsym", DLC.nlocalsym);
752 W.printNumber("iextdefsym", DLC.iextdefsym);
753 W.printNumber("nextdefsym", DLC.nextdefsym);
754 W.printNumber("iundefsym", DLC.iundefsym);
755 W.printNumber("nundefsym", DLC.nundefsym);
756 W.printNumber("tocoff", DLC.tocoff);
757 W.printNumber("ntoc", DLC.ntoc);
758 W.printNumber("modtaboff", DLC.modtaboff);
759 W.printNumber("nmodtab", DLC.nmodtab);
760 W.printNumber("extrefsymoff", DLC.extrefsymoff);
761 W.printNumber("nextrefsyms", DLC.nextrefsyms);
762 W.printNumber("indirectsymoff", DLC.indirectsymoff);
763 W.printNumber("nindirectsyms", DLC.nindirectsyms);
764 W.printNumber("extreloff", DLC.extreloff);
765 W.printNumber("nextrel", DLC.nextrel);
766 W.printNumber("locreloff", DLC.locreloff);
767 W.printNumber("nlocrel", DLC.nlocrel);
768 }
769 }
770 }
771
printMachOSegment()772 void MachODumper::printMachOSegment() {
773 for (const auto &Load : Obj->load_commands()) {
774 if (Load.C.cmd == MachO::LC_SEGMENT || Load.C.cmd == MachO::LC_SEGMENT_64) {
775 MachOSegment MOSegment;
776 getSegment(Obj, Load, MOSegment);
777 DictScope Group(W, "Segment");
778 W.printString("Cmd", MOSegment.CmdName);
779 W.printString("Name", MOSegment.SegName);
780 W.printNumber("Size", MOSegment.cmdsize);
781 W.printHex("vmaddr", MOSegment.vmaddr);
782 W.printHex("vmsize", MOSegment.vmsize);
783 W.printNumber("fileoff", MOSegment.fileoff);
784 W.printNumber("filesize", MOSegment.filesize);
785 W.printString("maxprot", getMask(MOSegment.maxprot));
786 W.printString("initprot", getMask(MOSegment.initprot));
787 W.printNumber("nsects", MOSegment.nsects);
788 W.printHex("flags", MOSegment.flags);
789 }
790 }
791 }
792
printMachOIndirectSymbols()793 void MachODumper::printMachOIndirectSymbols() {
794 for (const auto &Load : Obj->load_commands()) {
795 if (Load.C.cmd == MachO::LC_DYSYMTAB) {
796 MachO::dysymtab_command DLC = Obj->getDysymtabLoadCommand();
797 DictScope Group(W, "Indirect Symbols");
798 W.printNumber("Number", DLC.nindirectsyms);
799 ListScope D(W, "Symbols");
800 for (unsigned i = 0; i < DLC.nindirectsyms; ++i) {
801 DictScope Group(W, "Entry");
802 W.printNumber("Entry Index", i);
803 W.printHex("Symbol Index", Obj->getIndirectSymbolTableEntry(DLC, i));
804 }
805 }
806 }
807 }
808
printMachOLinkerOptions()809 void MachODumper::printMachOLinkerOptions() {
810 for (const auto &Load : Obj->load_commands()) {
811 if (Load.C.cmd == MachO::LC_LINKER_OPTION) {
812 MachO::linker_option_command LOLC = Obj->getLinkerOptionLoadCommand(Load);
813 DictScope Group(W, "Linker Options");
814 W.printNumber("Size", LOLC.cmdsize);
815 ListScope D(W, "Strings");
816 uint64_t DataSize = LOLC.cmdsize - sizeof(MachO::linker_option_command);
817 const char *P = Load.Ptr + sizeof(MachO::linker_option_command);
818 StringRef Data(P, DataSize);
819 for (unsigned i = 0; i < LOLC.count; ++i) {
820 std::pair<StringRef,StringRef> Split = Data.split('\0');
821 W.printString("Value", Split.first);
822 Data = Split.second;
823 }
824 }
825 }
826 }
827