xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-objdump/MachODump.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===-- MachODump.cpp - Object file dumping utility for llvm --------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the MachO-specific dumper for llvm-objdump.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
135ffd83dbSDimitry Andric #include "MachODump.h"
145ffd83dbSDimitry Andric 
15fe6060f1SDimitry Andric #include "ObjdumpOptID.h"
160b57cec5SDimitry Andric #include "llvm-objdump.h"
170b57cec5SDimitry Andric #include "llvm-c/Disassembler.h"
180b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
190b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h"
200b57cec5SDimitry Andric #include "llvm/BinaryFormat/MachO.h"
210b57cec5SDimitry Andric #include "llvm/Config/config.h"
220b57cec5SDimitry Andric #include "llvm/DebugInfo/DIContext.h"
230b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFContext.h"
240b57cec5SDimitry Andric #include "llvm/Demangle/Demangle.h"
250b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
260b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
270b57cec5SDimitry Andric #include "llvm/MC/MCDisassembler/MCDisassembler.h"
280b57cec5SDimitry Andric #include "llvm/MC/MCInst.h"
290b57cec5SDimitry Andric #include "llvm/MC/MCInstPrinter.h"
300b57cec5SDimitry Andric #include "llvm/MC/MCInstrDesc.h"
310b57cec5SDimitry Andric #include "llvm/MC/MCInstrInfo.h"
320b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
330b57cec5SDimitry Andric #include "llvm/MC/MCSubtargetInfo.h"
34480093f4SDimitry Andric #include "llvm/MC/MCTargetOptions.h"
35349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h"
360b57cec5SDimitry Andric #include "llvm/Object/MachO.h"
370b57cec5SDimitry Andric #include "llvm/Object/MachOUniversal.h"
38fe6060f1SDimitry Andric #include "llvm/Option/ArgList.h"
390b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
400b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
410b57cec5SDimitry Andric #include "llvm/Support/Endian.h"
420b57cec5SDimitry Andric #include "llvm/Support/Format.h"
430b57cec5SDimitry Andric #include "llvm/Support/FormattedStream.h"
440b57cec5SDimitry Andric #include "llvm/Support/GraphWriter.h"
450b57cec5SDimitry Andric #include "llvm/Support/LEB128.h"
460b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
470b57cec5SDimitry Andric #include "llvm/Support/TargetSelect.h"
480b57cec5SDimitry Andric #include "llvm/Support/ToolOutputFile.h"
490b57cec5SDimitry Andric #include "llvm/Support/WithColor.h"
500b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
5106c3fb27SDimitry Andric #include "llvm/TargetParser/Triple.h"
520b57cec5SDimitry Andric #include <algorithm>
530b57cec5SDimitry Andric #include <cstring>
540b57cec5SDimitry Andric #include <system_error>
550b57cec5SDimitry Andric 
565ffd83dbSDimitry Andric using namespace llvm;
570b57cec5SDimitry Andric using namespace llvm::object;
585ffd83dbSDimitry Andric using namespace llvm::objdump;
590b57cec5SDimitry Andric 
60fe6060f1SDimitry Andric bool objdump::FirstPrivateHeader;
61fe6060f1SDimitry Andric bool objdump::ExportsTrie;
62fe6060f1SDimitry Andric bool objdump::Rebase;
63fe6060f1SDimitry Andric bool objdump::Rpaths;
64fe6060f1SDimitry Andric bool objdump::Bind;
65fe6060f1SDimitry Andric bool objdump::LazyBind;
66fe6060f1SDimitry Andric bool objdump::WeakBind;
67fe6060f1SDimitry Andric static bool UseDbg;
68fe6060f1SDimitry Andric static std::string DSYMFile;
69fe6060f1SDimitry Andric bool objdump::FullLeadingAddr;
70fe6060f1SDimitry Andric bool objdump::LeadingHeaders;
71fe6060f1SDimitry Andric bool objdump::UniversalHeaders;
72fe6060f1SDimitry Andric static bool ArchiveMemberOffsets;
73fe6060f1SDimitry Andric bool objdump::IndirectSymbols;
74fe6060f1SDimitry Andric bool objdump::DataInCode;
75bdd1243dSDimitry Andric FunctionStartsMode objdump::FunctionStartsType =
76bdd1243dSDimitry Andric     objdump::FunctionStartsMode::None;
77fe6060f1SDimitry Andric bool objdump::LinkOptHints;
78fe6060f1SDimitry Andric bool objdump::InfoPlist;
79bdd1243dSDimitry Andric bool objdump::ChainedFixups;
8081ad6265SDimitry Andric bool objdump::DyldInfo;
81fe6060f1SDimitry Andric bool objdump::DylibsUsed;
82fe6060f1SDimitry Andric bool objdump::DylibId;
83fe6060f1SDimitry Andric bool objdump::Verbose;
84fe6060f1SDimitry Andric bool objdump::ObjcMetaData;
85fe6060f1SDimitry Andric std::string objdump::DisSymName;
86fe6060f1SDimitry Andric bool objdump::SymbolicOperands;
87fe6060f1SDimitry Andric static std::vector<std::string> ArchFlags;
880b57cec5SDimitry Andric 
895ffd83dbSDimitry Andric static bool ArchAll = false;
900b57cec5SDimitry Andric static std::string ThumbTripleName;
910b57cec5SDimitry Andric 
92bdd1243dSDimitry Andric static StringRef ordinalName(const object::MachOObjectFile *, int);
93bdd1243dSDimitry Andric 
parseMachOOptions(const llvm::opt::InputArgList & InputArgs)94fe6060f1SDimitry Andric void objdump::parseMachOOptions(const llvm::opt::InputArgList &InputArgs) {
95fe6060f1SDimitry Andric   FirstPrivateHeader = InputArgs.hasArg(OBJDUMP_private_header);
96fe6060f1SDimitry Andric   ExportsTrie = InputArgs.hasArg(OBJDUMP_exports_trie);
97fe6060f1SDimitry Andric   Rebase = InputArgs.hasArg(OBJDUMP_rebase);
98fe6060f1SDimitry Andric   Rpaths = InputArgs.hasArg(OBJDUMP_rpaths);
99fe6060f1SDimitry Andric   Bind = InputArgs.hasArg(OBJDUMP_bind);
100fe6060f1SDimitry Andric   LazyBind = InputArgs.hasArg(OBJDUMP_lazy_bind);
101fe6060f1SDimitry Andric   WeakBind = InputArgs.hasArg(OBJDUMP_weak_bind);
102fe6060f1SDimitry Andric   UseDbg = InputArgs.hasArg(OBJDUMP_g);
103fe6060f1SDimitry Andric   DSYMFile = InputArgs.getLastArgValue(OBJDUMP_dsym_EQ).str();
104fe6060f1SDimitry Andric   FullLeadingAddr = InputArgs.hasArg(OBJDUMP_full_leading_addr);
105fe6060f1SDimitry Andric   LeadingHeaders = !InputArgs.hasArg(OBJDUMP_no_leading_headers);
106fe6060f1SDimitry Andric   UniversalHeaders = InputArgs.hasArg(OBJDUMP_universal_headers);
107fe6060f1SDimitry Andric   ArchiveMemberOffsets = InputArgs.hasArg(OBJDUMP_archive_member_offsets);
108fe6060f1SDimitry Andric   IndirectSymbols = InputArgs.hasArg(OBJDUMP_indirect_symbols);
109fe6060f1SDimitry Andric   DataInCode = InputArgs.hasArg(OBJDUMP_data_in_code);
110bdd1243dSDimitry Andric   if (const opt::Arg *A = InputArgs.getLastArg(OBJDUMP_function_starts_EQ)) {
111bdd1243dSDimitry Andric     FunctionStartsType = StringSwitch<FunctionStartsMode>(A->getValue())
112bdd1243dSDimitry Andric                              .Case("addrs", FunctionStartsMode::Addrs)
113bdd1243dSDimitry Andric                              .Case("names", FunctionStartsMode::Names)
114bdd1243dSDimitry Andric                              .Case("both", FunctionStartsMode::Both)
115bdd1243dSDimitry Andric                              .Default(FunctionStartsMode::None);
116bdd1243dSDimitry Andric     if (FunctionStartsType == FunctionStartsMode::None)
117bdd1243dSDimitry Andric       invalidArgValue(A);
118bdd1243dSDimitry Andric   }
119fe6060f1SDimitry Andric   LinkOptHints = InputArgs.hasArg(OBJDUMP_link_opt_hints);
120fe6060f1SDimitry Andric   InfoPlist = InputArgs.hasArg(OBJDUMP_info_plist);
121bdd1243dSDimitry Andric   ChainedFixups = InputArgs.hasArg(OBJDUMP_chained_fixups);
12281ad6265SDimitry Andric   DyldInfo = InputArgs.hasArg(OBJDUMP_dyld_info);
123fe6060f1SDimitry Andric   DylibsUsed = InputArgs.hasArg(OBJDUMP_dylibs_used);
124fe6060f1SDimitry Andric   DylibId = InputArgs.hasArg(OBJDUMP_dylib_id);
125fe6060f1SDimitry Andric   Verbose = !InputArgs.hasArg(OBJDUMP_non_verbose);
126fe6060f1SDimitry Andric   ObjcMetaData = InputArgs.hasArg(OBJDUMP_objc_meta_data);
127fe6060f1SDimitry Andric   DisSymName = InputArgs.getLastArgValue(OBJDUMP_dis_symname).str();
128fe6060f1SDimitry Andric   SymbolicOperands = !InputArgs.hasArg(OBJDUMP_no_symbolic_operands);
129fe6060f1SDimitry Andric   ArchFlags = InputArgs.getAllArgValues(OBJDUMP_arch_EQ);
130fe6060f1SDimitry Andric }
131fe6060f1SDimitry Andric 
GetTarget(const MachOObjectFile * MachOObj,const char ** McpuDefault,const Target ** ThumbTarget)1320b57cec5SDimitry Andric static const Target *GetTarget(const MachOObjectFile *MachOObj,
1330b57cec5SDimitry Andric                                const char **McpuDefault,
1340b57cec5SDimitry Andric                                const Target **ThumbTarget) {
1350b57cec5SDimitry Andric   // Figure out the target triple.
1360b57cec5SDimitry Andric   Triple TT(TripleName);
1370b57cec5SDimitry Andric   if (TripleName.empty()) {
1380b57cec5SDimitry Andric     TT = MachOObj->getArchTriple(McpuDefault);
1390b57cec5SDimitry Andric     TripleName = TT.str();
1400b57cec5SDimitry Andric   }
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric   if (TT.getArch() == Triple::arm) {
1430b57cec5SDimitry Andric     // We've inferred a 32-bit ARM target from the object file. All MachO CPUs
1440b57cec5SDimitry Andric     // that support ARM are also capable of Thumb mode.
1450b57cec5SDimitry Andric     Triple ThumbTriple = TT;
1460b57cec5SDimitry Andric     std::string ThumbName = (Twine("thumb") + TT.getArchName().substr(3)).str();
1470b57cec5SDimitry Andric     ThumbTriple.setArchName(ThumbName);
1480b57cec5SDimitry Andric     ThumbTripleName = ThumbTriple.str();
1490b57cec5SDimitry Andric   }
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric   // Get the target specific parser.
1520b57cec5SDimitry Andric   std::string Error;
1530b57cec5SDimitry Andric   const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
1540b57cec5SDimitry Andric   if (TheTarget && ThumbTripleName.empty())
1550b57cec5SDimitry Andric     return TheTarget;
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric   *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
1580b57cec5SDimitry Andric   if (*ThumbTarget)
1590b57cec5SDimitry Andric     return TheTarget;
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric   WithColor::error(errs(), "llvm-objdump") << "unable to get target for '";
1620b57cec5SDimitry Andric   if (!TheTarget)
1630b57cec5SDimitry Andric     errs() << TripleName;
1640b57cec5SDimitry Andric   else
1650b57cec5SDimitry Andric     errs() << ThumbTripleName;
1660b57cec5SDimitry Andric   errs() << "', see --version and --triple.\n";
1670b57cec5SDimitry Andric   return nullptr;
1680b57cec5SDimitry Andric }
1690b57cec5SDimitry Andric 
1705ffd83dbSDimitry Andric namespace {
1710b57cec5SDimitry Andric struct SymbolSorter {
operator ()__anon061faff00111::SymbolSorter1720b57cec5SDimitry Andric   bool operator()(const SymbolRef &A, const SymbolRef &B) {
1730b57cec5SDimitry Andric     Expected<SymbolRef::Type> ATypeOrErr = A.getType();
1740b57cec5SDimitry Andric     if (!ATypeOrErr)
1758bcb0991SDimitry Andric       reportError(ATypeOrErr.takeError(), A.getObject()->getFileName());
1760b57cec5SDimitry Andric     SymbolRef::Type AType = *ATypeOrErr;
1770b57cec5SDimitry Andric     Expected<SymbolRef::Type> BTypeOrErr = B.getType();
1780b57cec5SDimitry Andric     if (!BTypeOrErr)
1798bcb0991SDimitry Andric       reportError(BTypeOrErr.takeError(), B.getObject()->getFileName());
1800b57cec5SDimitry Andric     SymbolRef::Type BType = *BTypeOrErr;
1815ffd83dbSDimitry Andric     uint64_t AAddr =
1825ffd83dbSDimitry Andric         (AType != SymbolRef::ST_Function) ? 0 : cantFail(A.getValue());
1835ffd83dbSDimitry Andric     uint64_t BAddr =
1845ffd83dbSDimitry Andric         (BType != SymbolRef::ST_Function) ? 0 : cantFail(B.getValue());
1850b57cec5SDimitry Andric     return AAddr < BAddr;
1860b57cec5SDimitry Andric   }
1870b57cec5SDimitry Andric };
18806c3fb27SDimitry Andric 
18906c3fb27SDimitry Andric class MachODumper : public Dumper {
19006c3fb27SDimitry Andric   const object::MachOObjectFile &Obj;
19106c3fb27SDimitry Andric 
19206c3fb27SDimitry Andric public:
MachODumper(const object::MachOObjectFile & O)19306c3fb27SDimitry Andric   MachODumper(const object::MachOObjectFile &O) : Dumper(O), Obj(O) {}
1945f757f3fSDimitry Andric   void printPrivateHeaders() override;
19506c3fb27SDimitry Andric };
1965ffd83dbSDimitry Andric } // namespace
1970b57cec5SDimitry Andric 
19806c3fb27SDimitry Andric std::unique_ptr<Dumper>
createMachODumper(const object::MachOObjectFile & Obj)19906c3fb27SDimitry Andric objdump::createMachODumper(const object::MachOObjectFile &Obj) {
20006c3fb27SDimitry Andric   return std::make_unique<MachODumper>(Obj);
20106c3fb27SDimitry Andric }
20206c3fb27SDimitry Andric 
2030b57cec5SDimitry Andric // Types for the storted data in code table that is built before disassembly
2040b57cec5SDimitry Andric // and the predicate function to sort them.
2050b57cec5SDimitry Andric typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
2060b57cec5SDimitry Andric typedef std::vector<DiceTableEntry> DiceTable;
2070b57cec5SDimitry Andric typedef DiceTable::iterator dice_table_iterator;
2080b57cec5SDimitry Andric 
2090b57cec5SDimitry Andric // This is used to search for a data in code table entry for the PC being
2100b57cec5SDimitry Andric // disassembled.  The j parameter has the PC in j.first.  A single data in code
2110b57cec5SDimitry Andric // table entry can cover many bytes for each of its Kind's.  So if the offset,
2120b57cec5SDimitry Andric // aka the i.first value, of the data in code table entry plus its Length
2130b57cec5SDimitry Andric // covers the PC being searched for this will return true.  If not it will
2140b57cec5SDimitry Andric // return false.
compareDiceTableEntries(const DiceTableEntry & i,const DiceTableEntry & j)2150b57cec5SDimitry Andric static bool compareDiceTableEntries(const DiceTableEntry &i,
2160b57cec5SDimitry Andric                                     const DiceTableEntry &j) {
2170b57cec5SDimitry Andric   uint16_t Length;
2180b57cec5SDimitry Andric   i.second.getLength(Length);
2190b57cec5SDimitry Andric 
2200b57cec5SDimitry Andric   return j.first >= i.first && j.first < i.first + Length;
2210b57cec5SDimitry Andric }
2220b57cec5SDimitry Andric 
DumpDataInCode(const uint8_t * bytes,uint64_t Length,unsigned short Kind)2230b57cec5SDimitry Andric static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
2240b57cec5SDimitry Andric                                unsigned short Kind) {
2250b57cec5SDimitry Andric   uint32_t Value, Size = 1;
2260b57cec5SDimitry Andric 
2270b57cec5SDimitry Andric   switch (Kind) {
2280b57cec5SDimitry Andric   default:
2290b57cec5SDimitry Andric   case MachO::DICE_KIND_DATA:
2300b57cec5SDimitry Andric     if (Length >= 4) {
231fe6060f1SDimitry Andric       if (ShowRawInsn)
232bdd1243dSDimitry Andric         dumpBytes(ArrayRef(bytes, 4), outs());
2330b57cec5SDimitry Andric       Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
2340b57cec5SDimitry Andric       outs() << "\t.long " << Value;
2350b57cec5SDimitry Andric       Size = 4;
2360b57cec5SDimitry Andric     } else if (Length >= 2) {
237fe6060f1SDimitry Andric       if (ShowRawInsn)
238bdd1243dSDimitry Andric         dumpBytes(ArrayRef(bytes, 2), outs());
2390b57cec5SDimitry Andric       Value = bytes[1] << 8 | bytes[0];
2400b57cec5SDimitry Andric       outs() << "\t.short " << Value;
2410b57cec5SDimitry Andric       Size = 2;
2420b57cec5SDimitry Andric     } else {
243fe6060f1SDimitry Andric       if (ShowRawInsn)
244bdd1243dSDimitry Andric         dumpBytes(ArrayRef(bytes, 2), outs());
2450b57cec5SDimitry Andric       Value = bytes[0];
2460b57cec5SDimitry Andric       outs() << "\t.byte " << Value;
2470b57cec5SDimitry Andric       Size = 1;
2480b57cec5SDimitry Andric     }
2490b57cec5SDimitry Andric     if (Kind == MachO::DICE_KIND_DATA)
2500b57cec5SDimitry Andric       outs() << "\t@ KIND_DATA\n";
2510b57cec5SDimitry Andric     else
2520b57cec5SDimitry Andric       outs() << "\t@ data in code kind = " << Kind << "\n";
2530b57cec5SDimitry Andric     break;
2540b57cec5SDimitry Andric   case MachO::DICE_KIND_JUMP_TABLE8:
255fe6060f1SDimitry Andric     if (ShowRawInsn)
256bdd1243dSDimitry Andric       dumpBytes(ArrayRef(bytes, 1), outs());
2570b57cec5SDimitry Andric     Value = bytes[0];
2580b57cec5SDimitry Andric     outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
2590b57cec5SDimitry Andric     Size = 1;
2600b57cec5SDimitry Andric     break;
2610b57cec5SDimitry Andric   case MachO::DICE_KIND_JUMP_TABLE16:
262fe6060f1SDimitry Andric     if (ShowRawInsn)
263bdd1243dSDimitry Andric       dumpBytes(ArrayRef(bytes, 2), outs());
2640b57cec5SDimitry Andric     Value = bytes[1] << 8 | bytes[0];
2650b57cec5SDimitry Andric     outs() << "\t.short " << format("%5u", Value & 0xffff)
2660b57cec5SDimitry Andric            << "\t@ KIND_JUMP_TABLE16\n";
2670b57cec5SDimitry Andric     Size = 2;
2680b57cec5SDimitry Andric     break;
2690b57cec5SDimitry Andric   case MachO::DICE_KIND_JUMP_TABLE32:
2700b57cec5SDimitry Andric   case MachO::DICE_KIND_ABS_JUMP_TABLE32:
271fe6060f1SDimitry Andric     if (ShowRawInsn)
272bdd1243dSDimitry Andric       dumpBytes(ArrayRef(bytes, 4), outs());
2730b57cec5SDimitry Andric     Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
2740b57cec5SDimitry Andric     outs() << "\t.long " << Value;
2750b57cec5SDimitry Andric     if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
2760b57cec5SDimitry Andric       outs() << "\t@ KIND_JUMP_TABLE32\n";
2770b57cec5SDimitry Andric     else
2780b57cec5SDimitry Andric       outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
2790b57cec5SDimitry Andric     Size = 4;
2800b57cec5SDimitry Andric     break;
2810b57cec5SDimitry Andric   }
2820b57cec5SDimitry Andric   return Size;
2830b57cec5SDimitry Andric }
2840b57cec5SDimitry Andric 
getSectionsAndSymbols(MachOObjectFile * MachOObj,std::vector<SectionRef> & Sections,std::vector<SymbolRef> & Symbols,SmallVectorImpl<uint64_t> & FoundFns,uint64_t & BaseSegmentAddress)2850b57cec5SDimitry Andric static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
2860b57cec5SDimitry Andric                                   std::vector<SectionRef> &Sections,
2870b57cec5SDimitry Andric                                   std::vector<SymbolRef> &Symbols,
2880b57cec5SDimitry Andric                                   SmallVectorImpl<uint64_t> &FoundFns,
2890b57cec5SDimitry Andric                                   uint64_t &BaseSegmentAddress) {
2900b57cec5SDimitry Andric   const StringRef FileName = MachOObj->getFileName();
2910b57cec5SDimitry Andric   for (const SymbolRef &Symbol : MachOObj->symbols()) {
2920b57cec5SDimitry Andric     StringRef SymName = unwrapOrError(Symbol.getName(), FileName);
2935f757f3fSDimitry Andric     if (!SymName.starts_with("ltmp"))
2940b57cec5SDimitry Andric       Symbols.push_back(Symbol);
2950b57cec5SDimitry Andric   }
2960b57cec5SDimitry Andric 
297fe6060f1SDimitry Andric   append_range(Sections, MachOObj->sections());
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric   bool BaseSegmentAddressSet = false;
3000b57cec5SDimitry Andric   for (const auto &Command : MachOObj->load_commands()) {
3010b57cec5SDimitry Andric     if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
3020b57cec5SDimitry Andric       // We found a function starts segment, parse the addresses for later
3030b57cec5SDimitry Andric       // consumption.
3040b57cec5SDimitry Andric       MachO::linkedit_data_command LLC =
3050b57cec5SDimitry Andric           MachOObj->getLinkeditDataLoadCommand(Command);
3060b57cec5SDimitry Andric 
3070b57cec5SDimitry Andric       MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
3080b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_SEGMENT) {
3090b57cec5SDimitry Andric       MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
3100b57cec5SDimitry Andric       StringRef SegName = SLC.segname;
3110b57cec5SDimitry Andric       if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
3120b57cec5SDimitry Andric         BaseSegmentAddressSet = true;
3130b57cec5SDimitry Andric         BaseSegmentAddress = SLC.vmaddr;
3140b57cec5SDimitry Andric       }
3158bcb0991SDimitry Andric     } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
3168bcb0991SDimitry Andric       MachO::segment_command_64 SLC = MachOObj->getSegment64LoadCommand(Command);
3178bcb0991SDimitry Andric       StringRef SegName = SLC.segname;
3188bcb0991SDimitry Andric       if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
3198bcb0991SDimitry Andric         BaseSegmentAddressSet = true;
3208bcb0991SDimitry Andric         BaseSegmentAddress = SLC.vmaddr;
3210b57cec5SDimitry Andric       }
3220b57cec5SDimitry Andric     }
3230b57cec5SDimitry Andric   }
3248bcb0991SDimitry Andric }
3258bcb0991SDimitry Andric 
DumpAndSkipDataInCode(uint64_t PC,const uint8_t * bytes,DiceTable & Dices,uint64_t & InstSize)3268bcb0991SDimitry Andric static bool DumpAndSkipDataInCode(uint64_t PC, const uint8_t *bytes,
3278bcb0991SDimitry Andric                                  DiceTable &Dices, uint64_t &InstSize) {
3288bcb0991SDimitry Andric   // Check the data in code table here to see if this is data not an
3298bcb0991SDimitry Andric   // instruction to be disassembled.
3308bcb0991SDimitry Andric   DiceTable Dice;
3318bcb0991SDimitry Andric   Dice.push_back(std::make_pair(PC, DiceRef()));
3328bcb0991SDimitry Andric   dice_table_iterator DTI =
3338bcb0991SDimitry Andric       std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
3348bcb0991SDimitry Andric                   compareDiceTableEntries);
3358bcb0991SDimitry Andric   if (DTI != Dices.end()) {
3368bcb0991SDimitry Andric     uint16_t Length;
3378bcb0991SDimitry Andric     DTI->second.getLength(Length);
3388bcb0991SDimitry Andric     uint16_t Kind;
3398bcb0991SDimitry Andric     DTI->second.getKind(Kind);
3408bcb0991SDimitry Andric     InstSize = DumpDataInCode(bytes, Length, Kind);
3418bcb0991SDimitry Andric     if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
3428bcb0991SDimitry Andric         (PC == (DTI->first + Length - 1)) && (Length & 1))
3438bcb0991SDimitry Andric       InstSize++;
3448bcb0991SDimitry Andric     return true;
3458bcb0991SDimitry Andric   }
3468bcb0991SDimitry Andric   return false;
3478bcb0991SDimitry Andric }
3480b57cec5SDimitry Andric 
printRelocationTargetName(const MachOObjectFile * O,const MachO::any_relocation_info & RE,raw_string_ostream & Fmt)3490b57cec5SDimitry Andric static void printRelocationTargetName(const MachOObjectFile *O,
3500b57cec5SDimitry Andric                                       const MachO::any_relocation_info &RE,
3510b57cec5SDimitry Andric                                       raw_string_ostream &Fmt) {
3520b57cec5SDimitry Andric   // Target of a scattered relocation is an address.  In the interest of
3530b57cec5SDimitry Andric   // generating pretty output, scan through the symbol table looking for a
3540b57cec5SDimitry Andric   // symbol that aligns with that address.  If we find one, print it.
3550b57cec5SDimitry Andric   // Otherwise, we just print the hex address of the target.
3560b57cec5SDimitry Andric   const StringRef FileName = O->getFileName();
3570b57cec5SDimitry Andric   if (O->isRelocationScattered(RE)) {
3580b57cec5SDimitry Andric     uint32_t Val = O->getPlainRelocationSymbolNum(RE);
3590b57cec5SDimitry Andric 
3600b57cec5SDimitry Andric     for (const SymbolRef &Symbol : O->symbols()) {
3610b57cec5SDimitry Andric       uint64_t Addr = unwrapOrError(Symbol.getAddress(), FileName);
3620b57cec5SDimitry Andric       if (Addr != Val)
3630b57cec5SDimitry Andric         continue;
3640b57cec5SDimitry Andric       Fmt << unwrapOrError(Symbol.getName(), FileName);
3650b57cec5SDimitry Andric       return;
3660b57cec5SDimitry Andric     }
3670b57cec5SDimitry Andric 
3680b57cec5SDimitry Andric     // If we couldn't find a symbol that this relocation refers to, try
3690b57cec5SDimitry Andric     // to find a section beginning instead.
3700b57cec5SDimitry Andric     for (const SectionRef &Section : ToolSectionFilter(*O)) {
3710b57cec5SDimitry Andric       uint64_t Addr = Section.getAddress();
3720b57cec5SDimitry Andric       if (Addr != Val)
3730b57cec5SDimitry Andric         continue;
3748bcb0991SDimitry Andric       StringRef NameOrErr = unwrapOrError(Section.getName(), O->getFileName());
3758bcb0991SDimitry Andric       Fmt << NameOrErr;
3760b57cec5SDimitry Andric       return;
3770b57cec5SDimitry Andric     }
3780b57cec5SDimitry Andric 
3790b57cec5SDimitry Andric     Fmt << format("0x%x", Val);
3800b57cec5SDimitry Andric     return;
3810b57cec5SDimitry Andric   }
3820b57cec5SDimitry Andric 
3830b57cec5SDimitry Andric   StringRef S;
3840b57cec5SDimitry Andric   bool isExtern = O->getPlainRelocationExternal(RE);
3850b57cec5SDimitry Andric   uint64_t Val = O->getPlainRelocationSymbolNum(RE);
3860b57cec5SDimitry Andric 
387e8d8bef9SDimitry Andric   if (O->getAnyRelocationType(RE) == MachO::ARM64_RELOC_ADDEND &&
388e8d8bef9SDimitry Andric       (O->getArch() == Triple::aarch64 || O->getArch() == Triple::aarch64_be)) {
3890b57cec5SDimitry Andric     Fmt << format("0x%0" PRIx64, Val);
3900b57cec5SDimitry Andric     return;
3910b57cec5SDimitry Andric   }
3920b57cec5SDimitry Andric 
3930b57cec5SDimitry Andric   if (isExtern) {
3940b57cec5SDimitry Andric     symbol_iterator SI = O->symbol_begin();
395fe6060f1SDimitry Andric     std::advance(SI, Val);
3960b57cec5SDimitry Andric     S = unwrapOrError(SI->getName(), FileName);
3970b57cec5SDimitry Andric   } else {
3980b57cec5SDimitry Andric     section_iterator SI = O->section_begin();
3990b57cec5SDimitry Andric     // Adjust for the fact that sections are 1-indexed.
4000b57cec5SDimitry Andric     if (Val == 0) {
4010b57cec5SDimitry Andric       Fmt << "0 (?,?)";
4020b57cec5SDimitry Andric       return;
4030b57cec5SDimitry Andric     }
4040b57cec5SDimitry Andric     uint32_t I = Val - 1;
4050b57cec5SDimitry Andric     while (I != 0 && SI != O->section_end()) {
4060b57cec5SDimitry Andric       --I;
407fe6060f1SDimitry Andric       std::advance(SI, 1);
4080b57cec5SDimitry Andric     }
4098bcb0991SDimitry Andric     if (SI == O->section_end()) {
4100b57cec5SDimitry Andric       Fmt << Val << " (?,?)";
4118bcb0991SDimitry Andric     } else {
4128bcb0991SDimitry Andric       if (Expected<StringRef> NameOrErr = SI->getName())
4138bcb0991SDimitry Andric         S = *NameOrErr;
4140b57cec5SDimitry Andric       else
4158bcb0991SDimitry Andric         consumeError(NameOrErr.takeError());
4168bcb0991SDimitry Andric     }
4170b57cec5SDimitry Andric   }
4180b57cec5SDimitry Andric 
4190b57cec5SDimitry Andric   Fmt << S;
4200b57cec5SDimitry Andric }
4210b57cec5SDimitry Andric 
getMachORelocationValueString(const MachOObjectFile * Obj,const RelocationRef & RelRef,SmallVectorImpl<char> & Result)4225ffd83dbSDimitry Andric Error objdump::getMachORelocationValueString(const MachOObjectFile *Obj,
4230b57cec5SDimitry Andric                                              const RelocationRef &RelRef,
4240b57cec5SDimitry Andric                                              SmallVectorImpl<char> &Result) {
4250b57cec5SDimitry Andric   DataRefImpl Rel = RelRef.getRawDataRefImpl();
4260b57cec5SDimitry Andric   MachO::any_relocation_info RE = Obj->getRelocation(Rel);
4270b57cec5SDimitry Andric 
4280b57cec5SDimitry Andric   unsigned Arch = Obj->getArch();
4290b57cec5SDimitry Andric 
4300b57cec5SDimitry Andric   std::string FmtBuf;
4310b57cec5SDimitry Andric   raw_string_ostream Fmt(FmtBuf);
4320b57cec5SDimitry Andric   unsigned Type = Obj->getAnyRelocationType(RE);
4330b57cec5SDimitry Andric   bool IsPCRel = Obj->getAnyRelocationPCRel(RE);
4340b57cec5SDimitry Andric 
4350b57cec5SDimitry Andric   // Determine any addends that should be displayed with the relocation.
4360b57cec5SDimitry Andric   // These require decoding the relocation type, which is triple-specific.
4370b57cec5SDimitry Andric 
4380b57cec5SDimitry Andric   // X86_64 has entirely custom relocation types.
4390b57cec5SDimitry Andric   if (Arch == Triple::x86_64) {
4400b57cec5SDimitry Andric     switch (Type) {
4410b57cec5SDimitry Andric     case MachO::X86_64_RELOC_GOT_LOAD:
4420b57cec5SDimitry Andric     case MachO::X86_64_RELOC_GOT: {
4430b57cec5SDimitry Andric       printRelocationTargetName(Obj, RE, Fmt);
4440b57cec5SDimitry Andric       Fmt << "@GOT";
4450b57cec5SDimitry Andric       if (IsPCRel)
4460b57cec5SDimitry Andric         Fmt << "PCREL";
4470b57cec5SDimitry Andric       break;
4480b57cec5SDimitry Andric     }
4490b57cec5SDimitry Andric     case MachO::X86_64_RELOC_SUBTRACTOR: {
4500b57cec5SDimitry Andric       DataRefImpl RelNext = Rel;
4510b57cec5SDimitry Andric       Obj->moveRelocationNext(RelNext);
4520b57cec5SDimitry Andric       MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
4530b57cec5SDimitry Andric 
4540b57cec5SDimitry Andric       // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
4550b57cec5SDimitry Andric       // X86_64_RELOC_UNSIGNED.
4560b57cec5SDimitry Andric       // NOTE: Scattered relocations don't exist on x86_64.
4570b57cec5SDimitry Andric       unsigned RType = Obj->getAnyRelocationType(RENext);
4580b57cec5SDimitry Andric       if (RType != MachO::X86_64_RELOC_UNSIGNED)
4598bcb0991SDimitry Andric         reportError(Obj->getFileName(), "Expected X86_64_RELOC_UNSIGNED after "
4600b57cec5SDimitry Andric                                         "X86_64_RELOC_SUBTRACTOR.");
4610b57cec5SDimitry Andric 
4620b57cec5SDimitry Andric       // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
4630b57cec5SDimitry Andric       // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
4640b57cec5SDimitry Andric       printRelocationTargetName(Obj, RENext, Fmt);
4650b57cec5SDimitry Andric       Fmt << "-";
4660b57cec5SDimitry Andric       printRelocationTargetName(Obj, RE, Fmt);
4670b57cec5SDimitry Andric       break;
4680b57cec5SDimitry Andric     }
4690b57cec5SDimitry Andric     case MachO::X86_64_RELOC_TLV:
4700b57cec5SDimitry Andric       printRelocationTargetName(Obj, RE, Fmt);
4710b57cec5SDimitry Andric       Fmt << "@TLV";
4720b57cec5SDimitry Andric       if (IsPCRel)
4730b57cec5SDimitry Andric         Fmt << "P";
4740b57cec5SDimitry Andric       break;
4750b57cec5SDimitry Andric     case MachO::X86_64_RELOC_SIGNED_1:
4760b57cec5SDimitry Andric       printRelocationTargetName(Obj, RE, Fmt);
4770b57cec5SDimitry Andric       Fmt << "-1";
4780b57cec5SDimitry Andric       break;
4790b57cec5SDimitry Andric     case MachO::X86_64_RELOC_SIGNED_2:
4800b57cec5SDimitry Andric       printRelocationTargetName(Obj, RE, Fmt);
4810b57cec5SDimitry Andric       Fmt << "-2";
4820b57cec5SDimitry Andric       break;
4830b57cec5SDimitry Andric     case MachO::X86_64_RELOC_SIGNED_4:
4840b57cec5SDimitry Andric       printRelocationTargetName(Obj, RE, Fmt);
4850b57cec5SDimitry Andric       Fmt << "-4";
4860b57cec5SDimitry Andric       break;
4870b57cec5SDimitry Andric     default:
4880b57cec5SDimitry Andric       printRelocationTargetName(Obj, RE, Fmt);
4890b57cec5SDimitry Andric       break;
4900b57cec5SDimitry Andric     }
4910b57cec5SDimitry Andric     // X86 and ARM share some relocation types in common.
4920b57cec5SDimitry Andric   } else if (Arch == Triple::x86 || Arch == Triple::arm ||
4930b57cec5SDimitry Andric              Arch == Triple::ppc) {
4940b57cec5SDimitry Andric     // Generic relocation types...
4950b57cec5SDimitry Andric     switch (Type) {
4960b57cec5SDimitry Andric     case MachO::GENERIC_RELOC_PAIR: // prints no info
4970b57cec5SDimitry Andric       return Error::success();
4980b57cec5SDimitry Andric     case MachO::GENERIC_RELOC_SECTDIFF: {
4990b57cec5SDimitry Andric       DataRefImpl RelNext = Rel;
5000b57cec5SDimitry Andric       Obj->moveRelocationNext(RelNext);
5010b57cec5SDimitry Andric       MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
5020b57cec5SDimitry Andric 
5030b57cec5SDimitry Andric       // X86 sect diff's must be followed by a relocation of type
5040b57cec5SDimitry Andric       // GENERIC_RELOC_PAIR.
5050b57cec5SDimitry Andric       unsigned RType = Obj->getAnyRelocationType(RENext);
5060b57cec5SDimitry Andric 
5070b57cec5SDimitry Andric       if (RType != MachO::GENERIC_RELOC_PAIR)
5088bcb0991SDimitry Andric         reportError(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after "
5090b57cec5SDimitry Andric                                         "GENERIC_RELOC_SECTDIFF.");
5100b57cec5SDimitry Andric 
5110b57cec5SDimitry Andric       printRelocationTargetName(Obj, RE, Fmt);
5120b57cec5SDimitry Andric       Fmt << "-";
5130b57cec5SDimitry Andric       printRelocationTargetName(Obj, RENext, Fmt);
5140b57cec5SDimitry Andric       break;
5150b57cec5SDimitry Andric     }
5160b57cec5SDimitry Andric     }
5170b57cec5SDimitry Andric 
5180b57cec5SDimitry Andric     if (Arch == Triple::x86 || Arch == Triple::ppc) {
5190b57cec5SDimitry Andric       switch (Type) {
5200b57cec5SDimitry Andric       case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
5210b57cec5SDimitry Andric         DataRefImpl RelNext = Rel;
5220b57cec5SDimitry Andric         Obj->moveRelocationNext(RelNext);
5230b57cec5SDimitry Andric         MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
5240b57cec5SDimitry Andric 
5250b57cec5SDimitry Andric         // X86 sect diff's must be followed by a relocation of type
5260b57cec5SDimitry Andric         // GENERIC_RELOC_PAIR.
5270b57cec5SDimitry Andric         unsigned RType = Obj->getAnyRelocationType(RENext);
5280b57cec5SDimitry Andric         if (RType != MachO::GENERIC_RELOC_PAIR)
5298bcb0991SDimitry Andric           reportError(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after "
5300b57cec5SDimitry Andric                                           "GENERIC_RELOC_LOCAL_SECTDIFF.");
5310b57cec5SDimitry Andric 
5320b57cec5SDimitry Andric         printRelocationTargetName(Obj, RE, Fmt);
5330b57cec5SDimitry Andric         Fmt << "-";
5340b57cec5SDimitry Andric         printRelocationTargetName(Obj, RENext, Fmt);
5350b57cec5SDimitry Andric         break;
5360b57cec5SDimitry Andric       }
5370b57cec5SDimitry Andric       case MachO::GENERIC_RELOC_TLV: {
5380b57cec5SDimitry Andric         printRelocationTargetName(Obj, RE, Fmt);
5390b57cec5SDimitry Andric         Fmt << "@TLV";
5400b57cec5SDimitry Andric         if (IsPCRel)
5410b57cec5SDimitry Andric           Fmt << "P";
5420b57cec5SDimitry Andric         break;
5430b57cec5SDimitry Andric       }
5440b57cec5SDimitry Andric       default:
5450b57cec5SDimitry Andric         printRelocationTargetName(Obj, RE, Fmt);
5460b57cec5SDimitry Andric       }
5470b57cec5SDimitry Andric     } else { // ARM-specific relocations
5480b57cec5SDimitry Andric       switch (Type) {
5490b57cec5SDimitry Andric       case MachO::ARM_RELOC_HALF:
5500b57cec5SDimitry Andric       case MachO::ARM_RELOC_HALF_SECTDIFF: {
5510b57cec5SDimitry Andric         // Half relocations steal a bit from the length field to encode
5520b57cec5SDimitry Andric         // whether this is an upper16 or a lower16 relocation.
5530b57cec5SDimitry Andric         bool isUpper = (Obj->getAnyRelocationLength(RE) & 0x1) == 1;
5540b57cec5SDimitry Andric 
5550b57cec5SDimitry Andric         if (isUpper)
5560b57cec5SDimitry Andric           Fmt << ":upper16:(";
5570b57cec5SDimitry Andric         else
5580b57cec5SDimitry Andric           Fmt << ":lower16:(";
5590b57cec5SDimitry Andric         printRelocationTargetName(Obj, RE, Fmt);
5600b57cec5SDimitry Andric 
5610b57cec5SDimitry Andric         DataRefImpl RelNext = Rel;
5620b57cec5SDimitry Andric         Obj->moveRelocationNext(RelNext);
5630b57cec5SDimitry Andric         MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
5640b57cec5SDimitry Andric 
5650b57cec5SDimitry Andric         // ARM half relocs must be followed by a relocation of type
5660b57cec5SDimitry Andric         // ARM_RELOC_PAIR.
5670b57cec5SDimitry Andric         unsigned RType = Obj->getAnyRelocationType(RENext);
5680b57cec5SDimitry Andric         if (RType != MachO::ARM_RELOC_PAIR)
5698bcb0991SDimitry Andric           reportError(Obj->getFileName(), "Expected ARM_RELOC_PAIR after "
5700b57cec5SDimitry Andric                                           "ARM_RELOC_HALF");
5710b57cec5SDimitry Andric 
5720b57cec5SDimitry Andric         // NOTE: The half of the target virtual address is stashed in the
5730b57cec5SDimitry Andric         // address field of the secondary relocation, but we can't reverse
5740b57cec5SDimitry Andric         // engineer the constant offset from it without decoding the movw/movt
5750b57cec5SDimitry Andric         // instruction to find the other half in its immediate field.
5760b57cec5SDimitry Andric 
5770b57cec5SDimitry Andric         // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
5780b57cec5SDimitry Andric         // symbol/section pointer of the follow-on relocation.
5790b57cec5SDimitry Andric         if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
5800b57cec5SDimitry Andric           Fmt << "-";
5810b57cec5SDimitry Andric           printRelocationTargetName(Obj, RENext, Fmt);
5820b57cec5SDimitry Andric         }
5830b57cec5SDimitry Andric 
5840b57cec5SDimitry Andric         Fmt << ")";
5850b57cec5SDimitry Andric         break;
5860b57cec5SDimitry Andric       }
5870b57cec5SDimitry Andric       default: {
5880b57cec5SDimitry Andric         printRelocationTargetName(Obj, RE, Fmt);
5890b57cec5SDimitry Andric       }
5900b57cec5SDimitry Andric       }
5910b57cec5SDimitry Andric     }
5920b57cec5SDimitry Andric   } else
5930b57cec5SDimitry Andric     printRelocationTargetName(Obj, RE, Fmt);
5940b57cec5SDimitry Andric 
5950b57cec5SDimitry Andric   Fmt.flush();
5960b57cec5SDimitry Andric   Result.append(FmtBuf.begin(), FmtBuf.end());
5970b57cec5SDimitry Andric   return Error::success();
5980b57cec5SDimitry Andric }
5990b57cec5SDimitry Andric 
PrintIndirectSymbolTable(MachOObjectFile * O,bool verbose,uint32_t n,uint32_t count,uint32_t stride,uint64_t addr)6000b57cec5SDimitry Andric static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
6010b57cec5SDimitry Andric                                      uint32_t n, uint32_t count,
6020b57cec5SDimitry Andric                                      uint32_t stride, uint64_t addr) {
6030b57cec5SDimitry Andric   MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
6040b57cec5SDimitry Andric   uint32_t nindirectsyms = Dysymtab.nindirectsyms;
6050b57cec5SDimitry Andric   if (n > nindirectsyms)
6060b57cec5SDimitry Andric     outs() << " (entries start past the end of the indirect symbol "
6070b57cec5SDimitry Andric               "table) (reserved1 field greater than the table size)";
6080b57cec5SDimitry Andric   else if (n + count > nindirectsyms)
6090b57cec5SDimitry Andric     outs() << " (entries extends past the end of the indirect symbol "
6100b57cec5SDimitry Andric               "table)";
6110b57cec5SDimitry Andric   outs() << "\n";
6120b57cec5SDimitry Andric   uint32_t cputype = O->getHeader().cputype;
6130b57cec5SDimitry Andric   if (cputype & MachO::CPU_ARCH_ABI64)
6140b57cec5SDimitry Andric     outs() << "address            index";
6150b57cec5SDimitry Andric   else
6160b57cec5SDimitry Andric     outs() << "address    index";
6170b57cec5SDimitry Andric   if (verbose)
6180b57cec5SDimitry Andric     outs() << " name\n";
6190b57cec5SDimitry Andric   else
6200b57cec5SDimitry Andric     outs() << "\n";
6210b57cec5SDimitry Andric   for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
6220b57cec5SDimitry Andric     if (cputype & MachO::CPU_ARCH_ABI64)
6230b57cec5SDimitry Andric       outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
6240b57cec5SDimitry Andric     else
6250b57cec5SDimitry Andric       outs() << format("0x%08" PRIx32, (uint32_t)addr + j * stride) << " ";
6260b57cec5SDimitry Andric     MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
6270b57cec5SDimitry Andric     uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
6280b57cec5SDimitry Andric     if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
6290b57cec5SDimitry Andric       outs() << "LOCAL\n";
6300b57cec5SDimitry Andric       continue;
6310b57cec5SDimitry Andric     }
6320b57cec5SDimitry Andric     if (indirect_symbol ==
6330b57cec5SDimitry Andric         (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
6340b57cec5SDimitry Andric       outs() << "LOCAL ABSOLUTE\n";
6350b57cec5SDimitry Andric       continue;
6360b57cec5SDimitry Andric     }
6370b57cec5SDimitry Andric     if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
6380b57cec5SDimitry Andric       outs() << "ABSOLUTE\n";
6390b57cec5SDimitry Andric       continue;
6400b57cec5SDimitry Andric     }
6410b57cec5SDimitry Andric     outs() << format("%5u ", indirect_symbol);
6420b57cec5SDimitry Andric     if (verbose) {
6430b57cec5SDimitry Andric       MachO::symtab_command Symtab = O->getSymtabLoadCommand();
6440b57cec5SDimitry Andric       if (indirect_symbol < Symtab.nsyms) {
6450b57cec5SDimitry Andric         symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
6460b57cec5SDimitry Andric         SymbolRef Symbol = *Sym;
6470b57cec5SDimitry Andric         outs() << unwrapOrError(Symbol.getName(), O->getFileName());
6480b57cec5SDimitry Andric       } else {
6490b57cec5SDimitry Andric         outs() << "?";
6500b57cec5SDimitry Andric       }
6510b57cec5SDimitry Andric     }
6520b57cec5SDimitry Andric     outs() << "\n";
6530b57cec5SDimitry Andric   }
6540b57cec5SDimitry Andric }
6550b57cec5SDimitry Andric 
PrintIndirectSymbols(MachOObjectFile * O,bool verbose)6560b57cec5SDimitry Andric static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
6570b57cec5SDimitry Andric   for (const auto &Load : O->load_commands()) {
6580b57cec5SDimitry Andric     if (Load.C.cmd == MachO::LC_SEGMENT_64) {
6590b57cec5SDimitry Andric       MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
6600b57cec5SDimitry Andric       for (unsigned J = 0; J < Seg.nsects; ++J) {
6610b57cec5SDimitry Andric         MachO::section_64 Sec = O->getSection64(Load, J);
6620b57cec5SDimitry Andric         uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
6630b57cec5SDimitry Andric         if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
6640b57cec5SDimitry Andric             section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
6650b57cec5SDimitry Andric             section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
6660b57cec5SDimitry Andric             section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
6670b57cec5SDimitry Andric             section_type == MachO::S_SYMBOL_STUBS) {
6680b57cec5SDimitry Andric           uint32_t stride;
6690b57cec5SDimitry Andric           if (section_type == MachO::S_SYMBOL_STUBS)
6700b57cec5SDimitry Andric             stride = Sec.reserved2;
6710b57cec5SDimitry Andric           else
6720b57cec5SDimitry Andric             stride = 8;
6730b57cec5SDimitry Andric           if (stride == 0) {
6740b57cec5SDimitry Andric             outs() << "Can't print indirect symbols for (" << Sec.segname << ","
6750b57cec5SDimitry Andric                    << Sec.sectname << ") "
6760b57cec5SDimitry Andric                    << "(size of stubs in reserved2 field is zero)\n";
6770b57cec5SDimitry Andric             continue;
6780b57cec5SDimitry Andric           }
6790b57cec5SDimitry Andric           uint32_t count = Sec.size / stride;
6800b57cec5SDimitry Andric           outs() << "Indirect symbols for (" << Sec.segname << ","
6810b57cec5SDimitry Andric                  << Sec.sectname << ") " << count << " entries";
6820b57cec5SDimitry Andric           uint32_t n = Sec.reserved1;
6830b57cec5SDimitry Andric           PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
6840b57cec5SDimitry Andric         }
6850b57cec5SDimitry Andric       }
6860b57cec5SDimitry Andric     } else if (Load.C.cmd == MachO::LC_SEGMENT) {
6870b57cec5SDimitry Andric       MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
6880b57cec5SDimitry Andric       for (unsigned J = 0; J < Seg.nsects; ++J) {
6890b57cec5SDimitry Andric         MachO::section Sec = O->getSection(Load, J);
6900b57cec5SDimitry Andric         uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
6910b57cec5SDimitry Andric         if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
6920b57cec5SDimitry Andric             section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
6930b57cec5SDimitry Andric             section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
6940b57cec5SDimitry Andric             section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
6950b57cec5SDimitry Andric             section_type == MachO::S_SYMBOL_STUBS) {
6960b57cec5SDimitry Andric           uint32_t stride;
6970b57cec5SDimitry Andric           if (section_type == MachO::S_SYMBOL_STUBS)
6980b57cec5SDimitry Andric             stride = Sec.reserved2;
6990b57cec5SDimitry Andric           else
7000b57cec5SDimitry Andric             stride = 4;
7010b57cec5SDimitry Andric           if (stride == 0) {
7020b57cec5SDimitry Andric             outs() << "Can't print indirect symbols for (" << Sec.segname << ","
7030b57cec5SDimitry Andric                    << Sec.sectname << ") "
7040b57cec5SDimitry Andric                    << "(size of stubs in reserved2 field is zero)\n";
7050b57cec5SDimitry Andric             continue;
7060b57cec5SDimitry Andric           }
7070b57cec5SDimitry Andric           uint32_t count = Sec.size / stride;
7080b57cec5SDimitry Andric           outs() << "Indirect symbols for (" << Sec.segname << ","
7090b57cec5SDimitry Andric                  << Sec.sectname << ") " << count << " entries";
7100b57cec5SDimitry Andric           uint32_t n = Sec.reserved1;
7110b57cec5SDimitry Andric           PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
7120b57cec5SDimitry Andric         }
7130b57cec5SDimitry Andric       }
7140b57cec5SDimitry Andric     }
7150b57cec5SDimitry Andric   }
7160b57cec5SDimitry Andric }
7170b57cec5SDimitry Andric 
PrintRType(const uint64_t cputype,const unsigned r_type)7180b57cec5SDimitry Andric static void PrintRType(const uint64_t cputype, const unsigned r_type) {
7190b57cec5SDimitry Andric   static char const *generic_r_types[] = {
7200b57cec5SDimitry Andric     "VANILLA ", "PAIR    ", "SECTDIF ", "PBLAPTR ", "LOCSDIF ", "TLV     ",
7210b57cec5SDimitry Andric     "  6 (?) ", "  7 (?) ", "  8 (?) ", "  9 (?) ", " 10 (?) ", " 11 (?) ",
7220b57cec5SDimitry Andric     " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
7230b57cec5SDimitry Andric   };
7240b57cec5SDimitry Andric   static char const *x86_64_r_types[] = {
7250b57cec5SDimitry Andric     "UNSIGND ", "SIGNED  ", "BRANCH  ", "GOT_LD  ", "GOT     ", "SUB     ",
7260b57cec5SDimitry Andric     "SIGNED1 ", "SIGNED2 ", "SIGNED4 ", "TLV     ", " 10 (?) ", " 11 (?) ",
7270b57cec5SDimitry Andric     " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
7280b57cec5SDimitry Andric   };
7290b57cec5SDimitry Andric   static char const *arm_r_types[] = {
7300b57cec5SDimitry Andric     "VANILLA ", "PAIR    ", "SECTDIFF", "LOCSDIF ", "PBLAPTR ",
7310b57cec5SDimitry Andric     "BR24    ", "T_BR22  ", "T_BR32  ", "HALF    ", "HALFDIF ",
7320b57cec5SDimitry Andric     " 10 (?) ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
7330b57cec5SDimitry Andric   };
7340b57cec5SDimitry Andric   static char const *arm64_r_types[] = {
7350b57cec5SDimitry Andric     "UNSIGND ", "SUB     ", "BR26    ", "PAGE21  ", "PAGOF12 ",
7360b57cec5SDimitry Andric     "GOTLDP  ", "GOTLDPOF", "PTRTGOT ", "TLVLDP  ", "TLVLDPOF",
7370b57cec5SDimitry Andric     "ADDEND  ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
7380b57cec5SDimitry Andric   };
7390b57cec5SDimitry Andric 
7400b57cec5SDimitry Andric   if (r_type > 0xf){
7410b57cec5SDimitry Andric     outs() << format("%-7u", r_type) << " ";
7420b57cec5SDimitry Andric     return;
7430b57cec5SDimitry Andric   }
7440b57cec5SDimitry Andric   switch (cputype) {
7450b57cec5SDimitry Andric     case MachO::CPU_TYPE_I386:
7460b57cec5SDimitry Andric       outs() << generic_r_types[r_type];
7470b57cec5SDimitry Andric       break;
7480b57cec5SDimitry Andric     case MachO::CPU_TYPE_X86_64:
7490b57cec5SDimitry Andric       outs() << x86_64_r_types[r_type];
7500b57cec5SDimitry Andric       break;
7510b57cec5SDimitry Andric     case MachO::CPU_TYPE_ARM:
7520b57cec5SDimitry Andric       outs() << arm_r_types[r_type];
7530b57cec5SDimitry Andric       break;
7540b57cec5SDimitry Andric     case MachO::CPU_TYPE_ARM64:
7550b57cec5SDimitry Andric     case MachO::CPU_TYPE_ARM64_32:
7560b57cec5SDimitry Andric       outs() << arm64_r_types[r_type];
7570b57cec5SDimitry Andric       break;
7580b57cec5SDimitry Andric     default:
7590b57cec5SDimitry Andric       outs() << format("%-7u ", r_type);
7600b57cec5SDimitry Andric   }
7610b57cec5SDimitry Andric }
7620b57cec5SDimitry Andric 
PrintRLength(const uint64_t cputype,const unsigned r_type,const unsigned r_length,const bool previous_arm_half)7630b57cec5SDimitry Andric static void PrintRLength(const uint64_t cputype, const unsigned r_type,
7640b57cec5SDimitry Andric                          const unsigned r_length, const bool previous_arm_half){
7650b57cec5SDimitry Andric   if (cputype == MachO::CPU_TYPE_ARM &&
7660b57cec5SDimitry Andric       (r_type == MachO::ARM_RELOC_HALF ||
7670b57cec5SDimitry Andric        r_type == MachO::ARM_RELOC_HALF_SECTDIFF || previous_arm_half == true)) {
7680b57cec5SDimitry Andric     if ((r_length & 0x1) == 0)
7690b57cec5SDimitry Andric       outs() << "lo/";
7700b57cec5SDimitry Andric     else
7710b57cec5SDimitry Andric       outs() << "hi/";
7720b57cec5SDimitry Andric     if ((r_length & 0x1) == 0)
7730b57cec5SDimitry Andric       outs() << "arm ";
7740b57cec5SDimitry Andric     else
7750b57cec5SDimitry Andric       outs() << "thm ";
7760b57cec5SDimitry Andric   } else {
7770b57cec5SDimitry Andric     switch (r_length) {
7780b57cec5SDimitry Andric       case 0:
7790b57cec5SDimitry Andric         outs() << "byte   ";
7800b57cec5SDimitry Andric         break;
7810b57cec5SDimitry Andric       case 1:
7820b57cec5SDimitry Andric         outs() << "word   ";
7830b57cec5SDimitry Andric         break;
7840b57cec5SDimitry Andric       case 2:
7850b57cec5SDimitry Andric         outs() << "long   ";
7860b57cec5SDimitry Andric         break;
7870b57cec5SDimitry Andric       case 3:
7880b57cec5SDimitry Andric         if (cputype == MachO::CPU_TYPE_X86_64)
7890b57cec5SDimitry Andric           outs() << "quad   ";
7900b57cec5SDimitry Andric         else
7910b57cec5SDimitry Andric           outs() << format("?(%2d)  ", r_length);
7920b57cec5SDimitry Andric         break;
7930b57cec5SDimitry Andric       default:
7940b57cec5SDimitry Andric         outs() << format("?(%2d)  ", r_length);
7950b57cec5SDimitry Andric     }
7960b57cec5SDimitry Andric   }
7970b57cec5SDimitry Andric }
7980b57cec5SDimitry Andric 
PrintRelocationEntries(const MachOObjectFile * O,const relocation_iterator Begin,const relocation_iterator End,const uint64_t cputype,const bool verbose)7990b57cec5SDimitry Andric static void PrintRelocationEntries(const MachOObjectFile *O,
8000b57cec5SDimitry Andric                                    const relocation_iterator Begin,
8010b57cec5SDimitry Andric                                    const relocation_iterator End,
8020b57cec5SDimitry Andric                                    const uint64_t cputype,
8030b57cec5SDimitry Andric                                    const bool verbose) {
8040b57cec5SDimitry Andric   const MachO::symtab_command Symtab = O->getSymtabLoadCommand();
8050b57cec5SDimitry Andric   bool previous_arm_half = false;
8060b57cec5SDimitry Andric   bool previous_sectdiff = false;
8070b57cec5SDimitry Andric   uint32_t sectdiff_r_type = 0;
8080b57cec5SDimitry Andric 
8090b57cec5SDimitry Andric   for (relocation_iterator Reloc = Begin; Reloc != End; ++Reloc) {
8100b57cec5SDimitry Andric     const DataRefImpl Rel = Reloc->getRawDataRefImpl();
8110b57cec5SDimitry Andric     const MachO::any_relocation_info RE = O->getRelocation(Rel);
8120b57cec5SDimitry Andric     const unsigned r_type = O->getAnyRelocationType(RE);
8130b57cec5SDimitry Andric     const bool r_scattered = O->isRelocationScattered(RE);
8140b57cec5SDimitry Andric     const unsigned r_pcrel = O->getAnyRelocationPCRel(RE);
8150b57cec5SDimitry Andric     const unsigned r_length = O->getAnyRelocationLength(RE);
8160b57cec5SDimitry Andric     const unsigned r_address = O->getAnyRelocationAddress(RE);
8170b57cec5SDimitry Andric     const bool r_extern = (r_scattered ? false :
8180b57cec5SDimitry Andric                            O->getPlainRelocationExternal(RE));
8190b57cec5SDimitry Andric     const uint32_t r_value = (r_scattered ?
8200b57cec5SDimitry Andric                               O->getScatteredRelocationValue(RE) : 0);
8210b57cec5SDimitry Andric     const unsigned r_symbolnum = (r_scattered ? 0 :
8220b57cec5SDimitry Andric                                   O->getPlainRelocationSymbolNum(RE));
8230b57cec5SDimitry Andric 
8240b57cec5SDimitry Andric     if (r_scattered && cputype != MachO::CPU_TYPE_X86_64) {
8250b57cec5SDimitry Andric       if (verbose) {
8260b57cec5SDimitry Andric         // scattered: address
8270b57cec5SDimitry Andric         if ((cputype == MachO::CPU_TYPE_I386 &&
8280b57cec5SDimitry Andric              r_type == MachO::GENERIC_RELOC_PAIR) ||
8290b57cec5SDimitry Andric             (cputype == MachO::CPU_TYPE_ARM && r_type == MachO::ARM_RELOC_PAIR))
8300b57cec5SDimitry Andric           outs() << "         ";
8310b57cec5SDimitry Andric         else
8320b57cec5SDimitry Andric           outs() << format("%08x ", (unsigned int)r_address);
8330b57cec5SDimitry Andric 
8340b57cec5SDimitry Andric         // scattered: pcrel
8350b57cec5SDimitry Andric         if (r_pcrel)
8360b57cec5SDimitry Andric           outs() << "True  ";
8370b57cec5SDimitry Andric         else
8380b57cec5SDimitry Andric           outs() << "False ";
8390b57cec5SDimitry Andric 
8400b57cec5SDimitry Andric         // scattered: length
8410b57cec5SDimitry Andric         PrintRLength(cputype, r_type, r_length, previous_arm_half);
8420b57cec5SDimitry Andric 
8430b57cec5SDimitry Andric         // scattered: extern & type
8440b57cec5SDimitry Andric         outs() << "n/a    ";
8450b57cec5SDimitry Andric         PrintRType(cputype, r_type);
8460b57cec5SDimitry Andric 
8470b57cec5SDimitry Andric         // scattered: scattered & value
8480b57cec5SDimitry Andric         outs() << format("True      0x%08x", (unsigned int)r_value);
8490b57cec5SDimitry Andric         if (previous_sectdiff == false) {
8500b57cec5SDimitry Andric           if ((cputype == MachO::CPU_TYPE_ARM &&
8510b57cec5SDimitry Andric                r_type == MachO::ARM_RELOC_PAIR))
8520b57cec5SDimitry Andric             outs() << format(" half = 0x%04x ", (unsigned int)r_address);
8530b57cec5SDimitry Andric         } else if (cputype == MachO::CPU_TYPE_ARM &&
8540b57cec5SDimitry Andric                    sectdiff_r_type == MachO::ARM_RELOC_HALF_SECTDIFF)
8550b57cec5SDimitry Andric           outs() << format(" other_half = 0x%04x ", (unsigned int)r_address);
8560b57cec5SDimitry Andric         if ((cputype == MachO::CPU_TYPE_I386 &&
8570b57cec5SDimitry Andric              (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
8580b57cec5SDimitry Andric               r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) ||
8590b57cec5SDimitry Andric             (cputype == MachO::CPU_TYPE_ARM &&
8600b57cec5SDimitry Andric              (sectdiff_r_type == MachO::ARM_RELOC_SECTDIFF ||
8610b57cec5SDimitry Andric               sectdiff_r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
8620b57cec5SDimitry Andric               sectdiff_r_type == MachO::ARM_RELOC_HALF_SECTDIFF))) {
8630b57cec5SDimitry Andric           previous_sectdiff = true;
8640b57cec5SDimitry Andric           sectdiff_r_type = r_type;
8650b57cec5SDimitry Andric         } else {
8660b57cec5SDimitry Andric           previous_sectdiff = false;
8670b57cec5SDimitry Andric           sectdiff_r_type = 0;
8680b57cec5SDimitry Andric         }
8690b57cec5SDimitry Andric         if (cputype == MachO::CPU_TYPE_ARM &&
8700b57cec5SDimitry Andric             (r_type == MachO::ARM_RELOC_HALF ||
8710b57cec5SDimitry Andric              r_type == MachO::ARM_RELOC_HALF_SECTDIFF))
8720b57cec5SDimitry Andric           previous_arm_half = true;
8730b57cec5SDimitry Andric         else
8740b57cec5SDimitry Andric           previous_arm_half = false;
8750b57cec5SDimitry Andric         outs() << "\n";
8760b57cec5SDimitry Andric       }
8770b57cec5SDimitry Andric       else {
8780b57cec5SDimitry Andric         // scattered: address pcrel length extern type scattered value
8790b57cec5SDimitry Andric         outs() << format("%08x %1d     %-2d     n/a    %-7d 1         0x%08x\n",
8800b57cec5SDimitry Andric                          (unsigned int)r_address, r_pcrel, r_length, r_type,
8810b57cec5SDimitry Andric                          (unsigned int)r_value);
8820b57cec5SDimitry Andric       }
8830b57cec5SDimitry Andric     }
8840b57cec5SDimitry Andric     else {
8850b57cec5SDimitry Andric       if (verbose) {
8860b57cec5SDimitry Andric         // plain: address
8870b57cec5SDimitry Andric         if (cputype == MachO::CPU_TYPE_ARM && r_type == MachO::ARM_RELOC_PAIR)
8880b57cec5SDimitry Andric           outs() << "         ";
8890b57cec5SDimitry Andric         else
8900b57cec5SDimitry Andric           outs() << format("%08x ", (unsigned int)r_address);
8910b57cec5SDimitry Andric 
8920b57cec5SDimitry Andric         // plain: pcrel
8930b57cec5SDimitry Andric         if (r_pcrel)
8940b57cec5SDimitry Andric           outs() << "True  ";
8950b57cec5SDimitry Andric         else
8960b57cec5SDimitry Andric           outs() << "False ";
8970b57cec5SDimitry Andric 
8980b57cec5SDimitry Andric         // plain: length
8990b57cec5SDimitry Andric         PrintRLength(cputype, r_type, r_length, previous_arm_half);
9000b57cec5SDimitry Andric 
9010b57cec5SDimitry Andric         if (r_extern) {
9020b57cec5SDimitry Andric           // plain: extern & type & scattered
9030b57cec5SDimitry Andric           outs() << "True   ";
9040b57cec5SDimitry Andric           PrintRType(cputype, r_type);
9050b57cec5SDimitry Andric           outs() << "False     ";
9060b57cec5SDimitry Andric 
9070b57cec5SDimitry Andric           // plain: symbolnum/value
9080b57cec5SDimitry Andric           if (r_symbolnum > Symtab.nsyms)
9090b57cec5SDimitry Andric             outs() << format("?(%d)\n", r_symbolnum);
9100b57cec5SDimitry Andric           else {
9110b57cec5SDimitry Andric             SymbolRef Symbol = *O->getSymbolByIndex(r_symbolnum);
9120b57cec5SDimitry Andric             Expected<StringRef> SymNameNext = Symbol.getName();
91304eeddc0SDimitry Andric             const char *name = nullptr;
9140b57cec5SDimitry Andric             if (SymNameNext)
9150b57cec5SDimitry Andric               name = SymNameNext->data();
91604eeddc0SDimitry Andric             if (name == nullptr)
9170b57cec5SDimitry Andric               outs() << format("?(%d)\n", r_symbolnum);
9180b57cec5SDimitry Andric             else
9190b57cec5SDimitry Andric               outs() << name << "\n";
9200b57cec5SDimitry Andric           }
9210b57cec5SDimitry Andric         }
9220b57cec5SDimitry Andric         else {
9230b57cec5SDimitry Andric           // plain: extern & type & scattered
9240b57cec5SDimitry Andric           outs() << "False  ";
9250b57cec5SDimitry Andric           PrintRType(cputype, r_type);
9260b57cec5SDimitry Andric           outs() << "False     ";
9270b57cec5SDimitry Andric 
9280b57cec5SDimitry Andric           // plain: symbolnum/value
9290b57cec5SDimitry Andric           if (cputype == MachO::CPU_TYPE_ARM && r_type == MachO::ARM_RELOC_PAIR)
9300b57cec5SDimitry Andric             outs() << format("other_half = 0x%04x\n", (unsigned int)r_address);
9310b57cec5SDimitry Andric           else if ((cputype == MachO::CPU_TYPE_ARM64 ||
9320b57cec5SDimitry Andric                     cputype == MachO::CPU_TYPE_ARM64_32) &&
9330b57cec5SDimitry Andric                    r_type == MachO::ARM64_RELOC_ADDEND)
9340b57cec5SDimitry Andric             outs() << format("addend = 0x%06x\n", (unsigned int)r_symbolnum);
9350b57cec5SDimitry Andric           else {
9360b57cec5SDimitry Andric             outs() << format("%d ", r_symbolnum);
9370b57cec5SDimitry Andric             if (r_symbolnum == MachO::R_ABS)
9380b57cec5SDimitry Andric               outs() << "R_ABS\n";
9390b57cec5SDimitry Andric             else {
9400b57cec5SDimitry Andric               // in this case, r_symbolnum is actually a 1-based section number
9410b57cec5SDimitry Andric               uint32_t nsects = O->section_end()->getRawDataRefImpl().d.a;
9420b57cec5SDimitry Andric               if (r_symbolnum > 0 && r_symbolnum <= nsects) {
9430b57cec5SDimitry Andric                 object::DataRefImpl DRI;
9440b57cec5SDimitry Andric                 DRI.d.a = r_symbolnum-1;
9450b57cec5SDimitry Andric                 StringRef SegName = O->getSectionFinalSegmentName(DRI);
9460b57cec5SDimitry Andric                 if (Expected<StringRef> NameOrErr = O->getSectionName(DRI))
9470b57cec5SDimitry Andric                   outs() << "(" << SegName << "," << *NameOrErr << ")\n";
9480b57cec5SDimitry Andric                 else
9490b57cec5SDimitry Andric                   outs() << "(?,?)\n";
9500b57cec5SDimitry Andric               }
9510b57cec5SDimitry Andric               else {
9520b57cec5SDimitry Andric                 outs() << "(?,?)\n";
9530b57cec5SDimitry Andric               }
9540b57cec5SDimitry Andric             }
9550b57cec5SDimitry Andric           }
9560b57cec5SDimitry Andric         }
9570b57cec5SDimitry Andric         if (cputype == MachO::CPU_TYPE_ARM &&
9580b57cec5SDimitry Andric             (r_type == MachO::ARM_RELOC_HALF ||
9590b57cec5SDimitry Andric              r_type == MachO::ARM_RELOC_HALF_SECTDIFF))
9600b57cec5SDimitry Andric           previous_arm_half = true;
9610b57cec5SDimitry Andric         else
9620b57cec5SDimitry Andric           previous_arm_half = false;
9630b57cec5SDimitry Andric       }
9640b57cec5SDimitry Andric       else {
9650b57cec5SDimitry Andric         // plain: address pcrel length extern type scattered symbolnum/section
9660b57cec5SDimitry Andric         outs() << format("%08x %1d     %-2d     %1d      %-7d 0         %d\n",
9670b57cec5SDimitry Andric                          (unsigned int)r_address, r_pcrel, r_length, r_extern,
9680b57cec5SDimitry Andric                          r_type, r_symbolnum);
9690b57cec5SDimitry Andric       }
9700b57cec5SDimitry Andric     }
9710b57cec5SDimitry Andric   }
9720b57cec5SDimitry Andric }
9730b57cec5SDimitry Andric 
PrintRelocations(const MachOObjectFile * O,const bool verbose)9740b57cec5SDimitry Andric static void PrintRelocations(const MachOObjectFile *O, const bool verbose) {
9750b57cec5SDimitry Andric   const uint64_t cputype = O->getHeader().cputype;
9760b57cec5SDimitry Andric   const MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
9770b57cec5SDimitry Andric   if (Dysymtab.nextrel != 0) {
9780b57cec5SDimitry Andric     outs() << "External relocation information " << Dysymtab.nextrel
9790b57cec5SDimitry Andric            << " entries";
9800b57cec5SDimitry Andric     outs() << "\naddress  pcrel length extern type    scattered "
9810b57cec5SDimitry Andric               "symbolnum/value\n";
9820b57cec5SDimitry Andric     PrintRelocationEntries(O, O->extrel_begin(), O->extrel_end(), cputype,
9830b57cec5SDimitry Andric                            verbose);
9840b57cec5SDimitry Andric   }
9850b57cec5SDimitry Andric   if (Dysymtab.nlocrel != 0) {
9860b57cec5SDimitry Andric     outs() << format("Local relocation information %u entries",
9870b57cec5SDimitry Andric                      Dysymtab.nlocrel);
9880b57cec5SDimitry Andric     outs() << "\naddress  pcrel length extern type    scattered "
9890b57cec5SDimitry Andric               "symbolnum/value\n";
9900b57cec5SDimitry Andric     PrintRelocationEntries(O, O->locrel_begin(), O->locrel_end(), cputype,
9910b57cec5SDimitry Andric                            verbose);
9920b57cec5SDimitry Andric   }
9930b57cec5SDimitry Andric   for (const auto &Load : O->load_commands()) {
9940b57cec5SDimitry Andric     if (Load.C.cmd == MachO::LC_SEGMENT_64) {
9950b57cec5SDimitry Andric       const MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
9960b57cec5SDimitry Andric       for (unsigned J = 0; J < Seg.nsects; ++J) {
9970b57cec5SDimitry Andric         const MachO::section_64 Sec = O->getSection64(Load, J);
9980b57cec5SDimitry Andric         if (Sec.nreloc != 0) {
9990b57cec5SDimitry Andric           DataRefImpl DRI;
10000b57cec5SDimitry Andric           DRI.d.a = J;
10010b57cec5SDimitry Andric           const StringRef SegName = O->getSectionFinalSegmentName(DRI);
10020b57cec5SDimitry Andric           if (Expected<StringRef> NameOrErr = O->getSectionName(DRI))
10030b57cec5SDimitry Andric             outs() << "Relocation information (" << SegName << "," << *NameOrErr
10040b57cec5SDimitry Andric                    << format(") %u entries", Sec.nreloc);
10050b57cec5SDimitry Andric           else
10060b57cec5SDimitry Andric             outs() << "Relocation information (" << SegName << ",?) "
10070b57cec5SDimitry Andric                    << format("%u entries", Sec.nreloc);
10080b57cec5SDimitry Andric           outs() << "\naddress  pcrel length extern type    scattered "
10090b57cec5SDimitry Andric                     "symbolnum/value\n";
10100b57cec5SDimitry Andric           PrintRelocationEntries(O, O->section_rel_begin(DRI),
10110b57cec5SDimitry Andric                                  O->section_rel_end(DRI), cputype, verbose);
10120b57cec5SDimitry Andric         }
10130b57cec5SDimitry Andric       }
10140b57cec5SDimitry Andric     } else if (Load.C.cmd == MachO::LC_SEGMENT) {
10150b57cec5SDimitry Andric       const MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
10160b57cec5SDimitry Andric       for (unsigned J = 0; J < Seg.nsects; ++J) {
10170b57cec5SDimitry Andric         const MachO::section Sec = O->getSection(Load, J);
10180b57cec5SDimitry Andric         if (Sec.nreloc != 0) {
10190b57cec5SDimitry Andric           DataRefImpl DRI;
10200b57cec5SDimitry Andric           DRI.d.a = J;
10210b57cec5SDimitry Andric           const StringRef SegName = O->getSectionFinalSegmentName(DRI);
10220b57cec5SDimitry Andric           if (Expected<StringRef> NameOrErr = O->getSectionName(DRI))
10230b57cec5SDimitry Andric             outs() << "Relocation information (" << SegName << "," << *NameOrErr
10240b57cec5SDimitry Andric                    << format(") %u entries", Sec.nreloc);
10250b57cec5SDimitry Andric           else
10260b57cec5SDimitry Andric             outs() << "Relocation information (" << SegName << ",?) "
10270b57cec5SDimitry Andric                    << format("%u entries", Sec.nreloc);
10280b57cec5SDimitry Andric           outs() << "\naddress  pcrel length extern type    scattered "
10290b57cec5SDimitry Andric                     "symbolnum/value\n";
10300b57cec5SDimitry Andric           PrintRelocationEntries(O, O->section_rel_begin(DRI),
10310b57cec5SDimitry Andric                                  O->section_rel_end(DRI), cputype, verbose);
10320b57cec5SDimitry Andric         }
10330b57cec5SDimitry Andric       }
10340b57cec5SDimitry Andric     }
10350b57cec5SDimitry Andric   }
10360b57cec5SDimitry Andric }
10370b57cec5SDimitry Andric 
PrintFunctionStarts(MachOObjectFile * O)1038fe6060f1SDimitry Andric static void PrintFunctionStarts(MachOObjectFile *O) {
1039fe6060f1SDimitry Andric   uint64_t BaseSegmentAddress = 0;
1040fe6060f1SDimitry Andric   for (const MachOObjectFile::LoadCommandInfo &Command : O->load_commands()) {
1041fe6060f1SDimitry Andric     if (Command.C.cmd == MachO::LC_SEGMENT) {
1042fe6060f1SDimitry Andric       MachO::segment_command SLC = O->getSegmentLoadCommand(Command);
1043fe6060f1SDimitry Andric       if (StringRef(SLC.segname) == "__TEXT") {
1044fe6060f1SDimitry Andric         BaseSegmentAddress = SLC.vmaddr;
1045fe6060f1SDimitry Andric         break;
1046fe6060f1SDimitry Andric       }
1047fe6060f1SDimitry Andric     } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
1048fe6060f1SDimitry Andric       MachO::segment_command_64 SLC = O->getSegment64LoadCommand(Command);
1049fe6060f1SDimitry Andric       if (StringRef(SLC.segname) == "__TEXT") {
1050fe6060f1SDimitry Andric         BaseSegmentAddress = SLC.vmaddr;
1051fe6060f1SDimitry Andric         break;
1052fe6060f1SDimitry Andric       }
1053fe6060f1SDimitry Andric     }
1054fe6060f1SDimitry Andric   }
1055fe6060f1SDimitry Andric 
1056fe6060f1SDimitry Andric   SmallVector<uint64_t, 8> FunctionStarts;
1057fe6060f1SDimitry Andric   for (const MachOObjectFile::LoadCommandInfo &LC : O->load_commands()) {
1058fe6060f1SDimitry Andric     if (LC.C.cmd == MachO::LC_FUNCTION_STARTS) {
1059fe6060f1SDimitry Andric       MachO::linkedit_data_command FunctionStartsLC =
1060fe6060f1SDimitry Andric           O->getLinkeditDataLoadCommand(LC);
1061fe6060f1SDimitry Andric       O->ReadULEB128s(FunctionStartsLC.dataoff, FunctionStarts);
1062fe6060f1SDimitry Andric       break;
1063fe6060f1SDimitry Andric     }
1064fe6060f1SDimitry Andric   }
1065fe6060f1SDimitry Andric 
1066bdd1243dSDimitry Andric   DenseMap<uint64_t, StringRef> SymbolNames;
1067bdd1243dSDimitry Andric   if (FunctionStartsType == FunctionStartsMode::Names ||
1068bdd1243dSDimitry Andric       FunctionStartsType == FunctionStartsMode::Both) {
1069bdd1243dSDimitry Andric     for (SymbolRef Sym : O->symbols()) {
1070bdd1243dSDimitry Andric       if (Expected<uint64_t> Addr = Sym.getAddress()) {
1071bdd1243dSDimitry Andric         if (Expected<StringRef> Name = Sym.getName()) {
1072bdd1243dSDimitry Andric           SymbolNames[*Addr] = *Name;
1073bdd1243dSDimitry Andric         }
1074bdd1243dSDimitry Andric       }
1075bdd1243dSDimitry Andric     }
1076bdd1243dSDimitry Andric   }
1077bdd1243dSDimitry Andric 
1078fe6060f1SDimitry Andric   for (uint64_t S : FunctionStarts) {
1079fe6060f1SDimitry Andric     uint64_t Addr = BaseSegmentAddress + S;
1080bdd1243dSDimitry Andric     if (FunctionStartsType == FunctionStartsMode::Names) {
1081bdd1243dSDimitry Andric       auto It = SymbolNames.find(Addr);
1082bdd1243dSDimitry Andric       if (It != SymbolNames.end())
1083bdd1243dSDimitry Andric         outs() << It->second << "\n";
1084bdd1243dSDimitry Andric     } else {
1085fe6060f1SDimitry Andric       if (O->is64Bit())
1086bdd1243dSDimitry Andric         outs() << format("%016" PRIx64, Addr);
1087fe6060f1SDimitry Andric       else
1088bdd1243dSDimitry Andric         outs() << format("%08" PRIx32, static_cast<uint32_t>(Addr));
1089bdd1243dSDimitry Andric 
1090bdd1243dSDimitry Andric       if (FunctionStartsType == FunctionStartsMode::Both) {
1091bdd1243dSDimitry Andric         auto It = SymbolNames.find(Addr);
1092bdd1243dSDimitry Andric         if (It != SymbolNames.end())
1093bdd1243dSDimitry Andric           outs() << " " << It->second;
1094bdd1243dSDimitry Andric         else
1095bdd1243dSDimitry Andric           outs() << " ?";
1096bdd1243dSDimitry Andric       }
1097bdd1243dSDimitry Andric       outs() << "\n";
1098bdd1243dSDimitry Andric     }
1099fe6060f1SDimitry Andric   }
1100fe6060f1SDimitry Andric }
1101fe6060f1SDimitry Andric 
PrintDataInCodeTable(MachOObjectFile * O,bool verbose)11020b57cec5SDimitry Andric static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
11030b57cec5SDimitry Andric   MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
11040b57cec5SDimitry Andric   uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
11050b57cec5SDimitry Andric   outs() << "Data in code table (" << nentries << " entries)\n";
11060b57cec5SDimitry Andric   outs() << "offset     length kind\n";
11070b57cec5SDimitry Andric   for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
11080b57cec5SDimitry Andric        ++DI) {
11090b57cec5SDimitry Andric     uint32_t Offset;
11100b57cec5SDimitry Andric     DI->getOffset(Offset);
11110b57cec5SDimitry Andric     outs() << format("0x%08" PRIx32, Offset) << " ";
11120b57cec5SDimitry Andric     uint16_t Length;
11130b57cec5SDimitry Andric     DI->getLength(Length);
11140b57cec5SDimitry Andric     outs() << format("%6u", Length) << " ";
11150b57cec5SDimitry Andric     uint16_t Kind;
11160b57cec5SDimitry Andric     DI->getKind(Kind);
11170b57cec5SDimitry Andric     if (verbose) {
11180b57cec5SDimitry Andric       switch (Kind) {
11190b57cec5SDimitry Andric       case MachO::DICE_KIND_DATA:
11200b57cec5SDimitry Andric         outs() << "DATA";
11210b57cec5SDimitry Andric         break;
11220b57cec5SDimitry Andric       case MachO::DICE_KIND_JUMP_TABLE8:
11230b57cec5SDimitry Andric         outs() << "JUMP_TABLE8";
11240b57cec5SDimitry Andric         break;
11250b57cec5SDimitry Andric       case MachO::DICE_KIND_JUMP_TABLE16:
11260b57cec5SDimitry Andric         outs() << "JUMP_TABLE16";
11270b57cec5SDimitry Andric         break;
11280b57cec5SDimitry Andric       case MachO::DICE_KIND_JUMP_TABLE32:
11290b57cec5SDimitry Andric         outs() << "JUMP_TABLE32";
11300b57cec5SDimitry Andric         break;
11310b57cec5SDimitry Andric       case MachO::DICE_KIND_ABS_JUMP_TABLE32:
11320b57cec5SDimitry Andric         outs() << "ABS_JUMP_TABLE32";
11330b57cec5SDimitry Andric         break;
11340b57cec5SDimitry Andric       default:
11350b57cec5SDimitry Andric         outs() << format("0x%04" PRIx32, Kind);
11360b57cec5SDimitry Andric         break;
11370b57cec5SDimitry Andric       }
11380b57cec5SDimitry Andric     } else
11390b57cec5SDimitry Andric       outs() << format("0x%04" PRIx32, Kind);
11400b57cec5SDimitry Andric     outs() << "\n";
11410b57cec5SDimitry Andric   }
11420b57cec5SDimitry Andric }
11430b57cec5SDimitry Andric 
PrintLinkOptHints(MachOObjectFile * O)11440b57cec5SDimitry Andric static void PrintLinkOptHints(MachOObjectFile *O) {
11450b57cec5SDimitry Andric   MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
11460b57cec5SDimitry Andric   const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
11470b57cec5SDimitry Andric   uint32_t nloh = LohLC.datasize;
11480b57cec5SDimitry Andric   outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
11490b57cec5SDimitry Andric   for (uint32_t i = 0; i < nloh;) {
11500b57cec5SDimitry Andric     unsigned n;
11510b57cec5SDimitry Andric     uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
11520b57cec5SDimitry Andric     i += n;
11530b57cec5SDimitry Andric     outs() << "    identifier " << identifier << " ";
11540b57cec5SDimitry Andric     if (i >= nloh)
11550b57cec5SDimitry Andric       return;
11560b57cec5SDimitry Andric     switch (identifier) {
11570b57cec5SDimitry Andric     case 1:
11580b57cec5SDimitry Andric       outs() << "AdrpAdrp\n";
11590b57cec5SDimitry Andric       break;
11600b57cec5SDimitry Andric     case 2:
11610b57cec5SDimitry Andric       outs() << "AdrpLdr\n";
11620b57cec5SDimitry Andric       break;
11630b57cec5SDimitry Andric     case 3:
11640b57cec5SDimitry Andric       outs() << "AdrpAddLdr\n";
11650b57cec5SDimitry Andric       break;
11660b57cec5SDimitry Andric     case 4:
11670b57cec5SDimitry Andric       outs() << "AdrpLdrGotLdr\n";
11680b57cec5SDimitry Andric       break;
11690b57cec5SDimitry Andric     case 5:
11700b57cec5SDimitry Andric       outs() << "AdrpAddStr\n";
11710b57cec5SDimitry Andric       break;
11720b57cec5SDimitry Andric     case 6:
11730b57cec5SDimitry Andric       outs() << "AdrpLdrGotStr\n";
11740b57cec5SDimitry Andric       break;
11750b57cec5SDimitry Andric     case 7:
11760b57cec5SDimitry Andric       outs() << "AdrpAdd\n";
11770b57cec5SDimitry Andric       break;
11780b57cec5SDimitry Andric     case 8:
11790b57cec5SDimitry Andric       outs() << "AdrpLdrGot\n";
11800b57cec5SDimitry Andric       break;
11810b57cec5SDimitry Andric     default:
11820b57cec5SDimitry Andric       outs() << "Unknown identifier value\n";
11830b57cec5SDimitry Andric       break;
11840b57cec5SDimitry Andric     }
11850b57cec5SDimitry Andric     uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
11860b57cec5SDimitry Andric     i += n;
11870b57cec5SDimitry Andric     outs() << "    narguments " << narguments << "\n";
11880b57cec5SDimitry Andric     if (i >= nloh)
11890b57cec5SDimitry Andric       return;
11900b57cec5SDimitry Andric 
11910b57cec5SDimitry Andric     for (uint32_t j = 0; j < narguments; j++) {
11920b57cec5SDimitry Andric       uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
11930b57cec5SDimitry Andric       i += n;
11940b57cec5SDimitry Andric       outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
11950b57cec5SDimitry Andric       if (i >= nloh)
11960b57cec5SDimitry Andric         return;
11970b57cec5SDimitry Andric     }
11980b57cec5SDimitry Andric   }
11990b57cec5SDimitry Andric }
12000b57cec5SDimitry Andric 
GetSegmentNames(object::MachOObjectFile * O)1201bdd1243dSDimitry Andric static SmallVector<std::string> GetSegmentNames(object::MachOObjectFile *O) {
1202bdd1243dSDimitry Andric   SmallVector<std::string> Ret;
1203bdd1243dSDimitry Andric   for (const MachOObjectFile::LoadCommandInfo &Command : O->load_commands()) {
1204bdd1243dSDimitry Andric     if (Command.C.cmd == MachO::LC_SEGMENT) {
1205bdd1243dSDimitry Andric       MachO::segment_command SLC = O->getSegmentLoadCommand(Command);
1206bdd1243dSDimitry Andric       Ret.push_back(SLC.segname);
1207bdd1243dSDimitry Andric     } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
1208bdd1243dSDimitry Andric       MachO::segment_command_64 SLC = O->getSegment64LoadCommand(Command);
1209bdd1243dSDimitry Andric       Ret.push_back(SLC.segname);
121081ad6265SDimitry Andric     }
1211bdd1243dSDimitry Andric   }
1212bdd1243dSDimitry Andric   return Ret;
1213bdd1243dSDimitry Andric }
1214bdd1243dSDimitry Andric 
1215bdd1243dSDimitry Andric static void
PrintChainedFixupsHeader(const MachO::dyld_chained_fixups_header & H)1216bdd1243dSDimitry Andric PrintChainedFixupsHeader(const MachO::dyld_chained_fixups_header &H) {
1217bdd1243dSDimitry Andric   outs() << "chained fixups header (LC_DYLD_CHAINED_FIXUPS)\n";
1218bdd1243dSDimitry Andric   outs() << "  fixups_version = " << H.fixups_version << '\n';
1219bdd1243dSDimitry Andric   outs() << "  starts_offset  = " << H.starts_offset << '\n';
1220bdd1243dSDimitry Andric   outs() << "  imports_offset = " << H.imports_offset << '\n';
1221bdd1243dSDimitry Andric   outs() << "  symbols_offset = " << H.symbols_offset << '\n';
1222bdd1243dSDimitry Andric   outs() << "  imports_count  = " << H.imports_count << '\n';
1223bdd1243dSDimitry Andric 
1224bdd1243dSDimitry Andric   outs() << "  imports_format = " << H.imports_format;
1225bdd1243dSDimitry Andric   switch (H.imports_format) {
1226bdd1243dSDimitry Andric   case llvm::MachO::DYLD_CHAINED_IMPORT:
1227bdd1243dSDimitry Andric     outs() << " (DYLD_CHAINED_IMPORT)";
1228bdd1243dSDimitry Andric     break;
1229bdd1243dSDimitry Andric   case llvm::MachO::DYLD_CHAINED_IMPORT_ADDEND:
1230bdd1243dSDimitry Andric     outs() << " (DYLD_CHAINED_IMPORT_ADDEND)";
1231bdd1243dSDimitry Andric     break;
1232bdd1243dSDimitry Andric   case llvm::MachO::DYLD_CHAINED_IMPORT_ADDEND64:
1233bdd1243dSDimitry Andric     outs() << " (DYLD_CHAINED_IMPORT_ADDEND64)";
1234bdd1243dSDimitry Andric     break;
1235bdd1243dSDimitry Andric   }
1236bdd1243dSDimitry Andric   outs() << '\n';
1237bdd1243dSDimitry Andric 
1238bdd1243dSDimitry Andric   outs() << "  symbols_format = " << H.symbols_format;
1239bdd1243dSDimitry Andric   if (H.symbols_format == llvm::MachO::DYLD_CHAINED_SYMBOL_ZLIB)
1240bdd1243dSDimitry Andric     outs() << " (zlib compressed)";
1241bdd1243dSDimitry Andric   outs() << '\n';
1242bdd1243dSDimitry Andric }
1243bdd1243dSDimitry Andric 
1244bdd1243dSDimitry Andric static constexpr std::array<StringRef, 13> PointerFormats{
1245bdd1243dSDimitry Andric     "DYLD_CHAINED_PTR_ARM64E",
1246bdd1243dSDimitry Andric     "DYLD_CHAINED_PTR_64",
1247bdd1243dSDimitry Andric     "DYLD_CHAINED_PTR_32",
1248bdd1243dSDimitry Andric     "DYLD_CHAINED_PTR_32_CACHE",
1249bdd1243dSDimitry Andric     "DYLD_CHAINED_PTR_32_FIRMWARE",
1250bdd1243dSDimitry Andric     "DYLD_CHAINED_PTR_64_OFFSET",
1251bdd1243dSDimitry Andric     "DYLD_CHAINED_PTR_ARM64E_KERNEL",
1252bdd1243dSDimitry Andric     "DYLD_CHAINED_PTR_64_KERNEL_CACHE",
1253bdd1243dSDimitry Andric     "DYLD_CHAINED_PTR_ARM64E_USERLAND",
1254bdd1243dSDimitry Andric     "DYLD_CHAINED_PTR_ARM64E_FIRMWARE",
1255bdd1243dSDimitry Andric     "DYLD_CHAINED_PTR_X86_64_KERNEL_CACHE",
1256bdd1243dSDimitry Andric     "DYLD_CHAINED_PTR_ARM64E_USERLAND24",
1257bdd1243dSDimitry Andric };
1258bdd1243dSDimitry Andric 
PrintChainedFixupsSegment(const ChainedFixupsSegment & Segment,StringRef SegName)1259bdd1243dSDimitry Andric static void PrintChainedFixupsSegment(const ChainedFixupsSegment &Segment,
1260bdd1243dSDimitry Andric                                       StringRef SegName) {
1261bdd1243dSDimitry Andric   outs() << "chained starts in segment " << Segment.SegIdx << " (" << SegName
1262bdd1243dSDimitry Andric          << ")\n";
1263bdd1243dSDimitry Andric   outs() << "  size = " << Segment.Header.size << '\n';
1264bdd1243dSDimitry Andric   outs() << "  page_size = " << format("0x%0" PRIx16, Segment.Header.page_size)
1265bdd1243dSDimitry Andric          << '\n';
1266bdd1243dSDimitry Andric 
1267bdd1243dSDimitry Andric   outs() << "  pointer_format = " << Segment.Header.pointer_format;
1268bdd1243dSDimitry Andric   if ((Segment.Header.pointer_format - 1) <
1269bdd1243dSDimitry Andric       MachO::DYLD_CHAINED_PTR_ARM64E_USERLAND24)
1270bdd1243dSDimitry Andric     outs() << " (" << PointerFormats[Segment.Header.pointer_format - 1] << ")";
1271bdd1243dSDimitry Andric   outs() << '\n';
1272bdd1243dSDimitry Andric 
1273bdd1243dSDimitry Andric   outs() << "  segment_offset = "
1274bdd1243dSDimitry Andric          << format("0x%0" PRIx64, Segment.Header.segment_offset) << '\n';
1275bdd1243dSDimitry Andric   outs() << "  max_valid_pointer = " << Segment.Header.max_valid_pointer
1276bdd1243dSDimitry Andric          << '\n';
1277bdd1243dSDimitry Andric   outs() << "  page_count = " << Segment.Header.page_count << '\n';
1278bdd1243dSDimitry Andric   for (auto [Index, PageStart] : enumerate(Segment.PageStarts)) {
1279bdd1243dSDimitry Andric     outs() << "    page_start[" << Index << "] = " << PageStart;
1280bdd1243dSDimitry Andric     // FIXME: Support DYLD_CHAINED_PTR_START_MULTI (32-bit only)
1281bdd1243dSDimitry Andric     if (PageStart == MachO::DYLD_CHAINED_PTR_START_NONE)
1282bdd1243dSDimitry Andric       outs() << " (DYLD_CHAINED_PTR_START_NONE)";
1283bdd1243dSDimitry Andric     outs() << '\n';
1284bdd1243dSDimitry Andric   }
1285bdd1243dSDimitry Andric }
1286bdd1243dSDimitry Andric 
PrintChainedFixupTarget(ChainedFixupTarget & Target,size_t Idx,int Format,MachOObjectFile * O)1287bdd1243dSDimitry Andric static void PrintChainedFixupTarget(ChainedFixupTarget &Target, size_t Idx,
1288bdd1243dSDimitry Andric                                     int Format, MachOObjectFile *O) {
1289bdd1243dSDimitry Andric   if (Format == MachO::DYLD_CHAINED_IMPORT)
1290bdd1243dSDimitry Andric     outs() << "dyld chained import";
1291bdd1243dSDimitry Andric   else if (Format == MachO::DYLD_CHAINED_IMPORT_ADDEND)
1292bdd1243dSDimitry Andric     outs() << "dyld chained import addend";
1293bdd1243dSDimitry Andric   else if (Format == MachO::DYLD_CHAINED_IMPORT_ADDEND64)
1294bdd1243dSDimitry Andric     outs() << "dyld chained import addend64";
1295bdd1243dSDimitry Andric   // FIXME: otool prints the encoded value as well.
1296bdd1243dSDimitry Andric   outs() << '[' << Idx << "]\n";
1297bdd1243dSDimitry Andric 
1298bdd1243dSDimitry Andric   outs() << "  lib_ordinal = " << Target.libOrdinal() << " ("
1299bdd1243dSDimitry Andric          << ordinalName(O, Target.libOrdinal()) << ")\n";
1300bdd1243dSDimitry Andric   outs() << "  weak_import = " << Target.weakImport() << '\n';
1301bdd1243dSDimitry Andric   outs() << "  name_offset = " << Target.nameOffset() << " ("
1302bdd1243dSDimitry Andric          << Target.symbolName() << ")\n";
1303bdd1243dSDimitry Andric   if (Format != MachO::DYLD_CHAINED_IMPORT)
1304bdd1243dSDimitry Andric     outs() << "  addend      = " << (int64_t)Target.addend() << '\n';
1305bdd1243dSDimitry Andric }
1306bdd1243dSDimitry Andric 
PrintChainedFixups(MachOObjectFile * O)1307bdd1243dSDimitry Andric static void PrintChainedFixups(MachOObjectFile *O) {
1308bdd1243dSDimitry Andric   // MachOObjectFile::getChainedFixupsHeader() reads LC_DYLD_CHAINED_FIXUPS.
1309bdd1243dSDimitry Andric   // FIXME: Support chained fixups in __TEXT,__chain_starts section too.
1310bdd1243dSDimitry Andric   auto ChainedFixupHeader =
1311bdd1243dSDimitry Andric       unwrapOrError(O->getChainedFixupsHeader(), O->getFileName());
1312bdd1243dSDimitry Andric   if (!ChainedFixupHeader)
1313bdd1243dSDimitry Andric     return;
1314bdd1243dSDimitry Andric 
1315bdd1243dSDimitry Andric   PrintChainedFixupsHeader(*ChainedFixupHeader);
1316bdd1243dSDimitry Andric 
1317bdd1243dSDimitry Andric   auto [SegCount, Segments] =
1318bdd1243dSDimitry Andric       unwrapOrError(O->getChainedFixupsSegments(), O->getFileName());
1319bdd1243dSDimitry Andric 
1320bdd1243dSDimitry Andric   auto SegNames = GetSegmentNames(O);
1321bdd1243dSDimitry Andric 
1322bdd1243dSDimitry Andric   size_t StartsIdx = 0;
1323bdd1243dSDimitry Andric   outs() << "chained starts in image\n";
1324bdd1243dSDimitry Andric   outs() << "  seg_count = " << SegCount << '\n';
1325bdd1243dSDimitry Andric   for (size_t I = 0; I < SegCount; ++I) {
1326bdd1243dSDimitry Andric     uint64_t SegOffset = 0;
1327bdd1243dSDimitry Andric     if (StartsIdx < Segments.size() && I == Segments[StartsIdx].SegIdx) {
1328bdd1243dSDimitry Andric       SegOffset = Segments[StartsIdx].Offset;
1329bdd1243dSDimitry Andric       ++StartsIdx;
1330bdd1243dSDimitry Andric     }
1331bdd1243dSDimitry Andric 
1332bdd1243dSDimitry Andric     outs() << "    seg_offset[" << I << "] = " << SegOffset << " ("
1333bdd1243dSDimitry Andric            << SegNames[I] << ")\n";
1334bdd1243dSDimitry Andric   }
1335bdd1243dSDimitry Andric 
1336bdd1243dSDimitry Andric   for (const ChainedFixupsSegment &S : Segments)
1337bdd1243dSDimitry Andric     PrintChainedFixupsSegment(S, SegNames[S.SegIdx]);
1338bdd1243dSDimitry Andric 
1339bdd1243dSDimitry Andric   auto FixupTargets =
1340bdd1243dSDimitry Andric       unwrapOrError(O->getDyldChainedFixupTargets(), O->getFileName());
1341bdd1243dSDimitry Andric 
1342bdd1243dSDimitry Andric   uint32_t ImportsFormat = ChainedFixupHeader->imports_format;
1343bdd1243dSDimitry Andric   for (auto [Idx, Target] : enumerate(FixupTargets))
1344bdd1243dSDimitry Andric     PrintChainedFixupTarget(Target, Idx, ImportsFormat, O);
134581ad6265SDimitry Andric }
134681ad6265SDimitry Andric 
PrintDyldInfo(MachOObjectFile * O)134781ad6265SDimitry Andric static void PrintDyldInfo(MachOObjectFile *O) {
1348bdd1243dSDimitry Andric   Error Err = Error::success();
1349bdd1243dSDimitry Andric 
1350bdd1243dSDimitry Andric   size_t SegmentWidth = strlen("segment");
1351bdd1243dSDimitry Andric   size_t SectionWidth = strlen("section");
1352bdd1243dSDimitry Andric   size_t AddressWidth = strlen("address");
1353bdd1243dSDimitry Andric   size_t AddendWidth = strlen("addend");
1354bdd1243dSDimitry Andric   size_t DylibWidth = strlen("dylib");
1355bdd1243dSDimitry Andric   const size_t PointerWidth = 2 + O->getBytesInAddress() * 2;
1356bdd1243dSDimitry Andric 
1357bdd1243dSDimitry Andric   auto HexLength = [](uint64_t Num) {
1358bdd1243dSDimitry Andric     return Num ? (size_t)divideCeil(Log2_64(Num), 4) : 1;
1359bdd1243dSDimitry Andric   };
1360bdd1243dSDimitry Andric   for (const object::MachOChainedFixupEntry &Entry : O->fixupTable(Err)) {
1361bdd1243dSDimitry Andric     SegmentWidth = std::max(SegmentWidth, Entry.segmentName().size());
1362bdd1243dSDimitry Andric     SectionWidth = std::max(SectionWidth, Entry.sectionName().size());
1363bdd1243dSDimitry Andric     AddressWidth = std::max(AddressWidth, HexLength(Entry.address()) + 2);
1364bdd1243dSDimitry Andric     if (Entry.isBind()) {
1365bdd1243dSDimitry Andric       AddendWidth = std::max(AddendWidth, HexLength(Entry.addend()) + 2);
1366bdd1243dSDimitry Andric       DylibWidth = std::max(DylibWidth, Entry.symbolName().size());
1367bdd1243dSDimitry Andric     }
1368bdd1243dSDimitry Andric   }
1369bdd1243dSDimitry Andric   // Errors will be handled when printing the table.
1370bdd1243dSDimitry Andric   if (Err)
1371bdd1243dSDimitry Andric     consumeError(std::move(Err));
1372bdd1243dSDimitry Andric 
1373bdd1243dSDimitry Andric   outs() << "dyld information:\n";
1374bdd1243dSDimitry Andric   outs() << left_justify("segment", SegmentWidth) << ' '
1375bdd1243dSDimitry Andric          << left_justify("section", SectionWidth) << ' '
1376bdd1243dSDimitry Andric          << left_justify("address", AddressWidth) << ' '
1377bdd1243dSDimitry Andric          << left_justify("pointer", PointerWidth) << " type   "
1378bdd1243dSDimitry Andric          << left_justify("addend", AddendWidth) << ' '
1379bdd1243dSDimitry Andric          << left_justify("dylib", DylibWidth) << " symbol/vm address\n";
1380bdd1243dSDimitry Andric   for (const object::MachOChainedFixupEntry &Entry : O->fixupTable(Err)) {
1381bdd1243dSDimitry Andric     outs() << left_justify(Entry.segmentName(), SegmentWidth) << ' '
1382bdd1243dSDimitry Andric            << left_justify(Entry.sectionName(), SectionWidth) << ' ' << "0x"
1383bdd1243dSDimitry Andric            << left_justify(utohexstr(Entry.address()), AddressWidth - 2) << ' '
1384bdd1243dSDimitry Andric            << format_hex(Entry.rawValue(), PointerWidth, true) << ' ';
1385bdd1243dSDimitry Andric     if (Entry.isBind()) {
1386bdd1243dSDimitry Andric       outs() << "bind   "
1387bdd1243dSDimitry Andric              << "0x" << left_justify(utohexstr(Entry.addend()), AddendWidth - 2)
1388bdd1243dSDimitry Andric              << ' ' << left_justify(ordinalName(O, Entry.ordinal()), DylibWidth)
1389bdd1243dSDimitry Andric              << ' ' << Entry.symbolName();
1390bdd1243dSDimitry Andric       if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
1391bdd1243dSDimitry Andric         outs() << " (weak import)";
1392bdd1243dSDimitry Andric       outs() << '\n';
1393bdd1243dSDimitry Andric     } else {
1394bdd1243dSDimitry Andric       assert(Entry.isRebase());
1395bdd1243dSDimitry Andric       outs() << "rebase";
1396bdd1243dSDimitry Andric       outs().indent(AddendWidth + DylibWidth + 2);
1397bdd1243dSDimitry Andric       outs() << format("0x%" PRIX64, Entry.pointerValue()) << '\n';
1398bdd1243dSDimitry Andric     }
1399bdd1243dSDimitry Andric   }
1400bdd1243dSDimitry Andric   if (Err)
1401bdd1243dSDimitry Andric     reportError(std::move(Err), O->getFileName());
1402bdd1243dSDimitry Andric 
1403bdd1243dSDimitry Andric   // TODO: Print opcode-based fixups if the object uses those.
140481ad6265SDimitry Andric }
140581ad6265SDimitry Andric 
PrintDylibs(MachOObjectFile * O,bool JustId)14060b57cec5SDimitry Andric static void PrintDylibs(MachOObjectFile *O, bool JustId) {
14070b57cec5SDimitry Andric   unsigned Index = 0;
14080b57cec5SDimitry Andric   for (const auto &Load : O->load_commands()) {
14090b57cec5SDimitry Andric     if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
14100b57cec5SDimitry Andric         (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
14110b57cec5SDimitry Andric                      Load.C.cmd == MachO::LC_LOAD_DYLIB ||
14120b57cec5SDimitry Andric                      Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
14130b57cec5SDimitry Andric                      Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
14140b57cec5SDimitry Andric                      Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
14150b57cec5SDimitry Andric                      Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
14160b57cec5SDimitry Andric       MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
14170b57cec5SDimitry Andric       if (dl.dylib.name < dl.cmdsize) {
14180b57cec5SDimitry Andric         const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
14190b57cec5SDimitry Andric         if (JustId)
14200b57cec5SDimitry Andric           outs() << p << "\n";
14210b57cec5SDimitry Andric         else {
14220b57cec5SDimitry Andric           outs() << "\t" << p;
14230b57cec5SDimitry Andric           outs() << " (compatibility version "
14240b57cec5SDimitry Andric                  << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
14250b57cec5SDimitry Andric                  << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
14260b57cec5SDimitry Andric                  << (dl.dylib.compatibility_version & 0xff) << ",";
14270b57cec5SDimitry Andric           outs() << " current version "
14280b57cec5SDimitry Andric                  << ((dl.dylib.current_version >> 16) & 0xffff) << "."
14290b57cec5SDimitry Andric                  << ((dl.dylib.current_version >> 8) & 0xff) << "."
14300b57cec5SDimitry Andric                  << (dl.dylib.current_version & 0xff);
14310b57cec5SDimitry Andric           if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
14320b57cec5SDimitry Andric             outs() << ", weak";
14330b57cec5SDimitry Andric           if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
14340b57cec5SDimitry Andric             outs() << ", reexport";
14350b57cec5SDimitry Andric           if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
14360b57cec5SDimitry Andric             outs() << ", upward";
14370b57cec5SDimitry Andric           if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
14380b57cec5SDimitry Andric             outs() << ", lazy";
14390b57cec5SDimitry Andric           outs() << ")\n";
14400b57cec5SDimitry Andric         }
14410b57cec5SDimitry Andric       } else {
14420b57cec5SDimitry Andric         outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
14430b57cec5SDimitry Andric         if (Load.C.cmd == MachO::LC_ID_DYLIB)
14440b57cec5SDimitry Andric           outs() << "LC_ID_DYLIB ";
14450b57cec5SDimitry Andric         else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
14460b57cec5SDimitry Andric           outs() << "LC_LOAD_DYLIB ";
14470b57cec5SDimitry Andric         else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
14480b57cec5SDimitry Andric           outs() << "LC_LOAD_WEAK_DYLIB ";
14490b57cec5SDimitry Andric         else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
14500b57cec5SDimitry Andric           outs() << "LC_LAZY_LOAD_DYLIB ";
14510b57cec5SDimitry Andric         else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
14520b57cec5SDimitry Andric           outs() << "LC_REEXPORT_DYLIB ";
14530b57cec5SDimitry Andric         else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
14540b57cec5SDimitry Andric           outs() << "LC_LOAD_UPWARD_DYLIB ";
14550b57cec5SDimitry Andric         else
14560b57cec5SDimitry Andric           outs() << "LC_??? ";
14570b57cec5SDimitry Andric         outs() << "command " << Index++ << "\n";
14580b57cec5SDimitry Andric       }
14590b57cec5SDimitry Andric     }
14600b57cec5SDimitry Andric   }
14610b57cec5SDimitry Andric }
14620b57cec5SDimitry Andric 
printRpaths(MachOObjectFile * O)1463fe6060f1SDimitry Andric static void printRpaths(MachOObjectFile *O) {
1464fe6060f1SDimitry Andric   for (const auto &Command : O->load_commands()) {
1465fe6060f1SDimitry Andric     if (Command.C.cmd == MachO::LC_RPATH) {
1466fe6060f1SDimitry Andric       auto Rpath = O->getRpathCommand(Command);
1467fe6060f1SDimitry Andric       const char *P = (const char *)(Command.Ptr) + Rpath.path;
1468fe6060f1SDimitry Andric       outs() << P << "\n";
1469fe6060f1SDimitry Andric     }
1470fe6060f1SDimitry Andric   }
1471fe6060f1SDimitry Andric }
1472fe6060f1SDimitry Andric 
14730b57cec5SDimitry Andric typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
14740b57cec5SDimitry Andric 
CreateSymbolAddressMap(MachOObjectFile * O,SymbolAddressMap * AddrMap)14750b57cec5SDimitry Andric static void CreateSymbolAddressMap(MachOObjectFile *O,
14760b57cec5SDimitry Andric                                    SymbolAddressMap *AddrMap) {
14770b57cec5SDimitry Andric   // Create a map of symbol addresses to symbol names.
14780b57cec5SDimitry Andric   const StringRef FileName = O->getFileName();
14790b57cec5SDimitry Andric   for (const SymbolRef &Symbol : O->symbols()) {
14800b57cec5SDimitry Andric     SymbolRef::Type ST = unwrapOrError(Symbol.getType(), FileName);
14810b57cec5SDimitry Andric     if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
14820b57cec5SDimitry Andric         ST == SymbolRef::ST_Other) {
14835ffd83dbSDimitry Andric       uint64_t Address = cantFail(Symbol.getValue());
14840b57cec5SDimitry Andric       StringRef SymName = unwrapOrError(Symbol.getName(), FileName);
14855f757f3fSDimitry Andric       if (!SymName.starts_with(".objc"))
14860b57cec5SDimitry Andric         (*AddrMap)[Address] = SymName;
14870b57cec5SDimitry Andric     }
14880b57cec5SDimitry Andric   }
14890b57cec5SDimitry Andric }
14900b57cec5SDimitry Andric 
14910b57cec5SDimitry Andric // GuessSymbolName is passed the address of what might be a symbol and a
14920b57cec5SDimitry Andric // pointer to the SymbolAddressMap.  It returns the name of a symbol
14930b57cec5SDimitry Andric // with that address or nullptr if no symbol is found with that address.
GuessSymbolName(uint64_t value,SymbolAddressMap * AddrMap)14940b57cec5SDimitry Andric static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
14950b57cec5SDimitry Andric   const char *SymbolName = nullptr;
14960b57cec5SDimitry Andric   // A DenseMap can't lookup up some values.
14970b57cec5SDimitry Andric   if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
14980b57cec5SDimitry Andric     StringRef name = AddrMap->lookup(value);
14990b57cec5SDimitry Andric     if (!name.empty())
15000b57cec5SDimitry Andric       SymbolName = name.data();
15010b57cec5SDimitry Andric   }
15020b57cec5SDimitry Andric   return SymbolName;
15030b57cec5SDimitry Andric }
15040b57cec5SDimitry Andric 
DumpCstringChar(const char c)15050b57cec5SDimitry Andric static void DumpCstringChar(const char c) {
15060b57cec5SDimitry Andric   char p[2];
15070b57cec5SDimitry Andric   p[0] = c;
15080b57cec5SDimitry Andric   p[1] = '\0';
15090b57cec5SDimitry Andric   outs().write_escaped(p);
15100b57cec5SDimitry Andric }
15110b57cec5SDimitry Andric 
DumpCstringSection(MachOObjectFile * O,const char * sect,uint32_t sect_size,uint64_t sect_addr,bool print_addresses)15120b57cec5SDimitry Andric static void DumpCstringSection(MachOObjectFile *O, const char *sect,
15130b57cec5SDimitry Andric                                uint32_t sect_size, uint64_t sect_addr,
15140b57cec5SDimitry Andric                                bool print_addresses) {
15150b57cec5SDimitry Andric   for (uint32_t i = 0; i < sect_size; i++) {
15160b57cec5SDimitry Andric     if (print_addresses) {
15170b57cec5SDimitry Andric       if (O->is64Bit())
15180b57cec5SDimitry Andric         outs() << format("%016" PRIx64, sect_addr + i) << "  ";
15190b57cec5SDimitry Andric       else
15200b57cec5SDimitry Andric         outs() << format("%08" PRIx64, sect_addr + i) << "  ";
15210b57cec5SDimitry Andric     }
15220b57cec5SDimitry Andric     for (; i < sect_size && sect[i] != '\0'; i++)
15230b57cec5SDimitry Andric       DumpCstringChar(sect[i]);
15240b57cec5SDimitry Andric     if (i < sect_size && sect[i] == '\0')
15250b57cec5SDimitry Andric       outs() << "\n";
15260b57cec5SDimitry Andric   }
15270b57cec5SDimitry Andric }
15280b57cec5SDimitry Andric 
DumpLiteral4(uint32_t l,float f)15290b57cec5SDimitry Andric static void DumpLiteral4(uint32_t l, float f) {
15300b57cec5SDimitry Andric   outs() << format("0x%08" PRIx32, l);
15310b57cec5SDimitry Andric   if ((l & 0x7f800000) != 0x7f800000)
15320b57cec5SDimitry Andric     outs() << format(" (%.16e)\n", f);
15330b57cec5SDimitry Andric   else {
15340b57cec5SDimitry Andric     if (l == 0x7f800000)
15350b57cec5SDimitry Andric       outs() << " (+Infinity)\n";
15360b57cec5SDimitry Andric     else if (l == 0xff800000)
15370b57cec5SDimitry Andric       outs() << " (-Infinity)\n";
15380b57cec5SDimitry Andric     else if ((l & 0x00400000) == 0x00400000)
15390b57cec5SDimitry Andric       outs() << " (non-signaling Not-a-Number)\n";
15400b57cec5SDimitry Andric     else
15410b57cec5SDimitry Andric       outs() << " (signaling Not-a-Number)\n";
15420b57cec5SDimitry Andric   }
15430b57cec5SDimitry Andric }
15440b57cec5SDimitry Andric 
DumpLiteral4Section(MachOObjectFile * O,const char * sect,uint32_t sect_size,uint64_t sect_addr,bool print_addresses)15450b57cec5SDimitry Andric static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
15460b57cec5SDimitry Andric                                 uint32_t sect_size, uint64_t sect_addr,
15470b57cec5SDimitry Andric                                 bool print_addresses) {
15480b57cec5SDimitry Andric   for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
15490b57cec5SDimitry Andric     if (print_addresses) {
15500b57cec5SDimitry Andric       if (O->is64Bit())
15510b57cec5SDimitry Andric         outs() << format("%016" PRIx64, sect_addr + i) << "  ";
15520b57cec5SDimitry Andric       else
15530b57cec5SDimitry Andric         outs() << format("%08" PRIx64, sect_addr + i) << "  ";
15540b57cec5SDimitry Andric     }
15550b57cec5SDimitry Andric     float f;
15560b57cec5SDimitry Andric     memcpy(&f, sect + i, sizeof(float));
15570b57cec5SDimitry Andric     if (O->isLittleEndian() != sys::IsLittleEndianHost)
15580b57cec5SDimitry Andric       sys::swapByteOrder(f);
15590b57cec5SDimitry Andric     uint32_t l;
15600b57cec5SDimitry Andric     memcpy(&l, sect + i, sizeof(uint32_t));
15610b57cec5SDimitry Andric     if (O->isLittleEndian() != sys::IsLittleEndianHost)
15620b57cec5SDimitry Andric       sys::swapByteOrder(l);
15630b57cec5SDimitry Andric     DumpLiteral4(l, f);
15640b57cec5SDimitry Andric   }
15650b57cec5SDimitry Andric }
15660b57cec5SDimitry Andric 
DumpLiteral8(MachOObjectFile * O,uint32_t l0,uint32_t l1,double d)15670b57cec5SDimitry Andric static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
15680b57cec5SDimitry Andric                          double d) {
15690b57cec5SDimitry Andric   outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
15700b57cec5SDimitry Andric   uint32_t Hi, Lo;
15710b57cec5SDimitry Andric   Hi = (O->isLittleEndian()) ? l1 : l0;
15720b57cec5SDimitry Andric   Lo = (O->isLittleEndian()) ? l0 : l1;
15730b57cec5SDimitry Andric 
15740b57cec5SDimitry Andric   // Hi is the high word, so this is equivalent to if(isfinite(d))
15750b57cec5SDimitry Andric   if ((Hi & 0x7ff00000) != 0x7ff00000)
15760b57cec5SDimitry Andric     outs() << format(" (%.16e)\n", d);
15770b57cec5SDimitry Andric   else {
15780b57cec5SDimitry Andric     if (Hi == 0x7ff00000 && Lo == 0)
15790b57cec5SDimitry Andric       outs() << " (+Infinity)\n";
15800b57cec5SDimitry Andric     else if (Hi == 0xfff00000 && Lo == 0)
15810b57cec5SDimitry Andric       outs() << " (-Infinity)\n";
15820b57cec5SDimitry Andric     else if ((Hi & 0x00080000) == 0x00080000)
15830b57cec5SDimitry Andric       outs() << " (non-signaling Not-a-Number)\n";
15840b57cec5SDimitry Andric     else
15850b57cec5SDimitry Andric       outs() << " (signaling Not-a-Number)\n";
15860b57cec5SDimitry Andric   }
15870b57cec5SDimitry Andric }
15880b57cec5SDimitry Andric 
DumpLiteral8Section(MachOObjectFile * O,const char * sect,uint32_t sect_size,uint64_t sect_addr,bool print_addresses)15890b57cec5SDimitry Andric static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
15900b57cec5SDimitry Andric                                 uint32_t sect_size, uint64_t sect_addr,
15910b57cec5SDimitry Andric                                 bool print_addresses) {
15920b57cec5SDimitry Andric   for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
15930b57cec5SDimitry Andric     if (print_addresses) {
15940b57cec5SDimitry Andric       if (O->is64Bit())
15950b57cec5SDimitry Andric         outs() << format("%016" PRIx64, sect_addr + i) << "  ";
15960b57cec5SDimitry Andric       else
15970b57cec5SDimitry Andric         outs() << format("%08" PRIx64, sect_addr + i) << "  ";
15980b57cec5SDimitry Andric     }
15990b57cec5SDimitry Andric     double d;
16000b57cec5SDimitry Andric     memcpy(&d, sect + i, sizeof(double));
16010b57cec5SDimitry Andric     if (O->isLittleEndian() != sys::IsLittleEndianHost)
16020b57cec5SDimitry Andric       sys::swapByteOrder(d);
16030b57cec5SDimitry Andric     uint32_t l0, l1;
16040b57cec5SDimitry Andric     memcpy(&l0, sect + i, sizeof(uint32_t));
16050b57cec5SDimitry Andric     memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
16060b57cec5SDimitry Andric     if (O->isLittleEndian() != sys::IsLittleEndianHost) {
16070b57cec5SDimitry Andric       sys::swapByteOrder(l0);
16080b57cec5SDimitry Andric       sys::swapByteOrder(l1);
16090b57cec5SDimitry Andric     }
16100b57cec5SDimitry Andric     DumpLiteral8(O, l0, l1, d);
16110b57cec5SDimitry Andric   }
16120b57cec5SDimitry Andric }
16130b57cec5SDimitry Andric 
DumpLiteral16(uint32_t l0,uint32_t l1,uint32_t l2,uint32_t l3)16140b57cec5SDimitry Andric static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
16150b57cec5SDimitry Andric   outs() << format("0x%08" PRIx32, l0) << " ";
16160b57cec5SDimitry Andric   outs() << format("0x%08" PRIx32, l1) << " ";
16170b57cec5SDimitry Andric   outs() << format("0x%08" PRIx32, l2) << " ";
16180b57cec5SDimitry Andric   outs() << format("0x%08" PRIx32, l3) << "\n";
16190b57cec5SDimitry Andric }
16200b57cec5SDimitry Andric 
DumpLiteral16Section(MachOObjectFile * O,const char * sect,uint32_t sect_size,uint64_t sect_addr,bool print_addresses)16210b57cec5SDimitry Andric static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
16220b57cec5SDimitry Andric                                  uint32_t sect_size, uint64_t sect_addr,
16230b57cec5SDimitry Andric                                  bool print_addresses) {
16240b57cec5SDimitry Andric   for (uint32_t i = 0; i < sect_size; i += 16) {
16250b57cec5SDimitry Andric     if (print_addresses) {
16260b57cec5SDimitry Andric       if (O->is64Bit())
16270b57cec5SDimitry Andric         outs() << format("%016" PRIx64, sect_addr + i) << "  ";
16280b57cec5SDimitry Andric       else
16290b57cec5SDimitry Andric         outs() << format("%08" PRIx64, sect_addr + i) << "  ";
16300b57cec5SDimitry Andric     }
16310b57cec5SDimitry Andric     uint32_t l0, l1, l2, l3;
16320b57cec5SDimitry Andric     memcpy(&l0, sect + i, sizeof(uint32_t));
16330b57cec5SDimitry Andric     memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
16340b57cec5SDimitry Andric     memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
16350b57cec5SDimitry Andric     memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
16360b57cec5SDimitry Andric     if (O->isLittleEndian() != sys::IsLittleEndianHost) {
16370b57cec5SDimitry Andric       sys::swapByteOrder(l0);
16380b57cec5SDimitry Andric       sys::swapByteOrder(l1);
16390b57cec5SDimitry Andric       sys::swapByteOrder(l2);
16400b57cec5SDimitry Andric       sys::swapByteOrder(l3);
16410b57cec5SDimitry Andric     }
16420b57cec5SDimitry Andric     DumpLiteral16(l0, l1, l2, l3);
16430b57cec5SDimitry Andric   }
16440b57cec5SDimitry Andric }
16450b57cec5SDimitry Andric 
DumpLiteralPointerSection(MachOObjectFile * O,const SectionRef & Section,const char * sect,uint32_t sect_size,uint64_t sect_addr,bool print_addresses)16460b57cec5SDimitry Andric static void DumpLiteralPointerSection(MachOObjectFile *O,
16470b57cec5SDimitry Andric                                       const SectionRef &Section,
16480b57cec5SDimitry Andric                                       const char *sect, uint32_t sect_size,
16490b57cec5SDimitry Andric                                       uint64_t sect_addr,
16500b57cec5SDimitry Andric                                       bool print_addresses) {
16510b57cec5SDimitry Andric   // Collect the literal sections in this Mach-O file.
16520b57cec5SDimitry Andric   std::vector<SectionRef> LiteralSections;
16530b57cec5SDimitry Andric   for (const SectionRef &Section : O->sections()) {
16540b57cec5SDimitry Andric     DataRefImpl Ref = Section.getRawDataRefImpl();
16550b57cec5SDimitry Andric     uint32_t section_type;
16560b57cec5SDimitry Andric     if (O->is64Bit()) {
16570b57cec5SDimitry Andric       const MachO::section_64 Sec = O->getSection64(Ref);
16580b57cec5SDimitry Andric       section_type = Sec.flags & MachO::SECTION_TYPE;
16590b57cec5SDimitry Andric     } else {
16600b57cec5SDimitry Andric       const MachO::section Sec = O->getSection(Ref);
16610b57cec5SDimitry Andric       section_type = Sec.flags & MachO::SECTION_TYPE;
16620b57cec5SDimitry Andric     }
16630b57cec5SDimitry Andric     if (section_type == MachO::S_CSTRING_LITERALS ||
16640b57cec5SDimitry Andric         section_type == MachO::S_4BYTE_LITERALS ||
16650b57cec5SDimitry Andric         section_type == MachO::S_8BYTE_LITERALS ||
16660b57cec5SDimitry Andric         section_type == MachO::S_16BYTE_LITERALS)
16670b57cec5SDimitry Andric       LiteralSections.push_back(Section);
16680b57cec5SDimitry Andric   }
16690b57cec5SDimitry Andric 
16700b57cec5SDimitry Andric   // Set the size of the literal pointer.
16710b57cec5SDimitry Andric   uint32_t lp_size = O->is64Bit() ? 8 : 4;
16720b57cec5SDimitry Andric 
16730b57cec5SDimitry Andric   // Collect the external relocation symbols for the literal pointers.
16740b57cec5SDimitry Andric   std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
16750b57cec5SDimitry Andric   for (const RelocationRef &Reloc : Section.relocations()) {
16760b57cec5SDimitry Andric     DataRefImpl Rel;
16770b57cec5SDimitry Andric     MachO::any_relocation_info RE;
16780b57cec5SDimitry Andric     bool isExtern = false;
16790b57cec5SDimitry Andric     Rel = Reloc.getRawDataRefImpl();
16800b57cec5SDimitry Andric     RE = O->getRelocation(Rel);
16810b57cec5SDimitry Andric     isExtern = O->getPlainRelocationExternal(RE);
16820b57cec5SDimitry Andric     if (isExtern) {
16830b57cec5SDimitry Andric       uint64_t RelocOffset = Reloc.getOffset();
16840b57cec5SDimitry Andric       symbol_iterator RelocSym = Reloc.getSymbol();
16850b57cec5SDimitry Andric       Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
16860b57cec5SDimitry Andric     }
16870b57cec5SDimitry Andric   }
16880b57cec5SDimitry Andric   array_pod_sort(Relocs.begin(), Relocs.end());
16890b57cec5SDimitry Andric 
16900b57cec5SDimitry Andric   // Dump each literal pointer.
16910b57cec5SDimitry Andric   for (uint32_t i = 0; i < sect_size; i += lp_size) {
16920b57cec5SDimitry Andric     if (print_addresses) {
16930b57cec5SDimitry Andric       if (O->is64Bit())
16940b57cec5SDimitry Andric         outs() << format("%016" PRIx64, sect_addr + i) << "  ";
16950b57cec5SDimitry Andric       else
16960b57cec5SDimitry Andric         outs() << format("%08" PRIx64, sect_addr + i) << "  ";
16970b57cec5SDimitry Andric     }
16980b57cec5SDimitry Andric     uint64_t lp;
16990b57cec5SDimitry Andric     if (O->is64Bit()) {
17000b57cec5SDimitry Andric       memcpy(&lp, sect + i, sizeof(uint64_t));
17010b57cec5SDimitry Andric       if (O->isLittleEndian() != sys::IsLittleEndianHost)
17020b57cec5SDimitry Andric         sys::swapByteOrder(lp);
17030b57cec5SDimitry Andric     } else {
17040b57cec5SDimitry Andric       uint32_t li;
17050b57cec5SDimitry Andric       memcpy(&li, sect + i, sizeof(uint32_t));
17060b57cec5SDimitry Andric       if (O->isLittleEndian() != sys::IsLittleEndianHost)
17070b57cec5SDimitry Andric         sys::swapByteOrder(li);
17080b57cec5SDimitry Andric       lp = li;
17090b57cec5SDimitry Andric     }
17100b57cec5SDimitry Andric 
17110b57cec5SDimitry Andric     // First look for an external relocation entry for this literal pointer.
17120b57cec5SDimitry Andric     auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) {
17130b57cec5SDimitry Andric       return P.first == i;
17140b57cec5SDimitry Andric     });
17150b57cec5SDimitry Andric     if (Reloc != Relocs.end()) {
17160b57cec5SDimitry Andric       symbol_iterator RelocSym = Reloc->second;
17170b57cec5SDimitry Andric       StringRef SymName = unwrapOrError(RelocSym->getName(), O->getFileName());
17180b57cec5SDimitry Andric       outs() << "external relocation entry for symbol:" << SymName << "\n";
17190b57cec5SDimitry Andric       continue;
17200b57cec5SDimitry Andric     }
17210b57cec5SDimitry Andric 
17220b57cec5SDimitry Andric     // For local references see what the section the literal pointer points to.
17230b57cec5SDimitry Andric     auto Sect = find_if(LiteralSections, [&](const SectionRef &R) {
17240b57cec5SDimitry Andric       return lp >= R.getAddress() && lp < R.getAddress() + R.getSize();
17250b57cec5SDimitry Andric     });
17260b57cec5SDimitry Andric     if (Sect == LiteralSections.end()) {
17270b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
17280b57cec5SDimitry Andric       continue;
17290b57cec5SDimitry Andric     }
17300b57cec5SDimitry Andric 
17310b57cec5SDimitry Andric     uint64_t SectAddress = Sect->getAddress();
17320b57cec5SDimitry Andric     uint64_t SectSize = Sect->getSize();
17330b57cec5SDimitry Andric 
17340b57cec5SDimitry Andric     StringRef SectName;
17358bcb0991SDimitry Andric     Expected<StringRef> SectNameOrErr = Sect->getName();
17368bcb0991SDimitry Andric     if (SectNameOrErr)
17378bcb0991SDimitry Andric       SectName = *SectNameOrErr;
17388bcb0991SDimitry Andric     else
17398bcb0991SDimitry Andric       consumeError(SectNameOrErr.takeError());
17408bcb0991SDimitry Andric 
17410b57cec5SDimitry Andric     DataRefImpl Ref = Sect->getRawDataRefImpl();
17420b57cec5SDimitry Andric     StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
17430b57cec5SDimitry Andric     outs() << SegmentName << ":" << SectName << ":";
17440b57cec5SDimitry Andric 
17450b57cec5SDimitry Andric     uint32_t section_type;
17460b57cec5SDimitry Andric     if (O->is64Bit()) {
17470b57cec5SDimitry Andric       const MachO::section_64 Sec = O->getSection64(Ref);
17480b57cec5SDimitry Andric       section_type = Sec.flags & MachO::SECTION_TYPE;
17490b57cec5SDimitry Andric     } else {
17500b57cec5SDimitry Andric       const MachO::section Sec = O->getSection(Ref);
17510b57cec5SDimitry Andric       section_type = Sec.flags & MachO::SECTION_TYPE;
17520b57cec5SDimitry Andric     }
17530b57cec5SDimitry Andric 
17540b57cec5SDimitry Andric     StringRef BytesStr = unwrapOrError(Sect->getContents(), O->getFileName());
17550b57cec5SDimitry Andric 
17560b57cec5SDimitry Andric     const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
17570b57cec5SDimitry Andric 
17580b57cec5SDimitry Andric     switch (section_type) {
17590b57cec5SDimitry Andric     case MachO::S_CSTRING_LITERALS:
17600b57cec5SDimitry Andric       for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
17610b57cec5SDimitry Andric            i++) {
17620b57cec5SDimitry Andric         DumpCstringChar(Contents[i]);
17630b57cec5SDimitry Andric       }
17640b57cec5SDimitry Andric       outs() << "\n";
17650b57cec5SDimitry Andric       break;
17660b57cec5SDimitry Andric     case MachO::S_4BYTE_LITERALS:
17670b57cec5SDimitry Andric       float f;
17680b57cec5SDimitry Andric       memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
17690b57cec5SDimitry Andric       uint32_t l;
17700b57cec5SDimitry Andric       memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
17710b57cec5SDimitry Andric       if (O->isLittleEndian() != sys::IsLittleEndianHost) {
17720b57cec5SDimitry Andric         sys::swapByteOrder(f);
17730b57cec5SDimitry Andric         sys::swapByteOrder(l);
17740b57cec5SDimitry Andric       }
17750b57cec5SDimitry Andric       DumpLiteral4(l, f);
17760b57cec5SDimitry Andric       break;
17770b57cec5SDimitry Andric     case MachO::S_8BYTE_LITERALS: {
17780b57cec5SDimitry Andric       double d;
17790b57cec5SDimitry Andric       memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
17800b57cec5SDimitry Andric       uint32_t l0, l1;
17810b57cec5SDimitry Andric       memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
17820b57cec5SDimitry Andric       memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
17830b57cec5SDimitry Andric              sizeof(uint32_t));
17840b57cec5SDimitry Andric       if (O->isLittleEndian() != sys::IsLittleEndianHost) {
17850b57cec5SDimitry Andric         sys::swapByteOrder(f);
17860b57cec5SDimitry Andric         sys::swapByteOrder(l0);
17870b57cec5SDimitry Andric         sys::swapByteOrder(l1);
17880b57cec5SDimitry Andric       }
17890b57cec5SDimitry Andric       DumpLiteral8(O, l0, l1, d);
17900b57cec5SDimitry Andric       break;
17910b57cec5SDimitry Andric     }
17920b57cec5SDimitry Andric     case MachO::S_16BYTE_LITERALS: {
17930b57cec5SDimitry Andric       uint32_t l0, l1, l2, l3;
17940b57cec5SDimitry Andric       memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
17950b57cec5SDimitry Andric       memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
17960b57cec5SDimitry Andric              sizeof(uint32_t));
17970b57cec5SDimitry Andric       memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
17980b57cec5SDimitry Andric              sizeof(uint32_t));
17990b57cec5SDimitry Andric       memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
18000b57cec5SDimitry Andric              sizeof(uint32_t));
18010b57cec5SDimitry Andric       if (O->isLittleEndian() != sys::IsLittleEndianHost) {
18020b57cec5SDimitry Andric         sys::swapByteOrder(l0);
18030b57cec5SDimitry Andric         sys::swapByteOrder(l1);
18040b57cec5SDimitry Andric         sys::swapByteOrder(l2);
18050b57cec5SDimitry Andric         sys::swapByteOrder(l3);
18060b57cec5SDimitry Andric       }
18070b57cec5SDimitry Andric       DumpLiteral16(l0, l1, l2, l3);
18080b57cec5SDimitry Andric       break;
18090b57cec5SDimitry Andric     }
18100b57cec5SDimitry Andric     }
18110b57cec5SDimitry Andric   }
18120b57cec5SDimitry Andric }
18130b57cec5SDimitry Andric 
DumpInitTermPointerSection(MachOObjectFile * O,const SectionRef & Section,const char * sect,uint32_t sect_size,uint64_t sect_addr,SymbolAddressMap * AddrMap,bool verbose)18140b57cec5SDimitry Andric static void DumpInitTermPointerSection(MachOObjectFile *O,
18150b57cec5SDimitry Andric                                        const SectionRef &Section,
18160b57cec5SDimitry Andric                                        const char *sect,
18170b57cec5SDimitry Andric                                        uint32_t sect_size, uint64_t sect_addr,
18180b57cec5SDimitry Andric                                        SymbolAddressMap *AddrMap,
18190b57cec5SDimitry Andric                                        bool verbose) {
18200b57cec5SDimitry Andric   uint32_t stride;
18210b57cec5SDimitry Andric   stride = (O->is64Bit()) ? sizeof(uint64_t) : sizeof(uint32_t);
18220b57cec5SDimitry Andric 
18230b57cec5SDimitry Andric   // Collect the external relocation symbols for the pointers.
18240b57cec5SDimitry Andric   std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
18250b57cec5SDimitry Andric   for (const RelocationRef &Reloc : Section.relocations()) {
18260b57cec5SDimitry Andric     DataRefImpl Rel;
18270b57cec5SDimitry Andric     MachO::any_relocation_info RE;
18280b57cec5SDimitry Andric     bool isExtern = false;
18290b57cec5SDimitry Andric     Rel = Reloc.getRawDataRefImpl();
18300b57cec5SDimitry Andric     RE = O->getRelocation(Rel);
18310b57cec5SDimitry Andric     isExtern = O->getPlainRelocationExternal(RE);
18320b57cec5SDimitry Andric     if (isExtern) {
18330b57cec5SDimitry Andric       uint64_t RelocOffset = Reloc.getOffset();
18340b57cec5SDimitry Andric       symbol_iterator RelocSym = Reloc.getSymbol();
18350b57cec5SDimitry Andric       Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
18360b57cec5SDimitry Andric     }
18370b57cec5SDimitry Andric   }
18380b57cec5SDimitry Andric   array_pod_sort(Relocs.begin(), Relocs.end());
18390b57cec5SDimitry Andric 
18400b57cec5SDimitry Andric   for (uint32_t i = 0; i < sect_size; i += stride) {
18410b57cec5SDimitry Andric     const char *SymbolName = nullptr;
18420b57cec5SDimitry Andric     uint64_t p;
18430b57cec5SDimitry Andric     if (O->is64Bit()) {
18440b57cec5SDimitry Andric       outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
18450b57cec5SDimitry Andric       uint64_t pointer_value;
18460b57cec5SDimitry Andric       memcpy(&pointer_value, sect + i, stride);
18470b57cec5SDimitry Andric       if (O->isLittleEndian() != sys::IsLittleEndianHost)
18480b57cec5SDimitry Andric         sys::swapByteOrder(pointer_value);
18490b57cec5SDimitry Andric       outs() << format("0x%016" PRIx64, pointer_value);
18500b57cec5SDimitry Andric       p = pointer_value;
18510b57cec5SDimitry Andric     } else {
18520b57cec5SDimitry Andric       outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
18530b57cec5SDimitry Andric       uint32_t pointer_value;
18540b57cec5SDimitry Andric       memcpy(&pointer_value, sect + i, stride);
18550b57cec5SDimitry Andric       if (O->isLittleEndian() != sys::IsLittleEndianHost)
18560b57cec5SDimitry Andric         sys::swapByteOrder(pointer_value);
18570b57cec5SDimitry Andric       outs() << format("0x%08" PRIx32, pointer_value);
18580b57cec5SDimitry Andric       p = pointer_value;
18590b57cec5SDimitry Andric     }
18600b57cec5SDimitry Andric     if (verbose) {
18610b57cec5SDimitry Andric       // First look for an external relocation entry for this pointer.
18620b57cec5SDimitry Andric       auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) {
18630b57cec5SDimitry Andric         return P.first == i;
18640b57cec5SDimitry Andric       });
18650b57cec5SDimitry Andric       if (Reloc != Relocs.end()) {
18660b57cec5SDimitry Andric         symbol_iterator RelocSym = Reloc->second;
18670b57cec5SDimitry Andric         outs() << " " << unwrapOrError(RelocSym->getName(), O->getFileName());
18680b57cec5SDimitry Andric       } else {
18690b57cec5SDimitry Andric         SymbolName = GuessSymbolName(p, AddrMap);
18700b57cec5SDimitry Andric         if (SymbolName)
18710b57cec5SDimitry Andric           outs() << " " << SymbolName;
18720b57cec5SDimitry Andric       }
18730b57cec5SDimitry Andric     }
18740b57cec5SDimitry Andric     outs() << "\n";
18750b57cec5SDimitry Andric   }
18760b57cec5SDimitry Andric }
18770b57cec5SDimitry Andric 
DumpRawSectionContents(MachOObjectFile * O,const char * sect,uint32_t size,uint64_t addr)18780b57cec5SDimitry Andric static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
18790b57cec5SDimitry Andric                                    uint32_t size, uint64_t addr) {
18800b57cec5SDimitry Andric   uint32_t cputype = O->getHeader().cputype;
18810b57cec5SDimitry Andric   if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
18820b57cec5SDimitry Andric     uint32_t j;
18830b57cec5SDimitry Andric     for (uint32_t i = 0; i < size; i += j, addr += j) {
18840b57cec5SDimitry Andric       if (O->is64Bit())
18850b57cec5SDimitry Andric         outs() << format("%016" PRIx64, addr) << "\t";
18860b57cec5SDimitry Andric       else
18870b57cec5SDimitry Andric         outs() << format("%08" PRIx64, addr) << "\t";
18880b57cec5SDimitry Andric       for (j = 0; j < 16 && i + j < size; j++) {
18890b57cec5SDimitry Andric         uint8_t byte_word = *(sect + i + j);
18900b57cec5SDimitry Andric         outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
18910b57cec5SDimitry Andric       }
18920b57cec5SDimitry Andric       outs() << "\n";
18930b57cec5SDimitry Andric     }
18940b57cec5SDimitry Andric   } else {
18950b57cec5SDimitry Andric     uint32_t j;
18960b57cec5SDimitry Andric     for (uint32_t i = 0; i < size; i += j, addr += j) {
18970b57cec5SDimitry Andric       if (O->is64Bit())
18980b57cec5SDimitry Andric         outs() << format("%016" PRIx64, addr) << "\t";
18990b57cec5SDimitry Andric       else
19000b57cec5SDimitry Andric         outs() << format("%08" PRIx64, addr) << "\t";
19010b57cec5SDimitry Andric       for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
19020b57cec5SDimitry Andric            j += sizeof(int32_t)) {
19030b57cec5SDimitry Andric         if (i + j + sizeof(int32_t) <= size) {
19040b57cec5SDimitry Andric           uint32_t long_word;
19050b57cec5SDimitry Andric           memcpy(&long_word, sect + i + j, sizeof(int32_t));
19060b57cec5SDimitry Andric           if (O->isLittleEndian() != sys::IsLittleEndianHost)
19070b57cec5SDimitry Andric             sys::swapByteOrder(long_word);
19080b57cec5SDimitry Andric           outs() << format("%08" PRIx32, long_word) << " ";
19090b57cec5SDimitry Andric         } else {
19100b57cec5SDimitry Andric           for (uint32_t k = 0; i + j + k < size; k++) {
19110b57cec5SDimitry Andric             uint8_t byte_word = *(sect + i + j + k);
19120b57cec5SDimitry Andric             outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
19130b57cec5SDimitry Andric           }
19140b57cec5SDimitry Andric         }
19150b57cec5SDimitry Andric       }
19160b57cec5SDimitry Andric       outs() << "\n";
19170b57cec5SDimitry Andric     }
19180b57cec5SDimitry Andric   }
19190b57cec5SDimitry Andric }
19200b57cec5SDimitry Andric 
19210b57cec5SDimitry Andric static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
19220b57cec5SDimitry Andric                              StringRef DisSegName, StringRef DisSectName);
19230b57cec5SDimitry Andric static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
19240b57cec5SDimitry Andric                                 uint32_t size, uint32_t addr);
DumpSectionContents(StringRef Filename,MachOObjectFile * O,bool verbose)19250b57cec5SDimitry Andric static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
19260b57cec5SDimitry Andric                                 bool verbose) {
19270b57cec5SDimitry Andric   SymbolAddressMap AddrMap;
19280b57cec5SDimitry Andric   if (verbose)
19290b57cec5SDimitry Andric     CreateSymbolAddressMap(O, &AddrMap);
19300b57cec5SDimitry Andric 
19310b57cec5SDimitry Andric   for (unsigned i = 0; i < FilterSections.size(); ++i) {
19320b57cec5SDimitry Andric     StringRef DumpSection = FilterSections[i];
19330b57cec5SDimitry Andric     std::pair<StringRef, StringRef> DumpSegSectName;
19340b57cec5SDimitry Andric     DumpSegSectName = DumpSection.split(',');
19350b57cec5SDimitry Andric     StringRef DumpSegName, DumpSectName;
19360b57cec5SDimitry Andric     if (!DumpSegSectName.second.empty()) {
19370b57cec5SDimitry Andric       DumpSegName = DumpSegSectName.first;
19380b57cec5SDimitry Andric       DumpSectName = DumpSegSectName.second;
19390b57cec5SDimitry Andric     } else {
19400b57cec5SDimitry Andric       DumpSegName = "";
19410b57cec5SDimitry Andric       DumpSectName = DumpSegSectName.first;
19420b57cec5SDimitry Andric     }
19430b57cec5SDimitry Andric     for (const SectionRef &Section : O->sections()) {
19440b57cec5SDimitry Andric       StringRef SectName;
19458bcb0991SDimitry Andric       Expected<StringRef> SecNameOrErr = Section.getName();
19468bcb0991SDimitry Andric       if (SecNameOrErr)
19478bcb0991SDimitry Andric         SectName = *SecNameOrErr;
19488bcb0991SDimitry Andric       else
19498bcb0991SDimitry Andric         consumeError(SecNameOrErr.takeError());
19508bcb0991SDimitry Andric 
19515ffd83dbSDimitry Andric       if (!DumpSection.empty())
19525ffd83dbSDimitry Andric         FoundSectionSet.insert(DumpSection);
19535ffd83dbSDimitry Andric 
19540b57cec5SDimitry Andric       DataRefImpl Ref = Section.getRawDataRefImpl();
19550b57cec5SDimitry Andric       StringRef SegName = O->getSectionFinalSegmentName(Ref);
19560b57cec5SDimitry Andric       if ((DumpSegName.empty() || SegName == DumpSegName) &&
19570b57cec5SDimitry Andric           (SectName == DumpSectName)) {
19580b57cec5SDimitry Andric 
19590b57cec5SDimitry Andric         uint32_t section_flags;
19600b57cec5SDimitry Andric         if (O->is64Bit()) {
19610b57cec5SDimitry Andric           const MachO::section_64 Sec = O->getSection64(Ref);
19620b57cec5SDimitry Andric           section_flags = Sec.flags;
19630b57cec5SDimitry Andric 
19640b57cec5SDimitry Andric         } else {
19650b57cec5SDimitry Andric           const MachO::section Sec = O->getSection(Ref);
19660b57cec5SDimitry Andric           section_flags = Sec.flags;
19670b57cec5SDimitry Andric         }
19680b57cec5SDimitry Andric         uint32_t section_type = section_flags & MachO::SECTION_TYPE;
19690b57cec5SDimitry Andric 
19700b57cec5SDimitry Andric         StringRef BytesStr =
19710b57cec5SDimitry Andric             unwrapOrError(Section.getContents(), O->getFileName());
19720b57cec5SDimitry Andric         const char *sect = reinterpret_cast<const char *>(BytesStr.data());
19730b57cec5SDimitry Andric         uint32_t sect_size = BytesStr.size();
19740b57cec5SDimitry Andric         uint64_t sect_addr = Section.getAddress();
19750b57cec5SDimitry Andric 
1976fe6060f1SDimitry Andric         if (LeadingHeaders)
19770b57cec5SDimitry Andric           outs() << "Contents of (" << SegName << "," << SectName
19780b57cec5SDimitry Andric                  << ") section\n";
19790b57cec5SDimitry Andric 
19800b57cec5SDimitry Andric         if (verbose) {
19810b57cec5SDimitry Andric           if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
19820b57cec5SDimitry Andric               (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
19830b57cec5SDimitry Andric             DisassembleMachO(Filename, O, SegName, SectName);
19840b57cec5SDimitry Andric             continue;
19850b57cec5SDimitry Andric           }
19860b57cec5SDimitry Andric           if (SegName == "__TEXT" && SectName == "__info_plist") {
19870b57cec5SDimitry Andric             outs() << sect;
19880b57cec5SDimitry Andric             continue;
19890b57cec5SDimitry Andric           }
19900b57cec5SDimitry Andric           if (SegName == "__OBJC" && SectName == "__protocol") {
19910b57cec5SDimitry Andric             DumpProtocolSection(O, sect, sect_size, sect_addr);
19920b57cec5SDimitry Andric             continue;
19930b57cec5SDimitry Andric           }
19940b57cec5SDimitry Andric           switch (section_type) {
19950b57cec5SDimitry Andric           case MachO::S_REGULAR:
19960b57cec5SDimitry Andric             DumpRawSectionContents(O, sect, sect_size, sect_addr);
19970b57cec5SDimitry Andric             break;
19980b57cec5SDimitry Andric           case MachO::S_ZEROFILL:
19990b57cec5SDimitry Andric             outs() << "zerofill section and has no contents in the file\n";
20000b57cec5SDimitry Andric             break;
20010b57cec5SDimitry Andric           case MachO::S_CSTRING_LITERALS:
2002fe6060f1SDimitry Andric             DumpCstringSection(O, sect, sect_size, sect_addr, LeadingAddr);
20030b57cec5SDimitry Andric             break;
20040b57cec5SDimitry Andric           case MachO::S_4BYTE_LITERALS:
2005fe6060f1SDimitry Andric             DumpLiteral4Section(O, sect, sect_size, sect_addr, LeadingAddr);
20060b57cec5SDimitry Andric             break;
20070b57cec5SDimitry Andric           case MachO::S_8BYTE_LITERALS:
2008fe6060f1SDimitry Andric             DumpLiteral8Section(O, sect, sect_size, sect_addr, LeadingAddr);
20090b57cec5SDimitry Andric             break;
20100b57cec5SDimitry Andric           case MachO::S_16BYTE_LITERALS:
2011fe6060f1SDimitry Andric             DumpLiteral16Section(O, sect, sect_size, sect_addr, LeadingAddr);
20120b57cec5SDimitry Andric             break;
20130b57cec5SDimitry Andric           case MachO::S_LITERAL_POINTERS:
20140b57cec5SDimitry Andric             DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
2015fe6060f1SDimitry Andric                                       LeadingAddr);
20160b57cec5SDimitry Andric             break;
20170b57cec5SDimitry Andric           case MachO::S_MOD_INIT_FUNC_POINTERS:
20180b57cec5SDimitry Andric           case MachO::S_MOD_TERM_FUNC_POINTERS:
20190b57cec5SDimitry Andric             DumpInitTermPointerSection(O, Section, sect, sect_size, sect_addr,
20200b57cec5SDimitry Andric                                        &AddrMap, verbose);
20210b57cec5SDimitry Andric             break;
20220b57cec5SDimitry Andric           default:
20230b57cec5SDimitry Andric             outs() << "Unknown section type ("
20240b57cec5SDimitry Andric                    << format("0x%08" PRIx32, section_type) << ")\n";
20250b57cec5SDimitry Andric             DumpRawSectionContents(O, sect, sect_size, sect_addr);
20260b57cec5SDimitry Andric             break;
20270b57cec5SDimitry Andric           }
20280b57cec5SDimitry Andric         } else {
20290b57cec5SDimitry Andric           if (section_type == MachO::S_ZEROFILL)
20300b57cec5SDimitry Andric             outs() << "zerofill section and has no contents in the file\n";
20310b57cec5SDimitry Andric           else
20320b57cec5SDimitry Andric             DumpRawSectionContents(O, sect, sect_size, sect_addr);
20330b57cec5SDimitry Andric         }
20340b57cec5SDimitry Andric       }
20350b57cec5SDimitry Andric     }
20360b57cec5SDimitry Andric   }
20370b57cec5SDimitry Andric }
20380b57cec5SDimitry Andric 
DumpInfoPlistSectionContents(StringRef Filename,MachOObjectFile * O)20390b57cec5SDimitry Andric static void DumpInfoPlistSectionContents(StringRef Filename,
20400b57cec5SDimitry Andric                                          MachOObjectFile *O) {
20410b57cec5SDimitry Andric   for (const SectionRef &Section : O->sections()) {
20420b57cec5SDimitry Andric     StringRef SectName;
20438bcb0991SDimitry Andric     Expected<StringRef> SecNameOrErr = Section.getName();
20448bcb0991SDimitry Andric     if (SecNameOrErr)
20458bcb0991SDimitry Andric       SectName = *SecNameOrErr;
20468bcb0991SDimitry Andric     else
20478bcb0991SDimitry Andric       consumeError(SecNameOrErr.takeError());
20488bcb0991SDimitry Andric 
20490b57cec5SDimitry Andric     DataRefImpl Ref = Section.getRawDataRefImpl();
20500b57cec5SDimitry Andric     StringRef SegName = O->getSectionFinalSegmentName(Ref);
20510b57cec5SDimitry Andric     if (SegName == "__TEXT" && SectName == "__info_plist") {
2052fe6060f1SDimitry Andric       if (LeadingHeaders)
20530b57cec5SDimitry Andric         outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
20540b57cec5SDimitry Andric       StringRef BytesStr =
20550b57cec5SDimitry Andric           unwrapOrError(Section.getContents(), O->getFileName());
20560b57cec5SDimitry Andric       const char *sect = reinterpret_cast<const char *>(BytesStr.data());
20570b57cec5SDimitry Andric       outs() << format("%.*s", BytesStr.size(), sect) << "\n";
20580b57cec5SDimitry Andric       return;
20590b57cec5SDimitry Andric     }
20600b57cec5SDimitry Andric   }
20610b57cec5SDimitry Andric }
20620b57cec5SDimitry Andric 
20630b57cec5SDimitry Andric // checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
20640b57cec5SDimitry Andric // and if it is and there is a list of architecture flags is specified then
20650b57cec5SDimitry Andric // check to make sure this Mach-O file is one of those architectures or all
20660b57cec5SDimitry Andric // architectures were specified.  If not then an error is generated and this
20670b57cec5SDimitry Andric // routine returns false.  Else it returns true.
checkMachOAndArchFlags(ObjectFile * O,StringRef Filename)20680b57cec5SDimitry Andric static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
20690b57cec5SDimitry Andric   auto *MachO = dyn_cast<MachOObjectFile>(O);
20700b57cec5SDimitry Andric 
20710b57cec5SDimitry Andric   if (!MachO || ArchAll || ArchFlags.empty())
20720b57cec5SDimitry Andric     return true;
20730b57cec5SDimitry Andric 
20740b57cec5SDimitry Andric   MachO::mach_header H;
20750b57cec5SDimitry Andric   MachO::mach_header_64 H_64;
20760b57cec5SDimitry Andric   Triple T;
20770b57cec5SDimitry Andric   const char *McpuDefault, *ArchFlag;
20780b57cec5SDimitry Andric   if (MachO->is64Bit()) {
20790b57cec5SDimitry Andric     H_64 = MachO->MachOObjectFile::getHeader64();
20800b57cec5SDimitry Andric     T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype,
20810b57cec5SDimitry Andric                                        &McpuDefault, &ArchFlag);
20820b57cec5SDimitry Andric   } else {
20830b57cec5SDimitry Andric     H = MachO->MachOObjectFile::getHeader();
20840b57cec5SDimitry Andric     T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype,
20850b57cec5SDimitry Andric                                        &McpuDefault, &ArchFlag);
20860b57cec5SDimitry Andric   }
20870b57cec5SDimitry Andric   const std::string ArchFlagName(ArchFlag);
2088e8d8bef9SDimitry Andric   if (!llvm::is_contained(ArchFlags, ArchFlagName)) {
20890b57cec5SDimitry Andric     WithColor::error(errs(), "llvm-objdump")
20900b57cec5SDimitry Andric         << Filename << ": no architecture specified.\n";
20910b57cec5SDimitry Andric     return false;
20920b57cec5SDimitry Andric   }
20930b57cec5SDimitry Andric   return true;
20940b57cec5SDimitry Andric }
20950b57cec5SDimitry Andric 
20960b57cec5SDimitry Andric static void printObjcMetaData(MachOObjectFile *O, bool verbose);
20970b57cec5SDimitry Andric 
20980b57cec5SDimitry Andric // ProcessMachO() is passed a single opened Mach-O file, which may be an
20990b57cec5SDimitry Andric // archive member and or in a slice of a universal file.  It prints the
21000b57cec5SDimitry Andric // the file name and header info and then processes it according to the
21010b57cec5SDimitry Andric // command line options.
ProcessMachO(StringRef Name,MachOObjectFile * MachOOF,StringRef ArchiveMemberName=StringRef (),StringRef ArchitectureName=StringRef ())21020b57cec5SDimitry Andric static void ProcessMachO(StringRef Name, MachOObjectFile *MachOOF,
21030b57cec5SDimitry Andric                          StringRef ArchiveMemberName = StringRef(),
21040b57cec5SDimitry Andric                          StringRef ArchitectureName = StringRef()) {
210506c3fb27SDimitry Andric   std::unique_ptr<Dumper> D = createMachODumper(*MachOOF);
210606c3fb27SDimitry Andric 
21070b57cec5SDimitry Andric   // If we are doing some processing here on the Mach-O file print the header
21080b57cec5SDimitry Andric   // info.  And don't print it otherwise like in the case of printing the
21090b57cec5SDimitry Andric   // UniversalHeaders or ArchiveHeaders.
21100b57cec5SDimitry Andric   if (Disassemble || Relocations || PrivateHeaders || ExportsTrie || Rebase ||
21110b57cec5SDimitry Andric       Bind || SymbolTable || LazyBind || WeakBind || IndirectSymbols ||
2112bdd1243dSDimitry Andric       DataInCode || FunctionStartsType != FunctionStartsMode::None ||
2113bdd1243dSDimitry Andric       LinkOptHints || ChainedFixups || DyldInfo || DylibsUsed || DylibId ||
2114bdd1243dSDimitry Andric       Rpaths || ObjcMetaData || (!FilterSections.empty())) {
2115fe6060f1SDimitry Andric     if (LeadingHeaders) {
21160b57cec5SDimitry Andric       outs() << Name;
21170b57cec5SDimitry Andric       if (!ArchiveMemberName.empty())
21180b57cec5SDimitry Andric         outs() << '(' << ArchiveMemberName << ')';
21190b57cec5SDimitry Andric       if (!ArchitectureName.empty())
21200b57cec5SDimitry Andric         outs() << " (architecture " << ArchitectureName << ")";
21210b57cec5SDimitry Andric       outs() << ":\n";
21220b57cec5SDimitry Andric     }
21230b57cec5SDimitry Andric   }
21240b57cec5SDimitry Andric   // To use the report_error() form with an ArchiveName and FileName set
21250b57cec5SDimitry Andric   // these up based on what is passed for Name and ArchiveMemberName.
21260b57cec5SDimitry Andric   StringRef ArchiveName;
21270b57cec5SDimitry Andric   StringRef FileName;
21280b57cec5SDimitry Andric   if (!ArchiveMemberName.empty()) {
21290b57cec5SDimitry Andric     ArchiveName = Name;
21300b57cec5SDimitry Andric     FileName = ArchiveMemberName;
21310b57cec5SDimitry Andric   } else {
21320b57cec5SDimitry Andric     ArchiveName = StringRef();
21330b57cec5SDimitry Andric     FileName = Name;
21340b57cec5SDimitry Andric   }
21350b57cec5SDimitry Andric 
21360b57cec5SDimitry Andric   // If we need the symbol table to do the operation then check it here to
21370b57cec5SDimitry Andric   // produce a good error message as to where the Mach-O file comes from in
21380b57cec5SDimitry Andric   // the error message.
21390b57cec5SDimitry Andric   if (Disassemble || IndirectSymbols || !FilterSections.empty() || UnwindInfo)
21400b57cec5SDimitry Andric     if (Error Err = MachOOF->checkSymbolTable())
21418bcb0991SDimitry Andric       reportError(std::move(Err), FileName, ArchiveName, ArchitectureName);
21420b57cec5SDimitry Andric 
21430b57cec5SDimitry Andric   if (DisassembleAll) {
21440b57cec5SDimitry Andric     for (const SectionRef &Section : MachOOF->sections()) {
21450b57cec5SDimitry Andric       StringRef SectName;
21468bcb0991SDimitry Andric       if (Expected<StringRef> NameOrErr = Section.getName())
21478bcb0991SDimitry Andric         SectName = *NameOrErr;
21488bcb0991SDimitry Andric       else
21498bcb0991SDimitry Andric         consumeError(NameOrErr.takeError());
21508bcb0991SDimitry Andric 
2151*0fca6ea1SDimitry Andric       if (SectName == "__text") {
21520b57cec5SDimitry Andric         DataRefImpl Ref = Section.getRawDataRefImpl();
21530b57cec5SDimitry Andric         StringRef SegName = MachOOF->getSectionFinalSegmentName(Ref);
21540b57cec5SDimitry Andric         DisassembleMachO(FileName, MachOOF, SegName, SectName);
21550b57cec5SDimitry Andric       }
21560b57cec5SDimitry Andric     }
21570b57cec5SDimitry Andric   }
21580b57cec5SDimitry Andric   else if (Disassemble) {
21590b57cec5SDimitry Andric     if (MachOOF->getHeader().filetype == MachO::MH_KEXT_BUNDLE &&
21600b57cec5SDimitry Andric         MachOOF->getHeader().cputype == MachO::CPU_TYPE_ARM64)
21610b57cec5SDimitry Andric       DisassembleMachO(FileName, MachOOF, "__TEXT_EXEC", "__text");
21620b57cec5SDimitry Andric     else
21630b57cec5SDimitry Andric       DisassembleMachO(FileName, MachOOF, "__TEXT", "__text");
21640b57cec5SDimitry Andric   }
21650b57cec5SDimitry Andric   if (IndirectSymbols)
2166fe6060f1SDimitry Andric     PrintIndirectSymbols(MachOOF, Verbose);
21670b57cec5SDimitry Andric   if (DataInCode)
2168fe6060f1SDimitry Andric     PrintDataInCodeTable(MachOOF, Verbose);
2169bdd1243dSDimitry Andric   if (FunctionStartsType != FunctionStartsMode::None)
2170fe6060f1SDimitry Andric     PrintFunctionStarts(MachOOF);
21710b57cec5SDimitry Andric   if (LinkOptHints)
21720b57cec5SDimitry Andric     PrintLinkOptHints(MachOOF);
21730b57cec5SDimitry Andric   if (Relocations)
2174fe6060f1SDimitry Andric     PrintRelocations(MachOOF, Verbose);
21750b57cec5SDimitry Andric   if (SectionHeaders)
2176753f127fSDimitry Andric     printSectionHeaders(*MachOOF);
21770b57cec5SDimitry Andric   if (SectionContents)
21780b57cec5SDimitry Andric     printSectionContents(MachOOF);
21790b57cec5SDimitry Andric   if (!FilterSections.empty())
2180fe6060f1SDimitry Andric     DumpSectionContents(FileName, MachOOF, Verbose);
21810b57cec5SDimitry Andric   if (InfoPlist)
21820b57cec5SDimitry Andric     DumpInfoPlistSectionContents(FileName, MachOOF);
218381ad6265SDimitry Andric   if (DyldInfo)
218481ad6265SDimitry Andric     PrintDyldInfo(MachOOF);
2185bdd1243dSDimitry Andric   if (ChainedFixups)
2186bdd1243dSDimitry Andric     PrintChainedFixups(MachOOF);
21870b57cec5SDimitry Andric   if (DylibsUsed)
21880b57cec5SDimitry Andric     PrintDylibs(MachOOF, false);
21890b57cec5SDimitry Andric   if (DylibId)
21900b57cec5SDimitry Andric     PrintDylibs(MachOOF, true);
21910b57cec5SDimitry Andric   if (SymbolTable)
219206c3fb27SDimitry Andric     D->printSymbolTable(ArchiveName, ArchitectureName);
21930b57cec5SDimitry Andric   if (UnwindInfo)
21940b57cec5SDimitry Andric     printMachOUnwindInfo(MachOOF);
21950b57cec5SDimitry Andric   if (PrivateHeaders) {
21960b57cec5SDimitry Andric     printMachOFileHeader(MachOOF);
21970b57cec5SDimitry Andric     printMachOLoadCommands(MachOOF);
21980b57cec5SDimitry Andric   }
21990b57cec5SDimitry Andric   if (FirstPrivateHeader)
22000b57cec5SDimitry Andric     printMachOFileHeader(MachOOF);
22010b57cec5SDimitry Andric   if (ObjcMetaData)
2202fe6060f1SDimitry Andric     printObjcMetaData(MachOOF, Verbose);
22030b57cec5SDimitry Andric   if (ExportsTrie)
22040b57cec5SDimitry Andric     printExportsTrie(MachOOF);
22050b57cec5SDimitry Andric   if (Rebase)
22060b57cec5SDimitry Andric     printRebaseTable(MachOOF);
2207fe6060f1SDimitry Andric   if (Rpaths)
2208fe6060f1SDimitry Andric     printRpaths(MachOOF);
22090b57cec5SDimitry Andric   if (Bind)
22100b57cec5SDimitry Andric     printBindTable(MachOOF);
22110b57cec5SDimitry Andric   if (LazyBind)
22120b57cec5SDimitry Andric     printLazyBindTable(MachOOF);
22130b57cec5SDimitry Andric   if (WeakBind)
22140b57cec5SDimitry Andric     printWeakBindTable(MachOOF);
22150b57cec5SDimitry Andric 
22160b57cec5SDimitry Andric   if (DwarfDumpType != DIDT_Null) {
22170b57cec5SDimitry Andric     std::unique_ptr<DIContext> DICtx = DWARFContext::create(*MachOOF);
22180b57cec5SDimitry Andric     // Dump the complete DWARF structure.
22190b57cec5SDimitry Andric     DIDumpOptions DumpOpts;
22200b57cec5SDimitry Andric     DumpOpts.DumpType = DwarfDumpType;
22210b57cec5SDimitry Andric     DICtx->dump(outs(), DumpOpts);
22220b57cec5SDimitry Andric   }
22230b57cec5SDimitry Andric }
22240b57cec5SDimitry Andric 
22250b57cec5SDimitry Andric // printUnknownCPUType() helps print_fat_headers for unknown CPU's.
printUnknownCPUType(uint32_t cputype,uint32_t cpusubtype)22260b57cec5SDimitry Andric static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
22270b57cec5SDimitry Andric   outs() << "    cputype (" << cputype << ")\n";
22280b57cec5SDimitry Andric   outs() << "    cpusubtype (" << cpusubtype << ")\n";
22290b57cec5SDimitry Andric }
22300b57cec5SDimitry Andric 
22310b57cec5SDimitry Andric // printCPUType() helps print_fat_headers by printing the cputype and
22320b57cec5SDimitry Andric // pusubtype (symbolically for the one's it knows about).
printCPUType(uint32_t cputype,uint32_t cpusubtype)22330b57cec5SDimitry Andric static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
22340b57cec5SDimitry Andric   switch (cputype) {
22350b57cec5SDimitry Andric   case MachO::CPU_TYPE_I386:
22360b57cec5SDimitry Andric     switch (cpusubtype) {
22370b57cec5SDimitry Andric     case MachO::CPU_SUBTYPE_I386_ALL:
22380b57cec5SDimitry Andric       outs() << "    cputype CPU_TYPE_I386\n";
22390b57cec5SDimitry Andric       outs() << "    cpusubtype CPU_SUBTYPE_I386_ALL\n";
22400b57cec5SDimitry Andric       break;
22410b57cec5SDimitry Andric     default:
22420b57cec5SDimitry Andric       printUnknownCPUType(cputype, cpusubtype);
22430b57cec5SDimitry Andric       break;
22440b57cec5SDimitry Andric     }
22450b57cec5SDimitry Andric     break;
22460b57cec5SDimitry Andric   case MachO::CPU_TYPE_X86_64:
22470b57cec5SDimitry Andric     switch (cpusubtype) {
22480b57cec5SDimitry Andric     case MachO::CPU_SUBTYPE_X86_64_ALL:
22490b57cec5SDimitry Andric       outs() << "    cputype CPU_TYPE_X86_64\n";
22500b57cec5SDimitry Andric       outs() << "    cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
22510b57cec5SDimitry Andric       break;
22520b57cec5SDimitry Andric     case MachO::CPU_SUBTYPE_X86_64_H:
22530b57cec5SDimitry Andric       outs() << "    cputype CPU_TYPE_X86_64\n";
22540b57cec5SDimitry Andric       outs() << "    cpusubtype CPU_SUBTYPE_X86_64_H\n";
22550b57cec5SDimitry Andric       break;
22560b57cec5SDimitry Andric     default:
22570b57cec5SDimitry Andric       printUnknownCPUType(cputype, cpusubtype);
22580b57cec5SDimitry Andric       break;
22590b57cec5SDimitry Andric     }
22600b57cec5SDimitry Andric     break;
22610b57cec5SDimitry Andric   case MachO::CPU_TYPE_ARM:
22620b57cec5SDimitry Andric     switch (cpusubtype) {
22630b57cec5SDimitry Andric     case MachO::CPU_SUBTYPE_ARM_ALL:
22640b57cec5SDimitry Andric       outs() << "    cputype CPU_TYPE_ARM\n";
22650b57cec5SDimitry Andric       outs() << "    cpusubtype CPU_SUBTYPE_ARM_ALL\n";
22660b57cec5SDimitry Andric       break;
22670b57cec5SDimitry Andric     case MachO::CPU_SUBTYPE_ARM_V4T:
22680b57cec5SDimitry Andric       outs() << "    cputype CPU_TYPE_ARM\n";
22690b57cec5SDimitry Andric       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V4T\n";
22700b57cec5SDimitry Andric       break;
22710b57cec5SDimitry Andric     case MachO::CPU_SUBTYPE_ARM_V5TEJ:
22720b57cec5SDimitry Andric       outs() << "    cputype CPU_TYPE_ARM\n";
22730b57cec5SDimitry Andric       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
22740b57cec5SDimitry Andric       break;
22750b57cec5SDimitry Andric     case MachO::CPU_SUBTYPE_ARM_XSCALE:
22760b57cec5SDimitry Andric       outs() << "    cputype CPU_TYPE_ARM\n";
22770b57cec5SDimitry Andric       outs() << "    cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
22780b57cec5SDimitry Andric       break;
22790b57cec5SDimitry Andric     case MachO::CPU_SUBTYPE_ARM_V6:
22800b57cec5SDimitry Andric       outs() << "    cputype CPU_TYPE_ARM\n";
22810b57cec5SDimitry Andric       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V6\n";
22820b57cec5SDimitry Andric       break;
22830b57cec5SDimitry Andric     case MachO::CPU_SUBTYPE_ARM_V6M:
22840b57cec5SDimitry Andric       outs() << "    cputype CPU_TYPE_ARM\n";
22850b57cec5SDimitry Andric       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V6M\n";
22860b57cec5SDimitry Andric       break;
22870b57cec5SDimitry Andric     case MachO::CPU_SUBTYPE_ARM_V7:
22880b57cec5SDimitry Andric       outs() << "    cputype CPU_TYPE_ARM\n";
22890b57cec5SDimitry Andric       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V7\n";
22900b57cec5SDimitry Andric       break;
22910b57cec5SDimitry Andric     case MachO::CPU_SUBTYPE_ARM_V7EM:
22920b57cec5SDimitry Andric       outs() << "    cputype CPU_TYPE_ARM\n";
22930b57cec5SDimitry Andric       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
22940b57cec5SDimitry Andric       break;
22950b57cec5SDimitry Andric     case MachO::CPU_SUBTYPE_ARM_V7K:
22960b57cec5SDimitry Andric       outs() << "    cputype CPU_TYPE_ARM\n";
22970b57cec5SDimitry Andric       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V7K\n";
22980b57cec5SDimitry Andric       break;
22990b57cec5SDimitry Andric     case MachO::CPU_SUBTYPE_ARM_V7M:
23000b57cec5SDimitry Andric       outs() << "    cputype CPU_TYPE_ARM\n";
23010b57cec5SDimitry Andric       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V7M\n";
23020b57cec5SDimitry Andric       break;
23030b57cec5SDimitry Andric     case MachO::CPU_SUBTYPE_ARM_V7S:
23040b57cec5SDimitry Andric       outs() << "    cputype CPU_TYPE_ARM\n";
23050b57cec5SDimitry Andric       outs() << "    cpusubtype CPU_SUBTYPE_ARM_V7S\n";
23060b57cec5SDimitry Andric       break;
23070b57cec5SDimitry Andric     default:
23080b57cec5SDimitry Andric       printUnknownCPUType(cputype, cpusubtype);
23090b57cec5SDimitry Andric       break;
23100b57cec5SDimitry Andric     }
23110b57cec5SDimitry Andric     break;
23120b57cec5SDimitry Andric   case MachO::CPU_TYPE_ARM64:
23130b57cec5SDimitry Andric     switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
23140b57cec5SDimitry Andric     case MachO::CPU_SUBTYPE_ARM64_ALL:
23150b57cec5SDimitry Andric       outs() << "    cputype CPU_TYPE_ARM64\n";
23160b57cec5SDimitry Andric       outs() << "    cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
23170b57cec5SDimitry Andric       break;
2318e8d8bef9SDimitry Andric     case MachO::CPU_SUBTYPE_ARM64_V8:
2319e8d8bef9SDimitry Andric       outs() << "    cputype CPU_TYPE_ARM64\n";
2320e8d8bef9SDimitry Andric       outs() << "    cpusubtype CPU_SUBTYPE_ARM64_V8\n";
2321e8d8bef9SDimitry Andric       break;
23220b57cec5SDimitry Andric     case MachO::CPU_SUBTYPE_ARM64E:
23230b57cec5SDimitry Andric       outs() << "    cputype CPU_TYPE_ARM64\n";
23240b57cec5SDimitry Andric       outs() << "    cpusubtype CPU_SUBTYPE_ARM64E\n";
23250b57cec5SDimitry Andric       break;
23260b57cec5SDimitry Andric     default:
23270b57cec5SDimitry Andric       printUnknownCPUType(cputype, cpusubtype);
23280b57cec5SDimitry Andric       break;
23290b57cec5SDimitry Andric     }
23300b57cec5SDimitry Andric     break;
23310b57cec5SDimitry Andric   case MachO::CPU_TYPE_ARM64_32:
23320b57cec5SDimitry Andric     switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
23330b57cec5SDimitry Andric     case MachO::CPU_SUBTYPE_ARM64_32_V8:
23340b57cec5SDimitry Andric       outs() << "    cputype CPU_TYPE_ARM64_32\n";
23350b57cec5SDimitry Andric       outs() << "    cpusubtype CPU_SUBTYPE_ARM64_32_V8\n";
23360b57cec5SDimitry Andric       break;
23370b57cec5SDimitry Andric     default:
23380b57cec5SDimitry Andric       printUnknownCPUType(cputype, cpusubtype);
23390b57cec5SDimitry Andric       break;
23400b57cec5SDimitry Andric     }
23410b57cec5SDimitry Andric     break;
23420b57cec5SDimitry Andric   default:
23430b57cec5SDimitry Andric     printUnknownCPUType(cputype, cpusubtype);
23440b57cec5SDimitry Andric     break;
23450b57cec5SDimitry Andric   }
23460b57cec5SDimitry Andric }
23470b57cec5SDimitry Andric 
printMachOUniversalHeaders(const object::MachOUniversalBinary * UB,bool verbose)23480b57cec5SDimitry Andric static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
23490b57cec5SDimitry Andric                                        bool verbose) {
23500b57cec5SDimitry Andric   outs() << "Fat headers\n";
23510b57cec5SDimitry Andric   if (verbose) {
23520b57cec5SDimitry Andric     if (UB->getMagic() == MachO::FAT_MAGIC)
23530b57cec5SDimitry Andric       outs() << "fat_magic FAT_MAGIC\n";
23540b57cec5SDimitry Andric     else // UB->getMagic() == MachO::FAT_MAGIC_64
23550b57cec5SDimitry Andric       outs() << "fat_magic FAT_MAGIC_64\n";
23560b57cec5SDimitry Andric   } else
23570b57cec5SDimitry Andric     outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
23580b57cec5SDimitry Andric 
23590b57cec5SDimitry Andric   uint32_t nfat_arch = UB->getNumberOfObjects();
23600b57cec5SDimitry Andric   StringRef Buf = UB->getData();
23610b57cec5SDimitry Andric   uint64_t size = Buf.size();
23620b57cec5SDimitry Andric   uint64_t big_size = sizeof(struct MachO::fat_header) +
23630b57cec5SDimitry Andric                       nfat_arch * sizeof(struct MachO::fat_arch);
23640b57cec5SDimitry Andric   outs() << "nfat_arch " << UB->getNumberOfObjects();
23650b57cec5SDimitry Andric   if (nfat_arch == 0)
23660b57cec5SDimitry Andric     outs() << " (malformed, contains zero architecture types)\n";
23670b57cec5SDimitry Andric   else if (big_size > size)
23680b57cec5SDimitry Andric     outs() << " (malformed, architectures past end of file)\n";
23690b57cec5SDimitry Andric   else
23700b57cec5SDimitry Andric     outs() << "\n";
23710b57cec5SDimitry Andric 
23720b57cec5SDimitry Andric   for (uint32_t i = 0; i < nfat_arch; ++i) {
23730b57cec5SDimitry Andric     MachOUniversalBinary::ObjectForArch OFA(UB, i);
23740b57cec5SDimitry Andric     uint32_t cputype = OFA.getCPUType();
23750b57cec5SDimitry Andric     uint32_t cpusubtype = OFA.getCPUSubType();
23760b57cec5SDimitry Andric     outs() << "architecture ";
23770b57cec5SDimitry Andric     for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
23780b57cec5SDimitry Andric       MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
23790b57cec5SDimitry Andric       uint32_t other_cputype = other_OFA.getCPUType();
23800b57cec5SDimitry Andric       uint32_t other_cpusubtype = other_OFA.getCPUSubType();
23810b57cec5SDimitry Andric       if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
23820b57cec5SDimitry Andric           (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
23830b57cec5SDimitry Andric               (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
23840b57cec5SDimitry Andric         outs() << "(illegal duplicate architecture) ";
23850b57cec5SDimitry Andric         break;
23860b57cec5SDimitry Andric       }
23870b57cec5SDimitry Andric     }
23880b57cec5SDimitry Andric     if (verbose) {
23890b57cec5SDimitry Andric       outs() << OFA.getArchFlagName() << "\n";
23900b57cec5SDimitry Andric       printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
23910b57cec5SDimitry Andric     } else {
23920b57cec5SDimitry Andric       outs() << i << "\n";
23930b57cec5SDimitry Andric       outs() << "    cputype " << cputype << "\n";
23940b57cec5SDimitry Andric       outs() << "    cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
23950b57cec5SDimitry Andric              << "\n";
23960b57cec5SDimitry Andric     }
23970b57cec5SDimitry Andric     if (verbose &&
23980b57cec5SDimitry Andric         (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
23990b57cec5SDimitry Andric       outs() << "    capabilities CPU_SUBTYPE_LIB64\n";
24000b57cec5SDimitry Andric     else
24010b57cec5SDimitry Andric       outs() << "    capabilities "
24020b57cec5SDimitry Andric              << format("0x%" PRIx32,
24030b57cec5SDimitry Andric                        (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
24040b57cec5SDimitry Andric     outs() << "    offset " << OFA.getOffset();
24050b57cec5SDimitry Andric     if (OFA.getOffset() > size)
24060b57cec5SDimitry Andric       outs() << " (past end of file)";
24078bcb0991SDimitry Andric     if (OFA.getOffset() % (1ull << OFA.getAlign()) != 0)
24080b57cec5SDimitry Andric       outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
24090b57cec5SDimitry Andric     outs() << "\n";
24100b57cec5SDimitry Andric     outs() << "    size " << OFA.getSize();
24110b57cec5SDimitry Andric     big_size = OFA.getOffset() + OFA.getSize();
24120b57cec5SDimitry Andric     if (big_size > size)
24130b57cec5SDimitry Andric       outs() << " (past end of file)";
24140b57cec5SDimitry Andric     outs() << "\n";
24150b57cec5SDimitry Andric     outs() << "    align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
24160b57cec5SDimitry Andric            << ")\n";
24170b57cec5SDimitry Andric   }
24180b57cec5SDimitry Andric }
24190b57cec5SDimitry Andric 
printArchiveChild(StringRef Filename,const Archive::Child & C,size_t ChildIndex,bool verbose,bool print_offset,StringRef ArchitectureName=StringRef ())24200b57cec5SDimitry Andric static void printArchiveChild(StringRef Filename, const Archive::Child &C,
24218bcb0991SDimitry Andric                               size_t ChildIndex, bool verbose,
24228bcb0991SDimitry Andric                               bool print_offset,
24230b57cec5SDimitry Andric                               StringRef ArchitectureName = StringRef()) {
24240b57cec5SDimitry Andric   if (print_offset)
24250b57cec5SDimitry Andric     outs() << C.getChildOffset() << "\t";
24260b57cec5SDimitry Andric   sys::fs::perms Mode =
24278bcb0991SDimitry Andric       unwrapOrError(C.getAccessMode(), getFileNameForError(C, ChildIndex),
24288bcb0991SDimitry Andric                     Filename, ArchitectureName);
24290b57cec5SDimitry Andric   if (verbose) {
24300b57cec5SDimitry Andric     // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
24310b57cec5SDimitry Andric     // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
24320b57cec5SDimitry Andric     outs() << "-";
24330b57cec5SDimitry Andric     outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
24340b57cec5SDimitry Andric     outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
24350b57cec5SDimitry Andric     outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
24360b57cec5SDimitry Andric     outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
24370b57cec5SDimitry Andric     outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
24380b57cec5SDimitry Andric     outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
24390b57cec5SDimitry Andric     outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
24400b57cec5SDimitry Andric     outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
24410b57cec5SDimitry Andric     outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
24420b57cec5SDimitry Andric   } else {
24430b57cec5SDimitry Andric     outs() << format("0%o ", Mode);
24440b57cec5SDimitry Andric   }
24450b57cec5SDimitry Andric 
24468bcb0991SDimitry Andric   outs() << format("%3d/%-3d %5" PRId64 " ",
24478bcb0991SDimitry Andric                    unwrapOrError(C.getUID(), getFileNameForError(C, ChildIndex),
24488bcb0991SDimitry Andric                                  Filename, ArchitectureName),
24498bcb0991SDimitry Andric                    unwrapOrError(C.getGID(), getFileNameForError(C, ChildIndex),
24508bcb0991SDimitry Andric                                  Filename, ArchitectureName),
24518bcb0991SDimitry Andric                    unwrapOrError(C.getRawSize(),
24528bcb0991SDimitry Andric                                  getFileNameForError(C, ChildIndex), Filename,
24538bcb0991SDimitry Andric                                  ArchitectureName));
24540b57cec5SDimitry Andric 
24550b57cec5SDimitry Andric   StringRef RawLastModified = C.getRawLastModified();
24560b57cec5SDimitry Andric   if (verbose) {
24570b57cec5SDimitry Andric     unsigned Seconds;
24580b57cec5SDimitry Andric     if (RawLastModified.getAsInteger(10, Seconds))
24590b57cec5SDimitry Andric       outs() << "(date: \"" << RawLastModified
24600b57cec5SDimitry Andric              << "\" contains non-decimal chars) ";
24610b57cec5SDimitry Andric     else {
24620b57cec5SDimitry Andric       // Since cime(3) returns a 26 character string of the form:
24630b57cec5SDimitry Andric       // "Sun Sep 16 01:03:52 1973\n\0"
24640b57cec5SDimitry Andric       // just print 24 characters.
24650b57cec5SDimitry Andric       time_t t = Seconds;
24660b57cec5SDimitry Andric       outs() << format("%.24s ", ctime(&t));
24670b57cec5SDimitry Andric     }
24680b57cec5SDimitry Andric   } else {
24690b57cec5SDimitry Andric     outs() << RawLastModified << " ";
24700b57cec5SDimitry Andric   }
24710b57cec5SDimitry Andric 
24720b57cec5SDimitry Andric   if (verbose) {
24730b57cec5SDimitry Andric     Expected<StringRef> NameOrErr = C.getName();
24740b57cec5SDimitry Andric     if (!NameOrErr) {
24750b57cec5SDimitry Andric       consumeError(NameOrErr.takeError());
24768bcb0991SDimitry Andric       outs() << unwrapOrError(C.getRawName(),
24778bcb0991SDimitry Andric                               getFileNameForError(C, ChildIndex), Filename,
24788bcb0991SDimitry Andric                               ArchitectureName)
24790b57cec5SDimitry Andric              << "\n";
24800b57cec5SDimitry Andric     } else {
24810b57cec5SDimitry Andric       StringRef Name = NameOrErr.get();
24820b57cec5SDimitry Andric       outs() << Name << "\n";
24830b57cec5SDimitry Andric     }
24840b57cec5SDimitry Andric   } else {
24858bcb0991SDimitry Andric     outs() << unwrapOrError(C.getRawName(), getFileNameForError(C, ChildIndex),
24868bcb0991SDimitry Andric                             Filename, ArchitectureName)
24870b57cec5SDimitry Andric            << "\n";
24880b57cec5SDimitry Andric   }
24890b57cec5SDimitry Andric }
24900b57cec5SDimitry Andric 
printArchiveHeaders(StringRef Filename,Archive * A,bool verbose,bool print_offset,StringRef ArchitectureName=StringRef ())24910b57cec5SDimitry Andric static void printArchiveHeaders(StringRef Filename, Archive *A, bool verbose,
24920b57cec5SDimitry Andric                                 bool print_offset,
24930b57cec5SDimitry Andric                                 StringRef ArchitectureName = StringRef()) {
24940b57cec5SDimitry Andric   Error Err = Error::success();
24958bcb0991SDimitry Andric   size_t I = 0;
24960b57cec5SDimitry Andric   for (const auto &C : A->children(Err, false))
24978bcb0991SDimitry Andric     printArchiveChild(Filename, C, I++, verbose, print_offset,
24988bcb0991SDimitry Andric                       ArchitectureName);
24990b57cec5SDimitry Andric 
25000b57cec5SDimitry Andric   if (Err)
25018bcb0991SDimitry Andric     reportError(std::move(Err), Filename, "", ArchitectureName);
25020b57cec5SDimitry Andric }
25030b57cec5SDimitry Andric 
ValidateArchFlags()25040b57cec5SDimitry Andric static bool ValidateArchFlags() {
25050b57cec5SDimitry Andric   // Check for -arch all and verifiy the -arch flags are valid.
25060b57cec5SDimitry Andric   for (unsigned i = 0; i < ArchFlags.size(); ++i) {
25070b57cec5SDimitry Andric     if (ArchFlags[i] == "all") {
25080b57cec5SDimitry Andric       ArchAll = true;
25090b57cec5SDimitry Andric     } else {
25100b57cec5SDimitry Andric       if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
25110b57cec5SDimitry Andric         WithColor::error(errs(), "llvm-objdump")
25120b57cec5SDimitry Andric             << "unknown architecture named '" + ArchFlags[i] +
25130b57cec5SDimitry Andric                    "'for the -arch option\n";
25140b57cec5SDimitry Andric         return false;
25150b57cec5SDimitry Andric       }
25160b57cec5SDimitry Andric     }
25170b57cec5SDimitry Andric   }
25180b57cec5SDimitry Andric   return true;
25190b57cec5SDimitry Andric }
25200b57cec5SDimitry Andric 
25210b57cec5SDimitry Andric // ParseInputMachO() parses the named Mach-O file in Filename and handles the
25220b57cec5SDimitry Andric // -arch flags selecting just those slices as specified by them and also parses
25230b57cec5SDimitry Andric // archive files.  Then for each individual Mach-O file ProcessMachO() is
25240b57cec5SDimitry Andric // called to process the file based on the command line options.
parseInputMachO(StringRef Filename)25255ffd83dbSDimitry Andric void objdump::parseInputMachO(StringRef Filename) {
25260b57cec5SDimitry Andric   if (!ValidateArchFlags())
25270b57cec5SDimitry Andric     return;
25280b57cec5SDimitry Andric 
25290b57cec5SDimitry Andric   // Attempt to open the binary.
25300b57cec5SDimitry Andric   Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
25310b57cec5SDimitry Andric   if (!BinaryOrErr) {
25320b57cec5SDimitry Andric     if (Error E = isNotObjectErrorInvalidFileType(BinaryOrErr.takeError()))
25338bcb0991SDimitry Andric       reportError(std::move(E), Filename);
25340b57cec5SDimitry Andric     else
25350b57cec5SDimitry Andric       outs() << Filename << ": is not an object file\n";
25360b57cec5SDimitry Andric     return;
25370b57cec5SDimitry Andric   }
25380b57cec5SDimitry Andric   Binary &Bin = *BinaryOrErr.get().getBinary();
25390b57cec5SDimitry Andric 
25400b57cec5SDimitry Andric   if (Archive *A = dyn_cast<Archive>(&Bin)) {
25410b57cec5SDimitry Andric     outs() << "Archive : " << Filename << "\n";
25420b57cec5SDimitry Andric     if (ArchiveHeaders)
2543fe6060f1SDimitry Andric       printArchiveHeaders(Filename, A, Verbose, ArchiveMemberOffsets);
25440b57cec5SDimitry Andric 
25450b57cec5SDimitry Andric     Error Err = Error::success();
25468bcb0991SDimitry Andric     unsigned I = -1;
25470b57cec5SDimitry Andric     for (auto &C : A->children(Err)) {
25488bcb0991SDimitry Andric       ++I;
25490b57cec5SDimitry Andric       Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
25500b57cec5SDimitry Andric       if (!ChildOrErr) {
25510b57cec5SDimitry Andric         if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
25528bcb0991SDimitry Andric           reportError(std::move(E), getFileNameForError(C, I), Filename);
25530b57cec5SDimitry Andric         continue;
25540b57cec5SDimitry Andric       }
25550b57cec5SDimitry Andric       if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
25560b57cec5SDimitry Andric         if (!checkMachOAndArchFlags(O, Filename))
25570b57cec5SDimitry Andric           return;
25580b57cec5SDimitry Andric         ProcessMachO(Filename, O, O->getFileName());
25590b57cec5SDimitry Andric       }
25600b57cec5SDimitry Andric     }
25610b57cec5SDimitry Andric     if (Err)
25628bcb0991SDimitry Andric       reportError(std::move(Err), Filename);
25630b57cec5SDimitry Andric     return;
25640b57cec5SDimitry Andric   }
25650b57cec5SDimitry Andric   if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
25660b57cec5SDimitry Andric     parseInputMachO(UB);
25670b57cec5SDimitry Andric     return;
25680b57cec5SDimitry Andric   }
25690b57cec5SDimitry Andric   if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
25700b57cec5SDimitry Andric     if (!checkMachOAndArchFlags(O, Filename))
25710b57cec5SDimitry Andric       return;
25720b57cec5SDimitry Andric     if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O))
25730b57cec5SDimitry Andric       ProcessMachO(Filename, MachOOF);
25740b57cec5SDimitry Andric     else
25750b57cec5SDimitry Andric       WithColor::error(errs(), "llvm-objdump")
25760b57cec5SDimitry Andric           << Filename << "': "
25770b57cec5SDimitry Andric           << "object is not a Mach-O file type.\n";
25780b57cec5SDimitry Andric     return;
25790b57cec5SDimitry Andric   }
25800b57cec5SDimitry Andric   llvm_unreachable("Input object can't be invalid at this point");
25810b57cec5SDimitry Andric }
25820b57cec5SDimitry Andric 
parseInputMachO(MachOUniversalBinary * UB)25835ffd83dbSDimitry Andric void objdump::parseInputMachO(MachOUniversalBinary *UB) {
25840b57cec5SDimitry Andric   if (!ValidateArchFlags())
25850b57cec5SDimitry Andric     return;
25860b57cec5SDimitry Andric 
25870b57cec5SDimitry Andric   auto Filename = UB->getFileName();
25880b57cec5SDimitry Andric 
25890b57cec5SDimitry Andric   if (UniversalHeaders)
2590fe6060f1SDimitry Andric     printMachOUniversalHeaders(UB, Verbose);
25910b57cec5SDimitry Andric 
25920b57cec5SDimitry Andric   // If we have a list of architecture flags specified dump only those.
25930b57cec5SDimitry Andric   if (!ArchAll && !ArchFlags.empty()) {
25940b57cec5SDimitry Andric     // Look for a slice in the universal binary that matches each ArchFlag.
25950b57cec5SDimitry Andric     bool ArchFound;
25960b57cec5SDimitry Andric     for (unsigned i = 0; i < ArchFlags.size(); ++i) {
25970b57cec5SDimitry Andric       ArchFound = false;
25980b57cec5SDimitry Andric       for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
25990b57cec5SDimitry Andric                                                   E = UB->end_objects();
26000b57cec5SDimitry Andric             I != E; ++I) {
26010b57cec5SDimitry Andric         if (ArchFlags[i] == I->getArchFlagName()) {
26020b57cec5SDimitry Andric           ArchFound = true;
26030b57cec5SDimitry Andric           Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
26040b57cec5SDimitry Andric               I->getAsObjectFile();
2605e8d8bef9SDimitry Andric           std::string ArchitectureName;
26060b57cec5SDimitry Andric           if (ArchFlags.size() > 1)
26070b57cec5SDimitry Andric             ArchitectureName = I->getArchFlagName();
26080b57cec5SDimitry Andric           if (ObjOrErr) {
26090b57cec5SDimitry Andric             ObjectFile &O = *ObjOrErr.get();
26100b57cec5SDimitry Andric             if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
26110b57cec5SDimitry Andric               ProcessMachO(Filename, MachOOF, "", ArchitectureName);
26120b57cec5SDimitry Andric           } else if (Error E = isNotObjectErrorInvalidFileType(
26130b57cec5SDimitry Andric                          ObjOrErr.takeError())) {
26148bcb0991SDimitry Andric             reportError(std::move(E), "", Filename, ArchitectureName);
26150b57cec5SDimitry Andric             continue;
26160b57cec5SDimitry Andric           } else if (Expected<std::unique_ptr<Archive>> AOrErr =
26170b57cec5SDimitry Andric                          I->getAsArchive()) {
26180b57cec5SDimitry Andric             std::unique_ptr<Archive> &A = *AOrErr;
26190b57cec5SDimitry Andric             outs() << "Archive : " << Filename;
26200b57cec5SDimitry Andric             if (!ArchitectureName.empty())
26210b57cec5SDimitry Andric               outs() << " (architecture " << ArchitectureName << ")";
26220b57cec5SDimitry Andric             outs() << "\n";
26230b57cec5SDimitry Andric             if (ArchiveHeaders)
2624fe6060f1SDimitry Andric               printArchiveHeaders(Filename, A.get(), Verbose,
26250b57cec5SDimitry Andric                                   ArchiveMemberOffsets, ArchitectureName);
26260b57cec5SDimitry Andric             Error Err = Error::success();
26278bcb0991SDimitry Andric             unsigned I = -1;
26280b57cec5SDimitry Andric             for (auto &C : A->children(Err)) {
26298bcb0991SDimitry Andric               ++I;
26300b57cec5SDimitry Andric               Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
26310b57cec5SDimitry Andric               if (!ChildOrErr) {
26328bcb0991SDimitry Andric                 if (Error E =
26338bcb0991SDimitry Andric                         isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
26348bcb0991SDimitry Andric                   reportError(std::move(E), getFileNameForError(C, I), Filename,
26358bcb0991SDimitry Andric                               ArchitectureName);
26360b57cec5SDimitry Andric                 continue;
26370b57cec5SDimitry Andric               }
26380b57cec5SDimitry Andric               if (MachOObjectFile *O =
26390b57cec5SDimitry Andric                       dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
26400b57cec5SDimitry Andric                 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
26410b57cec5SDimitry Andric             }
26420b57cec5SDimitry Andric             if (Err)
26438bcb0991SDimitry Andric               reportError(std::move(Err), Filename);
26440b57cec5SDimitry Andric           } else {
26450b57cec5SDimitry Andric             consumeError(AOrErr.takeError());
26468bcb0991SDimitry Andric             reportError(Filename,
26478bcb0991SDimitry Andric                         "Mach-O universal file for architecture " +
26488bcb0991SDimitry Andric                             StringRef(I->getArchFlagName()) +
26490b57cec5SDimitry Andric                             " is not a Mach-O file or an archive file");
26500b57cec5SDimitry Andric           }
26510b57cec5SDimitry Andric         }
26520b57cec5SDimitry Andric       }
26530b57cec5SDimitry Andric       if (!ArchFound) {
26540b57cec5SDimitry Andric         WithColor::error(errs(), "llvm-objdump")
26550b57cec5SDimitry Andric             << "file: " + Filename + " does not contain "
26560b57cec5SDimitry Andric             << "architecture: " + ArchFlags[i] + "\n";
26570b57cec5SDimitry Andric         return;
26580b57cec5SDimitry Andric       }
26590b57cec5SDimitry Andric     }
26600b57cec5SDimitry Andric     return;
26610b57cec5SDimitry Andric   }
26620b57cec5SDimitry Andric   // No architecture flags were specified so if this contains a slice that
26630b57cec5SDimitry Andric   // matches the host architecture dump only that.
26640b57cec5SDimitry Andric   if (!ArchAll) {
26650b57cec5SDimitry Andric     for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
26660b57cec5SDimitry Andric                                                 E = UB->end_objects();
26670b57cec5SDimitry Andric           I != E; ++I) {
26680b57cec5SDimitry Andric       if (MachOObjectFile::getHostArch().getArchName() ==
26690b57cec5SDimitry Andric           I->getArchFlagName()) {
26700b57cec5SDimitry Andric         Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
26710b57cec5SDimitry Andric         std::string ArchiveName;
26720b57cec5SDimitry Andric         ArchiveName.clear();
26730b57cec5SDimitry Andric         if (ObjOrErr) {
26740b57cec5SDimitry Andric           ObjectFile &O = *ObjOrErr.get();
26750b57cec5SDimitry Andric           if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
26760b57cec5SDimitry Andric             ProcessMachO(Filename, MachOOF);
26770b57cec5SDimitry Andric         } else if (Error E =
26780b57cec5SDimitry Andric                        isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) {
26798bcb0991SDimitry Andric           reportError(std::move(E), Filename);
26800b57cec5SDimitry Andric         } else if (Expected<std::unique_ptr<Archive>> AOrErr =
26810b57cec5SDimitry Andric                        I->getAsArchive()) {
26820b57cec5SDimitry Andric           std::unique_ptr<Archive> &A = *AOrErr;
26830b57cec5SDimitry Andric           outs() << "Archive : " << Filename << "\n";
26840b57cec5SDimitry Andric           if (ArchiveHeaders)
2685fe6060f1SDimitry Andric             printArchiveHeaders(Filename, A.get(), Verbose,
26860b57cec5SDimitry Andric                                 ArchiveMemberOffsets);
26870b57cec5SDimitry Andric           Error Err = Error::success();
26888bcb0991SDimitry Andric           unsigned I = -1;
26890b57cec5SDimitry Andric           for (auto &C : A->children(Err)) {
26908bcb0991SDimitry Andric             ++I;
26910b57cec5SDimitry Andric             Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
26920b57cec5SDimitry Andric             if (!ChildOrErr) {
26930b57cec5SDimitry Andric               if (Error E =
26940b57cec5SDimitry Andric                       isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
26958bcb0991SDimitry Andric                 reportError(std::move(E), getFileNameForError(C, I), Filename);
26960b57cec5SDimitry Andric               continue;
26970b57cec5SDimitry Andric             }
26980b57cec5SDimitry Andric             if (MachOObjectFile *O =
26990b57cec5SDimitry Andric                     dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
27000b57cec5SDimitry Andric               ProcessMachO(Filename, O, O->getFileName());
27010b57cec5SDimitry Andric           }
27020b57cec5SDimitry Andric           if (Err)
27038bcb0991SDimitry Andric             reportError(std::move(Err), Filename);
27040b57cec5SDimitry Andric         } else {
27050b57cec5SDimitry Andric           consumeError(AOrErr.takeError());
27068bcb0991SDimitry Andric           reportError(Filename, "Mach-O universal file for architecture " +
27070b57cec5SDimitry Andric                                     StringRef(I->getArchFlagName()) +
27080b57cec5SDimitry Andric                                     " is not a Mach-O file or an archive file");
27090b57cec5SDimitry Andric         }
27100b57cec5SDimitry Andric         return;
27110b57cec5SDimitry Andric       }
27120b57cec5SDimitry Andric     }
27130b57cec5SDimitry Andric   }
27140b57cec5SDimitry Andric   // Either all architectures have been specified or none have been specified
27150b57cec5SDimitry Andric   // and this does not contain the host architecture so dump all the slices.
27160b57cec5SDimitry Andric   bool moreThanOneArch = UB->getNumberOfObjects() > 1;
27170b57cec5SDimitry Andric   for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
27180b57cec5SDimitry Andric                                               E = UB->end_objects();
27190b57cec5SDimitry Andric         I != E; ++I) {
27200b57cec5SDimitry Andric     Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
2721e8d8bef9SDimitry Andric     std::string ArchitectureName;
27220b57cec5SDimitry Andric     if (moreThanOneArch)
27230b57cec5SDimitry Andric       ArchitectureName = I->getArchFlagName();
27240b57cec5SDimitry Andric     if (ObjOrErr) {
27250b57cec5SDimitry Andric       ObjectFile &Obj = *ObjOrErr.get();
27260b57cec5SDimitry Andric       if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
27270b57cec5SDimitry Andric         ProcessMachO(Filename, MachOOF, "", ArchitectureName);
27280b57cec5SDimitry Andric     } else if (Error E =
27290b57cec5SDimitry Andric                    isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) {
27308bcb0991SDimitry Andric       reportError(std::move(E), Filename, "", ArchitectureName);
27310b57cec5SDimitry Andric     } else if (Expected<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
27320b57cec5SDimitry Andric       std::unique_ptr<Archive> &A = *AOrErr;
27330b57cec5SDimitry Andric       outs() << "Archive : " << Filename;
27340b57cec5SDimitry Andric       if (!ArchitectureName.empty())
27350b57cec5SDimitry Andric         outs() << " (architecture " << ArchitectureName << ")";
27360b57cec5SDimitry Andric       outs() << "\n";
27370b57cec5SDimitry Andric       if (ArchiveHeaders)
2738fe6060f1SDimitry Andric         printArchiveHeaders(Filename, A.get(), Verbose, ArchiveMemberOffsets,
2739fe6060f1SDimitry Andric                             ArchitectureName);
27400b57cec5SDimitry Andric       Error Err = Error::success();
27418bcb0991SDimitry Andric       unsigned I = -1;
27420b57cec5SDimitry Andric       for (auto &C : A->children(Err)) {
27438bcb0991SDimitry Andric         ++I;
27440b57cec5SDimitry Andric         Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
27450b57cec5SDimitry Andric         if (!ChildOrErr) {
27460b57cec5SDimitry Andric           if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
27478bcb0991SDimitry Andric             reportError(std::move(E), getFileNameForError(C, I), Filename,
27488bcb0991SDimitry Andric                         ArchitectureName);
27490b57cec5SDimitry Andric           continue;
27500b57cec5SDimitry Andric         }
27510b57cec5SDimitry Andric         if (MachOObjectFile *O =
27520b57cec5SDimitry Andric                 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
27530b57cec5SDimitry Andric           if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
27540b57cec5SDimitry Andric             ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
27550b57cec5SDimitry Andric                           ArchitectureName);
27560b57cec5SDimitry Andric         }
27570b57cec5SDimitry Andric       }
27580b57cec5SDimitry Andric       if (Err)
27598bcb0991SDimitry Andric         reportError(std::move(Err), Filename);
27600b57cec5SDimitry Andric     } else {
27610b57cec5SDimitry Andric       consumeError(AOrErr.takeError());
27628bcb0991SDimitry Andric       reportError(Filename, "Mach-O universal file for architecture " +
27630b57cec5SDimitry Andric                                 StringRef(I->getArchFlagName()) +
27640b57cec5SDimitry Andric                                 " is not a Mach-O file or an archive file");
27650b57cec5SDimitry Andric     }
27660b57cec5SDimitry Andric   }
27670b57cec5SDimitry Andric }
27680b57cec5SDimitry Andric 
27695ffd83dbSDimitry Andric namespace {
27700b57cec5SDimitry Andric // The block of info used by the Symbolizer call backs.
27710b57cec5SDimitry Andric struct DisassembleInfo {
DisassembleInfo__anon061faff00611::DisassembleInfo27720b57cec5SDimitry Andric   DisassembleInfo(MachOObjectFile *O, SymbolAddressMap *AddrMap,
27730b57cec5SDimitry Andric                   std::vector<SectionRef> *Sections, bool verbose)
27740b57cec5SDimitry Andric     : verbose(verbose), O(O), AddrMap(AddrMap), Sections(Sections) {}
27750b57cec5SDimitry Andric   bool verbose;
27760b57cec5SDimitry Andric   MachOObjectFile *O;
27770b57cec5SDimitry Andric   SectionRef S;
27780b57cec5SDimitry Andric   SymbolAddressMap *AddrMap;
27790b57cec5SDimitry Andric   std::vector<SectionRef> *Sections;
27800b57cec5SDimitry Andric   const char *class_name = nullptr;
27810b57cec5SDimitry Andric   const char *selector_name = nullptr;
27820b57cec5SDimitry Andric   std::unique_ptr<char[]> method = nullptr;
27830b57cec5SDimitry Andric   char *demangled_name = nullptr;
27840b57cec5SDimitry Andric   uint64_t adrp_addr = 0;
27850b57cec5SDimitry Andric   uint32_t adrp_inst = 0;
27860b57cec5SDimitry Andric   std::unique_ptr<SymbolAddressMap> bindtable;
27870b57cec5SDimitry Andric   uint32_t depth = 0;
27880b57cec5SDimitry Andric };
27895ffd83dbSDimitry Andric } // namespace
27900b57cec5SDimitry Andric 
27910b57cec5SDimitry Andric // SymbolizerGetOpInfo() is the operand information call back function.
27920b57cec5SDimitry Andric // This is called to get the symbolic information for operand(s) of an
27930b57cec5SDimitry Andric // instruction when it is being done.  This routine does this from
27940b57cec5SDimitry Andric // the relocation information, symbol table, etc. That block of information
27950b57cec5SDimitry Andric // is a pointer to the struct DisassembleInfo that was passed when the
27960b57cec5SDimitry Andric // disassembler context was created and passed to back to here when
27970b57cec5SDimitry Andric // called back by the disassembler for instruction operands that could have
27980b57cec5SDimitry Andric // relocation information. The address of the instruction containing operand is
27990b57cec5SDimitry Andric // at the Pc parameter.  The immediate value the operand has is passed in
28000b57cec5SDimitry Andric // op_info->Value and is at Offset past the start of the instruction and has a
28010b57cec5SDimitry Andric // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
28020b57cec5SDimitry Andric // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
28030b57cec5SDimitry Andric // names and addends of the symbolic expression to add for the operand.  The
28040b57cec5SDimitry Andric // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
28050b57cec5SDimitry Andric // information is returned then this function returns 1 else it returns 0.
SymbolizerGetOpInfo(void * DisInfo,uint64_t Pc,uint64_t Offset,uint64_t OpSize,uint64_t InstSize,int TagType,void * TagBuf)28060b57cec5SDimitry Andric static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
280781ad6265SDimitry Andric                                uint64_t OpSize, uint64_t InstSize, int TagType,
280881ad6265SDimitry Andric                                void *TagBuf) {
28090b57cec5SDimitry Andric   struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
28100b57cec5SDimitry Andric   struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
28110b57cec5SDimitry Andric   uint64_t value = op_info->Value;
28120b57cec5SDimitry Andric 
28130b57cec5SDimitry Andric   // Make sure all fields returned are zero if we don't set them.
28140b57cec5SDimitry Andric   memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
28150b57cec5SDimitry Andric   op_info->Value = value;
28160b57cec5SDimitry Andric 
28170b57cec5SDimitry Andric   // If the TagType is not the value 1 which it code knows about or if no
28180b57cec5SDimitry Andric   // verbose symbolic information is wanted then just return 0, indicating no
28190b57cec5SDimitry Andric   // information is being returned.
28200b57cec5SDimitry Andric   if (TagType != 1 || !info->verbose)
28210b57cec5SDimitry Andric     return 0;
28220b57cec5SDimitry Andric 
28230b57cec5SDimitry Andric   unsigned int Arch = info->O->getArch();
28240b57cec5SDimitry Andric   if (Arch == Triple::x86) {
282581ad6265SDimitry Andric     if (OpSize != 1 && OpSize != 2 && OpSize != 4 && OpSize != 0)
28260b57cec5SDimitry Andric       return 0;
28270b57cec5SDimitry Andric     if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
28280b57cec5SDimitry Andric       // TODO:
28290b57cec5SDimitry Andric       // Search the external relocation entries of a fully linked image
28300b57cec5SDimitry Andric       // (if any) for an entry that matches this segment offset.
28310b57cec5SDimitry Andric       // uint32_t seg_offset = (Pc + Offset);
28320b57cec5SDimitry Andric       return 0;
28330b57cec5SDimitry Andric     }
28340b57cec5SDimitry Andric     // In MH_OBJECT filetypes search the section's relocation entries (if any)
28350b57cec5SDimitry Andric     // for an entry for this section offset.
28360b57cec5SDimitry Andric     uint32_t sect_addr = info->S.getAddress();
28370b57cec5SDimitry Andric     uint32_t sect_offset = (Pc + Offset) - sect_addr;
28380b57cec5SDimitry Andric     bool reloc_found = false;
28390b57cec5SDimitry Andric     DataRefImpl Rel;
28400b57cec5SDimitry Andric     MachO::any_relocation_info RE;
28410b57cec5SDimitry Andric     bool isExtern = false;
28420b57cec5SDimitry Andric     SymbolRef Symbol;
28430b57cec5SDimitry Andric     bool r_scattered = false;
28440b57cec5SDimitry Andric     uint32_t r_value, pair_r_value, r_type;
28450b57cec5SDimitry Andric     for (const RelocationRef &Reloc : info->S.relocations()) {
28460b57cec5SDimitry Andric       uint64_t RelocOffset = Reloc.getOffset();
28470b57cec5SDimitry Andric       if (RelocOffset == sect_offset) {
28480b57cec5SDimitry Andric         Rel = Reloc.getRawDataRefImpl();
28490b57cec5SDimitry Andric         RE = info->O->getRelocation(Rel);
28500b57cec5SDimitry Andric         r_type = info->O->getAnyRelocationType(RE);
28510b57cec5SDimitry Andric         r_scattered = info->O->isRelocationScattered(RE);
28520b57cec5SDimitry Andric         if (r_scattered) {
28530b57cec5SDimitry Andric           r_value = info->O->getScatteredRelocationValue(RE);
28540b57cec5SDimitry Andric           if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
28550b57cec5SDimitry Andric               r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
28560b57cec5SDimitry Andric             DataRefImpl RelNext = Rel;
28570b57cec5SDimitry Andric             info->O->moveRelocationNext(RelNext);
28580b57cec5SDimitry Andric             MachO::any_relocation_info RENext;
28590b57cec5SDimitry Andric             RENext = info->O->getRelocation(RelNext);
28600b57cec5SDimitry Andric             if (info->O->isRelocationScattered(RENext))
28610b57cec5SDimitry Andric               pair_r_value = info->O->getScatteredRelocationValue(RENext);
28620b57cec5SDimitry Andric             else
28630b57cec5SDimitry Andric               return 0;
28640b57cec5SDimitry Andric           }
28650b57cec5SDimitry Andric         } else {
28660b57cec5SDimitry Andric           isExtern = info->O->getPlainRelocationExternal(RE);
28670b57cec5SDimitry Andric           if (isExtern) {
28680b57cec5SDimitry Andric             symbol_iterator RelocSym = Reloc.getSymbol();
28690b57cec5SDimitry Andric             Symbol = *RelocSym;
28700b57cec5SDimitry Andric           }
28710b57cec5SDimitry Andric         }
28720b57cec5SDimitry Andric         reloc_found = true;
28730b57cec5SDimitry Andric         break;
28740b57cec5SDimitry Andric       }
28750b57cec5SDimitry Andric     }
28760b57cec5SDimitry Andric     if (reloc_found && isExtern) {
28770b57cec5SDimitry Andric       op_info->AddSymbol.Present = 1;
28780b57cec5SDimitry Andric       op_info->AddSymbol.Name =
28790b57cec5SDimitry Andric           unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
28800b57cec5SDimitry Andric       // For i386 extern relocation entries the value in the instruction is
28810b57cec5SDimitry Andric       // the offset from the symbol, and value is already set in op_info->Value.
28820b57cec5SDimitry Andric       return 1;
28830b57cec5SDimitry Andric     }
28840b57cec5SDimitry Andric     if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
28850b57cec5SDimitry Andric                         r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
28860b57cec5SDimitry Andric       const char *add = GuessSymbolName(r_value, info->AddrMap);
28870b57cec5SDimitry Andric       const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
28880b57cec5SDimitry Andric       uint32_t offset = value - (r_value - pair_r_value);
28890b57cec5SDimitry Andric       op_info->AddSymbol.Present = 1;
28900b57cec5SDimitry Andric       if (add != nullptr)
28910b57cec5SDimitry Andric         op_info->AddSymbol.Name = add;
28920b57cec5SDimitry Andric       else
28930b57cec5SDimitry Andric         op_info->AddSymbol.Value = r_value;
28940b57cec5SDimitry Andric       op_info->SubtractSymbol.Present = 1;
28950b57cec5SDimitry Andric       if (sub != nullptr)
28960b57cec5SDimitry Andric         op_info->SubtractSymbol.Name = sub;
28970b57cec5SDimitry Andric       else
28980b57cec5SDimitry Andric         op_info->SubtractSymbol.Value = pair_r_value;
28990b57cec5SDimitry Andric       op_info->Value = offset;
29000b57cec5SDimitry Andric       return 1;
29010b57cec5SDimitry Andric     }
29020b57cec5SDimitry Andric     return 0;
29030b57cec5SDimitry Andric   }
29040b57cec5SDimitry Andric   if (Arch == Triple::x86_64) {
290581ad6265SDimitry Andric     if (OpSize != 1 && OpSize != 2 && OpSize != 4 && OpSize != 0)
29060b57cec5SDimitry Andric       return 0;
29070b57cec5SDimitry Andric     // For non MH_OBJECT types, like MH_KEXT_BUNDLE, Search the external
29080b57cec5SDimitry Andric     // relocation entries of a linked image (if any) for an entry that matches
29090b57cec5SDimitry Andric     // this segment offset.
29100b57cec5SDimitry Andric     if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
29110b57cec5SDimitry Andric       uint64_t seg_offset = Pc + Offset;
29120b57cec5SDimitry Andric       bool reloc_found = false;
29130b57cec5SDimitry Andric       DataRefImpl Rel;
29140b57cec5SDimitry Andric       MachO::any_relocation_info RE;
29150b57cec5SDimitry Andric       bool isExtern = false;
29160b57cec5SDimitry Andric       SymbolRef Symbol;
29170b57cec5SDimitry Andric       for (const RelocationRef &Reloc : info->O->external_relocations()) {
29180b57cec5SDimitry Andric         uint64_t RelocOffset = Reloc.getOffset();
29190b57cec5SDimitry Andric         if (RelocOffset == seg_offset) {
29200b57cec5SDimitry Andric           Rel = Reloc.getRawDataRefImpl();
29210b57cec5SDimitry Andric           RE = info->O->getRelocation(Rel);
29220b57cec5SDimitry Andric           // external relocation entries should always be external.
29230b57cec5SDimitry Andric           isExtern = info->O->getPlainRelocationExternal(RE);
29240b57cec5SDimitry Andric           if (isExtern) {
29250b57cec5SDimitry Andric             symbol_iterator RelocSym = Reloc.getSymbol();
29260b57cec5SDimitry Andric             Symbol = *RelocSym;
29270b57cec5SDimitry Andric           }
29280b57cec5SDimitry Andric           reloc_found = true;
29290b57cec5SDimitry Andric           break;
29300b57cec5SDimitry Andric         }
29310b57cec5SDimitry Andric       }
29320b57cec5SDimitry Andric       if (reloc_found && isExtern) {
29330b57cec5SDimitry Andric         // The Value passed in will be adjusted by the Pc if the instruction
29340b57cec5SDimitry Andric         // adds the Pc.  But for x86_64 external relocation entries the Value
29350b57cec5SDimitry Andric         // is the offset from the external symbol.
29360b57cec5SDimitry Andric         if (info->O->getAnyRelocationPCRel(RE))
293781ad6265SDimitry Andric           op_info->Value -= Pc + InstSize;
29380b57cec5SDimitry Andric         const char *name =
29390b57cec5SDimitry Andric             unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
29400b57cec5SDimitry Andric         op_info->AddSymbol.Present = 1;
29410b57cec5SDimitry Andric         op_info->AddSymbol.Name = name;
29420b57cec5SDimitry Andric         return 1;
29430b57cec5SDimitry Andric       }
29440b57cec5SDimitry Andric       return 0;
29450b57cec5SDimitry Andric     }
29460b57cec5SDimitry Andric     // In MH_OBJECT filetypes search the section's relocation entries (if any)
29470b57cec5SDimitry Andric     // for an entry for this section offset.
29480b57cec5SDimitry Andric     uint64_t sect_addr = info->S.getAddress();
29490b57cec5SDimitry Andric     uint64_t sect_offset = (Pc + Offset) - sect_addr;
29500b57cec5SDimitry Andric     bool reloc_found = false;
29510b57cec5SDimitry Andric     DataRefImpl Rel;
29520b57cec5SDimitry Andric     MachO::any_relocation_info RE;
29530b57cec5SDimitry Andric     bool isExtern = false;
29540b57cec5SDimitry Andric     SymbolRef Symbol;
29550b57cec5SDimitry Andric     for (const RelocationRef &Reloc : info->S.relocations()) {
29560b57cec5SDimitry Andric       uint64_t RelocOffset = Reloc.getOffset();
29570b57cec5SDimitry Andric       if (RelocOffset == sect_offset) {
29580b57cec5SDimitry Andric         Rel = Reloc.getRawDataRefImpl();
29590b57cec5SDimitry Andric         RE = info->O->getRelocation(Rel);
29600b57cec5SDimitry Andric         // NOTE: Scattered relocations don't exist on x86_64.
29610b57cec5SDimitry Andric         isExtern = info->O->getPlainRelocationExternal(RE);
29620b57cec5SDimitry Andric         if (isExtern) {
29630b57cec5SDimitry Andric           symbol_iterator RelocSym = Reloc.getSymbol();
29640b57cec5SDimitry Andric           Symbol = *RelocSym;
29650b57cec5SDimitry Andric         }
29660b57cec5SDimitry Andric         reloc_found = true;
29670b57cec5SDimitry Andric         break;
29680b57cec5SDimitry Andric       }
29690b57cec5SDimitry Andric     }
29700b57cec5SDimitry Andric     if (reloc_found && isExtern) {
29710b57cec5SDimitry Andric       // The Value passed in will be adjusted by the Pc if the instruction
29720b57cec5SDimitry Andric       // adds the Pc.  But for x86_64 external relocation entries the Value
29730b57cec5SDimitry Andric       // is the offset from the external symbol.
29740b57cec5SDimitry Andric       if (info->O->getAnyRelocationPCRel(RE))
297581ad6265SDimitry Andric         op_info->Value -= Pc + InstSize;
29760b57cec5SDimitry Andric       const char *name =
29770b57cec5SDimitry Andric           unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
29780b57cec5SDimitry Andric       unsigned Type = info->O->getAnyRelocationType(RE);
29790b57cec5SDimitry Andric       if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
29800b57cec5SDimitry Andric         DataRefImpl RelNext = Rel;
29810b57cec5SDimitry Andric         info->O->moveRelocationNext(RelNext);
29820b57cec5SDimitry Andric         MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
29830b57cec5SDimitry Andric         unsigned TypeNext = info->O->getAnyRelocationType(RENext);
29840b57cec5SDimitry Andric         bool isExternNext = info->O->getPlainRelocationExternal(RENext);
29850b57cec5SDimitry Andric         unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
29860b57cec5SDimitry Andric         if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
29870b57cec5SDimitry Andric           op_info->SubtractSymbol.Present = 1;
29880b57cec5SDimitry Andric           op_info->SubtractSymbol.Name = name;
29890b57cec5SDimitry Andric           symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
29900b57cec5SDimitry Andric           Symbol = *RelocSymNext;
29910b57cec5SDimitry Andric           name = unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
29920b57cec5SDimitry Andric         }
29930b57cec5SDimitry Andric       }
29940b57cec5SDimitry Andric       // TODO: add the VariantKinds to op_info->VariantKind for relocation types
29950b57cec5SDimitry Andric       // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
29960b57cec5SDimitry Andric       op_info->AddSymbol.Present = 1;
29970b57cec5SDimitry Andric       op_info->AddSymbol.Name = name;
29980b57cec5SDimitry Andric       return 1;
29990b57cec5SDimitry Andric     }
30000b57cec5SDimitry Andric     return 0;
30010b57cec5SDimitry Andric   }
30020b57cec5SDimitry Andric   if (Arch == Triple::arm) {
300381ad6265SDimitry Andric     if (Offset != 0 || (InstSize != 4 && InstSize != 2))
30040b57cec5SDimitry Andric       return 0;
30050b57cec5SDimitry Andric     if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
30060b57cec5SDimitry Andric       // TODO:
30070b57cec5SDimitry Andric       // Search the external relocation entries of a fully linked image
30080b57cec5SDimitry Andric       // (if any) for an entry that matches this segment offset.
30090b57cec5SDimitry Andric       // uint32_t seg_offset = (Pc + Offset);
30100b57cec5SDimitry Andric       return 0;
30110b57cec5SDimitry Andric     }
30120b57cec5SDimitry Andric     // In MH_OBJECT filetypes search the section's relocation entries (if any)
30130b57cec5SDimitry Andric     // for an entry for this section offset.
30140b57cec5SDimitry Andric     uint32_t sect_addr = info->S.getAddress();
30150b57cec5SDimitry Andric     uint32_t sect_offset = (Pc + Offset) - sect_addr;
30160b57cec5SDimitry Andric     DataRefImpl Rel;
30170b57cec5SDimitry Andric     MachO::any_relocation_info RE;
30180b57cec5SDimitry Andric     bool isExtern = false;
30190b57cec5SDimitry Andric     SymbolRef Symbol;
30200b57cec5SDimitry Andric     bool r_scattered = false;
30210b57cec5SDimitry Andric     uint32_t r_value, pair_r_value, r_type, r_length, other_half;
30220b57cec5SDimitry Andric     auto Reloc =
30230b57cec5SDimitry Andric         find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
30240b57cec5SDimitry Andric           uint64_t RelocOffset = Reloc.getOffset();
30250b57cec5SDimitry Andric           return RelocOffset == sect_offset;
30260b57cec5SDimitry Andric         });
30270b57cec5SDimitry Andric 
30280b57cec5SDimitry Andric     if (Reloc == info->S.relocations().end())
30290b57cec5SDimitry Andric       return 0;
30300b57cec5SDimitry Andric 
30310b57cec5SDimitry Andric     Rel = Reloc->getRawDataRefImpl();
30320b57cec5SDimitry Andric     RE = info->O->getRelocation(Rel);
30330b57cec5SDimitry Andric     r_length = info->O->getAnyRelocationLength(RE);
30340b57cec5SDimitry Andric     r_scattered = info->O->isRelocationScattered(RE);
30350b57cec5SDimitry Andric     if (r_scattered) {
30360b57cec5SDimitry Andric       r_value = info->O->getScatteredRelocationValue(RE);
30370b57cec5SDimitry Andric       r_type = info->O->getScatteredRelocationType(RE);
30380b57cec5SDimitry Andric     } else {
30390b57cec5SDimitry Andric       r_type = info->O->getAnyRelocationType(RE);
30400b57cec5SDimitry Andric       isExtern = info->O->getPlainRelocationExternal(RE);
30410b57cec5SDimitry Andric       if (isExtern) {
30420b57cec5SDimitry Andric         symbol_iterator RelocSym = Reloc->getSymbol();
30430b57cec5SDimitry Andric         Symbol = *RelocSym;
30440b57cec5SDimitry Andric       }
30450b57cec5SDimitry Andric     }
30460b57cec5SDimitry Andric     if (r_type == MachO::ARM_RELOC_HALF ||
30470b57cec5SDimitry Andric         r_type == MachO::ARM_RELOC_SECTDIFF ||
30480b57cec5SDimitry Andric         r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
30490b57cec5SDimitry Andric         r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
30500b57cec5SDimitry Andric       DataRefImpl RelNext = Rel;
30510b57cec5SDimitry Andric       info->O->moveRelocationNext(RelNext);
30520b57cec5SDimitry Andric       MachO::any_relocation_info RENext;
30530b57cec5SDimitry Andric       RENext = info->O->getRelocation(RelNext);
30540b57cec5SDimitry Andric       other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
30550b57cec5SDimitry Andric       if (info->O->isRelocationScattered(RENext))
30560b57cec5SDimitry Andric         pair_r_value = info->O->getScatteredRelocationValue(RENext);
30570b57cec5SDimitry Andric     }
30580b57cec5SDimitry Andric 
30590b57cec5SDimitry Andric     if (isExtern) {
30600b57cec5SDimitry Andric       const char *name =
30610b57cec5SDimitry Andric           unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
30620b57cec5SDimitry Andric       op_info->AddSymbol.Present = 1;
30630b57cec5SDimitry Andric       op_info->AddSymbol.Name = name;
30640b57cec5SDimitry Andric       switch (r_type) {
30650b57cec5SDimitry Andric       case MachO::ARM_RELOC_HALF:
30660b57cec5SDimitry Andric         if ((r_length & 0x1) == 1) {
30670b57cec5SDimitry Andric           op_info->Value = value << 16 | other_half;
30680b57cec5SDimitry Andric           op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
30690b57cec5SDimitry Andric         } else {
30700b57cec5SDimitry Andric           op_info->Value = other_half << 16 | value;
30710b57cec5SDimitry Andric           op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
30720b57cec5SDimitry Andric         }
30730b57cec5SDimitry Andric         break;
30740b57cec5SDimitry Andric       default:
30750b57cec5SDimitry Andric         break;
30760b57cec5SDimitry Andric       }
30770b57cec5SDimitry Andric       return 1;
30780b57cec5SDimitry Andric     }
30790b57cec5SDimitry Andric     // If we have a branch that is not an external relocation entry then
30800b57cec5SDimitry Andric     // return 0 so the code in tryAddingSymbolicOperand() can use the
30810b57cec5SDimitry Andric     // SymbolLookUp call back with the branch target address to look up the
30820b57cec5SDimitry Andric     // symbol and possibility add an annotation for a symbol stub.
30830b57cec5SDimitry Andric     if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
30840b57cec5SDimitry Andric                           r_type == MachO::ARM_THUMB_RELOC_BR22))
30850b57cec5SDimitry Andric       return 0;
30860b57cec5SDimitry Andric 
30870b57cec5SDimitry Andric     uint32_t offset = 0;
30880b57cec5SDimitry Andric     if (r_type == MachO::ARM_RELOC_HALF ||
30890b57cec5SDimitry Andric         r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
30900b57cec5SDimitry Andric       if ((r_length & 0x1) == 1)
30910b57cec5SDimitry Andric         value = value << 16 | other_half;
30920b57cec5SDimitry Andric       else
30930b57cec5SDimitry Andric         value = other_half << 16 | value;
30940b57cec5SDimitry Andric     }
30950b57cec5SDimitry Andric     if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
30960b57cec5SDimitry Andric                         r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
30970b57cec5SDimitry Andric       offset = value - r_value;
30980b57cec5SDimitry Andric       value = r_value;
30990b57cec5SDimitry Andric     }
31000b57cec5SDimitry Andric 
31010b57cec5SDimitry Andric     if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
31020b57cec5SDimitry Andric       if ((r_length & 0x1) == 1)
31030b57cec5SDimitry Andric         op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
31040b57cec5SDimitry Andric       else
31050b57cec5SDimitry Andric         op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
31060b57cec5SDimitry Andric       const char *add = GuessSymbolName(r_value, info->AddrMap);
31070b57cec5SDimitry Andric       const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
31080b57cec5SDimitry Andric       int32_t offset = value - (r_value - pair_r_value);
31090b57cec5SDimitry Andric       op_info->AddSymbol.Present = 1;
31100b57cec5SDimitry Andric       if (add != nullptr)
31110b57cec5SDimitry Andric         op_info->AddSymbol.Name = add;
31120b57cec5SDimitry Andric       else
31130b57cec5SDimitry Andric         op_info->AddSymbol.Value = r_value;
31140b57cec5SDimitry Andric       op_info->SubtractSymbol.Present = 1;
31150b57cec5SDimitry Andric       if (sub != nullptr)
31160b57cec5SDimitry Andric         op_info->SubtractSymbol.Name = sub;
31170b57cec5SDimitry Andric       else
31180b57cec5SDimitry Andric         op_info->SubtractSymbol.Value = pair_r_value;
31190b57cec5SDimitry Andric       op_info->Value = offset;
31200b57cec5SDimitry Andric       return 1;
31210b57cec5SDimitry Andric     }
31220b57cec5SDimitry Andric 
31230b57cec5SDimitry Andric     op_info->AddSymbol.Present = 1;
31240b57cec5SDimitry Andric     op_info->Value = offset;
31250b57cec5SDimitry Andric     if (r_type == MachO::ARM_RELOC_HALF) {
31260b57cec5SDimitry Andric       if ((r_length & 0x1) == 1)
31270b57cec5SDimitry Andric         op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
31280b57cec5SDimitry Andric       else
31290b57cec5SDimitry Andric         op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
31300b57cec5SDimitry Andric     }
31310b57cec5SDimitry Andric     const char *add = GuessSymbolName(value, info->AddrMap);
31320b57cec5SDimitry Andric     if (add != nullptr) {
31330b57cec5SDimitry Andric       op_info->AddSymbol.Name = add;
31340b57cec5SDimitry Andric       return 1;
31350b57cec5SDimitry Andric     }
31360b57cec5SDimitry Andric     op_info->AddSymbol.Value = value;
31370b57cec5SDimitry Andric     return 1;
31380b57cec5SDimitry Andric   }
31390b57cec5SDimitry Andric   if (Arch == Triple::aarch64) {
314081ad6265SDimitry Andric     if (Offset != 0 || InstSize != 4)
31410b57cec5SDimitry Andric       return 0;
31420b57cec5SDimitry Andric     if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
31430b57cec5SDimitry Andric       // TODO:
31440b57cec5SDimitry Andric       // Search the external relocation entries of a fully linked image
31450b57cec5SDimitry Andric       // (if any) for an entry that matches this segment offset.
31460b57cec5SDimitry Andric       // uint64_t seg_offset = (Pc + Offset);
31470b57cec5SDimitry Andric       return 0;
31480b57cec5SDimitry Andric     }
31490b57cec5SDimitry Andric     // In MH_OBJECT filetypes search the section's relocation entries (if any)
31500b57cec5SDimitry Andric     // for an entry for this section offset.
31510b57cec5SDimitry Andric     uint64_t sect_addr = info->S.getAddress();
31520b57cec5SDimitry Andric     uint64_t sect_offset = (Pc + Offset) - sect_addr;
31530b57cec5SDimitry Andric     auto Reloc =
31540b57cec5SDimitry Andric         find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
31550b57cec5SDimitry Andric           uint64_t RelocOffset = Reloc.getOffset();
31560b57cec5SDimitry Andric           return RelocOffset == sect_offset;
31570b57cec5SDimitry Andric         });
31580b57cec5SDimitry Andric 
31590b57cec5SDimitry Andric     if (Reloc == info->S.relocations().end())
31600b57cec5SDimitry Andric       return 0;
31610b57cec5SDimitry Andric 
31620b57cec5SDimitry Andric     DataRefImpl Rel = Reloc->getRawDataRefImpl();
31630b57cec5SDimitry Andric     MachO::any_relocation_info RE = info->O->getRelocation(Rel);
31640b57cec5SDimitry Andric     uint32_t r_type = info->O->getAnyRelocationType(RE);
31650b57cec5SDimitry Andric     if (r_type == MachO::ARM64_RELOC_ADDEND) {
31660b57cec5SDimitry Andric       DataRefImpl RelNext = Rel;
31670b57cec5SDimitry Andric       info->O->moveRelocationNext(RelNext);
31680b57cec5SDimitry Andric       MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
31690b57cec5SDimitry Andric       if (value == 0) {
31700b57cec5SDimitry Andric         value = info->O->getPlainRelocationSymbolNum(RENext);
31710b57cec5SDimitry Andric         op_info->Value = value;
31720b57cec5SDimitry Andric       }
31730b57cec5SDimitry Andric     }
31740b57cec5SDimitry Andric     // NOTE: Scattered relocations don't exist on arm64.
31750b57cec5SDimitry Andric     if (!info->O->getPlainRelocationExternal(RE))
31760b57cec5SDimitry Andric       return 0;
31770b57cec5SDimitry Andric     const char *name =
31780b57cec5SDimitry Andric         unwrapOrError(Reloc->getSymbol()->getName(), info->O->getFileName())
31790b57cec5SDimitry Andric             .data();
31800b57cec5SDimitry Andric     op_info->AddSymbol.Present = 1;
31810b57cec5SDimitry Andric     op_info->AddSymbol.Name = name;
31820b57cec5SDimitry Andric 
31830b57cec5SDimitry Andric     switch (r_type) {
31840b57cec5SDimitry Andric     case MachO::ARM64_RELOC_PAGE21:
31850b57cec5SDimitry Andric       /* @page */
31860b57cec5SDimitry Andric       op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
31870b57cec5SDimitry Andric       break;
31880b57cec5SDimitry Andric     case MachO::ARM64_RELOC_PAGEOFF12:
31890b57cec5SDimitry Andric       /* @pageoff */
31900b57cec5SDimitry Andric       op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
31910b57cec5SDimitry Andric       break;
31920b57cec5SDimitry Andric     case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
31930b57cec5SDimitry Andric       /* @gotpage */
31940b57cec5SDimitry Andric       op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
31950b57cec5SDimitry Andric       break;
31960b57cec5SDimitry Andric     case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
31970b57cec5SDimitry Andric       /* @gotpageoff */
31980b57cec5SDimitry Andric       op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
31990b57cec5SDimitry Andric       break;
32000b57cec5SDimitry Andric     case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
32010b57cec5SDimitry Andric       /* @tvlppage is not implemented in llvm-mc */
32020b57cec5SDimitry Andric       op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
32030b57cec5SDimitry Andric       break;
32040b57cec5SDimitry Andric     case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
32050b57cec5SDimitry Andric       /* @tvlppageoff is not implemented in llvm-mc */
32060b57cec5SDimitry Andric       op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
32070b57cec5SDimitry Andric       break;
32080b57cec5SDimitry Andric     default:
32090b57cec5SDimitry Andric     case MachO::ARM64_RELOC_BRANCH26:
32100b57cec5SDimitry Andric       op_info->VariantKind = LLVMDisassembler_VariantKind_None;
32110b57cec5SDimitry Andric       break;
32120b57cec5SDimitry Andric     }
32130b57cec5SDimitry Andric     return 1;
32140b57cec5SDimitry Andric   }
32150b57cec5SDimitry Andric   return 0;
32160b57cec5SDimitry Andric }
32170b57cec5SDimitry Andric 
32180b57cec5SDimitry Andric // GuessCstringPointer is passed the address of what might be a pointer to a
32190b57cec5SDimitry Andric // literal string in a cstring section.  If that address is in a cstring section
32200b57cec5SDimitry Andric // it returns a pointer to that string.  Else it returns nullptr.
GuessCstringPointer(uint64_t ReferenceValue,struct DisassembleInfo * info)32210b57cec5SDimitry Andric static const char *GuessCstringPointer(uint64_t ReferenceValue,
32220b57cec5SDimitry Andric                                        struct DisassembleInfo *info) {
32230b57cec5SDimitry Andric   for (const auto &Load : info->O->load_commands()) {
32240b57cec5SDimitry Andric     if (Load.C.cmd == MachO::LC_SEGMENT_64) {
32250b57cec5SDimitry Andric       MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
32260b57cec5SDimitry Andric       for (unsigned J = 0; J < Seg.nsects; ++J) {
32270b57cec5SDimitry Andric         MachO::section_64 Sec = info->O->getSection64(Load, J);
32280b57cec5SDimitry Andric         uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
32290b57cec5SDimitry Andric         if (section_type == MachO::S_CSTRING_LITERALS &&
32300b57cec5SDimitry Andric             ReferenceValue >= Sec.addr &&
32310b57cec5SDimitry Andric             ReferenceValue < Sec.addr + Sec.size) {
32320b57cec5SDimitry Andric           uint64_t sect_offset = ReferenceValue - Sec.addr;
32330b57cec5SDimitry Andric           uint64_t object_offset = Sec.offset + sect_offset;
32340b57cec5SDimitry Andric           StringRef MachOContents = info->O->getData();
32350b57cec5SDimitry Andric           uint64_t object_size = MachOContents.size();
32360b57cec5SDimitry Andric           const char *object_addr = (const char *)MachOContents.data();
32370b57cec5SDimitry Andric           if (object_offset < object_size) {
32380b57cec5SDimitry Andric             const char *name = object_addr + object_offset;
32390b57cec5SDimitry Andric             return name;
32400b57cec5SDimitry Andric           } else {
32410b57cec5SDimitry Andric             return nullptr;
32420b57cec5SDimitry Andric           }
32430b57cec5SDimitry Andric         }
32440b57cec5SDimitry Andric       }
32450b57cec5SDimitry Andric     } else if (Load.C.cmd == MachO::LC_SEGMENT) {
32460b57cec5SDimitry Andric       MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
32470b57cec5SDimitry Andric       for (unsigned J = 0; J < Seg.nsects; ++J) {
32480b57cec5SDimitry Andric         MachO::section Sec = info->O->getSection(Load, J);
32490b57cec5SDimitry Andric         uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
32500b57cec5SDimitry Andric         if (section_type == MachO::S_CSTRING_LITERALS &&
32510b57cec5SDimitry Andric             ReferenceValue >= Sec.addr &&
32520b57cec5SDimitry Andric             ReferenceValue < Sec.addr + Sec.size) {
32530b57cec5SDimitry Andric           uint64_t sect_offset = ReferenceValue - Sec.addr;
32540b57cec5SDimitry Andric           uint64_t object_offset = Sec.offset + sect_offset;
32550b57cec5SDimitry Andric           StringRef MachOContents = info->O->getData();
32560b57cec5SDimitry Andric           uint64_t object_size = MachOContents.size();
32570b57cec5SDimitry Andric           const char *object_addr = (const char *)MachOContents.data();
32580b57cec5SDimitry Andric           if (object_offset < object_size) {
32590b57cec5SDimitry Andric             const char *name = object_addr + object_offset;
32600b57cec5SDimitry Andric             return name;
32610b57cec5SDimitry Andric           } else {
32620b57cec5SDimitry Andric             return nullptr;
32630b57cec5SDimitry Andric           }
32640b57cec5SDimitry Andric         }
32650b57cec5SDimitry Andric       }
32660b57cec5SDimitry Andric     }
32670b57cec5SDimitry Andric   }
32680b57cec5SDimitry Andric   return nullptr;
32690b57cec5SDimitry Andric }
32700b57cec5SDimitry Andric 
32710b57cec5SDimitry Andric // GuessIndirectSymbol returns the name of the indirect symbol for the
32720b57cec5SDimitry Andric // ReferenceValue passed in or nullptr.  This is used when ReferenceValue maybe
32730b57cec5SDimitry Andric // an address of a symbol stub or a lazy or non-lazy pointer to associate the
32740b57cec5SDimitry Andric // symbol name being referenced by the stub or pointer.
GuessIndirectSymbol(uint64_t ReferenceValue,struct DisassembleInfo * info)32750b57cec5SDimitry Andric static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
32760b57cec5SDimitry Andric                                        struct DisassembleInfo *info) {
32770b57cec5SDimitry Andric   MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
32780b57cec5SDimitry Andric   MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
32790b57cec5SDimitry Andric   for (const auto &Load : info->O->load_commands()) {
32800b57cec5SDimitry Andric     if (Load.C.cmd == MachO::LC_SEGMENT_64) {
32810b57cec5SDimitry Andric       MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
32820b57cec5SDimitry Andric       for (unsigned J = 0; J < Seg.nsects; ++J) {
32830b57cec5SDimitry Andric         MachO::section_64 Sec = info->O->getSection64(Load, J);
32840b57cec5SDimitry Andric         uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
32850b57cec5SDimitry Andric         if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
32860b57cec5SDimitry Andric              section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
32870b57cec5SDimitry Andric              section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
32880b57cec5SDimitry Andric              section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
32890b57cec5SDimitry Andric              section_type == MachO::S_SYMBOL_STUBS) &&
32900b57cec5SDimitry Andric             ReferenceValue >= Sec.addr &&
32910b57cec5SDimitry Andric             ReferenceValue < Sec.addr + Sec.size) {
32920b57cec5SDimitry Andric           uint32_t stride;
32930b57cec5SDimitry Andric           if (section_type == MachO::S_SYMBOL_STUBS)
32940b57cec5SDimitry Andric             stride = Sec.reserved2;
32950b57cec5SDimitry Andric           else
32960b57cec5SDimitry Andric             stride = 8;
32970b57cec5SDimitry Andric           if (stride == 0)
32980b57cec5SDimitry Andric             return nullptr;
32990b57cec5SDimitry Andric           uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
33000b57cec5SDimitry Andric           if (index < Dysymtab.nindirectsyms) {
33010b57cec5SDimitry Andric             uint32_t indirect_symbol =
33020b57cec5SDimitry Andric                 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
33030b57cec5SDimitry Andric             if (indirect_symbol < Symtab.nsyms) {
33040b57cec5SDimitry Andric               symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
33050b57cec5SDimitry Andric               return unwrapOrError(Sym->getName(), info->O->getFileName())
33060b57cec5SDimitry Andric                   .data();
33070b57cec5SDimitry Andric             }
33080b57cec5SDimitry Andric           }
33090b57cec5SDimitry Andric         }
33100b57cec5SDimitry Andric       }
33110b57cec5SDimitry Andric     } else if (Load.C.cmd == MachO::LC_SEGMENT) {
33120b57cec5SDimitry Andric       MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
33130b57cec5SDimitry Andric       for (unsigned J = 0; J < Seg.nsects; ++J) {
33140b57cec5SDimitry Andric         MachO::section Sec = info->O->getSection(Load, J);
33150b57cec5SDimitry Andric         uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
33160b57cec5SDimitry Andric         if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
33170b57cec5SDimitry Andric              section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
33180b57cec5SDimitry Andric              section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
33190b57cec5SDimitry Andric              section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
33200b57cec5SDimitry Andric              section_type == MachO::S_SYMBOL_STUBS) &&
33210b57cec5SDimitry Andric             ReferenceValue >= Sec.addr &&
33220b57cec5SDimitry Andric             ReferenceValue < Sec.addr + Sec.size) {
33230b57cec5SDimitry Andric           uint32_t stride;
33240b57cec5SDimitry Andric           if (section_type == MachO::S_SYMBOL_STUBS)
33250b57cec5SDimitry Andric             stride = Sec.reserved2;
33260b57cec5SDimitry Andric           else
33270b57cec5SDimitry Andric             stride = 4;
33280b57cec5SDimitry Andric           if (stride == 0)
33290b57cec5SDimitry Andric             return nullptr;
33300b57cec5SDimitry Andric           uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
33310b57cec5SDimitry Andric           if (index < Dysymtab.nindirectsyms) {
33320b57cec5SDimitry Andric             uint32_t indirect_symbol =
33330b57cec5SDimitry Andric                 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
33340b57cec5SDimitry Andric             if (indirect_symbol < Symtab.nsyms) {
33350b57cec5SDimitry Andric               symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
33360b57cec5SDimitry Andric               return unwrapOrError(Sym->getName(), info->O->getFileName())
33370b57cec5SDimitry Andric                   .data();
33380b57cec5SDimitry Andric             }
33390b57cec5SDimitry Andric           }
33400b57cec5SDimitry Andric         }
33410b57cec5SDimitry Andric       }
33420b57cec5SDimitry Andric     }
33430b57cec5SDimitry Andric   }
33440b57cec5SDimitry Andric   return nullptr;
33450b57cec5SDimitry Andric }
33460b57cec5SDimitry Andric 
33470b57cec5SDimitry Andric // method_reference() is called passing it the ReferenceName that might be
33480b57cec5SDimitry Andric // a reference it to an Objective-C method call.  If so then it allocates and
33490b57cec5SDimitry Andric // assembles a method call string with the values last seen and saved in
33500b57cec5SDimitry Andric // the DisassembleInfo's class_name and selector_name fields.  This is saved
33510b57cec5SDimitry Andric // into the method field of the info and any previous string is free'ed.
33520b57cec5SDimitry Andric // Then the class_name field in the info is set to nullptr.  The method call
33530b57cec5SDimitry Andric // string is set into ReferenceName and ReferenceType is set to
33540b57cec5SDimitry Andric // LLVMDisassembler_ReferenceType_Out_Objc_Message.  If this not a method call
33550b57cec5SDimitry Andric // then both ReferenceType and ReferenceName are left unchanged.
method_reference(struct DisassembleInfo * info,uint64_t * ReferenceType,const char ** ReferenceName)33560b57cec5SDimitry Andric static void method_reference(struct DisassembleInfo *info,
33570b57cec5SDimitry Andric                              uint64_t *ReferenceType,
33580b57cec5SDimitry Andric                              const char **ReferenceName) {
33590b57cec5SDimitry Andric   unsigned int Arch = info->O->getArch();
33600b57cec5SDimitry Andric   if (*ReferenceName != nullptr) {
33610b57cec5SDimitry Andric     if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
33620b57cec5SDimitry Andric       if (info->selector_name != nullptr) {
33630b57cec5SDimitry Andric         if (info->class_name != nullptr) {
33648bcb0991SDimitry Andric           info->method = std::make_unique<char[]>(
33650b57cec5SDimitry Andric               5 + strlen(info->class_name) + strlen(info->selector_name));
33660b57cec5SDimitry Andric           char *method = info->method.get();
33670b57cec5SDimitry Andric           if (method != nullptr) {
33680b57cec5SDimitry Andric             strcpy(method, "+[");
33690b57cec5SDimitry Andric             strcat(method, info->class_name);
33700b57cec5SDimitry Andric             strcat(method, " ");
33710b57cec5SDimitry Andric             strcat(method, info->selector_name);
33720b57cec5SDimitry Andric             strcat(method, "]");
33730b57cec5SDimitry Andric             *ReferenceName = method;
33740b57cec5SDimitry Andric             *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
33750b57cec5SDimitry Andric           }
33760b57cec5SDimitry Andric         } else {
33770b57cec5SDimitry Andric           info->method =
33788bcb0991SDimitry Andric               std::make_unique<char[]>(9 + strlen(info->selector_name));
33790b57cec5SDimitry Andric           char *method = info->method.get();
33800b57cec5SDimitry Andric           if (method != nullptr) {
33810b57cec5SDimitry Andric             if (Arch == Triple::x86_64)
33820b57cec5SDimitry Andric               strcpy(method, "-[%rdi ");
33830b57cec5SDimitry Andric             else if (Arch == Triple::aarch64)
33840b57cec5SDimitry Andric               strcpy(method, "-[x0 ");
33850b57cec5SDimitry Andric             else
33860b57cec5SDimitry Andric               strcpy(method, "-[r? ");
33870b57cec5SDimitry Andric             strcat(method, info->selector_name);
33880b57cec5SDimitry Andric             strcat(method, "]");
33890b57cec5SDimitry Andric             *ReferenceName = method;
33900b57cec5SDimitry Andric             *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
33910b57cec5SDimitry Andric           }
33920b57cec5SDimitry Andric         }
33930b57cec5SDimitry Andric         info->class_name = nullptr;
33940b57cec5SDimitry Andric       }
33950b57cec5SDimitry Andric     } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
33960b57cec5SDimitry Andric       if (info->selector_name != nullptr) {
33970b57cec5SDimitry Andric         info->method =
33988bcb0991SDimitry Andric             std::make_unique<char[]>(17 + strlen(info->selector_name));
33990b57cec5SDimitry Andric         char *method = info->method.get();
34000b57cec5SDimitry Andric         if (method != nullptr) {
34010b57cec5SDimitry Andric           if (Arch == Triple::x86_64)
34020b57cec5SDimitry Andric             strcpy(method, "-[[%rdi super] ");
34030b57cec5SDimitry Andric           else if (Arch == Triple::aarch64)
34040b57cec5SDimitry Andric             strcpy(method, "-[[x0 super] ");
34050b57cec5SDimitry Andric           else
34060b57cec5SDimitry Andric             strcpy(method, "-[[r? super] ");
34070b57cec5SDimitry Andric           strcat(method, info->selector_name);
34080b57cec5SDimitry Andric           strcat(method, "]");
34090b57cec5SDimitry Andric           *ReferenceName = method;
34100b57cec5SDimitry Andric           *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
34110b57cec5SDimitry Andric         }
34120b57cec5SDimitry Andric         info->class_name = nullptr;
34130b57cec5SDimitry Andric       }
34140b57cec5SDimitry Andric     }
34150b57cec5SDimitry Andric   }
34160b57cec5SDimitry Andric }
34170b57cec5SDimitry Andric 
34180b57cec5SDimitry Andric // GuessPointerPointer() is passed the address of what might be a pointer to
34190b57cec5SDimitry Andric // a reference to an Objective-C class, selector, message ref or cfstring.
34200b57cec5SDimitry Andric // If so the value of the pointer is returned and one of the booleans are set
34210b57cec5SDimitry Andric // to true.  If not zero is returned and all the booleans are set to false.
GuessPointerPointer(uint64_t ReferenceValue,struct DisassembleInfo * info,bool & classref,bool & selref,bool & msgref,bool & cfstring)34220b57cec5SDimitry Andric static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
34230b57cec5SDimitry Andric                                     struct DisassembleInfo *info,
34240b57cec5SDimitry Andric                                     bool &classref, bool &selref, bool &msgref,
34250b57cec5SDimitry Andric                                     bool &cfstring) {
34260b57cec5SDimitry Andric   classref = false;
34270b57cec5SDimitry Andric   selref = false;
34280b57cec5SDimitry Andric   msgref = false;
34290b57cec5SDimitry Andric   cfstring = false;
34300b57cec5SDimitry Andric   for (const auto &Load : info->O->load_commands()) {
34310b57cec5SDimitry Andric     if (Load.C.cmd == MachO::LC_SEGMENT_64) {
34320b57cec5SDimitry Andric       MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
34330b57cec5SDimitry Andric       for (unsigned J = 0; J < Seg.nsects; ++J) {
34340b57cec5SDimitry Andric         MachO::section_64 Sec = info->O->getSection64(Load, J);
34350b57cec5SDimitry Andric         if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
34360b57cec5SDimitry Andric              strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
34370b57cec5SDimitry Andric              strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
34380b57cec5SDimitry Andric              strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
34390b57cec5SDimitry Andric              strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
34400b57cec5SDimitry Andric             ReferenceValue >= Sec.addr &&
34410b57cec5SDimitry Andric             ReferenceValue < Sec.addr + Sec.size) {
34420b57cec5SDimitry Andric           uint64_t sect_offset = ReferenceValue - Sec.addr;
34430b57cec5SDimitry Andric           uint64_t object_offset = Sec.offset + sect_offset;
34440b57cec5SDimitry Andric           StringRef MachOContents = info->O->getData();
34450b57cec5SDimitry Andric           uint64_t object_size = MachOContents.size();
34460b57cec5SDimitry Andric           const char *object_addr = (const char *)MachOContents.data();
34470b57cec5SDimitry Andric           if (object_offset < object_size) {
34480b57cec5SDimitry Andric             uint64_t pointer_value;
34490b57cec5SDimitry Andric             memcpy(&pointer_value, object_addr + object_offset,
34500b57cec5SDimitry Andric                    sizeof(uint64_t));
34510b57cec5SDimitry Andric             if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
34520b57cec5SDimitry Andric               sys::swapByteOrder(pointer_value);
34530b57cec5SDimitry Andric             if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
34540b57cec5SDimitry Andric               selref = true;
34550b57cec5SDimitry Andric             else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
34560b57cec5SDimitry Andric                      strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
34570b57cec5SDimitry Andric               classref = true;
34580b57cec5SDimitry Andric             else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
34590b57cec5SDimitry Andric                      ReferenceValue + 8 < Sec.addr + Sec.size) {
34600b57cec5SDimitry Andric               msgref = true;
34610b57cec5SDimitry Andric               memcpy(&pointer_value, object_addr + object_offset + 8,
34620b57cec5SDimitry Andric                      sizeof(uint64_t));
34630b57cec5SDimitry Andric               if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
34640b57cec5SDimitry Andric                 sys::swapByteOrder(pointer_value);
34650b57cec5SDimitry Andric             } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
34660b57cec5SDimitry Andric               cfstring = true;
34670b57cec5SDimitry Andric             return pointer_value;
34680b57cec5SDimitry Andric           } else {
34690b57cec5SDimitry Andric             return 0;
34700b57cec5SDimitry Andric           }
34710b57cec5SDimitry Andric         }
34720b57cec5SDimitry Andric       }
34730b57cec5SDimitry Andric     }
34740b57cec5SDimitry Andric     // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
34750b57cec5SDimitry Andric   }
34760b57cec5SDimitry Andric   return 0;
34770b57cec5SDimitry Andric }
34780b57cec5SDimitry Andric 
34790b57cec5SDimitry Andric // get_pointer_64 returns a pointer to the bytes in the object file at the
34800b57cec5SDimitry Andric // Address from a section in the Mach-O file.  And indirectly returns the
34810b57cec5SDimitry Andric // offset into the section, number of bytes left in the section past the offset
34820b57cec5SDimitry Andric // and which section is was being referenced.  If the Address is not in a
34830b57cec5SDimitry Andric // section nullptr is returned.
get_pointer_64(uint64_t Address,uint32_t & offset,uint32_t & left,SectionRef & S,DisassembleInfo * info,bool objc_only=false)34840b57cec5SDimitry Andric static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
34850b57cec5SDimitry Andric                                   uint32_t &left, SectionRef &S,
34860b57cec5SDimitry Andric                                   DisassembleInfo *info,
34870b57cec5SDimitry Andric                                   bool objc_only = false) {
34880b57cec5SDimitry Andric   offset = 0;
34890b57cec5SDimitry Andric   left = 0;
34900b57cec5SDimitry Andric   S = SectionRef();
34910b57cec5SDimitry Andric   for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
34920b57cec5SDimitry Andric     uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
34930b57cec5SDimitry Andric     uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
34940b57cec5SDimitry Andric     if (SectSize == 0)
34950b57cec5SDimitry Andric       continue;
34960b57cec5SDimitry Andric     if (objc_only) {
34970b57cec5SDimitry Andric       StringRef SectName;
34988bcb0991SDimitry Andric       Expected<StringRef> SecNameOrErr =
34998bcb0991SDimitry Andric           ((*(info->Sections))[SectIdx]).getName();
35008bcb0991SDimitry Andric       if (SecNameOrErr)
35018bcb0991SDimitry Andric         SectName = *SecNameOrErr;
35028bcb0991SDimitry Andric       else
35038bcb0991SDimitry Andric         consumeError(SecNameOrErr.takeError());
35048bcb0991SDimitry Andric 
35050b57cec5SDimitry Andric       DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
35060b57cec5SDimitry Andric       StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
35070b57cec5SDimitry Andric       if (SegName != "__OBJC" && SectName != "__cstring")
35080b57cec5SDimitry Andric         continue;
35090b57cec5SDimitry Andric     }
35100b57cec5SDimitry Andric     if (Address >= SectAddress && Address < SectAddress + SectSize) {
35110b57cec5SDimitry Andric       S = (*(info->Sections))[SectIdx];
35120b57cec5SDimitry Andric       offset = Address - SectAddress;
35130b57cec5SDimitry Andric       left = SectSize - offset;
35140b57cec5SDimitry Andric       StringRef SectContents = unwrapOrError(
35150b57cec5SDimitry Andric           ((*(info->Sections))[SectIdx]).getContents(), info->O->getFileName());
35160b57cec5SDimitry Andric       return SectContents.data() + offset;
35170b57cec5SDimitry Andric     }
35180b57cec5SDimitry Andric   }
35190b57cec5SDimitry Andric   return nullptr;
35200b57cec5SDimitry Andric }
35210b57cec5SDimitry Andric 
get_pointer_32(uint32_t Address,uint32_t & offset,uint32_t & left,SectionRef & S,DisassembleInfo * info,bool objc_only=false)35220b57cec5SDimitry Andric static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
35230b57cec5SDimitry Andric                                   uint32_t &left, SectionRef &S,
35240b57cec5SDimitry Andric                                   DisassembleInfo *info,
35250b57cec5SDimitry Andric                                   bool objc_only = false) {
35260b57cec5SDimitry Andric   return get_pointer_64(Address, offset, left, S, info, objc_only);
35270b57cec5SDimitry Andric }
35280b57cec5SDimitry Andric 
35290b57cec5SDimitry Andric // get_symbol_64() returns the name of a symbol (or nullptr) and the address of
35300b57cec5SDimitry Andric // the symbol indirectly through n_value. Based on the relocation information
35310b57cec5SDimitry Andric // for the specified section offset in the specified section reference.
35320b57cec5SDimitry Andric // If no relocation information is found and a non-zero ReferenceValue for the
35330b57cec5SDimitry Andric // symbol is passed, look up that address in the info's AddrMap.
get_symbol_64(uint32_t sect_offset,SectionRef S,DisassembleInfo * info,uint64_t & n_value,uint64_t ReferenceValue=0)35340b57cec5SDimitry Andric static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
35350b57cec5SDimitry Andric                                  DisassembleInfo *info, uint64_t &n_value,
35360b57cec5SDimitry Andric                                  uint64_t ReferenceValue = 0) {
35370b57cec5SDimitry Andric   n_value = 0;
35380b57cec5SDimitry Andric   if (!info->verbose)
35390b57cec5SDimitry Andric     return nullptr;
35400b57cec5SDimitry Andric 
35410b57cec5SDimitry Andric   // See if there is an external relocation entry at the sect_offset.
35420b57cec5SDimitry Andric   bool reloc_found = false;
35430b57cec5SDimitry Andric   DataRefImpl Rel;
35440b57cec5SDimitry Andric   MachO::any_relocation_info RE;
35450b57cec5SDimitry Andric   bool isExtern = false;
35460b57cec5SDimitry Andric   SymbolRef Symbol;
35470b57cec5SDimitry Andric   for (const RelocationRef &Reloc : S.relocations()) {
35480b57cec5SDimitry Andric     uint64_t RelocOffset = Reloc.getOffset();
35490b57cec5SDimitry Andric     if (RelocOffset == sect_offset) {
35500b57cec5SDimitry Andric       Rel = Reloc.getRawDataRefImpl();
35510b57cec5SDimitry Andric       RE = info->O->getRelocation(Rel);
35520b57cec5SDimitry Andric       if (info->O->isRelocationScattered(RE))
35530b57cec5SDimitry Andric         continue;
35540b57cec5SDimitry Andric       isExtern = info->O->getPlainRelocationExternal(RE);
35550b57cec5SDimitry Andric       if (isExtern) {
35560b57cec5SDimitry Andric         symbol_iterator RelocSym = Reloc.getSymbol();
35570b57cec5SDimitry Andric         Symbol = *RelocSym;
35580b57cec5SDimitry Andric       }
35590b57cec5SDimitry Andric       reloc_found = true;
35600b57cec5SDimitry Andric       break;
35610b57cec5SDimitry Andric     }
35620b57cec5SDimitry Andric   }
35630b57cec5SDimitry Andric   // If there is an external relocation entry for a symbol in this section
35640b57cec5SDimitry Andric   // at this section_offset then use that symbol's value for the n_value
35650b57cec5SDimitry Andric   // and return its name.
35660b57cec5SDimitry Andric   const char *SymbolName = nullptr;
35670b57cec5SDimitry Andric   if (reloc_found && isExtern) {
35685ffd83dbSDimitry Andric     n_value = cantFail(Symbol.getValue());
35690b57cec5SDimitry Andric     StringRef Name = unwrapOrError(Symbol.getName(), info->O->getFileName());
35700b57cec5SDimitry Andric     if (!Name.empty()) {
35710b57cec5SDimitry Andric       SymbolName = Name.data();
35720b57cec5SDimitry Andric       return SymbolName;
35730b57cec5SDimitry Andric     }
35740b57cec5SDimitry Andric   }
35750b57cec5SDimitry Andric 
35760b57cec5SDimitry Andric   // TODO: For fully linked images, look through the external relocation
35770b57cec5SDimitry Andric   // entries off the dynamic symtab command. For these the r_offset is from the
35780b57cec5SDimitry Andric   // start of the first writeable segment in the Mach-O file.  So the offset
35790b57cec5SDimitry Andric   // to this section from that segment is passed to this routine by the caller,
35800b57cec5SDimitry Andric   // as the database_offset. Which is the difference of the section's starting
35810b57cec5SDimitry Andric   // address and the first writable segment.
35820b57cec5SDimitry Andric   //
35830b57cec5SDimitry Andric   // NOTE: need add passing the database_offset to this routine.
35840b57cec5SDimitry Andric 
35850b57cec5SDimitry Andric   // We did not find an external relocation entry so look up the ReferenceValue
35860b57cec5SDimitry Andric   // as an address of a symbol and if found return that symbol's name.
35870b57cec5SDimitry Andric   SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
35880b57cec5SDimitry Andric 
35890b57cec5SDimitry Andric   return SymbolName;
35900b57cec5SDimitry Andric }
35910b57cec5SDimitry Andric 
get_symbol_32(uint32_t sect_offset,SectionRef S,DisassembleInfo * info,uint32_t ReferenceValue)35920b57cec5SDimitry Andric static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
35930b57cec5SDimitry Andric                                  DisassembleInfo *info,
35940b57cec5SDimitry Andric                                  uint32_t ReferenceValue) {
35950b57cec5SDimitry Andric   uint64_t n_value64;
35960b57cec5SDimitry Andric   return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
35970b57cec5SDimitry Andric }
35980b57cec5SDimitry Andric 
35995ffd83dbSDimitry Andric namespace {
36005ffd83dbSDimitry Andric 
36010b57cec5SDimitry Andric // These are structs in the Objective-C meta data and read to produce the
36020b57cec5SDimitry Andric // comments for disassembly.  While these are part of the ABI they are no
36030b57cec5SDimitry Andric // public defintions.  So the are here not in include/llvm/BinaryFormat/MachO.h
36040b57cec5SDimitry Andric // .
36050b57cec5SDimitry Andric 
36060b57cec5SDimitry Andric // The cfstring object in a 64-bit Mach-O file.
36070b57cec5SDimitry Andric struct cfstring64_t {
36080b57cec5SDimitry Andric   uint64_t isa;        // class64_t * (64-bit pointer)
36090b57cec5SDimitry Andric   uint64_t flags;      // flag bits
36100b57cec5SDimitry Andric   uint64_t characters; // char * (64-bit pointer)
36110b57cec5SDimitry Andric   uint64_t length;     // number of non-NULL characters in above
36120b57cec5SDimitry Andric };
36130b57cec5SDimitry Andric 
36140b57cec5SDimitry Andric // The class object in a 64-bit Mach-O file.
36150b57cec5SDimitry Andric struct class64_t {
36160b57cec5SDimitry Andric   uint64_t isa;        // class64_t * (64-bit pointer)
36170b57cec5SDimitry Andric   uint64_t superclass; // class64_t * (64-bit pointer)
36180b57cec5SDimitry Andric   uint64_t cache;      // Cache (64-bit pointer)
36190b57cec5SDimitry Andric   uint64_t vtable;     // IMP * (64-bit pointer)
36200b57cec5SDimitry Andric   uint64_t data;       // class_ro64_t * (64-bit pointer)
36210b57cec5SDimitry Andric };
36220b57cec5SDimitry Andric 
36230b57cec5SDimitry Andric struct class32_t {
36240b57cec5SDimitry Andric   uint32_t isa;        /* class32_t * (32-bit pointer) */
36250b57cec5SDimitry Andric   uint32_t superclass; /* class32_t * (32-bit pointer) */
36260b57cec5SDimitry Andric   uint32_t cache;      /* Cache (32-bit pointer) */
36270b57cec5SDimitry Andric   uint32_t vtable;     /* IMP * (32-bit pointer) */
36280b57cec5SDimitry Andric   uint32_t data;       /* class_ro32_t * (32-bit pointer) */
36290b57cec5SDimitry Andric };
36300b57cec5SDimitry Andric 
36310b57cec5SDimitry Andric struct class_ro64_t {
36320b57cec5SDimitry Andric   uint32_t flags;
36330b57cec5SDimitry Andric   uint32_t instanceStart;
36340b57cec5SDimitry Andric   uint32_t instanceSize;
36350b57cec5SDimitry Andric   uint32_t reserved;
36360b57cec5SDimitry Andric   uint64_t ivarLayout;     // const uint8_t * (64-bit pointer)
36370b57cec5SDimitry Andric   uint64_t name;           // const char * (64-bit pointer)
36380b57cec5SDimitry Andric   uint64_t baseMethods;    // const method_list_t * (64-bit pointer)
36390b57cec5SDimitry Andric   uint64_t baseProtocols;  // const protocol_list_t * (64-bit pointer)
36400b57cec5SDimitry Andric   uint64_t ivars;          // const ivar_list_t * (64-bit pointer)
36410b57cec5SDimitry Andric   uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
36420b57cec5SDimitry Andric   uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
36430b57cec5SDimitry Andric };
36440b57cec5SDimitry Andric 
36450b57cec5SDimitry Andric struct class_ro32_t {
36460b57cec5SDimitry Andric   uint32_t flags;
36470b57cec5SDimitry Andric   uint32_t instanceStart;
36480b57cec5SDimitry Andric   uint32_t instanceSize;
36490b57cec5SDimitry Andric   uint32_t ivarLayout;     /* const uint8_t * (32-bit pointer) */
36500b57cec5SDimitry Andric   uint32_t name;           /* const char * (32-bit pointer) */
36510b57cec5SDimitry Andric   uint32_t baseMethods;    /* const method_list_t * (32-bit pointer) */
36520b57cec5SDimitry Andric   uint32_t baseProtocols;  /* const protocol_list_t * (32-bit pointer) */
36530b57cec5SDimitry Andric   uint32_t ivars;          /* const ivar_list_t * (32-bit pointer) */
36540b57cec5SDimitry Andric   uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
36550b57cec5SDimitry Andric   uint32_t baseProperties; /* const struct objc_property_list *
36560b57cec5SDimitry Andric                                                    (32-bit pointer) */
36570b57cec5SDimitry Andric };
36580b57cec5SDimitry Andric 
36590b57cec5SDimitry Andric /* Values for class_ro{64,32}_t->flags */
36600b57cec5SDimitry Andric #define RO_META (1 << 0)
36610b57cec5SDimitry Andric #define RO_ROOT (1 << 1)
36620b57cec5SDimitry Andric #define RO_HAS_CXX_STRUCTORS (1 << 2)
36630b57cec5SDimitry Andric 
3664*0fca6ea1SDimitry Andric /* Values for method_list{64,32}_t->entsize */
3665*0fca6ea1SDimitry Andric #define ML_HAS_RELATIVE_PTRS (1 << 31)
3666*0fca6ea1SDimitry Andric #define ML_ENTSIZE_MASK 0xFFFF
3667*0fca6ea1SDimitry Andric 
36680b57cec5SDimitry Andric struct method_list64_t {
36690b57cec5SDimitry Andric   uint32_t entsize;
36700b57cec5SDimitry Andric   uint32_t count;
36710b57cec5SDimitry Andric   /* struct method64_t first;  These structures follow inline */
36720b57cec5SDimitry Andric };
36730b57cec5SDimitry Andric 
36740b57cec5SDimitry Andric struct method_list32_t {
36750b57cec5SDimitry Andric   uint32_t entsize;
36760b57cec5SDimitry Andric   uint32_t count;
36770b57cec5SDimitry Andric   /* struct method32_t first;  These structures follow inline */
36780b57cec5SDimitry Andric };
36790b57cec5SDimitry Andric 
36800b57cec5SDimitry Andric struct method64_t {
36810b57cec5SDimitry Andric   uint64_t name;  /* SEL (64-bit pointer) */
36820b57cec5SDimitry Andric   uint64_t types; /* const char * (64-bit pointer) */
36830b57cec5SDimitry Andric   uint64_t imp;   /* IMP (64-bit pointer) */
36840b57cec5SDimitry Andric };
36850b57cec5SDimitry Andric 
36860b57cec5SDimitry Andric struct method32_t {
36870b57cec5SDimitry Andric   uint32_t name;  /* SEL (32-bit pointer) */
36880b57cec5SDimitry Andric   uint32_t types; /* const char * (32-bit pointer) */
36890b57cec5SDimitry Andric   uint32_t imp;   /* IMP (32-bit pointer) */
36900b57cec5SDimitry Andric };
36910b57cec5SDimitry Andric 
3692*0fca6ea1SDimitry Andric struct method_relative_t {
3693*0fca6ea1SDimitry Andric   int32_t name;  /* SEL (32-bit relative) */
3694*0fca6ea1SDimitry Andric   int32_t types; /* const char * (32-bit relative) */
3695*0fca6ea1SDimitry Andric   int32_t imp;   /* IMP (32-bit relative) */
3696*0fca6ea1SDimitry Andric };
3697*0fca6ea1SDimitry Andric 
36980b57cec5SDimitry Andric struct protocol_list64_t {
36990b57cec5SDimitry Andric   uint64_t count; /* uintptr_t (a 64-bit value) */
37000b57cec5SDimitry Andric   /* struct protocol64_t * list[0];  These pointers follow inline */
37010b57cec5SDimitry Andric };
37020b57cec5SDimitry Andric 
37030b57cec5SDimitry Andric struct protocol_list32_t {
37040b57cec5SDimitry Andric   uint32_t count; /* uintptr_t (a 32-bit value) */
37050b57cec5SDimitry Andric   /* struct protocol32_t * list[0];  These pointers follow inline */
37060b57cec5SDimitry Andric };
37070b57cec5SDimitry Andric 
37080b57cec5SDimitry Andric struct protocol64_t {
37090b57cec5SDimitry Andric   uint64_t isa;                     /* id * (64-bit pointer) */
37100b57cec5SDimitry Andric   uint64_t name;                    /* const char * (64-bit pointer) */
37110b57cec5SDimitry Andric   uint64_t protocols;               /* struct protocol_list64_t *
37120b57cec5SDimitry Andric                                                     (64-bit pointer) */
37130b57cec5SDimitry Andric   uint64_t instanceMethods;         /* method_list_t * (64-bit pointer) */
37140b57cec5SDimitry Andric   uint64_t classMethods;            /* method_list_t * (64-bit pointer) */
37150b57cec5SDimitry Andric   uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
37160b57cec5SDimitry Andric   uint64_t optionalClassMethods;    /* method_list_t * (64-bit pointer) */
37170b57cec5SDimitry Andric   uint64_t instanceProperties;      /* struct objc_property_list *
37180b57cec5SDimitry Andric                                                        (64-bit pointer) */
37190b57cec5SDimitry Andric };
37200b57cec5SDimitry Andric 
37210b57cec5SDimitry Andric struct protocol32_t {
37220b57cec5SDimitry Andric   uint32_t isa;                     /* id * (32-bit pointer) */
37230b57cec5SDimitry Andric   uint32_t name;                    /* const char * (32-bit pointer) */
37240b57cec5SDimitry Andric   uint32_t protocols;               /* struct protocol_list_t *
37250b57cec5SDimitry Andric                                                     (32-bit pointer) */
37260b57cec5SDimitry Andric   uint32_t instanceMethods;         /* method_list_t * (32-bit pointer) */
37270b57cec5SDimitry Andric   uint32_t classMethods;            /* method_list_t * (32-bit pointer) */
37280b57cec5SDimitry Andric   uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
37290b57cec5SDimitry Andric   uint32_t optionalClassMethods;    /* method_list_t * (32-bit pointer) */
37300b57cec5SDimitry Andric   uint32_t instanceProperties;      /* struct objc_property_list *
37310b57cec5SDimitry Andric                                                        (32-bit pointer) */
37320b57cec5SDimitry Andric };
37330b57cec5SDimitry Andric 
37340b57cec5SDimitry Andric struct ivar_list64_t {
37350b57cec5SDimitry Andric   uint32_t entsize;
37360b57cec5SDimitry Andric   uint32_t count;
37370b57cec5SDimitry Andric   /* struct ivar64_t first;  These structures follow inline */
37380b57cec5SDimitry Andric };
37390b57cec5SDimitry Andric 
37400b57cec5SDimitry Andric struct ivar_list32_t {
37410b57cec5SDimitry Andric   uint32_t entsize;
37420b57cec5SDimitry Andric   uint32_t count;
37430b57cec5SDimitry Andric   /* struct ivar32_t first;  These structures follow inline */
37440b57cec5SDimitry Andric };
37450b57cec5SDimitry Andric 
37460b57cec5SDimitry Andric struct ivar64_t {
37470b57cec5SDimitry Andric   uint64_t offset; /* uintptr_t * (64-bit pointer) */
37480b57cec5SDimitry Andric   uint64_t name;   /* const char * (64-bit pointer) */
37490b57cec5SDimitry Andric   uint64_t type;   /* const char * (64-bit pointer) */
37500b57cec5SDimitry Andric   uint32_t alignment;
37510b57cec5SDimitry Andric   uint32_t size;
37520b57cec5SDimitry Andric };
37530b57cec5SDimitry Andric 
37540b57cec5SDimitry Andric struct ivar32_t {
37550b57cec5SDimitry Andric   uint32_t offset; /* uintptr_t * (32-bit pointer) */
37560b57cec5SDimitry Andric   uint32_t name;   /* const char * (32-bit pointer) */
37570b57cec5SDimitry Andric   uint32_t type;   /* const char * (32-bit pointer) */
37580b57cec5SDimitry Andric   uint32_t alignment;
37590b57cec5SDimitry Andric   uint32_t size;
37600b57cec5SDimitry Andric };
37610b57cec5SDimitry Andric 
37620b57cec5SDimitry Andric struct objc_property_list64 {
37630b57cec5SDimitry Andric   uint32_t entsize;
37640b57cec5SDimitry Andric   uint32_t count;
37650b57cec5SDimitry Andric   /* struct objc_property64 first;  These structures follow inline */
37660b57cec5SDimitry Andric };
37670b57cec5SDimitry Andric 
37680b57cec5SDimitry Andric struct objc_property_list32 {
37690b57cec5SDimitry Andric   uint32_t entsize;
37700b57cec5SDimitry Andric   uint32_t count;
37710b57cec5SDimitry Andric   /* struct objc_property32 first;  These structures follow inline */
37720b57cec5SDimitry Andric };
37730b57cec5SDimitry Andric 
37740b57cec5SDimitry Andric struct objc_property64 {
37750b57cec5SDimitry Andric   uint64_t name;       /* const char * (64-bit pointer) */
37760b57cec5SDimitry Andric   uint64_t attributes; /* const char * (64-bit pointer) */
37770b57cec5SDimitry Andric };
37780b57cec5SDimitry Andric 
37790b57cec5SDimitry Andric struct objc_property32 {
37800b57cec5SDimitry Andric   uint32_t name;       /* const char * (32-bit pointer) */
37810b57cec5SDimitry Andric   uint32_t attributes; /* const char * (32-bit pointer) */
37820b57cec5SDimitry Andric };
37830b57cec5SDimitry Andric 
37840b57cec5SDimitry Andric struct category64_t {
37850b57cec5SDimitry Andric   uint64_t name;               /* const char * (64-bit pointer) */
37860b57cec5SDimitry Andric   uint64_t cls;                /* struct class_t * (64-bit pointer) */
37870b57cec5SDimitry Andric   uint64_t instanceMethods;    /* struct method_list_t * (64-bit pointer) */
37880b57cec5SDimitry Andric   uint64_t classMethods;       /* struct method_list_t * (64-bit pointer) */
37890b57cec5SDimitry Andric   uint64_t protocols;          /* struct protocol_list_t * (64-bit pointer) */
37900b57cec5SDimitry Andric   uint64_t instanceProperties; /* struct objc_property_list *
37910b57cec5SDimitry Andric                                   (64-bit pointer) */
37920b57cec5SDimitry Andric };
37930b57cec5SDimitry Andric 
37940b57cec5SDimitry Andric struct category32_t {
37950b57cec5SDimitry Andric   uint32_t name;               /* const char * (32-bit pointer) */
37960b57cec5SDimitry Andric   uint32_t cls;                /* struct class_t * (32-bit pointer) */
37970b57cec5SDimitry Andric   uint32_t instanceMethods;    /* struct method_list_t * (32-bit pointer) */
37980b57cec5SDimitry Andric   uint32_t classMethods;       /* struct method_list_t * (32-bit pointer) */
37990b57cec5SDimitry Andric   uint32_t protocols;          /* struct protocol_list_t * (32-bit pointer) */
38000b57cec5SDimitry Andric   uint32_t instanceProperties; /* struct objc_property_list *
38010b57cec5SDimitry Andric                                   (32-bit pointer) */
38020b57cec5SDimitry Andric };
38030b57cec5SDimitry Andric 
38040b57cec5SDimitry Andric struct objc_image_info64 {
38050b57cec5SDimitry Andric   uint32_t version;
38060b57cec5SDimitry Andric   uint32_t flags;
38070b57cec5SDimitry Andric };
38080b57cec5SDimitry Andric struct objc_image_info32 {
38090b57cec5SDimitry Andric   uint32_t version;
38100b57cec5SDimitry Andric   uint32_t flags;
38110b57cec5SDimitry Andric };
38120b57cec5SDimitry Andric struct imageInfo_t {
38130b57cec5SDimitry Andric   uint32_t version;
38140b57cec5SDimitry Andric   uint32_t flags;
38150b57cec5SDimitry Andric };
38160b57cec5SDimitry Andric /* masks for objc_image_info.flags */
38170b57cec5SDimitry Andric #define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
38180b57cec5SDimitry Andric #define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
38190b57cec5SDimitry Andric #define OBJC_IMAGE_IS_SIMULATED (1 << 5)
38200b57cec5SDimitry Andric #define OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES (1 << 6)
38210b57cec5SDimitry Andric 
38220b57cec5SDimitry Andric struct message_ref64 {
38230b57cec5SDimitry Andric   uint64_t imp; /* IMP (64-bit pointer) */
38240b57cec5SDimitry Andric   uint64_t sel; /* SEL (64-bit pointer) */
38250b57cec5SDimitry Andric };
38260b57cec5SDimitry Andric 
38270b57cec5SDimitry Andric struct message_ref32 {
38280b57cec5SDimitry Andric   uint32_t imp; /* IMP (32-bit pointer) */
38290b57cec5SDimitry Andric   uint32_t sel; /* SEL (32-bit pointer) */
38300b57cec5SDimitry Andric };
38310b57cec5SDimitry Andric 
38320b57cec5SDimitry Andric // Objective-C 1 (32-bit only) meta data structs.
38330b57cec5SDimitry Andric 
38340b57cec5SDimitry Andric struct objc_module_t {
38350b57cec5SDimitry Andric   uint32_t version;
38360b57cec5SDimitry Andric   uint32_t size;
38370b57cec5SDimitry Andric   uint32_t name;   /* char * (32-bit pointer) */
38380b57cec5SDimitry Andric   uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
38390b57cec5SDimitry Andric };
38400b57cec5SDimitry Andric 
38410b57cec5SDimitry Andric struct objc_symtab_t {
38420b57cec5SDimitry Andric   uint32_t sel_ref_cnt;
38430b57cec5SDimitry Andric   uint32_t refs; /* SEL * (32-bit pointer) */
38440b57cec5SDimitry Andric   uint16_t cls_def_cnt;
38450b57cec5SDimitry Andric   uint16_t cat_def_cnt;
38460b57cec5SDimitry Andric   // uint32_t defs[1];        /* void * (32-bit pointer) variable size */
38470b57cec5SDimitry Andric };
38480b57cec5SDimitry Andric 
38490b57cec5SDimitry Andric struct objc_class_t {
38500b57cec5SDimitry Andric   uint32_t isa;         /* struct objc_class * (32-bit pointer) */
38510b57cec5SDimitry Andric   uint32_t super_class; /* struct objc_class * (32-bit pointer) */
38520b57cec5SDimitry Andric   uint32_t name;        /* const char * (32-bit pointer) */
38530b57cec5SDimitry Andric   int32_t version;
38540b57cec5SDimitry Andric   int32_t info;
38550b57cec5SDimitry Andric   int32_t instance_size;
38560b57cec5SDimitry Andric   uint32_t ivars;       /* struct objc_ivar_list * (32-bit pointer) */
38570b57cec5SDimitry Andric   uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
38580b57cec5SDimitry Andric   uint32_t cache;       /* struct objc_cache * (32-bit pointer) */
38590b57cec5SDimitry Andric   uint32_t protocols;   /* struct objc_protocol_list * (32-bit pointer) */
38600b57cec5SDimitry Andric };
38610b57cec5SDimitry Andric 
38620b57cec5SDimitry Andric #define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
38630b57cec5SDimitry Andric // class is not a metaclass
38640b57cec5SDimitry Andric #define CLS_CLASS 0x1
38650b57cec5SDimitry Andric // class is a metaclass
38660b57cec5SDimitry Andric #define CLS_META 0x2
38670b57cec5SDimitry Andric 
38680b57cec5SDimitry Andric struct objc_category_t {
38690b57cec5SDimitry Andric   uint32_t category_name;    /* char * (32-bit pointer) */
38700b57cec5SDimitry Andric   uint32_t class_name;       /* char * (32-bit pointer) */
38710b57cec5SDimitry Andric   uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
38720b57cec5SDimitry Andric   uint32_t class_methods;    /* struct objc_method_list * (32-bit pointer) */
38730b57cec5SDimitry Andric   uint32_t protocols;        /* struct objc_protocol_list * (32-bit ptr) */
38740b57cec5SDimitry Andric };
38750b57cec5SDimitry Andric 
38760b57cec5SDimitry Andric struct objc_ivar_t {
38770b57cec5SDimitry Andric   uint32_t ivar_name; /* char * (32-bit pointer) */
38780b57cec5SDimitry Andric   uint32_t ivar_type; /* char * (32-bit pointer) */
38790b57cec5SDimitry Andric   int32_t ivar_offset;
38800b57cec5SDimitry Andric };
38810b57cec5SDimitry Andric 
38820b57cec5SDimitry Andric struct objc_ivar_list_t {
38830b57cec5SDimitry Andric   int32_t ivar_count;
38840b57cec5SDimitry Andric   // struct objc_ivar_t ivar_list[1];          /* variable length structure */
38850b57cec5SDimitry Andric };
38860b57cec5SDimitry Andric 
38870b57cec5SDimitry Andric struct objc_method_list_t {
38880b57cec5SDimitry Andric   uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
38890b57cec5SDimitry Andric   int32_t method_count;
38900b57cec5SDimitry Andric   // struct objc_method_t method_list[1];      /* variable length structure */
38910b57cec5SDimitry Andric };
38920b57cec5SDimitry Andric 
38930b57cec5SDimitry Andric struct objc_method_t {
38940b57cec5SDimitry Andric   uint32_t method_name;  /* SEL, aka struct objc_selector * (32-bit pointer) */
38950b57cec5SDimitry Andric   uint32_t method_types; /* char * (32-bit pointer) */
38960b57cec5SDimitry Andric   uint32_t method_imp;   /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
38970b57cec5SDimitry Andric                             (32-bit pointer) */
38980b57cec5SDimitry Andric };
38990b57cec5SDimitry Andric 
39000b57cec5SDimitry Andric struct objc_protocol_list_t {
39010b57cec5SDimitry Andric   uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
39020b57cec5SDimitry Andric   int32_t count;
39030b57cec5SDimitry Andric   // uint32_t list[1];   /* Protocol *, aka struct objc_protocol_t *
39040b57cec5SDimitry Andric   //                        (32-bit pointer) */
39050b57cec5SDimitry Andric };
39060b57cec5SDimitry Andric 
39070b57cec5SDimitry Andric struct objc_protocol_t {
39080b57cec5SDimitry Andric   uint32_t isa;              /* struct objc_class * (32-bit pointer) */
39090b57cec5SDimitry Andric   uint32_t protocol_name;    /* char * (32-bit pointer) */
39100b57cec5SDimitry Andric   uint32_t protocol_list;    /* struct objc_protocol_list * (32-bit pointer) */
39110b57cec5SDimitry Andric   uint32_t instance_methods; /* struct objc_method_description_list *
39120b57cec5SDimitry Andric                                 (32-bit pointer) */
39130b57cec5SDimitry Andric   uint32_t class_methods;    /* struct objc_method_description_list *
39140b57cec5SDimitry Andric                                 (32-bit pointer) */
39150b57cec5SDimitry Andric };
39160b57cec5SDimitry Andric 
39170b57cec5SDimitry Andric struct objc_method_description_list_t {
39180b57cec5SDimitry Andric   int32_t count;
39190b57cec5SDimitry Andric   // struct objc_method_description_t list[1];
39200b57cec5SDimitry Andric };
39210b57cec5SDimitry Andric 
39220b57cec5SDimitry Andric struct objc_method_description_t {
39230b57cec5SDimitry Andric   uint32_t name;  /* SEL, aka struct objc_selector * (32-bit pointer) */
39240b57cec5SDimitry Andric   uint32_t types; /* char * (32-bit pointer) */
39250b57cec5SDimitry Andric };
39260b57cec5SDimitry Andric 
swapStruct(struct cfstring64_t & cfs)39270b57cec5SDimitry Andric inline void swapStruct(struct cfstring64_t &cfs) {
39280b57cec5SDimitry Andric   sys::swapByteOrder(cfs.isa);
39290b57cec5SDimitry Andric   sys::swapByteOrder(cfs.flags);
39300b57cec5SDimitry Andric   sys::swapByteOrder(cfs.characters);
39310b57cec5SDimitry Andric   sys::swapByteOrder(cfs.length);
39320b57cec5SDimitry Andric }
39330b57cec5SDimitry Andric 
swapStruct(struct class64_t & c)39340b57cec5SDimitry Andric inline void swapStruct(struct class64_t &c) {
39350b57cec5SDimitry Andric   sys::swapByteOrder(c.isa);
39360b57cec5SDimitry Andric   sys::swapByteOrder(c.superclass);
39370b57cec5SDimitry Andric   sys::swapByteOrder(c.cache);
39380b57cec5SDimitry Andric   sys::swapByteOrder(c.vtable);
39390b57cec5SDimitry Andric   sys::swapByteOrder(c.data);
39400b57cec5SDimitry Andric }
39410b57cec5SDimitry Andric 
swapStruct(struct class32_t & c)39420b57cec5SDimitry Andric inline void swapStruct(struct class32_t &c) {
39430b57cec5SDimitry Andric   sys::swapByteOrder(c.isa);
39440b57cec5SDimitry Andric   sys::swapByteOrder(c.superclass);
39450b57cec5SDimitry Andric   sys::swapByteOrder(c.cache);
39460b57cec5SDimitry Andric   sys::swapByteOrder(c.vtable);
39470b57cec5SDimitry Andric   sys::swapByteOrder(c.data);
39480b57cec5SDimitry Andric }
39490b57cec5SDimitry Andric 
swapStruct(struct class_ro64_t & cro)39500b57cec5SDimitry Andric inline void swapStruct(struct class_ro64_t &cro) {
39510b57cec5SDimitry Andric   sys::swapByteOrder(cro.flags);
39520b57cec5SDimitry Andric   sys::swapByteOrder(cro.instanceStart);
39530b57cec5SDimitry Andric   sys::swapByteOrder(cro.instanceSize);
39540b57cec5SDimitry Andric   sys::swapByteOrder(cro.reserved);
39550b57cec5SDimitry Andric   sys::swapByteOrder(cro.ivarLayout);
39560b57cec5SDimitry Andric   sys::swapByteOrder(cro.name);
39570b57cec5SDimitry Andric   sys::swapByteOrder(cro.baseMethods);
39580b57cec5SDimitry Andric   sys::swapByteOrder(cro.baseProtocols);
39590b57cec5SDimitry Andric   sys::swapByteOrder(cro.ivars);
39600b57cec5SDimitry Andric   sys::swapByteOrder(cro.weakIvarLayout);
39610b57cec5SDimitry Andric   sys::swapByteOrder(cro.baseProperties);
39620b57cec5SDimitry Andric }
39630b57cec5SDimitry Andric 
swapStruct(struct class_ro32_t & cro)39640b57cec5SDimitry Andric inline void swapStruct(struct class_ro32_t &cro) {
39650b57cec5SDimitry Andric   sys::swapByteOrder(cro.flags);
39660b57cec5SDimitry Andric   sys::swapByteOrder(cro.instanceStart);
39670b57cec5SDimitry Andric   sys::swapByteOrder(cro.instanceSize);
39680b57cec5SDimitry Andric   sys::swapByteOrder(cro.ivarLayout);
39690b57cec5SDimitry Andric   sys::swapByteOrder(cro.name);
39700b57cec5SDimitry Andric   sys::swapByteOrder(cro.baseMethods);
39710b57cec5SDimitry Andric   sys::swapByteOrder(cro.baseProtocols);
39720b57cec5SDimitry Andric   sys::swapByteOrder(cro.ivars);
39730b57cec5SDimitry Andric   sys::swapByteOrder(cro.weakIvarLayout);
39740b57cec5SDimitry Andric   sys::swapByteOrder(cro.baseProperties);
39750b57cec5SDimitry Andric }
39760b57cec5SDimitry Andric 
swapStruct(struct method_list64_t & ml)39770b57cec5SDimitry Andric inline void swapStruct(struct method_list64_t &ml) {
39780b57cec5SDimitry Andric   sys::swapByteOrder(ml.entsize);
39790b57cec5SDimitry Andric   sys::swapByteOrder(ml.count);
39800b57cec5SDimitry Andric }
39810b57cec5SDimitry Andric 
swapStruct(struct method_list32_t & ml)39820b57cec5SDimitry Andric inline void swapStruct(struct method_list32_t &ml) {
39830b57cec5SDimitry Andric   sys::swapByteOrder(ml.entsize);
39840b57cec5SDimitry Andric   sys::swapByteOrder(ml.count);
39850b57cec5SDimitry Andric }
39860b57cec5SDimitry Andric 
swapStruct(struct method64_t & m)39870b57cec5SDimitry Andric inline void swapStruct(struct method64_t &m) {
39880b57cec5SDimitry Andric   sys::swapByteOrder(m.name);
39890b57cec5SDimitry Andric   sys::swapByteOrder(m.types);
39900b57cec5SDimitry Andric   sys::swapByteOrder(m.imp);
39910b57cec5SDimitry Andric }
39920b57cec5SDimitry Andric 
swapStruct(struct method32_t & m)39930b57cec5SDimitry Andric inline void swapStruct(struct method32_t &m) {
39940b57cec5SDimitry Andric   sys::swapByteOrder(m.name);
39950b57cec5SDimitry Andric   sys::swapByteOrder(m.types);
39960b57cec5SDimitry Andric   sys::swapByteOrder(m.imp);
39970b57cec5SDimitry Andric }
39980b57cec5SDimitry Andric 
swapStruct(struct method_relative_t & m)3999*0fca6ea1SDimitry Andric inline void swapStruct(struct method_relative_t &m) {
4000*0fca6ea1SDimitry Andric   sys::swapByteOrder(m.name);
4001*0fca6ea1SDimitry Andric   sys::swapByteOrder(m.types);
4002*0fca6ea1SDimitry Andric   sys::swapByteOrder(m.imp);
4003*0fca6ea1SDimitry Andric }
4004*0fca6ea1SDimitry Andric 
swapStruct(struct protocol_list64_t & pl)40050b57cec5SDimitry Andric inline void swapStruct(struct protocol_list64_t &pl) {
40060b57cec5SDimitry Andric   sys::swapByteOrder(pl.count);
40070b57cec5SDimitry Andric }
40080b57cec5SDimitry Andric 
swapStruct(struct protocol_list32_t & pl)40090b57cec5SDimitry Andric inline void swapStruct(struct protocol_list32_t &pl) {
40100b57cec5SDimitry Andric   sys::swapByteOrder(pl.count);
40110b57cec5SDimitry Andric }
40120b57cec5SDimitry Andric 
swapStruct(struct protocol64_t & p)40130b57cec5SDimitry Andric inline void swapStruct(struct protocol64_t &p) {
40140b57cec5SDimitry Andric   sys::swapByteOrder(p.isa);
40150b57cec5SDimitry Andric   sys::swapByteOrder(p.name);
40160b57cec5SDimitry Andric   sys::swapByteOrder(p.protocols);
40170b57cec5SDimitry Andric   sys::swapByteOrder(p.instanceMethods);
40180b57cec5SDimitry Andric   sys::swapByteOrder(p.classMethods);
40190b57cec5SDimitry Andric   sys::swapByteOrder(p.optionalInstanceMethods);
40200b57cec5SDimitry Andric   sys::swapByteOrder(p.optionalClassMethods);
40210b57cec5SDimitry Andric   sys::swapByteOrder(p.instanceProperties);
40220b57cec5SDimitry Andric }
40230b57cec5SDimitry Andric 
swapStruct(struct protocol32_t & p)40240b57cec5SDimitry Andric inline void swapStruct(struct protocol32_t &p) {
40250b57cec5SDimitry Andric   sys::swapByteOrder(p.isa);
40260b57cec5SDimitry Andric   sys::swapByteOrder(p.name);
40270b57cec5SDimitry Andric   sys::swapByteOrder(p.protocols);
40280b57cec5SDimitry Andric   sys::swapByteOrder(p.instanceMethods);
40290b57cec5SDimitry Andric   sys::swapByteOrder(p.classMethods);
40300b57cec5SDimitry Andric   sys::swapByteOrder(p.optionalInstanceMethods);
40310b57cec5SDimitry Andric   sys::swapByteOrder(p.optionalClassMethods);
40320b57cec5SDimitry Andric   sys::swapByteOrder(p.instanceProperties);
40330b57cec5SDimitry Andric }
40340b57cec5SDimitry Andric 
swapStruct(struct ivar_list64_t & il)40350b57cec5SDimitry Andric inline void swapStruct(struct ivar_list64_t &il) {
40360b57cec5SDimitry Andric   sys::swapByteOrder(il.entsize);
40370b57cec5SDimitry Andric   sys::swapByteOrder(il.count);
40380b57cec5SDimitry Andric }
40390b57cec5SDimitry Andric 
swapStruct(struct ivar_list32_t & il)40400b57cec5SDimitry Andric inline void swapStruct(struct ivar_list32_t &il) {
40410b57cec5SDimitry Andric   sys::swapByteOrder(il.entsize);
40420b57cec5SDimitry Andric   sys::swapByteOrder(il.count);
40430b57cec5SDimitry Andric }
40440b57cec5SDimitry Andric 
swapStruct(struct ivar64_t & i)40450b57cec5SDimitry Andric inline void swapStruct(struct ivar64_t &i) {
40460b57cec5SDimitry Andric   sys::swapByteOrder(i.offset);
40470b57cec5SDimitry Andric   sys::swapByteOrder(i.name);
40480b57cec5SDimitry Andric   sys::swapByteOrder(i.type);
40490b57cec5SDimitry Andric   sys::swapByteOrder(i.alignment);
40500b57cec5SDimitry Andric   sys::swapByteOrder(i.size);
40510b57cec5SDimitry Andric }
40520b57cec5SDimitry Andric 
swapStruct(struct ivar32_t & i)40530b57cec5SDimitry Andric inline void swapStruct(struct ivar32_t &i) {
40540b57cec5SDimitry Andric   sys::swapByteOrder(i.offset);
40550b57cec5SDimitry Andric   sys::swapByteOrder(i.name);
40560b57cec5SDimitry Andric   sys::swapByteOrder(i.type);
40570b57cec5SDimitry Andric   sys::swapByteOrder(i.alignment);
40580b57cec5SDimitry Andric   sys::swapByteOrder(i.size);
40590b57cec5SDimitry Andric }
40600b57cec5SDimitry Andric 
swapStruct(struct objc_property_list64 & pl)40610b57cec5SDimitry Andric inline void swapStruct(struct objc_property_list64 &pl) {
40620b57cec5SDimitry Andric   sys::swapByteOrder(pl.entsize);
40630b57cec5SDimitry Andric   sys::swapByteOrder(pl.count);
40640b57cec5SDimitry Andric }
40650b57cec5SDimitry Andric 
swapStruct(struct objc_property_list32 & pl)40660b57cec5SDimitry Andric inline void swapStruct(struct objc_property_list32 &pl) {
40670b57cec5SDimitry Andric   sys::swapByteOrder(pl.entsize);
40680b57cec5SDimitry Andric   sys::swapByteOrder(pl.count);
40690b57cec5SDimitry Andric }
40700b57cec5SDimitry Andric 
swapStruct(struct objc_property64 & op)40710b57cec5SDimitry Andric inline void swapStruct(struct objc_property64 &op) {
40720b57cec5SDimitry Andric   sys::swapByteOrder(op.name);
40730b57cec5SDimitry Andric   sys::swapByteOrder(op.attributes);
40740b57cec5SDimitry Andric }
40750b57cec5SDimitry Andric 
swapStruct(struct objc_property32 & op)40760b57cec5SDimitry Andric inline void swapStruct(struct objc_property32 &op) {
40770b57cec5SDimitry Andric   sys::swapByteOrder(op.name);
40780b57cec5SDimitry Andric   sys::swapByteOrder(op.attributes);
40790b57cec5SDimitry Andric }
40800b57cec5SDimitry Andric 
swapStruct(struct category64_t & c)40810b57cec5SDimitry Andric inline void swapStruct(struct category64_t &c) {
40820b57cec5SDimitry Andric   sys::swapByteOrder(c.name);
40830b57cec5SDimitry Andric   sys::swapByteOrder(c.cls);
40840b57cec5SDimitry Andric   sys::swapByteOrder(c.instanceMethods);
40850b57cec5SDimitry Andric   sys::swapByteOrder(c.classMethods);
40860b57cec5SDimitry Andric   sys::swapByteOrder(c.protocols);
40870b57cec5SDimitry Andric   sys::swapByteOrder(c.instanceProperties);
40880b57cec5SDimitry Andric }
40890b57cec5SDimitry Andric 
swapStruct(struct category32_t & c)40900b57cec5SDimitry Andric inline void swapStruct(struct category32_t &c) {
40910b57cec5SDimitry Andric   sys::swapByteOrder(c.name);
40920b57cec5SDimitry Andric   sys::swapByteOrder(c.cls);
40930b57cec5SDimitry Andric   sys::swapByteOrder(c.instanceMethods);
40940b57cec5SDimitry Andric   sys::swapByteOrder(c.classMethods);
40950b57cec5SDimitry Andric   sys::swapByteOrder(c.protocols);
40960b57cec5SDimitry Andric   sys::swapByteOrder(c.instanceProperties);
40970b57cec5SDimitry Andric }
40980b57cec5SDimitry Andric 
swapStruct(struct objc_image_info64 & o)40990b57cec5SDimitry Andric inline void swapStruct(struct objc_image_info64 &o) {
41000b57cec5SDimitry Andric   sys::swapByteOrder(o.version);
41010b57cec5SDimitry Andric   sys::swapByteOrder(o.flags);
41020b57cec5SDimitry Andric }
41030b57cec5SDimitry Andric 
swapStruct(struct objc_image_info32 & o)41040b57cec5SDimitry Andric inline void swapStruct(struct objc_image_info32 &o) {
41050b57cec5SDimitry Andric   sys::swapByteOrder(o.version);
41060b57cec5SDimitry Andric   sys::swapByteOrder(o.flags);
41070b57cec5SDimitry Andric }
41080b57cec5SDimitry Andric 
swapStruct(struct imageInfo_t & o)41090b57cec5SDimitry Andric inline void swapStruct(struct imageInfo_t &o) {
41100b57cec5SDimitry Andric   sys::swapByteOrder(o.version);
41110b57cec5SDimitry Andric   sys::swapByteOrder(o.flags);
41120b57cec5SDimitry Andric }
41130b57cec5SDimitry Andric 
swapStruct(struct message_ref64 & mr)41140b57cec5SDimitry Andric inline void swapStruct(struct message_ref64 &mr) {
41150b57cec5SDimitry Andric   sys::swapByteOrder(mr.imp);
41160b57cec5SDimitry Andric   sys::swapByteOrder(mr.sel);
41170b57cec5SDimitry Andric }
41180b57cec5SDimitry Andric 
swapStruct(struct message_ref32 & mr)41190b57cec5SDimitry Andric inline void swapStruct(struct message_ref32 &mr) {
41200b57cec5SDimitry Andric   sys::swapByteOrder(mr.imp);
41210b57cec5SDimitry Andric   sys::swapByteOrder(mr.sel);
41220b57cec5SDimitry Andric }
41230b57cec5SDimitry Andric 
swapStruct(struct objc_module_t & module)41240b57cec5SDimitry Andric inline void swapStruct(struct objc_module_t &module) {
41250b57cec5SDimitry Andric   sys::swapByteOrder(module.version);
41260b57cec5SDimitry Andric   sys::swapByteOrder(module.size);
41270b57cec5SDimitry Andric   sys::swapByteOrder(module.name);
41280b57cec5SDimitry Andric   sys::swapByteOrder(module.symtab);
41290b57cec5SDimitry Andric }
41300b57cec5SDimitry Andric 
swapStruct(struct objc_symtab_t & symtab)41310b57cec5SDimitry Andric inline void swapStruct(struct objc_symtab_t &symtab) {
41320b57cec5SDimitry Andric   sys::swapByteOrder(symtab.sel_ref_cnt);
41330b57cec5SDimitry Andric   sys::swapByteOrder(symtab.refs);
41340b57cec5SDimitry Andric   sys::swapByteOrder(symtab.cls_def_cnt);
41350b57cec5SDimitry Andric   sys::swapByteOrder(symtab.cat_def_cnt);
41360b57cec5SDimitry Andric }
41370b57cec5SDimitry Andric 
swapStruct(struct objc_class_t & objc_class)41380b57cec5SDimitry Andric inline void swapStruct(struct objc_class_t &objc_class) {
41390b57cec5SDimitry Andric   sys::swapByteOrder(objc_class.isa);
41400b57cec5SDimitry Andric   sys::swapByteOrder(objc_class.super_class);
41410b57cec5SDimitry Andric   sys::swapByteOrder(objc_class.name);
41420b57cec5SDimitry Andric   sys::swapByteOrder(objc_class.version);
41430b57cec5SDimitry Andric   sys::swapByteOrder(objc_class.info);
41440b57cec5SDimitry Andric   sys::swapByteOrder(objc_class.instance_size);
41450b57cec5SDimitry Andric   sys::swapByteOrder(objc_class.ivars);
41460b57cec5SDimitry Andric   sys::swapByteOrder(objc_class.methodLists);
41470b57cec5SDimitry Andric   sys::swapByteOrder(objc_class.cache);
41480b57cec5SDimitry Andric   sys::swapByteOrder(objc_class.protocols);
41490b57cec5SDimitry Andric }
41500b57cec5SDimitry Andric 
swapStruct(struct objc_category_t & objc_category)41510b57cec5SDimitry Andric inline void swapStruct(struct objc_category_t &objc_category) {
41520b57cec5SDimitry Andric   sys::swapByteOrder(objc_category.category_name);
41530b57cec5SDimitry Andric   sys::swapByteOrder(objc_category.class_name);
41540b57cec5SDimitry Andric   sys::swapByteOrder(objc_category.instance_methods);
41550b57cec5SDimitry Andric   sys::swapByteOrder(objc_category.class_methods);
41560b57cec5SDimitry Andric   sys::swapByteOrder(objc_category.protocols);
41570b57cec5SDimitry Andric }
41580b57cec5SDimitry Andric 
swapStruct(struct objc_ivar_list_t & objc_ivar_list)41590b57cec5SDimitry Andric inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
41600b57cec5SDimitry Andric   sys::swapByteOrder(objc_ivar_list.ivar_count);
41610b57cec5SDimitry Andric }
41620b57cec5SDimitry Andric 
swapStruct(struct objc_ivar_t & objc_ivar)41630b57cec5SDimitry Andric inline void swapStruct(struct objc_ivar_t &objc_ivar) {
41640b57cec5SDimitry Andric   sys::swapByteOrder(objc_ivar.ivar_name);
41650b57cec5SDimitry Andric   sys::swapByteOrder(objc_ivar.ivar_type);
41660b57cec5SDimitry Andric   sys::swapByteOrder(objc_ivar.ivar_offset);
41670b57cec5SDimitry Andric }
41680b57cec5SDimitry Andric 
swapStruct(struct objc_method_list_t & method_list)41690b57cec5SDimitry Andric inline void swapStruct(struct objc_method_list_t &method_list) {
41700b57cec5SDimitry Andric   sys::swapByteOrder(method_list.obsolete);
41710b57cec5SDimitry Andric   sys::swapByteOrder(method_list.method_count);
41720b57cec5SDimitry Andric }
41730b57cec5SDimitry Andric 
swapStruct(struct objc_method_t & method)41740b57cec5SDimitry Andric inline void swapStruct(struct objc_method_t &method) {
41750b57cec5SDimitry Andric   sys::swapByteOrder(method.method_name);
41760b57cec5SDimitry Andric   sys::swapByteOrder(method.method_types);
41770b57cec5SDimitry Andric   sys::swapByteOrder(method.method_imp);
41780b57cec5SDimitry Andric }
41790b57cec5SDimitry Andric 
swapStruct(struct objc_protocol_list_t & protocol_list)41800b57cec5SDimitry Andric inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
41810b57cec5SDimitry Andric   sys::swapByteOrder(protocol_list.next);
41820b57cec5SDimitry Andric   sys::swapByteOrder(protocol_list.count);
41830b57cec5SDimitry Andric }
41840b57cec5SDimitry Andric 
swapStruct(struct objc_protocol_t & protocol)41850b57cec5SDimitry Andric inline void swapStruct(struct objc_protocol_t &protocol) {
41860b57cec5SDimitry Andric   sys::swapByteOrder(protocol.isa);
41870b57cec5SDimitry Andric   sys::swapByteOrder(protocol.protocol_name);
41880b57cec5SDimitry Andric   sys::swapByteOrder(protocol.protocol_list);
41890b57cec5SDimitry Andric   sys::swapByteOrder(protocol.instance_methods);
41900b57cec5SDimitry Andric   sys::swapByteOrder(protocol.class_methods);
41910b57cec5SDimitry Andric }
41920b57cec5SDimitry Andric 
swapStruct(struct objc_method_description_list_t & mdl)41930b57cec5SDimitry Andric inline void swapStruct(struct objc_method_description_list_t &mdl) {
41940b57cec5SDimitry Andric   sys::swapByteOrder(mdl.count);
41950b57cec5SDimitry Andric }
41960b57cec5SDimitry Andric 
swapStruct(struct objc_method_description_t & md)41970b57cec5SDimitry Andric inline void swapStruct(struct objc_method_description_t &md) {
41980b57cec5SDimitry Andric   sys::swapByteOrder(md.name);
41990b57cec5SDimitry Andric   sys::swapByteOrder(md.types);
42000b57cec5SDimitry Andric }
42010b57cec5SDimitry Andric 
42025ffd83dbSDimitry Andric } // namespace
42035ffd83dbSDimitry Andric 
42040b57cec5SDimitry Andric static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
42050b57cec5SDimitry Andric                                                  struct DisassembleInfo *info);
42060b57cec5SDimitry Andric 
42070b57cec5SDimitry Andric // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
42080b57cec5SDimitry Andric // to an Objective-C class and returns the class name.  It is also passed the
42090b57cec5SDimitry Andric // address of the pointer, so when the pointer is zero as it can be in an .o
42100b57cec5SDimitry Andric // file, that is used to look for an external relocation entry with a symbol
42110b57cec5SDimitry Andric // name.
get_objc2_64bit_class_name(uint64_t pointer_value,uint64_t ReferenceValue,struct DisassembleInfo * info)42120b57cec5SDimitry Andric static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
42130b57cec5SDimitry Andric                                               uint64_t ReferenceValue,
42140b57cec5SDimitry Andric                                               struct DisassembleInfo *info) {
42150b57cec5SDimitry Andric   const char *r;
42160b57cec5SDimitry Andric   uint32_t offset, left;
42170b57cec5SDimitry Andric   SectionRef S;
42180b57cec5SDimitry Andric 
42190b57cec5SDimitry Andric   // The pointer_value can be 0 in an object file and have a relocation
42200b57cec5SDimitry Andric   // entry for the class symbol at the ReferenceValue (the address of the
42210b57cec5SDimitry Andric   // pointer).
42220b57cec5SDimitry Andric   if (pointer_value == 0) {
42230b57cec5SDimitry Andric     r = get_pointer_64(ReferenceValue, offset, left, S, info);
42240b57cec5SDimitry Andric     if (r == nullptr || left < sizeof(uint64_t))
42250b57cec5SDimitry Andric       return nullptr;
42260b57cec5SDimitry Andric     uint64_t n_value;
42270b57cec5SDimitry Andric     const char *symbol_name = get_symbol_64(offset, S, info, n_value);
42280b57cec5SDimitry Andric     if (symbol_name == nullptr)
42290b57cec5SDimitry Andric       return nullptr;
42300b57cec5SDimitry Andric     const char *class_name = strrchr(symbol_name, '$');
42310b57cec5SDimitry Andric     if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
42320b57cec5SDimitry Andric       return class_name + 2;
42330b57cec5SDimitry Andric     else
42340b57cec5SDimitry Andric       return nullptr;
42350b57cec5SDimitry Andric   }
42360b57cec5SDimitry Andric 
42370b57cec5SDimitry Andric   // The case were the pointer_value is non-zero and points to a class defined
42380b57cec5SDimitry Andric   // in this Mach-O file.
42390b57cec5SDimitry Andric   r = get_pointer_64(pointer_value, offset, left, S, info);
42400b57cec5SDimitry Andric   if (r == nullptr || left < sizeof(struct class64_t))
42410b57cec5SDimitry Andric     return nullptr;
42420b57cec5SDimitry Andric   struct class64_t c;
42430b57cec5SDimitry Andric   memcpy(&c, r, sizeof(struct class64_t));
42440b57cec5SDimitry Andric   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
42450b57cec5SDimitry Andric     swapStruct(c);
42460b57cec5SDimitry Andric   if (c.data == 0)
42470b57cec5SDimitry Andric     return nullptr;
42480b57cec5SDimitry Andric   r = get_pointer_64(c.data, offset, left, S, info);
42490b57cec5SDimitry Andric   if (r == nullptr || left < sizeof(struct class_ro64_t))
42500b57cec5SDimitry Andric     return nullptr;
42510b57cec5SDimitry Andric   struct class_ro64_t cro;
42520b57cec5SDimitry Andric   memcpy(&cro, r, sizeof(struct class_ro64_t));
42530b57cec5SDimitry Andric   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
42540b57cec5SDimitry Andric     swapStruct(cro);
42550b57cec5SDimitry Andric   if (cro.name == 0)
42560b57cec5SDimitry Andric     return nullptr;
42570b57cec5SDimitry Andric   const char *name = get_pointer_64(cro.name, offset, left, S, info);
42580b57cec5SDimitry Andric   return name;
42590b57cec5SDimitry Andric }
42600b57cec5SDimitry Andric 
42610b57cec5SDimitry Andric // get_objc2_64bit_cfstring_name is used for disassembly and is passed a
42620b57cec5SDimitry Andric // pointer to a cfstring and returns its name or nullptr.
get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,struct DisassembleInfo * info)42630b57cec5SDimitry Andric static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
42640b57cec5SDimitry Andric                                                  struct DisassembleInfo *info) {
42650b57cec5SDimitry Andric   const char *r, *name;
42660b57cec5SDimitry Andric   uint32_t offset, left;
42670b57cec5SDimitry Andric   SectionRef S;
42680b57cec5SDimitry Andric   struct cfstring64_t cfs;
42690b57cec5SDimitry Andric   uint64_t cfs_characters;
42700b57cec5SDimitry Andric 
42710b57cec5SDimitry Andric   r = get_pointer_64(ReferenceValue, offset, left, S, info);
42720b57cec5SDimitry Andric   if (r == nullptr || left < sizeof(struct cfstring64_t))
42730b57cec5SDimitry Andric     return nullptr;
42740b57cec5SDimitry Andric   memcpy(&cfs, r, sizeof(struct cfstring64_t));
42750b57cec5SDimitry Andric   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
42760b57cec5SDimitry Andric     swapStruct(cfs);
42770b57cec5SDimitry Andric   if (cfs.characters == 0) {
42780b57cec5SDimitry Andric     uint64_t n_value;
42790b57cec5SDimitry Andric     const char *symbol_name = get_symbol_64(
42800b57cec5SDimitry Andric         offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
42810b57cec5SDimitry Andric     if (symbol_name == nullptr)
42820b57cec5SDimitry Andric       return nullptr;
42830b57cec5SDimitry Andric     cfs_characters = n_value;
42840b57cec5SDimitry Andric   } else
42850b57cec5SDimitry Andric     cfs_characters = cfs.characters;
42860b57cec5SDimitry Andric   name = get_pointer_64(cfs_characters, offset, left, S, info);
42870b57cec5SDimitry Andric 
42880b57cec5SDimitry Andric   return name;
42890b57cec5SDimitry Andric }
42900b57cec5SDimitry Andric 
42910b57cec5SDimitry Andric // get_objc2_64bit_selref() is used for disassembly and is passed a the address
42920b57cec5SDimitry Andric // of a pointer to an Objective-C selector reference when the pointer value is
42930b57cec5SDimitry Andric // zero as in a .o file and is likely to have a external relocation entry with
42940b57cec5SDimitry Andric // who's symbol's n_value is the real pointer to the selector name.  If that is
42950b57cec5SDimitry Andric // the case the real pointer to the selector name is returned else 0 is
42960b57cec5SDimitry Andric // returned
get_objc2_64bit_selref(uint64_t ReferenceValue,struct DisassembleInfo * info)42970b57cec5SDimitry Andric static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
42980b57cec5SDimitry Andric                                        struct DisassembleInfo *info) {
42990b57cec5SDimitry Andric   uint32_t offset, left;
43000b57cec5SDimitry Andric   SectionRef S;
43010b57cec5SDimitry Andric 
43020b57cec5SDimitry Andric   const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
43030b57cec5SDimitry Andric   if (r == nullptr || left < sizeof(uint64_t))
43040b57cec5SDimitry Andric     return 0;
43050b57cec5SDimitry Andric   uint64_t n_value;
43060b57cec5SDimitry Andric   const char *symbol_name = get_symbol_64(offset, S, info, n_value);
43070b57cec5SDimitry Andric   if (symbol_name == nullptr)
43080b57cec5SDimitry Andric     return 0;
43090b57cec5SDimitry Andric   return n_value;
43100b57cec5SDimitry Andric }
43110b57cec5SDimitry Andric 
get_section(MachOObjectFile * O,const char * segname,const char * sectname)43120b57cec5SDimitry Andric static const SectionRef get_section(MachOObjectFile *O, const char *segname,
43130b57cec5SDimitry Andric                                     const char *sectname) {
43140b57cec5SDimitry Andric   for (const SectionRef &Section : O->sections()) {
43150b57cec5SDimitry Andric     StringRef SectName;
43168bcb0991SDimitry Andric     Expected<StringRef> SecNameOrErr = Section.getName();
43178bcb0991SDimitry Andric     if (SecNameOrErr)
43188bcb0991SDimitry Andric       SectName = *SecNameOrErr;
43198bcb0991SDimitry Andric     else
43208bcb0991SDimitry Andric       consumeError(SecNameOrErr.takeError());
43218bcb0991SDimitry Andric 
43220b57cec5SDimitry Andric     DataRefImpl Ref = Section.getRawDataRefImpl();
43230b57cec5SDimitry Andric     StringRef SegName = O->getSectionFinalSegmentName(Ref);
43240b57cec5SDimitry Andric     if (SegName == segname && SectName == sectname)
43250b57cec5SDimitry Andric       return Section;
43260b57cec5SDimitry Andric   }
43270b57cec5SDimitry Andric   return SectionRef();
43280b57cec5SDimitry Andric }
43290b57cec5SDimitry Andric 
43300b57cec5SDimitry Andric static void
walk_pointer_list_64(const char * listname,const SectionRef S,MachOObjectFile * O,struct DisassembleInfo * info,void (* func)(uint64_t,struct DisassembleInfo * info))43310b57cec5SDimitry Andric walk_pointer_list_64(const char *listname, const SectionRef S,
43320b57cec5SDimitry Andric                      MachOObjectFile *O, struct DisassembleInfo *info,
43330b57cec5SDimitry Andric                      void (*func)(uint64_t, struct DisassembleInfo *info)) {
43340b57cec5SDimitry Andric   if (S == SectionRef())
43350b57cec5SDimitry Andric     return;
43360b57cec5SDimitry Andric 
43370b57cec5SDimitry Andric   StringRef SectName;
43388bcb0991SDimitry Andric   Expected<StringRef> SecNameOrErr = S.getName();
43398bcb0991SDimitry Andric   if (SecNameOrErr)
43408bcb0991SDimitry Andric     SectName = *SecNameOrErr;
43418bcb0991SDimitry Andric   else
43428bcb0991SDimitry Andric     consumeError(SecNameOrErr.takeError());
43438bcb0991SDimitry Andric 
43440b57cec5SDimitry Andric   DataRefImpl Ref = S.getRawDataRefImpl();
43450b57cec5SDimitry Andric   StringRef SegName = O->getSectionFinalSegmentName(Ref);
43460b57cec5SDimitry Andric   outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
43470b57cec5SDimitry Andric 
43480b57cec5SDimitry Andric   StringRef BytesStr = unwrapOrError(S.getContents(), O->getFileName());
43490b57cec5SDimitry Andric   const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
43500b57cec5SDimitry Andric 
43510b57cec5SDimitry Andric   for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
43520b57cec5SDimitry Andric     uint32_t left = S.getSize() - i;
43530b57cec5SDimitry Andric     uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
43540b57cec5SDimitry Andric     uint64_t p = 0;
43550b57cec5SDimitry Andric     memcpy(&p, Contents + i, size);
43560b57cec5SDimitry Andric     if (i + sizeof(uint64_t) > S.getSize())
43570b57cec5SDimitry Andric       outs() << listname << " list pointer extends past end of (" << SegName
43580b57cec5SDimitry Andric              << "," << SectName << ") section\n";
43590b57cec5SDimitry Andric     outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
43600b57cec5SDimitry Andric 
43610b57cec5SDimitry Andric     if (O->isLittleEndian() != sys::IsLittleEndianHost)
43620b57cec5SDimitry Andric       sys::swapByteOrder(p);
43630b57cec5SDimitry Andric 
43640b57cec5SDimitry Andric     uint64_t n_value = 0;
43650b57cec5SDimitry Andric     const char *name = get_symbol_64(i, S, info, n_value, p);
43660b57cec5SDimitry Andric     if (name == nullptr)
43670b57cec5SDimitry Andric       name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
43680b57cec5SDimitry Andric 
43690b57cec5SDimitry Andric     if (n_value != 0) {
43700b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, n_value);
43710b57cec5SDimitry Andric       if (p != 0)
43720b57cec5SDimitry Andric         outs() << " + " << format("0x%" PRIx64, p);
43730b57cec5SDimitry Andric     } else
43740b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, p);
43750b57cec5SDimitry Andric     if (name != nullptr)
43760b57cec5SDimitry Andric       outs() << " " << name;
43770b57cec5SDimitry Andric     outs() << "\n";
43780b57cec5SDimitry Andric 
43790b57cec5SDimitry Andric     p += n_value;
43800b57cec5SDimitry Andric     if (func)
43810b57cec5SDimitry Andric       func(p, info);
43820b57cec5SDimitry Andric   }
43830b57cec5SDimitry Andric }
43840b57cec5SDimitry Andric 
43850b57cec5SDimitry Andric static void
walk_pointer_list_32(const char * listname,const SectionRef S,MachOObjectFile * O,struct DisassembleInfo * info,void (* func)(uint32_t,struct DisassembleInfo * info))43860b57cec5SDimitry Andric walk_pointer_list_32(const char *listname, const SectionRef S,
43870b57cec5SDimitry Andric                      MachOObjectFile *O, struct DisassembleInfo *info,
43880b57cec5SDimitry Andric                      void (*func)(uint32_t, struct DisassembleInfo *info)) {
43890b57cec5SDimitry Andric   if (S == SectionRef())
43900b57cec5SDimitry Andric     return;
43910b57cec5SDimitry Andric 
43928bcb0991SDimitry Andric   StringRef SectName = unwrapOrError(S.getName(), O->getFileName());
43930b57cec5SDimitry Andric   DataRefImpl Ref = S.getRawDataRefImpl();
43940b57cec5SDimitry Andric   StringRef SegName = O->getSectionFinalSegmentName(Ref);
43950b57cec5SDimitry Andric   outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
43960b57cec5SDimitry Andric 
43970b57cec5SDimitry Andric   StringRef BytesStr = unwrapOrError(S.getContents(), O->getFileName());
43980b57cec5SDimitry Andric   const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
43990b57cec5SDimitry Andric 
44000b57cec5SDimitry Andric   for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
44010b57cec5SDimitry Andric     uint32_t left = S.getSize() - i;
44020b57cec5SDimitry Andric     uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
44030b57cec5SDimitry Andric     uint32_t p = 0;
44040b57cec5SDimitry Andric     memcpy(&p, Contents + i, size);
44050b57cec5SDimitry Andric     if (i + sizeof(uint32_t) > S.getSize())
44060b57cec5SDimitry Andric       outs() << listname << " list pointer extends past end of (" << SegName
44070b57cec5SDimitry Andric              << "," << SectName << ") section\n";
44080b57cec5SDimitry Andric     uint32_t Address = S.getAddress() + i;
44090b57cec5SDimitry Andric     outs() << format("%08" PRIx32, Address) << " ";
44100b57cec5SDimitry Andric 
44110b57cec5SDimitry Andric     if (O->isLittleEndian() != sys::IsLittleEndianHost)
44120b57cec5SDimitry Andric       sys::swapByteOrder(p);
44130b57cec5SDimitry Andric     outs() << format("0x%" PRIx32, p);
44140b57cec5SDimitry Andric 
44150b57cec5SDimitry Andric     const char *name = get_symbol_32(i, S, info, p);
44160b57cec5SDimitry Andric     if (name != nullptr)
44170b57cec5SDimitry Andric       outs() << " " << name;
44180b57cec5SDimitry Andric     outs() << "\n";
44190b57cec5SDimitry Andric 
44200b57cec5SDimitry Andric     if (func)
44210b57cec5SDimitry Andric       func(p, info);
44220b57cec5SDimitry Andric   }
44230b57cec5SDimitry Andric }
44240b57cec5SDimitry Andric 
print_layout_map(const char * layout_map,uint32_t left)44250b57cec5SDimitry Andric static void print_layout_map(const char *layout_map, uint32_t left) {
44260b57cec5SDimitry Andric   if (layout_map == nullptr)
44270b57cec5SDimitry Andric     return;
44280b57cec5SDimitry Andric   outs() << "                layout map: ";
44290b57cec5SDimitry Andric   do {
44300b57cec5SDimitry Andric     outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
44310b57cec5SDimitry Andric     left--;
44320b57cec5SDimitry Andric     layout_map++;
44330b57cec5SDimitry Andric   } while (*layout_map != '\0' && left != 0);
44340b57cec5SDimitry Andric   outs() << "\n";
44350b57cec5SDimitry Andric }
44360b57cec5SDimitry Andric 
print_layout_map64(uint64_t p,struct DisassembleInfo * info)44370b57cec5SDimitry Andric static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
44380b57cec5SDimitry Andric   uint32_t offset, left;
44390b57cec5SDimitry Andric   SectionRef S;
44400b57cec5SDimitry Andric   const char *layout_map;
44410b57cec5SDimitry Andric 
44420b57cec5SDimitry Andric   if (p == 0)
44430b57cec5SDimitry Andric     return;
44440b57cec5SDimitry Andric   layout_map = get_pointer_64(p, offset, left, S, info);
44450b57cec5SDimitry Andric   print_layout_map(layout_map, left);
44460b57cec5SDimitry Andric }
44470b57cec5SDimitry Andric 
print_layout_map32(uint32_t p,struct DisassembleInfo * info)44480b57cec5SDimitry Andric static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
44490b57cec5SDimitry Andric   uint32_t offset, left;
44500b57cec5SDimitry Andric   SectionRef S;
44510b57cec5SDimitry Andric   const char *layout_map;
44520b57cec5SDimitry Andric 
44530b57cec5SDimitry Andric   if (p == 0)
44540b57cec5SDimitry Andric     return;
44550b57cec5SDimitry Andric   layout_map = get_pointer_32(p, offset, left, S, info);
44560b57cec5SDimitry Andric   print_layout_map(layout_map, left);
44570b57cec5SDimitry Andric }
44580b57cec5SDimitry Andric 
print_relative_method_list(uint32_t structSizeAndFlags,uint32_t structCount,uint64_t p,struct DisassembleInfo * info,const char * indent,uint32_t pointerBits)4459*0fca6ea1SDimitry Andric static void print_relative_method_list(uint32_t structSizeAndFlags,
4460*0fca6ea1SDimitry Andric                                        uint32_t structCount, uint64_t p,
4461*0fca6ea1SDimitry Andric                                        struct DisassembleInfo *info,
4462*0fca6ea1SDimitry Andric                                        const char *indent,
4463*0fca6ea1SDimitry Andric                                        uint32_t pointerBits) {
4464*0fca6ea1SDimitry Andric   struct method_relative_t m;
4465*0fca6ea1SDimitry Andric   const char *r, *name;
4466*0fca6ea1SDimitry Andric   uint32_t offset, xoffset, left, i;
4467*0fca6ea1SDimitry Andric   SectionRef S, xS;
4468*0fca6ea1SDimitry Andric 
4469*0fca6ea1SDimitry Andric   assert(((structSizeAndFlags & ML_HAS_RELATIVE_PTRS) != 0) &&
4470*0fca6ea1SDimitry Andric          "expected structSizeAndFlags to have ML_HAS_RELATIVE_PTRS flag");
4471*0fca6ea1SDimitry Andric 
4472*0fca6ea1SDimitry Andric   outs() << indent << "\t\t   entsize "
4473*0fca6ea1SDimitry Andric          << (structSizeAndFlags & ML_ENTSIZE_MASK) << " (relative) \n";
4474*0fca6ea1SDimitry Andric   outs() << indent << "\t\t     count " << structCount << "\n";
4475*0fca6ea1SDimitry Andric 
4476*0fca6ea1SDimitry Andric   for (i = 0; i < structCount; i++) {
4477*0fca6ea1SDimitry Andric     r = get_pointer_64(p, offset, left, S, info);
4478*0fca6ea1SDimitry Andric     memset(&m, '\0', sizeof(struct method_relative_t));
4479*0fca6ea1SDimitry Andric     if (left < sizeof(struct method_relative_t)) {
4480*0fca6ea1SDimitry Andric       memcpy(&m, r, left);
4481*0fca6ea1SDimitry Andric       outs() << indent << "   (method_t extends past the end of the section)\n";
4482*0fca6ea1SDimitry Andric     } else
4483*0fca6ea1SDimitry Andric       memcpy(&m, r, sizeof(struct method_relative_t));
4484*0fca6ea1SDimitry Andric     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4485*0fca6ea1SDimitry Andric       swapStruct(m);
4486*0fca6ea1SDimitry Andric 
4487*0fca6ea1SDimitry Andric     outs() << indent << "\t\t      name " << format("0x%" PRIx32, m.name);
4488*0fca6ea1SDimitry Andric     uint64_t relNameRefVA = p + offsetof(struct method_relative_t, name);
4489*0fca6ea1SDimitry Andric     uint64_t absNameRefVA = relNameRefVA + m.name;
4490*0fca6ea1SDimitry Andric     outs() << " (" << format("0x%" PRIx32, absNameRefVA) << ")";
4491*0fca6ea1SDimitry Andric 
4492*0fca6ea1SDimitry Andric     // since this is a relative list, absNameRefVA is the address of the
4493*0fca6ea1SDimitry Andric     // __objc_selrefs entry, so a pointer, not the actual name
4494*0fca6ea1SDimitry Andric     const char *nameRefPtr =
4495*0fca6ea1SDimitry Andric         get_pointer_64(absNameRefVA, xoffset, left, xS, info);
4496*0fca6ea1SDimitry Andric     if (nameRefPtr) {
4497*0fca6ea1SDimitry Andric       uint32_t pointerSize = pointerBits / CHAR_BIT;
4498*0fca6ea1SDimitry Andric       if (left < pointerSize)
4499*0fca6ea1SDimitry Andric         outs() << indent << " (nameRefPtr extends past the end of the section)";
4500*0fca6ea1SDimitry Andric       else {
4501*0fca6ea1SDimitry Andric         if (pointerSize == 64) {
4502*0fca6ea1SDimitry Andric           uint64_t nameOff_64 = *reinterpret_cast<const uint64_t *>(nameRefPtr);
4503*0fca6ea1SDimitry Andric           if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4504*0fca6ea1SDimitry Andric             sys::swapByteOrder(nameOff_64);
4505*0fca6ea1SDimitry Andric           name = get_pointer_64(nameOff_64, xoffset, left, xS, info);
4506*0fca6ea1SDimitry Andric         } else {
4507*0fca6ea1SDimitry Andric           uint32_t nameOff_32 = *reinterpret_cast<const uint32_t *>(nameRefPtr);
4508*0fca6ea1SDimitry Andric           if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4509*0fca6ea1SDimitry Andric             sys::swapByteOrder(nameOff_32);
4510*0fca6ea1SDimitry Andric           name = get_pointer_32(nameOff_32, xoffset, left, xS, info);
4511*0fca6ea1SDimitry Andric         }
4512*0fca6ea1SDimitry Andric         if (name != nullptr)
4513*0fca6ea1SDimitry Andric           outs() << format(" %.*s", left, name);
4514*0fca6ea1SDimitry Andric       }
4515*0fca6ea1SDimitry Andric     }
4516*0fca6ea1SDimitry Andric     outs() << "\n";
4517*0fca6ea1SDimitry Andric 
4518*0fca6ea1SDimitry Andric     outs() << indent << "\t\t     types " << format("0x%" PRIx32, m.types);
4519*0fca6ea1SDimitry Andric     uint64_t relTypesVA = p + offsetof(struct method_relative_t, types);
4520*0fca6ea1SDimitry Andric     uint64_t absTypesVA = relTypesVA + m.types;
4521*0fca6ea1SDimitry Andric     outs() << " (" << format("0x%" PRIx32, absTypesVA) << ")";
4522*0fca6ea1SDimitry Andric     name = get_pointer_32(absTypesVA, xoffset, left, xS, info);
4523*0fca6ea1SDimitry Andric     if (name != nullptr)
4524*0fca6ea1SDimitry Andric       outs() << format(" %.*s", left, name);
4525*0fca6ea1SDimitry Andric     outs() << "\n";
4526*0fca6ea1SDimitry Andric 
4527*0fca6ea1SDimitry Andric     outs() << indent << "\t\t       imp " << format("0x%" PRIx32, m.imp);
4528*0fca6ea1SDimitry Andric     uint64_t relImpVA = p + offsetof(struct method_relative_t, imp);
4529*0fca6ea1SDimitry Andric     uint64_t absImpVA = relImpVA + m.imp;
4530*0fca6ea1SDimitry Andric     outs() << " (" << format("0x%" PRIx32, absImpVA) << ")";
4531*0fca6ea1SDimitry Andric     name = GuessSymbolName(absImpVA, info->AddrMap);
4532*0fca6ea1SDimitry Andric     if (name != nullptr)
4533*0fca6ea1SDimitry Andric       outs() << " " << name;
4534*0fca6ea1SDimitry Andric     outs() << "\n";
4535*0fca6ea1SDimitry Andric 
4536*0fca6ea1SDimitry Andric     p += sizeof(struct method_relative_t);
4537*0fca6ea1SDimitry Andric     offset += sizeof(struct method_relative_t);
4538*0fca6ea1SDimitry Andric   }
4539*0fca6ea1SDimitry Andric }
4540*0fca6ea1SDimitry Andric 
print_method_list64_t(uint64_t p,struct DisassembleInfo * info,const char * indent)45410b57cec5SDimitry Andric static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
45420b57cec5SDimitry Andric                                   const char *indent) {
45430b57cec5SDimitry Andric   struct method_list64_t ml;
45440b57cec5SDimitry Andric   struct method64_t m;
45450b57cec5SDimitry Andric   const char *r;
45460b57cec5SDimitry Andric   uint32_t offset, xoffset, left, i;
45470b57cec5SDimitry Andric   SectionRef S, xS;
45480b57cec5SDimitry Andric   const char *name, *sym_name;
45490b57cec5SDimitry Andric   uint64_t n_value;
45500b57cec5SDimitry Andric 
45510b57cec5SDimitry Andric   r = get_pointer_64(p, offset, left, S, info);
45520b57cec5SDimitry Andric   if (r == nullptr)
45530b57cec5SDimitry Andric     return;
45540b57cec5SDimitry Andric   memset(&ml, '\0', sizeof(struct method_list64_t));
45550b57cec5SDimitry Andric   if (left < sizeof(struct method_list64_t)) {
45560b57cec5SDimitry Andric     memcpy(&ml, r, left);
45570b57cec5SDimitry Andric     outs() << "   (method_list_t entends past the end of the section)\n";
45580b57cec5SDimitry Andric   } else
45590b57cec5SDimitry Andric     memcpy(&ml, r, sizeof(struct method_list64_t));
45600b57cec5SDimitry Andric   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
45610b57cec5SDimitry Andric     swapStruct(ml);
4562*0fca6ea1SDimitry Andric   p += sizeof(struct method_list64_t);
4563*0fca6ea1SDimitry Andric 
4564*0fca6ea1SDimitry Andric   if ((ml.entsize & ML_HAS_RELATIVE_PTRS) != 0) {
4565*0fca6ea1SDimitry Andric     print_relative_method_list(ml.entsize, ml.count, p, info, indent,
4566*0fca6ea1SDimitry Andric                                /*pointerBits=*/64);
4567*0fca6ea1SDimitry Andric     return;
4568*0fca6ea1SDimitry Andric   }
4569*0fca6ea1SDimitry Andric 
45700b57cec5SDimitry Andric   outs() << indent << "\t\t   entsize " << ml.entsize << "\n";
45710b57cec5SDimitry Andric   outs() << indent << "\t\t     count " << ml.count << "\n";
45720b57cec5SDimitry Andric 
45730b57cec5SDimitry Andric   offset += sizeof(struct method_list64_t);
45740b57cec5SDimitry Andric   for (i = 0; i < ml.count; i++) {
45750b57cec5SDimitry Andric     r = get_pointer_64(p, offset, left, S, info);
45760b57cec5SDimitry Andric     if (r == nullptr)
45770b57cec5SDimitry Andric       return;
45780b57cec5SDimitry Andric     memset(&m, '\0', sizeof(struct method64_t));
45790b57cec5SDimitry Andric     if (left < sizeof(struct method64_t)) {
45800b57cec5SDimitry Andric       memcpy(&m, r, left);
45810b57cec5SDimitry Andric       outs() << indent << "   (method_t extends past the end of the section)\n";
45820b57cec5SDimitry Andric     } else
45830b57cec5SDimitry Andric       memcpy(&m, r, sizeof(struct method64_t));
45840b57cec5SDimitry Andric     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
45850b57cec5SDimitry Andric       swapStruct(m);
45860b57cec5SDimitry Andric 
45870b57cec5SDimitry Andric     outs() << indent << "\t\t      name ";
45880b57cec5SDimitry Andric     sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
45890b57cec5SDimitry Andric                              info, n_value, m.name);
45900b57cec5SDimitry Andric     if (n_value != 0) {
45910b57cec5SDimitry Andric       if (info->verbose && sym_name != nullptr)
45920b57cec5SDimitry Andric         outs() << sym_name;
45930b57cec5SDimitry Andric       else
45940b57cec5SDimitry Andric         outs() << format("0x%" PRIx64, n_value);
45950b57cec5SDimitry Andric       if (m.name != 0)
45960b57cec5SDimitry Andric         outs() << " + " << format("0x%" PRIx64, m.name);
45970b57cec5SDimitry Andric     } else
45980b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, m.name);
45990b57cec5SDimitry Andric     name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
46000b57cec5SDimitry Andric     if (name != nullptr)
46010b57cec5SDimitry Andric       outs() << format(" %.*s", left, name);
46020b57cec5SDimitry Andric     outs() << "\n";
46030b57cec5SDimitry Andric 
46040b57cec5SDimitry Andric     outs() << indent << "\t\t     types ";
46050b57cec5SDimitry Andric     sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
46060b57cec5SDimitry Andric                              info, n_value, m.types);
46070b57cec5SDimitry Andric     if (n_value != 0) {
46080b57cec5SDimitry Andric       if (info->verbose && sym_name != nullptr)
46090b57cec5SDimitry Andric         outs() << sym_name;
46100b57cec5SDimitry Andric       else
46110b57cec5SDimitry Andric         outs() << format("0x%" PRIx64, n_value);
46120b57cec5SDimitry Andric       if (m.types != 0)
46130b57cec5SDimitry Andric         outs() << " + " << format("0x%" PRIx64, m.types);
46140b57cec5SDimitry Andric     } else
46150b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, m.types);
46160b57cec5SDimitry Andric     name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
46170b57cec5SDimitry Andric     if (name != nullptr)
46180b57cec5SDimitry Andric       outs() << format(" %.*s", left, name);
46190b57cec5SDimitry Andric     outs() << "\n";
46200b57cec5SDimitry Andric 
46210b57cec5SDimitry Andric     outs() << indent << "\t\t       imp ";
46220b57cec5SDimitry Andric     name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
46230b57cec5SDimitry Andric                          n_value, m.imp);
46240b57cec5SDimitry Andric     if (info->verbose && name == nullptr) {
46250b57cec5SDimitry Andric       if (n_value != 0) {
46260b57cec5SDimitry Andric         outs() << format("0x%" PRIx64, n_value) << " ";
46270b57cec5SDimitry Andric         if (m.imp != 0)
46280b57cec5SDimitry Andric           outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
46290b57cec5SDimitry Andric       } else
46300b57cec5SDimitry Andric         outs() << format("0x%" PRIx64, m.imp) << " ";
46310b57cec5SDimitry Andric     }
46320b57cec5SDimitry Andric     if (name != nullptr)
46330b57cec5SDimitry Andric       outs() << name;
46340b57cec5SDimitry Andric     outs() << "\n";
46350b57cec5SDimitry Andric 
46360b57cec5SDimitry Andric     p += sizeof(struct method64_t);
46370b57cec5SDimitry Andric     offset += sizeof(struct method64_t);
46380b57cec5SDimitry Andric   }
46390b57cec5SDimitry Andric }
46400b57cec5SDimitry Andric 
print_method_list32_t(uint64_t p,struct DisassembleInfo * info,const char * indent)46410b57cec5SDimitry Andric static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
46420b57cec5SDimitry Andric                                   const char *indent) {
46430b57cec5SDimitry Andric   struct method_list32_t ml;
46440b57cec5SDimitry Andric   struct method32_t m;
46450b57cec5SDimitry Andric   const char *r, *name;
46460b57cec5SDimitry Andric   uint32_t offset, xoffset, left, i;
46470b57cec5SDimitry Andric   SectionRef S, xS;
46480b57cec5SDimitry Andric 
46490b57cec5SDimitry Andric   r = get_pointer_32(p, offset, left, S, info);
46500b57cec5SDimitry Andric   if (r == nullptr)
46510b57cec5SDimitry Andric     return;
46520b57cec5SDimitry Andric   memset(&ml, '\0', sizeof(struct method_list32_t));
46530b57cec5SDimitry Andric   if (left < sizeof(struct method_list32_t)) {
46540b57cec5SDimitry Andric     memcpy(&ml, r, left);
46550b57cec5SDimitry Andric     outs() << "   (method_list_t entends past the end of the section)\n";
46560b57cec5SDimitry Andric   } else
46570b57cec5SDimitry Andric     memcpy(&ml, r, sizeof(struct method_list32_t));
46580b57cec5SDimitry Andric   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
46590b57cec5SDimitry Andric     swapStruct(ml);
4660*0fca6ea1SDimitry Andric   p += sizeof(struct method_list32_t);
4661*0fca6ea1SDimitry Andric 
4662*0fca6ea1SDimitry Andric   if ((ml.entsize & ML_HAS_RELATIVE_PTRS) != 0) {
4663*0fca6ea1SDimitry Andric     print_relative_method_list(ml.entsize, ml.count, p, info, indent,
4664*0fca6ea1SDimitry Andric                                /*pointerBits=*/32);
4665*0fca6ea1SDimitry Andric     return;
4666*0fca6ea1SDimitry Andric   }
4667*0fca6ea1SDimitry Andric 
46680b57cec5SDimitry Andric   outs() << indent << "\t\t   entsize " << ml.entsize << "\n";
46690b57cec5SDimitry Andric   outs() << indent << "\t\t     count " << ml.count << "\n";
46700b57cec5SDimitry Andric 
46710b57cec5SDimitry Andric   offset += sizeof(struct method_list32_t);
46720b57cec5SDimitry Andric   for (i = 0; i < ml.count; i++) {
46730b57cec5SDimitry Andric     r = get_pointer_32(p, offset, left, S, info);
46740b57cec5SDimitry Andric     if (r == nullptr)
46750b57cec5SDimitry Andric       return;
46760b57cec5SDimitry Andric     memset(&m, '\0', sizeof(struct method32_t));
46770b57cec5SDimitry Andric     if (left < sizeof(struct method32_t)) {
46780b57cec5SDimitry Andric       memcpy(&ml, r, left);
46790b57cec5SDimitry Andric       outs() << indent << "   (method_t entends past the end of the section)\n";
46800b57cec5SDimitry Andric     } else
46810b57cec5SDimitry Andric       memcpy(&m, r, sizeof(struct method32_t));
46820b57cec5SDimitry Andric     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
46830b57cec5SDimitry Andric       swapStruct(m);
46840b57cec5SDimitry Andric 
46850b57cec5SDimitry Andric     outs() << indent << "\t\t      name " << format("0x%" PRIx32, m.name);
46860b57cec5SDimitry Andric     name = get_pointer_32(m.name, xoffset, left, xS, info);
46870b57cec5SDimitry Andric     if (name != nullptr)
46880b57cec5SDimitry Andric       outs() << format(" %.*s", left, name);
46890b57cec5SDimitry Andric     outs() << "\n";
46900b57cec5SDimitry Andric 
46910b57cec5SDimitry Andric     outs() << indent << "\t\t     types " << format("0x%" PRIx32, m.types);
46920b57cec5SDimitry Andric     name = get_pointer_32(m.types, xoffset, left, xS, info);
46930b57cec5SDimitry Andric     if (name != nullptr)
46940b57cec5SDimitry Andric       outs() << format(" %.*s", left, name);
46950b57cec5SDimitry Andric     outs() << "\n";
46960b57cec5SDimitry Andric 
46970b57cec5SDimitry Andric     outs() << indent << "\t\t       imp " << format("0x%" PRIx32, m.imp);
46980b57cec5SDimitry Andric     name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
46990b57cec5SDimitry Andric                          m.imp);
47000b57cec5SDimitry Andric     if (name != nullptr)
47010b57cec5SDimitry Andric       outs() << " " << name;
47020b57cec5SDimitry Andric     outs() << "\n";
47030b57cec5SDimitry Andric 
47040b57cec5SDimitry Andric     p += sizeof(struct method32_t);
47050b57cec5SDimitry Andric     offset += sizeof(struct method32_t);
47060b57cec5SDimitry Andric   }
47070b57cec5SDimitry Andric }
47080b57cec5SDimitry Andric 
print_method_list(uint32_t p,struct DisassembleInfo * info)47090b57cec5SDimitry Andric static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
47100b57cec5SDimitry Andric   uint32_t offset, left, xleft;
47110b57cec5SDimitry Andric   SectionRef S;
47120b57cec5SDimitry Andric   struct objc_method_list_t method_list;
47130b57cec5SDimitry Andric   struct objc_method_t method;
47140b57cec5SDimitry Andric   const char *r, *methods, *name, *SymbolName;
47150b57cec5SDimitry Andric   int32_t i;
47160b57cec5SDimitry Andric 
47170b57cec5SDimitry Andric   r = get_pointer_32(p, offset, left, S, info, true);
47180b57cec5SDimitry Andric   if (r == nullptr)
47190b57cec5SDimitry Andric     return true;
47200b57cec5SDimitry Andric 
47210b57cec5SDimitry Andric   outs() << "\n";
47220b57cec5SDimitry Andric   if (left > sizeof(struct objc_method_list_t)) {
47230b57cec5SDimitry Andric     memcpy(&method_list, r, sizeof(struct objc_method_list_t));
47240b57cec5SDimitry Andric   } else {
47250b57cec5SDimitry Andric     outs() << "\t\t objc_method_list extends past end of the section\n";
47260b57cec5SDimitry Andric     memset(&method_list, '\0', sizeof(struct objc_method_list_t));
47270b57cec5SDimitry Andric     memcpy(&method_list, r, left);
47280b57cec5SDimitry Andric   }
47290b57cec5SDimitry Andric   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
47300b57cec5SDimitry Andric     swapStruct(method_list);
47310b57cec5SDimitry Andric 
47320b57cec5SDimitry Andric   outs() << "\t\t         obsolete "
47330b57cec5SDimitry Andric          << format("0x%08" PRIx32, method_list.obsolete) << "\n";
47340b57cec5SDimitry Andric   outs() << "\t\t     method_count " << method_list.method_count << "\n";
47350b57cec5SDimitry Andric 
47360b57cec5SDimitry Andric   methods = r + sizeof(struct objc_method_list_t);
47370b57cec5SDimitry Andric   for (i = 0; i < method_list.method_count; i++) {
47380b57cec5SDimitry Andric     if ((i + 1) * sizeof(struct objc_method_t) > left) {
47390b57cec5SDimitry Andric       outs() << "\t\t remaining method's extend past the of the section\n";
47400b57cec5SDimitry Andric       break;
47410b57cec5SDimitry Andric     }
47420b57cec5SDimitry Andric     memcpy(&method, methods + i * sizeof(struct objc_method_t),
47430b57cec5SDimitry Andric            sizeof(struct objc_method_t));
47440b57cec5SDimitry Andric     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
47450b57cec5SDimitry Andric       swapStruct(method);
47460b57cec5SDimitry Andric 
47470b57cec5SDimitry Andric     outs() << "\t\t      method_name "
47480b57cec5SDimitry Andric            << format("0x%08" PRIx32, method.method_name);
47490b57cec5SDimitry Andric     if (info->verbose) {
47500b57cec5SDimitry Andric       name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
47510b57cec5SDimitry Andric       if (name != nullptr)
47520b57cec5SDimitry Andric         outs() << format(" %.*s", xleft, name);
47530b57cec5SDimitry Andric       else
47540b57cec5SDimitry Andric         outs() << " (not in an __OBJC section)";
47550b57cec5SDimitry Andric     }
47560b57cec5SDimitry Andric     outs() << "\n";
47570b57cec5SDimitry Andric 
47580b57cec5SDimitry Andric     outs() << "\t\t     method_types "
47590b57cec5SDimitry Andric            << format("0x%08" PRIx32, method.method_types);
47600b57cec5SDimitry Andric     if (info->verbose) {
47610b57cec5SDimitry Andric       name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
47620b57cec5SDimitry Andric       if (name != nullptr)
47630b57cec5SDimitry Andric         outs() << format(" %.*s", xleft, name);
47640b57cec5SDimitry Andric       else
47650b57cec5SDimitry Andric         outs() << " (not in an __OBJC section)";
47660b57cec5SDimitry Andric     }
47670b57cec5SDimitry Andric     outs() << "\n";
47680b57cec5SDimitry Andric 
47690b57cec5SDimitry Andric     outs() << "\t\t       method_imp "
47700b57cec5SDimitry Andric            << format("0x%08" PRIx32, method.method_imp) << " ";
47710b57cec5SDimitry Andric     if (info->verbose) {
47720b57cec5SDimitry Andric       SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
47730b57cec5SDimitry Andric       if (SymbolName != nullptr)
47740b57cec5SDimitry Andric         outs() << SymbolName;
47750b57cec5SDimitry Andric     }
47760b57cec5SDimitry Andric     outs() << "\n";
47770b57cec5SDimitry Andric   }
47780b57cec5SDimitry Andric   return false;
47790b57cec5SDimitry Andric }
47800b57cec5SDimitry Andric 
print_protocol_list64_t(uint64_t p,struct DisassembleInfo * info)47810b57cec5SDimitry Andric static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
47820b57cec5SDimitry Andric   struct protocol_list64_t pl;
47830b57cec5SDimitry Andric   uint64_t q, n_value;
47840b57cec5SDimitry Andric   struct protocol64_t pc;
47850b57cec5SDimitry Andric   const char *r;
47860b57cec5SDimitry Andric   uint32_t offset, xoffset, left, i;
47870b57cec5SDimitry Andric   SectionRef S, xS;
47880b57cec5SDimitry Andric   const char *name, *sym_name;
47890b57cec5SDimitry Andric 
47900b57cec5SDimitry Andric   r = get_pointer_64(p, offset, left, S, info);
47910b57cec5SDimitry Andric   if (r == nullptr)
47920b57cec5SDimitry Andric     return;
47930b57cec5SDimitry Andric   memset(&pl, '\0', sizeof(struct protocol_list64_t));
47940b57cec5SDimitry Andric   if (left < sizeof(struct protocol_list64_t)) {
47950b57cec5SDimitry Andric     memcpy(&pl, r, left);
47960b57cec5SDimitry Andric     outs() << "   (protocol_list_t entends past the end of the section)\n";
47970b57cec5SDimitry Andric   } else
47980b57cec5SDimitry Andric     memcpy(&pl, r, sizeof(struct protocol_list64_t));
47990b57cec5SDimitry Andric   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
48000b57cec5SDimitry Andric     swapStruct(pl);
48010b57cec5SDimitry Andric   outs() << "                      count " << pl.count << "\n";
48020b57cec5SDimitry Andric 
48030b57cec5SDimitry Andric   p += sizeof(struct protocol_list64_t);
48040b57cec5SDimitry Andric   offset += sizeof(struct protocol_list64_t);
48050b57cec5SDimitry Andric   for (i = 0; i < pl.count; i++) {
48060b57cec5SDimitry Andric     r = get_pointer_64(p, offset, left, S, info);
48070b57cec5SDimitry Andric     if (r == nullptr)
48080b57cec5SDimitry Andric       return;
48090b57cec5SDimitry Andric     q = 0;
48100b57cec5SDimitry Andric     if (left < sizeof(uint64_t)) {
48110b57cec5SDimitry Andric       memcpy(&q, r, left);
48120b57cec5SDimitry Andric       outs() << "   (protocol_t * entends past the end of the section)\n";
48130b57cec5SDimitry Andric     } else
48140b57cec5SDimitry Andric       memcpy(&q, r, sizeof(uint64_t));
48150b57cec5SDimitry Andric     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
48160b57cec5SDimitry Andric       sys::swapByteOrder(q);
48170b57cec5SDimitry Andric 
48180b57cec5SDimitry Andric     outs() << "\t\t      list[" << i << "] ";
48190b57cec5SDimitry Andric     sym_name = get_symbol_64(offset, S, info, n_value, q);
48200b57cec5SDimitry Andric     if (n_value != 0) {
48210b57cec5SDimitry Andric       if (info->verbose && sym_name != nullptr)
48220b57cec5SDimitry Andric         outs() << sym_name;
48230b57cec5SDimitry Andric       else
48240b57cec5SDimitry Andric         outs() << format("0x%" PRIx64, n_value);
48250b57cec5SDimitry Andric       if (q != 0)
48260b57cec5SDimitry Andric         outs() << " + " << format("0x%" PRIx64, q);
48270b57cec5SDimitry Andric     } else
48280b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, q);
48290b57cec5SDimitry Andric     outs() << " (struct protocol_t *)\n";
48300b57cec5SDimitry Andric 
48310b57cec5SDimitry Andric     r = get_pointer_64(q + n_value, offset, left, S, info);
48320b57cec5SDimitry Andric     if (r == nullptr)
48330b57cec5SDimitry Andric       return;
48340b57cec5SDimitry Andric     memset(&pc, '\0', sizeof(struct protocol64_t));
48350b57cec5SDimitry Andric     if (left < sizeof(struct protocol64_t)) {
48360b57cec5SDimitry Andric       memcpy(&pc, r, left);
48370b57cec5SDimitry Andric       outs() << "   (protocol_t entends past the end of the section)\n";
48380b57cec5SDimitry Andric     } else
48390b57cec5SDimitry Andric       memcpy(&pc, r, sizeof(struct protocol64_t));
48400b57cec5SDimitry Andric     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
48410b57cec5SDimitry Andric       swapStruct(pc);
48420b57cec5SDimitry Andric 
48430b57cec5SDimitry Andric     outs() << "\t\t\t      isa " << format("0x%" PRIx64, pc.isa) << "\n";
48440b57cec5SDimitry Andric 
48450b57cec5SDimitry Andric     outs() << "\t\t\t     name ";
48460b57cec5SDimitry Andric     sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
48470b57cec5SDimitry Andric                              info, n_value, pc.name);
48480b57cec5SDimitry Andric     if (n_value != 0) {
48490b57cec5SDimitry Andric       if (info->verbose && sym_name != nullptr)
48500b57cec5SDimitry Andric         outs() << sym_name;
48510b57cec5SDimitry Andric       else
48520b57cec5SDimitry Andric         outs() << format("0x%" PRIx64, n_value);
48530b57cec5SDimitry Andric       if (pc.name != 0)
48540b57cec5SDimitry Andric         outs() << " + " << format("0x%" PRIx64, pc.name);
48550b57cec5SDimitry Andric     } else
48560b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, pc.name);
48570b57cec5SDimitry Andric     name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
48580b57cec5SDimitry Andric     if (name != nullptr)
48590b57cec5SDimitry Andric       outs() << format(" %.*s", left, name);
48600b57cec5SDimitry Andric     outs() << "\n";
48610b57cec5SDimitry Andric 
48620b57cec5SDimitry Andric     outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
48630b57cec5SDimitry Andric 
48640b57cec5SDimitry Andric     outs() << "\t\t  instanceMethods ";
48650b57cec5SDimitry Andric     sym_name =
48660b57cec5SDimitry Andric         get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
48670b57cec5SDimitry Andric                       S, info, n_value, pc.instanceMethods);
48680b57cec5SDimitry Andric     if (n_value != 0) {
48690b57cec5SDimitry Andric       if (info->verbose && sym_name != nullptr)
48700b57cec5SDimitry Andric         outs() << sym_name;
48710b57cec5SDimitry Andric       else
48720b57cec5SDimitry Andric         outs() << format("0x%" PRIx64, n_value);
48730b57cec5SDimitry Andric       if (pc.instanceMethods != 0)
48740b57cec5SDimitry Andric         outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
48750b57cec5SDimitry Andric     } else
48760b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, pc.instanceMethods);
48770b57cec5SDimitry Andric     outs() << " (struct method_list_t *)\n";
48780b57cec5SDimitry Andric     if (pc.instanceMethods + n_value != 0)
48790b57cec5SDimitry Andric       print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
48800b57cec5SDimitry Andric 
48810b57cec5SDimitry Andric     outs() << "\t\t     classMethods ";
48820b57cec5SDimitry Andric     sym_name =
48830b57cec5SDimitry Andric         get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
48840b57cec5SDimitry Andric                       info, n_value, pc.classMethods);
48850b57cec5SDimitry Andric     if (n_value != 0) {
48860b57cec5SDimitry Andric       if (info->verbose && sym_name != nullptr)
48870b57cec5SDimitry Andric         outs() << sym_name;
48880b57cec5SDimitry Andric       else
48890b57cec5SDimitry Andric         outs() << format("0x%" PRIx64, n_value);
48900b57cec5SDimitry Andric       if (pc.classMethods != 0)
48910b57cec5SDimitry Andric         outs() << " + " << format("0x%" PRIx64, pc.classMethods);
48920b57cec5SDimitry Andric     } else
48930b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, pc.classMethods);
48940b57cec5SDimitry Andric     outs() << " (struct method_list_t *)\n";
48950b57cec5SDimitry Andric     if (pc.classMethods + n_value != 0)
48960b57cec5SDimitry Andric       print_method_list64_t(pc.classMethods + n_value, info, "\t");
48970b57cec5SDimitry Andric 
48980b57cec5SDimitry Andric     outs() << "\t  optionalInstanceMethods "
48990b57cec5SDimitry Andric            << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
49000b57cec5SDimitry Andric     outs() << "\t     optionalClassMethods "
49010b57cec5SDimitry Andric            << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
49020b57cec5SDimitry Andric     outs() << "\t       instanceProperties "
49030b57cec5SDimitry Andric            << format("0x%" PRIx64, pc.instanceProperties) << "\n";
49040b57cec5SDimitry Andric 
49050b57cec5SDimitry Andric     p += sizeof(uint64_t);
49060b57cec5SDimitry Andric     offset += sizeof(uint64_t);
49070b57cec5SDimitry Andric   }
49080b57cec5SDimitry Andric }
49090b57cec5SDimitry Andric 
print_protocol_list32_t(uint32_t p,struct DisassembleInfo * info)49100b57cec5SDimitry Andric static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
49110b57cec5SDimitry Andric   struct protocol_list32_t pl;
49120b57cec5SDimitry Andric   uint32_t q;
49130b57cec5SDimitry Andric   struct protocol32_t pc;
49140b57cec5SDimitry Andric   const char *r;
49150b57cec5SDimitry Andric   uint32_t offset, xoffset, left, i;
49160b57cec5SDimitry Andric   SectionRef S, xS;
49170b57cec5SDimitry Andric   const char *name;
49180b57cec5SDimitry Andric 
49190b57cec5SDimitry Andric   r = get_pointer_32(p, offset, left, S, info);
49200b57cec5SDimitry Andric   if (r == nullptr)
49210b57cec5SDimitry Andric     return;
49220b57cec5SDimitry Andric   memset(&pl, '\0', sizeof(struct protocol_list32_t));
49230b57cec5SDimitry Andric   if (left < sizeof(struct protocol_list32_t)) {
49240b57cec5SDimitry Andric     memcpy(&pl, r, left);
49250b57cec5SDimitry Andric     outs() << "   (protocol_list_t entends past the end of the section)\n";
49260b57cec5SDimitry Andric   } else
49270b57cec5SDimitry Andric     memcpy(&pl, r, sizeof(struct protocol_list32_t));
49280b57cec5SDimitry Andric   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
49290b57cec5SDimitry Andric     swapStruct(pl);
49300b57cec5SDimitry Andric   outs() << "                      count " << pl.count << "\n";
49310b57cec5SDimitry Andric 
49320b57cec5SDimitry Andric   p += sizeof(struct protocol_list32_t);
49330b57cec5SDimitry Andric   offset += sizeof(struct protocol_list32_t);
49340b57cec5SDimitry Andric   for (i = 0; i < pl.count; i++) {
49350b57cec5SDimitry Andric     r = get_pointer_32(p, offset, left, S, info);
49360b57cec5SDimitry Andric     if (r == nullptr)
49370b57cec5SDimitry Andric       return;
49380b57cec5SDimitry Andric     q = 0;
49390b57cec5SDimitry Andric     if (left < sizeof(uint32_t)) {
49400b57cec5SDimitry Andric       memcpy(&q, r, left);
49410b57cec5SDimitry Andric       outs() << "   (protocol_t * entends past the end of the section)\n";
49420b57cec5SDimitry Andric     } else
49430b57cec5SDimitry Andric       memcpy(&q, r, sizeof(uint32_t));
49440b57cec5SDimitry Andric     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
49450b57cec5SDimitry Andric       sys::swapByteOrder(q);
49460b57cec5SDimitry Andric     outs() << "\t\t      list[" << i << "] " << format("0x%" PRIx32, q)
49470b57cec5SDimitry Andric            << " (struct protocol_t *)\n";
49480b57cec5SDimitry Andric     r = get_pointer_32(q, offset, left, S, info);
49490b57cec5SDimitry Andric     if (r == nullptr)
49500b57cec5SDimitry Andric       return;
49510b57cec5SDimitry Andric     memset(&pc, '\0', sizeof(struct protocol32_t));
49520b57cec5SDimitry Andric     if (left < sizeof(struct protocol32_t)) {
49530b57cec5SDimitry Andric       memcpy(&pc, r, left);
49540b57cec5SDimitry Andric       outs() << "   (protocol_t entends past the end of the section)\n";
49550b57cec5SDimitry Andric     } else
49560b57cec5SDimitry Andric       memcpy(&pc, r, sizeof(struct protocol32_t));
49570b57cec5SDimitry Andric     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
49580b57cec5SDimitry Andric       swapStruct(pc);
49590b57cec5SDimitry Andric     outs() << "\t\t\t      isa " << format("0x%" PRIx32, pc.isa) << "\n";
49600b57cec5SDimitry Andric     outs() << "\t\t\t     name " << format("0x%" PRIx32, pc.name);
49610b57cec5SDimitry Andric     name = get_pointer_32(pc.name, xoffset, left, xS, info);
49620b57cec5SDimitry Andric     if (name != nullptr)
49630b57cec5SDimitry Andric       outs() << format(" %.*s", left, name);
49640b57cec5SDimitry Andric     outs() << "\n";
49650b57cec5SDimitry Andric     outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
49660b57cec5SDimitry Andric     outs() << "\t\t  instanceMethods "
49670b57cec5SDimitry Andric            << format("0x%" PRIx32, pc.instanceMethods)
49680b57cec5SDimitry Andric            << " (struct method_list_t *)\n";
49690b57cec5SDimitry Andric     if (pc.instanceMethods != 0)
49700b57cec5SDimitry Andric       print_method_list32_t(pc.instanceMethods, info, "\t");
49710b57cec5SDimitry Andric     outs() << "\t\t     classMethods " << format("0x%" PRIx32, pc.classMethods)
49720b57cec5SDimitry Andric            << " (struct method_list_t *)\n";
49730b57cec5SDimitry Andric     if (pc.classMethods != 0)
49740b57cec5SDimitry Andric       print_method_list32_t(pc.classMethods, info, "\t");
49750b57cec5SDimitry Andric     outs() << "\t  optionalInstanceMethods "
49760b57cec5SDimitry Andric            << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
49770b57cec5SDimitry Andric     outs() << "\t     optionalClassMethods "
49780b57cec5SDimitry Andric            << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
49790b57cec5SDimitry Andric     outs() << "\t       instanceProperties "
49800b57cec5SDimitry Andric            << format("0x%" PRIx32, pc.instanceProperties) << "\n";
49810b57cec5SDimitry Andric     p += sizeof(uint32_t);
49820b57cec5SDimitry Andric     offset += sizeof(uint32_t);
49830b57cec5SDimitry Andric   }
49840b57cec5SDimitry Andric }
49850b57cec5SDimitry Andric 
print_indent(uint32_t indent)49860b57cec5SDimitry Andric static void print_indent(uint32_t indent) {
49870b57cec5SDimitry Andric   for (uint32_t i = 0; i < indent;) {
49880b57cec5SDimitry Andric     if (indent - i >= 8) {
49890b57cec5SDimitry Andric       outs() << "\t";
49900b57cec5SDimitry Andric       i += 8;
49910b57cec5SDimitry Andric     } else {
49920b57cec5SDimitry Andric       for (uint32_t j = i; j < indent; j++)
49930b57cec5SDimitry Andric         outs() << " ";
49940b57cec5SDimitry Andric       return;
49950b57cec5SDimitry Andric     }
49960b57cec5SDimitry Andric   }
49970b57cec5SDimitry Andric }
49980b57cec5SDimitry Andric 
print_method_description_list(uint32_t p,uint32_t indent,struct DisassembleInfo * info)49990b57cec5SDimitry Andric static bool print_method_description_list(uint32_t p, uint32_t indent,
50000b57cec5SDimitry Andric                                           struct DisassembleInfo *info) {
50010b57cec5SDimitry Andric   uint32_t offset, left, xleft;
50020b57cec5SDimitry Andric   SectionRef S;
50030b57cec5SDimitry Andric   struct objc_method_description_list_t mdl;
50040b57cec5SDimitry Andric   struct objc_method_description_t md;
50050b57cec5SDimitry Andric   const char *r, *list, *name;
50060b57cec5SDimitry Andric   int32_t i;
50070b57cec5SDimitry Andric 
50080b57cec5SDimitry Andric   r = get_pointer_32(p, offset, left, S, info, true);
50090b57cec5SDimitry Andric   if (r == nullptr)
50100b57cec5SDimitry Andric     return true;
50110b57cec5SDimitry Andric 
50120b57cec5SDimitry Andric   outs() << "\n";
50130b57cec5SDimitry Andric   if (left > sizeof(struct objc_method_description_list_t)) {
50140b57cec5SDimitry Andric     memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
50150b57cec5SDimitry Andric   } else {
50160b57cec5SDimitry Andric     print_indent(indent);
50170b57cec5SDimitry Andric     outs() << " objc_method_description_list extends past end of the section\n";
50180b57cec5SDimitry Andric     memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
50190b57cec5SDimitry Andric     memcpy(&mdl, r, left);
50200b57cec5SDimitry Andric   }
50210b57cec5SDimitry Andric   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
50220b57cec5SDimitry Andric     swapStruct(mdl);
50230b57cec5SDimitry Andric 
50240b57cec5SDimitry Andric   print_indent(indent);
50250b57cec5SDimitry Andric   outs() << "        count " << mdl.count << "\n";
50260b57cec5SDimitry Andric 
50270b57cec5SDimitry Andric   list = r + sizeof(struct objc_method_description_list_t);
50280b57cec5SDimitry Andric   for (i = 0; i < mdl.count; i++) {
50290b57cec5SDimitry Andric     if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
50300b57cec5SDimitry Andric       print_indent(indent);
50310b57cec5SDimitry Andric       outs() << " remaining list entries extend past the of the section\n";
50320b57cec5SDimitry Andric       break;
50330b57cec5SDimitry Andric     }
50340b57cec5SDimitry Andric     print_indent(indent);
50350b57cec5SDimitry Andric     outs() << "        list[" << i << "]\n";
50360b57cec5SDimitry Andric     memcpy(&md, list + i * sizeof(struct objc_method_description_t),
50370b57cec5SDimitry Andric            sizeof(struct objc_method_description_t));
50380b57cec5SDimitry Andric     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
50390b57cec5SDimitry Andric       swapStruct(md);
50400b57cec5SDimitry Andric 
50410b57cec5SDimitry Andric     print_indent(indent);
50420b57cec5SDimitry Andric     outs() << "             name " << format("0x%08" PRIx32, md.name);
50430b57cec5SDimitry Andric     if (info->verbose) {
50440b57cec5SDimitry Andric       name = get_pointer_32(md.name, offset, xleft, S, info, true);
50450b57cec5SDimitry Andric       if (name != nullptr)
50460b57cec5SDimitry Andric         outs() << format(" %.*s", xleft, name);
50470b57cec5SDimitry Andric       else
50480b57cec5SDimitry Andric         outs() << " (not in an __OBJC section)";
50490b57cec5SDimitry Andric     }
50500b57cec5SDimitry Andric     outs() << "\n";
50510b57cec5SDimitry Andric 
50520b57cec5SDimitry Andric     print_indent(indent);
50530b57cec5SDimitry Andric     outs() << "            types " << format("0x%08" PRIx32, md.types);
50540b57cec5SDimitry Andric     if (info->verbose) {
50550b57cec5SDimitry Andric       name = get_pointer_32(md.types, offset, xleft, S, info, true);
50560b57cec5SDimitry Andric       if (name != nullptr)
50570b57cec5SDimitry Andric         outs() << format(" %.*s", xleft, name);
50580b57cec5SDimitry Andric       else
50590b57cec5SDimitry Andric         outs() << " (not in an __OBJC section)";
50600b57cec5SDimitry Andric     }
50610b57cec5SDimitry Andric     outs() << "\n";
50620b57cec5SDimitry Andric   }
50630b57cec5SDimitry Andric   return false;
50640b57cec5SDimitry Andric }
50650b57cec5SDimitry Andric 
50660b57cec5SDimitry Andric static bool print_protocol_list(uint32_t p, uint32_t indent,
50670b57cec5SDimitry Andric                                 struct DisassembleInfo *info);
50680b57cec5SDimitry Andric 
print_protocol(uint32_t p,uint32_t indent,struct DisassembleInfo * info)50690b57cec5SDimitry Andric static bool print_protocol(uint32_t p, uint32_t indent,
50700b57cec5SDimitry Andric                            struct DisassembleInfo *info) {
50710b57cec5SDimitry Andric   uint32_t offset, left;
50720b57cec5SDimitry Andric   SectionRef S;
50730b57cec5SDimitry Andric   struct objc_protocol_t protocol;
50740b57cec5SDimitry Andric   const char *r, *name;
50750b57cec5SDimitry Andric 
50760b57cec5SDimitry Andric   r = get_pointer_32(p, offset, left, S, info, true);
50770b57cec5SDimitry Andric   if (r == nullptr)
50780b57cec5SDimitry Andric     return true;
50790b57cec5SDimitry Andric 
50800b57cec5SDimitry Andric   outs() << "\n";
50810b57cec5SDimitry Andric   if (left >= sizeof(struct objc_protocol_t)) {
50820b57cec5SDimitry Andric     memcpy(&protocol, r, sizeof(struct objc_protocol_t));
50830b57cec5SDimitry Andric   } else {
50840b57cec5SDimitry Andric     print_indent(indent);
50850b57cec5SDimitry Andric     outs() << "            Protocol extends past end of the section\n";
50860b57cec5SDimitry Andric     memset(&protocol, '\0', sizeof(struct objc_protocol_t));
50870b57cec5SDimitry Andric     memcpy(&protocol, r, left);
50880b57cec5SDimitry Andric   }
50890b57cec5SDimitry Andric   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
50900b57cec5SDimitry Andric     swapStruct(protocol);
50910b57cec5SDimitry Andric 
50920b57cec5SDimitry Andric   print_indent(indent);
50930b57cec5SDimitry Andric   outs() << "              isa " << format("0x%08" PRIx32, protocol.isa)
50940b57cec5SDimitry Andric          << "\n";
50950b57cec5SDimitry Andric 
50960b57cec5SDimitry Andric   print_indent(indent);
50970b57cec5SDimitry Andric   outs() << "    protocol_name "
50980b57cec5SDimitry Andric          << format("0x%08" PRIx32, protocol.protocol_name);
50990b57cec5SDimitry Andric   if (info->verbose) {
51000b57cec5SDimitry Andric     name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
51010b57cec5SDimitry Andric     if (name != nullptr)
51020b57cec5SDimitry Andric       outs() << format(" %.*s", left, name);
51030b57cec5SDimitry Andric     else
51040b57cec5SDimitry Andric       outs() << " (not in an __OBJC section)";
51050b57cec5SDimitry Andric   }
51060b57cec5SDimitry Andric   outs() << "\n";
51070b57cec5SDimitry Andric 
51080b57cec5SDimitry Andric   print_indent(indent);
51090b57cec5SDimitry Andric   outs() << "    protocol_list "
51100b57cec5SDimitry Andric          << format("0x%08" PRIx32, protocol.protocol_list);
51110b57cec5SDimitry Andric   if (print_protocol_list(protocol.protocol_list, indent + 4, info))
51120b57cec5SDimitry Andric     outs() << " (not in an __OBJC section)\n";
51130b57cec5SDimitry Andric 
51140b57cec5SDimitry Andric   print_indent(indent);
51150b57cec5SDimitry Andric   outs() << " instance_methods "
51160b57cec5SDimitry Andric          << format("0x%08" PRIx32, protocol.instance_methods);
51170b57cec5SDimitry Andric   if (print_method_description_list(protocol.instance_methods, indent, info))
51180b57cec5SDimitry Andric     outs() << " (not in an __OBJC section)\n";
51190b57cec5SDimitry Andric 
51200b57cec5SDimitry Andric   print_indent(indent);
51210b57cec5SDimitry Andric   outs() << "    class_methods "
51220b57cec5SDimitry Andric          << format("0x%08" PRIx32, protocol.class_methods);
51230b57cec5SDimitry Andric   if (print_method_description_list(protocol.class_methods, indent, info))
51240b57cec5SDimitry Andric     outs() << " (not in an __OBJC section)\n";
51250b57cec5SDimitry Andric 
51260b57cec5SDimitry Andric   return false;
51270b57cec5SDimitry Andric }
51280b57cec5SDimitry Andric 
print_protocol_list(uint32_t p,uint32_t indent,struct DisassembleInfo * info)51290b57cec5SDimitry Andric static bool print_protocol_list(uint32_t p, uint32_t indent,
51300b57cec5SDimitry Andric                                 struct DisassembleInfo *info) {
51310b57cec5SDimitry Andric   uint32_t offset, left, l;
51320b57cec5SDimitry Andric   SectionRef S;
51330b57cec5SDimitry Andric   struct objc_protocol_list_t protocol_list;
51340b57cec5SDimitry Andric   const char *r, *list;
51350b57cec5SDimitry Andric   int32_t i;
51360b57cec5SDimitry Andric 
51370b57cec5SDimitry Andric   r = get_pointer_32(p, offset, left, S, info, true);
51380b57cec5SDimitry Andric   if (r == nullptr)
51390b57cec5SDimitry Andric     return true;
51400b57cec5SDimitry Andric 
51410b57cec5SDimitry Andric   outs() << "\n";
51420b57cec5SDimitry Andric   if (left > sizeof(struct objc_protocol_list_t)) {
51430b57cec5SDimitry Andric     memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
51440b57cec5SDimitry Andric   } else {
51450b57cec5SDimitry Andric     outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
51460b57cec5SDimitry Andric     memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
51470b57cec5SDimitry Andric     memcpy(&protocol_list, r, left);
51480b57cec5SDimitry Andric   }
51490b57cec5SDimitry Andric   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
51500b57cec5SDimitry Andric     swapStruct(protocol_list);
51510b57cec5SDimitry Andric 
51520b57cec5SDimitry Andric   print_indent(indent);
51530b57cec5SDimitry Andric   outs() << "         next " << format("0x%08" PRIx32, protocol_list.next)
51540b57cec5SDimitry Andric          << "\n";
51550b57cec5SDimitry Andric   print_indent(indent);
51560b57cec5SDimitry Andric   outs() << "        count " << protocol_list.count << "\n";
51570b57cec5SDimitry Andric 
51580b57cec5SDimitry Andric   list = r + sizeof(struct objc_protocol_list_t);
51590b57cec5SDimitry Andric   for (i = 0; i < protocol_list.count; i++) {
51600b57cec5SDimitry Andric     if ((i + 1) * sizeof(uint32_t) > left) {
51610b57cec5SDimitry Andric       outs() << "\t\t remaining list entries extend past the of the section\n";
51620b57cec5SDimitry Andric       break;
51630b57cec5SDimitry Andric     }
51640b57cec5SDimitry Andric     memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
51650b57cec5SDimitry Andric     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
51660b57cec5SDimitry Andric       sys::swapByteOrder(l);
51670b57cec5SDimitry Andric 
51680b57cec5SDimitry Andric     print_indent(indent);
51690b57cec5SDimitry Andric     outs() << "      list[" << i << "] " << format("0x%08" PRIx32, l);
51700b57cec5SDimitry Andric     if (print_protocol(l, indent, info))
51710b57cec5SDimitry Andric       outs() << "(not in an __OBJC section)\n";
51720b57cec5SDimitry Andric   }
51730b57cec5SDimitry Andric   return false;
51740b57cec5SDimitry Andric }
51750b57cec5SDimitry Andric 
print_ivar_list64_t(uint64_t p,struct DisassembleInfo * info)51760b57cec5SDimitry Andric static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
51770b57cec5SDimitry Andric   struct ivar_list64_t il;
51780b57cec5SDimitry Andric   struct ivar64_t i;
51790b57cec5SDimitry Andric   const char *r;
51800b57cec5SDimitry Andric   uint32_t offset, xoffset, left, j;
51810b57cec5SDimitry Andric   SectionRef S, xS;
51820b57cec5SDimitry Andric   const char *name, *sym_name, *ivar_offset_p;
51830b57cec5SDimitry Andric   uint64_t ivar_offset, n_value;
51840b57cec5SDimitry Andric 
51850b57cec5SDimitry Andric   r = get_pointer_64(p, offset, left, S, info);
51860b57cec5SDimitry Andric   if (r == nullptr)
51870b57cec5SDimitry Andric     return;
51880b57cec5SDimitry Andric   memset(&il, '\0', sizeof(struct ivar_list64_t));
51890b57cec5SDimitry Andric   if (left < sizeof(struct ivar_list64_t)) {
51900b57cec5SDimitry Andric     memcpy(&il, r, left);
51910b57cec5SDimitry Andric     outs() << "   (ivar_list_t entends past the end of the section)\n";
51920b57cec5SDimitry Andric   } else
51930b57cec5SDimitry Andric     memcpy(&il, r, sizeof(struct ivar_list64_t));
51940b57cec5SDimitry Andric   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
51950b57cec5SDimitry Andric     swapStruct(il);
51960b57cec5SDimitry Andric   outs() << "                    entsize " << il.entsize << "\n";
51970b57cec5SDimitry Andric   outs() << "                      count " << il.count << "\n";
51980b57cec5SDimitry Andric 
51990b57cec5SDimitry Andric   p += sizeof(struct ivar_list64_t);
52000b57cec5SDimitry Andric   offset += sizeof(struct ivar_list64_t);
52010b57cec5SDimitry Andric   for (j = 0; j < il.count; j++) {
52020b57cec5SDimitry Andric     r = get_pointer_64(p, offset, left, S, info);
52030b57cec5SDimitry Andric     if (r == nullptr)
52040b57cec5SDimitry Andric       return;
52050b57cec5SDimitry Andric     memset(&i, '\0', sizeof(struct ivar64_t));
52060b57cec5SDimitry Andric     if (left < sizeof(struct ivar64_t)) {
52070b57cec5SDimitry Andric       memcpy(&i, r, left);
52080b57cec5SDimitry Andric       outs() << "   (ivar_t entends past the end of the section)\n";
52090b57cec5SDimitry Andric     } else
52100b57cec5SDimitry Andric       memcpy(&i, r, sizeof(struct ivar64_t));
52110b57cec5SDimitry Andric     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
52120b57cec5SDimitry Andric       swapStruct(i);
52130b57cec5SDimitry Andric 
52140b57cec5SDimitry Andric     outs() << "\t\t\t   offset ";
52150b57cec5SDimitry Andric     sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
52160b57cec5SDimitry Andric                              info, n_value, i.offset);
52170b57cec5SDimitry Andric     if (n_value != 0) {
52180b57cec5SDimitry Andric       if (info->verbose && sym_name != nullptr)
52190b57cec5SDimitry Andric         outs() << sym_name;
52200b57cec5SDimitry Andric       else
52210b57cec5SDimitry Andric         outs() << format("0x%" PRIx64, n_value);
52220b57cec5SDimitry Andric       if (i.offset != 0)
52230b57cec5SDimitry Andric         outs() << " + " << format("0x%" PRIx64, i.offset);
52240b57cec5SDimitry Andric     } else
52250b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, i.offset);
52260b57cec5SDimitry Andric     ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
52270b57cec5SDimitry Andric     if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
52280b57cec5SDimitry Andric       memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
52290b57cec5SDimitry Andric       if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
52300b57cec5SDimitry Andric         sys::swapByteOrder(ivar_offset);
52310b57cec5SDimitry Andric       outs() << " " << ivar_offset << "\n";
52320b57cec5SDimitry Andric     } else
52330b57cec5SDimitry Andric       outs() << "\n";
52340b57cec5SDimitry Andric 
52350b57cec5SDimitry Andric     outs() << "\t\t\t     name ";
52360b57cec5SDimitry Andric     sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
52370b57cec5SDimitry Andric                              n_value, i.name);
52380b57cec5SDimitry Andric     if (n_value != 0) {
52390b57cec5SDimitry Andric       if (info->verbose && sym_name != nullptr)
52400b57cec5SDimitry Andric         outs() << sym_name;
52410b57cec5SDimitry Andric       else
52420b57cec5SDimitry Andric         outs() << format("0x%" PRIx64, n_value);
52430b57cec5SDimitry Andric       if (i.name != 0)
52440b57cec5SDimitry Andric         outs() << " + " << format("0x%" PRIx64, i.name);
52450b57cec5SDimitry Andric     } else
52460b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, i.name);
52470b57cec5SDimitry Andric     name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
52480b57cec5SDimitry Andric     if (name != nullptr)
52490b57cec5SDimitry Andric       outs() << format(" %.*s", left, name);
52500b57cec5SDimitry Andric     outs() << "\n";
52510b57cec5SDimitry Andric 
52520b57cec5SDimitry Andric     outs() << "\t\t\t     type ";
52530b57cec5SDimitry Andric     sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
52540b57cec5SDimitry Andric                              n_value, i.name);
52550b57cec5SDimitry Andric     name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
52560b57cec5SDimitry Andric     if (n_value != 0) {
52570b57cec5SDimitry Andric       if (info->verbose && sym_name != nullptr)
52580b57cec5SDimitry Andric         outs() << sym_name;
52590b57cec5SDimitry Andric       else
52600b57cec5SDimitry Andric         outs() << format("0x%" PRIx64, n_value);
52610b57cec5SDimitry Andric       if (i.type != 0)
52620b57cec5SDimitry Andric         outs() << " + " << format("0x%" PRIx64, i.type);
52630b57cec5SDimitry Andric     } else
52640b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, i.type);
52650b57cec5SDimitry Andric     if (name != nullptr)
52660b57cec5SDimitry Andric       outs() << format(" %.*s", left, name);
52670b57cec5SDimitry Andric     outs() << "\n";
52680b57cec5SDimitry Andric 
52690b57cec5SDimitry Andric     outs() << "\t\t\talignment " << i.alignment << "\n";
52700b57cec5SDimitry Andric     outs() << "\t\t\t     size " << i.size << "\n";
52710b57cec5SDimitry Andric 
52720b57cec5SDimitry Andric     p += sizeof(struct ivar64_t);
52730b57cec5SDimitry Andric     offset += sizeof(struct ivar64_t);
52740b57cec5SDimitry Andric   }
52750b57cec5SDimitry Andric }
52760b57cec5SDimitry Andric 
print_ivar_list32_t(uint32_t p,struct DisassembleInfo * info)52770b57cec5SDimitry Andric static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
52780b57cec5SDimitry Andric   struct ivar_list32_t il;
52790b57cec5SDimitry Andric   struct ivar32_t i;
52800b57cec5SDimitry Andric   const char *r;
52810b57cec5SDimitry Andric   uint32_t offset, xoffset, left, j;
52820b57cec5SDimitry Andric   SectionRef S, xS;
52830b57cec5SDimitry Andric   const char *name, *ivar_offset_p;
52840b57cec5SDimitry Andric   uint32_t ivar_offset;
52850b57cec5SDimitry Andric 
52860b57cec5SDimitry Andric   r = get_pointer_32(p, offset, left, S, info);
52870b57cec5SDimitry Andric   if (r == nullptr)
52880b57cec5SDimitry Andric     return;
52890b57cec5SDimitry Andric   memset(&il, '\0', sizeof(struct ivar_list32_t));
52900b57cec5SDimitry Andric   if (left < sizeof(struct ivar_list32_t)) {
52910b57cec5SDimitry Andric     memcpy(&il, r, left);
52920b57cec5SDimitry Andric     outs() << "   (ivar_list_t entends past the end of the section)\n";
52930b57cec5SDimitry Andric   } else
52940b57cec5SDimitry Andric     memcpy(&il, r, sizeof(struct ivar_list32_t));
52950b57cec5SDimitry Andric   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
52960b57cec5SDimitry Andric     swapStruct(il);
52970b57cec5SDimitry Andric   outs() << "                    entsize " << il.entsize << "\n";
52980b57cec5SDimitry Andric   outs() << "                      count " << il.count << "\n";
52990b57cec5SDimitry Andric 
53000b57cec5SDimitry Andric   p += sizeof(struct ivar_list32_t);
53010b57cec5SDimitry Andric   offset += sizeof(struct ivar_list32_t);
53020b57cec5SDimitry Andric   for (j = 0; j < il.count; j++) {
53030b57cec5SDimitry Andric     r = get_pointer_32(p, offset, left, S, info);
53040b57cec5SDimitry Andric     if (r == nullptr)
53050b57cec5SDimitry Andric       return;
53060b57cec5SDimitry Andric     memset(&i, '\0', sizeof(struct ivar32_t));
53070b57cec5SDimitry Andric     if (left < sizeof(struct ivar32_t)) {
53080b57cec5SDimitry Andric       memcpy(&i, r, left);
53090b57cec5SDimitry Andric       outs() << "   (ivar_t entends past the end of the section)\n";
53100b57cec5SDimitry Andric     } else
53110b57cec5SDimitry Andric       memcpy(&i, r, sizeof(struct ivar32_t));
53120b57cec5SDimitry Andric     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
53130b57cec5SDimitry Andric       swapStruct(i);
53140b57cec5SDimitry Andric 
53150b57cec5SDimitry Andric     outs() << "\t\t\t   offset " << format("0x%" PRIx32, i.offset);
53160b57cec5SDimitry Andric     ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
53170b57cec5SDimitry Andric     if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
53180b57cec5SDimitry Andric       memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
53190b57cec5SDimitry Andric       if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
53200b57cec5SDimitry Andric         sys::swapByteOrder(ivar_offset);
53210b57cec5SDimitry Andric       outs() << " " << ivar_offset << "\n";
53220b57cec5SDimitry Andric     } else
53230b57cec5SDimitry Andric       outs() << "\n";
53240b57cec5SDimitry Andric 
53250b57cec5SDimitry Andric     outs() << "\t\t\t     name " << format("0x%" PRIx32, i.name);
53260b57cec5SDimitry Andric     name = get_pointer_32(i.name, xoffset, left, xS, info);
53270b57cec5SDimitry Andric     if (name != nullptr)
53280b57cec5SDimitry Andric       outs() << format(" %.*s", left, name);
53290b57cec5SDimitry Andric     outs() << "\n";
53300b57cec5SDimitry Andric 
53310b57cec5SDimitry Andric     outs() << "\t\t\t     type " << format("0x%" PRIx32, i.type);
53320b57cec5SDimitry Andric     name = get_pointer_32(i.type, xoffset, left, xS, info);
53330b57cec5SDimitry Andric     if (name != nullptr)
53340b57cec5SDimitry Andric       outs() << format(" %.*s", left, name);
53350b57cec5SDimitry Andric     outs() << "\n";
53360b57cec5SDimitry Andric 
53370b57cec5SDimitry Andric     outs() << "\t\t\talignment " << i.alignment << "\n";
53380b57cec5SDimitry Andric     outs() << "\t\t\t     size " << i.size << "\n";
53390b57cec5SDimitry Andric 
53400b57cec5SDimitry Andric     p += sizeof(struct ivar32_t);
53410b57cec5SDimitry Andric     offset += sizeof(struct ivar32_t);
53420b57cec5SDimitry Andric   }
53430b57cec5SDimitry Andric }
53440b57cec5SDimitry Andric 
print_objc_property_list64(uint64_t p,struct DisassembleInfo * info)53450b57cec5SDimitry Andric static void print_objc_property_list64(uint64_t p,
53460b57cec5SDimitry Andric                                        struct DisassembleInfo *info) {
53470b57cec5SDimitry Andric   struct objc_property_list64 opl;
53480b57cec5SDimitry Andric   struct objc_property64 op;
53490b57cec5SDimitry Andric   const char *r;
53500b57cec5SDimitry Andric   uint32_t offset, xoffset, left, j;
53510b57cec5SDimitry Andric   SectionRef S, xS;
53520b57cec5SDimitry Andric   const char *name, *sym_name;
53530b57cec5SDimitry Andric   uint64_t n_value;
53540b57cec5SDimitry Andric 
53550b57cec5SDimitry Andric   r = get_pointer_64(p, offset, left, S, info);
53560b57cec5SDimitry Andric   if (r == nullptr)
53570b57cec5SDimitry Andric     return;
53580b57cec5SDimitry Andric   memset(&opl, '\0', sizeof(struct objc_property_list64));
53590b57cec5SDimitry Andric   if (left < sizeof(struct objc_property_list64)) {
53600b57cec5SDimitry Andric     memcpy(&opl, r, left);
53610b57cec5SDimitry Andric     outs() << "   (objc_property_list entends past the end of the section)\n";
53620b57cec5SDimitry Andric   } else
53630b57cec5SDimitry Andric     memcpy(&opl, r, sizeof(struct objc_property_list64));
53640b57cec5SDimitry Andric   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
53650b57cec5SDimitry Andric     swapStruct(opl);
53660b57cec5SDimitry Andric   outs() << "                    entsize " << opl.entsize << "\n";
53670b57cec5SDimitry Andric   outs() << "                      count " << opl.count << "\n";
53680b57cec5SDimitry Andric 
53690b57cec5SDimitry Andric   p += sizeof(struct objc_property_list64);
53700b57cec5SDimitry Andric   offset += sizeof(struct objc_property_list64);
53710b57cec5SDimitry Andric   for (j = 0; j < opl.count; j++) {
53720b57cec5SDimitry Andric     r = get_pointer_64(p, offset, left, S, info);
53730b57cec5SDimitry Andric     if (r == nullptr)
53740b57cec5SDimitry Andric       return;
53750b57cec5SDimitry Andric     memset(&op, '\0', sizeof(struct objc_property64));
53760b57cec5SDimitry Andric     if (left < sizeof(struct objc_property64)) {
53770b57cec5SDimitry Andric       memcpy(&op, r, left);
53780b57cec5SDimitry Andric       outs() << "   (objc_property entends past the end of the section)\n";
53790b57cec5SDimitry Andric     } else
53800b57cec5SDimitry Andric       memcpy(&op, r, sizeof(struct objc_property64));
53810b57cec5SDimitry Andric     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
53820b57cec5SDimitry Andric       swapStruct(op);
53830b57cec5SDimitry Andric 
53840b57cec5SDimitry Andric     outs() << "\t\t\t     name ";
53850b57cec5SDimitry Andric     sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
53860b57cec5SDimitry Andric                              info, n_value, op.name);
53870b57cec5SDimitry Andric     if (n_value != 0) {
53880b57cec5SDimitry Andric       if (info->verbose && sym_name != nullptr)
53890b57cec5SDimitry Andric         outs() << sym_name;
53900b57cec5SDimitry Andric       else
53910b57cec5SDimitry Andric         outs() << format("0x%" PRIx64, n_value);
53920b57cec5SDimitry Andric       if (op.name != 0)
53930b57cec5SDimitry Andric         outs() << " + " << format("0x%" PRIx64, op.name);
53940b57cec5SDimitry Andric     } else
53950b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, op.name);
53960b57cec5SDimitry Andric     name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
53970b57cec5SDimitry Andric     if (name != nullptr)
53980b57cec5SDimitry Andric       outs() << format(" %.*s", left, name);
53990b57cec5SDimitry Andric     outs() << "\n";
54000b57cec5SDimitry Andric 
54010b57cec5SDimitry Andric     outs() << "\t\t\tattributes ";
54020b57cec5SDimitry Andric     sym_name =
54030b57cec5SDimitry Andric         get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
54040b57cec5SDimitry Andric                       info, n_value, op.attributes);
54050b57cec5SDimitry Andric     if (n_value != 0) {
54060b57cec5SDimitry Andric       if (info->verbose && sym_name != nullptr)
54070b57cec5SDimitry Andric         outs() << sym_name;
54080b57cec5SDimitry Andric       else
54090b57cec5SDimitry Andric         outs() << format("0x%" PRIx64, n_value);
54100b57cec5SDimitry Andric       if (op.attributes != 0)
54110b57cec5SDimitry Andric         outs() << " + " << format("0x%" PRIx64, op.attributes);
54120b57cec5SDimitry Andric     } else
54130b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, op.attributes);
54140b57cec5SDimitry Andric     name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
54150b57cec5SDimitry Andric     if (name != nullptr)
54160b57cec5SDimitry Andric       outs() << format(" %.*s", left, name);
54170b57cec5SDimitry Andric     outs() << "\n";
54180b57cec5SDimitry Andric 
54190b57cec5SDimitry Andric     p += sizeof(struct objc_property64);
54200b57cec5SDimitry Andric     offset += sizeof(struct objc_property64);
54210b57cec5SDimitry Andric   }
54220b57cec5SDimitry Andric }
54230b57cec5SDimitry Andric 
print_objc_property_list32(uint32_t p,struct DisassembleInfo * info)54240b57cec5SDimitry Andric static void print_objc_property_list32(uint32_t p,
54250b57cec5SDimitry Andric                                        struct DisassembleInfo *info) {
54260b57cec5SDimitry Andric   struct objc_property_list32 opl;
54270b57cec5SDimitry Andric   struct objc_property32 op;
54280b57cec5SDimitry Andric   const char *r;
54290b57cec5SDimitry Andric   uint32_t offset, xoffset, left, j;
54300b57cec5SDimitry Andric   SectionRef S, xS;
54310b57cec5SDimitry Andric   const char *name;
54320b57cec5SDimitry Andric 
54330b57cec5SDimitry Andric   r = get_pointer_32(p, offset, left, S, info);
54340b57cec5SDimitry Andric   if (r == nullptr)
54350b57cec5SDimitry Andric     return;
54360b57cec5SDimitry Andric   memset(&opl, '\0', sizeof(struct objc_property_list32));
54370b57cec5SDimitry Andric   if (left < sizeof(struct objc_property_list32)) {
54380b57cec5SDimitry Andric     memcpy(&opl, r, left);
54390b57cec5SDimitry Andric     outs() << "   (objc_property_list entends past the end of the section)\n";
54400b57cec5SDimitry Andric   } else
54410b57cec5SDimitry Andric     memcpy(&opl, r, sizeof(struct objc_property_list32));
54420b57cec5SDimitry Andric   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
54430b57cec5SDimitry Andric     swapStruct(opl);
54440b57cec5SDimitry Andric   outs() << "                    entsize " << opl.entsize << "\n";
54450b57cec5SDimitry Andric   outs() << "                      count " << opl.count << "\n";
54460b57cec5SDimitry Andric 
54470b57cec5SDimitry Andric   p += sizeof(struct objc_property_list32);
54480b57cec5SDimitry Andric   offset += sizeof(struct objc_property_list32);
54490b57cec5SDimitry Andric   for (j = 0; j < opl.count; j++) {
54500b57cec5SDimitry Andric     r = get_pointer_32(p, offset, left, S, info);
54510b57cec5SDimitry Andric     if (r == nullptr)
54520b57cec5SDimitry Andric       return;
54530b57cec5SDimitry Andric     memset(&op, '\0', sizeof(struct objc_property32));
54540b57cec5SDimitry Andric     if (left < sizeof(struct objc_property32)) {
54550b57cec5SDimitry Andric       memcpy(&op, r, left);
54560b57cec5SDimitry Andric       outs() << "   (objc_property entends past the end of the section)\n";
54570b57cec5SDimitry Andric     } else
54580b57cec5SDimitry Andric       memcpy(&op, r, sizeof(struct objc_property32));
54590b57cec5SDimitry Andric     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
54600b57cec5SDimitry Andric       swapStruct(op);
54610b57cec5SDimitry Andric 
54620b57cec5SDimitry Andric     outs() << "\t\t\t     name " << format("0x%" PRIx32, op.name);
54630b57cec5SDimitry Andric     name = get_pointer_32(op.name, xoffset, left, xS, info);
54640b57cec5SDimitry Andric     if (name != nullptr)
54650b57cec5SDimitry Andric       outs() << format(" %.*s", left, name);
54660b57cec5SDimitry Andric     outs() << "\n";
54670b57cec5SDimitry Andric 
54680b57cec5SDimitry Andric     outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
54690b57cec5SDimitry Andric     name = get_pointer_32(op.attributes, xoffset, left, xS, info);
54700b57cec5SDimitry Andric     if (name != nullptr)
54710b57cec5SDimitry Andric       outs() << format(" %.*s", left, name);
54720b57cec5SDimitry Andric     outs() << "\n";
54730b57cec5SDimitry Andric 
54740b57cec5SDimitry Andric     p += sizeof(struct objc_property32);
54750b57cec5SDimitry Andric     offset += sizeof(struct objc_property32);
54760b57cec5SDimitry Andric   }
54770b57cec5SDimitry Andric }
54780b57cec5SDimitry Andric 
print_class_ro64_t(uint64_t p,struct DisassembleInfo * info,bool & is_meta_class)54790b57cec5SDimitry Andric static bool print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
54800b57cec5SDimitry Andric                                bool &is_meta_class) {
54810b57cec5SDimitry Andric   struct class_ro64_t cro;
54820b57cec5SDimitry Andric   const char *r;
54830b57cec5SDimitry Andric   uint32_t offset, xoffset, left;
54840b57cec5SDimitry Andric   SectionRef S, xS;
54850b57cec5SDimitry Andric   const char *name, *sym_name;
54860b57cec5SDimitry Andric   uint64_t n_value;
54870b57cec5SDimitry Andric 
54880b57cec5SDimitry Andric   r = get_pointer_64(p, offset, left, S, info);
54890b57cec5SDimitry Andric   if (r == nullptr || left < sizeof(struct class_ro64_t))
54900b57cec5SDimitry Andric     return false;
54910b57cec5SDimitry Andric   memcpy(&cro, r, sizeof(struct class_ro64_t));
54920b57cec5SDimitry Andric   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
54930b57cec5SDimitry Andric     swapStruct(cro);
54940b57cec5SDimitry Andric   outs() << "                    flags " << format("0x%" PRIx32, cro.flags);
54950b57cec5SDimitry Andric   if (cro.flags & RO_META)
54960b57cec5SDimitry Andric     outs() << " RO_META";
54970b57cec5SDimitry Andric   if (cro.flags & RO_ROOT)
54980b57cec5SDimitry Andric     outs() << " RO_ROOT";
54990b57cec5SDimitry Andric   if (cro.flags & RO_HAS_CXX_STRUCTORS)
55000b57cec5SDimitry Andric     outs() << " RO_HAS_CXX_STRUCTORS";
55010b57cec5SDimitry Andric   outs() << "\n";
55020b57cec5SDimitry Andric   outs() << "            instanceStart " << cro.instanceStart << "\n";
55030b57cec5SDimitry Andric   outs() << "             instanceSize " << cro.instanceSize << "\n";
55040b57cec5SDimitry Andric   outs() << "                 reserved " << format("0x%" PRIx32, cro.reserved)
55050b57cec5SDimitry Andric          << "\n";
55060b57cec5SDimitry Andric   outs() << "               ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
55070b57cec5SDimitry Andric          << "\n";
55080b57cec5SDimitry Andric   print_layout_map64(cro.ivarLayout, info);
55090b57cec5SDimitry Andric 
55100b57cec5SDimitry Andric   outs() << "                     name ";
55110b57cec5SDimitry Andric   sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
55120b57cec5SDimitry Andric                            info, n_value, cro.name);
55130b57cec5SDimitry Andric   if (n_value != 0) {
55140b57cec5SDimitry Andric     if (info->verbose && sym_name != nullptr)
55150b57cec5SDimitry Andric       outs() << sym_name;
55160b57cec5SDimitry Andric     else
55170b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, n_value);
55180b57cec5SDimitry Andric     if (cro.name != 0)
55190b57cec5SDimitry Andric       outs() << " + " << format("0x%" PRIx64, cro.name);
55200b57cec5SDimitry Andric   } else
55210b57cec5SDimitry Andric     outs() << format("0x%" PRIx64, cro.name);
55220b57cec5SDimitry Andric   name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
55230b57cec5SDimitry Andric   if (name != nullptr)
55240b57cec5SDimitry Andric     outs() << format(" %.*s", left, name);
55250b57cec5SDimitry Andric   outs() << "\n";
55260b57cec5SDimitry Andric 
55270b57cec5SDimitry Andric   outs() << "              baseMethods ";
55280b57cec5SDimitry Andric   sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
55290b57cec5SDimitry Andric                            S, info, n_value, cro.baseMethods);
55300b57cec5SDimitry Andric   if (n_value != 0) {
55310b57cec5SDimitry Andric     if (info->verbose && sym_name != nullptr)
55320b57cec5SDimitry Andric       outs() << sym_name;
55330b57cec5SDimitry Andric     else
55340b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, n_value);
55350b57cec5SDimitry Andric     if (cro.baseMethods != 0)
55360b57cec5SDimitry Andric       outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
55370b57cec5SDimitry Andric   } else
55380b57cec5SDimitry Andric     outs() << format("0x%" PRIx64, cro.baseMethods);
55390b57cec5SDimitry Andric   outs() << " (struct method_list_t *)\n";
55400b57cec5SDimitry Andric   if (cro.baseMethods + n_value != 0)
55410b57cec5SDimitry Andric     print_method_list64_t(cro.baseMethods + n_value, info, "");
55420b57cec5SDimitry Andric 
55430b57cec5SDimitry Andric   outs() << "            baseProtocols ";
55440b57cec5SDimitry Andric   sym_name =
55450b57cec5SDimitry Andric       get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
55460b57cec5SDimitry Andric                     info, n_value, cro.baseProtocols);
55470b57cec5SDimitry Andric   if (n_value != 0) {
55480b57cec5SDimitry Andric     if (info->verbose && sym_name != nullptr)
55490b57cec5SDimitry Andric       outs() << sym_name;
55500b57cec5SDimitry Andric     else
55510b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, n_value);
55520b57cec5SDimitry Andric     if (cro.baseProtocols != 0)
55530b57cec5SDimitry Andric       outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
55540b57cec5SDimitry Andric   } else
55550b57cec5SDimitry Andric     outs() << format("0x%" PRIx64, cro.baseProtocols);
55560b57cec5SDimitry Andric   outs() << "\n";
55570b57cec5SDimitry Andric   if (cro.baseProtocols + n_value != 0)
55580b57cec5SDimitry Andric     print_protocol_list64_t(cro.baseProtocols + n_value, info);
55590b57cec5SDimitry Andric 
55600b57cec5SDimitry Andric   outs() << "                    ivars ";
55610b57cec5SDimitry Andric   sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
55620b57cec5SDimitry Andric                            info, n_value, cro.ivars);
55630b57cec5SDimitry Andric   if (n_value != 0) {
55640b57cec5SDimitry Andric     if (info->verbose && sym_name != nullptr)
55650b57cec5SDimitry Andric       outs() << sym_name;
55660b57cec5SDimitry Andric     else
55670b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, n_value);
55680b57cec5SDimitry Andric     if (cro.ivars != 0)
55690b57cec5SDimitry Andric       outs() << " + " << format("0x%" PRIx64, cro.ivars);
55700b57cec5SDimitry Andric   } else
55710b57cec5SDimitry Andric     outs() << format("0x%" PRIx64, cro.ivars);
55720b57cec5SDimitry Andric   outs() << "\n";
55730b57cec5SDimitry Andric   if (cro.ivars + n_value != 0)
55740b57cec5SDimitry Andric     print_ivar_list64_t(cro.ivars + n_value, info);
55750b57cec5SDimitry Andric 
55760b57cec5SDimitry Andric   outs() << "           weakIvarLayout ";
55770b57cec5SDimitry Andric   sym_name =
55780b57cec5SDimitry Andric       get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
55790b57cec5SDimitry Andric                     info, n_value, cro.weakIvarLayout);
55800b57cec5SDimitry Andric   if (n_value != 0) {
55810b57cec5SDimitry Andric     if (info->verbose && sym_name != nullptr)
55820b57cec5SDimitry Andric       outs() << sym_name;
55830b57cec5SDimitry Andric     else
55840b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, n_value);
55850b57cec5SDimitry Andric     if (cro.weakIvarLayout != 0)
55860b57cec5SDimitry Andric       outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
55870b57cec5SDimitry Andric   } else
55880b57cec5SDimitry Andric     outs() << format("0x%" PRIx64, cro.weakIvarLayout);
55890b57cec5SDimitry Andric   outs() << "\n";
55900b57cec5SDimitry Andric   print_layout_map64(cro.weakIvarLayout + n_value, info);
55910b57cec5SDimitry Andric 
55920b57cec5SDimitry Andric   outs() << "           baseProperties ";
55930b57cec5SDimitry Andric   sym_name =
55940b57cec5SDimitry Andric       get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
55950b57cec5SDimitry Andric                     info, n_value, cro.baseProperties);
55960b57cec5SDimitry Andric   if (n_value != 0) {
55970b57cec5SDimitry Andric     if (info->verbose && sym_name != nullptr)
55980b57cec5SDimitry Andric       outs() << sym_name;
55990b57cec5SDimitry Andric     else
56000b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, n_value);
56010b57cec5SDimitry Andric     if (cro.baseProperties != 0)
56020b57cec5SDimitry Andric       outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
56030b57cec5SDimitry Andric   } else
56040b57cec5SDimitry Andric     outs() << format("0x%" PRIx64, cro.baseProperties);
56050b57cec5SDimitry Andric   outs() << "\n";
56060b57cec5SDimitry Andric   if (cro.baseProperties + n_value != 0)
56070b57cec5SDimitry Andric     print_objc_property_list64(cro.baseProperties + n_value, info);
56080b57cec5SDimitry Andric 
56090b57cec5SDimitry Andric   is_meta_class = (cro.flags & RO_META) != 0;
56100b57cec5SDimitry Andric   return true;
56110b57cec5SDimitry Andric }
56120b57cec5SDimitry Andric 
print_class_ro32_t(uint32_t p,struct DisassembleInfo * info,bool & is_meta_class)56130b57cec5SDimitry Andric static bool print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
56140b57cec5SDimitry Andric                                bool &is_meta_class) {
56150b57cec5SDimitry Andric   struct class_ro32_t cro;
56160b57cec5SDimitry Andric   const char *r;
56170b57cec5SDimitry Andric   uint32_t offset, xoffset, left;
56180b57cec5SDimitry Andric   SectionRef S, xS;
56190b57cec5SDimitry Andric   const char *name;
56200b57cec5SDimitry Andric 
56210b57cec5SDimitry Andric   r = get_pointer_32(p, offset, left, S, info);
56220b57cec5SDimitry Andric   if (r == nullptr)
56230b57cec5SDimitry Andric     return false;
56240b57cec5SDimitry Andric   memset(&cro, '\0', sizeof(struct class_ro32_t));
56250b57cec5SDimitry Andric   if (left < sizeof(struct class_ro32_t)) {
56260b57cec5SDimitry Andric     memcpy(&cro, r, left);
56270b57cec5SDimitry Andric     outs() << "   (class_ro_t entends past the end of the section)\n";
56280b57cec5SDimitry Andric   } else
56290b57cec5SDimitry Andric     memcpy(&cro, r, sizeof(struct class_ro32_t));
56300b57cec5SDimitry Andric   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
56310b57cec5SDimitry Andric     swapStruct(cro);
56320b57cec5SDimitry Andric   outs() << "                    flags " << format("0x%" PRIx32, cro.flags);
56330b57cec5SDimitry Andric   if (cro.flags & RO_META)
56340b57cec5SDimitry Andric     outs() << " RO_META";
56350b57cec5SDimitry Andric   if (cro.flags & RO_ROOT)
56360b57cec5SDimitry Andric     outs() << " RO_ROOT";
56370b57cec5SDimitry Andric   if (cro.flags & RO_HAS_CXX_STRUCTORS)
56380b57cec5SDimitry Andric     outs() << " RO_HAS_CXX_STRUCTORS";
56390b57cec5SDimitry Andric   outs() << "\n";
56400b57cec5SDimitry Andric   outs() << "            instanceStart " << cro.instanceStart << "\n";
56410b57cec5SDimitry Andric   outs() << "             instanceSize " << cro.instanceSize << "\n";
56420b57cec5SDimitry Andric   outs() << "               ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
56430b57cec5SDimitry Andric          << "\n";
56440b57cec5SDimitry Andric   print_layout_map32(cro.ivarLayout, info);
56450b57cec5SDimitry Andric 
56460b57cec5SDimitry Andric   outs() << "                     name " << format("0x%" PRIx32, cro.name);
56470b57cec5SDimitry Andric   name = get_pointer_32(cro.name, xoffset, left, xS, info);
56480b57cec5SDimitry Andric   if (name != nullptr)
56490b57cec5SDimitry Andric     outs() << format(" %.*s", left, name);
56500b57cec5SDimitry Andric   outs() << "\n";
56510b57cec5SDimitry Andric 
56520b57cec5SDimitry Andric   outs() << "              baseMethods "
56530b57cec5SDimitry Andric          << format("0x%" PRIx32, cro.baseMethods)
56540b57cec5SDimitry Andric          << " (struct method_list_t *)\n";
56550b57cec5SDimitry Andric   if (cro.baseMethods != 0)
56560b57cec5SDimitry Andric     print_method_list32_t(cro.baseMethods, info, "");
56570b57cec5SDimitry Andric 
56580b57cec5SDimitry Andric   outs() << "            baseProtocols "
56590b57cec5SDimitry Andric          << format("0x%" PRIx32, cro.baseProtocols) << "\n";
56600b57cec5SDimitry Andric   if (cro.baseProtocols != 0)
56610b57cec5SDimitry Andric     print_protocol_list32_t(cro.baseProtocols, info);
56620b57cec5SDimitry Andric   outs() << "                    ivars " << format("0x%" PRIx32, cro.ivars)
56630b57cec5SDimitry Andric          << "\n";
56640b57cec5SDimitry Andric   if (cro.ivars != 0)
56650b57cec5SDimitry Andric     print_ivar_list32_t(cro.ivars, info);
56660b57cec5SDimitry Andric   outs() << "           weakIvarLayout "
56670b57cec5SDimitry Andric          << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
56680b57cec5SDimitry Andric   print_layout_map32(cro.weakIvarLayout, info);
56690b57cec5SDimitry Andric   outs() << "           baseProperties "
56700b57cec5SDimitry Andric          << format("0x%" PRIx32, cro.baseProperties) << "\n";
56710b57cec5SDimitry Andric   if (cro.baseProperties != 0)
56720b57cec5SDimitry Andric     print_objc_property_list32(cro.baseProperties, info);
56730b57cec5SDimitry Andric   is_meta_class = (cro.flags & RO_META) != 0;
56740b57cec5SDimitry Andric   return true;
56750b57cec5SDimitry Andric }
56760b57cec5SDimitry Andric 
print_class64_t(uint64_t p,struct DisassembleInfo * info)56770b57cec5SDimitry Andric static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
56780b57cec5SDimitry Andric   struct class64_t c;
56790b57cec5SDimitry Andric   const char *r;
56800b57cec5SDimitry Andric   uint32_t offset, left;
56810b57cec5SDimitry Andric   SectionRef S;
56820b57cec5SDimitry Andric   const char *name;
56830b57cec5SDimitry Andric   uint64_t isa_n_value, n_value;
56840b57cec5SDimitry Andric 
56850b57cec5SDimitry Andric   r = get_pointer_64(p, offset, left, S, info);
56860b57cec5SDimitry Andric   if (r == nullptr || left < sizeof(struct class64_t))
56870b57cec5SDimitry Andric     return;
56880b57cec5SDimitry Andric   memcpy(&c, r, sizeof(struct class64_t));
56890b57cec5SDimitry Andric   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
56900b57cec5SDimitry Andric     swapStruct(c);
56910b57cec5SDimitry Andric 
56920b57cec5SDimitry Andric   outs() << "           isa " << format("0x%" PRIx64, c.isa);
56930b57cec5SDimitry Andric   name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
56940b57cec5SDimitry Andric                        isa_n_value, c.isa);
56950b57cec5SDimitry Andric   if (name != nullptr)
56960b57cec5SDimitry Andric     outs() << " " << name;
56970b57cec5SDimitry Andric   outs() << "\n";
56980b57cec5SDimitry Andric 
56990b57cec5SDimitry Andric   outs() << "    superclass " << format("0x%" PRIx64, c.superclass);
57000b57cec5SDimitry Andric   name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
57010b57cec5SDimitry Andric                        n_value, c.superclass);
57020b57cec5SDimitry Andric   if (name != nullptr)
57030b57cec5SDimitry Andric     outs() << " " << name;
57040b57cec5SDimitry Andric   else {
57050b57cec5SDimitry Andric     name = get_dyld_bind_info_symbolname(S.getAddress() +
57060b57cec5SDimitry Andric              offset + offsetof(struct class64_t, superclass), info);
57070b57cec5SDimitry Andric     if (name != nullptr)
57080b57cec5SDimitry Andric       outs() << " " << name;
57090b57cec5SDimitry Andric   }
57100b57cec5SDimitry Andric   outs() << "\n";
57110b57cec5SDimitry Andric 
57120b57cec5SDimitry Andric   outs() << "         cache " << format("0x%" PRIx64, c.cache);
57130b57cec5SDimitry Andric   name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
57140b57cec5SDimitry Andric                        n_value, c.cache);
57150b57cec5SDimitry Andric   if (name != nullptr)
57160b57cec5SDimitry Andric     outs() << " " << name;
57170b57cec5SDimitry Andric   outs() << "\n";
57180b57cec5SDimitry Andric 
57190b57cec5SDimitry Andric   outs() << "        vtable " << format("0x%" PRIx64, c.vtable);
57200b57cec5SDimitry Andric   name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
57210b57cec5SDimitry Andric                        n_value, c.vtable);
57220b57cec5SDimitry Andric   if (name != nullptr)
57230b57cec5SDimitry Andric     outs() << " " << name;
57240b57cec5SDimitry Andric   outs() << "\n";
57250b57cec5SDimitry Andric 
57260b57cec5SDimitry Andric   name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
57270b57cec5SDimitry Andric                        n_value, c.data);
57280b57cec5SDimitry Andric   outs() << "          data ";
57290b57cec5SDimitry Andric   if (n_value != 0) {
57300b57cec5SDimitry Andric     if (info->verbose && name != nullptr)
57310b57cec5SDimitry Andric       outs() << name;
57320b57cec5SDimitry Andric     else
57330b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, n_value);
57340b57cec5SDimitry Andric     if (c.data != 0)
57350b57cec5SDimitry Andric       outs() << " + " << format("0x%" PRIx64, c.data);
57360b57cec5SDimitry Andric   } else
57370b57cec5SDimitry Andric     outs() << format("0x%" PRIx64, c.data);
57380b57cec5SDimitry Andric   outs() << " (struct class_ro_t *)";
57390b57cec5SDimitry Andric 
57400b57cec5SDimitry Andric   // This is a Swift class if some of the low bits of the pointer are set.
57410b57cec5SDimitry Andric   if ((c.data + n_value) & 0x7)
57420b57cec5SDimitry Andric     outs() << " Swift class";
57430b57cec5SDimitry Andric   outs() << "\n";
57440b57cec5SDimitry Andric   bool is_meta_class;
57450b57cec5SDimitry Andric   if (!print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class))
57460b57cec5SDimitry Andric     return;
57470b57cec5SDimitry Andric 
57480b57cec5SDimitry Andric   if (!is_meta_class &&
57490b57cec5SDimitry Andric       c.isa + isa_n_value != p &&
57500b57cec5SDimitry Andric       c.isa + isa_n_value != 0 &&
57510b57cec5SDimitry Andric       info->depth < 100) {
57520b57cec5SDimitry Andric       info->depth++;
57530b57cec5SDimitry Andric       outs() << "Meta Class\n";
57540b57cec5SDimitry Andric       print_class64_t(c.isa + isa_n_value, info);
57550b57cec5SDimitry Andric   }
57560b57cec5SDimitry Andric }
57570b57cec5SDimitry Andric 
print_class32_t(uint32_t p,struct DisassembleInfo * info)57580b57cec5SDimitry Andric static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
57590b57cec5SDimitry Andric   struct class32_t c;
57600b57cec5SDimitry Andric   const char *r;
57610b57cec5SDimitry Andric   uint32_t offset, left;
57620b57cec5SDimitry Andric   SectionRef S;
57630b57cec5SDimitry Andric   const char *name;
57640b57cec5SDimitry Andric 
57650b57cec5SDimitry Andric   r = get_pointer_32(p, offset, left, S, info);
57660b57cec5SDimitry Andric   if (r == nullptr)
57670b57cec5SDimitry Andric     return;
57680b57cec5SDimitry Andric   memset(&c, '\0', sizeof(struct class32_t));
57690b57cec5SDimitry Andric   if (left < sizeof(struct class32_t)) {
57700b57cec5SDimitry Andric     memcpy(&c, r, left);
57710b57cec5SDimitry Andric     outs() << "   (class_t entends past the end of the section)\n";
57720b57cec5SDimitry Andric   } else
57730b57cec5SDimitry Andric     memcpy(&c, r, sizeof(struct class32_t));
57740b57cec5SDimitry Andric   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
57750b57cec5SDimitry Andric     swapStruct(c);
57760b57cec5SDimitry Andric 
57770b57cec5SDimitry Andric   outs() << "           isa " << format("0x%" PRIx32, c.isa);
57780b57cec5SDimitry Andric   name =
57790b57cec5SDimitry Andric       get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
57800b57cec5SDimitry Andric   if (name != nullptr)
57810b57cec5SDimitry Andric     outs() << " " << name;
57820b57cec5SDimitry Andric   outs() << "\n";
57830b57cec5SDimitry Andric 
57840b57cec5SDimitry Andric   outs() << "    superclass " << format("0x%" PRIx32, c.superclass);
57850b57cec5SDimitry Andric   name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
57860b57cec5SDimitry Andric                        c.superclass);
57870b57cec5SDimitry Andric   if (name != nullptr)
57880b57cec5SDimitry Andric     outs() << " " << name;
57890b57cec5SDimitry Andric   outs() << "\n";
57900b57cec5SDimitry Andric 
57910b57cec5SDimitry Andric   outs() << "         cache " << format("0x%" PRIx32, c.cache);
57920b57cec5SDimitry Andric   name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
57930b57cec5SDimitry Andric                        c.cache);
57940b57cec5SDimitry Andric   if (name != nullptr)
57950b57cec5SDimitry Andric     outs() << " " << name;
57960b57cec5SDimitry Andric   outs() << "\n";
57970b57cec5SDimitry Andric 
57980b57cec5SDimitry Andric   outs() << "        vtable " << format("0x%" PRIx32, c.vtable);
57990b57cec5SDimitry Andric   name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
58000b57cec5SDimitry Andric                        c.vtable);
58010b57cec5SDimitry Andric   if (name != nullptr)
58020b57cec5SDimitry Andric     outs() << " " << name;
58030b57cec5SDimitry Andric   outs() << "\n";
58040b57cec5SDimitry Andric 
58050b57cec5SDimitry Andric   name =
58060b57cec5SDimitry Andric       get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
58070b57cec5SDimitry Andric   outs() << "          data " << format("0x%" PRIx32, c.data)
58080b57cec5SDimitry Andric          << " (struct class_ro_t *)";
58090b57cec5SDimitry Andric 
58100b57cec5SDimitry Andric   // This is a Swift class if some of the low bits of the pointer are set.
58110b57cec5SDimitry Andric   if (c.data & 0x3)
58120b57cec5SDimitry Andric     outs() << " Swift class";
58130b57cec5SDimitry Andric   outs() << "\n";
58140b57cec5SDimitry Andric   bool is_meta_class;
58150b57cec5SDimitry Andric   if (!print_class_ro32_t(c.data & ~0x3, info, is_meta_class))
58160b57cec5SDimitry Andric     return;
58170b57cec5SDimitry Andric 
58180b57cec5SDimitry Andric   if (!is_meta_class) {
58190b57cec5SDimitry Andric     outs() << "Meta Class\n";
58200b57cec5SDimitry Andric     print_class32_t(c.isa, info);
58210b57cec5SDimitry Andric   }
58220b57cec5SDimitry Andric }
58230b57cec5SDimitry Andric 
print_objc_class_t(struct objc_class_t * objc_class,struct DisassembleInfo * info)58240b57cec5SDimitry Andric static void print_objc_class_t(struct objc_class_t *objc_class,
58250b57cec5SDimitry Andric                                struct DisassembleInfo *info) {
58260b57cec5SDimitry Andric   uint32_t offset, left, xleft;
58270b57cec5SDimitry Andric   const char *name, *p, *ivar_list;
58280b57cec5SDimitry Andric   SectionRef S;
58290b57cec5SDimitry Andric   int32_t i;
58300b57cec5SDimitry Andric   struct objc_ivar_list_t objc_ivar_list;
58310b57cec5SDimitry Andric   struct objc_ivar_t ivar;
58320b57cec5SDimitry Andric 
58330b57cec5SDimitry Andric   outs() << "\t\t      isa " << format("0x%08" PRIx32, objc_class->isa);
58340b57cec5SDimitry Andric   if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
58350b57cec5SDimitry Andric     name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
58360b57cec5SDimitry Andric     if (name != nullptr)
58370b57cec5SDimitry Andric       outs() << format(" %.*s", left, name);
58380b57cec5SDimitry Andric     else
58390b57cec5SDimitry Andric       outs() << " (not in an __OBJC section)";
58400b57cec5SDimitry Andric   }
58410b57cec5SDimitry Andric   outs() << "\n";
58420b57cec5SDimitry Andric 
58430b57cec5SDimitry Andric   outs() << "\t      super_class "
58440b57cec5SDimitry Andric          << format("0x%08" PRIx32, objc_class->super_class);
58450b57cec5SDimitry Andric   if (info->verbose) {
58460b57cec5SDimitry Andric     name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
58470b57cec5SDimitry Andric     if (name != nullptr)
58480b57cec5SDimitry Andric       outs() << format(" %.*s", left, name);
58490b57cec5SDimitry Andric     else
58500b57cec5SDimitry Andric       outs() << " (not in an __OBJC section)";
58510b57cec5SDimitry Andric   }
58520b57cec5SDimitry Andric   outs() << "\n";
58530b57cec5SDimitry Andric 
58540b57cec5SDimitry Andric   outs() << "\t\t     name " << format("0x%08" PRIx32, objc_class->name);
58550b57cec5SDimitry Andric   if (info->verbose) {
58560b57cec5SDimitry Andric     name = get_pointer_32(objc_class->name, offset, left, S, info, true);
58570b57cec5SDimitry Andric     if (name != nullptr)
58580b57cec5SDimitry Andric       outs() << format(" %.*s", left, name);
58590b57cec5SDimitry Andric     else
58600b57cec5SDimitry Andric       outs() << " (not in an __OBJC section)";
58610b57cec5SDimitry Andric   }
58620b57cec5SDimitry Andric   outs() << "\n";
58630b57cec5SDimitry Andric 
58640b57cec5SDimitry Andric   outs() << "\t\t  version " << format("0x%08" PRIx32, objc_class->version)
58650b57cec5SDimitry Andric          << "\n";
58660b57cec5SDimitry Andric 
58670b57cec5SDimitry Andric   outs() << "\t\t     info " << format("0x%08" PRIx32, objc_class->info);
58680b57cec5SDimitry Andric   if (info->verbose) {
58690b57cec5SDimitry Andric     if (CLS_GETINFO(objc_class, CLS_CLASS))
58700b57cec5SDimitry Andric       outs() << " CLS_CLASS";
58710b57cec5SDimitry Andric     else if (CLS_GETINFO(objc_class, CLS_META))
58720b57cec5SDimitry Andric       outs() << " CLS_META";
58730b57cec5SDimitry Andric   }
58740b57cec5SDimitry Andric   outs() << "\n";
58750b57cec5SDimitry Andric 
58760b57cec5SDimitry Andric   outs() << "\t    instance_size "
58770b57cec5SDimitry Andric          << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
58780b57cec5SDimitry Andric 
58790b57cec5SDimitry Andric   p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
58800b57cec5SDimitry Andric   outs() << "\t\t    ivars " << format("0x%08" PRIx32, objc_class->ivars);
58810b57cec5SDimitry Andric   if (p != nullptr) {
58820b57cec5SDimitry Andric     if (left > sizeof(struct objc_ivar_list_t)) {
58830b57cec5SDimitry Andric       outs() << "\n";
58840b57cec5SDimitry Andric       memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
58850b57cec5SDimitry Andric     } else {
58860b57cec5SDimitry Andric       outs() << " (entends past the end of the section)\n";
58870b57cec5SDimitry Andric       memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
58880b57cec5SDimitry Andric       memcpy(&objc_ivar_list, p, left);
58890b57cec5SDimitry Andric     }
58900b57cec5SDimitry Andric     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
58910b57cec5SDimitry Andric       swapStruct(objc_ivar_list);
58920b57cec5SDimitry Andric     outs() << "\t\t       ivar_count " << objc_ivar_list.ivar_count << "\n";
58930b57cec5SDimitry Andric     ivar_list = p + sizeof(struct objc_ivar_list_t);
58940b57cec5SDimitry Andric     for (i = 0; i < objc_ivar_list.ivar_count; i++) {
58950b57cec5SDimitry Andric       if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
58960b57cec5SDimitry Andric         outs() << "\t\t remaining ivar's extend past the of the section\n";
58970b57cec5SDimitry Andric         break;
58980b57cec5SDimitry Andric       }
58990b57cec5SDimitry Andric       memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
59000b57cec5SDimitry Andric              sizeof(struct objc_ivar_t));
59010b57cec5SDimitry Andric       if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
59020b57cec5SDimitry Andric         swapStruct(ivar);
59030b57cec5SDimitry Andric 
59040b57cec5SDimitry Andric       outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
59050b57cec5SDimitry Andric       if (info->verbose) {
59060b57cec5SDimitry Andric         name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
59070b57cec5SDimitry Andric         if (name != nullptr)
59080b57cec5SDimitry Andric           outs() << format(" %.*s", xleft, name);
59090b57cec5SDimitry Andric         else
59100b57cec5SDimitry Andric           outs() << " (not in an __OBJC section)";
59110b57cec5SDimitry Andric       }
59120b57cec5SDimitry Andric       outs() << "\n";
59130b57cec5SDimitry Andric 
59140b57cec5SDimitry Andric       outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
59150b57cec5SDimitry Andric       if (info->verbose) {
59160b57cec5SDimitry Andric         name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
59170b57cec5SDimitry Andric         if (name != nullptr)
59180b57cec5SDimitry Andric           outs() << format(" %.*s", xleft, name);
59190b57cec5SDimitry Andric         else
59200b57cec5SDimitry Andric           outs() << " (not in an __OBJC section)";
59210b57cec5SDimitry Andric       }
59220b57cec5SDimitry Andric       outs() << "\n";
59230b57cec5SDimitry Andric 
59240b57cec5SDimitry Andric       outs() << "\t\t      ivar_offset "
59250b57cec5SDimitry Andric              << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
59260b57cec5SDimitry Andric     }
59270b57cec5SDimitry Andric   } else {
59280b57cec5SDimitry Andric     outs() << " (not in an __OBJC section)\n";
59290b57cec5SDimitry Andric   }
59300b57cec5SDimitry Andric 
59310b57cec5SDimitry Andric   outs() << "\t\t  methods " << format("0x%08" PRIx32, objc_class->methodLists);
59320b57cec5SDimitry Andric   if (print_method_list(objc_class->methodLists, info))
59330b57cec5SDimitry Andric     outs() << " (not in an __OBJC section)\n";
59340b57cec5SDimitry Andric 
59350b57cec5SDimitry Andric   outs() << "\t\t    cache " << format("0x%08" PRIx32, objc_class->cache)
59360b57cec5SDimitry Andric          << "\n";
59370b57cec5SDimitry Andric 
59380b57cec5SDimitry Andric   outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
59390b57cec5SDimitry Andric   if (print_protocol_list(objc_class->protocols, 16, info))
59400b57cec5SDimitry Andric     outs() << " (not in an __OBJC section)\n";
59410b57cec5SDimitry Andric }
59420b57cec5SDimitry Andric 
print_objc_objc_category_t(struct objc_category_t * objc_category,struct DisassembleInfo * info)59430b57cec5SDimitry Andric static void print_objc_objc_category_t(struct objc_category_t *objc_category,
59440b57cec5SDimitry Andric                                        struct DisassembleInfo *info) {
59450b57cec5SDimitry Andric   uint32_t offset, left;
59460b57cec5SDimitry Andric   const char *name;
59470b57cec5SDimitry Andric   SectionRef S;
59480b57cec5SDimitry Andric 
59490b57cec5SDimitry Andric   outs() << "\t       category name "
59500b57cec5SDimitry Andric          << format("0x%08" PRIx32, objc_category->category_name);
59510b57cec5SDimitry Andric   if (info->verbose) {
59520b57cec5SDimitry Andric     name = get_pointer_32(objc_category->category_name, offset, left, S, info,
59530b57cec5SDimitry Andric                           true);
59540b57cec5SDimitry Andric     if (name != nullptr)
59550b57cec5SDimitry Andric       outs() << format(" %.*s", left, name);
59560b57cec5SDimitry Andric     else
59570b57cec5SDimitry Andric       outs() << " (not in an __OBJC section)";
59580b57cec5SDimitry Andric   }
59590b57cec5SDimitry Andric   outs() << "\n";
59600b57cec5SDimitry Andric 
59610b57cec5SDimitry Andric   outs() << "\t\t  class name "
59620b57cec5SDimitry Andric          << format("0x%08" PRIx32, objc_category->class_name);
59630b57cec5SDimitry Andric   if (info->verbose) {
59640b57cec5SDimitry Andric     name =
59650b57cec5SDimitry Andric         get_pointer_32(objc_category->class_name, offset, left, S, info, true);
59660b57cec5SDimitry Andric     if (name != nullptr)
59670b57cec5SDimitry Andric       outs() << format(" %.*s", left, name);
59680b57cec5SDimitry Andric     else
59690b57cec5SDimitry Andric       outs() << " (not in an __OBJC section)";
59700b57cec5SDimitry Andric   }
59710b57cec5SDimitry Andric   outs() << "\n";
59720b57cec5SDimitry Andric 
59730b57cec5SDimitry Andric   outs() << "\t    instance methods "
59740b57cec5SDimitry Andric          << format("0x%08" PRIx32, objc_category->instance_methods);
59750b57cec5SDimitry Andric   if (print_method_list(objc_category->instance_methods, info))
59760b57cec5SDimitry Andric     outs() << " (not in an __OBJC section)\n";
59770b57cec5SDimitry Andric 
59780b57cec5SDimitry Andric   outs() << "\t       class methods "
59790b57cec5SDimitry Andric          << format("0x%08" PRIx32, objc_category->class_methods);
59800b57cec5SDimitry Andric   if (print_method_list(objc_category->class_methods, info))
59810b57cec5SDimitry Andric     outs() << " (not in an __OBJC section)\n";
59820b57cec5SDimitry Andric }
59830b57cec5SDimitry Andric 
print_category64_t(uint64_t p,struct DisassembleInfo * info)59840b57cec5SDimitry Andric static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
59850b57cec5SDimitry Andric   struct category64_t c;
59860b57cec5SDimitry Andric   const char *r;
59870b57cec5SDimitry Andric   uint32_t offset, xoffset, left;
59880b57cec5SDimitry Andric   SectionRef S, xS;
59890b57cec5SDimitry Andric   const char *name, *sym_name;
59900b57cec5SDimitry Andric   uint64_t n_value;
59910b57cec5SDimitry Andric 
59920b57cec5SDimitry Andric   r = get_pointer_64(p, offset, left, S, info);
59930b57cec5SDimitry Andric   if (r == nullptr)
59940b57cec5SDimitry Andric     return;
59950b57cec5SDimitry Andric   memset(&c, '\0', sizeof(struct category64_t));
59960b57cec5SDimitry Andric   if (left < sizeof(struct category64_t)) {
59970b57cec5SDimitry Andric     memcpy(&c, r, left);
59980b57cec5SDimitry Andric     outs() << "   (category_t entends past the end of the section)\n";
59990b57cec5SDimitry Andric   } else
60000b57cec5SDimitry Andric     memcpy(&c, r, sizeof(struct category64_t));
60010b57cec5SDimitry Andric   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
60020b57cec5SDimitry Andric     swapStruct(c);
60030b57cec5SDimitry Andric 
60040b57cec5SDimitry Andric   outs() << "              name ";
60050b57cec5SDimitry Andric   sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
60060b57cec5SDimitry Andric                            info, n_value, c.name);
60070b57cec5SDimitry Andric   if (n_value != 0) {
60080b57cec5SDimitry Andric     if (info->verbose && sym_name != nullptr)
60090b57cec5SDimitry Andric       outs() << sym_name;
60100b57cec5SDimitry Andric     else
60110b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, n_value);
60120b57cec5SDimitry Andric     if (c.name != 0)
60130b57cec5SDimitry Andric       outs() << " + " << format("0x%" PRIx64, c.name);
60140b57cec5SDimitry Andric   } else
60150b57cec5SDimitry Andric     outs() << format("0x%" PRIx64, c.name);
60160b57cec5SDimitry Andric   name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
60170b57cec5SDimitry Andric   if (name != nullptr)
60180b57cec5SDimitry Andric     outs() << format(" %.*s", left, name);
60190b57cec5SDimitry Andric   outs() << "\n";
60200b57cec5SDimitry Andric 
60210b57cec5SDimitry Andric   outs() << "               cls ";
60220b57cec5SDimitry Andric   sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
60230b57cec5SDimitry Andric                            n_value, c.cls);
60240b57cec5SDimitry Andric   if (n_value != 0) {
60250b57cec5SDimitry Andric     if (info->verbose && sym_name != nullptr)
60260b57cec5SDimitry Andric       outs() << sym_name;
60270b57cec5SDimitry Andric     else
60280b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, n_value);
60290b57cec5SDimitry Andric     if (c.cls != 0)
60300b57cec5SDimitry Andric       outs() << " + " << format("0x%" PRIx64, c.cls);
60310b57cec5SDimitry Andric   } else
60320b57cec5SDimitry Andric     outs() << format("0x%" PRIx64, c.cls);
60330b57cec5SDimitry Andric   outs() << "\n";
60340b57cec5SDimitry Andric   if (c.cls + n_value != 0)
60350b57cec5SDimitry Andric     print_class64_t(c.cls + n_value, info);
60360b57cec5SDimitry Andric 
60370b57cec5SDimitry Andric   outs() << "   instanceMethods ";
60380b57cec5SDimitry Andric   sym_name =
60390b57cec5SDimitry Andric       get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
60400b57cec5SDimitry Andric                     info, n_value, c.instanceMethods);
60410b57cec5SDimitry Andric   if (n_value != 0) {
60420b57cec5SDimitry Andric     if (info->verbose && sym_name != nullptr)
60430b57cec5SDimitry Andric       outs() << sym_name;
60440b57cec5SDimitry Andric     else
60450b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, n_value);
60460b57cec5SDimitry Andric     if (c.instanceMethods != 0)
60470b57cec5SDimitry Andric       outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
60480b57cec5SDimitry Andric   } else
60490b57cec5SDimitry Andric     outs() << format("0x%" PRIx64, c.instanceMethods);
60500b57cec5SDimitry Andric   outs() << "\n";
60510b57cec5SDimitry Andric   if (c.instanceMethods + n_value != 0)
60520b57cec5SDimitry Andric     print_method_list64_t(c.instanceMethods + n_value, info, "");
60530b57cec5SDimitry Andric 
60540b57cec5SDimitry Andric   outs() << "      classMethods ";
60550b57cec5SDimitry Andric   sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
60560b57cec5SDimitry Andric                            S, info, n_value, c.classMethods);
60570b57cec5SDimitry Andric   if (n_value != 0) {
60580b57cec5SDimitry Andric     if (info->verbose && sym_name != nullptr)
60590b57cec5SDimitry Andric       outs() << sym_name;
60600b57cec5SDimitry Andric     else
60610b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, n_value);
60620b57cec5SDimitry Andric     if (c.classMethods != 0)
60630b57cec5SDimitry Andric       outs() << " + " << format("0x%" PRIx64, c.classMethods);
60640b57cec5SDimitry Andric   } else
60650b57cec5SDimitry Andric     outs() << format("0x%" PRIx64, c.classMethods);
60660b57cec5SDimitry Andric   outs() << "\n";
60670b57cec5SDimitry Andric   if (c.classMethods + n_value != 0)
60680b57cec5SDimitry Andric     print_method_list64_t(c.classMethods + n_value, info, "");
60690b57cec5SDimitry Andric 
60700b57cec5SDimitry Andric   outs() << "         protocols ";
60710b57cec5SDimitry Andric   sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
60720b57cec5SDimitry Andric                            info, n_value, c.protocols);
60730b57cec5SDimitry Andric   if (n_value != 0) {
60740b57cec5SDimitry Andric     if (info->verbose && sym_name != nullptr)
60750b57cec5SDimitry Andric       outs() << sym_name;
60760b57cec5SDimitry Andric     else
60770b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, n_value);
60780b57cec5SDimitry Andric     if (c.protocols != 0)
60790b57cec5SDimitry Andric       outs() << " + " << format("0x%" PRIx64, c.protocols);
60800b57cec5SDimitry Andric   } else
60810b57cec5SDimitry Andric     outs() << format("0x%" PRIx64, c.protocols);
60820b57cec5SDimitry Andric   outs() << "\n";
60830b57cec5SDimitry Andric   if (c.protocols + n_value != 0)
60840b57cec5SDimitry Andric     print_protocol_list64_t(c.protocols + n_value, info);
60850b57cec5SDimitry Andric 
60860b57cec5SDimitry Andric   outs() << "instanceProperties ";
60870b57cec5SDimitry Andric   sym_name =
60880b57cec5SDimitry Andric       get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
60890b57cec5SDimitry Andric                     S, info, n_value, c.instanceProperties);
60900b57cec5SDimitry Andric   if (n_value != 0) {
60910b57cec5SDimitry Andric     if (info->verbose && sym_name != nullptr)
60920b57cec5SDimitry Andric       outs() << sym_name;
60930b57cec5SDimitry Andric     else
60940b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, n_value);
60950b57cec5SDimitry Andric     if (c.instanceProperties != 0)
60960b57cec5SDimitry Andric       outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
60970b57cec5SDimitry Andric   } else
60980b57cec5SDimitry Andric     outs() << format("0x%" PRIx64, c.instanceProperties);
60990b57cec5SDimitry Andric   outs() << "\n";
61000b57cec5SDimitry Andric   if (c.instanceProperties + n_value != 0)
61010b57cec5SDimitry Andric     print_objc_property_list64(c.instanceProperties + n_value, info);
61020b57cec5SDimitry Andric }
61030b57cec5SDimitry Andric 
print_category32_t(uint32_t p,struct DisassembleInfo * info)61040b57cec5SDimitry Andric static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
61050b57cec5SDimitry Andric   struct category32_t c;
61060b57cec5SDimitry Andric   const char *r;
61070b57cec5SDimitry Andric   uint32_t offset, left;
61080b57cec5SDimitry Andric   SectionRef S, xS;
61090b57cec5SDimitry Andric   const char *name;
61100b57cec5SDimitry Andric 
61110b57cec5SDimitry Andric   r = get_pointer_32(p, offset, left, S, info);
61120b57cec5SDimitry Andric   if (r == nullptr)
61130b57cec5SDimitry Andric     return;
61140b57cec5SDimitry Andric   memset(&c, '\0', sizeof(struct category32_t));
61150b57cec5SDimitry Andric   if (left < sizeof(struct category32_t)) {
61160b57cec5SDimitry Andric     memcpy(&c, r, left);
61170b57cec5SDimitry Andric     outs() << "   (category_t entends past the end of the section)\n";
61180b57cec5SDimitry Andric   } else
61190b57cec5SDimitry Andric     memcpy(&c, r, sizeof(struct category32_t));
61200b57cec5SDimitry Andric   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
61210b57cec5SDimitry Andric     swapStruct(c);
61220b57cec5SDimitry Andric 
61230b57cec5SDimitry Andric   outs() << "              name " << format("0x%" PRIx32, c.name);
61240b57cec5SDimitry Andric   name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
61250b57cec5SDimitry Andric                        c.name);
61260b57cec5SDimitry Andric   if (name)
61270b57cec5SDimitry Andric     outs() << " " << name;
61280b57cec5SDimitry Andric   outs() << "\n";
61290b57cec5SDimitry Andric 
61300b57cec5SDimitry Andric   outs() << "               cls " << format("0x%" PRIx32, c.cls) << "\n";
61310b57cec5SDimitry Andric   if (c.cls != 0)
61320b57cec5SDimitry Andric     print_class32_t(c.cls, info);
61330b57cec5SDimitry Andric   outs() << "   instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
61340b57cec5SDimitry Andric          << "\n";
61350b57cec5SDimitry Andric   if (c.instanceMethods != 0)
61360b57cec5SDimitry Andric     print_method_list32_t(c.instanceMethods, info, "");
61370b57cec5SDimitry Andric   outs() << "      classMethods " << format("0x%" PRIx32, c.classMethods)
61380b57cec5SDimitry Andric          << "\n";
61390b57cec5SDimitry Andric   if (c.classMethods != 0)
61400b57cec5SDimitry Andric     print_method_list32_t(c.classMethods, info, "");
61410b57cec5SDimitry Andric   outs() << "         protocols " << format("0x%" PRIx32, c.protocols) << "\n";
61420b57cec5SDimitry Andric   if (c.protocols != 0)
61430b57cec5SDimitry Andric     print_protocol_list32_t(c.protocols, info);
61440b57cec5SDimitry Andric   outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
61450b57cec5SDimitry Andric          << "\n";
61460b57cec5SDimitry Andric   if (c.instanceProperties != 0)
61470b57cec5SDimitry Andric     print_objc_property_list32(c.instanceProperties, info);
61480b57cec5SDimitry Andric }
61490b57cec5SDimitry Andric 
print_message_refs64(SectionRef S,struct DisassembleInfo * info)61500b57cec5SDimitry Andric static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
61510b57cec5SDimitry Andric   uint32_t i, left, offset, xoffset;
61520b57cec5SDimitry Andric   uint64_t p, n_value;
61530b57cec5SDimitry Andric   struct message_ref64 mr;
61540b57cec5SDimitry Andric   const char *name, *sym_name;
61550b57cec5SDimitry Andric   const char *r;
61560b57cec5SDimitry Andric   SectionRef xS;
61570b57cec5SDimitry Andric 
61580b57cec5SDimitry Andric   if (S == SectionRef())
61590b57cec5SDimitry Andric     return;
61600b57cec5SDimitry Andric 
61610b57cec5SDimitry Andric   StringRef SectName;
61628bcb0991SDimitry Andric   Expected<StringRef> SecNameOrErr = S.getName();
61638bcb0991SDimitry Andric   if (SecNameOrErr)
61648bcb0991SDimitry Andric     SectName = *SecNameOrErr;
61658bcb0991SDimitry Andric   else
61668bcb0991SDimitry Andric     consumeError(SecNameOrErr.takeError());
61678bcb0991SDimitry Andric 
61680b57cec5SDimitry Andric   DataRefImpl Ref = S.getRawDataRefImpl();
61690b57cec5SDimitry Andric   StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
61700b57cec5SDimitry Andric   outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
61710b57cec5SDimitry Andric   offset = 0;
61720b57cec5SDimitry Andric   for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
61730b57cec5SDimitry Andric     p = S.getAddress() + i;
61740b57cec5SDimitry Andric     r = get_pointer_64(p, offset, left, S, info);
61750b57cec5SDimitry Andric     if (r == nullptr)
61760b57cec5SDimitry Andric       return;
61770b57cec5SDimitry Andric     memset(&mr, '\0', sizeof(struct message_ref64));
61780b57cec5SDimitry Andric     if (left < sizeof(struct message_ref64)) {
61790b57cec5SDimitry Andric       memcpy(&mr, r, left);
61800b57cec5SDimitry Andric       outs() << "   (message_ref entends past the end of the section)\n";
61810b57cec5SDimitry Andric     } else
61820b57cec5SDimitry Andric       memcpy(&mr, r, sizeof(struct message_ref64));
61830b57cec5SDimitry Andric     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
61840b57cec5SDimitry Andric       swapStruct(mr);
61850b57cec5SDimitry Andric 
61860b57cec5SDimitry Andric     outs() << "  imp ";
61870b57cec5SDimitry Andric     name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
61880b57cec5SDimitry Andric                          n_value, mr.imp);
61890b57cec5SDimitry Andric     if (n_value != 0) {
61900b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, n_value) << " ";
61910b57cec5SDimitry Andric       if (mr.imp != 0)
61920b57cec5SDimitry Andric         outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
61930b57cec5SDimitry Andric     } else
61940b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, mr.imp) << " ";
61950b57cec5SDimitry Andric     if (name != nullptr)
61960b57cec5SDimitry Andric       outs() << " " << name;
61970b57cec5SDimitry Andric     outs() << "\n";
61980b57cec5SDimitry Andric 
61990b57cec5SDimitry Andric     outs() << "  sel ";
62000b57cec5SDimitry Andric     sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
62010b57cec5SDimitry Andric                              info, n_value, mr.sel);
62020b57cec5SDimitry Andric     if (n_value != 0) {
62030b57cec5SDimitry Andric       if (info->verbose && sym_name != nullptr)
62040b57cec5SDimitry Andric         outs() << sym_name;
62050b57cec5SDimitry Andric       else
62060b57cec5SDimitry Andric         outs() << format("0x%" PRIx64, n_value);
62070b57cec5SDimitry Andric       if (mr.sel != 0)
62080b57cec5SDimitry Andric         outs() << " + " << format("0x%" PRIx64, mr.sel);
62090b57cec5SDimitry Andric     } else
62100b57cec5SDimitry Andric       outs() << format("0x%" PRIx64, mr.sel);
62110b57cec5SDimitry Andric     name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
62120b57cec5SDimitry Andric     if (name != nullptr)
62130b57cec5SDimitry Andric       outs() << format(" %.*s", left, name);
62140b57cec5SDimitry Andric     outs() << "\n";
62150b57cec5SDimitry Andric 
62160b57cec5SDimitry Andric     offset += sizeof(struct message_ref64);
62170b57cec5SDimitry Andric   }
62180b57cec5SDimitry Andric }
62190b57cec5SDimitry Andric 
print_message_refs32(SectionRef S,struct DisassembleInfo * info)62200b57cec5SDimitry Andric static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
62210b57cec5SDimitry Andric   uint32_t i, left, offset, xoffset, p;
62220b57cec5SDimitry Andric   struct message_ref32 mr;
62230b57cec5SDimitry Andric   const char *name, *r;
62240b57cec5SDimitry Andric   SectionRef xS;
62250b57cec5SDimitry Andric 
62260b57cec5SDimitry Andric   if (S == SectionRef())
62270b57cec5SDimitry Andric     return;
62280b57cec5SDimitry Andric 
62290b57cec5SDimitry Andric   StringRef SectName;
62308bcb0991SDimitry Andric   Expected<StringRef> SecNameOrErr = S.getName();
62318bcb0991SDimitry Andric   if (SecNameOrErr)
62328bcb0991SDimitry Andric     SectName = *SecNameOrErr;
62338bcb0991SDimitry Andric   else
62348bcb0991SDimitry Andric     consumeError(SecNameOrErr.takeError());
62358bcb0991SDimitry Andric 
62360b57cec5SDimitry Andric   DataRefImpl Ref = S.getRawDataRefImpl();
62370b57cec5SDimitry Andric   StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
62380b57cec5SDimitry Andric   outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
62390b57cec5SDimitry Andric   offset = 0;
62400b57cec5SDimitry Andric   for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
62410b57cec5SDimitry Andric     p = S.getAddress() + i;
62420b57cec5SDimitry Andric     r = get_pointer_32(p, offset, left, S, info);
62430b57cec5SDimitry Andric     if (r == nullptr)
62440b57cec5SDimitry Andric       return;
62450b57cec5SDimitry Andric     memset(&mr, '\0', sizeof(struct message_ref32));
62460b57cec5SDimitry Andric     if (left < sizeof(struct message_ref32)) {
62470b57cec5SDimitry Andric       memcpy(&mr, r, left);
62480b57cec5SDimitry Andric       outs() << "   (message_ref entends past the end of the section)\n";
62490b57cec5SDimitry Andric     } else
62500b57cec5SDimitry Andric       memcpy(&mr, r, sizeof(struct message_ref32));
62510b57cec5SDimitry Andric     if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
62520b57cec5SDimitry Andric       swapStruct(mr);
62530b57cec5SDimitry Andric 
62540b57cec5SDimitry Andric     outs() << "  imp " << format("0x%" PRIx32, mr.imp);
62550b57cec5SDimitry Andric     name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
62560b57cec5SDimitry Andric                          mr.imp);
62570b57cec5SDimitry Andric     if (name != nullptr)
62580b57cec5SDimitry Andric       outs() << " " << name;
62590b57cec5SDimitry Andric     outs() << "\n";
62600b57cec5SDimitry Andric 
62610b57cec5SDimitry Andric     outs() << "  sel " << format("0x%" PRIx32, mr.sel);
62620b57cec5SDimitry Andric     name = get_pointer_32(mr.sel, xoffset, left, xS, info);
62630b57cec5SDimitry Andric     if (name != nullptr)
62640b57cec5SDimitry Andric       outs() << " " << name;
62650b57cec5SDimitry Andric     outs() << "\n";
62660b57cec5SDimitry Andric 
62670b57cec5SDimitry Andric     offset += sizeof(struct message_ref32);
62680b57cec5SDimitry Andric   }
62690b57cec5SDimitry Andric }
62700b57cec5SDimitry Andric 
print_image_info64(SectionRef S,struct DisassembleInfo * info)62710b57cec5SDimitry Andric static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
62720b57cec5SDimitry Andric   uint32_t left, offset, swift_version;
62730b57cec5SDimitry Andric   uint64_t p;
62740b57cec5SDimitry Andric   struct objc_image_info64 o;
62750b57cec5SDimitry Andric   const char *r;
62760b57cec5SDimitry Andric 
62770b57cec5SDimitry Andric   if (S == SectionRef())
62780b57cec5SDimitry Andric     return;
62790b57cec5SDimitry Andric 
62800b57cec5SDimitry Andric   StringRef SectName;
62818bcb0991SDimitry Andric   Expected<StringRef> SecNameOrErr = S.getName();
62828bcb0991SDimitry Andric   if (SecNameOrErr)
62838bcb0991SDimitry Andric     SectName = *SecNameOrErr;
62848bcb0991SDimitry Andric   else
62858bcb0991SDimitry Andric     consumeError(SecNameOrErr.takeError());
62868bcb0991SDimitry Andric 
62870b57cec5SDimitry Andric   DataRefImpl Ref = S.getRawDataRefImpl();
62880b57cec5SDimitry Andric   StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
62890b57cec5SDimitry Andric   outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
62900b57cec5SDimitry Andric   p = S.getAddress();
62910b57cec5SDimitry Andric   r = get_pointer_64(p, offset, left, S, info);
62920b57cec5SDimitry Andric   if (r == nullptr)
62930b57cec5SDimitry Andric     return;
62940b57cec5SDimitry Andric   memset(&o, '\0', sizeof(struct objc_image_info64));
62950b57cec5SDimitry Andric   if (left < sizeof(struct objc_image_info64)) {
62960b57cec5SDimitry Andric     memcpy(&o, r, left);
62970b57cec5SDimitry Andric     outs() << "   (objc_image_info entends past the end of the section)\n";
62980b57cec5SDimitry Andric   } else
62990b57cec5SDimitry Andric     memcpy(&o, r, sizeof(struct objc_image_info64));
63000b57cec5SDimitry Andric   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
63010b57cec5SDimitry Andric     swapStruct(o);
63020b57cec5SDimitry Andric   outs() << "  version " << o.version << "\n";
63030b57cec5SDimitry Andric   outs() << "    flags " << format("0x%" PRIx32, o.flags);
63040b57cec5SDimitry Andric   if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
63050b57cec5SDimitry Andric     outs() << " OBJC_IMAGE_IS_REPLACEMENT";
63060b57cec5SDimitry Andric   if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
63070b57cec5SDimitry Andric     outs() << " OBJC_IMAGE_SUPPORTS_GC";
63080b57cec5SDimitry Andric   if (o.flags & OBJC_IMAGE_IS_SIMULATED)
63090b57cec5SDimitry Andric     outs() << " OBJC_IMAGE_IS_SIMULATED";
63100b57cec5SDimitry Andric   if (o.flags & OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES)
63110b57cec5SDimitry Andric     outs() << " OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES";
63120b57cec5SDimitry Andric   swift_version = (o.flags >> 8) & 0xff;
63130b57cec5SDimitry Andric   if (swift_version != 0) {
63140b57cec5SDimitry Andric     if (swift_version == 1)
63150b57cec5SDimitry Andric       outs() << " Swift 1.0";
63160b57cec5SDimitry Andric     else if (swift_version == 2)
63170b57cec5SDimitry Andric       outs() << " Swift 1.1";
63180b57cec5SDimitry Andric     else if(swift_version == 3)
63190b57cec5SDimitry Andric       outs() << " Swift 2.0";
63200b57cec5SDimitry Andric     else if(swift_version == 4)
63210b57cec5SDimitry Andric       outs() << " Swift 3.0";
63220b57cec5SDimitry Andric     else if(swift_version == 5)
63230b57cec5SDimitry Andric       outs() << " Swift 4.0";
63240b57cec5SDimitry Andric     else if(swift_version == 6)
63250b57cec5SDimitry Andric       outs() << " Swift 4.1/Swift 4.2";
63260b57cec5SDimitry Andric     else if(swift_version == 7)
63270b57cec5SDimitry Andric       outs() << " Swift 5 or later";
63280b57cec5SDimitry Andric     else
63290b57cec5SDimitry Andric       outs() << " unknown future Swift version (" << swift_version << ")";
63300b57cec5SDimitry Andric   }
63310b57cec5SDimitry Andric   outs() << "\n";
63320b57cec5SDimitry Andric }
63330b57cec5SDimitry Andric 
print_image_info32(SectionRef S,struct DisassembleInfo * info)63340b57cec5SDimitry Andric static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
63350b57cec5SDimitry Andric   uint32_t left, offset, swift_version, p;
63360b57cec5SDimitry Andric   struct objc_image_info32 o;
63370b57cec5SDimitry Andric   const char *r;
63380b57cec5SDimitry Andric 
63390b57cec5SDimitry Andric   if (S == SectionRef())
63400b57cec5SDimitry Andric     return;
63410b57cec5SDimitry Andric 
63420b57cec5SDimitry Andric   StringRef SectName;
63438bcb0991SDimitry Andric   Expected<StringRef> SecNameOrErr = S.getName();
63448bcb0991SDimitry Andric   if (SecNameOrErr)
63458bcb0991SDimitry Andric     SectName = *SecNameOrErr;
63468bcb0991SDimitry Andric   else
63478bcb0991SDimitry Andric     consumeError(SecNameOrErr.takeError());
63488bcb0991SDimitry Andric 
63490b57cec5SDimitry Andric   DataRefImpl Ref = S.getRawDataRefImpl();
63500b57cec5SDimitry Andric   StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
63510b57cec5SDimitry Andric   outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
63520b57cec5SDimitry Andric   p = S.getAddress();
63530b57cec5SDimitry Andric   r = get_pointer_32(p, offset, left, S, info);
63540b57cec5SDimitry Andric   if (r == nullptr)
63550b57cec5SDimitry Andric     return;
63560b57cec5SDimitry Andric   memset(&o, '\0', sizeof(struct objc_image_info32));
63570b57cec5SDimitry Andric   if (left < sizeof(struct objc_image_info32)) {
63580b57cec5SDimitry Andric     memcpy(&o, r, left);
63590b57cec5SDimitry Andric     outs() << "   (objc_image_info entends past the end of the section)\n";
63600b57cec5SDimitry Andric   } else
63610b57cec5SDimitry Andric     memcpy(&o, r, sizeof(struct objc_image_info32));
63620b57cec5SDimitry Andric   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
63630b57cec5SDimitry Andric     swapStruct(o);
63640b57cec5SDimitry Andric   outs() << "  version " << o.version << "\n";
63650b57cec5SDimitry Andric   outs() << "    flags " << format("0x%" PRIx32, o.flags);
63660b57cec5SDimitry Andric   if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
63670b57cec5SDimitry Andric     outs() << " OBJC_IMAGE_IS_REPLACEMENT";
63680b57cec5SDimitry Andric   if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
63690b57cec5SDimitry Andric     outs() << " OBJC_IMAGE_SUPPORTS_GC";
63700b57cec5SDimitry Andric   swift_version = (o.flags >> 8) & 0xff;
63710b57cec5SDimitry Andric   if (swift_version != 0) {
63720b57cec5SDimitry Andric     if (swift_version == 1)
63730b57cec5SDimitry Andric       outs() << " Swift 1.0";
63740b57cec5SDimitry Andric     else if (swift_version == 2)
63750b57cec5SDimitry Andric       outs() << " Swift 1.1";
63760b57cec5SDimitry Andric     else if(swift_version == 3)
63770b57cec5SDimitry Andric       outs() << " Swift 2.0";
63780b57cec5SDimitry Andric     else if(swift_version == 4)
63790b57cec5SDimitry Andric       outs() << " Swift 3.0";
63800b57cec5SDimitry Andric     else if(swift_version == 5)
63810b57cec5SDimitry Andric       outs() << " Swift 4.0";
63820b57cec5SDimitry Andric     else if(swift_version == 6)
63830b57cec5SDimitry Andric       outs() << " Swift 4.1/Swift 4.2";
63840b57cec5SDimitry Andric     else if(swift_version == 7)
63850b57cec5SDimitry Andric       outs() << " Swift 5 or later";
63860b57cec5SDimitry Andric     else
63870b57cec5SDimitry Andric       outs() << " unknown future Swift version (" << swift_version << ")";
63880b57cec5SDimitry Andric   }
63890b57cec5SDimitry Andric   outs() << "\n";
63900b57cec5SDimitry Andric }
63910b57cec5SDimitry Andric 
print_image_info(SectionRef S,struct DisassembleInfo * info)63920b57cec5SDimitry Andric static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
63930b57cec5SDimitry Andric   uint32_t left, offset, p;
63940b57cec5SDimitry Andric   struct imageInfo_t o;
63950b57cec5SDimitry Andric   const char *r;
63960b57cec5SDimitry Andric 
63970b57cec5SDimitry Andric   StringRef SectName;
63988bcb0991SDimitry Andric   Expected<StringRef> SecNameOrErr = S.getName();
63998bcb0991SDimitry Andric   if (SecNameOrErr)
64008bcb0991SDimitry Andric     SectName = *SecNameOrErr;
64018bcb0991SDimitry Andric   else
64028bcb0991SDimitry Andric     consumeError(SecNameOrErr.takeError());
64038bcb0991SDimitry Andric 
64040b57cec5SDimitry Andric   DataRefImpl Ref = S.getRawDataRefImpl();
64050b57cec5SDimitry Andric   StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
64060b57cec5SDimitry Andric   outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
64070b57cec5SDimitry Andric   p = S.getAddress();
64080b57cec5SDimitry Andric   r = get_pointer_32(p, offset, left, S, info);
64090b57cec5SDimitry Andric   if (r == nullptr)
64100b57cec5SDimitry Andric     return;
64110b57cec5SDimitry Andric   memset(&o, '\0', sizeof(struct imageInfo_t));
64120b57cec5SDimitry Andric   if (left < sizeof(struct imageInfo_t)) {
64130b57cec5SDimitry Andric     memcpy(&o, r, left);
64140b57cec5SDimitry Andric     outs() << " (imageInfo entends past the end of the section)\n";
64150b57cec5SDimitry Andric   } else
64160b57cec5SDimitry Andric     memcpy(&o, r, sizeof(struct imageInfo_t));
64170b57cec5SDimitry Andric   if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
64180b57cec5SDimitry Andric     swapStruct(o);
64190b57cec5SDimitry Andric   outs() << "  version " << o.version << "\n";
64200b57cec5SDimitry Andric   outs() << "    flags " << format("0x%" PRIx32, o.flags);
64210b57cec5SDimitry Andric   if (o.flags & 0x1)
64220b57cec5SDimitry Andric     outs() << "  F&C";
64230b57cec5SDimitry Andric   if (o.flags & 0x2)
64240b57cec5SDimitry Andric     outs() << " GC";
64250b57cec5SDimitry Andric   if (o.flags & 0x4)
64260b57cec5SDimitry Andric     outs() << " GC-only";
64270b57cec5SDimitry Andric   else
64280b57cec5SDimitry Andric     outs() << " RR";
64290b57cec5SDimitry Andric   outs() << "\n";
64300b57cec5SDimitry Andric }
64310b57cec5SDimitry Andric 
printObjc2_64bit_MetaData(MachOObjectFile * O,bool verbose)64320b57cec5SDimitry Andric static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
64330b57cec5SDimitry Andric   SymbolAddressMap AddrMap;
64340b57cec5SDimitry Andric   if (verbose)
64350b57cec5SDimitry Andric     CreateSymbolAddressMap(O, &AddrMap);
64360b57cec5SDimitry Andric 
64370b57cec5SDimitry Andric   std::vector<SectionRef> Sections;
6438fe6060f1SDimitry Andric   append_range(Sections, O->sections());
64390b57cec5SDimitry Andric 
64400b57cec5SDimitry Andric   struct DisassembleInfo info(O, &AddrMap, &Sections, verbose);
64410b57cec5SDimitry Andric 
64420b57cec5SDimitry Andric   SectionRef CL = get_section(O, "__OBJC2", "__class_list");
64430b57cec5SDimitry Andric   if (CL == SectionRef())
64440b57cec5SDimitry Andric     CL = get_section(O, "__DATA", "__objc_classlist");
64450b57cec5SDimitry Andric   if (CL == SectionRef())
64460b57cec5SDimitry Andric     CL = get_section(O, "__DATA_CONST", "__objc_classlist");
64470b57cec5SDimitry Andric   if (CL == SectionRef())
64480b57cec5SDimitry Andric     CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
64490b57cec5SDimitry Andric   info.S = CL;
64500b57cec5SDimitry Andric   walk_pointer_list_64("class", CL, O, &info, print_class64_t);
64510b57cec5SDimitry Andric 
64520b57cec5SDimitry Andric   SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
64530b57cec5SDimitry Andric   if (CR == SectionRef())
64540b57cec5SDimitry Andric     CR = get_section(O, "__DATA", "__objc_classrefs");
64550b57cec5SDimitry Andric   if (CR == SectionRef())
64560b57cec5SDimitry Andric     CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
64570b57cec5SDimitry Andric   if (CR == SectionRef())
64580b57cec5SDimitry Andric     CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
64590b57cec5SDimitry Andric   info.S = CR;
64600b57cec5SDimitry Andric   walk_pointer_list_64("class refs", CR, O, &info, nullptr);
64610b57cec5SDimitry Andric 
64620b57cec5SDimitry Andric   SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
64630b57cec5SDimitry Andric   if (SR == SectionRef())
64640b57cec5SDimitry Andric     SR = get_section(O, "__DATA", "__objc_superrefs");
64650b57cec5SDimitry Andric   if (SR == SectionRef())
64660b57cec5SDimitry Andric     SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
64670b57cec5SDimitry Andric   if (SR == SectionRef())
64680b57cec5SDimitry Andric     SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
64690b57cec5SDimitry Andric   info.S = SR;
64700b57cec5SDimitry Andric   walk_pointer_list_64("super refs", SR, O, &info, nullptr);
64710b57cec5SDimitry Andric 
64720b57cec5SDimitry Andric   SectionRef CA = get_section(O, "__OBJC2", "__category_list");
64730b57cec5SDimitry Andric   if (CA == SectionRef())
64740b57cec5SDimitry Andric     CA = get_section(O, "__DATA", "__objc_catlist");
64750b57cec5SDimitry Andric   if (CA == SectionRef())
64760b57cec5SDimitry Andric     CA = get_section(O, "__DATA_CONST", "__objc_catlist");
64770b57cec5SDimitry Andric   if (CA == SectionRef())
64780b57cec5SDimitry Andric     CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
64790b57cec5SDimitry Andric   info.S = CA;
64800b57cec5SDimitry Andric   walk_pointer_list_64("category", CA, O, &info, print_category64_t);
64810b57cec5SDimitry Andric 
64820b57cec5SDimitry Andric   SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
64830b57cec5SDimitry Andric   if (PL == SectionRef())
64840b57cec5SDimitry Andric     PL = get_section(O, "__DATA", "__objc_protolist");
64850b57cec5SDimitry Andric   if (PL == SectionRef())
64860b57cec5SDimitry Andric     PL = get_section(O, "__DATA_CONST", "__objc_protolist");
64870b57cec5SDimitry Andric   if (PL == SectionRef())
64880b57cec5SDimitry Andric     PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
64890b57cec5SDimitry Andric   info.S = PL;
64900b57cec5SDimitry Andric   walk_pointer_list_64("protocol", PL, O, &info, nullptr);
64910b57cec5SDimitry Andric 
64920b57cec5SDimitry Andric   SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
64930b57cec5SDimitry Andric   if (MR == SectionRef())
64940b57cec5SDimitry Andric     MR = get_section(O, "__DATA", "__objc_msgrefs");
64950b57cec5SDimitry Andric   if (MR == SectionRef())
64960b57cec5SDimitry Andric     MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
64970b57cec5SDimitry Andric   if (MR == SectionRef())
64980b57cec5SDimitry Andric     MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
64990b57cec5SDimitry Andric   info.S = MR;
65000b57cec5SDimitry Andric   print_message_refs64(MR, &info);
65010b57cec5SDimitry Andric 
65020b57cec5SDimitry Andric   SectionRef II = get_section(O, "__OBJC2", "__image_info");
65030b57cec5SDimitry Andric   if (II == SectionRef())
65040b57cec5SDimitry Andric     II = get_section(O, "__DATA", "__objc_imageinfo");
65050b57cec5SDimitry Andric   if (II == SectionRef())
65060b57cec5SDimitry Andric     II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
65070b57cec5SDimitry Andric   if (II == SectionRef())
65080b57cec5SDimitry Andric     II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
65090b57cec5SDimitry Andric   info.S = II;
65100b57cec5SDimitry Andric   print_image_info64(II, &info);
65110b57cec5SDimitry Andric }
65120b57cec5SDimitry Andric 
printObjc2_32bit_MetaData(MachOObjectFile * O,bool verbose)65130b57cec5SDimitry Andric static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
65140b57cec5SDimitry Andric   SymbolAddressMap AddrMap;
65150b57cec5SDimitry Andric   if (verbose)
65160b57cec5SDimitry Andric     CreateSymbolAddressMap(O, &AddrMap);
65170b57cec5SDimitry Andric 
65180b57cec5SDimitry Andric   std::vector<SectionRef> Sections;
6519fe6060f1SDimitry Andric   append_range(Sections, O->sections());
65200b57cec5SDimitry Andric 
65210b57cec5SDimitry Andric   struct DisassembleInfo info(O, &AddrMap, &Sections, verbose);
65220b57cec5SDimitry Andric 
65230b57cec5SDimitry Andric   SectionRef CL = get_section(O, "__OBJC2", "__class_list");
65240b57cec5SDimitry Andric   if (CL == SectionRef())
65250b57cec5SDimitry Andric     CL = get_section(O, "__DATA", "__objc_classlist");
65260b57cec5SDimitry Andric   if (CL == SectionRef())
65270b57cec5SDimitry Andric     CL = get_section(O, "__DATA_CONST", "__objc_classlist");
65280b57cec5SDimitry Andric   if (CL == SectionRef())
65290b57cec5SDimitry Andric     CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
65300b57cec5SDimitry Andric   info.S = CL;
65310b57cec5SDimitry Andric   walk_pointer_list_32("class", CL, O, &info, print_class32_t);
65320b57cec5SDimitry Andric 
65330b57cec5SDimitry Andric   SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
65340b57cec5SDimitry Andric   if (CR == SectionRef())
65350b57cec5SDimitry Andric     CR = get_section(O, "__DATA", "__objc_classrefs");
65360b57cec5SDimitry Andric   if (CR == SectionRef())
65370b57cec5SDimitry Andric     CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
65380b57cec5SDimitry Andric   if (CR == SectionRef())
65390b57cec5SDimitry Andric     CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
65400b57cec5SDimitry Andric   info.S = CR;
65410b57cec5SDimitry Andric   walk_pointer_list_32("class refs", CR, O, &info, nullptr);
65420b57cec5SDimitry Andric 
65430b57cec5SDimitry Andric   SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
65440b57cec5SDimitry Andric   if (SR == SectionRef())
65450b57cec5SDimitry Andric     SR = get_section(O, "__DATA", "__objc_superrefs");
65460b57cec5SDimitry Andric   if (SR == SectionRef())
65470b57cec5SDimitry Andric     SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
65480b57cec5SDimitry Andric   if (SR == SectionRef())
65490b57cec5SDimitry Andric     SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
65500b57cec5SDimitry Andric   info.S = SR;
65510b57cec5SDimitry Andric   walk_pointer_list_32("super refs", SR, O, &info, nullptr);
65520b57cec5SDimitry Andric 
65530b57cec5SDimitry Andric   SectionRef CA = get_section(O, "__OBJC2", "__category_list");
65540b57cec5SDimitry Andric   if (CA == SectionRef())
65550b57cec5SDimitry Andric     CA = get_section(O, "__DATA", "__objc_catlist");
65560b57cec5SDimitry Andric   if (CA == SectionRef())
65570b57cec5SDimitry Andric     CA = get_section(O, "__DATA_CONST", "__objc_catlist");
65580b57cec5SDimitry Andric   if (CA == SectionRef())
65590b57cec5SDimitry Andric     CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
65600b57cec5SDimitry Andric   info.S = CA;
65610b57cec5SDimitry Andric   walk_pointer_list_32("category", CA, O, &info, print_category32_t);
65620b57cec5SDimitry Andric 
65630b57cec5SDimitry Andric   SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
65640b57cec5SDimitry Andric   if (PL == SectionRef())
65650b57cec5SDimitry Andric     PL = get_section(O, "__DATA", "__objc_protolist");
65660b57cec5SDimitry Andric   if (PL == SectionRef())
65670b57cec5SDimitry Andric     PL = get_section(O, "__DATA_CONST", "__objc_protolist");
65680b57cec5SDimitry Andric   if (PL == SectionRef())
65690b57cec5SDimitry Andric     PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
65700b57cec5SDimitry Andric   info.S = PL;
65710b57cec5SDimitry Andric   walk_pointer_list_32("protocol", PL, O, &info, nullptr);
65720b57cec5SDimitry Andric 
65730b57cec5SDimitry Andric   SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
65740b57cec5SDimitry Andric   if (MR == SectionRef())
65750b57cec5SDimitry Andric     MR = get_section(O, "__DATA", "__objc_msgrefs");
65760b57cec5SDimitry Andric   if (MR == SectionRef())
65770b57cec5SDimitry Andric     MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
65780b57cec5SDimitry Andric   if (MR == SectionRef())
65790b57cec5SDimitry Andric     MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
65800b57cec5SDimitry Andric   info.S = MR;
65810b57cec5SDimitry Andric   print_message_refs32(MR, &info);
65820b57cec5SDimitry Andric 
65830b57cec5SDimitry Andric   SectionRef II = get_section(O, "__OBJC2", "__image_info");
65840b57cec5SDimitry Andric   if (II == SectionRef())
65850b57cec5SDimitry Andric     II = get_section(O, "__DATA", "__objc_imageinfo");
65860b57cec5SDimitry Andric   if (II == SectionRef())
65870b57cec5SDimitry Andric     II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
65880b57cec5SDimitry Andric   if (II == SectionRef())
65890b57cec5SDimitry Andric     II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
65900b57cec5SDimitry Andric   info.S = II;
65910b57cec5SDimitry Andric   print_image_info32(II, &info);
65920b57cec5SDimitry Andric }
65930b57cec5SDimitry Andric 
printObjc1_32bit_MetaData(MachOObjectFile * O,bool verbose)65940b57cec5SDimitry Andric static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
65950b57cec5SDimitry Andric   uint32_t i, j, p, offset, xoffset, left, defs_left, def;
65960b57cec5SDimitry Andric   const char *r, *name, *defs;
65970b57cec5SDimitry Andric   struct objc_module_t module;
65980b57cec5SDimitry Andric   SectionRef S, xS;
65990b57cec5SDimitry Andric   struct objc_symtab_t symtab;
66000b57cec5SDimitry Andric   struct objc_class_t objc_class;
66010b57cec5SDimitry Andric   struct objc_category_t objc_category;
66020b57cec5SDimitry Andric 
66030b57cec5SDimitry Andric   outs() << "Objective-C segment\n";
66040b57cec5SDimitry Andric   S = get_section(O, "__OBJC", "__module_info");
66050b57cec5SDimitry Andric   if (S == SectionRef())
66060b57cec5SDimitry Andric     return false;
66070b57cec5SDimitry Andric 
66080b57cec5SDimitry Andric   SymbolAddressMap AddrMap;
66090b57cec5SDimitry Andric   if (verbose)
66100b57cec5SDimitry Andric     CreateSymbolAddressMap(O, &AddrMap);
66110b57cec5SDimitry Andric 
66120b57cec5SDimitry Andric   std::vector<SectionRef> Sections;
6613fe6060f1SDimitry Andric   append_range(Sections, O->sections());
66140b57cec5SDimitry Andric 
66150b57cec5SDimitry Andric   struct DisassembleInfo info(O, &AddrMap, &Sections, verbose);
66160b57cec5SDimitry Andric 
66170b57cec5SDimitry Andric   for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
66180b57cec5SDimitry Andric     p = S.getAddress() + i;
66190b57cec5SDimitry Andric     r = get_pointer_32(p, offset, left, S, &info, true);
66200b57cec5SDimitry Andric     if (r == nullptr)
66210b57cec5SDimitry Andric       return true;
66220b57cec5SDimitry Andric     memset(&module, '\0', sizeof(struct objc_module_t));
66230b57cec5SDimitry Andric     if (left < sizeof(struct objc_module_t)) {
66240b57cec5SDimitry Andric       memcpy(&module, r, left);
66250b57cec5SDimitry Andric       outs() << "   (module extends past end of __module_info section)\n";
66260b57cec5SDimitry Andric     } else
66270b57cec5SDimitry Andric       memcpy(&module, r, sizeof(struct objc_module_t));
66280b57cec5SDimitry Andric     if (O->isLittleEndian() != sys::IsLittleEndianHost)
66290b57cec5SDimitry Andric       swapStruct(module);
66300b57cec5SDimitry Andric 
66310b57cec5SDimitry Andric     outs() << "Module " << format("0x%" PRIx32, p) << "\n";
66320b57cec5SDimitry Andric     outs() << "    version " << module.version << "\n";
66330b57cec5SDimitry Andric     outs() << "       size " << module.size << "\n";
66340b57cec5SDimitry Andric     outs() << "       name ";
66350b57cec5SDimitry Andric     name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
66360b57cec5SDimitry Andric     if (name != nullptr)
66370b57cec5SDimitry Andric       outs() << format("%.*s", left, name);
66380b57cec5SDimitry Andric     else
66390b57cec5SDimitry Andric       outs() << format("0x%08" PRIx32, module.name)
66400b57cec5SDimitry Andric              << "(not in an __OBJC section)";
66410b57cec5SDimitry Andric     outs() << "\n";
66420b57cec5SDimitry Andric 
66430b57cec5SDimitry Andric     r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
66440b57cec5SDimitry Andric     if (module.symtab == 0 || r == nullptr) {
66450b57cec5SDimitry Andric       outs() << "     symtab " << format("0x%08" PRIx32, module.symtab)
66460b57cec5SDimitry Andric              << " (not in an __OBJC section)\n";
66470b57cec5SDimitry Andric       continue;
66480b57cec5SDimitry Andric     }
66490b57cec5SDimitry Andric     outs() << "     symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
66500b57cec5SDimitry Andric     memset(&symtab, '\0', sizeof(struct objc_symtab_t));
66510b57cec5SDimitry Andric     defs_left = 0;
66520b57cec5SDimitry Andric     defs = nullptr;
66530b57cec5SDimitry Andric     if (left < sizeof(struct objc_symtab_t)) {
66540b57cec5SDimitry Andric       memcpy(&symtab, r, left);
66550b57cec5SDimitry Andric       outs() << "\tsymtab extends past end of an __OBJC section)\n";
66560b57cec5SDimitry Andric     } else {
66570b57cec5SDimitry Andric       memcpy(&symtab, r, sizeof(struct objc_symtab_t));
66580b57cec5SDimitry Andric       if (left > sizeof(struct objc_symtab_t)) {
66590b57cec5SDimitry Andric         defs_left = left - sizeof(struct objc_symtab_t);
66600b57cec5SDimitry Andric         defs = r + sizeof(struct objc_symtab_t);
66610b57cec5SDimitry Andric       }
66620b57cec5SDimitry Andric     }
66630b57cec5SDimitry Andric     if (O->isLittleEndian() != sys::IsLittleEndianHost)
66640b57cec5SDimitry Andric       swapStruct(symtab);
66650b57cec5SDimitry Andric 
66660b57cec5SDimitry Andric     outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
66670b57cec5SDimitry Andric     r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
66680b57cec5SDimitry Andric     outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
66690b57cec5SDimitry Andric     if (r == nullptr)
66700b57cec5SDimitry Andric       outs() << " (not in an __OBJC section)";
66710b57cec5SDimitry Andric     outs() << "\n";
66720b57cec5SDimitry Andric     outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
66730b57cec5SDimitry Andric     outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
66740b57cec5SDimitry Andric     if (symtab.cls_def_cnt > 0)
66750b57cec5SDimitry Andric       outs() << "\tClass Definitions\n";
66760b57cec5SDimitry Andric     for (j = 0; j < symtab.cls_def_cnt; j++) {
66770b57cec5SDimitry Andric       if ((j + 1) * sizeof(uint32_t) > defs_left) {
66780b57cec5SDimitry Andric         outs() << "\t(remaining class defs entries entends past the end of the "
66790b57cec5SDimitry Andric                << "section)\n";
66800b57cec5SDimitry Andric         break;
66810b57cec5SDimitry Andric       }
66820b57cec5SDimitry Andric       memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
66830b57cec5SDimitry Andric       if (O->isLittleEndian() != sys::IsLittleEndianHost)
66840b57cec5SDimitry Andric         sys::swapByteOrder(def);
66850b57cec5SDimitry Andric 
66860b57cec5SDimitry Andric       r = get_pointer_32(def, xoffset, left, xS, &info, true);
66870b57cec5SDimitry Andric       outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
66880b57cec5SDimitry Andric       if (r != nullptr) {
66890b57cec5SDimitry Andric         if (left > sizeof(struct objc_class_t)) {
66900b57cec5SDimitry Andric           outs() << "\n";
66910b57cec5SDimitry Andric           memcpy(&objc_class, r, sizeof(struct objc_class_t));
66920b57cec5SDimitry Andric         } else {
66930b57cec5SDimitry Andric           outs() << " (entends past the end of the section)\n";
66940b57cec5SDimitry Andric           memset(&objc_class, '\0', sizeof(struct objc_class_t));
66950b57cec5SDimitry Andric           memcpy(&objc_class, r, left);
66960b57cec5SDimitry Andric         }
66970b57cec5SDimitry Andric         if (O->isLittleEndian() != sys::IsLittleEndianHost)
66980b57cec5SDimitry Andric           swapStruct(objc_class);
66990b57cec5SDimitry Andric         print_objc_class_t(&objc_class, &info);
67000b57cec5SDimitry Andric       } else {
67010b57cec5SDimitry Andric         outs() << "(not in an __OBJC section)\n";
67020b57cec5SDimitry Andric       }
67030b57cec5SDimitry Andric 
67040b57cec5SDimitry Andric       if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
67050b57cec5SDimitry Andric         outs() << "\tMeta Class";
67060b57cec5SDimitry Andric         r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
67070b57cec5SDimitry Andric         if (r != nullptr) {
67080b57cec5SDimitry Andric           if (left > sizeof(struct objc_class_t)) {
67090b57cec5SDimitry Andric             outs() << "\n";
67100b57cec5SDimitry Andric             memcpy(&objc_class, r, sizeof(struct objc_class_t));
67110b57cec5SDimitry Andric           } else {
67120b57cec5SDimitry Andric             outs() << " (entends past the end of the section)\n";
67130b57cec5SDimitry Andric             memset(&objc_class, '\0', sizeof(struct objc_class_t));
67140b57cec5SDimitry Andric             memcpy(&objc_class, r, left);
67150b57cec5SDimitry Andric           }
67160b57cec5SDimitry Andric           if (O->isLittleEndian() != sys::IsLittleEndianHost)
67170b57cec5SDimitry Andric             swapStruct(objc_class);
67180b57cec5SDimitry Andric           print_objc_class_t(&objc_class, &info);
67190b57cec5SDimitry Andric         } else {
67200b57cec5SDimitry Andric           outs() << "(not in an __OBJC section)\n";
67210b57cec5SDimitry Andric         }
67220b57cec5SDimitry Andric       }
67230b57cec5SDimitry Andric     }
67240b57cec5SDimitry Andric     if (symtab.cat_def_cnt > 0)
67250b57cec5SDimitry Andric       outs() << "\tCategory Definitions\n";
67260b57cec5SDimitry Andric     for (j = 0; j < symtab.cat_def_cnt; j++) {
67270b57cec5SDimitry Andric       if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
67280b57cec5SDimitry Andric         outs() << "\t(remaining category defs entries entends past the end of "
67290b57cec5SDimitry Andric                << "the section)\n";
67300b57cec5SDimitry Andric         break;
67310b57cec5SDimitry Andric       }
67320b57cec5SDimitry Andric       memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
67330b57cec5SDimitry Andric              sizeof(uint32_t));
67340b57cec5SDimitry Andric       if (O->isLittleEndian() != sys::IsLittleEndianHost)
67350b57cec5SDimitry Andric         sys::swapByteOrder(def);
67360b57cec5SDimitry Andric 
67370b57cec5SDimitry Andric       r = get_pointer_32(def, xoffset, left, xS, &info, true);
67380b57cec5SDimitry Andric       outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
67390b57cec5SDimitry Andric              << format("0x%08" PRIx32, def);
67400b57cec5SDimitry Andric       if (r != nullptr) {
67410b57cec5SDimitry Andric         if (left > sizeof(struct objc_category_t)) {
67420b57cec5SDimitry Andric           outs() << "\n";
67430b57cec5SDimitry Andric           memcpy(&objc_category, r, sizeof(struct objc_category_t));
67440b57cec5SDimitry Andric         } else {
67450b57cec5SDimitry Andric           outs() << " (entends past the end of the section)\n";
67460b57cec5SDimitry Andric           memset(&objc_category, '\0', sizeof(struct objc_category_t));
67470b57cec5SDimitry Andric           memcpy(&objc_category, r, left);
67480b57cec5SDimitry Andric         }
67490b57cec5SDimitry Andric         if (O->isLittleEndian() != sys::IsLittleEndianHost)
67500b57cec5SDimitry Andric           swapStruct(objc_category);
67510b57cec5SDimitry Andric         print_objc_objc_category_t(&objc_category, &info);
67520b57cec5SDimitry Andric       } else {
67530b57cec5SDimitry Andric         outs() << "(not in an __OBJC section)\n";
67540b57cec5SDimitry Andric       }
67550b57cec5SDimitry Andric     }
67560b57cec5SDimitry Andric   }
67570b57cec5SDimitry Andric   const SectionRef II = get_section(O, "__OBJC", "__image_info");
67580b57cec5SDimitry Andric   if (II != SectionRef())
67590b57cec5SDimitry Andric     print_image_info(II, &info);
67600b57cec5SDimitry Andric 
67610b57cec5SDimitry Andric   return true;
67620b57cec5SDimitry Andric }
67630b57cec5SDimitry Andric 
DumpProtocolSection(MachOObjectFile * O,const char * sect,uint32_t size,uint32_t addr)67640b57cec5SDimitry Andric static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
67650b57cec5SDimitry Andric                                 uint32_t size, uint32_t addr) {
67660b57cec5SDimitry Andric   SymbolAddressMap AddrMap;
67670b57cec5SDimitry Andric   CreateSymbolAddressMap(O, &AddrMap);
67680b57cec5SDimitry Andric 
67690b57cec5SDimitry Andric   std::vector<SectionRef> Sections;
6770fe6060f1SDimitry Andric   append_range(Sections, O->sections());
67710b57cec5SDimitry Andric 
67720b57cec5SDimitry Andric   struct DisassembleInfo info(O, &AddrMap, &Sections, true);
67730b57cec5SDimitry Andric 
67740b57cec5SDimitry Andric   const char *p;
67750b57cec5SDimitry Andric   struct objc_protocol_t protocol;
67760b57cec5SDimitry Andric   uint32_t left, paddr;
67770b57cec5SDimitry Andric   for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
67780b57cec5SDimitry Andric     memset(&protocol, '\0', sizeof(struct objc_protocol_t));
67790b57cec5SDimitry Andric     left = size - (p - sect);
67800b57cec5SDimitry Andric     if (left < sizeof(struct objc_protocol_t)) {
67810b57cec5SDimitry Andric       outs() << "Protocol extends past end of __protocol section\n";
67820b57cec5SDimitry Andric       memcpy(&protocol, p, left);
67830b57cec5SDimitry Andric     } else
67840b57cec5SDimitry Andric       memcpy(&protocol, p, sizeof(struct objc_protocol_t));
67850b57cec5SDimitry Andric     if (O->isLittleEndian() != sys::IsLittleEndianHost)
67860b57cec5SDimitry Andric       swapStruct(protocol);
67870b57cec5SDimitry Andric     paddr = addr + (p - sect);
67880b57cec5SDimitry Andric     outs() << "Protocol " << format("0x%" PRIx32, paddr);
67890b57cec5SDimitry Andric     if (print_protocol(paddr, 0, &info))
67900b57cec5SDimitry Andric       outs() << "(not in an __OBJC section)\n";
67910b57cec5SDimitry Andric   }
67920b57cec5SDimitry Andric }
67930b57cec5SDimitry Andric 
printObjcMetaData(MachOObjectFile * O,bool verbose)67940b57cec5SDimitry Andric static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
67950b57cec5SDimitry Andric   if (O->is64Bit())
67960b57cec5SDimitry Andric     printObjc2_64bit_MetaData(O, verbose);
67970b57cec5SDimitry Andric   else {
67980b57cec5SDimitry Andric     MachO::mach_header H;
67990b57cec5SDimitry Andric     H = O->getHeader();
68000b57cec5SDimitry Andric     if (H.cputype == MachO::CPU_TYPE_ARM)
68010b57cec5SDimitry Andric       printObjc2_32bit_MetaData(O, verbose);
68020b57cec5SDimitry Andric     else {
68030b57cec5SDimitry Andric       // This is the 32-bit non-arm cputype case.  Which is normally
68040b57cec5SDimitry Andric       // the first Objective-C ABI.  But it may be the case of a
68050b57cec5SDimitry Andric       // binary for the iOS simulator which is the second Objective-C
68060b57cec5SDimitry Andric       // ABI.  In that case printObjc1_32bit_MetaData() will determine that
68070b57cec5SDimitry Andric       // and return false.
68080b57cec5SDimitry Andric       if (!printObjc1_32bit_MetaData(O, verbose))
68090b57cec5SDimitry Andric         printObjc2_32bit_MetaData(O, verbose);
68100b57cec5SDimitry Andric     }
68110b57cec5SDimitry Andric   }
68120b57cec5SDimitry Andric }
68130b57cec5SDimitry Andric 
68140b57cec5SDimitry Andric // GuessLiteralPointer returns a string which for the item in the Mach-O file
68150b57cec5SDimitry Andric // for the address passed in as ReferenceValue for printing as a comment with
68160b57cec5SDimitry Andric // the instruction and also returns the corresponding type of that item
68170b57cec5SDimitry Andric // indirectly through ReferenceType.
68180b57cec5SDimitry Andric //
68190b57cec5SDimitry Andric // If ReferenceValue is an address of literal cstring then a pointer to the
68200b57cec5SDimitry Andric // cstring is returned and ReferenceType is set to
68210b57cec5SDimitry Andric // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
68220b57cec5SDimitry Andric //
68230b57cec5SDimitry Andric // If ReferenceValue is an address of an Objective-C CFString, Selector ref or
68240b57cec5SDimitry Andric // Class ref that name is returned and the ReferenceType is set accordingly.
68250b57cec5SDimitry Andric //
68260b57cec5SDimitry Andric // Lastly, literals which are Symbol address in a literal pool are looked for
68270b57cec5SDimitry Andric // and if found the symbol name is returned and ReferenceType is set to
68280b57cec5SDimitry Andric // LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
68290b57cec5SDimitry Andric //
68300b57cec5SDimitry Andric // If there is no item in the Mach-O file for the address passed in as
68310b57cec5SDimitry Andric // ReferenceValue nullptr is returned and ReferenceType is unchanged.
GuessLiteralPointer(uint64_t ReferenceValue,uint64_t ReferencePC,uint64_t * ReferenceType,struct DisassembleInfo * info)68320b57cec5SDimitry Andric static const char *GuessLiteralPointer(uint64_t ReferenceValue,
68330b57cec5SDimitry Andric                                        uint64_t ReferencePC,
68340b57cec5SDimitry Andric                                        uint64_t *ReferenceType,
68350b57cec5SDimitry Andric                                        struct DisassembleInfo *info) {
68360b57cec5SDimitry Andric   // First see if there is an external relocation entry at the ReferencePC.
68370b57cec5SDimitry Andric   if (info->O->getHeader().filetype == MachO::MH_OBJECT) {
68380b57cec5SDimitry Andric     uint64_t sect_addr = info->S.getAddress();
68390b57cec5SDimitry Andric     uint64_t sect_offset = ReferencePC - sect_addr;
68400b57cec5SDimitry Andric     bool reloc_found = false;
68410b57cec5SDimitry Andric     DataRefImpl Rel;
68420b57cec5SDimitry Andric     MachO::any_relocation_info RE;
68430b57cec5SDimitry Andric     bool isExtern = false;
68440b57cec5SDimitry Andric     SymbolRef Symbol;
68450b57cec5SDimitry Andric     for (const RelocationRef &Reloc : info->S.relocations()) {
68460b57cec5SDimitry Andric       uint64_t RelocOffset = Reloc.getOffset();
68470b57cec5SDimitry Andric       if (RelocOffset == sect_offset) {
68480b57cec5SDimitry Andric         Rel = Reloc.getRawDataRefImpl();
68490b57cec5SDimitry Andric         RE = info->O->getRelocation(Rel);
68500b57cec5SDimitry Andric         if (info->O->isRelocationScattered(RE))
68510b57cec5SDimitry Andric           continue;
68520b57cec5SDimitry Andric         isExtern = info->O->getPlainRelocationExternal(RE);
68530b57cec5SDimitry Andric         if (isExtern) {
68540b57cec5SDimitry Andric           symbol_iterator RelocSym = Reloc.getSymbol();
68550b57cec5SDimitry Andric           Symbol = *RelocSym;
68560b57cec5SDimitry Andric         }
68570b57cec5SDimitry Andric         reloc_found = true;
68580b57cec5SDimitry Andric         break;
68590b57cec5SDimitry Andric       }
68600b57cec5SDimitry Andric     }
68610b57cec5SDimitry Andric     // If there is an external relocation entry for a symbol in a section
68620b57cec5SDimitry Andric     // then used that symbol's value for the value of the reference.
68630b57cec5SDimitry Andric     if (reloc_found && isExtern) {
68640b57cec5SDimitry Andric       if (info->O->getAnyRelocationPCRel(RE)) {
68650b57cec5SDimitry Andric         unsigned Type = info->O->getAnyRelocationType(RE);
68660b57cec5SDimitry Andric         if (Type == MachO::X86_64_RELOC_SIGNED) {
68675ffd83dbSDimitry Andric           ReferenceValue = cantFail(Symbol.getValue());
68680b57cec5SDimitry Andric         }
68690b57cec5SDimitry Andric       }
68700b57cec5SDimitry Andric     }
68710b57cec5SDimitry Andric   }
68720b57cec5SDimitry Andric 
68730b57cec5SDimitry Andric   // Look for literals such as Objective-C CFStrings refs, Selector refs,
68740b57cec5SDimitry Andric   // Message refs and Class refs.
68750b57cec5SDimitry Andric   bool classref, selref, msgref, cfstring;
68760b57cec5SDimitry Andric   uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
68770b57cec5SDimitry Andric                                                selref, msgref, cfstring);
68780b57cec5SDimitry Andric   if (classref && pointer_value == 0) {
68790b57cec5SDimitry Andric     // Note the ReferenceValue is a pointer into the __objc_classrefs section.
68800b57cec5SDimitry Andric     // And the pointer_value in that section is typically zero as it will be
68810b57cec5SDimitry Andric     // set by dyld as part of the "bind information".
68820b57cec5SDimitry Andric     const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
68830b57cec5SDimitry Andric     if (name != nullptr) {
68840b57cec5SDimitry Andric       *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
68850b57cec5SDimitry Andric       const char *class_name = strrchr(name, '$');
68860b57cec5SDimitry Andric       if (class_name != nullptr && class_name[1] == '_' &&
68870b57cec5SDimitry Andric           class_name[2] != '\0') {
68880b57cec5SDimitry Andric         info->class_name = class_name + 2;
68890b57cec5SDimitry Andric         return name;
68900b57cec5SDimitry Andric       }
68910b57cec5SDimitry Andric     }
68920b57cec5SDimitry Andric   }
68930b57cec5SDimitry Andric 
68940b57cec5SDimitry Andric   if (classref) {
68950b57cec5SDimitry Andric     *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
68960b57cec5SDimitry Andric     const char *name =
68970b57cec5SDimitry Andric         get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
68980b57cec5SDimitry Andric     if (name != nullptr)
68990b57cec5SDimitry Andric       info->class_name = name;
69000b57cec5SDimitry Andric     else
69010b57cec5SDimitry Andric       name = "bad class ref";
69020b57cec5SDimitry Andric     return name;
69030b57cec5SDimitry Andric   }
69040b57cec5SDimitry Andric 
69050b57cec5SDimitry Andric   if (cfstring) {
69060b57cec5SDimitry Andric     *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
69070b57cec5SDimitry Andric     const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
69080b57cec5SDimitry Andric     return name;
69090b57cec5SDimitry Andric   }
69100b57cec5SDimitry Andric 
69110b57cec5SDimitry Andric   if (selref && pointer_value == 0)
69120b57cec5SDimitry Andric     pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
69130b57cec5SDimitry Andric 
69140b57cec5SDimitry Andric   if (pointer_value != 0)
69150b57cec5SDimitry Andric     ReferenceValue = pointer_value;
69160b57cec5SDimitry Andric 
69170b57cec5SDimitry Andric   const char *name = GuessCstringPointer(ReferenceValue, info);
69180b57cec5SDimitry Andric   if (name) {
69190b57cec5SDimitry Andric     if (pointer_value != 0 && selref) {
69200b57cec5SDimitry Andric       *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
69210b57cec5SDimitry Andric       info->selector_name = name;
69220b57cec5SDimitry Andric     } else if (pointer_value != 0 && msgref) {
69230b57cec5SDimitry Andric       info->class_name = nullptr;
69240b57cec5SDimitry Andric       *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
69250b57cec5SDimitry Andric       info->selector_name = name;
69260b57cec5SDimitry Andric     } else
69270b57cec5SDimitry Andric       *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
69280b57cec5SDimitry Andric     return name;
69290b57cec5SDimitry Andric   }
69300b57cec5SDimitry Andric 
69310b57cec5SDimitry Andric   // Lastly look for an indirect symbol with this ReferenceValue which is in
69320b57cec5SDimitry Andric   // a literal pool.  If found return that symbol name.
69330b57cec5SDimitry Andric   name = GuessIndirectSymbol(ReferenceValue, info);
69340b57cec5SDimitry Andric   if (name) {
69350b57cec5SDimitry Andric     *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
69360b57cec5SDimitry Andric     return name;
69370b57cec5SDimitry Andric   }
69380b57cec5SDimitry Andric 
69390b57cec5SDimitry Andric   return nullptr;
69400b57cec5SDimitry Andric }
69410b57cec5SDimitry Andric 
69420b57cec5SDimitry Andric // SymbolizerSymbolLookUp is the symbol lookup function passed when creating
69430b57cec5SDimitry Andric // the Symbolizer.  It looks up the ReferenceValue using the info passed via the
69440b57cec5SDimitry Andric // pointer to the struct DisassembleInfo that was passed when MCSymbolizer
69450b57cec5SDimitry Andric // is created and returns the symbol name that matches the ReferenceValue or
69460b57cec5SDimitry Andric // nullptr if none.  The ReferenceType is passed in for the IN type of
69470b57cec5SDimitry Andric // reference the instruction is making from the values in defined in the header
69480b57cec5SDimitry Andric // "llvm-c/Disassembler.h".  On return the ReferenceType can set to a specific
69490b57cec5SDimitry Andric // Out type and the ReferenceName will also be set which is added as a comment
69500b57cec5SDimitry Andric // to the disassembled instruction.
69510b57cec5SDimitry Andric //
69520b57cec5SDimitry Andric // If the symbol name is a C++ mangled name then the demangled name is
69530b57cec5SDimitry Andric // returned through ReferenceName and ReferenceType is set to
69540b57cec5SDimitry Andric // LLVMDisassembler_ReferenceType_DeMangled_Name .
69550b57cec5SDimitry Andric //
69560b57cec5SDimitry Andric // When this is called to get a symbol name for a branch target then the
69570b57cec5SDimitry Andric // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
69580b57cec5SDimitry Andric // SymbolValue will be looked for in the indirect symbol table to determine if
69590b57cec5SDimitry Andric // it is an address for a symbol stub.  If so then the symbol name for that
69600b57cec5SDimitry Andric // stub is returned indirectly through ReferenceName and then ReferenceType is
69610b57cec5SDimitry Andric // set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
69620b57cec5SDimitry Andric //
69630b57cec5SDimitry Andric // When this is called with an value loaded via a PC relative load then
69640b57cec5SDimitry Andric // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
69650b57cec5SDimitry Andric // SymbolValue is checked to be an address of literal pointer, symbol pointer,
69660b57cec5SDimitry Andric // or an Objective-C meta data reference.  If so the output ReferenceType is
69670b57cec5SDimitry Andric // set to correspond to that as well as setting the ReferenceName.
SymbolizerSymbolLookUp(void * DisInfo,uint64_t ReferenceValue,uint64_t * ReferenceType,uint64_t ReferencePC,const char ** ReferenceName)69680b57cec5SDimitry Andric static const char *SymbolizerSymbolLookUp(void *DisInfo,
69690b57cec5SDimitry Andric                                           uint64_t ReferenceValue,
69700b57cec5SDimitry Andric                                           uint64_t *ReferenceType,
69710b57cec5SDimitry Andric                                           uint64_t ReferencePC,
69720b57cec5SDimitry Andric                                           const char **ReferenceName) {
69730b57cec5SDimitry Andric   struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
69740b57cec5SDimitry Andric   // If no verbose symbolic information is wanted then just return nullptr.
69750b57cec5SDimitry Andric   if (!info->verbose) {
69760b57cec5SDimitry Andric     *ReferenceName = nullptr;
69770b57cec5SDimitry Andric     *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
69780b57cec5SDimitry Andric     return nullptr;
69790b57cec5SDimitry Andric   }
69800b57cec5SDimitry Andric 
69810b57cec5SDimitry Andric   const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
69820b57cec5SDimitry Andric 
69830b57cec5SDimitry Andric   if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
69840b57cec5SDimitry Andric     *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
69850b57cec5SDimitry Andric     if (*ReferenceName != nullptr) {
69860b57cec5SDimitry Andric       method_reference(info, ReferenceType, ReferenceName);
69870b57cec5SDimitry Andric       if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
69880b57cec5SDimitry Andric         *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
69890b57cec5SDimitry Andric     } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
69900b57cec5SDimitry Andric       if (info->demangled_name != nullptr)
69910b57cec5SDimitry Andric         free(info->demangled_name);
699206c3fb27SDimitry Andric       info->demangled_name = itaniumDemangle(SymbolName + 1);
69930b57cec5SDimitry Andric       if (info->demangled_name != nullptr) {
69940b57cec5SDimitry Andric         *ReferenceName = info->demangled_name;
69950b57cec5SDimitry Andric         *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
69960b57cec5SDimitry Andric       } else
69970b57cec5SDimitry Andric         *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
69980b57cec5SDimitry Andric     } else
69990b57cec5SDimitry Andric       *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
70000b57cec5SDimitry Andric   } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
70010b57cec5SDimitry Andric     *ReferenceName =
70020b57cec5SDimitry Andric         GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
70030b57cec5SDimitry Andric     if (*ReferenceName)
70040b57cec5SDimitry Andric       method_reference(info, ReferenceType, ReferenceName);
70050b57cec5SDimitry Andric     else
70060b57cec5SDimitry Andric       *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
70070b57cec5SDimitry Andric     // If this is arm64 and the reference is an adrp instruction save the
70080b57cec5SDimitry Andric     // instruction, passed in ReferenceValue and the address of the instruction
70090b57cec5SDimitry Andric     // for use later if we see and add immediate instruction.
70100b57cec5SDimitry Andric   } else if (info->O->getArch() == Triple::aarch64 &&
70110b57cec5SDimitry Andric              *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
70120b57cec5SDimitry Andric     info->adrp_inst = ReferenceValue;
70130b57cec5SDimitry Andric     info->adrp_addr = ReferencePC;
70140b57cec5SDimitry Andric     SymbolName = nullptr;
70150b57cec5SDimitry Andric     *ReferenceName = nullptr;
70160b57cec5SDimitry Andric     *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
70170b57cec5SDimitry Andric     // If this is arm64 and reference is an add immediate instruction and we
70180b57cec5SDimitry Andric     // have
70190b57cec5SDimitry Andric     // seen an adrp instruction just before it and the adrp's Xd register
70200b57cec5SDimitry Andric     // matches
70210b57cec5SDimitry Andric     // this add's Xn register reconstruct the value being referenced and look to
70220b57cec5SDimitry Andric     // see if it is a literal pointer.  Note the add immediate instruction is
70230b57cec5SDimitry Andric     // passed in ReferenceValue.
70240b57cec5SDimitry Andric   } else if (info->O->getArch() == Triple::aarch64 &&
70250b57cec5SDimitry Andric              *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
70260b57cec5SDimitry Andric              ReferencePC - 4 == info->adrp_addr &&
70270b57cec5SDimitry Andric              (info->adrp_inst & 0x9f000000) == 0x90000000 &&
70280b57cec5SDimitry Andric              (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
70290b57cec5SDimitry Andric     uint32_t addxri_inst;
70300b57cec5SDimitry Andric     uint64_t adrp_imm, addxri_imm;
70310b57cec5SDimitry Andric 
70320b57cec5SDimitry Andric     adrp_imm =
70330b57cec5SDimitry Andric         ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
70340b57cec5SDimitry Andric     if (info->adrp_inst & 0x0200000)
70350b57cec5SDimitry Andric       adrp_imm |= 0xfffffffffc000000LL;
70360b57cec5SDimitry Andric 
70370b57cec5SDimitry Andric     addxri_inst = ReferenceValue;
70380b57cec5SDimitry Andric     addxri_imm = (addxri_inst >> 10) & 0xfff;
70390b57cec5SDimitry Andric     if (((addxri_inst >> 22) & 0x3) == 1)
70400b57cec5SDimitry Andric       addxri_imm <<= 12;
70410b57cec5SDimitry Andric 
70420b57cec5SDimitry Andric     ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
70430b57cec5SDimitry Andric                      (adrp_imm << 12) + addxri_imm;
70440b57cec5SDimitry Andric 
70450b57cec5SDimitry Andric     *ReferenceName =
70460b57cec5SDimitry Andric         GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
70470b57cec5SDimitry Andric     if (*ReferenceName == nullptr)
70480b57cec5SDimitry Andric       *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
70490b57cec5SDimitry Andric     // If this is arm64 and the reference is a load register instruction and we
70500b57cec5SDimitry Andric     // have seen an adrp instruction just before it and the adrp's Xd register
70510b57cec5SDimitry Andric     // matches this add's Xn register reconstruct the value being referenced and
70520b57cec5SDimitry Andric     // look to see if it is a literal pointer.  Note the load register
70530b57cec5SDimitry Andric     // instruction is passed in ReferenceValue.
70540b57cec5SDimitry Andric   } else if (info->O->getArch() == Triple::aarch64 &&
70550b57cec5SDimitry Andric              *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
70560b57cec5SDimitry Andric              ReferencePC - 4 == info->adrp_addr &&
70570b57cec5SDimitry Andric              (info->adrp_inst & 0x9f000000) == 0x90000000 &&
70580b57cec5SDimitry Andric              (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
70590b57cec5SDimitry Andric     uint32_t ldrxui_inst;
70600b57cec5SDimitry Andric     uint64_t adrp_imm, ldrxui_imm;
70610b57cec5SDimitry Andric 
70620b57cec5SDimitry Andric     adrp_imm =
70630b57cec5SDimitry Andric         ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
70640b57cec5SDimitry Andric     if (info->adrp_inst & 0x0200000)
70650b57cec5SDimitry Andric       adrp_imm |= 0xfffffffffc000000LL;
70660b57cec5SDimitry Andric 
70670b57cec5SDimitry Andric     ldrxui_inst = ReferenceValue;
70680b57cec5SDimitry Andric     ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
70690b57cec5SDimitry Andric 
70700b57cec5SDimitry Andric     ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
70710b57cec5SDimitry Andric                      (adrp_imm << 12) + (ldrxui_imm << 3);
70720b57cec5SDimitry Andric 
70730b57cec5SDimitry Andric     *ReferenceName =
70740b57cec5SDimitry Andric         GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
70750b57cec5SDimitry Andric     if (*ReferenceName == nullptr)
70760b57cec5SDimitry Andric       *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
70770b57cec5SDimitry Andric   }
70780b57cec5SDimitry Andric   // If this arm64 and is an load register (PC-relative) instruction the
70790b57cec5SDimitry Andric   // ReferenceValue is the PC plus the immediate value.
70800b57cec5SDimitry Andric   else if (info->O->getArch() == Triple::aarch64 &&
70810b57cec5SDimitry Andric            (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
70820b57cec5SDimitry Andric             *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
70830b57cec5SDimitry Andric     *ReferenceName =
70840b57cec5SDimitry Andric         GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
70850b57cec5SDimitry Andric     if (*ReferenceName == nullptr)
70860b57cec5SDimitry Andric       *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
70870b57cec5SDimitry Andric   } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
70880b57cec5SDimitry Andric     if (info->demangled_name != nullptr)
70890b57cec5SDimitry Andric       free(info->demangled_name);
709006c3fb27SDimitry Andric     info->demangled_name = itaniumDemangle(SymbolName + 1);
70910b57cec5SDimitry Andric     if (info->demangled_name != nullptr) {
70920b57cec5SDimitry Andric       *ReferenceName = info->demangled_name;
70930b57cec5SDimitry Andric       *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
70940b57cec5SDimitry Andric     }
70950b57cec5SDimitry Andric   }
70960b57cec5SDimitry Andric   else {
70970b57cec5SDimitry Andric     *ReferenceName = nullptr;
70980b57cec5SDimitry Andric     *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
70990b57cec5SDimitry Andric   }
71000b57cec5SDimitry Andric 
71010b57cec5SDimitry Andric   return SymbolName;
71020b57cec5SDimitry Andric }
71030b57cec5SDimitry Andric 
71040b57cec5SDimitry Andric /// Emits the comments that are stored in the CommentStream.
71050b57cec5SDimitry Andric /// Each comment in the CommentStream must end with a newline.
emitComments(raw_svector_ostream & CommentStream,SmallString<128> & CommentsToEmit,formatted_raw_ostream & FormattedOS,const MCAsmInfo & MAI)71060b57cec5SDimitry Andric static void emitComments(raw_svector_ostream &CommentStream,
71070b57cec5SDimitry Andric                          SmallString<128> &CommentsToEmit,
71080b57cec5SDimitry Andric                          formatted_raw_ostream &FormattedOS,
71090b57cec5SDimitry Andric                          const MCAsmInfo &MAI) {
71100b57cec5SDimitry Andric   // Flush the stream before taking its content.
71110b57cec5SDimitry Andric   StringRef Comments = CommentsToEmit.str();
71120b57cec5SDimitry Andric   // Get the default information for printing a comment.
71130b57cec5SDimitry Andric   StringRef CommentBegin = MAI.getCommentString();
71140b57cec5SDimitry Andric   unsigned CommentColumn = MAI.getCommentColumn();
7115fe6060f1SDimitry Andric   ListSeparator LS("\n");
71160b57cec5SDimitry Andric   while (!Comments.empty()) {
7117fe6060f1SDimitry Andric     FormattedOS << LS;
71180b57cec5SDimitry Andric     // Emit a line of comments.
71190b57cec5SDimitry Andric     FormattedOS.PadToColumn(CommentColumn);
71200b57cec5SDimitry Andric     size_t Position = Comments.find('\n');
71210b57cec5SDimitry Andric     FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
71220b57cec5SDimitry Andric     // Move after the newline character.
71230b57cec5SDimitry Andric     Comments = Comments.substr(Position + 1);
71240b57cec5SDimitry Andric   }
71250b57cec5SDimitry Andric   FormattedOS.flush();
71260b57cec5SDimitry Andric 
71270b57cec5SDimitry Andric   // Tell the comment stream that the vector changed underneath it.
71280b57cec5SDimitry Andric   CommentsToEmit.clear();
71290b57cec5SDimitry Andric }
71300b57cec5SDimitry Andric 
7131bdd1243dSDimitry Andric const MachOObjectFile *
getMachODSymObject(const MachOObjectFile * MachOOF,StringRef Filename,std::unique_ptr<Binary> & DSYMBinary,std::unique_ptr<MemoryBuffer> & DSYMBuf)7132bdd1243dSDimitry Andric objdump::getMachODSymObject(const MachOObjectFile *MachOOF, StringRef Filename,
7133bdd1243dSDimitry Andric                             std::unique_ptr<Binary> &DSYMBinary,
7134bdd1243dSDimitry Andric                             std::unique_ptr<MemoryBuffer> &DSYMBuf) {
7135bdd1243dSDimitry Andric   const MachOObjectFile *DbgObj = MachOOF;
7136bdd1243dSDimitry Andric   std::string DSYMPath;
7137bdd1243dSDimitry Andric 
7138bdd1243dSDimitry Andric   // Auto-detect w/o --dsym.
7139bdd1243dSDimitry Andric   if (DSYMFile.empty()) {
7140bdd1243dSDimitry Andric     sys::fs::file_status DSYMStatus;
7141bdd1243dSDimitry Andric     Twine FilenameDSYM = Filename + ".dSYM";
7142bdd1243dSDimitry Andric     if (!status(FilenameDSYM, DSYMStatus)) {
7143bdd1243dSDimitry Andric       if (sys::fs::is_directory(DSYMStatus)) {
7144bdd1243dSDimitry Andric         SmallString<1024> Path;
7145bdd1243dSDimitry Andric         FilenameDSYM.toVector(Path);
7146bdd1243dSDimitry Andric         sys::path::append(Path, "Contents", "Resources", "DWARF",
7147bdd1243dSDimitry Andric                           sys::path::filename(Filename));
7148bdd1243dSDimitry Andric         DSYMPath = std::string(Path);
7149bdd1243dSDimitry Andric       } else if (sys::fs::is_regular_file(DSYMStatus)) {
7150bdd1243dSDimitry Andric         DSYMPath = FilenameDSYM.str();
7151bdd1243dSDimitry Andric       }
7152bdd1243dSDimitry Andric     }
7153bdd1243dSDimitry Andric   }
7154bdd1243dSDimitry Andric 
7155bdd1243dSDimitry Andric   if (DSYMPath.empty() && !DSYMFile.empty()) {
7156bdd1243dSDimitry Andric     // If DSYMPath is a .dSYM directory, append the Mach-O file.
7157bdd1243dSDimitry Andric     if (sys::fs::is_directory(DSYMFile) &&
7158bdd1243dSDimitry Andric         sys::path::extension(DSYMFile) == ".dSYM") {
7159bdd1243dSDimitry Andric       SmallString<128> ShortName(sys::path::filename(DSYMFile));
7160bdd1243dSDimitry Andric       sys::path::replace_extension(ShortName, "");
7161bdd1243dSDimitry Andric       SmallString<1024> FullPath(DSYMFile);
7162bdd1243dSDimitry Andric       sys::path::append(FullPath, "Contents", "Resources", "DWARF", ShortName);
7163bdd1243dSDimitry Andric       DSYMPath = FullPath.str();
7164bdd1243dSDimitry Andric     } else {
7165bdd1243dSDimitry Andric       DSYMPath = DSYMFile;
7166bdd1243dSDimitry Andric     }
7167bdd1243dSDimitry Andric   }
7168bdd1243dSDimitry Andric 
7169bdd1243dSDimitry Andric   if (!DSYMPath.empty()) {
7170bdd1243dSDimitry Andric     // Load the file.
7171bdd1243dSDimitry Andric     ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
7172bdd1243dSDimitry Andric         MemoryBuffer::getFileOrSTDIN(DSYMPath);
7173bdd1243dSDimitry Andric     if (std::error_code EC = BufOrErr.getError()) {
7174bdd1243dSDimitry Andric       reportError(errorCodeToError(EC), DSYMPath);
7175bdd1243dSDimitry Andric       return nullptr;
7176bdd1243dSDimitry Andric     }
7177bdd1243dSDimitry Andric 
7178bdd1243dSDimitry Andric     // We need to keep the file alive, because we're replacing DbgObj with it.
7179bdd1243dSDimitry Andric     DSYMBuf = std::move(BufOrErr.get());
7180bdd1243dSDimitry Andric 
7181bdd1243dSDimitry Andric     Expected<std::unique_ptr<Binary>> BinaryOrErr =
7182*0fca6ea1SDimitry Andric         createBinary(DSYMBuf->getMemBufferRef());
7183bdd1243dSDimitry Andric     if (!BinaryOrErr) {
7184bdd1243dSDimitry Andric       reportError(BinaryOrErr.takeError(), DSYMPath);
7185bdd1243dSDimitry Andric       return nullptr;
7186bdd1243dSDimitry Andric     }
7187bdd1243dSDimitry Andric 
7188bdd1243dSDimitry Andric     // We need to keep the Binary alive with the buffer
7189bdd1243dSDimitry Andric     DSYMBinary = std::move(BinaryOrErr.get());
7190bdd1243dSDimitry Andric     if (ObjectFile *O = dyn_cast<ObjectFile>(DSYMBinary.get())) {
7191bdd1243dSDimitry Andric       // this is a Mach-O object file, use it
7192bdd1243dSDimitry Andric       if (MachOObjectFile *MachDSYM = dyn_cast<MachOObjectFile>(&*O)) {
7193bdd1243dSDimitry Andric         DbgObj = MachDSYM;
7194bdd1243dSDimitry Andric       } else {
7195bdd1243dSDimitry Andric         WithColor::error(errs(), "llvm-objdump")
7196bdd1243dSDimitry Andric             << DSYMPath << " is not a Mach-O file type.\n";
7197bdd1243dSDimitry Andric         return nullptr;
7198bdd1243dSDimitry Andric       }
7199bdd1243dSDimitry Andric     } else if (auto *UB = dyn_cast<MachOUniversalBinary>(DSYMBinary.get())) {
7200bdd1243dSDimitry Andric       // this is a Universal Binary, find a Mach-O for this architecture
7201bdd1243dSDimitry Andric       uint32_t CPUType, CPUSubType;
7202bdd1243dSDimitry Andric       const char *ArchFlag;
7203bdd1243dSDimitry Andric       if (MachOOF->is64Bit()) {
7204bdd1243dSDimitry Andric         const MachO::mach_header_64 H_64 = MachOOF->getHeader64();
7205bdd1243dSDimitry Andric         CPUType = H_64.cputype;
7206bdd1243dSDimitry Andric         CPUSubType = H_64.cpusubtype;
7207bdd1243dSDimitry Andric       } else {
7208bdd1243dSDimitry Andric         const MachO::mach_header H = MachOOF->getHeader();
7209bdd1243dSDimitry Andric         CPUType = H.cputype;
7210bdd1243dSDimitry Andric         CPUSubType = H.cpusubtype;
7211bdd1243dSDimitry Andric       }
7212bdd1243dSDimitry Andric       Triple T = MachOObjectFile::getArchTriple(CPUType, CPUSubType, nullptr,
7213bdd1243dSDimitry Andric                                                 &ArchFlag);
7214bdd1243dSDimitry Andric       Expected<std::unique_ptr<MachOObjectFile>> MachDSYM =
7215bdd1243dSDimitry Andric           UB->getMachOObjectForArch(ArchFlag);
7216bdd1243dSDimitry Andric       if (!MachDSYM) {
7217bdd1243dSDimitry Andric         reportError(MachDSYM.takeError(), DSYMPath);
7218bdd1243dSDimitry Andric         return nullptr;
7219bdd1243dSDimitry Andric       }
7220bdd1243dSDimitry Andric 
7221bdd1243dSDimitry Andric       // We need to keep the Binary alive with the buffer
7222bdd1243dSDimitry Andric       DbgObj = &*MachDSYM.get();
7223bdd1243dSDimitry Andric       DSYMBinary = std::move(*MachDSYM);
7224bdd1243dSDimitry Andric     } else {
7225bdd1243dSDimitry Andric       WithColor::error(errs(), "llvm-objdump")
7226bdd1243dSDimitry Andric           << DSYMPath << " is not a Mach-O or Universal file type.\n";
7227bdd1243dSDimitry Andric       return nullptr;
7228bdd1243dSDimitry Andric     }
7229bdd1243dSDimitry Andric   }
7230bdd1243dSDimitry Andric   return DbgObj;
7231bdd1243dSDimitry Andric }
7232bdd1243dSDimitry Andric 
DisassembleMachO(StringRef Filename,MachOObjectFile * MachOOF,StringRef DisSegName,StringRef DisSectName)72330b57cec5SDimitry Andric static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
72340b57cec5SDimitry Andric                              StringRef DisSegName, StringRef DisSectName) {
72350b57cec5SDimitry Andric   const char *McpuDefault = nullptr;
72360b57cec5SDimitry Andric   const Target *ThumbTarget = nullptr;
72370b57cec5SDimitry Andric   const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
72380b57cec5SDimitry Andric   if (!TheTarget) {
72390b57cec5SDimitry Andric     // GetTarget prints out stuff.
72400b57cec5SDimitry Andric     return;
72410b57cec5SDimitry Andric   }
72420b57cec5SDimitry Andric   std::string MachOMCPU;
72430b57cec5SDimitry Andric   if (MCPU.empty() && McpuDefault)
72440b57cec5SDimitry Andric     MachOMCPU = McpuDefault;
72450b57cec5SDimitry Andric   else
72460b57cec5SDimitry Andric     MachOMCPU = MCPU;
72470b57cec5SDimitry Andric 
7248e8d8bef9SDimitry Andric #define CHECK_TARGET_INFO_CREATION(NAME)                                       \
7249e8d8bef9SDimitry Andric   do {                                                                         \
7250e8d8bef9SDimitry Andric     if (!NAME) {                                                               \
7251e8d8bef9SDimitry Andric       WithColor::error(errs(), "llvm-objdump")                                 \
7252e8d8bef9SDimitry Andric           << "couldn't initialize disassembler for target " << TripleName      \
7253e8d8bef9SDimitry Andric           << '\n';                                                             \
7254e8d8bef9SDimitry Andric       return;                                                                  \
7255e8d8bef9SDimitry Andric     }                                                                          \
7256e8d8bef9SDimitry Andric   } while (false)
7257e8d8bef9SDimitry Andric #define CHECK_THUMB_TARGET_INFO_CREATION(NAME)                                 \
7258e8d8bef9SDimitry Andric   do {                                                                         \
7259e8d8bef9SDimitry Andric     if (!NAME) {                                                               \
7260e8d8bef9SDimitry Andric       WithColor::error(errs(), "llvm-objdump")                                 \
7261e8d8bef9SDimitry Andric           << "couldn't initialize disassembler for target " << ThumbTripleName \
7262e8d8bef9SDimitry Andric           << '\n';                                                             \
7263e8d8bef9SDimitry Andric       return;                                                                  \
7264e8d8bef9SDimitry Andric     }                                                                          \
7265e8d8bef9SDimitry Andric   } while (false)
7266e8d8bef9SDimitry Andric 
72670b57cec5SDimitry Andric   std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
7268e8d8bef9SDimitry Andric   CHECK_TARGET_INFO_CREATION(InstrInfo);
72690b57cec5SDimitry Andric   std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
7270e8d8bef9SDimitry Andric   if (ThumbTarget) {
72710b57cec5SDimitry Andric     ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
7272e8d8bef9SDimitry Andric     CHECK_THUMB_TARGET_INFO_CREATION(ThumbInstrInfo);
7273e8d8bef9SDimitry Andric   }
72740b57cec5SDimitry Andric 
72750b57cec5SDimitry Andric   // Package up features to be passed to target/subtarget
72760b57cec5SDimitry Andric   std::string FeaturesStr;
72770b57cec5SDimitry Andric   if (!MAttrs.empty()) {
72780b57cec5SDimitry Andric     SubtargetFeatures Features;
72790b57cec5SDimitry Andric     for (unsigned i = 0; i != MAttrs.size(); ++i)
72800b57cec5SDimitry Andric       Features.AddFeature(MAttrs[i]);
72810b57cec5SDimitry Andric     FeaturesStr = Features.getString();
72820b57cec5SDimitry Andric   }
72830b57cec5SDimitry Andric 
7284480093f4SDimitry Andric   MCTargetOptions MCOptions;
72850b57cec5SDimitry Andric   // Set up disassembler.
72860b57cec5SDimitry Andric   std::unique_ptr<const MCRegisterInfo> MRI(
72870b57cec5SDimitry Andric       TheTarget->createMCRegInfo(TripleName));
7288e8d8bef9SDimitry Andric   CHECK_TARGET_INFO_CREATION(MRI);
72890b57cec5SDimitry Andric   std::unique_ptr<const MCAsmInfo> AsmInfo(
7290480093f4SDimitry Andric       TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
7291e8d8bef9SDimitry Andric   CHECK_TARGET_INFO_CREATION(AsmInfo);
72920b57cec5SDimitry Andric   std::unique_ptr<const MCSubtargetInfo> STI(
72930b57cec5SDimitry Andric       TheTarget->createMCSubtargetInfo(TripleName, MachOMCPU, FeaturesStr));
7294e8d8bef9SDimitry Andric   CHECK_TARGET_INFO_CREATION(STI);
7295fe6060f1SDimitry Andric   MCContext Ctx(Triple(TripleName), AsmInfo.get(), MRI.get(), STI.get());
72960b57cec5SDimitry Andric   std::unique_ptr<MCDisassembler> DisAsm(
72970b57cec5SDimitry Andric       TheTarget->createMCDisassembler(*STI, Ctx));
7298e8d8bef9SDimitry Andric   CHECK_TARGET_INFO_CREATION(DisAsm);
72990b57cec5SDimitry Andric   std::unique_ptr<MCSymbolizer> Symbolizer;
73000b57cec5SDimitry Andric   struct DisassembleInfo SymbolizerInfo(nullptr, nullptr, nullptr, false);
73010b57cec5SDimitry Andric   std::unique_ptr<MCRelocationInfo> RelInfo(
73020b57cec5SDimitry Andric       TheTarget->createMCRelocationInfo(TripleName, Ctx));
73030b57cec5SDimitry Andric   if (RelInfo) {
73040b57cec5SDimitry Andric     Symbolizer.reset(TheTarget->createMCSymbolizer(
73050b57cec5SDimitry Andric         TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
73060b57cec5SDimitry Andric         &SymbolizerInfo, &Ctx, std::move(RelInfo)));
73070b57cec5SDimitry Andric     DisAsm->setSymbolizer(std::move(Symbolizer));
73080b57cec5SDimitry Andric   }
73090b57cec5SDimitry Andric   int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
73100b57cec5SDimitry Andric   std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
73110b57cec5SDimitry Andric       Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
7312e8d8bef9SDimitry Andric   CHECK_TARGET_INFO_CREATION(IP);
73130b57cec5SDimitry Andric   // Set the display preference for hex vs. decimal immediates.
73140b57cec5SDimitry Andric   IP->setPrintImmHex(PrintImmHex);
73150b57cec5SDimitry Andric   // Comment stream and backing vector.
73160b57cec5SDimitry Andric   SmallString<128> CommentsToEmit;
73170b57cec5SDimitry Andric   raw_svector_ostream CommentStream(CommentsToEmit);
73180b57cec5SDimitry Andric   // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
73190b57cec5SDimitry Andric   // if it is done then arm64 comments for string literals don't get printed
73200b57cec5SDimitry Andric   // and some constant get printed instead and not setting it causes intel
73210b57cec5SDimitry Andric   // (32-bit and 64-bit) comments printed with different spacing before the
73220b57cec5SDimitry Andric   // comment causing different diffs with the 'C' disassembler library API.
73230b57cec5SDimitry Andric   // IP->setCommentStream(CommentStream);
73240b57cec5SDimitry Andric 
73250b57cec5SDimitry Andric   // Set up separate thumb disassembler if needed.
73260b57cec5SDimitry Andric   std::unique_ptr<const MCRegisterInfo> ThumbMRI;
73270b57cec5SDimitry Andric   std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
73280b57cec5SDimitry Andric   std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
73290b57cec5SDimitry Andric   std::unique_ptr<MCDisassembler> ThumbDisAsm;
73300b57cec5SDimitry Andric   std::unique_ptr<MCInstPrinter> ThumbIP;
73310b57cec5SDimitry Andric   std::unique_ptr<MCContext> ThumbCtx;
73320b57cec5SDimitry Andric   std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
73330b57cec5SDimitry Andric   struct DisassembleInfo ThumbSymbolizerInfo(nullptr, nullptr, nullptr, false);
73340b57cec5SDimitry Andric   std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
73350b57cec5SDimitry Andric   if (ThumbTarget) {
73360b57cec5SDimitry Andric     ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
7337e8d8bef9SDimitry Andric     CHECK_THUMB_TARGET_INFO_CREATION(ThumbMRI);
73380b57cec5SDimitry Andric     ThumbAsmInfo.reset(
7339480093f4SDimitry Andric         ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName, MCOptions));
7340e8d8bef9SDimitry Andric     CHECK_THUMB_TARGET_INFO_CREATION(ThumbAsmInfo);
73410b57cec5SDimitry Andric     ThumbSTI.reset(
73420b57cec5SDimitry Andric         ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MachOMCPU,
73430b57cec5SDimitry Andric                                            FeaturesStr));
7344e8d8bef9SDimitry Andric     CHECK_THUMB_TARGET_INFO_CREATION(ThumbSTI);
7345fe6060f1SDimitry Andric     ThumbCtx.reset(new MCContext(Triple(ThumbTripleName), ThumbAsmInfo.get(),
7346fe6060f1SDimitry Andric                                  ThumbMRI.get(), ThumbSTI.get()));
73470b57cec5SDimitry Andric     ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
7348e8d8bef9SDimitry Andric     CHECK_THUMB_TARGET_INFO_CREATION(ThumbDisAsm);
73490b57cec5SDimitry Andric     MCContext *PtrThumbCtx = ThumbCtx.get();
73500b57cec5SDimitry Andric     ThumbRelInfo.reset(
73510b57cec5SDimitry Andric         ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
73520b57cec5SDimitry Andric     if (ThumbRelInfo) {
73530b57cec5SDimitry Andric       ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
73540b57cec5SDimitry Andric           ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
73550b57cec5SDimitry Andric           &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
73560b57cec5SDimitry Andric       ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
73570b57cec5SDimitry Andric     }
73580b57cec5SDimitry Andric     int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
73590b57cec5SDimitry Andric     ThumbIP.reset(ThumbTarget->createMCInstPrinter(
73600b57cec5SDimitry Andric         Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
73610b57cec5SDimitry Andric         *ThumbInstrInfo, *ThumbMRI));
7362e8d8bef9SDimitry Andric     CHECK_THUMB_TARGET_INFO_CREATION(ThumbIP);
73630b57cec5SDimitry Andric     // Set the display preference for hex vs. decimal immediates.
73640b57cec5SDimitry Andric     ThumbIP->setPrintImmHex(PrintImmHex);
73650b57cec5SDimitry Andric   }
73660b57cec5SDimitry Andric 
7367e8d8bef9SDimitry Andric #undef CHECK_TARGET_INFO_CREATION
7368e8d8bef9SDimitry Andric #undef CHECK_THUMB_TARGET_INFO_CREATION
73690b57cec5SDimitry Andric 
73700b57cec5SDimitry Andric   MachO::mach_header Header = MachOOF->getHeader();
73710b57cec5SDimitry Andric 
73720b57cec5SDimitry Andric   // FIXME: Using the -cfg command line option, this code used to be able to
73730b57cec5SDimitry Andric   // annotate relocations with the referenced symbol's name, and if this was
73740b57cec5SDimitry Andric   // inside a __[cf]string section, the data it points to. This is now replaced
73750b57cec5SDimitry Andric   // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
73760b57cec5SDimitry Andric   std::vector<SectionRef> Sections;
73770b57cec5SDimitry Andric   std::vector<SymbolRef> Symbols;
73780b57cec5SDimitry Andric   SmallVector<uint64_t, 8> FoundFns;
73798bcb0991SDimitry Andric   uint64_t BaseSegmentAddress = 0;
73800b57cec5SDimitry Andric 
73810b57cec5SDimitry Andric   getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
73820b57cec5SDimitry Andric                         BaseSegmentAddress);
73830b57cec5SDimitry Andric 
73840b57cec5SDimitry Andric   // Sort the symbols by address, just in case they didn't come in that way.
7385fe6060f1SDimitry Andric   llvm::stable_sort(Symbols, SymbolSorter());
73860b57cec5SDimitry Andric 
73870b57cec5SDimitry Andric   // Build a data in code table that is sorted on by the address of each entry.
73880b57cec5SDimitry Andric   uint64_t BaseAddress = 0;
73890b57cec5SDimitry Andric   if (Header.filetype == MachO::MH_OBJECT)
73900b57cec5SDimitry Andric     BaseAddress = Sections[0].getAddress();
73910b57cec5SDimitry Andric   else
73920b57cec5SDimitry Andric     BaseAddress = BaseSegmentAddress;
73930b57cec5SDimitry Andric   DiceTable Dices;
73940b57cec5SDimitry Andric   for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
73950b57cec5SDimitry Andric        DI != DE; ++DI) {
73960b57cec5SDimitry Andric     uint32_t Offset;
73970b57cec5SDimitry Andric     DI->getOffset(Offset);
73980b57cec5SDimitry Andric     Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
73990b57cec5SDimitry Andric   }
74000b57cec5SDimitry Andric   array_pod_sort(Dices.begin(), Dices.end());
74010b57cec5SDimitry Andric 
74020b57cec5SDimitry Andric   // Try to find debug info and set up the DIContext for it.
74030b57cec5SDimitry Andric   std::unique_ptr<DIContext> diContext;
74040b57cec5SDimitry Andric   std::unique_ptr<Binary> DSYMBinary;
74050b57cec5SDimitry Andric   std::unique_ptr<MemoryBuffer> DSYMBuf;
74060b57cec5SDimitry Andric   if (UseDbg) {
7407bdd1243dSDimitry Andric     // If separate DSym file path was specified, parse it as a macho file,
74080b57cec5SDimitry Andric     // get the sections and supply it to the section name parsing machinery.
7409bdd1243dSDimitry Andric     if (const ObjectFile *DbgObj =
7410bdd1243dSDimitry Andric             getMachODSymObject(MachOOF, Filename, DSYMBinary, DSYMBuf)) {
74110b57cec5SDimitry Andric       // Setup the DIContext
74120b57cec5SDimitry Andric       diContext = DWARFContext::create(*DbgObj);
7413bdd1243dSDimitry Andric     } else {
7414bdd1243dSDimitry Andric       return;
7415bdd1243dSDimitry Andric     }
74160b57cec5SDimitry Andric   }
74170b57cec5SDimitry Andric 
74180b57cec5SDimitry Andric   if (FilterSections.empty())
74190b57cec5SDimitry Andric     outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
74200b57cec5SDimitry Andric 
74210b57cec5SDimitry Andric   for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
74228bcb0991SDimitry Andric     Expected<StringRef> SecNameOrErr = Sections[SectIdx].getName();
74238bcb0991SDimitry Andric     if (!SecNameOrErr) {
74248bcb0991SDimitry Andric       consumeError(SecNameOrErr.takeError());
74258bcb0991SDimitry Andric       continue;
74268bcb0991SDimitry Andric     }
74278bcb0991SDimitry Andric     if (*SecNameOrErr != DisSectName)
74280b57cec5SDimitry Andric       continue;
74290b57cec5SDimitry Andric 
74300b57cec5SDimitry Andric     DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
74310b57cec5SDimitry Andric 
74320b57cec5SDimitry Andric     StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
74330b57cec5SDimitry Andric     if (SegmentName != DisSegName)
74340b57cec5SDimitry Andric       continue;
74350b57cec5SDimitry Andric 
74360b57cec5SDimitry Andric     StringRef BytesStr =
74370b57cec5SDimitry Andric         unwrapOrError(Sections[SectIdx].getContents(), Filename);
74380b57cec5SDimitry Andric     ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(BytesStr);
74390b57cec5SDimitry Andric     uint64_t SectAddress = Sections[SectIdx].getAddress();
74400b57cec5SDimitry Andric 
74410b57cec5SDimitry Andric     bool symbolTableWorked = false;
74420b57cec5SDimitry Andric 
74430b57cec5SDimitry Andric     // Create a map of symbol addresses to symbol names for use by
74440b57cec5SDimitry Andric     // the SymbolizerSymbolLookUp() routine.
74450b57cec5SDimitry Andric     SymbolAddressMap AddrMap;
74460b57cec5SDimitry Andric     bool DisSymNameFound = false;
74470b57cec5SDimitry Andric     for (const SymbolRef &Symbol : MachOOF->symbols()) {
74480b57cec5SDimitry Andric       SymbolRef::Type ST =
74490b57cec5SDimitry Andric           unwrapOrError(Symbol.getType(), MachOOF->getFileName());
74500b57cec5SDimitry Andric       if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
74510b57cec5SDimitry Andric           ST == SymbolRef::ST_Other) {
74525ffd83dbSDimitry Andric         uint64_t Address = cantFail(Symbol.getValue());
74530b57cec5SDimitry Andric         StringRef SymName =
74540b57cec5SDimitry Andric             unwrapOrError(Symbol.getName(), MachOOF->getFileName());
74550b57cec5SDimitry Andric         AddrMap[Address] = SymName;
74560b57cec5SDimitry Andric         if (!DisSymName.empty() && DisSymName == SymName)
74570b57cec5SDimitry Andric           DisSymNameFound = true;
74580b57cec5SDimitry Andric       }
74590b57cec5SDimitry Andric     }
74600b57cec5SDimitry Andric     if (!DisSymName.empty() && !DisSymNameFound) {
74610b57cec5SDimitry Andric       outs() << "Can't find -dis-symname: " << DisSymName << "\n";
74620b57cec5SDimitry Andric       return;
74630b57cec5SDimitry Andric     }
74640b57cec5SDimitry Andric     // Set up the block of info used by the Symbolizer call backs.
7465fe6060f1SDimitry Andric     SymbolizerInfo.verbose = SymbolicOperands;
74660b57cec5SDimitry Andric     SymbolizerInfo.O = MachOOF;
74670b57cec5SDimitry Andric     SymbolizerInfo.S = Sections[SectIdx];
74680b57cec5SDimitry Andric     SymbolizerInfo.AddrMap = &AddrMap;
74690b57cec5SDimitry Andric     SymbolizerInfo.Sections = &Sections;
74700b57cec5SDimitry Andric     // Same for the ThumbSymbolizer
7471fe6060f1SDimitry Andric     ThumbSymbolizerInfo.verbose = SymbolicOperands;
74720b57cec5SDimitry Andric     ThumbSymbolizerInfo.O = MachOOF;
74730b57cec5SDimitry Andric     ThumbSymbolizerInfo.S = Sections[SectIdx];
74740b57cec5SDimitry Andric     ThumbSymbolizerInfo.AddrMap = &AddrMap;
74750b57cec5SDimitry Andric     ThumbSymbolizerInfo.Sections = &Sections;
74760b57cec5SDimitry Andric 
74770b57cec5SDimitry Andric     unsigned int Arch = MachOOF->getArch();
74780b57cec5SDimitry Andric 
74790b57cec5SDimitry Andric     // Skip all symbols if this is a stubs file.
74800b57cec5SDimitry Andric     if (Bytes.empty())
74810b57cec5SDimitry Andric       return;
74820b57cec5SDimitry Andric 
74830b57cec5SDimitry Andric     // If the section has symbols but no symbol at the start of the section
74840b57cec5SDimitry Andric     // these are used to make sure the bytes before the first symbol are
74850b57cec5SDimitry Andric     // disassembled.
74860b57cec5SDimitry Andric     bool FirstSymbol = true;
74870b57cec5SDimitry Andric     bool FirstSymbolAtSectionStart = true;
74880b57cec5SDimitry Andric 
74890b57cec5SDimitry Andric     // Disassemble symbol by symbol.
74900b57cec5SDimitry Andric     for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
74910b57cec5SDimitry Andric       StringRef SymName =
74920b57cec5SDimitry Andric           unwrapOrError(Symbols[SymIdx].getName(), MachOOF->getFileName());
74930b57cec5SDimitry Andric       SymbolRef::Type ST =
74940b57cec5SDimitry Andric           unwrapOrError(Symbols[SymIdx].getType(), MachOOF->getFileName());
74950b57cec5SDimitry Andric       if (ST != SymbolRef::ST_Function && ST != SymbolRef::ST_Data)
74960b57cec5SDimitry Andric         continue;
74970b57cec5SDimitry Andric 
74980b57cec5SDimitry Andric       // Make sure the symbol is defined in this section.
74990b57cec5SDimitry Andric       bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
75000b57cec5SDimitry Andric       if (!containsSym) {
75010b57cec5SDimitry Andric         if (!DisSymName.empty() && DisSymName == SymName) {
75020b57cec5SDimitry Andric           outs() << "-dis-symname: " << DisSymName << " not in the section\n";
75030b57cec5SDimitry Andric           return;
75040b57cec5SDimitry Andric         }
75050b57cec5SDimitry Andric         continue;
75060b57cec5SDimitry Andric       }
75070b57cec5SDimitry Andric       // The __mh_execute_header is special and we need to deal with that fact
75080b57cec5SDimitry Andric       // this symbol is before the start of the (__TEXT,__text) section and at the
75090b57cec5SDimitry Andric       // address of the start of the __TEXT segment.  This is because this symbol
75100b57cec5SDimitry Andric       // is an N_SECT symbol in the (__TEXT,__text) but its address is before the
75110b57cec5SDimitry Andric       // start of the section in a standard MH_EXECUTE filetype.
75120b57cec5SDimitry Andric       if (!DisSymName.empty() && DisSymName == "__mh_execute_header") {
75130b57cec5SDimitry Andric         outs() << "-dis-symname: __mh_execute_header not in any section\n";
75140b57cec5SDimitry Andric         return;
75150b57cec5SDimitry Andric       }
75160b57cec5SDimitry Andric       // When this code is trying to disassemble a symbol at a time and in the
75170b57cec5SDimitry Andric       // case there is only the __mh_execute_header symbol left as in a stripped
75180b57cec5SDimitry Andric       // executable, we need to deal with this by ignoring this symbol so the
75190b57cec5SDimitry Andric       // whole section is disassembled and this symbol is then not displayed.
75200b57cec5SDimitry Andric       if (SymName == "__mh_execute_header" || SymName == "__mh_dylib_header" ||
75210b57cec5SDimitry Andric           SymName == "__mh_bundle_header" || SymName == "__mh_object_header" ||
75220b57cec5SDimitry Andric           SymName == "__mh_preload_header" || SymName == "__mh_dylinker_header")
75230b57cec5SDimitry Andric         continue;
75240b57cec5SDimitry Andric 
75250b57cec5SDimitry Andric       // If we are only disassembling one symbol see if this is that symbol.
75260b57cec5SDimitry Andric       if (!DisSymName.empty() && DisSymName != SymName)
75270b57cec5SDimitry Andric         continue;
75280b57cec5SDimitry Andric 
75290b57cec5SDimitry Andric       // Start at the address of the symbol relative to the section's address.
75300b57cec5SDimitry Andric       uint64_t SectSize = Sections[SectIdx].getSize();
75315ffd83dbSDimitry Andric       uint64_t Start = cantFail(Symbols[SymIdx].getValue());
75320b57cec5SDimitry Andric       uint64_t SectionAddress = Sections[SectIdx].getAddress();
75330b57cec5SDimitry Andric       Start -= SectionAddress;
75340b57cec5SDimitry Andric 
75350b57cec5SDimitry Andric       if (Start > SectSize) {
75360b57cec5SDimitry Andric         outs() << "section data ends, " << SymName
75370b57cec5SDimitry Andric                << " lies outside valid range\n";
75380b57cec5SDimitry Andric         return;
75390b57cec5SDimitry Andric       }
75400b57cec5SDimitry Andric 
75410b57cec5SDimitry Andric       // Stop disassembling either at the beginning of the next symbol or at
75420b57cec5SDimitry Andric       // the end of the section.
75430b57cec5SDimitry Andric       bool containsNextSym = false;
75440b57cec5SDimitry Andric       uint64_t NextSym = 0;
75450b57cec5SDimitry Andric       uint64_t NextSymIdx = SymIdx + 1;
75460b57cec5SDimitry Andric       while (Symbols.size() > NextSymIdx) {
75470b57cec5SDimitry Andric         SymbolRef::Type NextSymType = unwrapOrError(
75480b57cec5SDimitry Andric             Symbols[NextSymIdx].getType(), MachOOF->getFileName());
75490b57cec5SDimitry Andric         if (NextSymType == SymbolRef::ST_Function) {
75500b57cec5SDimitry Andric           containsNextSym =
75510b57cec5SDimitry Andric               Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
75525ffd83dbSDimitry Andric           NextSym = cantFail(Symbols[NextSymIdx].getValue());
75530b57cec5SDimitry Andric           NextSym -= SectionAddress;
75540b57cec5SDimitry Andric           break;
75550b57cec5SDimitry Andric         }
75560b57cec5SDimitry Andric         ++NextSymIdx;
75570b57cec5SDimitry Andric       }
75580b57cec5SDimitry Andric 
75590b57cec5SDimitry Andric       uint64_t End = containsNextSym ? std::min(NextSym, SectSize) : SectSize;
75600b57cec5SDimitry Andric       uint64_t Size;
75610b57cec5SDimitry Andric 
75620b57cec5SDimitry Andric       symbolTableWorked = true;
75630b57cec5SDimitry Andric 
75640b57cec5SDimitry Andric       DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
75655ffd83dbSDimitry Andric       uint32_t SymbolFlags = cantFail(MachOOF->getSymbolFlags(Symb));
75665ffd83dbSDimitry Andric       bool IsThumb = SymbolFlags & SymbolRef::SF_Thumb;
75670b57cec5SDimitry Andric 
75680b57cec5SDimitry Andric       // We only need the dedicated Thumb target if there's a real choice
75690b57cec5SDimitry Andric       // (i.e. we're not targeting M-class) and the function is Thumb.
75700b57cec5SDimitry Andric       bool UseThumbTarget = IsThumb && ThumbTarget;
75710b57cec5SDimitry Andric 
75720b57cec5SDimitry Andric       // If we are not specifying a symbol to start disassembly with and this
75730b57cec5SDimitry Andric       // is the first symbol in the section but not at the start of the section
75740b57cec5SDimitry Andric       // then move the disassembly index to the start of the section and
75750b57cec5SDimitry Andric       // don't print the symbol name just yet.  This is so the bytes before the
75760b57cec5SDimitry Andric       // first symbol are disassembled.
75770b57cec5SDimitry Andric       uint64_t SymbolStart = Start;
75780b57cec5SDimitry Andric       if (DisSymName.empty() && FirstSymbol && Start != 0) {
75790b57cec5SDimitry Andric         FirstSymbolAtSectionStart = false;
75800b57cec5SDimitry Andric         Start = 0;
75810b57cec5SDimitry Andric       }
75820b57cec5SDimitry Andric       else
75830b57cec5SDimitry Andric         outs() << SymName << ":\n";
75840b57cec5SDimitry Andric 
75850b57cec5SDimitry Andric       DILineInfo lastLine;
75860b57cec5SDimitry Andric       for (uint64_t Index = Start; Index < End; Index += Size) {
75870b57cec5SDimitry Andric         MCInst Inst;
75880b57cec5SDimitry Andric 
75890b57cec5SDimitry Andric         // If this is the first symbol in the section and it was not at the
75900b57cec5SDimitry Andric         // start of the section, see if we are at its Index now and if so print
75910b57cec5SDimitry Andric         // the symbol name.
75920b57cec5SDimitry Andric         if (FirstSymbol && !FirstSymbolAtSectionStart && Index == SymbolStart)
75930b57cec5SDimitry Andric           outs() << SymName << ":\n";
75940b57cec5SDimitry Andric 
75950b57cec5SDimitry Andric         uint64_t PC = SectAddress + Index;
7596fe6060f1SDimitry Andric         if (LeadingAddr) {
75970b57cec5SDimitry Andric           if (FullLeadingAddr) {
75980b57cec5SDimitry Andric             if (MachOOF->is64Bit())
75990b57cec5SDimitry Andric               outs() << format("%016" PRIx64, PC);
76000b57cec5SDimitry Andric             else
76010b57cec5SDimitry Andric               outs() << format("%08" PRIx64, PC);
76020b57cec5SDimitry Andric           } else {
76030b57cec5SDimitry Andric             outs() << format("%8" PRIx64 ":", PC);
76040b57cec5SDimitry Andric           }
76050b57cec5SDimitry Andric         }
7606fe6060f1SDimitry Andric         if (ShowRawInsn || Arch == Triple::arm)
76070b57cec5SDimitry Andric           outs() << "\t";
76080b57cec5SDimitry Andric 
76098bcb0991SDimitry Andric         if (DumpAndSkipDataInCode(PC, Bytes.data() + Index, Dices, Size))
76100b57cec5SDimitry Andric           continue;
76110b57cec5SDimitry Andric 
76120b57cec5SDimitry Andric         SmallVector<char, 64> AnnotationsBytes;
76130b57cec5SDimitry Andric         raw_svector_ostream Annotations(AnnotationsBytes);
76140b57cec5SDimitry Andric 
76150b57cec5SDimitry Andric         bool gotInst;
76160b57cec5SDimitry Andric         if (UseThumbTarget)
76170b57cec5SDimitry Andric           gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
7618480093f4SDimitry Andric                                                 PC, Annotations);
76190b57cec5SDimitry Andric         else
76200b57cec5SDimitry Andric           gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
7621480093f4SDimitry Andric                                            Annotations);
76220b57cec5SDimitry Andric         if (gotInst) {
7623fe6060f1SDimitry Andric           if (ShowRawInsn || Arch == Triple::arm) {
7624bdd1243dSDimitry Andric             dumpBytes(ArrayRef(Bytes.data() + Index, Size), outs());
76250b57cec5SDimitry Andric           }
76260b57cec5SDimitry Andric           formatted_raw_ostream FormattedOS(outs());
76270b57cec5SDimitry Andric           StringRef AnnotationsStr = Annotations.str();
76280b57cec5SDimitry Andric           if (UseThumbTarget)
7629480093f4SDimitry Andric             ThumbIP->printInst(&Inst, PC, AnnotationsStr, *ThumbSTI,
7630480093f4SDimitry Andric                                FormattedOS);
76310b57cec5SDimitry Andric           else
7632480093f4SDimitry Andric             IP->printInst(&Inst, PC, AnnotationsStr, *STI, FormattedOS);
76330b57cec5SDimitry Andric           emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
76340b57cec5SDimitry Andric 
76350b57cec5SDimitry Andric           // Print debug info.
76360b57cec5SDimitry Andric           if (diContext) {
76370b57cec5SDimitry Andric             DILineInfo dli = diContext->getLineInfoForAddress({PC, SectIdx});
76380b57cec5SDimitry Andric             // Print valid line info if it changed.
76390b57cec5SDimitry Andric             if (dli != lastLine && dli.Line != 0)
76400b57cec5SDimitry Andric               outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
76410b57cec5SDimitry Andric                      << dli.Column;
76420b57cec5SDimitry Andric             lastLine = dli;
76430b57cec5SDimitry Andric           }
76440b57cec5SDimitry Andric           outs() << "\n";
76450b57cec5SDimitry Andric         } else {
7646480093f4SDimitry Andric           if (MachOOF->getArchTriple().isX86()) {
76470b57cec5SDimitry Andric             outs() << format("\t.byte 0x%02x #bad opcode\n",
76480b57cec5SDimitry Andric                              *(Bytes.data() + Index) & 0xff);
76490b57cec5SDimitry Andric             Size = 1; // skip exactly one illegible byte and move on.
76500b57cec5SDimitry Andric           } else if (Arch == Triple::aarch64 ||
76510b57cec5SDimitry Andric                      (Arch == Triple::arm && !IsThumb)) {
76520b57cec5SDimitry Andric             uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
76530b57cec5SDimitry Andric                               (*(Bytes.data() + Index + 1) & 0xff) << 8 |
76540b57cec5SDimitry Andric                               (*(Bytes.data() + Index + 2) & 0xff) << 16 |
76550b57cec5SDimitry Andric                               (*(Bytes.data() + Index + 3) & 0xff) << 24;
76560b57cec5SDimitry Andric             outs() << format("\t.long\t0x%08x\n", opcode);
76570b57cec5SDimitry Andric             Size = 4;
76580b57cec5SDimitry Andric           } else if (Arch == Triple::arm) {
76590b57cec5SDimitry Andric             assert(IsThumb && "ARM mode should have been dealt with above");
76600b57cec5SDimitry Andric             uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
76610b57cec5SDimitry Andric                               (*(Bytes.data() + Index + 1) & 0xff) << 8;
76620b57cec5SDimitry Andric             outs() << format("\t.short\t0x%04x\n", opcode);
76630b57cec5SDimitry Andric             Size = 2;
76640b57cec5SDimitry Andric           } else{
76650b57cec5SDimitry Andric             WithColor::warning(errs(), "llvm-objdump")
76660b57cec5SDimitry Andric                 << "invalid instruction encoding\n";
76670b57cec5SDimitry Andric             if (Size == 0)
76680b57cec5SDimitry Andric               Size = 1; // skip illegible bytes
76690b57cec5SDimitry Andric           }
76700b57cec5SDimitry Andric         }
76710b57cec5SDimitry Andric       }
76720b57cec5SDimitry Andric       // Now that we are done disassembled the first symbol set the bool that
76730b57cec5SDimitry Andric       // were doing this to false.
76740b57cec5SDimitry Andric       FirstSymbol = false;
76750b57cec5SDimitry Andric     }
76760b57cec5SDimitry Andric     if (!symbolTableWorked) {
76770b57cec5SDimitry Andric       // Reading the symbol table didn't work, disassemble the whole section.
76780b57cec5SDimitry Andric       uint64_t SectAddress = Sections[SectIdx].getAddress();
76790b57cec5SDimitry Andric       uint64_t SectSize = Sections[SectIdx].getSize();
76800b57cec5SDimitry Andric       uint64_t InstSize;
76810b57cec5SDimitry Andric       for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
76820b57cec5SDimitry Andric         MCInst Inst;
76830b57cec5SDimitry Andric 
76840b57cec5SDimitry Andric         uint64_t PC = SectAddress + Index;
76858bcb0991SDimitry Andric 
76868bcb0991SDimitry Andric         if (DumpAndSkipDataInCode(PC, Bytes.data() + Index, Dices, InstSize))
76878bcb0991SDimitry Andric           continue;
76888bcb0991SDimitry Andric 
76890b57cec5SDimitry Andric         SmallVector<char, 64> AnnotationsBytes;
76900b57cec5SDimitry Andric         raw_svector_ostream Annotations(AnnotationsBytes);
76910b57cec5SDimitry Andric         if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
7692480093f4SDimitry Andric                                    Annotations)) {
7693fe6060f1SDimitry Andric           if (LeadingAddr) {
76940b57cec5SDimitry Andric             if (FullLeadingAddr) {
76950b57cec5SDimitry Andric               if (MachOOF->is64Bit())
76960b57cec5SDimitry Andric                 outs() << format("%016" PRIx64, PC);
76970b57cec5SDimitry Andric               else
76980b57cec5SDimitry Andric                 outs() << format("%08" PRIx64, PC);
76990b57cec5SDimitry Andric             } else {
77000b57cec5SDimitry Andric               outs() << format("%8" PRIx64 ":", PC);
77010b57cec5SDimitry Andric             }
77020b57cec5SDimitry Andric           }
7703fe6060f1SDimitry Andric           if (ShowRawInsn || Arch == Triple::arm) {
77040b57cec5SDimitry Andric             outs() << "\t";
7705bdd1243dSDimitry Andric             dumpBytes(ArrayRef(Bytes.data() + Index, InstSize), outs());
77060b57cec5SDimitry Andric           }
77070b57cec5SDimitry Andric           StringRef AnnotationsStr = Annotations.str();
7708480093f4SDimitry Andric           IP->printInst(&Inst, PC, AnnotationsStr, *STI, outs());
77090b57cec5SDimitry Andric           outs() << "\n";
77100b57cec5SDimitry Andric         } else {
7711480093f4SDimitry Andric           if (MachOOF->getArchTriple().isX86()) {
77120b57cec5SDimitry Andric             outs() << format("\t.byte 0x%02x #bad opcode\n",
77130b57cec5SDimitry Andric                              *(Bytes.data() + Index) & 0xff);
77140b57cec5SDimitry Andric             InstSize = 1; // skip exactly one illegible byte and move on.
77150b57cec5SDimitry Andric           } else {
77160b57cec5SDimitry Andric             WithColor::warning(errs(), "llvm-objdump")
77170b57cec5SDimitry Andric                 << "invalid instruction encoding\n";
77180b57cec5SDimitry Andric             if (InstSize == 0)
77190b57cec5SDimitry Andric               InstSize = 1; // skip illegible bytes
77200b57cec5SDimitry Andric           }
77210b57cec5SDimitry Andric         }
77220b57cec5SDimitry Andric       }
77230b57cec5SDimitry Andric     }
77240b57cec5SDimitry Andric     // The TripleName's need to be reset if we are called again for a different
7725480093f4SDimitry Andric     // architecture.
77260b57cec5SDimitry Andric     TripleName = "";
77270b57cec5SDimitry Andric     ThumbTripleName = "";
77280b57cec5SDimitry Andric 
77290b57cec5SDimitry Andric     if (SymbolizerInfo.demangled_name != nullptr)
77300b57cec5SDimitry Andric       free(SymbolizerInfo.demangled_name);
77310b57cec5SDimitry Andric     if (ThumbSymbolizerInfo.demangled_name != nullptr)
77320b57cec5SDimitry Andric       free(ThumbSymbolizerInfo.demangled_name);
77330b57cec5SDimitry Andric   }
77340b57cec5SDimitry Andric }
77350b57cec5SDimitry Andric 
77360b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
77370b57cec5SDimitry Andric // __compact_unwind section dumping
77380b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
77390b57cec5SDimitry Andric 
77400b57cec5SDimitry Andric namespace {
77410b57cec5SDimitry Andric 
77420b57cec5SDimitry Andric template <typename T>
read(StringRef Contents,ptrdiff_t Offset)77430b57cec5SDimitry Andric static uint64_t read(StringRef Contents, ptrdiff_t Offset) {
77440b57cec5SDimitry Andric   if (Offset + sizeof(T) > Contents.size()) {
77450b57cec5SDimitry Andric     outs() << "warning: attempt to read past end of buffer\n";
77460b57cec5SDimitry Andric     return T();
77470b57cec5SDimitry Andric   }
77480b57cec5SDimitry Andric 
77495f757f3fSDimitry Andric   uint64_t Val = support::endian::read<T, llvm::endianness::little>(
77505f757f3fSDimitry Andric       Contents.data() + Offset);
77510b57cec5SDimitry Andric   return Val;
77520b57cec5SDimitry Andric }
77530b57cec5SDimitry Andric 
77540b57cec5SDimitry Andric template <typename T>
readNext(StringRef Contents,ptrdiff_t & Offset)77550b57cec5SDimitry Andric static uint64_t readNext(StringRef Contents, ptrdiff_t &Offset) {
77560b57cec5SDimitry Andric   T Val = read<T>(Contents, Offset);
77570b57cec5SDimitry Andric   Offset += sizeof(T);
77580b57cec5SDimitry Andric   return Val;
77590b57cec5SDimitry Andric }
77600b57cec5SDimitry Andric 
77610b57cec5SDimitry Andric struct CompactUnwindEntry {
77620b57cec5SDimitry Andric   uint32_t OffsetInSection;
77630b57cec5SDimitry Andric 
77640b57cec5SDimitry Andric   uint64_t FunctionAddr;
77650b57cec5SDimitry Andric   uint32_t Length;
77660b57cec5SDimitry Andric   uint32_t CompactEncoding;
77670b57cec5SDimitry Andric   uint64_t PersonalityAddr;
77680b57cec5SDimitry Andric   uint64_t LSDAAddr;
77690b57cec5SDimitry Andric 
77700b57cec5SDimitry Andric   RelocationRef FunctionReloc;
77710b57cec5SDimitry Andric   RelocationRef PersonalityReloc;
77720b57cec5SDimitry Andric   RelocationRef LSDAReloc;
77730b57cec5SDimitry Andric 
CompactUnwindEntry__anon061faff00a11::CompactUnwindEntry77740b57cec5SDimitry Andric   CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
77750b57cec5SDimitry Andric       : OffsetInSection(Offset) {
77760b57cec5SDimitry Andric     if (Is64)
77770b57cec5SDimitry Andric       read<uint64_t>(Contents, Offset);
77780b57cec5SDimitry Andric     else
77790b57cec5SDimitry Andric       read<uint32_t>(Contents, Offset);
77800b57cec5SDimitry Andric   }
77810b57cec5SDimitry Andric 
77820b57cec5SDimitry Andric private:
read__anon061faff00a11::CompactUnwindEntry77830b57cec5SDimitry Andric   template <typename UIntPtr> void read(StringRef Contents, ptrdiff_t Offset) {
77840b57cec5SDimitry Andric     FunctionAddr = readNext<UIntPtr>(Contents, Offset);
77850b57cec5SDimitry Andric     Length = readNext<uint32_t>(Contents, Offset);
77860b57cec5SDimitry Andric     CompactEncoding = readNext<uint32_t>(Contents, Offset);
77870b57cec5SDimitry Andric     PersonalityAddr = readNext<UIntPtr>(Contents, Offset);
77880b57cec5SDimitry Andric     LSDAAddr = readNext<UIntPtr>(Contents, Offset);
77890b57cec5SDimitry Andric   }
77900b57cec5SDimitry Andric };
77910b57cec5SDimitry Andric }
77920b57cec5SDimitry Andric 
77930b57cec5SDimitry Andric /// Given a relocation from __compact_unwind, consisting of the RelocationRef
77940b57cec5SDimitry Andric /// and data being relocated, determine the best base Name and Addend to use for
77950b57cec5SDimitry Andric /// display purposes.
77960b57cec5SDimitry Andric ///
77970b57cec5SDimitry Andric /// 1. An Extern relocation will directly reference a symbol (and the data is
77980b57cec5SDimitry Andric ///    then already an addend), so use that.
77990b57cec5SDimitry Andric /// 2. Otherwise the data is an offset in the object file's layout; try to find
78000b57cec5SDimitry Andric //     a symbol before it in the same section, and use the offset from there.
78010b57cec5SDimitry Andric /// 3. Finally, if all that fails, fall back to an offset from the start of the
78020b57cec5SDimitry Andric ///    referenced section.
findUnwindRelocNameAddend(const MachOObjectFile * Obj,std::map<uint64_t,SymbolRef> & Symbols,const RelocationRef & Reloc,uint64_t Addr,StringRef & Name,uint64_t & Addend)78030b57cec5SDimitry Andric static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
78040b57cec5SDimitry Andric                                       std::map<uint64_t, SymbolRef> &Symbols,
78050b57cec5SDimitry Andric                                       const RelocationRef &Reloc, uint64_t Addr,
78060b57cec5SDimitry Andric                                       StringRef &Name, uint64_t &Addend) {
78070b57cec5SDimitry Andric   if (Reloc.getSymbol() != Obj->symbol_end()) {
78080b57cec5SDimitry Andric     Name = unwrapOrError(Reloc.getSymbol()->getName(), Obj->getFileName());
78090b57cec5SDimitry Andric     Addend = Addr;
78100b57cec5SDimitry Andric     return;
78110b57cec5SDimitry Andric   }
78120b57cec5SDimitry Andric 
78130b57cec5SDimitry Andric   auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
78140b57cec5SDimitry Andric   SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
78150b57cec5SDimitry Andric 
78160b57cec5SDimitry Andric   uint64_t SectionAddr = RelocSection.getAddress();
78170b57cec5SDimitry Andric 
78180b57cec5SDimitry Andric   auto Sym = Symbols.upper_bound(Addr);
78190b57cec5SDimitry Andric   if (Sym == Symbols.begin()) {
78200b57cec5SDimitry Andric     // The first symbol in the object is after this reference, the best we can
78210b57cec5SDimitry Andric     // do is section-relative notation.
78228bcb0991SDimitry Andric     if (Expected<StringRef> NameOrErr = RelocSection.getName())
78238bcb0991SDimitry Andric       Name = *NameOrErr;
78248bcb0991SDimitry Andric     else
78258bcb0991SDimitry Andric       consumeError(NameOrErr.takeError());
78268bcb0991SDimitry Andric 
78270b57cec5SDimitry Andric     Addend = Addr - SectionAddr;
78280b57cec5SDimitry Andric     return;
78290b57cec5SDimitry Andric   }
78300b57cec5SDimitry Andric 
78310b57cec5SDimitry Andric   // Go back one so that SymbolAddress <= Addr.
78320b57cec5SDimitry Andric   --Sym;
78330b57cec5SDimitry Andric 
78340b57cec5SDimitry Andric   section_iterator SymSection =
78350b57cec5SDimitry Andric       unwrapOrError(Sym->second.getSection(), Obj->getFileName());
78360b57cec5SDimitry Andric   if (RelocSection == *SymSection) {
78370b57cec5SDimitry Andric     // There's a valid symbol in the same section before this reference.
78380b57cec5SDimitry Andric     Name = unwrapOrError(Sym->second.getName(), Obj->getFileName());
78390b57cec5SDimitry Andric     Addend = Addr - Sym->first;
78400b57cec5SDimitry Andric     return;
78410b57cec5SDimitry Andric   }
78420b57cec5SDimitry Andric 
78430b57cec5SDimitry Andric   // There is a symbol before this reference, but it's in a different
78440b57cec5SDimitry Andric   // section. Probably not helpful to mention it, so use the section name.
78458bcb0991SDimitry Andric   if (Expected<StringRef> NameOrErr = RelocSection.getName())
78468bcb0991SDimitry Andric     Name = *NameOrErr;
78478bcb0991SDimitry Andric   else
78488bcb0991SDimitry Andric     consumeError(NameOrErr.takeError());
78498bcb0991SDimitry Andric 
78500b57cec5SDimitry Andric   Addend = Addr - SectionAddr;
78510b57cec5SDimitry Andric }
78520b57cec5SDimitry Andric 
printUnwindRelocDest(const MachOObjectFile * Obj,std::map<uint64_t,SymbolRef> & Symbols,const RelocationRef & Reloc,uint64_t Addr)78530b57cec5SDimitry Andric static void printUnwindRelocDest(const MachOObjectFile *Obj,
78540b57cec5SDimitry Andric                                  std::map<uint64_t, SymbolRef> &Symbols,
78550b57cec5SDimitry Andric                                  const RelocationRef &Reloc, uint64_t Addr) {
78560b57cec5SDimitry Andric   StringRef Name;
78570b57cec5SDimitry Andric   uint64_t Addend;
78580b57cec5SDimitry Andric 
78590b57cec5SDimitry Andric   if (!Reloc.getObject())
78600b57cec5SDimitry Andric     return;
78610b57cec5SDimitry Andric 
78620b57cec5SDimitry Andric   findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
78630b57cec5SDimitry Andric 
78640b57cec5SDimitry Andric   outs() << Name;
78650b57cec5SDimitry Andric   if (Addend)
78660b57cec5SDimitry Andric     outs() << " + " << format("0x%" PRIx64, Addend);
78670b57cec5SDimitry Andric }
78680b57cec5SDimitry Andric 
78690b57cec5SDimitry Andric static void
printMachOCompactUnwindSection(const MachOObjectFile * Obj,std::map<uint64_t,SymbolRef> & Symbols,const SectionRef & CompactUnwind)78700b57cec5SDimitry Andric printMachOCompactUnwindSection(const MachOObjectFile *Obj,
78710b57cec5SDimitry Andric                                std::map<uint64_t, SymbolRef> &Symbols,
78720b57cec5SDimitry Andric                                const SectionRef &CompactUnwind) {
78730b57cec5SDimitry Andric 
78740b57cec5SDimitry Andric   if (!Obj->isLittleEndian()) {
78750b57cec5SDimitry Andric     outs() << "Skipping big-endian __compact_unwind section\n";
78760b57cec5SDimitry Andric     return;
78770b57cec5SDimitry Andric   }
78780b57cec5SDimitry Andric 
78790b57cec5SDimitry Andric   bool Is64 = Obj->is64Bit();
78800b57cec5SDimitry Andric   uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
78810b57cec5SDimitry Andric   uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
78820b57cec5SDimitry Andric 
78830b57cec5SDimitry Andric   StringRef Contents =
78840b57cec5SDimitry Andric       unwrapOrError(CompactUnwind.getContents(), Obj->getFileName());
78850b57cec5SDimitry Andric   SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
78860b57cec5SDimitry Andric 
78870b57cec5SDimitry Andric   // First populate the initial raw offsets, encodings and so on from the entry.
78880b57cec5SDimitry Andric   for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
78890b57cec5SDimitry Andric     CompactUnwindEntry Entry(Contents, Offset, Is64);
78900b57cec5SDimitry Andric     CompactUnwinds.push_back(Entry);
78910b57cec5SDimitry Andric   }
78920b57cec5SDimitry Andric 
78930b57cec5SDimitry Andric   // Next we need to look at the relocations to find out what objects are
78940b57cec5SDimitry Andric   // actually being referred to.
78950b57cec5SDimitry Andric   for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
78960b57cec5SDimitry Andric     uint64_t RelocAddress = Reloc.getOffset();
78970b57cec5SDimitry Andric 
78980b57cec5SDimitry Andric     uint32_t EntryIdx = RelocAddress / EntrySize;
78990b57cec5SDimitry Andric     uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
79000b57cec5SDimitry Andric     CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
79010b57cec5SDimitry Andric 
79020b57cec5SDimitry Andric     if (OffsetInEntry == 0)
79030b57cec5SDimitry Andric       Entry.FunctionReloc = Reloc;
79040b57cec5SDimitry Andric     else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
79050b57cec5SDimitry Andric       Entry.PersonalityReloc = Reloc;
79060b57cec5SDimitry Andric     else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
79070b57cec5SDimitry Andric       Entry.LSDAReloc = Reloc;
79080b57cec5SDimitry Andric     else {
79090b57cec5SDimitry Andric       outs() << "Invalid relocation in __compact_unwind section\n";
79100b57cec5SDimitry Andric       return;
79110b57cec5SDimitry Andric     }
79120b57cec5SDimitry Andric   }
79130b57cec5SDimitry Andric 
79140b57cec5SDimitry Andric   // Finally, we're ready to print the data we've gathered.
79150b57cec5SDimitry Andric   outs() << "Contents of __compact_unwind section:\n";
79160b57cec5SDimitry Andric   for (auto &Entry : CompactUnwinds) {
79170b57cec5SDimitry Andric     outs() << "  Entry at offset "
79180b57cec5SDimitry Andric            << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
79190b57cec5SDimitry Andric 
79200b57cec5SDimitry Andric     // 1. Start of the region this entry applies to.
79210b57cec5SDimitry Andric     outs() << "    start:                " << format("0x%" PRIx64,
79220b57cec5SDimitry Andric                                                      Entry.FunctionAddr) << ' ';
79230b57cec5SDimitry Andric     printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
79240b57cec5SDimitry Andric     outs() << '\n';
79250b57cec5SDimitry Andric 
79260b57cec5SDimitry Andric     // 2. Length of the region this entry applies to.
79270b57cec5SDimitry Andric     outs() << "    length:               " << format("0x%" PRIx32, Entry.Length)
79280b57cec5SDimitry Andric            << '\n';
79290b57cec5SDimitry Andric     // 3. The 32-bit compact encoding.
79300b57cec5SDimitry Andric     outs() << "    compact encoding:     "
79310b57cec5SDimitry Andric            << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
79320b57cec5SDimitry Andric 
79330b57cec5SDimitry Andric     // 4. The personality function, if present.
79340b57cec5SDimitry Andric     if (Entry.PersonalityReloc.getObject()) {
79350b57cec5SDimitry Andric       outs() << "    personality function: "
79360b57cec5SDimitry Andric              << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
79370b57cec5SDimitry Andric       printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
79380b57cec5SDimitry Andric                            Entry.PersonalityAddr);
79390b57cec5SDimitry Andric       outs() << '\n';
79400b57cec5SDimitry Andric     }
79410b57cec5SDimitry Andric 
79420b57cec5SDimitry Andric     // 5. This entry's language-specific data area.
79430b57cec5SDimitry Andric     if (Entry.LSDAReloc.getObject()) {
79440b57cec5SDimitry Andric       outs() << "    LSDA:                 " << format("0x%" PRIx64,
79450b57cec5SDimitry Andric                                                        Entry.LSDAAddr) << ' ';
79460b57cec5SDimitry Andric       printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
79470b57cec5SDimitry Andric       outs() << '\n';
79480b57cec5SDimitry Andric     }
79490b57cec5SDimitry Andric   }
79500b57cec5SDimitry Andric }
79510b57cec5SDimitry Andric 
79520b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
79530b57cec5SDimitry Andric // __unwind_info section dumping
79540b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
79550b57cec5SDimitry Andric 
printRegularSecondLevelUnwindPage(StringRef PageData)79560b57cec5SDimitry Andric static void printRegularSecondLevelUnwindPage(StringRef PageData) {
79570b57cec5SDimitry Andric   ptrdiff_t Pos = 0;
79580b57cec5SDimitry Andric   uint32_t Kind = readNext<uint32_t>(PageData, Pos);
79590b57cec5SDimitry Andric   (void)Kind;
79600b57cec5SDimitry Andric   assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
79610b57cec5SDimitry Andric 
79620b57cec5SDimitry Andric   uint16_t EntriesStart = readNext<uint16_t>(PageData, Pos);
79630b57cec5SDimitry Andric   uint16_t NumEntries = readNext<uint16_t>(PageData, Pos);
79640b57cec5SDimitry Andric 
79650b57cec5SDimitry Andric   Pos = EntriesStart;
79660b57cec5SDimitry Andric   for (unsigned i = 0; i < NumEntries; ++i) {
79670b57cec5SDimitry Andric     uint32_t FunctionOffset = readNext<uint32_t>(PageData, Pos);
79680b57cec5SDimitry Andric     uint32_t Encoding = readNext<uint32_t>(PageData, Pos);
79690b57cec5SDimitry Andric 
79700b57cec5SDimitry Andric     outs() << "      [" << i << "]: "
79710b57cec5SDimitry Andric            << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
79720b57cec5SDimitry Andric            << ", "
79730b57cec5SDimitry Andric            << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
79740b57cec5SDimitry Andric   }
79750b57cec5SDimitry Andric }
79760b57cec5SDimitry Andric 
printCompressedSecondLevelUnwindPage(StringRef PageData,uint32_t FunctionBase,const SmallVectorImpl<uint32_t> & CommonEncodings)79770b57cec5SDimitry Andric static void printCompressedSecondLevelUnwindPage(
79780b57cec5SDimitry Andric     StringRef PageData, uint32_t FunctionBase,
79790b57cec5SDimitry Andric     const SmallVectorImpl<uint32_t> &CommonEncodings) {
79800b57cec5SDimitry Andric   ptrdiff_t Pos = 0;
79810b57cec5SDimitry Andric   uint32_t Kind = readNext<uint32_t>(PageData, Pos);
79820b57cec5SDimitry Andric   (void)Kind;
79830b57cec5SDimitry Andric   assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
79840b57cec5SDimitry Andric 
7985fe6060f1SDimitry Andric   uint32_t NumCommonEncodings = CommonEncodings.size();
79860b57cec5SDimitry Andric   uint16_t EntriesStart = readNext<uint16_t>(PageData, Pos);
79870b57cec5SDimitry Andric   uint16_t NumEntries = readNext<uint16_t>(PageData, Pos);
79880b57cec5SDimitry Andric 
7989fe6060f1SDimitry Andric   uint16_t PageEncodingsStart = readNext<uint16_t>(PageData, Pos);
7990fe6060f1SDimitry Andric   uint16_t NumPageEncodings = readNext<uint16_t>(PageData, Pos);
7991fe6060f1SDimitry Andric   SmallVector<uint32_t, 64> PageEncodings;
7992fe6060f1SDimitry Andric   if (NumPageEncodings) {
7993fe6060f1SDimitry Andric     outs() << "      Page encodings: (count = " << NumPageEncodings << ")\n";
7994fe6060f1SDimitry Andric     Pos = PageEncodingsStart;
7995fe6060f1SDimitry Andric     for (unsigned i = 0; i < NumPageEncodings; ++i) {
7996fe6060f1SDimitry Andric       uint32_t Encoding = readNext<uint32_t>(PageData, Pos);
7997fe6060f1SDimitry Andric       PageEncodings.push_back(Encoding);
7998fe6060f1SDimitry Andric       outs() << "        encoding[" << (i + NumCommonEncodings)
7999fe6060f1SDimitry Andric              << "]: " << format("0x%08" PRIx32, Encoding) << '\n';
8000fe6060f1SDimitry Andric     }
8001fe6060f1SDimitry Andric   }
80020b57cec5SDimitry Andric 
80030b57cec5SDimitry Andric   Pos = EntriesStart;
80040b57cec5SDimitry Andric   for (unsigned i = 0; i < NumEntries; ++i) {
80050b57cec5SDimitry Andric     uint32_t Entry = readNext<uint32_t>(PageData, Pos);
80060b57cec5SDimitry Andric     uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
80070b57cec5SDimitry Andric     uint32_t EncodingIdx = Entry >> 24;
80080b57cec5SDimitry Andric 
80090b57cec5SDimitry Andric     uint32_t Encoding;
8010fe6060f1SDimitry Andric     if (EncodingIdx < NumCommonEncodings)
80110b57cec5SDimitry Andric       Encoding = CommonEncodings[EncodingIdx];
80120b57cec5SDimitry Andric     else
8013fe6060f1SDimitry Andric       Encoding = PageEncodings[EncodingIdx - NumCommonEncodings];
80140b57cec5SDimitry Andric 
80150b57cec5SDimitry Andric     outs() << "      [" << i << "]: "
80160b57cec5SDimitry Andric            << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
80170b57cec5SDimitry Andric            << ", "
80180b57cec5SDimitry Andric            << "encoding[" << EncodingIdx
80190b57cec5SDimitry Andric            << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
80200b57cec5SDimitry Andric   }
80210b57cec5SDimitry Andric }
80220b57cec5SDimitry Andric 
printMachOUnwindInfoSection(const MachOObjectFile * Obj,std::map<uint64_t,SymbolRef> & Symbols,const SectionRef & UnwindInfo)80230b57cec5SDimitry Andric static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
80240b57cec5SDimitry Andric                                         std::map<uint64_t, SymbolRef> &Symbols,
80250b57cec5SDimitry Andric                                         const SectionRef &UnwindInfo) {
80260b57cec5SDimitry Andric 
80270b57cec5SDimitry Andric   if (!Obj->isLittleEndian()) {
80280b57cec5SDimitry Andric     outs() << "Skipping big-endian __unwind_info section\n";
80290b57cec5SDimitry Andric     return;
80300b57cec5SDimitry Andric   }
80310b57cec5SDimitry Andric 
80320b57cec5SDimitry Andric   outs() << "Contents of __unwind_info section:\n";
80330b57cec5SDimitry Andric 
80340b57cec5SDimitry Andric   StringRef Contents =
80350b57cec5SDimitry Andric       unwrapOrError(UnwindInfo.getContents(), Obj->getFileName());
80360b57cec5SDimitry Andric   ptrdiff_t Pos = 0;
80370b57cec5SDimitry Andric 
80380b57cec5SDimitry Andric   //===----------------------------------
80390b57cec5SDimitry Andric   // Section header
80400b57cec5SDimitry Andric   //===----------------------------------
80410b57cec5SDimitry Andric 
80420b57cec5SDimitry Andric   uint32_t Version = readNext<uint32_t>(Contents, Pos);
80430b57cec5SDimitry Andric   outs() << "  Version:                                   "
80440b57cec5SDimitry Andric          << format("0x%" PRIx32, Version) << '\n';
80450b57cec5SDimitry Andric   if (Version != 1) {
80460b57cec5SDimitry Andric     outs() << "    Skipping section with unknown version\n";
80470b57cec5SDimitry Andric     return;
80480b57cec5SDimitry Andric   }
80490b57cec5SDimitry Andric 
80500b57cec5SDimitry Andric   uint32_t CommonEncodingsStart = readNext<uint32_t>(Contents, Pos);
80510b57cec5SDimitry Andric   outs() << "  Common encodings array section offset:     "
80520b57cec5SDimitry Andric          << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
80530b57cec5SDimitry Andric   uint32_t NumCommonEncodings = readNext<uint32_t>(Contents, Pos);
80540b57cec5SDimitry Andric   outs() << "  Number of common encodings in array:       "
80550b57cec5SDimitry Andric          << format("0x%" PRIx32, NumCommonEncodings) << '\n';
80560b57cec5SDimitry Andric 
80570b57cec5SDimitry Andric   uint32_t PersonalitiesStart = readNext<uint32_t>(Contents, Pos);
80580b57cec5SDimitry Andric   outs() << "  Personality function array section offset: "
80590b57cec5SDimitry Andric          << format("0x%" PRIx32, PersonalitiesStart) << '\n';
80600b57cec5SDimitry Andric   uint32_t NumPersonalities = readNext<uint32_t>(Contents, Pos);
80610b57cec5SDimitry Andric   outs() << "  Number of personality functions in array:  "
80620b57cec5SDimitry Andric          << format("0x%" PRIx32, NumPersonalities) << '\n';
80630b57cec5SDimitry Andric 
80640b57cec5SDimitry Andric   uint32_t IndicesStart = readNext<uint32_t>(Contents, Pos);
80650b57cec5SDimitry Andric   outs() << "  Index array section offset:                "
80660b57cec5SDimitry Andric          << format("0x%" PRIx32, IndicesStart) << '\n';
80670b57cec5SDimitry Andric   uint32_t NumIndices = readNext<uint32_t>(Contents, Pos);
80680b57cec5SDimitry Andric   outs() << "  Number of indices in array:                "
80690b57cec5SDimitry Andric          << format("0x%" PRIx32, NumIndices) << '\n';
80700b57cec5SDimitry Andric 
80710b57cec5SDimitry Andric   //===----------------------------------
80720b57cec5SDimitry Andric   // A shared list of common encodings
80730b57cec5SDimitry Andric   //===----------------------------------
80740b57cec5SDimitry Andric 
80750b57cec5SDimitry Andric   // These occupy indices in the range [0, N] whenever an encoding is referenced
80760b57cec5SDimitry Andric   // from a compressed 2nd level index table. In practice the linker only
80770b57cec5SDimitry Andric   // creates ~128 of these, so that indices are available to embed encodings in
80780b57cec5SDimitry Andric   // the 2nd level index.
80790b57cec5SDimitry Andric 
80800b57cec5SDimitry Andric   SmallVector<uint32_t, 64> CommonEncodings;
80810b57cec5SDimitry Andric   outs() << "  Common encodings: (count = " << NumCommonEncodings << ")\n";
80820b57cec5SDimitry Andric   Pos = CommonEncodingsStart;
80830b57cec5SDimitry Andric   for (unsigned i = 0; i < NumCommonEncodings; ++i) {
80840b57cec5SDimitry Andric     uint32_t Encoding = readNext<uint32_t>(Contents, Pos);
80850b57cec5SDimitry Andric     CommonEncodings.push_back(Encoding);
80860b57cec5SDimitry Andric 
80870b57cec5SDimitry Andric     outs() << "    encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
80880b57cec5SDimitry Andric            << '\n';
80890b57cec5SDimitry Andric   }
80900b57cec5SDimitry Andric 
80910b57cec5SDimitry Andric   //===----------------------------------
80920b57cec5SDimitry Andric   // Personality functions used in this executable
80930b57cec5SDimitry Andric   //===----------------------------------
80940b57cec5SDimitry Andric 
80950b57cec5SDimitry Andric   // There should be only a handful of these (one per source language,
80960b57cec5SDimitry Andric   // roughly). Particularly since they only get 2 bits in the compact encoding.
80970b57cec5SDimitry Andric 
80980b57cec5SDimitry Andric   outs() << "  Personality functions: (count = " << NumPersonalities << ")\n";
80990b57cec5SDimitry Andric   Pos = PersonalitiesStart;
81000b57cec5SDimitry Andric   for (unsigned i = 0; i < NumPersonalities; ++i) {
81010b57cec5SDimitry Andric     uint32_t PersonalityFn = readNext<uint32_t>(Contents, Pos);
81020b57cec5SDimitry Andric     outs() << "    personality[" << i + 1
81030b57cec5SDimitry Andric            << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
81040b57cec5SDimitry Andric   }
81050b57cec5SDimitry Andric 
81060b57cec5SDimitry Andric   //===----------------------------------
81070b57cec5SDimitry Andric   // The level 1 index entries
81080b57cec5SDimitry Andric   //===----------------------------------
81090b57cec5SDimitry Andric 
81100b57cec5SDimitry Andric   // These specify an approximate place to start searching for the more detailed
81110b57cec5SDimitry Andric   // information, sorted by PC.
81120b57cec5SDimitry Andric 
81130b57cec5SDimitry Andric   struct IndexEntry {
81140b57cec5SDimitry Andric     uint32_t FunctionOffset;
81150b57cec5SDimitry Andric     uint32_t SecondLevelPageStart;
81160b57cec5SDimitry Andric     uint32_t LSDAStart;
81170b57cec5SDimitry Andric   };
81180b57cec5SDimitry Andric 
81190b57cec5SDimitry Andric   SmallVector<IndexEntry, 4> IndexEntries;
81200b57cec5SDimitry Andric 
81210b57cec5SDimitry Andric   outs() << "  Top level indices: (count = " << NumIndices << ")\n";
81220b57cec5SDimitry Andric   Pos = IndicesStart;
81230b57cec5SDimitry Andric   for (unsigned i = 0; i < NumIndices; ++i) {
81240b57cec5SDimitry Andric     IndexEntry Entry;
81250b57cec5SDimitry Andric 
81260b57cec5SDimitry Andric     Entry.FunctionOffset = readNext<uint32_t>(Contents, Pos);
81270b57cec5SDimitry Andric     Entry.SecondLevelPageStart = readNext<uint32_t>(Contents, Pos);
81280b57cec5SDimitry Andric     Entry.LSDAStart = readNext<uint32_t>(Contents, Pos);
81290b57cec5SDimitry Andric     IndexEntries.push_back(Entry);
81300b57cec5SDimitry Andric 
81310b57cec5SDimitry Andric     outs() << "    [" << i << "]: "
81320b57cec5SDimitry Andric            << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
81330b57cec5SDimitry Andric            << ", "
81340b57cec5SDimitry Andric            << "2nd level page offset="
81350b57cec5SDimitry Andric            << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
81360b57cec5SDimitry Andric            << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
81370b57cec5SDimitry Andric   }
81380b57cec5SDimitry Andric 
81390b57cec5SDimitry Andric   //===----------------------------------
81400b57cec5SDimitry Andric   // Next come the LSDA tables
81410b57cec5SDimitry Andric   //===----------------------------------
81420b57cec5SDimitry Andric 
81430b57cec5SDimitry Andric   // The LSDA layout is rather implicit: it's a contiguous array of entries from
81440b57cec5SDimitry Andric   // the first top-level index's LSDAOffset to the last (sentinel).
81450b57cec5SDimitry Andric 
81460b57cec5SDimitry Andric   outs() << "  LSDA descriptors:\n";
81470b57cec5SDimitry Andric   Pos = IndexEntries[0].LSDAStart;
81480b57cec5SDimitry Andric   const uint32_t LSDASize = 2 * sizeof(uint32_t);
81490b57cec5SDimitry Andric   int NumLSDAs =
81500b57cec5SDimitry Andric       (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) / LSDASize;
81510b57cec5SDimitry Andric 
81520b57cec5SDimitry Andric   for (int i = 0; i < NumLSDAs; ++i) {
81530b57cec5SDimitry Andric     uint32_t FunctionOffset = readNext<uint32_t>(Contents, Pos);
81540b57cec5SDimitry Andric     uint32_t LSDAOffset = readNext<uint32_t>(Contents, Pos);
81550b57cec5SDimitry Andric     outs() << "    [" << i << "]: "
81560b57cec5SDimitry Andric            << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
81570b57cec5SDimitry Andric            << ", "
81580b57cec5SDimitry Andric            << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
81590b57cec5SDimitry Andric   }
81600b57cec5SDimitry Andric 
81610b57cec5SDimitry Andric   //===----------------------------------
81620b57cec5SDimitry Andric   // Finally, the 2nd level indices
81630b57cec5SDimitry Andric   //===----------------------------------
81640b57cec5SDimitry Andric 
81650b57cec5SDimitry Andric   // Generally these are 4K in size, and have 2 possible forms:
81660b57cec5SDimitry Andric   //   + Regular stores up to 511 entries with disparate encodings
81670b57cec5SDimitry Andric   //   + Compressed stores up to 1021 entries if few enough compact encoding
81680b57cec5SDimitry Andric   //     values are used.
81690b57cec5SDimitry Andric   outs() << "  Second level indices:\n";
81700b57cec5SDimitry Andric   for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
81710b57cec5SDimitry Andric     // The final sentinel top-level index has no associated 2nd level page
81720b57cec5SDimitry Andric     if (IndexEntries[i].SecondLevelPageStart == 0)
81730b57cec5SDimitry Andric       break;
81740b57cec5SDimitry Andric 
81750b57cec5SDimitry Andric     outs() << "    Second level index[" << i << "]: "
81760b57cec5SDimitry Andric            << "offset in section="
81770b57cec5SDimitry Andric            << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
81780b57cec5SDimitry Andric            << ", "
81790b57cec5SDimitry Andric            << "base function offset="
81800b57cec5SDimitry Andric            << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
81810b57cec5SDimitry Andric 
81820b57cec5SDimitry Andric     Pos = IndexEntries[i].SecondLevelPageStart;
81830b57cec5SDimitry Andric     if (Pos + sizeof(uint32_t) > Contents.size()) {
81840b57cec5SDimitry Andric       outs() << "warning: invalid offset for second level page: " << Pos << '\n';
81850b57cec5SDimitry Andric       continue;
81860b57cec5SDimitry Andric     }
81870b57cec5SDimitry Andric 
81880b57cec5SDimitry Andric     uint32_t Kind =
81890b57cec5SDimitry Andric         *reinterpret_cast<const support::ulittle32_t *>(Contents.data() + Pos);
81900b57cec5SDimitry Andric     if (Kind == 2)
81910b57cec5SDimitry Andric       printRegularSecondLevelUnwindPage(Contents.substr(Pos, 4096));
81920b57cec5SDimitry Andric     else if (Kind == 3)
81930b57cec5SDimitry Andric       printCompressedSecondLevelUnwindPage(Contents.substr(Pos, 4096),
81940b57cec5SDimitry Andric                                            IndexEntries[i].FunctionOffset,
81950b57cec5SDimitry Andric                                            CommonEncodings);
81960b57cec5SDimitry Andric     else
81970b57cec5SDimitry Andric       outs() << "    Skipping 2nd level page with unknown kind " << Kind
81980b57cec5SDimitry Andric              << '\n';
81990b57cec5SDimitry Andric   }
82000b57cec5SDimitry Andric }
82010b57cec5SDimitry Andric 
printMachOUnwindInfo(const MachOObjectFile * Obj)82025ffd83dbSDimitry Andric void objdump::printMachOUnwindInfo(const MachOObjectFile *Obj) {
82030b57cec5SDimitry Andric   std::map<uint64_t, SymbolRef> Symbols;
82040b57cec5SDimitry Andric   for (const SymbolRef &SymRef : Obj->symbols()) {
82050b57cec5SDimitry Andric     // Discard any undefined or absolute symbols. They're not going to take part
82060b57cec5SDimitry Andric     // in the convenience lookup for unwind info and just take up resources.
82070b57cec5SDimitry Andric     auto SectOrErr = SymRef.getSection();
82080b57cec5SDimitry Andric     if (!SectOrErr) {
82090b57cec5SDimitry Andric       // TODO: Actually report errors helpfully.
82100b57cec5SDimitry Andric       consumeError(SectOrErr.takeError());
82110b57cec5SDimitry Andric       continue;
82120b57cec5SDimitry Andric     }
82130b57cec5SDimitry Andric     section_iterator Section = *SectOrErr;
82140b57cec5SDimitry Andric     if (Section == Obj->section_end())
82150b57cec5SDimitry Andric       continue;
82160b57cec5SDimitry Andric 
82175ffd83dbSDimitry Andric     uint64_t Addr = cantFail(SymRef.getValue());
82180b57cec5SDimitry Andric     Symbols.insert(std::make_pair(Addr, SymRef));
82190b57cec5SDimitry Andric   }
82200b57cec5SDimitry Andric 
82210b57cec5SDimitry Andric   for (const SectionRef &Section : Obj->sections()) {
82220b57cec5SDimitry Andric     StringRef SectName;
82238bcb0991SDimitry Andric     if (Expected<StringRef> NameOrErr = Section.getName())
82248bcb0991SDimitry Andric       SectName = *NameOrErr;
82258bcb0991SDimitry Andric     else
82268bcb0991SDimitry Andric       consumeError(NameOrErr.takeError());
82278bcb0991SDimitry Andric 
82280b57cec5SDimitry Andric     if (SectName == "__compact_unwind")
82290b57cec5SDimitry Andric       printMachOCompactUnwindSection(Obj, Symbols, Section);
82300b57cec5SDimitry Andric     else if (SectName == "__unwind_info")
82310b57cec5SDimitry Andric       printMachOUnwindInfoSection(Obj, Symbols, Section);
82320b57cec5SDimitry Andric   }
82330b57cec5SDimitry Andric }
82340b57cec5SDimitry Andric 
PrintMachHeader(uint32_t magic,uint32_t cputype,uint32_t cpusubtype,uint32_t filetype,uint32_t ncmds,uint32_t sizeofcmds,uint32_t flags,bool verbose)82350b57cec5SDimitry Andric static void PrintMachHeader(uint32_t magic, uint32_t cputype,
82360b57cec5SDimitry Andric                             uint32_t cpusubtype, uint32_t filetype,
82370b57cec5SDimitry Andric                             uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
82380b57cec5SDimitry Andric                             bool verbose) {
82390b57cec5SDimitry Andric   outs() << "Mach header\n";
82400b57cec5SDimitry Andric   outs() << "      magic cputype cpusubtype  caps    filetype ncmds "
82410b57cec5SDimitry Andric             "sizeofcmds      flags\n";
82420b57cec5SDimitry Andric   if (verbose) {
82430b57cec5SDimitry Andric     if (magic == MachO::MH_MAGIC)
82440b57cec5SDimitry Andric       outs() << "   MH_MAGIC";
82450b57cec5SDimitry Andric     else if (magic == MachO::MH_MAGIC_64)
82460b57cec5SDimitry Andric       outs() << "MH_MAGIC_64";
82470b57cec5SDimitry Andric     else
82480b57cec5SDimitry Andric       outs() << format(" 0x%08" PRIx32, magic);
82490b57cec5SDimitry Andric     switch (cputype) {
82500b57cec5SDimitry Andric     case MachO::CPU_TYPE_I386:
82510b57cec5SDimitry Andric       outs() << "    I386";
82520b57cec5SDimitry Andric       switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
82530b57cec5SDimitry Andric       case MachO::CPU_SUBTYPE_I386_ALL:
82540b57cec5SDimitry Andric         outs() << "        ALL";
82550b57cec5SDimitry Andric         break;
82560b57cec5SDimitry Andric       default:
82570b57cec5SDimitry Andric         outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
82580b57cec5SDimitry Andric         break;
82590b57cec5SDimitry Andric       }
82600b57cec5SDimitry Andric       break;
82610b57cec5SDimitry Andric     case MachO::CPU_TYPE_X86_64:
82620b57cec5SDimitry Andric       outs() << "  X86_64";
82630b57cec5SDimitry Andric       switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
82640b57cec5SDimitry Andric       case MachO::CPU_SUBTYPE_X86_64_ALL:
82650b57cec5SDimitry Andric         outs() << "        ALL";
82660b57cec5SDimitry Andric         break;
82670b57cec5SDimitry Andric       case MachO::CPU_SUBTYPE_X86_64_H:
82680b57cec5SDimitry Andric         outs() << "    Haswell";
82690b57cec5SDimitry Andric         break;
82700b57cec5SDimitry Andric       default:
82710b57cec5SDimitry Andric         outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
82720b57cec5SDimitry Andric         break;
82730b57cec5SDimitry Andric       }
82740b57cec5SDimitry Andric       break;
82750b57cec5SDimitry Andric     case MachO::CPU_TYPE_ARM:
82760b57cec5SDimitry Andric       outs() << "     ARM";
82770b57cec5SDimitry Andric       switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
82780b57cec5SDimitry Andric       case MachO::CPU_SUBTYPE_ARM_ALL:
82790b57cec5SDimitry Andric         outs() << "        ALL";
82800b57cec5SDimitry Andric         break;
82810b57cec5SDimitry Andric       case MachO::CPU_SUBTYPE_ARM_V4T:
82820b57cec5SDimitry Andric         outs() << "        V4T";
82830b57cec5SDimitry Andric         break;
82840b57cec5SDimitry Andric       case MachO::CPU_SUBTYPE_ARM_V5TEJ:
82850b57cec5SDimitry Andric         outs() << "      V5TEJ";
82860b57cec5SDimitry Andric         break;
82870b57cec5SDimitry Andric       case MachO::CPU_SUBTYPE_ARM_XSCALE:
82880b57cec5SDimitry Andric         outs() << "     XSCALE";
82890b57cec5SDimitry Andric         break;
82900b57cec5SDimitry Andric       case MachO::CPU_SUBTYPE_ARM_V6:
82910b57cec5SDimitry Andric         outs() << "         V6";
82920b57cec5SDimitry Andric         break;
82930b57cec5SDimitry Andric       case MachO::CPU_SUBTYPE_ARM_V6M:
82940b57cec5SDimitry Andric         outs() << "        V6M";
82950b57cec5SDimitry Andric         break;
82960b57cec5SDimitry Andric       case MachO::CPU_SUBTYPE_ARM_V7:
82970b57cec5SDimitry Andric         outs() << "         V7";
82980b57cec5SDimitry Andric         break;
82990b57cec5SDimitry Andric       case MachO::CPU_SUBTYPE_ARM_V7EM:
83000b57cec5SDimitry Andric         outs() << "       V7EM";
83010b57cec5SDimitry Andric         break;
83020b57cec5SDimitry Andric       case MachO::CPU_SUBTYPE_ARM_V7K:
83030b57cec5SDimitry Andric         outs() << "        V7K";
83040b57cec5SDimitry Andric         break;
83050b57cec5SDimitry Andric       case MachO::CPU_SUBTYPE_ARM_V7M:
83060b57cec5SDimitry Andric         outs() << "        V7M";
83070b57cec5SDimitry Andric         break;
83080b57cec5SDimitry Andric       case MachO::CPU_SUBTYPE_ARM_V7S:
83090b57cec5SDimitry Andric         outs() << "        V7S";
83100b57cec5SDimitry Andric         break;
83110b57cec5SDimitry Andric       default:
83120b57cec5SDimitry Andric         outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
83130b57cec5SDimitry Andric         break;
83140b57cec5SDimitry Andric       }
83150b57cec5SDimitry Andric       break;
83160b57cec5SDimitry Andric     case MachO::CPU_TYPE_ARM64:
83170b57cec5SDimitry Andric       outs() << "   ARM64";
83180b57cec5SDimitry Andric       switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
83190b57cec5SDimitry Andric       case MachO::CPU_SUBTYPE_ARM64_ALL:
83200b57cec5SDimitry Andric         outs() << "        ALL";
83210b57cec5SDimitry Andric         break;
8322e8d8bef9SDimitry Andric       case MachO::CPU_SUBTYPE_ARM64_V8:
8323e8d8bef9SDimitry Andric         outs() << "         V8";
8324e8d8bef9SDimitry Andric         break;
83250b57cec5SDimitry Andric       case MachO::CPU_SUBTYPE_ARM64E:
83260b57cec5SDimitry Andric         outs() << "          E";
83270b57cec5SDimitry Andric         break;
83280b57cec5SDimitry Andric       default:
83290b57cec5SDimitry Andric         outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
83300b57cec5SDimitry Andric         break;
83310b57cec5SDimitry Andric       }
83320b57cec5SDimitry Andric       break;
83330b57cec5SDimitry Andric     case MachO::CPU_TYPE_ARM64_32:
83340b57cec5SDimitry Andric       outs() << " ARM64_32";
83350b57cec5SDimitry Andric       switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
83360b57cec5SDimitry Andric       case MachO::CPU_SUBTYPE_ARM64_32_V8:
83370b57cec5SDimitry Andric         outs() << "        V8";
83380b57cec5SDimitry Andric         break;
83390b57cec5SDimitry Andric       default:
83400b57cec5SDimitry Andric         outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
83410b57cec5SDimitry Andric         break;
83420b57cec5SDimitry Andric       }
83430b57cec5SDimitry Andric       break;
83440b57cec5SDimitry Andric     case MachO::CPU_TYPE_POWERPC:
83450b57cec5SDimitry Andric       outs() << "     PPC";
83460b57cec5SDimitry Andric       switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
83470b57cec5SDimitry Andric       case MachO::CPU_SUBTYPE_POWERPC_ALL:
83480b57cec5SDimitry Andric         outs() << "        ALL";
83490b57cec5SDimitry Andric         break;
83500b57cec5SDimitry Andric       default:
83510b57cec5SDimitry Andric         outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
83520b57cec5SDimitry Andric         break;
83530b57cec5SDimitry Andric       }
83540b57cec5SDimitry Andric       break;
83550b57cec5SDimitry Andric     case MachO::CPU_TYPE_POWERPC64:
83560b57cec5SDimitry Andric       outs() << "   PPC64";
83570b57cec5SDimitry Andric       switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
83580b57cec5SDimitry Andric       case MachO::CPU_SUBTYPE_POWERPC_ALL:
83590b57cec5SDimitry Andric         outs() << "        ALL";
83600b57cec5SDimitry Andric         break;
83610b57cec5SDimitry Andric       default:
83620b57cec5SDimitry Andric         outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
83630b57cec5SDimitry Andric         break;
83640b57cec5SDimitry Andric       }
83650b57cec5SDimitry Andric       break;
83660b57cec5SDimitry Andric     default:
83670b57cec5SDimitry Andric       outs() << format(" %7d", cputype);
83680b57cec5SDimitry Andric       outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
83690b57cec5SDimitry Andric       break;
83700b57cec5SDimitry Andric     }
83710b57cec5SDimitry Andric     if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
83720b57cec5SDimitry Andric       outs() << " LIB64";
83730b57cec5SDimitry Andric     } else {
83740b57cec5SDimitry Andric       outs() << format("  0x%02" PRIx32,
83750b57cec5SDimitry Andric                        (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
83760b57cec5SDimitry Andric     }
83770b57cec5SDimitry Andric     switch (filetype) {
83780b57cec5SDimitry Andric     case MachO::MH_OBJECT:
83790b57cec5SDimitry Andric       outs() << "      OBJECT";
83800b57cec5SDimitry Andric       break;
83810b57cec5SDimitry Andric     case MachO::MH_EXECUTE:
83820b57cec5SDimitry Andric       outs() << "     EXECUTE";
83830b57cec5SDimitry Andric       break;
83840b57cec5SDimitry Andric     case MachO::MH_FVMLIB:
83850b57cec5SDimitry Andric       outs() << "      FVMLIB";
83860b57cec5SDimitry Andric       break;
83870b57cec5SDimitry Andric     case MachO::MH_CORE:
83880b57cec5SDimitry Andric       outs() << "        CORE";
83890b57cec5SDimitry Andric       break;
83900b57cec5SDimitry Andric     case MachO::MH_PRELOAD:
83910b57cec5SDimitry Andric       outs() << "     PRELOAD";
83920b57cec5SDimitry Andric       break;
83930b57cec5SDimitry Andric     case MachO::MH_DYLIB:
83940b57cec5SDimitry Andric       outs() << "       DYLIB";
83950b57cec5SDimitry Andric       break;
83960b57cec5SDimitry Andric     case MachO::MH_DYLIB_STUB:
83970b57cec5SDimitry Andric       outs() << "  DYLIB_STUB";
83980b57cec5SDimitry Andric       break;
83990b57cec5SDimitry Andric     case MachO::MH_DYLINKER:
84000b57cec5SDimitry Andric       outs() << "    DYLINKER";
84010b57cec5SDimitry Andric       break;
84020b57cec5SDimitry Andric     case MachO::MH_BUNDLE:
84030b57cec5SDimitry Andric       outs() << "      BUNDLE";
84040b57cec5SDimitry Andric       break;
84050b57cec5SDimitry Andric     case MachO::MH_DSYM:
84060b57cec5SDimitry Andric       outs() << "        DSYM";
84070b57cec5SDimitry Andric       break;
84080b57cec5SDimitry Andric     case MachO::MH_KEXT_BUNDLE:
84090b57cec5SDimitry Andric       outs() << "  KEXTBUNDLE";
84100b57cec5SDimitry Andric       break;
8411bdd1243dSDimitry Andric     case MachO::MH_FILESET:
8412bdd1243dSDimitry Andric       outs() << "     FILESET";
8413bdd1243dSDimitry Andric       break;
84140b57cec5SDimitry Andric     default:
84150b57cec5SDimitry Andric       outs() << format("  %10u", filetype);
84160b57cec5SDimitry Andric       break;
84170b57cec5SDimitry Andric     }
84180b57cec5SDimitry Andric     outs() << format(" %5u", ncmds);
84190b57cec5SDimitry Andric     outs() << format(" %10u", sizeofcmds);
84200b57cec5SDimitry Andric     uint32_t f = flags;
84210b57cec5SDimitry Andric     if (f & MachO::MH_NOUNDEFS) {
84220b57cec5SDimitry Andric       outs() << "   NOUNDEFS";
84230b57cec5SDimitry Andric       f &= ~MachO::MH_NOUNDEFS;
84240b57cec5SDimitry Andric     }
84250b57cec5SDimitry Andric     if (f & MachO::MH_INCRLINK) {
84260b57cec5SDimitry Andric       outs() << " INCRLINK";
84270b57cec5SDimitry Andric       f &= ~MachO::MH_INCRLINK;
84280b57cec5SDimitry Andric     }
84290b57cec5SDimitry Andric     if (f & MachO::MH_DYLDLINK) {
84300b57cec5SDimitry Andric       outs() << " DYLDLINK";
84310b57cec5SDimitry Andric       f &= ~MachO::MH_DYLDLINK;
84320b57cec5SDimitry Andric     }
84330b57cec5SDimitry Andric     if (f & MachO::MH_BINDATLOAD) {
84340b57cec5SDimitry Andric       outs() << " BINDATLOAD";
84350b57cec5SDimitry Andric       f &= ~MachO::MH_BINDATLOAD;
84360b57cec5SDimitry Andric     }
84370b57cec5SDimitry Andric     if (f & MachO::MH_PREBOUND) {
84380b57cec5SDimitry Andric       outs() << " PREBOUND";
84390b57cec5SDimitry Andric       f &= ~MachO::MH_PREBOUND;
84400b57cec5SDimitry Andric     }
84410b57cec5SDimitry Andric     if (f & MachO::MH_SPLIT_SEGS) {
84420b57cec5SDimitry Andric       outs() << " SPLIT_SEGS";
84430b57cec5SDimitry Andric       f &= ~MachO::MH_SPLIT_SEGS;
84440b57cec5SDimitry Andric     }
84450b57cec5SDimitry Andric     if (f & MachO::MH_LAZY_INIT) {
84460b57cec5SDimitry Andric       outs() << " LAZY_INIT";
84470b57cec5SDimitry Andric       f &= ~MachO::MH_LAZY_INIT;
84480b57cec5SDimitry Andric     }
84490b57cec5SDimitry Andric     if (f & MachO::MH_TWOLEVEL) {
84500b57cec5SDimitry Andric       outs() << " TWOLEVEL";
84510b57cec5SDimitry Andric       f &= ~MachO::MH_TWOLEVEL;
84520b57cec5SDimitry Andric     }
84530b57cec5SDimitry Andric     if (f & MachO::MH_FORCE_FLAT) {
84540b57cec5SDimitry Andric       outs() << " FORCE_FLAT";
84550b57cec5SDimitry Andric       f &= ~MachO::MH_FORCE_FLAT;
84560b57cec5SDimitry Andric     }
84570b57cec5SDimitry Andric     if (f & MachO::MH_NOMULTIDEFS) {
84580b57cec5SDimitry Andric       outs() << " NOMULTIDEFS";
84590b57cec5SDimitry Andric       f &= ~MachO::MH_NOMULTIDEFS;
84600b57cec5SDimitry Andric     }
84610b57cec5SDimitry Andric     if (f & MachO::MH_NOFIXPREBINDING) {
84620b57cec5SDimitry Andric       outs() << " NOFIXPREBINDING";
84630b57cec5SDimitry Andric       f &= ~MachO::MH_NOFIXPREBINDING;
84640b57cec5SDimitry Andric     }
84650b57cec5SDimitry Andric     if (f & MachO::MH_PREBINDABLE) {
84660b57cec5SDimitry Andric       outs() << " PREBINDABLE";
84670b57cec5SDimitry Andric       f &= ~MachO::MH_PREBINDABLE;
84680b57cec5SDimitry Andric     }
84690b57cec5SDimitry Andric     if (f & MachO::MH_ALLMODSBOUND) {
84700b57cec5SDimitry Andric       outs() << " ALLMODSBOUND";
84710b57cec5SDimitry Andric       f &= ~MachO::MH_ALLMODSBOUND;
84720b57cec5SDimitry Andric     }
84730b57cec5SDimitry Andric     if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
84740b57cec5SDimitry Andric       outs() << " SUBSECTIONS_VIA_SYMBOLS";
84750b57cec5SDimitry Andric       f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
84760b57cec5SDimitry Andric     }
84770b57cec5SDimitry Andric     if (f & MachO::MH_CANONICAL) {
84780b57cec5SDimitry Andric       outs() << " CANONICAL";
84790b57cec5SDimitry Andric       f &= ~MachO::MH_CANONICAL;
84800b57cec5SDimitry Andric     }
84810b57cec5SDimitry Andric     if (f & MachO::MH_WEAK_DEFINES) {
84820b57cec5SDimitry Andric       outs() << " WEAK_DEFINES";
84830b57cec5SDimitry Andric       f &= ~MachO::MH_WEAK_DEFINES;
84840b57cec5SDimitry Andric     }
84850b57cec5SDimitry Andric     if (f & MachO::MH_BINDS_TO_WEAK) {
84860b57cec5SDimitry Andric       outs() << " BINDS_TO_WEAK";
84870b57cec5SDimitry Andric       f &= ~MachO::MH_BINDS_TO_WEAK;
84880b57cec5SDimitry Andric     }
84890b57cec5SDimitry Andric     if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
84900b57cec5SDimitry Andric       outs() << " ALLOW_STACK_EXECUTION";
84910b57cec5SDimitry Andric       f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
84920b57cec5SDimitry Andric     }
84930b57cec5SDimitry Andric     if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
84940b57cec5SDimitry Andric       outs() << " DEAD_STRIPPABLE_DYLIB";
84950b57cec5SDimitry Andric       f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
84960b57cec5SDimitry Andric     }
84970b57cec5SDimitry Andric     if (f & MachO::MH_PIE) {
84980b57cec5SDimitry Andric       outs() << " PIE";
84990b57cec5SDimitry Andric       f &= ~MachO::MH_PIE;
85000b57cec5SDimitry Andric     }
85010b57cec5SDimitry Andric     if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
85020b57cec5SDimitry Andric       outs() << " NO_REEXPORTED_DYLIBS";
85030b57cec5SDimitry Andric       f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
85040b57cec5SDimitry Andric     }
85050b57cec5SDimitry Andric     if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
85060b57cec5SDimitry Andric       outs() << " MH_HAS_TLV_DESCRIPTORS";
85070b57cec5SDimitry Andric       f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
85080b57cec5SDimitry Andric     }
85090b57cec5SDimitry Andric     if (f & MachO::MH_NO_HEAP_EXECUTION) {
85100b57cec5SDimitry Andric       outs() << " MH_NO_HEAP_EXECUTION";
85110b57cec5SDimitry Andric       f &= ~MachO::MH_NO_HEAP_EXECUTION;
85120b57cec5SDimitry Andric     }
85130b57cec5SDimitry Andric     if (f & MachO::MH_APP_EXTENSION_SAFE) {
85140b57cec5SDimitry Andric       outs() << " APP_EXTENSION_SAFE";
85150b57cec5SDimitry Andric       f &= ~MachO::MH_APP_EXTENSION_SAFE;
85160b57cec5SDimitry Andric     }
85170b57cec5SDimitry Andric     if (f & MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO) {
85180b57cec5SDimitry Andric       outs() << " NLIST_OUTOFSYNC_WITH_DYLDINFO";
85190b57cec5SDimitry Andric       f &= ~MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO;
85200b57cec5SDimitry Andric     }
85210b57cec5SDimitry Andric     if (f != 0 || flags == 0)
85220b57cec5SDimitry Andric       outs() << format(" 0x%08" PRIx32, f);
85230b57cec5SDimitry Andric   } else {
85240b57cec5SDimitry Andric     outs() << format(" 0x%08" PRIx32, magic);
85250b57cec5SDimitry Andric     outs() << format(" %7d", cputype);
85260b57cec5SDimitry Andric     outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
85270b57cec5SDimitry Andric     outs() << format("  0x%02" PRIx32,
85280b57cec5SDimitry Andric                      (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
85290b57cec5SDimitry Andric     outs() << format("  %10u", filetype);
85300b57cec5SDimitry Andric     outs() << format(" %5u", ncmds);
85310b57cec5SDimitry Andric     outs() << format(" %10u", sizeofcmds);
85320b57cec5SDimitry Andric     outs() << format(" 0x%08" PRIx32, flags);
85330b57cec5SDimitry Andric   }
85340b57cec5SDimitry Andric   outs() << "\n";
85350b57cec5SDimitry Andric }
85360b57cec5SDimitry Andric 
PrintSegmentCommand(uint32_t cmd,uint32_t cmdsize,StringRef SegName,uint64_t vmaddr,uint64_t vmsize,uint64_t fileoff,uint64_t filesize,uint32_t maxprot,uint32_t initprot,uint32_t nsects,uint32_t flags,uint32_t object_size,bool verbose)85370b57cec5SDimitry Andric static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
85380b57cec5SDimitry Andric                                 StringRef SegName, uint64_t vmaddr,
85390b57cec5SDimitry Andric                                 uint64_t vmsize, uint64_t fileoff,
85400b57cec5SDimitry Andric                                 uint64_t filesize, uint32_t maxprot,
85410b57cec5SDimitry Andric                                 uint32_t initprot, uint32_t nsects,
85420b57cec5SDimitry Andric                                 uint32_t flags, uint32_t object_size,
85430b57cec5SDimitry Andric                                 bool verbose) {
85440b57cec5SDimitry Andric   uint64_t expected_cmdsize;
85450b57cec5SDimitry Andric   if (cmd == MachO::LC_SEGMENT) {
85460b57cec5SDimitry Andric     outs() << "      cmd LC_SEGMENT\n";
85470b57cec5SDimitry Andric     expected_cmdsize = nsects;
85480b57cec5SDimitry Andric     expected_cmdsize *= sizeof(struct MachO::section);
85490b57cec5SDimitry Andric     expected_cmdsize += sizeof(struct MachO::segment_command);
85500b57cec5SDimitry Andric   } else {
85510b57cec5SDimitry Andric     outs() << "      cmd LC_SEGMENT_64\n";
85520b57cec5SDimitry Andric     expected_cmdsize = nsects;
85530b57cec5SDimitry Andric     expected_cmdsize *= sizeof(struct MachO::section_64);
85540b57cec5SDimitry Andric     expected_cmdsize += sizeof(struct MachO::segment_command_64);
85550b57cec5SDimitry Andric   }
85560b57cec5SDimitry Andric   outs() << "  cmdsize " << cmdsize;
85570b57cec5SDimitry Andric   if (cmdsize != expected_cmdsize)
85580b57cec5SDimitry Andric     outs() << " Inconsistent size\n";
85590b57cec5SDimitry Andric   else
85600b57cec5SDimitry Andric     outs() << "\n";
85610b57cec5SDimitry Andric   outs() << "  segname " << SegName << "\n";
85620b57cec5SDimitry Andric   if (cmd == MachO::LC_SEGMENT_64) {
85630b57cec5SDimitry Andric     outs() << "   vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
85640b57cec5SDimitry Andric     outs() << "   vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
85650b57cec5SDimitry Andric   } else {
85660b57cec5SDimitry Andric     outs() << "   vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
85670b57cec5SDimitry Andric     outs() << "   vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
85680b57cec5SDimitry Andric   }
85690b57cec5SDimitry Andric   outs() << "  fileoff " << fileoff;
85700b57cec5SDimitry Andric   if (fileoff > object_size)
85710b57cec5SDimitry Andric     outs() << " (past end of file)\n";
85720b57cec5SDimitry Andric   else
85730b57cec5SDimitry Andric     outs() << "\n";
85740b57cec5SDimitry Andric   outs() << " filesize " << filesize;
85750b57cec5SDimitry Andric   if (fileoff + filesize > object_size)
85760b57cec5SDimitry Andric     outs() << " (past end of file)\n";
85770b57cec5SDimitry Andric   else
85780b57cec5SDimitry Andric     outs() << "\n";
85790b57cec5SDimitry Andric   if (verbose) {
85800b57cec5SDimitry Andric     if ((maxprot &
85810b57cec5SDimitry Andric          ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
85820b57cec5SDimitry Andric            MachO::VM_PROT_EXECUTE)) != 0)
85830b57cec5SDimitry Andric       outs() << "  maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
85840b57cec5SDimitry Andric     else {
85850b57cec5SDimitry Andric       outs() << "  maxprot ";
85860b57cec5SDimitry Andric       outs() << ((maxprot & MachO::VM_PROT_READ) ? "r" : "-");
85870b57cec5SDimitry Andric       outs() << ((maxprot & MachO::VM_PROT_WRITE) ? "w" : "-");
85880b57cec5SDimitry Andric       outs() << ((maxprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
85890b57cec5SDimitry Andric     }
85900b57cec5SDimitry Andric     if ((initprot &
85910b57cec5SDimitry Andric          ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
85920b57cec5SDimitry Andric            MachO::VM_PROT_EXECUTE)) != 0)
85930b57cec5SDimitry Andric       outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
85940b57cec5SDimitry Andric     else {
85950b57cec5SDimitry Andric       outs() << " initprot ";
85960b57cec5SDimitry Andric       outs() << ((initprot & MachO::VM_PROT_READ) ? "r" : "-");
85970b57cec5SDimitry Andric       outs() << ((initprot & MachO::VM_PROT_WRITE) ? "w" : "-");
85980b57cec5SDimitry Andric       outs() << ((initprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
85990b57cec5SDimitry Andric     }
86000b57cec5SDimitry Andric   } else {
86010b57cec5SDimitry Andric     outs() << "  maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
86020b57cec5SDimitry Andric     outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
86030b57cec5SDimitry Andric   }
86040b57cec5SDimitry Andric   outs() << "   nsects " << nsects << "\n";
86050b57cec5SDimitry Andric   if (verbose) {
86060b57cec5SDimitry Andric     outs() << "    flags";
86070b57cec5SDimitry Andric     if (flags == 0)
86080b57cec5SDimitry Andric       outs() << " (none)\n";
86090b57cec5SDimitry Andric     else {
86100b57cec5SDimitry Andric       if (flags & MachO::SG_HIGHVM) {
86110b57cec5SDimitry Andric         outs() << " HIGHVM";
86120b57cec5SDimitry Andric         flags &= ~MachO::SG_HIGHVM;
86130b57cec5SDimitry Andric       }
86140b57cec5SDimitry Andric       if (flags & MachO::SG_FVMLIB) {
86150b57cec5SDimitry Andric         outs() << " FVMLIB";
86160b57cec5SDimitry Andric         flags &= ~MachO::SG_FVMLIB;
86170b57cec5SDimitry Andric       }
86180b57cec5SDimitry Andric       if (flags & MachO::SG_NORELOC) {
86190b57cec5SDimitry Andric         outs() << " NORELOC";
86200b57cec5SDimitry Andric         flags &= ~MachO::SG_NORELOC;
86210b57cec5SDimitry Andric       }
86220b57cec5SDimitry Andric       if (flags & MachO::SG_PROTECTED_VERSION_1) {
86230b57cec5SDimitry Andric         outs() << " PROTECTED_VERSION_1";
86240b57cec5SDimitry Andric         flags &= ~MachO::SG_PROTECTED_VERSION_1;
86250b57cec5SDimitry Andric       }
8626bdd1243dSDimitry Andric       if (flags & MachO::SG_READ_ONLY) {
8627bdd1243dSDimitry Andric         // Apple's otool prints the SG_ prefix for this flag, but not for the
8628bdd1243dSDimitry Andric         // others.
8629bdd1243dSDimitry Andric         outs() << " SG_READ_ONLY";
8630bdd1243dSDimitry Andric         flags &= ~MachO::SG_READ_ONLY;
8631bdd1243dSDimitry Andric       }
86320b57cec5SDimitry Andric       if (flags)
86330b57cec5SDimitry Andric         outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
86340b57cec5SDimitry Andric       else
86350b57cec5SDimitry Andric         outs() << "\n";
86360b57cec5SDimitry Andric     }
86370b57cec5SDimitry Andric   } else {
86380b57cec5SDimitry Andric     outs() << "    flags " << format("0x%" PRIx32, flags) << "\n";
86390b57cec5SDimitry Andric   }
86400b57cec5SDimitry Andric }
86410b57cec5SDimitry Andric 
PrintSection(const char * sectname,const char * segname,uint64_t addr,uint64_t size,uint32_t offset,uint32_t align,uint32_t reloff,uint32_t nreloc,uint32_t flags,uint32_t reserved1,uint32_t reserved2,uint32_t cmd,const char * sg_segname,uint32_t filetype,uint32_t object_size,bool verbose)86420b57cec5SDimitry Andric static void PrintSection(const char *sectname, const char *segname,
86430b57cec5SDimitry Andric                          uint64_t addr, uint64_t size, uint32_t offset,
86440b57cec5SDimitry Andric                          uint32_t align, uint32_t reloff, uint32_t nreloc,
86450b57cec5SDimitry Andric                          uint32_t flags, uint32_t reserved1, uint32_t reserved2,
86460b57cec5SDimitry Andric                          uint32_t cmd, const char *sg_segname,
86470b57cec5SDimitry Andric                          uint32_t filetype, uint32_t object_size,
86480b57cec5SDimitry Andric                          bool verbose) {
86490b57cec5SDimitry Andric   outs() << "Section\n";
86500b57cec5SDimitry Andric   outs() << "  sectname " << format("%.16s\n", sectname);
86510b57cec5SDimitry Andric   outs() << "   segname " << format("%.16s", segname);
86520b57cec5SDimitry Andric   if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
86530b57cec5SDimitry Andric     outs() << " (does not match segment)\n";
86540b57cec5SDimitry Andric   else
86550b57cec5SDimitry Andric     outs() << "\n";
86560b57cec5SDimitry Andric   if (cmd == MachO::LC_SEGMENT_64) {
86570b57cec5SDimitry Andric     outs() << "      addr " << format("0x%016" PRIx64, addr) << "\n";
86580b57cec5SDimitry Andric     outs() << "      size " << format("0x%016" PRIx64, size);
86590b57cec5SDimitry Andric   } else {
86600b57cec5SDimitry Andric     outs() << "      addr " << format("0x%08" PRIx64, addr) << "\n";
86610b57cec5SDimitry Andric     outs() << "      size " << format("0x%08" PRIx64, size);
86620b57cec5SDimitry Andric   }
86630b57cec5SDimitry Andric   if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
86640b57cec5SDimitry Andric     outs() << " (past end of file)\n";
86650b57cec5SDimitry Andric   else
86660b57cec5SDimitry Andric     outs() << "\n";
86670b57cec5SDimitry Andric   outs() << "    offset " << offset;
86680b57cec5SDimitry Andric   if (offset > object_size)
86690b57cec5SDimitry Andric     outs() << " (past end of file)\n";
86700b57cec5SDimitry Andric   else
86710b57cec5SDimitry Andric     outs() << "\n";
86720b57cec5SDimitry Andric   uint32_t align_shifted = 1 << align;
86730b57cec5SDimitry Andric   outs() << "     align 2^" << align << " (" << align_shifted << ")\n";
86740b57cec5SDimitry Andric   outs() << "    reloff " << reloff;
86750b57cec5SDimitry Andric   if (reloff > object_size)
86760b57cec5SDimitry Andric     outs() << " (past end of file)\n";
86770b57cec5SDimitry Andric   else
86780b57cec5SDimitry Andric     outs() << "\n";
86790b57cec5SDimitry Andric   outs() << "    nreloc " << nreloc;
86800b57cec5SDimitry Andric   if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
86810b57cec5SDimitry Andric     outs() << " (past end of file)\n";
86820b57cec5SDimitry Andric   else
86830b57cec5SDimitry Andric     outs() << "\n";
86840b57cec5SDimitry Andric   uint32_t section_type = flags & MachO::SECTION_TYPE;
86850b57cec5SDimitry Andric   if (verbose) {
86860b57cec5SDimitry Andric     outs() << "      type";
86870b57cec5SDimitry Andric     if (section_type == MachO::S_REGULAR)
86880b57cec5SDimitry Andric       outs() << " S_REGULAR\n";
86890b57cec5SDimitry Andric     else if (section_type == MachO::S_ZEROFILL)
86900b57cec5SDimitry Andric       outs() << " S_ZEROFILL\n";
86910b57cec5SDimitry Andric     else if (section_type == MachO::S_CSTRING_LITERALS)
86920b57cec5SDimitry Andric       outs() << " S_CSTRING_LITERALS\n";
86930b57cec5SDimitry Andric     else if (section_type == MachO::S_4BYTE_LITERALS)
86940b57cec5SDimitry Andric       outs() << " S_4BYTE_LITERALS\n";
86950b57cec5SDimitry Andric     else if (section_type == MachO::S_8BYTE_LITERALS)
86960b57cec5SDimitry Andric       outs() << " S_8BYTE_LITERALS\n";
86970b57cec5SDimitry Andric     else if (section_type == MachO::S_16BYTE_LITERALS)
86980b57cec5SDimitry Andric       outs() << " S_16BYTE_LITERALS\n";
86990b57cec5SDimitry Andric     else if (section_type == MachO::S_LITERAL_POINTERS)
87000b57cec5SDimitry Andric       outs() << " S_LITERAL_POINTERS\n";
87010b57cec5SDimitry Andric     else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
87020b57cec5SDimitry Andric       outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
87030b57cec5SDimitry Andric     else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
87040b57cec5SDimitry Andric       outs() << " S_LAZY_SYMBOL_POINTERS\n";
87050b57cec5SDimitry Andric     else if (section_type == MachO::S_SYMBOL_STUBS)
87060b57cec5SDimitry Andric       outs() << " S_SYMBOL_STUBS\n";
87070b57cec5SDimitry Andric     else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
87080b57cec5SDimitry Andric       outs() << " S_MOD_INIT_FUNC_POINTERS\n";
87090b57cec5SDimitry Andric     else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
87100b57cec5SDimitry Andric       outs() << " S_MOD_TERM_FUNC_POINTERS\n";
87110b57cec5SDimitry Andric     else if (section_type == MachO::S_COALESCED)
87120b57cec5SDimitry Andric       outs() << " S_COALESCED\n";
87130b57cec5SDimitry Andric     else if (section_type == MachO::S_INTERPOSING)
87140b57cec5SDimitry Andric       outs() << " S_INTERPOSING\n";
87150b57cec5SDimitry Andric     else if (section_type == MachO::S_DTRACE_DOF)
87160b57cec5SDimitry Andric       outs() << " S_DTRACE_DOF\n";
87170b57cec5SDimitry Andric     else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
87180b57cec5SDimitry Andric       outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
87190b57cec5SDimitry Andric     else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
87200b57cec5SDimitry Andric       outs() << " S_THREAD_LOCAL_REGULAR\n";
87210b57cec5SDimitry Andric     else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
87220b57cec5SDimitry Andric       outs() << " S_THREAD_LOCAL_ZEROFILL\n";
87230b57cec5SDimitry Andric     else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
87240b57cec5SDimitry Andric       outs() << " S_THREAD_LOCAL_VARIABLES\n";
87250b57cec5SDimitry Andric     else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
87260b57cec5SDimitry Andric       outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
87270b57cec5SDimitry Andric     else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
87280b57cec5SDimitry Andric       outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
8729bdd1243dSDimitry Andric     else if (section_type == MachO::S_INIT_FUNC_OFFSETS)
8730bdd1243dSDimitry Andric       outs() << " S_INIT_FUNC_OFFSETS\n";
87310b57cec5SDimitry Andric     else
87320b57cec5SDimitry Andric       outs() << format("0x%08" PRIx32, section_type) << "\n";
87330b57cec5SDimitry Andric     outs() << "attributes";
87340b57cec5SDimitry Andric     uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
87350b57cec5SDimitry Andric     if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
87360b57cec5SDimitry Andric       outs() << " PURE_INSTRUCTIONS";
87370b57cec5SDimitry Andric     if (section_attributes & MachO::S_ATTR_NO_TOC)
87380b57cec5SDimitry Andric       outs() << " NO_TOC";
87390b57cec5SDimitry Andric     if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
87400b57cec5SDimitry Andric       outs() << " STRIP_STATIC_SYMS";
87410b57cec5SDimitry Andric     if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
87420b57cec5SDimitry Andric       outs() << " NO_DEAD_STRIP";
87430b57cec5SDimitry Andric     if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
87440b57cec5SDimitry Andric       outs() << " LIVE_SUPPORT";
87450b57cec5SDimitry Andric     if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
87460b57cec5SDimitry Andric       outs() << " SELF_MODIFYING_CODE";
87470b57cec5SDimitry Andric     if (section_attributes & MachO::S_ATTR_DEBUG)
87480b57cec5SDimitry Andric       outs() << " DEBUG";
87490b57cec5SDimitry Andric     if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
87500b57cec5SDimitry Andric       outs() << " SOME_INSTRUCTIONS";
87510b57cec5SDimitry Andric     if (section_attributes & MachO::S_ATTR_EXT_RELOC)
87520b57cec5SDimitry Andric       outs() << " EXT_RELOC";
87530b57cec5SDimitry Andric     if (section_attributes & MachO::S_ATTR_LOC_RELOC)
87540b57cec5SDimitry Andric       outs() << " LOC_RELOC";
87550b57cec5SDimitry Andric     if (section_attributes == 0)
87560b57cec5SDimitry Andric       outs() << " (none)";
87570b57cec5SDimitry Andric     outs() << "\n";
87580b57cec5SDimitry Andric   } else
87590b57cec5SDimitry Andric     outs() << "     flags " << format("0x%08" PRIx32, flags) << "\n";
87600b57cec5SDimitry Andric   outs() << " reserved1 " << reserved1;
87610b57cec5SDimitry Andric   if (section_type == MachO::S_SYMBOL_STUBS ||
87620b57cec5SDimitry Andric       section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
87630b57cec5SDimitry Andric       section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
87640b57cec5SDimitry Andric       section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
87650b57cec5SDimitry Andric       section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
87660b57cec5SDimitry Andric     outs() << " (index into indirect symbol table)\n";
87670b57cec5SDimitry Andric   else
87680b57cec5SDimitry Andric     outs() << "\n";
87690b57cec5SDimitry Andric   outs() << " reserved2 " << reserved2;
87700b57cec5SDimitry Andric   if (section_type == MachO::S_SYMBOL_STUBS)
87710b57cec5SDimitry Andric     outs() << " (size of stubs)\n";
87720b57cec5SDimitry Andric   else
87730b57cec5SDimitry Andric     outs() << "\n";
87740b57cec5SDimitry Andric }
87750b57cec5SDimitry Andric 
PrintSymtabLoadCommand(MachO::symtab_command st,bool Is64Bit,uint32_t object_size)87760b57cec5SDimitry Andric static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
87770b57cec5SDimitry Andric                                    uint32_t object_size) {
87780b57cec5SDimitry Andric   outs() << "     cmd LC_SYMTAB\n";
87790b57cec5SDimitry Andric   outs() << " cmdsize " << st.cmdsize;
87800b57cec5SDimitry Andric   if (st.cmdsize != sizeof(struct MachO::symtab_command))
87810b57cec5SDimitry Andric     outs() << " Incorrect size\n";
87820b57cec5SDimitry Andric   else
87830b57cec5SDimitry Andric     outs() << "\n";
87840b57cec5SDimitry Andric   outs() << "  symoff " << st.symoff;
87850b57cec5SDimitry Andric   if (st.symoff > object_size)
87860b57cec5SDimitry Andric     outs() << " (past end of file)\n";
87870b57cec5SDimitry Andric   else
87880b57cec5SDimitry Andric     outs() << "\n";
87890b57cec5SDimitry Andric   outs() << "   nsyms " << st.nsyms;
87900b57cec5SDimitry Andric   uint64_t big_size;
87910b57cec5SDimitry Andric   if (Is64Bit) {
87920b57cec5SDimitry Andric     big_size = st.nsyms;
87930b57cec5SDimitry Andric     big_size *= sizeof(struct MachO::nlist_64);
87940b57cec5SDimitry Andric     big_size += st.symoff;
87950b57cec5SDimitry Andric     if (big_size > object_size)
87960b57cec5SDimitry Andric       outs() << " (past end of file)\n";
87970b57cec5SDimitry Andric     else
87980b57cec5SDimitry Andric       outs() << "\n";
87990b57cec5SDimitry Andric   } else {
88000b57cec5SDimitry Andric     big_size = st.nsyms;
88010b57cec5SDimitry Andric     big_size *= sizeof(struct MachO::nlist);
88020b57cec5SDimitry Andric     big_size += st.symoff;
88030b57cec5SDimitry Andric     if (big_size > object_size)
88040b57cec5SDimitry Andric       outs() << " (past end of file)\n";
88050b57cec5SDimitry Andric     else
88060b57cec5SDimitry Andric       outs() << "\n";
88070b57cec5SDimitry Andric   }
88080b57cec5SDimitry Andric   outs() << "  stroff " << st.stroff;
88090b57cec5SDimitry Andric   if (st.stroff > object_size)
88100b57cec5SDimitry Andric     outs() << " (past end of file)\n";
88110b57cec5SDimitry Andric   else
88120b57cec5SDimitry Andric     outs() << "\n";
88130b57cec5SDimitry Andric   outs() << " strsize " << st.strsize;
88140b57cec5SDimitry Andric   big_size = st.stroff;
88150b57cec5SDimitry Andric   big_size += st.strsize;
88160b57cec5SDimitry Andric   if (big_size > object_size)
88170b57cec5SDimitry Andric     outs() << " (past end of file)\n";
88180b57cec5SDimitry Andric   else
88190b57cec5SDimitry Andric     outs() << "\n";
88200b57cec5SDimitry Andric }
88210b57cec5SDimitry Andric 
PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,uint32_t nsyms,uint32_t object_size,bool Is64Bit)88220b57cec5SDimitry Andric static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
88230b57cec5SDimitry Andric                                      uint32_t nsyms, uint32_t object_size,
88240b57cec5SDimitry Andric                                      bool Is64Bit) {
88250b57cec5SDimitry Andric   outs() << "            cmd LC_DYSYMTAB\n";
88260b57cec5SDimitry Andric   outs() << "        cmdsize " << dyst.cmdsize;
88270b57cec5SDimitry Andric   if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
88280b57cec5SDimitry Andric     outs() << " Incorrect size\n";
88290b57cec5SDimitry Andric   else
88300b57cec5SDimitry Andric     outs() << "\n";
88310b57cec5SDimitry Andric   outs() << "      ilocalsym " << dyst.ilocalsym;
88320b57cec5SDimitry Andric   if (dyst.ilocalsym > nsyms)
88330b57cec5SDimitry Andric     outs() << " (greater than the number of symbols)\n";
88340b57cec5SDimitry Andric   else
88350b57cec5SDimitry Andric     outs() << "\n";
88360b57cec5SDimitry Andric   outs() << "      nlocalsym " << dyst.nlocalsym;
88370b57cec5SDimitry Andric   uint64_t big_size;
88380b57cec5SDimitry Andric   big_size = dyst.ilocalsym;
88390b57cec5SDimitry Andric   big_size += dyst.nlocalsym;
88400b57cec5SDimitry Andric   if (big_size > nsyms)
88410b57cec5SDimitry Andric     outs() << " (past the end of the symbol table)\n";
88420b57cec5SDimitry Andric   else
88430b57cec5SDimitry Andric     outs() << "\n";
88440b57cec5SDimitry Andric   outs() << "     iextdefsym " << dyst.iextdefsym;
88450b57cec5SDimitry Andric   if (dyst.iextdefsym > nsyms)
88460b57cec5SDimitry Andric     outs() << " (greater than the number of symbols)\n";
88470b57cec5SDimitry Andric   else
88480b57cec5SDimitry Andric     outs() << "\n";
88490b57cec5SDimitry Andric   outs() << "     nextdefsym " << dyst.nextdefsym;
88500b57cec5SDimitry Andric   big_size = dyst.iextdefsym;
88510b57cec5SDimitry Andric   big_size += dyst.nextdefsym;
88520b57cec5SDimitry Andric   if (big_size > nsyms)
88530b57cec5SDimitry Andric     outs() << " (past the end of the symbol table)\n";
88540b57cec5SDimitry Andric   else
88550b57cec5SDimitry Andric     outs() << "\n";
88560b57cec5SDimitry Andric   outs() << "      iundefsym " << dyst.iundefsym;
88570b57cec5SDimitry Andric   if (dyst.iundefsym > nsyms)
88580b57cec5SDimitry Andric     outs() << " (greater than the number of symbols)\n";
88590b57cec5SDimitry Andric   else
88600b57cec5SDimitry Andric     outs() << "\n";
88610b57cec5SDimitry Andric   outs() << "      nundefsym " << dyst.nundefsym;
88620b57cec5SDimitry Andric   big_size = dyst.iundefsym;
88630b57cec5SDimitry Andric   big_size += dyst.nundefsym;
88640b57cec5SDimitry Andric   if (big_size > nsyms)
88650b57cec5SDimitry Andric     outs() << " (past the end of the symbol table)\n";
88660b57cec5SDimitry Andric   else
88670b57cec5SDimitry Andric     outs() << "\n";
88680b57cec5SDimitry Andric   outs() << "         tocoff " << dyst.tocoff;
88690b57cec5SDimitry Andric   if (dyst.tocoff > object_size)
88700b57cec5SDimitry Andric     outs() << " (past end of file)\n";
88710b57cec5SDimitry Andric   else
88720b57cec5SDimitry Andric     outs() << "\n";
88730b57cec5SDimitry Andric   outs() << "           ntoc " << dyst.ntoc;
88740b57cec5SDimitry Andric   big_size = dyst.ntoc;
88750b57cec5SDimitry Andric   big_size *= sizeof(struct MachO::dylib_table_of_contents);
88760b57cec5SDimitry Andric   big_size += dyst.tocoff;
88770b57cec5SDimitry Andric   if (big_size > object_size)
88780b57cec5SDimitry Andric     outs() << " (past end of file)\n";
88790b57cec5SDimitry Andric   else
88800b57cec5SDimitry Andric     outs() << "\n";
88810b57cec5SDimitry Andric   outs() << "      modtaboff " << dyst.modtaboff;
88820b57cec5SDimitry Andric   if (dyst.modtaboff > object_size)
88830b57cec5SDimitry Andric     outs() << " (past end of file)\n";
88840b57cec5SDimitry Andric   else
88850b57cec5SDimitry Andric     outs() << "\n";
88860b57cec5SDimitry Andric   outs() << "        nmodtab " << dyst.nmodtab;
88870b57cec5SDimitry Andric   uint64_t modtabend;
88880b57cec5SDimitry Andric   if (Is64Bit) {
88890b57cec5SDimitry Andric     modtabend = dyst.nmodtab;
88900b57cec5SDimitry Andric     modtabend *= sizeof(struct MachO::dylib_module_64);
88910b57cec5SDimitry Andric     modtabend += dyst.modtaboff;
88920b57cec5SDimitry Andric   } else {
88930b57cec5SDimitry Andric     modtabend = dyst.nmodtab;
88940b57cec5SDimitry Andric     modtabend *= sizeof(struct MachO::dylib_module);
88950b57cec5SDimitry Andric     modtabend += dyst.modtaboff;
88960b57cec5SDimitry Andric   }
88970b57cec5SDimitry Andric   if (modtabend > object_size)
88980b57cec5SDimitry Andric     outs() << " (past end of file)\n";
88990b57cec5SDimitry Andric   else
89000b57cec5SDimitry Andric     outs() << "\n";
89010b57cec5SDimitry Andric   outs() << "   extrefsymoff " << dyst.extrefsymoff;
89020b57cec5SDimitry Andric   if (dyst.extrefsymoff > object_size)
89030b57cec5SDimitry Andric     outs() << " (past end of file)\n";
89040b57cec5SDimitry Andric   else
89050b57cec5SDimitry Andric     outs() << "\n";
89060b57cec5SDimitry Andric   outs() << "    nextrefsyms " << dyst.nextrefsyms;
89070b57cec5SDimitry Andric   big_size = dyst.nextrefsyms;
89080b57cec5SDimitry Andric   big_size *= sizeof(struct MachO::dylib_reference);
89090b57cec5SDimitry Andric   big_size += dyst.extrefsymoff;
89100b57cec5SDimitry Andric   if (big_size > object_size)
89110b57cec5SDimitry Andric     outs() << " (past end of file)\n";
89120b57cec5SDimitry Andric   else
89130b57cec5SDimitry Andric     outs() << "\n";
89140b57cec5SDimitry Andric   outs() << " indirectsymoff " << dyst.indirectsymoff;
89150b57cec5SDimitry Andric   if (dyst.indirectsymoff > object_size)
89160b57cec5SDimitry Andric     outs() << " (past end of file)\n";
89170b57cec5SDimitry Andric   else
89180b57cec5SDimitry Andric     outs() << "\n";
89190b57cec5SDimitry Andric   outs() << "  nindirectsyms " << dyst.nindirectsyms;
89200b57cec5SDimitry Andric   big_size = dyst.nindirectsyms;
89210b57cec5SDimitry Andric   big_size *= sizeof(uint32_t);
89220b57cec5SDimitry Andric   big_size += dyst.indirectsymoff;
89230b57cec5SDimitry Andric   if (big_size > object_size)
89240b57cec5SDimitry Andric     outs() << " (past end of file)\n";
89250b57cec5SDimitry Andric   else
89260b57cec5SDimitry Andric     outs() << "\n";
89270b57cec5SDimitry Andric   outs() << "      extreloff " << dyst.extreloff;
89280b57cec5SDimitry Andric   if (dyst.extreloff > object_size)
89290b57cec5SDimitry Andric     outs() << " (past end of file)\n";
89300b57cec5SDimitry Andric   else
89310b57cec5SDimitry Andric     outs() << "\n";
89320b57cec5SDimitry Andric   outs() << "        nextrel " << dyst.nextrel;
89330b57cec5SDimitry Andric   big_size = dyst.nextrel;
89340b57cec5SDimitry Andric   big_size *= sizeof(struct MachO::relocation_info);
89350b57cec5SDimitry Andric   big_size += dyst.extreloff;
89360b57cec5SDimitry Andric   if (big_size > object_size)
89370b57cec5SDimitry Andric     outs() << " (past end of file)\n";
89380b57cec5SDimitry Andric   else
89390b57cec5SDimitry Andric     outs() << "\n";
89400b57cec5SDimitry Andric   outs() << "      locreloff " << dyst.locreloff;
89410b57cec5SDimitry Andric   if (dyst.locreloff > object_size)
89420b57cec5SDimitry Andric     outs() << " (past end of file)\n";
89430b57cec5SDimitry Andric   else
89440b57cec5SDimitry Andric     outs() << "\n";
89450b57cec5SDimitry Andric   outs() << "        nlocrel " << dyst.nlocrel;
89460b57cec5SDimitry Andric   big_size = dyst.nlocrel;
89470b57cec5SDimitry Andric   big_size *= sizeof(struct MachO::relocation_info);
89480b57cec5SDimitry Andric   big_size += dyst.locreloff;
89490b57cec5SDimitry Andric   if (big_size > object_size)
89500b57cec5SDimitry Andric     outs() << " (past end of file)\n";
89510b57cec5SDimitry Andric   else
89520b57cec5SDimitry Andric     outs() << "\n";
89530b57cec5SDimitry Andric }
89540b57cec5SDimitry Andric 
PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,uint32_t object_size)89550b57cec5SDimitry Andric static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
89560b57cec5SDimitry Andric                                      uint32_t object_size) {
89570b57cec5SDimitry Andric   if (dc.cmd == MachO::LC_DYLD_INFO)
89580b57cec5SDimitry Andric     outs() << "            cmd LC_DYLD_INFO\n";
89590b57cec5SDimitry Andric   else
89600b57cec5SDimitry Andric     outs() << "            cmd LC_DYLD_INFO_ONLY\n";
89610b57cec5SDimitry Andric   outs() << "        cmdsize " << dc.cmdsize;
89620b57cec5SDimitry Andric   if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
89630b57cec5SDimitry Andric     outs() << " Incorrect size\n";
89640b57cec5SDimitry Andric   else
89650b57cec5SDimitry Andric     outs() << "\n";
89660b57cec5SDimitry Andric   outs() << "     rebase_off " << dc.rebase_off;
89670b57cec5SDimitry Andric   if (dc.rebase_off > object_size)
89680b57cec5SDimitry Andric     outs() << " (past end of file)\n";
89690b57cec5SDimitry Andric   else
89700b57cec5SDimitry Andric     outs() << "\n";
89710b57cec5SDimitry Andric   outs() << "    rebase_size " << dc.rebase_size;
89720b57cec5SDimitry Andric   uint64_t big_size;
89730b57cec5SDimitry Andric   big_size = dc.rebase_off;
89740b57cec5SDimitry Andric   big_size += dc.rebase_size;
89750b57cec5SDimitry Andric   if (big_size > object_size)
89760b57cec5SDimitry Andric     outs() << " (past end of file)\n";
89770b57cec5SDimitry Andric   else
89780b57cec5SDimitry Andric     outs() << "\n";
89790b57cec5SDimitry Andric   outs() << "       bind_off " << dc.bind_off;
89800b57cec5SDimitry Andric   if (dc.bind_off > object_size)
89810b57cec5SDimitry Andric     outs() << " (past end of file)\n";
89820b57cec5SDimitry Andric   else
89830b57cec5SDimitry Andric     outs() << "\n";
89840b57cec5SDimitry Andric   outs() << "      bind_size " << dc.bind_size;
89850b57cec5SDimitry Andric   big_size = dc.bind_off;
89860b57cec5SDimitry Andric   big_size += dc.bind_size;
89870b57cec5SDimitry Andric   if (big_size > object_size)
89880b57cec5SDimitry Andric     outs() << " (past end of file)\n";
89890b57cec5SDimitry Andric   else
89900b57cec5SDimitry Andric     outs() << "\n";
89910b57cec5SDimitry Andric   outs() << "  weak_bind_off " << dc.weak_bind_off;
89920b57cec5SDimitry Andric   if (dc.weak_bind_off > object_size)
89930b57cec5SDimitry Andric     outs() << " (past end of file)\n";
89940b57cec5SDimitry Andric   else
89950b57cec5SDimitry Andric     outs() << "\n";
89960b57cec5SDimitry Andric   outs() << " weak_bind_size " << dc.weak_bind_size;
89970b57cec5SDimitry Andric   big_size = dc.weak_bind_off;
89980b57cec5SDimitry Andric   big_size += dc.weak_bind_size;
89990b57cec5SDimitry Andric   if (big_size > object_size)
90000b57cec5SDimitry Andric     outs() << " (past end of file)\n";
90010b57cec5SDimitry Andric   else
90020b57cec5SDimitry Andric     outs() << "\n";
90030b57cec5SDimitry Andric   outs() << "  lazy_bind_off " << dc.lazy_bind_off;
90040b57cec5SDimitry Andric   if (dc.lazy_bind_off > object_size)
90050b57cec5SDimitry Andric     outs() << " (past end of file)\n";
90060b57cec5SDimitry Andric   else
90070b57cec5SDimitry Andric     outs() << "\n";
90080b57cec5SDimitry Andric   outs() << " lazy_bind_size " << dc.lazy_bind_size;
90090b57cec5SDimitry Andric   big_size = dc.lazy_bind_off;
90100b57cec5SDimitry Andric   big_size += dc.lazy_bind_size;
90110b57cec5SDimitry Andric   if (big_size > object_size)
90120b57cec5SDimitry Andric     outs() << " (past end of file)\n";
90130b57cec5SDimitry Andric   else
90140b57cec5SDimitry Andric     outs() << "\n";
90150b57cec5SDimitry Andric   outs() << "     export_off " << dc.export_off;
90160b57cec5SDimitry Andric   if (dc.export_off > object_size)
90170b57cec5SDimitry Andric     outs() << " (past end of file)\n";
90180b57cec5SDimitry Andric   else
90190b57cec5SDimitry Andric     outs() << "\n";
90200b57cec5SDimitry Andric   outs() << "    export_size " << dc.export_size;
90210b57cec5SDimitry Andric   big_size = dc.export_off;
90220b57cec5SDimitry Andric   big_size += dc.export_size;
90230b57cec5SDimitry Andric   if (big_size > object_size)
90240b57cec5SDimitry Andric     outs() << " (past end of file)\n";
90250b57cec5SDimitry Andric   else
90260b57cec5SDimitry Andric     outs() << "\n";
90270b57cec5SDimitry Andric }
90280b57cec5SDimitry Andric 
PrintDyldLoadCommand(MachO::dylinker_command dyld,const char * Ptr)90290b57cec5SDimitry Andric static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
90300b57cec5SDimitry Andric                                  const char *Ptr) {
90310b57cec5SDimitry Andric   if (dyld.cmd == MachO::LC_ID_DYLINKER)
90320b57cec5SDimitry Andric     outs() << "          cmd LC_ID_DYLINKER\n";
90330b57cec5SDimitry Andric   else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
90340b57cec5SDimitry Andric     outs() << "          cmd LC_LOAD_DYLINKER\n";
90350b57cec5SDimitry Andric   else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
90360b57cec5SDimitry Andric     outs() << "          cmd LC_DYLD_ENVIRONMENT\n";
90370b57cec5SDimitry Andric   else
90380b57cec5SDimitry Andric     outs() << "          cmd ?(" << dyld.cmd << ")\n";
90390b57cec5SDimitry Andric   outs() << "      cmdsize " << dyld.cmdsize;
90400b57cec5SDimitry Andric   if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
90410b57cec5SDimitry Andric     outs() << " Incorrect size\n";
90420b57cec5SDimitry Andric   else
90430b57cec5SDimitry Andric     outs() << "\n";
90440b57cec5SDimitry Andric   if (dyld.name >= dyld.cmdsize)
90450b57cec5SDimitry Andric     outs() << "         name ?(bad offset " << dyld.name << ")\n";
90460b57cec5SDimitry Andric   else {
90470b57cec5SDimitry Andric     const char *P = (const char *)(Ptr) + dyld.name;
90480b57cec5SDimitry Andric     outs() << "         name " << P << " (offset " << dyld.name << ")\n";
90490b57cec5SDimitry Andric   }
90500b57cec5SDimitry Andric }
90510b57cec5SDimitry Andric 
PrintUuidLoadCommand(MachO::uuid_command uuid)90520b57cec5SDimitry Andric static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
90530b57cec5SDimitry Andric   outs() << "     cmd LC_UUID\n";
90540b57cec5SDimitry Andric   outs() << " cmdsize " << uuid.cmdsize;
90550b57cec5SDimitry Andric   if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
90560b57cec5SDimitry Andric     outs() << " Incorrect size\n";
90570b57cec5SDimitry Andric   else
90580b57cec5SDimitry Andric     outs() << "\n";
90590b57cec5SDimitry Andric   outs() << "    uuid ";
90600b57cec5SDimitry Andric   for (int i = 0; i < 16; ++i) {
90610b57cec5SDimitry Andric     outs() << format("%02" PRIX32, uuid.uuid[i]);
90620b57cec5SDimitry Andric     if (i == 3 || i == 5 || i == 7 || i == 9)
90630b57cec5SDimitry Andric       outs() << "-";
90640b57cec5SDimitry Andric   }
90650b57cec5SDimitry Andric   outs() << "\n";
90660b57cec5SDimitry Andric }
90670b57cec5SDimitry Andric 
PrintRpathLoadCommand(MachO::rpath_command rpath,const char * Ptr)90680b57cec5SDimitry Andric static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
90690b57cec5SDimitry Andric   outs() << "          cmd LC_RPATH\n";
90700b57cec5SDimitry Andric   outs() << "      cmdsize " << rpath.cmdsize;
90710b57cec5SDimitry Andric   if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
90720b57cec5SDimitry Andric     outs() << " Incorrect size\n";
90730b57cec5SDimitry Andric   else
90740b57cec5SDimitry Andric     outs() << "\n";
90750b57cec5SDimitry Andric   if (rpath.path >= rpath.cmdsize)
90760b57cec5SDimitry Andric     outs() << "         path ?(bad offset " << rpath.path << ")\n";
90770b57cec5SDimitry Andric   else {
90780b57cec5SDimitry Andric     const char *P = (const char *)(Ptr) + rpath.path;
90790b57cec5SDimitry Andric     outs() << "         path " << P << " (offset " << rpath.path << ")\n";
90800b57cec5SDimitry Andric   }
90810b57cec5SDimitry Andric }
90820b57cec5SDimitry Andric 
PrintVersionMinLoadCommand(MachO::version_min_command vd)90830b57cec5SDimitry Andric static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
90840b57cec5SDimitry Andric   StringRef LoadCmdName;
90850b57cec5SDimitry Andric   switch (vd.cmd) {
90860b57cec5SDimitry Andric   case MachO::LC_VERSION_MIN_MACOSX:
90870b57cec5SDimitry Andric     LoadCmdName = "LC_VERSION_MIN_MACOSX";
90880b57cec5SDimitry Andric     break;
90890b57cec5SDimitry Andric   case MachO::LC_VERSION_MIN_IPHONEOS:
90900b57cec5SDimitry Andric     LoadCmdName = "LC_VERSION_MIN_IPHONEOS";
90910b57cec5SDimitry Andric     break;
90920b57cec5SDimitry Andric   case MachO::LC_VERSION_MIN_TVOS:
90930b57cec5SDimitry Andric     LoadCmdName = "LC_VERSION_MIN_TVOS";
90940b57cec5SDimitry Andric     break;
90950b57cec5SDimitry Andric   case MachO::LC_VERSION_MIN_WATCHOS:
90960b57cec5SDimitry Andric     LoadCmdName = "LC_VERSION_MIN_WATCHOS";
90970b57cec5SDimitry Andric     break;
90980b57cec5SDimitry Andric   default:
90990b57cec5SDimitry Andric     llvm_unreachable("Unknown version min load command");
91000b57cec5SDimitry Andric   }
91010b57cec5SDimitry Andric 
91020b57cec5SDimitry Andric   outs() << "      cmd " << LoadCmdName << '\n';
91030b57cec5SDimitry Andric   outs() << "  cmdsize " << vd.cmdsize;
91040b57cec5SDimitry Andric   if (vd.cmdsize != sizeof(struct MachO::version_min_command))
91050b57cec5SDimitry Andric     outs() << " Incorrect size\n";
91060b57cec5SDimitry Andric   else
91070b57cec5SDimitry Andric     outs() << "\n";
91080b57cec5SDimitry Andric   outs() << "  version "
91090b57cec5SDimitry Andric          << MachOObjectFile::getVersionMinMajor(vd, false) << "."
91100b57cec5SDimitry Andric          << MachOObjectFile::getVersionMinMinor(vd, false);
91110b57cec5SDimitry Andric   uint32_t Update = MachOObjectFile::getVersionMinUpdate(vd, false);
91120b57cec5SDimitry Andric   if (Update != 0)
91130b57cec5SDimitry Andric     outs() << "." << Update;
91140b57cec5SDimitry Andric   outs() << "\n";
91150b57cec5SDimitry Andric   if (vd.sdk == 0)
91160b57cec5SDimitry Andric     outs() << "      sdk n/a";
91170b57cec5SDimitry Andric   else {
91180b57cec5SDimitry Andric     outs() << "      sdk "
91190b57cec5SDimitry Andric            << MachOObjectFile::getVersionMinMajor(vd, true) << "."
91200b57cec5SDimitry Andric            << MachOObjectFile::getVersionMinMinor(vd, true);
91210b57cec5SDimitry Andric   }
91220b57cec5SDimitry Andric   Update = MachOObjectFile::getVersionMinUpdate(vd, true);
91230b57cec5SDimitry Andric   if (Update != 0)
91240b57cec5SDimitry Andric     outs() << "." << Update;
91250b57cec5SDimitry Andric   outs() << "\n";
91260b57cec5SDimitry Andric }
91270b57cec5SDimitry Andric 
PrintNoteLoadCommand(MachO::note_command Nt)91280b57cec5SDimitry Andric static void PrintNoteLoadCommand(MachO::note_command Nt) {
91290b57cec5SDimitry Andric   outs() << "       cmd LC_NOTE\n";
91300b57cec5SDimitry Andric   outs() << "   cmdsize " << Nt.cmdsize;
91310b57cec5SDimitry Andric   if (Nt.cmdsize != sizeof(struct MachO::note_command))
91320b57cec5SDimitry Andric     outs() << " Incorrect size\n";
91330b57cec5SDimitry Andric   else
91340b57cec5SDimitry Andric     outs() << "\n";
91350b57cec5SDimitry Andric   const char *d = Nt.data_owner;
91360b57cec5SDimitry Andric   outs() << "data_owner " << format("%.16s\n", d);
91370b57cec5SDimitry Andric   outs() << "    offset " << Nt.offset << "\n";
91380b57cec5SDimitry Andric   outs() << "      size " << Nt.size << "\n";
91390b57cec5SDimitry Andric }
91400b57cec5SDimitry Andric 
PrintBuildToolVersion(MachO::build_tool_version bv,bool verbose)914181ad6265SDimitry Andric static void PrintBuildToolVersion(MachO::build_tool_version bv, bool verbose) {
914281ad6265SDimitry Andric   outs() << "      tool ";
914381ad6265SDimitry Andric   if (verbose)
914481ad6265SDimitry Andric     outs() << MachOObjectFile::getBuildTool(bv.tool);
914581ad6265SDimitry Andric   else
914681ad6265SDimitry Andric     outs() << bv.tool;
914781ad6265SDimitry Andric   outs() << "\n";
91480b57cec5SDimitry Andric   outs() << "   version " << MachOObjectFile::getVersionString(bv.version)
91490b57cec5SDimitry Andric          << "\n";
91500b57cec5SDimitry Andric }
91510b57cec5SDimitry Andric 
PrintBuildVersionLoadCommand(const MachOObjectFile * obj,MachO::build_version_command bd,bool verbose)91520b57cec5SDimitry Andric static void PrintBuildVersionLoadCommand(const MachOObjectFile *obj,
915381ad6265SDimitry Andric                                          MachO::build_version_command bd,
915481ad6265SDimitry Andric                                          bool verbose) {
91550b57cec5SDimitry Andric   outs() << "       cmd LC_BUILD_VERSION\n";
91560b57cec5SDimitry Andric   outs() << "   cmdsize " << bd.cmdsize;
91570b57cec5SDimitry Andric   if (bd.cmdsize !=
91580b57cec5SDimitry Andric       sizeof(struct MachO::build_version_command) +
91590b57cec5SDimitry Andric           bd.ntools * sizeof(struct MachO::build_tool_version))
91600b57cec5SDimitry Andric     outs() << " Incorrect size\n";
91610b57cec5SDimitry Andric   else
91620b57cec5SDimitry Andric     outs() << "\n";
916381ad6265SDimitry Andric   outs() << "  platform ";
916481ad6265SDimitry Andric   if (verbose)
916581ad6265SDimitry Andric     outs() << MachOObjectFile::getBuildPlatform(bd.platform);
916681ad6265SDimitry Andric   else
916781ad6265SDimitry Andric     outs() << bd.platform;
916881ad6265SDimitry Andric   outs() << "\n";
91690b57cec5SDimitry Andric   if (bd.sdk)
91700b57cec5SDimitry Andric     outs() << "       sdk " << MachOObjectFile::getVersionString(bd.sdk)
91710b57cec5SDimitry Andric            << "\n";
91720b57cec5SDimitry Andric   else
91730b57cec5SDimitry Andric     outs() << "       sdk n/a\n";
91740b57cec5SDimitry Andric   outs() << "     minos " << MachOObjectFile::getVersionString(bd.minos)
91750b57cec5SDimitry Andric          << "\n";
91760b57cec5SDimitry Andric   outs() << "    ntools " << bd.ntools << "\n";
91770b57cec5SDimitry Andric   for (unsigned i = 0; i < bd.ntools; ++i) {
91780b57cec5SDimitry Andric     MachO::build_tool_version bv = obj->getBuildToolVersion(i);
917981ad6265SDimitry Andric     PrintBuildToolVersion(bv, verbose);
91800b57cec5SDimitry Andric   }
91810b57cec5SDimitry Andric }
91820b57cec5SDimitry Andric 
PrintSourceVersionCommand(MachO::source_version_command sd)91830b57cec5SDimitry Andric static void PrintSourceVersionCommand(MachO::source_version_command sd) {
91840b57cec5SDimitry Andric   outs() << "      cmd LC_SOURCE_VERSION\n";
91850b57cec5SDimitry Andric   outs() << "  cmdsize " << sd.cmdsize;
91860b57cec5SDimitry Andric   if (sd.cmdsize != sizeof(struct MachO::source_version_command))
91870b57cec5SDimitry Andric     outs() << " Incorrect size\n";
91880b57cec5SDimitry Andric   else
91890b57cec5SDimitry Andric     outs() << "\n";
91900b57cec5SDimitry Andric   uint64_t a = (sd.version >> 40) & 0xffffff;
91910b57cec5SDimitry Andric   uint64_t b = (sd.version >> 30) & 0x3ff;
91920b57cec5SDimitry Andric   uint64_t c = (sd.version >> 20) & 0x3ff;
91930b57cec5SDimitry Andric   uint64_t d = (sd.version >> 10) & 0x3ff;
91940b57cec5SDimitry Andric   uint64_t e = sd.version & 0x3ff;
91950b57cec5SDimitry Andric   outs() << "  version " << a << "." << b;
91960b57cec5SDimitry Andric   if (e != 0)
91970b57cec5SDimitry Andric     outs() << "." << c << "." << d << "." << e;
91980b57cec5SDimitry Andric   else if (d != 0)
91990b57cec5SDimitry Andric     outs() << "." << c << "." << d;
92000b57cec5SDimitry Andric   else if (c != 0)
92010b57cec5SDimitry Andric     outs() << "." << c;
92020b57cec5SDimitry Andric   outs() << "\n";
92030b57cec5SDimitry Andric }
92040b57cec5SDimitry Andric 
PrintEntryPointCommand(MachO::entry_point_command ep)92050b57cec5SDimitry Andric static void PrintEntryPointCommand(MachO::entry_point_command ep) {
92060b57cec5SDimitry Andric   outs() << "       cmd LC_MAIN\n";
92070b57cec5SDimitry Andric   outs() << "   cmdsize " << ep.cmdsize;
92080b57cec5SDimitry Andric   if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
92090b57cec5SDimitry Andric     outs() << " Incorrect size\n";
92100b57cec5SDimitry Andric   else
92110b57cec5SDimitry Andric     outs() << "\n";
92120b57cec5SDimitry Andric   outs() << "  entryoff " << ep.entryoff << "\n";
92130b57cec5SDimitry Andric   outs() << " stacksize " << ep.stacksize << "\n";
92140b57cec5SDimitry Andric }
92150b57cec5SDimitry Andric 
PrintEncryptionInfoCommand(MachO::encryption_info_command ec,uint32_t object_size)92160b57cec5SDimitry Andric static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
92170b57cec5SDimitry Andric                                        uint32_t object_size) {
92180b57cec5SDimitry Andric   outs() << "          cmd LC_ENCRYPTION_INFO\n";
92190b57cec5SDimitry Andric   outs() << "      cmdsize " << ec.cmdsize;
92200b57cec5SDimitry Andric   if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
92210b57cec5SDimitry Andric     outs() << " Incorrect size\n";
92220b57cec5SDimitry Andric   else
92230b57cec5SDimitry Andric     outs() << "\n";
92240b57cec5SDimitry Andric   outs() << "     cryptoff " << ec.cryptoff;
92250b57cec5SDimitry Andric   if (ec.cryptoff > object_size)
92260b57cec5SDimitry Andric     outs() << " (past end of file)\n";
92270b57cec5SDimitry Andric   else
92280b57cec5SDimitry Andric     outs() << "\n";
92290b57cec5SDimitry Andric   outs() << "    cryptsize " << ec.cryptsize;
92300b57cec5SDimitry Andric   if (ec.cryptsize > object_size)
92310b57cec5SDimitry Andric     outs() << " (past end of file)\n";
92320b57cec5SDimitry Andric   else
92330b57cec5SDimitry Andric     outs() << "\n";
92340b57cec5SDimitry Andric   outs() << "      cryptid " << ec.cryptid << "\n";
92350b57cec5SDimitry Andric }
92360b57cec5SDimitry Andric 
PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,uint32_t object_size)92370b57cec5SDimitry Andric static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
92380b57cec5SDimitry Andric                                          uint32_t object_size) {
92390b57cec5SDimitry Andric   outs() << "          cmd LC_ENCRYPTION_INFO_64\n";
92400b57cec5SDimitry Andric   outs() << "      cmdsize " << ec.cmdsize;
92410b57cec5SDimitry Andric   if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
92420b57cec5SDimitry Andric     outs() << " Incorrect size\n";
92430b57cec5SDimitry Andric   else
92440b57cec5SDimitry Andric     outs() << "\n";
92450b57cec5SDimitry Andric   outs() << "     cryptoff " << ec.cryptoff;
92460b57cec5SDimitry Andric   if (ec.cryptoff > object_size)
92470b57cec5SDimitry Andric     outs() << " (past end of file)\n";
92480b57cec5SDimitry Andric   else
92490b57cec5SDimitry Andric     outs() << "\n";
92500b57cec5SDimitry Andric   outs() << "    cryptsize " << ec.cryptsize;
92510b57cec5SDimitry Andric   if (ec.cryptsize > object_size)
92520b57cec5SDimitry Andric     outs() << " (past end of file)\n";
92530b57cec5SDimitry Andric   else
92540b57cec5SDimitry Andric     outs() << "\n";
92550b57cec5SDimitry Andric   outs() << "      cryptid " << ec.cryptid << "\n";
92560b57cec5SDimitry Andric   outs() << "          pad " << ec.pad << "\n";
92570b57cec5SDimitry Andric }
92580b57cec5SDimitry Andric 
PrintLinkerOptionCommand(MachO::linker_option_command lo,const char * Ptr)92590b57cec5SDimitry Andric static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
92600b57cec5SDimitry Andric                                      const char *Ptr) {
92610b57cec5SDimitry Andric   outs() << "     cmd LC_LINKER_OPTION\n";
92620b57cec5SDimitry Andric   outs() << " cmdsize " << lo.cmdsize;
92630b57cec5SDimitry Andric   if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
92640b57cec5SDimitry Andric     outs() << " Incorrect size\n";
92650b57cec5SDimitry Andric   else
92660b57cec5SDimitry Andric     outs() << "\n";
92670b57cec5SDimitry Andric   outs() << "   count " << lo.count << "\n";
92680b57cec5SDimitry Andric   const char *string = Ptr + sizeof(struct MachO::linker_option_command);
92690b57cec5SDimitry Andric   uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
92700b57cec5SDimitry Andric   uint32_t i = 0;
92710b57cec5SDimitry Andric   while (left > 0) {
92720b57cec5SDimitry Andric     while (*string == '\0' && left > 0) {
92730b57cec5SDimitry Andric       string++;
92740b57cec5SDimitry Andric       left--;
92750b57cec5SDimitry Andric     }
92760b57cec5SDimitry Andric     if (left > 0) {
92770b57cec5SDimitry Andric       i++;
92780b57cec5SDimitry Andric       outs() << "  string #" << i << " " << format("%.*s\n", left, string);
92790b57cec5SDimitry Andric       uint32_t NullPos = StringRef(string, left).find('\0');
92800b57cec5SDimitry Andric       uint32_t len = std::min(NullPos, left) + 1;
92810b57cec5SDimitry Andric       string += len;
92820b57cec5SDimitry Andric       left -= len;
92830b57cec5SDimitry Andric     }
92840b57cec5SDimitry Andric   }
92850b57cec5SDimitry Andric   if (lo.count != i)
92860b57cec5SDimitry Andric     outs() << "   count " << lo.count << " does not match number of strings "
92870b57cec5SDimitry Andric            << i << "\n";
92880b57cec5SDimitry Andric }
92890b57cec5SDimitry Andric 
PrintSubFrameworkCommand(MachO::sub_framework_command sub,const char * Ptr)92900b57cec5SDimitry Andric static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
92910b57cec5SDimitry Andric                                      const char *Ptr) {
92920b57cec5SDimitry Andric   outs() << "          cmd LC_SUB_FRAMEWORK\n";
92930b57cec5SDimitry Andric   outs() << "      cmdsize " << sub.cmdsize;
92940b57cec5SDimitry Andric   if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
92950b57cec5SDimitry Andric     outs() << " Incorrect size\n";
92960b57cec5SDimitry Andric   else
92970b57cec5SDimitry Andric     outs() << "\n";
92980b57cec5SDimitry Andric   if (sub.umbrella < sub.cmdsize) {
92990b57cec5SDimitry Andric     const char *P = Ptr + sub.umbrella;
93000b57cec5SDimitry Andric     outs() << "     umbrella " << P << " (offset " << sub.umbrella << ")\n";
93010b57cec5SDimitry Andric   } else {
93020b57cec5SDimitry Andric     outs() << "     umbrella ?(bad offset " << sub.umbrella << ")\n";
93030b57cec5SDimitry Andric   }
93040b57cec5SDimitry Andric }
93050b57cec5SDimitry Andric 
PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,const char * Ptr)93060b57cec5SDimitry Andric static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
93070b57cec5SDimitry Andric                                     const char *Ptr) {
93080b57cec5SDimitry Andric   outs() << "          cmd LC_SUB_UMBRELLA\n";
93090b57cec5SDimitry Andric   outs() << "      cmdsize " << sub.cmdsize;
93100b57cec5SDimitry Andric   if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
93110b57cec5SDimitry Andric     outs() << " Incorrect size\n";
93120b57cec5SDimitry Andric   else
93130b57cec5SDimitry Andric     outs() << "\n";
93140b57cec5SDimitry Andric   if (sub.sub_umbrella < sub.cmdsize) {
93150b57cec5SDimitry Andric     const char *P = Ptr + sub.sub_umbrella;
93160b57cec5SDimitry Andric     outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
93170b57cec5SDimitry Andric   } else {
93180b57cec5SDimitry Andric     outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
93190b57cec5SDimitry Andric   }
93200b57cec5SDimitry Andric }
93210b57cec5SDimitry Andric 
PrintSubLibraryCommand(MachO::sub_library_command sub,const char * Ptr)93220b57cec5SDimitry Andric static void PrintSubLibraryCommand(MachO::sub_library_command sub,
93230b57cec5SDimitry Andric                                    const char *Ptr) {
93240b57cec5SDimitry Andric   outs() << "          cmd LC_SUB_LIBRARY\n";
93250b57cec5SDimitry Andric   outs() << "      cmdsize " << sub.cmdsize;
93260b57cec5SDimitry Andric   if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
93270b57cec5SDimitry Andric     outs() << " Incorrect size\n";
93280b57cec5SDimitry Andric   else
93290b57cec5SDimitry Andric     outs() << "\n";
93300b57cec5SDimitry Andric   if (sub.sub_library < sub.cmdsize) {
93310b57cec5SDimitry Andric     const char *P = Ptr + sub.sub_library;
93320b57cec5SDimitry Andric     outs() << "  sub_library " << P << " (offset " << sub.sub_library << ")\n";
93330b57cec5SDimitry Andric   } else {
93340b57cec5SDimitry Andric     outs() << "  sub_library ?(bad offset " << sub.sub_library << ")\n";
93350b57cec5SDimitry Andric   }
93360b57cec5SDimitry Andric }
93370b57cec5SDimitry Andric 
PrintSubClientCommand(MachO::sub_client_command sub,const char * Ptr)93380b57cec5SDimitry Andric static void PrintSubClientCommand(MachO::sub_client_command sub,
93390b57cec5SDimitry Andric                                   const char *Ptr) {
93400b57cec5SDimitry Andric   outs() << "          cmd LC_SUB_CLIENT\n";
93410b57cec5SDimitry Andric   outs() << "      cmdsize " << sub.cmdsize;
93420b57cec5SDimitry Andric   if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
93430b57cec5SDimitry Andric     outs() << " Incorrect size\n";
93440b57cec5SDimitry Andric   else
93450b57cec5SDimitry Andric     outs() << "\n";
93460b57cec5SDimitry Andric   if (sub.client < sub.cmdsize) {
93470b57cec5SDimitry Andric     const char *P = Ptr + sub.client;
93480b57cec5SDimitry Andric     outs() << "       client " << P << " (offset " << sub.client << ")\n";
93490b57cec5SDimitry Andric   } else {
93500b57cec5SDimitry Andric     outs() << "       client ?(bad offset " << sub.client << ")\n";
93510b57cec5SDimitry Andric   }
93520b57cec5SDimitry Andric }
93530b57cec5SDimitry Andric 
PrintRoutinesCommand(MachO::routines_command r)93540b57cec5SDimitry Andric static void PrintRoutinesCommand(MachO::routines_command r) {
93550b57cec5SDimitry Andric   outs() << "          cmd LC_ROUTINES\n";
93560b57cec5SDimitry Andric   outs() << "      cmdsize " << r.cmdsize;
93570b57cec5SDimitry Andric   if (r.cmdsize != sizeof(struct MachO::routines_command))
93580b57cec5SDimitry Andric     outs() << " Incorrect size\n";
93590b57cec5SDimitry Andric   else
93600b57cec5SDimitry Andric     outs() << "\n";
93610b57cec5SDimitry Andric   outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
93620b57cec5SDimitry Andric   outs() << "  init_module " << r.init_module << "\n";
93630b57cec5SDimitry Andric   outs() << "    reserved1 " << r.reserved1 << "\n";
93640b57cec5SDimitry Andric   outs() << "    reserved2 " << r.reserved2 << "\n";
93650b57cec5SDimitry Andric   outs() << "    reserved3 " << r.reserved3 << "\n";
93660b57cec5SDimitry Andric   outs() << "    reserved4 " << r.reserved4 << "\n";
93670b57cec5SDimitry Andric   outs() << "    reserved5 " << r.reserved5 << "\n";
93680b57cec5SDimitry Andric   outs() << "    reserved6 " << r.reserved6 << "\n";
93690b57cec5SDimitry Andric }
93700b57cec5SDimitry Andric 
PrintRoutinesCommand64(MachO::routines_command_64 r)93710b57cec5SDimitry Andric static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
93720b57cec5SDimitry Andric   outs() << "          cmd LC_ROUTINES_64\n";
93730b57cec5SDimitry Andric   outs() << "      cmdsize " << r.cmdsize;
93740b57cec5SDimitry Andric   if (r.cmdsize != sizeof(struct MachO::routines_command_64))
93750b57cec5SDimitry Andric     outs() << " Incorrect size\n";
93760b57cec5SDimitry Andric   else
93770b57cec5SDimitry Andric     outs() << "\n";
93780b57cec5SDimitry Andric   outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
93790b57cec5SDimitry Andric   outs() << "  init_module " << r.init_module << "\n";
93800b57cec5SDimitry Andric   outs() << "    reserved1 " << r.reserved1 << "\n";
93810b57cec5SDimitry Andric   outs() << "    reserved2 " << r.reserved2 << "\n";
93820b57cec5SDimitry Andric   outs() << "    reserved3 " << r.reserved3 << "\n";
93830b57cec5SDimitry Andric   outs() << "    reserved4 " << r.reserved4 << "\n";
93840b57cec5SDimitry Andric   outs() << "    reserved5 " << r.reserved5 << "\n";
93850b57cec5SDimitry Andric   outs() << "    reserved6 " << r.reserved6 << "\n";
93860b57cec5SDimitry Andric }
93870b57cec5SDimitry Andric 
Print_x86_thread_state32_t(MachO::x86_thread_state32_t & cpu32)93880b57cec5SDimitry Andric static void Print_x86_thread_state32_t(MachO::x86_thread_state32_t &cpu32) {
93890b57cec5SDimitry Andric   outs() << "\t    eax " << format("0x%08" PRIx32, cpu32.eax);
93900b57cec5SDimitry Andric   outs() << " ebx    " << format("0x%08" PRIx32, cpu32.ebx);
93910b57cec5SDimitry Andric   outs() << " ecx " << format("0x%08" PRIx32, cpu32.ecx);
93920b57cec5SDimitry Andric   outs() << " edx " << format("0x%08" PRIx32, cpu32.edx) << "\n";
93930b57cec5SDimitry Andric   outs() << "\t    edi " << format("0x%08" PRIx32, cpu32.edi);
93940b57cec5SDimitry Andric   outs() << " esi    " << format("0x%08" PRIx32, cpu32.esi);
93950b57cec5SDimitry Andric   outs() << " ebp " << format("0x%08" PRIx32, cpu32.ebp);
93960b57cec5SDimitry Andric   outs() << " esp " << format("0x%08" PRIx32, cpu32.esp) << "\n";
93970b57cec5SDimitry Andric   outs() << "\t    ss  " << format("0x%08" PRIx32, cpu32.ss);
93980b57cec5SDimitry Andric   outs() << " eflags " << format("0x%08" PRIx32, cpu32.eflags);
93990b57cec5SDimitry Andric   outs() << " eip " << format("0x%08" PRIx32, cpu32.eip);
94000b57cec5SDimitry Andric   outs() << " cs  " << format("0x%08" PRIx32, cpu32.cs) << "\n";
94010b57cec5SDimitry Andric   outs() << "\t    ds  " << format("0x%08" PRIx32, cpu32.ds);
94020b57cec5SDimitry Andric   outs() << " es     " << format("0x%08" PRIx32, cpu32.es);
94030b57cec5SDimitry Andric   outs() << " fs  " << format("0x%08" PRIx32, cpu32.fs);
94040b57cec5SDimitry Andric   outs() << " gs  " << format("0x%08" PRIx32, cpu32.gs) << "\n";
94050b57cec5SDimitry Andric }
94060b57cec5SDimitry Andric 
Print_x86_thread_state64_t(MachO::x86_thread_state64_t & cpu64)94070b57cec5SDimitry Andric static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
94080b57cec5SDimitry Andric   outs() << "   rax  " << format("0x%016" PRIx64, cpu64.rax);
94090b57cec5SDimitry Andric   outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
94100b57cec5SDimitry Andric   outs() << " rcx  " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
94110b57cec5SDimitry Andric   outs() << "   rdx  " << format("0x%016" PRIx64, cpu64.rdx);
94120b57cec5SDimitry Andric   outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
94130b57cec5SDimitry Andric   outs() << " rsi  " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
94140b57cec5SDimitry Andric   outs() << "   rbp  " << format("0x%016" PRIx64, cpu64.rbp);
94150b57cec5SDimitry Andric   outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
94160b57cec5SDimitry Andric   outs() << " r8   " << format("0x%016" PRIx64, cpu64.r8) << "\n";
94170b57cec5SDimitry Andric   outs() << "    r9  " << format("0x%016" PRIx64, cpu64.r9);
94180b57cec5SDimitry Andric   outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
94190b57cec5SDimitry Andric   outs() << " r11  " << format("0x%016" PRIx64, cpu64.r11) << "\n";
94200b57cec5SDimitry Andric   outs() << "   r12  " << format("0x%016" PRIx64, cpu64.r12);
94210b57cec5SDimitry Andric   outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
94220b57cec5SDimitry Andric   outs() << " r14  " << format("0x%016" PRIx64, cpu64.r14) << "\n";
94230b57cec5SDimitry Andric   outs() << "   r15  " << format("0x%016" PRIx64, cpu64.r15);
94240b57cec5SDimitry Andric   outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
94250b57cec5SDimitry Andric   outs() << "rflags  " << format("0x%016" PRIx64, cpu64.rflags);
94260b57cec5SDimitry Andric   outs() << " cs  " << format("0x%016" PRIx64, cpu64.cs);
94270b57cec5SDimitry Andric   outs() << " fs   " << format("0x%016" PRIx64, cpu64.fs) << "\n";
94280b57cec5SDimitry Andric   outs() << "    gs  " << format("0x%016" PRIx64, cpu64.gs) << "\n";
94290b57cec5SDimitry Andric }
94300b57cec5SDimitry Andric 
Print_mmst_reg(MachO::mmst_reg_t & r)94310b57cec5SDimitry Andric static void Print_mmst_reg(MachO::mmst_reg_t &r) {
94320b57cec5SDimitry Andric   uint32_t f;
94330b57cec5SDimitry Andric   outs() << "\t      mmst_reg  ";
94340b57cec5SDimitry Andric   for (f = 0; f < 10; f++)
94350b57cec5SDimitry Andric     outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
94360b57cec5SDimitry Andric   outs() << "\n";
94370b57cec5SDimitry Andric   outs() << "\t      mmst_rsrv ";
94380b57cec5SDimitry Andric   for (f = 0; f < 6; f++)
94390b57cec5SDimitry Andric     outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
94400b57cec5SDimitry Andric   outs() << "\n";
94410b57cec5SDimitry Andric }
94420b57cec5SDimitry Andric 
Print_xmm_reg(MachO::xmm_reg_t & r)94430b57cec5SDimitry Andric static void Print_xmm_reg(MachO::xmm_reg_t &r) {
94440b57cec5SDimitry Andric   uint32_t f;
94450b57cec5SDimitry Andric   outs() << "\t      xmm_reg ";
94460b57cec5SDimitry Andric   for (f = 0; f < 16; f++)
94470b57cec5SDimitry Andric     outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
94480b57cec5SDimitry Andric   outs() << "\n";
94490b57cec5SDimitry Andric }
94500b57cec5SDimitry Andric 
Print_x86_float_state_t(MachO::x86_float_state64_t & fpu)94510b57cec5SDimitry Andric static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
94520b57cec5SDimitry Andric   outs() << "\t    fpu_reserved[0] " << fpu.fpu_reserved[0];
94530b57cec5SDimitry Andric   outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
94540b57cec5SDimitry Andric   outs() << "\t    control: invalid " << fpu.fpu_fcw.invalid;
94550b57cec5SDimitry Andric   outs() << " denorm " << fpu.fpu_fcw.denorm;
94560b57cec5SDimitry Andric   outs() << " zdiv " << fpu.fpu_fcw.zdiv;
94570b57cec5SDimitry Andric   outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
94580b57cec5SDimitry Andric   outs() << " undfl " << fpu.fpu_fcw.undfl;
94590b57cec5SDimitry Andric   outs() << " precis " << fpu.fpu_fcw.precis << "\n";
94600b57cec5SDimitry Andric   outs() << "\t\t     pc ";
94610b57cec5SDimitry Andric   if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
94620b57cec5SDimitry Andric     outs() << "FP_PREC_24B ";
94630b57cec5SDimitry Andric   else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
94640b57cec5SDimitry Andric     outs() << "FP_PREC_53B ";
94650b57cec5SDimitry Andric   else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
94660b57cec5SDimitry Andric     outs() << "FP_PREC_64B ";
94670b57cec5SDimitry Andric   else
94680b57cec5SDimitry Andric     outs() << fpu.fpu_fcw.pc << " ";
94690b57cec5SDimitry Andric   outs() << "rc ";
94700b57cec5SDimitry Andric   if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
94710b57cec5SDimitry Andric     outs() << "FP_RND_NEAR ";
94720b57cec5SDimitry Andric   else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
94730b57cec5SDimitry Andric     outs() << "FP_RND_DOWN ";
94740b57cec5SDimitry Andric   else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
94750b57cec5SDimitry Andric     outs() << "FP_RND_UP ";
94760b57cec5SDimitry Andric   else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
94770b57cec5SDimitry Andric     outs() << "FP_CHOP ";
94780b57cec5SDimitry Andric   outs() << "\n";
94790b57cec5SDimitry Andric   outs() << "\t    status: invalid " << fpu.fpu_fsw.invalid;
94800b57cec5SDimitry Andric   outs() << " denorm " << fpu.fpu_fsw.denorm;
94810b57cec5SDimitry Andric   outs() << " zdiv " << fpu.fpu_fsw.zdiv;
94820b57cec5SDimitry Andric   outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
94830b57cec5SDimitry Andric   outs() << " undfl " << fpu.fpu_fsw.undfl;
94840b57cec5SDimitry Andric   outs() << " precis " << fpu.fpu_fsw.precis;
94850b57cec5SDimitry Andric   outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
94860b57cec5SDimitry Andric   outs() << "\t            errsumm " << fpu.fpu_fsw.errsumm;
94870b57cec5SDimitry Andric   outs() << " c0 " << fpu.fpu_fsw.c0;
94880b57cec5SDimitry Andric   outs() << " c1 " << fpu.fpu_fsw.c1;
94890b57cec5SDimitry Andric   outs() << " c2 " << fpu.fpu_fsw.c2;
94900b57cec5SDimitry Andric   outs() << " tos " << fpu.fpu_fsw.tos;
94910b57cec5SDimitry Andric   outs() << " c3 " << fpu.fpu_fsw.c3;
94920b57cec5SDimitry Andric   outs() << " busy " << fpu.fpu_fsw.busy << "\n";
94930b57cec5SDimitry Andric   outs() << "\t    fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
94940b57cec5SDimitry Andric   outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
94950b57cec5SDimitry Andric   outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
94960b57cec5SDimitry Andric   outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
94970b57cec5SDimitry Andric   outs() << "\t    fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
94980b57cec5SDimitry Andric   outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
94990b57cec5SDimitry Andric   outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
95000b57cec5SDimitry Andric   outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
95010b57cec5SDimitry Andric   outs() << "\t    fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
95020b57cec5SDimitry Andric   outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
95030b57cec5SDimitry Andric   outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
95040b57cec5SDimitry Andric   outs() << "\n";
95050b57cec5SDimitry Andric   outs() << "\t    fpu_stmm0:\n";
95060b57cec5SDimitry Andric   Print_mmst_reg(fpu.fpu_stmm0);
95070b57cec5SDimitry Andric   outs() << "\t    fpu_stmm1:\n";
95080b57cec5SDimitry Andric   Print_mmst_reg(fpu.fpu_stmm1);
95090b57cec5SDimitry Andric   outs() << "\t    fpu_stmm2:\n";
95100b57cec5SDimitry Andric   Print_mmst_reg(fpu.fpu_stmm2);
95110b57cec5SDimitry Andric   outs() << "\t    fpu_stmm3:\n";
95120b57cec5SDimitry Andric   Print_mmst_reg(fpu.fpu_stmm3);
95130b57cec5SDimitry Andric   outs() << "\t    fpu_stmm4:\n";
95140b57cec5SDimitry Andric   Print_mmst_reg(fpu.fpu_stmm4);
95150b57cec5SDimitry Andric   outs() << "\t    fpu_stmm5:\n";
95160b57cec5SDimitry Andric   Print_mmst_reg(fpu.fpu_stmm5);
95170b57cec5SDimitry Andric   outs() << "\t    fpu_stmm6:\n";
95180b57cec5SDimitry Andric   Print_mmst_reg(fpu.fpu_stmm6);
95190b57cec5SDimitry Andric   outs() << "\t    fpu_stmm7:\n";
95200b57cec5SDimitry Andric   Print_mmst_reg(fpu.fpu_stmm7);
95210b57cec5SDimitry Andric   outs() << "\t    fpu_xmm0:\n";
95220b57cec5SDimitry Andric   Print_xmm_reg(fpu.fpu_xmm0);
95230b57cec5SDimitry Andric   outs() << "\t    fpu_xmm1:\n";
95240b57cec5SDimitry Andric   Print_xmm_reg(fpu.fpu_xmm1);
95250b57cec5SDimitry Andric   outs() << "\t    fpu_xmm2:\n";
95260b57cec5SDimitry Andric   Print_xmm_reg(fpu.fpu_xmm2);
95270b57cec5SDimitry Andric   outs() << "\t    fpu_xmm3:\n";
95280b57cec5SDimitry Andric   Print_xmm_reg(fpu.fpu_xmm3);
95290b57cec5SDimitry Andric   outs() << "\t    fpu_xmm4:\n";
95300b57cec5SDimitry Andric   Print_xmm_reg(fpu.fpu_xmm4);
95310b57cec5SDimitry Andric   outs() << "\t    fpu_xmm5:\n";
95320b57cec5SDimitry Andric   Print_xmm_reg(fpu.fpu_xmm5);
95330b57cec5SDimitry Andric   outs() << "\t    fpu_xmm6:\n";
95340b57cec5SDimitry Andric   Print_xmm_reg(fpu.fpu_xmm6);
95350b57cec5SDimitry Andric   outs() << "\t    fpu_xmm7:\n";
95360b57cec5SDimitry Andric   Print_xmm_reg(fpu.fpu_xmm7);
95370b57cec5SDimitry Andric   outs() << "\t    fpu_xmm8:\n";
95380b57cec5SDimitry Andric   Print_xmm_reg(fpu.fpu_xmm8);
95390b57cec5SDimitry Andric   outs() << "\t    fpu_xmm9:\n";
95400b57cec5SDimitry Andric   Print_xmm_reg(fpu.fpu_xmm9);
95410b57cec5SDimitry Andric   outs() << "\t    fpu_xmm10:\n";
95420b57cec5SDimitry Andric   Print_xmm_reg(fpu.fpu_xmm10);
95430b57cec5SDimitry Andric   outs() << "\t    fpu_xmm11:\n";
95440b57cec5SDimitry Andric   Print_xmm_reg(fpu.fpu_xmm11);
95450b57cec5SDimitry Andric   outs() << "\t    fpu_xmm12:\n";
95460b57cec5SDimitry Andric   Print_xmm_reg(fpu.fpu_xmm12);
95470b57cec5SDimitry Andric   outs() << "\t    fpu_xmm13:\n";
95480b57cec5SDimitry Andric   Print_xmm_reg(fpu.fpu_xmm13);
95490b57cec5SDimitry Andric   outs() << "\t    fpu_xmm14:\n";
95500b57cec5SDimitry Andric   Print_xmm_reg(fpu.fpu_xmm14);
95510b57cec5SDimitry Andric   outs() << "\t    fpu_xmm15:\n";
95520b57cec5SDimitry Andric   Print_xmm_reg(fpu.fpu_xmm15);
95530b57cec5SDimitry Andric   outs() << "\t    fpu_rsrv4:\n";
95540b57cec5SDimitry Andric   for (uint32_t f = 0; f < 6; f++) {
95550b57cec5SDimitry Andric     outs() << "\t            ";
95560b57cec5SDimitry Andric     for (uint32_t g = 0; g < 16; g++)
95570b57cec5SDimitry Andric       outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
95580b57cec5SDimitry Andric     outs() << "\n";
95590b57cec5SDimitry Andric   }
95600b57cec5SDimitry Andric   outs() << "\t    fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
95610b57cec5SDimitry Andric   outs() << "\n";
95620b57cec5SDimitry Andric }
95630b57cec5SDimitry Andric 
Print_x86_exception_state_t(MachO::x86_exception_state64_t & exc64)95640b57cec5SDimitry Andric static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
95650b57cec5SDimitry Andric   outs() << "\t    trapno " << format("0x%08" PRIx32, exc64.trapno);
95660b57cec5SDimitry Andric   outs() << " err " << format("0x%08" PRIx32, exc64.err);
95670b57cec5SDimitry Andric   outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
95680b57cec5SDimitry Andric }
95690b57cec5SDimitry Andric 
Print_arm_thread_state32_t(MachO::arm_thread_state32_t & cpu32)95700b57cec5SDimitry Andric static void Print_arm_thread_state32_t(MachO::arm_thread_state32_t &cpu32) {
95710b57cec5SDimitry Andric   outs() << "\t    r0  " << format("0x%08" PRIx32, cpu32.r[0]);
95720b57cec5SDimitry Andric   outs() << " r1     "   << format("0x%08" PRIx32, cpu32.r[1]);
95730b57cec5SDimitry Andric   outs() << " r2  "      << format("0x%08" PRIx32, cpu32.r[2]);
95740b57cec5SDimitry Andric   outs() << " r3  "      << format("0x%08" PRIx32, cpu32.r[3]) << "\n";
95750b57cec5SDimitry Andric   outs() << "\t    r4  " << format("0x%08" PRIx32, cpu32.r[4]);
95760b57cec5SDimitry Andric   outs() << " r5     "   << format("0x%08" PRIx32, cpu32.r[5]);
95770b57cec5SDimitry Andric   outs() << " r6  "      << format("0x%08" PRIx32, cpu32.r[6]);
95780b57cec5SDimitry Andric   outs() << " r7  "      << format("0x%08" PRIx32, cpu32.r[7]) << "\n";
95790b57cec5SDimitry Andric   outs() << "\t    r8  " << format("0x%08" PRIx32, cpu32.r[8]);
95800b57cec5SDimitry Andric   outs() << " r9     "   << format("0x%08" PRIx32, cpu32.r[9]);
95810b57cec5SDimitry Andric   outs() << " r10 "      << format("0x%08" PRIx32, cpu32.r[10]);
95820b57cec5SDimitry Andric   outs() << " r11 "      << format("0x%08" PRIx32, cpu32.r[11]) << "\n";
95830b57cec5SDimitry Andric   outs() << "\t    r12 " << format("0x%08" PRIx32, cpu32.r[12]);
95840b57cec5SDimitry Andric   outs() << " sp     "   << format("0x%08" PRIx32, cpu32.sp);
95850b57cec5SDimitry Andric   outs() << " lr  "      << format("0x%08" PRIx32, cpu32.lr);
95860b57cec5SDimitry Andric   outs() << " pc  "      << format("0x%08" PRIx32, cpu32.pc) << "\n";
95870b57cec5SDimitry Andric   outs() << "\t   cpsr " << format("0x%08" PRIx32, cpu32.cpsr) << "\n";
95880b57cec5SDimitry Andric }
95890b57cec5SDimitry Andric 
Print_arm_thread_state64_t(MachO::arm_thread_state64_t & cpu64)95900b57cec5SDimitry Andric static void Print_arm_thread_state64_t(MachO::arm_thread_state64_t &cpu64) {
95910b57cec5SDimitry Andric   outs() << "\t    x0  " << format("0x%016" PRIx64, cpu64.x[0]);
95920b57cec5SDimitry Andric   outs() << " x1  "      << format("0x%016" PRIx64, cpu64.x[1]);
95930b57cec5SDimitry Andric   outs() << " x2  "      << format("0x%016" PRIx64, cpu64.x[2]) << "\n";
95940b57cec5SDimitry Andric   outs() << "\t    x3  " << format("0x%016" PRIx64, cpu64.x[3]);
95950b57cec5SDimitry Andric   outs() << " x4  "      << format("0x%016" PRIx64, cpu64.x[4]);
95960b57cec5SDimitry Andric   outs() << " x5  "      << format("0x%016" PRIx64, cpu64.x[5]) << "\n";
95970b57cec5SDimitry Andric   outs() << "\t    x6  " << format("0x%016" PRIx64, cpu64.x[6]);
95980b57cec5SDimitry Andric   outs() << " x7  "      << format("0x%016" PRIx64, cpu64.x[7]);
95990b57cec5SDimitry Andric   outs() << " x8  "      << format("0x%016" PRIx64, cpu64.x[8]) << "\n";
96000b57cec5SDimitry Andric   outs() << "\t    x9  " << format("0x%016" PRIx64, cpu64.x[9]);
96010b57cec5SDimitry Andric   outs() << " x10 "      << format("0x%016" PRIx64, cpu64.x[10]);
96020b57cec5SDimitry Andric   outs() << " x11 "      << format("0x%016" PRIx64, cpu64.x[11]) << "\n";
96030b57cec5SDimitry Andric   outs() << "\t    x12 " << format("0x%016" PRIx64, cpu64.x[12]);
96040b57cec5SDimitry Andric   outs() << " x13 "      << format("0x%016" PRIx64, cpu64.x[13]);
96050b57cec5SDimitry Andric   outs() << " x14 "      << format("0x%016" PRIx64, cpu64.x[14]) << "\n";
96060b57cec5SDimitry Andric   outs() << "\t    x15 " << format("0x%016" PRIx64, cpu64.x[15]);
96070b57cec5SDimitry Andric   outs() << " x16 "      << format("0x%016" PRIx64, cpu64.x[16]);
96080b57cec5SDimitry Andric   outs() << " x17 "      << format("0x%016" PRIx64, cpu64.x[17]) << "\n";
96090b57cec5SDimitry Andric   outs() << "\t    x18 " << format("0x%016" PRIx64, cpu64.x[18]);
96100b57cec5SDimitry Andric   outs() << " x19 "      << format("0x%016" PRIx64, cpu64.x[19]);
96110b57cec5SDimitry Andric   outs() << " x20 "      << format("0x%016" PRIx64, cpu64.x[20]) << "\n";
96120b57cec5SDimitry Andric   outs() << "\t    x21 " << format("0x%016" PRIx64, cpu64.x[21]);
96130b57cec5SDimitry Andric   outs() << " x22 "      << format("0x%016" PRIx64, cpu64.x[22]);
96140b57cec5SDimitry Andric   outs() << " x23 "      << format("0x%016" PRIx64, cpu64.x[23]) << "\n";
96150b57cec5SDimitry Andric   outs() << "\t    x24 " << format("0x%016" PRIx64, cpu64.x[24]);
96160b57cec5SDimitry Andric   outs() << " x25 "      << format("0x%016" PRIx64, cpu64.x[25]);
96170b57cec5SDimitry Andric   outs() << " x26 "      << format("0x%016" PRIx64, cpu64.x[26]) << "\n";
96180b57cec5SDimitry Andric   outs() << "\t    x27 " << format("0x%016" PRIx64, cpu64.x[27]);
96190b57cec5SDimitry Andric   outs() << " x28 "      << format("0x%016" PRIx64, cpu64.x[28]);
96200b57cec5SDimitry Andric   outs() << "  fp "      << format("0x%016" PRIx64, cpu64.fp) << "\n";
96210b57cec5SDimitry Andric   outs() << "\t     lr " << format("0x%016" PRIx64, cpu64.lr);
96220b57cec5SDimitry Andric   outs() << " sp  "      << format("0x%016" PRIx64, cpu64.sp);
96230b57cec5SDimitry Andric   outs() << "  pc "      << format("0x%016" PRIx64, cpu64.pc) << "\n";
96240b57cec5SDimitry Andric   outs() << "\t   cpsr " << format("0x%08"  PRIx32, cpu64.cpsr) << "\n";
96250b57cec5SDimitry Andric }
96260b57cec5SDimitry Andric 
PrintThreadCommand(MachO::thread_command t,const char * Ptr,bool isLittleEndian,uint32_t cputype)96270b57cec5SDimitry Andric static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
96280b57cec5SDimitry Andric                                bool isLittleEndian, uint32_t cputype) {
96290b57cec5SDimitry Andric   if (t.cmd == MachO::LC_THREAD)
96300b57cec5SDimitry Andric     outs() << "        cmd LC_THREAD\n";
96310b57cec5SDimitry Andric   else if (t.cmd == MachO::LC_UNIXTHREAD)
96320b57cec5SDimitry Andric     outs() << "        cmd LC_UNIXTHREAD\n";
96330b57cec5SDimitry Andric   else
96340b57cec5SDimitry Andric     outs() << "        cmd " << t.cmd << " (unknown)\n";
96350b57cec5SDimitry Andric   outs() << "    cmdsize " << t.cmdsize;
96360b57cec5SDimitry Andric   if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
96370b57cec5SDimitry Andric     outs() << " Incorrect size\n";
96380b57cec5SDimitry Andric   else
96390b57cec5SDimitry Andric     outs() << "\n";
96400b57cec5SDimitry Andric 
96410b57cec5SDimitry Andric   const char *begin = Ptr + sizeof(struct MachO::thread_command);
96420b57cec5SDimitry Andric   const char *end = Ptr + t.cmdsize;
96430b57cec5SDimitry Andric   uint32_t flavor, count, left;
96440b57cec5SDimitry Andric   if (cputype == MachO::CPU_TYPE_I386) {
96450b57cec5SDimitry Andric     while (begin < end) {
96460b57cec5SDimitry Andric       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
96470b57cec5SDimitry Andric         memcpy((char *)&flavor, begin, sizeof(uint32_t));
96480b57cec5SDimitry Andric         begin += sizeof(uint32_t);
96490b57cec5SDimitry Andric       } else {
96500b57cec5SDimitry Andric         flavor = 0;
96510b57cec5SDimitry Andric         begin = end;
96520b57cec5SDimitry Andric       }
96530b57cec5SDimitry Andric       if (isLittleEndian != sys::IsLittleEndianHost)
96540b57cec5SDimitry Andric         sys::swapByteOrder(flavor);
96550b57cec5SDimitry Andric       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
96560b57cec5SDimitry Andric         memcpy((char *)&count, begin, sizeof(uint32_t));
96570b57cec5SDimitry Andric         begin += sizeof(uint32_t);
96580b57cec5SDimitry Andric       } else {
96590b57cec5SDimitry Andric         count = 0;
96600b57cec5SDimitry Andric         begin = end;
96610b57cec5SDimitry Andric       }
96620b57cec5SDimitry Andric       if (isLittleEndian != sys::IsLittleEndianHost)
96630b57cec5SDimitry Andric         sys::swapByteOrder(count);
96640b57cec5SDimitry Andric       if (flavor == MachO::x86_THREAD_STATE32) {
96650b57cec5SDimitry Andric         outs() << "     flavor i386_THREAD_STATE\n";
96660b57cec5SDimitry Andric         if (count == MachO::x86_THREAD_STATE32_COUNT)
96670b57cec5SDimitry Andric           outs() << "      count i386_THREAD_STATE_COUNT\n";
96680b57cec5SDimitry Andric         else
96690b57cec5SDimitry Andric           outs() << "      count " << count
96700b57cec5SDimitry Andric                  << " (not x86_THREAD_STATE32_COUNT)\n";
96710b57cec5SDimitry Andric         MachO::x86_thread_state32_t cpu32;
96720b57cec5SDimitry Andric         left = end - begin;
96730b57cec5SDimitry Andric         if (left >= sizeof(MachO::x86_thread_state32_t)) {
96740b57cec5SDimitry Andric           memcpy(&cpu32, begin, sizeof(MachO::x86_thread_state32_t));
96750b57cec5SDimitry Andric           begin += sizeof(MachO::x86_thread_state32_t);
96760b57cec5SDimitry Andric         } else {
96770b57cec5SDimitry Andric           memset(&cpu32, '\0', sizeof(MachO::x86_thread_state32_t));
96780b57cec5SDimitry Andric           memcpy(&cpu32, begin, left);
96790b57cec5SDimitry Andric           begin += left;
96800b57cec5SDimitry Andric         }
96810b57cec5SDimitry Andric         if (isLittleEndian != sys::IsLittleEndianHost)
96820b57cec5SDimitry Andric           swapStruct(cpu32);
96830b57cec5SDimitry Andric         Print_x86_thread_state32_t(cpu32);
96840b57cec5SDimitry Andric       } else if (flavor == MachO::x86_THREAD_STATE) {
96850b57cec5SDimitry Andric         outs() << "     flavor x86_THREAD_STATE\n";
96860b57cec5SDimitry Andric         if (count == MachO::x86_THREAD_STATE_COUNT)
96870b57cec5SDimitry Andric           outs() << "      count x86_THREAD_STATE_COUNT\n";
96880b57cec5SDimitry Andric         else
96890b57cec5SDimitry Andric           outs() << "      count " << count
96900b57cec5SDimitry Andric                  << " (not x86_THREAD_STATE_COUNT)\n";
96910b57cec5SDimitry Andric         struct MachO::x86_thread_state_t ts;
96920b57cec5SDimitry Andric         left = end - begin;
96930b57cec5SDimitry Andric         if (left >= sizeof(MachO::x86_thread_state_t)) {
96940b57cec5SDimitry Andric           memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
96950b57cec5SDimitry Andric           begin += sizeof(MachO::x86_thread_state_t);
96960b57cec5SDimitry Andric         } else {
96970b57cec5SDimitry Andric           memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
96980b57cec5SDimitry Andric           memcpy(&ts, begin, left);
96990b57cec5SDimitry Andric           begin += left;
97000b57cec5SDimitry Andric         }
97010b57cec5SDimitry Andric         if (isLittleEndian != sys::IsLittleEndianHost)
97020b57cec5SDimitry Andric           swapStruct(ts);
97030b57cec5SDimitry Andric         if (ts.tsh.flavor == MachO::x86_THREAD_STATE32) {
97040b57cec5SDimitry Andric           outs() << "\t    tsh.flavor x86_THREAD_STATE32 ";
97050b57cec5SDimitry Andric           if (ts.tsh.count == MachO::x86_THREAD_STATE32_COUNT)
97060b57cec5SDimitry Andric             outs() << "tsh.count x86_THREAD_STATE32_COUNT\n";
97070b57cec5SDimitry Andric           else
97080b57cec5SDimitry Andric             outs() << "tsh.count " << ts.tsh.count
97090b57cec5SDimitry Andric                    << " (not x86_THREAD_STATE32_COUNT\n";
97100b57cec5SDimitry Andric           Print_x86_thread_state32_t(ts.uts.ts32);
97110b57cec5SDimitry Andric         } else {
97120b57cec5SDimitry Andric           outs() << "\t    tsh.flavor " << ts.tsh.flavor << "  tsh.count "
97130b57cec5SDimitry Andric                  << ts.tsh.count << "\n";
97140b57cec5SDimitry Andric         }
97150b57cec5SDimitry Andric       } else {
97160b57cec5SDimitry Andric         outs() << "     flavor " << flavor << " (unknown)\n";
97170b57cec5SDimitry Andric         outs() << "      count " << count << "\n";
97180b57cec5SDimitry Andric         outs() << "      state (unknown)\n";
97190b57cec5SDimitry Andric         begin += count * sizeof(uint32_t);
97200b57cec5SDimitry Andric       }
97210b57cec5SDimitry Andric     }
97220b57cec5SDimitry Andric   } else if (cputype == MachO::CPU_TYPE_X86_64) {
97230b57cec5SDimitry Andric     while (begin < end) {
97240b57cec5SDimitry Andric       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
97250b57cec5SDimitry Andric         memcpy((char *)&flavor, begin, sizeof(uint32_t));
97260b57cec5SDimitry Andric         begin += sizeof(uint32_t);
97270b57cec5SDimitry Andric       } else {
97280b57cec5SDimitry Andric         flavor = 0;
97290b57cec5SDimitry Andric         begin = end;
97300b57cec5SDimitry Andric       }
97310b57cec5SDimitry Andric       if (isLittleEndian != sys::IsLittleEndianHost)
97320b57cec5SDimitry Andric         sys::swapByteOrder(flavor);
97330b57cec5SDimitry Andric       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
97340b57cec5SDimitry Andric         memcpy((char *)&count, begin, sizeof(uint32_t));
97350b57cec5SDimitry Andric         begin += sizeof(uint32_t);
97360b57cec5SDimitry Andric       } else {
97370b57cec5SDimitry Andric         count = 0;
97380b57cec5SDimitry Andric         begin = end;
97390b57cec5SDimitry Andric       }
97400b57cec5SDimitry Andric       if (isLittleEndian != sys::IsLittleEndianHost)
97410b57cec5SDimitry Andric         sys::swapByteOrder(count);
97420b57cec5SDimitry Andric       if (flavor == MachO::x86_THREAD_STATE64) {
97430b57cec5SDimitry Andric         outs() << "     flavor x86_THREAD_STATE64\n";
97440b57cec5SDimitry Andric         if (count == MachO::x86_THREAD_STATE64_COUNT)
97450b57cec5SDimitry Andric           outs() << "      count x86_THREAD_STATE64_COUNT\n";
97460b57cec5SDimitry Andric         else
97470b57cec5SDimitry Andric           outs() << "      count " << count
97480b57cec5SDimitry Andric                  << " (not x86_THREAD_STATE64_COUNT)\n";
97490b57cec5SDimitry Andric         MachO::x86_thread_state64_t cpu64;
97500b57cec5SDimitry Andric         left = end - begin;
97510b57cec5SDimitry Andric         if (left >= sizeof(MachO::x86_thread_state64_t)) {
97520b57cec5SDimitry Andric           memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
97530b57cec5SDimitry Andric           begin += sizeof(MachO::x86_thread_state64_t);
97540b57cec5SDimitry Andric         } else {
97550b57cec5SDimitry Andric           memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
97560b57cec5SDimitry Andric           memcpy(&cpu64, begin, left);
97570b57cec5SDimitry Andric           begin += left;
97580b57cec5SDimitry Andric         }
97590b57cec5SDimitry Andric         if (isLittleEndian != sys::IsLittleEndianHost)
97600b57cec5SDimitry Andric           swapStruct(cpu64);
97610b57cec5SDimitry Andric         Print_x86_thread_state64_t(cpu64);
97620b57cec5SDimitry Andric       } else if (flavor == MachO::x86_THREAD_STATE) {
97630b57cec5SDimitry Andric         outs() << "     flavor x86_THREAD_STATE\n";
97640b57cec5SDimitry Andric         if (count == MachO::x86_THREAD_STATE_COUNT)
97650b57cec5SDimitry Andric           outs() << "      count x86_THREAD_STATE_COUNT\n";
97660b57cec5SDimitry Andric         else
97670b57cec5SDimitry Andric           outs() << "      count " << count
97680b57cec5SDimitry Andric                  << " (not x86_THREAD_STATE_COUNT)\n";
97690b57cec5SDimitry Andric         struct MachO::x86_thread_state_t ts;
97700b57cec5SDimitry Andric         left = end - begin;
97710b57cec5SDimitry Andric         if (left >= sizeof(MachO::x86_thread_state_t)) {
97720b57cec5SDimitry Andric           memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
97730b57cec5SDimitry Andric           begin += sizeof(MachO::x86_thread_state_t);
97740b57cec5SDimitry Andric         } else {
97750b57cec5SDimitry Andric           memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
97760b57cec5SDimitry Andric           memcpy(&ts, begin, left);
97770b57cec5SDimitry Andric           begin += left;
97780b57cec5SDimitry Andric         }
97790b57cec5SDimitry Andric         if (isLittleEndian != sys::IsLittleEndianHost)
97800b57cec5SDimitry Andric           swapStruct(ts);
97810b57cec5SDimitry Andric         if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
97820b57cec5SDimitry Andric           outs() << "\t    tsh.flavor x86_THREAD_STATE64 ";
97830b57cec5SDimitry Andric           if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
97840b57cec5SDimitry Andric             outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
97850b57cec5SDimitry Andric           else
97860b57cec5SDimitry Andric             outs() << "tsh.count " << ts.tsh.count
97870b57cec5SDimitry Andric                    << " (not x86_THREAD_STATE64_COUNT\n";
97880b57cec5SDimitry Andric           Print_x86_thread_state64_t(ts.uts.ts64);
97890b57cec5SDimitry Andric         } else {
97900b57cec5SDimitry Andric           outs() << "\t    tsh.flavor " << ts.tsh.flavor << "  tsh.count "
97910b57cec5SDimitry Andric                  << ts.tsh.count << "\n";
97920b57cec5SDimitry Andric         }
97930b57cec5SDimitry Andric       } else if (flavor == MachO::x86_FLOAT_STATE) {
97940b57cec5SDimitry Andric         outs() << "     flavor x86_FLOAT_STATE\n";
97950b57cec5SDimitry Andric         if (count == MachO::x86_FLOAT_STATE_COUNT)
97960b57cec5SDimitry Andric           outs() << "      count x86_FLOAT_STATE_COUNT\n";
97970b57cec5SDimitry Andric         else
97980b57cec5SDimitry Andric           outs() << "      count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
97990b57cec5SDimitry Andric         struct MachO::x86_float_state_t fs;
98000b57cec5SDimitry Andric         left = end - begin;
98010b57cec5SDimitry Andric         if (left >= sizeof(MachO::x86_float_state_t)) {
98020b57cec5SDimitry Andric           memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
98030b57cec5SDimitry Andric           begin += sizeof(MachO::x86_float_state_t);
98040b57cec5SDimitry Andric         } else {
98050b57cec5SDimitry Andric           memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
98060b57cec5SDimitry Andric           memcpy(&fs, begin, left);
98070b57cec5SDimitry Andric           begin += left;
98080b57cec5SDimitry Andric         }
98090b57cec5SDimitry Andric         if (isLittleEndian != sys::IsLittleEndianHost)
98100b57cec5SDimitry Andric           swapStruct(fs);
98110b57cec5SDimitry Andric         if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
98120b57cec5SDimitry Andric           outs() << "\t    fsh.flavor x86_FLOAT_STATE64 ";
98130b57cec5SDimitry Andric           if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
98140b57cec5SDimitry Andric             outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
98150b57cec5SDimitry Andric           else
98160b57cec5SDimitry Andric             outs() << "fsh.count " << fs.fsh.count
98170b57cec5SDimitry Andric                    << " (not x86_FLOAT_STATE64_COUNT\n";
98180b57cec5SDimitry Andric           Print_x86_float_state_t(fs.ufs.fs64);
98190b57cec5SDimitry Andric         } else {
98200b57cec5SDimitry Andric           outs() << "\t    fsh.flavor " << fs.fsh.flavor << "  fsh.count "
98210b57cec5SDimitry Andric                  << fs.fsh.count << "\n";
98220b57cec5SDimitry Andric         }
98230b57cec5SDimitry Andric       } else if (flavor == MachO::x86_EXCEPTION_STATE) {
98240b57cec5SDimitry Andric         outs() << "     flavor x86_EXCEPTION_STATE\n";
98250b57cec5SDimitry Andric         if (count == MachO::x86_EXCEPTION_STATE_COUNT)
98260b57cec5SDimitry Andric           outs() << "      count x86_EXCEPTION_STATE_COUNT\n";
98270b57cec5SDimitry Andric         else
98280b57cec5SDimitry Andric           outs() << "      count " << count
98290b57cec5SDimitry Andric                  << " (not x86_EXCEPTION_STATE_COUNT)\n";
98300b57cec5SDimitry Andric         struct MachO::x86_exception_state_t es;
98310b57cec5SDimitry Andric         left = end - begin;
98320b57cec5SDimitry Andric         if (left >= sizeof(MachO::x86_exception_state_t)) {
98330b57cec5SDimitry Andric           memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
98340b57cec5SDimitry Andric           begin += sizeof(MachO::x86_exception_state_t);
98350b57cec5SDimitry Andric         } else {
98360b57cec5SDimitry Andric           memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
98370b57cec5SDimitry Andric           memcpy(&es, begin, left);
98380b57cec5SDimitry Andric           begin += left;
98390b57cec5SDimitry Andric         }
98400b57cec5SDimitry Andric         if (isLittleEndian != sys::IsLittleEndianHost)
98410b57cec5SDimitry Andric           swapStruct(es);
98420b57cec5SDimitry Andric         if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
98430b57cec5SDimitry Andric           outs() << "\t    esh.flavor x86_EXCEPTION_STATE64\n";
98440b57cec5SDimitry Andric           if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
98450b57cec5SDimitry Andric             outs() << "\t    esh.count x86_EXCEPTION_STATE64_COUNT\n";
98460b57cec5SDimitry Andric           else
98470b57cec5SDimitry Andric             outs() << "\t    esh.count " << es.esh.count
98480b57cec5SDimitry Andric                    << " (not x86_EXCEPTION_STATE64_COUNT\n";
98490b57cec5SDimitry Andric           Print_x86_exception_state_t(es.ues.es64);
98500b57cec5SDimitry Andric         } else {
98510b57cec5SDimitry Andric           outs() << "\t    esh.flavor " << es.esh.flavor << "  esh.count "
98520b57cec5SDimitry Andric                  << es.esh.count << "\n";
98530b57cec5SDimitry Andric         }
98540b57cec5SDimitry Andric       } else if (flavor == MachO::x86_EXCEPTION_STATE64) {
98550b57cec5SDimitry Andric         outs() << "     flavor x86_EXCEPTION_STATE64\n";
98560b57cec5SDimitry Andric         if (count == MachO::x86_EXCEPTION_STATE64_COUNT)
98570b57cec5SDimitry Andric           outs() << "      count x86_EXCEPTION_STATE64_COUNT\n";
98580b57cec5SDimitry Andric         else
98590b57cec5SDimitry Andric           outs() << "      count " << count
98600b57cec5SDimitry Andric                  << " (not x86_EXCEPTION_STATE64_COUNT)\n";
98610b57cec5SDimitry Andric         struct MachO::x86_exception_state64_t es64;
98620b57cec5SDimitry Andric         left = end - begin;
98630b57cec5SDimitry Andric         if (left >= sizeof(MachO::x86_exception_state64_t)) {
98640b57cec5SDimitry Andric           memcpy(&es64, begin, sizeof(MachO::x86_exception_state64_t));
98650b57cec5SDimitry Andric           begin += sizeof(MachO::x86_exception_state64_t);
98660b57cec5SDimitry Andric         } else {
98670b57cec5SDimitry Andric           memset(&es64, '\0', sizeof(MachO::x86_exception_state64_t));
98680b57cec5SDimitry Andric           memcpy(&es64, begin, left);
98690b57cec5SDimitry Andric           begin += left;
98700b57cec5SDimitry Andric         }
98710b57cec5SDimitry Andric         if (isLittleEndian != sys::IsLittleEndianHost)
98720b57cec5SDimitry Andric           swapStruct(es64);
98730b57cec5SDimitry Andric         Print_x86_exception_state_t(es64);
98740b57cec5SDimitry Andric       } else {
98750b57cec5SDimitry Andric         outs() << "     flavor " << flavor << " (unknown)\n";
98760b57cec5SDimitry Andric         outs() << "      count " << count << "\n";
98770b57cec5SDimitry Andric         outs() << "      state (unknown)\n";
98780b57cec5SDimitry Andric         begin += count * sizeof(uint32_t);
98790b57cec5SDimitry Andric       }
98800b57cec5SDimitry Andric     }
98810b57cec5SDimitry Andric   } else if (cputype == MachO::CPU_TYPE_ARM) {
98820b57cec5SDimitry Andric     while (begin < end) {
98830b57cec5SDimitry Andric       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
98840b57cec5SDimitry Andric         memcpy((char *)&flavor, begin, sizeof(uint32_t));
98850b57cec5SDimitry Andric         begin += sizeof(uint32_t);
98860b57cec5SDimitry Andric       } else {
98870b57cec5SDimitry Andric         flavor = 0;
98880b57cec5SDimitry Andric         begin = end;
98890b57cec5SDimitry Andric       }
98900b57cec5SDimitry Andric       if (isLittleEndian != sys::IsLittleEndianHost)
98910b57cec5SDimitry Andric         sys::swapByteOrder(flavor);
98920b57cec5SDimitry Andric       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
98930b57cec5SDimitry Andric         memcpy((char *)&count, begin, sizeof(uint32_t));
98940b57cec5SDimitry Andric         begin += sizeof(uint32_t);
98950b57cec5SDimitry Andric       } else {
98960b57cec5SDimitry Andric         count = 0;
98970b57cec5SDimitry Andric         begin = end;
98980b57cec5SDimitry Andric       }
98990b57cec5SDimitry Andric       if (isLittleEndian != sys::IsLittleEndianHost)
99000b57cec5SDimitry Andric         sys::swapByteOrder(count);
99010b57cec5SDimitry Andric       if (flavor == MachO::ARM_THREAD_STATE) {
99020b57cec5SDimitry Andric         outs() << "     flavor ARM_THREAD_STATE\n";
99030b57cec5SDimitry Andric         if (count == MachO::ARM_THREAD_STATE_COUNT)
99040b57cec5SDimitry Andric           outs() << "      count ARM_THREAD_STATE_COUNT\n";
99050b57cec5SDimitry Andric         else
99060b57cec5SDimitry Andric           outs() << "      count " << count
99070b57cec5SDimitry Andric                  << " (not ARM_THREAD_STATE_COUNT)\n";
99080b57cec5SDimitry Andric         MachO::arm_thread_state32_t cpu32;
99090b57cec5SDimitry Andric         left = end - begin;
99100b57cec5SDimitry Andric         if (left >= sizeof(MachO::arm_thread_state32_t)) {
99110b57cec5SDimitry Andric           memcpy(&cpu32, begin, sizeof(MachO::arm_thread_state32_t));
99120b57cec5SDimitry Andric           begin += sizeof(MachO::arm_thread_state32_t);
99130b57cec5SDimitry Andric         } else {
99140b57cec5SDimitry Andric           memset(&cpu32, '\0', sizeof(MachO::arm_thread_state32_t));
99150b57cec5SDimitry Andric           memcpy(&cpu32, begin, left);
99160b57cec5SDimitry Andric           begin += left;
99170b57cec5SDimitry Andric         }
99180b57cec5SDimitry Andric         if (isLittleEndian != sys::IsLittleEndianHost)
99190b57cec5SDimitry Andric           swapStruct(cpu32);
99200b57cec5SDimitry Andric         Print_arm_thread_state32_t(cpu32);
99210b57cec5SDimitry Andric       } else {
99220b57cec5SDimitry Andric         outs() << "     flavor " << flavor << " (unknown)\n";
99230b57cec5SDimitry Andric         outs() << "      count " << count << "\n";
99240b57cec5SDimitry Andric         outs() << "      state (unknown)\n";
99250b57cec5SDimitry Andric         begin += count * sizeof(uint32_t);
99260b57cec5SDimitry Andric       }
99270b57cec5SDimitry Andric     }
99280b57cec5SDimitry Andric   } else if (cputype == MachO::CPU_TYPE_ARM64 ||
99290b57cec5SDimitry Andric              cputype == MachO::CPU_TYPE_ARM64_32) {
99300b57cec5SDimitry Andric     while (begin < end) {
99310b57cec5SDimitry Andric       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
99320b57cec5SDimitry Andric         memcpy((char *)&flavor, begin, sizeof(uint32_t));
99330b57cec5SDimitry Andric         begin += sizeof(uint32_t);
99340b57cec5SDimitry Andric       } else {
99350b57cec5SDimitry Andric         flavor = 0;
99360b57cec5SDimitry Andric         begin = end;
99370b57cec5SDimitry Andric       }
99380b57cec5SDimitry Andric       if (isLittleEndian != sys::IsLittleEndianHost)
99390b57cec5SDimitry Andric         sys::swapByteOrder(flavor);
99400b57cec5SDimitry Andric       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
99410b57cec5SDimitry Andric         memcpy((char *)&count, begin, sizeof(uint32_t));
99420b57cec5SDimitry Andric         begin += sizeof(uint32_t);
99430b57cec5SDimitry Andric       } else {
99440b57cec5SDimitry Andric         count = 0;
99450b57cec5SDimitry Andric         begin = end;
99460b57cec5SDimitry Andric       }
99470b57cec5SDimitry Andric       if (isLittleEndian != sys::IsLittleEndianHost)
99480b57cec5SDimitry Andric         sys::swapByteOrder(count);
99490b57cec5SDimitry Andric       if (flavor == MachO::ARM_THREAD_STATE64) {
99500b57cec5SDimitry Andric         outs() << "     flavor ARM_THREAD_STATE64\n";
99510b57cec5SDimitry Andric         if (count == MachO::ARM_THREAD_STATE64_COUNT)
99520b57cec5SDimitry Andric           outs() << "      count ARM_THREAD_STATE64_COUNT\n";
99530b57cec5SDimitry Andric         else
99540b57cec5SDimitry Andric           outs() << "      count " << count
99550b57cec5SDimitry Andric                  << " (not ARM_THREAD_STATE64_COUNT)\n";
99560b57cec5SDimitry Andric         MachO::arm_thread_state64_t cpu64;
99570b57cec5SDimitry Andric         left = end - begin;
99580b57cec5SDimitry Andric         if (left >= sizeof(MachO::arm_thread_state64_t)) {
99590b57cec5SDimitry Andric           memcpy(&cpu64, begin, sizeof(MachO::arm_thread_state64_t));
99600b57cec5SDimitry Andric           begin += sizeof(MachO::arm_thread_state64_t);
99610b57cec5SDimitry Andric         } else {
99620b57cec5SDimitry Andric           memset(&cpu64, '\0', sizeof(MachO::arm_thread_state64_t));
99630b57cec5SDimitry Andric           memcpy(&cpu64, begin, left);
99640b57cec5SDimitry Andric           begin += left;
99650b57cec5SDimitry Andric         }
99660b57cec5SDimitry Andric         if (isLittleEndian != sys::IsLittleEndianHost)
99670b57cec5SDimitry Andric           swapStruct(cpu64);
99680b57cec5SDimitry Andric         Print_arm_thread_state64_t(cpu64);
99690b57cec5SDimitry Andric       } else {
99700b57cec5SDimitry Andric         outs() << "     flavor " << flavor << " (unknown)\n";
99710b57cec5SDimitry Andric         outs() << "      count " << count << "\n";
99720b57cec5SDimitry Andric         outs() << "      state (unknown)\n";
99730b57cec5SDimitry Andric         begin += count * sizeof(uint32_t);
99740b57cec5SDimitry Andric       }
99750b57cec5SDimitry Andric     }
99760b57cec5SDimitry Andric   } else {
99770b57cec5SDimitry Andric     while (begin < end) {
99780b57cec5SDimitry Andric       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
99790b57cec5SDimitry Andric         memcpy((char *)&flavor, begin, sizeof(uint32_t));
99800b57cec5SDimitry Andric         begin += sizeof(uint32_t);
99810b57cec5SDimitry Andric       } else {
99820b57cec5SDimitry Andric         flavor = 0;
99830b57cec5SDimitry Andric         begin = end;
99840b57cec5SDimitry Andric       }
99850b57cec5SDimitry Andric       if (isLittleEndian != sys::IsLittleEndianHost)
99860b57cec5SDimitry Andric         sys::swapByteOrder(flavor);
99870b57cec5SDimitry Andric       if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
99880b57cec5SDimitry Andric         memcpy((char *)&count, begin, sizeof(uint32_t));
99890b57cec5SDimitry Andric         begin += sizeof(uint32_t);
99900b57cec5SDimitry Andric       } else {
99910b57cec5SDimitry Andric         count = 0;
99920b57cec5SDimitry Andric         begin = end;
99930b57cec5SDimitry Andric       }
99940b57cec5SDimitry Andric       if (isLittleEndian != sys::IsLittleEndianHost)
99950b57cec5SDimitry Andric         sys::swapByteOrder(count);
99960b57cec5SDimitry Andric       outs() << "     flavor " << flavor << "\n";
99970b57cec5SDimitry Andric       outs() << "      count " << count << "\n";
99980b57cec5SDimitry Andric       outs() << "      state (Unknown cputype/cpusubtype)\n";
99990b57cec5SDimitry Andric       begin += count * sizeof(uint32_t);
100000b57cec5SDimitry Andric     }
100010b57cec5SDimitry Andric   }
100020b57cec5SDimitry Andric }
100030b57cec5SDimitry Andric 
PrintDylibCommand(MachO::dylib_command dl,const char * Ptr)100040b57cec5SDimitry Andric static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
100050b57cec5SDimitry Andric   if (dl.cmd == MachO::LC_ID_DYLIB)
100060b57cec5SDimitry Andric     outs() << "          cmd LC_ID_DYLIB\n";
100070b57cec5SDimitry Andric   else if (dl.cmd == MachO::LC_LOAD_DYLIB)
100080b57cec5SDimitry Andric     outs() << "          cmd LC_LOAD_DYLIB\n";
100090b57cec5SDimitry Andric   else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
100100b57cec5SDimitry Andric     outs() << "          cmd LC_LOAD_WEAK_DYLIB\n";
100110b57cec5SDimitry Andric   else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
100120b57cec5SDimitry Andric     outs() << "          cmd LC_REEXPORT_DYLIB\n";
100130b57cec5SDimitry Andric   else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
100140b57cec5SDimitry Andric     outs() << "          cmd LC_LAZY_LOAD_DYLIB\n";
100150b57cec5SDimitry Andric   else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
100160b57cec5SDimitry Andric     outs() << "          cmd LC_LOAD_UPWARD_DYLIB\n";
100170b57cec5SDimitry Andric   else
100180b57cec5SDimitry Andric     outs() << "          cmd " << dl.cmd << " (unknown)\n";
100190b57cec5SDimitry Andric   outs() << "      cmdsize " << dl.cmdsize;
100200b57cec5SDimitry Andric   if (dl.cmdsize < sizeof(struct MachO::dylib_command))
100210b57cec5SDimitry Andric     outs() << " Incorrect size\n";
100220b57cec5SDimitry Andric   else
100230b57cec5SDimitry Andric     outs() << "\n";
100240b57cec5SDimitry Andric   if (dl.dylib.name < dl.cmdsize) {
100250b57cec5SDimitry Andric     const char *P = (const char *)(Ptr) + dl.dylib.name;
100260b57cec5SDimitry Andric     outs() << "         name " << P << " (offset " << dl.dylib.name << ")\n";
100270b57cec5SDimitry Andric   } else {
100280b57cec5SDimitry Andric     outs() << "         name ?(bad offset " << dl.dylib.name << ")\n";
100290b57cec5SDimitry Andric   }
100300b57cec5SDimitry Andric   outs() << "   time stamp " << dl.dylib.timestamp << " ";
100310b57cec5SDimitry Andric   time_t t = dl.dylib.timestamp;
100320b57cec5SDimitry Andric   outs() << ctime(&t);
100330b57cec5SDimitry Andric   outs() << "      current version ";
100340b57cec5SDimitry Andric   if (dl.dylib.current_version == 0xffffffff)
100350b57cec5SDimitry Andric     outs() << "n/a\n";
100360b57cec5SDimitry Andric   else
100370b57cec5SDimitry Andric     outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
100380b57cec5SDimitry Andric            << ((dl.dylib.current_version >> 8) & 0xff) << "."
100390b57cec5SDimitry Andric            << (dl.dylib.current_version & 0xff) << "\n";
100400b57cec5SDimitry Andric   outs() << "compatibility version ";
100410b57cec5SDimitry Andric   if (dl.dylib.compatibility_version == 0xffffffff)
100420b57cec5SDimitry Andric     outs() << "n/a\n";
100430b57cec5SDimitry Andric   else
100440b57cec5SDimitry Andric     outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
100450b57cec5SDimitry Andric            << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
100460b57cec5SDimitry Andric            << (dl.dylib.compatibility_version & 0xff) << "\n";
100470b57cec5SDimitry Andric }
100480b57cec5SDimitry Andric 
PrintLinkEditDataCommand(MachO::linkedit_data_command ld,uint32_t object_size)100490b57cec5SDimitry Andric static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
100500b57cec5SDimitry Andric                                      uint32_t object_size) {
100510b57cec5SDimitry Andric   if (ld.cmd == MachO::LC_CODE_SIGNATURE)
100520b57cec5SDimitry Andric     outs() << "      cmd LC_CODE_SIGNATURE\n";
100530b57cec5SDimitry Andric   else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
100540b57cec5SDimitry Andric     outs() << "      cmd LC_SEGMENT_SPLIT_INFO\n";
100550b57cec5SDimitry Andric   else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
100560b57cec5SDimitry Andric     outs() << "      cmd LC_FUNCTION_STARTS\n";
100570b57cec5SDimitry Andric   else if (ld.cmd == MachO::LC_DATA_IN_CODE)
100580b57cec5SDimitry Andric     outs() << "      cmd LC_DATA_IN_CODE\n";
100590b57cec5SDimitry Andric   else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
100600b57cec5SDimitry Andric     outs() << "      cmd LC_DYLIB_CODE_SIGN_DRS\n";
100610b57cec5SDimitry Andric   else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
100620b57cec5SDimitry Andric     outs() << "      cmd LC_LINKER_OPTIMIZATION_HINT\n";
10063349cc55cSDimitry Andric   else if (ld.cmd == MachO::LC_DYLD_EXPORTS_TRIE)
10064349cc55cSDimitry Andric     outs() << "      cmd LC_DYLD_EXPORTS_TRIE\n";
10065349cc55cSDimitry Andric   else if (ld.cmd == MachO::LC_DYLD_CHAINED_FIXUPS)
10066349cc55cSDimitry Andric     outs() << "      cmd LC_DYLD_CHAINED_FIXUPS\n";
1006706c3fb27SDimitry Andric   else if (ld.cmd == MachO::LC_ATOM_INFO)
1006806c3fb27SDimitry Andric     outs() << "      cmd LC_ATOM_INFO\n";
100690b57cec5SDimitry Andric   else
100700b57cec5SDimitry Andric     outs() << "      cmd " << ld.cmd << " (?)\n";
100710b57cec5SDimitry Andric   outs() << "  cmdsize " << ld.cmdsize;
100720b57cec5SDimitry Andric   if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
100730b57cec5SDimitry Andric     outs() << " Incorrect size\n";
100740b57cec5SDimitry Andric   else
100750b57cec5SDimitry Andric     outs() << "\n";
100760b57cec5SDimitry Andric   outs() << "  dataoff " << ld.dataoff;
100770b57cec5SDimitry Andric   if (ld.dataoff > object_size)
100780b57cec5SDimitry Andric     outs() << " (past end of file)\n";
100790b57cec5SDimitry Andric   else
100800b57cec5SDimitry Andric     outs() << "\n";
100810b57cec5SDimitry Andric   outs() << " datasize " << ld.datasize;
100820b57cec5SDimitry Andric   uint64_t big_size = ld.dataoff;
100830b57cec5SDimitry Andric   big_size += ld.datasize;
100840b57cec5SDimitry Andric   if (big_size > object_size)
100850b57cec5SDimitry Andric     outs() << " (past end of file)\n";
100860b57cec5SDimitry Andric   else
100870b57cec5SDimitry Andric     outs() << "\n";
100880b57cec5SDimitry Andric }
100890b57cec5SDimitry Andric 
PrintLoadCommands(const MachOObjectFile * Obj,uint32_t filetype,uint32_t cputype,bool verbose)100900b57cec5SDimitry Andric static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
100910b57cec5SDimitry Andric                               uint32_t cputype, bool verbose) {
100920b57cec5SDimitry Andric   StringRef Buf = Obj->getData();
100930b57cec5SDimitry Andric   unsigned Index = 0;
100940b57cec5SDimitry Andric   for (const auto &Command : Obj->load_commands()) {
100950b57cec5SDimitry Andric     outs() << "Load command " << Index++ << "\n";
100960b57cec5SDimitry Andric     if (Command.C.cmd == MachO::LC_SEGMENT) {
100970b57cec5SDimitry Andric       MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
100980b57cec5SDimitry Andric       const char *sg_segname = SLC.segname;
100990b57cec5SDimitry Andric       PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
101000b57cec5SDimitry Andric                           SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
101010b57cec5SDimitry Andric                           SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
101020b57cec5SDimitry Andric                           verbose);
101030b57cec5SDimitry Andric       for (unsigned j = 0; j < SLC.nsects; j++) {
101040b57cec5SDimitry Andric         MachO::section S = Obj->getSection(Command, j);
101050b57cec5SDimitry Andric         PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
101060b57cec5SDimitry Andric                      S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
101070b57cec5SDimitry Andric                      SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
101080b57cec5SDimitry Andric       }
101090b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
101100b57cec5SDimitry Andric       MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
101110b57cec5SDimitry Andric       const char *sg_segname = SLC_64.segname;
101120b57cec5SDimitry Andric       PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
101130b57cec5SDimitry Andric                           SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
101140b57cec5SDimitry Andric                           SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
101150b57cec5SDimitry Andric                           SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
101160b57cec5SDimitry Andric       for (unsigned j = 0; j < SLC_64.nsects; j++) {
101170b57cec5SDimitry Andric         MachO::section_64 S_64 = Obj->getSection64(Command, j);
101180b57cec5SDimitry Andric         PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
101190b57cec5SDimitry Andric                      S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
101200b57cec5SDimitry Andric                      S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
101210b57cec5SDimitry Andric                      sg_segname, filetype, Buf.size(), verbose);
101220b57cec5SDimitry Andric       }
101230b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_SYMTAB) {
101240b57cec5SDimitry Andric       MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
101250b57cec5SDimitry Andric       PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
101260b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
101270b57cec5SDimitry Andric       MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
101280b57cec5SDimitry Andric       MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
101290b57cec5SDimitry Andric       PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
101300b57cec5SDimitry Andric                                Obj->is64Bit());
101310b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
101320b57cec5SDimitry Andric                Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
101330b57cec5SDimitry Andric       MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
101340b57cec5SDimitry Andric       PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
101350b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
101360b57cec5SDimitry Andric                Command.C.cmd == MachO::LC_ID_DYLINKER ||
101370b57cec5SDimitry Andric                Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
101380b57cec5SDimitry Andric       MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
101390b57cec5SDimitry Andric       PrintDyldLoadCommand(Dyld, Command.Ptr);
101400b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_UUID) {
101410b57cec5SDimitry Andric       MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
101420b57cec5SDimitry Andric       PrintUuidLoadCommand(Uuid);
101430b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_RPATH) {
101440b57cec5SDimitry Andric       MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
101450b57cec5SDimitry Andric       PrintRpathLoadCommand(Rpath, Command.Ptr);
101460b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
101470b57cec5SDimitry Andric                Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS ||
101480b57cec5SDimitry Andric                Command.C.cmd == MachO::LC_VERSION_MIN_TVOS ||
101490b57cec5SDimitry Andric                Command.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) {
101500b57cec5SDimitry Andric       MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
101510b57cec5SDimitry Andric       PrintVersionMinLoadCommand(Vd);
101520b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_NOTE) {
101530b57cec5SDimitry Andric       MachO::note_command Nt = Obj->getNoteLoadCommand(Command);
101540b57cec5SDimitry Andric       PrintNoteLoadCommand(Nt);
101550b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_BUILD_VERSION) {
101560b57cec5SDimitry Andric       MachO::build_version_command Bv =
101570b57cec5SDimitry Andric           Obj->getBuildVersionLoadCommand(Command);
1015881ad6265SDimitry Andric       PrintBuildVersionLoadCommand(Obj, Bv, verbose);
101590b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
101600b57cec5SDimitry Andric       MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
101610b57cec5SDimitry Andric       PrintSourceVersionCommand(Sd);
101620b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_MAIN) {
101630b57cec5SDimitry Andric       MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
101640b57cec5SDimitry Andric       PrintEntryPointCommand(Ep);
101650b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
101660b57cec5SDimitry Andric       MachO::encryption_info_command Ei =
101670b57cec5SDimitry Andric           Obj->getEncryptionInfoCommand(Command);
101680b57cec5SDimitry Andric       PrintEncryptionInfoCommand(Ei, Buf.size());
101690b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
101700b57cec5SDimitry Andric       MachO::encryption_info_command_64 Ei =
101710b57cec5SDimitry Andric           Obj->getEncryptionInfoCommand64(Command);
101720b57cec5SDimitry Andric       PrintEncryptionInfoCommand64(Ei, Buf.size());
101730b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
101740b57cec5SDimitry Andric       MachO::linker_option_command Lo =
101750b57cec5SDimitry Andric           Obj->getLinkerOptionLoadCommand(Command);
101760b57cec5SDimitry Andric       PrintLinkerOptionCommand(Lo, Command.Ptr);
101770b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
101780b57cec5SDimitry Andric       MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
101790b57cec5SDimitry Andric       PrintSubFrameworkCommand(Sf, Command.Ptr);
101800b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
101810b57cec5SDimitry Andric       MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
101820b57cec5SDimitry Andric       PrintSubUmbrellaCommand(Sf, Command.Ptr);
101830b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
101840b57cec5SDimitry Andric       MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
101850b57cec5SDimitry Andric       PrintSubLibraryCommand(Sl, Command.Ptr);
101860b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
101870b57cec5SDimitry Andric       MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
101880b57cec5SDimitry Andric       PrintSubClientCommand(Sc, Command.Ptr);
101890b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_ROUTINES) {
101900b57cec5SDimitry Andric       MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
101910b57cec5SDimitry Andric       PrintRoutinesCommand(Rc);
101920b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
101930b57cec5SDimitry Andric       MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
101940b57cec5SDimitry Andric       PrintRoutinesCommand64(Rc);
101950b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_THREAD ||
101960b57cec5SDimitry Andric                Command.C.cmd == MachO::LC_UNIXTHREAD) {
101970b57cec5SDimitry Andric       MachO::thread_command Tc = Obj->getThreadCommand(Command);
101980b57cec5SDimitry Andric       PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
101990b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
102000b57cec5SDimitry Andric                Command.C.cmd == MachO::LC_ID_DYLIB ||
102010b57cec5SDimitry Andric                Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
102020b57cec5SDimitry Andric                Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
102030b57cec5SDimitry Andric                Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
102040b57cec5SDimitry Andric                Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
102050b57cec5SDimitry Andric       MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
102060b57cec5SDimitry Andric       PrintDylibCommand(Dl, Command.Ptr);
102070b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
102080b57cec5SDimitry Andric                Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
102090b57cec5SDimitry Andric                Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
102100b57cec5SDimitry Andric                Command.C.cmd == MachO::LC_DATA_IN_CODE ||
102110b57cec5SDimitry Andric                Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
10212349cc55cSDimitry Andric                Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT ||
10213349cc55cSDimitry Andric                Command.C.cmd == MachO::LC_DYLD_EXPORTS_TRIE ||
1021406c3fb27SDimitry Andric                Command.C.cmd == MachO::LC_DYLD_CHAINED_FIXUPS ||
1021506c3fb27SDimitry Andric                Command.C.cmd == MachO::LC_ATOM_INFO) {
102160b57cec5SDimitry Andric       MachO::linkedit_data_command Ld =
102170b57cec5SDimitry Andric           Obj->getLinkeditDataLoadCommand(Command);
102180b57cec5SDimitry Andric       PrintLinkEditDataCommand(Ld, Buf.size());
102190b57cec5SDimitry Andric     } else {
102200b57cec5SDimitry Andric       outs() << "      cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
102210b57cec5SDimitry Andric              << ")\n";
102220b57cec5SDimitry Andric       outs() << "  cmdsize " << Command.C.cmdsize << "\n";
102230b57cec5SDimitry Andric       // TODO: get and print the raw bytes of the load command.
102240b57cec5SDimitry Andric     }
102250b57cec5SDimitry Andric     // TODO: print all the other kinds of load commands.
102260b57cec5SDimitry Andric   }
102270b57cec5SDimitry Andric }
102280b57cec5SDimitry Andric 
PrintMachHeader(const MachOObjectFile * Obj,bool verbose)102290b57cec5SDimitry Andric static void PrintMachHeader(const MachOObjectFile *Obj, bool verbose) {
102300b57cec5SDimitry Andric   if (Obj->is64Bit()) {
102310b57cec5SDimitry Andric     MachO::mach_header_64 H_64;
102320b57cec5SDimitry Andric     H_64 = Obj->getHeader64();
102330b57cec5SDimitry Andric     PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
102340b57cec5SDimitry Andric                     H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
102350b57cec5SDimitry Andric   } else {
102360b57cec5SDimitry Andric     MachO::mach_header H;
102370b57cec5SDimitry Andric     H = Obj->getHeader();
102380b57cec5SDimitry Andric     PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
102390b57cec5SDimitry Andric                     H.sizeofcmds, H.flags, verbose);
102400b57cec5SDimitry Andric   }
102410b57cec5SDimitry Andric }
102420b57cec5SDimitry Andric 
printMachOFileHeader(const object::ObjectFile * Obj)102435ffd83dbSDimitry Andric void objdump::printMachOFileHeader(const object::ObjectFile *Obj) {
1024404eeddc0SDimitry Andric   const MachOObjectFile *file = cast<const MachOObjectFile>(Obj);
10245fe6060f1SDimitry Andric   PrintMachHeader(file, Verbose);
102460b57cec5SDimitry Andric }
102470b57cec5SDimitry Andric 
printPrivateHeaders()102485f757f3fSDimitry Andric void MachODumper::printPrivateHeaders() {
1024906c3fb27SDimitry Andric   printMachOFileHeader(&Obj);
102505f757f3fSDimitry Andric   if (!FirstPrivateHeader)
1025106c3fb27SDimitry Andric     printMachOLoadCommands(&Obj);
1025206c3fb27SDimitry Andric }
1025306c3fb27SDimitry Andric 
printMachOLoadCommands(const object::ObjectFile * Obj)102545ffd83dbSDimitry Andric void objdump::printMachOLoadCommands(const object::ObjectFile *Obj) {
1025504eeddc0SDimitry Andric   const MachOObjectFile *file = cast<const MachOObjectFile>(Obj);
102560b57cec5SDimitry Andric   uint32_t filetype = 0;
102570b57cec5SDimitry Andric   uint32_t cputype = 0;
102580b57cec5SDimitry Andric   if (file->is64Bit()) {
102590b57cec5SDimitry Andric     MachO::mach_header_64 H_64;
102600b57cec5SDimitry Andric     H_64 = file->getHeader64();
102610b57cec5SDimitry Andric     filetype = H_64.filetype;
102620b57cec5SDimitry Andric     cputype = H_64.cputype;
102630b57cec5SDimitry Andric   } else {
102640b57cec5SDimitry Andric     MachO::mach_header H;
102650b57cec5SDimitry Andric     H = file->getHeader();
102660b57cec5SDimitry Andric     filetype = H.filetype;
102670b57cec5SDimitry Andric     cputype = H.cputype;
102680b57cec5SDimitry Andric   }
10269fe6060f1SDimitry Andric   PrintLoadCommands(file, filetype, cputype, Verbose);
102700b57cec5SDimitry Andric }
102710b57cec5SDimitry Andric 
102720b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
102730b57cec5SDimitry Andric // export trie dumping
102740b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
102750b57cec5SDimitry Andric 
printMachOExportsTrie(const object::MachOObjectFile * Obj)102765ffd83dbSDimitry Andric static void printMachOExportsTrie(const object::MachOObjectFile *Obj) {
102770b57cec5SDimitry Andric   uint64_t BaseSegmentAddress = 0;
102780b57cec5SDimitry Andric   for (const auto &Command : Obj->load_commands()) {
102790b57cec5SDimitry Andric     if (Command.C.cmd == MachO::LC_SEGMENT) {
102800b57cec5SDimitry Andric       MachO::segment_command Seg = Obj->getSegmentLoadCommand(Command);
102810b57cec5SDimitry Andric       if (Seg.fileoff == 0 && Seg.filesize != 0) {
102820b57cec5SDimitry Andric         BaseSegmentAddress = Seg.vmaddr;
102830b57cec5SDimitry Andric         break;
102840b57cec5SDimitry Andric       }
102850b57cec5SDimitry Andric     } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
102860b57cec5SDimitry Andric       MachO::segment_command_64 Seg = Obj->getSegment64LoadCommand(Command);
102870b57cec5SDimitry Andric       if (Seg.fileoff == 0 && Seg.filesize != 0) {
102880b57cec5SDimitry Andric         BaseSegmentAddress = Seg.vmaddr;
102890b57cec5SDimitry Andric         break;
102900b57cec5SDimitry Andric       }
102910b57cec5SDimitry Andric     }
102920b57cec5SDimitry Andric   }
102930b57cec5SDimitry Andric   Error Err = Error::success();
102940b57cec5SDimitry Andric   for (const object::ExportEntry &Entry : Obj->exports(Err)) {
102950b57cec5SDimitry Andric     uint64_t Flags = Entry.flags();
102960b57cec5SDimitry Andric     bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
102970b57cec5SDimitry Andric     bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
102980b57cec5SDimitry Andric     bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
102990b57cec5SDimitry Andric                         MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
103000b57cec5SDimitry Andric     bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
103010b57cec5SDimitry Andric                 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
103020b57cec5SDimitry Andric     bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
103030b57cec5SDimitry Andric     if (ReExport)
103040b57cec5SDimitry Andric       outs() << "[re-export] ";
103050b57cec5SDimitry Andric     else
103060b57cec5SDimitry Andric       outs() << format("0x%08llX  ",
103070b57cec5SDimitry Andric                        Entry.address() + BaseSegmentAddress);
103080b57cec5SDimitry Andric     outs() << Entry.name();
103090b57cec5SDimitry Andric     if (WeakDef || ThreadLocal || Resolver || Abs) {
10310fe6060f1SDimitry Andric       ListSeparator LS;
103110b57cec5SDimitry Andric       outs() << " [";
10312fe6060f1SDimitry Andric       if (WeakDef)
10313fe6060f1SDimitry Andric         outs() << LS << "weak_def";
10314fe6060f1SDimitry Andric       if (ThreadLocal)
10315fe6060f1SDimitry Andric         outs() << LS << "per-thread";
10316fe6060f1SDimitry Andric       if (Abs)
10317fe6060f1SDimitry Andric         outs() << LS << "absolute";
10318fe6060f1SDimitry Andric       if (Resolver)
10319fe6060f1SDimitry Andric         outs() << LS << format("resolver=0x%08llX", Entry.other());
103200b57cec5SDimitry Andric       outs() << "]";
103210b57cec5SDimitry Andric     }
103220b57cec5SDimitry Andric     if (ReExport) {
103230b57cec5SDimitry Andric       StringRef DylibName = "unknown";
103240b57cec5SDimitry Andric       int Ordinal = Entry.other() - 1;
103250b57cec5SDimitry Andric       Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
103260b57cec5SDimitry Andric       if (Entry.otherName().empty())
103270b57cec5SDimitry Andric         outs() << " (from " << DylibName << ")";
103280b57cec5SDimitry Andric       else
103290b57cec5SDimitry Andric         outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
103300b57cec5SDimitry Andric     }
103310b57cec5SDimitry Andric     outs() << "\n";
103320b57cec5SDimitry Andric   }
103330b57cec5SDimitry Andric   if (Err)
103348bcb0991SDimitry Andric     reportError(std::move(Err), Obj->getFileName());
103350b57cec5SDimitry Andric }
103360b57cec5SDimitry Andric 
103370b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
103380b57cec5SDimitry Andric // rebase table dumping
103390b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
103400b57cec5SDimitry Andric 
printMachORebaseTable(object::MachOObjectFile * Obj)103415ffd83dbSDimitry Andric static void printMachORebaseTable(object::MachOObjectFile *Obj) {
103420b57cec5SDimitry Andric   outs() << "segment  section            address     type\n";
103430b57cec5SDimitry Andric   Error Err = Error::success();
103440b57cec5SDimitry Andric   for (const object::MachORebaseEntry &Entry : Obj->rebaseTable(Err)) {
103450b57cec5SDimitry Andric     StringRef SegmentName = Entry.segmentName();
103460b57cec5SDimitry Andric     StringRef SectionName = Entry.sectionName();
103470b57cec5SDimitry Andric     uint64_t Address = Entry.address();
103480b57cec5SDimitry Andric 
103490b57cec5SDimitry Andric     // Table lines look like: __DATA  __nl_symbol_ptr  0x0000F00C  pointer
103500b57cec5SDimitry Andric     outs() << format("%-8s %-18s 0x%08" PRIX64 "  %s\n",
103510b57cec5SDimitry Andric                      SegmentName.str().c_str(), SectionName.str().c_str(),
103520b57cec5SDimitry Andric                      Address, Entry.typeName().str().c_str());
103530b57cec5SDimitry Andric   }
103540b57cec5SDimitry Andric   if (Err)
103558bcb0991SDimitry Andric     reportError(std::move(Err), Obj->getFileName());
103560b57cec5SDimitry Andric }
103570b57cec5SDimitry Andric 
ordinalName(const object::MachOObjectFile * Obj,int Ordinal)103580b57cec5SDimitry Andric static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
103590b57cec5SDimitry Andric   StringRef DylibName;
103600b57cec5SDimitry Andric   switch (Ordinal) {
103610b57cec5SDimitry Andric   case MachO::BIND_SPECIAL_DYLIB_SELF:
103620b57cec5SDimitry Andric     return "this-image";
103630b57cec5SDimitry Andric   case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
103640b57cec5SDimitry Andric     return "main-executable";
103650b57cec5SDimitry Andric   case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
103660b57cec5SDimitry Andric     return "flat-namespace";
10367bdd1243dSDimitry Andric   case MachO::BIND_SPECIAL_DYLIB_WEAK_LOOKUP:
10368bdd1243dSDimitry Andric     return "weak";
103690b57cec5SDimitry Andric   default:
103700b57cec5SDimitry Andric     if (Ordinal > 0) {
103710b57cec5SDimitry Andric       std::error_code EC =
103720b57cec5SDimitry Andric           Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
103730b57cec5SDimitry Andric       if (EC)
103740b57cec5SDimitry Andric         return "<<bad library ordinal>>";
103750b57cec5SDimitry Andric       return DylibName;
103760b57cec5SDimitry Andric     }
103770b57cec5SDimitry Andric   }
103780b57cec5SDimitry Andric   return "<<unknown special ordinal>>";
103790b57cec5SDimitry Andric }
103800b57cec5SDimitry Andric 
103810b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
103820b57cec5SDimitry Andric // bind table dumping
103830b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
103840b57cec5SDimitry Andric 
printMachOBindTable(object::MachOObjectFile * Obj)103855ffd83dbSDimitry Andric static void printMachOBindTable(object::MachOObjectFile *Obj) {
103860b57cec5SDimitry Andric   // Build table of sections so names can used in final output.
103870b57cec5SDimitry Andric   outs() << "segment  section            address    type       "
103880b57cec5SDimitry Andric             "addend dylib            symbol\n";
103890b57cec5SDimitry Andric   Error Err = Error::success();
103900b57cec5SDimitry Andric   for (const object::MachOBindEntry &Entry : Obj->bindTable(Err)) {
103910b57cec5SDimitry Andric     StringRef SegmentName = Entry.segmentName();
103920b57cec5SDimitry Andric     StringRef SectionName = Entry.sectionName();
103930b57cec5SDimitry Andric     uint64_t Address = Entry.address();
103940b57cec5SDimitry Andric 
103950b57cec5SDimitry Andric     // Table lines look like:
103960b57cec5SDimitry Andric     //  __DATA  __got  0x00012010    pointer   0 libSystem ___stack_chk_guard
103970b57cec5SDimitry Andric     StringRef Attr;
103980b57cec5SDimitry Andric     if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
103990b57cec5SDimitry Andric       Attr = " (weak_import)";
104000b57cec5SDimitry Andric     outs() << left_justify(SegmentName, 8) << " "
104010b57cec5SDimitry Andric            << left_justify(SectionName, 18) << " "
104020b57cec5SDimitry Andric            << format_hex(Address, 10, true) << " "
104030b57cec5SDimitry Andric            << left_justify(Entry.typeName(), 8) << " "
104040b57cec5SDimitry Andric            << format_decimal(Entry.addend(), 8) << " "
104050b57cec5SDimitry Andric            << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
104060b57cec5SDimitry Andric            << Entry.symbolName() << Attr << "\n";
104070b57cec5SDimitry Andric   }
104080b57cec5SDimitry Andric   if (Err)
104098bcb0991SDimitry Andric     reportError(std::move(Err), Obj->getFileName());
104100b57cec5SDimitry Andric }
104110b57cec5SDimitry Andric 
104120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
104130b57cec5SDimitry Andric // lazy bind table dumping
104140b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
104150b57cec5SDimitry Andric 
printMachOLazyBindTable(object::MachOObjectFile * Obj)104165ffd83dbSDimitry Andric static void printMachOLazyBindTable(object::MachOObjectFile *Obj) {
104170b57cec5SDimitry Andric   outs() << "segment  section            address     "
104180b57cec5SDimitry Andric             "dylib            symbol\n";
104190b57cec5SDimitry Andric   Error Err = Error::success();
104200b57cec5SDimitry Andric   for (const object::MachOBindEntry &Entry : Obj->lazyBindTable(Err)) {
104210b57cec5SDimitry Andric     StringRef SegmentName = Entry.segmentName();
104220b57cec5SDimitry Andric     StringRef SectionName = Entry.sectionName();
104230b57cec5SDimitry Andric     uint64_t Address = Entry.address();
104240b57cec5SDimitry Andric 
104250b57cec5SDimitry Andric     // Table lines look like:
104260b57cec5SDimitry Andric     //  __DATA  __got  0x00012010 libSystem ___stack_chk_guard
104270b57cec5SDimitry Andric     outs() << left_justify(SegmentName, 8) << " "
104280b57cec5SDimitry Andric            << left_justify(SectionName, 18) << " "
104290b57cec5SDimitry Andric            << format_hex(Address, 10, true) << " "
104300b57cec5SDimitry Andric            << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
104310b57cec5SDimitry Andric            << Entry.symbolName() << "\n";
104320b57cec5SDimitry Andric   }
104330b57cec5SDimitry Andric   if (Err)
104348bcb0991SDimitry Andric     reportError(std::move(Err), Obj->getFileName());
104350b57cec5SDimitry Andric }
104360b57cec5SDimitry Andric 
104370b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
104380b57cec5SDimitry Andric // weak bind table dumping
104390b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
104400b57cec5SDimitry Andric 
printMachOWeakBindTable(object::MachOObjectFile * Obj)104415ffd83dbSDimitry Andric static void printMachOWeakBindTable(object::MachOObjectFile *Obj) {
104420b57cec5SDimitry Andric   outs() << "segment  section            address     "
104430b57cec5SDimitry Andric             "type       addend   symbol\n";
104440b57cec5SDimitry Andric   Error Err = Error::success();
104450b57cec5SDimitry Andric   for (const object::MachOBindEntry &Entry : Obj->weakBindTable(Err)) {
104460b57cec5SDimitry Andric     // Strong symbols don't have a location to update.
104470b57cec5SDimitry Andric     if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
104480b57cec5SDimitry Andric       outs() << "                                        strong              "
104490b57cec5SDimitry Andric              << Entry.symbolName() << "\n";
104500b57cec5SDimitry Andric       continue;
104510b57cec5SDimitry Andric     }
104520b57cec5SDimitry Andric     StringRef SegmentName = Entry.segmentName();
104530b57cec5SDimitry Andric     StringRef SectionName = Entry.sectionName();
104540b57cec5SDimitry Andric     uint64_t Address = Entry.address();
104550b57cec5SDimitry Andric 
104560b57cec5SDimitry Andric     // Table lines look like:
104570b57cec5SDimitry Andric     // __DATA  __data  0x00001000  pointer    0   _foo
104580b57cec5SDimitry Andric     outs() << left_justify(SegmentName, 8) << " "
104590b57cec5SDimitry Andric            << left_justify(SectionName, 18) << " "
104600b57cec5SDimitry Andric            << format_hex(Address, 10, true) << " "
104610b57cec5SDimitry Andric            << left_justify(Entry.typeName(), 8) << " "
104620b57cec5SDimitry Andric            << format_decimal(Entry.addend(), 8) << "   " << Entry.symbolName()
104630b57cec5SDimitry Andric            << "\n";
104640b57cec5SDimitry Andric   }
104650b57cec5SDimitry Andric   if (Err)
104668bcb0991SDimitry Andric     reportError(std::move(Err), Obj->getFileName());
104670b57cec5SDimitry Andric }
104680b57cec5SDimitry Andric 
104690b57cec5SDimitry Andric // get_dyld_bind_info_symbolname() is used for disassembly and passed an
104700b57cec5SDimitry Andric // address, ReferenceValue, in the Mach-O file and looks in the dyld bind
104710b57cec5SDimitry Andric // information for that address. If the address is found its binding symbol
104720b57cec5SDimitry Andric // name is returned.  If not nullptr is returned.
get_dyld_bind_info_symbolname(uint64_t ReferenceValue,struct DisassembleInfo * info)104730b57cec5SDimitry Andric static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
104740b57cec5SDimitry Andric                                                  struct DisassembleInfo *info) {
104750b57cec5SDimitry Andric   if (info->bindtable == nullptr) {
104768bcb0991SDimitry Andric     info->bindtable = std::make_unique<SymbolAddressMap>();
104770b57cec5SDimitry Andric     Error Err = Error::success();
104780b57cec5SDimitry Andric     for (const object::MachOBindEntry &Entry : info->O->bindTable(Err)) {
104790b57cec5SDimitry Andric       uint64_t Address = Entry.address();
104800b57cec5SDimitry Andric       StringRef name = Entry.symbolName();
104810b57cec5SDimitry Andric       if (!name.empty())
104820b57cec5SDimitry Andric         (*info->bindtable)[Address] = name;
104830b57cec5SDimitry Andric     }
104840b57cec5SDimitry Andric     if (Err)
104858bcb0991SDimitry Andric       reportError(std::move(Err), info->O->getFileName());
104860b57cec5SDimitry Andric   }
104870b57cec5SDimitry Andric   auto name = info->bindtable->lookup(ReferenceValue);
104880b57cec5SDimitry Andric   return !name.empty() ? name.data() : nullptr;
104890b57cec5SDimitry Andric }
104900b57cec5SDimitry Andric 
printLazyBindTable(ObjectFile * o)104915ffd83dbSDimitry Andric void objdump::printLazyBindTable(ObjectFile *o) {
10492fe6060f1SDimitry Andric   outs() << "\nLazy bind table:\n";
104930b57cec5SDimitry Andric   if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
104940b57cec5SDimitry Andric     printMachOLazyBindTable(MachO);
104950b57cec5SDimitry Andric   else
104960b57cec5SDimitry Andric     WithColor::error()
104970b57cec5SDimitry Andric         << "This operation is only currently supported "
104980b57cec5SDimitry Andric            "for Mach-O executable files.\n";
104990b57cec5SDimitry Andric }
105000b57cec5SDimitry Andric 
printWeakBindTable(ObjectFile * o)105015ffd83dbSDimitry Andric void objdump::printWeakBindTable(ObjectFile *o) {
10502fe6060f1SDimitry Andric   outs() << "\nWeak bind table:\n";
105030b57cec5SDimitry Andric   if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
105040b57cec5SDimitry Andric     printMachOWeakBindTable(MachO);
105050b57cec5SDimitry Andric   else
105060b57cec5SDimitry Andric     WithColor::error()
105070b57cec5SDimitry Andric         << "This operation is only currently supported "
105080b57cec5SDimitry Andric            "for Mach-O executable files.\n";
105090b57cec5SDimitry Andric }
105100b57cec5SDimitry Andric 
printExportsTrie(const ObjectFile * o)105115ffd83dbSDimitry Andric void objdump::printExportsTrie(const ObjectFile *o) {
10512fe6060f1SDimitry Andric   outs() << "\nExports trie:\n";
105130b57cec5SDimitry Andric   if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
105140b57cec5SDimitry Andric     printMachOExportsTrie(MachO);
105150b57cec5SDimitry Andric   else
105160b57cec5SDimitry Andric     WithColor::error()
105170b57cec5SDimitry Andric         << "This operation is only currently supported "
105180b57cec5SDimitry Andric            "for Mach-O executable files.\n";
105190b57cec5SDimitry Andric }
105200b57cec5SDimitry Andric 
printRebaseTable(ObjectFile * o)105215ffd83dbSDimitry Andric void objdump::printRebaseTable(ObjectFile *o) {
10522fe6060f1SDimitry Andric   outs() << "\nRebase table:\n";
105230b57cec5SDimitry Andric   if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
105240b57cec5SDimitry Andric     printMachORebaseTable(MachO);
105250b57cec5SDimitry Andric   else
105260b57cec5SDimitry Andric     WithColor::error()
105270b57cec5SDimitry Andric         << "This operation is only currently supported "
105280b57cec5SDimitry Andric            "for Mach-O executable files.\n";
105290b57cec5SDimitry Andric }
105300b57cec5SDimitry Andric 
printBindTable(ObjectFile * o)105315ffd83dbSDimitry Andric void objdump::printBindTable(ObjectFile *o) {
10532fe6060f1SDimitry Andric   outs() << "\nBind table:\n";
105330b57cec5SDimitry Andric   if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
105340b57cec5SDimitry Andric     printMachOBindTable(MachO);
105350b57cec5SDimitry Andric   else
105360b57cec5SDimitry Andric     WithColor::error()
105370b57cec5SDimitry Andric         << "This operation is only currently supported "
105380b57cec5SDimitry Andric            "for Mach-O executable files.\n";
105390b57cec5SDimitry Andric }
10540