]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-objdump/MachODump.cpp
Merge ^/head r311314 through r311459.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / llvm-objdump / MachODump.cpp
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-objdump.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Object/MachO.h"
15 #include "llvm-objdump.h"
16 #include "llvm-c/Disassembler.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/Config/config.h"
21 #include "llvm/DebugInfo/DIContext.h"
22 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
23 #include "llvm/Demangle/Demangle.h"
24 #include "llvm/MC/MCAsmInfo.h"
25 #include "llvm/MC/MCContext.h"
26 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
27 #include "llvm/MC/MCInst.h"
28 #include "llvm/MC/MCInstPrinter.h"
29 #include "llvm/MC/MCInstrDesc.h"
30 #include "llvm/MC/MCInstrInfo.h"
31 #include "llvm/MC/MCRegisterInfo.h"
32 #include "llvm/MC/MCSubtargetInfo.h"
33 #include "llvm/Object/MachOUniversal.h"
34 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/Endian.h"
38 #include "llvm/Support/Format.h"
39 #include "llvm/Support/FormattedStream.h"
40 #include "llvm/Support/GraphWriter.h"
41 #include "llvm/Support/LEB128.h"
42 #include "llvm/Support/MachO.h"
43 #include "llvm/Support/MemoryBuffer.h"
44 #include "llvm/Support/TargetRegistry.h"
45 #include "llvm/Support/TargetSelect.h"
46 #include "llvm/Support/ToolOutputFile.h"
47 #include "llvm/Support/raw_ostream.h"
48 #include <algorithm>
49 #include <cstring>
50 #include <system_error>
51
52 #ifdef HAVE_LIBXAR
53 extern "C" {
54 #include <xar/xar.h>
55 }
56 #endif
57
58 using namespace llvm;
59 using namespace object;
60
61 static cl::opt<bool>
62     UseDbg("g",
63            cl::desc("Print line information from debug info if available"));
64
65 static cl::opt<std::string> DSYMFile("dsym",
66                                      cl::desc("Use .dSYM file for debug info"));
67
68 static cl::opt<bool> FullLeadingAddr("full-leading-addr",
69                                      cl::desc("Print full leading address"));
70
71 static cl::opt<bool> NoLeadingAddr("no-leading-addr",
72                                    cl::desc("Print no leading address"));
73
74 static cl::opt<bool> NoLeadingHeaders("no-leading-headers",
75                                       cl::desc("Print no leading headers"));
76
77 cl::opt<bool> llvm::UniversalHeaders("universal-headers",
78                                      cl::desc("Print Mach-O universal headers "
79                                               "(requires -macho)"));
80
81 cl::opt<bool>
82     llvm::ArchiveHeaders("archive-headers",
83                          cl::desc("Print archive headers for Mach-O archives "
84                                   "(requires -macho)"));
85
86 cl::opt<bool>
87     ArchiveMemberOffsets("archive-member-offsets",
88                          cl::desc("Print the offset to each archive member for "
89                                   "Mach-O archives (requires -macho and "
90                                   "-archive-headers)"));
91
92 cl::opt<bool>
93     llvm::IndirectSymbols("indirect-symbols",
94                           cl::desc("Print indirect symbol table for Mach-O "
95                                    "objects (requires -macho)"));
96
97 cl::opt<bool>
98     llvm::DataInCode("data-in-code",
99                      cl::desc("Print the data in code table for Mach-O objects "
100                               "(requires -macho)"));
101
102 cl::opt<bool>
103     llvm::LinkOptHints("link-opt-hints",
104                        cl::desc("Print the linker optimization hints for "
105                                 "Mach-O objects (requires -macho)"));
106
107 cl::opt<bool>
108     llvm::InfoPlist("info-plist",
109                     cl::desc("Print the info plist section as strings for "
110                              "Mach-O objects (requires -macho)"));
111
112 cl::opt<bool>
113     llvm::DylibsUsed("dylibs-used",
114                      cl::desc("Print the shared libraries used for linked "
115                               "Mach-O files (requires -macho)"));
116
117 cl::opt<bool>
118     llvm::DylibId("dylib-id",
119                   cl::desc("Print the shared library's id for the dylib Mach-O "
120                            "file (requires -macho)"));
121
122 cl::opt<bool>
123     llvm::NonVerbose("non-verbose",
124                      cl::desc("Print the info for Mach-O objects in "
125                               "non-verbose or numeric form (requires -macho)"));
126
127 cl::opt<bool>
128     llvm::ObjcMetaData("objc-meta-data",
129                        cl::desc("Print the Objective-C runtime meta data for "
130                                 "Mach-O files (requires -macho)"));
131
132 cl::opt<std::string> llvm::DisSymName(
133     "dis-symname",
134     cl::desc("disassemble just this symbol's instructions (requires -macho)"));
135
136 static cl::opt<bool> NoSymbolicOperands(
137     "no-symbolic-operands",
138     cl::desc("do not symbolic operands when disassembling (requires -macho)"));
139
140 static cl::list<std::string>
141     ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
142               cl::ZeroOrMore);
143
144 bool ArchAll = false;
145
146 static std::string ThumbTripleName;
147
148 static const Target *GetTarget(const MachOObjectFile *MachOObj,
149                                const char **McpuDefault,
150                                const Target **ThumbTarget) {
151   // Figure out the target triple.
152   llvm::Triple TT(TripleName);
153   if (TripleName.empty()) {
154     TT = MachOObj->getArchTriple(McpuDefault);
155     TripleName = TT.str();
156   }
157
158   if (TT.getArch() == Triple::arm) {
159     // We've inferred a 32-bit ARM target from the object file. All MachO CPUs
160     // that support ARM are also capable of Thumb mode.
161     llvm::Triple ThumbTriple = TT;
162     std::string ThumbName = (Twine("thumb") + TT.getArchName().substr(3)).str();
163     ThumbTriple.setArchName(ThumbName);
164     ThumbTripleName = ThumbTriple.str();
165   }
166
167   // Get the target specific parser.
168   std::string Error;
169   const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
170   if (TheTarget && ThumbTripleName.empty())
171     return TheTarget;
172
173   *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
174   if (*ThumbTarget)
175     return TheTarget;
176
177   errs() << "llvm-objdump: error: unable to get target for '";
178   if (!TheTarget)
179     errs() << TripleName;
180   else
181     errs() << ThumbTripleName;
182   errs() << "', see --version and --triple.\n";
183   return nullptr;
184 }
185
186 struct SymbolSorter {
187   bool operator()(const SymbolRef &A, const SymbolRef &B) {
188     Expected<SymbolRef::Type> ATypeOrErr = A.getType();
189     if (!ATypeOrErr)
190       report_error(A.getObject()->getFileName(), ATypeOrErr.takeError());
191     SymbolRef::Type AType = *ATypeOrErr;
192     Expected<SymbolRef::Type> BTypeOrErr = B.getType();
193     if (!BTypeOrErr)
194       report_error(B.getObject()->getFileName(), BTypeOrErr.takeError());
195     SymbolRef::Type BType = *BTypeOrErr;
196     uint64_t AAddr = (AType != SymbolRef::ST_Function) ? 0 : A.getValue();
197     uint64_t BAddr = (BType != SymbolRef::ST_Function) ? 0 : B.getValue();
198     return AAddr < BAddr;
199   }
200 };
201
202 // Types for the storted data in code table that is built before disassembly
203 // and the predicate function to sort them.
204 typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
205 typedef std::vector<DiceTableEntry> DiceTable;
206 typedef DiceTable::iterator dice_table_iterator;
207
208 // This is used to search for a data in code table entry for the PC being
209 // disassembled.  The j parameter has the PC in j.first.  A single data in code
210 // table entry can cover many bytes for each of its Kind's.  So if the offset,
211 // aka the i.first value, of the data in code table entry plus its Length
212 // covers the PC being searched for this will return true.  If not it will
213 // return false.
214 static bool compareDiceTableEntries(const DiceTableEntry &i,
215                                     const DiceTableEntry &j) {
216   uint16_t Length;
217   i.second.getLength(Length);
218
219   return j.first >= i.first && j.first < i.first + Length;
220 }
221
222 static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
223                                unsigned short Kind) {
224   uint32_t Value, Size = 1;
225
226   switch (Kind) {
227   default:
228   case MachO::DICE_KIND_DATA:
229     if (Length >= 4) {
230       if (!NoShowRawInsn)
231         dumpBytes(makeArrayRef(bytes, 4), outs());
232       Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
233       outs() << "\t.long " << Value;
234       Size = 4;
235     } else if (Length >= 2) {
236       if (!NoShowRawInsn)
237         dumpBytes(makeArrayRef(bytes, 2), outs());
238       Value = bytes[1] << 8 | bytes[0];
239       outs() << "\t.short " << Value;
240       Size = 2;
241     } else {
242       if (!NoShowRawInsn)
243         dumpBytes(makeArrayRef(bytes, 2), outs());
244       Value = bytes[0];
245       outs() << "\t.byte " << Value;
246       Size = 1;
247     }
248     if (Kind == MachO::DICE_KIND_DATA)
249       outs() << "\t@ KIND_DATA\n";
250     else
251       outs() << "\t@ data in code kind = " << Kind << "\n";
252     break;
253   case MachO::DICE_KIND_JUMP_TABLE8:
254     if (!NoShowRawInsn)
255       dumpBytes(makeArrayRef(bytes, 1), outs());
256     Value = bytes[0];
257     outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
258     Size = 1;
259     break;
260   case MachO::DICE_KIND_JUMP_TABLE16:
261     if (!NoShowRawInsn)
262       dumpBytes(makeArrayRef(bytes, 2), outs());
263     Value = bytes[1] << 8 | bytes[0];
264     outs() << "\t.short " << format("%5u", Value & 0xffff)
265            << "\t@ KIND_JUMP_TABLE16\n";
266     Size = 2;
267     break;
268   case MachO::DICE_KIND_JUMP_TABLE32:
269   case MachO::DICE_KIND_ABS_JUMP_TABLE32:
270     if (!NoShowRawInsn)
271       dumpBytes(makeArrayRef(bytes, 4), outs());
272     Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
273     outs() << "\t.long " << Value;
274     if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
275       outs() << "\t@ KIND_JUMP_TABLE32\n";
276     else
277       outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
278     Size = 4;
279     break;
280   }
281   return Size;
282 }
283
284 static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
285                                   std::vector<SectionRef> &Sections,
286                                   std::vector<SymbolRef> &Symbols,
287                                   SmallVectorImpl<uint64_t> &FoundFns,
288                                   uint64_t &BaseSegmentAddress) {
289   for (const SymbolRef &Symbol : MachOObj->symbols()) {
290     Expected<StringRef> SymName = Symbol.getName();
291     if (!SymName)
292       report_error(MachOObj->getFileName(), SymName.takeError());
293     if (!SymName->startswith("ltmp"))
294       Symbols.push_back(Symbol);
295   }
296
297   for (const SectionRef &Section : MachOObj->sections()) {
298     StringRef SectName;
299     Section.getName(SectName);
300     Sections.push_back(Section);
301   }
302
303   bool BaseSegmentAddressSet = false;
304   for (const auto &Command : MachOObj->load_commands()) {
305     if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
306       // We found a function starts segment, parse the addresses for later
307       // consumption.
308       MachO::linkedit_data_command LLC =
309           MachOObj->getLinkeditDataLoadCommand(Command);
310
311       MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
312     } else if (Command.C.cmd == MachO::LC_SEGMENT) {
313       MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
314       StringRef SegName = SLC.segname;
315       if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
316         BaseSegmentAddressSet = true;
317         BaseSegmentAddress = SLC.vmaddr;
318       }
319     }
320   }
321 }
322
323 static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
324                                      uint32_t n, uint32_t count,
325                                      uint32_t stride, uint64_t addr) {
326   MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
327   uint32_t nindirectsyms = Dysymtab.nindirectsyms;
328   if (n > nindirectsyms)
329     outs() << " (entries start past the end of the indirect symbol "
330               "table) (reserved1 field greater than the table size)";
331   else if (n + count > nindirectsyms)
332     outs() << " (entries extends past the end of the indirect symbol "
333               "table)";
334   outs() << "\n";
335   uint32_t cputype = O->getHeader().cputype;
336   if (cputype & MachO::CPU_ARCH_ABI64)
337     outs() << "address            index";
338   else
339     outs() << "address    index";
340   if (verbose)
341     outs() << " name\n";
342   else
343     outs() << "\n";
344   for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
345     if (cputype & MachO::CPU_ARCH_ABI64)
346       outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
347     else
348       outs() << format("0x%08" PRIx32, (uint32_t)addr + j * stride) << " ";
349     MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
350     uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
351     if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
352       outs() << "LOCAL\n";
353       continue;
354     }
355     if (indirect_symbol ==
356         (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
357       outs() << "LOCAL ABSOLUTE\n";
358       continue;
359     }
360     if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
361       outs() << "ABSOLUTE\n";
362       continue;
363     }
364     outs() << format("%5u ", indirect_symbol);
365     if (verbose) {
366       MachO::symtab_command Symtab = O->getSymtabLoadCommand();
367       if (indirect_symbol < Symtab.nsyms) {
368         symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
369         SymbolRef Symbol = *Sym;
370         Expected<StringRef> SymName = Symbol.getName();
371         if (!SymName)
372           report_error(O->getFileName(), SymName.takeError());
373         outs() << *SymName;
374       } else {
375         outs() << "?";
376       }
377     }
378     outs() << "\n";
379   }
380 }
381
382 static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
383   for (const auto &Load : O->load_commands()) {
384     if (Load.C.cmd == MachO::LC_SEGMENT_64) {
385       MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
386       for (unsigned J = 0; J < Seg.nsects; ++J) {
387         MachO::section_64 Sec = O->getSection64(Load, J);
388         uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
389         if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
390             section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
391             section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
392             section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
393             section_type == MachO::S_SYMBOL_STUBS) {
394           uint32_t stride;
395           if (section_type == MachO::S_SYMBOL_STUBS)
396             stride = Sec.reserved2;
397           else
398             stride = 8;
399           if (stride == 0) {
400             outs() << "Can't print indirect symbols for (" << Sec.segname << ","
401                    << Sec.sectname << ") "
402                    << "(size of stubs in reserved2 field is zero)\n";
403             continue;
404           }
405           uint32_t count = Sec.size / stride;
406           outs() << "Indirect symbols for (" << Sec.segname << ","
407                  << Sec.sectname << ") " << count << " entries";
408           uint32_t n = Sec.reserved1;
409           PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
410         }
411       }
412     } else if (Load.C.cmd == MachO::LC_SEGMENT) {
413       MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
414       for (unsigned J = 0; J < Seg.nsects; ++J) {
415         MachO::section Sec = O->getSection(Load, J);
416         uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
417         if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
418             section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
419             section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
420             section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
421             section_type == MachO::S_SYMBOL_STUBS) {
422           uint32_t stride;
423           if (section_type == MachO::S_SYMBOL_STUBS)
424             stride = Sec.reserved2;
425           else
426             stride = 4;
427           if (stride == 0) {
428             outs() << "Can't print indirect symbols for (" << Sec.segname << ","
429                    << Sec.sectname << ") "
430                    << "(size of stubs in reserved2 field is zero)\n";
431             continue;
432           }
433           uint32_t count = Sec.size / stride;
434           outs() << "Indirect symbols for (" << Sec.segname << ","
435                  << Sec.sectname << ") " << count << " entries";
436           uint32_t n = Sec.reserved1;
437           PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
438         }
439       }
440     }
441   }
442 }
443
444 static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
445   MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
446   uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
447   outs() << "Data in code table (" << nentries << " entries)\n";
448   outs() << "offset     length kind\n";
449   for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
450        ++DI) {
451     uint32_t Offset;
452     DI->getOffset(Offset);
453     outs() << format("0x%08" PRIx32, Offset) << " ";
454     uint16_t Length;
455     DI->getLength(Length);
456     outs() << format("%6u", Length) << " ";
457     uint16_t Kind;
458     DI->getKind(Kind);
459     if (verbose) {
460       switch (Kind) {
461       case MachO::DICE_KIND_DATA:
462         outs() << "DATA";
463         break;
464       case MachO::DICE_KIND_JUMP_TABLE8:
465         outs() << "JUMP_TABLE8";
466         break;
467       case MachO::DICE_KIND_JUMP_TABLE16:
468         outs() << "JUMP_TABLE16";
469         break;
470       case MachO::DICE_KIND_JUMP_TABLE32:
471         outs() << "JUMP_TABLE32";
472         break;
473       case MachO::DICE_KIND_ABS_JUMP_TABLE32:
474         outs() << "ABS_JUMP_TABLE32";
475         break;
476       default:
477         outs() << format("0x%04" PRIx32, Kind);
478         break;
479       }
480     } else
481       outs() << format("0x%04" PRIx32, Kind);
482     outs() << "\n";
483   }
484 }
485
486 static void PrintLinkOptHints(MachOObjectFile *O) {
487   MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
488   const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
489   uint32_t nloh = LohLC.datasize;
490   outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
491   for (uint32_t i = 0; i < nloh;) {
492     unsigned n;
493     uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
494     i += n;
495     outs() << "    identifier " << identifier << " ";
496     if (i >= nloh)
497       return;
498     switch (identifier) {
499     case 1:
500       outs() << "AdrpAdrp\n";
501       break;
502     case 2:
503       outs() << "AdrpLdr\n";
504       break;
505     case 3:
506       outs() << "AdrpAddLdr\n";
507       break;
508     case 4:
509       outs() << "AdrpLdrGotLdr\n";
510       break;
511     case 5:
512       outs() << "AdrpAddStr\n";
513       break;
514     case 6:
515       outs() << "AdrpLdrGotStr\n";
516       break;
517     case 7:
518       outs() << "AdrpAdd\n";
519       break;
520     case 8:
521       outs() << "AdrpLdrGot\n";
522       break;
523     default:
524       outs() << "Unknown identifier value\n";
525       break;
526     }
527     uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
528     i += n;
529     outs() << "    narguments " << narguments << "\n";
530     if (i >= nloh)
531       return;
532
533     for (uint32_t j = 0; j < narguments; j++) {
534       uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
535       i += n;
536       outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
537       if (i >= nloh)
538         return;
539     }
540   }
541 }
542
543 static void PrintDylibs(MachOObjectFile *O, bool JustId) {
544   unsigned Index = 0;
545   for (const auto &Load : O->load_commands()) {
546     if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
547         (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
548                      Load.C.cmd == MachO::LC_LOAD_DYLIB ||
549                      Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
550                      Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
551                      Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
552                      Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
553       MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
554       if (dl.dylib.name < dl.cmdsize) {
555         const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
556         if (JustId)
557           outs() << p << "\n";
558         else {
559           outs() << "\t" << p;
560           outs() << " (compatibility version "
561                  << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
562                  << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
563                  << (dl.dylib.compatibility_version & 0xff) << ",";
564           outs() << " current version "
565                  << ((dl.dylib.current_version >> 16) & 0xffff) << "."
566                  << ((dl.dylib.current_version >> 8) & 0xff) << "."
567                  << (dl.dylib.current_version & 0xff) << ")\n";
568         }
569       } else {
570         outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
571         if (Load.C.cmd == MachO::LC_ID_DYLIB)
572           outs() << "LC_ID_DYLIB ";
573         else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
574           outs() << "LC_LOAD_DYLIB ";
575         else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
576           outs() << "LC_LOAD_WEAK_DYLIB ";
577         else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
578           outs() << "LC_LAZY_LOAD_DYLIB ";
579         else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
580           outs() << "LC_REEXPORT_DYLIB ";
581         else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
582           outs() << "LC_LOAD_UPWARD_DYLIB ";
583         else
584           outs() << "LC_??? ";
585         outs() << "command " << Index++ << "\n";
586       }
587     }
588   }
589 }
590
591 typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
592
593 static void CreateSymbolAddressMap(MachOObjectFile *O,
594                                    SymbolAddressMap *AddrMap) {
595   // Create a map of symbol addresses to symbol names.
596   for (const SymbolRef &Symbol : O->symbols()) {
597     Expected<SymbolRef::Type> STOrErr = Symbol.getType();
598     if (!STOrErr)
599       report_error(O->getFileName(), STOrErr.takeError());
600     SymbolRef::Type ST = *STOrErr;
601     if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
602         ST == SymbolRef::ST_Other) {
603       uint64_t Address = Symbol.getValue();
604       Expected<StringRef> SymNameOrErr = Symbol.getName();
605       if (!SymNameOrErr)
606         report_error(O->getFileName(), SymNameOrErr.takeError());
607       StringRef SymName = *SymNameOrErr;
608       if (!SymName.startswith(".objc"))
609         (*AddrMap)[Address] = SymName;
610     }
611   }
612 }
613
614 // GuessSymbolName is passed the address of what might be a symbol and a
615 // pointer to the SymbolAddressMap.  It returns the name of a symbol
616 // with that address or nullptr if no symbol is found with that address.
617 static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
618   const char *SymbolName = nullptr;
619   // A DenseMap can't lookup up some values.
620   if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
621     StringRef name = AddrMap->lookup(value);
622     if (!name.empty())
623       SymbolName = name.data();
624   }
625   return SymbolName;
626 }
627
628 static void DumpCstringChar(const char c) {
629   char p[2];
630   p[0] = c;
631   p[1] = '\0';
632   outs().write_escaped(p);
633 }
634
635 static void DumpCstringSection(MachOObjectFile *O, const char *sect,
636                                uint32_t sect_size, uint64_t sect_addr,
637                                bool print_addresses) {
638   for (uint32_t i = 0; i < sect_size; i++) {
639     if (print_addresses) {
640       if (O->is64Bit())
641         outs() << format("%016" PRIx64, sect_addr + i) << "  ";
642       else
643         outs() << format("%08" PRIx64, sect_addr + i) << "  ";
644     }
645     for (; i < sect_size && sect[i] != '\0'; i++)
646       DumpCstringChar(sect[i]);
647     if (i < sect_size && sect[i] == '\0')
648       outs() << "\n";
649   }
650 }
651
652 static void DumpLiteral4(uint32_t l, float f) {
653   outs() << format("0x%08" PRIx32, l);
654   if ((l & 0x7f800000) != 0x7f800000)
655     outs() << format(" (%.16e)\n", f);
656   else {
657     if (l == 0x7f800000)
658       outs() << " (+Infinity)\n";
659     else if (l == 0xff800000)
660       outs() << " (-Infinity)\n";
661     else if ((l & 0x00400000) == 0x00400000)
662       outs() << " (non-signaling Not-a-Number)\n";
663     else
664       outs() << " (signaling Not-a-Number)\n";
665   }
666 }
667
668 static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
669                                 uint32_t sect_size, uint64_t sect_addr,
670                                 bool print_addresses) {
671   for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
672     if (print_addresses) {
673       if (O->is64Bit())
674         outs() << format("%016" PRIx64, sect_addr + i) << "  ";
675       else
676         outs() << format("%08" PRIx64, sect_addr + i) << "  ";
677     }
678     float f;
679     memcpy(&f, sect + i, sizeof(float));
680     if (O->isLittleEndian() != sys::IsLittleEndianHost)
681       sys::swapByteOrder(f);
682     uint32_t l;
683     memcpy(&l, sect + i, sizeof(uint32_t));
684     if (O->isLittleEndian() != sys::IsLittleEndianHost)
685       sys::swapByteOrder(l);
686     DumpLiteral4(l, f);
687   }
688 }
689
690 static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
691                          double d) {
692   outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
693   uint32_t Hi, Lo;
694   Hi = (O->isLittleEndian()) ? l1 : l0;
695   Lo = (O->isLittleEndian()) ? l0 : l1;
696
697   // Hi is the high word, so this is equivalent to if(isfinite(d))
698   if ((Hi & 0x7ff00000) != 0x7ff00000)
699     outs() << format(" (%.16e)\n", d);
700   else {
701     if (Hi == 0x7ff00000 && Lo == 0)
702       outs() << " (+Infinity)\n";
703     else if (Hi == 0xfff00000 && Lo == 0)
704       outs() << " (-Infinity)\n";
705     else if ((Hi & 0x00080000) == 0x00080000)
706       outs() << " (non-signaling Not-a-Number)\n";
707     else
708       outs() << " (signaling Not-a-Number)\n";
709   }
710 }
711
712 static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
713                                 uint32_t sect_size, uint64_t sect_addr,
714                                 bool print_addresses) {
715   for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
716     if (print_addresses) {
717       if (O->is64Bit())
718         outs() << format("%016" PRIx64, sect_addr + i) << "  ";
719       else
720         outs() << format("%08" PRIx64, sect_addr + i) << "  ";
721     }
722     double d;
723     memcpy(&d, sect + i, sizeof(double));
724     if (O->isLittleEndian() != sys::IsLittleEndianHost)
725       sys::swapByteOrder(d);
726     uint32_t l0, l1;
727     memcpy(&l0, sect + i, sizeof(uint32_t));
728     memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
729     if (O->isLittleEndian() != sys::IsLittleEndianHost) {
730       sys::swapByteOrder(l0);
731       sys::swapByteOrder(l1);
732     }
733     DumpLiteral8(O, l0, l1, d);
734   }
735 }
736
737 static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
738   outs() << format("0x%08" PRIx32, l0) << " ";
739   outs() << format("0x%08" PRIx32, l1) << " ";
740   outs() << format("0x%08" PRIx32, l2) << " ";
741   outs() << format("0x%08" PRIx32, l3) << "\n";
742 }
743
744 static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
745                                  uint32_t sect_size, uint64_t sect_addr,
746                                  bool print_addresses) {
747   for (uint32_t i = 0; i < sect_size; i += 16) {
748     if (print_addresses) {
749       if (O->is64Bit())
750         outs() << format("%016" PRIx64, sect_addr + i) << "  ";
751       else
752         outs() << format("%08" PRIx64, sect_addr + i) << "  ";
753     }
754     uint32_t l0, l1, l2, l3;
755     memcpy(&l0, sect + i, sizeof(uint32_t));
756     memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
757     memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
758     memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
759     if (O->isLittleEndian() != sys::IsLittleEndianHost) {
760       sys::swapByteOrder(l0);
761       sys::swapByteOrder(l1);
762       sys::swapByteOrder(l2);
763       sys::swapByteOrder(l3);
764     }
765     DumpLiteral16(l0, l1, l2, l3);
766   }
767 }
768
769 static void DumpLiteralPointerSection(MachOObjectFile *O,
770                                       const SectionRef &Section,
771                                       const char *sect, uint32_t sect_size,
772                                       uint64_t sect_addr,
773                                       bool print_addresses) {
774   // Collect the literal sections in this Mach-O file.
775   std::vector<SectionRef> LiteralSections;
776   for (const SectionRef &Section : O->sections()) {
777     DataRefImpl Ref = Section.getRawDataRefImpl();
778     uint32_t section_type;
779     if (O->is64Bit()) {
780       const MachO::section_64 Sec = O->getSection64(Ref);
781       section_type = Sec.flags & MachO::SECTION_TYPE;
782     } else {
783       const MachO::section Sec = O->getSection(Ref);
784       section_type = Sec.flags & MachO::SECTION_TYPE;
785     }
786     if (section_type == MachO::S_CSTRING_LITERALS ||
787         section_type == MachO::S_4BYTE_LITERALS ||
788         section_type == MachO::S_8BYTE_LITERALS ||
789         section_type == MachO::S_16BYTE_LITERALS)
790       LiteralSections.push_back(Section);
791   }
792
793   // Set the size of the literal pointer.
794   uint32_t lp_size = O->is64Bit() ? 8 : 4;
795
796   // Collect the external relocation symbols for the literal pointers.
797   std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
798   for (const RelocationRef &Reloc : Section.relocations()) {
799     DataRefImpl Rel;
800     MachO::any_relocation_info RE;
801     bool isExtern = false;
802     Rel = Reloc.getRawDataRefImpl();
803     RE = O->getRelocation(Rel);
804     isExtern = O->getPlainRelocationExternal(RE);
805     if (isExtern) {
806       uint64_t RelocOffset = Reloc.getOffset();
807       symbol_iterator RelocSym = Reloc.getSymbol();
808       Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
809     }
810   }
811   array_pod_sort(Relocs.begin(), Relocs.end());
812
813   // Dump each literal pointer.
814   for (uint32_t i = 0; i < sect_size; i += lp_size) {
815     if (print_addresses) {
816       if (O->is64Bit())
817         outs() << format("%016" PRIx64, sect_addr + i) << "  ";
818       else
819         outs() << format("%08" PRIx64, sect_addr + i) << "  ";
820     }
821     uint64_t lp;
822     if (O->is64Bit()) {
823       memcpy(&lp, sect + i, sizeof(uint64_t));
824       if (O->isLittleEndian() != sys::IsLittleEndianHost)
825         sys::swapByteOrder(lp);
826     } else {
827       uint32_t li;
828       memcpy(&li, sect + i, sizeof(uint32_t));
829       if (O->isLittleEndian() != sys::IsLittleEndianHost)
830         sys::swapByteOrder(li);
831       lp = li;
832     }
833
834     // First look for an external relocation entry for this literal pointer.
835     auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) {
836       return P.first == i;
837     });
838     if (Reloc != Relocs.end()) {
839       symbol_iterator RelocSym = Reloc->second;
840       Expected<StringRef> SymName = RelocSym->getName();
841       if (!SymName)
842         report_error(O->getFileName(), SymName.takeError());
843       outs() << "external relocation entry for symbol:" << *SymName << "\n";
844       continue;
845     }
846
847     // For local references see what the section the literal pointer points to.
848     auto Sect = find_if(LiteralSections, [&](const SectionRef &R) {
849       return lp >= R.getAddress() && lp < R.getAddress() + R.getSize();
850     });
851     if (Sect == LiteralSections.end()) {
852       outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
853       continue;
854     }
855
856     uint64_t SectAddress = Sect->getAddress();
857     uint64_t SectSize = Sect->getSize();
858
859     StringRef SectName;
860     Sect->getName(SectName);
861     DataRefImpl Ref = Sect->getRawDataRefImpl();
862     StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
863     outs() << SegmentName << ":" << SectName << ":";
864
865     uint32_t section_type;
866     if (O->is64Bit()) {
867       const MachO::section_64 Sec = O->getSection64(Ref);
868       section_type = Sec.flags & MachO::SECTION_TYPE;
869     } else {
870       const MachO::section Sec = O->getSection(Ref);
871       section_type = Sec.flags & MachO::SECTION_TYPE;
872     }
873
874     StringRef BytesStr;
875     Sect->getContents(BytesStr);
876     const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
877
878     switch (section_type) {
879     case MachO::S_CSTRING_LITERALS:
880       for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
881            i++) {
882         DumpCstringChar(Contents[i]);
883       }
884       outs() << "\n";
885       break;
886     case MachO::S_4BYTE_LITERALS:
887       float f;
888       memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
889       uint32_t l;
890       memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
891       if (O->isLittleEndian() != sys::IsLittleEndianHost) {
892         sys::swapByteOrder(f);
893         sys::swapByteOrder(l);
894       }
895       DumpLiteral4(l, f);
896       break;
897     case MachO::S_8BYTE_LITERALS: {
898       double d;
899       memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
900       uint32_t l0, l1;
901       memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
902       memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
903              sizeof(uint32_t));
904       if (O->isLittleEndian() != sys::IsLittleEndianHost) {
905         sys::swapByteOrder(f);
906         sys::swapByteOrder(l0);
907         sys::swapByteOrder(l1);
908       }
909       DumpLiteral8(O, l0, l1, d);
910       break;
911     }
912     case MachO::S_16BYTE_LITERALS: {
913       uint32_t l0, l1, l2, l3;
914       memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
915       memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
916              sizeof(uint32_t));
917       memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
918              sizeof(uint32_t));
919       memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
920              sizeof(uint32_t));
921       if (O->isLittleEndian() != sys::IsLittleEndianHost) {
922         sys::swapByteOrder(l0);
923         sys::swapByteOrder(l1);
924         sys::swapByteOrder(l2);
925         sys::swapByteOrder(l3);
926       }
927       DumpLiteral16(l0, l1, l2, l3);
928       break;
929     }
930     }
931   }
932 }
933
934 static void DumpInitTermPointerSection(MachOObjectFile *O, const char *sect,
935                                        uint32_t sect_size, uint64_t sect_addr,
936                                        SymbolAddressMap *AddrMap,
937                                        bool verbose) {
938   uint32_t stride;
939   stride = (O->is64Bit()) ? sizeof(uint64_t) : sizeof(uint32_t);
940   for (uint32_t i = 0; i < sect_size; i += stride) {
941     const char *SymbolName = nullptr;
942     if (O->is64Bit()) {
943       outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
944       uint64_t pointer_value;
945       memcpy(&pointer_value, sect + i, stride);
946       if (O->isLittleEndian() != sys::IsLittleEndianHost)
947         sys::swapByteOrder(pointer_value);
948       outs() << format("0x%016" PRIx64, pointer_value);
949       if (verbose)
950         SymbolName = GuessSymbolName(pointer_value, AddrMap);
951     } else {
952       outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
953       uint32_t pointer_value;
954       memcpy(&pointer_value, sect + i, stride);
955       if (O->isLittleEndian() != sys::IsLittleEndianHost)
956         sys::swapByteOrder(pointer_value);
957       outs() << format("0x%08" PRIx32, pointer_value);
958       if (verbose)
959         SymbolName = GuessSymbolName(pointer_value, AddrMap);
960     }
961     if (SymbolName)
962       outs() << " " << SymbolName;
963     outs() << "\n";
964   }
965 }
966
967 static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
968                                    uint32_t size, uint64_t addr) {
969   uint32_t cputype = O->getHeader().cputype;
970   if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
971     uint32_t j;
972     for (uint32_t i = 0; i < size; i += j, addr += j) {
973       if (O->is64Bit())
974         outs() << format("%016" PRIx64, addr) << "\t";
975       else
976         outs() << format("%08" PRIx64, addr) << "\t";
977       for (j = 0; j < 16 && i + j < size; j++) {
978         uint8_t byte_word = *(sect + i + j);
979         outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
980       }
981       outs() << "\n";
982     }
983   } else {
984     uint32_t j;
985     for (uint32_t i = 0; i < size; i += j, addr += j) {
986       if (O->is64Bit())
987         outs() << format("%016" PRIx64, addr) << "\t";
988       else
989         outs() << format("%08" PRIx64, addr) << "\t";
990       for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
991            j += sizeof(int32_t)) {
992         if (i + j + sizeof(int32_t) <= size) {
993           uint32_t long_word;
994           memcpy(&long_word, sect + i + j, sizeof(int32_t));
995           if (O->isLittleEndian() != sys::IsLittleEndianHost)
996             sys::swapByteOrder(long_word);
997           outs() << format("%08" PRIx32, long_word) << " ";
998         } else {
999           for (uint32_t k = 0; i + j + k < size; k++) {
1000             uint8_t byte_word = *(sect + i + j + k);
1001             outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
1002           }
1003         }
1004       }
1005       outs() << "\n";
1006     }
1007   }
1008 }
1009
1010 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
1011                              StringRef DisSegName, StringRef DisSectName);
1012 static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
1013                                 uint32_t size, uint32_t addr);
1014 #ifdef HAVE_LIBXAR
1015 static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
1016                                 uint32_t size, bool verbose,
1017                                 bool PrintXarHeader, bool PrintXarFileHeaders,
1018                                 std::string XarMemberName);
1019 #endif // defined(HAVE_LIBXAR)
1020
1021 static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
1022                                 bool verbose) {
1023   SymbolAddressMap AddrMap;
1024   if (verbose)
1025     CreateSymbolAddressMap(O, &AddrMap);
1026
1027   for (unsigned i = 0; i < FilterSections.size(); ++i) {
1028     StringRef DumpSection = FilterSections[i];
1029     std::pair<StringRef, StringRef> DumpSegSectName;
1030     DumpSegSectName = DumpSection.split(',');
1031     StringRef DumpSegName, DumpSectName;
1032     if (DumpSegSectName.second.size()) {
1033       DumpSegName = DumpSegSectName.first;
1034       DumpSectName = DumpSegSectName.second;
1035     } else {
1036       DumpSegName = "";
1037       DumpSectName = DumpSegSectName.first;
1038     }
1039     for (const SectionRef &Section : O->sections()) {
1040       StringRef SectName;
1041       Section.getName(SectName);
1042       DataRefImpl Ref = Section.getRawDataRefImpl();
1043       StringRef SegName = O->getSectionFinalSegmentName(Ref);
1044       if ((DumpSegName.empty() || SegName == DumpSegName) &&
1045           (SectName == DumpSectName)) {
1046
1047         uint32_t section_flags;
1048         if (O->is64Bit()) {
1049           const MachO::section_64 Sec = O->getSection64(Ref);
1050           section_flags = Sec.flags;
1051
1052         } else {
1053           const MachO::section Sec = O->getSection(Ref);
1054           section_flags = Sec.flags;
1055         }
1056         uint32_t section_type = section_flags & MachO::SECTION_TYPE;
1057
1058         StringRef BytesStr;
1059         Section.getContents(BytesStr);
1060         const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1061         uint32_t sect_size = BytesStr.size();
1062         uint64_t sect_addr = Section.getAddress();
1063
1064         outs() << "Contents of (" << SegName << "," << SectName
1065                << ") section\n";
1066
1067         if (verbose) {
1068           if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1069               (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1070             DisassembleMachO(Filename, O, SegName, SectName);
1071             continue;
1072           }
1073           if (SegName == "__TEXT" && SectName == "__info_plist") {
1074             outs() << sect;
1075             continue;
1076           }
1077           if (SegName == "__OBJC" && SectName == "__protocol") {
1078             DumpProtocolSection(O, sect, sect_size, sect_addr);
1079             continue;
1080           }
1081 #ifdef HAVE_LIBXAR
1082           if (SegName == "__LLVM" && SectName == "__bundle") {
1083             DumpBitcodeSection(O, sect, sect_size, verbose, !NoSymbolicOperands,
1084                                ArchiveHeaders, "");
1085             continue;
1086           }
1087 #endif // defined(HAVE_LIBXAR)
1088           switch (section_type) {
1089           case MachO::S_REGULAR:
1090             DumpRawSectionContents(O, sect, sect_size, sect_addr);
1091             break;
1092           case MachO::S_ZEROFILL:
1093             outs() << "zerofill section and has no contents in the file\n";
1094             break;
1095           case MachO::S_CSTRING_LITERALS:
1096             DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1097             break;
1098           case MachO::S_4BYTE_LITERALS:
1099             DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1100             break;
1101           case MachO::S_8BYTE_LITERALS:
1102             DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1103             break;
1104           case MachO::S_16BYTE_LITERALS:
1105             DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1106             break;
1107           case MachO::S_LITERAL_POINTERS:
1108             DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
1109                                       !NoLeadingAddr);
1110             break;
1111           case MachO::S_MOD_INIT_FUNC_POINTERS:
1112           case MachO::S_MOD_TERM_FUNC_POINTERS:
1113             DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap,
1114                                        verbose);
1115             break;
1116           default:
1117             outs() << "Unknown section type ("
1118                    << format("0x%08" PRIx32, section_type) << ")\n";
1119             DumpRawSectionContents(O, sect, sect_size, sect_addr);
1120             break;
1121           }
1122         } else {
1123           if (section_type == MachO::S_ZEROFILL)
1124             outs() << "zerofill section and has no contents in the file\n";
1125           else
1126             DumpRawSectionContents(O, sect, sect_size, sect_addr);
1127         }
1128       }
1129     }
1130   }
1131 }
1132
1133 static void DumpInfoPlistSectionContents(StringRef Filename,
1134                                          MachOObjectFile *O) {
1135   for (const SectionRef &Section : O->sections()) {
1136     StringRef SectName;
1137     Section.getName(SectName);
1138     DataRefImpl Ref = Section.getRawDataRefImpl();
1139     StringRef SegName = O->getSectionFinalSegmentName(Ref);
1140     if (SegName == "__TEXT" && SectName == "__info_plist") {
1141       outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
1142       StringRef BytesStr;
1143       Section.getContents(BytesStr);
1144       const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1145       outs() << sect;
1146       return;
1147     }
1148   }
1149 }
1150
1151 // checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1152 // and if it is and there is a list of architecture flags is specified then
1153 // check to make sure this Mach-O file is one of those architectures or all
1154 // architectures were specified.  If not then an error is generated and this
1155 // routine returns false.  Else it returns true.
1156 static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
1157   auto *MachO = dyn_cast<MachOObjectFile>(O);
1158
1159   if (!MachO || ArchAll || ArchFlags.empty())
1160     return true;
1161
1162   MachO::mach_header H;
1163   MachO::mach_header_64 H_64;
1164   Triple T;
1165   const char *McpuDefault, *ArchFlag;
1166   if (MachO->is64Bit()) {
1167     H_64 = MachO->MachOObjectFile::getHeader64();
1168     T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype,
1169                                        &McpuDefault, &ArchFlag);
1170   } else {
1171     H = MachO->MachOObjectFile::getHeader();
1172     T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype,
1173                                        &McpuDefault, &ArchFlag);
1174   }
1175   const std::string ArchFlagName(ArchFlag);
1176   if (none_of(ArchFlags, [&](const std::string &Name) {
1177         return Name == ArchFlagName;
1178       })) {
1179     errs() << "llvm-objdump: " + Filename + ": No architecture specified.\n";
1180     return false;
1181   }
1182   return true;
1183 }
1184
1185 static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1186
1187 // ProcessMachO() is passed a single opened Mach-O file, which may be an
1188 // archive member and or in a slice of a universal file.  It prints the
1189 // the file name and header info and then processes it according to the
1190 // command line options.
1191 static void ProcessMachO(StringRef Name, MachOObjectFile *MachOOF,
1192                          StringRef ArchiveMemberName = StringRef(),
1193                          StringRef ArchitectureName = StringRef()) {
1194   // If we are doing some processing here on the Mach-O file print the header
1195   // info.  And don't print it otherwise like in the case of printing the
1196   // UniversalHeaders or ArchiveHeaders.
1197   if (Disassemble || PrivateHeaders || ExportsTrie || Rebase || Bind || SymbolTable ||
1198       LazyBind || WeakBind || IndirectSymbols || DataInCode || LinkOptHints ||
1199       DylibsUsed || DylibId || ObjcMetaData || (FilterSections.size() != 0)) {
1200     if (!NoLeadingHeaders) {
1201       outs() << Name;
1202       if (!ArchiveMemberName.empty())
1203         outs() << '(' << ArchiveMemberName << ')';
1204       if (!ArchitectureName.empty())
1205         outs() << " (architecture " << ArchitectureName << ")";
1206       outs() << ":\n";
1207     }
1208   }
1209   // To use the report_error() form with an ArchiveName and FileName set
1210   // these up based on what is passed for Name and ArchiveMemberName.
1211   StringRef ArchiveName;
1212   StringRef FileName;
1213   if (!ArchiveMemberName.empty()) {
1214     ArchiveName = Name;
1215     FileName = ArchiveMemberName;
1216   } else {
1217     ArchiveName = StringRef();
1218     FileName = Name;
1219   }
1220
1221   // If we need the symbol table to do the operation then check it here to
1222   // produce a good error message as to where the Mach-O file comes from in
1223   // the error message.
1224   if (Disassemble || IndirectSymbols || FilterSections.size() != 0 ||
1225       UnwindInfo)
1226     if (Error Err = MachOOF->checkSymbolTable())
1227       report_error(ArchiveName, FileName, std::move(Err), ArchitectureName);
1228
1229   if (Disassemble)
1230     DisassembleMachO(FileName, MachOOF, "__TEXT", "__text");
1231   if (IndirectSymbols)
1232     PrintIndirectSymbols(MachOOF, !NonVerbose);
1233   if (DataInCode)
1234     PrintDataInCodeTable(MachOOF, !NonVerbose);
1235   if (LinkOptHints)
1236     PrintLinkOptHints(MachOOF);
1237   if (Relocations)
1238     PrintRelocations(MachOOF);
1239   if (SectionHeaders)
1240     PrintSectionHeaders(MachOOF);
1241   if (SectionContents)
1242     PrintSectionContents(MachOOF);
1243   if (FilterSections.size() != 0)
1244     DumpSectionContents(FileName, MachOOF, !NonVerbose);
1245   if (InfoPlist)
1246     DumpInfoPlistSectionContents(FileName, MachOOF);
1247   if (DylibsUsed)
1248     PrintDylibs(MachOOF, false);
1249   if (DylibId)
1250     PrintDylibs(MachOOF, true);
1251   if (SymbolTable)
1252     PrintSymbolTable(MachOOF, ArchiveName, ArchitectureName);
1253   if (UnwindInfo)
1254     printMachOUnwindInfo(MachOOF);
1255   if (PrivateHeaders) {
1256     printMachOFileHeader(MachOOF);
1257     printMachOLoadCommands(MachOOF);
1258   }
1259   if (FirstPrivateHeader)
1260     printMachOFileHeader(MachOOF);
1261   if (ObjcMetaData)
1262     printObjcMetaData(MachOOF, !NonVerbose);
1263   if (ExportsTrie)
1264     printExportsTrie(MachOOF);
1265   if (Rebase)
1266     printRebaseTable(MachOOF);
1267   if (Bind)
1268     printBindTable(MachOOF);
1269   if (LazyBind)
1270     printLazyBindTable(MachOOF);
1271   if (WeakBind)
1272     printWeakBindTable(MachOOF);
1273
1274   if (DwarfDumpType != DIDT_Null) {
1275     std::unique_ptr<DIContext> DICtx(new DWARFContextInMemory(*MachOOF));
1276     // Dump the complete DWARF structure.
1277     DICtx->dump(outs(), DwarfDumpType, true /* DumpEH */);
1278   }
1279 }
1280
1281 // printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1282 static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
1283   outs() << "    cputype (" << cputype << ")\n";
1284   outs() << "    cpusubtype (" << cpusubtype << ")\n";
1285 }
1286
1287 // printCPUType() helps print_fat_headers by printing the cputype and
1288 // pusubtype (symbolically for the one's it knows about).
1289 static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
1290   switch (cputype) {
1291   case MachO::CPU_TYPE_I386:
1292     switch (cpusubtype) {
1293     case MachO::CPU_SUBTYPE_I386_ALL:
1294       outs() << "    cputype CPU_TYPE_I386\n";
1295       outs() << "    cpusubtype CPU_SUBTYPE_I386_ALL\n";
1296       break;
1297     default:
1298       printUnknownCPUType(cputype, cpusubtype);
1299       break;
1300     }
1301     break;
1302   case MachO::CPU_TYPE_X86_64:
1303     switch (cpusubtype) {
1304     case MachO::CPU_SUBTYPE_X86_64_ALL:
1305       outs() << "    cputype CPU_TYPE_X86_64\n";
1306       outs() << "    cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1307       break;
1308     case MachO::CPU_SUBTYPE_X86_64_H:
1309       outs() << "    cputype CPU_TYPE_X86_64\n";
1310       outs() << "    cpusubtype CPU_SUBTYPE_X86_64_H\n";
1311       break;
1312     default:
1313       printUnknownCPUType(cputype, cpusubtype);
1314       break;
1315     }
1316     break;
1317   case MachO::CPU_TYPE_ARM:
1318     switch (cpusubtype) {
1319     case MachO::CPU_SUBTYPE_ARM_ALL:
1320       outs() << "    cputype CPU_TYPE_ARM\n";
1321       outs() << "    cpusubtype CPU_SUBTYPE_ARM_ALL\n";
1322       break;
1323     case MachO::CPU_SUBTYPE_ARM_V4T:
1324       outs() << "    cputype CPU_TYPE_ARM\n";
1325       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V4T\n";
1326       break;
1327     case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1328       outs() << "    cputype CPU_TYPE_ARM\n";
1329       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
1330       break;
1331     case MachO::CPU_SUBTYPE_ARM_XSCALE:
1332       outs() << "    cputype CPU_TYPE_ARM\n";
1333       outs() << "    cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
1334       break;
1335     case MachO::CPU_SUBTYPE_ARM_V6:
1336       outs() << "    cputype CPU_TYPE_ARM\n";
1337       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V6\n";
1338       break;
1339     case MachO::CPU_SUBTYPE_ARM_V6M:
1340       outs() << "    cputype CPU_TYPE_ARM\n";
1341       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V6M\n";
1342       break;
1343     case MachO::CPU_SUBTYPE_ARM_V7:
1344       outs() << "    cputype CPU_TYPE_ARM\n";
1345       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V7\n";
1346       break;
1347     case MachO::CPU_SUBTYPE_ARM_V7EM:
1348       outs() << "    cputype CPU_TYPE_ARM\n";
1349       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
1350       break;
1351     case MachO::CPU_SUBTYPE_ARM_V7K:
1352       outs() << "    cputype CPU_TYPE_ARM\n";
1353       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V7K\n";
1354       break;
1355     case MachO::CPU_SUBTYPE_ARM_V7M:
1356       outs() << "    cputype CPU_TYPE_ARM\n";
1357       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V7M\n";
1358       break;
1359     case MachO::CPU_SUBTYPE_ARM_V7S:
1360       outs() << "    cputype CPU_TYPE_ARM\n";
1361       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V7S\n";
1362       break;
1363     default:
1364       printUnknownCPUType(cputype, cpusubtype);
1365       break;
1366     }
1367     break;
1368   case MachO::CPU_TYPE_ARM64:
1369     switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1370     case MachO::CPU_SUBTYPE_ARM64_ALL:
1371       outs() << "    cputype CPU_TYPE_ARM64\n";
1372       outs() << "    cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
1373       break;
1374     default:
1375       printUnknownCPUType(cputype, cpusubtype);
1376       break;
1377     }
1378     break;
1379   default:
1380     printUnknownCPUType(cputype, cpusubtype);
1381     break;
1382   }
1383 }
1384
1385 static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
1386                                        bool verbose) {
1387   outs() << "Fat headers\n";
1388   if (verbose) {
1389     if (UB->getMagic() == MachO::FAT_MAGIC)
1390       outs() << "fat_magic FAT_MAGIC\n";
1391     else // UB->getMagic() == MachO::FAT_MAGIC_64
1392       outs() << "fat_magic FAT_MAGIC_64\n";
1393   } else
1394     outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
1395
1396   uint32_t nfat_arch = UB->getNumberOfObjects();
1397   StringRef Buf = UB->getData();
1398   uint64_t size = Buf.size();
1399   uint64_t big_size = sizeof(struct MachO::fat_header) +
1400                       nfat_arch * sizeof(struct MachO::fat_arch);
1401   outs() << "nfat_arch " << UB->getNumberOfObjects();
1402   if (nfat_arch == 0)
1403     outs() << " (malformed, contains zero architecture types)\n";
1404   else if (big_size > size)
1405     outs() << " (malformed, architectures past end of file)\n";
1406   else
1407     outs() << "\n";
1408
1409   for (uint32_t i = 0; i < nfat_arch; ++i) {
1410     MachOUniversalBinary::ObjectForArch OFA(UB, i);
1411     uint32_t cputype = OFA.getCPUType();
1412     uint32_t cpusubtype = OFA.getCPUSubType();
1413     outs() << "architecture ";
1414     for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
1415       MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
1416       uint32_t other_cputype = other_OFA.getCPUType();
1417       uint32_t other_cpusubtype = other_OFA.getCPUSubType();
1418       if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
1419           (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
1420               (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
1421         outs() << "(illegal duplicate architecture) ";
1422         break;
1423       }
1424     }
1425     if (verbose) {
1426       outs() << OFA.getArchFlagName() << "\n";
1427       printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1428     } else {
1429       outs() << i << "\n";
1430       outs() << "    cputype " << cputype << "\n";
1431       outs() << "    cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
1432              << "\n";
1433     }
1434     if (verbose &&
1435         (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
1436       outs() << "    capabilities CPU_SUBTYPE_LIB64\n";
1437     else
1438       outs() << "    capabilities "
1439              << format("0x%" PRIx32,
1440                        (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
1441     outs() << "    offset " << OFA.getOffset();
1442     if (OFA.getOffset() > size)
1443       outs() << " (past end of file)";
1444     if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
1445       outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
1446     outs() << "\n";
1447     outs() << "    size " << OFA.getSize();
1448     big_size = OFA.getOffset() + OFA.getSize();
1449     if (big_size > size)
1450       outs() << " (past end of file)";
1451     outs() << "\n";
1452     outs() << "    align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
1453            << ")\n";
1454   }
1455 }
1456
1457 static void printArchiveChild(StringRef Filename, const Archive::Child &C,
1458                               bool verbose, bool print_offset,
1459                               StringRef ArchitectureName = StringRef()) {
1460   if (print_offset)
1461     outs() << C.getChildOffset() << "\t";
1462   Expected<sys::fs::perms> ModeOrErr = C.getAccessMode();
1463   if (!ModeOrErr)
1464     report_error(Filename, C, ModeOrErr.takeError(), ArchitectureName);
1465   sys::fs::perms Mode = ModeOrErr.get();
1466   if (verbose) {
1467     // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
1468     // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
1469     outs() << "-";
1470     outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
1471     outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
1472     outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
1473     outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
1474     outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
1475     outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
1476     outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
1477     outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
1478     outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
1479   } else {
1480     outs() << format("0%o ", Mode);
1481   }
1482
1483   Expected<unsigned> UIDOrErr = C.getUID();
1484   if (!UIDOrErr)
1485     report_error(Filename, C, UIDOrErr.takeError(), ArchitectureName);
1486   unsigned UID = UIDOrErr.get();
1487   outs() << format("%3d/", UID);
1488   Expected<unsigned> GIDOrErr = C.getGID();
1489   if (!GIDOrErr)
1490     report_error(Filename, C, GIDOrErr.takeError(), ArchitectureName);
1491   unsigned GID = GIDOrErr.get();
1492   outs() << format("%-3d ", GID);
1493   Expected<uint64_t> Size = C.getRawSize();
1494   if (!Size)
1495     report_error(Filename, C, Size.takeError(), ArchitectureName);
1496   outs() << format("%5" PRId64, Size.get()) << " ";
1497
1498   StringRef RawLastModified = C.getRawLastModified();
1499   if (verbose) {
1500     unsigned Seconds;
1501     if (RawLastModified.getAsInteger(10, Seconds))
1502       outs() << "(date: \"" << RawLastModified
1503              << "\" contains non-decimal chars) ";
1504     else {
1505       // Since cime(3) returns a 26 character string of the form:
1506       // "Sun Sep 16 01:03:52 1973\n\0"
1507       // just print 24 characters.
1508       time_t t = Seconds;
1509       outs() << format("%.24s ", ctime(&t));
1510     }
1511   } else {
1512     outs() << RawLastModified << " ";
1513   }
1514
1515   if (verbose) {
1516     Expected<StringRef> NameOrErr = C.getName();
1517     if (!NameOrErr) {
1518       consumeError(NameOrErr.takeError());
1519       Expected<StringRef> NameOrErr = C.getRawName();
1520       if (!NameOrErr)
1521         report_error(Filename, C, NameOrErr.takeError(), ArchitectureName);
1522       StringRef RawName = NameOrErr.get();
1523       outs() << RawName << "\n";
1524     } else {
1525       StringRef Name = NameOrErr.get();
1526       outs() << Name << "\n";
1527     }
1528   } else {
1529     Expected<StringRef> NameOrErr = C.getRawName();
1530     if (!NameOrErr)
1531       report_error(Filename, C, NameOrErr.takeError(), ArchitectureName);
1532     StringRef RawName = NameOrErr.get();
1533     outs() << RawName << "\n";
1534   }
1535 }
1536
1537 static void printArchiveHeaders(StringRef Filename, Archive *A, bool verbose,
1538                                 bool print_offset,
1539                                 StringRef ArchitectureName = StringRef()) {
1540   Error Err = Error::success();
1541   ;
1542   for (const auto &C : A->children(Err, false))
1543     printArchiveChild(Filename, C, verbose, print_offset, ArchitectureName);
1544
1545   if (Err)
1546     report_error(StringRef(), Filename, std::move(Err), ArchitectureName);
1547 }
1548
1549 // ParseInputMachO() parses the named Mach-O file in Filename and handles the
1550 // -arch flags selecting just those slices as specified by them and also parses
1551 // archive files.  Then for each individual Mach-O file ProcessMachO() is
1552 // called to process the file based on the command line options.
1553 void llvm::ParseInputMachO(StringRef Filename) {
1554   // Check for -arch all and verifiy the -arch flags are valid.
1555   for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1556     if (ArchFlags[i] == "all") {
1557       ArchAll = true;
1558     } else {
1559       if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
1560         errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
1561                       "'for the -arch option\n";
1562         return;
1563       }
1564     }
1565   }
1566
1567   // Attempt to open the binary.
1568   Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
1569   if (!BinaryOrErr)
1570     report_error(Filename, BinaryOrErr.takeError());
1571   Binary &Bin = *BinaryOrErr.get().getBinary();
1572
1573   if (Archive *A = dyn_cast<Archive>(&Bin)) {
1574     outs() << "Archive : " << Filename << "\n";
1575     if (ArchiveHeaders)
1576       printArchiveHeaders(Filename, A, !NonVerbose, ArchiveMemberOffsets);
1577
1578     Error Err = Error::success();
1579     for (auto &C : A->children(Err)) {
1580       Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1581       if (!ChildOrErr) {
1582         if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1583           report_error(Filename, C, std::move(E));
1584         continue;
1585       }
1586       if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1587         if (!checkMachOAndArchFlags(O, Filename))
1588           return;
1589         ProcessMachO(Filename, O, O->getFileName());
1590       }
1591     }
1592     if (Err)
1593       report_error(Filename, std::move(Err));
1594     return;
1595   }
1596   if (UniversalHeaders) {
1597     if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
1598       printMachOUniversalHeaders(UB, !NonVerbose);
1599   }
1600   if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1601     // If we have a list of architecture flags specified dump only those.
1602     if (!ArchAll && ArchFlags.size() != 0) {
1603       // Look for a slice in the universal binary that matches each ArchFlag.
1604       bool ArchFound;
1605       for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1606         ArchFound = false;
1607         for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1608                                                    E = UB->end_objects();
1609              I != E; ++I) {
1610           if (ArchFlags[i] == I->getArchFlagName()) {
1611             ArchFound = true;
1612             Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
1613                 I->getAsObjectFile();
1614             std::string ArchitectureName = "";
1615             if (ArchFlags.size() > 1)
1616               ArchitectureName = I->getArchFlagName();
1617             if (ObjOrErr) {
1618               ObjectFile &O = *ObjOrErr.get();
1619               if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
1620                 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
1621             } else if (auto E = isNotObjectErrorInvalidFileType(
1622                        ObjOrErr.takeError())) {
1623               report_error(Filename, StringRef(), std::move(E),
1624                            ArchitectureName);
1625               continue;
1626             } else if (Expected<std::unique_ptr<Archive>> AOrErr =
1627                            I->getAsArchive()) {
1628               std::unique_ptr<Archive> &A = *AOrErr;
1629               outs() << "Archive : " << Filename;
1630               if (!ArchitectureName.empty())
1631                 outs() << " (architecture " << ArchitectureName << ")";
1632               outs() << "\n";
1633               if (ArchiveHeaders)
1634                 printArchiveHeaders(Filename, A.get(), !NonVerbose,
1635                                     ArchiveMemberOffsets, ArchitectureName);
1636               Error Err = Error::success();
1637               for (auto &C : A->children(Err)) {
1638                 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1639                 if (!ChildOrErr) {
1640                   if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1641                     report_error(Filename, C, std::move(E), ArchitectureName);
1642                   continue;
1643                 }
1644                 if (MachOObjectFile *O =
1645                         dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
1646                   ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
1647               }
1648               if (Err)
1649                 report_error(Filename, std::move(Err));
1650             } else {
1651               consumeError(AOrErr.takeError());
1652               error("Mach-O universal file: " + Filename + " for " +
1653                     "architecture " + StringRef(I->getArchFlagName()) +
1654                     " is not a Mach-O file or an archive file");
1655             }
1656           }
1657         }
1658         if (!ArchFound) {
1659           errs() << "llvm-objdump: file: " + Filename + " does not contain "
1660                  << "architecture: " + ArchFlags[i] + "\n";
1661           return;
1662         }
1663       }
1664       return;
1665     }
1666     // No architecture flags were specified so if this contains a slice that
1667     // matches the host architecture dump only that.
1668     if (!ArchAll) {
1669       for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1670                                                  E = UB->end_objects();
1671            I != E; ++I) {
1672         if (MachOObjectFile::getHostArch().getArchName() ==
1673             I->getArchFlagName()) {
1674           Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1675           std::string ArchiveName;
1676           ArchiveName.clear();
1677           if (ObjOrErr) {
1678             ObjectFile &O = *ObjOrErr.get();
1679             if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
1680               ProcessMachO(Filename, MachOOF);
1681           } else if (auto E = isNotObjectErrorInvalidFileType(
1682                      ObjOrErr.takeError())) {
1683             report_error(Filename, std::move(E));
1684             continue;
1685           } else if (Expected<std::unique_ptr<Archive>> AOrErr =
1686                          I->getAsArchive()) {
1687             std::unique_ptr<Archive> &A = *AOrErr;
1688             outs() << "Archive : " << Filename << "\n";
1689             if (ArchiveHeaders)
1690               printArchiveHeaders(Filename, A.get(), !NonVerbose,
1691                                   ArchiveMemberOffsets);
1692             Error Err = Error::success();
1693             for (auto &C : A->children(Err)) {
1694               Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1695               if (!ChildOrErr) {
1696                 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1697                   report_error(Filename, C, std::move(E));
1698                 continue;
1699               }
1700               if (MachOObjectFile *O =
1701                       dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
1702                 ProcessMachO(Filename, O, O->getFileName());
1703             }
1704             if (Err)
1705               report_error(Filename, std::move(Err));
1706           } else {
1707             consumeError(AOrErr.takeError());
1708             error("Mach-O universal file: " + Filename + " for architecture " +
1709                   StringRef(I->getArchFlagName()) +
1710                   " is not a Mach-O file or an archive file");
1711           }
1712           return;
1713         }
1714       }
1715     }
1716     // Either all architectures have been specified or none have been specified
1717     // and this does not contain the host architecture so dump all the slices.
1718     bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1719     for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1720                                                E = UB->end_objects();
1721          I != E; ++I) {
1722       Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1723       std::string ArchitectureName = "";
1724       if (moreThanOneArch)
1725         ArchitectureName = I->getArchFlagName();
1726       if (ObjOrErr) {
1727         ObjectFile &Obj = *ObjOrErr.get();
1728         if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
1729           ProcessMachO(Filename, MachOOF, "", ArchitectureName);
1730       } else if (auto E = isNotObjectErrorInvalidFileType(
1731                  ObjOrErr.takeError())) {
1732         report_error(StringRef(), Filename, std::move(E), ArchitectureName);
1733         continue;
1734       } else if (Expected<std::unique_ptr<Archive>> AOrErr =
1735                    I->getAsArchive()) {
1736         std::unique_ptr<Archive> &A = *AOrErr;
1737         outs() << "Archive : " << Filename;
1738         if (!ArchitectureName.empty())
1739           outs() << " (architecture " << ArchitectureName << ")";
1740         outs() << "\n";
1741         if (ArchiveHeaders)
1742           printArchiveHeaders(Filename, A.get(), !NonVerbose,
1743                               ArchiveMemberOffsets, ArchitectureName);
1744         Error Err = Error::success();
1745         for (auto &C : A->children(Err)) {
1746           Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1747           if (!ChildOrErr) {
1748             if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1749               report_error(Filename, C, std::move(E), ArchitectureName);
1750             continue;
1751           }
1752           if (MachOObjectFile *O =
1753                   dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1754             if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
1755               ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
1756                            ArchitectureName);
1757           }
1758         }
1759         if (Err)
1760           report_error(Filename, std::move(Err));
1761       } else {
1762         consumeError(AOrErr.takeError());
1763         error("Mach-O universal file: " + Filename + " for architecture " +
1764               StringRef(I->getArchFlagName()) +
1765               " is not a Mach-O file or an archive file");
1766       }
1767     }
1768     return;
1769   }
1770   if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
1771     if (!checkMachOAndArchFlags(O, Filename))
1772       return;
1773     if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
1774       ProcessMachO(Filename, MachOOF);
1775     } else
1776       errs() << "llvm-objdump: '" << Filename << "': "
1777              << "Object is not a Mach-O file type.\n";
1778     return;
1779   }
1780   llvm_unreachable("Input object can't be invalid at this point");
1781 }
1782
1783 typedef std::pair<uint64_t, const char *> BindInfoEntry;
1784 typedef std::vector<BindInfoEntry> BindTable;
1785 typedef BindTable::iterator bind_table_iterator;
1786
1787 // The block of info used by the Symbolizer call backs.
1788 struct DisassembleInfo {
1789   bool verbose;
1790   MachOObjectFile *O;
1791   SectionRef S;
1792   SymbolAddressMap *AddrMap;
1793   std::vector<SectionRef> *Sections;
1794   const char *class_name;
1795   const char *selector_name;
1796   char *method;
1797   char *demangled_name;
1798   uint64_t adrp_addr;
1799   uint32_t adrp_inst;
1800   BindTable *bindtable;
1801   uint32_t depth;
1802 };
1803
1804 // SymbolizerGetOpInfo() is the operand information call back function.
1805 // This is called to get the symbolic information for operand(s) of an
1806 // instruction when it is being done.  This routine does this from
1807 // the relocation information, symbol table, etc. That block of information
1808 // is a pointer to the struct DisassembleInfo that was passed when the
1809 // disassembler context was created and passed to back to here when
1810 // called back by the disassembler for instruction operands that could have
1811 // relocation information. The address of the instruction containing operand is
1812 // at the Pc parameter.  The immediate value the operand has is passed in
1813 // op_info->Value and is at Offset past the start of the instruction and has a
1814 // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
1815 // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
1816 // names and addends of the symbolic expression to add for the operand.  The
1817 // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
1818 // information is returned then this function returns 1 else it returns 0.
1819 static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
1820                                uint64_t Size, int TagType, void *TagBuf) {
1821   struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
1822   struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
1823   uint64_t value = op_info->Value;
1824
1825   // Make sure all fields returned are zero if we don't set them.
1826   memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
1827   op_info->Value = value;
1828
1829   // If the TagType is not the value 1 which it code knows about or if no
1830   // verbose symbolic information is wanted then just return 0, indicating no
1831   // information is being returned.
1832   if (TagType != 1 || !info->verbose)
1833     return 0;
1834
1835   unsigned int Arch = info->O->getArch();
1836   if (Arch == Triple::x86) {
1837     if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1838       return 0;
1839     if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
1840       // TODO:
1841       // Search the external relocation entries of a fully linked image
1842       // (if any) for an entry that matches this segment offset.
1843       // uint32_t seg_offset = (Pc + Offset);
1844       return 0;
1845     }
1846     // In MH_OBJECT filetypes search the section's relocation entries (if any)
1847     // for an entry for this section offset.
1848     uint32_t sect_addr = info->S.getAddress();
1849     uint32_t sect_offset = (Pc + Offset) - sect_addr;
1850     bool reloc_found = false;
1851     DataRefImpl Rel;
1852     MachO::any_relocation_info RE;
1853     bool isExtern = false;
1854     SymbolRef Symbol;
1855     bool r_scattered = false;
1856     uint32_t r_value, pair_r_value, r_type;
1857     for (const RelocationRef &Reloc : info->S.relocations()) {
1858       uint64_t RelocOffset = Reloc.getOffset();
1859       if (RelocOffset == sect_offset) {
1860         Rel = Reloc.getRawDataRefImpl();
1861         RE = info->O->getRelocation(Rel);
1862         r_type = info->O->getAnyRelocationType(RE);
1863         r_scattered = info->O->isRelocationScattered(RE);
1864         if (r_scattered) {
1865           r_value = info->O->getScatteredRelocationValue(RE);
1866           if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1867               r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
1868             DataRefImpl RelNext = Rel;
1869             info->O->moveRelocationNext(RelNext);
1870             MachO::any_relocation_info RENext;
1871             RENext = info->O->getRelocation(RelNext);
1872             if (info->O->isRelocationScattered(RENext))
1873               pair_r_value = info->O->getScatteredRelocationValue(RENext);
1874             else
1875               return 0;
1876           }
1877         } else {
1878           isExtern = info->O->getPlainRelocationExternal(RE);
1879           if (isExtern) {
1880             symbol_iterator RelocSym = Reloc.getSymbol();
1881             Symbol = *RelocSym;
1882           }
1883         }
1884         reloc_found = true;
1885         break;
1886       }
1887     }
1888     if (reloc_found && isExtern) {
1889       Expected<StringRef> SymName = Symbol.getName();
1890       if (!SymName)
1891         report_error(info->O->getFileName(), SymName.takeError());
1892       const char *name = SymName->data();
1893       op_info->AddSymbol.Present = 1;
1894       op_info->AddSymbol.Name = name;
1895       // For i386 extern relocation entries the value in the instruction is
1896       // the offset from the symbol, and value is already set in op_info->Value.
1897       return 1;
1898     }
1899     if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1900                         r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
1901       const char *add = GuessSymbolName(r_value, info->AddrMap);
1902       const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
1903       uint32_t offset = value - (r_value - pair_r_value);
1904       op_info->AddSymbol.Present = 1;
1905       if (add != nullptr)
1906         op_info->AddSymbol.Name = add;
1907       else
1908         op_info->AddSymbol.Value = r_value;
1909       op_info->SubtractSymbol.Present = 1;
1910       if (sub != nullptr)
1911         op_info->SubtractSymbol.Name = sub;
1912       else
1913         op_info->SubtractSymbol.Value = pair_r_value;
1914       op_info->Value = offset;
1915       return 1;
1916     }
1917     return 0;
1918   }
1919   if (Arch == Triple::x86_64) {
1920     if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1921       return 0;
1922     if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
1923       // TODO:
1924       // Search the external relocation entries of a fully linked image
1925       // (if any) for an entry that matches this segment offset.
1926       // uint64_t seg_offset = (Pc + Offset);
1927       return 0;
1928     }
1929     // In MH_OBJECT filetypes search the section's relocation entries (if any)
1930     // for an entry for this section offset.
1931     uint64_t sect_addr = info->S.getAddress();
1932     uint64_t sect_offset = (Pc + Offset) - sect_addr;
1933     bool reloc_found = false;
1934     DataRefImpl Rel;
1935     MachO::any_relocation_info RE;
1936     bool isExtern = false;
1937     SymbolRef Symbol;
1938     for (const RelocationRef &Reloc : info->S.relocations()) {
1939       uint64_t RelocOffset = Reloc.getOffset();
1940       if (RelocOffset == sect_offset) {
1941         Rel = Reloc.getRawDataRefImpl();
1942         RE = info->O->getRelocation(Rel);
1943         // NOTE: Scattered relocations don't exist on x86_64.
1944         isExtern = info->O->getPlainRelocationExternal(RE);
1945         if (isExtern) {
1946           symbol_iterator RelocSym = Reloc.getSymbol();
1947           Symbol = *RelocSym;
1948         }
1949         reloc_found = true;
1950         break;
1951       }
1952     }
1953     if (reloc_found && isExtern) {
1954       // The Value passed in will be adjusted by the Pc if the instruction
1955       // adds the Pc.  But for x86_64 external relocation entries the Value
1956       // is the offset from the external symbol.
1957       if (info->O->getAnyRelocationPCRel(RE))
1958         op_info->Value -= Pc + Offset + Size;
1959       Expected<StringRef> SymName = Symbol.getName();
1960       if (!SymName)
1961         report_error(info->O->getFileName(), SymName.takeError());
1962       const char *name = SymName->data();
1963       unsigned Type = info->O->getAnyRelocationType(RE);
1964       if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
1965         DataRefImpl RelNext = Rel;
1966         info->O->moveRelocationNext(RelNext);
1967         MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
1968         unsigned TypeNext = info->O->getAnyRelocationType(RENext);
1969         bool isExternNext = info->O->getPlainRelocationExternal(RENext);
1970         unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
1971         if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
1972           op_info->SubtractSymbol.Present = 1;
1973           op_info->SubtractSymbol.Name = name;
1974           symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
1975           Symbol = *RelocSymNext;
1976           Expected<StringRef> SymNameNext = Symbol.getName();
1977           if (!SymNameNext)
1978             report_error(info->O->getFileName(), SymNameNext.takeError());
1979           name = SymNameNext->data();
1980         }
1981       }
1982       // TODO: add the VariantKinds to op_info->VariantKind for relocation types
1983       // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
1984       op_info->AddSymbol.Present = 1;
1985       op_info->AddSymbol.Name = name;
1986       return 1;
1987     }
1988     return 0;
1989   }
1990   if (Arch == Triple::arm) {
1991     if (Offset != 0 || (Size != 4 && Size != 2))
1992       return 0;
1993     if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
1994       // TODO:
1995       // Search the external relocation entries of a fully linked image
1996       // (if any) for an entry that matches this segment offset.
1997       // uint32_t seg_offset = (Pc + Offset);
1998       return 0;
1999     }
2000     // In MH_OBJECT filetypes search the section's relocation entries (if any)
2001     // for an entry for this section offset.
2002     uint32_t sect_addr = info->S.getAddress();
2003     uint32_t sect_offset = (Pc + Offset) - sect_addr;
2004     DataRefImpl Rel;
2005     MachO::any_relocation_info RE;
2006     bool isExtern = false;
2007     SymbolRef Symbol;
2008     bool r_scattered = false;
2009     uint32_t r_value, pair_r_value, r_type, r_length, other_half;
2010     auto Reloc =
2011         find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
2012           uint64_t RelocOffset = Reloc.getOffset();
2013           return RelocOffset == sect_offset;
2014         });
2015
2016     if (Reloc == info->S.relocations().end())
2017       return 0;
2018
2019     Rel = Reloc->getRawDataRefImpl();
2020     RE = info->O->getRelocation(Rel);
2021     r_length = info->O->getAnyRelocationLength(RE);
2022     r_scattered = info->O->isRelocationScattered(RE);
2023     if (r_scattered) {
2024       r_value = info->O->getScatteredRelocationValue(RE);
2025       r_type = info->O->getScatteredRelocationType(RE);
2026     } else {
2027       r_type = info->O->getAnyRelocationType(RE);
2028       isExtern = info->O->getPlainRelocationExternal(RE);
2029       if (isExtern) {
2030         symbol_iterator RelocSym = Reloc->getSymbol();
2031         Symbol = *RelocSym;
2032       }
2033     }
2034     if (r_type == MachO::ARM_RELOC_HALF ||
2035         r_type == MachO::ARM_RELOC_SECTDIFF ||
2036         r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
2037         r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2038       DataRefImpl RelNext = Rel;
2039       info->O->moveRelocationNext(RelNext);
2040       MachO::any_relocation_info RENext;
2041       RENext = info->O->getRelocation(RelNext);
2042       other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
2043       if (info->O->isRelocationScattered(RENext))
2044         pair_r_value = info->O->getScatteredRelocationValue(RENext);
2045     }
2046
2047     if (isExtern) {
2048       Expected<StringRef> SymName = Symbol.getName();
2049       if (!SymName)
2050         report_error(info->O->getFileName(), SymName.takeError());
2051       const char *name = SymName->data();
2052       op_info->AddSymbol.Present = 1;
2053       op_info->AddSymbol.Name = name;
2054       switch (r_type) {
2055       case MachO::ARM_RELOC_HALF:
2056         if ((r_length & 0x1) == 1) {
2057           op_info->Value = value << 16 | other_half;
2058           op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2059         } else {
2060           op_info->Value = other_half << 16 | value;
2061           op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2062         }
2063         break;
2064       default:
2065         break;
2066       }
2067       return 1;
2068     }
2069     // If we have a branch that is not an external relocation entry then
2070     // return 0 so the code in tryAddingSymbolicOperand() can use the
2071     // SymbolLookUp call back with the branch target address to look up the
2072     // symbol and possibility add an annotation for a symbol stub.
2073     if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
2074                           r_type == MachO::ARM_THUMB_RELOC_BR22))
2075       return 0;
2076
2077     uint32_t offset = 0;
2078     if (r_type == MachO::ARM_RELOC_HALF ||
2079         r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2080       if ((r_length & 0x1) == 1)
2081         value = value << 16 | other_half;
2082       else
2083         value = other_half << 16 | value;
2084     }
2085     if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
2086                         r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
2087       offset = value - r_value;
2088       value = r_value;
2089     }
2090
2091     if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2092       if ((r_length & 0x1) == 1)
2093         op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2094       else
2095         op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2096       const char *add = GuessSymbolName(r_value, info->AddrMap);
2097       const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
2098       int32_t offset = value - (r_value - pair_r_value);
2099       op_info->AddSymbol.Present = 1;
2100       if (add != nullptr)
2101         op_info->AddSymbol.Name = add;
2102       else
2103         op_info->AddSymbol.Value = r_value;
2104       op_info->SubtractSymbol.Present = 1;
2105       if (sub != nullptr)
2106         op_info->SubtractSymbol.Name = sub;
2107       else
2108         op_info->SubtractSymbol.Value = pair_r_value;
2109       op_info->Value = offset;
2110       return 1;
2111     }
2112
2113     op_info->AddSymbol.Present = 1;
2114     op_info->Value = offset;
2115     if (r_type == MachO::ARM_RELOC_HALF) {
2116       if ((r_length & 0x1) == 1)
2117         op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2118       else
2119         op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2120     }
2121     const char *add = GuessSymbolName(value, info->AddrMap);
2122     if (add != nullptr) {
2123       op_info->AddSymbol.Name = add;
2124       return 1;
2125     }
2126     op_info->AddSymbol.Value = value;
2127     return 1;
2128   }
2129   if (Arch == Triple::aarch64) {
2130     if (Offset != 0 || Size != 4)
2131       return 0;
2132     if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2133       // TODO:
2134       // Search the external relocation entries of a fully linked image
2135       // (if any) for an entry that matches this segment offset.
2136       // uint64_t seg_offset = (Pc + Offset);
2137       return 0;
2138     }
2139     // In MH_OBJECT filetypes search the section's relocation entries (if any)
2140     // for an entry for this section offset.
2141     uint64_t sect_addr = info->S.getAddress();
2142     uint64_t sect_offset = (Pc + Offset) - sect_addr;
2143     auto Reloc =
2144         find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
2145           uint64_t RelocOffset = Reloc.getOffset();
2146           return RelocOffset == sect_offset;
2147         });
2148
2149     if (Reloc == info->S.relocations().end())
2150       return 0;
2151
2152     DataRefImpl Rel = Reloc->getRawDataRefImpl();
2153     MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2154     uint32_t r_type = info->O->getAnyRelocationType(RE);
2155     if (r_type == MachO::ARM64_RELOC_ADDEND) {
2156       DataRefImpl RelNext = Rel;
2157       info->O->moveRelocationNext(RelNext);
2158       MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2159       if (value == 0) {
2160         value = info->O->getPlainRelocationSymbolNum(RENext);
2161         op_info->Value = value;
2162       }
2163     }
2164     // NOTE: Scattered relocations don't exist on arm64.
2165     if (!info->O->getPlainRelocationExternal(RE))
2166       return 0;
2167     Expected<StringRef> SymName = Reloc->getSymbol()->getName();
2168     if (!SymName)
2169       report_error(info->O->getFileName(), SymName.takeError());
2170     const char *name = SymName->data();
2171     op_info->AddSymbol.Present = 1;
2172     op_info->AddSymbol.Name = name;
2173
2174     switch (r_type) {
2175     case MachO::ARM64_RELOC_PAGE21:
2176       /* @page */
2177       op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2178       break;
2179     case MachO::ARM64_RELOC_PAGEOFF12:
2180       /* @pageoff */
2181       op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2182       break;
2183     case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2184       /* @gotpage */
2185       op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2186       break;
2187     case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2188       /* @gotpageoff */
2189       op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2190       break;
2191     case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2192       /* @tvlppage is not implemented in llvm-mc */
2193       op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2194       break;
2195     case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2196       /* @tvlppageoff is not implemented in llvm-mc */
2197       op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2198       break;
2199     default:
2200     case MachO::ARM64_RELOC_BRANCH26:
2201       op_info->VariantKind = LLVMDisassembler_VariantKind_None;
2202       break;
2203     }
2204     return 1;
2205   }
2206   return 0;
2207 }
2208
2209 // GuessCstringPointer is passed the address of what might be a pointer to a
2210 // literal string in a cstring section.  If that address is in a cstring section
2211 // it returns a pointer to that string.  Else it returns nullptr.
2212 static const char *GuessCstringPointer(uint64_t ReferenceValue,
2213                                        struct DisassembleInfo *info) {
2214   for (const auto &Load : info->O->load_commands()) {
2215     if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2216       MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2217       for (unsigned J = 0; J < Seg.nsects; ++J) {
2218         MachO::section_64 Sec = info->O->getSection64(Load, J);
2219         uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2220         if (section_type == MachO::S_CSTRING_LITERALS &&
2221             ReferenceValue >= Sec.addr &&
2222             ReferenceValue < Sec.addr + Sec.size) {
2223           uint64_t sect_offset = ReferenceValue - Sec.addr;
2224           uint64_t object_offset = Sec.offset + sect_offset;
2225           StringRef MachOContents = info->O->getData();
2226           uint64_t object_size = MachOContents.size();
2227           const char *object_addr = (const char *)MachOContents.data();
2228           if (object_offset < object_size) {
2229             const char *name = object_addr + object_offset;
2230             return name;
2231           } else {
2232             return nullptr;
2233           }
2234         }
2235       }
2236     } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2237       MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2238       for (unsigned J = 0; J < Seg.nsects; ++J) {
2239         MachO::section Sec = info->O->getSection(Load, J);
2240         uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2241         if (section_type == MachO::S_CSTRING_LITERALS &&
2242             ReferenceValue >= Sec.addr &&
2243             ReferenceValue < Sec.addr + Sec.size) {
2244           uint64_t sect_offset = ReferenceValue - Sec.addr;
2245           uint64_t object_offset = Sec.offset + sect_offset;
2246           StringRef MachOContents = info->O->getData();
2247           uint64_t object_size = MachOContents.size();
2248           const char *object_addr = (const char *)MachOContents.data();
2249           if (object_offset < object_size) {
2250             const char *name = object_addr + object_offset;
2251             return name;
2252           } else {
2253             return nullptr;
2254           }
2255         }
2256       }
2257     }
2258   }
2259   return nullptr;
2260 }
2261
2262 // GuessIndirectSymbol returns the name of the indirect symbol for the
2263 // ReferenceValue passed in or nullptr.  This is used when ReferenceValue maybe
2264 // an address of a symbol stub or a lazy or non-lazy pointer to associate the
2265 // symbol name being referenced by the stub or pointer.
2266 static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2267                                        struct DisassembleInfo *info) {
2268   MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2269   MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
2270   for (const auto &Load : info->O->load_commands()) {
2271     if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2272       MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2273       for (unsigned J = 0; J < Seg.nsects; ++J) {
2274         MachO::section_64 Sec = info->O->getSection64(Load, J);
2275         uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2276         if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2277              section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2278              section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2279              section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2280              section_type == MachO::S_SYMBOL_STUBS) &&
2281             ReferenceValue >= Sec.addr &&
2282             ReferenceValue < Sec.addr + Sec.size) {
2283           uint32_t stride;
2284           if (section_type == MachO::S_SYMBOL_STUBS)
2285             stride = Sec.reserved2;
2286           else
2287             stride = 8;
2288           if (stride == 0)
2289             return nullptr;
2290           uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2291           if (index < Dysymtab.nindirectsyms) {
2292             uint32_t indirect_symbol =
2293                 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
2294             if (indirect_symbol < Symtab.nsyms) {
2295               symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2296               SymbolRef Symbol = *Sym;
2297               Expected<StringRef> SymName = Symbol.getName();
2298               if (!SymName)
2299                 report_error(info->O->getFileName(), SymName.takeError());
2300               const char *name = SymName->data();
2301               return name;
2302             }
2303           }
2304         }
2305       }
2306     } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2307       MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2308       for (unsigned J = 0; J < Seg.nsects; ++J) {
2309         MachO::section Sec = info->O->getSection(Load, J);
2310         uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2311         if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2312              section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2313              section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2314              section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2315              section_type == MachO::S_SYMBOL_STUBS) &&
2316             ReferenceValue >= Sec.addr &&
2317             ReferenceValue < Sec.addr + Sec.size) {
2318           uint32_t stride;
2319           if (section_type == MachO::S_SYMBOL_STUBS)
2320             stride = Sec.reserved2;
2321           else
2322             stride = 4;
2323           if (stride == 0)
2324             return nullptr;
2325           uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2326           if (index < Dysymtab.nindirectsyms) {
2327             uint32_t indirect_symbol =
2328                 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
2329             if (indirect_symbol < Symtab.nsyms) {
2330               symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2331               SymbolRef Symbol = *Sym;
2332               Expected<StringRef> SymName = Symbol.getName();
2333               if (!SymName)
2334                 report_error(info->O->getFileName(), SymName.takeError());
2335               const char *name = SymName->data();
2336               return name;
2337             }
2338           }
2339         }
2340       }
2341     }
2342   }
2343   return nullptr;
2344 }
2345
2346 // method_reference() is called passing it the ReferenceName that might be
2347 // a reference it to an Objective-C method call.  If so then it allocates and
2348 // assembles a method call string with the values last seen and saved in
2349 // the DisassembleInfo's class_name and selector_name fields.  This is saved
2350 // into the method field of the info and any previous string is free'ed.
2351 // Then the class_name field in the info is set to nullptr.  The method call
2352 // string is set into ReferenceName and ReferenceType is set to
2353 // LLVMDisassembler_ReferenceType_Out_Objc_Message.  If this not a method call
2354 // then both ReferenceType and ReferenceName are left unchanged.
2355 static void method_reference(struct DisassembleInfo *info,
2356                              uint64_t *ReferenceType,
2357                              const char **ReferenceName) {
2358   unsigned int Arch = info->O->getArch();
2359   if (*ReferenceName != nullptr) {
2360     if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
2361       if (info->selector_name != nullptr) {
2362         if (info->method != nullptr)
2363           free(info->method);
2364         if (info->class_name != nullptr) {
2365           info->method = (char *)malloc(5 + strlen(info->class_name) +
2366                                         strlen(info->selector_name));
2367           if (info->method != nullptr) {
2368             strcpy(info->method, "+[");
2369             strcat(info->method, info->class_name);
2370             strcat(info->method, " ");
2371             strcat(info->method, info->selector_name);
2372             strcat(info->method, "]");
2373             *ReferenceName = info->method;
2374             *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2375           }
2376         } else {
2377           info->method = (char *)malloc(9 + strlen(info->selector_name));
2378           if (info->method != nullptr) {
2379             if (Arch == Triple::x86_64)
2380               strcpy(info->method, "-[%rdi ");
2381             else if (Arch == Triple::aarch64)
2382               strcpy(info->method, "-[x0 ");
2383             else
2384               strcpy(info->method, "-[r? ");
2385             strcat(info->method, info->selector_name);
2386             strcat(info->method, "]");
2387             *ReferenceName = info->method;
2388             *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2389           }
2390         }
2391         info->class_name = nullptr;
2392       }
2393     } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
2394       if (info->selector_name != nullptr) {
2395         if (info->method != nullptr)
2396           free(info->method);
2397         info->method = (char *)malloc(17 + strlen(info->selector_name));
2398         if (info->method != nullptr) {
2399           if (Arch == Triple::x86_64)
2400             strcpy(info->method, "-[[%rdi super] ");
2401           else if (Arch == Triple::aarch64)
2402             strcpy(info->method, "-[[x0 super] ");
2403           else
2404             strcpy(info->method, "-[[r? super] ");
2405           strcat(info->method, info->selector_name);
2406           strcat(info->method, "]");
2407           *ReferenceName = info->method;
2408           *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2409         }
2410         info->class_name = nullptr;
2411       }
2412     }
2413   }
2414 }
2415
2416 // GuessPointerPointer() is passed the address of what might be a pointer to
2417 // a reference to an Objective-C class, selector, message ref or cfstring.
2418 // If so the value of the pointer is returned and one of the booleans are set
2419 // to true.  If not zero is returned and all the booleans are set to false.
2420 static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
2421                                     struct DisassembleInfo *info,
2422                                     bool &classref, bool &selref, bool &msgref,
2423                                     bool &cfstring) {
2424   classref = false;
2425   selref = false;
2426   msgref = false;
2427   cfstring = false;
2428   for (const auto &Load : info->O->load_commands()) {
2429     if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2430       MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2431       for (unsigned J = 0; J < Seg.nsects; ++J) {
2432         MachO::section_64 Sec = info->O->getSection64(Load, J);
2433         if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
2434              strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2435              strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
2436              strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
2437              strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
2438             ReferenceValue >= Sec.addr &&
2439             ReferenceValue < Sec.addr + Sec.size) {
2440           uint64_t sect_offset = ReferenceValue - Sec.addr;
2441           uint64_t object_offset = Sec.offset + sect_offset;
2442           StringRef MachOContents = info->O->getData();
2443           uint64_t object_size = MachOContents.size();
2444           const char *object_addr = (const char *)MachOContents.data();
2445           if (object_offset < object_size) {
2446             uint64_t pointer_value;
2447             memcpy(&pointer_value, object_addr + object_offset,
2448                    sizeof(uint64_t));
2449             if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2450               sys::swapByteOrder(pointer_value);
2451             if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
2452               selref = true;
2453             else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2454                      strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
2455               classref = true;
2456             else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
2457                      ReferenceValue + 8 < Sec.addr + Sec.size) {
2458               msgref = true;
2459               memcpy(&pointer_value, object_addr + object_offset + 8,
2460                      sizeof(uint64_t));
2461               if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2462                 sys::swapByteOrder(pointer_value);
2463             } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
2464               cfstring = true;
2465             return pointer_value;
2466           } else {
2467             return 0;
2468           }
2469         }
2470       }
2471     }
2472     // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
2473   }
2474   return 0;
2475 }
2476
2477 // get_pointer_64 returns a pointer to the bytes in the object file at the
2478 // Address from a section in the Mach-O file.  And indirectly returns the
2479 // offset into the section, number of bytes left in the section past the offset
2480 // and which section is was being referenced.  If the Address is not in a
2481 // section nullptr is returned.
2482 static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
2483                                   uint32_t &left, SectionRef &S,
2484                                   DisassembleInfo *info,
2485                                   bool objc_only = false) {
2486   offset = 0;
2487   left = 0;
2488   S = SectionRef();
2489   for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
2490     uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
2491     uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
2492     if (SectSize == 0)
2493       continue;
2494     if (objc_only) {
2495       StringRef SectName;
2496       ((*(info->Sections))[SectIdx]).getName(SectName);
2497       DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
2498       StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
2499       if (SegName != "__OBJC" && SectName != "__cstring")
2500         continue;
2501     }
2502     if (Address >= SectAddress && Address < SectAddress + SectSize) {
2503       S = (*(info->Sections))[SectIdx];
2504       offset = Address - SectAddress;
2505       left = SectSize - offset;
2506       StringRef SectContents;
2507       ((*(info->Sections))[SectIdx]).getContents(SectContents);
2508       return SectContents.data() + offset;
2509     }
2510   }
2511   return nullptr;
2512 }
2513
2514 static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
2515                                   uint32_t &left, SectionRef &S,
2516                                   DisassembleInfo *info,
2517                                   bool objc_only = false) {
2518   return get_pointer_64(Address, offset, left, S, info, objc_only);
2519 }
2520
2521 // get_symbol_64() returns the name of a symbol (or nullptr) and the address of
2522 // the symbol indirectly through n_value. Based on the relocation information
2523 // for the specified section offset in the specified section reference.
2524 // If no relocation information is found and a non-zero ReferenceValue for the
2525 // symbol is passed, look up that address in the info's AddrMap.
2526 static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
2527                                  DisassembleInfo *info, uint64_t &n_value,
2528                                  uint64_t ReferenceValue = 0) {
2529   n_value = 0;
2530   if (!info->verbose)
2531     return nullptr;
2532
2533   // See if there is an external relocation entry at the sect_offset.
2534   bool reloc_found = false;
2535   DataRefImpl Rel;
2536   MachO::any_relocation_info RE;
2537   bool isExtern = false;
2538   SymbolRef Symbol;
2539   for (const RelocationRef &Reloc : S.relocations()) {
2540     uint64_t RelocOffset = Reloc.getOffset();
2541     if (RelocOffset == sect_offset) {
2542       Rel = Reloc.getRawDataRefImpl();
2543       RE = info->O->getRelocation(Rel);
2544       if (info->O->isRelocationScattered(RE))
2545         continue;
2546       isExtern = info->O->getPlainRelocationExternal(RE);
2547       if (isExtern) {
2548         symbol_iterator RelocSym = Reloc.getSymbol();
2549         Symbol = *RelocSym;
2550       }
2551       reloc_found = true;
2552       break;
2553     }
2554   }
2555   // If there is an external relocation entry for a symbol in this section
2556   // at this section_offset then use that symbol's value for the n_value
2557   // and return its name.
2558   const char *SymbolName = nullptr;
2559   if (reloc_found && isExtern) {
2560     n_value = Symbol.getValue();
2561     Expected<StringRef> NameOrError = Symbol.getName();
2562     if (!NameOrError)
2563       report_error(info->O->getFileName(), NameOrError.takeError());
2564     StringRef Name = *NameOrError;
2565     if (!Name.empty()) {
2566       SymbolName = Name.data();
2567       return SymbolName;
2568     }
2569   }
2570
2571   // TODO: For fully linked images, look through the external relocation
2572   // entries off the dynamic symtab command. For these the r_offset is from the
2573   // start of the first writeable segment in the Mach-O file.  So the offset
2574   // to this section from that segment is passed to this routine by the caller,
2575   // as the database_offset. Which is the difference of the section's starting
2576   // address and the first writable segment.
2577   //
2578   // NOTE: need add passing the database_offset to this routine.
2579
2580   // We did not find an external relocation entry so look up the ReferenceValue
2581   // as an address of a symbol and if found return that symbol's name.
2582   SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
2583
2584   return SymbolName;
2585 }
2586
2587 static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
2588                                  DisassembleInfo *info,
2589                                  uint32_t ReferenceValue) {
2590   uint64_t n_value64;
2591   return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
2592 }
2593
2594 // These are structs in the Objective-C meta data and read to produce the
2595 // comments for disassembly.  While these are part of the ABI they are no
2596 // public defintions.  So the are here not in include/llvm/Support/MachO.h .
2597
2598 // The cfstring object in a 64-bit Mach-O file.
2599 struct cfstring64_t {
2600   uint64_t isa;        // class64_t * (64-bit pointer)
2601   uint64_t flags;      // flag bits
2602   uint64_t characters; // char * (64-bit pointer)
2603   uint64_t length;     // number of non-NULL characters in above
2604 };
2605
2606 // The class object in a 64-bit Mach-O file.
2607 struct class64_t {
2608   uint64_t isa;        // class64_t * (64-bit pointer)
2609   uint64_t superclass; // class64_t * (64-bit pointer)
2610   uint64_t cache;      // Cache (64-bit pointer)
2611   uint64_t vtable;     // IMP * (64-bit pointer)
2612   uint64_t data;       // class_ro64_t * (64-bit pointer)
2613 };
2614
2615 struct class32_t {
2616   uint32_t isa;        /* class32_t * (32-bit pointer) */
2617   uint32_t superclass; /* class32_t * (32-bit pointer) */
2618   uint32_t cache;      /* Cache (32-bit pointer) */
2619   uint32_t vtable;     /* IMP * (32-bit pointer) */
2620   uint32_t data;       /* class_ro32_t * (32-bit pointer) */
2621 };
2622
2623 struct class_ro64_t {
2624   uint32_t flags;
2625   uint32_t instanceStart;
2626   uint32_t instanceSize;
2627   uint32_t reserved;
2628   uint64_t ivarLayout;     // const uint8_t * (64-bit pointer)
2629   uint64_t name;           // const char * (64-bit pointer)
2630   uint64_t baseMethods;    // const method_list_t * (64-bit pointer)
2631   uint64_t baseProtocols;  // const protocol_list_t * (64-bit pointer)
2632   uint64_t ivars;          // const ivar_list_t * (64-bit pointer)
2633   uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
2634   uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
2635 };
2636
2637 struct class_ro32_t {
2638   uint32_t flags;
2639   uint32_t instanceStart;
2640   uint32_t instanceSize;
2641   uint32_t ivarLayout;     /* const uint8_t * (32-bit pointer) */
2642   uint32_t name;           /* const char * (32-bit pointer) */
2643   uint32_t baseMethods;    /* const method_list_t * (32-bit pointer) */
2644   uint32_t baseProtocols;  /* const protocol_list_t * (32-bit pointer) */
2645   uint32_t ivars;          /* const ivar_list_t * (32-bit pointer) */
2646   uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
2647   uint32_t baseProperties; /* const struct objc_property_list *
2648                                                    (32-bit pointer) */
2649 };
2650
2651 /* Values for class_ro{64,32}_t->flags */
2652 #define RO_META (1 << 0)
2653 #define RO_ROOT (1 << 1)
2654 #define RO_HAS_CXX_STRUCTORS (1 << 2)
2655
2656 struct method_list64_t {
2657   uint32_t entsize;
2658   uint32_t count;
2659   /* struct method64_t first;  These structures follow inline */
2660 };
2661
2662 struct method_list32_t {
2663   uint32_t entsize;
2664   uint32_t count;
2665   /* struct method32_t first;  These structures follow inline */
2666 };
2667
2668 struct method64_t {
2669   uint64_t name;  /* SEL (64-bit pointer) */
2670   uint64_t types; /* const char * (64-bit pointer) */
2671   uint64_t imp;   /* IMP (64-bit pointer) */
2672 };
2673
2674 struct method32_t {
2675   uint32_t name;  /* SEL (32-bit pointer) */
2676   uint32_t types; /* const char * (32-bit pointer) */
2677   uint32_t imp;   /* IMP (32-bit pointer) */
2678 };
2679
2680 struct protocol_list64_t {
2681   uint64_t count; /* uintptr_t (a 64-bit value) */
2682   /* struct protocol64_t * list[0];  These pointers follow inline */
2683 };
2684
2685 struct protocol_list32_t {
2686   uint32_t count; /* uintptr_t (a 32-bit value) */
2687   /* struct protocol32_t * list[0];  These pointers follow inline */
2688 };
2689
2690 struct protocol64_t {
2691   uint64_t isa;                     /* id * (64-bit pointer) */
2692   uint64_t name;                    /* const char * (64-bit pointer) */
2693   uint64_t protocols;               /* struct protocol_list64_t *
2694                                                     (64-bit pointer) */
2695   uint64_t instanceMethods;         /* method_list_t * (64-bit pointer) */
2696   uint64_t classMethods;            /* method_list_t * (64-bit pointer) */
2697   uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
2698   uint64_t optionalClassMethods;    /* method_list_t * (64-bit pointer) */
2699   uint64_t instanceProperties;      /* struct objc_property_list *
2700                                                        (64-bit pointer) */
2701 };
2702
2703 struct protocol32_t {
2704   uint32_t isa;                     /* id * (32-bit pointer) */
2705   uint32_t name;                    /* const char * (32-bit pointer) */
2706   uint32_t protocols;               /* struct protocol_list_t *
2707                                                     (32-bit pointer) */
2708   uint32_t instanceMethods;         /* method_list_t * (32-bit pointer) */
2709   uint32_t classMethods;            /* method_list_t * (32-bit pointer) */
2710   uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
2711   uint32_t optionalClassMethods;    /* method_list_t * (32-bit pointer) */
2712   uint32_t instanceProperties;      /* struct objc_property_list *
2713                                                        (32-bit pointer) */
2714 };
2715
2716 struct ivar_list64_t {
2717   uint32_t entsize;
2718   uint32_t count;
2719   /* struct ivar64_t first;  These structures follow inline */
2720 };
2721
2722 struct ivar_list32_t {
2723   uint32_t entsize;
2724   uint32_t count;
2725   /* struct ivar32_t first;  These structures follow inline */
2726 };
2727
2728 struct ivar64_t {
2729   uint64_t offset; /* uintptr_t * (64-bit pointer) */
2730   uint64_t name;   /* const char * (64-bit pointer) */
2731   uint64_t type;   /* const char * (64-bit pointer) */
2732   uint32_t alignment;
2733   uint32_t size;
2734 };
2735
2736 struct ivar32_t {
2737   uint32_t offset; /* uintptr_t * (32-bit pointer) */
2738   uint32_t name;   /* const char * (32-bit pointer) */
2739   uint32_t type;   /* const char * (32-bit pointer) */
2740   uint32_t alignment;
2741   uint32_t size;
2742 };
2743
2744 struct objc_property_list64 {
2745   uint32_t entsize;
2746   uint32_t count;
2747   /* struct objc_property64 first;  These structures follow inline */
2748 };
2749
2750 struct objc_property_list32 {
2751   uint32_t entsize;
2752   uint32_t count;
2753   /* struct objc_property32 first;  These structures follow inline */
2754 };
2755
2756 struct objc_property64 {
2757   uint64_t name;       /* const char * (64-bit pointer) */
2758   uint64_t attributes; /* const char * (64-bit pointer) */
2759 };
2760
2761 struct objc_property32 {
2762   uint32_t name;       /* const char * (32-bit pointer) */
2763   uint32_t attributes; /* const char * (32-bit pointer) */
2764 };
2765
2766 struct category64_t {
2767   uint64_t name;               /* const char * (64-bit pointer) */
2768   uint64_t cls;                /* struct class_t * (64-bit pointer) */
2769   uint64_t instanceMethods;    /* struct method_list_t * (64-bit pointer) */
2770   uint64_t classMethods;       /* struct method_list_t * (64-bit pointer) */
2771   uint64_t protocols;          /* struct protocol_list_t * (64-bit pointer) */
2772   uint64_t instanceProperties; /* struct objc_property_list *
2773                                   (64-bit pointer) */
2774 };
2775
2776 struct category32_t {
2777   uint32_t name;               /* const char * (32-bit pointer) */
2778   uint32_t cls;                /* struct class_t * (32-bit pointer) */
2779   uint32_t instanceMethods;    /* struct method_list_t * (32-bit pointer) */
2780   uint32_t classMethods;       /* struct method_list_t * (32-bit pointer) */
2781   uint32_t protocols;          /* struct protocol_list_t * (32-bit pointer) */
2782   uint32_t instanceProperties; /* struct objc_property_list *
2783                                   (32-bit pointer) */
2784 };
2785
2786 struct objc_image_info64 {
2787   uint32_t version;
2788   uint32_t flags;
2789 };
2790 struct objc_image_info32 {
2791   uint32_t version;
2792   uint32_t flags;
2793 };
2794 struct imageInfo_t {
2795   uint32_t version;
2796   uint32_t flags;
2797 };
2798 /* masks for objc_image_info.flags */
2799 #define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
2800 #define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
2801
2802 struct message_ref64 {
2803   uint64_t imp; /* IMP (64-bit pointer) */
2804   uint64_t sel; /* SEL (64-bit pointer) */
2805 };
2806
2807 struct message_ref32 {
2808   uint32_t imp; /* IMP (32-bit pointer) */
2809   uint32_t sel; /* SEL (32-bit pointer) */
2810 };
2811
2812 // Objective-C 1 (32-bit only) meta data structs.
2813
2814 struct objc_module_t {
2815   uint32_t version;
2816   uint32_t size;
2817   uint32_t name;   /* char * (32-bit pointer) */
2818   uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
2819 };
2820
2821 struct objc_symtab_t {
2822   uint32_t sel_ref_cnt;
2823   uint32_t refs; /* SEL * (32-bit pointer) */
2824   uint16_t cls_def_cnt;
2825   uint16_t cat_def_cnt;
2826   // uint32_t defs[1];        /* void * (32-bit pointer) variable size */
2827 };
2828
2829 struct objc_class_t {
2830   uint32_t isa;         /* struct objc_class * (32-bit pointer) */
2831   uint32_t super_class; /* struct objc_class * (32-bit pointer) */
2832   uint32_t name;        /* const char * (32-bit pointer) */
2833   int32_t version;
2834   int32_t info;
2835   int32_t instance_size;
2836   uint32_t ivars;       /* struct objc_ivar_list * (32-bit pointer) */
2837   uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
2838   uint32_t cache;       /* struct objc_cache * (32-bit pointer) */
2839   uint32_t protocols;   /* struct objc_protocol_list * (32-bit pointer) */
2840 };
2841
2842 #define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
2843 // class is not a metaclass
2844 #define CLS_CLASS 0x1
2845 // class is a metaclass
2846 #define CLS_META 0x2
2847
2848 struct objc_category_t {
2849   uint32_t category_name;    /* char * (32-bit pointer) */
2850   uint32_t class_name;       /* char * (32-bit pointer) */
2851   uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
2852   uint32_t class_methods;    /* struct objc_method_list * (32-bit pointer) */
2853   uint32_t protocols;        /* struct objc_protocol_list * (32-bit ptr) */
2854 };
2855
2856 struct objc_ivar_t {
2857   uint32_t ivar_name; /* char * (32-bit pointer) */
2858   uint32_t ivar_type; /* char * (32-bit pointer) */
2859   int32_t ivar_offset;
2860 };
2861
2862 struct objc_ivar_list_t {
2863   int32_t ivar_count;
2864   // struct objc_ivar_t ivar_list[1];          /* variable length structure */
2865 };
2866
2867 struct objc_method_list_t {
2868   uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
2869   int32_t method_count;
2870   // struct objc_method_t method_list[1];      /* variable length structure */
2871 };
2872
2873 struct objc_method_t {
2874   uint32_t method_name;  /* SEL, aka struct objc_selector * (32-bit pointer) */
2875   uint32_t method_types; /* char * (32-bit pointer) */
2876   uint32_t method_imp;   /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
2877                             (32-bit pointer) */
2878 };
2879
2880 struct objc_protocol_list_t {
2881   uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
2882   int32_t count;
2883   // uint32_t list[1];   /* Protocol *, aka struct objc_protocol_t *
2884   //                        (32-bit pointer) */
2885 };
2886
2887 struct objc_protocol_t {
2888   uint32_t isa;              /* struct objc_class * (32-bit pointer) */
2889   uint32_t protocol_name;    /* char * (32-bit pointer) */
2890   uint32_t protocol_list;    /* struct objc_protocol_list * (32-bit pointer) */
2891   uint32_t instance_methods; /* struct objc_method_description_list *
2892                                 (32-bit pointer) */
2893   uint32_t class_methods;    /* struct objc_method_description_list *
2894                                 (32-bit pointer) */
2895 };
2896
2897 struct objc_method_description_list_t {
2898   int32_t count;
2899   // struct objc_method_description_t list[1];
2900 };
2901
2902 struct objc_method_description_t {
2903   uint32_t name;  /* SEL, aka struct objc_selector * (32-bit pointer) */
2904   uint32_t types; /* char * (32-bit pointer) */
2905 };
2906
2907 inline void swapStruct(struct cfstring64_t &cfs) {
2908   sys::swapByteOrder(cfs.isa);
2909   sys::swapByteOrder(cfs.flags);
2910   sys::swapByteOrder(cfs.characters);
2911   sys::swapByteOrder(cfs.length);
2912 }
2913
2914 inline void swapStruct(struct class64_t &c) {
2915   sys::swapByteOrder(c.isa);
2916   sys::swapByteOrder(c.superclass);
2917   sys::swapByteOrder(c.cache);
2918   sys::swapByteOrder(c.vtable);
2919   sys::swapByteOrder(c.data);
2920 }
2921
2922 inline void swapStruct(struct class32_t &c) {
2923   sys::swapByteOrder(c.isa);
2924   sys::swapByteOrder(c.superclass);
2925   sys::swapByteOrder(c.cache);
2926   sys::swapByteOrder(c.vtable);
2927   sys::swapByteOrder(c.data);
2928 }
2929
2930 inline void swapStruct(struct class_ro64_t &cro) {
2931   sys::swapByteOrder(cro.flags);
2932   sys::swapByteOrder(cro.instanceStart);
2933   sys::swapByteOrder(cro.instanceSize);
2934   sys::swapByteOrder(cro.reserved);
2935   sys::swapByteOrder(cro.ivarLayout);
2936   sys::swapByteOrder(cro.name);
2937   sys::swapByteOrder(cro.baseMethods);
2938   sys::swapByteOrder(cro.baseProtocols);
2939   sys::swapByteOrder(cro.ivars);
2940   sys::swapByteOrder(cro.weakIvarLayout);
2941   sys::swapByteOrder(cro.baseProperties);
2942 }
2943
2944 inline void swapStruct(struct class_ro32_t &cro) {
2945   sys::swapByteOrder(cro.flags);
2946   sys::swapByteOrder(cro.instanceStart);
2947   sys::swapByteOrder(cro.instanceSize);
2948   sys::swapByteOrder(cro.ivarLayout);
2949   sys::swapByteOrder(cro.name);
2950   sys::swapByteOrder(cro.baseMethods);
2951   sys::swapByteOrder(cro.baseProtocols);
2952   sys::swapByteOrder(cro.ivars);
2953   sys::swapByteOrder(cro.weakIvarLayout);
2954   sys::swapByteOrder(cro.baseProperties);
2955 }
2956
2957 inline void swapStruct(struct method_list64_t &ml) {
2958   sys::swapByteOrder(ml.entsize);
2959   sys::swapByteOrder(ml.count);
2960 }
2961
2962 inline void swapStruct(struct method_list32_t &ml) {
2963   sys::swapByteOrder(ml.entsize);
2964   sys::swapByteOrder(ml.count);
2965 }
2966
2967 inline void swapStruct(struct method64_t &m) {
2968   sys::swapByteOrder(m.name);
2969   sys::swapByteOrder(m.types);
2970   sys::swapByteOrder(m.imp);
2971 }
2972
2973 inline void swapStruct(struct method32_t &m) {
2974   sys::swapByteOrder(m.name);
2975   sys::swapByteOrder(m.types);
2976   sys::swapByteOrder(m.imp);
2977 }
2978
2979 inline void swapStruct(struct protocol_list64_t &pl) {
2980   sys::swapByteOrder(pl.count);
2981 }
2982
2983 inline void swapStruct(struct protocol_list32_t &pl) {
2984   sys::swapByteOrder(pl.count);
2985 }
2986
2987 inline void swapStruct(struct protocol64_t &p) {
2988   sys::swapByteOrder(p.isa);
2989   sys::swapByteOrder(p.name);
2990   sys::swapByteOrder(p.protocols);
2991   sys::swapByteOrder(p.instanceMethods);
2992   sys::swapByteOrder(p.classMethods);
2993   sys::swapByteOrder(p.optionalInstanceMethods);
2994   sys::swapByteOrder(p.optionalClassMethods);
2995   sys::swapByteOrder(p.instanceProperties);
2996 }
2997
2998 inline void swapStruct(struct protocol32_t &p) {
2999   sys::swapByteOrder(p.isa);
3000   sys::swapByteOrder(p.name);
3001   sys::swapByteOrder(p.protocols);
3002   sys::swapByteOrder(p.instanceMethods);
3003   sys::swapByteOrder(p.classMethods);
3004   sys::swapByteOrder(p.optionalInstanceMethods);
3005   sys::swapByteOrder(p.optionalClassMethods);
3006   sys::swapByteOrder(p.instanceProperties);
3007 }
3008
3009 inline void swapStruct(struct ivar_list64_t &il) {
3010   sys::swapByteOrder(il.entsize);
3011   sys::swapByteOrder(il.count);
3012 }
3013
3014 inline void swapStruct(struct ivar_list32_t &il) {
3015   sys::swapByteOrder(il.entsize);
3016   sys::swapByteOrder(il.count);
3017 }
3018
3019 inline void swapStruct(struct ivar64_t &i) {
3020   sys::swapByteOrder(i.offset);
3021   sys::swapByteOrder(i.name);
3022   sys::swapByteOrder(i.type);
3023   sys::swapByteOrder(i.alignment);
3024   sys::swapByteOrder(i.size);
3025 }
3026
3027 inline void swapStruct(struct ivar32_t &i) {
3028   sys::swapByteOrder(i.offset);
3029   sys::swapByteOrder(i.name);
3030   sys::swapByteOrder(i.type);
3031   sys::swapByteOrder(i.alignment);
3032   sys::swapByteOrder(i.size);
3033 }
3034
3035 inline void swapStruct(struct objc_property_list64 &pl) {
3036   sys::swapByteOrder(pl.entsize);
3037   sys::swapByteOrder(pl.count);
3038 }
3039
3040 inline void swapStruct(struct objc_property_list32 &pl) {
3041   sys::swapByteOrder(pl.entsize);
3042   sys::swapByteOrder(pl.count);
3043 }
3044
3045 inline void swapStruct(struct objc_property64 &op) {
3046   sys::swapByteOrder(op.name);
3047   sys::swapByteOrder(op.attributes);
3048 }
3049
3050 inline void swapStruct(struct objc_property32 &op) {
3051   sys::swapByteOrder(op.name);
3052   sys::swapByteOrder(op.attributes);
3053 }
3054
3055 inline void swapStruct(struct category64_t &c) {
3056   sys::swapByteOrder(c.name);
3057   sys::swapByteOrder(c.cls);
3058   sys::swapByteOrder(c.instanceMethods);
3059   sys::swapByteOrder(c.classMethods);
3060   sys::swapByteOrder(c.protocols);
3061   sys::swapByteOrder(c.instanceProperties);
3062 }
3063
3064 inline void swapStruct(struct category32_t &c) {
3065   sys::swapByteOrder(c.name);
3066   sys::swapByteOrder(c.cls);
3067   sys::swapByteOrder(c.instanceMethods);
3068   sys::swapByteOrder(c.classMethods);
3069   sys::swapByteOrder(c.protocols);
3070   sys::swapByteOrder(c.instanceProperties);
3071 }
3072
3073 inline void swapStruct(struct objc_image_info64 &o) {
3074   sys::swapByteOrder(o.version);
3075   sys::swapByteOrder(o.flags);
3076 }
3077
3078 inline void swapStruct(struct objc_image_info32 &o) {
3079   sys::swapByteOrder(o.version);
3080   sys::swapByteOrder(o.flags);
3081 }
3082
3083 inline void swapStruct(struct imageInfo_t &o) {
3084   sys::swapByteOrder(o.version);
3085   sys::swapByteOrder(o.flags);
3086 }
3087
3088 inline void swapStruct(struct message_ref64 &mr) {
3089   sys::swapByteOrder(mr.imp);
3090   sys::swapByteOrder(mr.sel);
3091 }
3092
3093 inline void swapStruct(struct message_ref32 &mr) {
3094   sys::swapByteOrder(mr.imp);
3095   sys::swapByteOrder(mr.sel);
3096 }
3097
3098 inline void swapStruct(struct objc_module_t &module) {
3099   sys::swapByteOrder(module.version);
3100   sys::swapByteOrder(module.size);
3101   sys::swapByteOrder(module.name);
3102   sys::swapByteOrder(module.symtab);
3103 }
3104
3105 inline void swapStruct(struct objc_symtab_t &symtab) {
3106   sys::swapByteOrder(symtab.sel_ref_cnt);
3107   sys::swapByteOrder(symtab.refs);
3108   sys::swapByteOrder(symtab.cls_def_cnt);
3109   sys::swapByteOrder(symtab.cat_def_cnt);
3110 }
3111
3112 inline void swapStruct(struct objc_class_t &objc_class) {
3113   sys::swapByteOrder(objc_class.isa);
3114   sys::swapByteOrder(objc_class.super_class);
3115   sys::swapByteOrder(objc_class.name);
3116   sys::swapByteOrder(objc_class.version);
3117   sys::swapByteOrder(objc_class.info);
3118   sys::swapByteOrder(objc_class.instance_size);
3119   sys::swapByteOrder(objc_class.ivars);
3120   sys::swapByteOrder(objc_class.methodLists);
3121   sys::swapByteOrder(objc_class.cache);
3122   sys::swapByteOrder(objc_class.protocols);
3123 }
3124
3125 inline void swapStruct(struct objc_category_t &objc_category) {
3126   sys::swapByteOrder(objc_category.category_name);
3127   sys::swapByteOrder(objc_category.class_name);
3128   sys::swapByteOrder(objc_category.instance_methods);
3129   sys::swapByteOrder(objc_category.class_methods);
3130   sys::swapByteOrder(objc_category.protocols);
3131 }
3132
3133 inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3134   sys::swapByteOrder(objc_ivar_list.ivar_count);
3135 }
3136
3137 inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3138   sys::swapByteOrder(objc_ivar.ivar_name);
3139   sys::swapByteOrder(objc_ivar.ivar_type);
3140   sys::swapByteOrder(objc_ivar.ivar_offset);
3141 }
3142
3143 inline void swapStruct(struct objc_method_list_t &method_list) {
3144   sys::swapByteOrder(method_list.obsolete);
3145   sys::swapByteOrder(method_list.method_count);
3146 }
3147
3148 inline void swapStruct(struct objc_method_t &method) {
3149   sys::swapByteOrder(method.method_name);
3150   sys::swapByteOrder(method.method_types);
3151   sys::swapByteOrder(method.method_imp);
3152 }
3153
3154 inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3155   sys::swapByteOrder(protocol_list.next);
3156   sys::swapByteOrder(protocol_list.count);
3157 }
3158
3159 inline void swapStruct(struct objc_protocol_t &protocol) {
3160   sys::swapByteOrder(protocol.isa);
3161   sys::swapByteOrder(protocol.protocol_name);
3162   sys::swapByteOrder(protocol.protocol_list);
3163   sys::swapByteOrder(protocol.instance_methods);
3164   sys::swapByteOrder(protocol.class_methods);
3165 }
3166
3167 inline void swapStruct(struct objc_method_description_list_t &mdl) {
3168   sys::swapByteOrder(mdl.count);
3169 }
3170
3171 inline void swapStruct(struct objc_method_description_t &md) {
3172   sys::swapByteOrder(md.name);
3173   sys::swapByteOrder(md.types);
3174 }
3175
3176 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3177                                                  struct DisassembleInfo *info);
3178
3179 // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3180 // to an Objective-C class and returns the class name.  It is also passed the
3181 // address of the pointer, so when the pointer is zero as it can be in an .o
3182 // file, that is used to look for an external relocation entry with a symbol
3183 // name.
3184 static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3185                                               uint64_t ReferenceValue,
3186                                               struct DisassembleInfo *info) {
3187   const char *r;
3188   uint32_t offset, left;
3189   SectionRef S;
3190
3191   // The pointer_value can be 0 in an object file and have a relocation
3192   // entry for the class symbol at the ReferenceValue (the address of the
3193   // pointer).
3194   if (pointer_value == 0) {
3195     r = get_pointer_64(ReferenceValue, offset, left, S, info);
3196     if (r == nullptr || left < sizeof(uint64_t))
3197       return nullptr;
3198     uint64_t n_value;
3199     const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3200     if (symbol_name == nullptr)
3201       return nullptr;
3202     const char *class_name = strrchr(symbol_name, '$');
3203     if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3204       return class_name + 2;
3205     else
3206       return nullptr;
3207   }
3208
3209   // The case were the pointer_value is non-zero and points to a class defined
3210   // in this Mach-O file.
3211   r = get_pointer_64(pointer_value, offset, left, S, info);
3212   if (r == nullptr || left < sizeof(struct class64_t))
3213     return nullptr;
3214   struct class64_t c;
3215   memcpy(&c, r, sizeof(struct class64_t));
3216   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3217     swapStruct(c);
3218   if (c.data == 0)
3219     return nullptr;
3220   r = get_pointer_64(c.data, offset, left, S, info);
3221   if (r == nullptr || left < sizeof(struct class_ro64_t))
3222     return nullptr;
3223   struct class_ro64_t cro;
3224   memcpy(&cro, r, sizeof(struct class_ro64_t));
3225   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3226     swapStruct(cro);
3227   if (cro.name == 0)
3228     return nullptr;
3229   const char *name = get_pointer_64(cro.name, offset, left, S, info);
3230   return name;
3231 }
3232
3233 // get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3234 // pointer to a cfstring and returns its name or nullptr.
3235 static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3236                                                  struct DisassembleInfo *info) {
3237   const char *r, *name;
3238   uint32_t offset, left;
3239   SectionRef S;
3240   struct cfstring64_t cfs;
3241   uint64_t cfs_characters;
3242
3243   r = get_pointer_64(ReferenceValue, offset, left, S, info);
3244   if (r == nullptr || left < sizeof(struct cfstring64_t))
3245     return nullptr;
3246   memcpy(&cfs, r, sizeof(struct cfstring64_t));
3247   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3248     swapStruct(cfs);
3249   if (cfs.characters == 0) {
3250     uint64_t n_value;
3251     const char *symbol_name = get_symbol_64(
3252         offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
3253     if (symbol_name == nullptr)
3254       return nullptr;
3255     cfs_characters = n_value;
3256   } else
3257     cfs_characters = cfs.characters;
3258   name = get_pointer_64(cfs_characters, offset, left, S, info);
3259
3260   return name;
3261 }
3262
3263 // get_objc2_64bit_selref() is used for disassembly and is passed a the address
3264 // of a pointer to an Objective-C selector reference when the pointer value is
3265 // zero as in a .o file and is likely to have a external relocation entry with
3266 // who's symbol's n_value is the real pointer to the selector name.  If that is
3267 // the case the real pointer to the selector name is returned else 0 is
3268 // returned
3269 static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3270                                        struct DisassembleInfo *info) {
3271   uint32_t offset, left;
3272   SectionRef S;
3273
3274   const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3275   if (r == nullptr || left < sizeof(uint64_t))
3276     return 0;
3277   uint64_t n_value;
3278   const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3279   if (symbol_name == nullptr)
3280     return 0;
3281   return n_value;
3282 }
3283
3284 static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3285                                     const char *sectname) {
3286   for (const SectionRef &Section : O->sections()) {
3287     StringRef SectName;
3288     Section.getName(SectName);
3289     DataRefImpl Ref = Section.getRawDataRefImpl();
3290     StringRef SegName = O->getSectionFinalSegmentName(Ref);
3291     if (SegName == segname && SectName == sectname)
3292       return Section;
3293   }
3294   return SectionRef();
3295 }
3296
3297 static void
3298 walk_pointer_list_64(const char *listname, const SectionRef S,
3299                      MachOObjectFile *O, struct DisassembleInfo *info,
3300                      void (*func)(uint64_t, struct DisassembleInfo *info)) {
3301   if (S == SectionRef())
3302     return;
3303
3304   StringRef SectName;
3305   S.getName(SectName);
3306   DataRefImpl Ref = S.getRawDataRefImpl();
3307   StringRef SegName = O->getSectionFinalSegmentName(Ref);
3308   outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3309
3310   StringRef BytesStr;
3311   S.getContents(BytesStr);
3312   const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3313
3314   for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3315     uint32_t left = S.getSize() - i;
3316     uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3317     uint64_t p = 0;
3318     memcpy(&p, Contents + i, size);
3319     if (i + sizeof(uint64_t) > S.getSize())
3320       outs() << listname << " list pointer extends past end of (" << SegName
3321              << "," << SectName << ") section\n";
3322     outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
3323
3324     if (O->isLittleEndian() != sys::IsLittleEndianHost)
3325       sys::swapByteOrder(p);
3326
3327     uint64_t n_value = 0;
3328     const char *name = get_symbol_64(i, S, info, n_value, p);
3329     if (name == nullptr)
3330       name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
3331
3332     if (n_value != 0) {
3333       outs() << format("0x%" PRIx64, n_value);
3334       if (p != 0)
3335         outs() << " + " << format("0x%" PRIx64, p);
3336     } else
3337       outs() << format("0x%" PRIx64, p);
3338     if (name != nullptr)
3339       outs() << " " << name;
3340     outs() << "\n";
3341
3342     p += n_value;
3343     if (func)
3344       func(p, info);
3345   }
3346 }
3347
3348 static void
3349 walk_pointer_list_32(const char *listname, const SectionRef S,
3350                      MachOObjectFile *O, struct DisassembleInfo *info,
3351                      void (*func)(uint32_t, struct DisassembleInfo *info)) {
3352   if (S == SectionRef())
3353     return;
3354
3355   StringRef SectName;
3356   S.getName(SectName);
3357   DataRefImpl Ref = S.getRawDataRefImpl();
3358   StringRef SegName = O->getSectionFinalSegmentName(Ref);
3359   outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3360
3361   StringRef BytesStr;
3362   S.getContents(BytesStr);
3363   const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3364
3365   for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
3366     uint32_t left = S.getSize() - i;
3367     uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
3368     uint32_t p = 0;
3369     memcpy(&p, Contents + i, size);
3370     if (i + sizeof(uint32_t) > S.getSize())
3371       outs() << listname << " list pointer extends past end of (" << SegName
3372              << "," << SectName << ") section\n";
3373     uint32_t Address = S.getAddress() + i;
3374     outs() << format("%08" PRIx32, Address) << " ";
3375
3376     if (O->isLittleEndian() != sys::IsLittleEndianHost)
3377       sys::swapByteOrder(p);
3378     outs() << format("0x%" PRIx32, p);
3379
3380     const char *name = get_symbol_32(i, S, info, p);
3381     if (name != nullptr)
3382       outs() << " " << name;
3383     outs() << "\n";
3384
3385     if (func)
3386       func(p, info);
3387   }
3388 }
3389
3390 static void print_layout_map(const char *layout_map, uint32_t left) {
3391   if (layout_map == nullptr)
3392     return;
3393   outs() << "                layout map: ";
3394   do {
3395     outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
3396     left--;
3397     layout_map++;
3398   } while (*layout_map != '\0' && left != 0);
3399   outs() << "\n";
3400 }
3401
3402 static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
3403   uint32_t offset, left;
3404   SectionRef S;
3405   const char *layout_map;
3406
3407   if (p == 0)
3408     return;
3409   layout_map = get_pointer_64(p, offset, left, S, info);
3410   print_layout_map(layout_map, left);
3411 }
3412
3413 static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
3414   uint32_t offset, left;
3415   SectionRef S;
3416   const char *layout_map;
3417
3418   if (p == 0)
3419     return;
3420   layout_map = get_pointer_32(p, offset, left, S, info);
3421   print_layout_map(layout_map, left);
3422 }
3423
3424 static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
3425                                   const char *indent) {
3426   struct method_list64_t ml;
3427   struct method64_t m;
3428   const char *r;
3429   uint32_t offset, xoffset, left, i;
3430   SectionRef S, xS;
3431   const char *name, *sym_name;
3432   uint64_t n_value;
3433
3434   r = get_pointer_64(p, offset, left, S, info);
3435   if (r == nullptr)
3436     return;
3437   memset(&ml, '\0', sizeof(struct method_list64_t));
3438   if (left < sizeof(struct method_list64_t)) {
3439     memcpy(&ml, r, left);
3440     outs() << "   (method_list_t entends past the end of the section)\n";
3441   } else
3442     memcpy(&ml, r, sizeof(struct method_list64_t));
3443   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3444     swapStruct(ml);
3445   outs() << indent << "\t\t   entsize " << ml.entsize << "\n";
3446   outs() << indent << "\t\t     count " << ml.count << "\n";
3447
3448   p += sizeof(struct method_list64_t);
3449   offset += sizeof(struct method_list64_t);
3450   for (i = 0; i < ml.count; i++) {
3451     r = get_pointer_64(p, offset, left, S, info);
3452     if (r == nullptr)
3453       return;
3454     memset(&m, '\0', sizeof(struct method64_t));
3455     if (left < sizeof(struct method64_t)) {
3456       memcpy(&m, r, left);
3457       outs() << indent << "   (method_t extends past the end of the section)\n";
3458     } else
3459       memcpy(&m, r, sizeof(struct method64_t));
3460     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3461       swapStruct(m);
3462
3463     outs() << indent << "\t\t      name ";
3464     sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
3465                              info, n_value, m.name);
3466     if (n_value != 0) {
3467       if (info->verbose && sym_name != nullptr)
3468         outs() << sym_name;
3469       else
3470         outs() << format("0x%" PRIx64, n_value);
3471       if (m.name != 0)
3472         outs() << " + " << format("0x%" PRIx64, m.name);
3473     } else
3474       outs() << format("0x%" PRIx64, m.name);
3475     name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
3476     if (name != nullptr)
3477       outs() << format(" %.*s", left, name);
3478     outs() << "\n";
3479
3480     outs() << indent << "\t\t     types ";
3481     sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
3482                              info, n_value, m.types);
3483     if (n_value != 0) {
3484       if (info->verbose && sym_name != nullptr)
3485         outs() << sym_name;
3486       else
3487         outs() << format("0x%" PRIx64, n_value);
3488       if (m.types != 0)
3489         outs() << " + " << format("0x%" PRIx64, m.types);
3490     } else
3491       outs() << format("0x%" PRIx64, m.types);
3492     name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
3493     if (name != nullptr)
3494       outs() << format(" %.*s", left, name);
3495     outs() << "\n";
3496
3497     outs() << indent << "\t\t       imp ";
3498     name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
3499                          n_value, m.imp);
3500     if (info->verbose && name == nullptr) {
3501       if (n_value != 0) {
3502         outs() << format("0x%" PRIx64, n_value) << " ";
3503         if (m.imp != 0)
3504           outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
3505       } else
3506         outs() << format("0x%" PRIx64, m.imp) << " ";
3507     }
3508     if (name != nullptr)
3509       outs() << name;
3510     outs() << "\n";
3511
3512     p += sizeof(struct method64_t);
3513     offset += sizeof(struct method64_t);
3514   }
3515 }
3516
3517 static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
3518                                   const char *indent) {
3519   struct method_list32_t ml;
3520   struct method32_t m;
3521   const char *r, *name;
3522   uint32_t offset, xoffset, left, i;
3523   SectionRef S, xS;
3524
3525   r = get_pointer_32(p, offset, left, S, info);
3526   if (r == nullptr)
3527     return;
3528   memset(&ml, '\0', sizeof(struct method_list32_t));
3529   if (left < sizeof(struct method_list32_t)) {
3530     memcpy(&ml, r, left);
3531     outs() << "   (method_list_t entends past the end of the section)\n";
3532   } else
3533     memcpy(&ml, r, sizeof(struct method_list32_t));
3534   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3535     swapStruct(ml);
3536   outs() << indent << "\t\t   entsize " << ml.entsize << "\n";
3537   outs() << indent << "\t\t     count " << ml.count << "\n";
3538
3539   p += sizeof(struct method_list32_t);
3540   offset += sizeof(struct method_list32_t);
3541   for (i = 0; i < ml.count; i++) {
3542     r = get_pointer_32(p, offset, left, S, info);
3543     if (r == nullptr)
3544       return;
3545     memset(&m, '\0', sizeof(struct method32_t));
3546     if (left < sizeof(struct method32_t)) {
3547       memcpy(&ml, r, left);
3548       outs() << indent << "   (method_t entends past the end of the section)\n";
3549     } else
3550       memcpy(&m, r, sizeof(struct method32_t));
3551     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3552       swapStruct(m);
3553
3554     outs() << indent << "\t\t      name " << format("0x%" PRIx32, m.name);
3555     name = get_pointer_32(m.name, xoffset, left, xS, info);
3556     if (name != nullptr)
3557       outs() << format(" %.*s", left, name);
3558     outs() << "\n";
3559
3560     outs() << indent << "\t\t     types " << format("0x%" PRIx32, m.types);
3561     name = get_pointer_32(m.types, xoffset, left, xS, info);
3562     if (name != nullptr)
3563       outs() << format(" %.*s", left, name);
3564     outs() << "\n";
3565
3566     outs() << indent << "\t\t       imp " << format("0x%" PRIx32, m.imp);
3567     name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
3568                          m.imp);
3569     if (name != nullptr)
3570       outs() << " " << name;
3571     outs() << "\n";
3572
3573     p += sizeof(struct method32_t);
3574     offset += sizeof(struct method32_t);
3575   }
3576 }
3577
3578 static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
3579   uint32_t offset, left, xleft;
3580   SectionRef S;
3581   struct objc_method_list_t method_list;
3582   struct objc_method_t method;
3583   const char *r, *methods, *name, *SymbolName;
3584   int32_t i;
3585
3586   r = get_pointer_32(p, offset, left, S, info, true);
3587   if (r == nullptr)
3588     return true;
3589
3590   outs() << "\n";
3591   if (left > sizeof(struct objc_method_list_t)) {
3592     memcpy(&method_list, r, sizeof(struct objc_method_list_t));
3593   } else {
3594     outs() << "\t\t objc_method_list extends past end of the section\n";
3595     memset(&method_list, '\0', sizeof(struct objc_method_list_t));
3596     memcpy(&method_list, r, left);
3597   }
3598   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3599     swapStruct(method_list);
3600
3601   outs() << "\t\t         obsolete "
3602          << format("0x%08" PRIx32, method_list.obsolete) << "\n";
3603   outs() << "\t\t     method_count " << method_list.method_count << "\n";
3604
3605   methods = r + sizeof(struct objc_method_list_t);
3606   for (i = 0; i < method_list.method_count; i++) {
3607     if ((i + 1) * sizeof(struct objc_method_t) > left) {
3608       outs() << "\t\t remaining method's extend past the of the section\n";
3609       break;
3610     }
3611     memcpy(&method, methods + i * sizeof(struct objc_method_t),
3612            sizeof(struct objc_method_t));
3613     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3614       swapStruct(method);
3615
3616     outs() << "\t\t      method_name "
3617            << format("0x%08" PRIx32, method.method_name);
3618     if (info->verbose) {
3619       name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
3620       if (name != nullptr)
3621         outs() << format(" %.*s", xleft, name);
3622       else
3623         outs() << " (not in an __OBJC section)";
3624     }
3625     outs() << "\n";
3626
3627     outs() << "\t\t     method_types "
3628            << format("0x%08" PRIx32, method.method_types);
3629     if (info->verbose) {
3630       name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
3631       if (name != nullptr)
3632         outs() << format(" %.*s", xleft, name);
3633       else
3634         outs() << " (not in an __OBJC section)";
3635     }
3636     outs() << "\n";
3637
3638     outs() << "\t\t       method_imp "
3639            << format("0x%08" PRIx32, method.method_imp) << " ";
3640     if (info->verbose) {
3641       SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
3642       if (SymbolName != nullptr)
3643         outs() << SymbolName;
3644     }
3645     outs() << "\n";
3646   }
3647   return false;
3648 }
3649
3650 static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
3651   struct protocol_list64_t pl;
3652   uint64_t q, n_value;
3653   struct protocol64_t pc;
3654   const char *r;
3655   uint32_t offset, xoffset, left, i;
3656   SectionRef S, xS;
3657   const char *name, *sym_name;
3658
3659   r = get_pointer_64(p, offset, left, S, info);
3660   if (r == nullptr)
3661     return;
3662   memset(&pl, '\0', sizeof(struct protocol_list64_t));
3663   if (left < sizeof(struct protocol_list64_t)) {
3664     memcpy(&pl, r, left);
3665     outs() << "   (protocol_list_t entends past the end of the section)\n";
3666   } else
3667     memcpy(&pl, r, sizeof(struct protocol_list64_t));
3668   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3669     swapStruct(pl);
3670   outs() << "                      count " << pl.count << "\n";
3671
3672   p += sizeof(struct protocol_list64_t);
3673   offset += sizeof(struct protocol_list64_t);
3674   for (i = 0; i < pl.count; i++) {
3675     r = get_pointer_64(p, offset, left, S, info);
3676     if (r == nullptr)
3677       return;
3678     q = 0;
3679     if (left < sizeof(uint64_t)) {
3680       memcpy(&q, r, left);
3681       outs() << "   (protocol_t * entends past the end of the section)\n";
3682     } else
3683       memcpy(&q, r, sizeof(uint64_t));
3684     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3685       sys::swapByteOrder(q);
3686
3687     outs() << "\t\t      list[" << i << "] ";
3688     sym_name = get_symbol_64(offset, S, info, n_value, q);
3689     if (n_value != 0) {
3690       if (info->verbose && sym_name != nullptr)
3691         outs() << sym_name;
3692       else
3693         outs() << format("0x%" PRIx64, n_value);
3694       if (q != 0)
3695         outs() << " + " << format("0x%" PRIx64, q);
3696     } else
3697       outs() << format("0x%" PRIx64, q);
3698     outs() << " (struct protocol_t *)\n";
3699
3700     r = get_pointer_64(q + n_value, offset, left, S, info);
3701     if (r == nullptr)
3702       return;
3703     memset(&pc, '\0', sizeof(struct protocol64_t));
3704     if (left < sizeof(struct protocol64_t)) {
3705       memcpy(&pc, r, left);
3706       outs() << "   (protocol_t entends past the end of the section)\n";
3707     } else
3708       memcpy(&pc, r, sizeof(struct protocol64_t));
3709     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3710       swapStruct(pc);
3711
3712     outs() << "\t\t\t      isa " << format("0x%" PRIx64, pc.isa) << "\n";
3713
3714     outs() << "\t\t\t     name ";
3715     sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
3716                              info, n_value, pc.name);
3717     if (n_value != 0) {
3718       if (info->verbose && sym_name != nullptr)
3719         outs() << sym_name;
3720       else
3721         outs() << format("0x%" PRIx64, n_value);
3722       if (pc.name != 0)
3723         outs() << " + " << format("0x%" PRIx64, pc.name);
3724     } else
3725       outs() << format("0x%" PRIx64, pc.name);
3726     name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
3727     if (name != nullptr)
3728       outs() << format(" %.*s", left, name);
3729     outs() << "\n";
3730
3731     outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
3732
3733     outs() << "\t\t  instanceMethods ";
3734     sym_name =
3735         get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
3736                       S, info, n_value, pc.instanceMethods);
3737     if (n_value != 0) {
3738       if (info->verbose && sym_name != nullptr)
3739         outs() << sym_name;
3740       else
3741         outs() << format("0x%" PRIx64, n_value);
3742       if (pc.instanceMethods != 0)
3743         outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
3744     } else
3745       outs() << format("0x%" PRIx64, pc.instanceMethods);
3746     outs() << " (struct method_list_t *)\n";
3747     if (pc.instanceMethods + n_value != 0)
3748       print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
3749
3750     outs() << "\t\t     classMethods ";
3751     sym_name =
3752         get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
3753                       info, n_value, pc.classMethods);
3754     if (n_value != 0) {
3755       if (info->verbose && sym_name != nullptr)
3756         outs() << sym_name;
3757       else
3758         outs() << format("0x%" PRIx64, n_value);
3759       if (pc.classMethods != 0)
3760         outs() << " + " << format("0x%" PRIx64, pc.classMethods);
3761     } else
3762       outs() << format("0x%" PRIx64, pc.classMethods);
3763     outs() << " (struct method_list_t *)\n";
3764     if (pc.classMethods + n_value != 0)
3765       print_method_list64_t(pc.classMethods + n_value, info, "\t");
3766
3767     outs() << "\t  optionalInstanceMethods "
3768            << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
3769     outs() << "\t     optionalClassMethods "
3770            << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
3771     outs() << "\t       instanceProperties "
3772            << format("0x%" PRIx64, pc.instanceProperties) << "\n";
3773
3774     p += sizeof(uint64_t);
3775     offset += sizeof(uint64_t);
3776   }
3777 }
3778
3779 static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
3780   struct protocol_list32_t pl;
3781   uint32_t q;
3782   struct protocol32_t pc;
3783   const char *r;
3784   uint32_t offset, xoffset, left, i;
3785   SectionRef S, xS;
3786   const char *name;
3787
3788   r = get_pointer_32(p, offset, left, S, info);
3789   if (r == nullptr)
3790     return;
3791   memset(&pl, '\0', sizeof(struct protocol_list32_t));
3792   if (left < sizeof(struct protocol_list32_t)) {
3793     memcpy(&pl, r, left);
3794     outs() << "   (protocol_list_t entends past the end of the section)\n";
3795   } else
3796     memcpy(&pl, r, sizeof(struct protocol_list32_t));
3797   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3798     swapStruct(pl);
3799   outs() << "                      count " << pl.count << "\n";
3800
3801   p += sizeof(struct protocol_list32_t);
3802   offset += sizeof(struct protocol_list32_t);
3803   for (i = 0; i < pl.count; i++) {
3804     r = get_pointer_32(p, offset, left, S, info);
3805     if (r == nullptr)
3806       return;
3807     q = 0;
3808     if (left < sizeof(uint32_t)) {
3809       memcpy(&q, r, left);
3810       outs() << "   (protocol_t * entends past the end of the section)\n";
3811     } else
3812       memcpy(&q, r, sizeof(uint32_t));
3813     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3814       sys::swapByteOrder(q);
3815     outs() << "\t\t      list[" << i << "] " << format("0x%" PRIx32, q)
3816            << " (struct protocol_t *)\n";
3817     r = get_pointer_32(q, offset, left, S, info);
3818     if (r == nullptr)
3819       return;
3820     memset(&pc, '\0', sizeof(struct protocol32_t));
3821     if (left < sizeof(struct protocol32_t)) {
3822       memcpy(&pc, r, left);
3823       outs() << "   (protocol_t entends past the end of the section)\n";
3824     } else
3825       memcpy(&pc, r, sizeof(struct protocol32_t));
3826     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3827       swapStruct(pc);
3828     outs() << "\t\t\t      isa " << format("0x%" PRIx32, pc.isa) << "\n";
3829     outs() << "\t\t\t     name " << format("0x%" PRIx32, pc.name);
3830     name = get_pointer_32(pc.name, xoffset, left, xS, info);
3831     if (name != nullptr)
3832       outs() << format(" %.*s", left, name);
3833     outs() << "\n";
3834     outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
3835     outs() << "\t\t  instanceMethods "
3836            << format("0x%" PRIx32, pc.instanceMethods)
3837            << " (struct method_list_t *)\n";
3838     if (pc.instanceMethods != 0)
3839       print_method_list32_t(pc.instanceMethods, info, "\t");
3840     outs() << "\t\t     classMethods " << format("0x%" PRIx32, pc.classMethods)
3841            << " (struct method_list_t *)\n";
3842     if (pc.classMethods != 0)
3843       print_method_list32_t(pc.classMethods, info, "\t");
3844     outs() << "\t  optionalInstanceMethods "
3845            << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
3846     outs() << "\t     optionalClassMethods "
3847            << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
3848     outs() << "\t       instanceProperties "
3849            << format("0x%" PRIx32, pc.instanceProperties) << "\n";
3850     p += sizeof(uint32_t);
3851     offset += sizeof(uint32_t);
3852   }
3853 }
3854
3855 static void print_indent(uint32_t indent) {
3856   for (uint32_t i = 0; i < indent;) {
3857     if (indent - i >= 8) {
3858       outs() << "\t";
3859       i += 8;
3860     } else {
3861       for (uint32_t j = i; j < indent; j++)
3862         outs() << " ";
3863       return;
3864     }
3865   }
3866 }
3867
3868 static bool print_method_description_list(uint32_t p, uint32_t indent,
3869                                           struct DisassembleInfo *info) {
3870   uint32_t offset, left, xleft;
3871   SectionRef S;
3872   struct objc_method_description_list_t mdl;
3873   struct objc_method_description_t md;
3874   const char *r, *list, *name;
3875   int32_t i;
3876
3877   r = get_pointer_32(p, offset, left, S, info, true);
3878   if (r == nullptr)
3879     return true;
3880
3881   outs() << "\n";
3882   if (left > sizeof(struct objc_method_description_list_t)) {
3883     memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
3884   } else {
3885     print_indent(indent);
3886     outs() << " objc_method_description_list extends past end of the section\n";
3887     memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
3888     memcpy(&mdl, r, left);
3889   }
3890   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3891     swapStruct(mdl);
3892
3893   print_indent(indent);
3894   outs() << "        count " << mdl.count << "\n";
3895
3896   list = r + sizeof(struct objc_method_description_list_t);
3897   for (i = 0; i < mdl.count; i++) {
3898     if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
3899       print_indent(indent);
3900       outs() << " remaining list entries extend past the of the section\n";
3901       break;
3902     }
3903     print_indent(indent);
3904     outs() << "        list[" << i << "]\n";
3905     memcpy(&md, list + i * sizeof(struct objc_method_description_t),
3906            sizeof(struct objc_method_description_t));
3907     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3908       swapStruct(md);
3909
3910     print_indent(indent);
3911     outs() << "             name " << format("0x%08" PRIx32, md.name);
3912     if (info->verbose) {
3913       name = get_pointer_32(md.name, offset, xleft, S, info, true);
3914       if (name != nullptr)
3915         outs() << format(" %.*s", xleft, name);
3916       else
3917         outs() << " (not in an __OBJC section)";
3918     }
3919     outs() << "\n";
3920
3921     print_indent(indent);
3922     outs() << "            types " << format("0x%08" PRIx32, md.types);
3923     if (info->verbose) {
3924       name = get_pointer_32(md.types, offset, xleft, S, info, true);
3925       if (name != nullptr)
3926         outs() << format(" %.*s", xleft, name);
3927       else
3928         outs() << " (not in an __OBJC section)";
3929     }
3930     outs() << "\n";
3931   }
3932   return false;
3933 }
3934
3935 static bool print_protocol_list(uint32_t p, uint32_t indent,
3936                                 struct DisassembleInfo *info);
3937
3938 static bool print_protocol(uint32_t p, uint32_t indent,
3939                            struct DisassembleInfo *info) {
3940   uint32_t offset, left;
3941   SectionRef S;
3942   struct objc_protocol_t protocol;
3943   const char *r, *name;
3944
3945   r = get_pointer_32(p, offset, left, S, info, true);
3946   if (r == nullptr)
3947     return true;
3948
3949   outs() << "\n";
3950   if (left >= sizeof(struct objc_protocol_t)) {
3951     memcpy(&protocol, r, sizeof(struct objc_protocol_t));
3952   } else {
3953     print_indent(indent);
3954     outs() << "            Protocol extends past end of the section\n";
3955     memset(&protocol, '\0', sizeof(struct objc_protocol_t));
3956     memcpy(&protocol, r, left);
3957   }
3958   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3959     swapStruct(protocol);
3960
3961   print_indent(indent);
3962   outs() << "              isa " << format("0x%08" PRIx32, protocol.isa)
3963          << "\n";
3964
3965   print_indent(indent);
3966   outs() << "    protocol_name "
3967          << format("0x%08" PRIx32, protocol.protocol_name);
3968   if (info->verbose) {
3969     name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
3970     if (name != nullptr)
3971       outs() << format(" %.*s", left, name);
3972     else
3973       outs() << " (not in an __OBJC section)";
3974   }
3975   outs() << "\n";
3976
3977   print_indent(indent);
3978   outs() << "    protocol_list "
3979          << format("0x%08" PRIx32, protocol.protocol_list);
3980   if (print_protocol_list(protocol.protocol_list, indent + 4, info))
3981     outs() << " (not in an __OBJC section)\n";
3982
3983   print_indent(indent);
3984   outs() << " instance_methods "
3985          << format("0x%08" PRIx32, protocol.instance_methods);
3986   if (print_method_description_list(protocol.instance_methods, indent, info))
3987     outs() << " (not in an __OBJC section)\n";
3988
3989   print_indent(indent);
3990   outs() << "    class_methods "
3991          << format("0x%08" PRIx32, protocol.class_methods);
3992   if (print_method_description_list(protocol.class_methods, indent, info))
3993     outs() << " (not in an __OBJC section)\n";
3994
3995   return false;
3996 }
3997
3998 static bool print_protocol_list(uint32_t p, uint32_t indent,
3999                                 struct DisassembleInfo *info) {
4000   uint32_t offset, left, l;
4001   SectionRef S;
4002   struct objc_protocol_list_t protocol_list;
4003   const char *r, *list;
4004   int32_t i;
4005
4006   r = get_pointer_32(p, offset, left, S, info, true);
4007   if (r == nullptr)
4008     return true;
4009
4010   outs() << "\n";
4011   if (left > sizeof(struct objc_protocol_list_t)) {
4012     memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
4013   } else {
4014     outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
4015     memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
4016     memcpy(&protocol_list, r, left);
4017   }
4018   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4019     swapStruct(protocol_list);
4020
4021   print_indent(indent);
4022   outs() << "         next " << format("0x%08" PRIx32, protocol_list.next)
4023          << "\n";
4024   print_indent(indent);
4025   outs() << "        count " << protocol_list.count << "\n";
4026
4027   list = r + sizeof(struct objc_protocol_list_t);
4028   for (i = 0; i < protocol_list.count; i++) {
4029     if ((i + 1) * sizeof(uint32_t) > left) {
4030       outs() << "\t\t remaining list entries extend past the of the section\n";
4031       break;
4032     }
4033     memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
4034     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4035       sys::swapByteOrder(l);
4036
4037     print_indent(indent);
4038     outs() << "      list[" << i << "] " << format("0x%08" PRIx32, l);
4039     if (print_protocol(l, indent, info))
4040       outs() << "(not in an __OBJC section)\n";
4041   }
4042   return false;
4043 }
4044
4045 static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
4046   struct ivar_list64_t il;
4047   struct ivar64_t i;
4048   const char *r;
4049   uint32_t offset, xoffset, left, j;
4050   SectionRef S, xS;
4051   const char *name, *sym_name, *ivar_offset_p;
4052   uint64_t ivar_offset, n_value;
4053
4054   r = get_pointer_64(p, offset, left, S, info);
4055   if (r == nullptr)
4056     return;
4057   memset(&il, '\0', sizeof(struct ivar_list64_t));
4058   if (left < sizeof(struct ivar_list64_t)) {
4059     memcpy(&il, r, left);
4060     outs() << "   (ivar_list_t entends past the end of the section)\n";
4061   } else
4062     memcpy(&il, r, sizeof(struct ivar_list64_t));
4063   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4064     swapStruct(il);
4065   outs() << "                    entsize " << il.entsize << "\n";
4066   outs() << "                      count " << il.count << "\n";
4067
4068   p += sizeof(struct ivar_list64_t);
4069   offset += sizeof(struct ivar_list64_t);
4070   for (j = 0; j < il.count; j++) {
4071     r = get_pointer_64(p, offset, left, S, info);
4072     if (r == nullptr)
4073       return;
4074     memset(&i, '\0', sizeof(struct ivar64_t));
4075     if (left < sizeof(struct ivar64_t)) {
4076       memcpy(&i, r, left);
4077       outs() << "   (ivar_t entends past the end of the section)\n";
4078     } else
4079       memcpy(&i, r, sizeof(struct ivar64_t));
4080     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4081       swapStruct(i);
4082
4083     outs() << "\t\t\t   offset ";
4084     sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
4085                              info, n_value, i.offset);
4086     if (n_value != 0) {
4087       if (info->verbose && sym_name != nullptr)
4088         outs() << sym_name;
4089       else
4090         outs() << format("0x%" PRIx64, n_value);
4091       if (i.offset != 0)
4092         outs() << " + " << format("0x%" PRIx64, i.offset);
4093     } else
4094       outs() << format("0x%" PRIx64, i.offset);
4095     ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
4096     if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4097       memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4098       if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4099         sys::swapByteOrder(ivar_offset);
4100       outs() << " " << ivar_offset << "\n";
4101     } else
4102       outs() << "\n";
4103
4104     outs() << "\t\t\t     name ";
4105     sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
4106                              n_value, i.name);
4107     if (n_value != 0) {
4108       if (info->verbose && sym_name != nullptr)
4109         outs() << sym_name;
4110       else
4111         outs() << format("0x%" PRIx64, n_value);
4112       if (i.name != 0)
4113         outs() << " + " << format("0x%" PRIx64, i.name);
4114     } else
4115       outs() << format("0x%" PRIx64, i.name);
4116     name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
4117     if (name != nullptr)
4118       outs() << format(" %.*s", left, name);
4119     outs() << "\n";
4120
4121     outs() << "\t\t\t     type ";
4122     sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
4123                              n_value, i.name);
4124     name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
4125     if (n_value != 0) {
4126       if (info->verbose && sym_name != nullptr)
4127         outs() << sym_name;
4128       else
4129         outs() << format("0x%" PRIx64, n_value);
4130       if (i.type != 0)
4131         outs() << " + " << format("0x%" PRIx64, i.type);
4132     } else
4133       outs() << format("0x%" PRIx64, i.type);
4134     if (name != nullptr)
4135       outs() << format(" %.*s", left, name);
4136     outs() << "\n";
4137
4138     outs() << "\t\t\talignment " << i.alignment << "\n";
4139     outs() << "\t\t\t     size " << i.size << "\n";
4140
4141     p += sizeof(struct ivar64_t);
4142     offset += sizeof(struct ivar64_t);
4143   }
4144 }
4145
4146 static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
4147   struct ivar_list32_t il;
4148   struct ivar32_t i;
4149   const char *r;
4150   uint32_t offset, xoffset, left, j;
4151   SectionRef S, xS;
4152   const char *name, *ivar_offset_p;
4153   uint32_t ivar_offset;
4154
4155   r = get_pointer_32(p, offset, left, S, info);
4156   if (r == nullptr)
4157     return;
4158   memset(&il, '\0', sizeof(struct ivar_list32_t));
4159   if (left < sizeof(struct ivar_list32_t)) {
4160     memcpy(&il, r, left);
4161     outs() << "   (ivar_list_t entends past the end of the section)\n";
4162   } else
4163     memcpy(&il, r, sizeof(struct ivar_list32_t));
4164   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4165     swapStruct(il);
4166   outs() << "                    entsize " << il.entsize << "\n";
4167   outs() << "                      count " << il.count << "\n";
4168
4169   p += sizeof(struct ivar_list32_t);
4170   offset += sizeof(struct ivar_list32_t);
4171   for (j = 0; j < il.count; j++) {
4172     r = get_pointer_32(p, offset, left, S, info);
4173     if (r == nullptr)
4174       return;
4175     memset(&i, '\0', sizeof(struct ivar32_t));
4176     if (left < sizeof(struct ivar32_t)) {
4177       memcpy(&i, r, left);
4178       outs() << "   (ivar_t entends past the end of the section)\n";
4179     } else
4180       memcpy(&i, r, sizeof(struct ivar32_t));
4181     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4182       swapStruct(i);
4183
4184     outs() << "\t\t\t   offset " << format("0x%" PRIx32, i.offset);
4185     ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4186     if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4187       memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4188       if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4189         sys::swapByteOrder(ivar_offset);
4190       outs() << " " << ivar_offset << "\n";
4191     } else
4192       outs() << "\n";
4193
4194     outs() << "\t\t\t     name " << format("0x%" PRIx32, i.name);
4195     name = get_pointer_32(i.name, xoffset, left, xS, info);
4196     if (name != nullptr)
4197       outs() << format(" %.*s", left, name);
4198     outs() << "\n";
4199
4200     outs() << "\t\t\t     type " << format("0x%" PRIx32, i.type);
4201     name = get_pointer_32(i.type, xoffset, left, xS, info);
4202     if (name != nullptr)
4203       outs() << format(" %.*s", left, name);
4204     outs() << "\n";
4205
4206     outs() << "\t\t\talignment " << i.alignment << "\n";
4207     outs() << "\t\t\t     size " << i.size << "\n";
4208
4209     p += sizeof(struct ivar32_t);
4210     offset += sizeof(struct ivar32_t);
4211   }
4212 }
4213
4214 static void print_objc_property_list64(uint64_t p,
4215                                        struct DisassembleInfo *info) {
4216   struct objc_property_list64 opl;
4217   struct objc_property64 op;
4218   const char *r;
4219   uint32_t offset, xoffset, left, j;
4220   SectionRef S, xS;
4221   const char *name, *sym_name;
4222   uint64_t n_value;
4223
4224   r = get_pointer_64(p, offset, left, S, info);
4225   if (r == nullptr)
4226     return;
4227   memset(&opl, '\0', sizeof(struct objc_property_list64));
4228   if (left < sizeof(struct objc_property_list64)) {
4229     memcpy(&opl, r, left);
4230     outs() << "   (objc_property_list entends past the end of the section)\n";
4231   } else
4232     memcpy(&opl, r, sizeof(struct objc_property_list64));
4233   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4234     swapStruct(opl);
4235   outs() << "                    entsize " << opl.entsize << "\n";
4236   outs() << "                      count " << opl.count << "\n";
4237
4238   p += sizeof(struct objc_property_list64);
4239   offset += sizeof(struct objc_property_list64);
4240   for (j = 0; j < opl.count; j++) {
4241     r = get_pointer_64(p, offset, left, S, info);
4242     if (r == nullptr)
4243       return;
4244     memset(&op, '\0', sizeof(struct objc_property64));
4245     if (left < sizeof(struct objc_property64)) {
4246       memcpy(&op, r, left);
4247       outs() << "   (objc_property entends past the end of the section)\n";
4248     } else
4249       memcpy(&op, r, sizeof(struct objc_property64));
4250     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4251       swapStruct(op);
4252
4253     outs() << "\t\t\t     name ";
4254     sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
4255                              info, n_value, op.name);
4256     if (n_value != 0) {
4257       if (info->verbose && sym_name != nullptr)
4258         outs() << sym_name;
4259       else
4260         outs() << format("0x%" PRIx64, n_value);
4261       if (op.name != 0)
4262         outs() << " + " << format("0x%" PRIx64, op.name);
4263     } else
4264       outs() << format("0x%" PRIx64, op.name);
4265     name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
4266     if (name != nullptr)
4267       outs() << format(" %.*s", left, name);
4268     outs() << "\n";
4269
4270     outs() << "\t\t\tattributes ";
4271     sym_name =
4272         get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
4273                       info, n_value, op.attributes);
4274     if (n_value != 0) {
4275       if (info->verbose && sym_name != nullptr)
4276         outs() << sym_name;
4277       else
4278         outs() << format("0x%" PRIx64, n_value);
4279       if (op.attributes != 0)
4280         outs() << " + " << format("0x%" PRIx64, op.attributes);
4281     } else
4282       outs() << format("0x%" PRIx64, op.attributes);
4283     name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
4284     if (name != nullptr)
4285       outs() << format(" %.*s", left, name);
4286     outs() << "\n";
4287
4288     p += sizeof(struct objc_property64);
4289     offset += sizeof(struct objc_property64);
4290   }
4291 }
4292
4293 static void print_objc_property_list32(uint32_t p,
4294                                        struct DisassembleInfo *info) {
4295   struct objc_property_list32 opl;
4296   struct objc_property32 op;
4297   const char *r;
4298   uint32_t offset, xoffset, left, j;
4299   SectionRef S, xS;
4300   const char *name;
4301
4302   r = get_pointer_32(p, offset, left, S, info);
4303   if (r == nullptr)
4304     return;
4305   memset(&opl, '\0', sizeof(struct objc_property_list32));
4306   if (left < sizeof(struct objc_property_list32)) {
4307     memcpy(&opl, r, left);
4308     outs() << "   (objc_property_list entends past the end of the section)\n";
4309   } else
4310     memcpy(&opl, r, sizeof(struct objc_property_list32));
4311   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4312     swapStruct(opl);
4313   outs() << "                    entsize " << opl.entsize << "\n";
4314   outs() << "                      count " << opl.count << "\n";
4315
4316   p += sizeof(struct objc_property_list32);
4317   offset += sizeof(struct objc_property_list32);
4318   for (j = 0; j < opl.count; j++) {
4319     r = get_pointer_32(p, offset, left, S, info);
4320     if (r == nullptr)
4321       return;
4322     memset(&op, '\0', sizeof(struct objc_property32));
4323     if (left < sizeof(struct objc_property32)) {
4324       memcpy(&op, r, left);
4325       outs() << "   (objc_property entends past the end of the section)\n";
4326     } else
4327       memcpy(&op, r, sizeof(struct objc_property32));
4328     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4329       swapStruct(op);
4330
4331     outs() << "\t\t\t     name " << format("0x%" PRIx32, op.name);
4332     name = get_pointer_32(op.name, xoffset, left, xS, info);
4333     if (name != nullptr)
4334       outs() << format(" %.*s", left, name);
4335     outs() << "\n";
4336
4337     outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
4338     name = get_pointer_32(op.attributes, xoffset, left, xS, info);
4339     if (name != nullptr)
4340       outs() << format(" %.*s", left, name);
4341     outs() << "\n";
4342
4343     p += sizeof(struct objc_property32);
4344     offset += sizeof(struct objc_property32);
4345   }
4346 }
4347
4348 static bool print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
4349                                bool &is_meta_class) {
4350   struct class_ro64_t cro;
4351   const char *r;
4352   uint32_t offset, xoffset, left;
4353   SectionRef S, xS;
4354   const char *name, *sym_name;
4355   uint64_t n_value;
4356
4357   r = get_pointer_64(p, offset, left, S, info);
4358   if (r == nullptr || left < sizeof(struct class_ro64_t))
4359     return false;
4360   memcpy(&cro, r, sizeof(struct class_ro64_t));
4361   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4362     swapStruct(cro);
4363   outs() << "                    flags " << format("0x%" PRIx32, cro.flags);
4364   if (cro.flags & RO_META)
4365     outs() << " RO_META";
4366   if (cro.flags & RO_ROOT)
4367     outs() << " RO_ROOT";
4368   if (cro.flags & RO_HAS_CXX_STRUCTORS)
4369     outs() << " RO_HAS_CXX_STRUCTORS";
4370   outs() << "\n";
4371   outs() << "            instanceStart " << cro.instanceStart << "\n";
4372   outs() << "             instanceSize " << cro.instanceSize << "\n";
4373   outs() << "                 reserved " << format("0x%" PRIx32, cro.reserved)
4374          << "\n";
4375   outs() << "               ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
4376          << "\n";
4377   print_layout_map64(cro.ivarLayout, info);
4378
4379   outs() << "                     name ";
4380   sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
4381                            info, n_value, cro.name);
4382   if (n_value != 0) {
4383     if (info->verbose && sym_name != nullptr)
4384       outs() << sym_name;
4385     else
4386       outs() << format("0x%" PRIx64, n_value);
4387     if (cro.name != 0)
4388       outs() << " + " << format("0x%" PRIx64, cro.name);
4389   } else
4390     outs() << format("0x%" PRIx64, cro.name);
4391   name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
4392   if (name != nullptr)
4393     outs() << format(" %.*s", left, name);
4394   outs() << "\n";
4395
4396   outs() << "              baseMethods ";
4397   sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
4398                            S, info, n_value, cro.baseMethods);
4399   if (n_value != 0) {
4400     if (info->verbose && sym_name != nullptr)
4401       outs() << sym_name;
4402     else
4403       outs() << format("0x%" PRIx64, n_value);
4404     if (cro.baseMethods != 0)
4405       outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
4406   } else
4407     outs() << format("0x%" PRIx64, cro.baseMethods);
4408   outs() << " (struct method_list_t *)\n";
4409   if (cro.baseMethods + n_value != 0)
4410     print_method_list64_t(cro.baseMethods + n_value, info, "");
4411
4412   outs() << "            baseProtocols ";
4413   sym_name =
4414       get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
4415                     info, n_value, cro.baseProtocols);
4416   if (n_value != 0) {
4417     if (info->verbose && sym_name != nullptr)
4418       outs() << sym_name;
4419     else
4420       outs() << format("0x%" PRIx64, n_value);
4421     if (cro.baseProtocols != 0)
4422       outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
4423   } else
4424     outs() << format("0x%" PRIx64, cro.baseProtocols);
4425   outs() << "\n";
4426   if (cro.baseProtocols + n_value != 0)
4427     print_protocol_list64_t(cro.baseProtocols + n_value, info);
4428
4429   outs() << "                    ivars ";
4430   sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
4431                            info, n_value, cro.ivars);
4432   if (n_value != 0) {
4433     if (info->verbose && sym_name != nullptr)
4434       outs() << sym_name;
4435     else
4436       outs() << format("0x%" PRIx64, n_value);
4437     if (cro.ivars != 0)
4438       outs() << " + " << format("0x%" PRIx64, cro.ivars);
4439   } else
4440     outs() << format("0x%" PRIx64, cro.ivars);
4441   outs() << "\n";
4442   if (cro.ivars + n_value != 0)
4443     print_ivar_list64_t(cro.ivars + n_value, info);
4444
4445   outs() << "           weakIvarLayout ";
4446   sym_name =
4447       get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
4448                     info, n_value, cro.weakIvarLayout);
4449   if (n_value != 0) {
4450     if (info->verbose && sym_name != nullptr)
4451       outs() << sym_name;
4452     else
4453       outs() << format("0x%" PRIx64, n_value);
4454     if (cro.weakIvarLayout != 0)
4455       outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
4456   } else
4457     outs() << format("0x%" PRIx64, cro.weakIvarLayout);
4458   outs() << "\n";
4459   print_layout_map64(cro.weakIvarLayout + n_value, info);
4460
4461   outs() << "           baseProperties ";
4462   sym_name =
4463       get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
4464                     info, n_value, cro.baseProperties);
4465   if (n_value != 0) {
4466     if (info->verbose && sym_name != nullptr)
4467       outs() << sym_name;
4468     else
4469       outs() << format("0x%" PRIx64, n_value);
4470     if (cro.baseProperties != 0)
4471       outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
4472   } else
4473     outs() << format("0x%" PRIx64, cro.baseProperties);
4474   outs() << "\n";
4475   if (cro.baseProperties + n_value != 0)
4476     print_objc_property_list64(cro.baseProperties + n_value, info);
4477
4478   is_meta_class = (cro.flags & RO_META) != 0;
4479   return true;
4480 }
4481
4482 static bool print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
4483                                bool &is_meta_class) {
4484   struct class_ro32_t cro;
4485   const char *r;
4486   uint32_t offset, xoffset, left;
4487   SectionRef S, xS;
4488   const char *name;
4489
4490   r = get_pointer_32(p, offset, left, S, info);
4491   if (r == nullptr)
4492     return false;
4493   memset(&cro, '\0', sizeof(struct class_ro32_t));
4494   if (left < sizeof(struct class_ro32_t)) {
4495     memcpy(&cro, r, left);
4496     outs() << "   (class_ro_t entends past the end of the section)\n";
4497   } else
4498     memcpy(&cro, r, sizeof(struct class_ro32_t));
4499   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4500     swapStruct(cro);
4501   outs() << "                    flags " << format("0x%" PRIx32, cro.flags);
4502   if (cro.flags & RO_META)
4503     outs() << " RO_META";
4504   if (cro.flags & RO_ROOT)
4505     outs() << " RO_ROOT";
4506   if (cro.flags & RO_HAS_CXX_STRUCTORS)
4507     outs() << " RO_HAS_CXX_STRUCTORS";
4508   outs() << "\n";
4509   outs() << "            instanceStart " << cro.instanceStart << "\n";
4510   outs() << "             instanceSize " << cro.instanceSize << "\n";
4511   outs() << "               ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
4512          << "\n";
4513   print_layout_map32(cro.ivarLayout, info);
4514
4515   outs() << "                     name " << format("0x%" PRIx32, cro.name);
4516   name = get_pointer_32(cro.name, xoffset, left, xS, info);
4517   if (name != nullptr)
4518     outs() << format(" %.*s", left, name);
4519   outs() << "\n";
4520
4521   outs() << "              baseMethods "
4522          << format("0x%" PRIx32, cro.baseMethods)
4523          << " (struct method_list_t *)\n";
4524   if (cro.baseMethods != 0)
4525     print_method_list32_t(cro.baseMethods, info, "");
4526
4527   outs() << "            baseProtocols "
4528          << format("0x%" PRIx32, cro.baseProtocols) << "\n";
4529   if (cro.baseProtocols != 0)
4530     print_protocol_list32_t(cro.baseProtocols, info);
4531   outs() << "                    ivars " << format("0x%" PRIx32, cro.ivars)
4532          << "\n";
4533   if (cro.ivars != 0)
4534     print_ivar_list32_t(cro.ivars, info);
4535   outs() << "           weakIvarLayout "
4536          << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
4537   print_layout_map32(cro.weakIvarLayout, info);
4538   outs() << "           baseProperties "
4539          << format("0x%" PRIx32, cro.baseProperties) << "\n";
4540   if (cro.baseProperties != 0)
4541     print_objc_property_list32(cro.baseProperties, info);
4542   is_meta_class = (cro.flags & RO_META) != 0;
4543   return true;
4544 }
4545
4546 static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
4547   struct class64_t c;
4548   const char *r;
4549   uint32_t offset, left;
4550   SectionRef S;
4551   const char *name;
4552   uint64_t isa_n_value, n_value;
4553
4554   r = get_pointer_64(p, offset, left, S, info);
4555   if (r == nullptr || left < sizeof(struct class64_t))
4556     return;
4557   memcpy(&c, r, sizeof(struct class64_t));
4558   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4559     swapStruct(c);
4560
4561   outs() << "           isa " << format("0x%" PRIx64, c.isa);
4562   name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
4563                        isa_n_value, c.isa);
4564   if (name != nullptr)
4565     outs() << " " << name;
4566   outs() << "\n";
4567
4568   outs() << "    superclass " << format("0x%" PRIx64, c.superclass);
4569   name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
4570                        n_value, c.superclass);
4571   if (name != nullptr)
4572     outs() << " " << name;
4573   outs() << "\n";
4574
4575   outs() << "         cache " << format("0x%" PRIx64, c.cache);
4576   name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
4577                        n_value, c.cache);
4578   if (name != nullptr)
4579     outs() << " " << name;
4580   outs() << "\n";
4581
4582   outs() << "        vtable " << format("0x%" PRIx64, c.vtable);
4583   name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
4584                        n_value, c.vtable);
4585   if (name != nullptr)
4586     outs() << " " << name;
4587   outs() << "\n";
4588
4589   name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
4590                        n_value, c.data);
4591   outs() << "          data ";
4592   if (n_value != 0) {
4593     if (info->verbose && name != nullptr)
4594       outs() << name;
4595     else
4596       outs() << format("0x%" PRIx64, n_value);
4597     if (c.data != 0)
4598       outs() << " + " << format("0x%" PRIx64, c.data);
4599   } else
4600     outs() << format("0x%" PRIx64, c.data);
4601   outs() << " (struct class_ro_t *)";
4602
4603   // This is a Swift class if some of the low bits of the pointer are set.
4604   if ((c.data + n_value) & 0x7)
4605     outs() << " Swift class";
4606   outs() << "\n";
4607   bool is_meta_class;
4608   if (!print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class))
4609     return;
4610
4611   if (!is_meta_class &&
4612       c.isa + isa_n_value != p &&
4613       c.isa + isa_n_value != 0 &&
4614       info->depth < 100) {
4615       info->depth++;
4616       outs() << "Meta Class\n";
4617       print_class64_t(c.isa + isa_n_value, info);
4618   }
4619 }
4620
4621 static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
4622   struct class32_t c;
4623   const char *r;
4624   uint32_t offset, left;
4625   SectionRef S;
4626   const char *name;
4627
4628   r = get_pointer_32(p, offset, left, S, info);
4629   if (r == nullptr)
4630     return;
4631   memset(&c, '\0', sizeof(struct class32_t));
4632   if (left < sizeof(struct class32_t)) {
4633     memcpy(&c, r, left);
4634     outs() << "   (class_t entends past the end of the section)\n";
4635   } else
4636     memcpy(&c, r, sizeof(struct class32_t));
4637   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4638     swapStruct(c);
4639
4640   outs() << "           isa " << format("0x%" PRIx32, c.isa);
4641   name =
4642       get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
4643   if (name != nullptr)
4644     outs() << " " << name;
4645   outs() << "\n";
4646
4647   outs() << "    superclass " << format("0x%" PRIx32, c.superclass);
4648   name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
4649                        c.superclass);
4650   if (name != nullptr)
4651     outs() << " " << name;
4652   outs() << "\n";
4653
4654   outs() << "         cache " << format("0x%" PRIx32, c.cache);
4655   name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
4656                        c.cache);
4657   if (name != nullptr)
4658     outs() << " " << name;
4659   outs() << "\n";
4660
4661   outs() << "        vtable " << format("0x%" PRIx32, c.vtable);
4662   name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
4663                        c.vtable);
4664   if (name != nullptr)
4665     outs() << " " << name;
4666   outs() << "\n";
4667
4668   name =
4669       get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
4670   outs() << "          data " << format("0x%" PRIx32, c.data)
4671          << " (struct class_ro_t *)";
4672
4673   // This is a Swift class if some of the low bits of the pointer are set.
4674   if (c.data & 0x3)
4675     outs() << " Swift class";
4676   outs() << "\n";
4677   bool is_meta_class;
4678   if (!print_class_ro32_t(c.data & ~0x3, info, is_meta_class))
4679     return;
4680
4681   if (!is_meta_class) {
4682     outs() << "Meta Class\n";
4683     print_class32_t(c.isa, info);
4684   }
4685 }
4686
4687 static void print_objc_class_t(struct objc_class_t *objc_class,
4688                                struct DisassembleInfo *info) {
4689   uint32_t offset, left, xleft;
4690   const char *name, *p, *ivar_list;
4691   SectionRef S;
4692   int32_t i;
4693   struct objc_ivar_list_t objc_ivar_list;
4694   struct objc_ivar_t ivar;
4695
4696   outs() << "\t\t      isa " << format("0x%08" PRIx32, objc_class->isa);
4697   if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
4698     name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
4699     if (name != nullptr)
4700       outs() << format(" %.*s", left, name);
4701     else
4702       outs() << " (not in an __OBJC section)";
4703   }
4704   outs() << "\n";
4705
4706   outs() << "\t      super_class "
4707          << format("0x%08" PRIx32, objc_class->super_class);
4708   if (info->verbose) {
4709     name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
4710     if (name != nullptr)
4711       outs() << format(" %.*s", left, name);
4712     else
4713       outs() << " (not in an __OBJC section)";
4714   }
4715   outs() << "\n";
4716
4717   outs() << "\t\t     name " << format("0x%08" PRIx32, objc_class->name);
4718   if (info->verbose) {
4719     name = get_pointer_32(objc_class->name, offset, left, S, info, true);
4720     if (name != nullptr)
4721       outs() << format(" %.*s", left, name);
4722     else
4723       outs() << " (not in an __OBJC section)";
4724   }
4725   outs() << "\n";
4726
4727   outs() << "\t\t  version " << format("0x%08" PRIx32, objc_class->version)
4728          << "\n";
4729
4730   outs() << "\t\t     info " << format("0x%08" PRIx32, objc_class->info);
4731   if (info->verbose) {
4732     if (CLS_GETINFO(objc_class, CLS_CLASS))
4733       outs() << " CLS_CLASS";
4734     else if (CLS_GETINFO(objc_class, CLS_META))
4735       outs() << " CLS_META";
4736   }
4737   outs() << "\n";
4738
4739   outs() << "\t    instance_size "
4740          << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
4741
4742   p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
4743   outs() << "\t\t    ivars " << format("0x%08" PRIx32, objc_class->ivars);
4744   if (p != nullptr) {
4745     if (left > sizeof(struct objc_ivar_list_t)) {
4746       outs() << "\n";
4747       memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
4748     } else {
4749       outs() << " (entends past the end of the section)\n";
4750       memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
4751       memcpy(&objc_ivar_list, p, left);
4752     }
4753     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4754       swapStruct(objc_ivar_list);
4755     outs() << "\t\t       ivar_count " << objc_ivar_list.ivar_count << "\n";
4756     ivar_list = p + sizeof(struct objc_ivar_list_t);
4757     for (i = 0; i < objc_ivar_list.ivar_count; i++) {
4758       if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
4759         outs() << "\t\t remaining ivar's extend past the of the section\n";
4760         break;
4761       }
4762       memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
4763              sizeof(struct objc_ivar_t));
4764       if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4765         swapStruct(ivar);
4766
4767       outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
4768       if (info->verbose) {
4769         name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
4770         if (name != nullptr)
4771           outs() << format(" %.*s", xleft, name);
4772         else
4773           outs() << " (not in an __OBJC section)";
4774       }
4775       outs() << "\n";
4776
4777       outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
4778       if (info->verbose) {
4779         name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
4780         if (name != nullptr)
4781           outs() << format(" %.*s", xleft, name);
4782         else
4783           outs() << " (not in an __OBJC section)";
4784       }
4785       outs() << "\n";
4786
4787       outs() << "\t\t      ivar_offset "
4788              << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
4789     }
4790   } else {
4791     outs() << " (not in an __OBJC section)\n";
4792   }
4793
4794   outs() << "\t\t  methods " << format("0x%08" PRIx32, objc_class->methodLists);
4795   if (print_method_list(objc_class->methodLists, info))
4796     outs() << " (not in an __OBJC section)\n";
4797
4798   outs() << "\t\t    cache " << format("0x%08" PRIx32, objc_class->cache)
4799          << "\n";
4800
4801   outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
4802   if (print_protocol_list(objc_class->protocols, 16, info))
4803     outs() << " (not in an __OBJC section)\n";
4804 }
4805
4806 static void print_objc_objc_category_t(struct objc_category_t *objc_category,
4807                                        struct DisassembleInfo *info) {
4808   uint32_t offset, left;
4809   const char *name;
4810   SectionRef S;
4811
4812   outs() << "\t       category name "
4813          << format("0x%08" PRIx32, objc_category->category_name);
4814   if (info->verbose) {
4815     name = get_pointer_32(objc_category->category_name, offset, left, S, info,
4816                           true);
4817     if (name != nullptr)
4818       outs() << format(" %.*s", left, name);
4819     else
4820       outs() << " (not in an __OBJC section)";
4821   }
4822   outs() << "\n";
4823
4824   outs() << "\t\t  class name "
4825          << format("0x%08" PRIx32, objc_category->class_name);
4826   if (info->verbose) {
4827     name =
4828         get_pointer_32(objc_category->class_name, offset, left, S, info, true);
4829     if (name != nullptr)
4830       outs() << format(" %.*s", left, name);
4831     else
4832       outs() << " (not in an __OBJC section)";
4833   }
4834   outs() << "\n";
4835
4836   outs() << "\t    instance methods "
4837          << format("0x%08" PRIx32, objc_category->instance_methods);
4838   if (print_method_list(objc_category->instance_methods, info))
4839     outs() << " (not in an __OBJC section)\n";
4840
4841   outs() << "\t       class methods "
4842          << format("0x%08" PRIx32, objc_category->class_methods);
4843   if (print_method_list(objc_category->class_methods, info))
4844     outs() << " (not in an __OBJC section)\n";
4845 }
4846
4847 static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
4848   struct category64_t c;
4849   const char *r;
4850   uint32_t offset, xoffset, left;
4851   SectionRef S, xS;
4852   const char *name, *sym_name;
4853   uint64_t n_value;
4854
4855   r = get_pointer_64(p, offset, left, S, info);
4856   if (r == nullptr)
4857     return;
4858   memset(&c, '\0', sizeof(struct category64_t));
4859   if (left < sizeof(struct category64_t)) {
4860     memcpy(&c, r, left);
4861     outs() << "   (category_t entends past the end of the section)\n";
4862   } else
4863     memcpy(&c, r, sizeof(struct category64_t));
4864   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4865     swapStruct(c);
4866
4867   outs() << "              name ";
4868   sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
4869                            info, n_value, c.name);
4870   if (n_value != 0) {
4871     if (info->verbose && sym_name != nullptr)
4872       outs() << sym_name;
4873     else
4874       outs() << format("0x%" PRIx64, n_value);
4875     if (c.name != 0)
4876       outs() << " + " << format("0x%" PRIx64, c.name);
4877   } else
4878     outs() << format("0x%" PRIx64, c.name);
4879   name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
4880   if (name != nullptr)
4881     outs() << format(" %.*s", left, name);
4882   outs() << "\n";
4883
4884   outs() << "               cls ";
4885   sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
4886                            n_value, c.cls);
4887   if (n_value != 0) {
4888     if (info->verbose && sym_name != nullptr)
4889       outs() << sym_name;
4890     else
4891       outs() << format("0x%" PRIx64, n_value);
4892     if (c.cls != 0)
4893       outs() << " + " << format("0x%" PRIx64, c.cls);
4894   } else
4895     outs() << format("0x%" PRIx64, c.cls);
4896   outs() << "\n";
4897   if (c.cls + n_value != 0)
4898     print_class64_t(c.cls + n_value, info);
4899
4900   outs() << "   instanceMethods ";
4901   sym_name =
4902       get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
4903                     info, n_value, c.instanceMethods);
4904   if (n_value != 0) {
4905     if (info->verbose && sym_name != nullptr)
4906       outs() << sym_name;
4907     else
4908       outs() << format("0x%" PRIx64, n_value);
4909     if (c.instanceMethods != 0)
4910       outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
4911   } else
4912     outs() << format("0x%" PRIx64, c.instanceMethods);
4913   outs() << "\n";
4914   if (c.instanceMethods + n_value != 0)
4915     print_method_list64_t(c.instanceMethods + n_value, info, "");
4916
4917   outs() << "      classMethods ";
4918   sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
4919                            S, info, n_value, c.classMethods);
4920   if (n_value != 0) {
4921     if (info->verbose && sym_name != nullptr)
4922       outs() << sym_name;
4923     else
4924       outs() << format("0x%" PRIx64, n_value);
4925     if (c.classMethods != 0)
4926       outs() << " + " << format("0x%" PRIx64, c.classMethods);
4927   } else
4928     outs() << format("0x%" PRIx64, c.classMethods);
4929   outs() << "\n";
4930   if (c.classMethods + n_value != 0)
4931     print_method_list64_t(c.classMethods + n_value, info, "");
4932
4933   outs() << "         protocols ";
4934   sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
4935                            info, n_value, c.protocols);
4936   if (n_value != 0) {
4937     if (info->verbose && sym_name != nullptr)
4938       outs() << sym_name;
4939     else
4940       outs() << format("0x%" PRIx64, n_value);
4941     if (c.protocols != 0)
4942       outs() << " + " << format("0x%" PRIx64, c.protocols);
4943   } else
4944     outs() << format("0x%" PRIx64, c.protocols);
4945   outs() << "\n";
4946   if (c.protocols + n_value != 0)
4947     print_protocol_list64_t(c.protocols + n_value, info);
4948
4949   outs() << "instanceProperties ";
4950   sym_name =
4951       get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
4952                     S, info, n_value, c.instanceProperties);
4953   if (n_value != 0) {
4954     if (info->verbose && sym_name != nullptr)
4955       outs() << sym_name;
4956     else
4957       outs() << format("0x%" PRIx64, n_value);
4958     if (c.instanceProperties != 0)
4959       outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
4960   } else
4961     outs() << format("0x%" PRIx64, c.instanceProperties);
4962   outs() << "\n";
4963   if (c.instanceProperties + n_value != 0)
4964     print_objc_property_list64(c.instanceProperties + n_value, info);
4965 }
4966
4967 static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
4968   struct category32_t c;
4969   const char *r;
4970   uint32_t offset, left;
4971   SectionRef S, xS;
4972   const char *name;
4973
4974   r = get_pointer_32(p, offset, left, S, info);
4975   if (r == nullptr)
4976     return;
4977   memset(&c, '\0', sizeof(struct category32_t));
4978   if (left < sizeof(struct category32_t)) {
4979     memcpy(&c, r, left);
4980     outs() << "   (category_t entends past the end of the section)\n";
4981   } else
4982     memcpy(&c, r, sizeof(struct category32_t));
4983   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4984     swapStruct(c);
4985
4986   outs() << "              name " << format("0x%" PRIx32, c.name);
4987   name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
4988                        c.name);
4989   if (name)
4990     outs() << " " << name;
4991   outs() << "\n";
4992
4993   outs() << "               cls " << format("0x%" PRIx32, c.cls) << "\n";
4994   if (c.cls != 0)
4995     print_class32_t(c.cls, info);
4996   outs() << "   instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
4997          << "\n";
4998   if (c.instanceMethods != 0)
4999     print_method_list32_t(c.instanceMethods, info, "");
5000   outs() << "      classMethods " << format("0x%" PRIx32, c.classMethods)
5001          << "\n";
5002   if (c.classMethods != 0)
5003     print_method_list32_t(c.classMethods, info, "");
5004   outs() << "         protocols " << format("0x%" PRIx32, c.protocols) << "\n";
5005   if (c.protocols != 0)
5006     print_protocol_list32_t(c.protocols, info);
5007   outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
5008          << "\n";
5009   if (c.instanceProperties != 0)
5010     print_objc_property_list32(c.instanceProperties, info);
5011 }
5012
5013 static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
5014   uint32_t i, left, offset, xoffset;
5015   uint64_t p, n_value;
5016   struct message_ref64 mr;
5017   const char *name, *sym_name;
5018   const char *r;
5019   SectionRef xS;
5020
5021   if (S == SectionRef())
5022     return;
5023
5024   StringRef SectName;
5025   S.getName(SectName);
5026   DataRefImpl Ref = S.getRawDataRefImpl();
5027   StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5028   outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5029   offset = 0;
5030   for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5031     p = S.getAddress() + i;
5032     r = get_pointer_64(p, offset, left, S, info);
5033     if (r == nullptr)
5034       return;
5035     memset(&mr, '\0', sizeof(struct message_ref64));
5036     if (left < sizeof(struct message_ref64)) {
5037       memcpy(&mr, r, left);
5038       outs() << "   (message_ref entends past the end of the section)\n";
5039     } else
5040       memcpy(&mr, r, sizeof(struct message_ref64));
5041     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5042       swapStruct(mr);
5043
5044     outs() << "  imp ";
5045     name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
5046                          n_value, mr.imp);
5047     if (n_value != 0) {
5048       outs() << format("0x%" PRIx64, n_value) << " ";
5049       if (mr.imp != 0)
5050         outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
5051     } else
5052       outs() << format("0x%" PRIx64, mr.imp) << " ";
5053     if (name != nullptr)
5054       outs() << " " << name;
5055     outs() << "\n";
5056
5057     outs() << "  sel ";
5058     sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
5059                              info, n_value, mr.sel);
5060     if (n_value != 0) {
5061       if (info->verbose && sym_name != nullptr)
5062         outs() << sym_name;
5063       else
5064         outs() << format("0x%" PRIx64, n_value);
5065       if (mr.sel != 0)
5066         outs() << " + " << format("0x%" PRIx64, mr.sel);
5067     } else
5068       outs() << format("0x%" PRIx64, mr.sel);
5069     name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
5070     if (name != nullptr)
5071       outs() << format(" %.*s", left, name);
5072     outs() << "\n";
5073
5074     offset += sizeof(struct message_ref64);
5075   }
5076 }
5077
5078 static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
5079   uint32_t i, left, offset, xoffset, p;
5080   struct message_ref32 mr;
5081   const char *name, *r;
5082   SectionRef xS;
5083
5084   if (S == SectionRef())
5085     return;
5086
5087   StringRef SectName;
5088   S.getName(SectName);
5089   DataRefImpl Ref = S.getRawDataRefImpl();
5090   StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5091   outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5092   offset = 0;
5093   for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5094     p = S.getAddress() + i;
5095     r = get_pointer_32(p, offset, left, S, info);
5096     if (r == nullptr)
5097       return;
5098     memset(&mr, '\0', sizeof(struct message_ref32));
5099     if (left < sizeof(struct message_ref32)) {
5100       memcpy(&mr, r, left);
5101       outs() << "   (message_ref entends past the end of the section)\n";
5102     } else
5103       memcpy(&mr, r, sizeof(struct message_ref32));
5104     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5105       swapStruct(mr);
5106
5107     outs() << "  imp " << format("0x%" PRIx32, mr.imp);
5108     name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
5109                          mr.imp);
5110     if (name != nullptr)
5111       outs() << " " << name;
5112     outs() << "\n";
5113
5114     outs() << "  sel " << format("0x%" PRIx32, mr.sel);
5115     name = get_pointer_32(mr.sel, xoffset, left, xS, info);
5116     if (name != nullptr)
5117       outs() << " " << name;
5118     outs() << "\n";
5119
5120     offset += sizeof(struct message_ref32);
5121   }
5122 }
5123
5124 static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
5125   uint32_t left, offset, swift_version;
5126   uint64_t p;
5127   struct objc_image_info64 o;
5128   const char *r;
5129
5130   if (S == SectionRef())
5131     return;
5132
5133   StringRef SectName;
5134   S.getName(SectName);
5135   DataRefImpl Ref = S.getRawDataRefImpl();
5136   StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5137   outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5138   p = S.getAddress();
5139   r = get_pointer_64(p, offset, left, S, info);
5140   if (r == nullptr)
5141     return;
5142   memset(&o, '\0', sizeof(struct objc_image_info64));
5143   if (left < sizeof(struct objc_image_info64)) {
5144     memcpy(&o, r, left);
5145     outs() << "   (objc_image_info entends past the end of the section)\n";
5146   } else
5147     memcpy(&o, r, sizeof(struct objc_image_info64));
5148   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5149     swapStruct(o);
5150   outs() << "  version " << o.version << "\n";
5151   outs() << "    flags " << format("0x%" PRIx32, o.flags);
5152   if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5153     outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5154   if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5155     outs() << " OBJC_IMAGE_SUPPORTS_GC";
5156   swift_version = (o.flags >> 8) & 0xff;
5157   if (swift_version != 0) {
5158     if (swift_version == 1)
5159       outs() << " Swift 1.0";
5160     else if (swift_version == 2)
5161       outs() << " Swift 1.1";
5162     else
5163       outs() << " unknown future Swift version (" << swift_version << ")";
5164   }
5165   outs() << "\n";
5166 }
5167
5168 static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5169   uint32_t left, offset, swift_version, p;
5170   struct objc_image_info32 o;
5171   const char *r;
5172
5173   if (S == SectionRef())
5174     return;
5175
5176   StringRef SectName;
5177   S.getName(SectName);
5178   DataRefImpl Ref = S.getRawDataRefImpl();
5179   StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5180   outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5181   p = S.getAddress();
5182   r = get_pointer_32(p, offset, left, S, info);
5183   if (r == nullptr)
5184     return;
5185   memset(&o, '\0', sizeof(struct objc_image_info32));
5186   if (left < sizeof(struct objc_image_info32)) {
5187     memcpy(&o, r, left);
5188     outs() << "   (objc_image_info entends past the end of the section)\n";
5189   } else
5190     memcpy(&o, r, sizeof(struct objc_image_info32));
5191   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5192     swapStruct(o);
5193   outs() << "  version " << o.version << "\n";
5194   outs() << "    flags " << format("0x%" PRIx32, o.flags);
5195   if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5196     outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5197   if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5198     outs() << " OBJC_IMAGE_SUPPORTS_GC";
5199   swift_version = (o.flags >> 8) & 0xff;
5200   if (swift_version != 0) {
5201     if (swift_version == 1)
5202       outs() << " Swift 1.0";
5203     else if (swift_version == 2)
5204       outs() << " Swift 1.1";
5205     else
5206       outs() << " unknown future Swift version (" << swift_version << ")";
5207   }
5208   outs() << "\n";
5209 }
5210
5211 static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5212   uint32_t left, offset, p;
5213   struct imageInfo_t o;
5214   const char *r;
5215
5216   StringRef SectName;
5217   S.getName(SectName);
5218   DataRefImpl Ref = S.getRawDataRefImpl();
5219   StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5220   outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5221   p = S.getAddress();
5222   r = get_pointer_32(p, offset, left, S, info);
5223   if (r == nullptr)
5224     return;
5225   memset(&o, '\0', sizeof(struct imageInfo_t));
5226   if (left < sizeof(struct imageInfo_t)) {
5227     memcpy(&o, r, left);
5228     outs() << " (imageInfo entends past the end of the section)\n";
5229   } else
5230     memcpy(&o, r, sizeof(struct imageInfo_t));
5231   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5232     swapStruct(o);
5233   outs() << "  version " << o.version << "\n";
5234   outs() << "    flags " << format("0x%" PRIx32, o.flags);
5235   if (o.flags & 0x1)
5236     outs() << "  F&C";
5237   if (o.flags & 0x2)
5238     outs() << " GC";
5239   if (o.flags & 0x4)
5240     outs() << " GC-only";
5241   else
5242     outs() << " RR";
5243   outs() << "\n";
5244 }
5245
5246 static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
5247   SymbolAddressMap AddrMap;
5248   if (verbose)
5249     CreateSymbolAddressMap(O, &AddrMap);
5250
5251   std::vector<SectionRef> Sections;
5252   for (const SectionRef &Section : O->sections()) {
5253     StringRef SectName;
5254     Section.getName(SectName);
5255     Sections.push_back(Section);
5256   }
5257
5258   struct DisassembleInfo info;
5259   // Set up the block of info used by the Symbolizer call backs.
5260   info.verbose = verbose;
5261   info.O = O;
5262   info.AddrMap = &AddrMap;
5263   info.Sections = &Sections;
5264   info.class_name = nullptr;
5265   info.selector_name = nullptr;
5266   info.method = nullptr;
5267   info.demangled_name = nullptr;
5268   info.bindtable = nullptr;
5269   info.adrp_addr = 0;
5270   info.adrp_inst = 0;
5271
5272   info.depth = 0;
5273   SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5274   if (CL == SectionRef())
5275     CL = get_section(O, "__DATA", "__objc_classlist");
5276   info.S = CL;
5277   walk_pointer_list_64("class", CL, O, &info, print_class64_t);
5278
5279   SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5280   if (CR == SectionRef())
5281     CR = get_section(O, "__DATA", "__objc_classrefs");
5282   info.S = CR;
5283   walk_pointer_list_64("class refs", CR, O, &info, nullptr);
5284
5285   SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5286   if (SR == SectionRef())
5287     SR = get_section(O, "__DATA", "__objc_superrefs");
5288   info.S = SR;
5289   walk_pointer_list_64("super refs", SR, O, &info, nullptr);
5290
5291   SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5292   if (CA == SectionRef())
5293     CA = get_section(O, "__DATA", "__objc_catlist");
5294   info.S = CA;
5295   walk_pointer_list_64("category", CA, O, &info, print_category64_t);
5296
5297   SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5298   if (PL == SectionRef())
5299     PL = get_section(O, "__DATA", "__objc_protolist");
5300   info.S = PL;
5301   walk_pointer_list_64("protocol", PL, O, &info, nullptr);
5302
5303   SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5304   if (MR == SectionRef())
5305     MR = get_section(O, "__DATA", "__objc_msgrefs");
5306   info.S = MR;
5307   print_message_refs64(MR, &info);
5308
5309   SectionRef II = get_section(O, "__OBJC2", "__image_info");
5310   if (II == SectionRef())
5311     II = get_section(O, "__DATA", "__objc_imageinfo");
5312   info.S = II;
5313   print_image_info64(II, &info);
5314
5315   if (info.bindtable != nullptr)
5316     delete info.bindtable;
5317 }
5318
5319 static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
5320   SymbolAddressMap AddrMap;
5321   if (verbose)
5322     CreateSymbolAddressMap(O, &AddrMap);
5323
5324   std::vector<SectionRef> Sections;
5325   for (const SectionRef &Section : O->sections()) {
5326     StringRef SectName;
5327     Section.getName(SectName);
5328     Sections.push_back(Section);
5329   }
5330
5331   struct DisassembleInfo info;
5332   // Set up the block of info used by the Symbolizer call backs.
5333   info.verbose = verbose;
5334   info.O = O;
5335   info.AddrMap = &AddrMap;
5336   info.Sections = &Sections;
5337   info.class_name = nullptr;
5338   info.selector_name = nullptr;
5339   info.method = nullptr;
5340   info.demangled_name = nullptr;
5341   info.bindtable = nullptr;
5342   info.adrp_addr = 0;
5343   info.adrp_inst = 0;
5344
5345   const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5346   if (CL != SectionRef()) {
5347     info.S = CL;
5348     walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5349   } else {
5350     const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5351     info.S = CL;
5352     walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5353   }
5354
5355   const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5356   if (CR != SectionRef()) {
5357     info.S = CR;
5358     walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5359   } else {
5360     const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5361     info.S = CR;
5362     walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5363   }
5364
5365   const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5366   if (SR != SectionRef()) {
5367     info.S = SR;
5368     walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5369   } else {
5370     const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5371     info.S = SR;
5372     walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5373   }
5374
5375   const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5376   if (CA != SectionRef()) {
5377     info.S = CA;
5378     walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5379   } else {
5380     const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5381     info.S = CA;
5382     walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5383   }
5384
5385   const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5386   if (PL != SectionRef()) {
5387     info.S = PL;
5388     walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5389   } else {
5390     const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5391     info.S = PL;
5392     walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5393   }
5394
5395   const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5396   if (MR != SectionRef()) {
5397     info.S = MR;
5398     print_message_refs32(MR, &info);
5399   } else {
5400     const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5401     info.S = MR;
5402     print_message_refs32(MR, &info);
5403   }
5404
5405   const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5406   if (II != SectionRef()) {
5407     info.S = II;
5408     print_image_info32(II, &info);
5409   } else {
5410     const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5411     info.S = II;
5412     print_image_info32(II, &info);
5413   }
5414 }
5415
5416 static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
5417   uint32_t i, j, p, offset, xoffset, left, defs_left, def;
5418   const char *r, *name, *defs;
5419   struct objc_module_t module;
5420   SectionRef S, xS;
5421   struct objc_symtab_t symtab;
5422   struct objc_class_t objc_class;
5423   struct objc_category_t objc_category;
5424
5425   outs() << "Objective-C segment\n";
5426   S = get_section(O, "__OBJC", "__module_info");
5427   if (S == SectionRef())
5428     return false;
5429
5430   SymbolAddressMap AddrMap;
5431   if (verbose)
5432     CreateSymbolAddressMap(O, &AddrMap);
5433
5434   std::vector<SectionRef> Sections;
5435   for (const SectionRef &Section : O->sections()) {
5436     StringRef SectName;
5437     Section.getName(SectName);
5438     Sections.push_back(Section);
5439   }
5440
5441   struct DisassembleInfo info;
5442   // Set up the block of info used by the Symbolizer call backs.
5443   info.verbose = verbose;
5444   info.O = O;
5445   info.AddrMap = &AddrMap;
5446   info.Sections = &Sections;
5447   info.class_name = nullptr;
5448   info.selector_name = nullptr;
5449   info.method = nullptr;
5450   info.demangled_name = nullptr;
5451   info.bindtable = nullptr;
5452   info.adrp_addr = 0;
5453   info.adrp_inst = 0;
5454
5455   for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
5456     p = S.getAddress() + i;
5457     r = get_pointer_32(p, offset, left, S, &info, true);
5458     if (r == nullptr)
5459       return true;
5460     memset(&module, '\0', sizeof(struct objc_module_t));
5461     if (left < sizeof(struct objc_module_t)) {
5462       memcpy(&module, r, left);
5463       outs() << "   (module extends past end of __module_info section)\n";
5464     } else
5465       memcpy(&module, r, sizeof(struct objc_module_t));
5466     if (O->isLittleEndian() != sys::IsLittleEndianHost)
5467       swapStruct(module);
5468
5469     outs() << "Module " << format("0x%" PRIx32, p) << "\n";
5470     outs() << "    version " << module.version << "\n";
5471     outs() << "       size " << module.size << "\n";
5472     outs() << "       name ";
5473     name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
5474     if (name != nullptr)
5475       outs() << format("%.*s", left, name);
5476     else
5477       outs() << format("0x%08" PRIx32, module.name)
5478              << "(not in an __OBJC section)";
5479     outs() << "\n";
5480
5481     r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
5482     if (module.symtab == 0 || r == nullptr) {
5483       outs() << "     symtab " << format("0x%08" PRIx32, module.symtab)
5484              << " (not in an __OBJC section)\n";
5485       continue;
5486     }
5487     outs() << "     symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
5488     memset(&symtab, '\0', sizeof(struct objc_symtab_t));
5489     defs_left = 0;
5490     defs = nullptr;
5491     if (left < sizeof(struct objc_symtab_t)) {
5492       memcpy(&symtab, r, left);
5493       outs() << "\tsymtab extends past end of an __OBJC section)\n";
5494     } else {
5495       memcpy(&symtab, r, sizeof(struct objc_symtab_t));
5496       if (left > sizeof(struct objc_symtab_t)) {
5497         defs_left = left - sizeof(struct objc_symtab_t);
5498         defs = r + sizeof(struct objc_symtab_t);
5499       }
5500     }
5501     if (O->isLittleEndian() != sys::IsLittleEndianHost)
5502       swapStruct(symtab);
5503
5504     outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
5505     r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
5506     outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
5507     if (r == nullptr)
5508       outs() << " (not in an __OBJC section)";
5509     outs() << "\n";
5510     outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
5511     outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
5512     if (symtab.cls_def_cnt > 0)
5513       outs() << "\tClass Definitions\n";
5514     for (j = 0; j < symtab.cls_def_cnt; j++) {
5515       if ((j + 1) * sizeof(uint32_t) > defs_left) {
5516         outs() << "\t(remaining class defs entries entends past the end of the "
5517                << "section)\n";
5518         break;
5519       }
5520       memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
5521       if (O->isLittleEndian() != sys::IsLittleEndianHost)
5522         sys::swapByteOrder(def);
5523
5524       r = get_pointer_32(def, xoffset, left, xS, &info, true);
5525       outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
5526       if (r != nullptr) {
5527         if (left > sizeof(struct objc_class_t)) {
5528           outs() << "\n";
5529           memcpy(&objc_class, r, sizeof(struct objc_class_t));
5530         } else {
5531           outs() << " (entends past the end of the section)\n";
5532           memset(&objc_class, '\0', sizeof(struct objc_class_t));
5533           memcpy(&objc_class, r, left);
5534         }
5535         if (O->isLittleEndian() != sys::IsLittleEndianHost)
5536           swapStruct(objc_class);
5537         print_objc_class_t(&objc_class, &info);
5538       } else {
5539         outs() << "(not in an __OBJC section)\n";
5540       }
5541
5542       if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
5543         outs() << "\tMeta Class";
5544         r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
5545         if (r != nullptr) {
5546           if (left > sizeof(struct objc_class_t)) {
5547             outs() << "\n";
5548             memcpy(&objc_class, r, sizeof(struct objc_class_t));
5549           } else {
5550             outs() << " (entends past the end of the section)\n";
5551             memset(&objc_class, '\0', sizeof(struct objc_class_t));
5552             memcpy(&objc_class, r, left);
5553           }
5554           if (O->isLittleEndian() != sys::IsLittleEndianHost)
5555             swapStruct(objc_class);
5556           print_objc_class_t(&objc_class, &info);
5557         } else {
5558           outs() << "(not in an __OBJC section)\n";
5559         }
5560       }
5561     }
5562     if (symtab.cat_def_cnt > 0)
5563       outs() << "\tCategory Definitions\n";
5564     for (j = 0; j < symtab.cat_def_cnt; j++) {
5565       if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
5566         outs() << "\t(remaining category defs entries entends past the end of "
5567                << "the section)\n";
5568         break;
5569       }
5570       memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
5571              sizeof(uint32_t));
5572       if (O->isLittleEndian() != sys::IsLittleEndianHost)
5573         sys::swapByteOrder(def);
5574
5575       r = get_pointer_32(def, xoffset, left, xS, &info, true);
5576       outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
5577              << format("0x%08" PRIx32, def);
5578       if (r != nullptr) {
5579         if (left > sizeof(struct objc_category_t)) {
5580           outs() << "\n";
5581           memcpy(&objc_category, r, sizeof(struct objc_category_t));
5582         } else {
5583           outs() << " (entends past the end of the section)\n";
5584           memset(&objc_category, '\0', sizeof(struct objc_category_t));
5585           memcpy(&objc_category, r, left);
5586         }
5587         if (O->isLittleEndian() != sys::IsLittleEndianHost)
5588           swapStruct(objc_category);
5589         print_objc_objc_category_t(&objc_category, &info);
5590       } else {
5591         outs() << "(not in an __OBJC section)\n";
5592       }
5593     }
5594   }
5595   const SectionRef II = get_section(O, "__OBJC", "__image_info");
5596   if (II != SectionRef())
5597     print_image_info(II, &info);
5598
5599   return true;
5600 }
5601
5602 static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
5603                                 uint32_t size, uint32_t addr) {
5604   SymbolAddressMap AddrMap;
5605   CreateSymbolAddressMap(O, &AddrMap);
5606
5607   std::vector<SectionRef> Sections;
5608   for (const SectionRef &Section : O->sections()) {
5609     StringRef SectName;
5610     Section.getName(SectName);
5611     Sections.push_back(Section);
5612   }
5613
5614   struct DisassembleInfo info;
5615   // Set up the block of info used by the Symbolizer call backs.
5616   info.verbose = true;
5617   info.O = O;
5618   info.AddrMap = &AddrMap;
5619   info.Sections = &Sections;
5620   info.class_name = nullptr;
5621   info.selector_name = nullptr;
5622   info.method = nullptr;
5623   info.demangled_name = nullptr;
5624   info.bindtable = nullptr;
5625   info.adrp_addr = 0;
5626   info.adrp_inst = 0;
5627
5628   const char *p;
5629   struct objc_protocol_t protocol;
5630   uint32_t left, paddr;
5631   for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
5632     memset(&protocol, '\0', sizeof(struct objc_protocol_t));
5633     left = size - (p - sect);
5634     if (left < sizeof(struct objc_protocol_t)) {
5635       outs() << "Protocol extends past end of __protocol section\n";
5636       memcpy(&protocol, p, left);
5637     } else
5638       memcpy(&protocol, p, sizeof(struct objc_protocol_t));
5639     if (O->isLittleEndian() != sys::IsLittleEndianHost)
5640       swapStruct(protocol);
5641     paddr = addr + (p - sect);
5642     outs() << "Protocol " << format("0x%" PRIx32, paddr);
5643     if (print_protocol(paddr, 0, &info))
5644       outs() << "(not in an __OBJC section)\n";
5645   }
5646 }
5647
5648 #ifdef HAVE_LIBXAR
5649 inline void swapStruct(struct xar_header &xar) {
5650   sys::swapByteOrder(xar.magic);
5651   sys::swapByteOrder(xar.size);
5652   sys::swapByteOrder(xar.version);
5653   sys::swapByteOrder(xar.toc_length_compressed);
5654   sys::swapByteOrder(xar.toc_length_uncompressed);
5655   sys::swapByteOrder(xar.cksum_alg);
5656 }
5657
5658 static void PrintModeVerbose(uint32_t mode) {
5659   switch(mode & S_IFMT){
5660   case S_IFDIR:
5661     outs() << "d";
5662     break;
5663   case S_IFCHR:
5664     outs() << "c";
5665     break;
5666   case S_IFBLK:
5667     outs() << "b";
5668     break;
5669   case S_IFREG:
5670     outs() << "-";
5671     break;
5672   case S_IFLNK:
5673     outs() << "l";
5674     break;
5675   case S_IFSOCK:
5676     outs() << "s";
5677     break;
5678   default:
5679     outs() << "?";
5680     break;
5681   }
5682
5683   /* owner permissions */
5684   if(mode & S_IREAD)
5685     outs() << "r";
5686   else
5687     outs() << "-";
5688   if(mode & S_IWRITE)
5689     outs() << "w";
5690   else
5691     outs() << "-";
5692   if(mode & S_ISUID)
5693     outs() << "s";
5694   else if(mode & S_IEXEC)
5695     outs() << "x";
5696   else
5697     outs() << "-";
5698
5699   /* group permissions */
5700   if(mode & (S_IREAD >> 3))
5701     outs() << "r";
5702   else
5703     outs() << "-";
5704   if(mode & (S_IWRITE >> 3))
5705     outs() << "w";
5706   else
5707     outs() << "-";
5708   if(mode & S_ISGID)
5709     outs() << "s";
5710   else if(mode & (S_IEXEC >> 3))
5711     outs() << "x";
5712   else
5713     outs() << "-";
5714
5715   /* other permissions */
5716   if(mode & (S_IREAD >> 6))
5717     outs() << "r";
5718   else
5719     outs() << "-";
5720   if(mode & (S_IWRITE >> 6))
5721     outs() << "w";
5722   else
5723     outs() << "-";
5724   if(mode & S_ISVTX)
5725     outs() << "t";
5726   else if(mode & (S_IEXEC >> 6))
5727     outs() << "x";
5728   else
5729     outs() << "-";
5730 }
5731
5732 static void PrintXarFilesSummary(const char *XarFilename, xar_t xar) {
5733   xar_iter_t xi;
5734   xar_file_t xf;
5735   xar_iter_t xp;
5736   const char *key, *type, *mode, *user, *group, *size, *mtime, *name, *m;
5737   char *endp;
5738   uint32_t mode_value;
5739
5740   xi = xar_iter_new();
5741   if (!xi) {
5742     errs() << "Can't obtain an xar iterator for xar archive "
5743            << XarFilename << "\n";
5744     return;
5745   }
5746
5747   // Go through the xar's files.
5748   for (xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)) {
5749     xp = xar_iter_new();
5750     if(!xp){
5751       errs() << "Can't obtain an xar iterator for xar archive "
5752              << XarFilename << "\n";
5753       return;
5754     }
5755     type = nullptr;
5756     mode = nullptr;
5757     user = nullptr;
5758     group = nullptr;
5759     size = nullptr;
5760     mtime = nullptr;
5761     name = nullptr;
5762     for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
5763       const char *val = nullptr; 
5764       xar_prop_get(xf, key, &val);
5765 #if 0 // Useful for debugging.
5766       outs() << "key: " << key << " value: " << val << "\n";
5767 #endif
5768       if(strcmp(key, "type") == 0)
5769         type = val;
5770       if(strcmp(key, "mode") == 0)
5771         mode = val;
5772       if(strcmp(key, "user") == 0)
5773         user = val;
5774       if(strcmp(key, "group") == 0)
5775         group = val;
5776       if(strcmp(key, "data/size") == 0)
5777         size = val;
5778       if(strcmp(key, "mtime") == 0)
5779         mtime = val;
5780       if(strcmp(key, "name") == 0)
5781         name = val;
5782     }
5783     if(mode != nullptr){
5784       mode_value = strtoul(mode, &endp, 8);
5785       if(*endp != '\0')
5786         outs() << "(mode: \"" << mode << "\" contains non-octal chars) ";
5787       if(strcmp(type, "file") == 0)
5788         mode_value |= S_IFREG;
5789       PrintModeVerbose(mode_value);
5790       outs() << " ";
5791     }
5792     if(user != nullptr)
5793       outs() << format("%10s/", user);
5794     if(group != nullptr)
5795       outs() << format("%-10s ", group);
5796     if(size != nullptr)
5797       outs() << format("%7s ", size);
5798     if(mtime != nullptr){
5799       for(m = mtime; *m != 'T' && *m != '\0'; m++)
5800         outs() << *m;
5801       if(*m == 'T')
5802         m++;
5803       outs() << " ";
5804       for( ; *m != 'Z' && *m != '\0'; m++)
5805         outs() << *m;
5806       outs() << " ";
5807     }
5808     if(name != nullptr)
5809       outs() << name;
5810     outs() << "\n";
5811   }
5812 }
5813
5814 static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
5815                                 uint32_t size, bool verbose,
5816                                 bool PrintXarHeader, bool PrintXarFileHeaders,
5817                                 std::string XarMemberName) {
5818   if(size < sizeof(struct xar_header)) {
5819     outs() << "size of (__LLVM,__bundle) section too small (smaller than size "
5820               "of struct xar_header)\n";
5821     return;
5822   }
5823   struct xar_header XarHeader;
5824   memcpy(&XarHeader, sect, sizeof(struct xar_header));
5825   if (sys::IsLittleEndianHost)
5826     swapStruct(XarHeader);
5827   if (PrintXarHeader) {
5828     if (!XarMemberName.empty())
5829       outs() << "In xar member " << XarMemberName << ": ";
5830     else
5831       outs() << "For (__LLVM,__bundle) section: ";
5832     outs() << "xar header\n";
5833     if (XarHeader.magic == XAR_HEADER_MAGIC)
5834       outs() << "                  magic XAR_HEADER_MAGIC\n";
5835     else
5836       outs() << "                  magic "
5837              << format_hex(XarHeader.magic, 10, true)
5838              << " (not XAR_HEADER_MAGIC)\n";
5839     outs() << "                   size " << XarHeader.size << "\n";
5840     outs() << "                version " << XarHeader.version << "\n";
5841     outs() << "  toc_length_compressed " << XarHeader.toc_length_compressed
5842            << "\n";
5843     outs() << "toc_length_uncompressed " << XarHeader.toc_length_uncompressed
5844            << "\n";
5845     outs() << "              cksum_alg ";
5846     switch (XarHeader.cksum_alg) {
5847       case XAR_CKSUM_NONE:
5848         outs() << "XAR_CKSUM_NONE\n";
5849         break;
5850       case XAR_CKSUM_SHA1:
5851         outs() << "XAR_CKSUM_SHA1\n";
5852         break;
5853       case XAR_CKSUM_MD5:
5854         outs() << "XAR_CKSUM_MD5\n";
5855         break;
5856 #ifdef XAR_CKSUM_SHA256
5857       case XAR_CKSUM_SHA256:
5858         outs() << "XAR_CKSUM_SHA256\n";
5859         break;
5860 #endif
5861 #ifdef XAR_CKSUM_SHA512
5862       case XAR_CKSUM_SHA512:
5863         outs() << "XAR_CKSUM_SHA512\n";
5864         break;
5865 #endif
5866       default:
5867         outs() << XarHeader.cksum_alg << "\n";
5868     }
5869   }
5870
5871   SmallString<128> XarFilename;
5872   int FD;
5873   std::error_code XarEC =
5874       sys::fs::createTemporaryFile("llvm-objdump", "xar", FD, XarFilename);
5875   if (XarEC) {
5876     errs() << XarEC.message() << "\n";
5877     return;
5878   }
5879   tool_output_file XarFile(XarFilename, FD);
5880   raw_fd_ostream &XarOut = XarFile.os();
5881   StringRef XarContents(sect, size);
5882   XarOut << XarContents;
5883   XarOut.close();
5884   if (XarOut.has_error())
5885     return;
5886
5887   xar_t xar = xar_open(XarFilename.c_str(), READ);
5888   if (!xar) {
5889     errs() << "Can't create temporary xar archive " << XarFilename << "\n";
5890     return;
5891   }
5892
5893   SmallString<128> TocFilename;
5894   std::error_code TocEC =
5895       sys::fs::createTemporaryFile("llvm-objdump", "toc", TocFilename);
5896   if (TocEC) {
5897     errs() << TocEC.message() << "\n";
5898     return;
5899   }
5900   xar_serialize(xar, TocFilename.c_str());
5901
5902   if (PrintXarFileHeaders) {
5903     if (!XarMemberName.empty())
5904       outs() << "In xar member " << XarMemberName << ": ";
5905     else
5906       outs() << "For (__LLVM,__bundle) section: ";
5907     outs() << "xar archive files:\n";
5908     PrintXarFilesSummary(XarFilename.c_str(), xar);
5909   }
5910
5911   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
5912     MemoryBuffer::getFileOrSTDIN(TocFilename.c_str());
5913   if (std::error_code EC = FileOrErr.getError()) {
5914     errs() << EC.message() << "\n";
5915     return;
5916   }
5917   std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
5918
5919   if (!XarMemberName.empty())
5920     outs() << "In xar member " << XarMemberName << ": ";
5921   else
5922     outs() << "For (__LLVM,__bundle) section: ";
5923   outs() << "xar table of contents:\n";
5924   outs() << Buffer->getBuffer() << "\n";
5925
5926   // TODO: Go through the xar's files.
5927   xar_iter_t xi = xar_iter_new();
5928   if(!xi){
5929     errs() << "Can't obtain an xar iterator for xar archive "
5930            << XarFilename.c_str() << "\n";
5931     xar_close(xar);
5932     return;
5933   }
5934   for(xar_file_t xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)){
5935     const char *key;
5936     xar_iter_t xp;
5937     const char *member_name, *member_type, *member_size_string;
5938     size_t member_size;
5939
5940     xp = xar_iter_new();
5941     if(!xp){
5942       errs() << "Can't obtain an xar iterator for xar archive "
5943              << XarFilename.c_str() << "\n";
5944       xar_close(xar);
5945       return;
5946     }
5947     member_name = NULL;
5948     member_type = NULL;
5949     member_size_string = NULL;
5950     for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
5951       const char *val = nullptr; 
5952       xar_prop_get(xf, key, &val);
5953 #if 0 // Useful for debugging.
5954       outs() << "key: " << key << " value: " << val << "\n";
5955 #endif
5956       if(strcmp(key, "name") == 0)
5957         member_name = val;
5958       if(strcmp(key, "type") == 0)
5959         member_type = val;
5960       if(strcmp(key, "data/size") == 0)
5961         member_size_string = val;
5962     }
5963     /*
5964      * If we find a file with a name, date/size and type properties
5965      * and with the type being "file" see if that is a xar file.
5966      */
5967     if (member_name != NULL && member_type != NULL &&
5968         strcmp(member_type, "file") == 0 &&
5969         member_size_string != NULL){
5970       // Extract the file into a buffer.
5971       char *endptr;
5972       member_size = strtoul(member_size_string, &endptr, 10);
5973       if (*endptr == '\0' && member_size != 0) {
5974         char *buffer = (char *) ::operator new (member_size);
5975         if (xar_extract_tobuffersz(xar, xf, &buffer, &member_size) == 0) {
5976 #if 0 // Useful for debugging.
5977           outs() << "xar member: " << member_name << " extracted\n";
5978 #endif
5979           // Set the XarMemberName we want to see printed in the header.
5980           std::string OldXarMemberName;
5981           // If XarMemberName is already set this is nested. So
5982           // save the old name and create the nested name.
5983           if (!XarMemberName.empty()) {
5984             OldXarMemberName = XarMemberName;
5985             XarMemberName =
5986              (Twine("[") + XarMemberName + "]" + member_name).str();
5987           } else {
5988             OldXarMemberName = "";
5989             XarMemberName = member_name;
5990           }
5991           // See if this is could be a xar file (nested).
5992           if (member_size >= sizeof(struct xar_header)) {
5993 #if 0 // Useful for debugging.
5994             outs() << "could be a xar file: " << member_name << "\n";
5995 #endif
5996             memcpy((char *)&XarHeader, buffer, sizeof(struct xar_header));
5997             if (sys::IsLittleEndianHost)
5998               swapStruct(XarHeader);
5999             if(XarHeader.magic == XAR_HEADER_MAGIC)
6000               DumpBitcodeSection(O, buffer, member_size, verbose,
6001                                  PrintXarHeader, PrintXarFileHeaders,
6002                                  XarMemberName);
6003           }
6004           XarMemberName = OldXarMemberName;
6005         }
6006         delete buffer;
6007       }
6008     }
6009     xar_iter_free(xp);
6010   }
6011   xar_close(xar);
6012 }
6013 #endif // defined(HAVE_LIBXAR)
6014
6015 static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
6016   if (O->is64Bit())
6017     printObjc2_64bit_MetaData(O, verbose);
6018   else {
6019     MachO::mach_header H;
6020     H = O->getHeader();
6021     if (H.cputype == MachO::CPU_TYPE_ARM)
6022       printObjc2_32bit_MetaData(O, verbose);
6023     else {
6024       // This is the 32-bit non-arm cputype case.  Which is normally
6025       // the first Objective-C ABI.  But it may be the case of a
6026       // binary for the iOS simulator which is the second Objective-C
6027       // ABI.  In that case printObjc1_32bit_MetaData() will determine that
6028       // and return false.
6029       if (!printObjc1_32bit_MetaData(O, verbose))
6030         printObjc2_32bit_MetaData(O, verbose);
6031     }
6032   }
6033 }
6034
6035 // GuessLiteralPointer returns a string which for the item in the Mach-O file
6036 // for the address passed in as ReferenceValue for printing as a comment with
6037 // the instruction and also returns the corresponding type of that item
6038 // indirectly through ReferenceType.
6039 //
6040 // If ReferenceValue is an address of literal cstring then a pointer to the
6041 // cstring is returned and ReferenceType is set to
6042 // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
6043 //
6044 // If ReferenceValue is an address of an Objective-C CFString, Selector ref or
6045 // Class ref that name is returned and the ReferenceType is set accordingly.
6046 //
6047 // Lastly, literals which are Symbol address in a literal pool are looked for
6048 // and if found the symbol name is returned and ReferenceType is set to
6049 // LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
6050 //
6051 // If there is no item in the Mach-O file for the address passed in as
6052 // ReferenceValue nullptr is returned and ReferenceType is unchanged.
6053 static const char *GuessLiteralPointer(uint64_t ReferenceValue,
6054                                        uint64_t ReferencePC,
6055                                        uint64_t *ReferenceType,
6056                                        struct DisassembleInfo *info) {
6057   // First see if there is an external relocation entry at the ReferencePC.
6058   if (info->O->getHeader().filetype == MachO::MH_OBJECT) {
6059     uint64_t sect_addr = info->S.getAddress();
6060     uint64_t sect_offset = ReferencePC - sect_addr;
6061     bool reloc_found = false;
6062     DataRefImpl Rel;
6063     MachO::any_relocation_info RE;
6064     bool isExtern = false;
6065     SymbolRef Symbol;
6066     for (const RelocationRef &Reloc : info->S.relocations()) {
6067       uint64_t RelocOffset = Reloc.getOffset();
6068       if (RelocOffset == sect_offset) {
6069         Rel = Reloc.getRawDataRefImpl();
6070         RE = info->O->getRelocation(Rel);
6071         if (info->O->isRelocationScattered(RE))
6072           continue;
6073         isExtern = info->O->getPlainRelocationExternal(RE);
6074         if (isExtern) {
6075           symbol_iterator RelocSym = Reloc.getSymbol();
6076           Symbol = *RelocSym;
6077         }
6078         reloc_found = true;
6079         break;
6080       }
6081     }
6082     // If there is an external relocation entry for a symbol in a section
6083     // then used that symbol's value for the value of the reference.
6084     if (reloc_found && isExtern) {
6085       if (info->O->getAnyRelocationPCRel(RE)) {
6086         unsigned Type = info->O->getAnyRelocationType(RE);
6087         if (Type == MachO::X86_64_RELOC_SIGNED) {
6088           ReferenceValue = Symbol.getValue();
6089         }
6090       }
6091     }
6092   }
6093
6094   // Look for literals such as Objective-C CFStrings refs, Selector refs,
6095   // Message refs and Class refs.
6096   bool classref, selref, msgref, cfstring;
6097   uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
6098                                                selref, msgref, cfstring);
6099   if (classref && pointer_value == 0) {
6100     // Note the ReferenceValue is a pointer into the __objc_classrefs section.
6101     // And the pointer_value in that section is typically zero as it will be
6102     // set by dyld as part of the "bind information".
6103     const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
6104     if (name != nullptr) {
6105       *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
6106       const char *class_name = strrchr(name, '$');
6107       if (class_name != nullptr && class_name[1] == '_' &&
6108           class_name[2] != '\0') {
6109         info->class_name = class_name + 2;
6110         return name;
6111       }
6112     }
6113   }
6114
6115   if (classref) {
6116     *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
6117     const char *name =
6118         get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
6119     if (name != nullptr)
6120       info->class_name = name;
6121     else
6122       name = "bad class ref";
6123     return name;
6124   }
6125
6126   if (cfstring) {
6127     *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
6128     const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
6129     return name;
6130   }
6131
6132   if (selref && pointer_value == 0)
6133     pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
6134
6135   if (pointer_value != 0)
6136     ReferenceValue = pointer_value;
6137
6138   const char *name = GuessCstringPointer(ReferenceValue, info);
6139   if (name) {
6140     if (pointer_value != 0 && selref) {
6141       *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
6142       info->selector_name = name;
6143     } else if (pointer_value != 0 && msgref) {
6144       info->class_name = nullptr;
6145       *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
6146       info->selector_name = name;
6147     } else
6148       *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
6149     return name;
6150   }
6151
6152   // Lastly look for an indirect symbol with this ReferenceValue which is in
6153   // a literal pool.  If found return that symbol name.
6154   name = GuessIndirectSymbol(ReferenceValue, info);
6155   if (name) {
6156     *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
6157     return name;
6158   }
6159
6160   return nullptr;
6161 }
6162
6163 // SymbolizerSymbolLookUp is the symbol lookup function passed when creating
6164 // the Symbolizer.  It looks up the ReferenceValue using the info passed via the
6165 // pointer to the struct DisassembleInfo that was passed when MCSymbolizer
6166 // is created and returns the symbol name that matches the ReferenceValue or
6167 // nullptr if none.  The ReferenceType is passed in for the IN type of
6168 // reference the instruction is making from the values in defined in the header
6169 // "llvm-c/Disassembler.h".  On return the ReferenceType can set to a specific
6170 // Out type and the ReferenceName will also be set which is added as a comment
6171 // to the disassembled instruction.
6172 //
6173 // If the symbol name is a C++ mangled name then the demangled name is
6174 // returned through ReferenceName and ReferenceType is set to
6175 // LLVMDisassembler_ReferenceType_DeMangled_Name .
6176 //
6177 // When this is called to get a symbol name for a branch target then the
6178 // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
6179 // SymbolValue will be looked for in the indirect symbol table to determine if
6180 // it is an address for a symbol stub.  If so then the symbol name for that
6181 // stub is returned indirectly through ReferenceName and then ReferenceType is
6182 // set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
6183 //
6184 // When this is called with an value loaded via a PC relative load then
6185 // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
6186 // SymbolValue is checked to be an address of literal pointer, symbol pointer,
6187 // or an Objective-C meta data reference.  If so the output ReferenceType is
6188 // set to correspond to that as well as setting the ReferenceName.
6189 static const char *SymbolizerSymbolLookUp(void *DisInfo,
6190                                           uint64_t ReferenceValue,
6191                                           uint64_t *ReferenceType,
6192                                           uint64_t ReferencePC,
6193                                           const char **ReferenceName) {
6194   struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
6195   // If no verbose symbolic information is wanted then just return nullptr.
6196   if (!info->verbose) {
6197     *ReferenceName = nullptr;
6198     *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6199     return nullptr;
6200   }
6201
6202   const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
6203
6204   if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
6205     *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
6206     if (*ReferenceName != nullptr) {
6207       method_reference(info, ReferenceType, ReferenceName);
6208       if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
6209         *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
6210     } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
6211       if (info->demangled_name != nullptr)
6212         free(info->demangled_name);
6213       int status;
6214       info->demangled_name =
6215           itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
6216       if (info->demangled_name != nullptr) {
6217         *ReferenceName = info->demangled_name;
6218         *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
6219       } else
6220         *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6221     } else
6222       *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6223   } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
6224     *ReferenceName =
6225         GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6226     if (*ReferenceName)
6227       method_reference(info, ReferenceType, ReferenceName);
6228     else
6229       *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6230     // If this is arm64 and the reference is an adrp instruction save the
6231     // instruction, passed in ReferenceValue and the address of the instruction
6232     // for use later if we see and add immediate instruction.
6233   } else if (info->O->getArch() == Triple::aarch64 &&
6234              *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
6235     info->adrp_inst = ReferenceValue;
6236     info->adrp_addr = ReferencePC;
6237     SymbolName = nullptr;
6238     *ReferenceName = nullptr;
6239     *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6240     // If this is arm64 and reference is an add immediate instruction and we
6241     // have
6242     // seen an adrp instruction just before it and the adrp's Xd register
6243     // matches
6244     // this add's Xn register reconstruct the value being referenced and look to
6245     // see if it is a literal pointer.  Note the add immediate instruction is
6246     // passed in ReferenceValue.
6247   } else if (info->O->getArch() == Triple::aarch64 &&
6248              *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
6249              ReferencePC - 4 == info->adrp_addr &&
6250              (info->adrp_inst & 0x9f000000) == 0x90000000 &&
6251              (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
6252     uint32_t addxri_inst;
6253     uint64_t adrp_imm, addxri_imm;
6254
6255     adrp_imm =
6256         ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
6257     if (info->adrp_inst & 0x0200000)
6258       adrp_imm |= 0xfffffffffc000000LL;
6259
6260     addxri_inst = ReferenceValue;
6261     addxri_imm = (addxri_inst >> 10) & 0xfff;
6262     if (((addxri_inst >> 22) & 0x3) == 1)
6263       addxri_imm <<= 12;
6264
6265     ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
6266                      (adrp_imm << 12) + addxri_imm;
6267
6268     *ReferenceName =
6269         GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6270     if (*ReferenceName == nullptr)
6271       *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6272     // If this is arm64 and the reference is a load register instruction and we
6273     // have seen an adrp instruction just before it and the adrp's Xd register
6274     // matches this add's Xn register reconstruct the value being referenced and
6275     // look to see if it is a literal pointer.  Note the load register
6276     // instruction is passed in ReferenceValue.
6277   } else if (info->O->getArch() == Triple::aarch64 &&
6278              *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
6279              ReferencePC - 4 == info->adrp_addr &&
6280              (info->adrp_inst & 0x9f000000) == 0x90000000 &&
6281              (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
6282     uint32_t ldrxui_inst;
6283     uint64_t adrp_imm, ldrxui_imm;
6284
6285     adrp_imm =
6286         ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
6287     if (info->adrp_inst & 0x0200000)
6288       adrp_imm |= 0xfffffffffc000000LL;
6289
6290     ldrxui_inst = ReferenceValue;
6291     ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
6292
6293     ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
6294                      (adrp_imm << 12) + (ldrxui_imm << 3);
6295
6296     *ReferenceName =
6297         GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6298     if (*ReferenceName == nullptr)
6299       *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6300   }
6301   // If this arm64 and is an load register (PC-relative) instruction the
6302   // ReferenceValue is the PC plus the immediate value.
6303   else if (info->O->getArch() == Triple::aarch64 &&
6304            (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
6305             *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
6306     *ReferenceName =
6307         GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6308     if (*ReferenceName == nullptr)
6309       *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6310   } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
6311     if (info->demangled_name != nullptr)
6312       free(info->demangled_name);
6313     int status;
6314     info->demangled_name =
6315         itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
6316     if (info->demangled_name != nullptr) {
6317       *ReferenceName = info->demangled_name;
6318       *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
6319     }
6320   }
6321   else {
6322     *ReferenceName = nullptr;
6323     *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6324   }
6325
6326   return SymbolName;
6327 }
6328
6329 /// \brief Emits the comments that are stored in the CommentStream.
6330 /// Each comment in the CommentStream must end with a newline.
6331 static void emitComments(raw_svector_ostream &CommentStream,
6332                          SmallString<128> &CommentsToEmit,
6333                          formatted_raw_ostream &FormattedOS,
6334                          const MCAsmInfo &MAI) {
6335   // Flush the stream before taking its content.
6336   StringRef Comments = CommentsToEmit.str();
6337   // Get the default information for printing a comment.
6338   StringRef CommentBegin = MAI.getCommentString();
6339   unsigned CommentColumn = MAI.getCommentColumn();
6340   bool IsFirst = true;
6341   while (!Comments.empty()) {
6342     if (!IsFirst)
6343       FormattedOS << '\n';
6344     // Emit a line of comments.
6345     FormattedOS.PadToColumn(CommentColumn);
6346     size_t Position = Comments.find('\n');
6347     FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
6348     // Move after the newline character.
6349     Comments = Comments.substr(Position + 1);
6350     IsFirst = false;
6351   }
6352   FormattedOS.flush();
6353
6354   // Tell the comment stream that the vector changed underneath it.
6355   CommentsToEmit.clear();
6356 }
6357
6358 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
6359                              StringRef DisSegName, StringRef DisSectName) {
6360   const char *McpuDefault = nullptr;
6361   const Target *ThumbTarget = nullptr;
6362   const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
6363   if (!TheTarget) {
6364     // GetTarget prints out stuff.
6365     return;
6366   }
6367   if (MCPU.empty() && McpuDefault)
6368     MCPU = McpuDefault;
6369
6370   std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
6371   std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
6372   if (ThumbTarget)
6373     ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
6374
6375   // Package up features to be passed to target/subtarget
6376   std::string FeaturesStr;
6377   if (MAttrs.size()) {
6378     SubtargetFeatures Features;
6379     for (unsigned i = 0; i != MAttrs.size(); ++i)
6380       Features.AddFeature(MAttrs[i]);
6381     FeaturesStr = Features.getString();
6382   }
6383
6384   // Set up disassembler.
6385   std::unique_ptr<const MCRegisterInfo> MRI(
6386       TheTarget->createMCRegInfo(TripleName));
6387   std::unique_ptr<const MCAsmInfo> AsmInfo(
6388       TheTarget->createMCAsmInfo(*MRI, TripleName));
6389   std::unique_ptr<const MCSubtargetInfo> STI(
6390       TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
6391   MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
6392   std::unique_ptr<MCDisassembler> DisAsm(
6393       TheTarget->createMCDisassembler(*STI, Ctx));
6394   std::unique_ptr<MCSymbolizer> Symbolizer;
6395   struct DisassembleInfo SymbolizerInfo;
6396   std::unique_ptr<MCRelocationInfo> RelInfo(
6397       TheTarget->createMCRelocationInfo(TripleName, Ctx));
6398   if (RelInfo) {
6399     Symbolizer.reset(TheTarget->createMCSymbolizer(
6400         TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
6401         &SymbolizerInfo, &Ctx, std::move(RelInfo)));
6402     DisAsm->setSymbolizer(std::move(Symbolizer));
6403   }
6404   int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
6405   std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
6406       Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
6407   // Set the display preference for hex vs. decimal immediates.
6408   IP->setPrintImmHex(PrintImmHex);
6409   // Comment stream and backing vector.
6410   SmallString<128> CommentsToEmit;
6411   raw_svector_ostream CommentStream(CommentsToEmit);
6412   // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
6413   // if it is done then arm64 comments for string literals don't get printed
6414   // and some constant get printed instead and not setting it causes intel
6415   // (32-bit and 64-bit) comments printed with different spacing before the
6416   // comment causing different diffs with the 'C' disassembler library API.
6417   // IP->setCommentStream(CommentStream);
6418
6419   if (!AsmInfo || !STI || !DisAsm || !IP) {
6420     errs() << "error: couldn't initialize disassembler for target "
6421            << TripleName << '\n';
6422     return;
6423   }
6424
6425   // Set up separate thumb disassembler if needed.
6426   std::unique_ptr<const MCRegisterInfo> ThumbMRI;
6427   std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
6428   std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
6429   std::unique_ptr<MCDisassembler> ThumbDisAsm;
6430   std::unique_ptr<MCInstPrinter> ThumbIP;
6431   std::unique_ptr<MCContext> ThumbCtx;
6432   std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
6433   struct DisassembleInfo ThumbSymbolizerInfo;
6434   std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
6435   if (ThumbTarget) {
6436     ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
6437     ThumbAsmInfo.reset(
6438         ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
6439     ThumbSTI.reset(
6440         ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr));
6441     ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
6442     ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
6443     MCContext *PtrThumbCtx = ThumbCtx.get();
6444     ThumbRelInfo.reset(
6445         ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
6446     if (ThumbRelInfo) {
6447       ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
6448           ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
6449           &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
6450       ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
6451     }
6452     int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
6453     ThumbIP.reset(ThumbTarget->createMCInstPrinter(
6454         Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
6455         *ThumbInstrInfo, *ThumbMRI));
6456     // Set the display preference for hex vs. decimal immediates.
6457     ThumbIP->setPrintImmHex(PrintImmHex);
6458   }
6459
6460   if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
6461     errs() << "error: couldn't initialize disassembler for target "
6462            << ThumbTripleName << '\n';
6463     return;
6464   }
6465
6466   MachO::mach_header Header = MachOOF->getHeader();
6467
6468   // FIXME: Using the -cfg command line option, this code used to be able to
6469   // annotate relocations with the referenced symbol's name, and if this was
6470   // inside a __[cf]string section, the data it points to. This is now replaced
6471   // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
6472   std::vector<SectionRef> Sections;
6473   std::vector<SymbolRef> Symbols;
6474   SmallVector<uint64_t, 8> FoundFns;
6475   uint64_t BaseSegmentAddress;
6476
6477   getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
6478                         BaseSegmentAddress);
6479
6480   // Sort the symbols by address, just in case they didn't come in that way.
6481   std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
6482
6483   // Build a data in code table that is sorted on by the address of each entry.
6484   uint64_t BaseAddress = 0;
6485   if (Header.filetype == MachO::MH_OBJECT)
6486     BaseAddress = Sections[0].getAddress();
6487   else
6488     BaseAddress = BaseSegmentAddress;
6489   DiceTable Dices;
6490   for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
6491        DI != DE; ++DI) {
6492     uint32_t Offset;
6493     DI->getOffset(Offset);
6494     Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
6495   }
6496   array_pod_sort(Dices.begin(), Dices.end());
6497
6498 #ifndef NDEBUG
6499   raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
6500 #else
6501   raw_ostream &DebugOut = nulls();
6502 #endif
6503
6504   std::unique_ptr<DIContext> diContext;
6505   ObjectFile *DbgObj = MachOOF;
6506   // Try to find debug info and set up the DIContext for it.
6507   if (UseDbg) {
6508     // A separate DSym file path was specified, parse it as a macho file,
6509     // get the sections and supply it to the section name parsing machinery.
6510     if (!DSYMFile.empty()) {
6511       ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
6512           MemoryBuffer::getFileOrSTDIN(DSYMFile);
6513       if (std::error_code EC = BufOrErr.getError()) {
6514         errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
6515         return;
6516       }
6517       DbgObj =
6518           ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
6519               .get()
6520               .release();
6521     }
6522
6523     // Setup the DIContext
6524     diContext.reset(new DWARFContextInMemory(*DbgObj));
6525   }
6526
6527   if (FilterSections.size() == 0)
6528     outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
6529
6530   for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
6531     StringRef SectName;
6532     if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
6533       continue;
6534
6535     DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
6536
6537     StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
6538     if (SegmentName != DisSegName)
6539       continue;
6540
6541     StringRef BytesStr;
6542     Sections[SectIdx].getContents(BytesStr);
6543     ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
6544                             BytesStr.size());
6545     uint64_t SectAddress = Sections[SectIdx].getAddress();
6546
6547     bool symbolTableWorked = false;
6548
6549     // Create a map of symbol addresses to symbol names for use by
6550     // the SymbolizerSymbolLookUp() routine.
6551     SymbolAddressMap AddrMap;
6552     bool DisSymNameFound = false;
6553     for (const SymbolRef &Symbol : MachOOF->symbols()) {
6554       Expected<SymbolRef::Type> STOrErr = Symbol.getType();
6555       if (!STOrErr)
6556         report_error(MachOOF->getFileName(), STOrErr.takeError());
6557       SymbolRef::Type ST = *STOrErr;
6558       if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
6559           ST == SymbolRef::ST_Other) {
6560         uint64_t Address = Symbol.getValue();
6561         Expected<StringRef> SymNameOrErr = Symbol.getName();
6562         if (!SymNameOrErr)
6563           report_error(MachOOF->getFileName(), SymNameOrErr.takeError());
6564         StringRef SymName = *SymNameOrErr;
6565         AddrMap[Address] = SymName;
6566         if (!DisSymName.empty() && DisSymName == SymName)
6567           DisSymNameFound = true;
6568       }
6569     }
6570     if (!DisSymName.empty() && !DisSymNameFound) {
6571       outs() << "Can't find -dis-symname: " << DisSymName << "\n";
6572       return;
6573     }
6574     // Set up the block of info used by the Symbolizer call backs.
6575     SymbolizerInfo.verbose = !NoSymbolicOperands;
6576     SymbolizerInfo.O = MachOOF;
6577     SymbolizerInfo.S = Sections[SectIdx];
6578     SymbolizerInfo.AddrMap = &AddrMap;
6579     SymbolizerInfo.Sections = &Sections;
6580     SymbolizerInfo.class_name = nullptr;
6581     SymbolizerInfo.selector_name = nullptr;
6582     SymbolizerInfo.method = nullptr;
6583     SymbolizerInfo.demangled_name = nullptr;
6584     SymbolizerInfo.bindtable = nullptr;
6585     SymbolizerInfo.adrp_addr = 0;
6586     SymbolizerInfo.adrp_inst = 0;
6587     // Same for the ThumbSymbolizer
6588     ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
6589     ThumbSymbolizerInfo.O = MachOOF;
6590     ThumbSymbolizerInfo.S = Sections[SectIdx];
6591     ThumbSymbolizerInfo.AddrMap = &AddrMap;
6592     ThumbSymbolizerInfo.Sections = &Sections;
6593     ThumbSymbolizerInfo.class_name = nullptr;
6594     ThumbSymbolizerInfo.selector_name = nullptr;
6595     ThumbSymbolizerInfo.method = nullptr;
6596     ThumbSymbolizerInfo.demangled_name = nullptr;
6597     ThumbSymbolizerInfo.bindtable = nullptr;
6598     ThumbSymbolizerInfo.adrp_addr = 0;
6599     ThumbSymbolizerInfo.adrp_inst = 0;
6600
6601     unsigned int Arch = MachOOF->getArch();
6602
6603     // Skip all symbols if this is a stubs file.
6604     if (Bytes.size() == 0)
6605       return;
6606
6607     // Disassemble symbol by symbol.
6608     for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
6609       Expected<StringRef> SymNameOrErr = Symbols[SymIdx].getName();
6610       if (!SymNameOrErr)
6611         report_error(MachOOF->getFileName(), SymNameOrErr.takeError());
6612       StringRef SymName = *SymNameOrErr;
6613
6614       Expected<SymbolRef::Type> STOrErr = Symbols[SymIdx].getType();
6615       if (!STOrErr)
6616         report_error(MachOOF->getFileName(), STOrErr.takeError());
6617       SymbolRef::Type ST = *STOrErr;
6618       if (ST != SymbolRef::ST_Function && ST != SymbolRef::ST_Data)
6619         continue;
6620
6621       // Make sure the symbol is defined in this section.
6622       bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
6623       if (!containsSym) {
6624         if (!DisSymName.empty() && DisSymName == SymName) {
6625           outs() << "-dis-symname: " << DisSymName << " not in the section\n";
6626           return;
6627         }
6628         continue;
6629       }
6630       // The __mh_execute_header is special and we need to deal with that fact
6631       // this symbol is before the start of the (__TEXT,__text) section and at the
6632       // address of the start of the __TEXT segment.  This is because this symbol
6633       // is an N_SECT symbol in the (__TEXT,__text) but its address is before the
6634       // start of the section in a standard MH_EXECUTE filetype.
6635       if (!DisSymName.empty() && DisSymName == "__mh_execute_header") {
6636         outs() << "-dis-symname: __mh_execute_header not in any section\n";
6637         return;
6638       }
6639       // When this code is trying to disassemble a symbol at a time and in the
6640       // case there is only the __mh_execute_header symbol left as in a stripped
6641       // executable, we need to deal with this by ignoring this symbol so the
6642       // whole section is disassembled and this symbol is then not displayed.
6643       if (SymName == "__mh_execute_header" || SymName == "__mh_dylib_header" ||
6644           SymName == "__mh_bundle_header" || SymName == "__mh_object_header" ||
6645           SymName == "__mh_preload_header" || SymName == "__mh_dylinker_header")
6646         continue;
6647
6648       // If we are only disassembling one symbol see if this is that symbol.
6649       if (!DisSymName.empty() && DisSymName != SymName)
6650         continue;
6651
6652       // Start at the address of the symbol relative to the section's address.
6653       uint64_t SectSize = Sections[SectIdx].getSize();
6654       uint64_t Start = Symbols[SymIdx].getValue();
6655       uint64_t SectionAddress = Sections[SectIdx].getAddress();
6656       Start -= SectionAddress;
6657
6658       if (Start > SectSize) {
6659         outs() << "section data ends, " << SymName
6660                << " lies outside valid range\n";
6661         return;
6662       }
6663
6664       // Stop disassembling either at the beginning of the next symbol or at
6665       // the end of the section.
6666       bool containsNextSym = false;
6667       uint64_t NextSym = 0;
6668       uint64_t NextSymIdx = SymIdx + 1;
6669       while (Symbols.size() > NextSymIdx) {
6670         Expected<SymbolRef::Type> STOrErr = Symbols[NextSymIdx].getType();
6671         if (!STOrErr)
6672           report_error(MachOOF->getFileName(), STOrErr.takeError());
6673         SymbolRef::Type NextSymType = *STOrErr;
6674         if (NextSymType == SymbolRef::ST_Function) {
6675           containsNextSym =
6676               Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
6677           NextSym = Symbols[NextSymIdx].getValue();
6678           NextSym -= SectionAddress;
6679           break;
6680         }
6681         ++NextSymIdx;
6682       }
6683
6684       uint64_t End = containsNextSym ? std::min(NextSym, SectSize) : SectSize;
6685       uint64_t Size;
6686
6687       symbolTableWorked = true;
6688
6689       DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
6690       bool IsThumb = MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb;
6691
6692       // We only need the dedicated Thumb target if there's a real choice
6693       // (i.e. we're not targeting M-class) and the function is Thumb.
6694       bool UseThumbTarget = IsThumb && ThumbTarget;
6695
6696       outs() << SymName << ":\n";
6697       DILineInfo lastLine;
6698       for (uint64_t Index = Start; Index < End; Index += Size) {
6699         MCInst Inst;
6700
6701         uint64_t PC = SectAddress + Index;
6702         if (!NoLeadingAddr) {
6703           if (FullLeadingAddr) {
6704             if (MachOOF->is64Bit())
6705               outs() << format("%016" PRIx64, PC);
6706             else
6707               outs() << format("%08" PRIx64, PC);
6708           } else {
6709             outs() << format("%8" PRIx64 ":", PC);
6710           }
6711         }
6712         if (!NoShowRawInsn || Arch == Triple::arm)
6713           outs() << "\t";
6714
6715         // Check the data in code table here to see if this is data not an
6716         // instruction to be disassembled.
6717         DiceTable Dice;
6718         Dice.push_back(std::make_pair(PC, DiceRef()));
6719         dice_table_iterator DTI =
6720             std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
6721                         compareDiceTableEntries);
6722         if (DTI != Dices.end()) {
6723           uint16_t Length;
6724           DTI->second.getLength(Length);
6725           uint16_t Kind;
6726           DTI->second.getKind(Kind);
6727           Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
6728           if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
6729               (PC == (DTI->first + Length - 1)) && (Length & 1))
6730             Size++;
6731           continue;
6732         }
6733
6734         SmallVector<char, 64> AnnotationsBytes;
6735         raw_svector_ostream Annotations(AnnotationsBytes);
6736
6737         bool gotInst;
6738         if (UseThumbTarget)
6739           gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
6740                                                 PC, DebugOut, Annotations);
6741         else
6742           gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
6743                                            DebugOut, Annotations);
6744         if (gotInst) {
6745           if (!NoShowRawInsn || Arch == Triple::arm) {
6746             dumpBytes(makeArrayRef(Bytes.data() + Index, Size), outs());
6747           }
6748           formatted_raw_ostream FormattedOS(outs());
6749           StringRef AnnotationsStr = Annotations.str();
6750           if (UseThumbTarget)
6751             ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
6752           else
6753             IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
6754           emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
6755
6756           // Print debug info.
6757           if (diContext) {
6758             DILineInfo dli = diContext->getLineInfoForAddress(PC);
6759             // Print valid line info if it changed.
6760             if (dli != lastLine && dli.Line != 0)
6761               outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
6762                      << dli.Column;
6763             lastLine = dli;
6764           }
6765           outs() << "\n";
6766         } else {
6767           unsigned int Arch = MachOOF->getArch();
6768           if (Arch == Triple::x86_64 || Arch == Triple::x86) {
6769             outs() << format("\t.byte 0x%02x #bad opcode\n",
6770                              *(Bytes.data() + Index) & 0xff);
6771             Size = 1; // skip exactly one illegible byte and move on.
6772           } else if (Arch == Triple::aarch64 ||
6773                      (Arch == Triple::arm && !IsThumb)) {
6774             uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6775                               (*(Bytes.data() + Index + 1) & 0xff) << 8 |
6776                               (*(Bytes.data() + Index + 2) & 0xff) << 16 |
6777                               (*(Bytes.data() + Index + 3) & 0xff) << 24;
6778             outs() << format("\t.long\t0x%08x\n", opcode);
6779             Size = 4;
6780           } else if (Arch == Triple::arm) {
6781             assert(IsThumb && "ARM mode should have been dealt with above");
6782             uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6783                               (*(Bytes.data() + Index + 1) & 0xff) << 8;
6784             outs() << format("\t.short\t0x%04x\n", opcode);
6785             Size = 2;
6786           } else{
6787             errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6788             if (Size == 0)
6789               Size = 1; // skip illegible bytes
6790           }
6791         }
6792       }
6793     }
6794     if (!symbolTableWorked) {
6795       // Reading the symbol table didn't work, disassemble the whole section.
6796       uint64_t SectAddress = Sections[SectIdx].getAddress();
6797       uint64_t SectSize = Sections[SectIdx].getSize();
6798       uint64_t InstSize;
6799       for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
6800         MCInst Inst;
6801
6802         uint64_t PC = SectAddress + Index;
6803         if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
6804                                    DebugOut, nulls())) {
6805           if (!NoLeadingAddr) {
6806             if (FullLeadingAddr) {
6807               if (MachOOF->is64Bit())
6808                 outs() << format("%016" PRIx64, PC);
6809               else
6810                 outs() << format("%08" PRIx64, PC);
6811             } else {
6812               outs() << format("%8" PRIx64 ":", PC);
6813             }
6814           }
6815           if (!NoShowRawInsn || Arch == Triple::arm) {
6816             outs() << "\t";
6817             dumpBytes(makeArrayRef(Bytes.data() + Index, InstSize), outs());
6818           }
6819           IP->printInst(&Inst, outs(), "", *STI);
6820           outs() << "\n";
6821         } else {
6822           unsigned int Arch = MachOOF->getArch();
6823           if (Arch == Triple::x86_64 || Arch == Triple::x86) {
6824             outs() << format("\t.byte 0x%02x #bad opcode\n",
6825                              *(Bytes.data() + Index) & 0xff);
6826             InstSize = 1; // skip exactly one illegible byte and move on.
6827           } else {
6828             errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6829             if (InstSize == 0)
6830               InstSize = 1; // skip illegible bytes
6831           }
6832         }
6833       }
6834     }
6835     // The TripleName's need to be reset if we are called again for a different
6836     // archtecture.
6837     TripleName = "";
6838     ThumbTripleName = "";
6839
6840     if (SymbolizerInfo.method != nullptr)
6841       free(SymbolizerInfo.method);
6842     if (SymbolizerInfo.demangled_name != nullptr)
6843       free(SymbolizerInfo.demangled_name);
6844     if (SymbolizerInfo.bindtable != nullptr)
6845       delete SymbolizerInfo.bindtable;
6846     if (ThumbSymbolizerInfo.method != nullptr)
6847       free(ThumbSymbolizerInfo.method);
6848     if (ThumbSymbolizerInfo.demangled_name != nullptr)
6849       free(ThumbSymbolizerInfo.demangled_name);
6850     if (ThumbSymbolizerInfo.bindtable != nullptr)
6851       delete ThumbSymbolizerInfo.bindtable;
6852   }
6853 }
6854
6855 //===----------------------------------------------------------------------===//
6856 // __compact_unwind section dumping
6857 //===----------------------------------------------------------------------===//
6858
6859 namespace {
6860
6861 template <typename T> static uint64_t readNext(const char *&Buf) {
6862   using llvm::support::little;
6863   using llvm::support::unaligned;
6864
6865   uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
6866   Buf += sizeof(T);
6867   return Val;
6868 }
6869
6870 struct CompactUnwindEntry {
6871   uint32_t OffsetInSection;
6872
6873   uint64_t FunctionAddr;
6874   uint32_t Length;
6875   uint32_t CompactEncoding;
6876   uint64_t PersonalityAddr;
6877   uint64_t LSDAAddr;
6878
6879   RelocationRef FunctionReloc;
6880   RelocationRef PersonalityReloc;
6881   RelocationRef LSDAReloc;
6882
6883   CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
6884       : OffsetInSection(Offset) {
6885     if (Is64)
6886       read<uint64_t>(Contents.data() + Offset);
6887     else
6888       read<uint32_t>(Contents.data() + Offset);
6889   }
6890
6891 private:
6892   template <typename UIntPtr> void read(const char *Buf) {
6893     FunctionAddr = readNext<UIntPtr>(Buf);
6894     Length = readNext<uint32_t>(Buf);
6895     CompactEncoding = readNext<uint32_t>(Buf);
6896     PersonalityAddr = readNext<UIntPtr>(Buf);
6897     LSDAAddr = readNext<UIntPtr>(Buf);
6898   }
6899 };
6900 }
6901
6902 /// Given a relocation from __compact_unwind, consisting of the RelocationRef
6903 /// and data being relocated, determine the best base Name and Addend to use for
6904 /// display purposes.
6905 ///
6906 /// 1. An Extern relocation will directly reference a symbol (and the data is
6907 ///    then already an addend), so use that.
6908 /// 2. Otherwise the data is an offset in the object file's layout; try to find
6909 //     a symbol before it in the same section, and use the offset from there.
6910 /// 3. Finally, if all that fails, fall back to an offset from the start of the
6911 ///    referenced section.
6912 static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
6913                                       std::map<uint64_t, SymbolRef> &Symbols,
6914                                       const RelocationRef &Reloc, uint64_t Addr,
6915                                       StringRef &Name, uint64_t &Addend) {
6916   if (Reloc.getSymbol() != Obj->symbol_end()) {
6917     Expected<StringRef> NameOrErr = Reloc.getSymbol()->getName();
6918     if (!NameOrErr)
6919       report_error(Obj->getFileName(), NameOrErr.takeError());
6920     Name = *NameOrErr;
6921     Addend = Addr;
6922     return;
6923   }
6924
6925   auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
6926   SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
6927
6928   uint64_t SectionAddr = RelocSection.getAddress();
6929
6930   auto Sym = Symbols.upper_bound(Addr);
6931   if (Sym == Symbols.begin()) {
6932     // The first symbol in the object is after this reference, the best we can
6933     // do is section-relative notation.
6934     RelocSection.getName(Name);
6935     Addend = Addr - SectionAddr;
6936     return;
6937   }
6938
6939   // Go back one so that SymbolAddress <= Addr.
6940   --Sym;
6941
6942   auto SectOrErr = Sym->second.getSection();
6943   if (!SectOrErr)
6944     report_error(Obj->getFileName(), SectOrErr.takeError());
6945   section_iterator SymSection = *SectOrErr;
6946   if (RelocSection == *SymSection) {
6947     // There's a valid symbol in the same section before this reference.
6948     Expected<StringRef> NameOrErr = Sym->second.getName();
6949     if (!NameOrErr)
6950       report_error(Obj->getFileName(), NameOrErr.takeError());
6951     Name = *NameOrErr;
6952     Addend = Addr - Sym->first;
6953     return;
6954   }
6955
6956   // There is a symbol before this reference, but it's in a different
6957   // section. Probably not helpful to mention it, so use the section name.
6958   RelocSection.getName(Name);
6959   Addend = Addr - SectionAddr;
6960 }
6961
6962 static void printUnwindRelocDest(const MachOObjectFile *Obj,
6963                                  std::map<uint64_t, SymbolRef> &Symbols,
6964                                  const RelocationRef &Reloc, uint64_t Addr) {
6965   StringRef Name;
6966   uint64_t Addend;
6967
6968   if (!Reloc.getObject())
6969     return;
6970
6971   findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
6972
6973   outs() << Name;
6974   if (Addend)
6975     outs() << " + " << format("0x%" PRIx64, Addend);
6976 }
6977
6978 static void
6979 printMachOCompactUnwindSection(const MachOObjectFile *Obj,
6980                                std::map<uint64_t, SymbolRef> &Symbols,
6981                                const SectionRef &CompactUnwind) {
6982
6983   if (!Obj->isLittleEndian()) {
6984     outs() << "Skipping big-endian __compact_unwind section\n";
6985     return;
6986   }
6987
6988   bool Is64 = Obj->is64Bit();
6989   uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
6990   uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
6991
6992   StringRef Contents;
6993   CompactUnwind.getContents(Contents);
6994
6995   SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
6996
6997   // First populate the initial raw offsets, encodings and so on from the entry.
6998   for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
6999     CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
7000     CompactUnwinds.push_back(Entry);
7001   }
7002
7003   // Next we need to look at the relocations to find out what objects are
7004   // actually being referred to.
7005   for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
7006     uint64_t RelocAddress = Reloc.getOffset();
7007
7008     uint32_t EntryIdx = RelocAddress / EntrySize;
7009     uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
7010     CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
7011
7012     if (OffsetInEntry == 0)
7013       Entry.FunctionReloc = Reloc;
7014     else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
7015       Entry.PersonalityReloc = Reloc;
7016     else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
7017       Entry.LSDAReloc = Reloc;
7018     else {
7019       outs() << "Invalid relocation in __compact_unwind section\n";
7020       return;
7021     }
7022   }
7023
7024   // Finally, we're ready to print the data we've gathered.
7025   outs() << "Contents of __compact_unwind section:\n";
7026   for (auto &Entry : CompactUnwinds) {
7027     outs() << "  Entry at offset "
7028            << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
7029
7030     // 1. Start of the region this entry applies to.
7031     outs() << "    start:                " << format("0x%" PRIx64,
7032                                                      Entry.FunctionAddr) << ' ';
7033     printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
7034     outs() << '\n';
7035
7036     // 2. Length of the region this entry applies to.
7037     outs() << "    length:               " << format("0x%" PRIx32, Entry.Length)
7038            << '\n';
7039     // 3. The 32-bit compact encoding.
7040     outs() << "    compact encoding:     "
7041            << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
7042
7043     // 4. The personality function, if present.
7044     if (Entry.PersonalityReloc.getObject()) {
7045       outs() << "    personality function: "
7046              << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
7047       printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
7048                            Entry.PersonalityAddr);
7049       outs() << '\n';
7050     }
7051
7052     // 5. This entry's language-specific data area.
7053     if (Entry.LSDAReloc.getObject()) {
7054       outs() << "    LSDA:                 " << format("0x%" PRIx64,
7055                                                        Entry.LSDAAddr) << ' ';
7056       printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
7057       outs() << '\n';
7058     }
7059   }
7060 }
7061
7062 //===----------------------------------------------------------------------===//
7063 // __unwind_info section dumping
7064 //===----------------------------------------------------------------------===//
7065
7066 static void printRegularSecondLevelUnwindPage(const char *PageStart) {
7067   const char *Pos = PageStart;
7068   uint32_t Kind = readNext<uint32_t>(Pos);
7069   (void)Kind;
7070   assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
7071
7072   uint16_t EntriesStart = readNext<uint16_t>(Pos);
7073   uint16_t NumEntries = readNext<uint16_t>(Pos);
7074
7075   Pos = PageStart + EntriesStart;
7076   for (unsigned i = 0; i < NumEntries; ++i) {
7077     uint32_t FunctionOffset = readNext<uint32_t>(Pos);
7078     uint32_t Encoding = readNext<uint32_t>(Pos);
7079
7080     outs() << "      [" << i << "]: "
7081            << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7082            << ", "
7083            << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
7084   }
7085 }
7086
7087 static void printCompressedSecondLevelUnwindPage(
7088     const char *PageStart, uint32_t FunctionBase,
7089     const SmallVectorImpl<uint32_t> &CommonEncodings) {
7090   const char *Pos = PageStart;
7091   uint32_t Kind = readNext<uint32_t>(Pos);
7092   (void)Kind;
7093   assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
7094
7095   uint16_t EntriesStart = readNext<uint16_t>(Pos);
7096   uint16_t NumEntries = readNext<uint16_t>(Pos);
7097
7098   uint16_t EncodingsStart = readNext<uint16_t>(Pos);
7099   readNext<uint16_t>(Pos);
7100   const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
7101       PageStart + EncodingsStart);
7102
7103   Pos = PageStart + EntriesStart;
7104   for (unsigned i = 0; i < NumEntries; ++i) {
7105     uint32_t Entry = readNext<uint32_t>(Pos);
7106     uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
7107     uint32_t EncodingIdx = Entry >> 24;
7108
7109     uint32_t Encoding;
7110     if (EncodingIdx < CommonEncodings.size())
7111       Encoding = CommonEncodings[EncodingIdx];
7112     else
7113       Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
7114
7115     outs() << "      [" << i << "]: "
7116            << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7117            << ", "
7118            << "encoding[" << EncodingIdx
7119            << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
7120   }
7121 }
7122
7123 static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
7124                                         std::map<uint64_t, SymbolRef> &Symbols,
7125                                         const SectionRef &UnwindInfo) {
7126
7127   if (!Obj->isLittleEndian()) {
7128     outs() << "Skipping big-endian __unwind_info section\n";
7129     return;
7130   }
7131
7132   outs() << "Contents of __unwind_info section:\n";
7133
7134   StringRef Contents;
7135   UnwindInfo.getContents(Contents);
7136   const char *Pos = Contents.data();
7137
7138   //===----------------------------------
7139   // Section header
7140   //===----------------------------------
7141
7142   uint32_t Version = readNext<uint32_t>(Pos);
7143   outs() << "  Version:                                   "
7144          << format("0x%" PRIx32, Version) << '\n';
7145   if (Version != 1) {
7146     outs() << "    Skipping section with unknown version\n";
7147     return;
7148   }
7149
7150   uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
7151   outs() << "  Common encodings array section offset:     "
7152          << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
7153   uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
7154   outs() << "  Number of common encodings in array:       "
7155          << format("0x%" PRIx32, NumCommonEncodings) << '\n';
7156
7157   uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
7158   outs() << "  Personality function array section offset: "
7159          << format("0x%" PRIx32, PersonalitiesStart) << '\n';
7160   uint32_t NumPersonalities = readNext<uint32_t>(Pos);
7161   outs() << "  Number of personality functions in array:  "
7162          << format("0x%" PRIx32, NumPersonalities) << '\n';
7163
7164   uint32_t IndicesStart = readNext<uint32_t>(Pos);
7165   outs() << "  Index array section offset:                "
7166          << format("0x%" PRIx32, IndicesStart) << '\n';
7167   uint32_t NumIndices = readNext<uint32_t>(Pos);
7168   outs() << "  Number of indices in array:                "
7169          << format("0x%" PRIx32, NumIndices) << '\n';
7170
7171   //===----------------------------------
7172   // A shared list of common encodings
7173   //===----------------------------------
7174
7175   // These occupy indices in the range [0, N] whenever an encoding is referenced
7176   // from a compressed 2nd level index table. In practice the linker only
7177   // creates ~128 of these, so that indices are available to embed encodings in
7178   // the 2nd level index.
7179
7180   SmallVector<uint32_t, 64> CommonEncodings;
7181   outs() << "  Common encodings: (count = " << NumCommonEncodings << ")\n";
7182   Pos = Contents.data() + CommonEncodingsStart;
7183   for (unsigned i = 0; i < NumCommonEncodings; ++i) {
7184     uint32_t Encoding = readNext<uint32_t>(Pos);
7185     CommonEncodings.push_back(Encoding);
7186
7187     outs() << "    encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
7188            << '\n';
7189   }
7190
7191   //===----------------------------------
7192   // Personality functions used in this executable
7193   //===----------------------------------
7194
7195   // There should be only a handful of these (one per source language,
7196   // roughly). Particularly since they only get 2 bits in the compact encoding.
7197
7198   outs() << "  Personality functions: (count = " << NumPersonalities << ")\n";
7199   Pos = Contents.data() + PersonalitiesStart;
7200   for (unsigned i = 0; i < NumPersonalities; ++i) {
7201     uint32_t PersonalityFn = readNext<uint32_t>(Pos);
7202     outs() << "    personality[" << i + 1
7203            << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
7204   }
7205
7206   //===----------------------------------
7207   // The level 1 index entries
7208   //===----------------------------------
7209
7210   // These specify an approximate place to start searching for the more detailed
7211   // information, sorted by PC.
7212
7213   struct IndexEntry {
7214     uint32_t FunctionOffset;
7215     uint32_t SecondLevelPageStart;
7216     uint32_t LSDAStart;
7217   };
7218
7219   SmallVector<IndexEntry, 4> IndexEntries;
7220
7221   outs() << "  Top level indices: (count = " << NumIndices << ")\n";
7222   Pos = Contents.data() + IndicesStart;
7223   for (unsigned i = 0; i < NumIndices; ++i) {
7224     IndexEntry Entry;
7225
7226     Entry.FunctionOffset = readNext<uint32_t>(Pos);
7227     Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
7228     Entry.LSDAStart = readNext<uint32_t>(Pos);
7229     IndexEntries.push_back(Entry);
7230
7231     outs() << "    [" << i << "]: "
7232            << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
7233            << ", "
7234            << "2nd level page offset="
7235            << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
7236            << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
7237   }
7238
7239   //===----------------------------------
7240   // Next come the LSDA tables
7241   //===----------------------------------
7242
7243   // The LSDA layout is rather implicit: it's a contiguous array of entries from
7244   // the first top-level index's LSDAOffset to the last (sentinel).
7245
7246   outs() << "  LSDA descriptors:\n";
7247   Pos = Contents.data() + IndexEntries[0].LSDAStart;
7248   int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
7249                  (2 * sizeof(uint32_t));
7250   for (int i = 0; i < NumLSDAs; ++i) {
7251     uint32_t FunctionOffset = readNext<uint32_t>(Pos);
7252     uint32_t LSDAOffset = readNext<uint32_t>(Pos);
7253     outs() << "    [" << i << "]: "
7254            << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7255            << ", "
7256            << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
7257   }
7258
7259   //===----------------------------------
7260   // Finally, the 2nd level indices
7261   //===----------------------------------
7262
7263   // Generally these are 4K in size, and have 2 possible forms:
7264   //   + Regular stores up to 511 entries with disparate encodings
7265   //   + Compressed stores up to 1021 entries if few enough compact encoding
7266   //     values are used.
7267   outs() << "  Second level indices:\n";
7268   for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
7269     // The final sentinel top-level index has no associated 2nd level page
7270     if (IndexEntries[i].SecondLevelPageStart == 0)
7271       break;
7272
7273     outs() << "    Second level index[" << i << "]: "
7274            << "offset in section="
7275            << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
7276            << ", "
7277            << "base function offset="
7278            << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
7279
7280     Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
7281     uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
7282     if (Kind == 2)
7283       printRegularSecondLevelUnwindPage(Pos);
7284     else if (Kind == 3)
7285       printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
7286                                            CommonEncodings);
7287     else
7288       outs() << "    Skipping 2nd level page with unknown kind " << Kind
7289              << '\n';
7290   }
7291 }
7292
7293 void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
7294   std::map<uint64_t, SymbolRef> Symbols;
7295   for (const SymbolRef &SymRef : Obj->symbols()) {
7296     // Discard any undefined or absolute symbols. They're not going to take part
7297     // in the convenience lookup for unwind info and just take up resources.
7298     auto SectOrErr = SymRef.getSection();
7299     if (!SectOrErr) {
7300       // TODO: Actually report errors helpfully.
7301       consumeError(SectOrErr.takeError());
7302       continue;
7303     }
7304     section_iterator Section = *SectOrErr;
7305     if (Section == Obj->section_end())
7306       continue;
7307
7308     uint64_t Addr = SymRef.getValue();
7309     Symbols.insert(std::make_pair(Addr, SymRef));
7310   }
7311
7312   for (const SectionRef &Section : Obj->sections()) {
7313     StringRef SectName;
7314     Section.getName(SectName);
7315     if (SectName == "__compact_unwind")
7316       printMachOCompactUnwindSection(Obj, Symbols, Section);
7317     else if (SectName == "__unwind_info")
7318       printMachOUnwindInfoSection(Obj, Symbols, Section);
7319   }
7320 }
7321
7322 static void PrintMachHeader(uint32_t magic, uint32_t cputype,
7323                             uint32_t cpusubtype, uint32_t filetype,
7324                             uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
7325                             bool verbose) {
7326   outs() << "Mach header\n";
7327   outs() << "      magic cputype cpusubtype  caps    filetype ncmds "
7328             "sizeofcmds      flags\n";
7329   if (verbose) {
7330     if (magic == MachO::MH_MAGIC)
7331       outs() << "   MH_MAGIC";
7332     else if (magic == MachO::MH_MAGIC_64)
7333       outs() << "MH_MAGIC_64";
7334     else
7335       outs() << format(" 0x%08" PRIx32, magic);
7336     switch (cputype) {
7337     case MachO::CPU_TYPE_I386:
7338       outs() << "    I386";
7339       switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7340       case MachO::CPU_SUBTYPE_I386_ALL:
7341         outs() << "        ALL";
7342         break;
7343       default:
7344         outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7345         break;
7346       }
7347       break;
7348     case MachO::CPU_TYPE_X86_64:
7349       outs() << "  X86_64";
7350       switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7351       case MachO::CPU_SUBTYPE_X86_64_ALL:
7352         outs() << "        ALL";
7353         break;
7354       case MachO::CPU_SUBTYPE_X86_64_H:
7355         outs() << "    Haswell";
7356         break;
7357       default:
7358         outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7359         break;
7360       }
7361       break;
7362     case MachO::CPU_TYPE_ARM:
7363       outs() << "     ARM";
7364       switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7365       case MachO::CPU_SUBTYPE_ARM_ALL:
7366         outs() << "        ALL";
7367         break;
7368       case MachO::CPU_SUBTYPE_ARM_V4T:
7369         outs() << "        V4T";
7370         break;
7371       case MachO::CPU_SUBTYPE_ARM_V5TEJ:
7372         outs() << "      V5TEJ";
7373         break;
7374       case MachO::CPU_SUBTYPE_ARM_XSCALE:
7375         outs() << "     XSCALE";
7376         break;
7377       case MachO::CPU_SUBTYPE_ARM_V6:
7378         outs() << "         V6";
7379         break;
7380       case MachO::CPU_SUBTYPE_ARM_V6M:
7381         outs() << "        V6M";
7382         break;
7383       case MachO::CPU_SUBTYPE_ARM_V7:
7384         outs() << "         V7";
7385         break;
7386       case MachO::CPU_SUBTYPE_ARM_V7EM:
7387         outs() << "       V7EM";
7388         break;
7389       case MachO::CPU_SUBTYPE_ARM_V7K:
7390         outs() << "        V7K";
7391         break;
7392       case MachO::CPU_SUBTYPE_ARM_V7M:
7393         outs() << "        V7M";
7394         break;
7395       case MachO::CPU_SUBTYPE_ARM_V7S:
7396         outs() << "        V7S";
7397         break;
7398       default:
7399         outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7400         break;
7401       }
7402       break;
7403     case MachO::CPU_TYPE_ARM64:
7404       outs() << "   ARM64";
7405       switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7406       case MachO::CPU_SUBTYPE_ARM64_ALL:
7407         outs() << "        ALL";
7408         break;
7409       default:
7410         outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7411         break;
7412       }
7413       break;
7414     case MachO::CPU_TYPE_POWERPC:
7415       outs() << "     PPC";
7416       switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7417       case MachO::CPU_SUBTYPE_POWERPC_ALL:
7418         outs() << "        ALL";
7419         break;
7420       default:
7421         outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7422         break;
7423       }
7424       break;
7425     case MachO::CPU_TYPE_POWERPC64:
7426       outs() << "   PPC64";
7427       switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7428       case MachO::CPU_SUBTYPE_POWERPC_ALL:
7429         outs() << "        ALL";
7430         break;
7431       default:
7432         outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7433         break;
7434       }
7435       break;
7436     default:
7437       outs() << format(" %7d", cputype);
7438       outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7439       break;
7440     }
7441     if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
7442       outs() << " LIB64";
7443     } else {
7444       outs() << format("  0x%02" PRIx32,
7445                        (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7446     }
7447     switch (filetype) {
7448     case MachO::MH_OBJECT:
7449       outs() << "      OBJECT";
7450       break;
7451     case MachO::MH_EXECUTE:
7452       outs() << "     EXECUTE";
7453       break;
7454     case MachO::MH_FVMLIB:
7455       outs() << "      FVMLIB";
7456       break;
7457     case MachO::MH_CORE:
7458       outs() << "        CORE";
7459       break;
7460     case MachO::MH_PRELOAD:
7461       outs() << "     PRELOAD";
7462       break;
7463     case MachO::MH_DYLIB:
7464       outs() << "       DYLIB";
7465       break;
7466     case MachO::MH_DYLIB_STUB:
7467       outs() << "  DYLIB_STUB";
7468       break;
7469     case MachO::MH_DYLINKER:
7470       outs() << "    DYLINKER";
7471       break;
7472     case MachO::MH_BUNDLE:
7473       outs() << "      BUNDLE";
7474       break;
7475     case MachO::MH_DSYM:
7476       outs() << "        DSYM";
7477       break;
7478     case MachO::MH_KEXT_BUNDLE:
7479       outs() << "  KEXTBUNDLE";
7480       break;
7481     default:
7482       outs() << format("  %10u", filetype);
7483       break;
7484     }
7485     outs() << format(" %5u", ncmds);
7486     outs() << format(" %10u", sizeofcmds);
7487     uint32_t f = flags;
7488     if (f & MachO::MH_NOUNDEFS) {
7489       outs() << "   NOUNDEFS";
7490       f &= ~MachO::MH_NOUNDEFS;
7491     }
7492     if (f & MachO::MH_INCRLINK) {
7493       outs() << " INCRLINK";
7494       f &= ~MachO::MH_INCRLINK;
7495     }
7496     if (f & MachO::MH_DYLDLINK) {
7497       outs() << " DYLDLINK";
7498       f &= ~MachO::MH_DYLDLINK;
7499     }
7500     if (f & MachO::MH_BINDATLOAD) {
7501       outs() << " BINDATLOAD";
7502       f &= ~MachO::MH_BINDATLOAD;
7503     }
7504     if (f & MachO::MH_PREBOUND) {
7505       outs() << " PREBOUND";
7506       f &= ~MachO::MH_PREBOUND;
7507     }
7508     if (f & MachO::MH_SPLIT_SEGS) {
7509       outs() << " SPLIT_SEGS";
7510       f &= ~MachO::MH_SPLIT_SEGS;
7511     }
7512     if (f & MachO::MH_LAZY_INIT) {
7513       outs() << " LAZY_INIT";
7514       f &= ~MachO::MH_LAZY_INIT;
7515     }
7516     if (f & MachO::MH_TWOLEVEL) {
7517       outs() << " TWOLEVEL";
7518       f &= ~MachO::MH_TWOLEVEL;
7519     }
7520     if (f & MachO::MH_FORCE_FLAT) {
7521       outs() << " FORCE_FLAT";
7522       f &= ~MachO::MH_FORCE_FLAT;
7523     }
7524     if (f & MachO::MH_NOMULTIDEFS) {
7525       outs() << " NOMULTIDEFS";
7526       f &= ~MachO::MH_NOMULTIDEFS;
7527     }
7528     if (f & MachO::MH_NOFIXPREBINDING) {
7529       outs() << " NOFIXPREBINDING";
7530       f &= ~MachO::MH_NOFIXPREBINDING;
7531     }
7532     if (f & MachO::MH_PREBINDABLE) {
7533       outs() << " PREBINDABLE";
7534       f &= ~MachO::MH_PREBINDABLE;
7535     }
7536     if (f & MachO::MH_ALLMODSBOUND) {
7537       outs() << " ALLMODSBOUND";
7538       f &= ~MachO::MH_ALLMODSBOUND;
7539     }
7540     if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
7541       outs() << " SUBSECTIONS_VIA_SYMBOLS";
7542       f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
7543     }
7544     if (f & MachO::MH_CANONICAL) {
7545       outs() << " CANONICAL";
7546       f &= ~MachO::MH_CANONICAL;
7547     }
7548     if (f & MachO::MH_WEAK_DEFINES) {
7549       outs() << " WEAK_DEFINES";
7550       f &= ~MachO::MH_WEAK_DEFINES;
7551     }
7552     if (f & MachO::MH_BINDS_TO_WEAK) {
7553       outs() << " BINDS_TO_WEAK";
7554       f &= ~MachO::MH_BINDS_TO_WEAK;
7555     }
7556     if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
7557       outs() << " ALLOW_STACK_EXECUTION";
7558       f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
7559     }
7560     if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
7561       outs() << " DEAD_STRIPPABLE_DYLIB";
7562       f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
7563     }
7564     if (f & MachO::MH_PIE) {
7565       outs() << " PIE";
7566       f &= ~MachO::MH_PIE;
7567     }
7568     if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
7569       outs() << " NO_REEXPORTED_DYLIBS";
7570       f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
7571     }
7572     if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
7573       outs() << " MH_HAS_TLV_DESCRIPTORS";
7574       f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
7575     }
7576     if (f & MachO::MH_NO_HEAP_EXECUTION) {
7577       outs() << " MH_NO_HEAP_EXECUTION";
7578       f &= ~MachO::MH_NO_HEAP_EXECUTION;
7579     }
7580     if (f & MachO::MH_APP_EXTENSION_SAFE) {
7581       outs() << " APP_EXTENSION_SAFE";
7582       f &= ~MachO::MH_APP_EXTENSION_SAFE;
7583     }
7584     if (f != 0 || flags == 0)
7585       outs() << format(" 0x%08" PRIx32, f);
7586   } else {
7587     outs() << format(" 0x%08" PRIx32, magic);
7588     outs() << format(" %7d", cputype);
7589     outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7590     outs() << format("  0x%02" PRIx32,
7591                      (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7592     outs() << format("  %10u", filetype);
7593     outs() << format(" %5u", ncmds);
7594     outs() << format(" %10u", sizeofcmds);
7595     outs() << format(" 0x%08" PRIx32, flags);
7596   }
7597   outs() << "\n";
7598 }
7599
7600 static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
7601                                 StringRef SegName, uint64_t vmaddr,
7602                                 uint64_t vmsize, uint64_t fileoff,
7603                                 uint64_t filesize, uint32_t maxprot,
7604                                 uint32_t initprot, uint32_t nsects,
7605                                 uint32_t flags, uint32_t object_size,
7606                                 bool verbose) {
7607   uint64_t expected_cmdsize;
7608   if (cmd == MachO::LC_SEGMENT) {
7609     outs() << "      cmd LC_SEGMENT\n";
7610     expected_cmdsize = nsects;
7611     expected_cmdsize *= sizeof(struct MachO::section);
7612     expected_cmdsize += sizeof(struct MachO::segment_command);
7613   } else {
7614     outs() << "      cmd LC_SEGMENT_64\n";
7615     expected_cmdsize = nsects;
7616     expected_cmdsize *= sizeof(struct MachO::section_64);
7617     expected_cmdsize += sizeof(struct MachO::segment_command_64);
7618   }
7619   outs() << "  cmdsize " << cmdsize;
7620   if (cmdsize != expected_cmdsize)
7621     outs() << " Inconsistent size\n";
7622   else
7623     outs() << "\n";
7624   outs() << "  segname " << SegName << "\n";
7625   if (cmd == MachO::LC_SEGMENT_64) {
7626     outs() << "   vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
7627     outs() << "   vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
7628   } else {
7629     outs() << "   vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
7630     outs() << "   vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
7631   }
7632   outs() << "  fileoff " << fileoff;
7633   if (fileoff > object_size)
7634     outs() << " (past end of file)\n";
7635   else
7636     outs() << "\n";
7637   outs() << " filesize " << filesize;
7638   if (fileoff + filesize > object_size)
7639     outs() << " (past end of file)\n";
7640   else
7641     outs() << "\n";
7642   if (verbose) {
7643     if ((maxprot &
7644          ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7645            MachO::VM_PROT_EXECUTE)) != 0)
7646       outs() << "  maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
7647     else {
7648       outs() << "  maxprot ";
7649       outs() << ((maxprot & MachO::VM_PROT_READ) ? "r" : "-");
7650       outs() << ((maxprot & MachO::VM_PROT_WRITE) ? "w" : "-");
7651       outs() << ((maxprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
7652     }
7653     if ((initprot &
7654          ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7655            MachO::VM_PROT_EXECUTE)) != 0)
7656       outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
7657     else {
7658       outs() << " initprot ";
7659       outs() << ((initprot & MachO::VM_PROT_READ) ? "r" : "-");
7660       outs() << ((initprot & MachO::VM_PROT_WRITE) ? "w" : "-");
7661       outs() << ((initprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
7662     }
7663   } else {
7664     outs() << "  maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
7665     outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
7666   }
7667   outs() << "   nsects " << nsects << "\n";
7668   if (verbose) {
7669     outs() << "    flags";
7670     if (flags == 0)
7671       outs() << " (none)\n";
7672     else {
7673       if (flags & MachO::SG_HIGHVM) {
7674         outs() << " HIGHVM";
7675         flags &= ~MachO::SG_HIGHVM;
7676       }
7677       if (flags & MachO::SG_FVMLIB) {
7678         outs() << " FVMLIB";
7679         flags &= ~MachO::SG_FVMLIB;
7680       }
7681       if (flags & MachO::SG_NORELOC) {
7682         outs() << " NORELOC";
7683         flags &= ~MachO::SG_NORELOC;
7684       }
7685       if (flags & MachO::SG_PROTECTED_VERSION_1) {
7686         outs() << " PROTECTED_VERSION_1";
7687         flags &= ~MachO::SG_PROTECTED_VERSION_1;
7688       }
7689       if (flags)
7690         outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
7691       else
7692         outs() << "\n";
7693     }
7694   } else {
7695     outs() << "    flags " << format("0x%" PRIx32, flags) << "\n";
7696   }
7697 }
7698
7699 static void PrintSection(const char *sectname, const char *segname,
7700                          uint64_t addr, uint64_t size, uint32_t offset,
7701                          uint32_t align, uint32_t reloff, uint32_t nreloc,
7702                          uint32_t flags, uint32_t reserved1, uint32_t reserved2,
7703                          uint32_t cmd, const char *sg_segname,
7704                          uint32_t filetype, uint32_t object_size,
7705                          bool verbose) {
7706   outs() << "Section\n";
7707   outs() << "  sectname " << format("%.16s\n", sectname);
7708   outs() << "   segname " << format("%.16s", segname);
7709   if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
7710     outs() << " (does not match segment)\n";
7711   else
7712     outs() << "\n";
7713   if (cmd == MachO::LC_SEGMENT_64) {
7714     outs() << "      addr " << format("0x%016" PRIx64, addr) << "\n";
7715     outs() << "      size " << format("0x%016" PRIx64, size);
7716   } else {
7717     outs() << "      addr " << format("0x%08" PRIx64, addr) << "\n";
7718     outs() << "      size " << format("0x%08" PRIx64, size);
7719   }
7720   if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
7721     outs() << " (past end of file)\n";
7722   else
7723     outs() << "\n";
7724   outs() << "    offset " << offset;
7725   if (offset > object_size)
7726     outs() << " (past end of file)\n";
7727   else
7728     outs() << "\n";
7729   uint32_t align_shifted = 1 << align;
7730   outs() << "     align 2^" << align << " (" << align_shifted << ")\n";
7731   outs() << "    reloff " << reloff;
7732   if (reloff > object_size)
7733     outs() << " (past end of file)\n";
7734   else
7735     outs() << "\n";
7736   outs() << "    nreloc " << nreloc;
7737   if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
7738     outs() << " (past end of file)\n";
7739   else
7740     outs() << "\n";
7741   uint32_t section_type = flags & MachO::SECTION_TYPE;
7742   if (verbose) {
7743     outs() << "      type";
7744     if (section_type == MachO::S_REGULAR)
7745       outs() << " S_REGULAR\n";
7746     else if (section_type == MachO::S_ZEROFILL)
7747       outs() << " S_ZEROFILL\n";
7748     else if (section_type == MachO::S_CSTRING_LITERALS)
7749       outs() << " S_CSTRING_LITERALS\n";
7750     else if (section_type == MachO::S_4BYTE_LITERALS)
7751       outs() << " S_4BYTE_LITERALS\n";
7752     else if (section_type == MachO::S_8BYTE_LITERALS)
7753       outs() << " S_8BYTE_LITERALS\n";
7754     else if (section_type == MachO::S_16BYTE_LITERALS)
7755       outs() << " S_16BYTE_LITERALS\n";
7756     else if (section_type == MachO::S_LITERAL_POINTERS)
7757       outs() << " S_LITERAL_POINTERS\n";
7758     else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
7759       outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
7760     else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
7761       outs() << " S_LAZY_SYMBOL_POINTERS\n";
7762     else if (section_type == MachO::S_SYMBOL_STUBS)
7763       outs() << " S_SYMBOL_STUBS\n";
7764     else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
7765       outs() << " S_MOD_INIT_FUNC_POINTERS\n";
7766     else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
7767       outs() << " S_MOD_TERM_FUNC_POINTERS\n";
7768     else if (section_type == MachO::S_COALESCED)
7769       outs() << " S_COALESCED\n";
7770     else if (section_type == MachO::S_INTERPOSING)
7771       outs() << " S_INTERPOSING\n";
7772     else if (section_type == MachO::S_DTRACE_DOF)
7773       outs() << " S_DTRACE_DOF\n";
7774     else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
7775       outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
7776     else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
7777       outs() << " S_THREAD_LOCAL_REGULAR\n";
7778     else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
7779       outs() << " S_THREAD_LOCAL_ZEROFILL\n";
7780     else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
7781       outs() << " S_THREAD_LOCAL_VARIABLES\n";
7782     else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7783       outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
7784     else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
7785       outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
7786     else
7787       outs() << format("0x%08" PRIx32, section_type) << "\n";
7788     outs() << "attributes";
7789     uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
7790     if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
7791       outs() << " PURE_INSTRUCTIONS";
7792     if (section_attributes & MachO::S_ATTR_NO_TOC)
7793       outs() << " NO_TOC";
7794     if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
7795       outs() << " STRIP_STATIC_SYMS";
7796     if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
7797       outs() << " NO_DEAD_STRIP";
7798     if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
7799       outs() << " LIVE_SUPPORT";
7800     if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
7801       outs() << " SELF_MODIFYING_CODE";
7802     if (section_attributes & MachO::S_ATTR_DEBUG)
7803       outs() << " DEBUG";
7804     if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
7805       outs() << " SOME_INSTRUCTIONS";
7806     if (section_attributes & MachO::S_ATTR_EXT_RELOC)
7807       outs() << " EXT_RELOC";
7808     if (section_attributes & MachO::S_ATTR_LOC_RELOC)
7809       outs() << " LOC_RELOC";
7810     if (section_attributes == 0)
7811       outs() << " (none)";
7812     outs() << "\n";
7813   } else
7814     outs() << "     flags " << format("0x%08" PRIx32, flags) << "\n";
7815   outs() << " reserved1 " << reserved1;
7816   if (section_type == MachO::S_SYMBOL_STUBS ||
7817       section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
7818       section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
7819       section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
7820       section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7821     outs() << " (index into indirect symbol table)\n";
7822   else
7823     outs() << "\n";
7824   outs() << " reserved2 " << reserved2;
7825   if (section_type == MachO::S_SYMBOL_STUBS)
7826     outs() << " (size of stubs)\n";
7827   else
7828     outs() << "\n";
7829 }
7830
7831 static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
7832                                    uint32_t object_size) {
7833   outs() << "     cmd LC_SYMTAB\n";
7834   outs() << " cmdsize " << st.cmdsize;
7835   if (st.cmdsize != sizeof(struct MachO::symtab_command))
7836     outs() << " Incorrect size\n";
7837   else
7838     outs() << "\n";
7839   outs() << "  symoff " << st.symoff;
7840   if (st.symoff > object_size)
7841     outs() << " (past end of file)\n";
7842   else
7843     outs() << "\n";
7844   outs() << "   nsyms " << st.nsyms;
7845   uint64_t big_size;
7846   if (Is64Bit) {
7847     big_size = st.nsyms;
7848     big_size *= sizeof(struct MachO::nlist_64);
7849     big_size += st.symoff;
7850     if (big_size > object_size)
7851       outs() << " (past end of file)\n";
7852     else
7853       outs() << "\n";
7854   } else {
7855     big_size = st.nsyms;
7856     big_size *= sizeof(struct MachO::nlist);
7857     big_size += st.symoff;
7858     if (big_size > object_size)
7859       outs() << " (past end of file)\n";
7860     else
7861       outs() << "\n";
7862   }
7863   outs() << "  stroff " << st.stroff;
7864   if (st.stroff > object_size)
7865     outs() << " (past end of file)\n";
7866   else
7867     outs() << "\n";
7868   outs() << " strsize " << st.strsize;
7869   big_size = st.stroff;
7870   big_size += st.strsize;
7871   if (big_size > object_size)
7872     outs() << " (past end of file)\n";
7873   else
7874     outs() << "\n";
7875 }
7876
7877 static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
7878                                      uint32_t nsyms, uint32_t object_size,
7879                                      bool Is64Bit) {
7880   outs() << "            cmd LC_DYSYMTAB\n";
7881   outs() << "        cmdsize " << dyst.cmdsize;
7882   if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
7883     outs() << " Incorrect size\n";
7884   else
7885     outs() << "\n";
7886   outs() << "      ilocalsym " << dyst.ilocalsym;
7887   if (dyst.ilocalsym > nsyms)
7888     outs() << " (greater than the number of symbols)\n";
7889   else
7890     outs() << "\n";
7891   outs() << "      nlocalsym " << dyst.nlocalsym;
7892   uint64_t big_size;
7893   big_size = dyst.ilocalsym;
7894   big_size += dyst.nlocalsym;
7895   if (big_size > nsyms)
7896     outs() << " (past the end of the symbol table)\n";
7897   else
7898     outs() << "\n";
7899   outs() << "     iextdefsym " << dyst.iextdefsym;
7900   if (dyst.iextdefsym > nsyms)
7901     outs() << " (greater than the number of symbols)\n";
7902   else
7903     outs() << "\n";
7904   outs() << "     nextdefsym " << dyst.nextdefsym;
7905   big_size = dyst.iextdefsym;
7906   big_size += dyst.nextdefsym;
7907   if (big_size > nsyms)
7908     outs() << " (past the end of the symbol table)\n";
7909   else
7910     outs() << "\n";
7911   outs() << "      iundefsym " << dyst.iundefsym;
7912   if (dyst.iundefsym > nsyms)
7913     outs() << " (greater than the number of symbols)\n";
7914   else
7915     outs() << "\n";
7916   outs() << "      nundefsym " << dyst.nundefsym;
7917   big_size = dyst.iundefsym;
7918   big_size += dyst.nundefsym;
7919   if (big_size > nsyms)
7920     outs() << " (past the end of the symbol table)\n";
7921   else
7922     outs() << "\n";
7923   outs() << "         tocoff " << dyst.tocoff;
7924   if (dyst.tocoff > object_size)
7925     outs() << " (past end of file)\n";
7926   else
7927     outs() << "\n";
7928   outs() << "           ntoc " << dyst.ntoc;
7929   big_size = dyst.ntoc;
7930   big_size *= sizeof(struct MachO::dylib_table_of_contents);
7931   big_size += dyst.tocoff;
7932   if (big_size > object_size)
7933     outs() << " (past end of file)\n";
7934   else
7935     outs() << "\n";
7936   outs() << "      modtaboff " << dyst.modtaboff;
7937   if (dyst.modtaboff > object_size)
7938     outs() << " (past end of file)\n";
7939   else
7940     outs() << "\n";
7941   outs() << "        nmodtab " << dyst.nmodtab;
7942   uint64_t modtabend;
7943   if (Is64Bit) {
7944     modtabend = dyst.nmodtab;
7945     modtabend *= sizeof(struct MachO::dylib_module_64);
7946     modtabend += dyst.modtaboff;
7947   } else {
7948     modtabend = dyst.nmodtab;
7949     modtabend *= sizeof(struct MachO::dylib_module);
7950     modtabend += dyst.modtaboff;
7951   }
7952   if (modtabend > object_size)
7953     outs() << " (past end of file)\n";
7954   else
7955     outs() << "\n";
7956   outs() << "   extrefsymoff " << dyst.extrefsymoff;
7957   if (dyst.extrefsymoff > object_size)
7958     outs() << " (past end of file)\n";
7959   else
7960     outs() << "\n";
7961   outs() << "    nextrefsyms " << dyst.nextrefsyms;
7962   big_size = dyst.nextrefsyms;
7963   big_size *= sizeof(struct MachO::dylib_reference);
7964   big_size += dyst.extrefsymoff;
7965   if (big_size > object_size)
7966     outs() << " (past end of file)\n";
7967   else
7968     outs() << "\n";
7969   outs() << " indirectsymoff " << dyst.indirectsymoff;
7970   if (dyst.indirectsymoff > object_size)
7971     outs() << " (past end of file)\n";
7972   else
7973     outs() << "\n";
7974   outs() << "  nindirectsyms " << dyst.nindirectsyms;
7975   big_size = dyst.nindirectsyms;
7976   big_size *= sizeof(uint32_t);
7977   big_size += dyst.indirectsymoff;
7978   if (big_size > object_size)
7979     outs() << " (past end of file)\n";
7980   else
7981     outs() << "\n";
7982   outs() << "      extreloff " << dyst.extreloff;
7983   if (dyst.extreloff > object_size)
7984     outs() << " (past end of file)\n";
7985   else
7986     outs() << "\n";
7987   outs() << "        nextrel " << dyst.nextrel;
7988   big_size = dyst.nextrel;
7989   big_size *= sizeof(struct MachO::relocation_info);
7990   big_size += dyst.extreloff;
7991   if (big_size > object_size)
7992     outs() << " (past end of file)\n";
7993   else
7994     outs() << "\n";
7995   outs() << "      locreloff " << dyst.locreloff;
7996   if (dyst.locreloff > object_size)
7997     outs() << " (past end of file)\n";
7998   else
7999     outs() << "\n";
8000   outs() << "        nlocrel " << dyst.nlocrel;
8001   big_size = dyst.nlocrel;
8002   big_size *= sizeof(struct MachO::relocation_info);
8003   big_size += dyst.locreloff;
8004   if (big_size > object_size)
8005     outs() << " (past end of file)\n";
8006   else
8007     outs() << "\n";
8008 }
8009
8010 static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
8011                                      uint32_t object_size) {
8012   if (dc.cmd == MachO::LC_DYLD_INFO)
8013     outs() << "            cmd LC_DYLD_INFO\n";
8014   else
8015     outs() << "            cmd LC_DYLD_INFO_ONLY\n";
8016   outs() << "        cmdsize " << dc.cmdsize;
8017   if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
8018     outs() << " Incorrect size\n";
8019   else
8020     outs() << "\n";
8021   outs() << "     rebase_off " << dc.rebase_off;
8022   if (dc.rebase_off > object_size)
8023     outs() << " (past end of file)\n";
8024   else
8025     outs() << "\n";
8026   outs() << "    rebase_size " << dc.rebase_size;
8027   uint64_t big_size;
8028   big_size = dc.rebase_off;
8029   big_size += dc.rebase_size;
8030   if (big_size > object_size)
8031     outs() << " (past end of file)\n";
8032   else
8033     outs() << "\n";
8034   outs() << "       bind_off " << dc.bind_off;
8035   if (dc.bind_off > object_size)
8036     outs() << " (past end of file)\n";
8037   else
8038     outs() << "\n";
8039   outs() << "      bind_size " << dc.bind_size;
8040   big_size = dc.bind_off;
8041   big_size += dc.bind_size;
8042   if (big_size > object_size)
8043     outs() << " (past end of file)\n";
8044   else
8045     outs() << "\n";
8046   outs() << "  weak_bind_off " << dc.weak_bind_off;
8047   if (dc.weak_bind_off > object_size)
8048     outs() << " (past end of file)\n";
8049   else
8050     outs() << "\n";
8051   outs() << " weak_bind_size " << dc.weak_bind_size;
8052   big_size = dc.weak_bind_off;
8053   big_size += dc.weak_bind_size;
8054   if (big_size > object_size)
8055     outs() << " (past end of file)\n";
8056   else
8057     outs() << "\n";
8058   outs() << "  lazy_bind_off " << dc.lazy_bind_off;
8059   if (dc.lazy_bind_off > object_size)
8060     outs() << " (past end of file)\n";
8061   else
8062     outs() << "\n";
8063   outs() << " lazy_bind_size " << dc.lazy_bind_size;
8064   big_size = dc.lazy_bind_off;
8065   big_size += dc.lazy_bind_size;
8066   if (big_size > object_size)
8067     outs() << " (past end of file)\n";
8068   else
8069     outs() << "\n";
8070   outs() << "     export_off " << dc.export_off;
8071   if (dc.export_off > object_size)
8072     outs() << " (past end of file)\n";
8073   else
8074     outs() << "\n";
8075   outs() << "    export_size " << dc.export_size;
8076   big_size = dc.export_off;
8077   big_size += dc.export_size;
8078   if (big_size > object_size)
8079     outs() << " (past end of file)\n";
8080   else
8081     outs() << "\n";
8082 }
8083
8084 static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
8085                                  const char *Ptr) {
8086   if (dyld.cmd == MachO::LC_ID_DYLINKER)
8087     outs() << "          cmd LC_ID_DYLINKER\n";
8088   else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
8089     outs() << "          cmd LC_LOAD_DYLINKER\n";
8090   else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
8091     outs() << "          cmd LC_DYLD_ENVIRONMENT\n";
8092   else
8093     outs() << "          cmd ?(" << dyld.cmd << ")\n";
8094   outs() << "      cmdsize " << dyld.cmdsize;
8095   if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
8096     outs() << " Incorrect size\n";
8097   else
8098     outs() << "\n";
8099   if (dyld.name >= dyld.cmdsize)
8100     outs() << "         name ?(bad offset " << dyld.name << ")\n";
8101   else {
8102     const char *P = (const char *)(Ptr) + dyld.name;
8103     outs() << "         name " << P << " (offset " << dyld.name << ")\n";
8104   }
8105 }
8106
8107 static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
8108   outs() << "     cmd LC_UUID\n";
8109   outs() << " cmdsize " << uuid.cmdsize;
8110   if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
8111     outs() << " Incorrect size\n";
8112   else
8113     outs() << "\n";
8114   outs() << "    uuid ";
8115   for (int i = 0; i < 16; ++i) {
8116     outs() << format("%02" PRIX32, uuid.uuid[i]);
8117     if (i == 3 || i == 5 || i == 7 || i == 9)
8118       outs() << "-";
8119   }
8120   outs() << "\n";
8121 }
8122
8123 static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
8124   outs() << "          cmd LC_RPATH\n";
8125   outs() << "      cmdsize " << rpath.cmdsize;
8126   if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
8127     outs() << " Incorrect size\n";
8128   else
8129     outs() << "\n";
8130   if (rpath.path >= rpath.cmdsize)
8131     outs() << "         path ?(bad offset " << rpath.path << ")\n";
8132   else {
8133     const char *P = (const char *)(Ptr) + rpath.path;
8134     outs() << "         path " << P << " (offset " << rpath.path << ")\n";
8135   }
8136 }
8137
8138 static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
8139   StringRef LoadCmdName;
8140   switch (vd.cmd) {
8141   case MachO::LC_VERSION_MIN_MACOSX:
8142     LoadCmdName = "LC_VERSION_MIN_MACOSX";
8143     break;
8144   case MachO::LC_VERSION_MIN_IPHONEOS:
8145     LoadCmdName = "LC_VERSION_MIN_IPHONEOS";
8146     break;
8147   case MachO::LC_VERSION_MIN_TVOS:
8148     LoadCmdName = "LC_VERSION_MIN_TVOS";
8149     break;
8150   case MachO::LC_VERSION_MIN_WATCHOS:
8151     LoadCmdName = "LC_VERSION_MIN_WATCHOS";
8152     break;
8153   default:
8154     llvm_unreachable("Unknown version min load command");
8155   }
8156
8157   outs() << "      cmd " << LoadCmdName << '\n';
8158   outs() << "  cmdsize " << vd.cmdsize;
8159   if (vd.cmdsize != sizeof(struct MachO::version_min_command))
8160     outs() << " Incorrect size\n";
8161   else
8162     outs() << "\n";
8163   outs() << "  version "
8164          << MachOObjectFile::getVersionMinMajor(vd, false) << "."
8165          << MachOObjectFile::getVersionMinMinor(vd, false);
8166   uint32_t Update = MachOObjectFile::getVersionMinUpdate(vd, false);
8167   if (Update != 0)
8168     outs() << "." << Update;
8169   outs() << "\n";
8170   if (vd.sdk == 0)
8171     outs() << "      sdk n/a";
8172   else {
8173     outs() << "      sdk "
8174            << MachOObjectFile::getVersionMinMajor(vd, true) << "."
8175            << MachOObjectFile::getVersionMinMinor(vd, true);
8176   }
8177   Update = MachOObjectFile::getVersionMinUpdate(vd, true);
8178   if (Update != 0)
8179     outs() << "." << Update;
8180   outs() << "\n";
8181 }
8182
8183 static void PrintSourceVersionCommand(MachO::source_version_command sd) {
8184   outs() << "      cmd LC_SOURCE_VERSION\n";
8185   outs() << "  cmdsize " << sd.cmdsize;
8186   if (sd.cmdsize != sizeof(struct MachO::source_version_command))
8187     outs() << " Incorrect size\n";
8188   else
8189     outs() << "\n";
8190   uint64_t a = (sd.version >> 40) & 0xffffff;
8191   uint64_t b = (sd.version >> 30) & 0x3ff;
8192   uint64_t c = (sd.version >> 20) & 0x3ff;
8193   uint64_t d = (sd.version >> 10) & 0x3ff;
8194   uint64_t e = sd.version & 0x3ff;
8195   outs() << "  version " << a << "." << b;
8196   if (e != 0)
8197     outs() << "." << c << "." << d << "." << e;
8198   else if (d != 0)
8199     outs() << "." << c << "." << d;
8200   else if (c != 0)
8201     outs() << "." << c;
8202   outs() << "\n";
8203 }
8204
8205 static void PrintEntryPointCommand(MachO::entry_point_command ep) {
8206   outs() << "       cmd LC_MAIN\n";
8207   outs() << "   cmdsize " << ep.cmdsize;
8208   if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
8209     outs() << " Incorrect size\n";
8210   else
8211     outs() << "\n";
8212   outs() << "  entryoff " << ep.entryoff << "\n";
8213   outs() << " stacksize " << ep.stacksize << "\n";
8214 }
8215
8216 static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
8217                                        uint32_t object_size) {
8218   outs() << "          cmd LC_ENCRYPTION_INFO\n";
8219   outs() << "      cmdsize " << ec.cmdsize;
8220   if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
8221     outs() << " Incorrect size\n";
8222   else
8223     outs() << "\n";
8224   outs() << "     cryptoff " << ec.cryptoff;
8225   if (ec.cryptoff > object_size)
8226     outs() << " (past end of file)\n";
8227   else
8228     outs() << "\n";
8229   outs() << "    cryptsize " << ec.cryptsize;
8230   if (ec.cryptsize > object_size)
8231     outs() << " (past end of file)\n";
8232   else
8233     outs() << "\n";
8234   outs() << "      cryptid " << ec.cryptid << "\n";
8235 }
8236
8237 static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
8238                                          uint32_t object_size) {
8239   outs() << "          cmd LC_ENCRYPTION_INFO_64\n";
8240   outs() << "      cmdsize " << ec.cmdsize;
8241   if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
8242     outs() << " Incorrect size\n";
8243   else
8244     outs() << "\n";
8245   outs() << "     cryptoff " << ec.cryptoff;
8246   if (ec.cryptoff > object_size)
8247     outs() << " (past end of file)\n";
8248   else
8249     outs() << "\n";
8250   outs() << "    cryptsize " << ec.cryptsize;
8251   if (ec.cryptsize > object_size)
8252     outs() << " (past end of file)\n";
8253   else
8254     outs() << "\n";
8255   outs() << "      cryptid " << ec.cryptid << "\n";
8256   outs() << "          pad " << ec.pad << "\n";
8257 }
8258
8259 static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
8260                                      const char *Ptr) {
8261   outs() << "     cmd LC_LINKER_OPTION\n";
8262   outs() << " cmdsize " << lo.cmdsize;
8263   if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
8264     outs() << " Incorrect size\n";
8265   else
8266     outs() << "\n";
8267   outs() << "   count " << lo.count << "\n";
8268   const char *string = Ptr + sizeof(struct MachO::linker_option_command);
8269   uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
8270   uint32_t i = 0;
8271   while (left > 0) {
8272     while (*string == '\0' && left > 0) {
8273       string++;
8274       left--;
8275     }
8276     if (left > 0) {
8277       i++;
8278       outs() << "  string #" << i << " " << format("%.*s\n", left, string);
8279       uint32_t NullPos = StringRef(string, left).find('\0');
8280       uint32_t len = std::min(NullPos, left) + 1;
8281       string += len;
8282       left -= len;
8283     }
8284   }
8285   if (lo.count != i)
8286     outs() << "   count " << lo.count << " does not match number of strings "
8287            << i << "\n";
8288 }
8289
8290 static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
8291                                      const char *Ptr) {
8292   outs() << "          cmd LC_SUB_FRAMEWORK\n";
8293   outs() << "      cmdsize " << sub.cmdsize;
8294   if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
8295     outs() << " Incorrect size\n";
8296   else
8297     outs() << "\n";
8298   if (sub.umbrella < sub.cmdsize) {
8299     const char *P = Ptr + sub.umbrella;
8300     outs() << "     umbrella " << P << " (offset " << sub.umbrella << ")\n";
8301   } else {
8302     outs() << "     umbrella ?(bad offset " << sub.umbrella << ")\n";
8303   }
8304 }
8305
8306 static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
8307                                     const char *Ptr) {
8308   outs() << "          cmd LC_SUB_UMBRELLA\n";
8309   outs() << "      cmdsize " << sub.cmdsize;
8310   if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
8311     outs() << " Incorrect size\n";
8312   else
8313     outs() << "\n";
8314   if (sub.sub_umbrella < sub.cmdsize) {
8315     const char *P = Ptr + sub.sub_umbrella;
8316     outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
8317   } else {
8318     outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
8319   }
8320 }
8321
8322 static void PrintSubLibraryCommand(MachO::sub_library_command sub,
8323                                    const char *Ptr) {
8324   outs() << "          cmd LC_SUB_LIBRARY\n";
8325   outs() << "      cmdsize " << sub.cmdsize;
8326   if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
8327     outs() << " Incorrect size\n";
8328   else
8329     outs() << "\n";
8330   if (sub.sub_library < sub.cmdsize) {
8331     const char *P = Ptr + sub.sub_library;
8332     outs() << "  sub_library " << P << " (offset " << sub.sub_library << ")\n";
8333   } else {
8334     outs() << "  sub_library ?(bad offset " << sub.sub_library << ")\n";
8335   }
8336 }
8337
8338 static void PrintSubClientCommand(MachO::sub_client_command sub,
8339                                   const char *Ptr) {
8340   outs() << "          cmd LC_SUB_CLIENT\n";
8341   outs() << "      cmdsize " << sub.cmdsize;
8342   if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
8343     outs() << " Incorrect size\n";
8344   else
8345     outs() << "\n";
8346   if (sub.client < sub.cmdsize) {
8347     const char *P = Ptr + sub.client;
8348     outs() << "       client " << P << " (offset " << sub.client << ")\n";
8349   } else {
8350     outs() << "       client ?(bad offset " << sub.client << ")\n";
8351   }
8352 }
8353
8354 static void PrintRoutinesCommand(MachO::routines_command r) {
8355   outs() << "          cmd LC_ROUTINES\n";
8356   outs() << "      cmdsize " << r.cmdsize;
8357   if (r.cmdsize != sizeof(struct MachO::routines_command))
8358     outs() << " Incorrect size\n";
8359   else
8360     outs() << "\n";
8361   outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
8362   outs() << "  init_module " << r.init_module << "\n";
8363   outs() << "    reserved1 " << r.reserved1 << "\n";
8364   outs() << "    reserved2 " << r.reserved2 << "\n";
8365   outs() << "    reserved3 " << r.reserved3 << "\n";
8366   outs() << "    reserved4 " << r.reserved4 << "\n";
8367   outs() << "    reserved5 " << r.reserved5 << "\n";
8368   outs() << "    reserved6 " << r.reserved6 << "\n";
8369 }
8370
8371 static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
8372   outs() << "          cmd LC_ROUTINES_64\n";
8373   outs() << "      cmdsize " << r.cmdsize;
8374   if (r.cmdsize != sizeof(struct MachO::routines_command_64))
8375     outs() << " Incorrect size\n";
8376   else
8377     outs() << "\n";
8378   outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
8379   outs() << "  init_module " << r.init_module << "\n";
8380   outs() << "    reserved1 " << r.reserved1 << "\n";
8381   outs() << "    reserved2 " << r.reserved2 << "\n";
8382   outs() << "    reserved3 " << r.reserved3 << "\n";
8383   outs() << "    reserved4 " << r.reserved4 << "\n";
8384   outs() << "    reserved5 " << r.reserved5 << "\n";
8385   outs() << "    reserved6 " << r.reserved6 << "\n";
8386 }
8387
8388 static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
8389   outs() << "   rax  " << format("0x%016" PRIx64, cpu64.rax);
8390   outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
8391   outs() << " rcx  " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
8392   outs() << "   rdx  " << format("0x%016" PRIx64, cpu64.rdx);
8393   outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
8394   outs() << " rsi  " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
8395   outs() << "   rbp  " << format("0x%016" PRIx64, cpu64.rbp);
8396   outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
8397   outs() << " r8   " << format("0x%016" PRIx64, cpu64.r8) << "\n";
8398   outs() << "    r9  " << format("0x%016" PRIx64, cpu64.r9);
8399   outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
8400   outs() << " r11  " << format("0x%016" PRIx64, cpu64.r11) << "\n";
8401   outs() << "   r12  " << format("0x%016" PRIx64, cpu64.r12);
8402   outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
8403   outs() << " r14  " << format("0x%016" PRIx64, cpu64.r14) << "\n";
8404   outs() << "   r15  " << format("0x%016" PRIx64, cpu64.r15);
8405   outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
8406   outs() << "rflags  " << format("0x%016" PRIx64, cpu64.rflags);
8407   outs() << " cs  " << format("0x%016" PRIx64, cpu64.cs);
8408   outs() << " fs   " << format("0x%016" PRIx64, cpu64.fs) << "\n";
8409   outs() << "    gs  " << format("0x%016" PRIx64, cpu64.gs) << "\n";
8410 }
8411
8412 static void Print_mmst_reg(MachO::mmst_reg_t &r) {
8413   uint32_t f;
8414   outs() << "\t      mmst_reg  ";
8415   for (f = 0; f < 10; f++)
8416     outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
8417   outs() << "\n";
8418   outs() << "\t      mmst_rsrv ";
8419   for (f = 0; f < 6; f++)
8420     outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
8421   outs() << "\n";
8422 }
8423
8424 static void Print_xmm_reg(MachO::xmm_reg_t &r) {
8425   uint32_t f;
8426   outs() << "\t      xmm_reg ";
8427   for (f = 0; f < 16; f++)
8428     outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
8429   outs() << "\n";
8430 }
8431
8432 static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
8433   outs() << "\t    fpu_reserved[0] " << fpu.fpu_reserved[0];
8434   outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
8435   outs() << "\t    control: invalid " << fpu.fpu_fcw.invalid;
8436   outs() << " denorm " << fpu.fpu_fcw.denorm;
8437   outs() << " zdiv " << fpu.fpu_fcw.zdiv;
8438   outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
8439   outs() << " undfl " << fpu.fpu_fcw.undfl;
8440   outs() << " precis " << fpu.fpu_fcw.precis << "\n";
8441   outs() << "\t\t     pc ";
8442   if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
8443     outs() << "FP_PREC_24B ";
8444   else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
8445     outs() << "FP_PREC_53B ";
8446   else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
8447     outs() << "FP_PREC_64B ";
8448   else
8449     outs() << fpu.fpu_fcw.pc << " ";
8450   outs() << "rc ";
8451   if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
8452     outs() << "FP_RND_NEAR ";
8453   else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
8454     outs() << "FP_RND_DOWN ";
8455   else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
8456     outs() << "FP_RND_UP ";
8457   else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
8458     outs() << "FP_CHOP ";
8459   outs() << "\n";
8460   outs() << "\t    status: invalid " << fpu.fpu_fsw.invalid;
8461   outs() << " denorm " << fpu.fpu_fsw.denorm;
8462   outs() << " zdiv " << fpu.fpu_fsw.zdiv;
8463   outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
8464   outs() << " undfl " << fpu.fpu_fsw.undfl;
8465   outs() << " precis " << fpu.fpu_fsw.precis;
8466   outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
8467   outs() << "\t            errsumm " << fpu.fpu_fsw.errsumm;
8468   outs() << " c0 " << fpu.fpu_fsw.c0;
8469   outs() << " c1 " << fpu.fpu_fsw.c1;
8470   outs() << " c2 " << fpu.fpu_fsw.c2;
8471   outs() << " tos " << fpu.fpu_fsw.tos;
8472   outs() << " c3 " << fpu.fpu_fsw.c3;
8473   outs() << " busy " << fpu.fpu_fsw.busy << "\n";
8474   outs() << "\t    fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
8475   outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
8476   outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
8477   outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
8478   outs() << "\t    fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
8479   outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
8480   outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
8481   outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
8482   outs() << "\t    fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
8483   outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
8484   outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
8485   outs() << "\n";
8486   outs() << "\t    fpu_stmm0:\n";
8487   Print_mmst_reg(fpu.fpu_stmm0);
8488   outs() << "\t    fpu_stmm1:\n";
8489   Print_mmst_reg(fpu.fpu_stmm1);
8490   outs() << "\t    fpu_stmm2:\n";
8491   Print_mmst_reg(fpu.fpu_stmm2);
8492   outs() << "\t    fpu_stmm3:\n";
8493   Print_mmst_reg(fpu.fpu_stmm3);
8494   outs() << "\t    fpu_stmm4:\n";
8495   Print_mmst_reg(fpu.fpu_stmm4);
8496   outs() << "\t    fpu_stmm5:\n";
8497   Print_mmst_reg(fpu.fpu_stmm5);
8498   outs() << "\t    fpu_stmm6:\n";
8499   Print_mmst_reg(fpu.fpu_stmm6);
8500   outs() << "\t    fpu_stmm7:\n";
8501   Print_mmst_reg(fpu.fpu_stmm7);
8502   outs() << "\t    fpu_xmm0:\n";
8503   Print_xmm_reg(fpu.fpu_xmm0);
8504   outs() << "\t    fpu_xmm1:\n";
8505   Print_xmm_reg(fpu.fpu_xmm1);
8506   outs() << "\t    fpu_xmm2:\n";
8507   Print_xmm_reg(fpu.fpu_xmm2);
8508   outs() << "\t    fpu_xmm3:\n";
8509   Print_xmm_reg(fpu.fpu_xmm3);
8510   outs() << "\t    fpu_xmm4:\n";
8511   Print_xmm_reg(fpu.fpu_xmm4);
8512   outs() << "\t    fpu_xmm5:\n";
8513   Print_xmm_reg(fpu.fpu_xmm5);
8514   outs() << "\t    fpu_xmm6:\n";
8515   Print_xmm_reg(fpu.fpu_xmm6);
8516   outs() << "\t    fpu_xmm7:\n";
8517   Print_xmm_reg(fpu.fpu_xmm7);
8518   outs() << "\t    fpu_xmm8:\n";
8519   Print_xmm_reg(fpu.fpu_xmm8);
8520   outs() << "\t    fpu_xmm9:\n";
8521   Print_xmm_reg(fpu.fpu_xmm9);
8522   outs() << "\t    fpu_xmm10:\n";
8523   Print_xmm_reg(fpu.fpu_xmm10);
8524   outs() << "\t    fpu_xmm11:\n";
8525   Print_xmm_reg(fpu.fpu_xmm11);
8526   outs() << "\t    fpu_xmm12:\n";
8527   Print_xmm_reg(fpu.fpu_xmm12);
8528   outs() << "\t    fpu_xmm13:\n";
8529   Print_xmm_reg(fpu.fpu_xmm13);
8530   outs() << "\t    fpu_xmm14:\n";
8531   Print_xmm_reg(fpu.fpu_xmm14);
8532   outs() << "\t    fpu_xmm15:\n";
8533   Print_xmm_reg(fpu.fpu_xmm15);
8534   outs() << "\t    fpu_rsrv4:\n";
8535   for (uint32_t f = 0; f < 6; f++) {
8536     outs() << "\t            ";
8537     for (uint32_t g = 0; g < 16; g++)
8538       outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
8539     outs() << "\n";
8540   }
8541   outs() << "\t    fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
8542   outs() << "\n";
8543 }
8544
8545 static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
8546   outs() << "\t    trapno " << format("0x%08" PRIx32, exc64.trapno);
8547   outs() << " err " << format("0x%08" PRIx32, exc64.err);
8548   outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
8549 }
8550
8551 static void Print_arm_thread_state32_t(MachO::arm_thread_state32_t &cpu32) {
8552   outs() << "\t    r0  " << format("0x%08" PRIx32, cpu32.r[0]);
8553   outs() << " r1     "   << format("0x%08" PRIx32, cpu32.r[1]);
8554   outs() << " r2  "      << format("0x%08" PRIx32, cpu32.r[2]);
8555   outs() << " r3  "      << format("0x%08" PRIx32, cpu32.r[3]) << "\n";
8556   outs() << "\t    r4  " << format("0x%08" PRIx32, cpu32.r[4]);
8557   outs() << " r5     "   << format("0x%08" PRIx32, cpu32.r[5]);
8558   outs() << " r6  "      << format("0x%08" PRIx32, cpu32.r[6]);
8559   outs() << " r7  "      << format("0x%08" PRIx32, cpu32.r[7]) << "\n";
8560   outs() << "\t    r8  " << format("0x%08" PRIx32, cpu32.r[8]);
8561   outs() << " r9     "   << format("0x%08" PRIx32, cpu32.r[9]);
8562   outs() << " r10 "      << format("0x%08" PRIx32, cpu32.r[10]);
8563   outs() << " r11 "      << format("0x%08" PRIx32, cpu32.r[11]) << "\n";
8564   outs() << "\t    r12 " << format("0x%08" PRIx32, cpu32.r[12]);
8565   outs() << " sp     "   << format("0x%08" PRIx32, cpu32.sp);
8566   outs() << " lr  "      << format("0x%08" PRIx32, cpu32.lr);
8567   outs() << " pc  "      << format("0x%08" PRIx32, cpu32.pc) << "\n";
8568   outs() << "\t   cpsr " << format("0x%08" PRIx32, cpu32.cpsr) << "\n";
8569 }
8570
8571 static void Print_arm_thread_state64_t(MachO::arm_thread_state64_t &cpu64) {
8572   outs() << "\t    x0  " << format("0x%016" PRIx64, cpu64.x[0]);
8573   outs() << " x1  "      << format("0x%016" PRIx64, cpu64.x[1]);
8574   outs() << " x2  "      << format("0x%016" PRIx64, cpu64.x[2]) << "\n";
8575   outs() << "\t    x3  " << format("0x%016" PRIx64, cpu64.x[3]);
8576   outs() << " x4  "      << format("0x%016" PRIx64, cpu64.x[4]);
8577   outs() << " x5  "      << format("0x%016" PRIx64, cpu64.x[5]) << "\n";
8578   outs() << "\t    x6  " << format("0x%016" PRIx64, cpu64.x[6]);
8579   outs() << " x7  "      << format("0x%016" PRIx64, cpu64.x[7]);
8580   outs() << " x8  "      << format("0x%016" PRIx64, cpu64.x[8]) << "\n";
8581   outs() << "\t    x9  " << format("0x%016" PRIx64, cpu64.x[9]);
8582   outs() << " x10 "      << format("0x%016" PRIx64, cpu64.x[10]);
8583   outs() << " x11 "      << format("0x%016" PRIx64, cpu64.x[11]) << "\n";
8584   outs() << "\t    x12 " << format("0x%016" PRIx64, cpu64.x[12]);
8585   outs() << " x13 "      << format("0x%016" PRIx64, cpu64.x[13]);
8586   outs() << " x14 "      << format("0x%016" PRIx64, cpu64.x[14]) << "\n";
8587   outs() << "\t    x15 " << format("0x%016" PRIx64, cpu64.x[15]);
8588   outs() << " x16 "      << format("0x%016" PRIx64, cpu64.x[16]);
8589   outs() << " x17 "      << format("0x%016" PRIx64, cpu64.x[17]) << "\n";
8590   outs() << "\t    x18 " << format("0x%016" PRIx64, cpu64.x[18]);
8591   outs() << " x19 "      << format("0x%016" PRIx64, cpu64.x[19]);
8592   outs() << " x20 "      << format("0x%016" PRIx64, cpu64.x[20]) << "\n";
8593   outs() << "\t    x21 " << format("0x%016" PRIx64, cpu64.x[21]);
8594   outs() << " x22 "      << format("0x%016" PRIx64, cpu64.x[22]);
8595   outs() << " x23 "      << format("0x%016" PRIx64, cpu64.x[23]) << "\n";
8596   outs() << "\t    x24 " << format("0x%016" PRIx64, cpu64.x[24]);
8597   outs() << " x25 "      << format("0x%016" PRIx64, cpu64.x[25]);
8598   outs() << " x26 "      << format("0x%016" PRIx64, cpu64.x[26]) << "\n";
8599   outs() << "\t    x27 " << format("0x%016" PRIx64, cpu64.x[27]);
8600   outs() << " x28 "      << format("0x%016" PRIx64, cpu64.x[28]);
8601   outs() << "  fp "      << format("0x%016" PRIx64, cpu64.fp) << "\n";
8602   outs() << "\t     lr " << format("0x%016" PRIx64, cpu64.lr);
8603   outs() << " sp  "      << format("0x%016" PRIx64, cpu64.sp);
8604   outs() << "  pc "      << format("0x%016" PRIx64, cpu64.pc) << "\n";
8605   outs() << "\t   cpsr " << format("0x%08"  PRIx32, cpu64.cpsr) << "\n";
8606 }
8607
8608 static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
8609                                bool isLittleEndian, uint32_t cputype) {
8610   if (t.cmd == MachO::LC_THREAD)
8611     outs() << "        cmd LC_THREAD\n";
8612   else if (t.cmd == MachO::LC_UNIXTHREAD)
8613     outs() << "        cmd LC_UNIXTHREAD\n";
8614   else
8615     outs() << "        cmd " << t.cmd << " (unknown)\n";
8616   outs() << "    cmdsize " << t.cmdsize;
8617   if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
8618     outs() << " Incorrect size\n";
8619   else
8620     outs() << "\n";
8621
8622   const char *begin = Ptr + sizeof(struct MachO::thread_command);
8623   const char *end = Ptr + t.cmdsize;
8624   uint32_t flavor, count, left;
8625   if (cputype == MachO::CPU_TYPE_X86_64) {
8626     while (begin < end) {
8627       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8628         memcpy((char *)&flavor, begin, sizeof(uint32_t));
8629         begin += sizeof(uint32_t);
8630       } else {
8631         flavor = 0;
8632         begin = end;
8633       }
8634       if (isLittleEndian != sys::IsLittleEndianHost)
8635         sys::swapByteOrder(flavor);
8636       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8637         memcpy((char *)&count, begin, sizeof(uint32_t));
8638         begin += sizeof(uint32_t);
8639       } else {
8640         count = 0;
8641         begin = end;
8642       }
8643       if (isLittleEndian != sys::IsLittleEndianHost)
8644         sys::swapByteOrder(count);
8645       if (flavor == MachO::x86_THREAD_STATE64) {
8646         outs() << "     flavor x86_THREAD_STATE64\n";
8647         if (count == MachO::x86_THREAD_STATE64_COUNT)
8648           outs() << "      count x86_THREAD_STATE64_COUNT\n";
8649         else
8650           outs() << "      count " << count
8651                  << " (not x86_THREAD_STATE64_COUNT)\n";
8652         MachO::x86_thread_state64_t cpu64;
8653         left = end - begin;
8654         if (left >= sizeof(MachO::x86_thread_state64_t)) {
8655           memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
8656           begin += sizeof(MachO::x86_thread_state64_t);
8657         } else {
8658           memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
8659           memcpy(&cpu64, begin, left);
8660           begin += left;
8661         }
8662         if (isLittleEndian != sys::IsLittleEndianHost)
8663           swapStruct(cpu64);
8664         Print_x86_thread_state64_t(cpu64);
8665       } else if (flavor == MachO::x86_THREAD_STATE) {
8666         outs() << "     flavor x86_THREAD_STATE\n";
8667         if (count == MachO::x86_THREAD_STATE_COUNT)
8668           outs() << "      count x86_THREAD_STATE_COUNT\n";
8669         else
8670           outs() << "      count " << count
8671                  << " (not x86_THREAD_STATE_COUNT)\n";
8672         struct MachO::x86_thread_state_t ts;
8673         left = end - begin;
8674         if (left >= sizeof(MachO::x86_thread_state_t)) {
8675           memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
8676           begin += sizeof(MachO::x86_thread_state_t);
8677         } else {
8678           memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
8679           memcpy(&ts, begin, left);
8680           begin += left;
8681         }
8682         if (isLittleEndian != sys::IsLittleEndianHost)
8683           swapStruct(ts);
8684         if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
8685           outs() << "\t    tsh.flavor x86_THREAD_STATE64 ";
8686           if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
8687             outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
8688           else
8689             outs() << "tsh.count " << ts.tsh.count
8690                    << " (not x86_THREAD_STATE64_COUNT\n";
8691           Print_x86_thread_state64_t(ts.uts.ts64);
8692         } else {
8693           outs() << "\t    tsh.flavor " << ts.tsh.flavor << "  tsh.count "
8694                  << ts.tsh.count << "\n";
8695         }
8696       } else if (flavor == MachO::x86_FLOAT_STATE) {
8697         outs() << "     flavor x86_FLOAT_STATE\n";
8698         if (count == MachO::x86_FLOAT_STATE_COUNT)
8699           outs() << "      count x86_FLOAT_STATE_COUNT\n";
8700         else
8701           outs() << "      count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
8702         struct MachO::x86_float_state_t fs;
8703         left = end - begin;
8704         if (left >= sizeof(MachO::x86_float_state_t)) {
8705           memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
8706           begin += sizeof(MachO::x86_float_state_t);
8707         } else {
8708           memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
8709           memcpy(&fs, begin, left);
8710           begin += left;
8711         }
8712         if (isLittleEndian != sys::IsLittleEndianHost)
8713           swapStruct(fs);
8714         if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
8715           outs() << "\t    fsh.flavor x86_FLOAT_STATE64 ";
8716           if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
8717             outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
8718           else
8719             outs() << "fsh.count " << fs.fsh.count
8720                    << " (not x86_FLOAT_STATE64_COUNT\n";
8721           Print_x86_float_state_t(fs.ufs.fs64);
8722         } else {
8723           outs() << "\t    fsh.flavor " << fs.fsh.flavor << "  fsh.count "
8724                  << fs.fsh.count << "\n";
8725         }
8726       } else if (flavor == MachO::x86_EXCEPTION_STATE) {
8727         outs() << "     flavor x86_EXCEPTION_STATE\n";
8728         if (count == MachO::x86_EXCEPTION_STATE_COUNT)
8729           outs() << "      count x86_EXCEPTION_STATE_COUNT\n";
8730         else
8731           outs() << "      count " << count
8732                  << " (not x86_EXCEPTION_STATE_COUNT)\n";
8733         struct MachO::x86_exception_state_t es;
8734         left = end - begin;
8735         if (left >= sizeof(MachO::x86_exception_state_t)) {
8736           memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
8737           begin += sizeof(MachO::x86_exception_state_t);
8738         } else {
8739           memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
8740           memcpy(&es, begin, left);
8741           begin += left;
8742         }
8743         if (isLittleEndian != sys::IsLittleEndianHost)
8744           swapStruct(es);
8745         if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
8746           outs() << "\t    esh.flavor x86_EXCEPTION_STATE64\n";
8747           if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
8748             outs() << "\t    esh.count x86_EXCEPTION_STATE64_COUNT\n";
8749           else
8750             outs() << "\t    esh.count " << es.esh.count
8751                    << " (not x86_EXCEPTION_STATE64_COUNT\n";
8752           Print_x86_exception_state_t(es.ues.es64);
8753         } else {
8754           outs() << "\t    esh.flavor " << es.esh.flavor << "  esh.count "
8755                  << es.esh.count << "\n";
8756         }
8757       } else {
8758         outs() << "     flavor " << flavor << " (unknown)\n";
8759         outs() << "      count " << count << "\n";
8760         outs() << "      state (unknown)\n";
8761         begin += count * sizeof(uint32_t);
8762       }
8763     }
8764   } else if (cputype == MachO::CPU_TYPE_ARM) {
8765     while (begin < end) {
8766       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8767         memcpy((char *)&flavor, begin, sizeof(uint32_t));
8768         begin += sizeof(uint32_t);
8769       } else {
8770         flavor = 0;
8771         begin = end;
8772       }
8773       if (isLittleEndian != sys::IsLittleEndianHost)
8774         sys::swapByteOrder(flavor);
8775       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8776         memcpy((char *)&count, begin, sizeof(uint32_t));
8777         begin += sizeof(uint32_t);
8778       } else {
8779         count = 0;
8780         begin = end;
8781       }
8782       if (isLittleEndian != sys::IsLittleEndianHost)
8783         sys::swapByteOrder(count);
8784       if (flavor == MachO::ARM_THREAD_STATE) {
8785         outs() << "     flavor ARM_THREAD_STATE\n";
8786         if (count == MachO::ARM_THREAD_STATE_COUNT)
8787           outs() << "      count ARM_THREAD_STATE_COUNT\n";
8788         else
8789           outs() << "      count " << count
8790                  << " (not ARM_THREAD_STATE_COUNT)\n";
8791         MachO::arm_thread_state32_t cpu32;
8792         left = end - begin;
8793         if (left >= sizeof(MachO::arm_thread_state32_t)) {
8794           memcpy(&cpu32, begin, sizeof(MachO::arm_thread_state32_t));
8795           begin += sizeof(MachO::arm_thread_state32_t);
8796         } else {
8797           memset(&cpu32, '\0', sizeof(MachO::arm_thread_state32_t));
8798           memcpy(&cpu32, begin, left);
8799           begin += left;
8800         }
8801         if (isLittleEndian != sys::IsLittleEndianHost)
8802           swapStruct(cpu32);
8803         Print_arm_thread_state32_t(cpu32);
8804       } else {
8805         outs() << "     flavor " << flavor << " (unknown)\n";
8806         outs() << "      count " << count << "\n";
8807         outs() << "      state (unknown)\n";
8808         begin += count * sizeof(uint32_t);
8809       }
8810     }
8811   } else if (cputype == MachO::CPU_TYPE_ARM64) {
8812     while (begin < end) {
8813       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8814         memcpy((char *)&flavor, begin, sizeof(uint32_t));
8815         begin += sizeof(uint32_t);
8816       } else {
8817         flavor = 0;
8818         begin = end;
8819       }
8820       if (isLittleEndian != sys::IsLittleEndianHost)
8821         sys::swapByteOrder(flavor);
8822       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8823         memcpy((char *)&count, begin, sizeof(uint32_t));
8824         begin += sizeof(uint32_t);
8825       } else {
8826         count = 0;
8827         begin = end;
8828       }
8829       if (isLittleEndian != sys::IsLittleEndianHost)
8830         sys::swapByteOrder(count);
8831       if (flavor == MachO::ARM_THREAD_STATE64) {
8832         outs() << "     flavor ARM_THREAD_STATE64\n";
8833         if (count == MachO::ARM_THREAD_STATE64_COUNT)
8834           outs() << "      count ARM_THREAD_STATE64_COUNT\n";
8835         else
8836           outs() << "      count " << count
8837                  << " (not ARM_THREAD_STATE64_COUNT)\n";
8838         MachO::arm_thread_state64_t cpu64;
8839         left = end - begin;
8840         if (left >= sizeof(MachO::arm_thread_state64_t)) {
8841           memcpy(&cpu64, begin, sizeof(MachO::arm_thread_state64_t));
8842           begin += sizeof(MachO::arm_thread_state64_t);
8843         } else {
8844           memset(&cpu64, '\0', sizeof(MachO::arm_thread_state64_t));
8845           memcpy(&cpu64, begin, left);
8846           begin += left;
8847         }
8848         if (isLittleEndian != sys::IsLittleEndianHost)
8849           swapStruct(cpu64);
8850         Print_arm_thread_state64_t(cpu64);
8851       } else {
8852         outs() << "     flavor " << flavor << " (unknown)\n";
8853         outs() << "      count " << count << "\n";
8854         outs() << "      state (unknown)\n";
8855         begin += count * sizeof(uint32_t);
8856       }
8857     }
8858   } else {
8859     while (begin < end) {
8860       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8861         memcpy((char *)&flavor, begin, sizeof(uint32_t));
8862         begin += sizeof(uint32_t);
8863       } else {
8864         flavor = 0;
8865         begin = end;
8866       }
8867       if (isLittleEndian != sys::IsLittleEndianHost)
8868         sys::swapByteOrder(flavor);
8869       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8870         memcpy((char *)&count, begin, sizeof(uint32_t));
8871         begin += sizeof(uint32_t);
8872       } else {
8873         count = 0;
8874         begin = end;
8875       }
8876       if (isLittleEndian != sys::IsLittleEndianHost)
8877         sys::swapByteOrder(count);
8878       outs() << "     flavor " << flavor << "\n";
8879       outs() << "      count " << count << "\n";
8880       outs() << "      state (Unknown cputype/cpusubtype)\n";
8881       begin += count * sizeof(uint32_t);
8882     }
8883   }
8884 }
8885
8886 static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
8887   if (dl.cmd == MachO::LC_ID_DYLIB)
8888     outs() << "          cmd LC_ID_DYLIB\n";
8889   else if (dl.cmd == MachO::LC_LOAD_DYLIB)
8890     outs() << "          cmd LC_LOAD_DYLIB\n";
8891   else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
8892     outs() << "          cmd LC_LOAD_WEAK_DYLIB\n";
8893   else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
8894     outs() << "          cmd LC_REEXPORT_DYLIB\n";
8895   else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
8896     outs() << "          cmd LC_LAZY_LOAD_DYLIB\n";
8897   else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
8898     outs() << "          cmd LC_LOAD_UPWARD_DYLIB\n";
8899   else
8900     outs() << "          cmd " << dl.cmd << " (unknown)\n";
8901   outs() << "      cmdsize " << dl.cmdsize;
8902   if (dl.cmdsize < sizeof(struct MachO::dylib_command))
8903     outs() << " Incorrect size\n";
8904   else
8905     outs() << "\n";
8906   if (dl.dylib.name < dl.cmdsize) {
8907     const char *P = (const char *)(Ptr) + dl.dylib.name;
8908     outs() << "         name " << P << " (offset " << dl.dylib.name << ")\n";
8909   } else {
8910     outs() << "         name ?(bad offset " << dl.dylib.name << ")\n";
8911   }
8912   outs() << "   time stamp " << dl.dylib.timestamp << " ";
8913   time_t t = dl.dylib.timestamp;
8914   outs() << ctime(&t);
8915   outs() << "      current version ";
8916   if (dl.dylib.current_version == 0xffffffff)
8917     outs() << "n/a\n";
8918   else
8919     outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
8920            << ((dl.dylib.current_version >> 8) & 0xff) << "."
8921            << (dl.dylib.current_version & 0xff) << "\n";
8922   outs() << "compatibility version ";
8923   if (dl.dylib.compatibility_version == 0xffffffff)
8924     outs() << "n/a\n";
8925   else
8926     outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
8927            << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
8928            << (dl.dylib.compatibility_version & 0xff) << "\n";
8929 }
8930
8931 static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
8932                                      uint32_t object_size) {
8933   if (ld.cmd == MachO::LC_CODE_SIGNATURE)
8934     outs() << "      cmd LC_CODE_SIGNATURE\n";
8935   else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
8936     outs() << "      cmd LC_SEGMENT_SPLIT_INFO\n";
8937   else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
8938     outs() << "      cmd LC_FUNCTION_STARTS\n";
8939   else if (ld.cmd == MachO::LC_DATA_IN_CODE)
8940     outs() << "      cmd LC_DATA_IN_CODE\n";
8941   else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
8942     outs() << "      cmd LC_DYLIB_CODE_SIGN_DRS\n";
8943   else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
8944     outs() << "      cmd LC_LINKER_OPTIMIZATION_HINT\n";
8945   else
8946     outs() << "      cmd " << ld.cmd << " (?)\n";
8947   outs() << "  cmdsize " << ld.cmdsize;
8948   if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
8949     outs() << " Incorrect size\n";
8950   else
8951     outs() << "\n";
8952   outs() << "  dataoff " << ld.dataoff;
8953   if (ld.dataoff > object_size)
8954     outs() << " (past end of file)\n";
8955   else
8956     outs() << "\n";
8957   outs() << " datasize " << ld.datasize;
8958   uint64_t big_size = ld.dataoff;
8959   big_size += ld.datasize;
8960   if (big_size > object_size)
8961     outs() << " (past end of file)\n";
8962   else
8963     outs() << "\n";
8964 }
8965
8966 static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
8967                               uint32_t cputype, bool verbose) {
8968   StringRef Buf = Obj->getData();
8969   unsigned Index = 0;
8970   for (const auto &Command : Obj->load_commands()) {
8971     outs() << "Load command " << Index++ << "\n";
8972     if (Command.C.cmd == MachO::LC_SEGMENT) {
8973       MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
8974       const char *sg_segname = SLC.segname;
8975       PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
8976                           SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
8977                           SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
8978                           verbose);
8979       for (unsigned j = 0; j < SLC.nsects; j++) {
8980         MachO::section S = Obj->getSection(Command, j);
8981         PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
8982                      S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
8983                      SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
8984       }
8985     } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
8986       MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
8987       const char *sg_segname = SLC_64.segname;
8988       PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
8989                           SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
8990                           SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
8991                           SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
8992       for (unsigned j = 0; j < SLC_64.nsects; j++) {
8993         MachO::section_64 S_64 = Obj->getSection64(Command, j);
8994         PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
8995                      S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
8996                      S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
8997                      sg_segname, filetype, Buf.size(), verbose);
8998       }
8999     } else if (Command.C.cmd == MachO::LC_SYMTAB) {
9000       MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
9001       PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
9002     } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
9003       MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
9004       MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
9005       PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
9006                                Obj->is64Bit());
9007     } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
9008                Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
9009       MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
9010       PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
9011     } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
9012                Command.C.cmd == MachO::LC_ID_DYLINKER ||
9013                Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
9014       MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
9015       PrintDyldLoadCommand(Dyld, Command.Ptr);
9016     } else if (Command.C.cmd == MachO::LC_UUID) {
9017       MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
9018       PrintUuidLoadCommand(Uuid);
9019     } else if (Command.C.cmd == MachO::LC_RPATH) {
9020       MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
9021       PrintRpathLoadCommand(Rpath, Command.Ptr);
9022     } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
9023                Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS ||
9024                Command.C.cmd == MachO::LC_VERSION_MIN_TVOS ||
9025                Command.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) {
9026       MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
9027       PrintVersionMinLoadCommand(Vd);
9028     } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
9029       MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
9030       PrintSourceVersionCommand(Sd);
9031     } else if (Command.C.cmd == MachO::LC_MAIN) {
9032       MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
9033       PrintEntryPointCommand(Ep);
9034     } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
9035       MachO::encryption_info_command Ei =
9036           Obj->getEncryptionInfoCommand(Command);
9037       PrintEncryptionInfoCommand(Ei, Buf.size());
9038     } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
9039       MachO::encryption_info_command_64 Ei =
9040           Obj->getEncryptionInfoCommand64(Command);
9041       PrintEncryptionInfoCommand64(Ei, Buf.size());
9042     } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
9043       MachO::linker_option_command Lo =
9044           Obj->getLinkerOptionLoadCommand(Command);
9045       PrintLinkerOptionCommand(Lo, Command.Ptr);
9046     } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
9047       MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
9048       PrintSubFrameworkCommand(Sf, Command.Ptr);
9049     } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
9050       MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
9051       PrintSubUmbrellaCommand(Sf, Command.Ptr);
9052     } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
9053       MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
9054       PrintSubLibraryCommand(Sl, Command.Ptr);
9055     } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
9056       MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
9057       PrintSubClientCommand(Sc, Command.Ptr);
9058     } else if (Command.C.cmd == MachO::LC_ROUTINES) {
9059       MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
9060       PrintRoutinesCommand(Rc);
9061     } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
9062       MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
9063       PrintRoutinesCommand64(Rc);
9064     } else if (Command.C.cmd == MachO::LC_THREAD ||
9065                Command.C.cmd == MachO::LC_UNIXTHREAD) {
9066       MachO::thread_command Tc = Obj->getThreadCommand(Command);
9067       PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
9068     } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
9069                Command.C.cmd == MachO::LC_ID_DYLIB ||
9070                Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
9071                Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
9072                Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
9073                Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
9074       MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
9075       PrintDylibCommand(Dl, Command.Ptr);
9076     } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
9077                Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
9078                Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
9079                Command.C.cmd == MachO::LC_DATA_IN_CODE ||
9080                Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
9081                Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
9082       MachO::linkedit_data_command Ld =
9083           Obj->getLinkeditDataLoadCommand(Command);
9084       PrintLinkEditDataCommand(Ld, Buf.size());
9085     } else {
9086       outs() << "      cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
9087              << ")\n";
9088       outs() << "  cmdsize " << Command.C.cmdsize << "\n";
9089       // TODO: get and print the raw bytes of the load command.
9090     }
9091     // TODO: print all the other kinds of load commands.
9092   }
9093 }
9094
9095 static void PrintMachHeader(const MachOObjectFile *Obj, bool verbose) {
9096   if (Obj->is64Bit()) {
9097     MachO::mach_header_64 H_64;
9098     H_64 = Obj->getHeader64();
9099     PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
9100                     H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
9101   } else {
9102     MachO::mach_header H;
9103     H = Obj->getHeader();
9104     PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
9105                     H.sizeofcmds, H.flags, verbose);
9106   }
9107 }
9108
9109 void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
9110   const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
9111   PrintMachHeader(file, !NonVerbose);
9112 }
9113
9114 void llvm::printMachOLoadCommands(const object::ObjectFile *Obj) {
9115   const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
9116   uint32_t filetype = 0;
9117   uint32_t cputype = 0;
9118   if (file->is64Bit()) {
9119     MachO::mach_header_64 H_64;
9120     H_64 = file->getHeader64();
9121     filetype = H_64.filetype;
9122     cputype = H_64.cputype;
9123   } else {
9124     MachO::mach_header H;
9125     H = file->getHeader();
9126     filetype = H.filetype;
9127     cputype = H.cputype;
9128   }
9129   PrintLoadCommands(file, filetype, cputype, !NonVerbose);
9130 }
9131
9132 //===----------------------------------------------------------------------===//
9133 // export trie dumping
9134 //===----------------------------------------------------------------------===//
9135
9136 void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
9137   for (const llvm::object::ExportEntry &Entry : Obj->exports()) {
9138     uint64_t Flags = Entry.flags();
9139     bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
9140     bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
9141     bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
9142                         MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
9143     bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
9144                 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
9145     bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
9146     if (ReExport)
9147       outs() << "[re-export] ";
9148     else
9149       outs() << format("0x%08llX  ",
9150                        Entry.address()); // FIXME:add in base address
9151     outs() << Entry.name();
9152     if (WeakDef || ThreadLocal || Resolver || Abs) {
9153       bool NeedsComma = false;
9154       outs() << " [";
9155       if (WeakDef) {
9156         outs() << "weak_def";
9157         NeedsComma = true;
9158       }
9159       if (ThreadLocal) {
9160         if (NeedsComma)
9161           outs() << ", ";
9162         outs() << "per-thread";
9163         NeedsComma = true;
9164       }
9165       if (Abs) {
9166         if (NeedsComma)
9167           outs() << ", ";
9168         outs() << "absolute";
9169         NeedsComma = true;
9170       }
9171       if (Resolver) {
9172         if (NeedsComma)
9173           outs() << ", ";
9174         outs() << format("resolver=0x%08llX", Entry.other());
9175         NeedsComma = true;
9176       }
9177       outs() << "]";
9178     }
9179     if (ReExport) {
9180       StringRef DylibName = "unknown";
9181       int Ordinal = Entry.other() - 1;
9182       Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
9183       if (Entry.otherName().empty())
9184         outs() << " (from " << DylibName << ")";
9185       else
9186         outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
9187     }
9188     outs() << "\n";
9189   }
9190 }
9191
9192 //===----------------------------------------------------------------------===//
9193 // rebase table dumping
9194 //===----------------------------------------------------------------------===//
9195
9196 namespace {
9197 class SegInfo {
9198 public:
9199   SegInfo(const object::MachOObjectFile *Obj);
9200
9201   StringRef segmentName(uint32_t SegIndex);
9202   StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset);
9203   uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
9204   bool isValidSegIndexAndOffset(uint32_t SegIndex, uint64_t SegOffset);
9205
9206 private:
9207   struct SectionInfo {
9208     uint64_t Address;
9209     uint64_t Size;
9210     StringRef SectionName;
9211     StringRef SegmentName;
9212     uint64_t OffsetInSegment;
9213     uint64_t SegmentStartAddress;
9214     uint32_t SegmentIndex;
9215   };
9216   const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset);
9217   SmallVector<SectionInfo, 32> Sections;
9218 };
9219 }
9220
9221 SegInfo::SegInfo(const object::MachOObjectFile *Obj) {
9222   // Build table of sections so segIndex/offset pairs can be translated.
9223   uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
9224   StringRef CurSegName;
9225   uint64_t CurSegAddress;
9226   for (const SectionRef &Section : Obj->sections()) {
9227     SectionInfo Info;
9228     error(Section.getName(Info.SectionName));
9229     Info.Address = Section.getAddress();
9230     Info.Size = Section.getSize();
9231     Info.SegmentName =
9232         Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
9233     if (!Info.SegmentName.equals(CurSegName)) {
9234       ++CurSegIndex;
9235       CurSegName = Info.SegmentName;
9236       CurSegAddress = Info.Address;
9237     }
9238     Info.SegmentIndex = CurSegIndex - 1;
9239     Info.OffsetInSegment = Info.Address - CurSegAddress;
9240     Info.SegmentStartAddress = CurSegAddress;
9241     Sections.push_back(Info);
9242   }
9243 }
9244
9245 StringRef SegInfo::segmentName(uint32_t SegIndex) {
9246   for (const SectionInfo &SI : Sections) {
9247     if (SI.SegmentIndex == SegIndex)
9248       return SI.SegmentName;
9249   }
9250   llvm_unreachable("invalid segIndex");
9251 }
9252
9253 bool SegInfo::isValidSegIndexAndOffset(uint32_t SegIndex,
9254                                        uint64_t OffsetInSeg) {
9255   for (const SectionInfo &SI : Sections) {
9256     if (SI.SegmentIndex != SegIndex)
9257       continue;
9258     if (SI.OffsetInSegment > OffsetInSeg)
9259       continue;
9260     if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
9261       continue;
9262     return true;
9263   }
9264   return false;
9265 }
9266
9267 const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex,
9268                                                  uint64_t OffsetInSeg) {
9269   for (const SectionInfo &SI : Sections) {
9270     if (SI.SegmentIndex != SegIndex)
9271       continue;
9272     if (SI.OffsetInSegment > OffsetInSeg)
9273       continue;
9274     if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
9275       continue;
9276     return SI;
9277   }
9278   llvm_unreachable("segIndex and offset not in any section");
9279 }
9280
9281 StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) {
9282   return findSection(SegIndex, OffsetInSeg).SectionName;
9283 }
9284
9285 uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) {
9286   const SectionInfo &SI = findSection(SegIndex, OffsetInSeg);
9287   return SI.SegmentStartAddress + OffsetInSeg;
9288 }
9289
9290 void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) {
9291   // Build table of sections so names can used in final output.
9292   SegInfo sectionTable(Obj);
9293
9294   outs() << "segment  section            address     type\n";
9295   for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) {
9296     uint32_t SegIndex = Entry.segmentIndex();
9297     uint64_t OffsetInSeg = Entry.segmentOffset();
9298     StringRef SegmentName = sectionTable.segmentName(SegIndex);
9299     StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
9300     uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
9301
9302     // Table lines look like: __DATA  __nl_symbol_ptr  0x0000F00C  pointer
9303     outs() << format("%-8s %-18s 0x%08" PRIX64 "  %s\n",
9304                      SegmentName.str().c_str(), SectionName.str().c_str(),
9305                      Address, Entry.typeName().str().c_str());
9306   }
9307 }
9308
9309 static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
9310   StringRef DylibName;
9311   switch (Ordinal) {
9312   case MachO::BIND_SPECIAL_DYLIB_SELF:
9313     return "this-image";
9314   case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
9315     return "main-executable";
9316   case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
9317     return "flat-namespace";
9318   default:
9319     if (Ordinal > 0) {
9320       std::error_code EC =
9321           Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
9322       if (EC)
9323         return "<<bad library ordinal>>";
9324       return DylibName;
9325     }
9326   }
9327   return "<<unknown special ordinal>>";
9328 }
9329
9330 //===----------------------------------------------------------------------===//
9331 // bind table dumping
9332 //===----------------------------------------------------------------------===//
9333
9334 void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) {
9335   // Build table of sections so names can used in final output.
9336   SegInfo sectionTable(Obj);
9337
9338   outs() << "segment  section            address    type       "
9339             "addend dylib            symbol\n";
9340   for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) {
9341     uint32_t SegIndex = Entry.segmentIndex();
9342     uint64_t OffsetInSeg = Entry.segmentOffset();
9343     StringRef SegmentName = sectionTable.segmentName(SegIndex);
9344     StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
9345     uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
9346
9347     // Table lines look like:
9348     //  __DATA  __got  0x00012010    pointer   0 libSystem ___stack_chk_guard
9349     StringRef Attr;
9350     if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
9351       Attr = " (weak_import)";
9352     outs() << left_justify(SegmentName, 8) << " "
9353            << left_justify(SectionName, 18) << " "
9354            << format_hex(Address, 10, true) << " "
9355            << left_justify(Entry.typeName(), 8) << " "
9356            << format_decimal(Entry.addend(), 8) << " "
9357            << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
9358            << Entry.symbolName() << Attr << "\n";
9359   }
9360 }
9361
9362 //===----------------------------------------------------------------------===//
9363 // lazy bind table dumping
9364 //===----------------------------------------------------------------------===//
9365
9366 void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) {
9367   // Build table of sections so names can used in final output.
9368   SegInfo sectionTable(Obj);
9369
9370   outs() << "segment  section            address     "
9371             "dylib            symbol\n";
9372   for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) {
9373     uint32_t SegIndex = Entry.segmentIndex();
9374     uint64_t OffsetInSeg = Entry.segmentOffset();
9375     StringRef SegmentName = sectionTable.segmentName(SegIndex);
9376     StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
9377     uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
9378
9379     // Table lines look like:
9380     //  __DATA  __got  0x00012010 libSystem ___stack_chk_guard
9381     outs() << left_justify(SegmentName, 8) << " "
9382            << left_justify(SectionName, 18) << " "
9383            << format_hex(Address, 10, true) << " "
9384            << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
9385            << Entry.symbolName() << "\n";
9386   }
9387 }
9388
9389 //===----------------------------------------------------------------------===//
9390 // weak bind table dumping
9391 //===----------------------------------------------------------------------===//
9392
9393 void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) {
9394   // Build table of sections so names can used in final output.
9395   SegInfo sectionTable(Obj);
9396
9397   outs() << "segment  section            address     "
9398             "type       addend   symbol\n";
9399   for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) {
9400     // Strong symbols don't have a location to update.
9401     if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
9402       outs() << "                                        strong              "
9403              << Entry.symbolName() << "\n";
9404       continue;
9405     }
9406     uint32_t SegIndex = Entry.segmentIndex();
9407     uint64_t OffsetInSeg = Entry.segmentOffset();
9408     StringRef SegmentName = sectionTable.segmentName(SegIndex);
9409     StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
9410     uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
9411
9412     // Table lines look like:
9413     // __DATA  __data  0x00001000  pointer    0   _foo
9414     outs() << left_justify(SegmentName, 8) << " "
9415            << left_justify(SectionName, 18) << " "
9416            << format_hex(Address, 10, true) << " "
9417            << left_justify(Entry.typeName(), 8) << " "
9418            << format_decimal(Entry.addend(), 8) << "   " << Entry.symbolName()
9419            << "\n";
9420   }
9421 }
9422
9423 // get_dyld_bind_info_symbolname() is used for disassembly and passed an
9424 // address, ReferenceValue, in the Mach-O file and looks in the dyld bind
9425 // information for that address. If the address is found its binding symbol
9426 // name is returned.  If not nullptr is returned.
9427 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
9428                                                  struct DisassembleInfo *info) {
9429   if (info->bindtable == nullptr) {
9430     info->bindtable = new (BindTable);
9431     SegInfo sectionTable(info->O);
9432     for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable()) {
9433       uint32_t SegIndex = Entry.segmentIndex();
9434       uint64_t OffsetInSeg = Entry.segmentOffset();
9435       if (!sectionTable.isValidSegIndexAndOffset(SegIndex, OffsetInSeg))
9436         continue;
9437       uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
9438       const char *SymbolName = nullptr;
9439       StringRef name = Entry.symbolName();
9440       if (!name.empty())
9441         SymbolName = name.data();
9442       info->bindtable->push_back(std::make_pair(Address, SymbolName));
9443     }
9444   }
9445   for (bind_table_iterator BI = info->bindtable->begin(),
9446                            BE = info->bindtable->end();
9447        BI != BE; ++BI) {
9448     uint64_t Address = BI->first;
9449     if (ReferenceValue == Address) {
9450       const char *SymbolName = BI->second;
9451       return SymbolName;
9452     }
9453   }
9454   return nullptr;
9455 }