1 //===-- MachODump.cpp - Object file dumping utility for llvm --------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the MachO-specific dumper for llvm-objdump.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm-objdump.h"
14 #include "llvm-c/Disassembler.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/Triple.h"
18 #include "llvm/BinaryFormat/MachO.h"
19 #include "llvm/Config/config.h"
20 #include "llvm/DebugInfo/DIContext.h"
21 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
22 #include "llvm/Demangle/Demangle.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/MC/MCContext.h"
25 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
26 #include "llvm/MC/MCInst.h"
27 #include "llvm/MC/MCInstPrinter.h"
28 #include "llvm/MC/MCInstrDesc.h"
29 #include "llvm/MC/MCInstrInfo.h"
30 #include "llvm/MC/MCRegisterInfo.h"
31 #include "llvm/MC/MCSubtargetInfo.h"
32 #include "llvm/Object/MachO.h"
33 #include "llvm/Object/MachOUniversal.h"
34 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/Endian.h"
38 #include "llvm/Support/Format.h"
39 #include "llvm/Support/FormattedStream.h"
40 #include "llvm/Support/GraphWriter.h"
41 #include "llvm/Support/LEB128.h"
42 #include "llvm/Support/MemoryBuffer.h"
43 #include "llvm/Support/TargetRegistry.h"
44 #include "llvm/Support/TargetSelect.h"
45 #include "llvm/Support/ToolOutputFile.h"
46 #include "llvm/Support/WithColor.h"
47 #include "llvm/Support/raw_ostream.h"
48 #include <algorithm>
49 #include <cstring>
50 #include <system_error>
51 
52 #ifdef HAVE_LIBXAR
53 extern "C" {
54 #include <xar/xar.h>
55 }
56 #endif
57 
58 using namespace llvm::object;
59 
60 namespace llvm {
61 
62 cl::OptionCategory MachOCat("llvm-objdump MachO Specific Options");
63 
64 extern cl::opt<bool> ArchiveHeaders;
65 extern cl::opt<bool> Disassemble;
66 extern cl::opt<bool> DisassembleAll;
67 extern cl::opt<DIDumpType> DwarfDumpType;
68 extern cl::list<std::string> FilterSections;
69 extern cl::list<std::string> MAttrs;
70 extern cl::opt<std::string> MCPU;
71 extern cl::opt<bool> NoShowRawInsn;
72 extern cl::opt<bool> NoLeadingAddr;
73 extern cl::opt<bool> PrintImmHex;
74 extern cl::opt<bool> PrivateHeaders;
75 extern cl::opt<bool> Relocations;
76 extern cl::opt<bool> SectionHeaders;
77 extern cl::opt<bool> SectionContents;
78 extern cl::opt<bool> SymbolTable;
79 extern cl::opt<std::string> TripleName;
80 extern cl::opt<bool> UnwindInfo;
81 
82 cl::opt<bool>
83     FirstPrivateHeader("private-header",
84                        cl::desc("Display only the first format specific file "
85                                 "header"),
86                        cl::cat(MachOCat));
87 
88 cl::opt<bool> ExportsTrie("exports-trie",
89                           cl::desc("Display mach-o exported symbols"),
90                           cl::cat(MachOCat));
91 
92 cl::opt<bool> Rebase("rebase", cl::desc("Display mach-o rebasing info"),
93                      cl::cat(MachOCat));
94 
95 cl::opt<bool> Bind("bind", cl::desc("Display mach-o binding info"),
96                    cl::cat(MachOCat));
97 
98 cl::opt<bool> LazyBind("lazy-bind",
99                        cl::desc("Display mach-o lazy binding info"),
100                        cl::cat(MachOCat));
101 
102 cl::opt<bool> WeakBind("weak-bind",
103                        cl::desc("Display mach-o weak binding info"),
104                        cl::cat(MachOCat));
105 
106 static cl::opt<bool>
107     UseDbg("g", cl::Grouping,
108            cl::desc("Print line information from debug info if available"),
109            cl::cat(MachOCat));
110 
111 static cl::opt<std::string> DSYMFile("dsym",
112                                      cl::desc("Use .dSYM file for debug info"),
113                                      cl::cat(MachOCat));
114 
115 static cl::opt<bool> FullLeadingAddr("full-leading-addr",
116                                      cl::desc("Print full leading address"),
117                                      cl::cat(MachOCat));
118 
119 static cl::opt<bool> NoLeadingHeaders("no-leading-headers",
120                                       cl::desc("Print no leading headers"),
121                                       cl::cat(MachOCat));
122 
123 cl::opt<bool> UniversalHeaders("universal-headers",
124                                cl::desc("Print Mach-O universal headers "
125                                         "(requires -macho)"),
126                                cl::cat(MachOCat));
127 
128 cl::opt<bool>
129     ArchiveMemberOffsets("archive-member-offsets",
130                          cl::desc("Print the offset to each archive member for "
131                                   "Mach-O archives (requires -macho and "
132                                   "-archive-headers)"),
133                          cl::cat(MachOCat));
134 
135 cl::opt<bool> IndirectSymbols("indirect-symbols",
136                               cl::desc("Print indirect symbol table for Mach-O "
137                                        "objects (requires -macho)"),
138                               cl::cat(MachOCat));
139 
140 cl::opt<bool>
141     DataInCode("data-in-code",
142                cl::desc("Print the data in code table for Mach-O objects "
143                         "(requires -macho)"),
144                cl::cat(MachOCat));
145 
146 cl::opt<bool> LinkOptHints("link-opt-hints",
147                            cl::desc("Print the linker optimization hints for "
148                                     "Mach-O objects (requires -macho)"),
149                            cl::cat(MachOCat));
150 
151 cl::opt<bool> InfoPlist("info-plist",
152                         cl::desc("Print the info plist section as strings for "
153                                  "Mach-O objects (requires -macho)"),
154                         cl::cat(MachOCat));
155 
156 cl::opt<bool> DylibsUsed("dylibs-used",
157                          cl::desc("Print the shared libraries used for linked "
158                                   "Mach-O files (requires -macho)"),
159                          cl::cat(MachOCat));
160 
161 cl::opt<bool>
162     DylibId("dylib-id",
163             cl::desc("Print the shared library's id for the dylib Mach-O "
164                      "file (requires -macho)"),
165             cl::cat(MachOCat));
166 
167 cl::opt<bool>
168     NonVerbose("non-verbose",
169                cl::desc("Print the info for Mach-O objects in "
170                         "non-verbose or numeric form (requires -macho)"),
171                cl::cat(MachOCat));
172 
173 cl::opt<bool>
174     ObjcMetaData("objc-meta-data",
175                  cl::desc("Print the Objective-C runtime meta data for "
176                           "Mach-O files (requires -macho)"),
177                  cl::cat(MachOCat));
178 
179 cl::opt<std::string> DisSymName(
180     "dis-symname",
181     cl::desc("disassemble just this symbol's instructions (requires -macho)"),
182     cl::cat(MachOCat));
183 
184 static cl::opt<bool> NoSymbolicOperands(
185     "no-symbolic-operands",
186     cl::desc("do not symbolic operands when disassembling (requires -macho)"),
187     cl::cat(MachOCat));
188 
189 static cl::list<std::string>
190     ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
191               cl::ZeroOrMore, cl::cat(MachOCat));
192 
193 bool ArchAll = false;
194 
195 static std::string ThumbTripleName;
196 
197 static const Target *GetTarget(const MachOObjectFile *MachOObj,
198                                const char **McpuDefault,
199                                const Target **ThumbTarget) {
200   // Figure out the target triple.
201   Triple TT(TripleName);
202   if (TripleName.empty()) {
203     TT = MachOObj->getArchTriple(McpuDefault);
204     TripleName = TT.str();
205   }
206 
207   if (TT.getArch() == Triple::arm) {
208     // We've inferred a 32-bit ARM target from the object file. All MachO CPUs
209     // that support ARM are also capable of Thumb mode.
210     Triple ThumbTriple = TT;
211     std::string ThumbName = (Twine("thumb") + TT.getArchName().substr(3)).str();
212     ThumbTriple.setArchName(ThumbName);
213     ThumbTripleName = ThumbTriple.str();
214   }
215 
216   // Get the target specific parser.
217   std::string Error;
218   const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
219   if (TheTarget && ThumbTripleName.empty())
220     return TheTarget;
221 
222   *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
223   if (*ThumbTarget)
224     return TheTarget;
225 
226   WithColor::error(errs(), "llvm-objdump") << "unable to get target for '";
227   if (!TheTarget)
228     errs() << TripleName;
229   else
230     errs() << ThumbTripleName;
231   errs() << "', see --version and --triple.\n";
232   return nullptr;
233 }
234 
235 struct SymbolSorter {
236   bool operator()(const SymbolRef &A, const SymbolRef &B) {
237     Expected<SymbolRef::Type> ATypeOrErr = A.getType();
238     if (!ATypeOrErr)
239       report_error(ATypeOrErr.takeError(), A.getObject()->getFileName());
240     SymbolRef::Type AType = *ATypeOrErr;
241     Expected<SymbolRef::Type> BTypeOrErr = B.getType();
242     if (!BTypeOrErr)
243       report_error(BTypeOrErr.takeError(), B.getObject()->getFileName());
244     SymbolRef::Type BType = *BTypeOrErr;
245     uint64_t AAddr = (AType != SymbolRef::ST_Function) ? 0 : A.getValue();
246     uint64_t BAddr = (BType != SymbolRef::ST_Function) ? 0 : B.getValue();
247     return AAddr < BAddr;
248   }
249 };
250 
251 // Types for the storted data in code table that is built before disassembly
252 // and the predicate function to sort them.
253 typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
254 typedef std::vector<DiceTableEntry> DiceTable;
255 typedef DiceTable::iterator dice_table_iterator;
256 
257 #ifdef HAVE_LIBXAR
258 namespace {
259 struct ScopedXarFile {
260   xar_t xar;
261   ScopedXarFile(const char *filename, int32_t flags)
262       : xar(xar_open(filename, flags)) {}
263   ~ScopedXarFile() {
264     if (xar)
265       xar_close(xar);
266   }
267   ScopedXarFile(const ScopedXarFile &) = delete;
268   ScopedXarFile &operator=(const ScopedXarFile &) = delete;
269   operator xar_t() { return xar; }
270 };
271 
272 struct ScopedXarIter {
273   xar_iter_t iter;
274   ScopedXarIter() : iter(xar_iter_new()) {}
275   ~ScopedXarIter() {
276     if (iter)
277       xar_iter_free(iter);
278   }
279   ScopedXarIter(const ScopedXarIter &) = delete;
280   ScopedXarIter &operator=(const ScopedXarIter &) = delete;
281   operator xar_iter_t() { return iter; }
282 };
283 } // namespace
284 #endif // defined(HAVE_LIBXAR)
285 
286 // This is used to search for a data in code table entry for the PC being
287 // disassembled.  The j parameter has the PC in j.first.  A single data in code
288 // table entry can cover many bytes for each of its Kind's.  So if the offset,
289 // aka the i.first value, of the data in code table entry plus its Length
290 // covers the PC being searched for this will return true.  If not it will
291 // return false.
292 static bool compareDiceTableEntries(const DiceTableEntry &i,
293                                     const DiceTableEntry &j) {
294   uint16_t Length;
295   i.second.getLength(Length);
296 
297   return j.first >= i.first && j.first < i.first + Length;
298 }
299 
300 static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
301                                unsigned short Kind) {
302   uint32_t Value, Size = 1;
303 
304   switch (Kind) {
305   default:
306   case MachO::DICE_KIND_DATA:
307     if (Length >= 4) {
308       if (!NoShowRawInsn)
309         dumpBytes(makeArrayRef(bytes, 4), outs());
310       Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
311       outs() << "\t.long " << Value;
312       Size = 4;
313     } else if (Length >= 2) {
314       if (!NoShowRawInsn)
315         dumpBytes(makeArrayRef(bytes, 2), outs());
316       Value = bytes[1] << 8 | bytes[0];
317       outs() << "\t.short " << Value;
318       Size = 2;
319     } else {
320       if (!NoShowRawInsn)
321         dumpBytes(makeArrayRef(bytes, 2), outs());
322       Value = bytes[0];
323       outs() << "\t.byte " << Value;
324       Size = 1;
325     }
326     if (Kind == MachO::DICE_KIND_DATA)
327       outs() << "\t@ KIND_DATA\n";
328     else
329       outs() << "\t@ data in code kind = " << Kind << "\n";
330     break;
331   case MachO::DICE_KIND_JUMP_TABLE8:
332     if (!NoShowRawInsn)
333       dumpBytes(makeArrayRef(bytes, 1), outs());
334     Value = bytes[0];
335     outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
336     Size = 1;
337     break;
338   case MachO::DICE_KIND_JUMP_TABLE16:
339     if (!NoShowRawInsn)
340       dumpBytes(makeArrayRef(bytes, 2), outs());
341     Value = bytes[1] << 8 | bytes[0];
342     outs() << "\t.short " << format("%5u", Value & 0xffff)
343            << "\t@ KIND_JUMP_TABLE16\n";
344     Size = 2;
345     break;
346   case MachO::DICE_KIND_JUMP_TABLE32:
347   case MachO::DICE_KIND_ABS_JUMP_TABLE32:
348     if (!NoShowRawInsn)
349       dumpBytes(makeArrayRef(bytes, 4), outs());
350     Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
351     outs() << "\t.long " << Value;
352     if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
353       outs() << "\t@ KIND_JUMP_TABLE32\n";
354     else
355       outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
356     Size = 4;
357     break;
358   }
359   return Size;
360 }
361 
362 static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
363                                   std::vector<SectionRef> &Sections,
364                                   std::vector<SymbolRef> &Symbols,
365                                   SmallVectorImpl<uint64_t> &FoundFns,
366                                   uint64_t &BaseSegmentAddress) {
367   const StringRef FileName = MachOObj->getFileName();
368   for (const SymbolRef &Symbol : MachOObj->symbols()) {
369     StringRef SymName = unwrapOrError(Symbol.getName(), FileName);
370     if (!SymName.startswith("ltmp"))
371       Symbols.push_back(Symbol);
372   }
373 
374   for (const SectionRef &Section : MachOObj->sections()) {
375     StringRef SectName;
376     Section.getName(SectName);
377     Sections.push_back(Section);
378   }
379 
380   bool BaseSegmentAddressSet = false;
381   for (const auto &Command : MachOObj->load_commands()) {
382     if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
383       // We found a function starts segment, parse the addresses for later
384       // consumption.
385       MachO::linkedit_data_command LLC =
386           MachOObj->getLinkeditDataLoadCommand(Command);
387 
388       MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
389     } else if (Command.C.cmd == MachO::LC_SEGMENT) {
390       MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
391       StringRef SegName = SLC.segname;
392       if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
393         BaseSegmentAddressSet = true;
394         BaseSegmentAddress = SLC.vmaddr;
395       }
396     }
397   }
398 }
399 
400 static void printRelocationTargetName(const MachOObjectFile *O,
401                                       const MachO::any_relocation_info &RE,
402                                       raw_string_ostream &Fmt) {
403   // Target of a scattered relocation is an address.  In the interest of
404   // generating pretty output, scan through the symbol table looking for a
405   // symbol that aligns with that address.  If we find one, print it.
406   // Otherwise, we just print the hex address of the target.
407   const StringRef FileName = O->getFileName();
408   if (O->isRelocationScattered(RE)) {
409     uint32_t Val = O->getPlainRelocationSymbolNum(RE);
410 
411     for (const SymbolRef &Symbol : O->symbols()) {
412       uint64_t Addr = unwrapOrError(Symbol.getAddress(), FileName);
413       if (Addr != Val)
414         continue;
415       Fmt << unwrapOrError(Symbol.getName(), FileName);
416       return;
417     }
418 
419     // If we couldn't find a symbol that this relocation refers to, try
420     // to find a section beginning instead.
421     for (const SectionRef &Section : ToolSectionFilter(*O)) {
422       StringRef Name;
423       uint64_t Addr = Section.getAddress();
424       if (Addr != Val)
425         continue;
426       if (std::error_code EC = Section.getName(Name))
427         report_error(errorCodeToError(EC), O->getFileName());
428       Fmt << Name;
429       return;
430     }
431 
432     Fmt << format("0x%x", Val);
433     return;
434   }
435 
436   StringRef S;
437   bool isExtern = O->getPlainRelocationExternal(RE);
438   uint64_t Val = O->getPlainRelocationSymbolNum(RE);
439 
440   if (O->getAnyRelocationType(RE) == MachO::ARM64_RELOC_ADDEND) {
441     Fmt << format("0x%0" PRIx64, Val);
442     return;
443   }
444 
445   if (isExtern) {
446     symbol_iterator SI = O->symbol_begin();
447     advance(SI, Val);
448     S = unwrapOrError(SI->getName(), FileName);
449   } else {
450     section_iterator SI = O->section_begin();
451     // Adjust for the fact that sections are 1-indexed.
452     if (Val == 0) {
453       Fmt << "0 (?,?)";
454       return;
455     }
456     uint32_t I = Val - 1;
457     while (I != 0 && SI != O->section_end()) {
458       --I;
459       advance(SI, 1);
460     }
461     if (SI == O->section_end())
462       Fmt << Val << " (?,?)";
463     else
464       SI->getName(S);
465   }
466 
467   Fmt << S;
468 }
469 
470 Error getMachORelocationValueString(const MachOObjectFile *Obj,
471                                     const RelocationRef &RelRef,
472                                     SmallVectorImpl<char> &Result) {
473   DataRefImpl Rel = RelRef.getRawDataRefImpl();
474   MachO::any_relocation_info RE = Obj->getRelocation(Rel);
475 
476   unsigned Arch = Obj->getArch();
477 
478   std::string FmtBuf;
479   raw_string_ostream Fmt(FmtBuf);
480   unsigned Type = Obj->getAnyRelocationType(RE);
481   bool IsPCRel = Obj->getAnyRelocationPCRel(RE);
482 
483   // Determine any addends that should be displayed with the relocation.
484   // These require decoding the relocation type, which is triple-specific.
485 
486   // X86_64 has entirely custom relocation types.
487   if (Arch == Triple::x86_64) {
488     switch (Type) {
489     case MachO::X86_64_RELOC_GOT_LOAD:
490     case MachO::X86_64_RELOC_GOT: {
491       printRelocationTargetName(Obj, RE, Fmt);
492       Fmt << "@GOT";
493       if (IsPCRel)
494         Fmt << "PCREL";
495       break;
496     }
497     case MachO::X86_64_RELOC_SUBTRACTOR: {
498       DataRefImpl RelNext = Rel;
499       Obj->moveRelocationNext(RelNext);
500       MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
501 
502       // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
503       // X86_64_RELOC_UNSIGNED.
504       // NOTE: Scattered relocations don't exist on x86_64.
505       unsigned RType = Obj->getAnyRelocationType(RENext);
506       if (RType != MachO::X86_64_RELOC_UNSIGNED)
507         report_error(Obj->getFileName(), "Expected X86_64_RELOC_UNSIGNED after "
508                                          "X86_64_RELOC_SUBTRACTOR.");
509 
510       // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
511       // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
512       printRelocationTargetName(Obj, RENext, Fmt);
513       Fmt << "-";
514       printRelocationTargetName(Obj, RE, Fmt);
515       break;
516     }
517     case MachO::X86_64_RELOC_TLV:
518       printRelocationTargetName(Obj, RE, Fmt);
519       Fmt << "@TLV";
520       if (IsPCRel)
521         Fmt << "P";
522       break;
523     case MachO::X86_64_RELOC_SIGNED_1:
524       printRelocationTargetName(Obj, RE, Fmt);
525       Fmt << "-1";
526       break;
527     case MachO::X86_64_RELOC_SIGNED_2:
528       printRelocationTargetName(Obj, RE, Fmt);
529       Fmt << "-2";
530       break;
531     case MachO::X86_64_RELOC_SIGNED_4:
532       printRelocationTargetName(Obj, RE, Fmt);
533       Fmt << "-4";
534       break;
535     default:
536       printRelocationTargetName(Obj, RE, Fmt);
537       break;
538     }
539     // X86 and ARM share some relocation types in common.
540   } else if (Arch == Triple::x86 || Arch == Triple::arm ||
541              Arch == Triple::ppc) {
542     // Generic relocation types...
543     switch (Type) {
544     case MachO::GENERIC_RELOC_PAIR: // prints no info
545       return Error::success();
546     case MachO::GENERIC_RELOC_SECTDIFF: {
547       DataRefImpl RelNext = Rel;
548       Obj->moveRelocationNext(RelNext);
549       MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
550 
551       // X86 sect diff's must be followed by a relocation of type
552       // GENERIC_RELOC_PAIR.
553       unsigned RType = Obj->getAnyRelocationType(RENext);
554 
555       if (RType != MachO::GENERIC_RELOC_PAIR)
556         report_error(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after "
557                                          "GENERIC_RELOC_SECTDIFF.");
558 
559       printRelocationTargetName(Obj, RE, Fmt);
560       Fmt << "-";
561       printRelocationTargetName(Obj, RENext, Fmt);
562       break;
563     }
564     }
565 
566     if (Arch == Triple::x86 || Arch == Triple::ppc) {
567       switch (Type) {
568       case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
569         DataRefImpl RelNext = Rel;
570         Obj->moveRelocationNext(RelNext);
571         MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
572 
573         // X86 sect diff's must be followed by a relocation of type
574         // GENERIC_RELOC_PAIR.
575         unsigned RType = Obj->getAnyRelocationType(RENext);
576         if (RType != MachO::GENERIC_RELOC_PAIR)
577           report_error(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after "
578                                            "GENERIC_RELOC_LOCAL_SECTDIFF.");
579 
580         printRelocationTargetName(Obj, RE, Fmt);
581         Fmt << "-";
582         printRelocationTargetName(Obj, RENext, Fmt);
583         break;
584       }
585       case MachO::GENERIC_RELOC_TLV: {
586         printRelocationTargetName(Obj, RE, Fmt);
587         Fmt << "@TLV";
588         if (IsPCRel)
589           Fmt << "P";
590         break;
591       }
592       default:
593         printRelocationTargetName(Obj, RE, Fmt);
594       }
595     } else { // ARM-specific relocations
596       switch (Type) {
597       case MachO::ARM_RELOC_HALF:
598       case MachO::ARM_RELOC_HALF_SECTDIFF: {
599         // Half relocations steal a bit from the length field to encode
600         // whether this is an upper16 or a lower16 relocation.
601         bool isUpper = (Obj->getAnyRelocationLength(RE) & 0x1) == 1;
602 
603         if (isUpper)
604           Fmt << ":upper16:(";
605         else
606           Fmt << ":lower16:(";
607         printRelocationTargetName(Obj, RE, Fmt);
608 
609         DataRefImpl RelNext = Rel;
610         Obj->moveRelocationNext(RelNext);
611         MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
612 
613         // ARM half relocs must be followed by a relocation of type
614         // ARM_RELOC_PAIR.
615         unsigned RType = Obj->getAnyRelocationType(RENext);
616         if (RType != MachO::ARM_RELOC_PAIR)
617           report_error(Obj->getFileName(), "Expected ARM_RELOC_PAIR after "
618                                            "ARM_RELOC_HALF");
619 
620         // NOTE: The half of the target virtual address is stashed in the
621         // address field of the secondary relocation, but we can't reverse
622         // engineer the constant offset from it without decoding the movw/movt
623         // instruction to find the other half in its immediate field.
624 
625         // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
626         // symbol/section pointer of the follow-on relocation.
627         if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
628           Fmt << "-";
629           printRelocationTargetName(Obj, RENext, Fmt);
630         }
631 
632         Fmt << ")";
633         break;
634       }
635       default: {
636         printRelocationTargetName(Obj, RE, Fmt);
637       }
638       }
639     }
640   } else
641     printRelocationTargetName(Obj, RE, Fmt);
642 
643   Fmt.flush();
644   Result.append(FmtBuf.begin(), FmtBuf.end());
645   return Error::success();
646 }
647 
648 static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
649                                      uint32_t n, uint32_t count,
650                                      uint32_t stride, uint64_t addr) {
651   MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
652   uint32_t nindirectsyms = Dysymtab.nindirectsyms;
653   if (n > nindirectsyms)
654     outs() << " (entries start past the end of the indirect symbol "
655               "table) (reserved1 field greater than the table size)";
656   else if (n + count > nindirectsyms)
657     outs() << " (entries extends past the end of the indirect symbol "
658               "table)";
659   outs() << "\n";
660   uint32_t cputype = O->getHeader().cputype;
661   if (cputype & MachO::CPU_ARCH_ABI64)
662     outs() << "address            index";
663   else
664     outs() << "address    index";
665   if (verbose)
666     outs() << " name\n";
667   else
668     outs() << "\n";
669   for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
670     if (cputype & MachO::CPU_ARCH_ABI64)
671       outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
672     else
673       outs() << format("0x%08" PRIx32, (uint32_t)addr + j * stride) << " ";
674     MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
675     uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
676     if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
677       outs() << "LOCAL\n";
678       continue;
679     }
680     if (indirect_symbol ==
681         (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
682       outs() << "LOCAL ABSOLUTE\n";
683       continue;
684     }
685     if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
686       outs() << "ABSOLUTE\n";
687       continue;
688     }
689     outs() << format("%5u ", indirect_symbol);
690     if (verbose) {
691       MachO::symtab_command Symtab = O->getSymtabLoadCommand();
692       if (indirect_symbol < Symtab.nsyms) {
693         symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
694         SymbolRef Symbol = *Sym;
695         outs() << unwrapOrError(Symbol.getName(), O->getFileName());
696       } else {
697         outs() << "?";
698       }
699     }
700     outs() << "\n";
701   }
702 }
703 
704 static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
705   for (const auto &Load : O->load_commands()) {
706     if (Load.C.cmd == MachO::LC_SEGMENT_64) {
707       MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
708       for (unsigned J = 0; J < Seg.nsects; ++J) {
709         MachO::section_64 Sec = O->getSection64(Load, J);
710         uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
711         if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
712             section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
713             section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
714             section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
715             section_type == MachO::S_SYMBOL_STUBS) {
716           uint32_t stride;
717           if (section_type == MachO::S_SYMBOL_STUBS)
718             stride = Sec.reserved2;
719           else
720             stride = 8;
721           if (stride == 0) {
722             outs() << "Can't print indirect symbols for (" << Sec.segname << ","
723                    << Sec.sectname << ") "
724                    << "(size of stubs in reserved2 field is zero)\n";
725             continue;
726           }
727           uint32_t count = Sec.size / stride;
728           outs() << "Indirect symbols for (" << Sec.segname << ","
729                  << Sec.sectname << ") " << count << " entries";
730           uint32_t n = Sec.reserved1;
731           PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
732         }
733       }
734     } else if (Load.C.cmd == MachO::LC_SEGMENT) {
735       MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
736       for (unsigned J = 0; J < Seg.nsects; ++J) {
737         MachO::section Sec = O->getSection(Load, J);
738         uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
739         if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
740             section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
741             section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
742             section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
743             section_type == MachO::S_SYMBOL_STUBS) {
744           uint32_t stride;
745           if (section_type == MachO::S_SYMBOL_STUBS)
746             stride = Sec.reserved2;
747           else
748             stride = 4;
749           if (stride == 0) {
750             outs() << "Can't print indirect symbols for (" << Sec.segname << ","
751                    << Sec.sectname << ") "
752                    << "(size of stubs in reserved2 field is zero)\n";
753             continue;
754           }
755           uint32_t count = Sec.size / stride;
756           outs() << "Indirect symbols for (" << Sec.segname << ","
757                  << Sec.sectname << ") " << count << " entries";
758           uint32_t n = Sec.reserved1;
759           PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
760         }
761       }
762     }
763   }
764 }
765 
766 static void PrintRType(const uint64_t cputype, const unsigned r_type) {
767   static char const *generic_r_types[] = {
768     "VANILLA ", "PAIR    ", "SECTDIF ", "PBLAPTR ", "LOCSDIF ", "TLV     ",
769     "  6 (?) ", "  7 (?) ", "  8 (?) ", "  9 (?) ", " 10 (?) ", " 11 (?) ",
770     " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
771   };
772   static char const *x86_64_r_types[] = {
773     "UNSIGND ", "SIGNED  ", "BRANCH  ", "GOT_LD  ", "GOT     ", "SUB     ",
774     "SIGNED1 ", "SIGNED2 ", "SIGNED4 ", "TLV     ", " 10 (?) ", " 11 (?) ",
775     " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
776   };
777   static char const *arm_r_types[] = {
778     "VANILLA ", "PAIR    ", "SECTDIFF", "LOCSDIF ", "PBLAPTR ",
779     "BR24    ", "T_BR22  ", "T_BR32  ", "HALF    ", "HALFDIF ",
780     " 10 (?) ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
781   };
782   static char const *arm64_r_types[] = {
783     "UNSIGND ", "SUB     ", "BR26    ", "PAGE21  ", "PAGOF12 ",
784     "GOTLDP  ", "GOTLDPOF", "PTRTGOT ", "TLVLDP  ", "TLVLDPOF",
785     "ADDEND  ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
786   };
787 
788   if (r_type > 0xf){
789     outs() << format("%-7u", r_type) << " ";
790     return;
791   }
792   switch (cputype) {
793     case MachO::CPU_TYPE_I386:
794       outs() << generic_r_types[r_type];
795       break;
796     case MachO::CPU_TYPE_X86_64:
797       outs() << x86_64_r_types[r_type];
798       break;
799     case MachO::CPU_TYPE_ARM:
800       outs() << arm_r_types[r_type];
801       break;
802     case MachO::CPU_TYPE_ARM64:
803     case MachO::CPU_TYPE_ARM64_32:
804       outs() << arm64_r_types[r_type];
805       break;
806     default:
807       outs() << format("%-7u ", r_type);
808   }
809 }
810 
811 static void PrintRLength(const uint64_t cputype, const unsigned r_type,
812                          const unsigned r_length, const bool previous_arm_half){
813   if (cputype == MachO::CPU_TYPE_ARM &&
814       (r_type == MachO::ARM_RELOC_HALF ||
815        r_type == MachO::ARM_RELOC_HALF_SECTDIFF || previous_arm_half == true)) {
816     if ((r_length & 0x1) == 0)
817       outs() << "lo/";
818     else
819       outs() << "hi/";
820     if ((r_length & 0x1) == 0)
821       outs() << "arm ";
822     else
823       outs() << "thm ";
824   } else {
825     switch (r_length) {
826       case 0:
827         outs() << "byte   ";
828         break;
829       case 1:
830         outs() << "word   ";
831         break;
832       case 2:
833         outs() << "long   ";
834         break;
835       case 3:
836         if (cputype == MachO::CPU_TYPE_X86_64)
837           outs() << "quad   ";
838         else
839           outs() << format("?(%2d)  ", r_length);
840         break;
841       default:
842         outs() << format("?(%2d)  ", r_length);
843     }
844   }
845 }
846 
847 static void PrintRelocationEntries(const MachOObjectFile *O,
848                                    const relocation_iterator Begin,
849                                    const relocation_iterator End,
850                                    const uint64_t cputype,
851                                    const bool verbose) {
852   const MachO::symtab_command Symtab = O->getSymtabLoadCommand();
853   bool previous_arm_half = false;
854   bool previous_sectdiff = false;
855   uint32_t sectdiff_r_type = 0;
856 
857   for (relocation_iterator Reloc = Begin; Reloc != End; ++Reloc) {
858     const DataRefImpl Rel = Reloc->getRawDataRefImpl();
859     const MachO::any_relocation_info RE = O->getRelocation(Rel);
860     const unsigned r_type = O->getAnyRelocationType(RE);
861     const bool r_scattered = O->isRelocationScattered(RE);
862     const unsigned r_pcrel = O->getAnyRelocationPCRel(RE);
863     const unsigned r_length = O->getAnyRelocationLength(RE);
864     const unsigned r_address = O->getAnyRelocationAddress(RE);
865     const bool r_extern = (r_scattered ? false :
866                            O->getPlainRelocationExternal(RE));
867     const uint32_t r_value = (r_scattered ?
868                               O->getScatteredRelocationValue(RE) : 0);
869     const unsigned r_symbolnum = (r_scattered ? 0 :
870                                   O->getPlainRelocationSymbolNum(RE));
871 
872     if (r_scattered && cputype != MachO::CPU_TYPE_X86_64) {
873       if (verbose) {
874         // scattered: address
875         if ((cputype == MachO::CPU_TYPE_I386 &&
876              r_type == MachO::GENERIC_RELOC_PAIR) ||
877             (cputype == MachO::CPU_TYPE_ARM && r_type == MachO::ARM_RELOC_PAIR))
878           outs() << "         ";
879         else
880           outs() << format("%08x ", (unsigned int)r_address);
881 
882         // scattered: pcrel
883         if (r_pcrel)
884           outs() << "True  ";
885         else
886           outs() << "False ";
887 
888         // scattered: length
889         PrintRLength(cputype, r_type, r_length, previous_arm_half);
890 
891         // scattered: extern & type
892         outs() << "n/a    ";
893         PrintRType(cputype, r_type);
894 
895         // scattered: scattered & value
896         outs() << format("True      0x%08x", (unsigned int)r_value);
897         if (previous_sectdiff == false) {
898           if ((cputype == MachO::CPU_TYPE_ARM &&
899                r_type == MachO::ARM_RELOC_PAIR))
900             outs() << format(" half = 0x%04x ", (unsigned int)r_address);
901         } else if (cputype == MachO::CPU_TYPE_ARM &&
902                    sectdiff_r_type == MachO::ARM_RELOC_HALF_SECTDIFF)
903           outs() << format(" other_half = 0x%04x ", (unsigned int)r_address);
904         if ((cputype == MachO::CPU_TYPE_I386 &&
905              (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
906               r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) ||
907             (cputype == MachO::CPU_TYPE_ARM &&
908              (sectdiff_r_type == MachO::ARM_RELOC_SECTDIFF ||
909               sectdiff_r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
910               sectdiff_r_type == MachO::ARM_RELOC_HALF_SECTDIFF))) {
911           previous_sectdiff = true;
912           sectdiff_r_type = r_type;
913         } else {
914           previous_sectdiff = false;
915           sectdiff_r_type = 0;
916         }
917         if (cputype == MachO::CPU_TYPE_ARM &&
918             (r_type == MachO::ARM_RELOC_HALF ||
919              r_type == MachO::ARM_RELOC_HALF_SECTDIFF))
920           previous_arm_half = true;
921         else
922           previous_arm_half = false;
923         outs() << "\n";
924       }
925       else {
926         // scattered: address pcrel length extern type scattered value
927         outs() << format("%08x %1d     %-2d     n/a    %-7d 1         0x%08x\n",
928                          (unsigned int)r_address, r_pcrel, r_length, r_type,
929                          (unsigned int)r_value);
930       }
931     }
932     else {
933       if (verbose) {
934         // plain: address
935         if (cputype == MachO::CPU_TYPE_ARM && r_type == MachO::ARM_RELOC_PAIR)
936           outs() << "         ";
937         else
938           outs() << format("%08x ", (unsigned int)r_address);
939 
940         // plain: pcrel
941         if (r_pcrel)
942           outs() << "True  ";
943         else
944           outs() << "False ";
945 
946         // plain: length
947         PrintRLength(cputype, r_type, r_length, previous_arm_half);
948 
949         if (r_extern) {
950           // plain: extern & type & scattered
951           outs() << "True   ";
952           PrintRType(cputype, r_type);
953           outs() << "False     ";
954 
955           // plain: symbolnum/value
956           if (r_symbolnum > Symtab.nsyms)
957             outs() << format("?(%d)\n", r_symbolnum);
958           else {
959             SymbolRef Symbol = *O->getSymbolByIndex(r_symbolnum);
960             Expected<StringRef> SymNameNext = Symbol.getName();
961             const char *name = NULL;
962             if (SymNameNext)
963               name = SymNameNext->data();
964             if (name == NULL)
965               outs() << format("?(%d)\n", r_symbolnum);
966             else
967               outs() << name << "\n";
968           }
969         }
970         else {
971           // plain: extern & type & scattered
972           outs() << "False  ";
973           PrintRType(cputype, r_type);
974           outs() << "False     ";
975 
976           // plain: symbolnum/value
977           if (cputype == MachO::CPU_TYPE_ARM && r_type == MachO::ARM_RELOC_PAIR)
978             outs() << format("other_half = 0x%04x\n", (unsigned int)r_address);
979           else if ((cputype == MachO::CPU_TYPE_ARM64 ||
980                     cputype == MachO::CPU_TYPE_ARM64_32) &&
981                    r_type == MachO::ARM64_RELOC_ADDEND)
982             outs() << format("addend = 0x%06x\n", (unsigned int)r_symbolnum);
983           else {
984             outs() << format("%d ", r_symbolnum);
985             if (r_symbolnum == MachO::R_ABS)
986               outs() << "R_ABS\n";
987             else {
988               // in this case, r_symbolnum is actually a 1-based section number
989               uint32_t nsects = O->section_end()->getRawDataRefImpl().d.a;
990               if (r_symbolnum > 0 && r_symbolnum <= nsects) {
991                 object::DataRefImpl DRI;
992                 DRI.d.a = r_symbolnum-1;
993                 StringRef SegName = O->getSectionFinalSegmentName(DRI);
994                 if (Expected<StringRef> NameOrErr = O->getSectionName(DRI))
995                   outs() << "(" << SegName << "," << *NameOrErr << ")\n";
996                 else
997                   outs() << "(?,?)\n";
998               }
999               else {
1000                 outs() << "(?,?)\n";
1001               }
1002             }
1003           }
1004         }
1005         if (cputype == MachO::CPU_TYPE_ARM &&
1006             (r_type == MachO::ARM_RELOC_HALF ||
1007              r_type == MachO::ARM_RELOC_HALF_SECTDIFF))
1008           previous_arm_half = true;
1009         else
1010           previous_arm_half = false;
1011       }
1012       else {
1013         // plain: address pcrel length extern type scattered symbolnum/section
1014         outs() << format("%08x %1d     %-2d     %1d      %-7d 0         %d\n",
1015                          (unsigned int)r_address, r_pcrel, r_length, r_extern,
1016                          r_type, r_symbolnum);
1017       }
1018     }
1019   }
1020 }
1021 
1022 static void PrintRelocations(const MachOObjectFile *O, const bool verbose) {
1023   const uint64_t cputype = O->getHeader().cputype;
1024   const MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
1025   if (Dysymtab.nextrel != 0) {
1026     outs() << "External relocation information " << Dysymtab.nextrel
1027            << " entries";
1028     outs() << "\naddress  pcrel length extern type    scattered "
1029               "symbolnum/value\n";
1030     PrintRelocationEntries(O, O->extrel_begin(), O->extrel_end(), cputype,
1031                            verbose);
1032   }
1033   if (Dysymtab.nlocrel != 0) {
1034     outs() << format("Local relocation information %u entries",
1035                      Dysymtab.nlocrel);
1036     outs() << "\naddress  pcrel length extern type    scattered "
1037               "symbolnum/value\n";
1038     PrintRelocationEntries(O, O->locrel_begin(), O->locrel_end(), cputype,
1039                            verbose);
1040   }
1041   for (const auto &Load : O->load_commands()) {
1042     if (Load.C.cmd == MachO::LC_SEGMENT_64) {
1043       const MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
1044       for (unsigned J = 0; J < Seg.nsects; ++J) {
1045         const MachO::section_64 Sec = O->getSection64(Load, J);
1046         if (Sec.nreloc != 0) {
1047           DataRefImpl DRI;
1048           DRI.d.a = J;
1049           const StringRef SegName = O->getSectionFinalSegmentName(DRI);
1050           if (Expected<StringRef> NameOrErr = O->getSectionName(DRI))
1051             outs() << "Relocation information (" << SegName << "," << *NameOrErr
1052                    << format(") %u entries", Sec.nreloc);
1053           else
1054             outs() << "Relocation information (" << SegName << ",?) "
1055                    << format("%u entries", Sec.nreloc);
1056           outs() << "\naddress  pcrel length extern type    scattered "
1057                     "symbolnum/value\n";
1058           PrintRelocationEntries(O, O->section_rel_begin(DRI),
1059                                  O->section_rel_end(DRI), cputype, verbose);
1060         }
1061       }
1062     } else if (Load.C.cmd == MachO::LC_SEGMENT) {
1063       const MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
1064       for (unsigned J = 0; J < Seg.nsects; ++J) {
1065         const MachO::section Sec = O->getSection(Load, J);
1066         if (Sec.nreloc != 0) {
1067           DataRefImpl DRI;
1068           DRI.d.a = J;
1069           const StringRef SegName = O->getSectionFinalSegmentName(DRI);
1070           if (Expected<StringRef> NameOrErr = O->getSectionName(DRI))
1071             outs() << "Relocation information (" << SegName << "," << *NameOrErr
1072                    << format(") %u entries", Sec.nreloc);
1073           else
1074             outs() << "Relocation information (" << SegName << ",?) "
1075                    << format("%u entries", Sec.nreloc);
1076           outs() << "\naddress  pcrel length extern type    scattered "
1077                     "symbolnum/value\n";
1078           PrintRelocationEntries(O, O->section_rel_begin(DRI),
1079                                  O->section_rel_end(DRI), cputype, verbose);
1080         }
1081       }
1082     }
1083   }
1084 }
1085 
1086 static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
1087   MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
1088   uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
1089   outs() << "Data in code table (" << nentries << " entries)\n";
1090   outs() << "offset     length kind\n";
1091   for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
1092        ++DI) {
1093     uint32_t Offset;
1094     DI->getOffset(Offset);
1095     outs() << format("0x%08" PRIx32, Offset) << " ";
1096     uint16_t Length;
1097     DI->getLength(Length);
1098     outs() << format("%6u", Length) << " ";
1099     uint16_t Kind;
1100     DI->getKind(Kind);
1101     if (verbose) {
1102       switch (Kind) {
1103       case MachO::DICE_KIND_DATA:
1104         outs() << "DATA";
1105         break;
1106       case MachO::DICE_KIND_JUMP_TABLE8:
1107         outs() << "JUMP_TABLE8";
1108         break;
1109       case MachO::DICE_KIND_JUMP_TABLE16:
1110         outs() << "JUMP_TABLE16";
1111         break;
1112       case MachO::DICE_KIND_JUMP_TABLE32:
1113         outs() << "JUMP_TABLE32";
1114         break;
1115       case MachO::DICE_KIND_ABS_JUMP_TABLE32:
1116         outs() << "ABS_JUMP_TABLE32";
1117         break;
1118       default:
1119         outs() << format("0x%04" PRIx32, Kind);
1120         break;
1121       }
1122     } else
1123       outs() << format("0x%04" PRIx32, Kind);
1124     outs() << "\n";
1125   }
1126 }
1127 
1128 static void PrintLinkOptHints(MachOObjectFile *O) {
1129   MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
1130   const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
1131   uint32_t nloh = LohLC.datasize;
1132   outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
1133   for (uint32_t i = 0; i < nloh;) {
1134     unsigned n;
1135     uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
1136     i += n;
1137     outs() << "    identifier " << identifier << " ";
1138     if (i >= nloh)
1139       return;
1140     switch (identifier) {
1141     case 1:
1142       outs() << "AdrpAdrp\n";
1143       break;
1144     case 2:
1145       outs() << "AdrpLdr\n";
1146       break;
1147     case 3:
1148       outs() << "AdrpAddLdr\n";
1149       break;
1150     case 4:
1151       outs() << "AdrpLdrGotLdr\n";
1152       break;
1153     case 5:
1154       outs() << "AdrpAddStr\n";
1155       break;
1156     case 6:
1157       outs() << "AdrpLdrGotStr\n";
1158       break;
1159     case 7:
1160       outs() << "AdrpAdd\n";
1161       break;
1162     case 8:
1163       outs() << "AdrpLdrGot\n";
1164       break;
1165     default:
1166       outs() << "Unknown identifier value\n";
1167       break;
1168     }
1169     uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
1170     i += n;
1171     outs() << "    narguments " << narguments << "\n";
1172     if (i >= nloh)
1173       return;
1174 
1175     for (uint32_t j = 0; j < narguments; j++) {
1176       uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
1177       i += n;
1178       outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
1179       if (i >= nloh)
1180         return;
1181     }
1182   }
1183 }
1184 
1185 static void PrintDylibs(MachOObjectFile *O, bool JustId) {
1186   unsigned Index = 0;
1187   for (const auto &Load : O->load_commands()) {
1188     if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
1189         (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
1190                      Load.C.cmd == MachO::LC_LOAD_DYLIB ||
1191                      Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
1192                      Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
1193                      Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
1194                      Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
1195       MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
1196       if (dl.dylib.name < dl.cmdsize) {
1197         const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
1198         if (JustId)
1199           outs() << p << "\n";
1200         else {
1201           outs() << "\t" << p;
1202           outs() << " (compatibility version "
1203                  << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
1204                  << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
1205                  << (dl.dylib.compatibility_version & 0xff) << ",";
1206           outs() << " current version "
1207                  << ((dl.dylib.current_version >> 16) & 0xffff) << "."
1208                  << ((dl.dylib.current_version >> 8) & 0xff) << "."
1209                  << (dl.dylib.current_version & 0xff) << ")\n";
1210         }
1211       } else {
1212         outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
1213         if (Load.C.cmd == MachO::LC_ID_DYLIB)
1214           outs() << "LC_ID_DYLIB ";
1215         else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
1216           outs() << "LC_LOAD_DYLIB ";
1217         else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
1218           outs() << "LC_LOAD_WEAK_DYLIB ";
1219         else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
1220           outs() << "LC_LAZY_LOAD_DYLIB ";
1221         else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
1222           outs() << "LC_REEXPORT_DYLIB ";
1223         else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
1224           outs() << "LC_LOAD_UPWARD_DYLIB ";
1225         else
1226           outs() << "LC_??? ";
1227         outs() << "command " << Index++ << "\n";
1228       }
1229     }
1230   }
1231 }
1232 
1233 typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
1234 
1235 static void CreateSymbolAddressMap(MachOObjectFile *O,
1236                                    SymbolAddressMap *AddrMap) {
1237   // Create a map of symbol addresses to symbol names.
1238   const StringRef FileName = O->getFileName();
1239   for (const SymbolRef &Symbol : O->symbols()) {
1240     SymbolRef::Type ST = unwrapOrError(Symbol.getType(), FileName);
1241     if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
1242         ST == SymbolRef::ST_Other) {
1243       uint64_t Address = Symbol.getValue();
1244       StringRef SymName = unwrapOrError(Symbol.getName(), FileName);
1245       if (!SymName.startswith(".objc"))
1246         (*AddrMap)[Address] = SymName;
1247     }
1248   }
1249 }
1250 
1251 // GuessSymbolName is passed the address of what might be a symbol and a
1252 // pointer to the SymbolAddressMap.  It returns the name of a symbol
1253 // with that address or nullptr if no symbol is found with that address.
1254 static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
1255   const char *SymbolName = nullptr;
1256   // A DenseMap can't lookup up some values.
1257   if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
1258     StringRef name = AddrMap->lookup(value);
1259     if (!name.empty())
1260       SymbolName = name.data();
1261   }
1262   return SymbolName;
1263 }
1264 
1265 static void DumpCstringChar(const char c) {
1266   char p[2];
1267   p[0] = c;
1268   p[1] = '\0';
1269   outs().write_escaped(p);
1270 }
1271 
1272 static void DumpCstringSection(MachOObjectFile *O, const char *sect,
1273                                uint32_t sect_size, uint64_t sect_addr,
1274                                bool print_addresses) {
1275   for (uint32_t i = 0; i < sect_size; i++) {
1276     if (print_addresses) {
1277       if (O->is64Bit())
1278         outs() << format("%016" PRIx64, sect_addr + i) << "  ";
1279       else
1280         outs() << format("%08" PRIx64, sect_addr + i) << "  ";
1281     }
1282     for (; i < sect_size && sect[i] != '\0'; i++)
1283       DumpCstringChar(sect[i]);
1284     if (i < sect_size && sect[i] == '\0')
1285       outs() << "\n";
1286   }
1287 }
1288 
1289 static void DumpLiteral4(uint32_t l, float f) {
1290   outs() << format("0x%08" PRIx32, l);
1291   if ((l & 0x7f800000) != 0x7f800000)
1292     outs() << format(" (%.16e)\n", f);
1293   else {
1294     if (l == 0x7f800000)
1295       outs() << " (+Infinity)\n";
1296     else if (l == 0xff800000)
1297       outs() << " (-Infinity)\n";
1298     else if ((l & 0x00400000) == 0x00400000)
1299       outs() << " (non-signaling Not-a-Number)\n";
1300     else
1301       outs() << " (signaling Not-a-Number)\n";
1302   }
1303 }
1304 
1305 static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
1306                                 uint32_t sect_size, uint64_t sect_addr,
1307                                 bool print_addresses) {
1308   for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
1309     if (print_addresses) {
1310       if (O->is64Bit())
1311         outs() << format("%016" PRIx64, sect_addr + i) << "  ";
1312       else
1313         outs() << format("%08" PRIx64, sect_addr + i) << "  ";
1314     }
1315     float f;
1316     memcpy(&f, sect + i, sizeof(float));
1317     if (O->isLittleEndian() != sys::IsLittleEndianHost)
1318       sys::swapByteOrder(f);
1319     uint32_t l;
1320     memcpy(&l, sect + i, sizeof(uint32_t));
1321     if (O->isLittleEndian() != sys::IsLittleEndianHost)
1322       sys::swapByteOrder(l);
1323     DumpLiteral4(l, f);
1324   }
1325 }
1326 
1327 static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
1328                          double d) {
1329   outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
1330   uint32_t Hi, Lo;
1331   Hi = (O->isLittleEndian()) ? l1 : l0;
1332   Lo = (O->isLittleEndian()) ? l0 : l1;
1333 
1334   // Hi is the high word, so this is equivalent to if(isfinite(d))
1335   if ((Hi & 0x7ff00000) != 0x7ff00000)
1336     outs() << format(" (%.16e)\n", d);
1337   else {
1338     if (Hi == 0x7ff00000 && Lo == 0)
1339       outs() << " (+Infinity)\n";
1340     else if (Hi == 0xfff00000 && Lo == 0)
1341       outs() << " (-Infinity)\n";
1342     else if ((Hi & 0x00080000) == 0x00080000)
1343       outs() << " (non-signaling Not-a-Number)\n";
1344     else
1345       outs() << " (signaling Not-a-Number)\n";
1346   }
1347 }
1348 
1349 static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
1350                                 uint32_t sect_size, uint64_t sect_addr,
1351                                 bool print_addresses) {
1352   for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
1353     if (print_addresses) {
1354       if (O->is64Bit())
1355         outs() << format("%016" PRIx64, sect_addr + i) << "  ";
1356       else
1357         outs() << format("%08" PRIx64, sect_addr + i) << "  ";
1358     }
1359     double d;
1360     memcpy(&d, sect + i, sizeof(double));
1361     if (O->isLittleEndian() != sys::IsLittleEndianHost)
1362       sys::swapByteOrder(d);
1363     uint32_t l0, l1;
1364     memcpy(&l0, sect + i, sizeof(uint32_t));
1365     memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
1366     if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1367       sys::swapByteOrder(l0);
1368       sys::swapByteOrder(l1);
1369     }
1370     DumpLiteral8(O, l0, l1, d);
1371   }
1372 }
1373 
1374 static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
1375   outs() << format("0x%08" PRIx32, l0) << " ";
1376   outs() << format("0x%08" PRIx32, l1) << " ";
1377   outs() << format("0x%08" PRIx32, l2) << " ";
1378   outs() << format("0x%08" PRIx32, l3) << "\n";
1379 }
1380 
1381 static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
1382                                  uint32_t sect_size, uint64_t sect_addr,
1383                                  bool print_addresses) {
1384   for (uint32_t i = 0; i < sect_size; i += 16) {
1385     if (print_addresses) {
1386       if (O->is64Bit())
1387         outs() << format("%016" PRIx64, sect_addr + i) << "  ";
1388       else
1389         outs() << format("%08" PRIx64, sect_addr + i) << "  ";
1390     }
1391     uint32_t l0, l1, l2, l3;
1392     memcpy(&l0, sect + i, sizeof(uint32_t));
1393     memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
1394     memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
1395     memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
1396     if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1397       sys::swapByteOrder(l0);
1398       sys::swapByteOrder(l1);
1399       sys::swapByteOrder(l2);
1400       sys::swapByteOrder(l3);
1401     }
1402     DumpLiteral16(l0, l1, l2, l3);
1403   }
1404 }
1405 
1406 static void DumpLiteralPointerSection(MachOObjectFile *O,
1407                                       const SectionRef &Section,
1408                                       const char *sect, uint32_t sect_size,
1409                                       uint64_t sect_addr,
1410                                       bool print_addresses) {
1411   // Collect the literal sections in this Mach-O file.
1412   std::vector<SectionRef> LiteralSections;
1413   for (const SectionRef &Section : O->sections()) {
1414     DataRefImpl Ref = Section.getRawDataRefImpl();
1415     uint32_t section_type;
1416     if (O->is64Bit()) {
1417       const MachO::section_64 Sec = O->getSection64(Ref);
1418       section_type = Sec.flags & MachO::SECTION_TYPE;
1419     } else {
1420       const MachO::section Sec = O->getSection(Ref);
1421       section_type = Sec.flags & MachO::SECTION_TYPE;
1422     }
1423     if (section_type == MachO::S_CSTRING_LITERALS ||
1424         section_type == MachO::S_4BYTE_LITERALS ||
1425         section_type == MachO::S_8BYTE_LITERALS ||
1426         section_type == MachO::S_16BYTE_LITERALS)
1427       LiteralSections.push_back(Section);
1428   }
1429 
1430   // Set the size of the literal pointer.
1431   uint32_t lp_size = O->is64Bit() ? 8 : 4;
1432 
1433   // Collect the external relocation symbols for the literal pointers.
1434   std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
1435   for (const RelocationRef &Reloc : Section.relocations()) {
1436     DataRefImpl Rel;
1437     MachO::any_relocation_info RE;
1438     bool isExtern = false;
1439     Rel = Reloc.getRawDataRefImpl();
1440     RE = O->getRelocation(Rel);
1441     isExtern = O->getPlainRelocationExternal(RE);
1442     if (isExtern) {
1443       uint64_t RelocOffset = Reloc.getOffset();
1444       symbol_iterator RelocSym = Reloc.getSymbol();
1445       Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
1446     }
1447   }
1448   array_pod_sort(Relocs.begin(), Relocs.end());
1449 
1450   // Dump each literal pointer.
1451   for (uint32_t i = 0; i < sect_size; i += lp_size) {
1452     if (print_addresses) {
1453       if (O->is64Bit())
1454         outs() << format("%016" PRIx64, sect_addr + i) << "  ";
1455       else
1456         outs() << format("%08" PRIx64, sect_addr + i) << "  ";
1457     }
1458     uint64_t lp;
1459     if (O->is64Bit()) {
1460       memcpy(&lp, sect + i, sizeof(uint64_t));
1461       if (O->isLittleEndian() != sys::IsLittleEndianHost)
1462         sys::swapByteOrder(lp);
1463     } else {
1464       uint32_t li;
1465       memcpy(&li, sect + i, sizeof(uint32_t));
1466       if (O->isLittleEndian() != sys::IsLittleEndianHost)
1467         sys::swapByteOrder(li);
1468       lp = li;
1469     }
1470 
1471     // First look for an external relocation entry for this literal pointer.
1472     auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) {
1473       return P.first == i;
1474     });
1475     if (Reloc != Relocs.end()) {
1476       symbol_iterator RelocSym = Reloc->second;
1477       StringRef SymName = unwrapOrError(RelocSym->getName(), O->getFileName());
1478       outs() << "external relocation entry for symbol:" << SymName << "\n";
1479       continue;
1480     }
1481 
1482     // For local references see what the section the literal pointer points to.
1483     auto Sect = find_if(LiteralSections, [&](const SectionRef &R) {
1484       return lp >= R.getAddress() && lp < R.getAddress() + R.getSize();
1485     });
1486     if (Sect == LiteralSections.end()) {
1487       outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
1488       continue;
1489     }
1490 
1491     uint64_t SectAddress = Sect->getAddress();
1492     uint64_t SectSize = Sect->getSize();
1493 
1494     StringRef SectName;
1495     Sect->getName(SectName);
1496     DataRefImpl Ref = Sect->getRawDataRefImpl();
1497     StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
1498     outs() << SegmentName << ":" << SectName << ":";
1499 
1500     uint32_t section_type;
1501     if (O->is64Bit()) {
1502       const MachO::section_64 Sec = O->getSection64(Ref);
1503       section_type = Sec.flags & MachO::SECTION_TYPE;
1504     } else {
1505       const MachO::section Sec = O->getSection(Ref);
1506       section_type = Sec.flags & MachO::SECTION_TYPE;
1507     }
1508 
1509     StringRef BytesStr = unwrapOrError(Sect->getContents(), O->getFileName());
1510 
1511     const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
1512 
1513     switch (section_type) {
1514     case MachO::S_CSTRING_LITERALS:
1515       for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
1516            i++) {
1517         DumpCstringChar(Contents[i]);
1518       }
1519       outs() << "\n";
1520       break;
1521     case MachO::S_4BYTE_LITERALS:
1522       float f;
1523       memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
1524       uint32_t l;
1525       memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
1526       if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1527         sys::swapByteOrder(f);
1528         sys::swapByteOrder(l);
1529       }
1530       DumpLiteral4(l, f);
1531       break;
1532     case MachO::S_8BYTE_LITERALS: {
1533       double d;
1534       memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
1535       uint32_t l0, l1;
1536       memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
1537       memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
1538              sizeof(uint32_t));
1539       if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1540         sys::swapByteOrder(f);
1541         sys::swapByteOrder(l0);
1542         sys::swapByteOrder(l1);
1543       }
1544       DumpLiteral8(O, l0, l1, d);
1545       break;
1546     }
1547     case MachO::S_16BYTE_LITERALS: {
1548       uint32_t l0, l1, l2, l3;
1549       memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
1550       memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
1551              sizeof(uint32_t));
1552       memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
1553              sizeof(uint32_t));
1554       memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
1555              sizeof(uint32_t));
1556       if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1557         sys::swapByteOrder(l0);
1558         sys::swapByteOrder(l1);
1559         sys::swapByteOrder(l2);
1560         sys::swapByteOrder(l3);
1561       }
1562       DumpLiteral16(l0, l1, l2, l3);
1563       break;
1564     }
1565     }
1566   }
1567 }
1568 
1569 static void DumpInitTermPointerSection(MachOObjectFile *O,
1570                                        const SectionRef &Section,
1571                                        const char *sect,
1572                                        uint32_t sect_size, uint64_t sect_addr,
1573                                        SymbolAddressMap *AddrMap,
1574                                        bool verbose) {
1575   uint32_t stride;
1576   stride = (O->is64Bit()) ? sizeof(uint64_t) : sizeof(uint32_t);
1577 
1578   // Collect the external relocation symbols for the pointers.
1579   std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
1580   for (const RelocationRef &Reloc : Section.relocations()) {
1581     DataRefImpl Rel;
1582     MachO::any_relocation_info RE;
1583     bool isExtern = false;
1584     Rel = Reloc.getRawDataRefImpl();
1585     RE = O->getRelocation(Rel);
1586     isExtern = O->getPlainRelocationExternal(RE);
1587     if (isExtern) {
1588       uint64_t RelocOffset = Reloc.getOffset();
1589       symbol_iterator RelocSym = Reloc.getSymbol();
1590       Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
1591     }
1592   }
1593   array_pod_sort(Relocs.begin(), Relocs.end());
1594 
1595   for (uint32_t i = 0; i < sect_size; i += stride) {
1596     const char *SymbolName = nullptr;
1597     uint64_t p;
1598     if (O->is64Bit()) {
1599       outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
1600       uint64_t pointer_value;
1601       memcpy(&pointer_value, sect + i, stride);
1602       if (O->isLittleEndian() != sys::IsLittleEndianHost)
1603         sys::swapByteOrder(pointer_value);
1604       outs() << format("0x%016" PRIx64, pointer_value);
1605       p = pointer_value;
1606     } else {
1607       outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
1608       uint32_t pointer_value;
1609       memcpy(&pointer_value, sect + i, stride);
1610       if (O->isLittleEndian() != sys::IsLittleEndianHost)
1611         sys::swapByteOrder(pointer_value);
1612       outs() << format("0x%08" PRIx32, pointer_value);
1613       p = pointer_value;
1614     }
1615     if (verbose) {
1616       // First look for an external relocation entry for this pointer.
1617       auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) {
1618         return P.first == i;
1619       });
1620       if (Reloc != Relocs.end()) {
1621         symbol_iterator RelocSym = Reloc->second;
1622         outs() << " " << unwrapOrError(RelocSym->getName(), O->getFileName());
1623       } else {
1624         SymbolName = GuessSymbolName(p, AddrMap);
1625         if (SymbolName)
1626           outs() << " " << SymbolName;
1627       }
1628     }
1629     outs() << "\n";
1630   }
1631 }
1632 
1633 static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
1634                                    uint32_t size, uint64_t addr) {
1635   uint32_t cputype = O->getHeader().cputype;
1636   if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
1637     uint32_t j;
1638     for (uint32_t i = 0; i < size; i += j, addr += j) {
1639       if (O->is64Bit())
1640         outs() << format("%016" PRIx64, addr) << "\t";
1641       else
1642         outs() << format("%08" PRIx64, addr) << "\t";
1643       for (j = 0; j < 16 && i + j < size; j++) {
1644         uint8_t byte_word = *(sect + i + j);
1645         outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
1646       }
1647       outs() << "\n";
1648     }
1649   } else {
1650     uint32_t j;
1651     for (uint32_t i = 0; i < size; i += j, addr += j) {
1652       if (O->is64Bit())
1653         outs() << format("%016" PRIx64, addr) << "\t";
1654       else
1655         outs() << format("%08" PRIx64, addr) << "\t";
1656       for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
1657            j += sizeof(int32_t)) {
1658         if (i + j + sizeof(int32_t) <= size) {
1659           uint32_t long_word;
1660           memcpy(&long_word, sect + i + j, sizeof(int32_t));
1661           if (O->isLittleEndian() != sys::IsLittleEndianHost)
1662             sys::swapByteOrder(long_word);
1663           outs() << format("%08" PRIx32, long_word) << " ";
1664         } else {
1665           for (uint32_t k = 0; i + j + k < size; k++) {
1666             uint8_t byte_word = *(sect + i + j + k);
1667             outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
1668           }
1669         }
1670       }
1671       outs() << "\n";
1672     }
1673   }
1674 }
1675 
1676 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
1677                              StringRef DisSegName, StringRef DisSectName);
1678 static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
1679                                 uint32_t size, uint32_t addr);
1680 #ifdef HAVE_LIBXAR
1681 static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
1682                                 uint32_t size, bool verbose,
1683                                 bool PrintXarHeader, bool PrintXarFileHeaders,
1684                                 std::string XarMemberName);
1685 #endif // defined(HAVE_LIBXAR)
1686 
1687 static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
1688                                 bool verbose) {
1689   SymbolAddressMap AddrMap;
1690   if (verbose)
1691     CreateSymbolAddressMap(O, &AddrMap);
1692 
1693   for (unsigned i = 0; i < FilterSections.size(); ++i) {
1694     StringRef DumpSection = FilterSections[i];
1695     std::pair<StringRef, StringRef> DumpSegSectName;
1696     DumpSegSectName = DumpSection.split(',');
1697     StringRef DumpSegName, DumpSectName;
1698     if (!DumpSegSectName.second.empty()) {
1699       DumpSegName = DumpSegSectName.first;
1700       DumpSectName = DumpSegSectName.second;
1701     } else {
1702       DumpSegName = "";
1703       DumpSectName = DumpSegSectName.first;
1704     }
1705     for (const SectionRef &Section : O->sections()) {
1706       StringRef SectName;
1707       Section.getName(SectName);
1708       DataRefImpl Ref = Section.getRawDataRefImpl();
1709       StringRef SegName = O->getSectionFinalSegmentName(Ref);
1710       if ((DumpSegName.empty() || SegName == DumpSegName) &&
1711           (SectName == DumpSectName)) {
1712 
1713         uint32_t section_flags;
1714         if (O->is64Bit()) {
1715           const MachO::section_64 Sec = O->getSection64(Ref);
1716           section_flags = Sec.flags;
1717 
1718         } else {
1719           const MachO::section Sec = O->getSection(Ref);
1720           section_flags = Sec.flags;
1721         }
1722         uint32_t section_type = section_flags & MachO::SECTION_TYPE;
1723 
1724         StringRef BytesStr =
1725             unwrapOrError(Section.getContents(), O->getFileName());
1726         const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1727         uint32_t sect_size = BytesStr.size();
1728         uint64_t sect_addr = Section.getAddress();
1729 
1730         outs() << "Contents of (" << SegName << "," << SectName
1731                << ") section\n";
1732 
1733         if (verbose) {
1734           if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1735               (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1736             DisassembleMachO(Filename, O, SegName, SectName);
1737             continue;
1738           }
1739           if (SegName == "__TEXT" && SectName == "__info_plist") {
1740             outs() << sect;
1741             continue;
1742           }
1743           if (SegName == "__OBJC" && SectName == "__protocol") {
1744             DumpProtocolSection(O, sect, sect_size, sect_addr);
1745             continue;
1746           }
1747 #ifdef HAVE_LIBXAR
1748           if (SegName == "__LLVM" && SectName == "__bundle") {
1749             DumpBitcodeSection(O, sect, sect_size, verbose, !NoSymbolicOperands,
1750                                ArchiveHeaders, "");
1751             continue;
1752           }
1753 #endif // defined(HAVE_LIBXAR)
1754           switch (section_type) {
1755           case MachO::S_REGULAR:
1756             DumpRawSectionContents(O, sect, sect_size, sect_addr);
1757             break;
1758           case MachO::S_ZEROFILL:
1759             outs() << "zerofill section and has no contents in the file\n";
1760             break;
1761           case MachO::S_CSTRING_LITERALS:
1762             DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1763             break;
1764           case MachO::S_4BYTE_LITERALS:
1765             DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1766             break;
1767           case MachO::S_8BYTE_LITERALS:
1768             DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1769             break;
1770           case MachO::S_16BYTE_LITERALS:
1771             DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1772             break;
1773           case MachO::S_LITERAL_POINTERS:
1774             DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
1775                                       !NoLeadingAddr);
1776             break;
1777           case MachO::S_MOD_INIT_FUNC_POINTERS:
1778           case MachO::S_MOD_TERM_FUNC_POINTERS:
1779             DumpInitTermPointerSection(O, Section, sect, sect_size, sect_addr,
1780                                        &AddrMap, verbose);
1781             break;
1782           default:
1783             outs() << "Unknown section type ("
1784                    << format("0x%08" PRIx32, section_type) << ")\n";
1785             DumpRawSectionContents(O, sect, sect_size, sect_addr);
1786             break;
1787           }
1788         } else {
1789           if (section_type == MachO::S_ZEROFILL)
1790             outs() << "zerofill section and has no contents in the file\n";
1791           else
1792             DumpRawSectionContents(O, sect, sect_size, sect_addr);
1793         }
1794       }
1795     }
1796   }
1797 }
1798 
1799 static void DumpInfoPlistSectionContents(StringRef Filename,
1800                                          MachOObjectFile *O) {
1801   for (const SectionRef &Section : O->sections()) {
1802     StringRef SectName;
1803     Section.getName(SectName);
1804     DataRefImpl Ref = Section.getRawDataRefImpl();
1805     StringRef SegName = O->getSectionFinalSegmentName(Ref);
1806     if (SegName == "__TEXT" && SectName == "__info_plist") {
1807       if (!NoLeadingHeaders)
1808         outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
1809       StringRef BytesStr =
1810           unwrapOrError(Section.getContents(), O->getFileName());
1811       const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1812       outs() << format("%.*s", BytesStr.size(), sect) << "\n";
1813       return;
1814     }
1815   }
1816 }
1817 
1818 // checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1819 // and if it is and there is a list of architecture flags is specified then
1820 // check to make sure this Mach-O file is one of those architectures or all
1821 // architectures were specified.  If not then an error is generated and this
1822 // routine returns false.  Else it returns true.
1823 static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
1824   auto *MachO = dyn_cast<MachOObjectFile>(O);
1825 
1826   if (!MachO || ArchAll || ArchFlags.empty())
1827     return true;
1828 
1829   MachO::mach_header H;
1830   MachO::mach_header_64 H_64;
1831   Triple T;
1832   const char *McpuDefault, *ArchFlag;
1833   if (MachO->is64Bit()) {
1834     H_64 = MachO->MachOObjectFile::getHeader64();
1835     T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype,
1836                                        &McpuDefault, &ArchFlag);
1837   } else {
1838     H = MachO->MachOObjectFile::getHeader();
1839     T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype,
1840                                        &McpuDefault, &ArchFlag);
1841   }
1842   const std::string ArchFlagName(ArchFlag);
1843   if (none_of(ArchFlags, [&](const std::string &Name) {
1844         return Name == ArchFlagName;
1845       })) {
1846     WithColor::error(errs(), "llvm-objdump")
1847         << Filename << ": no architecture specified.\n";
1848     return false;
1849   }
1850   return true;
1851 }
1852 
1853 static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1854 
1855 // ProcessMachO() is passed a single opened Mach-O file, which may be an
1856 // archive member and or in a slice of a universal file.  It prints the
1857 // the file name and header info and then processes it according to the
1858 // command line options.
1859 static void ProcessMachO(StringRef Name, MachOObjectFile *MachOOF,
1860                          StringRef ArchiveMemberName = StringRef(),
1861                          StringRef ArchitectureName = StringRef()) {
1862   // If we are doing some processing here on the Mach-O file print the header
1863   // info.  And don't print it otherwise like in the case of printing the
1864   // UniversalHeaders or ArchiveHeaders.
1865   if (Disassemble || Relocations || PrivateHeaders || ExportsTrie || Rebase ||
1866       Bind || SymbolTable || LazyBind || WeakBind || IndirectSymbols ||
1867       DataInCode || LinkOptHints || DylibsUsed || DylibId || ObjcMetaData ||
1868       (!FilterSections.empty())) {
1869     if (!NoLeadingHeaders) {
1870       outs() << Name;
1871       if (!ArchiveMemberName.empty())
1872         outs() << '(' << ArchiveMemberName << ')';
1873       if (!ArchitectureName.empty())
1874         outs() << " (architecture " << ArchitectureName << ")";
1875       outs() << ":\n";
1876     }
1877   }
1878   // To use the report_error() form with an ArchiveName and FileName set
1879   // these up based on what is passed for Name and ArchiveMemberName.
1880   StringRef ArchiveName;
1881   StringRef FileName;
1882   if (!ArchiveMemberName.empty()) {
1883     ArchiveName = Name;
1884     FileName = ArchiveMemberName;
1885   } else {
1886     ArchiveName = StringRef();
1887     FileName = Name;
1888   }
1889 
1890   // If we need the symbol table to do the operation then check it here to
1891   // produce a good error message as to where the Mach-O file comes from in
1892   // the error message.
1893   if (Disassemble || IndirectSymbols || !FilterSections.empty() || UnwindInfo)
1894     if (Error Err = MachOOF->checkSymbolTable())
1895       report_error(std::move(Err), ArchiveName, FileName, ArchitectureName);
1896 
1897   if (DisassembleAll) {
1898     for (const SectionRef &Section : MachOOF->sections()) {
1899       StringRef SectName;
1900       Section.getName(SectName);
1901       if (SectName.equals("__text")) {
1902         DataRefImpl Ref = Section.getRawDataRefImpl();
1903         StringRef SegName = MachOOF->getSectionFinalSegmentName(Ref);
1904         DisassembleMachO(FileName, MachOOF, SegName, SectName);
1905       }
1906     }
1907   }
1908   else if (Disassemble) {
1909     if (MachOOF->getHeader().filetype == MachO::MH_KEXT_BUNDLE &&
1910         MachOOF->getHeader().cputype == MachO::CPU_TYPE_ARM64)
1911       DisassembleMachO(FileName, MachOOF, "__TEXT_EXEC", "__text");
1912     else
1913       DisassembleMachO(FileName, MachOOF, "__TEXT", "__text");
1914   }
1915   if (IndirectSymbols)
1916     PrintIndirectSymbols(MachOOF, !NonVerbose);
1917   if (DataInCode)
1918     PrintDataInCodeTable(MachOOF, !NonVerbose);
1919   if (LinkOptHints)
1920     PrintLinkOptHints(MachOOF);
1921   if (Relocations)
1922     PrintRelocations(MachOOF, !NonVerbose);
1923   if (SectionHeaders)
1924     printSectionHeaders(MachOOF);
1925   if (SectionContents)
1926     printSectionContents(MachOOF);
1927   if (!FilterSections.empty())
1928     DumpSectionContents(FileName, MachOOF, !NonVerbose);
1929   if (InfoPlist)
1930     DumpInfoPlistSectionContents(FileName, MachOOF);
1931   if (DylibsUsed)
1932     PrintDylibs(MachOOF, false);
1933   if (DylibId)
1934     PrintDylibs(MachOOF, true);
1935   if (SymbolTable)
1936     printSymbolTable(MachOOF, ArchiveName, ArchitectureName);
1937   if (UnwindInfo)
1938     printMachOUnwindInfo(MachOOF);
1939   if (PrivateHeaders) {
1940     printMachOFileHeader(MachOOF);
1941     printMachOLoadCommands(MachOOF);
1942   }
1943   if (FirstPrivateHeader)
1944     printMachOFileHeader(MachOOF);
1945   if (ObjcMetaData)
1946     printObjcMetaData(MachOOF, !NonVerbose);
1947   if (ExportsTrie)
1948     printExportsTrie(MachOOF);
1949   if (Rebase)
1950     printRebaseTable(MachOOF);
1951   if (Bind)
1952     printBindTable(MachOOF);
1953   if (LazyBind)
1954     printLazyBindTable(MachOOF);
1955   if (WeakBind)
1956     printWeakBindTable(MachOOF);
1957 
1958   if (DwarfDumpType != DIDT_Null) {
1959     std::unique_ptr<DIContext> DICtx = DWARFContext::create(*MachOOF);
1960     // Dump the complete DWARF structure.
1961     DIDumpOptions DumpOpts;
1962     DumpOpts.DumpType = DwarfDumpType;
1963     DICtx->dump(outs(), DumpOpts);
1964   }
1965 }
1966 
1967 // printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1968 static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
1969   outs() << "    cputype (" << cputype << ")\n";
1970   outs() << "    cpusubtype (" << cpusubtype << ")\n";
1971 }
1972 
1973 // printCPUType() helps print_fat_headers by printing the cputype and
1974 // pusubtype (symbolically for the one's it knows about).
1975 static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
1976   switch (cputype) {
1977   case MachO::CPU_TYPE_I386:
1978     switch (cpusubtype) {
1979     case MachO::CPU_SUBTYPE_I386_ALL:
1980       outs() << "    cputype CPU_TYPE_I386\n";
1981       outs() << "    cpusubtype CPU_SUBTYPE_I386_ALL\n";
1982       break;
1983     default:
1984       printUnknownCPUType(cputype, cpusubtype);
1985       break;
1986     }
1987     break;
1988   case MachO::CPU_TYPE_X86_64:
1989     switch (cpusubtype) {
1990     case MachO::CPU_SUBTYPE_X86_64_ALL:
1991       outs() << "    cputype CPU_TYPE_X86_64\n";
1992       outs() << "    cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1993       break;
1994     case MachO::CPU_SUBTYPE_X86_64_H:
1995       outs() << "    cputype CPU_TYPE_X86_64\n";
1996       outs() << "    cpusubtype CPU_SUBTYPE_X86_64_H\n";
1997       break;
1998     default:
1999       printUnknownCPUType(cputype, cpusubtype);
2000       break;
2001     }
2002     break;
2003   case MachO::CPU_TYPE_ARM:
2004     switch (cpusubtype) {
2005     case MachO::CPU_SUBTYPE_ARM_ALL:
2006       outs() << "    cputype CPU_TYPE_ARM\n";
2007       outs() << "    cpusubtype CPU_SUBTYPE_ARM_ALL\n";
2008       break;
2009     case MachO::CPU_SUBTYPE_ARM_V4T:
2010       outs() << "    cputype CPU_TYPE_ARM\n";
2011       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V4T\n";
2012       break;
2013     case MachO::CPU_SUBTYPE_ARM_V5TEJ:
2014       outs() << "    cputype CPU_TYPE_ARM\n";
2015       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
2016       break;
2017     case MachO::CPU_SUBTYPE_ARM_XSCALE:
2018       outs() << "    cputype CPU_TYPE_ARM\n";
2019       outs() << "    cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
2020       break;
2021     case MachO::CPU_SUBTYPE_ARM_V6:
2022       outs() << "    cputype CPU_TYPE_ARM\n";
2023       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V6\n";
2024       break;
2025     case MachO::CPU_SUBTYPE_ARM_V6M:
2026       outs() << "    cputype CPU_TYPE_ARM\n";
2027       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V6M\n";
2028       break;
2029     case MachO::CPU_SUBTYPE_ARM_V7:
2030       outs() << "    cputype CPU_TYPE_ARM\n";
2031       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V7\n";
2032       break;
2033     case MachO::CPU_SUBTYPE_ARM_V7EM:
2034       outs() << "    cputype CPU_TYPE_ARM\n";
2035       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
2036       break;
2037     case MachO::CPU_SUBTYPE_ARM_V7K:
2038       outs() << "    cputype CPU_TYPE_ARM\n";
2039       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V7K\n";
2040       break;
2041     case MachO::CPU_SUBTYPE_ARM_V7M:
2042       outs() << "    cputype CPU_TYPE_ARM\n";
2043       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V7M\n";
2044       break;
2045     case MachO::CPU_SUBTYPE_ARM_V7S:
2046       outs() << "    cputype CPU_TYPE_ARM\n";
2047       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V7S\n";
2048       break;
2049     default:
2050       printUnknownCPUType(cputype, cpusubtype);
2051       break;
2052     }
2053     break;
2054   case MachO::CPU_TYPE_ARM64:
2055     switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
2056     case MachO::CPU_SUBTYPE_ARM64_ALL:
2057       outs() << "    cputype CPU_TYPE_ARM64\n";
2058       outs() << "    cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
2059       break;
2060     case MachO::CPU_SUBTYPE_ARM64E:
2061       outs() << "    cputype CPU_TYPE_ARM64\n";
2062       outs() << "    cpusubtype CPU_SUBTYPE_ARM64E\n";
2063       break;
2064     default:
2065       printUnknownCPUType(cputype, cpusubtype);
2066       break;
2067     }
2068     break;
2069   case MachO::CPU_TYPE_ARM64_32:
2070     switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
2071     case MachO::CPU_SUBTYPE_ARM64_32_V8:
2072       outs() << "    cputype CPU_TYPE_ARM64_32\n";
2073       outs() << "    cpusubtype CPU_SUBTYPE_ARM64_32_V8\n";
2074       break;
2075     default:
2076       printUnknownCPUType(cputype, cpusubtype);
2077       break;
2078     }
2079     break;
2080   default:
2081     printUnknownCPUType(cputype, cpusubtype);
2082     break;
2083   }
2084 }
2085 
2086 static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
2087                                        bool verbose) {
2088   outs() << "Fat headers\n";
2089   if (verbose) {
2090     if (UB->getMagic() == MachO::FAT_MAGIC)
2091       outs() << "fat_magic FAT_MAGIC\n";
2092     else // UB->getMagic() == MachO::FAT_MAGIC_64
2093       outs() << "fat_magic FAT_MAGIC_64\n";
2094   } else
2095     outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
2096 
2097   uint32_t nfat_arch = UB->getNumberOfObjects();
2098   StringRef Buf = UB->getData();
2099   uint64_t size = Buf.size();
2100   uint64_t big_size = sizeof(struct MachO::fat_header) +
2101                       nfat_arch * sizeof(struct MachO::fat_arch);
2102   outs() << "nfat_arch " << UB->getNumberOfObjects();
2103   if (nfat_arch == 0)
2104     outs() << " (malformed, contains zero architecture types)\n";
2105   else if (big_size > size)
2106     outs() << " (malformed, architectures past end of file)\n";
2107   else
2108     outs() << "\n";
2109 
2110   for (uint32_t i = 0; i < nfat_arch; ++i) {
2111     MachOUniversalBinary::ObjectForArch OFA(UB, i);
2112     uint32_t cputype = OFA.getCPUType();
2113     uint32_t cpusubtype = OFA.getCPUSubType();
2114     outs() << "architecture ";
2115     for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
2116       MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
2117       uint32_t other_cputype = other_OFA.getCPUType();
2118       uint32_t other_cpusubtype = other_OFA.getCPUSubType();
2119       if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
2120           (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
2121               (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
2122         outs() << "(illegal duplicate architecture) ";
2123         break;
2124       }
2125     }
2126     if (verbose) {
2127       outs() << OFA.getArchFlagName() << "\n";
2128       printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
2129     } else {
2130       outs() << i << "\n";
2131       outs() << "    cputype " << cputype << "\n";
2132       outs() << "    cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
2133              << "\n";
2134     }
2135     if (verbose &&
2136         (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
2137       outs() << "    capabilities CPU_SUBTYPE_LIB64\n";
2138     else
2139       outs() << "    capabilities "
2140              << format("0x%" PRIx32,
2141                        (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
2142     outs() << "    offset " << OFA.getOffset();
2143     if (OFA.getOffset() > size)
2144       outs() << " (past end of file)";
2145     if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
2146       outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
2147     outs() << "\n";
2148     outs() << "    size " << OFA.getSize();
2149     big_size = OFA.getOffset() + OFA.getSize();
2150     if (big_size > size)
2151       outs() << " (past end of file)";
2152     outs() << "\n";
2153     outs() << "    align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
2154            << ")\n";
2155   }
2156 }
2157 
2158 static void printArchiveChild(StringRef Filename, const Archive::Child &C,
2159                               bool verbose, bool print_offset,
2160                               StringRef ArchitectureName = StringRef()) {
2161   if (print_offset)
2162     outs() << C.getChildOffset() << "\t";
2163   sys::fs::perms Mode =
2164       unwrapOrError(C.getAccessMode(), Filename, C, ArchitectureName);
2165   if (verbose) {
2166     // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
2167     // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
2168     outs() << "-";
2169     outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
2170     outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
2171     outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
2172     outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
2173     outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
2174     outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
2175     outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
2176     outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
2177     outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
2178   } else {
2179     outs() << format("0%o ", Mode);
2180   }
2181 
2182   outs() << format(
2183       "%3d/%-3d %5" PRId64 " ",
2184       unwrapOrError(C.getUID(), Filename, C, ArchitectureName),
2185       unwrapOrError(C.getGID(), Filename, C, ArchitectureName),
2186       unwrapOrError(C.getRawSize(), Filename, C, ArchitectureName));
2187 
2188   StringRef RawLastModified = C.getRawLastModified();
2189   if (verbose) {
2190     unsigned Seconds;
2191     if (RawLastModified.getAsInteger(10, Seconds))
2192       outs() << "(date: \"" << RawLastModified
2193              << "\" contains non-decimal chars) ";
2194     else {
2195       // Since cime(3) returns a 26 character string of the form:
2196       // "Sun Sep 16 01:03:52 1973\n\0"
2197       // just print 24 characters.
2198       time_t t = Seconds;
2199       outs() << format("%.24s ", ctime(&t));
2200     }
2201   } else {
2202     outs() << RawLastModified << " ";
2203   }
2204 
2205   if (verbose) {
2206     Expected<StringRef> NameOrErr = C.getName();
2207     if (!NameOrErr) {
2208       consumeError(NameOrErr.takeError());
2209       outs() << unwrapOrError(C.getRawName(), Filename, C, ArchitectureName)
2210              << "\n";
2211     } else {
2212       StringRef Name = NameOrErr.get();
2213       outs() << Name << "\n";
2214     }
2215   } else {
2216     outs() << unwrapOrError(C.getRawName(), Filename, C, ArchitectureName)
2217            << "\n";
2218   }
2219 }
2220 
2221 static void printArchiveHeaders(StringRef Filename, Archive *A, bool verbose,
2222                                 bool print_offset,
2223                                 StringRef ArchitectureName = StringRef()) {
2224   Error Err = Error::success();
2225   for (const auto &C : A->children(Err, false))
2226     printArchiveChild(Filename, C, verbose, print_offset, ArchitectureName);
2227 
2228   if (Err)
2229     report_error(std::move(Err), StringRef(), Filename, ArchitectureName);
2230 }
2231 
2232 static bool ValidateArchFlags() {
2233   // Check for -arch all and verifiy the -arch flags are valid.
2234   for (unsigned i = 0; i < ArchFlags.size(); ++i) {
2235     if (ArchFlags[i] == "all") {
2236       ArchAll = true;
2237     } else {
2238       if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
2239         WithColor::error(errs(), "llvm-objdump")
2240             << "unknown architecture named '" + ArchFlags[i] +
2241                    "'for the -arch option\n";
2242         return false;
2243       }
2244     }
2245   }
2246   return true;
2247 }
2248 
2249 // ParseInputMachO() parses the named Mach-O file in Filename and handles the
2250 // -arch flags selecting just those slices as specified by them and also parses
2251 // archive files.  Then for each individual Mach-O file ProcessMachO() is
2252 // called to process the file based on the command line options.
2253 void parseInputMachO(StringRef Filename) {
2254   if (!ValidateArchFlags())
2255     return;
2256 
2257   // Attempt to open the binary.
2258   Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
2259   if (!BinaryOrErr) {
2260     if (Error E = isNotObjectErrorInvalidFileType(BinaryOrErr.takeError()))
2261       report_error(std::move(E), Filename);
2262     else
2263       outs() << Filename << ": is not an object file\n";
2264     return;
2265   }
2266   Binary &Bin = *BinaryOrErr.get().getBinary();
2267 
2268   if (Archive *A = dyn_cast<Archive>(&Bin)) {
2269     outs() << "Archive : " << Filename << "\n";
2270     if (ArchiveHeaders)
2271       printArchiveHeaders(Filename, A, !NonVerbose, ArchiveMemberOffsets);
2272 
2273     Error Err = Error::success();
2274     for (auto &C : A->children(Err)) {
2275       Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2276       if (!ChildOrErr) {
2277         if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
2278           report_error(std::move(E), Filename, C);
2279         continue;
2280       }
2281       if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
2282         if (!checkMachOAndArchFlags(O, Filename))
2283           return;
2284         ProcessMachO(Filename, O, O->getFileName());
2285       }
2286     }
2287     if (Err)
2288       report_error(std::move(Err), Filename);
2289     return;
2290   }
2291   if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
2292     parseInputMachO(UB);
2293     return;
2294   }
2295   if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
2296     if (!checkMachOAndArchFlags(O, Filename))
2297       return;
2298     if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O))
2299       ProcessMachO(Filename, MachOOF);
2300     else
2301       WithColor::error(errs(), "llvm-objdump")
2302           << Filename << "': "
2303           << "object is not a Mach-O file type.\n";
2304     return;
2305   }
2306   llvm_unreachable("Input object can't be invalid at this point");
2307 }
2308 
2309 void parseInputMachO(MachOUniversalBinary *UB) {
2310   if (!ValidateArchFlags())
2311     return;
2312 
2313   auto Filename = UB->getFileName();
2314 
2315   if (UniversalHeaders)
2316     printMachOUniversalHeaders(UB, !NonVerbose);
2317 
2318   // If we have a list of architecture flags specified dump only those.
2319   if (!ArchAll && !ArchFlags.empty()) {
2320     // Look for a slice in the universal binary that matches each ArchFlag.
2321     bool ArchFound;
2322     for (unsigned i = 0; i < ArchFlags.size(); ++i) {
2323       ArchFound = false;
2324       for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
2325                                                   E = UB->end_objects();
2326             I != E; ++I) {
2327         if (ArchFlags[i] == I->getArchFlagName()) {
2328           ArchFound = true;
2329           Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
2330               I->getAsObjectFile();
2331           std::string ArchitectureName = "";
2332           if (ArchFlags.size() > 1)
2333             ArchitectureName = I->getArchFlagName();
2334           if (ObjOrErr) {
2335             ObjectFile &O = *ObjOrErr.get();
2336             if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
2337               ProcessMachO(Filename, MachOOF, "", ArchitectureName);
2338           } else if (Error E = isNotObjectErrorInvalidFileType(
2339                          ObjOrErr.takeError())) {
2340             report_error(std::move(E), Filename, StringRef(), ArchitectureName);
2341             continue;
2342           } else if (Expected<std::unique_ptr<Archive>> AOrErr =
2343                          I->getAsArchive()) {
2344             std::unique_ptr<Archive> &A = *AOrErr;
2345             outs() << "Archive : " << Filename;
2346             if (!ArchitectureName.empty())
2347               outs() << " (architecture " << ArchitectureName << ")";
2348             outs() << "\n";
2349             if (ArchiveHeaders)
2350               printArchiveHeaders(Filename, A.get(), !NonVerbose,
2351                                   ArchiveMemberOffsets, ArchitectureName);
2352             Error Err = Error::success();
2353             for (auto &C : A->children(Err)) {
2354               Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2355               if (!ChildOrErr) {
2356                 if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
2357                   report_error(std::move(E), Filename, C, ArchitectureName);
2358                 continue;
2359               }
2360               if (MachOObjectFile *O =
2361                       dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
2362                 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
2363             }
2364             if (Err)
2365               report_error(std::move(Err), Filename);
2366           } else {
2367             consumeError(AOrErr.takeError());
2368             error("Mach-O universal file: " + Filename + " for " +
2369                   "architecture " + StringRef(I->getArchFlagName()) +
2370                   " is not a Mach-O file or an archive file");
2371           }
2372         }
2373       }
2374       if (!ArchFound) {
2375         WithColor::error(errs(), "llvm-objdump")
2376             << "file: " + Filename + " does not contain "
2377             << "architecture: " + ArchFlags[i] + "\n";
2378         return;
2379       }
2380     }
2381     return;
2382   }
2383   // No architecture flags were specified so if this contains a slice that
2384   // matches the host architecture dump only that.
2385   if (!ArchAll) {
2386     for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
2387                                                 E = UB->end_objects();
2388           I != E; ++I) {
2389       if (MachOObjectFile::getHostArch().getArchName() ==
2390           I->getArchFlagName()) {
2391         Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
2392         std::string ArchiveName;
2393         ArchiveName.clear();
2394         if (ObjOrErr) {
2395           ObjectFile &O = *ObjOrErr.get();
2396           if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
2397             ProcessMachO(Filename, MachOOF);
2398         } else if (Error E =
2399                        isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) {
2400           report_error(std::move(E), Filename);
2401         } else if (Expected<std::unique_ptr<Archive>> AOrErr =
2402                        I->getAsArchive()) {
2403           std::unique_ptr<Archive> &A = *AOrErr;
2404           outs() << "Archive : " << Filename << "\n";
2405           if (ArchiveHeaders)
2406             printArchiveHeaders(Filename, A.get(), !NonVerbose,
2407                                 ArchiveMemberOffsets);
2408           Error Err = Error::success();
2409           for (auto &C : A->children(Err)) {
2410             Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2411             if (!ChildOrErr) {
2412               if (Error E =
2413                       isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
2414                 report_error(std::move(E), Filename, C);
2415               continue;
2416             }
2417             if (MachOObjectFile *O =
2418                     dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
2419               ProcessMachO(Filename, O, O->getFileName());
2420           }
2421           if (Err)
2422             report_error(std::move(Err), Filename);
2423         } else {
2424           consumeError(AOrErr.takeError());
2425           error("Mach-O universal file: " + Filename + " for architecture " +
2426                 StringRef(I->getArchFlagName()) +
2427                 " is not a Mach-O file or an archive file");
2428         }
2429         return;
2430       }
2431     }
2432   }
2433   // Either all architectures have been specified or none have been specified
2434   // and this does not contain the host architecture so dump all the slices.
2435   bool moreThanOneArch = UB->getNumberOfObjects() > 1;
2436   for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
2437                                               E = UB->end_objects();
2438         I != E; ++I) {
2439     Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
2440     std::string ArchitectureName = "";
2441     if (moreThanOneArch)
2442       ArchitectureName = I->getArchFlagName();
2443     if (ObjOrErr) {
2444       ObjectFile &Obj = *ObjOrErr.get();
2445       if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
2446         ProcessMachO(Filename, MachOOF, "", ArchitectureName);
2447     } else if (Error E =
2448                    isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) {
2449       report_error(std::move(E), StringRef(), Filename, ArchitectureName);
2450     } else if (Expected<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
2451       std::unique_ptr<Archive> &A = *AOrErr;
2452       outs() << "Archive : " << Filename;
2453       if (!ArchitectureName.empty())
2454         outs() << " (architecture " << ArchitectureName << ")";
2455       outs() << "\n";
2456       if (ArchiveHeaders)
2457         printArchiveHeaders(Filename, A.get(), !NonVerbose,
2458                             ArchiveMemberOffsets, ArchitectureName);
2459       Error Err = Error::success();
2460       for (auto &C : A->children(Err)) {
2461         Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2462         if (!ChildOrErr) {
2463           if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
2464             report_error(std::move(E), Filename, C, ArchitectureName);
2465           continue;
2466         }
2467         if (MachOObjectFile *O =
2468                 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
2469           if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
2470             ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
2471                           ArchitectureName);
2472         }
2473       }
2474       if (Err)
2475         report_error(std::move(Err), Filename);
2476     } else {
2477       consumeError(AOrErr.takeError());
2478       error("Mach-O universal file: " + Filename + " for architecture " +
2479             StringRef(I->getArchFlagName()) +
2480             " is not a Mach-O file or an archive file");
2481     }
2482   }
2483 }
2484 
2485 // The block of info used by the Symbolizer call backs.
2486 struct DisassembleInfo {
2487   DisassembleInfo(MachOObjectFile *O, SymbolAddressMap *AddrMap,
2488                   std::vector<SectionRef> *Sections, bool verbose)
2489     : verbose(verbose), O(O), AddrMap(AddrMap), Sections(Sections) {}
2490   bool verbose;
2491   MachOObjectFile *O;
2492   SectionRef S;
2493   SymbolAddressMap *AddrMap;
2494   std::vector<SectionRef> *Sections;
2495   const char *class_name = nullptr;
2496   const char *selector_name = nullptr;
2497   std::unique_ptr<char[]> method = nullptr;
2498   char *demangled_name = nullptr;
2499   uint64_t adrp_addr = 0;
2500   uint32_t adrp_inst = 0;
2501   std::unique_ptr<SymbolAddressMap> bindtable;
2502   uint32_t depth = 0;
2503 };
2504 
2505 // SymbolizerGetOpInfo() is the operand information call back function.
2506 // This is called to get the symbolic information for operand(s) of an
2507 // instruction when it is being done.  This routine does this from
2508 // the relocation information, symbol table, etc. That block of information
2509 // is a pointer to the struct DisassembleInfo that was passed when the
2510 // disassembler context was created and passed to back to here when
2511 // called back by the disassembler for instruction operands that could have
2512 // relocation information. The address of the instruction containing operand is
2513 // at the Pc parameter.  The immediate value the operand has is passed in
2514 // op_info->Value and is at Offset past the start of the instruction and has a
2515 // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
2516 // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
2517 // names and addends of the symbolic expression to add for the operand.  The
2518 // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
2519 // information is returned then this function returns 1 else it returns 0.
2520 static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
2521                                uint64_t Size, int TagType, void *TagBuf) {
2522   struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
2523   struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
2524   uint64_t value = op_info->Value;
2525 
2526   // Make sure all fields returned are zero if we don't set them.
2527   memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
2528   op_info->Value = value;
2529 
2530   // If the TagType is not the value 1 which it code knows about or if no
2531   // verbose symbolic information is wanted then just return 0, indicating no
2532   // information is being returned.
2533   if (TagType != 1 || !info->verbose)
2534     return 0;
2535 
2536   unsigned int Arch = info->O->getArch();
2537   if (Arch == Triple::x86) {
2538     if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
2539       return 0;
2540     if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2541       // TODO:
2542       // Search the external relocation entries of a fully linked image
2543       // (if any) for an entry that matches this segment offset.
2544       // uint32_t seg_offset = (Pc + Offset);
2545       return 0;
2546     }
2547     // In MH_OBJECT filetypes search the section's relocation entries (if any)
2548     // for an entry for this section offset.
2549     uint32_t sect_addr = info->S.getAddress();
2550     uint32_t sect_offset = (Pc + Offset) - sect_addr;
2551     bool reloc_found = false;
2552     DataRefImpl Rel;
2553     MachO::any_relocation_info RE;
2554     bool isExtern = false;
2555     SymbolRef Symbol;
2556     bool r_scattered = false;
2557     uint32_t r_value, pair_r_value, r_type;
2558     for (const RelocationRef &Reloc : info->S.relocations()) {
2559       uint64_t RelocOffset = Reloc.getOffset();
2560       if (RelocOffset == sect_offset) {
2561         Rel = Reloc.getRawDataRefImpl();
2562         RE = info->O->getRelocation(Rel);
2563         r_type = info->O->getAnyRelocationType(RE);
2564         r_scattered = info->O->isRelocationScattered(RE);
2565         if (r_scattered) {
2566           r_value = info->O->getScatteredRelocationValue(RE);
2567           if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
2568               r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
2569             DataRefImpl RelNext = Rel;
2570             info->O->moveRelocationNext(RelNext);
2571             MachO::any_relocation_info RENext;
2572             RENext = info->O->getRelocation(RelNext);
2573             if (info->O->isRelocationScattered(RENext))
2574               pair_r_value = info->O->getScatteredRelocationValue(RENext);
2575             else
2576               return 0;
2577           }
2578         } else {
2579           isExtern = info->O->getPlainRelocationExternal(RE);
2580           if (isExtern) {
2581             symbol_iterator RelocSym = Reloc.getSymbol();
2582             Symbol = *RelocSym;
2583           }
2584         }
2585         reloc_found = true;
2586         break;
2587       }
2588     }
2589     if (reloc_found && isExtern) {
2590       op_info->AddSymbol.Present = 1;
2591       op_info->AddSymbol.Name =
2592           unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
2593       // For i386 extern relocation entries the value in the instruction is
2594       // the offset from the symbol, and value is already set in op_info->Value.
2595       return 1;
2596     }
2597     if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
2598                         r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
2599       const char *add = GuessSymbolName(r_value, info->AddrMap);
2600       const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
2601       uint32_t offset = value - (r_value - pair_r_value);
2602       op_info->AddSymbol.Present = 1;
2603       if (add != nullptr)
2604         op_info->AddSymbol.Name = add;
2605       else
2606         op_info->AddSymbol.Value = r_value;
2607       op_info->SubtractSymbol.Present = 1;
2608       if (sub != nullptr)
2609         op_info->SubtractSymbol.Name = sub;
2610       else
2611         op_info->SubtractSymbol.Value = pair_r_value;
2612       op_info->Value = offset;
2613       return 1;
2614     }
2615     return 0;
2616   }
2617   if (Arch == Triple::x86_64) {
2618     if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
2619       return 0;
2620     // For non MH_OBJECT types, like MH_KEXT_BUNDLE, Search the external
2621     // relocation entries of a linked image (if any) for an entry that matches
2622     // this segment offset.
2623     if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2624       uint64_t seg_offset = Pc + Offset;
2625       bool reloc_found = false;
2626       DataRefImpl Rel;
2627       MachO::any_relocation_info RE;
2628       bool isExtern = false;
2629       SymbolRef Symbol;
2630       for (const RelocationRef &Reloc : info->O->external_relocations()) {
2631         uint64_t RelocOffset = Reloc.getOffset();
2632         if (RelocOffset == seg_offset) {
2633           Rel = Reloc.getRawDataRefImpl();
2634           RE = info->O->getRelocation(Rel);
2635           // external relocation entries should always be external.
2636           isExtern = info->O->getPlainRelocationExternal(RE);
2637           if (isExtern) {
2638             symbol_iterator RelocSym = Reloc.getSymbol();
2639             Symbol = *RelocSym;
2640           }
2641           reloc_found = true;
2642           break;
2643         }
2644       }
2645       if (reloc_found && isExtern) {
2646         // The Value passed in will be adjusted by the Pc if the instruction
2647         // adds the Pc.  But for x86_64 external relocation entries the Value
2648         // is the offset from the external symbol.
2649         if (info->O->getAnyRelocationPCRel(RE))
2650           op_info->Value -= Pc + Offset + Size;
2651         const char *name =
2652             unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
2653         op_info->AddSymbol.Present = 1;
2654         op_info->AddSymbol.Name = name;
2655         return 1;
2656       }
2657       return 0;
2658     }
2659     // In MH_OBJECT filetypes search the section's relocation entries (if any)
2660     // for an entry for this section offset.
2661     uint64_t sect_addr = info->S.getAddress();
2662     uint64_t sect_offset = (Pc + Offset) - sect_addr;
2663     bool reloc_found = false;
2664     DataRefImpl Rel;
2665     MachO::any_relocation_info RE;
2666     bool isExtern = false;
2667     SymbolRef Symbol;
2668     for (const RelocationRef &Reloc : info->S.relocations()) {
2669       uint64_t RelocOffset = Reloc.getOffset();
2670       if (RelocOffset == sect_offset) {
2671         Rel = Reloc.getRawDataRefImpl();
2672         RE = info->O->getRelocation(Rel);
2673         // NOTE: Scattered relocations don't exist on x86_64.
2674         isExtern = info->O->getPlainRelocationExternal(RE);
2675         if (isExtern) {
2676           symbol_iterator RelocSym = Reloc.getSymbol();
2677           Symbol = *RelocSym;
2678         }
2679         reloc_found = true;
2680         break;
2681       }
2682     }
2683     if (reloc_found && isExtern) {
2684       // The Value passed in will be adjusted by the Pc if the instruction
2685       // adds the Pc.  But for x86_64 external relocation entries the Value
2686       // is the offset from the external symbol.
2687       if (info->O->getAnyRelocationPCRel(RE))
2688         op_info->Value -= Pc + Offset + Size;
2689       const char *name =
2690           unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
2691       unsigned Type = info->O->getAnyRelocationType(RE);
2692       if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
2693         DataRefImpl RelNext = Rel;
2694         info->O->moveRelocationNext(RelNext);
2695         MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2696         unsigned TypeNext = info->O->getAnyRelocationType(RENext);
2697         bool isExternNext = info->O->getPlainRelocationExternal(RENext);
2698         unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
2699         if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
2700           op_info->SubtractSymbol.Present = 1;
2701           op_info->SubtractSymbol.Name = name;
2702           symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
2703           Symbol = *RelocSymNext;
2704           name = unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
2705         }
2706       }
2707       // TODO: add the VariantKinds to op_info->VariantKind for relocation types
2708       // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
2709       op_info->AddSymbol.Present = 1;
2710       op_info->AddSymbol.Name = name;
2711       return 1;
2712     }
2713     return 0;
2714   }
2715   if (Arch == Triple::arm) {
2716     if (Offset != 0 || (Size != 4 && Size != 2))
2717       return 0;
2718     if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2719       // TODO:
2720       // Search the external relocation entries of a fully linked image
2721       // (if any) for an entry that matches this segment offset.
2722       // uint32_t seg_offset = (Pc + Offset);
2723       return 0;
2724     }
2725     // In MH_OBJECT filetypes search the section's relocation entries (if any)
2726     // for an entry for this section offset.
2727     uint32_t sect_addr = info->S.getAddress();
2728     uint32_t sect_offset = (Pc + Offset) - sect_addr;
2729     DataRefImpl Rel;
2730     MachO::any_relocation_info RE;
2731     bool isExtern = false;
2732     SymbolRef Symbol;
2733     bool r_scattered = false;
2734     uint32_t r_value, pair_r_value, r_type, r_length, other_half;
2735     auto Reloc =
2736         find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
2737           uint64_t RelocOffset = Reloc.getOffset();
2738           return RelocOffset == sect_offset;
2739         });
2740 
2741     if (Reloc == info->S.relocations().end())
2742       return 0;
2743 
2744     Rel = Reloc->getRawDataRefImpl();
2745     RE = info->O->getRelocation(Rel);
2746     r_length = info->O->getAnyRelocationLength(RE);
2747     r_scattered = info->O->isRelocationScattered(RE);
2748     if (r_scattered) {
2749       r_value = info->O->getScatteredRelocationValue(RE);
2750       r_type = info->O->getScatteredRelocationType(RE);
2751     } else {
2752       r_type = info->O->getAnyRelocationType(RE);
2753       isExtern = info->O->getPlainRelocationExternal(RE);
2754       if (isExtern) {
2755         symbol_iterator RelocSym = Reloc->getSymbol();
2756         Symbol = *RelocSym;
2757       }
2758     }
2759     if (r_type == MachO::ARM_RELOC_HALF ||
2760         r_type == MachO::ARM_RELOC_SECTDIFF ||
2761         r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
2762         r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2763       DataRefImpl RelNext = Rel;
2764       info->O->moveRelocationNext(RelNext);
2765       MachO::any_relocation_info RENext;
2766       RENext = info->O->getRelocation(RelNext);
2767       other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
2768       if (info->O->isRelocationScattered(RENext))
2769         pair_r_value = info->O->getScatteredRelocationValue(RENext);
2770     }
2771 
2772     if (isExtern) {
2773       const char *name =
2774           unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
2775       op_info->AddSymbol.Present = 1;
2776       op_info->AddSymbol.Name = name;
2777       switch (r_type) {
2778       case MachO::ARM_RELOC_HALF:
2779         if ((r_length & 0x1) == 1) {
2780           op_info->Value = value << 16 | other_half;
2781           op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2782         } else {
2783           op_info->Value = other_half << 16 | value;
2784           op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2785         }
2786         break;
2787       default:
2788         break;
2789       }
2790       return 1;
2791     }
2792     // If we have a branch that is not an external relocation entry then
2793     // return 0 so the code in tryAddingSymbolicOperand() can use the
2794     // SymbolLookUp call back with the branch target address to look up the
2795     // symbol and possibility add an annotation for a symbol stub.
2796     if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
2797                           r_type == MachO::ARM_THUMB_RELOC_BR22))
2798       return 0;
2799 
2800     uint32_t offset = 0;
2801     if (r_type == MachO::ARM_RELOC_HALF ||
2802         r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2803       if ((r_length & 0x1) == 1)
2804         value = value << 16 | other_half;
2805       else
2806         value = other_half << 16 | value;
2807     }
2808     if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
2809                         r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
2810       offset = value - r_value;
2811       value = r_value;
2812     }
2813 
2814     if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2815       if ((r_length & 0x1) == 1)
2816         op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2817       else
2818         op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2819       const char *add = GuessSymbolName(r_value, info->AddrMap);
2820       const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
2821       int32_t offset = value - (r_value - pair_r_value);
2822       op_info->AddSymbol.Present = 1;
2823       if (add != nullptr)
2824         op_info->AddSymbol.Name = add;
2825       else
2826         op_info->AddSymbol.Value = r_value;
2827       op_info->SubtractSymbol.Present = 1;
2828       if (sub != nullptr)
2829         op_info->SubtractSymbol.Name = sub;
2830       else
2831         op_info->SubtractSymbol.Value = pair_r_value;
2832       op_info->Value = offset;
2833       return 1;
2834     }
2835 
2836     op_info->AddSymbol.Present = 1;
2837     op_info->Value = offset;
2838     if (r_type == MachO::ARM_RELOC_HALF) {
2839       if ((r_length & 0x1) == 1)
2840         op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2841       else
2842         op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
2843     }
2844     const char *add = GuessSymbolName(value, info->AddrMap);
2845     if (add != nullptr) {
2846       op_info->AddSymbol.Name = add;
2847       return 1;
2848     }
2849     op_info->AddSymbol.Value = value;
2850     return 1;
2851   }
2852   if (Arch == Triple::aarch64) {
2853     if (Offset != 0 || Size != 4)
2854       return 0;
2855     if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2856       // TODO:
2857       // Search the external relocation entries of a fully linked image
2858       // (if any) for an entry that matches this segment offset.
2859       // uint64_t seg_offset = (Pc + Offset);
2860       return 0;
2861     }
2862     // In MH_OBJECT filetypes search the section's relocation entries (if any)
2863     // for an entry for this section offset.
2864     uint64_t sect_addr = info->S.getAddress();
2865     uint64_t sect_offset = (Pc + Offset) - sect_addr;
2866     auto Reloc =
2867         find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
2868           uint64_t RelocOffset = Reloc.getOffset();
2869           return RelocOffset == sect_offset;
2870         });
2871 
2872     if (Reloc == info->S.relocations().end())
2873       return 0;
2874 
2875     DataRefImpl Rel = Reloc->getRawDataRefImpl();
2876     MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2877     uint32_t r_type = info->O->getAnyRelocationType(RE);
2878     if (r_type == MachO::ARM64_RELOC_ADDEND) {
2879       DataRefImpl RelNext = Rel;
2880       info->O->moveRelocationNext(RelNext);
2881       MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2882       if (value == 0) {
2883         value = info->O->getPlainRelocationSymbolNum(RENext);
2884         op_info->Value = value;
2885       }
2886     }
2887     // NOTE: Scattered relocations don't exist on arm64.
2888     if (!info->O->getPlainRelocationExternal(RE))
2889       return 0;
2890     const char *name =
2891         unwrapOrError(Reloc->getSymbol()->getName(), info->O->getFileName())
2892             .data();
2893     op_info->AddSymbol.Present = 1;
2894     op_info->AddSymbol.Name = name;
2895 
2896     switch (r_type) {
2897     case MachO::ARM64_RELOC_PAGE21:
2898       /* @page */
2899       op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2900       break;
2901     case MachO::ARM64_RELOC_PAGEOFF12:
2902       /* @pageoff */
2903       op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2904       break;
2905     case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2906       /* @gotpage */
2907       op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2908       break;
2909     case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2910       /* @gotpageoff */
2911       op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2912       break;
2913     case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2914       /* @tvlppage is not implemented in llvm-mc */
2915       op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2916       break;
2917     case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2918       /* @tvlppageoff is not implemented in llvm-mc */
2919       op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2920       break;
2921     default:
2922     case MachO::ARM64_RELOC_BRANCH26:
2923       op_info->VariantKind = LLVMDisassembler_VariantKind_None;
2924       break;
2925     }
2926     return 1;
2927   }
2928   return 0;
2929 }
2930 
2931 // GuessCstringPointer is passed the address of what might be a pointer to a
2932 // literal string in a cstring section.  If that address is in a cstring section
2933 // it returns a pointer to that string.  Else it returns nullptr.
2934 static const char *GuessCstringPointer(uint64_t ReferenceValue,
2935                                        struct DisassembleInfo *info) {
2936   for (const auto &Load : info->O->load_commands()) {
2937     if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2938       MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2939       for (unsigned J = 0; J < Seg.nsects; ++J) {
2940         MachO::section_64 Sec = info->O->getSection64(Load, J);
2941         uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2942         if (section_type == MachO::S_CSTRING_LITERALS &&
2943             ReferenceValue >= Sec.addr &&
2944             ReferenceValue < Sec.addr + Sec.size) {
2945           uint64_t sect_offset = ReferenceValue - Sec.addr;
2946           uint64_t object_offset = Sec.offset + sect_offset;
2947           StringRef MachOContents = info->O->getData();
2948           uint64_t object_size = MachOContents.size();
2949           const char *object_addr = (const char *)MachOContents.data();
2950           if (object_offset < object_size) {
2951             const char *name = object_addr + object_offset;
2952             return name;
2953           } else {
2954             return nullptr;
2955           }
2956         }
2957       }
2958     } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2959       MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2960       for (unsigned J = 0; J < Seg.nsects; ++J) {
2961         MachO::section Sec = info->O->getSection(Load, J);
2962         uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2963         if (section_type == MachO::S_CSTRING_LITERALS &&
2964             ReferenceValue >= Sec.addr &&
2965             ReferenceValue < Sec.addr + Sec.size) {
2966           uint64_t sect_offset = ReferenceValue - Sec.addr;
2967           uint64_t object_offset = Sec.offset + sect_offset;
2968           StringRef MachOContents = info->O->getData();
2969           uint64_t object_size = MachOContents.size();
2970           const char *object_addr = (const char *)MachOContents.data();
2971           if (object_offset < object_size) {
2972             const char *name = object_addr + object_offset;
2973             return name;
2974           } else {
2975             return nullptr;
2976           }
2977         }
2978       }
2979     }
2980   }
2981   return nullptr;
2982 }
2983 
2984 // GuessIndirectSymbol returns the name of the indirect symbol for the
2985 // ReferenceValue passed in or nullptr.  This is used when ReferenceValue maybe
2986 // an address of a symbol stub or a lazy or non-lazy pointer to associate the
2987 // symbol name being referenced by the stub or pointer.
2988 static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2989                                        struct DisassembleInfo *info) {
2990   MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2991   MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
2992   for (const auto &Load : info->O->load_commands()) {
2993     if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2994       MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2995       for (unsigned J = 0; J < Seg.nsects; ++J) {
2996         MachO::section_64 Sec = info->O->getSection64(Load, J);
2997         uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2998         if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2999              section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
3000              section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
3001              section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
3002              section_type == MachO::S_SYMBOL_STUBS) &&
3003             ReferenceValue >= Sec.addr &&
3004             ReferenceValue < Sec.addr + Sec.size) {
3005           uint32_t stride;
3006           if (section_type == MachO::S_SYMBOL_STUBS)
3007             stride = Sec.reserved2;
3008           else
3009             stride = 8;
3010           if (stride == 0)
3011             return nullptr;
3012           uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
3013           if (index < Dysymtab.nindirectsyms) {
3014             uint32_t indirect_symbol =
3015                 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
3016             if (indirect_symbol < Symtab.nsyms) {
3017               symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
3018               return unwrapOrError(Sym->getName(), info->O->getFileName())
3019                   .data();
3020             }
3021           }
3022         }
3023       }
3024     } else if (Load.C.cmd == MachO::LC_SEGMENT) {
3025       MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
3026       for (unsigned J = 0; J < Seg.nsects; ++J) {
3027         MachO::section Sec = info->O->getSection(Load, J);
3028         uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
3029         if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
3030              section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
3031              section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
3032              section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
3033              section_type == MachO::S_SYMBOL_STUBS) &&
3034             ReferenceValue >= Sec.addr &&
3035             ReferenceValue < Sec.addr + Sec.size) {
3036           uint32_t stride;
3037           if (section_type == MachO::S_SYMBOL_STUBS)
3038             stride = Sec.reserved2;
3039           else
3040             stride = 4;
3041           if (stride == 0)
3042             return nullptr;
3043           uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
3044           if (index < Dysymtab.nindirectsyms) {
3045             uint32_t indirect_symbol =
3046                 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
3047             if (indirect_symbol < Symtab.nsyms) {
3048               symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
3049               return unwrapOrError(Sym->getName(), info->O->getFileName())
3050                   .data();
3051             }
3052           }
3053         }
3054       }
3055     }
3056   }
3057   return nullptr;
3058 }
3059 
3060 // method_reference() is called passing it the ReferenceName that might be
3061 // a reference it to an Objective-C method call.  If so then it allocates and
3062 // assembles a method call string with the values last seen and saved in
3063 // the DisassembleInfo's class_name and selector_name fields.  This is saved
3064 // into the method field of the info and any previous string is free'ed.
3065 // Then the class_name field in the info is set to nullptr.  The method call
3066 // string is set into ReferenceName and ReferenceType is set to
3067 // LLVMDisassembler_ReferenceType_Out_Objc_Message.  If this not a method call
3068 // then both ReferenceType and ReferenceName are left unchanged.
3069 static void method_reference(struct DisassembleInfo *info,
3070                              uint64_t *ReferenceType,
3071                              const char **ReferenceName) {
3072   unsigned int Arch = info->O->getArch();
3073   if (*ReferenceName != nullptr) {
3074     if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
3075       if (info->selector_name != nullptr) {
3076         if (info->class_name != nullptr) {
3077           info->method = llvm::make_unique<char[]>(
3078               5 + strlen(info->class_name) + strlen(info->selector_name));
3079           char *method = info->method.get();
3080           if (method != nullptr) {
3081             strcpy(method, "+[");
3082             strcat(method, info->class_name);
3083             strcat(method, " ");
3084             strcat(method, info->selector_name);
3085             strcat(method, "]");
3086             *ReferenceName = method;
3087             *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
3088           }
3089         } else {
3090           info->method =
3091               llvm::make_unique<char[]>(9 + strlen(info->selector_name));
3092           char *method = info->method.get();
3093           if (method != nullptr) {
3094             if (Arch == Triple::x86_64)
3095               strcpy(method, "-[%rdi ");
3096             else if (Arch == Triple::aarch64)
3097               strcpy(method, "-[x0 ");
3098             else
3099               strcpy(method, "-[r? ");
3100             strcat(method, info->selector_name);
3101             strcat(method, "]");
3102             *ReferenceName = method;
3103             *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
3104           }
3105         }
3106         info->class_name = nullptr;
3107       }
3108     } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
3109       if (info->selector_name != nullptr) {
3110         info->method =
3111             llvm::make_unique<char[]>(17 + strlen(info->selector_name));
3112         char *method = info->method.get();
3113         if (method != nullptr) {
3114           if (Arch == Triple::x86_64)
3115             strcpy(method, "-[[%rdi super] ");
3116           else if (Arch == Triple::aarch64)
3117             strcpy(method, "-[[x0 super] ");
3118           else
3119             strcpy(method, "-[[r? super] ");
3120           strcat(method, info->selector_name);
3121           strcat(method, "]");
3122           *ReferenceName = method;
3123           *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
3124         }
3125         info->class_name = nullptr;
3126       }
3127     }
3128   }
3129 }
3130 
3131 // GuessPointerPointer() is passed the address of what might be a pointer to
3132 // a reference to an Objective-C class, selector, message ref or cfstring.
3133 // If so the value of the pointer is returned and one of the booleans are set
3134 // to true.  If not zero is returned and all the booleans are set to false.
3135 static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
3136                                     struct DisassembleInfo *info,
3137                                     bool &classref, bool &selref, bool &msgref,
3138                                     bool &cfstring) {
3139   classref = false;
3140   selref = false;
3141   msgref = false;
3142   cfstring = false;
3143   for (const auto &Load : info->O->load_commands()) {
3144     if (Load.C.cmd == MachO::LC_SEGMENT_64) {
3145       MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
3146       for (unsigned J = 0; J < Seg.nsects; ++J) {
3147         MachO::section_64 Sec = info->O->getSection64(Load, J);
3148         if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
3149              strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
3150              strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
3151              strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
3152              strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
3153             ReferenceValue >= Sec.addr &&
3154             ReferenceValue < Sec.addr + Sec.size) {
3155           uint64_t sect_offset = ReferenceValue - Sec.addr;
3156           uint64_t object_offset = Sec.offset + sect_offset;
3157           StringRef MachOContents = info->O->getData();
3158           uint64_t object_size = MachOContents.size();
3159           const char *object_addr = (const char *)MachOContents.data();
3160           if (object_offset < object_size) {
3161             uint64_t pointer_value;
3162             memcpy(&pointer_value, object_addr + object_offset,
3163                    sizeof(uint64_t));
3164             if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3165               sys::swapByteOrder(pointer_value);
3166             if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
3167               selref = true;
3168             else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
3169                      strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
3170               classref = true;
3171             else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
3172                      ReferenceValue + 8 < Sec.addr + Sec.size) {
3173               msgref = true;
3174               memcpy(&pointer_value, object_addr + object_offset + 8,
3175                      sizeof(uint64_t));
3176               if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3177                 sys::swapByteOrder(pointer_value);
3178             } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
3179               cfstring = true;
3180             return pointer_value;
3181           } else {
3182             return 0;
3183           }
3184         }
3185       }
3186     }
3187     // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
3188   }
3189   return 0;
3190 }
3191 
3192 // get_pointer_64 returns a pointer to the bytes in the object file at the
3193 // Address from a section in the Mach-O file.  And indirectly returns the
3194 // offset into the section, number of bytes left in the section past the offset
3195 // and which section is was being referenced.  If the Address is not in a
3196 // section nullptr is returned.
3197 static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
3198                                   uint32_t &left, SectionRef &S,
3199                                   DisassembleInfo *info,
3200                                   bool objc_only = false) {
3201   offset = 0;
3202   left = 0;
3203   S = SectionRef();
3204   for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
3205     uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
3206     uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
3207     if (SectSize == 0)
3208       continue;
3209     if (objc_only) {
3210       StringRef SectName;
3211       ((*(info->Sections))[SectIdx]).getName(SectName);
3212       DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
3213       StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
3214       if (SegName != "__OBJC" && SectName != "__cstring")
3215         continue;
3216     }
3217     if (Address >= SectAddress && Address < SectAddress + SectSize) {
3218       S = (*(info->Sections))[SectIdx];
3219       offset = Address - SectAddress;
3220       left = SectSize - offset;
3221       StringRef SectContents = unwrapOrError(
3222           ((*(info->Sections))[SectIdx]).getContents(), info->O->getFileName());
3223       return SectContents.data() + offset;
3224     }
3225   }
3226   return nullptr;
3227 }
3228 
3229 static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
3230                                   uint32_t &left, SectionRef &S,
3231                                   DisassembleInfo *info,
3232                                   bool objc_only = false) {
3233   return get_pointer_64(Address, offset, left, S, info, objc_only);
3234 }
3235 
3236 // get_symbol_64() returns the name of a symbol (or nullptr) and the address of
3237 // the symbol indirectly through n_value. Based on the relocation information
3238 // for the specified section offset in the specified section reference.
3239 // If no relocation information is found and a non-zero ReferenceValue for the
3240 // symbol is passed, look up that address in the info's AddrMap.
3241 static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
3242                                  DisassembleInfo *info, uint64_t &n_value,
3243                                  uint64_t ReferenceValue = 0) {
3244   n_value = 0;
3245   if (!info->verbose)
3246     return nullptr;
3247 
3248   // See if there is an external relocation entry at the sect_offset.
3249   bool reloc_found = false;
3250   DataRefImpl Rel;
3251   MachO::any_relocation_info RE;
3252   bool isExtern = false;
3253   SymbolRef Symbol;
3254   for (const RelocationRef &Reloc : S.relocations()) {
3255     uint64_t RelocOffset = Reloc.getOffset();
3256     if (RelocOffset == sect_offset) {
3257       Rel = Reloc.getRawDataRefImpl();
3258       RE = info->O->getRelocation(Rel);
3259       if (info->O->isRelocationScattered(RE))
3260         continue;
3261       isExtern = info->O->getPlainRelocationExternal(RE);
3262       if (isExtern) {
3263         symbol_iterator RelocSym = Reloc.getSymbol();
3264         Symbol = *RelocSym;
3265       }
3266       reloc_found = true;
3267       break;
3268     }
3269   }
3270   // If there is an external relocation entry for a symbol in this section
3271   // at this section_offset then use that symbol's value for the n_value
3272   // and return its name.
3273   const char *SymbolName = nullptr;
3274   if (reloc_found && isExtern) {
3275     n_value = Symbol.getValue();
3276     StringRef Name = unwrapOrError(Symbol.getName(), info->O->getFileName());
3277     if (!Name.empty()) {
3278       SymbolName = Name.data();
3279       return SymbolName;
3280     }
3281   }
3282 
3283   // TODO: For fully linked images, look through the external relocation
3284   // entries off the dynamic symtab command. For these the r_offset is from the
3285   // start of the first writeable segment in the Mach-O file.  So the offset
3286   // to this section from that segment is passed to this routine by the caller,
3287   // as the database_offset. Which is the difference of the section's starting
3288   // address and the first writable segment.
3289   //
3290   // NOTE: need add passing the database_offset to this routine.
3291 
3292   // We did not find an external relocation entry so look up the ReferenceValue
3293   // as an address of a symbol and if found return that symbol's name.
3294   SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
3295 
3296   return SymbolName;
3297 }
3298 
3299 static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
3300                                  DisassembleInfo *info,
3301                                  uint32_t ReferenceValue) {
3302   uint64_t n_value64;
3303   return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
3304 }
3305 
3306 // These are structs in the Objective-C meta data and read to produce the
3307 // comments for disassembly.  While these are part of the ABI they are no
3308 // public defintions.  So the are here not in include/llvm/BinaryFormat/MachO.h
3309 // .
3310 
3311 // The cfstring object in a 64-bit Mach-O file.
3312 struct cfstring64_t {
3313   uint64_t isa;        // class64_t * (64-bit pointer)
3314   uint64_t flags;      // flag bits
3315   uint64_t characters; // char * (64-bit pointer)
3316   uint64_t length;     // number of non-NULL characters in above
3317 };
3318 
3319 // The class object in a 64-bit Mach-O file.
3320 struct class64_t {
3321   uint64_t isa;        // class64_t * (64-bit pointer)
3322   uint64_t superclass; // class64_t * (64-bit pointer)
3323   uint64_t cache;      // Cache (64-bit pointer)
3324   uint64_t vtable;     // IMP * (64-bit pointer)
3325   uint64_t data;       // class_ro64_t * (64-bit pointer)
3326 };
3327 
3328 struct class32_t {
3329   uint32_t isa;        /* class32_t * (32-bit pointer) */
3330   uint32_t superclass; /* class32_t * (32-bit pointer) */
3331   uint32_t cache;      /* Cache (32-bit pointer) */
3332   uint32_t vtable;     /* IMP * (32-bit pointer) */
3333   uint32_t data;       /* class_ro32_t * (32-bit pointer) */
3334 };
3335 
3336 struct class_ro64_t {
3337   uint32_t flags;
3338   uint32_t instanceStart;
3339   uint32_t instanceSize;
3340   uint32_t reserved;
3341   uint64_t ivarLayout;     // const uint8_t * (64-bit pointer)
3342   uint64_t name;           // const char * (64-bit pointer)
3343   uint64_t baseMethods;    // const method_list_t * (64-bit pointer)
3344   uint64_t baseProtocols;  // const protocol_list_t * (64-bit pointer)
3345   uint64_t ivars;          // const ivar_list_t * (64-bit pointer)
3346   uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
3347   uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
3348 };
3349 
3350 struct class_ro32_t {
3351   uint32_t flags;
3352   uint32_t instanceStart;
3353   uint32_t instanceSize;
3354   uint32_t ivarLayout;     /* const uint8_t * (32-bit pointer) */
3355   uint32_t name;           /* const char * (32-bit pointer) */
3356   uint32_t baseMethods;    /* const method_list_t * (32-bit pointer) */
3357   uint32_t baseProtocols;  /* const protocol_list_t * (32-bit pointer) */
3358   uint32_t ivars;          /* const ivar_list_t * (32-bit pointer) */
3359   uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
3360   uint32_t baseProperties; /* const struct objc_property_list *
3361                                                    (32-bit pointer) */
3362 };
3363 
3364 /* Values for class_ro{64,32}_t->flags */
3365 #define RO_META (1 << 0)
3366 #define RO_ROOT (1 << 1)
3367 #define RO_HAS_CXX_STRUCTORS (1 << 2)
3368 
3369 struct method_list64_t {
3370   uint32_t entsize;
3371   uint32_t count;
3372   /* struct method64_t first;  These structures follow inline */
3373 };
3374 
3375 struct method_list32_t {
3376   uint32_t entsize;
3377   uint32_t count;
3378   /* struct method32_t first;  These structures follow inline */
3379 };
3380 
3381 struct method64_t {
3382   uint64_t name;  /* SEL (64-bit pointer) */
3383   uint64_t types; /* const char * (64-bit pointer) */
3384   uint64_t imp;   /* IMP (64-bit pointer) */
3385 };
3386 
3387 struct method32_t {
3388   uint32_t name;  /* SEL (32-bit pointer) */
3389   uint32_t types; /* const char * (32-bit pointer) */
3390   uint32_t imp;   /* IMP (32-bit pointer) */
3391 };
3392 
3393 struct protocol_list64_t {
3394   uint64_t count; /* uintptr_t (a 64-bit value) */
3395   /* struct protocol64_t * list[0];  These pointers follow inline */
3396 };
3397 
3398 struct protocol_list32_t {
3399   uint32_t count; /* uintptr_t (a 32-bit value) */
3400   /* struct protocol32_t * list[0];  These pointers follow inline */
3401 };
3402 
3403 struct protocol64_t {
3404   uint64_t isa;                     /* id * (64-bit pointer) */
3405   uint64_t name;                    /* const char * (64-bit pointer) */
3406   uint64_t protocols;               /* struct protocol_list64_t *
3407                                                     (64-bit pointer) */
3408   uint64_t instanceMethods;         /* method_list_t * (64-bit pointer) */
3409   uint64_t classMethods;            /* method_list_t * (64-bit pointer) */
3410   uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
3411   uint64_t optionalClassMethods;    /* method_list_t * (64-bit pointer) */
3412   uint64_t instanceProperties;      /* struct objc_property_list *
3413                                                        (64-bit pointer) */
3414 };
3415 
3416 struct protocol32_t {
3417   uint32_t isa;                     /* id * (32-bit pointer) */
3418   uint32_t name;                    /* const char * (32-bit pointer) */
3419   uint32_t protocols;               /* struct protocol_list_t *
3420                                                     (32-bit pointer) */
3421   uint32_t instanceMethods;         /* method_list_t * (32-bit pointer) */
3422   uint32_t classMethods;            /* method_list_t * (32-bit pointer) */
3423   uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
3424   uint32_t optionalClassMethods;    /* method_list_t * (32-bit pointer) */
3425   uint32_t instanceProperties;      /* struct objc_property_list *
3426                                                        (32-bit pointer) */
3427 };
3428 
3429 struct ivar_list64_t {
3430   uint32_t entsize;
3431   uint32_t count;
3432   /* struct ivar64_t first;  These structures follow inline */
3433 };
3434 
3435 struct ivar_list32_t {
3436   uint32_t entsize;
3437   uint32_t count;
3438   /* struct ivar32_t first;  These structures follow inline */
3439 };
3440 
3441 struct ivar64_t {
3442   uint64_t offset; /* uintptr_t * (64-bit pointer) */
3443   uint64_t name;   /* const char * (64-bit pointer) */
3444   uint64_t type;   /* const char * (64-bit pointer) */
3445   uint32_t alignment;
3446   uint32_t size;
3447 };
3448 
3449 struct ivar32_t {
3450   uint32_t offset; /* uintptr_t * (32-bit pointer) */
3451   uint32_t name;   /* const char * (32-bit pointer) */
3452   uint32_t type;   /* const char * (32-bit pointer) */
3453   uint32_t alignment;
3454   uint32_t size;
3455 };
3456 
3457 struct objc_property_list64 {
3458   uint32_t entsize;
3459   uint32_t count;
3460   /* struct objc_property64 first;  These structures follow inline */
3461 };
3462 
3463 struct objc_property_list32 {
3464   uint32_t entsize;
3465   uint32_t count;
3466   /* struct objc_property32 first;  These structures follow inline */
3467 };
3468 
3469 struct objc_property64 {
3470   uint64_t name;       /* const char * (64-bit pointer) */
3471   uint64_t attributes; /* const char * (64-bit pointer) */
3472 };
3473 
3474 struct objc_property32 {
3475   uint32_t name;       /* const char * (32-bit pointer) */
3476   uint32_t attributes; /* const char * (32-bit pointer) */
3477 };
3478 
3479 struct category64_t {
3480   uint64_t name;               /* const char * (64-bit pointer) */
3481   uint64_t cls;                /* struct class_t * (64-bit pointer) */
3482   uint64_t instanceMethods;    /* struct method_list_t * (64-bit pointer) */
3483   uint64_t classMethods;       /* struct method_list_t * (64-bit pointer) */
3484   uint64_t protocols;          /* struct protocol_list_t * (64-bit pointer) */
3485   uint64_t instanceProperties; /* struct objc_property_list *
3486                                   (64-bit pointer) */
3487 };
3488 
3489 struct category32_t {
3490   uint32_t name;               /* const char * (32-bit pointer) */
3491   uint32_t cls;                /* struct class_t * (32-bit pointer) */
3492   uint32_t instanceMethods;    /* struct method_list_t * (32-bit pointer) */
3493   uint32_t classMethods;       /* struct method_list_t * (32-bit pointer) */
3494   uint32_t protocols;          /* struct protocol_list_t * (32-bit pointer) */
3495   uint32_t instanceProperties; /* struct objc_property_list *
3496                                   (32-bit pointer) */
3497 };
3498 
3499 struct objc_image_info64 {
3500   uint32_t version;
3501   uint32_t flags;
3502 };
3503 struct objc_image_info32 {
3504   uint32_t version;
3505   uint32_t flags;
3506 };
3507 struct imageInfo_t {
3508   uint32_t version;
3509   uint32_t flags;
3510 };
3511 /* masks for objc_image_info.flags */
3512 #define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
3513 #define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
3514 #define OBJC_IMAGE_IS_SIMULATED (1 << 5)
3515 #define OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES (1 << 6)
3516 
3517 struct message_ref64 {
3518   uint64_t imp; /* IMP (64-bit pointer) */
3519   uint64_t sel; /* SEL (64-bit pointer) */
3520 };
3521 
3522 struct message_ref32 {
3523   uint32_t imp; /* IMP (32-bit pointer) */
3524   uint32_t sel; /* SEL (32-bit pointer) */
3525 };
3526 
3527 // Objective-C 1 (32-bit only) meta data structs.
3528 
3529 struct objc_module_t {
3530   uint32_t version;
3531   uint32_t size;
3532   uint32_t name;   /* char * (32-bit pointer) */
3533   uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
3534 };
3535 
3536 struct objc_symtab_t {
3537   uint32_t sel_ref_cnt;
3538   uint32_t refs; /* SEL * (32-bit pointer) */
3539   uint16_t cls_def_cnt;
3540   uint16_t cat_def_cnt;
3541   // uint32_t defs[1];        /* void * (32-bit pointer) variable size */
3542 };
3543 
3544 struct objc_class_t {
3545   uint32_t isa;         /* struct objc_class * (32-bit pointer) */
3546   uint32_t super_class; /* struct objc_class * (32-bit pointer) */
3547   uint32_t name;        /* const char * (32-bit pointer) */
3548   int32_t version;
3549   int32_t info;
3550   int32_t instance_size;
3551   uint32_t ivars;       /* struct objc_ivar_list * (32-bit pointer) */
3552   uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
3553   uint32_t cache;       /* struct objc_cache * (32-bit pointer) */
3554   uint32_t protocols;   /* struct objc_protocol_list * (32-bit pointer) */
3555 };
3556 
3557 #define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
3558 // class is not a metaclass
3559 #define CLS_CLASS 0x1
3560 // class is a metaclass
3561 #define CLS_META 0x2
3562 
3563 struct objc_category_t {
3564   uint32_t category_name;    /* char * (32-bit pointer) */
3565   uint32_t class_name;       /* char * (32-bit pointer) */
3566   uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
3567   uint32_t class_methods;    /* struct objc_method_list * (32-bit pointer) */
3568   uint32_t protocols;        /* struct objc_protocol_list * (32-bit ptr) */
3569 };
3570 
3571 struct objc_ivar_t {
3572   uint32_t ivar_name; /* char * (32-bit pointer) */
3573   uint32_t ivar_type; /* char * (32-bit pointer) */
3574   int32_t ivar_offset;
3575 };
3576 
3577 struct objc_ivar_list_t {
3578   int32_t ivar_count;
3579   // struct objc_ivar_t ivar_list[1];          /* variable length structure */
3580 };
3581 
3582 struct objc_method_list_t {
3583   uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
3584   int32_t method_count;
3585   // struct objc_method_t method_list[1];      /* variable length structure */
3586 };
3587 
3588 struct objc_method_t {
3589   uint32_t method_name;  /* SEL, aka struct objc_selector * (32-bit pointer) */
3590   uint32_t method_types; /* char * (32-bit pointer) */
3591   uint32_t method_imp;   /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
3592                             (32-bit pointer) */
3593 };
3594 
3595 struct objc_protocol_list_t {
3596   uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
3597   int32_t count;
3598   // uint32_t list[1];   /* Protocol *, aka struct objc_protocol_t *
3599   //                        (32-bit pointer) */
3600 };
3601 
3602 struct objc_protocol_t {
3603   uint32_t isa;              /* struct objc_class * (32-bit pointer) */
3604   uint32_t protocol_name;    /* char * (32-bit pointer) */
3605   uint32_t protocol_list;    /* struct objc_protocol_list * (32-bit pointer) */
3606   uint32_t instance_methods; /* struct objc_method_description_list *
3607                                 (32-bit pointer) */
3608   uint32_t class_methods;    /* struct objc_method_description_list *
3609                                 (32-bit pointer) */
3610 };
3611 
3612 struct objc_method_description_list_t {
3613   int32_t count;
3614   // struct objc_method_description_t list[1];
3615 };
3616 
3617 struct objc_method_description_t {
3618   uint32_t name;  /* SEL, aka struct objc_selector * (32-bit pointer) */
3619   uint32_t types; /* char * (32-bit pointer) */
3620 };
3621 
3622 inline void swapStruct(struct cfstring64_t &cfs) {
3623   sys::swapByteOrder(cfs.isa);
3624   sys::swapByteOrder(cfs.flags);
3625   sys::swapByteOrder(cfs.characters);
3626   sys::swapByteOrder(cfs.length);
3627 }
3628 
3629 inline void swapStruct(struct class64_t &c) {
3630   sys::swapByteOrder(c.isa);
3631   sys::swapByteOrder(c.superclass);
3632   sys::swapByteOrder(c.cache);
3633   sys::swapByteOrder(c.vtable);
3634   sys::swapByteOrder(c.data);
3635 }
3636 
3637 inline void swapStruct(struct class32_t &c) {
3638   sys::swapByteOrder(c.isa);
3639   sys::swapByteOrder(c.superclass);
3640   sys::swapByteOrder(c.cache);
3641   sys::swapByteOrder(c.vtable);
3642   sys::swapByteOrder(c.data);
3643 }
3644 
3645 inline void swapStruct(struct class_ro64_t &cro) {
3646   sys::swapByteOrder(cro.flags);
3647   sys::swapByteOrder(cro.instanceStart);
3648   sys::swapByteOrder(cro.instanceSize);
3649   sys::swapByteOrder(cro.reserved);
3650   sys::swapByteOrder(cro.ivarLayout);
3651   sys::swapByteOrder(cro.name);
3652   sys::swapByteOrder(cro.baseMethods);
3653   sys::swapByteOrder(cro.baseProtocols);
3654   sys::swapByteOrder(cro.ivars);
3655   sys::swapByteOrder(cro.weakIvarLayout);
3656   sys::swapByteOrder(cro.baseProperties);
3657 }
3658 
3659 inline void swapStruct(struct class_ro32_t &cro) {
3660   sys::swapByteOrder(cro.flags);
3661   sys::swapByteOrder(cro.instanceStart);
3662   sys::swapByteOrder(cro.instanceSize);
3663   sys::swapByteOrder(cro.ivarLayout);
3664   sys::swapByteOrder(cro.name);
3665   sys::swapByteOrder(cro.baseMethods);
3666   sys::swapByteOrder(cro.baseProtocols);
3667   sys::swapByteOrder(cro.ivars);
3668   sys::swapByteOrder(cro.weakIvarLayout);
3669   sys::swapByteOrder(cro.baseProperties);
3670 }
3671 
3672 inline void swapStruct(struct method_list64_t &ml) {
3673   sys::swapByteOrder(ml.entsize);
3674   sys::swapByteOrder(ml.count);
3675 }
3676 
3677 inline void swapStruct(struct method_list32_t &ml) {
3678   sys::swapByteOrder(ml.entsize);
3679   sys::swapByteOrder(ml.count);
3680 }
3681 
3682 inline void swapStruct(struct method64_t &m) {
3683   sys::swapByteOrder(m.name);
3684   sys::swapByteOrder(m.types);
3685   sys::swapByteOrder(m.imp);
3686 }
3687 
3688 inline void swapStruct(struct method32_t &m) {
3689   sys::swapByteOrder(m.name);
3690   sys::swapByteOrder(m.types);
3691   sys::swapByteOrder(m.imp);
3692 }
3693 
3694 inline void swapStruct(struct protocol_list64_t &pl) {
3695   sys::swapByteOrder(pl.count);
3696 }
3697 
3698 inline void swapStruct(struct protocol_list32_t &pl) {
3699   sys::swapByteOrder(pl.count);
3700 }
3701 
3702 inline void swapStruct(struct protocol64_t &p) {
3703   sys::swapByteOrder(p.isa);
3704   sys::swapByteOrder(p.name);
3705   sys::swapByteOrder(p.protocols);
3706   sys::swapByteOrder(p.instanceMethods);
3707   sys::swapByteOrder(p.classMethods);
3708   sys::swapByteOrder(p.optionalInstanceMethods);
3709   sys::swapByteOrder(p.optionalClassMethods);
3710   sys::swapByteOrder(p.instanceProperties);
3711 }
3712 
3713 inline void swapStruct(struct protocol32_t &p) {
3714   sys::swapByteOrder(p.isa);
3715   sys::swapByteOrder(p.name);
3716   sys::swapByteOrder(p.protocols);
3717   sys::swapByteOrder(p.instanceMethods);
3718   sys::swapByteOrder(p.classMethods);
3719   sys::swapByteOrder(p.optionalInstanceMethods);
3720   sys::swapByteOrder(p.optionalClassMethods);
3721   sys::swapByteOrder(p.instanceProperties);
3722 }
3723 
3724 inline void swapStruct(struct ivar_list64_t &il) {
3725   sys::swapByteOrder(il.entsize);
3726   sys::swapByteOrder(il.count);
3727 }
3728 
3729 inline void swapStruct(struct ivar_list32_t &il) {
3730   sys::swapByteOrder(il.entsize);
3731   sys::swapByteOrder(il.count);
3732 }
3733 
3734 inline void swapStruct(struct ivar64_t &i) {
3735   sys::swapByteOrder(i.offset);
3736   sys::swapByteOrder(i.name);
3737   sys::swapByteOrder(i.type);
3738   sys::swapByteOrder(i.alignment);
3739   sys::swapByteOrder(i.size);
3740 }
3741 
3742 inline void swapStruct(struct ivar32_t &i) {
3743   sys::swapByteOrder(i.offset);
3744   sys::swapByteOrder(i.name);
3745   sys::swapByteOrder(i.type);
3746   sys::swapByteOrder(i.alignment);
3747   sys::swapByteOrder(i.size);
3748 }
3749 
3750 inline void swapStruct(struct objc_property_list64 &pl) {
3751   sys::swapByteOrder(pl.entsize);
3752   sys::swapByteOrder(pl.count);
3753 }
3754 
3755 inline void swapStruct(struct objc_property_list32 &pl) {
3756   sys::swapByteOrder(pl.entsize);
3757   sys::swapByteOrder(pl.count);
3758 }
3759 
3760 inline void swapStruct(struct objc_property64 &op) {
3761   sys::swapByteOrder(op.name);
3762   sys::swapByteOrder(op.attributes);
3763 }
3764 
3765 inline void swapStruct(struct objc_property32 &op) {
3766   sys::swapByteOrder(op.name);
3767   sys::swapByteOrder(op.attributes);
3768 }
3769 
3770 inline void swapStruct(struct category64_t &c) {
3771   sys::swapByteOrder(c.name);
3772   sys::swapByteOrder(c.cls);
3773   sys::swapByteOrder(c.instanceMethods);
3774   sys::swapByteOrder(c.classMethods);
3775   sys::swapByteOrder(c.protocols);
3776   sys::swapByteOrder(c.instanceProperties);
3777 }
3778 
3779 inline void swapStruct(struct category32_t &c) {
3780   sys::swapByteOrder(c.name);
3781   sys::swapByteOrder(c.cls);
3782   sys::swapByteOrder(c.instanceMethods);
3783   sys::swapByteOrder(c.classMethods);
3784   sys::swapByteOrder(c.protocols);
3785   sys::swapByteOrder(c.instanceProperties);
3786 }
3787 
3788 inline void swapStruct(struct objc_image_info64 &o) {
3789   sys::swapByteOrder(o.version);
3790   sys::swapByteOrder(o.flags);
3791 }
3792 
3793 inline void swapStruct(struct objc_image_info32 &o) {
3794   sys::swapByteOrder(o.version);
3795   sys::swapByteOrder(o.flags);
3796 }
3797 
3798 inline void swapStruct(struct imageInfo_t &o) {
3799   sys::swapByteOrder(o.version);
3800   sys::swapByteOrder(o.flags);
3801 }
3802 
3803 inline void swapStruct(struct message_ref64 &mr) {
3804   sys::swapByteOrder(mr.imp);
3805   sys::swapByteOrder(mr.sel);
3806 }
3807 
3808 inline void swapStruct(struct message_ref32 &mr) {
3809   sys::swapByteOrder(mr.imp);
3810   sys::swapByteOrder(mr.sel);
3811 }
3812 
3813 inline void swapStruct(struct objc_module_t &module) {
3814   sys::swapByteOrder(module.version);
3815   sys::swapByteOrder(module.size);
3816   sys::swapByteOrder(module.name);
3817   sys::swapByteOrder(module.symtab);
3818 }
3819 
3820 inline void swapStruct(struct objc_symtab_t &symtab) {
3821   sys::swapByteOrder(symtab.sel_ref_cnt);
3822   sys::swapByteOrder(symtab.refs);
3823   sys::swapByteOrder(symtab.cls_def_cnt);
3824   sys::swapByteOrder(symtab.cat_def_cnt);
3825 }
3826 
3827 inline void swapStruct(struct objc_class_t &objc_class) {
3828   sys::swapByteOrder(objc_class.isa);
3829   sys::swapByteOrder(objc_class.super_class);
3830   sys::swapByteOrder(objc_class.name);
3831   sys::swapByteOrder(objc_class.version);
3832   sys::swapByteOrder(objc_class.info);
3833   sys::swapByteOrder(objc_class.instance_size);
3834   sys::swapByteOrder(objc_class.ivars);
3835   sys::swapByteOrder(objc_class.methodLists);
3836   sys::swapByteOrder(objc_class.cache);
3837   sys::swapByteOrder(objc_class.protocols);
3838 }
3839 
3840 inline void swapStruct(struct objc_category_t &objc_category) {
3841   sys::swapByteOrder(objc_category.category_name);
3842   sys::swapByteOrder(objc_category.class_name);
3843   sys::swapByteOrder(objc_category.instance_methods);
3844   sys::swapByteOrder(objc_category.class_methods);
3845   sys::swapByteOrder(objc_category.protocols);
3846 }
3847 
3848 inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3849   sys::swapByteOrder(objc_ivar_list.ivar_count);
3850 }
3851 
3852 inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3853   sys::swapByteOrder(objc_ivar.ivar_name);
3854   sys::swapByteOrder(objc_ivar.ivar_type);
3855   sys::swapByteOrder(objc_ivar.ivar_offset);
3856 }
3857 
3858 inline void swapStruct(struct objc_method_list_t &method_list) {
3859   sys::swapByteOrder(method_list.obsolete);
3860   sys::swapByteOrder(method_list.method_count);
3861 }
3862 
3863 inline void swapStruct(struct objc_method_t &method) {
3864   sys::swapByteOrder(method.method_name);
3865   sys::swapByteOrder(method.method_types);
3866   sys::swapByteOrder(method.method_imp);
3867 }
3868 
3869 inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3870   sys::swapByteOrder(protocol_list.next);
3871   sys::swapByteOrder(protocol_list.count);
3872 }
3873 
3874 inline void swapStruct(struct objc_protocol_t &protocol) {
3875   sys::swapByteOrder(protocol.isa);
3876   sys::swapByteOrder(protocol.protocol_name);
3877   sys::swapByteOrder(protocol.protocol_list);
3878   sys::swapByteOrder(protocol.instance_methods);
3879   sys::swapByteOrder(protocol.class_methods);
3880 }
3881 
3882 inline void swapStruct(struct objc_method_description_list_t &mdl) {
3883   sys::swapByteOrder(mdl.count);
3884 }
3885 
3886 inline void swapStruct(struct objc_method_description_t &md) {
3887   sys::swapByteOrder(md.name);
3888   sys::swapByteOrder(md.types);
3889 }
3890 
3891 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3892                                                  struct DisassembleInfo *info);
3893 
3894 // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3895 // to an Objective-C class and returns the class name.  It is also passed the
3896 // address of the pointer, so when the pointer is zero as it can be in an .o
3897 // file, that is used to look for an external relocation entry with a symbol
3898 // name.
3899 static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3900                                               uint64_t ReferenceValue,
3901                                               struct DisassembleInfo *info) {
3902   const char *r;
3903   uint32_t offset, left;
3904   SectionRef S;
3905 
3906   // The pointer_value can be 0 in an object file and have a relocation
3907   // entry for the class symbol at the ReferenceValue (the address of the
3908   // pointer).
3909   if (pointer_value == 0) {
3910     r = get_pointer_64(ReferenceValue, offset, left, S, info);
3911     if (r == nullptr || left < sizeof(uint64_t))
3912       return nullptr;
3913     uint64_t n_value;
3914     const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3915     if (symbol_name == nullptr)
3916       return nullptr;
3917     const char *class_name = strrchr(symbol_name, '$');
3918     if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3919       return class_name + 2;
3920     else
3921       return nullptr;
3922   }
3923 
3924   // The case were the pointer_value is non-zero and points to a class defined
3925   // in this Mach-O file.
3926   r = get_pointer_64(pointer_value, offset, left, S, info);
3927   if (r == nullptr || left < sizeof(struct class64_t))
3928     return nullptr;
3929   struct class64_t c;
3930   memcpy(&c, r, sizeof(struct class64_t));
3931   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3932     swapStruct(c);
3933   if (c.data == 0)
3934     return nullptr;
3935   r = get_pointer_64(c.data, offset, left, S, info);
3936   if (r == nullptr || left < sizeof(struct class_ro64_t))
3937     return nullptr;
3938   struct class_ro64_t cro;
3939   memcpy(&cro, r, sizeof(struct class_ro64_t));
3940   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3941     swapStruct(cro);
3942   if (cro.name == 0)
3943     return nullptr;
3944   const char *name = get_pointer_64(cro.name, offset, left, S, info);
3945   return name;
3946 }
3947 
3948 // get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3949 // pointer to a cfstring and returns its name or nullptr.
3950 static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3951                                                  struct DisassembleInfo *info) {
3952   const char *r, *name;
3953   uint32_t offset, left;
3954   SectionRef S;
3955   struct cfstring64_t cfs;
3956   uint64_t cfs_characters;
3957 
3958   r = get_pointer_64(ReferenceValue, offset, left, S, info);
3959   if (r == nullptr || left < sizeof(struct cfstring64_t))
3960     return nullptr;
3961   memcpy(&cfs, r, sizeof(struct cfstring64_t));
3962   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3963     swapStruct(cfs);
3964   if (cfs.characters == 0) {
3965     uint64_t n_value;
3966     const char *symbol_name = get_symbol_64(
3967         offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
3968     if (symbol_name == nullptr)
3969       return nullptr;
3970     cfs_characters = n_value;
3971   } else
3972     cfs_characters = cfs.characters;
3973   name = get_pointer_64(cfs_characters, offset, left, S, info);
3974 
3975   return name;
3976 }
3977 
3978 // get_objc2_64bit_selref() is used for disassembly and is passed a the address
3979 // of a pointer to an Objective-C selector reference when the pointer value is
3980 // zero as in a .o file and is likely to have a external relocation entry with
3981 // who's symbol's n_value is the real pointer to the selector name.  If that is
3982 // the case the real pointer to the selector name is returned else 0 is
3983 // returned
3984 static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3985                                        struct DisassembleInfo *info) {
3986   uint32_t offset, left;
3987   SectionRef S;
3988 
3989   const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3990   if (r == nullptr || left < sizeof(uint64_t))
3991     return 0;
3992   uint64_t n_value;
3993   const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3994   if (symbol_name == nullptr)
3995     return 0;
3996   return n_value;
3997 }
3998 
3999 static const SectionRef get_section(MachOObjectFile *O, const char *segname,
4000                                     const char *sectname) {
4001   for (const SectionRef &Section : O->sections()) {
4002     StringRef SectName;
4003     Section.getName(SectName);
4004     DataRefImpl Ref = Section.getRawDataRefImpl();
4005     StringRef SegName = O->getSectionFinalSegmentName(Ref);
4006     if (SegName == segname && SectName == sectname)
4007       return Section;
4008   }
4009   return SectionRef();
4010 }
4011 
4012 static void
4013 walk_pointer_list_64(const char *listname, const SectionRef S,
4014                      MachOObjectFile *O, struct DisassembleInfo *info,
4015                      void (*func)(uint64_t, struct DisassembleInfo *info)) {
4016   if (S == SectionRef())
4017     return;
4018 
4019   StringRef SectName;
4020   S.getName(SectName);
4021   DataRefImpl Ref = S.getRawDataRefImpl();
4022   StringRef SegName = O->getSectionFinalSegmentName(Ref);
4023   outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4024 
4025   StringRef BytesStr = unwrapOrError(S.getContents(), O->getFileName());
4026   const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
4027 
4028   for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
4029     uint32_t left = S.getSize() - i;
4030     uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
4031     uint64_t p = 0;
4032     memcpy(&p, Contents + i, size);
4033     if (i + sizeof(uint64_t) > S.getSize())
4034       outs() << listname << " list pointer extends past end of (" << SegName
4035              << "," << SectName << ") section\n";
4036     outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
4037 
4038     if (O->isLittleEndian() != sys::IsLittleEndianHost)
4039       sys::swapByteOrder(p);
4040 
4041     uint64_t n_value = 0;
4042     const char *name = get_symbol_64(i, S, info, n_value, p);
4043     if (name == nullptr)
4044       name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
4045 
4046     if (n_value != 0) {
4047       outs() << format("0x%" PRIx64, n_value);
4048       if (p != 0)
4049         outs() << " + " << format("0x%" PRIx64, p);
4050     } else
4051       outs() << format("0x%" PRIx64, p);
4052     if (name != nullptr)
4053       outs() << " " << name;
4054     outs() << "\n";
4055 
4056     p += n_value;
4057     if (func)
4058       func(p, info);
4059   }
4060 }
4061 
4062 static void
4063 walk_pointer_list_32(const char *listname, const SectionRef S,
4064                      MachOObjectFile *O, struct DisassembleInfo *info,
4065                      void (*func)(uint32_t, struct DisassembleInfo *info)) {
4066   if (S == SectionRef())
4067     return;
4068 
4069   StringRef SectName;
4070   S.getName(SectName);
4071   DataRefImpl Ref = S.getRawDataRefImpl();
4072   StringRef SegName = O->getSectionFinalSegmentName(Ref);
4073   outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
4074 
4075   StringRef BytesStr = unwrapOrError(S.getContents(), O->getFileName());
4076   const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
4077 
4078   for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
4079     uint32_t left = S.getSize() - i;
4080     uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
4081     uint32_t p = 0;
4082     memcpy(&p, Contents + i, size);
4083     if (i + sizeof(uint32_t) > S.getSize())
4084       outs() << listname << " list pointer extends past end of (" << SegName
4085              << "," << SectName << ") section\n";
4086     uint32_t Address = S.getAddress() + i;
4087     outs() << format("%08" PRIx32, Address) << " ";
4088 
4089     if (O->isLittleEndian() != sys::IsLittleEndianHost)
4090       sys::swapByteOrder(p);
4091     outs() << format("0x%" PRIx32, p);
4092 
4093     const char *name = get_symbol_32(i, S, info, p);
4094     if (name != nullptr)
4095       outs() << " " << name;
4096     outs() << "\n";
4097 
4098     if (func)
4099       func(p, info);
4100   }
4101 }
4102 
4103 static void print_layout_map(const char *layout_map, uint32_t left) {
4104   if (layout_map == nullptr)
4105     return;
4106   outs() << "                layout map: ";
4107   do {
4108     outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
4109     left--;
4110     layout_map++;
4111   } while (*layout_map != '\0' && left != 0);
4112   outs() << "\n";
4113 }
4114 
4115 static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
4116   uint32_t offset, left;
4117   SectionRef S;
4118   const char *layout_map;
4119 
4120   if (p == 0)
4121     return;
4122   layout_map = get_pointer_64(p, offset, left, S, info);
4123   print_layout_map(layout_map, left);
4124 }
4125 
4126 static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
4127   uint32_t offset, left;
4128   SectionRef S;
4129   const char *layout_map;
4130 
4131   if (p == 0)
4132     return;
4133   layout_map = get_pointer_32(p, offset, left, S, info);
4134   print_layout_map(layout_map, left);
4135 }
4136 
4137 static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
4138                                   const char *indent) {
4139   struct method_list64_t ml;
4140   struct method64_t m;
4141   const char *r;
4142   uint32_t offset, xoffset, left, i;
4143   SectionRef S, xS;
4144   const char *name, *sym_name;
4145   uint64_t n_value;
4146 
4147   r = get_pointer_64(p, offset, left, S, info);
4148   if (r == nullptr)
4149     return;
4150   memset(&ml, '\0', sizeof(struct method_list64_t));
4151   if (left < sizeof(struct method_list64_t)) {
4152     memcpy(&ml, r, left);
4153     outs() << "   (method_list_t entends past the end of the section)\n";
4154   } else
4155     memcpy(&ml, r, sizeof(struct method_list64_t));
4156   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4157     swapStruct(ml);
4158   outs() << indent << "\t\t   entsize " << ml.entsize << "\n";
4159   outs() << indent << "\t\t     count " << ml.count << "\n";
4160 
4161   p += sizeof(struct method_list64_t);
4162   offset += sizeof(struct method_list64_t);
4163   for (i = 0; i < ml.count; i++) {
4164     r = get_pointer_64(p, offset, left, S, info);
4165     if (r == nullptr)
4166       return;
4167     memset(&m, '\0', sizeof(struct method64_t));
4168     if (left < sizeof(struct method64_t)) {
4169       memcpy(&m, r, left);
4170       outs() << indent << "   (method_t extends past the end of the section)\n";
4171     } else
4172       memcpy(&m, r, sizeof(struct method64_t));
4173     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4174       swapStruct(m);
4175 
4176     outs() << indent << "\t\t      name ";
4177     sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
4178                              info, n_value, m.name);
4179     if (n_value != 0) {
4180       if (info->verbose && sym_name != nullptr)
4181         outs() << sym_name;
4182       else
4183         outs() << format("0x%" PRIx64, n_value);
4184       if (m.name != 0)
4185         outs() << " + " << format("0x%" PRIx64, m.name);
4186     } else
4187       outs() << format("0x%" PRIx64, m.name);
4188     name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
4189     if (name != nullptr)
4190       outs() << format(" %.*s", left, name);
4191     outs() << "\n";
4192 
4193     outs() << indent << "\t\t     types ";
4194     sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
4195                              info, n_value, m.types);
4196     if (n_value != 0) {
4197       if (info->verbose && sym_name != nullptr)
4198         outs() << sym_name;
4199       else
4200         outs() << format("0x%" PRIx64, n_value);
4201       if (m.types != 0)
4202         outs() << " + " << format("0x%" PRIx64, m.types);
4203     } else
4204       outs() << format("0x%" PRIx64, m.types);
4205     name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
4206     if (name != nullptr)
4207       outs() << format(" %.*s", left, name);
4208     outs() << "\n";
4209 
4210     outs() << indent << "\t\t       imp ";
4211     name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
4212                          n_value, m.imp);
4213     if (info->verbose && name == nullptr) {
4214       if (n_value != 0) {
4215         outs() << format("0x%" PRIx64, n_value) << " ";
4216         if (m.imp != 0)
4217           outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
4218       } else
4219         outs() << format("0x%" PRIx64, m.imp) << " ";
4220     }
4221     if (name != nullptr)
4222       outs() << name;
4223     outs() << "\n";
4224 
4225     p += sizeof(struct method64_t);
4226     offset += sizeof(struct method64_t);
4227   }
4228 }
4229 
4230 static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
4231                                   const char *indent) {
4232   struct method_list32_t ml;
4233   struct method32_t m;
4234   const char *r, *name;
4235   uint32_t offset, xoffset, left, i;
4236   SectionRef S, xS;
4237 
4238   r = get_pointer_32(p, offset, left, S, info);
4239   if (r == nullptr)
4240     return;
4241   memset(&ml, '\0', sizeof(struct method_list32_t));
4242   if (left < sizeof(struct method_list32_t)) {
4243     memcpy(&ml, r, left);
4244     outs() << "   (method_list_t entends past the end of the section)\n";
4245   } else
4246     memcpy(&ml, r, sizeof(struct method_list32_t));
4247   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4248     swapStruct(ml);
4249   outs() << indent << "\t\t   entsize " << ml.entsize << "\n";
4250   outs() << indent << "\t\t     count " << ml.count << "\n";
4251 
4252   p += sizeof(struct method_list32_t);
4253   offset += sizeof(struct method_list32_t);
4254   for (i = 0; i < ml.count; i++) {
4255     r = get_pointer_32(p, offset, left, S, info);
4256     if (r == nullptr)
4257       return;
4258     memset(&m, '\0', sizeof(struct method32_t));
4259     if (left < sizeof(struct method32_t)) {
4260       memcpy(&ml, r, left);
4261       outs() << indent << "   (method_t entends past the end of the section)\n";
4262     } else
4263       memcpy(&m, r, sizeof(struct method32_t));
4264     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4265       swapStruct(m);
4266 
4267     outs() << indent << "\t\t      name " << format("0x%" PRIx32, m.name);
4268     name = get_pointer_32(m.name, xoffset, left, xS, info);
4269     if (name != nullptr)
4270       outs() << format(" %.*s", left, name);
4271     outs() << "\n";
4272 
4273     outs() << indent << "\t\t     types " << format("0x%" PRIx32, m.types);
4274     name = get_pointer_32(m.types, xoffset, left, xS, info);
4275     if (name != nullptr)
4276       outs() << format(" %.*s", left, name);
4277     outs() << "\n";
4278 
4279     outs() << indent << "\t\t       imp " << format("0x%" PRIx32, m.imp);
4280     name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
4281                          m.imp);
4282     if (name != nullptr)
4283       outs() << " " << name;
4284     outs() << "\n";
4285 
4286     p += sizeof(struct method32_t);
4287     offset += sizeof(struct method32_t);
4288   }
4289 }
4290 
4291 static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
4292   uint32_t offset, left, xleft;
4293   SectionRef S;
4294   struct objc_method_list_t method_list;
4295   struct objc_method_t method;
4296   const char *r, *methods, *name, *SymbolName;
4297   int32_t i;
4298 
4299   r = get_pointer_32(p, offset, left, S, info, true);
4300   if (r == nullptr)
4301     return true;
4302 
4303   outs() << "\n";
4304   if (left > sizeof(struct objc_method_list_t)) {
4305     memcpy(&method_list, r, sizeof(struct objc_method_list_t));
4306   } else {
4307     outs() << "\t\t objc_method_list extends past end of the section\n";
4308     memset(&method_list, '\0', sizeof(struct objc_method_list_t));
4309     memcpy(&method_list, r, left);
4310   }
4311   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4312     swapStruct(method_list);
4313 
4314   outs() << "\t\t         obsolete "
4315          << format("0x%08" PRIx32, method_list.obsolete) << "\n";
4316   outs() << "\t\t     method_count " << method_list.method_count << "\n";
4317 
4318   methods = r + sizeof(struct objc_method_list_t);
4319   for (i = 0; i < method_list.method_count; i++) {
4320     if ((i + 1) * sizeof(struct objc_method_t) > left) {
4321       outs() << "\t\t remaining method's extend past the of the section\n";
4322       break;
4323     }
4324     memcpy(&method, methods + i * sizeof(struct objc_method_t),
4325            sizeof(struct objc_method_t));
4326     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4327       swapStruct(method);
4328 
4329     outs() << "\t\t      method_name "
4330            << format("0x%08" PRIx32, method.method_name);
4331     if (info->verbose) {
4332       name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
4333       if (name != nullptr)
4334         outs() << format(" %.*s", xleft, name);
4335       else
4336         outs() << " (not in an __OBJC section)";
4337     }
4338     outs() << "\n";
4339 
4340     outs() << "\t\t     method_types "
4341            << format("0x%08" PRIx32, method.method_types);
4342     if (info->verbose) {
4343       name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
4344       if (name != nullptr)
4345         outs() << format(" %.*s", xleft, name);
4346       else
4347         outs() << " (not in an __OBJC section)";
4348     }
4349     outs() << "\n";
4350 
4351     outs() << "\t\t       method_imp "
4352            << format("0x%08" PRIx32, method.method_imp) << " ";
4353     if (info->verbose) {
4354       SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
4355       if (SymbolName != nullptr)
4356         outs() << SymbolName;
4357     }
4358     outs() << "\n";
4359   }
4360   return false;
4361 }
4362 
4363 static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
4364   struct protocol_list64_t pl;
4365   uint64_t q, n_value;
4366   struct protocol64_t pc;
4367   const char *r;
4368   uint32_t offset, xoffset, left, i;
4369   SectionRef S, xS;
4370   const char *name, *sym_name;
4371 
4372   r = get_pointer_64(p, offset, left, S, info);
4373   if (r == nullptr)
4374     return;
4375   memset(&pl, '\0', sizeof(struct protocol_list64_t));
4376   if (left < sizeof(struct protocol_list64_t)) {
4377     memcpy(&pl, r, left);
4378     outs() << "   (protocol_list_t entends past the end of the section)\n";
4379   } else
4380     memcpy(&pl, r, sizeof(struct protocol_list64_t));
4381   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4382     swapStruct(pl);
4383   outs() << "                      count " << pl.count << "\n";
4384 
4385   p += sizeof(struct protocol_list64_t);
4386   offset += sizeof(struct protocol_list64_t);
4387   for (i = 0; i < pl.count; i++) {
4388     r = get_pointer_64(p, offset, left, S, info);
4389     if (r == nullptr)
4390       return;
4391     q = 0;
4392     if (left < sizeof(uint64_t)) {
4393       memcpy(&q, r, left);
4394       outs() << "   (protocol_t * entends past the end of the section)\n";
4395     } else
4396       memcpy(&q, r, sizeof(uint64_t));
4397     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4398       sys::swapByteOrder(q);
4399 
4400     outs() << "\t\t      list[" << i << "] ";
4401     sym_name = get_symbol_64(offset, S, info, n_value, q);
4402     if (n_value != 0) {
4403       if (info->verbose && sym_name != nullptr)
4404         outs() << sym_name;
4405       else
4406         outs() << format("0x%" PRIx64, n_value);
4407       if (q != 0)
4408         outs() << " + " << format("0x%" PRIx64, q);
4409     } else
4410       outs() << format("0x%" PRIx64, q);
4411     outs() << " (struct protocol_t *)\n";
4412 
4413     r = get_pointer_64(q + n_value, offset, left, S, info);
4414     if (r == nullptr)
4415       return;
4416     memset(&pc, '\0', sizeof(struct protocol64_t));
4417     if (left < sizeof(struct protocol64_t)) {
4418       memcpy(&pc, r, left);
4419       outs() << "   (protocol_t entends past the end of the section)\n";
4420     } else
4421       memcpy(&pc, r, sizeof(struct protocol64_t));
4422     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4423       swapStruct(pc);
4424 
4425     outs() << "\t\t\t      isa " << format("0x%" PRIx64, pc.isa) << "\n";
4426 
4427     outs() << "\t\t\t     name ";
4428     sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
4429                              info, n_value, pc.name);
4430     if (n_value != 0) {
4431       if (info->verbose && sym_name != nullptr)
4432         outs() << sym_name;
4433       else
4434         outs() << format("0x%" PRIx64, n_value);
4435       if (pc.name != 0)
4436         outs() << " + " << format("0x%" PRIx64, pc.name);
4437     } else
4438       outs() << format("0x%" PRIx64, pc.name);
4439     name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
4440     if (name != nullptr)
4441       outs() << format(" %.*s", left, name);
4442     outs() << "\n";
4443 
4444     outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
4445 
4446     outs() << "\t\t  instanceMethods ";
4447     sym_name =
4448         get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
4449                       S, info, n_value, pc.instanceMethods);
4450     if (n_value != 0) {
4451       if (info->verbose && sym_name != nullptr)
4452         outs() << sym_name;
4453       else
4454         outs() << format("0x%" PRIx64, n_value);
4455       if (pc.instanceMethods != 0)
4456         outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
4457     } else
4458       outs() << format("0x%" PRIx64, pc.instanceMethods);
4459     outs() << " (struct method_list_t *)\n";
4460     if (pc.instanceMethods + n_value != 0)
4461       print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
4462 
4463     outs() << "\t\t     classMethods ";
4464     sym_name =
4465         get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
4466                       info, n_value, pc.classMethods);
4467     if (n_value != 0) {
4468       if (info->verbose && sym_name != nullptr)
4469         outs() << sym_name;
4470       else
4471         outs() << format("0x%" PRIx64, n_value);
4472       if (pc.classMethods != 0)
4473         outs() << " + " << format("0x%" PRIx64, pc.classMethods);
4474     } else
4475       outs() << format("0x%" PRIx64, pc.classMethods);
4476     outs() << " (struct method_list_t *)\n";
4477     if (pc.classMethods + n_value != 0)
4478       print_method_list64_t(pc.classMethods + n_value, info, "\t");
4479 
4480     outs() << "\t  optionalInstanceMethods "
4481            << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
4482     outs() << "\t     optionalClassMethods "
4483            << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
4484     outs() << "\t       instanceProperties "
4485            << format("0x%" PRIx64, pc.instanceProperties) << "\n";
4486 
4487     p += sizeof(uint64_t);
4488     offset += sizeof(uint64_t);
4489   }
4490 }
4491 
4492 static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
4493   struct protocol_list32_t pl;
4494   uint32_t q;
4495   struct protocol32_t pc;
4496   const char *r;
4497   uint32_t offset, xoffset, left, i;
4498   SectionRef S, xS;
4499   const char *name;
4500 
4501   r = get_pointer_32(p, offset, left, S, info);
4502   if (r == nullptr)
4503     return;
4504   memset(&pl, '\0', sizeof(struct protocol_list32_t));
4505   if (left < sizeof(struct protocol_list32_t)) {
4506     memcpy(&pl, r, left);
4507     outs() << "   (protocol_list_t entends past the end of the section)\n";
4508   } else
4509     memcpy(&pl, r, sizeof(struct protocol_list32_t));
4510   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4511     swapStruct(pl);
4512   outs() << "                      count " << pl.count << "\n";
4513 
4514   p += sizeof(struct protocol_list32_t);
4515   offset += sizeof(struct protocol_list32_t);
4516   for (i = 0; i < pl.count; i++) {
4517     r = get_pointer_32(p, offset, left, S, info);
4518     if (r == nullptr)
4519       return;
4520     q = 0;
4521     if (left < sizeof(uint32_t)) {
4522       memcpy(&q, r, left);
4523       outs() << "   (protocol_t * entends past the end of the section)\n";
4524     } else
4525       memcpy(&q, r, sizeof(uint32_t));
4526     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4527       sys::swapByteOrder(q);
4528     outs() << "\t\t      list[" << i << "] " << format("0x%" PRIx32, q)
4529            << " (struct protocol_t *)\n";
4530     r = get_pointer_32(q, offset, left, S, info);
4531     if (r == nullptr)
4532       return;
4533     memset(&pc, '\0', sizeof(struct protocol32_t));
4534     if (left < sizeof(struct protocol32_t)) {
4535       memcpy(&pc, r, left);
4536       outs() << "   (protocol_t entends past the end of the section)\n";
4537     } else
4538       memcpy(&pc, r, sizeof(struct protocol32_t));
4539     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4540       swapStruct(pc);
4541     outs() << "\t\t\t      isa " << format("0x%" PRIx32, pc.isa) << "\n";
4542     outs() << "\t\t\t     name " << format("0x%" PRIx32, pc.name);
4543     name = get_pointer_32(pc.name, xoffset, left, xS, info);
4544     if (name != nullptr)
4545       outs() << format(" %.*s", left, name);
4546     outs() << "\n";
4547     outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
4548     outs() << "\t\t  instanceMethods "
4549            << format("0x%" PRIx32, pc.instanceMethods)
4550            << " (struct method_list_t *)\n";
4551     if (pc.instanceMethods != 0)
4552       print_method_list32_t(pc.instanceMethods, info, "\t");
4553     outs() << "\t\t     classMethods " << format("0x%" PRIx32, pc.classMethods)
4554            << " (struct method_list_t *)\n";
4555     if (pc.classMethods != 0)
4556       print_method_list32_t(pc.classMethods, info, "\t");
4557     outs() << "\t  optionalInstanceMethods "
4558            << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
4559     outs() << "\t     optionalClassMethods "
4560            << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
4561     outs() << "\t       instanceProperties "
4562            << format("0x%" PRIx32, pc.instanceProperties) << "\n";
4563     p += sizeof(uint32_t);
4564     offset += sizeof(uint32_t);
4565   }
4566 }
4567 
4568 static void print_indent(uint32_t indent) {
4569   for (uint32_t i = 0; i < indent;) {
4570     if (indent - i >= 8) {
4571       outs() << "\t";
4572       i += 8;
4573     } else {
4574       for (uint32_t j = i; j < indent; j++)
4575         outs() << " ";
4576       return;
4577     }
4578   }
4579 }
4580 
4581 static bool print_method_description_list(uint32_t p, uint32_t indent,
4582                                           struct DisassembleInfo *info) {
4583   uint32_t offset, left, xleft;
4584   SectionRef S;
4585   struct objc_method_description_list_t mdl;
4586   struct objc_method_description_t md;
4587   const char *r, *list, *name;
4588   int32_t i;
4589 
4590   r = get_pointer_32(p, offset, left, S, info, true);
4591   if (r == nullptr)
4592     return true;
4593 
4594   outs() << "\n";
4595   if (left > sizeof(struct objc_method_description_list_t)) {
4596     memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
4597   } else {
4598     print_indent(indent);
4599     outs() << " objc_method_description_list extends past end of the section\n";
4600     memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
4601     memcpy(&mdl, r, left);
4602   }
4603   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4604     swapStruct(mdl);
4605 
4606   print_indent(indent);
4607   outs() << "        count " << mdl.count << "\n";
4608 
4609   list = r + sizeof(struct objc_method_description_list_t);
4610   for (i = 0; i < mdl.count; i++) {
4611     if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
4612       print_indent(indent);
4613       outs() << " remaining list entries extend past the of the section\n";
4614       break;
4615     }
4616     print_indent(indent);
4617     outs() << "        list[" << i << "]\n";
4618     memcpy(&md, list + i * sizeof(struct objc_method_description_t),
4619            sizeof(struct objc_method_description_t));
4620     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4621       swapStruct(md);
4622 
4623     print_indent(indent);
4624     outs() << "             name " << format("0x%08" PRIx32, md.name);
4625     if (info->verbose) {
4626       name = get_pointer_32(md.name, offset, xleft, S, info, true);
4627       if (name != nullptr)
4628         outs() << format(" %.*s", xleft, name);
4629       else
4630         outs() << " (not in an __OBJC section)";
4631     }
4632     outs() << "\n";
4633 
4634     print_indent(indent);
4635     outs() << "            types " << format("0x%08" PRIx32, md.types);
4636     if (info->verbose) {
4637       name = get_pointer_32(md.types, offset, xleft, S, info, true);
4638       if (name != nullptr)
4639         outs() << format(" %.*s", xleft, name);
4640       else
4641         outs() << " (not in an __OBJC section)";
4642     }
4643     outs() << "\n";
4644   }
4645   return false;
4646 }
4647 
4648 static bool print_protocol_list(uint32_t p, uint32_t indent,
4649                                 struct DisassembleInfo *info);
4650 
4651 static bool print_protocol(uint32_t p, uint32_t indent,
4652                            struct DisassembleInfo *info) {
4653   uint32_t offset, left;
4654   SectionRef S;
4655   struct objc_protocol_t protocol;
4656   const char *r, *name;
4657 
4658   r = get_pointer_32(p, offset, left, S, info, true);
4659   if (r == nullptr)
4660     return true;
4661 
4662   outs() << "\n";
4663   if (left >= sizeof(struct objc_protocol_t)) {
4664     memcpy(&protocol, r, sizeof(struct objc_protocol_t));
4665   } else {
4666     print_indent(indent);
4667     outs() << "            Protocol extends past end of the section\n";
4668     memset(&protocol, '\0', sizeof(struct objc_protocol_t));
4669     memcpy(&protocol, r, left);
4670   }
4671   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4672     swapStruct(protocol);
4673 
4674   print_indent(indent);
4675   outs() << "              isa " << format("0x%08" PRIx32, protocol.isa)
4676          << "\n";
4677 
4678   print_indent(indent);
4679   outs() << "    protocol_name "
4680          << format("0x%08" PRIx32, protocol.protocol_name);
4681   if (info->verbose) {
4682     name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
4683     if (name != nullptr)
4684       outs() << format(" %.*s", left, name);
4685     else
4686       outs() << " (not in an __OBJC section)";
4687   }
4688   outs() << "\n";
4689 
4690   print_indent(indent);
4691   outs() << "    protocol_list "
4692          << format("0x%08" PRIx32, protocol.protocol_list);
4693   if (print_protocol_list(protocol.protocol_list, indent + 4, info))
4694     outs() << " (not in an __OBJC section)\n";
4695 
4696   print_indent(indent);
4697   outs() << " instance_methods "
4698          << format("0x%08" PRIx32, protocol.instance_methods);
4699   if (print_method_description_list(protocol.instance_methods, indent, info))
4700     outs() << " (not in an __OBJC section)\n";
4701 
4702   print_indent(indent);
4703   outs() << "    class_methods "
4704          << format("0x%08" PRIx32, protocol.class_methods);
4705   if (print_method_description_list(protocol.class_methods, indent, info))
4706     outs() << " (not in an __OBJC section)\n";
4707 
4708   return false;
4709 }
4710 
4711 static bool print_protocol_list(uint32_t p, uint32_t indent,
4712                                 struct DisassembleInfo *info) {
4713   uint32_t offset, left, l;
4714   SectionRef S;
4715   struct objc_protocol_list_t protocol_list;
4716   const char *r, *list;
4717   int32_t i;
4718 
4719   r = get_pointer_32(p, offset, left, S, info, true);
4720   if (r == nullptr)
4721     return true;
4722 
4723   outs() << "\n";
4724   if (left > sizeof(struct objc_protocol_list_t)) {
4725     memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
4726   } else {
4727     outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
4728     memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
4729     memcpy(&protocol_list, r, left);
4730   }
4731   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4732     swapStruct(protocol_list);
4733 
4734   print_indent(indent);
4735   outs() << "         next " << format("0x%08" PRIx32, protocol_list.next)
4736          << "\n";
4737   print_indent(indent);
4738   outs() << "        count " << protocol_list.count << "\n";
4739 
4740   list = r + sizeof(struct objc_protocol_list_t);
4741   for (i = 0; i < protocol_list.count; i++) {
4742     if ((i + 1) * sizeof(uint32_t) > left) {
4743       outs() << "\t\t remaining list entries extend past the of the section\n";
4744       break;
4745     }
4746     memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
4747     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4748       sys::swapByteOrder(l);
4749 
4750     print_indent(indent);
4751     outs() << "      list[" << i << "] " << format("0x%08" PRIx32, l);
4752     if (print_protocol(l, indent, info))
4753       outs() << "(not in an __OBJC section)\n";
4754   }
4755   return false;
4756 }
4757 
4758 static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
4759   struct ivar_list64_t il;
4760   struct ivar64_t i;
4761   const char *r;
4762   uint32_t offset, xoffset, left, j;
4763   SectionRef S, xS;
4764   const char *name, *sym_name, *ivar_offset_p;
4765   uint64_t ivar_offset, n_value;
4766 
4767   r = get_pointer_64(p, offset, left, S, info);
4768   if (r == nullptr)
4769     return;
4770   memset(&il, '\0', sizeof(struct ivar_list64_t));
4771   if (left < sizeof(struct ivar_list64_t)) {
4772     memcpy(&il, r, left);
4773     outs() << "   (ivar_list_t entends past the end of the section)\n";
4774   } else
4775     memcpy(&il, r, sizeof(struct ivar_list64_t));
4776   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4777     swapStruct(il);
4778   outs() << "                    entsize " << il.entsize << "\n";
4779   outs() << "                      count " << il.count << "\n";
4780 
4781   p += sizeof(struct ivar_list64_t);
4782   offset += sizeof(struct ivar_list64_t);
4783   for (j = 0; j < il.count; j++) {
4784     r = get_pointer_64(p, offset, left, S, info);
4785     if (r == nullptr)
4786       return;
4787     memset(&i, '\0', sizeof(struct ivar64_t));
4788     if (left < sizeof(struct ivar64_t)) {
4789       memcpy(&i, r, left);
4790       outs() << "   (ivar_t entends past the end of the section)\n";
4791     } else
4792       memcpy(&i, r, sizeof(struct ivar64_t));
4793     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4794       swapStruct(i);
4795 
4796     outs() << "\t\t\t   offset ";
4797     sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
4798                              info, n_value, i.offset);
4799     if (n_value != 0) {
4800       if (info->verbose && sym_name != nullptr)
4801         outs() << sym_name;
4802       else
4803         outs() << format("0x%" PRIx64, n_value);
4804       if (i.offset != 0)
4805         outs() << " + " << format("0x%" PRIx64, i.offset);
4806     } else
4807       outs() << format("0x%" PRIx64, i.offset);
4808     ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
4809     if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4810       memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4811       if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4812         sys::swapByteOrder(ivar_offset);
4813       outs() << " " << ivar_offset << "\n";
4814     } else
4815       outs() << "\n";
4816 
4817     outs() << "\t\t\t     name ";
4818     sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
4819                              n_value, i.name);
4820     if (n_value != 0) {
4821       if (info->verbose && sym_name != nullptr)
4822         outs() << sym_name;
4823       else
4824         outs() << format("0x%" PRIx64, n_value);
4825       if (i.name != 0)
4826         outs() << " + " << format("0x%" PRIx64, i.name);
4827     } else
4828       outs() << format("0x%" PRIx64, i.name);
4829     name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
4830     if (name != nullptr)
4831       outs() << format(" %.*s", left, name);
4832     outs() << "\n";
4833 
4834     outs() << "\t\t\t     type ";
4835     sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
4836                              n_value, i.name);
4837     name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
4838     if (n_value != 0) {
4839       if (info->verbose && sym_name != nullptr)
4840         outs() << sym_name;
4841       else
4842         outs() << format("0x%" PRIx64, n_value);
4843       if (i.type != 0)
4844         outs() << " + " << format("0x%" PRIx64, i.type);
4845     } else
4846       outs() << format("0x%" PRIx64, i.type);
4847     if (name != nullptr)
4848       outs() << format(" %.*s", left, name);
4849     outs() << "\n";
4850 
4851     outs() << "\t\t\talignment " << i.alignment << "\n";
4852     outs() << "\t\t\t     size " << i.size << "\n";
4853 
4854     p += sizeof(struct ivar64_t);
4855     offset += sizeof(struct ivar64_t);
4856   }
4857 }
4858 
4859 static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
4860   struct ivar_list32_t il;
4861   struct ivar32_t i;
4862   const char *r;
4863   uint32_t offset, xoffset, left, j;
4864   SectionRef S, xS;
4865   const char *name, *ivar_offset_p;
4866   uint32_t ivar_offset;
4867 
4868   r = get_pointer_32(p, offset, left, S, info);
4869   if (r == nullptr)
4870     return;
4871   memset(&il, '\0', sizeof(struct ivar_list32_t));
4872   if (left < sizeof(struct ivar_list32_t)) {
4873     memcpy(&il, r, left);
4874     outs() << "   (ivar_list_t entends past the end of the section)\n";
4875   } else
4876     memcpy(&il, r, sizeof(struct ivar_list32_t));
4877   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4878     swapStruct(il);
4879   outs() << "                    entsize " << il.entsize << "\n";
4880   outs() << "                      count " << il.count << "\n";
4881 
4882   p += sizeof(struct ivar_list32_t);
4883   offset += sizeof(struct ivar_list32_t);
4884   for (j = 0; j < il.count; j++) {
4885     r = get_pointer_32(p, offset, left, S, info);
4886     if (r == nullptr)
4887       return;
4888     memset(&i, '\0', sizeof(struct ivar32_t));
4889     if (left < sizeof(struct ivar32_t)) {
4890       memcpy(&i, r, left);
4891       outs() << "   (ivar_t entends past the end of the section)\n";
4892     } else
4893       memcpy(&i, r, sizeof(struct ivar32_t));
4894     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4895       swapStruct(i);
4896 
4897     outs() << "\t\t\t   offset " << format("0x%" PRIx32, i.offset);
4898     ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4899     if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4900       memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4901       if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4902         sys::swapByteOrder(ivar_offset);
4903       outs() << " " << ivar_offset << "\n";
4904     } else
4905       outs() << "\n";
4906 
4907     outs() << "\t\t\t     name " << format("0x%" PRIx32, i.name);
4908     name = get_pointer_32(i.name, xoffset, left, xS, info);
4909     if (name != nullptr)
4910       outs() << format(" %.*s", left, name);
4911     outs() << "\n";
4912 
4913     outs() << "\t\t\t     type " << format("0x%" PRIx32, i.type);
4914     name = get_pointer_32(i.type, xoffset, left, xS, info);
4915     if (name != nullptr)
4916       outs() << format(" %.*s", left, name);
4917     outs() << "\n";
4918 
4919     outs() << "\t\t\talignment " << i.alignment << "\n";
4920     outs() << "\t\t\t     size " << i.size << "\n";
4921 
4922     p += sizeof(struct ivar32_t);
4923     offset += sizeof(struct ivar32_t);
4924   }
4925 }
4926 
4927 static void print_objc_property_list64(uint64_t p,
4928                                        struct DisassembleInfo *info) {
4929   struct objc_property_list64 opl;
4930   struct objc_property64 op;
4931   const char *r;
4932   uint32_t offset, xoffset, left, j;
4933   SectionRef S, xS;
4934   const char *name, *sym_name;
4935   uint64_t n_value;
4936 
4937   r = get_pointer_64(p, offset, left, S, info);
4938   if (r == nullptr)
4939     return;
4940   memset(&opl, '\0', sizeof(struct objc_property_list64));
4941   if (left < sizeof(struct objc_property_list64)) {
4942     memcpy(&opl, r, left);
4943     outs() << "   (objc_property_list entends past the end of the section)\n";
4944   } else
4945     memcpy(&opl, r, sizeof(struct objc_property_list64));
4946   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4947     swapStruct(opl);
4948   outs() << "                    entsize " << opl.entsize << "\n";
4949   outs() << "                      count " << opl.count << "\n";
4950 
4951   p += sizeof(struct objc_property_list64);
4952   offset += sizeof(struct objc_property_list64);
4953   for (j = 0; j < opl.count; j++) {
4954     r = get_pointer_64(p, offset, left, S, info);
4955     if (r == nullptr)
4956       return;
4957     memset(&op, '\0', sizeof(struct objc_property64));
4958     if (left < sizeof(struct objc_property64)) {
4959       memcpy(&op, r, left);
4960       outs() << "   (objc_property entends past the end of the section)\n";
4961     } else
4962       memcpy(&op, r, sizeof(struct objc_property64));
4963     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4964       swapStruct(op);
4965 
4966     outs() << "\t\t\t     name ";
4967     sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
4968                              info, n_value, op.name);
4969     if (n_value != 0) {
4970       if (info->verbose && sym_name != nullptr)
4971         outs() << sym_name;
4972       else
4973         outs() << format("0x%" PRIx64, n_value);
4974       if (op.name != 0)
4975         outs() << " + " << format("0x%" PRIx64, op.name);
4976     } else
4977       outs() << format("0x%" PRIx64, op.name);
4978     name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
4979     if (name != nullptr)
4980       outs() << format(" %.*s", left, name);
4981     outs() << "\n";
4982 
4983     outs() << "\t\t\tattributes ";
4984     sym_name =
4985         get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
4986                       info, n_value, op.attributes);
4987     if (n_value != 0) {
4988       if (info->verbose && sym_name != nullptr)
4989         outs() << sym_name;
4990       else
4991         outs() << format("0x%" PRIx64, n_value);
4992       if (op.attributes != 0)
4993         outs() << " + " << format("0x%" PRIx64, op.attributes);
4994     } else
4995       outs() << format("0x%" PRIx64, op.attributes);
4996     name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
4997     if (name != nullptr)
4998       outs() << format(" %.*s", left, name);
4999     outs() << "\n";
5000 
5001     p += sizeof(struct objc_property64);
5002     offset += sizeof(struct objc_property64);
5003   }
5004 }
5005 
5006 static void print_objc_property_list32(uint32_t p,
5007                                        struct DisassembleInfo *info) {
5008   struct objc_property_list32 opl;
5009   struct objc_property32 op;
5010   const char *r;
5011   uint32_t offset, xoffset, left, j;
5012   SectionRef S, xS;
5013   const char *name;
5014 
5015   r = get_pointer_32(p, offset, left, S, info);
5016   if (r == nullptr)
5017     return;
5018   memset(&opl, '\0', sizeof(struct objc_property_list32));
5019   if (left < sizeof(struct objc_property_list32)) {
5020     memcpy(&opl, r, left);
5021     outs() << "   (objc_property_list entends past the end of the section)\n";
5022   } else
5023     memcpy(&opl, r, sizeof(struct objc_property_list32));
5024   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5025     swapStruct(opl);
5026   outs() << "                    entsize " << opl.entsize << "\n";
5027   outs() << "                      count " << opl.count << "\n";
5028 
5029   p += sizeof(struct objc_property_list32);
5030   offset += sizeof(struct objc_property_list32);
5031   for (j = 0; j < opl.count; j++) {
5032     r = get_pointer_32(p, offset, left, S, info);
5033     if (r == nullptr)
5034       return;
5035     memset(&op, '\0', sizeof(struct objc_property32));
5036     if (left < sizeof(struct objc_property32)) {
5037       memcpy(&op, r, left);
5038       outs() << "   (objc_property entends past the end of the section)\n";
5039     } else
5040       memcpy(&op, r, sizeof(struct objc_property32));
5041     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5042       swapStruct(op);
5043 
5044     outs() << "\t\t\t     name " << format("0x%" PRIx32, op.name);
5045     name = get_pointer_32(op.name, xoffset, left, xS, info);
5046     if (name != nullptr)
5047       outs() << format(" %.*s", left, name);
5048     outs() << "\n";
5049 
5050     outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
5051     name = get_pointer_32(op.attributes, xoffset, left, xS, info);
5052     if (name != nullptr)
5053       outs() << format(" %.*s", left, name);
5054     outs() << "\n";
5055 
5056     p += sizeof(struct objc_property32);
5057     offset += sizeof(struct objc_property32);
5058   }
5059 }
5060 
5061 static bool print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
5062                                bool &is_meta_class) {
5063   struct class_ro64_t cro;
5064   const char *r;
5065   uint32_t offset, xoffset, left;
5066   SectionRef S, xS;
5067   const char *name, *sym_name;
5068   uint64_t n_value;
5069 
5070   r = get_pointer_64(p, offset, left, S, info);
5071   if (r == nullptr || left < sizeof(struct class_ro64_t))
5072     return false;
5073   memcpy(&cro, r, sizeof(struct class_ro64_t));
5074   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5075     swapStruct(cro);
5076   outs() << "                    flags " << format("0x%" PRIx32, cro.flags);
5077   if (cro.flags & RO_META)
5078     outs() << " RO_META";
5079   if (cro.flags & RO_ROOT)
5080     outs() << " RO_ROOT";
5081   if (cro.flags & RO_HAS_CXX_STRUCTORS)
5082     outs() << " RO_HAS_CXX_STRUCTORS";
5083   outs() << "\n";
5084   outs() << "            instanceStart " << cro.instanceStart << "\n";
5085   outs() << "             instanceSize " << cro.instanceSize << "\n";
5086   outs() << "                 reserved " << format("0x%" PRIx32, cro.reserved)
5087          << "\n";
5088   outs() << "               ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
5089          << "\n";
5090   print_layout_map64(cro.ivarLayout, info);
5091 
5092   outs() << "                     name ";
5093   sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
5094                            info, n_value, cro.name);
5095   if (n_value != 0) {
5096     if (info->verbose && sym_name != nullptr)
5097       outs() << sym_name;
5098     else
5099       outs() << format("0x%" PRIx64, n_value);
5100     if (cro.name != 0)
5101       outs() << " + " << format("0x%" PRIx64, cro.name);
5102   } else
5103     outs() << format("0x%" PRIx64, cro.name);
5104   name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
5105   if (name != nullptr)
5106     outs() << format(" %.*s", left, name);
5107   outs() << "\n";
5108 
5109   outs() << "              baseMethods ";
5110   sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
5111                            S, info, n_value, cro.baseMethods);
5112   if (n_value != 0) {
5113     if (info->verbose && sym_name != nullptr)
5114       outs() << sym_name;
5115     else
5116       outs() << format("0x%" PRIx64, n_value);
5117     if (cro.baseMethods != 0)
5118       outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
5119   } else
5120     outs() << format("0x%" PRIx64, cro.baseMethods);
5121   outs() << " (struct method_list_t *)\n";
5122   if (cro.baseMethods + n_value != 0)
5123     print_method_list64_t(cro.baseMethods + n_value, info, "");
5124 
5125   outs() << "            baseProtocols ";
5126   sym_name =
5127       get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
5128                     info, n_value, cro.baseProtocols);
5129   if (n_value != 0) {
5130     if (info->verbose && sym_name != nullptr)
5131       outs() << sym_name;
5132     else
5133       outs() << format("0x%" PRIx64, n_value);
5134     if (cro.baseProtocols != 0)
5135       outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
5136   } else
5137     outs() << format("0x%" PRIx64, cro.baseProtocols);
5138   outs() << "\n";
5139   if (cro.baseProtocols + n_value != 0)
5140     print_protocol_list64_t(cro.baseProtocols + n_value, info);
5141 
5142   outs() << "                    ivars ";
5143   sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
5144                            info, n_value, cro.ivars);
5145   if (n_value != 0) {
5146     if (info->verbose && sym_name != nullptr)
5147       outs() << sym_name;
5148     else
5149       outs() << format("0x%" PRIx64, n_value);
5150     if (cro.ivars != 0)
5151       outs() << " + " << format("0x%" PRIx64, cro.ivars);
5152   } else
5153     outs() << format("0x%" PRIx64, cro.ivars);
5154   outs() << "\n";
5155   if (cro.ivars + n_value != 0)
5156     print_ivar_list64_t(cro.ivars + n_value, info);
5157 
5158   outs() << "           weakIvarLayout ";
5159   sym_name =
5160       get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
5161                     info, n_value, cro.weakIvarLayout);
5162   if (n_value != 0) {
5163     if (info->verbose && sym_name != nullptr)
5164       outs() << sym_name;
5165     else
5166       outs() << format("0x%" PRIx64, n_value);
5167     if (cro.weakIvarLayout != 0)
5168       outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
5169   } else
5170     outs() << format("0x%" PRIx64, cro.weakIvarLayout);
5171   outs() << "\n";
5172   print_layout_map64(cro.weakIvarLayout + n_value, info);
5173 
5174   outs() << "           baseProperties ";
5175   sym_name =
5176       get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
5177                     info, n_value, cro.baseProperties);
5178   if (n_value != 0) {
5179     if (info->verbose && sym_name != nullptr)
5180       outs() << sym_name;
5181     else
5182       outs() << format("0x%" PRIx64, n_value);
5183     if (cro.baseProperties != 0)
5184       outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
5185   } else
5186     outs() << format("0x%" PRIx64, cro.baseProperties);
5187   outs() << "\n";
5188   if (cro.baseProperties + n_value != 0)
5189     print_objc_property_list64(cro.baseProperties + n_value, info);
5190 
5191   is_meta_class = (cro.flags & RO_META) != 0;
5192   return true;
5193 }
5194 
5195 static bool print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
5196                                bool &is_meta_class) {
5197   struct class_ro32_t cro;
5198   const char *r;
5199   uint32_t offset, xoffset, left;
5200   SectionRef S, xS;
5201   const char *name;
5202 
5203   r = get_pointer_32(p, offset, left, S, info);
5204   if (r == nullptr)
5205     return false;
5206   memset(&cro, '\0', sizeof(struct class_ro32_t));
5207   if (left < sizeof(struct class_ro32_t)) {
5208     memcpy(&cro, r, left);
5209     outs() << "   (class_ro_t entends past the end of the section)\n";
5210   } else
5211     memcpy(&cro, r, sizeof(struct class_ro32_t));
5212   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5213     swapStruct(cro);
5214   outs() << "                    flags " << format("0x%" PRIx32, cro.flags);
5215   if (cro.flags & RO_META)
5216     outs() << " RO_META";
5217   if (cro.flags & RO_ROOT)
5218     outs() << " RO_ROOT";
5219   if (cro.flags & RO_HAS_CXX_STRUCTORS)
5220     outs() << " RO_HAS_CXX_STRUCTORS";
5221   outs() << "\n";
5222   outs() << "            instanceStart " << cro.instanceStart << "\n";
5223   outs() << "             instanceSize " << cro.instanceSize << "\n";
5224   outs() << "               ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
5225          << "\n";
5226   print_layout_map32(cro.ivarLayout, info);
5227 
5228   outs() << "                     name " << format("0x%" PRIx32, cro.name);
5229   name = get_pointer_32(cro.name, xoffset, left, xS, info);
5230   if (name != nullptr)
5231     outs() << format(" %.*s", left, name);
5232   outs() << "\n";
5233 
5234   outs() << "              baseMethods "
5235          << format("0x%" PRIx32, cro.baseMethods)
5236          << " (struct method_list_t *)\n";
5237   if (cro.baseMethods != 0)
5238     print_method_list32_t(cro.baseMethods, info, "");
5239 
5240   outs() << "            baseProtocols "
5241          << format("0x%" PRIx32, cro.baseProtocols) << "\n";
5242   if (cro.baseProtocols != 0)
5243     print_protocol_list32_t(cro.baseProtocols, info);
5244   outs() << "                    ivars " << format("0x%" PRIx32, cro.ivars)
5245          << "\n";
5246   if (cro.ivars != 0)
5247     print_ivar_list32_t(cro.ivars, info);
5248   outs() << "           weakIvarLayout "
5249          << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
5250   print_layout_map32(cro.weakIvarLayout, info);
5251   outs() << "           baseProperties "
5252          << format("0x%" PRIx32, cro.baseProperties) << "\n";
5253   if (cro.baseProperties != 0)
5254     print_objc_property_list32(cro.baseProperties, info);
5255   is_meta_class = (cro.flags & RO_META) != 0;
5256   return true;
5257 }
5258 
5259 static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
5260   struct class64_t c;
5261   const char *r;
5262   uint32_t offset, left;
5263   SectionRef S;
5264   const char *name;
5265   uint64_t isa_n_value, n_value;
5266 
5267   r = get_pointer_64(p, offset, left, S, info);
5268   if (r == nullptr || left < sizeof(struct class64_t))
5269     return;
5270   memcpy(&c, r, sizeof(struct class64_t));
5271   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5272     swapStruct(c);
5273 
5274   outs() << "           isa " << format("0x%" PRIx64, c.isa);
5275   name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
5276                        isa_n_value, c.isa);
5277   if (name != nullptr)
5278     outs() << " " << name;
5279   outs() << "\n";
5280 
5281   outs() << "    superclass " << format("0x%" PRIx64, c.superclass);
5282   name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
5283                        n_value, c.superclass);
5284   if (name != nullptr)
5285     outs() << " " << name;
5286   else {
5287     name = get_dyld_bind_info_symbolname(S.getAddress() +
5288              offset + offsetof(struct class64_t, superclass), info);
5289     if (name != nullptr)
5290       outs() << " " << name;
5291   }
5292   outs() << "\n";
5293 
5294   outs() << "         cache " << format("0x%" PRIx64, c.cache);
5295   name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
5296                        n_value, c.cache);
5297   if (name != nullptr)
5298     outs() << " " << name;
5299   outs() << "\n";
5300 
5301   outs() << "        vtable " << format("0x%" PRIx64, c.vtable);
5302   name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
5303                        n_value, c.vtable);
5304   if (name != nullptr)
5305     outs() << " " << name;
5306   outs() << "\n";
5307 
5308   name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
5309                        n_value, c.data);
5310   outs() << "          data ";
5311   if (n_value != 0) {
5312     if (info->verbose && name != nullptr)
5313       outs() << name;
5314     else
5315       outs() << format("0x%" PRIx64, n_value);
5316     if (c.data != 0)
5317       outs() << " + " << format("0x%" PRIx64, c.data);
5318   } else
5319     outs() << format("0x%" PRIx64, c.data);
5320   outs() << " (struct class_ro_t *)";
5321 
5322   // This is a Swift class if some of the low bits of the pointer are set.
5323   if ((c.data + n_value) & 0x7)
5324     outs() << " Swift class";
5325   outs() << "\n";
5326   bool is_meta_class;
5327   if (!print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class))
5328     return;
5329 
5330   if (!is_meta_class &&
5331       c.isa + isa_n_value != p &&
5332       c.isa + isa_n_value != 0 &&
5333       info->depth < 100) {
5334       info->depth++;
5335       outs() << "Meta Class\n";
5336       print_class64_t(c.isa + isa_n_value, info);
5337   }
5338 }
5339 
5340 static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
5341   struct class32_t c;
5342   const char *r;
5343   uint32_t offset, left;
5344   SectionRef S;
5345   const char *name;
5346 
5347   r = get_pointer_32(p, offset, left, S, info);
5348   if (r == nullptr)
5349     return;
5350   memset(&c, '\0', sizeof(struct class32_t));
5351   if (left < sizeof(struct class32_t)) {
5352     memcpy(&c, r, left);
5353     outs() << "   (class_t entends past the end of the section)\n";
5354   } else
5355     memcpy(&c, r, sizeof(struct class32_t));
5356   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5357     swapStruct(c);
5358 
5359   outs() << "           isa " << format("0x%" PRIx32, c.isa);
5360   name =
5361       get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
5362   if (name != nullptr)
5363     outs() << " " << name;
5364   outs() << "\n";
5365 
5366   outs() << "    superclass " << format("0x%" PRIx32, c.superclass);
5367   name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
5368                        c.superclass);
5369   if (name != nullptr)
5370     outs() << " " << name;
5371   outs() << "\n";
5372 
5373   outs() << "         cache " << format("0x%" PRIx32, c.cache);
5374   name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
5375                        c.cache);
5376   if (name != nullptr)
5377     outs() << " " << name;
5378   outs() << "\n";
5379 
5380   outs() << "        vtable " << format("0x%" PRIx32, c.vtable);
5381   name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
5382                        c.vtable);
5383   if (name != nullptr)
5384     outs() << " " << name;
5385   outs() << "\n";
5386 
5387   name =
5388       get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
5389   outs() << "          data " << format("0x%" PRIx32, c.data)
5390          << " (struct class_ro_t *)";
5391 
5392   // This is a Swift class if some of the low bits of the pointer are set.
5393   if (c.data & 0x3)
5394     outs() << " Swift class";
5395   outs() << "\n";
5396   bool is_meta_class;
5397   if (!print_class_ro32_t(c.data & ~0x3, info, is_meta_class))
5398     return;
5399 
5400   if (!is_meta_class) {
5401     outs() << "Meta Class\n";
5402     print_class32_t(c.isa, info);
5403   }
5404 }
5405 
5406 static void print_objc_class_t(struct objc_class_t *objc_class,
5407                                struct DisassembleInfo *info) {
5408   uint32_t offset, left, xleft;
5409   const char *name, *p, *ivar_list;
5410   SectionRef S;
5411   int32_t i;
5412   struct objc_ivar_list_t objc_ivar_list;
5413   struct objc_ivar_t ivar;
5414 
5415   outs() << "\t\t      isa " << format("0x%08" PRIx32, objc_class->isa);
5416   if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
5417     name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
5418     if (name != nullptr)
5419       outs() << format(" %.*s", left, name);
5420     else
5421       outs() << " (not in an __OBJC section)";
5422   }
5423   outs() << "\n";
5424 
5425   outs() << "\t      super_class "
5426          << format("0x%08" PRIx32, objc_class->super_class);
5427   if (info->verbose) {
5428     name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
5429     if (name != nullptr)
5430       outs() << format(" %.*s", left, name);
5431     else
5432       outs() << " (not in an __OBJC section)";
5433   }
5434   outs() << "\n";
5435 
5436   outs() << "\t\t     name " << format("0x%08" PRIx32, objc_class->name);
5437   if (info->verbose) {
5438     name = get_pointer_32(objc_class->name, offset, left, S, info, true);
5439     if (name != nullptr)
5440       outs() << format(" %.*s", left, name);
5441     else
5442       outs() << " (not in an __OBJC section)";
5443   }
5444   outs() << "\n";
5445 
5446   outs() << "\t\t  version " << format("0x%08" PRIx32, objc_class->version)
5447          << "\n";
5448 
5449   outs() << "\t\t     info " << format("0x%08" PRIx32, objc_class->info);
5450   if (info->verbose) {
5451     if (CLS_GETINFO(objc_class, CLS_CLASS))
5452       outs() << " CLS_CLASS";
5453     else if (CLS_GETINFO(objc_class, CLS_META))
5454       outs() << " CLS_META";
5455   }
5456   outs() << "\n";
5457 
5458   outs() << "\t    instance_size "
5459          << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
5460 
5461   p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
5462   outs() << "\t\t    ivars " << format("0x%08" PRIx32, objc_class->ivars);
5463   if (p != nullptr) {
5464     if (left > sizeof(struct objc_ivar_list_t)) {
5465       outs() << "\n";
5466       memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
5467     } else {
5468       outs() << " (entends past the end of the section)\n";
5469       memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
5470       memcpy(&objc_ivar_list, p, left);
5471     }
5472     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5473       swapStruct(objc_ivar_list);
5474     outs() << "\t\t       ivar_count " << objc_ivar_list.ivar_count << "\n";
5475     ivar_list = p + sizeof(struct objc_ivar_list_t);
5476     for (i = 0; i < objc_ivar_list.ivar_count; i++) {
5477       if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
5478         outs() << "\t\t remaining ivar's extend past the of the section\n";
5479         break;
5480       }
5481       memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
5482              sizeof(struct objc_ivar_t));
5483       if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5484         swapStruct(ivar);
5485 
5486       outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
5487       if (info->verbose) {
5488         name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
5489         if (name != nullptr)
5490           outs() << format(" %.*s", xleft, name);
5491         else
5492           outs() << " (not in an __OBJC section)";
5493       }
5494       outs() << "\n";
5495 
5496       outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
5497       if (info->verbose) {
5498         name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
5499         if (name != nullptr)
5500           outs() << format(" %.*s", xleft, name);
5501         else
5502           outs() << " (not in an __OBJC section)";
5503       }
5504       outs() << "\n";
5505 
5506       outs() << "\t\t      ivar_offset "
5507              << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
5508     }
5509   } else {
5510     outs() << " (not in an __OBJC section)\n";
5511   }
5512 
5513   outs() << "\t\t  methods " << format("0x%08" PRIx32, objc_class->methodLists);
5514   if (print_method_list(objc_class->methodLists, info))
5515     outs() << " (not in an __OBJC section)\n";
5516 
5517   outs() << "\t\t    cache " << format("0x%08" PRIx32, objc_class->cache)
5518          << "\n";
5519 
5520   outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
5521   if (print_protocol_list(objc_class->protocols, 16, info))
5522     outs() << " (not in an __OBJC section)\n";
5523 }
5524 
5525 static void print_objc_objc_category_t(struct objc_category_t *objc_category,
5526                                        struct DisassembleInfo *info) {
5527   uint32_t offset, left;
5528   const char *name;
5529   SectionRef S;
5530 
5531   outs() << "\t       category name "
5532          << format("0x%08" PRIx32, objc_category->category_name);
5533   if (info->verbose) {
5534     name = get_pointer_32(objc_category->category_name, offset, left, S, info,
5535                           true);
5536     if (name != nullptr)
5537       outs() << format(" %.*s", left, name);
5538     else
5539       outs() << " (not in an __OBJC section)";
5540   }
5541   outs() << "\n";
5542 
5543   outs() << "\t\t  class name "
5544          << format("0x%08" PRIx32, objc_category->class_name);
5545   if (info->verbose) {
5546     name =
5547         get_pointer_32(objc_category->class_name, offset, left, S, info, true);
5548     if (name != nullptr)
5549       outs() << format(" %.*s", left, name);
5550     else
5551       outs() << " (not in an __OBJC section)";
5552   }
5553   outs() << "\n";
5554 
5555   outs() << "\t    instance methods "
5556          << format("0x%08" PRIx32, objc_category->instance_methods);
5557   if (print_method_list(objc_category->instance_methods, info))
5558     outs() << " (not in an __OBJC section)\n";
5559 
5560   outs() << "\t       class methods "
5561          << format("0x%08" PRIx32, objc_category->class_methods);
5562   if (print_method_list(objc_category->class_methods, info))
5563     outs() << " (not in an __OBJC section)\n";
5564 }
5565 
5566 static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
5567   struct category64_t c;
5568   const char *r;
5569   uint32_t offset, xoffset, left;
5570   SectionRef S, xS;
5571   const char *name, *sym_name;
5572   uint64_t n_value;
5573 
5574   r = get_pointer_64(p, offset, left, S, info);
5575   if (r == nullptr)
5576     return;
5577   memset(&c, '\0', sizeof(struct category64_t));
5578   if (left < sizeof(struct category64_t)) {
5579     memcpy(&c, r, left);
5580     outs() << "   (category_t entends past the end of the section)\n";
5581   } else
5582     memcpy(&c, r, sizeof(struct category64_t));
5583   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5584     swapStruct(c);
5585 
5586   outs() << "              name ";
5587   sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
5588                            info, n_value, c.name);
5589   if (n_value != 0) {
5590     if (info->verbose && sym_name != nullptr)
5591       outs() << sym_name;
5592     else
5593       outs() << format("0x%" PRIx64, n_value);
5594     if (c.name != 0)
5595       outs() << " + " << format("0x%" PRIx64, c.name);
5596   } else
5597     outs() << format("0x%" PRIx64, c.name);
5598   name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
5599   if (name != nullptr)
5600     outs() << format(" %.*s", left, name);
5601   outs() << "\n";
5602 
5603   outs() << "               cls ";
5604   sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
5605                            n_value, c.cls);
5606   if (n_value != 0) {
5607     if (info->verbose && sym_name != nullptr)
5608       outs() << sym_name;
5609     else
5610       outs() << format("0x%" PRIx64, n_value);
5611     if (c.cls != 0)
5612       outs() << " + " << format("0x%" PRIx64, c.cls);
5613   } else
5614     outs() << format("0x%" PRIx64, c.cls);
5615   outs() << "\n";
5616   if (c.cls + n_value != 0)
5617     print_class64_t(c.cls + n_value, info);
5618 
5619   outs() << "   instanceMethods ";
5620   sym_name =
5621       get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
5622                     info, n_value, c.instanceMethods);
5623   if (n_value != 0) {
5624     if (info->verbose && sym_name != nullptr)
5625       outs() << sym_name;
5626     else
5627       outs() << format("0x%" PRIx64, n_value);
5628     if (c.instanceMethods != 0)
5629       outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
5630   } else
5631     outs() << format("0x%" PRIx64, c.instanceMethods);
5632   outs() << "\n";
5633   if (c.instanceMethods + n_value != 0)
5634     print_method_list64_t(c.instanceMethods + n_value, info, "");
5635 
5636   outs() << "      classMethods ";
5637   sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
5638                            S, info, n_value, c.classMethods);
5639   if (n_value != 0) {
5640     if (info->verbose && sym_name != nullptr)
5641       outs() << sym_name;
5642     else
5643       outs() << format("0x%" PRIx64, n_value);
5644     if (c.classMethods != 0)
5645       outs() << " + " << format("0x%" PRIx64, c.classMethods);
5646   } else
5647     outs() << format("0x%" PRIx64, c.classMethods);
5648   outs() << "\n";
5649   if (c.classMethods + n_value != 0)
5650     print_method_list64_t(c.classMethods + n_value, info, "");
5651 
5652   outs() << "         protocols ";
5653   sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
5654                            info, n_value, c.protocols);
5655   if (n_value != 0) {
5656     if (info->verbose && sym_name != nullptr)
5657       outs() << sym_name;
5658     else
5659       outs() << format("0x%" PRIx64, n_value);
5660     if (c.protocols != 0)
5661       outs() << " + " << format("0x%" PRIx64, c.protocols);
5662   } else
5663     outs() << format("0x%" PRIx64, c.protocols);
5664   outs() << "\n";
5665   if (c.protocols + n_value != 0)
5666     print_protocol_list64_t(c.protocols + n_value, info);
5667 
5668   outs() << "instanceProperties ";
5669   sym_name =
5670       get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
5671                     S, info, n_value, c.instanceProperties);
5672   if (n_value != 0) {
5673     if (info->verbose && sym_name != nullptr)
5674       outs() << sym_name;
5675     else
5676       outs() << format("0x%" PRIx64, n_value);
5677     if (c.instanceProperties != 0)
5678       outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
5679   } else
5680     outs() << format("0x%" PRIx64, c.instanceProperties);
5681   outs() << "\n";
5682   if (c.instanceProperties + n_value != 0)
5683     print_objc_property_list64(c.instanceProperties + n_value, info);
5684 }
5685 
5686 static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
5687   struct category32_t c;
5688   const char *r;
5689   uint32_t offset, left;
5690   SectionRef S, xS;
5691   const char *name;
5692 
5693   r = get_pointer_32(p, offset, left, S, info);
5694   if (r == nullptr)
5695     return;
5696   memset(&c, '\0', sizeof(struct category32_t));
5697   if (left < sizeof(struct category32_t)) {
5698     memcpy(&c, r, left);
5699     outs() << "   (category_t entends past the end of the section)\n";
5700   } else
5701     memcpy(&c, r, sizeof(struct category32_t));
5702   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5703     swapStruct(c);
5704 
5705   outs() << "              name " << format("0x%" PRIx32, c.name);
5706   name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
5707                        c.name);
5708   if (name)
5709     outs() << " " << name;
5710   outs() << "\n";
5711 
5712   outs() << "               cls " << format("0x%" PRIx32, c.cls) << "\n";
5713   if (c.cls != 0)
5714     print_class32_t(c.cls, info);
5715   outs() << "   instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
5716          << "\n";
5717   if (c.instanceMethods != 0)
5718     print_method_list32_t(c.instanceMethods, info, "");
5719   outs() << "      classMethods " << format("0x%" PRIx32, c.classMethods)
5720          << "\n";
5721   if (c.classMethods != 0)
5722     print_method_list32_t(c.classMethods, info, "");
5723   outs() << "         protocols " << format("0x%" PRIx32, c.protocols) << "\n";
5724   if (c.protocols != 0)
5725     print_protocol_list32_t(c.protocols, info);
5726   outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
5727          << "\n";
5728   if (c.instanceProperties != 0)
5729     print_objc_property_list32(c.instanceProperties, info);
5730 }
5731 
5732 static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
5733   uint32_t i, left, offset, xoffset;
5734   uint64_t p, n_value;
5735   struct message_ref64 mr;
5736   const char *name, *sym_name;
5737   const char *r;
5738   SectionRef xS;
5739 
5740   if (S == SectionRef())
5741     return;
5742 
5743   StringRef SectName;
5744   S.getName(SectName);
5745   DataRefImpl Ref = S.getRawDataRefImpl();
5746   StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5747   outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5748   offset = 0;
5749   for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5750     p = S.getAddress() + i;
5751     r = get_pointer_64(p, offset, left, S, info);
5752     if (r == nullptr)
5753       return;
5754     memset(&mr, '\0', sizeof(struct message_ref64));
5755     if (left < sizeof(struct message_ref64)) {
5756       memcpy(&mr, r, left);
5757       outs() << "   (message_ref entends past the end of the section)\n";
5758     } else
5759       memcpy(&mr, r, sizeof(struct message_ref64));
5760     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5761       swapStruct(mr);
5762 
5763     outs() << "  imp ";
5764     name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
5765                          n_value, mr.imp);
5766     if (n_value != 0) {
5767       outs() << format("0x%" PRIx64, n_value) << " ";
5768       if (mr.imp != 0)
5769         outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
5770     } else
5771       outs() << format("0x%" PRIx64, mr.imp) << " ";
5772     if (name != nullptr)
5773       outs() << " " << name;
5774     outs() << "\n";
5775 
5776     outs() << "  sel ";
5777     sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
5778                              info, n_value, mr.sel);
5779     if (n_value != 0) {
5780       if (info->verbose && sym_name != nullptr)
5781         outs() << sym_name;
5782       else
5783         outs() << format("0x%" PRIx64, n_value);
5784       if (mr.sel != 0)
5785         outs() << " + " << format("0x%" PRIx64, mr.sel);
5786     } else
5787       outs() << format("0x%" PRIx64, mr.sel);
5788     name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
5789     if (name != nullptr)
5790       outs() << format(" %.*s", left, name);
5791     outs() << "\n";
5792 
5793     offset += sizeof(struct message_ref64);
5794   }
5795 }
5796 
5797 static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
5798   uint32_t i, left, offset, xoffset, p;
5799   struct message_ref32 mr;
5800   const char *name, *r;
5801   SectionRef xS;
5802 
5803   if (S == SectionRef())
5804     return;
5805 
5806   StringRef SectName;
5807   S.getName(SectName);
5808   DataRefImpl Ref = S.getRawDataRefImpl();
5809   StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5810   outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5811   offset = 0;
5812   for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5813     p = S.getAddress() + i;
5814     r = get_pointer_32(p, offset, left, S, info);
5815     if (r == nullptr)
5816       return;
5817     memset(&mr, '\0', sizeof(struct message_ref32));
5818     if (left < sizeof(struct message_ref32)) {
5819       memcpy(&mr, r, left);
5820       outs() << "   (message_ref entends past the end of the section)\n";
5821     } else
5822       memcpy(&mr, r, sizeof(struct message_ref32));
5823     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5824       swapStruct(mr);
5825 
5826     outs() << "  imp " << format("0x%" PRIx32, mr.imp);
5827     name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
5828                          mr.imp);
5829     if (name != nullptr)
5830       outs() << " " << name;
5831     outs() << "\n";
5832 
5833     outs() << "  sel " << format("0x%" PRIx32, mr.sel);
5834     name = get_pointer_32(mr.sel, xoffset, left, xS, info);
5835     if (name != nullptr)
5836       outs() << " " << name;
5837     outs() << "\n";
5838 
5839     offset += sizeof(struct message_ref32);
5840   }
5841 }
5842 
5843 static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
5844   uint32_t left, offset, swift_version;
5845   uint64_t p;
5846   struct objc_image_info64 o;
5847   const char *r;
5848 
5849   if (S == SectionRef())
5850     return;
5851 
5852   StringRef SectName;
5853   S.getName(SectName);
5854   DataRefImpl Ref = S.getRawDataRefImpl();
5855   StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5856   outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5857   p = S.getAddress();
5858   r = get_pointer_64(p, offset, left, S, info);
5859   if (r == nullptr)
5860     return;
5861   memset(&o, '\0', sizeof(struct objc_image_info64));
5862   if (left < sizeof(struct objc_image_info64)) {
5863     memcpy(&o, r, left);
5864     outs() << "   (objc_image_info entends past the end of the section)\n";
5865   } else
5866     memcpy(&o, r, sizeof(struct objc_image_info64));
5867   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5868     swapStruct(o);
5869   outs() << "  version " << o.version << "\n";
5870   outs() << "    flags " << format("0x%" PRIx32, o.flags);
5871   if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5872     outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5873   if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5874     outs() << " OBJC_IMAGE_SUPPORTS_GC";
5875   if (o.flags & OBJC_IMAGE_IS_SIMULATED)
5876     outs() << " OBJC_IMAGE_IS_SIMULATED";
5877   if (o.flags & OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES)
5878     outs() << " OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES";
5879   swift_version = (o.flags >> 8) & 0xff;
5880   if (swift_version != 0) {
5881     if (swift_version == 1)
5882       outs() << " Swift 1.0";
5883     else if (swift_version == 2)
5884       outs() << " Swift 1.1";
5885     else if(swift_version == 3)
5886       outs() << " Swift 2.0";
5887     else if(swift_version == 4)
5888       outs() << " Swift 3.0";
5889     else if(swift_version == 5)
5890       outs() << " Swift 4.0";
5891     else if(swift_version == 6)
5892       outs() << " Swift 4.1/Swift 4.2";
5893     else if(swift_version == 7)
5894       outs() << " Swift 5 or later";
5895     else
5896       outs() << " unknown future Swift version (" << swift_version << ")";
5897   }
5898   outs() << "\n";
5899 }
5900 
5901 static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5902   uint32_t left, offset, swift_version, p;
5903   struct objc_image_info32 o;
5904   const char *r;
5905 
5906   if (S == SectionRef())
5907     return;
5908 
5909   StringRef SectName;
5910   S.getName(SectName);
5911   DataRefImpl Ref = S.getRawDataRefImpl();
5912   StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5913   outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5914   p = S.getAddress();
5915   r = get_pointer_32(p, offset, left, S, info);
5916   if (r == nullptr)
5917     return;
5918   memset(&o, '\0', sizeof(struct objc_image_info32));
5919   if (left < sizeof(struct objc_image_info32)) {
5920     memcpy(&o, r, left);
5921     outs() << "   (objc_image_info entends past the end of the section)\n";
5922   } else
5923     memcpy(&o, r, sizeof(struct objc_image_info32));
5924   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5925     swapStruct(o);
5926   outs() << "  version " << o.version << "\n";
5927   outs() << "    flags " << format("0x%" PRIx32, o.flags);
5928   if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5929     outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5930   if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5931     outs() << " OBJC_IMAGE_SUPPORTS_GC";
5932   swift_version = (o.flags >> 8) & 0xff;
5933   if (swift_version != 0) {
5934     if (swift_version == 1)
5935       outs() << " Swift 1.0";
5936     else if (swift_version == 2)
5937       outs() << " Swift 1.1";
5938     else if(swift_version == 3)
5939       outs() << " Swift 2.0";
5940     else if(swift_version == 4)
5941       outs() << " Swift 3.0";
5942     else if(swift_version == 5)
5943       outs() << " Swift 4.0";
5944     else if(swift_version == 6)
5945       outs() << " Swift 4.1/Swift 4.2";
5946     else if(swift_version == 7)
5947       outs() << " Swift 5 or later";
5948     else
5949       outs() << " unknown future Swift version (" << swift_version << ")";
5950   }
5951   outs() << "\n";
5952 }
5953 
5954 static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5955   uint32_t left, offset, p;
5956   struct imageInfo_t o;
5957   const char *r;
5958 
5959   StringRef SectName;
5960   S.getName(SectName);
5961   DataRefImpl Ref = S.getRawDataRefImpl();
5962   StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5963   outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5964   p = S.getAddress();
5965   r = get_pointer_32(p, offset, left, S, info);
5966   if (r == nullptr)
5967     return;
5968   memset(&o, '\0', sizeof(struct imageInfo_t));
5969   if (left < sizeof(struct imageInfo_t)) {
5970     memcpy(&o, r, left);
5971     outs() << " (imageInfo entends past the end of the section)\n";
5972   } else
5973     memcpy(&o, r, sizeof(struct imageInfo_t));
5974   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5975     swapStruct(o);
5976   outs() << "  version " << o.version << "\n";
5977   outs() << "    flags " << format("0x%" PRIx32, o.flags);
5978   if (o.flags & 0x1)
5979     outs() << "  F&C";
5980   if (o.flags & 0x2)
5981     outs() << " GC";
5982   if (o.flags & 0x4)
5983     outs() << " GC-only";
5984   else
5985     outs() << " RR";
5986   outs() << "\n";
5987 }
5988 
5989 static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
5990   SymbolAddressMap AddrMap;
5991   if (verbose)
5992     CreateSymbolAddressMap(O, &AddrMap);
5993 
5994   std::vector<SectionRef> Sections;
5995   for (const SectionRef &Section : O->sections()) {
5996     StringRef SectName;
5997     Section.getName(SectName);
5998     Sections.push_back(Section);
5999   }
6000 
6001   struct DisassembleInfo info(O, &AddrMap, &Sections, verbose);
6002 
6003   SectionRef CL = get_section(O, "__OBJC2", "__class_list");
6004   if (CL == SectionRef())
6005     CL = get_section(O, "__DATA", "__objc_classlist");
6006   if (CL == SectionRef())
6007     CL = get_section(O, "__DATA_CONST", "__objc_classlist");
6008   if (CL == SectionRef())
6009     CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
6010   info.S = CL;
6011   walk_pointer_list_64("class", CL, O, &info, print_class64_t);
6012 
6013   SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
6014   if (CR == SectionRef())
6015     CR = get_section(O, "__DATA", "__objc_classrefs");
6016   if (CR == SectionRef())
6017     CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
6018   if (CR == SectionRef())
6019     CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
6020   info.S = CR;
6021   walk_pointer_list_64("class refs", CR, O, &info, nullptr);
6022 
6023   SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
6024   if (SR == SectionRef())
6025     SR = get_section(O, "__DATA", "__objc_superrefs");
6026   if (SR == SectionRef())
6027     SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
6028   if (SR == SectionRef())
6029     SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
6030   info.S = SR;
6031   walk_pointer_list_64("super refs", SR, O, &info, nullptr);
6032 
6033   SectionRef CA = get_section(O, "__OBJC2", "__category_list");
6034   if (CA == SectionRef())
6035     CA = get_section(O, "__DATA", "__objc_catlist");
6036   if (CA == SectionRef())
6037     CA = get_section(O, "__DATA_CONST", "__objc_catlist");
6038   if (CA == SectionRef())
6039     CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
6040   info.S = CA;
6041   walk_pointer_list_64("category", CA, O, &info, print_category64_t);
6042 
6043   SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
6044   if (PL == SectionRef())
6045     PL = get_section(O, "__DATA", "__objc_protolist");
6046   if (PL == SectionRef())
6047     PL = get_section(O, "__DATA_CONST", "__objc_protolist");
6048   if (PL == SectionRef())
6049     PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
6050   info.S = PL;
6051   walk_pointer_list_64("protocol", PL, O, &info, nullptr);
6052 
6053   SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
6054   if (MR == SectionRef())
6055     MR = get_section(O, "__DATA", "__objc_msgrefs");
6056   if (MR == SectionRef())
6057     MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
6058   if (MR == SectionRef())
6059     MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
6060   info.S = MR;
6061   print_message_refs64(MR, &info);
6062 
6063   SectionRef II = get_section(O, "__OBJC2", "__image_info");
6064   if (II == SectionRef())
6065     II = get_section(O, "__DATA", "__objc_imageinfo");
6066   if (II == SectionRef())
6067     II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
6068   if (II == SectionRef())
6069     II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
6070   info.S = II;
6071   print_image_info64(II, &info);
6072 }
6073 
6074 static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
6075   SymbolAddressMap AddrMap;
6076   if (verbose)
6077     CreateSymbolAddressMap(O, &AddrMap);
6078 
6079   std::vector<SectionRef> Sections;
6080   for (const SectionRef &Section : O->sections()) {
6081     StringRef SectName;
6082     Section.getName(SectName);
6083     Sections.push_back(Section);
6084   }
6085 
6086   struct DisassembleInfo info(O, &AddrMap, &Sections, verbose);
6087 
6088   SectionRef CL = get_section(O, "__OBJC2", "__class_list");
6089   if (CL == SectionRef())
6090     CL = get_section(O, "__DATA", "__objc_classlist");
6091   if (CL == SectionRef())
6092     CL = get_section(O, "__DATA_CONST", "__objc_classlist");
6093   if (CL == SectionRef())
6094     CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
6095   info.S = CL;
6096   walk_pointer_list_32("class", CL, O, &info, print_class32_t);
6097 
6098   SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
6099   if (CR == SectionRef())
6100     CR = get_section(O, "__DATA", "__objc_classrefs");
6101   if (CR == SectionRef())
6102     CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
6103   if (CR == SectionRef())
6104     CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
6105   info.S = CR;
6106   walk_pointer_list_32("class refs", CR, O, &info, nullptr);
6107 
6108   SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
6109   if (SR == SectionRef())
6110     SR = get_section(O, "__DATA", "__objc_superrefs");
6111   if (SR == SectionRef())
6112     SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
6113   if (SR == SectionRef())
6114     SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
6115   info.S = SR;
6116   walk_pointer_list_32("super refs", SR, O, &info, nullptr);
6117 
6118   SectionRef CA = get_section(O, "__OBJC2", "__category_list");
6119   if (CA == SectionRef())
6120     CA = get_section(O, "__DATA", "__objc_catlist");
6121   if (CA == SectionRef())
6122     CA = get_section(O, "__DATA_CONST", "__objc_catlist");
6123   if (CA == SectionRef())
6124     CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
6125   info.S = CA;
6126   walk_pointer_list_32("category", CA, O, &info, print_category32_t);
6127 
6128   SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
6129   if (PL == SectionRef())
6130     PL = get_section(O, "__DATA", "__objc_protolist");
6131   if (PL == SectionRef())
6132     PL = get_section(O, "__DATA_CONST", "__objc_protolist");
6133   if (PL == SectionRef())
6134     PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
6135   info.S = PL;
6136   walk_pointer_list_32("protocol", PL, O, &info, nullptr);
6137 
6138   SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
6139   if (MR == SectionRef())
6140     MR = get_section(O, "__DATA", "__objc_msgrefs");
6141   if (MR == SectionRef())
6142     MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
6143   if (MR == SectionRef())
6144     MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
6145   info.S = MR;
6146   print_message_refs32(MR, &info);
6147 
6148   SectionRef II = get_section(O, "__OBJC2", "__image_info");
6149   if (II == SectionRef())
6150     II = get_section(O, "__DATA", "__objc_imageinfo");
6151   if (II == SectionRef())
6152     II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
6153   if (II == SectionRef())
6154     II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
6155   info.S = II;
6156   print_image_info32(II, &info);
6157 }
6158 
6159 static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
6160   uint32_t i, j, p, offset, xoffset, left, defs_left, def;
6161   const char *r, *name, *defs;
6162   struct objc_module_t module;
6163   SectionRef S, xS;
6164   struct objc_symtab_t symtab;
6165   struct objc_class_t objc_class;
6166   struct objc_category_t objc_category;
6167 
6168   outs() << "Objective-C segment\n";
6169   S = get_section(O, "__OBJC", "__module_info");
6170   if (S == SectionRef())
6171     return false;
6172 
6173   SymbolAddressMap AddrMap;
6174   if (verbose)
6175     CreateSymbolAddressMap(O, &AddrMap);
6176 
6177   std::vector<SectionRef> Sections;
6178   for (const SectionRef &Section : O->sections()) {
6179     StringRef SectName;
6180     Section.getName(SectName);
6181     Sections.push_back(Section);
6182   }
6183 
6184   struct DisassembleInfo info(O, &AddrMap, &Sections, verbose);
6185 
6186   for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
6187     p = S.getAddress() + i;
6188     r = get_pointer_32(p, offset, left, S, &info, true);
6189     if (r == nullptr)
6190       return true;
6191     memset(&module, '\0', sizeof(struct objc_module_t));
6192     if (left < sizeof(struct objc_module_t)) {
6193       memcpy(&module, r, left);
6194       outs() << "   (module extends past end of __module_info section)\n";
6195     } else
6196       memcpy(&module, r, sizeof(struct objc_module_t));
6197     if (O->isLittleEndian() != sys::IsLittleEndianHost)
6198       swapStruct(module);
6199 
6200     outs() << "Module " << format("0x%" PRIx32, p) << "\n";
6201     outs() << "    version " << module.version << "\n";
6202     outs() << "       size " << module.size << "\n";
6203     outs() << "       name ";
6204     name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
6205     if (name != nullptr)
6206       outs() << format("%.*s", left, name);
6207     else
6208       outs() << format("0x%08" PRIx32, module.name)
6209              << "(not in an __OBJC section)";
6210     outs() << "\n";
6211 
6212     r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
6213     if (module.symtab == 0 || r == nullptr) {
6214       outs() << "     symtab " << format("0x%08" PRIx32, module.symtab)
6215              << " (not in an __OBJC section)\n";
6216       continue;
6217     }
6218     outs() << "     symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
6219     memset(&symtab, '\0', sizeof(struct objc_symtab_t));
6220     defs_left = 0;
6221     defs = nullptr;
6222     if (left < sizeof(struct objc_symtab_t)) {
6223       memcpy(&symtab, r, left);
6224       outs() << "\tsymtab extends past end of an __OBJC section)\n";
6225     } else {
6226       memcpy(&symtab, r, sizeof(struct objc_symtab_t));
6227       if (left > sizeof(struct objc_symtab_t)) {
6228         defs_left = left - sizeof(struct objc_symtab_t);
6229         defs = r + sizeof(struct objc_symtab_t);
6230       }
6231     }
6232     if (O->isLittleEndian() != sys::IsLittleEndianHost)
6233       swapStruct(symtab);
6234 
6235     outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
6236     r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
6237     outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
6238     if (r == nullptr)
6239       outs() << " (not in an __OBJC section)";
6240     outs() << "\n";
6241     outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
6242     outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
6243     if (symtab.cls_def_cnt > 0)
6244       outs() << "\tClass Definitions\n";
6245     for (j = 0; j < symtab.cls_def_cnt; j++) {
6246       if ((j + 1) * sizeof(uint32_t) > defs_left) {
6247         outs() << "\t(remaining class defs entries entends past the end of the "
6248                << "section)\n";
6249         break;
6250       }
6251       memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
6252       if (O->isLittleEndian() != sys::IsLittleEndianHost)
6253         sys::swapByteOrder(def);
6254 
6255       r = get_pointer_32(def, xoffset, left, xS, &info, true);
6256       outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
6257       if (r != nullptr) {
6258         if (left > sizeof(struct objc_class_t)) {
6259           outs() << "\n";
6260           memcpy(&objc_class, r, sizeof(struct objc_class_t));
6261         } else {
6262           outs() << " (entends past the end of the section)\n";
6263           memset(&objc_class, '\0', sizeof(struct objc_class_t));
6264           memcpy(&objc_class, r, left);
6265         }
6266         if (O->isLittleEndian() != sys::IsLittleEndianHost)
6267           swapStruct(objc_class);
6268         print_objc_class_t(&objc_class, &info);
6269       } else {
6270         outs() << "(not in an __OBJC section)\n";
6271       }
6272 
6273       if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
6274         outs() << "\tMeta Class";
6275         r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
6276         if (r != nullptr) {
6277           if (left > sizeof(struct objc_class_t)) {
6278             outs() << "\n";
6279             memcpy(&objc_class, r, sizeof(struct objc_class_t));
6280           } else {
6281             outs() << " (entends past the end of the section)\n";
6282             memset(&objc_class, '\0', sizeof(struct objc_class_t));
6283             memcpy(&objc_class, r, left);
6284           }
6285           if (O->isLittleEndian() != sys::IsLittleEndianHost)
6286             swapStruct(objc_class);
6287           print_objc_class_t(&objc_class, &info);
6288         } else {
6289           outs() << "(not in an __OBJC section)\n";
6290         }
6291       }
6292     }
6293     if (symtab.cat_def_cnt > 0)
6294       outs() << "\tCategory Definitions\n";
6295     for (j = 0; j < symtab.cat_def_cnt; j++) {
6296       if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
6297         outs() << "\t(remaining category defs entries entends past the end of "
6298                << "the section)\n";
6299         break;
6300       }
6301       memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
6302              sizeof(uint32_t));
6303       if (O->isLittleEndian() != sys::IsLittleEndianHost)
6304         sys::swapByteOrder(def);
6305 
6306       r = get_pointer_32(def, xoffset, left, xS, &info, true);
6307       outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
6308              << format("0x%08" PRIx32, def);
6309       if (r != nullptr) {
6310         if (left > sizeof(struct objc_category_t)) {
6311           outs() << "\n";
6312           memcpy(&objc_category, r, sizeof(struct objc_category_t));
6313         } else {
6314           outs() << " (entends past the end of the section)\n";
6315           memset(&objc_category, '\0', sizeof(struct objc_category_t));
6316           memcpy(&objc_category, r, left);
6317         }
6318         if (O->isLittleEndian() != sys::IsLittleEndianHost)
6319           swapStruct(objc_category);
6320         print_objc_objc_category_t(&objc_category, &info);
6321       } else {
6322         outs() << "(not in an __OBJC section)\n";
6323       }
6324     }
6325   }
6326   const SectionRef II = get_section(O, "__OBJC", "__image_info");
6327   if (II != SectionRef())
6328     print_image_info(II, &info);
6329 
6330   return true;
6331 }
6332 
6333 static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
6334                                 uint32_t size, uint32_t addr) {
6335   SymbolAddressMap AddrMap;
6336   CreateSymbolAddressMap(O, &AddrMap);
6337 
6338   std::vector<SectionRef> Sections;
6339   for (const SectionRef &Section : O->sections()) {
6340     StringRef SectName;
6341     Section.getName(SectName);
6342     Sections.push_back(Section);
6343   }
6344 
6345   struct DisassembleInfo info(O, &AddrMap, &Sections, true);
6346 
6347   const char *p;
6348   struct objc_protocol_t protocol;
6349   uint32_t left, paddr;
6350   for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
6351     memset(&protocol, '\0', sizeof(struct objc_protocol_t));
6352     left = size - (p - sect);
6353     if (left < sizeof(struct objc_protocol_t)) {
6354       outs() << "Protocol extends past end of __protocol section\n";
6355       memcpy(&protocol, p, left);
6356     } else
6357       memcpy(&protocol, p, sizeof(struct objc_protocol_t));
6358     if (O->isLittleEndian() != sys::IsLittleEndianHost)
6359       swapStruct(protocol);
6360     paddr = addr + (p - sect);
6361     outs() << "Protocol " << format("0x%" PRIx32, paddr);
6362     if (print_protocol(paddr, 0, &info))
6363       outs() << "(not in an __OBJC section)\n";
6364   }
6365 }
6366 
6367 #ifdef HAVE_LIBXAR
6368 inline void swapStruct(struct xar_header &xar) {
6369   sys::swapByteOrder(xar.magic);
6370   sys::swapByteOrder(xar.size);
6371   sys::swapByteOrder(xar.version);
6372   sys::swapByteOrder(xar.toc_length_compressed);
6373   sys::swapByteOrder(xar.toc_length_uncompressed);
6374   sys::swapByteOrder(xar.cksum_alg);
6375 }
6376 
6377 static void PrintModeVerbose(uint32_t mode) {
6378   switch(mode & S_IFMT){
6379   case S_IFDIR:
6380     outs() << "d";
6381     break;
6382   case S_IFCHR:
6383     outs() << "c";
6384     break;
6385   case S_IFBLK:
6386     outs() << "b";
6387     break;
6388   case S_IFREG:
6389     outs() << "-";
6390     break;
6391   case S_IFLNK:
6392     outs() << "l";
6393     break;
6394   case S_IFSOCK:
6395     outs() << "s";
6396     break;
6397   default:
6398     outs() << "?";
6399     break;
6400   }
6401 
6402   /* owner permissions */
6403   if(mode & S_IREAD)
6404     outs() << "r";
6405   else
6406     outs() << "-";
6407   if(mode & S_IWRITE)
6408     outs() << "w";
6409   else
6410     outs() << "-";
6411   if(mode & S_ISUID)
6412     outs() << "s";
6413   else if(mode & S_IEXEC)
6414     outs() << "x";
6415   else
6416     outs() << "-";
6417 
6418   /* group permissions */
6419   if(mode & (S_IREAD >> 3))
6420     outs() << "r";
6421   else
6422     outs() << "-";
6423   if(mode & (S_IWRITE >> 3))
6424     outs() << "w";
6425   else
6426     outs() << "-";
6427   if(mode & S_ISGID)
6428     outs() << "s";
6429   else if(mode & (S_IEXEC >> 3))
6430     outs() << "x";
6431   else
6432     outs() << "-";
6433 
6434   /* other permissions */
6435   if(mode & (S_IREAD >> 6))
6436     outs() << "r";
6437   else
6438     outs() << "-";
6439   if(mode & (S_IWRITE >> 6))
6440     outs() << "w";
6441   else
6442     outs() << "-";
6443   if(mode & S_ISVTX)
6444     outs() << "t";
6445   else if(mode & (S_IEXEC >> 6))
6446     outs() << "x";
6447   else
6448     outs() << "-";
6449 }
6450 
6451 static void PrintXarFilesSummary(const char *XarFilename, xar_t xar) {
6452   xar_file_t xf;
6453   const char *key, *type, *mode, *user, *group, *size, *mtime, *name, *m;
6454   char *endp;
6455   uint32_t mode_value;
6456 
6457   ScopedXarIter xi;
6458   if (!xi) {
6459     WithColor::error(errs(), "llvm-objdump")
6460         << "can't obtain an xar iterator for xar archive " << XarFilename
6461         << "\n";
6462     return;
6463   }
6464 
6465   // Go through the xar's files.
6466   for (xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)) {
6467     ScopedXarIter xp;
6468     if(!xp){
6469       WithColor::error(errs(), "llvm-objdump")
6470           << "can't obtain an xar iterator for xar archive " << XarFilename
6471           << "\n";
6472       return;
6473     }
6474     type = nullptr;
6475     mode = nullptr;
6476     user = nullptr;
6477     group = nullptr;
6478     size = nullptr;
6479     mtime = nullptr;
6480     name = nullptr;
6481     for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
6482       const char *val = nullptr;
6483       xar_prop_get(xf, key, &val);
6484 #if 0 // Useful for debugging.
6485       outs() << "key: " << key << " value: " << val << "\n";
6486 #endif
6487       if(strcmp(key, "type") == 0)
6488         type = val;
6489       if(strcmp(key, "mode") == 0)
6490         mode = val;
6491       if(strcmp(key, "user") == 0)
6492         user = val;
6493       if(strcmp(key, "group") == 0)
6494         group = val;
6495       if(strcmp(key, "data/size") == 0)
6496         size = val;
6497       if(strcmp(key, "mtime") == 0)
6498         mtime = val;
6499       if(strcmp(key, "name") == 0)
6500         name = val;
6501     }
6502     if(mode != nullptr){
6503       mode_value = strtoul(mode, &endp, 8);
6504       if(*endp != '\0')
6505         outs() << "(mode: \"" << mode << "\" contains non-octal chars) ";
6506       if(strcmp(type, "file") == 0)
6507         mode_value |= S_IFREG;
6508       PrintModeVerbose(mode_value);
6509       outs() << " ";
6510     }
6511     if(user != nullptr)
6512       outs() << format("%10s/", user);
6513     if(group != nullptr)
6514       outs() << format("%-10s ", group);
6515     if(size != nullptr)
6516       outs() << format("%7s ", size);
6517     if(mtime != nullptr){
6518       for(m = mtime; *m != 'T' && *m != '\0'; m++)
6519         outs() << *m;
6520       if(*m == 'T')
6521         m++;
6522       outs() << " ";
6523       for( ; *m != 'Z' && *m != '\0'; m++)
6524         outs() << *m;
6525       outs() << " ";
6526     }
6527     if(name != nullptr)
6528       outs() << name;
6529     outs() << "\n";
6530   }
6531 }
6532 
6533 static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
6534                                 uint32_t size, bool verbose,
6535                                 bool PrintXarHeader, bool PrintXarFileHeaders,
6536                                 std::string XarMemberName) {
6537   if(size < sizeof(struct xar_header)) {
6538     outs() << "size of (__LLVM,__bundle) section too small (smaller than size "
6539               "of struct xar_header)\n";
6540     return;
6541   }
6542   struct xar_header XarHeader;
6543   memcpy(&XarHeader, sect, sizeof(struct xar_header));
6544   if (sys::IsLittleEndianHost)
6545     swapStruct(XarHeader);
6546   if (PrintXarHeader) {
6547     if (!XarMemberName.empty())
6548       outs() << "In xar member " << XarMemberName << ": ";
6549     else
6550       outs() << "For (__LLVM,__bundle) section: ";
6551     outs() << "xar header\n";
6552     if (XarHeader.magic == XAR_HEADER_MAGIC)
6553       outs() << "                  magic XAR_HEADER_MAGIC\n";
6554     else
6555       outs() << "                  magic "
6556              << format_hex(XarHeader.magic, 10, true)
6557              << " (not XAR_HEADER_MAGIC)\n";
6558     outs() << "                   size " << XarHeader.size << "\n";
6559     outs() << "                version " << XarHeader.version << "\n";
6560     outs() << "  toc_length_compressed " << XarHeader.toc_length_compressed
6561            << "\n";
6562     outs() << "toc_length_uncompressed " << XarHeader.toc_length_uncompressed
6563            << "\n";
6564     outs() << "              cksum_alg ";
6565     switch (XarHeader.cksum_alg) {
6566       case XAR_CKSUM_NONE:
6567         outs() << "XAR_CKSUM_NONE\n";
6568         break;
6569       case XAR_CKSUM_SHA1:
6570         outs() << "XAR_CKSUM_SHA1\n";
6571         break;
6572       case XAR_CKSUM_MD5:
6573         outs() << "XAR_CKSUM_MD5\n";
6574         break;
6575 #ifdef XAR_CKSUM_SHA256
6576       case XAR_CKSUM_SHA256:
6577         outs() << "XAR_CKSUM_SHA256\n";
6578         break;
6579 #endif
6580 #ifdef XAR_CKSUM_SHA512
6581       case XAR_CKSUM_SHA512:
6582         outs() << "XAR_CKSUM_SHA512\n";
6583         break;
6584 #endif
6585       default:
6586         outs() << XarHeader.cksum_alg << "\n";
6587     }
6588   }
6589 
6590   SmallString<128> XarFilename;
6591   int FD;
6592   std::error_code XarEC =
6593       sys::fs::createTemporaryFile("llvm-objdump", "xar", FD, XarFilename);
6594   if (XarEC) {
6595     WithColor::error(errs(), "llvm-objdump") << XarEC.message() << "\n";
6596     return;
6597   }
6598   ToolOutputFile XarFile(XarFilename, FD);
6599   raw_fd_ostream &XarOut = XarFile.os();
6600   StringRef XarContents(sect, size);
6601   XarOut << XarContents;
6602   XarOut.close();
6603   if (XarOut.has_error())
6604     return;
6605 
6606   ScopedXarFile xar(XarFilename.c_str(), READ);
6607   if (!xar) {
6608     WithColor::error(errs(), "llvm-objdump")
6609         << "can't create temporary xar archive " << XarFilename << "\n";
6610     return;
6611   }
6612 
6613   SmallString<128> TocFilename;
6614   std::error_code TocEC =
6615       sys::fs::createTemporaryFile("llvm-objdump", "toc", TocFilename);
6616   if (TocEC) {
6617     WithColor::error(errs(), "llvm-objdump") << TocEC.message() << "\n";
6618     return;
6619   }
6620   xar_serialize(xar, TocFilename.c_str());
6621 
6622   if (PrintXarFileHeaders) {
6623     if (!XarMemberName.empty())
6624       outs() << "In xar member " << XarMemberName << ": ";
6625     else
6626       outs() << "For (__LLVM,__bundle) section: ";
6627     outs() << "xar archive files:\n";
6628     PrintXarFilesSummary(XarFilename.c_str(), xar);
6629   }
6630 
6631   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
6632     MemoryBuffer::getFileOrSTDIN(TocFilename.c_str());
6633   if (std::error_code EC = FileOrErr.getError()) {
6634     WithColor::error(errs(), "llvm-objdump") << EC.message() << "\n";
6635     return;
6636   }
6637   std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
6638 
6639   if (!XarMemberName.empty())
6640     outs() << "In xar member " << XarMemberName << ": ";
6641   else
6642     outs() << "For (__LLVM,__bundle) section: ";
6643   outs() << "xar table of contents:\n";
6644   outs() << Buffer->getBuffer() << "\n";
6645 
6646   // TODO: Go through the xar's files.
6647   ScopedXarIter xi;
6648   if(!xi){
6649     WithColor::error(errs(), "llvm-objdump")
6650         << "can't obtain an xar iterator for xar archive "
6651         << XarFilename.c_str() << "\n";
6652     return;
6653   }
6654   for(xar_file_t xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)){
6655     const char *key;
6656     const char *member_name, *member_type, *member_size_string;
6657     size_t member_size;
6658 
6659     ScopedXarIter xp;
6660     if(!xp){
6661       WithColor::error(errs(), "llvm-objdump")
6662           << "can't obtain an xar iterator for xar archive "
6663           << XarFilename.c_str() << "\n";
6664       return;
6665     }
6666     member_name = NULL;
6667     member_type = NULL;
6668     member_size_string = NULL;
6669     for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
6670       const char *val = nullptr;
6671       xar_prop_get(xf, key, &val);
6672 #if 0 // Useful for debugging.
6673       outs() << "key: " << key << " value: " << val << "\n";
6674 #endif
6675       if (strcmp(key, "name") == 0)
6676         member_name = val;
6677       if (strcmp(key, "type") == 0)
6678         member_type = val;
6679       if (strcmp(key, "data/size") == 0)
6680         member_size_string = val;
6681     }
6682     /*
6683      * If we find a file with a name, date/size and type properties
6684      * and with the type being "file" see if that is a xar file.
6685      */
6686     if (member_name != NULL && member_type != NULL &&
6687         strcmp(member_type, "file") == 0 &&
6688         member_size_string != NULL){
6689       // Extract the file into a buffer.
6690       char *endptr;
6691       member_size = strtoul(member_size_string, &endptr, 10);
6692       if (*endptr == '\0' && member_size != 0) {
6693         char *buffer;
6694         if (xar_extract_tobuffersz(xar, xf, &buffer, &member_size) == 0) {
6695 #if 0 // Useful for debugging.
6696           outs() << "xar member: " << member_name << " extracted\n";
6697 #endif
6698           // Set the XarMemberName we want to see printed in the header.
6699           std::string OldXarMemberName;
6700           // If XarMemberName is already set this is nested. So
6701           // save the old name and create the nested name.
6702           if (!XarMemberName.empty()) {
6703             OldXarMemberName = XarMemberName;
6704             XarMemberName =
6705                 (Twine("[") + XarMemberName + "]" + member_name).str();
6706           } else {
6707             OldXarMemberName = "";
6708             XarMemberName = member_name;
6709           }
6710           // See if this is could be a xar file (nested).
6711           if (member_size >= sizeof(struct xar_header)) {
6712 #if 0 // Useful for debugging.
6713             outs() << "could be a xar file: " << member_name << "\n";
6714 #endif
6715             memcpy((char *)&XarHeader, buffer, sizeof(struct xar_header));
6716             if (sys::IsLittleEndianHost)
6717               swapStruct(XarHeader);
6718             if (XarHeader.magic == XAR_HEADER_MAGIC)
6719               DumpBitcodeSection(O, buffer, member_size, verbose,
6720                                  PrintXarHeader, PrintXarFileHeaders,
6721                                  XarMemberName);
6722           }
6723           XarMemberName = OldXarMemberName;
6724           delete buffer;
6725         }
6726       }
6727     }
6728   }
6729 }
6730 #endif // defined(HAVE_LIBXAR)
6731 
6732 static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
6733   if (O->is64Bit())
6734     printObjc2_64bit_MetaData(O, verbose);
6735   else {
6736     MachO::mach_header H;
6737     H = O->getHeader();
6738     if (H.cputype == MachO::CPU_TYPE_ARM)
6739       printObjc2_32bit_MetaData(O, verbose);
6740     else {
6741       // This is the 32-bit non-arm cputype case.  Which is normally
6742       // the first Objective-C ABI.  But it may be the case of a
6743       // binary for the iOS simulator which is the second Objective-C
6744       // ABI.  In that case printObjc1_32bit_MetaData() will determine that
6745       // and return false.
6746       if (!printObjc1_32bit_MetaData(O, verbose))
6747         printObjc2_32bit_MetaData(O, verbose);
6748     }
6749   }
6750 }
6751 
6752 // GuessLiteralPointer returns a string which for the item in the Mach-O file
6753 // for the address passed in as ReferenceValue for printing as a comment with
6754 // the instruction and also returns the corresponding type of that item
6755 // indirectly through ReferenceType.
6756 //
6757 // If ReferenceValue is an address of literal cstring then a pointer to the
6758 // cstring is returned and ReferenceType is set to
6759 // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
6760 //
6761 // If ReferenceValue is an address of an Objective-C CFString, Selector ref or
6762 // Class ref that name is returned and the ReferenceType is set accordingly.
6763 //
6764 // Lastly, literals which are Symbol address in a literal pool are looked for
6765 // and if found the symbol name is returned and ReferenceType is set to
6766 // LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
6767 //
6768 // If there is no item in the Mach-O file for the address passed in as
6769 // ReferenceValue nullptr is returned and ReferenceType is unchanged.
6770 static const char *GuessLiteralPointer(uint64_t ReferenceValue,
6771                                        uint64_t ReferencePC,
6772                                        uint64_t *ReferenceType,
6773                                        struct DisassembleInfo *info) {
6774   // First see if there is an external relocation entry at the ReferencePC.
6775   if (info->O->getHeader().filetype == MachO::MH_OBJECT) {
6776     uint64_t sect_addr = info->S.getAddress();
6777     uint64_t sect_offset = ReferencePC - sect_addr;
6778     bool reloc_found = false;
6779     DataRefImpl Rel;
6780     MachO::any_relocation_info RE;
6781     bool isExtern = false;
6782     SymbolRef Symbol;
6783     for (const RelocationRef &Reloc : info->S.relocations()) {
6784       uint64_t RelocOffset = Reloc.getOffset();
6785       if (RelocOffset == sect_offset) {
6786         Rel = Reloc.getRawDataRefImpl();
6787         RE = info->O->getRelocation(Rel);
6788         if (info->O->isRelocationScattered(RE))
6789           continue;
6790         isExtern = info->O->getPlainRelocationExternal(RE);
6791         if (isExtern) {
6792           symbol_iterator RelocSym = Reloc.getSymbol();
6793           Symbol = *RelocSym;
6794         }
6795         reloc_found = true;
6796         break;
6797       }
6798     }
6799     // If there is an external relocation entry for a symbol in a section
6800     // then used that symbol's value for the value of the reference.
6801     if (reloc_found && isExtern) {
6802       if (info->O->getAnyRelocationPCRel(RE)) {
6803         unsigned Type = info->O->getAnyRelocationType(RE);
6804         if (Type == MachO::X86_64_RELOC_SIGNED) {
6805           ReferenceValue = Symbol.getValue();
6806         }
6807       }
6808     }
6809   }
6810 
6811   // Look for literals such as Objective-C CFStrings refs, Selector refs,
6812   // Message refs and Class refs.
6813   bool classref, selref, msgref, cfstring;
6814   uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
6815                                                selref, msgref, cfstring);
6816   if (classref && pointer_value == 0) {
6817     // Note the ReferenceValue is a pointer into the __objc_classrefs section.
6818     // And the pointer_value in that section is typically zero as it will be
6819     // set by dyld as part of the "bind information".
6820     const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
6821     if (name != nullptr) {
6822       *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
6823       const char *class_name = strrchr(name, '$');
6824       if (class_name != nullptr && class_name[1] == '_' &&
6825           class_name[2] != '\0') {
6826         info->class_name = class_name + 2;
6827         return name;
6828       }
6829     }
6830   }
6831 
6832   if (classref) {
6833     *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
6834     const char *name =
6835         get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
6836     if (name != nullptr)
6837       info->class_name = name;
6838     else
6839       name = "bad class ref";
6840     return name;
6841   }
6842 
6843   if (cfstring) {
6844     *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
6845     const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
6846     return name;
6847   }
6848 
6849   if (selref && pointer_value == 0)
6850     pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
6851 
6852   if (pointer_value != 0)
6853     ReferenceValue = pointer_value;
6854 
6855   const char *name = GuessCstringPointer(ReferenceValue, info);
6856   if (name) {
6857     if (pointer_value != 0 && selref) {
6858       *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
6859       info->selector_name = name;
6860     } else if (pointer_value != 0 && msgref) {
6861       info->class_name = nullptr;
6862       *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
6863       info->selector_name = name;
6864     } else
6865       *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
6866     return name;
6867   }
6868 
6869   // Lastly look for an indirect symbol with this ReferenceValue which is in
6870   // a literal pool.  If found return that symbol name.
6871   name = GuessIndirectSymbol(ReferenceValue, info);
6872   if (name) {
6873     *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
6874     return name;
6875   }
6876 
6877   return nullptr;
6878 }
6879 
6880 // SymbolizerSymbolLookUp is the symbol lookup function passed when creating
6881 // the Symbolizer.  It looks up the ReferenceValue using the info passed via the
6882 // pointer to the struct DisassembleInfo that was passed when MCSymbolizer
6883 // is created and returns the symbol name that matches the ReferenceValue or
6884 // nullptr if none.  The ReferenceType is passed in for the IN type of
6885 // reference the instruction is making from the values in defined in the header
6886 // "llvm-c/Disassembler.h".  On return the ReferenceType can set to a specific
6887 // Out type and the ReferenceName will also be set which is added as a comment
6888 // to the disassembled instruction.
6889 //
6890 // If the symbol name is a C++ mangled name then the demangled name is
6891 // returned through ReferenceName and ReferenceType is set to
6892 // LLVMDisassembler_ReferenceType_DeMangled_Name .
6893 //
6894 // When this is called to get a symbol name for a branch target then the
6895 // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
6896 // SymbolValue will be looked for in the indirect symbol table to determine if
6897 // it is an address for a symbol stub.  If so then the symbol name for that
6898 // stub is returned indirectly through ReferenceName and then ReferenceType is
6899 // set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
6900 //
6901 // When this is called with an value loaded via a PC relative load then
6902 // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
6903 // SymbolValue is checked to be an address of literal pointer, symbol pointer,
6904 // or an Objective-C meta data reference.  If so the output ReferenceType is
6905 // set to correspond to that as well as setting the ReferenceName.
6906 static const char *SymbolizerSymbolLookUp(void *DisInfo,
6907                                           uint64_t ReferenceValue,
6908                                           uint64_t *ReferenceType,
6909                                           uint64_t ReferencePC,
6910                                           const char **ReferenceName) {
6911   struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
6912   // If no verbose symbolic information is wanted then just return nullptr.
6913   if (!info->verbose) {
6914     *ReferenceName = nullptr;
6915     *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6916     return nullptr;
6917   }
6918 
6919   const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
6920 
6921   if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
6922     *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
6923     if (*ReferenceName != nullptr) {
6924       method_reference(info, ReferenceType, ReferenceName);
6925       if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
6926         *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
6927     } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
6928       if (info->demangled_name != nullptr)
6929         free(info->demangled_name);
6930       int status;
6931       info->demangled_name =
6932           itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
6933       if (info->demangled_name != nullptr) {
6934         *ReferenceName = info->demangled_name;
6935         *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
6936       } else
6937         *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6938     } else
6939       *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6940   } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
6941     *ReferenceName =
6942         GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6943     if (*ReferenceName)
6944       method_reference(info, ReferenceType, ReferenceName);
6945     else
6946       *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6947     // If this is arm64 and the reference is an adrp instruction save the
6948     // instruction, passed in ReferenceValue and the address of the instruction
6949     // for use later if we see and add immediate instruction.
6950   } else if (info->O->getArch() == Triple::aarch64 &&
6951              *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
6952     info->adrp_inst = ReferenceValue;
6953     info->adrp_addr = ReferencePC;
6954     SymbolName = nullptr;
6955     *ReferenceName = nullptr;
6956     *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6957     // If this is arm64 and reference is an add immediate instruction and we
6958     // have
6959     // seen an adrp instruction just before it and the adrp's Xd register
6960     // matches
6961     // this add's Xn register reconstruct the value being referenced and look to
6962     // see if it is a literal pointer.  Note the add immediate instruction is
6963     // passed in ReferenceValue.
6964   } else if (info->O->getArch() == Triple::aarch64 &&
6965              *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
6966              ReferencePC - 4 == info->adrp_addr &&
6967              (info->adrp_inst & 0x9f000000) == 0x90000000 &&
6968              (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
6969     uint32_t addxri_inst;
6970     uint64_t adrp_imm, addxri_imm;
6971 
6972     adrp_imm =
6973         ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
6974     if (info->adrp_inst & 0x0200000)
6975       adrp_imm |= 0xfffffffffc000000LL;
6976 
6977     addxri_inst = ReferenceValue;
6978     addxri_imm = (addxri_inst >> 10) & 0xfff;
6979     if (((addxri_inst >> 22) & 0x3) == 1)
6980       addxri_imm <<= 12;
6981 
6982     ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
6983                      (adrp_imm << 12) + addxri_imm;
6984 
6985     *ReferenceName =
6986         GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6987     if (*ReferenceName == nullptr)
6988       *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6989     // If this is arm64 and the reference is a load register instruction and we
6990     // have seen an adrp instruction just before it and the adrp's Xd register
6991     // matches this add's Xn register reconstruct the value being referenced and
6992     // look to see if it is a literal pointer.  Note the load register
6993     // instruction is passed in ReferenceValue.
6994   } else if (info->O->getArch() == Triple::aarch64 &&
6995              *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
6996              ReferencePC - 4 == info->adrp_addr &&
6997              (info->adrp_inst & 0x9f000000) == 0x90000000 &&
6998              (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
6999     uint32_t ldrxui_inst;
7000     uint64_t adrp_imm, ldrxui_imm;
7001 
7002     adrp_imm =
7003         ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
7004     if (info->adrp_inst & 0x0200000)
7005       adrp_imm |= 0xfffffffffc000000LL;
7006 
7007     ldrxui_inst = ReferenceValue;
7008     ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
7009 
7010     ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
7011                      (adrp_imm << 12) + (ldrxui_imm << 3);
7012 
7013     *ReferenceName =
7014         GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
7015     if (*ReferenceName == nullptr)
7016       *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
7017   }
7018   // If this arm64 and is an load register (PC-relative) instruction the
7019   // ReferenceValue is the PC plus the immediate value.
7020   else if (info->O->getArch() == Triple::aarch64 &&
7021            (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
7022             *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
7023     *ReferenceName =
7024         GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
7025     if (*ReferenceName == nullptr)
7026       *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
7027   } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
7028     if (info->demangled_name != nullptr)
7029       free(info->demangled_name);
7030     int status;
7031     info->demangled_name =
7032         itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
7033     if (info->demangled_name != nullptr) {
7034       *ReferenceName = info->demangled_name;
7035       *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
7036     }
7037   }
7038   else {
7039     *ReferenceName = nullptr;
7040     *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
7041   }
7042 
7043   return SymbolName;
7044 }
7045 
7046 /// Emits the comments that are stored in the CommentStream.
7047 /// Each comment in the CommentStream must end with a newline.
7048 static void emitComments(raw_svector_ostream &CommentStream,
7049                          SmallString<128> &CommentsToEmit,
7050                          formatted_raw_ostream &FormattedOS,
7051                          const MCAsmInfo &MAI) {
7052   // Flush the stream before taking its content.
7053   StringRef Comments = CommentsToEmit.str();
7054   // Get the default information for printing a comment.
7055   StringRef CommentBegin = MAI.getCommentString();
7056   unsigned CommentColumn = MAI.getCommentColumn();
7057   bool IsFirst = true;
7058   while (!Comments.empty()) {
7059     if (!IsFirst)
7060       FormattedOS << '\n';
7061     // Emit a line of comments.
7062     FormattedOS.PadToColumn(CommentColumn);
7063     size_t Position = Comments.find('\n');
7064     FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
7065     // Move after the newline character.
7066     Comments = Comments.substr(Position + 1);
7067     IsFirst = false;
7068   }
7069   FormattedOS.flush();
7070 
7071   // Tell the comment stream that the vector changed underneath it.
7072   CommentsToEmit.clear();
7073 }
7074 
7075 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
7076                              StringRef DisSegName, StringRef DisSectName) {
7077   const char *McpuDefault = nullptr;
7078   const Target *ThumbTarget = nullptr;
7079   const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
7080   if (!TheTarget) {
7081     // GetTarget prints out stuff.
7082     return;
7083   }
7084   std::string MachOMCPU;
7085   if (MCPU.empty() && McpuDefault)
7086     MachOMCPU = McpuDefault;
7087   else
7088     MachOMCPU = MCPU;
7089 
7090   std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
7091   std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
7092   if (ThumbTarget)
7093     ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
7094 
7095   // Package up features to be passed to target/subtarget
7096   std::string FeaturesStr;
7097   if (!MAttrs.empty()) {
7098     SubtargetFeatures Features;
7099     for (unsigned i = 0; i != MAttrs.size(); ++i)
7100       Features.AddFeature(MAttrs[i]);
7101     FeaturesStr = Features.getString();
7102   }
7103 
7104   // Set up disassembler.
7105   std::unique_ptr<const MCRegisterInfo> MRI(
7106       TheTarget->createMCRegInfo(TripleName));
7107   std::unique_ptr<const MCAsmInfo> AsmInfo(
7108       TheTarget->createMCAsmInfo(*MRI, TripleName));
7109   std::unique_ptr<const MCSubtargetInfo> STI(
7110       TheTarget->createMCSubtargetInfo(TripleName, MachOMCPU, FeaturesStr));
7111   MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
7112   std::unique_ptr<MCDisassembler> DisAsm(
7113       TheTarget->createMCDisassembler(*STI, Ctx));
7114   std::unique_ptr<MCSymbolizer> Symbolizer;
7115   struct DisassembleInfo SymbolizerInfo(nullptr, nullptr, nullptr, false);
7116   std::unique_ptr<MCRelocationInfo> RelInfo(
7117       TheTarget->createMCRelocationInfo(TripleName, Ctx));
7118   if (RelInfo) {
7119     Symbolizer.reset(TheTarget->createMCSymbolizer(
7120         TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
7121         &SymbolizerInfo, &Ctx, std::move(RelInfo)));
7122     DisAsm->setSymbolizer(std::move(Symbolizer));
7123   }
7124   int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
7125   std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
7126       Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
7127   // Set the display preference for hex vs. decimal immediates.
7128   IP->setPrintImmHex(PrintImmHex);
7129   // Comment stream and backing vector.
7130   SmallString<128> CommentsToEmit;
7131   raw_svector_ostream CommentStream(CommentsToEmit);
7132   // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
7133   // if it is done then arm64 comments for string literals don't get printed
7134   // and some constant get printed instead and not setting it causes intel
7135   // (32-bit and 64-bit) comments printed with different spacing before the
7136   // comment causing different diffs with the 'C' disassembler library API.
7137   // IP->setCommentStream(CommentStream);
7138 
7139   if (!AsmInfo || !STI || !DisAsm || !IP) {
7140     WithColor::error(errs(), "llvm-objdump")
7141         << "couldn't initialize disassembler for target " << TripleName << '\n';
7142     return;
7143   }
7144 
7145   // Set up separate thumb disassembler if needed.
7146   std::unique_ptr<const MCRegisterInfo> ThumbMRI;
7147   std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
7148   std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
7149   std::unique_ptr<MCDisassembler> ThumbDisAsm;
7150   std::unique_ptr<MCInstPrinter> ThumbIP;
7151   std::unique_ptr<MCContext> ThumbCtx;
7152   std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
7153   struct DisassembleInfo ThumbSymbolizerInfo(nullptr, nullptr, nullptr, false);
7154   std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
7155   if (ThumbTarget) {
7156     ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
7157     ThumbAsmInfo.reset(
7158         ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
7159     ThumbSTI.reset(
7160         ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MachOMCPU,
7161                                            FeaturesStr));
7162     ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
7163     ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
7164     MCContext *PtrThumbCtx = ThumbCtx.get();
7165     ThumbRelInfo.reset(
7166         ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
7167     if (ThumbRelInfo) {
7168       ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
7169           ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
7170           &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
7171       ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
7172     }
7173     int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
7174     ThumbIP.reset(ThumbTarget->createMCInstPrinter(
7175         Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
7176         *ThumbInstrInfo, *ThumbMRI));
7177     // Set the display preference for hex vs. decimal immediates.
7178     ThumbIP->setPrintImmHex(PrintImmHex);
7179   }
7180 
7181   if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
7182     WithColor::error(errs(), "llvm-objdump")
7183         << "couldn't initialize disassembler for target " << ThumbTripleName
7184         << '\n';
7185     return;
7186   }
7187 
7188   MachO::mach_header Header = MachOOF->getHeader();
7189 
7190   // FIXME: Using the -cfg command line option, this code used to be able to
7191   // annotate relocations with the referenced symbol's name, and if this was
7192   // inside a __[cf]string section, the data it points to. This is now replaced
7193   // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
7194   std::vector<SectionRef> Sections;
7195   std::vector<SymbolRef> Symbols;
7196   SmallVector<uint64_t, 8> FoundFns;
7197   uint64_t BaseSegmentAddress;
7198 
7199   getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
7200                         BaseSegmentAddress);
7201 
7202   // Sort the symbols by address, just in case they didn't come in that way.
7203   llvm::sort(Symbols, SymbolSorter());
7204 
7205   // Build a data in code table that is sorted on by the address of each entry.
7206   uint64_t BaseAddress = 0;
7207   if (Header.filetype == MachO::MH_OBJECT)
7208     BaseAddress = Sections[0].getAddress();
7209   else
7210     BaseAddress = BaseSegmentAddress;
7211   DiceTable Dices;
7212   for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
7213        DI != DE; ++DI) {
7214     uint32_t Offset;
7215     DI->getOffset(Offset);
7216     Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
7217   }
7218   array_pod_sort(Dices.begin(), Dices.end());
7219 
7220 #ifndef NDEBUG
7221   raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
7222 #else
7223   raw_ostream &DebugOut = nulls();
7224 #endif
7225 
7226   // Try to find debug info and set up the DIContext for it.
7227   std::unique_ptr<DIContext> diContext;
7228   std::unique_ptr<Binary> DSYMBinary;
7229   std::unique_ptr<MemoryBuffer> DSYMBuf;
7230   if (UseDbg) {
7231     ObjectFile *DbgObj = MachOOF;
7232 
7233     // A separate DSym file path was specified, parse it as a macho file,
7234     // get the sections and supply it to the section name parsing machinery.
7235     if (!DSYMFile.empty()) {
7236       ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
7237           MemoryBuffer::getFileOrSTDIN(DSYMFile);
7238       if (std::error_code EC = BufOrErr.getError()) {
7239         report_error(errorCodeToError(EC), DSYMFile);
7240         return;
7241       }
7242 
7243       // We need to keep the file alive, because we're replacing DbgObj with it.
7244       DSYMBuf = std::move(BufOrErr.get());
7245 
7246       Expected<std::unique_ptr<Binary>> BinaryOrErr =
7247       createBinary(DSYMBuf.get()->getMemBufferRef());
7248       if (!BinaryOrErr) {
7249         report_error(BinaryOrErr.takeError(), DSYMFile);
7250         return;
7251       }
7252 
7253       // We need to keep the Binary elive with the buffer
7254       DSYMBinary = std::move(BinaryOrErr.get());
7255 
7256       if (ObjectFile *O = dyn_cast<ObjectFile>(DSYMBinary.get())) {
7257         // this is a Mach-O object file, use it
7258         if (MachOObjectFile *MachDSYM = dyn_cast<MachOObjectFile>(&*O)) {
7259           DbgObj = MachDSYM;
7260         }
7261         else {
7262           WithColor::error(errs(), "llvm-objdump")
7263             << DSYMFile << " is not a Mach-O file type.\n";
7264           return;
7265         }
7266       }
7267       else if (auto UB = dyn_cast<MachOUniversalBinary>(DSYMBinary.get())){
7268         // this is a Universal Binary, find a Mach-O for this architecture
7269         uint32_t CPUType, CPUSubType;
7270         const char *ArchFlag;
7271         if (MachOOF->is64Bit()) {
7272           const MachO::mach_header_64 H_64 = MachOOF->getHeader64();
7273           CPUType = H_64.cputype;
7274           CPUSubType = H_64.cpusubtype;
7275         } else {
7276           const MachO::mach_header H = MachOOF->getHeader();
7277           CPUType = H.cputype;
7278           CPUSubType = H.cpusubtype;
7279         }
7280         Triple T = MachOObjectFile::getArchTriple(CPUType, CPUSubType, nullptr,
7281                                                   &ArchFlag);
7282         Expected<std::unique_ptr<MachOObjectFile>> MachDSYM =
7283             UB->getObjectForArch(ArchFlag);
7284         if (!MachDSYM) {
7285           report_error(MachDSYM.takeError(), DSYMFile);
7286           return;
7287         }
7288 
7289         // We need to keep the Binary elive with the buffer
7290         DbgObj = &*MachDSYM.get();
7291         DSYMBinary = std::move(*MachDSYM);
7292       }
7293       else {
7294         WithColor::error(errs(), "llvm-objdump")
7295           << DSYMFile << " is not a Mach-O or Universal file type.\n";
7296         return;
7297       }
7298     }
7299 
7300     // Setup the DIContext
7301     diContext = DWARFContext::create(*DbgObj);
7302   }
7303 
7304   if (FilterSections.empty())
7305     outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
7306 
7307   for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
7308     StringRef SectName;
7309     if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
7310       continue;
7311 
7312     DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
7313 
7314     StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
7315     if (SegmentName != DisSegName)
7316       continue;
7317 
7318     StringRef BytesStr =
7319         unwrapOrError(Sections[SectIdx].getContents(), Filename);
7320     ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(BytesStr);
7321     uint64_t SectAddress = Sections[SectIdx].getAddress();
7322 
7323     bool symbolTableWorked = false;
7324 
7325     // Create a map of symbol addresses to symbol names for use by
7326     // the SymbolizerSymbolLookUp() routine.
7327     SymbolAddressMap AddrMap;
7328     bool DisSymNameFound = false;
7329     for (const SymbolRef &Symbol : MachOOF->symbols()) {
7330       SymbolRef::Type ST =
7331           unwrapOrError(Symbol.getType(), MachOOF->getFileName());
7332       if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
7333           ST == SymbolRef::ST_Other) {
7334         uint64_t Address = Symbol.getValue();
7335         StringRef SymName =
7336             unwrapOrError(Symbol.getName(), MachOOF->getFileName());
7337         AddrMap[Address] = SymName;
7338         if (!DisSymName.empty() && DisSymName == SymName)
7339           DisSymNameFound = true;
7340       }
7341     }
7342     if (!DisSymName.empty() && !DisSymNameFound) {
7343       outs() << "Can't find -dis-symname: " << DisSymName << "\n";
7344       return;
7345     }
7346     // Set up the block of info used by the Symbolizer call backs.
7347     SymbolizerInfo.verbose = !NoSymbolicOperands;
7348     SymbolizerInfo.O = MachOOF;
7349     SymbolizerInfo.S = Sections[SectIdx];
7350     SymbolizerInfo.AddrMap = &AddrMap;
7351     SymbolizerInfo.Sections = &Sections;
7352     // Same for the ThumbSymbolizer
7353     ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
7354     ThumbSymbolizerInfo.O = MachOOF;
7355     ThumbSymbolizerInfo.S = Sections[SectIdx];
7356     ThumbSymbolizerInfo.AddrMap = &AddrMap;
7357     ThumbSymbolizerInfo.Sections = &Sections;
7358 
7359     unsigned int Arch = MachOOF->getArch();
7360 
7361     // Skip all symbols if this is a stubs file.
7362     if (Bytes.empty())
7363       return;
7364 
7365     // If the section has symbols but no symbol at the start of the section
7366     // these are used to make sure the bytes before the first symbol are
7367     // disassembled.
7368     bool FirstSymbol = true;
7369     bool FirstSymbolAtSectionStart = true;
7370 
7371     // Disassemble symbol by symbol.
7372     for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
7373       StringRef SymName =
7374           unwrapOrError(Symbols[SymIdx].getName(), MachOOF->getFileName());
7375       SymbolRef::Type ST =
7376           unwrapOrError(Symbols[SymIdx].getType(), MachOOF->getFileName());
7377       if (ST != SymbolRef::ST_Function && ST != SymbolRef::ST_Data)
7378         continue;
7379 
7380       // Make sure the symbol is defined in this section.
7381       bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
7382       if (!containsSym) {
7383         if (!DisSymName.empty() && DisSymName == SymName) {
7384           outs() << "-dis-symname: " << DisSymName << " not in the section\n";
7385           return;
7386         }
7387         continue;
7388       }
7389       // The __mh_execute_header is special and we need to deal with that fact
7390       // this symbol is before the start of the (__TEXT,__text) section and at the
7391       // address of the start of the __TEXT segment.  This is because this symbol
7392       // is an N_SECT symbol in the (__TEXT,__text) but its address is before the
7393       // start of the section in a standard MH_EXECUTE filetype.
7394       if (!DisSymName.empty() && DisSymName == "__mh_execute_header") {
7395         outs() << "-dis-symname: __mh_execute_header not in any section\n";
7396         return;
7397       }
7398       // When this code is trying to disassemble a symbol at a time and in the
7399       // case there is only the __mh_execute_header symbol left as in a stripped
7400       // executable, we need to deal with this by ignoring this symbol so the
7401       // whole section is disassembled and this symbol is then not displayed.
7402       if (SymName == "__mh_execute_header" || SymName == "__mh_dylib_header" ||
7403           SymName == "__mh_bundle_header" || SymName == "__mh_object_header" ||
7404           SymName == "__mh_preload_header" || SymName == "__mh_dylinker_header")
7405         continue;
7406 
7407       // If we are only disassembling one symbol see if this is that symbol.
7408       if (!DisSymName.empty() && DisSymName != SymName)
7409         continue;
7410 
7411       // Start at the address of the symbol relative to the section's address.
7412       uint64_t SectSize = Sections[SectIdx].getSize();
7413       uint64_t Start = Symbols[SymIdx].getValue();
7414       uint64_t SectionAddress = Sections[SectIdx].getAddress();
7415       Start -= SectionAddress;
7416 
7417       if (Start > SectSize) {
7418         outs() << "section data ends, " << SymName
7419                << " lies outside valid range\n";
7420         return;
7421       }
7422 
7423       // Stop disassembling either at the beginning of the next symbol or at
7424       // the end of the section.
7425       bool containsNextSym = false;
7426       uint64_t NextSym = 0;
7427       uint64_t NextSymIdx = SymIdx + 1;
7428       while (Symbols.size() > NextSymIdx) {
7429         SymbolRef::Type NextSymType = unwrapOrError(
7430             Symbols[NextSymIdx].getType(), MachOOF->getFileName());
7431         if (NextSymType == SymbolRef::ST_Function) {
7432           containsNextSym =
7433               Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
7434           NextSym = Symbols[NextSymIdx].getValue();
7435           NextSym -= SectionAddress;
7436           break;
7437         }
7438         ++NextSymIdx;
7439       }
7440 
7441       uint64_t End = containsNextSym ? std::min(NextSym, SectSize) : SectSize;
7442       uint64_t Size;
7443 
7444       symbolTableWorked = true;
7445 
7446       DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
7447       bool IsThumb = MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb;
7448 
7449       // We only need the dedicated Thumb target if there's a real choice
7450       // (i.e. we're not targeting M-class) and the function is Thumb.
7451       bool UseThumbTarget = IsThumb && ThumbTarget;
7452 
7453       // If we are not specifying a symbol to start disassembly with and this
7454       // is the first symbol in the section but not at the start of the section
7455       // then move the disassembly index to the start of the section and
7456       // don't print the symbol name just yet.  This is so the bytes before the
7457       // first symbol are disassembled.
7458       uint64_t SymbolStart = Start;
7459       if (DisSymName.empty() && FirstSymbol && Start != 0) {
7460         FirstSymbolAtSectionStart = false;
7461         Start = 0;
7462       }
7463       else
7464         outs() << SymName << ":\n";
7465 
7466       DILineInfo lastLine;
7467       for (uint64_t Index = Start; Index < End; Index += Size) {
7468         MCInst Inst;
7469 
7470         // If this is the first symbol in the section and it was not at the
7471         // start of the section, see if we are at its Index now and if so print
7472         // the symbol name.
7473         if (FirstSymbol && !FirstSymbolAtSectionStart && Index == SymbolStart)
7474           outs() << SymName << ":\n";
7475 
7476         uint64_t PC = SectAddress + Index;
7477         if (!NoLeadingAddr) {
7478           if (FullLeadingAddr) {
7479             if (MachOOF->is64Bit())
7480               outs() << format("%016" PRIx64, PC);
7481             else
7482               outs() << format("%08" PRIx64, PC);
7483           } else {
7484             outs() << format("%8" PRIx64 ":", PC);
7485           }
7486         }
7487         if (!NoShowRawInsn || Arch == Triple::arm)
7488           outs() << "\t";
7489 
7490         // Check the data in code table here to see if this is data not an
7491         // instruction to be disassembled.
7492         DiceTable Dice;
7493         Dice.push_back(std::make_pair(PC, DiceRef()));
7494         dice_table_iterator DTI =
7495             std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
7496                         compareDiceTableEntries);
7497         if (DTI != Dices.end()) {
7498           uint16_t Length;
7499           DTI->second.getLength(Length);
7500           uint16_t Kind;
7501           DTI->second.getKind(Kind);
7502           Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
7503           if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
7504               (PC == (DTI->first + Length - 1)) && (Length & 1))
7505             Size++;
7506           continue;
7507         }
7508 
7509         SmallVector<char, 64> AnnotationsBytes;
7510         raw_svector_ostream Annotations(AnnotationsBytes);
7511 
7512         bool gotInst;
7513         if (UseThumbTarget)
7514           gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
7515                                                 PC, DebugOut, Annotations);
7516         else
7517           gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
7518                                            DebugOut, Annotations);
7519         if (gotInst) {
7520           if (!NoShowRawInsn || Arch == Triple::arm) {
7521             dumpBytes(makeArrayRef(Bytes.data() + Index, Size), outs());
7522           }
7523           formatted_raw_ostream FormattedOS(outs());
7524           StringRef AnnotationsStr = Annotations.str();
7525           if (UseThumbTarget)
7526             ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
7527           else
7528             IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
7529           emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
7530 
7531           // Print debug info.
7532           if (diContext) {
7533             DILineInfo dli = diContext->getLineInfoForAddress({PC, SectIdx});
7534             // Print valid line info if it changed.
7535             if (dli != lastLine && dli.Line != 0)
7536               outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
7537                      << dli.Column;
7538             lastLine = dli;
7539           }
7540           outs() << "\n";
7541         } else {
7542           unsigned int Arch = MachOOF->getArch();
7543           if (Arch == Triple::x86_64 || Arch == Triple::x86) {
7544             outs() << format("\t.byte 0x%02x #bad opcode\n",
7545                              *(Bytes.data() + Index) & 0xff);
7546             Size = 1; // skip exactly one illegible byte and move on.
7547           } else if (Arch == Triple::aarch64 ||
7548                      (Arch == Triple::arm && !IsThumb)) {
7549             uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
7550                               (*(Bytes.data() + Index + 1) & 0xff) << 8 |
7551                               (*(Bytes.data() + Index + 2) & 0xff) << 16 |
7552                               (*(Bytes.data() + Index + 3) & 0xff) << 24;
7553             outs() << format("\t.long\t0x%08x\n", opcode);
7554             Size = 4;
7555           } else if (Arch == Triple::arm) {
7556             assert(IsThumb && "ARM mode should have been dealt with above");
7557             uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
7558                               (*(Bytes.data() + Index + 1) & 0xff) << 8;
7559             outs() << format("\t.short\t0x%04x\n", opcode);
7560             Size = 2;
7561           } else{
7562             WithColor::warning(errs(), "llvm-objdump")
7563                 << "invalid instruction encoding\n";
7564             if (Size == 0)
7565               Size = 1; // skip illegible bytes
7566           }
7567         }
7568       }
7569       // Now that we are done disassembled the first symbol set the bool that
7570       // were doing this to false.
7571       FirstSymbol = false;
7572     }
7573     if (!symbolTableWorked) {
7574       // Reading the symbol table didn't work, disassemble the whole section.
7575       uint64_t SectAddress = Sections[SectIdx].getAddress();
7576       uint64_t SectSize = Sections[SectIdx].getSize();
7577       uint64_t InstSize;
7578       for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
7579         MCInst Inst;
7580 
7581         uint64_t PC = SectAddress + Index;
7582         SmallVector<char, 64> AnnotationsBytes;
7583         raw_svector_ostream Annotations(AnnotationsBytes);
7584         if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
7585                                    DebugOut, Annotations)) {
7586           if (!NoLeadingAddr) {
7587             if (FullLeadingAddr) {
7588               if (MachOOF->is64Bit())
7589                 outs() << format("%016" PRIx64, PC);
7590               else
7591                 outs() << format("%08" PRIx64, PC);
7592             } else {
7593               outs() << format("%8" PRIx64 ":", PC);
7594             }
7595           }
7596           if (!NoShowRawInsn || Arch == Triple::arm) {
7597             outs() << "\t";
7598             dumpBytes(makeArrayRef(Bytes.data() + Index, InstSize), outs());
7599           }
7600           StringRef AnnotationsStr = Annotations.str();
7601           IP->printInst(&Inst, outs(), AnnotationsStr, *STI);
7602           outs() << "\n";
7603         } else {
7604           unsigned int Arch = MachOOF->getArch();
7605           if (Arch == Triple::x86_64 || Arch == Triple::x86) {
7606             outs() << format("\t.byte 0x%02x #bad opcode\n",
7607                              *(Bytes.data() + Index) & 0xff);
7608             InstSize = 1; // skip exactly one illegible byte and move on.
7609           } else {
7610             WithColor::warning(errs(), "llvm-objdump")
7611                 << "invalid instruction encoding\n";
7612             if (InstSize == 0)
7613               InstSize = 1; // skip illegible bytes
7614           }
7615         }
7616       }
7617     }
7618     // The TripleName's need to be reset if we are called again for a different
7619     // archtecture.
7620     TripleName = "";
7621     ThumbTripleName = "";
7622 
7623     if (SymbolizerInfo.demangled_name != nullptr)
7624       free(SymbolizerInfo.demangled_name);
7625     if (ThumbSymbolizerInfo.demangled_name != nullptr)
7626       free(ThumbSymbolizerInfo.demangled_name);
7627   }
7628 }
7629 
7630 //===----------------------------------------------------------------------===//
7631 // __compact_unwind section dumping
7632 //===----------------------------------------------------------------------===//
7633 
7634 namespace {
7635 
7636 template <typename T>
7637 static uint64_t read(StringRef Contents, ptrdiff_t Offset) {
7638   using llvm::support::little;
7639   using llvm::support::unaligned;
7640 
7641   if (Offset + sizeof(T) > Contents.size()) {
7642     outs() << "warning: attempt to read past end of buffer\n";
7643     return T();
7644   }
7645 
7646   uint64_t Val =
7647       support::endian::read<T, little, unaligned>(Contents.data() + Offset);
7648   return Val;
7649 }
7650 
7651 template <typename T>
7652 static uint64_t readNext(StringRef Contents, ptrdiff_t &Offset) {
7653   T Val = read<T>(Contents, Offset);
7654   Offset += sizeof(T);
7655   return Val;
7656 }
7657 
7658 struct CompactUnwindEntry {
7659   uint32_t OffsetInSection;
7660 
7661   uint64_t FunctionAddr;
7662   uint32_t Length;
7663   uint32_t CompactEncoding;
7664   uint64_t PersonalityAddr;
7665   uint64_t LSDAAddr;
7666 
7667   RelocationRef FunctionReloc;
7668   RelocationRef PersonalityReloc;
7669   RelocationRef LSDAReloc;
7670 
7671   CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
7672       : OffsetInSection(Offset) {
7673     if (Is64)
7674       read<uint64_t>(Contents, Offset);
7675     else
7676       read<uint32_t>(Contents, Offset);
7677   }
7678 
7679 private:
7680   template <typename UIntPtr> void read(StringRef Contents, ptrdiff_t Offset) {
7681     FunctionAddr = readNext<UIntPtr>(Contents, Offset);
7682     Length = readNext<uint32_t>(Contents, Offset);
7683     CompactEncoding = readNext<uint32_t>(Contents, Offset);
7684     PersonalityAddr = readNext<UIntPtr>(Contents, Offset);
7685     LSDAAddr = readNext<UIntPtr>(Contents, Offset);
7686   }
7687 };
7688 }
7689 
7690 /// Given a relocation from __compact_unwind, consisting of the RelocationRef
7691 /// and data being relocated, determine the best base Name and Addend to use for
7692 /// display purposes.
7693 ///
7694 /// 1. An Extern relocation will directly reference a symbol (and the data is
7695 ///    then already an addend), so use that.
7696 /// 2. Otherwise the data is an offset in the object file's layout; try to find
7697 //     a symbol before it in the same section, and use the offset from there.
7698 /// 3. Finally, if all that fails, fall back to an offset from the start of the
7699 ///    referenced section.
7700 static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
7701                                       std::map<uint64_t, SymbolRef> &Symbols,
7702                                       const RelocationRef &Reloc, uint64_t Addr,
7703                                       StringRef &Name, uint64_t &Addend) {
7704   if (Reloc.getSymbol() != Obj->symbol_end()) {
7705     Name = unwrapOrError(Reloc.getSymbol()->getName(), Obj->getFileName());
7706     Addend = Addr;
7707     return;
7708   }
7709 
7710   auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
7711   SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
7712 
7713   uint64_t SectionAddr = RelocSection.getAddress();
7714 
7715   auto Sym = Symbols.upper_bound(Addr);
7716   if (Sym == Symbols.begin()) {
7717     // The first symbol in the object is after this reference, the best we can
7718     // do is section-relative notation.
7719     RelocSection.getName(Name);
7720     Addend = Addr - SectionAddr;
7721     return;
7722   }
7723 
7724   // Go back one so that SymbolAddress <= Addr.
7725   --Sym;
7726 
7727   section_iterator SymSection =
7728       unwrapOrError(Sym->second.getSection(), Obj->getFileName());
7729   if (RelocSection == *SymSection) {
7730     // There's a valid symbol in the same section before this reference.
7731     Name = unwrapOrError(Sym->second.getName(), Obj->getFileName());
7732     Addend = Addr - Sym->first;
7733     return;
7734   }
7735 
7736   // There is a symbol before this reference, but it's in a different
7737   // section. Probably not helpful to mention it, so use the section name.
7738   RelocSection.getName(Name);
7739   Addend = Addr - SectionAddr;
7740 }
7741 
7742 static void printUnwindRelocDest(const MachOObjectFile *Obj,
7743                                  std::map<uint64_t, SymbolRef> &Symbols,
7744                                  const RelocationRef &Reloc, uint64_t Addr) {
7745   StringRef Name;
7746   uint64_t Addend;
7747 
7748   if (!Reloc.getObject())
7749     return;
7750 
7751   findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
7752 
7753   outs() << Name;
7754   if (Addend)
7755     outs() << " + " << format("0x%" PRIx64, Addend);
7756 }
7757 
7758 static void
7759 printMachOCompactUnwindSection(const MachOObjectFile *Obj,
7760                                std::map<uint64_t, SymbolRef> &Symbols,
7761                                const SectionRef &CompactUnwind) {
7762 
7763   if (!Obj->isLittleEndian()) {
7764     outs() << "Skipping big-endian __compact_unwind section\n";
7765     return;
7766   }
7767 
7768   bool Is64 = Obj->is64Bit();
7769   uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
7770   uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
7771 
7772   StringRef Contents =
7773       unwrapOrError(CompactUnwind.getContents(), Obj->getFileName());
7774   SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
7775 
7776   // First populate the initial raw offsets, encodings and so on from the entry.
7777   for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
7778     CompactUnwindEntry Entry(Contents, Offset, Is64);
7779     CompactUnwinds.push_back(Entry);
7780   }
7781 
7782   // Next we need to look at the relocations to find out what objects are
7783   // actually being referred to.
7784   for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
7785     uint64_t RelocAddress = Reloc.getOffset();
7786 
7787     uint32_t EntryIdx = RelocAddress / EntrySize;
7788     uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
7789     CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
7790 
7791     if (OffsetInEntry == 0)
7792       Entry.FunctionReloc = Reloc;
7793     else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
7794       Entry.PersonalityReloc = Reloc;
7795     else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
7796       Entry.LSDAReloc = Reloc;
7797     else {
7798       outs() << "Invalid relocation in __compact_unwind section\n";
7799       return;
7800     }
7801   }
7802 
7803   // Finally, we're ready to print the data we've gathered.
7804   outs() << "Contents of __compact_unwind section:\n";
7805   for (auto &Entry : CompactUnwinds) {
7806     outs() << "  Entry at offset "
7807            << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
7808 
7809     // 1. Start of the region this entry applies to.
7810     outs() << "    start:                " << format("0x%" PRIx64,
7811                                                      Entry.FunctionAddr) << ' ';
7812     printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
7813     outs() << '\n';
7814 
7815     // 2. Length of the region this entry applies to.
7816     outs() << "    length:               " << format("0x%" PRIx32, Entry.Length)
7817            << '\n';
7818     // 3. The 32-bit compact encoding.
7819     outs() << "    compact encoding:     "
7820            << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
7821 
7822     // 4. The personality function, if present.
7823     if (Entry.PersonalityReloc.getObject()) {
7824       outs() << "    personality function: "
7825              << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
7826       printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
7827                            Entry.PersonalityAddr);
7828       outs() << '\n';
7829     }
7830 
7831     // 5. This entry's language-specific data area.
7832     if (Entry.LSDAReloc.getObject()) {
7833       outs() << "    LSDA:                 " << format("0x%" PRIx64,
7834                                                        Entry.LSDAAddr) << ' ';
7835       printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
7836       outs() << '\n';
7837     }
7838   }
7839 }
7840 
7841 //===----------------------------------------------------------------------===//
7842 // __unwind_info section dumping
7843 //===----------------------------------------------------------------------===//
7844 
7845 static void printRegularSecondLevelUnwindPage(StringRef PageData) {
7846   ptrdiff_t Pos = 0;
7847   uint32_t Kind = readNext<uint32_t>(PageData, Pos);
7848   (void)Kind;
7849   assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
7850 
7851   uint16_t EntriesStart = readNext<uint16_t>(PageData, Pos);
7852   uint16_t NumEntries = readNext<uint16_t>(PageData, Pos);
7853 
7854   Pos = EntriesStart;
7855   for (unsigned i = 0; i < NumEntries; ++i) {
7856     uint32_t FunctionOffset = readNext<uint32_t>(PageData, Pos);
7857     uint32_t Encoding = readNext<uint32_t>(PageData, Pos);
7858 
7859     outs() << "      [" << i << "]: "
7860            << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7861            << ", "
7862            << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
7863   }
7864 }
7865 
7866 static void printCompressedSecondLevelUnwindPage(
7867     StringRef PageData, uint32_t FunctionBase,
7868     const SmallVectorImpl<uint32_t> &CommonEncodings) {
7869   ptrdiff_t Pos = 0;
7870   uint32_t Kind = readNext<uint32_t>(PageData, Pos);
7871   (void)Kind;
7872   assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
7873 
7874   uint16_t EntriesStart = readNext<uint16_t>(PageData, Pos);
7875   uint16_t NumEntries = readNext<uint16_t>(PageData, Pos);
7876 
7877   uint16_t EncodingsStart = readNext<uint16_t>(PageData, Pos);
7878   readNext<uint16_t>(PageData, Pos);
7879   StringRef PageEncodings = PageData.substr(EncodingsStart, StringRef::npos);
7880 
7881   Pos = EntriesStart;
7882   for (unsigned i = 0; i < NumEntries; ++i) {
7883     uint32_t Entry = readNext<uint32_t>(PageData, Pos);
7884     uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
7885     uint32_t EncodingIdx = Entry >> 24;
7886 
7887     uint32_t Encoding;
7888     if (EncodingIdx < CommonEncodings.size())
7889       Encoding = CommonEncodings[EncodingIdx];
7890     else
7891       Encoding = read<uint32_t>(PageEncodings,
7892                                 sizeof(uint32_t) *
7893                                     (EncodingIdx - CommonEncodings.size()));
7894 
7895     outs() << "      [" << i << "]: "
7896            << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7897            << ", "
7898            << "encoding[" << EncodingIdx
7899            << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
7900   }
7901 }
7902 
7903 static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
7904                                         std::map<uint64_t, SymbolRef> &Symbols,
7905                                         const SectionRef &UnwindInfo) {
7906 
7907   if (!Obj->isLittleEndian()) {
7908     outs() << "Skipping big-endian __unwind_info section\n";
7909     return;
7910   }
7911 
7912   outs() << "Contents of __unwind_info section:\n";
7913 
7914   StringRef Contents =
7915       unwrapOrError(UnwindInfo.getContents(), Obj->getFileName());
7916   ptrdiff_t Pos = 0;
7917 
7918   //===----------------------------------
7919   // Section header
7920   //===----------------------------------
7921 
7922   uint32_t Version = readNext<uint32_t>(Contents, Pos);
7923   outs() << "  Version:                                   "
7924          << format("0x%" PRIx32, Version) << '\n';
7925   if (Version != 1) {
7926     outs() << "    Skipping section with unknown version\n";
7927     return;
7928   }
7929 
7930   uint32_t CommonEncodingsStart = readNext<uint32_t>(Contents, Pos);
7931   outs() << "  Common encodings array section offset:     "
7932          << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
7933   uint32_t NumCommonEncodings = readNext<uint32_t>(Contents, Pos);
7934   outs() << "  Number of common encodings in array:       "
7935          << format("0x%" PRIx32, NumCommonEncodings) << '\n';
7936 
7937   uint32_t PersonalitiesStart = readNext<uint32_t>(Contents, Pos);
7938   outs() << "  Personality function array section offset: "
7939          << format("0x%" PRIx32, PersonalitiesStart) << '\n';
7940   uint32_t NumPersonalities = readNext<uint32_t>(Contents, Pos);
7941   outs() << "  Number of personality functions in array:  "
7942          << format("0x%" PRIx32, NumPersonalities) << '\n';
7943 
7944   uint32_t IndicesStart = readNext<uint32_t>(Contents, Pos);
7945   outs() << "  Index array section offset:                "
7946          << format("0x%" PRIx32, IndicesStart) << '\n';
7947   uint32_t NumIndices = readNext<uint32_t>(Contents, Pos);
7948   outs() << "  Number of indices in array:                "
7949          << format("0x%" PRIx32, NumIndices) << '\n';
7950 
7951   //===----------------------------------
7952   // A shared list of common encodings
7953   //===----------------------------------
7954 
7955   // These occupy indices in the range [0, N] whenever an encoding is referenced
7956   // from a compressed 2nd level index table. In practice the linker only
7957   // creates ~128 of these, so that indices are available to embed encodings in
7958   // the 2nd level index.
7959 
7960   SmallVector<uint32_t, 64> CommonEncodings;
7961   outs() << "  Common encodings: (count = " << NumCommonEncodings << ")\n";
7962   Pos = CommonEncodingsStart;
7963   for (unsigned i = 0; i < NumCommonEncodings; ++i) {
7964     uint32_t Encoding = readNext<uint32_t>(Contents, Pos);
7965     CommonEncodings.push_back(Encoding);
7966 
7967     outs() << "    encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
7968            << '\n';
7969   }
7970 
7971   //===----------------------------------
7972   // Personality functions used in this executable
7973   //===----------------------------------
7974 
7975   // There should be only a handful of these (one per source language,
7976   // roughly). Particularly since they only get 2 bits in the compact encoding.
7977 
7978   outs() << "  Personality functions: (count = " << NumPersonalities << ")\n";
7979   Pos = PersonalitiesStart;
7980   for (unsigned i = 0; i < NumPersonalities; ++i) {
7981     uint32_t PersonalityFn = readNext<uint32_t>(Contents, Pos);
7982     outs() << "    personality[" << i + 1
7983            << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
7984   }
7985 
7986   //===----------------------------------
7987   // The level 1 index entries
7988   //===----------------------------------
7989 
7990   // These specify an approximate place to start searching for the more detailed
7991   // information, sorted by PC.
7992 
7993   struct IndexEntry {
7994     uint32_t FunctionOffset;
7995     uint32_t SecondLevelPageStart;
7996     uint32_t LSDAStart;
7997   };
7998 
7999   SmallVector<IndexEntry, 4> IndexEntries;
8000 
8001   outs() << "  Top level indices: (count = " << NumIndices << ")\n";
8002   Pos = IndicesStart;
8003   for (unsigned i = 0; i < NumIndices; ++i) {
8004     IndexEntry Entry;
8005 
8006     Entry.FunctionOffset = readNext<uint32_t>(Contents, Pos);
8007     Entry.SecondLevelPageStart = readNext<uint32_t>(Contents, Pos);
8008     Entry.LSDAStart = readNext<uint32_t>(Contents, Pos);
8009     IndexEntries.push_back(Entry);
8010 
8011     outs() << "    [" << i << "]: "
8012            << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
8013            << ", "
8014            << "2nd level page offset="
8015            << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
8016            << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
8017   }
8018 
8019   //===----------------------------------
8020   // Next come the LSDA tables
8021   //===----------------------------------
8022 
8023   // The LSDA layout is rather implicit: it's a contiguous array of entries from
8024   // the first top-level index's LSDAOffset to the last (sentinel).
8025 
8026   outs() << "  LSDA descriptors:\n";
8027   Pos = IndexEntries[0].LSDAStart;
8028   const uint32_t LSDASize = 2 * sizeof(uint32_t);
8029   int NumLSDAs =
8030       (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) / LSDASize;
8031 
8032   for (int i = 0; i < NumLSDAs; ++i) {
8033     uint32_t FunctionOffset = readNext<uint32_t>(Contents, Pos);
8034     uint32_t LSDAOffset = readNext<uint32_t>(Contents, Pos);
8035     outs() << "    [" << i << "]: "
8036            << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
8037            << ", "
8038            << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
8039   }
8040 
8041   //===----------------------------------
8042   // Finally, the 2nd level indices
8043   //===----------------------------------
8044 
8045   // Generally these are 4K in size, and have 2 possible forms:
8046   //   + Regular stores up to 511 entries with disparate encodings
8047   //   + Compressed stores up to 1021 entries if few enough compact encoding
8048   //     values are used.
8049   outs() << "  Second level indices:\n";
8050   for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
8051     // The final sentinel top-level index has no associated 2nd level page
8052     if (IndexEntries[i].SecondLevelPageStart == 0)
8053       break;
8054 
8055     outs() << "    Second level index[" << i << "]: "
8056            << "offset in section="
8057            << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
8058            << ", "
8059            << "base function offset="
8060            << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
8061 
8062     Pos = IndexEntries[i].SecondLevelPageStart;
8063     if (Pos + sizeof(uint32_t) > Contents.size()) {
8064       outs() << "warning: invalid offset for second level page: " << Pos << '\n';
8065       continue;
8066     }
8067 
8068     uint32_t Kind =
8069         *reinterpret_cast<const support::ulittle32_t *>(Contents.data() + Pos);
8070     if (Kind == 2)
8071       printRegularSecondLevelUnwindPage(Contents.substr(Pos, 4096));
8072     else if (Kind == 3)
8073       printCompressedSecondLevelUnwindPage(Contents.substr(Pos, 4096),
8074                                            IndexEntries[i].FunctionOffset,
8075                                            CommonEncodings);
8076     else
8077       outs() << "    Skipping 2nd level page with unknown kind " << Kind
8078              << '\n';
8079   }
8080 }
8081 
8082 void printMachOUnwindInfo(const MachOObjectFile *Obj) {
8083   std::map<uint64_t, SymbolRef> Symbols;
8084   for (const SymbolRef &SymRef : Obj->symbols()) {
8085     // Discard any undefined or absolute symbols. They're not going to take part
8086     // in the convenience lookup for unwind info and just take up resources.
8087     auto SectOrErr = SymRef.getSection();
8088     if (!SectOrErr) {
8089       // TODO: Actually report errors helpfully.
8090       consumeError(SectOrErr.takeError());
8091       continue;
8092     }
8093     section_iterator Section = *SectOrErr;
8094     if (Section == Obj->section_end())
8095       continue;
8096 
8097     uint64_t Addr = SymRef.getValue();
8098     Symbols.insert(std::make_pair(Addr, SymRef));
8099   }
8100 
8101   for (const SectionRef &Section : Obj->sections()) {
8102     StringRef SectName;
8103     Section.getName(SectName);
8104     if (SectName == "__compact_unwind")
8105       printMachOCompactUnwindSection(Obj, Symbols, Section);
8106     else if (SectName == "__unwind_info")
8107       printMachOUnwindInfoSection(Obj, Symbols, Section);
8108   }
8109 }
8110 
8111 static void PrintMachHeader(uint32_t magic, uint32_t cputype,
8112                             uint32_t cpusubtype, uint32_t filetype,
8113                             uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
8114                             bool verbose) {
8115   outs() << "Mach header\n";
8116   outs() << "      magic cputype cpusubtype  caps    filetype ncmds "
8117             "sizeofcmds      flags\n";
8118   if (verbose) {
8119     if (magic == MachO::MH_MAGIC)
8120       outs() << "   MH_MAGIC";
8121     else if (magic == MachO::MH_MAGIC_64)
8122       outs() << "MH_MAGIC_64";
8123     else
8124       outs() << format(" 0x%08" PRIx32, magic);
8125     switch (cputype) {
8126     case MachO::CPU_TYPE_I386:
8127       outs() << "    I386";
8128       switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8129       case MachO::CPU_SUBTYPE_I386_ALL:
8130         outs() << "        ALL";
8131         break;
8132       default:
8133         outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8134         break;
8135       }
8136       break;
8137     case MachO::CPU_TYPE_X86_64:
8138       outs() << "  X86_64";
8139       switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8140       case MachO::CPU_SUBTYPE_X86_64_ALL:
8141         outs() << "        ALL";
8142         break;
8143       case MachO::CPU_SUBTYPE_X86_64_H:
8144         outs() << "    Haswell";
8145         break;
8146       default:
8147         outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8148         break;
8149       }
8150       break;
8151     case MachO::CPU_TYPE_ARM:
8152       outs() << "     ARM";
8153       switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8154       case MachO::CPU_SUBTYPE_ARM_ALL:
8155         outs() << "        ALL";
8156         break;
8157       case MachO::CPU_SUBTYPE_ARM_V4T:
8158         outs() << "        V4T";
8159         break;
8160       case MachO::CPU_SUBTYPE_ARM_V5TEJ:
8161         outs() << "      V5TEJ";
8162         break;
8163       case MachO::CPU_SUBTYPE_ARM_XSCALE:
8164         outs() << "     XSCALE";
8165         break;
8166       case MachO::CPU_SUBTYPE_ARM_V6:
8167         outs() << "         V6";
8168         break;
8169       case MachO::CPU_SUBTYPE_ARM_V6M:
8170         outs() << "        V6M";
8171         break;
8172       case MachO::CPU_SUBTYPE_ARM_V7:
8173         outs() << "         V7";
8174         break;
8175       case MachO::CPU_SUBTYPE_ARM_V7EM:
8176         outs() << "       V7EM";
8177         break;
8178       case MachO::CPU_SUBTYPE_ARM_V7K:
8179         outs() << "        V7K";
8180         break;
8181       case MachO::CPU_SUBTYPE_ARM_V7M:
8182         outs() << "        V7M";
8183         break;
8184       case MachO::CPU_SUBTYPE_ARM_V7S:
8185         outs() << "        V7S";
8186         break;
8187       default:
8188         outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8189         break;
8190       }
8191       break;
8192     case MachO::CPU_TYPE_ARM64:
8193       outs() << "   ARM64";
8194       switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8195       case MachO::CPU_SUBTYPE_ARM64_ALL:
8196         outs() << "        ALL";
8197         break;
8198       case MachO::CPU_SUBTYPE_ARM64E:
8199         outs() << "          E";
8200         break;
8201       default:
8202         outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8203         break;
8204       }
8205       break;
8206     case MachO::CPU_TYPE_ARM64_32:
8207       outs() << " ARM64_32";
8208       switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8209       case MachO::CPU_SUBTYPE_ARM64_32_V8:
8210         outs() << "        V8";
8211         break;
8212       default:
8213         outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8214         break;
8215       }
8216       break;
8217     case MachO::CPU_TYPE_POWERPC:
8218       outs() << "     PPC";
8219       switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8220       case MachO::CPU_SUBTYPE_POWERPC_ALL:
8221         outs() << "        ALL";
8222         break;
8223       default:
8224         outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8225         break;
8226       }
8227       break;
8228     case MachO::CPU_TYPE_POWERPC64:
8229       outs() << "   PPC64";
8230       switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
8231       case MachO::CPU_SUBTYPE_POWERPC_ALL:
8232         outs() << "        ALL";
8233         break;
8234       default:
8235         outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8236         break;
8237       }
8238       break;
8239     default:
8240       outs() << format(" %7d", cputype);
8241       outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8242       break;
8243     }
8244     if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
8245       outs() << " LIB64";
8246     } else {
8247       outs() << format("  0x%02" PRIx32,
8248                        (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
8249     }
8250     switch (filetype) {
8251     case MachO::MH_OBJECT:
8252       outs() << "      OBJECT";
8253       break;
8254     case MachO::MH_EXECUTE:
8255       outs() << "     EXECUTE";
8256       break;
8257     case MachO::MH_FVMLIB:
8258       outs() << "      FVMLIB";
8259       break;
8260     case MachO::MH_CORE:
8261       outs() << "        CORE";
8262       break;
8263     case MachO::MH_PRELOAD:
8264       outs() << "     PRELOAD";
8265       break;
8266     case MachO::MH_DYLIB:
8267       outs() << "       DYLIB";
8268       break;
8269     case MachO::MH_DYLIB_STUB:
8270       outs() << "  DYLIB_STUB";
8271       break;
8272     case MachO::MH_DYLINKER:
8273       outs() << "    DYLINKER";
8274       break;
8275     case MachO::MH_BUNDLE:
8276       outs() << "      BUNDLE";
8277       break;
8278     case MachO::MH_DSYM:
8279       outs() << "        DSYM";
8280       break;
8281     case MachO::MH_KEXT_BUNDLE:
8282       outs() << "  KEXTBUNDLE";
8283       break;
8284     default:
8285       outs() << format("  %10u", filetype);
8286       break;
8287     }
8288     outs() << format(" %5u", ncmds);
8289     outs() << format(" %10u", sizeofcmds);
8290     uint32_t f = flags;
8291     if (f & MachO::MH_NOUNDEFS) {
8292       outs() << "   NOUNDEFS";
8293       f &= ~MachO::MH_NOUNDEFS;
8294     }
8295     if (f & MachO::MH_INCRLINK) {
8296       outs() << " INCRLINK";
8297       f &= ~MachO::MH_INCRLINK;
8298     }
8299     if (f & MachO::MH_DYLDLINK) {
8300       outs() << " DYLDLINK";
8301       f &= ~MachO::MH_DYLDLINK;
8302     }
8303     if (f & MachO::MH_BINDATLOAD) {
8304       outs() << " BINDATLOAD";
8305       f &= ~MachO::MH_BINDATLOAD;
8306     }
8307     if (f & MachO::MH_PREBOUND) {
8308       outs() << " PREBOUND";
8309       f &= ~MachO::MH_PREBOUND;
8310     }
8311     if (f & MachO::MH_SPLIT_SEGS) {
8312       outs() << " SPLIT_SEGS";
8313       f &= ~MachO::MH_SPLIT_SEGS;
8314     }
8315     if (f & MachO::MH_LAZY_INIT) {
8316       outs() << " LAZY_INIT";
8317       f &= ~MachO::MH_LAZY_INIT;
8318     }
8319     if (f & MachO::MH_TWOLEVEL) {
8320       outs() << " TWOLEVEL";
8321       f &= ~MachO::MH_TWOLEVEL;
8322     }
8323     if (f & MachO::MH_FORCE_FLAT) {
8324       outs() << " FORCE_FLAT";
8325       f &= ~MachO::MH_FORCE_FLAT;
8326     }
8327     if (f & MachO::MH_NOMULTIDEFS) {
8328       outs() << " NOMULTIDEFS";
8329       f &= ~MachO::MH_NOMULTIDEFS;
8330     }
8331     if (f & MachO::MH_NOFIXPREBINDING) {
8332       outs() << " NOFIXPREBINDING";
8333       f &= ~MachO::MH_NOFIXPREBINDING;
8334     }
8335     if (f & MachO::MH_PREBINDABLE) {
8336       outs() << " PREBINDABLE";
8337       f &= ~MachO::MH_PREBINDABLE;
8338     }
8339     if (f & MachO::MH_ALLMODSBOUND) {
8340       outs() << " ALLMODSBOUND";
8341       f &= ~MachO::MH_ALLMODSBOUND;
8342     }
8343     if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
8344       outs() << " SUBSECTIONS_VIA_SYMBOLS";
8345       f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
8346     }
8347     if (f & MachO::MH_CANONICAL) {
8348       outs() << " CANONICAL";
8349       f &= ~MachO::MH_CANONICAL;
8350     }
8351     if (f & MachO::MH_WEAK_DEFINES) {
8352       outs() << " WEAK_DEFINES";
8353       f &= ~MachO::MH_WEAK_DEFINES;
8354     }
8355     if (f & MachO::MH_BINDS_TO_WEAK) {
8356       outs() << " BINDS_TO_WEAK";
8357       f &= ~MachO::MH_BINDS_TO_WEAK;
8358     }
8359     if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
8360       outs() << " ALLOW_STACK_EXECUTION";
8361       f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
8362     }
8363     if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
8364       outs() << " DEAD_STRIPPABLE_DYLIB";
8365       f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
8366     }
8367     if (f & MachO::MH_PIE) {
8368       outs() << " PIE";
8369       f &= ~MachO::MH_PIE;
8370     }
8371     if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
8372       outs() << " NO_REEXPORTED_DYLIBS";
8373       f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
8374     }
8375     if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
8376       outs() << " MH_HAS_TLV_DESCRIPTORS";
8377       f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
8378     }
8379     if (f & MachO::MH_NO_HEAP_EXECUTION) {
8380       outs() << " MH_NO_HEAP_EXECUTION";
8381       f &= ~MachO::MH_NO_HEAP_EXECUTION;
8382     }
8383     if (f & MachO::MH_APP_EXTENSION_SAFE) {
8384       outs() << " APP_EXTENSION_SAFE";
8385       f &= ~MachO::MH_APP_EXTENSION_SAFE;
8386     }
8387     if (f & MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO) {
8388       outs() << " NLIST_OUTOFSYNC_WITH_DYLDINFO";
8389       f &= ~MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO;
8390     }
8391     if (f != 0 || flags == 0)
8392       outs() << format(" 0x%08" PRIx32, f);
8393   } else {
8394     outs() << format(" 0x%08" PRIx32, magic);
8395     outs() << format(" %7d", cputype);
8396     outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8397     outs() << format("  0x%02" PRIx32,
8398                      (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
8399     outs() << format("  %10u", filetype);
8400     outs() << format(" %5u", ncmds);
8401     outs() << format(" %10u", sizeofcmds);
8402     outs() << format(" 0x%08" PRIx32, flags);
8403   }
8404   outs() << "\n";
8405 }
8406 
8407 static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
8408                                 StringRef SegName, uint64_t vmaddr,
8409                                 uint64_t vmsize, uint64_t fileoff,
8410                                 uint64_t filesize, uint32_t maxprot,
8411                                 uint32_t initprot, uint32_t nsects,
8412                                 uint32_t flags, uint32_t object_size,
8413                                 bool verbose) {
8414   uint64_t expected_cmdsize;
8415   if (cmd == MachO::LC_SEGMENT) {
8416     outs() << "      cmd LC_SEGMENT\n";
8417     expected_cmdsize = nsects;
8418     expected_cmdsize *= sizeof(struct MachO::section);
8419     expected_cmdsize += sizeof(struct MachO::segment_command);
8420   } else {
8421     outs() << "      cmd LC_SEGMENT_64\n";
8422     expected_cmdsize = nsects;
8423     expected_cmdsize *= sizeof(struct MachO::section_64);
8424     expected_cmdsize += sizeof(struct MachO::segment_command_64);
8425   }
8426   outs() << "  cmdsize " << cmdsize;
8427   if (cmdsize != expected_cmdsize)
8428     outs() << " Inconsistent size\n";
8429   else
8430     outs() << "\n";
8431   outs() << "  segname " << SegName << "\n";
8432   if (cmd == MachO::LC_SEGMENT_64) {
8433     outs() << "   vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
8434     outs() << "   vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
8435   } else {
8436     outs() << "   vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
8437     outs() << "   vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
8438   }
8439   outs() << "  fileoff " << fileoff;
8440   if (fileoff > object_size)
8441     outs() << " (past end of file)\n";
8442   else
8443     outs() << "\n";
8444   outs() << " filesize " << filesize;
8445   if (fileoff + filesize > object_size)
8446     outs() << " (past end of file)\n";
8447   else
8448     outs() << "\n";
8449   if (verbose) {
8450     if ((maxprot &
8451          ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
8452            MachO::VM_PROT_EXECUTE)) != 0)
8453       outs() << "  maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
8454     else {
8455       outs() << "  maxprot ";
8456       outs() << ((maxprot & MachO::VM_PROT_READ) ? "r" : "-");
8457       outs() << ((maxprot & MachO::VM_PROT_WRITE) ? "w" : "-");
8458       outs() << ((maxprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
8459     }
8460     if ((initprot &
8461          ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
8462            MachO::VM_PROT_EXECUTE)) != 0)
8463       outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
8464     else {
8465       outs() << " initprot ";
8466       outs() << ((initprot & MachO::VM_PROT_READ) ? "r" : "-");
8467       outs() << ((initprot & MachO::VM_PROT_WRITE) ? "w" : "-");
8468       outs() << ((initprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
8469     }
8470   } else {
8471     outs() << "  maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
8472     outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
8473   }
8474   outs() << "   nsects " << nsects << "\n";
8475   if (verbose) {
8476     outs() << "    flags";
8477     if (flags == 0)
8478       outs() << " (none)\n";
8479     else {
8480       if (flags & MachO::SG_HIGHVM) {
8481         outs() << " HIGHVM";
8482         flags &= ~MachO::SG_HIGHVM;
8483       }
8484       if (flags & MachO::SG_FVMLIB) {
8485         outs() << " FVMLIB";
8486         flags &= ~MachO::SG_FVMLIB;
8487       }
8488       if (flags & MachO::SG_NORELOC) {
8489         outs() << " NORELOC";
8490         flags &= ~MachO::SG_NORELOC;
8491       }
8492       if (flags & MachO::SG_PROTECTED_VERSION_1) {
8493         outs() << " PROTECTED_VERSION_1";
8494         flags &= ~MachO::SG_PROTECTED_VERSION_1;
8495       }
8496       if (flags)
8497         outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
8498       else
8499         outs() << "\n";
8500     }
8501   } else {
8502     outs() << "    flags " << format("0x%" PRIx32, flags) << "\n";
8503   }
8504 }
8505 
8506 static void PrintSection(const char *sectname, const char *segname,
8507                          uint64_t addr, uint64_t size, uint32_t offset,
8508                          uint32_t align, uint32_t reloff, uint32_t nreloc,
8509                          uint32_t flags, uint32_t reserved1, uint32_t reserved2,
8510                          uint32_t cmd, const char *sg_segname,
8511                          uint32_t filetype, uint32_t object_size,
8512                          bool verbose) {
8513   outs() << "Section\n";
8514   outs() << "  sectname " << format("%.16s\n", sectname);
8515   outs() << "   segname " << format("%.16s", segname);
8516   if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
8517     outs() << " (does not match segment)\n";
8518   else
8519     outs() << "\n";
8520   if (cmd == MachO::LC_SEGMENT_64) {
8521     outs() << "      addr " << format("0x%016" PRIx64, addr) << "\n";
8522     outs() << "      size " << format("0x%016" PRIx64, size);
8523   } else {
8524     outs() << "      addr " << format("0x%08" PRIx64, addr) << "\n";
8525     outs() << "      size " << format("0x%08" PRIx64, size);
8526   }
8527   if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
8528     outs() << " (past end of file)\n";
8529   else
8530     outs() << "\n";
8531   outs() << "    offset " << offset;
8532   if (offset > object_size)
8533     outs() << " (past end of file)\n";
8534   else
8535     outs() << "\n";
8536   uint32_t align_shifted = 1 << align;
8537   outs() << "     align 2^" << align << " (" << align_shifted << ")\n";
8538   outs() << "    reloff " << reloff;
8539   if (reloff > object_size)
8540     outs() << " (past end of file)\n";
8541   else
8542     outs() << "\n";
8543   outs() << "    nreloc " << nreloc;
8544   if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
8545     outs() << " (past end of file)\n";
8546   else
8547     outs() << "\n";
8548   uint32_t section_type = flags & MachO::SECTION_TYPE;
8549   if (verbose) {
8550     outs() << "      type";
8551     if (section_type == MachO::S_REGULAR)
8552       outs() << " S_REGULAR\n";
8553     else if (section_type == MachO::S_ZEROFILL)
8554       outs() << " S_ZEROFILL\n";
8555     else if (section_type == MachO::S_CSTRING_LITERALS)
8556       outs() << " S_CSTRING_LITERALS\n";
8557     else if (section_type == MachO::S_4BYTE_LITERALS)
8558       outs() << " S_4BYTE_LITERALS\n";
8559     else if (section_type == MachO::S_8BYTE_LITERALS)
8560       outs() << " S_8BYTE_LITERALS\n";
8561     else if (section_type == MachO::S_16BYTE_LITERALS)
8562       outs() << " S_16BYTE_LITERALS\n";
8563     else if (section_type == MachO::S_LITERAL_POINTERS)
8564       outs() << " S_LITERAL_POINTERS\n";
8565     else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
8566       outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
8567     else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
8568       outs() << " S_LAZY_SYMBOL_POINTERS\n";
8569     else if (section_type == MachO::S_SYMBOL_STUBS)
8570       outs() << " S_SYMBOL_STUBS\n";
8571     else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
8572       outs() << " S_MOD_INIT_FUNC_POINTERS\n";
8573     else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
8574       outs() << " S_MOD_TERM_FUNC_POINTERS\n";
8575     else if (section_type == MachO::S_COALESCED)
8576       outs() << " S_COALESCED\n";
8577     else if (section_type == MachO::S_INTERPOSING)
8578       outs() << " S_INTERPOSING\n";
8579     else if (section_type == MachO::S_DTRACE_DOF)
8580       outs() << " S_DTRACE_DOF\n";
8581     else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
8582       outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
8583     else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
8584       outs() << " S_THREAD_LOCAL_REGULAR\n";
8585     else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
8586       outs() << " S_THREAD_LOCAL_ZEROFILL\n";
8587     else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
8588       outs() << " S_THREAD_LOCAL_VARIABLES\n";
8589     else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
8590       outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
8591     else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
8592       outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
8593     else
8594       outs() << format("0x%08" PRIx32, section_type) << "\n";
8595     outs() << "attributes";
8596     uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
8597     if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
8598       outs() << " PURE_INSTRUCTIONS";
8599     if (section_attributes & MachO::S_ATTR_NO_TOC)
8600       outs() << " NO_TOC";
8601     if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
8602       outs() << " STRIP_STATIC_SYMS";
8603     if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
8604       outs() << " NO_DEAD_STRIP";
8605     if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
8606       outs() << " LIVE_SUPPORT";
8607     if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
8608       outs() << " SELF_MODIFYING_CODE";
8609     if (section_attributes & MachO::S_ATTR_DEBUG)
8610       outs() << " DEBUG";
8611     if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
8612       outs() << " SOME_INSTRUCTIONS";
8613     if (section_attributes & MachO::S_ATTR_EXT_RELOC)
8614       outs() << " EXT_RELOC";
8615     if (section_attributes & MachO::S_ATTR_LOC_RELOC)
8616       outs() << " LOC_RELOC";
8617     if (section_attributes == 0)
8618       outs() << " (none)";
8619     outs() << "\n";
8620   } else
8621     outs() << "     flags " << format("0x%08" PRIx32, flags) << "\n";
8622   outs() << " reserved1 " << reserved1;
8623   if (section_type == MachO::S_SYMBOL_STUBS ||
8624       section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
8625       section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
8626       section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
8627       section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
8628     outs() << " (index into indirect symbol table)\n";
8629   else
8630     outs() << "\n";
8631   outs() << " reserved2 " << reserved2;
8632   if (section_type == MachO::S_SYMBOL_STUBS)
8633     outs() << " (size of stubs)\n";
8634   else
8635     outs() << "\n";
8636 }
8637 
8638 static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
8639                                    uint32_t object_size) {
8640   outs() << "     cmd LC_SYMTAB\n";
8641   outs() << " cmdsize " << st.cmdsize;
8642   if (st.cmdsize != sizeof(struct MachO::symtab_command))
8643     outs() << " Incorrect size\n";
8644   else
8645     outs() << "\n";
8646   outs() << "  symoff " << st.symoff;
8647   if (st.symoff > object_size)
8648     outs() << " (past end of file)\n";
8649   else
8650     outs() << "\n";
8651   outs() << "   nsyms " << st.nsyms;
8652   uint64_t big_size;
8653   if (Is64Bit) {
8654     big_size = st.nsyms;
8655     big_size *= sizeof(struct MachO::nlist_64);
8656     big_size += st.symoff;
8657     if (big_size > object_size)
8658       outs() << " (past end of file)\n";
8659     else
8660       outs() << "\n";
8661   } else {
8662     big_size = st.nsyms;
8663     big_size *= sizeof(struct MachO::nlist);
8664     big_size += st.symoff;
8665     if (big_size > object_size)
8666       outs() << " (past end of file)\n";
8667     else
8668       outs() << "\n";
8669   }
8670   outs() << "  stroff " << st.stroff;
8671   if (st.stroff > object_size)
8672     outs() << " (past end of file)\n";
8673   else
8674     outs() << "\n";
8675   outs() << " strsize " << st.strsize;
8676   big_size = st.stroff;
8677   big_size += st.strsize;
8678   if (big_size > object_size)
8679     outs() << " (past end of file)\n";
8680   else
8681     outs() << "\n";
8682 }
8683 
8684 static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
8685                                      uint32_t nsyms, uint32_t object_size,
8686                                      bool Is64Bit) {
8687   outs() << "            cmd LC_DYSYMTAB\n";
8688   outs() << "        cmdsize " << dyst.cmdsize;
8689   if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
8690     outs() << " Incorrect size\n";
8691   else
8692     outs() << "\n";
8693   outs() << "      ilocalsym " << dyst.ilocalsym;
8694   if (dyst.ilocalsym > nsyms)
8695     outs() << " (greater than the number of symbols)\n";
8696   else
8697     outs() << "\n";
8698   outs() << "      nlocalsym " << dyst.nlocalsym;
8699   uint64_t big_size;
8700   big_size = dyst.ilocalsym;
8701   big_size += dyst.nlocalsym;
8702   if (big_size > nsyms)
8703     outs() << " (past the end of the symbol table)\n";
8704   else
8705     outs() << "\n";
8706   outs() << "     iextdefsym " << dyst.iextdefsym;
8707   if (dyst.iextdefsym > nsyms)
8708     outs() << " (greater than the number of symbols)\n";
8709   else
8710     outs() << "\n";
8711   outs() << "     nextdefsym " << dyst.nextdefsym;
8712   big_size = dyst.iextdefsym;
8713   big_size += dyst.nextdefsym;
8714   if (big_size > nsyms)
8715     outs() << " (past the end of the symbol table)\n";
8716   else
8717     outs() << "\n";
8718   outs() << "      iundefsym " << dyst.iundefsym;
8719   if (dyst.iundefsym > nsyms)
8720     outs() << " (greater than the number of symbols)\n";
8721   else
8722     outs() << "\n";
8723   outs() << "      nundefsym " << dyst.nundefsym;
8724   big_size = dyst.iundefsym;
8725   big_size += dyst.nundefsym;
8726   if (big_size > nsyms)
8727     outs() << " (past the end of the symbol table)\n";
8728   else
8729     outs() << "\n";
8730   outs() << "         tocoff " << dyst.tocoff;
8731   if (dyst.tocoff > object_size)
8732     outs() << " (past end of file)\n";
8733   else
8734     outs() << "\n";
8735   outs() << "           ntoc " << dyst.ntoc;
8736   big_size = dyst.ntoc;
8737   big_size *= sizeof(struct MachO::dylib_table_of_contents);
8738   big_size += dyst.tocoff;
8739   if (big_size > object_size)
8740     outs() << " (past end of file)\n";
8741   else
8742     outs() << "\n";
8743   outs() << "      modtaboff " << dyst.modtaboff;
8744   if (dyst.modtaboff > object_size)
8745     outs() << " (past end of file)\n";
8746   else
8747     outs() << "\n";
8748   outs() << "        nmodtab " << dyst.nmodtab;
8749   uint64_t modtabend;
8750   if (Is64Bit) {
8751     modtabend = dyst.nmodtab;
8752     modtabend *= sizeof(struct MachO::dylib_module_64);
8753     modtabend += dyst.modtaboff;
8754   } else {
8755     modtabend = dyst.nmodtab;
8756     modtabend *= sizeof(struct MachO::dylib_module);
8757     modtabend += dyst.modtaboff;
8758   }
8759   if (modtabend > object_size)
8760     outs() << " (past end of file)\n";
8761   else
8762     outs() << "\n";
8763   outs() << "   extrefsymoff " << dyst.extrefsymoff;
8764   if (dyst.extrefsymoff > object_size)
8765     outs() << " (past end of file)\n";
8766   else
8767     outs() << "\n";
8768   outs() << "    nextrefsyms " << dyst.nextrefsyms;
8769   big_size = dyst.nextrefsyms;
8770   big_size *= sizeof(struct MachO::dylib_reference);
8771   big_size += dyst.extrefsymoff;
8772   if (big_size > object_size)
8773     outs() << " (past end of file)\n";
8774   else
8775     outs() << "\n";
8776   outs() << " indirectsymoff " << dyst.indirectsymoff;
8777   if (dyst.indirectsymoff > object_size)
8778     outs() << " (past end of file)\n";
8779   else
8780     outs() << "\n";
8781   outs() << "  nindirectsyms " << dyst.nindirectsyms;
8782   big_size = dyst.nindirectsyms;
8783   big_size *= sizeof(uint32_t);
8784   big_size += dyst.indirectsymoff;
8785   if (big_size > object_size)
8786     outs() << " (past end of file)\n";
8787   else
8788     outs() << "\n";
8789   outs() << "      extreloff " << dyst.extreloff;
8790   if (dyst.extreloff > object_size)
8791     outs() << " (past end of file)\n";
8792   else
8793     outs() << "\n";
8794   outs() << "        nextrel " << dyst.nextrel;
8795   big_size = dyst.nextrel;
8796   big_size *= sizeof(struct MachO::relocation_info);
8797   big_size += dyst.extreloff;
8798   if (big_size > object_size)
8799     outs() << " (past end of file)\n";
8800   else
8801     outs() << "\n";
8802   outs() << "      locreloff " << dyst.locreloff;
8803   if (dyst.locreloff > object_size)
8804     outs() << " (past end of file)\n";
8805   else
8806     outs() << "\n";
8807   outs() << "        nlocrel " << dyst.nlocrel;
8808   big_size = dyst.nlocrel;
8809   big_size *= sizeof(struct MachO::relocation_info);
8810   big_size += dyst.locreloff;
8811   if (big_size > object_size)
8812     outs() << " (past end of file)\n";
8813   else
8814     outs() << "\n";
8815 }
8816 
8817 static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
8818                                      uint32_t object_size) {
8819   if (dc.cmd == MachO::LC_DYLD_INFO)
8820     outs() << "            cmd LC_DYLD_INFO\n";
8821   else
8822     outs() << "            cmd LC_DYLD_INFO_ONLY\n";
8823   outs() << "        cmdsize " << dc.cmdsize;
8824   if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
8825     outs() << " Incorrect size\n";
8826   else
8827     outs() << "\n";
8828   outs() << "     rebase_off " << dc.rebase_off;
8829   if (dc.rebase_off > object_size)
8830     outs() << " (past end of file)\n";
8831   else
8832     outs() << "\n";
8833   outs() << "    rebase_size " << dc.rebase_size;
8834   uint64_t big_size;
8835   big_size = dc.rebase_off;
8836   big_size += dc.rebase_size;
8837   if (big_size > object_size)
8838     outs() << " (past end of file)\n";
8839   else
8840     outs() << "\n";
8841   outs() << "       bind_off " << dc.bind_off;
8842   if (dc.bind_off > object_size)
8843     outs() << " (past end of file)\n";
8844   else
8845     outs() << "\n";
8846   outs() << "      bind_size " << dc.bind_size;
8847   big_size = dc.bind_off;
8848   big_size += dc.bind_size;
8849   if (big_size > object_size)
8850     outs() << " (past end of file)\n";
8851   else
8852     outs() << "\n";
8853   outs() << "  weak_bind_off " << dc.weak_bind_off;
8854   if (dc.weak_bind_off > object_size)
8855     outs() << " (past end of file)\n";
8856   else
8857     outs() << "\n";
8858   outs() << " weak_bind_size " << dc.weak_bind_size;
8859   big_size = dc.weak_bind_off;
8860   big_size += dc.weak_bind_size;
8861   if (big_size > object_size)
8862     outs() << " (past end of file)\n";
8863   else
8864     outs() << "\n";
8865   outs() << "  lazy_bind_off " << dc.lazy_bind_off;
8866   if (dc.lazy_bind_off > object_size)
8867     outs() << " (past end of file)\n";
8868   else
8869     outs() << "\n";
8870   outs() << " lazy_bind_size " << dc.lazy_bind_size;
8871   big_size = dc.lazy_bind_off;
8872   big_size += dc.lazy_bind_size;
8873   if (big_size > object_size)
8874     outs() << " (past end of file)\n";
8875   else
8876     outs() << "\n";
8877   outs() << "     export_off " << dc.export_off;
8878   if (dc.export_off > object_size)
8879     outs() << " (past end of file)\n";
8880   else
8881     outs() << "\n";
8882   outs() << "    export_size " << dc.export_size;
8883   big_size = dc.export_off;
8884   big_size += dc.export_size;
8885   if (big_size > object_size)
8886     outs() << " (past end of file)\n";
8887   else
8888     outs() << "\n";
8889 }
8890 
8891 static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
8892                                  const char *Ptr) {
8893   if (dyld.cmd == MachO::LC_ID_DYLINKER)
8894     outs() << "          cmd LC_ID_DYLINKER\n";
8895   else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
8896     outs() << "          cmd LC_LOAD_DYLINKER\n";
8897   else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
8898     outs() << "          cmd LC_DYLD_ENVIRONMENT\n";
8899   else
8900     outs() << "          cmd ?(" << dyld.cmd << ")\n";
8901   outs() << "      cmdsize " << dyld.cmdsize;
8902   if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
8903     outs() << " Incorrect size\n";
8904   else
8905     outs() << "\n";
8906   if (dyld.name >= dyld.cmdsize)
8907     outs() << "         name ?(bad offset " << dyld.name << ")\n";
8908   else {
8909     const char *P = (const char *)(Ptr) + dyld.name;
8910     outs() << "         name " << P << " (offset " << dyld.name << ")\n";
8911   }
8912 }
8913 
8914 static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
8915   outs() << "     cmd LC_UUID\n";
8916   outs() << " cmdsize " << uuid.cmdsize;
8917   if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
8918     outs() << " Incorrect size\n";
8919   else
8920     outs() << "\n";
8921   outs() << "    uuid ";
8922   for (int i = 0; i < 16; ++i) {
8923     outs() << format("%02" PRIX32, uuid.uuid[i]);
8924     if (i == 3 || i == 5 || i == 7 || i == 9)
8925       outs() << "-";
8926   }
8927   outs() << "\n";
8928 }
8929 
8930 static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
8931   outs() << "          cmd LC_RPATH\n";
8932   outs() << "      cmdsize " << rpath.cmdsize;
8933   if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
8934     outs() << " Incorrect size\n";
8935   else
8936     outs() << "\n";
8937   if (rpath.path >= rpath.cmdsize)
8938     outs() << "         path ?(bad offset " << rpath.path << ")\n";
8939   else {
8940     const char *P = (const char *)(Ptr) + rpath.path;
8941     outs() << "         path " << P << " (offset " << rpath.path << ")\n";
8942   }
8943 }
8944 
8945 static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
8946   StringRef LoadCmdName;
8947   switch (vd.cmd) {
8948   case MachO::LC_VERSION_MIN_MACOSX:
8949     LoadCmdName = "LC_VERSION_MIN_MACOSX";
8950     break;
8951   case MachO::LC_VERSION_MIN_IPHONEOS:
8952     LoadCmdName = "LC_VERSION_MIN_IPHONEOS";
8953     break;
8954   case MachO::LC_VERSION_MIN_TVOS:
8955     LoadCmdName = "LC_VERSION_MIN_TVOS";
8956     break;
8957   case MachO::LC_VERSION_MIN_WATCHOS:
8958     LoadCmdName = "LC_VERSION_MIN_WATCHOS";
8959     break;
8960   default:
8961     llvm_unreachable("Unknown version min load command");
8962   }
8963 
8964   outs() << "      cmd " << LoadCmdName << '\n';
8965   outs() << "  cmdsize " << vd.cmdsize;
8966   if (vd.cmdsize != sizeof(struct MachO::version_min_command))
8967     outs() << " Incorrect size\n";
8968   else
8969     outs() << "\n";
8970   outs() << "  version "
8971          << MachOObjectFile::getVersionMinMajor(vd, false) << "."
8972          << MachOObjectFile::getVersionMinMinor(vd, false);
8973   uint32_t Update = MachOObjectFile::getVersionMinUpdate(vd, false);
8974   if (Update != 0)
8975     outs() << "." << Update;
8976   outs() << "\n";
8977   if (vd.sdk == 0)
8978     outs() << "      sdk n/a";
8979   else {
8980     outs() << "      sdk "
8981            << MachOObjectFile::getVersionMinMajor(vd, true) << "."
8982            << MachOObjectFile::getVersionMinMinor(vd, true);
8983   }
8984   Update = MachOObjectFile::getVersionMinUpdate(vd, true);
8985   if (Update != 0)
8986     outs() << "." << Update;
8987   outs() << "\n";
8988 }
8989 
8990 static void PrintNoteLoadCommand(MachO::note_command Nt) {
8991   outs() << "       cmd LC_NOTE\n";
8992   outs() << "   cmdsize " << Nt.cmdsize;
8993   if (Nt.cmdsize != sizeof(struct MachO::note_command))
8994     outs() << " Incorrect size\n";
8995   else
8996     outs() << "\n";
8997   const char *d = Nt.data_owner;
8998   outs() << "data_owner " << format("%.16s\n", d);
8999   outs() << "    offset " << Nt.offset << "\n";
9000   outs() << "      size " << Nt.size << "\n";
9001 }
9002 
9003 static void PrintBuildToolVersion(MachO::build_tool_version bv) {
9004   outs() << "      tool " << MachOObjectFile::getBuildTool(bv.tool) << "\n";
9005   outs() << "   version " << MachOObjectFile::getVersionString(bv.version)
9006          << "\n";
9007 }
9008 
9009 static void PrintBuildVersionLoadCommand(const MachOObjectFile *obj,
9010                                          MachO::build_version_command bd) {
9011   outs() << "       cmd LC_BUILD_VERSION\n";
9012   outs() << "   cmdsize " << bd.cmdsize;
9013   if (bd.cmdsize !=
9014       sizeof(struct MachO::build_version_command) +
9015           bd.ntools * sizeof(struct MachO::build_tool_version))
9016     outs() << " Incorrect size\n";
9017   else
9018     outs() << "\n";
9019   outs() << "  platform " << MachOObjectFile::getBuildPlatform(bd.platform)
9020          << "\n";
9021   if (bd.sdk)
9022     outs() << "       sdk " << MachOObjectFile::getVersionString(bd.sdk)
9023            << "\n";
9024   else
9025     outs() << "       sdk n/a\n";
9026   outs() << "     minos " << MachOObjectFile::getVersionString(bd.minos)
9027          << "\n";
9028   outs() << "    ntools " << bd.ntools << "\n";
9029   for (unsigned i = 0; i < bd.ntools; ++i) {
9030     MachO::build_tool_version bv = obj->getBuildToolVersion(i);
9031     PrintBuildToolVersion(bv);
9032   }
9033 }
9034 
9035 static void PrintSourceVersionCommand(MachO::source_version_command sd) {
9036   outs() << "      cmd LC_SOURCE_VERSION\n";
9037   outs() << "  cmdsize " << sd.cmdsize;
9038   if (sd.cmdsize != sizeof(struct MachO::source_version_command))
9039     outs() << " Incorrect size\n";
9040   else
9041     outs() << "\n";
9042   uint64_t a = (sd.version >> 40) & 0xffffff;
9043   uint64_t b = (sd.version >> 30) & 0x3ff;
9044   uint64_t c = (sd.version >> 20) & 0x3ff;
9045   uint64_t d = (sd.version >> 10) & 0x3ff;
9046   uint64_t e = sd.version & 0x3ff;
9047   outs() << "  version " << a << "." << b;
9048   if (e != 0)
9049     outs() << "." << c << "." << d << "." << e;
9050   else if (d != 0)
9051     outs() << "." << c << "." << d;
9052   else if (c != 0)
9053     outs() << "." << c;
9054   outs() << "\n";
9055 }
9056 
9057 static void PrintEntryPointCommand(MachO::entry_point_command ep) {
9058   outs() << "       cmd LC_MAIN\n";
9059   outs() << "   cmdsize " << ep.cmdsize;
9060   if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
9061     outs() << " Incorrect size\n";
9062   else
9063     outs() << "\n";
9064   outs() << "  entryoff " << ep.entryoff << "\n";
9065   outs() << " stacksize " << ep.stacksize << "\n";
9066 }
9067 
9068 static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
9069                                        uint32_t object_size) {
9070   outs() << "          cmd LC_ENCRYPTION_INFO\n";
9071   outs() << "      cmdsize " << ec.cmdsize;
9072   if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
9073     outs() << " Incorrect size\n";
9074   else
9075     outs() << "\n";
9076   outs() << "     cryptoff " << ec.cryptoff;
9077   if (ec.cryptoff > object_size)
9078     outs() << " (past end of file)\n";
9079   else
9080     outs() << "\n";
9081   outs() << "    cryptsize " << ec.cryptsize;
9082   if (ec.cryptsize > object_size)
9083     outs() << " (past end of file)\n";
9084   else
9085     outs() << "\n";
9086   outs() << "      cryptid " << ec.cryptid << "\n";
9087 }
9088 
9089 static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
9090                                          uint32_t object_size) {
9091   outs() << "          cmd LC_ENCRYPTION_INFO_64\n";
9092   outs() << "      cmdsize " << ec.cmdsize;
9093   if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
9094     outs() << " Incorrect size\n";
9095   else
9096     outs() << "\n";
9097   outs() << "     cryptoff " << ec.cryptoff;
9098   if (ec.cryptoff > object_size)
9099     outs() << " (past end of file)\n";
9100   else
9101     outs() << "\n";
9102   outs() << "    cryptsize " << ec.cryptsize;
9103   if (ec.cryptsize > object_size)
9104     outs() << " (past end of file)\n";
9105   else
9106     outs() << "\n";
9107   outs() << "      cryptid " << ec.cryptid << "\n";
9108   outs() << "          pad " << ec.pad << "\n";
9109 }
9110 
9111 static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
9112                                      const char *Ptr) {
9113   outs() << "     cmd LC_LINKER_OPTION\n";
9114   outs() << " cmdsize " << lo.cmdsize;
9115   if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
9116     outs() << " Incorrect size\n";
9117   else
9118     outs() << "\n";
9119   outs() << "   count " << lo.count << "\n";
9120   const char *string = Ptr + sizeof(struct MachO::linker_option_command);
9121   uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
9122   uint32_t i = 0;
9123   while (left > 0) {
9124     while (*string == '\0' && left > 0) {
9125       string++;
9126       left--;
9127     }
9128     if (left > 0) {
9129       i++;
9130       outs() << "  string #" << i << " " << format("%.*s\n", left, string);
9131       uint32_t NullPos = StringRef(string, left).find('\0');
9132       uint32_t len = std::min(NullPos, left) + 1;
9133       string += len;
9134       left -= len;
9135     }
9136   }
9137   if (lo.count != i)
9138     outs() << "   count " << lo.count << " does not match number of strings "
9139            << i << "\n";
9140 }
9141 
9142 static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
9143                                      const char *Ptr) {
9144   outs() << "          cmd LC_SUB_FRAMEWORK\n";
9145   outs() << "      cmdsize " << sub.cmdsize;
9146   if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
9147     outs() << " Incorrect size\n";
9148   else
9149     outs() << "\n";
9150   if (sub.umbrella < sub.cmdsize) {
9151     const char *P = Ptr + sub.umbrella;
9152     outs() << "     umbrella " << P << " (offset " << sub.umbrella << ")\n";
9153   } else {
9154     outs() << "     umbrella ?(bad offset " << sub.umbrella << ")\n";
9155   }
9156 }
9157 
9158 static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
9159                                     const char *Ptr) {
9160   outs() << "          cmd LC_SUB_UMBRELLA\n";
9161   outs() << "      cmdsize " << sub.cmdsize;
9162   if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
9163     outs() << " Incorrect size\n";
9164   else
9165     outs() << "\n";
9166   if (sub.sub_umbrella < sub.cmdsize) {
9167     const char *P = Ptr + sub.sub_umbrella;
9168     outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
9169   } else {
9170     outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
9171   }
9172 }
9173 
9174 static void PrintSubLibraryCommand(MachO::sub_library_command sub,
9175                                    const char *Ptr) {
9176   outs() << "          cmd LC_SUB_LIBRARY\n";
9177   outs() << "      cmdsize " << sub.cmdsize;
9178   if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
9179     outs() << " Incorrect size\n";
9180   else
9181     outs() << "\n";
9182   if (sub.sub_library < sub.cmdsize) {
9183     const char *P = Ptr + sub.sub_library;
9184     outs() << "  sub_library " << P << " (offset " << sub.sub_library << ")\n";
9185   } else {
9186     outs() << "  sub_library ?(bad offset " << sub.sub_library << ")\n";
9187   }
9188 }
9189 
9190 static void PrintSubClientCommand(MachO::sub_client_command sub,
9191                                   const char *Ptr) {
9192   outs() << "          cmd LC_SUB_CLIENT\n";
9193   outs() << "      cmdsize " << sub.cmdsize;
9194   if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
9195     outs() << " Incorrect size\n";
9196   else
9197     outs() << "\n";
9198   if (sub.client < sub.cmdsize) {
9199     const char *P = Ptr + sub.client;
9200     outs() << "       client " << P << " (offset " << sub.client << ")\n";
9201   } else {
9202     outs() << "       client ?(bad offset " << sub.client << ")\n";
9203   }
9204 }
9205 
9206 static void PrintRoutinesCommand(MachO::routines_command r) {
9207   outs() << "          cmd LC_ROUTINES\n";
9208   outs() << "      cmdsize " << r.cmdsize;
9209   if (r.cmdsize != sizeof(struct MachO::routines_command))
9210     outs() << " Incorrect size\n";
9211   else
9212     outs() << "\n";
9213   outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
9214   outs() << "  init_module " << r.init_module << "\n";
9215   outs() << "    reserved1 " << r.reserved1 << "\n";
9216   outs() << "    reserved2 " << r.reserved2 << "\n";
9217   outs() << "    reserved3 " << r.reserved3 << "\n";
9218   outs() << "    reserved4 " << r.reserved4 << "\n";
9219   outs() << "    reserved5 " << r.reserved5 << "\n";
9220   outs() << "    reserved6 " << r.reserved6 << "\n";
9221 }
9222 
9223 static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
9224   outs() << "          cmd LC_ROUTINES_64\n";
9225   outs() << "      cmdsize " << r.cmdsize;
9226   if (r.cmdsize != sizeof(struct MachO::routines_command_64))
9227     outs() << " Incorrect size\n";
9228   else
9229     outs() << "\n";
9230   outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
9231   outs() << "  init_module " << r.init_module << "\n";
9232   outs() << "    reserved1 " << r.reserved1 << "\n";
9233   outs() << "    reserved2 " << r.reserved2 << "\n";
9234   outs() << "    reserved3 " << r.reserved3 << "\n";
9235   outs() << "    reserved4 " << r.reserved4 << "\n";
9236   outs() << "    reserved5 " << r.reserved5 << "\n";
9237   outs() << "    reserved6 " << r.reserved6 << "\n";
9238 }
9239 
9240 static void Print_x86_thread_state32_t(MachO::x86_thread_state32_t &cpu32) {
9241   outs() << "\t    eax " << format("0x%08" PRIx32, cpu32.eax);
9242   outs() << " ebx    " << format("0x%08" PRIx32, cpu32.ebx);
9243   outs() << " ecx " << format("0x%08" PRIx32, cpu32.ecx);
9244   outs() << " edx " << format("0x%08" PRIx32, cpu32.edx) << "\n";
9245   outs() << "\t    edi " << format("0x%08" PRIx32, cpu32.edi);
9246   outs() << " esi    " << format("0x%08" PRIx32, cpu32.esi);
9247   outs() << " ebp " << format("0x%08" PRIx32, cpu32.ebp);
9248   outs() << " esp " << format("0x%08" PRIx32, cpu32.esp) << "\n";
9249   outs() << "\t    ss  " << format("0x%08" PRIx32, cpu32.ss);
9250   outs() << " eflags " << format("0x%08" PRIx32, cpu32.eflags);
9251   outs() << " eip " << format("0x%08" PRIx32, cpu32.eip);
9252   outs() << " cs  " << format("0x%08" PRIx32, cpu32.cs) << "\n";
9253   outs() << "\t    ds  " << format("0x%08" PRIx32, cpu32.ds);
9254   outs() << " es     " << format("0x%08" PRIx32, cpu32.es);
9255   outs() << " fs  " << format("0x%08" PRIx32, cpu32.fs);
9256   outs() << " gs  " << format("0x%08" PRIx32, cpu32.gs) << "\n";
9257 }
9258 
9259 static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
9260   outs() << "   rax  " << format("0x%016" PRIx64, cpu64.rax);
9261   outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
9262   outs() << " rcx  " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
9263   outs() << "   rdx  " << format("0x%016" PRIx64, cpu64.rdx);
9264   outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
9265   outs() << " rsi  " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
9266   outs() << "   rbp  " << format("0x%016" PRIx64, cpu64.rbp);
9267   outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
9268   outs() << " r8   " << format("0x%016" PRIx64, cpu64.r8) << "\n";
9269   outs() << "    r9  " << format("0x%016" PRIx64, cpu64.r9);
9270   outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
9271   outs() << " r11  " << format("0x%016" PRIx64, cpu64.r11) << "\n";
9272   outs() << "   r12  " << format("0x%016" PRIx64, cpu64.r12);
9273   outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
9274   outs() << " r14  " << format("0x%016" PRIx64, cpu64.r14) << "\n";
9275   outs() << "   r15  " << format("0x%016" PRIx64, cpu64.r15);
9276   outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
9277   outs() << "rflags  " << format("0x%016" PRIx64, cpu64.rflags);
9278   outs() << " cs  " << format("0x%016" PRIx64, cpu64.cs);
9279   outs() << " fs   " << format("0x%016" PRIx64, cpu64.fs) << "\n";
9280   outs() << "    gs  " << format("0x%016" PRIx64, cpu64.gs) << "\n";
9281 }
9282 
9283 static void Print_mmst_reg(MachO::mmst_reg_t &r) {
9284   uint32_t f;
9285   outs() << "\t      mmst_reg  ";
9286   for (f = 0; f < 10; f++)
9287     outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
9288   outs() << "\n";
9289   outs() << "\t      mmst_rsrv ";
9290   for (f = 0; f < 6; f++)
9291     outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
9292   outs() << "\n";
9293 }
9294 
9295 static void Print_xmm_reg(MachO::xmm_reg_t &r) {
9296   uint32_t f;
9297   outs() << "\t      xmm_reg ";
9298   for (f = 0; f < 16; f++)
9299     outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
9300   outs() << "\n";
9301 }
9302 
9303 static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
9304   outs() << "\t    fpu_reserved[0] " << fpu.fpu_reserved[0];
9305   outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
9306   outs() << "\t    control: invalid " << fpu.fpu_fcw.invalid;
9307   outs() << " denorm " << fpu.fpu_fcw.denorm;
9308   outs() << " zdiv " << fpu.fpu_fcw.zdiv;
9309   outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
9310   outs() << " undfl " << fpu.fpu_fcw.undfl;
9311   outs() << " precis " << fpu.fpu_fcw.precis << "\n";
9312   outs() << "\t\t     pc ";
9313   if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
9314     outs() << "FP_PREC_24B ";
9315   else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
9316     outs() << "FP_PREC_53B ";
9317   else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
9318     outs() << "FP_PREC_64B ";
9319   else
9320     outs() << fpu.fpu_fcw.pc << " ";
9321   outs() << "rc ";
9322   if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
9323     outs() << "FP_RND_NEAR ";
9324   else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
9325     outs() << "FP_RND_DOWN ";
9326   else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
9327     outs() << "FP_RND_UP ";
9328   else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
9329     outs() << "FP_CHOP ";
9330   outs() << "\n";
9331   outs() << "\t    status: invalid " << fpu.fpu_fsw.invalid;
9332   outs() << " denorm " << fpu.fpu_fsw.denorm;
9333   outs() << " zdiv " << fpu.fpu_fsw.zdiv;
9334   outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
9335   outs() << " undfl " << fpu.fpu_fsw.undfl;
9336   outs() << " precis " << fpu.fpu_fsw.precis;
9337   outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
9338   outs() << "\t            errsumm " << fpu.fpu_fsw.errsumm;
9339   outs() << " c0 " << fpu.fpu_fsw.c0;
9340   outs() << " c1 " << fpu.fpu_fsw.c1;
9341   outs() << " c2 " << fpu.fpu_fsw.c2;
9342   outs() << " tos " << fpu.fpu_fsw.tos;
9343   outs() << " c3 " << fpu.fpu_fsw.c3;
9344   outs() << " busy " << fpu.fpu_fsw.busy << "\n";
9345   outs() << "\t    fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
9346   outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
9347   outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
9348   outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
9349   outs() << "\t    fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
9350   outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
9351   outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
9352   outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
9353   outs() << "\t    fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
9354   outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
9355   outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
9356   outs() << "\n";
9357   outs() << "\t    fpu_stmm0:\n";
9358   Print_mmst_reg(fpu.fpu_stmm0);
9359   outs() << "\t    fpu_stmm1:\n";
9360   Print_mmst_reg(fpu.fpu_stmm1);
9361   outs() << "\t    fpu_stmm2:\n";
9362   Print_mmst_reg(fpu.fpu_stmm2);
9363   outs() << "\t    fpu_stmm3:\n";
9364   Print_mmst_reg(fpu.fpu_stmm3);
9365   outs() << "\t    fpu_stmm4:\n";
9366   Print_mmst_reg(fpu.fpu_stmm4);
9367   outs() << "\t    fpu_stmm5:\n";
9368   Print_mmst_reg(fpu.fpu_stmm5);
9369   outs() << "\t    fpu_stmm6:\n";
9370   Print_mmst_reg(fpu.fpu_stmm6);
9371   outs() << "\t    fpu_stmm7:\n";
9372   Print_mmst_reg(fpu.fpu_stmm7);
9373   outs() << "\t    fpu_xmm0:\n";
9374   Print_xmm_reg(fpu.fpu_xmm0);
9375   outs() << "\t    fpu_xmm1:\n";
9376   Print_xmm_reg(fpu.fpu_xmm1);
9377   outs() << "\t    fpu_xmm2:\n";
9378   Print_xmm_reg(fpu.fpu_xmm2);
9379   outs() << "\t    fpu_xmm3:\n";
9380   Print_xmm_reg(fpu.fpu_xmm3);
9381   outs() << "\t    fpu_xmm4:\n";
9382   Print_xmm_reg(fpu.fpu_xmm4);
9383   outs() << "\t    fpu_xmm5:\n";
9384   Print_xmm_reg(fpu.fpu_xmm5);
9385   outs() << "\t    fpu_xmm6:\n";
9386   Print_xmm_reg(fpu.fpu_xmm6);
9387   outs() << "\t    fpu_xmm7:\n";
9388   Print_xmm_reg(fpu.fpu_xmm7);
9389   outs() << "\t    fpu_xmm8:\n";
9390   Print_xmm_reg(fpu.fpu_xmm8);
9391   outs() << "\t    fpu_xmm9:\n";
9392   Print_xmm_reg(fpu.fpu_xmm9);
9393   outs() << "\t    fpu_xmm10:\n";
9394   Print_xmm_reg(fpu.fpu_xmm10);
9395   outs() << "\t    fpu_xmm11:\n";
9396   Print_xmm_reg(fpu.fpu_xmm11);
9397   outs() << "\t    fpu_xmm12:\n";
9398   Print_xmm_reg(fpu.fpu_xmm12);
9399   outs() << "\t    fpu_xmm13:\n";
9400   Print_xmm_reg(fpu.fpu_xmm13);
9401   outs() << "\t    fpu_xmm14:\n";
9402   Print_xmm_reg(fpu.fpu_xmm14);
9403   outs() << "\t    fpu_xmm15:\n";
9404   Print_xmm_reg(fpu.fpu_xmm15);
9405   outs() << "\t    fpu_rsrv4:\n";
9406   for (uint32_t f = 0; f < 6; f++) {
9407     outs() << "\t            ";
9408     for (uint32_t g = 0; g < 16; g++)
9409       outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
9410     outs() << "\n";
9411   }
9412   outs() << "\t    fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
9413   outs() << "\n";
9414 }
9415 
9416 static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
9417   outs() << "\t    trapno " << format("0x%08" PRIx32, exc64.trapno);
9418   outs() << " err " << format("0x%08" PRIx32, exc64.err);
9419   outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
9420 }
9421 
9422 static void Print_arm_thread_state32_t(MachO::arm_thread_state32_t &cpu32) {
9423   outs() << "\t    r0  " << format("0x%08" PRIx32, cpu32.r[0]);
9424   outs() << " r1     "   << format("0x%08" PRIx32, cpu32.r[1]);
9425   outs() << " r2  "      << format("0x%08" PRIx32, cpu32.r[2]);
9426   outs() << " r3  "      << format("0x%08" PRIx32, cpu32.r[3]) << "\n";
9427   outs() << "\t    r4  " << format("0x%08" PRIx32, cpu32.r[4]);
9428   outs() << " r5     "   << format("0x%08" PRIx32, cpu32.r[5]);
9429   outs() << " r6  "      << format("0x%08" PRIx32, cpu32.r[6]);
9430   outs() << " r7  "      << format("0x%08" PRIx32, cpu32.r[7]) << "\n";
9431   outs() << "\t    r8  " << format("0x%08" PRIx32, cpu32.r[8]);
9432   outs() << " r9     "   << format("0x%08" PRIx32, cpu32.r[9]);
9433   outs() << " r10 "      << format("0x%08" PRIx32, cpu32.r[10]);
9434   outs() << " r11 "      << format("0x%08" PRIx32, cpu32.r[11]) << "\n";
9435   outs() << "\t    r12 " << format("0x%08" PRIx32, cpu32.r[12]);
9436   outs() << " sp     "   << format("0x%08" PRIx32, cpu32.sp);
9437   outs() << " lr  "      << format("0x%08" PRIx32, cpu32.lr);
9438   outs() << " pc  "      << format("0x%08" PRIx32, cpu32.pc) << "\n";
9439   outs() << "\t   cpsr " << format("0x%08" PRIx32, cpu32.cpsr) << "\n";
9440 }
9441 
9442 static void Print_arm_thread_state64_t(MachO::arm_thread_state64_t &cpu64) {
9443   outs() << "\t    x0  " << format("0x%016" PRIx64, cpu64.x[0]);
9444   outs() << " x1  "      << format("0x%016" PRIx64, cpu64.x[1]);
9445   outs() << " x2  "      << format("0x%016" PRIx64, cpu64.x[2]) << "\n";
9446   outs() << "\t    x3  " << format("0x%016" PRIx64, cpu64.x[3]);
9447   outs() << " x4  "      << format("0x%016" PRIx64, cpu64.x[4]);
9448   outs() << " x5  "      << format("0x%016" PRIx64, cpu64.x[5]) << "\n";
9449   outs() << "\t    x6  " << format("0x%016" PRIx64, cpu64.x[6]);
9450   outs() << " x7  "      << format("0x%016" PRIx64, cpu64.x[7]);
9451   outs() << " x8  "      << format("0x%016" PRIx64, cpu64.x[8]) << "\n";
9452   outs() << "\t    x9  " << format("0x%016" PRIx64, cpu64.x[9]);
9453   outs() << " x10 "      << format("0x%016" PRIx64, cpu64.x[10]);
9454   outs() << " x11 "      << format("0x%016" PRIx64, cpu64.x[11]) << "\n";
9455   outs() << "\t    x12 " << format("0x%016" PRIx64, cpu64.x[12]);
9456   outs() << " x13 "      << format("0x%016" PRIx64, cpu64.x[13]);
9457   outs() << " x14 "      << format("0x%016" PRIx64, cpu64.x[14]) << "\n";
9458   outs() << "\t    x15 " << format("0x%016" PRIx64, cpu64.x[15]);
9459   outs() << " x16 "      << format("0x%016" PRIx64, cpu64.x[16]);
9460   outs() << " x17 "      << format("0x%016" PRIx64, cpu64.x[17]) << "\n";
9461   outs() << "\t    x18 " << format("0x%016" PRIx64, cpu64.x[18]);
9462   outs() << " x19 "      << format("0x%016" PRIx64, cpu64.x[19]);
9463   outs() << " x20 "      << format("0x%016" PRIx64, cpu64.x[20]) << "\n";
9464   outs() << "\t    x21 " << format("0x%016" PRIx64, cpu64.x[21]);
9465   outs() << " x22 "      << format("0x%016" PRIx64, cpu64.x[22]);
9466   outs() << " x23 "      << format("0x%016" PRIx64, cpu64.x[23]) << "\n";
9467   outs() << "\t    x24 " << format("0x%016" PRIx64, cpu64.x[24]);
9468   outs() << " x25 "      << format("0x%016" PRIx64, cpu64.x[25]);
9469   outs() << " x26 "      << format("0x%016" PRIx64, cpu64.x[26]) << "\n";
9470   outs() << "\t    x27 " << format("0x%016" PRIx64, cpu64.x[27]);
9471   outs() << " x28 "      << format("0x%016" PRIx64, cpu64.x[28]);
9472   outs() << "  fp "      << format("0x%016" PRIx64, cpu64.fp) << "\n";
9473   outs() << "\t     lr " << format("0x%016" PRIx64, cpu64.lr);
9474   outs() << " sp  "      << format("0x%016" PRIx64, cpu64.sp);
9475   outs() << "  pc "      << format("0x%016" PRIx64, cpu64.pc) << "\n";
9476   outs() << "\t   cpsr " << format("0x%08"  PRIx32, cpu64.cpsr) << "\n";
9477 }
9478 
9479 static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
9480                                bool isLittleEndian, uint32_t cputype) {
9481   if (t.cmd == MachO::LC_THREAD)
9482     outs() << "        cmd LC_THREAD\n";
9483   else if (t.cmd == MachO::LC_UNIXTHREAD)
9484     outs() << "        cmd LC_UNIXTHREAD\n";
9485   else
9486     outs() << "        cmd " << t.cmd << " (unknown)\n";
9487   outs() << "    cmdsize " << t.cmdsize;
9488   if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
9489     outs() << " Incorrect size\n";
9490   else
9491     outs() << "\n";
9492 
9493   const char *begin = Ptr + sizeof(struct MachO::thread_command);
9494   const char *end = Ptr + t.cmdsize;
9495   uint32_t flavor, count, left;
9496   if (cputype == MachO::CPU_TYPE_I386) {
9497     while (begin < end) {
9498       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9499         memcpy((char *)&flavor, begin, sizeof(uint32_t));
9500         begin += sizeof(uint32_t);
9501       } else {
9502         flavor = 0;
9503         begin = end;
9504       }
9505       if (isLittleEndian != sys::IsLittleEndianHost)
9506         sys::swapByteOrder(flavor);
9507       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9508         memcpy((char *)&count, begin, sizeof(uint32_t));
9509         begin += sizeof(uint32_t);
9510       } else {
9511         count = 0;
9512         begin = end;
9513       }
9514       if (isLittleEndian != sys::IsLittleEndianHost)
9515         sys::swapByteOrder(count);
9516       if (flavor == MachO::x86_THREAD_STATE32) {
9517         outs() << "     flavor i386_THREAD_STATE\n";
9518         if (count == MachO::x86_THREAD_STATE32_COUNT)
9519           outs() << "      count i386_THREAD_STATE_COUNT\n";
9520         else
9521           outs() << "      count " << count
9522                  << " (not x86_THREAD_STATE32_COUNT)\n";
9523         MachO::x86_thread_state32_t cpu32;
9524         left = end - begin;
9525         if (left >= sizeof(MachO::x86_thread_state32_t)) {
9526           memcpy(&cpu32, begin, sizeof(MachO::x86_thread_state32_t));
9527           begin += sizeof(MachO::x86_thread_state32_t);
9528         } else {
9529           memset(&cpu32, '\0', sizeof(MachO::x86_thread_state32_t));
9530           memcpy(&cpu32, begin, left);
9531           begin += left;
9532         }
9533         if (isLittleEndian != sys::IsLittleEndianHost)
9534           swapStruct(cpu32);
9535         Print_x86_thread_state32_t(cpu32);
9536       } else if (flavor == MachO::x86_THREAD_STATE) {
9537         outs() << "     flavor x86_THREAD_STATE\n";
9538         if (count == MachO::x86_THREAD_STATE_COUNT)
9539           outs() << "      count x86_THREAD_STATE_COUNT\n";
9540         else
9541           outs() << "      count " << count
9542                  << " (not x86_THREAD_STATE_COUNT)\n";
9543         struct MachO::x86_thread_state_t ts;
9544         left = end - begin;
9545         if (left >= sizeof(MachO::x86_thread_state_t)) {
9546           memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
9547           begin += sizeof(MachO::x86_thread_state_t);
9548         } else {
9549           memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
9550           memcpy(&ts, begin, left);
9551           begin += left;
9552         }
9553         if (isLittleEndian != sys::IsLittleEndianHost)
9554           swapStruct(ts);
9555         if (ts.tsh.flavor == MachO::x86_THREAD_STATE32) {
9556           outs() << "\t    tsh.flavor x86_THREAD_STATE32 ";
9557           if (ts.tsh.count == MachO::x86_THREAD_STATE32_COUNT)
9558             outs() << "tsh.count x86_THREAD_STATE32_COUNT\n";
9559           else
9560             outs() << "tsh.count " << ts.tsh.count
9561                    << " (not x86_THREAD_STATE32_COUNT\n";
9562           Print_x86_thread_state32_t(ts.uts.ts32);
9563         } else {
9564           outs() << "\t    tsh.flavor " << ts.tsh.flavor << "  tsh.count "
9565                  << ts.tsh.count << "\n";
9566         }
9567       } else {
9568         outs() << "     flavor " << flavor << " (unknown)\n";
9569         outs() << "      count " << count << "\n";
9570         outs() << "      state (unknown)\n";
9571         begin += count * sizeof(uint32_t);
9572       }
9573     }
9574   } else if (cputype == MachO::CPU_TYPE_X86_64) {
9575     while (begin < end) {
9576       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9577         memcpy((char *)&flavor, begin, sizeof(uint32_t));
9578         begin += sizeof(uint32_t);
9579       } else {
9580         flavor = 0;
9581         begin = end;
9582       }
9583       if (isLittleEndian != sys::IsLittleEndianHost)
9584         sys::swapByteOrder(flavor);
9585       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9586         memcpy((char *)&count, begin, sizeof(uint32_t));
9587         begin += sizeof(uint32_t);
9588       } else {
9589         count = 0;
9590         begin = end;
9591       }
9592       if (isLittleEndian != sys::IsLittleEndianHost)
9593         sys::swapByteOrder(count);
9594       if (flavor == MachO::x86_THREAD_STATE64) {
9595         outs() << "     flavor x86_THREAD_STATE64\n";
9596         if (count == MachO::x86_THREAD_STATE64_COUNT)
9597           outs() << "      count x86_THREAD_STATE64_COUNT\n";
9598         else
9599           outs() << "      count " << count
9600                  << " (not x86_THREAD_STATE64_COUNT)\n";
9601         MachO::x86_thread_state64_t cpu64;
9602         left = end - begin;
9603         if (left >= sizeof(MachO::x86_thread_state64_t)) {
9604           memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
9605           begin += sizeof(MachO::x86_thread_state64_t);
9606         } else {
9607           memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
9608           memcpy(&cpu64, begin, left);
9609           begin += left;
9610         }
9611         if (isLittleEndian != sys::IsLittleEndianHost)
9612           swapStruct(cpu64);
9613         Print_x86_thread_state64_t(cpu64);
9614       } else if (flavor == MachO::x86_THREAD_STATE) {
9615         outs() << "     flavor x86_THREAD_STATE\n";
9616         if (count == MachO::x86_THREAD_STATE_COUNT)
9617           outs() << "      count x86_THREAD_STATE_COUNT\n";
9618         else
9619           outs() << "      count " << count
9620                  << " (not x86_THREAD_STATE_COUNT)\n";
9621         struct MachO::x86_thread_state_t ts;
9622         left = end - begin;
9623         if (left >= sizeof(MachO::x86_thread_state_t)) {
9624           memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
9625           begin += sizeof(MachO::x86_thread_state_t);
9626         } else {
9627           memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
9628           memcpy(&ts, begin, left);
9629           begin += left;
9630         }
9631         if (isLittleEndian != sys::IsLittleEndianHost)
9632           swapStruct(ts);
9633         if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
9634           outs() << "\t    tsh.flavor x86_THREAD_STATE64 ";
9635           if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
9636             outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
9637           else
9638             outs() << "tsh.count " << ts.tsh.count
9639                    << " (not x86_THREAD_STATE64_COUNT\n";
9640           Print_x86_thread_state64_t(ts.uts.ts64);
9641         } else {
9642           outs() << "\t    tsh.flavor " << ts.tsh.flavor << "  tsh.count "
9643                  << ts.tsh.count << "\n";
9644         }
9645       } else if (flavor == MachO::x86_FLOAT_STATE) {
9646         outs() << "     flavor x86_FLOAT_STATE\n";
9647         if (count == MachO::x86_FLOAT_STATE_COUNT)
9648           outs() << "      count x86_FLOAT_STATE_COUNT\n";
9649         else
9650           outs() << "      count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
9651         struct MachO::x86_float_state_t fs;
9652         left = end - begin;
9653         if (left >= sizeof(MachO::x86_float_state_t)) {
9654           memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
9655           begin += sizeof(MachO::x86_float_state_t);
9656         } else {
9657           memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
9658           memcpy(&fs, begin, left);
9659           begin += left;
9660         }
9661         if (isLittleEndian != sys::IsLittleEndianHost)
9662           swapStruct(fs);
9663         if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
9664           outs() << "\t    fsh.flavor x86_FLOAT_STATE64 ";
9665           if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
9666             outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
9667           else
9668             outs() << "fsh.count " << fs.fsh.count
9669                    << " (not x86_FLOAT_STATE64_COUNT\n";
9670           Print_x86_float_state_t(fs.ufs.fs64);
9671         } else {
9672           outs() << "\t    fsh.flavor " << fs.fsh.flavor << "  fsh.count "
9673                  << fs.fsh.count << "\n";
9674         }
9675       } else if (flavor == MachO::x86_EXCEPTION_STATE) {
9676         outs() << "     flavor x86_EXCEPTION_STATE\n";
9677         if (count == MachO::x86_EXCEPTION_STATE_COUNT)
9678           outs() << "      count x86_EXCEPTION_STATE_COUNT\n";
9679         else
9680           outs() << "      count " << count
9681                  << " (not x86_EXCEPTION_STATE_COUNT)\n";
9682         struct MachO::x86_exception_state_t es;
9683         left = end - begin;
9684         if (left >= sizeof(MachO::x86_exception_state_t)) {
9685           memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
9686           begin += sizeof(MachO::x86_exception_state_t);
9687         } else {
9688           memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
9689           memcpy(&es, begin, left);
9690           begin += left;
9691         }
9692         if (isLittleEndian != sys::IsLittleEndianHost)
9693           swapStruct(es);
9694         if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
9695           outs() << "\t    esh.flavor x86_EXCEPTION_STATE64\n";
9696           if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
9697             outs() << "\t    esh.count x86_EXCEPTION_STATE64_COUNT\n";
9698           else
9699             outs() << "\t    esh.count " << es.esh.count
9700                    << " (not x86_EXCEPTION_STATE64_COUNT\n";
9701           Print_x86_exception_state_t(es.ues.es64);
9702         } else {
9703           outs() << "\t    esh.flavor " << es.esh.flavor << "  esh.count "
9704                  << es.esh.count << "\n";
9705         }
9706       } else if (flavor == MachO::x86_EXCEPTION_STATE64) {
9707         outs() << "     flavor x86_EXCEPTION_STATE64\n";
9708         if (count == MachO::x86_EXCEPTION_STATE64_COUNT)
9709           outs() << "      count x86_EXCEPTION_STATE64_COUNT\n";
9710         else
9711           outs() << "      count " << count
9712                  << " (not x86_EXCEPTION_STATE64_COUNT)\n";
9713         struct MachO::x86_exception_state64_t es64;
9714         left = end - begin;
9715         if (left >= sizeof(MachO::x86_exception_state64_t)) {
9716           memcpy(&es64, begin, sizeof(MachO::x86_exception_state64_t));
9717           begin += sizeof(MachO::x86_exception_state64_t);
9718         } else {
9719           memset(&es64, '\0', sizeof(MachO::x86_exception_state64_t));
9720           memcpy(&es64, begin, left);
9721           begin += left;
9722         }
9723         if (isLittleEndian != sys::IsLittleEndianHost)
9724           swapStruct(es64);
9725         Print_x86_exception_state_t(es64);
9726       } else {
9727         outs() << "     flavor " << flavor << " (unknown)\n";
9728         outs() << "      count " << count << "\n";
9729         outs() << "      state (unknown)\n";
9730         begin += count * sizeof(uint32_t);
9731       }
9732     }
9733   } else if (cputype == MachO::CPU_TYPE_ARM) {
9734     while (begin < end) {
9735       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9736         memcpy((char *)&flavor, begin, sizeof(uint32_t));
9737         begin += sizeof(uint32_t);
9738       } else {
9739         flavor = 0;
9740         begin = end;
9741       }
9742       if (isLittleEndian != sys::IsLittleEndianHost)
9743         sys::swapByteOrder(flavor);
9744       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9745         memcpy((char *)&count, begin, sizeof(uint32_t));
9746         begin += sizeof(uint32_t);
9747       } else {
9748         count = 0;
9749         begin = end;
9750       }
9751       if (isLittleEndian != sys::IsLittleEndianHost)
9752         sys::swapByteOrder(count);
9753       if (flavor == MachO::ARM_THREAD_STATE) {
9754         outs() << "     flavor ARM_THREAD_STATE\n";
9755         if (count == MachO::ARM_THREAD_STATE_COUNT)
9756           outs() << "      count ARM_THREAD_STATE_COUNT\n";
9757         else
9758           outs() << "      count " << count
9759                  << " (not ARM_THREAD_STATE_COUNT)\n";
9760         MachO::arm_thread_state32_t cpu32;
9761         left = end - begin;
9762         if (left >= sizeof(MachO::arm_thread_state32_t)) {
9763           memcpy(&cpu32, begin, sizeof(MachO::arm_thread_state32_t));
9764           begin += sizeof(MachO::arm_thread_state32_t);
9765         } else {
9766           memset(&cpu32, '\0', sizeof(MachO::arm_thread_state32_t));
9767           memcpy(&cpu32, begin, left);
9768           begin += left;
9769         }
9770         if (isLittleEndian != sys::IsLittleEndianHost)
9771           swapStruct(cpu32);
9772         Print_arm_thread_state32_t(cpu32);
9773       } else {
9774         outs() << "     flavor " << flavor << " (unknown)\n";
9775         outs() << "      count " << count << "\n";
9776         outs() << "      state (unknown)\n";
9777         begin += count * sizeof(uint32_t);
9778       }
9779     }
9780   } else if (cputype == MachO::CPU_TYPE_ARM64 ||
9781              cputype == MachO::CPU_TYPE_ARM64_32) {
9782     while (begin < end) {
9783       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9784         memcpy((char *)&flavor, begin, sizeof(uint32_t));
9785         begin += sizeof(uint32_t);
9786       } else {
9787         flavor = 0;
9788         begin = end;
9789       }
9790       if (isLittleEndian != sys::IsLittleEndianHost)
9791         sys::swapByteOrder(flavor);
9792       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9793         memcpy((char *)&count, begin, sizeof(uint32_t));
9794         begin += sizeof(uint32_t);
9795       } else {
9796         count = 0;
9797         begin = end;
9798       }
9799       if (isLittleEndian != sys::IsLittleEndianHost)
9800         sys::swapByteOrder(count);
9801       if (flavor == MachO::ARM_THREAD_STATE64) {
9802         outs() << "     flavor ARM_THREAD_STATE64\n";
9803         if (count == MachO::ARM_THREAD_STATE64_COUNT)
9804           outs() << "      count ARM_THREAD_STATE64_COUNT\n";
9805         else
9806           outs() << "      count " << count
9807                  << " (not ARM_THREAD_STATE64_COUNT)\n";
9808         MachO::arm_thread_state64_t cpu64;
9809         left = end - begin;
9810         if (left >= sizeof(MachO::arm_thread_state64_t)) {
9811           memcpy(&cpu64, begin, sizeof(MachO::arm_thread_state64_t));
9812           begin += sizeof(MachO::arm_thread_state64_t);
9813         } else {
9814           memset(&cpu64, '\0', sizeof(MachO::arm_thread_state64_t));
9815           memcpy(&cpu64, begin, left);
9816           begin += left;
9817         }
9818         if (isLittleEndian != sys::IsLittleEndianHost)
9819           swapStruct(cpu64);
9820         Print_arm_thread_state64_t(cpu64);
9821       } else {
9822         outs() << "     flavor " << flavor << " (unknown)\n";
9823         outs() << "      count " << count << "\n";
9824         outs() << "      state (unknown)\n";
9825         begin += count * sizeof(uint32_t);
9826       }
9827     }
9828   } else {
9829     while (begin < end) {
9830       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9831         memcpy((char *)&flavor, begin, sizeof(uint32_t));
9832         begin += sizeof(uint32_t);
9833       } else {
9834         flavor = 0;
9835         begin = end;
9836       }
9837       if (isLittleEndian != sys::IsLittleEndianHost)
9838         sys::swapByteOrder(flavor);
9839       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9840         memcpy((char *)&count, begin, sizeof(uint32_t));
9841         begin += sizeof(uint32_t);
9842       } else {
9843         count = 0;
9844         begin = end;
9845       }
9846       if (isLittleEndian != sys::IsLittleEndianHost)
9847         sys::swapByteOrder(count);
9848       outs() << "     flavor " << flavor << "\n";
9849       outs() << "      count " << count << "\n";
9850       outs() << "      state (Unknown cputype/cpusubtype)\n";
9851       begin += count * sizeof(uint32_t);
9852     }
9853   }
9854 }
9855 
9856 static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
9857   if (dl.cmd == MachO::LC_ID_DYLIB)
9858     outs() << "          cmd LC_ID_DYLIB\n";
9859   else if (dl.cmd == MachO::LC_LOAD_DYLIB)
9860     outs() << "          cmd LC_LOAD_DYLIB\n";
9861   else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
9862     outs() << "          cmd LC_LOAD_WEAK_DYLIB\n";
9863   else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
9864     outs() << "          cmd LC_REEXPORT_DYLIB\n";
9865   else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
9866     outs() << "          cmd LC_LAZY_LOAD_DYLIB\n";
9867   else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
9868     outs() << "          cmd LC_LOAD_UPWARD_DYLIB\n";
9869   else
9870     outs() << "          cmd " << dl.cmd << " (unknown)\n";
9871   outs() << "      cmdsize " << dl.cmdsize;
9872   if (dl.cmdsize < sizeof(struct MachO::dylib_command))
9873     outs() << " Incorrect size\n";
9874   else
9875     outs() << "\n";
9876   if (dl.dylib.name < dl.cmdsize) {
9877     const char *P = (const char *)(Ptr) + dl.dylib.name;
9878     outs() << "         name " << P << " (offset " << dl.dylib.name << ")\n";
9879   } else {
9880     outs() << "         name ?(bad offset " << dl.dylib.name << ")\n";
9881   }
9882   outs() << "   time stamp " << dl.dylib.timestamp << " ";
9883   time_t t = dl.dylib.timestamp;
9884   outs() << ctime(&t);
9885   outs() << "      current version ";
9886   if (dl.dylib.current_version == 0xffffffff)
9887     outs() << "n/a\n";
9888   else
9889     outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
9890            << ((dl.dylib.current_version >> 8) & 0xff) << "."
9891            << (dl.dylib.current_version & 0xff) << "\n";
9892   outs() << "compatibility version ";
9893   if (dl.dylib.compatibility_version == 0xffffffff)
9894     outs() << "n/a\n";
9895   else
9896     outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
9897            << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
9898            << (dl.dylib.compatibility_version & 0xff) << "\n";
9899 }
9900 
9901 static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
9902                                      uint32_t object_size) {
9903   if (ld.cmd == MachO::LC_CODE_SIGNATURE)
9904     outs() << "      cmd LC_CODE_SIGNATURE\n";
9905   else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
9906     outs() << "      cmd LC_SEGMENT_SPLIT_INFO\n";
9907   else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
9908     outs() << "      cmd LC_FUNCTION_STARTS\n";
9909   else if (ld.cmd == MachO::LC_DATA_IN_CODE)
9910     outs() << "      cmd LC_DATA_IN_CODE\n";
9911   else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
9912     outs() << "      cmd LC_DYLIB_CODE_SIGN_DRS\n";
9913   else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
9914     outs() << "      cmd LC_LINKER_OPTIMIZATION_HINT\n";
9915   else
9916     outs() << "      cmd " << ld.cmd << " (?)\n";
9917   outs() << "  cmdsize " << ld.cmdsize;
9918   if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
9919     outs() << " Incorrect size\n";
9920   else
9921     outs() << "\n";
9922   outs() << "  dataoff " << ld.dataoff;
9923   if (ld.dataoff > object_size)
9924     outs() << " (past end of file)\n";
9925   else
9926     outs() << "\n";
9927   outs() << " datasize " << ld.datasize;
9928   uint64_t big_size = ld.dataoff;
9929   big_size += ld.datasize;
9930   if (big_size > object_size)
9931     outs() << " (past end of file)\n";
9932   else
9933     outs() << "\n";
9934 }
9935 
9936 static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
9937                               uint32_t cputype, bool verbose) {
9938   StringRef Buf = Obj->getData();
9939   unsigned Index = 0;
9940   for (const auto &Command : Obj->load_commands()) {
9941     outs() << "Load command " << Index++ << "\n";
9942     if (Command.C.cmd == MachO::LC_SEGMENT) {
9943       MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
9944       const char *sg_segname = SLC.segname;
9945       PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
9946                           SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
9947                           SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
9948                           verbose);
9949       for (unsigned j = 0; j < SLC.nsects; j++) {
9950         MachO::section S = Obj->getSection(Command, j);
9951         PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
9952                      S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
9953                      SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
9954       }
9955     } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
9956       MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
9957       const char *sg_segname = SLC_64.segname;
9958       PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
9959                           SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
9960                           SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
9961                           SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
9962       for (unsigned j = 0; j < SLC_64.nsects; j++) {
9963         MachO::section_64 S_64 = Obj->getSection64(Command, j);
9964         PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
9965                      S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
9966                      S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
9967                      sg_segname, filetype, Buf.size(), verbose);
9968       }
9969     } else if (Command.C.cmd == MachO::LC_SYMTAB) {
9970       MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
9971       PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
9972     } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
9973       MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
9974       MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
9975       PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
9976                                Obj->is64Bit());
9977     } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
9978                Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
9979       MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
9980       PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
9981     } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
9982                Command.C.cmd == MachO::LC_ID_DYLINKER ||
9983                Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
9984       MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
9985       PrintDyldLoadCommand(Dyld, Command.Ptr);
9986     } else if (Command.C.cmd == MachO::LC_UUID) {
9987       MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
9988       PrintUuidLoadCommand(Uuid);
9989     } else if (Command.C.cmd == MachO::LC_RPATH) {
9990       MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
9991       PrintRpathLoadCommand(Rpath, Command.Ptr);
9992     } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
9993                Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS ||
9994                Command.C.cmd == MachO::LC_VERSION_MIN_TVOS ||
9995                Command.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) {
9996       MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
9997       PrintVersionMinLoadCommand(Vd);
9998     } else if (Command.C.cmd == MachO::LC_NOTE) {
9999       MachO::note_command Nt = Obj->getNoteLoadCommand(Command);
10000       PrintNoteLoadCommand(Nt);
10001     } else if (Command.C.cmd == MachO::LC_BUILD_VERSION) {
10002       MachO::build_version_command Bv =
10003           Obj->getBuildVersionLoadCommand(Command);
10004       PrintBuildVersionLoadCommand(Obj, Bv);
10005     } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
10006       MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
10007       PrintSourceVersionCommand(Sd);
10008     } else if (Command.C.cmd == MachO::LC_MAIN) {
10009       MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
10010       PrintEntryPointCommand(Ep);
10011     } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
10012       MachO::encryption_info_command Ei =
10013           Obj->getEncryptionInfoCommand(Command);
10014       PrintEncryptionInfoCommand(Ei, Buf.size());
10015     } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
10016       MachO::encryption_info_command_64 Ei =
10017           Obj->getEncryptionInfoCommand64(Command);
10018       PrintEncryptionInfoCommand64(Ei, Buf.size());
10019     } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
10020       MachO::linker_option_command Lo =
10021           Obj->getLinkerOptionLoadCommand(Command);
10022       PrintLinkerOptionCommand(Lo, Command.Ptr);
10023     } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
10024       MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
10025       PrintSubFrameworkCommand(Sf, Command.Ptr);
10026     } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
10027       MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
10028       PrintSubUmbrellaCommand(Sf, Command.Ptr);
10029     } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
10030       MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
10031       PrintSubLibraryCommand(Sl, Command.Ptr);
10032     } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
10033       MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
10034       PrintSubClientCommand(Sc, Command.Ptr);
10035     } else if (Command.C.cmd == MachO::LC_ROUTINES) {
10036       MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
10037       PrintRoutinesCommand(Rc);
10038     } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
10039       MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
10040       PrintRoutinesCommand64(Rc);
10041     } else if (Command.C.cmd == MachO::LC_THREAD ||
10042                Command.C.cmd == MachO::LC_UNIXTHREAD) {
10043       MachO::thread_command Tc = Obj->getThreadCommand(Command);
10044       PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
10045     } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
10046                Command.C.cmd == MachO::LC_ID_DYLIB ||
10047                Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
10048                Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
10049                Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
10050                Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
10051       MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
10052       PrintDylibCommand(Dl, Command.Ptr);
10053     } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
10054                Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
10055                Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
10056                Command.C.cmd == MachO::LC_DATA_IN_CODE ||
10057                Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
10058                Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
10059       MachO::linkedit_data_command Ld =
10060           Obj->getLinkeditDataLoadCommand(Command);
10061       PrintLinkEditDataCommand(Ld, Buf.size());
10062     } else {
10063       outs() << "      cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
10064              << ")\n";
10065       outs() << "  cmdsize " << Command.C.cmdsize << "\n";
10066       // TODO: get and print the raw bytes of the load command.
10067     }
10068     // TODO: print all the other kinds of load commands.
10069   }
10070 }
10071 
10072 static void PrintMachHeader(const MachOObjectFile *Obj, bool verbose) {
10073   if (Obj->is64Bit()) {
10074     MachO::mach_header_64 H_64;
10075     H_64 = Obj->getHeader64();
10076     PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
10077                     H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
10078   } else {
10079     MachO::mach_header H;
10080     H = Obj->getHeader();
10081     PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
10082                     H.sizeofcmds, H.flags, verbose);
10083   }
10084 }
10085 
10086 void printMachOFileHeader(const object::ObjectFile *Obj) {
10087   const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
10088   PrintMachHeader(file, !NonVerbose);
10089 }
10090 
10091 void printMachOLoadCommands(const object::ObjectFile *Obj) {
10092   const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
10093   uint32_t filetype = 0;
10094   uint32_t cputype = 0;
10095   if (file->is64Bit()) {
10096     MachO::mach_header_64 H_64;
10097     H_64 = file->getHeader64();
10098     filetype = H_64.filetype;
10099     cputype = H_64.cputype;
10100   } else {
10101     MachO::mach_header H;
10102     H = file->getHeader();
10103     filetype = H.filetype;
10104     cputype = H.cputype;
10105   }
10106   PrintLoadCommands(file, filetype, cputype, !NonVerbose);
10107 }
10108 
10109 //===----------------------------------------------------------------------===//
10110 // export trie dumping
10111 //===----------------------------------------------------------------------===//
10112 
10113 void printMachOExportsTrie(const object::MachOObjectFile *Obj) {
10114   uint64_t BaseSegmentAddress = 0;
10115   for (const auto &Command : Obj->load_commands()) {
10116     if (Command.C.cmd == MachO::LC_SEGMENT) {
10117       MachO::segment_command Seg = Obj->getSegmentLoadCommand(Command);
10118       if (Seg.fileoff == 0 && Seg.filesize != 0) {
10119         BaseSegmentAddress = Seg.vmaddr;
10120         break;
10121       }
10122     } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
10123       MachO::segment_command_64 Seg = Obj->getSegment64LoadCommand(Command);
10124       if (Seg.fileoff == 0 && Seg.filesize != 0) {
10125         BaseSegmentAddress = Seg.vmaddr;
10126         break;
10127       }
10128     }
10129   }
10130   Error Err = Error::success();
10131   for (const object::ExportEntry &Entry : Obj->exports(Err)) {
10132     uint64_t Flags = Entry.flags();
10133     bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
10134     bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
10135     bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
10136                         MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
10137     bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
10138                 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
10139     bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
10140     if (ReExport)
10141       outs() << "[re-export] ";
10142     else
10143       outs() << format("0x%08llX  ",
10144                        Entry.address() + BaseSegmentAddress);
10145     outs() << Entry.name();
10146     if (WeakDef || ThreadLocal || Resolver || Abs) {
10147       bool NeedsComma = false;
10148       outs() << " [";
10149       if (WeakDef) {
10150         outs() << "weak_def";
10151         NeedsComma = true;
10152       }
10153       if (ThreadLocal) {
10154         if (NeedsComma)
10155           outs() << ", ";
10156         outs() << "per-thread";
10157         NeedsComma = true;
10158       }
10159       if (Abs) {
10160         if (NeedsComma)
10161           outs() << ", ";
10162         outs() << "absolute";
10163         NeedsComma = true;
10164       }
10165       if (Resolver) {
10166         if (NeedsComma)
10167           outs() << ", ";
10168         outs() << format("resolver=0x%08llX", Entry.other());
10169         NeedsComma = true;
10170       }
10171       outs() << "]";
10172     }
10173     if (ReExport) {
10174       StringRef DylibName = "unknown";
10175       int Ordinal = Entry.other() - 1;
10176       Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
10177       if (Entry.otherName().empty())
10178         outs() << " (from " << DylibName << ")";
10179       else
10180         outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
10181     }
10182     outs() << "\n";
10183   }
10184   if (Err)
10185     report_error(std::move(Err), Obj->getFileName());
10186 }
10187 
10188 //===----------------------------------------------------------------------===//
10189 // rebase table dumping
10190 //===----------------------------------------------------------------------===//
10191 
10192 void printMachORebaseTable(object::MachOObjectFile *Obj) {
10193   outs() << "segment  section            address     type\n";
10194   Error Err = Error::success();
10195   for (const object::MachORebaseEntry &Entry : Obj->rebaseTable(Err)) {
10196     StringRef SegmentName = Entry.segmentName();
10197     StringRef SectionName = Entry.sectionName();
10198     uint64_t Address = Entry.address();
10199 
10200     // Table lines look like: __DATA  __nl_symbol_ptr  0x0000F00C  pointer
10201     outs() << format("%-8s %-18s 0x%08" PRIX64 "  %s\n",
10202                      SegmentName.str().c_str(), SectionName.str().c_str(),
10203                      Address, Entry.typeName().str().c_str());
10204   }
10205   if (Err)
10206     report_error(std::move(Err), Obj->getFileName());
10207 }
10208 
10209 static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
10210   StringRef DylibName;
10211   switch (Ordinal) {
10212   case MachO::BIND_SPECIAL_DYLIB_SELF:
10213     return "this-image";
10214   case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
10215     return "main-executable";
10216   case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
10217     return "flat-namespace";
10218   default:
10219     if (Ordinal > 0) {
10220       std::error_code EC =
10221           Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
10222       if (EC)
10223         return "<<bad library ordinal>>";
10224       return DylibName;
10225     }
10226   }
10227   return "<<unknown special ordinal>>";
10228 }
10229 
10230 //===----------------------------------------------------------------------===//
10231 // bind table dumping
10232 //===----------------------------------------------------------------------===//
10233 
10234 void printMachOBindTable(object::MachOObjectFile *Obj) {
10235   // Build table of sections so names can used in final output.
10236   outs() << "segment  section            address    type       "
10237             "addend dylib            symbol\n";
10238   Error Err = Error::success();
10239   for (const object::MachOBindEntry &Entry : Obj->bindTable(Err)) {
10240     StringRef SegmentName = Entry.segmentName();
10241     StringRef SectionName = Entry.sectionName();
10242     uint64_t Address = Entry.address();
10243 
10244     // Table lines look like:
10245     //  __DATA  __got  0x00012010    pointer   0 libSystem ___stack_chk_guard
10246     StringRef Attr;
10247     if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
10248       Attr = " (weak_import)";
10249     outs() << left_justify(SegmentName, 8) << " "
10250            << left_justify(SectionName, 18) << " "
10251            << format_hex(Address, 10, true) << " "
10252            << left_justify(Entry.typeName(), 8) << " "
10253            << format_decimal(Entry.addend(), 8) << " "
10254            << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
10255            << Entry.symbolName() << Attr << "\n";
10256   }
10257   if (Err)
10258     report_error(std::move(Err), Obj->getFileName());
10259 }
10260 
10261 //===----------------------------------------------------------------------===//
10262 // lazy bind table dumping
10263 //===----------------------------------------------------------------------===//
10264 
10265 void printMachOLazyBindTable(object::MachOObjectFile *Obj) {
10266   outs() << "segment  section            address     "
10267             "dylib            symbol\n";
10268   Error Err = Error::success();
10269   for (const object::MachOBindEntry &Entry : Obj->lazyBindTable(Err)) {
10270     StringRef SegmentName = Entry.segmentName();
10271     StringRef SectionName = Entry.sectionName();
10272     uint64_t Address = Entry.address();
10273 
10274     // Table lines look like:
10275     //  __DATA  __got  0x00012010 libSystem ___stack_chk_guard
10276     outs() << left_justify(SegmentName, 8) << " "
10277            << left_justify(SectionName, 18) << " "
10278            << format_hex(Address, 10, true) << " "
10279            << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
10280            << Entry.symbolName() << "\n";
10281   }
10282   if (Err)
10283     report_error(std::move(Err), Obj->getFileName());
10284 }
10285 
10286 //===----------------------------------------------------------------------===//
10287 // weak bind table dumping
10288 //===----------------------------------------------------------------------===//
10289 
10290 void printMachOWeakBindTable(object::MachOObjectFile *Obj) {
10291   outs() << "segment  section            address     "
10292             "type       addend   symbol\n";
10293   Error Err = Error::success();
10294   for (const object::MachOBindEntry &Entry : Obj->weakBindTable(Err)) {
10295     // Strong symbols don't have a location to update.
10296     if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
10297       outs() << "                                        strong              "
10298              << Entry.symbolName() << "\n";
10299       continue;
10300     }
10301     StringRef SegmentName = Entry.segmentName();
10302     StringRef SectionName = Entry.sectionName();
10303     uint64_t Address = Entry.address();
10304 
10305     // Table lines look like:
10306     // __DATA  __data  0x00001000  pointer    0   _foo
10307     outs() << left_justify(SegmentName, 8) << " "
10308            << left_justify(SectionName, 18) << " "
10309            << format_hex(Address, 10, true) << " "
10310            << left_justify(Entry.typeName(), 8) << " "
10311            << format_decimal(Entry.addend(), 8) << "   " << Entry.symbolName()
10312            << "\n";
10313   }
10314   if (Err)
10315     report_error(std::move(Err), Obj->getFileName());
10316 }
10317 
10318 // get_dyld_bind_info_symbolname() is used for disassembly and passed an
10319 // address, ReferenceValue, in the Mach-O file and looks in the dyld bind
10320 // information for that address. If the address is found its binding symbol
10321 // name is returned.  If not nullptr is returned.
10322 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
10323                                                  struct DisassembleInfo *info) {
10324   if (info->bindtable == nullptr) {
10325     info->bindtable = llvm::make_unique<SymbolAddressMap>();
10326     Error Err = Error::success();
10327     for (const object::MachOBindEntry &Entry : info->O->bindTable(Err)) {
10328       uint64_t Address = Entry.address();
10329       StringRef name = Entry.symbolName();
10330       if (!name.empty())
10331         (*info->bindtable)[Address] = name;
10332     }
10333     if (Err)
10334       report_error(std::move(Err), info->O->getFileName());
10335   }
10336   auto name = info->bindtable->lookup(ReferenceValue);
10337   return !name.empty() ? name.data() : nullptr;
10338 }
10339 
10340 void printLazyBindTable(ObjectFile *o) {
10341   outs() << "Lazy bind table:\n";
10342   if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
10343     printMachOLazyBindTable(MachO);
10344   else
10345     WithColor::error()
10346         << "This operation is only currently supported "
10347            "for Mach-O executable files.\n";
10348 }
10349 
10350 void printWeakBindTable(ObjectFile *o) {
10351   outs() << "Weak bind table:\n";
10352   if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
10353     printMachOWeakBindTable(MachO);
10354   else
10355     WithColor::error()
10356         << "This operation is only currently supported "
10357            "for Mach-O executable files.\n";
10358 }
10359 
10360 void printExportsTrie(const ObjectFile *o) {
10361   outs() << "Exports trie:\n";
10362   if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
10363     printMachOExportsTrie(MachO);
10364   else
10365     WithColor::error()
10366         << "This operation is only currently supported "
10367            "for Mach-O executable files.\n";
10368 }
10369 
10370 void printRebaseTable(ObjectFile *o) {
10371   outs() << "Rebase table:\n";
10372   if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
10373     printMachORebaseTable(MachO);
10374   else
10375     WithColor::error()
10376         << "This operation is only currently supported "
10377            "for Mach-O executable files.\n";
10378 }
10379 
10380 void printBindTable(ObjectFile *o) {
10381   outs() << "Bind table:\n";
10382   if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
10383     printMachOBindTable(MachO);
10384   else
10385     WithColor::error()
10386         << "This operation is only currently supported "
10387            "for Mach-O executable files.\n";
10388 }
10389 } // namespace llvm
10390