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