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