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 "llvm-readobj.h"
15 #include "Error.h"
16 #include "ObjDumper.h"
17 #include "StreamWriter.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 
23 using namespace llvm;
24 using namespace object;
25 
26 namespace {
27 
28 class MachODumper : public ObjDumper {
29 public:
MachODumper(const MachOObjectFile * Obj,StreamWriter & Writer)30   MachODumper(const MachOObjectFile *Obj, StreamWriter& Writer)
31     : ObjDumper(Writer)
32     , 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 
41 private:
42   template<class MachHeader>
43   void printFileHeaders(const MachHeader &Header);
44 
45   void printSymbol(const SymbolRef &Symbol);
46 
47   void printRelocation(const RelocationRef &Reloc);
48 
49   void printRelocation(const MachOObjectFile *Obj, const RelocationRef &Reloc);
50 
51   void printSections(const MachOObjectFile *Obj);
52 
53   const MachOObjectFile *Obj;
54 };
55 
56 } // namespace
57 
58 
59 namespace llvm {
60 
createMachODumper(const object::ObjectFile * Obj,StreamWriter & Writer,std::unique_ptr<ObjDumper> & Result)61 std::error_code createMachODumper(const object::ObjectFile *Obj,
62                                   StreamWriter &Writer,
63                                   std::unique_ptr<ObjDumper> &Result) {
64   const MachOObjectFile *MachOObj = dyn_cast<MachOObjectFile>(Obj);
65   if (!MachOObj)
66     return readobj_error::unsupported_obj_file_format;
67 
68   Result.reset(new MachODumper(MachOObj, Writer));
69   return readobj_error::success;
70 }
71 
72 } // namespace llvm
73 
74 static const EnumEntry<uint32_t> MachOMagics[] = {
75   { "Magic",      MachO::MH_MAGIC    },
76   { "Cigam",      MachO::MH_CIGAM    },
77   { "Magic64",    MachO::MH_MAGIC_64 },
78   { "Cigam64",    MachO::MH_CIGAM_64 },
79   { "FatMagic",   MachO::FAT_MAGIC   },
80   { "FatCigam",   MachO::FAT_CIGAM   },
81 };
82 
83 static const EnumEntry<uint32_t> MachOHeaderFileTypes[] = {
84   { "Relocatable",          MachO::MH_OBJECT      },
85   { "Executable",           MachO::MH_EXECUTE     },
86   { "FixedVMLibrary",       MachO::MH_FVMLIB      },
87   { "Core",                 MachO::MH_CORE        },
88   { "PreloadedExecutable",  MachO::MH_PRELOAD     },
89   { "DynamicLibrary",       MachO::MH_DYLIB       },
90   { "DynamicLinker",        MachO::MH_DYLINKER    },
91   { "Bundle",               MachO::MH_BUNDLE      },
92   { "DynamicLibraryStub",   MachO::MH_DYLIB_STUB  },
93   { "DWARFSymbol",          MachO::MH_DSYM        },
94   { "KextBundle",           MachO::MH_KEXT_BUNDLE },
95 };
96 
97 static const EnumEntry<uint32_t> MachOHeaderCpuTypes[] = {
98   { "Any"       , static_cast<uint32_t>(MachO::CPU_TYPE_ANY) },
99   { "X86"       , MachO::CPU_TYPE_X86       },
100   { "X86-64"    , MachO::CPU_TYPE_X86_64    },
101   { "Mc98000"   , MachO::CPU_TYPE_MC98000   },
102   { "Arm"       , MachO::CPU_TYPE_ARM       },
103   { "Arm64"     , MachO::CPU_TYPE_ARM64     },
104   { "Sparc"     , MachO::CPU_TYPE_SPARC     },
105   { "PowerPC"   , MachO::CPU_TYPE_POWERPC   },
106   { "PowerPC64" , MachO::CPU_TYPE_POWERPC64 },
107 };
108 
109 static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesX86[] = {
110   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_I386_ALL),
111   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_386),
112   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_486),
113   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_486SX),
114   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_586),
115   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTPRO),
116   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTII_M3),
117   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTII_M5),
118   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_CELERON),
119   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_CELERON_MOBILE),
120   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_3),
121   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_3_M),
122   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_3_XEON),
123   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_M),
124   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_4),
125   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_4_M),
126   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ITANIUM),
127   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ITANIUM_2),
128   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_XEON),
129   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_XEON_MP),
130 };
131 
132 static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesX64[] = {
133   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_X86_64_ALL),
134   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_X86_ARCH1),
135   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_X86_64_H),
136 };
137 
138 static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesARM[] = {
139   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_ALL),
140   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V4T),
141   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V6),
142   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V5),
143   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V5TEJ),
144   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_XSCALE),
145   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7),
146   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7S),
147   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7K),
148   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V6M),
149   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7M),
150   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7EM),
151 };
152 
153 static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesARM64[] = {
154   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM64_ALL),
155 };
156 
157 static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesSPARC[] = {
158   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_SPARC_ALL),
159 };
160 
161 static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesPPC[] = {
162   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_ALL),
163   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_601),
164   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_602),
165   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_603),
166   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_603e),
167   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_603ev),
168   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_604),
169   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_604e),
170   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_620),
171   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_750),
172   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_7400),
173   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_7450),
174   LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_970),
175 };
176 
177 static const EnumEntry<uint32_t> MachOHeaderFlags[] = {
178   LLVM_READOBJ_ENUM_ENT(MachO, MH_NOUNDEFS),
179   LLVM_READOBJ_ENUM_ENT(MachO, MH_INCRLINK),
180   LLVM_READOBJ_ENUM_ENT(MachO, MH_DYLDLINK),
181   LLVM_READOBJ_ENUM_ENT(MachO, MH_BINDATLOAD),
182   LLVM_READOBJ_ENUM_ENT(MachO, MH_PREBOUND),
183   LLVM_READOBJ_ENUM_ENT(MachO, MH_SPLIT_SEGS),
184   LLVM_READOBJ_ENUM_ENT(MachO, MH_LAZY_INIT),
185   LLVM_READOBJ_ENUM_ENT(MachO, MH_TWOLEVEL),
186   LLVM_READOBJ_ENUM_ENT(MachO, MH_FORCE_FLAT),
187   LLVM_READOBJ_ENUM_ENT(MachO, MH_NOMULTIDEFS),
188   LLVM_READOBJ_ENUM_ENT(MachO, MH_NOFIXPREBINDING),
189   LLVM_READOBJ_ENUM_ENT(MachO, MH_PREBINDABLE),
190   LLVM_READOBJ_ENUM_ENT(MachO, MH_ALLMODSBOUND),
191   LLVM_READOBJ_ENUM_ENT(MachO, MH_SUBSECTIONS_VIA_SYMBOLS),
192   LLVM_READOBJ_ENUM_ENT(MachO, MH_CANONICAL),
193   LLVM_READOBJ_ENUM_ENT(MachO, MH_WEAK_DEFINES),
194   LLVM_READOBJ_ENUM_ENT(MachO, MH_BINDS_TO_WEAK),
195   LLVM_READOBJ_ENUM_ENT(MachO, MH_ALLOW_STACK_EXECUTION),
196   LLVM_READOBJ_ENUM_ENT(MachO, MH_ROOT_SAFE),
197   LLVM_READOBJ_ENUM_ENT(MachO, MH_SETUID_SAFE),
198   LLVM_READOBJ_ENUM_ENT(MachO, MH_NO_REEXPORTED_DYLIBS),
199   LLVM_READOBJ_ENUM_ENT(MachO, MH_PIE),
200   LLVM_READOBJ_ENUM_ENT(MachO, MH_DEAD_STRIPPABLE_DYLIB),
201   LLVM_READOBJ_ENUM_ENT(MachO, MH_HAS_TLV_DESCRIPTORS),
202   LLVM_READOBJ_ENUM_ENT(MachO, MH_NO_HEAP_EXECUTION),
203   LLVM_READOBJ_ENUM_ENT(MachO, MH_APP_EXTENSION_SAFE),
204 };
205 
206 static const EnumEntry<unsigned> MachOSectionTypes[] = {
207   { "Regular"                        , 0x00 },
208   { "ZeroFill"                       , 0x01 },
209   { "CStringLiterals"                , 0x02 },
210   { "4ByteLiterals"                  , 0x03 },
211   { "8ByteLiterals"                  , 0x04 },
212   { "LiteralPointers"                , 0x05 },
213   { "NonLazySymbolPointers"          , 0x06 },
214   { "LazySymbolPointers"             , 0x07 },
215   { "SymbolStubs"                    , 0x08 },
216   { "ModInitFuncs"                   , 0x09 },
217   { "ModTermFuncs"                   , 0x0A },
218   { "Coalesced"                      , 0x0B },
219   { "GBZeroFill"                     , 0x0C },
220   { "Interposing"                    , 0x0D },
221   { "16ByteLiterals"                 , 0x0E },
222   { "DTraceDOF"                      , 0x0F },
223   { "LazyDylibSymbolPoints"          , 0x10 },
224   { "ThreadLocalRegular"             , 0x11 },
225   { "ThreadLocalZerofill"            , 0x12 },
226   { "ThreadLocalVariables"           , 0x13 },
227   { "ThreadLocalVariablePointers"    , 0x14 },
228   { "ThreadLocalInitFunctionPointers", 0x15 }
229 };
230 
231 static const EnumEntry<unsigned> MachOSectionAttributes[] = {
232   { "LocReloc"         , 1 <<  0 /*S_ATTR_LOC_RELOC          */ },
233   { "ExtReloc"         , 1 <<  1 /*S_ATTR_EXT_RELOC          */ },
234   { "SomeInstructions" , 1 <<  2 /*S_ATTR_SOME_INSTRUCTIONS  */ },
235   { "Debug"            , 1 << 17 /*S_ATTR_DEBUG              */ },
236   { "SelfModifyingCode", 1 << 18 /*S_ATTR_SELF_MODIFYING_CODE*/ },
237   { "LiveSupport"      , 1 << 19 /*S_ATTR_LIVE_SUPPORT       */ },
238   { "NoDeadStrip"      , 1 << 20 /*S_ATTR_NO_DEAD_STRIP      */ },
239   { "StripStaticSyms"  , 1 << 21 /*S_ATTR_STRIP_STATIC_SYMS  */ },
240   { "NoTOC"            , 1 << 22 /*S_ATTR_NO_TOC             */ },
241   { "PureInstructions" , 1 << 23 /*S_ATTR_PURE_INSTRUCTIONS  */ },
242 };
243 
244 static const EnumEntry<unsigned> MachOSymbolRefTypes[] = {
245   { "UndefinedNonLazy",                     0 },
246   { "ReferenceFlagUndefinedLazy",           1 },
247   { "ReferenceFlagDefined",                 2 },
248   { "ReferenceFlagPrivateDefined",          3 },
249   { "ReferenceFlagPrivateUndefinedNonLazy", 4 },
250   { "ReferenceFlagPrivateUndefinedLazy",    5 }
251 };
252 
253 static const EnumEntry<unsigned> MachOSymbolFlags[] = {
254   { "ReferencedDynamically", 0x10 },
255   { "NoDeadStrip",           0x20 },
256   { "WeakRef",               0x40 },
257   { "WeakDef",               0x80 }
258 };
259 
260 static const EnumEntry<unsigned> MachOSymbolTypes[] = {
261   { "Undef",           0x0 },
262   { "Abs",             0x2 },
263   { "Indirect",        0xA },
264   { "PreboundUndef",   0xC },
265   { "Section",         0xE }
266 };
267 
268 namespace {
269   struct MachOSection {
270     ArrayRef<char> Name;
271     ArrayRef<char> SegmentName;
272     uint64_t Address;
273     uint64_t Size;
274     uint32_t Offset;
275     uint32_t Alignment;
276     uint32_t RelocationTableOffset;
277     uint32_t NumRelocationTableEntries;
278     uint32_t Flags;
279     uint32_t Reserved1;
280     uint32_t Reserved2;
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 
getSection(const MachOObjectFile * Obj,DataRefImpl Sec,MachOSection & Section)292 static void getSection(const MachOObjectFile *Obj,
293                        DataRefImpl Sec,
294                        MachOSection &Section) {
295   if (!Obj->is64Bit()) {
296     MachO::section Sect = Obj->getSection(Sec);
297     Section.Address     = Sect.addr;
298     Section.Size        = Sect.size;
299     Section.Offset      = Sect.offset;
300     Section.Alignment   = Sect.align;
301     Section.RelocationTableOffset = Sect.reloff;
302     Section.NumRelocationTableEntries = Sect.nreloc;
303     Section.Flags       = Sect.flags;
304     Section.Reserved1   = Sect.reserved1;
305     Section.Reserved2   = Sect.reserved2;
306     return;
307   }
308   MachO::section_64 Sect = Obj->getSection64(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 }
319 
320 
getSymbol(const MachOObjectFile * Obj,DataRefImpl DRI,MachOSymbol & Symbol)321 static void getSymbol(const MachOObjectFile *Obj,
322                       DataRefImpl DRI,
323                       MachOSymbol &Symbol) {
324   if (!Obj->is64Bit()) {
325     MachO::nlist Entry = Obj->getSymbolTableEntry(DRI);
326     Symbol.StringIndex  = Entry.n_strx;
327     Symbol.Type         = Entry.n_type;
328     Symbol.SectionIndex = Entry.n_sect;
329     Symbol.Flags        = Entry.n_desc;
330     Symbol.Value        = Entry.n_value;
331     return;
332   }
333   MachO::nlist_64 Entry = Obj->getSymbol64TableEntry(DRI);
334   Symbol.StringIndex  = Entry.n_strx;
335   Symbol.Type         = Entry.n_type;
336   Symbol.SectionIndex = Entry.n_sect;
337   Symbol.Flags        = Entry.n_desc;
338   Symbol.Value        = Entry.n_value;
339 }
340 
printFileHeaders()341 void MachODumper::printFileHeaders() {
342   DictScope H(W, "MachHeader");
343   if (!Obj->is64Bit()) {
344     printFileHeaders(Obj->getHeader());
345   } else {
346     printFileHeaders(Obj->getHeader64());
347     W.printHex("Reserved", Obj->getHeader64().reserved);
348   }
349 }
350 
351 template<class MachHeader>
printFileHeaders(const MachHeader & Header)352 void MachODumper::printFileHeaders(const MachHeader &Header) {
353   W.printEnum("Magic", Header.magic, makeArrayRef(MachOMagics));
354   W.printEnum("CpuType", Header.cputype, makeArrayRef(MachOHeaderCpuTypes));
355   uint32_t subtype = Header.cpusubtype & ~MachO::CPU_SUBTYPE_MASK;
356   switch (Header.cputype) {
357   case MachO::CPU_TYPE_X86:
358     W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesX86));
359     break;
360   case MachO::CPU_TYPE_X86_64:
361     W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesX64));
362     break;
363   case MachO::CPU_TYPE_ARM:
364     W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesARM));
365     break;
366   case MachO::CPU_TYPE_POWERPC:
367     W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesPPC));
368     break;
369   case MachO::CPU_TYPE_SPARC:
370     W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesSPARC));
371     break;
372   case MachO::CPU_TYPE_ARM64:
373     W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesARM64));
374     break;
375   case MachO::CPU_TYPE_POWERPC64:
376   default:
377     W.printHex("CpuSubtype", subtype);
378   }
379   W.printEnum("FileType", Header.filetype, makeArrayRef(MachOHeaderFileTypes));
380   W.printNumber("NumOfLoadCommands", Header.ncmds);
381   W.printNumber("SizeOfLoadCommands", Header.sizeofcmds);
382   W.printFlags("Flags", Header.flags, makeArrayRef(MachOHeaderFlags));
383 }
384 
printSections()385 void MachODumper::printSections() {
386   return printSections(Obj);
387 }
388 
printSections(const MachOObjectFile * Obj)389 void MachODumper::printSections(const MachOObjectFile *Obj) {
390   ListScope Group(W, "Sections");
391 
392   int SectionIndex = -1;
393   for (const SectionRef &Section : Obj->sections()) {
394     ++SectionIndex;
395 
396     MachOSection MOSection;
397     getSection(Obj, Section.getRawDataRefImpl(), MOSection);
398     DataRefImpl DR = Section.getRawDataRefImpl();
399 
400     StringRef Name;
401     if (error(Section.getName(Name)))
402       Name = "";
403 
404     ArrayRef<char> RawName = Obj->getSectionRawName(DR);
405     StringRef SegmentName = Obj->getSectionFinalSegmentName(DR);
406     ArrayRef<char> RawSegmentName = Obj->getSectionRawFinalSegmentName(DR);
407 
408     DictScope SectionD(W, "Section");
409     W.printNumber("Index", SectionIndex);
410     W.printBinary("Name", Name, RawName);
411     W.printBinary("Segment", SegmentName, RawSegmentName);
412     W.printHex("Address", MOSection.Address);
413     W.printHex("Size", MOSection.Size);
414     W.printNumber("Offset", MOSection.Offset);
415     W.printNumber("Alignment", MOSection.Alignment);
416     W.printHex("RelocationOffset", MOSection.RelocationTableOffset);
417     W.printNumber("RelocationCount", MOSection.NumRelocationTableEntries);
418     W.printEnum("Type", MOSection.Flags & 0xFF,
419                 makeArrayRef(MachOSectionAttributes));
420     W.printFlags("Attributes", MOSection.Flags >> 8,
421                  makeArrayRef(MachOSectionAttributes));
422     W.printHex("Reserved1", MOSection.Reserved1);
423     W.printHex("Reserved2", MOSection.Reserved2);
424 
425     if (opts::SectionRelocations) {
426       ListScope D(W, "Relocations");
427       for (const RelocationRef &Reloc : Section.relocations())
428         printRelocation(Reloc);
429     }
430 
431     if (opts::SectionSymbols) {
432       ListScope D(W, "Symbols");
433       for (const SymbolRef &Symbol : Obj->symbols()) {
434         if (!Section.containsSymbol(Symbol))
435           continue;
436 
437         printSymbol(Symbol);
438       }
439     }
440 
441     if (opts::SectionData) {
442       bool IsBSS = Section.isBSS();
443       if (!IsBSS) {
444         StringRef Data;
445         if (error(Section.getContents(Data)))
446           break;
447 
448         W.printBinaryBlock("SectionData", Data);
449       }
450     }
451   }
452 }
453 
printRelocations()454 void MachODumper::printRelocations() {
455   ListScope D(W, "Relocations");
456 
457   std::error_code EC;
458   for (const SectionRef &Section : Obj->sections()) {
459     StringRef Name;
460     if (error(Section.getName(Name)))
461       continue;
462 
463     bool PrintedGroup = false;
464     for (const RelocationRef &Reloc : Section.relocations()) {
465       if (!PrintedGroup) {
466         W.startLine() << "Section " << Name << " {\n";
467         W.indent();
468         PrintedGroup = true;
469       }
470 
471       printRelocation(Reloc);
472     }
473 
474     if (PrintedGroup) {
475       W.unindent();
476       W.startLine() << "}\n";
477     }
478   }
479 }
480 
printRelocation(const RelocationRef & Reloc)481 void MachODumper::printRelocation(const RelocationRef &Reloc) {
482   return printRelocation(Obj, Reloc);
483 }
484 
printRelocation(const MachOObjectFile * Obj,const RelocationRef & Reloc)485 void MachODumper::printRelocation(const MachOObjectFile *Obj,
486                                   const RelocationRef &Reloc) {
487   uint64_t Offset;
488   SmallString<32> RelocName;
489   if (error(Reloc.getOffset(Offset)))
490     return;
491   if (error(Reloc.getTypeName(RelocName)))
492     return;
493 
494   DataRefImpl DR = Reloc.getRawDataRefImpl();
495   MachO::any_relocation_info RE = Obj->getRelocation(DR);
496   bool IsScattered = Obj->isRelocationScattered(RE);
497   SmallString<32> SymbolNameOrOffset("0x");
498   if (IsScattered) {
499     // Scattered relocations don't really have an associated symbol
500     // for some reason, even if one exists in the symtab at the correct address.
501     SymbolNameOrOffset += utohexstr(Obj->getScatteredRelocationValue(RE));
502   } else {
503     symbol_iterator Symbol = Reloc.getSymbol();
504     if (Symbol != Obj->symbol_end()) {
505       StringRef SymbolName;
506       if (error(Symbol->getName(SymbolName)))
507         return;
508       SymbolNameOrOffset = SymbolName;
509     } else
510       SymbolNameOrOffset += utohexstr(Obj->getPlainRelocationSymbolNum(RE));
511   }
512 
513   if (opts::ExpandRelocs) {
514     DictScope Group(W, "Relocation");
515     W.printHex("Offset", Offset);
516     W.printNumber("PCRel", Obj->getAnyRelocationPCRel(RE));
517     W.printNumber("Length", Obj->getAnyRelocationLength(RE));
518     if (IsScattered)
519       W.printString("Extern", StringRef("N/A"));
520     else
521       W.printNumber("Extern", Obj->getPlainRelocationExternal(RE));
522     W.printNumber("Type", RelocName, Obj->getAnyRelocationType(RE));
523     W.printString("Symbol", SymbolNameOrOffset);
524     W.printNumber("Scattered", IsScattered);
525   } else {
526     raw_ostream& OS = W.startLine();
527     OS << W.hex(Offset)
528        << " " << Obj->getAnyRelocationPCRel(RE)
529        << " " << Obj->getAnyRelocationLength(RE);
530     if (IsScattered)
531       OS << " n/a";
532     else
533       OS << " " << Obj->getPlainRelocationExternal(RE);
534     OS << " " << RelocName
535        << " " << IsScattered
536        << " " << SymbolNameOrOffset
537        << "\n";
538   }
539 }
540 
printSymbols()541 void MachODumper::printSymbols() {
542   ListScope Group(W, "Symbols");
543 
544   for (const SymbolRef &Symbol : Obj->symbols()) {
545     printSymbol(Symbol);
546   }
547 }
548 
printDynamicSymbols()549 void MachODumper::printDynamicSymbols() {
550   ListScope Group(W, "DynamicSymbols");
551 }
552 
printSymbol(const SymbolRef & Symbol)553 void MachODumper::printSymbol(const SymbolRef &Symbol) {
554   StringRef SymbolName;
555   if (Symbol.getName(SymbolName))
556     SymbolName = "";
557 
558   MachOSymbol MOSymbol;
559   getSymbol(Obj, Symbol.getRawDataRefImpl(), MOSymbol);
560 
561   StringRef SectionName = "";
562   section_iterator SecI(Obj->section_begin());
563   if (!error(Symbol.getSection(SecI)) && SecI != Obj->section_end())
564     error(SecI->getName(SectionName));
565 
566   DictScope D(W, "Symbol");
567   W.printNumber("Name", SymbolName, MOSymbol.StringIndex);
568   if (MOSymbol.Type & MachO::N_STAB) {
569     W.printHex("Type", "SymDebugTable", MOSymbol.Type);
570   } else {
571     if (MOSymbol.Type & MachO::N_PEXT)
572       W.startLine() << "PrivateExtern\n";
573     if (MOSymbol.Type & MachO::N_EXT)
574       W.startLine() << "Extern\n";
575     W.printEnum("Type", uint8_t(MOSymbol.Type & MachO::N_TYPE),
576                 makeArrayRef(MachOSymbolTypes));
577   }
578   W.printHex("Section", SectionName, MOSymbol.SectionIndex);
579   W.printEnum("RefType", static_cast<uint16_t>(MOSymbol.Flags & 0xF),
580               makeArrayRef(MachOSymbolRefTypes));
581   W.printFlags("Flags", static_cast<uint16_t>(MOSymbol.Flags & ~0xF),
582                makeArrayRef(MachOSymbolFlags));
583   W.printHex("Value", MOSymbol.Value);
584 }
585 
printUnwindInfo()586 void MachODumper::printUnwindInfo() {
587   W.startLine() << "UnwindInfo not implemented.\n";
588 }
589